@nexface/chat 0.1.21-alpha.2 → 0.1.21-alpha.4
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 +0 -12
- package/dist/BUNDLED_LICENSES.txt +0 -50
- package/dist/assistant-components.d.ts +2 -1
- package/dist/assistant-components.js +9 -12
- package/dist/assistant-runtime.d.ts +4 -1
- package/dist/assistant-runtime.js +5 -1
- package/dist/client.js +27 -9
- package/dist/default-chat.js +1404 -8030
- package/dist/messages.d.ts +3 -0
- package/dist/protocol-messages.d.ts +3 -2
- package/dist/protocol-messages.js +144 -20
- package/dist/protocol-response.js +2 -2
- package/dist/react.js +1 -1
- package/dist/shared/text-only-reply.d.ts +6 -0
- package/dist/shared/text-only-reply.js +10 -0
- package/dist/shared/tool-result-markdown.d.ts +15 -0
- package/dist/shared/tool-result-markdown.js +55 -0
- package/dist/shared/tool-result-reference.d.ts +2 -0
- package/dist/shared/tool-result-reference.js +5 -0
- package/dist/styles.css +8 -0
- package/dist/tool-presentation.d.ts +5 -5
- package/dist/tool-presentation.js +57 -77
- package/dist/ui/home-demo/action-bar.js +13 -2
- package/dist/ui/markdown-text.d.ts +6 -1
- package/dist/ui/markdown-text.js +39 -2
- package/package.json +4 -3
package/dist/messages.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type Message, type AGUIEvent } from "@ag-ui/core";
|
|
2
|
-
import type { UIMessage } from "./messages.js";
|
|
2
|
+
import type { PresentedMessage, UIMessage } from "./messages.js";
|
|
3
3
|
import type { AgentMessage, ToolApprovalInterruptDescriptor } from "@nexface/agent";
|
|
4
4
|
export type ProtocolEvent = AGUIEvent;
|
|
5
5
|
export declare function updateMessages(messages: Message[], event: ProtocolEvent): Message[];
|
|
6
6
|
export declare function createMessageUpdater(): typeof updateMessages;
|
|
7
|
-
export declare function projectMessages(messages: readonly Message[]):
|
|
7
|
+
export declare function projectMessages(messages: readonly Message[], allowStreamingToolReply?: boolean): PresentedMessage[];
|
|
8
|
+
export declare function completeToolReply(messages: Message[]): Message[];
|
|
8
9
|
export declare function restoreMessages(messages: UIMessage[], history: readonly AgentMessage[]): Message[];
|
|
9
10
|
export declare function settleApproval(messages: Message[], approval: ToolApprovalInterruptDescriptor, code: "TOOL_CONFIRMATION_INVALIDATED" | "TOOL_CANCELLED", message: string, reason: string): Message[];
|
|
@@ -3,7 +3,39 @@ import { toolError, isNexfaceError } from "./errors.js";
|
|
|
3
3
|
import { EventType, mergeMetadata } from "@ag-ui/core";
|
|
4
4
|
import { getToolResultError } from "./tool-result.js";
|
|
5
5
|
import jsonPatch from "fast-json-patch";
|
|
6
|
+
import { stripToolResultBlocks, toolResultBlocks } from "./shared/tool-result-markdown.js";
|
|
7
|
+
import { requestsTextOnlyReply } from "./shared/text-only-reply.js";
|
|
6
8
|
const uuid = () => globalThis.crypto.randomUUID();
|
|
9
|
+
// Retired display controls remain readable, but never become visible tool calls.
|
|
10
|
+
function legacyDisplayText(name, args) {
|
|
11
|
+
if (name === "nxf_showToolResults")
|
|
12
|
+
return "";
|
|
13
|
+
if (name === "nxf_finish") {
|
|
14
|
+
try {
|
|
15
|
+
const value = JSON.parse(args);
|
|
16
|
+
if (Array.isArray(value?.toolCallIds))
|
|
17
|
+
return typeof value.output === "string" ? value.output : "";
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
/* Invalid historical arguments retain the default execution UI. */
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
function finishReplyText(message) {
|
|
26
|
+
const [call] = message.toolCalls ?? [];
|
|
27
|
+
if (message.toolCalls?.length !== 1 || call?.function.name !== "nxf_finish")
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
const value = JSON.parse(call.function.arguments);
|
|
31
|
+
if (value?.status === "completed" && typeof value.output === "string" && value.output.trim())
|
|
32
|
+
return value.output.trim();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// Invalid finish calls retain their execution record.
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
7
39
|
function withMetadata(value, metadata) {
|
|
8
40
|
const existing = value.metadata;
|
|
9
41
|
const merged = mergeMetadata(existing, metadata);
|
|
@@ -24,12 +56,24 @@ export function updateMessages(messages, event) {
|
|
|
24
56
|
delete merged.content;
|
|
25
57
|
if (merged.role === "assistant" &&
|
|
26
58
|
previous.role === "assistant" &&
|
|
27
|
-
message.role === "assistant"
|
|
28
|
-
message.toolCalls) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
59
|
+
message.role === "assistant") {
|
|
60
|
+
if (message.toolCalls) {
|
|
61
|
+
merged.toolCalls = message.toolCalls.map((call) => {
|
|
62
|
+
const old = previous.toolCalls?.find((item) => item.id === call.id);
|
|
63
|
+
return withMetadata({ ...old, ...call, metadata: old?.metadata }, call.metadata);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (previous.metadata?.nexface?.finalToolReply === true) {
|
|
67
|
+
// The server can replace its metadata without knowing about local presentation state.
|
|
68
|
+
const nexface = { ...merged.metadata?.nexface };
|
|
69
|
+
const previousText = finishReplyText(previous) ?? previous.content;
|
|
70
|
+
const nextText = finishReplyText(merged) ?? merged.content;
|
|
71
|
+
if (previousText === nextText)
|
|
72
|
+
nexface.finalToolReply = true;
|
|
73
|
+
else
|
|
74
|
+
delete nexface.finalToolReply;
|
|
75
|
+
merged.metadata = { ...merged.metadata, nexface };
|
|
76
|
+
}
|
|
33
77
|
}
|
|
34
78
|
return JSON.stringify(previous) === JSON.stringify(merged) ? previous : merged;
|
|
35
79
|
});
|
|
@@ -329,10 +373,15 @@ export function createMessageUpdater() {
|
|
|
329
373
|
};
|
|
330
374
|
}
|
|
331
375
|
const projectionCache = new WeakMap();
|
|
332
|
-
|
|
333
|
-
|
|
376
|
+
const streamingProjectionCache = new WeakMap();
|
|
377
|
+
export function projectMessages(messages, allowStreamingToolReply = false) {
|
|
378
|
+
const cache = allowStreamingToolReply ? streamingProjectionCache : projectionCache;
|
|
379
|
+
const cached = cache.get(messages);
|
|
334
380
|
if (cached)
|
|
335
381
|
return cached;
|
|
382
|
+
let streamingReplyIndex = allowStreamingToolReply ? messages.length - 1 : -1;
|
|
383
|
+
while (streamingReplyIndex >= 0 && messages[streamingReplyIndex]?.role === "reasoning")
|
|
384
|
+
streamingReplyIndex -= 1;
|
|
336
385
|
const ownedReasoning = new Map();
|
|
337
386
|
const assistantIds = new Set(messages.filter((message) => message.role === "assistant").map((message) => message.id));
|
|
338
387
|
for (const message of messages) {
|
|
@@ -343,10 +392,15 @@ export function projectMessages(messages) {
|
|
|
343
392
|
ownedReasoning.set(owner, group);
|
|
344
393
|
}
|
|
345
394
|
}
|
|
395
|
+
let textOnly = false;
|
|
346
396
|
const projected = messages.flatMap((message, index) => {
|
|
347
397
|
if (message.role !== "user" && message.role !== "assistant" && message.role !== "reasoning")
|
|
348
398
|
return [];
|
|
349
399
|
const metadata = message.metadata?.nexface;
|
|
400
|
+
const finish = message.role === "assistant" ? finishReplyText(message) : undefined;
|
|
401
|
+
const content = finish ?? message.content;
|
|
402
|
+
if (message.role === "user")
|
|
403
|
+
textOnly = requestsTextOnlyReply(message.content);
|
|
350
404
|
if (message.role === "reasoning" && assistantIds.has(metadata?.reasoningOwnerId))
|
|
351
405
|
return [];
|
|
352
406
|
const parts = [];
|
|
@@ -354,16 +408,20 @@ export function projectMessages(messages) {
|
|
|
354
408
|
? metadata.stoppedText
|
|
355
409
|
: typeof metadata?.reasoningText === "string"
|
|
356
410
|
? metadata.reasoningText
|
|
357
|
-
: typeof
|
|
358
|
-
?
|
|
359
|
-
: (
|
|
411
|
+
: typeof content === "string"
|
|
412
|
+
? content
|
|
413
|
+
: (content ?? [])
|
|
360
414
|
.filter((part) => part.type === "text")
|
|
361
415
|
.map((part) => part.text)
|
|
362
416
|
.join("");
|
|
363
417
|
const reasoning = ownedReasoning.get(message.id) ?? [];
|
|
418
|
+
const referenceBlocks = reasoning.length ? toolResultBlocks(text) : [];
|
|
364
419
|
let offset = 0;
|
|
365
420
|
for (const entry of reasoning) {
|
|
366
|
-
const
|
|
421
|
+
const textOffset = Math.min(text.length, Number(entry.metadata?.nexface?.textOffset) || 0);
|
|
422
|
+
// Keep the fence and its payload together when reasoning interrupts a reference.
|
|
423
|
+
const block = referenceBlocks.find(({ start, end }) => start < textOffset && textOffset < end);
|
|
424
|
+
const position = Math.max(offset, block?.start ?? textOffset);
|
|
367
425
|
if (position > offset)
|
|
368
426
|
parts.push({ type: "text", text: text.slice(offset, position) });
|
|
369
427
|
const content = entry.content;
|
|
@@ -380,15 +438,21 @@ export function projectMessages(messages) {
|
|
|
380
438
|
return [];
|
|
381
439
|
if (typeof metadata?.reasoningText === "string" &&
|
|
382
440
|
typeof metadata?.stoppedText !== "string" &&
|
|
383
|
-
typeof
|
|
384
|
-
|
|
385
|
-
parts.push({ type: "text", text:
|
|
441
|
+
typeof content === "string" &&
|
|
442
|
+
content !== text) {
|
|
443
|
+
parts.push({ type: "text", text: content });
|
|
386
444
|
}
|
|
387
|
-
if (message.role === "assistant" && message.toolCalls?.length) {
|
|
445
|
+
if (message.role === "assistant" && message.toolCalls?.length && finish === undefined) {
|
|
388
446
|
const following = messages.slice(index + 1);
|
|
389
447
|
const nextBatch = following.findIndex((item) => item.role === "user" || (item.role === "assistant" && Boolean(item.toolCalls?.length)));
|
|
390
448
|
const batch = nextBatch < 0 ? following : following.slice(0, nextBatch);
|
|
391
449
|
for (const call of message.toolCalls) {
|
|
450
|
+
const legacyText = legacyDisplayText(call.function.name, call.function.arguments);
|
|
451
|
+
if (legacyText !== undefined) {
|
|
452
|
+
if (!text.trim() && legacyText)
|
|
453
|
+
parts.push({ type: "text", text: legacyText });
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
392
456
|
const result = batch.find((item) => item.role === "tool" && item.toolCallId === call.id);
|
|
393
457
|
let name = call.function.name, args = call.function.arguments;
|
|
394
458
|
if (name === "nxf_callAppTool")
|
|
@@ -430,9 +494,36 @@ export function projectMessages(messages) {
|
|
|
430
494
|
? [{ id: `${message.id}-image-${i}`, name: "图片", content }]
|
|
431
495
|
: [])
|
|
432
496
|
: []);
|
|
433
|
-
|
|
434
|
-
|
|
497
|
+
const restoredParts = metadata?.restoredParts;
|
|
498
|
+
if (restoredParts) {
|
|
499
|
+
parts.splice(0, parts.length);
|
|
500
|
+
for (const part of restoredParts) {
|
|
501
|
+
const legacyText = part.type === "tool" ? legacyDisplayText(part.name, part.arguments) : undefined;
|
|
502
|
+
if (legacyText === undefined)
|
|
503
|
+
parts.push(part);
|
|
504
|
+
else if (legacyText &&
|
|
505
|
+
!restoredParts.some((item) => item.type === "text" && item.text.trim()))
|
|
506
|
+
parts.push({ type: "text", text: legacyText });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
435
509
|
const toolParts = parts.filter((part) => part.type === "tool");
|
|
510
|
+
const finalToolReply = message.role === "assistant" &&
|
|
511
|
+
!textOnly &&
|
|
512
|
+
!toolParts.length &&
|
|
513
|
+
metadata?.state !== "cancelled" &&
|
|
514
|
+
metadata?.state !== "error" &&
|
|
515
|
+
(metadata?.finalToolReply === true ||
|
|
516
|
+
// A closed reference signals a final answer while the stream is active. If a
|
|
517
|
+
// tool call follows, the stream projection withdraws it without affecting history.
|
|
518
|
+
(index === streamingReplyIndex &&
|
|
519
|
+
!message.toolCalls?.length &&
|
|
520
|
+
toolResultBlocks(text).some(({ id }) => id !== undefined)));
|
|
521
|
+
if (finalToolReply) {
|
|
522
|
+
// Parse the final Markdown as one document even if reasoning interrupted its stream.
|
|
523
|
+
const fullText = finish ?? parts.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("");
|
|
524
|
+
const reasoningParts = parts.filter((part) => part.type === "reasoning");
|
|
525
|
+
parts.splice(0, parts.length, ...reasoningParts, { type: "text", text: fullText });
|
|
526
|
+
}
|
|
436
527
|
const state = metadata?.state === "cancelled"
|
|
437
528
|
? "cancelled"
|
|
438
529
|
: metadata?.state === "error"
|
|
@@ -450,7 +541,15 @@ export function projectMessages(messages) {
|
|
|
450
541
|
{
|
|
451
542
|
id: message.id,
|
|
452
543
|
role: message.role === "reasoning" ? "assistant" : message.role,
|
|
453
|
-
parts
|
|
544
|
+
parts: textOnly && message.role === "assistant"
|
|
545
|
+
? parts.flatMap((part) => {
|
|
546
|
+
if (part.type !== "text")
|
|
547
|
+
return [part];
|
|
548
|
+
const text = stripToolResultBlocks(part.text);
|
|
549
|
+
return text ? [{ ...part, text }] : [];
|
|
550
|
+
})
|
|
551
|
+
: parts,
|
|
552
|
+
...(finalToolReply ? { finalToolReply: true } : {}),
|
|
454
553
|
...(attachments.length ? { attachments } : {}),
|
|
455
554
|
...(metadata?.revisionOf ? { revisionOf: metadata.revisionOf } : {}),
|
|
456
555
|
...(state ? { state } : {}),
|
|
@@ -458,9 +557,31 @@ export function projectMessages(messages) {
|
|
|
458
557
|
},
|
|
459
558
|
];
|
|
460
559
|
});
|
|
461
|
-
|
|
560
|
+
cache.set(messages, projected);
|
|
462
561
|
return projected;
|
|
463
562
|
}
|
|
563
|
+
export function completeToolReply(messages) {
|
|
564
|
+
let index = messages.length - 1;
|
|
565
|
+
while (index >= 0 && messages[index]?.role !== "user" && messages[index]?.role !== "assistant")
|
|
566
|
+
index -= 1;
|
|
567
|
+
const reply = messages[index];
|
|
568
|
+
if (reply?.role !== "assistant" ||
|
|
569
|
+
reply.metadata?.nexface?.state === "error" ||
|
|
570
|
+
reply.metadata?.nexface?.state === "cancelled")
|
|
571
|
+
return messages;
|
|
572
|
+
const finish = finishReplyText(reply);
|
|
573
|
+
const content = finish ?? reply.content;
|
|
574
|
+
if ((reply.toolCalls?.length && finish === undefined) || typeof content !== "string")
|
|
575
|
+
return messages;
|
|
576
|
+
const hasReferences = toolResultBlocks(content).length > 0;
|
|
577
|
+
if (!hasReferences && finish === undefined)
|
|
578
|
+
return messages;
|
|
579
|
+
return messages.map((message, i) => i === index
|
|
580
|
+
? withMetadata(reply, {
|
|
581
|
+
nexface: { state: "success", ...(hasReferences ? { finalToolReply: true } : {}) },
|
|
582
|
+
})
|
|
583
|
+
: message);
|
|
584
|
+
}
|
|
464
585
|
export function restoreMessages(messages, history) {
|
|
465
586
|
const conversation = new Map(history.map((message) => [message.id, message]));
|
|
466
587
|
return messages.map((message) => {
|
|
@@ -475,6 +596,9 @@ export function restoreMessages(messages, history) {
|
|
|
475
596
|
attachments: message.attachments ?? [],
|
|
476
597
|
revisionOf: message.revisionOf,
|
|
477
598
|
state: message.state,
|
|
599
|
+
...(message.finalToolReply === true
|
|
600
|
+
? { finalToolReply: true }
|
|
601
|
+
: {}),
|
|
478
602
|
error: message.error,
|
|
479
603
|
displayOnly: message.role === "assistant" && message.parts.length === 0,
|
|
480
604
|
},
|
|
@@ -71,7 +71,7 @@ export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
|
71
71
|
const input = args;
|
|
72
72
|
const proxy = call.name === "nxf_callAppTool" ? decodeToolProxy(input) : undefined;
|
|
73
73
|
const name = proxy ? proxy.name : call.name;
|
|
74
|
-
const
|
|
74
|
+
const wireInput = call.name === "nxf_callAppTool"
|
|
75
75
|
? proxy.arguments
|
|
76
76
|
: actionIsNative(call.name)
|
|
77
77
|
? Object.fromEntries(Object.entries(input).filter(([key]) => key !== "progressLabel"))
|
|
@@ -82,7 +82,7 @@ export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
|
82
82
|
if (!action ||
|
|
83
83
|
action.toolCallId !== call.id ||
|
|
84
84
|
name !== action.definition?.name ||
|
|
85
|
-
JSON.stringify(
|
|
85
|
+
JSON.stringify(wireInput) !== JSON.stringify(action.input)) {
|
|
86
86
|
throw new Error("Browser action does not match its original call.");
|
|
87
87
|
}
|
|
88
88
|
if (action.pageDecision !== undefined &&
|
package/dist/react.js
CHANGED
|
@@ -726,7 +726,7 @@ export function NexfaceProvider(props) {
|
|
|
726
726
|
})
|
|
727
727
|
.reverse(),
|
|
728
728
|
activeSessionId: session.id,
|
|
729
|
-
messages: projectMessages(client.getMessages()),
|
|
729
|
+
messages: projectMessages(client.getMessages(), client.getStatus() === "running" && !controller.cancelling),
|
|
730
730
|
status: store.switching
|
|
731
731
|
? "switching-model"
|
|
732
732
|
: store.admitting
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
}
|
package/dist/styles.css
CHANGED
|
@@ -482,6 +482,10 @@
|
|
|
482
482
|
position: relative;
|
|
483
483
|
}
|
|
484
484
|
|
|
485
|
+
.nexface-ui .nxf-static {
|
|
486
|
+
position: static;
|
|
487
|
+
}
|
|
488
|
+
|
|
485
489
|
.nexface-ui .nxf-sticky {
|
|
486
490
|
position: sticky;
|
|
487
491
|
}
|
|
@@ -632,6 +636,10 @@
|
|
|
632
636
|
margin-block: var(--nxf-spacing);
|
|
633
637
|
}
|
|
634
638
|
|
|
639
|
+
.nexface-ui .nxf-my-2 {
|
|
640
|
+
margin-block: calc(var(--nxf-spacing) * 2);
|
|
641
|
+
}
|
|
642
|
+
|
|
635
643
|
.nexface-ui .nxf-my-3 {
|
|
636
644
|
margin-block: calc(var(--nxf-spacing) * 3);
|
|
637
645
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { ToolCallMessagePartProps } from "@assistant-ui/react";
|
|
2
1
|
import type { UIMessagePart } from "./messages.js";
|
|
3
|
-
export declare function ToolPresentationUI({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
2
|
+
export declare function ToolPresentationUI({ tool }: {
|
|
3
|
+
tool: Extract<UIMessagePart, {
|
|
4
|
+
type: "tool";
|
|
5
|
+
}>;
|
|
6
|
+
}): import("react").JSX.Element | null;
|
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import { Fragment as _Fragment, jsx as _jsx
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Component, memo, useMemo } from "react";
|
|
3
3
|
import { validateAgentToolInput } from "./shared/schema.js";
|
|
4
4
|
import { decodeToolResult } from "./tool-result.js";
|
|
5
5
|
import { useChat, useToolPresentations } from "./react.js";
|
|
6
|
-
import { ToolCall } from "./ui/home-demo/trace.js";
|
|
7
6
|
import { cn } from "./ui/utils.js";
|
|
8
|
-
function
|
|
7
|
+
function confirmationArgumentsText(argsText, toolName, tool, messages) {
|
|
9
8
|
try {
|
|
10
|
-
const
|
|
11
|
-
if (!
|
|
9
|
+
const parsed = JSON.parse(argsText);
|
|
10
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
12
11
|
return;
|
|
12
|
+
const wire = parsed;
|
|
13
13
|
if (Object.keys(wire).length !== 1)
|
|
14
14
|
return;
|
|
15
15
|
if (Object.hasOwn(wire, "input"))
|
|
16
|
-
return
|
|
16
|
+
return argsText;
|
|
17
17
|
const answer = wire.confirmation;
|
|
18
18
|
if (!answer || typeof answer.requestId !== "string" || typeof answer.approved !== "boolean")
|
|
19
19
|
return;
|
|
20
|
-
//
|
|
20
|
+
// A confirmation continuation uses the original call's business input.
|
|
21
21
|
for (const message of messages)
|
|
22
22
|
for (const part of message.parts) {
|
|
23
|
-
if (part
|
|
24
|
-
continue;
|
|
25
|
-
if (part.toolCallId === toolCallId)
|
|
23
|
+
if (part === tool)
|
|
26
24
|
return;
|
|
27
|
-
if (part.name !== toolName || part.result === undefined)
|
|
25
|
+
if (part.type !== "tool" || part.name !== toolName || part.result === undefined)
|
|
28
26
|
continue;
|
|
29
27
|
const { value, omitted } = decodeToolResult(part.result, toolName);
|
|
30
28
|
if (!omitted &&
|
|
@@ -34,14 +32,12 @@ function confirmationInputText(argsText, toolName, toolCallId, messages) {
|
|
|
34
32
|
value.status === "confirmation_required" &&
|
|
35
33
|
"requestId" in value &&
|
|
36
34
|
value.requestId === answer.requestId) {
|
|
37
|
-
|
|
38
|
-
if (original && Object.hasOwn(original, "input"))
|
|
39
|
-
return JSON.stringify(original.input);
|
|
35
|
+
return part.arguments;
|
|
40
36
|
}
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
catch {
|
|
44
|
-
/* Invalid or
|
|
40
|
+
/* Invalid or incomplete model arguments use the default execution UI. */
|
|
45
41
|
}
|
|
46
42
|
return;
|
|
47
43
|
}
|
|
@@ -60,73 +56,57 @@ class RenderBoundary extends Component {
|
|
|
60
56
|
const ToolContent = memo(function ToolContent({ presentation, context, }) {
|
|
61
57
|
return _jsx(_Fragment, { children: presentation.render(context) });
|
|
62
58
|
});
|
|
63
|
-
export function ToolPresentationUI({
|
|
59
|
+
export function ToolPresentationUI({ tool }) {
|
|
64
60
|
const registry = useToolPresentations();
|
|
65
|
-
const
|
|
66
|
-
const presentation = registry?.controller?.get(
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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 renderedResult = useMemo(() => {
|
|
68
|
+
if (result === undefined || isError || toolState)
|
|
69
|
+
return;
|
|
70
|
+
const { value, omitted } = decodeToolResult(result, name);
|
|
71
|
+
// A completed snapshot can add a toolName envelope to the streamed receipt.
|
|
72
|
+
// Keep the renderer's props stable when the business result has not changed.
|
|
73
|
+
if (!omitted)
|
|
74
|
+
return JSON.stringify(value);
|
|
75
|
+
}, [result, name, isError, toolState]);
|
|
76
|
+
const inputSchema = presentation?.definition.inputSchema;
|
|
77
|
+
const confirmation = presentation?.confirmationProtocol;
|
|
78
|
+
const context = useMemo(() => {
|
|
79
|
+
if (!inputSchema || renderedResult === undefined || argsText === undefined)
|
|
80
|
+
return;
|
|
81
|
+
const value = JSON.parse(renderedResult);
|
|
82
|
+
if (confirmation &&
|
|
83
|
+
value &&
|
|
84
|
+
typeof value === "object" &&
|
|
85
|
+
"status" in value &&
|
|
86
|
+
((value.status === "confirmation_required" &&
|
|
87
|
+
"requestId" in value &&
|
|
88
|
+
"message" in value &&
|
|
89
|
+
"nextAction" in value &&
|
|
90
|
+
Object.keys(value).length === 4) ||
|
|
91
|
+
(value.status === "rejected" &&
|
|
92
|
+
"toolExecuted" in value &&
|
|
93
|
+
value.toolExecuted === false &&
|
|
94
|
+
Object.keys(value).every((key) => ["status", "requestId", "toolExecuted"].includes(key)))))
|
|
79
95
|
return;
|
|
80
96
|
try {
|
|
81
|
-
const
|
|
82
|
-
if (!
|
|
83
|
-
return
|
|
97
|
+
const wire = JSON.parse(argsText);
|
|
98
|
+
if (!wire || typeof wire !== "object" || Array.isArray(wire))
|
|
99
|
+
return;
|
|
100
|
+
const input = confirmation ? wire.input : wire;
|
|
101
|
+
if (!validateAgentToolInput(inputSchema, input).length)
|
|
102
|
+
return { input, status: "success", result: value };
|
|
84
103
|
}
|
|
85
104
|
catch {
|
|
86
|
-
|
|
105
|
+
// Broken or missing historical arguments do not prevent displaying the reply.
|
|
87
106
|
}
|
|
88
107
|
return;
|
|
89
|
-
}, [
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
: undefined;
|
|
94
|
-
const confirmationPending = confirmation?.status === "confirmation_required" &&
|
|
95
|
-
typeof confirmation.requestId === "string" &&
|
|
96
|
-
typeof confirmation.message === "string" &&
|
|
97
|
-
typeof confirmation.nextAction === "string" &&
|
|
98
|
-
Object.keys(confirmation).length === 4;
|
|
99
|
-
const confirmationRejected = confirmation?.status === "rejected" &&
|
|
100
|
-
confirmation.toolExecuted === false &&
|
|
101
|
-
Object.keys(confirmation).every((key) => ["status", "requestId", "toolExecuted"].includes(key));
|
|
102
|
-
const state = tool?.type === "tool" ? tool.toolState : undefined;
|
|
103
|
-
const status = confirmationRejected ||
|
|
104
|
-
state === "cancelled" ||
|
|
105
|
-
state === "rejected" ||
|
|
106
|
-
(props.result === undefined &&
|
|
107
|
-
props.status.type === "incomplete" &&
|
|
108
|
-
props.status.reason === "cancelled")
|
|
109
|
-
? "cancelled"
|
|
110
|
-
: state || props.isError || (props.result === undefined && props.status.type === "incomplete")
|
|
111
|
-
? "error"
|
|
112
|
-
: props.result === undefined
|
|
113
|
-
? "running"
|
|
114
|
-
: "success";
|
|
115
|
-
const context = useMemo(() => parsed
|
|
116
|
-
? {
|
|
117
|
-
input: parsed.input,
|
|
118
|
-
status,
|
|
119
|
-
...(status === "success" ? { result: decoded ? decoded.value : props.result } : {}),
|
|
120
|
-
}
|
|
121
|
-
: undefined, [parsed, status, decoded, props.result]);
|
|
122
|
-
const fallback = _jsx(ToolCall, { ...props, presentation: tool });
|
|
123
|
-
if (!presentation ||
|
|
124
|
-
!context ||
|
|
125
|
-
confirmationPending ||
|
|
126
|
-
decoded?.omitted ||
|
|
127
|
-
(chat.interrupt?.toolName === props.toolName &&
|
|
128
|
-
tool?.type === "tool" &&
|
|
129
|
-
tool.result === undefined))
|
|
130
|
-
return fallback;
|
|
131
|
-
return (_jsxs(RenderBoundary, { fallback: fallback, report: (error) => registry?.report(error, chat.activeSessionId, props.toolCallId), children: [_jsx("div", { className: cn("my-2 min-w-0 max-w-full"), "data-slot": "nexface-tool-content", children: _jsx(ToolContent, { presentation: presentation, context: context }) }), props.showDetails ? fallback : null] }, props.toolCallId));
|
|
108
|
+
}, [inputSchema, confirmation, argsText, renderedResult]);
|
|
109
|
+
if (!presentation || !context)
|
|
110
|
+
return null;
|
|
111
|
+
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));
|
|
132
112
|
}
|
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
// Adapted from the pinned official homepage Demo; see vendor/assistant-ui/home-demo/ADAPTATIONS.md.
|
|
3
3
|
import { cn } from "../utils.js";
|
|
4
|
-
import { ActionBarPrimitive, AuiIf } from "@assistant-ui/react";
|
|
4
|
+
import { ActionBarPrimitive, AuiIf, useAui } from "@assistant-ui/react";
|
|
5
5
|
import { CheckIcon, CopyIcon } from "lucide-react";
|
|
6
|
+
import { stripToolResultBlocks } from "../../shared/tool-result-markdown.js";
|
|
7
|
+
import { useCopyToClipboard } from "../use-copy-to-clipboard.js";
|
|
6
8
|
import { actionButtonClass } from "./styles.js";
|
|
7
9
|
export function AssistantActionBar() {
|
|
8
|
-
|
|
10
|
+
const aui = useAui();
|
|
11
|
+
const { isCopied, copyToClipboard } = useCopyToClipboard();
|
|
12
|
+
return (_jsx(ActionBarPrimitive.Root, { autohide: "never", className: cn("flex items-center gap-1.5"), children: _jsxs(ActionBarPrimitive.Copy, { "aria-label": "\u590D\u5236\u56DE\u590D", className: cn(actionButtonClass), onClick: (event) => {
|
|
13
|
+
const raw = aui.message.getCopyText();
|
|
14
|
+
const text = stripToolResultBlocks(raw);
|
|
15
|
+
if (text !== raw) {
|
|
16
|
+
event.preventDefault();
|
|
17
|
+
copyToClipboard(text);
|
|
18
|
+
}
|
|
19
|
+
}, children: [_jsx(AuiIf, { condition: (s) => isCopied || s.message.isCopied, children: _jsx(CheckIcon, { className: cn("size-4") }) }), _jsx(AuiIf, { condition: (s) => !isCopied && !s.message.isCopied, children: _jsx(CopyIcon, { className: cn("size-4") }) })] }) }));
|
|
9
20
|
}
|