@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/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// packages/js-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,
|
|
@@ -70,8 +70,8 @@ function parseInteger(value, name, minimum = 0, maximum = Number.MAX_SAFE_INTEGE
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
// packages/execution/src/cells.ts
|
|
73
|
-
import {
|
|
74
|
-
import {
|
|
73
|
+
import { withCancel as withCancel2 } from "@earendil-works/chord/context";
|
|
74
|
+
import { copyJson as copyJson12 } from "@earendil-works/chord/json";
|
|
75
75
|
import {
|
|
76
76
|
defineExtension
|
|
77
77
|
} from "@earendil-works/pi-durable";
|
|
@@ -82,7 +82,7 @@ import {
|
|
|
82
82
|
} from "@earendil-works/pi-durable";
|
|
83
83
|
|
|
84
84
|
// packages/execution/src/forwarding.ts
|
|
85
|
-
import { copyJson as copyJson2 } from "@earendil-works/chord";
|
|
85
|
+
import { copyJson as copyJson2 } from "@earendil-works/chord/json";
|
|
86
86
|
import {
|
|
87
87
|
LiveDoc,
|
|
88
88
|
NestedCallDoc,
|
|
@@ -92,7 +92,7 @@ import {
|
|
|
92
92
|
// packages/execution/src/invocation-state.ts
|
|
93
93
|
import {
|
|
94
94
|
copyJson
|
|
95
|
-
} from "@earendil-works/chord";
|
|
95
|
+
} from "@earendil-works/chord/json";
|
|
96
96
|
import {
|
|
97
97
|
defineDoc
|
|
98
98
|
} from "@earendil-works/pi-durable";
|
|
@@ -569,7 +569,7 @@ async function observeCell(kind, id, api, context, budget) {
|
|
|
569
569
|
}
|
|
570
570
|
|
|
571
571
|
// packages/execution/src/cell-progress.ts
|
|
572
|
-
import { copyJson as copyJson3 } from "@earendil-works/chord";
|
|
572
|
+
import { copyJson as copyJson3 } from "@earendil-works/chord/json";
|
|
573
573
|
import {
|
|
574
574
|
LiveDoc as LiveDoc2
|
|
575
575
|
} from "@earendil-works/pi-durable";
|
|
@@ -596,8 +596,14 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
596
596
|
return work;
|
|
597
597
|
};
|
|
598
598
|
const live = await api.watchDoc(LiveDoc2, api.conversationId, context);
|
|
599
|
-
cell.start((
|
|
600
|
-
|
|
599
|
+
cell.start((call) => own(async () => {
|
|
600
|
+
if (call.kind === "failed")
|
|
601
|
+
throw call.error;
|
|
602
|
+
await publish(call.value);
|
|
603
|
+
}));
|
|
604
|
+
live?.start((call) => own(async () => {
|
|
605
|
+
if (call.kind === "failed")
|
|
606
|
+
throw call.error;
|
|
601
607
|
await api.commit(async (tx) => {
|
|
602
608
|
const record = await tx.task(id);
|
|
603
609
|
if (record === undefined || record.state.status === "terminal")
|
|
@@ -617,43 +623,70 @@ async function observeCellProgress(id, api, context, budget) {
|
|
|
617
623
|
const endings = await Promise.all([cell.stop(), live?.stop()]);
|
|
618
624
|
await Promise.all([...pending]);
|
|
619
625
|
for (const ending of endings)
|
|
620
|
-
if (ending?.
|
|
626
|
+
if (ending?.kind === "listener_error" || ending?.kind === "failed")
|
|
621
627
|
throw ending.error;
|
|
622
628
|
};
|
|
623
629
|
}
|
|
624
630
|
|
|
625
631
|
// packages/execution/src/cell-task.ts
|
|
626
|
-
import { copyJson as copyJson8 } from "@earendil-works/chord";
|
|
627
632
|
import { withAbortSignal } from "@earendil-works/chord/context";
|
|
633
|
+
import { copyJson as copyJson8 } from "@earendil-works/chord/json";
|
|
628
634
|
import {
|
|
629
635
|
defineTask
|
|
630
636
|
} from "@earendil-works/pi-durable";
|
|
631
637
|
|
|
632
638
|
// packages/execution/src/cell-tools.ts
|
|
633
|
-
import { copyJson as copyJson6 } from "@earendil-works/chord";
|
|
634
639
|
import { withoutAbortSignal as withoutAbortSignal2 } from "@earendil-works/chord/context";
|
|
640
|
+
import { copyJson as copyJson6 } from "@earendil-works/chord/json";
|
|
635
641
|
|
|
636
642
|
// packages/execution/src/invocation.ts
|
|
637
|
-
import { copyJson as copyJson5 } from "@earendil-works/chord";
|
|
638
643
|
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
644
|
+
import { copyJson as copyJson5 } from "@earendil-works/chord/json";
|
|
645
|
+
import {
|
|
646
|
+
NestedCallDoc as NestedCallDoc3
|
|
647
|
+
} from "@earendil-works/pi-durable";
|
|
648
|
+
import { equal } from "@earendil-works/pi-schema";
|
|
639
649
|
|
|
640
650
|
// packages/execution/src/native-tools.ts
|
|
641
|
-
import {
|
|
642
|
-
|
|
651
|
+
import {
|
|
652
|
+
withoutAbortSignal
|
|
653
|
+
} from "@earendil-works/chord/context";
|
|
654
|
+
import { copyJson as copyJson4 } from "@earendil-works/chord/json";
|
|
643
655
|
import {
|
|
644
656
|
LiveDoc as LiveDoc3,
|
|
645
657
|
NestedCallDoc as NestedCallDoc2,
|
|
646
|
-
ToolTask as
|
|
658
|
+
ToolTask as ToolTask3
|
|
647
659
|
} from "@earendil-works/pi-durable";
|
|
648
660
|
import { Equal } from "typebox/value";
|
|
649
|
-
|
|
661
|
+
|
|
662
|
+
// packages/execution/src/tool-origin.ts
|
|
663
|
+
import {
|
|
664
|
+
defineDoc as defineDoc2,
|
|
665
|
+
ToolTask as ToolTask2
|
|
666
|
+
} from "@earendil-works/pi-durable";
|
|
667
|
+
var ToolOriginDoc = defineDoc2({
|
|
668
|
+
kind: "howaboua.tool-origin",
|
|
669
|
+
version: 1,
|
|
670
|
+
scope: "task",
|
|
671
|
+
checkpointWhen: () => false,
|
|
672
|
+
initial: () => ({})
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
// packages/execution/src/native-tools.ts
|
|
676
|
+
function assertNestedKey(key) {
|
|
677
|
+
if (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key))
|
|
678
|
+
throw new Error("Invalid nested call or task key");
|
|
679
|
+
}
|
|
680
|
+
function nativeToolCalls(runtime, callId, replay, resumed, assertLive, context) {
|
|
650
681
|
let sequence = 0;
|
|
651
682
|
const admissions = [];
|
|
652
|
-
|
|
683
|
+
async function execute(name, args, callContext, options = {}) {
|
|
653
684
|
assertLive();
|
|
685
|
+
if (options.key !== undefined)
|
|
686
|
+
assertNestedKey(options.key);
|
|
687
|
+
else if (resumed)
|
|
688
|
+
throw new Error("Nested calls require an explicit key after waking");
|
|
654
689
|
const key = options.key ?? String(++sequence);
|
|
655
|
-
if (options.key !== undefined && (!key || key.includes("/") || key === "__proto__" || /^[1-9][0-9]*$/.test(key)))
|
|
656
|
-
throw new Error("Invalid nested call key");
|
|
657
690
|
const call = {
|
|
658
691
|
type: "toolCall",
|
|
659
692
|
id: `${callId}/${key}`,
|
|
@@ -665,11 +698,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
665
698
|
const admission = runtime.commit(async (tx) => {
|
|
666
699
|
const slot = await tx.doc(NestedCallDoc2, runtime.taskId, key, null);
|
|
667
700
|
if (slot.taskId !== undefined) {
|
|
668
|
-
const input = (await tx.task(slot.taskId))?.input;
|
|
701
|
+
const input = slot.kind === "task" ? undefined : (await tx.task(slot.taskId))?.input;
|
|
669
702
|
if (input?.kind !== "nested" || input.call.name !== name || !Equal(input.call.arguments, call.arguments))
|
|
670
703
|
throw new Error(`Nested call ${call.id} was already made with another tool or other arguments`);
|
|
671
704
|
} else {
|
|
672
|
-
slot.
|
|
705
|
+
slot.kind = "nested";
|
|
706
|
+
slot.taskId = await tx.createTask(ToolTask3, {
|
|
673
707
|
kind: "nested",
|
|
674
708
|
parent: runtime.taskId,
|
|
675
709
|
parentCallId: callId,
|
|
@@ -681,6 +715,9 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
681
715
|
abandonOnRestart: replay !== "safe"
|
|
682
716
|
});
|
|
683
717
|
const live = await tx.doc(LiveDoc3, runtime.conversationId);
|
|
718
|
+
const origin = (await tx.doc(ToolOriginDoc, runtime.taskId)).origin;
|
|
719
|
+
if (origin)
|
|
720
|
+
(await tx.doc(ToolOriginDoc, slot.taskId)).origin = { ...origin };
|
|
684
721
|
live.nestedTools ??= [];
|
|
685
722
|
live.nestedTools.push({
|
|
686
723
|
callId: call.id,
|
|
@@ -712,7 +749,13 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
712
749
|
const slot = await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext);
|
|
713
750
|
if (slot?.taskId === undefined)
|
|
714
751
|
throw new Error("Nested task creation did not commit");
|
|
715
|
-
const
|
|
752
|
+
const taskId = slot.taskId;
|
|
753
|
+
const result = (resultContext) => readResult(name, key, taskId, resultContext);
|
|
754
|
+
return options.wait === false ? { taskId, result } : result(callContext);
|
|
755
|
+
}
|
|
756
|
+
async function readResult(name, key, taskId, callContext) {
|
|
757
|
+
assertLive();
|
|
758
|
+
const settled = await runtime.waitForTask(taskId, callContext);
|
|
716
759
|
const result = (await runtime.snapshot(NestedCallDoc2, runtime.taskId, key, callContext))?.result;
|
|
717
760
|
await runtime.commit(async (tx) => {
|
|
718
761
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
@@ -720,7 +763,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
720
763
|
return;
|
|
721
764
|
}, callContext);
|
|
722
765
|
return result === undefined ? {
|
|
723
|
-
taskId
|
|
766
|
+
taskId,
|
|
724
767
|
isError: true,
|
|
725
768
|
diagnostics: [
|
|
726
769
|
{
|
|
@@ -730,9 +773,12 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
730
773
|
}
|
|
731
774
|
]
|
|
732
775
|
} : copyJson4(result);
|
|
733
|
-
}
|
|
776
|
+
}
|
|
734
777
|
return {
|
|
735
778
|
execute,
|
|
779
|
+
async drain() {
|
|
780
|
+
await Promise.allSettled(admissions);
|
|
781
|
+
},
|
|
736
782
|
async finish() {
|
|
737
783
|
await Promise.allSettled(admissions);
|
|
738
784
|
await cleanupNativeCalls(runtime, withoutAbortSignal(context));
|
|
@@ -740,7 +786,7 @@ function nativeToolCalls(runtime, callId, replay, assertLive, context) {
|
|
|
740
786
|
};
|
|
741
787
|
}
|
|
742
788
|
async function cleanupNativeCalls(runtime, context) {
|
|
743
|
-
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind ===
|
|
789
|
+
await Promise.all((await runtime.ownedTasks(context)).filter((child) => child.kind === ToolTask3.definition.name).map((child) => runtime.abortOwned(child.id, context)));
|
|
744
790
|
await runtime.commit(async (tx) => {
|
|
745
791
|
const owner = await tx.doc(InvocationDoc, runtime.taskId);
|
|
746
792
|
await captureNativeCalls(tx, runtime.conversationId, runtime.taskId, owner.forwardingCellId);
|
|
@@ -834,18 +880,6 @@ class Progress {
|
|
|
834
880
|
}
|
|
835
881
|
}
|
|
836
882
|
|
|
837
|
-
// packages/execution/src/tool-origin.ts
|
|
838
|
-
import {
|
|
839
|
-
defineDoc as defineDoc2
|
|
840
|
-
} from "@earendil-works/pi-durable";
|
|
841
|
-
var ToolOriginDoc = defineDoc2({
|
|
842
|
-
kind: "howaboua.tool-origin",
|
|
843
|
-
version: 1,
|
|
844
|
-
scope: "task",
|
|
845
|
-
checkpointWhen: () => false,
|
|
846
|
-
initial: () => ({})
|
|
847
|
-
});
|
|
848
|
-
|
|
849
883
|
// packages/execution/src/invocation.ts
|
|
850
884
|
function executionError(code, message) {
|
|
851
885
|
return {
|
|
@@ -857,21 +891,23 @@ function executionError(code, message) {
|
|
|
857
891
|
function errorText(error) {
|
|
858
892
|
return error instanceof Error ? error.message : String(error);
|
|
859
893
|
}
|
|
860
|
-
function createInvocation(runtime, tool, context) {
|
|
894
|
+
function createInvocation(runtime, tool, context, resume) {
|
|
861
895
|
const limits = {
|
|
862
896
|
maxBytes: tool.outputLimits?.maxBytes ?? 50 * 1024,
|
|
863
897
|
maxLines: tool.outputLimits?.maxLines ?? 2000,
|
|
864
898
|
retain: tool.outputLimits?.retain ?? "head"
|
|
865
899
|
};
|
|
866
900
|
const output = new OutputBuffer(limits);
|
|
867
|
-
const diagnostics = [
|
|
868
|
-
|
|
901
|
+
const diagnostics = [
|
|
902
|
+
...resume?.progress?.diagnostics ?? []
|
|
903
|
+
];
|
|
904
|
+
let details = resume?.progress?.details;
|
|
869
905
|
let ended = false;
|
|
870
906
|
const assertLive = () => {
|
|
871
907
|
if (ended)
|
|
872
908
|
throw new Error(`Invocation ${runtime.taskId} has settled`);
|
|
873
909
|
};
|
|
874
|
-
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, assertLive, context);
|
|
910
|
+
const native = nativeToolCalls(runtime, `nested-${runtime.taskId}`, tool.replay, resume?.wake !== undefined, assertLive, context);
|
|
875
911
|
const snapshot = () => {
|
|
876
912
|
const retained = output.snapshot();
|
|
877
913
|
return {
|
|
@@ -900,6 +936,8 @@ function createInvocation(runtime, tool, context) {
|
|
|
900
936
|
taskId: runtime.taskId,
|
|
901
937
|
conversationId: runtime.conversationId,
|
|
902
938
|
callId: `nested-${runtime.taskId}`,
|
|
939
|
+
wake: resume?.wake,
|
|
940
|
+
checkpoint: resume?.checkpoint,
|
|
903
941
|
registry: runtime.registry,
|
|
904
942
|
agent: runtime.agent,
|
|
905
943
|
env: undefined,
|
|
@@ -920,7 +958,7 @@ function createInvocation(runtime, tool, context) {
|
|
|
920
958
|
const retained = output.snapshot();
|
|
921
959
|
return { text: retained.text, truncated: retained.droppedBytes > 0 };
|
|
922
960
|
},
|
|
923
|
-
executeTool:
|
|
961
|
+
executeTool: native.execute,
|
|
924
962
|
diagnostic: (value) => {
|
|
925
963
|
assertLive();
|
|
926
964
|
diagnostics.push(copyJson5(value, { omitUndefinedProperties: true }));
|
|
@@ -934,24 +972,44 @@ function createInvocation(runtime, tool, context) {
|
|
|
934
972
|
committed.catch(() => {});
|
|
935
973
|
await awaitWithContext(committed, detailContext);
|
|
936
974
|
},
|
|
937
|
-
commit: async (change, commitContext) => {
|
|
975
|
+
commit: async (change, commitContext, options) => {
|
|
938
976
|
assertLive();
|
|
939
977
|
let result;
|
|
940
978
|
await runtime.commit(async (tx) => {
|
|
941
979
|
result = await change(tx);
|
|
942
980
|
return;
|
|
943
|
-
}, commitContext);
|
|
981
|
+
}, commitContext, options);
|
|
944
982
|
return result;
|
|
945
983
|
},
|
|
946
984
|
memo: runtime.memo,
|
|
947
|
-
|
|
985
|
+
readSignal: runtime.readSignal,
|
|
986
|
+
async createTask(task, input, {
|
|
987
|
+
key,
|
|
988
|
+
...options
|
|
989
|
+
}, taskContext) {
|
|
948
990
|
assertLive();
|
|
991
|
+
if (key !== undefined)
|
|
992
|
+
assertNestedKey(key);
|
|
993
|
+
else if (resume?.wake !== undefined)
|
|
994
|
+
throw new Error("Child tasks require an explicit key after waking");
|
|
949
995
|
let id;
|
|
950
996
|
await runtime.commit(async (tx) => {
|
|
997
|
+
const member = key === undefined ? undefined : await tx.doc(NestedCallDoc3, runtime.taskId, key, null);
|
|
998
|
+
if (member?.taskId !== undefined) {
|
|
999
|
+
const existing = member.kind === "task" ? await tx.task(member.taskId) : undefined;
|
|
1000
|
+
if (existing?.kind !== task.definition.name || !equal(existing.input, copyJson5(input, { omitUndefinedProperties: true })))
|
|
1001
|
+
throw new Error(`Key ${key} was already used for another call or task`);
|
|
1002
|
+
id = member.taskId;
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
951
1005
|
id = await tx.createTask(task, input, {
|
|
952
1006
|
...options,
|
|
953
1007
|
...options.abandonOnRestart === undefined && tool.replay !== "safe" && options.ownership.kind === "task" && options.ownership.taskId === runtime.taskId ? { abandonOnRestart: true } : {}
|
|
954
1008
|
});
|
|
1009
|
+
if (member !== undefined) {
|
|
1010
|
+
member.kind = "task";
|
|
1011
|
+
member.taskId = id;
|
|
1012
|
+
}
|
|
955
1013
|
const source = await tx.doc(ToolOriginDoc, runtime.taskId);
|
|
956
1014
|
if (source.origin !== undefined)
|
|
957
1015
|
(await tx.doc(ToolOriginDoc, id)).origin = { ...source.origin };
|
|
@@ -998,6 +1056,14 @@ function createInvocation(runtime, tool, context) {
|
|
|
998
1056
|
for (const waiter of await progress.stop())
|
|
999
1057
|
waiter.reject(error);
|
|
1000
1058
|
},
|
|
1059
|
+
async park() {
|
|
1060
|
+
ended = true;
|
|
1061
|
+
await native.drain();
|
|
1062
|
+
output.end();
|
|
1063
|
+
const pending = await progress.stop();
|
|
1064
|
+
const { output: _output, ...kept } = snapshot();
|
|
1065
|
+
return { progress: storeResult(kept), pending };
|
|
1066
|
+
},
|
|
1001
1067
|
limits,
|
|
1002
1068
|
output,
|
|
1003
1069
|
progress
|
|
@@ -1099,7 +1165,18 @@ function combineControls(controls, allowTermination = true) {
|
|
|
1099
1165
|
}
|
|
1100
1166
|
|
|
1101
1167
|
// packages/execution/src/tool-contract.ts
|
|
1102
|
-
import { copyJson as copyJson7 } from "@earendil-works/chord";
|
|
1168
|
+
import { copyJson as copyJson7 } from "@earendil-works/chord/json";
|
|
1169
|
+
|
|
1170
|
+
// packages/execution/src/tool-namespaces.ts
|
|
1171
|
+
import { isJsonValue } from "@earendil-works/chord/json";
|
|
1172
|
+
function validateJsToolNamespace(tool, namespace) {
|
|
1173
|
+
const key = (value) => typeof value === "string" && /^[a-zA-Z_$][\w$]*$/.test(value);
|
|
1174
|
+
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))))
|
|
1175
|
+
throw new Error(`Tool ${tool} has invalid jsNamespace metadata`);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
// packages/execution/src/tool-contract.ts
|
|
1179
|
+
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 {}";
|
|
1103
1180
|
function readToolContract(tool) {
|
|
1104
1181
|
const candidate = "executionHints" in tool ? tool.executionHints : undefined;
|
|
1105
1182
|
if (candidate !== undefined && (candidate === null || typeof candidate !== "object" || Array.isArray(candidate))) {
|
|
@@ -1113,6 +1190,7 @@ function readToolContract(tool) {
|
|
|
1113
1190
|
"yieldTimeMs",
|
|
1114
1191
|
"inputSchema",
|
|
1115
1192
|
"nativeOnly",
|
|
1193
|
+
"nestedHidden",
|
|
1116
1194
|
"discovery"
|
|
1117
1195
|
]);
|
|
1118
1196
|
for (const key of Object.keys(hints))
|
|
@@ -1136,6 +1214,12 @@ function readToolContract(tool) {
|
|
|
1136
1214
|
const nativeOnly = "nativeOnly" in hints ? hints.nativeOnly : undefined;
|
|
1137
1215
|
if (nativeOnly !== undefined && typeof nativeOnly !== "boolean")
|
|
1138
1216
|
throw new Error(`Tool ${tool.name} executionHints.nativeOnly must be a boolean`);
|
|
1217
|
+
const nestedHidden = "nestedHidden" in hints ? hints.nestedHidden : undefined;
|
|
1218
|
+
const jsNamespace = "jsNamespace" in tool ? tool.jsNamespace : undefined;
|
|
1219
|
+
if (jsNamespace !== undefined)
|
|
1220
|
+
validateJsToolNamespace(tool.name, jsNamespace);
|
|
1221
|
+
if (nestedHidden !== undefined && typeof nestedHidden !== "boolean")
|
|
1222
|
+
throw new Error(`Tool ${tool.name} executionHints.nestedHidden must be a boolean`);
|
|
1139
1223
|
const yieldTimeMs = "yieldTimeMs" in hints ? hints.yieldTimeMs : undefined;
|
|
1140
1224
|
if (yieldTimeMs !== undefined && (typeof yieldTimeMs !== "number" || !Number.isSafeInteger(yieldTimeMs) || yieldTimeMs < 0))
|
|
1141
1225
|
throw new Error(`Tool ${tool.name} executionHints.yieldTimeMs must be a non-negative safe integer`);
|
|
@@ -1152,6 +1236,8 @@ function readToolContract(tool) {
|
|
|
1152
1236
|
inputSchema,
|
|
1153
1237
|
deferLoading: typeof deferred === "boolean" ? deferred : true,
|
|
1154
1238
|
nativeOnly: nativeOnly === true,
|
|
1239
|
+
nestedHidden: nestedHidden === true,
|
|
1240
|
+
...jsNamespace === undefined ? {} : { jsNamespace },
|
|
1155
1241
|
...typeof yieldTimeMs === "number" ? { yieldTimeMs } : {},
|
|
1156
1242
|
...outputHint === undefined ? {} : { output: outputHint },
|
|
1157
1243
|
help: [
|
|
@@ -1387,7 +1473,7 @@ function createCellTask(options, nestedTask) {
|
|
|
1387
1473
|
import {
|
|
1388
1474
|
hook,
|
|
1389
1475
|
LiveDoc as LiveDoc4,
|
|
1390
|
-
ToolTask as
|
|
1476
|
+
ToolTask as ToolTask4
|
|
1391
1477
|
} from "@earendil-works/pi-durable";
|
|
1392
1478
|
function forwardingHook() {
|
|
1393
1479
|
let host;
|
|
@@ -1397,7 +1483,7 @@ function forwardingHook() {
|
|
|
1397
1483
|
throw new Error("Create a separate execution component for each Harness");
|
|
1398
1484
|
host = harness;
|
|
1399
1485
|
},
|
|
1400
|
-
hook: hook(
|
|
1486
|
+
hook: hook(ToolTask4, {
|
|
1401
1487
|
async beforeTool(call, api, context) {
|
|
1402
1488
|
const parent = call.parent;
|
|
1403
1489
|
if (parent === undefined || host === undefined)
|
|
@@ -1410,7 +1496,7 @@ function forwardingHook() {
|
|
|
1410
1496
|
if (conversation === undefined)
|
|
1411
1497
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1412
1498
|
await conversation.commit(async (tx) => {
|
|
1413
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1499
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1414
1500
|
return;
|
|
1415
1501
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1416
1502
|
const admission = owner.forwardingCellId === undefined;
|
|
@@ -1437,7 +1523,7 @@ function forwardingHook() {
|
|
|
1437
1523
|
if (conversation === undefined)
|
|
1438
1524
|
throw new Error("Forwarded call conversation is unavailable");
|
|
1439
1525
|
await conversation.commit(async (tx) => {
|
|
1440
|
-
if ((await tx.task(api.taskId))?.kind !==
|
|
1526
|
+
if ((await tx.task(api.taskId))?.kind !== ToolTask4.definition.name)
|
|
1441
1527
|
return;
|
|
1442
1528
|
const owner = await tx.doc(InvocationDoc, api.taskId);
|
|
1443
1529
|
const cellId = owner.forwardingCellId;
|
|
@@ -1462,17 +1548,18 @@ function forwardingHook() {
|
|
|
1462
1548
|
}
|
|
1463
1549
|
|
|
1464
1550
|
// packages/execution/src/nested.ts
|
|
1551
|
+
import { copyJson as copyJson11 } from "@earendil-works/chord/json";
|
|
1465
1552
|
import {
|
|
1466
1553
|
defineTask as defineTask2
|
|
1467
1554
|
} from "@earendil-works/pi-durable";
|
|
1468
|
-
import {
|
|
1555
|
+
import { compile as compile2 } from "@earendil-works/pi-schema";
|
|
1469
1556
|
|
|
1470
1557
|
// packages/execution/src/nested-call.ts
|
|
1471
1558
|
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
1472
1559
|
import {
|
|
1473
|
-
ToolTask as
|
|
1560
|
+
ToolTask as ToolTask5
|
|
1474
1561
|
} from "@earendil-works/pi-durable";
|
|
1475
|
-
import {
|
|
1562
|
+
import { compile } from "@earendil-works/pi-schema";
|
|
1476
1563
|
function nestedCall(runtime, input) {
|
|
1477
1564
|
const args = input.arguments;
|
|
1478
1565
|
return {
|
|
@@ -1493,7 +1580,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1493
1580
|
if (typeof input !== "string")
|
|
1494
1581
|
return input;
|
|
1495
1582
|
const contract = readToolContract(tool);
|
|
1496
|
-
if (Boolean(
|
|
1583
|
+
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)))
|
|
1497
1584
|
return input;
|
|
1498
1585
|
const text = input.trim();
|
|
1499
1586
|
if (contract.discovery === "help" && text === "help")
|
|
@@ -1516,7 +1603,7 @@ function prepareNestedInput(tool, input) {
|
|
|
1516
1603
|
async function eachHook(agent, name, api, invoke) {
|
|
1517
1604
|
for (const extension of agent.extensions)
|
|
1518
1605
|
for (const registration of extension.hooks ?? []) {
|
|
1519
|
-
if (registration.task !==
|
|
1606
|
+
if (registration.task !== ToolTask5.definition.name)
|
|
1520
1607
|
continue;
|
|
1521
1608
|
const handler = registration.handlers[name];
|
|
1522
1609
|
if (handler === undefined)
|
|
@@ -1531,8 +1618,120 @@ async function eachHook(agent, name, api, invoke) {
|
|
|
1531
1618
|
}
|
|
1532
1619
|
}
|
|
1533
1620
|
|
|
1621
|
+
// packages/execution/src/nested-park.ts
|
|
1622
|
+
import {
|
|
1623
|
+
withCancel,
|
|
1624
|
+
withoutAbortSignal as withoutAbortSignal3
|
|
1625
|
+
} from "@earendil-works/chord/context";
|
|
1626
|
+
import { copyJson as copyJson9 } from "@earendil-works/chord/json";
|
|
1627
|
+
var resultFields = [
|
|
1628
|
+
"output",
|
|
1629
|
+
"structuredOutput",
|
|
1630
|
+
"isError",
|
|
1631
|
+
"details",
|
|
1632
|
+
"diagnostics",
|
|
1633
|
+
"usage",
|
|
1634
|
+
"control"
|
|
1635
|
+
];
|
|
1636
|
+
function sleepState(checkpoint, { until, signals }) {
|
|
1637
|
+
return {
|
|
1638
|
+
status: "sleeping",
|
|
1639
|
+
checkpoint,
|
|
1640
|
+
...until === undefined ? {} : { until },
|
|
1641
|
+
...signals === undefined ? {} : { signals }
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
async function parkGate(runtime, sleep, context) {
|
|
1645
|
+
try {
|
|
1646
|
+
await runtime.commit(() => sleepState({ phase: "call" }, sleep), context);
|
|
1647
|
+
} catch (error) {
|
|
1648
|
+
runtime.signal.throwIfAborted();
|
|
1649
|
+
return errorText(error);
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
async function parkTool(runtime, invocation, tool, args, park, context) {
|
|
1653
|
+
if (park.kind !== "sleep" && park.kind !== "wait")
|
|
1654
|
+
return "Unknown park kind";
|
|
1655
|
+
const mixed = resultFields.find((field) => (field in park));
|
|
1656
|
+
if (mixed !== undefined)
|
|
1657
|
+
return `A park cannot carry the result field ${mixed}`;
|
|
1658
|
+
const intent = {
|
|
1659
|
+
arguments: args,
|
|
1660
|
+
replay: tool.replay ?? "unsafe",
|
|
1661
|
+
...park.checkpoint === undefined ? {} : { checkpoint: copyJson9(park.checkpoint) }
|
|
1662
|
+
};
|
|
1663
|
+
const { progress, pending } = await invocation.park();
|
|
1664
|
+
try {
|
|
1665
|
+
await runtime.commit(async (tx) => {
|
|
1666
|
+
if (park.kind === "wait") {
|
|
1667
|
+
for (const id of park.on)
|
|
1668
|
+
if ((await tx.task(id))?.owner !== runtime.taskId)
|
|
1669
|
+
throw new Error(`Task ${id} is not owned by the call`);
|
|
1670
|
+
}
|
|
1671
|
+
const checkpoint = { ...intent, phase: "parked" };
|
|
1672
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1673
|
+
doc.result = progress;
|
|
1674
|
+
doc.droppedBytes = 0;
|
|
1675
|
+
doc.droppedLines = 0;
|
|
1676
|
+
await forwardInvocationResult(tx, runtime.taskId, progress, "running");
|
|
1677
|
+
return park.kind === "sleep" ? sleepState(checkpoint, park) : { status: "waiting", checkpoint, on: park.on, policy: park.policy };
|
|
1678
|
+
}, context);
|
|
1679
|
+
} catch (error) {
|
|
1680
|
+
for (const waiter of pending)
|
|
1681
|
+
waiter.reject(error);
|
|
1682
|
+
const detached = withoutAbortSignal3(context);
|
|
1683
|
+
const record = await runtime.getTask(runtime.taskId, detached);
|
|
1684
|
+
if (record?.abortRequested) {
|
|
1685
|
+
const watch = await runtime.watchDoc(InvocationDoc, runtime.taskId, detached);
|
|
1686
|
+
if (watch !== undefined) {
|
|
1687
|
+
const cleanup = withCancel(detached);
|
|
1688
|
+
watch.closed.then((end) => cleanup.cancel(end.kind === "failed" ? end.error : undefined));
|
|
1689
|
+
try {
|
|
1690
|
+
await cancelParkedTool(runtime, tool, intent, cleanup.context);
|
|
1691
|
+
} finally {
|
|
1692
|
+
await watch.stop();
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
throw error;
|
|
1696
|
+
}
|
|
1697
|
+
runtime.signal.throwIfAborted();
|
|
1698
|
+
return errorText(error);
|
|
1699
|
+
}
|
|
1700
|
+
for (const waiter of pending)
|
|
1701
|
+
waiter.resolve();
|
|
1702
|
+
}
|
|
1703
|
+
async function cancelParkedTool(runtime, tool, intent, context) {
|
|
1704
|
+
if (!tool) {
|
|
1705
|
+
runtime.report(new Error("The parked tool is no longer registered; cleanup could not run"));
|
|
1706
|
+
return;
|
|
1707
|
+
}
|
|
1708
|
+
if (tool.cancel === undefined)
|
|
1709
|
+
return;
|
|
1710
|
+
const cleanup = createInvocation(runtime, tool, context, {
|
|
1711
|
+
checkpoint: intent.checkpoint,
|
|
1712
|
+
wake: undefined
|
|
1713
|
+
});
|
|
1714
|
+
const refuseWork = () => {
|
|
1715
|
+
throw new Error("A tool cannot start tasks or nested calls during cancellation");
|
|
1716
|
+
};
|
|
1717
|
+
let reason = new Error("Tool cancellation finished");
|
|
1718
|
+
try {
|
|
1719
|
+
const api = await cleanup.executionApi();
|
|
1720
|
+
await tool.cancel(intent.arguments, {
|
|
1721
|
+
...api,
|
|
1722
|
+
createTask: refuseWork,
|
|
1723
|
+
executeTool: refuseWork
|
|
1724
|
+
}, context);
|
|
1725
|
+
} catch (error) {
|
|
1726
|
+
reason = error;
|
|
1727
|
+
runtime.report(error);
|
|
1728
|
+
} finally {
|
|
1729
|
+
await cleanup.stop(reason);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1534
1733
|
// packages/execution/src/nested-result.ts
|
|
1535
|
-
import { copyJson as
|
|
1734
|
+
import { copyJson as copyJson10 } from "@earendil-works/chord/json";
|
|
1536
1735
|
import {
|
|
1537
1736
|
defineEntry,
|
|
1538
1737
|
UsageDoc
|
|
@@ -1580,11 +1779,11 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1580
1779
|
taskId: runtime.taskId,
|
|
1581
1780
|
callId: call.id,
|
|
1582
1781
|
name: call.name,
|
|
1583
|
-
arguments:
|
|
1782
|
+
arguments: copyJson10(call.arguments),
|
|
1584
1783
|
result: stored,
|
|
1585
1784
|
...current.state.checkpoint.phase === "execute" ? {
|
|
1586
1785
|
intent: {
|
|
1587
|
-
arguments:
|
|
1786
|
+
arguments: copyJson10(current.state.checkpoint.arguments),
|
|
1588
1787
|
replay: current.state.checkpoint.replay
|
|
1589
1788
|
}
|
|
1590
1789
|
} : {}
|
|
@@ -1593,7 +1792,7 @@ async function settle(runtime, call, result, ending, context) {
|
|
|
1593
1792
|
if (result.usage !== undefined) {
|
|
1594
1793
|
const totals = (await tx.doc(UsageDoc, runtime.conversationId)).tools;
|
|
1595
1794
|
const previous = Object.hasOwn(totals, call.name) ? totals[call.name] : undefined;
|
|
1596
|
-
totals[call.name] =
|
|
1795
|
+
totals[call.name] = copyJson10(previous === undefined ? result.usage : sumUsage(previous, result.usage), { omitUndefinedProperties: true });
|
|
1597
1796
|
}
|
|
1598
1797
|
const outcome = ending === "completed" ? { status: ending, result: stored } : ending === "aborted" ? { status: ending, result: stored } : {
|
|
1599
1798
|
status: ending,
|
|
@@ -1630,7 +1829,11 @@ function sumUsage(previous, added) {
|
|
|
1630
1829
|
function createNestedToolTask(name) {
|
|
1631
1830
|
return defineTask2({
|
|
1632
1831
|
name,
|
|
1633
|
-
version:
|
|
1832
|
+
version: 2,
|
|
1833
|
+
migrate: (input, checkpoint) => ({
|
|
1834
|
+
input,
|
|
1835
|
+
checkpoint
|
|
1836
|
+
}),
|
|
1634
1837
|
initial: () => ({ phase: "call" }),
|
|
1635
1838
|
phases: {
|
|
1636
1839
|
call: async (task, runtime, context) => {
|
|
@@ -1656,22 +1859,25 @@ function createNestedToolTask(name) {
|
|
|
1656
1859
|
return reject(runtime, call, executionError("tool_unavailable", `Tool ${call.name} is not available`), agent, tool, context);
|
|
1657
1860
|
let args;
|
|
1658
1861
|
try {
|
|
1659
|
-
const supplied = task.input.arguments === null && !
|
|
1862
|
+
const supplied = task.input.arguments === null && !compile2(tool.parameters).check(null) && compile2(tool.parameters).check({}) ? undefined : task.input.arguments;
|
|
1660
1863
|
const prepared = tool.prepareArguments === undefined ? prepareNestedInput(tool, supplied) : tool.prepareArguments(prepareNestedInput(tool, supplied));
|
|
1661
|
-
args = checked(tool, call, prepared === undefined &&
|
|
1864
|
+
args = checked(tool, call, prepared === undefined && compile2(tool.parameters).check({}) ? {} : prepared);
|
|
1662
1865
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1663
1866
|
call.arguments = args;
|
|
1664
1867
|
} catch (error) {
|
|
1665
1868
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1666
1869
|
}
|
|
1667
1870
|
let block;
|
|
1871
|
+
let sleep;
|
|
1668
1872
|
await eachHook(agent, "beforeTool", runtime, async (handler) => {
|
|
1669
|
-
if (block !== undefined)
|
|
1873
|
+
if (block !== undefined || sleep !== undefined)
|
|
1670
1874
|
return;
|
|
1671
1875
|
try {
|
|
1672
1876
|
const decision = await handler({ ...call, arguments: args }, runtime, context);
|
|
1673
1877
|
if (decision?.block !== undefined)
|
|
1674
1878
|
block = decision.block;
|
|
1879
|
+
else if (decision?.sleep !== undefined)
|
|
1880
|
+
sleep = decision.sleep;
|
|
1675
1881
|
else if (decision?.arguments !== undefined)
|
|
1676
1882
|
args = decision.arguments;
|
|
1677
1883
|
} catch (error) {
|
|
@@ -1682,71 +1888,122 @@ function createNestedToolTask(name) {
|
|
|
1682
1888
|
});
|
|
1683
1889
|
if (block !== undefined)
|
|
1684
1890
|
return reject(runtime, call, executionError("blocked", `Tool call blocked: ${block}`), agent, tool, context);
|
|
1891
|
+
if (sleep !== undefined) {
|
|
1892
|
+
const reason = await parkGate(runtime, sleep, context);
|
|
1893
|
+
if (reason === undefined)
|
|
1894
|
+
return;
|
|
1895
|
+
return reject(runtime, call, executionError("invalid_park", reason), agent, tool, context);
|
|
1896
|
+
}
|
|
1685
1897
|
try {
|
|
1686
|
-
args = checked(tool, call, args)
|
|
1898
|
+
args = copyJson11(checked(tool, call, args), {
|
|
1899
|
+
omitUndefinedProperties: true
|
|
1900
|
+
});
|
|
1687
1901
|
} catch (error) {
|
|
1688
1902
|
return reject(runtime, call, executionError("invalid_arguments", errorText(error)), agent, tool, context);
|
|
1689
1903
|
}
|
|
1690
|
-
await runtime
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
callId: call.id,
|
|
1694
|
-
parentCallId: call.parent?.callId ?? `nested-${runtime.taskId}`,
|
|
1695
|
-
name: call.name,
|
|
1696
|
-
arguments: args,
|
|
1697
|
-
status: "running",
|
|
1698
|
-
result: null,
|
|
1699
|
-
omittedFields: []
|
|
1700
|
-
});
|
|
1701
|
-
return {
|
|
1702
|
-
status: "running",
|
|
1703
|
-
checkpoint: {
|
|
1704
|
-
phase: "execute",
|
|
1705
|
-
arguments: args,
|
|
1706
|
-
replay: tool.replay ?? "unsafe"
|
|
1707
|
-
}
|
|
1708
|
-
};
|
|
1904
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1905
|
+
arguments: args,
|
|
1906
|
+
replay: tool.replay ?? "unsafe"
|
|
1709
1907
|
}, context);
|
|
1710
|
-
|
|
1908
|
+
},
|
|
1909
|
+
parked: async (task, runtime, context) => {
|
|
1910
|
+
const intent = task.state.checkpoint;
|
|
1911
|
+
const call = nestedCall(runtime, task.input);
|
|
1912
|
+
call.arguments = intent.arguments;
|
|
1913
|
+
const agent = await runtime.agent(context);
|
|
1914
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1915
|
+
if (tool === undefined || readToolContract(tool).nativeOnly)
|
|
1916
|
+
return interrupted(runtime, call, "interrupted", `Parked tool ${call.name} is no longer available and could not be resumed`, context);
|
|
1917
|
+
await startAttempt(runtime, call, tool, agent, {
|
|
1918
|
+
...intent,
|
|
1919
|
+
replay: tool.replay ?? "unsafe"
|
|
1920
|
+
}, context, task.state.wake);
|
|
1711
1921
|
},
|
|
1712
1922
|
execute: async (task, runtime, context) => {
|
|
1923
|
+
const intent = task.state.checkpoint;
|
|
1713
1924
|
const call = nestedCall(runtime, task.input);
|
|
1714
1925
|
if (task.input.arguments === null || typeof task.input.arguments !== "object" || Array.isArray(task.input.arguments))
|
|
1715
1926
|
call.arguments = task.state.checkpoint.arguments;
|
|
1716
1927
|
const agent = await runtime.agent(context);
|
|
1717
1928
|
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1718
|
-
if (
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1929
|
+
if (intent.replay === "safe" && tool?.replay === "safe" && !readToolContract(tool).nativeOnly) {
|
|
1930
|
+
if (intent.resume === undefined)
|
|
1931
|
+
await runtime.commit(async (tx) => {
|
|
1932
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1933
|
+
doc.result = {};
|
|
1934
|
+
doc.droppedBytes = 0;
|
|
1935
|
+
doc.droppedLines = 0;
|
|
1936
|
+
return;
|
|
1937
|
+
}, context);
|
|
1938
|
+
return run(runtime, call, tool, intent, agent, context, intent.resume?.wake);
|
|
1727
1939
|
}
|
|
1940
|
+
if (intent.resume !== undefined)
|
|
1941
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1728
1942
|
await interrupted(runtime, call, "interrupted", `Tool ${call.name} was interrupted and may have partially run`, context);
|
|
1729
1943
|
}
|
|
1730
1944
|
},
|
|
1731
1945
|
abort: async (task, runtime, context) => {
|
|
1732
1946
|
const call = nestedCall(runtime, task.input);
|
|
1947
|
+
const intent = task.state.checkpoint;
|
|
1948
|
+
if (intent.phase === "parked" || intent.phase === "execute" && intent.resume !== undefined) {
|
|
1949
|
+
const agent = await runtime.agent(context);
|
|
1950
|
+
const tool = agent.callable.find((each) => each.name === call.name);
|
|
1951
|
+
await cancelParkedTool(runtime, tool, intent, context);
|
|
1952
|
+
}
|
|
1733
1953
|
await interrupted(runtime, call, "aborted", `Tool ${call.name} was aborted${task.state.checkpoint.phase === "execute" ? " and may have partially run" : ""}`, context);
|
|
1734
1954
|
}
|
|
1735
1955
|
});
|
|
1736
1956
|
}
|
|
1957
|
+
async function startAttempt(runtime, call, tool, agent, intent, context, wake) {
|
|
1958
|
+
await runtime.commit(async (tx) => {
|
|
1959
|
+
const doc = await tx.doc(InvocationDoc, runtime.taskId);
|
|
1960
|
+
await forwardCall(tx, doc.forwardingCellId, {
|
|
1961
|
+
callId: call.id,
|
|
1962
|
+
parentCallId: call.parent?.callId ?? call.id,
|
|
1963
|
+
name: call.name,
|
|
1964
|
+
arguments: intent.arguments,
|
|
1965
|
+
status: "running",
|
|
1966
|
+
result: null,
|
|
1967
|
+
omittedFields: []
|
|
1968
|
+
});
|
|
1969
|
+
return {
|
|
1970
|
+
status: "running",
|
|
1971
|
+
checkpoint: {
|
|
1972
|
+
...intent,
|
|
1973
|
+
phase: "execute",
|
|
1974
|
+
...wake === undefined ? {} : { resume: { wake } }
|
|
1975
|
+
}
|
|
1976
|
+
};
|
|
1977
|
+
}, context);
|
|
1978
|
+
await run(runtime, call, tool, intent, agent, context, wake);
|
|
1979
|
+
}
|
|
1737
1980
|
async function reject(runtime, call, result, agent, tool, context) {
|
|
1738
1981
|
await eachHook(agent, "afterTool", runtime, async (handler) => {
|
|
1739
1982
|
result = await handler(call, result, runtime, context) ?? result;
|
|
1740
1983
|
});
|
|
1741
1984
|
await settle(runtime, call, finishResult(tool, call.name, result), "completed", context);
|
|
1742
1985
|
}
|
|
1743
|
-
async function run(runtime, call, tool,
|
|
1986
|
+
async function run(runtime, call, tool, intent, agent, context, wake) {
|
|
1744
1987
|
let invocation;
|
|
1745
1988
|
let result;
|
|
1746
1989
|
let ending = "completed";
|
|
1747
1990
|
try {
|
|
1748
|
-
|
|
1749
|
-
|
|
1991
|
+
const previous = wake === undefined ? undefined : await runtime.snapshot(InvocationDoc, runtime.taskId, context);
|
|
1992
|
+
invocation = createInvocation(runtime, tool, context, {
|
|
1993
|
+
wake,
|
|
1994
|
+
checkpoint: intent.checkpoint,
|
|
1995
|
+
...previous === undefined ? {} : { progress: previous.result }
|
|
1996
|
+
});
|
|
1997
|
+
const returned = await tool.execute(intent.arguments, await invocation.executionApi(), context);
|
|
1998
|
+
if (returned.kind === undefined)
|
|
1999
|
+
result = returned;
|
|
2000
|
+
else {
|
|
2001
|
+
const reason = await parkTool(runtime, invocation, tool, intent.arguments, returned, context);
|
|
2002
|
+
if (reason === undefined)
|
|
2003
|
+
return;
|
|
2004
|
+
result = executionError("invalid_park", reason);
|
|
2005
|
+
ending = "failed";
|
|
2006
|
+
}
|
|
1750
2007
|
} catch (error) {
|
|
1751
2008
|
if (runtime.signal.aborted) {
|
|
1752
2009
|
await invocation?.stop(error);
|
|
@@ -1798,7 +2055,7 @@ function finishResult(tool, name, result) {
|
|
|
1798
2055
|
} else if (final.structuredOutput === undefined) {
|
|
1799
2056
|
if (!final.isError)
|
|
1800
2057
|
throw new Error(`Tool ${name} returned no structuredOutput`);
|
|
1801
|
-
} else if (!
|
|
2058
|
+
} else if (!compile2(tool.structuredOutputSchema).check(final.structuredOutput)) {
|
|
1802
2059
|
throw new Error(`Tool ${name} returned structuredOutput that does not match its schema`);
|
|
1803
2060
|
}
|
|
1804
2061
|
} catch (error) {
|
|
@@ -1847,9 +2104,11 @@ function providerProjection(surfaceTools) {
|
|
|
1847
2104
|
const conversation = await host.conversation(api.conversationId, context);
|
|
1848
2105
|
if (conversation === undefined)
|
|
1849
2106
|
throw new Error(`Conversation ${api.conversationId} is unavailable`);
|
|
1850
|
-
for (const tool of (await conversation.agent(context)).tools)
|
|
1851
|
-
|
|
2107
|
+
for (const tool of (await conversation.agent(context)).tools) {
|
|
2108
|
+
const contract = readToolContract(tool);
|
|
2109
|
+
if (contract.nativeOnly || contract.nestedHidden)
|
|
1852
2110
|
visible.add(tool.name);
|
|
2111
|
+
}
|
|
1853
2112
|
}
|
|
1854
2113
|
return { messages: projectToolSchemas(request.messages, visible) };
|
|
1855
2114
|
}
|
|
@@ -1858,7 +2117,7 @@ function providerProjection(surfaceTools) {
|
|
|
1858
2117
|
}
|
|
1859
2118
|
|
|
1860
2119
|
// packages/execution/src/wait-session.ts
|
|
1861
|
-
import { withoutAbortSignal as
|
|
2120
|
+
import { withoutAbortSignal as withoutAbortSignal4 } from "@earendil-works/chord/context";
|
|
1862
2121
|
|
|
1863
2122
|
// packages/execution/src/tool-value.ts
|
|
1864
2123
|
class NestedToolError extends Error {
|
|
@@ -1929,7 +2188,7 @@ async function recoverWaitSession(cellId, yieldTimeMs, maxTokens, api, context,
|
|
|
1929
2188
|
}, context);
|
|
1930
2189
|
let cancelling;
|
|
1931
2190
|
const abort = () => {
|
|
1932
|
-
cancelling ??= cancelTask(id,
|
|
2191
|
+
cancelling ??= cancelTask(id, withoutAbortSignal4(context));
|
|
1933
2192
|
cancelling.catch(() => {});
|
|
1934
2193
|
};
|
|
1935
2194
|
context.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
@@ -2013,7 +2272,7 @@ function createCellCoordinator(options) {
|
|
|
2013
2272
|
if (driver.closed)
|
|
2014
2273
|
throw new Error("Execution host is closed");
|
|
2015
2274
|
assertSingleExecutionMode(await api.agent(context));
|
|
2016
|
-
const copied =
|
|
2275
|
+
const copied = copyJson12(input);
|
|
2017
2276
|
return api.commit(async (tx) => {
|
|
2018
2277
|
const isolatedLimit = options.isolatedConcurrency ?? 0;
|
|
2019
2278
|
const isolated = isolatedLimit > 0 && input["isolated"] === true;
|
|
@@ -2093,7 +2352,7 @@ function createCellCoordinator(options) {
|
|
|
2093
2352
|
if (yielding.consume()) {
|
|
2094
2353
|
return initial;
|
|
2095
2354
|
}
|
|
2096
|
-
const wait =
|
|
2355
|
+
const wait = withCancel2(context);
|
|
2097
2356
|
const timeout = new Error("Cell observation yielded");
|
|
2098
2357
|
const timer = setTimeout(() => wait.cancel(timeout), Math.min(yieldTimeMs, 2147483647));
|
|
2099
2358
|
try {
|
|
@@ -3281,7 +3540,7 @@ function createUpdates(write, context) {
|
|
|
3281
3540
|
}
|
|
3282
3541
|
|
|
3283
3542
|
// packages/notebook/src/tool-observation.ts
|
|
3284
|
-
import { copyJson as
|
|
3543
|
+
import { copyJson as copyJson13 } from "@earendil-works/chord/json";
|
|
3285
3544
|
import {
|
|
3286
3545
|
defineDocFamily as defineDocFamily2
|
|
3287
3546
|
} from "@earendil-works/pi-durable";
|
|
@@ -3336,7 +3595,7 @@ async function observeNotebookCell(observation, maxTokens, api, context) {
|
|
|
3336
3595
|
cell_id: String(observation.cellId),
|
|
3337
3596
|
status: observation.status,
|
|
3338
3597
|
...observation.forwardedToolCalls === undefined ? {} : {
|
|
3339
|
-
forwardedToolCalls:
|
|
3598
|
+
forwardedToolCalls: copyJson13(observation.forwardedToolCalls, {
|
|
3340
3599
|
omitUndefinedProperties: true
|
|
3341
3600
|
})
|
|
3342
3601
|
},
|
|
@@ -3357,7 +3616,7 @@ async function observeNotebookCell(observation, maxTokens, api, context) {
|
|
|
3357
3616
|
|
|
3358
3617
|
// packages/js-notebook/src/engine.ts
|
|
3359
3618
|
import { randomUUID as randomUUID7 } from "node:crypto";
|
|
3360
|
-
import { copyJson as
|
|
3619
|
+
import { copyJson as copyJson14 } from "@earendil-works/chord/json";
|
|
3361
3620
|
import {
|
|
3362
3621
|
defineDoc as defineDoc3
|
|
3363
3622
|
} from "@earendil-works/pi-durable";
|
|
@@ -4183,7 +4442,7 @@ async function runtimePackage(name, version, stateDirectory, signal) {
|
|
|
4183
4442
|
}
|
|
4184
4443
|
async function loadJsRuntime(stateDirectory, signal) {
|
|
4185
4444
|
signal?.throwIfAborted();
|
|
4186
|
-
const installed = await runtimePackage("@earendil-works/pi-codemode", "1.
|
|
4445
|
+
const installed = await runtimePackage("@earendil-works/pi-codemode", "1.8.0", stateDirectory, signal);
|
|
4187
4446
|
const manifest = JSON.parse(await readFile3(installed.manifest, "utf8"));
|
|
4188
4447
|
if (!manifest || typeof manifest !== "object" || !("main" in manifest) || typeof manifest.main !== "string")
|
|
4189
4448
|
throw new Error("The JS Notebook runtime package is incomplete; reinstall it, then retry");
|
|
@@ -5212,7 +5471,7 @@ class JsNotebookClient {
|
|
|
5212
5471
|
const definitions = tools.map((tool) => ({
|
|
5213
5472
|
name: tool.name,
|
|
5214
5473
|
exposed: tool.name,
|
|
5215
|
-
description: tool.help ?? tool.usage ?? tool.description,
|
|
5474
|
+
...tool.hidden ? {} : { description: tool.help ?? tool.usage ?? tool.description },
|
|
5216
5475
|
..."textOutput" in tool && tool.textOutput ? { output: tool.textOutput } : {}
|
|
5217
5476
|
}));
|
|
5218
5477
|
this.project.setTools(definitions);
|
|
@@ -5622,13 +5881,14 @@ function createJsNotebookEngine(options) {
|
|
|
5622
5881
|
const contract = readToolContract(registration);
|
|
5623
5882
|
return {
|
|
5624
5883
|
name: registration.name,
|
|
5884
|
+
hidden: contract.nestedHidden,
|
|
5625
5885
|
description: contract.description,
|
|
5626
5886
|
usage: contract.usage,
|
|
5627
5887
|
help: contract.help,
|
|
5628
5888
|
...registration.name === "exec_command" || registration.name === "write_stdin" ? { textOutput: "plain-command" } : {},
|
|
5629
5889
|
async invoke(input, _context, signal) {
|
|
5630
5890
|
signal.throwIfAborted();
|
|
5631
|
-
const args =
|
|
5891
|
+
const args = copyJson14(input === undefined ? null : input, {
|
|
5632
5892
|
omitUndefinedProperties: true
|
|
5633
5893
|
});
|
|
5634
5894
|
const invoke = api.tools[registration.name];
|
|
@@ -6277,15 +6537,15 @@ function createJsNotebookMode(options) {
|
|
|
6277
6537
|
};
|
|
6278
6538
|
return {
|
|
6279
6539
|
output: [{ type: "text", text: result.message }],
|
|
6280
|
-
details:
|
|
6281
|
-
structuredOutput:
|
|
6540
|
+
details: copyJson15(result.details),
|
|
6541
|
+
structuredOutput: copyJson15(result)
|
|
6282
6542
|
};
|
|
6283
6543
|
}
|
|
6284
6544
|
const result = await engine.control(request, api, context);
|
|
6285
6545
|
return {
|
|
6286
6546
|
output: [{ type: "text", text: result.message }],
|
|
6287
|
-
details:
|
|
6288
|
-
structuredOutput:
|
|
6547
|
+
details: copyJson15(result.details, { omitUndefinedProperties: true }),
|
|
6548
|
+
structuredOutput: copyJson15(result, { omitUndefinedProperties: true })
|
|
6289
6549
|
};
|
|
6290
6550
|
}
|
|
6291
6551
|
});
|
|
@@ -6300,12 +6560,8 @@ function createJsNotebookMode(options) {
|
|
|
6300
6560
|
sections: [
|
|
6301
6561
|
section("notebook", (input) => {
|
|
6302
6562
|
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.';
|
|
6303
|
-
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);
|
|
6304
|
-
return [
|
|
6305
|
-
base,
|
|
6306
|
-
"For tools listed without a call signature, call with no arguments to get usage without performing an operation. Structured calls use {}.",
|
|
6307
|
-
...promoted
|
|
6308
|
-
].join(`
|
|
6563
|
+
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);
|
|
6564
|
+
return [base, nestedToolDiscoveryGuidance, ...promoted].join(`
|
|
6309
6565
|
`);
|
|
6310
6566
|
})
|
|
6311
6567
|
]
|