@botdojo/chat-sdk 0.0.23 → 0.0.24
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/lib/index.js +87 -39
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -8918,6 +8918,13 @@ var BotDojoChat = ({
|
|
|
8918
8918
|
useEffect(() => {
|
|
8919
8919
|
modelContextRef.current = modelContext2;
|
|
8920
8920
|
}, [modelContext2]);
|
|
8921
|
+
const initialApiKeyRef = useRef(null);
|
|
8922
|
+
if (apiKey && !initialApiKeyRef.current) {
|
|
8923
|
+
initialApiKeyRef.current = apiKey;
|
|
8924
|
+
}
|
|
8925
|
+
const iframeApiKey = initialApiKeyRef.current || apiKey;
|
|
8926
|
+
const apiKeyRef = useRef(apiKey);
|
|
8927
|
+
apiKeyRef.current = apiKey;
|
|
8921
8928
|
const eventHandlersRef = useRef({
|
|
8922
8929
|
onReady,
|
|
8923
8930
|
onError,
|
|
@@ -9009,9 +9016,10 @@ var BotDojoChat = ({
|
|
|
9009
9016
|
onToken: !!onToken,
|
|
9010
9017
|
onMessageStart: !!onMessageStart
|
|
9011
9018
|
});
|
|
9012
|
-
const
|
|
9019
|
+
const initialIframeUrlRef = useRef(null);
|
|
9020
|
+
if (iframeApiKey && !initialIframeUrlRef.current) {
|
|
9013
9021
|
try {
|
|
9014
|
-
const base = `${getBaseUrl()}/embed/chat?key=${encodeURIComponent(
|
|
9022
|
+
const base = `${getBaseUrl()}/embed/chat?key=${encodeURIComponent(iframeApiKey)}`;
|
|
9015
9023
|
const url2 = new URL(base);
|
|
9016
9024
|
url2.searchParams.set("accent-color", accentColor);
|
|
9017
9025
|
url2.searchParams.set("flow-version", flowVersion);
|
|
@@ -9050,35 +9058,17 @@ var BotDojoChat = ({
|
|
|
9050
9058
|
url2.searchParams.set("forward-events", "true");
|
|
9051
9059
|
console.log("[BotDojoChat] Added forward-events=true to iframe URL");
|
|
9052
9060
|
}
|
|
9053
|
-
|
|
9054
|
-
console.log("[BotDojoChat] iframeUrl:",
|
|
9055
|
-
return finalUrl;
|
|
9061
|
+
initialIframeUrlRef.current = url2.toString();
|
|
9062
|
+
console.log("[BotDojoChat] iframeUrl (set once):", initialIframeUrlRef.current);
|
|
9056
9063
|
} catch (error) {
|
|
9057
9064
|
console.warn("[BotDojoChat] Error building iframe URL:", error);
|
|
9058
|
-
|
|
9065
|
+
initialIframeUrlRef.current = `${getBaseUrl()}/embed/chat?key=${encodeURIComponent(iframeApiKey)}`;
|
|
9059
9066
|
}
|
|
9060
|
-
}
|
|
9061
|
-
|
|
9062
|
-
|
|
9063
|
-
accentColor,
|
|
9064
|
-
flowVersion,
|
|
9065
|
-
flowHeaders,
|
|
9066
|
-
botImage,
|
|
9067
|
-
userImage,
|
|
9068
|
-
voiceIcon,
|
|
9069
|
-
hideBotIcon,
|
|
9070
|
-
sessionId,
|
|
9071
|
-
sessionKeyPrefix,
|
|
9072
|
-
welcomeMessage,
|
|
9073
|
-
fontSize,
|
|
9074
|
-
backgroundColor,
|
|
9075
|
-
theme,
|
|
9076
|
-
newSession,
|
|
9077
|
-
effectiveAutoFocus,
|
|
9078
|
-
hasEventCallbacks
|
|
9079
|
-
]);
|
|
9067
|
+
}
|
|
9068
|
+
const iframeUrl = initialIframeUrlRef.current;
|
|
9069
|
+
const connectorInitializedRef = useRef(false);
|
|
9080
9070
|
useEffect(() => {
|
|
9081
|
-
if (!needsConnector) {
|
|
9071
|
+
if (connectorInitializedRef.current || !needsConnector || !apiKey) {
|
|
9082
9072
|
return;
|
|
9083
9073
|
}
|
|
9084
9074
|
let mounted = true;
|
|
@@ -9104,6 +9094,7 @@ var BotDojoChat = ({
|
|
|
9104
9094
|
await currentConnector.close();
|
|
9105
9095
|
return;
|
|
9106
9096
|
}
|
|
9097
|
+
connectorInitializedRef.current = true;
|
|
9107
9098
|
setConnector(currentConnector);
|
|
9108
9099
|
setIsInitializingConnector(false);
|
|
9109
9100
|
onConnectorInit == null ? void 0 : onConnectorInit(currentConnector);
|
|
@@ -9128,15 +9119,22 @@ var BotDojoChat = ({
|
|
|
9128
9119
|
initConnector();
|
|
9129
9120
|
return () => {
|
|
9130
9121
|
mounted = false;
|
|
9122
|
+
};
|
|
9123
|
+
}, [needsConnector, apiKey]);
|
|
9124
|
+
useEffect(() => {
|
|
9125
|
+
return () => {
|
|
9126
|
+
if (!connectorInitializedRef.current)
|
|
9127
|
+
return;
|
|
9131
9128
|
const connectorToClose = connectorRef.current;
|
|
9132
9129
|
if (connectorToClose) {
|
|
9130
|
+
connectorInitializedRef.current = false;
|
|
9133
9131
|
connectorToClose.close().catch((err) => {
|
|
9134
9132
|
console.warn("[BotDojoChat] Error during unmount cleanup:", err);
|
|
9135
9133
|
});
|
|
9136
9134
|
connectorRef.current = null;
|
|
9137
9135
|
}
|
|
9138
9136
|
};
|
|
9139
|
-
}, [
|
|
9137
|
+
}, []);
|
|
9140
9138
|
useEffect(() => {
|
|
9141
9139
|
if (!connector)
|
|
9142
9140
|
return;
|
|
@@ -9515,7 +9513,8 @@ var BotDojoChat = ({
|
|
|
9515
9513
|
}
|
|
9516
9514
|
try {
|
|
9517
9515
|
console.log("[BotDojoChat] Creating PostMessageRPCClient + RPCConnection");
|
|
9518
|
-
const
|
|
9516
|
+
const currentModelContext = modelContextRef.current;
|
|
9517
|
+
const firstContext = currentModelContext ? Array.isArray(currentModelContext) ? currentModelContext[0] : currentModelContext : null;
|
|
9519
9518
|
const corsConfig = (firstContext == null ? void 0 : firstContext.cors) ? {
|
|
9520
9519
|
allowedToolCallOrigins: firstContext.cors.allowedToolCallOrigins
|
|
9521
9520
|
// botdojoChatDomain is configured server-side, not by SDK users
|
|
@@ -9523,7 +9522,8 @@ var BotDojoChat = ({
|
|
|
9523
9522
|
const client = new import_botdojo_rpc.PostMessageRPCClient(
|
|
9524
9523
|
iframeRef.current.contentWindow,
|
|
9525
9524
|
{
|
|
9526
|
-
getToken: async () =>
|
|
9525
|
+
getToken: async () => apiKeyRef.current,
|
|
9526
|
+
// Use ref to always get current token
|
|
9527
9527
|
clientId: "chat_embed_parent",
|
|
9528
9528
|
defaultDestinationId: "agent_host",
|
|
9529
9529
|
baseChannel: "chat_embed"
|
|
@@ -9749,10 +9749,10 @@ var BotDojoChat = ({
|
|
|
9749
9749
|
await new Promise((r) => setTimeout(r, 50));
|
|
9750
9750
|
waited += 50;
|
|
9751
9751
|
}
|
|
9752
|
-
const
|
|
9753
|
-
if (!uri || !
|
|
9752
|
+
const currentModelContext3 = modelContextRef.current;
|
|
9753
|
+
if (!uri || !currentModelContext3)
|
|
9754
9754
|
return { error: "no modelContext" };
|
|
9755
|
-
const contexts = Array.isArray(
|
|
9755
|
+
const contexts = Array.isArray(currentModelContext3) ? currentModelContext3 : [currentModelContext3];
|
|
9756
9756
|
const allResources = contexts.flatMap((ctx) => ctx.resources || []);
|
|
9757
9757
|
console.log("[BotDojoChat] tryModelContext: Searching", allResources.length, "resources for URI:", uri);
|
|
9758
9758
|
allResources.forEach((r, i2) => {
|
|
@@ -9906,7 +9906,8 @@ var BotDojoChat = ({
|
|
|
9906
9906
|
}
|
|
9907
9907
|
if (msg.functionName === "broadcastAgentHostConnected") {
|
|
9908
9908
|
console.log("[BotDojoChat] Received broadcastAgentHostConnected, registering ModelContext(s) with chat iframe router");
|
|
9909
|
-
const
|
|
9909
|
+
const currentModelContext3 = modelContextRef.current;
|
|
9910
|
+
const contexts = Array.isArray(currentModelContext3) ? currentModelContext3 : currentModelContext3 ? [currentModelContext3] : [];
|
|
9910
9911
|
for (const ctx of contexts) {
|
|
9911
9912
|
try {
|
|
9912
9913
|
console.log(`[BotDojoChat] Registering ModelContext "${ctx.name}" with chat iframe router`);
|
|
@@ -9923,8 +9924,9 @@ var BotDojoChat = ({
|
|
|
9923
9924
|
}
|
|
9924
9925
|
return;
|
|
9925
9926
|
}
|
|
9926
|
-
|
|
9927
|
-
|
|
9927
|
+
const currentModelContext2 = modelContextRef.current;
|
|
9928
|
+
if (currentModelContext2) {
|
|
9929
|
+
const contexts = Array.isArray(currentModelContext2) ? currentModelContext2 : [currentModelContext2];
|
|
9928
9930
|
for (const ctx of contexts) {
|
|
9929
9931
|
if (ctx.tools) {
|
|
9930
9932
|
const toolsArray = Array.isArray(ctx.tools) ? ctx.tools : Object.values(ctx.tools);
|
|
@@ -9944,7 +9946,8 @@ var BotDojoChat = ({
|
|
|
9944
9946
|
source: connection.sender,
|
|
9945
9947
|
func: async () => {
|
|
9946
9948
|
console.log("[BotDojoChat] Handling getModelContexts request");
|
|
9947
|
-
const
|
|
9949
|
+
const currentModelContext2 = modelContextRef.current;
|
|
9950
|
+
const contexts = Array.isArray(currentModelContext2) ? currentModelContext2 : currentModelContext2 ? [currentModelContext2] : [];
|
|
9948
9951
|
return contexts.map((ctx) => {
|
|
9949
9952
|
let tools = [];
|
|
9950
9953
|
if (ctx.tools) {
|
|
@@ -9973,7 +9976,7 @@ var BotDojoChat = ({
|
|
|
9973
9976
|
}
|
|
9974
9977
|
});
|
|
9975
9978
|
connectionRef.current = connection;
|
|
9976
|
-
modelContextCapturedRef.current = !!
|
|
9979
|
+
modelContextCapturedRef.current = !!modelContextRef.current;
|
|
9977
9980
|
console.log("[BotDojoChat] RPC connection initialized");
|
|
9978
9981
|
if (!chatReadyFiredRef.current) {
|
|
9979
9982
|
console.log("[BotDojoChat] RPC ready, firing onReady");
|
|
@@ -10001,7 +10004,46 @@ var BotDojoChat = ({
|
|
|
10001
10004
|
connectionRef.current = null;
|
|
10002
10005
|
}
|
|
10003
10006
|
};
|
|
10004
|
-
}, [connector, iframeLoaded,
|
|
10007
|
+
}, [connector, iframeLoaded, rpcTrigger]);
|
|
10008
|
+
useEffect(() => {
|
|
10009
|
+
if (!connectionRef.current || !rpcReadyRef.current) {
|
|
10010
|
+
return;
|
|
10011
|
+
}
|
|
10012
|
+
const reRegisterModelContext = async () => {
|
|
10013
|
+
const connection = connectionRef.current;
|
|
10014
|
+
if (!connection)
|
|
10015
|
+
return;
|
|
10016
|
+
const currentModelContext = modelContextRef.current;
|
|
10017
|
+
if (!currentModelContext) {
|
|
10018
|
+
return;
|
|
10019
|
+
}
|
|
10020
|
+
try {
|
|
10021
|
+
const contexts = Array.isArray(currentModelContext) ? currentModelContext : [currentModelContext];
|
|
10022
|
+
for (const ctx of contexts) {
|
|
10023
|
+
try {
|
|
10024
|
+
console.log(`[BotDojoChat] Re-registering ModelContext "${ctx.name}" after change`);
|
|
10025
|
+
await connection.sendRequest(
|
|
10026
|
+
"agent_host",
|
|
10027
|
+
"registerModelContext",
|
|
10028
|
+
[ctx],
|
|
10029
|
+
5e3
|
|
10030
|
+
);
|
|
10031
|
+
console.log(`[BotDojoChat] \u2705 Re-registered ModelContext "${ctx.name}"`);
|
|
10032
|
+
} catch (error) {
|
|
10033
|
+
console.error(`[BotDojoChat] \u274C Error re-registering ModelContext "${ctx.name}":`, error);
|
|
10034
|
+
}
|
|
10035
|
+
}
|
|
10036
|
+
} catch (error) {
|
|
10037
|
+
console.error("[BotDojoChat] Error re-registering model context:", error);
|
|
10038
|
+
}
|
|
10039
|
+
};
|
|
10040
|
+
const timeoutId = setTimeout(() => {
|
|
10041
|
+
reRegisterModelContext();
|
|
10042
|
+
}, 100);
|
|
10043
|
+
return () => {
|
|
10044
|
+
clearTimeout(timeoutId);
|
|
10045
|
+
};
|
|
10046
|
+
}, [modelContext2]);
|
|
10005
10047
|
const parseSize = useCallback((value2, defaultVal) => {
|
|
10006
10048
|
if (!value2 || value2 === "auto")
|
|
10007
10049
|
return defaultVal;
|
|
@@ -10348,6 +10390,9 @@ var BotDojoChat = ({
|
|
|
10348
10390
|
);
|
|
10349
10391
|
};
|
|
10350
10392
|
const renderIframe = () => {
|
|
10393
|
+
if (!iframeApiKey) {
|
|
10394
|
+
return null;
|
|
10395
|
+
}
|
|
10351
10396
|
if ((mode === "chat-popup" || mode === "side-panel" || mode === "side-push") && !isOpen && !iframeLoaded) {
|
|
10352
10397
|
return null;
|
|
10353
10398
|
}
|
|
@@ -10371,6 +10416,9 @@ var BotDojoChat = ({
|
|
|
10371
10416
|
renderResizeHandle()
|
|
10372
10417
|
] });
|
|
10373
10418
|
};
|
|
10419
|
+
if (!apiKey) {
|
|
10420
|
+
return null;
|
|
10421
|
+
}
|
|
10374
10422
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
10375
10423
|
/* @__PURE__ */ jsx("style", { children: `
|
|
10376
10424
|
@keyframes spin {
|