tandem-kit-chat 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 +38 -1
- package/dist/src/chats.d.ts +27 -0
- package/dist/src/chats.js +108 -0
- package/dist/src/commands.d.ts +22 -0
- package/dist/src/commands.js +43 -0
- package/dist/src/controller.d.ts +143 -0
- package/dist/src/controller.js +600 -0
- package/dist/src/fake.d.ts +59 -0
- package/dist/src/fake.js +98 -0
- package/dist/src/focus.d.ts +29 -0
- package/dist/src/focus.js +61 -0
- package/dist/src/format.d.ts +88 -0
- package/dist/src/format.js +174 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +13 -0
- package/dist/src/labels.d.ts +156 -0
- package/dist/src/labels.js +162 -0
- package/dist/src/locales.d.ts +21 -0
- package/dist/src/locales.js +474 -0
- package/dist/src/queue.d.ts +23 -0
- package/dist/src/queue.js +42 -0
- package/dist/src/reduce.d.ts +5 -0
- package/dist/src/reduce.js +318 -0
- package/dist/src/store.d.ts +10 -0
- package/dist/src/store.js +21 -0
- package/dist/src/tools.d.ts +9 -0
- package/dist/src/tools.js +23 -0
- package/dist/src/transport.d.ts +69 -0
- package/dist/src/transport.js +27 -0
- package/dist/src/types.d.ts +191 -0
- package/dist/src/types.js +1 -0
- package/package.json +40 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class MessageQueue {
|
|
2
|
+
items = [];
|
|
3
|
+
steerId = null;
|
|
4
|
+
n = 0;
|
|
5
|
+
add(text, meta) {
|
|
6
|
+
const item = { id: `q${++this.n}`, text, ...(meta ? { meta } : {}) };
|
|
7
|
+
this.items.push(item);
|
|
8
|
+
return item;
|
|
9
|
+
}
|
|
10
|
+
remove(id) {
|
|
11
|
+
this.items = this.items.filter((item) => item.id !== id);
|
|
12
|
+
if (this.steerId === id)
|
|
13
|
+
this.steerId = null;
|
|
14
|
+
}
|
|
15
|
+
steer(id) {
|
|
16
|
+
const item = this.items.find((it) => it.id === id);
|
|
17
|
+
if (!item)
|
|
18
|
+
return null;
|
|
19
|
+
this.items = [item, ...this.items.filter((it) => it !== item)];
|
|
20
|
+
this.steerId = id;
|
|
21
|
+
return item;
|
|
22
|
+
}
|
|
23
|
+
next(options = {}) {
|
|
24
|
+
if (!this.items.length)
|
|
25
|
+
return null;
|
|
26
|
+
if (options.interrupted && this.steerId !== this.items[0].id)
|
|
27
|
+
return null;
|
|
28
|
+
this.steerId = null;
|
|
29
|
+
return this.items.shift();
|
|
30
|
+
}
|
|
31
|
+
putBack(item) {
|
|
32
|
+
this.items.unshift(item);
|
|
33
|
+
this.steerId = null;
|
|
34
|
+
}
|
|
35
|
+
clear() {
|
|
36
|
+
this.items = [];
|
|
37
|
+
this.steerId = null;
|
|
38
|
+
}
|
|
39
|
+
get size() {
|
|
40
|
+
return this.items.length;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type TandemEvent } from 'tandem-kit-core';
|
|
2
|
+
import type { ChatState } from './types.js';
|
|
3
|
+
export declare function initialChatState(): ChatState;
|
|
4
|
+
export declare function reduce(state: ChatState, event: TandemEvent): ChatState;
|
|
5
|
+
export declare function foldChat(events: TandemEvent[], from?: ChatState): ChatState;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { initialSessionState, reduceSession } from 'tandem-kit-core';
|
|
2
|
+
import { SILENT_TOOLS, isDeclined, isPlainExit } from "./tools.js";
|
|
3
|
+
export function initialChatState() {
|
|
4
|
+
return { items: [], session: initialSessionState(), activeTurn: false, thinkingSince: null, turnStartedAt: null, turnWaitedMs: 0, waitingSince: null, n: 0 };
|
|
5
|
+
}
|
|
6
|
+
const timeOf = (event) => Date.parse(event.ts) || 0;
|
|
7
|
+
function lastIndex(items, test) {
|
|
8
|
+
for (let i = items.length - 1; i >= 0; i -= 1)
|
|
9
|
+
if (test(items[i]))
|
|
10
|
+
return i;
|
|
11
|
+
return -1;
|
|
12
|
+
}
|
|
13
|
+
function swap(items, index, item) {
|
|
14
|
+
const out = items.slice();
|
|
15
|
+
out[index] = item;
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
function add(state, item) {
|
|
19
|
+
const n = state.n + 1;
|
|
20
|
+
const full = { ...item, key: item.key ?? `${item.kind}:${n}` };
|
|
21
|
+
return { ...state, n, items: [...state.items, full] };
|
|
22
|
+
}
|
|
23
|
+
function patchByKey(state, key, patch) {
|
|
24
|
+
const index = lastIndex(state.items, (item) => item.key === key);
|
|
25
|
+
if (index === -1)
|
|
26
|
+
return state;
|
|
27
|
+
return { ...state, items: swap(state.items, index, patch(state.items[index])) };
|
|
28
|
+
}
|
|
29
|
+
function findCall(items, id) {
|
|
30
|
+
for (let i = items.length - 1; i >= 0; i -= 1) {
|
|
31
|
+
const item = items[i];
|
|
32
|
+
if (item.kind !== 'steps')
|
|
33
|
+
continue;
|
|
34
|
+
const call = item.calls.find((c) => c.id === id);
|
|
35
|
+
if (call)
|
|
36
|
+
return { index: i, group: item, call };
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function patchCall(state, id, patch) {
|
|
41
|
+
const hit = findCall(state.items, id);
|
|
42
|
+
if (!hit)
|
|
43
|
+
return state;
|
|
44
|
+
const calls = hit.group.calls.map((call) => (call.id === id ? patch(call) : call));
|
|
45
|
+
return { ...state, items: swap(state.items, hit.index, { ...hit.group, calls }) };
|
|
46
|
+
}
|
|
47
|
+
function settleLive(state) {
|
|
48
|
+
const last = state.items.at(-1);
|
|
49
|
+
const waiting = state.session.pending.length > 0;
|
|
50
|
+
let items = state.items;
|
|
51
|
+
state.items.forEach((item, i) => {
|
|
52
|
+
if (item.kind !== 'steps')
|
|
53
|
+
return;
|
|
54
|
+
const live = state.activeTurn && !waiting && item === last && item.calls.some((call) => !call.hidden);
|
|
55
|
+
if (item.live !== live) {
|
|
56
|
+
if (items === state.items)
|
|
57
|
+
items = items.slice();
|
|
58
|
+
items[i] = { ...item, live };
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return items === state.items ? state : { ...state, items };
|
|
62
|
+
}
|
|
63
|
+
function endTurn(state) {
|
|
64
|
+
const items = state.items.map((item) => {
|
|
65
|
+
if (item.kind === 'assistant' && item.streaming)
|
|
66
|
+
return { ...item, streaming: false };
|
|
67
|
+
if (item.kind === 'steps' && (item.live || item.calls.some((c) => c.status === 'running'))) {
|
|
68
|
+
return { ...item, live: false, calls: item.calls.map((c) => (c.status === 'running' ? { ...c, status: 'stopped' } : c)) };
|
|
69
|
+
}
|
|
70
|
+
if (item.kind === 'approval' && item.status === 'open')
|
|
71
|
+
return { ...item, status: 'expired' };
|
|
72
|
+
if (item.kind === 'question' && item.status === 'open')
|
|
73
|
+
return { ...item, status: 'expired' };
|
|
74
|
+
return item;
|
|
75
|
+
});
|
|
76
|
+
return { ...state, items, activeTurn: false, thinkingSince: null, turnStartedAt: null, turnWaitedMs: 0, waitingSince: null };
|
|
77
|
+
}
|
|
78
|
+
function stepGroup(state, at) {
|
|
79
|
+
const last = state.items.at(-1);
|
|
80
|
+
if (last?.kind === 'steps')
|
|
81
|
+
return { state, key: last.key };
|
|
82
|
+
const next = add(state, { kind: 'steps', at, calls: [], live: state.activeTurn });
|
|
83
|
+
return { state: next, key: next.items.at(-1).key };
|
|
84
|
+
}
|
|
85
|
+
function applyEvent(state, event) {
|
|
86
|
+
const at = timeOf(event);
|
|
87
|
+
switch (event.type) {
|
|
88
|
+
case 'user':
|
|
89
|
+
return add({ ...state, activeTurn: true }, { kind: 'user', key: `user:${event.seq ?? state.n + 1}`, at, text: event.text, note: event.note ?? null });
|
|
90
|
+
case 'turn_started':
|
|
91
|
+
return { ...state, activeTurn: true, turnStartedAt: at, turnWaitedMs: 0, waitingSince: null };
|
|
92
|
+
case 'session_started':
|
|
93
|
+
return add(state, {
|
|
94
|
+
kind: 'session',
|
|
95
|
+
at,
|
|
96
|
+
resumed: Boolean(event.resumed),
|
|
97
|
+
model: event.model ?? null,
|
|
98
|
+
harness: event.harness,
|
|
99
|
+
effort: 'effort' in event ? (event.effort ?? null) : (state.session.settings?.effort ?? null),
|
|
100
|
+
permissionMode: event.permission_mode ?? state.session.settings?.permission_mode ?? null,
|
|
101
|
+
});
|
|
102
|
+
case 'thinking':
|
|
103
|
+
return { ...state, thinkingSince: state.thinkingSince ?? (at || Date.now()) };
|
|
104
|
+
case 'text_delta':
|
|
105
|
+
case 'message': {
|
|
106
|
+
const key = `text:${event.message_id}:${event.index ?? 0}`;
|
|
107
|
+
const base = { ...state, thinkingSince: null };
|
|
108
|
+
const index = lastIndex(base.items, (item) => item.key === key);
|
|
109
|
+
if (index === -1) {
|
|
110
|
+
return add(base, { kind: 'assistant', key, at, text: event.text, streaming: event.type === 'text_delta' });
|
|
111
|
+
}
|
|
112
|
+
const item = base.items[index];
|
|
113
|
+
const patched = event.type === 'text_delta' ? { ...item, text: item.text + event.text } : { ...item, text: event.text, streaming: false };
|
|
114
|
+
return { ...base, items: swap(base.items, index, patched) };
|
|
115
|
+
}
|
|
116
|
+
case 'tool_call': {
|
|
117
|
+
const base = { ...state, thinkingSince: null };
|
|
118
|
+
if (SILENT_TOOLS.has(event.name))
|
|
119
|
+
return base;
|
|
120
|
+
if (event.name === 'TodoWrite') {
|
|
121
|
+
const todos = Array.isArray(event.input?.todos) ? event.input.todos : [];
|
|
122
|
+
const index = lastIndex(base.items, (item) => item.kind === 'todos');
|
|
123
|
+
const turnStart = lastIndex(base.items, (item) => item.kind === 'user');
|
|
124
|
+
if (index > turnStart)
|
|
125
|
+
return { ...base, items: swap(base.items, index, { ...base.items[index], todos }) };
|
|
126
|
+
return add(base, { kind: 'todos', at, todos });
|
|
127
|
+
}
|
|
128
|
+
const { state: grouped, key } = stepGroup(base, at);
|
|
129
|
+
const waiting = grouped.items.find((item) => item.kind === 'approval' && item.plan === null && item.toolUseId === event.id && !item.shown);
|
|
130
|
+
const call = {
|
|
131
|
+
id: event.id,
|
|
132
|
+
name: event.name,
|
|
133
|
+
summary: event.summary ?? '',
|
|
134
|
+
input: event.input ?? {},
|
|
135
|
+
reason: event.reason ?? '',
|
|
136
|
+
status: 'running',
|
|
137
|
+
result: null,
|
|
138
|
+
startedAt: at || Date.now(),
|
|
139
|
+
};
|
|
140
|
+
if (waiting) {
|
|
141
|
+
call.prompt = waiting.id;
|
|
142
|
+
if (waiting.status !== 'allowed' && waiting.status !== 'always')
|
|
143
|
+
call.hidden = true;
|
|
144
|
+
if (waiting.status !== 'open' && waiting.status !== 'allowed' && waiting.status !== 'always')
|
|
145
|
+
call.declined = true;
|
|
146
|
+
}
|
|
147
|
+
let next = patchByKey(grouped, key, (item) => ({ ...item, calls: [...item.calls, call] }));
|
|
148
|
+
if (waiting)
|
|
149
|
+
next = patchByKey(next, waiting.key, (item) => ({ ...item, shown: true }));
|
|
150
|
+
return next;
|
|
151
|
+
}
|
|
152
|
+
case 'tool_result': {
|
|
153
|
+
const hit = findCall(state.items, event.id);
|
|
154
|
+
if (!hit)
|
|
155
|
+
return state;
|
|
156
|
+
const declined = event.is_error && (hit.call.declined || isDeclined(event.text));
|
|
157
|
+
const status = declined ? 'declined' : event.is_error ? (isPlainExit(hit.call.name, event.text) ? 'exited' : 'failed') : 'done';
|
|
158
|
+
return patchCall(state, event.id, (call) => ({ ...call, status, result: { text: event.text ?? '', summary: event.summary ?? '', is_error: Boolean(event.is_error), images: resultImages(event.images) } }));
|
|
159
|
+
}
|
|
160
|
+
case 'approval': {
|
|
161
|
+
const plan = typeof event.plan === 'string' ? event.plan : null;
|
|
162
|
+
let next = add(state, {
|
|
163
|
+
kind: 'approval',
|
|
164
|
+
key: `approval:${event.id}`,
|
|
165
|
+
at,
|
|
166
|
+
id: event.id,
|
|
167
|
+
status: 'open',
|
|
168
|
+
tool: event.tool,
|
|
169
|
+
display: event.display_name ?? event.tool,
|
|
170
|
+
summary: event.summary ?? '',
|
|
171
|
+
input: event.input ?? {},
|
|
172
|
+
description: event.description ?? null,
|
|
173
|
+
blockedPath: event.blocked_path ?? null,
|
|
174
|
+
plan,
|
|
175
|
+
always: event.always ?? null,
|
|
176
|
+
toolUseId: event.tool_use_id ?? null,
|
|
177
|
+
shown: false,
|
|
178
|
+
message: null,
|
|
179
|
+
mode: null,
|
|
180
|
+
rules: [],
|
|
181
|
+
});
|
|
182
|
+
if (plan === null && event.tool_use_id) {
|
|
183
|
+
const hit = findCall(next.items, event.tool_use_id);
|
|
184
|
+
if (hit && hit.call.status === 'running' && !hit.call.prompt) {
|
|
185
|
+
next = patchCall(next, event.tool_use_id, (call) => ({ ...call, prompt: event.id, hidden: true }));
|
|
186
|
+
next = patchByKey(next, `approval:${event.id}`, (item) => ({ ...item, shown: true }));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return next;
|
|
190
|
+
}
|
|
191
|
+
case 'approval_resolved': {
|
|
192
|
+
const key = `approval:${event.id}`;
|
|
193
|
+
const status = event.allow ? (event.always ? 'always' : 'allowed') : 'denied';
|
|
194
|
+
let next = patchByKey(state, key, (item) => ({ ...item, status, message: event.message ?? null, mode: event.mode ?? null, rules: event.rules ?? [] }));
|
|
195
|
+
const linked = next.items.flatMap((item) => (item.kind === 'steps' ? item.calls : [])).find((call) => call.prompt === event.id);
|
|
196
|
+
if (linked)
|
|
197
|
+
next = patchCall(next, linked.id, (call) => (event.allow ? { ...call, hidden: false } : { ...call, declined: true }));
|
|
198
|
+
return next;
|
|
199
|
+
}
|
|
200
|
+
case 'question':
|
|
201
|
+
return add(state, { kind: 'question', key: `question:${event.id}`, at, id: event.id, status: 'open', questions: event.questions ?? [], answers: null });
|
|
202
|
+
case 'question_answered':
|
|
203
|
+
return patchByKey(state, `question:${event.id}`, (item) => ({ ...item, status: event.answers ? 'answered' : 'skipped', answers: event.answers ?? null }));
|
|
204
|
+
case 'prompt_closed': {
|
|
205
|
+
let next = patchByKey(state, `approval:${event.id}`, (item) => (item.kind === 'approval' && item.status === 'open' ? { ...item, status: 'expired' } : item));
|
|
206
|
+
next = patchByKey(next, `question:${event.id}`, (item) => (item.kind === 'question' && item.status === 'open' ? { ...item, status: 'expired' } : item));
|
|
207
|
+
const linked = next.items.flatMap((item) => (item.kind === 'steps' ? item.calls : [])).find((call) => call.prompt === event.id);
|
|
208
|
+
return linked ? patchCall(next, linked.id, (call) => ({ ...call, declined: true })) : next;
|
|
209
|
+
}
|
|
210
|
+
case 'change':
|
|
211
|
+
return add(state, {
|
|
212
|
+
kind: 'change',
|
|
213
|
+
key: `change:${event.doc ?? ''}:${event.version}`,
|
|
214
|
+
at,
|
|
215
|
+
version: event.version,
|
|
216
|
+
actor: event.actor,
|
|
217
|
+
delta: event.delta ?? [],
|
|
218
|
+
reason: event.reason ?? null,
|
|
219
|
+
summary: event.summary ?? '',
|
|
220
|
+
findings: event.findings ?? [],
|
|
221
|
+
revision: event.revision ?? null,
|
|
222
|
+
doc: event.doc ?? null,
|
|
223
|
+
});
|
|
224
|
+
case 'rejected':
|
|
225
|
+
return add(state, { kind: 'rejected', at, actor: event.actor, findings: event.findings ?? [], reason: event.reason ?? null, doc: event.doc ?? null });
|
|
226
|
+
case 'clash':
|
|
227
|
+
return add(state, { kind: 'clash', key: `clash:${event.id}`, at, id: event.id, path: event.path ?? [], keptActor: event.kept_actor, droppedActor: event.dropped_actor, doc: event.doc ?? null, status: 'open' });
|
|
228
|
+
case 'clash_resolved':
|
|
229
|
+
return patchByKey(state, `clash:${event.id}`, (item) => ({ ...item, status: event.choice }));
|
|
230
|
+
case 'proposal': {
|
|
231
|
+
const key = `proposal:${event.id}`;
|
|
232
|
+
const fields = {
|
|
233
|
+
status: event.state,
|
|
234
|
+
proposalKind: event.kind,
|
|
235
|
+
title: event.title ?? null,
|
|
236
|
+
reason: event.reason ?? null,
|
|
237
|
+
doc: event.doc ?? null,
|
|
238
|
+
delta: event.delta ?? [],
|
|
239
|
+
data: event.data ?? null,
|
|
240
|
+
problems: event.problems ?? [],
|
|
241
|
+
edited: Boolean(event.edited),
|
|
242
|
+
by: event.by ?? null,
|
|
243
|
+
result: event.result ?? null,
|
|
244
|
+
};
|
|
245
|
+
if (lastIndex(state.items, (item) => item.key === key) === -1)
|
|
246
|
+
return add(state, { kind: 'proposal', key, at, id: event.id, ...fields });
|
|
247
|
+
return patchByKey(state, key, (item) => ({ ...item, ...fields }));
|
|
248
|
+
}
|
|
249
|
+
case 'compaction': {
|
|
250
|
+
if (event.phase === 'started')
|
|
251
|
+
return add(state, { kind: 'compaction', at, status: 'running', trigger: event.trigger ?? null, pre: null, post: null });
|
|
252
|
+
const index = lastIndex(state.items, (item) => item.kind === 'compaction' && item.status === 'running');
|
|
253
|
+
const done = event.phase === 'done';
|
|
254
|
+
const fields = { status: done ? 'done' : 'failed', trigger: event.trigger ?? null, pre: event.pre_tokens ?? null, post: event.post_tokens ?? null };
|
|
255
|
+
let next;
|
|
256
|
+
let at2;
|
|
257
|
+
if (index === -1) {
|
|
258
|
+
next = add(state, { kind: 'compaction', at, ...fields });
|
|
259
|
+
at2 = next.items.length - 1;
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
next = { ...state, items: swap(state.items, index, { ...state.items[index], ...fields }) };
|
|
263
|
+
at2 = index;
|
|
264
|
+
}
|
|
265
|
+
if (!done)
|
|
266
|
+
return next;
|
|
267
|
+
const items = next.items.map((item, i) => (i < at2 && item.kind !== 'compaction' && !item.summarized ? { ...item, summarized: true } : item));
|
|
268
|
+
return { ...next, items };
|
|
269
|
+
}
|
|
270
|
+
case 'notice':
|
|
271
|
+
return add(state, { kind: 'notice', at, level: event.level, text: event.text });
|
|
272
|
+
case 'turn_finished': {
|
|
273
|
+
const ended = endTurn(state);
|
|
274
|
+
const limit = event.limit;
|
|
275
|
+
if (limit)
|
|
276
|
+
return add(ended, { kind: 'notice', at, level: 'warn', text: '', limit });
|
|
277
|
+
if (event.interrupted)
|
|
278
|
+
return add(ended, { kind: 'turn', at, tone: 'muted', error: null });
|
|
279
|
+
if (event.is_error)
|
|
280
|
+
return add(ended, { kind: 'turn', at, tone: 'error', error: event.error ? String(event.error) : null });
|
|
281
|
+
return ended;
|
|
282
|
+
}
|
|
283
|
+
case 'ended':
|
|
284
|
+
return add(endTurn(state), { kind: 'notice', at, level: 'info', text: '', ended: { reason: String(event.reason), wasBusy: typeof event.was_busy === 'boolean' ? event.was_busy : null } });
|
|
285
|
+
default:
|
|
286
|
+
if (typeof event.type === 'string' && event.type.startsWith('app.'))
|
|
287
|
+
return add(state, { kind: 'app', at, type: event.type, event });
|
|
288
|
+
return state;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function trackWaiting(before, after, at) {
|
|
292
|
+
const was = before.session.pending.length > 0;
|
|
293
|
+
const now = after.session.pending.length > 0;
|
|
294
|
+
if (was === now)
|
|
295
|
+
return after;
|
|
296
|
+
const stamp = at || Date.now();
|
|
297
|
+
if (now)
|
|
298
|
+
return { ...after, waitingSince: after.waitingSince ?? stamp };
|
|
299
|
+
if (after.waitingSince === null)
|
|
300
|
+
return after;
|
|
301
|
+
return { ...after, turnWaitedMs: after.turnWaitedMs + Math.max(0, stamp - after.waitingSince), waitingSince: null };
|
|
302
|
+
}
|
|
303
|
+
function resultImages(value) {
|
|
304
|
+
if (!Array.isArray(value))
|
|
305
|
+
return [];
|
|
306
|
+
return value.filter((image) => Boolean(image) && typeof image.mime === 'string' && image.mime.startsWith('image/') && typeof image.data === 'string');
|
|
307
|
+
}
|
|
308
|
+
export function reduce(state, event) {
|
|
309
|
+
const seq = event.seq;
|
|
310
|
+
if (seq !== undefined && seq <= state.session.last_seq)
|
|
311
|
+
return state;
|
|
312
|
+
const session = reduceSession(state.session, event);
|
|
313
|
+
const next = applyEvent({ ...state, session }, event);
|
|
314
|
+
return settleLive(trackWaiting(state, next, timeOf(event)));
|
|
315
|
+
}
|
|
316
|
+
export function foldChat(events, from = initialChatState()) {
|
|
317
|
+
return events.reduce(reduce, from);
|
|
318
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type Listener = () => void;
|
|
2
|
+
export declare class Store<T> {
|
|
3
|
+
private value;
|
|
4
|
+
private readonly listeners;
|
|
5
|
+
constructor(initial: T);
|
|
6
|
+
getState: () => T;
|
|
7
|
+
subscribe: (listener: Listener) => (() => void);
|
|
8
|
+
protected replace(next: T, notify?: boolean): void;
|
|
9
|
+
protected emit(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class Store {
|
|
2
|
+
value;
|
|
3
|
+
listeners = new Set();
|
|
4
|
+
constructor(initial) {
|
|
5
|
+
this.value = initial;
|
|
6
|
+
}
|
|
7
|
+
getState = () => this.value;
|
|
8
|
+
subscribe = (listener) => {
|
|
9
|
+
this.listeners.add(listener);
|
|
10
|
+
return () => void this.listeners.delete(listener);
|
|
11
|
+
};
|
|
12
|
+
replace(next, notify = true) {
|
|
13
|
+
this.value = next;
|
|
14
|
+
if (notify)
|
|
15
|
+
this.emit();
|
|
16
|
+
}
|
|
17
|
+
emit() {
|
|
18
|
+
for (const listener of [...this.listeners])
|
|
19
|
+
listener();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ToolCall } from './types.js';
|
|
2
|
+
export type ToolKind = 'read' | 'edit' | 'run' | 'tool';
|
|
3
|
+
export declare const SILENT_TOOLS: ReadonlySet<string>;
|
|
4
|
+
export declare function shortToolName(name: string): string;
|
|
5
|
+
export type ToolClassifier = (name: string) => ToolKind;
|
|
6
|
+
export declare const toolKind: ToolClassifier;
|
|
7
|
+
export declare const isDeclined: (text: string | null | undefined) => boolean;
|
|
8
|
+
export declare const isPlainExit: (name: string, text: string | null | undefined) => boolean;
|
|
9
|
+
export declare const visibleCalls: (calls: ToolCall[]) => ToolCall[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const READ_TOOLS = new Set(['Read', 'Glob', 'Grep', 'LS', 'NotebookRead', 'WebFetch', 'WebSearch']);
|
|
2
|
+
const EDIT_TOOLS = new Set(['Write', 'Edit', 'MultiEdit', 'NotebookEdit']);
|
|
3
|
+
const RUN_TOOLS = new Set(['Bash', 'BashOutput', 'KillShell', 'Task']);
|
|
4
|
+
const READ_PREFIX = /^(read|get|list|find|search|check|view|show)(_|$)/i;
|
|
5
|
+
const DECLINED = /interrupted by user|user (doesn'?t|does not) want|user (denied|rejected|declined)|permission to use .* (has been )?denied|was denied|did not allow|not allowed/i;
|
|
6
|
+
export const SILENT_TOOLS = new Set(['AskUserQuestion', 'ExitPlanMode']);
|
|
7
|
+
export function shortToolName(name) {
|
|
8
|
+
return String(name ?? '').replace(/^mcp__[^_]+(?:_[^_]+)*?__/, '');
|
|
9
|
+
}
|
|
10
|
+
export const toolKind = (name) => {
|
|
11
|
+
if (READ_TOOLS.has(name))
|
|
12
|
+
return 'read';
|
|
13
|
+
if (EDIT_TOOLS.has(name))
|
|
14
|
+
return 'edit';
|
|
15
|
+
if (RUN_TOOLS.has(name))
|
|
16
|
+
return 'run';
|
|
17
|
+
if (name.startsWith('mcp__'))
|
|
18
|
+
return READ_PREFIX.test(shortToolName(name)) ? 'read' : 'tool';
|
|
19
|
+
return 'tool';
|
|
20
|
+
};
|
|
21
|
+
export const isDeclined = (text) => DECLINED.test(text ?? '');
|
|
22
|
+
export const isPlainExit = (name, text) => name === 'Bash' && /^Exit code \d+/.test(text ?? '');
|
|
23
|
+
export const visibleCalls = (calls) => calls.filter((call) => !call.hidden);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type ClientOptions, type ProposalEdit, type SessionSummary, type TandemEvent } from 'tandem-kit-core';
|
|
2
|
+
export interface ChatInfo {
|
|
3
|
+
protocol?: string;
|
|
4
|
+
harnesses?: string[];
|
|
5
|
+
models?: {
|
|
6
|
+
id: string;
|
|
7
|
+
label: string;
|
|
8
|
+
note?: string;
|
|
9
|
+
}[];
|
|
10
|
+
efforts?: string[];
|
|
11
|
+
docs?: string[];
|
|
12
|
+
}
|
|
13
|
+
export interface ChatSettings {
|
|
14
|
+
model?: string | null;
|
|
15
|
+
effort?: string | null;
|
|
16
|
+
permission_mode?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface SendExtras {
|
|
19
|
+
attachments?: {
|
|
20
|
+
name: string;
|
|
21
|
+
type: string;
|
|
22
|
+
size: number;
|
|
23
|
+
dataUrl?: string;
|
|
24
|
+
text?: string;
|
|
25
|
+
}[];
|
|
26
|
+
/** pictures to send as the `images` field of the message, so the model gets the image itself */
|
|
27
|
+
images?: {
|
|
28
|
+
media_type: string;
|
|
29
|
+
data: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
export interface ChatTransport {
|
|
34
|
+
capabilities?: {
|
|
35
|
+
attachments?: boolean;
|
|
36
|
+
images?: boolean;
|
|
37
|
+
};
|
|
38
|
+
info?(): Promise<ChatInfo>;
|
|
39
|
+
sessions?(): Promise<{
|
|
40
|
+
sessions: SessionSummary[];
|
|
41
|
+
}>;
|
|
42
|
+
createSession(options?: Record<string, unknown>): Promise<SessionSummary>;
|
|
43
|
+
session(id: string): Promise<SessionSummary>;
|
|
44
|
+
send(id: string, text: string, extras?: SendExtras): Promise<unknown>;
|
|
45
|
+
interrupt(id: string): Promise<unknown>;
|
|
46
|
+
compact(id: string, instructions?: string): Promise<unknown>;
|
|
47
|
+
settings(id: string, settings: ChatSettings): Promise<unknown>;
|
|
48
|
+
approve(id: string, approvalId: string, reply: {
|
|
49
|
+
allow: boolean;
|
|
50
|
+
always?: boolean;
|
|
51
|
+
message?: string;
|
|
52
|
+
mode?: string;
|
|
53
|
+
}): Promise<unknown>;
|
|
54
|
+
answer(id: string, questionId: string, answers: Record<string, string> | null): Promise<unknown>;
|
|
55
|
+
events(id: string, after?: number): Promise<{
|
|
56
|
+
events: TandemEvent[];
|
|
57
|
+
last_seq: number;
|
|
58
|
+
}>;
|
|
59
|
+
sessionEvents(id: string, after?: number, signal?: AbortSignal): AsyncIterable<TandemEvent>;
|
|
60
|
+
deleteSession?(id: string): Promise<unknown>;
|
|
61
|
+
resolveClash?(doc: string, clashId: string, choice: 'kept' | 'dropped'): Promise<unknown>;
|
|
62
|
+
/** decide a proposal the agent made. A transport without these leaves the proposal card showing, with no buttons that work */
|
|
63
|
+
applyProposal?(id: string, options?: {
|
|
64
|
+
edit?: ProposalEdit;
|
|
65
|
+
}): Promise<unknown>;
|
|
66
|
+
discardProposal?(id: string): Promise<unknown>;
|
|
67
|
+
}
|
|
68
|
+
export type HttpTransportOptions = ClientOptions;
|
|
69
|
+
export declare function httpTransport(options: HttpTransportOptions): ChatTransport;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TandemClient } from 'tandem-kit-core';
|
|
2
|
+
export function httpTransport(options) {
|
|
3
|
+
const client = new TandemClient(options);
|
|
4
|
+
return {
|
|
5
|
+
capabilities: { images: true },
|
|
6
|
+
info: () => client.info(),
|
|
7
|
+
sessions: () => client.sessions(),
|
|
8
|
+
createSession: (o) => client.createSession(o),
|
|
9
|
+
session: (id) => client.session(id),
|
|
10
|
+
send: (id, text, extras) => client.send(id, text, extras?.images),
|
|
11
|
+
interrupt: (id) => client.interrupt(id),
|
|
12
|
+
compact: (id, instructions) => client.compact(id, instructions),
|
|
13
|
+
settings: (id, settings) => client.settings(id, { ...settings }),
|
|
14
|
+
approve: (id, approvalId, reply) => client.approve(id, approvalId, reply),
|
|
15
|
+
answer: (id, questionId, answers) => client.answer(id, questionId, answers),
|
|
16
|
+
events: (id, after) => client.events(id, after),
|
|
17
|
+
sessionEvents: (id, after, signal) => client.sessionEvents(id, after, signal),
|
|
18
|
+
deleteSession: async (id) => {
|
|
19
|
+
const reply = await client.request('DELETE', `/v1/sessions/${encodeURIComponent(id)}`);
|
|
20
|
+
if (reply.status >= 400)
|
|
21
|
+
throw Object.assign(new Error(`could not delete the chat (${reply.status})`), { status: reply.status });
|
|
22
|
+
},
|
|
23
|
+
resolveClash: (doc, clashId, choice) => client.request('POST', `/v1/workcopies/${encodeURIComponent(doc)}/clashes/${encodeURIComponent(clashId)}`, { choice, actor: 'human' }),
|
|
24
|
+
applyProposal: (id, o) => client.applyProposal(id, o),
|
|
25
|
+
discardProposal: (id) => client.discardProposal(id),
|
|
26
|
+
};
|
|
27
|
+
}
|