toolsplice-core 0.0.1 → 0.0.2
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/README.md +26 -17
- package/dist/abortable.d.ts +6 -0
- package/dist/abortable.js +50 -0
- package/dist/adaptive-read.d.ts +50 -0
- package/dist/adaptive-read.js +780 -0
- package/dist/answer-wait.d.ts +7 -0
- package/dist/answer-wait.js +45 -0
- package/dist/cache-reads.d.ts +18 -0
- package/dist/cache-reads.js +89 -0
- package/dist/calibration-ledger.d.ts +37 -0
- package/dist/calibration-ledger.js +148 -0
- package/dist/calibration-marker.d.ts +4 -0
- package/dist/calibration-marker.js +77 -0
- package/dist/catalog-setup.d.ts +33 -0
- package/dist/catalog-setup.js +89 -0
- package/dist/catalog.d.ts +10 -0
- package/dist/catalog.js +42 -0
- package/dist/checkpoint-files.d.ts +15 -0
- package/dist/checkpoint-files.js +99 -0
- package/dist/coalesce-reads.d.ts +6 -0
- package/dist/coalesce-reads.js +33 -0
- package/dist/contract-caller.d.ts +2 -1
- package/dist/contract-caller.js +35 -16
- package/dist/contract-schema.d.ts +3 -1
- package/dist/contract-schema.js +34 -4
- package/dist/contracts.d.ts +1 -0
- package/dist/contracts.js +5 -1
- package/dist/core.d.ts +31 -1
- package/dist/core.js +27 -0
- package/dist/db-final-answer.d.ts +9 -0
- package/dist/db-final-answer.js +62 -0
- package/dist/define.d.ts +78 -0
- package/dist/define.js +14 -0
- package/dist/final-answer-stores.d.ts +71 -0
- package/dist/final-answer-stores.js +72 -0
- package/dist/final-answer.d.ts +32 -0
- package/dist/final-answer.js +104 -0
- package/dist/impact.d.ts +48 -0
- package/dist/impact.js +71 -0
- package/dist/local-tool.d.ts +18 -0
- package/dist/local-tool.js +32 -0
- package/dist/mcp-apps.d.ts +53 -0
- package/dist/mcp-apps.js +32 -0
- package/dist/mcp-event-delivery.d.ts +98 -0
- package/dist/mcp-event-delivery.js +211 -0
- package/dist/mcp-extensions.d.ts +46 -0
- package/dist/mcp-extensions.js +200 -0
- package/dist/mcp-peer.d.ts +45 -0
- package/dist/mcp-peer.js +207 -0
- package/dist/mcp-server-card.d.ts +21 -0
- package/dist/mcp-server-card.js +54 -0
- package/dist/page-checkpoint.d.ts +71 -0
- package/dist/page-checkpoint.js +286 -0
- package/dist/page-read-gate.d.ts +15 -0
- package/dist/page-read-gate.js +48 -0
- package/dist/pattern.d.ts +1 -0
- package/dist/pattern.js +27 -5
- package/dist/read-pages.d.ts +25 -0
- package/dist/read-pages.js +85 -0
- package/dist/read-policy.d.ts +6 -0
- package/dist/read-policy.js +16 -0
- package/dist/read-steps.d.ts +12 -0
- package/dist/read-steps.js +36 -0
- package/dist/result-session.d.ts +109 -0
- package/dist/result-session.js +620 -0
- package/dist/route-checkpoint.d.ts +3 -0
- package/dist/route-checkpoint.js +80 -0
- package/dist/route-evaluation.d.ts +141 -0
- package/dist/route-evaluation.js +329 -0
- package/dist/row-groups.d.ts +17 -0
- package/dist/row-groups.js +79 -0
- package/dist/row-query.d.ts +62 -0
- package/dist/row-query.js +172 -0
- package/dist/rules.d.ts +30 -0
- package/dist/rules.js +72 -0
- package/dist/run-diagnostics.d.ts +75 -0
- package/dist/run-diagnostics.js +212 -0
- package/dist/search-fetch.d.ts +50 -0
- package/dist/search-fetch.js +264 -0
- package/dist/skill-files.d.ts +9 -0
- package/dist/skill-files.js +49 -0
- package/dist/splice-diagnostics.d.ts +4 -0
- package/dist/splice-diagnostics.js +24 -16
- package/dist/splice-recording.d.ts +29 -2
- package/dist/splice-recording.js +22 -5
- package/dist/splice.d.ts +98 -1
- package/dist/splice.js +225 -27
- package/dist/sqlite-checkpoint-store.d.ts +5 -0
- package/dist/sqlite-checkpoint-store.js +91 -0
- package/dist/sqlite-checkpoint-worker.js +115 -0
- package/dist/test-difference.d.ts +13 -0
- package/dist/test-difference.js +42 -0
- package/dist/testing.d.ts +122 -0
- package/dist/testing.js +261 -0
- package/dist/text-pages.d.ts +24 -0
- package/dist/text-pages.js +36 -0
- package/dist/tool-discovery.d.ts +59 -0
- package/dist/tool-discovery.js +136 -0
- package/dist/unevaluated.d.ts +7 -0
- package/dist/unevaluated.js +156 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflow-json.js +1 -1
- package/integrations/ai.d.mts +36 -0
- package/integrations/ai.mjs +178 -0
- package/integrations/call-options.mjs +9 -0
- package/integrations/langchain.d.mts +31 -0
- package/integrations/langchain.mjs +114 -0
- package/integrations/managed.mjs +154 -0
- package/integrations/types.d.mts +13 -0
- package/package.json +30 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ResultTool } from "./result-session.js";
|
|
2
|
+
import type { SpliceRunOptions } from "./splice.js";
|
|
3
|
+
export declare class CatalogSearch {
|
|
4
|
+
#private;
|
|
5
|
+
private limit;
|
|
6
|
+
readonly definition: {
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
inputSchema: {
|
|
10
|
+
type: string;
|
|
11
|
+
properties: Record<string, unknown>;
|
|
12
|
+
required: string[];
|
|
13
|
+
additionalProperties: boolean;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
constructor(tools: readonly ResultTool[], limit: number);
|
|
17
|
+
matches(q: string): {
|
|
18
|
+
entry: {
|
|
19
|
+
tool: ResultTool;
|
|
20
|
+
definition: import("./local-tool.js").LocalToolDefinition;
|
|
21
|
+
name: Set<string>;
|
|
22
|
+
body: Set<string>;
|
|
23
|
+
};
|
|
24
|
+
score: number;
|
|
25
|
+
}[];
|
|
26
|
+
initial(question: string): ResultTool[];
|
|
27
|
+
execute(args: Record<string, unknown>): Promise<{
|
|
28
|
+
tools: {
|
|
29
|
+
name: string;
|
|
30
|
+
description: string;
|
|
31
|
+
inputSchema: Record<string, unknown>;
|
|
32
|
+
}[];
|
|
33
|
+
}>;
|
|
34
|
+
}
|
|
35
|
+
export declare class CatalogCall {
|
|
36
|
+
#private;
|
|
37
|
+
readonly definition: {
|
|
38
|
+
name: string;
|
|
39
|
+
description: string;
|
|
40
|
+
inputSchema: {
|
|
41
|
+
type: string;
|
|
42
|
+
properties: Record<string, unknown>;
|
|
43
|
+
required: string[];
|
|
44
|
+
additionalProperties: boolean;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
constructor(tools: readonly ResultTool[]);
|
|
48
|
+
resolve(args: Record<string, unknown>): {
|
|
49
|
+
tool: ResultTool;
|
|
50
|
+
input: Record<string, unknown>;
|
|
51
|
+
};
|
|
52
|
+
execute(args: Record<string, unknown>, options?: SpliceRunOptions): Promise<unknown>;
|
|
53
|
+
}
|
|
54
|
+
/** Limit model-visible schemas to requested matches. Pass only executables authorized for this run. */
|
|
55
|
+
export declare function discoverTools(tools: readonly ResultTool[], { threshold, limit, question, }?: {
|
|
56
|
+
threshold?: number;
|
|
57
|
+
limit?: number;
|
|
58
|
+
question?: string;
|
|
59
|
+
}): ResultTool[];
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { createGuardCompiler } from "./contract-caller.js";
|
|
2
|
+
import { copyJson, jsonObject, parseWorkflowJson } from "./workflow-json.js";
|
|
3
|
+
const tokens = (text) => new Set(text
|
|
4
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
5
|
+
.toLowerCase()
|
|
6
|
+
.match(/[a-z0-9]+/g) ?? []);
|
|
7
|
+
const properties = (fields) => ({
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: fields,
|
|
10
|
+
required: Object.keys(fields),
|
|
11
|
+
additionalProperties: false,
|
|
12
|
+
});
|
|
13
|
+
export class CatalogSearch {
|
|
14
|
+
limit;
|
|
15
|
+
definition = {
|
|
16
|
+
name: "discover_tools",
|
|
17
|
+
description: "Find authorized tools by exact name or keywords. Returns matching names, descriptions and full input schemas for call_discovered_tool.",
|
|
18
|
+
inputSchema: properties({
|
|
19
|
+
query: { type: "string", minLength: 1, maxLength: 512 },
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
#index;
|
|
23
|
+
constructor(tools, limit) {
|
|
24
|
+
this.limit = limit;
|
|
25
|
+
this.#index = tools.map((t) => ({
|
|
26
|
+
tool: t,
|
|
27
|
+
definition: copyJson(t.definition),
|
|
28
|
+
name: tokens(t.definition.name),
|
|
29
|
+
body: tokens((t.definition.description ?? "").slice(0, 2048)),
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
matches(q) {
|
|
33
|
+
const words = tokens(q);
|
|
34
|
+
return this.#index
|
|
35
|
+
.map((entry) => {
|
|
36
|
+
let score = entry.definition.name.toLowerCase() === q.trim().toLowerCase()
|
|
37
|
+
? 10000
|
|
38
|
+
: 0;
|
|
39
|
+
for (const word of words)
|
|
40
|
+
score +=
|
|
41
|
+
(entry.name.has(word) ? 8 : 0) + (entry.body.has(word) ? 1 : 0);
|
|
42
|
+
return { entry, score };
|
|
43
|
+
})
|
|
44
|
+
.filter((r) => r.score > 0)
|
|
45
|
+
.sort((a, b) => b.score - a.score);
|
|
46
|
+
}
|
|
47
|
+
initial(question) {
|
|
48
|
+
if (typeof question !== "string" ||
|
|
49
|
+
!question.trim() ||
|
|
50
|
+
Buffer.byteLength(question) > 32768)
|
|
51
|
+
throw new Error("Expected a question of 1 to 32768 UTF-8 bytes.");
|
|
52
|
+
return this.matches(question)
|
|
53
|
+
.slice(0, this.limit)
|
|
54
|
+
.map((row) => row.entry.tool);
|
|
55
|
+
}
|
|
56
|
+
async execute(args) {
|
|
57
|
+
const q = args.query;
|
|
58
|
+
if (typeof q !== "string" || !q.trim() || q.length > 512)
|
|
59
|
+
throw new Error("Enter a bounded tool name or search query.");
|
|
60
|
+
return {
|
|
61
|
+
tools: this.matches(q)
|
|
62
|
+
.slice(0, this.limit)
|
|
63
|
+
.map(({ entry: { definition: d } }) => copyJson({
|
|
64
|
+
name: d.name,
|
|
65
|
+
description: d.description ?? "",
|
|
66
|
+
inputSchema: d.inputSchema,
|
|
67
|
+
})),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export class CatalogCall {
|
|
72
|
+
definition = {
|
|
73
|
+
name: "call_discovered_tool",
|
|
74
|
+
description: "Call an authorized tool by its exact name. Encode its arguments as a JSON object string matching its discovered input schema.",
|
|
75
|
+
inputSchema: properties({
|
|
76
|
+
name: { type: "string" },
|
|
77
|
+
arguments: { type: "string" },
|
|
78
|
+
}),
|
|
79
|
+
};
|
|
80
|
+
#tools;
|
|
81
|
+
#compile;
|
|
82
|
+
#guards = new Map();
|
|
83
|
+
constructor(tools) {
|
|
84
|
+
this.#tools = new Map(tools.map((t) => [t.definition.name, t]));
|
|
85
|
+
}
|
|
86
|
+
resolve(args) {
|
|
87
|
+
const tool = typeof args.name === "string" && this.#tools.get(args.name);
|
|
88
|
+
if (!tool)
|
|
89
|
+
throw new Error("Unknown catalog tool.");
|
|
90
|
+
if (typeof args.arguments !== "string")
|
|
91
|
+
throw new Error("Pass a JSON object string for arguments.");
|
|
92
|
+
const input = parseWorkflowJson(args.arguments);
|
|
93
|
+
if (!jsonObject(input))
|
|
94
|
+
throw new Error("Tool arguments must be a JSON object.");
|
|
95
|
+
const name = tool.definition.name;
|
|
96
|
+
let guard = this.#guards.get(name);
|
|
97
|
+
if (!guard) {
|
|
98
|
+
guard = (this.#compile ??= createGuardCompiler())(tool.definition.inputSchema, name, "input");
|
|
99
|
+
this.#guards.set(name, guard);
|
|
100
|
+
}
|
|
101
|
+
if (!guard(input))
|
|
102
|
+
throw new Error("Arguments do not match the selected tool schema.");
|
|
103
|
+
return { tool, input };
|
|
104
|
+
}
|
|
105
|
+
async execute(args, options) {
|
|
106
|
+
const { tool, input } = this.resolve(args);
|
|
107
|
+
return tool.execute(input, options);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** Limit model-visible schemas to requested matches. Pass only executables authorized for this run. */
|
|
111
|
+
export function discoverTools(tools, { threshold = 16, limit = 2, question, } = {}) {
|
|
112
|
+
if (!Number.isSafeInteger(threshold) ||
|
|
113
|
+
threshold < 1 ||
|
|
114
|
+
!Number.isSafeInteger(limit) ||
|
|
115
|
+
limit < 1 ||
|
|
116
|
+
limit > 8)
|
|
117
|
+
throw new RangeError("Invalid catalog threshold or result limit.");
|
|
118
|
+
const names = tools.map((t) => t.definition.name);
|
|
119
|
+
if (new Set(names).size !== names.length)
|
|
120
|
+
throw new Error("Duplicate catalog tool name.");
|
|
121
|
+
if (tools.length < threshold)
|
|
122
|
+
return [...tools];
|
|
123
|
+
if (names.some((n) => [
|
|
124
|
+
"discover_tools",
|
|
125
|
+
"call_discovered_tool",
|
|
126
|
+
"inspect_result",
|
|
127
|
+
"call_with_result",
|
|
128
|
+
].includes(n)))
|
|
129
|
+
throw new Error("Reserved catalog tool name.");
|
|
130
|
+
const search = new CatalogSearch(tools, limit);
|
|
131
|
+
return [
|
|
132
|
+
...(question === undefined ? [] : search.initial(question)),
|
|
133
|
+
search,
|
|
134
|
+
new CatalogCall(tools),
|
|
135
|
+
];
|
|
136
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Options, ValidateFunction } from "ajv";
|
|
2
|
+
type Schema = boolean | Record<string, unknown>;
|
|
3
|
+
/** Used only for modern schemas that explicitly request unevaluated-location checks.
|
|
4
|
+
* Ajv still compiles each assertion. Only the collection of successful locations is local.
|
|
5
|
+
* Every subvalidator resolves against the same detached, already-guarded schema. */
|
|
6
|
+
export declare function compileUnevaluated(root: Schema, options: Options): ValidateFunction;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// JSON Schema 2020-12 records the individual indices matched by contains and the
|
|
2
|
+
// properties evaluated by successful branches. Ajv alone tracks only an array prefix.
|
|
3
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
4
|
+
import { escapePointer, jsonObject } from "./workflow-json.js";
|
|
5
|
+
const maps = ["properties", "$defs", "definitions", "dependentSchemas"];
|
|
6
|
+
const lists = ["prefixItems", "allOf", "anyOf", "oneOf"];
|
|
7
|
+
const children = ["items", "contains", "not", "if", "then", "else", "propertyNames",
|
|
8
|
+
"additionalProperties", "unevaluatedItems", "unevaluatedProperties"];
|
|
9
|
+
/** Used only for modern schemas that explicitly request unevaluated-location checks.
|
|
10
|
+
* Ajv still compiles each assertion. Only the collection of successful locations is local.
|
|
11
|
+
* Every subvalidator resolves against the same detached, already-guarded schema. */
|
|
12
|
+
export function compileUnevaluated(root, options) {
|
|
13
|
+
const ajv = new Ajv2020(options);
|
|
14
|
+
const id = "urn:toolsplice:local-guard";
|
|
15
|
+
const paths = new WeakMap();
|
|
16
|
+
const schemas = new Map();
|
|
17
|
+
const validators = new Map();
|
|
18
|
+
const required = new Set();
|
|
19
|
+
const references = new Set();
|
|
20
|
+
function index(schema, path) {
|
|
21
|
+
schemas.set(path, schema);
|
|
22
|
+
if (typeof schema === "boolean")
|
|
23
|
+
return;
|
|
24
|
+
paths.set(schema, path);
|
|
25
|
+
if (typeof schema.$ref === "string")
|
|
26
|
+
references.add(decodeURIComponent(schema.$ref.slice(1)));
|
|
27
|
+
for (const key of maps) {
|
|
28
|
+
const entries = schema[key];
|
|
29
|
+
if (jsonObject(entries))
|
|
30
|
+
for (const [name, child] of Object.entries(entries))
|
|
31
|
+
index(child, path + "/" + key + "/" + escapePointer(name));
|
|
32
|
+
}
|
|
33
|
+
for (const key of lists) {
|
|
34
|
+
const subs = schema[key];
|
|
35
|
+
if (Array.isArray(subs))
|
|
36
|
+
subs.forEach((child, i) => {
|
|
37
|
+
const pointer = path + "/" + key + "/" + i;
|
|
38
|
+
index(child, pointer);
|
|
39
|
+
if (key === "oneOf" || key === "anyOf")
|
|
40
|
+
required.add(pointer);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
for (const key of children) {
|
|
44
|
+
if (!Object.hasOwn(schema, key))
|
|
45
|
+
continue;
|
|
46
|
+
const pointer = path + "/" + key;
|
|
47
|
+
index(schema[key], pointer);
|
|
48
|
+
if (["if", "contains", "unevaluatedItems", "unevaluatedProperties"].includes(key))
|
|
49
|
+
required.add(pointer);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
index(root, "");
|
|
53
|
+
for (const ref of references)
|
|
54
|
+
if (!schemas.has(ref))
|
|
55
|
+
throw new Error("Reference must identify a local schema.");
|
|
56
|
+
function valid(path, value) {
|
|
57
|
+
const schema = schemas.get(path);
|
|
58
|
+
if (typeof schema === "boolean")
|
|
59
|
+
return schema;
|
|
60
|
+
const check = validators.get(path);
|
|
61
|
+
if (!check)
|
|
62
|
+
throw new Error("Missing compiled schema condition.");
|
|
63
|
+
return !!check(value);
|
|
64
|
+
}
|
|
65
|
+
for (const [keyword, type] of [["unevaluatedItems", "array"], ["unevaluatedProperties", "object"]]) {
|
|
66
|
+
ajv.removeKeyword(keyword);
|
|
67
|
+
ajv.addKeyword({
|
|
68
|
+
keyword, type, schemaType: ["boolean", "object"], errors: false,
|
|
69
|
+
compile(_subschema, parent) {
|
|
70
|
+
const location = paths.get(parent);
|
|
71
|
+
if (location === undefined)
|
|
72
|
+
throw new Error("Unknown schema location.");
|
|
73
|
+
return (value) => {
|
|
74
|
+
const covered = new Set();
|
|
75
|
+
const visited = new Set();
|
|
76
|
+
const array = Array.isArray(value);
|
|
77
|
+
// Return true when a successful branch evaluated every location.
|
|
78
|
+
function collect(path, includeOwn) {
|
|
79
|
+
if (visited.has(path))
|
|
80
|
+
return false;
|
|
81
|
+
visited.add(path);
|
|
82
|
+
const schema = schemas.get(path);
|
|
83
|
+
if (schema === undefined)
|
|
84
|
+
throw new Error("Missing referenced schema.");
|
|
85
|
+
if (typeof schema === "boolean")
|
|
86
|
+
return false;
|
|
87
|
+
if (includeOwn && Object.hasOwn(schema, keyword))
|
|
88
|
+
return true;
|
|
89
|
+
if (array) {
|
|
90
|
+
if (Object.hasOwn(schema, "items"))
|
|
91
|
+
return true;
|
|
92
|
+
if (Array.isArray(schema.prefixItems))
|
|
93
|
+
for (let i = 0; i < Math.min(value.length, schema.prefixItems.length); i++)
|
|
94
|
+
covered.add(String(i));
|
|
95
|
+
if (Object.hasOwn(schema, "contains"))
|
|
96
|
+
for (const [i, item] of value.entries())
|
|
97
|
+
if (valid(path + "/contains", item))
|
|
98
|
+
covered.add(String(i));
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
if (Object.hasOwn(schema, "additionalProperties"))
|
|
102
|
+
return true;
|
|
103
|
+
if (jsonObject(schema.properties))
|
|
104
|
+
for (const name of Object.keys(schema.properties))
|
|
105
|
+
covered.add(name);
|
|
106
|
+
if (jsonObject(schema.dependentSchemas))
|
|
107
|
+
for (const name of Object.keys(schema.dependentSchemas))
|
|
108
|
+
if (Object.hasOwn(value, name) && collect(path + "/dependentSchemas/" + escapePointer(name), true))
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (typeof schema.$ref === "string" && collect(decodeURIComponent(schema.$ref.slice(1)), true))
|
|
112
|
+
return true;
|
|
113
|
+
for (const key of ["allOf", "anyOf", "oneOf"]) {
|
|
114
|
+
const subs = schema[key];
|
|
115
|
+
if (!Array.isArray(subs))
|
|
116
|
+
continue;
|
|
117
|
+
for (let i = 0; i < subs.length; i++) {
|
|
118
|
+
const branch = path + "/" + key + "/" + i;
|
|
119
|
+
if ((key === "allOf" || valid(branch, value)) && collect(branch, true))
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (Object.hasOwn(schema, "if")) {
|
|
124
|
+
const condition = valid(path + "/if", value);
|
|
125
|
+
if (condition && collect(path + "/if", true))
|
|
126
|
+
return true;
|
|
127
|
+
const branch = condition ? "then" : "else";
|
|
128
|
+
if (Object.hasOwn(schema, branch))
|
|
129
|
+
return collect(path + "/" + branch, true);
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (collect(location, false))
|
|
134
|
+
return true;
|
|
135
|
+
for (const [name, item] of Object.entries(value))
|
|
136
|
+
if (!covered.has(name) && !valid(location + "/" + keyword, item))
|
|
137
|
+
return false;
|
|
138
|
+
return true;
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
ajv.addSchema(root, id);
|
|
144
|
+
const result = ajv.getSchema(id);
|
|
145
|
+
if (!result)
|
|
146
|
+
throw new Error("Could not compile schema.");
|
|
147
|
+
for (const path of required) {
|
|
148
|
+
if (typeof schemas.get(path) === "boolean")
|
|
149
|
+
continue;
|
|
150
|
+
const check = ajv.getSchema(id + "#" + path.split("/").map(encodeURIComponent).join("/"));
|
|
151
|
+
if (!check)
|
|
152
|
+
throw new Error("Could not compile schema condition.");
|
|
153
|
+
validators.set(path, check);
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.0.
|
|
1
|
+
export declare const VERSION = "0.0.2";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.0.
|
|
1
|
+
export const VERSION = "0.0.2";
|
package/dist/workflow-json.js
CHANGED
|
@@ -4,7 +4,7 @@ import { stableHash } from "./drift.js";
|
|
|
4
4
|
export const JSON_LIMIT = 5 * 1024 * 1024;
|
|
5
5
|
export const jsonObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
6
6
|
export function copyJson(value, maxBytes = JSON_LIMIT) {
|
|
7
|
-
assertContractJson(value);
|
|
7
|
+
assertContractJson(value, undefined, undefined, maxBytes);
|
|
8
8
|
const encoded = JSON.stringify(value);
|
|
9
9
|
if (Buffer.byteLength(encoded) > maxBytes)
|
|
10
10
|
throw new Error("Workflow JSON exceeds its byte limit.");
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Tool } from "ai";
|
|
2
|
+
import type { RunDiagnostics, SpliceRunOptions } from "../dist/core.js";
|
|
3
|
+
import type { ExecutableTool } from "./types.mjs";
|
|
4
|
+
|
|
5
|
+
export declare function aiTools<Result>(
|
|
6
|
+
workflow: ExecutableTool<Result>,
|
|
7
|
+
options?: SpliceRunOptions,
|
|
8
|
+
): Record<string, Tool<Record<string, unknown>, Result>>;
|
|
9
|
+
|
|
10
|
+
import type { ToolSet } from "ai";
|
|
11
|
+
import type { ResultTool } from "../dist/core.js";
|
|
12
|
+
|
|
13
|
+
/** Consume streaming results before the callback finishes. */
|
|
14
|
+
export declare function withAiTools<T extends ToolSet, R>(
|
|
15
|
+
existing: T,
|
|
16
|
+
question: string,
|
|
17
|
+
use: (tools: T & ToolSet, activeTools: () => string[]) => R,
|
|
18
|
+
options?: { enabled?: boolean; diagnostics?: RunDiagnostics },
|
|
19
|
+
): Promise<Awaited<R>>;
|
|
20
|
+
export declare function withAiTools<R>(
|
|
21
|
+
existing: readonly ResultTool[],
|
|
22
|
+
question: string,
|
|
23
|
+
use: (tools: ToolSet, activeTools: () => string[]) => R,
|
|
24
|
+
options?: { enabled?: boolean; diagnostics?: RunDiagnostics },
|
|
25
|
+
): Promise<Awaited<R>>;
|
|
26
|
+
|
|
27
|
+
/** Yield a stream from the callback and keep its tools live until consumption ends. */
|
|
28
|
+
export declare function streamAiTools<T>(
|
|
29
|
+
existing: ToolSet | readonly ResultTool[],
|
|
30
|
+
question: string,
|
|
31
|
+
use: (
|
|
32
|
+
tools: ToolSet,
|
|
33
|
+
activeTools: () => string[],
|
|
34
|
+
) => AsyncIterable<T> | PromiseLike<AsyncIterable<T>>,
|
|
35
|
+
options?: { enabled?: boolean; diagnostics?: RunDiagnostics },
|
|
36
|
+
): AsyncGenerator<T>;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { jsonSchema, tool } from "ai";
|
|
3
|
+
import {
|
|
4
|
+
managedTools,
|
|
5
|
+
activeTools,
|
|
6
|
+
checkNames,
|
|
7
|
+
visibleContent,
|
|
8
|
+
withRun,
|
|
9
|
+
streamRun,
|
|
10
|
+
} from "./managed.mjs";
|
|
11
|
+
import { callOptions } from "./call-options.mjs";
|
|
12
|
+
|
|
13
|
+
/** Register one checked workflow under its existing name in a Vercel AI SDK tool set. */
|
|
14
|
+
export function aiTools(workflow, options = {}) {
|
|
15
|
+
const { name, description, inputSchema } = workflow.definition;
|
|
16
|
+
return {
|
|
17
|
+
[name]: tool({
|
|
18
|
+
description,
|
|
19
|
+
inputSchema: jsonSchema(inputSchema),
|
|
20
|
+
execute: (input, { abortSignal }) =>
|
|
21
|
+
workflow.execute(input, callOptions(options, abortSignal)),
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Run a Vercel agent with its existing tool set and a private result scope. */
|
|
27
|
+
export async function withAiTools(
|
|
28
|
+
existing,
|
|
29
|
+
question,
|
|
30
|
+
use,
|
|
31
|
+
{ enabled = true, diagnostics } = {},
|
|
32
|
+
) {
|
|
33
|
+
if (diagnostics) return diagnostics.run(enabled ? "adaptive" : "native", () => withAiTools(existing, question, use, {enabled}));
|
|
34
|
+
if (!enabled) {
|
|
35
|
+
const native = nativeTools(existing);
|
|
36
|
+
return use(native, () => activeTools(native, true));
|
|
37
|
+
}
|
|
38
|
+
return withRun(question, buildTools(existing, question), use);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Stream a run and release retained results on completion, failure or early return. */
|
|
42
|
+
export async function* streamAiTools(
|
|
43
|
+
existing,
|
|
44
|
+
question,
|
|
45
|
+
use,
|
|
46
|
+
{ enabled = true, diagnostics } = {},
|
|
47
|
+
) {
|
|
48
|
+
if (diagnostics) {
|
|
49
|
+
yield* diagnostics.stream(enabled ? "adaptive" : "native", () => streamAiTools(existing, question, use, {enabled}));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (!enabled) {
|
|
53
|
+
const native = nativeTools(existing);
|
|
54
|
+
yield* await use(native, () => activeTools(native, true));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
yield* streamRun(question, buildTools(existing, question), use);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function nativeTools(existing) {
|
|
61
|
+
if (!Array.isArray(existing)) return existing;
|
|
62
|
+
checkNames(existing.map((t) => t.definition.name));
|
|
63
|
+
return Object.fromEntries(
|
|
64
|
+
existing.flatMap((t) => Object.entries(aiTools(t))),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function buildTools(existing, question) {
|
|
69
|
+
return (session, begin, cleanup) => {
|
|
70
|
+
const sources = Array.isArray(existing) ? existing : [];
|
|
71
|
+
const entries = [];
|
|
72
|
+
if (!Array.isArray(existing)) {
|
|
73
|
+
for (const [name, native] of Object.entries(existing)) {
|
|
74
|
+
entries.push([name, wrapNative(name, native, begin, cleanup)]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const source of managedTools(session, sources, question)) {
|
|
78
|
+
entries.push(...Object.entries(aiTools(source)));
|
|
79
|
+
}
|
|
80
|
+
checkNames(entries.map(([name]) => name));
|
|
81
|
+
return Object.fromEntries(entries);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function defaultOutput(raw) {
|
|
86
|
+
if (typeof raw === "string") return { type: "text", value: raw };
|
|
87
|
+
const json = JSON.stringify(raw);
|
|
88
|
+
return { type: "json", value: json === undefined ? null : JSON.parse(json) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function wrapNative(name, native, begin, cleanup) {
|
|
92
|
+
if (
|
|
93
|
+
native.type === "provider" ||
|
|
94
|
+
native.type === "provider-defined" ||
|
|
95
|
+
!native.execute
|
|
96
|
+
)
|
|
97
|
+
return native;
|
|
98
|
+
// Vercel can serialize the same tool result again when rebuilding message history.
|
|
99
|
+
// Keep the submitted URL and reuse a view only when the model-visible content agrees.
|
|
100
|
+
const calls = new Map();
|
|
101
|
+
let submittedInputs = new WeakMap();
|
|
102
|
+
cleanup.push(() => {
|
|
103
|
+
calls.clear();
|
|
104
|
+
submittedInputs = new WeakMap();
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
...native,
|
|
108
|
+
execute(input, options) {
|
|
109
|
+
const present = begin(name, input ?? {});
|
|
110
|
+
const id = options?.toolCallId;
|
|
111
|
+
if (id) {
|
|
112
|
+
if (calls.size === 32) calls.delete(calls.keys().next().value);
|
|
113
|
+
calls.set(id, { present });
|
|
114
|
+
if (input && typeof input === "object")
|
|
115
|
+
submittedInputs.set(input, { id, present });
|
|
116
|
+
}
|
|
117
|
+
return native.execute(input, options);
|
|
118
|
+
},
|
|
119
|
+
async toModelOutput(options) {
|
|
120
|
+
const call = calls.get(options.toolCallId);
|
|
121
|
+
const original = submittedInputs.get(options.input);
|
|
122
|
+
// A reconstructed input may already contain a redirect. Only attach a submitted
|
|
123
|
+
// URL to an identified call when we still have its dispatch snapshot.
|
|
124
|
+
const fallback = begin(
|
|
125
|
+
name,
|
|
126
|
+
options.toolCallId ? {} : (options.input ?? {}),
|
|
127
|
+
);
|
|
128
|
+
const present =
|
|
129
|
+
call?.present ??
|
|
130
|
+
(original && original.id === options.toolCallId
|
|
131
|
+
? original.present
|
|
132
|
+
: fallback);
|
|
133
|
+
const output = native.toModelOutput
|
|
134
|
+
? await native.toModelOutput(options)
|
|
135
|
+
: defaultOutput(options.output);
|
|
136
|
+
if (
|
|
137
|
+
options.output?.isError === true ||
|
|
138
|
+
(output.type === "json" && output.value?.isError === true) ||
|
|
139
|
+
!["text", "json", "content"].includes(output.type)
|
|
140
|
+
)
|
|
141
|
+
return output;
|
|
142
|
+
if (
|
|
143
|
+
output.type === "content" &&
|
|
144
|
+
output.value.some((p) => p.type !== "text")
|
|
145
|
+
)
|
|
146
|
+
return output;
|
|
147
|
+
const serialized = call && JSON.stringify(output.value);
|
|
148
|
+
const hash =
|
|
149
|
+
serialized?.length > 1024
|
|
150
|
+
? createHash("sha256").update(serialized).digest("hex")
|
|
151
|
+
: undefined;
|
|
152
|
+
let value;
|
|
153
|
+
if (hash && call.hash === hash && call.type === output.type) {
|
|
154
|
+
value = structuredClone(call.view);
|
|
155
|
+
} else {
|
|
156
|
+
value =
|
|
157
|
+
output.type === "content"
|
|
158
|
+
? visibleContent(output.value, present)
|
|
159
|
+
: present(output.value);
|
|
160
|
+
if (hash && JSON.stringify(value).length <= 8192) {
|
|
161
|
+
Object.assign(call, {
|
|
162
|
+
hash,
|
|
163
|
+
type: output.type,
|
|
164
|
+
view: structuredClone(value),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
...output,
|
|
170
|
+
type:
|
|
171
|
+
output.type === "text" && typeof value !== "string"
|
|
172
|
+
? "json"
|
|
173
|
+
: output.type,
|
|
174
|
+
value,
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Model arguments and application-owned run options use separate paths.
|
|
2
|
+
export function callOptions(options, frameworkSignal) {
|
|
3
|
+
if (!frameworkSignal) return options;
|
|
4
|
+
const signal =
|
|
5
|
+
options.signal && options.signal !== frameworkSignal
|
|
6
|
+
? AbortSignal.any([options.signal, frameworkSignal])
|
|
7
|
+
: frameworkSignal;
|
|
8
|
+
return { ...options, signal };
|
|
9
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
|
+
import type { RunDiagnostics, SpliceRunOptions } from "../dist/core.js";
|
|
3
|
+
import type { ExecutableTool } from "./types.mjs";
|
|
4
|
+
|
|
5
|
+
export declare function asLangChainTool<Result>(
|
|
6
|
+
workflow: ExecutableTool<Result>,
|
|
7
|
+
options?: SpliceRunOptions & { returnDirect?: boolean },
|
|
8
|
+
): StructuredToolInterface;
|
|
9
|
+
|
|
10
|
+
import type { ResultTool } from "../dist/core.js";
|
|
11
|
+
|
|
12
|
+
/** Consume streaming results before the callback finishes. */
|
|
13
|
+
export declare function withLangChainTools<R>(
|
|
14
|
+
existing: readonly (ResultTool | StructuredToolInterface)[],
|
|
15
|
+
question: string,
|
|
16
|
+
use: (
|
|
17
|
+
tools: StructuredToolInterface[],
|
|
18
|
+
activeTools: () => StructuredToolInterface[],
|
|
19
|
+
) => R,
|
|
20
|
+
options?: { enabled?: boolean; diagnostics?: RunDiagnostics },
|
|
21
|
+
): Promise<Awaited<R>>;
|
|
22
|
+
|
|
23
|
+
export declare function streamLangChainTools<T>(
|
|
24
|
+
existing: readonly (ResultTool | StructuredToolInterface)[],
|
|
25
|
+
question: string,
|
|
26
|
+
use: (
|
|
27
|
+
tools: StructuredToolInterface[],
|
|
28
|
+
activeTools: () => StructuredToolInterface[],
|
|
29
|
+
) => AsyncIterable<T> | PromiseLike<AsyncIterable<T>>,
|
|
30
|
+
options?: { enabled?: boolean; diagnostics?: RunDiagnostics },
|
|
31
|
+
): AsyncGenerator<T>;
|