@orkestrel/tool 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/index.cjs +195 -87
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +84 -24
- package/dist/src/core/index.d.ts +84 -24
- package/dist/src/core/index.js +195 -88
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/server/index.cjs +225 -160
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +68 -26
- package/dist/src/server/index.d.ts +68 -26
- package/dist/src/server/index.js +225 -162
- package/dist/src/server/index.js.map +1 -1
- package/package.json +20 -20
package/dist/src/core/index.cjs
CHANGED
|
@@ -560,12 +560,11 @@ var INFER_TOOL_DESCRIPTION = [
|
|
|
560
560
|
*/
|
|
561
561
|
var AgentToolError = class extends Error {
|
|
562
562
|
code;
|
|
563
|
-
context;
|
|
564
563
|
constructor(code, message, context) {
|
|
565
564
|
super(message);
|
|
566
565
|
this.name = "AgentToolError";
|
|
567
566
|
this.code = code;
|
|
568
|
-
this.context = context;
|
|
567
|
+
if (context !== void 0) this.context = context;
|
|
569
568
|
}
|
|
570
569
|
};
|
|
571
570
|
/**
|
|
@@ -1544,19 +1543,6 @@ function relationToolCode(error) {
|
|
|
1544
1543
|
* ```
|
|
1545
1544
|
*/
|
|
1546
1545
|
function expandInclude(paths, depth) {
|
|
1547
|
-
function merge(base, segments) {
|
|
1548
|
-
const [head, ...rest] = segments;
|
|
1549
|
-
const existing = base[head];
|
|
1550
|
-
if (rest.length === 0) return {
|
|
1551
|
-
...base,
|
|
1552
|
-
[head]: existing === void 0 ? true : existing
|
|
1553
|
-
};
|
|
1554
|
-
const nextBase = typeof existing === "object" ? existing : {};
|
|
1555
|
-
return {
|
|
1556
|
-
...base,
|
|
1557
|
-
[head]: merge(nextBase, rest)
|
|
1558
|
-
};
|
|
1559
|
-
}
|
|
1560
1546
|
let include = {};
|
|
1561
1547
|
for (const path of paths ?? []) {
|
|
1562
1548
|
const segments = path.split(".");
|
|
@@ -1564,7 +1550,42 @@ function expandInclude(paths, depth) {
|
|
|
1564
1550
|
path,
|
|
1565
1551
|
depth
|
|
1566
1552
|
});
|
|
1567
|
-
|
|
1553
|
+
const ancestors = [];
|
|
1554
|
+
let branch = include;
|
|
1555
|
+
const last = segments.length - 1;
|
|
1556
|
+
for (let index = 0; index < last; index++) {
|
|
1557
|
+
const segment = segments[index];
|
|
1558
|
+
if (segment === void 0) throw new AgentToolError("TOOL", `malformed include path '${path}'`, {
|
|
1559
|
+
path,
|
|
1560
|
+
depth
|
|
1561
|
+
});
|
|
1562
|
+
ancestors.push(branch);
|
|
1563
|
+
const existing = branch[segment];
|
|
1564
|
+
branch = typeof existing === "object" ? existing : {};
|
|
1565
|
+
}
|
|
1566
|
+
const leaf = segments[last];
|
|
1567
|
+
if (leaf === void 0) throw new AgentToolError("TOOL", `malformed include path '${path}'`, {
|
|
1568
|
+
path,
|
|
1569
|
+
depth
|
|
1570
|
+
});
|
|
1571
|
+
const existing = branch[leaf];
|
|
1572
|
+
let merged = {
|
|
1573
|
+
...branch,
|
|
1574
|
+
[leaf]: existing === void 0 ? true : existing
|
|
1575
|
+
};
|
|
1576
|
+
for (let index = last - 1; index >= 0; index--) {
|
|
1577
|
+
const ancestor = ancestors[index];
|
|
1578
|
+
const segment = segments[index];
|
|
1579
|
+
if (ancestor === void 0 || segment === void 0) throw new AgentToolError("TOOL", `malformed include path '${path}'`, {
|
|
1580
|
+
path,
|
|
1581
|
+
depth
|
|
1582
|
+
});
|
|
1583
|
+
merged = {
|
|
1584
|
+
...ancestor,
|
|
1585
|
+
[segment]: merged
|
|
1586
|
+
};
|
|
1587
|
+
}
|
|
1588
|
+
include = merged;
|
|
1568
1589
|
}
|
|
1569
1590
|
return include;
|
|
1570
1591
|
}
|
|
@@ -1592,7 +1613,11 @@ function relationManagerOf(managers, name) {
|
|
|
1592
1613
|
return manager;
|
|
1593
1614
|
}
|
|
1594
1615
|
const names = Object.keys(managers);
|
|
1595
|
-
|
|
1616
|
+
const [single] = names;
|
|
1617
|
+
if (names.length === 1 && single !== void 0) {
|
|
1618
|
+
const manager = managers[single];
|
|
1619
|
+
if (manager !== void 0) return manager;
|
|
1620
|
+
}
|
|
1596
1621
|
throw new AgentToolError("TOOL", "no relation manager resolved for the call", { managers: names });
|
|
1597
1622
|
}
|
|
1598
1623
|
/**
|
|
@@ -1820,6 +1845,105 @@ var DatabaseDefinitionStore = class {
|
|
|
1820
1845
|
}
|
|
1821
1846
|
};
|
|
1822
1847
|
//#endregion
|
|
1848
|
+
//#region src/core/databases/DatabaseResolver.ts
|
|
1849
|
+
/**
|
|
1850
|
+
* Resolve database definitions into cached live handles for database tools.
|
|
1851
|
+
*
|
|
1852
|
+
* @example
|
|
1853
|
+
* ```ts
|
|
1854
|
+
* import { DatabaseResolver } from '@orkestrel/tool'
|
|
1855
|
+
*
|
|
1856
|
+
* const resolver = new DatabaseResolver(handles, drivers, key, store)
|
|
1857
|
+
* const database = await resolver.resolve('shop')
|
|
1858
|
+
* ```
|
|
1859
|
+
*/
|
|
1860
|
+
var DatabaseResolver = class {
|
|
1861
|
+
#handles;
|
|
1862
|
+
#drivers;
|
|
1863
|
+
#key;
|
|
1864
|
+
#store;
|
|
1865
|
+
/**
|
|
1866
|
+
* Create a database resolver over the tool's live state and optional definition store.
|
|
1867
|
+
*
|
|
1868
|
+
* @param handles - Initial live database handles cached by id
|
|
1869
|
+
* @param drivers - Driver factories keyed by definition driver name
|
|
1870
|
+
* @param key - Key generator supplied to newly created databases
|
|
1871
|
+
* @param store - Optional persistent definition store
|
|
1872
|
+
*/
|
|
1873
|
+
constructor(handles, drivers, key, store) {
|
|
1874
|
+
this.#handles = new Map(handles);
|
|
1875
|
+
this.#drivers = drivers;
|
|
1876
|
+
this.#key = key;
|
|
1877
|
+
this.#store = store;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Determine whether a live database is cached by id.
|
|
1881
|
+
*
|
|
1882
|
+
* @param id - Database id
|
|
1883
|
+
* @returns Whether a live handle is cached
|
|
1884
|
+
*/
|
|
1885
|
+
has(id) {
|
|
1886
|
+
return this.#handles.has(id);
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* Read a cached database without consulting the definition store.
|
|
1890
|
+
*
|
|
1891
|
+
* @param id - Database id
|
|
1892
|
+
* @returns The cached live database, or `undefined`
|
|
1893
|
+
*/
|
|
1894
|
+
get(id) {
|
|
1895
|
+
return this.#handles.get(id);
|
|
1896
|
+
}
|
|
1897
|
+
/**
|
|
1898
|
+
* Cache a live database by id.
|
|
1899
|
+
*
|
|
1900
|
+
* @param id - Database id
|
|
1901
|
+
* @param database - Live database handle
|
|
1902
|
+
* @returns Nothing
|
|
1903
|
+
*/
|
|
1904
|
+
set(id, database) {
|
|
1905
|
+
this.#handles.set(id, database);
|
|
1906
|
+
}
|
|
1907
|
+
/**
|
|
1908
|
+
* Remove a cached live database by id.
|
|
1909
|
+
*
|
|
1910
|
+
* @param id - Database id
|
|
1911
|
+
* @returns Nothing
|
|
1912
|
+
*/
|
|
1913
|
+
delete(id) {
|
|
1914
|
+
this.#handles.delete(id);
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Resolve a cached or stored database by id.
|
|
1918
|
+
*
|
|
1919
|
+
* @param id - Database definition id
|
|
1920
|
+
* @returns The cached or newly constructed live database
|
|
1921
|
+
*/
|
|
1922
|
+
async resolve(id) {
|
|
1923
|
+
const cached = this.#handles.get(id);
|
|
1924
|
+
if (cached !== void 0) return cached;
|
|
1925
|
+
if (this.#store !== void 0) {
|
|
1926
|
+
const definition = await this.#store.get(id);
|
|
1927
|
+
if (definition !== void 0) {
|
|
1928
|
+
const factory = this.#drivers[definition.driver];
|
|
1929
|
+
if (factory === void 0) throw new AgentToolError("TOOL", `unknown driver '${definition.driver}'`, {
|
|
1930
|
+
id,
|
|
1931
|
+
driver: definition.driver
|
|
1932
|
+
});
|
|
1933
|
+
const handle = (0, _orkestrel_database.createDatabase)({
|
|
1934
|
+
driver: factory(),
|
|
1935
|
+
tables: expandTables(definition.tables),
|
|
1936
|
+
...definition.keys === void 0 ? {} : { keys: definition.keys },
|
|
1937
|
+
key: this.#key
|
|
1938
|
+
});
|
|
1939
|
+
this.set(id, handle);
|
|
1940
|
+
return handle;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
throw new AgentToolError("TOOL", `unknown database '${id}'`, { id });
|
|
1944
|
+
}
|
|
1945
|
+
};
|
|
1946
|
+
//#endregion
|
|
1823
1947
|
//#region src/core/factories.ts
|
|
1824
1948
|
/**
|
|
1825
1949
|
* Wrap a registered tool as a {@link WorkflowFunction} (`@orkestrel/workflow`) — the OPT-IN
|
|
@@ -1935,7 +2059,9 @@ function createAgentFunction(agent, options) {
|
|
|
1935
2059
|
}));
|
|
1936
2060
|
}
|
|
1937
2061
|
const signal = controller.signal;
|
|
1938
|
-
const onAbort = ()
|
|
2062
|
+
const onAbort = { handleEvent() {
|
|
2063
|
+
agent.abort(signal.reason);
|
|
2064
|
+
} };
|
|
1939
2065
|
if (signal.aborted) agent.abort(signal.reason);
|
|
1940
2066
|
else signal.addEventListener("abort", onAbort, { once: true });
|
|
1941
2067
|
try {
|
|
@@ -2040,12 +2166,13 @@ function createWorkflowTool(definition, runner, options) {
|
|
|
2040
2166
|
const depth = options?.depth ?? 0;
|
|
2041
2167
|
const ancestry = options?.ancestry ?? [];
|
|
2042
2168
|
const store = options?.store;
|
|
2169
|
+
const parameters = (0, _orkestrel_contract.schemaToParameters)(steps.schema);
|
|
2043
2170
|
return (0, _orkestrel_agent.createTool)({
|
|
2044
2171
|
name: WORKFLOW_TOOL_NAME,
|
|
2045
2172
|
description: WORKFLOW_TOOL_DESCRIPTION,
|
|
2046
2173
|
summary: WORKFLOW_TOOL_SUMMARY,
|
|
2047
|
-
parameters
|
|
2048
|
-
|
|
2174
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2175
|
+
async execute(args) {
|
|
2049
2176
|
let target;
|
|
2050
2177
|
if (Object.keys(args).length === 0) target = definition;
|
|
2051
2178
|
else if (Array.isArray(args.steps)) {
|
|
@@ -2129,8 +2256,8 @@ function createWorkspaceTool(options) {
|
|
|
2129
2256
|
name: options?.name ?? "workspace",
|
|
2130
2257
|
description: options?.description ?? WORKSPACE_TOOL_DESCRIPTION,
|
|
2131
2258
|
summary: WORKSPACE_TOOL_SUMMARY,
|
|
2132
|
-
parameters,
|
|
2133
|
-
execute
|
|
2259
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2260
|
+
execute(args) {
|
|
2134
2261
|
const op = contract.parse(args);
|
|
2135
2262
|
if (op === void 0) throw new _orkestrel_agent.WorkspaceError("TOOL", `unknown or malformed operation`, { args });
|
|
2136
2263
|
if (op.operation === "workspaces") {
|
|
@@ -2164,14 +2291,14 @@ function createWorkspaceTool(options) {
|
|
|
2164
2291
|
}));
|
|
2165
2292
|
case "has": return active?.has(op.path) ?? false;
|
|
2166
2293
|
case "search": return active?.search(op.query, {
|
|
2167
|
-
regex: op.regex,
|
|
2168
|
-
exact: op.exact,
|
|
2169
|
-
limit: op.limit
|
|
2294
|
+
...op.regex === void 0 ? {} : { regex: op.regex },
|
|
2295
|
+
...op.exact === void 0 ? {} : { exact: op.exact },
|
|
2296
|
+
...op.limit === void 0 ? {} : { limit: op.limit }
|
|
2170
2297
|
}) ?? [];
|
|
2171
2298
|
case "replace": return (active ?? manager.add()).replace(op.query, op.replacement, {
|
|
2172
|
-
regex: op.regex,
|
|
2173
|
-
exact: op.exact,
|
|
2174
|
-
limit: op.limit
|
|
2299
|
+
...op.regex === void 0 ? {} : { regex: op.regex },
|
|
2300
|
+
...op.exact === void 0 ? {} : { exact: op.exact },
|
|
2301
|
+
...op.limit === void 0 ? {} : { limit: op.limit }
|
|
2175
2302
|
});
|
|
2176
2303
|
case "write": {
|
|
2177
2304
|
const workspace = active ?? manager.add();
|
|
@@ -2271,8 +2398,8 @@ function createAgentTool(registry, options) {
|
|
|
2271
2398
|
name: options?.name ?? "agent",
|
|
2272
2399
|
description: options?.description ?? AGENT_TOOL_DESCRIPTION,
|
|
2273
2400
|
summary: AGENT_TOOL_SUMMARY,
|
|
2274
|
-
parameters,
|
|
2275
|
-
|
|
2401
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2402
|
+
async execute(args) {
|
|
2276
2403
|
const call = contract.parse(args);
|
|
2277
2404
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed agent-delegation call", { args });
|
|
2278
2405
|
const provider = call.provider ?? options?.provider;
|
|
@@ -2343,12 +2470,13 @@ function createAgentTool(registry, options) {
|
|
|
2343
2470
|
*/
|
|
2344
2471
|
function createDescribeTool(tools) {
|
|
2345
2472
|
const contract = (0, _orkestrel_contract.createContract)(describeToolShape);
|
|
2473
|
+
const parameters = (0, _orkestrel_contract.schemaToParameters)(contract.schema);
|
|
2346
2474
|
return (0, _orkestrel_agent.createTool)({
|
|
2347
2475
|
name: DESCRIBE_TOOL_NAME,
|
|
2348
2476
|
description: DESCRIBE_TOOL_DESCRIPTION,
|
|
2349
2477
|
summary: DESCRIBE_TOOL_SUMMARY,
|
|
2350
|
-
parameters
|
|
2351
|
-
|
|
2478
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2479
|
+
async execute(args) {
|
|
2352
2480
|
const call = contract.parse(args);
|
|
2353
2481
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed describe call", { args });
|
|
2354
2482
|
const tool = tools.tool(call.name);
|
|
@@ -2398,8 +2526,8 @@ function createPromptTool(options) {
|
|
|
2398
2526
|
name: options.name ?? "ask",
|
|
2399
2527
|
description: options.description ?? PROMPT_TOOL_DESCRIPTION,
|
|
2400
2528
|
summary: PROMPT_TOOL_SUMMARY,
|
|
2401
|
-
parameters,
|
|
2402
|
-
|
|
2529
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2530
|
+
async execute(args) {
|
|
2403
2531
|
const call = contract.parse(args);
|
|
2404
2532
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed ask call", { args });
|
|
2405
2533
|
if ((call.form === "select" || call.form === "checkbox") && (call.choices ?? []).length === 0) throw new AgentToolError("TOOL", "select/checkbox requires at least one choice", {
|
|
@@ -2500,8 +2628,8 @@ function createAnswerTool(options) {
|
|
|
2500
2628
|
name: options.name ?? "answer",
|
|
2501
2629
|
description: options.description ?? ANSWER_TOOL_DESCRIPTION,
|
|
2502
2630
|
summary: ANSWER_TOOL_SUMMARY,
|
|
2503
|
-
parameters,
|
|
2504
|
-
|
|
2631
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2632
|
+
async execute(args) {
|
|
2505
2633
|
const call = contract.parse(args);
|
|
2506
2634
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed answer call", { args });
|
|
2507
2635
|
if (call.operation === "pending") return options.manager.pending(options.to).map((prompt) => ({
|
|
@@ -2633,40 +2761,17 @@ function createDatabaseTool(options = {}) {
|
|
|
2633
2761
|
const parameters = (0, _orkestrel_contract.schemaToParameters)(contract.schema);
|
|
2634
2762
|
const handles = new Map(Object.entries(options.databases ?? {}));
|
|
2635
2763
|
const definitions = /* @__PURE__ */ new Map();
|
|
2636
|
-
const drivers = options.drivers ?? { memory:
|
|
2764
|
+
const drivers = options.drivers ?? { memory: _orkestrel_database.createMemoryDriver };
|
|
2637
2765
|
const key = options.key ?? _orkestrel_database.generateUUID;
|
|
2638
2766
|
const cap = options.limit ?? 1e3;
|
|
2639
2767
|
const store = options.store;
|
|
2640
|
-
|
|
2641
|
-
const cached = handles.get(id);
|
|
2642
|
-
if (cached !== void 0) return cached;
|
|
2643
|
-
if (store !== void 0) {
|
|
2644
|
-
const definition = await store.get(id);
|
|
2645
|
-
if (definition !== void 0) {
|
|
2646
|
-
const factory = drivers[definition.driver];
|
|
2647
|
-
if (factory === void 0) throw new AgentToolError("TOOL", `unknown driver '${definition.driver}'`, {
|
|
2648
|
-
id,
|
|
2649
|
-
driver: definition.driver
|
|
2650
|
-
});
|
|
2651
|
-
const handle = (0, _orkestrel_database.createDatabase)({
|
|
2652
|
-
driver: factory(),
|
|
2653
|
-
tables: expandTables(definition.tables),
|
|
2654
|
-
...definition.keys === void 0 ? {} : { keys: definition.keys },
|
|
2655
|
-
key
|
|
2656
|
-
});
|
|
2657
|
-
handles.set(id, handle);
|
|
2658
|
-
definitions.set(id, definition);
|
|
2659
|
-
return handle;
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
throw new AgentToolError("TOOL", `unknown database '${id}'`, { id });
|
|
2663
|
-
}
|
|
2768
|
+
const resolver = store === void 0 ? new DatabaseResolver(handles, drivers, key) : new DatabaseResolver(handles, drivers, key, store);
|
|
2664
2769
|
return (0, _orkestrel_agent.createTool)({
|
|
2665
2770
|
name: options.name ?? "database",
|
|
2666
2771
|
description: options.description ?? DATABASE_TOOL_DESCRIPTION,
|
|
2667
2772
|
summary: DATABASE_TOOL_SUMMARY,
|
|
2668
|
-
parameters,
|
|
2669
|
-
|
|
2773
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2774
|
+
async execute(args) {
|
|
2670
2775
|
const call = contract.parse(args);
|
|
2671
2776
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed database call", { args });
|
|
2672
2777
|
if (options.readonly === true && DATABASE_TOOL_MUTATIONS.has(call.operation)) throw new AgentToolError("TOOL", `operation '${call.operation}' is disabled in readonly mode`, { operation: call.operation });
|
|
@@ -2674,7 +2779,7 @@ function createDatabaseTool(options = {}) {
|
|
|
2674
2779
|
try {
|
|
2675
2780
|
switch (call.operation) {
|
|
2676
2781
|
case "create": {
|
|
2677
|
-
if (
|
|
2782
|
+
if (resolver.has(call.id) || store !== void 0 && await store.get(call.id) !== void 0) throw new AgentToolError("TOOL", `database '${call.id}' already exists`, { id: call.id });
|
|
2678
2783
|
const name = call.driver ?? "memory";
|
|
2679
2784
|
const factory = drivers[name];
|
|
2680
2785
|
if (factory === void 0) throw new AgentToolError("TOOL", `unknown driver '${name}'`, {
|
|
@@ -2689,7 +2794,7 @@ function createDatabaseTool(options = {}) {
|
|
|
2689
2794
|
...keys === void 0 ? {} : { keys },
|
|
2690
2795
|
key
|
|
2691
2796
|
});
|
|
2692
|
-
|
|
2797
|
+
resolver.set(call.id, handle);
|
|
2693
2798
|
const definition = {
|
|
2694
2799
|
id: call.id,
|
|
2695
2800
|
driver: name,
|
|
@@ -2704,7 +2809,7 @@ function createDatabaseTool(options = {}) {
|
|
|
2704
2809
|
};
|
|
2705
2810
|
}
|
|
2706
2811
|
case "tables": {
|
|
2707
|
-
const handle = await resolve(call.id);
|
|
2812
|
+
const handle = await resolver.resolve(call.id);
|
|
2708
2813
|
return { tables: Object.keys(handle.export()).map((name) => {
|
|
2709
2814
|
const table = handle.table(name);
|
|
2710
2815
|
return {
|
|
@@ -2715,14 +2820,14 @@ function createDatabaseTool(options = {}) {
|
|
|
2715
2820
|
}) };
|
|
2716
2821
|
}
|
|
2717
2822
|
case "get": {
|
|
2718
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2823
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2719
2824
|
const many = Array.isArray(call.key);
|
|
2720
2825
|
const keys = Array.isArray(call.key) ? call.key : [call.key];
|
|
2721
2826
|
const rows = await table.get(keys);
|
|
2722
2827
|
return many ? { rows } : { row: rows[0] };
|
|
2723
2828
|
}
|
|
2724
2829
|
case "records": {
|
|
2725
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2830
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2726
2831
|
const { criteria: probe, limit } = clampCriteria(criteriaOf(call.criteria), cap);
|
|
2727
2832
|
const rows = await table.records(probe, read);
|
|
2728
2833
|
const truncated = rows.length > limit;
|
|
@@ -2734,24 +2839,24 @@ function createDatabaseTool(options = {}) {
|
|
|
2734
2839
|
limit
|
|
2735
2840
|
};
|
|
2736
2841
|
}
|
|
2737
|
-
case "count": return { count: await (await resolve(call.id)).table(call.table).count(criteriaOf(call.criteria), read) };
|
|
2738
|
-
case "aggregate": return { value: await (await resolve(call.id)).table(call.table).aggregate(call.function, call.column, criteriaOf(call.criteria), read) };
|
|
2842
|
+
case "count": return { count: await (await resolver.resolve(call.id)).table(call.table).count(criteriaOf(call.criteria), read) };
|
|
2843
|
+
case "aggregate": return { value: await (await resolver.resolve(call.id)).table(call.table).aggregate(call.function, call.column, criteriaOf(call.criteria), read) };
|
|
2739
2844
|
case "add": {
|
|
2740
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2845
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2741
2846
|
const many = Array.isArray(call.row);
|
|
2742
2847
|
const rows = Array.isArray(call.row) ? call.row : [call.row];
|
|
2743
2848
|
const keys = await table.add(rows, read);
|
|
2744
2849
|
return many ? { keys } : { key: keys[0] };
|
|
2745
2850
|
}
|
|
2746
2851
|
case "set": {
|
|
2747
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2852
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2748
2853
|
const many = Array.isArray(call.row);
|
|
2749
2854
|
const rows = Array.isArray(call.row) ? call.row : [call.row];
|
|
2750
2855
|
const keys = await table.set(rows, read);
|
|
2751
2856
|
return many ? { keys } : { key: keys[0] };
|
|
2752
2857
|
}
|
|
2753
2858
|
case "update": {
|
|
2754
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2859
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2755
2860
|
const changes = call.changes;
|
|
2756
2861
|
const many = Array.isArray(call.key);
|
|
2757
2862
|
const keys = Array.isArray(call.key) ? call.key : [call.key];
|
|
@@ -2759,14 +2864,14 @@ function createDatabaseTool(options = {}) {
|
|
|
2759
2864
|
return many ? { updated } : { updated: updated[0] };
|
|
2760
2865
|
}
|
|
2761
2866
|
case "remove": {
|
|
2762
|
-
const table = (await resolve(call.id)).table(call.table);
|
|
2867
|
+
const table = (await resolver.resolve(call.id)).table(call.table);
|
|
2763
2868
|
const many = Array.isArray(call.key);
|
|
2764
2869
|
const keys = Array.isArray(call.key) ? call.key : [call.key];
|
|
2765
2870
|
const removed = await table.remove(keys, read);
|
|
2766
2871
|
return many ? { removed } : { removed: removed[0] };
|
|
2767
2872
|
}
|
|
2768
2873
|
case "migrate": {
|
|
2769
|
-
const handle = await resolve(call.id);
|
|
2874
|
+
const handle = await resolver.resolve(call.id);
|
|
2770
2875
|
const previous = handle.export();
|
|
2771
2876
|
const deployed = Object.entries(previous).map(([name, table]) => tableSchema(name, table));
|
|
2772
2877
|
const tables = call.tables;
|
|
@@ -2778,8 +2883,8 @@ function createDatabaseTool(options = {}) {
|
|
|
2778
2883
|
const declared = expandTables(tables);
|
|
2779
2884
|
const migrated = handle.import(declared, Object.keys(keys).length > 0 ? keys : void 0);
|
|
2780
2885
|
const migration = await migrated.migrate(deployed, read);
|
|
2781
|
-
|
|
2782
|
-
const tracked = definitions.get(call.id);
|
|
2886
|
+
resolver.set(call.id, migrated);
|
|
2887
|
+
const tracked = definitions.get(call.id) ?? (store === void 0 ? void 0 : await store.get(call.id));
|
|
2783
2888
|
if (tracked !== void 0) {
|
|
2784
2889
|
const updated = {
|
|
2785
2890
|
id: call.id,
|
|
@@ -2793,11 +2898,11 @@ function createDatabaseTool(options = {}) {
|
|
|
2793
2898
|
return { migration };
|
|
2794
2899
|
}
|
|
2795
2900
|
case "destroy": {
|
|
2796
|
-
const cached =
|
|
2901
|
+
const cached = resolver.get(call.id);
|
|
2797
2902
|
const persisted = store !== void 0 && cached === void 0 ? await store.get(call.id) !== void 0 : false;
|
|
2798
2903
|
if (cached !== void 0) {
|
|
2799
2904
|
await cached.close();
|
|
2800
|
-
|
|
2905
|
+
resolver.delete(call.id);
|
|
2801
2906
|
}
|
|
2802
2907
|
definitions.delete(call.id);
|
|
2803
2908
|
if (store !== void 0) await store.delete(call.id);
|
|
@@ -2877,8 +2982,8 @@ function createRelationTool(options) {
|
|
|
2877
2982
|
name: options.name ?? "relation",
|
|
2878
2983
|
description: options.description ?? RELATION_TOOL_DESCRIPTION,
|
|
2879
2984
|
summary: RELATION_TOOL_SUMMARY,
|
|
2880
|
-
parameters,
|
|
2881
|
-
|
|
2985
|
+
...parameters === void 0 ? {} : { parameters },
|
|
2986
|
+
async execute(args) {
|
|
2882
2987
|
const call = contract.parse(args);
|
|
2883
2988
|
if (call === void 0) throw new AgentToolError("TOOL", "malformed relation call", { args });
|
|
2884
2989
|
try {
|
|
@@ -3028,8 +3133,8 @@ function createInferTool(options) {
|
|
|
3028
3133
|
name: options?.name ?? "infer",
|
|
3029
3134
|
description: options?.description ?? INFER_TOOL_DESCRIPTION,
|
|
3030
3135
|
summary: INFER_TOOL_SUMMARY,
|
|
3031
|
-
parameters,
|
|
3032
|
-
|
|
3136
|
+
...parameters === void 0 ? {} : { parameters },
|
|
3137
|
+
async execute(args) {
|
|
3033
3138
|
const parsed = contract.parse(args);
|
|
3034
3139
|
if (parsed === void 0) throw new AgentToolError("TOOL", "malformed infer arguments", { args });
|
|
3035
3140
|
const schema = (0, _orkestrel_contract.samplesToSchema)(parsed.samples, {
|
|
@@ -3136,15 +3241,17 @@ function createEndpointTool(definition, options) {
|
|
|
3136
3241
|
if (!(options?.validate ?? true)) return (0, _orkestrel_agent.createTool)({
|
|
3137
3242
|
name: definition.name,
|
|
3138
3243
|
description: definition.description,
|
|
3139
|
-
parameters,
|
|
3140
|
-
execute
|
|
3244
|
+
...parameters === void 0 ? {} : { parameters },
|
|
3245
|
+
execute(args) {
|
|
3246
|
+
return definition.invoke(args);
|
|
3247
|
+
}
|
|
3141
3248
|
});
|
|
3142
3249
|
const contract = (0, _orkestrel_contract.createContract)((0, _orkestrel_contract.schemaToShape)(objectSchema));
|
|
3143
3250
|
return (0, _orkestrel_agent.createTool)({
|
|
3144
3251
|
name: definition.name,
|
|
3145
3252
|
description: definition.description,
|
|
3146
|
-
parameters,
|
|
3147
|
-
execute
|
|
3253
|
+
...parameters === void 0 ? {} : { parameters },
|
|
3254
|
+
execute(args) {
|
|
3148
3255
|
const parsed = contract.parse(args);
|
|
3149
3256
|
if (parsed === void 0 || !(0, _orkestrel_contract.isRecord)(parsed)) throw new AgentToolError("TOOL", "malformed endpoint call arguments", {
|
|
3150
3257
|
name: definition.name,
|
|
@@ -3172,6 +3279,7 @@ exports.DESCRIBE_TOOL_DESCRIPTION = DESCRIBE_TOOL_DESCRIPTION;
|
|
|
3172
3279
|
exports.DESCRIBE_TOOL_NAME = DESCRIBE_TOOL_NAME;
|
|
3173
3280
|
exports.DESCRIBE_TOOL_SUMMARY = DESCRIBE_TOOL_SUMMARY;
|
|
3174
3281
|
exports.DatabaseDefinitionStore = DatabaseDefinitionStore;
|
|
3282
|
+
exports.DatabaseResolver = DatabaseResolver;
|
|
3175
3283
|
exports.INFER_TOOL_DESCRIPTION = INFER_TOOL_DESCRIPTION;
|
|
3176
3284
|
exports.INFER_TOOL_NAME = INFER_TOOL_NAME;
|
|
3177
3285
|
exports.INFER_TOOL_SUMMARY = INFER_TOOL_SUMMARY;
|