@astralbeam/sdk 0.23.6 → 0.24.0
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/README.md +7 -5
- package/dist/{api-SpkUzEv3.js → api-BYkm7p8c.js} +15 -4
- package/dist/api.d.ts +2 -2
- package/dist/api.js +1 -1
- package/dist/{badge-CN5Cq8Xf.js → badge-Do9jhFNO.js} +1 -1
- package/dist/client.d.ts +6418 -46
- package/dist/client.js +51 -1
- package/dist/combobox-B6w-LeTo.js +42 -0
- package/dist/{core-Bn3w2Q44.js → core-Vz6aUEaN.js} +305 -152
- package/dist/core.d.ts +1 -1
- package/dist/core.js +1 -1
- package/dist/{index-DGfDkU_f.d.ts → index-CXzO4-3C.d.ts} +20 -4
- package/dist/{index-D8X2hDGe.d.ts → index-DF5gqYlY.d.ts} +7 -2
- package/dist/{item-Dl9F5Xpc.js → item-U0ZPJBqA.js} +1 -1
- package/dist/listings-BMUU5Ssj.js +1 -0
- package/dist/{mcp-servers-BIGSawh_.js → mcp-servers-B4IbhRo_.js} +1 -1
- package/dist/react.d.ts +4 -27
- package/dist/react.js +13 -42
- package/dist/{styles.generated-CW6iaZWa.js → styles.generated-BBScqnSk.js} +11 -11
- package/dist/widget-DxIW_E_U.js +27 -0
- package/package.json +1 -1
- package/dist/listings-UNePN1vA.js +0 -1
- package/dist/threads-CZIvwSRC.js +0 -42
- package/dist/widget-D_AwRq1A.js +0 -27
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Bt as
|
|
1
|
+
import { Bt as isAstralBeamApiError, C as getChatConfig, Ct as personalMcpBindBrowser, Dt as personalMcpGetCatalog, Et as personalMcpDisconnectConnection, Ft as updateChatThread, Ot as personalMcpSetHeaders, Rt as uploadChatFile, S as deleteChatThread, St as personalMcpAuthorizeConnection, Tt as personalMcpCreateConnection, Vt as resolveApiUrl, _t as listChatThreads, ht as listChatMessages, it as getRunChatUrl, k as getCurrentUser, kt as personalMcpTestConnection, rt as getResolveChatToolResultUrl, ut as getThreadAttachment, wt as personalMcpCompleteAuthorization, y as createChatThread, zt as astralBeamChatFetch } from "./api-BYkm7p8c.js";
|
|
2
2
|
import { _ as strictDecoder, i as isObject, l as decode, p as JWTInvalid } from "./validate-BOw-BAyv.js";
|
|
3
3
|
//#region \0rolldown/runtime.js
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -11060,6 +11060,186 @@ function createDebugLogger(enabled) {
|
|
|
11060
11060
|
};
|
|
11061
11061
|
}
|
|
11062
11062
|
//#endregion
|
|
11063
|
+
//#region src/core/threads.ts
|
|
11064
|
+
function newUuid() {
|
|
11065
|
+
const bytes = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
11066
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
11067
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
11068
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
11069
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
11070
|
+
}
|
|
11071
|
+
function threadFromRecord(record, hasMessages = true) {
|
|
11072
|
+
return {
|
|
11073
|
+
id: record.id,
|
|
11074
|
+
title: record.title,
|
|
11075
|
+
agentId: record.agent_id,
|
|
11076
|
+
version: record.version,
|
|
11077
|
+
role: record.role,
|
|
11078
|
+
writerActive: record.writer_active,
|
|
11079
|
+
hasMessages,
|
|
11080
|
+
updatedAt: record.updated_at
|
|
11081
|
+
};
|
|
11082
|
+
}
|
|
11083
|
+
function savedToolCallId(messageId, partId, targetId) {
|
|
11084
|
+
return `saved:${messageId}:${partId}${targetId ? `:${targetId}` : ""}`;
|
|
11085
|
+
}
|
|
11086
|
+
async function loadThreadMessages(id, options, pageAfter, liveToolMessageIds = /* @__PURE__ */ new Map()) {
|
|
11087
|
+
const page = await listChatMessages(id, {
|
|
11088
|
+
page_size: 100,
|
|
11089
|
+
...pageAfter ? { page_after: pageAfter } : {}
|
|
11090
|
+
}, options);
|
|
11091
|
+
const pendingInteractions = page.pending_interactions.map((pending) => ({
|
|
11092
|
+
sourceMessageId: pending.source_message_id,
|
|
11093
|
+
sourcePartId: pending.source_part_id,
|
|
11094
|
+
responseTargetId: pending.response_target_id,
|
|
11095
|
+
toolCallId: liveToolMessageIds.get(pending.tool_call_id) === pending.source_message_id ? pending.tool_call_id : savedToolCallId(pending.source_message_id, pending.source_part_id, pending.response_target_id),
|
|
11096
|
+
targetTenantUserId: pending.target_tenant_user_id,
|
|
11097
|
+
targetClientId: pending.target_client_id,
|
|
11098
|
+
executionLocation: pending.execution_location
|
|
11099
|
+
}));
|
|
11100
|
+
return {
|
|
11101
|
+
...page,
|
|
11102
|
+
pendingInteractions
|
|
11103
|
+
};
|
|
11104
|
+
}
|
|
11105
|
+
function projectThreadMessages(records, pendingInteractions = [], liveToolMessageIds = /* @__PURE__ */ new Map()) {
|
|
11106
|
+
const messages = [];
|
|
11107
|
+
const calls = /* @__PURE__ */ new Map();
|
|
11108
|
+
for (const message of records) {
|
|
11109
|
+
if (message.role === "tool") {
|
|
11110
|
+
for (const part of message.parts) {
|
|
11111
|
+
const call = calls.get(savedToolCallId(message.source_assistant_message_id, message.source_tool_part_id, message.response_target_id));
|
|
11112
|
+
if (!call) continue;
|
|
11113
|
+
call.output = part.output ?? null;
|
|
11114
|
+
Object.assign(call, { resultOutcome: part.outcome });
|
|
11115
|
+
call.state = part.outcome === "succeeded" ? "complete" : "error";
|
|
11116
|
+
}
|
|
11117
|
+
continue;
|
|
11118
|
+
}
|
|
11119
|
+
const parts = message.parts.flatMap((part) => {
|
|
11120
|
+
const source = part.source;
|
|
11121
|
+
if (typeof part.type === "string" && [
|
|
11122
|
+
"image",
|
|
11123
|
+
"document",
|
|
11124
|
+
"audio",
|
|
11125
|
+
"video"
|
|
11126
|
+
].includes(part.type) && source && typeof source === "object" && !Array.isArray(source) && "type" in source && source.type === "attachment") return [{
|
|
11127
|
+
...part,
|
|
11128
|
+
source: {
|
|
11129
|
+
...source,
|
|
11130
|
+
type: "url",
|
|
11131
|
+
value: ""
|
|
11132
|
+
},
|
|
11133
|
+
fileId: "id" in source && typeof source.id === "string" ? source.id : void 0
|
|
11134
|
+
}];
|
|
11135
|
+
if (part.type !== "tool-call") return [part];
|
|
11136
|
+
if (part.executionLocation === "provider") return [part];
|
|
11137
|
+
const toolCallId = String(part.toolCallId);
|
|
11138
|
+
const targets = Array.isArray(part.targets) ? part.targets.flatMap((target) => target && typeof target === "object" && !Array.isArray(target) && "id" in target && typeof target.id === "string" ? [target.id] : []) : pendingInteractions.filter((pending) => pending.sourceMessageId === message.id && pending.sourcePartId === part.id).map((pending) => pending.responseTargetId);
|
|
11139
|
+
return (targets.length ? targets : [void 0]).map((responseTargetId) => {
|
|
11140
|
+
const savedId = savedToolCallId(message.id, String(part.id), responseTargetId);
|
|
11141
|
+
const call = {
|
|
11142
|
+
...part,
|
|
11143
|
+
savedToolTitle: part.declaration && typeof part.declaration === "object" && !Array.isArray(part.declaration) && "title" in part.declaration && typeof part.declaration.title === "string" ? part.declaration.title : void 0,
|
|
11144
|
+
id: liveToolMessageIds.get(toolCallId) === message.id ? toolCallId : savedId,
|
|
11145
|
+
upstreamToolCallId: toolCallId,
|
|
11146
|
+
applicationPartId: part.id,
|
|
11147
|
+
sourceMessageId: message.id,
|
|
11148
|
+
responseTargetId
|
|
11149
|
+
};
|
|
11150
|
+
calls.set(savedId, call);
|
|
11151
|
+
return call;
|
|
11152
|
+
});
|
|
11153
|
+
});
|
|
11154
|
+
messages.push({
|
|
11155
|
+
id: message.id,
|
|
11156
|
+
role: message.role,
|
|
11157
|
+
parts,
|
|
11158
|
+
createdAt: new Date(message.created_at),
|
|
11159
|
+
metadata: { astralbeam: {
|
|
11160
|
+
state: message.state,
|
|
11161
|
+
authorTenantUserId: message.author_tenant_user_id
|
|
11162
|
+
} }
|
|
11163
|
+
});
|
|
11164
|
+
}
|
|
11165
|
+
return messages;
|
|
11166
|
+
}
|
|
11167
|
+
function chatPlacementKey(options) {
|
|
11168
|
+
return options.placementKey ?? options.agentId ?? "default";
|
|
11169
|
+
}
|
|
11170
|
+
/** Cache selection within this tab. Stored IDs are reauthorized before loading history. */
|
|
11171
|
+
function selectedThread(key, id) {
|
|
11172
|
+
try {
|
|
11173
|
+
if (id === void 0) return globalThis.sessionStorage?.getItem(key) ?? void 0;
|
|
11174
|
+
if (id === null) globalThis.sessionStorage?.removeItem(key);
|
|
11175
|
+
else globalThis.sessionStorage?.setItem(key, id);
|
|
11176
|
+
} catch {}
|
|
11177
|
+
}
|
|
11178
|
+
//#endregion
|
|
11179
|
+
//#region src/lib/drafts.ts
|
|
11180
|
+
let draftSessionId;
|
|
11181
|
+
function draftStorageKey(apiUrl, identity, threadId) {
|
|
11182
|
+
if (!threadId) {
|
|
11183
|
+
if (!draftSessionId) try {
|
|
11184
|
+
const key = "astralbeam:draft-session";
|
|
11185
|
+
draftSessionId = globalThis.sessionStorage?.getItem(key) ?? newUuid();
|
|
11186
|
+
globalThis.sessionStorage?.setItem(key, draftSessionId);
|
|
11187
|
+
} catch {
|
|
11188
|
+
draftSessionId ??= newUuid();
|
|
11189
|
+
}
|
|
11190
|
+
threadId = `new:${draftSessionId}`;
|
|
11191
|
+
}
|
|
11192
|
+
return JSON.stringify([
|
|
11193
|
+
apiUrl.replace(/\/+$/, ""),
|
|
11194
|
+
identity,
|
|
11195
|
+
threadId
|
|
11196
|
+
]);
|
|
11197
|
+
}
|
|
11198
|
+
function isStoredDraftAttachment(file) {
|
|
11199
|
+
return typeof file === "object" && file !== null && "id" in file && typeof file.id === "string" && "name" in file && typeof file.name === "string" && "size" in file && typeof file.size === "number" && Number.isFinite(file.size) && file.size >= 0 && "mimeType" in file && typeof file.mimeType === "string" && "status" in file && file.status === "ready" && "fileId" in file && typeof file.fileId === "string" && (!("kind" in file) || typeof file.kind === "string" && [
|
|
11200
|
+
"image",
|
|
11201
|
+
"pdf",
|
|
11202
|
+
"text",
|
|
11203
|
+
"data",
|
|
11204
|
+
"office"
|
|
11205
|
+
].includes(file.kind)) && (!("error" in file) || typeof file.error === "string");
|
|
11206
|
+
}
|
|
11207
|
+
/** Reads or writes unsent text, or with `sent`, the recovery copy of a send not yet accepted. */
|
|
11208
|
+
function storedThreadDraft(apiUrl, identity, threadId, text, slot = "draft") {
|
|
11209
|
+
if (!identity) return "";
|
|
11210
|
+
const key = `astralbeam:${slot}:${draftStorageKey(apiUrl, identity, threadId)}`;
|
|
11211
|
+
try {
|
|
11212
|
+
if (text === void 0) return globalThis.localStorage?.getItem(key) ?? "";
|
|
11213
|
+
if (text) globalThis.localStorage?.setItem(key, text);
|
|
11214
|
+
else globalThis.localStorage?.removeItem(key);
|
|
11215
|
+
} catch {}
|
|
11216
|
+
return "";
|
|
11217
|
+
}
|
|
11218
|
+
function storedThreadAttachments({ apiUrl, identity, threadId, update, moveTo }) {
|
|
11219
|
+
const applyChanges = () => {
|
|
11220
|
+
if (!identity) return [];
|
|
11221
|
+
const key = `astralbeam:attachments:${draftStorageKey(apiUrl, identity, threadId)}`;
|
|
11222
|
+
const read = (key) => {
|
|
11223
|
+
const files = JSON.parse(globalThis.localStorage?.getItem(key) ?? "[]");
|
|
11224
|
+
if (!Array.isArray(files) || !files.every(isStoredDraftAttachment)) throw new TypeError("Stored attachment draft is invalid");
|
|
11225
|
+
return files;
|
|
11226
|
+
};
|
|
11227
|
+
let attachments = read(key);
|
|
11228
|
+
if (update) attachments = update(attachments).filter((file) => file.status === "ready" && file.fileId !== void 0);
|
|
11229
|
+
if (moveTo !== void 0) {
|
|
11230
|
+
const destinationKey = `astralbeam:attachments:${draftStorageKey(apiUrl, identity, moveTo)}`;
|
|
11231
|
+
attachments = [...new Map([...read(destinationKey), ...attachments].map((file) => [file.id, file])).values()];
|
|
11232
|
+
globalThis.localStorage?.setItem(destinationKey, JSON.stringify(attachments));
|
|
11233
|
+
globalThis.localStorage?.removeItem(key);
|
|
11234
|
+
} else if (update) {
|
|
11235
|
+
if (attachments.length) globalThis.localStorage?.setItem(key, JSON.stringify(attachments));
|
|
11236
|
+
else globalThis.localStorage?.removeItem(key);
|
|
11237
|
+
}
|
|
11238
|
+
return attachments;
|
|
11239
|
+
};
|
|
11240
|
+
return globalThis.navigator?.locks ? navigator.locks.request("astralbeam:attachment-drafts", applyChanges) : Promise.resolve().then(applyChanges);
|
|
11241
|
+
}
|
|
11242
|
+
//#endregion
|
|
11063
11243
|
//#region src/core/protocol.ts
|
|
11064
11244
|
const RENDER_WIDGET_TOOL = "render_widget";
|
|
11065
11245
|
const ASK_QUESTIONNAIRE_TOOL = "ask_questionnaire";
|
|
@@ -11696,124 +11876,12 @@ function describeSandboxCommandRun(run) {
|
|
|
11696
11876
|
return parts.join(" · ");
|
|
11697
11877
|
}
|
|
11698
11878
|
//#endregion
|
|
11699
|
-
//#region src/core/threads.ts
|
|
11700
|
-
function newUuid() {
|
|
11701
|
-
const bytes = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
11702
|
-
bytes[6] = bytes[6] & 15 | 64;
|
|
11703
|
-
bytes[8] = bytes[8] & 63 | 128;
|
|
11704
|
-
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
11705
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
11706
|
-
}
|
|
11707
|
-
function threadFromRecord(record, hasMessages = true) {
|
|
11708
|
-
return {
|
|
11709
|
-
id: record.id,
|
|
11710
|
-
title: record.title,
|
|
11711
|
-
agentId: record.agent_id,
|
|
11712
|
-
version: record.version,
|
|
11713
|
-
role: record.role,
|
|
11714
|
-
writerActive: record.writer_active,
|
|
11715
|
-
hasMessages,
|
|
11716
|
-
updatedAt: record.updated_at
|
|
11717
|
-
};
|
|
11718
|
-
}
|
|
11719
|
-
function savedToolCallId(messageId, partId, targetId) {
|
|
11720
|
-
return `saved:${messageId}:${partId}${targetId ? `:${targetId}` : ""}`;
|
|
11721
|
-
}
|
|
11722
|
-
async function loadThreadMessages(id, options, pageAfter, liveToolMessageIds = /* @__PURE__ */ new Map()) {
|
|
11723
|
-
const page = await listChatMessages(id, {
|
|
11724
|
-
page_size: 100,
|
|
11725
|
-
...pageAfter ? { page_after: pageAfter } : {}
|
|
11726
|
-
}, options);
|
|
11727
|
-
const pendingInteractions = page.pending_interactions.map((pending) => ({
|
|
11728
|
-
sourceMessageId: pending.source_message_id,
|
|
11729
|
-
sourcePartId: pending.source_part_id,
|
|
11730
|
-
responseTargetId: pending.response_target_id,
|
|
11731
|
-
toolCallId: liveToolMessageIds.get(pending.tool_call_id) === pending.source_message_id ? pending.tool_call_id : savedToolCallId(pending.source_message_id, pending.source_part_id, pending.response_target_id),
|
|
11732
|
-
targetTenantUserId: pending.target_tenant_user_id,
|
|
11733
|
-
targetClientId: pending.target_client_id,
|
|
11734
|
-
executionLocation: pending.execution_location
|
|
11735
|
-
}));
|
|
11736
|
-
return {
|
|
11737
|
-
...page,
|
|
11738
|
-
pendingInteractions
|
|
11739
|
-
};
|
|
11740
|
-
}
|
|
11741
|
-
function projectThreadMessages(records, pendingInteractions = [], liveToolMessageIds = /* @__PURE__ */ new Map()) {
|
|
11742
|
-
const messages = [];
|
|
11743
|
-
const calls = /* @__PURE__ */ new Map();
|
|
11744
|
-
for (const message of records) {
|
|
11745
|
-
if (message.role === "tool") {
|
|
11746
|
-
for (const part of message.parts) {
|
|
11747
|
-
const call = calls.get(savedToolCallId(message.source_assistant_message_id, message.source_tool_part_id, message.response_target_id));
|
|
11748
|
-
if (!call) continue;
|
|
11749
|
-
call.output = part.output ?? null;
|
|
11750
|
-
Object.assign(call, { resultOutcome: part.outcome });
|
|
11751
|
-
call.state = part.outcome === "succeeded" ? "complete" : "error";
|
|
11752
|
-
}
|
|
11753
|
-
continue;
|
|
11754
|
-
}
|
|
11755
|
-
const parts = message.parts.flatMap((part) => {
|
|
11756
|
-
const source = part.source;
|
|
11757
|
-
if (typeof part.type === "string" && [
|
|
11758
|
-
"image",
|
|
11759
|
-
"document",
|
|
11760
|
-
"audio",
|
|
11761
|
-
"video"
|
|
11762
|
-
].includes(part.type) && source && typeof source === "object" && !Array.isArray(source) && "type" in source && source.type === "attachment") return [{
|
|
11763
|
-
...part,
|
|
11764
|
-
source: {
|
|
11765
|
-
...source,
|
|
11766
|
-
type: "url",
|
|
11767
|
-
value: ""
|
|
11768
|
-
},
|
|
11769
|
-
fileId: "id" in source && typeof source.id === "string" ? source.id : void 0
|
|
11770
|
-
}];
|
|
11771
|
-
if (part.type !== "tool-call") return [part];
|
|
11772
|
-
if (part.executionLocation === "provider") return [part];
|
|
11773
|
-
const toolCallId = String(part.toolCallId);
|
|
11774
|
-
const targets = Array.isArray(part.targets) ? part.targets.flatMap((target) => target && typeof target === "object" && !Array.isArray(target) && "id" in target && typeof target.id === "string" ? [target.id] : []) : pendingInteractions.filter((pending) => pending.sourceMessageId === message.id && pending.sourcePartId === part.id).map((pending) => pending.responseTargetId);
|
|
11775
|
-
return (targets.length ? targets : [void 0]).map((responseTargetId) => {
|
|
11776
|
-
const savedId = savedToolCallId(message.id, String(part.id), responseTargetId);
|
|
11777
|
-
const call = {
|
|
11778
|
-
...part,
|
|
11779
|
-
savedToolTitle: part.declaration && typeof part.declaration === "object" && !Array.isArray(part.declaration) && "title" in part.declaration && typeof part.declaration.title === "string" ? part.declaration.title : void 0,
|
|
11780
|
-
id: liveToolMessageIds.get(toolCallId) === message.id ? toolCallId : savedId,
|
|
11781
|
-
upstreamToolCallId: toolCallId,
|
|
11782
|
-
applicationPartId: part.id,
|
|
11783
|
-
sourceMessageId: message.id,
|
|
11784
|
-
responseTargetId
|
|
11785
|
-
};
|
|
11786
|
-
calls.set(savedId, call);
|
|
11787
|
-
return call;
|
|
11788
|
-
});
|
|
11789
|
-
});
|
|
11790
|
-
messages.push({
|
|
11791
|
-
id: message.id,
|
|
11792
|
-
role: message.role,
|
|
11793
|
-
parts,
|
|
11794
|
-
createdAt: new Date(message.created_at),
|
|
11795
|
-
metadata: { astralbeam: {
|
|
11796
|
-
state: message.state,
|
|
11797
|
-
authorTenantUserId: message.author_tenant_user_id
|
|
11798
|
-
} }
|
|
11799
|
-
});
|
|
11800
|
-
}
|
|
11801
|
-
return messages;
|
|
11802
|
-
}
|
|
11803
|
-
/** Cache selection within this tab. Stored IDs are reauthorized before loading history. */
|
|
11804
|
-
function selectedThread(key, id) {
|
|
11805
|
-
try {
|
|
11806
|
-
if (id === void 0) return globalThis.sessionStorage?.getItem(key) ?? void 0;
|
|
11807
|
-
if (id === null) globalThis.sessionStorage?.removeItem(key);
|
|
11808
|
-
else globalThis.sessionStorage?.setItem(key, id);
|
|
11809
|
-
} catch {}
|
|
11810
|
-
}
|
|
11811
|
-
//#endregion
|
|
11812
11879
|
//#region src/core/session.ts
|
|
11813
11880
|
const HISTORY_FAILED = "Reopen this conversation or start a new one before sending.";
|
|
11814
11881
|
const CORE_OPTION_KEYS = Object.keys({
|
|
11815
11882
|
agentId: true,
|
|
11816
11883
|
threadId: true,
|
|
11884
|
+
placementKey: true,
|
|
11817
11885
|
apiUrl: true,
|
|
11818
11886
|
fetchAstralBeamToken: true,
|
|
11819
11887
|
tools: true,
|
|
@@ -11834,6 +11902,8 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
11834
11902
|
let live = { ...options };
|
|
11835
11903
|
let debug = createDebugLogger(live.debug);
|
|
11836
11904
|
const listeners = /* @__PURE__ */ new Set();
|
|
11905
|
+
let view;
|
|
11906
|
+
const widgetOptions = () => view?.options ?? live;
|
|
11837
11907
|
let state = {
|
|
11838
11908
|
messages: [],
|
|
11839
11909
|
status: "ready",
|
|
@@ -11858,8 +11928,9 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
11858
11928
|
let submission;
|
|
11859
11929
|
const submissions = /* @__PURE__ */ new Map();
|
|
11860
11930
|
let clientStatus = "ready";
|
|
11931
|
+
let operation;
|
|
11861
11932
|
const update = (next) => {
|
|
11862
|
-
const status = submission?.phase === "submitting"
|
|
11933
|
+
const status = operation || submission?.phase === "submitting" ? clientStatus === "streaming" || state.status === "streaming" ? "streaming" : "submitted" : clientStatus;
|
|
11863
11934
|
state = {
|
|
11864
11935
|
...state,
|
|
11865
11936
|
...next,
|
|
@@ -11867,6 +11938,11 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
11867
11938
|
};
|
|
11868
11939
|
for (const listener of listeners) listener();
|
|
11869
11940
|
};
|
|
11941
|
+
const finishOperation = (owner) => {
|
|
11942
|
+
if (operation !== owner) return;
|
|
11943
|
+
operation = void 0;
|
|
11944
|
+
update({});
|
|
11945
|
+
};
|
|
11870
11946
|
const project = ({ id, content, phase, error }) => ({
|
|
11871
11947
|
id,
|
|
11872
11948
|
content,
|
|
@@ -11888,15 +11964,15 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
11888
11964
|
sent.phase = phase;
|
|
11889
11965
|
sent.error = error;
|
|
11890
11966
|
if (sent === submission) update({ submission: project(sent) });
|
|
11891
|
-
if (phase === "refused") sent.onRefused?.();
|
|
11892
11967
|
return true;
|
|
11893
11968
|
};
|
|
11894
|
-
const current = (sent, attempt) => sent === submission &&
|
|
11969
|
+
const current = (sent, attempt) => sent === submission && operation === attempt;
|
|
11895
11970
|
const owns = (sent, attempt) => current(sent, attempt) && sent.phase === "submitting";
|
|
11896
11971
|
const endUnaccepted = (sent, error) => settle(sent, sent.tools ? "failed" : "refused", error ?? (sent.tools ? /* @__PURE__ */ new Error("This message may not have been sent. Retry to send it.") : void 0));
|
|
11897
11972
|
const releaseSubmission = (reason) => {
|
|
11898
11973
|
const released = submission;
|
|
11899
11974
|
if (released?.phase === "submitting") endUnaccepted(released, released.tools ? void 0 : new Error(reason));
|
|
11975
|
+
if (released?.phase === "refused") released.onRefused?.();
|
|
11900
11976
|
submission = void 0;
|
|
11901
11977
|
return released;
|
|
11902
11978
|
};
|
|
@@ -12028,25 +12104,32 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12028
12104
|
const renderCleanups = /* @__PURE__ */ new Map();
|
|
12029
12105
|
const restoredWidgets = /* @__PURE__ */ new Set();
|
|
12030
12106
|
const restoringWidgets = /* @__PURE__ */ new Set();
|
|
12107
|
+
let renderGeneration = 0;
|
|
12031
12108
|
const disposeRenders = () => {
|
|
12109
|
+
renderGeneration++;
|
|
12032
12110
|
for (const cleanup of renderCleanups.values()) cleanup();
|
|
12033
12111
|
renderCleanups.clear();
|
|
12034
12112
|
restoredWidgets.clear();
|
|
12035
12113
|
restoringWidgets.clear();
|
|
12036
12114
|
};
|
|
12037
12115
|
const renderWidget = async (input, toolCallId) => {
|
|
12116
|
+
if (!widgetOptions().onRenderWidget) return {
|
|
12117
|
+
widget: input.widget,
|
|
12118
|
+
rendered: false
|
|
12119
|
+
};
|
|
12038
12120
|
const generation = selectionGeneration;
|
|
12039
|
-
const
|
|
12121
|
+
const rendering = renderGeneration;
|
|
12122
|
+
const widgets = widgetOptions().widgets ?? {};
|
|
12040
12123
|
if (!Object.hasOwn(widgets, input.widget)) throw new Error(`Unknown widget "${input.widget}"`);
|
|
12041
12124
|
const declaration = widgets[input.widget];
|
|
12042
12125
|
const validated = await validateParameters(declaration?.parameters, input.props ?? {});
|
|
12043
|
-
if (generation !== selectionGeneration || state.thread?.role === "viewer") return {
|
|
12126
|
+
if (generation !== selectionGeneration || rendering !== renderGeneration || state.thread?.role === "viewer") return {
|
|
12044
12127
|
widget: input.widget,
|
|
12045
12128
|
rendered: false
|
|
12046
12129
|
};
|
|
12047
|
-
if (declaration !==
|
|
12130
|
+
if (declaration !== widgetOptions().widgets?.[input.widget]) return renderWidget(input, toolCallId);
|
|
12048
12131
|
if (validated == null) throw new Error(`Props for widget "${input.widget}" failed validation`);
|
|
12049
|
-
const onRenderWidget =
|
|
12132
|
+
const onRenderWidget = widgetOptions().onRenderWidget;
|
|
12050
12133
|
if (!onRenderWidget) return {
|
|
12051
12134
|
widget: input.widget,
|
|
12052
12135
|
rendered: false
|
|
@@ -12122,17 +12205,18 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12122
12205
|
if (restoredWidgets.has(renderId) || restoringWidgets.has(renderId)) continue;
|
|
12123
12206
|
restoringWidgets.add(renderId);
|
|
12124
12207
|
const generation = selectionGeneration;
|
|
12125
|
-
const
|
|
12208
|
+
const rendering = renderGeneration;
|
|
12209
|
+
const { widgets, onRenderWidget } = widgetOptions();
|
|
12126
12210
|
renderWidget(part.input, renderId).catch((error) => debug?.("error", "Saved widget could not be rendered", error)).finally(() => {
|
|
12127
|
-
if (generation !== selectionGeneration) return;
|
|
12211
|
+
if (generation !== selectionGeneration || rendering !== renderGeneration) return;
|
|
12128
12212
|
restoringWidgets.delete(renderId);
|
|
12129
|
-
if (widgets !==
|
|
12213
|
+
if (widgets !== widgetOptions().widgets || onRenderWidget !== widgetOptions().onRenderWidget) restoreCompletedWidgets(state.messages);
|
|
12130
12214
|
});
|
|
12131
12215
|
}
|
|
12132
12216
|
};
|
|
12133
12217
|
const buildTools = () => {
|
|
12134
12218
|
const selection = selectionGeneration;
|
|
12135
|
-
return buildAgentTools(
|
|
12219
|
+
return buildAgentTools(widgetOptions().widgets ?? {}, live.tools ?? {}, renderWidget, debug, (name, declaration, toolCallId) => {
|
|
12136
12220
|
const execution = toolExecutionGeneration;
|
|
12137
12221
|
const executingClient = client;
|
|
12138
12222
|
const executingIdentity = identity;
|
|
@@ -12511,6 +12595,7 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12511
12595
|
};
|
|
12512
12596
|
const replaceClient = (messages) => {
|
|
12513
12597
|
const previous = client;
|
|
12598
|
+
operation = void 0;
|
|
12514
12599
|
unsubscribeSnapshot?.();
|
|
12515
12600
|
unsubscribeSnapshot = void 0;
|
|
12516
12601
|
snapshotMessages = void 0;
|
|
@@ -12539,7 +12624,7 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12539
12624
|
...part,
|
|
12540
12625
|
upstreamToolCallId,
|
|
12541
12626
|
id,
|
|
12542
|
-
...part.name === "render_widget"
|
|
12627
|
+
...part.name === "render_widget" ? { widgetRenderId: stored.widgetRenderId ?? messagePresentation.get(message.id)?.renderIds?.get(savedToolCallId(message.id, part.id)) ?? part.id } : {}
|
|
12543
12628
|
};
|
|
12544
12629
|
});
|
|
12545
12630
|
if (aliases.size) historicalToolIds.set(message.id, aliases);
|
|
@@ -12617,8 +12702,10 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12617
12702
|
if (!threadKey) return;
|
|
12618
12703
|
selected = selectedThread(threadKey);
|
|
12619
12704
|
}
|
|
12620
|
-
if (selected && selected !== "new")
|
|
12621
|
-
|
|
12705
|
+
if (selected && selected !== "new") {
|
|
12706
|
+
if (selected === state.thread?.id && threadKey) selectedThread(threadKey, selected);
|
|
12707
|
+
else openThread(selected, restoring);
|
|
12708
|
+
} else if (!restoring) newThread();
|
|
12622
12709
|
};
|
|
12623
12710
|
const ensureThread = async (sent, attempt) => {
|
|
12624
12711
|
if (state.thread) return;
|
|
@@ -12689,7 +12776,7 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12689
12776
|
};
|
|
12690
12777
|
const addToolResult = async (result) => {
|
|
12691
12778
|
const generation = selectionGeneration;
|
|
12692
|
-
if (submission?.phase === "submitting" || client.getIsLoading()) {
|
|
12779
|
+
if (operation || submission?.phase === "submitting" || client.getIsLoading()) {
|
|
12693
12780
|
reportError(/* @__PURE__ */ new Error("Wait for this client's current request to finish before responding."));
|
|
12694
12781
|
return;
|
|
12695
12782
|
}
|
|
@@ -12712,22 +12799,30 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12712
12799
|
output: output ?? null
|
|
12713
12800
|
});
|
|
12714
12801
|
}
|
|
12715
|
-
|
|
12716
|
-
|
|
12717
|
-
|
|
12718
|
-
|
|
12719
|
-
|
|
12720
|
-
|
|
12721
|
-
|
|
12722
|
-
|
|
12723
|
-
|
|
12724
|
-
|
|
12725
|
-
|
|
12726
|
-
|
|
12802
|
+
const owner = operation = {};
|
|
12803
|
+
try {
|
|
12804
|
+
update({});
|
|
12805
|
+
if (operation !== owner) return;
|
|
12806
|
+
scopeCompletedToolCalls();
|
|
12807
|
+
if (pending) {
|
|
12808
|
+
client.stop();
|
|
12809
|
+
await refreshThread(false);
|
|
12810
|
+
if (operation !== owner || generation !== selectionGeneration) return;
|
|
12811
|
+
await client.append({
|
|
12812
|
+
role: "tool",
|
|
12813
|
+
toolCallId: result.toolCallId,
|
|
12814
|
+
content: JSON.stringify(toolResults.get(toolResultKey(result.toolCallId)))
|
|
12815
|
+
});
|
|
12816
|
+
} else if ("tool" in result) await client.addToolResult(result);
|
|
12817
|
+
if (operation === owner && generation === selectionGeneration) await refreshThread(false);
|
|
12818
|
+
} finally {
|
|
12819
|
+
finishOperation(owner);
|
|
12820
|
+
}
|
|
12727
12821
|
};
|
|
12728
12822
|
const abandonToolCall = (toolCallId) => addToolResult({ toolCallId });
|
|
12729
12823
|
const deliver = async (sent) => {
|
|
12730
|
-
|
|
12824
|
+
if (sent !== submission || sent.phase !== "submitting") return project(sent);
|
|
12825
|
+
const attempt = operation = {};
|
|
12731
12826
|
try {
|
|
12732
12827
|
await getValidChatAuthToken(authentication);
|
|
12733
12828
|
await hydration.catch(() => void 0);
|
|
@@ -12747,11 +12842,13 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12747
12842
|
const failure = error instanceof Error ? error : new Error(String(error));
|
|
12748
12843
|
if (failure.name === "AbortError") endUnaccepted(sent);
|
|
12749
12844
|
else if (endUnaccepted(sent, failure)) notifyError(failure);
|
|
12845
|
+
} finally {
|
|
12846
|
+
finishOperation(attempt);
|
|
12750
12847
|
}
|
|
12751
12848
|
return project(sent);
|
|
12752
12849
|
};
|
|
12753
12850
|
const sendMessage = async (content, callbacks) => {
|
|
12754
|
-
const rejection = submission?.phase === "submitting" ||
|
|
12851
|
+
const rejection = operation || submission?.phase === "submitting" || client.getIsLoading() ? "Wait for this client's current request to finish before sending." : state.history === "loading" ? "Wait for this conversation to load before sending." : state.history === "failed" ? HISTORY_FAILED : state.thread?.agentId === null ? "This conversation’s agent is unavailable. Start a new conversation to continue." : state.thread?.role === "viewer" ? "You have read-only access to this conversation." : submission?.phase === "failed" ? "The previous message’s acceptance is unconfirmed. Retry that message before sending different text, or start a new conversation." : void 0;
|
|
12755
12852
|
if (rejection) return {
|
|
12756
12853
|
id: newUuid(),
|
|
12757
12854
|
content,
|
|
@@ -12763,8 +12860,7 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12763
12860
|
content,
|
|
12764
12861
|
phase: "submitting",
|
|
12765
12862
|
key: newUuid(),
|
|
12766
|
-
callbacks
|
|
12767
|
-
attempt: 0
|
|
12863
|
+
callbacks
|
|
12768
12864
|
};
|
|
12769
12865
|
submission = sent;
|
|
12770
12866
|
update({
|
|
@@ -12781,9 +12877,10 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12781
12877
|
return Promise.race([deliver(sent), refused.then(() => project(sent))]);
|
|
12782
12878
|
};
|
|
12783
12879
|
const retry = async () => {
|
|
12784
|
-
|
|
12880
|
+
const sent = submission;
|
|
12881
|
+
if (operation || client.getIsLoading() || !sent || !settle(sent, "submitting")) return void 0;
|
|
12785
12882
|
update({ error: void 0 });
|
|
12786
|
-
return deliver(
|
|
12883
|
+
return deliver(sent);
|
|
12787
12884
|
};
|
|
12788
12885
|
const observeAuthentication = () => {
|
|
12789
12886
|
const auth = authenticationState(authentication);
|
|
@@ -12795,9 +12892,12 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12795
12892
|
submissions.clear();
|
|
12796
12893
|
threadToolResults.clear();
|
|
12797
12894
|
toolResults.clear();
|
|
12895
|
+
changeSelection();
|
|
12896
|
+
threadKey = void 0;
|
|
12798
12897
|
}
|
|
12799
12898
|
identity = nextIdentity;
|
|
12800
|
-
const
|
|
12899
|
+
const scope = JSON.stringify(chatPlacementKey(live));
|
|
12900
|
+
const nextThreadKey = `astralbeam:thread:${live.apiUrl ?? "https://astralbeam.ai/api"}:${nextIdentity}:session:${scope}`;
|
|
12801
12901
|
if (threadKey !== nextThreadKey) {
|
|
12802
12902
|
if (threadKey !== void 0) changeSelection();
|
|
12803
12903
|
threadKey = nextThreadKey;
|
|
@@ -12830,6 +12930,32 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12830
12930
|
return {
|
|
12831
12931
|
start,
|
|
12832
12932
|
getState: () => state,
|
|
12933
|
+
getOptions: () => live,
|
|
12934
|
+
attachView: (options) => {
|
|
12935
|
+
if (view) throw new Error("This chat session already has a view attached");
|
|
12936
|
+
const binding = { options };
|
|
12937
|
+
const refresh = () => {
|
|
12938
|
+
client.updateOptions({ tools: declareTools() });
|
|
12939
|
+
restoreCompletedWidgets(state.messages);
|
|
12940
|
+
};
|
|
12941
|
+
disposeRenders();
|
|
12942
|
+
view = binding;
|
|
12943
|
+
refresh();
|
|
12944
|
+
return {
|
|
12945
|
+
update: (next) => {
|
|
12946
|
+
if (view !== binding) return;
|
|
12947
|
+
const previous = binding.options;
|
|
12948
|
+
binding.options = next;
|
|
12949
|
+
if (previous.widgets !== next.widgets || previous.onRenderWidget !== next.onRenderWidget) refresh();
|
|
12950
|
+
},
|
|
12951
|
+
dispose: () => {
|
|
12952
|
+
if (view !== binding) return;
|
|
12953
|
+
disposeRenders();
|
|
12954
|
+
view = void 0;
|
|
12955
|
+
refresh();
|
|
12956
|
+
}
|
|
12957
|
+
};
|
|
12958
|
+
},
|
|
12833
12959
|
subscribe: (listener) => {
|
|
12834
12960
|
listeners.add(listener);
|
|
12835
12961
|
return () => listeners.delete(listener);
|
|
@@ -12838,11 +12964,13 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12838
12964
|
const agent = live.agentId;
|
|
12839
12965
|
const apiUrl = live.apiUrl;
|
|
12840
12966
|
const selectedId = live.threadId;
|
|
12967
|
+
const placementKey = chatPlacementKey(live);
|
|
12841
12968
|
const { widgets, onRenderWidget } = live;
|
|
12842
12969
|
live = {
|
|
12843
12970
|
...live,
|
|
12844
12971
|
...next
|
|
12845
12972
|
};
|
|
12973
|
+
const placementChanged = chatPlacementKey(live) !== placementKey;
|
|
12846
12974
|
debug = createDebugLogger(live.debug);
|
|
12847
12975
|
updateAuthentication(authentication, {
|
|
12848
12976
|
apiUrl: live.apiUrl,
|
|
@@ -12861,17 +12989,26 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12861
12989
|
changeSelection();
|
|
12862
12990
|
threadKey = void 0;
|
|
12863
12991
|
observeAuthentication();
|
|
12992
|
+
} else if (placementChanged) {
|
|
12993
|
+
startWithNewThread = false;
|
|
12994
|
+
if (!state.thread || live.threadId !== state.thread.id) changeSelection();
|
|
12995
|
+
threadKey = void 0;
|
|
12996
|
+
observeAuthentication();
|
|
12864
12997
|
} else if (live.threadId !== selectedId) selectConfiguredThread();
|
|
12865
12998
|
if (widgets !== live.widgets || onRenderWidget !== live.onRenderWidget) restoreCompletedWidgets(state.messages);
|
|
12866
|
-
if (started && (live.agentId !== agent || live.apiUrl !== apiUrl)) resolveCapabilities();
|
|
12999
|
+
if (started && (live.agentId !== agent || live.apiUrl !== apiUrl || placementChanged)) resolveCapabilities();
|
|
12867
13000
|
},
|
|
12868
13001
|
sendMessage,
|
|
12869
13002
|
retry,
|
|
12870
13003
|
addToolResult,
|
|
12871
13004
|
stop: () => {
|
|
12872
13005
|
toolExecutionGeneration++;
|
|
12873
|
-
|
|
13006
|
+
const stopped = submission;
|
|
13007
|
+
const owner = operation;
|
|
13008
|
+
if (stopped?.phase === "submitting") endUnaccepted(stopped);
|
|
12874
13009
|
client.stop();
|
|
13010
|
+
if (owner) finishOperation(owner);
|
|
13011
|
+
if (stopped?.phase === "refused") stopped.onRefused?.();
|
|
12875
13012
|
},
|
|
12876
13013
|
retryAuthentication: () => {
|
|
12877
13014
|
getValidChatAuthToken({
|
|
@@ -12928,9 +13065,11 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12928
13065
|
},
|
|
12929
13066
|
getThreadAttachment: async (params, options) => {
|
|
12930
13067
|
if (!params.threadId && !options?.preview) throw new Error("A conversation ID is required to download files.");
|
|
12931
|
-
return (await getThreadAttachment(
|
|
13068
|
+
return (await getThreadAttachment({
|
|
13069
|
+
...params,
|
|
13070
|
+
delivery: "direct"
|
|
13071
|
+
}, {
|
|
12932
13072
|
...await requestOptions(!!params.threadId || !options?.preview),
|
|
12933
|
-
cache: "default",
|
|
12934
13073
|
headers: options?.preview ? { "X-File-Preview": "true" } : {}
|
|
12935
13074
|
})).blob();
|
|
12936
13075
|
},
|
|
@@ -12949,9 +13088,23 @@ function createAstralBeamChat(options, deferStart = false) {
|
|
|
12949
13088
|
reset: () => {
|
|
12950
13089
|
debug?.("status", "conversation reset");
|
|
12951
13090
|
newThread();
|
|
13091
|
+
if (state.auth.status === "ready") {
|
|
13092
|
+
const draftIdentity = JSON.stringify([identity, chatPlacementKey(live)]);
|
|
13093
|
+
const apiUrl = live.apiUrl ?? "https://astralbeam.ai/api";
|
|
13094
|
+
storedThreadDraft(apiUrl, draftIdentity, "", "");
|
|
13095
|
+
storedThreadDraft(apiUrl, draftIdentity, "", "", "sent");
|
|
13096
|
+
storedThreadAttachments({
|
|
13097
|
+
apiUrl,
|
|
13098
|
+
identity: draftIdentity,
|
|
13099
|
+
threadId: "",
|
|
13100
|
+
update: () => []
|
|
13101
|
+
}).catch((error) => debug?.("error", "Draft could not be cleared", error));
|
|
13102
|
+
}
|
|
13103
|
+
view?.options.onReset();
|
|
12952
13104
|
},
|
|
12953
13105
|
dispose: () => {
|
|
12954
13106
|
releaseSubmission("The chat closed before this was sent.");
|
|
13107
|
+
operation = void 0;
|
|
12955
13108
|
unsubscribeSnapshot?.();
|
|
12956
13109
|
unsubscribeSnapshot = void 0;
|
|
12957
13110
|
client.detach();
|