@downcity/agent 1.1.111 → 1.1.118

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 (154) hide show
  1. package/bin/agent/local/Agent.d.ts +22 -0
  2. package/bin/agent/local/Agent.d.ts.map +1 -1
  3. package/bin/agent/local/Agent.js +31 -0
  4. package/bin/agent/local/Agent.js.map +1 -1
  5. package/bin/agent/local/services/AgentAssemblyService.d.ts +4 -0
  6. package/bin/agent/local/services/AgentAssemblyService.d.ts.map +1 -1
  7. package/bin/agent/local/services/AgentAssemblyService.js +18 -2
  8. package/bin/agent/local/services/AgentAssemblyService.js.map +1 -1
  9. package/bin/agent/local/services/AgentLifecycleService.d.ts +6 -0
  10. package/bin/agent/local/services/AgentLifecycleService.d.ts.map +1 -1
  11. package/bin/agent/local/services/AgentLifecycleService.js +4 -0
  12. package/bin/agent/local/services/AgentLifecycleService.js.map +1 -1
  13. package/bin/agent/remote/RemoteAgent.d.ts +26 -1
  14. package/bin/agent/remote/RemoteAgent.d.ts.map +1 -1
  15. package/bin/agent/remote/RemoteAgent.js +40 -0
  16. package/bin/agent/remote/RemoteAgent.js.map +1 -1
  17. package/bin/agent/remote/RemoteTransport.d.ts +14 -1
  18. package/bin/agent/remote/RemoteTransport.d.ts.map +1 -1
  19. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts +11 -1
  20. package/bin/agent/remote/transports/HttpRemoteAgentTransport.d.ts.map +1 -1
  21. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js +54 -0
  22. package/bin/agent/remote/transports/HttpRemoteAgentTransport.js.map +1 -1
  23. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts +10 -1
  24. package/bin/agent/remote/transports/RpcRemoteAgentTransport.d.ts.map +1 -1
  25. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js +16 -0
  26. package/bin/agent/remote/transports/RpcRemoteAgentTransport.js.map +1 -1
  27. package/bin/executor/Executor.d.ts.map +1 -1
  28. package/bin/executor/Executor.js +7 -1
  29. package/bin/executor/Executor.js.map +1 -1
  30. package/bin/executor/composer/system/default/assets/core.prompt.d.ts +1 -1
  31. package/bin/executor/composer/system/default/assets/core.prompt.d.ts.map +1 -1
  32. package/bin/executor/composer/system/default/assets/core.prompt.js +1 -1
  33. package/bin/executor/composer/system/default/assets/core.prompt.js.map +1 -1
  34. package/bin/index.d.ts +1 -2
  35. package/bin/index.d.ts.map +1 -1
  36. package/bin/index.js +0 -1
  37. package/bin/index.js.map +1 -1
  38. package/bin/rpc/Client.d.ts +13 -0
  39. package/bin/rpc/Client.d.ts.map +1 -1
  40. package/bin/rpc/Client.js +31 -0
  41. package/bin/rpc/Client.js.map +1 -1
  42. package/bin/rpc/server/InternalHandlers.d.ts.map +1 -1
  43. package/bin/rpc/server/InternalHandlers.js +30 -0
  44. package/bin/rpc/server/InternalHandlers.js.map +1 -1
  45. package/bin/rpc/server/ServerTypes.d.ts +5 -0
  46. package/bin/rpc/server/ServerTypes.d.ts.map +1 -1
  47. package/bin/types/agent/AgentOptions.d.ts +9 -0
  48. package/bin/types/agent/AgentOptions.d.ts.map +1 -1
  49. package/bin/types/agent/AgentTypes.d.ts +1 -0
  50. package/bin/types/agent/AgentTypes.d.ts.map +1 -1
  51. package/bin/types/agent/RemoteAgentPluginAction.d.ts +46 -0
  52. package/bin/types/agent/RemoteAgentPluginAction.d.ts.map +1 -0
  53. package/bin/types/agent/RemoteAgentPluginAction.js +9 -0
  54. package/bin/types/agent/RemoteAgentPluginAction.js.map +1 -0
  55. package/bin/types/config/DowncityConfig.d.ts +2 -2
  56. package/bin/types/config/DowncityConfig.d.ts.map +1 -1
  57. package/bin/types/rpc/RpcProtocol.d.ts +25 -0
  58. package/bin/types/rpc/RpcProtocol.d.ts.map +1 -1
  59. package/bin/types/sdk/AgentSessionEvent.d.ts +24 -4
  60. package/bin/types/sdk/AgentSessionEvent.d.ts.map +1 -1
  61. package/package.json +4 -3
  62. package/scripts/linux-bubblewrap-sandbox.test.mjs +1 -1
  63. package/scripts/shell-sandbox-preflight.test.mjs +1 -1
  64. package/src/agent/local/Agent.ts +38 -0
  65. package/src/agent/local/services/AgentAssemblyService.ts +23 -2
  66. package/src/agent/local/services/AgentLifecycleService.ts +11 -0
  67. package/src/agent/remote/RemoteAgent.ts +52 -0
  68. package/src/agent/remote/RemoteTransport.ts +16 -0
  69. package/src/agent/remote/transports/HttpRemoteAgentTransport.ts +83 -0
  70. package/src/agent/remote/transports/RpcRemoteAgentTransport.ts +28 -0
  71. package/src/executor/Executor.ts +12 -2
  72. package/src/executor/composer/system/default/assets/core.prompt.ts +1 -1
  73. package/src/executor/composer/system/default/assets/core.prompt.ts.txt +1 -0
  74. package/src/index.ts +2 -1
  75. package/src/rpc/Client.ts +38 -0
  76. package/src/rpc/server/InternalHandlers.ts +31 -0
  77. package/src/rpc/server/ServerTypes.ts +5 -0
  78. package/src/types/agent/AgentOptions.ts +10 -0
  79. package/src/types/agent/AgentTypes.ts +4 -0
  80. package/src/types/agent/RemoteAgentPluginAction.ts +52 -0
  81. package/src/types/config/DowncityConfig.ts +2 -2
  82. package/src/types/rpc/RpcProtocol.ts +28 -0
  83. package/src/types/sdk/AgentSessionEvent.ts +28 -4
  84. package/tsconfig.tsbuildinfo +1 -1
  85. package/bin/executor/tools/shell/ShellToolBridge.d.ts +0 -79
  86. package/bin/executor/tools/shell/ShellToolBridge.d.ts.map +0 -1
  87. package/bin/executor/tools/shell/ShellToolBridge.js +0 -320
  88. package/bin/executor/tools/shell/ShellToolBridge.js.map +0 -1
  89. package/bin/executor/tools/shell/ShellToolDefinition.d.ts +0 -114
  90. package/bin/executor/tools/shell/ShellToolDefinition.d.ts.map +0 -1
  91. package/bin/executor/tools/shell/ShellToolDefinition.js +0 -367
  92. package/bin/executor/tools/shell/ShellToolDefinition.js.map +0 -1
  93. package/bin/executor/tools/shell/ShellToolFormatting.d.ts +0 -17
  94. package/bin/executor/tools/shell/ShellToolFormatting.d.ts.map +0 -1
  95. package/bin/executor/tools/shell/ShellToolFormatting.js +0 -36
  96. package/bin/executor/tools/shell/ShellToolFormatting.js.map +0 -1
  97. package/bin/executor/tools/shell/ShellToolSchemas.d.ts +0 -60
  98. package/bin/executor/tools/shell/ShellToolSchemas.d.ts.map +0 -1
  99. package/bin/executor/tools/shell/ShellToolSchemas.js +0 -126
  100. package/bin/executor/tools/shell/ShellToolSchemas.js.map +0 -1
  101. package/bin/executor/tools/shell/types/Shell.d.ts +0 -109
  102. package/bin/executor/tools/shell/types/Shell.d.ts.map +0 -1
  103. package/bin/executor/tools/shell/types/Shell.js +0 -9
  104. package/bin/executor/tools/shell/types/Shell.js.map +0 -1
  105. package/bin/executor/tools/shell/types/ShellPlugin.d.ts +0 -252
  106. package/bin/executor/tools/shell/types/ShellPlugin.d.ts.map +0 -1
  107. package/bin/executor/tools/shell/types/ShellPlugin.js +0 -9
  108. package/bin/executor/tools/shell/types/ShellPlugin.js.map +0 -1
  109. package/bin/sandbox/LinuxBubblewrapSandbox.d.ts +0 -19
  110. package/bin/sandbox/LinuxBubblewrapSandbox.d.ts.map +0 -1
  111. package/bin/sandbox/LinuxBubblewrapSandbox.js +0 -186
  112. package/bin/sandbox/LinuxBubblewrapSandbox.js.map +0 -1
  113. package/bin/sandbox/MacOsSeatbeltSandbox.d.ts +0 -16
  114. package/bin/sandbox/MacOsSeatbeltSandbox.d.ts.map +0 -1
  115. package/bin/sandbox/MacOsSeatbeltSandbox.js +0 -154
  116. package/bin/sandbox/MacOsSeatbeltSandbox.js.map +0 -1
  117. package/bin/sandbox/SandboxConfigResolver.d.ts +0 -37
  118. package/bin/sandbox/SandboxConfigResolver.d.ts.map +0 -1
  119. package/bin/sandbox/SandboxConfigResolver.js +0 -130
  120. package/bin/sandbox/SandboxConfigResolver.js.map +0 -1
  121. package/bin/sandbox/SandboxPreflight.d.ts +0 -73
  122. package/bin/sandbox/SandboxPreflight.d.ts.map +0 -1
  123. package/bin/sandbox/SandboxPreflight.js +0 -122
  124. package/bin/sandbox/SandboxPreflight.js.map +0 -1
  125. package/bin/sandbox/SandboxRunner.d.ts +0 -61
  126. package/bin/sandbox/SandboxRunner.d.ts.map +0 -1
  127. package/bin/sandbox/SandboxRunner.js +0 -107
  128. package/bin/sandbox/SandboxRunner.js.map +0 -1
  129. package/bin/sandbox/UnrestrictedSandbox.d.ts +0 -16
  130. package/bin/sandbox/UnrestrictedSandbox.d.ts.map +0 -1
  131. package/bin/sandbox/UnrestrictedSandbox.js +0 -39
  132. package/bin/sandbox/UnrestrictedSandbox.js.map +0 -1
  133. package/bin/sandbox/types/Sandbox.d.ts +0 -130
  134. package/bin/sandbox/types/Sandbox.d.ts.map +0 -1
  135. package/bin/sandbox/types/Sandbox.js +0 -10
  136. package/bin/sandbox/types/Sandbox.js.map +0 -1
  137. package/bin/sandbox/types/SandboxRuntime.d.ts +0 -370
  138. package/bin/sandbox/types/SandboxRuntime.d.ts.map +0 -1
  139. package/bin/sandbox/types/SandboxRuntime.js +0 -10
  140. package/bin/sandbox/types/SandboxRuntime.js.map +0 -1
  141. package/src/executor/tools/shell/ShellToolBridge.ts +0 -412
  142. package/src/executor/tools/shell/ShellToolDefinition.ts +0 -521
  143. package/src/executor/tools/shell/ShellToolFormatting.ts +0 -34
  144. package/src/executor/tools/shell/ShellToolSchemas.ts +0 -135
  145. package/src/executor/tools/shell/types/Shell.ts +0 -116
  146. package/src/executor/tools/shell/types/ShellPlugin.ts +0 -271
  147. package/src/sandbox/LinuxBubblewrapSandbox.ts +0 -222
  148. package/src/sandbox/MacOsSeatbeltSandbox.ts +0 -191
  149. package/src/sandbox/SandboxConfigResolver.ts +0 -152
  150. package/src/sandbox/SandboxPreflight.ts +0 -205
  151. package/src/sandbox/SandboxRunner.ts +0 -151
  152. package/src/sandbox/UnrestrictedSandbox.ts +0 -53
  153. package/src/sandbox/types/Sandbox.ts +0 -144
  154. package/src/sandbox/types/SandboxRuntime.ts +0 -440
@@ -1 +0,0 @@
1
- {"version":3,"file":"SandboxR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Z;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,KAAK,EAAE,OAAO,CAAC;IAEf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC,UAAU,CAAC;IAE3B;;OAEG;IACH,MAAM,EAAE,qBAAqB,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,KAAK,EAAE,8BAA8B,CAAC;IAEtC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC;IAEtC;;OAEG;IACH,OAAO,EAAE,cAAc,CAAC;IAExB;;OAEG;IACH,WAAW,EAAE,kBAAkB,CAAC;IAEhC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB"}
@@ -1,10 +0,0 @@
1
- /**
2
- * SandboxRuntime 类型定义。
3
- *
4
- * 关键点(中文)
5
- * - 这里放的是 agent 执行层内部使用的最小 sandbox 运行时类型。
6
- * - 当前只围绕 agent 级 sandbox spawn 设计,不引入复杂 provider / policy / binding 对象。
7
- * - 目标是让 shell、task script 等本地执行入口都能复用同一个 agent sandbox 边界。
8
- */
9
- export {};
10
- //# sourceMappingURL=SandboxRuntime.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SandboxRuntime.js","sourceRoot":"","sources":["../../../src/sandbox/types/SandboxRuntime.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
@@ -1,412 +0,0 @@
1
- /**
2
- * Shell tool 运行时桥接与响应整理。
3
- *
4
- * 关键点(中文)
5
- * - tool 层只负责协议适配,不直接持有 shell plugin runtime 具体实现。
6
- * - runtime 注入、桥接协议解析、输出扁平化都收敛在这里,避免 `Tool.ts` 继续膨胀。
7
- */
8
-
9
- import { generateId } from "@/utils/Id.js";
10
- import type { JsonObject, JsonValue } from "@/types/common/Json.js";
11
- import type {
12
- InvokePluginPort,
13
- InvokePluginResult,
14
- } from "@/types/runtime/agent/AgentContext.js";
15
- import type { ShellActionResponse } from "@/executor/tools/shell/types/ShellPlugin.js";
16
- import {
17
- enqueueDeferredPersistedUserMessage,
18
- enqueueInjectedUserMessage,
19
- getSessionRunContext,
20
- } from "@executor/SessionRunScope.js";
21
-
22
- /**
23
- * 单个 shell 命令桥接状态。
24
- */
25
- interface CommandBridgeState {
26
- /**
27
- * 当前 shell 会话累计输出,用于在命令结束后解析桥接 JSON。
28
- */
29
- bufferedOutput: string;
30
- }
31
-
32
- let shellToolRuntime: InvokePluginPort | null = null;
33
-
34
- /**
35
- * 通用命令桥接状态表(按 shell_id)。
36
- */
37
- const commandBridgeStates = new Map<string, CommandBridgeState>();
38
-
39
- export function setShellToolRuntime(next: InvokePluginPort): void {
40
- shellToolRuntime = next;
41
- }
42
-
43
- function requireShellToolRuntime(): InvokePluginPort {
44
- if (shellToolRuntime) return shellToolRuntime;
45
- throw new Error(
46
- "Shell tool runtime is not initialized. Ensure runtime startup has completed before using shell tools.",
47
- );
48
- }
49
-
50
- function toJsonObject(value: unknown): JsonObject | null {
51
- if (!value || typeof value !== "object" || Array.isArray(value)) return null;
52
- return value as JsonObject;
53
- }
54
-
55
- function tryParseJsonObject(raw: string): JsonObject | null {
56
- const text = String(raw || "").trim();
57
- if (!text) return null;
58
- try {
59
- const parsed = JSON.parse(text) as unknown;
60
- return toJsonObject(parsed);
61
- } catch {
62
- const firstBrace = text.indexOf("{");
63
- const lastBrace = text.lastIndexOf("}");
64
- if (firstBrace < 0 || lastBrace <= firstBrace) return null;
65
- const candidate = text.slice(firstBrace, lastBrace + 1);
66
- try {
67
- const parsed = JSON.parse(candidate) as unknown;
68
- return toJsonObject(parsed);
69
- } catch {
70
- return null;
71
- }
72
- }
73
- }
74
-
75
- function shouldEnableCommandBridge(command: string): boolean {
76
- const raw = String(command || "").trim();
77
- if (!raw) return false;
78
- return /(?:^|\s)(?:city|town)(?:\s|$)/i.test(raw);
79
- }
80
-
81
- async function injectUserTextMessage(params: {
82
- /**
83
- * 要注入的用户文本。
84
- */
85
- text: string;
86
- /**
87
- * 可选的注入说明。
88
- */
89
- note?: string;
90
- }): Promise<boolean> {
91
- const run_context = getSessionRunContext();
92
- const sessionId = String(run_context?.sessionId || "").trim();
93
- const text = String(params.text || "").trim();
94
- if (!sessionId || !text) return false;
95
- const note = String(params.note || "runtime_injected_user_message");
96
-
97
- enqueueInjectedUserMessage({
98
- id: `u:${sessionId}:${generateId()}`,
99
- role: "user",
100
- metadata: {
101
- v: 1,
102
- ts: Date.now(),
103
- sessionId,
104
- source: "ingress",
105
- kind: "normal",
106
- extra: {
107
- note,
108
- },
109
- },
110
- parts: [{ type: "text", text }],
111
- });
112
-
113
- enqueueDeferredPersistedUserMessage(sessionId, {
114
- id: `u:${sessionId}:${generateId()}`,
115
- role: "user",
116
- metadata: {
117
- v: 1,
118
- ts: Date.now(),
119
- sessionId,
120
- source: "ingress",
121
- kind: "normal",
122
- extra: {
123
- note,
124
- injectedBy: "shell_runtime_bridge",
125
- },
126
- },
127
- parts: [{ type: "text", text }],
128
- });
129
- return true;
130
- }
131
-
132
- function extractRuntimeBridgeBlock(payload: JsonObject): {
133
- injectUserMessages: Array<{ text: string; note?: string }>;
134
- suppressToolOutput: boolean;
135
- toolOutputMessage: string;
136
- } | null {
137
- const data = toJsonObject(payload.data);
138
- const bridge = toJsonObject((data?.__ship ?? payload.__ship) as unknown);
139
- if (!bridge) return null;
140
-
141
- const rawMessages = Array.isArray(bridge.injectUserMessages)
142
- ? bridge.injectUserMessages
143
- : [];
144
- const injectUserMessages: Array<{ text: string; note?: string }> = [];
145
- for (const item of rawMessages) {
146
- const obj = toJsonObject(item);
147
- if (!obj) continue;
148
- const text = typeof obj.text === "string" ? obj.text.trim() : "";
149
- if (!text) continue;
150
- const note =
151
- typeof obj.note === "string" && obj.note.trim() ? obj.note.trim() : undefined;
152
- injectUserMessages.push({ text, ...(note ? { note } : {}) });
153
- }
154
-
155
- const suppressToolOutput = bridge.suppressToolOutput !== false;
156
- const toolOutputMessage =
157
- typeof bridge.toolOutputMessage === "string" && bridge.toolOutputMessage.trim()
158
- ? bridge.toolOutputMessage.trim()
159
- : "runtime bridge applied";
160
-
161
- return {
162
- injectUserMessages,
163
- suppressToolOutput,
164
- toolOutputMessage,
165
- };
166
- }
167
-
168
- function sanitizeBridgeMarkers(payload: JsonObject): JsonObject {
169
- const cloned = JSON.parse(JSON.stringify(payload)) as JsonObject;
170
- if ("__ship" in cloned) delete cloned.__ship;
171
- const data = toJsonObject(cloned.data);
172
- if (data && "__ship" in data) {
173
- delete data.__ship;
174
- }
175
- return cloned;
176
- }
177
-
178
- /**
179
- * 为可能触发桥接协议的命令建立状态缓存。
180
- */
181
- export function trackShellCommandBridge(command: string, shellId: string): void {
182
- if (!shouldEnableCommandBridge(command)) return;
183
- commandBridgeStates.set(shellId, { bufferedOutput: "" });
184
- }
185
-
186
- /**
187
- * 清理指定 shell 的桥接状态。
188
- */
189
- export function clearShellCommandBridge(shellId: string): void {
190
- commandBridgeStates.delete(shellId);
191
- }
192
-
193
- /**
194
- * 将 shell plugin runtime 响应整理为通用工具输出。
195
- */
196
- export function flattenShellActionResponse(params: {
197
- /**
198
- * plugin runtime 返回的 shell 响应。
199
- */
200
- response: ShellActionResponse;
201
- /**
202
- * tool 调用开始时间。
203
- */
204
- startedAt: number;
205
- }): JsonObject {
206
- const shell = params.response.shell;
207
- const chunk = params.response.chunk;
208
- const exitCode = typeof shell.exitCode === "number" ? shell.exitCode : null;
209
- const success =
210
- shell.approvalStatus !== "denied" &&
211
- shell.approvalStatus !== "expired" &&
212
- (exitCode === null || exitCode === 0);
213
- return {
214
- success,
215
- shell_id: shell.shellId,
216
- status: shell.status,
217
- cmd: shell.cmd,
218
- cwd: shell.cwd,
219
- sandboxed: shell.sandboxed === true,
220
- sandbox: shell.sandboxMode || (shell.sandboxed === false ? "unrestricted" : "safe"),
221
- approval_status: shell.approvalStatus || null,
222
- approval_id: shell.approvalId || null,
223
- approval_reason: shell.approvalReason || null,
224
- stdin_writable: shell.stdinWritable !== false,
225
- sandbox_backend: shell.sandboxBackend || null,
226
- sandbox_network_mode: shell.sandboxNetworkMode || null,
227
- sandbox_dir: shell.sandboxDir || null,
228
- sandbox_home_dir: shell.sandboxHomeDir || null,
229
- sandbox_tmp_dir: shell.sandboxTmpDir || null,
230
- sandbox_cache_dir: shell.sandboxCacheDir || null,
231
- pid: typeof shell.pid === "number" ? shell.pid : null,
232
- version: shell.version,
233
- started_at: shell.startedAt,
234
- updated_at: shell.updatedAt,
235
- ended_at: typeof shell.endedAt === "number" ? shell.endedAt : null,
236
- exit_code: exitCode,
237
- output: chunk?.output || "",
238
- start_cursor: typeof chunk?.startCursor === "number" ? chunk.startCursor : null,
239
- end_cursor: typeof chunk?.endCursor === "number" ? chunk.endCursor : null,
240
- original_chars: chunk?.originalChars ?? 0,
241
- original_lines: chunk?.originalLines ?? 0,
242
- has_more_output: chunk?.hasMoreOutput === true,
243
- last_output_preview: shell.lastOutputPreview || "",
244
- output_chars: shell.outputChars,
245
- dropped_chars: shell.droppedChars,
246
- auto_notify_on_exit: shell.autoNotifyOnExit,
247
- notification_sent: shell.notificationSent,
248
- owner_context_id: shell.ownerContextId || null,
249
- external_refs: shell.externalRefs,
250
- wall_time_seconds: Math.max(0, (Date.now() - params.startedAt) / 1000),
251
- ...(params.response.note ? { note: params.response.note } : {}),
252
- };
253
- }
254
-
255
- /**
256
- * 将一次性执行响应整理为简化输出。
257
- */
258
- export function flattenShellExecResponse(params: {
259
- /**
260
- * plugin runtime 返回的 shell 响应。
261
- */
262
- response: ShellActionResponse;
263
- /**
264
- * tool 调用开始时间。
265
- */
266
- startedAt: number;
267
- }): JsonObject {
268
- const shell = params.response.shell;
269
- const chunk = params.response.chunk;
270
- const exitCode = typeof shell.exitCode === "number" ? shell.exitCode : null;
271
- const success =
272
- shell.approvalStatus !== "denied" &&
273
- shell.approvalStatus !== "expired" &&
274
- (exitCode === null || exitCode === 0);
275
- return {
276
- success,
277
- status: shell.status,
278
- cmd: shell.cmd,
279
- cwd: shell.cwd,
280
- sandboxed: shell.sandboxed === true,
281
- sandbox: shell.sandboxMode || (shell.sandboxed === false ? "unrestricted" : "safe"),
282
- approval_status: shell.approvalStatus || null,
283
- approval_id: shell.approvalId || null,
284
- approval_reason: shell.approvalReason || null,
285
- stdin_writable: shell.stdinWritable !== false,
286
- sandbox_backend: shell.sandboxBackend || null,
287
- sandbox_network_mode: shell.sandboxNetworkMode || null,
288
- sandbox_dir: shell.sandboxDir || null,
289
- sandbox_home_dir: shell.sandboxHomeDir || null,
290
- sandbox_tmp_dir: shell.sandboxTmpDir || null,
291
- sandbox_cache_dir: shell.sandboxCacheDir || null,
292
- exit_code: exitCode,
293
- output: chunk?.output || "",
294
- original_chars: chunk?.originalChars ?? 0,
295
- original_lines: chunk?.originalLines ?? 0,
296
- external_refs: shell.externalRefs,
297
- wall_time_seconds: Math.max(0, (Date.now() - params.startedAt) / 1000),
298
- ...(params.response.note ? { note: params.response.note } : {}),
299
- };
300
- }
301
-
302
- /**
303
- * 尝试解析 shell 输出中的 runtime bridge 指令。
304
- */
305
- export async function bridgeCommandResponse(params: {
306
- /**
307
- * shell 唯一标识。
308
- */
309
- shellId: string;
310
- /**
311
- * 已经扁平化后的 tool 输出。
312
- */
313
- response: JsonObject;
314
- }): Promise<JsonObject> {
315
- const state = commandBridgeStates.get(params.shellId);
316
- if (!state) return params.response;
317
-
318
- const rawOutput =
319
- typeof params.response.output === "string" ? params.response.output : "";
320
- if (rawOutput) {
321
- state.bufferedOutput += rawOutput;
322
- }
323
-
324
- const hasMoreOutput = params.response.has_more_output === true;
325
- const exitCode =
326
- typeof params.response.exit_code === "number"
327
- ? params.response.exit_code
328
- : null;
329
- const status = String(params.response.status || "").trim();
330
- const completed =
331
- !hasMoreOutput &&
332
- exitCode !== null &&
333
- status !== "running" &&
334
- status !== "starting";
335
- if (!completed) return params.response;
336
-
337
- commandBridgeStates.delete(params.shellId);
338
- const payload = tryParseJsonObject(state.bufferedOutput);
339
- if (!payload) return params.response;
340
- const bridge = extractRuntimeBridgeBlock(payload);
341
- if (!bridge) return params.response;
342
-
343
- let injectedCount = 0;
344
- for (const item of bridge.injectUserMessages) {
345
- if (await injectUserTextMessage(item)) {
346
- injectedCount += 1;
347
- }
348
- }
349
-
350
- if (!bridge.suppressToolOutput) {
351
- const sanitized = sanitizeBridgeMarkers(payload);
352
- return {
353
- ...params.response,
354
- output: JSON.stringify(sanitized, null, 2),
355
- };
356
- }
357
-
358
- return {
359
- ...params.response,
360
- output: JSON.stringify({
361
- success: true,
362
- message: bridge.toolOutputMessage,
363
- injected_user_messages: injectedCount,
364
- }),
365
- };
366
- }
367
-
368
- /**
369
- * 调用 shell plugin runtime action。
370
- */
371
- export async function invokeShellAction<TPayload extends JsonValue = JsonValue>(params: {
372
- /**
373
- * action 名称。
374
- */
375
- action: string;
376
- /**
377
- * action 载荷。
378
- */
379
- payload: TPayload;
380
- }): Promise<ShellActionResponse> {
381
- const runtime = requireShellToolRuntime();
382
- const result = await runtime.invoke({
383
- plugin: "shell",
384
- action: params.action,
385
- payload: params.payload,
386
- });
387
- if (!result.success) {
388
- throw new Error(result.error || `shell.${params.action} failed`);
389
- }
390
- const data = toJsonObject(result.data);
391
- if (!data) {
392
- throw new Error(`shell.${params.action} returned invalid data`);
393
- }
394
- const response = data as unknown as ShellActionResponse;
395
- if (!response.shell || typeof response.shell.shellId !== "string") {
396
- throw new Error(`shell.${params.action} returned invalid shell payload`);
397
- }
398
- return response;
399
- }
400
-
401
- /**
402
- * 统一格式化 tool 错误。
403
- */
404
- export function formatToolError(
405
- prefix: string,
406
- error: unknown,
407
- ): { success: false; error: string } {
408
- return {
409
- success: false,
410
- error: `${prefix}: ${String(error)}`,
411
- };
412
- }