@nexface/chat 0.1.13 → 0.1.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/dist/assistant-attachments.js +60 -17
- package/dist/assistant-components.d.ts +3 -3
- package/dist/assistant-components.js +77 -29
- package/dist/assistant-runtime.d.ts +1 -1
- package/dist/assistant-runtime.js +100 -43
- package/dist/attachments.js +36 -15
- package/dist/client.js +312 -73
- package/dist/config.js +4 -5
- package/dist/default-chat.d.ts +1 -1
- package/dist/default-chat.js +2102 -728
- package/dist/errors.js +55 -18
- package/dist/index.js +0 -1
- package/dist/messages.js +0 -1
- package/dist/page-confirmation.js +3 -4
- package/dist/protocol-messages.js +290 -66
- package/dist/protocol-response.js +38 -18
- package/dist/react.js +146 -50
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/errors.js +0 -1
- package/dist/shared/runtime-controller.d.ts +1 -0
- package/dist/shared/runtime-controller.js +0 -1
- package/dist/shared/runtime-execution.js +0 -1
- package/dist/shared/schema.js +45 -19
- package/dist/storage.js +130 -53
- package/dist/styles.css +99 -20
- package/dist/tool-result.js +7 -3
- package/dist/types.js +0 -1
- package/dist/ui/attachment.aui.d.ts +1 -1
- package/dist/ui/attachment.aui.js +16 -20
- package/dist/ui/avatar.js +0 -1
- package/dist/ui/button.js +0 -1
- package/dist/ui/code-block.js +2 -3
- package/dist/ui/collapsible.js +2 -3
- package/dist/ui/composer-voice.js +3 -2
- package/dist/ui/dialog.d.ts +1 -1
- package/dist/ui/dialog.js +9 -4
- package/dist/ui/dropdown-menu.js +2 -3
- package/dist/ui/home-demo/action-bar.js +0 -1
- package/dist/ui/home-demo/composer.js +7 -4
- package/dist/ui/home-demo/greeting.d.ts +1 -1
- package/dist/ui/home-demo/greeting.js +1 -2
- package/dist/ui/home-demo/messages.d.ts +1 -1
- package/dist/ui/home-demo/messages.js +4 -5
- package/dist/ui/home-demo/styles.js +0 -1
- package/dist/ui/home-demo/suggestions.d.ts +1 -1
- package/dist/ui/home-demo/suggestions.js +5 -3
- package/dist/ui/home-demo/surfaces.js +3 -2
- package/dist/ui/home-demo/thread.d.ts +1 -1
- package/dist/ui/home-demo/thread.js +1 -2
- package/dist/ui/home-demo/trace-line.d.ts +1 -1
- package/dist/ui/home-demo/trace-line.js +2 -3
- package/dist/ui/home-demo/trace.d.ts +1 -1
- package/dist/ui/home-demo/trace.js +28 -9
- package/dist/ui/home-demo/type.js +0 -1
- package/dist/ui/input.js +0 -1
- package/dist/ui/markdown-text.js +2 -3
- package/dist/ui/reasoning.aui.js +2 -3
- package/dist/ui/reasoning.d.ts +1 -1
- package/dist/ui/reasoning.js +6 -9
- package/dist/ui/surface.js +27 -8
- package/dist/ui/thread-list.aui.d.ts +2 -2
- package/dist/ui/thread-list.aui.js +18 -12
- package/dist/ui/tool-group.aui.js +2 -3
- package/dist/ui/tooltip-icon-button.js +1 -2
- package/dist/ui/tooltip.js +0 -1
- package/dist/ui/use-attachment-src.js +1 -2
- package/dist/ui/use-copy-to-clipboard.d.ts +1 -1
- package/dist/ui/use-copy-to-clipboard.js +1 -2
- package/dist/ui/utils.js +9 -3
- package/dist/use-next-message.js +7 -4
- package/dist/voice/types.js +0 -1
- package/dist/voice/use-voice.js +35 -13
- package/package.json +8 -4
- package/dist/assistant-attachments.js.map +0 -1
- package/dist/assistant-components.js.map +0 -1
- package/dist/assistant-runtime.js.map +0 -1
- package/dist/attachments.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/default-chat.js.map +0 -7
- package/dist/errors.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/messages.js.map +0 -1
- package/dist/page-confirmation.js.map +0 -1
- package/dist/protocol-messages.js.map +0 -1
- package/dist/protocol-response.js.map +0 -1
- package/dist/react.js.map +0 -1
- package/dist/shared/errors.js.map +0 -1
- package/dist/shared/runtime-controller.js.map +0 -1
- package/dist/shared/runtime-execution.js.map +0 -1
- package/dist/shared/schema.js.map +0 -1
- package/dist/storage.js.map +0 -1
- package/dist/tool-result.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/ui/attachment.aui.js.map +0 -1
- package/dist/ui/avatar.js.map +0 -1
- package/dist/ui/button.js.map +0 -1
- package/dist/ui/code-block.js.map +0 -1
- package/dist/ui/collapsible.js.map +0 -1
- package/dist/ui/composer-voice.js.map +0 -1
- package/dist/ui/dialog.js.map +0 -1
- package/dist/ui/dropdown-menu.js.map +0 -1
- package/dist/ui/home-demo/action-bar.js.map +0 -1
- package/dist/ui/home-demo/composer.js.map +0 -1
- package/dist/ui/home-demo/greeting.js.map +0 -1
- package/dist/ui/home-demo/messages.js.map +0 -1
- package/dist/ui/home-demo/styles.js.map +0 -1
- package/dist/ui/home-demo/suggestions.js.map +0 -1
- package/dist/ui/home-demo/surfaces.js.map +0 -1
- package/dist/ui/home-demo/thread.js.map +0 -1
- package/dist/ui/home-demo/trace-line.js.map +0 -1
- package/dist/ui/home-demo/trace.js.map +0 -1
- package/dist/ui/home-demo/type.js.map +0 -1
- package/dist/ui/input.js.map +0 -1
- package/dist/ui/markdown-text.js.map +0 -1
- package/dist/ui/reasoning.aui.js.map +0 -1
- package/dist/ui/reasoning.js.map +0 -1
- package/dist/ui/surface.js.map +0 -1
- package/dist/ui/thread-list.aui.js.map +0 -1
- package/dist/ui/tool-group.aui.js.map +0 -1
- package/dist/ui/tooltip-icon-button.js.map +0 -1
- package/dist/ui/tooltip.js.map +0 -1
- package/dist/ui/use-attachment-src.js.map +0 -1
- package/dist/ui/use-copy-to-clipboard.js.map +0 -1
- package/dist/ui/utils.js.map +0 -1
- package/dist/use-next-message.js.map +0 -1
- package/dist/voice/types.js.map +0 -1
- package/dist/voice/use-voice.js.map +0 -1
package/dist/errors.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
function errorCode(cause) {
|
|
2
|
-
return cause && typeof cause === "object" && "code" in cause && typeof cause.code === "string"
|
|
2
|
+
return cause && typeof cause === "object" && "code" in cause && typeof cause.code === "string"
|
|
3
|
+
? cause.code
|
|
4
|
+
: undefined;
|
|
3
5
|
}
|
|
4
6
|
export function runError(id, cause, unknownTools = false) {
|
|
5
7
|
const code = errorCode(cause) ?? "CHAT_RUN_ERROR";
|
|
6
|
-
const userMessage = unknownTools
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const userMessage = unknownTools
|
|
9
|
+
? "Some results are missing. Check whether the actions completed."
|
|
10
|
+
: code === "CHAT_REQUEST_FAILED"
|
|
11
|
+
? "Request failed. Please try again later."
|
|
12
|
+
: code === "CHAT_STREAM_INCOMPLETE"
|
|
13
|
+
? "The response ended before it was complete."
|
|
14
|
+
: code === "CHAT_RECOVERY_UNAVAILABLE"
|
|
15
|
+
? "This chat cannot continue. Check the action results, then start a new chat."
|
|
10
16
|
: "Something went wrong.";
|
|
11
17
|
return { id, scope: "run", code, userMessage };
|
|
12
18
|
}
|
|
@@ -15,7 +21,10 @@ export function toolError(id, toolCallId, content, parentErrorId, outcomeUnknown
|
|
|
15
21
|
try {
|
|
16
22
|
const payload = JSON.parse(content);
|
|
17
23
|
cause = payload?.error;
|
|
18
|
-
if (!cause &&
|
|
24
|
+
if (!cause &&
|
|
25
|
+
toolName &&
|
|
26
|
+
["nxf_executeAction", "nxf_applyAction", "nxf_getSnapshot"].includes(toolName) &&
|
|
27
|
+
payload?.result?.ok === false)
|
|
19
28
|
cause = payload.result.error;
|
|
20
29
|
}
|
|
21
30
|
catch {
|
|
@@ -23,31 +32,60 @@ export function toolError(id, toolCallId, content, parentErrorId, outcomeUnknown
|
|
|
23
32
|
}
|
|
24
33
|
if (!cause)
|
|
25
34
|
return {};
|
|
26
|
-
const code = outcomeUnknown ? "EXECUTION_OUTCOME_UNKNOWN" : errorCode(cause) ?? "TOOL_ERROR";
|
|
35
|
+
const code = outcomeUnknown ? "EXECUTION_OUTCOME_UNKNOWN" : (errorCode(cause) ?? "TOOL_ERROR");
|
|
27
36
|
if (code === "TOOL_CONFIRMATION_REJECTED")
|
|
28
37
|
return { toolState: "rejected" };
|
|
29
38
|
if (code === "TOOL_SKIPPED")
|
|
30
39
|
return { toolState: "not_started" };
|
|
31
40
|
if (code === "TOOL_CANCELLED")
|
|
32
41
|
return { toolState: "cancelled" };
|
|
33
|
-
const execution = code === "EXECUTION_OUTCOME_UNKNOWN"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
const execution = code === "EXECUTION_OUTCOME_UNKNOWN"
|
|
43
|
+
? "unknown"
|
|
44
|
+
: [
|
|
45
|
+
"INVALID_INPUT",
|
|
46
|
+
"UNKNOWN_TOOL",
|
|
47
|
+
"TOOL_UNAVAILABLE",
|
|
48
|
+
"TOOL_CONFIRMATION_UNAVAILABLE",
|
|
49
|
+
"TOOL_CONFIRMATION_INVALIDATED",
|
|
50
|
+
"TOOL_REGISTRATION_TIMEOUT",
|
|
51
|
+
].includes(code)
|
|
52
|
+
? "not_started"
|
|
53
|
+
: "failed";
|
|
54
|
+
return {
|
|
55
|
+
toolState: execution,
|
|
56
|
+
cause,
|
|
57
|
+
error: {
|
|
58
|
+
id,
|
|
59
|
+
scope: "tool",
|
|
60
|
+
code,
|
|
61
|
+
toolCallId,
|
|
62
|
+
execution,
|
|
63
|
+
userMessage: execution === "unknown"
|
|
64
|
+
? "No result was received. Check whether the action completed."
|
|
65
|
+
: execution === "not_started"
|
|
66
|
+
? "The action could not start."
|
|
67
|
+
: "The action failed.",
|
|
38
68
|
...(parentErrorId ? { parentErrorId } : {}),
|
|
39
|
-
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
40
71
|
}
|
|
41
72
|
export function isNexfaceError(value) {
|
|
42
73
|
if (!value || typeof value !== "object")
|
|
43
74
|
return false;
|
|
44
75
|
const error = value;
|
|
45
|
-
return typeof error.id === "string" &&
|
|
46
|
-
|
|
47
|
-
|
|
76
|
+
return (typeof error.id === "string" &&
|
|
77
|
+
typeof error.code === "string" &&
|
|
78
|
+
typeof error.userMessage === "string" &&
|
|
79
|
+
(error.parentErrorId === undefined || typeof error.parentErrorId === "string") &&
|
|
80
|
+
(error.scope === "run" ||
|
|
81
|
+
(error.scope === "tool" &&
|
|
82
|
+
typeof error.toolCallId === "string" &&
|
|
83
|
+
["not_started", "failed", "unknown"].includes(String(error.execution)))));
|
|
48
84
|
}
|
|
49
85
|
const presentedFailures = new WeakSet();
|
|
50
|
-
export function markErrorPresented(error) {
|
|
86
|
+
export function markErrorPresented(error) {
|
|
87
|
+
presentedFailures.add(error);
|
|
88
|
+
}
|
|
51
89
|
export function sendErrorNotice(error) {
|
|
52
90
|
if (error instanceof Error && presentedFailures.has(error))
|
|
53
91
|
return "";
|
|
@@ -58,4 +96,3 @@ export function sendErrorNotice(error) {
|
|
|
58
96
|
return "Check your message and send it again.";
|
|
59
97
|
return runError("send", error).userMessage;
|
|
60
98
|
}
|
|
61
|
-
//# sourceMappingURL=errors.js.map
|
package/dist/index.js
CHANGED
package/dist/messages.js
CHANGED
|
@@ -40,9 +40,9 @@ export function PageConfirmationProvider({ interrupt, onConfirm, onBindingRemove
|
|
|
40
40
|
if (resumingRef.current.has(binding))
|
|
41
41
|
return true;
|
|
42
42
|
const pending = interruptRef.current;
|
|
43
|
-
if (pending?.mode !== "page"
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (pending?.mode !== "page" ||
|
|
44
|
+
pending.toolName !== binding.toolName ||
|
|
45
|
+
bindingsRef.current.get(binding.toolName) !== binding) {
|
|
46
46
|
return false;
|
|
47
47
|
}
|
|
48
48
|
resumingRef.current.add(binding);
|
|
@@ -86,4 +86,3 @@ export function usePageConfirmation(binding, fallback) {
|
|
|
86
86
|
return fallbackRef.current(...args);
|
|
87
87
|
}, [binding, context]);
|
|
88
88
|
}
|
|
89
|
-
//# sourceMappingURL=page-confirmation.js.map
|
|
@@ -16,9 +16,15 @@ export function updateMessages(messages, event) {
|
|
|
16
16
|
if (!previous)
|
|
17
17
|
return message;
|
|
18
18
|
const merged = withMetadata({ ...previous, ...message, metadata: previous.metadata }, message.metadata);
|
|
19
|
-
if (previous.role === "assistant" &&
|
|
19
|
+
if (previous.role === "assistant" &&
|
|
20
|
+
message.role === "assistant" &&
|
|
21
|
+
previous.content === "" &&
|
|
22
|
+
message.content === undefined)
|
|
20
23
|
delete merged.content;
|
|
21
|
-
if (merged.role === "assistant" &&
|
|
24
|
+
if (merged.role === "assistant" &&
|
|
25
|
+
previous.role === "assistant" &&
|
|
26
|
+
message.role === "assistant" &&
|
|
27
|
+
message.toolCalls) {
|
|
22
28
|
merged.toolCalls = message.toolCalls.map((call) => {
|
|
23
29
|
const old = previous.toolCalls?.find((item) => item.id === call.id);
|
|
24
30
|
return withMetadata({ ...old, ...call, metadata: old?.metadata }, call.metadata);
|
|
@@ -31,64 +37,140 @@ export function updateMessages(messages, event) {
|
|
|
31
37
|
for (const [index, message] of messages.entries()) {
|
|
32
38
|
if (!message.metadata?.nexface?.displayOnly || next.some((item) => item.id === message.id))
|
|
33
39
|
continue;
|
|
34
|
-
const start = messages
|
|
35
|
-
|
|
40
|
+
const start = messages
|
|
41
|
+
.slice(0, index + 1)
|
|
42
|
+
.reduce((last, item, position) => (item.role === "user" ? position : last), 0);
|
|
43
|
+
const anchor = messages
|
|
44
|
+
.slice(start, index)
|
|
45
|
+
.reverse()
|
|
46
|
+
.find((item) => next.some((candidate) => candidate.id === item.id));
|
|
36
47
|
if (anchor)
|
|
37
48
|
next.splice(next.findIndex((item) => item.id === anchor.id) + 1, 0, message);
|
|
38
49
|
else {
|
|
39
50
|
// A failed local run may roll its entire turn out of the Agent history.
|
|
40
|
-
const following = messages
|
|
41
|
-
|
|
42
|
-
.
|
|
51
|
+
const following = messages
|
|
52
|
+
.slice(index + 1)
|
|
53
|
+
.find((item) => next.some((candidate) => candidate.id === item.id));
|
|
54
|
+
const retained = messages
|
|
55
|
+
.slice(start, index + 1)
|
|
56
|
+
.filter((item) => !next.some((candidate) => candidate.id === item.id))
|
|
57
|
+
.map((item) => ({
|
|
58
|
+
...item,
|
|
59
|
+
metadata: {
|
|
60
|
+
...item.metadata,
|
|
61
|
+
nexface: { ...item.metadata?.nexface, displayOnly: true },
|
|
62
|
+
},
|
|
63
|
+
}));
|
|
43
64
|
next.splice(following ? next.findIndex((item) => item.id === following.id) : 0, 0, ...retained);
|
|
44
65
|
}
|
|
45
66
|
}
|
|
46
|
-
return next.length === messages.length &&
|
|
67
|
+
return next.length === messages.length &&
|
|
68
|
+
next.every((message, index) => message === messages[index])
|
|
69
|
+
? messages
|
|
70
|
+
: next;
|
|
47
71
|
}
|
|
48
|
-
if (event.type === EventType.TEXT_MESSAGE_START ||
|
|
72
|
+
if (event.type === EventType.TEXT_MESSAGE_START ||
|
|
73
|
+
event.type === EventType.REASONING_MESSAGE_START) {
|
|
49
74
|
if (messages.some((message) => message.id === event.messageId)) {
|
|
50
75
|
return messages.map((message) => message.id === event.messageId ? withMetadata(message, event.metadata) : message);
|
|
51
76
|
}
|
|
52
|
-
return [
|
|
53
|
-
|
|
77
|
+
return [
|
|
78
|
+
...messages,
|
|
79
|
+
withMetadata({
|
|
80
|
+
id: event.messageId,
|
|
81
|
+
role: event.role,
|
|
82
|
+
content: "",
|
|
83
|
+
...(event.type === EventType.TEXT_MESSAGE_START && event.name !== undefined
|
|
84
|
+
? { name: event.name }
|
|
85
|
+
: {}),
|
|
54
86
|
metadata: { nexface: { state: "running" } },
|
|
55
|
-
}, event.metadata)
|
|
87
|
+
}, event.metadata),
|
|
88
|
+
];
|
|
56
89
|
}
|
|
57
|
-
if (event.type === EventType.TEXT_MESSAGE_CONTENT ||
|
|
58
|
-
|
|
90
|
+
if (event.type === EventType.TEXT_MESSAGE_CONTENT ||
|
|
91
|
+
event.type === EventType.TEXT_MESSAGE_END ||
|
|
92
|
+
event.type === EventType.REASONING_MESSAGE_CONTENT ||
|
|
93
|
+
event.type === EventType.REASONING_MESSAGE_END) {
|
|
59
94
|
return messages.map((message) => {
|
|
60
95
|
if (message.id !== event.messageId || message.role === "tool" || message.role === "activity")
|
|
61
96
|
return message;
|
|
62
97
|
const updated = withMetadata(message, event.metadata);
|
|
63
|
-
if (event.type === EventType.TEXT_MESSAGE_CONTENT ||
|
|
98
|
+
if (event.type === EventType.TEXT_MESSAGE_CONTENT ||
|
|
99
|
+
event.type === EventType.REASONING_MESSAGE_CONTENT) {
|
|
64
100
|
const content = Array.isArray(updated.content)
|
|
65
101
|
? [...updated.content, { type: "text", text: event.delta }]
|
|
66
102
|
: (updated.content ?? "") + event.delta;
|
|
67
103
|
return { ...updated, content };
|
|
68
104
|
}
|
|
69
|
-
return {
|
|
105
|
+
return {
|
|
106
|
+
...updated,
|
|
107
|
+
metadata: {
|
|
108
|
+
...updated.metadata,
|
|
109
|
+
nexface: {
|
|
110
|
+
...updated.metadata?.nexface,
|
|
111
|
+
state: event.metadata?.nexface?.state === "error" ? "error" : "success",
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
};
|
|
70
115
|
});
|
|
71
116
|
}
|
|
72
117
|
if (event.type === EventType.TOOL_CALL_START) {
|
|
73
118
|
const messageId = event.parentMessageId ?? event.toolCallId;
|
|
74
|
-
const base = messages.some((message) => message.id === messageId)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
119
|
+
const base = messages.some((message) => message.id === messageId)
|
|
120
|
+
? messages
|
|
121
|
+
: [...messages, { id: messageId, role: "assistant" }];
|
|
122
|
+
return base.map((message) => message.id !== messageId || message.role !== "assistant"
|
|
123
|
+
? message
|
|
124
|
+
: {
|
|
125
|
+
...message,
|
|
126
|
+
toolCalls: [
|
|
127
|
+
...(message.toolCalls ?? []),
|
|
128
|
+
withMetadata({
|
|
129
|
+
id: event.toolCallId,
|
|
130
|
+
type: "function",
|
|
131
|
+
function: { name: event.toolCallName, arguments: "" },
|
|
132
|
+
}, event.metadata),
|
|
133
|
+
],
|
|
134
|
+
});
|
|
78
135
|
}
|
|
79
136
|
if (event.type === EventType.TOOL_CALL_ARGS || event.type === EventType.TOOL_CALL_END) {
|
|
80
|
-
const owner = [...messages]
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
137
|
+
const owner = [...messages]
|
|
138
|
+
.reverse()
|
|
139
|
+
.find((message) => message.role === "assistant" &&
|
|
140
|
+
message.toolCalls?.some((call) => call.id === event.toolCallId));
|
|
141
|
+
return messages.map((message) => message !== owner || message.role !== "assistant"
|
|
142
|
+
? message
|
|
143
|
+
: {
|
|
144
|
+
...message,
|
|
145
|
+
toolCalls: message.toolCalls?.map((call) => call.id !== event.toolCallId
|
|
146
|
+
? call
|
|
147
|
+
: withMetadata({
|
|
148
|
+
...call,
|
|
149
|
+
...(event.type === EventType.TOOL_CALL_ARGS
|
|
150
|
+
? {
|
|
151
|
+
function: {
|
|
152
|
+
...call.function,
|
|
153
|
+
arguments: call.function.arguments + event.delta,
|
|
154
|
+
},
|
|
155
|
+
}
|
|
156
|
+
: {}),
|
|
157
|
+
}, event.metadata)),
|
|
158
|
+
});
|
|
86
159
|
}
|
|
87
160
|
if (event.type === EventType.TOOL_CALL_RESULT) {
|
|
88
161
|
const previous = messages.find((message) => message.id === event.messageId);
|
|
89
162
|
const error = getToolResultError(event.content);
|
|
90
|
-
const result = withMetadata({
|
|
91
|
-
|
|
163
|
+
const result = withMetadata({
|
|
164
|
+
id: event.messageId,
|
|
165
|
+
role: "tool",
|
|
166
|
+
toolCallId: event.toolCallId,
|
|
167
|
+
content: event.content,
|
|
168
|
+
...(error === undefined ? {} : { error }),
|
|
169
|
+
metadata: previous?.metadata,
|
|
170
|
+
}, event.metadata);
|
|
171
|
+
return messages.some((message) => message.id === result.id)
|
|
172
|
+
? messages.map((message) => message.id === result.id ? { ...message, ...result } : message)
|
|
173
|
+
: [...messages, result];
|
|
92
174
|
}
|
|
93
175
|
if (event.type === EventType.ACTIVITY_SNAPSHOT || event.type === EventType.ACTIVITY_DELTA) {
|
|
94
176
|
const previous = messages.find((message) => message.id === event.messageId);
|
|
@@ -97,12 +179,21 @@ export function updateMessages(messages, event) {
|
|
|
97
179
|
if (event.type === EventType.ACTIVITY_SNAPSHOT && previous && event.replace === false) {
|
|
98
180
|
return messages.map((message) => message === previous ? withMetadata(message, event.metadata) : message);
|
|
99
181
|
}
|
|
100
|
-
const content = event.type === EventType.ACTIVITY_SNAPSHOT
|
|
182
|
+
const content = event.type === EventType.ACTIVITY_SNAPSHOT
|
|
183
|
+
? event.content
|
|
101
184
|
: jsonPatch.applyPatch(previous?.content ?? {}, event.patch, true, false).newDocument;
|
|
102
185
|
if (!content || typeof content !== "object" || Array.isArray(content))
|
|
103
186
|
throw new Error("Activity content must be an object.");
|
|
104
|
-
const message = withMetadata({
|
|
105
|
-
|
|
187
|
+
const message = withMetadata({
|
|
188
|
+
...previous,
|
|
189
|
+
id: event.messageId,
|
|
190
|
+
role: "activity",
|
|
191
|
+
activityType: event.activityType,
|
|
192
|
+
content,
|
|
193
|
+
}, event.metadata);
|
|
194
|
+
return messages.some((item) => item.id === message.id)
|
|
195
|
+
? messages.map((item) => (item.id === message.id ? message : item))
|
|
196
|
+
: [...messages, message];
|
|
106
197
|
}
|
|
107
198
|
return messages;
|
|
108
199
|
}
|
|
@@ -117,17 +208,31 @@ export function createMessageUpdater() {
|
|
|
117
208
|
}
|
|
118
209
|
if (event.type === EventType.RUN_FINISHED || event.type === EventType.RUN_ERROR) {
|
|
119
210
|
if (textId)
|
|
120
|
-
messages = updateMessages(messages, {
|
|
211
|
+
messages = updateMessages(messages, {
|
|
212
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
213
|
+
messageId: textId,
|
|
214
|
+
});
|
|
121
215
|
if (reasoningId)
|
|
122
|
-
messages = updateMessages(messages, {
|
|
216
|
+
messages = updateMessages(messages, {
|
|
217
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
218
|
+
messageId: reasoningId,
|
|
219
|
+
});
|
|
123
220
|
if (toolId)
|
|
124
221
|
messages = updateMessages(messages, { type: EventType.TOOL_CALL_END, toolCallId: toolId });
|
|
125
222
|
if (event.type === EventType.RUN_ERROR)
|
|
126
223
|
messages = messages.map((message) => message.id === textId || message.id === reasoningId
|
|
127
|
-
? {
|
|
224
|
+
? {
|
|
225
|
+
...message,
|
|
226
|
+
metadata: {
|
|
227
|
+
...message.metadata,
|
|
228
|
+
nexface: { ...message.metadata?.nexface, state: "error" },
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
: message);
|
|
128
232
|
textId = reasoningId = toolId = undefined;
|
|
129
233
|
}
|
|
130
|
-
if (event.type === EventType.TEXT_MESSAGE_CHUNK ||
|
|
234
|
+
if (event.type === EventType.TEXT_MESSAGE_CHUNK ||
|
|
235
|
+
event.type === EventType.REASONING_MESSAGE_CHUNK) {
|
|
131
236
|
const reasoning = event.type === EventType.REASONING_MESSAGE_CHUNK;
|
|
132
237
|
const previousId = reasoning ? reasoningId : textId;
|
|
133
238
|
const messageId = event.messageId ?? previousId;
|
|
@@ -135,19 +240,33 @@ export function createMessageUpdater() {
|
|
|
135
240
|
throw new Error("The first message chunk requires messageId.");
|
|
136
241
|
if (previousId && previousId !== messageId)
|
|
137
242
|
messages = updateMessages(messages, {
|
|
138
|
-
type: reasoning ? EventType.REASONING_MESSAGE_END : EventType.TEXT_MESSAGE_END,
|
|
243
|
+
type: reasoning ? EventType.REASONING_MESSAGE_END : EventType.TEXT_MESSAGE_END,
|
|
244
|
+
messageId: previousId,
|
|
139
245
|
});
|
|
140
246
|
if (previousId !== messageId)
|
|
141
247
|
messages = updateMessages(messages, reasoning
|
|
142
|
-
? {
|
|
143
|
-
|
|
248
|
+
? {
|
|
249
|
+
type: EventType.REASONING_MESSAGE_START,
|
|
250
|
+
messageId,
|
|
251
|
+
role: "reasoning",
|
|
252
|
+
metadata: event.metadata,
|
|
253
|
+
}
|
|
254
|
+
: {
|
|
255
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
256
|
+
messageId,
|
|
257
|
+
role: event.role ?? "assistant",
|
|
258
|
+
name: event.name,
|
|
259
|
+
metadata: event.metadata,
|
|
260
|
+
});
|
|
144
261
|
if (reasoning)
|
|
145
262
|
reasoningId = messageId;
|
|
146
263
|
else
|
|
147
264
|
textId = messageId;
|
|
148
265
|
return updateMessages(messages, {
|
|
149
266
|
type: reasoning ? EventType.REASONING_MESSAGE_CONTENT : EventType.TEXT_MESSAGE_CONTENT,
|
|
150
|
-
messageId,
|
|
267
|
+
messageId,
|
|
268
|
+
delta: event.delta ?? "",
|
|
269
|
+
metadata: event.metadata,
|
|
151
270
|
});
|
|
152
271
|
}
|
|
153
272
|
if (event.type === EventType.TOOL_CALL_CHUNK) {
|
|
@@ -158,23 +277,40 @@ export function createMessageUpdater() {
|
|
|
158
277
|
if (!event.toolCallName)
|
|
159
278
|
throw new Error("The first tool chunk requires toolCallName.");
|
|
160
279
|
if (toolId)
|
|
161
|
-
messages = updateMessages(messages, {
|
|
280
|
+
messages = updateMessages(messages, {
|
|
281
|
+
type: EventType.TOOL_CALL_END,
|
|
282
|
+
toolCallId: toolId,
|
|
283
|
+
});
|
|
162
284
|
messages = updateMessages(messages, {
|
|
163
|
-
type: EventType.TOOL_CALL_START,
|
|
164
|
-
|
|
285
|
+
type: EventType.TOOL_CALL_START,
|
|
286
|
+
toolCallId,
|
|
287
|
+
toolCallName: event.toolCallName,
|
|
288
|
+
parentMessageId: event.parentMessageId,
|
|
289
|
+
metadata: event.metadata,
|
|
165
290
|
});
|
|
166
291
|
toolId = toolCallId;
|
|
167
292
|
}
|
|
168
|
-
return updateMessages(messages, {
|
|
293
|
+
return updateMessages(messages, {
|
|
294
|
+
type: EventType.TOOL_CALL_ARGS,
|
|
295
|
+
toolCallId,
|
|
296
|
+
delta: event.delta ?? "",
|
|
297
|
+
metadata: event.metadata,
|
|
298
|
+
});
|
|
169
299
|
}
|
|
170
300
|
if (event.type === EventType.TEXT_MESSAGE_START) {
|
|
171
301
|
if (textId && textId !== event.messageId)
|
|
172
|
-
messages = updateMessages(messages, {
|
|
302
|
+
messages = updateMessages(messages, {
|
|
303
|
+
type: EventType.TEXT_MESSAGE_END,
|
|
304
|
+
messageId: textId,
|
|
305
|
+
});
|
|
173
306
|
textId = event.messageId;
|
|
174
307
|
}
|
|
175
308
|
else if (event.type === EventType.REASONING_MESSAGE_START) {
|
|
176
309
|
if (reasoningId && reasoningId !== event.messageId)
|
|
177
|
-
messages = updateMessages(messages, {
|
|
310
|
+
messages = updateMessages(messages, {
|
|
311
|
+
type: EventType.REASONING_MESSAGE_END,
|
|
312
|
+
messageId: reasoningId,
|
|
313
|
+
});
|
|
178
314
|
reasoningId = event.messageId;
|
|
179
315
|
}
|
|
180
316
|
else if (event.type === EventType.TOOL_CALL_START) {
|
|
@@ -213,7 +349,14 @@ export function projectMessages(messages) {
|
|
|
213
349
|
if (message.role === "reasoning" && assistantIds.has(metadata?.reasoningOwnerId))
|
|
214
350
|
return [];
|
|
215
351
|
const parts = [];
|
|
216
|
-
const text = typeof metadata?.reasoningText === "string"
|
|
352
|
+
const text = typeof metadata?.reasoningText === "string"
|
|
353
|
+
? metadata.reasoningText
|
|
354
|
+
: typeof message.content === "string"
|
|
355
|
+
? message.content
|
|
356
|
+
: (message.content ?? [])
|
|
357
|
+
.filter((part) => part.type === "text")
|
|
358
|
+
.map((part) => part.text)
|
|
359
|
+
.join("");
|
|
217
360
|
const reasoning = ownedReasoning.get(message.id) ?? [];
|
|
218
361
|
let offset = 0;
|
|
219
362
|
for (const entry of reasoning) {
|
|
@@ -226,10 +369,15 @@ export function projectMessages(messages) {
|
|
|
226
369
|
offset = position;
|
|
227
370
|
}
|
|
228
371
|
if (offset < text.length)
|
|
229
|
-
parts.push({
|
|
372
|
+
parts.push({
|
|
373
|
+
type: message.role === "reasoning" ? "reasoning" : "text",
|
|
374
|
+
text: text.slice(offset),
|
|
375
|
+
});
|
|
230
376
|
if (message.role === "reasoning" && !text)
|
|
231
377
|
return [];
|
|
232
|
-
if (typeof metadata?.reasoningText === "string" &&
|
|
378
|
+
if (typeof metadata?.reasoningText === "string" &&
|
|
379
|
+
typeof message.content === "string" &&
|
|
380
|
+
message.content !== text) {
|
|
233
381
|
parts.push({ type: "text", text: message.content });
|
|
234
382
|
}
|
|
235
383
|
if (message.role === "assistant" && message.toolCalls?.length) {
|
|
@@ -245,22 +393,71 @@ export function projectMessages(messages) {
|
|
|
245
393
|
name = parsed.name;
|
|
246
394
|
args = JSON.stringify(parsed.input);
|
|
247
395
|
}
|
|
248
|
-
catch {
|
|
249
|
-
|
|
396
|
+
catch {
|
|
397
|
+
/* Incomplete arguments remain visible. */
|
|
398
|
+
}
|
|
399
|
+
let presentation = result?.role === "tool"
|
|
400
|
+
? toolError(result.id, call.id, result.content, result.metadata?.nexface?.parentErrorId, result.metadata?.nexface?.executionUnknown === true, name)
|
|
401
|
+
: {};
|
|
250
402
|
if (!presentation.toolState && result?.role === "tool" && result.error)
|
|
251
403
|
presentation = toolError(result.id, call.id, JSON.stringify({ error: { code: "TOOL_ERROR" } }));
|
|
252
404
|
const isError = Boolean(presentation.error);
|
|
253
|
-
parts.push({
|
|
254
|
-
...(
|
|
405
|
+
parts.push({
|
|
406
|
+
...(presentation.error
|
|
407
|
+
? { error: presentation.error, errorMessage: presentation.error.userMessage }
|
|
408
|
+
: {}),
|
|
409
|
+
...(presentation.toolState ? { toolState: presentation.toolState } : {}),
|
|
410
|
+
type: "tool",
|
|
411
|
+
toolCallId: call.id,
|
|
412
|
+
name,
|
|
413
|
+
arguments: args,
|
|
414
|
+
...(result?.role === "tool"
|
|
415
|
+
? {
|
|
416
|
+
result: result.content.length > 4000
|
|
417
|
+
? JSON.stringify({
|
|
418
|
+
contentOmitted: true,
|
|
419
|
+
preview: result.content.slice(0, 4000),
|
|
420
|
+
})
|
|
421
|
+
: result.content,
|
|
422
|
+
isError,
|
|
423
|
+
}
|
|
424
|
+
: {}),
|
|
255
425
|
});
|
|
256
426
|
}
|
|
257
427
|
}
|
|
258
|
-
const attachments = metadata?.attachments ??
|
|
428
|
+
const attachments = metadata?.attachments ??
|
|
429
|
+
(message.role === "user" && Array.isArray(message.content)
|
|
430
|
+
? message.content.flatMap((content, i) => content.type === "image"
|
|
431
|
+
? [{ id: `${message.id}-image-${i}`, name: "图片", content }]
|
|
432
|
+
: [])
|
|
433
|
+
: []);
|
|
259
434
|
if (metadata?.restoredParts)
|
|
260
435
|
parts.splice(0, parts.length, ...metadata.restoredParts);
|
|
261
436
|
const toolParts = parts.filter((part) => part.type === "tool");
|
|
262
|
-
const state = metadata?.state === "cancelled"
|
|
263
|
-
|
|
437
|
+
const state = metadata?.state === "cancelled"
|
|
438
|
+
? "cancelled"
|
|
439
|
+
: metadata?.state === "error"
|
|
440
|
+
? "error"
|
|
441
|
+
: toolParts.length
|
|
442
|
+
? toolParts.some((part) => part.result === undefined)
|
|
443
|
+
? "running"
|
|
444
|
+
: toolParts.some((part) => part.isError)
|
|
445
|
+
? "error"
|
|
446
|
+
: toolParts.some((part) => part.toolState === "cancelled")
|
|
447
|
+
? "cancelled"
|
|
448
|
+
: "success"
|
|
449
|
+
: metadata?.state;
|
|
450
|
+
return [
|
|
451
|
+
{
|
|
452
|
+
id: message.id,
|
|
453
|
+
role: message.role === "reasoning" ? "assistant" : message.role,
|
|
454
|
+
parts,
|
|
455
|
+
...(attachments.length ? { attachments } : {}),
|
|
456
|
+
...(metadata?.revisionOf ? { revisionOf: metadata.revisionOf } : {}),
|
|
457
|
+
...(state ? { state } : {}),
|
|
458
|
+
...(isNexfaceError(metadata?.error) ? { error: metadata.error } : {}),
|
|
459
|
+
},
|
|
460
|
+
];
|
|
264
461
|
});
|
|
265
462
|
projectionCache.set(messages, projected);
|
|
266
463
|
return projected;
|
|
@@ -269,13 +466,27 @@ export function restoreMessages(messages, history) {
|
|
|
269
466
|
const conversation = new Map(history.map((message) => [message.id, message]));
|
|
270
467
|
return messages.map((message) => {
|
|
271
468
|
const entry = conversation.get(message.id);
|
|
272
|
-
return {
|
|
273
|
-
|
|
274
|
-
|
|
469
|
+
return {
|
|
470
|
+
id: message.id,
|
|
471
|
+
role: message.role,
|
|
472
|
+
content: entry?.content ?? "",
|
|
473
|
+
metadata: {
|
|
474
|
+
nexface: {
|
|
475
|
+
restoredParts: message.parts,
|
|
476
|
+
attachments: message.attachments ?? [],
|
|
477
|
+
revisionOf: message.revisionOf,
|
|
478
|
+
state: message.state,
|
|
479
|
+
error: message.error,
|
|
480
|
+
displayOnly: message.role === "assistant" && message.parts.length === 0,
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
};
|
|
275
484
|
});
|
|
276
485
|
}
|
|
277
486
|
export function settleApproval(messages, approval, code, message, reason) {
|
|
278
|
-
const owner = messages.reduce((last, item, index) => item.role === "assistant" && item.toolCalls?.some((call) => call.id === approval.toolCallId)
|
|
487
|
+
const owner = messages.reduce((last, item, index) => item.role === "assistant" && item.toolCalls?.some((call) => call.id === approval.toolCallId)
|
|
488
|
+
? index
|
|
489
|
+
: last, -1);
|
|
279
490
|
const assistant = messages[owner];
|
|
280
491
|
if (!assistant || assistant.role !== "assistant")
|
|
281
492
|
return messages;
|
|
@@ -289,13 +500,26 @@ export function settleApproval(messages, approval, code, message, reason) {
|
|
|
289
500
|
for (const [index, call] of calls.slice(first).entries()) {
|
|
290
501
|
if (receipts.some((item) => item.role === "tool" && item.toolCallId === call.id))
|
|
291
502
|
continue;
|
|
292
|
-
const content = JSON.stringify({
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
503
|
+
const content = JSON.stringify({
|
|
504
|
+
error: index === 0
|
|
505
|
+
? {
|
|
506
|
+
code,
|
|
507
|
+
message,
|
|
508
|
+
details: { reason, toolExecuted: false },
|
|
509
|
+
}
|
|
510
|
+
: {
|
|
511
|
+
code: "TOOL_SKIPPED",
|
|
512
|
+
message: "Skipped because an earlier tool call required confirmation.",
|
|
513
|
+
details: { failedToolCallId: approval.toolCallId },
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
next = updateMessages(next, {
|
|
517
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
518
|
+
messageId: uuid(),
|
|
519
|
+
role: "tool",
|
|
520
|
+
toolCallId: call.id,
|
|
521
|
+
content,
|
|
522
|
+
});
|
|
298
523
|
}
|
|
299
524
|
return [...next, ...messages.slice(end)];
|
|
300
525
|
}
|
|
301
|
-
//# sourceMappingURL=protocol-messages.js.map
|