opencode-ahp 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -25
- package/options.example.jsonc +13 -8
- package/package.json +1 -1
- package/src/bridge.ts +114 -51
- package/src/channels.ts +66 -30
- package/src/client-tools.ts +127 -0
- package/src/handshake.ts +2 -3
- package/src/host.ts +1760 -307
- package/src/index.ts +145 -26
- package/src/keep-alive.ts +3 -3
- package/src/options.ts +12 -9
- package/src/seq.ts +4 -5
- package/src/state.ts +29 -6
- package/src/transport.ts +6 -6
package/src/channels.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* AHP channel registry
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* AHP channel registry: URI parsing/formatting, per-socket subscription
|
|
3
|
+
* bookkeeping, and the sets of live opencode sessions and live terminal
|
|
4
|
+
* channels. Pure in-memory — no runtime dependencies.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* the changeset channel kind (`<owner>/changeset/{uncommitted|session|
|
|
9
|
-
* turn/<turnId>}` — plan §11).
|
|
6
|
+
* Channel kinds: root, session, chat, changeset
|
|
7
|
+
* (`<owner>/changeset/{uncommitted|session|turn/<turnId>}`), and terminal.
|
|
10
8
|
*/
|
|
11
9
|
|
|
12
10
|
export const ROOT_CHANNEL = "ahp-root://";
|
|
13
11
|
const SESSION_PREFIX = "ahp-session:/";
|
|
14
12
|
const CHAT_PREFIX = "ahp-chat:/";
|
|
15
13
|
const CHANGASET_MARKER = "/changeset/";
|
|
14
|
+
const TERMINAL_PREFIX = "ahp-terminal:/";
|
|
16
15
|
|
|
17
|
-
export type ChannelKind = "root" | "session" | "chat" | "changeset" | "unknown";
|
|
16
|
+
export type ChannelKind = "root" | "session" | "chat" | "changeset" | "terminal" | "unknown";
|
|
18
17
|
|
|
19
18
|
export function channelKind(uri: string): ChannelKind {
|
|
20
19
|
if (uri === ROOT_CHANNEL) return "root";
|
|
@@ -22,27 +21,41 @@ export function channelKind(uri: string): ChannelKind {
|
|
|
22
21
|
// Checked before the owner prefixes so `<sessionURI>/changeset/session`
|
|
23
22
|
// is not misread as a session channel whose suffix contains the marker
|
|
24
23
|
// (createSession rejects such suffixes, so no live session channel can
|
|
25
|
-
// contain the marker
|
|
24
|
+
// contain the marker).
|
|
26
25
|
if (uri.lastIndexOf(CHANGASET_MARKER) > 0) return "changeset";
|
|
27
26
|
if (uri.startsWith(SESSION_PREFIX) && uri.length > SESSION_PREFIX.length) return "session";
|
|
28
27
|
if (uri.startsWith(CHAT_PREFIX) && uri.length > CHAT_PREFIX.length) return "chat";
|
|
28
|
+
// Synthetic shell-command terminals.
|
|
29
|
+
if (uri.startsWith(TERMINAL_PREFIX) && uri.length > TERMINAL_PREFIX.length) return "terminal";
|
|
29
30
|
return "unknown";
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
/**
|
|
33
|
-
* Channel URI of a session. opencode has one chat per session (plan §3.2
|
|
34
|
-
* gap, same as pi-ahp / ahp-acp-gateway), and the session's default chat
|
|
35
|
-
* reuses the session id.
|
|
36
|
-
*/
|
|
33
|
+
/** Channel URI of a session. */
|
|
37
34
|
export function sessionChannelURI(sessionID: string): string {
|
|
38
35
|
return SESSION_PREFIX + sessionID;
|
|
39
36
|
}
|
|
40
37
|
|
|
41
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Channel URI of a session's default chat. The default chat reuses the
|
|
40
|
+
* session's opencode id as its suffix (the single-chat zero-wire-change
|
|
41
|
+
* mapping); peer chats created via `createChat` carry client-chosen
|
|
42
|
+
* suffixes instead.
|
|
43
|
+
*/
|
|
42
44
|
export function chatChannelURI(sessionID: string): string {
|
|
43
45
|
return CHAT_PREFIX + sessionID;
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Channel URI of a synthetic shell-command terminal. The suffix is the
|
|
50
|
+
* opencode tool call id —
|
|
51
|
+
* globally unique per tool invocation, so the URI is unambiguous and the
|
|
52
|
+
* terminal correlates 1:1 with the `chat/toolCall*` actions for the same
|
|
53
|
+
* call.
|
|
54
|
+
*/
|
|
55
|
+
export function terminalChannelURI(toolCallId: string): string {
|
|
56
|
+
return TERMINAL_PREFIX + toolCallId;
|
|
57
|
+
}
|
|
58
|
+
|
|
46
59
|
/** The session id behind a session/chat channel URI, or null. */
|
|
47
60
|
export function sessionIDOf(uri: string): string | null {
|
|
48
61
|
const kind = channelKind(uri);
|
|
@@ -52,7 +65,7 @@ export function sessionIDOf(uri: string): string | null {
|
|
|
52
65
|
}
|
|
53
66
|
|
|
54
67
|
/**
|
|
55
|
-
* The three changeset kinds this host advertises
|
|
68
|
+
* The three changeset kinds this host advertises:
|
|
56
69
|
* - `uncommitted`: working tree vs HEAD (owner: the chat channel);
|
|
57
70
|
* - `session`: session-wide changes, same approximation (owner: the
|
|
58
71
|
* session channel — the protocol's "session" changeKind);
|
|
@@ -97,35 +110,41 @@ export function parseChangesetChannel(uri: string): ParsedChangesetChannel | nul
|
|
|
97
110
|
return null;
|
|
98
111
|
}
|
|
99
112
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Changeset URI of one chat's uncommitted (working-tree) changes. The
|
|
115
|
+
* owner is the chat channel itself (multi-chat: every chat has its own
|
|
116
|
+
* uncommitted changeset).
|
|
117
|
+
*/
|
|
118
|
+
export function uncommittedChangesetURI(chatChannel: string): string {
|
|
119
|
+
return `${chatChannel}/changeset/uncommitted`;
|
|
103
120
|
}
|
|
104
121
|
|
|
105
|
-
/** Changeset URI of a session's session-wide changes. */
|
|
106
|
-
export function sessionChangesetURI(
|
|
107
|
-
return `${
|
|
122
|
+
/** Changeset URI of a session's session-wide changes (owner: the session channel). */
|
|
123
|
+
export function sessionChangesetURI(sessionChannel: string): string {
|
|
124
|
+
return `${sessionChannel}/changeset/session`;
|
|
108
125
|
}
|
|
109
126
|
|
|
110
|
-
/** Changeset URI of one turn's captured file changes. */
|
|
111
|
-
export function turnChangesetURI(
|
|
112
|
-
return `${
|
|
127
|
+
/** Changeset URI of one turn's captured file changes (owner: the chat channel). */
|
|
128
|
+
export function turnChangesetURI(chatChannel: string, turnId: string): string {
|
|
129
|
+
return `${chatChannel}/changeset/turn/${turnId}`;
|
|
113
130
|
}
|
|
114
131
|
|
|
115
132
|
export interface SubscribeOutcome {
|
|
116
133
|
ok: boolean;
|
|
117
|
-
reason?: "unknown-uri" | "no-such-session";
|
|
134
|
+
reason?: "unknown-uri" | "no-such-session" | "no-such-terminal";
|
|
118
135
|
}
|
|
119
136
|
|
|
120
137
|
/**
|
|
121
138
|
* Subscription bookkeeping keyed by transport socket id, plus the set of
|
|
122
|
-
* live session ids (
|
|
139
|
+
* live session ids and the set of live terminal channel URIs (the host
|
|
140
|
+
* adds/removes them as synthetic terminals are opened and torn down).
|
|
123
141
|
*/
|
|
124
142
|
export class ChannelRegistry {
|
|
125
143
|
private readonly subs = new Map<number, Set<string>>();
|
|
126
144
|
private readonly sessions = new Set<string>();
|
|
145
|
+
private readonly terminals = new Set<string>();
|
|
127
146
|
|
|
128
|
-
/** Subscribe a socket to a channel; fails for unknown URIs or
|
|
147
|
+
/** Subscribe a socket to a channel; fails for unknown URIs, dead sessions, or gone terminals. */
|
|
129
148
|
subscribe(socketId: number, uri: string): SubscribeOutcome {
|
|
130
149
|
const kind = channelKind(uri);
|
|
131
150
|
if (kind === "unknown") return { ok: false, reason: "unknown-uri" };
|
|
@@ -135,10 +154,13 @@ export class ChannelRegistry {
|
|
|
135
154
|
const parsed = parseChangesetChannel(uri);
|
|
136
155
|
if (parsed === null) return { ok: false, reason: "unknown-uri" };
|
|
137
156
|
id = parsed.sessionID;
|
|
157
|
+
} else if (kind === "terminal") {
|
|
158
|
+
if (!this.terminals.has(uri)) return { ok: false, reason: "no-such-terminal" };
|
|
159
|
+
id = null;
|
|
138
160
|
} else {
|
|
139
161
|
id = sessionIDOf(uri);
|
|
140
162
|
}
|
|
141
|
-
if (id
|
|
163
|
+
if (id !== null && !this.sessions.has(id)) return { ok: false, reason: "no-such-session" };
|
|
142
164
|
}
|
|
143
165
|
let set = this.subs.get(socketId);
|
|
144
166
|
if (!set) {
|
|
@@ -170,7 +192,7 @@ export class ChannelRegistry {
|
|
|
170
192
|
this.subs.delete(socketId);
|
|
171
193
|
}
|
|
172
194
|
|
|
173
|
-
// ── live sessions (
|
|
195
|
+
// ── live sessions (session/chat channels) ─────────────────────────────
|
|
174
196
|
|
|
175
197
|
addSession(sessionID: string): void {
|
|
176
198
|
this.sessions.add(sessionID);
|
|
@@ -183,4 +205,18 @@ export class ChannelRegistry {
|
|
|
183
205
|
hasSession(sessionID: string): boolean {
|
|
184
206
|
return this.sessions.has(sessionID);
|
|
185
207
|
}
|
|
208
|
+
|
|
209
|
+
// ── live terminals (synthetic shell-command terminals) ────────────────
|
|
210
|
+
|
|
211
|
+
addTerminal(uri: string): void {
|
|
212
|
+
this.terminals.add(uri);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
removeTerminal(uri: string): void {
|
|
216
|
+
this.terminals.delete(uri);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
hasTerminal(uri: string): boolean {
|
|
220
|
+
return this.terminals.has(uri);
|
|
221
|
+
}
|
|
186
222
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-provided tools: the pure helpers the host uses for the
|
|
3
|
+
* client-tool surface — wire validation
|
|
4
|
+
* of AHP `ToolDefinition`s, the cross-session definition union the model
|
|
5
|
+
* sees (first-registered wins per name), and the per-session provider
|
|
6
|
+
* lookup used to attribute the tool-call contributor and route execution.
|
|
7
|
+
*
|
|
8
|
+
* Everything here is a pure function over wire-shaped data: no sockets, no
|
|
9
|
+
* opencode, no timers (house pattern — see the other pure modules).
|
|
10
|
+
*/
|
|
11
|
+
import type { ToolDefinition } from "@microsoft/agent-host-protocol";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The per-invocation tracking record the host keeps for a client-executed
|
|
15
|
+
* tool call (keyed by the opencode tool-call id, which the client sees as
|
|
16
|
+
* `toolCallId` on the chat channel).
|
|
17
|
+
*
|
|
18
|
+
* Lifecycle: created at `tool.input.started` (the contributor is known —
|
|
19
|
+
* the session's active client providing the tool), the `result` is set
|
|
20
|
+
* when the execution settles (the client's `toolCallComplete`, the
|
|
21
|
+
* result-confirmation reply, or a disconnect eviction), and the record is
|
|
22
|
+
* removed at the opencode-side terminal (`tool.success`/`tool.failed`),
|
|
23
|
+
* which always follows the settlement (the settlement is what unblocks
|
|
24
|
+
* the opencode tool's `execute`).
|
|
25
|
+
*/
|
|
26
|
+
export interface ClientToolInvocation {
|
|
27
|
+
/** The active client that provides the tool (the contributor's clientId). */
|
|
28
|
+
clientId: string;
|
|
29
|
+
/** The AHP turn the call belongs to. */
|
|
30
|
+
turnId: string;
|
|
31
|
+
/**
|
|
32
|
+
* The client's `toolCallComplete` result, held while a
|
|
33
|
+
* `requiresResultConfirmation` awaits `toolCallResultConfirmed`.
|
|
34
|
+
*/
|
|
35
|
+
pendingResult?: unknown;
|
|
36
|
+
/** The settled execution result (undefined until settled). */
|
|
37
|
+
result?: { ok: true; pastTenseMessage: string; content?: string; structuredContent?: Record<string, unknown> } | { ok: false; error: string };
|
|
38
|
+
/** The opencode-side waiter, or null once it was settled. */
|
|
39
|
+
waiter?: ((result: NonNullable<ClientToolInvocation["result"]>) => void) | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Wire-validate an `activeClient.tools` array: keep the entries that are
|
|
44
|
+
* real `ToolDefinition`s (a non-empty `name` string), drop the rest. The
|
|
45
|
+
* AHP contract makes every other field optional, so no deeper validation
|
|
46
|
+
* is needed — the opencode side only reads `name`/`description`/
|
|
47
|
+
* `inputSchema`, and the AHP state mirror stores the rest verbatim.
|
|
48
|
+
*/
|
|
49
|
+
export function normalizeToolDefinitions(raw: unknown): ToolDefinition[] {
|
|
50
|
+
if (!Array.isArray(raw)) return [];
|
|
51
|
+
const out: ToolDefinition[] = [];
|
|
52
|
+
for (const item of raw) {
|
|
53
|
+
if (typeof item !== "object" || item === null) continue;
|
|
54
|
+
const name = (item as Record<string, unknown>)["name"];
|
|
55
|
+
if (typeof name !== "string" || name.length === 0) continue;
|
|
56
|
+
out.push(item as ToolDefinition);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The cross-session tool union the model sees: first-registered wins per
|
|
63
|
+
* name (iteration order = the caller's session order, which the host
|
|
64
|
+
* fixes to the live-session insertion order — deterministic per host
|
|
65
|
+
* lifetime).
|
|
66
|
+
*/
|
|
67
|
+
export function toolUnion(clients: ReadonlyArray<{ tools: readonly ToolDefinition[] }>): ToolDefinition[] {
|
|
68
|
+
const seen = new Map<string, ToolDefinition>();
|
|
69
|
+
for (const client of clients) {
|
|
70
|
+
for (const tool of client.tools ?? []) {
|
|
71
|
+
if (typeof tool.name !== "string" || tool.name.length === 0) continue;
|
|
72
|
+
if (!seen.has(tool.name)) seen.set(tool.name, tool);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return [...seen.values()];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** opencode's `normalizedName` (core `tool/runtime.ts`) — the tool id. */
|
|
79
|
+
export function normalizedName(name: string): string {
|
|
80
|
+
return name.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The active client providing `toolName` for a session (the contributor's
|
|
85
|
+
* clientId), or null. Matches on the raw name or on the opencode-
|
|
86
|
+
* normalized name (a tool named "my tool" registers under `my_tool`, so
|
|
87
|
+
* the pump's event name and the wire name can differ only by that
|
|
88
|
+
* sanitization).
|
|
89
|
+
*/
|
|
90
|
+
export function findProvider(
|
|
91
|
+
clients: ReadonlyArray<{ clientId: string; tools: readonly ToolDefinition[] }>,
|
|
92
|
+
toolName: string,
|
|
93
|
+
): string | null {
|
|
94
|
+
if (typeof toolName !== "string" || toolName.length === 0) return null;
|
|
95
|
+
const needle = normalizedName(toolName);
|
|
96
|
+
for (const client of clients) {
|
|
97
|
+
for (const tool of client.tools ?? []) {
|
|
98
|
+
if (tool.name === toolName || normalizedName(tool.name) === needle) return client.clientId;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The input JSON Schema for an opencode tool registration: the client's
|
|
106
|
+
* `inputSchema` verbatim when present (it is a JSON Schema — opencode's
|
|
107
|
+
* `inputJsonSchema` passes raw schemas through untouched), otherwise the
|
|
108
|
+
* lenient "any object" schema.
|
|
109
|
+
*/
|
|
110
|
+
export function inputSchemaFor(tool: ToolDefinition): Record<string, unknown> {
|
|
111
|
+
const s = tool.inputSchema;
|
|
112
|
+
if (
|
|
113
|
+
typeof s === "object" &&
|
|
114
|
+
s !== null &&
|
|
115
|
+
(s as Record<string, unknown>)["type"] === "object"
|
|
116
|
+
) {
|
|
117
|
+
return s as Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
return { type: "object" };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The description an opencode tool registration carries for a client tool. */
|
|
123
|
+
export function descriptionFor(tool: ToolDefinition): string {
|
|
124
|
+
if (typeof tool.description === "string" && tool.description.length > 0) return tool.description;
|
|
125
|
+
if (typeof tool.title === "string" && tool.title.length > 0) return tool.title;
|
|
126
|
+
return tool.name;
|
|
127
|
+
}
|
package/src/handshake.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* AHP 1.0.0 handshake
|
|
3
|
-
*
|
|
4
|
-
* `negotiateProtocolVersion`.
|
|
2
|
+
* AHP 1.0.0 handshake: the `initialize` command — semver capability
|
|
3
|
+
* negotiation via the official package's `negotiateProtocolVersion`.
|
|
5
4
|
*
|
|
6
5
|
* Version semantics (AHP versioning spec): the server picks the highest
|
|
7
6
|
* client-offered version inside its supported caret baselines and echoes
|