@botdojo/chat-sdk 0.0.18 → 0.0.20
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.d.ts +135 -102
- package/lib/index.js +349 -219
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -171,7 +171,7 @@ var require_tool_utils = __commonJS({
|
|
|
171
171
|
name: contextName,
|
|
172
172
|
description: (options == null ? void 0 : options.description) || `Tools for ${contextName}`,
|
|
173
173
|
toolPrefix: options == null ? void 0 : options.toolPrefix,
|
|
174
|
-
|
|
174
|
+
uri: (options == null ? void 0 : options.uri) || `app://${contextName}`,
|
|
175
175
|
tools: modelTools,
|
|
176
176
|
prompts: []
|
|
177
177
|
};
|
|
@@ -203,14 +203,14 @@ var require_tool_utils = __commonJS({
|
|
|
203
203
|
context = toolsToModelContext(name, tools, {
|
|
204
204
|
description,
|
|
205
205
|
toolPrefix,
|
|
206
|
-
|
|
206
|
+
uri
|
|
207
207
|
});
|
|
208
208
|
} else {
|
|
209
209
|
context = {
|
|
210
210
|
name,
|
|
211
211
|
description: description || `Model context for ${name}`,
|
|
212
212
|
toolPrefix,
|
|
213
|
-
|
|
213
|
+
uri,
|
|
214
214
|
tools: [],
|
|
215
215
|
prompts: []
|
|
216
216
|
};
|
|
@@ -408,7 +408,7 @@ var require_modelContextTranslation_snapshot = __commonJS({
|
|
|
408
408
|
name: context.name,
|
|
409
409
|
description: context.description,
|
|
410
410
|
toolPrefix: context.toolPrefix,
|
|
411
|
-
resourceUri: context.
|
|
411
|
+
resourceUri: context.uri,
|
|
412
412
|
tools,
|
|
413
413
|
prompts,
|
|
414
414
|
resources
|
|
@@ -8734,15 +8734,6 @@ var BotDojoChat = ({
|
|
|
8734
8734
|
return;
|
|
8735
8735
|
}
|
|
8736
8736
|
switch (data.type) {
|
|
8737
|
-
case "ready":
|
|
8738
|
-
if (handlers.onReady) {
|
|
8739
|
-
try {
|
|
8740
|
-
handlers.onReady();
|
|
8741
|
-
} catch (error) {
|
|
8742
|
-
console.error("[BotDojoChat] Error in onReady handler:", error);
|
|
8743
|
-
}
|
|
8744
|
-
}
|
|
8745
|
-
break;
|
|
8746
8737
|
case "error":
|
|
8747
8738
|
if (handlers.onError) {
|
|
8748
8739
|
try {
|
|
@@ -8921,19 +8912,18 @@ var BotDojoChat = ({
|
|
|
8921
8912
|
if (msg.functionName === "chat_ready") {
|
|
8922
8913
|
console.log("[BotDojoChat] Chat is ready for flow requests");
|
|
8923
8914
|
chatReadyRef.current = true;
|
|
8915
|
+
const handlers2 = eventHandlersRef.current;
|
|
8916
|
+
if (handlers2.onReady) {
|
|
8917
|
+
try {
|
|
8918
|
+
handlers2.onReady();
|
|
8919
|
+
} catch (e) {
|
|
8920
|
+
console.error("[BotDojoChat] Error in onReady:", e);
|
|
8921
|
+
}
|
|
8922
|
+
}
|
|
8924
8923
|
return;
|
|
8925
8924
|
}
|
|
8926
8925
|
const handlers = eventHandlersRef.current;
|
|
8927
8926
|
const eventData = (_a2 = msg.data) == null ? void 0 : _a2[0];
|
|
8928
|
-
if (msg.functionName === "ready" && handlers.onReady) {
|
|
8929
|
-
console.log("[BotDojoChat] Received ready event via RPC");
|
|
8930
|
-
try {
|
|
8931
|
-
handlers.onReady();
|
|
8932
|
-
} catch (e) {
|
|
8933
|
-
console.error("[BotDojoChat] Error in onReady:", e);
|
|
8934
|
-
}
|
|
8935
|
-
return;
|
|
8936
|
-
}
|
|
8937
8927
|
if (msg.functionName === "error" && handlers.onError) {
|
|
8938
8928
|
console.log("[BotDojoChat] Received error event via RPC:", eventData);
|
|
8939
8929
|
try {
|
|
@@ -9315,8 +9305,7 @@ var BotDojoChat = ({
|
|
|
9315
9305
|
name: ctx.name,
|
|
9316
9306
|
description: ctx.description,
|
|
9317
9307
|
toolPrefix: ctx.toolPrefix,
|
|
9318
|
-
|
|
9319
|
-
uri: ctx.uri || ctx.resourceUri,
|
|
9308
|
+
uri: ctx.uri,
|
|
9320
9309
|
tools,
|
|
9321
9310
|
prompts: ctx.prompts || [],
|
|
9322
9311
|
resources: ctx.resources || []
|
|
@@ -9995,7 +9984,24 @@ function findOrCreateStep(steps, stepId, parentStepId) {
|
|
|
9995
9984
|
return newStep;
|
|
9996
9985
|
}
|
|
9997
9986
|
function mergeStep(existing, update) {
|
|
9998
|
-
var _a;
|
|
9987
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
9988
|
+
let mergedCanvas = existing.canvas;
|
|
9989
|
+
if (update.canvas) {
|
|
9990
|
+
mergedCanvas = {
|
|
9991
|
+
...existing.canvas,
|
|
9992
|
+
...update.canvas,
|
|
9993
|
+
canvasData: {
|
|
9994
|
+
...(_a = existing.canvas) == null ? void 0 : _a.canvasData,
|
|
9995
|
+
...(_b = update.canvas) == null ? void 0 : _b.canvasData,
|
|
9996
|
+
// CRITICAL: Preserve html if the update doesn't have it
|
|
9997
|
+
// BotDojoChatProvider resolves ui:// URLs and adds html to canvasData
|
|
9998
|
+
// Subsequent step updates may not have html, so we must preserve it
|
|
9999
|
+
html: (_g = (_d = (_c = update.canvas) == null ? void 0 : _c.canvasData) == null ? void 0 : _d.html) != null ? _g : (_f = (_e = existing.canvas) == null ? void 0 : _e.canvasData) == null ? void 0 : _f.html,
|
|
10000
|
+
// Also preserve url if not in update
|
|
10001
|
+
url: (_l = (_i = (_h = update.canvas) == null ? void 0 : _h.canvasData) == null ? void 0 : _i.url) != null ? _l : (_k = (_j = existing.canvas) == null ? void 0 : _j.canvasData) == null ? void 0 : _k.url
|
|
10002
|
+
}
|
|
10003
|
+
};
|
|
10004
|
+
}
|
|
9999
10005
|
return {
|
|
10000
10006
|
...existing,
|
|
10001
10007
|
...update,
|
|
@@ -10003,9 +10009,7 @@ function mergeStep(existing, update) {
|
|
|
10003
10009
|
stepId: existing.stepId,
|
|
10004
10010
|
startTime: existing.startTime,
|
|
10005
10011
|
subSteps: update.subSteps || existing.subSteps,
|
|
10006
|
-
|
|
10007
|
-
// This allows notifyToolInputPartial to send progress updates without destroying the canvas
|
|
10008
|
-
canvas: (_a = update.canvas) != null ? _a : existing.canvas
|
|
10012
|
+
canvas: mergedCanvas
|
|
10009
10013
|
};
|
|
10010
10014
|
}
|
|
10011
10015
|
function convertCoreMessage(msg) {
|
|
@@ -10223,6 +10227,7 @@ function BotDojoChatProvider(props) {
|
|
|
10223
10227
|
const pendingToolNotificationsRef = useRef(/* @__PURE__ */ new Map());
|
|
10224
10228
|
const pendingBroadcastNotificationsRef = useRef([]);
|
|
10225
10229
|
const toolArgumentsCacheRef = useRef(/* @__PURE__ */ new Map());
|
|
10230
|
+
const mcpAppDataCacheRef = useRef(/* @__PURE__ */ new Map());
|
|
10226
10231
|
const pendingStepUpdatesRef = useRef(/* @__PURE__ */ new Map());
|
|
10227
10232
|
const appHandlersRef = useRef({
|
|
10228
10233
|
onOpenLink,
|
|
@@ -10473,12 +10478,30 @@ function BotDojoChatProvider(props) {
|
|
|
10473
10478
|
}
|
|
10474
10479
|
}
|
|
10475
10480
|
};
|
|
10476
|
-
|
|
10481
|
+
const result = await tool2.execute(args, executionContext);
|
|
10482
|
+
if (result !== void 0) {
|
|
10483
|
+
await executionContext.notifyToolResult(result);
|
|
10484
|
+
}
|
|
10485
|
+
return result;
|
|
10477
10486
|
}
|
|
10478
10487
|
}
|
|
10479
10488
|
}
|
|
10480
10489
|
throw new Error(`Tool not found: ${toolName}`);
|
|
10481
10490
|
}, []);
|
|
10491
|
+
const getMcpAppData = useCallback((appId) => {
|
|
10492
|
+
const cached = mcpAppDataCacheRef.current.get(appId);
|
|
10493
|
+
if (!cached)
|
|
10494
|
+
return null;
|
|
10495
|
+
return {
|
|
10496
|
+
mcpAppId: appId,
|
|
10497
|
+
mcpAppType: "mcp-app",
|
|
10498
|
+
...cached
|
|
10499
|
+
};
|
|
10500
|
+
}, []);
|
|
10501
|
+
const updateMcpAppDataCache = useCallback((appId, data) => {
|
|
10502
|
+
const existing = mcpAppDataCacheRef.current.get(appId) || {};
|
|
10503
|
+
mcpAppDataCacheRef.current.set(appId, { ...existing, ...data });
|
|
10504
|
+
}, []);
|
|
10482
10505
|
const getResource = useCallback(async (uri, params, onlyMetadata) => {
|
|
10483
10506
|
console.log("[BotDojoChatProvider] getResource called for URI:", uri);
|
|
10484
10507
|
const ctx = modelContextRef.current;
|
|
@@ -10741,7 +10764,7 @@ function BotDojoChatProvider(props) {
|
|
|
10741
10764
|
}
|
|
10742
10765
|
}
|
|
10743
10766
|
(async () => {
|
|
10744
|
-
var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
10767
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
10745
10768
|
let stepToDispatch = rawStep;
|
|
10746
10769
|
const canvasUrl = (_b = (_a2 = rawStep == null ? void 0 : rawStep.canvas) == null ? void 0 : _a2.canvasData) == null ? void 0 : _b.url;
|
|
10747
10770
|
if (canvasUrl && canvasUrl.startsWith("ui://") && !((_d = (_c = rawStep == null ? void 0 : rawStep.canvas) == null ? void 0 : _c.canvasData) == null ? void 0 : _d.html)) {
|
|
@@ -10783,6 +10806,17 @@ ${htmlContent}`;
|
|
|
10783
10806
|
}
|
|
10784
10807
|
};
|
|
10785
10808
|
console.log("[BotDojoChatProvider] \u2705 Resolved ui:// resource to HTML (" + htmlContent.length + " chars)");
|
|
10809
|
+
const appIdForCache = rawStep.canvas.canvasId;
|
|
10810
|
+
if (appIdForCache) {
|
|
10811
|
+
const toolNameForCache = rawStep.stepToolName || rawStep.stepLabel;
|
|
10812
|
+
updateMcpAppDataCache(appIdForCache, {
|
|
10813
|
+
html: htmlContent,
|
|
10814
|
+
url: canvasUrl,
|
|
10815
|
+
toolInfo: toolNameForCache ? { tool: { name: toolNameForCache } } : void 0,
|
|
10816
|
+
height: (_e = rawStep.canvas.canvasData) == null ? void 0 : _e.height,
|
|
10817
|
+
width: (_f = rawStep.canvas.canvasData) == null ? void 0 : _f.width
|
|
10818
|
+
});
|
|
10819
|
+
}
|
|
10786
10820
|
} else {
|
|
10787
10821
|
console.warn("[BotDojoChatProvider] \u26A0\uFE0F Resource resolved but no text/content found:", Object.keys(resource || {}));
|
|
10788
10822
|
}
|
|
@@ -10796,19 +10830,33 @@ ${htmlContent}`;
|
|
|
10796
10830
|
messageId: data.messageId,
|
|
10797
10831
|
step: stepToDispatch
|
|
10798
10832
|
});
|
|
10799
|
-
const appIdFromCanvas = (
|
|
10833
|
+
const appIdFromCanvas = (_g = stepToDispatch == null ? void 0 : stepToDispatch.canvas) == null ? void 0 : _g.canvasId;
|
|
10800
10834
|
const appIdFromStepId = (stepToDispatch == null ? void 0 : stepToDispatch.stepId) ? stepIdToAppIdRef.current.get(stepToDispatch.stepId) : void 0;
|
|
10801
10835
|
const toolNameForLookup = (stepToDispatch == null ? void 0 : stepToDispatch.stepToolName) || (stepToDispatch == null ? void 0 : stepToDispatch.stepLabel);
|
|
10802
10836
|
const appIdFromToolName = toolNameForLookup ? activeAppByToolRef.current.get(toolNameForLookup) : void 0;
|
|
10803
10837
|
const appId = appIdFromCanvas || appIdFromStepId || appIdFromToolName;
|
|
10804
10838
|
if (appId) {
|
|
10805
10839
|
const toolName = stepToDispatch.stepToolName || stepToDispatch.stepLabel;
|
|
10806
|
-
const
|
|
10840
|
+
const parseIfJson = (value2) => {
|
|
10841
|
+
if (value2 === void 0 || value2 === null || value2 === "")
|
|
10842
|
+
return void 0;
|
|
10843
|
+
if (typeof value2 === "object")
|
|
10844
|
+
return value2;
|
|
10845
|
+
if (typeof value2 === "string" && value2.trim()) {
|
|
10846
|
+
try {
|
|
10847
|
+
return JSON.parse(value2);
|
|
10848
|
+
} catch {
|
|
10849
|
+
return value2;
|
|
10850
|
+
}
|
|
10851
|
+
}
|
|
10852
|
+
return value2;
|
|
10853
|
+
};
|
|
10854
|
+
const canvasResult = parseIfJson((_i = (_h = stepToDispatch == null ? void 0 : stepToDispatch.canvas) == null ? void 0 : _h.canvasData) == null ? void 0 : _i.result);
|
|
10807
10855
|
const rawStepResult = stepToDispatch.stepToolResult;
|
|
10808
|
-
const stepResult = rawStepResult !== void 0 && rawStepResult !== ""
|
|
10856
|
+
const stepResult = rawStepResult !== void 0 && rawStepResult !== "" ? parseIfJson(rawStepResult) : canvasResult !== void 0 ? canvasResult : parseIfJson(stepToDispatch.content);
|
|
10809
10857
|
const rawStepArgs = stepToDispatch.stepToolArguments;
|
|
10810
10858
|
const rawArgsString = stepToDispatch.arguments;
|
|
10811
|
-
const canvasArgs = (
|
|
10859
|
+
const canvasArgs = (_k = (_j = stepToDispatch == null ? void 0 : stepToDispatch.canvas) == null ? void 0 : _j.canvasData) == null ? void 0 : _k.arguments;
|
|
10812
10860
|
let stepArguments = rawStepArgs && typeof rawStepArgs === "object" ? rawStepArgs : null;
|
|
10813
10861
|
if (!stepArguments && rawArgsString && rawArgsString.trim()) {
|
|
10814
10862
|
try {
|
|
@@ -10835,6 +10883,12 @@ ${htmlContent}`;
|
|
|
10835
10883
|
stepToolArguments: stepArguments != null ? stepArguments : void 0,
|
|
10836
10884
|
stepToolResult: stepResult
|
|
10837
10885
|
};
|
|
10886
|
+
updateMcpAppDataCache(appId, {
|
|
10887
|
+
...stepArguments ? { arguments: stepArguments } : {},
|
|
10888
|
+
...stepResult !== void 0 ? { result: stepResult } : {},
|
|
10889
|
+
...stepToDispatch.stepStatus === "complete" ? { isComplete: true } : {},
|
|
10890
|
+
...toolName ? { toolInfo: { tool: { name: toolName } } } : {}
|
|
10891
|
+
});
|
|
10838
10892
|
const callback = stepUpdateCallbacksRef.current.get(appId);
|
|
10839
10893
|
if (callback) {
|
|
10840
10894
|
console.log(`[BotDojoChatProvider] Sending step update via callback to app: ${appId}`, {
|
|
@@ -10896,9 +10950,10 @@ ${htmlContent}`;
|
|
|
10896
10950
|
sendToolNotificationToApp,
|
|
10897
10951
|
registerStepUpdateCallback,
|
|
10898
10952
|
sendStepUpdateToApp,
|
|
10953
|
+
getMcpAppData,
|
|
10899
10954
|
appHandlers: appHandlersRef
|
|
10900
10955
|
}),
|
|
10901
|
-
[state, connector, registerMcpApp, unregisterMcpApp, registeredMcpApps, registerToolNotificationSender, sendToolNotificationToApp, registerStepUpdateCallback, sendStepUpdateToApp]
|
|
10956
|
+
[state, connector, registerMcpApp, unregisterMcpApp, registeredMcpApps, registerToolNotificationSender, sendToolNotificationToApp, registerStepUpdateCallback, sendStepUpdateToApp, getMcpAppData]
|
|
10902
10957
|
);
|
|
10903
10958
|
return /* @__PURE__ */ jsxs(BotDojoChatContext.Provider, { value: contextValue, children: [
|
|
10904
10959
|
children,
|
|
@@ -11198,10 +11253,20 @@ function useMcpAppHostRequired() {
|
|
|
11198
11253
|
}
|
|
11199
11254
|
return context;
|
|
11200
11255
|
}
|
|
11256
|
+
function getContentKey(appHtml, appUrl) {
|
|
11257
|
+
if (appUrl)
|
|
11258
|
+
return `url:${appUrl}`;
|
|
11259
|
+
if (appHtml) {
|
|
11260
|
+
const prefix = appHtml.slice(0, 100);
|
|
11261
|
+
const suffix = appHtml.slice(-100);
|
|
11262
|
+
return `html:${appHtml.length}:${prefix}:${suffix}`;
|
|
11263
|
+
}
|
|
11264
|
+
return "empty";
|
|
11265
|
+
}
|
|
11201
11266
|
function McpAppHost(props) {
|
|
11267
|
+
var _a;
|
|
11202
11268
|
const {
|
|
11203
11269
|
mcpAppId,
|
|
11204
|
-
mcpAppData,
|
|
11205
11270
|
onOpenLink,
|
|
11206
11271
|
onToolCall,
|
|
11207
11272
|
onUiMessage,
|
|
@@ -11215,8 +11280,7 @@ function McpAppHost(props) {
|
|
|
11215
11280
|
debug = false
|
|
11216
11281
|
} = props;
|
|
11217
11282
|
const iframeRef = useRef(null);
|
|
11218
|
-
const [
|
|
11219
|
-
const [iframeLoaded, setIframeLoaded] = useState(false);
|
|
11283
|
+
const [iframeState, setIframeState] = useState("idle");
|
|
11220
11284
|
const [autoHeight, setAutoHeight] = useState(null);
|
|
11221
11285
|
const pendingRequestsRef = useRef(/* @__PURE__ */ new Map());
|
|
11222
11286
|
const nextRequestIdRef = useRef(0);
|
|
@@ -11225,17 +11289,20 @@ function McpAppHost(props) {
|
|
|
11225
11289
|
const mcpAppHostContext = useMcpAppHost();
|
|
11226
11290
|
const chatContext = useContext(BotDojoChatContext);
|
|
11227
11291
|
const pendingNotificationsRef = useRef([]);
|
|
11228
|
-
const
|
|
11292
|
+
const mcpAppData = (_a = chatContext == null ? void 0 : chatContext.getMcpAppData) == null ? void 0 : _a.call(chatContext, mcpAppId);
|
|
11229
11293
|
const appUrl = mcpAppData == null ? void 0 : mcpAppData.url;
|
|
11230
11294
|
const appHtml = mcpAppData == null ? void 0 : mcpAppData.html;
|
|
11295
|
+
const contentKey = useMemo(() => getContentKey(appHtml, appUrl), [appHtml, appUrl]);
|
|
11296
|
+
const renderedContentKeyRef = useRef("");
|
|
11297
|
+
const currentBlobUrlRef = useRef(null);
|
|
11231
11298
|
const log = useCallback((...args) => {
|
|
11232
11299
|
if (debug) {
|
|
11233
11300
|
console.log(`[McpAppHost:${mcpAppId}]`, ...args);
|
|
11234
11301
|
}
|
|
11235
11302
|
}, [debug, mcpAppId]);
|
|
11236
11303
|
const sendResponse = useCallback((id, result) => {
|
|
11237
|
-
var
|
|
11238
|
-
if (!((
|
|
11304
|
+
var _a2;
|
|
11305
|
+
if (!((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
|
|
11239
11306
|
return;
|
|
11240
11307
|
iframeRef.current.contentWindow.postMessage(
|
|
11241
11308
|
{ jsonrpc: "2.0", id, result },
|
|
@@ -11243,8 +11310,8 @@ function McpAppHost(props) {
|
|
|
11243
11310
|
);
|
|
11244
11311
|
}, []);
|
|
11245
11312
|
const sendError = useCallback((id, code, message) => {
|
|
11246
|
-
var
|
|
11247
|
-
if (!((
|
|
11313
|
+
var _a2;
|
|
11314
|
+
if (!((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
|
|
11248
11315
|
return;
|
|
11249
11316
|
iframeRef.current.contentWindow.postMessage(
|
|
11250
11317
|
{ jsonrpc: "2.0", id, error: { code, message } },
|
|
@@ -11252,8 +11319,8 @@ function McpAppHost(props) {
|
|
|
11252
11319
|
);
|
|
11253
11320
|
}, []);
|
|
11254
11321
|
const sendNotification = useCallback((method, params) => {
|
|
11255
|
-
var
|
|
11256
|
-
if (!((
|
|
11322
|
+
var _a2;
|
|
11323
|
+
if (!((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
|
|
11257
11324
|
return;
|
|
11258
11325
|
log("Sending notification:", method, params);
|
|
11259
11326
|
iframeRef.current.contentWindow.postMessage(
|
|
@@ -11263,8 +11330,8 @@ function McpAppHost(props) {
|
|
|
11263
11330
|
}, [log]);
|
|
11264
11331
|
const sendRequest = useCallback((method, params, timeout = 5e3) => {
|
|
11265
11332
|
return new Promise((resolve, reject) => {
|
|
11266
|
-
var
|
|
11267
|
-
if (!((
|
|
11333
|
+
var _a2;
|
|
11334
|
+
if (!((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow)) {
|
|
11268
11335
|
reject(new Error("Iframe not ready"));
|
|
11269
11336
|
return;
|
|
11270
11337
|
}
|
|
@@ -11282,11 +11349,10 @@ function McpAppHost(props) {
|
|
|
11282
11349
|
});
|
|
11283
11350
|
}, [log]);
|
|
11284
11351
|
const sendInitialize = useCallback(() => {
|
|
11285
|
-
var
|
|
11286
|
-
if (
|
|
11352
|
+
var _a2;
|
|
11353
|
+
if (iframeStateRef.current === "ready" || !((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
|
|
11287
11354
|
return;
|
|
11288
11355
|
const hostContext = {
|
|
11289
|
-
// Include mcpAppId so app knows its canvas ID for message routing
|
|
11290
11356
|
mcpAppId,
|
|
11291
11357
|
state: mcpAppData == null ? void 0 : mcpAppData.state,
|
|
11292
11358
|
toolInfo: mcpAppData == null ? void 0 : mcpAppData.toolInfo,
|
|
@@ -11310,12 +11376,14 @@ function McpAppHost(props) {
|
|
|
11310
11376
|
}).catch((err) => {
|
|
11311
11377
|
log("ui/initialize failed:", err.message);
|
|
11312
11378
|
});
|
|
11313
|
-
}, [
|
|
11379
|
+
}, [mcpAppId, mcpAppData, initialHostContext, log, sendRequest]);
|
|
11380
|
+
const iframeStateRef = useRef(iframeState);
|
|
11381
|
+
iframeStateRef.current = iframeState;
|
|
11314
11382
|
const sendInitializeWithRetry = useCallback(() => {
|
|
11315
|
-
if (
|
|
11383
|
+
if (iframeStateRef.current === "ready")
|
|
11316
11384
|
return;
|
|
11317
11385
|
const attemptSend = () => {
|
|
11318
|
-
if (
|
|
11386
|
+
if (iframeStateRef.current === "ready")
|
|
11319
11387
|
return;
|
|
11320
11388
|
initializeAttemptsRef.current += 1;
|
|
11321
11389
|
sendInitialize();
|
|
@@ -11331,16 +11399,63 @@ function McpAppHost(props) {
|
|
|
11331
11399
|
}
|
|
11332
11400
|
initializeAttemptsRef.current = 0;
|
|
11333
11401
|
attemptSend();
|
|
11334
|
-
}, [
|
|
11402
|
+
}, [sendInitialize, log]);
|
|
11403
|
+
const sendHydrationData = useCallback(() => {
|
|
11404
|
+
var _a2, _b, _c;
|
|
11405
|
+
const toolName = (_b = (_a2 = mcpAppData == null ? void 0 : mcpAppData.toolInfo) == null ? void 0 : _a2.tool) == null ? void 0 : _b.name;
|
|
11406
|
+
if (!toolName || !((_c = iframeRef.current) == null ? void 0 : _c.contentWindow))
|
|
11407
|
+
return;
|
|
11408
|
+
const parseMaybeJson2 = (value2) => {
|
|
11409
|
+
if (value2 === void 0 || value2 === null || value2 === "")
|
|
11410
|
+
return void 0;
|
|
11411
|
+
if (typeof value2 === "object")
|
|
11412
|
+
return value2;
|
|
11413
|
+
if (typeof value2 === "string" && value2.trim()) {
|
|
11414
|
+
try {
|
|
11415
|
+
return JSON.parse(value2);
|
|
11416
|
+
} catch {
|
|
11417
|
+
return value2;
|
|
11418
|
+
}
|
|
11419
|
+
}
|
|
11420
|
+
return value2;
|
|
11421
|
+
};
|
|
11422
|
+
const args = parseMaybeJson2(mcpAppData == null ? void 0 : mcpAppData.arguments);
|
|
11423
|
+
const result = parseMaybeJson2(mcpAppData == null ? void 0 : mcpAppData.result);
|
|
11424
|
+
const toolIsComplete = mcpAppData == null ? void 0 : mcpAppData.isComplete;
|
|
11425
|
+
const hasValidArgs = args !== void 0 && typeof args === "object" && Object.keys(args).length > 0;
|
|
11426
|
+
const hasValidResult = result !== void 0 && result !== "";
|
|
11427
|
+
log("Sending hydration data:", { toolName, hasArgs: hasValidArgs, hasResult: hasValidResult });
|
|
11428
|
+
if (hasValidArgs) {
|
|
11429
|
+
iframeRef.current.contentWindow.postMessage({
|
|
11430
|
+
jsonrpc: "2.0",
|
|
11431
|
+
method: "ui/notifications/tool-input",
|
|
11432
|
+
params: { tool: { name: toolName }, arguments: args }
|
|
11433
|
+
}, "*");
|
|
11434
|
+
}
|
|
11435
|
+
if (hasValidResult) {
|
|
11436
|
+
iframeRef.current.contentWindow.postMessage({
|
|
11437
|
+
jsonrpc: "2.0",
|
|
11438
|
+
method: "ui/notifications/tool-result",
|
|
11439
|
+
params: { tool: { name: toolName }, result }
|
|
11440
|
+
}, "*");
|
|
11441
|
+
} else if (toolIsComplete) {
|
|
11442
|
+
log("Sending synthetic tool-result for completed tool without stored result");
|
|
11443
|
+
iframeRef.current.contentWindow.postMessage({
|
|
11444
|
+
jsonrpc: "2.0",
|
|
11445
|
+
method: "ui/notifications/tool-result",
|
|
11446
|
+
params: { tool: { name: toolName }, result: { completed: true } }
|
|
11447
|
+
}, "*");
|
|
11448
|
+
}
|
|
11449
|
+
}, [mcpAppData, log]);
|
|
11335
11450
|
const handleMessage = useCallback((event) => {
|
|
11336
|
-
var
|
|
11451
|
+
var _a2;
|
|
11337
11452
|
if (!iframeRef.current || event.source !== iframeRef.current.contentWindow) {
|
|
11338
11453
|
return;
|
|
11339
11454
|
}
|
|
11340
11455
|
if (mcpAppHostContext == null ? void 0 : mcpAppHostContext.findAppIdBySource) {
|
|
11341
11456
|
const sourceAppId = mcpAppHostContext.findAppIdBySource(event.source);
|
|
11342
11457
|
if (sourceAppId && sourceAppId !== mcpAppId) {
|
|
11343
|
-
log("Message from different app, ignoring:", sourceAppId
|
|
11458
|
+
log("Message from different app, ignoring:", sourceAppId);
|
|
11344
11459
|
return;
|
|
11345
11460
|
}
|
|
11346
11461
|
}
|
|
@@ -11370,55 +11485,9 @@ function McpAppHost(props) {
|
|
|
11370
11485
|
clearTimeout(initializeTimerRef.current);
|
|
11371
11486
|
initializeTimerRef.current = null;
|
|
11372
11487
|
}
|
|
11373
|
-
|
|
11374
|
-
|
|
11375
|
-
|
|
11376
|
-
if (toolName && ((_c = iframeRef.current) == null ? void 0 : _c.contentWindow)) {
|
|
11377
|
-
const rawArgs = mcpAppData == null ? void 0 : mcpAppData.arguments;
|
|
11378
|
-
const rawResult = mcpAppData == null ? void 0 : mcpAppData.result;
|
|
11379
|
-
const args = rawArgs && rawArgs !== "" && typeof rawArgs === "object" ? rawArgs : void 0;
|
|
11380
|
-
const result = rawResult !== void 0 && rawResult !== "" ? rawResult : void 0;
|
|
11381
|
-
const toolIsComplete = mcpAppData == null ? void 0 : mcpAppData.isComplete;
|
|
11382
|
-
log("Sending hydration data after initialized:", {
|
|
11383
|
-
toolName,
|
|
11384
|
-
hasArgs: args !== void 0,
|
|
11385
|
-
hasResult: result !== void 0,
|
|
11386
|
-
isComplete: toolIsComplete,
|
|
11387
|
-
rawArgs,
|
|
11388
|
-
rawResult
|
|
11389
|
-
});
|
|
11390
|
-
if (args !== void 0) {
|
|
11391
|
-
iframeRef.current.contentWindow.postMessage({
|
|
11392
|
-
jsonrpc: "2.0",
|
|
11393
|
-
method: "ui/notifications/tool-input",
|
|
11394
|
-
params: {
|
|
11395
|
-
tool: { name: toolName },
|
|
11396
|
-
arguments: args
|
|
11397
|
-
}
|
|
11398
|
-
}, "*");
|
|
11399
|
-
}
|
|
11400
|
-
if (result !== void 0) {
|
|
11401
|
-
iframeRef.current.contentWindow.postMessage({
|
|
11402
|
-
jsonrpc: "2.0",
|
|
11403
|
-
method: "ui/notifications/tool-result",
|
|
11404
|
-
params: {
|
|
11405
|
-
tool: { name: toolName },
|
|
11406
|
-
result
|
|
11407
|
-
}
|
|
11408
|
-
}, "*");
|
|
11409
|
-
} else if (toolIsComplete) {
|
|
11410
|
-
log("Sending synthetic tool-result for completed tool without stored result");
|
|
11411
|
-
iframeRef.current.contentWindow.postMessage({
|
|
11412
|
-
jsonrpc: "2.0",
|
|
11413
|
-
method: "ui/notifications/tool-result",
|
|
11414
|
-
params: {
|
|
11415
|
-
tool: { name: toolName },
|
|
11416
|
-
result: { completed: true }
|
|
11417
|
-
}
|
|
11418
|
-
}, "*");
|
|
11419
|
-
}
|
|
11420
|
-
}
|
|
11421
|
-
if (pendingNotificationsRef.current.length > 0 && ((_d = iframeRef.current) == null ? void 0 : _d.contentWindow)) {
|
|
11488
|
+
setIframeState("ready");
|
|
11489
|
+
sendHydrationData();
|
|
11490
|
+
if (pendingNotificationsRef.current.length > 0 && ((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow)) {
|
|
11422
11491
|
log(`Replaying ${pendingNotificationsRef.current.length} queued notifications`);
|
|
11423
11492
|
pendingNotificationsRef.current.forEach(({ method: method2, params: params2 }) => {
|
|
11424
11493
|
log("Replaying notification:", method2, params2);
|
|
@@ -11455,7 +11524,7 @@ function McpAppHost(props) {
|
|
|
11455
11524
|
clearTimeout(initializeTimerRef.current);
|
|
11456
11525
|
initializeTimerRef.current = null;
|
|
11457
11526
|
}
|
|
11458
|
-
|
|
11527
|
+
setIframeState("ready");
|
|
11459
11528
|
onReady == null ? void 0 : onReady(mcpAppId);
|
|
11460
11529
|
break;
|
|
11461
11530
|
}
|
|
@@ -11496,15 +11565,12 @@ function McpAppHost(props) {
|
|
|
11496
11565
|
const contentArray = Array.isArray(params == null ? void 0 : params.content) ? params.content : [params == null ? void 0 : params.content].filter(Boolean);
|
|
11497
11566
|
const firstContent = contentArray[0];
|
|
11498
11567
|
if ((firstContent == null ? void 0 : firstContent["botdojo/messageType"]) === "persist-state" && typeof firstContent.text === "string") {
|
|
11499
|
-
log("Persist state message received (format 1
|
|
11568
|
+
log("Persist state message received (format 1)");
|
|
11500
11569
|
try {
|
|
11501
11570
|
const parsed = JSON.parse(firstContent.text);
|
|
11502
|
-
log("Parsed state to persist:", parsed);
|
|
11503
11571
|
const persistHandler = onPersistState || (mcpAppHostContext == null ? void 0 : mcpAppHostContext.onPersistState);
|
|
11504
11572
|
if (persistHandler) {
|
|
11505
11573
|
persistHandler(parsed, mcpAppId);
|
|
11506
|
-
} else {
|
|
11507
|
-
log("No onPersistState handler provided - state will not be saved");
|
|
11508
11574
|
}
|
|
11509
11575
|
} catch (err) {
|
|
11510
11576
|
log("Failed to parse persist-state content:", err);
|
|
@@ -11515,13 +11581,10 @@ function McpAppHost(props) {
|
|
|
11515
11581
|
break;
|
|
11516
11582
|
}
|
|
11517
11583
|
if ((firstContent == null ? void 0 : firstContent.type) === "botdojo/persist" && firstContent.state) {
|
|
11518
|
-
log("Persist state message received (format 2/3
|
|
11519
|
-
log("State to persist:", firstContent.state);
|
|
11584
|
+
log("Persist state message received (format 2/3)");
|
|
11520
11585
|
const persistHandler = onPersistState || (mcpAppHostContext == null ? void 0 : mcpAppHostContext.onPersistState);
|
|
11521
11586
|
if (persistHandler) {
|
|
11522
11587
|
persistHandler(firstContent.state, mcpAppId);
|
|
11523
|
-
} else {
|
|
11524
|
-
log("No onPersistState handler provided - state will not be saved");
|
|
11525
11588
|
}
|
|
11526
11589
|
if (id !== void 0) {
|
|
11527
11590
|
sendResponse(id, { ok: true });
|
|
@@ -11569,21 +11632,72 @@ function McpAppHost(props) {
|
|
|
11569
11632
|
log,
|
|
11570
11633
|
sendResponse,
|
|
11571
11634
|
sendError,
|
|
11572
|
-
sendNotification
|
|
11635
|
+
sendNotification,
|
|
11636
|
+
sendHydrationData
|
|
11573
11637
|
]);
|
|
11574
11638
|
useEffect(() => {
|
|
11575
11639
|
window.addEventListener("message", handleMessage);
|
|
11576
11640
|
return () => window.removeEventListener("message", handleMessage);
|
|
11577
11641
|
}, [handleMessage]);
|
|
11578
11642
|
useEffect(() => {
|
|
11579
|
-
if (
|
|
11643
|
+
if (!iframeRef.current)
|
|
11644
|
+
return;
|
|
11645
|
+
if (contentKey === renderedContentKeyRef.current && iframeState !== "idle") {
|
|
11646
|
+
log("Content key unchanged, keeping existing iframe");
|
|
11647
|
+
return;
|
|
11648
|
+
}
|
|
11649
|
+
if (contentKey === "empty") {
|
|
11650
|
+
if (iframeState === "ready" && iframeRef.current.src) {
|
|
11651
|
+
log("Content became empty but iframe is ready, preserving");
|
|
11652
|
+
renderedContentKeyRef.current = contentKey;
|
|
11653
|
+
return;
|
|
11654
|
+
}
|
|
11655
|
+
if (iframeState !== "idle" && iframeRef.current.src) {
|
|
11656
|
+
log("Content became empty but iframe is loading/initializing, preserving");
|
|
11657
|
+
renderedContentKeyRef.current = contentKey;
|
|
11658
|
+
return;
|
|
11659
|
+
}
|
|
11660
|
+
if (iframeState === "idle" && !iframeRef.current.src) {
|
|
11661
|
+
log("Content is empty and iframe is idle with no src, skipping");
|
|
11662
|
+
renderedContentKeyRef.current = contentKey;
|
|
11663
|
+
return;
|
|
11664
|
+
}
|
|
11665
|
+
}
|
|
11666
|
+
log("Content key changed:", { old: renderedContentKeyRef.current, new: contentKey });
|
|
11667
|
+
renderedContentKeyRef.current = contentKey;
|
|
11668
|
+
setIframeState("idle");
|
|
11669
|
+
setAutoHeight(null);
|
|
11670
|
+
initializeAttemptsRef.current = 0;
|
|
11671
|
+
pendingNotificationsRef.current = [];
|
|
11672
|
+
if (initializeTimerRef.current !== null) {
|
|
11673
|
+
clearTimeout(initializeTimerRef.current);
|
|
11674
|
+
initializeTimerRef.current = null;
|
|
11675
|
+
}
|
|
11676
|
+
if (currentBlobUrlRef.current) {
|
|
11677
|
+
URL.revokeObjectURL(currentBlobUrlRef.current);
|
|
11678
|
+
currentBlobUrlRef.current = null;
|
|
11679
|
+
}
|
|
11680
|
+
if (appHtml) {
|
|
11681
|
+
const blob = new Blob([appHtml], { type: "text/html" });
|
|
11682
|
+
const url2 = URL.createObjectURL(blob);
|
|
11683
|
+
currentBlobUrlRef.current = url2;
|
|
11684
|
+
iframeRef.current.src = url2;
|
|
11685
|
+
log("Set iframe src to blob URL");
|
|
11686
|
+
} else if (appUrl) {
|
|
11687
|
+
iframeRef.current.src = appUrl;
|
|
11688
|
+
log("Set iframe src to URL:", appUrl);
|
|
11689
|
+
}
|
|
11690
|
+
}, [contentKey, appHtml, appUrl, iframeState, log]);
|
|
11691
|
+
useEffect(() => {
|
|
11692
|
+
if (iframeState === "loading") {
|
|
11580
11693
|
log("Iframe loaded, sending ui/initialize");
|
|
11694
|
+
setIframeState("initializing");
|
|
11581
11695
|
sendInitializeWithRetry();
|
|
11582
11696
|
}
|
|
11583
|
-
}, [
|
|
11697
|
+
}, [iframeState, sendInitializeWithRetry, log]);
|
|
11584
11698
|
const prevStateRef = useRef(void 0);
|
|
11585
11699
|
useEffect(() => {
|
|
11586
|
-
if (
|
|
11700
|
+
if (iframeState !== "ready") {
|
|
11587
11701
|
prevStateRef.current = mcpAppData == null ? void 0 : mcpAppData.state;
|
|
11588
11702
|
return;
|
|
11589
11703
|
}
|
|
@@ -11591,7 +11705,7 @@ function McpAppHost(props) {
|
|
|
11591
11705
|
const prevState = prevStateRef.current;
|
|
11592
11706
|
const stateChanged = JSON.stringify(currentState) !== JSON.stringify(prevState);
|
|
11593
11707
|
if (stateChanged && currentState !== void 0) {
|
|
11594
|
-
log("State changed after initialization, sending host-context-changed"
|
|
11708
|
+
log("State changed after initialization, sending host-context-changed");
|
|
11595
11709
|
sendNotification("ui/notifications/host-context-changed", {
|
|
11596
11710
|
state: currentState,
|
|
11597
11711
|
toolInfo: mcpAppData == null ? void 0 : mcpAppData.toolInfo,
|
|
@@ -11599,14 +11713,14 @@ function McpAppHost(props) {
|
|
|
11599
11713
|
});
|
|
11600
11714
|
prevStateRef.current = currentState;
|
|
11601
11715
|
}
|
|
11602
|
-
}, [
|
|
11716
|
+
}, [iframeState, mcpAppData == null ? void 0 : mcpAppData.state, mcpAppData == null ? void 0 : mcpAppData.toolInfo, initialHostContext, sendNotification, log]);
|
|
11603
11717
|
const handleIframeLoad = useCallback(() => {
|
|
11604
11718
|
log("Iframe onLoad fired");
|
|
11605
|
-
|
|
11719
|
+
setIframeState((prev2) => prev2 === "idle" ? "loading" : prev2);
|
|
11606
11720
|
}, [log]);
|
|
11607
11721
|
useEffect(() => {
|
|
11608
|
-
var
|
|
11609
|
-
if (mcpAppHostContext && ((
|
|
11722
|
+
var _a2;
|
|
11723
|
+
if (mcpAppHostContext && ((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow)) {
|
|
11610
11724
|
mcpAppHostContext.registerApp(mcpAppId, iframeRef.current.contentWindow);
|
|
11611
11725
|
return () => mcpAppHostContext.unregisterApp(mcpAppId);
|
|
11612
11726
|
}
|
|
@@ -11616,38 +11730,26 @@ function McpAppHost(props) {
|
|
|
11616
11730
|
useEffect(() => {
|
|
11617
11731
|
const ctx = chatContextRef.current;
|
|
11618
11732
|
if (!(ctx == null ? void 0 : ctx.registerToolNotificationSender)) {
|
|
11619
|
-
console.log(`[McpAppHost:${mcpAppId}] No registerToolNotificationSender available in context`);
|
|
11620
11733
|
return;
|
|
11621
11734
|
}
|
|
11622
|
-
console.log(`[McpAppHost:${mcpAppId}] Registering tool notification sender`);
|
|
11623
11735
|
const sender = (method, params) => {
|
|
11624
|
-
var
|
|
11625
|
-
if (
|
|
11626
|
-
console.log(`[McpAppHost:${mcpAppId}] Iframe not initialized, queueing notification:`, method);
|
|
11736
|
+
var _a2;
|
|
11737
|
+
if (iframeStateRef.current !== "ready" || !((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow)) {
|
|
11627
11738
|
pendingNotificationsRef.current.push({ method, params });
|
|
11628
11739
|
return;
|
|
11629
11740
|
}
|
|
11630
11741
|
if (method === "ui/notifications/tool-input" || method === "ui/notifications/tool-input-partial") {
|
|
11631
11742
|
const args = params == null ? void 0 : params.arguments;
|
|
11632
11743
|
if (!args || args === "" || typeof args !== "object" || Object.keys(args).length === 0) {
|
|
11633
|
-
if (debug) {
|
|
11634
|
-
console.log(`[McpAppHost:${mcpAppId}] Skipping ${method} - empty arguments:`, args);
|
|
11635
|
-
}
|
|
11636
11744
|
return;
|
|
11637
11745
|
}
|
|
11638
11746
|
}
|
|
11639
11747
|
if (method === "ui/notifications/tool-result") {
|
|
11640
11748
|
const result = params == null ? void 0 : params.result;
|
|
11641
11749
|
if (result === "" || result === void 0) {
|
|
11642
|
-
if (debug) {
|
|
11643
|
-
console.log(`[McpAppHost:${mcpAppId}] Skipping ${method} - empty result:`, result);
|
|
11644
|
-
}
|
|
11645
11750
|
return;
|
|
11646
11751
|
}
|
|
11647
11752
|
}
|
|
11648
|
-
if (debug) {
|
|
11649
|
-
console.log(`[McpAppHost:${mcpAppId}] Forwarding tool notification:`, method, params);
|
|
11650
|
-
}
|
|
11651
11753
|
iframeRef.current.contentWindow.postMessage(
|
|
11652
11754
|
{ jsonrpc: "2.0", method, params },
|
|
11653
11755
|
"*"
|
|
@@ -11655,100 +11757,85 @@ function McpAppHost(props) {
|
|
|
11655
11757
|
};
|
|
11656
11758
|
const unregister = ctx.registerToolNotificationSender(mcpAppId, sender);
|
|
11657
11759
|
return unregister;
|
|
11658
|
-
}, [mcpAppId
|
|
11760
|
+
}, [mcpAppId]);
|
|
11659
11761
|
useEffect(() => {
|
|
11660
11762
|
const ctx = chatContextRef.current;
|
|
11661
11763
|
if (!(ctx == null ? void 0 : ctx.registerStepUpdateCallback)) {
|
|
11662
|
-
console.log(`[McpAppHost:${mcpAppId}] No registerStepUpdateCallback available in context`);
|
|
11663
11764
|
return;
|
|
11664
11765
|
}
|
|
11665
|
-
console.log(`[McpAppHost:${mcpAppId}] Registering step update callback`);
|
|
11666
11766
|
const callback = (stepUpdate) => {
|
|
11667
|
-
var
|
|
11668
|
-
if (!((
|
|
11767
|
+
var _a2;
|
|
11768
|
+
if (!((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
|
|
11669
11769
|
return;
|
|
11670
|
-
if (debug) {
|
|
11671
|
-
console.log(`[McpAppHost:${mcpAppId}] Received step update via callback:`, stepUpdate);
|
|
11672
|
-
}
|
|
11673
11770
|
const toolName = (stepUpdate == null ? void 0 : stepUpdate.stepToolName) || (stepUpdate == null ? void 0 : stepUpdate.toolName);
|
|
11674
11771
|
const toolPhase = stepUpdate == null ? void 0 : stepUpdate.toolPhase;
|
|
11675
11772
|
const toolArguments = stepUpdate == null ? void 0 : stepUpdate.stepToolArguments;
|
|
11676
11773
|
const toolProgress = stepUpdate == null ? void 0 : stepUpdate.stepToolProgress;
|
|
11677
|
-
const
|
|
11774
|
+
const rawToolResult = stepUpdate == null ? void 0 : stepUpdate.stepToolResult;
|
|
11678
11775
|
if (!toolName)
|
|
11679
11776
|
return;
|
|
11680
|
-
|
|
11681
|
-
if (
|
|
11682
|
-
|
|
11683
|
-
|
|
11684
|
-
|
|
11685
|
-
|
|
11686
|
-
|
|
11687
|
-
|
|
11688
|
-
|
|
11689
|
-
|
|
11777
|
+
const parseIfJson = (value2) => {
|
|
11778
|
+
if (value2 === void 0 || value2 === null || value2 === "")
|
|
11779
|
+
return void 0;
|
|
11780
|
+
if (typeof value2 === "object")
|
|
11781
|
+
return value2;
|
|
11782
|
+
if (typeof value2 === "string" && value2.trim()) {
|
|
11783
|
+
try {
|
|
11784
|
+
return JSON.parse(value2);
|
|
11785
|
+
} catch {
|
|
11786
|
+
return value2;
|
|
11690
11787
|
}
|
|
11691
|
-
}
|
|
11788
|
+
}
|
|
11789
|
+
return value2;
|
|
11790
|
+
};
|
|
11791
|
+
const toolResult = parseIfJson(rawToolResult);
|
|
11792
|
+
const sendOrQueue = (method, params) => {
|
|
11793
|
+
var _a3;
|
|
11794
|
+
if (iframeStateRef.current === "ready" && ((_a3 = iframeRef.current) == null ? void 0 : _a3.contentWindow)) {
|
|
11795
|
+
iframeRef.current.contentWindow.postMessage({ jsonrpc: "2.0", method, params }, "*");
|
|
11796
|
+
} else {
|
|
11797
|
+
pendingNotificationsRef.current.push({ method, params });
|
|
11798
|
+
}
|
|
11799
|
+
};
|
|
11800
|
+
if (toolPhase === "executing" && toolArguments) {
|
|
11801
|
+
sendOrQueue("ui/notifications/tool-input", {
|
|
11802
|
+
tool: { name: toolName },
|
|
11803
|
+
arguments: toolArguments
|
|
11804
|
+
});
|
|
11692
11805
|
}
|
|
11693
11806
|
if (toolProgress) {
|
|
11694
|
-
|
|
11695
|
-
|
|
11696
|
-
|
|
11697
|
-
|
|
11698
|
-
jsonrpc: "2.0",
|
|
11699
|
-
method: "ui/notifications/tool-input-partial",
|
|
11700
|
-
params: {
|
|
11701
|
-
tool: { name: toolName },
|
|
11702
|
-
arguments: { ...toolProgress, _botdojoProgress: true }
|
|
11703
|
-
}
|
|
11704
|
-
}, "*");
|
|
11807
|
+
sendOrQueue("ui/notifications/tool-input-partial", {
|
|
11808
|
+
tool: { name: toolName },
|
|
11809
|
+
arguments: { ...toolProgress, _botdojoProgress: true }
|
|
11810
|
+
});
|
|
11705
11811
|
} else if (toolPhase === "streaming_args" && (stepUpdate.stepId || stepUpdate.stepLabel)) {
|
|
11706
|
-
|
|
11707
|
-
|
|
11708
|
-
|
|
11709
|
-
|
|
11710
|
-
|
|
11711
|
-
|
|
11712
|
-
params: {
|
|
11713
|
-
tool: { name: toolName },
|
|
11714
|
-
arguments: {
|
|
11715
|
-
...toolArguments || {},
|
|
11716
|
-
stepId: stepUpdate.stepId,
|
|
11717
|
-
stepLabel: stepUpdate.stepLabel
|
|
11718
|
-
}
|
|
11812
|
+
sendOrQueue("ui/notifications/tool-input-partial", {
|
|
11813
|
+
tool: { name: toolName },
|
|
11814
|
+
arguments: {
|
|
11815
|
+
...toolArguments || {},
|
|
11816
|
+
stepId: stepUpdate.stepId,
|
|
11817
|
+
stepLabel: stepUpdate.stepLabel
|
|
11719
11818
|
}
|
|
11720
|
-
}
|
|
11819
|
+
});
|
|
11721
11820
|
}
|
|
11722
11821
|
if (toolResult !== void 0 && toolResult !== "") {
|
|
11723
|
-
|
|
11724
|
-
|
|
11725
|
-
|
|
11726
|
-
|
|
11727
|
-
jsonrpc: "2.0",
|
|
11728
|
-
method: "ui/notifications/tool-result",
|
|
11729
|
-
params: {
|
|
11730
|
-
tool: { name: toolName },
|
|
11731
|
-
result: toolResult
|
|
11732
|
-
}
|
|
11733
|
-
}, "*");
|
|
11822
|
+
sendOrQueue("ui/notifications/tool-result", {
|
|
11823
|
+
tool: { name: toolName },
|
|
11824
|
+
result: toolResult
|
|
11825
|
+
});
|
|
11734
11826
|
}
|
|
11735
11827
|
};
|
|
11736
11828
|
const unregister = ctx.registerStepUpdateCallback(mcpAppId, callback);
|
|
11737
11829
|
return unregister;
|
|
11738
|
-
}, [mcpAppId
|
|
11830
|
+
}, [mcpAppId]);
|
|
11739
11831
|
useEffect(() => {
|
|
11740
|
-
|
|
11741
|
-
|
|
11742
|
-
|
|
11743
|
-
|
|
11744
|
-
|
|
11745
|
-
|
|
11746
|
-
|
|
11747
|
-
}
|
|
11748
|
-
if (appUrl) {
|
|
11749
|
-
iframeRef.current.src = appUrl;
|
|
11750
|
-
}
|
|
11751
|
-
}, [appHtml, appUrl]);
|
|
11832
|
+
return () => {
|
|
11833
|
+
if (currentBlobUrlRef.current) {
|
|
11834
|
+
URL.revokeObjectURL(currentBlobUrlRef.current);
|
|
11835
|
+
currentBlobUrlRef.current = null;
|
|
11836
|
+
}
|
|
11837
|
+
};
|
|
11838
|
+
}, []);
|
|
11752
11839
|
useEffect(() => {
|
|
11753
11840
|
return () => {
|
|
11754
11841
|
if (initializeTimerRef.current !== null) {
|
|
@@ -11762,18 +11849,6 @@ function McpAppHost(props) {
|
|
|
11762
11849
|
pendingRequestsRef.current.clear();
|
|
11763
11850
|
};
|
|
11764
11851
|
}, []);
|
|
11765
|
-
useEffect(() => {
|
|
11766
|
-
setIframeLoaded(false);
|
|
11767
|
-
setIsInitialized(false);
|
|
11768
|
-
isInitializedRef.current = false;
|
|
11769
|
-
setAutoHeight(null);
|
|
11770
|
-
initializeAttemptsRef.current = 0;
|
|
11771
|
-
pendingNotificationsRef.current = [];
|
|
11772
|
-
if (initializeTimerRef.current !== null) {
|
|
11773
|
-
clearTimeout(initializeTimerRef.current);
|
|
11774
|
-
initializeTimerRef.current = null;
|
|
11775
|
-
}
|
|
11776
|
-
}, [appHtml, appUrl]);
|
|
11777
11852
|
const effectiveHeight = autoHeight ? `${autoHeight}px` : (mcpAppData == null ? void 0 : mcpAppData.height) || height;
|
|
11778
11853
|
return /* @__PURE__ */ jsx(
|
|
11779
11854
|
"iframe",
|
|
@@ -11791,6 +11866,61 @@ function McpAppHost(props) {
|
|
|
11791
11866
|
}
|
|
11792
11867
|
);
|
|
11793
11868
|
}
|
|
11869
|
+
|
|
11870
|
+
// src/headless/types/headless.ts
|
|
11871
|
+
function parseMaybeJson(value2) {
|
|
11872
|
+
if (value2 === void 0 || value2 === null || value2 === "")
|
|
11873
|
+
return void 0;
|
|
11874
|
+
if (typeof value2 === "object")
|
|
11875
|
+
return value2;
|
|
11876
|
+
if (typeof value2 === "string" && value2.trim()) {
|
|
11877
|
+
try {
|
|
11878
|
+
return JSON.parse(value2);
|
|
11879
|
+
} catch {
|
|
11880
|
+
return value2;
|
|
11881
|
+
}
|
|
11882
|
+
}
|
|
11883
|
+
return value2;
|
|
11884
|
+
}
|
|
11885
|
+
function extractMcpAppData(step, options) {
|
|
11886
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
11887
|
+
const canvas = step == null ? void 0 : step.canvas;
|
|
11888
|
+
if (!(canvas == null ? void 0 : canvas.canvasId)) {
|
|
11889
|
+
return null;
|
|
11890
|
+
}
|
|
11891
|
+
const toolName = step.stepToolName || ((_a = canvas.canvasData) == null ? void 0 : _a.toolName);
|
|
11892
|
+
let toolArguments = step.stepToolArguments;
|
|
11893
|
+
if (!toolArguments || typeof toolArguments === "object" && Object.keys(toolArguments).length === 0) {
|
|
11894
|
+
toolArguments = parseMaybeJson(step.arguments);
|
|
11895
|
+
}
|
|
11896
|
+
if (!toolArguments || typeof toolArguments === "object" && Object.keys(toolArguments).length === 0) {
|
|
11897
|
+
toolArguments = parseMaybeJson((_b = canvas.canvasData) == null ? void 0 : _b.arguments);
|
|
11898
|
+
}
|
|
11899
|
+
let toolResult = step.stepToolResult;
|
|
11900
|
+
if (toolResult === void 0 || toolResult === "") {
|
|
11901
|
+
toolResult = parseMaybeJson((_c = canvas.canvasData) == null ? void 0 : _c.result);
|
|
11902
|
+
}
|
|
11903
|
+
if (toolResult === void 0 || toolResult === "") {
|
|
11904
|
+
const toolPhase = step.stepToolPhase || step.toolPhase;
|
|
11905
|
+
if (toolPhase === "complete" && step.content) {
|
|
11906
|
+
toolResult = parseMaybeJson(step.content);
|
|
11907
|
+
}
|
|
11908
|
+
}
|
|
11909
|
+
return {
|
|
11910
|
+
mcpAppId: canvas.canvasId,
|
|
11911
|
+
mcpAppType: canvas.canvasType || "mcp-app",
|
|
11912
|
+
url: (_d = canvas.canvasData) == null ? void 0 : _d.url,
|
|
11913
|
+
html: (_e = canvas.canvasData) == null ? void 0 : _e.html,
|
|
11914
|
+
height: (_f = canvas.canvasData) == null ? void 0 : _f.height,
|
|
11915
|
+
width: (_g = canvas.canvasData) == null ? void 0 : _g.width,
|
|
11916
|
+
state: (_h = canvas.canvasData) == null ? void 0 : _h.state,
|
|
11917
|
+
arguments: toolArguments,
|
|
11918
|
+
result: toolResult,
|
|
11919
|
+
isComplete: (_i = options == null ? void 0 : options.isComplete) != null ? _i : false,
|
|
11920
|
+
// Include toolInfo for hydration - McpAppHost needs tool.name to send notifications
|
|
11921
|
+
toolInfo: toolName ? { tool: { name: toolName } } : void 0
|
|
11922
|
+
};
|
|
11923
|
+
}
|
|
11794
11924
|
var DEFAULT_CONFIG = {
|
|
11795
11925
|
mode: "chat-popup",
|
|
11796
11926
|
popupOptions: {
|
|
@@ -13511,6 +13641,6 @@ var export_textResult = import_sdk_types2.textResult;
|
|
|
13511
13641
|
var export_tool = import_sdk_types2.tool;
|
|
13512
13642
|
var export_uiResource = import_sdk_types2.uiResource;
|
|
13513
13643
|
|
|
13514
|
-
export { BotDojoCanvasFrame, BotDojoCanvasProvider, BotDojoChat, BotDojoChatConfigurator, BotDojoChatProvider, export_BotDojoConnector as BotDojoConnector, HeadlessChat, export_MIME_TYPES as MIME_TYPES, McpAppHost, McpAppHostContext, McpAppHostProvider, MockCanvasProvider, export_citation as citation, export_citations as citations, export_imageResult as imageResult, export_mcpResource as mcpResource, export_modelContext as modelContext, export_textResult as textResult, export_tool as tool, export_uiResource as uiResource, useBotDojoCanvas, useBotDojoChat, useChatActions, useChatMessages, useChatStatus, useMcpAppHost, useMcpAppHostRequired };
|
|
13644
|
+
export { BotDojoCanvasFrame, BotDojoCanvasProvider, BotDojoChat, BotDojoChatConfigurator, BotDojoChatProvider, export_BotDojoConnector as BotDojoConnector, HeadlessChat, export_MIME_TYPES as MIME_TYPES, McpAppHost, McpAppHostContext, McpAppHostProvider, MockCanvasProvider, export_citation as citation, export_citations as citations, extractMcpAppData, export_imageResult as imageResult, export_mcpResource as mcpResource, export_modelContext as modelContext, parseMaybeJson, export_textResult as textResult, export_tool as tool, export_uiResource as uiResource, useBotDojoCanvas, useBotDojoChat, useChatActions, useChatMessages, useChatStatus, useMcpAppHost, useMcpAppHostRequired };
|
|
13515
13645
|
//# sourceMappingURL=out.js.map
|
|
13516
13646
|
//# sourceMappingURL=index.js.map
|