@providerkit/agents 0.1.0 → 0.2.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/LICENSE-APACHE-2.0 +202 -0
- package/README.md +2108 -52
- package/THIRD_PARTY_NOTICES.md +521 -0
- package/dist/a2a/auth.d.ts +10 -0
- package/dist/a2a/auth.d.ts.map +1 -0
- package/dist/a2a/auth.js +52 -0
- package/dist/a2a/auth.js.map +1 -0
- package/dist/a2a/client.d.ts +74 -0
- package/dist/a2a/client.d.ts.map +1 -0
- package/dist/a2a/client.js +601 -0
- package/dist/a2a/client.js.map +1 -0
- package/dist/a2a/index.d.ts +22 -0
- package/dist/a2a/index.d.ts.map +1 -0
- package/dist/a2a/index.js +11 -0
- package/dist/a2a/index.js.map +1 -0
- package/dist/a2a/memory-runs.d.ts +62 -0
- package/dist/a2a/memory-runs.d.ts.map +1 -0
- package/dist/a2a/memory-runs.js +349 -0
- package/dist/a2a/memory-runs.js.map +1 -0
- package/dist/a2a/policy.d.ts +15 -0
- package/dist/a2a/policy.d.ts.map +1 -0
- package/dist/a2a/policy.js +114 -0
- package/dist/a2a/policy.js.map +1 -0
- package/dist/a2a/push.d.ts +34 -0
- package/dist/a2a/push.d.ts.map +1 -0
- package/dist/a2a/push.js +180 -0
- package/dist/a2a/push.js.map +1 -0
- package/dist/a2a/remote.d.ts +155 -0
- package/dist/a2a/remote.d.ts.map +1 -0
- package/dist/a2a/remote.js +1196 -0
- package/dist/a2a/remote.js.map +1 -0
- package/dist/a2a/server.d.ts +110 -0
- package/dist/a2a/server.d.ts.map +1 -0
- package/dist/a2a/server.js +1139 -0
- package/dist/a2a/server.js.map +1 -0
- package/dist/a2a/signing.d.ts +21 -0
- package/dist/a2a/signing.d.ts.map +1 -0
- package/dist/a2a/signing.js +305 -0
- package/dist/a2a/signing.js.map +1 -0
- package/dist/a2a/tasks.d.ts +88 -0
- package/dist/a2a/tasks.d.ts.map +1 -0
- package/dist/a2a/tasks.js +170 -0
- package/dist/a2a/tasks.js.map +1 -0
- package/dist/a2a/wire.d.ts +267 -0
- package/dist/a2a/wire.d.ts.map +1 -0
- package/dist/a2a/wire.js +1202 -0
- package/dist/a2a/wire.js.map +1 -0
- package/dist/background-types.d.ts +76 -0
- package/dist/background-types.d.ts.map +1 -0
- package/dist/background-types.js +2 -0
- package/dist/background-types.js.map +1 -0
- package/dist/child.d.ts +25 -0
- package/dist/child.d.ts.map +1 -0
- package/dist/child.js +129 -0
- package/dist/child.js.map +1 -0
- package/dist/code-driver.d.ts +40 -0
- package/dist/code-driver.d.ts.map +1 -0
- package/dist/code-driver.js +2 -0
- package/dist/code-driver.js.map +1 -0
- package/dist/code-mode.d.ts +3 -0
- package/dist/code-mode.d.ts.map +1 -0
- package/dist/code-mode.js +2 -0
- package/dist/code-mode.js.map +1 -0
- package/dist/code-script.d.ts +3 -0
- package/dist/code-script.d.ts.map +1 -0
- package/dist/code-script.js +149 -0
- package/dist/code-script.js.map +1 -0
- package/dist/code.d.ts +75 -0
- package/dist/code.d.ts.map +1 -0
- package/dist/code.js +326 -0
- package/dist/code.js.map +1 -0
- package/dist/compaction.d.ts +15 -0
- package/dist/compaction.d.ts.map +1 -0
- package/dist/compaction.js +168 -0
- package/dist/compaction.js.map +1 -0
- package/dist/delegation.d.ts +85 -16
- package/dist/delegation.d.ts.map +1 -1
- package/dist/delegation.js +240 -106
- package/dist/delegation.js.map +1 -1
- package/dist/evals/driver.d.ts +10 -0
- package/dist/evals/driver.d.ts.map +1 -0
- package/dist/evals/driver.js +192 -0
- package/dist/evals/driver.js.map +1 -0
- package/dist/evals/journal.d.ts +26 -0
- package/dist/evals/journal.d.ts.map +1 -0
- package/dist/evals/journal.js +483 -0
- package/dist/evals/journal.js.map +1 -0
- package/dist/evals/judge.d.ts +31 -0
- package/dist/evals/judge.d.ts.map +1 -0
- package/dist/evals/judge.js +142 -0
- package/dist/evals/judge.js.map +1 -0
- package/dist/evals/report.d.ts +38 -0
- package/dist/evals/report.d.ts.map +1 -0
- package/dist/evals/report.js +241 -0
- package/dist/evals/report.js.map +1 -0
- package/dist/evals/runner.d.ts +24 -0
- package/dist/evals/runner.d.ts.map +1 -0
- package/dist/evals/runner.js +655 -0
- package/dist/evals/runner.js.map +1 -0
- package/dist/evals/scorers.d.ts +36 -0
- package/dist/evals/scorers.d.ts.map +1 -0
- package/dist/evals/scorers.js +259 -0
- package/dist/evals/scorers.js.map +1 -0
- package/dist/evals/simulation.d.ts +7 -0
- package/dist/evals/simulation.d.ts.map +1 -0
- package/dist/evals/simulation.js +38 -0
- package/dist/evals/simulation.js.map +1 -0
- package/dist/evals/testing.d.ts +36 -0
- package/dist/evals/testing.d.ts.map +1 -0
- package/dist/evals/testing.js +87 -0
- package/dist/evals/testing.js.map +1 -0
- package/dist/evals/trajectory.d.ts +11 -0
- package/dist/evals/trajectory.d.ts.map +1 -0
- package/dist/evals/trajectory.js +180 -0
- package/dist/evals/trajectory.js.map +1 -0
- package/dist/evals/types.d.ts +431 -0
- package/dist/evals/types.d.ts.map +1 -0
- package/dist/evals/types.js +4 -0
- package/dist/evals/types.js.map +1 -0
- package/dist/evals.d.ts +14 -0
- package/dist/evals.d.ts.map +1 -0
- package/dist/evals.js +9 -0
- package/dist/evals.js.map +1 -0
- package/dist/extensions.d.ts +33 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +188 -0
- package/dist/extensions.js.map +1 -0
- package/dist/handoff.d.ts +58 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +160 -0
- package/dist/handoff.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/input.d.ts +42 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +266 -0
- package/dist/input.js.map +1 -0
- package/dist/mcp.d.ts +77 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +505 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory-store.d.ts +11 -0
- package/dist/memory-store.d.ts.map +1 -0
- package/dist/memory-store.js +272 -0
- package/dist/memory-store.js.map +1 -0
- package/dist/memory.d.ts +212 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +613 -0
- package/dist/memory.js.map +1 -0
- package/dist/otel.d.ts +74 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +427 -0
- package/dist/otel.js.map +1 -0
- package/dist/progress.d.ts +17 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +193 -0
- package/dist/progress.js.map +1 -0
- package/dist/quickjs.d.ts +19 -0
- package/dist/quickjs.d.ts.map +1 -0
- package/dist/quickjs.js +519 -0
- package/dist/quickjs.js.map +1 -0
- package/dist/recovery.d.ts +10 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +62 -0
- package/dist/recovery.js.map +1 -0
- package/dist/request.d.ts +88 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +422 -0
- package/dist/request.js.map +1 -0
- package/dist/run.d.ts +16 -4
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +2016 -245
- package/dist/run.js.map +1 -1
- package/dist/runs.d.ts +77 -34
- package/dist/runs.d.ts.map +1 -1
- package/dist/runs.js +267 -85
- package/dist/runs.js.map +1 -1
- package/dist/schema-to-ts.d.ts +4 -0
- package/dist/schema-to-ts.d.ts.map +1 -0
- package/dist/schema-to-ts.js +191 -0
- package/dist/schema-to-ts.js.map +1 -0
- package/dist/session.d.ts +23 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +137 -0
- package/dist/session.js.map +1 -0
- package/dist/snapshot.d.ts +57 -8
- package/dist/snapshot.d.ts.map +1 -1
- package/dist/snapshot.js +1206 -109
- package/dist/snapshot.js.map +1 -1
- package/dist/steering.d.ts +75 -0
- package/dist/steering.d.ts.map +1 -0
- package/dist/steering.js +214 -0
- package/dist/steering.js.map +1 -0
- package/dist/subagents.d.ts +141 -0
- package/dist/subagents.d.ts.map +1 -0
- package/dist/subagents.js +746 -0
- package/dist/subagents.js.map +1 -0
- package/dist/tool-retry.d.ts +12 -0
- package/dist/tool-retry.d.ts.map +1 -0
- package/dist/tool-retry.js +53 -0
- package/dist/tool-retry.js.map +1 -0
- package/dist/tree.d.ts +62 -6
- package/dist/tree.d.ts.map +1 -1
- package/dist/tree.js +192 -23
- package/dist/tree.js.map +1 -1
- package/dist/types.d.ts +767 -52
- package/dist/types.d.ts.map +1 -1
- package/dist/workflow-snapshot.d.ts +8 -0
- package/dist/workflow-snapshot.d.ts.map +1 -0
- package/dist/workflow-snapshot.js +276 -0
- package/dist/workflow-snapshot.js.map +1 -0
- package/dist/workflow-types.d.ts +171 -0
- package/dist/workflow-types.d.ts.map +1 -0
- package/dist/workflow-types.js +2 -0
- package/dist/workflow-types.js.map +1 -0
- package/dist/workflow.d.ts +19 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +1053 -0
- package/dist/workflow.js.map +1 -0
- package/examples/a2a-remote.ts +85 -0
- package/examples/a2a-server.ts +100 -0
- package/examples/background.ts +18 -4
- package/examples/code-mode.ts +76 -0
- package/examples/conversation.ts +57 -0
- package/examples/evals.ts +79 -0
- package/examples/guardrails.ts +56 -0
- package/examples/handoff.ts +79 -0
- package/examples/memory.ts +44 -0
- package/examples/otel.ts +99 -0
- package/examples/steering.ts +69 -0
- package/examples/structured-handoff.ts +92 -0
- package/examples/subagents.ts +127 -0
- package/examples/workflow.ts +74 -0
- package/package.json +58 -6
- package/src/a2a/auth.ts +62 -0
- package/src/a2a/client.ts +733 -0
- package/src/a2a/index.ts +74 -0
- package/src/a2a/memory-runs.ts +448 -0
- package/src/a2a/policy.ts +148 -0
- package/src/a2a/push.ts +251 -0
- package/src/a2a/remote.ts +1652 -0
- package/src/a2a/server.ts +1390 -0
- package/src/a2a/signing.ts +348 -0
- package/src/a2a/tasks.ts +260 -0
- package/src/a2a/wire.ts +1443 -0
- package/src/background-types.ts +86 -0
- package/src/child.ts +181 -0
- package/src/code-driver.ts +48 -0
- package/src/code-mode.ts +12 -0
- package/src/code-script.ts +149 -0
- package/src/code.ts +424 -0
- package/src/compaction.ts +212 -0
- package/src/delegation.ts +444 -155
- package/src/evals/driver.ts +222 -0
- package/src/evals/journal.ts +605 -0
- package/src/evals/judge.ts +188 -0
- package/src/evals/report.ts +295 -0
- package/src/evals/runner.ts +826 -0
- package/src/evals/scorers.ts +339 -0
- package/src/evals/simulation.ts +46 -0
- package/src/evals/testing.ts +126 -0
- package/src/evals/trajectory.ts +223 -0
- package/src/evals/types.ts +463 -0
- package/src/evals.ts +72 -0
- package/src/extensions.ts +247 -0
- package/src/handoff.ts +214 -0
- package/src/index.ts +51 -0
- package/src/input.ts +319 -0
- package/src/mcp.ts +690 -0
- package/src/memory-store.ts +366 -0
- package/src/memory.ts +893 -0
- package/src/otel.ts +568 -0
- package/src/progress.ts +214 -0
- package/src/quickjs.ts +524 -0
- package/src/recovery.ts +90 -0
- package/src/request.ts +513 -0
- package/src/run.ts +2678 -313
- package/src/runs.ts +464 -133
- package/src/schema-to-ts.ts +194 -0
- package/src/session.ts +178 -0
- package/src/snapshot.ts +1595 -124
- package/src/steering.ts +329 -0
- package/src/subagents.ts +1084 -0
- package/src/tool-retry.ts +86 -0
- package/src/tree.ts +223 -27
- package/src/types.ts +711 -52
- package/src/workflow-snapshot.ts +366 -0
- package/src/workflow-types.ts +213 -0
- package/src/workflow.ts +1316 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Adapted from TanStack AI's ai-code-mode/type-generator/json-schema-to-ts.ts
|
|
2
|
+
// (MIT, Copyright (c) 2025 Tanner Linsley) and Mastra's tools/code-mode/stub-generator.ts
|
|
3
|
+
// (Apache-2.0, Copyright (c) 2025 Kepler Software, Inc.).
|
|
4
|
+
// Changes: per-tool reference aliases, intersections, safe keys/comments and no runtime dependency.
|
|
5
|
+
// See THIRD_PARTY_NOTICES.md and LICENSE-APACHE-2.0.
|
|
6
|
+
import type { Tool } from "@providerkit/core";
|
|
7
|
+
|
|
8
|
+
/** Prompt-only typings. The tool's validator, not these declarations, checks each call. */
|
|
9
|
+
export function toolTypings(tools: readonly Tool[]): string {
|
|
10
|
+
const declarations: string[] = [];
|
|
11
|
+
const methods: string[] = [];
|
|
12
|
+
const names = new Set<string>();
|
|
13
|
+
for (const [index, tool] of tools.entries()) {
|
|
14
|
+
if (names.has(tool.name)) throw new TypeError(`Duplicate tool name: ${tool.name}`);
|
|
15
|
+
names.add(tool.name);
|
|
16
|
+
// Position avoids collisions between names like a-b/a_b, and between input/output $defs.
|
|
17
|
+
const input = `Tool${index + 1}Input`;
|
|
18
|
+
const output = `Tool${index + 1}Output`;
|
|
19
|
+
declarations.push(...schemaDeclarations(tool.inputSchema, input));
|
|
20
|
+
if (tool.outputSchema !== undefined) {
|
|
21
|
+
declarations.push(...schemaDeclarations(tool.outputSchema, output));
|
|
22
|
+
}
|
|
23
|
+
methods.push(
|
|
24
|
+
` ${comment(tool.description)}\n ${JSON.stringify(tool.name)}(input: ${input}): Promise<${tool.outputSchema === undefined ? "unknown" : output}>;`,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return [...declarations, `declare const tools: {\n${methods.join("\n")}\n};`].join("\n\n");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function record(value: unknown): value is Record<string, unknown> {
|
|
31
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function comment(value: string): string {
|
|
35
|
+
return `/** ${value.replaceAll("*/", "* /").replace(/\s+/g, " ")} */`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function union(parts: readonly string[]): string {
|
|
39
|
+
const choices = [...new Set(parts)].filter((part) => part !== "never");
|
|
40
|
+
if (choices.includes("unknown")) return "unknown";
|
|
41
|
+
return choices.join(" | ") || "never";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function intersection(parts: readonly (readonly string[])[]): readonly string[] {
|
|
45
|
+
const constraints = new Map(parts.map((part) => [union(part), part]));
|
|
46
|
+
constraints.delete("unknown");
|
|
47
|
+
if (constraints.has("never")) return ["never"];
|
|
48
|
+
if (constraints.size < 2) return constraints.values().next().value ?? ["unknown"];
|
|
49
|
+
return [[...constraints.keys()].map((part) => `(${part})`).join(" & ")];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function literal(value: unknown, depth = 0): string {
|
|
53
|
+
if (depth > 32) return "unknown";
|
|
54
|
+
if (value === null) return "null";
|
|
55
|
+
if (typeof value === "string" || typeof value === "boolean") return JSON.stringify(value);
|
|
56
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
57
|
+
if (Array.isArray(value)) return `[${value.map((item) => literal(item, depth + 1)).join(", ")}]`;
|
|
58
|
+
if (record(value)) {
|
|
59
|
+
return `{ ${Object.entries(value)
|
|
60
|
+
.map(([key, item]) => `${JSON.stringify(key)}: ${literal(item, depth + 1)};`)
|
|
61
|
+
.join(" ")} }`;
|
|
62
|
+
}
|
|
63
|
+
return "unknown";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function schemaDeclarations(root: unknown, name: string): string[] {
|
|
67
|
+
const aliases = new Map<string, { name: string; type: string }>();
|
|
68
|
+
aliases.set("#", { name, type: "unknown" });
|
|
69
|
+
|
|
70
|
+
function reference(ref: string, ancestors: ReadonlySet<string>, depth: number): string {
|
|
71
|
+
if (ancestors.has(ref)) return "unknown";
|
|
72
|
+
let target: unknown = root;
|
|
73
|
+
if (ref !== "#") {
|
|
74
|
+
if (!ref.startsWith("#/")) return "unknown";
|
|
75
|
+
try {
|
|
76
|
+
for (const part of decodeURIComponent(ref.slice(2)).split("/")) {
|
|
77
|
+
const key = part.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
78
|
+
if (!record(target) || !Object.hasOwn(target, key)) return "unknown";
|
|
79
|
+
target = target[key];
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
return "unknown";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const existing = aliases.get(ref);
|
|
86
|
+
if (existing) return existing.name;
|
|
87
|
+
const alias = { name: `${name}Ref${aliases.size}`, type: "unknown" };
|
|
88
|
+
aliases.set(ref, alias);
|
|
89
|
+
alias.type = union(render(target, new Set([...ancestors, ref]), depth + 1));
|
|
90
|
+
return alias.name;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function object(schema: Record<string, unknown>, depth: number): string {
|
|
94
|
+
const properties = record(schema.properties) ? schema.properties : {};
|
|
95
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
96
|
+
const fields: string[] = [];
|
|
97
|
+
const fieldTypes: string[] = [];
|
|
98
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
99
|
+
const type = render(value, new Set(), depth + 1);
|
|
100
|
+
fieldTypes.push(...type);
|
|
101
|
+
const description =
|
|
102
|
+
record(value) && typeof value.description === "string"
|
|
103
|
+
? `${comment(value.description)} `
|
|
104
|
+
: "";
|
|
105
|
+
fields.push(
|
|
106
|
+
`${description}${JSON.stringify(key)}${required.has(key) ? "" : "?"}: ${union(type)};`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
// A required name can exist without a corresponding properties schema.
|
|
110
|
+
for (const key of required) {
|
|
111
|
+
if (typeof key === "string" && !Object.hasOwn(properties, key)) {
|
|
112
|
+
fieldTypes.push("unknown");
|
|
113
|
+
fields.push(`${JSON.stringify(key)}: unknown;`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const additional = schema.additionalProperties;
|
|
117
|
+
if (additional === false) {
|
|
118
|
+
return fields.length ? `{ ${fields.join(" ")} }` : "Record<string, never>";
|
|
119
|
+
}
|
|
120
|
+
const value = render(additional, new Set(), depth + 1);
|
|
121
|
+
if (!fields.length) return `Record<string, ${union(value)}>`;
|
|
122
|
+
// TS cannot exclude named fields from a string index. Widen only that index,
|
|
123
|
+
// preserving each declared field instead of producing an impossible intersection.
|
|
124
|
+
return `{ ${fields.join(" ")} } & Record<string, ${union([...value, ...fieldTypes])}>`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function array(schema: Record<string, unknown>, depth: number): string {
|
|
128
|
+
const prefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : schema.items;
|
|
129
|
+
if (!Array.isArray(prefix))
|
|
130
|
+
return `Array<${union(render(schema.items, new Set(), depth + 1))}>`;
|
|
131
|
+
const minimum = typeof schema.minItems === "number" ? schema.minItems : 0;
|
|
132
|
+
const tail = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
|
133
|
+
const fields = prefix.map((item, index) => {
|
|
134
|
+
const type = union(render(item, new Set(), depth + 1));
|
|
135
|
+
return index < minimum ? type : `(${type})?`;
|
|
136
|
+
});
|
|
137
|
+
if (tail !== false) fields.push(`...Array<${union(render(tail, new Set(), depth + 1))}>`);
|
|
138
|
+
return `[${fields.join(", ")}]`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function typed(type: unknown, schema: Record<string, unknown>, depth: number): string {
|
|
142
|
+
switch (type) {
|
|
143
|
+
case "string":
|
|
144
|
+
return "string";
|
|
145
|
+
case "number":
|
|
146
|
+
case "integer":
|
|
147
|
+
return "number";
|
|
148
|
+
case "boolean":
|
|
149
|
+
return "boolean";
|
|
150
|
+
case "null":
|
|
151
|
+
return "null";
|
|
152
|
+
case "object":
|
|
153
|
+
return object(schema, depth);
|
|
154
|
+
case "array":
|
|
155
|
+
return array(schema, depth);
|
|
156
|
+
default:
|
|
157
|
+
return "unknown";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function render(
|
|
162
|
+
schema: unknown,
|
|
163
|
+
ancestors: ReadonlySet<string>,
|
|
164
|
+
depth: number,
|
|
165
|
+
): readonly string[] {
|
|
166
|
+
if (schema === false) return ["never"];
|
|
167
|
+
// ponytail: unsupported constraints and deeply nested schemas become unknown.
|
|
168
|
+
// This is model guidance, not a second JSON Schema validator.
|
|
169
|
+
if (!record(schema) || depth > 64) return ["unknown"];
|
|
170
|
+
const parts: (readonly string[])[] = [];
|
|
171
|
+
if (typeof schema.$ref === "string") parts.push([reference(schema.$ref, ancestors, depth)]);
|
|
172
|
+
if (Object.hasOwn(schema, "const")) parts.push([literal(schema.const)]);
|
|
173
|
+
else if (Array.isArray(schema.enum)) parts.push(schema.enum.map((item) => literal(item)));
|
|
174
|
+
else if (Array.isArray(schema.type)) {
|
|
175
|
+
parts.push(schema.type.map((type) => typed(type, schema, depth)));
|
|
176
|
+
} else if (schema.type !== undefined) parts.push([typed(schema.type, schema, depth)]);
|
|
177
|
+
else if (schema.properties !== undefined || schema.additionalProperties !== undefined) {
|
|
178
|
+
parts.push([object(schema, depth)]);
|
|
179
|
+
} else if (schema.items !== undefined || schema.prefixItems !== undefined)
|
|
180
|
+
parts.push([array(schema, depth)]);
|
|
181
|
+
for (const key of ["anyOf", "oneOf"] as const) {
|
|
182
|
+
const choices = schema[key];
|
|
183
|
+
if (Array.isArray(choices))
|
|
184
|
+
parts.push(choices.flatMap((item) => render(item, ancestors, depth + 1)));
|
|
185
|
+
}
|
|
186
|
+
if (Array.isArray(schema.allOf)) {
|
|
187
|
+
parts.push(...schema.allOf.map((item) => render(item, ancestors, depth + 1)));
|
|
188
|
+
}
|
|
189
|
+
return intersection(parts);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
aliases.get("#")!.type = union(render(root, new Set(["#"]), 0));
|
|
193
|
+
return [...aliases.values()].map((alias) => `type ${alias.name} = ${alias.type};`);
|
|
194
|
+
}
|
package/src/session.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// A conversation store the run reads once and appends to once.
|
|
2
|
+
//
|
|
3
|
+
// Load happens before the run is saved, so the history is frozen in the snapshot. The append
|
|
4
|
+
// happens after the run settles, keyed by its run id, so a resumed or retried run writes its
|
|
5
|
+
// turn once. A revision check keeps a run from appending over turns that landed after it
|
|
6
|
+
// loaded: a full replace would let the last writer erase them.
|
|
7
|
+
import { messageOf } from "@providerkit/core";
|
|
8
|
+
import { copyData, equalData, isId, isRecord, isRevision } from "./snapshot.ts";
|
|
9
|
+
import { readStored } from "./input.ts";
|
|
10
|
+
import type { AgentMessage, Session, SessionTurn } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
/** The session changed after the run loaded it, or this run id already appended another turn.
|
|
13
|
+
* Load the conversation again before continuing it. */
|
|
14
|
+
export class SessionConflictError extends Error {
|
|
15
|
+
constructor(readonly sessionId: string) {
|
|
16
|
+
super(
|
|
17
|
+
`Conversation ${sessionId} changed after this run loaded it. Load it again before you add to it.`,
|
|
18
|
+
);
|
|
19
|
+
this.name = "SessionConflictError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function readTurn(value: unknown): SessionTurn {
|
|
24
|
+
if (!isRecord(value) || !isId(value.runId) || !isRevision(value.after))
|
|
25
|
+
throw new TypeError("A session turn needs a runId, the revision it follows and its messages.");
|
|
26
|
+
return { runId: value.runId, after: value.after, messages: readStored(value.messages, "A turn") };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The duplicate and conflict rule is ported from OpenAI Agents JS (MIT),
|
|
30
|
+
// packages/agents-core/src/memory/memorySession.ts: an id that already applied the same change
|
|
31
|
+
// succeeds without applying it again, and the same id with another change fails. The revision
|
|
32
|
+
// check and the copies on every read and write follow PydanticAI's (MIT) InMemoryConversationStore,
|
|
33
|
+
// pydantic_ai_slim/pydantic_ai/conversation_store.py.
|
|
34
|
+
/**
|
|
35
|
+
* A session kept in this process, for tests, examples and single-process tools. It copies on
|
|
36
|
+
* every read and write, so changing what it returned or what you appended never changes what
|
|
37
|
+
* it holds. Everything is lost when the process ends.
|
|
38
|
+
*/
|
|
39
|
+
export function createMemorySession(
|
|
40
|
+
options: { id?: string; messages?: readonly AgentMessage[] } = {},
|
|
41
|
+
): Session {
|
|
42
|
+
const id = options.id ?? crypto.randomUUID();
|
|
43
|
+
if (!isId(id)) throw new TypeError("A session id must be non-empty text.");
|
|
44
|
+
let messages = readStored(options.messages ?? [], "The starting messages");
|
|
45
|
+
let count = messages.length > 0 ? 1 : 0;
|
|
46
|
+
let revision: string | null = count > 0 ? String(count) : null;
|
|
47
|
+
const turns = new Map<string, { turn: SessionTurn; revision: string }>();
|
|
48
|
+
return Object.freeze({
|
|
49
|
+
id,
|
|
50
|
+
async load({ limit, signal }: { limit?: number; signal: AbortSignal }) {
|
|
51
|
+
signal.throwIfAborted();
|
|
52
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 0))
|
|
53
|
+
throw new TypeError("limit must be a whole number, 0 or more.");
|
|
54
|
+
let window = messages;
|
|
55
|
+
if (limit !== undefined) window = limit === 0 ? [] : messages.slice(-limit);
|
|
56
|
+
return { messages: copyData(window), revision };
|
|
57
|
+
},
|
|
58
|
+
async append(raw: SessionTurn, { signal }: { signal: AbortSignal }) {
|
|
59
|
+
signal.throwIfAborted();
|
|
60
|
+
const turn = readTurn(raw);
|
|
61
|
+
const applied = turns.get(turn.runId);
|
|
62
|
+
if (applied) {
|
|
63
|
+
if (!equalData(applied.turn, turn)) throw new SessionConflictError(id);
|
|
64
|
+
return { revision: applied.revision };
|
|
65
|
+
}
|
|
66
|
+
if (turn.after !== revision) throw new SessionConflictError(id);
|
|
67
|
+
messages = [...messages, ...turn.messages];
|
|
68
|
+
count++;
|
|
69
|
+
revision = String(count);
|
|
70
|
+
turns.set(turn.runId, { turn, revision });
|
|
71
|
+
return { revision };
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check a session store against the contract the run relies on, by writing to fresh sessions
|
|
78
|
+
* from `create`. Each call to `create` must return a new, empty session. Throws an Error that
|
|
79
|
+
* names the first rule the store breaks. Run it against a test database, never a real one.
|
|
80
|
+
*/
|
|
81
|
+
export async function checkSession(create: () => Session | Promise<Session>): Promise<void> {
|
|
82
|
+
const signal = new AbortController().signal;
|
|
83
|
+
function expect(ok: boolean, rule: string): asserts ok {
|
|
84
|
+
if (!ok) throw new Error(`The session store breaks a rule: ${rule}`);
|
|
85
|
+
}
|
|
86
|
+
/** A step that must succeed. If it throws, name the rule, with the store's error as cause. */
|
|
87
|
+
async function must<T>(work: () => Promise<T>, rule: string): Promise<T> {
|
|
88
|
+
try {
|
|
89
|
+
return await work();
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw new Error(`The session store breaks a rule: ${rule} It threw: ${messageOf(error)}`, {
|
|
92
|
+
cause: error,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function conflicts(work: Promise<unknown>): Promise<boolean> {
|
|
97
|
+
try {
|
|
98
|
+
await work;
|
|
99
|
+
return false;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return error instanceof Error && error.name === "SessionConflictError";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const first: AgentMessage[] = [
|
|
105
|
+
{ role: "user", content: "First question" },
|
|
106
|
+
{ role: "assistant", content: "First answer" },
|
|
107
|
+
];
|
|
108
|
+
const second: AgentMessage[] = [
|
|
109
|
+
{ role: "user", content: "Second question" },
|
|
110
|
+
{ role: "assistant", content: "Second answer" },
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
const session = await create();
|
|
114
|
+
expect(isId(session.id), "a session has a non-empty id.");
|
|
115
|
+
const empty = await must(() => session.load({ signal }), "a new session loads.");
|
|
116
|
+
expect(
|
|
117
|
+
empty.messages.length === 0 && empty.revision === null,
|
|
118
|
+
"a new session loads no messages and a null revision.",
|
|
119
|
+
);
|
|
120
|
+
const turn: SessionTurn = { runId: "run-1", after: null, messages: copyData(first) };
|
|
121
|
+
const saved = await must(
|
|
122
|
+
() => session.append(turn, { signal }),
|
|
123
|
+
"the first append to a new session, after null, succeeds.",
|
|
124
|
+
);
|
|
125
|
+
expect(isId(saved.revision), "append returns the new revision as non-empty text.");
|
|
126
|
+
turn.messages[0] = { role: "user", content: "Changed after append" };
|
|
127
|
+
const loaded = await session.load({ signal });
|
|
128
|
+
expect(
|
|
129
|
+
equalData(loaded.messages, first) && loaded.revision === saved.revision,
|
|
130
|
+
"load returns what was appended, oldest first, with the revision append returned, and changing the appended array afterwards changes nothing stored.",
|
|
131
|
+
);
|
|
132
|
+
loaded.messages.pop();
|
|
133
|
+
expect(
|
|
134
|
+
equalData((await session.load({ signal })).messages, first),
|
|
135
|
+
"changing a loaded array changes nothing stored.",
|
|
136
|
+
);
|
|
137
|
+
const repeated = await must(
|
|
138
|
+
() => session.append({ runId: "run-1", after: null, messages: copyData(first) }, { signal }),
|
|
139
|
+
"repeating the same turn succeeds and writes nothing.",
|
|
140
|
+
);
|
|
141
|
+
expect(
|
|
142
|
+
isId(repeated.revision) && equalData((await session.load({ signal })).messages, first),
|
|
143
|
+
"repeating the same turn succeeds and writes nothing.",
|
|
144
|
+
);
|
|
145
|
+
expect(
|
|
146
|
+
await conflicts(
|
|
147
|
+
session.append({ runId: "run-1", after: null, messages: copyData(second) }, { signal }),
|
|
148
|
+
),
|
|
149
|
+
"the same runId with other messages throws SessionConflictError.",
|
|
150
|
+
);
|
|
151
|
+
expect(
|
|
152
|
+
await conflicts(
|
|
153
|
+
session.append({ runId: "run-2", after: null, messages: copyData(second) }, { signal }),
|
|
154
|
+
),
|
|
155
|
+
"a stale revision throws SessionConflictError.",
|
|
156
|
+
);
|
|
157
|
+
expect(
|
|
158
|
+
equalData((await session.load({ signal })).messages, first),
|
|
159
|
+
"a refused append writes nothing.",
|
|
160
|
+
);
|
|
161
|
+
await must(
|
|
162
|
+
() =>
|
|
163
|
+
session.append(
|
|
164
|
+
{ runId: "run-2", after: saved.revision, messages: copyData(second) },
|
|
165
|
+
{
|
|
166
|
+
signal,
|
|
167
|
+
},
|
|
168
|
+
),
|
|
169
|
+
"an append after the current revision succeeds.",
|
|
170
|
+
);
|
|
171
|
+
const window = await must(() => session.load({ limit: 3, signal }), "load takes a limit.");
|
|
172
|
+
expect(
|
|
173
|
+
equalData(window.messages, [...first.slice(1), ...second]),
|
|
174
|
+
"load({ limit }) returns the newest messages, oldest first.",
|
|
175
|
+
);
|
|
176
|
+
const other = await create();
|
|
177
|
+
expect(other.id !== session.id, "create returns a new session each time.");
|
|
178
|
+
}
|