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.
Files changed (266) hide show
  1. package/OBSERVABILITY.md +65 -0
  2. package/README.md +238 -21
  3. package/README.zh-CN.md +165 -20
  4. package/SKILLS/ufoo/SKILL.md +40 -4
  5. package/SKILLS/ufoo-bus/SKILL.md +46 -3
  6. package/bin/uclaude.js +4 -0
  7. package/bin/ucodex.js +4 -0
  8. package/bin/ufoo.js +6 -4
  9. package/dist/tui/darwin-arm64/ufoo-tui +0 -0
  10. package/dist/tui/darwin-x64/ufoo-tui +0 -0
  11. package/dist/tui/linux-arm64/ufoo-tui +0 -0
  12. package/dist/tui/linux-x64/ufoo-tui +0 -0
  13. package/package.json +14 -7
  14. package/scripts/postinstall-skills.js +53 -0
  15. package/scripts/postinstall.js +11 -16
  16. package/scripts/smoke-native-messages.js +214 -0
  17. package/src/agents/activity/activityDetector.js +1 -1
  18. package/src/agents/activity/activityStateWriter.js +37 -38
  19. package/src/agents/capabilities/agentManagement/index.js +4 -0
  20. package/src/agents/capabilities/agentManagement/service.js +140 -0
  21. package/src/agents/capabilities/agentRouting/index.js +4 -0
  22. package/src/agents/capabilities/coding/index.js +32 -0
  23. package/src/agents/capabilities/coordination/index.js +37 -0
  24. package/src/agents/capabilities/globalRouting/index.js +5 -0
  25. package/src/agents/capabilities/groups/index.js +3 -0
  26. package/src/agents/capabilities/index.js +25 -0
  27. package/src/agents/capabilities/planning/index.js +123 -0
  28. package/src/agents/capabilities/planning/main.js +28 -0
  29. package/src/agents/capabilities/projectDiscovery/index.js +3 -0
  30. package/src/agents/capabilities/scheduling/index.js +3 -0
  31. package/src/agents/capabilities/sharedMemory/index.js +4 -0
  32. package/src/agents/capabilities/skills/index.js +12 -0
  33. package/src/agents/capabilities/taskExecution/index.js +3 -0
  34. package/src/agents/capabilities/taskReports/index.js +6 -0
  35. package/src/agents/controller/controllerToolExecutor.js +40 -22
  36. package/src/agents/controller/index.js +1 -6
  37. package/src/agents/internal/internalRunner.js +181 -76
  38. package/src/agents/launch/index.js +0 -1
  39. package/src/agents/launch/launcher.js +96 -14
  40. package/src/agents/launch/nativeCapabilities.js +40 -0
  41. package/src/agents/launch/nativeMessages.js +362 -0
  42. package/src/agents/launch/notifier.js +15 -20
  43. package/src/agents/launch/readyDetector.js +28 -14
  44. package/src/agents/profiles/coding.js +6 -0
  45. package/src/agents/profiles/globalRouter.js +2 -0
  46. package/src/agents/profiles/main.js +5 -0
  47. package/src/agents/prompts/cooperation.js +14 -0
  48. package/src/agents/prompts/defaultBootstrap.js +1 -1
  49. package/src/agents/prompts/groupBootstrap.js +32 -6
  50. package/src/agents/prompts/mainAgent.js +48 -0
  51. package/src/agents/prompts/native/system.js +4 -1
  52. package/src/agents/prompts/native/tasks.js +2 -2
  53. package/src/agents/prompts/native/ufoo.js +14 -3
  54. package/src/agents/prompts/promptProfiles.js +2 -0
  55. package/src/agents/prompts/reporting.js +16 -0
  56. package/src/agents/providers/claudeEventTranslator.js +9 -1
  57. package/src/agents/providers/claudeThreadProvider.js +47 -4
  58. package/src/agents/providers/clientIdentity.js +75 -0
  59. package/src/agents/providers/codexEventTranslator.js +68 -7
  60. package/src/agents/providers/codexThreadProvider.js +18 -8
  61. package/src/agents/providers/credentials/claude.js +10 -3
  62. package/src/agents/providers/credentials/index.js +2 -0
  63. package/src/agents/providers/directAuthStatus.js +1 -1
  64. package/src/agents/providers/index.js +1 -1
  65. package/src/agents/providers/modelSettings.js +34 -0
  66. package/src/agents/providers/modelsCatalog.js +379 -0
  67. package/src/agents/providers/nativeTransport.js +1146 -0
  68. package/src/agents/providers/runtimeConfig.js +188 -0
  69. package/src/agents/providers/thinkingLevels.js +106 -0
  70. package/src/agents/providers/transports/anthropicMessagesTransport.js +121 -0
  71. package/src/agents/providers/transports/index.js +10 -0
  72. package/src/agents/providers/transports/openaiChatTransport.js +117 -0
  73. package/src/agents/providers/transports/openaiResponsesTransport.js +120 -0
  74. package/src/agents/providers/transports/responsesProtocol.js +504 -0
  75. package/src/agents/providers/transports/systemBlocks.js +16 -0
  76. package/src/agents/providers/transports/transportContract.js +50 -0
  77. package/src/agents/providers/transports/visionBlocks.js +151 -0
  78. package/src/agents/providers/upstreamRuntime.js +135 -0
  79. package/src/agents/runtime/composeCapabilities.js +89 -0
  80. package/src/agents/runtime/context/checkpointStore.js +38 -0
  81. package/src/agents/runtime/context/commandStore.js +43 -0
  82. package/src/agents/runtime/context/compaction.js +151 -0
  83. package/src/agents/runtime/context/contextBudget.js +93 -0
  84. package/src/agents/runtime/context/contextWindow.js +119 -0
  85. package/src/agents/runtime/context/fileLock.js +36 -0
  86. package/src/agents/runtime/context/replayHistory.js +28 -0
  87. package/src/agents/runtime/context/runtimeStore.js +127 -0
  88. package/src/agents/runtime/context/sessionJournal.js +360 -0
  89. package/src/agents/runtime/context/sessionStore.js +57 -0
  90. package/src/agents/runtime/core/agentLoop.js +351 -0
  91. package/src/agents/runtime/core/executionBudget.js +36 -0
  92. package/src/agents/runtime/core/stableJson.js +29 -0
  93. package/src/agents/runtime/core/toolLoopGuard.js +55 -0
  94. package/src/agents/runtime/createAgentRuntime.js +259 -0
  95. package/src/agents/runtime/index.js +14 -0
  96. package/src/agents/runtime/prompts/responseLanguage.js +12 -0
  97. package/src/agents/runtime/protocol/faultHarness.js +90 -0
  98. package/src/agents/runtime/protocol/materialize.js +107 -0
  99. package/src/agents/runtime/protocol/runObservation.js +24 -0
  100. package/src/agents/runtime/protocol/toolCallLedger.js +221 -0
  101. package/src/agents/runtime/protocol/traceContext.js +9 -0
  102. package/src/agents/runtime/tasks/resourceLease.js +66 -0
  103. package/src/agents/runtime/tasks/scheduler.js +73 -0
  104. package/src/agents/runtime/tools/registry.js +57 -0
  105. package/src/app/chat/ChatController.js +29 -45
  106. package/src/app/chat/agentDirectory.js +9 -18
  107. package/src/app/chat/bootstrap.js +1 -2
  108. package/src/app/chat/commandExecutor.js +111 -313
  109. package/src/app/chat/commands.js +39 -45
  110. package/src/app/chat/daemonConnection.js +58 -5
  111. package/src/app/chat/daemonMessageRouter.js +61 -48
  112. package/src/app/chat/historyStore.js +12 -48
  113. package/src/app/chat/inputSubmitHandler.js +21 -35
  114. package/src/app/chat/internalAgentLogHistory.js +19 -60
  115. package/src/app/chat/launchRequestContext.js +42 -0
  116. package/src/app/chat/multiWindow/paneManager.js +91 -164
  117. package/src/app/chat/streamState.js +3 -7
  118. package/src/app/chat/transport.js +1 -1
  119. package/src/app/cli/busCoreCommands.js +9 -0
  120. package/src/app/cli/features/doctor.js +33 -0
  121. package/src/app/cli/features/init.js +5 -5
  122. package/src/app/cli/features/skills.js +16 -4
  123. package/src/app/cli/metricsCommands.js +68 -0
  124. package/src/app/cli/reportCoreCommands.js +37 -0
  125. package/src/app/cli/run.js +82 -42
  126. package/src/app/cli/traceCommands.js +36 -0
  127. package/src/code/agent.js +34 -17
  128. package/src/code/busConsumer.js +1 -3
  129. package/src/code/commands.js +1 -1
  130. package/src/code/context/artifacts.js +12 -11
  131. package/src/code/context/assembler.js +9 -9
  132. package/src/code/context/promptLayers.js +1 -13
  133. package/src/code/context/stableJson.js +2 -27
  134. package/src/code/context/stateCommit.js +2 -1
  135. package/src/code/contextWindow.js +2 -117
  136. package/src/code/conversation/sessionJournal.js +3 -349
  137. package/src/code/internalThread.js +107 -0
  138. package/src/code/launcher/ucode.js +1 -1
  139. package/src/code/launcher/ucodeBootstrap.js +1 -1
  140. package/src/code/modelCommand.js +11 -35
  141. package/src/code/nativeRunner.js +104 -2697
  142. package/src/code/protocol/faultHarness.js +2 -88
  143. package/src/code/protocol/materialize.js +2 -105
  144. package/src/code/protocol/toolCallLedger.js +5 -215
  145. package/src/code/providers/anthropicMessagesTransport.js +2 -118
  146. package/src/code/providers/modelsCatalog.js +1 -317
  147. package/src/code/providers/openaiChatTransport.js +2 -114
  148. package/src/code/providers/openaiResponsesTransport.js +2 -117
  149. package/src/code/providers/responsesProtocol.js +2 -497
  150. package/src/code/providers/transportContract.js +2 -48
  151. package/src/code/providers/visionBlocks.js +2 -149
  152. package/src/code/repl.js +2 -11
  153. package/src/code/sessionStore.js +27 -99
  154. package/src/code/taskDecomposer.js +18 -0
  155. package/src/code/thinkingLevels.js +1 -130
  156. package/src/code/toolBudget.js +1 -17
  157. package/src/code/toolLoopGuard.js +3 -49
  158. package/src/code/tools/artifactRead.js +1 -1
  159. package/src/code/tools/executor.js +373 -0
  160. package/src/code/tools/specs.js +312 -0
  161. package/src/config.js +95 -131
  162. package/src/coordination/bus/deliveryQueue.js +16 -2
  163. package/src/coordination/bus/index.js +14 -9
  164. package/src/coordination/bus/inject.js +15 -7
  165. package/src/coordination/bus/message.js +15 -1
  166. package/src/coordination/bus/nativeReceipts.js +100 -0
  167. package/src/coordination/bus/nickname.js +1 -1
  168. package/src/coordination/bus/queue.js +1 -3
  169. package/src/coordination/bus/subscriber.js +3 -2
  170. package/src/coordination/bus/utils.js +8 -4
  171. package/src/coordination/history/agentPerformance.js +279 -0
  172. package/src/coordination/history/agentSurface.js +142 -0
  173. package/src/coordination/history/agentTrace.js +249 -0
  174. package/src/coordination/memory/index.js +5 -21
  175. package/src/coordination/report/store.js +103 -83
  176. package/src/coordination/state/agentsStore.js +57 -3
  177. package/src/coordination/state/fileLock.js +2 -0
  178. package/src/coordination/state/workspaceAccess.js +36 -0
  179. package/src/orchestration/controller/index.js +1 -8
  180. package/src/orchestration/controller/launchRouting.js +1 -1
  181. package/src/orchestration/solo/commands.js +3 -1
  182. package/src/runtime/contracts/agentMode.js +26 -0
  183. package/src/runtime/contracts/eventContract.js +3 -0
  184. package/src/runtime/contracts/uiProtocol.js +20 -17
  185. package/src/runtime/daemon/agentHost.js +407 -0
  186. package/src/runtime/daemon/agentRecoveryGroups.js +70 -0
  187. package/src/runtime/daemon/busBridge.js +396 -0
  188. package/src/runtime/daemon/claudeChannel.js +159 -0
  189. package/src/runtime/daemon/controlPlaneService.js +28 -18
  190. package/src/runtime/daemon/cronOps.js +25 -17
  191. package/src/runtime/daemon/deliveryScheduler.js +32 -5
  192. package/src/runtime/daemon/globalDaemon.js +28 -1
  193. package/src/runtime/daemon/groupOrchestrator.js +21 -5
  194. package/src/runtime/daemon/groupStatus.js +20 -0
  195. package/src/runtime/daemon/index.js +120 -375
  196. package/src/runtime/daemon/ipcServer.js +32 -10
  197. package/src/runtime/daemon/mcpConfigure.js +113 -20
  198. package/src/runtime/daemon/mcpHttpServer.js +27 -5
  199. package/src/runtime/daemon/mcpServer.js +8 -5
  200. package/src/runtime/daemon/mcpStdioProxy.js +127 -10
  201. package/src/runtime/daemon/nicknameScope.js +1 -1
  202. package/src/runtime/daemon/ops.js +68 -40
  203. package/src/runtime/daemon/projectRuntimeGateway.js +2 -0
  204. package/src/runtime/daemon/promptRequest.js +92 -570
  205. package/src/runtime/daemon/providerSessions.js +67 -33
  206. package/src/runtime/daemon/reportControlBus.js +59 -25
  207. package/src/runtime/daemon/status.js +9 -6
  208. package/src/runtime/privacy/redactor.js +2 -2
  209. package/src/runtime/projects/identity.js +2 -1
  210. package/src/runtime/projects/registry.js +98 -38
  211. package/src/tools/controllerSchemas.js +28 -0
  212. package/src/tools/handlers/dispatchMessage.js +5 -2
  213. package/src/tools/handlers/listAgents.js +3 -1
  214. package/src/tools/handlers/readBusSummary.js +3 -1
  215. package/src/tools/handlers/readProjectRegistry.js +29 -2
  216. package/src/tools/handlers/routeAgent.js +35 -0
  217. package/src/tools/handlers/tier2.js +4 -2
  218. package/src/tools/index.js +1 -0
  219. package/src/tools/registry.js +6 -0
  220. package/src/tools/schemaFixtures.js +42 -5
  221. package/src/tools/tier0/readAgentMetrics.js +14 -0
  222. package/src/tools/tier0/readAgentTrace.js +12 -0
  223. package/src/tools/tier1/routeAgent.js +2 -2
  224. package/src/tools/tier2/runtimeCoordination.js +38 -0
  225. package/src/tools/types.js +2 -0
  226. package/src/ui/agentPresentation.js +109 -0
  227. package/src/ui/agentSettingsController.js +107 -0
  228. package/src/ui/agentSurface.js +270 -0
  229. package/src/ui/dashboardBridge.js +1 -1
  230. package/src/ui/format/banner.js +9 -15
  231. package/src/ui/format/index.js +14 -10
  232. package/src/ui/format/loopSummary.js +50 -0
  233. package/src/ui/format/markdownRenderer.js +3 -4
  234. package/src/ui/format/startupBanner.js +50 -0
  235. package/src/ui/multiPaneBusMirror.js +29 -3
  236. package/src/ui/rustChatHost.js +316 -599
  237. package/src/ui/rustMultiSession.js +288 -73
  238. package/src/ui/rustUcodeHost.js +21 -88
  239. package/src/ui/settingsBridge.js +34 -14
  240. package/src/ui/toolMergeBridge.js +0 -4
  241. package/src/ui/uiHostServer.js +5 -8
  242. package/src/agents/controller/loopObservability.js +0 -200
  243. package/src/agents/controller/loopRuntime.js +0 -472
  244. package/src/agents/controller/ufooAgent.js +0 -829
  245. package/src/agents/launch/ptyRunner.js +0 -1135
  246. package/src/agents/providers/upstreamTransport.js +0 -689
  247. package/src/app/chat/agentBar.js +0 -135
  248. package/src/app/chat/agentEnter.js +0 -70
  249. package/src/app/chat/agentSockets.js +0 -225
  250. package/src/app/chat/dashboardView.js +0 -405
  251. package/src/app/chat/multiWindow/index.js +0 -310
  252. package/src/app/chat/multiWindow/paneLayout.js +0 -84
  253. package/src/app/chat/multiWindow/renderer.js +0 -420
  254. package/src/app/chat/multiWindow/virtualTerminal.js +0 -327
  255. package/src/app/chat/multiWindow/vtFrame.js +0 -93
  256. package/src/app/chat/rawKeyMap.js +0 -42
  257. package/src/app/chat/settingsController.js +0 -111
  258. package/src/orchestration/controller/flags.js +0 -167
  259. package/src/orchestration/controller/gateRouter.js +0 -211
  260. package/src/orchestration/controller/routerFastPath.js +0 -22
  261. package/src/orchestration/controller/routerFinalize.js +0 -222
  262. package/src/orchestration/controller/shadowGuard.js +0 -280
  263. package/src/runtime/daemon/promptLoop.js +0 -136
  264. package/src/runtime/privacy/shadowDiff.js +0 -174
  265. package/src/ui/multiWindowHandoff.js +0 -232
  266. package/src/ui/ptyHandoff.js +0 -23
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+
3
+ const { loadConfig, defaultAgentModelForProvider, sameModelProvider } = require("../../config");
4
+ const { readKimiAccessToken } = require("./credentials/kimi");
5
+
6
+ const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
7
+ const DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com/v1";
8
+ const DEFAULT_KIMI_BASE_URL = "https://api.kimi.com/coding/v1";
9
+ const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api/codex";
10
+ const DEFAULT_XAI_BASE_URL = "https://api.x.ai/v1";
11
+ const DEFAULT_GROK_BUILD_BASE_URL = DEFAULT_XAI_BASE_URL;
12
+ const DEFAULT_GROK_BUILD_MODEL = "grok-4.6";
13
+ const DEFAULT_KIMI_MODEL = "k3";
14
+
15
+ function normalizeProvider(value = "") {
16
+ const text = String(value || "").trim().toLowerCase();
17
+ if (!text) return "";
18
+ if (text === "custom") return "openai";
19
+ if (text === "codex" || text === "codex-cli" || text === "codex-code") return "codex";
20
+ if (text === "claude" || text === "claude-cli" || text === "claude-code") return "anthropic";
21
+ if (text === "kimi" || text === "kimi-code" || text === "moonshot") return "kimi";
22
+ if (text === "grok" || text === "grok-build" || text === "grok-shell" || text === "grok-api") return "grok-build";
23
+ if (text === "xai") return "xai";
24
+ if (text === "openai" || text === "anthropic") return text;
25
+ return text;
26
+ }
27
+
28
+ function normalizeKimiModel(value = "") {
29
+ const raw = String(value || "").trim();
30
+ if (!raw) return "";
31
+ const suffix = raw.match(/^(.*?)(\([^)]*\))$/);
32
+ const base = String(suffix ? suffix[1] : raw).trim().replace(/\[1m\]$/i, "");
33
+ const lower = base.toLowerCase();
34
+ let normalized = lower;
35
+ if (["kimi-k2.7-code", "k2.7-code", "kimi-for-coding", "for-coding"].includes(lower)) {
36
+ normalized = "kimi-for-coding";
37
+ } else if (["kimi-k2.7-code-highspeed", "k2.7-code-highspeed", "kimi-for-coding-highspeed", "for-coding-highspeed"].includes(lower)) {
38
+ normalized = "kimi-for-coding-highspeed";
39
+ } else if (normalized.startsWith("kimi-")) {
40
+ normalized = normalized.slice("kimi-".length);
41
+ }
42
+ return `${normalized}${suffix ? suffix[2] : ""}`;
43
+ }
44
+
45
+ function resolveTransport({ provider = "", baseUrl = "" } = {}) {
46
+ const normalizedProvider = normalizeProvider(provider);
47
+ const url = String(baseUrl || "").trim().toLowerCase();
48
+
49
+ if (normalizedProvider === "codex" || normalizedProvider === "grok-build" || normalizedProvider === "xai") {
50
+ return "openai-responses";
51
+ }
52
+ if (normalizedProvider === "anthropic") return "anthropic-messages";
53
+ if (normalizedProvider === "kimi") return "openai-chat";
54
+ if (/\/responses(?:$|[/?#])/.test(url)) return "openai-responses";
55
+
56
+ return "openai-chat";
57
+ }
58
+
59
+ function resolveRuntimeConfig({ workspaceRoot = process.cwd(), provider = "", model = "", useCodingConfig = true } = {}) {
60
+ const config = loadConfig(workspaceRoot);
61
+ const configuredProvider = normalizeProvider((useCodingConfig ? config.ucodeProvider : "") || config.agentProvider || "");
62
+ const selectedProvider = normalizeProvider(
63
+ provider
64
+ || (useCodingConfig ? process.env.UFOO_UCODE_PROVIDER : "")
65
+ || configuredProvider
66
+ || "openai"
67
+ ) || "openai";
68
+ const custom = selectedProvider === "openai" && config.agentProvider === "custom"
69
+ && (!useCodingConfig || !(config.ucodeProvider || process.env.UFOO_UCODE_PROVIDER));
70
+ const configuredModel = sameModelProvider((useCodingConfig ? config.ucodeProvider : "") || config.agentProvider, selectedProvider)
71
+ ? ((useCodingConfig ? config.ucodeModel : "") || config.agentModel)
72
+ : "";
73
+
74
+ const selectedModel = String(
75
+ model
76
+ || (useCodingConfig ? process.env.UFOO_UCODE_MODEL : "")
77
+ || configuredModel
78
+ || (selectedProvider === "kimi"
79
+ ? DEFAULT_KIMI_MODEL
80
+ : (selectedProvider === "grok-build" ? DEFAULT_GROK_BUILD_MODEL : custom ? "" : defaultAgentModelForProvider(selectedProvider)))
81
+ ).trim();
82
+
83
+ const defaultBaseUrl = selectedProvider === "anthropic"
84
+ ? String(process.env.ANTHROPIC_BASE_URL || DEFAULT_ANTHROPIC_BASE_URL)
85
+ : selectedProvider === "kimi"
86
+ ? String(process.env.KIMI_BASE_URL || DEFAULT_KIMI_BASE_URL)
87
+ : selectedProvider === "codex"
88
+ ? String(process.env.UFOO_CODEX_BASE_URL || DEFAULT_CODEX_BASE_URL)
89
+ : selectedProvider === "grok-build"
90
+ ? String(
91
+ process.env.UFOO_GROK_BUILD_BASE_URL
92
+ || process.env.GROK_BUILD_BASE_URL
93
+ || DEFAULT_GROK_BUILD_BASE_URL
94
+ )
95
+ : selectedProvider === "xai"
96
+ ? String(process.env.XAI_BASE_URL || DEFAULT_XAI_BASE_URL)
97
+ : String(process.env.OPENAI_BASE_URL || DEFAULT_OPENAI_BASE_URL);
98
+
99
+ let baseUrl = String(
100
+ (custom ? process.env.UFOO_AGENT_BASE_URL || config.agentBaseUrl : "")
101
+ || (useCodingConfig ? process.env.UFOO_UCODE_BASE_URL : "")
102
+ || (useCodingConfig ? config.ucodeBaseUrl : "")
103
+ || defaultBaseUrl
104
+ ).trim();
105
+ if (custom) baseUrl = String(process.env.UFOO_AGENT_BASE_URL || config.agentBaseUrl || "").trim();
106
+
107
+ const explicitApiKey = String(custom ? process.env.UFOO_AGENT_API_KEY || config.agentApiKey || "" :
108
+ (useCodingConfig ? process.env.UFOO_UCODE_API_KEY : "")
109
+ || (useCodingConfig ? config.ucodeApiKey : "")
110
+ || ""
111
+ ).trim();
112
+ let apiKey = explicitApiKey;
113
+ let apiKeySource = explicitApiKey ? "explicit" : "";
114
+ let kimiCredentialState = "";
115
+ if (!apiKey && selectedProvider === "kimi") {
116
+ const credential = readKimiAccessToken({ env: process.env });
117
+ if (credential && credential.accessToken) {
118
+ apiKey = String(credential.accessToken).trim();
119
+ apiKeySource = "kimi-credential";
120
+ kimiCredentialState = String(credential.state || "");
121
+ }
122
+ }
123
+ if (!apiKey && !custom) {
124
+ apiKey = String(
125
+ (selectedProvider === "openai" ? process.env.OPENAI_API_KEY : "")
126
+ || (selectedProvider === "anthropic" ? process.env.ANTHROPIC_API_KEY : "")
127
+ || (selectedProvider === "codex" ? process.env.OPENAI_API_KEY : "")
128
+ || (selectedProvider === "grok-build" ? (process.env.GROK_BUILD_API_KEY || process.env.XAI_API_KEY) : "")
129
+ || (selectedProvider === "xai" ? process.env.XAI_API_KEY : "")
130
+ || ""
131
+ ).trim();
132
+ if (apiKey) apiKeySource = "env";
133
+ }
134
+
135
+ // ChatGPT's Codex backend accepts Codex OAuth credentials, while standard
136
+ // OpenAI API keys belong on the public OpenAI API. Generic ucode base URL
137
+ // overrides remain authoritative for an explicitly configured gateway.
138
+ if (
139
+ selectedProvider === "codex"
140
+ && apiKey
141
+ && !String(useCodingConfig ? process.env.UFOO_UCODE_BASE_URL || "" : "").trim()
142
+ && !String(useCodingConfig ? config.ucodeBaseUrl || "" : "").trim()
143
+ ) {
144
+ baseUrl = String(process.env.OPENAI_BASE_URL || DEFAULT_OPENAI_BASE_URL).trim();
145
+ }
146
+
147
+ return {
148
+ provider: selectedProvider,
149
+ model: selectedModel,
150
+ baseUrl,
151
+ apiKey,
152
+ apiKeySource,
153
+ kimiCredentialState,
154
+ transport: resolveTransport({ provider: selectedProvider, baseUrl }),
155
+ allowUnlistedModels: custom,
156
+ };
157
+ }
158
+
159
+ function resolveCompletionUrl(baseUrl = "") {
160
+ const raw = String(baseUrl || "").trim();
161
+ if (!raw) return "";
162
+ const normalized = raw.replace(/\/+$/, "");
163
+ if (/\/chat\/completions$/i.test(normalized)) return normalized;
164
+ if (/\/v1$/i.test(normalized)) return `${normalized}/chat/completions`;
165
+ if (/\/api$/i.test(normalized)) return `${normalized}/v1/chat/completions`;
166
+ return `${normalized}/chat/completions`;
167
+ }
168
+
169
+ function resolveResponsesUrl(baseUrl = "") {
170
+ const raw = String(baseUrl || "").trim();
171
+ if (!raw) return "";
172
+ const normalized = raw.replace(/\/+$/, "");
173
+ if (/\/responses$/i.test(normalized)) return normalized;
174
+ if (/\/v1$/i.test(normalized)) return `${normalized}/responses`;
175
+ if (/\/api$/i.test(normalized)) return `${normalized}/v1/responses`;
176
+ return `${normalized}/responses`;
177
+ }
178
+
179
+ function resolveAnthropicMessagesUrl(baseUrl = "") {
180
+ const raw = String(baseUrl || "").trim() || DEFAULT_ANTHROPIC_BASE_URL;
181
+ const normalized = raw.replace(/\/+$/, "");
182
+ if (/\/messages$/i.test(normalized)) return normalized;
183
+ if (/\/v1$/i.test(normalized)) return `${normalized}/messages`;
184
+ if (/\/api$/i.test(normalized)) return `${normalized}/v1/messages`;
185
+ return `${normalized}/messages`;
186
+ }
187
+
188
+ module.exports = { normalizeProvider, normalizeKimiModel, resolveTransport, resolveRuntimeConfig, resolveCompletionUrl, resolveResponsesUrl, resolveAnthropicMessagesUrl, DEFAULT_OPENAI_BASE_URL };
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Ucode thinking-intensity levels.
5
+ *
6
+ * Used as the secondary `/model <id> <level>` menu after picking a model.
7
+ * Maps to Anthropic effort and OpenAI-compatible reasoning_effort where
8
+ * the transport supports it.
9
+ */
10
+
11
+ const THINKING_LEVELS = Object.freeze([
12
+ {
13
+ id: "off",
14
+ desc: "disable extended thinking",
15
+ reasoningEffort: "",
16
+ },
17
+ {
18
+ id: "low",
19
+ desc: "light thinking",
20
+ reasoningEffort: "low",
21
+ },
22
+ {
23
+ id: "medium",
24
+ desc: "default thinking",
25
+ reasoningEffort: "medium",
26
+ },
27
+ {
28
+ id: "high",
29
+ desc: "deeper thinking",
30
+ reasoningEffort: "high",
31
+ },
32
+ {
33
+ id: "xhigh",
34
+ desc: "extra high thinking",
35
+ reasoningEffort: "xhigh",
36
+ },
37
+ {
38
+ id: "max",
39
+ desc: "maximum thinking",
40
+ reasoningEffort: "max",
41
+ },
42
+ ]);
43
+
44
+ const DEFAULT_THINKING_LEVEL = "medium";
45
+ const THINKING_LEVEL_IDS = new Set(THINKING_LEVELS.map((item) => item.id));
46
+
47
+ function normalizeThinkingLevel(value = "") {
48
+ const raw = String(value || "").trim().toLowerCase();
49
+ if (!raw) return "";
50
+ if (raw === "none" || raw === "disable" || raw === "disabled" || raw === "0") return "off";
51
+ if (raw === "med" || raw === "default") return "medium";
52
+ if (raw === "maximum" || raw === "ultra") return "max";
53
+ if (THINKING_LEVEL_IDS.has(raw)) return raw;
54
+ return "";
55
+ }
56
+
57
+ function getThinkingLevel(id = "") {
58
+ const normalized = normalizeThinkingLevel(id) || DEFAULT_THINKING_LEVEL;
59
+ return THINKING_LEVELS.find((item) => item.id === normalized) || THINKING_LEVELS[2];
60
+ }
61
+
62
+ function suggestThinkingLevels(options = {}) {
63
+ const current = normalizeThinkingLevel(options.current || "") || DEFAULT_THINKING_LEVEL;
64
+ return THINKING_LEVELS.map((item) => ({
65
+ id: item.id,
66
+ desc: item.id === current ? `${item.desc} · current` : item.desc,
67
+ }));
68
+ }
69
+
70
+ function resolveThinkingFromEnvAndConfig({
71
+ env = process.env,
72
+ configLevel = "",
73
+ } = {}) {
74
+ const fromEnv = normalizeThinkingLevel(env && env.UFOO_UCODE_THINKING);
75
+ const fromConfig = normalizeThinkingLevel(configLevel);
76
+ const level = fromEnv || fromConfig || DEFAULT_THINKING_LEVEL;
77
+ const spec = getThinkingLevel(level);
78
+ return {
79
+ level: spec.id,
80
+ reasoningEffort: spec.reasoningEffort,
81
+ source: fromEnv ? "env" : (fromConfig ? "config" : "default"),
82
+ };
83
+ }
84
+
85
+ function applyThinkingLevelToEnv(level = "", env = process.env) {
86
+ const normalized = normalizeThinkingLevel(level);
87
+ if (!normalized) return "";
88
+ const spec = getThinkingLevel(normalized);
89
+ try {
90
+ env.UFOO_UCODE_THINKING = spec.id;
91
+ } catch {
92
+ // ignore env write failures
93
+ }
94
+ return spec.id;
95
+ }
96
+
97
+ module.exports = {
98
+ THINKING_LEVELS,
99
+ THINKING_LEVEL_IDS,
100
+ DEFAULT_THINKING_LEVEL,
101
+ normalizeThinkingLevel,
102
+ getThinkingLevel,
103
+ suggestThinkingLevels,
104
+ resolveThinkingFromEnvAndConfig,
105
+ applyThinkingLevelToEnv,
106
+ };
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+
3
+ const { assertTransport } = require("./transportContract");
4
+ const {
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
+ features: Object.freeze(["tool_calls", "streaming", "vision", "deferred_tools"]),
31
+ resolveUrl,
32
+ prepareMessages({ messages, prompt }) {
33
+ messages.push({
34
+ role: "user",
35
+ content: String(prompt || ""),
36
+ });
37
+ },
38
+ runTurn,
39
+ getToolCalls(turnResult) {
40
+ return Array.isArray(turnResult.toolCalls) ? turnResult.toolCalls : [];
41
+ },
42
+ appendFinalAssistantMessage({ messages, turnResult }) {
43
+ const assistantContent = Array.isArray(turnResult.assistantContent)
44
+ ? turnResult.assistantContent
45
+ : [];
46
+ if (assistantContent.length > 0) {
47
+ messages.push({
48
+ role: "assistant",
49
+ content: assistantContent,
50
+ });
51
+ } else if (String(turnResult.text || "").trim()) {
52
+ messages.push({
53
+ role: "assistant",
54
+ content: [
55
+ {
56
+ type: "text",
57
+ text: String(turnResult.text || ""),
58
+ },
59
+ ],
60
+ });
61
+ }
62
+ },
63
+ prepareToolCalls({ messages, turnResult, toolCalls }) {
64
+ const assistantContent = Array.isArray(turnResult.assistantContent)
65
+ ? turnResult.assistantContent
66
+ : [];
67
+
68
+ messages.push({
69
+ role: "assistant",
70
+ content: assistantContent,
71
+ });
72
+
73
+ return toolCalls.map((call) => ({
74
+ name: call.name,
75
+ args: call.args,
76
+ source: call,
77
+ }));
78
+ },
79
+ appendToolResult({ collected, call, toolResult }) {
80
+ const vision = extractVisionPayload(toolResult);
81
+ const isError = Boolean(!toolResult || toolResult.ok === false);
82
+ if (vision) {
83
+ const textPayload = stripVisionBase64(toolResult);
84
+ collected.push({
85
+ type: "tool_result",
86
+ tool_use_id: String(call.source.id || ""),
87
+ content: [
88
+ {
89
+ type: "text",
90
+ text: clipText(
91
+ `${visionSummaryText(vision, toolResult)}\n${toJsonString(textPayload)}`,
92
+ 12000,
93
+ ),
94
+ },
95
+ toAnthropicImageBlock(vision),
96
+ ],
97
+ is_error: isError,
98
+ });
99
+ return;
100
+ }
101
+ collected.push({
102
+ type: "tool_result",
103
+ tool_use_id: String(call.source.id || ""),
104
+ content: clipText(toJsonString(toolResult), 12000),
105
+ is_error: isError,
106
+ });
107
+ },
108
+ flushToolResults({ messages, collected }) {
109
+ messages.push({
110
+ role: "user",
111
+ content: collected,
112
+ });
113
+ },
114
+ };
115
+
116
+ return assertTransport(transport, "anthropic-messages");
117
+ }
118
+
119
+ module.exports = {
120
+ createAnthropicMessagesTransport,
121
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ module.exports = {
4
+ ...require("./transportContract"),
5
+ ...require("./openaiChatTransport"),
6
+ ...require("./openaiResponsesTransport"),
7
+ ...require("./anthropicMessagesTransport"),
8
+ ...require("./responsesProtocol"),
9
+ ...require("./visionBlocks"),
10
+ };
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ const { randomUUID } = require("crypto");
4
+ const { assertTransport } = require("./transportContract");
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
+ features: Object.freeze(["tool_calls", "streaming", "vision", "deferred_tools"]),
36
+ resolveUrl,
37
+ prepareMessages({ messages, systemPrompt, prompt }) {
38
+ const systemText = String(systemPrompt || "").trim();
39
+ const hasSystem = messages.some((entry) => String(entry.role || "").trim() === "system");
40
+ if (systemText && !hasSystem) {
41
+ messages.unshift({ role: "system", content: systemText });
42
+ }
43
+ messages.push({ role: "user", content: String(prompt || "") });
44
+ },
45
+ runTurn,
46
+ getToolCalls(turnResult) {
47
+ return Array.isArray(turnResult.toolCalls)
48
+ ? turnResult.toolCalls.filter((call) => call && call.function && typeof call.function === "object")
49
+ : [];
50
+ },
51
+ appendFinalAssistantMessage({ messages, turnResult }) {
52
+ const text = String(turnResult.text || "").trim();
53
+ if (text) {
54
+ messages.push({
55
+ role: "assistant",
56
+ content: text,
57
+ });
58
+ }
59
+ },
60
+ prepareToolCalls({ messages, toolCalls }) {
61
+ const assistantToolCalls = [];
62
+ for (const call of toolCalls) {
63
+ const callId = String(call.id || `call_${randomUUID()}`);
64
+ const name = normalizeToolName(call.function.name || "");
65
+ const args = normalizeToolCallArgs(call.function.arguments || "");
66
+
67
+ assistantToolCalls.push({
68
+ id: callId,
69
+ type: "function",
70
+ function: {
71
+ name: name || String(call.function.name || ""),
72
+ arguments: toJsonString(args),
73
+ },
74
+ });
75
+ }
76
+
77
+ if (assistantToolCalls.length === 0) return null;
78
+
79
+ messages.push({
80
+ role: "assistant",
81
+ content: null,
82
+ tool_calls: assistantToolCalls,
83
+ });
84
+
85
+ return assistantToolCalls.map((toolCall) => ({
86
+ name: toolCall.function.name,
87
+ args: normalizeToolCallArgs(toolCall.function.arguments),
88
+ source: toolCall,
89
+ }));
90
+ },
91
+ appendToolResult({ messages, call, toolResult }) {
92
+ const vision = extractVisionPayload(toolResult);
93
+ const payload = vision ? stripVisionBase64(toolResult) : toolResult;
94
+ messages.push({
95
+ role: "tool",
96
+ tool_call_id: call.source.id,
97
+ content: clipText(toJsonString(payload), 12000),
98
+ });
99
+ if (vision) {
100
+ const imagePart = toOpenAiImagePart(vision);
101
+ messages.push({
102
+ role: "user",
103
+ content: [
104
+ { type: "text", text: visionSummaryText(vision, toolResult) },
105
+ imagePart,
106
+ ],
107
+ });
108
+ }
109
+ },
110
+ };
111
+
112
+ return assertTransport(transport, "openai-chat");
113
+ }
114
+
115
+ module.exports = {
116
+ createOpenAiChatTransport,
117
+ };
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ const { randomUUID } = require("crypto");
4
+ const { assertTransport } = require("./transportContract");
5
+ const {
6
+ extractVisionPayload,
7
+ stripVisionBase64,
8
+ visionSummaryText,
9
+ toOpenAiImagePart,
10
+ } = require("./visionBlocks");
11
+ const { attachResponsesReasoningItems } = require("./responsesProtocol");
12
+
13
+ /**
14
+ * OpenAI Responses transport adapter.
15
+ *
16
+ * The loop keeps the public, provider-neutral message projection in the same
17
+ * shape as the Chat transport. The Responses wire conversion is owned by the
18
+ * request builder, so response-only metadata never gets persisted into the
19
+ * Session Journal.
20
+ */
21
+ function createOpenAiResponsesTransport(deps = {}) {
22
+ const {
23
+ resolveUrl,
24
+ runTurn,
25
+ normalizeToolName,
26
+ normalizeToolCallArgs,
27
+ toJsonString,
28
+ clipText,
29
+ } = deps;
30
+
31
+ const transport = {
32
+ name: "openai-responses",
33
+ features: Object.freeze(["tool_calls", "streaming", "vision", "deferred_tools"]),
34
+ resolveUrl,
35
+ prepareMessages({ messages, prompt }) {
36
+ messages.push({
37
+ role: "user",
38
+ content: String(prompt || ""),
39
+ });
40
+ },
41
+ runTurn,
42
+ getToolCalls(turnResult) {
43
+ return Array.isArray(turnResult.toolCalls)
44
+ ? turnResult.toolCalls.filter((call) => call && call.function && typeof call.function === "object")
45
+ : [];
46
+ },
47
+ appendFinalAssistantMessage({ messages, turnResult }) {
48
+ const text = String(turnResult.text || "").trim();
49
+ if (text) {
50
+ messages.push({
51
+ role: "assistant",
52
+ content: text,
53
+ });
54
+ }
55
+ },
56
+ prepareToolCalls({ messages, turnResult, toolCalls }) {
57
+ const assistantToolCalls = [];
58
+ for (const call of toolCalls) {
59
+ const source = call && call.function && typeof call.function === "object"
60
+ ? call.function
61
+ : {};
62
+ const callId = String(call.id || `call_${randomUUID()}`);
63
+ const name = normalizeToolName(source.name || "");
64
+ const args = normalizeToolCallArgs(source.arguments || "");
65
+ assistantToolCalls.push({
66
+ id: callId,
67
+ type: "function",
68
+ function: {
69
+ name: name || String(source.name || ""),
70
+ arguments: toJsonString(args),
71
+ },
72
+ });
73
+ }
74
+
75
+ if (assistantToolCalls.length === 0) return null;
76
+ const assistantMessage = {
77
+ role: "assistant",
78
+ content: null,
79
+ tool_calls: assistantToolCalls,
80
+ };
81
+ attachResponsesReasoningItems(
82
+ assistantMessage,
83
+ turnResult && Array.isArray(turnResult.outputItems)
84
+ ? { output: turnResult.outputItems }
85
+ : (turnResult && turnResult.response),
86
+ );
87
+ messages.push(assistantMessage);
88
+
89
+ return assistantToolCalls.map((toolCall) => ({
90
+ name: toolCall.function.name,
91
+ args: normalizeToolCallArgs(toolCall.function.arguments),
92
+ source: toolCall,
93
+ }));
94
+ },
95
+ appendToolResult({ messages, call, toolResult }) {
96
+ const vision = extractVisionPayload(toolResult);
97
+ const payload = vision ? stripVisionBase64(toolResult) : toolResult;
98
+ messages.push({
99
+ role: "tool",
100
+ tool_call_id: call.source.id,
101
+ content: clipText(toJsonString(payload), 12000),
102
+ });
103
+ if (vision) {
104
+ messages.push({
105
+ role: "user",
106
+ content: [
107
+ { type: "text", text: visionSummaryText(vision, toolResult) },
108
+ toOpenAiImagePart(vision),
109
+ ],
110
+ });
111
+ }
112
+ },
113
+ };
114
+
115
+ return assertTransport(transport, "openai-responses");
116
+ }
117
+
118
+ module.exports = {
119
+ createOpenAiResponsesTransport,
120
+ };