@celljs/ta-harness 4.8.0 → 4.9.0
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/README.en.md +1 -1
- package/README.md +1 -1
- package/esm/node/index.d.ts +1 -0
- package/esm/node/index.js +1 -0
- package/esm/node/package-archive.d.ts +38 -0
- package/esm/node/package-archive.js +258 -0
- package/esm/node/serve-state.d.ts +8 -0
- package/esm/node/serve-state.js +36 -0
- package/esm/node/ta-cli.d.ts +2 -0
- package/esm/node/ta-cli.js +45 -4
- package/esm/node/ta-packages.d.ts +1 -0
- package/esm/node/ta-packages.js +15 -1
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.js +1 -0
- package/lib/node/package-archive.d.ts +39 -0
- package/lib/node/package-archive.js +261 -0
- package/lib/node/serve-state.d.ts +8 -0
- package/lib/node/serve-state.js +36 -0
- package/lib/node/ta-cli.d.ts +2 -0
- package/lib/node/ta-cli.js +45 -4
- package/lib/node/ta-packages.d.ts +1 -0
- package/lib/node/ta-packages.js +15 -1
- package/package.json +6 -5
- package/esm/node/brand-extension.js.map +0 -1
- package/esm/node/bug-report.js.map +0 -1
- package/esm/node/dino-logo.js.map +0 -1
- package/esm/node/exec-extension.js.map +0 -1
- package/esm/node/exec-mcp-transport.js.map +0 -1
- package/esm/node/exec-operations.js.map +0 -1
- package/esm/node/extensions.js.map +0 -1
- package/esm/node/glob-util.js.map +0 -1
- package/esm/node/index.js.map +0 -1
- package/esm/node/pi-types.js.map +0 -1
- package/esm/node/remote-context-files.js.map +0 -1
- package/esm/node/serve-state.js.map +0 -1
- package/esm/node/session-lock.js.map +0 -1
- package/esm/node/ta-brand.js.map +0 -1
- package/esm/node/ta-cli.js.map +0 -1
- package/esm/node/ta-header.js.map +0 -1
- package/esm/node/ta-packages.js.map +0 -1
- package/lib/node/brand-extension.d.ts.map +0 -1
- package/lib/node/brand-extension.js.map +0 -1
- package/lib/node/brand-extension.spec.d.ts +0 -2
- package/lib/node/brand-extension.spec.d.ts.map +0 -1
- package/lib/node/brand-extension.spec.js +0 -57
- package/lib/node/brand-extension.spec.js.map +0 -1
- package/lib/node/bug-report.d.ts.map +0 -1
- package/lib/node/bug-report.js.map +0 -1
- package/lib/node/bug-report.spec.d.ts +0 -2
- package/lib/node/bug-report.spec.d.ts.map +0 -1
- package/lib/node/bug-report.spec.js +0 -11
- package/lib/node/bug-report.spec.js.map +0 -1
- package/lib/node/dino-logo.d.ts.map +0 -1
- package/lib/node/dino-logo.js.map +0 -1
- package/lib/node/exec-extension.d.ts.map +0 -1
- package/lib/node/exec-extension.js.map +0 -1
- package/lib/node/exec-mcp-transport.d.ts.map +0 -1
- package/lib/node/exec-mcp-transport.js.map +0 -1
- package/lib/node/exec-operations.d.ts.map +0 -1
- package/lib/node/exec-operations.js.map +0 -1
- package/lib/node/exec-operations.spec.d.ts +0 -2
- package/lib/node/exec-operations.spec.d.ts.map +0 -1
- package/lib/node/exec-operations.spec.js +0 -77
- package/lib/node/exec-operations.spec.js.map +0 -1
- package/lib/node/extensions.d.ts.map +0 -1
- package/lib/node/extensions.js.map +0 -1
- package/lib/node/extensions.spec.d.ts +0 -2
- package/lib/node/extensions.spec.d.ts.map +0 -1
- package/lib/node/extensions.spec.js +0 -94
- package/lib/node/extensions.spec.js.map +0 -1
- package/lib/node/glob-util.d.ts.map +0 -1
- package/lib/node/glob-util.js.map +0 -1
- package/lib/node/glob-util.spec.d.ts +0 -2
- package/lib/node/glob-util.spec.d.ts.map +0 -1
- package/lib/node/glob-util.spec.js +0 -27
- package/lib/node/glob-util.spec.js.map +0 -1
- package/lib/node/index.d.ts.map +0 -1
- package/lib/node/index.js.map +0 -1
- package/lib/node/pi-types.d.ts.map +0 -1
- package/lib/node/pi-types.js.map +0 -1
- package/lib/node/remote-context-files.d.ts.map +0 -1
- package/lib/node/remote-context-files.js.map +0 -1
- package/lib/node/remote-context-files.spec.d.ts +0 -2
- package/lib/node/remote-context-files.spec.d.ts.map +0 -1
- package/lib/node/remote-context-files.spec.js +0 -37
- package/lib/node/remote-context-files.spec.js.map +0 -1
- package/lib/node/serve-state.d.ts.map +0 -1
- package/lib/node/serve-state.js.map +0 -1
- package/lib/node/serve-state.spec.d.ts +0 -2
- package/lib/node/serve-state.spec.d.ts.map +0 -1
- package/lib/node/serve-state.spec.js +0 -35
- package/lib/node/serve-state.spec.js.map +0 -1
- package/lib/node/session-lock.d.ts.map +0 -1
- package/lib/node/session-lock.js.map +0 -1
- package/lib/node/session-lock.spec.d.ts +0 -2
- package/lib/node/session-lock.spec.d.ts.map +0 -1
- package/lib/node/session-lock.spec.js +0 -53
- package/lib/node/session-lock.spec.js.map +0 -1
- package/lib/node/ta-brand.d.ts.map +0 -1
- package/lib/node/ta-brand.js.map +0 -1
- package/lib/node/ta-brand.spec.d.ts +0 -2
- package/lib/node/ta-brand.spec.d.ts.map +0 -1
- package/lib/node/ta-brand.spec.js +0 -43
- package/lib/node/ta-brand.spec.js.map +0 -1
- package/lib/node/ta-cli.d.ts.map +0 -1
- package/lib/node/ta-cli.js.map +0 -1
- package/lib/node/ta-header.d.ts.map +0 -1
- package/lib/node/ta-header.js.map +0 -1
- package/lib/node/ta-header.spec.d.ts +0 -2
- package/lib/node/ta-header.spec.d.ts.map +0 -1
- package/lib/node/ta-header.spec.js +0 -67
- package/lib/node/ta-header.spec.js.map +0 -1
- package/lib/node/ta-packages.d.ts.map +0 -1
- package/lib/node/ta-packages.js.map +0 -1
- package/lib/node/ta-packages.spec.d.ts +0 -2
- package/lib/node/ta-packages.spec.d.ts.map +0 -1
- package/lib/node/ta-packages.spec.js +0 -89
- package/lib/node/ta-packages.spec.js.map +0 -1
- package/src/node/brand-extension.spec.ts +0 -63
- package/src/node/brand-extension.ts +0 -40
- package/src/node/bug-report.spec.ts +0 -9
- package/src/node/bug-report.ts +0 -55
- package/src/node/dino-logo.ts +0 -70
- package/src/node/exec-extension.ts +0 -40
- package/src/node/exec-mcp-transport.ts +0 -89
- package/src/node/exec-operations.spec.ts +0 -84
- package/src/node/exec-operations.ts +0 -123
- package/src/node/extensions.spec.ts +0 -110
- package/src/node/extensions.ts +0 -69
- package/src/node/glob-util.spec.ts +0 -29
- package/src/node/glob-util.ts +0 -47
- package/src/node/index.ts +0 -16
- package/src/node/pi-types.ts +0 -16
- package/src/node/remote-context-files.spec.ts +0 -37
- package/src/node/remote-context-files.ts +0 -51
- package/src/node/serve-state.spec.ts +0 -36
- package/src/node/serve-state.ts +0 -51
- package/src/node/session-lock.spec.ts +0 -56
- package/src/node/session-lock.ts +0 -105
- package/src/node/ta-brand.spec.ts +0 -46
- package/src/node/ta-brand.ts +0 -51
- package/src/node/ta-cli.ts +0 -207
- package/src/node/ta-header.spec.ts +0 -74
- package/src/node/ta-header.ts +0 -192
- package/src/node/ta-packages.spec.ts +0 -95
- package/src/node/ta-packages.ts +0 -208
package/src/node/bug-report.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
|
|
3
|
-
/** What a bug report on ta says about where it happened; no keys, paths or conversation. */
|
|
4
|
-
export interface BugReportEnvironment {
|
|
5
|
-
version: string;
|
|
6
|
-
/** `<provider>/<model id>`. */
|
|
7
|
-
model?: string;
|
|
8
|
-
platform: string;
|
|
9
|
-
node: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export namespace BugReport {
|
|
13
|
-
export const ISSUES_URL = 'https://github.com/cellbang/cell/issues/new';
|
|
14
|
-
|
|
15
|
-
/** A new issue on GitHub, filled in with the description and the environment. */
|
|
16
|
-
export function issueUrl(description: string, environment: BugReportEnvironment): string {
|
|
17
|
-
const url = new URL(ISSUES_URL);
|
|
18
|
-
url.searchParams.set('title', `[Ta] ${description.trim() || 'Describe the bug'}`);
|
|
19
|
-
url.searchParams.set('labels', 'ta');
|
|
20
|
-
url.searchParams.set(
|
|
21
|
-
'body',
|
|
22
|
-
[
|
|
23
|
-
'## What happened',
|
|
24
|
-
'',
|
|
25
|
-
description.trim() || '<!-- What did you do, what did you expect, what happened instead? -->',
|
|
26
|
-
'',
|
|
27
|
-
'## Environment',
|
|
28
|
-
'',
|
|
29
|
-
`- Ta ${environment.version}`,
|
|
30
|
-
`- model: ${environment.model ?? 'none'}`,
|
|
31
|
-
`- ${environment.platform}, Node.js ${environment.node}`,
|
|
32
|
-
].join('\n'),
|
|
33
|
-
);
|
|
34
|
-
return url.href;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function currentEnvironment(version: string, model?: { provider: string; id: string }): BugReportEnvironment {
|
|
38
|
-
return { version, model: model && `${model.provider}/${model.id}`, platform: `${process.platform} ${process.arch}`, node: process.versions.node };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Opens the link in the browser of this machine; nothing happens where there is none. */
|
|
42
|
-
export function open(url: string): void {
|
|
43
|
-
const [command, ...args] = process.platform === 'darwin' ? ['open'] : process.platform === 'win32' ? ['cmd', '/c', 'start', '""'] : ['xdg-open'];
|
|
44
|
-
const child = spawn(command, [...args, url], { detached: true, stdio: 'ignore' });
|
|
45
|
-
child.on('error', () => undefined);
|
|
46
|
-
child.unref();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** What `/bug` says, in the language of Ta's pages (`TA_LANGUAGE`) or the computer's. */
|
|
50
|
-
export function notice(url: string, env: NodeJS.ProcessEnv = process.env): string {
|
|
51
|
-
return /^zh/i.test(env.TA_LANGUAGE || env.LC_ALL || env.LANG || '')
|
|
52
|
-
? `一个新的 Ta 问题(只带版本、模型和平台信息)会在浏览器里打开。如果没有打开:\n${url}`
|
|
53
|
-
: `A new issue for Ta, with its version, the model and the platform and nothing else, opens in the browser. If it did not:\n${url}`;
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/node/dino-logo.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/** The terminal color depths pi renders for. */
|
|
2
|
-
export type ColorMode = '256color' | 'truecolor';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Cell's dinosaur (assets/logo.svg) as pixel art: 16 by 16 pixels, drawn with half blocks as 16
|
|
6
|
-
* columns and 8 lines. Its green stays the same across themes, as a brand color does.
|
|
7
|
-
*/
|
|
8
|
-
export namespace DinoLogo {
|
|
9
|
-
const PIXELS = [
|
|
10
|
-
'........######..',
|
|
11
|
-
'.......########.',
|
|
12
|
-
'.......##.######',
|
|
13
|
-
'.......#########',
|
|
14
|
-
'#......#########',
|
|
15
|
-
'#......####.....',
|
|
16
|
-
'##.....#######..',
|
|
17
|
-
'##....#######...',
|
|
18
|
-
'###..########...',
|
|
19
|
-
'.###########.##.',
|
|
20
|
-
'..##########..#.',
|
|
21
|
-
'...########.....',
|
|
22
|
-
'....######......',
|
|
23
|
-
'....##..##......',
|
|
24
|
-
'....##..##......',
|
|
25
|
-
'...###..###.....',
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
export const WIDTH = PIXELS[0].length;
|
|
29
|
-
export const HEIGHT = PIXELS.length / 2;
|
|
30
|
-
|
|
31
|
-
/** #10B981, and the nearest color of the 256-color palette. */
|
|
32
|
-
const GREEN: Record<ColorMode, string> = { truecolor: '\x1b[38;2;16;185;129m', '256color': '\x1b[38;5;36m' };
|
|
33
|
-
const RESET = '\x1b[39m';
|
|
34
|
-
|
|
35
|
-
export function lines(mode: ColorMode): string[] {
|
|
36
|
-
const lines: string[] = [];
|
|
37
|
-
for (let row = 0; row < PIXELS.length; row += 2) {
|
|
38
|
-
let line = '';
|
|
39
|
-
for (let column = 0; column < WIDTH; column++) {
|
|
40
|
-
line += cell(PIXELS[row][column] === '#', PIXELS[row + 1][column] === '#');
|
|
41
|
-
}
|
|
42
|
-
lines.push(paint(mode, line));
|
|
43
|
-
}
|
|
44
|
-
return lines;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Text in the green of the logo. */
|
|
48
|
-
export function paint(mode: ColorMode, text: string): string {
|
|
49
|
-
return `${GREEN[mode]}${text}${RESET}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Two pixels, one above the other, in one character. */
|
|
53
|
-
function cell(top: boolean, bottom: boolean): string {
|
|
54
|
-
if (top && bottom) {
|
|
55
|
-
return '█';
|
|
56
|
-
}
|
|
57
|
-
if (top) {
|
|
58
|
-
return '▀';
|
|
59
|
-
}
|
|
60
|
-
return bottom ? '▄' : ' ';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Apple's Terminal leaves gaps between lines and misaligns half blocks, which breaks the picture: pi
|
|
65
|
-
* shows a text logo there too.
|
|
66
|
-
*/
|
|
67
|
-
export function isSupported(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
68
|
-
return env.TERM_PROGRAM !== 'Apple_Terminal';
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { ExecEnvironment } from '@celljs/ta-exec';
|
|
2
|
-
import { ExecOperations } from './exec-operations';
|
|
3
|
-
import type { TaExtension } from './extensions';
|
|
4
|
-
import type { ExtensionAPI, PiSdk } from './pi-types';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Sends pi's built-in tools and `!` commands to an environment that is not this machine, by registering
|
|
8
|
-
* tools of the same names, as pi's documentation describes. Only for those: a local environment keeps
|
|
9
|
-
* pi's own tools, which do the same work. `localCwd` is the directory of this machine pi works in for
|
|
10
|
-
* the environment's (PiDirectories of ta-host): the system prompt names the environment's instead.
|
|
11
|
-
*/
|
|
12
|
-
export class ExecExtension implements TaExtension {
|
|
13
|
-
readonly name = 'ta:exec';
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
protected readonly environment: ExecEnvironment,
|
|
17
|
-
protected readonly sdk: PiSdk,
|
|
18
|
-
protected readonly localCwd?: string,
|
|
19
|
-
) {}
|
|
20
|
-
|
|
21
|
-
register(pi: ExtensionAPI): void {
|
|
22
|
-
const { cwd } = this.environment;
|
|
23
|
-
const operations = new ExecOperations(this.environment, this.localCwd);
|
|
24
|
-
pi.registerTool(this.sdk.createReadToolDefinition(cwd, { operations: operations.read }));
|
|
25
|
-
pi.registerTool(this.sdk.createBashToolDefinition(cwd, { operations: operations.bash }));
|
|
26
|
-
pi.registerTool(this.sdk.createEditToolDefinition(cwd, { operations: operations.edit }));
|
|
27
|
-
pi.registerTool(this.sdk.createWriteToolDefinition(cwd, { operations: operations.write }));
|
|
28
|
-
pi.registerTool(this.sdk.createLsToolDefinition(cwd, { operations: operations.ls }));
|
|
29
|
-
pi.registerTool(this.sdk.createFindToolDefinition(cwd, { operations: operations.find }));
|
|
30
|
-
pi.on('user_bash', () => ({ operations: operations.bash }));
|
|
31
|
-
// pi's grep always runs ripgrep on this machine, whatever its operations say: without the tool the
|
|
32
|
-
// model searches with rg through bash, in the environment.
|
|
33
|
-
pi.on('session_start', () => pi.setActiveTools(pi.getActiveTools().filter(name => name !== 'grep')));
|
|
34
|
-
if (this.localCwd && this.localCwd !== cwd) {
|
|
35
|
-
pi.on('before_agent_start', event => {
|
|
36
|
-
event.systemPromptOptions.cwd = cwd;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import type { ExecEnvironment, ExecProcess } from '@celljs/ta-exec';
|
|
2
|
-
import type { JsonRpcMessage, McpServerEntry, McpTransport } from './pi-types';
|
|
3
|
-
|
|
4
|
-
type Listener<T extends unknown[]> = (...args: T) => void;
|
|
5
|
-
|
|
6
|
-
/** An MCP server over stdio, started in an {@link ExecEnvironment}: one JSON message per line. */
|
|
7
|
-
export class ExecMcpTransport implements McpTransport {
|
|
8
|
-
protected process?: ExecProcess;
|
|
9
|
-
protected buffer = '';
|
|
10
|
-
protected readonly decoder = new TextDecoder();
|
|
11
|
-
protected readonly messageListeners = new Set<Listener<[JsonRpcMessage]>>();
|
|
12
|
-
protected readonly errorListeners = new Set<Listener<[Error]>>();
|
|
13
|
-
protected readonly closeListeners = new Set<Listener<[]>>();
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
protected readonly environment: ExecEnvironment,
|
|
17
|
-
protected readonly entry: McpServerEntry,
|
|
18
|
-
) {}
|
|
19
|
-
|
|
20
|
-
async start(): Promise<void> {
|
|
21
|
-
const config = this.entry.config;
|
|
22
|
-
if ('url' in config) {
|
|
23
|
-
throw new Error(`The MCP server ${this.entry.name} has a URL: it needs no process in the execution environment.`);
|
|
24
|
-
}
|
|
25
|
-
const child = this.environment.spawn(config.command, config.args ?? [], { cwd: config.cwd, env: { ...process.env, ...config.env } });
|
|
26
|
-
child.onStdout(data => this.receive(data));
|
|
27
|
-
child.onExit(() => this.closed());
|
|
28
|
-
this.process = child;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async send(message: JsonRpcMessage): Promise<void> {
|
|
32
|
-
if (!this.process) {
|
|
33
|
-
throw new Error(`The MCP server ${this.entry.name} is not running.`);
|
|
34
|
-
}
|
|
35
|
-
this.process.write(`${JSON.stringify(message)}\n`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async close(): Promise<void> {
|
|
39
|
-
this.process?.kill();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
onMessage(listener: Listener<[JsonRpcMessage]>): () => void {
|
|
43
|
-
return this.listen(this.messageListeners, listener);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
onError(listener: Listener<[Error]>): () => void {
|
|
47
|
-
return this.listen(this.errorListeners, listener);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
onClose(listener: Listener<[]>): () => void {
|
|
51
|
-
return this.listen(this.closeListeners, listener);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
protected listen<T extends unknown[]>(listeners: Set<Listener<T>>, listener: Listener<T>): () => void {
|
|
55
|
-
listeners.add(listener);
|
|
56
|
-
return () => listeners.delete(listener);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
protected emit<T extends unknown[]>(listeners: Set<Listener<T>>, ...args: T): void {
|
|
60
|
-
for (const listener of listeners) {
|
|
61
|
-
listener(...args);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
protected receive(data: Uint8Array): void {
|
|
66
|
-
const lines = (this.buffer + this.decoder.decode(data, { stream: true })).split('\n');
|
|
67
|
-
this.buffer = lines.pop() ?? '';
|
|
68
|
-
for (const line of lines) {
|
|
69
|
-
if (line.trim()) {
|
|
70
|
-
this.dispatch(line);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
protected dispatch(line: string): void {
|
|
76
|
-
try {
|
|
77
|
-
const message = JSON.parse(line) as JsonRpcMessage;
|
|
78
|
-
this.emit(this.messageListeners, message);
|
|
79
|
-
} catch (cause) {
|
|
80
|
-
const error = new Error(`The MCP server ${this.entry.name} sent something that is not JSON: ${line}`, { cause });
|
|
81
|
-
this.emit(this.errorListeners, error);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
protected closed(): void {
|
|
86
|
-
this.process = undefined;
|
|
87
|
-
this.emit(this.closeListeners);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { mkdtempSync } from 'node:fs';
|
|
3
|
-
import { tmpdir } from 'node:os';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { LocalExecEnvironment } from '@celljs/ta-exec';
|
|
6
|
-
import { ExecMcpTransport } from './exec-mcp-transport';
|
|
7
|
-
import { ExecOperations } from './exec-operations';
|
|
8
|
-
import type { JsonRpcMessage, McpServerEntry } from './pi-types';
|
|
9
|
-
|
|
10
|
-
function setup() {
|
|
11
|
-
const environment = new LocalExecEnvironment(mkdtempSync(join(tmpdir(), 'ta-ops-')));
|
|
12
|
-
return { environment, cwd: environment.cwd, operations: new ExecOperations(environment) };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
describe('ExecOperations', () => {
|
|
16
|
-
it("runs bash with pi's timeout in seconds", async () => {
|
|
17
|
-
const { operations, cwd } = setup();
|
|
18
|
-
const chunks: Buffer[] = [];
|
|
19
|
-
const result = await operations.bash.exec('echo hi', cwd, { onData: data => chunks.push(data) });
|
|
20
|
-
expect(result.exitCode).toBe(0);
|
|
21
|
-
expect(Buffer.concat(chunks).toString()).toBe('hi\n');
|
|
22
|
-
const start = Date.now();
|
|
23
|
-
await operations.bash.exec('sleep 30', cwd, { onData: () => undefined, timeout: 0.05 });
|
|
24
|
-
expect(Date.now() - start).toBeLessThan(5000);
|
|
25
|
-
// Longer than bun's 5 s, so that a slow kill fails with the time it took, not as a timeout.
|
|
26
|
-
}, 40000);
|
|
27
|
-
|
|
28
|
-
it('reads and writes files', async () => {
|
|
29
|
-
const { operations, cwd } = setup();
|
|
30
|
-
await operations.write.mkdir(join(cwd, 'a'));
|
|
31
|
-
await operations.write.writeFile(join(cwd, 'a/b.txt'), 'data');
|
|
32
|
-
expect((await operations.read.readFile(join(cwd, 'a/b.txt'))).toString()).toBe('data');
|
|
33
|
-
await operations.edit.access(join(cwd, 'a/b.txt'));
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("reports a missing file with ENOENT, as pi's tools expect", async () => {
|
|
37
|
-
const { operations, cwd } = setup();
|
|
38
|
-
await expect(operations.read.access(join(cwd, 'nope'))).rejects.toMatchObject({ code: 'ENOENT' });
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('lists directories', async () => {
|
|
42
|
-
const { operations, environment, cwd } = setup();
|
|
43
|
-
await environment.writeFile('a.txt', '');
|
|
44
|
-
expect(await operations.ls.readdir(cwd)).toEqual(['a.txt']);
|
|
45
|
-
expect((await operations.ls.stat(cwd)).isDirectory()).toBe(true);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('finds files by glob, honors ignores and stops at the limit', async () => {
|
|
49
|
-
const { operations, environment, cwd } = setup();
|
|
50
|
-
await environment.mkdir('src/gen');
|
|
51
|
-
for (const name of ['src/a.ts', 'src/b.ts', 'src/gen/c.ts', 'src/d.js']) {
|
|
52
|
-
await environment.writeFile(name, '');
|
|
53
|
-
}
|
|
54
|
-
const found = await operations.find.glob('*.ts', cwd, { ignore: ['src/gen/**'], limit: 10 });
|
|
55
|
-
expect((found as string[]).sort()).toEqual([join(cwd, 'src/a.ts'), join(cwd, 'src/b.ts')]);
|
|
56
|
-
expect(await operations.find.glob('*.ts', cwd, { ignore: [], limit: 1 })).toHaveLength(1);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe('ExecMcpTransport', () => {
|
|
61
|
-
const entry = (command: string, args: string[]): McpServerEntry => ({ name: 'test', source: 'test', config: { command, args } });
|
|
62
|
-
|
|
63
|
-
it('exchanges one JSON message per line with the server', async () => {
|
|
64
|
-
const { environment } = setup();
|
|
65
|
-
const transport = new ExecMcpTransport(environment, entry('cat', []));
|
|
66
|
-
const received = new Promise<JsonRpcMessage>(done => transport.onMessage(done));
|
|
67
|
-
const closed = new Promise<void>(done => transport.onClose(done));
|
|
68
|
-
await transport.start();
|
|
69
|
-
const message = { jsonrpc: '2.0', id: 1, method: 'ping' } as JsonRpcMessage;
|
|
70
|
-
await transport.send(message);
|
|
71
|
-
expect(await received).toEqual(message);
|
|
72
|
-
await transport.close();
|
|
73
|
-
await closed;
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('reports output that is not JSON', async () => {
|
|
77
|
-
const { environment } = setup();
|
|
78
|
-
const transport = new ExecMcpTransport(environment, entry('sh', ['-c', 'echo nonsense; sleep 5']));
|
|
79
|
-
const error = new Promise<Error>(done => transport.onError(done));
|
|
80
|
-
await transport.start();
|
|
81
|
-
expect((await error).message).toContain('nonsense');
|
|
82
|
-
await transport.close();
|
|
83
|
-
});
|
|
84
|
-
});
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { join, posix, relative, sep } from 'node:path';
|
|
2
|
-
import type { ExecEnvironment } from '@celljs/ta-exec';
|
|
3
|
-
import { GlobUtil } from './glob-util';
|
|
4
|
-
import type { BashOperations, EditOperations, FindOperations, LsOperations, ReadOperations, WriteOperations } from './pi-types';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The file and process operations of pi's built-in tools, on an {@link ExecEnvironment}. A path under
|
|
8
|
-
* `localCwd`, the directory of this machine that pi works in for the environment's (PiDirectories of
|
|
9
|
-
* ta-host), is the same path under the environment's directory: pi gives its own for `!` commands.
|
|
10
|
-
*/
|
|
11
|
-
export class ExecOperations {
|
|
12
|
-
constructor(
|
|
13
|
-
protected readonly environment: ExecEnvironment,
|
|
14
|
-
protected readonly localCwd?: string,
|
|
15
|
-
) {}
|
|
16
|
-
|
|
17
|
-
readonly bash: BashOperations = {
|
|
18
|
-
exec: (command, cwd, options) =>
|
|
19
|
-
this.environment.exec(command, {
|
|
20
|
-
cwd: this.map(cwd),
|
|
21
|
-
env: options.env,
|
|
22
|
-
signal: options.signal,
|
|
23
|
-
// pi's timeouts are in seconds.
|
|
24
|
-
timeout: options.timeout ? options.timeout * 1000 : undefined,
|
|
25
|
-
onData: data => options.onData(Buffer.from(data)),
|
|
26
|
-
}),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
readonly read: ReadOperations = {
|
|
30
|
-
readFile: path => this.readBuffer(path),
|
|
31
|
-
access: path => this.access(path),
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
readonly write: WriteOperations = {
|
|
35
|
-
writeFile: (path, content) => this.environment.writeFile(this.map(path), content),
|
|
36
|
-
mkdir: dir => this.environment.mkdir(this.map(dir)),
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
readonly edit: EditOperations = {
|
|
40
|
-
readFile: path => this.readBuffer(path),
|
|
41
|
-
writeFile: (path, content) => this.environment.writeFile(this.map(path), content),
|
|
42
|
-
access: path => this.access(path),
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
readonly ls: LsOperations = {
|
|
46
|
-
exists: path => this.exists(path),
|
|
47
|
-
stat: async path => {
|
|
48
|
-
const info = await this.environment.stat(this.map(path));
|
|
49
|
-
if (!info) {
|
|
50
|
-
throw this.notFound(path);
|
|
51
|
-
}
|
|
52
|
-
return { isDirectory: () => info.isDirectory };
|
|
53
|
-
},
|
|
54
|
-
readdir: async path => (await this.environment.readdir(this.map(path))).map(entry => entry.name),
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
readonly find: FindOperations = {
|
|
58
|
-
exists: path => this.exists(path),
|
|
59
|
-
glob: (pattern, cwd, options) => this.glob(pattern, this.map(cwd), options.ignore, options.limit),
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/** A path of the environment for one under `localCwd`; any other as it is. */
|
|
63
|
-
map(path: string): string {
|
|
64
|
-
const local = this.localCwd;
|
|
65
|
-
if (!local || (path !== local && !path.startsWith(`${local}${sep}`))) {
|
|
66
|
-
return path;
|
|
67
|
-
}
|
|
68
|
-
const rest = path.slice(local.length).split(sep).filter(Boolean);
|
|
69
|
-
return rest.length ? posix.join(this.environment.cwd, ...rest) : this.environment.cwd;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected async readBuffer(path: string): Promise<Buffer> {
|
|
73
|
-
return Buffer.from(await this.environment.readFile(this.map(path)));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
protected async access(path: string): Promise<void> {
|
|
77
|
-
if (!(await this.exists(path))) {
|
|
78
|
-
throw this.notFound(path);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
protected async exists(path: string): Promise<boolean> {
|
|
83
|
-
return (await this.environment.stat(this.map(path))) !== undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
protected notFound(path: string): NodeJS.ErrnoException {
|
|
87
|
-
return Object.assign(new Error(`ENOENT: no such file or directory, '${path}'`), { code: 'ENOENT' });
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Lists the files with `find`, the one tool every environment has, and matches them here. Stops the
|
|
92
|
-
* listing at `limit` matches, so that a huge tree costs no more than the answer needs.
|
|
93
|
-
*/
|
|
94
|
-
protected async glob(pattern: string, cwd: string, ignore: string[], limit: number): Promise<string[]> {
|
|
95
|
-
const matches = GlobUtil.matcher(pattern);
|
|
96
|
-
const ignored = ignore.map(GlobUtil.matcher);
|
|
97
|
-
const found: string[] = [];
|
|
98
|
-
const stop = new AbortController();
|
|
99
|
-
let rest = '';
|
|
100
|
-
const accept = (line: string) => {
|
|
101
|
-
const path = relative('.', line);
|
|
102
|
-
if (found.length < limit && path && matches(path) && !ignored.some(isIgnored => isIgnored(path))) {
|
|
103
|
-
found.push(join(cwd, path));
|
|
104
|
-
if (found.length === limit) {
|
|
105
|
-
stop.abort();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
await this.environment.exec('find . -type f -not -path "*/.git/*"', {
|
|
110
|
-
cwd,
|
|
111
|
-
signal: stop.signal,
|
|
112
|
-
onData: data => {
|
|
113
|
-
const lines = (rest + Buffer.from(data).toString()).split('\n');
|
|
114
|
-
rest = lines.pop() ?? '';
|
|
115
|
-
lines.forEach(accept);
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
if (rest) {
|
|
119
|
-
accept(rest);
|
|
120
|
-
}
|
|
121
|
-
return found;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { mkdtempSync } from 'node:fs';
|
|
3
|
-
import { tmpdir } from 'node:os';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { PiBundleUtil } from '@celljs/ai-coding-agent/lib/node/pi/pi-bundle-util';
|
|
6
|
-
import { LocalExecEnvironment } from '@celljs/ta-exec';
|
|
7
|
-
import { ExecExtension } from './exec-extension';
|
|
8
|
-
import { SessionLockExtension, TaExtensionUtil } from './extensions';
|
|
9
|
-
import type { ExtensionAPI, PiSdk } from './pi-types';
|
|
10
|
-
import { SessionLock } from './session-lock';
|
|
11
|
-
|
|
12
|
-
type Handler = (event: unknown, ctx: unknown) => any;
|
|
13
|
-
|
|
14
|
-
/** The part of pi's extension API these extensions use. */
|
|
15
|
-
function fakePi(activeTools: string[] = []) {
|
|
16
|
-
const handlers = new Map<string, Handler>();
|
|
17
|
-
const tools = new Map<string, any>();
|
|
18
|
-
const state = { activeTools };
|
|
19
|
-
const pi = {
|
|
20
|
-
on: (event: string, handler: Handler) => handlers.set(event, handler),
|
|
21
|
-
registerTool: (tool: { name: string }) => tools.set(tool.name, tool),
|
|
22
|
-
getActiveTools: () => state.activeTools,
|
|
23
|
-
setActiveTools: (names: string[]) => {
|
|
24
|
-
state.activeTools = names;
|
|
25
|
-
},
|
|
26
|
-
} as unknown as ExtensionAPI;
|
|
27
|
-
return { pi, handlers, tools, state };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const context = (sessionFile: string | undefined, extra: object = {}) => ({ sessionManager: { getSessionFile: () => sessionFile }, ...extra });
|
|
31
|
-
|
|
32
|
-
describe('SessionLockExtension', () => {
|
|
33
|
-
const file = () => join(mkdtempSync(join(tmpdir(), 'ta-ext-')), 'a.jsonl');
|
|
34
|
-
|
|
35
|
-
it('holds the session from its start to its shutdown', () => {
|
|
36
|
-
const lock = new SessionLock();
|
|
37
|
-
const { pi, handlers } = fakePi();
|
|
38
|
-
new SessionLockExtension(lock).register(pi);
|
|
39
|
-
const session = file();
|
|
40
|
-
const other = new SessionLock();
|
|
41
|
-
handlers.get('session_start')!({}, context(session));
|
|
42
|
-
expect(other.isHeldByOther(session)).toBe(true);
|
|
43
|
-
handlers.get('session_shutdown')!({}, context(session));
|
|
44
|
-
expect(other.isHeldByOther(session)).toBe(false);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('cancels switching to a session another owner holds', () => {
|
|
48
|
-
const { pi, handlers } = fakePi();
|
|
49
|
-
new SessionLockExtension(new SessionLock()).register(pi);
|
|
50
|
-
const session = file();
|
|
51
|
-
new SessionLock().acquire(session);
|
|
52
|
-
expect(handlers.get('session_before_switch')!({ targetSessionFile: session }, context(undefined))).toEqual({ cancel: true });
|
|
53
|
-
expect(handlers.get('session_before_switch')!({ targetSessionFile: file() }, context(undefined))).toBeUndefined();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('asks the terminal UI to end when its session is held elsewhere', () => {
|
|
57
|
-
const { pi, handlers } = fakePi();
|
|
58
|
-
new SessionLockExtension(new SessionLock()).register(pi);
|
|
59
|
-
const session = file();
|
|
60
|
-
new SessionLock().acquire(session);
|
|
61
|
-
const calls: string[] = [];
|
|
62
|
-
handlers.get('session_start')!(
|
|
63
|
-
{},
|
|
64
|
-
context(session, { mode: 'tui', ui: { notify: () => calls.push('notify') }, shutdown: () => calls.push('shutdown') }),
|
|
65
|
-
);
|
|
66
|
-
expect(calls).toEqual(['notify', 'shutdown']);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('ignores in-memory sessions', () => {
|
|
70
|
-
const { pi, handlers } = fakePi();
|
|
71
|
-
new SessionLockExtension(new SessionLock()).register(pi);
|
|
72
|
-
handlers.get('session_start')!({}, context(undefined));
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it("is named in pi's diagnostics", () => {
|
|
76
|
-
expect(TaExtensionUtil.toInline(new SessionLockExtension(new SessionLock())).name).toBe('ta:session-lock');
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe('ExecExtension', () => {
|
|
81
|
-
async function setup() {
|
|
82
|
-
const sdk: PiSdk = await import(PiBundleUtil.specifier());
|
|
83
|
-
const environment = new LocalExecEnvironment(mkdtempSync(join(tmpdir(), 'ta-exec-ext-')));
|
|
84
|
-
const fake = fakePi(['read', 'bash', 'grep']);
|
|
85
|
-
new ExecExtension(environment, sdk).register(fake.pi);
|
|
86
|
-
return { ...fake, environment };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
it('replaces the tools that work through the environment', async () => {
|
|
90
|
-
const { tools } = await setup();
|
|
91
|
-
expect([...tools.keys()].sort()).toEqual(['bash', 'edit', 'find', 'ls', 'read', 'write']);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('runs them in the environment', async () => {
|
|
95
|
-
const { tools, environment } = await setup();
|
|
96
|
-
await tools.get('write').execute('c1', { path: 'a.txt', content: 'written' }, undefined, undefined, {});
|
|
97
|
-
expect(Buffer.from(await environment.readFile('a.txt')).toString()).toBe('written');
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('takes over the ! commands', async () => {
|
|
101
|
-
const { handlers } = await setup();
|
|
102
|
-
expect(typeof handlers.get('user_bash')!({}, {}).operations.exec).toBe('function');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('takes away grep, which would search this machine', async () => {
|
|
106
|
-
const { handlers, state } = await setup();
|
|
107
|
-
handlers.get('session_start')!({}, {});
|
|
108
|
-
expect(state.activeTools).toEqual(['read', 'bash']);
|
|
109
|
-
});
|
|
110
|
-
});
|
package/src/node/extensions.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI, ExtensionContext, InlineExtension } from './pi-types';
|
|
2
|
-
import type { SessionLock } from './session-lock';
|
|
3
|
-
|
|
4
|
-
/** A pi extension of ta. A plain object: the `ta` command runs without a Cell container. */
|
|
5
|
-
export interface TaExtension {
|
|
6
|
-
readonly name: string;
|
|
7
|
-
register(pi: ExtensionAPI): void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export namespace TaExtensionUtil {
|
|
11
|
-
export function toInline(extension: TaExtension): InlineExtension {
|
|
12
|
-
return { name: extension.name, factory: pi => extension.register(pi) };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** ta's built-in extensions as one, listed by pi as `ta` among the loaded extensions. */
|
|
16
|
-
export function bundle(extensions: TaExtension[]): InlineExtension {
|
|
17
|
-
return {
|
|
18
|
-
name: 'ta',
|
|
19
|
-
factory: pi => {
|
|
20
|
-
for (const extension of extensions) {
|
|
21
|
-
extension.register(pi);
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Holds the lock of the session file that is open. See {@link SessionLock}. */
|
|
29
|
-
export class SessionLockExtension implements TaExtension {
|
|
30
|
-
readonly name = 'ta:session-lock';
|
|
31
|
-
|
|
32
|
-
constructor(protected readonly lock: SessionLock) {}
|
|
33
|
-
|
|
34
|
-
register(pi: ExtensionAPI): void {
|
|
35
|
-
pi.on('session_before_switch', event => {
|
|
36
|
-
if (event.targetSessionFile && this.lock.isHeldByOther(event.targetSessionFile)) {
|
|
37
|
-
return { cancel: true };
|
|
38
|
-
}
|
|
39
|
-
return undefined;
|
|
40
|
-
});
|
|
41
|
-
pi.on('session_start', (_event, ctx) => {
|
|
42
|
-
const file = ctx.sessionManager.getSessionFile();
|
|
43
|
-
if (file && !this.lock.acquire(file)) {
|
|
44
|
-
this.refuse(file, ctx);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
pi.on('session_shutdown', (_event, ctx) => {
|
|
48
|
-
const file = ctx.sessionManager.getSessionFile();
|
|
49
|
-
if (file) {
|
|
50
|
-
this.lock.release(file);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* pi has opened the session by the time an extension can see it (`--continue`, `--resume`), so this
|
|
57
|
-
* can only end the process. `ctx.shutdown` restores the terminal; print modes do not act on it.
|
|
58
|
-
*/
|
|
59
|
-
protected refuse(file: string, ctx: ExtensionContext): void {
|
|
60
|
-
const message = `This session is open in another Ta process. If that process is gone, delete ${this.lock.path(file)}.`;
|
|
61
|
-
if (ctx.mode === 'tui') {
|
|
62
|
-
ctx.ui.notify(message, 'error');
|
|
63
|
-
ctx.shutdown();
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
process.stderr.write(`${message}\n`);
|
|
67
|
-
process.exit(1);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { GlobUtil } from './glob-util';
|
|
3
|
-
|
|
4
|
-
const matches = (pattern: string, path: string) => GlobUtil.matcher(pattern)(path);
|
|
5
|
-
|
|
6
|
-
describe('GlobUtil', () => {
|
|
7
|
-
it('matches names at any depth when the pattern has no slash', () => {
|
|
8
|
-
expect(matches('*.ts', 'src/a/b.ts')).toBe(true);
|
|
9
|
-
expect(matches('*.ts', 'src/a/b.js')).toBe(false);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('matches paths from the root when the pattern has a slash', () => {
|
|
13
|
-
expect(matches('src/*.ts', 'src/a.ts')).toBe(true);
|
|
14
|
-
expect(matches('src/*.ts', 'src/a/b.ts')).toBe(false);
|
|
15
|
-
expect(matches('src/**/*.ts', 'src/b.ts')).toBe(true);
|
|
16
|
-
expect(matches('src/**/*.ts', 'src/a/b/c.ts')).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('understands braces, question marks and classes', () => {
|
|
20
|
-
expect(matches('*.{ts,tsx}', 'a.tsx')).toBe(true);
|
|
21
|
-
expect(matches('a?.ts', 'ab.ts')).toBe(true);
|
|
22
|
-
expect(matches('[ab].ts', 'c.ts')).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('takes other characters literally', () => {
|
|
26
|
-
expect(matches('a.b', 'axb')).toBe(false);
|
|
27
|
-
expect(matches('a+(b)', 'a+(b)')).toBe(true);
|
|
28
|
-
});
|
|
29
|
-
});
|