@tencent-adp/portal-cli 0.0.4 → 0.0.6
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/dist/acp/agent.d.ts +34 -0
- package/dist/acp/agent.js +111 -0
- package/dist/acp/server.js +14 -1
- package/dist/acp/session.d.ts +14 -0
- package/dist/acp/turn.d.ts +4 -0
- package/dist/acp/turn.js +123 -36
- package/dist/auth/loadAuth.js +2 -1
- package/dist/bin.js +6 -1
- package/dist/cli/ask.d.ts +6 -2
- package/dist/cli/ask.js +16 -3
- package/dist/cli/chat.js +0 -8
- package/dist/dev.d.ts +3 -0
- package/dist/dev.js +10 -0
- package/dist/portal/callerTools.d.ts +22 -65
- package/dist/portal/callerTools.js +103 -11
- package/dist/portal/chat.d.ts +2 -8
- package/dist/portal/chat.js +9 -21
- package/dist/portal/client.d.ts +0 -9
- package/dist/portal/client.js +0 -11
- package/dist/portal/sse.js +0 -30
- package/dist/portal/sseLog.d.ts +1 -0
- package/dist/portal/sseLog.js +10 -0
- package/dist/portal/types.d.ts +6 -6
- package/dist/tools/local.d.ts +35 -2
- package/dist/tools/local.js +148 -61
- package/dist/tools/tasks.d.ts +47 -0
- package/dist/tools/tasks.js +127 -0
- package/package.json +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ChildProcess } from 'node:child_process';
|
|
2
|
+
export interface TaskSnapshot {
|
|
3
|
+
taskId: string;
|
|
4
|
+
running: boolean;
|
|
5
|
+
exitCode: number | null;
|
|
6
|
+
/** Output the caller has not seen yet. */
|
|
7
|
+
output: string;
|
|
8
|
+
}
|
|
9
|
+
interface Task {
|
|
10
|
+
id: string;
|
|
11
|
+
child: ChildProcess;
|
|
12
|
+
stdout: string;
|
|
13
|
+
stderr: string;
|
|
14
|
+
/** How much of stdout/stderr the caller has already been given. */
|
|
15
|
+
stdoutCursor: number;
|
|
16
|
+
stderrCursor: number;
|
|
17
|
+
running: boolean;
|
|
18
|
+
exitCode: number | null;
|
|
19
|
+
/** One-shot callers blocked in `wait()`, resolved with the exit. */
|
|
20
|
+
waiters: Array<(snapshot: TaskSnapshot) => void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Commands that outlive the wait window.
|
|
24
|
+
*
|
|
25
|
+
* A task never gets killed for taking too long: the caller is only told that it
|
|
26
|
+
* is still running, and can read its output later. Killing stays the caller's
|
|
27
|
+
* decision.
|
|
28
|
+
*/
|
|
29
|
+
export declare class BackgroundTasks {
|
|
30
|
+
private readonly tasks;
|
|
31
|
+
private nextId;
|
|
32
|
+
start(cwd: string, command: string, onExit?: (snapshot: TaskSnapshot) => void): Task;
|
|
33
|
+
/** Reads what is new since the last call. Reading does not consume the task. */
|
|
34
|
+
snapshot(taskId: string): TaskSnapshot | null;
|
|
35
|
+
/**
|
|
36
|
+
* Waits up to `waitMs` for the task to exit, then reports what is new.
|
|
37
|
+
*
|
|
38
|
+
* This is what makes polling cheap: the caller blocks for one window instead
|
|
39
|
+
* of sleeping between reads, and gets the result the moment it exists. An
|
|
40
|
+
* abort (the user stopped the turn) resolves immediately.
|
|
41
|
+
*/
|
|
42
|
+
wait(taskId: string, waitMs: number, signal?: AbortSignal): Promise<TaskSnapshot | null>;
|
|
43
|
+
stop(taskId: string): boolean;
|
|
44
|
+
get(taskId: string): Task | undefined;
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=tasks.d.ts.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
/** How much of a stream to keep. Long-running commands can print a lot. */
|
|
3
|
+
const OUTPUT_LIMIT = 1_000_000;
|
|
4
|
+
/**
|
|
5
|
+
* Commands that outlive the wait window.
|
|
6
|
+
*
|
|
7
|
+
* A task never gets killed for taking too long: the caller is only told that it
|
|
8
|
+
* is still running, and can read its output later. Killing stays the caller's
|
|
9
|
+
* decision.
|
|
10
|
+
*/
|
|
11
|
+
export class BackgroundTasks {
|
|
12
|
+
tasks = new Map();
|
|
13
|
+
nextId = 1;
|
|
14
|
+
start(cwd, command, onExit) {
|
|
15
|
+
const id = `task_${this.nextId++}`;
|
|
16
|
+
// detached: the shell becomes its own process group, so the whole tree can
|
|
17
|
+
// be stopped as one unit and outlives the tool call that started it.
|
|
18
|
+
const child = spawn(command, { cwd, shell: true, detached: true });
|
|
19
|
+
const task = {
|
|
20
|
+
id,
|
|
21
|
+
child,
|
|
22
|
+
stdout: '',
|
|
23
|
+
stderr: '',
|
|
24
|
+
stdoutCursor: 0,
|
|
25
|
+
stderrCursor: 0,
|
|
26
|
+
running: true,
|
|
27
|
+
exitCode: null,
|
|
28
|
+
waiters: [],
|
|
29
|
+
};
|
|
30
|
+
this.tasks.set(id, task);
|
|
31
|
+
child.stdout?.setEncoding('utf8');
|
|
32
|
+
child.stderr?.setEncoding('utf8');
|
|
33
|
+
child.stdout?.on('data', (chunk) => { task.stdout = append(task.stdout, chunk); });
|
|
34
|
+
child.stderr?.on('data', (chunk) => { task.stderr = append(task.stderr, chunk); });
|
|
35
|
+
const settle = (code) => {
|
|
36
|
+
if (!task.running)
|
|
37
|
+
return;
|
|
38
|
+
task.running = false;
|
|
39
|
+
task.exitCode = code;
|
|
40
|
+
const waiters = task.waiters.splice(0);
|
|
41
|
+
// Nobody wants the exit: leave the bytes unread for the next call.
|
|
42
|
+
if (waiters.length === 0 && !onExit)
|
|
43
|
+
return;
|
|
44
|
+
// Read the exit exactly once: the bytes may go to a caller that was
|
|
45
|
+
// waiting for them, or to the exit notice, never to both. Incremental
|
|
46
|
+
// like every other read, so what the caller was already shown stays out.
|
|
47
|
+
const exitSnapshot = this.snapshot(id);
|
|
48
|
+
for (const waiter of waiters)
|
|
49
|
+
waiter(exitSnapshot);
|
|
50
|
+
// Nobody was waiting: the exit has to reach the model as a turn of its
|
|
51
|
+
// own. A call that saw the exit has already reported it.
|
|
52
|
+
if (waiters.length === 0)
|
|
53
|
+
onExit?.(exitSnapshot);
|
|
54
|
+
};
|
|
55
|
+
child.on('error', () => settle(null));
|
|
56
|
+
child.on('close', (code) => settle(code));
|
|
57
|
+
return task;
|
|
58
|
+
}
|
|
59
|
+
/** Reads what is new since the last call. Reading does not consume the task. */
|
|
60
|
+
snapshot(taskId) {
|
|
61
|
+
const task = this.tasks.get(taskId);
|
|
62
|
+
if (!task)
|
|
63
|
+
return null;
|
|
64
|
+
const stdout = task.stdout.slice(task.stdoutCursor);
|
|
65
|
+
const stderr = task.stderr.slice(task.stderrCursor);
|
|
66
|
+
task.stdoutCursor = task.stdout.length;
|
|
67
|
+
task.stderrCursor = task.stderr.length;
|
|
68
|
+
const parts = [stdout.trimEnd(), stderr.trimEnd()].filter((part) => part.length > 0);
|
|
69
|
+
return { taskId: task.id, running: task.running, exitCode: task.exitCode, output: parts.join('\n') };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Waits up to `waitMs` for the task to exit, then reports what is new.
|
|
73
|
+
*
|
|
74
|
+
* This is what makes polling cheap: the caller blocks for one window instead
|
|
75
|
+
* of sleeping between reads, and gets the result the moment it exists. An
|
|
76
|
+
* abort (the user stopped the turn) resolves immediately.
|
|
77
|
+
*/
|
|
78
|
+
wait(taskId, waitMs, signal) {
|
|
79
|
+
const task = this.tasks.get(taskId);
|
|
80
|
+
if (!task)
|
|
81
|
+
return Promise.resolve(null);
|
|
82
|
+
if (!task.running || signal?.aborted)
|
|
83
|
+
return Promise.resolve(this.snapshot(taskId));
|
|
84
|
+
return new Promise((resolve) => {
|
|
85
|
+
let settled = false;
|
|
86
|
+
const finish = (snapshot) => {
|
|
87
|
+
if (settled)
|
|
88
|
+
return;
|
|
89
|
+
settled = true;
|
|
90
|
+
clearTimeout(timer);
|
|
91
|
+
signal?.removeEventListener('abort', onAbort);
|
|
92
|
+
const index = task.waiters.indexOf(waiter);
|
|
93
|
+
if (index >= 0)
|
|
94
|
+
task.waiters.splice(index, 1);
|
|
95
|
+
resolve(snapshot);
|
|
96
|
+
};
|
|
97
|
+
const waiter = (snapshot) => finish(snapshot);
|
|
98
|
+
const onAbort = () => finish(this.snapshot(taskId));
|
|
99
|
+
const timer = setTimeout(() => finish(this.snapshot(taskId)), Math.max(0, waitMs));
|
|
100
|
+
task.waiters.push(waiter);
|
|
101
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
stop(taskId) {
|
|
105
|
+
const task = this.tasks.get(taskId);
|
|
106
|
+
if (!task || !task.running)
|
|
107
|
+
return false;
|
|
108
|
+
if (task.child.pid) {
|
|
109
|
+
try {
|
|
110
|
+
process.kill(-task.child.pid, 'SIGKILL');
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error.code !== 'ESRCH')
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
get(taskId) {
|
|
120
|
+
return this.tasks.get(taskId);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function append(current, chunk) {
|
|
124
|
+
const next = current + chunk;
|
|
125
|
+
return next.length > OUTPUT_LIMIT ? next.slice(next.length - OUTPUT_LIMIT) : next;
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=tasks.js.map
|