1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/**
* Shell — HTTP transport layer (effects injected at the edges).
*
* streamChat: POST /chat, returns an async iterable of AgentEvents.
* fetchModels: GET /models, returns the ModelsResponse.
*
* The fetchImpl dependency is injected (outermost edge mock allowed).
*/
import type {
AgentEvent,
ChatRequest,
CompactResponse,
ConversationListResponse,
LastMessageResponse,
ModelsResponse,
OpenConversationResponse,
QueueResponse,
} from "@dispatch/transport-contract";
import { splitNdjsonLines } from "./ndjson.js";
interface FetchDeps {
readonly fetchImpl: typeof fetch;
}
interface StreamChatOpts {
readonly server: string;
readonly request: ChatRequest;
}
export async function streamChat(
deps: FetchDeps,
opts: StreamChatOpts,
): Promise<{ conversationId: string | null; events: AsyncIterable<AgentEvent> }> {
const url = `${opts.server}/chat`;
const res = await deps.fetchImpl(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(opts.request),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`POST /chat failed with status ${res.status}: ${body}`);
}
const conversationId = res.headers.get("X-Conversation-Id");
if (!res.body) {
throw new Error("POST /chat returned no body");
}
const events = readNdjsonStream(res.body);
return { conversationId, events };
}
async function* readNdjsonStream(body: ReadableStream<Uint8Array>): AsyncIterable<AgentEvent> {
const reader = body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const { lines, rest } = splitNdjsonLines(buffer);
buffer = rest;
for (const line of lines) {
yield JSON.parse(line) as AgentEvent;
}
}
if (buffer.length > 0) {
yield JSON.parse(buffer) as AgentEvent;
}
} finally {
reader.releaseLock();
}
}
interface FetchModelsOpts {
readonly server: string;
}
export async function fetchModels(deps: FetchDeps, opts: FetchModelsOpts): Promise<ModelsResponse> {
const url = `${opts.server}/models`;
const res = await deps.fetchImpl(url);
if (!res.ok) {
const body = await res.text();
throw new Error(`GET /models failed with status ${res.status}: ${body}`);
}
return (await res.json()) as ModelsResponse;
}
interface FetchConversationsOpts {
readonly server: string;
readonly query?: string;
readonly status?: string;
readonly workspaceId?: string;
}
export async function fetchConversations(
deps: FetchDeps,
opts: FetchConversationsOpts,
): Promise<ConversationListResponse> {
const params = new URLSearchParams();
if (opts.query !== undefined) params.set("q", opts.query);
if (opts.status !== undefined) params.set("status", opts.status);
if (opts.workspaceId !== undefined) params.set("workspaceId", opts.workspaceId);
const qs = params.toString();
const url = qs.length > 0 ? `${opts.server}/conversations?${qs}` : `${opts.server}/conversations`;
const res = await deps.fetchImpl(url);
if (!res.ok) {
const body = await res.text();
throw new Error(`GET /conversations failed with status ${res.status}: ${body}`);
}
return (await res.json()) as ConversationListResponse;
}
interface FetchLastMessageOpts {
readonly server: string;
readonly conversationId: string;
}
export async function fetchLastMessage(
deps: FetchDeps,
opts: FetchLastMessageOpts,
): Promise<LastMessageResponse> {
const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/last`;
const res = await deps.fetchImpl(url);
if (!res.ok) {
const body = await res.text();
throw new Error(`GET /conversations/:id/last failed with status ${res.status}: ${body}`);
}
return (await res.json()) as LastMessageResponse;
}
interface EnqueueMessageOpts {
readonly server: string;
readonly conversationId: string;
readonly text: string;
}
export async function enqueueMessage(
deps: FetchDeps,
opts: EnqueueMessageOpts,
): Promise<QueueResponse> {
const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/queue`;
const res = await deps.fetchImpl(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: opts.text }),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`POST /conversations/:id/queue failed with status ${res.status}: ${body}`);
}
return (await res.json()) as QueueResponse;
}
interface OpenConversationOpts {
readonly server: string;
readonly conversationId: string;
}
export async function openConversation(
deps: FetchDeps,
opts: OpenConversationOpts,
): Promise<OpenConversationResponse> {
const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/open`;
const res = await deps.fetchImpl(url, { method: "POST" });
if (!res.ok) {
const body = await res.text();
throw new Error(`POST /conversations/:id/open failed with status ${res.status}: ${body}`);
}
return (await res.json()) as OpenConversationResponse;
}
interface CompactConversationOpts {
readonly server: string;
readonly conversationId: string;
}
export async function compactConversation(
deps: FetchDeps,
opts: CompactConversationOpts,
): Promise<CompactResponse> {
const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/compact`;
const res = await deps.fetchImpl(url, { method: "POST" });
if (!res.ok) {
const body = await res.text();
throw new Error(`POST /conversations/:id/compact failed with status ${res.status}: ${body}`);
}
return (await res.json()) as CompactResponse;
}
interface StopTurnOpts {
readonly server: string;
readonly conversationId: string;
}
export async function stopTurn(
deps: FetchDeps,
opts: StopTurnOpts,
): Promise<{ conversationId: string; abortedTurn: boolean }> {
const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/stop`;
const res = await deps.fetchImpl(url, { method: "POST" });
if (!res.ok) {
const body = await res.text();
throw new Error(`POST /conversations/:id/stop failed with status ${res.status}: ${body}`);
}
return (await res.json()) as { conversationId: string; abortedTurn: boolean };
}
/**
* The outcome of short-ID resolution: either the full conversation id to use,
* or a human-readable error describing why resolution failed.
*/
export type ConversationIdResolution = string | { readonly error: string };
interface ResolveConversationIdOpts {
readonly server: string;
readonly shortId: string;
}
/**
* Resolve a user-typed conversation prefix to a full id. A 32+ char input is
* assumed to be a full UUID and returned untouched. Otherwise the conversation
* list is filtered by the prefix: 1 match → its id; 0 → error; >1 → error with
* the candidate short ids + titles so the user can disambiguate.
*/
export async function resolveConversationId(
deps: FetchDeps,
opts: ResolveConversationIdOpts,
): Promise<ConversationIdResolution> {
if (opts.shortId.length >= 32) {
return opts.shortId;
}
const list = await fetchConversations(deps, { server: opts.server, query: opts.shortId });
const matches = list.conversations;
if (matches.length === 0) {
return { error: `No conversation matching "${opts.shortId}"` };
}
if (matches.length === 1) {
const only = matches[0];
if (only === undefined) return { error: `No conversation matching "${opts.shortId}"` };
return only.id;
}
const lines = matches.map((m) => `${m.id.slice(0, 8)} ${m.title}`).join("\n");
return {
error: `Multiple conversations matching "${opts.shortId}"\n${lines}`,
};
}
|