@copilotkit/react-core 1.55.3-canary.1776260990 → 1.55.3-canary.1776979102
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-Dcf7Lkjb.d.mts → copilotkit-3mXoM0Hd.d.mts} +9 -36
- package/dist/copilotkit-3mXoM0Hd.d.mts.map +1 -0
- package/dist/{copilotkit-9_JxoZgF.d.cts → copilotkit-BDDjvB-p.d.cts} +9 -36
- package/dist/copilotkit-BDDjvB-p.d.cts.map +1 -0
- package/dist/{copilotkit-9l47K1Ot.cjs → copilotkit-BkcqmpWt.cjs} +163 -282
- package/dist/copilotkit-BkcqmpWt.cjs.map +1 -0
- package/dist/{copilotkit-a2pL_KeT.mjs → copilotkit-C7n8Umv9.mjs} +165 -278
- package/dist/copilotkit-C7n8Umv9.mjs.map +1 -0
- package/dist/index.cjs +4 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +4 -9
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +144 -233
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +1 -2
- package/dist/v2/index.d.cts +2 -2
- package/dist/v2/index.d.mts +2 -2
- package/dist/v2/index.mjs +2 -2
- package/dist/v2/index.umd.js +166 -282
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +6 -6
- package/src/components/copilot-provider/copilot-messages.tsx +24 -39
- package/src/components/copilot-provider/copilotkit-props.tsx +5 -9
- package/src/components/copilot-provider/copilotkit.tsx +1 -4
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +16 -27
- package/src/hooks/use-copilot-chat_internal.ts +4 -15
- package/src/v2/__tests__/utils/test-helpers.tsx +7 -40
- package/src/v2/components/chat/CopilotChat.tsx +1 -1
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +15 -18
- package/src/v2/components/chat/CopilotChatMessageView.tsx +2 -7
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +4 -17
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +10 -13
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +5 -131
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -60
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +1 -1
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +2 -5
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2 -5
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1 -55
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -8
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +10 -10
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +2 -13
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +4 -23
- package/src/v2/hooks/index.ts +0 -1
- package/src/v2/hooks/use-agent.tsx +10 -157
- package/src/v2/hooks/use-interrupt.tsx +1 -10
- package/src/v2/hooks/use-render-activity-message.tsx +3 -9
- package/src/v2/hooks/use-render-custom-messages.tsx +1 -6
- package/src/v2/providers/CopilotKitProvider.tsx +2 -6
- package/dist/copilotkit-9_JxoZgF.d.cts.map +0 -1
- package/dist/copilotkit-9l47K1Ot.cjs.map +0 -1
- package/dist/copilotkit-Dcf7Lkjb.d.mts.map +0 -1
- package/dist/copilotkit-a2pL_KeT.mjs.map +0 -1
- package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
- package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
- package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +0 -327
- package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
- package/src/v2/hooks/use-capabilities.tsx +0 -25
package/dist/index.umd.js
CHANGED
|
@@ -1954,7 +1954,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
1954
1954
|
}, [value, warningMessage]);
|
|
1955
1955
|
return value;
|
|
1956
1956
|
}
|
|
1957
|
-
const CopilotKitProvider = ({ children, runtimeUrl, headers
|
|
1957
|
+
const CopilotKitProvider = ({ children, runtimeUrl, headers = {}, credentials, publicApiKey, publicLicenseKey, licenseToken, properties = {}, agents__unsafe_dev_only: agents = {}, selfManagedAgents = {}, renderToolCalls, renderActivityMessages, renderCustomMessages, frontendTools, humanInTheLoop, openGenerativeUI, showDevConsole = false, useSingleEndpoint, onError, a2ui, defaultThrottleMs, inspectorDefaultAnchor }) => {
|
|
1958
1958
|
var _openGenerativeUI$des;
|
|
1959
1959
|
const [shouldRenderInspector, setShouldRenderInspector] = (0, react.useState)(false);
|
|
1960
1960
|
const [runtimeA2UIEnabled, setRuntimeA2UIEnabled] = (0, react.useState)(false);
|
|
@@ -2016,7 +2016,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2016
2016
|
...selfManagedAgents
|
|
2017
2017
|
}), [agents, selfManagedAgents]);
|
|
2018
2018
|
const hasLocalAgents = mergedAgents && Object.keys(mergedAgents).length > 0;
|
|
2019
|
-
const headers = typeof headersProp === "function" ? headersProp() : headersProp;
|
|
2020
2019
|
const mergedHeaders = (0, react.useMemo)(() => {
|
|
2021
2020
|
if (!resolvedPublicKey) return headers;
|
|
2022
2021
|
if (headers[HEADER_NAME]) return headers;
|
|
@@ -2378,204 +2377,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2378
2377
|
]);
|
|
2379
2378
|
}
|
|
2380
2379
|
|
|
2381
|
-
//#endregion
|
|
2382
|
-
//#region src/v2/hooks/use-agent.tsx
|
|
2383
|
-
let UseAgentUpdate = /* @__PURE__ */ function(UseAgentUpdate) {
|
|
2384
|
-
UseAgentUpdate["OnMessagesChanged"] = "OnMessagesChanged";
|
|
2385
|
-
UseAgentUpdate["OnStateChanged"] = "OnStateChanged";
|
|
2386
|
-
UseAgentUpdate["OnRunStatusChanged"] = "OnRunStatusChanged";
|
|
2387
|
-
return UseAgentUpdate;
|
|
2388
|
-
}({});
|
|
2389
|
-
const ALL_UPDATES = [
|
|
2390
|
-
UseAgentUpdate.OnMessagesChanged,
|
|
2391
|
-
UseAgentUpdate.OnStateChanged,
|
|
2392
|
-
UseAgentUpdate.OnRunStatusChanged
|
|
2393
|
-
];
|
|
2394
|
-
/**
|
|
2395
|
-
* Clone a registry agent for per-thread isolation.
|
|
2396
|
-
* Copies agent configuration (transport, headers, etc.) but resets conversation
|
|
2397
|
-
* state (messages, threadId, state) so each thread starts fresh.
|
|
2398
|
-
*/
|
|
2399
|
-
function cloneForThread(source, threadId, headers) {
|
|
2400
|
-
const clone = source.clone();
|
|
2401
|
-
if (clone === source) throw new Error(`useAgent: ${source.constructor.name}.clone() returned the same instance. clone() must return a new, independent object.`);
|
|
2402
|
-
clone.threadId = threadId;
|
|
2403
|
-
clone.setMessages([]);
|
|
2404
|
-
clone.setState({});
|
|
2405
|
-
if (clone instanceof _ag_ui_client.HttpAgent) clone.headers = { ...headers };
|
|
2406
|
-
return clone;
|
|
2407
|
-
}
|
|
2408
|
-
/**
|
|
2409
|
-
* Module-level WeakMap: registryAgent → (threadId → clone).
|
|
2410
|
-
* Shared across all useAgent() calls so that every component using the same
|
|
2411
|
-
* (agentId, threadId) pair receives the same agent instance. Using WeakMap
|
|
2412
|
-
* ensures the clone map is garbage-collected when the registry agent is
|
|
2413
|
-
* replaced (e.g. after reconnect or hot-reload).
|
|
2414
|
-
*/
|
|
2415
|
-
const globalThreadCloneMap = /* @__PURE__ */ new WeakMap();
|
|
2416
|
-
/**
|
|
2417
|
-
* Look up an existing per-thread clone without creating one.
|
|
2418
|
-
* Returns undefined when no clone has been created yet for this pair.
|
|
2419
|
-
*/
|
|
2420
|
-
function getThreadClone(registryAgent, threadId) {
|
|
2421
|
-
var _globalThreadCloneMap;
|
|
2422
|
-
if (!registryAgent || !threadId) return void 0;
|
|
2423
|
-
return (_globalThreadCloneMap = globalThreadCloneMap.get(registryAgent)) === null || _globalThreadCloneMap === void 0 ? void 0 : _globalThreadCloneMap.get(threadId);
|
|
2424
|
-
}
|
|
2425
|
-
function getOrCreateThreadClone(existing, threadId, headers) {
|
|
2426
|
-
let byThread = globalThreadCloneMap.get(existing);
|
|
2427
|
-
if (!byThread) {
|
|
2428
|
-
byThread = /* @__PURE__ */ new Map();
|
|
2429
|
-
globalThreadCloneMap.set(existing, byThread);
|
|
2430
|
-
}
|
|
2431
|
-
const cached = byThread.get(threadId);
|
|
2432
|
-
if (cached) return cached;
|
|
2433
|
-
const clone = cloneForThread(existing, threadId, headers);
|
|
2434
|
-
byThread.set(threadId, clone);
|
|
2435
|
-
return clone;
|
|
2436
|
-
}
|
|
2437
|
-
function useAgent({ agentId, threadId, updates, throttleMs } = {}) {
|
|
2438
|
-
var _agentId, _threadId;
|
|
2439
|
-
(_agentId = agentId) !== null && _agentId !== void 0 || (agentId = _copilotkit_shared.DEFAULT_AGENT_ID);
|
|
2440
|
-
const { copilotkit } = useCopilotKit();
|
|
2441
|
-
const providerThrottleMs = copilotkit.defaultThrottleMs;
|
|
2442
|
-
const chatConfig = useCopilotChatConfiguration();
|
|
2443
|
-
(_threadId = threadId) !== null && _threadId !== void 0 || (threadId = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.threadId);
|
|
2444
|
-
const effectiveThrottleMs = (0, react.useMemo)(() => {
|
|
2445
|
-
var _ref;
|
|
2446
|
-
const resolved = (_ref = throttleMs !== null && throttleMs !== void 0 ? throttleMs : providerThrottleMs) !== null && _ref !== void 0 ? _ref : 0;
|
|
2447
|
-
if (!Number.isFinite(resolved) || resolved < 0) {
|
|
2448
|
-
const source = throttleMs !== void 0 ? "hook-level throttleMs" : "provider-level defaultThrottleMs";
|
|
2449
|
-
console.error(`useAgent: ${source} must be a non-negative finite number, got ${resolved}. Falling back to unthrottled.`);
|
|
2450
|
-
return 0;
|
|
2451
|
-
}
|
|
2452
|
-
return resolved;
|
|
2453
|
-
}, [throttleMs, providerThrottleMs]);
|
|
2454
|
-
const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
|
|
2455
|
-
const updateFlags = (0, react.useMemo)(() => updates !== null && updates !== void 0 ? updates : ALL_UPDATES, [JSON.stringify(updates)]);
|
|
2456
|
-
const provisionalAgentCache = (0, react.useRef)(/* @__PURE__ */ new Map());
|
|
2457
|
-
const agent = (0, react.useMemo)(() => {
|
|
2458
|
-
var _copilotkit$agents;
|
|
2459
|
-
const cacheKey = threadId ? `${agentId}:${threadId}` : agentId;
|
|
2460
|
-
const existing = copilotkit.getAgent(agentId);
|
|
2461
|
-
if (existing) {
|
|
2462
|
-
provisionalAgentCache.current.delete(cacheKey);
|
|
2463
|
-
provisionalAgentCache.current.delete(agentId);
|
|
2464
|
-
if (!threadId) return existing;
|
|
2465
|
-
return getOrCreateThreadClone(existing, threadId, copilotkit.headers);
|
|
2466
|
-
}
|
|
2467
|
-
const isRuntimeConfigured = copilotkit.runtimeUrl !== void 0;
|
|
2468
|
-
const status = copilotkit.runtimeConnectionStatus;
|
|
2469
|
-
if (isRuntimeConfigured && (status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Disconnected || status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Connecting)) {
|
|
2470
|
-
const cached = provisionalAgentCache.current.get(cacheKey);
|
|
2471
|
-
if (cached) {
|
|
2472
|
-
cached.headers = { ...copilotkit.headers };
|
|
2473
|
-
return cached;
|
|
2474
|
-
}
|
|
2475
|
-
const provisional = new _copilotkit_core.ProxiedCopilotRuntimeAgent({
|
|
2476
|
-
runtimeUrl: copilotkit.runtimeUrl,
|
|
2477
|
-
agentId,
|
|
2478
|
-
transport: copilotkit.runtimeTransport,
|
|
2479
|
-
runtimeMode: "pending"
|
|
2480
|
-
});
|
|
2481
|
-
provisional.headers = { ...copilotkit.headers };
|
|
2482
|
-
if (threadId) provisional.threadId = threadId;
|
|
2483
|
-
provisionalAgentCache.current.set(cacheKey, provisional);
|
|
2484
|
-
return provisional;
|
|
2485
|
-
}
|
|
2486
|
-
if (isRuntimeConfigured && status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Error) {
|
|
2487
|
-
const cached = provisionalAgentCache.current.get(cacheKey);
|
|
2488
|
-
if (cached) {
|
|
2489
|
-
cached.headers = { ...copilotkit.headers };
|
|
2490
|
-
return cached;
|
|
2491
|
-
}
|
|
2492
|
-
const provisional = new _copilotkit_core.ProxiedCopilotRuntimeAgent({
|
|
2493
|
-
runtimeUrl: copilotkit.runtimeUrl,
|
|
2494
|
-
agentId,
|
|
2495
|
-
transport: copilotkit.runtimeTransport,
|
|
2496
|
-
runtimeMode: "pending"
|
|
2497
|
-
});
|
|
2498
|
-
provisional.headers = { ...copilotkit.headers };
|
|
2499
|
-
if (threadId) provisional.threadId = threadId;
|
|
2500
|
-
provisionalAgentCache.current.set(cacheKey, provisional);
|
|
2501
|
-
return provisional;
|
|
2502
|
-
}
|
|
2503
|
-
const knownAgents = Object.keys((_copilotkit$agents = copilotkit.agents) !== null && _copilotkit$agents !== void 0 ? _copilotkit$agents : {});
|
|
2504
|
-
const runtimePart = isRuntimeConfigured ? `runtimeUrl=${copilotkit.runtimeUrl}` : "no runtimeUrl";
|
|
2505
|
-
throw new Error(`useAgent: Agent '${agentId}' not found after runtime sync (${runtimePart}). ` + (knownAgents.length ? `Known agents: [${knownAgents.join(", ")}]` : "No agents registered.") + " Verify your runtime /info and/or agents__unsafe_dev_only.");
|
|
2506
|
-
}, [
|
|
2507
|
-
agentId,
|
|
2508
|
-
threadId,
|
|
2509
|
-
copilotkit.agents,
|
|
2510
|
-
copilotkit.runtimeConnectionStatus,
|
|
2511
|
-
copilotkit.runtimeUrl,
|
|
2512
|
-
copilotkit.runtimeTransport,
|
|
2513
|
-
JSON.stringify(copilotkit.headers)
|
|
2514
|
-
]);
|
|
2515
|
-
(0, react.useEffect)(() => {
|
|
2516
|
-
if (updateFlags.length === 0) return;
|
|
2517
|
-
const handlers = {};
|
|
2518
|
-
let timerId = null;
|
|
2519
|
-
let active = true;
|
|
2520
|
-
let batchScheduled = false;
|
|
2521
|
-
const batchedForceUpdate = () => {
|
|
2522
|
-
if (!active) return;
|
|
2523
|
-
if (!batchScheduled) {
|
|
2524
|
-
batchScheduled = true;
|
|
2525
|
-
queueMicrotask(() => {
|
|
2526
|
-
batchScheduled = false;
|
|
2527
|
-
if (active) forceUpdate();
|
|
2528
|
-
});
|
|
2529
|
-
}
|
|
2530
|
-
};
|
|
2531
|
-
if (updateFlags.includes(UseAgentUpdate.OnMessagesChanged)) {
|
|
2532
|
-
const ms = effectiveThrottleMs;
|
|
2533
|
-
if (ms > 0) {
|
|
2534
|
-
let throttleActive = false;
|
|
2535
|
-
let pending = false;
|
|
2536
|
-
const throttledNotify = () => {
|
|
2537
|
-
if (!active) return;
|
|
2538
|
-
if (!throttleActive) {
|
|
2539
|
-
throttleActive = true;
|
|
2540
|
-
pending = false;
|
|
2541
|
-
forceUpdate();
|
|
2542
|
-
timerId = setTimeout(function trailingEdge() {
|
|
2543
|
-
timerId = null;
|
|
2544
|
-
if (active && pending) {
|
|
2545
|
-
pending = false;
|
|
2546
|
-
forceUpdate();
|
|
2547
|
-
timerId = setTimeout(trailingEdge, ms);
|
|
2548
|
-
} else throttleActive = false;
|
|
2549
|
-
}, ms);
|
|
2550
|
-
} else pending = true;
|
|
2551
|
-
};
|
|
2552
|
-
handlers.onMessagesChanged = throttledNotify;
|
|
2553
|
-
} else handlers.onMessagesChanged = forceUpdate;
|
|
2554
|
-
}
|
|
2555
|
-
if (updateFlags.includes(UseAgentUpdate.OnStateChanged)) handlers.onStateChanged = batchedForceUpdate;
|
|
2556
|
-
if (updateFlags.includes(UseAgentUpdate.OnRunStatusChanged)) {
|
|
2557
|
-
handlers.onRunInitialized = batchedForceUpdate;
|
|
2558
|
-
handlers.onRunFinalized = batchedForceUpdate;
|
|
2559
|
-
handlers.onRunFailed = batchedForceUpdate;
|
|
2560
|
-
}
|
|
2561
|
-
const subscription = agent.subscribe(handlers);
|
|
2562
|
-
return () => {
|
|
2563
|
-
active = false;
|
|
2564
|
-
if (timerId !== null) clearTimeout(timerId);
|
|
2565
|
-
subscription.unsubscribe();
|
|
2566
|
-
};
|
|
2567
|
-
}, [
|
|
2568
|
-
agent,
|
|
2569
|
-
forceUpdate,
|
|
2570
|
-
effectiveThrottleMs,
|
|
2571
|
-
updateFlags
|
|
2572
|
-
]);
|
|
2573
|
-
(0, react.useEffect)(() => {
|
|
2574
|
-
if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...copilotkit.headers };
|
|
2575
|
-
}, [agent, JSON.stringify(copilotkit.headers)]);
|
|
2576
|
-
return { agent };
|
|
2577
|
-
}
|
|
2578
|
-
|
|
2579
2380
|
//#endregion
|
|
2580
2381
|
//#region src/v2/hooks/use-render-custom-messages.tsx
|
|
2581
2382
|
function useRenderCustomMessages() {
|
|
@@ -2589,13 +2390,12 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2589
2390
|
return aHasAgent ? -1 : 1;
|
|
2590
2391
|
});
|
|
2591
2392
|
return function(params) {
|
|
2592
|
-
var _copilotkit$getRunIdF
|
|
2393
|
+
var _copilotkit$getRunIdF;
|
|
2593
2394
|
if (!customMessageRenderers.length) return null;
|
|
2594
2395
|
const { message, position } = params;
|
|
2595
2396
|
const resolvedRunId = (_copilotkit$getRunIdF = copilotkit.getRunIdForMessage(agentId, threadId, message.id)) !== null && _copilotkit$getRunIdF !== void 0 ? _copilotkit$getRunIdF : copilotkit.getRunIdsForThread(agentId, threadId).slice(-1)[0];
|
|
2596
2397
|
const runId = resolvedRunId !== null && resolvedRunId !== void 0 ? resolvedRunId : `missing-run-id:${message.id}`;
|
|
2597
|
-
const
|
|
2598
|
-
const agent = (_getThreadClone = getThreadClone(registryAgent, threadId)) !== null && _getThreadClone !== void 0 ? _getThreadClone : registryAgent;
|
|
2398
|
+
const agent = copilotkit.getAgent(agentId);
|
|
2599
2399
|
if (!agent) throw new Error("Agent not found");
|
|
2600
2400
|
const messagesIdsInRun = resolvedRunId ? agent.messages.filter((msg) => copilotkit.getRunIdForMessage(agentId, threadId, msg.id) === resolvedRunId).map((msg) => msg.id) : [message.id];
|
|
2601
2401
|
const rawMessageIndex = agent.messages.findIndex((msg) => msg.id === message.id);
|
|
@@ -2723,6 +2523,133 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2723
2523
|
]);
|
|
2724
2524
|
}
|
|
2725
2525
|
|
|
2526
|
+
//#endregion
|
|
2527
|
+
//#region src/v2/hooks/use-agent.tsx
|
|
2528
|
+
let UseAgentUpdate = /* @__PURE__ */ function(UseAgentUpdate) {
|
|
2529
|
+
UseAgentUpdate["OnMessagesChanged"] = "OnMessagesChanged";
|
|
2530
|
+
UseAgentUpdate["OnStateChanged"] = "OnStateChanged";
|
|
2531
|
+
UseAgentUpdate["OnRunStatusChanged"] = "OnRunStatusChanged";
|
|
2532
|
+
return UseAgentUpdate;
|
|
2533
|
+
}({});
|
|
2534
|
+
const ALL_UPDATES = [
|
|
2535
|
+
UseAgentUpdate.OnMessagesChanged,
|
|
2536
|
+
UseAgentUpdate.OnStateChanged,
|
|
2537
|
+
UseAgentUpdate.OnRunStatusChanged
|
|
2538
|
+
];
|
|
2539
|
+
function useAgent({ agentId, updates, throttleMs } = {}) {
|
|
2540
|
+
var _agentId;
|
|
2541
|
+
(_agentId = agentId) !== null && _agentId !== void 0 || (agentId = _copilotkit_shared.DEFAULT_AGENT_ID);
|
|
2542
|
+
const { copilotkit } = useCopilotKit();
|
|
2543
|
+
const providerThrottleMs = copilotkit.defaultThrottleMs;
|
|
2544
|
+
const effectiveThrottleMs = (0, react.useMemo)(() => {
|
|
2545
|
+
var _ref;
|
|
2546
|
+
const resolved = (_ref = throttleMs !== null && throttleMs !== void 0 ? throttleMs : providerThrottleMs) !== null && _ref !== void 0 ? _ref : 0;
|
|
2547
|
+
if (!Number.isFinite(resolved) || resolved < 0) {
|
|
2548
|
+
const source = throttleMs !== void 0 ? "hook-level throttleMs" : "provider-level defaultThrottleMs";
|
|
2549
|
+
console.error(`useAgent: ${source} must be a non-negative finite number, got ${resolved}. Falling back to unthrottled.`);
|
|
2550
|
+
return 0;
|
|
2551
|
+
}
|
|
2552
|
+
return resolved;
|
|
2553
|
+
}, [throttleMs, providerThrottleMs]);
|
|
2554
|
+
const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0);
|
|
2555
|
+
const updateFlags = (0, react.useMemo)(() => updates !== null && updates !== void 0 ? updates : ALL_UPDATES, [JSON.stringify(updates)]);
|
|
2556
|
+
const provisionalAgentCache = (0, react.useRef)(/* @__PURE__ */ new Map());
|
|
2557
|
+
const agent = (0, react.useMemo)(() => {
|
|
2558
|
+
var _copilotkit$agents;
|
|
2559
|
+
const existing = copilotkit.getAgent(agentId);
|
|
2560
|
+
if (existing) {
|
|
2561
|
+
provisionalAgentCache.current.delete(agentId);
|
|
2562
|
+
return existing;
|
|
2563
|
+
}
|
|
2564
|
+
const isRuntimeConfigured = copilotkit.runtimeUrl !== void 0;
|
|
2565
|
+
const status = copilotkit.runtimeConnectionStatus;
|
|
2566
|
+
if (isRuntimeConfigured && (status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Disconnected || status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Connecting)) {
|
|
2567
|
+
const cached = provisionalAgentCache.current.get(agentId);
|
|
2568
|
+
if (cached) {
|
|
2569
|
+
cached.headers = { ...copilotkit.headers };
|
|
2570
|
+
return cached;
|
|
2571
|
+
}
|
|
2572
|
+
const provisional = new _copilotkit_core.ProxiedCopilotRuntimeAgent({
|
|
2573
|
+
runtimeUrl: copilotkit.runtimeUrl,
|
|
2574
|
+
agentId,
|
|
2575
|
+
transport: copilotkit.runtimeTransport,
|
|
2576
|
+
runtimeMode: "pending"
|
|
2577
|
+
});
|
|
2578
|
+
provisional.headers = { ...copilotkit.headers };
|
|
2579
|
+
provisionalAgentCache.current.set(agentId, provisional);
|
|
2580
|
+
return provisional;
|
|
2581
|
+
}
|
|
2582
|
+
if (isRuntimeConfigured && status === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Error) {
|
|
2583
|
+
const provisional = new _copilotkit_core.ProxiedCopilotRuntimeAgent({
|
|
2584
|
+
runtimeUrl: copilotkit.runtimeUrl,
|
|
2585
|
+
agentId,
|
|
2586
|
+
transport: copilotkit.runtimeTransport,
|
|
2587
|
+
runtimeMode: "pending"
|
|
2588
|
+
});
|
|
2589
|
+
provisional.headers = { ...copilotkit.headers };
|
|
2590
|
+
return provisional;
|
|
2591
|
+
}
|
|
2592
|
+
const knownAgents = Object.keys((_copilotkit$agents = copilotkit.agents) !== null && _copilotkit$agents !== void 0 ? _copilotkit$agents : {});
|
|
2593
|
+
const runtimePart = isRuntimeConfigured ? `runtimeUrl=${copilotkit.runtimeUrl}` : "no runtimeUrl";
|
|
2594
|
+
throw new Error(`useAgent: Agent '${agentId}' not found after runtime sync (${runtimePart}). ` + (knownAgents.length ? `Known agents: [${knownAgents.join(", ")}]` : "No agents registered.") + " Verify your runtime /info and/or agents__unsafe_dev_only.");
|
|
2595
|
+
}, [
|
|
2596
|
+
agentId,
|
|
2597
|
+
copilotkit.agents,
|
|
2598
|
+
copilotkit.runtimeConnectionStatus,
|
|
2599
|
+
copilotkit.runtimeUrl,
|
|
2600
|
+
copilotkit.runtimeTransport,
|
|
2601
|
+
JSON.stringify(copilotkit.headers)
|
|
2602
|
+
]);
|
|
2603
|
+
(0, react.useEffect)(() => {
|
|
2604
|
+
if (updateFlags.length === 0) return;
|
|
2605
|
+
const handlers = {};
|
|
2606
|
+
let timerId = null;
|
|
2607
|
+
let active = true;
|
|
2608
|
+
if (updateFlags.includes(UseAgentUpdate.OnMessagesChanged)) {
|
|
2609
|
+
const ms = effectiveThrottleMs;
|
|
2610
|
+
if (ms > 0) {
|
|
2611
|
+
let throttleActive = false;
|
|
2612
|
+
let pending = false;
|
|
2613
|
+
const throttledNotify = () => {
|
|
2614
|
+
if (!active) return;
|
|
2615
|
+
if (!throttleActive) {
|
|
2616
|
+
throttleActive = true;
|
|
2617
|
+
pending = false;
|
|
2618
|
+
forceUpdate();
|
|
2619
|
+
timerId = setTimeout(function trailingEdge() {
|
|
2620
|
+
timerId = null;
|
|
2621
|
+
if (active && pending) {
|
|
2622
|
+
pending = false;
|
|
2623
|
+
forceUpdate();
|
|
2624
|
+
timerId = setTimeout(trailingEdge, ms);
|
|
2625
|
+
} else throttleActive = false;
|
|
2626
|
+
}, ms);
|
|
2627
|
+
} else pending = true;
|
|
2628
|
+
};
|
|
2629
|
+
handlers.onMessagesChanged = throttledNotify;
|
|
2630
|
+
} else handlers.onMessagesChanged = forceUpdate;
|
|
2631
|
+
}
|
|
2632
|
+
if (updateFlags.includes(UseAgentUpdate.OnStateChanged)) handlers.onStateChanged = forceUpdate;
|
|
2633
|
+
if (updateFlags.includes(UseAgentUpdate.OnRunStatusChanged)) {
|
|
2634
|
+
handlers.onRunInitialized = forceUpdate;
|
|
2635
|
+
handlers.onRunFinalized = forceUpdate;
|
|
2636
|
+
handlers.onRunFailed = forceUpdate;
|
|
2637
|
+
}
|
|
2638
|
+
const subscription = agent.subscribe(handlers);
|
|
2639
|
+
return () => {
|
|
2640
|
+
active = false;
|
|
2641
|
+
if (timerId !== null) clearTimeout(timerId);
|
|
2642
|
+
subscription.unsubscribe();
|
|
2643
|
+
};
|
|
2644
|
+
}, [
|
|
2645
|
+
agent,
|
|
2646
|
+
forceUpdate,
|
|
2647
|
+
effectiveThrottleMs,
|
|
2648
|
+
updateFlags
|
|
2649
|
+
]);
|
|
2650
|
+
return { agent };
|
|
2651
|
+
}
|
|
2652
|
+
|
|
2726
2653
|
//#endregion
|
|
2727
2654
|
//#region src/v2/hooks/use-suggestions.tsx
|
|
2728
2655
|
function useSuggestions({ agentId } = {}) {
|
|
@@ -2988,10 +2915,8 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
2988
2915
|
* ```
|
|
2989
2916
|
*/
|
|
2990
2917
|
function useInterrupt(config) {
|
|
2991
|
-
var _config$agent;
|
|
2992
2918
|
const { copilotkit } = useCopilotKit();
|
|
2993
|
-
const { agent
|
|
2994
|
-
const agent = (_config$agent = config.agent) !== null && _config$agent !== void 0 ? _config$agent : internalAgent;
|
|
2919
|
+
const { agent } = useAgent({ agentId: config.agentId });
|
|
2995
2920
|
const [pendingEvent, setPendingEvent] = (0, react.useState)(null);
|
|
2996
2921
|
const pendingEventRef = (0, react.useRef)(pendingEvent);
|
|
2997
2922
|
pendingEventRef.current = pendingEvent;
|
|
@@ -3682,20 +3607,6 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3682
3607
|
/**
|
|
3683
3608
|
* An internal context to separate the messages state (which is constantly changing) from the rest of CopilotKit context
|
|
3684
3609
|
*/
|
|
3685
|
-
/**
|
|
3686
|
-
* Determine whether a GraphQL error should be suppressed based on its visibility
|
|
3687
|
-
* and whether the dev console is active.
|
|
3688
|
-
*
|
|
3689
|
-
* Returns `null` when the error should be surfaced to the UI, or a log prefix
|
|
3690
|
-
* string when the error should be suppressed (logged to console only).
|
|
3691
|
-
*
|
|
3692
|
-
* Exported for unit testing.
|
|
3693
|
-
*/
|
|
3694
|
-
function getErrorSuppression(visibility, isDev) {
|
|
3695
|
-
if (visibility === _copilotkit_shared.ErrorVisibility.SILENT) return "CopilotKit Silent Error:";
|
|
3696
|
-
if (!isDev && visibility === _copilotkit_shared.ErrorVisibility.DEV_ONLY) return "CopilotKit Error (hidden in production):";
|
|
3697
|
-
return null;
|
|
3698
|
-
}
|
|
3699
3610
|
const MessagesTapContext = (0, react.createContext)(null);
|
|
3700
3611
|
function useMessagesTap() {
|
|
3701
3612
|
const tap = (0, react.useContext)(MessagesTapContext);
|
|
@@ -3780,9 +3691,13 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3780
3691
|
const graphQLErrors = error.graphQLErrors;
|
|
3781
3692
|
const routeError = (gqlError) => {
|
|
3782
3693
|
const extensions = gqlError.extensions;
|
|
3783
|
-
const
|
|
3784
|
-
if (
|
|
3785
|
-
console.error(
|
|
3694
|
+
const visibility = extensions === null || extensions === void 0 ? void 0 : extensions.visibility;
|
|
3695
|
+
if (!shouldShowDevConsole(showDevConsole)) {
|
|
3696
|
+
console.error("CopilotKit Error (hidden in production):", gqlError.message);
|
|
3697
|
+
return;
|
|
3698
|
+
}
|
|
3699
|
+
if (visibility === _copilotkit_shared.ErrorVisibility.SILENT) {
|
|
3700
|
+
console.error("CopilotKit Silent Error:", gqlError.message);
|
|
3786
3701
|
return;
|
|
3787
3702
|
}
|
|
3788
3703
|
const ckError = createStructuredError(gqlError);
|
|
@@ -3799,7 +3714,8 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
3799
3714
|
}
|
|
3800
3715
|
};
|
|
3801
3716
|
graphQLErrors.forEach(routeError);
|
|
3802
|
-
} else
|
|
3717
|
+
} else if (!shouldShowDevConsole(showDevConsole)) console.error("CopilotKit Error (hidden in production):", error);
|
|
3718
|
+
else {
|
|
3803
3719
|
const fallbackError = new _copilotkit_shared.CopilotKitError({
|
|
3804
3720
|
message: (error === null || error === void 0 ? void 0 : error.message) || String(error),
|
|
3805
3721
|
code: _copilotkit_shared.CopilotKitErrorCode.UNKNOWN
|
|
@@ -4901,7 +4817,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
4901
4817
|
publicApiKey,
|
|
4902
4818
|
...cloud ? { cloud } : {},
|
|
4903
4819
|
chatApiEndpoint,
|
|
4904
|
-
headers:
|
|
4820
|
+
headers: props.headers || {},
|
|
4905
4821
|
properties: props.properties || {},
|
|
4906
4822
|
transcribeAudioUrl: props.transcribeAudioUrl,
|
|
4907
4823
|
textToSpeechUrl: props.textToSpeechUrl,
|
|
@@ -5279,11 +5195,7 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
5279
5195
|
const existingConfig = useCopilotChatConfiguration();
|
|
5280
5196
|
const [agentAvailable, setAgentAvailable] = (0, react.useState)(false);
|
|
5281
5197
|
const resolvedAgentId = (_existingConfig$agent = existingConfig === null || existingConfig === void 0 ? void 0 : existingConfig.agentId) !== null && _existingConfig$agent !== void 0 ? _existingConfig$agent : "default";
|
|
5282
|
-
const { agent } = useAgent({
|
|
5283
|
-
agentId: resolvedAgentId,
|
|
5284
|
-
threadId: existingConfig === null || existingConfig === void 0 ? void 0 : existingConfig.threadId
|
|
5285
|
-
});
|
|
5286
|
-
const lastConnectedAgentRef = (0, react.useRef)(null);
|
|
5198
|
+
const { agent } = useAgent({ agentId: resolvedAgentId });
|
|
5287
5199
|
(0, react.useEffect)(() => {
|
|
5288
5200
|
let detached = false;
|
|
5289
5201
|
const connectAbortController = new AbortController();
|
|
@@ -5298,12 +5210,11 @@ window.parent.postMessage({jsonrpc:"2.0",method:"ui/notifications/sandbox-proxy-
|
|
|
5298
5210
|
if (error instanceof _ag_ui_client.AGUIConnectNotImplementedError) {} else console.error("CopilotChat: connectAgent failed", error);
|
|
5299
5211
|
}
|
|
5300
5212
|
};
|
|
5301
|
-
if (agent && agent !==
|
|
5302
|
-
|
|
5213
|
+
if (agent && (existingConfig === null || existingConfig === void 0 ? void 0 : existingConfig.threadId) && agent.threadId !== existingConfig.threadId && copilotkit.runtimeConnectionStatus === _copilotkit_core.CopilotKitCoreRuntimeConnectionStatus.Connected) {
|
|
5214
|
+
agent.threadId = existingConfig.threadId;
|
|
5303
5215
|
connect(agent);
|
|
5304
5216
|
}
|
|
5305
5217
|
return () => {
|
|
5306
|
-
lastConnectedAgentRef.current = null;
|
|
5307
5218
|
detached = true;
|
|
5308
5219
|
connectAbortController.abort();
|
|
5309
5220
|
agent === null || agent === void 0 || agent.detachActiveRun();
|