@meshagent/meshagent-tailwind 0.43.3 → 0.44.1
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/CHANGELOG.md +14 -0
- package/dist/cjs/chat/agent-thread-message-status-store.d.ts +105 -0
- package/dist/cjs/chat/agent-thread-message-status-store.js +617 -0
- package/dist/cjs/chat/agent-thread.d.ts +76 -4
- package/dist/cjs/chat/agent-thread.js +1114 -54
- package/dist/cjs/chat/chat-bot-view.d.ts +7 -4
- package/dist/cjs/chat/chat-bot-view.js +33 -525
- package/dist/cjs/chat/chat-hooks.d.ts +6 -5
- package/dist/cjs/chat/chat-hooks.js +21 -466
- package/dist/cjs/chat/chat-typing-indicator.d.ts +15 -1
- package/dist/cjs/chat/chat-typing-indicator.js +35 -5
- package/dist/cjs/chat/file-attachment.d.ts +7 -3
- package/dist/cjs/chat/file-attachment.js +30 -6
- package/dist/cjs/chat/multi-thread-view.d.ts +3 -1
- package/dist/cjs/chat/multi-thread-view.js +4 -2
- package/dist/cjs/chat/new-chat-thread.d.ts +3 -1
- package/dist/cjs/chat/new-chat-thread.js +7 -3
- package/dist/cjs/chat/thread-list-view.d.ts +22 -0
- package/dist/cjs/chat/thread-list-view.js +518 -0
- package/dist/cjs/chat/tool-call-status-accumulator.d.ts +69 -0
- package/dist/cjs/chat/tool-call-status-accumulator.js +268 -0
- package/dist/cjs/components/pane-example.d.ts +1 -1
- package/dist/cjs/components/ui/avatar.d.ts +3 -3
- package/dist/cjs/components/ui/button.d.ts +3 -3
- package/dist/cjs/components/ui/card.d.ts +7 -7
- package/dist/cjs/components/ui/dialog.d.ts +10 -10
- package/dist/cjs/components/ui/form.d.ts +7 -7
- package/dist/cjs/components/ui/input.d.ts +1 -1
- package/dist/cjs/components/ui/label.d.ts +1 -1
- package/dist/cjs/components/ui/progress.d.ts +1 -1
- package/dist/cjs/components/ui/radio-group.d.ts +2 -2
- package/dist/cjs/components/ui/select.d.ts +10 -10
- package/dist/cjs/components/ui/sheet.d.ts +8 -8
- package/dist/cjs/components/ui/skeleton.d.ts +1 -1
- package/dist/cjs/components/ui/sonner.d.ts +1 -1
- package/dist/cjs/components/ui/spinner.d.ts +2 -2
- package/dist/cjs/components/ui/textarea.d.ts +1 -1
- package/dist/cjs/file-preview/file-preview.d.ts +1 -0
- package/dist/cjs/file-preview/file-preview.js +41 -5
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/meetings/camera-grid.d.ts +2 -2
- package/dist/cjs/tools/ask-user-for-file.js +2 -2
- package/dist/cjs/tools/file-dialog.d.ts +2 -1
- package/dist/cjs/tools/form-dialog.d.ts +2 -1
- package/dist/esm/chat/agent-thread-message-status-store.d.ts +105 -0
- package/dist/esm/chat/agent-thread-message-status-store.js +614 -0
- package/dist/esm/chat/agent-thread.d.ts +76 -4
- package/dist/esm/chat/agent-thread.js +1132 -56
- package/dist/esm/chat/chat-bot-view.d.ts +7 -4
- package/dist/esm/chat/chat-bot-view.js +40 -555
- package/dist/esm/chat/chat-hooks.d.ts +6 -5
- package/dist/esm/chat/chat-hooks.js +23 -468
- package/dist/esm/chat/chat-typing-indicator.d.ts +15 -1
- package/dist/esm/chat/chat-typing-indicator.js +35 -5
- package/dist/esm/chat/file-attachment.d.ts +7 -3
- package/dist/esm/chat/file-attachment.js +30 -6
- package/dist/esm/chat/multi-thread-view.d.ts +3 -1
- package/dist/esm/chat/multi-thread-view.js +4 -2
- package/dist/esm/chat/new-chat-thread.d.ts +3 -1
- package/dist/esm/chat/new-chat-thread.js +8 -4
- package/dist/esm/chat/thread-list-view.d.ts +22 -0
- package/dist/esm/chat/thread-list-view.js +514 -0
- package/dist/esm/chat/tool-call-status-accumulator.d.ts +69 -0
- package/dist/esm/chat/tool-call-status-accumulator.js +248 -0
- package/dist/esm/components/pane-example.d.ts +1 -1
- package/dist/esm/components/ui/avatar.d.ts +3 -3
- package/dist/esm/components/ui/button.d.ts +3 -3
- package/dist/esm/components/ui/card.d.ts +7 -7
- package/dist/esm/components/ui/dialog.d.ts +10 -10
- package/dist/esm/components/ui/form.d.ts +7 -7
- package/dist/esm/components/ui/input.d.ts +1 -1
- package/dist/esm/components/ui/label.d.ts +1 -1
- package/dist/esm/components/ui/progress.d.ts +1 -1
- package/dist/esm/components/ui/radio-group.d.ts +2 -2
- package/dist/esm/components/ui/select.d.ts +10 -10
- package/dist/esm/components/ui/sheet.d.ts +8 -8
- package/dist/esm/components/ui/skeleton.d.ts +1 -1
- package/dist/esm/components/ui/sonner.d.ts +1 -1
- package/dist/esm/components/ui/spinner.d.ts +2 -2
- package/dist/esm/components/ui/textarea.d.ts +1 -1
- package/dist/esm/file-preview/file-preview.d.ts +1 -0
- package/dist/esm/file-preview/file-preview.js +29 -3
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/meetings/camera-grid.d.ts +2 -2
- package/dist/esm/tools/ask-user-for-file.js +2 -2
- package/dist/esm/tools/file-dialog.d.ts +2 -1
- package/dist/esm/tools/form-dialog.d.ts +2 -1
- package/dist/index.css +1 -1
- package/package.json +4 -4
- package/dist/cjs/chat/chat-bot-view.test.js +0 -325
- package/dist/cjs/chat/live-chat-widgets.test.js +0 -256
- package/dist/esm/chat/chat-bot-view.test.js +0 -336
- package/dist/esm/chat/live-chat-widgets.test.js +0 -262
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import {
|
|
2
|
+
agentThreadClearedType,
|
|
3
|
+
agentThreadStatusType,
|
|
4
|
+
agentToolCallArgumentsDeltaType,
|
|
5
|
+
agentToolCallEndedType,
|
|
6
|
+
agentToolCallInProgressType,
|
|
7
|
+
agentToolCallPendingType,
|
|
8
|
+
agentToolCallStartedType,
|
|
9
|
+
agentTurnEndedType,
|
|
10
|
+
agentTurnStartAcceptedType,
|
|
11
|
+
agentTurnStartRejectedType,
|
|
12
|
+
agentTurnStartType,
|
|
13
|
+
agentTurnStartedType,
|
|
14
|
+
agentTurnSteerAcceptedType,
|
|
15
|
+
agentTurnSteerRejectedType,
|
|
16
|
+
agentTurnSteerType,
|
|
17
|
+
agentTurnSteeredType
|
|
18
|
+
} from "@meshagent/meshagent-agents";
|
|
19
|
+
import { LiveToolCallAccumulator } from "./tool-call-status-accumulator.js";
|
|
20
|
+
class PendingAgentMessage {
|
|
21
|
+
messageId;
|
|
22
|
+
messageType;
|
|
23
|
+
threadPath;
|
|
24
|
+
text;
|
|
25
|
+
attachments;
|
|
26
|
+
senderName;
|
|
27
|
+
createdAt;
|
|
28
|
+
matchByContentOnly;
|
|
29
|
+
awaitingAcceptance;
|
|
30
|
+
awaitingApplication;
|
|
31
|
+
awaitingOnline;
|
|
32
|
+
constructor(params) {
|
|
33
|
+
this.messageId = params.messageId;
|
|
34
|
+
this.messageType = params.messageType;
|
|
35
|
+
this.threadPath = params.threadPath;
|
|
36
|
+
this.text = params.text;
|
|
37
|
+
this.attachments = params.attachments;
|
|
38
|
+
this.senderName = params.senderName;
|
|
39
|
+
this.createdAt = params.createdAt;
|
|
40
|
+
this.matchByContentOnly = params.matchByContentOnly ?? false;
|
|
41
|
+
this.awaitingAcceptance = params.awaitingAcceptance ?? false;
|
|
42
|
+
this.awaitingApplication = params.awaitingApplication ?? false;
|
|
43
|
+
this.awaitingOnline = params.awaitingOnline ?? false;
|
|
44
|
+
}
|
|
45
|
+
get hasVisibleContent() {
|
|
46
|
+
return this.text.trim() !== "" || this.attachments.length > 0;
|
|
47
|
+
}
|
|
48
|
+
static fromQueueJson(json) {
|
|
49
|
+
const parsedContent = parseContent(json["content"]);
|
|
50
|
+
const createdAt = json["created_at"];
|
|
51
|
+
const parsedCreatedAt = typeof createdAt === "string" ? new Date(createdAt) : void 0;
|
|
52
|
+
return new PendingAgentMessage({
|
|
53
|
+
messageId: typeof json["message_id"] === "string" ? json["message_id"] : crypto.randomUUID(),
|
|
54
|
+
messageType: typeof json["message_type"] === "string" ? json["message_type"] : agentTurnSteerType,
|
|
55
|
+
threadPath: typeof json["thread_id"] === "string" ? json["thread_id"] : "",
|
|
56
|
+
text: parsedContent.text,
|
|
57
|
+
attachments: parsedContent.attachments,
|
|
58
|
+
senderName: normalizeString(typeof json["sender_name"] === "string" ? json["sender_name"] : void 0),
|
|
59
|
+
createdAt: parsedCreatedAt instanceof Date && !Number.isNaN(parsedCreatedAt.getTime()) ? parsedCreatedAt : void 0,
|
|
60
|
+
matchByContentOnly: false,
|
|
61
|
+
awaitingApplication: true,
|
|
62
|
+
awaitingOnline: false
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
static fromTurnInputMessage(message) {
|
|
66
|
+
const content = message.type === agentTurnStartType || message.type === agentTurnSteerType ? message.content : [];
|
|
67
|
+
const parsedContent = parseContent(content);
|
|
68
|
+
return new PendingAgentMessage({
|
|
69
|
+
messageId: normalizeString(message.messageId) ?? crypto.randomUUID(),
|
|
70
|
+
messageType: message.type,
|
|
71
|
+
threadPath: message.threadId,
|
|
72
|
+
text: parsedContent.text,
|
|
73
|
+
attachments: parsedContent.attachments,
|
|
74
|
+
senderName: normalizeString(message.senderName),
|
|
75
|
+
matchByContentOnly: false,
|
|
76
|
+
awaitingAcceptance: true,
|
|
77
|
+
awaitingApplication: true,
|
|
78
|
+
awaitingOnline: false
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
static fromAcceptedMessage(message) {
|
|
82
|
+
const parsedContent = parseContent(message.content);
|
|
83
|
+
return new PendingAgentMessage({
|
|
84
|
+
messageId: normalizeString(message.sourceMessageId) ?? crypto.randomUUID(),
|
|
85
|
+
messageType: message.type === agentTurnSteerAcceptedType ? agentTurnSteerType : agentTurnStartType,
|
|
86
|
+
threadPath: message.threadId,
|
|
87
|
+
text: parsedContent.text,
|
|
88
|
+
attachments: parsedContent.attachments,
|
|
89
|
+
senderName: normalizeString(message.senderName),
|
|
90
|
+
matchByContentOnly: false,
|
|
91
|
+
awaitingAcceptance: false,
|
|
92
|
+
awaitingApplication: true,
|
|
93
|
+
awaitingOnline: false
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
class ChatThreadStatusState {
|
|
98
|
+
text;
|
|
99
|
+
startedAt;
|
|
100
|
+
mode;
|
|
101
|
+
turnId;
|
|
102
|
+
pendingMessages;
|
|
103
|
+
pendingItemId;
|
|
104
|
+
totalBytes;
|
|
105
|
+
linesAdded;
|
|
106
|
+
linesRemoved;
|
|
107
|
+
supportsAgentMessages;
|
|
108
|
+
constructor(params = {}) {
|
|
109
|
+
this.text = params.text;
|
|
110
|
+
this.startedAt = params.startedAt;
|
|
111
|
+
this.mode = params.mode;
|
|
112
|
+
this.turnId = params.turnId;
|
|
113
|
+
this.pendingMessages = params.pendingMessages ?? [];
|
|
114
|
+
this.pendingItemId = params.pendingItemId;
|
|
115
|
+
this.totalBytes = params.totalBytes;
|
|
116
|
+
this.linesAdded = params.linesAdded;
|
|
117
|
+
this.linesRemoved = params.linesRemoved;
|
|
118
|
+
this.supportsAgentMessages = params.supportsAgentMessages ?? false;
|
|
119
|
+
}
|
|
120
|
+
get hasStatus() {
|
|
121
|
+
return this.text?.trim() !== "";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function shouldShowChatThreadStatus(status) {
|
|
125
|
+
return status.hasStatus;
|
|
126
|
+
}
|
|
127
|
+
class AgentThreadMessageStatusStore {
|
|
128
|
+
static maxRemoteStatusClockSkewMs = 2 * 60 * 1e3;
|
|
129
|
+
touchedThreadPaths = /* @__PURE__ */ new Set();
|
|
130
|
+
statusByThreadPath = /* @__PURE__ */ new Map();
|
|
131
|
+
pendingMessagesByThreadPath = /* @__PURE__ */ new Map();
|
|
132
|
+
toolCallAccumulatorsByThreadPath = /* @__PURE__ */ new Map();
|
|
133
|
+
apply(message, options = {}) {
|
|
134
|
+
if (!isAgentThreadMessage(message) || message.threadId.trim() === "") {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const threadMessage = message;
|
|
138
|
+
const normalizedThreadPath = threadMessage.threadId.trim();
|
|
139
|
+
switch (threadMessage.type) {
|
|
140
|
+
case agentThreadStatusType:
|
|
141
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
142
|
+
return this.applyStatus(normalizedThreadPath, threadMessage);
|
|
143
|
+
case agentTurnStartType:
|
|
144
|
+
case agentTurnSteerType:
|
|
145
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
146
|
+
return this.applyTurnInput(normalizedThreadPath, threadMessage);
|
|
147
|
+
case agentTurnStartAcceptedType:
|
|
148
|
+
case agentTurnSteerAcceptedType:
|
|
149
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
150
|
+
return this.applyAccepted(normalizedThreadPath, threadMessage);
|
|
151
|
+
case agentTurnStartedType:
|
|
152
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
153
|
+
return this.markPendingApplied(
|
|
154
|
+
normalizedThreadPath,
|
|
155
|
+
threadMessage.sourceMessageId,
|
|
156
|
+
threadMessage.turnId
|
|
157
|
+
);
|
|
158
|
+
case agentTurnSteeredType:
|
|
159
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
160
|
+
return this.markPendingApplied(
|
|
161
|
+
normalizedThreadPath,
|
|
162
|
+
threadMessage.sourceMessageId
|
|
163
|
+
);
|
|
164
|
+
case agentToolCallArgumentsDeltaType:
|
|
165
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
166
|
+
return this.applyToolCallArgumentsDelta(
|
|
167
|
+
normalizedThreadPath,
|
|
168
|
+
threadMessage
|
|
169
|
+
);
|
|
170
|
+
case agentToolCallPendingType:
|
|
171
|
+
case agentToolCallInProgressType:
|
|
172
|
+
case agentToolCallStartedType:
|
|
173
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
174
|
+
return this.applyToolCallLifecycle(normalizedThreadPath, threadMessage);
|
|
175
|
+
case agentToolCallEndedType:
|
|
176
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
177
|
+
return this.clearToolCallBytes(normalizedThreadPath, threadMessage);
|
|
178
|
+
case agentTurnStartRejectedType:
|
|
179
|
+
case agentTurnSteerRejectedType:
|
|
180
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
181
|
+
return this.removePending(normalizedThreadPath, threadMessage.sourceMessageId);
|
|
182
|
+
case agentTurnEndedType:
|
|
183
|
+
case agentThreadClearedType: {
|
|
184
|
+
this.touchedThreadPaths.add(normalizedThreadPath);
|
|
185
|
+
const hadPending = (this.pendingMessagesByThreadPath.get(normalizedThreadPath)?.size ?? 0) > 0;
|
|
186
|
+
const hadStatus = this.statusByThreadPath.delete(normalizedThreadPath);
|
|
187
|
+
const hadTools = this.toolCallAccumulatorsByThreadPath.get(normalizedThreadPath)?.isEmpty === false;
|
|
188
|
+
this.pendingMessagesByThreadPath.delete(normalizedThreadPath);
|
|
189
|
+
this.toolCallAccumulatorsByThreadPath.delete(normalizedThreadPath);
|
|
190
|
+
return hadPending || hadStatus || hadTools;
|
|
191
|
+
}
|
|
192
|
+
default:
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
hasThread(path) {
|
|
197
|
+
return this.touchedThreadPaths.has(path.trim());
|
|
198
|
+
}
|
|
199
|
+
clearThread(path) {
|
|
200
|
+
const normalizedPath = path.trim();
|
|
201
|
+
if (normalizedPath === "") {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.touchedThreadPaths.delete(normalizedPath);
|
|
205
|
+
this.statusByThreadPath.delete(normalizedPath);
|
|
206
|
+
this.pendingMessagesByThreadPath.delete(normalizedPath);
|
|
207
|
+
this.toolCallAccumulatorsByThreadPath.delete(normalizedPath);
|
|
208
|
+
}
|
|
209
|
+
state(params) {
|
|
210
|
+
const normalizedPath = params.path.trim();
|
|
211
|
+
const status = this.statusByThreadPath.get(normalizedPath);
|
|
212
|
+
const pendingMessages = [...this.pendingMessagesByThreadPath.get(normalizedPath)?.values() ?? []];
|
|
213
|
+
if (status == null && pendingMessages.length === 0) {
|
|
214
|
+
return new ChatThreadStatusState({ supportsAgentMessages: params.supportsAgentMessages });
|
|
215
|
+
}
|
|
216
|
+
let startedAt = status?.startedAt;
|
|
217
|
+
if (status?.text != null) {
|
|
218
|
+
startedAt ??= /* @__PURE__ */ new Date();
|
|
219
|
+
}
|
|
220
|
+
return new ChatThreadStatusState({
|
|
221
|
+
text: status?.text,
|
|
222
|
+
startedAt,
|
|
223
|
+
mode: status?.text == null ? status?.mode : status.mode ?? "busy",
|
|
224
|
+
turnId: status?.turnId,
|
|
225
|
+
pendingMessages,
|
|
226
|
+
pendingItemId: status?.pendingItemId,
|
|
227
|
+
totalBytes: status?.totalBytes,
|
|
228
|
+
linesAdded: status?.linesAdded,
|
|
229
|
+
linesRemoved: status?.linesRemoved,
|
|
230
|
+
supportsAgentMessages: true
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
applyStatus(threadPath, message) {
|
|
234
|
+
const previous = this.statusByThreadPath.get(threadPath);
|
|
235
|
+
const text = normalizeString(message.status);
|
|
236
|
+
const rawMode = normalizeString(message.mode)?.toLowerCase();
|
|
237
|
+
const mode = rawMode === "busy" || rawMode === "steerable" ? rawMode : void 0;
|
|
238
|
+
const parsedStartedAt = parseDate(message.startedAt);
|
|
239
|
+
const turnId = normalizeString(message.turnId);
|
|
240
|
+
const pendingItemId = normalizeString(message.pendingItemId);
|
|
241
|
+
const sameStatusOperation = previous != null && previous.text === text && previous.turnId === turnId && previous.pendingItemId === pendingItemId;
|
|
242
|
+
const startedAt = text == null ? void 0 : this.statusStartedAt(parsedStartedAt, sameStatusOperation ? previous?.startedAt : void 0);
|
|
243
|
+
const parsedTotalBytes = positiveIntValue(message.totalBytes);
|
|
244
|
+
const linesAdded = nonNegativeIntValue(message.linesAdded) ?? (message.linesAdded == null && previous?.text === text ? previous?.linesAdded : void 0);
|
|
245
|
+
const linesRemoved = nonNegativeIntValue(message.linesRemoved) ?? (message.linesRemoved == null && previous?.text === text ? previous?.linesRemoved : void 0);
|
|
246
|
+
const totalBytes = parsedTotalBytes ?? this.toolArgumentBytes(threadPath, pendingItemId) ?? (message.totalBytes == null && previous?.text === text ? previous?.totalBytes : void 0);
|
|
247
|
+
const totalBytesFromStatus = parsedTotalBytes != null;
|
|
248
|
+
if (text == null && mode == null && startedAt == null && turnId == null && pendingItemId == null && totalBytes == null && linesAdded == null && linesRemoved == null) {
|
|
249
|
+
return this.statusByThreadPath.delete(threadPath);
|
|
250
|
+
}
|
|
251
|
+
const next = {
|
|
252
|
+
text,
|
|
253
|
+
startedAt,
|
|
254
|
+
mode,
|
|
255
|
+
turnId,
|
|
256
|
+
pendingItemId,
|
|
257
|
+
totalBytes,
|
|
258
|
+
totalBytesFromStatus,
|
|
259
|
+
linesAdded,
|
|
260
|
+
linesRemoved
|
|
261
|
+
};
|
|
262
|
+
if (statusEquals(previous, next)) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
this.statusByThreadPath.set(threadPath, next);
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
statusStartedAt(parsedStartedAt, previousStartedAt) {
|
|
269
|
+
if (previousStartedAt != null) {
|
|
270
|
+
return previousStartedAt;
|
|
271
|
+
}
|
|
272
|
+
const now = /* @__PURE__ */ new Date();
|
|
273
|
+
if (parsedStartedAt == null) {
|
|
274
|
+
return now;
|
|
275
|
+
}
|
|
276
|
+
const skew = Math.abs(now.getTime() - parsedStartedAt.getTime());
|
|
277
|
+
return skew > AgentThreadMessageStatusStore.maxRemoteStatusClockSkewMs ? now : parsedStartedAt;
|
|
278
|
+
}
|
|
279
|
+
applyToolCallArgumentsDelta(threadPath, message) {
|
|
280
|
+
const itemId = normalizeString(message.itemId);
|
|
281
|
+
if (itemId == null) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
const deltaBytes = new TextEncoder().encode(message.delta).length;
|
|
285
|
+
if (deltaBytes <= 0) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
const accumulator = this.toolCallAccumulator(threadPath);
|
|
289
|
+
const status = this.statusByThreadPath.get(threadPath);
|
|
290
|
+
const snapshot = accumulator.appendDelta({ itemId, delta: message.delta, fallbackText: status?.text });
|
|
291
|
+
if (status?.text == null || status.text.trim() === "") {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
const isStatusItem = status.pendingItemId == null || status.pendingItemId === itemId || accumulator.hasSingleItem(itemId);
|
|
295
|
+
if (!isStatusItem) {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
const nextPendingItemId = status.pendingItemId == null || status.pendingItemId === itemId ? status.pendingItemId ?? itemId : itemId;
|
|
299
|
+
const nextStatusText = snapshot.text ?? status.text;
|
|
300
|
+
const nextTotalBytes = snapshot.totalBytes == null ? status.totalBytes : Math.max(status.totalBytes ?? 0, snapshot.totalBytes);
|
|
301
|
+
if (status.text === nextStatusText && status.totalBytes === nextTotalBytes && status.linesAdded === (snapshot.linesAdded ?? status.linesAdded) && status.linesRemoved === (snapshot.linesRemoved ?? status.linesRemoved)) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
this.statusByThreadPath.set(threadPath, {
|
|
305
|
+
text: nextStatusText,
|
|
306
|
+
startedAt: status.startedAt,
|
|
307
|
+
mode: status.mode,
|
|
308
|
+
turnId: status.turnId,
|
|
309
|
+
pendingItemId: nextPendingItemId,
|
|
310
|
+
totalBytes: nextTotalBytes,
|
|
311
|
+
totalBytesFromStatus: false,
|
|
312
|
+
linesAdded: snapshot.linesAdded ?? status.linesAdded,
|
|
313
|
+
linesRemoved: snapshot.linesRemoved ?? status.linesRemoved
|
|
314
|
+
});
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
applyToolCallLifecycle(threadPath, message) {
|
|
318
|
+
const itemId = normalizeString(message.itemId);
|
|
319
|
+
if (itemId == null) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
const accumulator = this.toolCallAccumulator(threadPath);
|
|
323
|
+
const existing = accumulator.get(itemId);
|
|
324
|
+
const tool = normalizeString(message.tool) ?? existing?.tool ?? "";
|
|
325
|
+
const args = message.arguments ?? existing?.arguments;
|
|
326
|
+
const status = this.statusByThreadPath.get(threadPath);
|
|
327
|
+
const snapshot = accumulator.upsert({ itemId, tool, arguments: args, fallbackText: status?.text });
|
|
328
|
+
if (status?.text == null || status.text.trim() === "") {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const nextPendingItemId = status.pendingItemId == null || status.pendingItemId === itemId || accumulator.hasSingleItem(itemId) ? itemId : status.pendingItemId;
|
|
332
|
+
const nextTotalBytes = snapshot.totalBytes == null ? status.totalBytes : Math.max(status.totalBytes ?? 0, snapshot.totalBytes);
|
|
333
|
+
if (nextPendingItemId === status.pendingItemId && snapshot.text === status.text && snapshot.linesAdded == null && snapshot.linesRemoved == null && nextTotalBytes === status.totalBytes) {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
this.statusByThreadPath.set(threadPath, {
|
|
337
|
+
text: snapshot.text ?? status.text,
|
|
338
|
+
startedAt: status.startedAt,
|
|
339
|
+
mode: status.mode,
|
|
340
|
+
turnId: status.turnId,
|
|
341
|
+
pendingItemId: nextPendingItemId,
|
|
342
|
+
totalBytes: nextTotalBytes,
|
|
343
|
+
totalBytesFromStatus: false,
|
|
344
|
+
linesAdded: snapshot.linesAdded ?? status.linesAdded,
|
|
345
|
+
linesRemoved: snapshot.linesRemoved ?? status.linesRemoved
|
|
346
|
+
});
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
clearToolCallBytes(threadPath, message) {
|
|
350
|
+
const itemId = normalizeString(message.itemId);
|
|
351
|
+
let hadBytes = false;
|
|
352
|
+
if (itemId != null) {
|
|
353
|
+
const accumulator = this.toolCallAccumulatorsByThreadPath.get(threadPath);
|
|
354
|
+
hadBytes = accumulator?.remove(itemId) === true;
|
|
355
|
+
if (accumulator?.isEmpty === true) {
|
|
356
|
+
this.toolCallAccumulatorsByThreadPath.delete(threadPath);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const status = this.statusByThreadPath.get(threadPath);
|
|
360
|
+
if (status?.totalBytes == null || itemId != null && status.pendingItemId !== itemId) {
|
|
361
|
+
return hadBytes;
|
|
362
|
+
}
|
|
363
|
+
this.statusByThreadPath.set(threadPath, {
|
|
364
|
+
text: status.text,
|
|
365
|
+
startedAt: status.startedAt,
|
|
366
|
+
mode: status.mode,
|
|
367
|
+
turnId: status.turnId,
|
|
368
|
+
pendingItemId: status.pendingItemId,
|
|
369
|
+
totalBytesFromStatus: false
|
|
370
|
+
});
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
applyTurnInput(threadPath, message) {
|
|
374
|
+
const parsedMessage = PendingAgentMessage.fromTurnInputMessage(message);
|
|
375
|
+
if (parsedMessage.messageId.trim() === "") {
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
return this.upsertPendingMessage(threadPath, parsedMessage);
|
|
379
|
+
}
|
|
380
|
+
applyAccepted(threadPath, message) {
|
|
381
|
+
const parsedMessage = PendingAgentMessage.fromAcceptedMessage(message);
|
|
382
|
+
if (parsedMessage.messageId.trim() === "") {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
const pendingMessages = this.pendingMessages(threadPath);
|
|
386
|
+
const existing = pendingMessages.get(parsedMessage.messageId);
|
|
387
|
+
if (existing == null && parsedMessage.text.trim() === "" && parsedMessage.attachments.length === 0) {
|
|
388
|
+
if (pendingMessages.size === 0) {
|
|
389
|
+
this.pendingMessagesByThreadPath.delete(threadPath);
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
const nextMessage = existing == null ? parsedMessage : new PendingAgentMessage({
|
|
394
|
+
messageId: existing.messageId,
|
|
395
|
+
messageType: existing.messageType,
|
|
396
|
+
threadPath: existing.threadPath,
|
|
397
|
+
text: existing.text,
|
|
398
|
+
attachments: existing.attachments,
|
|
399
|
+
senderName: existing.senderName,
|
|
400
|
+
createdAt: existing.createdAt,
|
|
401
|
+
matchByContentOnly: existing.matchByContentOnly,
|
|
402
|
+
awaitingAcceptance: false,
|
|
403
|
+
awaitingApplication: existing.awaitingApplication,
|
|
404
|
+
awaitingOnline: existing.awaitingOnline
|
|
405
|
+
});
|
|
406
|
+
return this.upsertPendingMessage(threadPath, nextMessage);
|
|
407
|
+
}
|
|
408
|
+
upsertPendingMessage(threadPath, message) {
|
|
409
|
+
const pendingMessages = this.pendingMessages(threadPath);
|
|
410
|
+
if (!message.hasVisibleContent) {
|
|
411
|
+
const removed = pendingMessages.delete(message.messageId);
|
|
412
|
+
if (pendingMessages.size === 0) {
|
|
413
|
+
this.pendingMessagesByThreadPath.delete(threadPath);
|
|
414
|
+
}
|
|
415
|
+
return removed;
|
|
416
|
+
}
|
|
417
|
+
const existing = pendingMessages.get(message.messageId);
|
|
418
|
+
if (existing != null && existing.messageType === message.messageType && existing.text === message.text && attachmentsEqual(existing.attachments, message.attachments) && existing.senderName === message.senderName && existing.awaitingAcceptance === message.awaitingAcceptance && existing.awaitingApplication === message.awaitingApplication) {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
pendingMessages.set(message.messageId, message);
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
markPendingApplied(threadPath, sourceMessageId, turnId) {
|
|
425
|
+
let changed = false;
|
|
426
|
+
const pendingMessages = this.pendingMessagesByThreadPath.get(threadPath);
|
|
427
|
+
const normalizedSourceMessageId = normalizeString(sourceMessageId);
|
|
428
|
+
if (normalizedSourceMessageId != null) {
|
|
429
|
+
const existing = pendingMessages?.get(normalizedSourceMessageId);
|
|
430
|
+
if (existing != null && existing.awaitingApplication) {
|
|
431
|
+
if (existing.messageType === agentTurnSteerType) {
|
|
432
|
+
pendingMessages?.delete(normalizedSourceMessageId);
|
|
433
|
+
if (pendingMessages?.size === 0) {
|
|
434
|
+
this.pendingMessagesByThreadPath.delete(threadPath);
|
|
435
|
+
}
|
|
436
|
+
} else {
|
|
437
|
+
pendingMessages?.set(normalizedSourceMessageId, new PendingAgentMessage({
|
|
438
|
+
messageId: existing.messageId,
|
|
439
|
+
messageType: existing.messageType,
|
|
440
|
+
threadPath: existing.threadPath,
|
|
441
|
+
text: existing.text,
|
|
442
|
+
attachments: existing.attachments,
|
|
443
|
+
senderName: existing.senderName,
|
|
444
|
+
createdAt: existing.createdAt,
|
|
445
|
+
matchByContentOnly: existing.matchByContentOnly,
|
|
446
|
+
awaitingAcceptance: existing.awaitingAcceptance,
|
|
447
|
+
awaitingApplication: false,
|
|
448
|
+
awaitingOnline: existing.awaitingOnline
|
|
449
|
+
}));
|
|
450
|
+
}
|
|
451
|
+
changed = true;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return this.applyTurnId(threadPath, turnId) || changed;
|
|
455
|
+
}
|
|
456
|
+
removePending(threadPath, sourceMessageId, turnId) {
|
|
457
|
+
const pendingMessages = this.pendingMessagesByThreadPath.get(threadPath);
|
|
458
|
+
const normalizedSourceMessageId = normalizeString(sourceMessageId);
|
|
459
|
+
const changed = normalizedSourceMessageId != null && pendingMessages?.delete(normalizedSourceMessageId) === true;
|
|
460
|
+
if (pendingMessages?.size === 0) {
|
|
461
|
+
this.pendingMessagesByThreadPath.delete(threadPath);
|
|
462
|
+
}
|
|
463
|
+
return this.applyTurnId(threadPath, turnId) || changed;
|
|
464
|
+
}
|
|
465
|
+
applyTurnId(threadPath, turnId) {
|
|
466
|
+
const normalizedTurnId = normalizeString(turnId);
|
|
467
|
+
if (normalizedTurnId == null) {
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
const previous = this.statusByThreadPath.get(threadPath);
|
|
471
|
+
if (previous?.turnId === normalizedTurnId) {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
this.statusByThreadPath.set(threadPath, {
|
|
475
|
+
text: previous?.text,
|
|
476
|
+
startedAt: previous?.text != null && previous.turnId != null ? /* @__PURE__ */ new Date() : previous?.startedAt,
|
|
477
|
+
mode: previous?.mode,
|
|
478
|
+
turnId: normalizedTurnId,
|
|
479
|
+
pendingItemId: previous?.pendingItemId,
|
|
480
|
+
totalBytes: previous?.totalBytes,
|
|
481
|
+
totalBytesFromStatus: false,
|
|
482
|
+
linesAdded: previous?.linesAdded,
|
|
483
|
+
linesRemoved: previous?.linesRemoved
|
|
484
|
+
});
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
toolArgumentBytes(threadPath, itemId) {
|
|
488
|
+
const normalizedItemId = normalizeString(itemId);
|
|
489
|
+
if (normalizedItemId == null) {
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
492
|
+
return this.toolCallAccumulatorsByThreadPath.get(threadPath)?.totalBytes(normalizedItemId);
|
|
493
|
+
}
|
|
494
|
+
pendingMessages(threadPath) {
|
|
495
|
+
let pendingMessages = this.pendingMessagesByThreadPath.get(threadPath);
|
|
496
|
+
if (pendingMessages == null) {
|
|
497
|
+
pendingMessages = /* @__PURE__ */ new Map();
|
|
498
|
+
this.pendingMessagesByThreadPath.set(threadPath, pendingMessages);
|
|
499
|
+
}
|
|
500
|
+
return pendingMessages;
|
|
501
|
+
}
|
|
502
|
+
toolCallAccumulator(threadPath) {
|
|
503
|
+
let accumulator = this.toolCallAccumulatorsByThreadPath.get(threadPath);
|
|
504
|
+
if (accumulator == null) {
|
|
505
|
+
accumulator = new LiveToolCallAccumulator();
|
|
506
|
+
this.toolCallAccumulatorsByThreadPath.set(threadPath, accumulator);
|
|
507
|
+
}
|
|
508
|
+
return accumulator;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
function trackAgentThreadStatusMessageInStore(params) {
|
|
512
|
+
return params.store.apply(params.message, { path: params.path });
|
|
513
|
+
}
|
|
514
|
+
function resolveChatThreadStatusFromStore(params) {
|
|
515
|
+
const hasMessageStatus = params.store.hasThread(params.path);
|
|
516
|
+
const messageState = params.store.state({
|
|
517
|
+
path: params.path,
|
|
518
|
+
previous: params.previous,
|
|
519
|
+
supportsAgentMessages: hasMessageStatus || params.supportsAgentMessages === true
|
|
520
|
+
});
|
|
521
|
+
let nextStartedAt = messageState.startedAt;
|
|
522
|
+
let nextMode = messageState.mode;
|
|
523
|
+
if (messageState.text != null) {
|
|
524
|
+
nextMode ??= "busy";
|
|
525
|
+
nextStartedAt ??= /* @__PURE__ */ new Date();
|
|
526
|
+
}
|
|
527
|
+
return new ChatThreadStatusState({
|
|
528
|
+
text: messageState.text,
|
|
529
|
+
startedAt: nextStartedAt,
|
|
530
|
+
mode: nextMode,
|
|
531
|
+
turnId: messageState.turnId,
|
|
532
|
+
pendingMessages: messageState.pendingMessages,
|
|
533
|
+
pendingItemId: messageState.pendingItemId,
|
|
534
|
+
totalBytes: messageState.totalBytes,
|
|
535
|
+
linesAdded: messageState.linesAdded,
|
|
536
|
+
linesRemoved: messageState.linesRemoved,
|
|
537
|
+
supportsAgentMessages: messageState.supportsAgentMessages
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
function parseContent(content = []) {
|
|
541
|
+
const textParts = [];
|
|
542
|
+
const attachments = [];
|
|
543
|
+
if (!Array.isArray(content)) {
|
|
544
|
+
return { text: "", attachments };
|
|
545
|
+
}
|
|
546
|
+
for (const item of content) {
|
|
547
|
+
if (item == null || typeof item !== "object") {
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
const typedItem = item;
|
|
551
|
+
if (typedItem.type === "text" && typedItem.text?.trim()) {
|
|
552
|
+
textParts.push(typedItem.text);
|
|
553
|
+
} else if (typedItem.type === "file" && typedItem.url?.trim()) {
|
|
554
|
+
attachments.push({
|
|
555
|
+
url: typedItem.url.trim(),
|
|
556
|
+
name: normalizeString(typedItem.name)
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return { text: textParts.join("\n\n"), attachments };
|
|
561
|
+
}
|
|
562
|
+
function normalizeString(value) {
|
|
563
|
+
const normalized = value?.trim();
|
|
564
|
+
return normalized ? normalized : void 0;
|
|
565
|
+
}
|
|
566
|
+
function positiveIntValue(value) {
|
|
567
|
+
const parsed = intValue(value);
|
|
568
|
+
return parsed != null && parsed > 0 ? parsed : void 0;
|
|
569
|
+
}
|
|
570
|
+
function nonNegativeIntValue(value) {
|
|
571
|
+
const parsed = intValue(value);
|
|
572
|
+
return parsed != null && parsed >= 0 ? parsed : void 0;
|
|
573
|
+
}
|
|
574
|
+
function intValue(value) {
|
|
575
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
576
|
+
return Math.trunc(value);
|
|
577
|
+
}
|
|
578
|
+
if (typeof value === "bigint") {
|
|
579
|
+
return Number(value);
|
|
580
|
+
}
|
|
581
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
582
|
+
const parsed = Number.parseInt(value.trim(), 10);
|
|
583
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
584
|
+
}
|
|
585
|
+
return void 0;
|
|
586
|
+
}
|
|
587
|
+
function parseDate(value) {
|
|
588
|
+
const normalized = normalizeString(value);
|
|
589
|
+
if (normalized == null) {
|
|
590
|
+
return void 0;
|
|
591
|
+
}
|
|
592
|
+
const parsed = new Date(normalized);
|
|
593
|
+
return Number.isNaN(parsed.getTime()) ? void 0 : parsed;
|
|
594
|
+
}
|
|
595
|
+
function statusEquals(left, right) {
|
|
596
|
+
return left != null && left.text === right.text && left.mode === right.mode && left.turnId === right.turnId && left.pendingItemId === right.pendingItemId && left.totalBytes === right.totalBytes && left.totalBytesFromStatus === right.totalBytesFromStatus && left.linesAdded === right.linesAdded && left.linesRemoved === right.linesRemoved && left.startedAt?.getTime() === right.startedAt?.getTime();
|
|
597
|
+
}
|
|
598
|
+
function isAgentThreadMessage(message) {
|
|
599
|
+
return typeof message.threadId === "string";
|
|
600
|
+
}
|
|
601
|
+
function attachmentsEqual(left, right) {
|
|
602
|
+
return left.length === right.length && left.every((attachment, index) => {
|
|
603
|
+
const other = right[index];
|
|
604
|
+
return attachment.url === other?.url && attachment.name === other.name;
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
export {
|
|
608
|
+
AgentThreadMessageStatusStore,
|
|
609
|
+
ChatThreadStatusState,
|
|
610
|
+
PendingAgentMessage,
|
|
611
|
+
resolveChatThreadStatusFromStore,
|
|
612
|
+
shouldShowChatThreadStatus,
|
|
613
|
+
trackAgentThreadStatusMessageInStore
|
|
614
|
+
};
|