opencode-acp-client 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 +202 -0
- package/README.md +220 -2
- package/README.zh.md +199 -0
- package/dist/acp/capabilities.d.ts +168 -0
- package/dist/acp/capabilities.js +240 -0
- package/dist/acp/capabilities.js.map +1 -0
- package/dist/acp/client.d.ts +241 -0
- package/dist/acp/client.js +767 -0
- package/dist/acp/client.js.map +1 -0
- package/dist/acp/types.d.ts +228 -0
- package/dist/acp/types.js +23 -0
- package/dist/acp/types.js.map +1 -0
- package/dist/acp-replay.d.ts +60 -0
- package/dist/acp-replay.js +188 -0
- package/dist/acp-replay.js.map +1 -0
- package/dist/contract.d.ts +435 -0
- package/dist/contract.js +126 -0
- package/dist/contract.js.map +1 -0
- package/dist/daemons.d.ts +39 -0
- package/dist/daemons.js +112 -0
- package/dist/daemons.js.map +1 -0
- package/dist/index.d.ts +268 -0
- package/dist/index.js +1882 -0
- package/dist/index.js.map +1 -0
- package/dist/opencode-bin.d.ts +73 -0
- package/dist/opencode-bin.js +109 -0
- package/dist/opencode-bin.js.map +1 -0
- package/dist/project.d.ts +81 -0
- package/dist/project.js +230 -0
- package/dist/project.js.map +1 -0
- package/dist/server.d.ts +743 -0
- package/dist/server.js +5029 -0
- package/dist/server.js.map +1 -0
- package/dist/session-map.d.ts +89 -0
- package/dist/session-map.js +305 -0
- package/dist/session-map.js.map +1 -0
- package/dist/sessions-command.d.ts +70 -0
- package/dist/sessions-command.js +188 -0
- package/dist/sessions-command.js.map +1 -0
- package/dist/sse.d.ts +40 -0
- package/dist/sse.js +147 -0
- package/dist/sse.js.map +1 -0
- package/dist/state.d.ts +161 -0
- package/dist/state.js +135 -0
- package/dist/state.js.map +1 -0
- package/dist/subagent-sessions.d.ts +71 -0
- package/dist/subagent-sessions.js +107 -0
- package/dist/subagent-sessions.js.map +1 -0
- package/dist/translate/events.d.ts +231 -0
- package/dist/translate/events.js +1244 -0
- package/dist/translate/events.js.map +1 -0
- package/dist/upgrade.d.ts +56 -0
- package/dist/upgrade.js +162 -0
- package/dist/upgrade.js.map +1 -0
- package/package.json +66 -4
package/dist/sse.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `/api/event` SSE hub.
|
|
3
|
+
*
|
|
4
|
+
* Contract (observed, including the handshake the client waits for):
|
|
5
|
+
* frame := `data: ` + JSON + `\n\n`
|
|
6
|
+
* first := {"id":"evt_connected","type":"server.connected","data":{}}
|
|
7
|
+
*
|
|
8
|
+
* A bounded ring is kept so a reconnecting client can replay what it missed —
|
|
9
|
+
* without it, a client that attaches a moment after a session is created never
|
|
10
|
+
* learns the session exists until the next event happens to arrive.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* How often an idle stream is nudged. Observed: with no traffic at all the
|
|
14
|
+
* connection was reaped after roughly 45s, and each reap cost a full client
|
|
15
|
+
* re-bootstrap. 15s leaves plenty of margin.
|
|
16
|
+
*/
|
|
17
|
+
const KEEPALIVE_MS = 15_000;
|
|
18
|
+
export class SseHub {
|
|
19
|
+
seq = 0;
|
|
20
|
+
ring = [];
|
|
21
|
+
clients = new Set();
|
|
22
|
+
capacity;
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
this.capacity = options.replayCapacity ?? 512;
|
|
25
|
+
}
|
|
26
|
+
trace(line) {
|
|
27
|
+
if (process.env['OPENCODE_ACP_CLIENT_MESSAGE_PROBE'] === '1') {
|
|
28
|
+
console.error(`[sse] ${line}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Build the Response for `GET /api/event`. */
|
|
32
|
+
connect(opts = {}) {
|
|
33
|
+
const encoder = new TextEncoder();
|
|
34
|
+
let controller = null;
|
|
35
|
+
let keepalive;
|
|
36
|
+
const stopKeepalive = () => {
|
|
37
|
+
if (keepalive !== undefined) {
|
|
38
|
+
clearInterval(keepalive);
|
|
39
|
+
keepalive = undefined;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const client = {
|
|
43
|
+
send: (event) => {
|
|
44
|
+
try {
|
|
45
|
+
controller?.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// The client vanished mid-write; drop it rather than crash the hub.
|
|
49
|
+
this.clients.delete(client);
|
|
50
|
+
stopKeepalive();
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
close: () => {
|
|
54
|
+
stopKeepalive();
|
|
55
|
+
try {
|
|
56
|
+
controller?.close();
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
/* already closed */
|
|
60
|
+
}
|
|
61
|
+
this.clients.delete(client);
|
|
62
|
+
this.trace(`client closed (clients=${this.clients.size})`);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
const stream = new ReadableStream({
|
|
66
|
+
start: (c) => {
|
|
67
|
+
controller = c;
|
|
68
|
+
this.clients.add(client);
|
|
69
|
+
this.trace(`client connected (clients=${this.clients.size}, lastEventId=${opts.lastEventId ?? 'none'})`);
|
|
70
|
+
// Handshake first: the TUI treats it as "the server is live".
|
|
71
|
+
client.send({ id: 'evt_connected', type: 'server.connected', data: {}, created: Date.now() });
|
|
72
|
+
for (const missed of this.since(opts.lastEventId)) {
|
|
73
|
+
client.send(missed);
|
|
74
|
+
}
|
|
75
|
+
// Keep the stream warm. With no events to send, an idle SSE connection
|
|
76
|
+
// gets reaped by the HTTP stack, and the client answers a closed stream
|
|
77
|
+
// with a full re-bootstrap: it re-fetches every message and reconciles
|
|
78
|
+
// the store, which drops the turn the user is looking at - the reported
|
|
79
|
+
// "the newest exchange disappears after a while". A comment frame is
|
|
80
|
+
// ignored by the client and costs nothing.
|
|
81
|
+
keepalive = setInterval(() => {
|
|
82
|
+
try {
|
|
83
|
+
controller?.enqueue(encoder.encode(': keepalive\n\n'));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
/* the stream is gone; cancel() clears the timer */
|
|
87
|
+
}
|
|
88
|
+
}, KEEPALIVE_MS);
|
|
89
|
+
keepalive.unref?.();
|
|
90
|
+
},
|
|
91
|
+
cancel: () => {
|
|
92
|
+
stopKeepalive();
|
|
93
|
+
this.clients.delete(client);
|
|
94
|
+
this.trace(`client cancelled (clients=${this.clients.size})`);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
return new Response(stream, {
|
|
98
|
+
headers: {
|
|
99
|
+
'content-type': 'text/event-stream',
|
|
100
|
+
'cache-control': 'no-cache, no-transform',
|
|
101
|
+
connection: 'keep-alive',
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** Broadcast an event. Returns it with its assigned id. */
|
|
106
|
+
emit(event) {
|
|
107
|
+
const framed = {
|
|
108
|
+
...event,
|
|
109
|
+
id: event.id ?? `evt_${++this.seq}`,
|
|
110
|
+
created: event.created ?? Date.now(),
|
|
111
|
+
};
|
|
112
|
+
this.ring.push(framed);
|
|
113
|
+
if (this.ring.length > this.capacity)
|
|
114
|
+
this.ring.shift();
|
|
115
|
+
// ★ Which session did this event name? Measured while chasing a report that a
|
|
116
|
+
// brand-new tab showed a message belonging to an older one: the bridge log proves
|
|
117
|
+
// every request reached the right session, but it says nothing about the events we
|
|
118
|
+
// EMIT, so "the bridge published the wrong sessionID" and "the client drew it in the
|
|
119
|
+
// active tab" look identical. `OPENCODE_ACP_CLIENT_EVENT_LOG=1` closes that gap by
|
|
120
|
+
// recording the addressed session for every event; TRACE stays as it was (the
|
|
121
|
+
// payload head) for the cases that need the body.
|
|
122
|
+
if (process.env['OPENCODE_ACP_CLIENT_EVENT_LOG'] === '1') {
|
|
123
|
+
const sid = framed.data?.sessionID;
|
|
124
|
+
console.error(`[sse] ${framed.type} sid=${typeof sid === 'string' ? sid : '-'} -> ${this.clients.size} client(s)`);
|
|
125
|
+
}
|
|
126
|
+
if (process.env.OPENCODE_ACP_CLIENT_TRACE) {
|
|
127
|
+
console.error(`[sse] ${framed.type} -> ${this.clients.size} client(s) ${JSON.stringify(framed.data).slice(0, 300)}`);
|
|
128
|
+
}
|
|
129
|
+
for (const client of [...this.clients])
|
|
130
|
+
client.send(framed);
|
|
131
|
+
return framed;
|
|
132
|
+
}
|
|
133
|
+
get clientCount() {
|
|
134
|
+
return this.clients.size;
|
|
135
|
+
}
|
|
136
|
+
closeAll() {
|
|
137
|
+
for (const client of [...this.clients])
|
|
138
|
+
client.close();
|
|
139
|
+
}
|
|
140
|
+
since(lastEventId) {
|
|
141
|
+
if (!lastEventId)
|
|
142
|
+
return [];
|
|
143
|
+
const index = this.ring.findIndex((e) => e.id === lastEventId);
|
|
144
|
+
return index < 0 ? [] : this.ring.slice(index + 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH;;;;GAIG;AACH,MAAM,YAAY,GAAG,MAAM,CAAC;AAY5B,MAAM,OAAO,MAAM;IACT,GAAG,GAAG,CAAC,CAAC;IACC,IAAI,GAAkB,EAAE,CAAC;IACzB,OAAO,GAAG,IAAI,GAAG,EAAa,CAAC;IAC/B,QAAQ,CAAS;IAElC,YAAY,UAAyB,EAAE;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,cAAc,IAAI,GAAG,CAAC;IAChD,CAAC;IAEO,KAAK,CAAC,IAAY;QACxB,IAAI,OAAO,CAAC,GAAG,CAAC,mCAAmC,CAAC,KAAK,GAAG,EAAE,CAAC;YAC7D,OAAO,CAAC,KAAK,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IAED,+CAA+C;IAC/C,OAAO,CAAC,OAAiC,EAAE;QACzC,MAAM,OAAO,GAAG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I,WAAW;QACb,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;IAED,QAAQ;QACN,KAAK,MAAM,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC;YAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IACzD,CAAC;IAEO,KAAK,CAAC,WAA+B;QAC3C,IAAI,CAAC,WAAW;YAAE,OAAO,EAAE,CAAC;QAC5B,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,WAAW,CAAC,CAAC;QAC/D,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IACrD,CAAC;CACF"}
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bridge state: sessions, messages and parts.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately small. The ACP agent owns the authoritative transcript; this is
|
|
5
|
+
* only what the opencode surface needs to answer its reads (`/api/session`,
|
|
6
|
+
* `/api/session/:id/message`, …) between events.
|
|
7
|
+
*
|
|
8
|
+
* One subtlety worth stating: an ACP agent only ever tells us about *its*
|
|
9
|
+
* session id. opencode's surface is keyed the same way, so we use the agent's
|
|
10
|
+
* id directly and keep a reverse map for the paths that arrive before the id is
|
|
11
|
+
* known.
|
|
12
|
+
*/
|
|
13
|
+
import type { Location, Message, Part, Session } from './contract.js';
|
|
14
|
+
export interface SessionRecord {
|
|
15
|
+
session: Session;
|
|
16
|
+
messages: Message[];
|
|
17
|
+
parts: Map<string, Part[]>;
|
|
18
|
+
/** ACP side id, once `session/new` answered. */
|
|
19
|
+
acpSessionId?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The agent command that owns this session, when it is not the one this bridge was
|
|
22
|
+
* launched with. A tab restored from a session created by `opencode acp` must be
|
|
23
|
+
* reattached with THAT agent — ACP has no notion of which agent a session belongs to,
|
|
24
|
+
* so the session map records it and the adoption path binds the record to it here.
|
|
25
|
+
* Undefined means "this bridge's own agent".
|
|
26
|
+
*/
|
|
27
|
+
agent?: {
|
|
28
|
+
command: string;
|
|
29
|
+
args: string[];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The shell `ensureSession` creates so the client has a session route before the user
|
|
33
|
+
* types anything. It is not a conversation: `POST /api/session` adopts it (re-keyed to
|
|
34
|
+
* the id the client chose) instead of leaving an empty duplicate row and a second agent
|
|
35
|
+
* behind — measured: a fresh TUI conversation used to end with two sessions and two
|
|
36
|
+
* agent processes, one of them an empty "New session" titled after the directory.
|
|
37
|
+
* Cleared the moment a turn starts, so a shell that has been used is never adopted.
|
|
38
|
+
*/
|
|
39
|
+
precreated?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Where this session's title came from. Kept so the fallback title (the first
|
|
42
|
+
* user message, truncated) can never overwrite something better:
|
|
43
|
+
*
|
|
44
|
+
* - `titleFromAgent` — the agent said it (`session_info_update`, `session/list`);
|
|
45
|
+
* - `titleFromUser` — the client renamed it (PATCH / `/rename`);
|
|
46
|
+
* - `titleDerived` — we made it up from the first prompt.
|
|
47
|
+
*
|
|
48
|
+
* ACP has no client-side title method, and the plain ACP profile publishes no
|
|
49
|
+
* titles at all, so without the derived fallback every such session is stuck on
|
|
50
|
+
* the client's "New session". The agent's own title always wins when it arrives.
|
|
51
|
+
*/
|
|
52
|
+
titleFromAgent?: boolean;
|
|
53
|
+
titleFromUser?: boolean;
|
|
54
|
+
titleDerived?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* The title came back from the session map (or was restored by the client) rather
|
|
57
|
+
* than being the directory default. The first prompt must not replace it: measured
|
|
58
|
+
* 2026-10-20 — `-s ses_acp_…` showed the real title, then the first prompt renamed
|
|
59
|
+
* the tab and the sidebar to the prompt text, and only a restart restored it.
|
|
60
|
+
*/
|
|
61
|
+
titleRestored?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The ACP agent's slash commands, from `available_commands_update` — dsh advertises
|
|
64
|
+
* ~45, most of them "Run the <skill> skill". Served by `GET /api/command` so the TUI
|
|
65
|
+
* palette offers them, and run by `POST /api/session/:id/command` as a
|
|
66
|
+
* `/name args` prompt (the official ACP slash-commands contract: "Commands are
|
|
67
|
+
* included as regular user messages in prompt requests").
|
|
68
|
+
*/
|
|
69
|
+
commands?: Array<{
|
|
70
|
+
name: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
input?: {
|
|
73
|
+
hint?: string;
|
|
74
|
+
};
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* The workspace this session belongs to, taken from `?location[directory]=<path>`.
|
|
78
|
+
*
|
|
79
|
+
* Both clients send one. The TUI sends the directory it was started in, which is the
|
|
80
|
+
* bridge's own, so nothing changes for it; the desktop sends the workspace it is
|
|
81
|
+
* showing, which is the point. `cwd` is a required field of `session/new` in ACP, so a
|
|
82
|
+
* per-session directory is the protocol's own model — and the TUI also checks that a
|
|
83
|
+
* session's location matches the directory it is working in, which is why this must
|
|
84
|
+
* never disagree with what the client asked for.
|
|
85
|
+
*/
|
|
86
|
+
directory?: string;
|
|
87
|
+
/**
|
|
88
|
+
* `_meta` the agent has sent for this session, merged and never overwritten.
|
|
89
|
+
*
|
|
90
|
+
* ACP reserves `_meta` for extensions. opencode's HTTP schemas have no field
|
|
91
|
+
* for it, so it is kept here rather than invented into a surface that would
|
|
92
|
+
* ignore it — but it is not dropped either, and our own extensions must use
|
|
93
|
+
* keys that cannot collide with an agent's.
|
|
94
|
+
*/
|
|
95
|
+
meta?: Record<string, unknown>;
|
|
96
|
+
/** True while a prompt is in flight (drives `turn.wait`/`turn.idle`). */
|
|
97
|
+
busy: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Set when we ask the agent to stop (`/interrupt`, `/abort`), cleared when the
|
|
100
|
+
* next turn begins. The turn's *outcome* is reported from this rather than from
|
|
101
|
+
* the agent's stop reason alone: an agent that is cancelled mid-permission may
|
|
102
|
+
* still answer `end_turn`, and a turn the user cancelled must not be reported
|
|
103
|
+
* as one that finished on its own.
|
|
104
|
+
*/
|
|
105
|
+
interruptRequested?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* The model this session is on. Reported in `session.step.started` so the
|
|
108
|
+
* client's assistant row carries one — the sidebar's context meter looks it up
|
|
109
|
+
* by providerID/modelID and gives up without it.
|
|
110
|
+
*/
|
|
111
|
+
model?: {
|
|
112
|
+
providerID: string;
|
|
113
|
+
modelID: string;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* A resumed session's tape, read off disk on first use and then frozen. It is
|
|
117
|
+
* kept beside `messages` rather than copied into it: `messages` is the bridge's
|
|
118
|
+
* own append-ordered record of what it watched happen, this is what happened
|
|
119
|
+
* before the bridge was there.
|
|
120
|
+
*/
|
|
121
|
+
history?: Array<Record<string, unknown>>;
|
|
122
|
+
/**
|
|
123
|
+
* What the backend calls itself (`initialize`'s `agentInfo.name`).
|
|
124
|
+
*
|
|
125
|
+
* Used wherever opencode wants an agent or provider id. It is read from the
|
|
126
|
+
* backend rather than hardcoded so nothing here assumes which one it is.
|
|
127
|
+
*/
|
|
128
|
+
backendName?: string;
|
|
129
|
+
}
|
|
130
|
+
export declare class BridgeState {
|
|
131
|
+
private readonly sessions;
|
|
132
|
+
private seq;
|
|
133
|
+
newId(prefix: string): string;
|
|
134
|
+
/**
|
|
135
|
+
* Create a session, optionally under an id the caller already chose.
|
|
136
|
+
*
|
|
137
|
+
* opencode creates optimistically: it mints the id client-side, admits a local
|
|
138
|
+
* row, and only then POSTs it. Ignoring that id means the client looks up the
|
|
139
|
+
* session it thinks it made, misses, and follows with
|
|
140
|
+
* `GET /api/session/undefined`.
|
|
141
|
+
*/
|
|
142
|
+
createSession(location: Location, title?: string, requestedId?: string): SessionRecord;
|
|
143
|
+
/** Re-key a session under the id the client chose (see `precreated`). */
|
|
144
|
+
rekey(oldId: string, newId: string): SessionRecord | undefined;
|
|
145
|
+
get(id: string): SessionRecord | undefined;
|
|
146
|
+
all(): SessionRecord[];
|
|
147
|
+
touch(id: string): void;
|
|
148
|
+
addMessage(record: SessionRecord, message: Message): void;
|
|
149
|
+
getMessage(record: SessionRecord, messageId: string): Message | undefined;
|
|
150
|
+
partsOf(record: SessionRecord, messageId: string): Part[];
|
|
151
|
+
/**
|
|
152
|
+
* Append to a part, creating it if needed. Returns the part and whether it
|
|
153
|
+
* was newly created, so the caller can decide between `updated` and `delta`.
|
|
154
|
+
*/
|
|
155
|
+
upsertPart(record: SessionRecord, messageId: string, part: Part): {
|
|
156
|
+
part: Part;
|
|
157
|
+
created: boolean;
|
|
158
|
+
};
|
|
159
|
+
findPart(record: SessionRecord, partId: string): Part | undefined;
|
|
160
|
+
removeSession(id: string): void;
|
|
161
|
+
}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory bridge state: sessions, messages and parts.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately small. The ACP agent owns the authoritative transcript; this is
|
|
5
|
+
* only what the opencode surface needs to answer its reads (`/api/session`,
|
|
6
|
+
* `/api/session/:id/message`, …) between events.
|
|
7
|
+
*
|
|
8
|
+
* One subtlety worth stating: an ACP agent only ever tells us about *its*
|
|
9
|
+
* session id. opencode's surface is keyed the same way, so we use the agent's
|
|
10
|
+
* id directly and keep a reverse map for the paths that arrive before the id is
|
|
11
|
+
* known.
|
|
12
|
+
*/
|
|
13
|
+
export class BridgeState {
|
|
14
|
+
sessions = new Map();
|
|
15
|
+
seq = 0;
|
|
16
|
+
newId(prefix) {
|
|
17
|
+
return `${prefix}_${Date.now().toString(36)}${(++this.seq).toString(36)}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a session, optionally under an id the caller already chose.
|
|
21
|
+
*
|
|
22
|
+
* opencode creates optimistically: it mints the id client-side, admits a local
|
|
23
|
+
* row, and only then POSTs it. Ignoring that id means the client looks up the
|
|
24
|
+
* session it thinks it made, misses, and follows with
|
|
25
|
+
* `GET /api/session/undefined`.
|
|
26
|
+
*/
|
|
27
|
+
createSession(location, title, requestedId) {
|
|
28
|
+
const existing = requestedId ? this.sessions.get(requestedId) : undefined;
|
|
29
|
+
if (existing)
|
|
30
|
+
return existing;
|
|
31
|
+
const id = requestedId ?? this.newId('ses');
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
const session = {
|
|
34
|
+
id,
|
|
35
|
+
directory: location.directory,
|
|
36
|
+
time: { created: now, updated: now },
|
|
37
|
+
// The client reads session.location.directory directly; see SessionInfo.
|
|
38
|
+
location: { directory: location.directory, workspaceID: location.workspaceID },
|
|
39
|
+
projectID: location.project.id,
|
|
40
|
+
cost: 0,
|
|
41
|
+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
42
|
+
...(title ? { title } : {}),
|
|
43
|
+
};
|
|
44
|
+
const record = {
|
|
45
|
+
session,
|
|
46
|
+
messages: [],
|
|
47
|
+
parts: new Map(),
|
|
48
|
+
busy: false,
|
|
49
|
+
};
|
|
50
|
+
this.sessions.set(id, record);
|
|
51
|
+
return record;
|
|
52
|
+
}
|
|
53
|
+
/** Re-key a session under the id the client chose (see `precreated`). */
|
|
54
|
+
rekey(oldId, newId) {
|
|
55
|
+
const record = this.sessions.get(oldId);
|
|
56
|
+
if (!record || this.sessions.has(newId))
|
|
57
|
+
return undefined;
|
|
58
|
+
this.sessions.delete(oldId);
|
|
59
|
+
record.session.id = newId;
|
|
60
|
+
this.sessions.set(newId, record);
|
|
61
|
+
return record;
|
|
62
|
+
}
|
|
63
|
+
get(id) {
|
|
64
|
+
const direct = this.sessions.get(id);
|
|
65
|
+
if (direct)
|
|
66
|
+
return direct;
|
|
67
|
+
// Alias: the ACP agent's own id. The visible id is the bridge's (or whatever a
|
|
68
|
+
// launcher chose — an external launcher may pass the agent's own id straight
|
|
69
|
+
// through), and the agent's id is the other half of that pair. Accepting either
|
|
70
|
+
// is what makes "resume with the id you copied" work without knowing which one
|
|
71
|
+
// it is.
|
|
72
|
+
for (const record of this.sessions.values()) {
|
|
73
|
+
if (record.acpSessionId === id)
|
|
74
|
+
return record;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
all() {
|
|
79
|
+
return [...this.sessions.values()].sort((a, b) => {
|
|
80
|
+
// A record with no messages yet (the pre-created one, titled by the
|
|
81
|
+
// directory basename) must never outrank a real conversation as "newest"
|
|
82
|
+
// — that is exactly what made `-c` open a blank session while the
|
|
83
|
+
// imported one sat in the tab row. Emptiness first, then recency.
|
|
84
|
+
const aEmpty = a.messages.length === 0 ? 1 : 0;
|
|
85
|
+
const bEmpty = b.messages.length === 0 ? 1 : 0;
|
|
86
|
+
if (aEmpty !== bEmpty)
|
|
87
|
+
return aEmpty - bEmpty;
|
|
88
|
+
return b.session.time.updated - a.session.time.updated;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
touch(id) {
|
|
92
|
+
const record = this.sessions.get(id);
|
|
93
|
+
if (record)
|
|
94
|
+
record.session.time.updated = Date.now();
|
|
95
|
+
}
|
|
96
|
+
addMessage(record, message) {
|
|
97
|
+
record.messages.push(message);
|
|
98
|
+
if (!record.parts.has(message.id))
|
|
99
|
+
record.parts.set(message.id, []);
|
|
100
|
+
record.session.time.updated = Date.now();
|
|
101
|
+
}
|
|
102
|
+
getMessage(record, messageId) {
|
|
103
|
+
return record.messages.find((m) => m.id === messageId);
|
|
104
|
+
}
|
|
105
|
+
partsOf(record, messageId) {
|
|
106
|
+
return record.parts.get(messageId) ?? [];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Append to a part, creating it if needed. Returns the part and whether it
|
|
110
|
+
* was newly created, so the caller can decide between `updated` and `delta`.
|
|
111
|
+
*/
|
|
112
|
+
upsertPart(record, messageId, part) {
|
|
113
|
+
const list = record.parts.get(messageId) ?? [];
|
|
114
|
+
const index = list.findIndex((p) => p.id === part.id);
|
|
115
|
+
if (index < 0) {
|
|
116
|
+
list.push(part);
|
|
117
|
+
record.parts.set(messageId, list);
|
|
118
|
+
return { part, created: true };
|
|
119
|
+
}
|
|
120
|
+
list[index] = part;
|
|
121
|
+
return { part, created: false };
|
|
122
|
+
}
|
|
123
|
+
findPart(record, partId) {
|
|
124
|
+
for (const list of record.parts.values()) {
|
|
125
|
+
const hit = list.find((p) => p.id === partId);
|
|
126
|
+
if (hit)
|
|
127
|
+
return hit;
|
|
128
|
+
}
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
removeSession(id) {
|
|
132
|
+
this.sessions.delete(id);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA6GH,MAAM,OAAO,WAAW;IACL,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAC;IACrD,GAAG,GAAG,CAAC,CAAC;IAEhB,KAAK,CAAC,MAAc;QAClB,OAAO,GAAG,MAAM,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IAC5E,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,QAAkB,EAAE,KAAc,EAAE,WAAoB;QACpE,MAAM,QAAQ,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QAC1E,IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;QAC9B,MAAM,EAAE,GAAG,WAAW,IAAI,IAA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AC,IAAI,CAAC,CAAC;YAChB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAClC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACjC,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;QACnB,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAClC,CAAC;IAED,QAAQ,CAAC,MAAqB,EAAE,MAAc;QAC5C,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;YACzC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,MAAM,CAAC,CAAC;YAC9C,IAAI,GAAG;gBAAE,OAAO,GAAG,CAAC;QACtB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,aAAa,CAAC,EAAU;QACtB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `opencode/session/child_update` — opencode's own extension for a delegated run.
|
|
3
|
+
*
|
|
4
|
+
* ACP has an RFD for this ("Subagent Sessions", each child its own session id), but
|
|
5
|
+
* `opencode acp` ships this quieter negotiated form instead: announced as
|
|
6
|
+
* `agentCapabilities._meta["opencode/child-session-updates"]` and delivered as one
|
|
7
|
+
* notification per child event.
|
|
8
|
+
*
|
|
9
|
+
* { rootSessionId, childSessionId, parentSessionId, depth, title?,
|
|
10
|
+
* type: "update", update: <the child's own session/update> }
|
|
11
|
+
* | { …, type: "status", status: "created"|"running"|"completed"|"failed"|"interrupted" }
|
|
12
|
+
*
|
|
13
|
+
* ★ What it is NOT for: it does not create a second session. opencode's TUI would need a
|
|
14
|
+
* real session in the strip to open one, and a tab the user never opened is the wrong
|
|
15
|
+
* shape for a delegated run. It exists to put the child's ACTIVITY on the card that
|
|
16
|
+
* reports it — the two slots that component actually renders:
|
|
17
|
+
*
|
|
18
|
+
* • the caption (`input.description`, via `session.tool.called`) — the only free text;
|
|
19
|
+
* • the `Background` badge (`metadata.status`, via `session.tool.progress`), which the
|
|
20
|
+
* client shows when the card is `completed` but `metadata.status === "running"`
|
|
21
|
+
* (`isBackgroundSubagent`). A delegated run that returned its card while still
|
|
22
|
+
* working is exactly that case.
|
|
23
|
+
*
|
|
24
|
+
* Shape taken from `packages/cli/src/acp/event.ts` (`ChildSessionUpdate`); nothing here
|
|
25
|
+
* is invented.
|
|
26
|
+
*/
|
|
27
|
+
export type ChildStatus = 'created' | 'running' | 'completed' | 'failed' | 'interrupted';
|
|
28
|
+
export interface ChildSessionEvent {
|
|
29
|
+
rootSessionId?: string;
|
|
30
|
+
childSessionId?: string;
|
|
31
|
+
parentSessionId?: string;
|
|
32
|
+
depth?: number;
|
|
33
|
+
title?: string;
|
|
34
|
+
type?: string;
|
|
35
|
+
status?: ChildStatus;
|
|
36
|
+
error?: {
|
|
37
|
+
type?: string;
|
|
38
|
+
message?: string;
|
|
39
|
+
};
|
|
40
|
+
update?: {
|
|
41
|
+
sessionUpdate?: string;
|
|
42
|
+
[key: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** What one delegated run has done so far. */
|
|
46
|
+
export interface ChildProgress {
|
|
47
|
+
childSessionId: string;
|
|
48
|
+
title?: string;
|
|
49
|
+
status: ChildStatus;
|
|
50
|
+
/** How many tool calls the child has made. */
|
|
51
|
+
toolCalls: number;
|
|
52
|
+
/** The most recent tool the child used, or what it is doing (thinking/writing). */
|
|
53
|
+
lastActivity?: string;
|
|
54
|
+
error?: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Fold one child event into the record for that child.
|
|
58
|
+
*
|
|
59
|
+
* Returns the updated record so the caller can refresh the card; a frame that carries
|
|
60
|
+
* nothing new returns undefined, which keeps a chatty agent from churning the transcript.
|
|
61
|
+
*/
|
|
62
|
+
export declare function applyChildEvent(known: ChildProgress | undefined, event: ChildSessionEvent): ChildProgress | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The parenthetical appended to the card's caption.
|
|
65
|
+
*
|
|
66
|
+
* The agent's own description is kept verbatim and the progress follows in brackets, so
|
|
67
|
+
* the card still says what it was asked to do — the parenthetical is visibly secondary.
|
|
68
|
+
*/
|
|
69
|
+
export declare function childCaption(progress: ChildProgress): string | undefined;
|
|
70
|
+
/** Whether the client should show its `Background` badge for this child. */
|
|
71
|
+
export declare function isStillRunning(progress: ChildProgress): boolean;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `opencode/session/child_update` — opencode's own extension for a delegated run.
|
|
3
|
+
*
|
|
4
|
+
* ACP has an RFD for this ("Subagent Sessions", each child its own session id), but
|
|
5
|
+
* `opencode acp` ships this quieter negotiated form instead: announced as
|
|
6
|
+
* `agentCapabilities._meta["opencode/child-session-updates"]` and delivered as one
|
|
7
|
+
* notification per child event.
|
|
8
|
+
*
|
|
9
|
+
* { rootSessionId, childSessionId, parentSessionId, depth, title?,
|
|
10
|
+
* type: "update", update: <the child's own session/update> }
|
|
11
|
+
* | { …, type: "status", status: "created"|"running"|"completed"|"failed"|"interrupted" }
|
|
12
|
+
*
|
|
13
|
+
* ★ What it is NOT for: it does not create a second session. opencode's TUI would need a
|
|
14
|
+
* real session in the strip to open one, and a tab the user never opened is the wrong
|
|
15
|
+
* shape for a delegated run. It exists to put the child's ACTIVITY on the card that
|
|
16
|
+
* reports it — the two slots that component actually renders:
|
|
17
|
+
*
|
|
18
|
+
* • the caption (`input.description`, via `session.tool.called`) — the only free text;
|
|
19
|
+
* • the `Background` badge (`metadata.status`, via `session.tool.progress`), which the
|
|
20
|
+
* client shows when the card is `completed` but `metadata.status === "running"`
|
|
21
|
+
* (`isBackgroundSubagent`). A delegated run that returned its card while still
|
|
22
|
+
* working is exactly that case.
|
|
23
|
+
*
|
|
24
|
+
* Shape taken from `packages/cli/src/acp/event.ts` (`ChildSessionUpdate`); nothing here
|
|
25
|
+
* is invented.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Fold one child event into the record for that child.
|
|
29
|
+
*
|
|
30
|
+
* Returns the updated record so the caller can refresh the card; a frame that carries
|
|
31
|
+
* nothing new returns undefined, which keeps a chatty agent from churning the transcript.
|
|
32
|
+
*/
|
|
33
|
+
export function applyChildEvent(known, event) {
|
|
34
|
+
const childSessionId = typeof event.childSessionId === 'string' ? event.childSessionId : undefined;
|
|
35
|
+
if (!childSessionId)
|
|
36
|
+
return undefined;
|
|
37
|
+
const next = known ?? { childSessionId, status: 'running', toolCalls: 0 };
|
|
38
|
+
if (typeof event.title === 'string' && event.title)
|
|
39
|
+
next.title = event.title;
|
|
40
|
+
if (event.type === 'status') {
|
|
41
|
+
if (!event.status || event.status === next.status)
|
|
42
|
+
return undefined;
|
|
43
|
+
next.status = event.status;
|
|
44
|
+
if (event.error?.message)
|
|
45
|
+
next.error = event.error.message;
|
|
46
|
+
return next;
|
|
47
|
+
}
|
|
48
|
+
if (event.type !== 'update' || !event.update)
|
|
49
|
+
return undefined;
|
|
50
|
+
const kind = event.update.sessionUpdate;
|
|
51
|
+
if (typeof kind !== 'string')
|
|
52
|
+
return undefined;
|
|
53
|
+
if (kind === 'tool_call')
|
|
54
|
+
next.toolCalls += 1;
|
|
55
|
+
const activity = describe(kind, event.update);
|
|
56
|
+
if (activity === next.lastActivity)
|
|
57
|
+
return undefined;
|
|
58
|
+
if (activity)
|
|
59
|
+
next.lastActivity = activity;
|
|
60
|
+
// A tool_call is itself news even when its caption repeats.
|
|
61
|
+
return kind === 'tool_call' || activity !== undefined ? next : undefined;
|
|
62
|
+
}
|
|
63
|
+
/** A short phrase for the caption. Never reads agent prose beyond the tool's own name. */
|
|
64
|
+
function describe(kind, update) {
|
|
65
|
+
if (kind === 'tool_call' || kind === 'tool_call_update') {
|
|
66
|
+
const title = typeof update['title'] === 'string' ? update['title'] : undefined;
|
|
67
|
+
const name = typeof update['name'] === 'string' ? update['name'] : undefined;
|
|
68
|
+
return title || name;
|
|
69
|
+
}
|
|
70
|
+
if (kind === 'agent_thought_chunk')
|
|
71
|
+
return 'thinking';
|
|
72
|
+
if (kind === 'agent_message_chunk')
|
|
73
|
+
return 'writing';
|
|
74
|
+
if (kind === 'plan')
|
|
75
|
+
return 'planning';
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The parenthetical appended to the card's caption.
|
|
80
|
+
*
|
|
81
|
+
* The agent's own description is kept verbatim and the progress follows in brackets, so
|
|
82
|
+
* the card still says what it was asked to do — the parenthetical is visibly secondary.
|
|
83
|
+
*/
|
|
84
|
+
export function childCaption(progress) {
|
|
85
|
+
const parts = [];
|
|
86
|
+
if (progress.status === 'failed') {
|
|
87
|
+
if (progress.error)
|
|
88
|
+
parts.push(`failed: ${progress.error}`);
|
|
89
|
+
}
|
|
90
|
+
else if (progress.status === 'interrupted') {
|
|
91
|
+
parts.push('interrupted');
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (progress.toolCalls > 0)
|
|
95
|
+
parts.push(`${progress.toolCalls} call${progress.toolCalls === 1 ? '' : 's'}`);
|
|
96
|
+
if (progress.lastActivity)
|
|
97
|
+
parts.push(progress.lastActivity);
|
|
98
|
+
if (progress.status === 'completed')
|
|
99
|
+
parts.push('done');
|
|
100
|
+
}
|
|
101
|
+
return parts.length > 0 ? parts.join(', ') : undefined;
|
|
102
|
+
}
|
|
103
|
+
/** Whether the client should show its `Background` badge for this child. */
|
|
104
|
+
export function isStillRunning(progress) {
|
|
105
|
+
return progress.status === 'running';
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=subagent-sessions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subagent-sessions.js","sourceRoot":"","sources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