@howaboua/pi-durable-code 0.10.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 +427 -110
- 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 +10 -3
- 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 +427 -110
- package/package.json +10 -9
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";
|
|
@@ -603,8 +603,14 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
603
603
|
return work;
|
|
604
604
|
};
|
|
605
605
|
const live = await api.watchDoc(LiveDoc2, api.conversationId, context);
|
|
606
|
-
cell.start((
|
|
607
|
-
|
|
606
|
+
cell.start((call) => own(async () => {
|
|
607
|
+
if (call.kind === "failed")
|
|
608
|
+
throw call.error;
|
|
609
|
+
await publish(call.value);
|
|
610
|
+
}));
|
|
611
|
+
live?.start((call) => own(async () => {
|
|
612
|
+
if (call.kind === "failed")
|
|
613
|
+
throw call.error;
|
|
608
614
|
await api.commit(async (tx) => {
|
|
609
615
|
const record = await tx.task(id);
|
|
610
616
|
if (record === undefined || record.state.status === "terminal")
|
|
@@ -624,43 +630,70 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
624
630
|
const endings = await Promise.all([cell.stop(), live?.stop()]);
|
|
625
631
|
await Promise.all([...pending]);
|
|
626
632
|
for (const ending of endings)
|
|
627
|
-
if (ending?.
|
|
633
|
+
if (ending?.kind === "listener_error" || ending?.kind === "failed")
|
|
628
634
|
throw ending.error;
|
|
629
635
|
};
|
|
630
636
|
}
|
|
631
637
|
|
|
632
638
|
// packages/execution/src/cell-task.ts
|
|
633
|
-
import { copyJson as copyJson8 } from "@earendil-works/chord";
|
|
634
639
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
640
|
+
import { copyJson as copyJson8 } from "@earendil-works/chord/json";
|
|
635
641
|
import {
|
|
636
642
|
defineTask
|
|
637
643
|
} from "@earendil-works/pi-durable";
|
|
638
644
|
|
|
639
645
|
// packages/execution/src/cell-tools.ts
|
|
640
|
-
import { copyJson as copyJson6 } from "@earendil-works/chord";
|
|
641
646
|
import { withoutAbortSignal as withoutAbortSignal2 } from "@earendil-works/chord/context";
|
|
647
|
+
import { copyJson as copyJson6 } from "@earendil-works/chord/json";
|
|
642
648
|
|
|
643
649
|
// packages/execution/src/invocation.ts
|
|
644
|
-
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
645
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";
|
|
646
656
|
|
|
647
657
|
// packages/execution/src/native-tools.ts
|
|
648
|
-
import {
|
|
649
|
-
|
|
658
|
+
import {
|
|
659
|
+
withoutAbortSignal
|
|
660
|
+
} from "@earendil-works/chord/context";
|
|
661
|
+
import { copyJson as copyJson4 } from "@earendil-works/chord/json";
|
|
650
662
|
import {
|
|
651
663
|
LiveDoc as LiveDoc3,
|
|
652
664
|
NestedCallDoc as NestedCallDoc2,
|
|
653
|
-
ToolTask as
|
|
665
|
+
ToolTask as ToolTask3
|
|
654
666
|
} from "@earendil-works/pi-durable";
|
|
655
667
|
import { Equal } from "typebox/value";
|
|
656
|
-
|
|
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) {
|
|
657
688
|
let sequence = 0;
|
|
658
689
|
const admissions = [];
|
|
659
|
-
|
|
690
|
+
async function execute(name, args, callContext, options = {}) {
|
|
660
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");
|
|
661
696
|
const key = options.key ?? String(++sequence);
|
|
662
|
-
if (options.key !== undefined && (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key)))
|
|
663
|
-
throw new Error("Invalid nested call key");
|
|
664
697
|
const call = {
|
|
665
698
|
type: "toolCall",
|
|
666
699
|
id: `${callId}/${key}`,
|
|
@@ -672,11 +705,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
672
705
|
const admission = runtime.commit(async (tx) => {
|
|
673
706
|
const slot = await tx.doc(NestedCallDoc2, runtime.taskId, key, null);
|
|
674
707
|
if (slot.taskId !== undefined) {
|
|
675
|
-
const input = (await tx.task(slot.taskId))?.input;
|
|
708
|
+
const input = slot.kind === "task" ? undefined : (await tx.task(slot.taskId))?.input;
|
|
676
709
|
if (input?.kind !== "nested" || input.call.name !== name || !Equal(input.call.arguments, call.arguments))
|
|
677
710
|
throw new Error(`Nested call ${call.id} was already made with another tool or other arguments`);
|
|
678
711
|
} else {
|
|
679
|
-
slot.
|
|
712
|
+
slot.kind = "nested";
|
|
713
|
+
slot.taskId = await tx.createTask(ToolTask3, {
|
|
680
714
|
kind: "nested",
|
|
681
715
|
parent: runtime.taskId,
|
|
682
716
|
parentCallId: callId,
|
|
@@ -688,6 +722,9 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
688
722
|
abandonOnRestart: replay !== "safe"
|
|
689
723
|
});
|
|
690
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 };
|
|
691
728
|
live.nestedTools ??= [];
|
|
692
729
|
live.nestedTools.push({
|
|
693
730
|
callId: call.id,
|
|
@@ -719,7 +756,13 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
719
756
|
const slot = await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext);
|
|
720
757
|
if (slot?.taskId === undefined)
|
|
721
758
|
throw new Error("Nested task creation did not commit");
|
|
722
|
-
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);
|
|
723
766
|
const result = (await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext))?.result;
|
|
724
767
|
await runtime.commit(async (tx) => {
|
|
725
768
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
@@ -727,7 +770,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
727
770
|
return;
|
|
728
771
|
}, callContext);
|
|
729
772
|
return result === undefined ? {
|
|
730
|
-
taskId
|
|
773
|
+
taskId,
|
|
731
774
|
isError: true,
|
|
732
775
|
diagnostics: [
|
|
733
776
|
{
|
|
@@ -737,9 +780,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
737
780
|
}
|
|
738
781
|
]
|
|
739
782
|
} : copyJson4(result);
|
|
740
|
-
}
|
|
783
|
+
}
|
|
741
784
|
return {
|
|
742
785
|
execute,
|
|
786
|
+
async drain() {
|
|
787
|
+
await Promise.allSettled(admissions);
|
|
788
|
+
},
|
|
743
789
|
async finish() {
|
|
744
790
|
await Promise.allSettled(admissions);
|
|
745
791
|
await cleanupNativeCalls(runtime, withoutAbortSignal(context));
|
|
@@ -747,7 +793,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
747
793
|
};
|
|
748
794
|
}
|
|
749
795
|
async function cleanupNativeCalls(runtime, context) {
|
|
750
|
-
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)));
|
|
751
797
|
await runtime.commit(async (tx) => {
|
|
752
798
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
753
799
|
await captureNativeCalls(tx, runtime.conversationId, runtime.taskId, owner.forwardingCellId);
|
|
@@ -841,18 +887,6 @@ class Progress {
|
|
|
841
887
|
}
|
|
842
888
|
}
|
|
843
889
|
|
|
844
|
-
// packages/execution/src/tool-origin.ts
|
|
845
|
-
import {
|
|
846
|
-
defineDoc as defineDoc2
|
|
847
|
-
} from "@earendil-works/pi-durable";
|
|
848
|
-
var ToolOriginDoc = defineDoc2({
|
|
849
|
-
kind: "howaboua.tool-origin",
|
|
850
|
-
version: 1,
|
|
851
|
-
scope: "task",
|
|
852
|
-
checkpointWhen: () => false,
|
|
853
|
-
initial: () => ({})
|
|
854
|
-
});
|
|
855
|
-
|
|
856
890
|
// packages/execution/src/invocation.ts
|
|
857
891
|
function executionError(code, message) {
|
|
858
892
|
return {
|
|
@@ -864,21 +898,23 @@ function executionError(code, message) {
|
|
|
864
898
|
function errorText(error) {
|
|
865
899
|
return error instanceof Error ? error.message : String(error);
|
|
866
900
|
}
|
|
867
|
-
function createInvocation(runtime, tool, context) {
|
|
901
|
+
function createInvocation(runtime, tool, context, resume) {
|
|
868
902
|
const limits = {
|
|
869
903
|
maxBytes: tool.outputLimits?.maxBytes ?? 50 * 1024,
|
|
870
904
|
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
|
871
905
|
retain: tool.outputLimits?.retain ?? "head"
|
|
872
906
|
};
|
|
873
907
|
const output = new OutputBuffer(limits);
|
|
874
|
-
const diagnostics = [
|
|
875
|
-
|
|
908
|
+
const diagnostics = [
|
|
909
|
+
...resume?.progress?.diagnostics ?? []
|
|
910
|
+
];
|
|
911
|
+
let details = resume?.progress?.details;
|
|
876
912
|
let ended = false;
|
|
877
913
|
const assertLive = () => {
|
|
878
914
|
if (ended)
|
|
879
915
|
throw new Error(`Invocation ${runtime.taskId} has settled`);
|
|
880
916
|
};
|
|
881
|
-
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);
|
|
882
918
|
const snapshot = () => {
|
|
883
919
|
const retained = output.snapshot();
|
|
884
920
|
return {
|
|
@@ -907,6 +943,8 @@ function createInvocation(runtime, tool, context) {
|
|
|
907
943
|
taskId: runtime.taskId,
|
|
908
944
|
conversationId: runtime.conversationId,
|
|
909
945
|
callId: `nested-${runtime.taskId}`,
|
|
946
|
+
wake: resume?.wake,
|
|
947
|
+
checkpoint: resume?.checkpoint,
|
|
910
948
|
registry: runtime.registry,
|
|
911
949
|
agent: runtime.agent,
|
|
912
950
|
env: undefined,
|
|
@@ -927,7 +965,7 @@ function createInvocation(runtime, tool, context) {
|
|
|
927
965
|
const retained = output.snapshot();
|
|
928
966
|
return { text: retained.text, truncated: retained.droppedBytes > 0 };
|
|
929
967
|
},
|
|
930
|
-
executeTool:
|
|
968
|
+
executeTool: native.execute,
|
|
931
969
|
diagnostic: (value) => {
|
|
932
970
|
assertLive();
|
|
933
971
|
diagnostics.push(copyJson5(value, { omitUndefinedProperties: true }));
|
|
@@ -941,24 +979,44 @@ function createInvocation(runtime, tool, context) {
|
|
|
941
979
|
committed.catch(() => {});
|
|
942
980
|
await awaitWithContext(committed, detailContext);
|
|
943
981
|
},
|
|
944
|
-
commit: async (change, commitContext) => {
|
|
982
|
+
commit: async (change, commitContext, options) => {
|
|
945
983
|
assertLive();
|
|
946
984
|
let result;
|
|
947
985
|
await runtime.commit(async (tx) => {
|
|
948
986
|
result = await change(tx);
|
|
949
987
|
return;
|
|
950
|
-
}, commitContext);
|
|
988
|
+
}, commitContext, options);
|
|
951
989
|
return result;
|
|
952
990
|
},
|
|
953
991
|
memo: runtime.memo,
|
|
954
|
-
|
|
992
|
+
readSignal: runtime.readSignal,
|
|
993
|
+
async createTask(task, input, {
|
|
994
|
+
key,
|
|
995
|
+
...options
|
|
996
|
+
}, taskContext) {
|
|
955
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");
|
|
956
1002
|
let id;
|
|
957
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
|
+
}
|
|
958
1012
|
id = await tx.createTask(task, input, {
|
|
959
1013
|
...options,
|
|
960
1014
|
...options.abandonOnRestart === undefined && tool.replay !== "safe" && options.ownership.kind === "task" && options.ownership.taskId === runtime.taskId ? { abandonOnRestart: true } : {}
|
|
961
1015
|
});
|
|
1016
|
+
if (member !== undefined) {
|
|
1017
|
+
member.kind = "task";
|
|
1018
|
+
member.taskId = id;
|
|
1019
|
+
}
|
|
962
1020
|
const source = await tx.doc(ToolOriginDoc, runtime.taskId);
|
|
963
1021
|
if (source.origin !== undefined)
|
|
964
1022
|
(await tx.doc(ToolOriginDoc, id)).origin = { ...source.origin };
|
|
@@ -1005,6 +1063,14 @@ function createInvocation(runtime, tool, context) {
|
|
|
1005
1063
|
for (const waiter of await progress.stop())
|
|
1006
1064
|
waiter.reject(error);
|
|
1007
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
|
+
},
|
|
1008
1074
|
limits,
|
|
1009
1075
|
output,
|
|
1010
1076
|
progress
|
|
@@ -1106,7 +1172,74 @@ function combineControls(controls, allowTermination = true) {
|
|
|
1106
1172
|
}
|
|
1107
1173
|
|
|
1108
1174
|
// packages/execution/src/tool-contract.ts
|
|
1109
|
-
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
|
|
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 {}";
|
|
1110
1243
|
function readToolContract(tool) {
|
|
1111
1244
|
const candidate = "executionHints" in tool ? tool.executionHints : undefined;
|
|
1112
1245
|
if (candidate !== undefined && (candidate === null || typeof candidate !== "object" || Array.isArray(candidate))) {
|
|
@@ -1120,6 +1253,7 @@ function readToolContract(tool) {
|
|
|
1120
1253
|
"yieldTimeMs",
|
|
1121
1254
|
"inputSchema",
|
|
1122
1255
|
"nativeOnly",
|
|
1256
|
+
"nestedHidden",
|
|
1123
1257
|
"discovery"
|
|
1124
1258
|
]);
|
|
1125
1259
|
for (const key of Object.keys(hints))
|
|
@@ -1143,6 +1277,12 @@ function readToolContract(tool) {
|
|
|
1143
1277
|
const nativeOnly = "nativeOnly" in hints ? hints.nativeOnly : undefined;
|
|
1144
1278
|
if (nativeOnly !== undefined && typeof nativeOnly !== "boolean")
|
|
1145
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`);
|
|
1146
1286
|
const yieldTimeMs = "yieldTimeMs" in hints ? hints.yieldTimeMs : undefined;
|
|
1147
1287
|
if (yieldTimeMs !== undefined && (typeof yieldTimeMs !== "number" || !Number.isSafeInteger(yieldTimeMs) || yieldTimeMs < 0))
|
|
1148
1288
|
throw new Error(`Tool ${tool.name} executionHints.yieldTimeMs must be a non-negative safe integer`);
|
|
@@ -1159,6 +1299,8 @@ function readToolContract(tool) {
|
|
|
1159
1299
|
inputSchema,
|
|
1160
1300
|
deferLoading: typeof deferred === "boolean" ? deferred : true,
|
|
1161
1301
|
nativeOnly: nativeOnly === true,
|
|
1302
|
+
nestedHidden: nestedHidden === true,
|
|
1303
|
+
...jsNamespace === undefined ? {} : { jsNamespace },
|
|
1162
1304
|
...typeof yieldTimeMs === "number" ? { yieldTimeMs } : {},
|
|
1163
1305
|
...outputHint === undefined ? {} : { output: outputHint },
|
|
1164
1306
|
help: [
|
|
@@ -1394,7 +1536,7 @@ function createCellTask(options, nestedTask) {
|
|
|
1394
1536
|
import {
|
|
1395
1537
|
hook,
|
|
1396
1538
|
LiveDoc as LiveDoc4,
|
|
1397
|
-
ToolTask as
|
|
1539
|
+
ToolTask as ToolTask4
|
|
1398
1540
|
} from "@earendil-works/pi-durable";
|
|
1399
1541
|
function forwardingHook() {
|
|
1400
1542
|
let host;
|
|
@@ -1404,7 +1546,7 @@ function forwardingHook() {
|
|
|
1404
1546
|
throw new Error("Create a separate execution component for each Harness");
|
|
1405
1547
|
host = harness;
|
|
1406
1548
|
},
|
|
1407
|
-
hook: hook(
|
|
1549
|
+
hook: hook(ToolTask4, {
|
|
1408
1550
|
async beforeTool(call, api, context) {
|
|
1409
1551
|
const parent = call.parent;
|
|
1410
1552
|
if (parent === undefined || host === undefined)
|
|
@@ -1417,7 +1559,7 @@ function forwardingHook() {
|
|
|
1417
1559
|
if (conversation === undefined)
|
|
1418
1560
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1419
1561
|
await conversation.commit(async (tx) => {
|
|
1420
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1562
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1421
1563
|
return;
|
|
1422
1564
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1423
1565
|
const admission = owner.forwardingCellId === undefined;
|
|
@@ -1444,7 +1586,7 @@ function forwardingHook() {
|
|
|
1444
1586
|
if (conversation === undefined)
|
|
1445
1587
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1446
1588
|
await conversation.commit(async (tx) => {
|
|
1447
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1589
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1448
1590
|
return;
|
|
1449
1591
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1450
1592
|
const cellId = owner.forwardingCellId;
|
|
@@ -1469,17 +1611,18 @@ function forwardingHook() {
|
|
|
1469
1611
|
}
|
|
1470
1612
|
|
|
1471
1613
|
// packages/execution/src/nested.ts
|
|
1614
|
+
import { copyJson as copyJson11 } from "@earendil-works/chord/json";
|
|
1472
1615
|
import {
|
|
1473
1616
|
defineTask as defineTask2
|
|
1474
1617
|
} from "@earendil-works/pi-durable";
|
|
1475
|
-
import {
|
|
1618
|
+
import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
1476
1619
|
|
|
1477
1620
|
// packages/execution/src/nested-call.ts
|
|
1478
1621
|
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
1479
1622
|
import {
|
|
1480
|
-
ToolTask as
|
|
1623
|
+
ToolTask as ToolTask5
|
|
1481
1624
|
} from "@earendil-works/pi-durable";
|
|
1482
|
-
import {
|
|
1625
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
1483
1626
|
function nestedCall(runtime, input) {
|
|
1484
1627
|
const args = input.arguments;
|
|
1485
1628
|
return {
|
|
@@ -1500,7 +1643,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1500
1643
|
if (typeof input !== "string")
|
|
1501
1644
|
return input;
|
|
1502
1645
|
const contract = readToolContract(tool);
|
|
1503
|
-
if (Boolean(
|
|
1646
|
+
if (Boolean(compile(tool.parameters).check(input)) || (typeof contract.inputSchema === "boolean" || typeof contract.inputSchema === "object" && contract.inputSchema !== null && !Array.isArray(contract.inputSchema)) && Boolean(compile(contract.inputSchema).check(input)))
|
|
1504
1647
|
return input;
|
|
1505
1648
|
const text = input.trim();
|
|
1506
1649
|
if (contract.discovery === "help" && text === "help")
|
|
@@ -1523,7 +1666,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1523
1666
|
async function eachHook(agent, name, api, invoke) {
|
|
1524
1667
|
for (const extension of agent.extensions)
|
|
1525
1668
|
for (const registration of extension.hooks ?? []) {
|
|
1526
|
-
if (registration.task !==
|
|
1669
|
+
if (registration.task !== ToolTask5.definition.name)
|
|
1527
1670
|
continue;
|
|
1528
1671
|
const handler = registration.handlers[name];
|
|
1529
1672
|
if (handler === undefined)
|
|
@@ -1538,8 +1681,120 @@ async function eachHook(agent, name, api, invoke) {
|
|
|
1538
1681
|
}
|
|
1539
1682
|
}
|
|
1540
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
|
+
|
|
1541
1796
|
// packages/execution/src/nested-result.ts
|
|
1542
|
-
import { copyJson as
|
|
1797
|
+
import { copyJson as copyJson10 } from "@earendil-works/chord/json";
|
|
1543
1798
|
import {
|
|
1544
1799
|
defineEntry,
|
|
1545
1800
|
UsageDoc
|
|
@@ -1587,11 +1842,11 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1587
1842
|
taskId: runtime.taskId,
|
|
1588
1843
|
callId: call.id,
|
|
1589
1844
|
name: call.name,
|
|
1590
|
-
arguments:
|
|
1845
|
+
arguments: copyJson10(call.arguments),
|
|
1591
1846
|
result: stored,
|
|
1592
1847
|
...current.state.checkpoint.phase === "execute" ? {
|
|
1593
1848
|
intent: {
|
|
1594
|
-
arguments:
|
|
1849
|
+
arguments: copyJson10(current.state.checkpoint.arguments),
|
|
1595
1850
|
replay: current.state.checkpoint.replay
|
|
1596
1851
|
}
|
|
1597
1852
|
} : {}
|
|
@@ -1600,7 +1855,7 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1600
1855
|
if (result.usage !== undefined) {
|
|
1601
1856
|
const totals = (await tx.doc(UsageDoc, runtime.conversationId)).tools;
|
|
1602
1857
|
const previous = Object.hasOwn(totals, call.name) ? totals[call.name] : undefined;
|
|
1603
|
-
totals[call.name] =
|
|
1858
|
+
totals[call.name] = copyJson10(previous === undefined ? result.usage : sumUsage(previous, result.usage), { omitUndefinedProperties: true });
|
|
1604
1859
|
}
|
|
1605
1860
|
const outcome = ending === "completed" ? { status: ending, result: stored } : ending === "aborted" ? { status: ending, result: stored } : {
|
|
1606
1861
|
status: ending,
|
|
@@ -1637,7 +1892,11 @@ function sumUsage(previous, added) {
|
|
|
1637
1892
|
function createNestedToolTask(name) {
|
|
1638
1893
|
return defineTask2({
|
|
1639
1894
|
name,
|
|
1640
|
-
version:
|
|
1895
|
+
version: 2,
|
|
1896
|
+
migrate: (input, checkpoint) => ({
|
|
1897
|
+
input,
|
|
1898
|
+
checkpoint
|
|
1899
|
+
}),
|
|
1641
1900
|
initial: () => ({ phase: "call" }),
|
|
1642
1901
|
phases: {
|
|
1643
1902
|
call: async (task, runtime, context) => {
|
|
@@ -1663,22 +1922,25 @@ function createNestedToolTask(name) {
|
|
|
1663
1922
|
return reject(runtime, call, executionError("tool_unavailable", `Tool ${call.name} is not available`), agent, tool, context);
|
|
1664
1923
|
let args;
|
|
1665
1924
|
try {
|
|
1666
|
-
const supplied = task.input.arguments === null && !
|
|
1925
|
+
const supplied = task.input.arguments === null && !compile2(tool.parameters).check(null) && compile2(tool.parameters).check({}) ? undefined : task.input.arguments;
|
|
1667
1926
|
const prepared = tool.prepareArguments === undefined ? prepareNestedInput(tool, supplied) : tool.prepareArguments(prepareNestedInput(tool, supplied));
|
|
1668
|
-
args = checked(tool, call, prepared === undefined &&
|
|
1927
|
+
args = checked(tool, call, prepared === undefined && compile2(tool.parameters).check({}) ? {} : prepared);
|
|
1669
1928
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1670
1929
|
call.arguments = args;
|
|
1671
1930
|
} catch (error) {
|
|
1672
1931
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1673
1932
|
}
|
|
1674
1933
|
let block;
|
|
1934
|
+
let sleep;
|
|
1675
1935
|
await eachHook(agent, "beforeTool", runtime, async (handler) => {
|
|
1676
|
-
if (block !== undefined)
|
|
1936
|
+
if (block !== undefined || sleep !== undefined)
|
|
1677
1937
|
return;
|
|
1678
1938
|
try {
|
|
1679
1939
|
const decision = await handler({ ...call, arguments: args }, runtime, context);
|
|
1680
1940
|
if (decision?.block !== undefined)
|
|
1681
1941
|
block = decision.block;
|
|
1942
|
+
else if (decision?.sleep !== undefined)
|
|
1943
|
+
sleep = decision.sleep;
|
|
1682
1944
|
else if (decision?.arguments !== undefined)
|
|
1683
1945
|
args = decision.arguments;
|
|
1684
1946
|
} catch (error) {
|
|
@@ -1689,71 +1951,122 @@ function createNestedToolTask(name) {
|
|
|
1689
1951
|
});
|
|
1690
1952
|
if (block !== undefined)
|
|
1691
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
|
+
}
|
|
1692
1960
|
try {
|
|
1693
|
-
args = checked(tool, call, args)
|
|
1961
|
+
args = copyJson11(checked(tool, call, args), {
|
|
1962
|
+
omitUndefinedProperties: true
|
|
1963
|
+
});
|
|
1694
1964
|
} catch (error) {
|
|
1695
1965
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1696
1966
|
}
|
|
1697
|
-
await runtime
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
callId: call.id,
|
|
1701
|
-
parentCallId: call.parent?.callId ?? `nested-${runtime.taskId}`,
|
|
1702
|
-
name: call.name,
|
|
1703
|
-
arguments: args,
|
|
1704
|
-
status: "running",
|
|
1705
|
-
result: null,
|
|
1706
|
-
omittedFields: []
|
|
1707
|
-
});
|
|
1708
|
-
return {
|
|
1709
|
-
status: "running",
|
|
1710
|
-
checkpoint: {
|
|
1711
|
-
phase: "execute",
|
|
1712
|
-
arguments: args,
|
|
1713
|
-
replay: tool.replay ?? "unsafe"
|
|
1714
|
-
}
|
|
1715
|
-
};
|
|
1967
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1968
|
+
arguments: args,
|
|
1969
|
+
replay: tool.replay ?? "unsafe"
|
|
1716
1970
|
}, context);
|
|
1717
|
-
|
|
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);
|
|
1718
1984
|
},
|
|
1719
1985
|
execute: async (task, runtime, context) => {
|
|
1986
|
+
const intent = task.state.checkpoint;
|
|
1720
1987
|
const call = nestedCall(runtime, task.input);
|
|
1721
1988
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1722
1989
|
call.arguments = task.state.checkpoint.arguments;
|
|
1723
1990
|
const agent = await runtime.agent(context);
|
|
1724
1991
|
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1725
|
-
if (
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
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);
|
|
1734
2002
|
}
|
|
2003
|
+
if (intent.resume !== undefined)
|
|
2004
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1735
2005
|
await interrupted(runtime, call, "interrupted", `Tool ${call.name} was interrupted and may have partially run`, context);
|
|
1736
2006
|
}
|
|
1737
2007
|
},
|
|
1738
2008
|
abort: async (task, runtime, context) => {
|
|
1739
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
|
+
}
|
|
1740
2016
|
await interrupted(runtime, call, "aborted", `Tool ${call.name} was aborted${task.state.checkpoint.phase === "execute" ? " and may have partially run" : ""}`, context);
|
|
1741
2017
|
}
|
|
1742
2018
|
});
|
|
1743
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
|
+
}
|
|
1744
2043
|
async function reject(runtime, call, result, agent, tool, context) {
|
|
1745
2044
|
await eachHook(agent, "afterTool", runtime, async (handler) => {
|
|
1746
2045
|
result = await handler(call, result, runtime, context) ?? result;
|
|
1747
2046
|
});
|
|
1748
2047
|
await settle(runtime, call, finishResult(tool, call.name, result), "completed", context);
|
|
1749
2048
|
}
|
|
1750
|
-
async function run(runtime, call, tool,
|
|
2049
|
+
async function run(runtime, call, tool, intent, agent, context, wake) {
|
|
1751
2050
|
let invocation;
|
|
1752
2051
|
let result;
|
|
1753
2052
|
let ending = "completed";
|
|
1754
2053
|
try {
|
|
1755
|
-
|
|
1756
|
-
|
|
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
|
+
}
|
|
1757
2070
|
} catch (error) {
|
|
1758
2071
|
if (runtime.signal.aborted) {
|
|
1759
2072
|
await invocation?.stop(error);
|
|
@@ -1805,7 +2118,7 @@ function finishResult(tool, name, result) {
|
|
|
1805
2118
|
} else if (final.structuredOutput === undefined) {
|
|
1806
2119
|
if (!final.isError)
|
|
1807
2120
|
throw new Error(`Tool ${name} returned no structuredOutput`);
|
|
1808
|
-
} else if (!
|
|
2121
|
+
} else if (!compile2(tool.structuredOutputSchema).check(final.structuredOutput)) {
|
|
1809
2122
|
throw new Error(`Tool ${name} returned structuredOutput that does not match its schema`);
|
|
1810
2123
|
}
|
|
1811
2124
|
} catch (error) {
|
|
@@ -1854,9 +2167,11 @@ function providerProjection(surfaceTools) {
|
|
|
1854
2167
|
const conversation = await host.conversation(api.conversationId, context);
|
|
1855
2168
|
if (conversation === undefined)
|
|
1856
2169
|
throw new Error(`Conversation ${api.conversationId} is unavailable`);
|
|
1857
|
-
for (const tool of (await conversation.agent(context)).tools)
|
|
1858
|
-
|
|
2170
|
+
for (const tool of (await conversation.agent(context)).tools) {
|
|
2171
|
+
const contract = readToolContract(tool);
|
|
2172
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1859
2173
|
visible.add(tool.name);
|
|
2174
|
+
}
|
|
1860
2175
|
}
|
|
1861
2176
|
return { messages: projectToolSchemas(request.messages, visible) };
|
|
1862
2177
|
}
|
|
@@ -1865,7 +2180,7 @@ function providerProjection(surfaceTools) {
|
|
|
1865
2180
|
}
|
|
1866
2181
|
|
|
1867
2182
|
// packages/execution/src/wait-session.ts
|
|
1868
|
-
import { withoutAbortSignal as
|
|
2183
|
+
import { withoutAbortSignal as withoutAbortSignal4 } from "@earendil-works/chord/context";
|
|
1869
2184
|
|
|
1870
2185
|
// packages/execution/src/tool-value.ts
|
|
1871
2186
|
class NestedToolError extends Error {
|
|
@@ -1936,7 +2251,7 @@ async function recoverWaitSession(cellId, yieldTimeMs, maxTokens, api, context,
|
|
|
1936
2251
|
}, context);
|
|
1937
2252
|
let cancelling;
|
|
1938
2253
|
const abort = () => {
|
|
1939
|
-
cancelling ??= cancelTask(id,
|
|
2254
|
+
cancelling ??= cancelTask(id, withoutAbortSignal4(context));
|
|
1940
2255
|
cancelling.catch(() => {});
|
|
1941
2256
|
};
|
|
1942
2257
|
context.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
@@ -2020,7 +2335,7 @@ function createCellCoordinator(options) {
|
|
|
2020
2335
|
if (driver.closed)
|
|
2021
2336
|
throw new Error("Execution host is closed");
|
|
2022
2337
|
assertSingleExecutionMode(await api.agent(context));
|
|
2023
|
-
const copied =
|
|
2338
|
+
const copied = copyJson12(input);
|
|
2024
2339
|
return api.commit(async (tx) => {
|
|
2025
2340
|
const isolatedLimit = options.isolatedConcurrency ?? 0;
|
|
2026
2341
|
const isolated = isolatedLimit > 0 && input["isolated"] === true;
|
|
@@ -2100,7 +2415,7 @@ function createCellCoordinator(options) {
|
|
|
2100
2415
|
if (yielding.consume()) {
|
|
2101
2416
|
return initial;
|
|
2102
2417
|
}
|
|
2103
|
-
const wait =
|
|
2418
|
+
const wait = withCancel2(context);
|
|
2104
2419
|
const timeout = new Error("Cell observation yielded");
|
|
2105
2420
|
const timer = setTimeout(() => wait.cancel(timeout), Math.min(yieldTimeMs, 2147483647));
|
|
2106
2421
|
try {
|
|
@@ -3288,7 +3603,7 @@ function createUpdates(write, context) {
|
|
|
3288
3603
|
}
|
|
3289
3604
|
|
|
3290
3605
|
// packages/code/src/cell-output.ts
|
|
3291
|
-
import { copyJson as
|
|
3606
|
+
import { copyJson as copyJson13 } from "@earendil-works/chord/json";
|
|
3292
3607
|
|
|
3293
3608
|
// packages/code/src/result.ts
|
|
3294
3609
|
function runtimeResult(response, maxTokens = 1e4) {
|
|
@@ -3536,7 +3851,7 @@ ${TRUNCATED_OUTPUT_NOTICE}`
|
|
|
3536
3851
|
...details !== null && typeof details === "object" && !Array.isArray(details) ? details : {},
|
|
3537
3852
|
cellId: observation.cellId,
|
|
3538
3853
|
...observation.forwardedToolCalls === undefined ? {} : {
|
|
3539
|
-
forwardedToolCalls:
|
|
3854
|
+
forwardedToolCalls: copyJson13(observation.forwardedToolCalls, {
|
|
3540
3855
|
omitUndefinedProperties: true
|
|
3541
3856
|
})
|
|
3542
3857
|
},
|
|
@@ -4437,19 +4752,21 @@ function toolBootstrap(tools, yieldMarker) {
|
|
|
4437
4752
|
tool.name,
|
|
4438
4753
|
`durable_tool_${index}`
|
|
4439
4754
|
]);
|
|
4440
|
-
const discovery = tools.map((tool) => ({
|
|
4755
|
+
const discovery = tools.filter((tool) => !tool.hidden).map((tool) => ({
|
|
4441
4756
|
name: tool.name,
|
|
4442
4757
|
description: tool.description
|
|
4443
4758
|
}));
|
|
4444
|
-
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();};})();`;
|
|
4445
4760
|
}
|
|
4446
4761
|
|
|
4447
4762
|
// packages/code/src/runtime-tools.ts
|
|
4448
|
-
import { copyJson as
|
|
4763
|
+
import { copyJson as copyJson14 } from "@earendil-works/chord/json";
|
|
4449
4764
|
function runtimeTools(api, contracts, observation, context) {
|
|
4450
4765
|
const publish = () => api.publish(observation.result(), context);
|
|
4451
4766
|
const tools = api.registrations.map((registration, index) => ({
|
|
4452
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 },
|
|
4453
4770
|
description: contracts[index]?.help ?? registration.description,
|
|
4454
4771
|
inputSchema: contracts[index]?.inputSchema ?? registration.parameters,
|
|
4455
4772
|
invoke: async (input, signal, callId) => {
|
|
@@ -4457,7 +4774,7 @@ function runtimeTools(api, contracts, observation, context) {
|
|
|
4457
4774
|
const invoke = api.tools[registration.name];
|
|
4458
4775
|
if (!invoke)
|
|
4459
4776
|
throw new Error(`Tool ${registration.name} is unavailable`);
|
|
4460
|
-
const argumentsValue =
|
|
4777
|
+
const argumentsValue = copyJson14(input === undefined ? null : input);
|
|
4461
4778
|
const trace = observation.trace(callId, registration.name, argumentsValue);
|
|
4462
4779
|
await publish();
|
|
4463
4780
|
try {
|
|
@@ -4674,10 +4991,10 @@ function createCodeMode(options) {
|
|
|
4674
4991
|
tools: [exec, wait, ...shell.tools],
|
|
4675
4992
|
sections: [
|
|
4676
4993
|
section("code", (input) => {
|
|
4677
|
-
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(`
|
|
4678
4995
|
`);
|
|
4679
4996
|
return [
|
|
4680
|
-
|
|
4997
|
+
nestedToolDiscoveryGuidance,
|
|
4681
4998
|
'exec accepts JavaScript source with optional // @exec: {"yield_time_ms":30000,"max_output_tokens":10000}. Await work and emit with text(value), image(value), generatedImage(value), notify(value). tools contains ordinary registered tools. ALL_TOOLS lists callable contracts. store(key,value)/load(key) retain serializable values between cells. Cells have no filesystem, network or Node globals. Shell tools follow the conversation environment. Use wait for exec cells and tools.write_stdin for shell sessions.',
|
|
4682
4999
|
usage
|
|
4683
5000
|
].filter(Boolean).join(`
|