diacode 0.0.0-stage → 0.2.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/CHANGELOG.md +317 -0
- package/LICENSE +21 -0
- package/README.md +73 -2
- package/THIRD-PARTY-NOTICES.md +1535 -0
- package/bin/dcursed.js +8090 -0
- package/editor/app/compact/index.html +24 -0
- package/editor/app/index.html +115 -0
- package/editor/assets/app-1SR3-4wc.css +1 -0
- package/editor/assets/app-CSD76pDG.js +122 -0
- package/editor/assets/compact-D4LqjEE-.css +1 -0
- package/editor/assets/compact-OOTi-DHu.js +36 -0
- package/editor/assets/fonts-C71K6yRy.js +153 -0
- package/editor/assets/fonts-CdeCoHOe.css +1 -0
- package/editor/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/editor/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/editor/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/editor/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/editor/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/editor/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/editor/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/editor/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/editor/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/editor/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/editor/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/editor/favicon-needs.svg +12 -0
- package/editor/favicon.svg +11 -0
- package/opencode-plugin.js +353 -0
- package/package.json +32 -4
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
|
|
2
|
+
<title>DiaCode</title>
|
|
3
|
+
<rect width="32" height="32" rx="7" fill="#19120F"/>
|
|
4
|
+
<rect x="8" y="10" width="16" height="12" rx="2.5" fill="#EA5202"/>
|
|
5
|
+
<g fill="#FBFAF8" stroke="#19120F" stroke-width="1.2">
|
|
6
|
+
<rect x="5.4" y="7.4" width="5.2" height="5.2" rx="1"/>
|
|
7
|
+
<rect x="21.4" y="7.4" width="5.2" height="5.2" rx="1"/>
|
|
8
|
+
<rect x="5.4" y="19.4" width="5.2" height="5.2" rx="1"/>
|
|
9
|
+
<rect x="21.4" y="19.4" width="5.2" height="5.2" rx="1"/>
|
|
10
|
+
</g>
|
|
11
|
+
<circle cx="25" cy="7" r="6.5" fill="#EA5202" stroke="#FBFAF8" stroke-width="2"/>
|
|
12
|
+
</svg>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
|
|
2
|
+
<title>DiaCode</title>
|
|
3
|
+
<rect width="32" height="32" rx="7" fill="#19120F"/>
|
|
4
|
+
<rect x="8" y="10" width="16" height="12" rx="2.5" fill="#EA5202"/>
|
|
5
|
+
<g fill="#FBFAF8" stroke="#19120F" stroke-width="1.2">
|
|
6
|
+
<rect x="5.4" y="7.4" width="5.2" height="5.2" rx="1"/>
|
|
7
|
+
<rect x="21.4" y="7.4" width="5.2" height="5.2" rx="1"/>
|
|
8
|
+
<rect x="5.4" y="19.4" width="5.2" height="5.2" rx="1"/>
|
|
9
|
+
<rect x="21.4" y="19.4" width="5.2" height="5.2" rx="1"/>
|
|
10
|
+
</g>
|
|
11
|
+
</svg>
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// dcursed.js — the OpenCode side of DiaCode. One file with no dependencies, so `diacode init`
|
|
2
|
+
// can copy it into a repository's .opencode/plugins/.
|
|
3
|
+
//
|
|
4
|
+
// The MCP tools are how the agent reads and edits the plan. This plugin does what the tools
|
|
5
|
+
// cannot, because it sees what the agent does instead of what it says:
|
|
6
|
+
// - the plan's state rides at the top of every request, so the agent never starts blind;
|
|
7
|
+
// - what the person (or another agent) changed is appended to the agent's NEXT tool result,
|
|
8
|
+
// which is when it is next listening (docs/PLAN.md, decision 6);
|
|
9
|
+
// - a file the agent edits that exactly one ready step lists puts that step in progress, as
|
|
10
|
+
// `observed`: the model forgetting to call step_start does not leave the plan lying;
|
|
11
|
+
// - whether the agent is working, idle, waiting for your permission in OpenCode, or has hit an
|
|
12
|
+
// error is told to the plan server, so the editor can say who is busy now and who is waiting on
|
|
13
|
+
// you, and a step held by an agent that has gone quiet shows it;
|
|
14
|
+
// - when you approve the plan in the editor, or answer a question the agent stopped on, the
|
|
15
|
+
// session that was working on the plan is given a turn with it: you do not copy anything into
|
|
16
|
+
// OpenCode. The plugin keeps one request open to the plan server for that (a "call").
|
|
17
|
+
//
|
|
18
|
+
// It never gets in the agent's way: a server that is down or slow, or a reply it does not
|
|
19
|
+
// understand, means it says nothing.
|
|
20
|
+
|
|
21
|
+
import { appendFileSync } from 'node:fs';
|
|
22
|
+
import { relative, isAbsolute } from 'node:path';
|
|
23
|
+
|
|
24
|
+
const TIMEOUT_MS = 1500;
|
|
25
|
+
/** While a session is busy the plugin says so this often, so a long wait for a model is not read as silence. */
|
|
26
|
+
const HEARTBEAT_MS = 20_000;
|
|
27
|
+
/** Tools that write a file named by one of these arguments. */
|
|
28
|
+
const FILE_ARGS = ['filePath', 'path', 'file'];
|
|
29
|
+
const PATCH_FILE = /^\*\*\* (?:Add|Update|Delete) File: (.+?)\s*$/gm;
|
|
30
|
+
/** The end of every dcursed tool's reply: which plan it was about. */
|
|
31
|
+
const FOOTER = /\[plan ([^\s\]]+) · rev (\d+)\]\s*$/;
|
|
32
|
+
/** How long the plan server holds the request for a call before answering with nothing (and it is asked again). */
|
|
33
|
+
const HOLD_MS = 50_000;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Options, from the loader `diacode init` writes, and overridden by the environment for one run:
|
|
37
|
+
* `server` (DCURSED_URL), the plan server; `agent` (DCURSED_AGENT), the name the MCP door uses, so a
|
|
38
|
+
* step this agent starts through a tool and one the plugin observes belong to the same actor;
|
|
39
|
+
* `plan`, to follow one plan; `wake` (DCURSED_WAKE=0 turns it off), whether what you do in the editor
|
|
40
|
+
* may start a turn.
|
|
41
|
+
* @type {import('@opencode-ai/plugin').Plugin}
|
|
42
|
+
*/
|
|
43
|
+
export const DiaCode = async ({ directory, worktree, client }, options = {}) => {
|
|
44
|
+
const server = String(process.env.DCURSED_URL ?? options.server ?? 'http://127.0.0.1:7300').replace(/\/$/, '');
|
|
45
|
+
const agent = `opencode:${String(process.env.DCURSED_AGENT ?? options.agent ?? 'build')}`;
|
|
46
|
+
const only = typeof options.plan === 'string' ? options.plan : undefined;
|
|
47
|
+
const root = worktree && worktree !== '/' ? worktree : directory;
|
|
48
|
+
|
|
49
|
+
/** DCURSED_DEBUG=<file> makes the plugin say what it does there: how to tell it is loaded and what it saw. @param {string} line */
|
|
50
|
+
const debug = (line) => {
|
|
51
|
+
const file = process.env.DCURSED_DEBUG;
|
|
52
|
+
if (file) { try { appendFileSync(file, `${new Date().toISOString()} ${line}\n`); } catch { /* not worth failing for */ } }
|
|
53
|
+
};
|
|
54
|
+
debug(`loaded: ${agent} -> ${server}, repository ${root}`);
|
|
55
|
+
|
|
56
|
+
/** The plan each session follows, and the rev up to which its agent has been told. @type {Map<string, { plan: string, rev: number }>} */
|
|
57
|
+
const sessions = new Map();
|
|
58
|
+
/** When each session last did something: the one to give a turn to is the one that was working. @type {Map<string, number>} */
|
|
59
|
+
const lastAt = new Map();
|
|
60
|
+
/** Subagents' sessions, which are never given a turn of their own. @type {Set<string>} */
|
|
61
|
+
const children = new Set();
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {string} path @param {RequestInit} [init]
|
|
65
|
+
* @returns {Promise<{ status: number, text: string, headers: Headers } | null>} null: the server is not there
|
|
66
|
+
*/
|
|
67
|
+
async function call(path, init = {}) {
|
|
68
|
+
try {
|
|
69
|
+
const res = await fetch(server + path, {
|
|
70
|
+
...init,
|
|
71
|
+
headers: { 'x-dcursed-agent': agent, ...(init.body ? { 'content-type': 'application/json' } : {}) },
|
|
72
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
73
|
+
});
|
|
74
|
+
return { status: res.status, text: await res.text(), headers: res.headers };
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The plan's brief, and which plan and rev it is about. @param {string} [byName] a plan to ask about by name */
|
|
81
|
+
async function brief(byName = only) {
|
|
82
|
+
const res = await call(`/api/brief${byName ? `?plan=${encodeURIComponent(byName)}` : ''}`);
|
|
83
|
+
if (!res || res.status !== 200 || !res.text) return null;
|
|
84
|
+
const plan = res.headers.get('x-dcursed-plan');
|
|
85
|
+
const rev = Number(res.headers.get('x-dcursed-rev'));
|
|
86
|
+
return { text: res.text, plan: plan ?? undefined, rev: Number.isInteger(rev) ? rev : 0 };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @param {string} sessionID */
|
|
90
|
+
async function follow(sessionID) {
|
|
91
|
+
const known = sessions.get(sessionID);
|
|
92
|
+
if (known) return known;
|
|
93
|
+
const b = await brief();
|
|
94
|
+
if (!b?.plan) return null;
|
|
95
|
+
const state = { plan: b.plan, rev: b.rev };
|
|
96
|
+
sessions.set(sessionID, state);
|
|
97
|
+
return state;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The repository-relative files a tool call wrote. @param {string} tool @param {any} args @returns {string[]} */
|
|
101
|
+
function filesOf(tool, args) {
|
|
102
|
+
if (!args || typeof args !== 'object') return [];
|
|
103
|
+
/** @type {string[]} */
|
|
104
|
+
const found = [];
|
|
105
|
+
for (const key of FILE_ARGS) if (typeof args[key] === 'string') found.push(args[key]);
|
|
106
|
+
if (typeof args.patchText === 'string') for (const m of args.patchText.matchAll(PATCH_FILE)) found.push(m[1]);
|
|
107
|
+
if (!found.length || !/edit|write|patch/i.test(tool)) return [];
|
|
108
|
+
return found
|
|
109
|
+
.map((f) => (isAbsolute(f) ? relative(root, f) : f.replace(/^\.\//, '')))
|
|
110
|
+
.filter((f) => f && !f.startsWith('..') && !isAbsolute(f));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @param {string} plan @param {string} file @returns {Promise<string | null>} a line for the agent, or nothing */
|
|
114
|
+
async function observe(plan, file) {
|
|
115
|
+
const res = await call(`/api/plans/${encodeURIComponent(plan)}/observe`, { method: 'POST', body: JSON.stringify({ file }) });
|
|
116
|
+
if (!res || res.status !== 200) return null;
|
|
117
|
+
const r = JSON.parse(res.text);
|
|
118
|
+
return r.kind === 'started' ? `Marked ${r.step} in progress, because you edited ${file} and only ${r.step} lists it. Call step_done when it is finished.` : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** What others changed since this session's agent last heard, and move the mark. @param {{ plan: string, rev: number }} s */
|
|
122
|
+
async function changes(s) {
|
|
123
|
+
const res = await call(`/api/plans/${encodeURIComponent(s.plan)}/changes?since=${s.rev}¬=${encodeURIComponent(agent)}`);
|
|
124
|
+
if (!res || res.status !== 200) return { lines: [], attention: [] };
|
|
125
|
+
const r = JSON.parse(res.text);
|
|
126
|
+
s.rev = r.rev;
|
|
127
|
+
return { lines: /** @type {string[]} */ (r.lines), attention: /** @type {string[]} */ (r.attention ?? []) };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Run a hook so that nothing it does can reach the agent. @template {any[]} A @param {(...args: A) => Promise<void>} fn */
|
|
131
|
+
const quietly = (fn) => async (/** @type {A} */ ...args) => { try { await fn(...args); } catch { /* the plan is a help, never a hazard */ } };
|
|
132
|
+
|
|
133
|
+
// Presence: which of this agent's sessions are busy (a subagent is a session of its own), the
|
|
134
|
+
// permissions it is waiting for you to give, and the last thing that went wrong. Told to the server
|
|
135
|
+
// whenever it changes, and every HEARTBEAT_MS while busy or waiting.
|
|
136
|
+
/** @type {Set<string>} */
|
|
137
|
+
const busy = new Set();
|
|
138
|
+
/** Permission requests not answered yet, by id: the session that asked, and what for. @type {Map<string, { session: string, what: string }>} */
|
|
139
|
+
const asks = new Map();
|
|
140
|
+
/** @type {string | undefined} */
|
|
141
|
+
let trouble;
|
|
142
|
+
/** @type {ReturnType<typeof setInterval> | undefined} */
|
|
143
|
+
let beat;
|
|
144
|
+
// The plan of the session that did something last: after `/new` that is the new one, not the first this OpenCode saw.
|
|
145
|
+
const followed = () => only ?? [...sessions].filter(([id]) => !children.has(id))
|
|
146
|
+
.sort(([a], [b]) => (lastAt.get(b) ?? 0) - (lastAt.get(a) ?? 0))[0]?.[1].plan;
|
|
147
|
+
|
|
148
|
+
/** A session that stopped has nothing left to ask: its prompts went with it. @param {string} sid */
|
|
149
|
+
const dropAsks = (sid) => { for (const [id, a] of asks) if (a.session === sid) asks.delete(id); };
|
|
150
|
+
|
|
151
|
+
/** What was last told, so a session that says `busy` at every step of its loop is told once. */
|
|
152
|
+
let told = '';
|
|
153
|
+
let toldAt = 0;
|
|
154
|
+
|
|
155
|
+
/** @param {string} [detail] @param {boolean} [heartbeat] */
|
|
156
|
+
function tell(detail, heartbeat = false) {
|
|
157
|
+
const asking = [...asks.values()][0]?.what;
|
|
158
|
+
const state = trouble ? 'error' : asking ? 'asking' : busy.size ? 'working' : 'idle';
|
|
159
|
+
const note = trouble ?? asking ?? detail;
|
|
160
|
+
const key = `${state}|${note ?? ''}|${followed() ?? ''}`;
|
|
161
|
+
const alive = busy.size > 0 || asks.size > 0;
|
|
162
|
+
if (alive && !beat) {
|
|
163
|
+
beat = setInterval(() => tell(undefined, true), HEARTBEAT_MS);
|
|
164
|
+
// A heartbeat never keeps a process alive: it is only worth sending while the agent is.
|
|
165
|
+
/** @type {any} */ (beat).unref?.();
|
|
166
|
+
}
|
|
167
|
+
if (!alive && beat) { clearInterval(beat); beat = undefined; }
|
|
168
|
+
if (!heartbeat && key === told && Date.now() - toldAt < HEARTBEAT_MS) return;
|
|
169
|
+
told = key;
|
|
170
|
+
toldAt = Date.now();
|
|
171
|
+
debug(`presence: ${state}${note ? ` (${note})` : ''}`);
|
|
172
|
+
// OpenCode's own process: an agent asking you in a window that was then closed is not believed for long.
|
|
173
|
+
void call('/api/presence', { method: 'POST', body: JSON.stringify({ state, plan: followed(), ...(note ? { detail: note } : {}), pid: process.pid }) });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ── calls: what you did in the editor that this agent should act on now ──────────────────────
|
|
177
|
+
const stopped = new AbortController();
|
|
178
|
+
const wakeOn = options.wake !== false && process.env.DCURSED_WAKE !== '0' && Boolean(client)
|
|
179
|
+
// `opencode run` is one turn and then exits: there is no session to come back to.
|
|
180
|
+
&& !process.argv.slice(1).includes('run');
|
|
181
|
+
const hold = Number(options.holdMs) > 0 ? Number(options.holdMs) : HOLD_MS;
|
|
182
|
+
|
|
183
|
+
/** @param {number} ms */
|
|
184
|
+
const pause = (ms) => new Promise((resolve) => {
|
|
185
|
+
const t = setTimeout(resolve, ms);
|
|
186
|
+
/** @type {any} */ (t).unref?.();
|
|
187
|
+
stopped.signal.addEventListener('abort', () => { clearTimeout(t); resolve(undefined); }, { once: true });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
/** The session to give the call to: the latest on its plan, else the latest at all. Never a subagent. @param {{ plan: string }} c */
|
|
191
|
+
function sessionFor(c) {
|
|
192
|
+
const mine = [...lastAt.keys()].filter((id) => !children.has(id));
|
|
193
|
+
const onPlan = mine.filter((id) => sessions.get(id)?.plan === c.plan);
|
|
194
|
+
return (onPlan.length ? onPlan : mine).sort((a, b) => (lastAt.get(b) ?? 0) - (lastAt.get(a) ?? 0))[0];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Start a turn with the call's words, and tell the plan server how it went. A stop is the other way round: the
|
|
199
|
+
* turn under way on that plan is aborted, now.
|
|
200
|
+
* @param {{ id: string, plan: string, text: string, kind?: string }} c
|
|
201
|
+
*/
|
|
202
|
+
async function deliver(c) {
|
|
203
|
+
let state = 'failed';
|
|
204
|
+
/** @type {string | undefined} */
|
|
205
|
+
let detail;
|
|
206
|
+
try {
|
|
207
|
+
const sid = sessionFor(c);
|
|
208
|
+
const api = /** @type {any} */ (client);
|
|
209
|
+
if (c.kind === 'stopped') {
|
|
210
|
+
const mine = [...lastAt.keys()].filter((id) => !children.has(id) && sessions.get(id)?.plan === c.plan);
|
|
211
|
+
const targets = mine.length ? mine : sid ? [sid] : [];
|
|
212
|
+
const done = [];
|
|
213
|
+
for (const id of targets) {
|
|
214
|
+
const r = await api.session.abort({ path: { id } });
|
|
215
|
+
if (!r?.error) done.push(id);
|
|
216
|
+
else detail = String(r.error?.data?.message ?? r.error?.message ?? JSON.stringify(r.error)).slice(0, 200);
|
|
217
|
+
}
|
|
218
|
+
if (done.length || !targets.length) { state = 'delivered'; detail = done.length ? `aborted session ${done.join(', ')}` : 'no session was running'; }
|
|
219
|
+
} else if (sid) {
|
|
220
|
+
const prompt = (/** @type {boolean} */ withAgent) => api.session.promptAsync({ path: { id: sid }, body: { parts: [{ type: 'text', text: c.text }], ...(withAgent ? { agent: agent.slice('opencode:'.length) } : {}) } });
|
|
221
|
+
// The agent the door names, so a session left in plan mode can do the work; any agent if that one is not there.
|
|
222
|
+
let r = await prompt(true);
|
|
223
|
+
if (r?.error) r = await prompt(false);
|
|
224
|
+
if (r?.error) detail = String(r.error?.data?.message ?? r.error?.message ?? JSON.stringify(r.error)).slice(0, 200);
|
|
225
|
+
else { state = 'delivered'; detail = `session ${sid}`; }
|
|
226
|
+
} else {
|
|
227
|
+
// No session yet in this OpenCode: put the words in the TUI's prompt, as if typed, and send them.
|
|
228
|
+
const a = await api.tui.appendPrompt({ body: { text: c.text } });
|
|
229
|
+
const b = a?.error || a?.data === false ? a : await api.tui.submitPrompt();
|
|
230
|
+
if (b?.error || b?.data === false) detail = 'no session to start, and no TUI took the words';
|
|
231
|
+
else { state = 'delivered'; detail = 'typed into the TUI'; }
|
|
232
|
+
}
|
|
233
|
+
} catch (e) {
|
|
234
|
+
detail = String(/** @type {any} */ (e)?.message ?? e).slice(0, 200);
|
|
235
|
+
}
|
|
236
|
+
debug(`call ${c.id}: ${state}${detail ? ` (${detail})` : ''}`);
|
|
237
|
+
await call(`/api/agent/calls/${encodeURIComponent(c.id)}`, { method: 'POST', body: JSON.stringify({ state, ...(detail ? { detail } : {}) }) });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Keep one request open for a call, for as long as OpenCode runs. A server that is not there is asked again later. */
|
|
241
|
+
async function listen() {
|
|
242
|
+
let wait = 0;
|
|
243
|
+
while (!stopped.signal.aborted) {
|
|
244
|
+
if (wait) await pause(wait);
|
|
245
|
+
if (stopped.signal.aborted) return;
|
|
246
|
+
/** @type {Response | null} */
|
|
247
|
+
let res = null;
|
|
248
|
+
try {
|
|
249
|
+
res = await fetch(`${server}/api/agent/wait?hold=${hold}`, {
|
|
250
|
+
method: 'POST',
|
|
251
|
+
headers: { 'x-dcursed-agent': agent, 'content-type': 'application/json' },
|
|
252
|
+
body: JSON.stringify({ plans: [...new Set([...sessions.values()].map((s) => s.plan))] }),
|
|
253
|
+
signal: AbortSignal.any([stopped.signal, AbortSignal.timeout(hold + 10_000)]),
|
|
254
|
+
});
|
|
255
|
+
} catch { /* down, or stopped */ }
|
|
256
|
+
if (!res) { wait = Math.min(Math.max(wait * 2, 2000), 30_000); continue; }
|
|
257
|
+
if (res.status === 204) { wait = 0; continue; }
|
|
258
|
+
// An older plan server, without calls: ask now and then, in case it is restarted as a newer one.
|
|
259
|
+
if (res.status !== 200) { wait = 60_000; continue; }
|
|
260
|
+
wait = 0;
|
|
261
|
+
try { const { call: c } = await res.json(); if (c?.id) await deliver(c); } catch { /* the plan is a help, never a hazard */ }
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (wakeOn) { debug('waiting for calls from the editor'); void listen(); }
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
dispose: async () => { stopped.abort(); if (beat) clearInterval(beat); beat = undefined; },
|
|
268
|
+
|
|
269
|
+
// What OpenCode says about its sessions. (Logged with DCURSED_DEBUG, to learn what arrives.)
|
|
270
|
+
event: quietly(async ({ event }) => {
|
|
271
|
+
const e = /** @type {any} */ (event);
|
|
272
|
+
const sid = e.properties?.sessionID;
|
|
273
|
+
if (e.type !== 'message.part.updated' && e.type !== 'message.part.delta' && e.type !== 'file.watcher.updated') debug(`event ${e.type} ${JSON.stringify(e.properties ?? {}).slice(0, 160)}`);
|
|
274
|
+
const info = e.properties?.info;
|
|
275
|
+
if ((e.type === 'session.created' || e.type === 'session.updated') && info?.id && info.parentID) children.add(info.id);
|
|
276
|
+
if (e.type === 'session.deleted' && info?.id) { sessions.delete(info.id); lastAt.delete(info.id); children.delete(info.id); dropAsks(info.id); }
|
|
277
|
+
// A permission prompt waiting for you: `permission.asked` (OpenCode's current events) names the permission,
|
|
278
|
+
// `permission.updated` (its older ones) the type. Only that goes on, never the command or the path.
|
|
279
|
+
if ((e.type === 'permission.asked' || e.type === 'permission.updated') && typeof e.properties?.id === 'string' && sid) {
|
|
280
|
+
const what = String(e.properties.permission ?? e.properties.type ?? 'a tool');
|
|
281
|
+
asks.set(e.properties.id, { session: sid, what: `OpenCode needs your permission to use ${what}`.slice(0, 200) });
|
|
282
|
+
tell();
|
|
283
|
+
} else if (e.type === 'permission.replied') {
|
|
284
|
+
const id = e.properties?.requestID ?? e.properties?.permissionID;
|
|
285
|
+
if (asks.delete(id)) tell();
|
|
286
|
+
}
|
|
287
|
+
if (e.type === 'session.status' && sid) {
|
|
288
|
+
const type = e.properties.status?.type;
|
|
289
|
+
if (type === 'busy' || type === 'retry') { busy.add(sid); trouble = undefined; } else busy.delete(sid);
|
|
290
|
+
tell(type === 'retry' ? String(e.properties.status.message ?? 'retrying') : undefined);
|
|
291
|
+
} else if (e.type === 'session.idle' && sid) {
|
|
292
|
+
busy.delete(sid);
|
|
293
|
+
dropAsks(sid);
|
|
294
|
+
tell();
|
|
295
|
+
} else if (e.type === 'session.error') {
|
|
296
|
+
if (sid) { busy.delete(sid); dropAsks(sid); }
|
|
297
|
+
const err = e.properties?.error;
|
|
298
|
+
trouble = String(err?.data?.message ?? err?.name ?? 'the session ended with an error').slice(0, 200);
|
|
299
|
+
tell();
|
|
300
|
+
}
|
|
301
|
+
}),
|
|
302
|
+
|
|
303
|
+
'experimental.chat.system.transform': quietly(async (input, output) => {
|
|
304
|
+
const b = await brief();
|
|
305
|
+
if (input.sessionID) lastAt.set(input.sessionID, Date.now());
|
|
306
|
+
debug(`request ${input.sessionID ?? '?'}: ${b ? `brief of ${b.plan ?? 'several plans'} at rev ${b.rev}` : 'no brief'}`);
|
|
307
|
+
if (!b) return;
|
|
308
|
+
output.system.push(b.text);
|
|
309
|
+
// The brief is the state as of now; changes are told from here.
|
|
310
|
+
if (b.plan && input.sessionID && !sessions.has(input.sessionID)) sessions.set(input.sessionID, { plan: b.plan, rev: b.rev });
|
|
311
|
+
}),
|
|
312
|
+
|
|
313
|
+
'tool.execute.after': quietly(async (input, output) => {
|
|
314
|
+
// A dcursed tool names the plan it is about: that is the plan this session is on, whatever
|
|
315
|
+
// else the repository holds.
|
|
316
|
+
lastAt.set(input.sessionID, Date.now());
|
|
317
|
+
// Its reply ends with the plan's id, which plan_create only learns when it is made.
|
|
318
|
+
const footer = input.tool.startsWith('dcursed_') ? FOOTER.exec(String(output.output ?? '')) : null;
|
|
319
|
+
const named = input.tool.startsWith('dcursed_') ? (typeof input.args?.plan === 'string' ? input.args.plan : footer?.[1]) : undefined;
|
|
320
|
+
if (named && sessions.get(input.sessionID)?.plan !== named) {
|
|
321
|
+
const b = await brief(named);
|
|
322
|
+
sessions.set(input.sessionID, { plan: named, rev: b?.rev ?? 0 });
|
|
323
|
+
}
|
|
324
|
+
const s = await follow(input.sessionID);
|
|
325
|
+
debug(`${input.tool} ${JSON.stringify(input.args ?? {}).slice(0, 120)} -> ${s ? `following ${s.plan} from rev ${s.rev}` : 'no plan'}`);
|
|
326
|
+
if (!s) return;
|
|
327
|
+
/** @type {string[]} */
|
|
328
|
+
const notes = [];
|
|
329
|
+
for (const file of filesOf(input.tool, input.args)) {
|
|
330
|
+
const note = await observe(s.plan, file);
|
|
331
|
+
if (note) notes.push(note);
|
|
332
|
+
}
|
|
333
|
+
const { lines, attention } = await changes(s);
|
|
334
|
+
// The dcursed tools say what changed at the end of their own replies.
|
|
335
|
+
if (!input.tool.startsWith('dcursed_')) {
|
|
336
|
+
if (attention.length) notes.unshift(`HEADS UP, about your steps:\n${attention.map((a) => `- ${a}`).join('\n')}`);
|
|
337
|
+
if (lines.length) {
|
|
338
|
+
notes.push(`The plan changed since you last looked (the person, or another agent):\n${lines.map((l) => `- ${l}`).join('\n')}\nRead plan_read before your next step if this touches your work.`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (notes.length) {
|
|
342
|
+
debug(` told the agent: ${notes.join(' | ').replace(/\s+/g, ' ').slice(0, 300)}`);
|
|
343
|
+
output.output = `${output.output}\n\n[DiaCode] ${notes.join('\n\n')}`;
|
|
344
|
+
}
|
|
345
|
+
}),
|
|
346
|
+
|
|
347
|
+
// So the plan survives the conversation being summarised.
|
|
348
|
+
'experimental.session.compacting': quietly(async (_input, output) => {
|
|
349
|
+
const b = await brief();
|
|
350
|
+
if (b) output.context.push(b.text);
|
|
351
|
+
}),
|
|
352
|
+
};
|
|
353
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diacode",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A plan you and your coding agents share: a diagram in your repository that you edit by hand, with each step showing who has it and how that is known. For OpenCode, Claude Code, Cursor and Antigravity.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"diacode": "bin/dcursed.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"editor",
|
|
12
|
+
"opencode-plugin.js",
|
|
13
|
+
"README.md",
|
|
14
|
+
"CHANGELOG.md",
|
|
15
|
+
"THIRD-PARTY-NOTICES.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"plan",
|
|
23
|
+
"diagram",
|
|
24
|
+
"coding-agents",
|
|
25
|
+
"agents",
|
|
26
|
+
"mcp",
|
|
27
|
+
"claude-code",
|
|
28
|
+
"opencode",
|
|
29
|
+
"cursor",
|
|
30
|
+
"antigravity",
|
|
31
|
+
"planning"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT"
|
|
34
|
+
}
|