@howaboua/pi-durable-notebook 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 +423 -107
- 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 +423 -107
- package/dist/notebook/src/engine.d.ts +1 -1
- package/dist/notebook/src/history.d.ts +4 -4
- package/dist/notebook/src/index.d.ts +6 -3
- package/dist/notebook/src/runtime-contract.d.ts +3 -0
- package/dist/notebook/src/tool-observation.d.ts +5 -4
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// packages/notebook/src/index.ts
|
|
2
|
-
import { copyJson as
|
|
2
|
+
import { copyJson as copyJson15 } from "@earendil-works/chord/json";
|
|
3
3
|
import {
|
|
4
4
|
defineExtension as defineExtension3,
|
|
5
5
|
defineTool as defineTool3,
|
|
@@ -71,8 +71,8 @@ function parseInteger(value, name, minimum = 0, maximum = Number.MAX_SAFE_INTEGE
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
// packages/execution/src/cells.ts
|
|
74
|
-
import {
|
|
75
|
-
import {
|
|
74
|
+
import { withCancel as withCancel2 } from "@earendil-works/chord/context";
|
|
75
|
+
import { copyJson as copyJson12 } from "@earendil-works/chord/json";
|
|
76
76
|
import {
|
|
77
77
|
defineExtension
|
|
78
78
|
} from "@earendil-works/pi-durable";
|
|
@@ -83,7 +83,7 @@ import {
|
|
|
83
83
|
} from "@earendil-works/pi-durable";
|
|
84
84
|
|
|
85
85
|
// packages/execution/src/forwarding.ts
|
|
86
|
-
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
86
|
+
import { copyJson as copyJson2 } from "@earendil-works/chord/json";
|
|
87
87
|
import {
|
|
88
88
|
LiveDoc,
|
|
89
89
|
NestedCallDoc,
|
|
@@ -93,7 +93,7 @@ import {
|
|
|
93
93
|
// packages/execution/src/invocation-state.ts
|
|
94
94
|
import {
|
|
95
95
|
copyJson
|
|
96
|
-
} from "@earendil-works/chord";
|
|
96
|
+
} from "@earendil-works/chord/json";
|
|
97
97
|
import {
|
|
98
98
|
defineDoc
|
|
99
99
|
} from "@earendil-works/pi-durable";
|
|
@@ -570,7 +570,7 @@ async function observeCell(kind, id, api, context, budget) {
|
|
|
570
570
|
}
|
|
571
571
|
|
|
572
572
|
// packages/execution/src/cell-progress.ts
|
|
573
|
-
import { copyJson as copyJson3 } from "@earendil-works/chord";
|
|
573
|
+
import { copyJson as copyJson3 } from "@earendil-works/chord/json";
|
|
574
574
|
import {
|
|
575
575
|
LiveDoc as LiveDoc2
|
|
576
576
|
} from "@earendil-works/pi-durable";
|
|
@@ -630,37 +630,64 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
630
630
|
}
|
|
631
631
|
|
|
632
632
|
// packages/execution/src/cell-task.ts
|
|
633
|
-
import { copyJson as copyJson8 } from "@earendil-works/chord";
|
|
634
633
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
634
|
+
import { copyJson as copyJson8 } from "@earendil-works/chord/json";
|
|
635
635
|
import {
|
|
636
636
|
defineTask
|
|
637
637
|
} from "@earendil-works/pi-durable";
|
|
638
638
|
|
|
639
639
|
// packages/execution/src/cell-tools.ts
|
|
640
|
-
import { copyJson as copyJson6 } from "@earendil-works/chord";
|
|
641
640
|
import { withoutAbortSignal as withoutAbortSignal2 } from "@earendil-works/chord/context";
|
|
641
|
+
import { copyJson as copyJson6 } from "@earendil-works/chord/json";
|
|
642
642
|
|
|
643
643
|
// packages/execution/src/invocation.ts
|
|
644
|
-
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
645
644
|
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
645
|
+
import { copyJson as copyJson5 } from "@earendil-works/chord/json";
|
|
646
|
+
import {
|
|
647
|
+
NestedCallDoc as NestedCallDoc3
|
|
648
|
+
} from "@earendil-works/pi-durable";
|
|
649
|
+
import { equal } from "@earendil-works/pi-schema";
|
|
646
650
|
|
|
647
651
|
// packages/execution/src/native-tools.ts
|
|
648
|
-
import {
|
|
649
|
-
|
|
652
|
+
import {
|
|
653
|
+
withoutAbortSignal
|
|
654
|
+
} from "@earendil-works/chord/context";
|
|
655
|
+
import { copyJson as copyJson4 } from "@earendil-works/chord/json";
|
|
650
656
|
import {
|
|
651
657
|
LiveDoc as LiveDoc3,
|
|
652
658
|
NestedCallDoc as NestedCallDoc2,
|
|
653
|
-
ToolTask as
|
|
659
|
+
ToolTask as ToolTask3
|
|
654
660
|
} from "@earendil-works/pi-durable";
|
|
655
661
|
import { Equal } from "typebox/value";
|
|
656
|
-
|
|
662
|
+
|
|
663
|
+
// packages/execution/src/tool-origin.ts
|
|
664
|
+
import {
|
|
665
|
+
defineDoc as defineDoc2,
|
|
666
|
+
ToolTask as ToolTask2
|
|
667
|
+
} from "@earendil-works/pi-durable";
|
|
668
|
+
var ToolOriginDoc = defineDoc2({
|
|
669
|
+
kind: "howaboua.tool-origin",
|
|
670
|
+
version: 1,
|
|
671
|
+
scope: "task",
|
|
672
|
+
checkpointWhen: () => false,
|
|
673
|
+
initial: () => ({})
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
// packages/execution/src/native-tools.ts
|
|
677
|
+
function assertNestedKey(key) {
|
|
678
|
+
if (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key))
|
|
679
|
+
throw new Error("Invalid nested call or task key");
|
|
680
|
+
}
|
|
681
|
+
function nativeToolCalls(runtime, callId, replay, resumed, assertLive, context) {
|
|
657
682
|
let sequence = 0;
|
|
658
683
|
const admissions = [];
|
|
659
|
-
|
|
684
|
+
async function execute(name, args, callContext, options = {}) {
|
|
660
685
|
assertLive();
|
|
686
|
+
if (options.key !== undefined)
|
|
687
|
+
assertNestedKey(options.key);
|
|
688
|
+
else if (resumed)
|
|
689
|
+
throw new Error("Nested calls require an explicit key after waking");
|
|
661
690
|
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
691
|
const call = {
|
|
665
692
|
type: "toolCall",
|
|
666
693
|
id: `${callId}/${key}`,
|
|
@@ -672,11 +699,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
672
699
|
const admission = runtime.commit(async (tx) => {
|
|
673
700
|
const slot = await tx.doc(NestedCallDoc2, runtime.taskId, key, null);
|
|
674
701
|
if (slot.taskId !== undefined) {
|
|
675
|
-
const input = (await tx.task(slot.taskId))?.input;
|
|
702
|
+
const input = slot.kind === "task" ? undefined : (await tx.task(slot.taskId))?.input;
|
|
676
703
|
if (input?.kind !== "nested" || input.call.name !== name || !Equal(input.call.arguments, call.arguments))
|
|
677
704
|
throw new Error(`Nested call ${call.id} was already made with another tool or other arguments`);
|
|
678
705
|
} else {
|
|
679
|
-
slot.
|
|
706
|
+
slot.kind = "nested";
|
|
707
|
+
slot.taskId = await tx.createTask(ToolTask3, {
|
|
680
708
|
kind: "nested",
|
|
681
709
|
parent: runtime.taskId,
|
|
682
710
|
parentCallId: callId,
|
|
@@ -688,6 +716,9 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
688
716
|
abandonOnRestart: replay !== "safe"
|
|
689
717
|
});
|
|
690
718
|
const live = await tx.doc(LiveDoc3, runtime.conversationId);
|
|
719
|
+
const origin = (await tx.doc(ToolOriginDoc, runtime.taskId)).origin;
|
|
720
|
+
if (origin)
|
|
721
|
+
(await tx.doc(ToolOriginDoc, slot.taskId)).origin = { ...origin };
|
|
691
722
|
live.nestedTools ??= [];
|
|
692
723
|
live.nestedTools.push({
|
|
693
724
|
callId: call.id,
|
|
@@ -719,7 +750,13 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
719
750
|
const slot = await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext);
|
|
720
751
|
if (slot?.taskId === undefined)
|
|
721
752
|
throw new Error("Nested task creation did not commit");
|
|
722
|
-
const
|
|
753
|
+
const taskId = slot.taskId;
|
|
754
|
+
const result = (resultContext) => readResult(name, key, taskId, resultContext);
|
|
755
|
+
return options.wait === false ? { taskId, result } : result(callContext);
|
|
756
|
+
}
|
|
757
|
+
async function readResult(name, key, taskId, callContext) {
|
|
758
|
+
assertLive();
|
|
759
|
+
const settled = await runtime.waitForTask(taskId, callContext);
|
|
723
760
|
const result = (await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext))?.result;
|
|
724
761
|
await runtime.commit(async (tx) => {
|
|
725
762
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
@@ -727,7 +764,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
727
764
|
return;
|
|
728
765
|
}, callContext);
|
|
729
766
|
return result === undefined ? {
|
|
730
|
-
taskId
|
|
767
|
+
taskId,
|
|
731
768
|
isError: true,
|
|
732
769
|
diagnostics: [
|
|
733
770
|
{
|
|
@@ -737,9 +774,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
737
774
|
}
|
|
738
775
|
]
|
|
739
776
|
} : copyJson4(result);
|
|
740
|
-
}
|
|
777
|
+
}
|
|
741
778
|
return {
|
|
742
779
|
execute,
|
|
780
|
+
async drain() {
|
|
781
|
+
await Promise.allSettled(admissions);
|
|
782
|
+
},
|
|
743
783
|
async finish() {
|
|
744
784
|
await Promise.allSettled(admissions);
|
|
745
785
|
await cleanupNativeCalls(runtime, withoutAbortSignal(context));
|
|
@@ -747,7 +787,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
747
787
|
};
|
|
748
788
|
}
|
|
749
789
|
async function cleanupNativeCalls(runtime, context) {
|
|
750
|
-
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind ===
|
|
790
|
+
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind === ToolTask3.definition.name).map((child) => runtime.abortOwned(child.id, context)));
|
|
751
791
|
await runtime.commit(async (tx) => {
|
|
752
792
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
753
793
|
await captureNativeCalls(tx, runtime.conversationId, runtime.taskId, owner.forwardingCellId);
|
|
@@ -841,18 +881,6 @@ class Progress {
|
|
|
841
881
|
}
|
|
842
882
|
}
|
|
843
883
|
|
|
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
884
|
// packages/execution/src/invocation.ts
|
|
857
885
|
function executionError(code, message) {
|
|
858
886
|
return {
|
|
@@ -864,21 +892,23 @@ function executionError(code, message) {
|
|
|
864
892
|
function errorText(error) {
|
|
865
893
|
return error instanceof Error ? error.message : String(error);
|
|
866
894
|
}
|
|
867
|
-
function createInvocation(runtime, tool, context) {
|
|
895
|
+
function createInvocation(runtime, tool, context, resume) {
|
|
868
896
|
const limits = {
|
|
869
897
|
maxBytes: tool.outputLimits?.maxBytes ?? 50 * 1024,
|
|
870
898
|
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
|
871
899
|
retain: tool.outputLimits?.retain ?? "head"
|
|
872
900
|
};
|
|
873
901
|
const output = new OutputBuffer(limits);
|
|
874
|
-
const diagnostics = [
|
|
875
|
-
|
|
902
|
+
const diagnostics = [
|
|
903
|
+
...resume?.progress?.diagnostics ?? []
|
|
904
|
+
];
|
|
905
|
+
let details = resume?.progress?.details;
|
|
876
906
|
let ended = false;
|
|
877
907
|
const assertLive = () => {
|
|
878
908
|
if (ended)
|
|
879
909
|
throw new Error(`Invocation ${runtime.taskId} has settled`);
|
|
880
910
|
};
|
|
881
|
-
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, assertLive, context);
|
|
911
|
+
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, resume?.wake !== undefined, assertLive, context);
|
|
882
912
|
const snapshot = () => {
|
|
883
913
|
const retained = output.snapshot();
|
|
884
914
|
return {
|
|
@@ -907,6 +937,8 @@ function createInvocation(runtime, tool, context) {
|
|
|
907
937
|
taskId: runtime.taskId,
|
|
908
938
|
conversationId: runtime.conversationId,
|
|
909
939
|
callId: `nested-${runtime.taskId}`,
|
|
940
|
+
wake: resume?.wake,
|
|
941
|
+
checkpoint: resume?.checkpoint,
|
|
910
942
|
registry: runtime.registry,
|
|
911
943
|
agent: runtime.agent,
|
|
912
944
|
env: undefined,
|
|
@@ -927,7 +959,7 @@ function createInvocation(runtime, tool, context) {
|
|
|
927
959
|
const retained = output.snapshot();
|
|
928
960
|
return { text: retained.text, truncated: retained.droppedBytes > 0 };
|
|
929
961
|
},
|
|
930
|
-
executeTool:
|
|
962
|
+
executeTool: native.execute,
|
|
931
963
|
diagnostic: (value) => {
|
|
932
964
|
assertLive();
|
|
933
965
|
diagnostics.push(copyJson5(value, { omitUndefinedProperties: true }));
|
|
@@ -941,24 +973,44 @@ function createInvocation(runtime, tool, context) {
|
|
|
941
973
|
committed.catch(() => {});
|
|
942
974
|
await awaitWithContext(committed, detailContext);
|
|
943
975
|
},
|
|
944
|
-
commit: async (change, commitContext) => {
|
|
976
|
+
commit: async (change, commitContext, options) => {
|
|
945
977
|
assertLive();
|
|
946
978
|
let result;
|
|
947
979
|
await runtime.commit(async (tx) => {
|
|
948
980
|
result = await change(tx);
|
|
949
981
|
return;
|
|
950
|
-
}, commitContext);
|
|
982
|
+
}, commitContext, options);
|
|
951
983
|
return result;
|
|
952
984
|
},
|
|
953
985
|
memo: runtime.memo,
|
|
954
|
-
|
|
986
|
+
readSignal: runtime.readSignal,
|
|
987
|
+
async createTask(task, input, {
|
|
988
|
+
key,
|
|
989
|
+
...options
|
|
990
|
+
}, taskContext) {
|
|
955
991
|
assertLive();
|
|
992
|
+
if (key !== undefined)
|
|
993
|
+
assertNestedKey(key);
|
|
994
|
+
else if (resume?.wake !== undefined)
|
|
995
|
+
throw new Error("Child tasks require an explicit key after waking");
|
|
956
996
|
let id;
|
|
957
997
|
await runtime.commit(async (tx) => {
|
|
998
|
+
const member = key === undefined ? undefined : await tx.doc(NestedCallDoc3, runtime.taskId, key, null);
|
|
999
|
+
if (member?.taskId !== undefined) {
|
|
1000
|
+
const existing = member.kind === "task" ? await tx.task(member.taskId) : undefined;
|
|
1001
|
+
if (existing?.kind !== task.definition.name || !equal(existing.input, copyJson5(input, { omitUndefinedProperties: true })))
|
|
1002
|
+
throw new Error(`Key ${key} was already used for another call or task`);
|
|
1003
|
+
id = member.taskId;
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
958
1006
|
id = await tx.createTask(task, input, {
|
|
959
1007
|
...options,
|
|
960
1008
|
...options.abandonOnRestart === undefined && tool.replay !== "safe" && options.ownership.kind === "task" && options.ownership.taskId === runtime.taskId ? { abandonOnRestart: true } : {}
|
|
961
1009
|
});
|
|
1010
|
+
if (member !== undefined) {
|
|
1011
|
+
member.kind = "task";
|
|
1012
|
+
member.taskId = id;
|
|
1013
|
+
}
|
|
962
1014
|
const source = await tx.doc(ToolOriginDoc, runtime.taskId);
|
|
963
1015
|
if (source.origin !== undefined)
|
|
964
1016
|
(await tx.doc(ToolOriginDoc, id)).origin = { ...source.origin };
|
|
@@ -1005,6 +1057,14 @@ function createInvocation(runtime, tool, context) {
|
|
|
1005
1057
|
for (const waiter of await progress.stop())
|
|
1006
1058
|
waiter.reject(error);
|
|
1007
1059
|
},
|
|
1060
|
+
async park() {
|
|
1061
|
+
ended = true;
|
|
1062
|
+
await native.drain();
|
|
1063
|
+
output.end();
|
|
1064
|
+
const pending = await progress.stop();
|
|
1065
|
+
const { output: _output, ...kept } = snapshot();
|
|
1066
|
+
return { progress: storeResult(kept), pending };
|
|
1067
|
+
},
|
|
1008
1068
|
limits,
|
|
1009
1069
|
output,
|
|
1010
1070
|
progress
|
|
@@ -1106,7 +1166,73 @@ function combineControls(controls, allowTermination = true) {
|
|
|
1106
1166
|
}
|
|
1107
1167
|
|
|
1108
1168
|
// packages/execution/src/tool-contract.ts
|
|
1109
|
-
import { copyJson as copyJson7 } from "@earendil-works/chord";
|
|
1169
|
+
import { copyJson as copyJson7 } from "@earendil-works/chord/json";
|
|
1170
|
+
|
|
1171
|
+
// packages/execution/src/tool-namespaces.ts
|
|
1172
|
+
import { isJsonValue } from "@earendil-works/chord/json";
|
|
1173
|
+
function validateJsToolNamespace(tool, namespace) {
|
|
1174
|
+
const key = (value) => typeof value === "string" && /^[a-zA-Z_$][\w$]*$/.test(value);
|
|
1175
|
+
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))))
|
|
1176
|
+
throw new Error(`Tool ${tool} has invalid jsNamespace metadata`);
|
|
1177
|
+
}
|
|
1178
|
+
function jsToolPrompt(contract) {
|
|
1179
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1180
|
+
return [];
|
|
1181
|
+
if (contract.jsNamespace)
|
|
1182
|
+
return [contract.jsNamespace.prompt];
|
|
1183
|
+
return contract.deferLoading ? [] : [contract.prompt];
|
|
1184
|
+
}
|
|
1185
|
+
function namespaceDefinitions(tools) {
|
|
1186
|
+
return tools.flatMap(({ name, jsNamespace }) => jsNamespace ? [{ tool: name, name: jsNamespace.name, methods: jsNamespace.methods }] : []);
|
|
1187
|
+
}
|
|
1188
|
+
var installToolNamespacesSource = `(tools, definitions) => {
|
|
1189
|
+
const namespaces = Object.create(null);
|
|
1190
|
+
const binders = Object.create(null);
|
|
1191
|
+
let active = true;
|
|
1192
|
+
const add = (target, key, value) => {
|
|
1193
|
+
if (Object.hasOwn(target, key)) throw new Error("Duplicate tool namespace member: " + key);
|
|
1194
|
+
target[key] = value;
|
|
1195
|
+
};
|
|
1196
|
+
const object = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1197
|
+
for (const { tool, name, methods } of definitions) {
|
|
1198
|
+
const namespace = namespaces[name] ??= Object.create(null);
|
|
1199
|
+
const bound = binders[name] ??= Object.create(null);
|
|
1200
|
+
for (const { name: method, bind, defaults } of methods) {
|
|
1201
|
+
const invoke = (args, ref) => {
|
|
1202
|
+
if (!active)
|
|
1203
|
+
throw new Error("Tool namespace binding expired; bind the server reference again in this cell");
|
|
1204
|
+
if (args !== undefined && !object(args))
|
|
1205
|
+
throw new TypeError("Tool namespace arguments must be an object");
|
|
1206
|
+
return tools[tool]({ ...args, ...defaults, ...(bind === undefined ? {} : { [bind]: ref }) });
|
|
1207
|
+
};
|
|
1208
|
+
if (bind === undefined) {
|
|
1209
|
+
add(namespace, method, (args) => invoke(args));
|
|
1210
|
+
continue;
|
|
1211
|
+
}
|
|
1212
|
+
if (!Object.hasOwn(bound, bind)) {
|
|
1213
|
+
const members = bound[bind] = Object.create(null);
|
|
1214
|
+
add(namespace, bind, (ref) => {
|
|
1215
|
+
if (!object(ref)) throw new TypeError("Supply a discovered namespace reference");
|
|
1216
|
+
const reference = { ...ref };
|
|
1217
|
+
const result = Object.create(null);
|
|
1218
|
+
for (const [key, call] of Object.entries(members))
|
|
1219
|
+
result[key] = (args) => call(args, reference);
|
|
1220
|
+
return Object.freeze(result);
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
add(bound[bind], method, invoke);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
for (const namespace of Object.values(namespaces)) Object.freeze(namespace);
|
|
1227
|
+
globalThis.toolNamespaces = Object.freeze(namespaces);
|
|
1228
|
+
return () => {
|
|
1229
|
+
active = false;
|
|
1230
|
+
if (globalThis.toolNamespaces === namespaces)
|
|
1231
|
+
globalThis.toolNamespaces = Object.freeze(Object.create(null));
|
|
1232
|
+
};
|
|
1233
|
+
}`;
|
|
1234
|
+
|
|
1235
|
+
// packages/execution/src/tool-contract.ts
|
|
1110
1236
|
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 {}";
|
|
1111
1237
|
function readToolContract(tool) {
|
|
1112
1238
|
const candidate = "executionHints" in tool ? tool.executionHints : undefined;
|
|
@@ -1121,6 +1247,7 @@ function readToolContract(tool) {
|
|
|
1121
1247
|
"yieldTimeMs",
|
|
1122
1248
|
"inputSchema",
|
|
1123
1249
|
"nativeOnly",
|
|
1250
|
+
"nestedHidden",
|
|
1124
1251
|
"discovery"
|
|
1125
1252
|
]);
|
|
1126
1253
|
for (const key of Object.keys(hints))
|
|
@@ -1144,6 +1271,12 @@ function readToolContract(tool) {
|
|
|
1144
1271
|
const nativeOnly = "nativeOnly" in hints ? hints.nativeOnly : undefined;
|
|
1145
1272
|
if (nativeOnly !== undefined && typeof nativeOnly !== "boolean")
|
|
1146
1273
|
throw new Error(`Tool ${tool.name} executionHints.nativeOnly must be a boolean`);
|
|
1274
|
+
const nestedHidden = "nestedHidden" in hints ? hints.nestedHidden : undefined;
|
|
1275
|
+
const jsNamespace = "jsNamespace" in tool ? tool.jsNamespace : undefined;
|
|
1276
|
+
if (jsNamespace !== undefined)
|
|
1277
|
+
validateJsToolNamespace(tool.name, jsNamespace);
|
|
1278
|
+
if (nestedHidden !== undefined && typeof nestedHidden !== "boolean")
|
|
1279
|
+
throw new Error(`Tool ${tool.name} executionHints.nestedHidden must be a boolean`);
|
|
1147
1280
|
const yieldTimeMs = "yieldTimeMs" in hints ? hints.yieldTimeMs : undefined;
|
|
1148
1281
|
if (yieldTimeMs !== undefined && (typeof yieldTimeMs !== "number" || !Number.isSafeInteger(yieldTimeMs) || yieldTimeMs < 0))
|
|
1149
1282
|
throw new Error(`Tool ${tool.name} executionHints.yieldTimeMs must be a non-negative safe integer`);
|
|
@@ -1160,6 +1293,8 @@ function readToolContract(tool) {
|
|
|
1160
1293
|
inputSchema,
|
|
1161
1294
|
deferLoading: typeof deferred === "boolean" ? deferred : true,
|
|
1162
1295
|
nativeOnly: nativeOnly === true,
|
|
1296
|
+
nestedHidden: nestedHidden === true,
|
|
1297
|
+
...jsNamespace === undefined ? {} : { jsNamespace },
|
|
1163
1298
|
...typeof yieldTimeMs === "number" ? { yieldTimeMs } : {},
|
|
1164
1299
|
...outputHint === undefined ? {} : { output: outputHint },
|
|
1165
1300
|
help: [
|
|
@@ -1395,7 +1530,7 @@ function createCellTask(options, nestedTask) {
|
|
|
1395
1530
|
import {
|
|
1396
1531
|
hook,
|
|
1397
1532
|
LiveDoc as LiveDoc4,
|
|
1398
|
-
ToolTask as
|
|
1533
|
+
ToolTask as ToolTask4
|
|
1399
1534
|
} from "@earendil-works/pi-durable";
|
|
1400
1535
|
function forwardingHook() {
|
|
1401
1536
|
let host;
|
|
@@ -1405,7 +1540,7 @@ function forwardingHook() {
|
|
|
1405
1540
|
throw new Error("Create a separate execution component for each Harness");
|
|
1406
1541
|
host = harness;
|
|
1407
1542
|
},
|
|
1408
|
-
hook: hook(
|
|
1543
|
+
hook: hook(ToolTask4, {
|
|
1409
1544
|
async beforeTool(call, api, context) {
|
|
1410
1545
|
const parent = call.parent;
|
|
1411
1546
|
if (parent === undefined || host === undefined)
|
|
@@ -1418,7 +1553,7 @@ function forwardingHook() {
|
|
|
1418
1553
|
if (conversation === undefined)
|
|
1419
1554
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1420
1555
|
await conversation.commit(async (tx) => {
|
|
1421
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1556
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1422
1557
|
return;
|
|
1423
1558
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1424
1559
|
const admission = owner.forwardingCellId === undefined;
|
|
@@ -1445,7 +1580,7 @@ function forwardingHook() {
|
|
|
1445
1580
|
if (conversation === undefined)
|
|
1446
1581
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1447
1582
|
await conversation.commit(async (tx) => {
|
|
1448
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1583
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1449
1584
|
return;
|
|
1450
1585
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1451
1586
|
const cellId = owner.forwardingCellId;
|
|
@@ -1470,6 +1605,7 @@ function forwardingHook() {
|
|
|
1470
1605
|
}
|
|
1471
1606
|
|
|
1472
1607
|
// packages/execution/src/nested.ts
|
|
1608
|
+
import { copyJson as copyJson11 } from "@earendil-works/chord/json";
|
|
1473
1609
|
import {
|
|
1474
1610
|
defineTask as defineTask2
|
|
1475
1611
|
} from "@earendil-works/pi-durable";
|
|
@@ -1478,7 +1614,7 @@ import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
|
1478
1614
|
// packages/execution/src/nested-call.ts
|
|
1479
1615
|
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
1480
1616
|
import {
|
|
1481
|
-
ToolTask as
|
|
1617
|
+
ToolTask as ToolTask5
|
|
1482
1618
|
} from "@earendil-works/pi-durable";
|
|
1483
1619
|
import { compile } from "@earendil-works/pi-schema";
|
|
1484
1620
|
function nestedCall(runtime, input) {
|
|
@@ -1524,7 +1660,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1524
1660
|
async function eachHook(agent, name, api, invoke) {
|
|
1525
1661
|
for (const extension of agent.extensions)
|
|
1526
1662
|
for (const registration of extension.hooks ?? []) {
|
|
1527
|
-
if (registration.task !==
|
|
1663
|
+
if (registration.task !== ToolTask5.definition.name)
|
|
1528
1664
|
continue;
|
|
1529
1665
|
const handler = registration.handlers[name];
|
|
1530
1666
|
if (handler === undefined)
|
|
@@ -1539,8 +1675,120 @@ async function eachHook(agent, name, api, invoke) {
|
|
|
1539
1675
|
}
|
|
1540
1676
|
}
|
|
1541
1677
|
|
|
1678
|
+
// packages/execution/src/nested-park.ts
|
|
1679
|
+
import {
|
|
1680
|
+
withCancel,
|
|
1681
|
+
withoutAbortSignal as withoutAbortSignal3
|
|
1682
|
+
} from "@earendil-works/chord/context";
|
|
1683
|
+
import { copyJson as copyJson9 } from "@earendil-works/chord/json";
|
|
1684
|
+
var resultFields = [
|
|
1685
|
+
"output",
|
|
1686
|
+
"structuredOutput",
|
|
1687
|
+
"isError",
|
|
1688
|
+
"details",
|
|
1689
|
+
"diagnostics",
|
|
1690
|
+
"usage",
|
|
1691
|
+
"control"
|
|
1692
|
+
];
|
|
1693
|
+
function sleepState(checkpoint, { until, signals }) {
|
|
1694
|
+
return {
|
|
1695
|
+
status: "sleeping",
|
|
1696
|
+
checkpoint,
|
|
1697
|
+
...until === undefined ? {} : { until },
|
|
1698
|
+
...signals === undefined ? {} : { signals }
|
|
1699
|
+
};
|
|
1700
|
+
}
|
|
1701
|
+
async function parkGate(runtime, sleep, context) {
|
|
1702
|
+
try {
|
|
1703
|
+
await runtime.commit(() => sleepState({ phase: "call" }, sleep), context);
|
|
1704
|
+
} catch (error) {
|
|
1705
|
+
runtime.signal.throwIfAborted();
|
|
1706
|
+
return errorText(error);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
async function parkTool(runtime, invocation, tool, args, park, context) {
|
|
1710
|
+
if (park.kind !== "sleep" && park.kind !== "wait")
|
|
1711
|
+
return "Unknown park kind";
|
|
1712
|
+
const mixed = resultFields.find((field) => (field in park));
|
|
1713
|
+
if (mixed !== undefined)
|
|
1714
|
+
return `A park cannot carry the result field ${mixed}`;
|
|
1715
|
+
const intent = {
|
|
1716
|
+
arguments: args,
|
|
1717
|
+
replay: tool.replay ?? "unsafe",
|
|
1718
|
+
...park.checkpoint === undefined ? {} : { checkpoint: copyJson9(park.checkpoint) }
|
|
1719
|
+
};
|
|
1720
|
+
const { progress, pending } = await invocation.park();
|
|
1721
|
+
try {
|
|
1722
|
+
await runtime.commit(async (tx) => {
|
|
1723
|
+
if (park.kind === "wait") {
|
|
1724
|
+
for (const id of park.on)
|
|
1725
|
+
if ((await tx.task(id))?.owner !== runtime.taskId)
|
|
1726
|
+
throw new Error(`Task ${id} is not owned by the call`);
|
|
1727
|
+
}
|
|
1728
|
+
const checkpoint = { ...intent, phase: "parked" };
|
|
1729
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1730
|
+
doc.result = progress;
|
|
1731
|
+
doc.droppedBytes = 0;
|
|
1732
|
+
doc.droppedLines = 0;
|
|
1733
|
+
await forwardInvocationResult(tx, runtime.taskId, progress, "running");
|
|
1734
|
+
return park.kind === "sleep" ? sleepState(checkpoint, park) : { status: "waiting", checkpoint, on: park.on, policy: park.policy };
|
|
1735
|
+
}, context);
|
|
1736
|
+
} catch (error) {
|
|
1737
|
+
for (const waiter of pending)
|
|
1738
|
+
waiter.reject(error);
|
|
1739
|
+
const detached = withoutAbortSignal3(context);
|
|
1740
|
+
const record = await runtime.getTask(runtime.taskId, detached);
|
|
1741
|
+
if (record?.abortRequested) {
|
|
1742
|
+
const watch = await runtime.watchDoc(InvocationDoc, runtime.taskId, detached);
|
|
1743
|
+
if (watch !== undefined) {
|
|
1744
|
+
const cleanup = withCancel(detached);
|
|
1745
|
+
watch.closed.then((end) => cleanup.cancel(end.kind === "failed" ? end.error : undefined));
|
|
1746
|
+
try {
|
|
1747
|
+
await cancelParkedTool(runtime, tool, intent, cleanup.context);
|
|
1748
|
+
} finally {
|
|
1749
|
+
await watch.stop();
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
throw error;
|
|
1753
|
+
}
|
|
1754
|
+
runtime.signal.throwIfAborted();
|
|
1755
|
+
return errorText(error);
|
|
1756
|
+
}
|
|
1757
|
+
for (const waiter of pending)
|
|
1758
|
+
waiter.resolve();
|
|
1759
|
+
}
|
|
1760
|
+
async function cancelParkedTool(runtime, tool, intent, context) {
|
|
1761
|
+
if (!tool) {
|
|
1762
|
+
runtime.report(new Error("The parked tool is no longer registered; cleanup could not run"));
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
if (tool.cancel === undefined)
|
|
1766
|
+
return;
|
|
1767
|
+
const cleanup = createInvocation(runtime, tool, context, {
|
|
1768
|
+
checkpoint: intent.checkpoint,
|
|
1769
|
+
wake: undefined
|
|
1770
|
+
});
|
|
1771
|
+
const refuseWork = () => {
|
|
1772
|
+
throw new Error("A tool cannot start tasks or nested calls during cancellation");
|
|
1773
|
+
};
|
|
1774
|
+
let reason = new Error("Tool cancellation finished");
|
|
1775
|
+
try {
|
|
1776
|
+
const api = await cleanup.executionApi();
|
|
1777
|
+
await tool.cancel(intent.arguments, {
|
|
1778
|
+
...api,
|
|
1779
|
+
createTask: refuseWork,
|
|
1780
|
+
executeTool: refuseWork
|
|
1781
|
+
}, context);
|
|
1782
|
+
} catch (error) {
|
|
1783
|
+
reason = error;
|
|
1784
|
+
runtime.report(error);
|
|
1785
|
+
} finally {
|
|
1786
|
+
await cleanup.stop(reason);
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1542
1790
|
// packages/execution/src/nested-result.ts
|
|
1543
|
-
import { copyJson as
|
|
1791
|
+
import { copyJson as copyJson10 } from "@earendil-works/chord/json";
|
|
1544
1792
|
import {
|
|
1545
1793
|
defineEntry,
|
|
1546
1794
|
UsageDoc
|
|
@@ -1588,11 +1836,11 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1588
1836
|
taskId: runtime.taskId,
|
|
1589
1837
|
callId: call.id,
|
|
1590
1838
|
name: call.name,
|
|
1591
|
-
arguments:
|
|
1839
|
+
arguments: copyJson10(call.arguments),
|
|
1592
1840
|
result: stored,
|
|
1593
1841
|
...current.state.checkpoint.phase === "execute" ? {
|
|
1594
1842
|
intent: {
|
|
1595
|
-
arguments:
|
|
1843
|
+
arguments: copyJson10(current.state.checkpoint.arguments),
|
|
1596
1844
|
replay: current.state.checkpoint.replay
|
|
1597
1845
|
}
|
|
1598
1846
|
} : {}
|
|
@@ -1601,7 +1849,7 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1601
1849
|
if (result.usage !== undefined) {
|
|
1602
1850
|
const totals = (await tx.doc(UsageDoc, runtime.conversationId)).tools;
|
|
1603
1851
|
const previous = Object.hasOwn(totals, call.name) ? totals[call.name] : undefined;
|
|
1604
|
-
totals[call.name] =
|
|
1852
|
+
totals[call.name] = copyJson10(previous === undefined ? result.usage : sumUsage(previous, result.usage), { omitUndefinedProperties: true });
|
|
1605
1853
|
}
|
|
1606
1854
|
const outcome = ending === "completed" ? { status: ending, result: stored } : ending === "aborted" ? { status: ending, result: stored } : {
|
|
1607
1855
|
status: ending,
|
|
@@ -1638,7 +1886,11 @@ function sumUsage(previous, added) {
|
|
|
1638
1886
|
function createNestedToolTask(name) {
|
|
1639
1887
|
return defineTask2({
|
|
1640
1888
|
name,
|
|
1641
|
-
version:
|
|
1889
|
+
version: 2,
|
|
1890
|
+
migrate: (input, checkpoint) => ({
|
|
1891
|
+
input,
|
|
1892
|
+
checkpoint
|
|
1893
|
+
}),
|
|
1642
1894
|
initial: () => ({ phase: "call" }),
|
|
1643
1895
|
phases: {
|
|
1644
1896
|
call: async (task, runtime, context) => {
|
|
@@ -1673,13 +1925,16 @@ function createNestedToolTask(name) {
|
|
|
1673
1925
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1674
1926
|
}
|
|
1675
1927
|
let block;
|
|
1928
|
+
let sleep;
|
|
1676
1929
|
await eachHook(agent, "beforeTool", runtime, async (handler) => {
|
|
1677
|
-
if (block !== undefined)
|
|
1930
|
+
if (block !== undefined || sleep !== undefined)
|
|
1678
1931
|
return;
|
|
1679
1932
|
try {
|
|
1680
1933
|
const decision = await handler({ ...call, arguments: args }, runtime, context);
|
|
1681
1934
|
if (decision?.block !== undefined)
|
|
1682
1935
|
block = decision.block;
|
|
1936
|
+
else if (decision?.sleep !== undefined)
|
|
1937
|
+
sleep = decision.sleep;
|
|
1683
1938
|
else if (decision?.arguments !== undefined)
|
|
1684
1939
|
args = decision.arguments;
|
|
1685
1940
|
} catch (error) {
|
|
@@ -1690,71 +1945,122 @@ function createNestedToolTask(name) {
|
|
|
1690
1945
|
});
|
|
1691
1946
|
if (block !== undefined)
|
|
1692
1947
|
return reject(runtime, call, executionError("blocked", `Tool call blocked: ${block}`), agent, tool, context);
|
|
1948
|
+
if (sleep !== undefined) {
|
|
1949
|
+
const reason = await parkGate(runtime, sleep, context);
|
|
1950
|
+
if (reason === undefined)
|
|
1951
|
+
return;
|
|
1952
|
+
return reject(runtime, call, executionError("invalid_park", reason), agent, tool, context);
|
|
1953
|
+
}
|
|
1693
1954
|
try {
|
|
1694
|
-
args = checked(tool, call, args)
|
|
1955
|
+
args = copyJson11(checked(tool, call, args), {
|
|
1956
|
+
omitUndefinedProperties: true
|
|
1957
|
+
});
|
|
1695
1958
|
} catch (error) {
|
|
1696
1959
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1697
1960
|
}
|
|
1698
|
-
await runtime
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
callId: call.id,
|
|
1702
|
-
parentCallId: call.parent?.callId ?? `nested-${runtime.taskId}`,
|
|
1703
|
-
name: call.name,
|
|
1704
|
-
arguments: args,
|
|
1705
|
-
status: "running",
|
|
1706
|
-
result: null,
|
|
1707
|
-
omittedFields: []
|
|
1708
|
-
});
|
|
1709
|
-
return {
|
|
1710
|
-
status: "running",
|
|
1711
|
-
checkpoint: {
|
|
1712
|
-
phase: "execute",
|
|
1713
|
-
arguments: args,
|
|
1714
|
-
replay: tool.replay ?? "unsafe"
|
|
1715
|
-
}
|
|
1716
|
-
};
|
|
1961
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1962
|
+
arguments: args,
|
|
1963
|
+
replay: tool.replay ?? "unsafe"
|
|
1717
1964
|
}, context);
|
|
1718
|
-
|
|
1965
|
+
},
|
|
1966
|
+
parked: async (task, runtime, context) => {
|
|
1967
|
+
const intent = task.state.checkpoint;
|
|
1968
|
+
const call = nestedCall(runtime, task.input);
|
|
1969
|
+
call.arguments = intent.arguments;
|
|
1970
|
+
const agent = await runtime.agent(context);
|
|
1971
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1972
|
+
if (tool === undefined || readToolContract(tool).nativeOnly)
|
|
1973
|
+
return interrupted(runtime, call, "interrupted", `Parked tool ${call.name} is no longer available and could not be resumed`, context);
|
|
1974
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1975
|
+
...intent,
|
|
1976
|
+
replay: tool.replay ?? "unsafe"
|
|
1977
|
+
}, context, task.state.wake);
|
|
1719
1978
|
},
|
|
1720
1979
|
execute: async (task, runtime, context) => {
|
|
1980
|
+
const intent = task.state.checkpoint;
|
|
1721
1981
|
const call = nestedCall(runtime, task.input);
|
|
1722
1982
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1723
1983
|
call.arguments = task.state.checkpoint.arguments;
|
|
1724
1984
|
const agent = await runtime.agent(context);
|
|
1725
1985
|
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1726
|
-
if (
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1986
|
+
if (intent.replay === "safe" && tool?.replay === "safe" && !readToolContract(tool).nativeOnly) {
|
|
1987
|
+
if (intent.resume === undefined)
|
|
1988
|
+
await runtime.commit(async (tx) => {
|
|
1989
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1990
|
+
doc.result = {};
|
|
1991
|
+
doc.droppedBytes = 0;
|
|
1992
|
+
doc.droppedLines = 0;
|
|
1993
|
+
return;
|
|
1994
|
+
}, context);
|
|
1995
|
+
return run(runtime, call, tool, intent, agent, context, intent.resume?.wake);
|
|
1735
1996
|
}
|
|
1997
|
+
if (intent.resume !== undefined)
|
|
1998
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1736
1999
|
await interrupted(runtime, call, "interrupted", `Tool ${call.name} was interrupted and may have partially run`, context);
|
|
1737
2000
|
}
|
|
1738
2001
|
},
|
|
1739
2002
|
abort: async (task, runtime, context) => {
|
|
1740
2003
|
const call = nestedCall(runtime, task.input);
|
|
2004
|
+
const intent = task.state.checkpoint;
|
|
2005
|
+
if (intent.phase === "parked" || intent.phase === "execute" && intent.resume !== undefined) {
|
|
2006
|
+
const agent = await runtime.agent(context);
|
|
2007
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
2008
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
2009
|
+
}
|
|
1741
2010
|
await interrupted(runtime, call, "aborted", `Tool ${call.name} was aborted${task.state.checkpoint.phase === "execute" ? " and may have partially run" : ""}`, context);
|
|
1742
2011
|
}
|
|
1743
2012
|
});
|
|
1744
2013
|
}
|
|
2014
|
+
async function startAttempt(runtime, call, tool, agent, intent, context, wake) {
|
|
2015
|
+
await runtime.commit(async (tx) => {
|
|
2016
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
2017
|
+
await forwardCall(tx, doc.forwardingCellId, {
|
|
2018
|
+
callId: call.id,
|
|
2019
|
+
parentCallId: call.parent?.callId ?? call.id,
|
|
2020
|
+
name: call.name,
|
|
2021
|
+
arguments: intent.arguments,
|
|
2022
|
+
status: "running",
|
|
2023
|
+
result: null,
|
|
2024
|
+
omittedFields: []
|
|
2025
|
+
});
|
|
2026
|
+
return {
|
|
2027
|
+
status: "running",
|
|
2028
|
+
checkpoint: {
|
|
2029
|
+
...intent,
|
|
2030
|
+
phase: "execute",
|
|
2031
|
+
...wake === undefined ? {} : { resume: { wake } }
|
|
2032
|
+
}
|
|
2033
|
+
};
|
|
2034
|
+
}, context);
|
|
2035
|
+
await run(runtime, call, tool, intent, agent, context, wake);
|
|
2036
|
+
}
|
|
1745
2037
|
async function reject(runtime, call, result, agent, tool, context) {
|
|
1746
2038
|
await eachHook(agent, "afterTool", runtime, async (handler) => {
|
|
1747
2039
|
result = await handler(call, result, runtime, context) ?? result;
|
|
1748
2040
|
});
|
|
1749
2041
|
await settle(runtime, call, finishResult(tool, call.name, result), "completed", context);
|
|
1750
2042
|
}
|
|
1751
|
-
async function run(runtime, call, tool,
|
|
2043
|
+
async function run(runtime, call, tool, intent, agent, context, wake) {
|
|
1752
2044
|
let invocation;
|
|
1753
2045
|
let result;
|
|
1754
2046
|
let ending = "completed";
|
|
1755
2047
|
try {
|
|
1756
|
-
|
|
1757
|
-
|
|
2048
|
+
const previous = wake === undefined ? undefined : await runtime.snapshot(InvocationDoc, runtime.taskId, context);
|
|
2049
|
+
invocation = createInvocation(runtime, tool, context, {
|
|
2050
|
+
wake,
|
|
2051
|
+
checkpoint: intent.checkpoint,
|
|
2052
|
+
...previous === undefined ? {} : { progress: previous.result }
|
|
2053
|
+
});
|
|
2054
|
+
const returned = await tool.execute(intent.arguments, await invocation.executionApi(), context);
|
|
2055
|
+
if (returned.kind === undefined)
|
|
2056
|
+
result = returned;
|
|
2057
|
+
else {
|
|
2058
|
+
const reason = await parkTool(runtime, invocation, tool, intent.arguments, returned, context);
|
|
2059
|
+
if (reason === undefined)
|
|
2060
|
+
return;
|
|
2061
|
+
result = executionError("invalid_park", reason);
|
|
2062
|
+
ending = "failed";
|
|
2063
|
+
}
|
|
1758
2064
|
} catch (error) {
|
|
1759
2065
|
if (runtime.signal.aborted) {
|
|
1760
2066
|
await invocation?.stop(error);
|
|
@@ -1855,9 +2161,11 @@ function providerProjection(surfaceTools) {
|
|
|
1855
2161
|
const conversation = await host.conversation(api.conversationId, context);
|
|
1856
2162
|
if (conversation === undefined)
|
|
1857
2163
|
throw new Error(`Conversation ${api.conversationId} is unavailable`);
|
|
1858
|
-
for (const tool of (await conversation.agent(context)).tools)
|
|
1859
|
-
|
|
2164
|
+
for (const tool of (await conversation.agent(context)).tools) {
|
|
2165
|
+
const contract = readToolContract(tool);
|
|
2166
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1860
2167
|
visible.add(tool.name);
|
|
2168
|
+
}
|
|
1861
2169
|
}
|
|
1862
2170
|
return { messages: projectToolSchemas(request.messages, visible) };
|
|
1863
2171
|
}
|
|
@@ -1866,7 +2174,7 @@ function providerProjection(surfaceTools) {
|
|
|
1866
2174
|
}
|
|
1867
2175
|
|
|
1868
2176
|
// packages/execution/src/wait-session.ts
|
|
1869
|
-
import { withoutAbortSignal as
|
|
2177
|
+
import { withoutAbortSignal as withoutAbortSignal4 } from "@earendil-works/chord/context";
|
|
1870
2178
|
|
|
1871
2179
|
// packages/execution/src/tool-value.ts
|
|
1872
2180
|
class NestedToolError extends Error {
|
|
@@ -1937,7 +2245,7 @@ async function recoverWaitSession(cellId, yieldTimeMs, maxTokens, api, context,
|
|
|
1937
2245
|
}, context);
|
|
1938
2246
|
let cancelling;
|
|
1939
2247
|
const abort = () => {
|
|
1940
|
-
cancelling ??= cancelTask(id,
|
|
2248
|
+
cancelling ??= cancelTask(id, withoutAbortSignal4(context));
|
|
1941
2249
|
cancelling.catch(() => {});
|
|
1942
2250
|
};
|
|
1943
2251
|
context.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
@@ -2021,7 +2329,7 @@ function createCellCoordinator(options) {
|
|
|
2021
2329
|
if (driver.closed)
|
|
2022
2330
|
throw new Error("Execution host is closed");
|
|
2023
2331
|
assertSingleExecutionMode(await api.agent(context));
|
|
2024
|
-
const copied =
|
|
2332
|
+
const copied = copyJson12(input);
|
|
2025
2333
|
return api.commit(async (tx) => {
|
|
2026
2334
|
const isolatedLimit = options.isolatedConcurrency ?? 0;
|
|
2027
2335
|
const isolated = isolatedLimit > 0 && input["isolated"] === true;
|
|
@@ -2101,7 +2409,7 @@ function createCellCoordinator(options) {
|
|
|
2101
2409
|
if (yielding.consume()) {
|
|
2102
2410
|
return initial;
|
|
2103
2411
|
}
|
|
2104
|
-
const wait =
|
|
2412
|
+
const wait = withCancel2(context);
|
|
2105
2413
|
const timeout = new Error("Cell observation yielded");
|
|
2106
2414
|
const timer = setTimeout(() => wait.cancel(timeout), Math.min(yieldTimeMs, 2147483647));
|
|
2107
2415
|
try {
|
|
@@ -3299,7 +3607,7 @@ function notebookExecStartupNotice() {
|
|
|
3299
3607
|
|
|
3300
3608
|
// packages/notebook/src/engine.ts
|
|
3301
3609
|
import { randomUUID as randomUUID16 } from "node:crypto";
|
|
3302
|
-
import { copyJson as
|
|
3610
|
+
import { copyJson as copyJson13 } from "@earendil-works/chord/json";
|
|
3303
3611
|
import {
|
|
3304
3612
|
defineDoc as defineDoc3
|
|
3305
3613
|
} from "@earendil-works/pi-durable";
|
|
@@ -5066,7 +5374,7 @@ class NotebookExecutionRuntime {
|
|
|
5066
5374
|
this.settlement.reportJournalFailure(error, "start", cell);
|
|
5067
5375
|
}
|
|
5068
5376
|
}
|
|
5069
|
-
const metadata = tools.map((tool) => ({
|
|
5377
|
+
const metadata = tools.filter((tool) => !tool.hidden).map((tool) => ({
|
|
5070
5378
|
name: tool.name,
|
|
5071
5379
|
description: tool.help ?? tool.usage + `
|
|
5072
5380
|
` + tool.description
|
|
@@ -5075,7 +5383,7 @@ class NotebookExecutionRuntime {
|
|
|
5075
5383
|
const outputHints = Object.fromEntries(tools.flatMap((tool) => ("textOutput" in tool) && tool.textOutput !== undefined ? [[tool.name, tool.textOutput]] : []));
|
|
5076
5384
|
const wrapped = [
|
|
5077
5385
|
`if (typeof globalThis.__piNotebook?.begin !== "function") throw new Error("Notebook runtime bootstrap unavailable: __piNotebook.begin");`,
|
|
5078
|
-
`await globalThis.__piNotebook.begin(${JSON.stringify(id)}, ${JSON.stringify(metadata)}, JSON.parse(${JSON.stringify(JSON.stringify(toolNames))}), JSON.parse(${JSON.stringify(JSON.stringify(outputHints))}));`,
|
|
5386
|
+
`await globalThis.__piNotebook.begin(${JSON.stringify(id)}, ${JSON.stringify(metadata)}, JSON.parse(${JSON.stringify(JSON.stringify(toolNames))}), JSON.parse(${JSON.stringify(JSON.stringify(outputHints))}), ${JSON.stringify(namespaceDefinitions(tools))});`,
|
|
5079
5387
|
code,
|
|
5080
5388
|
`if (typeof globalThis.__piNotebook?.flush !== "function") throw new Error("Notebook runtime bootstrap unavailable: __piNotebook.flush");`,
|
|
5081
5389
|
`await globalThis.__piNotebook.flush(${JSON.stringify(id)});`,
|
|
@@ -9068,8 +9376,10 @@ function notebookBootstrapSource(origin, token, exitToken, cwd) {
|
|
|
9068
9376
|
};
|
|
9069
9377
|
},
|
|
9070
9378
|
});
|
|
9379
|
+
const __installToolNamespaces = ${installToolNamespacesSource};
|
|
9380
|
+
let __disposeToolNamespaces = __installToolNamespaces(__tools, []);
|
|
9071
9381
|
const __runtime = {
|
|
9072
|
-
async begin(cellId, tools, toolNames, toolOutputHints
|
|
9382
|
+
async begin(cellId, tools, toolNames, toolOutputHints, toolNamespaceDefinitions) {
|
|
9073
9383
|
if (__state.memoryTimer !== undefined) clearInterval(__state.memoryTimer);
|
|
9074
9384
|
__state.cellId = cellId;
|
|
9075
9385
|
__state.toolNames = toolNames;
|
|
@@ -9082,6 +9392,8 @@ function notebookBootstrapSource(origin, token, exitToken, cwd) {
|
|
|
9082
9392
|
__state.outputTruncated = false;
|
|
9083
9393
|
globalThis.tools = __tools;
|
|
9084
9394
|
globalThis.ALL_TOOLS = tools;
|
|
9395
|
+
__disposeToolNamespaces();
|
|
9396
|
+
__disposeToolNamespaces = __installToolNamespaces(__tools, toolNamespaceDefinitions);
|
|
9085
9397
|
__state.cellGlobals = new Set(Object.getOwnPropertyNames(globalThis));
|
|
9086
9398
|
await __reportMemory(cellId);
|
|
9087
9399
|
__state.memoryTimer = setInterval(() => void __reportMemory(cellId).catch(() => undefined), 1000);
|
|
@@ -9109,6 +9421,7 @@ function notebookBootstrapSource(origin, token, exitToken, cwd) {
|
|
|
9109
9421
|
if (__state.memoryTimer !== undefined) clearInterval(__state.memoryTimer);
|
|
9110
9422
|
__state.memoryTimer = undefined;
|
|
9111
9423
|
__state.cellId = null;
|
|
9424
|
+
__disposeToolNamespaces();
|
|
9112
9425
|
},
|
|
9113
9426
|
projectBindings() {
|
|
9114
9427
|
return [...__state.projectBindings].sort();
|
|
@@ -10503,6 +10816,7 @@ var HOST_BINDINGS = new Set([
|
|
|
10503
10816
|
"notify",
|
|
10504
10817
|
"store",
|
|
10505
10818
|
"text",
|
|
10819
|
+
"toolNamespaces",
|
|
10506
10820
|
"tools",
|
|
10507
10821
|
"yield_control"
|
|
10508
10822
|
]);
|
|
@@ -10929,13 +11243,15 @@ function createNotebookEngine(options) {
|
|
|
10929
11243
|
const contract = readToolContract(registration);
|
|
10930
11244
|
return {
|
|
10931
11245
|
name: registration.name,
|
|
11246
|
+
hidden: contract.nestedHidden || contract.jsNamespace !== undefined,
|
|
11247
|
+
...contract.nestedHidden || contract.jsNamespace === undefined ? {} : { jsNamespace: contract.jsNamespace },
|
|
10932
11248
|
description: contract.description,
|
|
10933
11249
|
usage: contract.usage,
|
|
10934
11250
|
help: contract.help,
|
|
10935
11251
|
...registration.name === "exec_command" || registration.name === "write_stdin" ? { textOutput: "plain-command" } : {},
|
|
10936
11252
|
async invoke(input, _context, signal) {
|
|
10937
11253
|
signal.throwIfAborted();
|
|
10938
|
-
const args =
|
|
11254
|
+
const args = copyJson13(input === undefined ? null : input, {
|
|
10939
11255
|
omitUndefinedProperties: true
|
|
10940
11256
|
});
|
|
10941
11257
|
const invoke = api.tools[registration.name];
|
|
@@ -11088,7 +11404,7 @@ function parseNotebookRequest(input) {
|
|
|
11088
11404
|
}
|
|
11089
11405
|
|
|
11090
11406
|
// packages/notebook/src/tool-observation.ts
|
|
11091
|
-
import { copyJson as
|
|
11407
|
+
import { copyJson as copyJson14 } from "@earendil-works/chord/json";
|
|
11092
11408
|
import {
|
|
11093
11409
|
defineDocFamily as defineDocFamily2
|
|
11094
11410
|
} from "@earendil-works/pi-durable";
|
|
@@ -11143,7 +11459,7 @@ async function observeNotebookCell(observation, maxTokens, api, context) {
|
|
|
11143
11459
|
cell_id: String(observation.cellId),
|
|
11144
11460
|
status: observation.status,
|
|
11145
11461
|
...observation.forwardedToolCalls === undefined ? {} : {
|
|
11146
|
-
forwardedToolCalls:
|
|
11462
|
+
forwardedToolCalls: copyJson14(observation.forwardedToolCalls, {
|
|
11147
11463
|
omitUndefinedProperties: true
|
|
11148
11464
|
})
|
|
11149
11465
|
},
|
|
@@ -11657,15 +11973,15 @@ function createNotebookMode(options) {
|
|
|
11657
11973
|
};
|
|
11658
11974
|
return {
|
|
11659
11975
|
output: [{ type: "text", text: result.message }],
|
|
11660
|
-
details:
|
|
11661
|
-
structuredOutput:
|
|
11976
|
+
details: copyJson15(result.details),
|
|
11977
|
+
structuredOutput: copyJson15(result)
|
|
11662
11978
|
};
|
|
11663
11979
|
}
|
|
11664
11980
|
const result = await engine.control(request, api, context);
|
|
11665
11981
|
return {
|
|
11666
11982
|
output: [{ type: "text", text: result.message }],
|
|
11667
|
-
details:
|
|
11668
|
-
structuredOutput:
|
|
11983
|
+
details: copyJson15(result.details, { omitUndefinedProperties: true }),
|
|
11984
|
+
structuredOutput: copyJson15(result, { omitUndefinedProperties: true })
|
|
11669
11985
|
};
|
|
11670
11986
|
}
|
|
11671
11987
|
});
|
|
@@ -11678,7 +11994,7 @@ function createNotebookMode(options) {
|
|
|
11678
11994
|
...notebook,
|
|
11679
11995
|
executionHints: {
|
|
11680
11996
|
discovery: "help",
|
|
11681
|
-
inputSchema:
|
|
11997
|
+
inputSchema: copyJson15(JSON.parse(JSON.stringify(Type4.Union([NOTEBOOK_PARAMETERS, NOTEBOOK_ACTION_PARAMETERS]))))
|
|
11682
11998
|
}
|
|
11683
11999
|
},
|
|
11684
12000
|
...shell.tools
|
|
@@ -11686,7 +12002,7 @@ function createNotebookMode(options) {
|
|
|
11686
12002
|
sections: [
|
|
11687
12003
|
section("notebook", (input) => {
|
|
11688
12004
|
const base = 'exec: TypeScript, top-level await and globals persist. Run normal exec cells sequentially. A leading // @exec: {"isolated":true} runs a separate kernel from committed state, up to four at once; completion saves a candidate without changing live bindings. Use wait to continue or terminate. Bare values are discarded. Use text(value), image(value), generatedImage(value), notify(value), store(key,value), load(key), exit() and yield_control(). ALL_TOOLS lists selected tool contracts. Reuse matching retained globals. Keep one-offs block-local. Retain only what you plan to reuse, and clean up when the task ends. Keep retained helpers self-contained; recreate imports, closures and live handles after restart. Keep canonical project artifacts in files and carry shell state through files or arguments. Ask before adding npm imports inside this Deno notebook, then use exact-version npm: specifiers. This rule does not govern repository dependencies. Inside exec tools.notebook supports status without query, list and diagnostics. Run other notebook actions after exec returns.';
|
|
11689
|
-
const promoted = input.agent.callable.filter((tool) => !["exec", "wait", "notebook"].includes(tool.name)).map(readToolContract).
|
|
12005
|
+
const promoted = input.agent.callable.filter((tool) => !["exec", "wait", "notebook"].includes(tool.name)).map(readToolContract).flatMap(jsToolPrompt);
|
|
11690
12006
|
return [base, nestedToolDiscoveryGuidance, ...promoted].join(`
|
|
11691
12007
|
`);
|
|
11692
12008
|
})
|