@adroit-app/chat-core 0.0.0 → 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 +21 -0
- package/README.md +151 -2
- package/dist/acp.d.ts +22 -0
- package/dist/acp.js +1 -0
- package/dist/asks.d.ts +38 -0
- package/dist/asks.js +147 -0
- package/dist/completion.d.ts +84 -0
- package/dist/completion.js +154 -0
- package/dist/config.d.ts +18 -0
- package/dist/config.js +36 -0
- package/dist/connection.d.ts +85 -0
- package/dist/connection.js +1 -0
- package/dist/controller.d.ts +59 -0
- package/dist/controller.js +235 -0
- package/dist/diff.d.ts +6 -0
- package/dist/diff.js +137 -0
- package/dist/images.d.ts +48 -0
- package/dist/images.js +45 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/input.d.ts +42 -0
- package/dist/input.js +37 -0
- package/dist/layout.d.ts +25 -0
- package/dist/layout.js +48 -0
- package/dist/locations.d.ts +33 -0
- package/dist/locations.js +116 -0
- package/dist/questions.d.ts +28 -0
- package/dist/questions.js +67 -0
- package/dist/reduce.d.ts +67 -0
- package/dist/reduce.js +559 -0
- package/dist/state.d.ts +200 -0
- package/dist/state.js +25 -0
- package/dist/text.d.ts +64 -0
- package/dist/text.js +190 -0
- package/dist/timing.d.ts +18 -0
- package/dist/timing.js +43 -0
- package/dist/title.d.ts +10 -0
- package/dist/title.js +15 -0
- package/dist/v1.d.ts +60 -0
- package/dist/v1.js +311 -0
- package/package.json +18 -3
package/dist/v1.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { unifiedDiff } from "./diff.js";
|
|
2
|
+
const CANCELLED = { code: -32800, message: "Request cancelled" };
|
|
3
|
+
/** Why a prompt fails once the connection to the agent has closed. */
|
|
4
|
+
const GONE = { code: -32603, message: "The agent has stopped" };
|
|
5
|
+
/**
|
|
6
|
+
* `connection` seen as an ACP v2 session, so the Chat keeps one model of a
|
|
7
|
+
* session whichever protocol the agent speaks. v1 tells foreground work by a
|
|
8
|
+
* prompt's request lasting the turn; this tells it as v2 does, by state
|
|
9
|
+
* updates, and gives messages the ids v1 leaves out.
|
|
10
|
+
*
|
|
11
|
+
* With acp-adapter's queue, prompts sent during a turn go to the agent at
|
|
12
|
+
* once and are held there. A plain v1 agent takes one prompt at a time, so
|
|
13
|
+
* they wait here until the turn ends.
|
|
14
|
+
*/
|
|
15
|
+
export function fromV1(connection) {
|
|
16
|
+
const queued = connection.queue !== undefined;
|
|
17
|
+
let client = null;
|
|
18
|
+
let running = null;
|
|
19
|
+
/** The connection closed: no turn starts again. */
|
|
20
|
+
let closed = false;
|
|
21
|
+
let held = [];
|
|
22
|
+
let serial = 0;
|
|
23
|
+
const ids = new MessageIds(() => `v1-${serial++}`);
|
|
24
|
+
const emit = (update, sessionId = connection.sessionId) => client?.sessionUpdate({ sessionId, update: update });
|
|
25
|
+
// An agent that sends config options has them stand for its modes, as v2 has only them.
|
|
26
|
+
const configured = (connection.configOptions?.length ?? 0) > 0;
|
|
27
|
+
let modes = configured ? null : (connection.modes ?? null);
|
|
28
|
+
const initialOptions = configured
|
|
29
|
+
? (connection.configOptions ?? []).map(optionFromV1)
|
|
30
|
+
: modes
|
|
31
|
+
? [modesOption(modes)]
|
|
32
|
+
: null;
|
|
33
|
+
const start = (submission) => {
|
|
34
|
+
running = submission;
|
|
35
|
+
ids.interrupt(connection.sessionId);
|
|
36
|
+
const messageId = `user-${serial++}`;
|
|
37
|
+
emit({ sessionUpdate: "state_update", state: "running" });
|
|
38
|
+
emit({ sessionUpdate: "user_message", messageId, content: submission.prompt });
|
|
39
|
+
submission.resolve({ messageId });
|
|
40
|
+
if (!submission.sent)
|
|
41
|
+
send(submission);
|
|
42
|
+
};
|
|
43
|
+
const finish = (stop) => {
|
|
44
|
+
running = null;
|
|
45
|
+
emit({ sessionUpdate: "state_update", state: "idle", ...stop });
|
|
46
|
+
if (closed)
|
|
47
|
+
return;
|
|
48
|
+
const next = held.shift();
|
|
49
|
+
if (next)
|
|
50
|
+
start(next);
|
|
51
|
+
};
|
|
52
|
+
const send = (submission) => {
|
|
53
|
+
submission.sent = true;
|
|
54
|
+
const meta = { idempotencyKey: submission.key };
|
|
55
|
+
if (submission.steer && queued)
|
|
56
|
+
meta.acpAdapter = { steer: true };
|
|
57
|
+
connection.prompt({ prompt: submission.prompt, _meta: meta }).then((response) => {
|
|
58
|
+
if (running === submission) {
|
|
59
|
+
const thought = thinkingTokensOf(response);
|
|
60
|
+
finish({
|
|
61
|
+
stopReason: response.stopReason,
|
|
62
|
+
...(response.usage ? { usage: response.usage } : {}),
|
|
63
|
+
...(thought !== null ? { _meta: { acpAdapter: { thinkingTokens: thought } } } : {}),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// Dropped before its turn began: withdrawn, or cancelled with the turn.
|
|
68
|
+
held = held.filter((h) => h !== submission);
|
|
69
|
+
submission.reject(CANCELLED);
|
|
70
|
+
}
|
|
71
|
+
}, (error) => {
|
|
72
|
+
if (running === submission) {
|
|
73
|
+
finish({ stopReason: "error", error: errorOf(error) });
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
held = held.filter((h) => h !== submission);
|
|
77
|
+
submission.reject(error);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
sessionId: connection.sessionId,
|
|
83
|
+
abilities: { steer: true, withdraw: true },
|
|
84
|
+
...(connection.signInRequired ? { signInRequired: true } : {}),
|
|
85
|
+
...(connection.promptCapabilities ? { promptCapabilities: connection.promptCapabilities } : {}),
|
|
86
|
+
...(connection.auth ? { auth: connection.auth } : {}),
|
|
87
|
+
...(initialOptions ? { configOptions: initialOptions } : {}),
|
|
88
|
+
prompt({ prompt, idempotencyKey, steer }) {
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
if (closed) {
|
|
91
|
+
reject(GONE);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const submission = {
|
|
95
|
+
prompt,
|
|
96
|
+
key: idempotencyKey,
|
|
97
|
+
steer: steer === true,
|
|
98
|
+
resolve,
|
|
99
|
+
reject,
|
|
100
|
+
sent: false,
|
|
101
|
+
};
|
|
102
|
+
if (!running) {
|
|
103
|
+
start(submission);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (steer)
|
|
107
|
+
held.unshift(submission);
|
|
108
|
+
else
|
|
109
|
+
held.push(submission);
|
|
110
|
+
if (queued)
|
|
111
|
+
send(submission);
|
|
112
|
+
else if (steer)
|
|
113
|
+
void connection.cancel();
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
async cancel() {
|
|
117
|
+
if (!queued) {
|
|
118
|
+
for (const h of held.splice(0))
|
|
119
|
+
h.reject(CANCELLED);
|
|
120
|
+
}
|
|
121
|
+
await connection.cancel();
|
|
122
|
+
},
|
|
123
|
+
async withdraw(key) {
|
|
124
|
+
const submission = held.find((h) => h.key === key);
|
|
125
|
+
if (!submission)
|
|
126
|
+
return false;
|
|
127
|
+
if (!queued) {
|
|
128
|
+
held = held.filter((h) => h !== submission);
|
|
129
|
+
submission.reject(CANCELLED);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
if (!submission.promptId || !connection.queue)
|
|
133
|
+
return false;
|
|
134
|
+
return connection.queue.remove(submission.promptId);
|
|
135
|
+
},
|
|
136
|
+
connect(next) {
|
|
137
|
+
client = next;
|
|
138
|
+
const stop = connection.connect({
|
|
139
|
+
sessionUpdate(notification) {
|
|
140
|
+
const u = notification.update;
|
|
141
|
+
if (u.sessionUpdate === "current_mode_update") {
|
|
142
|
+
if (!modes || notification.sessionId !== connection.sessionId)
|
|
143
|
+
return;
|
|
144
|
+
modes = { ...modes, currentModeId: u.currentModeId ?? modes.currentModeId };
|
|
145
|
+
emit({ sessionUpdate: "config_option_update", configOptions: [modesOption(modes)] });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const update = updateFromV1(notification.update, notification.sessionId, ids);
|
|
149
|
+
if (update)
|
|
150
|
+
emit(update, notification.sessionId);
|
|
151
|
+
},
|
|
152
|
+
requestPermission(request) {
|
|
153
|
+
const toolCall = toolCallFromV1(request.toolCall);
|
|
154
|
+
return next.requestPermission({
|
|
155
|
+
sessionId: request.sessionId,
|
|
156
|
+
title: request.toolCall.title ?? "Permission",
|
|
157
|
+
subject: { type: "tool_call", toolCall },
|
|
158
|
+
options: request.options,
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
createElicitation(request) {
|
|
162
|
+
return next.createElicitation(request);
|
|
163
|
+
},
|
|
164
|
+
authStatus(status) {
|
|
165
|
+
next.authStatus?.(status);
|
|
166
|
+
},
|
|
167
|
+
closed(error) {
|
|
168
|
+
closed = true;
|
|
169
|
+
running = null;
|
|
170
|
+
for (const h of held.splice(0))
|
|
171
|
+
h.reject(GONE);
|
|
172
|
+
next.closed?.(error);
|
|
173
|
+
},
|
|
174
|
+
queueChanged(prompts) {
|
|
175
|
+
// acp-adapter holds prompts in the order they were sent, as `held` does.
|
|
176
|
+
const waiting = held.filter((h) => h.sent);
|
|
177
|
+
prompts.forEach((p, i) => {
|
|
178
|
+
const submission = waiting[i];
|
|
179
|
+
if (submission)
|
|
180
|
+
submission.promptId = p.promptId;
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
return () => {
|
|
185
|
+
stop();
|
|
186
|
+
if (client === next)
|
|
187
|
+
client = null;
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/** The ids of the messages a v1 agent streams without ids, per session. */
|
|
193
|
+
class MessageIds {
|
|
194
|
+
#next;
|
|
195
|
+
#current = new Map();
|
|
196
|
+
constructor(next) {
|
|
197
|
+
this.#next = next;
|
|
198
|
+
}
|
|
199
|
+
/** The id of a chunk of `kind`: the message being streamed, or a new one. */
|
|
200
|
+
of(sessionId, kind) {
|
|
201
|
+
const current = this.#current.get(sessionId);
|
|
202
|
+
if (current?.kind === kind)
|
|
203
|
+
return current.id;
|
|
204
|
+
const id = this.#next();
|
|
205
|
+
this.#current.set(sessionId, { kind, id });
|
|
206
|
+
return id;
|
|
207
|
+
}
|
|
208
|
+
/** Something other than a chunk came: the next chunk starts a new message. */
|
|
209
|
+
interrupt(sessionId) {
|
|
210
|
+
this.#current.delete(sessionId);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/** A v1 update in v2's terms; none for what v2 has no place for. */
|
|
214
|
+
function updateFromV1(update, sessionId, ids) {
|
|
215
|
+
const u = update;
|
|
216
|
+
switch (u.sessionUpdate) {
|
|
217
|
+
case "user_message_chunk":
|
|
218
|
+
case "agent_message_chunk":
|
|
219
|
+
case "agent_thought_chunk": {
|
|
220
|
+
const messageId = u.messageId ?? ids.of(sessionId, u.sessionUpdate);
|
|
221
|
+
return { ...u, messageId };
|
|
222
|
+
}
|
|
223
|
+
default:
|
|
224
|
+
ids.interrupt(sessionId);
|
|
225
|
+
}
|
|
226
|
+
switch (u.sessionUpdate) {
|
|
227
|
+
case "tool_call":
|
|
228
|
+
case "tool_call_update":
|
|
229
|
+
return {
|
|
230
|
+
...toolCallFromV1(u),
|
|
231
|
+
sessionUpdate: "tool_call_update",
|
|
232
|
+
};
|
|
233
|
+
case "plan":
|
|
234
|
+
return {
|
|
235
|
+
sessionUpdate: "plan_update",
|
|
236
|
+
plan: { type: "items", planId: "plan", entries: u.entries },
|
|
237
|
+
};
|
|
238
|
+
case "available_commands_update":
|
|
239
|
+
return {
|
|
240
|
+
sessionUpdate: "available_commands_update",
|
|
241
|
+
availableCommands: u.availableCommands.map((c) => ({
|
|
242
|
+
type: "command",
|
|
243
|
+
...c,
|
|
244
|
+
})),
|
|
245
|
+
};
|
|
246
|
+
case "current_mode_update":
|
|
247
|
+
return null;
|
|
248
|
+
case "config_option_update":
|
|
249
|
+
return {
|
|
250
|
+
sessionUpdate: "config_option_update",
|
|
251
|
+
configOptions: u.configOptions.map(optionFromV1),
|
|
252
|
+
};
|
|
253
|
+
default:
|
|
254
|
+
return u;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** A v1 config option as v2 names it: its `id` is v2's `configId`. */
|
|
258
|
+
function optionFromV1(option) {
|
|
259
|
+
const { id, ...rest } = option;
|
|
260
|
+
return { ...rest, configId: id };
|
|
261
|
+
}
|
|
262
|
+
/** v1's modes as the mode setting v2 reports in config options. */
|
|
263
|
+
function modesOption(modes) {
|
|
264
|
+
return {
|
|
265
|
+
type: "select",
|
|
266
|
+
configId: "mode",
|
|
267
|
+
name: "Mode",
|
|
268
|
+
category: "mode",
|
|
269
|
+
currentValue: modes.currentModeId,
|
|
270
|
+
options: modes.availableModes.map((m) => ({
|
|
271
|
+
value: m.id,
|
|
272
|
+
name: m.name,
|
|
273
|
+
...(m.description ? { description: m.description } : {}),
|
|
274
|
+
})),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function toolCallFromV1(call) {
|
|
278
|
+
const { content, ...rest } = call;
|
|
279
|
+
return {
|
|
280
|
+
...rest,
|
|
281
|
+
...(content != null ? { content: content.map(contentFromV1) } : {}),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
function contentFromV1(content) {
|
|
285
|
+
if (content.type !== "diff")
|
|
286
|
+
return content;
|
|
287
|
+
const diff = content;
|
|
288
|
+
return {
|
|
289
|
+
type: "diff",
|
|
290
|
+
changes: [{ operation: diff.oldText == null ? "add" : "modify", path: diff.path }],
|
|
291
|
+
patch: { format: "git_patch", text: unifiedDiff(diff.oldText, diff.newText) },
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function errorOf(error) {
|
|
295
|
+
if (error && typeof error === "object" && "message" in error) {
|
|
296
|
+
const e = error;
|
|
297
|
+
return { code: typeof e.code === "number" ? e.code : -32603, message: String(e.message) };
|
|
298
|
+
}
|
|
299
|
+
return { code: -32603, message: String(error) };
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The tokens acp-adapter says the turn thought without saying what, in
|
|
303
|
+
* `_meta.acpAdapter.thinkingTokens` of the response or of its usage.
|
|
304
|
+
*/
|
|
305
|
+
function thinkingTokensOf(response) {
|
|
306
|
+
const from = (meta) => meta?.acpAdapter
|
|
307
|
+
?.thinkingTokens;
|
|
308
|
+
const tokens = from(response._meta) ??
|
|
309
|
+
from(response.usage?._meta);
|
|
310
|
+
return typeof tokens === "number" ? tokens : null;
|
|
311
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adroit-app/chat-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Chat's view model of an ACP session: the reducer, the controller, the connection interface and the ACP v1 adapter",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/adroit-app/adroit-chat.git",
|
|
9
9
|
"directory": "packages/chat-core"
|
|
10
10
|
},
|
|
11
|
-
"
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@agentclientprotocol/sdk": "^2.12.1"
|
|
26
|
+
}
|
|
12
27
|
}
|