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
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_RESPONSES_MAX_OUTPUT_TOKENS = 32768;
|
|
4
|
+
const RESPONSES_REASONING_ITEMS = Symbol("ufoo.responsesReasoningItems");
|
|
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
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function systemBlocksToAnthropicPayload(blocks = []) {
|
|
4
|
+
const list = (Array.isArray(blocks) ? blocks : []).filter((b) => b && b.text);
|
|
5
|
+
const ANTHROPIC_CACHE_CONTROL = { type: "ephemeral" };
|
|
6
|
+
// Place cache breakpoints on every cacheable layer so Anthropic can reuse
|
|
7
|
+
// Immutable → SessionStable → Epoch prefixes independently. Turn-dynamic
|
|
8
|
+
// never gets cache_control.
|
|
9
|
+
return list.map((block) => {
|
|
10
|
+
const entry = { type: "text", text: block.text };
|
|
11
|
+
if (block.cacheable) entry.cache_control = { ...ANTHROPIC_CACHE_CONTROL };
|
|
12
|
+
return entry;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = { systemBlocksToAnthropicPayload };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transport contract for native Agent Loop Provider adapters.
|
|
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
|
+
};
|