@downcity/shell 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/Shell.d.ts +55 -0
- package/bin/Shell.d.ts.map +1 -0
- package/bin/Shell.js +171 -0
- package/bin/Shell.js.map +1 -0
- package/bin/approval/ShellApprovalRuntime.d.ts +62 -0
- package/bin/approval/ShellApprovalRuntime.d.ts.map +1 -0
- package/bin/approval/ShellApprovalRuntime.js +214 -0
- package/bin/approval/ShellApprovalRuntime.js.map +1 -0
- package/bin/index.d.ts +27 -0
- package/bin/index.d.ts.map +1 -0
- package/bin/index.js +26 -0
- package/bin/index.js.map +1 -0
- package/bin/sandbox/LinuxBubblewrapSandbox.d.ts +19 -0
- package/bin/sandbox/LinuxBubblewrapSandbox.d.ts.map +1 -0
- package/bin/sandbox/LinuxBubblewrapSandbox.js +186 -0
- package/bin/sandbox/LinuxBubblewrapSandbox.js.map +1 -0
- package/bin/sandbox/MacOsSeatbeltSandbox.d.ts +16 -0
- package/bin/sandbox/MacOsSeatbeltSandbox.d.ts.map +1 -0
- package/bin/sandbox/MacOsSeatbeltSandbox.js +154 -0
- package/bin/sandbox/MacOsSeatbeltSandbox.js.map +1 -0
- package/bin/sandbox/SandboxConfigResolver.d.ts +37 -0
- package/bin/sandbox/SandboxConfigResolver.d.ts.map +1 -0
- package/bin/sandbox/SandboxConfigResolver.js +130 -0
- package/bin/sandbox/SandboxConfigResolver.js.map +1 -0
- package/bin/sandbox/SandboxPreflight.d.ts +73 -0
- package/bin/sandbox/SandboxPreflight.d.ts.map +1 -0
- package/bin/sandbox/SandboxPreflight.js +122 -0
- package/bin/sandbox/SandboxPreflight.js.map +1 -0
- package/bin/sandbox/SandboxRunner.d.ts +61 -0
- package/bin/sandbox/SandboxRunner.d.ts.map +1 -0
- package/bin/sandbox/SandboxRunner.js +107 -0
- package/bin/sandbox/SandboxRunner.js.map +1 -0
- package/bin/sandbox/UnrestrictedSandbox.d.ts +16 -0
- package/bin/sandbox/UnrestrictedSandbox.d.ts.map +1 -0
- package/bin/sandbox/UnrestrictedSandbox.js +39 -0
- package/bin/sandbox/UnrestrictedSandbox.js.map +1 -0
- package/bin/sandbox/types/Sandbox.d.ts +130 -0
- package/bin/sandbox/types/Sandbox.d.ts.map +1 -0
- package/bin/sandbox/types/Sandbox.js +10 -0
- package/bin/sandbox/types/Sandbox.js.map +1 -0
- package/bin/sandbox/types/SandboxRuntime.d.ts +370 -0
- package/bin/sandbox/types/SandboxRuntime.d.ts.map +1 -0
- package/bin/sandbox/types/SandboxRuntime.js +10 -0
- package/bin/sandbox/types/SandboxRuntime.js.map +1 -0
- package/bin/session/Paths.d.ts +12 -0
- package/bin/session/Paths.d.ts.map +1 -0
- package/bin/session/Paths.js +21 -0
- package/bin/session/Paths.js.map +1 -0
- package/bin/session/ShellActionResponse.d.ts +52 -0
- package/bin/session/ShellActionResponse.d.ts.map +1 -0
- package/bin/session/ShellActionResponse.js +73 -0
- package/bin/session/ShellActionResponse.js.map +1 -0
- package/bin/session/ShellActionRuntime.d.ts +15 -0
- package/bin/session/ShellActionRuntime.d.ts.map +1 -0
- package/bin/session/ShellActionRuntime.js +15 -0
- package/bin/session/ShellActionRuntime.js.map +1 -0
- package/bin/session/ShellActionRuntimeSupport.d.ts +88 -0
- package/bin/session/ShellActionRuntimeSupport.d.ts.map +1 -0
- package/bin/session/ShellActionRuntimeSupport.js +305 -0
- package/bin/session/ShellActionRuntimeSupport.js.map +1 -0
- package/bin/session/ShellProcessEvents.d.ts +22 -0
- package/bin/session/ShellProcessEvents.d.ts.map +1 -0
- package/bin/session/ShellProcessEvents.js +41 -0
- package/bin/session/ShellProcessEvents.js.map +1 -0
- package/bin/session/ShellRunScope.d.ts +43 -0
- package/bin/session/ShellRunScope.d.ts.map +1 -0
- package/bin/session/ShellRunScope.js +29 -0
- package/bin/session/ShellRunScope.js.map +1 -0
- package/bin/session/ShellRuntimeEnvironment.d.ts +21 -0
- package/bin/session/ShellRuntimeEnvironment.d.ts.map +1 -0
- package/bin/session/ShellRuntimeEnvironment.js +69 -0
- package/bin/session/ShellRuntimeEnvironment.js.map +1 -0
- package/bin/session/ShellRuntimeTypes.d.ts +160 -0
- package/bin/session/ShellRuntimeTypes.d.ts.map +1 -0
- package/bin/session/ShellRuntimeTypes.js +10 -0
- package/bin/session/ShellRuntimeTypes.js.map +1 -0
- package/bin/session/actions/ShellActionShared.d.ts +76 -0
- package/bin/session/actions/ShellActionShared.d.ts.map +1 -0
- package/bin/session/actions/ShellActionShared.js +100 -0
- package/bin/session/actions/ShellActionShared.js.map +1 -0
- package/bin/session/actions/ShellApprovalActions.d.ts +34 -0
- package/bin/session/actions/ShellApprovalActions.d.ts.map +1 -0
- package/bin/session/actions/ShellApprovalActions.js +37 -0
- package/bin/session/actions/ShellApprovalActions.js.map +1 -0
- package/bin/session/actions/ShellExecActions.d.ts +15 -0
- package/bin/session/actions/ShellExecActions.d.ts.map +1 -0
- package/bin/session/actions/ShellExecActions.js +117 -0
- package/bin/session/actions/ShellExecActions.js.map +1 -0
- package/bin/session/actions/ShellLifecycleActions.d.ts +18 -0
- package/bin/session/actions/ShellLifecycleActions.d.ts.map +1 -0
- package/bin/session/actions/ShellLifecycleActions.js +53 -0
- package/bin/session/actions/ShellLifecycleActions.js.map +1 -0
- package/bin/session/actions/ShellQueryActions.d.ts +27 -0
- package/bin/session/actions/ShellQueryActions.d.ts.map +1 -0
- package/bin/session/actions/ShellQueryActions.js +151 -0
- package/bin/session/actions/ShellQueryActions.js.map +1 -0
- package/bin/session/actions/ShellStartActions.d.ts +15 -0
- package/bin/session/actions/ShellStartActions.d.ts.map +1 -0
- package/bin/session/actions/ShellStartActions.js +167 -0
- package/bin/session/actions/ShellStartActions.js.map +1 -0
- package/bin/session/actions/ShellWriteActions.d.ts +15 -0
- package/bin/session/actions/ShellWriteActions.d.ts.map +1 -0
- package/bin/session/actions/ShellWriteActions.js +85 -0
- package/bin/session/actions/ShellWriteActions.js.map +1 -0
- package/bin/tool/ShellToolFormatting.d.ts +12 -0
- package/bin/tool/ShellToolFormatting.d.ts.map +1 -0
- package/bin/tool/ShellToolFormatting.js +31 -0
- package/bin/tool/ShellToolFormatting.js.map +1 -0
- package/bin/tool/ShellToolSchemas.d.ts +61 -0
- package/bin/tool/ShellToolSchemas.d.ts.map +1 -0
- package/bin/tool/ShellToolSchemas.js +130 -0
- package/bin/tool/ShellToolSchemas.js.map +1 -0
- package/bin/tool/ShellTools.d.ts +32 -0
- package/bin/tool/ShellTools.d.ts.map +1 -0
- package/bin/tool/ShellTools.js +296 -0
- package/bin/tool/ShellTools.js.map +1 -0
- package/bin/types/Shell.d.ts +115 -0
- package/bin/types/Shell.d.ts.map +1 -0
- package/bin/types/Shell.js +9 -0
- package/bin/types/Shell.js.map +1 -0
- package/bin/types/ShellAction.d.ts +258 -0
- package/bin/types/ShellAction.d.ts.map +1 -0
- package/bin/types/ShellAction.js +9 -0
- package/bin/types/ShellAction.js.map +1 -0
- package/bin/types/ShellHostContext.d.ts +78 -0
- package/bin/types/ShellHostContext.d.ts.map +1 -0
- package/bin/types/ShellHostContext.js +9 -0
- package/bin/types/ShellHostContext.js.map +1 -0
- package/bin/types/ShellRuntime.d.ts +128 -0
- package/bin/types/ShellRuntime.d.ts.map +1 -0
- package/bin/types/ShellRuntime.js +9 -0
- package/bin/types/ShellRuntime.js.map +1 -0
- package/bin/types/ShellRuntimeOptions.d.ts +103 -0
- package/bin/types/ShellRuntimeOptions.d.ts.map +1 -0
- package/bin/types/ShellRuntimeOptions.js +10 -0
- package/bin/types/ShellRuntimeOptions.js.map +1 -0
- package/bin/utils/Id.d.ts +11 -0
- package/bin/utils/Id.d.ts.map +1 -0
- package/bin/utils/Id.js +14 -0
- package/bin/utils/Id.js.map +1 -0
- package/package.json +81 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell runtime 生命周期 action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 负责绑定宿主上下文与关闭当前 state 下的所有 shell。
|
|
6
|
+
* - 不处理单个 shell 的 start/read/write 等业务动作。
|
|
7
|
+
*/
|
|
8
|
+
import type { ShellHostContext } from "../../types/ShellHostContext.js";
|
|
9
|
+
import type { ShellRuntimeState } from "../../session/ShellRuntimeTypes.js";
|
|
10
|
+
/**
|
|
11
|
+
* 绑定当前 shell runtime 实例的 execution runtime。
|
|
12
|
+
*/
|
|
13
|
+
export declare function bindShellRuntime(state: ShellRuntimeState, context: ShellHostContext): void;
|
|
14
|
+
/**
|
|
15
|
+
* 关闭当前实例持有的所有活动 shell。
|
|
16
|
+
*/
|
|
17
|
+
export declare function closeAllShellSessions(state: ShellRuntimeState, force?: boolean): Promise<void>;
|
|
18
|
+
//# sourceMappingURL=ShellLifecycleActions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellLifecycleActions.d.ts","sourceRoot":"","sources":["../../../src/session/actions/ShellLifecycleActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAOxE;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,GACxB,IAAI,CAEN;AAED;;GAEG;AACH,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,iBAAiB,EACxB,KAAK,UAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CAkCf"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell runtime 生命周期 action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 负责绑定宿主上下文与关闭当前 state 下的所有 shell。
|
|
6
|
+
* - 不处理单个 shell 的 start/read/write 等业务动作。
|
|
7
|
+
*/
|
|
8
|
+
import { nowMs, updateSessionSnapshot, } from "../ShellActionRuntimeSupport.js";
|
|
9
|
+
import { resolveApproval } from "../../approval/ShellApprovalRuntime.js";
|
|
10
|
+
/**
|
|
11
|
+
* 绑定当前 shell runtime 实例的 execution runtime。
|
|
12
|
+
*/
|
|
13
|
+
export function bindShellRuntime(state, context) {
|
|
14
|
+
state.context = context;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 关闭当前实例持有的所有活动 shell。
|
|
18
|
+
*/
|
|
19
|
+
export async function closeAllShellSessions(state, force = false) {
|
|
20
|
+
for (const approval of Array.from(state.approvals.values())) {
|
|
21
|
+
if (state.context) {
|
|
22
|
+
await resolveApproval({
|
|
23
|
+
state,
|
|
24
|
+
context: state.context,
|
|
25
|
+
approvalId: approval.approvalId,
|
|
26
|
+
decision: "expired",
|
|
27
|
+
}).catch(() => undefined);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
clearTimeout(approval.timer);
|
|
31
|
+
state.approvals.delete(approval.approvalId);
|
|
32
|
+
approval.resolve("expired");
|
|
33
|
+
}
|
|
34
|
+
const closing = Array.from(state.sessions.values()).map(async (session) => {
|
|
35
|
+
if (session.snapshot.status !== "running" &&
|
|
36
|
+
session.snapshot.status !== "starting") {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
session.child.kill(force ? "SIGKILL" : "SIGTERM");
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// ignore
|
|
44
|
+
}
|
|
45
|
+
await updateSessionSnapshot(session, (snapshot) => {
|
|
46
|
+
snapshot.status = force ? "killed" : "failed";
|
|
47
|
+
snapshot.exitCode = force ? -9 : -15;
|
|
48
|
+
snapshot.endedAt = nowMs();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
await Promise.all(closing);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=ShellLifecycleActions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
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AK,UAAU,EACtC,CAAC;YACD,OAAO;QACT,CAAC;QACD,IAAI,CAAC;YACH,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACpD,CAAC;QAAC,MAAM,CAAC;YACP,SAAS;QACX,CAAC;QACD,MAAM,qBAAqB,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE;YAChD,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;YAC9C,QAAQ,CAAC,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACrC,QAAQ,CAAC,OAAO,GAAG,KAAK,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell query/read/wait/close actions。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 聚合不创建新进程的 session 操作。
|
|
6
|
+
* - close 只负责单个 session,runtime shutdown 使用 lifecycle action。
|
|
7
|
+
*/
|
|
8
|
+
import type { ShellHostContext } from "../../types/ShellHostContext.js";
|
|
9
|
+
import type { ShellRuntimeState } from "../../session/ShellRuntimeTypes.js";
|
|
10
|
+
import type { ShellActionResponse, ShellCloseRequest, ShellQueryRequest, ShellReadRequest, ShellWaitRequest } from "../../types/ShellAction.js";
|
|
11
|
+
/**
|
|
12
|
+
* 查询 shell session 状态。
|
|
13
|
+
*/
|
|
14
|
+
export declare function getShellSessionStatus(state: ShellRuntimeState, context: ShellHostContext, request: ShellQueryRequest): Promise<ShellActionResponse>;
|
|
15
|
+
/**
|
|
16
|
+
* 读取 shell session 输出。
|
|
17
|
+
*/
|
|
18
|
+
export declare function readShellSession(state: ShellRuntimeState, context: ShellHostContext, request: ShellReadRequest): Promise<ShellActionResponse>;
|
|
19
|
+
/**
|
|
20
|
+
* 等待 shell session 状态变化。
|
|
21
|
+
*/
|
|
22
|
+
export declare function waitShellSession(state: ShellRuntimeState, context: ShellHostContext, request: ShellWaitRequest): Promise<ShellActionResponse>;
|
|
23
|
+
/**
|
|
24
|
+
* 关闭 shell session。
|
|
25
|
+
*/
|
|
26
|
+
export declare function closeShellSession(state: ShellRuntimeState, context: ShellHostContext, request: ShellCloseRequest): Promise<ShellActionResponse>;
|
|
27
|
+
//# sourceMappingURL=ShellQueryActions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellQueryActions.d.ts","sourceRoot":"","sources":["../../../src/session/actions/ShellQueryActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,KAAK,EACV,iBAAiB,EAElB,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EACV,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,wBAAwB,CAAC;AAahC;;GAEG;AACH,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,mBAAmB,CAAC,CAW9B;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,mBAAmB,CAAC,CAmB9B;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,mBAAmB,CAAC,CAiE9B;AAED;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,mBAAmB,CAAC,CAuC9B"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell query/read/wait/close actions。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 聚合不创建新进程的 session 操作。
|
|
6
|
+
* - close 只负责单个 session,runtime shutdown 使用 lifecycle action。
|
|
7
|
+
*/
|
|
8
|
+
import { buildActionResponse, clampWaitMsWithOptions, createOutputChunk, isInMemorySession, isTerminalStatus, nowMs, resolveSession, scheduleCleanup, updateSessionSnapshot, } from "../ShellActionRuntimeSupport.js";
|
|
9
|
+
/**
|
|
10
|
+
* 查询 shell session 状态。
|
|
11
|
+
*/
|
|
12
|
+
export async function getShellSessionStatus(state, context, request) {
|
|
13
|
+
const session = await resolveSession(state, context, {
|
|
14
|
+
...request,
|
|
15
|
+
includeCompleted: request.includeCompleted !== false,
|
|
16
|
+
});
|
|
17
|
+
if (!session) {
|
|
18
|
+
throw new Error("shell session not found");
|
|
19
|
+
}
|
|
20
|
+
return buildActionResponse({
|
|
21
|
+
shell: session.snapshot,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 读取 shell session 输出。
|
|
26
|
+
*/
|
|
27
|
+
export async function readShellSession(state, context, request) {
|
|
28
|
+
const session = await resolveSession(state, context, {
|
|
29
|
+
...request,
|
|
30
|
+
includeCompleted: request.includeCompleted !== false,
|
|
31
|
+
});
|
|
32
|
+
if (!session) {
|
|
33
|
+
throw new Error("shell session not found");
|
|
34
|
+
}
|
|
35
|
+
const chunk = createOutputChunk({
|
|
36
|
+
shellId: session.snapshot.shellId,
|
|
37
|
+
outputText: session.outputText,
|
|
38
|
+
fromCursor: request.fromCursor,
|
|
39
|
+
context,
|
|
40
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
41
|
+
});
|
|
42
|
+
return buildActionResponse({
|
|
43
|
+
shell: session.snapshot,
|
|
44
|
+
chunk,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 等待 shell session 状态变化。
|
|
49
|
+
*/
|
|
50
|
+
export async function waitShellSession(state, context, request) {
|
|
51
|
+
const shellId = String(request.shellId || "").trim();
|
|
52
|
+
if (!shellId)
|
|
53
|
+
throw new Error("shell.wait requires shellId");
|
|
54
|
+
const session = await resolveSession(state, context, {
|
|
55
|
+
shellId,
|
|
56
|
+
includeCompleted: true,
|
|
57
|
+
});
|
|
58
|
+
if (!session)
|
|
59
|
+
throw new Error("shell session not found");
|
|
60
|
+
if (isInMemorySession(session) &&
|
|
61
|
+
typeof request.afterVersion === "number" &&
|
|
62
|
+
session.snapshot.version <= request.afterVersion &&
|
|
63
|
+
(session.snapshot.status === "running" || session.snapshot.status === "starting")) {
|
|
64
|
+
const afterVersion = request.afterVersion;
|
|
65
|
+
await new Promise((resolve) => {
|
|
66
|
+
let settled = false;
|
|
67
|
+
let waiter;
|
|
68
|
+
const finish = () => {
|
|
69
|
+
if (settled)
|
|
70
|
+
return;
|
|
71
|
+
settled = true;
|
|
72
|
+
clearTimeout(waiter.timer);
|
|
73
|
+
session.waiters.delete(waiter);
|
|
74
|
+
resolve();
|
|
75
|
+
};
|
|
76
|
+
const timer = setTimeout(() => {
|
|
77
|
+
finish();
|
|
78
|
+
}, clampWaitMsWithOptions(state.options, request.timeoutMs, state.options.defaultWaitTimeoutMs));
|
|
79
|
+
waiter = {
|
|
80
|
+
resolve: finish,
|
|
81
|
+
timer,
|
|
82
|
+
};
|
|
83
|
+
session.waiters.add(waiter);
|
|
84
|
+
// 关键点(中文):注册 waiter 后立刻复查,避免状态变化发生在判断和注册之间。
|
|
85
|
+
if (session.snapshot.version > afterVersion ||
|
|
86
|
+
isTerminalStatus(session.snapshot.status)) {
|
|
87
|
+
finish();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
const refreshed = await resolveSession(state, context, {
|
|
92
|
+
shellId,
|
|
93
|
+
includeCompleted: true,
|
|
94
|
+
});
|
|
95
|
+
if (!refreshed)
|
|
96
|
+
throw new Error("shell session not found after wait");
|
|
97
|
+
const chunk = createOutputChunk({
|
|
98
|
+
shellId,
|
|
99
|
+
outputText: refreshed.outputText,
|
|
100
|
+
fromCursor: request.fromCursor,
|
|
101
|
+
context,
|
|
102
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
103
|
+
});
|
|
104
|
+
return buildActionResponse({
|
|
105
|
+
shell: refreshed.snapshot,
|
|
106
|
+
chunk,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 关闭 shell session。
|
|
111
|
+
*/
|
|
112
|
+
export async function closeShellSession(state, context, request) {
|
|
113
|
+
const shellId = String(request.shellId || "").trim();
|
|
114
|
+
if (!shellId)
|
|
115
|
+
throw new Error("shell.close requires shellId");
|
|
116
|
+
const session = await resolveSession(state, context, {
|
|
117
|
+
shellId,
|
|
118
|
+
includeCompleted: true,
|
|
119
|
+
});
|
|
120
|
+
if (!session)
|
|
121
|
+
throw new Error("shell session not found");
|
|
122
|
+
if (!isInMemorySession(session)) {
|
|
123
|
+
return buildActionResponse({
|
|
124
|
+
shell: session.snapshot,
|
|
125
|
+
note: "shell already completed and only persisted snapshot remains",
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (session.cleanupTimer) {
|
|
129
|
+
clearTimeout(session.cleanupTimer);
|
|
130
|
+
session.cleanupTimer = null;
|
|
131
|
+
}
|
|
132
|
+
if (session.snapshot.status === "running" || session.snapshot.status === "starting") {
|
|
133
|
+
try {
|
|
134
|
+
session.child.kill(request.force === true ? "SIGKILL" : "SIGTERM");
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// ignore
|
|
138
|
+
}
|
|
139
|
+
await updateSessionSnapshot(session, (snapshot) => {
|
|
140
|
+
snapshot.status = request.force === true ? "killed" : "failed";
|
|
141
|
+
snapshot.exitCode = request.force === true ? -9 : -15;
|
|
142
|
+
snapshot.endedAt = nowMs();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
scheduleCleanup(state, shellId);
|
|
146
|
+
return buildActionResponse({
|
|
147
|
+
shell: session.snapshot,
|
|
148
|
+
note: "shell close requested",
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=ShellQueryActions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellQueryActions.js","sourceRoot":"","sources":["../../../src/session/actions/ShellQueryActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAcH,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,EACL,cAAc,EACd,eAAe,EACf,qBAAqB,GACtB,MAAM,iCAAiC,CAAC;AAEzC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,KAAwB,EACxB,OAAyB,EACzB,OAA0B;IAE1B,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE;QACnD,GAAG,OAAO;QACV,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,KAAK,KAAK;KACrD,CAAC,CAAC;IACH,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,mBAAmB,CAAC;QACzB,KAAK,EAAE,OAAO,CAAC,QAAQ;KACxB,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,KAAwB,EACxB,OAAyB,EACzB,OAAyB;IAEzB,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,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C,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACrE,CAAC;QAAC,MAAM,CAAC;YACP,SAAS;QACX,CAAC;QACD,MAAM,qBAAqB,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE;YAChD,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC;YAC/D,QAAQ,CAAC,QAAQ,GAAG,OAAO,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACtD,QAAQ,CAAC,OAAO,GAAG,KAAK,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAChC,OAAO,mBAAmB,CAAC;QACzB,KAAK,EAAE,OAAO,CAAC,QAAQ;QACvB,IAAI,EAAE,uBAAuB;KAC9B,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell start action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 负责创建 shell session、发起 unrestricted 审批、启动子进程并返回初始输出。
|
|
6
|
+
* - 长轮询与读取逻辑仍由 query actions 提供。
|
|
7
|
+
*/
|
|
8
|
+
import type { ShellHostContext } from "../../types/ShellHostContext.js";
|
|
9
|
+
import type { ShellRuntimeState } from "../../session/ShellRuntimeTypes.js";
|
|
10
|
+
import type { ShellActionResponse, ShellStartRequest } from "../../types/ShellAction.js";
|
|
11
|
+
/**
|
|
12
|
+
* 启动一个 shell session。
|
|
13
|
+
*/
|
|
14
|
+
export declare function startShellSession(state: ShellRuntimeState, context: ShellHostContext, request: ShellStartRequest): Promise<ShellActionResponse>;
|
|
15
|
+
//# sourceMappingURL=ShellStartActions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellStartActions.d.ts","sourceRoot":"","sources":["../../../src/session/actions/ShellStartActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,KAAK,EACV,iBAAiB,EAGlB,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AA2BhC;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,mBAAmB,CAAC,CA+J9B"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell start action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 负责创建 shell session、发起 unrestricted 审批、启动子进程并返回初始输出。
|
|
6
|
+
* - 长轮询与读取逻辑仍由 query actions 提供。
|
|
7
|
+
*/
|
|
8
|
+
import fs from "fs-extra";
|
|
9
|
+
import { spawnShellProcess } from "../../sandbox/SandboxRunner.js";
|
|
10
|
+
import { generateId } from "../../utils/Id.js";
|
|
11
|
+
import { getShellDir, getShellOutputPath, getShellSnapshotPath } from "../Paths.js";
|
|
12
|
+
import { buildActionResponse, buildShellEnv, clampWaitMsWithOptions, createOutputChunk, ensureCapacity, isInMemorySession, persistSnapshot, resolveOwnerContextId, resolveSession, resolveShellCwd, } from "../ShellActionRuntimeSupport.js";
|
|
13
|
+
import { attachShellProcessEventHandlers } from "../ShellProcessEvents.js";
|
|
14
|
+
import { requestUnrestrictedApproval, validateUnrestrictedRequest, } from "../../approval/ShellApprovalRuntime.js";
|
|
15
|
+
import { buildDeniedApprovalResponse, resolveDefaultShellPath, resolveSandboxMode, } from "./ShellActionShared.js";
|
|
16
|
+
import { bindShellRuntime } from "./ShellLifecycleActions.js";
|
|
17
|
+
import { waitShellSession } from "./ShellQueryActions.js";
|
|
18
|
+
/**
|
|
19
|
+
* 启动一个 shell session。
|
|
20
|
+
*/
|
|
21
|
+
export async function startShellSession(state, context, request) {
|
|
22
|
+
const cmd = String(request.cmd || "").trim();
|
|
23
|
+
if (!cmd)
|
|
24
|
+
throw new Error("shell.start requires a non-empty cmd");
|
|
25
|
+
ensureCapacity(state);
|
|
26
|
+
bindShellRuntime(state, context);
|
|
27
|
+
const shellId = `sh_${generateId()}`;
|
|
28
|
+
const cwd = resolveShellCwd(context, request.cwd);
|
|
29
|
+
const shellPath = String(request.shell || resolveDefaultShellPath()).trim() || resolveDefaultShellPath();
|
|
30
|
+
const login = request.login !== false;
|
|
31
|
+
const sandboxMode = resolveSandboxMode(request.sandbox);
|
|
32
|
+
const reason = String(request.reason || "").trim();
|
|
33
|
+
const ownerContextId = resolveOwnerContextId(context, request.ownerContextId);
|
|
34
|
+
const shellDir = getShellDir(context.rootPath, shellId);
|
|
35
|
+
const snapshotFilePath = getShellSnapshotPath(context.rootPath, shellId);
|
|
36
|
+
const outputFilePath = getShellOutputPath(context.rootPath, shellId);
|
|
37
|
+
await fs.ensureDir(shellDir);
|
|
38
|
+
await fs.writeFile(outputFilePath, "", "utf-8");
|
|
39
|
+
let approvalId;
|
|
40
|
+
let approvalStatus;
|
|
41
|
+
if (sandboxMode === "unrestricted") {
|
|
42
|
+
const validationError = validateUnrestrictedRequest({ cmd, reason });
|
|
43
|
+
if (validationError)
|
|
44
|
+
throw new Error(validationError);
|
|
45
|
+
const approval = await requestUnrestrictedApproval({
|
|
46
|
+
state,
|
|
47
|
+
context,
|
|
48
|
+
shellId,
|
|
49
|
+
toolName: request.approvalToolName || "shell_start",
|
|
50
|
+
cmd,
|
|
51
|
+
cwd,
|
|
52
|
+
reason,
|
|
53
|
+
...(ownerContextId ? { ownerContextId } : {}),
|
|
54
|
+
});
|
|
55
|
+
approvalId = approval.approvalId;
|
|
56
|
+
approvalStatus = approval.status;
|
|
57
|
+
if (approval.status !== "approved") {
|
|
58
|
+
return buildDeniedApprovalResponse({
|
|
59
|
+
shellId,
|
|
60
|
+
...(ownerContextId ? { ownerContextId } : {}),
|
|
61
|
+
cmd,
|
|
62
|
+
cwd,
|
|
63
|
+
shellPath,
|
|
64
|
+
approvalId: approval.approvalId,
|
|
65
|
+
reason,
|
|
66
|
+
approvalStatus: approval.status,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const spawnResult = await spawnShellProcess({
|
|
71
|
+
context,
|
|
72
|
+
shellId,
|
|
73
|
+
shellDir,
|
|
74
|
+
cmd,
|
|
75
|
+
cwd,
|
|
76
|
+
shellPath,
|
|
77
|
+
login,
|
|
78
|
+
baseEnv: buildShellEnv(context),
|
|
79
|
+
sandboxMode,
|
|
80
|
+
});
|
|
81
|
+
const child = spawnResult.child;
|
|
82
|
+
const actualCwd = spawnResult.cwd;
|
|
83
|
+
const startedAt = Date.now();
|
|
84
|
+
let resolveCompletion = () => { };
|
|
85
|
+
const completionPromise = new Promise((resolve) => {
|
|
86
|
+
resolveCompletion = resolve;
|
|
87
|
+
});
|
|
88
|
+
const session = {
|
|
89
|
+
snapshot: {
|
|
90
|
+
shellId,
|
|
91
|
+
...(ownerContextId ? { ownerContextId } : {}),
|
|
92
|
+
cmd,
|
|
93
|
+
cwd: actualCwd,
|
|
94
|
+
shellPath,
|
|
95
|
+
sandboxed: spawnResult.sandboxed,
|
|
96
|
+
sandboxMode: spawnResult.sandboxMode || sandboxMode,
|
|
97
|
+
sandboxBackend: spawnResult.backend,
|
|
98
|
+
sandboxNetworkMode: spawnResult.networkMode,
|
|
99
|
+
sandboxDir: spawnResult.sandboxDir,
|
|
100
|
+
sandboxHomeDir: spawnResult.homeDir,
|
|
101
|
+
sandboxTmpDir: spawnResult.tmpDir,
|
|
102
|
+
sandboxCacheDir: spawnResult.cacheDir,
|
|
103
|
+
...(approvalStatus ? { approvalStatus } : {}),
|
|
104
|
+
...(approvalId ? { approvalId } : {}),
|
|
105
|
+
...(reason ? { approvalReason: reason } : {}),
|
|
106
|
+
stdinWritable: true,
|
|
107
|
+
status: "running",
|
|
108
|
+
...(typeof child.pid === "number" ? { pid: child.pid } : {}),
|
|
109
|
+
startedAt,
|
|
110
|
+
updatedAt: startedAt,
|
|
111
|
+
outputChars: 0,
|
|
112
|
+
droppedChars: 0,
|
|
113
|
+
version: 1,
|
|
114
|
+
autoNotifyOnExit: request.autoNotifyOnExit === true,
|
|
115
|
+
notificationSent: false,
|
|
116
|
+
externalRefs: [],
|
|
117
|
+
},
|
|
118
|
+
child,
|
|
119
|
+
outputText: "",
|
|
120
|
+
outputFilePath,
|
|
121
|
+
snapshotFilePath,
|
|
122
|
+
writeChain: Promise.resolve(),
|
|
123
|
+
cleanupTimer: null,
|
|
124
|
+
waiters: new Set(),
|
|
125
|
+
completionPromise,
|
|
126
|
+
resolveCompletion,
|
|
127
|
+
};
|
|
128
|
+
state.sessions.set(shellId, session);
|
|
129
|
+
// 关键点(中文):监听器必须先挂上,避免瞬时命令在 snapshot 持久化期间退出而丢失 close 事件。
|
|
130
|
+
attachShellProcessEventHandlers({ state, session });
|
|
131
|
+
await persistSnapshot(session);
|
|
132
|
+
const inlineWaitMs = clampWaitMsWithOptions(state.options, request.inlineWaitMs, state.options.defaultInlineWaitMs);
|
|
133
|
+
await waitShellSession(state, context, {
|
|
134
|
+
shellId,
|
|
135
|
+
afterVersion: 1,
|
|
136
|
+
fromCursor: 0,
|
|
137
|
+
timeoutMs: inlineWaitMs,
|
|
138
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
139
|
+
}).catch(() => undefined);
|
|
140
|
+
const latest = await resolveSession(state, context, { shellId, includeCompleted: true });
|
|
141
|
+
if (!latest) {
|
|
142
|
+
throw new Error(`shell session disappeared unexpectedly: ${shellId}`);
|
|
143
|
+
}
|
|
144
|
+
if (isInMemorySession(latest) &&
|
|
145
|
+
latest.snapshot.status === "running" &&
|
|
146
|
+
latest.snapshot.autoNotifyOnExit === false &&
|
|
147
|
+
request.autoNotifyOnExit !== false &&
|
|
148
|
+
Boolean(ownerContextId)) {
|
|
149
|
+
latest.snapshot.autoNotifyOnExit = true;
|
|
150
|
+
await persistSnapshot(latest);
|
|
151
|
+
}
|
|
152
|
+
const chunk = createOutputChunk({
|
|
153
|
+
shellId,
|
|
154
|
+
outputText: latest.outputText,
|
|
155
|
+
fromCursor: 0,
|
|
156
|
+
context,
|
|
157
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
158
|
+
});
|
|
159
|
+
return buildActionResponse({
|
|
160
|
+
shell: latest.snapshot,
|
|
161
|
+
chunk,
|
|
162
|
+
note: latest.snapshot.status === "running"
|
|
163
|
+
? "shell started and is still running"
|
|
164
|
+
: "shell finished during inline wait",
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=ShellStartActions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellStartActions.js","sourceRoot":"","sources":["../../../src/session/actions/ShellStartActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,UAAU,CAAC;AAE1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAM/D,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAM3C,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,sBAAsB,EACtB,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,eAAe,GAChB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,+BAA+B,EAAE,MAAM,0BAA0B,CAAC;AAC3E,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAC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cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;IACzF,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,2CAA2C,OAAO,EAAE,CAAC,CAAC;IACxE,CAAC;IACD,IACE,iBAAiB,CAAC,MAAM,CAAC;QACzB,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,SAAS;QACpC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,KAAK,KAAK;QAC1C,OAAO,CAAC,gBAAgB,KAAK,KAAK;QAClC,OAAO,CAAC,cAAc,CAAC,EACvB,CAAC;QACD,MAAM,CAAC,QAAQ,CAAC,gBAAgB,GAAG,IAAI,CAAC;QACxC,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IACD,MAAM,KAAK,GAAG,iBAAiB,CAAC;QAC9B,OAAO;QACP,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,UAAU,EAAE,CAAC;QACb,OAAO;QACP,eAAe,EAAE,OAAO,CAAC,eAAe;KACzC,CAAC,CAAC;IACH,OAAO,mBAAmB,CAAC;QACzB,KAAK,EAAE,MAAM,CAAC,QAAQ;QACtB,KAAK;QACL,IAAI,EACF,MAAM,CAAC,QAAQ,CAAC,MAAM,KAAK,SAAS;YAClC,CAAC,CAAC,oCAAoC;YACtC,CAAC,CAAC,mCAAmC;KAC1C,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell write action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - safe shell 可直接写入 stdin。
|
|
6
|
+
* - unrestricted shell 每次写入都必须带 reason 并完成审批。
|
|
7
|
+
*/
|
|
8
|
+
import type { ShellHostContext } from "../../types/ShellHostContext.js";
|
|
9
|
+
import type { ShellRuntimeState } from "../../session/ShellRuntimeTypes.js";
|
|
10
|
+
import type { ShellActionResponse, ShellWriteRequest } from "../../types/ShellAction.js";
|
|
11
|
+
/**
|
|
12
|
+
* 向 shell session 写入 stdin。
|
|
13
|
+
*/
|
|
14
|
+
export declare function writeShellSession(state: ShellRuntimeState, context: ShellHostContext, request: ShellWriteRequest): Promise<ShellActionResponse>;
|
|
15
|
+
//# sourceMappingURL=ShellWriteActions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellWriteActions.d.ts","sourceRoot":"","sources":["../../../src/session/actions/ShellWriteActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,KAAK,EACV,mBAAmB,EAEnB,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AAYhC;;GAEG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,iBAAiB,EACxB,OAAO,EAAE,gBAAgB,EACzB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,mBAAmB,CAAC,CAqE9B"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell write action。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - safe shell 可直接写入 stdin。
|
|
6
|
+
* - unrestricted shell 每次写入都必须带 reason 并完成审批。
|
|
7
|
+
*/
|
|
8
|
+
import { buildActionResponse, isInMemorySession, resolveSession, } from "../ShellActionRuntimeSupport.js";
|
|
9
|
+
import { requestUnrestrictedApproval, validateUnrestrictedRequest, } from "../../approval/ShellApprovalRuntime.js";
|
|
10
|
+
import { buildDeniedWriteApprovalResponse } from "./ShellActionShared.js";
|
|
11
|
+
/**
|
|
12
|
+
* 向 shell session 写入 stdin。
|
|
13
|
+
*/
|
|
14
|
+
export async function writeShellSession(state, context, request) {
|
|
15
|
+
const shellId = String(request.shellId || "").trim();
|
|
16
|
+
const chars = String(request.chars ?? "");
|
|
17
|
+
if (!shellId)
|
|
18
|
+
throw new Error("shell.write requires shellId");
|
|
19
|
+
const session = await resolveSession(state, context, {
|
|
20
|
+
shellId,
|
|
21
|
+
includeCompleted: true,
|
|
22
|
+
});
|
|
23
|
+
if (!session || !isInMemorySession(session)) {
|
|
24
|
+
throw new Error("shell session is not active in memory");
|
|
25
|
+
}
|
|
26
|
+
if (session.snapshot.status !== "running" && session.snapshot.status !== "starting") {
|
|
27
|
+
throw new Error(`shell session ${shellId} is not running`);
|
|
28
|
+
}
|
|
29
|
+
if (!session.child.stdin.writable) {
|
|
30
|
+
throw new Error(`shell session ${shellId} stdin is closed`);
|
|
31
|
+
}
|
|
32
|
+
if (session.snapshot.stdinWritable === false) {
|
|
33
|
+
throw new Error(`shell session ${shellId} stdin is closed`);
|
|
34
|
+
}
|
|
35
|
+
let approvalId;
|
|
36
|
+
let approvalStatus;
|
|
37
|
+
const reason = String(request.reason || "").trim();
|
|
38
|
+
if (session.snapshot.sandboxMode === "unrestricted") {
|
|
39
|
+
const validationError = validateUnrestrictedRequest({ cmd: chars, reason });
|
|
40
|
+
if (validationError)
|
|
41
|
+
throw new Error(validationError);
|
|
42
|
+
const approval = await requestUnrestrictedApproval({
|
|
43
|
+
state,
|
|
44
|
+
context,
|
|
45
|
+
shellId,
|
|
46
|
+
toolName: "shell_write",
|
|
47
|
+
cmd: chars,
|
|
48
|
+
cwd: session.snapshot.cwd,
|
|
49
|
+
reason,
|
|
50
|
+
...(session.snapshot.ownerContextId ? { ownerContextId: session.snapshot.ownerContextId } : {}),
|
|
51
|
+
inputPreview: chars,
|
|
52
|
+
inputChars: chars.length,
|
|
53
|
+
});
|
|
54
|
+
approvalId = approval.approvalId;
|
|
55
|
+
approvalStatus = approval.status;
|
|
56
|
+
if (approval.status !== "approved") {
|
|
57
|
+
return buildDeniedWriteApprovalResponse({
|
|
58
|
+
session,
|
|
59
|
+
approvalId: approval.approvalId,
|
|
60
|
+
reason,
|
|
61
|
+
approvalStatus: approval.status,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
await new Promise((resolve, reject) => {
|
|
66
|
+
session.child.stdin.write(chars, (error) => {
|
|
67
|
+
if (error) {
|
|
68
|
+
reject(error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
resolve();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
return buildActionResponse({
|
|
75
|
+
shell: {
|
|
76
|
+
...session.snapshot,
|
|
77
|
+
...(approvalStatus ? { approvalStatus } : {}),
|
|
78
|
+
...(approvalId ? { approvalId } : {}),
|
|
79
|
+
...(reason ? { approvalReason: reason } : {}),
|
|
80
|
+
stdinWritable: true,
|
|
81
|
+
},
|
|
82
|
+
note: chars ? "stdin written" : "no chars written",
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=ShellWriteActions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellWriteActions.js","sourceRoot":"","sources":["../../../src/session/actions/ShellWriteActions.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AASH,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,GACf,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,gCAAgC,EAAE,MAAM,wBAAwB,CAAC;AAE1E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,KAAwB,EACxB,OAAyB,EACzB,OAA0B;IAE1B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACrD,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;IAC1C,IAAI,CAAC,OAAO;QAAE,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C;YACrC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,aAAa,EAAE,IAAI;SACpB;QACD,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,kBAAkB;KACnD,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell tool 辅助函数。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 这里只保留 shell tool 自身的输入保护逻辑。
|
|
6
|
+
* - 不解析 Agent 私有输出协议,不向 session 注入消息。
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 对聊天发送命令做前置安全校验。
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateChatSendCommand(cmd: string): string | null;
|
|
12
|
+
//# sourceMappingURL=ShellToolFormatting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellToolFormatting.d.ts","sourceRoot":"","sources":["../../src/tool/ShellToolFormatting.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiBlE"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell tool 辅助函数。
|
|
3
|
+
*
|
|
4
|
+
* 关键点(中文)
|
|
5
|
+
* - 这里只保留 shell tool 自身的输入保护逻辑。
|
|
6
|
+
* - 不解析 Agent 私有输出协议,不向 session 注入消息。
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 对聊天发送命令做前置安全校验。
|
|
10
|
+
*/
|
|
11
|
+
export function validateChatSendCommand(cmd) {
|
|
12
|
+
const source = String(cmd ?? "");
|
|
13
|
+
if (!/\b(?:bay|town)\s+chat\s+send\b/.test(source))
|
|
14
|
+
return null;
|
|
15
|
+
if (!/[\r\n]/.test(source))
|
|
16
|
+
return null;
|
|
17
|
+
if (/\b(?:bay|town)\s+chat\s+send\b[\s\S]*\s--stdin(?:\s|$)/.test(source)) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
if (/\b(?:bay|town)\s+chat\s+send\b[\s\S]*\s--text(?:\s|$)/.test(source)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
if (/\b(?:bay|town)\s+chat\s+send\b[\s\S]*\s--text-file(?:\s|$)/.test(source)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return [
|
|
27
|
+
"Unsafe chat send command: real newlines are not allowed.",
|
|
28
|
+
"If your message is multi-line, use chat send --stdin, --text-file, or explicit --text.",
|
|
29
|
+
].join(" ");
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=ShellToolFormatting.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShellToolFormatting.js","sourceRoot":"","sources":["../../src/tool/ShellToolFormatting.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;GAEG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAW;IACjD,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;IACjC,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IAChE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IACxC,IAAI,wDAAwD,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1E,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,uDAAuD,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACzE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,4DAA4D,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9E,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO;QACL,0DAA0D;QAC1D,wFAAwF;KACzF,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC"}
|