opencode-ahp 0.1.3 → 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 +95 -20
- package/options.example.jsonc +20 -9
- package/package.json +1 -1
- package/src/bridge.ts +427 -33
- package/src/channels.ts +131 -18
- package/src/client-tools.ts +127 -0
- package/src/handshake.ts +2 -3
- package/src/host.ts +2231 -243
- package/src/index.ts +273 -24
- package/src/keep-alive.ts +3 -3
- package/src/options.ts +12 -7
- package/src/seq.ts +4 -5
- package/src/state.ts +52 -5
- package/src/transport.ts +15 -8
package/src/channels.ts
CHANGED
|
@@ -1,39 +1,61 @@
|
|
|
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
|
-
*
|
|
6
|
+
* Channel kinds: root, session, chat, changeset
|
|
7
|
+
* (`<owner>/changeset/{uncommitted|session|turn/<turnId>}`), and terminal.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
export const ROOT_CHANNEL = "ahp-root://";
|
|
11
11
|
const SESSION_PREFIX = "ahp-session:/";
|
|
12
12
|
const CHAT_PREFIX = "ahp-chat:/";
|
|
13
|
+
const CHANGASET_MARKER = "/changeset/";
|
|
14
|
+
const TERMINAL_PREFIX = "ahp-terminal:/";
|
|
13
15
|
|
|
14
|
-
export type ChannelKind = "root" | "session" | "chat" | "unknown";
|
|
16
|
+
export type ChannelKind = "root" | "session" | "chat" | "changeset" | "terminal" | "unknown";
|
|
15
17
|
|
|
16
18
|
export function channelKind(uri: string): ChannelKind {
|
|
17
19
|
if (uri === ROOT_CHANNEL) return "root";
|
|
20
|
+
// A changeset URI is an owner session/chat URI plus `/changeset/<kind>`.
|
|
21
|
+
// Checked before the owner prefixes so `<sessionURI>/changeset/session`
|
|
22
|
+
// is not misread as a session channel whose suffix contains the marker
|
|
23
|
+
// (createSession rejects such suffixes, so no live session channel can
|
|
24
|
+
// contain the marker).
|
|
25
|
+
if (uri.lastIndexOf(CHANGASET_MARKER) > 0) return "changeset";
|
|
18
26
|
if (uri.startsWith(SESSION_PREFIX) && uri.length > SESSION_PREFIX.length) return "session";
|
|
19
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";
|
|
20
30
|
return "unknown";
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
/**
|
|
24
|
-
* Channel URI of a session. opencode has one chat per session (plan §3.2
|
|
25
|
-
* gap, same as pi-ahp / ahp-acp-gateway), and the session's default chat
|
|
26
|
-
* reuses the session id.
|
|
27
|
-
*/
|
|
33
|
+
/** Channel URI of a session. */
|
|
28
34
|
export function sessionChannelURI(sessionID: string): string {
|
|
29
35
|
return SESSION_PREFIX + sessionID;
|
|
30
36
|
}
|
|
31
37
|
|
|
32
|
-
/**
|
|
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
|
+
*/
|
|
33
44
|
export function chatChannelURI(sessionID: string): string {
|
|
34
45
|
return CHAT_PREFIX + sessionID;
|
|
35
46
|
}
|
|
36
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
|
+
|
|
37
59
|
/** The session id behind a session/chat channel URI, or null. */
|
|
38
60
|
export function sessionIDOf(uri: string): string | null {
|
|
39
61
|
const kind = channelKind(uri);
|
|
@@ -42,26 +64,103 @@ export function sessionIDOf(uri: string): string | null {
|
|
|
42
64
|
return null;
|
|
43
65
|
}
|
|
44
66
|
|
|
67
|
+
/**
|
|
68
|
+
* The three changeset kinds this host advertises:
|
|
69
|
+
* - `uncommitted`: working tree vs HEAD (owner: the chat channel);
|
|
70
|
+
* - `session`: session-wide changes, same approximation (owner: the
|
|
71
|
+
* session channel — the protocol's "session" changeKind);
|
|
72
|
+
* - `turn`: the files a single turn captured before its edits
|
|
73
|
+
* (owner: the chat channel; `turnId` is the AHP turn id).
|
|
74
|
+
*/
|
|
75
|
+
export type ChangesetKind = "uncommitted" | "session" | "turn";
|
|
76
|
+
|
|
77
|
+
export interface ParsedChangesetChannel {
|
|
78
|
+
/** The owner session/chat channel URI (valid — see `sessionIDOf`). */
|
|
79
|
+
ownerUri: string;
|
|
80
|
+
/** The owner's opencode session id. */
|
|
81
|
+
sessionID: string;
|
|
82
|
+
kind: ChangesetKind;
|
|
83
|
+
/** Present iff `kind === "turn"`. */
|
|
84
|
+
turnId?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Parse a changeset channel URI of the shape
|
|
89
|
+
* `<owner session|chat URI>/changeset/{uncommitted|session|turn/<turnId>}`,
|
|
90
|
+
* or null when the URI is not one. `lastIndexOf` splits on the LAST marker,
|
|
91
|
+
* and the owner must classify as a session/chat channel — so a URI whose
|
|
92
|
+
* owner itself contains the marker fails (no live session channel ever
|
|
93
|
+
* contains it: createSession rejects such suffixes).
|
|
94
|
+
*/
|
|
95
|
+
export function parseChangesetChannel(uri: string): ParsedChangesetChannel | null {
|
|
96
|
+
const index = uri.lastIndexOf(CHANGASET_MARKER);
|
|
97
|
+
if (index <= 0) return null;
|
|
98
|
+
const ownerUri = uri.slice(0, index);
|
|
99
|
+
const sessionID = sessionIDOf(ownerUri);
|
|
100
|
+
if (sessionID === null) return null;
|
|
101
|
+
const rest = uri.slice(index + CHANGASET_MARKER.length);
|
|
102
|
+
if (rest === "uncommitted" || rest === "session") {
|
|
103
|
+
return { ownerUri, sessionID, kind: rest };
|
|
104
|
+
}
|
|
105
|
+
if (rest.startsWith("turn/")) {
|
|
106
|
+
const turnId = rest.slice("turn/".length);
|
|
107
|
+
// Non-empty, no slashes: an AHP turn id (host-minted `turn-…`).
|
|
108
|
+
if (turnId.length > 0 && !turnId.includes("/")) return { ownerUri, sessionID, kind: "turn", turnId };
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
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`;
|
|
120
|
+
}
|
|
121
|
+
|
|
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`;
|
|
125
|
+
}
|
|
126
|
+
|
|
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}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
45
132
|
export interface SubscribeOutcome {
|
|
46
133
|
ok: boolean;
|
|
47
|
-
reason?: "unknown-uri" | "no-such-session";
|
|
134
|
+
reason?: "unknown-uri" | "no-such-session" | "no-such-terminal";
|
|
48
135
|
}
|
|
49
136
|
|
|
50
137
|
/**
|
|
51
138
|
* Subscription bookkeeping keyed by transport socket id, plus the set of
|
|
52
|
-
* 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).
|
|
53
141
|
*/
|
|
54
142
|
export class ChannelRegistry {
|
|
55
143
|
private readonly subs = new Map<number, Set<string>>();
|
|
56
144
|
private readonly sessions = new Set<string>();
|
|
145
|
+
private readonly terminals = new Set<string>();
|
|
57
146
|
|
|
58
|
-
/** 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. */
|
|
59
148
|
subscribe(socketId: number, uri: string): SubscribeOutcome {
|
|
60
149
|
const kind = channelKind(uri);
|
|
61
150
|
if (kind === "unknown") return { ok: false, reason: "unknown-uri" };
|
|
62
151
|
if (kind !== "root") {
|
|
63
|
-
|
|
64
|
-
if (
|
|
152
|
+
let id: string | null;
|
|
153
|
+
if (kind === "changeset") {
|
|
154
|
+
const parsed = parseChangesetChannel(uri);
|
|
155
|
+
if (parsed === null) return { ok: false, reason: "unknown-uri" };
|
|
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;
|
|
160
|
+
} else {
|
|
161
|
+
id = sessionIDOf(uri);
|
|
162
|
+
}
|
|
163
|
+
if (id !== null && !this.sessions.has(id)) return { ok: false, reason: "no-such-session" };
|
|
65
164
|
}
|
|
66
165
|
let set = this.subs.get(socketId);
|
|
67
166
|
if (!set) {
|
|
@@ -93,7 +192,7 @@ export class ChannelRegistry {
|
|
|
93
192
|
this.subs.delete(socketId);
|
|
94
193
|
}
|
|
95
194
|
|
|
96
|
-
// ── live sessions (
|
|
195
|
+
// ── live sessions (session/chat channels) ─────────────────────────────
|
|
97
196
|
|
|
98
197
|
addSession(sessionID: string): void {
|
|
99
198
|
this.sessions.add(sessionID);
|
|
@@ -106,4 +205,18 @@ export class ChannelRegistry {
|
|
|
106
205
|
hasSession(sessionID: string): boolean {
|
|
107
206
|
return this.sessions.has(sessionID);
|
|
108
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
|
+
}
|
|
109
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
|