@zvirb/pinup-canvas 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +9 -2
- package/dist/agent/acp.d.ts +21 -0
- package/dist/agent/acp.d.ts.map +1 -0
- package/dist/agent/acp.js +248 -0
- package/dist/agent/acp.js.map +1 -0
- package/dist/agent/backend.d.ts +70 -0
- package/dist/agent/backend.d.ts.map +1 -0
- package/dist/agent/backend.js +112 -0
- package/dist/agent/backend.js.map +1 -0
- package/dist/agent/brief.d.ts +12 -0
- package/dist/agent/brief.d.ts.map +1 -0
- package/dist/agent/brief.js +35 -0
- package/dist/agent/brief.js.map +1 -0
- package/dist/agent/claude.d.ts +6 -0
- package/dist/agent/claude.d.ts.map +1 -0
- package/dist/agent/claude.js +104 -0
- package/dist/agent/claude.js.map +1 -0
- package/dist/agent/codex.d.ts +5 -0
- package/dist/agent/codex.d.ts.map +1 -0
- package/dist/agent/codex.js +127 -0
- package/dist/agent/codex.js.map +1 -0
- package/dist/agent/copies.d.ts +47 -0
- package/dist/agent/copies.d.ts.map +1 -0
- package/dist/agent/copies.js +229 -0
- package/dist/agent/copies.js.map +1 -0
- package/dist/agent/gemini.d.ts +5 -0
- package/dist/agent/gemini.d.ts.map +1 -0
- package/dist/agent/gemini.js +116 -0
- package/dist/agent/gemini.js.map +1 -0
- package/dist/agent/http.d.ts +4 -0
- package/dist/agent/http.d.ts.map +1 -0
- package/dist/agent/http.js +20 -0
- package/dist/agent/http.js.map +1 -0
- package/dist/agent/registry.d.ts +9 -0
- package/dist/agent/registry.d.ts.map +1 -0
- package/dist/agent/registry.js +43 -0
- package/dist/agent/registry.js.map +1 -0
- package/dist/agent/runner.d.ts +33 -0
- package/dist/agent/runner.d.ts.map +1 -0
- package/dist/agent/runner.js +377 -0
- package/dist/agent/runner.js.map +1 -0
- package/dist/agent/steps.d.ts +13 -0
- package/dist/agent/steps.d.ts.map +1 -0
- package/dist/agent/steps.js +88 -0
- package/dist/agent/steps.js.map +1 -0
- package/dist/agents.d.ts +5 -0
- package/dist/agents.d.ts.map +1 -0
- package/dist/agents.js +4 -0
- package/dist/agents.js.map +1 -0
- package/dist/branch-css.d.ts +7 -0
- package/dist/branch-css.d.ts.map +1 -0
- package/dist/branch-css.js +69 -0
- package/dist/branch-css.js.map +1 -0
- package/dist/branch-graph.d.ts +14 -0
- package/dist/branch-graph.d.ts.map +1 -0
- package/dist/branch-graph.js +83 -0
- package/dist/branch-graph.js.map +1 -0
- package/dist/branches.d.ts +29 -0
- package/dist/branches.d.ts.map +1 -0
- package/dist/branches.js +115 -0
- package/dist/branches.js.map +1 -0
- package/dist/client.css +1741 -0
- package/dist/client.js +12153 -0
- package/dist/client.js.map +1 -0
- package/dist/server.d.ts +10 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +66 -0
- package/dist/server.js.map +1 -0
- package/dist/vite.d.ts +29 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +286 -0
- package/dist/vite.js.map +1 -0
- package/package.json +53 -3
- package/src/agent/acp.ts +299 -0
- package/src/agent/backend.ts +168 -0
- package/src/agent/brief.ts +43 -0
- package/src/agent/claude.ts +142 -0
- package/src/agent/codex.ts +156 -0
- package/src/agent/copies.ts +272 -0
- package/src/agent/gemini.ts +150 -0
- package/src/agent/http.ts +26 -0
- package/src/agent/registry.ts +60 -0
- package/src/agent/runner.ts +423 -0
- package/src/agent/steps.ts +107 -0
- package/src/agents.ts +5 -0
- package/src/branch-css.ts +70 -0
- package/src/branch-graph.ts +100 -0
- package/src/branches.ts +132 -0
- package/src/canvas.css +110 -0
- package/src/server.ts +86 -0
- package/src/vite.ts +339 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { allCommands, once, parseLine, probeVersion, run, } from "./backend.js";
|
|
2
|
+
import { clip, describeGeminiTool } from "./steps.js";
|
|
3
|
+
export function geminiCommand(options, commands, session, prompt, resume) {
|
|
4
|
+
return [
|
|
5
|
+
...(options.command ?? ['gemini']),
|
|
6
|
+
'--output-format',
|
|
7
|
+
'stream-json',
|
|
8
|
+
'--approval-mode',
|
|
9
|
+
'auto_edit',
|
|
10
|
+
...(options.model === undefined ? [] : ['--model', options.model]),
|
|
11
|
+
...(resume === undefined ? [] : ['--resume', resume]),
|
|
12
|
+
...session.writable.flatMap((dir) => ['--include-directories', dir]),
|
|
13
|
+
'--allowed-tools',
|
|
14
|
+
commands.map((c) => `run_shell_command(${c})`).join(','),
|
|
15
|
+
...(options.args ?? []),
|
|
16
|
+
'--prompt',
|
|
17
|
+
prompt,
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
export function gemini(options, commands) {
|
|
21
|
+
const allowed = allCommands(commands);
|
|
22
|
+
return {
|
|
23
|
+
name: 'gemini',
|
|
24
|
+
label: options.model === undefined ? 'Gemini' : `Gemini · ${options.model}`,
|
|
25
|
+
guard: `Edits files; runs only ${allowed.join(', ')}`,
|
|
26
|
+
tools: 'Read and search with your file tools (read_file, glob, search_file_content), not the shell: only the commands listed run, anything else is refused.',
|
|
27
|
+
check: () => probeVersion(options.command ?? ['gemini']),
|
|
28
|
+
open: (session) => geminiSession(options, allowed, session),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function geminiSession(options, commands, session) {
|
|
32
|
+
let id = session.resume;
|
|
33
|
+
let briefed = id !== undefined;
|
|
34
|
+
let current;
|
|
35
|
+
let stopped = false;
|
|
36
|
+
return {
|
|
37
|
+
turn(prompt, onStep) {
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
const done = once(resolve);
|
|
40
|
+
const text = briefed ? prompt : `${session.brief}\n\n${prompt}`;
|
|
41
|
+
briefed = true;
|
|
42
|
+
// Assistant text streams in deltas: one step per message, said when the next thing starts.
|
|
43
|
+
let message = '';
|
|
44
|
+
let last = '';
|
|
45
|
+
const flush = () => {
|
|
46
|
+
if (message.trim() === '')
|
|
47
|
+
return;
|
|
48
|
+
last = message.trim();
|
|
49
|
+
onStep({ kind: 'text', text: clip(last, 280) });
|
|
50
|
+
message = '';
|
|
51
|
+
};
|
|
52
|
+
let failure;
|
|
53
|
+
current = run(geminiCommand(options, commands, session, text, id), { cwd: session.cwd, env: session.env, stdin: 'ignore' }, (line) => {
|
|
54
|
+
const event = parseLine(line);
|
|
55
|
+
if (event === undefined)
|
|
56
|
+
return;
|
|
57
|
+
switch (event['type']) {
|
|
58
|
+
case 'init':
|
|
59
|
+
if (typeof event['session_id'] === 'string')
|
|
60
|
+
id = event['session_id'];
|
|
61
|
+
break;
|
|
62
|
+
case 'message':
|
|
63
|
+
if (event['role'] === 'assistant') {
|
|
64
|
+
if (event['delta'] !== true)
|
|
65
|
+
flush();
|
|
66
|
+
message += String(event['content'] ?? '');
|
|
67
|
+
if (event['delta'] !== true)
|
|
68
|
+
flush();
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case 'tool_use':
|
|
72
|
+
flush();
|
|
73
|
+
onStep({
|
|
74
|
+
kind: 'tool',
|
|
75
|
+
text: describeGeminiTool(session.cwd, String(event['tool_name'] ?? ''), (event['parameters'] ?? {})),
|
|
76
|
+
});
|
|
77
|
+
break;
|
|
78
|
+
case 'error':
|
|
79
|
+
failure = String(event['message'] ?? 'error');
|
|
80
|
+
break;
|
|
81
|
+
case 'result':
|
|
82
|
+
flush();
|
|
83
|
+
if (event['status'] === 'error') {
|
|
84
|
+
const error = event['error'];
|
|
85
|
+
failure = error?.message ?? failure ?? 'error';
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const child = current;
|
|
91
|
+
child.proc.on('error', (error) => done({ text: '', error: `${options.command?.[0] ?? 'gemini'}: ${error.message}` }));
|
|
92
|
+
child.proc.on('exit', (code, signal) => {
|
|
93
|
+
if (current === child)
|
|
94
|
+
current = undefined;
|
|
95
|
+
flush();
|
|
96
|
+
if (stopped || signal === 'SIGTERM')
|
|
97
|
+
return done({ text: last, stopped: true });
|
|
98
|
+
if (failure !== undefined)
|
|
99
|
+
return done({ text: last, error: clip(failure, 300) });
|
|
100
|
+
done(code === 0
|
|
101
|
+
? { text: last }
|
|
102
|
+
: { text: last, error: `gemini exited (${code ?? signal}): ${child.stderr()}` });
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
id: () => id,
|
|
107
|
+
stop() {
|
|
108
|
+
stopped = true;
|
|
109
|
+
current?.proc.kill('SIGTERM');
|
|
110
|
+
},
|
|
111
|
+
close() {
|
|
112
|
+
current?.proc.kill('SIGTERM');
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=gemini.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gemini.js","sourceRoot":"","sources":["../../src/agent/gemini.ts"],"names":[],"mappings":"AAOA,OAAO,EAGL,WAAW,EACX,IAAI,EACJ,SAAS,EACT,YAAY,EACZ,GAAG,GAEJ,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAErD,MAAM,UAAU,aAAa,CAC3B,OAAuB,EACvB,QAAkB,EAClB,OAAyC,EACzC,MAAc,EACd,MAA0B;IAE1B,OAAO;QACL,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClC,iBAAiB;QACjB,aAAa;QACb,iBAAiB;QACjB,WAAW;QACX,GAAG,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;QAClE,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACrD,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA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BAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,IAC/B,SAAS,eAAe,EAAE,UAAU,cAAc,KAAG,IAAI,CAalE"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function agentHandler(agent) {
|
|
2
|
+
return (request, response) => {
|
|
3
|
+
const url = new URL(request.url ?? '/', 'http://pinup');
|
|
4
|
+
const [, action, id] = url.pathname.split('/');
|
|
5
|
+
let extra = {};
|
|
6
|
+
if (request.method === 'POST') {
|
|
7
|
+
if (action === 'send' && id)
|
|
8
|
+
agent.send(id, url.searchParams.get('backend') ?? undefined);
|
|
9
|
+
else if (action === 'stop')
|
|
10
|
+
agent.stop(id || undefined);
|
|
11
|
+
else if (action === 'approve' && id)
|
|
12
|
+
extra = { merge: agent.approve(id) };
|
|
13
|
+
else if (action === 'discard' && id)
|
|
14
|
+
agent.discard(id);
|
|
15
|
+
}
|
|
16
|
+
response.setHeader('Content-Type', 'application/json');
|
|
17
|
+
response.end(JSON.stringify({ ...agent.status(), ...extra }));
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/agent/http.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,YAAY,CAAC,KAAY;IACvC,OAAO,CAAC,OAAwB,EAAE,QAAwB,EAAQ,EAAE;QAClE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,GAAG,EAAE,cAAc,CAAC,CAAA;QACvD,MAAM,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QAC9C,IAAI,KAAK,GAA4B,EAAE,CAAA;QACvC,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YAC9B,IAAI,MAAM,KAAK,MAAM,IAAI,EAAE;gBAAE,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,SAAS,CAAC,CAAA;iBACpF,IAAI,MAAM,KAAK,MAAM;gBAAE,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,SAAS,CAAC,CAAA;iBAClD,IAAI,MAAM,KAAK,SAAS,IAAI,EAAE;gBAAE,KAAK,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAA;iBACpE,IAAI,MAAM,KAAK,SAAS,IAAI,EAAE;gBAAE,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QACxD,CAAC;QACD,QAAQ,CAAC,SAAS,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAA;QACtD,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,KAAK,CAAC,MAAM,EAAE,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC,CAAA;IAC/D,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type AgentConfig, type BackendInfo } from '@zvirb/pinup-core';
|
|
2
|
+
import type { AgentBackend } from './backend.ts';
|
|
3
|
+
/** The backend by this name, with its settings: the default's model applies to the default only. */
|
|
4
|
+
export declare function makeBackend(config: AgentConfig, name: string): AgentBackend | undefined;
|
|
5
|
+
/** Every backend worth offering: the default, configured ones, and known CLIs that turn out installed. */
|
|
6
|
+
export declare function candidateNames(config: AgentConfig): string[];
|
|
7
|
+
/** Checks each candidate. The default is always listed; others only when they can run. */
|
|
8
|
+
export declare function checkBackends(config: AgentConfig): Promise<BackendInfo[]>;
|
|
9
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/agent/registry.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,KAAK,WAAW,EAEhB,KAAK,WAAW,EAIjB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAUhD,oGAAoG;AACpG,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAUvF;AAED,0GAA0G;AAC1G,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,EAAE,CAI5D;AAED,0FAA0F;AAC1F,wBAAsB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAiB/E"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Which backend runs: the config's default, any it has settings for, and the CLIs Pinup knows that
|
|
2
|
+
// are installed, so a comment can be sent to another one from the canvas.
|
|
3
|
+
import { BACKENDS, backendName, } from '@zvirb/pinup-core';
|
|
4
|
+
import { acp, antigravity } from "./acp.js";
|
|
5
|
+
import { claude } from "./claude.js";
|
|
6
|
+
import { codex } from "./codex.js";
|
|
7
|
+
import { gemini } from "./gemini.js";
|
|
8
|
+
const FACTORIES = { claude, codex, gemini, antigravity, acp };
|
|
9
|
+
/** The backend by this name, with its settings: the default's model applies to the default only. */
|
|
10
|
+
export function makeBackend(config, name) {
|
|
11
|
+
const chosen = config.backend;
|
|
12
|
+
if (typeof chosen === 'object' && chosen.name === name) {
|
|
13
|
+
const { name: _name, protocol, ...options } = chosen;
|
|
14
|
+
return { ...FACTORIES[protocol](options, config.commands), name };
|
|
15
|
+
}
|
|
16
|
+
if (!BACKENDS.includes(name))
|
|
17
|
+
return undefined;
|
|
18
|
+
const options = { ...config.backends?.[name] };
|
|
19
|
+
if (name === backendName(config) && config.model !== undefined)
|
|
20
|
+
options.model = config.model;
|
|
21
|
+
return FACTORIES[name](options, config.commands);
|
|
22
|
+
}
|
|
23
|
+
/** Every backend worth offering: the default, configured ones, and known CLIs that turn out installed. */
|
|
24
|
+
export function candidateNames(config) {
|
|
25
|
+
const configured = Object.keys(config.backends ?? {});
|
|
26
|
+
const known = BACKENDS.filter((name) => name !== 'acp');
|
|
27
|
+
return [...new Set([backendName(config), ...configured, ...known])];
|
|
28
|
+
}
|
|
29
|
+
/** Checks each candidate. The default is always listed; others only when they can run. */
|
|
30
|
+
export async function checkBackends(config) {
|
|
31
|
+
const fallback = backendName(config);
|
|
32
|
+
const names = candidateNames(config);
|
|
33
|
+
const infos = await Promise.all(names.map(async (name) => {
|
|
34
|
+
const backend = makeBackend(config, name);
|
|
35
|
+
if (backend === undefined)
|
|
36
|
+
return { name, label: name, ok: false, guard: '', problem: `unknown backend "${name}"` };
|
|
37
|
+
const result = await backend.check();
|
|
38
|
+
return { name, label: backend.label, guard: backend.guard, ...result };
|
|
39
|
+
}));
|
|
40
|
+
return infos.filter((info) => info !== undefined &&
|
|
41
|
+
(info.ok || info.name === fallback || Object.hasOwn(config.backends ?? {}, info.name)));
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/agent/registry.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,0EAA0E;AAC1E,OAAO,EAEL,QAAQ,EAIR,WAAW,GACZ,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,MAAM,SAAS,GAGX,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,CAAA;AAE/C,oGAAoG;AACpG,MAAM,UAAU,WAAW,CAAC,MAAmB,EAAE,IAAY;IAC3D,MAAM,MAAM,GAAG,MAAM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,CAAC,MAAM,CAAC,CAAA;IACpC,MAAM,KAAK,GAAG,cAAc,CAAC,MAAM,CAAC,CAAA;IACpC,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAC7B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAoC,EAAE;QACzD,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QACzC,IAAI,OAAO,KAAK,SAAS;YACvB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,oBAAoB,IAAI,GAAG,EAAE,CAAA;QAC1F,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,EAAE,CAAA;QACpC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,GAAG,MAAM,EAAE,CAAA;IACxE,CAAC,CAAC,CACH,CAAA;IACD,OAAO,KAAK,CAAC,MAAM,CACjB,CAAC,IAAI,EAAuB,EAAE,CAC5B,IAAI,KAAK,SAAS;QAClB,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CACzF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type AgentConfig, type AgentStatus, type CommentFile } from '@zvirb/pinup-core';
|
|
2
|
+
import { type AgentBackend } from './backend.ts';
|
|
3
|
+
import { type MergeResult } from './copies.ts';
|
|
4
|
+
export interface AgentOptions {
|
|
5
|
+
root: string;
|
|
6
|
+
commentsFile: string;
|
|
7
|
+
config: AgentConfig | undefined;
|
|
8
|
+
onStatus: (status: AgentStatus) => void;
|
|
9
|
+
log: (line: string) => void;
|
|
10
|
+
/** Tests swap in their own backends. */
|
|
11
|
+
backend?: (name: string) => AgentBackend | undefined;
|
|
12
|
+
}
|
|
13
|
+
export declare function createAgent(options: AgentOptions): {
|
|
14
|
+
status: () => AgentStatus;
|
|
15
|
+
/** The comments file changed: queue whatever now waits on an agent. */
|
|
16
|
+
sync(file?: CommentFile): void;
|
|
17
|
+
/** "Send to agent": queue it now, whoever spoke last, on this backend or the default. */
|
|
18
|
+
send(id: string, backend?: string): void;
|
|
19
|
+
/** Stops the comment it is on (or every agent). The queue carries on. */
|
|
20
|
+
stop(id?: string): void;
|
|
21
|
+
/**
|
|
22
|
+
* Worktree isolation: copies a comment's change into the project and resolves it. On a
|
|
23
|
+
* conflict, nothing changes in the project and the comment goes back to its agent.
|
|
24
|
+
*/
|
|
25
|
+
approve(id: string): MergeResult | {
|
|
26
|
+
problem: string;
|
|
27
|
+
};
|
|
28
|
+
/** Worktree isolation: drops a comment's change. The comment stays open. */
|
|
29
|
+
discard(id: string): void;
|
|
30
|
+
close(): void;
|
|
31
|
+
};
|
|
32
|
+
export type Agent = ReturnType<typeof createAgent>;
|
|
33
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../src/agent/runner.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,WAAW,EAQhB,KAAK,WAAW,EAIjB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,KAAK,YAAY,EAAmD,MAAM,cAAc,CAAA;AAEjG,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,aAAa,CAAA;AA2B5D,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,EAAE,WAAW,GAAG,SAAS,CAAA;IAC/B,QAAQ,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,IAAI,CAAA;IACvC,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC3B,wCAAwC;IACxC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,YAAY,GAAG,SAAS,CAAA;CACrD;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,YAAY;kBA2C5B,WAAW;IAwO5B,uEAAuE;gBAC5D,WAAW;IAkBtB,yFAAyF;aAChF,MAAM,YAAY,MAAM;IAQjC,yEAAyE;cAC/D,MAAM;IAKhB;;;OAGG;gBACS,MAAM,GAAG,WAAW,GAAG;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;IAwBtD,4EAA4E;gBAChE,MAAM;;EAgBrB;AAED,MAAM,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAA"}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// The canvas agents: `maxAgents` workers, given open comments one at a time each. A worker drives
|
|
2
|
+
// one backend (./backend.ts) at a time; a comment can be sent to another. The runner knows the
|
|
3
|
+
// comments file, the queue and the copies; it knows nothing about any one CLI.
|
|
4
|
+
//
|
|
5
|
+
// tree one worker, editing the project in place: the canvas hot-reloads as it works. One
|
|
6
|
+
// session across comments (it remembers what it did), fresh every `rotate` comments.
|
|
7
|
+
// worktree each comment gets a copy of the project (./copies.ts), shown on the canvas as a
|
|
8
|
+
// proposal. A session per comment, resumed when the reviewer replies. Approve merges.
|
|
9
|
+
//
|
|
10
|
+
// Approving stays with the reviewer: agents are told never to resolve.
|
|
11
|
+
import { delimiter, dirname, join, relative, sep } from 'node:path';
|
|
12
|
+
import { addReply, agentSlots, awaitsAgent, backendName, setStatus, } from '@zvirb/pinup-core';
|
|
13
|
+
import { load, save } from '@zvirb/pinup-core/node';
|
|
14
|
+
import { allCommands } from "./backend.js";
|
|
15
|
+
import { brief, prompt } from "./brief.js";
|
|
16
|
+
import { createCopies } from "./copies.js";
|
|
17
|
+
import { checkBackends, makeBackend } from "./registry.js";
|
|
18
|
+
import { clip } from "./steps.js";
|
|
19
|
+
export function createAgent(options) {
|
|
20
|
+
const { root, commentsFile } = options;
|
|
21
|
+
// `agent: false` isn't a config, but a plain-JS one may say it: it means off, not the defaults.
|
|
22
|
+
const config = options.config || undefined;
|
|
23
|
+
const { isolation, maxAgents } = agentSlots(config);
|
|
24
|
+
const send = config?.send ?? 'new';
|
|
25
|
+
const rotate = config?.rotate ?? 12;
|
|
26
|
+
const fallback = backendName(config);
|
|
27
|
+
const commands = allCommands(config?.commands);
|
|
28
|
+
const copies = createCopies(root, [relative(root, commentsFile).split(sep).join('/')]);
|
|
29
|
+
const backendFor = (name) => options.backend?.(name) ?? (config === undefined ? undefined : makeBackend(config, name));
|
|
30
|
+
const workers = Array.from({ length: maxAgents }, (_, i) => ({
|
|
31
|
+
id: `agent-${i + 1}`,
|
|
32
|
+
name: maxAgents === 1 ? 'Agent' : `Agent ${i + 1}`,
|
|
33
|
+
backend: fallback,
|
|
34
|
+
done: 0,
|
|
35
|
+
}));
|
|
36
|
+
const queue = [];
|
|
37
|
+
/** `<id>:<replies>` already given to an agent (or stopped): never auto-sent twice. */
|
|
38
|
+
const handled = new Set();
|
|
39
|
+
let backends = [];
|
|
40
|
+
let proposals = [];
|
|
41
|
+
let error;
|
|
42
|
+
let closed = false;
|
|
43
|
+
const view = (worker) => ({
|
|
44
|
+
id: worker.id,
|
|
45
|
+
name: worker.name,
|
|
46
|
+
backend: worker.backend,
|
|
47
|
+
running: worker.session !== undefined || worker.active !== undefined,
|
|
48
|
+
...(worker.current === undefined
|
|
49
|
+
? {}
|
|
50
|
+
: {
|
|
51
|
+
current: {
|
|
52
|
+
id: worker.current.id,
|
|
53
|
+
since: worker.current.since,
|
|
54
|
+
steps: worker.current.steps,
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
const status = () => ({
|
|
59
|
+
enabled: config !== undefined,
|
|
60
|
+
send,
|
|
61
|
+
isolation,
|
|
62
|
+
backend: fallback,
|
|
63
|
+
workers: workers.map(view),
|
|
64
|
+
queue: queue.map((job) => job.id),
|
|
65
|
+
backends,
|
|
66
|
+
proposals,
|
|
67
|
+
...(error === undefined ? {} : { error }),
|
|
68
|
+
});
|
|
69
|
+
const report = () => {
|
|
70
|
+
if (!closed)
|
|
71
|
+
options.onStatus(status());
|
|
72
|
+
};
|
|
73
|
+
const refreshProposals = () => {
|
|
74
|
+
if (isolation !== 'worktree')
|
|
75
|
+
return;
|
|
76
|
+
proposals = copies.list().flatMap((id) => {
|
|
77
|
+
const meta = copies.meta(id);
|
|
78
|
+
if (meta === undefined)
|
|
79
|
+
return [];
|
|
80
|
+
return [
|
|
81
|
+
{
|
|
82
|
+
comment: meta.comment,
|
|
83
|
+
branch: id,
|
|
84
|
+
label: meta.label,
|
|
85
|
+
files: copies.changes(id).map((change) => change.path),
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
const busy = (id) => workers.some((w) => w.current?.id === id);
|
|
91
|
+
const comments = () => load(commentsFile);
|
|
92
|
+
const find = (id) => comments().comments.find((c) => c.id === id);
|
|
93
|
+
function next() {
|
|
94
|
+
if (closed)
|
|
95
|
+
return;
|
|
96
|
+
for (const worker of workers) {
|
|
97
|
+
if (worker.current !== undefined)
|
|
98
|
+
continue;
|
|
99
|
+
// Two agents never work on one board at once: they would change the same component.
|
|
100
|
+
const taken = new Set(workers.flatMap((w) => (w.current ? [w.current.source] : [])));
|
|
101
|
+
const index = queue.findIndex((job) => {
|
|
102
|
+
const comment = find(job.id);
|
|
103
|
+
return comment !== undefined && !taken.has(comment.anchor.source);
|
|
104
|
+
});
|
|
105
|
+
if (index < 0)
|
|
106
|
+
break;
|
|
107
|
+
const [job] = queue.splice(index, 1);
|
|
108
|
+
if (job !== undefined)
|
|
109
|
+
void take(worker, job);
|
|
110
|
+
}
|
|
111
|
+
// Resolved or deleted while queued.
|
|
112
|
+
const open = new Set(comments()
|
|
113
|
+
.comments.filter((c) => c.status === 'open')
|
|
114
|
+
.map((c) => c.id));
|
|
115
|
+
for (let i = queue.length - 1; i >= 0; i--)
|
|
116
|
+
if (!open.has(queue[i]?.id ?? ''))
|
|
117
|
+
queue.splice(i, 1);
|
|
118
|
+
report();
|
|
119
|
+
}
|
|
120
|
+
async function take(worker, job) {
|
|
121
|
+
const comment = find(job.id);
|
|
122
|
+
if (comment === undefined || comment.status !== 'open')
|
|
123
|
+
return next();
|
|
124
|
+
const copy = isolation === 'worktree' ? copies.meta(comment.id) : undefined;
|
|
125
|
+
const name = job.backend ?? copy?.backend ?? fallback;
|
|
126
|
+
const backend = backendFor(name);
|
|
127
|
+
if (backend === undefined) {
|
|
128
|
+
error = `no backend "${name}"`;
|
|
129
|
+
return next();
|
|
130
|
+
}
|
|
131
|
+
worker.backend = name;
|
|
132
|
+
worker.current = {
|
|
133
|
+
...job,
|
|
134
|
+
since: new Date().toISOString(),
|
|
135
|
+
steps: [],
|
|
136
|
+
source: comment.anchor.source,
|
|
137
|
+
};
|
|
138
|
+
// A conflict's message stays up while its agent works it out.
|
|
139
|
+
if (job.note === undefined)
|
|
140
|
+
error = undefined;
|
|
141
|
+
const signature = `${worker.name} · ${backend.label}`;
|
|
142
|
+
const cwd = isolation === 'tree' ? root : copies.dirOf(comment.id);
|
|
143
|
+
const env = {
|
|
144
|
+
PINUP_COMMENTS: commentsFile,
|
|
145
|
+
PINUP_AGENT_NAME: signature,
|
|
146
|
+
// The project's own `pinup` comes first: `npx pinup` would fetch an unrelated npm package.
|
|
147
|
+
PATH: [
|
|
148
|
+
join(cwd, 'node_modules/.bin'),
|
|
149
|
+
join(root, 'node_modules/.bin'),
|
|
150
|
+
process.env['PATH'] ?? '',
|
|
151
|
+
]
|
|
152
|
+
.filter(Boolean)
|
|
153
|
+
.join(delimiter),
|
|
154
|
+
};
|
|
155
|
+
let session;
|
|
156
|
+
try {
|
|
157
|
+
if (isolation === 'tree') {
|
|
158
|
+
// Sent to another backend: that one gets a session of its own.
|
|
159
|
+
if (worker.session !== undefined && worker.sessionBackend !== name) {
|
|
160
|
+
worker.session.close();
|
|
161
|
+
worker.session = undefined;
|
|
162
|
+
}
|
|
163
|
+
worker.session ??= backend.open({
|
|
164
|
+
cwd: root,
|
|
165
|
+
brief: brief({ tools: backend.tools, commands }),
|
|
166
|
+
env,
|
|
167
|
+
writable: [],
|
|
168
|
+
});
|
|
169
|
+
worker.sessionBackend = name;
|
|
170
|
+
session = worker.session;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
if (copy === undefined) {
|
|
174
|
+
copies.create(comment.id, {
|
|
175
|
+
label: `${worker.name} · ${backend.label} (#${comment.id.replace(/\D/g, '')})`,
|
|
176
|
+
comment: comment.id,
|
|
177
|
+
agent: worker.id,
|
|
178
|
+
backend: name,
|
|
179
|
+
createdAt: new Date().toISOString(),
|
|
180
|
+
});
|
|
181
|
+
refreshProposals();
|
|
182
|
+
}
|
|
183
|
+
const dir = copies.dirOf(comment.id);
|
|
184
|
+
const resume = copy?.backend === name ? copy.session : undefined;
|
|
185
|
+
session = backend.open({
|
|
186
|
+
cwd: dir,
|
|
187
|
+
brief: brief({ tools: backend.tools, commands, copy: { dir } }),
|
|
188
|
+
env,
|
|
189
|
+
writable: [dirname(commentsFile)],
|
|
190
|
+
...(resume === undefined ? {} : { resume }),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (failure) {
|
|
195
|
+
worker.current = undefined;
|
|
196
|
+
error = `${backend.label}: ${failure.message}`;
|
|
197
|
+
options.log(`agent: ${error}`);
|
|
198
|
+
return next();
|
|
199
|
+
}
|
|
200
|
+
worker.active = session;
|
|
201
|
+
options.log(`agent: ${worker.name} (${backend.label}) on ${job.id}`);
|
|
202
|
+
report();
|
|
203
|
+
const result = await session.turn(prompt(comment, job.seen, job.note), (step) => {
|
|
204
|
+
const current = worker.current;
|
|
205
|
+
if (current === undefined || step.text === '')
|
|
206
|
+
return;
|
|
207
|
+
current.steps.push({ ...step, at: new Date().toISOString() });
|
|
208
|
+
if (current.steps.length > 40)
|
|
209
|
+
current.steps.splice(0, current.steps.length - 40);
|
|
210
|
+
report();
|
|
211
|
+
});
|
|
212
|
+
worker.active = undefined;
|
|
213
|
+
finish(worker, job, session, result, signature);
|
|
214
|
+
}
|
|
215
|
+
/** The turn is over. If it never answered on the thread, its last words become the answer. */
|
|
216
|
+
function finish(worker, job, session, result, signature) {
|
|
217
|
+
worker.current = undefined;
|
|
218
|
+
worker.done++;
|
|
219
|
+
if (closed)
|
|
220
|
+
return;
|
|
221
|
+
if (result.stopped) {
|
|
222
|
+
const current = find(job.id);
|
|
223
|
+
handled.add(`${job.id}:${current?.replies.length ?? job.seen}`);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const file = comments();
|
|
227
|
+
const comment = file.comments.find((c) => c.id === job.id);
|
|
228
|
+
const answered = comment?.replies.slice(job.seen).some((r) => r.author === 'agent') ?? false;
|
|
229
|
+
if (comment !== undefined && !answered) {
|
|
230
|
+
const text = result.text.trim();
|
|
231
|
+
const body = result.error !== undefined || text === ''
|
|
232
|
+
? 'I stopped without an answer. Reply to have me try again.'
|
|
233
|
+
: clip(text, 1200);
|
|
234
|
+
const next = addReply(file, job.id, { author: 'agent', body, agent: signature }, new Date().toISOString());
|
|
235
|
+
// Its own reply must not queue the comment again.
|
|
236
|
+
handled.add(`${job.id}:${next.comments.find((c) => c.id === job.id)?.replies.length ?? 0}`);
|
|
237
|
+
save(commentsFile, next);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
error = result.error;
|
|
241
|
+
if (result.error !== undefined)
|
|
242
|
+
options.log(`agent: ${result.error}`);
|
|
243
|
+
if (isolation === 'tree') {
|
|
244
|
+
// A session that failed or was stopped is gone; a healthy one is renewed every `rotate`.
|
|
245
|
+
if (result.stopped || result.error !== undefined || worker.done % rotate === 0) {
|
|
246
|
+
if (!result.stopped)
|
|
247
|
+
session.close();
|
|
248
|
+
worker.session = undefined;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
const id = session.id();
|
|
253
|
+
if (id !== undefined)
|
|
254
|
+
copies.update(job.id, { session: id });
|
|
255
|
+
session.close();
|
|
256
|
+
refreshProposals();
|
|
257
|
+
}
|
|
258
|
+
next();
|
|
259
|
+
}
|
|
260
|
+
const enqueue = (comment, extra = {}) => {
|
|
261
|
+
const key = `${comment.id}:${comment.replies.length}`;
|
|
262
|
+
if (handled.has(key) && extra.note === undefined)
|
|
263
|
+
return false;
|
|
264
|
+
if (busy(comment.id) || queue.some((job) => job.id === comment.id))
|
|
265
|
+
return false;
|
|
266
|
+
handled.add(key);
|
|
267
|
+
queue.push({ id: comment.id, seen: comment.replies.length, ...extra });
|
|
268
|
+
return true;
|
|
269
|
+
};
|
|
270
|
+
const stopOn = (id) => {
|
|
271
|
+
const worker = workers.find((w) => w.current?.id === id);
|
|
272
|
+
worker?.active?.stop();
|
|
273
|
+
};
|
|
274
|
+
if (config !== undefined) {
|
|
275
|
+
refreshProposals();
|
|
276
|
+
void checkBackends(config).then((infos) => {
|
|
277
|
+
backends = infos;
|
|
278
|
+
const chosen = infos.find((info) => info.name === fallback);
|
|
279
|
+
if (chosen !== undefined && !chosen.ok) {
|
|
280
|
+
error = `${chosen.label}: ${chosen.problem ?? 'unavailable'}`;
|
|
281
|
+
options.log(`agent: ${error}`);
|
|
282
|
+
}
|
|
283
|
+
report();
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
status,
|
|
288
|
+
/** The comments file changed: queue whatever now waits on an agent. */
|
|
289
|
+
sync(file = comments()) {
|
|
290
|
+
if (config === undefined)
|
|
291
|
+
return;
|
|
292
|
+
if (send === 'new')
|
|
293
|
+
for (const comment of file.comments)
|
|
294
|
+
if (awaitsAgent(comment))
|
|
295
|
+
enqueue(comment);
|
|
296
|
+
if (isolation === 'worktree') {
|
|
297
|
+
// A copy whose comment was resolved or deleted elsewhere has nothing left to propose.
|
|
298
|
+
const open = new Set(file.comments.filter((c) => c.status === 'open').map((c) => c.id));
|
|
299
|
+
let pruned = false;
|
|
300
|
+
for (const id of copies.list())
|
|
301
|
+
if (!open.has(id) && !busy(id)) {
|
|
302
|
+
copies.discard(id);
|
|
303
|
+
pruned = true;
|
|
304
|
+
}
|
|
305
|
+
if (pruned)
|
|
306
|
+
refreshProposals();
|
|
307
|
+
}
|
|
308
|
+
next();
|
|
309
|
+
},
|
|
310
|
+
/** "Send to agent": queue it now, whoever spoke last, on this backend or the default. */
|
|
311
|
+
send(id, backend) {
|
|
312
|
+
if (config === undefined)
|
|
313
|
+
return;
|
|
314
|
+
const comment = find(id);
|
|
315
|
+
if (comment === undefined || comment.status !== 'open')
|
|
316
|
+
return;
|
|
317
|
+
handled.delete(`${comment.id}:${comment.replies.length}`);
|
|
318
|
+
if (enqueue(comment, backend === undefined ? {} : { backend }))
|
|
319
|
+
next();
|
|
320
|
+
},
|
|
321
|
+
/** Stops the comment it is on (or every agent). The queue carries on. */
|
|
322
|
+
stop(id) {
|
|
323
|
+
if (id !== undefined)
|
|
324
|
+
return stopOn(id);
|
|
325
|
+
for (const worker of workers)
|
|
326
|
+
worker.active?.stop();
|
|
327
|
+
},
|
|
328
|
+
/**
|
|
329
|
+
* Worktree isolation: copies a comment's change into the project and resolves it. On a
|
|
330
|
+
* conflict, nothing changes in the project and the comment goes back to its agent.
|
|
331
|
+
*/
|
|
332
|
+
approve(id) {
|
|
333
|
+
if (isolation !== 'worktree')
|
|
334
|
+
return { problem: 'tree isolation: nothing to merge' };
|
|
335
|
+
const meta = copies.meta(id);
|
|
336
|
+
if (meta === undefined)
|
|
337
|
+
return { problem: `no proposal for ${id}` };
|
|
338
|
+
if (busy(id))
|
|
339
|
+
return { problem: `${id}: an agent is still working on it` };
|
|
340
|
+
const result = copies.merge(id);
|
|
341
|
+
const comment = find(id);
|
|
342
|
+
if ('applied' in result) {
|
|
343
|
+
copies.discard(id);
|
|
344
|
+
if (comment !== undefined)
|
|
345
|
+
save(commentsFile, setStatus(comments(), id, 'resolved'));
|
|
346
|
+
error = undefined;
|
|
347
|
+
}
|
|
348
|
+
else if (comment !== undefined) {
|
|
349
|
+
const files = result.conflicts.join(', ');
|
|
350
|
+
error = `${id}: ${files} changed in the project meanwhile; sent back to the agent`;
|
|
351
|
+
enqueue(comment, {
|
|
352
|
+
backend: meta.backend,
|
|
353
|
+
note: `Since you started, the project changed in ${files}. In your folder ${result.conflicts.length === 1 ? 'that file now holds' : 'those files now hold'} conflict markers (project / proposal) against the current version. Resolve them, keeping both the newer edits and your change, run the checks again, then reply.`,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
refreshProposals();
|
|
357
|
+
next();
|
|
358
|
+
return result;
|
|
359
|
+
},
|
|
360
|
+
/** Worktree isolation: drops a comment's change. The comment stays open. */
|
|
361
|
+
discard(id) {
|
|
362
|
+
stopOn(id);
|
|
363
|
+
copies.discard(id);
|
|
364
|
+
refreshProposals();
|
|
365
|
+
report();
|
|
366
|
+
},
|
|
367
|
+
close() {
|
|
368
|
+
closed = true;
|
|
369
|
+
queue.length = 0;
|
|
370
|
+
for (const worker of workers) {
|
|
371
|
+
worker.active?.stop();
|
|
372
|
+
worker.session?.close();
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
//# sourceMappingURL=runner.js.map
|