u-foo 3.1.7 → 4.0.1
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/OBSERVABILITY.md +65 -0
- package/README.md +238 -21
- package/README.zh-CN.md +165 -20
- package/SKILLS/ufoo/SKILL.md +40 -4
- package/SKILLS/ufoo-bus/SKILL.md +46 -3
- package/bin/uclaude.js +4 -0
- package/bin/ucodex.js +4 -0
- package/bin/ufoo.js +6 -4
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/dist/tui/darwin-x64/ufoo-tui +0 -0
- package/dist/tui/linux-arm64/ufoo-tui +0 -0
- package/dist/tui/linux-x64/ufoo-tui +0 -0
- package/package.json +14 -7
- package/scripts/postinstall-skills.js +53 -0
- package/scripts/postinstall.js +11 -16
- package/scripts/smoke-native-messages.js +214 -0
- package/src/agents/activity/activityDetector.js +1 -1
- package/src/agents/activity/activityStateWriter.js +37 -38
- package/src/agents/capabilities/agentManagement/index.js +4 -0
- package/src/agents/capabilities/agentManagement/service.js +140 -0
- package/src/agents/capabilities/agentRouting/index.js +4 -0
- package/src/agents/capabilities/coding/index.js +32 -0
- package/src/agents/capabilities/coordination/index.js +37 -0
- package/src/agents/capabilities/globalRouting/index.js +5 -0
- package/src/agents/capabilities/groups/index.js +3 -0
- package/src/agents/capabilities/index.js +25 -0
- package/src/agents/capabilities/planning/index.js +123 -0
- package/src/agents/capabilities/planning/main.js +28 -0
- package/src/agents/capabilities/projectDiscovery/index.js +3 -0
- package/src/agents/capabilities/scheduling/index.js +3 -0
- package/src/agents/capabilities/sharedMemory/index.js +4 -0
- package/src/agents/capabilities/skills/index.js +12 -0
- package/src/agents/capabilities/taskExecution/index.js +3 -0
- package/src/agents/capabilities/taskReports/index.js +6 -0
- package/src/agents/controller/controllerToolExecutor.js +40 -22
- package/src/agents/controller/index.js +1 -6
- package/src/agents/internal/internalRunner.js +181 -76
- package/src/agents/launch/index.js +0 -1
- package/src/agents/launch/launcher.js +96 -14
- package/src/agents/launch/nativeCapabilities.js +40 -0
- package/src/agents/launch/nativeMessages.js +362 -0
- package/src/agents/launch/notifier.js +15 -20
- package/src/agents/launch/readyDetector.js +28 -14
- package/src/agents/profiles/coding.js +6 -0
- package/src/agents/profiles/globalRouter.js +2 -0
- package/src/agents/profiles/main.js +5 -0
- package/src/agents/prompts/cooperation.js +14 -0
- package/src/agents/prompts/defaultBootstrap.js +1 -1
- package/src/agents/prompts/groupBootstrap.js +32 -6
- package/src/agents/prompts/mainAgent.js +48 -0
- package/src/agents/prompts/native/system.js +4 -1
- package/src/agents/prompts/native/tasks.js +2 -2
- package/src/agents/prompts/native/ufoo.js +14 -3
- package/src/agents/prompts/promptProfiles.js +2 -0
- package/src/agents/prompts/reporting.js +16 -0
- package/src/agents/providers/claudeEventTranslator.js +9 -1
- package/src/agents/providers/claudeThreadProvider.js +47 -4
- package/src/agents/providers/clientIdentity.js +75 -0
- package/src/agents/providers/codexEventTranslator.js +68 -7
- package/src/agents/providers/codexThreadProvider.js +18 -8
- package/src/agents/providers/credentials/claude.js +10 -3
- package/src/agents/providers/credentials/index.js +2 -0
- package/src/agents/providers/directAuthStatus.js +1 -1
- package/src/agents/providers/index.js +1 -1
- package/src/agents/providers/modelSettings.js +34 -0
- package/src/agents/providers/modelsCatalog.js +379 -0
- package/src/agents/providers/nativeTransport.js +1146 -0
- package/src/agents/providers/runtimeConfig.js +188 -0
- package/src/agents/providers/thinkingLevels.js +106 -0
- package/src/agents/providers/transports/anthropicMessagesTransport.js +121 -0
- package/src/agents/providers/transports/index.js +10 -0
- package/src/agents/providers/transports/openaiChatTransport.js +117 -0
- package/src/agents/providers/transports/openaiResponsesTransport.js +120 -0
- package/src/agents/providers/transports/responsesProtocol.js +504 -0
- package/src/agents/providers/transports/systemBlocks.js +16 -0
- package/src/agents/providers/transports/transportContract.js +50 -0
- package/src/agents/providers/transports/visionBlocks.js +151 -0
- package/src/agents/providers/upstreamRuntime.js +135 -0
- package/src/agents/runtime/composeCapabilities.js +89 -0
- package/src/agents/runtime/context/checkpointStore.js +38 -0
- package/src/agents/runtime/context/commandStore.js +43 -0
- package/src/agents/runtime/context/compaction.js +151 -0
- package/src/agents/runtime/context/contextBudget.js +93 -0
- package/src/agents/runtime/context/contextWindow.js +119 -0
- package/src/agents/runtime/context/fileLock.js +36 -0
- package/src/agents/runtime/context/replayHistory.js +28 -0
- package/src/agents/runtime/context/runtimeStore.js +127 -0
- package/src/agents/runtime/context/sessionJournal.js +360 -0
- package/src/agents/runtime/context/sessionStore.js +57 -0
- package/src/agents/runtime/core/agentLoop.js +351 -0
- package/src/agents/runtime/core/executionBudget.js +36 -0
- package/src/agents/runtime/core/stableJson.js +29 -0
- package/src/agents/runtime/core/toolLoopGuard.js +55 -0
- package/src/agents/runtime/createAgentRuntime.js +259 -0
- package/src/agents/runtime/index.js +14 -0
- package/src/agents/runtime/prompts/responseLanguage.js +12 -0
- package/src/agents/runtime/protocol/faultHarness.js +90 -0
- package/src/agents/runtime/protocol/materialize.js +107 -0
- package/src/agents/runtime/protocol/runObservation.js +24 -0
- package/src/agents/runtime/protocol/toolCallLedger.js +221 -0
- package/src/agents/runtime/protocol/traceContext.js +9 -0
- package/src/agents/runtime/tasks/resourceLease.js +66 -0
- package/src/agents/runtime/tasks/scheduler.js +73 -0
- package/src/agents/runtime/tools/registry.js +57 -0
- package/src/app/chat/ChatController.js +29 -45
- package/src/app/chat/agentDirectory.js +9 -18
- package/src/app/chat/bootstrap.js +1 -2
- package/src/app/chat/commandExecutor.js +111 -313
- package/src/app/chat/commands.js +39 -45
- package/src/app/chat/daemonConnection.js +58 -5
- package/src/app/chat/daemonMessageRouter.js +61 -48
- package/src/app/chat/historyStore.js +12 -48
- package/src/app/chat/inputSubmitHandler.js +21 -35
- package/src/app/chat/internalAgentLogHistory.js +19 -60
- package/src/app/chat/launchRequestContext.js +42 -0
- package/src/app/chat/multiWindow/paneManager.js +91 -164
- package/src/app/chat/streamState.js +3 -7
- package/src/app/chat/transport.js +1 -1
- package/src/app/cli/busCoreCommands.js +9 -0
- package/src/app/cli/features/doctor.js +33 -0
- package/src/app/cli/features/init.js +5 -5
- package/src/app/cli/features/skills.js +16 -4
- package/src/app/cli/metricsCommands.js +68 -0
- package/src/app/cli/reportCoreCommands.js +37 -0
- package/src/app/cli/run.js +82 -42
- package/src/app/cli/traceCommands.js +36 -0
- package/src/code/agent.js +34 -17
- package/src/code/busConsumer.js +1 -3
- package/src/code/commands.js +1 -1
- package/src/code/context/artifacts.js +12 -11
- package/src/code/context/assembler.js +9 -9
- package/src/code/context/promptLayers.js +1 -13
- package/src/code/context/stableJson.js +2 -27
- package/src/code/context/stateCommit.js +2 -1
- package/src/code/contextWindow.js +2 -117
- package/src/code/conversation/sessionJournal.js +3 -349
- package/src/code/internalThread.js +107 -0
- package/src/code/launcher/ucode.js +1 -1
- package/src/code/launcher/ucodeBootstrap.js +1 -1
- package/src/code/modelCommand.js +11 -35
- package/src/code/nativeRunner.js +104 -2697
- package/src/code/protocol/faultHarness.js +2 -88
- package/src/code/protocol/materialize.js +2 -105
- package/src/code/protocol/toolCallLedger.js +5 -215
- package/src/code/providers/anthropicMessagesTransport.js +2 -118
- package/src/code/providers/modelsCatalog.js +1 -317
- package/src/code/providers/openaiChatTransport.js +2 -114
- package/src/code/providers/openaiResponsesTransport.js +2 -117
- package/src/code/providers/responsesProtocol.js +2 -497
- package/src/code/providers/transportContract.js +2 -48
- package/src/code/providers/visionBlocks.js +2 -149
- package/src/code/repl.js +2 -11
- package/src/code/sessionStore.js +27 -99
- package/src/code/taskDecomposer.js +18 -0
- package/src/code/thinkingLevels.js +1 -130
- package/src/code/toolBudget.js +1 -17
- package/src/code/toolLoopGuard.js +3 -49
- package/src/code/tools/artifactRead.js +1 -1
- package/src/code/tools/executor.js +373 -0
- package/src/code/tools/specs.js +312 -0
- package/src/config.js +95 -131
- package/src/coordination/bus/deliveryQueue.js +16 -2
- package/src/coordination/bus/index.js +14 -9
- package/src/coordination/bus/inject.js +15 -7
- package/src/coordination/bus/message.js +15 -1
- package/src/coordination/bus/nativeReceipts.js +100 -0
- package/src/coordination/bus/nickname.js +1 -1
- package/src/coordination/bus/queue.js +1 -3
- package/src/coordination/bus/subscriber.js +3 -2
- package/src/coordination/bus/utils.js +8 -4
- package/src/coordination/history/agentPerformance.js +279 -0
- package/src/coordination/history/agentSurface.js +142 -0
- package/src/coordination/history/agentTrace.js +249 -0
- package/src/coordination/memory/index.js +5 -21
- package/src/coordination/report/store.js +103 -83
- package/src/coordination/state/agentsStore.js +57 -3
- package/src/coordination/state/fileLock.js +2 -0
- package/src/coordination/state/workspaceAccess.js +36 -0
- package/src/orchestration/controller/index.js +1 -8
- package/src/orchestration/controller/launchRouting.js +1 -1
- package/src/orchestration/solo/commands.js +3 -1
- package/src/runtime/contracts/agentMode.js +26 -0
- package/src/runtime/contracts/eventContract.js +3 -0
- package/src/runtime/contracts/uiProtocol.js +20 -17
- package/src/runtime/daemon/agentHost.js +407 -0
- package/src/runtime/daemon/agentRecoveryGroups.js +70 -0
- package/src/runtime/daemon/busBridge.js +396 -0
- package/src/runtime/daemon/claudeChannel.js +159 -0
- package/src/runtime/daemon/controlPlaneService.js +28 -18
- package/src/runtime/daemon/cronOps.js +25 -17
- package/src/runtime/daemon/deliveryScheduler.js +32 -5
- package/src/runtime/daemon/globalDaemon.js +28 -1
- package/src/runtime/daemon/groupOrchestrator.js +21 -5
- package/src/runtime/daemon/groupStatus.js +20 -0
- package/src/runtime/daemon/index.js +120 -375
- package/src/runtime/daemon/ipcServer.js +32 -10
- package/src/runtime/daemon/mcpConfigure.js +113 -20
- package/src/runtime/daemon/mcpHttpServer.js +27 -5
- package/src/runtime/daemon/mcpServer.js +8 -5
- package/src/runtime/daemon/mcpStdioProxy.js +127 -10
- package/src/runtime/daemon/nicknameScope.js +1 -1
- package/src/runtime/daemon/ops.js +68 -40
- package/src/runtime/daemon/projectRuntimeGateway.js +2 -0
- package/src/runtime/daemon/promptRequest.js +92 -570
- package/src/runtime/daemon/providerSessions.js +67 -33
- package/src/runtime/daemon/reportControlBus.js +59 -25
- package/src/runtime/daemon/status.js +9 -6
- package/src/runtime/privacy/redactor.js +2 -2
- package/src/runtime/projects/identity.js +2 -1
- package/src/runtime/projects/registry.js +98 -38
- package/src/tools/controllerSchemas.js +28 -0
- package/src/tools/handlers/dispatchMessage.js +5 -2
- package/src/tools/handlers/listAgents.js +3 -1
- package/src/tools/handlers/readBusSummary.js +3 -1
- package/src/tools/handlers/readProjectRegistry.js +29 -2
- package/src/tools/handlers/routeAgent.js +35 -0
- package/src/tools/handlers/tier2.js +4 -2
- package/src/tools/index.js +1 -0
- package/src/tools/registry.js +6 -0
- package/src/tools/schemaFixtures.js +42 -5
- package/src/tools/tier0/readAgentMetrics.js +14 -0
- package/src/tools/tier0/readAgentTrace.js +12 -0
- package/src/tools/tier1/routeAgent.js +2 -2
- package/src/tools/tier2/runtimeCoordination.js +38 -0
- package/src/tools/types.js +2 -0
- package/src/ui/agentPresentation.js +109 -0
- package/src/ui/agentSettingsController.js +107 -0
- package/src/ui/agentSurface.js +270 -0
- package/src/ui/dashboardBridge.js +1 -1
- package/src/ui/format/banner.js +9 -15
- package/src/ui/format/index.js +14 -10
- package/src/ui/format/loopSummary.js +50 -0
- package/src/ui/format/markdownRenderer.js +3 -4
- package/src/ui/format/startupBanner.js +50 -0
- package/src/ui/multiPaneBusMirror.js +29 -3
- package/src/ui/rustChatHost.js +316 -599
- package/src/ui/rustMultiSession.js +288 -73
- package/src/ui/rustUcodeHost.js +21 -88
- package/src/ui/settingsBridge.js +34 -14
- package/src/ui/toolMergeBridge.js +0 -4
- package/src/ui/uiHostServer.js +5 -8
- package/src/agents/controller/loopObservability.js +0 -200
- package/src/agents/controller/loopRuntime.js +0 -472
- package/src/agents/controller/ufooAgent.js +0 -829
- package/src/agents/launch/ptyRunner.js +0 -1135
- package/src/agents/providers/upstreamTransport.js +0 -689
- package/src/app/chat/agentBar.js +0 -135
- package/src/app/chat/agentEnter.js +0 -70
- package/src/app/chat/agentSockets.js +0 -225
- package/src/app/chat/dashboardView.js +0 -405
- package/src/app/chat/multiWindow/index.js +0 -310
- package/src/app/chat/multiWindow/paneLayout.js +0 -84
- package/src/app/chat/multiWindow/renderer.js +0 -420
- package/src/app/chat/multiWindow/virtualTerminal.js +0 -327
- package/src/app/chat/multiWindow/vtFrame.js +0 -93
- package/src/app/chat/rawKeyMap.js +0 -42
- package/src/app/chat/settingsController.js +0 -111
- package/src/orchestration/controller/flags.js +0 -167
- package/src/orchestration/controller/gateRouter.js +0 -211
- package/src/orchestration/controller/routerFastPath.js +0 -22
- package/src/orchestration/controller/routerFinalize.js +0 -222
- package/src/orchestration/controller/shadowGuard.js +0 -280
- package/src/runtime/daemon/promptLoop.js +0 -136
- package/src/runtime/privacy/shadowDiff.js +0 -174
- package/src/ui/multiWindowHandoff.js +0 -232
- package/src/ui/ptyHandoff.js +0 -23
|
@@ -1,90 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* Usage:
|
|
7
|
-
* armFault("after_prepare_tool_calls");
|
|
8
|
-
* await withFaultPoint("after_prepare_tool_calls", async () => { ... });
|
|
9
|
-
*
|
|
10
|
-
* Armed points throw FaultInjectedError once (or until disarmed).
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const armed = new Map();
|
|
14
|
-
|
|
15
|
-
class FaultInjectedError extends Error {
|
|
16
|
-
constructor(point = "") {
|
|
17
|
-
super(`fault injected at ${point}`);
|
|
18
|
-
this.name = "FaultInjectedError";
|
|
19
|
-
this.code = "FAULT_INJECTED";
|
|
20
|
-
this.point = String(point || "");
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** Known hook names reserved for native loop / resume (R4 expands coverage). */
|
|
25
|
-
const FAULT_POINTS = Object.freeze([
|
|
26
|
-
"after_prepare_tool_calls",
|
|
27
|
-
"before_tool_exec",
|
|
28
|
-
"after_tool_effect",
|
|
29
|
-
"before_result_commit",
|
|
30
|
-
"after_answer_commit",
|
|
31
|
-
"before_provider_resume",
|
|
32
|
-
"after_taskrun_acquire_lease",
|
|
33
|
-
"before_parent_node_sync",
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
function armFault(point = "", { times = 1, error = null } = {}) {
|
|
37
|
-
const name = String(point || "").trim();
|
|
38
|
-
if (!name) throw new Error("fault point name required");
|
|
39
|
-
armed.set(name, {
|
|
40
|
-
remaining: Math.max(1, Math.floor(Number(times) || 1)),
|
|
41
|
-
error: error || null,
|
|
42
|
-
});
|
|
43
|
-
return name;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function disarmFault(point = "") {
|
|
47
|
-
const name = String(point || "").trim();
|
|
48
|
-
if (!name) {
|
|
49
|
-
armed.clear();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
armed.delete(name);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isFaultArmed(point = "") {
|
|
56
|
-
return armed.has(String(point || "").trim());
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function checkFaultPoint(point = "") {
|
|
60
|
-
const name = String(point || "").trim();
|
|
61
|
-
const entry = armed.get(name);
|
|
62
|
-
if (!entry) return;
|
|
63
|
-
entry.remaining -= 1;
|
|
64
|
-
if (entry.remaining <= 0) armed.delete(name);
|
|
65
|
-
if (entry.error) throw entry.error;
|
|
66
|
-
throw new FaultInjectedError(name);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Run fn; if point is armed, throw before invoking fn.
|
|
71
|
-
*/
|
|
72
|
-
async function withFaultPoint(point = "", fn) {
|
|
73
|
-
checkFaultPoint(point);
|
|
74
|
-
return typeof fn === "function" ? fn() : undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function listArmedFaults() {
|
|
78
|
-
return Array.from(armed.keys());
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module.exports = {
|
|
82
|
-
FAULT_POINTS,
|
|
83
|
-
FaultInjectedError,
|
|
84
|
-
armFault,
|
|
85
|
-
disarmFault,
|
|
86
|
-
isFaultArmed,
|
|
87
|
-
checkFaultPoint,
|
|
88
|
-
withFaultPoint,
|
|
89
|
-
listArmedFaults,
|
|
90
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/runtime/protocol/faultHarness");
|
|
@@ -1,107 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Business branches must resolve on the ledger; they must not hand-write
|
|
6
|
-
* unpaired tool results.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const { listCalls } = require("./toolCallLedger");
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Clip helper kept local so protocol does not depend on nativeRunner.
|
|
13
|
-
*/
|
|
14
|
-
function clipText(value = "", maxChars = 12000) {
|
|
15
|
-
const text = String(value == null ? "" : value);
|
|
16
|
-
if (text.length <= maxChars) return text;
|
|
17
|
-
return `${text.slice(0, maxChars)}\n...[truncated ${text.length - maxChars} chars]`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function toJsonString(value) {
|
|
21
|
-
try {
|
|
22
|
-
return JSON.stringify(value);
|
|
23
|
-
} catch {
|
|
24
|
-
return String(value);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @param {object} ledger
|
|
30
|
-
* @param {{
|
|
31
|
-
* transport: object,
|
|
32
|
-
* messages: object[],
|
|
33
|
-
* pendingById: Record<string, object>,
|
|
34
|
-
* }} opts
|
|
35
|
-
* @returns {{ appended: number, flushed: boolean }}
|
|
36
|
-
*/
|
|
37
|
-
function materializeResolvedToolResults(ledger, {
|
|
38
|
-
transport = null,
|
|
39
|
-
messages = [],
|
|
40
|
-
pendingById = {},
|
|
41
|
-
} = {}) {
|
|
42
|
-
if (!ledger || !transport || typeof transport.appendToolResult !== "function") {
|
|
43
|
-
return { appended: 0, flushed: false };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const collected = [];
|
|
47
|
-
let appended = 0;
|
|
48
|
-
for (const call of listCalls(ledger)) {
|
|
49
|
-
if (call.state !== "resolved") continue;
|
|
50
|
-
const pending = pendingById[call.callId];
|
|
51
|
-
if (!pending) continue;
|
|
52
|
-
transport.appendToolResult({
|
|
53
|
-
messages,
|
|
54
|
-
collected,
|
|
55
|
-
call: pending,
|
|
56
|
-
toolResult: call.resultPayload,
|
|
57
|
-
});
|
|
58
|
-
appended += 1;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
let flushed = false;
|
|
62
|
-
if (
|
|
63
|
-
collected.length > 0
|
|
64
|
-
&& typeof transport.flushToolResults === "function"
|
|
65
|
-
) {
|
|
66
|
-
transport.flushToolResults({ messages, collected });
|
|
67
|
-
flushed = true;
|
|
68
|
-
}
|
|
69
|
-
return { appended, flushed };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Append a single resume answer as a tool result (idempotent via ledger resolve).
|
|
74
|
-
*/
|
|
75
|
-
function materializeAnswerToolResult(messages = [], resume = null, answer = {}, {
|
|
76
|
-
clip = clipText,
|
|
77
|
-
} = {}) {
|
|
78
|
-
const call = resume && resume.call ? resume.call : null;
|
|
79
|
-
if (!call || !call.source) return { ok: false, error: "missing deferred tool call" };
|
|
80
|
-
const transportName = String(resume.transport || "openai-chat");
|
|
81
|
-
const content = clip(toJsonString(answer), 12000);
|
|
82
|
-
if (transportName === "anthropic-messages") {
|
|
83
|
-
messages.push({
|
|
84
|
-
role: "user",
|
|
85
|
-
content: [{
|
|
86
|
-
type: "tool_result",
|
|
87
|
-
tool_use_id: String(call.source.id || resume.toolCallId || ""),
|
|
88
|
-
content,
|
|
89
|
-
is_error: false,
|
|
90
|
-
}],
|
|
91
|
-
});
|
|
92
|
-
} else {
|
|
93
|
-
messages.push({
|
|
94
|
-
role: "tool",
|
|
95
|
-
tool_call_id: String(call.source.id || resume.toolCallId || ""),
|
|
96
|
-
content,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
return { ok: true };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
module.exports = {
|
|
103
|
-
materializeResolvedToolResults,
|
|
104
|
-
materializeAnswerToolResult,
|
|
105
|
-
clipText,
|
|
106
|
-
toJsonString,
|
|
107
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/runtime/protocol/materialize");
|
|
@@ -1,222 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
* Tool Call Ledger — authoritative record of declared tool calls for a turn.
|
|
5
|
-
*
|
|
6
|
-
* Shadow mode (Phase 0 / R1): ledger tracks declare/defer/resolve alongside the
|
|
7
|
-
* existing TRANSPORTS message assembly. Materialize-from-ledger comes later.
|
|
8
|
-
*
|
|
9
|
-
* Call states: declared | executing | deferred | resolved
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const { createHash, randomUUID } = require("crypto");
|
|
13
|
-
|
|
14
|
-
const CALL_STATES = Object.freeze(["declared", "executing", "deferred", "resolved"]);
|
|
3
|
+
const protocol = require("../../agents/runtime/protocol/toolCallLedger");
|
|
15
4
|
const DEFERABLE_TOOLS = Object.freeze(new Set(["ask_user"]));
|
|
16
5
|
|
|
17
|
-
function digestValue(value) {
|
|
18
|
-
const raw = typeof value === "string" ? value : stableStringify(value);
|
|
19
|
-
return createHash("sha256").update(raw).digest("hex").slice(0, 16);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function stableStringify(value) {
|
|
23
|
-
if (value == null) return "null";
|
|
24
|
-
if (typeof value !== "object") return JSON.stringify(value);
|
|
25
|
-
if (Array.isArray(value)) {
|
|
26
|
-
return `[${value.map((entry) => stableStringify(entry)).join(",")}]`;
|
|
27
|
-
}
|
|
28
|
-
const keys = Object.keys(value).sort();
|
|
29
|
-
return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function createTurnId() {
|
|
33
|
-
return `turn_${Date.now().toString(36)}_${randomUUID().slice(0, 8)}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @param {{ provider?: string, sessionId?: string, turnId?: string, assistantMessageId?: string }} opts
|
|
38
|
-
*/
|
|
39
|
-
function createToolCallLedger(opts = {}) {
|
|
40
|
-
return {
|
|
41
|
-
turnId: String(opts.turnId || createTurnId()),
|
|
42
|
-
provider: String(opts.provider || "").trim(),
|
|
43
|
-
sessionId: String(opts.sessionId || "").trim(),
|
|
44
|
-
assistantMessageId: String(opts.assistantMessageId || "").trim(),
|
|
45
|
-
calls: Object.create(null),
|
|
46
|
-
violations: [],
|
|
47
|
-
createdAt: new Date().toISOString(),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function getCall(ledger, callId) {
|
|
52
|
-
if (!ledger || !ledger.calls) return null;
|
|
53
|
-
const id = String(callId || "").trim();
|
|
54
|
-
return id && ledger.calls[id] ? ledger.calls[id] : null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function listCalls(ledger) {
|
|
58
|
-
if (!ledger || !ledger.calls) return [];
|
|
59
|
-
return Object.keys(ledger.calls).map((id) => ledger.calls[id]);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function listUnresolved(ledger) {
|
|
63
|
-
return listCalls(ledger).filter((call) => (
|
|
64
|
-
call.state === "declared" || call.state === "executing"
|
|
65
|
-
));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function listDeferred(ledger) {
|
|
69
|
-
return listCalls(ledger).filter((call) => call.state === "deferred");
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Declare one or more tool calls after prepareToolCalls.
|
|
74
|
-
* @param {object} ledger
|
|
75
|
-
* @param {Array<{ callId: string, name: string, args?: object }>} calls
|
|
76
|
-
*/
|
|
77
|
-
function declareCalls(ledger, calls = []) {
|
|
78
|
-
if (!ledger || !ledger.calls) {
|
|
79
|
-
return { ok: false, error: "missing ledger" };
|
|
80
|
-
}
|
|
81
|
-
const list = Array.isArray(calls) ? calls : [];
|
|
82
|
-
for (const entry of list) {
|
|
83
|
-
const callId = String(entry && entry.callId || "").trim();
|
|
84
|
-
if (!callId) {
|
|
85
|
-
return { ok: false, error: "callId required" };
|
|
86
|
-
}
|
|
87
|
-
if (ledger.calls[callId]) {
|
|
88
|
-
return { ok: false, error: `duplicate callId: ${callId}`, code: "DUPLICATE_CALL_ID" };
|
|
89
|
-
}
|
|
90
|
-
const name = String(entry.name || "").trim().toLowerCase();
|
|
91
|
-
ledger.calls[callId] = {
|
|
92
|
-
callId,
|
|
93
|
-
name,
|
|
94
|
-
argsDigest: digestValue(entry.args == null ? {} : entry.args),
|
|
95
|
-
state: "declared",
|
|
96
|
-
resultDigest: "",
|
|
97
|
-
isError: false,
|
|
98
|
-
resolvedAt: "",
|
|
99
|
-
deferredAt: "",
|
|
100
|
-
executingAt: "",
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
if (!ledger.assistantMessageId && list.length > 0) {
|
|
104
|
-
ledger.assistantMessageId = String(list[0].callId || "");
|
|
105
|
-
}
|
|
106
|
-
return { ok: true, count: list.length };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function markExecuting(ledger, callId) {
|
|
110
|
-
const call = getCall(ledger, callId);
|
|
111
|
-
if (!call) return { ok: false, error: "call not found", code: "CALL_NOT_FOUND" };
|
|
112
|
-
if (call.state !== "declared") {
|
|
113
|
-
return { ok: false, error: `cannot execute from ${call.state}`, code: "INVALID_STATE" };
|
|
114
|
-
}
|
|
115
|
-
call.state = "executing";
|
|
116
|
-
call.executingAt = new Date().toISOString();
|
|
117
|
-
return { ok: true, call };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Defer a call (ask_user only). Leaves assistant tool_call unpaired until resume.
|
|
122
|
-
*/
|
|
123
|
-
function deferCall(ledger, callId, { reason = "" } = {}) {
|
|
124
|
-
const call = getCall(ledger, callId);
|
|
125
|
-
if (!call) return { ok: false, error: "call not found", code: "CALL_NOT_FOUND" };
|
|
126
|
-
if (call.state !== "declared" && call.state !== "executing") {
|
|
127
|
-
return { ok: false, error: `cannot defer from ${call.state}`, code: "INVALID_STATE" };
|
|
128
|
-
}
|
|
129
|
-
if (!DEFERABLE_TOOLS.has(call.name)) {
|
|
130
|
-
return {
|
|
131
|
-
ok: false,
|
|
132
|
-
error: `tool ${call.name} cannot be deferred`,
|
|
133
|
-
code: "NOT_DEFERABLE",
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
call.state = "deferred";
|
|
137
|
-
call.deferredAt = new Date().toISOString();
|
|
138
|
-
call.deferReason = String(reason || "").trim();
|
|
139
|
-
return { ok: true, call };
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Resolve a declared/executing/deferred call with exactly one result.
|
|
144
|
-
* Idempotent when resultDigest matches a prior resolve.
|
|
145
|
-
*/
|
|
146
|
-
function resolveCall(ledger, callId, {
|
|
147
|
-
result = null,
|
|
148
|
-
isError = false,
|
|
149
|
-
allowFromDeferred = true,
|
|
150
|
-
} = {}) {
|
|
151
|
-
const call = getCall(ledger, callId);
|
|
152
|
-
if (!call) return { ok: false, error: "call not found", code: "CALL_NOT_FOUND" };
|
|
153
|
-
|
|
154
|
-
const resultDigest = digestValue(result);
|
|
155
|
-
if (call.state === "resolved") {
|
|
156
|
-
if (call.resultDigest === resultDigest) {
|
|
157
|
-
return { ok: true, call, idempotent: true };
|
|
158
|
-
}
|
|
159
|
-
return {
|
|
160
|
-
ok: false,
|
|
161
|
-
error: "call already resolved with different result",
|
|
162
|
-
code: "DUPLICATE_RESOLVE",
|
|
163
|
-
call,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (call.state === "deferred" && !allowFromDeferred) {
|
|
168
|
-
return { ok: false, error: "call is deferred", code: "STILL_DEFERRED", call };
|
|
169
|
-
}
|
|
170
|
-
if (call.state !== "declared" && call.state !== "executing" && call.state !== "deferred") {
|
|
171
|
-
return { ok: false, error: `cannot resolve from ${call.state}`, code: "INVALID_STATE", call };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
call.state = "resolved";
|
|
175
|
-
call.resultDigest = resultDigest;
|
|
176
|
-
call.resultPayload = result;
|
|
177
|
-
call.isError = Boolean(isError);
|
|
178
|
-
call.resolvedAt = new Date().toISOString();
|
|
179
|
-
return { ok: true, call, idempotent: false };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function recordViolation(ledger, violation = {}) {
|
|
183
|
-
if (!ledger) return;
|
|
184
|
-
if (!Array.isArray(ledger.violations)) ledger.violations = [];
|
|
185
|
-
ledger.violations.push({
|
|
186
|
-
code: String(violation.code || "PROTOCOL_VIOLATION"),
|
|
187
|
-
message: String(violation.message || ""),
|
|
188
|
-
callId: String(violation.callId || ""),
|
|
189
|
-
at: new Date().toISOString(),
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function snapshotLedger(ledger) {
|
|
194
|
-
if (!ledger) return null;
|
|
195
|
-
return JSON.parse(JSON.stringify({
|
|
196
|
-
turnId: ledger.turnId,
|
|
197
|
-
provider: ledger.provider,
|
|
198
|
-
sessionId: ledger.sessionId,
|
|
199
|
-
assistantMessageId: ledger.assistantMessageId,
|
|
200
|
-
calls: ledger.calls,
|
|
201
|
-
violations: ledger.violations || [],
|
|
202
|
-
createdAt: ledger.createdAt,
|
|
203
|
-
}));
|
|
204
|
-
}
|
|
205
|
-
|
|
206
6
|
module.exports = {
|
|
207
|
-
|
|
7
|
+
...protocol,
|
|
208
8
|
DEFERABLE_TOOLS,
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
getCall,
|
|
213
|
-
listCalls,
|
|
214
|
-
listUnresolved,
|
|
215
|
-
listDeferred,
|
|
216
|
-
declareCalls,
|
|
217
|
-
markExecuting,
|
|
218
|
-
deferCall,
|
|
219
|
-
resolveCall,
|
|
220
|
-
recordViolation,
|
|
221
|
-
snapshotLedger,
|
|
9
|
+
createToolCallLedger: (options = {}) => protocol.createToolCallLedger({
|
|
10
|
+
...options, deferableTools: options.deferableTools || DEFERABLE_TOOLS,
|
|
11
|
+
}),
|
|
222
12
|
};
|
|
@@ -1,120 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
extractVisionPayload,
|
|
6
|
-
stripVisionBase64,
|
|
7
|
-
visionSummaryText,
|
|
8
|
-
toAnthropicImageBlock,
|
|
9
|
-
} = require("./visionBlocks");
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Anthropic Messages API transport adapter.
|
|
13
|
-
* @param {{
|
|
14
|
-
* resolveUrl: Function,
|
|
15
|
-
* runTurn: Function,
|
|
16
|
-
* toJsonString: Function,
|
|
17
|
-
* clipText: Function,
|
|
18
|
-
* }} deps
|
|
19
|
-
*/
|
|
20
|
-
function createAnthropicMessagesTransport(deps = {}) {
|
|
21
|
-
const {
|
|
22
|
-
resolveUrl,
|
|
23
|
-
runTurn,
|
|
24
|
-
toJsonString,
|
|
25
|
-
clipText,
|
|
26
|
-
} = deps;
|
|
27
|
-
|
|
28
|
-
const transport = {
|
|
29
|
-
name: "anthropic-messages",
|
|
30
|
-
resolveUrl,
|
|
31
|
-
prepareMessages({ messages, prompt }) {
|
|
32
|
-
messages.push({
|
|
33
|
-
role: "user",
|
|
34
|
-
content: String(prompt || ""),
|
|
35
|
-
});
|
|
36
|
-
},
|
|
37
|
-
runTurn,
|
|
38
|
-
getToolCalls(turnResult) {
|
|
39
|
-
return Array.isArray(turnResult.toolCalls) ? turnResult.toolCalls : [];
|
|
40
|
-
},
|
|
41
|
-
appendFinalAssistantMessage({ messages, turnResult }) {
|
|
42
|
-
const assistantContent = Array.isArray(turnResult.assistantContent)
|
|
43
|
-
? turnResult.assistantContent
|
|
44
|
-
: [];
|
|
45
|
-
if (assistantContent.length > 0) {
|
|
46
|
-
messages.push({
|
|
47
|
-
role: "assistant",
|
|
48
|
-
content: assistantContent,
|
|
49
|
-
});
|
|
50
|
-
} else if (String(turnResult.text || "").trim()) {
|
|
51
|
-
messages.push({
|
|
52
|
-
role: "assistant",
|
|
53
|
-
content: [
|
|
54
|
-
{
|
|
55
|
-
type: "text",
|
|
56
|
-
text: String(turnResult.text || ""),
|
|
57
|
-
},
|
|
58
|
-
],
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
prepareToolCalls({ messages, turnResult, toolCalls }) {
|
|
63
|
-
const assistantContent = Array.isArray(turnResult.assistantContent)
|
|
64
|
-
? turnResult.assistantContent
|
|
65
|
-
: [];
|
|
66
|
-
|
|
67
|
-
messages.push({
|
|
68
|
-
role: "assistant",
|
|
69
|
-
content: assistantContent,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
return toolCalls.map((call) => ({
|
|
73
|
-
name: call.name,
|
|
74
|
-
args: call.args,
|
|
75
|
-
source: call,
|
|
76
|
-
}));
|
|
77
|
-
},
|
|
78
|
-
appendToolResult({ collected, call, toolResult }) {
|
|
79
|
-
const vision = extractVisionPayload(toolResult);
|
|
80
|
-
const isError = Boolean(!toolResult || toolResult.ok === false);
|
|
81
|
-
if (vision) {
|
|
82
|
-
const textPayload = stripVisionBase64(toolResult);
|
|
83
|
-
collected.push({
|
|
84
|
-
type: "tool_result",
|
|
85
|
-
tool_use_id: String(call.source.id || ""),
|
|
86
|
-
content: [
|
|
87
|
-
{
|
|
88
|
-
type: "text",
|
|
89
|
-
text: clipText(
|
|
90
|
-
`${visionSummaryText(vision, toolResult)}\n${toJsonString(textPayload)}`,
|
|
91
|
-
12000,
|
|
92
|
-
),
|
|
93
|
-
},
|
|
94
|
-
toAnthropicImageBlock(vision),
|
|
95
|
-
],
|
|
96
|
-
is_error: isError,
|
|
97
|
-
});
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
collected.push({
|
|
101
|
-
type: "tool_result",
|
|
102
|
-
tool_use_id: String(call.source.id || ""),
|
|
103
|
-
content: clipText(toJsonString(toolResult), 12000),
|
|
104
|
-
is_error: isError,
|
|
105
|
-
});
|
|
106
|
-
},
|
|
107
|
-
flushToolResults({ messages, collected }) {
|
|
108
|
-
messages.push({
|
|
109
|
-
role: "user",
|
|
110
|
-
content: collected,
|
|
111
|
-
});
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
return assertTransport(transport, "anthropic-messages");
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
module.exports = {
|
|
119
|
-
createAnthropicMessagesTransport,
|
|
120
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/providers/transports/anthropicMessagesTransport");
|