@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.
Files changed (48) hide show
  1. package/dist/BUNDLED_DEPENDENCIES.json +12 -0
  2. package/dist/BUNDLED_LICENSES.txt +50 -0
  3. package/dist/assistant-components.d.ts +1 -2
  4. package/dist/assistant-components.js +6 -8
  5. package/dist/assistant-runtime.d.ts +1 -4
  6. package/dist/assistant-runtime.js +5 -19
  7. package/dist/client.js +12 -24
  8. package/dist/default-chat.d.ts +1 -1
  9. package/dist/default-chat.js +7864 -1553
  10. package/dist/messages.d.ts +0 -3
  11. package/dist/protocol-messages.d.ts +2 -3
  12. package/dist/protocol-messages.js +27 -145
  13. package/dist/protocol-response.js +2 -2
  14. package/dist/react.d.ts +1 -11
  15. package/dist/react.js +100 -515
  16. package/dist/storage.d.ts +6 -3
  17. package/dist/storage.js +44 -50
  18. package/dist/styles.css +676 -1503
  19. package/dist/tool-result.d.ts +0 -5
  20. package/dist/tool-result.js +0 -31
  21. package/dist/types.d.ts +1 -3
  22. package/dist/ui/home-demo/action-bar.js +2 -13
  23. package/dist/ui/home-demo/greeting.d.ts +1 -3
  24. package/dist/ui/home-demo/greeting.js +2 -2
  25. package/dist/ui/home-demo/suggestions.d.ts +4 -2
  26. package/dist/ui/home-demo/suggestions.js +4 -5
  27. package/dist/ui/home-demo/trace.js +3 -2
  28. package/dist/ui/markdown-text.d.ts +1 -6
  29. package/dist/ui/markdown-text.js +2 -39
  30. package/dist/ui/thread-list.aui.d.ts +1 -2
  31. package/dist/ui/thread-list.aui.js +2 -6
  32. package/package.json +3 -4
  33. package/dist/fonts/Inter-OFL.txt +0 -93
  34. package/dist/fonts/inter-medium.ttf +0 -0
  35. package/dist/session-coordinator.d.ts +0 -23
  36. package/dist/session-coordinator.js +0 -139
  37. package/dist/session-history.d.ts +0 -29
  38. package/dist/session-history.js +0 -282
  39. package/dist/shared/text-only-reply.d.ts +0 -6
  40. package/dist/shared/text-only-reply.js +0 -10
  41. package/dist/shared/tool-presentation.d.ts +0 -18
  42. package/dist/shared/tool-presentation.js +0 -5
  43. package/dist/shared/tool-result-markdown.d.ts +0 -15
  44. package/dist/shared/tool-result-markdown.js +0 -55
  45. package/dist/shared/tool-result-reference.d.ts +0 -2
  46. package/dist/shared/tool-result-reference.js +0 -5
  47. package/dist/tool-presentation.d.ts +0 -6
  48. package/dist/tool-presentation.js +0 -113
@@ -28,6 +28,3 @@ export interface UIMessage {
28
28
  state?: "running" | "success" | "error" | "cancelled";
29
29
  error?: NexfaceError;
30
30
  }
31
- export type PresentedMessage = UIMessage & {
32
- finalToolReply?: true;
33
- };
@@ -1,10 +1,9 @@
1
1
  import { type Message, type AGUIEvent } from "@ag-ui/core";
2
- import type { PresentedMessage, UIMessage } from "./messages.js";
2
+ import type { 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[], allowStreamingToolReply?: boolean): PresentedMessage[];
8
- export declare function completeToolReply(messages: Message[]): Message[];
7
+ export declare function projectMessages(messages: readonly Message[]): UIMessage[];
9
8
  export declare function restoreMessages(messages: UIMessage[], history: readonly AgentMessage[]): Message[];
10
9
  export declare function settleApproval(messages: Message[], approval: ToolApprovalInterruptDescriptor, code: "TOOL_CONFIRMATION_INVALIDATED" | "TOOL_CANCELLED", message: string, reason: string): Message[];
@@ -1,41 +1,10 @@
1
+ import { parseConfirmationResult } from "./tool-confirmation.js";
1
2
  import { decodeToolProxy } from "./shared/tool-proxy.js";
2
3
  import { toolError, isNexfaceError } from "./errors.js";
3
4
  import { EventType, mergeMetadata } from "@ag-ui/core";
4
5
  import { getToolResultError } from "./tool-result.js";
5
6
  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";
8
7
  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
- }
39
8
  function withMetadata(value, metadata) {
40
9
  const existing = value.metadata;
41
10
  const merged = mergeMetadata(existing, metadata);
@@ -56,24 +25,12 @@ export function updateMessages(messages, event) {
56
25
  delete merged.content;
57
26
  if (merged.role === "assistant" &&
58
27
  previous.role === "assistant" &&
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
- }
28
+ message.role === "assistant" &&
29
+ message.toolCalls) {
30
+ merged.toolCalls = message.toolCalls.map((call) => {
31
+ const old = previous.toolCalls?.find((item) => item.id === call.id);
32
+ return withMetadata({ ...old, ...call, metadata: old?.metadata }, call.metadata);
33
+ });
77
34
  }
78
35
  return JSON.stringify(previous) === JSON.stringify(merged) ? previous : merged;
79
36
  });
@@ -373,15 +330,10 @@ export function createMessageUpdater() {
373
330
  };
374
331
  }
375
332
  const projectionCache = new WeakMap();
376
- const streamingProjectionCache = new WeakMap();
377
- export function projectMessages(messages, allowStreamingToolReply = false) {
378
- const cache = allowStreamingToolReply ? streamingProjectionCache : projectionCache;
379
- const cached = cache.get(messages);
333
+ export function projectMessages(messages) {
334
+ const cached = projectionCache.get(messages);
380
335
  if (cached)
381
336
  return cached;
382
- let streamingReplyIndex = allowStreamingToolReply ? messages.length - 1 : -1;
383
- while (streamingReplyIndex >= 0 && messages[streamingReplyIndex]?.role === "reasoning")
384
- streamingReplyIndex -= 1;
385
337
  const ownedReasoning = new Map();
386
338
  const assistantIds = new Set(messages.filter((message) => message.role === "assistant").map((message) => message.id));
387
339
  for (const message of messages) {
@@ -392,15 +344,10 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
392
344
  ownedReasoning.set(owner, group);
393
345
  }
394
346
  }
395
- let textOnly = false;
396
347
  const projected = messages.flatMap((message, index) => {
397
348
  if (message.role !== "user" && message.role !== "assistant" && message.role !== "reasoning")
398
349
  return [];
399
350
  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);
404
351
  if (message.role === "reasoning" && assistantIds.has(metadata?.reasoningOwnerId))
405
352
  return [];
406
353
  const parts = [];
@@ -408,20 +355,16 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
408
355
  ? metadata.stoppedText
409
356
  : typeof metadata?.reasoningText === "string"
410
357
  ? metadata.reasoningText
411
- : typeof content === "string"
412
- ? content
413
- : (content ?? [])
358
+ : typeof message.content === "string"
359
+ ? message.content
360
+ : (message.content ?? [])
414
361
  .filter((part) => part.type === "text")
415
362
  .map((part) => part.text)
416
363
  .join("");
417
364
  const reasoning = ownedReasoning.get(message.id) ?? [];
418
- const referenceBlocks = reasoning.length ? toolResultBlocks(text) : [];
419
365
  let offset = 0;
420
366
  for (const entry of reasoning) {
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
+ const position = Math.max(offset, Math.min(text.length, Number(entry.metadata?.nexface?.textOffset) || 0));
425
368
  if (position > offset)
426
369
  parts.push({ type: "text", text: text.slice(offset, position) });
427
370
  const content = entry.content;
@@ -438,21 +381,15 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
438
381
  return [];
439
382
  if (typeof metadata?.reasoningText === "string" &&
440
383
  typeof metadata?.stoppedText !== "string" &&
441
- typeof content === "string" &&
442
- content !== text) {
443
- parts.push({ type: "text", text: content });
384
+ typeof message.content === "string" &&
385
+ message.content !== text) {
386
+ parts.push({ type: "text", text: message.content });
444
387
  }
445
- if (message.role === "assistant" && message.toolCalls?.length && finish === undefined) {
388
+ if (message.role === "assistant" && message.toolCalls?.length) {
446
389
  const following = messages.slice(index + 1);
447
390
  const nextBatch = following.findIndex((item) => item.role === "user" || (item.role === "assistant" && Boolean(item.toolCalls?.length)));
448
391
  const batch = nextBatch < 0 ? following : following.slice(0, nextBatch);
449
392
  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
- }
456
393
  const result = batch.find((item) => item.role === "tool" && item.toolCallId === call.id);
457
394
  let name = call.function.name, args = call.function.arguments;
458
395
  if (name === "nxf_callAppTool")
@@ -481,7 +418,12 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
481
418
  arguments: args,
482
419
  ...(result?.role === "tool"
483
420
  ? {
484
- result: result.content,
421
+ result: result.content.length > 4000 && !parseConfirmationResult(result.content)
422
+ ? JSON.stringify({
423
+ contentOmitted: true,
424
+ preview: result.content.slice(0, 4000),
425
+ })
426
+ : result.content,
485
427
  isError,
486
428
  }
487
429
  : {}),
@@ -494,36 +436,9 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
494
436
  ? [{ id: `${message.id}-image-${i}`, name: "图片", content }]
495
437
  : [])
496
438
  : []);
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
- }
439
+ if (metadata?.restoredParts)
440
+ parts.splice(0, parts.length, ...metadata.restoredParts);
509
441
  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
- }
527
442
  const state = metadata?.state === "cancelled"
528
443
  ? "cancelled"
529
444
  : metadata?.state === "error"
@@ -541,15 +456,7 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
541
456
  {
542
457
  id: message.id,
543
458
  role: message.role === "reasoning" ? "assistant" : message.role,
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 } : {}),
459
+ parts,
553
460
  ...(attachments.length ? { attachments } : {}),
554
461
  ...(metadata?.revisionOf ? { revisionOf: metadata.revisionOf } : {}),
555
462
  ...(state ? { state } : {}),
@@ -557,31 +464,9 @@ export function projectMessages(messages, allowStreamingToolReply = false) {
557
464
  },
558
465
  ];
559
466
  });
560
- cache.set(messages, projected);
467
+ projectionCache.set(messages, projected);
561
468
  return projected;
562
469
  }
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
- }
585
470
  export function restoreMessages(messages, history) {
586
471
  const conversation = new Map(history.map((message) => [message.id, message]));
587
472
  return messages.map((message) => {
@@ -596,9 +481,6 @@ export function restoreMessages(messages, history) {
596
481
  attachments: message.attachments ?? [],
597
482
  revisionOf: message.revisionOf,
598
483
  state: message.state,
599
- ...(message.finalToolReply === true
600
- ? { finalToolReply: true }
601
- : {}),
602
484
  error: message.error,
603
485
  displayOnly: message.role === "assistant" && message.parts.length === 0,
604
486
  },
@@ -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 wireInput = call.name === "nxf_callAppTool"
74
+ const toolInput = 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(wireInput) !== JSON.stringify(action.input)) {
85
+ JSON.stringify(toolInput) !== 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.d.ts CHANGED
@@ -8,7 +8,6 @@ import type { NexfaceAgentConfig } from "./config.js";
8
8
  import type { ChatRunResult } from "./types.js";
9
9
  import { type Attachment } from "./attachments.js";
10
10
  import type { UIMessage } from "./messages.js";
11
- import { type ToolPresentationController } from "./shared/tool-presentation.js";
12
11
  export type { UIMessage, UIMessagePart } from "./messages.js";
13
12
  export type ChatCommandErrorCode = "CHAT_MODEL_SWITCH_BLOCKED" | "CHAT_RUNTIME_CHANGED" | "CHAT_RUN_ACTIVE" | "CHAT_SESSION_CREATE_BLOCKED" | "CHAT_SESSION_SWITCH_BLOCKED" | "INVALID_INPUT" | "UNKNOWN_SESSION";
14
13
  export interface ChatError {
@@ -26,6 +25,7 @@ interface UseChatResult {
26
25
  readonly activeSessionId: string;
27
26
  readonly messages: readonly UIMessage[];
28
27
  readonly status: ChatClientStatus | "cancelling" | "switching-model";
28
+ readonly storageWarning?: string;
29
29
  readonly error?: ChatError;
30
30
  readonly model: AgentModelInfo;
31
31
  readonly interrupt?: ToolApprovalInterruptDescriptor;
@@ -60,18 +60,8 @@ export type NexfaceProviderProps = {
60
60
  }) => Promise<Response>;
61
61
  agent?: never;
62
62
  });
63
- export declare function isChatHistoryReady(chat: UseChatResult): boolean;
64
- export declare function isSessionOccupied(chat: UseChatResult, id: string): boolean;
65
- export declare function isSessionOperationPending(chat: UseChatResult): boolean;
66
- export declare function watchHistorySidebar(chat: UseChatResult): (() => void) | undefined;
67
- export declare function historyNotice(chat: UseChatResult): string | undefined;
68
- export declare function sessionCommandResult(chat: UseChatResult): Promise<void> | undefined;
69
63
  export declare function isChatRecoveryUnavailable(chat: UseChatResult): boolean;
70
64
  export declare function canEditChatSession(chat: UseChatResult, sessionId: string): boolean;
71
65
  export declare function assertChatSendable(chat: UseChatResult, sessionId: string): void;
72
- export declare function useToolPresentations(): {
73
- controller: ToolPresentationController | undefined;
74
- report(cause: unknown, sessionId: string, toolCallId: string): void;
75
- } | null;
76
66
  export declare function NexfaceProvider(props: NexfaceProviderProps): ReactElement;
77
67
  export declare function useChat(): UseChatResult;