@crestapps/ai-chat-ui 2.0.0-preview.250 → 2.0.0-preview.258
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/ai-chat.css +96 -0
- package/dist/ai-chat.js +548 -3
- package/dist/ai-chat.js.map +1 -1
- package/dist/ai-chat.min.css +1 -1
- package/dist/ai-chat.min.js +7 -5
- package/dist/ai-chat.min.js.map +1 -1
- package/dist/chat-widget.css +95 -0
- package/dist/chat-widget.min.css +1 -1
- package/package.json +1 -1
package/dist/ai-chat.js
CHANGED
|
@@ -4,6 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
(function() {
|
|
6
6
|
//#region src/Resources/CrestApps.AI.Resources/Assets/js/ai-chat.js
|
|
7
|
+
window.crestAppsAGUI = (function(registry) {
|
|
8
|
+
registry._contextProviders = Array.isArray(registry._contextProviders) ? registry._contextProviders : [];
|
|
9
|
+
registry._stateProvider = registry._stateProvider || null;
|
|
10
|
+
registry.registerContextProvider = function(provider) {
|
|
11
|
+
if (typeof provider === "function") registry._contextProviders.push(provider);
|
|
12
|
+
};
|
|
13
|
+
registry.registerStateProvider = function(provider) {
|
|
14
|
+
registry._stateProvider = provider && typeof provider === "object" ? provider : null;
|
|
15
|
+
};
|
|
16
|
+
return registry;
|
|
17
|
+
})(window.crestAppsAGUI || {});
|
|
7
18
|
window.coreAIChatManager = function() {
|
|
8
19
|
var defaultConfig = {
|
|
9
20
|
generatedImageAltText: "Generated Image",
|
|
@@ -13,6 +24,21 @@
|
|
|
13
24
|
downloadChartButtonText: "Download",
|
|
14
25
|
userLabel: "You",
|
|
15
26
|
assistantLabel: "Assistant",
|
|
27
|
+
protocol: "agui",
|
|
28
|
+
sendPageContext: true,
|
|
29
|
+
aguiThinkingLabel: "Thinking",
|
|
30
|
+
aguiInputLabel: "Input",
|
|
31
|
+
aguiResultLabel: "Result",
|
|
32
|
+
aguiApprovalPrompt: "Allow this action?",
|
|
33
|
+
aguiAllowLabel: "Allow",
|
|
34
|
+
aguiDenyLabel: "Deny",
|
|
35
|
+
aguiAllowedLabel: "Allowed",
|
|
36
|
+
aguiDeniedLabel: "Denied",
|
|
37
|
+
aguiWaitingLabel: "Waiting for your answer",
|
|
38
|
+
aguiRunningLabel: "Running",
|
|
39
|
+
aguiCurrentPageLabel: "Current page",
|
|
40
|
+
aguiSelectedTextLabel: "Text the user selected on the page",
|
|
41
|
+
aguiToolLabels: {},
|
|
16
42
|
assistantAvatarUrl: null,
|
|
17
43
|
thumbsUpTitle: "Thumbs up",
|
|
18
44
|
thumbsDownTitle: "Thumbs down",
|
|
@@ -287,9 +313,150 @@
|
|
|
287
313
|
message.references = messageReferences;
|
|
288
314
|
message.citationReferences = citationDisplay.citations;
|
|
289
315
|
message.copyContent = buildCopyContent(splitCombinedCitations(displayContent), citationDisplay.citations);
|
|
290
|
-
message.htmlContent = parseMarkdownContent(citationDisplay.content, message);
|
|
316
|
+
message.htmlContent = buildActivityHtml(message) + parseMarkdownContent(citationDisplay.content, message);
|
|
291
317
|
return message;
|
|
292
318
|
}
|
|
319
|
+
const aguiRegistry = window.crestAppsAGUI;
|
|
320
|
+
const aguiDefaultToolLabels = {
|
|
321
|
+
page_get_info: "Reading the page",
|
|
322
|
+
page_read_content: "Reading the page's content",
|
|
323
|
+
page_list_links: "Looking at the page's links",
|
|
324
|
+
page_navigate: "Opening a page",
|
|
325
|
+
page_list_form_fields: "Reading the form",
|
|
326
|
+
page_set_form_field: "Filling in a field",
|
|
327
|
+
update_shared_state: "Updating what you share"
|
|
328
|
+
};
|
|
329
|
+
const aguiToolCallStatuses = [
|
|
330
|
+
"requested",
|
|
331
|
+
"completed",
|
|
332
|
+
"awaitingApproval",
|
|
333
|
+
"approved",
|
|
334
|
+
"rejected",
|
|
335
|
+
"awaitingClient"
|
|
336
|
+
];
|
|
337
|
+
function escapeHtmlText(text) {
|
|
338
|
+
return String(text ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
339
|
+
}
|
|
340
|
+
function normalizeToolCallStatus(status) {
|
|
341
|
+
if (typeof status === "number") return aguiToolCallStatuses[status] || "requested";
|
|
342
|
+
if (typeof status === "string" && status) return status.charAt(0).toLowerCase() + status.slice(1);
|
|
343
|
+
return "requested";
|
|
344
|
+
}
|
|
345
|
+
function normalizeToolCalls(toolCalls) {
|
|
346
|
+
if (!Array.isArray(toolCalls)) return [];
|
|
347
|
+
return toolCalls.filter((call) => call && (call.callId || call.id)).map((call) => ({
|
|
348
|
+
id: call.callId || call.id,
|
|
349
|
+
name: call.name || "",
|
|
350
|
+
arguments: call.arguments || "",
|
|
351
|
+
result: call.result || "",
|
|
352
|
+
status: normalizeToolCallStatus(call.status),
|
|
353
|
+
interruptId: call.interruptId || call.approvalRequestId || null
|
|
354
|
+
}));
|
|
355
|
+
}
|
|
356
|
+
function parseToolArguments(text) {
|
|
357
|
+
if (!text) return null;
|
|
358
|
+
try {
|
|
359
|
+
return JSON.parse(text);
|
|
360
|
+
} catch {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
function describeToolCall(call) {
|
|
365
|
+
let label = Object.assign({}, aguiDefaultToolLabels, defaultConfig.aguiToolLabels || {})[call.name];
|
|
366
|
+
if (!label) {
|
|
367
|
+
label = String(call.name || "tool").replace(/[_.-]+/g, " ");
|
|
368
|
+
label = label.charAt(0).toUpperCase() + label.slice(1);
|
|
369
|
+
}
|
|
370
|
+
const args = parseToolArguments(call.arguments);
|
|
371
|
+
if (args && call.name === "page_set_form_field" && args.field) label += ": " + args.field;
|
|
372
|
+
else if (args && call.name === "page_navigate" && args.url) label += ": " + args.url;
|
|
373
|
+
return label;
|
|
374
|
+
}
|
|
375
|
+
function formatToolText(text, maxLength) {
|
|
376
|
+
const parsed = parseToolArguments(text);
|
|
377
|
+
let value = parsed !== null && typeof parsed === "object" ? JSON.stringify(parsed, null, 2) : String(text ?? "");
|
|
378
|
+
if (value.length > maxLength) value = value.slice(0, maxLength) + "…";
|
|
379
|
+
return value;
|
|
380
|
+
}
|
|
381
|
+
function buildActivityHtml(message) {
|
|
382
|
+
const toolCalls = Array.isArray(message.toolCalls) ? message.toolCalls : [];
|
|
383
|
+
const parts = [];
|
|
384
|
+
if (message.reasoning) parts.push("<details class=\"ai-chat-agui-reasoning\"><summary>" + escapeHtmlText(defaultConfig.aguiThinkingLabel) + "</summary><div class=\"ai-chat-agui-reasoning-text\">" + escapeHtmlText(message.reasoning) + "</div></details>");
|
|
385
|
+
for (const call of toolCalls) {
|
|
386
|
+
const status = call.status || "requested";
|
|
387
|
+
let icon = "fa-solid fa-circle-notch fa-spin";
|
|
388
|
+
let state = defaultConfig.aguiRunningLabel;
|
|
389
|
+
if (status === "completed" || status === "approved") {
|
|
390
|
+
icon = "fa-solid fa-check";
|
|
391
|
+
state = status === "approved" ? defaultConfig.aguiAllowedLabel : "";
|
|
392
|
+
} else if (status === "rejected") {
|
|
393
|
+
icon = "fa-solid fa-ban";
|
|
394
|
+
state = defaultConfig.aguiDeniedLabel;
|
|
395
|
+
} else if (status === "awaitingApproval") {
|
|
396
|
+
icon = "fa-solid fa-hand";
|
|
397
|
+
state = defaultConfig.aguiWaitingLabel;
|
|
398
|
+
}
|
|
399
|
+
let details = "";
|
|
400
|
+
if (call.arguments && call.arguments !== "{}") details += "<div class=\"ai-chat-agui-tool-section\"><div class=\"ai-chat-agui-tool-section-label\">" + escapeHtmlText(defaultConfig.aguiInputLabel) + "</div><pre>" + escapeHtmlText(formatToolText(call.arguments, 2e3)) + "</pre></div>";
|
|
401
|
+
if (call.result) details += "<div class=\"ai-chat-agui-tool-section\"><div class=\"ai-chat-agui-tool-section-label\">" + escapeHtmlText(defaultConfig.aguiResultLabel) + "</div><pre>" + escapeHtmlText(formatToolText(call.result, 2e3)) + "</pre></div>";
|
|
402
|
+
const summary = "<i class=\"" + icon + "\" aria-hidden=\"true\"></i> <span class=\"ai-chat-agui-tool-name\">" + escapeHtmlText(describeToolCall(call)) + "</span>" + (state ? " <span class=\"ai-chat-agui-tool-state\">" + escapeHtmlText(state) + "</span>" : "");
|
|
403
|
+
let html = "<div class=\"ai-chat-agui-tool ai-chat-agui-tool-" + escapeHtmlText(status) + "\" data-agui-tool-call=\"" + escapeHtmlText(call.id) + "\">";
|
|
404
|
+
html += details ? "<details><summary>" + summary + "</summary>" + details + "</details>" : "<div class=\"ai-chat-agui-tool-summary\">" + summary + "</div>";
|
|
405
|
+
if (status === "awaitingApproval" && call.interruptId) html += "<div class=\"ai-chat-agui-approval\"><span>" + escapeHtmlText(defaultConfig.aguiApprovalPrompt) + "</span><button type=\"button\" class=\"btn btn-sm btn-primary\" data-agui-interrupt=\"" + escapeHtmlText(call.interruptId) + "\" data-agui-approved=\"true\">" + escapeHtmlText(defaultConfig.aguiAllowLabel) + "</button><button type=\"button\" class=\"btn btn-sm btn-outline-secondary\" data-agui-interrupt=\"" + escapeHtmlText(call.interruptId) + "\" data-agui-approved=\"false\">" + escapeHtmlText(defaultConfig.aguiDenyLabel) + "</button></div>";
|
|
406
|
+
html += "</div>";
|
|
407
|
+
parts.push(html);
|
|
408
|
+
}
|
|
409
|
+
if (!parts.length) return "";
|
|
410
|
+
return DOMPurify.sanitize("<div class=\"ai-chat-agui-activity\">" + parts.join("") + "</div>");
|
|
411
|
+
}
|
|
412
|
+
function cloneJson(value) {
|
|
413
|
+
return value === void 0 ? void 0 : JSON.parse(JSON.stringify(value));
|
|
414
|
+
}
|
|
415
|
+
function applyJsonPatch(document, operations) {
|
|
416
|
+
let result = cloneJson(document) ?? {};
|
|
417
|
+
for (const operation of operations || []) {
|
|
418
|
+
if (!operation || typeof operation.path !== "string") continue;
|
|
419
|
+
if (operation.path === "") {
|
|
420
|
+
if (operation.op === "replace" && operation.value && typeof operation.value === "object") result = cloneJson(operation.value);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
const segments = operation.path.slice(1).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
424
|
+
const key = segments.pop();
|
|
425
|
+
let parent = result;
|
|
426
|
+
for (const segment of segments) parent = parent == null ? void 0 : parent[Array.isArray(parent) ? Number(segment) : segment];
|
|
427
|
+
if (parent == null || typeof parent !== "object") continue;
|
|
428
|
+
if (Array.isArray(parent)) {
|
|
429
|
+
const index = key === "-" ? parent.length : Number(key);
|
|
430
|
+
if (operation.op === "add") parent.splice(index, 0, cloneJson(operation.value));
|
|
431
|
+
else if (operation.op === "replace") parent[index] = cloneJson(operation.value);
|
|
432
|
+
else if (operation.op === "remove") parent.splice(index, 1);
|
|
433
|
+
} else if (operation.op === "remove") delete parent[key];
|
|
434
|
+
else if (operation.op === "add" || operation.op === "replace") parent[key] = cloneJson(operation.value);
|
|
435
|
+
}
|
|
436
|
+
return result;
|
|
437
|
+
}
|
|
438
|
+
function newAGUIId() {
|
|
439
|
+
if (window.crypto && typeof window.crypto.randomUUID === "function") return window.crypto.randomUUID().replace(/-/g, "");
|
|
440
|
+
return Date.now().toString(36) + Math.random().toString(36).slice(2);
|
|
441
|
+
}
|
|
442
|
+
const PAGE_NAVIGATE_TOOL_NAME = "page_navigate";
|
|
443
|
+
const CONTINUE_RUN_STORAGE_KEY = "crestapps-agui-continue-run";
|
|
444
|
+
const CONTINUE_RUN_MAX_WAIT_MS = 12e4;
|
|
445
|
+
const MAX_CONTINUE_RUN_DEPTH = 5;
|
|
446
|
+
function readContinueRunMarker() {
|
|
447
|
+
try {
|
|
448
|
+
const value = JSON.parse(window.sessionStorage.getItem(CONTINUE_RUN_STORAGE_KEY) || "null");
|
|
449
|
+
return value && typeof value.sessionId === "string" ? value : null;
|
|
450
|
+
} catch (e) {
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function writeContinueRunMarker(marker) {
|
|
455
|
+
try {
|
|
456
|
+
if (marker) window.sessionStorage.setItem(CONTINUE_RUN_STORAGE_KEY, JSON.stringify(marker));
|
|
457
|
+
else window.sessionStorage.removeItem(CONTINUE_RUN_STORAGE_KEY);
|
|
458
|
+
} catch (e) {}
|
|
459
|
+
}
|
|
293
460
|
const renderer = new marked.Renderer();
|
|
294
461
|
renderer.link = function(data) {
|
|
295
462
|
var href = sanitizeUrl(data.href);
|
|
@@ -499,6 +666,10 @@
|
|
|
499
666
|
autoScroll: true,
|
|
500
667
|
stream: null,
|
|
501
668
|
pageTools: null,
|
|
669
|
+
aguiRun: null,
|
|
670
|
+
aguiDisabled: false,
|
|
671
|
+
sharedStateCache: null,
|
|
672
|
+
lastPageSelection: null,
|
|
502
673
|
messages: [],
|
|
503
674
|
notifications: [],
|
|
504
675
|
prompt: "",
|
|
@@ -880,6 +1051,7 @@
|
|
|
880
1051
|
this.connection.serverTimeoutInMilliseconds = 6e5;
|
|
881
1052
|
this.connection.keepAliveIntervalInMilliseconds = 15e3;
|
|
882
1053
|
this.connection.on("LoadSession", (data) => {
|
|
1054
|
+
if (this.stream && data && data.sessionId === this.getSessionId()) return;
|
|
883
1055
|
this.forceStopActiveVoice();
|
|
884
1056
|
this.initializeSession(data.sessionId, true);
|
|
885
1057
|
this.messages = [];
|
|
@@ -898,6 +1070,7 @@
|
|
|
898
1070
|
role: "assistant",
|
|
899
1071
|
content: config.initialPrompt
|
|
900
1072
|
});
|
|
1073
|
+
this.continueRunOnThisPage(data);
|
|
901
1074
|
});
|
|
902
1075
|
this.connection.on("ReceiveError", (error) => {
|
|
903
1076
|
console.error("SignalR Error: ", error);
|
|
@@ -1125,8 +1298,9 @@
|
|
|
1125
1298
|
},
|
|
1126
1299
|
addMessage(message) {
|
|
1127
1300
|
if (message.userRating === void 0) message.userRating = null;
|
|
1128
|
-
if (message.
|
|
1129
|
-
|
|
1301
|
+
if (message.role === "assistant" && message.toolCalls) message.toolCalls = normalizeToolCalls(message.toolCalls);
|
|
1302
|
+
if (message.content || message.toolCalls && message.toolCalls.length) {
|
|
1303
|
+
message.rawContent = message.rawContent ?? message.content ?? "";
|
|
1130
1304
|
updateMessagePresentation(message, message.references);
|
|
1131
1305
|
}
|
|
1132
1306
|
this.addMessageInternal(message);
|
|
@@ -1331,6 +1505,10 @@
|
|
|
1331
1505
|
}
|
|
1332
1506
|
},
|
|
1333
1507
|
streamMessage(profileId, trimmedPrompt, sessionProfileId) {
|
|
1508
|
+
if ((config.protocol || "agui") === "agui" && !this.aguiDisabled) {
|
|
1509
|
+
this.streamAGUIRun(profileId, trimmedPrompt, sessionProfileId, null);
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1334
1512
|
if (this.stream) {
|
|
1335
1513
|
this.stream.dispose();
|
|
1336
1514
|
this.stream = null;
|
|
@@ -1403,6 +1581,366 @@
|
|
|
1403
1581
|
};
|
|
1404
1582
|
this.syncPageTools().then(openStream, openStream);
|
|
1405
1583
|
},
|
|
1584
|
+
collectRunContext() {
|
|
1585
|
+
const context = [];
|
|
1586
|
+
if (config.sendPageContext === false) return context;
|
|
1587
|
+
try {
|
|
1588
|
+
context.push({
|
|
1589
|
+
description: config.aguiCurrentPageLabel,
|
|
1590
|
+
value: (document.title ? document.title + " " : "") + "(" + window.location.pathname + window.location.search + ")"
|
|
1591
|
+
});
|
|
1592
|
+
const selection = this.lastPageSelection;
|
|
1593
|
+
if (selection && selection.text && Date.now() - selection.at < 3e5) context.push({
|
|
1594
|
+
description: config.aguiSelectedTextLabel,
|
|
1595
|
+
value: selection.text.slice(0, 1e3)
|
|
1596
|
+
});
|
|
1597
|
+
} catch (e) {
|
|
1598
|
+
console.warn("The page context could not be read.", e);
|
|
1599
|
+
}
|
|
1600
|
+
for (const provider of aguiRegistry._contextProviders) try {
|
|
1601
|
+
const entries = [].concat(provider() || []);
|
|
1602
|
+
for (const entry of entries) if (entry && entry.value) context.push({
|
|
1603
|
+
description: String(entry.description || "Detail"),
|
|
1604
|
+
value: typeof entry.value === "string" ? entry.value : JSON.stringify(entry.value)
|
|
1605
|
+
});
|
|
1606
|
+
} catch (e) {
|
|
1607
|
+
console.warn("An AG-UI context provider failed.", e);
|
|
1608
|
+
}
|
|
1609
|
+
return context.slice(0, 20);
|
|
1610
|
+
},
|
|
1611
|
+
trackPageSelection() {
|
|
1612
|
+
try {
|
|
1613
|
+
const selection = window.getSelection ? window.getSelection() : null;
|
|
1614
|
+
const text = selection ? String(selection).trim() : "";
|
|
1615
|
+
if (!text) return;
|
|
1616
|
+
if (this.chatContainer && selection.anchorNode && this.chatContainer.contains(selection.anchorNode)) return;
|
|
1617
|
+
this.lastPageSelection = {
|
|
1618
|
+
text,
|
|
1619
|
+
at: Date.now()
|
|
1620
|
+
};
|
|
1621
|
+
} catch {}
|
|
1622
|
+
},
|
|
1623
|
+
getSharedState() {
|
|
1624
|
+
const provider = aguiRegistry._stateProvider;
|
|
1625
|
+
if (!provider || typeof provider.get !== "function") return;
|
|
1626
|
+
try {
|
|
1627
|
+
const state = provider.get();
|
|
1628
|
+
if (state && typeof state === "object" && !Array.isArray(state)) {
|
|
1629
|
+
this.sharedStateCache = cloneJson(state);
|
|
1630
|
+
return state;
|
|
1631
|
+
}
|
|
1632
|
+
} catch (e) {
|
|
1633
|
+
console.warn("The AG-UI state provider failed.", e);
|
|
1634
|
+
}
|
|
1635
|
+
},
|
|
1636
|
+
applySharedState(evt) {
|
|
1637
|
+
const delta = evt.type === "STATE_DELTA" ? evt.delta || [] : null;
|
|
1638
|
+
const state = delta ? applyJsonPatch(this.sharedStateCache ?? {}, delta) : cloneJson(evt.snapshot ?? {});
|
|
1639
|
+
this.sharedStateCache = state;
|
|
1640
|
+
const provider = aguiRegistry._stateProvider;
|
|
1641
|
+
if (provider && typeof provider.apply === "function") try {
|
|
1642
|
+
provider.apply(cloneJson(state), delta);
|
|
1643
|
+
} catch (e) {
|
|
1644
|
+
console.warn("The AG-UI state provider could not apply the state.", e);
|
|
1645
|
+
}
|
|
1646
|
+
document.dispatchEvent(new CustomEvent("crestapps:agui-state", { detail: {
|
|
1647
|
+
type: evt.type,
|
|
1648
|
+
state: cloneJson(state),
|
|
1649
|
+
delta
|
|
1650
|
+
} }));
|
|
1651
|
+
},
|
|
1652
|
+
ensureAGUIMessage(run, messageId) {
|
|
1653
|
+
if (run.message) return run.message;
|
|
1654
|
+
this.hideTypingIndicator();
|
|
1655
|
+
this.messages.push({
|
|
1656
|
+
id: messageId || run.assistantMessageId || newAGUIId(),
|
|
1657
|
+
role: "assistant",
|
|
1658
|
+
title: run.title,
|
|
1659
|
+
content: "",
|
|
1660
|
+
htmlContent: "",
|
|
1661
|
+
isStreaming: true,
|
|
1662
|
+
userRating: null,
|
|
1663
|
+
toolCalls: [],
|
|
1664
|
+
reasoning: "",
|
|
1665
|
+
appearance: run.appearance
|
|
1666
|
+
});
|
|
1667
|
+
run.messageIndex = this.messages.length - 1;
|
|
1668
|
+
run.message = this.messages[run.messageIndex];
|
|
1669
|
+
return run.message;
|
|
1670
|
+
},
|
|
1671
|
+
refreshAGUIMessage(run) {
|
|
1672
|
+
const message = run.message;
|
|
1673
|
+
if (!message) return;
|
|
1674
|
+
message.rawContent = run.content;
|
|
1675
|
+
updateMessagePresentation(message, run.references);
|
|
1676
|
+
this.messages[run.messageIndex] = message;
|
|
1677
|
+
this.$nextTick(() => {
|
|
1678
|
+
renderMessageMedia(message);
|
|
1679
|
+
this.scrollToBottom();
|
|
1680
|
+
});
|
|
1681
|
+
},
|
|
1682
|
+
handleAGUIEvent(run, evt) {
|
|
1683
|
+
if (!evt || typeof evt.type !== "string") return;
|
|
1684
|
+
switch (evt.type) {
|
|
1685
|
+
case "RUN_STARTED":
|
|
1686
|
+
if (evt.threadId && evt.threadId !== run.threadId && evt.threadId !== this.getSessionId()) this.initializeSession(evt.threadId, true);
|
|
1687
|
+
break;
|
|
1688
|
+
case "CUSTOM":
|
|
1689
|
+
this.handleAGUICustomEvent(run, evt.name, evt.value || {});
|
|
1690
|
+
break;
|
|
1691
|
+
case "TEXT_MESSAGE_START":
|
|
1692
|
+
this.ensureAGUIMessage(run, evt.messageId);
|
|
1693
|
+
if (run.textMessageId && run.textMessageId !== evt.messageId && run.content) run.content += "\n\n";
|
|
1694
|
+
run.textMessageId = evt.messageId;
|
|
1695
|
+
break;
|
|
1696
|
+
case "TEXT_MESSAGE_CONTENT":
|
|
1697
|
+
this.ensureAGUIMessage(run, evt.messageId);
|
|
1698
|
+
run.content += evt.delta || "";
|
|
1699
|
+
this.refreshAGUIMessage(run);
|
|
1700
|
+
break;
|
|
1701
|
+
case "REASONING_MESSAGE_CONTENT": {
|
|
1702
|
+
const message = this.ensureAGUIMessage(run, run.assistantMessageId);
|
|
1703
|
+
message.reasoning = (message.reasoning || "") + (evt.delta || "");
|
|
1704
|
+
this.refreshAGUIMessage(run);
|
|
1705
|
+
break;
|
|
1706
|
+
}
|
|
1707
|
+
case "TOOL_CALL_START": {
|
|
1708
|
+
const message = this.ensureAGUIMessage(run, evt.parentMessageId);
|
|
1709
|
+
const call = {
|
|
1710
|
+
id: evt.toolCallId,
|
|
1711
|
+
name: evt.toolCallName,
|
|
1712
|
+
arguments: "",
|
|
1713
|
+
result: "",
|
|
1714
|
+
status: "requested",
|
|
1715
|
+
interruptId: null
|
|
1716
|
+
};
|
|
1717
|
+
message.toolCalls = message.toolCalls || [];
|
|
1718
|
+
message.toolCalls.push(call);
|
|
1719
|
+
run.toolCalls[evt.toolCallId] = message.toolCalls[message.toolCalls.length - 1];
|
|
1720
|
+
this.refreshAGUIMessage(run);
|
|
1721
|
+
if (evt.toolCallName === PAGE_NAVIGATE_TOOL_NAME) this.markRunContinuesOnNextPage(run.threadId || this.getSessionId());
|
|
1722
|
+
break;
|
|
1723
|
+
}
|
|
1724
|
+
case "TOOL_CALL_ARGS": {
|
|
1725
|
+
const call = run.toolCalls[evt.toolCallId];
|
|
1726
|
+
if (call) call.arguments += evt.delta || "";
|
|
1727
|
+
break;
|
|
1728
|
+
}
|
|
1729
|
+
case "TOOL_CALL_END":
|
|
1730
|
+
this.refreshAGUIMessage(run);
|
|
1731
|
+
break;
|
|
1732
|
+
case "TOOL_CALL_RESULT": {
|
|
1733
|
+
const call = run.toolCalls[evt.toolCallId];
|
|
1734
|
+
if (call) {
|
|
1735
|
+
call.result = typeof evt.content === "string" ? evt.content : JSON.stringify(evt.content ?? "");
|
|
1736
|
+
call.status = run.rejectedCallIds.has(evt.toolCallId) ? "rejected" : "completed";
|
|
1737
|
+
this.refreshAGUIMessage(run);
|
|
1738
|
+
}
|
|
1739
|
+
break;
|
|
1740
|
+
}
|
|
1741
|
+
case "STATE_SNAPSHOT":
|
|
1742
|
+
case "STATE_DELTA":
|
|
1743
|
+
this.applySharedState(evt);
|
|
1744
|
+
break;
|
|
1745
|
+
case "RUN_FINISHED": {
|
|
1746
|
+
const outcome = evt.outcome || {};
|
|
1747
|
+
if (outcome.type === "interrupt" && Array.isArray(outcome.interrupts)) {
|
|
1748
|
+
for (const interrupt of outcome.interrupts) {
|
|
1749
|
+
const call = interrupt && run.toolCalls[interrupt.toolCallId];
|
|
1750
|
+
if (call) {
|
|
1751
|
+
call.status = "awaitingApproval";
|
|
1752
|
+
call.interruptId = interrupt.id;
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
this.refreshAGUIMessage(run);
|
|
1756
|
+
}
|
|
1757
|
+
break;
|
|
1758
|
+
}
|
|
1759
|
+
case "RUN_ERROR":
|
|
1760
|
+
run.error = evt.message || "";
|
|
1761
|
+
this.hideTypingIndicator();
|
|
1762
|
+
if (evt.code === "session_start_rejected") this.rejectPendingSessionRequest(evt.message);
|
|
1763
|
+
if (!this.isNavigatingAway) this.addMessage(this.getErrorMessage(evt.message));
|
|
1764
|
+
}
|
|
1765
|
+
},
|
|
1766
|
+
handleAGUICustomEvent(run, name, value) {
|
|
1767
|
+
switch (name) {
|
|
1768
|
+
case "crestapps.assistant_message":
|
|
1769
|
+
run.assistantMessageId = value.messageId;
|
|
1770
|
+
run.title = value.title;
|
|
1771
|
+
run.appearance = this.normalizeAssistantAppearance(value.appearance);
|
|
1772
|
+
if (run.message) {
|
|
1773
|
+
run.message.title = run.title;
|
|
1774
|
+
run.message.appearance = run.appearance;
|
|
1775
|
+
}
|
|
1776
|
+
break;
|
|
1777
|
+
case "crestapps.references":
|
|
1778
|
+
for (const [key, reference] of Object.entries(value.references || {})) run.references[key] = normalizeReference(reference) ?? {};
|
|
1779
|
+
this.refreshAGUIMessage(run);
|
|
1780
|
+
break;
|
|
1781
|
+
case "crestapps.message_replaced":
|
|
1782
|
+
this.ensureAGUIMessage(run, value.messageId);
|
|
1783
|
+
run.content = value.content || "";
|
|
1784
|
+
this.refreshAGUIMessage(run);
|
|
1785
|
+
break;
|
|
1786
|
+
case "crestapps.tool_calls_resolved": {
|
|
1787
|
+
const message = this.messages.find((m) => m.id === value.messageId);
|
|
1788
|
+
for (const call of normalizeToolCalls(value.toolCalls)) if (call.status === "rejected") run.rejectedCallIds.add(call.id);
|
|
1789
|
+
if (message) {
|
|
1790
|
+
message.toolCalls = normalizeToolCalls(value.toolCalls);
|
|
1791
|
+
message.rawContent = message.rawContent ?? message.content ?? "";
|
|
1792
|
+
updateMessagePresentation(message, message.references);
|
|
1793
|
+
}
|
|
1794
|
+
break;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
},
|
|
1798
|
+
finishAGUIRun(run) {
|
|
1799
|
+
if (run.finished) return;
|
|
1800
|
+
run.finished = true;
|
|
1801
|
+
if (run.message) {
|
|
1802
|
+
this.processReferences(run.references, run.messageIndex);
|
|
1803
|
+
run.message.isStreaming = false;
|
|
1804
|
+
} else this.hideTypingIndicator();
|
|
1805
|
+
this.streamingFinished();
|
|
1806
|
+
if (this.isConversationMode && this.textToSpeechEnabled && run.message && run.message.content) this.synthesizeSpeech(run.message.content);
|
|
1807
|
+
this.stream?.dispose();
|
|
1808
|
+
this.stream = null;
|
|
1809
|
+
if (this.aguiRun === run) this.aguiRun = null;
|
|
1810
|
+
},
|
|
1811
|
+
streamAGUIRun(profileId, prompt, sessionProfileId, resume, options) {
|
|
1812
|
+
const continueRun = !!(options && options.continueRun);
|
|
1813
|
+
if (prompt) writeContinueRunMarker(null);
|
|
1814
|
+
if (this.stream) {
|
|
1815
|
+
this.stream.dispose();
|
|
1816
|
+
this.stream = null;
|
|
1817
|
+
}
|
|
1818
|
+
this.streamingStarted();
|
|
1819
|
+
this.showTypingIndicator();
|
|
1820
|
+
this.autoScroll = true;
|
|
1821
|
+
const run = {
|
|
1822
|
+
threadId: "",
|
|
1823
|
+
message: null,
|
|
1824
|
+
messageIndex: -1,
|
|
1825
|
+
assistantMessageId: null,
|
|
1826
|
+
title: null,
|
|
1827
|
+
appearance: null,
|
|
1828
|
+
content: "",
|
|
1829
|
+
references: {},
|
|
1830
|
+
textMessageId: null,
|
|
1831
|
+
toolCalls: {},
|
|
1832
|
+
rejectedCallIds: /* @__PURE__ */ new Set(),
|
|
1833
|
+
finished: false,
|
|
1834
|
+
error: null
|
|
1835
|
+
};
|
|
1836
|
+
this.aguiRun = run;
|
|
1837
|
+
const openStream = () => {
|
|
1838
|
+
run.threadId = this.getSessionId() || "";
|
|
1839
|
+
const input = {
|
|
1840
|
+
threadId: run.threadId,
|
|
1841
|
+
runId: newAGUIId(),
|
|
1842
|
+
messages: prompt ? [{
|
|
1843
|
+
id: newAGUIId(),
|
|
1844
|
+
role: "user",
|
|
1845
|
+
content: prompt
|
|
1846
|
+
}] : [],
|
|
1847
|
+
tools: [],
|
|
1848
|
+
context: this.collectRunContext(),
|
|
1849
|
+
forwardedProps: {
|
|
1850
|
+
profileId,
|
|
1851
|
+
sessionProfileId: sessionProfileId || null
|
|
1852
|
+
}
|
|
1853
|
+
};
|
|
1854
|
+
if (continueRun) {
|
|
1855
|
+
input.forwardedProps.continueRun = true;
|
|
1856
|
+
input.forwardedProps.continuePage = (document.title ? document.title + " " : "") + "(" + window.location.pathname + window.location.search + ")";
|
|
1857
|
+
}
|
|
1858
|
+
const state = this.getSharedState();
|
|
1859
|
+
if (state !== void 0) input.state = state;
|
|
1860
|
+
if (resume && resume.length) input.resume = resume;
|
|
1861
|
+
this.stream = this.connection.stream("RunAgent", input).subscribe({
|
|
1862
|
+
next: (evt) => this.handleAGUIEvent(run, evt),
|
|
1863
|
+
complete: () => this.finishAGUIRun(run),
|
|
1864
|
+
error: (err) => {
|
|
1865
|
+
const errorText = String(err && err.message || err || "");
|
|
1866
|
+
if (!resume && !continueRun && !run.message && errorText.includes("Method does not exist")) {
|
|
1867
|
+
this.aguiDisabled = true;
|
|
1868
|
+
this.stream = null;
|
|
1869
|
+
this.hideTypingIndicator();
|
|
1870
|
+
this.streamingFinished();
|
|
1871
|
+
this.streamMessage(profileId, prompt, sessionProfileId);
|
|
1872
|
+
return;
|
|
1873
|
+
}
|
|
1874
|
+
this.finishAGUIRun(run);
|
|
1875
|
+
if (!this.isNavigatingAway) this.addMessage(this.getServiceDownMessage());
|
|
1876
|
+
console.error("Stream error:", err);
|
|
1877
|
+
}
|
|
1878
|
+
});
|
|
1879
|
+
};
|
|
1880
|
+
(this.pendingSessionPromise ? this.pendingSessionPromise.catch(() => null) : Promise.resolve()).then(() => this.syncPageTools()).then(openStream, openStream);
|
|
1881
|
+
},
|
|
1882
|
+
answerApproval(interruptId, approved) {
|
|
1883
|
+
let target = null;
|
|
1884
|
+
let targetCall = null;
|
|
1885
|
+
for (const message of this.messages) {
|
|
1886
|
+
const call = (message.toolCalls || []).find((c) => c.interruptId === interruptId);
|
|
1887
|
+
if (call) {
|
|
1888
|
+
target = message;
|
|
1889
|
+
targetCall = call;
|
|
1890
|
+
break;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
if (!targetCall || targetCall.status !== "awaitingApproval" || this.stream) return;
|
|
1894
|
+
targetCall.status = approved ? "approved" : "rejected";
|
|
1895
|
+
targetCall.decided = true;
|
|
1896
|
+
target.rawContent = target.rawContent ?? target.content ?? "";
|
|
1897
|
+
updateMessagePresentation(target, target.references);
|
|
1898
|
+
const calls = target.toolCalls.filter((c) => c.interruptId);
|
|
1899
|
+
if (calls.some((c) => c.status === "awaitingApproval")) return;
|
|
1900
|
+
const resume = calls.filter((c) => c.decided).map((c) => ({
|
|
1901
|
+
interruptId: c.interruptId,
|
|
1902
|
+
status: "resolved",
|
|
1903
|
+
payload: { approved: c.status === "approved" }
|
|
1904
|
+
}));
|
|
1905
|
+
this.streamAGUIRun(this.getProfileId(), null, null, resume);
|
|
1906
|
+
},
|
|
1907
|
+
markRunContinuesOnNextPage(sessionId) {
|
|
1908
|
+
if (!sessionId) return;
|
|
1909
|
+
const marker = readContinueRunMarker();
|
|
1910
|
+
writeContinueRunMarker({
|
|
1911
|
+
sessionId,
|
|
1912
|
+
depth: (marker && marker.sessionId === sessionId ? marker.depth : 0) + 1,
|
|
1913
|
+
at: Date.now()
|
|
1914
|
+
});
|
|
1915
|
+
},
|
|
1916
|
+
continueRunOnThisPage(data) {
|
|
1917
|
+
const marker = readContinueRunMarker();
|
|
1918
|
+
if (!marker || !data || marker.sessionId !== data.sessionId || this.aguiDisabled) return;
|
|
1919
|
+
if (Date.now() - marker.at > CONTINUE_RUN_MAX_WAIT_MS) {
|
|
1920
|
+
writeContinueRunMarker(null);
|
|
1921
|
+
this.hideTypingIndicator();
|
|
1922
|
+
return;
|
|
1923
|
+
}
|
|
1924
|
+
const messages = data.messages || [];
|
|
1925
|
+
const last = messages.length ? messages[messages.length - 1] : null;
|
|
1926
|
+
if (!last || last.role !== "assistant" || !last.continuesOnNextPage) {
|
|
1927
|
+
if (!last || last.role === "user") this.showTypingIndicator();
|
|
1928
|
+
else writeContinueRunMarker(null);
|
|
1929
|
+
return;
|
|
1930
|
+
}
|
|
1931
|
+
if (marker.depth > MAX_CONTINUE_RUN_DEPTH) {
|
|
1932
|
+
writeContinueRunMarker(null);
|
|
1933
|
+
return;
|
|
1934
|
+
}
|
|
1935
|
+
writeContinueRunMarker({
|
|
1936
|
+
sessionId: marker.sessionId,
|
|
1937
|
+
depth: marker.depth,
|
|
1938
|
+
at: 0
|
|
1939
|
+
});
|
|
1940
|
+
if (this.stream) return;
|
|
1941
|
+
last.continuesOnNextPage = false;
|
|
1942
|
+
this.streamAGUIRun(this.getProfileId(), null, null, null, { continueRun: true });
|
|
1943
|
+
},
|
|
1406
1944
|
getServiceDownMessage() {
|
|
1407
1945
|
return {
|
|
1408
1946
|
role: "assistant",
|
|
@@ -1976,6 +2514,13 @@
|
|
|
1976
2514
|
return false;
|
|
1977
2515
|
}
|
|
1978
2516
|
this.applyOrchestratorAvailability();
|
|
2517
|
+
this.chatContainer.addEventListener("click", (event) => {
|
|
2518
|
+
const button = event.target && event.target.closest ? event.target.closest("[data-agui-interrupt]") : null;
|
|
2519
|
+
if (!button || !this.chatContainer.contains(button)) return;
|
|
2520
|
+
event.preventDefault();
|
|
2521
|
+
this.answerApproval(button.getAttribute("data-agui-interrupt"), button.getAttribute("data-agui-approved") === "true");
|
|
2522
|
+
});
|
|
2523
|
+
document.addEventListener("selectionchange", () => this.trackPageSelection());
|
|
1979
2524
|
const sessionId = this.getSessionId();
|
|
1980
2525
|
if (!hasWidgetConfig && sessionId) this.loadSession(sessionId);
|
|
1981
2526
|
if (config.sessionDocumentsEnabled && config.documentBarSelector) {
|