@botdojo/chat-sdk 0.0.20 → 0.0.21
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 +81 -11
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -8305,7 +8305,13 @@ var BotDojoChat = ({
|
|
|
8305
8305
|
const connectorRef = useRef(null);
|
|
8306
8306
|
const iframeLoadedRef = useRef(false);
|
|
8307
8307
|
const chatReadyRef = useRef(false);
|
|
8308
|
+
const chatReadyFiredRef = useRef(false);
|
|
8308
8309
|
const resizeStartPos = useRef({ x: 0, y: 0, width: 0, height: 0 });
|
|
8310
|
+
const modelContextCapturedRef = useRef(false);
|
|
8311
|
+
const modelContextRef = useRef(modelContext2);
|
|
8312
|
+
useEffect(() => {
|
|
8313
|
+
modelContextRef.current = modelContext2;
|
|
8314
|
+
}, [modelContext2]);
|
|
8309
8315
|
const eventHandlersRef = useRef({
|
|
8310
8316
|
onReady,
|
|
8311
8317
|
onError,
|
|
@@ -8350,6 +8356,28 @@ var BotDojoChat = ({
|
|
|
8350
8356
|
onToolCall,
|
|
8351
8357
|
onUiMessage
|
|
8352
8358
|
]);
|
|
8359
|
+
const rpcReadyRef = useRef(false);
|
|
8360
|
+
const [rpcTrigger, setRpcTrigger] = useState(0);
|
|
8361
|
+
useEffect(() => {
|
|
8362
|
+
const handleEarlyMessages = (event) => {
|
|
8363
|
+
const data = event.data;
|
|
8364
|
+
if ((data == null ? void 0 : data.type) !== "botdojo-rpc")
|
|
8365
|
+
return;
|
|
8366
|
+
const payload = data.payload;
|
|
8367
|
+
if ((payload == null ? void 0 : payload.functionName) === "chat_ready") {
|
|
8368
|
+
console.log("[BotDojoChat] Early listener captured chat_ready");
|
|
8369
|
+
chatReadyRef.current = true;
|
|
8370
|
+
if (!iframeLoadedRef.current) {
|
|
8371
|
+
console.log("[BotDojoChat] Setting iframeLoaded=true (inferred from chat_ready)");
|
|
8372
|
+
iframeLoadedRef.current = true;
|
|
8373
|
+
setIframeLoaded(true);
|
|
8374
|
+
setRpcTrigger((t) => t + 1);
|
|
8375
|
+
}
|
|
8376
|
+
}
|
|
8377
|
+
};
|
|
8378
|
+
window.addEventListener("message", handleEarlyMessages);
|
|
8379
|
+
return () => window.removeEventListener("message", handleEarlyMessages);
|
|
8380
|
+
}, []);
|
|
8353
8381
|
const hasModelContextContent = useMemo(() => {
|
|
8354
8382
|
if (!modelContext2)
|
|
8355
8383
|
return false;
|
|
@@ -8868,7 +8896,10 @@ var BotDojoChat = ({
|
|
|
8868
8896
|
}, [getBaseUrl]);
|
|
8869
8897
|
useEffect(() => {
|
|
8870
8898
|
var _a;
|
|
8871
|
-
if (!((_a = iframeRef.current) == null ? void 0 : _a.contentWindow)
|
|
8899
|
+
if (!((_a = iframeRef.current) == null ? void 0 : _a.contentWindow)) {
|
|
8900
|
+
return;
|
|
8901
|
+
}
|
|
8902
|
+
if (!iframeLoaded && !iframeLoadedRef.current) {
|
|
8872
8903
|
return;
|
|
8873
8904
|
}
|
|
8874
8905
|
const initializeConnection = async () => {
|
|
@@ -8912,12 +8943,15 @@ var BotDojoChat = ({
|
|
|
8912
8943
|
if (msg.functionName === "chat_ready") {
|
|
8913
8944
|
console.log("[BotDojoChat] Chat is ready for flow requests");
|
|
8914
8945
|
chatReadyRef.current = true;
|
|
8915
|
-
|
|
8916
|
-
|
|
8917
|
-
|
|
8918
|
-
|
|
8919
|
-
|
|
8920
|
-
|
|
8946
|
+
if (!chatReadyFiredRef.current) {
|
|
8947
|
+
chatReadyFiredRef.current = true;
|
|
8948
|
+
const handlers2 = eventHandlersRef.current;
|
|
8949
|
+
if (handlers2.onReady) {
|
|
8950
|
+
try {
|
|
8951
|
+
handlers2.onReady();
|
|
8952
|
+
} catch (e) {
|
|
8953
|
+
console.error("[BotDojoChat] Error in onReady:", e);
|
|
8954
|
+
}
|
|
8921
8955
|
}
|
|
8922
8956
|
}
|
|
8923
8957
|
return;
|
|
@@ -9091,9 +9125,28 @@ var BotDojoChat = ({
|
|
|
9091
9125
|
}
|
|
9092
9126
|
};
|
|
9093
9127
|
const tryModelContext = async () => {
|
|
9094
|
-
|
|
9128
|
+
let waited = 0;
|
|
9129
|
+
const maxWait = 3e3;
|
|
9130
|
+
while (waited < maxWait) {
|
|
9131
|
+
const currentContext = modelContextRef.current;
|
|
9132
|
+
if (currentContext) {
|
|
9133
|
+
const contexts2 = Array.isArray(currentContext) ? currentContext : [currentContext];
|
|
9134
|
+
const allResources2 = contexts2.flatMap((ctx) => ctx.resources || []);
|
|
9135
|
+
const hasMatchingResource = allResources2.some((r) => {
|
|
9136
|
+
return (r == null ? void 0 : r.uri) === uri || (r == null ? void 0 : r.uriTemplate) && matchesTemplate(r.uriTemplate, uri);
|
|
9137
|
+
});
|
|
9138
|
+
if (hasMatchingResource) {
|
|
9139
|
+
console.log("[BotDojoChat] tryModelContext: Found matching resource after", waited, "ms");
|
|
9140
|
+
break;
|
|
9141
|
+
}
|
|
9142
|
+
}
|
|
9143
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
9144
|
+
waited += 50;
|
|
9145
|
+
}
|
|
9146
|
+
const currentModelContext = modelContextRef.current;
|
|
9147
|
+
if (!uri || !currentModelContext)
|
|
9095
9148
|
return { error: "no modelContext" };
|
|
9096
|
-
const contexts = Array.isArray(
|
|
9149
|
+
const contexts = Array.isArray(currentModelContext) ? currentModelContext : [currentModelContext];
|
|
9097
9150
|
const allResources = contexts.flatMap((ctx) => ctx.resources || []);
|
|
9098
9151
|
console.log("[BotDojoChat] tryModelContext: Searching", allResources.length, "resources for URI:", uri);
|
|
9099
9152
|
allResources.forEach((r, i2) => {
|
|
@@ -9107,7 +9160,7 @@ var BotDojoChat = ({
|
|
|
9107
9160
|
return uriMatch || templateMatch;
|
|
9108
9161
|
});
|
|
9109
9162
|
if (!match) {
|
|
9110
|
-
console.log("[BotDojoChat] tryModelContext: No match found");
|
|
9163
|
+
console.log("[BotDojoChat] tryModelContext: No match found after waiting", waited, "ms");
|
|
9111
9164
|
return { error: "resource not found in modelContext" };
|
|
9112
9165
|
}
|
|
9113
9166
|
console.log("[BotDojoChat] tryModelContext: MATCH FOUND:", match.uri);
|
|
@@ -9314,19 +9367,35 @@ var BotDojoChat = ({
|
|
|
9314
9367
|
}
|
|
9315
9368
|
});
|
|
9316
9369
|
connectionRef.current = connection;
|
|
9370
|
+
modelContextCapturedRef.current = !!modelContext2;
|
|
9317
9371
|
console.log("[BotDojoChat] RPC connection initialized");
|
|
9372
|
+
if (!chatReadyFiredRef.current) {
|
|
9373
|
+
console.log("[BotDojoChat] RPC ready, firing onReady");
|
|
9374
|
+
chatReadyRef.current = true;
|
|
9375
|
+
chatReadyFiredRef.current = true;
|
|
9376
|
+
rpcReadyRef.current = true;
|
|
9377
|
+
const handlers = eventHandlersRef.current;
|
|
9378
|
+
if (handlers.onReady) {
|
|
9379
|
+
try {
|
|
9380
|
+
handlers.onReady();
|
|
9381
|
+
} catch (e) {
|
|
9382
|
+
console.error("[BotDojoChat] Error in onReady:", e);
|
|
9383
|
+
}
|
|
9384
|
+
}
|
|
9385
|
+
}
|
|
9318
9386
|
} catch (error) {
|
|
9319
9387
|
console.error("[BotDojoChat] Failed to initialize RPC connection:", error);
|
|
9320
9388
|
}
|
|
9321
9389
|
};
|
|
9322
9390
|
initializeConnection();
|
|
9323
9391
|
return () => {
|
|
9392
|
+
modelContextCapturedRef.current = false;
|
|
9324
9393
|
if (connectionRef.current) {
|
|
9325
9394
|
connectionRef.current.close();
|
|
9326
9395
|
connectionRef.current = null;
|
|
9327
9396
|
}
|
|
9328
9397
|
};
|
|
9329
|
-
}, [connector, iframeLoaded, modelContext2, apiKey]);
|
|
9398
|
+
}, [connector, iframeLoaded, modelContext2, apiKey, rpcTrigger]);
|
|
9330
9399
|
const parseSize = useCallback((value2, defaultVal) => {
|
|
9331
9400
|
if (!value2 || value2 === "auto")
|
|
9332
9401
|
return defaultVal;
|
|
@@ -9429,6 +9498,7 @@ var BotDojoChat = ({
|
|
|
9429
9498
|
setIframeLoaded(true);
|
|
9430
9499
|
iframeLoadedRef.current = true;
|
|
9431
9500
|
chatReadyRef.current = false;
|
|
9501
|
+
chatReadyFiredRef.current = false;
|
|
9432
9502
|
setIsLoading(false);
|
|
9433
9503
|
onLoad == null ? void 0 : onLoad();
|
|
9434
9504
|
}, [onLoad]);
|