Skip to content

Commit d9e9931

Browse files
author
linyuan.yang
committed
整理 shell 代码
1 parent 6cf00ec commit d9e9931

6 files changed

Lines changed: 49 additions & 58 deletions

File tree

‎packages/sbot/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ program
3838
const child = spawn(process.execPath, [__filename, ...args], {
3939
detached: true,
4040
stdio: 'ignore',
41+
windowsHide: true,
4142
});
4243
child.unref();
4344
console.log(`sbot 已在后台启动 (PID: ${child.pid})`);

‎packages/scorpio.ai/src/Tools/Process/codeTool.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { z } from 'zod';
66
import { DynamicStructuredTool, type StructuredToolInterface } from '@langchain/core/tools';
77
import { createErrorResult, type MCPToolResult } from '../Core';
88
import { runProgram, runShellCommand } from './runtime/foreground';
9-
import { resolveWorkingDir } from './runtime/workingDir';
10-
import { isCommandAvailable } from './runtime/process';
9+
import { isCommandAvailable, resolveWorkingDir } from './runtime/process';
1110
import { processManager, formatProcessResult } from './runtime/processManager';
1211

1312
export enum CodeRuntime {

‎packages/scorpio.ai/src/Tools/Process/index.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export { runProgram, runShellCommand, MAX_OUTPUT_BYTES } from './runtime/foreground';
2-
export { getCurrentShell, isCommandAvailable } from './runtime/process';
3-
export { validatePath, resolveWorkingDir } from './runtime/workingDir';
2+
export { getCurrentShell, isCommandAvailable, validatePath, resolveWorkingDir } from './runtime/process';
43
export {
54
createShellTool,
65
createReadProcessTool,

‎packages/scorpio.ai/src/Tools/Process/runtime/process.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,45 @@ export function getChildProcessEnv(): NodeJS.ProcessEnv {
9898
return safeEnv;
9999
}
100100

101+
export function validatePath(filePath: string): { valid: boolean; error?: string; absolutePath?: string } {
102+
if (!filePath || typeof filePath !== 'string') {
103+
return { valid: false, error: 'Path is empty' };
104+
}
105+
if (!path.isAbsolute(filePath)) {
106+
return { valid: false, error: `Path must be absolute: ${filePath}` };
107+
}
108+
return { valid: true, absolutePath: path.normalize(filePath) };
109+
}
110+
111+
function withTimeout<T>(p: Promise<T>, ms: number, label: string): Promise<T> {
112+
let timer: NodeJS.Timeout | undefined;
113+
p.catch(() => { /* 防止 race 失败方触发 unhandledRejection */ });
114+
return Promise.race([
115+
p,
116+
new Promise<T>((_, reject) => {
117+
timer = setTimeout(() => reject(Object.assign(new Error(`${label} timed out (${ms} ms)`), { code: 'ETIMEDOUT' })), ms);
118+
}),
119+
]).finally(() => { if (timer) clearTimeout(timer); });
120+
}
121+
122+
export async function resolveWorkingDir(workingDir: string | undefined): Promise<{ cwd?: string; error?: string }> {
123+
if (!workingDir) return { error: 'workingDir is required' };
124+
125+
const v = validatePath(workingDir);
126+
if (!v.valid) return { error: v.error };
127+
128+
const cwd = v.absolutePath!;
129+
try {
130+
const stat = await withTimeout(fs.promises.stat(cwd), PROBE_TIMEOUT_MS, `stat ${cwd}`);
131+
if (!stat.isDirectory()) return { error: `Path is not a directory: ${cwd}` };
132+
return { cwd };
133+
} catch (e: any) {
134+
if (e?.code === 'ENOENT') return { error: `Working directory not found: ${cwd}` };
135+
if (e?.code === 'ETIMEDOUT') return { error: e.message };
136+
return { error: `Failed to check working directory: ${e?.message ?? e}` };
137+
}
138+
}
139+
101140
export async function killProcessTree(proc: ChildProcess, isExited: () => boolean): Promise<void> {
102141
const pid = proc.pid;
103142
if (!pid || isExited()) return;

‎packages/scorpio.ai/src/Tools/Process/runtime/processManager.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ interface ProcSession {
6969
cleanup?: CleanupFn;
7070
}
7171

72-
function clampYield(ms: number | undefined): number {
73-
const v = typeof ms === 'number' && Number.isFinite(ms) ? ms : YIELD_MIN_MS;
74-
return Math.min(YIELD_MAX_MS, Math.max(YIELD_MIN_MS, v));
75-
}
76-
7772
export class ProcessManager {
7873
private sessions = new Map<number, ProcSession>();
7974
private nextProcessId = 1;
@@ -94,7 +89,7 @@ export class ProcessManager {
9489
}
9590
session.lastUsed = Date.now();
9691

97-
const collected = await this.collectOutput(session, clampYield(yieldMs), true);
92+
const collected = await this.collectOutput(session, this.clampYield(yieldMs), true);
9893

9994
if (session.exited) {
10095
this.sessions.delete(processId);
@@ -169,7 +164,7 @@ export class ProcessManager {
169164
this.wireStreams(session);
170165
this.register(session);
171166

172-
const collected = await this.collectOutput(session, clampYield(yieldMs), false);
167+
const collected = await this.collectOutput(session, this.clampYield(yieldMs), false);
173168

174169
if (spawnError) {
175170
this.sessions.delete(id);
@@ -263,6 +258,11 @@ export class ProcessManager {
263258
}
264259
}
265260

261+
private clampYield(ms: number | undefined): number {
262+
const v = typeof ms === 'number' && Number.isFinite(ms) ? ms : YIELD_MIN_MS;
263+
return Math.min(YIELD_MAX_MS, Math.max(YIELD_MIN_MS, v));
264+
}
265+
266266
private ensureReaper(): void {
267267
if (this.reaper) return;
268268
this.reaper = setInterval(() => {

‎packages/scorpio.ai/src/Tools/Process/runtime/workingDir.ts‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)