Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisman committed Nov 22, 2024
1 parent 2a60fd7 commit 0ba15e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 64 deletions.
2 changes: 2 additions & 0 deletions ts/packages/actionSchema/src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
SchemaTypeUnion,
} from "./type.js";

export function string(): SchemaTypeString;
export function string(...union: (string | string[])[]): SchemaTypeStringUnion;
export function string(
...union: (string | string[])[]
): SchemaTypeString | SchemaTypeStringUnion {
Expand Down
30 changes: 19 additions & 11 deletions ts/packages/actionSchema/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,40 @@ function createActionSchemaFile(
strict: boolean,
): ActionSchemaFile {
if (strict && !entry.exported) {
throw new Error(`Type ${entry.name} must be exported`);
throw new Error(
`Schema Error: ${schemaName}: Type ${entry.name} must be exported`,
);
}

const pending: SchemaTypeDefinition[] = [entry];
const actionSchemas: [string, ActionSchemaTypeDefinition][] = [];
const actionSchemas = new Map<string, ActionSchemaTypeDefinition>();
while (pending.length > 0) {
const current = pending.shift()!;
switch (current.type.type) {
case "object":
actionSchemas.push(checkActionSchema(current));
const [actionName, actionSchema] = checkActionSchema(current);
if (actionSchemas.get(actionName)) {
throw new Error(
`Schema Error: ${schemaName}: Duplicate action name '${actionName}'`,
);
}
actionSchemas.set(actionName, actionSchema);
break;
case "type-union":
if (strict && current.comments) {
throw new Error(
`Schema Error: entry type comments for '${current.name}' are not supported`,
`Schema Error: ${schemaName}: entry type comments for '${current.name}' are not supported`,
);
}
for (const t of current.type.types) {
if (t.type !== "type-reference") {
throw new Error(
"Schema Error: expected type reference in the entry type union",
`Schema Error: ${schemaName}: expected type reference in the entry type union`,
);
}
if (t.definition === undefined) {
throw new Error(
`Schema Error: unresolved type reference '${t.name}' in the entry type union`,
`Schema Error: ${schemaName}: unresolved type reference '${t.name}' in the entry type union`,
);
}
pending.push(t.definition);
Expand All @@ -103,29 +111,29 @@ function createActionSchemaFile(
// Definition that references another type is the same as a union type with a single type.
if (strict && current.comments) {
throw new Error(
`Schema Error: entry type comments for '${current.name} are not supported`,
`Schema Error: ${schemaName}: entry type comments for '${current.name} are not supported`,
);
}
if (current.type.definition === undefined) {
throw new Error(
`Schema Error: unresolved type reference '${current.type.name}' in the entry type union`,
`Schema Error: ${schemaName}: unresolved type reference '${current.type.name}' in the entry type union`,
);
}
pending.push(current.type.definition);
break;
default:
throw new Error(
`Schema Error: invalid type ${current.type.type} in action schema type ${current.name}`,
`Schema Error: ${schemaName}: invalid type ${current.type.type} in action schema type ${current.name}`,
);
}
}
if (actionSchemas.length === 0) {
if (actionSchemas.size === 0) {
throw new Error("No action schema found");
}
return {
entry: entry as ActionSchemaEntryTypeDefinition,
schemaName,
actionSchemas: new Map(actionSchemas),
actionSchemas,
order,
};
}
Expand Down
6 changes: 5 additions & 1 deletion ts/packages/actionSchema/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export type SchemaType =

export interface ActionSchemaObject extends SchemaTypeObject {
fields: {
actionName: SchemaObjectField<SchemaTypeString | SchemaTypeStringUnion>;
actionName: SchemaObjectField<SchemaTypeStringUnion>;
parameters: SchemaObjectField<
| SchemaTypeObject
| SchemaTypeReference<
Expand All @@ -132,8 +132,12 @@ type ActionSchemaTypeReference =
export type ActionSchemaUnion = SchemaTypeUnion<ActionSchemaTypeReference>;

export type ActionSchemaFile = {
// The entry type definition for the action schema.
entry: ActionSchemaEntryTypeDefinition;
// Map action name to action type definition
actionSchemas: Map<string, ActionSchemaTypeDefinition>;
// Schema name
schemaName?: string;
// Order for the type definitions
order?: Map<string, number>; // for exact regen
};
1 change: 0 additions & 1 deletion ts/packages/agents/player/src/trackFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export function parseFilter(filter: string): IFilterResult {
}
} else if (token.type === FilterTokenType.Value) {
if (!pendingConstraint) {
console.log(token.rawValue);
return {
diagnostics: [
"Unexpected: value without constraint prefix",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
createChangeAssistantActionSchema,
} from "./agentTranslators.js";
import { createMultipleActionSchema } from "./multipleActionSchema.js";
import { getPackageFilePath } from "../utils/getPackageFilePath.js";

function createActionSchemaJsonValidator<T extends TranslatedAction>(
actionSchemaFile: ActionSchemaFile,
Expand Down Expand Up @@ -58,36 +59,14 @@ function createActionSchemaJsonValidator<T extends TranslatedAction>(
};
}

export function loadActionSchemas(
typeName: string,
schemas: TranslatorSchemaDef[],
): ActionSchemaFile {
const schema = composeTranslatorSchemas(typeName, schemas);
const translatorName = "";
return parseActionSchemaSource(schema, translatorName, typeName);
}

export function createActionJsonTranslatorFromSchemaDef<
T extends TranslatedAction,
>(
typeName: string,
schemas: string | TranslatorSchemaDef[],
actionSchemaFile: ActionSchemaFile,
options?: JsonTranslatorOptions<T>,
) {
const actionSchemas = loadActionSchemas(
typeName,
Array.isArray(schemas)
? schemas
: [
{
kind: "inline",
typeName,
schema: schemas,
},
],
);

const validator = createActionSchemaJsonValidator<T>(actionSchemas);
const validator = createActionSchemaJsonValidator<T>(actionSchemaFile);

return createJsonTranslatorWithValidator(
typeName.toLowerCase(),
Expand All @@ -103,7 +82,7 @@ class ActionSchemaBuilder {
addActionConfig(...configs: ActionConfig[]) {
for (const config of configs) {
const actionSchemaFile = parseActionSchemaFile(
config.schemaFile,
getPackageFilePath(config.schemaFile),
config.schemaName,
config.schemaType,
);
Expand All @@ -129,15 +108,18 @@ class ActionSchemaBuilder {
if (file.order) {
const base = order.size;
for (const [name, num] of file.order) {
if (order.has(name)) {
throw new Error(
`Schema Builder Error: duplicate type definition '${name}'`,
);
}
order.set(name, base + num);
}
}
}

const actionSchemas: [string, ActionSchemaTypeDefinition][] = [];
const pending: ActionSchemaEntryTypeDefinition[] = [
...this.definitions,
];
const actionSchemas = new Map<string, ActionSchemaTypeDefinition>();
const pending = [...this.definitions];
while (pending.length > 0) {
const current = pending.shift()!;
const currentType = current.type;
Expand All @@ -161,22 +143,25 @@ class ActionSchemaBuilder {
pending.push(currentType.definition);
break;
case "object":
actionSchemas.push([
current.name,
const actionName =
currentType.fields.actionName.type.typeEnum[0];
if (actionSchemas.get(actionName)) {
throw new Error(
`Schema Builder Error: duplicate action name '${actionName}'`,
);
}
actionSchemas.set(
actionName,
current as ActionSchemaTypeDefinition,
]);
);
break;
default:
// Should not reach here.
throw new Error("Invalid type");
}
}

return {
entry,
actionSchemas: new Map(actionSchemas),
order,
};
return { entry, actionSchemas, order };
}
}

Expand Down
36 changes: 22 additions & 14 deletions ts/packages/dispatcher/src/translation/agentTranslators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,28 @@ export function loadAgentJsonTranslator<
): TypeAgentTranslator<T> {
const translatorConfig = provider.getTranslatorConfig(translatorName);

const createTranslator = regenerateSchema
? createActionJsonTranslatorFromSchemaDef<T>
: createJsonTranslatorFromSchemaDef<T>;
const translator = createTranslator(
"AllActions",
getTranslatorSchemaDefs(
translatorConfig,
translatorName,
provider,
activeTranslators,
multipleActions,
),
{ model },
);
const translator = regenerateSchema
? createActionJsonTranslatorFromSchemaDef<T>(
"AllActions",
composeActionSchema(
translatorName,
provider,
activeTranslators,
multipleActions,
),
{ model },
)
: createJsonTranslatorFromSchemaDef<T>(
"AllActions",
getTranslatorSchemaDefs(
translatorConfig,
translatorName,
provider,
activeTranslators,
multipleActions,
),
{ model },
);

const streamingTranslator = enableJsonTranslatorStreaming(translator);

Expand Down

0 comments on commit 0ba15e9

Please sign in to comment.