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,499 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function cloneJsonValue(value) {
|
|
7
|
-
try {
|
|
8
|
-
return JSON.parse(JSON.stringify(value));
|
|
9
|
-
} catch {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function reasoningItemsFromResponse(response = null) {
|
|
15
|
-
const output = response && typeof response === "object" && Array.isArray(response.output)
|
|
16
|
-
? response.output
|
|
17
|
-
: [];
|
|
18
|
-
return output
|
|
19
|
-
.filter((item) => (
|
|
20
|
-
item
|
|
21
|
-
&& typeof item === "object"
|
|
22
|
-
&& item.type === "reasoning"
|
|
23
|
-
&& typeof item.encrypted_content === "string"
|
|
24
|
-
&& item.encrypted_content.length > 0
|
|
25
|
-
))
|
|
26
|
-
.map(cloneJsonValue)
|
|
27
|
-
.filter(Boolean);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function attachResponsesReasoningItems(message = null, response = null) {
|
|
31
|
-
if (!message || typeof message !== "object") return message;
|
|
32
|
-
const items = reasoningItemsFromResponse(response);
|
|
33
|
-
if (items.length === 0) return message;
|
|
34
|
-
Object.defineProperty(message, RESPONSES_REASONING_ITEMS, {
|
|
35
|
-
value: items,
|
|
36
|
-
enumerable: false,
|
|
37
|
-
configurable: false,
|
|
38
|
-
writable: false,
|
|
39
|
-
});
|
|
40
|
-
return message;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getResponsesReasoningItems(message = null) {
|
|
44
|
-
const items = message && typeof message === "object"
|
|
45
|
-
? message[RESPONSES_REASONING_ITEMS]
|
|
46
|
-
: null;
|
|
47
|
-
return (Array.isArray(items) ? items : [])
|
|
48
|
-
.map(cloneJsonValue)
|
|
49
|
-
.filter(Boolean);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function resolveResponsesUrl(baseUrl = "") {
|
|
53
|
-
const raw = String(baseUrl || "").trim();
|
|
54
|
-
if (!raw) return "";
|
|
55
|
-
const normalized = raw.replace(/\/+$/, "");
|
|
56
|
-
if (/\/responses$/i.test(normalized)) return normalized;
|
|
57
|
-
if (/\/v1$/i.test(normalized)) return `${normalized}/responses`;
|
|
58
|
-
if (/\/api$/i.test(normalized)) return `${normalized}/v1/responses`;
|
|
59
|
-
return `${normalized}/responses`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function toJsonString(value) {
|
|
63
|
-
if (typeof value === "string") return value;
|
|
64
|
-
try {
|
|
65
|
-
return JSON.stringify(value == null ? {} : value);
|
|
66
|
-
} catch {
|
|
67
|
-
return "{}";
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function normalizeArguments(value) {
|
|
72
|
-
if (value && typeof value === "object") return value;
|
|
73
|
-
const text = String(value || "").trim();
|
|
74
|
-
if (!text) return {};
|
|
75
|
-
try {
|
|
76
|
-
const parsed = JSON.parse(text);
|
|
77
|
-
return parsed && typeof parsed === "object" ? parsed : {};
|
|
78
|
-
} catch {
|
|
79
|
-
return {};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function normalizeContentParts(content, role = "user") {
|
|
84
|
-
const outputRole = role === "assistant" ? "output_text" : "input_text";
|
|
85
|
-
if (typeof content === "string") {
|
|
86
|
-
return content ? [{ type: outputRole, text: content }] : [];
|
|
87
|
-
}
|
|
88
|
-
if (!Array.isArray(content)) return [];
|
|
89
|
-
|
|
90
|
-
const parts = [];
|
|
91
|
-
for (const block of content) {
|
|
92
|
-
if (typeof block === "string") {
|
|
93
|
-
if (block) parts.push({ type: outputRole, text: block });
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
if (!block || typeof block !== "object") continue;
|
|
97
|
-
const type = String(block.type || "").trim().toLowerCase();
|
|
98
|
-
if (type === "text" || type === "input_text" || type === "output_text") {
|
|
99
|
-
const text = String(block.text || block.content || "");
|
|
100
|
-
if (text) parts.push({ type: type === "text" ? outputRole : type, text });
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if (type === "image_url") {
|
|
104
|
-
const image = block.image_url;
|
|
105
|
-
const url = typeof image === "string" ? image : image && image.url;
|
|
106
|
-
if (url) parts.push({ type: "input_image", image_url: String(url) });
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (type === "image") {
|
|
110
|
-
const source = block.source && typeof block.source === "object" ? block.source : block;
|
|
111
|
-
if (source && source.type === "base64" && source.data) {
|
|
112
|
-
const mediaType = String(source.media_type || "application/octet-stream");
|
|
113
|
-
parts.push({
|
|
114
|
-
type: "input_image",
|
|
115
|
-
image_url: `data:${mediaType};base64,${String(source.data)}`,
|
|
116
|
-
});
|
|
117
|
-
} else if (block.url) {
|
|
118
|
-
parts.push({ type: "input_image", image_url: String(block.url) });
|
|
119
|
-
}
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (type === "reasoning" && block.encrypted_content) {
|
|
123
|
-
parts.push({ type: "reasoning", encrypted_content: String(block.encrypted_content) });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return parts;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function messagesToResponsesInput(messages = []) {
|
|
130
|
-
const input = [];
|
|
131
|
-
for (const message of Array.isArray(messages) ? messages : []) {
|
|
132
|
-
if (!message || typeof message !== "object") continue;
|
|
133
|
-
const role = String(message.role || "user").trim().toLowerCase() || "user";
|
|
134
|
-
|
|
135
|
-
if (role === "tool") {
|
|
136
|
-
const callId = String(message.tool_call_id || message.call_id || "").trim();
|
|
137
|
-
if (callId) {
|
|
138
|
-
input.push({
|
|
139
|
-
type: "function_call_output",
|
|
140
|
-
call_id: callId,
|
|
141
|
-
output: typeof message.content === "string" ? message.content : toJsonString(message.content),
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (role === "assistant" && Array.isArray(message.tool_calls)) {
|
|
148
|
-
// With store=false and no previous_response_id, encrypted reasoning
|
|
149
|
-
// output must be replayed before the corresponding function calls. The
|
|
150
|
-
// items live on a non-enumerable message property, so this projection is
|
|
151
|
-
// available only to the in-flight provider loop and cannot enter the
|
|
152
|
-
// Session Journal through JSON serialization.
|
|
153
|
-
input.push(...getResponsesReasoningItems(message));
|
|
154
|
-
for (const call of message.tool_calls) {
|
|
155
|
-
if (!call || typeof call !== "object") continue;
|
|
156
|
-
const fn = call.function && typeof call.function === "object" ? call.function : call;
|
|
157
|
-
const callId = String(call.id || call.call_id || "").trim();
|
|
158
|
-
const name = String(fn.name || "").trim();
|
|
159
|
-
if (!callId || !name) continue;
|
|
160
|
-
input.push({
|
|
161
|
-
type: "function_call",
|
|
162
|
-
call_id: callId,
|
|
163
|
-
name,
|
|
164
|
-
arguments: typeof fn.arguments === "string" ? fn.arguments : toJsonString(fn.arguments),
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
const assistantText = normalizeContentParts(message.content, "assistant");
|
|
168
|
-
if (assistantText.length > 0) {
|
|
169
|
-
input.push({ type: "message", role: "assistant", content: assistantText });
|
|
170
|
-
}
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const responseRole = role === "system" ? "developer" : role;
|
|
175
|
-
const content = normalizeContentParts(message.content, role);
|
|
176
|
-
if (content.length > 0) {
|
|
177
|
-
input.push({ type: "message", role: responseRole, content });
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return input;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function toolsToResponsesTools(tools = []) {
|
|
184
|
-
const result = [];
|
|
185
|
-
for (const tool of Array.isArray(tools) ? tools : []) {
|
|
186
|
-
if (!tool || typeof tool !== "object") continue;
|
|
187
|
-
if (tool.type && tool.type !== "function") {
|
|
188
|
-
// Hosted/namespace tools are provider-specific. Pass through only when
|
|
189
|
-
// they already use the Responses shape.
|
|
190
|
-
if (tool.type === "web_search" || tool.type === "x_search" || tool.type === "image_generation") {
|
|
191
|
-
result.push({ ...tool });
|
|
192
|
-
}
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
const fn = tool.function && typeof tool.function === "object" ? tool.function : tool;
|
|
196
|
-
const name = String(fn.name || "").trim();
|
|
197
|
-
if (!name) continue;
|
|
198
|
-
result.push({
|
|
199
|
-
type: "function",
|
|
200
|
-
name,
|
|
201
|
-
description: String(fn.description || ""),
|
|
202
|
-
parameters: fn.parameters && typeof fn.parameters === "object"
|
|
203
|
-
? fn.parameters
|
|
204
|
-
: { type: "object", properties: {}, additionalProperties: true },
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
return result;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function buildResponsesPayload({
|
|
211
|
-
model = "",
|
|
212
|
-
instructions = "",
|
|
213
|
-
messages = [],
|
|
214
|
-
tools = [],
|
|
215
|
-
maxOutputTokens = DEFAULT_RESPONSES_MAX_OUTPUT_TOKENS,
|
|
216
|
-
reasoningEffort = "",
|
|
217
|
-
provider = "",
|
|
218
|
-
} = {}) {
|
|
219
|
-
const payload = {
|
|
220
|
-
model: String(model || "").trim(),
|
|
221
|
-
instructions: String(instructions || ""),
|
|
222
|
-
input: messagesToResponsesInput(messages),
|
|
223
|
-
max_output_tokens: Math.max(1, Number(maxOutputTokens) || DEFAULT_RESPONSES_MAX_OUTPUT_TOKENS),
|
|
224
|
-
stream: true,
|
|
225
|
-
store: false,
|
|
226
|
-
parallel_tool_calls: true,
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
const responseTools = toolsToResponsesTools(tools);
|
|
230
|
-
if (responseTools.length > 0) {
|
|
231
|
-
payload.tools = responseTools;
|
|
232
|
-
payload.tool_choice = "auto";
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const normalizedProvider = String(provider || "").trim().toLowerCase();
|
|
236
|
-
if (normalizedProvider === "codex" || normalizedProvider === "openai"
|
|
237
|
-
|| normalizedProvider === "grok-build" || normalizedProvider === "xai") {
|
|
238
|
-
payload.include = ["reasoning.encrypted_content"];
|
|
239
|
-
}
|
|
240
|
-
const effort = String(reasoningEffort || "").trim();
|
|
241
|
-
if (effort) {
|
|
242
|
-
payload.reasoning = { effort, summary: "auto" };
|
|
243
|
-
} else if (normalizedProvider === "codex") {
|
|
244
|
-
payload.reasoning = { effort: "medium", summary: "auto" };
|
|
245
|
-
}
|
|
246
|
-
return payload;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function parseJson(value) {
|
|
250
|
-
if (value && typeof value === "object") return value;
|
|
251
|
-
try {
|
|
252
|
-
return JSON.parse(String(value || ""));
|
|
253
|
-
} catch {
|
|
254
|
-
return null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function isResponsesKeepalive(eventName = "", data = null) {
|
|
259
|
-
if (String(eventName || "").trim().toLowerCase() === "keepalive") return true;
|
|
260
|
-
const parsed = parseJson(data);
|
|
261
|
-
return Boolean(parsed && parsed.type === "keepalive");
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function responseEventDelta(frame = {}) {
|
|
265
|
-
const data = parseJson(frame.data);
|
|
266
|
-
if (!data || isResponsesKeepalive(frame.event, data)) {
|
|
267
|
-
return { text: "", reasoning: "", type: "" };
|
|
268
|
-
}
|
|
269
|
-
const type = String(data.type || frame.event || "").trim();
|
|
270
|
-
if (type === "response.output_text.delta") {
|
|
271
|
-
return { text: String(data.delta || ""), reasoning: "", type };
|
|
272
|
-
}
|
|
273
|
-
if (/^response\.(?:reasoning|reasoning_text|reasoning_summary|reasoning_summary_text)\.(?:delta|text\.delta)$/.test(type)) {
|
|
274
|
-
return { text: "", reasoning: String(data.delta || data.text || ""), type };
|
|
275
|
-
}
|
|
276
|
-
return { text: "", reasoning: "", type };
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function responseOutputText(output = []) {
|
|
280
|
-
const parts = [];
|
|
281
|
-
for (const item of Array.isArray(output) ? output : []) {
|
|
282
|
-
if (!item || typeof item !== "object") continue;
|
|
283
|
-
if (item.type === "message") {
|
|
284
|
-
for (const content of Array.isArray(item.content) ? item.content : []) {
|
|
285
|
-
if (content && (content.type === "output_text" || content.type === "text")) {
|
|
286
|
-
parts.push(String(content.text || ""));
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
} else if (item.type === "output_text") {
|
|
290
|
-
parts.push(String(item.text || ""));
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return parts.join("");
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function responseUsage(response = null) {
|
|
297
|
-
const usage = response && typeof response === "object" && response.usage && typeof response.usage === "object"
|
|
298
|
-
? response.usage
|
|
299
|
-
: null;
|
|
300
|
-
if (!usage) return null;
|
|
301
|
-
const inputDetails = usage.input_token_details && typeof usage.input_token_details === "object"
|
|
302
|
-
? usage.input_token_details
|
|
303
|
-
: {};
|
|
304
|
-
return {
|
|
305
|
-
input: Number(usage.input_tokens || usage.prompt_tokens || 0) || 0,
|
|
306
|
-
output: Number(usage.output_tokens || usage.completion_tokens || 0) || 0,
|
|
307
|
-
cacheRead: Number(
|
|
308
|
-
inputDetails.cached_tokens || usage.cached_input_tokens || usage.cache_read_input_tokens || 0,
|
|
309
|
-
) || 0,
|
|
310
|
-
cacheCreation: Number(usage.cache_creation_input_tokens || usage.cache_creation_tokens || 0) || 0,
|
|
311
|
-
inputIncludesCache: true,
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function responseToolKey(data = {}, item = null) {
|
|
316
|
-
return String(
|
|
317
|
-
data.item_id
|
|
318
|
-
|| (item && item.id)
|
|
319
|
-
|| data.output_index
|
|
320
|
-
|| (item && (item.output_index || item.call_id))
|
|
321
|
-
|| data.call_id
|
|
322
|
-
|| "",
|
|
323
|
-
).trim();
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function mergeFunctionCall(toolMap, data = {}, item = null) {
|
|
327
|
-
const key = responseToolKey(data, item);
|
|
328
|
-
if (!key) return;
|
|
329
|
-
const existing = toolMap.get(key) || {
|
|
330
|
-
order: Number.isFinite(Number(data.output_index)) ? Number(data.output_index) : toolMap.size,
|
|
331
|
-
id: "",
|
|
332
|
-
name: "",
|
|
333
|
-
arguments: "",
|
|
334
|
-
};
|
|
335
|
-
const source = item && typeof item === "object" ? item : data;
|
|
336
|
-
existing.id = String(source.call_id || source.id || existing.id || data.call_id || "").trim();
|
|
337
|
-
existing.name = String(source.name || existing.name || data.name || "").trim();
|
|
338
|
-
if (typeof source.arguments === "string") existing.arguments = source.arguments;
|
|
339
|
-
if (typeof data.arguments === "string") existing.arguments = data.arguments;
|
|
340
|
-
if (typeof data.delta === "string" && data.type === "response.function_call_arguments.delta") {
|
|
341
|
-
existing.arguments += data.delta;
|
|
342
|
-
}
|
|
343
|
-
toolMap.set(key, existing);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function mergeResponseOutput(toolMap, output = []) {
|
|
347
|
-
for (const item of Array.isArray(output) ? output : []) {
|
|
348
|
-
if (!item || typeof item !== "object") continue;
|
|
349
|
-
if (item.type === "function_call") mergeFunctionCall(toolMap, item, item);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
function mergeOutputItem(outputItemMap, data = {}, item = null) {
|
|
354
|
-
if (!item || typeof item !== "object") return;
|
|
355
|
-
const numericIndex = Number(data.output_index);
|
|
356
|
-
const hasIndex = Number.isFinite(numericIndex);
|
|
357
|
-
const key = hasIndex
|
|
358
|
-
? `index:${numericIndex}`
|
|
359
|
-
: `id:${String(item.id || outputItemMap.size)}`;
|
|
360
|
-
outputItemMap.set(key, {
|
|
361
|
-
order: hasIndex ? numericIndex : outputItemMap.size,
|
|
362
|
-
item: cloneJsonValue(item),
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function mergeCompletedOutput(outputItemMap, output = []) {
|
|
367
|
-
for (const [index, item] of (Array.isArray(output) ? output : []).entries()) {
|
|
368
|
-
if (!item || typeof item !== "object") continue;
|
|
369
|
-
const key = `index:${index}`;
|
|
370
|
-
if (!outputItemMap.has(key)) {
|
|
371
|
-
mergeOutputItem(outputItemMap, { output_index: index }, item);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
function parseResponsesEvents(frames = [], { assumeCompleted = false } = {}) {
|
|
377
|
-
const events = [];
|
|
378
|
-
const textParts = [];
|
|
379
|
-
const reasoningParts = [];
|
|
380
|
-
const toolMap = new Map();
|
|
381
|
-
const outputItemMap = new Map();
|
|
382
|
-
let response = null;
|
|
383
|
-
let terminal = assumeCompleted ? "completed" : "";
|
|
384
|
-
|
|
385
|
-
for (const frame of Array.isArray(frames) ? frames : []) {
|
|
386
|
-
const data = parseJson(frame && frame.data);
|
|
387
|
-
if (!data || isResponsesKeepalive(frame && frame.event, data)) continue;
|
|
388
|
-
events.push(data);
|
|
389
|
-
const type = String(data.type || (frame && frame.event) || "").trim();
|
|
390
|
-
const delta = responseEventDelta({ event: type, data });
|
|
391
|
-
if (delta.text) textParts.push(delta.text);
|
|
392
|
-
if (delta.reasoning) reasoningParts.push(delta.reasoning);
|
|
393
|
-
|
|
394
|
-
if (type === "response.output_item.added" || type === "response.output_item.done") {
|
|
395
|
-
mergeFunctionCall(toolMap, data, data.item);
|
|
396
|
-
mergeOutputItem(outputItemMap, data, data.item);
|
|
397
|
-
} else if (type === "response.function_call_arguments.delta"
|
|
398
|
-
|| type === "response.function_call_arguments.done") {
|
|
399
|
-
mergeFunctionCall(toolMap, data, null);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
if (type === "response.completed" || type === "response.incomplete") {
|
|
403
|
-
terminal = type === "response.completed" ? "completed" : "incomplete";
|
|
404
|
-
if (data.response && typeof data.response === "object") {
|
|
405
|
-
response = data.response;
|
|
406
|
-
mergeResponseOutput(toolMap, response.output);
|
|
407
|
-
mergeCompletedOutput(outputItemMap, response.output);
|
|
408
|
-
if (!textParts.length) {
|
|
409
|
-
const fallbackText = responseOutputText(response.output);
|
|
410
|
-
if (fallbackText) textParts.push(fallbackText);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
} else if (!response && data.object === "response") {
|
|
414
|
-
response = data;
|
|
415
|
-
mergeResponseOutput(toolMap, response.output);
|
|
416
|
-
mergeCompletedOutput(outputItemMap, response.output);
|
|
417
|
-
if (!textParts.length) {
|
|
418
|
-
const fallbackText = responseOutputText(response.output);
|
|
419
|
-
if (fallbackText) textParts.push(fallbackText);
|
|
420
|
-
}
|
|
421
|
-
if (data.status === "completed" || data.status === "incomplete") {
|
|
422
|
-
terminal = data.status;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
const toolCalls = Array.from(toolMap.values())
|
|
428
|
-
.sort((left, right) => left.order - right.order)
|
|
429
|
-
.filter((call) => call.id && call.name)
|
|
430
|
-
.map((call) => ({
|
|
431
|
-
id: call.id,
|
|
432
|
-
type: "function",
|
|
433
|
-
function: {
|
|
434
|
-
name: call.name,
|
|
435
|
-
arguments: call.arguments || "{}",
|
|
436
|
-
},
|
|
437
|
-
}));
|
|
438
|
-
const outputItems = Array.from(outputItemMap.values())
|
|
439
|
-
.sort((left, right) => left.order - right.order)
|
|
440
|
-
.map((entry) => entry.item)
|
|
441
|
-
.filter(Boolean);
|
|
442
|
-
|
|
443
|
-
return {
|
|
444
|
-
text: textParts.join(""),
|
|
445
|
-
reasoning: reasoningParts.join(""),
|
|
446
|
-
toolCalls,
|
|
447
|
-
outputItems,
|
|
448
|
-
response,
|
|
449
|
-
responseId: String(response && response.id || "").trim(),
|
|
450
|
-
usage: responseUsage(response),
|
|
451
|
-
terminal,
|
|
452
|
-
incompleteDetails: response && response.incomplete_details ? response.incomplete_details : null,
|
|
453
|
-
events,
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function parseSseBlocks(raw = "") {
|
|
458
|
-
const text = String(raw || "");
|
|
459
|
-
const blocks = text.split(/\r?\n\r?\n/);
|
|
460
|
-
const rest = blocks.pop() || "";
|
|
461
|
-
return { blocks: blocks.filter(Boolean), rest };
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
function parseSseFrame(block = "") {
|
|
465
|
-
let event = "";
|
|
466
|
-
const dataLines = [];
|
|
467
|
-
for (const line of String(block || "").split(/\r?\n/)) {
|
|
468
|
-
if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
469
|
-
if (line.startsWith("data:")) dataLines.push(line.slice(5).trimStart());
|
|
470
|
-
}
|
|
471
|
-
return { event, data: dataLines.join("\n").trim() };
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
function parseResponsesSsePayload(payload = "") {
|
|
475
|
-
const frames = [];
|
|
476
|
-
let buffer = String(payload || "");
|
|
477
|
-
const parsed = parseSseBlocks(buffer);
|
|
478
|
-
for (const block of parsed.blocks) frames.push(parseSseFrame(block));
|
|
479
|
-
buffer = parsed.rest;
|
|
480
|
-
if (buffer.trim()) frames.push(parseSseFrame(buffer));
|
|
481
|
-
return parseResponsesEvents(frames);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
module.exports = {
|
|
485
|
-
DEFAULT_RESPONSES_MAX_OUTPUT_TOKENS,
|
|
486
|
-
attachResponsesReasoningItems,
|
|
487
|
-
getResponsesReasoningItems,
|
|
488
|
-
reasoningItemsFromResponse,
|
|
489
|
-
resolveResponsesUrl,
|
|
490
|
-
messagesToResponsesInput,
|
|
491
|
-
toolsToResponsesTools,
|
|
492
|
-
buildResponsesPayload,
|
|
493
|
-
isResponsesKeepalive,
|
|
494
|
-
responseEventDelta,
|
|
495
|
-
responseOutputText,
|
|
496
|
-
responseUsage,
|
|
497
|
-
parseResponsesEvents,
|
|
498
|
-
parseResponsesSsePayload,
|
|
499
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/providers/transports/responsesProtocol");
|
|
@@ -1,50 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* Transports own wire-format conversion and turn execution only.
|
|
7
|
-
* They must not decide Plan Mode, write leases, or tool batch policy.
|
|
8
|
-
*
|
|
9
|
-
* Required methods:
|
|
10
|
-
* - resolveUrl(baseUrl) → string
|
|
11
|
-
* - prepareMessages({ messages, systemPrompt?, prompt })
|
|
12
|
-
* - runTurn(params) → Promise<turnResult>
|
|
13
|
-
* - getToolCalls(turnResult) → array
|
|
14
|
-
* - appendFinalAssistantMessage({ messages, turnResult })
|
|
15
|
-
* - prepareToolCalls({ messages, turnResult?, toolCalls }) → pendingCalls|null
|
|
16
|
-
* - appendToolResult({ messages?, collected?, call, toolResult })
|
|
17
|
-
* - flushToolResults?({ messages, collected }) // Anthropic-style batch
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
const TRANSPORT_NAMES = Object.freeze([
|
|
21
|
-
"openai-chat",
|
|
22
|
-
"openai-responses",
|
|
23
|
-
"anthropic-messages",
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
function assertTransport(transport = null, name = "") {
|
|
27
|
-
if (!transport || typeof transport !== "object") {
|
|
28
|
-
throw new Error(`missing transport${name ? `: ${name}` : ""}`);
|
|
29
|
-
}
|
|
30
|
-
const required = [
|
|
31
|
-
"resolveUrl",
|
|
32
|
-
"prepareMessages",
|
|
33
|
-
"runTurn",
|
|
34
|
-
"getToolCalls",
|
|
35
|
-
"appendFinalAssistantMessage",
|
|
36
|
-
"prepareToolCalls",
|
|
37
|
-
"appendToolResult",
|
|
38
|
-
];
|
|
39
|
-
for (const key of required) {
|
|
40
|
-
if (typeof transport[key] !== "function") {
|
|
41
|
-
throw new Error(`transport ${name || "?"} missing ${key}`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return transport;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
module.exports = {
|
|
48
|
-
TRANSPORT_NAMES,
|
|
49
|
-
assertTransport,
|
|
50
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/providers/transports/transportContract");
|
|
@@ -1,151 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
function extractVisionPayload(toolResult = null) {
|
|
8
|
-
if (!toolResult || typeof toolResult !== "object") return null;
|
|
9
|
-
const base64 = String(toolResult.base64 || "").trim();
|
|
10
|
-
const mediaType = String(toolResult.mediaType || "").trim().toLowerCase();
|
|
11
|
-
if (!base64 || !mediaType.startsWith("image/")) return null;
|
|
12
|
-
if (toolResult.ok === false) return null;
|
|
13
|
-
return {
|
|
14
|
-
path: String(toolResult.path || "").trim(),
|
|
15
|
-
mediaType,
|
|
16
|
-
bytes: Number.isFinite(toolResult.bytes) ? toolResult.bytes : null,
|
|
17
|
-
base64,
|
|
18
|
-
artifactId: String(toolResult.artifactId || "").trim(),
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function isVisionToolResult(toolResult = null) {
|
|
23
|
-
return Boolean(extractVisionPayload(toolResult));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function stripVisionBase64(value) {
|
|
27
|
-
if (Array.isArray(value)) {
|
|
28
|
-
return value.map((item) => stripVisionBase64(item));
|
|
29
|
-
}
|
|
30
|
-
if (!value || typeof value !== "object") return value;
|
|
31
|
-
const out = {};
|
|
32
|
-
for (const [key, entry] of Object.entries(value)) {
|
|
33
|
-
if (key === "base64") continue;
|
|
34
|
-
out[key] = stripVisionBase64(entry);
|
|
35
|
-
}
|
|
36
|
-
return out;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function visionSummaryText(vision = null, toolResult = null) {
|
|
40
|
-
const pathText = (vision && vision.path) || (toolResult && toolResult.path) || "image";
|
|
41
|
-
const mediaType = (vision && vision.mediaType) || (toolResult && toolResult.mediaType) || "image/*";
|
|
42
|
-
const bytes = (vision && vision.bytes) != null
|
|
43
|
-
? vision.bytes
|
|
44
|
-
: (toolResult && toolResult.bytes);
|
|
45
|
-
const parts = [
|
|
46
|
-
`Image loaded for vision: ${pathText}`,
|
|
47
|
-
`mediaType=${mediaType}`,
|
|
48
|
-
];
|
|
49
|
-
if (Number.isFinite(bytes)) parts.push(`bytes=${bytes}`);
|
|
50
|
-
if (vision && vision.artifactId) parts.push(`artifactId=${vision.artifactId}`);
|
|
51
|
-
parts.push("Visual content is attached for this model call only; call read_image again later if needed.");
|
|
52
|
-
return parts.join(" | ");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function toAnthropicImageBlock(vision = null) {
|
|
56
|
-
if (!vision) return null;
|
|
57
|
-
return {
|
|
58
|
-
type: "image",
|
|
59
|
-
source: {
|
|
60
|
-
type: "base64",
|
|
61
|
-
media_type: vision.mediaType,
|
|
62
|
-
data: vision.base64,
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function toOpenAiImagePart(vision = null) {
|
|
68
|
-
if (!vision) return null;
|
|
69
|
-
return {
|
|
70
|
-
type: "image_url",
|
|
71
|
-
image_url: {
|
|
72
|
-
url: `data:${vision.mediaType};base64,${vision.base64}`,
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function degradeVisionContent(content) {
|
|
78
|
-
if (typeof content === "string") return content;
|
|
79
|
-
if (!Array.isArray(content)) return content;
|
|
80
|
-
const texts = [];
|
|
81
|
-
for (const block of content) {
|
|
82
|
-
if (!block || typeof block !== "object") continue;
|
|
83
|
-
const type = String(block.type || "").trim().toLowerCase();
|
|
84
|
-
if (type === "text" && block.text) {
|
|
85
|
-
texts.push(String(block.text));
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (type === "image" || type === "image_url") {
|
|
89
|
-
const pathHint = block.path
|
|
90
|
-
|| (block.source && block.source.path)
|
|
91
|
-
|| "";
|
|
92
|
-
texts.push(pathHint ? `[image: ${pathHint}]` : "[image]");
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (type === "tool_result" && Array.isArray(block.content)) {
|
|
96
|
-
texts.push(degradeVisionContent(block.content));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return texts.filter(Boolean).join("\n") || "[multimodal content]";
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Retire only visual blocks after a successful model call. In particular,
|
|
103
|
-
// preserve Anthropic tool_result envelopes and their tool_use_id linkage.
|
|
104
|
-
function retireVisionContent(content) {
|
|
105
|
-
if (!Array.isArray(content)) return content;
|
|
106
|
-
let changed = false;
|
|
107
|
-
const retired = content.map((block) => {
|
|
108
|
-
if (!block || typeof block !== "object") return block;
|
|
109
|
-
const type = String(block.type || "").toLowerCase();
|
|
110
|
-
if (type === "image" || type === "image_url" || type === "input_image") {
|
|
111
|
-
changed = true;
|
|
112
|
-
const pathHint = block.path || (block.source && block.source.path) || "";
|
|
113
|
-
return {
|
|
114
|
-
type: "text",
|
|
115
|
-
text: pathHint
|
|
116
|
-
? `[image already viewed: ${pathHint}; use read_image to view again]`
|
|
117
|
-
: "[image already viewed; use read_image with the recorded path to view again]",
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
if (Array.isArray(block.content)) {
|
|
121
|
-
const nested = retireVisionContent(block.content);
|
|
122
|
-
if (nested !== block.content) {
|
|
123
|
-
changed = true;
|
|
124
|
-
return { ...block, content: nested };
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return block;
|
|
128
|
-
});
|
|
129
|
-
return changed ? retired : content;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function retireMessageImages(messages = []) {
|
|
133
|
-
for (let i = 0; i < messages.length; i += 1) {
|
|
134
|
-
const message = messages[i];
|
|
135
|
-
if (message && Array.isArray(message.content)) {
|
|
136
|
-
const content = retireVisionContent(message.content);
|
|
137
|
-
if (content !== message.content) messages[i] = { ...message, content };
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
module.exports = {
|
|
143
|
-
extractVisionPayload,
|
|
144
|
-
isVisionToolResult,
|
|
145
|
-
stripVisionBase64,
|
|
146
|
-
visionSummaryText,
|
|
147
|
-
toAnthropicImageBlock,
|
|
148
|
-
toOpenAiImagePart,
|
|
149
|
-
degradeVisionContent,
|
|
150
|
-
retireMessageImages,
|
|
151
|
-
};
|
|
3
|
+
// Existing public entry; implementation belongs to the shared agent layer.
|
|
4
|
+
module.exports = require("../../agents/providers/transports/visionBlocks");
|