bunnyquery 1.21.13 → 1.21.14
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 +13 -0
- package/bunnyquery.css +6 -0
- package/bunnyquery.js +313 -13
- package/dist/engine.cjs +282 -11
- package/dist/engine.cjs.map +1 -1
- package/dist/engine.d.mts +68 -1
- package/dist/engine.d.ts +68 -1
- package/dist/engine.mjs +279 -12
- package/dist/engine.mjs.map +1 -1
- package/package.json +1 -1
- package/src/engine/activity.ts +253 -0
- package/src/engine/index.ts +3 -0
- package/src/engine/prompts/chat_system_prompt.ts +1 -1
- package/src/engine/session.ts +5 -0
- package/src/engine/sse.ts +110 -3
- package/styles/chat.css +6 -0
package/package.json
CHANGED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The progress line under a waiting chat turn: "Searching your files for "oil leak"...".
|
|
3
|
+
*
|
|
4
|
+
* A turn takes about 20 s, and until its first word of answer arrives the bubble
|
|
5
|
+
* used to show nothing but a dot trail. The stream already says what the model is
|
|
6
|
+
* doing (which tool, with what input, whether it answered), so this turns that into
|
|
7
|
+
* one sentence. Both clients render the sentence and nothing else, so they cannot
|
|
8
|
+
* describe the same moment differently.
|
|
9
|
+
*
|
|
10
|
+
* HOW THE SENTENCE IS PICKED
|
|
11
|
+
* - The step comes from the latest stream snapshot (noteChatActivity, called by
|
|
12
|
+
* the session on every live paint): thinking (no tool yet), a tool running,
|
|
13
|
+
* between tools (the last one answered, the model is deciding), or writing.
|
|
14
|
+
* - Each step has a short list of sentences. The list advances every
|
|
15
|
+
* ROTATE_MS while the step lasts and loops after its end, so a slow search
|
|
16
|
+
* or a long first think reads as progress instead of a frozen label.
|
|
17
|
+
* - A turn with no stream yet (before the first chunk, or a client that does not
|
|
18
|
+
* stream) is "thinking", timed from when the bubble was first asked about.
|
|
19
|
+
*
|
|
20
|
+
* The trailing dots are NOT part of the sentence: each client draws its own
|
|
21
|
+
* animated dot trail after it (.bq-loader), so they keep moving even when nothing
|
|
22
|
+
* new arrives for a while.
|
|
23
|
+
*
|
|
24
|
+
* Render-only, DOM-free and framework-free like the rest of the engine. Nothing
|
|
25
|
+
* here reads or writes chat state; the two maps below are display memory only and
|
|
26
|
+
* forget entries on their own.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { SseSnapshot, SseToolCall } from './sse';
|
|
30
|
+
|
|
31
|
+
/** How long one sentence stays before the next one in its list. */
|
|
32
|
+
export const CHAT_ACTIVITY_ROTATE_MS = 4000;
|
|
33
|
+
|
|
34
|
+
export type ChatActivityStep = 'thinking' | 'tool' | 'between' | 'writing';
|
|
35
|
+
|
|
36
|
+
export interface ChatActivity {
|
|
37
|
+
step: ChatActivityStep;
|
|
38
|
+
/** The running tool's name ('tool' step only). */
|
|
39
|
+
tool?: string;
|
|
40
|
+
/** The running tool's input, parsed, when it has arrived ('tool' step only). */
|
|
41
|
+
input?: any;
|
|
42
|
+
/** Wall-clock ms when this step began. */
|
|
43
|
+
since: number;
|
|
44
|
+
/** Identifies the step, so a repeat snapshot of the same step keeps `since`. */
|
|
45
|
+
key: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type Lang = 'en' | 'ko';
|
|
49
|
+
|
|
50
|
+
/** Latest activity per server item id, written by the session's live paint. */
|
|
51
|
+
var byItem = new Map<string, ChatActivity>();
|
|
52
|
+
/** When each waiting bubble was first seen, by object and by its ids. */
|
|
53
|
+
var startByObj = new WeakMap<object, number>();
|
|
54
|
+
var startById = new Map<string, number>();
|
|
55
|
+
var MAX_IDS = 200;
|
|
56
|
+
|
|
57
|
+
function remember(map: Map<string, any>, key: string, value: any): void {
|
|
58
|
+
if (map.has(key)) map.delete(key);
|
|
59
|
+
map.set(key, value);
|
|
60
|
+
if (map.size > MAX_IDS) {
|
|
61
|
+
var oldest = map.keys().next();
|
|
62
|
+
if (!oldest.done) map.delete(oldest.value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function stepOf(snap: SseSnapshot | null | undefined): { step: ChatActivityStep; call?: SseToolCall; count: number } {
|
|
67
|
+
if (!snap) return { step: 'thinking', count: 0 };
|
|
68
|
+
var calls = Array.isArray(snap.toolCalls) ? snap.toolCalls : [];
|
|
69
|
+
if (typeof snap.text === 'string' && /\S/.test(snap.text)) return { step: 'writing', count: calls.length };
|
|
70
|
+
if (!calls.length) return { step: 'thinking', count: 0 };
|
|
71
|
+
var last = calls[calls.length - 1];
|
|
72
|
+
if (last && !last.done) return { step: 'tool', call: last, count: calls.length };
|
|
73
|
+
return { step: 'between', count: calls.length };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Record what a streaming turn is doing now. Called by the session with every
|
|
78
|
+
* snapshot it paints; cheap, and a repeat of the same step keeps its start time
|
|
79
|
+
* (only a newly arrived input is taken).
|
|
80
|
+
*/
|
|
81
|
+
export function noteChatActivity(serverItemId: string, snap: SseSnapshot | null | undefined, now?: number): void {
|
|
82
|
+
if (!serverItemId) return;
|
|
83
|
+
var t = typeof now === 'number' ? now : Date.now();
|
|
84
|
+
var s = stepOf(snap);
|
|
85
|
+
var key = s.step + ':' + s.count;
|
|
86
|
+
var prev = byItem.get(serverItemId);
|
|
87
|
+
if (prev && prev.key === key) {
|
|
88
|
+
if (s.call && s.call.input !== undefined && prev.input === undefined) prev.input = s.call.input;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
var act: ChatActivity = { step: s.step, since: t, key: key };
|
|
92
|
+
if (s.call) {
|
|
93
|
+
act.tool = s.call.name;
|
|
94
|
+
if (s.call.input !== undefined) act.input = s.call.input;
|
|
95
|
+
}
|
|
96
|
+
remember(byItem, serverItemId, act);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The recorded activity for a bubble, by its server item id. */
|
|
100
|
+
export function chatActivityOf(msg: any): ChatActivity | null {
|
|
101
|
+
var id = msg && typeof msg._serverItemId === 'string' ? msg._serverItemId : '';
|
|
102
|
+
return (id && byItem.get(id)) || null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** When this waiting bubble was first seen. Survives the bubble gaining its server
|
|
106
|
+
* id (same object) and a history refetch replacing the object (same id). */
|
|
107
|
+
function turnStart(msg: any, now: number): number {
|
|
108
|
+
var ids: string[] = [];
|
|
109
|
+
if (msg && typeof msg._serverItemId === 'string' && msg._serverItemId) ids.push(msg._serverItemId);
|
|
110
|
+
if (msg && typeof msg._localId === 'string' && msg._localId) ids.push(msg._localId);
|
|
111
|
+
var t: number | undefined;
|
|
112
|
+
if (msg && typeof msg === 'object') t = startByObj.get(msg);
|
|
113
|
+
for (var i = 0; t === undefined && i < ids.length; i++) t = startById.get(ids[i]);
|
|
114
|
+
if (t === undefined) t = now;
|
|
115
|
+
if (msg && typeof msg === 'object') startByObj.set(msg, t);
|
|
116
|
+
for (var j = 0; j < ids.length; j++) if (startById.get(ids[j]) !== t) remember(startById, ids[j], t);
|
|
117
|
+
return t;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* ── wording ─────────────────────────────────────────────────────────────── */
|
|
121
|
+
|
|
122
|
+
function langOf(lang?: string): Lang {
|
|
123
|
+
return typeof lang === 'string' && /^ko\b/i.test(lang) ? 'ko' : 'en';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function clip(v: any, max: number): string {
|
|
127
|
+
if (typeof v !== 'string') return '';
|
|
128
|
+
var s = v.replace(/\s+/g, ' ').trim();
|
|
129
|
+
if (s.length > max) s = s.slice(0, max - 1).trim() + '…';
|
|
130
|
+
return s;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A file's own name out of a stored path: "src::manuals/BP-252.pdf" -> "BP-252.pdf". */
|
|
134
|
+
function fileName(v: any): string {
|
|
135
|
+
if (typeof v !== 'string' || !v) return '';
|
|
136
|
+
var s = v.replace(/^(src::|db:)/, '');
|
|
137
|
+
var parts = s.split('/').filter(function (p) { return !!p; });
|
|
138
|
+
return clip(parts.length ? parts[parts.length - 1] : s, 40);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
var T = {
|
|
142
|
+
en: {
|
|
143
|
+
thinking: ['Reading your question', 'Thinking about where to look', 'Planning the search', 'Still thinking, this can take a moment', 'Working out the best way to find it', 'Getting ready to look through your data'],
|
|
144
|
+
between: ['Looking at what it found', 'Deciding what to check next', 'Putting the pieces together', 'Checking that it has what it needs', 'Still working on it'],
|
|
145
|
+
writing: ['Writing the answer'],
|
|
146
|
+
still: 'Still working on it',
|
|
147
|
+
working: 'Working',
|
|
148
|
+
},
|
|
149
|
+
ko: {
|
|
150
|
+
thinking: ['질문을 읽는 중', '어디를 찾아볼지 생각하는 중', '검색 방법을 정하는 중', '아직 생각하는 중, 잠시만 기다려 주세요', '가장 좋은 방법을 찾는 중', '데이터를 살펴볼 준비를 하는 중'],
|
|
151
|
+
between: ['찾은 내용을 살펴보는 중', '다음에 확인할 것을 정하는 중', '내용을 정리하는 중', '필요한 내용이 다 있는지 확인하는 중', '아직 작업 중'],
|
|
152
|
+
writing: ['답변을 작성하는 중'],
|
|
153
|
+
still: '아직 작업 중',
|
|
154
|
+
working: '작업 중',
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/** The sentence list for one tool. `i` is the call's parsed input (may be empty). */
|
|
159
|
+
function toolLines(tool: string, i: any, lang: Lang): string[] {
|
|
160
|
+
var ko = lang === 'ko';
|
|
161
|
+
var inp = i && typeof i === 'object' ? i : {};
|
|
162
|
+
var q = clip(inp.query || inp.question, 40);
|
|
163
|
+
var f = fileName(inp.path);
|
|
164
|
+
var tbl = clip(inp.table_name, 30);
|
|
165
|
+
switch (tool) {
|
|
166
|
+
case 'searchText':
|
|
167
|
+
return ko ? [
|
|
168
|
+
q ? (f ? f + '에서 "' + q + '" 찾는 중' : '파일에서 "' + q + '" 찾는 중') : '파일에서 찾는 중',
|
|
169
|
+
'모든 페이지를 확인하는 중',
|
|
170
|
+
'아직 찾는 중, 큰 프로젝트는 조금 더 걸려요',
|
|
171
|
+
'계속 찾는 중, 기다려 주셔서 감사해요',
|
|
172
|
+
] : [
|
|
173
|
+
q ? (f ? 'Searching ' + f + ' for "' + q + '"' : 'Searching your files for "' + q + '"') : 'Searching your files',
|
|
174
|
+
'Checking every page',
|
|
175
|
+
'Still searching, big projects take a bit longer',
|
|
176
|
+
'Still searching, thanks for waiting',
|
|
177
|
+
];
|
|
178
|
+
case 'readFileContent':
|
|
179
|
+
return ko ? [
|
|
180
|
+
f ? f + ' 읽는 중' : '파일을 읽는 중',
|
|
181
|
+
'페이지를 살펴보는 중',
|
|
182
|
+
'아직 읽는 중, 긴 파일은 조금 더 걸려요',
|
|
183
|
+
] : [
|
|
184
|
+
f ? 'Reading ' + f : 'Reading a file',
|
|
185
|
+
'Going through the pages',
|
|
186
|
+
'Still reading, long files take a bit longer',
|
|
187
|
+
];
|
|
188
|
+
case 'getFileOutline':
|
|
189
|
+
return ko ? [f ? f + ' 구성을 살펴보는 중' : '파일 구성을 살펴보는 중', '목차를 확인하는 중', T.ko.still]
|
|
190
|
+
: [f ? 'Looking at how ' + f + ' is laid out' : 'Looking at how the file is laid out', 'Checking the table of contents', T.en.still];
|
|
191
|
+
case 'getRecords':
|
|
192
|
+
return ko ? [
|
|
193
|
+
tbl ? tbl + ' 테이블에서 기록을 찾는 중' : '기록을 찾는 중',
|
|
194
|
+
'행을 살펴보는 중',
|
|
195
|
+
'아직 찾는 중, 큰 테이블은 조금 더 걸려요',
|
|
196
|
+
] : [
|
|
197
|
+
tbl ? 'Looking up records in ' + tbl : 'Looking up records',
|
|
198
|
+
'Going through the rows',
|
|
199
|
+
'Still looking, big tables take a bit longer',
|
|
200
|
+
];
|
|
201
|
+
case 'exportRecordsToFile':
|
|
202
|
+
return ko ? [tbl ? tbl + ' 기록을 파일로 만드는 중' : '기록을 파일로 만드는 중', '파일을 만드는 중', '아직 만드는 중, 큰 테이블은 조금 더 걸려요']
|
|
203
|
+
: [tbl ? 'Exporting records from ' + tbl : 'Exporting records', 'Building the file', 'Still exporting, big tables take a bit longer'];
|
|
204
|
+
case 'writeReport':
|
|
205
|
+
return ko ? ['보고서를 작성하는 중', '아직 작성하는 중, 긴 보고서는 조금 더 걸려요']
|
|
206
|
+
: ['Writing the report', 'Still writing, long reports take a bit longer'];
|
|
207
|
+
case 'getTables': return ko ? ['어떤 테이블이 있는지 확인하는 중', T.ko.still] : ['Checking which tables there are', T.en.still];
|
|
208
|
+
case 'getTags': return ko ? ['태그를 확인하는 중', T.ko.still] : ['Checking tags', T.en.still];
|
|
209
|
+
case 'getIndexes': return ko ? ['테이블 항목을 확인하는 중', T.ko.still] : ["Checking the table's fields", T.en.still];
|
|
210
|
+
case 'getUniqueId': return ko ? ['레코드 ID를 확인하는 중', T.ko.still] : ['Checking record IDs', T.en.still];
|
|
211
|
+
case 'postRecords': return ko ? ['기록을 저장하는 중', T.ko.still] : ['Saving records', T.en.still];
|
|
212
|
+
case 'updateRecords': return ko ? ['기록을 수정하는 중', T.ko.still] : ['Updating records', T.en.still];
|
|
213
|
+
case 'deleteRecords': return ko ? ['기록을 삭제하는 중', T.ko.still] : ['Deleting records', T.en.still];
|
|
214
|
+
case 'getTemporaryUrl': return ko ? [f ? f + ' 링크를 만드는 중' : '링크를 만드는 중', T.ko.still] : [f ? 'Getting a link to ' + f : 'Getting a link', T.en.still];
|
|
215
|
+
case 'getToolResponsePage': return ko ? ['결과의 다음 부분을 읽는 중', T.ko.still] : ['Reading the next part of the results', T.en.still];
|
|
216
|
+
case 'correctSearchLink': return ko ? ['페이지 링크를 확인하는 중', T.ko.still] : ['Checking the page link', T.en.still];
|
|
217
|
+
case 'getProjectInfos': return ko ? ['프로젝트 정보를 확인하는 중', T.ko.still] : ['Checking the project details', T.en.still];
|
|
218
|
+
case 'getProfile': return ko ? ['계정 정보를 확인하는 중', T.ko.still] : ['Checking your account', T.en.still];
|
|
219
|
+
case 'web_search':
|
|
220
|
+
case 'web_search_call':
|
|
221
|
+
return ko ? ['웹에서 찾는 중', T.ko.still] : ['Searching the web', T.en.still];
|
|
222
|
+
default:
|
|
223
|
+
return ko ? [T.ko.working, T.ko.still] : [T.en.working, T.en.still];
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The sentence for `elapsed` ms into a step. Past the end of the list it loops,
|
|
228
|
+
* from `loopFrom` on, so a long step keeps changing instead of freezing on its
|
|
229
|
+
* last sentence (the thinking list skips "Reading your question" on the loop). */
|
|
230
|
+
function pick(lines: string[], elapsed: number, loopFrom?: number): string {
|
|
231
|
+
var n = Math.floor(Math.max(0, elapsed) / CHAT_ACTIVITY_ROTATE_MS);
|
|
232
|
+
if (n < lines.length) return lines[n];
|
|
233
|
+
var from = Math.min(Math.max(0, loopFrom || 0), lines.length - 1);
|
|
234
|
+
var span = lines.length - from;
|
|
235
|
+
return lines[from + ((n - lines.length) % span)];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The sentence for a waiting assistant bubble right now, WITHOUT trailing dots.
|
|
240
|
+
* Call it again about once a second to rotate; `lang` is a BCP 47 tag such as
|
|
241
|
+
* navigator.language ("ko-KR" picks Korean, anything else English).
|
|
242
|
+
*/
|
|
243
|
+
export function chatActivityLine(msg: any, now?: number, lang?: string): string {
|
|
244
|
+
var t = typeof now === 'number' ? now : Date.now();
|
|
245
|
+
var L = langOf(lang);
|
|
246
|
+
var start = turnStart(msg, t);
|
|
247
|
+
var act = chatActivityOf(msg);
|
|
248
|
+
if (!act || act.step === 'thinking') return pick(T[L].thinking, t - start, 1);
|
|
249
|
+
var elapsed = t - act.since;
|
|
250
|
+
if (act.step === 'writing') return pick(T[L].writing, elapsed);
|
|
251
|
+
if (act.step === 'between') return pick(T[L].between, elapsed);
|
|
252
|
+
return pick(toolLines(act.tool || '', act.input, L), elapsed);
|
|
253
|
+
}
|
package/src/engine/index.ts
CHANGED
|
@@ -154,6 +154,9 @@ export * from './ai_agent';
|
|
|
154
154
|
// to be on the barrel or the built engine bundle does not contain it and
|
|
155
155
|
// www.skapi.com's `bunnyquery/engine` import resolves to undefined at runtime.
|
|
156
156
|
export * from './sse';
|
|
157
|
+
// The progress line under a waiting turn ("Searching your files for ..."), worded
|
|
158
|
+
// once here so the widget and agent.vue say the same thing at the same moment.
|
|
159
|
+
export * from './activity';
|
|
157
160
|
export {
|
|
158
161
|
filterListByClearHorizon,
|
|
159
162
|
normalizeTextContent,
|
|
@@ -109,7 +109,7 @@ export function buildChatSystemPrompt(params: ChatSystemPromptParams): string {
|
|
|
109
109
|
: `re-indexing that file (attach the same file again and choose to reindex it)`;
|
|
110
110
|
const groundingRules =
|
|
111
111
|
`ANSWER ONLY FROM THE DATA. Every fact you state about this project's data - a name, a number, a date, a status, what a document says, whether something exists - must come from one of four places: a tool result returned in THIS conversation, a file or image attached to it (including one you read through its attachment link), the project description at the end of this prompt, or what the user told you themselves in this chat. Your general knowledge, the open internet and a plausible guess are NOT sources. Never use them to answer a question about the user's data, to fill a gap in it, or to complete a record that came back partial. A value that is not in what you retrieved is unknown: say it is not in the data instead of supplying a likely one.
|
|
112
|
-
FILES IN ANOTHER LANGUAGE. The files may be written in another language than the question (a Russian or English manual asked about in Korean). When searches in the question's language find nothing, or searchText reports query_language_mismatch, search again with the question's key terms translated into the language the files
|
|
112
|
+
FILES IN ANOTHER LANGUAGE. The files may be written in another language than the question, in another alphabet or in the same one (a Russian or English manual asked about in Korean, German documents asked about in English). When searches in the question's language find nothing, or searchText reports query_language_mismatch (its text_language names the files' language), search again with the question's key terms translated into the language the files' TEXT is in: searchText names that language for each file ("its text is in German"; only when it cannot tell, the alphabet, "Latin script, e.g. English"), and earlier hits show it; passing the question as the DOCUMENTS rule says lets searchText tell the question's language; a file NAME can be in another language than its text (a "РЭ" manual written in English), so go by the text. One or two short queries of the main terms (e.g. "oil leak", "gearbox"), and if the first translation finds nothing, try the other likely language (English is the most common for technical manuals), before concluding anything.
|
|
113
113
|
WHEN THE DATA DOES NOT HAVE IT, SAY SO. Once you have searched the way these rules require (in the files' language too) and nothing answers the question, tell the user plainly, in their language, that this project's data does not contain it, and say what you searched (the tables, files or terms) so they can tell missing data from a missed search. Do not answer from memory instead, and do not turn a missing answer into a general explanation of the topic. Offer what would let you answer it: ${canUpload === false
|
|
114
114
|
? `another name or file to look under, or asking the project's owner to add it`
|
|
115
115
|
: `the document to upload, or another name or file to look under`}.
|
package/src/engine/session.ts
CHANGED
|
@@ -52,6 +52,7 @@ import { LookScheduler, LOOK_STALE_MS, runSettleSideEffect, claimSideEffect, sid
|
|
|
52
52
|
// The wire-format knowledge. skapi relays the provider's bytes without reading them,
|
|
53
53
|
// so the session hands them straight to this parser and never inspects a frame itself.
|
|
54
54
|
import { createSseParser, type SseParser } from './sse';
|
|
55
|
+
import { noteChatActivity } from './activity';
|
|
55
56
|
import { isErrorResponseBody, isAuthExpiredError, isNonRetryableRequestError, getErrorMessage, isCsrStatusEnvelope, isTransportFailure, isNotExistsError, isRetriedFailure } from './errors';
|
|
56
57
|
import { buildBoundedChatMessages } from './budget';
|
|
57
58
|
import { extractRetrievalNote, type RetrievalNote } from './retrieval_memory';
|
|
@@ -1942,6 +1943,10 @@ export class ChatSession {
|
|
|
1942
1943
|
private _paintLiveStream(st: LiveStreamState): void {
|
|
1943
1944
|
if (st.ended) return;
|
|
1944
1945
|
st.lastPaintAt = nowMs();
|
|
1946
|
+
// The waiting bubble's progress line ("Searching your files for ..."). Noted
|
|
1947
|
+
// before the on-screen checks below so a turn running in a chat the reader has
|
|
1948
|
+
// left still has the right line when they come back. Display memory only.
|
|
1949
|
+
noteChatActivity(st.id, st.parser.snapshot());
|
|
1945
1950
|
// Another project (or another platform) is on screen. The turn keeps parsing -
|
|
1946
1951
|
// its answer is still being assembled and will still be finalized - but nothing
|
|
1947
1952
|
// is painted, because the index would point into a different chat's list.
|
package/src/engine/sse.ts
CHANGED
|
@@ -133,6 +133,16 @@ export interface SseToolCall {
|
|
|
133
133
|
type: string;
|
|
134
134
|
/** Present on Anthropic mcp_tool_use only. */
|
|
135
135
|
serverName?: string;
|
|
136
|
+
/** The provider's id for the call (Anthropic block id, OpenAI item id), when it
|
|
137
|
+
* gave one. A result block names its call by this. */
|
|
138
|
+
id?: string;
|
|
139
|
+
/** The call's arguments, parsed, once they have fully arrived. Absent while they
|
|
140
|
+
* are still streaming and when they would not parse. Render-only: it is what a
|
|
141
|
+
* "searching for ..." line reads, never something to act on. */
|
|
142
|
+
input?: any;
|
|
143
|
+
/** The tool has answered (a result block or a finished item arrived for it, or
|
|
144
|
+
* the model went back to writing after it). False while it is still running. */
|
|
145
|
+
done?: boolean;
|
|
136
146
|
}
|
|
137
147
|
|
|
138
148
|
export interface SseSnapshot {
|
|
@@ -389,6 +399,8 @@ export function createSseParser(): SseParser {
|
|
|
389
399
|
var error: any = null;
|
|
390
400
|
var stopReason: string | null = null;
|
|
391
401
|
var toolCalls: SseToolCall[] = [];
|
|
402
|
+
/** OpenAI tool-argument deltas by output index, for SseToolCall.input only. */
|
|
403
|
+
var argBuf: Record<number, string> = {};
|
|
392
404
|
var malformedFrames = 0;
|
|
393
405
|
var malformedToolJson = 0;
|
|
394
406
|
|
|
@@ -590,7 +602,10 @@ export function createSseParser(): SseParser {
|
|
|
590
602
|
type: block.type,
|
|
591
603
|
};
|
|
592
604
|
if (typeof block.server_name === 'string') call.serverName = block.server_name;
|
|
605
|
+
if (typeof block.id === 'string' && block.id) call.id = block.id;
|
|
593
606
|
toolCalls.push(call);
|
|
607
|
+
} else if (block && typeof block.type === 'string') {
|
|
608
|
+
markClaudeCallsDone(block);
|
|
594
609
|
}
|
|
595
610
|
return;
|
|
596
611
|
}
|
|
@@ -620,6 +635,10 @@ export function createSseParser(): SseParser {
|
|
|
620
635
|
var j = numberOr(ev.index, -1);
|
|
621
636
|
var s = j >= 0 ? blocks.get(j) : undefined;
|
|
622
637
|
if (s && s.sawJson) finishToolJson(s);
|
|
638
|
+
if (s && s.block && s.block.input && typeof s.block.input === 'object') {
|
|
639
|
+
var sc = callAt(j);
|
|
640
|
+
if (sc) sc.input = s.block.input;
|
|
641
|
+
}
|
|
623
642
|
return;
|
|
624
643
|
}
|
|
625
644
|
|
|
@@ -701,6 +720,39 @@ export function createSseParser(): SseParser {
|
|
|
701
720
|
}
|
|
702
721
|
}
|
|
703
722
|
|
|
723
|
+
/** The tool call drawn from the block at `index`, newest first. Render-only. */
|
|
724
|
+
function callAt(index: number): SseToolCall | null {
|
|
725
|
+
for (var k = toolCalls.length - 1; k >= 0; k--) {
|
|
726
|
+
if (toolCalls[k].index === index) return toolCalls[k];
|
|
727
|
+
}
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/** A non-tool block started. A result block finishes the call it names; text or
|
|
732
|
+
* thinking means the model is writing again, so every call before it answered.
|
|
733
|
+
* Feeds SseToolCall.done only, which nothing but a progress line reads. */
|
|
734
|
+
function markClaudeCallsDone(block: any): void {
|
|
735
|
+
var t = block.type as string;
|
|
736
|
+
if (/tool_result$/.test(t)) {
|
|
737
|
+
var ref = typeof block.tool_use_id === 'string' ? block.tool_use_id : '';
|
|
738
|
+
for (var k = 0; k < toolCalls.length; k++) {
|
|
739
|
+
if (ref && toolCalls[k].id === ref) toolCalls[k].done = true;
|
|
740
|
+
}
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
if (t === 'text' || t === 'thinking' || t === 'redacted_thinking') {
|
|
744
|
+
for (var m = 0; m < toolCalls.length; m++) toolCalls[m].done = true;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** OpenAI tool arguments arrive as a JSON string. Parsed for display only; a
|
|
749
|
+
* string that will not parse is simply not shown. */
|
|
750
|
+
function parseArgs(args: any): any {
|
|
751
|
+
if (args && typeof args === 'object') return args;
|
|
752
|
+
if (typeof args !== 'string' || !args) return undefined;
|
|
753
|
+
try { return JSON.parse(args); } catch (e) { return undefined; }
|
|
754
|
+
}
|
|
755
|
+
|
|
704
756
|
function deltaBlockType(deltaType: any): string {
|
|
705
757
|
if (deltaType === 'thinking_delta' || deltaType === 'signature_delta') return 'thinking';
|
|
706
758
|
if (deltaType === 'input_json_delta') return 'tool_use';
|
|
@@ -748,13 +800,63 @@ export function createSseParser(): SseParser {
|
|
|
748
800
|
if (type === 'response.output_item.added') {
|
|
749
801
|
var item = ev.item;
|
|
750
802
|
if (item && typeof item.type === 'string' && OPENAI_TOOL_ITEMS[item.type]) {
|
|
751
|
-
|
|
803
|
+
var oc: SseToolCall = {
|
|
752
804
|
index: numberOr(ev.output_index, toolCalls.length),
|
|
753
805
|
// A built-in tool (web_search_call) has no name of its own, so the item
|
|
754
806
|
// type is the only label there is and a row can still be drawn.
|
|
755
807
|
name: typeof item.name === 'string' && item.name ? item.name : item.type,
|
|
756
808
|
type: item.type,
|
|
757
|
-
}
|
|
809
|
+
};
|
|
810
|
+
if (typeof item.id === 'string' && item.id) oc.id = item.id;
|
|
811
|
+
var oa = parseArgs(item.arguments);
|
|
812
|
+
if (oa !== undefined) oc.input = oa;
|
|
813
|
+
toolCalls.push(oc);
|
|
814
|
+
}
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Progress-line bookkeeping only (SseToolCall.input / .done). None of these
|
|
819
|
+
// feed the text, the body or any keep/store decision.
|
|
820
|
+
//
|
|
821
|
+
// The DELTAS are read, not just the done frame: the relay cuts chunks where it
|
|
822
|
+
// likes, and a done frame split across two chunks has been seen to reach the
|
|
823
|
+
// browser only when the tool FINISHED (the rest sat in the relay while the
|
|
824
|
+
// tool ran and no new bytes came). The arguments are one small delta in
|
|
825
|
+
// practice, so the call's input is known the moment it starts.
|
|
826
|
+
if (type === 'response.mcp_call_arguments.delta' || type === 'response.function_call_arguments.delta') {
|
|
827
|
+
var gi = numberOr(ev.output_index, -1);
|
|
828
|
+
var gc = callAt(gi);
|
|
829
|
+
if (gc && gc.input === undefined && typeof ev.delta === 'string') {
|
|
830
|
+
argBuf[gi] = (argBuf[gi] || '') + ev.delta;
|
|
831
|
+
var gt = argBuf[gi].trim();
|
|
832
|
+
if (gt.charAt(gt.length - 1) === '}') {
|
|
833
|
+
var ga = parseArgs(gt);
|
|
834
|
+
if (ga !== undefined) gc.input = ga;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (type === 'response.mcp_call_arguments.done' || type === 'response.function_call_arguments.done') {
|
|
840
|
+
var ac = callAt(numberOr(ev.output_index, -1));
|
|
841
|
+
var aa = parseArgs(ev.arguments);
|
|
842
|
+
if (ac && aa !== undefined) ac.input = aa;
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
if (type === 'response.mcp_call.completed' || type === 'response.mcp_call.failed') {
|
|
846
|
+
var cc = callAt(numberOr(ev.output_index, -1));
|
|
847
|
+
if (cc) cc.done = true;
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
if (type === 'response.output_item.done') {
|
|
851
|
+
var di = ev.item;
|
|
852
|
+
if (di && typeof di.type === 'string' && OPENAI_TOOL_ITEMS[di.type]) {
|
|
853
|
+
var dc = callAt(numberOr(ev.output_index, -1));
|
|
854
|
+
if (dc) {
|
|
855
|
+
var da = parseArgs(di.arguments);
|
|
856
|
+
if (da !== undefined) dc.input = da;
|
|
857
|
+
else if (di.action && typeof di.action === 'object' && dc.input === undefined) dc.input = di.action;
|
|
858
|
+
dc.done = true;
|
|
859
|
+
}
|
|
758
860
|
}
|
|
759
861
|
return;
|
|
760
862
|
}
|
|
@@ -992,7 +1094,12 @@ export function createSseParser(): SseParser {
|
|
|
992
1094
|
provider: provider,
|
|
993
1095
|
text: currentText(),
|
|
994
1096
|
thinkingText: currentThinking(),
|
|
995
|
-
|
|
1097
|
+
// Copied per call, not just per array: a call's input/done fill in after it
|
|
1098
|
+
// is first seen, and a snapshot already handed out must not change under
|
|
1099
|
+
// its reader.
|
|
1100
|
+
toolCalls: toolCalls.map(function (t) {
|
|
1101
|
+
return Object.assign({}, t);
|
|
1102
|
+
}),
|
|
996
1103
|
toolNames: toolCalls.map(function (t) {
|
|
997
1104
|
return t.name;
|
|
998
1105
|
}),
|
package/styles/chat.css
CHANGED
|
@@ -83,6 +83,12 @@
|
|
|
83
83
|
.bq-message.is-assistant:not(.is-error):not(.is-cancelled):not(.bq-index-pass) .bq-bubble:has(> .bq-loader) > .bq-loader {
|
|
84
84
|
vertical-align: middle;
|
|
85
85
|
}
|
|
86
|
+
/* The waiting turn's progress line ("Searching your files for ..."): a sentence
|
|
87
|
+
from the engine (chatActivityLine) sitting right before the dot trail, so the
|
|
88
|
+
dots read as its ending. Middle-aligned with the inline bunny like the dots. */
|
|
89
|
+
.bq-bubble > .bq-activity-text {
|
|
90
|
+
vertical-align: middle;
|
|
91
|
+
}
|
|
86
92
|
/* The dot-trail cycles '.', '..', '...', so left to itself it is a box that
|
|
87
93
|
changes width three times every 1.2s: the bubble pulsed with it, and the
|
|
88
94
|
stop-waiting control that follows the dots moved each time, which made it hard
|