@anhlp/alp-runtime 0.0.0-stage → 0.0.1-beta.10
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/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +6 -2
- package/dist/acp-transport.d.ts +130 -0
- package/dist/acp-transport.js +482 -0
- package/dist/antigravity-transport.d.ts +107 -0
- package/dist/antigravity-transport.js +665 -0
- package/dist/assignment-results.d.ts +55 -0
- package/dist/assignment-results.js +181 -0
- package/dist/attachments.d.ts +84 -0
- package/dist/attachments.js +213 -0
- package/dist/board-domain.d.ts +56 -0
- package/dist/board-domain.js +157 -0
- package/dist/board.d.ts +27 -0
- package/dist/board.js +76 -0
- package/dist/catalog.d.ts +44 -0
- package/dist/catalog.js +47 -0
- package/dist/claude-transport.d.ts +251 -0
- package/dist/claude-transport.js +931 -0
- package/dist/compact.d.ts +29 -0
- package/dist/compact.js +49 -0
- package/dist/configure.d.ts +44 -0
- package/dist/configure.js +64 -0
- package/dist/contracts.d.ts +378 -0
- package/dist/contracts.js +16 -0
- package/dist/conversation.d.ts +45 -0
- package/dist/conversation.js +62 -0
- package/dist/events.d.ts +1 -0
- package/dist/events.js +0 -0
- package/dist/github.d.ts +10 -0
- package/dist/github.js +39 -0
- package/dist/handoff.d.ts +36 -0
- package/dist/handoff.js +73 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +122 -0
- package/dist/input-limits.d.ts +25 -0
- package/dist/input-limits.js +48 -0
- package/dist/language.d.ts +51 -0
- package/dist/language.js +94 -0
- package/dist/live.d.ts +48 -0
- package/dist/live.js +50 -0
- package/dist/mailbox.d.ts +26 -0
- package/dist/mailbox.js +38 -0
- package/dist/mcp-tools.d.ts +123 -0
- package/dist/mcp-tools.js +97 -0
- package/dist/model-catalog.d.ts +162 -0
- package/dist/model-catalog.js +338 -0
- package/dist/model-probes.d.ts +7 -0
- package/dist/model-probes.js +69 -0
- package/dist/permission-path.d.ts +2 -0
- package/dist/permission-path.js +44 -0
- package/dist/process-info.d.ts +14 -0
- package/dist/process-info.js +32 -0
- package/dist/providers.d.ts +28 -0
- package/dist/providers.js +73 -0
- package/dist/recall.d.ts +42 -0
- package/dist/recall.js +61 -0
- package/dist/resolve.d.ts +123 -0
- package/dist/resolve.js +242 -0
- package/dist/runtime-context.d.ts +43 -0
- package/dist/runtime-context.js +61 -0
- package/dist/runtime.d.ts +13 -0
- package/dist/runtime.js +4818 -0
- package/dist/session-config.d.ts +2345 -0
- package/dist/session-config.js +209 -0
- package/dist/session-state.d.ts +190 -0
- package/dist/session-state.js +0 -0
- package/dist/skill-resources.d.ts +30 -0
- package/dist/skill-resources.js +55 -0
- package/dist/tool-detail.d.ts +81 -0
- package/dist/tool-detail.js +332 -0
- package/dist/tool-results.d.ts +12 -0
- package/dist/tool-results.js +25 -0
- package/dist/tools.d.ts +827 -0
- package/dist/tools.js +463 -0
- package/dist/transport.d.ts +47 -0
- package/dist/transport.js +171 -0
- package/dist/usage.d.ts +25 -0
- package/dist/usage.js +60 -0
- package/dist/workspace.d.ts +127 -0
- package/dist/workspace.js +340 -0
- package/dist/worktree-domain.d.ts +89 -0
- package/dist/worktree-domain.js +199 -0
- package/dist/write-error.d.ts +2 -0
- package/dist/write-error.js +10 -0
- package/package.json +262 -4
- package/src/acp-transport.ts +481 -0
- package/src/antigravity-transport.ts +740 -0
- package/src/assignment-results.ts +154 -0
- package/src/attachments.ts +254 -0
- package/src/board-domain.ts +185 -0
- package/src/board.ts +87 -0
- package/src/catalog.ts +43 -0
- package/src/claude-transport.ts +1097 -0
- package/src/compact.ts +62 -0
- package/src/configure.ts +93 -0
- package/src/contracts.ts +363 -0
- package/src/conversation.ts +81 -0
- package/src/events.ts +1 -0
- package/src/github.ts +37 -0
- package/src/handoff.ts +86 -0
- package/src/index.ts +41 -0
- package/src/input-limits.ts +65 -0
- package/src/language.ts +130 -0
- package/src/live.ts +90 -0
- package/src/mailbox.ts +49 -0
- package/src/mcp-tools.ts +106 -0
- package/src/model-catalog.ts +316 -0
- package/src/model-probes.ts +58 -0
- package/src/permission-path.ts +37 -0
- package/src/process-info.ts +40 -0
- package/src/providers.ts +87 -0
- package/src/recall.ts +86 -0
- package/src/resolve.ts +238 -0
- package/src/runtime-context.ts +49 -0
- package/src/runtime.ts +5608 -0
- package/src/session-config.ts +312 -0
- package/src/session-state.ts +180 -0
- package/src/skill-resources.ts +73 -0
- package/src/tool-detail.ts +397 -0
- package/src/tool-results.ts +26 -0
- package/src/tools.ts +466 -0
- package/src/transport.ts +118 -0
- package/src/usage.ts +79 -0
- package/src/workspace.ts +424 -0
- package/src/worktree-domain.ts +220 -0
- package/src/write-error.ts +4 -0
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
import { spawn, execFile, type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
2
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
3
|
+
import { realpathSync, rmSync } from 'node:fs';
|
|
4
|
+
import { mkdir, open, readFile, rename, writeFile, unlink } from 'node:fs/promises';
|
|
5
|
+
import net from 'node:net';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { createInterface, type Interface } from 'node:readline';
|
|
9
|
+
import { openMcpTools } from './mcp-tools.js';
|
|
10
|
+
import { antigravityToolDetail } from './tool-detail.js';
|
|
11
|
+
import { bridgeCommand, isBridgeArgs } from '@anhlp/alp-core/bridges';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Antigravity provider definition for ALP.
|
|
15
|
+
*/
|
|
16
|
+
export type AntigravityProvider = {
|
|
17
|
+
id: string;
|
|
18
|
+
label: string;
|
|
19
|
+
command: string;
|
|
20
|
+
args?: string[];
|
|
21
|
+
env?: Record<string, string>;
|
|
22
|
+
models?: Array<{ id: string; label?: string; description?: string }>;
|
|
23
|
+
additionalModels?: Array<{ id: string; label?: string; description?: string }>;
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type Listener = (method: string, params: any) => void;
|
|
28
|
+
type RequestHandler = (method: string, params: any) => Promise<unknown>;
|
|
29
|
+
|
|
30
|
+
/** The MCP server ALP's tools reach an Antigravity agent through via local socket. */
|
|
31
|
+
|
|
32
|
+
const MODES = ['read-only', 'workspace-write', 'full-access'];
|
|
33
|
+
/**
|
|
34
|
+
* Commands read-only allows. agy matches an allow rule by command prefix and denies
|
|
35
|
+
* redirection, chaining and substitution itself (checked with agy 1.4.10), so each
|
|
36
|
+
* entry must have no flag that writes: git is left out (`git log --output`, and
|
|
37
|
+
* `git status` refreshes the index), as are rg (`--pre`) and find (`-delete`).
|
|
38
|
+
* A prefix rule cannot check a path or a flag, so no command that reads file contents
|
|
39
|
+
* is allowed: cat, head, grep and wc read files outside the workspace, and tail -f or a
|
|
40
|
+
* read of a FIFO hangs the turn. ls is left out too: a prefix rule cannot keep its path
|
|
41
|
+
* argument inside the workspace (`ls /`, `ls ~/.ssh`). agy's own file reading, listing and
|
|
42
|
+
* search tools remain.
|
|
43
|
+
*/
|
|
44
|
+
const READ_ONLY_COMMANDS = ['pwd'];
|
|
45
|
+
/** Shell syntax that makes a command line more than one allowed command. */
|
|
46
|
+
const SHELL_SYNTAX = /[;&|<>`$(){}\\\n\r]/;
|
|
47
|
+
/**
|
|
48
|
+
* Whether read-only allows a terminal command line, by ALP's own reading of the allowlist:
|
|
49
|
+
* one allowed command with no shell syntax. Unknown input is refused.
|
|
50
|
+
*/
|
|
51
|
+
export function antigravityReadOnlyCommandAllowed(commandLine: unknown): boolean {
|
|
52
|
+
if (typeof commandLine !== 'string' || SHELL_SYNTAX.test(commandLine)) return false;
|
|
53
|
+
const [command] = commandLine.trim().split(/\s+/);
|
|
54
|
+
return READ_ONLY_COMMANDS.includes(command);
|
|
55
|
+
}
|
|
56
|
+
const COMMAND_TOOLS = new Set(['run_command']);
|
|
57
|
+
/** agy's file-editing tools, which read-only always denies. */
|
|
58
|
+
const EDIT_TOOLS = new Set(['write_file', 'write_to_file', 'replace_file_content', 'multi_replace_file_content', 'sed_file', 'notebook_edit']);
|
|
59
|
+
const modeOf = (sandbox: unknown) => sandbox === 'danger-full-access' ? 'full-access' : String(sandbox);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The agy flags for an ALP mode. Headless agy cannot ask the user, so a tool its
|
|
63
|
+
* permission mode does not allow is denied: both limited modes fail closed.
|
|
64
|
+
* - read-only: agy's default request-review policy; built-in reads and search run,
|
|
65
|
+
* READ_ONLY_COMMANDS run; edits and every other command are denied.
|
|
66
|
+
* - workspace-write: edits accepted and commands run in agy's terminal sandbox,
|
|
67
|
+
* which keeps writes in the workspace; a bypass of the sandbox is denied.
|
|
68
|
+
* - full-access: every tool runs without asking.
|
|
69
|
+
* Both limited modes trust the working directory (otherwise agy denies even its
|
|
70
|
+
* reads and edits there) and allow ALP's own MCP server, which carries alp_*.
|
|
71
|
+
*/
|
|
72
|
+
export function antigravityModeArgs(sandbox: unknown, cwd: string): string[] {
|
|
73
|
+
const mode = modeOf(sandbox);
|
|
74
|
+
if (mode === 'full-access') return ['--dangerously-skip-permissions'];
|
|
75
|
+
if (!MODES.includes(mode)) throw new Error(`Antigravity does not support mode '${String(sandbox)}'`);
|
|
76
|
+
let real = cwd;
|
|
77
|
+
try { real = realpathSync(cwd); } catch {}
|
|
78
|
+
const settings = {
|
|
79
|
+
toolPermission: mode === 'workspace-write' ? 'proceed-in-sandbox' : 'request-review',
|
|
80
|
+
enableTerminalSandbox: true,
|
|
81
|
+
trustedWorkspaces: [...new Set([cwd, real])],
|
|
82
|
+
permissions: { allow: ['mcp(alp/*)', ...(mode === 'read-only' ? READ_ONLY_COMMANDS.map(command => `command(${command})`) : [])] },
|
|
83
|
+
};
|
|
84
|
+
return [...(mode === 'workspace-write' ? ['--mode', 'accept-edits'] : []), '--sandbox', '--settings', JSON.stringify(settings)];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Efforts `agy --effort` takes (agy --help: low|medium|high|xhigh|max); others leave agy's default. */
|
|
88
|
+
export const AGY_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
|
|
89
|
+
export function antigravityEffortArgs(thinking: unknown): string[] {
|
|
90
|
+
return typeof thinking === 'string' && AGY_EFFORTS.includes(thinking) ? ['--effort', thinking] : [];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** What each mode lets an Antigravity agent do, for its instructions. */
|
|
94
|
+
export function antigravityModeText(sandbox: unknown): string {
|
|
95
|
+
const mode = modeOf(sandbox);
|
|
96
|
+
if (mode === 'read-only') return 'agy enforces read-only: its own file reading and search tools work, and ALP\'s tools; it allows only the terminal command pwd, with no redirection or chaining (read files with its own tools), and denies every file edit and other command. A denied tool ends your turn, so do not attempt one: report work that needs changes to your requester.';
|
|
97
|
+
if (mode === 'workspace-write') return 'agy accepts file edits and runs terminal commands in its sandbox, which writes only inside your workspace; anything outside the workspace or the sandbox is denied, and a denied tool ends your turn. Report work that needs more to your requester.';
|
|
98
|
+
return 'Antigravity runs with full access: every tool runs without asking.';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function encodePrompt(text: string): string {
|
|
102
|
+
return `${JSON.stringify({ event: 'user', message: { content: text } })}\n`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* AntigravityTransport runs the Antigravity CLI (agy) over its stream-json protocol.
|
|
107
|
+
* Emits line-delimited JSON events and parses stdout line by line.
|
|
108
|
+
*/
|
|
109
|
+
export class AntigravityTransport {
|
|
110
|
+
/** Antigravity's prompt is one line of text: no images. */
|
|
111
|
+
takesImages() { return false; }
|
|
112
|
+
private child?: ChildProcessWithoutNullStreams;
|
|
113
|
+
private lines?: Interface;
|
|
114
|
+
private exit: Promise<void> = Promise.resolve();
|
|
115
|
+
private listeners = new Set<Listener>();
|
|
116
|
+
private failures = new Set<(error: Error) => void>();
|
|
117
|
+
private requestHandler?: RequestHandler;
|
|
118
|
+
private closed = false;
|
|
119
|
+
private conversationId = '';
|
|
120
|
+
private config: any;
|
|
121
|
+
private activeTurn?: string;
|
|
122
|
+
private messageText = new Map<string, string>();
|
|
123
|
+
private messages = 0;
|
|
124
|
+
private instructionsSent = false;
|
|
125
|
+
private modeNote?: string;
|
|
126
|
+
private tools: Array<{ name: string; description: string; inputSchema?: unknown }> = [];
|
|
127
|
+
private bridge?: { server: net.Server; socket: string; token: string; connections: Set<net.Socket> };
|
|
128
|
+
private readyPromise?: Promise<void>;
|
|
129
|
+
private resolveReady?: () => void;
|
|
130
|
+
private rejectReady?: (error: Error) => void;
|
|
131
|
+
private external?: Awaited<ReturnType<typeof openMcpTools>>;
|
|
132
|
+
private externalStarting?: ReturnType<typeof openMcpTools>;
|
|
133
|
+
private readonly startupAbort = new AbortController();
|
|
134
|
+
|
|
135
|
+
constructor(
|
|
136
|
+
private readonly command: string = 'agy',
|
|
137
|
+
private readonly cwd: string = process.cwd(),
|
|
138
|
+
private readonly env: NodeJS.ProcessEnv = process.env,
|
|
139
|
+
private readonly provider?: AntigravityProvider,
|
|
140
|
+
) {}
|
|
141
|
+
|
|
142
|
+
async initialize(): Promise<void> {
|
|
143
|
+
if (/\.(cmd|bat|ps1)$/i.test(this.command)) {
|
|
144
|
+
throw new Error('Antigravity command must be a native executable, not a shell launcher');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
onNotification(listener: Listener): () => void {
|
|
149
|
+
this.listeners.add(listener);
|
|
150
|
+
return () => this.listeners.delete(listener);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
onFailure(listener: (error: Error) => void): () => void {
|
|
154
|
+
this.failures.add(listener);
|
|
155
|
+
return () => this.failures.delete(listener);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
onRequest(handler: RequestHandler): void {
|
|
159
|
+
this.requestHandler = handler;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async request(method: string, params: any): Promise<any> {
|
|
163
|
+
if (this.closed) throw new Error('Runtime is closed');
|
|
164
|
+
|
|
165
|
+
if (method === 'thread/start' || method === 'thread/resume') {
|
|
166
|
+
antigravityModeArgs(params.sandbox, params.cwd || this.cwd);
|
|
167
|
+
this.config = params;
|
|
168
|
+
this.tools = params.dynamicTools ?? [];
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
if (Object.keys(params.mcpServers ?? {}).length) {
|
|
172
|
+
this.externalStarting = openMcpTools(params.mcpServers, params.cwd || this.cwd, { ...this.env, ...this.provider?.env, ...params.env }, this.startupAbort.signal);
|
|
173
|
+
const external = await this.externalStarting;
|
|
174
|
+
if (this.closed) { await external.close(); throw new Error('Runtime is closed'); }
|
|
175
|
+
this.external = external;
|
|
176
|
+
}
|
|
177
|
+
if (this.tools.length > 0 || this.external?.tools.length) await this.openBridge();
|
|
178
|
+
if (this.closed) throw new Error('Runtime is closed');
|
|
179
|
+
await this.startProcess(params);
|
|
180
|
+
} catch (error) { await this.close(); throw error; }
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
thread: { id: this.conversationId, turns: [] },
|
|
184
|
+
cwd: params.cwd,
|
|
185
|
+
model: params.model,
|
|
186
|
+
reasoningEffort: params.thinking,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (method === 'turn/start') {
|
|
191
|
+
if (!this.conversationId) throw new Error('Antigravity session is not open');
|
|
192
|
+
if (!this.child) await this.startProcess({ ...this.config, threadId: this.conversationId });
|
|
193
|
+
if (this.activeTurn) throw new Error('Antigravity already has an active turn');
|
|
194
|
+
|
|
195
|
+
const turnId = randomUUID();
|
|
196
|
+
this.activeTurn = turnId;
|
|
197
|
+
this.messageText.clear();
|
|
198
|
+
|
|
199
|
+
const parts = (params.input ?? [])
|
|
200
|
+
.filter((part: any) => typeof part?.text === 'string')
|
|
201
|
+
.map((part: any) => part.text);
|
|
202
|
+
let promptText = parts.join('\n\n');
|
|
203
|
+
|
|
204
|
+
if (this.modeNote) {
|
|
205
|
+
promptText = `${this.modeNote}\n\n${promptText}`;
|
|
206
|
+
this.modeNote = undefined;
|
|
207
|
+
}
|
|
208
|
+
if (!this.instructionsSent && this.config?.developerInstructions) {
|
|
209
|
+
promptText = `Instructions from ALP, which runs you. Follow them for this whole session.\n\n${this.config.developerInstructions}\n\n${promptText}`;
|
|
210
|
+
this.instructionsSent = true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
setImmediate(() => {
|
|
214
|
+
try {
|
|
215
|
+
this.child?.stdin.write(encodePrompt(promptText));
|
|
216
|
+
} catch (error) {
|
|
217
|
+
this.endTurn(turnId, 'failed', error instanceof Error ? error.message : String(error));
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return { turn: { id: turnId } };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (method === 'turn/steer') {
|
|
225
|
+
throw new Error('Antigravity takes no messages during a turn; ALP delivers them when it ends');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (method === 'turn/interrupt') {
|
|
229
|
+
if (this.child && this.activeTurn) {
|
|
230
|
+
const turnId = this.activeTurn;
|
|
231
|
+
await this.stopProcess();
|
|
232
|
+
this.endTurn(turnId, 'interrupted');
|
|
233
|
+
}
|
|
234
|
+
return {};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (method === 'session/configure' || method === 'thread/delete') {
|
|
238
|
+
if (method === 'session/configure' && params.sandbox && this.config && modeOf(params.sandbox) !== modeOf(this.config.sandbox)) {
|
|
239
|
+
antigravityModeArgs(params.sandbox, this.config.cwd || this.cwd);
|
|
240
|
+
if (this.activeTurn) throw new Error('Antigravity changes mode between turns only');
|
|
241
|
+
// agy takes its mode at launch: the next turn resumes the conversation in a process with the new one.
|
|
242
|
+
this.config = { ...this.config, sandbox: params.sandbox };
|
|
243
|
+
this.modeNote = `ALP changed your permission mode to ${modeOf(params.sandbox)}. ${antigravityModeText(params.sandbox)}`;
|
|
244
|
+
await this.stopProcess();
|
|
245
|
+
}
|
|
246
|
+
return {};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
throw new Error(`Antigravity does not support '${method}'`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private startProcess(params: any): Promise<void> {
|
|
253
|
+
if (this.closed) return Promise.reject(new Error('Runtime is closed'));
|
|
254
|
+
this.readyPromise = new Promise((resolve, reject) => {
|
|
255
|
+
this.resolveReady = resolve;
|
|
256
|
+
this.rejectReady = reject;
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const args = [
|
|
260
|
+
...(this.provider?.args ?? []),
|
|
261
|
+
'--input-format',
|
|
262
|
+
'stream-json',
|
|
263
|
+
'--output-format',
|
|
264
|
+
'stream-json',
|
|
265
|
+
'--add-dir',
|
|
266
|
+
params.cwd || this.cwd,
|
|
267
|
+
'--disable-slash-commands',
|
|
268
|
+
...antigravityModeArgs(params.sandbox, params.cwd || this.cwd),
|
|
269
|
+
];
|
|
270
|
+
|
|
271
|
+
if (params.model) {
|
|
272
|
+
args.push('--model', params.model);
|
|
273
|
+
}
|
|
274
|
+
args.push(...antigravityEffortArgs(params.thinking));
|
|
275
|
+
|
|
276
|
+
if (params.threadId) {
|
|
277
|
+
args.push('--conversation', params.threadId);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const env: NodeJS.ProcessEnv = {
|
|
281
|
+
...this.env,
|
|
282
|
+
...(this.provider?.env ?? {}),
|
|
283
|
+
...(params.env ?? {}),
|
|
284
|
+
// Provider or session env never sheds the agent mark (t-ca1f).
|
|
285
|
+
...(this.env.ALP_AGENT_SESSION ? { ALP_AGENT_SESSION: this.env.ALP_AGENT_SESSION } : {}),
|
|
286
|
+
// Session credentials are inherited by this agy's MCP child only, never persisted.
|
|
287
|
+
ALP_BRIDGE_SOCKET: this.bridge?.socket ?? '',
|
|
288
|
+
ALP_BRIDGE_TOKEN: this.bridge?.token ?? '',
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const child = spawn(this.command, args, {
|
|
292
|
+
cwd: params.cwd || this.cwd,
|
|
293
|
+
env,
|
|
294
|
+
windowsHide: true,
|
|
295
|
+
detached: process.platform !== 'win32',
|
|
296
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
this.child = child;
|
|
300
|
+
this.exit = new Promise(resolve => {
|
|
301
|
+
child.once('close', () => resolve());
|
|
302
|
+
child.once('error', () => resolve());
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
let stderr = '';
|
|
306
|
+
child.stderr.on('data', chunk => {
|
|
307
|
+
stderr = (stderr + chunk.toString()).slice(-8192);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
child.stdin.on('error', error => this.fail(error));
|
|
311
|
+
child.on('error', error => {
|
|
312
|
+
const err = new Error(`Antigravity did not start: ${error.message}`);
|
|
313
|
+
if (this.rejectReady) this.rejectReady(err);
|
|
314
|
+
this.fail(err);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
child.on('close', code => {
|
|
318
|
+
const msg = stderr.trim() || `Antigravity exited (${code})`;
|
|
319
|
+
if (!this.closed && this.child === child) {
|
|
320
|
+
const error = new Error(msg);
|
|
321
|
+
if (this.rejectReady) this.rejectReady(error);
|
|
322
|
+
if (this.activeTurn) {
|
|
323
|
+
this.endTurn(this.activeTurn, 'failed', msg);
|
|
324
|
+
}
|
|
325
|
+
this.fail(error);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const lines = createInterface({ input: child.stdout, terminal: false });
|
|
330
|
+
this.lines = lines;
|
|
331
|
+
lines.on('line', line => this.receiveLine(line));
|
|
332
|
+
|
|
333
|
+
const startupTimeout = setTimeout(() => {
|
|
334
|
+
if (this.rejectReady) {
|
|
335
|
+
const error = new Error('Antigravity did not initialize within 30 seconds');
|
|
336
|
+
this.rejectReady(error);
|
|
337
|
+
this.fail(error);
|
|
338
|
+
}
|
|
339
|
+
}, 30_000);
|
|
340
|
+
|
|
341
|
+
return this.readyPromise.finally(() => clearTimeout(startupTimeout));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
private receiveLine(line: string): void {
|
|
345
|
+
if (!line.trim() || this.closed) return;
|
|
346
|
+
if (line.length > 10 * 1024 * 1024) {
|
|
347
|
+
this.fail(new Error('Antigravity frame exceeds 10MB limit'));
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let frame: any;
|
|
352
|
+
try {
|
|
353
|
+
frame = JSON.parse(line);
|
|
354
|
+
} catch {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (frame.event === 'init') {
|
|
359
|
+
this.conversationId = frame.conversation_id || frame.init?.conversation_id || randomUUID();
|
|
360
|
+
if (this.resolveReady) {
|
|
361
|
+
this.resolveReady();
|
|
362
|
+
this.resolveReady = undefined;
|
|
363
|
+
this.rejectReady = undefined;
|
|
364
|
+
}
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (frame.event === 'step_update') {
|
|
369
|
+
this.handleStepUpdate(frame.step_update);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (frame.event === 'result') {
|
|
374
|
+
this.handleResult(frame.result);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private handleStepUpdate(step: any): void {
|
|
380
|
+
if (!step || !this.activeTurn) return;
|
|
381
|
+
|
|
382
|
+
if (step.step_type === 'agent_response') {
|
|
383
|
+
const itemId = `agy-msg-${this.activeTurn}-${step.step_index ?? this.messages}`;
|
|
384
|
+
const delta = typeof step.text_delta === 'string' ? step.text_delta : '';
|
|
385
|
+
if (delta) {
|
|
386
|
+
this.messageText.set(itemId, (this.messageText.get(itemId) ?? '') + delta);
|
|
387
|
+
this.emit('item/agentMessage/delta', {
|
|
388
|
+
threadId: this.conversationId,
|
|
389
|
+
itemId,
|
|
390
|
+
delta,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (step.state === 'DONE' && this.messageText.has(itemId)) {
|
|
395
|
+
this.emit('item/completed', {
|
|
396
|
+
threadId: this.conversationId,
|
|
397
|
+
item: { type: 'agentMessage', id: itemId, text: this.messageText.get(itemId) },
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (step.state === 'DONE') { this.messageText.delete(itemId); this.messages++; }
|
|
402
|
+
|
|
403
|
+
if (step.usage && typeof step.usage === 'object') {
|
|
404
|
+
const inTokens = step.usage.input_tokens || 0;
|
|
405
|
+
const outTokens = step.usage.output_tokens || 0;
|
|
406
|
+
const cachedTokens = step.usage.cache_read_tokens || 0;
|
|
407
|
+
const total = inTokens + outTokens;
|
|
408
|
+
const last = { totalTokens: total, inputTokens: inTokens, outputTokens: outTokens };
|
|
409
|
+
this.emit('thread/tokenUsage/updated', {
|
|
410
|
+
threadId: this.conversationId,
|
|
411
|
+
turnId: this.activeTurn,
|
|
412
|
+
tokenUsage: {
|
|
413
|
+
last,
|
|
414
|
+
total: last,
|
|
415
|
+
...(cachedTokens ? { cachedInputTokens: cachedTokens } : {}),
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (step.step_type === 'tool') {
|
|
423
|
+
const toolName = step.tool_name || 'tool';
|
|
424
|
+
const callId = `agy-tool-${this.activeTurn}-${step.step_index ?? ++this.messages}`;
|
|
425
|
+
|
|
426
|
+
// The command stays the tool's name, as the journal reads it; the typed detail rides along.
|
|
427
|
+
const detail = antigravityToolDetail(step.tool_info ?? { name: toolName });
|
|
428
|
+
const typed = { tool: typeof step.tool_info?.name === 'string' && step.tool_info.name ? step.tool_info.name : toolName, detail };
|
|
429
|
+
if (step.state === 'ACTIVE') {
|
|
430
|
+
this.emit('item/started', {
|
|
431
|
+
threadId: this.conversationId,
|
|
432
|
+
item: {
|
|
433
|
+
type: 'commandExecution',
|
|
434
|
+
id: callId,
|
|
435
|
+
command: toolName,
|
|
436
|
+
cwd: this.cwd,
|
|
437
|
+
status: 'inProgress',
|
|
438
|
+
aggregatedOutput: '',
|
|
439
|
+
...typed,
|
|
440
|
+
},
|
|
441
|
+
});
|
|
442
|
+
} else if (step.state === 'DONE' || step.state === 'ERROR') {
|
|
443
|
+
// agy 1.4.10 sometimes marks a tool step it denied DONE, with no error and nothing run. ALP's own
|
|
444
|
+
// record is authoritative: read-only denies every edit and every command outside its allowlist,
|
|
445
|
+
// so never report one as done, whatever agy says.
|
|
446
|
+
if (step.state === 'DONE' && modeOf(this.config?.sandbox) === 'read-only' && this.readOnlyRefuses(toolName, step.tool_info)) {
|
|
447
|
+
step = { ...step, state: 'ERROR', tool_info: { ...step.tool_info, output: undefined, error: { message: `Antigravity denied ${typed.tool} in read-only mode` } } };
|
|
448
|
+
}
|
|
449
|
+
const output = typeof step.tool_info?.output === 'string'
|
|
450
|
+
? step.tool_info.output
|
|
451
|
+
: step.tool_info?.error?.message || '';
|
|
452
|
+
this.emit('item/completed', {
|
|
453
|
+
threadId: this.conversationId,
|
|
454
|
+
item: {
|
|
455
|
+
type: 'commandExecution',
|
|
456
|
+
id: callId,
|
|
457
|
+
command: toolName,
|
|
458
|
+
cwd: this.cwd,
|
|
459
|
+
status: step.state === 'DONE' ? 'completed' : 'failed',
|
|
460
|
+
exitCode: step.state === 'DONE' ? 0 : 1,
|
|
461
|
+
aggregatedOutput: output,
|
|
462
|
+
...typed,
|
|
463
|
+
...(step.state === 'ERROR' ? { error: step.tool_info?.error?.message } : {}),
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Whether read-only refuses this tool step: an edit, or a terminal command outside the allowlist. */
|
|
471
|
+
private readOnlyRefuses(toolName: string, info: any): boolean {
|
|
472
|
+
const name = typeof info?.name === 'string' && info.name ? info.name : toolName;
|
|
473
|
+
if (EDIT_TOOLS.has(toolName) || EDIT_TOOLS.has(name)) return true;
|
|
474
|
+
if (COMMAND_TOOLS.has(toolName) || COMMAND_TOOLS.has(name)) return !antigravityReadOnlyCommandAllowed(info?.parameters?.CommandLine);
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
private handleResult(result: any): void {
|
|
479
|
+
if (!this.activeTurn) return;
|
|
480
|
+
const turnId = this.activeTurn;
|
|
481
|
+
|
|
482
|
+
if (!result || typeof result !== 'object') { this.endTurn(turnId, 'failed', 'Invalid Antigravity result'); return; }
|
|
483
|
+
if (result.status === 'ERROR') {
|
|
484
|
+
const isInterrupted = result.error === 'interrupted';
|
|
485
|
+
this.endTurn(turnId, isInterrupted ? 'interrupted' : 'failed', result.error);
|
|
486
|
+
} else {
|
|
487
|
+
// Headless agy cannot ask, so it denies a tool the mode does not allow and ends the turn, often with no answer: say so.
|
|
488
|
+
const denied = Array.isArray(result.denied_actions) ? result.denied_actions.map((action: any) => String(action?.display_name || action?.action || '')).filter(Boolean) : [];
|
|
489
|
+
if (denied.length) {
|
|
490
|
+
const text = `Antigravity denied ${[...new Set(denied)].join(', ')} in ${modeOf(this.config?.sandbox)} mode and ended the turn.`;
|
|
491
|
+
this.emit('item/completed', { threadId: this.conversationId, item: { type: 'agentMessage', id: `agy-denied-${turnId}`, text } });
|
|
492
|
+
}
|
|
493
|
+
this.endTurn(turnId, 'completed');
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private endTurn(turnId: string, status: 'completed' | 'interrupted' | 'failed', error?: string): void {
|
|
498
|
+
if (this.activeTurn !== turnId) return;
|
|
499
|
+
this.activeTurn = undefined;
|
|
500
|
+
|
|
501
|
+
setImmediate(() => {
|
|
502
|
+
this.emit('turn/completed', {
|
|
503
|
+
threadId: this.conversationId,
|
|
504
|
+
turn: {
|
|
505
|
+
id: turnId,
|
|
506
|
+
status,
|
|
507
|
+
...(status === 'failed' ? { error: { message: error || 'Antigravity turn failed' } } : {}),
|
|
508
|
+
},
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
private async openBridge(): Promise<void> {
|
|
515
|
+
if (this.bridge) return;
|
|
516
|
+
|
|
517
|
+
const token = randomBytes(16).toString('hex');
|
|
518
|
+
const name = `alp-agy-${randomBytes(6).toString('hex')}`;
|
|
519
|
+
const socket = process.platform === 'win32'
|
|
520
|
+
? `\\\\.\\pipe\\${name}`
|
|
521
|
+
: path.join(os.tmpdir(), `${name}.sock`);
|
|
522
|
+
const connections = new Set<net.Socket>();
|
|
523
|
+
|
|
524
|
+
const server = net.createServer(connection => {
|
|
525
|
+
connections.add(connection);
|
|
526
|
+
connection.once('close', () => connections.delete(connection));
|
|
527
|
+
this.serveBridge(connection, token);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
await new Promise<void>((resolve, reject) => {
|
|
531
|
+
server.once('error', reject);
|
|
532
|
+
server.listen(socket, () => resolve());
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
this.bridge = { server, socket, token, connections };
|
|
536
|
+
|
|
537
|
+
// Register into ~/.gemini/config/mcp_config.json
|
|
538
|
+
await this.registerMcpServer();
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** One stable launcher; each child inherits its own socket/token from agy.
|
|
542
|
+
* No session endpoint or credential is written to the global configuration.
|
|
543
|
+
* Leave the launcher installed: removing it would break concurrent sessions.
|
|
544
|
+
*/
|
|
545
|
+
private async registerMcpServer(): Promise<void> {
|
|
546
|
+
const configFile = this.env.ALP_AGY_MCP_CONFIG ?? path.join(os.homedir(), '.gemini', 'config', 'mcp_config.json');
|
|
547
|
+
await mkdir(path.dirname(configFile), { recursive: true });
|
|
548
|
+
const lockFile = `${configFile}.alp-lock`;
|
|
549
|
+
let lock;
|
|
550
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
551
|
+
try { lock = await open(lockFile, 'wx', 0o600); break; }
|
|
552
|
+
catch (error: any) {
|
|
553
|
+
if (error.code !== 'EEXIST') throw error;
|
|
554
|
+
await new Promise(resolve => setTimeout(resolve, 25));
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (!lock) throw new Error(`Antigravity MCP configuration is locked: ${lockFile}`);
|
|
558
|
+
const temporary = `${configFile}.${randomUUID()}.tmp`;
|
|
559
|
+
try {
|
|
560
|
+
let text = '{}';
|
|
561
|
+
try { text = await readFile(configFile, 'utf8'); } catch (error: any) { if (error.code !== 'ENOENT') throw error; }
|
|
562
|
+
const config = JSON.parse(text);
|
|
563
|
+
if (!config || typeof config !== 'object' || Array.isArray(config) ||
|
|
564
|
+
(config.mcpServers !== undefined && (!config.mcpServers || typeof config.mcpServers !== 'object' || Array.isArray(config.mcpServers)))) {
|
|
565
|
+
throw new Error('Invalid Antigravity MCP configuration; existing file was preserved');
|
|
566
|
+
}
|
|
567
|
+
config.mcpServers ??= {};
|
|
568
|
+
const existing = config.mcpServers.alp;
|
|
569
|
+
if (existing && !isBridgeArgs('antigravity', existing.args, existing.command)) {
|
|
570
|
+
throw new Error('Antigravity already has an MCP server named alp; rename it before using ALP');
|
|
571
|
+
}
|
|
572
|
+
const launcher = { ...bridgeCommand('antigravity'), ...(process.versions.electron ? { env: { ELECTRON_RUN_AS_NODE: '1' } } : {}) };
|
|
573
|
+
if (JSON.stringify(existing) === JSON.stringify(launcher)) return;
|
|
574
|
+
config.mcpServers.alp = launcher;
|
|
575
|
+
await writeFile(temporary, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
|
|
576
|
+
await rename(temporary, configFile);
|
|
577
|
+
} finally {
|
|
578
|
+
await unlink(temporary).catch(() => {});
|
|
579
|
+
await lock.close();
|
|
580
|
+
await unlink(lockFile);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
private serveBridge(connection: net.Socket, token: string): void {
|
|
585
|
+
let trusted = false;
|
|
586
|
+
connection.setEncoding('utf8');
|
|
587
|
+
connection.on('error', () => {});
|
|
588
|
+
const reply = (value: unknown) => {
|
|
589
|
+
if (!connection.destroyed) connection.write(`${JSON.stringify(value)}\n`);
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
createInterface({ input: connection }).on('line', async line => {
|
|
593
|
+
let message: any;
|
|
594
|
+
try { message = JSON.parse(line); } catch { connection.destroy(); return; }
|
|
595
|
+
if (!trusted) {
|
|
596
|
+
if (message.token !== token) { connection.destroy(); return; }
|
|
597
|
+
trusted = true;
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (message.op === 'list') {
|
|
601
|
+
reply({
|
|
602
|
+
id: message.id,
|
|
603
|
+
tools: [...this.tools.map(tool => ({
|
|
604
|
+
name: tool.name,
|
|
605
|
+
description: tool.description,
|
|
606
|
+
inputSchema: tool.inputSchema ?? { type: 'object' },
|
|
607
|
+
})), ...(this.external?.tools ?? [])],
|
|
608
|
+
});
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (message.op === 'call') {
|
|
612
|
+
reply({
|
|
613
|
+
id: message.id,
|
|
614
|
+
result: await this.toolCall(String(message.name), message.arguments ?? {}),
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
private async toolCall(tool: string, args: Record<string, unknown>): Promise<any> {
|
|
621
|
+
if (this.activeTurn && this.external?.has(tool)) {
|
|
622
|
+
const callId = `agy-mcp-tool-${randomUUID()}`;
|
|
623
|
+
const item = { type: 'dynamicToolCall', id: callId, callId, tool, arguments: args };
|
|
624
|
+
this.emit('item/started', { threadId: this.conversationId, item: { ...item, status: 'inProgress' } });
|
|
625
|
+
const result = await this.external.call(tool, args).catch(() => ({ content: [{ type: 'text', text: `External MCP tool '${tool}' failed` }], isError: true }));
|
|
626
|
+
const ok = result.isError !== true;
|
|
627
|
+
this.emit('item/completed', {
|
|
628
|
+
threadId: this.conversationId,
|
|
629
|
+
item: { ...item, status: ok ? 'completed' : 'failed', success: ok, contentItems: (Array.isArray(result.content) ? result.content : []).map(part => ({ type: 'inputText', text: part.type === 'text' ? part.text : JSON.stringify(part) })) },
|
|
630
|
+
});
|
|
631
|
+
return result;
|
|
632
|
+
}
|
|
633
|
+
if (!this.requestHandler || !this.activeTurn || !this.tools.some(t => t.name === tool)) {
|
|
634
|
+
return {
|
|
635
|
+
content: [{ type: 'text', text: this.activeTurn ? `Unknown ALP tool '${tool}'` : 'ALP tools are unavailable between turns' }],
|
|
636
|
+
isError: true,
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const callId = `agy-alp-tool-${randomUUID()}`;
|
|
641
|
+
const item = { type: 'dynamicToolCall', id: callId, callId, tool, arguments: args };
|
|
642
|
+
this.emit('item/started', { threadId: this.conversationId, item: { ...item, status: 'inProgress' } });
|
|
643
|
+
|
|
644
|
+
const result: any = await this.requestHandler('item/tool/call', {
|
|
645
|
+
threadId: this.conversationId,
|
|
646
|
+
turnId: this.activeTurn,
|
|
647
|
+
callId,
|
|
648
|
+
namespace: null,
|
|
649
|
+
tool,
|
|
650
|
+
arguments: args,
|
|
651
|
+
}).catch(error => ({
|
|
652
|
+
success: false,
|
|
653
|
+
contentItems: [{ type: 'inputText', text: error instanceof Error ? error.message : String(error) }],
|
|
654
|
+
}));
|
|
655
|
+
|
|
656
|
+
const ok = result?.success !== false;
|
|
657
|
+
this.emit('item/completed', {
|
|
658
|
+
threadId: this.conversationId,
|
|
659
|
+
item: { ...item, status: ok ? 'completed' : 'failed', success: ok, contentItems: result?.contentItems ?? [] },
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
return {
|
|
663
|
+
content: (result?.contentItems ?? []).map((part: any) => ({ type: 'text', text: part.text ?? JSON.stringify(part) })),
|
|
664
|
+
isError: !ok,
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private emit(method: string, params: any): void {
|
|
669
|
+
for (const listener of this.listeners) listener(method, params);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** agy exits on SIGINT. Ignore that expected exit; the next prompt resumes its conversation in a fresh process. */
|
|
673
|
+
private async stopProcess(): Promise<void> {
|
|
674
|
+
const child = this.child;
|
|
675
|
+
if (!child) return;
|
|
676
|
+
this.child = undefined;
|
|
677
|
+
this.lines?.close();
|
|
678
|
+
const kill = (signal: NodeJS.Signals) => {
|
|
679
|
+
if (process.platform !== 'win32' && child.pid) {
|
|
680
|
+
try { process.kill(-child.pid, signal); } catch { child.kill(signal); }
|
|
681
|
+
} else child.kill(signal);
|
|
682
|
+
};
|
|
683
|
+
kill('SIGINT');
|
|
684
|
+
const timer = setTimeout(() => kill('SIGKILL'), 1500);
|
|
685
|
+
try { await this.exit; } finally { clearTimeout(timer); }
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
private fail(error: Error): void {
|
|
689
|
+
if (this.closed) return;
|
|
690
|
+
for (const listener of this.failures) listener(error);
|
|
691
|
+
void this.close();
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
async close(): Promise<void> {
|
|
695
|
+
if (this.closed) return this.exit;
|
|
696
|
+
this.closed = true;
|
|
697
|
+
|
|
698
|
+
this.listeners.clear();
|
|
699
|
+
this.failures.clear();
|
|
700
|
+
this.startupAbort.abort();
|
|
701
|
+
await this.externalStarting?.then(external => external.close(), () => {});
|
|
702
|
+
await this.external?.close();
|
|
703
|
+
|
|
704
|
+
if (this.bridge) {
|
|
705
|
+
this.bridge.server.close();
|
|
706
|
+
for (const connection of this.bridge.connections) connection.destroy();
|
|
707
|
+
if (process.platform !== 'win32') {
|
|
708
|
+
try { rmSync(this.bridge.socket, { force: true }); } catch {}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (this.lines) {
|
|
713
|
+
this.lines.close();
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const child = this.child;
|
|
717
|
+
if (!child) return;
|
|
718
|
+
|
|
719
|
+
try { child.stdin.end(); } catch {}
|
|
720
|
+
|
|
721
|
+
const timer = setTimeout(() => {
|
|
722
|
+
if (process.platform === 'win32' && child.pid) {
|
|
723
|
+
execFile('taskkill.exe', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true }, () => {});
|
|
724
|
+
} else if (child.pid) {
|
|
725
|
+
try { process.kill(-child.pid, 'SIGKILL'); } catch { child.kill('SIGKILL'); }
|
|
726
|
+
}
|
|
727
|
+
}, 1500);
|
|
728
|
+
|
|
729
|
+
let deadline: NodeJS.Timeout | undefined;
|
|
730
|
+
try {
|
|
731
|
+
await Promise.race([
|
|
732
|
+
this.exit,
|
|
733
|
+
new Promise<void>(resolve => { deadline = setTimeout(resolve, 4000); }),
|
|
734
|
+
]);
|
|
735
|
+
} finally {
|
|
736
|
+
clearTimeout(timer);
|
|
737
|
+
clearTimeout(deadline);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|