@nexface/chat 0.1.21-alpha.5 → 0.1.21-alpha.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/dist/BUNDLED_DEPENDENCIES.json +12 -0
- package/dist/BUNDLED_LICENSES.txt +50 -0
- package/dist/assistant-components.d.ts +1 -2
- package/dist/assistant-components.js +6 -8
- package/dist/assistant-runtime.d.ts +1 -4
- package/dist/assistant-runtime.js +5 -19
- package/dist/client.js +12 -24
- package/dist/default-chat.d.ts +1 -1
- package/dist/default-chat.js +7864 -1553
- package/dist/messages.d.ts +0 -3
- package/dist/protocol-messages.d.ts +2 -3
- package/dist/protocol-messages.js +27 -145
- package/dist/protocol-response.js +2 -2
- package/dist/react.d.ts +1 -11
- package/dist/react.js +100 -515
- package/dist/storage.d.ts +6 -3
- package/dist/storage.js +44 -50
- package/dist/styles.css +676 -1503
- package/dist/tool-result.d.ts +0 -5
- package/dist/tool-result.js +0 -31
- package/dist/types.d.ts +1 -3
- package/dist/ui/home-demo/action-bar.js +2 -13
- package/dist/ui/home-demo/greeting.d.ts +1 -3
- package/dist/ui/home-demo/greeting.js +2 -2
- package/dist/ui/home-demo/suggestions.d.ts +4 -2
- package/dist/ui/home-demo/suggestions.js +4 -5
- package/dist/ui/home-demo/trace.js +3 -2
- package/dist/ui/markdown-text.d.ts +1 -6
- package/dist/ui/markdown-text.js +2 -39
- package/dist/ui/thread-list.aui.d.ts +1 -2
- package/dist/ui/thread-list.aui.js +2 -6
- package/package.json +3 -4
- package/dist/fonts/Inter-OFL.txt +0 -93
- package/dist/fonts/inter-medium.ttf +0 -0
- package/dist/session-coordinator.d.ts +0 -23
- package/dist/session-coordinator.js +0 -139
- package/dist/session-history.d.ts +0 -29
- package/dist/session-history.js +0 -282
- package/dist/shared/text-only-reply.d.ts +0 -6
- package/dist/shared/text-only-reply.js +0 -10
- package/dist/shared/tool-presentation.d.ts +0 -18
- package/dist/shared/tool-presentation.js +0 -5
- package/dist/shared/tool-result-markdown.d.ts +0 -15
- package/dist/shared/tool-result-markdown.js +0 -55
- package/dist/shared/tool-result-reference.d.ts +0 -2
- package/dist/shared/tool-result-reference.js +0 -5
- package/dist/tool-presentation.d.ts +0 -6
- package/dist/tool-presentation.js +0 -113
package/dist/session-history.js
DELETED
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
import { decodeChatStorage, encodeSession } from "./storage.js";
|
|
2
|
-
import { HistoryError, SessionCoordinator } from "./session-coordinator.js";
|
|
3
|
-
const prefix = "nexface:chat:sessions:v4:";
|
|
4
|
-
export const historyMetaKey = `${prefix}meta`;
|
|
5
|
-
export const historySessionKey = (id) => `${prefix}session:${id}`;
|
|
6
|
-
const failure = (message) => new HistoryError("CHAT_STORAGE_UNAVAILABLE", message);
|
|
7
|
-
const quota = (cause) => cause instanceof DOMException && cause.name === "QuotaExceededError";
|
|
8
|
-
const conflict = () => new HistoryError("CHAT_HISTORY_CONFLICT", "This chat history changed outside this page. Refresh before continuing.");
|
|
9
|
-
const locked = () => new HistoryError("CHAT_SESSION_LOCKED", "This conversation is already open in another tab.");
|
|
10
|
-
export { locked as sessionLockedError };
|
|
11
|
-
function keys() {
|
|
12
|
-
const found = [];
|
|
13
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
14
|
-
const key = localStorage.key(i);
|
|
15
|
-
if (key?.startsWith(`${prefix}session:`))
|
|
16
|
-
found.push(key);
|
|
17
|
-
}
|
|
18
|
-
return found;
|
|
19
|
-
}
|
|
20
|
-
function parseMeta() {
|
|
21
|
-
const raw = localStorage.getItem(historyMetaKey);
|
|
22
|
-
if (raw === null)
|
|
23
|
-
return;
|
|
24
|
-
const value = JSON.parse(raw);
|
|
25
|
-
if (value?.version !== 4 || typeof value.generation !== "string")
|
|
26
|
-
throw failure("Chat history metadata is invalid. Keep the stored data and refresh to retry.");
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
function read() {
|
|
30
|
-
const meta = parseMeta() ?? { version: 4, generation: "initial" };
|
|
31
|
-
const entries = keys().flatMap((key) => {
|
|
32
|
-
const raw = localStorage.getItem(key);
|
|
33
|
-
if (raw === null)
|
|
34
|
-
return [];
|
|
35
|
-
const entry = JSON.parse(raw);
|
|
36
|
-
if (entry?.version !== 4 || typeof entry.generation !== "string")
|
|
37
|
-
throw failure("Stored chat history is invalid.");
|
|
38
|
-
if (entry.generation !== meta.generation)
|
|
39
|
-
return [];
|
|
40
|
-
if (!Number.isSafeInteger(entry.revision) ||
|
|
41
|
-
entry.revision < 1 ||
|
|
42
|
-
!Number.isFinite(entry.createdAt) ||
|
|
43
|
-
!Number.isFinite(entry.lastUsedAt) ||
|
|
44
|
-
historySessionKey(entry.session?.id) !== key)
|
|
45
|
-
throw failure("Stored chat history is invalid.");
|
|
46
|
-
const decoded = decodeChatStorage(JSON.stringify({ version: 3, activeSessionId: entry.session.id, sessions: [entry.session] }));
|
|
47
|
-
if (!decoded)
|
|
48
|
-
throw failure("Stored chat history is invalid.");
|
|
49
|
-
return [entry];
|
|
50
|
-
});
|
|
51
|
-
entries.sort((a, b) => a.createdAt - b.createdAt || a.session.id.localeCompare(b.session.id));
|
|
52
|
-
return { meta, entries };
|
|
53
|
-
}
|
|
54
|
-
// Compare persisted business content independently of recency metadata.
|
|
55
|
-
const signature = (entry) => JSON.stringify(encodeSession(entry.session));
|
|
56
|
-
const fingerprint = (entry) => `${entry.revision}:${signature(entry)}`;
|
|
57
|
-
const restored = (session) => decodeChatStorage(JSON.stringify({ version: 3, activeSessionId: session.id, sessions: [session] })).sessions[0];
|
|
58
|
-
export class SessionHistory {
|
|
59
|
-
coordinator = new SessionCoordinator();
|
|
60
|
-
generation;
|
|
61
|
-
seen = new Map();
|
|
62
|
-
reusable = new Set();
|
|
63
|
-
observed = new Map();
|
|
64
|
-
opened = new Map();
|
|
65
|
-
fresh = new Set();
|
|
66
|
-
baselines = new Map();
|
|
67
|
-
initialized;
|
|
68
|
-
onEvict;
|
|
69
|
-
initialize() {
|
|
70
|
-
return (this.initialized ??= this.coordinator.write(() => {
|
|
71
|
-
this.generation = read().meta.generation;
|
|
72
|
-
}));
|
|
73
|
-
}
|
|
74
|
-
list() {
|
|
75
|
-
const entries = read().entries;
|
|
76
|
-
this.observed.clear();
|
|
77
|
-
for (const entry of entries)
|
|
78
|
-
this.observed.set(entry.session.id, fingerprint(entry));
|
|
79
|
-
return entries.map((entry) => restored(entry.session));
|
|
80
|
-
}
|
|
81
|
-
unchangedSinceSaved(id) {
|
|
82
|
-
return this.seen.has(id) && this.seen.get(id) === this.observed.get(id);
|
|
83
|
-
}
|
|
84
|
-
snapshot(fresh = false) {
|
|
85
|
-
const state = read();
|
|
86
|
-
if (this.generation !== state.meta.generation) {
|
|
87
|
-
if (fresh)
|
|
88
|
-
this.generation = state.meta.generation;
|
|
89
|
-
else
|
|
90
|
-
throw conflict();
|
|
91
|
-
}
|
|
92
|
-
return state;
|
|
93
|
-
}
|
|
94
|
-
async open(id, fresh = false) {
|
|
95
|
-
await this.initialize();
|
|
96
|
-
if (!(await this.coordinator.acquire(id)))
|
|
97
|
-
throw locked();
|
|
98
|
-
try {
|
|
99
|
-
return await this.coordinator.write(() => {
|
|
100
|
-
const state = this.snapshot(fresh);
|
|
101
|
-
const entry = state.entries.find((item) => item.session.id === id);
|
|
102
|
-
if (!entry && !fresh)
|
|
103
|
-
throw new HistoryError("UNKNOWN_SESSION", "This conversation was deleted. Choose another conversation.");
|
|
104
|
-
if (entry) {
|
|
105
|
-
if (this.seen.get(id) === fingerprint(entry))
|
|
106
|
-
this.reusable.add(id);
|
|
107
|
-
else
|
|
108
|
-
this.reusable.delete(id);
|
|
109
|
-
this.seen.set(id, fingerprint(entry));
|
|
110
|
-
this.baselines.set(id, JSON.stringify(encodeSession(restored(entry.session))));
|
|
111
|
-
}
|
|
112
|
-
else
|
|
113
|
-
this.fresh.add(id);
|
|
114
|
-
this.opened.set(id, Math.max(Date.now(), ...state.entries.map((item) => item.lastUsedAt + 1)));
|
|
115
|
-
if (entry) {
|
|
116
|
-
try {
|
|
117
|
-
this.commit({
|
|
118
|
-
...entry,
|
|
119
|
-
lastUsedAt: this.opened.get(id),
|
|
120
|
-
session: encodeSession(entry.session),
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
/* Recency alone must never evict history or block opening. */
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return entry ? restored(entry.session) : undefined;
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
catch (cause) {
|
|
131
|
-
this.coordinator.release(id);
|
|
132
|
-
throw cause;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
isSaved(session) {
|
|
136
|
-
return (this.baselines.get(session.id) === JSON.stringify(encodeSession(session)) ||
|
|
137
|
-
(!session.messages.length && !session.title));
|
|
138
|
-
}
|
|
139
|
-
canReuse(id) {
|
|
140
|
-
return this.reusable.has(id);
|
|
141
|
-
}
|
|
142
|
-
adoptRestored(session) {
|
|
143
|
-
this.baselines.set(session.id, JSON.stringify(encodeSession(session)));
|
|
144
|
-
}
|
|
145
|
-
commit(entry, removed) {
|
|
146
|
-
if (removed)
|
|
147
|
-
localStorage.removeItem(historySessionKey(removed));
|
|
148
|
-
else if (entry)
|
|
149
|
-
localStorage.setItem(historySessionKey(entry.session.id), JSON.stringify(entry));
|
|
150
|
-
}
|
|
151
|
-
async save(session) {
|
|
152
|
-
await this.initialize();
|
|
153
|
-
if (!this.coordinator.owns(session.id))
|
|
154
|
-
throw locked();
|
|
155
|
-
// Encode before deleting anything. Snapshot/encoding errors must never trigger LRU.
|
|
156
|
-
const encoded = encodeSession(session);
|
|
157
|
-
const baseline = JSON.stringify(encoded);
|
|
158
|
-
if (this.baselines.get(session.id) === baseline)
|
|
159
|
-
return;
|
|
160
|
-
await this.coordinator.write(async () => {
|
|
161
|
-
if (!this.coordinator.owns(session.id))
|
|
162
|
-
throw locked();
|
|
163
|
-
let state = this.snapshot(this.fresh.has(session.id));
|
|
164
|
-
let existing = state.entries.find((item) => item.session.id === session.id);
|
|
165
|
-
if (existing ? this.seen.get(session.id) !== fingerprint(existing) : !this.fresh.has(session.id))
|
|
166
|
-
throw conflict();
|
|
167
|
-
if (!existing && !encoded.messages.length && !encoded.title)
|
|
168
|
-
return;
|
|
169
|
-
const entry = {
|
|
170
|
-
version: 4,
|
|
171
|
-
generation: state.meta.generation,
|
|
172
|
-
revision: (existing?.revision ?? 0) + 1,
|
|
173
|
-
createdAt: existing?.createdAt ??
|
|
174
|
-
Math.max(Date.now(), ...state.entries.map((item) => item.createdAt + 1)),
|
|
175
|
-
lastUsedAt: Math.max(Date.now(), this.opened.get(session.id) ?? 0, ...state.entries.map((item) => item.lastUsedAt + 1)),
|
|
176
|
-
session: encoded,
|
|
177
|
-
};
|
|
178
|
-
// Empty sessions and read-only mounts do not create storage records.
|
|
179
|
-
if (localStorage.getItem(historyMetaKey) === null)
|
|
180
|
-
localStorage.setItem(historyMetaKey, JSON.stringify(state.meta));
|
|
181
|
-
const candidates = state.entries
|
|
182
|
-
.filter((item) => item.session.id !== session.id)
|
|
183
|
-
.sort((a, b) => a.lastUsedAt - b.lastUsedAt ||
|
|
184
|
-
a.createdAt - b.createdAt ||
|
|
185
|
-
a.session.id.localeCompare(b.session.id));
|
|
186
|
-
let evicted = 0;
|
|
187
|
-
try {
|
|
188
|
-
for (;;) {
|
|
189
|
-
if (!this.coordinator.owns(session.id))
|
|
190
|
-
throw locked();
|
|
191
|
-
try {
|
|
192
|
-
this.commit(entry);
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
catch (cause) {
|
|
196
|
-
if (!quota(cause))
|
|
197
|
-
throw cause;
|
|
198
|
-
let removed = false;
|
|
199
|
-
while (candidates.length) {
|
|
200
|
-
const candidate = candidates.shift();
|
|
201
|
-
const id = candidate.session.id;
|
|
202
|
-
if (this.coordinator.owns(id) || !(await this.coordinator.acquire(id)))
|
|
203
|
-
continue;
|
|
204
|
-
try {
|
|
205
|
-
// All writers share the storage lock; the session lock protects opening races.
|
|
206
|
-
state = this.snapshot();
|
|
207
|
-
existing = state.entries.find((item) => item.session.id === id);
|
|
208
|
-
if (!existing)
|
|
209
|
-
continue;
|
|
210
|
-
if (!this.coordinator.owns(session.id))
|
|
211
|
-
throw locked();
|
|
212
|
-
this.commit(undefined, id);
|
|
213
|
-
state.entries = state.entries.filter((item) => item.session.id !== id);
|
|
214
|
-
removed = true;
|
|
215
|
-
evicted++;
|
|
216
|
-
this.coordinator.notify();
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
finally {
|
|
220
|
-
this.coordinator.release(id);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
if (!removed)
|
|
224
|
-
throw new HistoryError("CHAT_STORAGE_FULL", "Chat history could not be saved because storage is full. Free space and retry. Older conversations may already have been removed.");
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
this.seen.set(session.id, fingerprint(entry));
|
|
228
|
-
this.baselines.set(session.id, baseline);
|
|
229
|
-
this.fresh.delete(session.id);
|
|
230
|
-
this.coordinator.notify();
|
|
231
|
-
}
|
|
232
|
-
finally {
|
|
233
|
-
if (evicted)
|
|
234
|
-
this.onEvict?.(evicted);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
async remove(ids, clearAll = false) {
|
|
239
|
-
await this.initialize();
|
|
240
|
-
const temporary = [];
|
|
241
|
-
let modified = false;
|
|
242
|
-
try {
|
|
243
|
-
// Never wait for a session while holding the global writer lock.
|
|
244
|
-
await this.coordinator.write(async () => {
|
|
245
|
-
const state = this.snapshot();
|
|
246
|
-
const targets = clearAll
|
|
247
|
-
? [...new Set([...ids, ...state.entries.map((entry) => entry.session.id)])]
|
|
248
|
-
: ids;
|
|
249
|
-
for (const id of [...targets].sort()) {
|
|
250
|
-
if (this.coordinator.owns(id))
|
|
251
|
-
continue;
|
|
252
|
-
if (!(await this.coordinator.acquire(id)))
|
|
253
|
-
throw locked();
|
|
254
|
-
temporary.push(id);
|
|
255
|
-
}
|
|
256
|
-
if (clearAll) {
|
|
257
|
-
const meta = { ...state.meta, generation: crypto.randomUUID() };
|
|
258
|
-
localStorage.setItem(historyMetaKey, JSON.stringify(meta));
|
|
259
|
-
this.generation = meta.generation;
|
|
260
|
-
this.baselines.clear();
|
|
261
|
-
modified = true;
|
|
262
|
-
}
|
|
263
|
-
for (const id of targets) {
|
|
264
|
-
this.commit(undefined, id);
|
|
265
|
-
modified = true;
|
|
266
|
-
state.entries = state.entries.filter((entry) => entry.session.id !== id);
|
|
267
|
-
}
|
|
268
|
-
for (const id of targets) {
|
|
269
|
-
this.seen.delete(id);
|
|
270
|
-
this.baselines.delete(id);
|
|
271
|
-
this.fresh.delete(id);
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
finally {
|
|
276
|
-
if (modified)
|
|
277
|
-
this.coordinator.notify();
|
|
278
|
-
for (const id of temporary)
|
|
279
|
-
this.coordinator.release(id);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// Generated from shared/text-only-reply.ts. Do not edit manually.
|
|
2
|
-
const chineseTextOnly = /^(?:请[ \t]*)?(?:(?:只用|仅用)(?:纯)?|(?:用|以)纯)(?:文字|文本)(?:[\s,。;:,:]|(?:简要|简单|详细|直接)?(?:回答|回复|介绍|解释|说明|描述|总结|列出)|$)/u;
|
|
3
|
-
const englishTextOnly = /^(?:please\s+)?(?:(?:answer|reply|respond)\s+(?:using|in|with)\s+|use\s+)(?:plain\s+text(?:\s+only)?|text\s+only)(?=\s*(?:[.,:;!]|$)|\s+(?:to|and|about)\b)/i;
|
|
4
|
-
export function requestsTextOnlyReply(content) {
|
|
5
|
-
const text = typeof content === "string"
|
|
6
|
-
? content
|
|
7
|
-
: content?.find((part) => part.type === "text" && part.text?.trim())?.text;
|
|
8
|
-
const opening = text?.trimStart() ?? "";
|
|
9
|
-
return chineseTextOnly.test(opening) || englishTextOnly.test(opening);
|
|
10
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { ToolRuntime } from "@nexface/tools";
|
|
2
|
-
export interface ToolRenderContext<TInput = unknown, TResult = unknown> {
|
|
3
|
-
input: TInput;
|
|
4
|
-
status: "running" | "success" | "error" | "cancelled";
|
|
5
|
-
result?: TResult;
|
|
6
|
-
}
|
|
7
|
-
export interface ToolPresentation {
|
|
8
|
-
definition?: ReturnType<ToolRuntime["getTools"]>[number]["definition"];
|
|
9
|
-
confirmationProtocol?: true;
|
|
10
|
-
render(context: ToolRenderContext): unknown;
|
|
11
|
-
}
|
|
12
|
-
export interface ToolPresentationController {
|
|
13
|
-
get(name: string): ToolPresentation | undefined;
|
|
14
|
-
names(): readonly string[];
|
|
15
|
-
subscribe(listener: () => void): () => void;
|
|
16
|
-
}
|
|
17
|
-
export declare const toolPresentationSymbol: unique symbol;
|
|
18
|
-
export declare function getToolPresentationController(runtime: ToolRuntime): ToolPresentationController | undefined;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
2
|
-
export type MarkdownDocument = ReturnType<typeof fromMarkdown>;
|
|
3
|
-
export declare const toolResultReferenceLanguages: string[];
|
|
4
|
-
export declare function toolResultBlock(node: MarkdownDocument["children"][number], source: string): {
|
|
5
|
-
start: number;
|
|
6
|
-
end: number;
|
|
7
|
-
id: string | undefined;
|
|
8
|
-
} | undefined;
|
|
9
|
-
export declare function toolResultBlocks(text: string): {
|
|
10
|
-
start: number;
|
|
11
|
-
end: number;
|
|
12
|
-
id: string | undefined;
|
|
13
|
-
}[];
|
|
14
|
-
export declare function filterToolResultBlocks(text: string, keep?: (toolCallId: string) => boolean): string;
|
|
15
|
-
export declare function stripToolResultBlocks(text: string): string;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// Generated from shared/tool-result-markdown.ts. Do not edit manually.
|
|
2
|
-
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
3
|
-
import { isToolResultReferenceId, toolResultReferenceLanguage } from "./tool-result-reference.js";
|
|
4
|
-
export const toolResultReferenceLanguages = [toolResultReferenceLanguage, "nexface-result"];
|
|
5
|
-
export function toolResultBlock(node, source) {
|
|
6
|
-
if (node.type !== "code")
|
|
7
|
-
return;
|
|
8
|
-
const start = node.position?.start.offset;
|
|
9
|
-
const end = node.position?.end.offset;
|
|
10
|
-
if (start === undefined || end === undefined)
|
|
11
|
-
return;
|
|
12
|
-
const [opening, ...lines] = source.slice(start, end).split(/\r\n?|\n/);
|
|
13
|
-
const fence = opening?.match(/^(`{3,}|~{3,})/)?.[1];
|
|
14
|
-
const language = node.lang;
|
|
15
|
-
if (!fence || !language)
|
|
16
|
-
return;
|
|
17
|
-
if (!toolResultReferenceLanguages.includes(language)) {
|
|
18
|
-
// A streaming language token may be split across chunks.
|
|
19
|
-
if (!lines.length &&
|
|
20
|
-
end === source.length &&
|
|
21
|
-
toolResultReferenceLanguages.some((referenceLanguage) => referenceLanguage.startsWith(language)))
|
|
22
|
-
return { start, end, id: undefined };
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const id = node.value.trim();
|
|
26
|
-
const closing = lines.at(-1)?.match(/^ {0,3}(`{3,}|~{3,})[ \t]*$/)?.[1];
|
|
27
|
-
const closed = closing && closing[0] === fence[0] && closing.length >= fence.length;
|
|
28
|
-
return { start, end, id: !node.meta && closed && isToolResultReferenceId(id) ? id : undefined };
|
|
29
|
-
}
|
|
30
|
-
export function toolResultBlocks(text) {
|
|
31
|
-
if (!text.includes("```") && !text.includes("~~~"))
|
|
32
|
-
return [];
|
|
33
|
-
return fromMarkdown(text).children.flatMap((node) => toolResultBlock(node, text) ?? []);
|
|
34
|
-
}
|
|
35
|
-
export function filterToolResultBlocks(text, keep) {
|
|
36
|
-
const blocks = toolResultBlocks(text);
|
|
37
|
-
if (!blocks.length)
|
|
38
|
-
return text;
|
|
39
|
-
let output = "";
|
|
40
|
-
let offset = 0;
|
|
41
|
-
const seen = new Set();
|
|
42
|
-
for (const { start, end, id } of blocks) {
|
|
43
|
-
if (id && keep?.(id) && !seen.has(id)) {
|
|
44
|
-
seen.add(id);
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
output += text.slice(offset, start);
|
|
48
|
-
offset = end;
|
|
49
|
-
}
|
|
50
|
-
output += text.slice(offset);
|
|
51
|
-
return output.trim() ? output : "";
|
|
52
|
-
}
|
|
53
|
-
export function stripToolResultBlocks(text) {
|
|
54
|
-
return filterToolResultBlocks(text);
|
|
55
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Component, memo, useMemo } from "react";
|
|
3
|
-
import { validateAgentToolInput } from "./shared/schema.js";
|
|
4
|
-
import { decodeToolResult } from "./tool-result.js";
|
|
5
|
-
import { useChat, useToolPresentations } from "./react.js";
|
|
6
|
-
import { cn } from "./ui/utils.js";
|
|
7
|
-
function confirmationArgumentsText(argsText, toolName, tool, messages) {
|
|
8
|
-
try {
|
|
9
|
-
const parsed = JSON.parse(argsText);
|
|
10
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
11
|
-
return;
|
|
12
|
-
const wire = parsed;
|
|
13
|
-
if (Object.keys(wire).length !== 1)
|
|
14
|
-
return;
|
|
15
|
-
if (Object.hasOwn(wire, "input"))
|
|
16
|
-
return argsText;
|
|
17
|
-
const answer = wire.confirmation;
|
|
18
|
-
if (!answer || typeof answer.requestId !== "string" || typeof answer.approved !== "boolean")
|
|
19
|
-
return;
|
|
20
|
-
// A confirmation continuation uses the original call's business input.
|
|
21
|
-
for (const message of messages)
|
|
22
|
-
for (const part of message.parts) {
|
|
23
|
-
if (part === tool)
|
|
24
|
-
return;
|
|
25
|
-
if (part.type !== "tool" || part.name !== toolName || part.result === undefined)
|
|
26
|
-
continue;
|
|
27
|
-
const { value, omitted } = decodeToolResult(part.result, toolName);
|
|
28
|
-
if (!omitted &&
|
|
29
|
-
value &&
|
|
30
|
-
typeof value === "object" &&
|
|
31
|
-
"status" in value &&
|
|
32
|
-
value.status === "confirmation_required" &&
|
|
33
|
-
"requestId" in value &&
|
|
34
|
-
value.requestId === answer.requestId) {
|
|
35
|
-
return part.arguments;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
/* Invalid or incomplete model arguments use the default execution UI. */
|
|
41
|
-
}
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
class RenderBoundary extends Component {
|
|
45
|
-
state = { failed: false };
|
|
46
|
-
static getDerivedStateFromError() {
|
|
47
|
-
return { failed: true };
|
|
48
|
-
}
|
|
49
|
-
componentDidCatch(error) {
|
|
50
|
-
this.props.report(error);
|
|
51
|
-
}
|
|
52
|
-
render() {
|
|
53
|
-
return this.state.failed ? this.props.fallback : this.props.children;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
const ToolContent = memo(function ToolContent({ presentation, context, }) {
|
|
57
|
-
return _jsx(_Fragment, { children: presentation.render(context) });
|
|
58
|
-
});
|
|
59
|
-
export function ToolPresentationUI({ tool }) {
|
|
60
|
-
const registry = useToolPresentations();
|
|
61
|
-
const { activeSessionId, messages } = useChat();
|
|
62
|
-
const presentation = registry?.controller?.get(tool.name);
|
|
63
|
-
const argsText = presentation?.confirmationProtocol
|
|
64
|
-
? confirmationArgumentsText(tool.arguments, tool.name, tool, messages)
|
|
65
|
-
: tool.arguments;
|
|
66
|
-
const { result, isError, toolState, name } = tool;
|
|
67
|
-
const hasRenderer = presentation !== undefined;
|
|
68
|
-
const renderedResult = useMemo(() => {
|
|
69
|
-
if (!hasRenderer || result === undefined || isError || toolState)
|
|
70
|
-
return;
|
|
71
|
-
const { value, omitted } = decodeToolResult(result, name);
|
|
72
|
-
// A completed snapshot can add a toolName envelope to the streamed receipt.
|
|
73
|
-
// Keep the renderer's props stable when the business result has not changed.
|
|
74
|
-
if (!omitted)
|
|
75
|
-
return JSON.stringify(value);
|
|
76
|
-
}, [hasRenderer, result, name, isError, toolState]);
|
|
77
|
-
const inputSchema = presentation?.definition?.inputSchema;
|
|
78
|
-
const confirmation = presentation?.confirmationProtocol;
|
|
79
|
-
const context = useMemo(() => {
|
|
80
|
-
if (renderedResult === undefined || argsText === undefined)
|
|
81
|
-
return;
|
|
82
|
-
const value = JSON.parse(renderedResult);
|
|
83
|
-
if (confirmation &&
|
|
84
|
-
value &&
|
|
85
|
-
typeof value === "object" &&
|
|
86
|
-
"status" in value &&
|
|
87
|
-
((value.status === "confirmation_required" &&
|
|
88
|
-
"requestId" in value &&
|
|
89
|
-
"message" in value &&
|
|
90
|
-
"nextAction" in value &&
|
|
91
|
-
Object.keys(value).length === 4) ||
|
|
92
|
-
(value.status === "rejected" &&
|
|
93
|
-
"toolExecuted" in value &&
|
|
94
|
-
value.toolExecuted === false &&
|
|
95
|
-
Object.keys(value).every((key) => ["status", "requestId", "toolExecuted"].includes(key)))))
|
|
96
|
-
return;
|
|
97
|
-
try {
|
|
98
|
-
const wire = JSON.parse(argsText);
|
|
99
|
-
if (!wire || typeof wire !== "object" || Array.isArray(wire))
|
|
100
|
-
return;
|
|
101
|
-
const input = confirmation ? wire.input : wire;
|
|
102
|
-
if (!inputSchema || !validateAgentToolInput(inputSchema, input).length)
|
|
103
|
-
return { input, status: "success", result: value };
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
// Broken or missing historical arguments do not prevent displaying the reply.
|
|
107
|
-
}
|
|
108
|
-
return;
|
|
109
|
-
}, [inputSchema, confirmation, argsText, renderedResult]);
|
|
110
|
-
if (!presentation || !context)
|
|
111
|
-
return null;
|
|
112
|
-
return (_jsx(RenderBoundary, { fallback: null, report: (error) => registry?.report(error, activeSessionId, tool.toolCallId), children: _jsx("div", { className: cn("my-2 min-w-0 max-w-full empty:hidden"), "data-slot": "nexface-tool-content", children: _jsx(ToolContent, { presentation: presentation, context: context }) }) }, tool.toolCallId));
|
|
113
|
-
}
|