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
@@ -1,167 +0,0 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const { loadConfig, normalizeControllerMode } = require("../../config");
4
-
5
- const CONTROLLER_MODES = Object.freeze({
6
- LEGACY: "legacy",
7
- SHADOW: "shadow",
8
- MAIN: "main",
9
- LOOP: "loop",
10
- });
11
-
12
- const CONTROLLER_MODE_HISTORY_LIMIT = 16;
13
-
14
- const appliedControllerModes = new Map();
15
- const appliedControllerModeHistory = new Map();
16
-
17
- function readProcessControllerMode(env = process.env) {
18
- const normalized = normalizeControllerMode(env.UFOO_CONTROLLER_MODE);
19
- if (normalized !== CONTROLLER_MODES.LEGACY || String(env.UFOO_CONTROLLER_MODE || "").trim().toLowerCase() === CONTROLLER_MODES.LEGACY) {
20
- return normalized;
21
- }
22
- return CONTROLLER_MODES.MAIN;
23
- }
24
-
25
- function resolveControllerMode({
26
- projectRoot,
27
- requestedMode = "",
28
- env = process.env,
29
- config = null,
30
- } = {}) {
31
- const explicit = normalizeControllerMode(requestedMode);
32
- if (explicit !== CONTROLLER_MODES.LEGACY || String(requestedMode || "").trim().toLowerCase() === CONTROLLER_MODES.LEGACY) {
33
- return explicit;
34
- }
35
-
36
- const hasExplicitConfig = config && typeof config === "object"
37
- ? Object.prototype.hasOwnProperty.call(config, "controllerMode")
38
- : Boolean(projectRoot) && fs.existsSync(path.join(projectRoot, ".ufoo", "config.json"));
39
- const loadedConfig = hasExplicitConfig
40
- ? (config && typeof config === "object" ? config : loadConfig(projectRoot))
41
- : null;
42
- if (loadedConfig) {
43
- const projectMode = normalizeControllerMode(loadedConfig.controllerMode);
44
- if (projectMode !== CONTROLLER_MODES.LEGACY || String(loadedConfig.controllerMode || "").trim().toLowerCase() === CONTROLLER_MODES.LEGACY) {
45
- return projectMode;
46
- }
47
- return CONTROLLER_MODES.MAIN;
48
- }
49
-
50
- return readProcessControllerMode(env);
51
- }
52
-
53
- function getControllerModeStateKey(projectRoot = "") {
54
- const text = String(projectRoot || "").trim();
55
- return text || "__default__";
56
- }
57
-
58
- function pushModeHistory(key, previousMode, normalizedMode, messageId) {
59
- if (!previousMode || previousMode === normalizedMode) return;
60
- const list = appliedControllerModeHistory.get(key) || [];
61
- list.push({
62
- from_mode: previousMode,
63
- to_mode: normalizedMode,
64
- applied_from_msg_id: String(messageId || "").trim(),
65
- });
66
- while (list.length > CONTROLLER_MODE_HISTORY_LIMIT) list.shift();
67
- appliedControllerModeHistory.set(key, list);
68
- }
69
-
70
- function applyControllerModeForMessage({
71
- projectRoot,
72
- nextMode = CONTROLLER_MODES.MAIN,
73
- messageId = "",
74
- } = {}) {
75
- const key = getControllerModeStateKey(projectRoot);
76
- const normalizedMode = normalizeControllerMode(nextMode);
77
- const previousMode = appliedControllerModes.get(key) || null;
78
- appliedControllerModes.set(key, normalizedMode);
79
-
80
- if (!previousMode || previousMode === normalizedMode) {
81
- return {
82
- mode: normalizedMode,
83
- transition: null,
84
- };
85
- }
86
-
87
- pushModeHistory(key, previousMode, normalizedMode, messageId);
88
-
89
- return {
90
- mode: normalizedMode,
91
- transition: {
92
- from_mode: previousMode,
93
- to_mode: normalizedMode,
94
- applied_from_msg_id: String(messageId || "").trim(),
95
- },
96
- };
97
- }
98
-
99
- function rollbackControllerModeForMessage({
100
- projectRoot,
101
- messageId = "",
102
- } = {}) {
103
- const key = getControllerModeStateKey(projectRoot);
104
- const list = appliedControllerModeHistory.get(key) || [];
105
- const lastTransition = list.length > 0 ? list[list.length - 1] : null;
106
- const currentMode = appliedControllerModes.get(key) || CONTROLLER_MODES.MAIN;
107
-
108
- if (!lastTransition) {
109
- return {
110
- mode: currentMode,
111
- transition: null,
112
- rolled_back: false,
113
- };
114
- }
115
-
116
- list.pop();
117
- appliedControllerModeHistory.set(key, list);
118
-
119
- const nextMode = normalizeControllerMode(lastTransition.from_mode);
120
- appliedControllerModes.set(key, nextMode);
121
-
122
- if (nextMode === currentMode) {
123
- return {
124
- mode: nextMode,
125
- transition: null,
126
- rolled_back: true,
127
- };
128
- }
129
-
130
- return {
131
- mode: nextMode,
132
- rolled_back: true,
133
- transition: {
134
- from_mode: currentMode,
135
- to_mode: nextMode,
136
- applied_from_msg_id: String(messageId || "").trim(),
137
- rolled_back: true,
138
- restored_from_msg_id: String(lastTransition.applied_from_msg_id || "").trim(),
139
- },
140
- };
141
- }
142
-
143
- function getAppliedControllerMode(projectRoot) {
144
- const key = getControllerModeStateKey(projectRoot);
145
- return appliedControllerModes.get(key) || CONTROLLER_MODES.MAIN;
146
- }
147
-
148
- function getControllerModeHistoryForTests(projectRoot) {
149
- const key = getControllerModeStateKey(projectRoot);
150
- return (appliedControllerModeHistory.get(key) || []).slice();
151
- }
152
-
153
- function resetAppliedControllerModesForTests() {
154
- appliedControllerModes.clear();
155
- appliedControllerModeHistory.clear();
156
- }
157
-
158
- module.exports = {
159
- CONTROLLER_MODES,
160
- applyControllerModeForMessage,
161
- getAppliedControllerMode,
162
- getControllerModeHistoryForTests,
163
- readProcessControllerMode,
164
- resetAppliedControllerModesForTests,
165
- resolveControllerMode,
166
- rollbackControllerModeForMessage,
167
- };
@@ -1,211 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- loadConfig,
5
- defaultRouterProviderForAgentProvider,
6
- defaultRouterModelForProvider,
7
- sameModelProvider,
8
- } = require("../../config");
9
-
10
- const DEFAULT_EXECUTION_PATH = "main";
11
- const DEFAULT_CONFIDENCE_THRESHOLD = 0.6;
12
- const DEFAULT_TIMEOUT_MS = 5000;
13
-
14
- const ROUTING_PATTERNS = [
15
- /\bwho should\b/i,
16
- /\bwhich agent\b/i,
17
- /\broute (?:this|it|that|the request)\b/i,
18
- /\bcontinue with\b/i,
19
- /\bhand\s+off\b/i,
20
- /\bsend (?:this|it|that)\b/i,
21
- /\bassign (?:this|it|that)\b/i,
22
- /\bforward (?:this|it|that)\b/i,
23
- /\bshould (?:i|we) ask\b/i,
24
- /\bnew (?:codex|claude|agy|grok|kimi|ucode)\b/i,
25
- /交给谁/,
26
- /继续给/,
27
- /发送(?:这个|它|这条|这个任务)?给/,
28
- /让.*(?:接这个任务|接手|处理这个|来处理)/,
29
- /发给/,
30
- /转给/,
31
- /应该找谁/,
32
- /需要新开.*(?:codex|claude|agy|grok|kimi|ucode)/i,
33
- ];
34
-
35
- const NON_ROUTING_PATTERNS = [
36
- /\bfix\b/i,
37
- /\bimplement\b/i,
38
- /\bwrite\b/i,
39
- /\bedit\b/i,
40
- /\brefactor\b/i,
41
- /\btest\b/i,
42
- /\binvestigate\b/i,
43
- /\bdebug\b/i,
44
- /\bbuild\b/i,
45
- /\bcreate\b/i,
46
- /\bsearch\b/i,
47
- /\bopen\b/i,
48
- /\bread\b/i,
49
- /修复/,
50
- /修正/,
51
- /实现/,
52
- /重构/,
53
- /测试/,
54
- /排查/,
55
- /调试/,
56
- /构建/,
57
- /搜索/,
58
- /查看/,
59
- ];
60
-
61
- function normalizeExecutionPath(value = "") {
62
- const text = String(value || "").trim().toLowerCase();
63
- if (text === "router-api") return "main";
64
- if (text === "main") return "main";
65
- if (text === "shadow") return "shadow";
66
- if (text === "loop") return "loop";
67
- return DEFAULT_EXECUTION_PATH;
68
- }
69
-
70
- function toPositiveNumber(value, fallback) {
71
- const parsed = Number(value);
72
- if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
73
- return parsed;
74
- }
75
-
76
- function resolveExecutionPath({
77
- projectRoot,
78
- requestMeta = {},
79
- env = process.env,
80
- loadConfigImpl = loadConfig,
81
- } = {}) {
82
- const config = loadConfigImpl(projectRoot || process.cwd());
83
- return normalizeExecutionPath(
84
- requestMeta.agent_execution_path
85
- || env.UFOO_AGENT_EXECUTION_PATH
86
- || config.agentExecutionPath
87
- || config.controllerMode
88
- || DEFAULT_EXECUTION_PATH
89
- );
90
- }
91
-
92
- function classifyPromptIntent(prompt = "") {
93
- const text = String(prompt || "").trim();
94
- if (!text) return { kind: "general", reason: "empty_prompt" };
95
- const hasRoutingSignal = ROUTING_PATTERNS.some((pattern) => pattern.test(text));
96
- if (!hasRoutingSignal) return { kind: "general", reason: "no_routing_signal" };
97
- const hasNonRoutingSignal = NON_ROUTING_PATTERNS.some((pattern) => pattern.test(text));
98
- if (hasNonRoutingSignal) return { kind: "general", reason: "contains_execution_language" };
99
- return { kind: "routing", reason: "routing_signal" };
100
- }
101
-
102
- function shouldUseGateRouter({
103
- projectRoot,
104
- prompt,
105
- requestMeta = {},
106
- env = process.env,
107
- loadConfigImpl = loadConfig,
108
- } = {}) {
109
- const executionPath = resolveExecutionPath({
110
- projectRoot,
111
- requestMeta,
112
- env,
113
- loadConfigImpl,
114
- });
115
- const intent = classifyPromptIntent(prompt);
116
- return {
117
- executionPath,
118
- intent,
119
- enabled: executionPath === "main" || executionPath === "loop",
120
- };
121
- }
122
-
123
- function resolveGateRouterConfig({
124
- projectRoot,
125
- requestMeta = {},
126
- env = process.env,
127
- loadConfigImpl = loadConfig,
128
- } = {}) {
129
- const config = loadConfigImpl(projectRoot || process.cwd());
130
- const requestProvider = requestMeta.router_provider || env.UFOO_AGENT_ROUTER_PROVIDER;
131
- const provider = String(
132
- requestProvider
133
- || config.routerProvider
134
- || defaultRouterProviderForAgentProvider(config.agentProvider)
135
- ).trim();
136
- const configuredRouterModel = !requestProvider || sameModelProvider(config.routerProvider, provider)
137
- ? config.routerModel
138
- : "";
139
- return {
140
- provider,
141
- model: String(
142
- requestMeta.router_model
143
- || env.UFOO_AGENT_ROUTER_MODEL
144
- || configuredRouterModel
145
- || defaultRouterModelForProvider(provider)
146
- ).trim(),
147
- timeoutMs: toPositiveNumber(
148
- requestMeta.router_timeout_ms
149
- || env.UFOO_AGENT_ROUTER_TIMEOUT_MS
150
- || config.routerTimeoutMs,
151
- DEFAULT_TIMEOUT_MS
152
- ),
153
- confidenceThreshold: Math.min(
154
- 1,
155
- toPositiveNumber(
156
- requestMeta.router_confidence_threshold
157
- || env.UFOO_AGENT_ROUTER_CONFIDENCE_THRESHOLD
158
- || config.routerConfidenceThreshold,
159
- DEFAULT_CONFIDENCE_THRESHOLD
160
- )
161
- ),
162
- };
163
- }
164
-
165
- function normalizeInjectionMode(value = "") {
166
- const text = String(value || "").trim().toLowerCase();
167
- return text === "queued" ? "queued" : "immediate";
168
- }
169
-
170
- function normalizeGateRouterResult(payload = {}, fallbackMessage = "") {
171
- if (!payload || typeof payload !== "object") {
172
- return {
173
- decision: "upgrade_to_main_router",
174
- target: "unknown",
175
- confidence: 0,
176
- reason: "",
177
- message: String(fallbackMessage || ""),
178
- injection_mode: "immediate",
179
- };
180
- }
181
-
182
- const rawConfidence = Number(payload.confidence);
183
- const normalizedConfidence = Number.isFinite(rawConfidence)
184
- ? Math.max(0, Math.min(1, rawConfidence))
185
- : 0;
186
- const rawDecision = String(payload.decision || "").trim().toLowerCase();
187
- const normalizedDecision = rawDecision === "direct_dispatch"
188
- || rawDecision === "upgrade_to_main_router"
189
- ? rawDecision
190
- : (String(payload.target || "").trim() && String(payload.target || "").trim() !== "unknown"
191
- ? "direct_dispatch"
192
- : "upgrade_to_main_router");
193
-
194
- return {
195
- decision: normalizedDecision,
196
- target: String(payload.target || "unknown").trim() || "unknown",
197
- confidence: normalizedConfidence,
198
- reason: String(payload.reason || "").trim(),
199
- message: String(payload.message || fallbackMessage || "").trim(),
200
- injection_mode: normalizeInjectionMode(payload.injection_mode),
201
- };
202
- }
203
-
204
- module.exports = {
205
- classifyPromptIntent,
206
- normalizeExecutionPath,
207
- normalizeGateRouterResult,
208
- resolveExecutionPath,
209
- resolveGateRouterConfig,
210
- shouldUseGateRouter,
211
- };
@@ -1,22 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- classifyPromptIntent,
5
- normalizeExecutionPath,
6
- normalizeGateRouterResult,
7
- resolveExecutionPath,
8
- resolveGateRouterConfig,
9
- shouldUseGateRouter,
10
- } = require("./gateRouter");
11
-
12
- module.exports = {
13
- classifyPromptIntent,
14
- normalizeExecutionPath,
15
- normalizeRouteAgentResult: normalizeGateRouterResult,
16
- resolveExecutionPath,
17
- resolveRouterFastPathConfig: resolveGateRouterConfig,
18
- shouldUseRouterFastPath: shouldUseGateRouter,
19
- normalizeGateRouterResult,
20
- resolveGateRouterConfig,
21
- shouldUseGateRouter,
22
- };
@@ -1,222 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- normalizeLaunchAgentForNickname,
5
- stripRoutingPromptMetadata,
6
- } = require("./launchRouting");
7
-
8
- const LAUNCH_TARGET_PREFIX = "__ufoo_launch_";
9
-
10
- function normalizePayload(payload) {
11
- if (!payload || typeof payload !== "object") {
12
- return { reply: "", dispatch: [], ops: [] };
13
- }
14
- return {
15
- ...payload,
16
- reply: typeof payload.reply === "string" ? payload.reply : "",
17
- dispatch: Array.isArray(payload.dispatch) ? payload.dispatch : [],
18
- ops: Array.isArray(payload.ops) ? payload.ops : [],
19
- };
20
- }
21
-
22
- function isLaunchOp(op) {
23
- return Boolean(op && op.action === "launch");
24
- }
25
-
26
- function hasDispatchMessage(item) {
27
- return Boolean(item && String(item.target || "").trim() && String(item.message || "").trim());
28
- }
29
-
30
- function launchPlaceholder(index) {
31
- return `${LAUNCH_TARGET_PREFIX}${index}`;
32
- }
33
-
34
- function parseLaunchPlaceholder(target = "") {
35
- const raw = String(target || "").trim();
36
- if (!raw.startsWith(LAUNCH_TARGET_PREFIX)) return -1;
37
- const parsed = Number.parseInt(raw.slice(LAUNCH_TARGET_PREFIX.length), 10);
38
- return Number.isFinite(parsed) && parsed >= 0 ? parsed : -1;
39
- }
40
-
41
- function isLaunchDispatchTarget(target = "", op = {}) {
42
- const raw = String(target || "").trim();
43
- if (!raw || raw.includes(":")) return false;
44
- const normalized = raw.toLowerCase();
45
- const nickname = String(op.nickname || "").trim();
46
- if (nickname && raw === nickname) return true;
47
- const placeholders = new Set([
48
- "new",
49
- "new-agent",
50
- "new-worker",
51
- "worker",
52
- "agent",
53
- "launched",
54
- "launched-agent",
55
- "launched-worker",
56
- ]);
57
- if (placeholders.has(normalized)) return true;
58
- const launchAgent = normalizeLaunchAgentForNickname(op.agent || "");
59
- return Boolean(launchAgent && normalizeLaunchAgentForNickname(raw) === launchAgent);
60
- }
61
-
62
- function resolveLaunchResultTarget(op = {}, result = {}) {
63
- if (result && result.agent_id) return String(result.agent_id);
64
- if (result && Array.isArray(result.subscriber_ids) && result.subscriber_ids[0]) {
65
- return String(result.subscriber_ids[0]);
66
- }
67
- if (result && result.subscriber_id) return String(result.subscriber_id);
68
- if (result && result.nickname) return String(result.nickname);
69
- if (op && op.nickname) return String(op.nickname);
70
- return "";
71
- }
72
-
73
- function resolveDispatchLaunchIndex(dispatchItem = {}, launchOps = []) {
74
- const target = String(dispatchItem.target || "").trim();
75
- const placeholderIndex = parseLaunchPlaceholder(target);
76
- if (placeholderIndex >= 0) return placeholderIndex;
77
-
78
- for (let i = 0; i < launchOps.length; i += 1) {
79
- if (isLaunchDispatchTarget(target, launchOps[i])) return i;
80
- }
81
- return -1;
82
- }
83
-
84
- function prepareLaunchDispatchPayload(payload, prompt = "") {
85
- const normalized = normalizePayload(payload);
86
- const launchOps = [];
87
- const otherOps = [];
88
- for (const op of normalized.ops) {
89
- if (isLaunchOp(op)) launchOps.push(op);
90
- else if (op) otherOps.push(op);
91
- }
92
-
93
- if (launchOps.length === 0) {
94
- return {
95
- payload: normalized,
96
- launchOps,
97
- otherOps,
98
- dispatch: normalized.dispatch.filter(hasDispatchMessage),
99
- };
100
- }
101
-
102
- let dispatch = normalized.dispatch.filter(hasDispatchMessage).map((item) => ({ ...item }));
103
- const taskMessage = stripRoutingPromptMetadata(prompt);
104
- if (dispatch.length === 0 && !taskMessage) {
105
- return {
106
- payload: { ...normalized, dispatch: [], ops: otherOps },
107
- launchOps: [],
108
- otherOps,
109
- dispatch: [],
110
- droppedLaunches: launchOps,
111
- };
112
- }
113
-
114
- if (dispatch.length === 0 && taskMessage) {
115
- dispatch = launchOps.map((op, index) => ({
116
- target: op.nickname || launchPlaceholder(index),
117
- message: taskMessage,
118
- injection_mode: "immediate",
119
- source: "ufoo-agent",
120
- }));
121
- } else if (launchOps.length === 1) {
122
- const fallbackTarget = launchOps[0].nickname || launchPlaceholder(0);
123
- dispatch = dispatch.map((item) => (
124
- isLaunchDispatchTarget(item.target, launchOps[0])
125
- ? { ...item, target: fallbackTarget }
126
- : item
127
- ));
128
- }
129
-
130
- return {
131
- payload: { ...normalized, dispatch, ops: [...launchOps, ...otherOps] },
132
- launchOps,
133
- otherOps,
134
- dispatch,
135
- };
136
- }
137
-
138
- function bindDispatchToLaunchResults(dispatch = [], launchOps = [], launchResults = []) {
139
- const resultByIndex = launchResults.filter((item) => item && item.action === "launch");
140
- return dispatch.map((item) => {
141
- const index = resolveDispatchLaunchIndex(item, launchOps);
142
- if (index < 0) return item;
143
- const target = resolveLaunchResultTarget(launchOps[index], resultByIndex[index] || {});
144
- return target ? { ...item, target } : item;
145
- });
146
- }
147
-
148
- async function finalizeRouterPayload({
149
- projectRoot,
150
- payload,
151
- prompt = "",
152
- processManager,
153
- dispatchMessages,
154
- handleOps,
155
- markPending = () => {},
156
- finalizeLocally = true,
157
- }) {
158
- const prepared = prepareLaunchDispatchPayload(payload, prompt);
159
- if (finalizeLocally === false) {
160
- return {
161
- ok: true,
162
- payload: prepared.payload,
163
- opsResults: [],
164
- };
165
- }
166
-
167
- if (prepared.launchOps.length === 0) {
168
- for (const item of prepared.dispatch) {
169
- if (item && item.target && item.target !== "broadcast") {
170
- markPending(item.target);
171
- }
172
- }
173
- if (typeof dispatchMessages === "function") {
174
- await dispatchMessages(projectRoot, prepared.dispatch);
175
- }
176
- const opsResults = typeof handleOps === "function" && prepared.otherOps.length > 0
177
- ? await handleOps(projectRoot, prepared.otherOps, processManager)
178
- : [];
179
- return {
180
- ok: true,
181
- payload: { ...prepared.payload, dispatch: prepared.dispatch, ops: prepared.otherOps },
182
- opsResults,
183
- };
184
- }
185
-
186
- const launchResults = typeof handleOps === "function"
187
- ? await handleOps(projectRoot, prepared.launchOps, processManager)
188
- : [];
189
- const boundDispatch = bindDispatchToLaunchResults(prepared.dispatch, prepared.launchOps, launchResults);
190
-
191
- for (const item of boundDispatch) {
192
- if (item && item.target && item.target !== "broadcast") {
193
- markPending(item.target);
194
- }
195
- }
196
- if (typeof dispatchMessages === "function") {
197
- await dispatchMessages(projectRoot, boundDispatch);
198
- }
199
-
200
- const otherResults = typeof handleOps === "function" && prepared.otherOps.length > 0
201
- ? await handleOps(projectRoot, prepared.otherOps, processManager)
202
- : [];
203
-
204
- return {
205
- ok: true,
206
- payload: { ...prepared.payload, dispatch: boundDispatch },
207
- opsResults: [...launchResults, ...otherResults],
208
- };
209
- }
210
-
211
- module.exports = {
212
- bindDispatchToLaunchResults,
213
- finalizeRouterPayload,
214
- normalizePayload,
215
- prepareLaunchDispatchPayload,
216
- __private: {
217
- isLaunchDispatchTarget,
218
- launchPlaceholder,
219
- resolveLaunchResultTarget,
220
- stripRoutingPromptMetadata,
221
- },
222
- };