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,62 @@
|
|
|
1
|
+
import { type PageJob, type PageCheckpoint, type PageOutcome } from "./page-checkpoint.js";
|
|
2
|
+
import { type PageReadOptions } from "./read-pages.js";
|
|
3
|
+
import { type RowGrouping, type RowGroup } from "./row-groups.js";
|
|
4
|
+
type Scalar = string | number | boolean | null;
|
|
5
|
+
export type RowPredicate = {
|
|
6
|
+
field: string;
|
|
7
|
+
equals: Scalar;
|
|
8
|
+
} | {
|
|
9
|
+
field: string;
|
|
10
|
+
notEquals: Scalar;
|
|
11
|
+
} | {
|
|
12
|
+
field: string;
|
|
13
|
+
lt: number;
|
|
14
|
+
} | {
|
|
15
|
+
field: string;
|
|
16
|
+
lte: number;
|
|
17
|
+
} | {
|
|
18
|
+
field: string;
|
|
19
|
+
gt: number;
|
|
20
|
+
} | {
|
|
21
|
+
field: string;
|
|
22
|
+
gte: number;
|
|
23
|
+
} | {
|
|
24
|
+
field: string;
|
|
25
|
+
contains: string;
|
|
26
|
+
caseSensitive?: boolean;
|
|
27
|
+
};
|
|
28
|
+
export interface RowQuery {
|
|
29
|
+
where?: RowPredicate;
|
|
30
|
+
/** Include this field from every matching record, preserving duplicates and source order. */
|
|
31
|
+
values?: string;
|
|
32
|
+
groupBy?: RowGrouping;
|
|
33
|
+
}
|
|
34
|
+
export interface RowQueryResult {
|
|
35
|
+
count: number;
|
|
36
|
+
values?: unknown[];
|
|
37
|
+
groups?: RowGroup[];
|
|
38
|
+
}
|
|
39
|
+
/** Compute an exact count and optional projection over the supplied JSON array. */
|
|
40
|
+
export declare function queryRows(source: unknown, query?: RowQuery): RowQueryResult;
|
|
41
|
+
export interface RowPage {
|
|
42
|
+
rows: unknown[];
|
|
43
|
+
/** Omit, or use null, only when this is the last page. An empty string is a valid cursor. */
|
|
44
|
+
nextCursor?: string | null;
|
|
45
|
+
}
|
|
46
|
+
export type RowPageOptions<C extends PageCheckpoint | undefined = undefined> = PageReadOptions & {
|
|
47
|
+
checkpoint?: C;
|
|
48
|
+
};
|
|
49
|
+
type Reader = (cursor: string | undefined, signal?: AbortSignal) => RowPage | PromiseLike<RowPage>;
|
|
50
|
+
/** Follow every supplied continuation before returning an exact row count or projection. */
|
|
51
|
+
export declare function queryRowPages(read: Reader, query?: RowQuery, options?: PageReadOptions & {
|
|
52
|
+
checkpoint?: undefined;
|
|
53
|
+
}): Promise<RowQueryResult>;
|
|
54
|
+
export declare function queryRowPages(read: Reader, query: RowQuery | undefined, options: RowPageOptions<PageCheckpoint> & {
|
|
55
|
+
checkpoint: PageCheckpoint;
|
|
56
|
+
}): Promise<PageOutcome<RowQueryResult>>;
|
|
57
|
+
export declare function queryRowPages(read: Reader, query: RowQuery | undefined, options: RowPageOptions<PageCheckpoint | undefined>): Promise<RowQueryResult | PageOutcome<RowQueryResult>>;
|
|
58
|
+
/** Open a resumable row projection or grouping over a stable source snapshot. */
|
|
59
|
+
export declare function rowPageJob(query: RowQuery, options: PageReadOptions & {
|
|
60
|
+
checkpoint: PageCheckpoint;
|
|
61
|
+
}): PageJob<RowPage, RowQueryResult>;
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { createPageJob, } from "./page-checkpoint.js";
|
|
2
|
+
import { pageLimits, readPages } from "./read-pages.js";
|
|
3
|
+
import { copyJson, jsonObject } from "./workflow-json.js";
|
|
4
|
+
import { rowGroups } from "./row-groups.js";
|
|
5
|
+
const numericComparisons = new Map([
|
|
6
|
+
["lt", (a, b) => a < b],
|
|
7
|
+
["lte", (a, b) => a <= b],
|
|
8
|
+
["gt", (a, b) => a > b],
|
|
9
|
+
["gte", (a, b) => a >= b],
|
|
10
|
+
]);
|
|
11
|
+
const comparable = (n) => typeof n === "number" &&
|
|
12
|
+
Number.isFinite(n) &&
|
|
13
|
+
(!Number.isInteger(n) || Number.isSafeInteger(n));
|
|
14
|
+
const owns = (object, key) => Object.hasOwn(object, key);
|
|
15
|
+
const fieldName = (v) => typeof v === "string" && v.length > 0 && v.length <= 2048;
|
|
16
|
+
function predicate(where) {
|
|
17
|
+
if (where === undefined)
|
|
18
|
+
return () => true;
|
|
19
|
+
if (!jsonObject(where) || !fieldName(where.field))
|
|
20
|
+
throw new TypeError("A row predicate needs a named field.");
|
|
21
|
+
const field = where.field;
|
|
22
|
+
const operators = [
|
|
23
|
+
"equals",
|
|
24
|
+
"notEquals",
|
|
25
|
+
"contains",
|
|
26
|
+
...numericComparisons.keys(),
|
|
27
|
+
].filter((key) => owns(where, key));
|
|
28
|
+
const op = operators[0];
|
|
29
|
+
if (operators.length !== 1 ||
|
|
30
|
+
!op ||
|
|
31
|
+
Object.keys(where).some((k) => k !== "field" &&
|
|
32
|
+
k !== op &&
|
|
33
|
+
!(op === "contains" && k === "caseSensitive")))
|
|
34
|
+
throw new TypeError("Choose one row predicate.");
|
|
35
|
+
const value = where[op];
|
|
36
|
+
let match;
|
|
37
|
+
const compare = numericComparisons.get(op);
|
|
38
|
+
if (compare) {
|
|
39
|
+
if (!comparable(value))
|
|
40
|
+
throw new TypeError("Numeric comparisons require a finite, precise number.");
|
|
41
|
+
match = (actual) => {
|
|
42
|
+
if (!comparable(actual))
|
|
43
|
+
throw new TypeError("Numeric comparisons require a finite, precise numeric column.");
|
|
44
|
+
return compare(actual, value);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
else if (op === "contains") {
|
|
48
|
+
if (typeof value !== "string" ||
|
|
49
|
+
!value.length ||
|
|
50
|
+
(owns(where, "caseSensitive") && typeof where.caseSensitive !== "boolean"))
|
|
51
|
+
throw new TypeError("contains requires nonempty text and optional boolean caseSensitive.");
|
|
52
|
+
const sensitive = where.caseSensitive !== false;
|
|
53
|
+
const term = sensitive ? value : value.toLowerCase();
|
|
54
|
+
match = (actual) => {
|
|
55
|
+
if (typeof actual !== "string")
|
|
56
|
+
throw new TypeError("Text matching requires a string column.");
|
|
57
|
+
return (sensitive ? actual : actual.toLowerCase()).includes(term);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
if (value !== null &&
|
|
62
|
+
!["string", "number", "boolean"].includes(typeof value))
|
|
63
|
+
throw new TypeError("A row comparison requires a JSON scalar.");
|
|
64
|
+
match = (actual) => {
|
|
65
|
+
if (actual !== null && typeof actual === "object")
|
|
66
|
+
throw new TypeError("A row comparison requires a scalar column.");
|
|
67
|
+
return op === "equals" ? actual === value : actual !== value;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return (row) => {
|
|
71
|
+
if (!owns(row, field))
|
|
72
|
+
throw new TypeError("Predicate field missing from a source row.");
|
|
73
|
+
return match(row[field]);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function rowAccumulator(query) {
|
|
77
|
+
const q = copyJson(query);
|
|
78
|
+
if (!jsonObject(q) ||
|
|
79
|
+
Object.keys(q).some((k) => k !== "where" && k !== "values" && k !== "groupBy"))
|
|
80
|
+
throw new TypeError("Invalid row query.");
|
|
81
|
+
if (q.values !== undefined && !fieldName(q.values))
|
|
82
|
+
throw new TypeError("Invalid projection field.");
|
|
83
|
+
const groups = q.groupBy === undefined ? undefined : rowGroups(q.groupBy);
|
|
84
|
+
const field = q.values;
|
|
85
|
+
const matches = predicate(q.where);
|
|
86
|
+
const result = field === undefined ? { count: 0 } : { count: 0, values: [] };
|
|
87
|
+
function append(rows) {
|
|
88
|
+
if (!Array.isArray(rows))
|
|
89
|
+
throw new TypeError("Expected an array of source rows.");
|
|
90
|
+
for (const row of rows) {
|
|
91
|
+
if (!jsonObject(row))
|
|
92
|
+
throw new TypeError("Expected an object for each source row.");
|
|
93
|
+
if (field !== undefined && !owns(row, field))
|
|
94
|
+
throw new TypeError("Projection field missing from a source row.");
|
|
95
|
+
if (!matches(row))
|
|
96
|
+
continue;
|
|
97
|
+
result.count++;
|
|
98
|
+
groups?.append(row);
|
|
99
|
+
if (field !== undefined)
|
|
100
|
+
result.values.push(row[field]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
append,
|
|
105
|
+
finish: () => (groups ? { ...result, groups: groups.finish() } : result),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** Compute an exact count and optional projection over the supplied JSON array. */
|
|
109
|
+
export function queryRows(source, query = {}) {
|
|
110
|
+
const { finish, append } = rowAccumulator(query);
|
|
111
|
+
append(copyJson(source));
|
|
112
|
+
return finish();
|
|
113
|
+
}
|
|
114
|
+
export async function queryRowPages(read, query = {}, options = {}) {
|
|
115
|
+
if (options.checkpoint !== undefined) {
|
|
116
|
+
return rowPageJob(query, {
|
|
117
|
+
...options,
|
|
118
|
+
checkpoint: options.checkpoint,
|
|
119
|
+
}).run(({ cursor }, signal) => read(cursor ?? undefined, signal));
|
|
120
|
+
}
|
|
121
|
+
const { maxPages, maxBytes } = pageLimits(options);
|
|
122
|
+
const { finish, append } = rowAccumulator(query);
|
|
123
|
+
let bytes = 0;
|
|
124
|
+
for await (const page of readPages(read, maxPages, options)) {
|
|
125
|
+
if (!jsonObject(page) || !Array.isArray(page.rows))
|
|
126
|
+
throw new TypeError("Expected a row page.");
|
|
127
|
+
const rows = copyJson(page.rows);
|
|
128
|
+
bytes += Buffer.byteLength(JSON.stringify(rows));
|
|
129
|
+
if (bytes > maxBytes)
|
|
130
|
+
throw new RangeError("Row query exceeded the byte limit.");
|
|
131
|
+
append(rows);
|
|
132
|
+
}
|
|
133
|
+
return finish();
|
|
134
|
+
}
|
|
135
|
+
/** Open a resumable row projection or grouping over a stable source snapshot. */
|
|
136
|
+
export function rowPageJob(query, options) {
|
|
137
|
+
const q = copyJson(query);
|
|
138
|
+
rowAccumulator(q); // Validate even on a completed replay.
|
|
139
|
+
return createPageJob(options.checkpoint, "rows", q, options, () => {
|
|
140
|
+
const result = q.values === undefined ? { count: 0 } : { count: 0, values: [] };
|
|
141
|
+
const groups = q.groupBy ? rowGroups(q.groupBy) : undefined;
|
|
142
|
+
const localQuery = copyJson(q);
|
|
143
|
+
if (localQuery.groupBy) {
|
|
144
|
+
delete localQuery.groupBy.top;
|
|
145
|
+
delete localQuery.groupBy.ties;
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
project(page) {
|
|
149
|
+
if (!jsonObject(page) || !Array.isArray(page.rows))
|
|
150
|
+
throw new TypeError("Expected a row page.");
|
|
151
|
+
const rows = copyJson(page.rows);
|
|
152
|
+
const local = rowAccumulator(localQuery);
|
|
153
|
+
local.append(rows);
|
|
154
|
+
return {
|
|
155
|
+
part: local.finish(),
|
|
156
|
+
bytes: Buffer.byteLength(JSON.stringify(rows)),
|
|
157
|
+
};
|
|
158
|
+
},
|
|
159
|
+
append(part) {
|
|
160
|
+
const slice = part;
|
|
161
|
+
result.count += slice.count;
|
|
162
|
+
if (result.values)
|
|
163
|
+
for (const value of slice.values)
|
|
164
|
+
result.values.push(value);
|
|
165
|
+
if (groups)
|
|
166
|
+
for (const item of slice.groups)
|
|
167
|
+
groups.merge(item.key, item.count);
|
|
168
|
+
},
|
|
169
|
+
finish: () => groups ? { ...result, groups: groups.finish() } : result,
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
}
|
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type RuleOperand = {
|
|
2
|
+
source: string;
|
|
3
|
+
} | {
|
|
4
|
+
row: string;
|
|
5
|
+
} | {
|
|
6
|
+
mapped: string;
|
|
7
|
+
} | {
|
|
8
|
+
context: string;
|
|
9
|
+
} | {
|
|
10
|
+
value: unknown;
|
|
11
|
+
};
|
|
12
|
+
export interface SpliceRule {
|
|
13
|
+
id: string;
|
|
14
|
+
left: RuleOperand;
|
|
15
|
+
op: "equals" | "in" | "lte" | "gte";
|
|
16
|
+
right: RuleOperand;
|
|
17
|
+
}
|
|
18
|
+
export declare function validateRules(value: unknown, hasContext: boolean): asserts value is SpliceRule[];
|
|
19
|
+
/** One evaluator/budget per mapping invocation; paths/codes never contain operand values. */
|
|
20
|
+
export declare function ruleEvaluator(rules?: SpliceRule[]): (values: {
|
|
21
|
+
source: unknown;
|
|
22
|
+
row: unknown;
|
|
23
|
+
mapped: unknown;
|
|
24
|
+
context: unknown;
|
|
25
|
+
}, row: number) => {
|
|
26
|
+
severity: "error";
|
|
27
|
+
code: string;
|
|
28
|
+
path: string;
|
|
29
|
+
message: string;
|
|
30
|
+
} | undefined;
|
package/dist/rules.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Explicit, data-only application invariants. No eval, callbacks or coercion.
|
|
2
|
+
import { jsonObject, pointerParts, jsonFingerprint } from "./workflow-json.js";
|
|
3
|
+
export function validateRules(value, hasContext) {
|
|
4
|
+
if (!Array.isArray(value) || value.length < 1 || value.length > 32)
|
|
5
|
+
throw new Error("Rules require 1–32 named conditions.");
|
|
6
|
+
const ids = new Set();
|
|
7
|
+
for (const rule of value) {
|
|
8
|
+
if (!jsonObject(rule) || Object.keys(rule).sort().join() !== "id,left,op,right" ||
|
|
9
|
+
typeof rule.id !== "string" || !/^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(rule.id) || ids.has(rule.id) ||
|
|
10
|
+
!["equals", "in", "lte", "gte"].includes(String(rule.op)))
|
|
11
|
+
throw new Error("Each rule needs a distinct id, left, op and right.");
|
|
12
|
+
ids.add(rule.id);
|
|
13
|
+
for (const operand of [rule.left, rule.right]) {
|
|
14
|
+
if (!jsonObject(operand) || Object.keys(operand).length !== 1)
|
|
15
|
+
throw new Error("Rule operands need exactly one source, row, mapped, context or value.");
|
|
16
|
+
const key = Object.keys(operand)[0];
|
|
17
|
+
if (key === "value")
|
|
18
|
+
continue;
|
|
19
|
+
if (!["source", "row", "mapped", "context"].includes(key) || typeof operand[key] !== "string" ||
|
|
20
|
+
(key === "context" && !hasContext) || pointerParts(operand[key]).length > 32)
|
|
21
|
+
throw new Error("Invalid rule reference; context references require contextSchema.");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const missing = Symbol("missing");
|
|
26
|
+
function lookup(root, pointer) {
|
|
27
|
+
for (const key of pointerParts(pointer)) {
|
|
28
|
+
if (!root || typeof root !== "object" || !Object.hasOwn(root, key) ||
|
|
29
|
+
(Array.isArray(root) && !/^(0|[1-9]\d*)$/.test(key)))
|
|
30
|
+
return missing;
|
|
31
|
+
root = root[key];
|
|
32
|
+
}
|
|
33
|
+
return root;
|
|
34
|
+
}
|
|
35
|
+
/** One evaluator/budget per mapping invocation; paths/codes never contain operand values. */
|
|
36
|
+
export function ruleEvaluator(rules = []) {
|
|
37
|
+
let bytes = 0;
|
|
38
|
+
return (values, row) => {
|
|
39
|
+
for (const [index, rule] of rules.entries()) {
|
|
40
|
+
const path = `/rows/${row}/rules/${index}`;
|
|
41
|
+
const operand = (ref) => {
|
|
42
|
+
const key = Object.keys(ref)[0];
|
|
43
|
+
return key === "value" ? ref.value : lookup(values[key], ref[key]);
|
|
44
|
+
};
|
|
45
|
+
const left = operand(rule.left), right = operand(rule.right);
|
|
46
|
+
let code;
|
|
47
|
+
if (left === missing || right === missing)
|
|
48
|
+
code = "rule-field-missing";
|
|
49
|
+
else {
|
|
50
|
+
const size = Buffer.byteLength(JSON.stringify([left, right]));
|
|
51
|
+
bytes += size;
|
|
52
|
+
if (size > 5 * 1024 * 1024 || bytes > 16 * 1024 * 1024)
|
|
53
|
+
code = "rule-budget-exceeded";
|
|
54
|
+
else if (rule.op === "in" && (!Array.isArray(right) || right.length > 1000))
|
|
55
|
+
code = "rule-type-mismatch";
|
|
56
|
+
else if ((rule.op === "lte" || rule.op === "gte") && (typeof left !== "number" || typeof right !== "number"))
|
|
57
|
+
code = "rule-type-mismatch";
|
|
58
|
+
else {
|
|
59
|
+
const leftHash = rule.op === "equals" || rule.op === "in" ? jsonFingerprint(left) : "";
|
|
60
|
+
const matches = rule.op === "equals" ? leftHash === jsonFingerprint(right)
|
|
61
|
+
: rule.op === "in" ? right.some(v => jsonFingerprint(v) === leftHash)
|
|
62
|
+
: rule.op === "lte" ? left <= right : left >= right;
|
|
63
|
+
if (!matches)
|
|
64
|
+
code = "rule-failed";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (code)
|
|
68
|
+
return { severity: "error", code, path, message: `Application rule ${rule.id} rejected row ${row}.` };
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { ToolRoute } from "./route-evaluation.js";
|
|
2
|
+
export interface DiagnosticModelUsage {
|
|
3
|
+
inputTokens: number;
|
|
4
|
+
outputTokens: number;
|
|
5
|
+
cachedInputTokens?: number;
|
|
6
|
+
costUsd?: number;
|
|
7
|
+
}
|
|
8
|
+
type RunOutcome = "ok" | "error" | "cancelled" | "timeout" | "closed";
|
|
9
|
+
export type DiagnosticEvent = {
|
|
10
|
+
type: "route";
|
|
11
|
+
route: ToolRoute;
|
|
12
|
+
} | {
|
|
13
|
+
type: "read";
|
|
14
|
+
operation: "search" | "fetch";
|
|
15
|
+
} | {
|
|
16
|
+
type: "model";
|
|
17
|
+
inputTokens: number;
|
|
18
|
+
outputTokens: number;
|
|
19
|
+
cachedInputTokens: number;
|
|
20
|
+
costUsd: number | null;
|
|
21
|
+
} | {
|
|
22
|
+
type: "upstream-bytes";
|
|
23
|
+
bytes: number;
|
|
24
|
+
} | {
|
|
25
|
+
type: "view";
|
|
26
|
+
inputBytes: number;
|
|
27
|
+
visibleBytes: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: "finish";
|
|
30
|
+
outcome: RunOutcome;
|
|
31
|
+
} | {
|
|
32
|
+
type: "inspect" | "retry" | "deadline" | "cancel" | "handoff-error";
|
|
33
|
+
};
|
|
34
|
+
export type TimedDiagnosticEvent = DiagnosticEvent & {
|
|
35
|
+
elapsedMs: number;
|
|
36
|
+
};
|
|
37
|
+
/** One observer per agent run. Events contain bounded operation names, counts and timing only. */
|
|
38
|
+
export declare class RunDiagnostics {
|
|
39
|
+
#private;
|
|
40
|
+
constructor(options?: {
|
|
41
|
+
onEvent?: (event: TimedDiagnosticEvent) => unknown;
|
|
42
|
+
});
|
|
43
|
+
run<T>(route: ToolRoute, work: () => T | PromiseLike<T>): Promise<Awaited<T>>;
|
|
44
|
+
stream<T>(route: ToolRoute, work: () => AsyncIterable<T> | PromiseLike<AsyncIterable<T>>): AsyncGenerator<T>;
|
|
45
|
+
/** Attach provider-reported usage. Omit cost when the host has no price. */
|
|
46
|
+
modelCall(usage: DiagnosticModelUsage): void;
|
|
47
|
+
snapshot(): {
|
|
48
|
+
searches: number;
|
|
49
|
+
fetches: number;
|
|
50
|
+
retries: number;
|
|
51
|
+
inspections: number;
|
|
52
|
+
views: number;
|
|
53
|
+
upstreamBytes: number;
|
|
54
|
+
inputBytes: number;
|
|
55
|
+
visibleBytes: number;
|
|
56
|
+
deadlines: number;
|
|
57
|
+
cancellations: number;
|
|
58
|
+
handoffErrors: number;
|
|
59
|
+
modelCalls: number;
|
|
60
|
+
inputTokens: number;
|
|
61
|
+
outputTokens: number;
|
|
62
|
+
cachedInputTokens: number;
|
|
63
|
+
knownCostUsd: number;
|
|
64
|
+
unpricedCalls: number;
|
|
65
|
+
route: "adaptive" | "configured" | "native" | null;
|
|
66
|
+
status: string;
|
|
67
|
+
elapsedMs: number;
|
|
68
|
+
};
|
|
69
|
+
/** @internal Only SDK instrumentation supplies events. */
|
|
70
|
+
record(event: DiagnosticEvent): void;
|
|
71
|
+
}
|
|
72
|
+
export declare function currentDiagnostics(): RunDiagnostics | undefined;
|
|
73
|
+
export declare function recordReadResult<T>(value: T): T;
|
|
74
|
+
export declare function recordView(input: unknown, visible: unknown): void;
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
function outcomeOf(error) {
|
|
3
|
+
return error instanceof Error && error.name === "AbortError"
|
|
4
|
+
? "cancelled"
|
|
5
|
+
: error instanceof Error && error.name === "TimeoutError"
|
|
6
|
+
? "timeout"
|
|
7
|
+
: "error";
|
|
8
|
+
}
|
|
9
|
+
const active = new AsyncLocalStorage();
|
|
10
|
+
/** One observer per agent run. Events contain bounded operation names, counts and timing only. */
|
|
11
|
+
export class RunDiagnostics {
|
|
12
|
+
#started;
|
|
13
|
+
#ended;
|
|
14
|
+
#route;
|
|
15
|
+
#outcome;
|
|
16
|
+
#events;
|
|
17
|
+
#counts = {
|
|
18
|
+
searches: 0,
|
|
19
|
+
fetches: 0,
|
|
20
|
+
retries: 0,
|
|
21
|
+
inspections: 0,
|
|
22
|
+
views: 0,
|
|
23
|
+
upstreamBytes: 0,
|
|
24
|
+
inputBytes: 0,
|
|
25
|
+
visibleBytes: 0,
|
|
26
|
+
deadlines: 0,
|
|
27
|
+
cancellations: 0,
|
|
28
|
+
handoffErrors: 0,
|
|
29
|
+
modelCalls: 0,
|
|
30
|
+
inputTokens: 0,
|
|
31
|
+
outputTokens: 0,
|
|
32
|
+
cachedInputTokens: 0,
|
|
33
|
+
knownCostUsd: 0,
|
|
34
|
+
unpricedCalls: 0,
|
|
35
|
+
};
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
if (options.onEvent !== undefined && typeof options.onEvent !== "function")
|
|
38
|
+
throw new TypeError("onEvent must be callable.");
|
|
39
|
+
this.#events = options.onEvent;
|
|
40
|
+
}
|
|
41
|
+
#start(route) {
|
|
42
|
+
if (this.#started !== undefined ||
|
|
43
|
+
!["native", "adaptive", "configured"].includes(route))
|
|
44
|
+
throw new Error("Use a fresh diagnostic observer per run and a valid route.");
|
|
45
|
+
this.#started = performance.now();
|
|
46
|
+
this.#route = route;
|
|
47
|
+
this.record({ type: "route", route });
|
|
48
|
+
}
|
|
49
|
+
#finish(outcome) {
|
|
50
|
+
this.#outcome = outcome;
|
|
51
|
+
this.record({ type: "finish", outcome });
|
|
52
|
+
this.#ended = performance.now();
|
|
53
|
+
}
|
|
54
|
+
async run(route, work) {
|
|
55
|
+
if (active.getStore() === this && this.#ended === undefined)
|
|
56
|
+
return await work();
|
|
57
|
+
this.#start(route);
|
|
58
|
+
let outcome = "ok";
|
|
59
|
+
try {
|
|
60
|
+
return await active.run(this, work);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
outcome = outcomeOf(error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
this.#finish(outcome);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async *stream(route, work) {
|
|
71
|
+
if (active.getStore() === this && this.#ended === undefined) {
|
|
72
|
+
yield* await work();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.#start(route);
|
|
76
|
+
let outcome = "closed";
|
|
77
|
+
try {
|
|
78
|
+
const iterator = await active.run(this, async () => (await work())[Symbol.asyncIterator]());
|
|
79
|
+
const scoped = {
|
|
80
|
+
next: (...args) => active.run(this, () => iterator.next(...args)),
|
|
81
|
+
...(iterator.return && {
|
|
82
|
+
return: (value) => active.run(this, () => iterator.return(value)),
|
|
83
|
+
}),
|
|
84
|
+
...(iterator.throw && {
|
|
85
|
+
throw: (error) => active.run(this, () => iterator.throw(error)),
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
yield* { [Symbol.asyncIterator]: () => scoped };
|
|
89
|
+
outcome = "ok";
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
outcome = outcomeOf(error);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
finally {
|
|
96
|
+
this.#finish(outcome);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/** Attach provider-reported usage. Omit cost when the host has no price. */
|
|
100
|
+
modelCall(usage) {
|
|
101
|
+
const { inputTokens, outputTokens, cachedInputTokens = 0, costUsd } = usage;
|
|
102
|
+
if (![inputTokens, outputTokens, cachedInputTokens].every((n) => Number.isSafeInteger(n) && n >= 0) ||
|
|
103
|
+
cachedInputTokens > inputTokens ||
|
|
104
|
+
(costUsd !== undefined && !(Number.isFinite(costUsd) && costUsd >= 0)))
|
|
105
|
+
throw new TypeError("Invalid model usage.");
|
|
106
|
+
this.record({
|
|
107
|
+
type: "model",
|
|
108
|
+
inputTokens,
|
|
109
|
+
outputTokens,
|
|
110
|
+
cachedInputTokens,
|
|
111
|
+
costUsd: costUsd ?? null,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
snapshot() {
|
|
115
|
+
return {
|
|
116
|
+
route: this.#route ?? null,
|
|
117
|
+
status: this.#outcome ?? (this.#started === undefined ? "idle" : "running"),
|
|
118
|
+
elapsedMs: this.#started === undefined
|
|
119
|
+
? 0
|
|
120
|
+
: (this.#ended ?? performance.now()) - this.#started,
|
|
121
|
+
...this.#counts,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/** @internal Only SDK instrumentation supplies events. */
|
|
125
|
+
record(event) {
|
|
126
|
+
if (this.#started === undefined || this.#ended !== undefined)
|
|
127
|
+
return;
|
|
128
|
+
switch (event.type) {
|
|
129
|
+
case "read":
|
|
130
|
+
this.#counts[event.operation === "search" ? "searches" : "fetches"]++;
|
|
131
|
+
break;
|
|
132
|
+
case "model":
|
|
133
|
+
this.#counts.modelCalls++;
|
|
134
|
+
this.#counts.inputTokens += event.inputTokens;
|
|
135
|
+
this.#counts.outputTokens += event.outputTokens;
|
|
136
|
+
this.#counts.cachedInputTokens += event.cachedInputTokens;
|
|
137
|
+
if (event.costUsd === null)
|
|
138
|
+
this.#counts.unpricedCalls++;
|
|
139
|
+
else
|
|
140
|
+
this.#counts.knownCostUsd += event.costUsd;
|
|
141
|
+
break;
|
|
142
|
+
case "upstream-bytes":
|
|
143
|
+
this.#counts.upstreamBytes += event.bytes;
|
|
144
|
+
break;
|
|
145
|
+
case "view":
|
|
146
|
+
this.#counts.views++;
|
|
147
|
+
this.#counts.inputBytes += event.inputBytes;
|
|
148
|
+
this.#counts.visibleBytes += event.visibleBytes;
|
|
149
|
+
break;
|
|
150
|
+
case "inspect":
|
|
151
|
+
this.#counts.inspections++;
|
|
152
|
+
break;
|
|
153
|
+
case "retry":
|
|
154
|
+
this.#counts.retries++;
|
|
155
|
+
break;
|
|
156
|
+
case "deadline":
|
|
157
|
+
this.#counts.deadlines++;
|
|
158
|
+
break;
|
|
159
|
+
case "cancel":
|
|
160
|
+
this.#counts.cancellations++;
|
|
161
|
+
break;
|
|
162
|
+
case "handoff-error":
|
|
163
|
+
this.#counts.handoffErrors++;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
if (this.#events) {
|
|
167
|
+
try {
|
|
168
|
+
const result = this.#events({
|
|
169
|
+
...event,
|
|
170
|
+
elapsedMs: performance.now() - this.#started,
|
|
171
|
+
});
|
|
172
|
+
if (result &&
|
|
173
|
+
typeof result.then === "function")
|
|
174
|
+
Promise.resolve(result).catch(() => { });
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
/* Exporter failures do not alter tool results. */
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export function currentDiagnostics() {
|
|
183
|
+
return active.getStore();
|
|
184
|
+
}
|
|
185
|
+
const byteLength = (value) => Buffer.byteLength(typeof value === "string" ? value : JSON.stringify(value));
|
|
186
|
+
export function recordReadResult(value) {
|
|
187
|
+
const trace = active.getStore();
|
|
188
|
+
if (trace) {
|
|
189
|
+
try {
|
|
190
|
+
trace.record({ type: "upstream-bytes", bytes: byteLength(value) });
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
/* Optional measurement. */
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
export function recordView(input, visible) {
|
|
199
|
+
const trace = active.getStore();
|
|
200
|
+
if (!trace)
|
|
201
|
+
return;
|
|
202
|
+
try {
|
|
203
|
+
trace.record({
|
|
204
|
+
type: "view",
|
|
205
|
+
inputBytes: byteLength(input),
|
|
206
|
+
visibleBytes: byteLength(visible),
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
/* Framework values may use a non-JSON representation. */
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { SpliceRunOptions } from "./splice.js";
|
|
2
|
+
import type { ResultTool } from "./result-session.js";
|
|
3
|
+
export interface SearchFetchOptions {
|
|
4
|
+
origins: readonly string[];
|
|
5
|
+
pageView?: "page" | "markdown";
|
|
6
|
+
/** Return the first ATX heading of this level in the Markdown view. Defaults to H1. */
|
|
7
|
+
headingLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
8
|
+
name?: string;
|
|
9
|
+
/** Opt in only for a repeatable read. Retry the same URL once when this predicate accepts the failure. */
|
|
10
|
+
retryFetchIf?: (error: unknown) => boolean | PromiseLike<boolean>;
|
|
11
|
+
/** One-based rank, or let the caller choose a rank in the tool input. */
|
|
12
|
+
rank?: number | "input";
|
|
13
|
+
resultsPath?: string;
|
|
14
|
+
urlPath?: string;
|
|
15
|
+
urlArgument?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare class SearchFetchTool implements ResultTool {
|
|
18
|
+
#private;
|
|
19
|
+
private search;
|
|
20
|
+
private fetchPage;
|
|
21
|
+
readonly definition: {
|
|
22
|
+
name: string;
|
|
23
|
+
description: string;
|
|
24
|
+
inputSchema: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
constructor(search: ResultTool, fetchPage: ResultTool, options: SearchFetchOptions);
|
|
27
|
+
execute(args: Record<string, unknown>, options?: SpliceRunOptions): Promise<{
|
|
28
|
+
status: "no_match";
|
|
29
|
+
rank: number;
|
|
30
|
+
totalResults: number;
|
|
31
|
+
requestedUrl?: undefined;
|
|
32
|
+
page?: undefined;
|
|
33
|
+
} | {
|
|
34
|
+
heading: string | null;
|
|
35
|
+
literal?: string | undefined;
|
|
36
|
+
count?: number | undefined;
|
|
37
|
+
status?: undefined;
|
|
38
|
+
rank?: undefined;
|
|
39
|
+
totalResults?: undefined;
|
|
40
|
+
requestedUrl: string;
|
|
41
|
+
page?: undefined;
|
|
42
|
+
} | {
|
|
43
|
+
status?: undefined;
|
|
44
|
+
rank?: undefined;
|
|
45
|
+
totalResults?: undefined;
|
|
46
|
+
requestedUrl: string;
|
|
47
|
+
page: unknown;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
export declare function searchThenFetch(search: ResultTool, fetchPage: ResultTool, options: SearchFetchOptions): SearchFetchTool;
|