toolsplice 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 +62 -330
- 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/cli.js +545 -14
- 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/index.d.ts +32 -1
- package/dist/index.js +28 -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/probe.js +6 -19
- 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-driver-cli.d.ts +3 -0
- package/dist/route-driver-cli.js +43 -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-measure.d.ts +33 -0
- package/dist/splice-measure.js +55 -0
- package/dist/splice-project.d.ts +12 -6
- package/dist/splice-project.js +72 -20
- 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.d.ts +1 -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/v2.js +25 -8
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/workflow-json.js +1 -1
- package/docs/AGENT-FRAMEWORKS.md +431 -0
- package/docs/BENCHMARKS.md +43 -0
- package/docs/INTEGRATION-QUICKSTART.md +859 -0
- package/docs/LEARNING-AND-STORAGE.md +393 -0
- package/docs/MCP.md +204 -0
- package/docs/PROJECTS-AND-UPGRADES.md +67 -9
- package/docs/PYTHON-REFERENCE.md +348 -0
- package/docs/PYTHON.md +120 -0
- package/docs/RECORDING-AND-CORRECTIONS.md +30 -1
- package/docs/TESTING.md +259 -0
- package/examples/mcp-app/build.mjs +11 -0
- package/examples/mcp-app/view.mjs +22 -0
- package/examples/quickstart.mjs +43 -0
- package/examples/route-driver.mjs +67 -0
- 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 +47 -3
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { jsonObject } from "./workflow-json.js";
|
|
2
|
+
const timestamp = /^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:\d{2})$/;
|
|
3
|
+
function utcDay(value) {
|
|
4
|
+
let ms;
|
|
5
|
+
if (typeof value === "number" && Number.isSafeInteger(value))
|
|
6
|
+
ms = value;
|
|
7
|
+
else {
|
|
8
|
+
const parts = typeof value === "string" && timestamp.exec(value);
|
|
9
|
+
if (!parts ||
|
|
10
|
+
parts[0] !== value ||
|
|
11
|
+
parts[1].startsWith("0000") ||
|
|
12
|
+
+parts[2] > 23 ||
|
|
13
|
+
+parts[3] > 59 ||
|
|
14
|
+
+parts[4] > 59)
|
|
15
|
+
throw new TypeError("Expected an offset-qualified timestamp or integer Unix milliseconds.");
|
|
16
|
+
const date = new Date(parts[1] + "T00:00:00Z");
|
|
17
|
+
if (!Number.isFinite(date.getTime()) ||
|
|
18
|
+
date.toISOString().slice(0, 10) !== parts[1])
|
|
19
|
+
throw new TypeError("Invalid calendar date.");
|
|
20
|
+
ms = Date.parse(value);
|
|
21
|
+
}
|
|
22
|
+
if (!Number.isFinite(ms) || ms < -62135596800000 || ms > 253402300799999)
|
|
23
|
+
throw new RangeError("UTC year must be between 0001 and 9999.");
|
|
24
|
+
return new Date(ms).toISOString().slice(0, 10);
|
|
25
|
+
}
|
|
26
|
+
function rank(key) {
|
|
27
|
+
return key === null
|
|
28
|
+
? 0
|
|
29
|
+
: typeof key === "boolean"
|
|
30
|
+
? 1
|
|
31
|
+
: typeof key === "number"
|
|
32
|
+
? 2
|
|
33
|
+
: 3;
|
|
34
|
+
}
|
|
35
|
+
function keyOrder(a, b) {
|
|
36
|
+
return (rank(a) - rank(b) ||
|
|
37
|
+
(a === b || a === null || b === null ? 0 : a < b ? -1 : 1));
|
|
38
|
+
}
|
|
39
|
+
export function rowGroups(spec) {
|
|
40
|
+
if (!jsonObject(spec) ||
|
|
41
|
+
typeof spec.field !== "string" ||
|
|
42
|
+
!spec.field.length ||
|
|
43
|
+
spec.field.length > 2048 ||
|
|
44
|
+
Object.keys(spec).some((k) => !["field", "bucket", "top", "ties"].includes(k)) ||
|
|
45
|
+
(spec.bucket !== undefined && spec.bucket !== "utcDay") ||
|
|
46
|
+
(spec.top !== undefined &&
|
|
47
|
+
(!Number.isSafeInteger(spec.top) || spec.top < 1)) ||
|
|
48
|
+
(spec.ties !== undefined &&
|
|
49
|
+
(spec.top === undefined || !["include", "truncate"].includes(spec.ties))))
|
|
50
|
+
throw new TypeError("Invalid groupBy specification.");
|
|
51
|
+
const counts = new Map();
|
|
52
|
+
return {
|
|
53
|
+
merge(key, count) {
|
|
54
|
+
counts.set(key, (counts.get(key) ?? 0) + count);
|
|
55
|
+
},
|
|
56
|
+
append(row) {
|
|
57
|
+
if (!Object.hasOwn(row, spec.field))
|
|
58
|
+
throw new TypeError("Grouping field missing from a source row.");
|
|
59
|
+
const key = spec.bucket ? utcDay(row[spec.field]) : row[spec.field];
|
|
60
|
+
if (!(key === null ||
|
|
61
|
+
typeof key === "boolean" ||
|
|
62
|
+
typeof key === "string" ||
|
|
63
|
+
(typeof key === "number" &&
|
|
64
|
+
Number.isFinite(key) &&
|
|
65
|
+
(!Number.isInteger(key) || Number.isSafeInteger(key)))))
|
|
66
|
+
throw new TypeError("Expected a precise JSON scalar group key.");
|
|
67
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
68
|
+
},
|
|
69
|
+
finish() {
|
|
70
|
+
const rows = Array.from(counts, ([key, count]) => ({ key, count })).sort((a, b) => b.count - a.count || keyOrder(a.key, b.key));
|
|
71
|
+
if (spec.top === undefined || spec.top >= rows.length)
|
|
72
|
+
return rows;
|
|
73
|
+
const boundary = rows[spec.top - 1].count;
|
|
74
|
+
return spec.ties === "truncate"
|
|
75
|
+
? rows.slice(0, spec.top)
|
|
76
|
+
: rows.filter((r) => r.count >= boundary);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -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 {};
|