opencode-ahp 0.1.4 → 0.1.6
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 +91 -37
- package/options.example.jsonc +25 -8
- package/package.json +1 -1
- package/src/bridge.ts +335 -127
- package/src/channels.ts +74 -32
- package/src/client-tools.ts +127 -0
- package/src/handshake.ts +2 -3
- package/src/host-base.ts +335 -0
- package/src/host-changesets.ts +381 -0
- package/src/host-chats.ts +300 -0
- package/src/host-client-tools.ts +205 -0
- package/src/host-dispatch.ts +593 -0
- package/src/host-errors.ts +29 -0
- package/src/host-events.ts +386 -0
- package/src/host-rpc.ts +435 -0
- package/src/host-sessions.ts +558 -0
- package/src/host-terminals.ts +654 -0
- package/src/host-turns.ts +82 -0
- package/src/host-types.ts +225 -0
- package/src/host-util.ts +194 -0
- package/src/host.ts +23 -2299
- package/src/index.ts +265 -33
- package/src/keep-alive.ts +3 -3
- package/src/options.ts +23 -9
- package/src/seq.ts +4 -5
- package/src/state.ts +40 -6
- package/src/transport.ts +6 -6
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-tool section (design §10): tool-call ready/tracking state, the location-global tool union sync, and the `execute` bridge (public — called from the plugin registration in index.ts).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { HostTurns } from "./host-turns.js";
|
|
6
|
+
import { ActionType, ResponsePartKind, ToolCallConfirmationReason, ToolCallStatus, SessionActiveClient, SessionToolConfirmationRequest, ToolCallConfirmationState, ToolCallState } from "@microsoft/agent-host-protocol";
|
|
7
|
+
import { invocationMessageFor, FacadeClientTool, FacadeClientToolResult } from "./bridge.js";
|
|
8
|
+
import { descriptionFor, findProvider, inputSchemaFor, toolUnion, ClientToolInvocation } from "./client-tools.js";
|
|
9
|
+
import { ChatRuntime, SessionRecord } from "./host-types.js";
|
|
10
|
+
import { errorMessage } from "./host-util.js";
|
|
11
|
+
|
|
12
|
+
export class HostClientTools extends HostTurns {
|
|
13
|
+
/**
|
|
14
|
+
* Lazily publish `chat/toolCallReady` for a tool call (first of
|
|
15
|
+
* progress/success/failed/permission.asked):
|
|
16
|
+
* - permission trigger → no `confirmed` → the reducer parks it in
|
|
17
|
+
* `pending-confirmation` (the caller then raises `inputNeededSet`);
|
|
18
|
+
* - otherwise → `confirmed: "not-needed"` → straight to `running`
|
|
19
|
+
* (opencode auto-approved).
|
|
20
|
+
*/
|
|
21
|
+
protected ensureToolReady(chat: ChatRuntime, toolCallId: string, permission: boolean): void {
|
|
22
|
+
if (chat.toolReady.has(toolCallId) || chat.currentTurnId === null) return;
|
|
23
|
+
chat.toolReady.add(toolCallId);
|
|
24
|
+
const name = chat.toolName.get(toolCallId) ?? "tool";
|
|
25
|
+
const input = chat.toolInput.get(toolCallId);
|
|
26
|
+
this.publish({
|
|
27
|
+
channel: chat.uri,
|
|
28
|
+
action: {
|
|
29
|
+
type: ActionType.ChatToolCallReady,
|
|
30
|
+
turnId: chat.currentTurnId,
|
|
31
|
+
toolCallId,
|
|
32
|
+
invocationMessage: invocationMessageFor(name, input),
|
|
33
|
+
toolInput: input,
|
|
34
|
+
...(permission ? {} : { confirmed: ToolCallConfirmationReason.NotNeeded }),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The live tool-call state in the chat's current turn, or null when absent. */
|
|
40
|
+
protected toolCallState(chat: ChatRuntime, toolCallId: string): ToolCallState | null {
|
|
41
|
+
const turn = chat.chatStore.state.activeTurn;
|
|
42
|
+
if (turn === undefined) return null;
|
|
43
|
+
for (const part of turn.responseParts) {
|
|
44
|
+
if (part.kind === ResponsePartKind.ToolCall && part.toolCall.toolCallId === toolCallId) {
|
|
45
|
+
return part.toolCall;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The tool call's state when it is awaiting confirmation (the
|
|
53
|
+
* `SessionToolConfirmationRequest.toolCall` shape), or null otherwise.
|
|
54
|
+
*/
|
|
55
|
+
protected pendingToolCall(chat: ChatRuntime, toolCallId: string): ToolCallConfirmationState | null {
|
|
56
|
+
const tc = this.toolCallState(chat, toolCallId);
|
|
57
|
+
if (tc === null) return null;
|
|
58
|
+
if (tc.status === ToolCallStatus.PendingConfirmation || tc.status === ToolCallStatus.PendingResultConfirmation) {
|
|
59
|
+
return tc;
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** True when the tool call already left the ready states (completed/cancelled). */
|
|
65
|
+
protected toolCallTerminal(chat: ChatRuntime, toolCallId: string): boolean {
|
|
66
|
+
const tc = this.toolCallState(chat, toolCallId);
|
|
67
|
+
return tc !== null && (tc.status === ToolCallStatus.Completed || tc.status === ToolCallStatus.Cancelled);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected clearToolTracking(chat: ChatRuntime, toolCallId: string): void {
|
|
71
|
+
chat.toolInput.delete(toolCallId);
|
|
72
|
+
chat.toolName.delete(toolCallId);
|
|
73
|
+
chat.toolReady.delete(toolCallId);
|
|
74
|
+
chat.clientToolCalls.delete(toolCallId);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The session's active client providing `toolName` (the tool-call
|
|
79
|
+
* contributor), or null when no active client provides it (a
|
|
80
|
+
* server-executed call — the normal path).
|
|
81
|
+
*/
|
|
82
|
+
// ── client-tool helpers ─────────────────────────────────────────────────
|
|
83
|
+
protected clientToolContributor(rec: SessionRecord, toolName: string): string | null {
|
|
84
|
+
return findProvider(rec.sessionStore.state.activeClients, toolName);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Re-sync the location-global opencode tool registry with the
|
|
89
|
+
* cross-session union of every live session's active-client tools
|
|
90
|
+
* (first-registered wins per name). Location-global by design —
|
|
91
|
+
* opencode's tool state is per-location, not per-session: the
|
|
92
|
+
* `execute` bridge re-checks per-session ownership at
|
|
93
|
+
* runtime.
|
|
94
|
+
*/
|
|
95
|
+
protected async syncClientToolsToOpencode(): Promise<void> {
|
|
96
|
+
const clients: SessionActiveClient[] = [];
|
|
97
|
+
for (const rec of this.sessions.values()) {
|
|
98
|
+
for (const client of rec.sessionStore.state.activeClients) clients.push(client);
|
|
99
|
+
}
|
|
100
|
+
const tools = toolUnion(clients).map(
|
|
101
|
+
(t): FacadeClientTool => ({
|
|
102
|
+
name: t.name,
|
|
103
|
+
description: descriptionFor(t),
|
|
104
|
+
inputSchema: inputSchemaFor(t),
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
try {
|
|
108
|
+
await this.opts.facade.syncClientTools(tools);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
// R2: a tool-registry failure must not break the AHP surface —
|
|
111
|
+
// the active-client state stays authoritative on the AHP side.
|
|
112
|
+
this.log(`client tool sync failed: ${errorMessage(err)}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Settle a client-tool invocation with its final result: the
|
|
118
|
+
* opencode-side `execute` (blocked on the waiter) unblocks and
|
|
119
|
+
* returns/throws, which drives the pump's `tool.success`/
|
|
120
|
+
* `tool.failed` terminal (the AHP state is already terminal — the
|
|
121
|
+
* pump's `toolCallTerminal` check suppresses a duplicate echo and
|
|
122
|
+
* clears the tracking). Settling twice is a no-op.
|
|
123
|
+
*
|
|
124
|
+
* `result` may be omitted when the invocation's `result` was already
|
|
125
|
+
* set by the caller (the eviction / session-removal paths set it
|
|
126
|
+
* first and then settle to deliver it).
|
|
127
|
+
*/
|
|
128
|
+
protected settleClientTool(invocation: ClientToolInvocation, result?: FacadeClientToolResult): void {
|
|
129
|
+
if (invocation.result === undefined) {
|
|
130
|
+
if (result === undefined) return; // nothing to settle
|
|
131
|
+
invocation.result = result;
|
|
132
|
+
}
|
|
133
|
+
if (invocation.waiter !== undefined && invocation.waiter !== null) {
|
|
134
|
+
invocation.waiter(invocation.result);
|
|
135
|
+
invocation.waiter = null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The opencode tool `execute` bridge (called from the
|
|
141
|
+
* `ctx.tool.transform` registration in src/index.ts): route one
|
|
142
|
+
* client-tool invocation to the owning AHP client and wait for its
|
|
143
|
+
* result.
|
|
144
|
+
*
|
|
145
|
+
* Per-session scoping: the tool is visible location-
|
|
146
|
+
* globally, but execution only resolves when *this* session is an
|
|
147
|
+
* AHP session with an active client providing the tool — otherwise
|
|
148
|
+
* the opencode tool fails (a `Tool.Error` the model sees), so e.g. a
|
|
149
|
+
* TUI session can see the tool but not execute it.
|
|
150
|
+
*/
|
|
151
|
+
executeClientTool(input: {
|
|
152
|
+
sessionID: string;
|
|
153
|
+
toolCallId: string;
|
|
154
|
+
name: string;
|
|
155
|
+
signal: AbortSignal;
|
|
156
|
+
}): Promise<FacadeClientToolResult> {
|
|
157
|
+
// The opencode session id joins to the (record, chat) that owns it —
|
|
158
|
+
// every chat has its own backend session.
|
|
159
|
+
const binding = this.byOpencode.get(input.sessionID);
|
|
160
|
+
if (binding === undefined) {
|
|
161
|
+
return Promise.resolve({
|
|
162
|
+
ok: false,
|
|
163
|
+
error: `no AHP session for tool "${input.name}" (session ${input.sessionID} is not an AHP session)`,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const { rec, chat } = binding;
|
|
167
|
+
const invocation = chat.clientToolCalls.get(input.toolCallId);
|
|
168
|
+
if (invocation === undefined) {
|
|
169
|
+
return Promise.resolve({
|
|
170
|
+
ok: false,
|
|
171
|
+
error: `no client-executed call in flight for tool "${input.name}" (it was not registered by this session's active client)`,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (findProvider(rec.sessionStore.state.activeClients, input.name) !== invocation.clientId) {
|
|
175
|
+
return Promise.resolve({
|
|
176
|
+
ok: false,
|
|
177
|
+
error: `session ${rec.sessionID} has no active client providing tool "${input.name}"`,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (input.signal.aborted) {
|
|
181
|
+
const result: FacadeClientToolResult = { ok: false, error: `execution of "${input.name}" was cancelled` };
|
|
182
|
+
this.settleClientTool(invocation, result);
|
|
183
|
+
return Promise.resolve(result);
|
|
184
|
+
}
|
|
185
|
+
// The invocation may already be settled (the client completed before
|
|
186
|
+
// opencode started the execute — possible: the client only needs the
|
|
187
|
+
// pump's ready/inputNeeded actions, not our execute). Deliver the
|
|
188
|
+
// settled result; otherwise register the waiter.
|
|
189
|
+
const settled = invocation.result;
|
|
190
|
+
if (settled !== undefined) return Promise.resolve(settled);
|
|
191
|
+
return new Promise<FacadeClientToolResult>((resolve) => {
|
|
192
|
+
invocation.waiter = resolve;
|
|
193
|
+
const onAbort = () => {
|
|
194
|
+
if (invocation.result === undefined) {
|
|
195
|
+
this.settleClientTool(invocation, {
|
|
196
|
+
ok: false,
|
|
197
|
+
error: `execution of "${input.name}" was cancelled (turn interrupted)`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
// If already settled, the waiter is gone — the abort is a no-op.
|
|
201
|
+
};
|
|
202
|
+
input.signal.addEventListener("abort", onAbort, { once: true });
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|