@modelprofile.com/flexharness 10.12.0 → 10.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/classes.codesandbox.d.ts +39 -0
- package/dist_ts/classes.codesandbox.js +539 -0
- package/dist_ts/classes.flexharness.d.ts +17 -0
- package/dist_ts/classes.flexharness.js +222 -8
- package/dist_ts/classes.toolcatalog.d.ts +90 -0
- package/dist_ts/classes.toolcatalog.js +1398 -0
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -1
- package/dist_ts/interfaces.d.ts +237 -3
- package/dist_ts/interfaces.js +50 -1
- package/dist_ts/plugins.d.ts +66 -1
- package/dist_ts/plugins.js +19 -1
- package/dist_ts/utils.harnessoptions.d.ts +7 -1
- package/dist_ts/utils.harnessoptions.js +48 -2
- package/dist_ts/utils.json.js +3 -1
- package/dist_ts/utils.schemadeclarations.d.ts +53 -0
- package/dist_ts/utils.schemadeclarations.js +315 -0
- package/dist_ts/utils.tooloutput.d.ts +2 -0
- package/dist_ts/utils.tooloutput.js +11 -1
- package/dist_ts_compaction/plugins.d.ts +3 -3
- package/dist_ts_compaction/plugins.js +3 -3
- package/dist_ts_compaction/smartagent.compaction.d.ts +10 -1
- package/dist_ts_compaction/smartagent.compaction.js +79 -38
- package/package.json +17 -4
- package/readme.md +1 -1
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/classes.codesandbox.ts +565 -0
- package/ts/classes.flexharness.ts +242 -7
- package/ts/classes.toolcatalog.ts +1512 -0
- package/ts/index.ts +1 -0
- package/ts/interfaces.ts +285 -3
- package/ts/plugins.ts +86 -0
- package/ts/readme.md +1 -1
- package/ts/utils.harnessoptions.ts +55 -0
- package/ts/utils.json.ts +2 -0
- package/ts/utils.schemadeclarations.ts +322 -0
- package/ts/utils.tooloutput.ts +9 -0
- package/ts_compaction/plugins.ts +3 -3
- package/ts_compaction/smartagent.compaction.ts +90 -34
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders JSON Schema as TypeScript declarations for the tool catalog. The output depends only on
|
|
3
|
+
* the schema, so the same schema always renders to the same bytes.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type TJsonSchemaNode = boolean | { [key: string]: unknown };
|
|
7
|
+
|
|
8
|
+
export interface ISchemaDeclarationContext {
|
|
9
|
+
/** The named type a `$ref` denotes, or undefined to inline the referenced schema. */
|
|
10
|
+
namedType(ref: string): string | undefined;
|
|
11
|
+
/** The schema a local `$ref` points to. */
|
|
12
|
+
resolve(ref: string): TJsonSchemaNode | undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface IRenderedDeclaration {
|
|
16
|
+
text: string;
|
|
17
|
+
/** Named types the text refers to, sorted. */
|
|
18
|
+
types: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const identifierPattern = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
22
|
+
const maxRenderDepth = 32;
|
|
23
|
+
const maxInlineObjectCharacters = 100;
|
|
24
|
+
const maxSafeInteger = Number.MAX_SAFE_INTEGER;
|
|
25
|
+
|
|
26
|
+
export const isSchemaRecord = (value: unknown): value is { [key: string]: unknown } =>
|
|
27
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
28
|
+
|
|
29
|
+
export const isIdentifier = (value: string): boolean => identifierPattern.test(value);
|
|
30
|
+
|
|
31
|
+
const literal = (value: unknown): string => {
|
|
32
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
33
|
+
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
34
|
+
if (typeof value === 'boolean' || value === null) return String(value);
|
|
35
|
+
return 'unknown';
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Reports whether `character` occurs outside brackets, braces, parentheses, string literals and
|
|
40
|
+
* comments.
|
|
41
|
+
*/
|
|
42
|
+
const hasTopLevel = (text: string, character: string): boolean => {
|
|
43
|
+
let depth = 0;
|
|
44
|
+
let quote: string | undefined;
|
|
45
|
+
for (let index = 0; index < text.length; index++) {
|
|
46
|
+
const current = text[index];
|
|
47
|
+
if (quote) {
|
|
48
|
+
if (current === '\\') index++;
|
|
49
|
+
else if (current === quote) quote = undefined;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (current === '/' && text[index + 1] === '*') {
|
|
53
|
+
const end = text.indexOf('*/', index + 2);
|
|
54
|
+
if (end === -1) return false;
|
|
55
|
+
index = end + 1;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (current === '"' || current === "'") quote = current;
|
|
59
|
+
else if (current === '{' || current === '[' || current === '(' || current === '<') depth++;
|
|
60
|
+
else if (current === '}' || current === ']' || current === ')' || current === '>') depth--;
|
|
61
|
+
else if (depth === 0 && current === character) return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const union = (parts: string[]): string => {
|
|
67
|
+
const unique = [...new Set(parts)];
|
|
68
|
+
if (unique.includes('unknown')) return 'unknown';
|
|
69
|
+
const withoutNever = unique.length > 1 ? unique.filter((part) => part !== 'never') : unique;
|
|
70
|
+
return withoutNever.join(' | ');
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const intersection = (parts: string[]): string => {
|
|
74
|
+
const unique = [...new Set(parts)].filter((part) => part !== 'unknown');
|
|
75
|
+
if (unique.length === 0) return 'unknown';
|
|
76
|
+
return unique.map((part) => hasTopLevel(part, '|') ? `(${part})` : part).join(' & ');
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const arrayOf = (element: string): string =>
|
|
80
|
+
hasTopLevel(element, '|') || hasTopLevel(element, '&') ? `(${element})[]` : `${element}[]`;
|
|
81
|
+
|
|
82
|
+
const propertyKey = (name: string): string => isIdentifier(name) ? name : JSON.stringify(name);
|
|
83
|
+
|
|
84
|
+
/** A JSDoc block at `indent`; a short single line stays on one line. */
|
|
85
|
+
export const docBlock = (text: string, indent: string): string => {
|
|
86
|
+
const lines = text.replace(/\*\//g, '*\\/').split('\n').map((line) => line.trimEnd());
|
|
87
|
+
if (lines.length === 1 && indent.length + lines[0].length <= 100) return `${indent}/** ${lines[0]} */\n`;
|
|
88
|
+
return `${indent}/**\n${lines.map((line) => line ? `${indent} * ${line}` : `${indent} *`).join('\n')}\n${indent} */\n`;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const countOf = (count: number, unit: string): string => `${count} ${unit}${count === 1 ? '' : 's'}`;
|
|
92
|
+
|
|
93
|
+
/** Constraints a TypeScript type cannot state, in a short readable form. */
|
|
94
|
+
const constraintsOf = (schema: { [key: string]: unknown }): string[] => {
|
|
95
|
+
const constraints: string[] = [];
|
|
96
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
97
|
+
if (types.includes('integer')) constraints.push('integer');
|
|
98
|
+
if (typeof schema.format === 'string') constraints.push(`format ${schema.format}`);
|
|
99
|
+
const bound = (value: unknown): value is number =>
|
|
100
|
+
typeof value === 'number' && Number.isFinite(value) && Math.abs(value) !== maxSafeInteger;
|
|
101
|
+
if (bound(schema.exclusiveMinimum)) constraints.push(`> ${schema.exclusiveMinimum}`);
|
|
102
|
+
else if (bound(schema.minimum)) constraints.push(`${schema.exclusiveMinimum === true ? '>' : '>='} ${schema.minimum}`);
|
|
103
|
+
if (bound(schema.exclusiveMaximum)) constraints.push(`< ${schema.exclusiveMaximum}`);
|
|
104
|
+
else if (bound(schema.maximum)) constraints.push(`${schema.exclusiveMaximum === true ? '<' : '<='} ${schema.maximum}`);
|
|
105
|
+
if (bound(schema.multipleOf)) constraints.push(`multiple of ${schema.multipleOf}`);
|
|
106
|
+
const range = (min: unknown, max: unknown, unit: string): void => {
|
|
107
|
+
const hasMin = bound(min) && min > 0;
|
|
108
|
+
const hasMax = bound(max);
|
|
109
|
+
if (hasMin && hasMax) constraints.push(min === max ? countOf(min, unit) : `${min}-${countOf(max, unit)}`);
|
|
110
|
+
else if (hasMin) constraints.push(`at least ${countOf(min, unit)}`);
|
|
111
|
+
else if (hasMax) constraints.push(`at most ${countOf(max, unit)}`);
|
|
112
|
+
};
|
|
113
|
+
range(schema.minLength, schema.maxLength, 'character');
|
|
114
|
+
range(schema.minItems, schema.maxItems, 'item');
|
|
115
|
+
if (schema.uniqueItems === true) constraints.push('unique items');
|
|
116
|
+
if (typeof schema.pattern === 'string') constraints.push(`pattern /${schema.pattern}/`);
|
|
117
|
+
if (schema.default !== undefined) {
|
|
118
|
+
const value = JSON.stringify(schema.default);
|
|
119
|
+
if (value !== undefined && value.length <= 60) constraints.push(`default ${value}`);
|
|
120
|
+
}
|
|
121
|
+
if (schema.deprecated === true) constraints.push('deprecated');
|
|
122
|
+
return constraints;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/** The documentation of a property: its description and the constraints its type cannot state. */
|
|
126
|
+
export const schemaDoc = (schema: TJsonSchemaNode, description?: string): string | undefined => {
|
|
127
|
+
if (!isSchemaRecord(schema)) return description?.trim() || undefined;
|
|
128
|
+
const text = (description ?? (typeof schema.description === 'string' ? schema.description : '')).trim();
|
|
129
|
+
const constraints = constraintsOf(schema);
|
|
130
|
+
if (constraints.length === 0) return text || undefined;
|
|
131
|
+
return text ? `${text} (${constraints.join(', ')})` : constraints.join(', ');
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export class SchemaDeclarationWriter {
|
|
135
|
+
private readonly used = new Set<string>();
|
|
136
|
+
private readonly inlining = new Set<string>();
|
|
137
|
+
|
|
138
|
+
constructor(private readonly context: ISchemaDeclarationContext) {}
|
|
139
|
+
|
|
140
|
+
public get usedTypes(): string[] {
|
|
141
|
+
return [...this.used].sort();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The TypeScript type of `schema`, with continuation lines indented relative to `indent`. */
|
|
145
|
+
public type(schema: TJsonSchemaNode, indent = '', depth = 0, omitMemberDocs = false): string {
|
|
146
|
+
if (depth > maxRenderDepth) return 'unknown';
|
|
147
|
+
if (schema === true) return 'unknown';
|
|
148
|
+
if (schema === false) return 'never';
|
|
149
|
+
if (!isSchemaRecord(schema)) return 'unknown';
|
|
150
|
+
const base = this.baseType(schema, indent, depth, omitMemberDocs);
|
|
151
|
+
return schema.nullable === true ? union([base, 'null']) : base;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The members of an object schema; undefined when the schema is not a plain object type. */
|
|
155
|
+
public objectMembers(
|
|
156
|
+
schema: TJsonSchemaNode,
|
|
157
|
+
): Array<{ name: string; doc?: string }> | undefined {
|
|
158
|
+
if (!isSchemaRecord(schema) || !this.isPlainObject(schema)) return undefined;
|
|
159
|
+
const properties = isSchemaRecord(schema.properties) ? schema.properties : {};
|
|
160
|
+
return Object.entries(properties).map(([name, property]) => {
|
|
161
|
+
const doc = schemaDoc(property as TJsonSchemaNode);
|
|
162
|
+
return { name, ...(doc === undefined ? {} : { doc }) };
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Whether an object schema requires none of its properties. */
|
|
167
|
+
public hasNoRequiredProperties(schema: TJsonSchemaNode): boolean {
|
|
168
|
+
return isSchemaRecord(schema)
|
|
169
|
+
&& this.isPlainObject(schema)
|
|
170
|
+
&& !(Array.isArray(schema.required) && schema.required.length > 0);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `interface Name { … }` for a plain object schema, otherwise `type Name = …;`. */
|
|
174
|
+
public namedDeclaration(name: string, schema: TJsonSchemaNode, description?: string): string {
|
|
175
|
+
const doc = isSchemaRecord(schema)
|
|
176
|
+
? (description ?? (typeof schema.description === 'string' ? schema.description : undefined))?.trim()
|
|
177
|
+
: description?.trim();
|
|
178
|
+
const prefix = doc ? docBlock(doc, '') : '';
|
|
179
|
+
if (isSchemaRecord(schema) && this.isPlainObject(schema) && schema.nullable !== true) {
|
|
180
|
+
return `${prefix}interface ${name} ${this.objectType(schema, '', 0, false)}`;
|
|
181
|
+
}
|
|
182
|
+
return `${prefix}type ${name} = ${this.type(schema, '', 0)};`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private isPlainObject(schema: { [key: string]: unknown }): boolean {
|
|
186
|
+
if (schema.$ref !== undefined || 'const' in schema || schema.enum !== undefined) return false;
|
|
187
|
+
if (schema.anyOf !== undefined || schema.oneOf !== undefined || schema.allOf !== undefined) return false;
|
|
188
|
+
if (schema.type === 'object') return true;
|
|
189
|
+
return schema.type === undefined && isSchemaRecord(schema.properties);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private baseType(
|
|
193
|
+
schema: { [key: string]: unknown },
|
|
194
|
+
indent: string,
|
|
195
|
+
depth: number,
|
|
196
|
+
omitMemberDocs: boolean,
|
|
197
|
+
): string {
|
|
198
|
+
if (typeof schema.$ref === 'string') return this.reference(schema.$ref, indent, depth);
|
|
199
|
+
if ('const' in schema) return literal(schema.const);
|
|
200
|
+
if (Array.isArray(schema.enum)) return schema.enum.length ? union(schema.enum.map(literal)) : 'never';
|
|
201
|
+
const alternatives = Array.isArray(schema.anyOf) ? schema.anyOf : Array.isArray(schema.oneOf) ? schema.oneOf : undefined;
|
|
202
|
+
if (alternatives?.length) {
|
|
203
|
+
return union(alternatives.map((alternative) => this.type(alternative as TJsonSchemaNode, indent, depth + 1)));
|
|
204
|
+
}
|
|
205
|
+
if (Array.isArray(schema.allOf) && schema.allOf.length) {
|
|
206
|
+
const own = { ...schema };
|
|
207
|
+
delete own.allOf;
|
|
208
|
+
const hasOwnShape = own.type !== undefined || own.properties !== undefined;
|
|
209
|
+
return intersection([
|
|
210
|
+
...schema.allOf.map((part) => this.type(part as TJsonSchemaNode, indent, depth + 1)),
|
|
211
|
+
...(hasOwnShape ? [this.type(own, indent, depth + 1)] : []),
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
const types = Array.isArray(schema.type)
|
|
215
|
+
? schema.type.filter((type): type is string => typeof type === 'string')
|
|
216
|
+
: typeof schema.type === 'string'
|
|
217
|
+
? [schema.type]
|
|
218
|
+
: this.inferredTypes(schema);
|
|
219
|
+
if (types.length === 0) return 'unknown';
|
|
220
|
+
return union(types.map((type) => {
|
|
221
|
+
switch (type) {
|
|
222
|
+
case 'string': return 'string';
|
|
223
|
+
case 'number':
|
|
224
|
+
case 'integer': return 'number';
|
|
225
|
+
case 'boolean': return 'boolean';
|
|
226
|
+
case 'null': return 'null';
|
|
227
|
+
case 'array': return this.arrayType(schema, indent, depth);
|
|
228
|
+
case 'object': return this.objectType(schema, indent, depth, omitMemberDocs);
|
|
229
|
+
default: return 'unknown';
|
|
230
|
+
}
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private inferredTypes(schema: { [key: string]: unknown }): string[] {
|
|
235
|
+
if (
|
|
236
|
+
schema.properties !== undefined
|
|
237
|
+
|| schema.additionalProperties !== undefined
|
|
238
|
+
|| schema.patternProperties !== undefined
|
|
239
|
+
|| schema.required !== undefined
|
|
240
|
+
) return ['object'];
|
|
241
|
+
if (schema.items !== undefined || schema.prefixItems !== undefined) return ['array'];
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private reference(ref: string, indent: string, depth: number): string {
|
|
246
|
+
const name = this.context.namedType(ref);
|
|
247
|
+
if (name) {
|
|
248
|
+
this.used.add(name);
|
|
249
|
+
return name;
|
|
250
|
+
}
|
|
251
|
+
const target = this.context.resolve(ref);
|
|
252
|
+
if (target === undefined || this.inlining.has(ref)) return 'unknown';
|
|
253
|
+
this.inlining.add(ref);
|
|
254
|
+
try {
|
|
255
|
+
return this.type(target, indent, depth + 1);
|
|
256
|
+
} finally {
|
|
257
|
+
this.inlining.delete(ref);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private arrayType(schema: { [key: string]: unknown }, indent: string, depth: number): string {
|
|
262
|
+
const tuple = Array.isArray(schema.prefixItems)
|
|
263
|
+
? schema.prefixItems
|
|
264
|
+
: Array.isArray(schema.items) ? schema.items : undefined;
|
|
265
|
+
if (tuple) {
|
|
266
|
+
const rest = Array.isArray(schema.prefixItems) ? schema.items : schema.additionalItems;
|
|
267
|
+
const members = tuple.map((item) => this.type(item as TJsonSchemaNode, indent, depth + 1));
|
|
268
|
+
if (rest !== undefined && rest !== false) {
|
|
269
|
+
members.push(`...${arrayOf(this.type(rest as TJsonSchemaNode, indent, depth + 1))}`);
|
|
270
|
+
}
|
|
271
|
+
return `[${members.join(', ')}]`;
|
|
272
|
+
}
|
|
273
|
+
if (schema.items === undefined) return 'unknown[]';
|
|
274
|
+
return arrayOf(this.type(schema.items as TJsonSchemaNode, indent, depth + 1));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private objectType(
|
|
278
|
+
schema: { [key: string]: unknown },
|
|
279
|
+
indent: string,
|
|
280
|
+
depth: number,
|
|
281
|
+
omitMemberDocs: boolean,
|
|
282
|
+
): string {
|
|
283
|
+
const memberIndent = `${indent} `;
|
|
284
|
+
const properties = isSchemaRecord(schema.properties) ? schema.properties : {};
|
|
285
|
+
const required = new Set(Array.isArray(schema.required)
|
|
286
|
+
? schema.required.filter((name): name is string => typeof name === 'string')
|
|
287
|
+
: []);
|
|
288
|
+
const members: Array<{ doc?: string; text: string }> = [];
|
|
289
|
+
for (const [name, property] of Object.entries(properties)) {
|
|
290
|
+
const type = this.type(property as TJsonSchemaNode, memberIndent, depth + 1);
|
|
291
|
+
const doc = omitMemberDocs ? undefined : schemaDoc(property as TJsonSchemaNode);
|
|
292
|
+
members.push({
|
|
293
|
+
...(doc === undefined ? {} : { doc }),
|
|
294
|
+
text: `${propertyKey(name)}${required.has(name) ? '' : '?'}: ${type}`,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
const indexTypes: string[] = [];
|
|
298
|
+
if (isSchemaRecord(schema.patternProperties)) {
|
|
299
|
+
for (const property of Object.values(schema.patternProperties)) {
|
|
300
|
+
indexTypes.push(this.type(property as TJsonSchemaNode, memberIndent, depth + 1));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (isSchemaRecord(schema.additionalProperties)) {
|
|
304
|
+
indexTypes.push(this.type(schema.additionalProperties, memberIndent, depth + 1));
|
|
305
|
+
} else if (schema.additionalProperties === true && members.length > 0) {
|
|
306
|
+
indexTypes.push('unknown');
|
|
307
|
+
}
|
|
308
|
+
if (indexTypes.length) members.push({ text: `[key: string]: ${union(indexTypes)}` });
|
|
309
|
+
if (members.length === 0) return schema.additionalProperties === false ? '{}' : 'Record<string, unknown>';
|
|
310
|
+
if (members.every((member) => member.doc === undefined && !member.text.includes('\n'))) {
|
|
311
|
+
const inline = `{ ${members.map((member) => member.text).join('; ')} }`;
|
|
312
|
+
if (inline.length <= maxInlineObjectCharacters) return inline;
|
|
313
|
+
}
|
|
314
|
+
const lines = members.map((member) =>
|
|
315
|
+
`${member.doc === undefined ? '' : docBlock(member.doc, memberIndent)}${memberIndent}${member.text};`);
|
|
316
|
+
return `{\n${lines.join('\n')}\n${indent}}`;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Indents every non-empty line of `text`. */
|
|
321
|
+
export const indentLines = (text: string, indent: string): string =>
|
|
322
|
+
text.split('\n').map((line) => line ? `${indent}${line}` : line).join('\n');
|
package/ts/utils.tooloutput.ts
CHANGED
|
@@ -25,6 +25,15 @@ async function* normalizeAsyncIterable(
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** A tool output's final value: the value itself, or the last value an async iterable yields. */
|
|
29
|
+
export async function settleToolOutput(output: unknown): Promise<unknown> {
|
|
30
|
+
const value = await output;
|
|
31
|
+
if (!isAsyncIterable(value)) return value;
|
|
32
|
+
let last: unknown;
|
|
33
|
+
for await (const item of value) last = item;
|
|
34
|
+
return last;
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
export function wrapToolSet(
|
|
29
38
|
tools: TFlexAgentToolSet,
|
|
30
39
|
limits: Required<NonNullable<IFlexHarnessOptions<unknown>['toolOutputLimits']>>,
|
package/ts_compaction/plugins.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { streamText, wrapLanguageModel } from 'ai';
|
|
1
|
+
import { asSchema, streamText, wrapLanguageModel } from 'ai';
|
|
2
2
|
|
|
3
|
-
export { streamText, wrapLanguageModel };
|
|
3
|
+
export { asSchema, streamText, wrapLanguageModel };
|
|
4
4
|
|
|
5
|
-
export type { ModelMessage } from 'ai';
|
|
5
|
+
export type { ModelMessage, ToolSet } from 'ai';
|
|
6
6
|
export type { TSmartAiLanguageModel } from '@modelprofile.com/flexharness-models';
|
|
7
7
|
|
|
8
8
|
import { AgentModelCallUsageRecorder } from '@modelprofile.com/flexharness-agent';
|
|
@@ -25,6 +25,9 @@ When constructing the summary, try to stick to this template:
|
|
|
25
25
|
/** The instructions of a summary call whose caller names none of the conversation's own. */
|
|
26
26
|
const COMPACTION_INSTRUCTIONS = 'You summarize a conversation so that another agent can continue its work.';
|
|
27
27
|
|
|
28
|
+
/** Added to the summary request when it carries the conversation's tools. */
|
|
29
|
+
const NO_TOOL_CALLS = 'Do not call any tool. Reply with the summary text only.';
|
|
30
|
+
|
|
28
31
|
export interface ICompactMessagesOptions {
|
|
29
32
|
abortSignal?: AbortSignal;
|
|
30
33
|
/**
|
|
@@ -45,13 +48,51 @@ export interface ICompactMessagesOptions {
|
|
|
45
48
|
* options' `providerOptions` through.
|
|
46
49
|
*/
|
|
47
50
|
providerOptions?: plugins.ProviderOptions;
|
|
51
|
+
/**
|
|
52
|
+
* The tools the conversation's turns are sent with. The summary call carries their definitions,
|
|
53
|
+
* none of their code, so its request repeats the turns' prefix and reads it from the provider's
|
|
54
|
+
* prompt cache, unless the definitions outweigh the history nine times, when reading them would
|
|
55
|
+
* cost more than the history saves. A context compactor passes its options' `tools` through.
|
|
56
|
+
*/
|
|
57
|
+
tools?: plugins.ToolSet;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether the summary request should carry the turns' tool definitions. A cached prefix is read at
|
|
62
|
+
* about a tenth of the input price (Anthropic and OpenAI), so repeating the turns' prefix costs about
|
|
63
|
+
* 0.1 x (tools + history), while a request without the tools reads nothing and pays the history in
|
|
64
|
+
* full: the tools pay their way while they are less than nine times the history.
|
|
65
|
+
*/
|
|
66
|
+
async function toolsRepayTheirPrefix(tools: plugins.ToolSet, messages: plugins.ModelMessage[]): Promise<boolean> {
|
|
67
|
+
let toolBytes = 0;
|
|
68
|
+
for (const [name, definition] of Object.entries(tools)) {
|
|
69
|
+
const schema = definition.type === 'provider' ? definition.args : await plugins.asSchema(definition.inputSchema).jsonSchema;
|
|
70
|
+
toolBytes += name.length + JSON.stringify([definition.description ?? '', schema ?? null]).length;
|
|
71
|
+
}
|
|
72
|
+
return toolBytes <= 9 * JSON.stringify(messages).length;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A tool set's definitions without anything that runs: no `execute`, no input callbacks. */
|
|
76
|
+
function toolDefinitions(tools: plugins.ToolSet): plugins.ToolSet {
|
|
77
|
+
return Object.fromEntries(Object.entries(tools).map(([name, definition]) => {
|
|
78
|
+
const {
|
|
79
|
+
execute: _execute,
|
|
80
|
+
onInputStart: _onInputStart,
|
|
81
|
+
onInputDelta: _onInputDelta,
|
|
82
|
+
onInputAvailable: _onInputAvailable,
|
|
83
|
+
...rest
|
|
84
|
+
} = definition as plugins.ToolSet[string] & Record<string, unknown>;
|
|
85
|
+
return [name, rest as plugins.ToolSet[string]];
|
|
86
|
+
}));
|
|
48
87
|
}
|
|
49
88
|
|
|
50
89
|
/**
|
|
51
90
|
* Compacts a message history into a summary.
|
|
52
91
|
*
|
|
53
92
|
* The summary is one streamed, instructed model call, the shape every agent turn has, so it reaches
|
|
54
|
-
* a provider that serves only that shape (the ChatGPT Codex backend) the way the turns do.
|
|
93
|
+
* a provider that serves only that shape (the ChatGPT Codex backend) the way the turns do. With the
|
|
94
|
+
* turns' `tools` it also repeats their prefix, which the provider has cached; a model that answers
|
|
95
|
+
* it with a tool call instead of a summary is asked once more with tool use turned off.
|
|
55
96
|
*
|
|
56
97
|
* @param model The same model used by runAgent, or a cheaper small model
|
|
57
98
|
* @param messages The full message history that overflowed
|
|
@@ -63,9 +104,12 @@ export async function compactMessages(
|
|
|
63
104
|
messages: plugins.ModelMessage[],
|
|
64
105
|
options: ICompactMessagesOptions = {},
|
|
65
106
|
): Promise<plugins.ModelMessage[]> {
|
|
107
|
+
const tools = options.tools && Object.keys(options.tools).length > 0 && await toolsRepayTheirPrefix(options.tools, messages)
|
|
108
|
+
? toolDefinitions(options.tools)
|
|
109
|
+
: undefined;
|
|
66
110
|
const prompt: plugins.ModelMessage[] = [
|
|
67
111
|
...messages,
|
|
68
|
-
{ role: 'user', content: COMPACTION_PROMPT },
|
|
112
|
+
{ role: 'user', content: tools ? `${COMPACTION_PROMPT}\n\n${NO_TOOL_CALLS}` : COMPACTION_PROMPT },
|
|
69
113
|
];
|
|
70
114
|
const reportUsage = options.reportUsage;
|
|
71
115
|
const modelCalls = reportUsage
|
|
@@ -89,38 +133,50 @@ export async function compactMessages(
|
|
|
89
133
|
},
|
|
90
134
|
})
|
|
91
135
|
: model;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
136
|
+
// The summary is asked for in the shape of a turn, the tools offered as the turns offer them. A
|
|
137
|
+
// model that calls a tool instead of answering is asked again with tool use turned off, which
|
|
138
|
+
// keeps the tool definitions and so still the cached tools and instructions.
|
|
139
|
+
const summarize = async (toolChoice?: 'none'): Promise<{ summary: string; calledTools: boolean }> => {
|
|
140
|
+
let streamError: unknown;
|
|
141
|
+
try {
|
|
142
|
+
const result = plugins.streamText({
|
|
143
|
+
model: observedModel,
|
|
144
|
+
instructions: options.system ?? COMPACTION_INSTRUCTIONS,
|
|
145
|
+
messages: prompt,
|
|
146
|
+
allowSystemInMessages: true,
|
|
147
|
+
...(tools ? { tools, ...(toolChoice ? { toolChoice } : {}) } : {}),
|
|
148
|
+
...(options.providerOptions ? { providerOptions: options.providerOptions } : {}),
|
|
149
|
+
abortSignal: options.abortSignal,
|
|
150
|
+
onError: ({ error }) => {
|
|
151
|
+
streamError ??= error;
|
|
152
|
+
},
|
|
153
|
+
...(modelCalls
|
|
154
|
+
? {
|
|
155
|
+
onLanguageModelCallEnd: ({ modelId, usage }) => {
|
|
156
|
+
modelCalls.end(modelId, usage);
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
: {}),
|
|
160
|
+
});
|
|
161
|
+
let summary = '';
|
|
162
|
+
for await (const delta of result.textStream) summary += delta;
|
|
163
|
+
// A stream that fails delivers its error to onError and ends, and an aborted stream just ends;
|
|
164
|
+
// either way the summary is incomplete and must not replace the history.
|
|
165
|
+
if (streamError !== undefined) throw streamError;
|
|
166
|
+
options.abortSignal?.throwIfAborted();
|
|
167
|
+
modelCalls?.settle('missing');
|
|
168
|
+
return { summary, calledTools: tools !== undefined && (await result.toolCalls).length > 0 };
|
|
169
|
+
} catch (error) {
|
|
170
|
+
modelCalls?.settle(options.abortSignal?.aborted ? 'aborted' : 'failed');
|
|
171
|
+
throw streamError ?? error;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
// A provider-executed tool, such as a web search, would run on the provider's side before the
|
|
175
|
+
// retry could prevent it: with one in the set, tool use is off from the start.
|
|
176
|
+
const providerExecuted = tools !== undefined && Object.values(tools).some((definition) => definition.type === 'provider');
|
|
177
|
+
let { summary, calledTools } = await summarize(providerExecuted ? 'none' : undefined);
|
|
178
|
+
if (calledTools && !summary.trim()) ({ summary } = await summarize('none'));
|
|
179
|
+
return summaryMessages(summary);
|
|
124
180
|
}
|
|
125
181
|
|
|
126
182
|
function summaryMessages(summary: string): plugins.ModelMessage[] {
|