@copilotkit/react-core 1.57.0-canary.1778146769 → 1.57.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{copilotkit-BNlJq5UO.d.mts → copilotkit-BN4I_y1n.d.mts} +6 -4
- package/dist/copilotkit-BN4I_y1n.d.mts.map +1 -0
- package/dist/{copilotkit-B_k0HSNz.cjs → copilotkit-C3k13WZn.cjs} +197 -198
- package/dist/copilotkit-C3k13WZn.cjs.map +1 -0
- package/dist/{copilotkit-ak8sGvQr.mjs → copilotkit-DjxXMYHG.mjs} +198 -199
- package/dist/copilotkit-DjxXMYHG.mjs.map +1 -0
- package/dist/{copilotkit-DgC5oCFO.d.cts → copilotkit-sQWiKtxA.d.cts} +6 -4
- package/dist/copilotkit-sQWiKtxA.d.cts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.umd.js +207 -205
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/context.cjs +135 -0
- package/dist/v2/context.cjs.map +1 -0
- package/dist/v2/context.d.cts +148 -0
- package/dist/v2/context.d.cts.map +1 -0
- package/dist/v2/context.d.mts +148 -0
- package/dist/v2/context.d.mts.map +1 -0
- package/dist/v2/context.mjs +129 -0
- package/dist/v2/context.mjs.map +1 -0
- package/dist/v2/headless.cjs +1043 -0
- package/dist/v2/headless.cjs.map +1 -0
- package/dist/v2/headless.d.cts +605 -0
- package/dist/v2/headless.d.cts.map +1 -0
- package/dist/v2/headless.d.mts +512 -0
- package/dist/v2/headless.d.mts.map +1 -0
- package/dist/v2/headless.mjs +997 -0
- package/dist/v2/headless.mjs.map +1 -0
- package/dist/v2/index.cjs +1 -1
- package/dist/v2/index.d.cts +1 -1
- package/dist/v2/index.d.mts +1 -1
- package/dist/v2/index.mjs +1 -1
- package/dist/v2/index.umd.js +209 -208
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +14 -6
- package/src/v2/context.ts +62 -0
- package/src/v2/headless.ts +42 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +3 -3
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +3 -3
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +85 -85
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-threads.test.tsx +2 -2
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +2 -2
- package/src/v2/hooks/use-agent-context.tsx +1 -1
- package/src/v2/hooks/use-agent.tsx +2 -2
- package/src/v2/hooks/use-configure-suggestions.tsx +1 -1
- package/src/v2/hooks/use-frontend-tool.tsx +2 -2
- package/src/v2/hooks/use-human-in-the-loop.tsx +1 -1
- package/src/v2/hooks/use-interrupt.tsx +1 -1
- package/src/v2/hooks/use-render-tool-call.tsx +1 -1
- package/src/v2/hooks/use-render-tool.tsx +2 -2
- package/src/v2/hooks/use-suggestions.tsx +1 -1
- package/src/v2/hooks/use-threads.tsx +1 -1
- package/src/v2/providers/CopilotKitProvider.tsx +19 -59
- package/tsdown.config.ts +75 -0
- package/dist/copilotkit-BNlJq5UO.d.mts.map +0 -1
- package/dist/copilotkit-B_k0HSNz.cjs.map +0 -1
- package/dist/copilotkit-DgC5oCFO.d.cts.map +0 -1
- package/dist/copilotkit-ak8sGvQr.mjs.map +0 -1
package/dist/v2/index.umd.js
CHANGED
|
@@ -1335,6 +1335,207 @@ _radix_ui_react_dropdown_menu = __toESM(_radix_ui_react_dropdown_menu);
|
|
|
1335
1335
|
}, []);
|
|
1336
1336
|
}
|
|
1337
1337
|
|
|
1338
|
+
//#endregion
|
|
1339
|
+
//#region src/v2/lib/react-core.ts
|
|
1340
|
+
var CopilotKitCoreReact = class extends _copilotkit_core.CopilotKitCore {
|
|
1341
|
+
constructor(config) {
|
|
1342
|
+
var _config$renderToolCal, _config$renderCustomM, _config$renderActivit;
|
|
1343
|
+
super(config);
|
|
1344
|
+
this._renderToolCalls = [];
|
|
1345
|
+
this._hookRenderToolCalls = /* @__PURE__ */ new Map();
|
|
1346
|
+
this._cachedMergedRenderToolCalls = null;
|
|
1347
|
+
this._renderCustomMessages = [];
|
|
1348
|
+
this._renderActivityMessages = [];
|
|
1349
|
+
this._interruptElement = null;
|
|
1350
|
+
this._renderToolCalls = (_config$renderToolCal = config.renderToolCalls) !== null && _config$renderToolCal !== void 0 ? _config$renderToolCal : [];
|
|
1351
|
+
this._renderCustomMessages = (_config$renderCustomM = config.renderCustomMessages) !== null && _config$renderCustomM !== void 0 ? _config$renderCustomM : [];
|
|
1352
|
+
this._renderActivityMessages = (_config$renderActivit = config.renderActivityMessages) !== null && _config$renderActivit !== void 0 ? _config$renderActivit : [];
|
|
1353
|
+
}
|
|
1354
|
+
get renderCustomMessages() {
|
|
1355
|
+
return this._renderCustomMessages;
|
|
1356
|
+
}
|
|
1357
|
+
get renderActivityMessages() {
|
|
1358
|
+
return this._renderActivityMessages;
|
|
1359
|
+
}
|
|
1360
|
+
get renderToolCalls() {
|
|
1361
|
+
if (this._hookRenderToolCalls.size === 0) return this._renderToolCalls;
|
|
1362
|
+
if (this._cachedMergedRenderToolCalls) return this._cachedMergedRenderToolCalls;
|
|
1363
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1364
|
+
for (const rc of this._renderToolCalls) {
|
|
1365
|
+
var _rc$agentId;
|
|
1366
|
+
merged.set(`${(_rc$agentId = rc.agentId) !== null && _rc$agentId !== void 0 ? _rc$agentId : ""}:${rc.name}`, rc);
|
|
1367
|
+
}
|
|
1368
|
+
for (const [key, rc] of this._hookRenderToolCalls) merged.set(key, rc);
|
|
1369
|
+
this._cachedMergedRenderToolCalls = Array.from(merged.values());
|
|
1370
|
+
return this._cachedMergedRenderToolCalls;
|
|
1371
|
+
}
|
|
1372
|
+
setRenderActivityMessages(renderers) {
|
|
1373
|
+
this._renderActivityMessages = renderers;
|
|
1374
|
+
}
|
|
1375
|
+
setRenderCustomMessages(renderers) {
|
|
1376
|
+
this._renderCustomMessages = renderers;
|
|
1377
|
+
}
|
|
1378
|
+
setRenderToolCalls(renderToolCalls) {
|
|
1379
|
+
this._renderToolCalls = renderToolCalls;
|
|
1380
|
+
this._cachedMergedRenderToolCalls = null;
|
|
1381
|
+
this._notifyRenderToolCallsChanged();
|
|
1382
|
+
}
|
|
1383
|
+
addHookRenderToolCall(entry) {
|
|
1384
|
+
var _entry$agentId;
|
|
1385
|
+
const key = `${(_entry$agentId = entry.agentId) !== null && _entry$agentId !== void 0 ? _entry$agentId : ""}:${entry.name}`;
|
|
1386
|
+
this._hookRenderToolCalls.set(key, entry);
|
|
1387
|
+
this._cachedMergedRenderToolCalls = null;
|
|
1388
|
+
this._notifyRenderToolCallsChanged();
|
|
1389
|
+
}
|
|
1390
|
+
removeHookRenderToolCall(name, agentId) {
|
|
1391
|
+
const key = `${agentId !== null && agentId !== void 0 ? agentId : ""}:${name}`;
|
|
1392
|
+
if (this._hookRenderToolCalls.delete(key)) {
|
|
1393
|
+
this._cachedMergedRenderToolCalls = null;
|
|
1394
|
+
this._notifyRenderToolCallsChanged();
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
_notifyRenderToolCallsChanged() {
|
|
1398
|
+
this.notifySubscribers((subscriber) => {
|
|
1399
|
+
const reactSubscriber = subscriber;
|
|
1400
|
+
if (reactSubscriber.onRenderToolCallsChanged) reactSubscriber.onRenderToolCallsChanged({
|
|
1401
|
+
copilotkit: this,
|
|
1402
|
+
renderToolCalls: this.renderToolCalls
|
|
1403
|
+
});
|
|
1404
|
+
}, "Subscriber onRenderToolCallsChanged error:");
|
|
1405
|
+
}
|
|
1406
|
+
get interruptElement() {
|
|
1407
|
+
return this._interruptElement;
|
|
1408
|
+
}
|
|
1409
|
+
setInterruptElement(element) {
|
|
1410
|
+
this._interruptElement = element;
|
|
1411
|
+
this.notifySubscribers((subscriber) => {
|
|
1412
|
+
var _reactSubscriber$onIn;
|
|
1413
|
+
const reactSubscriber = subscriber;
|
|
1414
|
+
(_reactSubscriber$onIn = reactSubscriber.onInterruptElementChanged) === null || _reactSubscriber$onIn === void 0 || _reactSubscriber$onIn.call(reactSubscriber, {
|
|
1415
|
+
copilotkit: this,
|
|
1416
|
+
interruptElement: this._interruptElement
|
|
1417
|
+
});
|
|
1418
|
+
}, "Subscriber onInterruptElementChanged error:");
|
|
1419
|
+
}
|
|
1420
|
+
subscribe(subscriber) {
|
|
1421
|
+
return super.subscribe(subscriber);
|
|
1422
|
+
}
|
|
1423
|
+
/**
|
|
1424
|
+
* Wait for pending React state updates before the follow-up agent run.
|
|
1425
|
+
*
|
|
1426
|
+
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
1427
|
+
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
1428
|
+
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
1429
|
+
* synchronously after React commits that batch.
|
|
1430
|
+
*
|
|
1431
|
+
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
1432
|
+
* MessageChannel task runs first, committing the pending state and running
|
|
1433
|
+
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
1434
|
+
* call then reads fresh context.
|
|
1435
|
+
*/
|
|
1436
|
+
async waitForPendingFrameworkUpdates() {
|
|
1437
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
//#endregion
|
|
1442
|
+
//#region src/v2/context.ts
|
|
1443
|
+
const CopilotKitContext = (0, react.createContext)(null);
|
|
1444
|
+
const useCopilotKit = () => {
|
|
1445
|
+
const context = (0, react.useContext)(CopilotKitContext);
|
|
1446
|
+
const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
|
|
1447
|
+
if (!context) throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
1448
|
+
(0, react.useEffect)(() => {
|
|
1449
|
+
const subscription = context.copilotkit.subscribe({ onRuntimeConnectionStatusChanged: () => {
|
|
1450
|
+
forceUpdate();
|
|
1451
|
+
} });
|
|
1452
|
+
return () => {
|
|
1453
|
+
subscription.unsubscribe();
|
|
1454
|
+
};
|
|
1455
|
+
}, []);
|
|
1456
|
+
return context;
|
|
1457
|
+
};
|
|
1458
|
+
const LicenseContext = (0, react.createContext)({
|
|
1459
|
+
status: null,
|
|
1460
|
+
license: null,
|
|
1461
|
+
checkFeature: () => true,
|
|
1462
|
+
getLimit: () => null
|
|
1463
|
+
});
|
|
1464
|
+
const useLicenseContext = () => (0, react.useContext)(LicenseContext);
|
|
1465
|
+
|
|
1466
|
+
//#endregion
|
|
1467
|
+
//#region src/v2/hooks/use-render-tool-call.tsx
|
|
1468
|
+
/**
|
|
1469
|
+
* Memoized component that renders a single tool call.
|
|
1470
|
+
* This prevents unnecessary re-renders when parent components update
|
|
1471
|
+
* but the tool call data hasn't changed.
|
|
1472
|
+
*/
|
|
1473
|
+
const ToolCallRenderer = react.default.memo(function ToolCallRenderer({ toolCall, toolMessage, RenderComponent, isExecuting }) {
|
|
1474
|
+
const args = (0, react.useMemo)(() => (0, _copilotkit_shared.partialJSONParse)(toolCall.function.arguments), [toolCall.function.arguments]);
|
|
1475
|
+
const toolName = toolCall.function.name;
|
|
1476
|
+
if (toolMessage) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
1477
|
+
name: toolName,
|
|
1478
|
+
toolCallId: toolCall.id,
|
|
1479
|
+
args,
|
|
1480
|
+
status: _copilotkit_core.ToolCallStatus.Complete,
|
|
1481
|
+
result: toolMessage.content
|
|
1482
|
+
});
|
|
1483
|
+
else if (isExecuting) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
1484
|
+
name: toolName,
|
|
1485
|
+
toolCallId: toolCall.id,
|
|
1486
|
+
args,
|
|
1487
|
+
status: _copilotkit_core.ToolCallStatus.Executing,
|
|
1488
|
+
result: void 0
|
|
1489
|
+
});
|
|
1490
|
+
else return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
1491
|
+
name: toolName,
|
|
1492
|
+
toolCallId: toolCall.id,
|
|
1493
|
+
args,
|
|
1494
|
+
status: _copilotkit_core.ToolCallStatus.InProgress,
|
|
1495
|
+
result: void 0
|
|
1496
|
+
});
|
|
1497
|
+
}, (prevProps, nextProps) => {
|
|
1498
|
+
var _prevProps$toolMessag, _nextProps$toolMessag;
|
|
1499
|
+
if (prevProps.toolCall.id !== nextProps.toolCall.id) return false;
|
|
1500
|
+
if (prevProps.toolCall.function.name !== nextProps.toolCall.function.name) return false;
|
|
1501
|
+
if (prevProps.toolCall.function.arguments !== nextProps.toolCall.function.arguments) return false;
|
|
1502
|
+
if (((_prevProps$toolMessag = prevProps.toolMessage) === null || _prevProps$toolMessag === void 0 ? void 0 : _prevProps$toolMessag.content) !== ((_nextProps$toolMessag = nextProps.toolMessage) === null || _nextProps$toolMessag === void 0 ? void 0 : _nextProps$toolMessag.content)) return false;
|
|
1503
|
+
if (prevProps.isExecuting !== nextProps.isExecuting) return false;
|
|
1504
|
+
if (prevProps.RenderComponent !== nextProps.RenderComponent) return false;
|
|
1505
|
+
return true;
|
|
1506
|
+
});
|
|
1507
|
+
/**
|
|
1508
|
+
* Hook that returns a function to render tool calls based on the render functions
|
|
1509
|
+
* defined in CopilotKitProvider.
|
|
1510
|
+
*
|
|
1511
|
+
* @returns A function that takes a tool call and optional tool message and returns the rendered component
|
|
1512
|
+
*/
|
|
1513
|
+
function useRenderToolCall() {
|
|
1514
|
+
var _config$agentId;
|
|
1515
|
+
const { copilotkit, executingToolCallIds } = useCopilotKit();
|
|
1516
|
+
const config = useCopilotChatConfiguration();
|
|
1517
|
+
const agentId = (_config$agentId = config === null || config === void 0 ? void 0 : config.agentId) !== null && _config$agentId !== void 0 ? _config$agentId : _copilotkit_shared.DEFAULT_AGENT_ID;
|
|
1518
|
+
const renderToolCalls = (0, react.useSyncExternalStore)((callback) => {
|
|
1519
|
+
return copilotkit.subscribe({ onRenderToolCallsChanged: callback }).unsubscribe;
|
|
1520
|
+
}, () => copilotkit.renderToolCalls, () => copilotkit.renderToolCalls);
|
|
1521
|
+
return (0, react.useCallback)(({ toolCall, toolMessage }) => {
|
|
1522
|
+
const exactMatches = renderToolCalls.filter((rc) => rc.name === toolCall.function.name);
|
|
1523
|
+
const renderConfig = exactMatches.find((rc) => rc.agentId === agentId) || exactMatches.find((rc) => !rc.agentId) || exactMatches[0] || renderToolCalls.find((rc) => rc.name === "*");
|
|
1524
|
+
if (!renderConfig) return null;
|
|
1525
|
+
const RenderComponent = renderConfig.render;
|
|
1526
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCallRenderer, {
|
|
1527
|
+
toolCall,
|
|
1528
|
+
toolMessage,
|
|
1529
|
+
RenderComponent,
|
|
1530
|
+
isExecuting: executingToolCallIds.has(toolCall.id)
|
|
1531
|
+
}, toolCall.id);
|
|
1532
|
+
}, [
|
|
1533
|
+
renderToolCalls,
|
|
1534
|
+
executingToolCallIds,
|
|
1535
|
+
agentId
|
|
1536
|
+
]);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1338
1539
|
//#endregion
|
|
1339
1540
|
//#region src/v2/components/CopilotKitInspector.tsx
|
|
1340
1541
|
const CopilotKitInspector = ({ core, ...rest }) => {
|
|
@@ -2996,109 +3197,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2996
3197
|
return null;
|
|
2997
3198
|
}
|
|
2998
3199
|
|
|
2999
|
-
//#endregion
|
|
3000
|
-
//#region src/v2/lib/react-core.ts
|
|
3001
|
-
var CopilotKitCoreReact = class extends _copilotkit_core.CopilotKitCore {
|
|
3002
|
-
constructor(config) {
|
|
3003
|
-
var _config$renderToolCal, _config$renderCustomM, _config$renderActivit;
|
|
3004
|
-
super(config);
|
|
3005
|
-
this._renderToolCalls = [];
|
|
3006
|
-
this._hookRenderToolCalls = /* @__PURE__ */ new Map();
|
|
3007
|
-
this._cachedMergedRenderToolCalls = null;
|
|
3008
|
-
this._renderCustomMessages = [];
|
|
3009
|
-
this._renderActivityMessages = [];
|
|
3010
|
-
this._interruptElement = null;
|
|
3011
|
-
this._renderToolCalls = (_config$renderToolCal = config.renderToolCalls) !== null && _config$renderToolCal !== void 0 ? _config$renderToolCal : [];
|
|
3012
|
-
this._renderCustomMessages = (_config$renderCustomM = config.renderCustomMessages) !== null && _config$renderCustomM !== void 0 ? _config$renderCustomM : [];
|
|
3013
|
-
this._renderActivityMessages = (_config$renderActivit = config.renderActivityMessages) !== null && _config$renderActivit !== void 0 ? _config$renderActivit : [];
|
|
3014
|
-
}
|
|
3015
|
-
get renderCustomMessages() {
|
|
3016
|
-
return this._renderCustomMessages;
|
|
3017
|
-
}
|
|
3018
|
-
get renderActivityMessages() {
|
|
3019
|
-
return this._renderActivityMessages;
|
|
3020
|
-
}
|
|
3021
|
-
get renderToolCalls() {
|
|
3022
|
-
if (this._hookRenderToolCalls.size === 0) return this._renderToolCalls;
|
|
3023
|
-
if (this._cachedMergedRenderToolCalls) return this._cachedMergedRenderToolCalls;
|
|
3024
|
-
const merged = /* @__PURE__ */ new Map();
|
|
3025
|
-
for (const rc of this._renderToolCalls) {
|
|
3026
|
-
var _rc$agentId;
|
|
3027
|
-
merged.set(`${(_rc$agentId = rc.agentId) !== null && _rc$agentId !== void 0 ? _rc$agentId : ""}:${rc.name}`, rc);
|
|
3028
|
-
}
|
|
3029
|
-
for (const [key, rc] of this._hookRenderToolCalls) merged.set(key, rc);
|
|
3030
|
-
this._cachedMergedRenderToolCalls = Array.from(merged.values());
|
|
3031
|
-
return this._cachedMergedRenderToolCalls;
|
|
3032
|
-
}
|
|
3033
|
-
setRenderActivityMessages(renderers) {
|
|
3034
|
-
this._renderActivityMessages = renderers;
|
|
3035
|
-
}
|
|
3036
|
-
setRenderCustomMessages(renderers) {
|
|
3037
|
-
this._renderCustomMessages = renderers;
|
|
3038
|
-
}
|
|
3039
|
-
setRenderToolCalls(renderToolCalls) {
|
|
3040
|
-
this._renderToolCalls = renderToolCalls;
|
|
3041
|
-
this._cachedMergedRenderToolCalls = null;
|
|
3042
|
-
this._notifyRenderToolCallsChanged();
|
|
3043
|
-
}
|
|
3044
|
-
addHookRenderToolCall(entry) {
|
|
3045
|
-
var _entry$agentId;
|
|
3046
|
-
const key = `${(_entry$agentId = entry.agentId) !== null && _entry$agentId !== void 0 ? _entry$agentId : ""}:${entry.name}`;
|
|
3047
|
-
this._hookRenderToolCalls.set(key, entry);
|
|
3048
|
-
this._cachedMergedRenderToolCalls = null;
|
|
3049
|
-
this._notifyRenderToolCallsChanged();
|
|
3050
|
-
}
|
|
3051
|
-
removeHookRenderToolCall(name, agentId) {
|
|
3052
|
-
const key = `${agentId !== null && agentId !== void 0 ? agentId : ""}:${name}`;
|
|
3053
|
-
if (this._hookRenderToolCalls.delete(key)) {
|
|
3054
|
-
this._cachedMergedRenderToolCalls = null;
|
|
3055
|
-
this._notifyRenderToolCallsChanged();
|
|
3056
|
-
}
|
|
3057
|
-
}
|
|
3058
|
-
_notifyRenderToolCallsChanged() {
|
|
3059
|
-
this.notifySubscribers((subscriber) => {
|
|
3060
|
-
const reactSubscriber = subscriber;
|
|
3061
|
-
if (reactSubscriber.onRenderToolCallsChanged) reactSubscriber.onRenderToolCallsChanged({
|
|
3062
|
-
copilotkit: this,
|
|
3063
|
-
renderToolCalls: this.renderToolCalls
|
|
3064
|
-
});
|
|
3065
|
-
}, "Subscriber onRenderToolCallsChanged error:");
|
|
3066
|
-
}
|
|
3067
|
-
get interruptElement() {
|
|
3068
|
-
return this._interruptElement;
|
|
3069
|
-
}
|
|
3070
|
-
setInterruptElement(element) {
|
|
3071
|
-
this._interruptElement = element;
|
|
3072
|
-
this.notifySubscribers((subscriber) => {
|
|
3073
|
-
var _reactSubscriber$onIn;
|
|
3074
|
-
const reactSubscriber = subscriber;
|
|
3075
|
-
(_reactSubscriber$onIn = reactSubscriber.onInterruptElementChanged) === null || _reactSubscriber$onIn === void 0 || _reactSubscriber$onIn.call(reactSubscriber, {
|
|
3076
|
-
copilotkit: this,
|
|
3077
|
-
interruptElement: this._interruptElement
|
|
3078
|
-
});
|
|
3079
|
-
}, "Subscriber onInterruptElementChanged error:");
|
|
3080
|
-
}
|
|
3081
|
-
subscribe(subscriber) {
|
|
3082
|
-
return super.subscribe(subscriber);
|
|
3083
|
-
}
|
|
3084
|
-
/**
|
|
3085
|
-
* Wait for pending React state updates before the follow-up agent run.
|
|
3086
|
-
*
|
|
3087
|
-
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
3088
|
-
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
3089
|
-
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
3090
|
-
* synchronously after React commits that batch.
|
|
3091
|
-
*
|
|
3092
|
-
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
3093
|
-
* MessageChannel task runs first, committing the pending state and running
|
|
3094
|
-
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
3095
|
-
* call then reads fresh context.
|
|
3096
|
-
*/
|
|
3097
|
-
async waitForPendingFrameworkUpdates() {
|
|
3098
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
3099
|
-
}
|
|
3100
|
-
};
|
|
3101
|
-
|
|
3102
3200
|
//#endregion
|
|
3103
3201
|
//#region src/v2/providers/CopilotKitProvider.tsx
|
|
3104
3202
|
const HEADER_NAME = "X-CopilotCloud-Public-Api-Key";
|
|
@@ -3114,12 +3212,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3114
3212
|
- Minimal transitions (150ms) for hover/focus states only. No decorative animations.
|
|
3115
3213
|
- Keep the UI focused and dense — avoid excessive padding. Use compact spacing (8–12px gaps, 10–14px padding in controls).`;
|
|
3116
3214
|
const GENERATE_SANDBOXED_UI_DESCRIPTION = "Generate sandboxed UI. IMPORTANT: The generated code runs in a sandboxed iframe WITHOUT same-origin access. Do NOT use localStorage, sessionStorage, document.cookie, IndexedDB, or fetch/XMLHttpRequest to same-origin URLs. To communicate with the host application, use Websandbox.connection.remote.<functionName>(args) which returns a Promise.\n\nYou CAN use external libraries from CDNs by including <script> or <link> tags in the HTML <head> (e.g., Chart.js, D3, Three.js, x-data-spreadsheet, etc.). CDN resources load normally inside the sandbox.\n\nPARAMETER ORDER IS CRITICAL — generate parameters in exactly this order:\n1. initialHeight + placeholderMessages (shown to user while generating)\n2. css (all styles FIRST — the user sees a placeholder until CSS is complete)\n3. html (streams in live — the user watches the UI build as HTML is generated)\n4. jsFunctions (reusable helper functions)\n5. jsExpressions (applied one-by-one — the user sees each expression take effect)";
|
|
3117
|
-
const CopilotKitContext = (0, react.createContext)({
|
|
3118
|
-
copilotkit: null,
|
|
3119
|
-
executingToolCallIds: /* @__PURE__ */ new Set()
|
|
3120
|
-
});
|
|
3121
|
-
const LicenseContext = (0, react.createContext)((0, _copilotkit_shared.createLicenseContextValue)(null));
|
|
3122
|
-
const useLicenseContext = () => (0, react.useContext)(LicenseContext);
|
|
3123
3215
|
function useStableArrayProp(prop, warningMessage, isMeaningfulChange) {
|
|
3124
3216
|
const empty = (0, react.useMemo)(() => [], []);
|
|
3125
3217
|
const value = prop !== null && prop !== void 0 ? prop : empty;
|
|
@@ -3348,14 +3440,12 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3348
3440
|
onErrorRef.current = onError;
|
|
3349
3441
|
}, [onError]);
|
|
3350
3442
|
(0, react.useEffect)(() => {
|
|
3351
|
-
if (!onErrorRef.current) return;
|
|
3352
3443
|
const subscription = copilotkit.subscribe({ onError: (event) => {
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
code
|
|
3357
|
-
|
|
3358
|
-
});
|
|
3444
|
+
if (onErrorRef.current) onErrorRef.current(event);
|
|
3445
|
+
else {
|
|
3446
|
+
var _event$context;
|
|
3447
|
+
console.error(`[CopilotKit] Error (${event.code}):`, event.error, (_event$context = event.context) !== null && _event$context !== void 0 ? _event$context : {});
|
|
3448
|
+
}
|
|
3359
3449
|
} });
|
|
3360
3450
|
return () => {
|
|
3361
3451
|
subscription.unsubscribe();
|
|
@@ -3470,93 +3560,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3470
3560
|
})
|
|
3471
3561
|
});
|
|
3472
3562
|
};
|
|
3473
|
-
const useCopilotKit = () => {
|
|
3474
|
-
const context = (0, react.useContext)(CopilotKitContext);
|
|
3475
|
-
const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
|
|
3476
|
-
if (!context) throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
3477
|
-
(0, react.useEffect)(() => {
|
|
3478
|
-
const subscription = context.copilotkit.subscribe({ onRuntimeConnectionStatusChanged: () => {
|
|
3479
|
-
forceUpdate();
|
|
3480
|
-
} });
|
|
3481
|
-
return () => {
|
|
3482
|
-
subscription.unsubscribe();
|
|
3483
|
-
};
|
|
3484
|
-
}, []);
|
|
3485
|
-
return context;
|
|
3486
|
-
};
|
|
3487
|
-
|
|
3488
|
-
//#endregion
|
|
3489
|
-
//#region src/v2/hooks/use-render-tool-call.tsx
|
|
3490
|
-
/**
|
|
3491
|
-
* Memoized component that renders a single tool call.
|
|
3492
|
-
* This prevents unnecessary re-renders when parent components update
|
|
3493
|
-
* but the tool call data hasn't changed.
|
|
3494
|
-
*/
|
|
3495
|
-
const ToolCallRenderer = react.default.memo(function ToolCallRenderer({ toolCall, toolMessage, RenderComponent, isExecuting }) {
|
|
3496
|
-
const args = (0, react.useMemo)(() => (0, _copilotkit_shared.partialJSONParse)(toolCall.function.arguments), [toolCall.function.arguments]);
|
|
3497
|
-
const toolName = toolCall.function.name;
|
|
3498
|
-
if (toolMessage) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
3499
|
-
name: toolName,
|
|
3500
|
-
toolCallId: toolCall.id,
|
|
3501
|
-
args,
|
|
3502
|
-
status: _copilotkit_core.ToolCallStatus.Complete,
|
|
3503
|
-
result: toolMessage.content
|
|
3504
|
-
});
|
|
3505
|
-
else if (isExecuting) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
3506
|
-
name: toolName,
|
|
3507
|
-
toolCallId: toolCall.id,
|
|
3508
|
-
args,
|
|
3509
|
-
status: _copilotkit_core.ToolCallStatus.Executing,
|
|
3510
|
-
result: void 0
|
|
3511
|
-
});
|
|
3512
|
-
else return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
3513
|
-
name: toolName,
|
|
3514
|
-
toolCallId: toolCall.id,
|
|
3515
|
-
args,
|
|
3516
|
-
status: _copilotkit_core.ToolCallStatus.InProgress,
|
|
3517
|
-
result: void 0
|
|
3518
|
-
});
|
|
3519
|
-
}, (prevProps, nextProps) => {
|
|
3520
|
-
var _prevProps$toolMessag, _nextProps$toolMessag;
|
|
3521
|
-
if (prevProps.toolCall.id !== nextProps.toolCall.id) return false;
|
|
3522
|
-
if (prevProps.toolCall.function.name !== nextProps.toolCall.function.name) return false;
|
|
3523
|
-
if (prevProps.toolCall.function.arguments !== nextProps.toolCall.function.arguments) return false;
|
|
3524
|
-
if (((_prevProps$toolMessag = prevProps.toolMessage) === null || _prevProps$toolMessag === void 0 ? void 0 : _prevProps$toolMessag.content) !== ((_nextProps$toolMessag = nextProps.toolMessage) === null || _nextProps$toolMessag === void 0 ? void 0 : _nextProps$toolMessag.content)) return false;
|
|
3525
|
-
if (prevProps.isExecuting !== nextProps.isExecuting) return false;
|
|
3526
|
-
if (prevProps.RenderComponent !== nextProps.RenderComponent) return false;
|
|
3527
|
-
return true;
|
|
3528
|
-
});
|
|
3529
|
-
/**
|
|
3530
|
-
* Hook that returns a function to render tool calls based on the render functions
|
|
3531
|
-
* defined in CopilotKitProvider.
|
|
3532
|
-
*
|
|
3533
|
-
* @returns A function that takes a tool call and optional tool message and returns the rendered component
|
|
3534
|
-
*/
|
|
3535
|
-
function useRenderToolCall() {
|
|
3536
|
-
var _config$agentId;
|
|
3537
|
-
const { copilotkit, executingToolCallIds } = useCopilotKit();
|
|
3538
|
-
const config = useCopilotChatConfiguration();
|
|
3539
|
-
const agentId = (_config$agentId = config === null || config === void 0 ? void 0 : config.agentId) !== null && _config$agentId !== void 0 ? _config$agentId : _copilotkit_shared.DEFAULT_AGENT_ID;
|
|
3540
|
-
const renderToolCalls = (0, react.useSyncExternalStore)((callback) => {
|
|
3541
|
-
return copilotkit.subscribe({ onRenderToolCallsChanged: callback }).unsubscribe;
|
|
3542
|
-
}, () => copilotkit.renderToolCalls, () => copilotkit.renderToolCalls);
|
|
3543
|
-
return (0, react.useCallback)(({ toolCall, toolMessage }) => {
|
|
3544
|
-
const exactMatches = renderToolCalls.filter((rc) => rc.name === toolCall.function.name);
|
|
3545
|
-
const renderConfig = exactMatches.find((rc) => rc.agentId === agentId) || exactMatches.find((rc) => !rc.agentId) || exactMatches[0] || renderToolCalls.find((rc) => rc.name === "*");
|
|
3546
|
-
if (!renderConfig) return null;
|
|
3547
|
-
const RenderComponent = renderConfig.render;
|
|
3548
|
-
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCallRenderer, {
|
|
3549
|
-
toolCall,
|
|
3550
|
-
toolMessage,
|
|
3551
|
-
RenderComponent,
|
|
3552
|
-
isExecuting: executingToolCallIds.has(toolCall.id)
|
|
3553
|
-
}, toolCall.id);
|
|
3554
|
-
}, [
|
|
3555
|
-
renderToolCalls,
|
|
3556
|
-
executingToolCallIds,
|
|
3557
|
-
agentId
|
|
3558
|
-
]);
|
|
3559
|
-
}
|
|
3560
3563
|
|
|
3561
3564
|
//#endregion
|
|
3562
3565
|
//#region src/v2/hooks/use-render-custom-messages.tsx
|
|
@@ -3673,8 +3676,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3673
3676
|
tool.name,
|
|
3674
3677
|
tool.available,
|
|
3675
3678
|
copilotkit,
|
|
3676
|
-
extraDeps
|
|
3677
|
-
...extraDeps
|
|
3679
|
+
JSON.stringify(extraDeps)
|
|
3678
3680
|
]);
|
|
3679
3681
|
}
|
|
3680
3682
|
|
|
@@ -3813,8 +3815,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3813
3815
|
}, [
|
|
3814
3816
|
config.name,
|
|
3815
3817
|
copilotkit,
|
|
3816
|
-
extraDeps
|
|
3817
|
-
...extraDeps
|
|
3818
|
+
JSON.stringify(extraDeps)
|
|
3818
3819
|
]);
|
|
3819
3820
|
}
|
|
3820
3821
|
|
|
@@ -4163,7 +4164,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
4163
4164
|
});
|
|
4164
4165
|
}
|
|
4165
4166
|
};
|
|
4166
|
-
if (updateFlags.includes(UseAgentUpdate.OnMessagesChanged)) handlers.onMessagesChanged =
|
|
4167
|
+
if (updateFlags.includes(UseAgentUpdate.OnMessagesChanged)) handlers.onMessagesChanged = batchedForceUpdate;
|
|
4167
4168
|
if (updateFlags.includes(UseAgentUpdate.OnStateChanged)) handlers.onStateChanged = batchedForceUpdate;
|
|
4168
4169
|
if (updateFlags.includes(UseAgentUpdate.OnRunStatusChanged)) {
|
|
4169
4170
|
handlers.onRunInitialized = batchedForceUpdate;
|