Files
Draupnir/packages/interface-manager/src/Command/PresentationSchema.ts
T
Gnuxie 879e822332 Monoreponir (#1046)
* Move src to apps/draupnir/src

https://github.com/the-draupnir-project/planning/issues/100

* Move package.json

https://github.com/the-draupnir-project/planning/issues/100

* Add matrix-basic-types to monorepo.

Get everything working including linting and prettier :3

https://github.com/the-draupnir-project/planning/issues/100

* Add interface-manager to monorepo.

This was a bitch because apparently we forgot to delete node_modules
before creating the workspace package.json. So it had linked a bunch
of local stuff like was in node_modules for Draupnir...

Anyways i think we're still on track.

https://github.com/the-draupnir-project/planning/issues/100

* idk why there are prettier changes in apps but there are.

* Add matrix-protection-suite to monorepo.

https://github.com/the-draupnir-project/planning/issues/100

* Add matrix-protection-suite-for-matrix-bot-sdk

https://github.com/the-draupnir-project/planning/issues/100

We will need to add the real upstreams and versions and remove the
file links as we publish the packages.

* Move mps-interface-adaptor into monorepo

https://github.com/the-draupnir-project/planning/issues/100

Wohoo, i think only draupnir is left now?

* Move Draupnir test files to draupnir directory smh smh smh.

https://github.com/the-draupnir-project/planning/issues/100

* Fix typescript config for tests and eslint.

Now we get proper linting and type checking of tests.

https://github.com/the-draupnir-project/planning/issues/100

* WIP Integrating draupnir into monorepo tooling...

https://github.com/the-draupnir-project/planning/issues/100

We need to stop aliasing bot-sdk but we should first check that
upstream is using a consistent name too.

* Remove matrix-bot-sdk alias for vector fork.

https://github.com/the-draupnir-project/planning/issues/100

* Add top command description type and weave through API.

A more recent version of typescript meant that the exectutor's
contravariance got checked which destroyed the API so we had to make a
top type for command descriptions and parametrise some of the API.

https://github.com/the-draupnir-project/planning/issues/100

* Fix typescript errors related to class property initialisation changes.

https://www.typescriptlang.org/tsconfig/#useDefineForClassFields

Seems like they were using defineProperty before which meant
properites were initialised after the constructor ran.

Honestly i like that more but we're going to stick with what they
intend to be the default.

https://github.com/the-draupnir-project/planning/issues/100

* Fix tests lacking fixtures context.

https://github.com/the-draupnir-project/planning/issues/100

* Fix typescript errors related to error destructuring in tests.

https://github.com/the-draupnir-project/planning/issues/100

* Pin postgres package to workaround upstream issue

https://github.com/porsager/postgres/issues/1150
Documented in DEPENDENCIES.md

https://github.com/the-draupnir-project/planning/issues/100

* Fix contravariance issue in hash store helper.

Part of the TS 5.9 upgrade fallout.
https://github.com/the-draupnir-project/planning/issues/100

* Fix minor typescript 5.9 migration issuess

All typescript errors finished, yay.

* Fix REUSE missing headers.

* Fix assets script in draupnir app.

* Add Draupnir to eslint scope

* Remove the appservice web API.

There are too many eslint errors here to do with unsafe parsing of
properties from the body etc. And there's actually no consumers to
this API. It's also a widget API, and all it does is provision the bot
and nothing more.

* Fix eslint config for DeadDocumentJSX.

It wasn't working well with the jsx templates.
We should probably delete the tsconfig.eslint.json shite now.

* Update src/utils.ts for eslint.

This shit is legacy i hate it.

* Fix eslint errors in config.

Really this is paint over rot since the config doesn't have a schema,
and we can't really make one either.

* Fix eslint issues in ReportManager.

This code is diabolical. It hasn't really been fixed that will take
refactoring and making sure people don't write this sorts of bad code
ever again. Which thankfully we have process in place for.

* Fix clientHelper eslint issues.

* Fix eslint for ImportCommand.

* Grinding eslint fml.

* Fix miscellaneous eslint issues.

* allow no-deprecate for logMessage.

shit's being annoying.

* Fix remaining eslint issues...

We also deleted one of the scripts used to evaluate the performance of
various endpoints, which we were not using.

* Give bot toggle asyncDispose for code consistency.

* Fix package.json access issues.

* Adjust Docker and CI for new app location in monorepo.

* Fix broken integration tests.

* Remove prepare script from matrix-protection-suite package.

Isn't needed anymore


* Fix build:all script missing base files.

* Remove test script from matrix-protection-suite-for-matrix-bot-sdk

It doesn't have any tests :/

* Order of setup is wrong in integration test workflows.

* Fix mps interface adaptor doesn't have any tests.

* Fix appservice registration for test harness.

* Fix matrix-basic-types jest configuration

* Fix no build step in mjolnir.yaml

* Transfer common dev dependencies to the workspace root.


They were just wrong.
2026-03-19 16:13:14 +00:00

185 lines
5.8 KiB
TypeScript

// Copyright 2024 Gnuxie <Gnuxie@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from @the-draupnir-project/interface-manager
// https://github.com/the-draupnir-project/interface-manager
// </text>
import {
ObjectTypeFromPresentationType,
Presentation,
PresentationTypeWithoutWrap,
} from "./Presentation";
import { CommandTable } from "./CommandTable";
export enum PresentationSchemaType {
Single = "Single",
Union = "Union",
Top = "Top",
}
type ObjectTypeForSingleSchema<T extends SinglePresentationSchema> =
T["presentationType"] extends PresentationTypeWithoutWrap<infer ObjectType>
? ObjectType
: never;
type ObjectTypeForUnionSchema<T extends UnionPresentationSchema> =
T["variants"] extends PresentationTypeWithoutWrap<infer ObjectType>[]
? ObjectType
: never;
type ObjectTypeForTopSchema = unknown;
/**
* There is something wrong with the way argument parsing code is using validators
* of presnetation types. When a parameter has declared that it expects arguments
* to be of a presnetation type, all we should be doing is checking that the
* presentationType of the argument is the specified presentation type.
*
* This works, except what happens when we want a command that accepts a union
* of presentation types, or maybe the union of all known presentation types?
*
* Well that is probably an issue for the parameter description code right?
* It has to specify a schema for the argument to get the presentation types it
* expects, rather than just a presentation type.
*
* So is born the presentation schema.
*/
export type SinglePresentationSchema<ObjectType = unknown> = {
readonly schemaType: PresentationSchemaType.Single;
readonly presentationType: PresentationTypeWithoutWrap<ObjectType>;
};
export type UnionPresentationSchema<ObjectType = unknown> = {
readonly schemaType: PresentationSchemaType.Union;
readonly variants: PresentationTypeWithoutWrap<ObjectType>[];
};
export type TopPresentationSchema = {
readonly schemaType: PresentationSchemaType.Top;
};
export const TopPresentationSchema: TopPresentationSchema = Object.freeze({
schemaType: PresentationSchemaType.Top,
});
export type PresentationSchema<ObjectType = unknown> =
| SinglePresentationSchema<ObjectType>
| UnionPresentationSchema<ObjectType>
| TopPresentationSchema;
export type ObjectTypeForPresentationSchema<T> =
T extends SinglePresentationSchema
? ObjectTypeForSingleSchema<T>
: T extends UnionPresentationSchema
? ObjectTypeForUnionSchema<T>
: T extends TopPresentationSchema
? ObjectTypeForTopSchema
: never;
export function checkPresentationSchema<ObjectType>(
schema: PresentationSchema,
presentation: Presentation
): presentation is Presentation<ObjectType> {
switch (schema.schemaType) {
case PresentationSchemaType.Single:
return presentation.presentationType === schema.presentationType;
case PresentationSchemaType.Union:
return Boolean(
schema.variants.find(
(presentationType) =>
presentation.presentationType === presentationType
)
);
case PresentationSchemaType.Top:
return true;
}
}
export function acceptPresentation<ObjectType>(
schema: PresentationSchema<ObjectType>,
commandTable: CommandTable,
presentation: Presentation
): Presentation<ObjectType> | undefined {
if (checkPresentationSchema<ObjectType>(schema, presentation)) {
return presentation;
} else if (schema.schemaType === PresentationSchemaType.Single) {
const translator = commandTable.findPresentationTypeTranslator(
schema.presentationType,
presentation.presentationType
);
if (translator) {
return translator.translate(presentation) as Presentation<ObjectType>;
} else {
return undefined;
}
} else if (schema.schemaType === PresentationSchemaType.Union) {
for (const variant of schema.variants) {
const result = acceptPresentation(
{
schemaType: PresentationSchemaType.Single,
presentationType: variant,
},
commandTable,
presentation
);
if (result !== undefined) {
return result;
}
}
return undefined;
}
throw new TypeError(`The code is wrong`);
}
export function printPresentationSchema(schema: PresentationSchema): string {
switch (schema.schemaType) {
case PresentationSchemaType.Single:
return schema.presentationType.name;
case PresentationSchemaType.Union:
return schema.variants.map((type) => type.name).join(" | ");
case PresentationSchemaType.Top:
return `TopPresentationSchema`;
}
}
type Acceptor<ObjectType = unknown> =
| PresentationSchema<ObjectType>
| PresentationTypeWithoutWrap<ObjectType>;
export type ObjectTypeFromAcceptor<T> = T extends PresentationTypeWithoutWrap
? ObjectTypeFromPresentationType<T>
: T extends PresentationSchema
? ObjectTypeForPresentationSchema<T>
: never;
type UnionOfObjectTypes<T extends Acceptor[]> = {
[P in keyof T]: T[P] extends Acceptor<infer U> ? U : never;
}[number];
export function union<
TAcceptor extends (PresentationTypeWithoutWrap | UnionPresentationSchema)[],
>(
...acceptors: TAcceptor
): UnionPresentationSchema<UnionOfObjectTypes<TAcceptor>> {
type PresentationTypeForUnion = PresentationTypeWithoutWrap<
UnionOfObjectTypes<TAcceptor>
>;
const presentationTypes = acceptors.reduce<PresentationTypeForUnion[]>(
(acc, acceptor) => {
if ("schemaType" in acceptor) {
acc.push(...(acceptor.variants as PresentationTypeForUnion[]));
} else {
acc.push(acceptor as PresentationTypeForUnion);
}
return acc;
},
[]
);
return {
schemaType: PresentationSchemaType.Union,
variants: presentationTypes,
} as UnionPresentationSchema<UnionOfObjectTypes<TAcceptor>>;
}