@howaboua/pi-durable-js-notebook 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/NOTICE +1 -1
- package/README.md +1 -1
- package/dist/activation.js +375 -119
- 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/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/index.js +375 -119
- package/dist/js-notebook/src/engine.d.ts +1 -1
- package/dist/js-notebook/src/index.d.ts +6 -3
- package/dist/js-notebook/src/runtime-contract.d.ts +1 -0
- package/dist/notebook/src/runtime-contract.d.ts +3 -0
- package/dist/notebook/src/tool-observation.d.ts +5 -4
- package/package.json +10 -9
package/dist/activation.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Type as Type5 } from "typebox";
|
|
|
9
9
|
import { Check as Check3 } from "typebox/value";
|
|
10
10
|
|
|
11
11
|
// packages/js-notebook/src/index.ts
|
|
12
|
-
import { copyJson as
|
|
12
|
+
import { copyJson as copyJson15 } from "@earendil-works/chord/json";
|
|
13
13
|
import {
|
|
14
14
|
defineExtension as defineExtension3,
|
|
15
15
|
defineTool as defineTool3,
|
|
@@ -80,8 +80,8 @@ function parseInteger(value, name, minimum = 0, maximum = Number.MAX_SAFE_INTEGE
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
// packages/execution/src/cells.ts
|
|
83
|
-
import {
|
|
84
|
-
import {
|
|
83
|
+
import { withCancel as withCancel2 } from "@earendil-works/chord/context";
|
|
84
|
+
import { copyJson as copyJson12 } from "@earendil-works/chord/json";
|
|
85
85
|
import {
|
|
86
86
|
defineExtension
|
|
87
87
|
} from "@earendil-works/pi-durable";
|
|
@@ -92,7 +92,7 @@ import {
|
|
|
92
92
|
} from "@earendil-works/pi-durable";
|
|
93
93
|
|
|
94
94
|
// packages/execution/src/forwarding.ts
|
|
95
|
-
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
95
|
+
import { copyJson as copyJson2 } from "@earendil-works/chord/json";
|
|
96
96
|
import {
|
|
97
97
|
LiveDoc,
|
|
98
98
|
NestedCallDoc,
|
|
@@ -102,7 +102,7 @@ import {
|
|
|
102
102
|
// packages/execution/src/invocation-state.ts
|
|
103
103
|
import {
|
|
104
104
|
copyJson
|
|
105
|
-
} from "@earendil-works/chord";
|
|
105
|
+
} from "@earendil-works/chord/json";
|
|
106
106
|
import {
|
|
107
107
|
defineDoc
|
|
108
108
|
} from "@earendil-works/pi-durable";
|
|
@@ -579,7 +579,7 @@ async function observeCell(kind, id, api, context, budget) {
|
|
|
579
579
|
}
|
|
580
580
|
|
|
581
581
|
// packages/execution/src/cell-progress.ts
|
|
582
|
-
import { copyJson as copyJson3 } from "@earendil-works/chord";
|
|
582
|
+
import { copyJson as copyJson3 } from "@earendil-works/chord/json";
|
|
583
583
|
import {
|
|
584
584
|
LiveDoc as LiveDoc2
|
|
585
585
|
} from "@earendil-works/pi-durable";
|
|
@@ -606,8 +606,14 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
606
606
|
return work;
|
|
607
607
|
};
|
|
608
608
|
const live = await api.watchDoc(LiveDoc2, api.conversationId, context);
|
|
609
|
-
cell.start((
|
|
610
|
-
|
|
609
|
+
cell.start((call) => own(async () => {
|
|
610
|
+
if (call.kind === "failed")
|
|
611
|
+
throw call.error;
|
|
612
|
+
await publish(call.value);
|
|
613
|
+
}));
|
|
614
|
+
live?.start((call) => own(async () => {
|
|
615
|
+
if (call.kind === "failed")
|
|
616
|
+
throw call.error;
|
|
611
617
|
await api.commit(async (tx) => {
|
|
612
618
|
const record = await tx.task(id);
|
|
613
619
|
if (record === undefined || record.state.status === "terminal")
|
|
@@ -627,43 +633,70 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
627
633
|
const endings = await Promise.all([cell.stop(), live?.stop()]);
|
|
628
634
|
await Promise.all([...pending]);
|
|
629
635
|
for (const ending of endings)
|
|
630
|
-
if (ending?.
|
|
636
|
+
if (ending?.kind === "listener_error" || ending?.kind === "failed")
|
|
631
637
|
throw ending.error;
|
|
632
638
|
};
|
|
633
639
|
}
|
|
634
640
|
|
|
635
641
|
// packages/execution/src/cell-task.ts
|
|
636
|
-
import { copyJson as copyJson8 } from "@earendil-works/chord";
|
|
637
642
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
643
|
+
import { copyJson as copyJson8 } from "@earendil-works/chord/json";
|
|
638
644
|
import {
|
|
639
645
|
defineTask
|
|
640
646
|
} from "@earendil-works/pi-durable";
|
|
641
647
|
|
|
642
648
|
// packages/execution/src/cell-tools.ts
|
|
643
|
-
import { copyJson as copyJson6 } from "@earendil-works/chord";
|
|
644
649
|
import { withoutAbortSignal as withoutAbortSignal2 } from "@earendil-works/chord/context";
|
|
650
|
+
import { copyJson as copyJson6 } from "@earendil-works/chord/json";
|
|
645
651
|
|
|
646
652
|
// packages/execution/src/invocation.ts
|
|
647
|
-
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
648
653
|
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
654
|
+
import { copyJson as copyJson5 } from "@earendil-works/chord/json";
|
|
655
|
+
import {
|
|
656
|
+
NestedCallDoc as NestedCallDoc3
|
|
657
|
+
} from "@earendil-works/pi-durable";
|
|
658
|
+
import { equal } from "@earendil-works/pi-schema";
|
|
649
659
|
|
|
650
660
|
// packages/execution/src/native-tools.ts
|
|
651
|
-
import {
|
|
652
|
-
|
|
661
|
+
import {
|
|
662
|
+
withoutAbortSignal
|
|
663
|
+
} from "@earendil-works/chord/context";
|
|
664
|
+
import { copyJson as copyJson4 } from "@earendil-works/chord/json";
|
|
653
665
|
import {
|
|
654
666
|
LiveDoc as LiveDoc3,
|
|
655
667
|
NestedCallDoc as NestedCallDoc2,
|
|
656
|
-
ToolTask as
|
|
668
|
+
ToolTask as ToolTask3
|
|
657
669
|
} from "@earendil-works/pi-durable";
|
|
658
670
|
import { Equal } from "typebox/value";
|
|
659
|
-
|
|
671
|
+
|
|
672
|
+
// packages/execution/src/tool-origin.ts
|
|
673
|
+
import {
|
|
674
|
+
defineDoc as defineDoc2,
|
|
675
|
+
ToolTask as ToolTask2
|
|
676
|
+
} from "@earendil-works/pi-durable";
|
|
677
|
+
var ToolOriginDoc = defineDoc2({
|
|
678
|
+
kind: "howaboua.tool-origin",
|
|
679
|
+
version: 1,
|
|
680
|
+
scope: "task",
|
|
681
|
+
checkpointWhen: () => false,
|
|
682
|
+
initial: () => ({})
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
// packages/execution/src/native-tools.ts
|
|
686
|
+
function assertNestedKey(key) {
|
|
687
|
+
if (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key))
|
|
688
|
+
throw new Error("Invalid nested call or task key");
|
|
689
|
+
}
|
|
690
|
+
function nativeToolCalls(runtime, callId, replay, resumed, assertLive, context) {
|
|
660
691
|
let sequence = 0;
|
|
661
692
|
const admissions = [];
|
|
662
|
-
|
|
693
|
+
async function execute(name, args, callContext, options = {}) {
|
|
663
694
|
assertLive();
|
|
695
|
+
if (options.key !== undefined)
|
|
696
|
+
assertNestedKey(options.key);
|
|
697
|
+
else if (resumed)
|
|
698
|
+
throw new Error("Nested calls require an explicit key after waking");
|
|
664
699
|
const key = options.key ?? String(++sequence);
|
|
665
|
-
if (options.key !== undefined && (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key)))
|
|
666
|
-
throw new Error("Invalid nested call key");
|
|
667
700
|
const call = {
|
|
668
701
|
type: "toolCall",
|
|
669
702
|
id: `${callId}/${key}`,
|
|
@@ -675,11 +708,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
675
708
|
const admission = runtime.commit(async (tx) => {
|
|
676
709
|
const slot = await tx.doc(NestedCallDoc2, runtime.taskId, key, null);
|
|
677
710
|
if (slot.taskId !== undefined) {
|
|
678
|
-
const input = (await tx.task(slot.taskId))?.input;
|
|
711
|
+
const input = slot.kind === "task" ? undefined : (await tx.task(slot.taskId))?.input;
|
|
679
712
|
if (input?.kind !== "nested" || input.call.name !== name || !Equal(input.call.arguments, call.arguments))
|
|
680
713
|
throw new Error(`Nested call ${call.id} was already made with another tool or other arguments`);
|
|
681
714
|
} else {
|
|
682
|
-
slot.
|
|
715
|
+
slot.kind = "nested";
|
|
716
|
+
slot.taskId = await tx.createTask(ToolTask3, {
|
|
683
717
|
kind: "nested",
|
|
684
718
|
parent: runtime.taskId,
|
|
685
719
|
parentCallId: callId,
|
|
@@ -691,6 +725,9 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
691
725
|
abandonOnRestart: replay !== "safe"
|
|
692
726
|
});
|
|
693
727
|
const live = await tx.doc(LiveDoc3, runtime.conversationId);
|
|
728
|
+
const origin = (await tx.doc(ToolOriginDoc, runtime.taskId)).origin;
|
|
729
|
+
if (origin)
|
|
730
|
+
(await tx.doc(ToolOriginDoc, slot.taskId)).origin = { ...origin };
|
|
694
731
|
live.nestedTools ??= [];
|
|
695
732
|
live.nestedTools.push({
|
|
696
733
|
callId: call.id,
|
|
@@ -722,7 +759,13 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
722
759
|
const slot = await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext);
|
|
723
760
|
if (slot?.taskId === undefined)
|
|
724
761
|
throw new Error("Nested task creation did not commit");
|
|
725
|
-
const
|
|
762
|
+
const taskId = slot.taskId;
|
|
763
|
+
const result = (resultContext) => readResult(name, key, taskId, resultContext);
|
|
764
|
+
return options.wait === false ? { taskId, result } : result(callContext);
|
|
765
|
+
}
|
|
766
|
+
async function readResult(name, key, taskId, callContext) {
|
|
767
|
+
assertLive();
|
|
768
|
+
const settled = await runtime.waitForTask(taskId, callContext);
|
|
726
769
|
const result = (await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext))?.result;
|
|
727
770
|
await runtime.commit(async (tx) => {
|
|
728
771
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
@@ -730,7 +773,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
730
773
|
return;
|
|
731
774
|
}, callContext);
|
|
732
775
|
return result === undefined ? {
|
|
733
|
-
taskId
|
|
776
|
+
taskId,
|
|
734
777
|
isError: true,
|
|
735
778
|
diagnostics: [
|
|
736
779
|
{
|
|
@@ -740,9 +783,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
740
783
|
}
|
|
741
784
|
]
|
|
742
785
|
} : copyJson4(result);
|
|
743
|
-
}
|
|
786
|
+
}
|
|
744
787
|
return {
|
|
745
788
|
execute,
|
|
789
|
+
async drain() {
|
|
790
|
+
await Promise.allSettled(admissions);
|
|
791
|
+
},
|
|
746
792
|
async finish() {
|
|
747
793
|
await Promise.allSettled(admissions);
|
|
748
794
|
await cleanupNativeCalls(runtime, withoutAbortSignal(context));
|
|
@@ -750,7 +796,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
750
796
|
};
|
|
751
797
|
}
|
|
752
798
|
async function cleanupNativeCalls(runtime, context) {
|
|
753
|
-
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind ===
|
|
799
|
+
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind === ToolTask3.definition.name).map((child) => runtime.abortOwned(child.id, context)));
|
|
754
800
|
await runtime.commit(async (tx) => {
|
|
755
801
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
756
802
|
await captureNativeCalls(tx, runtime.conversationId, runtime.taskId, owner.forwardingCellId);
|
|
@@ -844,18 +890,6 @@ class Progress {
|
|
|
844
890
|
}
|
|
845
891
|
}
|
|
846
892
|
|
|
847
|
-
// packages/execution/src/tool-origin.ts
|
|
848
|
-
import {
|
|
849
|
-
defineDoc as defineDoc2
|
|
850
|
-
} from "@earendil-works/pi-durable";
|
|
851
|
-
var ToolOriginDoc = defineDoc2({
|
|
852
|
-
kind: "howaboua.tool-origin",
|
|
853
|
-
version: 1,
|
|
854
|
-
scope: "task",
|
|
855
|
-
checkpointWhen: () => false,
|
|
856
|
-
initial: () => ({})
|
|
857
|
-
});
|
|
858
|
-
|
|
859
893
|
// packages/execution/src/invocation.ts
|
|
860
894
|
function executionError(code, message) {
|
|
861
895
|
return {
|
|
@@ -867,21 +901,23 @@ function executionError(code, message) {
|
|
|
867
901
|
function errorText(error) {
|
|
868
902
|
return error instanceof Error ? error.message : String(error);
|
|
869
903
|
}
|
|
870
|
-
function createInvocation(runtime, tool, context) {
|
|
904
|
+
function createInvocation(runtime, tool, context, resume) {
|
|
871
905
|
const limits = {
|
|
872
906
|
maxBytes: tool.outputLimits?.maxBytes ?? 50 * 1024,
|
|
873
907
|
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
|
874
908
|
retain: tool.outputLimits?.retain ?? "head"
|
|
875
909
|
};
|
|
876
910
|
const output = new OutputBuffer(limits);
|
|
877
|
-
const diagnostics = [
|
|
878
|
-
|
|
911
|
+
const diagnostics = [
|
|
912
|
+
...resume?.progress?.diagnostics ?? []
|
|
913
|
+
];
|
|
914
|
+
let details = resume?.progress?.details;
|
|
879
915
|
let ended = false;
|
|
880
916
|
const assertLive = () => {
|
|
881
917
|
if (ended)
|
|
882
918
|
throw new Error(`Invocation ${runtime.taskId} has settled`);
|
|
883
919
|
};
|
|
884
|
-
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, assertLive, context);
|
|
920
|
+
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, resume?.wake !== undefined, assertLive, context);
|
|
885
921
|
const snapshot = () => {
|
|
886
922
|
const retained = output.snapshot();
|
|
887
923
|
return {
|
|
@@ -910,6 +946,8 @@ function createInvocation(runtime, tool, context) {
|
|
|
910
946
|
taskId: runtime.taskId,
|
|
911
947
|
conversationId: runtime.conversationId,
|
|
912
948
|
callId: `nested-${runtime.taskId}`,
|
|
949
|
+
wake: resume?.wake,
|
|
950
|
+
checkpoint: resume?.checkpoint,
|
|
913
951
|
registry: runtime.registry,
|
|
914
952
|
agent: runtime.agent,
|
|
915
953
|
env: undefined,
|
|
@@ -930,7 +968,7 @@ function createInvocation(runtime, tool, context) {
|
|
|
930
968
|
const retained = output.snapshot();
|
|
931
969
|
return { text: retained.text, truncated: retained.droppedBytes > 0 };
|
|
932
970
|
},
|
|
933
|
-
executeTool:
|
|
971
|
+
executeTool: native.execute,
|
|
934
972
|
diagnostic: (value) => {
|
|
935
973
|
assertLive();
|
|
936
974
|
diagnostics.push(copyJson5(value, { omitUndefinedProperties: true }));
|
|
@@ -944,24 +982,44 @@ function createInvocation(runtime, tool, context) {
|
|
|
944
982
|
committed.catch(() => {});
|
|
945
983
|
await awaitWithContext(committed, detailContext);
|
|
946
984
|
},
|
|
947
|
-
commit: async (change, commitContext) => {
|
|
985
|
+
commit: async (change, commitContext, options) => {
|
|
948
986
|
assertLive();
|
|
949
987
|
let result;
|
|
950
988
|
await runtime.commit(async (tx) => {
|
|
951
989
|
result = await change(tx);
|
|
952
990
|
return;
|
|
953
|
-
}, commitContext);
|
|
991
|
+
}, commitContext, options);
|
|
954
992
|
return result;
|
|
955
993
|
},
|
|
956
994
|
memo: runtime.memo,
|
|
957
|
-
|
|
995
|
+
readSignal: runtime.readSignal,
|
|
996
|
+
async createTask(task, input, {
|
|
997
|
+
key,
|
|
998
|
+
...options
|
|
999
|
+
}, taskContext) {
|
|
958
1000
|
assertLive();
|
|
1001
|
+
if (key !== undefined)
|
|
1002
|
+
assertNestedKey(key);
|
|
1003
|
+
else if (resume?.wake !== undefined)
|
|
1004
|
+
throw new Error("Child tasks require an explicit key after waking");
|
|
959
1005
|
let id;
|
|
960
1006
|
await runtime.commit(async (tx) => {
|
|
1007
|
+
const member = key === undefined ? undefined : await tx.doc(NestedCallDoc3, runtime.taskId, key, null);
|
|
1008
|
+
if (member?.taskId !== undefined) {
|
|
1009
|
+
const existing = member.kind === "task" ? await tx.task(member.taskId) : undefined;
|
|
1010
|
+
if (existing?.kind !== task.definition.name || !equal(existing.input, copyJson5(input, { omitUndefinedProperties: true })))
|
|
1011
|
+
throw new Error(`Key ${key} was already used for another call or task`);
|
|
1012
|
+
id = member.taskId;
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
961
1015
|
id = await tx.createTask(task, input, {
|
|
962
1016
|
...options,
|
|
963
1017
|
...options.abandonOnRestart === undefined && tool.replay !== "safe" && options.ownership.kind === "task" && options.ownership.taskId === runtime.taskId ? { abandonOnRestart: true } : {}
|
|
964
1018
|
});
|
|
1019
|
+
if (member !== undefined) {
|
|
1020
|
+
member.kind = "task";
|
|
1021
|
+
member.taskId = id;
|
|
1022
|
+
}
|
|
965
1023
|
const source = await tx.doc(ToolOriginDoc, runtime.taskId);
|
|
966
1024
|
if (source.origin !== undefined)
|
|
967
1025
|
(await tx.doc(ToolOriginDoc, id)).origin = { ...source.origin };
|
|
@@ -1008,6 +1066,14 @@ function createInvocation(runtime, tool, context) {
|
|
|
1008
1066
|
for (const waiter of await progress.stop())
|
|
1009
1067
|
waiter.reject(error);
|
|
1010
1068
|
},
|
|
1069
|
+
async park() {
|
|
1070
|
+
ended = true;
|
|
1071
|
+
await native.drain();
|
|
1072
|
+
output.end();
|
|
1073
|
+
const pending = await progress.stop();
|
|
1074
|
+
const { output: _output, ...kept } = snapshot();
|
|
1075
|
+
return { progress: storeResult(kept), pending };
|
|
1076
|
+
},
|
|
1011
1077
|
limits,
|
|
1012
1078
|
output,
|
|
1013
1079
|
progress
|
|
@@ -1109,7 +1175,18 @@ function combineControls(controls, allowTermination = true) {
|
|
|
1109
1175
|
}
|
|
1110
1176
|
|
|
1111
1177
|
// packages/execution/src/tool-contract.ts
|
|
1112
|
-
import { copyJson as copyJson7 } from "@earendil-works/chord";
|
|
1178
|
+
import { copyJson as copyJson7 } from "@earendil-works/chord/json";
|
|
1179
|
+
|
|
1180
|
+
// packages/execution/src/tool-namespaces.ts
|
|
1181
|
+
import { isJsonValue } from "@earendil-works/chord/json";
|
|
1182
|
+
function validateJsToolNamespace(tool, namespace) {
|
|
1183
|
+
const key = (value) => typeof value === "string" && /^[a-zA-Z_$][\w$]*$/.test(value);
|
|
1184
|
+
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))))
|
|
1185
|
+
throw new Error(`Tool ${tool} has invalid jsNamespace metadata`);
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
// packages/execution/src/tool-contract.ts
|
|
1189
|
+
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 {}";
|
|
1113
1190
|
function readToolContract(tool) {
|
|
1114
1191
|
const candidate = "executionHints" in tool ? tool.executionHints : undefined;
|
|
1115
1192
|
if (candidate !== undefined && (candidate === null || typeof candidate !== "object" || Array.isArray(candidate))) {
|
|
@@ -1123,6 +1200,7 @@ function readToolContract(tool) {
|
|
|
1123
1200
|
"yieldTimeMs",
|
|
1124
1201
|
"inputSchema",
|
|
1125
1202
|
"nativeOnly",
|
|
1203
|
+
"nestedHidden",
|
|
1126
1204
|
"discovery"
|
|
1127
1205
|
]);
|
|
1128
1206
|
for (const key of Object.keys(hints))
|
|
@@ -1146,6 +1224,12 @@ function readToolContract(tool) {
|
|
|
1146
1224
|
const nativeOnly = "nativeOnly" in hints ? hints.nativeOnly : undefined;
|
|
1147
1225
|
if (nativeOnly !== undefined && typeof nativeOnly !== "boolean")
|
|
1148
1226
|
throw new Error(`Tool ${tool.name} executionHints.nativeOnly must be a boolean`);
|
|
1227
|
+
const nestedHidden = "nestedHidden" in hints ? hints.nestedHidden : undefined;
|
|
1228
|
+
const jsNamespace = "jsNamespace" in tool ? tool.jsNamespace : undefined;
|
|
1229
|
+
if (jsNamespace !== undefined)
|
|
1230
|
+
validateJsToolNamespace(tool.name, jsNamespace);
|
|
1231
|
+
if (nestedHidden !== undefined && typeof nestedHidden !== "boolean")
|
|
1232
|
+
throw new Error(`Tool ${tool.name} executionHints.nestedHidden must be a boolean`);
|
|
1149
1233
|
const yieldTimeMs = "yieldTimeMs" in hints ? hints.yieldTimeMs : undefined;
|
|
1150
1234
|
if (yieldTimeMs !== undefined && (typeof yieldTimeMs !== "number" || !Number.isSafeInteger(yieldTimeMs) || yieldTimeMs < 0))
|
|
1151
1235
|
throw new Error(`Tool ${tool.name} executionHints.yieldTimeMs must be a non-negative safe integer`);
|
|
@@ -1162,6 +1246,8 @@ function readToolContract(tool) {
|
|
|
1162
1246
|
inputSchema,
|
|
1163
1247
|
deferLoading: typeof deferred === "boolean" ? deferred : true,
|
|
1164
1248
|
nativeOnly: nativeOnly === true,
|
|
1249
|
+
nestedHidden: nestedHidden === true,
|
|
1250
|
+
...jsNamespace === undefined ? {} : { jsNamespace },
|
|
1165
1251
|
...typeof yieldTimeMs === "number" ? { yieldTimeMs } : {},
|
|
1166
1252
|
...outputHint === undefined ? {} : { output: outputHint },
|
|
1167
1253
|
help: [
|
|
@@ -1397,7 +1483,7 @@ function createCellTask(options, nestedTask) {
|
|
|
1397
1483
|
import {
|
|
1398
1484
|
hook,
|
|
1399
1485
|
LiveDoc as LiveDoc4,
|
|
1400
|
-
ToolTask as
|
|
1486
|
+
ToolTask as ToolTask4
|
|
1401
1487
|
} from "@earendil-works/pi-durable";
|
|
1402
1488
|
function forwardingHook() {
|
|
1403
1489
|
let host;
|
|
@@ -1407,7 +1493,7 @@ function forwardingHook() {
|
|
|
1407
1493
|
throw new Error("Create a separate execution component for each Harness");
|
|
1408
1494
|
host = harness;
|
|
1409
1495
|
},
|
|
1410
|
-
hook: hook(
|
|
1496
|
+
hook: hook(ToolTask4, {
|
|
1411
1497
|
async beforeTool(call, api, context) {
|
|
1412
1498
|
const parent = call.parent;
|
|
1413
1499
|
if (parent === undefined || host === undefined)
|
|
@@ -1420,7 +1506,7 @@ function forwardingHook() {
|
|
|
1420
1506
|
if (conversation === undefined)
|
|
1421
1507
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1422
1508
|
await conversation.commit(async (tx) => {
|
|
1423
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1509
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1424
1510
|
return;
|
|
1425
1511
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1426
1512
|
const admission = owner.forwardingCellId === undefined;
|
|
@@ -1447,7 +1533,7 @@ function forwardingHook() {
|
|
|
1447
1533
|
if (conversation === undefined)
|
|
1448
1534
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1449
1535
|
await conversation.commit(async (tx) => {
|
|
1450
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1536
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1451
1537
|
return;
|
|
1452
1538
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1453
1539
|
const cellId = owner.forwardingCellId;
|
|
@@ -1472,17 +1558,18 @@ function forwardingHook() {
|
|
|
1472
1558
|
}
|
|
1473
1559
|
|
|
1474
1560
|
// packages/execution/src/nested.ts
|
|
1561
|
+
import { copyJson as copyJson11 } from "@earendil-works/chord/json";
|
|
1475
1562
|
import {
|
|
1476
1563
|
defineTask as defineTask2
|
|
1477
1564
|
} from "@earendil-works/pi-durable";
|
|
1478
|
-
import {
|
|
1565
|
+
import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
1479
1566
|
|
|
1480
1567
|
// packages/execution/src/nested-call.ts
|
|
1481
1568
|
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
1482
1569
|
import {
|
|
1483
|
-
ToolTask as
|
|
1570
|
+
ToolTask as ToolTask5
|
|
1484
1571
|
} from "@earendil-works/pi-durable";
|
|
1485
|
-
import {
|
|
1572
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
1486
1573
|
function nestedCall(runtime, input) {
|
|
1487
1574
|
const args = input.arguments;
|
|
1488
1575
|
return {
|
|
@@ -1503,7 +1590,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1503
1590
|
if (typeof input !== "string")
|
|
1504
1591
|
return input;
|
|
1505
1592
|
const contract = readToolContract(tool);
|
|
1506
|
-
if (Boolean(
|
|
1593
|
+
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)))
|
|
1507
1594
|
return input;
|
|
1508
1595
|
const text = input.trim();
|
|
1509
1596
|
if (contract.discovery === "help" && text === "help")
|
|
@@ -1526,7 +1613,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1526
1613
|
async function eachHook(agent, name, api, invoke) {
|
|
1527
1614
|
for (const extension of agent.extensions)
|
|
1528
1615
|
for (const registration of extension.hooks ?? []) {
|
|
1529
|
-
if (registration.task !==
|
|
1616
|
+
if (registration.task !== ToolTask5.definition.name)
|
|
1530
1617
|
continue;
|
|
1531
1618
|
const handler = registration.handlers[name];
|
|
1532
1619
|
if (handler === undefined)
|
|
@@ -1541,8 +1628,120 @@ async function eachHook(agent, name, api, invoke) {
|
|
|
1541
1628
|
}
|
|
1542
1629
|
}
|
|
1543
1630
|
|
|
1631
|
+
// packages/execution/src/nested-park.ts
|
|
1632
|
+
import {
|
|
1633
|
+
withCancel,
|
|
1634
|
+
withoutAbortSignal as withoutAbortSignal3
|
|
1635
|
+
} from "@earendil-works/chord/context";
|
|
1636
|
+
import { copyJson as copyJson9 } from "@earendil-works/chord/json";
|
|
1637
|
+
var resultFields = [
|
|
1638
|
+
"output",
|
|
1639
|
+
"structuredOutput",
|
|
1640
|
+
"isError",
|
|
1641
|
+
"details",
|
|
1642
|
+
"diagnostics",
|
|
1643
|
+
"usage",
|
|
1644
|
+
"control"
|
|
1645
|
+
];
|
|
1646
|
+
function sleepState(checkpoint, { until, signals }) {
|
|
1647
|
+
return {
|
|
1648
|
+
status: "sleeping",
|
|
1649
|
+
checkpoint,
|
|
1650
|
+
...until === undefined ? {} : { until },
|
|
1651
|
+
...signals === undefined ? {} : { signals }
|
|
1652
|
+
};
|
|
1653
|
+
}
|
|
1654
|
+
async function parkGate(runtime, sleep, context) {
|
|
1655
|
+
try {
|
|
1656
|
+
await runtime.commit(() => sleepState({ phase: "call" }, sleep), context);
|
|
1657
|
+
} catch (error) {
|
|
1658
|
+
runtime.signal.throwIfAborted();
|
|
1659
|
+
return errorText(error);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
async function parkTool(runtime, invocation, tool, args, park, context) {
|
|
1663
|
+
if (park.kind !== "sleep" && park.kind !== "wait")
|
|
1664
|
+
return "Unknown park kind";
|
|
1665
|
+
const mixed = resultFields.find((field) => (field in park));
|
|
1666
|
+
if (mixed !== undefined)
|
|
1667
|
+
return `A park cannot carry the result field ${mixed}`;
|
|
1668
|
+
const intent = {
|
|
1669
|
+
arguments: args,
|
|
1670
|
+
replay: tool.replay ?? "unsafe",
|
|
1671
|
+
...park.checkpoint === undefined ? {} : { checkpoint: copyJson9(park.checkpoint) }
|
|
1672
|
+
};
|
|
1673
|
+
const { progress, pending } = await invocation.park();
|
|
1674
|
+
try {
|
|
1675
|
+
await runtime.commit(async (tx) => {
|
|
1676
|
+
if (park.kind === "wait") {
|
|
1677
|
+
for (const id of park.on)
|
|
1678
|
+
if ((await tx.task(id))?.owner !== runtime.taskId)
|
|
1679
|
+
throw new Error(`Task ${id} is not owned by the call`);
|
|
1680
|
+
}
|
|
1681
|
+
const checkpoint = { ...intent, phase: "parked" };
|
|
1682
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1683
|
+
doc.result = progress;
|
|
1684
|
+
doc.droppedBytes = 0;
|
|
1685
|
+
doc.droppedLines = 0;
|
|
1686
|
+
await forwardInvocationResult(tx, runtime.taskId, progress, "running");
|
|
1687
|
+
return park.kind === "sleep" ? sleepState(checkpoint, park) : { status: "waiting", checkpoint, on: park.on, policy: park.policy };
|
|
1688
|
+
}, context);
|
|
1689
|
+
} catch (error) {
|
|
1690
|
+
for (const waiter of pending)
|
|
1691
|
+
waiter.reject(error);
|
|
1692
|
+
const detached = withoutAbortSignal3(context);
|
|
1693
|
+
const record = await runtime.getTask(runtime.taskId, detached);
|
|
1694
|
+
if (record?.abortRequested) {
|
|
1695
|
+
const watch = await runtime.watchDoc(InvocationDoc, runtime.taskId, detached);
|
|
1696
|
+
if (watch !== undefined) {
|
|
1697
|
+
const cleanup = withCancel(detached);
|
|
1698
|
+
watch.closed.then((end) => cleanup.cancel(end.kind === "failed" ? end.error : undefined));
|
|
1699
|
+
try {
|
|
1700
|
+
await cancelParkedTool(runtime, tool, intent, cleanup.context);
|
|
1701
|
+
} finally {
|
|
1702
|
+
await watch.stop();
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
throw error;
|
|
1706
|
+
}
|
|
1707
|
+
runtime.signal.throwIfAborted();
|
|
1708
|
+
return errorText(error);
|
|
1709
|
+
}
|
|
1710
|
+
for (const waiter of pending)
|
|
1711
|
+
waiter.resolve();
|
|
1712
|
+
}
|
|
1713
|
+
async function cancelParkedTool(runtime, tool, intent, context) {
|
|
1714
|
+
if (!tool) {
|
|
1715
|
+
runtime.report(new Error("The parked tool is no longer registered; cleanup could not run"));
|
|
1716
|
+
return;
|
|
1717
|
+
}
|
|
1718
|
+
if (tool.cancel === undefined)
|
|
1719
|
+
return;
|
|
1720
|
+
const cleanup = createInvocation(runtime, tool, context, {
|
|
1721
|
+
checkpoint: intent.checkpoint,
|
|
1722
|
+
wake: undefined
|
|
1723
|
+
});
|
|
1724
|
+
const refuseWork = () => {
|
|
1725
|
+
throw new Error("A tool cannot start tasks or nested calls during cancellation");
|
|
1726
|
+
};
|
|
1727
|
+
let reason = new Error("Tool cancellation finished");
|
|
1728
|
+
try {
|
|
1729
|
+
const api = await cleanup.executionApi();
|
|
1730
|
+
await tool.cancel(intent.arguments, {
|
|
1731
|
+
...api,
|
|
1732
|
+
createTask: refuseWork,
|
|
1733
|
+
executeTool: refuseWork
|
|
1734
|
+
}, context);
|
|
1735
|
+
} catch (error) {
|
|
1736
|
+
reason = error;
|
|
1737
|
+
runtime.report(error);
|
|
1738
|
+
} finally {
|
|
1739
|
+
await cleanup.stop(reason);
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1544
1743
|
// packages/execution/src/nested-result.ts
|
|
1545
|
-
import { copyJson as
|
|
1744
|
+
import { copyJson as copyJson10 } from "@earendil-works/chord/json";
|
|
1546
1745
|
import {
|
|
1547
1746
|
defineEntry,
|
|
1548
1747
|
UsageDoc
|
|
@@ -1590,11 +1789,11 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1590
1789
|
taskId: runtime.taskId,
|
|
1591
1790
|
callId: call.id,
|
|
1592
1791
|
name: call.name,
|
|
1593
|
-
arguments:
|
|
1792
|
+
arguments: copyJson10(call.arguments),
|
|
1594
1793
|
result: stored,
|
|
1595
1794
|
...current.state.checkpoint.phase === "execute" ? {
|
|
1596
1795
|
intent: {
|
|
1597
|
-
arguments:
|
|
1796
|
+
arguments: copyJson10(current.state.checkpoint.arguments),
|
|
1598
1797
|
replay: current.state.checkpoint.replay
|
|
1599
1798
|
}
|
|
1600
1799
|
} : {}
|
|
@@ -1603,7 +1802,7 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1603
1802
|
if (result.usage !== undefined) {
|
|
1604
1803
|
const totals = (await tx.doc(UsageDoc, runtime.conversationId)).tools;
|
|
1605
1804
|
const previous = Object.hasOwn(totals, call.name) ? totals[call.name] : undefined;
|
|
1606
|
-
totals[call.name] =
|
|
1805
|
+
totals[call.name] = copyJson10(previous === undefined ? result.usage : sumUsage(previous, result.usage), { omitUndefinedProperties: true });
|
|
1607
1806
|
}
|
|
1608
1807
|
const outcome = ending === "completed" ? { status: ending, result: stored } : ending === "aborted" ? { status: ending, result: stored } : {
|
|
1609
1808
|
status: ending,
|
|
@@ -1640,7 +1839,11 @@ function sumUsage(previous, added) {
|
|
|
1640
1839
|
function createNestedToolTask(name) {
|
|
1641
1840
|
return defineTask2({
|
|
1642
1841
|
name,
|
|
1643
|
-
version:
|
|
1842
|
+
version: 2,
|
|
1843
|
+
migrate: (input, checkpoint) => ({
|
|
1844
|
+
input,
|
|
1845
|
+
checkpoint
|
|
1846
|
+
}),
|
|
1644
1847
|
initial: () => ({ phase: "call" }),
|
|
1645
1848
|
phases: {
|
|
1646
1849
|
call: async (task, runtime, context) => {
|
|
@@ -1666,22 +1869,25 @@ function createNestedToolTask(name) {
|
|
|
1666
1869
|
return reject(runtime, call, executionError("tool_unavailable", `Tool ${call.name} is not available`), agent, tool, context);
|
|
1667
1870
|
let args;
|
|
1668
1871
|
try {
|
|
1669
|
-
const supplied = task.input.arguments === null && !
|
|
1872
|
+
const supplied = task.input.arguments === null && !compile2(tool.parameters).check(null) && compile2(tool.parameters).check({}) ? undefined : task.input.arguments;
|
|
1670
1873
|
const prepared = tool.prepareArguments === undefined ? prepareNestedInput(tool, supplied) : tool.prepareArguments(prepareNestedInput(tool, supplied));
|
|
1671
|
-
args = checked(tool, call, prepared === undefined &&
|
|
1874
|
+
args = checked(tool, call, prepared === undefined && compile2(tool.parameters).check({}) ? {} : prepared);
|
|
1672
1875
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1673
1876
|
call.arguments = args;
|
|
1674
1877
|
} catch (error) {
|
|
1675
1878
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1676
1879
|
}
|
|
1677
1880
|
let block;
|
|
1881
|
+
let sleep;
|
|
1678
1882
|
await eachHook(agent, "beforeTool", runtime, async (handler) => {
|
|
1679
|
-
if (block !== undefined)
|
|
1883
|
+
if (block !== undefined || sleep !== undefined)
|
|
1680
1884
|
return;
|
|
1681
1885
|
try {
|
|
1682
1886
|
const decision = await handler({ ...call, arguments: args }, runtime, context);
|
|
1683
1887
|
if (decision?.block !== undefined)
|
|
1684
1888
|
block = decision.block;
|
|
1889
|
+
else if (decision?.sleep !== undefined)
|
|
1890
|
+
sleep = decision.sleep;
|
|
1685
1891
|
else if (decision?.arguments !== undefined)
|
|
1686
1892
|
args = decision.arguments;
|
|
1687
1893
|
} catch (error) {
|
|
@@ -1692,71 +1898,122 @@ function createNestedToolTask(name) {
|
|
|
1692
1898
|
});
|
|
1693
1899
|
if (block !== undefined)
|
|
1694
1900
|
return reject(runtime, call, executionError("blocked", `Tool call blocked: ${block}`), agent, tool, context);
|
|
1901
|
+
if (sleep !== undefined) {
|
|
1902
|
+
const reason = await parkGate(runtime, sleep, context);
|
|
1903
|
+
if (reason === undefined)
|
|
1904
|
+
return;
|
|
1905
|
+
return reject(runtime, call, executionError("invalid_park", reason), agent, tool, context);
|
|
1906
|
+
}
|
|
1695
1907
|
try {
|
|
1696
|
-
args = checked(tool, call, args)
|
|
1908
|
+
args = copyJson11(checked(tool, call, args), {
|
|
1909
|
+
omitUndefinedProperties: true
|
|
1910
|
+
});
|
|
1697
1911
|
} catch (error) {
|
|
1698
1912
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1699
1913
|
}
|
|
1700
|
-
await runtime
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
callId: call.id,
|
|
1704
|
-
parentCallId: call.parent?.callId ?? `nested-${runtime.taskId}`,
|
|
1705
|
-
name: call.name,
|
|
1706
|
-
arguments: args,
|
|
1707
|
-
status: "running",
|
|
1708
|
-
result: null,
|
|
1709
|
-
omittedFields: []
|
|
1710
|
-
});
|
|
1711
|
-
return {
|
|
1712
|
-
status: "running",
|
|
1713
|
-
checkpoint: {
|
|
1714
|
-
phase: "execute",
|
|
1715
|
-
arguments: args,
|
|
1716
|
-
replay: tool.replay ?? "unsafe"
|
|
1717
|
-
}
|
|
1718
|
-
};
|
|
1914
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1915
|
+
arguments: args,
|
|
1916
|
+
replay: tool.replay ?? "unsafe"
|
|
1719
1917
|
}, context);
|
|
1720
|
-
|
|
1918
|
+
},
|
|
1919
|
+
parked: async (task, runtime, context) => {
|
|
1920
|
+
const intent = task.state.checkpoint;
|
|
1921
|
+
const call = nestedCall(runtime, task.input);
|
|
1922
|
+
call.arguments = intent.arguments;
|
|
1923
|
+
const agent = await runtime.agent(context);
|
|
1924
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1925
|
+
if (tool === undefined || readToolContract(tool).nativeOnly)
|
|
1926
|
+
return interrupted(runtime, call, "interrupted", `Parked tool ${call.name} is no longer available and could not be resumed`, context);
|
|
1927
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1928
|
+
...intent,
|
|
1929
|
+
replay: tool.replay ?? "unsafe"
|
|
1930
|
+
}, context, task.state.wake);
|
|
1721
1931
|
},
|
|
1722
1932
|
execute: async (task, runtime, context) => {
|
|
1933
|
+
const intent = task.state.checkpoint;
|
|
1723
1934
|
const call = nestedCall(runtime, task.input);
|
|
1724
1935
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1725
1936
|
call.arguments = task.state.checkpoint.arguments;
|
|
1726
1937
|
const agent = await runtime.agent(context);
|
|
1727
1938
|
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1728
|
-
if (
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1939
|
+
if (intent.replay === "safe" && tool?.replay === "safe" && !readToolContract(tool).nativeOnly) {
|
|
1940
|
+
if (intent.resume === undefined)
|
|
1941
|
+
await runtime.commit(async (tx) => {
|
|
1942
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1943
|
+
doc.result = {};
|
|
1944
|
+
doc.droppedBytes = 0;
|
|
1945
|
+
doc.droppedLines = 0;
|
|
1946
|
+
return;
|
|
1947
|
+
}, context);
|
|
1948
|
+
return run(runtime, call, tool, intent, agent, context, intent.resume?.wake);
|
|
1737
1949
|
}
|
|
1950
|
+
if (intent.resume !== undefined)
|
|
1951
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1738
1952
|
await interrupted(runtime, call, "interrupted", `Tool ${call.name} was interrupted and may have partially run`, context);
|
|
1739
1953
|
}
|
|
1740
1954
|
},
|
|
1741
1955
|
abort: async (task, runtime, context) => {
|
|
1742
1956
|
const call = nestedCall(runtime, task.input);
|
|
1957
|
+
const intent = task.state.checkpoint;
|
|
1958
|
+
if (intent.phase === "parked" || intent.phase === "execute" && intent.resume !== undefined) {
|
|
1959
|
+
const agent = await runtime.agent(context);
|
|
1960
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1961
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1962
|
+
}
|
|
1743
1963
|
await interrupted(runtime, call, "aborted", `Tool ${call.name} was aborted${task.state.checkpoint.phase === "execute" ? " and may have partially run" : ""}`, context);
|
|
1744
1964
|
}
|
|
1745
1965
|
});
|
|
1746
1966
|
}
|
|
1967
|
+
async function startAttempt(runtime, call, tool, agent, intent, context, wake) {
|
|
1968
|
+
await runtime.commit(async (tx) => {
|
|
1969
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1970
|
+
await forwardCall(tx, doc.forwardingCellId, {
|
|
1971
|
+
callId: call.id,
|
|
1972
|
+
parentCallId: call.parent?.callId ?? call.id,
|
|
1973
|
+
name: call.name,
|
|
1974
|
+
arguments: intent.arguments,
|
|
1975
|
+
status: "running",
|
|
1976
|
+
result: null,
|
|
1977
|
+
omittedFields: []
|
|
1978
|
+
});
|
|
1979
|
+
return {
|
|
1980
|
+
status: "running",
|
|
1981
|
+
checkpoint: {
|
|
1982
|
+
...intent,
|
|
1983
|
+
phase: "execute",
|
|
1984
|
+
...wake === undefined ? {} : { resume: { wake } }
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
}, context);
|
|
1988
|
+
await run(runtime, call, tool, intent, agent, context, wake);
|
|
1989
|
+
}
|
|
1747
1990
|
async function reject(runtime, call, result, agent, tool, context) {
|
|
1748
1991
|
await eachHook(agent, "afterTool", runtime, async (handler) => {
|
|
1749
1992
|
result = await handler(call, result, runtime, context) ?? result;
|
|
1750
1993
|
});
|
|
1751
1994
|
await settle(runtime, call, finishResult(tool, call.name, result), "completed", context);
|
|
1752
1995
|
}
|
|
1753
|
-
async function run(runtime, call, tool,
|
|
1996
|
+
async function run(runtime, call, tool, intent, agent, context, wake) {
|
|
1754
1997
|
let invocation;
|
|
1755
1998
|
let result;
|
|
1756
1999
|
let ending = "completed";
|
|
1757
2000
|
try {
|
|
1758
|
-
|
|
1759
|
-
|
|
2001
|
+
const previous = wake === undefined ? undefined : await runtime.snapshot(InvocationDoc, runtime.taskId, context);
|
|
2002
|
+
invocation = createInvocation(runtime, tool, context, {
|
|
2003
|
+
wake,
|
|
2004
|
+
checkpoint: intent.checkpoint,
|
|
2005
|
+
...previous === undefined ? {} : { progress: previous.result }
|
|
2006
|
+
});
|
|
2007
|
+
const returned = await tool.execute(intent.arguments, await invocation.executionApi(), context);
|
|
2008
|
+
if (returned.kind === undefined)
|
|
2009
|
+
result = returned;
|
|
2010
|
+
else {
|
|
2011
|
+
const reason = await parkTool(runtime, invocation, tool, intent.arguments, returned, context);
|
|
2012
|
+
if (reason === undefined)
|
|
2013
|
+
return;
|
|
2014
|
+
result = executionError("invalid_park", reason);
|
|
2015
|
+
ending = "failed";
|
|
2016
|
+
}
|
|
1760
2017
|
} catch (error) {
|
|
1761
2018
|
if (runtime.signal.aborted) {
|
|
1762
2019
|
await invocation?.stop(error);
|
|
@@ -1808,7 +2065,7 @@ function finishResult(tool, name, result) {
|
|
|
1808
2065
|
} else if (final.structuredOutput === undefined) {
|
|
1809
2066
|
if (!final.isError)
|
|
1810
2067
|
throw new Error(`Tool ${name} returned no structuredOutput`);
|
|
1811
|
-
} else if (!
|
|
2068
|
+
} else if (!compile2(tool.structuredOutputSchema).check(final.structuredOutput)) {
|
|
1812
2069
|
throw new Error(`Tool ${name} returned structuredOutput that does not match its schema`);
|
|
1813
2070
|
}
|
|
1814
2071
|
} catch (error) {
|
|
@@ -1857,9 +2114,11 @@ function providerProjection(surfaceTools) {
|
|
|
1857
2114
|
const conversation = await host.conversation(api.conversationId, context);
|
|
1858
2115
|
if (conversation === undefined)
|
|
1859
2116
|
throw new Error(`Conversation ${api.conversationId} is unavailable`);
|
|
1860
|
-
for (const tool of (await conversation.agent(context)).tools)
|
|
1861
|
-
|
|
2117
|
+
for (const tool of (await conversation.agent(context)).tools) {
|
|
2118
|
+
const contract = readToolContract(tool);
|
|
2119
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1862
2120
|
visible.add(tool.name);
|
|
2121
|
+
}
|
|
1863
2122
|
}
|
|
1864
2123
|
return { messages: projectToolSchemas(request.messages, visible) };
|
|
1865
2124
|
}
|
|
@@ -1868,7 +2127,7 @@ function providerProjection(surfaceTools) {
|
|
|
1868
2127
|
}
|
|
1869
2128
|
|
|
1870
2129
|
// packages/execution/src/wait-session.ts
|
|
1871
|
-
import { withoutAbortSignal as
|
|
2130
|
+
import { withoutAbortSignal as withoutAbortSignal4 } from "@earendil-works/chord/context";
|
|
1872
2131
|
|
|
1873
2132
|
// packages/execution/src/tool-value.ts
|
|
1874
2133
|
class NestedToolError extends Error {
|
|
@@ -1939,7 +2198,7 @@ async function recoverWaitSession(cellId, yieldTimeMs, maxTokens, api, context,
|
|
|
1939
2198
|
}, context);
|
|
1940
2199
|
let cancelling;
|
|
1941
2200
|
const abort = () => {
|
|
1942
|
-
cancelling ??= cancelTask(id,
|
|
2201
|
+
cancelling ??= cancelTask(id, withoutAbortSignal4(context));
|
|
1943
2202
|
cancelling.catch(() => {});
|
|
1944
2203
|
};
|
|
1945
2204
|
context.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
@@ -2023,7 +2282,7 @@ function createCellCoordinator(options) {
|
|
|
2023
2282
|
if (driver.closed)
|
|
2024
2283
|
throw new Error("Execution host is closed");
|
|
2025
2284
|
assertSingleExecutionMode(await api.agent(context));
|
|
2026
|
-
const copied =
|
|
2285
|
+
const copied = copyJson12(input);
|
|
2027
2286
|
return api.commit(async (tx) => {
|
|
2028
2287
|
const isolatedLimit = options.isolatedConcurrency ?? 0;
|
|
2029
2288
|
const isolated = isolatedLimit > 0 && input["isolated"] === true;
|
|
@@ -2103,7 +2362,7 @@ function createCellCoordinator(options) {
|
|
|
2103
2362
|
if (yielding.consume()) {
|
|
2104
2363
|
return initial;
|
|
2105
2364
|
}
|
|
2106
|
-
const wait =
|
|
2365
|
+
const wait = withCancel2(context);
|
|
2107
2366
|
const timeout = new Error("Cell observation yielded");
|
|
2108
2367
|
const timer = setTimeout(() => wait.cancel(timeout), Math.min(yieldTimeMs, 2147483647));
|
|
2109
2368
|
try {
|
|
@@ -3291,7 +3550,7 @@ function createUpdates(write, context) {
|
|
|
3291
3550
|
}
|
|
3292
3551
|
|
|
3293
3552
|
// packages/notebook/src/tool-observation.ts
|
|
3294
|
-
import { copyJson as
|
|
3553
|
+
import { copyJson as copyJson13 } from "@earendil-works/chord/json";
|
|
3295
3554
|
import {
|
|
3296
3555
|
defineDocFamily as defineDocFamily2
|
|
3297
3556
|
} from "@earendil-works/pi-durable";
|
|
@@ -3346,7 +3605,7 @@ async function observeNotebookCell(observation, maxTokens, api, context) {
|
|
|
3346
3605
|
cell_id: String(observation.cellId),
|
|
3347
3606
|
status: observation.status,
|
|
3348
3607
|
...observation.forwardedToolCalls === undefined ? {} : {
|
|
3349
|
-
forwardedToolCalls:
|
|
3608
|
+
forwardedToolCalls: copyJson13(observation.forwardedToolCalls, {
|
|
3350
3609
|
omitUndefinedProperties: true
|
|
3351
3610
|
})
|
|
3352
3611
|
},
|
|
@@ -3367,7 +3626,7 @@ async function observeNotebookCell(observation, maxTokens, api, context) {
|
|
|
3367
3626
|
|
|
3368
3627
|
// packages/js-notebook/src/engine.ts
|
|
3369
3628
|
import { randomUUID as randomUUID7 } from "node:crypto";
|
|
3370
|
-
import { copyJson as
|
|
3629
|
+
import { copyJson as copyJson14 } from "@earendil-works/chord/json";
|
|
3371
3630
|
import {
|
|
3372
3631
|
defineDoc as defineDoc3
|
|
3373
3632
|
} from "@earendil-works/pi-durable";
|
|
@@ -4193,7 +4452,7 @@ async function runtimePackage(name, version, stateDirectory, signal) {
|
|
|
4193
4452
|
}
|
|
4194
4453
|
async function loadJsRuntime(stateDirectory, signal) {
|
|
4195
4454
|
signal?.throwIfAborted();
|
|
4196
|
-
const installed = await runtimePackage("@earendil-works/pi-codemode", "1.
|
|
4455
|
+
const installed = await runtimePackage("@earendil-works/pi-codemode", "1.8.0", stateDirectory, signal);
|
|
4197
4456
|
const manifest = JSON.parse(await readFile3(installed.manifest, "utf8"));
|
|
4198
4457
|
if (!manifest || typeof manifest !== "object" || !("main" in manifest) || typeof manifest.main !== "string")
|
|
4199
4458
|
throw new Error("The JS Notebook runtime package is incomplete; reinstall it, then retry");
|
|
@@ -5222,7 +5481,7 @@ class JsNotebookClient {
|
|
|
5222
5481
|
const definitions = tools.map((tool) => ({
|
|
5223
5482
|
name: tool.name,
|
|
5224
5483
|
exposed: tool.name,
|
|
5225
|
-
description: tool.help ?? tool.usage ?? tool.description,
|
|
5484
|
+
...tool.hidden ? {} : { description: tool.help ?? tool.usage ?? tool.description },
|
|
5226
5485
|
..."textOutput" in tool && tool.textOutput ? { output: tool.textOutput } : {}
|
|
5227
5486
|
}));
|
|
5228
5487
|
this.project.setTools(definitions);
|
|
@@ -5632,13 +5891,14 @@ function createJsNotebookEngine(options) {
|
|
|
5632
5891
|
const contract = readToolContract(registration);
|
|
5633
5892
|
return {
|
|
5634
5893
|
name: registration.name,
|
|
5894
|
+
hidden: contract.nestedHidden,
|
|
5635
5895
|
description: contract.description,
|
|
5636
5896
|
usage: contract.usage,
|
|
5637
5897
|
help: contract.help,
|
|
5638
5898
|
...registration.name === "exec_command" || registration.name === "write_stdin" ? { textOutput: "plain-command" } : {},
|
|
5639
5899
|
async invoke(input, _context, signal) {
|
|
5640
5900
|
signal.throwIfAborted();
|
|
5641
|
-
const args =
|
|
5901
|
+
const args = copyJson14(input === undefined ? null : input, {
|
|
5642
5902
|
omitUndefinedProperties: true
|
|
5643
5903
|
});
|
|
5644
5904
|
const invoke = api.tools[registration.name];
|
|
@@ -5923,15 +6183,15 @@ function createJsNotebookMode(options) {
|
|
|
5923
6183
|
};
|
|
5924
6184
|
return {
|
|
5925
6185
|
output: [{ type: "text", text: result.message }],
|
|
5926
|
-
details:
|
|
5927
|
-
structuredOutput:
|
|
6186
|
+
details: copyJson15(result.details),
|
|
6187
|
+
structuredOutput: copyJson15(result)
|
|
5928
6188
|
};
|
|
5929
6189
|
}
|
|
5930
6190
|
const result = await engine.control(request, api, context);
|
|
5931
6191
|
return {
|
|
5932
6192
|
output: [{ type: "text", text: result.message }],
|
|
5933
|
-
details:
|
|
5934
|
-
structuredOutput:
|
|
6193
|
+
details: copyJson15(result.details, { omitUndefinedProperties: true }),
|
|
6194
|
+
structuredOutput: copyJson15(result, { omitUndefinedProperties: true })
|
|
5935
6195
|
};
|
|
5936
6196
|
}
|
|
5937
6197
|
});
|
|
@@ -5946,12 +6206,8 @@ function createJsNotebookMode(options) {
|
|
|
5946
6206
|
sections: [
|
|
5947
6207
|
section("notebook", (input) => {
|
|
5948
6208
|
const base = 'exec: JavaScript with top-level await in a persistent QuickJS VM. Closures, classes and cyclic data survive checkpoints without replay. Bare values are discarded. Use text(value), image(value), notify(value), store(key,value), load(key), exit() and yield_control(). ALL_TOOLS lists selected registrations. Normal cells share project state. A leading // @exec: {"isolated":true} forks the last committed state and saves a candidate without adopting it. Whole-notebook profile loads replace live state. External effects are never rolled back. Node APIs and imports are unavailable inside the VM.';
|
|
5949
|
-
const promoted = input.agent.callable.filter((tool) => !["exec", "wait", "notebook"].includes(tool.name)).map(readToolContract).filter((contract) => !contract.nativeOnly && !contract.deferLoading).map((contract) => contract.prompt);
|
|
5950
|
-
return [
|
|
5951
|
-
base,
|
|
5952
|
-
"For tools listed without a call signature, call with no arguments to get usage without performing an operation. Structured calls use {}.",
|
|
5953
|
-
...promoted
|
|
5954
|
-
].join(`
|
|
6209
|
+
const promoted = input.agent.callable.filter((tool) => !["exec", "wait", "notebook"].includes(tool.name)).map(readToolContract).filter((contract) => !contract.nativeOnly && !contract.nestedHidden && !contract.deferLoading).map((contract) => contract.prompt);
|
|
6210
|
+
return [base, nestedToolDiscoveryGuidance, ...promoted].join(`
|
|
5955
6211
|
`);
|
|
5956
6212
|
})
|
|
5957
6213
|
]
|