u-foo 3.1.7 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +172 -13
- package/README.zh-CN.md +108 -13
- package/SKILLS/ufoo/SKILL.md +15 -3
- package/SKILLS/ufoo-bus/SKILL.md +21 -1
- package/bin/uclaude.js +4 -0
- package/bin/ucodex.js +4 -0
- package/bin/ufoo.js +3 -1
- 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 +10 -3
- 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/activityStateWriter.js +37 -38
- package/src/agents/capabilities/agentManagement/index.js +4 -0
- package/src/agents/capabilities/agentManagement/service.js +113 -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 +6 -4
- package/src/agents/controller/loopRuntime.js +85 -221
- package/src/agents/controller/ufooAgent.js +1 -0
- package/src/agents/internal/internalRunner.js +103 -32
- package/src/agents/launch/launcher.js +88 -6
- package/src/agents/launch/nativeCapabilities.js +40 -0
- package/src/agents/launch/nativeMessages.js +362 -0
- package/src/agents/launch/notifier.js +13 -18
- package/src/agents/launch/readyDetector.js +26 -12
- 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/groupBootstrap.js +10 -0
- package/src/agents/prompts/mainAgent.js +39 -0
- package/src/agents/prompts/native/tasks.js +2 -2
- package/src/agents/prompts/native/ufoo.js +3 -0
- package/src/agents/providers/claudeEventTranslator.js +6 -1
- package/src/agents/providers/claudeThreadProvider.js +44 -2
- package/src/agents/providers/codexEventTranslator.js +47 -7
- package/src/agents/providers/codexThreadProvider.js +3 -2
- package/src/agents/providers/controllerJsonTransport.js +28 -0
- package/src/agents/providers/credentials/claude.js +10 -3
- package/src/agents/providers/nativeTransport.js +1135 -0
- package/src/agents/providers/runtimeConfig.js +186 -0
- package/src/agents/providers/thinkingLevels.js +132 -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 +499 -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/upstreamTransport.js +13 -4
- package/src/agents/runtime/composeCapabilities.js +89 -0
- package/src/agents/runtime/context/commandStore.js +43 -0
- package/src/agents/runtime/context/contextWindow.js +119 -0
- package/src/agents/runtime/context/fileLock.js +36 -0
- package/src/agents/runtime/context/runtimeStore.js +126 -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 +270 -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 +247 -0
- package/src/agents/runtime/index.js +14 -0
- package/src/agents/runtime/protocol/faultHarness.js +90 -0
- package/src/agents/runtime/protocol/materialize.js +107 -0
- package/src/agents/runtime/protocol/toolCallLedger.js +221 -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 +14 -3
- package/src/app/chat/agentDirectory.js +8 -2
- package/src/app/chat/commandExecutor.js +36 -46
- package/src/app/chat/commands.js +14 -7
- package/src/app/chat/daemonConnection.js +58 -5
- package/src/app/chat/daemonMessageRouter.js +61 -3
- package/src/app/chat/inputSubmitHandler.js +14 -2
- package/src/app/chat/launchRequestContext.js +42 -0
- package/src/app/chat/multiWindow/paneManager.js +85 -165
- package/src/app/chat/streamState.js +3 -3
- 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/skills.js +16 -4
- package/src/app/cli/reportCoreCommands.js +37 -0
- package/src/app/cli/run.js +40 -39
- package/src/code/agent.js +16 -4
- package/src/code/context/artifacts.js +12 -11
- package/src/code/context/assembler.js +2 -1
- package/src/code/context/promptLayers.js +1 -13
- package/src/code/context/stableJson.js +2 -27
- package/src/code/contextWindow.js +2 -117
- package/src/code/conversation/sessionJournal.js +3 -349
- package/src/code/internalThread.js +100 -0
- package/src/code/nativeRunner.js +36 -2667
- 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/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/sessionStore.js +24 -96
- package/src/code/thinkingLevels.js +1 -130
- 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 +81 -67
- package/src/coordination/bus/deliveryQueue.js +16 -2
- package/src/coordination/bus/index.js +2 -6
- package/src/coordination/bus/inject.js +13 -5
- package/src/coordination/bus/nativeReceipts.js +100 -0
- package/src/coordination/bus/queue.js +1 -3
- package/src/coordination/bus/utils.js +7 -3
- package/src/coordination/history/agentSurface.js +142 -0
- package/src/coordination/history/controllerSummary.js +23 -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/runtime/contracts/agentMode.js +17 -0
- package/src/runtime/contracts/eventContract.js +3 -0
- package/src/runtime/contracts/uiProtocol.js +4 -0
- package/src/runtime/daemon/agentHost.js +375 -0
- package/src/runtime/daemon/busBridge.js +396 -0
- package/src/runtime/daemon/claudeChannel.js +159 -0
- package/src/runtime/daemon/controlPlaneService.js +27 -17
- package/src/runtime/daemon/cronOps.js +25 -17
- package/src/runtime/daemon/deliveryScheduler.js +32 -5
- package/src/runtime/daemon/executionPaths.js +30 -0
- package/src/runtime/daemon/globalDaemon.js +28 -1
- package/src/runtime/daemon/groupOrchestrator.js +1 -0
- package/src/runtime/daemon/index.js +114 -344
- 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 +7 -4
- package/src/runtime/daemon/mcpStdioProxy.js +127 -10
- package/src/runtime/daemon/ops.js +22 -17
- package/src/runtime/daemon/promptRequest.js +23 -4
- package/src/runtime/daemon/providerSessions.js +67 -33
- package/src/runtime/daemon/reportControlBus.js +59 -25
- package/src/runtime/daemon/status.js +4 -0
- 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 +24 -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/index.js +1 -0
- package/src/tools/registry.js +2 -0
- package/src/tools/schemaFixtures.js +16 -3
- 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/agentSurface.js +270 -0
- package/src/ui/format/banner.js +8 -11
- package/src/ui/format/startupBanner.js +50 -0
- package/src/ui/multiPaneBusMirror.js +29 -3
- package/src/ui/rustChatHost.js +170 -438
- package/src/ui/rustMultiSession.js +225 -51
- package/src/ui/rustUcodeHost.js +20 -87
- package/src/ui/settingsBridge.js +7 -4
- package/src/app/chat/agentEnter.js +0 -70
- 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/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");
|
|
@@ -1,116 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
extractVisionPayload,
|
|
7
|
-
stripVisionBase64,
|
|
8
|
-
visionSummaryText,
|
|
9
|
-
toOpenAiImagePart,
|
|
10
|
-
} = require("./visionBlocks");
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* OpenAI-compatible chat-completions transport adapter.
|
|
14
|
-
* @param {{
|
|
15
|
-
* resolveUrl: Function,
|
|
16
|
-
* runTurn: Function,
|
|
17
|
-
* normalizeToolName: Function,
|
|
18
|
-
* normalizeToolCallArgs: Function,
|
|
19
|
-
* toJsonString: Function,
|
|
20
|
-
* clipText: Function,
|
|
21
|
-
* }} deps
|
|
22
|
-
*/
|
|
23
|
-
function createOpenAiChatTransport(deps = {}) {
|
|
24
|
-
const {
|
|
25
|
-
resolveUrl,
|
|
26
|
-
runTurn,
|
|
27
|
-
normalizeToolName,
|
|
28
|
-
normalizeToolCallArgs,
|
|
29
|
-
toJsonString,
|
|
30
|
-
clipText,
|
|
31
|
-
} = deps;
|
|
32
|
-
|
|
33
|
-
const transport = {
|
|
34
|
-
name: "openai-chat",
|
|
35
|
-
resolveUrl,
|
|
36
|
-
prepareMessages({ messages, systemPrompt, prompt }) {
|
|
37
|
-
const systemText = String(systemPrompt || "").trim();
|
|
38
|
-
const hasSystem = messages.some((entry) => String(entry.role || "").trim() === "system");
|
|
39
|
-
if (systemText && !hasSystem) {
|
|
40
|
-
messages.unshift({ role: "system", content: systemText });
|
|
41
|
-
}
|
|
42
|
-
messages.push({ role: "user", content: String(prompt || "") });
|
|
43
|
-
},
|
|
44
|
-
runTurn,
|
|
45
|
-
getToolCalls(turnResult) {
|
|
46
|
-
return Array.isArray(turnResult.toolCalls)
|
|
47
|
-
? turnResult.toolCalls.filter((call) => call && call.function && typeof call.function === "object")
|
|
48
|
-
: [];
|
|
49
|
-
},
|
|
50
|
-
appendFinalAssistantMessage({ messages, turnResult }) {
|
|
51
|
-
const text = String(turnResult.text || "").trim();
|
|
52
|
-
if (text) {
|
|
53
|
-
messages.push({
|
|
54
|
-
role: "assistant",
|
|
55
|
-
content: text,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
prepareToolCalls({ messages, toolCalls }) {
|
|
60
|
-
const assistantToolCalls = [];
|
|
61
|
-
for (const call of toolCalls) {
|
|
62
|
-
const callId = String(call.id || `call_${randomUUID()}`);
|
|
63
|
-
const name = normalizeToolName(call.function.name || "");
|
|
64
|
-
const args = normalizeToolCallArgs(call.function.arguments || "");
|
|
65
|
-
|
|
66
|
-
assistantToolCalls.push({
|
|
67
|
-
id: callId,
|
|
68
|
-
type: "function",
|
|
69
|
-
function: {
|
|
70
|
-
name: name || String(call.function.name || ""),
|
|
71
|
-
arguments: toJsonString(args),
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (assistantToolCalls.length === 0) return null;
|
|
77
|
-
|
|
78
|
-
messages.push({
|
|
79
|
-
role: "assistant",
|
|
80
|
-
content: null,
|
|
81
|
-
tool_calls: assistantToolCalls,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
return assistantToolCalls.map((toolCall) => ({
|
|
85
|
-
name: toolCall.function.name,
|
|
86
|
-
args: normalizeToolCallArgs(toolCall.function.arguments),
|
|
87
|
-
source: toolCall,
|
|
88
|
-
}));
|
|
89
|
-
},
|
|
90
|
-
appendToolResult({ messages, call, toolResult }) {
|
|
91
|
-
const vision = extractVisionPayload(toolResult);
|
|
92
|
-
const payload = vision ? stripVisionBase64(toolResult) : toolResult;
|
|
93
|
-
messages.push({
|
|
94
|
-
role: "tool",
|
|
95
|
-
tool_call_id: call.source.id,
|
|
96
|
-
content: clipText(toJsonString(payload), 12000),
|
|
97
|
-
});
|
|
98
|
-
if (vision) {
|
|
99
|
-
const imagePart = toOpenAiImagePart(vision);
|
|
100
|
-
messages.push({
|
|
101
|
-
role: "user",
|
|
102
|
-
content: [
|
|
103
|
-
{ type: "text", text: visionSummaryText(vision, toolResult) },
|
|
104
|
-
imagePart,
|
|
105
|
-
],
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
return assertTransport(transport, "openai-chat");
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
module.exports = {
|
|
115
|
-
createOpenAiChatTransport,
|
|
116
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/providers/transports/openaiChatTransport");
|