@howaboua/pi-durable-code 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/activation.js +410 -100
- package/dist/code/src/engine.d.ts +1 -1
- package/dist/code/src/history.d.ts +4 -4
- package/dist/code/src/index.d.ts +1 -0
- package/dist/code/src/runtime-contract.d.ts +3 -0
- package/dist/code/src/runtime-tools.d.ts +1 -1
- package/dist/execution/src/cell-contract.d.ts +2 -1
- package/dist/execution/src/cell-observation.d.ts +1 -1
- package/dist/execution/src/cell-progress.d.ts +1 -1
- package/dist/execution/src/cell-tools.d.ts +2 -1
- package/dist/execution/src/cells.d.ts +1 -1
- package/dist/execution/src/custom-commands.d.ts +3 -2
- package/dist/execution/src/history.d.ts +1 -1
- package/dist/execution/src/invocation-state.d.ts +1 -1
- package/dist/execution/src/invocation.d.ts +79 -2
- package/dist/execution/src/native-tools.d.ts +18 -7
- package/dist/execution/src/nested-call.d.ts +11 -3
- package/dist/execution/src/nested-park.d.ts +9 -0
- package/dist/execution/src/nested-result.d.ts +1 -1
- package/dist/execution/src/tool-contract.d.ts +8 -1
- package/dist/execution/src/tool-namespaces.d.ts +31 -0
- package/dist/execution/src/tool-origin.d.ts +9 -1
- package/dist/execution/src/wait-session.d.ts +1 -1
- package/dist/history.js +18 -11
- package/dist/index.js +410 -100
- package/package.json +10 -10
package/dist/activation.js
CHANGED
|
@@ -77,8 +77,8 @@ function parseInteger(value, name, minimum = 0, maximum = Number.MAX_SAFE_INTEGE
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
// packages/execution/src/cells.ts
|
|
80
|
-
import {
|
|
81
|
-
import {
|
|
80
|
+
import { withCancel as withCancel2 } from "@earendil-works/chord/context";
|
|
81
|
+
import { copyJson as copyJson12 } from "@earendil-works/chord/json";
|
|
82
82
|
import {
|
|
83
83
|
defineExtension
|
|
84
84
|
} from "@earendil-works/pi-durable";
|
|
@@ -89,7 +89,7 @@ import {
|
|
|
89
89
|
} from "@earendil-works/pi-durable";
|
|
90
90
|
|
|
91
91
|
// packages/execution/src/forwarding.ts
|
|
92
|
-
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
92
|
+
import { copyJson as copyJson2 } from "@earendil-works/chord/json";
|
|
93
93
|
import {
|
|
94
94
|
LiveDoc,
|
|
95
95
|
NestedCallDoc,
|
|
@@ -99,7 +99,7 @@ import {
|
|
|
99
99
|
// packages/execution/src/invocation-state.ts
|
|
100
100
|
import {
|
|
101
101
|
copyJson
|
|
102
|
-
} from "@earendil-works/chord";
|
|
102
|
+
} from "@earendil-works/chord/json";
|
|
103
103
|
import {
|
|
104
104
|
defineDoc
|
|
105
105
|
} from "@earendil-works/pi-durable";
|
|
@@ -576,7 +576,7 @@ async function observeCell(kind, id, api, context, budget) {
|
|
|
576
576
|
}
|
|
577
577
|
|
|
578
578
|
// packages/execution/src/cell-progress.ts
|
|
579
|
-
import { copyJson as copyJson3 } from "@earendil-works/chord";
|
|
579
|
+
import { copyJson as copyJson3 } from "@earendil-works/chord/json";
|
|
580
580
|
import {
|
|
581
581
|
LiveDoc as LiveDoc2
|
|
582
582
|
} from "@earendil-works/pi-durable";
|
|
@@ -636,37 +636,64 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
636
636
|
}
|
|
637
637
|
|
|
638
638
|
// packages/execution/src/cell-task.ts
|
|
639
|
-
import { copyJson as copyJson8 } from "@earendil-works/chord";
|
|
640
639
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
640
|
+
import { copyJson as copyJson8 } from "@earendil-works/chord/json";
|
|
641
641
|
import {
|
|
642
642
|
defineTask
|
|
643
643
|
} from "@earendil-works/pi-durable";
|
|
644
644
|
|
|
645
645
|
// packages/execution/src/cell-tools.ts
|
|
646
|
-
import { copyJson as copyJson6 } from "@earendil-works/chord";
|
|
647
646
|
import { withoutAbortSignal as withoutAbortSignal2 } from "@earendil-works/chord/context";
|
|
647
|
+
import { copyJson as copyJson6 } from "@earendil-works/chord/json";
|
|
648
648
|
|
|
649
649
|
// packages/execution/src/invocation.ts
|
|
650
|
-
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
651
650
|
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
651
|
+
import { copyJson as copyJson5 } from "@earendil-works/chord/json";
|
|
652
|
+
import {
|
|
653
|
+
NestedCallDoc as NestedCallDoc3
|
|
654
|
+
} from "@earendil-works/pi-durable";
|
|
655
|
+
import { equal } from "@earendil-works/pi-schema";
|
|
652
656
|
|
|
653
657
|
// packages/execution/src/native-tools.ts
|
|
654
|
-
import {
|
|
655
|
-
|
|
658
|
+
import {
|
|
659
|
+
withoutAbortSignal
|
|
660
|
+
} from "@earendil-works/chord/context";
|
|
661
|
+
import { copyJson as copyJson4 } from "@earendil-works/chord/json";
|
|
656
662
|
import {
|
|
657
663
|
LiveDoc as LiveDoc3,
|
|
658
664
|
NestedCallDoc as NestedCallDoc2,
|
|
659
|
-
ToolTask as
|
|
665
|
+
ToolTask as ToolTask3
|
|
660
666
|
} from "@earendil-works/pi-durable";
|
|
661
667
|
import { Equal } from "typebox/value";
|
|
662
|
-
|
|
668
|
+
|
|
669
|
+
// packages/execution/src/tool-origin.ts
|
|
670
|
+
import {
|
|
671
|
+
defineDoc as defineDoc2,
|
|
672
|
+
ToolTask as ToolTask2
|
|
673
|
+
} from "@earendil-works/pi-durable";
|
|
674
|
+
var ToolOriginDoc = defineDoc2({
|
|
675
|
+
kind: "howaboua.tool-origin",
|
|
676
|
+
version: 1,
|
|
677
|
+
scope: "task",
|
|
678
|
+
checkpointWhen: () => false,
|
|
679
|
+
initial: () => ({})
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
// packages/execution/src/native-tools.ts
|
|
683
|
+
function assertNestedKey(key) {
|
|
684
|
+
if (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key))
|
|
685
|
+
throw new Error("Invalid nested call or task key");
|
|
686
|
+
}
|
|
687
|
+
function nativeToolCalls(runtime, callId, replay, resumed, assertLive, context) {
|
|
663
688
|
let sequence = 0;
|
|
664
689
|
const admissions = [];
|
|
665
|
-
|
|
690
|
+
async function execute(name, args, callContext, options = {}) {
|
|
666
691
|
assertLive();
|
|
692
|
+
if (options.key !== undefined)
|
|
693
|
+
assertNestedKey(options.key);
|
|
694
|
+
else if (resumed)
|
|
695
|
+
throw new Error("Nested calls require an explicit key after waking");
|
|
667
696
|
const key = options.key ?? String(++sequence);
|
|
668
|
-
if (options.key !== undefined && (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key)))
|
|
669
|
-
throw new Error("Invalid nested call key");
|
|
670
697
|
const call = {
|
|
671
698
|
type: "toolCall",
|
|
672
699
|
id: `${callId}/${key}`,
|
|
@@ -678,11 +705,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
678
705
|
const admission = runtime.commit(async (tx) => {
|
|
679
706
|
const slot = await tx.doc(NestedCallDoc2, runtime.taskId, key, null);
|
|
680
707
|
if (slot.taskId !== undefined) {
|
|
681
|
-
const input = (await tx.task(slot.taskId))?.input;
|
|
708
|
+
const input = slot.kind === "task" ? undefined : (await tx.task(slot.taskId))?.input;
|
|
682
709
|
if (input?.kind !== "nested" || input.call.name !== name || !Equal(input.call.arguments, call.arguments))
|
|
683
710
|
throw new Error(`Nested call ${call.id} was already made with another tool or other arguments`);
|
|
684
711
|
} else {
|
|
685
|
-
slot.
|
|
712
|
+
slot.kind = "nested";
|
|
713
|
+
slot.taskId = await tx.createTask(ToolTask3, {
|
|
686
714
|
kind: "nested",
|
|
687
715
|
parent: runtime.taskId,
|
|
688
716
|
parentCallId: callId,
|
|
@@ -694,6 +722,9 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
694
722
|
abandonOnRestart: replay !== "safe"
|
|
695
723
|
});
|
|
696
724
|
const live = await tx.doc(LiveDoc3, runtime.conversationId);
|
|
725
|
+
const origin = (await tx.doc(ToolOriginDoc, runtime.taskId)).origin;
|
|
726
|
+
if (origin)
|
|
727
|
+
(await tx.doc(ToolOriginDoc, slot.taskId)).origin = { ...origin };
|
|
697
728
|
live.nestedTools ??= [];
|
|
698
729
|
live.nestedTools.push({
|
|
699
730
|
callId: call.id,
|
|
@@ -725,7 +756,13 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
725
756
|
const slot = await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext);
|
|
726
757
|
if (slot?.taskId === undefined)
|
|
727
758
|
throw new Error("Nested task creation did not commit");
|
|
728
|
-
const
|
|
759
|
+
const taskId = slot.taskId;
|
|
760
|
+
const result = (resultContext) => readResult(name, key, taskId, resultContext);
|
|
761
|
+
return options.wait === false ? { taskId, result } : result(callContext);
|
|
762
|
+
}
|
|
763
|
+
async function readResult(name, key, taskId, callContext) {
|
|
764
|
+
assertLive();
|
|
765
|
+
const settled = await runtime.waitForTask(taskId, callContext);
|
|
729
766
|
const result = (await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext))?.result;
|
|
730
767
|
await runtime.commit(async (tx) => {
|
|
731
768
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
@@ -733,7 +770,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
733
770
|
return;
|
|
734
771
|
}, callContext);
|
|
735
772
|
return result === undefined ? {
|
|
736
|
-
taskId
|
|
773
|
+
taskId,
|
|
737
774
|
isError: true,
|
|
738
775
|
diagnostics: [
|
|
739
776
|
{
|
|
@@ -743,9 +780,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
743
780
|
}
|
|
744
781
|
]
|
|
745
782
|
} : copyJson4(result);
|
|
746
|
-
}
|
|
783
|
+
}
|
|
747
784
|
return {
|
|
748
785
|
execute,
|
|
786
|
+
async drain() {
|
|
787
|
+
await Promise.allSettled(admissions);
|
|
788
|
+
},
|
|
749
789
|
async finish() {
|
|
750
790
|
await Promise.allSettled(admissions);
|
|
751
791
|
await cleanupNativeCalls(runtime, withoutAbortSignal(context));
|
|
@@ -753,7 +793,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
753
793
|
};
|
|
754
794
|
}
|
|
755
795
|
async function cleanupNativeCalls(runtime, context) {
|
|
756
|
-
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind ===
|
|
796
|
+
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind === ToolTask3.definition.name).map((child) => runtime.abortOwned(child.id, context)));
|
|
757
797
|
await runtime.commit(async (tx) => {
|
|
758
798
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
759
799
|
await captureNativeCalls(tx, runtime.conversationId, runtime.taskId, owner.forwardingCellId);
|
|
@@ -847,18 +887,6 @@ class Progress {
|
|
|
847
887
|
}
|
|
848
888
|
}
|
|
849
889
|
|
|
850
|
-
// packages/execution/src/tool-origin.ts
|
|
851
|
-
import {
|
|
852
|
-
defineDoc as defineDoc2
|
|
853
|
-
} from "@earendil-works/pi-durable";
|
|
854
|
-
var ToolOriginDoc = defineDoc2({
|
|
855
|
-
kind: "howaboua.tool-origin",
|
|
856
|
-
version: 1,
|
|
857
|
-
scope: "task",
|
|
858
|
-
checkpointWhen: () => false,
|
|
859
|
-
initial: () => ({})
|
|
860
|
-
});
|
|
861
|
-
|
|
862
890
|
// packages/execution/src/invocation.ts
|
|
863
891
|
function executionError(code, message) {
|
|
864
892
|
return {
|
|
@@ -870,21 +898,23 @@ function executionError(code, message) {
|
|
|
870
898
|
function errorText(error) {
|
|
871
899
|
return error instanceof Error ? error.message : String(error);
|
|
872
900
|
}
|
|
873
|
-
function createInvocation(runtime, tool, context) {
|
|
901
|
+
function createInvocation(runtime, tool, context, resume) {
|
|
874
902
|
const limits = {
|
|
875
903
|
maxBytes: tool.outputLimits?.maxBytes ?? 50 * 1024,
|
|
876
904
|
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
|
877
905
|
retain: tool.outputLimits?.retain ?? "head"
|
|
878
906
|
};
|
|
879
907
|
const output = new OutputBuffer(limits);
|
|
880
|
-
const diagnostics = [
|
|
881
|
-
|
|
908
|
+
const diagnostics = [
|
|
909
|
+
...resume?.progress?.diagnostics ?? []
|
|
910
|
+
];
|
|
911
|
+
let details = resume?.progress?.details;
|
|
882
912
|
let ended = false;
|
|
883
913
|
const assertLive = () => {
|
|
884
914
|
if (ended)
|
|
885
915
|
throw new Error(`Invocation ${runtime.taskId} has settled`);
|
|
886
916
|
};
|
|
887
|
-
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, assertLive, context);
|
|
917
|
+
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, resume?.wake !== undefined, assertLive, context);
|
|
888
918
|
const snapshot = () => {
|
|
889
919
|
const retained = output.snapshot();
|
|
890
920
|
return {
|
|
@@ -913,6 +943,8 @@ function createInvocation(runtime, tool, context) {
|
|
|
913
943
|
taskId: runtime.taskId,
|
|
914
944
|
conversationId: runtime.conversationId,
|
|
915
945
|
callId: `nested-${runtime.taskId}`,
|
|
946
|
+
wake: resume?.wake,
|
|
947
|
+
checkpoint: resume?.checkpoint,
|
|
916
948
|
registry: runtime.registry,
|
|
917
949
|
agent: runtime.agent,
|
|
918
950
|
env: undefined,
|
|
@@ -933,7 +965,7 @@ function createInvocation(runtime, tool, context) {
|
|
|
933
965
|
const retained = output.snapshot();
|
|
934
966
|
return { text: retained.text, truncated: retained.droppedBytes > 0 };
|
|
935
967
|
},
|
|
936
|
-
executeTool:
|
|
968
|
+
executeTool: native.execute,
|
|
937
969
|
diagnostic: (value) => {
|
|
938
970
|
assertLive();
|
|
939
971
|
diagnostics.push(copyJson5(value, { omitUndefinedProperties: true }));
|
|
@@ -947,24 +979,44 @@ function createInvocation(runtime, tool, context) {
|
|
|
947
979
|
committed.catch(() => {});
|
|
948
980
|
await awaitWithContext(committed, detailContext);
|
|
949
981
|
},
|
|
950
|
-
commit: async (change, commitContext) => {
|
|
982
|
+
commit: async (change, commitContext, options) => {
|
|
951
983
|
assertLive();
|
|
952
984
|
let result;
|
|
953
985
|
await runtime.commit(async (tx) => {
|
|
954
986
|
result = await change(tx);
|
|
955
987
|
return;
|
|
956
|
-
}, commitContext);
|
|
988
|
+
}, commitContext, options);
|
|
957
989
|
return result;
|
|
958
990
|
},
|
|
959
991
|
memo: runtime.memo,
|
|
960
|
-
|
|
992
|
+
readSignal: runtime.readSignal,
|
|
993
|
+
async createTask(task, input, {
|
|
994
|
+
key,
|
|
995
|
+
...options
|
|
996
|
+
}, taskContext) {
|
|
961
997
|
assertLive();
|
|
998
|
+
if (key !== undefined)
|
|
999
|
+
assertNestedKey(key);
|
|
1000
|
+
else if (resume?.wake !== undefined)
|
|
1001
|
+
throw new Error("Child tasks require an explicit key after waking");
|
|
962
1002
|
let id;
|
|
963
1003
|
await runtime.commit(async (tx) => {
|
|
1004
|
+
const member = key === undefined ? undefined : await tx.doc(NestedCallDoc3, runtime.taskId, key, null);
|
|
1005
|
+
if (member?.taskId !== undefined) {
|
|
1006
|
+
const existing = member.kind === "task" ? await tx.task(member.taskId) : undefined;
|
|
1007
|
+
if (existing?.kind !== task.definition.name || !equal(existing.input, copyJson5(input, { omitUndefinedProperties: true })))
|
|
1008
|
+
throw new Error(`Key ${key} was already used for another call or task`);
|
|
1009
|
+
id = member.taskId;
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
964
1012
|
id = await tx.createTask(task, input, {
|
|
965
1013
|
...options,
|
|
966
1014
|
...options.abandonOnRestart === undefined && tool.replay !== "safe" && options.ownership.kind === "task" && options.ownership.taskId === runtime.taskId ? { abandonOnRestart: true } : {}
|
|
967
1015
|
});
|
|
1016
|
+
if (member !== undefined) {
|
|
1017
|
+
member.kind = "task";
|
|
1018
|
+
member.taskId = id;
|
|
1019
|
+
}
|
|
968
1020
|
const source = await tx.doc(ToolOriginDoc, runtime.taskId);
|
|
969
1021
|
if (source.origin !== undefined)
|
|
970
1022
|
(await tx.doc(ToolOriginDoc, id)).origin = { ...source.origin };
|
|
@@ -1011,6 +1063,14 @@ function createInvocation(runtime, tool, context) {
|
|
|
1011
1063
|
for (const waiter of await progress.stop())
|
|
1012
1064
|
waiter.reject(error);
|
|
1013
1065
|
},
|
|
1066
|
+
async park() {
|
|
1067
|
+
ended = true;
|
|
1068
|
+
await native.drain();
|
|
1069
|
+
output.end();
|
|
1070
|
+
const pending = await progress.stop();
|
|
1071
|
+
const { output: _output, ...kept } = snapshot();
|
|
1072
|
+
return { progress: storeResult(kept), pending };
|
|
1073
|
+
},
|
|
1014
1074
|
limits,
|
|
1015
1075
|
output,
|
|
1016
1076
|
progress
|
|
@@ -1112,7 +1172,73 @@ function combineControls(controls, allowTermination = true) {
|
|
|
1112
1172
|
}
|
|
1113
1173
|
|
|
1114
1174
|
// packages/execution/src/tool-contract.ts
|
|
1115
|
-
import { copyJson as copyJson7 } from "@earendil-works/chord";
|
|
1175
|
+
import { copyJson as copyJson7 } from "@earendil-works/chord/json";
|
|
1176
|
+
|
|
1177
|
+
// packages/execution/src/tool-namespaces.ts
|
|
1178
|
+
import { isJsonValue } from "@earendil-works/chord/json";
|
|
1179
|
+
function validateJsToolNamespace(tool, namespace) {
|
|
1180
|
+
const key = (value) => typeof value === "string" && /^[a-zA-Z_$][\w$]*$/.test(value);
|
|
1181
|
+
if (!namespace || !key(namespace.name) || typeof namespace.prompt !== "string" || !namespace.prompt.trim() || !Array.isArray(namespace.methods) || !namespace.methods.length || namespace.methods.some((method) => !method || !key(method.name) || method.bind !== undefined && !key(method.bind) || method.defaults !== undefined && (!method.defaults || typeof method.defaults !== "object" || Array.isArray(method.defaults) || !isJsonValue(method.defaults))))
|
|
1182
|
+
throw new Error(`Tool ${tool} has invalid jsNamespace metadata`);
|
|
1183
|
+
}
|
|
1184
|
+
function jsToolPrompt(contract) {
|
|
1185
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1186
|
+
return [];
|
|
1187
|
+
if (contract.jsNamespace)
|
|
1188
|
+
return [contract.jsNamespace.prompt];
|
|
1189
|
+
return contract.deferLoading ? [] : [contract.prompt];
|
|
1190
|
+
}
|
|
1191
|
+
function namespaceDefinitions(tools) {
|
|
1192
|
+
return tools.flatMap(({ name, jsNamespace }) => jsNamespace ? [{ tool: name, name: jsNamespace.name, methods: jsNamespace.methods }] : []);
|
|
1193
|
+
}
|
|
1194
|
+
var installToolNamespacesSource = `(tools, definitions) => {
|
|
1195
|
+
const namespaces = Object.create(null);
|
|
1196
|
+
const binders = Object.create(null);
|
|
1197
|
+
let active = true;
|
|
1198
|
+
const add = (target, key, value) => {
|
|
1199
|
+
if (Object.hasOwn(target, key)) throw new Error("Duplicate tool namespace member: " + key);
|
|
1200
|
+
target[key] = value;
|
|
1201
|
+
};
|
|
1202
|
+
const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1203
|
+
for (const { tool, name, methods } of definitions) {
|
|
1204
|
+
const namespace = namespaces[name] ??= Object.create(null);
|
|
1205
|
+
const bound = binders[name] ??= Object.create(null);
|
|
1206
|
+
for (const { name: method, bind, defaults } of methods) {
|
|
1207
|
+
const invoke = (args, ref) => {
|
|
1208
|
+
if (!active)
|
|
1209
|
+
throw new Error("Tool namespace binding expired; bind the server reference again in this cell");
|
|
1210
|
+
if (args !== undefined && !object(args))
|
|
1211
|
+
throw new TypeError("Tool namespace arguments must be an object");
|
|
1212
|
+
return tools[tool]({ ...args, ...defaults, ...(bind === undefined ? {} : { [bind]: ref }) });
|
|
1213
|
+
};
|
|
1214
|
+
if (bind === undefined) {
|
|
1215
|
+
add(namespace, method, (args) => invoke(args));
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
if (!Object.hasOwn(bound, bind)) {
|
|
1219
|
+
const members = bound[bind] = Object.create(null);
|
|
1220
|
+
add(namespace, bind, (ref) => {
|
|
1221
|
+
if (!object(ref)) throw new TypeError("Supply a discovered namespace reference");
|
|
1222
|
+
const reference = { ...ref };
|
|
1223
|
+
const result = Object.create(null);
|
|
1224
|
+
for (const [key, call] of Object.entries(members))
|
|
1225
|
+
result[key] = (args) => call(args, reference);
|
|
1226
|
+
return Object.freeze(result);
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
add(bound[bind], method, invoke);
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
for (const namespace of Object.values(namespaces)) Object.freeze(namespace);
|
|
1233
|
+
globalThis.toolNamespaces = Object.freeze(namespaces);
|
|
1234
|
+
return () => {
|
|
1235
|
+
active = false;
|
|
1236
|
+
if (globalThis.toolNamespaces === namespaces)
|
|
1237
|
+
globalThis.toolNamespaces = Object.freeze(Object.create(null));
|
|
1238
|
+
};
|
|
1239
|
+
}`;
|
|
1240
|
+
|
|
1241
|
+
// packages/execution/src/tool-contract.ts
|
|
1116
1242
|
var nestedToolDiscoveryGuidance = "Tools without a shown call signature: help is required only while their current arguments are unknown. Call with no arguments and read the result before composing an operation. Reuse known arguments without repeating help. Structured discovery uses {}";
|
|
1117
1243
|
function readToolContract(tool) {
|
|
1118
1244
|
const candidate = "executionHints" in tool ? tool.executionHints : undefined;
|
|
@@ -1127,6 +1253,7 @@ function readToolContract(tool) {
|
|
|
1127
1253
|
"yieldTimeMs",
|
|
1128
1254
|
"inputSchema",
|
|
1129
1255
|
"nativeOnly",
|
|
1256
|
+
"nestedHidden",
|
|
1130
1257
|
"discovery"
|
|
1131
1258
|
]);
|
|
1132
1259
|
for (const key of Object.keys(hints))
|
|
@@ -1150,6 +1277,12 @@ function readToolContract(tool) {
|
|
|
1150
1277
|
const nativeOnly = "nativeOnly" in hints ? hints.nativeOnly : undefined;
|
|
1151
1278
|
if (nativeOnly !== undefined && typeof nativeOnly !== "boolean")
|
|
1152
1279
|
throw new Error(`Tool ${tool.name} executionHints.nativeOnly must be a boolean`);
|
|
1280
|
+
const nestedHidden = "nestedHidden" in hints ? hints.nestedHidden : undefined;
|
|
1281
|
+
const jsNamespace = "jsNamespace" in tool ? tool.jsNamespace : undefined;
|
|
1282
|
+
if (jsNamespace !== undefined)
|
|
1283
|
+
validateJsToolNamespace(tool.name, jsNamespace);
|
|
1284
|
+
if (nestedHidden !== undefined && typeof nestedHidden !== "boolean")
|
|
1285
|
+
throw new Error(`Tool ${tool.name} executionHints.nestedHidden must be a boolean`);
|
|
1153
1286
|
const yieldTimeMs = "yieldTimeMs" in hints ? hints.yieldTimeMs : undefined;
|
|
1154
1287
|
if (yieldTimeMs !== undefined && (typeof yieldTimeMs !== "number" || !Number.isSafeInteger(yieldTimeMs) || yieldTimeMs < 0))
|
|
1155
1288
|
throw new Error(`Tool ${tool.name} executionHints.yieldTimeMs must be a non-negative safe integer`);
|
|
@@ -1166,6 +1299,8 @@ function readToolContract(tool) {
|
|
|
1166
1299
|
inputSchema,
|
|
1167
1300
|
deferLoading: typeof deferred === "boolean" ? deferred : true,
|
|
1168
1301
|
nativeOnly: nativeOnly === true,
|
|
1302
|
+
nestedHidden: nestedHidden === true,
|
|
1303
|
+
...jsNamespace === undefined ? {} : { jsNamespace },
|
|
1169
1304
|
...typeof yieldTimeMs === "number" ? { yieldTimeMs } : {},
|
|
1170
1305
|
...outputHint === undefined ? {} : { output: outputHint },
|
|
1171
1306
|
help: [
|
|
@@ -1401,7 +1536,7 @@ function createCellTask(options, nestedTask) {
|
|
|
1401
1536
|
import {
|
|
1402
1537
|
hook,
|
|
1403
1538
|
LiveDoc as LiveDoc4,
|
|
1404
|
-
ToolTask as
|
|
1539
|
+
ToolTask as ToolTask4
|
|
1405
1540
|
} from "@earendil-works/pi-durable";
|
|
1406
1541
|
function forwardingHook() {
|
|
1407
1542
|
let host;
|
|
@@ -1411,7 +1546,7 @@ function forwardingHook() {
|
|
|
1411
1546
|
throw new Error("Create a separate execution component for each Harness");
|
|
1412
1547
|
host = harness;
|
|
1413
1548
|
},
|
|
1414
|
-
hook: hook(
|
|
1549
|
+
hook: hook(ToolTask4, {
|
|
1415
1550
|
async beforeTool(call, api, context) {
|
|
1416
1551
|
const parent = call.parent;
|
|
1417
1552
|
if (parent === undefined || host === undefined)
|
|
@@ -1424,7 +1559,7 @@ function forwardingHook() {
|
|
|
1424
1559
|
if (conversation === undefined)
|
|
1425
1560
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1426
1561
|
await conversation.commit(async (tx) => {
|
|
1427
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1562
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1428
1563
|
return;
|
|
1429
1564
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1430
1565
|
const admission = owner.forwardingCellId === undefined;
|
|
@@ -1451,7 +1586,7 @@ function forwardingHook() {
|
|
|
1451
1586
|
if (conversation === undefined)
|
|
1452
1587
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1453
1588
|
await conversation.commit(async (tx) => {
|
|
1454
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1589
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1455
1590
|
return;
|
|
1456
1591
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1457
1592
|
const cellId = owner.forwardingCellId;
|
|
@@ -1476,6 +1611,7 @@ function forwardingHook() {
|
|
|
1476
1611
|
}
|
|
1477
1612
|
|
|
1478
1613
|
// packages/execution/src/nested.ts
|
|
1614
|
+
import { copyJson as copyJson11 } from "@earendil-works/chord/json";
|
|
1479
1615
|
import {
|
|
1480
1616
|
defineTask as defineTask2
|
|
1481
1617
|
} from "@earendil-works/pi-durable";
|
|
@@ -1484,7 +1620,7 @@ import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
|
1484
1620
|
// packages/execution/src/nested-call.ts
|
|
1485
1621
|
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
1486
1622
|
import {
|
|
1487
|
-
ToolTask as
|
|
1623
|
+
ToolTask as ToolTask5
|
|
1488
1624
|
} from "@earendil-works/pi-durable";
|
|
1489
1625
|
import { compile } from "@earendil-works/pi-schema";
|
|
1490
1626
|
function nestedCall(runtime, input) {
|
|
@@ -1530,7 +1666,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1530
1666
|
async function eachHook(agent, name, api, invoke) {
|
|
1531
1667
|
for (const extension of agent.extensions)
|
|
1532
1668
|
for (const registration of extension.hooks ?? []) {
|
|
1533
|
-
if (registration.task !==
|
|
1669
|
+
if (registration.task !== ToolTask5.definition.name)
|
|
1534
1670
|
continue;
|
|
1535
1671
|
const handler = registration.handlers[name];
|
|
1536
1672
|
if (handler === undefined)
|
|
@@ -1545,8 +1681,120 @@ async function eachHook(agent, name, api, invoke) {
|
|
|
1545
1681
|
}
|
|
1546
1682
|
}
|
|
1547
1683
|
|
|
1684
|
+
// packages/execution/src/nested-park.ts
|
|
1685
|
+
import {
|
|
1686
|
+
withCancel,
|
|
1687
|
+
withoutAbortSignal as withoutAbortSignal3
|
|
1688
|
+
} from "@earendil-works/chord/context";
|
|
1689
|
+
import { copyJson as copyJson9 } from "@earendil-works/chord/json";
|
|
1690
|
+
var resultFields = [
|
|
1691
|
+
"output",
|
|
1692
|
+
"structuredOutput",
|
|
1693
|
+
"isError",
|
|
1694
|
+
"details",
|
|
1695
|
+
"diagnostics",
|
|
1696
|
+
"usage",
|
|
1697
|
+
"control"
|
|
1698
|
+
];
|
|
1699
|
+
function sleepState(checkpoint, { until, signals }) {
|
|
1700
|
+
return {
|
|
1701
|
+
status: "sleeping",
|
|
1702
|
+
checkpoint,
|
|
1703
|
+
...until === undefined ? {} : { until },
|
|
1704
|
+
...signals === undefined ? {} : { signals }
|
|
1705
|
+
};
|
|
1706
|
+
}
|
|
1707
|
+
async function parkGate(runtime, sleep, context) {
|
|
1708
|
+
try {
|
|
1709
|
+
await runtime.commit(() => sleepState({ phase: "call" }, sleep), context);
|
|
1710
|
+
} catch (error) {
|
|
1711
|
+
runtime.signal.throwIfAborted();
|
|
1712
|
+
return errorText(error);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
async function parkTool(runtime, invocation, tool, args, park, context) {
|
|
1716
|
+
if (park.kind !== "sleep" && park.kind !== "wait")
|
|
1717
|
+
return "Unknown park kind";
|
|
1718
|
+
const mixed = resultFields.find((field) => (field in park));
|
|
1719
|
+
if (mixed !== undefined)
|
|
1720
|
+
return `A park cannot carry the result field ${mixed}`;
|
|
1721
|
+
const intent = {
|
|
1722
|
+
arguments: args,
|
|
1723
|
+
replay: tool.replay ?? "unsafe",
|
|
1724
|
+
...park.checkpoint === undefined ? {} : { checkpoint: copyJson9(park.checkpoint) }
|
|
1725
|
+
};
|
|
1726
|
+
const { progress, pending } = await invocation.park();
|
|
1727
|
+
try {
|
|
1728
|
+
await runtime.commit(async (tx) => {
|
|
1729
|
+
if (park.kind === "wait") {
|
|
1730
|
+
for (const id of park.on)
|
|
1731
|
+
if ((await tx.task(id))?.owner !== runtime.taskId)
|
|
1732
|
+
throw new Error(`Task ${id} is not owned by the call`);
|
|
1733
|
+
}
|
|
1734
|
+
const checkpoint = { ...intent, phase: "parked" };
|
|
1735
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1736
|
+
doc.result = progress;
|
|
1737
|
+
doc.droppedBytes = 0;
|
|
1738
|
+
doc.droppedLines = 0;
|
|
1739
|
+
await forwardInvocationResult(tx, runtime.taskId, progress, "running");
|
|
1740
|
+
return park.kind === "sleep" ? sleepState(checkpoint, park) : { status: "waiting", checkpoint, on: park.on, policy: park.policy };
|
|
1741
|
+
}, context);
|
|
1742
|
+
} catch (error) {
|
|
1743
|
+
for (const waiter of pending)
|
|
1744
|
+
waiter.reject(error);
|
|
1745
|
+
const detached = withoutAbortSignal3(context);
|
|
1746
|
+
const record = await runtime.getTask(runtime.taskId, detached);
|
|
1747
|
+
if (record?.abortRequested) {
|
|
1748
|
+
const watch = await runtime.watchDoc(InvocationDoc, runtime.taskId, detached);
|
|
1749
|
+
if (watch !== undefined) {
|
|
1750
|
+
const cleanup = withCancel(detached);
|
|
1751
|
+
watch.closed.then((end) => cleanup.cancel(end.kind === "failed" ? end.error : undefined));
|
|
1752
|
+
try {
|
|
1753
|
+
await cancelParkedTool(runtime, tool, intent, cleanup.context);
|
|
1754
|
+
} finally {
|
|
1755
|
+
await watch.stop();
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
throw error;
|
|
1759
|
+
}
|
|
1760
|
+
runtime.signal.throwIfAborted();
|
|
1761
|
+
return errorText(error);
|
|
1762
|
+
}
|
|
1763
|
+
for (const waiter of pending)
|
|
1764
|
+
waiter.resolve();
|
|
1765
|
+
}
|
|
1766
|
+
async function cancelParkedTool(runtime, tool, intent, context) {
|
|
1767
|
+
if (!tool) {
|
|
1768
|
+
runtime.report(new Error("The parked tool is no longer registered; cleanup could not run"));
|
|
1769
|
+
return;
|
|
1770
|
+
}
|
|
1771
|
+
if (tool.cancel === undefined)
|
|
1772
|
+
return;
|
|
1773
|
+
const cleanup = createInvocation(runtime, tool, context, {
|
|
1774
|
+
checkpoint: intent.checkpoint,
|
|
1775
|
+
wake: undefined
|
|
1776
|
+
});
|
|
1777
|
+
const refuseWork = () => {
|
|
1778
|
+
throw new Error("A tool cannot start tasks or nested calls during cancellation");
|
|
1779
|
+
};
|
|
1780
|
+
let reason = new Error("Tool cancellation finished");
|
|
1781
|
+
try {
|
|
1782
|
+
const api = await cleanup.executionApi();
|
|
1783
|
+
await tool.cancel(intent.arguments, {
|
|
1784
|
+
...api,
|
|
1785
|
+
createTask: refuseWork,
|
|
1786
|
+
executeTool: refuseWork
|
|
1787
|
+
}, context);
|
|
1788
|
+
} catch (error) {
|
|
1789
|
+
reason = error;
|
|
1790
|
+
runtime.report(error);
|
|
1791
|
+
} finally {
|
|
1792
|
+
await cleanup.stop(reason);
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1548
1796
|
// packages/execution/src/nested-result.ts
|
|
1549
|
-
import { copyJson as
|
|
1797
|
+
import { copyJson as copyJson10 } from "@earendil-works/chord/json";
|
|
1550
1798
|
import {
|
|
1551
1799
|
defineEntry,
|
|
1552
1800
|
UsageDoc
|
|
@@ -1594,11 +1842,11 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1594
1842
|
taskId: runtime.taskId,
|
|
1595
1843
|
callId: call.id,
|
|
1596
1844
|
name: call.name,
|
|
1597
|
-
arguments:
|
|
1845
|
+
arguments: copyJson10(call.arguments),
|
|
1598
1846
|
result: stored,
|
|
1599
1847
|
...current.state.checkpoint.phase === "execute" ? {
|
|
1600
1848
|
intent: {
|
|
1601
|
-
arguments:
|
|
1849
|
+
arguments: copyJson10(current.state.checkpoint.arguments),
|
|
1602
1850
|
replay: current.state.checkpoint.replay
|
|
1603
1851
|
}
|
|
1604
1852
|
} : {}
|
|
@@ -1607,7 +1855,7 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1607
1855
|
if (result.usage !== undefined) {
|
|
1608
1856
|
const totals = (await tx.doc(UsageDoc, runtime.conversationId)).tools;
|
|
1609
1857
|
const previous = Object.hasOwn(totals, call.name) ? totals[call.name] : undefined;
|
|
1610
|
-
totals[call.name] =
|
|
1858
|
+
totals[call.name] = copyJson10(previous === undefined ? result.usage : sumUsage(previous, result.usage), { omitUndefinedProperties: true });
|
|
1611
1859
|
}
|
|
1612
1860
|
const outcome = ending === "completed" ? { status: ending, result: stored } : ending === "aborted" ? { status: ending, result: stored } : {
|
|
1613
1861
|
status: ending,
|
|
@@ -1644,7 +1892,11 @@ function sumUsage(previous, added) {
|
|
|
1644
1892
|
function createNestedToolTask(name) {
|
|
1645
1893
|
return defineTask2({
|
|
1646
1894
|
name,
|
|
1647
|
-
version:
|
|
1895
|
+
version: 2,
|
|
1896
|
+
migrate: (input, checkpoint) => ({
|
|
1897
|
+
input,
|
|
1898
|
+
checkpoint
|
|
1899
|
+
}),
|
|
1648
1900
|
initial: () => ({ phase: "call" }),
|
|
1649
1901
|
phases: {
|
|
1650
1902
|
call: async (task, runtime, context) => {
|
|
@@ -1679,13 +1931,16 @@ function createNestedToolTask(name) {
|
|
|
1679
1931
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1680
1932
|
}
|
|
1681
1933
|
let block;
|
|
1934
|
+
let sleep;
|
|
1682
1935
|
await eachHook(agent, "beforeTool", runtime, async (handler) => {
|
|
1683
|
-
if (block !== undefined)
|
|
1936
|
+
if (block !== undefined || sleep !== undefined)
|
|
1684
1937
|
return;
|
|
1685
1938
|
try {
|
|
1686
1939
|
const decision = await handler({ ...call, arguments: args }, runtime, context);
|
|
1687
1940
|
if (decision?.block !== undefined)
|
|
1688
1941
|
block = decision.block;
|
|
1942
|
+
else if (decision?.sleep !== undefined)
|
|
1943
|
+
sleep = decision.sleep;
|
|
1689
1944
|
else if (decision?.arguments !== undefined)
|
|
1690
1945
|
args = decision.arguments;
|
|
1691
1946
|
} catch (error) {
|
|
@@ -1696,71 +1951,122 @@ function createNestedToolTask(name) {
|
|
|
1696
1951
|
});
|
|
1697
1952
|
if (block !== undefined)
|
|
1698
1953
|
return reject(runtime, call, executionError("blocked", `Tool call blocked: ${block}`), agent, tool, context);
|
|
1954
|
+
if (sleep !== undefined) {
|
|
1955
|
+
const reason = await parkGate(runtime, sleep, context);
|
|
1956
|
+
if (reason === undefined)
|
|
1957
|
+
return;
|
|
1958
|
+
return reject(runtime, call, executionError("invalid_park", reason), agent, tool, context);
|
|
1959
|
+
}
|
|
1699
1960
|
try {
|
|
1700
|
-
args = checked(tool, call, args)
|
|
1961
|
+
args = copyJson11(checked(tool, call, args), {
|
|
1962
|
+
omitUndefinedProperties: true
|
|
1963
|
+
});
|
|
1701
1964
|
} catch (error) {
|
|
1702
1965
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1703
1966
|
}
|
|
1704
|
-
await runtime
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
callId: call.id,
|
|
1708
|
-
parentCallId: call.parent?.callId ?? `nested-${runtime.taskId}`,
|
|
1709
|
-
name: call.name,
|
|
1710
|
-
arguments: args,
|
|
1711
|
-
status: "running",
|
|
1712
|
-
result: null,
|
|
1713
|
-
omittedFields: []
|
|
1714
|
-
});
|
|
1715
|
-
return {
|
|
1716
|
-
status: "running",
|
|
1717
|
-
checkpoint: {
|
|
1718
|
-
phase: "execute",
|
|
1719
|
-
arguments: args,
|
|
1720
|
-
replay: tool.replay ?? "unsafe"
|
|
1721
|
-
}
|
|
1722
|
-
};
|
|
1967
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1968
|
+
arguments: args,
|
|
1969
|
+
replay: tool.replay ?? "unsafe"
|
|
1723
1970
|
}, context);
|
|
1724
|
-
|
|
1971
|
+
},
|
|
1972
|
+
parked: async (task, runtime, context) => {
|
|
1973
|
+
const intent = task.state.checkpoint;
|
|
1974
|
+
const call = nestedCall(runtime, task.input);
|
|
1975
|
+
call.arguments = intent.arguments;
|
|
1976
|
+
const agent = await runtime.agent(context);
|
|
1977
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1978
|
+
if (tool === undefined || readToolContract(tool).nativeOnly)
|
|
1979
|
+
return interrupted(runtime, call, "interrupted", `Parked tool ${call.name} is no longer available and could not be resumed`, context);
|
|
1980
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1981
|
+
...intent,
|
|
1982
|
+
replay: tool.replay ?? "unsafe"
|
|
1983
|
+
}, context, task.state.wake);
|
|
1725
1984
|
},
|
|
1726
1985
|
execute: async (task, runtime, context) => {
|
|
1986
|
+
const intent = task.state.checkpoint;
|
|
1727
1987
|
const call = nestedCall(runtime, task.input);
|
|
1728
1988
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1729
1989
|
call.arguments = task.state.checkpoint.arguments;
|
|
1730
1990
|
const agent = await runtime.agent(context);
|
|
1731
1991
|
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1732
|
-
if (
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1992
|
+
if (intent.replay === "safe" && tool?.replay === "safe" && !readToolContract(tool).nativeOnly) {
|
|
1993
|
+
if (intent.resume === undefined)
|
|
1994
|
+
await runtime.commit(async (tx) => {
|
|
1995
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1996
|
+
doc.result = {};
|
|
1997
|
+
doc.droppedBytes = 0;
|
|
1998
|
+
doc.droppedLines = 0;
|
|
1999
|
+
return;
|
|
2000
|
+
}, context);
|
|
2001
|
+
return run(runtime, call, tool, intent, agent, context, intent.resume?.wake);
|
|
1741
2002
|
}
|
|
2003
|
+
if (intent.resume !== undefined)
|
|
2004
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1742
2005
|
await interrupted(runtime, call, "interrupted", `Tool ${call.name} was interrupted and may have partially run`, context);
|
|
1743
2006
|
}
|
|
1744
2007
|
},
|
|
1745
2008
|
abort: async (task, runtime, context) => {
|
|
1746
2009
|
const call = nestedCall(runtime, task.input);
|
|
2010
|
+
const intent = task.state.checkpoint;
|
|
2011
|
+
if (intent.phase === "parked" || intent.phase === "execute" && intent.resume !== undefined) {
|
|
2012
|
+
const agent = await runtime.agent(context);
|
|
2013
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
2014
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
2015
|
+
}
|
|
1747
2016
|
await interrupted(runtime, call, "aborted", `Tool ${call.name} was aborted${task.state.checkpoint.phase === "execute" ? " and may have partially run" : ""}`, context);
|
|
1748
2017
|
}
|
|
1749
2018
|
});
|
|
1750
2019
|
}
|
|
2020
|
+
async function startAttempt(runtime, call, tool, agent, intent, context, wake) {
|
|
2021
|
+
await runtime.commit(async (tx) => {
|
|
2022
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
2023
|
+
await forwardCall(tx, doc.forwardingCellId, {
|
|
2024
|
+
callId: call.id,
|
|
2025
|
+
parentCallId: call.parent?.callId ?? call.id,
|
|
2026
|
+
name: call.name,
|
|
2027
|
+
arguments: intent.arguments,
|
|
2028
|
+
status: "running",
|
|
2029
|
+
result: null,
|
|
2030
|
+
omittedFields: []
|
|
2031
|
+
});
|
|
2032
|
+
return {
|
|
2033
|
+
status: "running",
|
|
2034
|
+
checkpoint: {
|
|
2035
|
+
...intent,
|
|
2036
|
+
phase: "execute",
|
|
2037
|
+
...wake === undefined ? {} : { resume: { wake } }
|
|
2038
|
+
}
|
|
2039
|
+
};
|
|
2040
|
+
}, context);
|
|
2041
|
+
await run(runtime, call, tool, intent, agent, context, wake);
|
|
2042
|
+
}
|
|
1751
2043
|
async function reject(runtime, call, result, agent, tool, context) {
|
|
1752
2044
|
await eachHook(agent, "afterTool", runtime, async (handler) => {
|
|
1753
2045
|
result = await handler(call, result, runtime, context) ?? result;
|
|
1754
2046
|
});
|
|
1755
2047
|
await settle(runtime, call, finishResult(tool, call.name, result), "completed", context);
|
|
1756
2048
|
}
|
|
1757
|
-
async function run(runtime, call, tool,
|
|
2049
|
+
async function run(runtime, call, tool, intent, agent, context, wake) {
|
|
1758
2050
|
let invocation;
|
|
1759
2051
|
let result;
|
|
1760
2052
|
let ending = "completed";
|
|
1761
2053
|
try {
|
|
1762
|
-
|
|
1763
|
-
|
|
2054
|
+
const previous = wake === undefined ? undefined : await runtime.snapshot(InvocationDoc, runtime.taskId, context);
|
|
2055
|
+
invocation = createInvocation(runtime, tool, context, {
|
|
2056
|
+
wake,
|
|
2057
|
+
checkpoint: intent.checkpoint,
|
|
2058
|
+
...previous === undefined ? {} : { progress: previous.result }
|
|
2059
|
+
});
|
|
2060
|
+
const returned = await tool.execute(intent.arguments, await invocation.executionApi(), context);
|
|
2061
|
+
if (returned.kind === undefined)
|
|
2062
|
+
result = returned;
|
|
2063
|
+
else {
|
|
2064
|
+
const reason = await parkTool(runtime, invocation, tool, intent.arguments, returned, context);
|
|
2065
|
+
if (reason === undefined)
|
|
2066
|
+
return;
|
|
2067
|
+
result = executionError("invalid_park", reason);
|
|
2068
|
+
ending = "failed";
|
|
2069
|
+
}
|
|
1764
2070
|
} catch (error) {
|
|
1765
2071
|
if (runtime.signal.aborted) {
|
|
1766
2072
|
await invocation?.stop(error);
|
|
@@ -1861,9 +2167,11 @@ function providerProjection(surfaceTools) {
|
|
|
1861
2167
|
const conversation = await host.conversation(api.conversationId, context);
|
|
1862
2168
|
if (conversation === undefined)
|
|
1863
2169
|
throw new Error(`Conversation ${api.conversationId} is unavailable`);
|
|
1864
|
-
for (const tool of (await conversation.agent(context)).tools)
|
|
1865
|
-
|
|
2170
|
+
for (const tool of (await conversation.agent(context)).tools) {
|
|
2171
|
+
const contract = readToolContract(tool);
|
|
2172
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1866
2173
|
visible.add(tool.name);
|
|
2174
|
+
}
|
|
1867
2175
|
}
|
|
1868
2176
|
return { messages: projectToolSchemas(request.messages, visible) };
|
|
1869
2177
|
}
|
|
@@ -1872,7 +2180,7 @@ function providerProjection(surfaceTools) {
|
|
|
1872
2180
|
}
|
|
1873
2181
|
|
|
1874
2182
|
// packages/execution/src/wait-session.ts
|
|
1875
|
-
import { withoutAbortSignal as
|
|
2183
|
+
import { withoutAbortSignal as withoutAbortSignal4 } from "@earendil-works/chord/context";
|
|
1876
2184
|
|
|
1877
2185
|
// packages/execution/src/tool-value.ts
|
|
1878
2186
|
class NestedToolError extends Error {
|
|
@@ -1943,7 +2251,7 @@ async function recoverWaitSession(cellId, yieldTimeMs, maxTokens, api, context,
|
|
|
1943
2251
|
}, context);
|
|
1944
2252
|
let cancelling;
|
|
1945
2253
|
const abort = () => {
|
|
1946
|
-
cancelling ??= cancelTask(id,
|
|
2254
|
+
cancelling ??= cancelTask(id, withoutAbortSignal4(context));
|
|
1947
2255
|
cancelling.catch(() => {});
|
|
1948
2256
|
};
|
|
1949
2257
|
context.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
@@ -2027,7 +2335,7 @@ function createCellCoordinator(options) {
|
|
|
2027
2335
|
if (driver.closed)
|
|
2028
2336
|
throw new Error("Execution host is closed");
|
|
2029
2337
|
assertSingleExecutionMode(await api.agent(context));
|
|
2030
|
-
const copied =
|
|
2338
|
+
const copied = copyJson12(input);
|
|
2031
2339
|
return api.commit(async (tx) => {
|
|
2032
2340
|
const isolatedLimit = options.isolatedConcurrency ?? 0;
|
|
2033
2341
|
const isolated = isolatedLimit > 0 && input["isolated"] === true;
|
|
@@ -2107,7 +2415,7 @@ function createCellCoordinator(options) {
|
|
|
2107
2415
|
if (yielding.consume()) {
|
|
2108
2416
|
return initial;
|
|
2109
2417
|
}
|
|
2110
|
-
const wait =
|
|
2418
|
+
const wait = withCancel2(context);
|
|
2111
2419
|
const timeout = new Error("Cell observation yielded");
|
|
2112
2420
|
const timer = setTimeout(() => wait.cancel(timeout), Math.min(yieldTimeMs, 2147483647));
|
|
2113
2421
|
try {
|
|
@@ -3295,7 +3603,7 @@ function createUpdates(write, context) {
|
|
|
3295
3603
|
}
|
|
3296
3604
|
|
|
3297
3605
|
// packages/code/src/cell-output.ts
|
|
3298
|
-
import { copyJson as
|
|
3606
|
+
import { copyJson as copyJson13 } from "@earendil-works/chord/json";
|
|
3299
3607
|
|
|
3300
3608
|
// packages/code/src/result.ts
|
|
3301
3609
|
function runtimeResult(response, maxTokens = 1e4) {
|
|
@@ -3543,7 +3851,7 @@ ${TRUNCATED_OUTPUT_NOTICE}`
|
|
|
3543
3851
|
...details !== null && typeof details === "object" && !Array.isArray(details) ? details : {},
|
|
3544
3852
|
cellId: observation.cellId,
|
|
3545
3853
|
...observation.forwardedToolCalls === undefined ? {} : {
|
|
3546
|
-
forwardedToolCalls:
|
|
3854
|
+
forwardedToolCalls: copyJson13(observation.forwardedToolCalls, {
|
|
3547
3855
|
omitUndefinedProperties: true
|
|
3548
3856
|
})
|
|
3549
3857
|
},
|
|
@@ -4444,19 +4752,21 @@ function toolBootstrap(tools, yieldMarker) {
|
|
|
4444
4752
|
tool.name,
|
|
4445
4753
|
`durable_tool_${index}`
|
|
4446
4754
|
]);
|
|
4447
|
-
const discovery = tools.map((tool) => ({
|
|
4755
|
+
const discovery = tools.filter((tool) => !tool.hidden).map((tool) => ({
|
|
4448
4756
|
name: tool.name,
|
|
4449
4757
|
description: tool.description
|
|
4450
4758
|
}));
|
|
4451
|
-
return `(()=>{const native=globalThis.tools;const registry=Object.create(null);for(const [name,wire] of ${JSON.stringify(entries)})registry[name]=native[wire];globalThis.tools=registry;globalThis.ALL_TOOLS=${JSON.stringify(discovery)};const emit=globalThis.notify;const yieldCell=globalThis.yield_control;globalThis.yield_control=()=>{emit(${JSON.stringify(yieldMarker)});return yieldCell();};})();`;
|
|
4759
|
+
return `(()=>{const native=globalThis.tools;const registry=Object.create(null);for(const [name,wire] of ${JSON.stringify(entries)})registry[name]=native[wire];globalThis.tools=registry;globalThis.ALL_TOOLS=${JSON.stringify(discovery)};(${installToolNamespacesSource})(registry,${JSON.stringify(namespaceDefinitions(tools))});const emit=globalThis.notify;const yieldCell=globalThis.yield_control;globalThis.yield_control=()=>{emit(${JSON.stringify(yieldMarker)});return yieldCell();};})();`;
|
|
4452
4760
|
}
|
|
4453
4761
|
|
|
4454
4762
|
// packages/code/src/runtime-tools.ts
|
|
4455
|
-
import { copyJson as
|
|
4763
|
+
import { copyJson as copyJson14 } from "@earendil-works/chord/json";
|
|
4456
4764
|
function runtimeTools(api, contracts, observation, context) {
|
|
4457
4765
|
const publish = () => api.publish(observation.result(), context);
|
|
4458
4766
|
const tools = api.registrations.map((registration, index) => ({
|
|
4459
4767
|
name: registration.name,
|
|
4768
|
+
hidden: contracts[index]?.nestedHidden === true || contracts[index]?.jsNamespace !== undefined,
|
|
4769
|
+
...contracts[index]?.nestedHidden || contracts[index]?.jsNamespace === undefined ? {} : { jsNamespace: contracts[index].jsNamespace },
|
|
4460
4770
|
description: contracts[index]?.help ?? registration.description,
|
|
4461
4771
|
inputSchema: contracts[index]?.inputSchema ?? registration.parameters,
|
|
4462
4772
|
invoke: async (input, signal, callId) => {
|
|
@@ -4464,7 +4774,7 @@ function runtimeTools(api, contracts, observation, context) {
|
|
|
4464
4774
|
const invoke = api.tools[registration.name];
|
|
4465
4775
|
if (!invoke)
|
|
4466
4776
|
throw new Error(`Tool ${registration.name} is unavailable`);
|
|
4467
|
-
const argumentsValue =
|
|
4777
|
+
const argumentsValue = copyJson14(input === undefined ? null : input);
|
|
4468
4778
|
const trace = observation.trace(callId, registration.name, argumentsValue);
|
|
4469
4779
|
await publish();
|
|
4470
4780
|
try {
|
|
@@ -4681,7 +4991,7 @@ function createCodeMode(options) {
|
|
|
4681
4991
|
tools: [exec, wait, ...shell.tools],
|
|
4682
4992
|
sections: [
|
|
4683
4993
|
section("code", (input) => {
|
|
4684
|
-
const usage = input.agent.callable.filter((tool) => tool.name !== "exec" && tool.name !== "wait").map(readToolContract).
|
|
4994
|
+
const usage = input.agent.callable.filter((tool) => tool.name !== "exec" && tool.name !== "wait").map(readToolContract).flatMap(jsToolPrompt).join(`
|
|
4685
4995
|
`);
|
|
4686
4996
|
return [
|
|
4687
4997
|
nestedToolDiscoveryGuidance,
|