agents 0.14.5 → 0.16.0
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/{agent-tool-types-V25Z_HcX.d.ts → agent-tool-types-NofdbL9X.d.ts} +182 -112
- package/dist/agent-tool-types.d.ts +1 -1
- package/dist/{agent-tools-C-9s151X.d.ts → agent-tools-DLquv-dp.d.ts} +2 -2
- package/dist/agent-tools.d.ts +1 -1
- package/dist/browser/ai.d.ts +126 -7
- package/dist/browser/ai.js +73 -29
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/index.d.ts +81 -69
- package/dist/browser/index.js +3 -2
- package/dist/browser/tanstack-ai.d.ts +13 -7
- package/dist/browser/tanstack-ai.js +18 -19
- package/dist/browser/tanstack-ai.js.map +1 -1
- package/dist/chat/index.d.ts +111 -5
- package/dist/chat/index.js +207 -35
- package/dist/chat/index.js.map +1 -1
- package/dist/chat-sdk/index.d.ts +1 -1
- package/dist/{classPrivateFieldGet2-Beqsfu2Z.js → classPrivateFieldGet2-CZ7QjTXN.js} +5 -5
- package/dist/{classPrivateMethodInitSpec-B5ko1s2R.js → classPrivateMethodInitSpec-D-0__zd9.js} +2 -2
- package/dist/client.d.ts +19 -2
- package/dist/client.js +31 -11
- package/dist/client.js.map +1 -1
- package/dist/{compaction-helpers-BEUILPss.d.ts → compaction-helpers-DVcu5lPN.d.ts} +91 -12
- package/dist/connector-D6yYzYHg.js +1080 -0
- package/dist/connector-D6yYzYHg.js.map +1 -0
- package/dist/connector-DXursxV5.d.ts +340 -0
- package/dist/experimental/memory/session/index.d.ts +75 -12
- package/dist/experimental/memory/session/index.js +226 -21
- package/dist/experimental/memory/session/index.js.map +1 -1
- package/dist/experimental/memory/utils/index.d.ts +2 -2
- package/dist/{index-CPe1OtI0.d.ts → index-B7IbEeze.d.ts} +32 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +116 -45
- package/dist/index.js.map +1 -1
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +262 -487
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +1 -1
- package/dist/react.d.ts +12 -1
- package/dist/react.js +101 -30
- package/dist/react.js.map +1 -1
- package/dist/{retries-CF_HKSlJ.d.ts → retries-CwlpAGet.d.ts} +35 -5
- package/dist/retries.d.ts +9 -5
- package/dist/retries.js +87 -1
- package/dist/retries.js.map +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/skills/index.js +2 -2
- package/dist/sub-routing.d.ts +1 -1
- package/dist/workflows.d.ts +1 -1
- package/package.json +10 -10
- package/dist/shared-4CAYLCTO.d.ts +0 -34
- package/dist/shared-wyII629d.js +0 -432
- package/dist/shared-wyII629d.js.map +0 -1
package/dist/react.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
V as MCPServersState,
|
|
3
3
|
d as AgentToolRunState
|
|
4
|
-
} from "./agent-tool-types-
|
|
4
|
+
} from "./agent-tool-types-NofdbL9X.js";
|
|
5
5
|
import { ClientParameters } from "./serializable.js";
|
|
6
6
|
import {
|
|
7
7
|
AgentPromiseReturnType,
|
|
@@ -146,6 +146,17 @@ type UseAgentOptions<State = unknown> = Omit<
|
|
|
146
146
|
agent: string;
|
|
147
147
|
name: string;
|
|
148
148
|
}>;
|
|
149
|
+
/**
|
|
150
|
+
* Default timeout (in milliseconds) applied to non-streaming `call()`s
|
|
151
|
+
* that don't pass an explicit `timeout`. Acts as a backstop so calls
|
|
152
|
+
* whose response is lost (e.g. the connection is replaced mid-flight)
|
|
153
|
+
* reject instead of hanging forever.
|
|
154
|
+
*
|
|
155
|
+
* Defaults to 30 000 ms. Set to `0` to disable the default timeout.
|
|
156
|
+
* Streaming calls never get a default timeout (long-lived streams are
|
|
157
|
+
* legitimate); pass an explicit `timeout` to bound them.
|
|
158
|
+
*/
|
|
159
|
+
defaultCallTimeout?: number;
|
|
149
160
|
};
|
|
150
161
|
type OptionalArgsAgentMethodCall<AgentT> = <
|
|
151
162
|
K extends keyof OptionalAgentMethods<AgentT>
|
package/dist/react.js
CHANGED
|
@@ -67,7 +67,7 @@ const _testUtils = {
|
|
|
67
67
|
};
|
|
68
68
|
function useAgent(options) {
|
|
69
69
|
const agentNamespace = camelCaseToKebabCase(options.agent);
|
|
70
|
-
const { query, queryDeps, cacheTtl, sub: subOption, path: userPath, ...restOptions } = options;
|
|
70
|
+
const { query, queryDeps, cacheTtl, sub: subOption, path: userPath, defaultCallTimeout, ...restOptions } = options;
|
|
71
71
|
const subChain = useMemo(() => (subOption ?? []).map((s) => ({
|
|
72
72
|
agent: s.agent,
|
|
73
73
|
name: s.name
|
|
@@ -83,6 +83,38 @@ function useAgent(options) {
|
|
|
83
83
|
subChain
|
|
84
84
|
]);
|
|
85
85
|
const pendingCallsRef = useRef(/* @__PURE__ */ new Map());
|
|
86
|
+
const socketRef = useRef(null);
|
|
87
|
+
const defaultCallTimeoutRef = useRef(defaultCallTimeout ?? 3e4);
|
|
88
|
+
defaultCallTimeoutRef.current = defaultCallTimeout ?? 3e4;
|
|
89
|
+
/** Reject (and remove) every pending call transmitted on `socket`. */
|
|
90
|
+
const rejectCallsSentOn = (socket, reason) => {
|
|
91
|
+
const error = new Error(reason);
|
|
92
|
+
for (const [id, pending] of pendingCallsRef.current) if (pending.sentOn === socket) {
|
|
93
|
+
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
|
94
|
+
pendingCallsRef.current.delete(id);
|
|
95
|
+
pending.reject(error);
|
|
96
|
+
pending.stream?.onError?.(reason);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
/** Transmit queued (never-sent) calls if the live socket is open. */
|
|
100
|
+
const flushQueuedCalls = () => {
|
|
101
|
+
const socket = socketRef.current;
|
|
102
|
+
if (!socket || socket.readyState !== socket.OPEN) return;
|
|
103
|
+
for (const pending of pendingCallsRef.current.values()) if (pending.sentOn === null) {
|
|
104
|
+
socket.send(pending.request);
|
|
105
|
+
pending.sentOn = socket;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/** Reject (and remove) every still-queued (never transmitted) call. */
|
|
109
|
+
const rejectQueuedCalls = (reason) => {
|
|
110
|
+
const error = new Error(reason);
|
|
111
|
+
for (const [id, pending] of pendingCallsRef.current) if (pending.sentOn === null) {
|
|
112
|
+
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
|
113
|
+
pendingCallsRef.current.delete(id);
|
|
114
|
+
pending.reject(error);
|
|
115
|
+
pending.stream?.onError?.(reason);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
86
118
|
const cacheKey = useMemo(() => createCacheKey(agentNamespace, options.name, subChain, queryDeps || []), [
|
|
87
119
|
agentNamespace,
|
|
88
120
|
options.name,
|
|
@@ -176,9 +208,20 @@ function useAgent(options) {
|
|
|
176
208
|
...restOptions
|
|
177
209
|
};
|
|
178
210
|
const socketEnabled = !awaitingQueryRefresh && (restOptions.enabled ?? true);
|
|
211
|
+
const addressKey = JSON.stringify([
|
|
212
|
+
options.host ?? null,
|
|
213
|
+
options.basePath ?? null,
|
|
214
|
+
agentNamespace,
|
|
215
|
+
options.name || "default",
|
|
216
|
+
combinedPath || null
|
|
217
|
+
]);
|
|
179
218
|
const agent = usePartySocket({
|
|
180
219
|
...socketOptions,
|
|
181
220
|
enabled: socketEnabled,
|
|
221
|
+
onOpen: (event) => {
|
|
222
|
+
flushQueuedCalls();
|
|
223
|
+
options.onOpen?.(event);
|
|
224
|
+
},
|
|
182
225
|
onMessage: (message) => {
|
|
183
226
|
if (typeof message.data === "string") {
|
|
184
227
|
let parsedMessage;
|
|
@@ -237,7 +280,10 @@ function useAgent(options) {
|
|
|
237
280
|
if (parsedMessage.type === "rpc") {
|
|
238
281
|
const response = parsedMessage;
|
|
239
282
|
const pending = pendingCallsRef.current.get(response.id);
|
|
240
|
-
if (!pending)
|
|
283
|
+
if (!pending) {
|
|
284
|
+
console.warn(`[useAgent] Discarded an RPC response with no matching pending call (id "${response.id}"). The call likely timed out or was rejected when its connection closed before the response arrived.`);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
241
287
|
if (!response.success) {
|
|
242
288
|
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
|
243
289
|
pending.reject(new Error(response.error));
|
|
@@ -262,25 +308,42 @@ function useAgent(options) {
|
|
|
262
308
|
options.onMessage?.(message);
|
|
263
309
|
},
|
|
264
310
|
onClose: (event) => {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
pending.stream?.onError?.("Connection closed");
|
|
311
|
+
const closedSocket = event.target ?? socketRef.current;
|
|
312
|
+
const isCurrentSocket = closedSocket === socketRef.current;
|
|
313
|
+
if (closedSocket) rejectCallsSentOn(closedSocket, "Connection closed");
|
|
314
|
+
if (isCurrentSocket) {
|
|
315
|
+
resetReady();
|
|
316
|
+
if (mutableAgentRef.current) mutableAgentRef.current.identified = false;
|
|
317
|
+
setIdentity((prev) => ({
|
|
318
|
+
...prev,
|
|
319
|
+
identified: false
|
|
320
|
+
}));
|
|
321
|
+
if (isAsyncQuery) setAwaitingQueryRefresh(true);
|
|
322
|
+
deleteCacheEntry(cacheKeyRef.current);
|
|
323
|
+
setCacheInvalidatedAt(Date.now());
|
|
279
324
|
}
|
|
280
|
-
pendingCallsRef.current.clear();
|
|
281
325
|
options.onClose?.(event);
|
|
282
326
|
}
|
|
283
327
|
});
|
|
328
|
+
socketRef.current = agent;
|
|
329
|
+
const prevSocketRef = useRef(null);
|
|
330
|
+
const prevAddressKeyRef = useRef(addressKey);
|
|
331
|
+
useEffect(() => {
|
|
332
|
+
const prev = prevSocketRef.current;
|
|
333
|
+
prevSocketRef.current = agent;
|
|
334
|
+
const prevAddress = prevAddressKeyRef.current;
|
|
335
|
+
prevAddressKeyRef.current = addressKey;
|
|
336
|
+
if (prevAddress !== addressKey) rejectQueuedCalls("Call discarded: the agent address changed before the request could be sent");
|
|
337
|
+
if (prev && prev !== agent) {
|
|
338
|
+
rejectCallsSentOn(prev, "Connection closed");
|
|
339
|
+
resetReady();
|
|
340
|
+
if (mutableAgentRef.current) mutableAgentRef.current.identified = false;
|
|
341
|
+
setIdentity((current) => current.identified ? {
|
|
342
|
+
...current,
|
|
343
|
+
identified: false
|
|
344
|
+
} : current);
|
|
345
|
+
}
|
|
346
|
+
}, [agent, addressKey]);
|
|
284
347
|
const call = useCallback((method, args = [], options) => {
|
|
285
348
|
return new Promise((resolve, reject) => {
|
|
286
349
|
const id = crypto.randomUUID();
|
|
@@ -288,30 +351,38 @@ function useAgent(options) {
|
|
|
288
351
|
const isLegacyFormat = options && ("onChunk" in options || "onDone" in options || "onError" in options);
|
|
289
352
|
const streamOptions = isLegacyFormat ? options : options?.stream;
|
|
290
353
|
const timeout = isLegacyFormat ? void 0 : options?.timeout;
|
|
291
|
-
|
|
354
|
+
const effectiveTimeout = timeout !== void 0 ? timeout : streamOptions ? void 0 : defaultCallTimeoutRef.current;
|
|
355
|
+
if (effectiveTimeout) timeoutId = setTimeout(() => {
|
|
292
356
|
const pending = pendingCallsRef.current.get(id);
|
|
293
357
|
pendingCallsRef.current.delete(id);
|
|
294
|
-
const errorMessage = `RPC call to ${method} timed out after ${
|
|
358
|
+
const errorMessage = `RPC call to ${method} timed out after ${effectiveTimeout}ms`;
|
|
295
359
|
pending?.stream?.onError?.(errorMessage);
|
|
296
360
|
reject(new Error(errorMessage));
|
|
297
|
-
},
|
|
361
|
+
}, effectiveTimeout);
|
|
362
|
+
const request = JSON.stringify({
|
|
363
|
+
args,
|
|
364
|
+
id,
|
|
365
|
+
method,
|
|
366
|
+
type: "rpc"
|
|
367
|
+
});
|
|
298
368
|
pendingCallsRef.current.set(id, {
|
|
299
369
|
reject,
|
|
300
370
|
resolve,
|
|
301
371
|
stream: streamOptions,
|
|
302
|
-
timeoutId
|
|
372
|
+
timeoutId,
|
|
373
|
+
request,
|
|
374
|
+
sentOn: null
|
|
303
375
|
});
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
agent.send(JSON.stringify(request));
|
|
376
|
+
const socket = socketRef.current;
|
|
377
|
+
if (socket && socket.readyState === socket.OPEN) {
|
|
378
|
+
socket.send(request);
|
|
379
|
+
const pending = pendingCallsRef.current.get(id);
|
|
380
|
+
if (pending) pending.sentOn = socket;
|
|
381
|
+
}
|
|
311
382
|
});
|
|
312
|
-
}, [
|
|
383
|
+
}, []);
|
|
313
384
|
agent.setState = (newState) => {
|
|
314
|
-
agent.send(JSON.stringify({
|
|
385
|
+
(socketRef.current ?? agent).send(JSON.stringify({
|
|
315
386
|
state: newState,
|
|
316
387
|
type: "cf_agent_state"
|
|
317
388
|
}));
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","names":[],"sources":["../src/react.tsx"],"sourcesContent":["import type { PartySocket } from \"partysocket\";\nimport { usePartySocket } from \"partysocket/react\";\nimport { useCallback, useRef, use, useMemo, useState, useEffect } from \"react\";\nimport type { MCPServersState, RPCRequest, RPCResponse } from \"./\";\nimport type {\n AgentPromiseReturnType,\n AgentStub,\n CallOptions,\n OptionalAgentMethods,\n RequiredAgentMethods,\n StreamOptions,\n UntypedAgentStub\n} from \"./client\";\nimport type { ClientParameters } from \"./serializable\";\nimport { createStubProxy } from \"./client\";\nimport { camelCaseToKebabCase } from \"./utils\";\nimport { MessageType } from \"./types\";\nimport {\n applyAgentToolEvent,\n createAgentToolEventState,\n type AgentToolEventMessage,\n type AgentToolEventState,\n type AgentToolRunState\n} from \"./chat/agent-tools\";\n\ntype QueryObject = Record<string, string | null>;\n\ninterface CacheEntry {\n promise: Promise<QueryObject>;\n expiresAt: number;\n}\n\nconst queryCache = new Map<string, CacheEntry>();\n\nfunction createCacheKey(\n agentNamespace: string,\n name: string | undefined,\n subChainOrDeps: ReadonlyArray<{ agent: string; name: string }> | unknown[],\n deps?: unknown[]\n): string {\n // Backwards-compatible overload: if called with 3 args, the third\n // argument is `deps` and `subChain` defaults to empty. With 4 args,\n // the third is the sub-chain. This keeps existing callers (and\n // the `_testUtils` surface) working while letting new callers\n // include the nested chain in the cache key.\n //\n // Empty sub-chain must produce the same key as the old 3-arg\n // form, so nested-addressing code can opt-in without invalidating\n // existing caches.\n if (deps === undefined) {\n return JSON.stringify([\n agentNamespace,\n name || \"default\",\n ...(subChainOrDeps as unknown[])\n ]);\n }\n const subChain = subChainOrDeps as ReadonlyArray<{\n agent: string;\n name: string;\n }>;\n if (subChain.length === 0) {\n return JSON.stringify([agentNamespace, name || \"default\", ...deps]);\n }\n return JSON.stringify([\n agentNamespace,\n name || \"default\",\n subChain.map((s) => [s.agent, s.name]),\n ...deps\n ]);\n}\n\n/** Build a URL path tail `/sub/{agent-kebab}/{name}/...` from a sub chain. */\nfunction buildSubPath(\n subChain: ReadonlyArray<{ agent: string; name: string }>,\n extraPath?: string\n): string {\n if (subChain.length === 0) return extraPath ?? \"\";\n const parts = subChain.flatMap((step) => [\n \"sub\",\n camelCaseToKebabCase(step.agent),\n encodeURIComponent(step.name)\n ]);\n const combined = parts.join(\"/\");\n if (extraPath) {\n const trimmed = extraPath.startsWith(\"/\") ? extraPath.slice(1) : extraPath;\n return `${combined}/${trimmed}`;\n }\n return combined;\n}\n\nfunction getCacheEntry(key: string): CacheEntry | undefined {\n const entry = queryCache.get(key);\n if (!entry) return undefined;\n\n if (Date.now() >= entry.expiresAt) {\n queryCache.delete(key);\n return undefined;\n }\n\n return entry;\n}\n\nfunction setCacheEntry(\n key: string,\n promise: Promise<QueryObject>,\n cacheTtl: number\n): CacheEntry {\n const entry: CacheEntry = {\n promise,\n expiresAt: Date.now() + cacheTtl\n };\n queryCache.set(key, entry);\n return entry;\n}\n\nfunction deleteCacheEntry(key: string): void {\n queryCache.delete(key);\n}\n\n// Export for testing purposes\nexport const _testUtils = {\n queryCache,\n setCacheEntry,\n getCacheEntry,\n deleteCacheEntry,\n clearCache: () => queryCache.clear(),\n createStubProxy,\n createCacheKey\n};\n\n/**\n * Options for the useAgent hook\n * @template State Type of the Agent's state\n */\nexport type UseAgentOptions<State = unknown> = Omit<\n Parameters<typeof usePartySocket>[0],\n \"party\" | \"room\" | \"query\"\n> & {\n /** Name of the agent to connect to (ignored if basePath is set) */\n agent: string;\n /** Name of the specific Agent instance (ignored if basePath is set) */\n name?: string;\n /**\n * Full URL path - bypasses agent/name URL construction.\n * When set, the client connects to this path directly.\n * Server must handle routing manually (e.g., with getAgentByName + fetch).\n * @example\n * // Client connects to /user, server routes based on session\n * useAgent({ agent: \"UserAgent\", basePath: \"user\" })\n */\n basePath?: string;\n /** Query parameters - can be static object or async function */\n query?: QueryObject | (() => Promise<QueryObject>);\n /** Dependencies for async query caching */\n queryDeps?: unknown[];\n /** Cache TTL in milliseconds for auth tokens/time-sensitive data */\n cacheTtl?: number;\n /** Called when the Agent's state is updated */\n onStateUpdate?: (state: State, source: \"server\" | \"client\") => void;\n /** Called when a state update fails (e.g., connection is readonly) */\n onStateUpdateError?: (error: string) => void;\n /** Called when MCP server state is updated */\n onMcpUpdate?: (mcpServers: MCPServersState) => void;\n /**\n * Called when the server sends the agent's identity on connect.\n * Useful when using basePath, as the actual instance name is determined server-side.\n * @param name The actual agent instance name\n * @param agent The agent class name (kebab-case)\n */\n onIdentity?: (name: string, agent: string) => void;\n /**\n * Called when identity changes on reconnect (different instance than before).\n * If not provided and identity changes, a warning will be logged.\n * @param oldName Previous instance name\n * @param newName New instance name\n * @param oldAgent Previous agent class name\n * @param newAgent New agent class name\n */\n onIdentityChange?: (\n oldName: string,\n newName: string,\n oldAgent: string,\n newAgent: string\n ) => void;\n /**\n * Additional path to append to the URL.\n * Works with both standard routing and basePath.\n * @example\n * // With basePath: /user/settings\n * { basePath: \"user\", path: \"settings\" }\n * // Standard: /agents/my-agent/room/settings\n * { agent: \"MyAgent\", name: \"room\", path: \"settings\" }\n */\n path?: string;\n /**\n * Connect to a sub-agent (facet) via its parent. Flat array,\n * root-first. Each step addresses one parent↔child hop.\n *\n * The hook's returned `.agent` / `.name` report the **leaf**\n * identity (the deepest entry in `sub`), so downstream hooks\n * like `useAgentChat` see the child they actually talk to.\n * `.path` exposes the full chain for observability, deep links,\n * and reconnect keying.\n *\n * @example\n * ```ts\n * // Two-level nesting: Inbox (Alice) → Chat (abc)\n * useAgent({\n * agent: \"inbox\", name: userId,\n * sub: [{ agent: \"chat\", name: chatId }]\n * });\n *\n * // Three-level: tenant → inbox → chat\n * useAgent({\n * agent: \"tenant\", name: tenantId,\n * sub: [\n * { agent: \"inbox\", name: userId },\n * { agent: \"chat\", name: chatId }\n * ]\n * });\n * ```\n *\n * @experimental The API surface may change before stabilizing.\n */\n sub?: ReadonlyArray<{ agent: string; name: string }>;\n};\n\ntype OptionalArgsAgentMethodCall<AgentT> = <\n K extends keyof OptionalAgentMethods<AgentT>\n>(\n method: K,\n args?: ClientParameters<OptionalAgentMethods<AgentT>[K]>,\n options?: CallOptions | StreamOptions\n) => AgentPromiseReturnType<AgentT, K>;\n\ntype RequiredArgsAgentMethodCall<AgentT> = <\n K extends keyof RequiredAgentMethods<AgentT>\n>(\n method: K,\n args: ClientParameters<RequiredAgentMethods<AgentT>[K]>,\n options?: CallOptions | StreamOptions\n) => AgentPromiseReturnType<AgentT, K>;\n\ntype AgentMethodCall<AgentT> = OptionalArgsAgentMethodCall<AgentT> &\n RequiredArgsAgentMethodCall<AgentT>;\n\ntype UntypedAgentMethodCall = <T = unknown>(\n method: string,\n args?: unknown[],\n options?: CallOptions | StreamOptions\n) => Promise<T>;\n\n/**\n * React hook for connecting to an Agent\n */\nexport function useAgent<State = unknown>(\n options: UseAgentOptions<State>\n): Omit<PartySocket, \"path\"> & {\n agent: string;\n name: string;\n /** Full root-first address chain, including leaf. Single entry when `sub` isn't set. */\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n};\nexport function useAgent<\n AgentT extends {\n get state(): State;\n },\n State\n>(\n options: UseAgentOptions<State>\n): Omit<PartySocket, \"path\"> & {\n agent: string;\n name: string;\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: AgentMethodCall<AgentT>;\n stub: AgentStub<AgentT>;\n getHttpUrl: () => string;\n};\nexport function useAgent<State>(options: UseAgentOptions<unknown>): Omit<\n PartySocket,\n \"path\"\n> & {\n agent: string;\n name: string;\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall | AgentMethodCall<unknown>;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n} {\n const agentNamespace = camelCaseToKebabCase(options.agent);\n // NOTE: `path` is destructured out (as `userPath`) so it does NOT\n // end up in `restOptions`. Spreading `restOptions` after the\n // computed `path: combinedPath` would otherwise let the user's raw\n // `path` overwrite the combined sub-agent URL, dropping every\n // `/sub/{child}/{name}` segment on the way to the socket.\n const {\n query,\n queryDeps,\n cacheTtl,\n sub: subOption,\n path: userPath,\n ...restOptions\n } = options;\n\n const subChain = useMemo(\n () => (subOption ?? []).map((s) => ({ agent: s.agent, name: s.name })),\n // Stable serialization — deep changes re-memoize.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(subOption ?? [])]\n );\n\n // The \"leaf\" is the deepest entry in the chain; it's what\n // downstream code (useAgentChat etc.) should see as the\n // authoritative identity.\n const leafAgent =\n subChain.length > 0 ? subChain[subChain.length - 1].agent : options.agent;\n const leafName =\n subChain.length > 0\n ? subChain[subChain.length - 1].name\n : options.name || \"default\";\n\n // Full root-first chain, including the leaf. Exposed as `.path`\n // and used for cache keying so nested sessions with the same leaf\n // name don't collide.\n const fullPath = useMemo(\n () => [\n { agent: options.agent, name: options.name || \"default\" },\n ...subChain\n ],\n [options.agent, options.name, subChain]\n );\n\n // Keep track of pending RPC calls\n const pendingCallsRef = useRef(\n new Map<\n string,\n {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n stream?: StreamOptions;\n timeoutId?: ReturnType<typeof setTimeout>;\n }\n >()\n );\n\n const cacheKey = useMemo(\n () =>\n createCacheKey(agentNamespace, options.name, subChain, queryDeps || []),\n [agentNamespace, options.name, subChain, queryDeps]\n );\n\n // Track current cache key in a ref for use in onClose handler.\n // This ensures we invalidate the correct cache entry when the connection closes,\n // even if the component re-renders with different props before onClose fires.\n // We update synchronously during render (not in useEffect) to avoid race\n // conditions where onClose could fire before the effect runs.\n const cacheKeyRef = useRef(cacheKey);\n cacheKeyRef.current = cacheKey;\n\n const ttl = cacheTtl ?? 5 * 60 * 1000;\n\n // Track cache invalidation to force re-render when TTL expires\n const [cacheInvalidatedAt, setCacheInvalidatedAt] = useState<number>(0);\n\n // Disable socket while waiting for async query to refresh after disconnect\n const isAsyncQuery = query && typeof query === \"function\";\n const [awaitingQueryRefresh, setAwaitingQueryRefresh] = useState(false);\n\n // Get or create the query promise\n const queryPromise = useMemo(() => {\n // Re-run when cache is invalidated after TTL expiry\n void cacheInvalidatedAt;\n\n if (!query || typeof query !== \"function\") {\n return null;\n }\n\n // Always check cache first to deduplicate concurrent requests\n const cached = getCacheEntry(cacheKey);\n if (cached) {\n return cached.promise;\n }\n\n // Create new promise\n const promise = query().catch((error) => {\n console.error(\n `[useAgent] Query failed for agent \"${options.agent}\":`,\n error\n );\n deleteCacheEntry(cacheKey);\n throw error;\n });\n\n // Always cache to deduplicate concurrent requests\n setCacheEntry(cacheKey, promise, ttl);\n\n return promise;\n }, [cacheKey, query, options.agent, ttl, cacheInvalidatedAt]);\n\n // Schedule cache invalidation when TTL expires\n useEffect(() => {\n if (!queryPromise || ttl <= 0) return;\n\n const entry = getCacheEntry(cacheKey);\n if (!entry) return;\n\n const timeUntilExpiry = entry.expiresAt - Date.now();\n\n // Always set a timer (with min 0ms) to ensure cleanup function is returned\n const timer = setTimeout(\n () => {\n deleteCacheEntry(cacheKey);\n setCacheInvalidatedAt(Date.now());\n },\n Math.max(0, timeUntilExpiry)\n );\n\n return () => clearTimeout(timer);\n }, [cacheKey, queryPromise, ttl]);\n\n let resolvedQuery: QueryObject | undefined;\n\n if (query) {\n if (typeof query === \"function\") {\n // Use React's use() to resolve the promise\n const queryResult = use(queryPromise!);\n\n // Check for non-primitive values and warn\n if (queryResult) {\n for (const [key, value] of Object.entries(queryResult)) {\n if (\n value !== null &&\n value !== undefined &&\n typeof value !== \"string\" &&\n typeof value !== \"number\" &&\n typeof value !== \"boolean\"\n ) {\n console.warn(\n `[useAgent] Query parameter \"${key}\" is an object and will be converted to \"[object Object]\". ` +\n \"Query parameters should be string, number, boolean, or null.\"\n );\n }\n }\n resolvedQuery = queryResult;\n }\n } else {\n // Sync query - use directly\n resolvedQuery = query;\n }\n }\n\n // Re-enable socket after async query resolves\n useEffect(() => {\n if (awaitingQueryRefresh && resolvedQuery !== undefined) {\n setAwaitingQueryRefresh(false);\n }\n }, [awaitingQueryRefresh, resolvedQuery]);\n\n // Track agent state for reactivity — updated on server broadcasts and client setState\n const [agentState, setAgentState] = useState<State | undefined>(undefined);\n\n // Store identity in React state for reactivity. Seed with the\n // leaf's address — what the server will echo back in\n // `cf_agent_identity`.\n const [identity, setIdentity] = useState({\n name: leafName,\n agent: camelCaseToKebabCase(leafAgent),\n identified: false\n });\n\n // Track previous identity for change detection\n const previousIdentityRef = useRef<{\n name: string | null;\n agent: string | null;\n }>({ name: null, agent: null });\n\n // Ready promise - resolves when identity is received, resets on close\n const readyRef = useRef<\n { promise: Promise<void>; resolve: () => void } | undefined\n >(undefined);\n\n const resetReady = () => {\n let resolve: () => void;\n const promise = new Promise<void>((r) => {\n resolve = r;\n });\n readyRef.current = { promise, resolve: resolve! };\n };\n\n if (!readyRef.current) {\n resetReady();\n }\n\n const mutableAgentRef = useRef<{\n agent: string;\n name: string;\n identified: boolean;\n } | null>(null);\n\n // Combine the sub-agent chain with the user-provided `path`.\n // Order matters: `/sub/{child}/{name}/...` comes before `path` so\n // the server sees the hierarchy it expects.\n const combinedPath = useMemo(\n () => buildSubPath(subChain, userPath),\n [subChain, userPath]\n );\n\n // If basePath is provided, use it directly; otherwise construct from agent/name\n const socketOptions = options.basePath\n ? {\n basePath: options.basePath,\n path: combinedPath || undefined,\n query: resolvedQuery,\n ...restOptions\n }\n : {\n party: agentNamespace,\n prefix: \"agents\",\n room: options.name || \"default\",\n path: combinedPath || undefined,\n query: resolvedQuery,\n ...restOptions\n };\n\n const socketEnabled = !awaitingQueryRefresh && (restOptions.enabled ?? true);\n\n const agent = usePartySocket({\n ...socketOptions,\n enabled: socketEnabled,\n onMessage: (message) => {\n if (typeof message.data === \"string\") {\n let parsedMessage: Record<string, unknown>;\n try {\n parsedMessage = JSON.parse(message.data);\n } catch (_error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return options.onMessage?.(message);\n }\n if (parsedMessage.type === MessageType.CF_AGENT_IDENTITY) {\n const oldName = previousIdentityRef.current.name;\n const oldAgent = previousIdentityRef.current.agent;\n const newName = parsedMessage.name as string;\n const newAgent = parsedMessage.agent as string;\n\n const currentAgent = mutableAgentRef.current;\n if (currentAgent) {\n currentAgent.name = newName;\n currentAgent.agent = newAgent;\n currentAgent.identified = true;\n }\n\n // Update reactive state (triggers re-render)\n setIdentity({ name: newName, agent: newAgent, identified: true });\n\n // Resolve ready promise\n readyRef.current?.resolve();\n\n // Detect identity change on reconnect\n if (\n oldName !== null &&\n oldAgent !== null &&\n (oldName !== newName || oldAgent !== newAgent)\n ) {\n if (options.onIdentityChange) {\n options.onIdentityChange(oldName, newName, oldAgent, newAgent);\n } else {\n const agentChanged = oldAgent !== newAgent;\n const nameChanged = oldName !== newName;\n let changeDescription = \"\";\n if (agentChanged && nameChanged) {\n changeDescription = `agent \"${oldAgent}\" → \"${newAgent}\", instance \"${oldName}\" → \"${newName}\"`;\n } else if (agentChanged) {\n changeDescription = `agent \"${oldAgent}\" → \"${newAgent}\"`;\n } else {\n changeDescription = `instance \"${oldName}\" → \"${newName}\"`;\n }\n console.warn(\n `[agents] Identity changed on reconnect: ${changeDescription}. ` +\n \"This can happen with server-side routing (e.g., basePath with getAgentByName) \" +\n \"where the instance is determined by auth/session. \" +\n \"Provide onIdentityChange callback to handle this explicitly, \" +\n \"or ignore if this is expected for your routing pattern.\"\n );\n }\n }\n\n // Track for next change detection\n previousIdentityRef.current = { name: newName, agent: newAgent };\n\n // Call onIdentity callback\n options.onIdentity?.(newName, newAgent);\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_STATE) {\n setAgentState(parsedMessage.state as State);\n options.onStateUpdate?.(parsedMessage.state as State, \"server\");\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_STATE_ERROR) {\n options.onStateUpdateError?.(parsedMessage.error as string);\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_MCP_SERVERS) {\n options.onMcpUpdate?.(parsedMessage.mcp as MCPServersState);\n return;\n }\n if (parsedMessage.type === MessageType.RPC) {\n const response = parsedMessage as RPCResponse;\n const pending = pendingCallsRef.current.get(response.id);\n if (!pending) return;\n\n if (!response.success) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.reject(new Error(response.error));\n pendingCallsRef.current.delete(response.id);\n pending.stream?.onError?.(response.error);\n return;\n }\n\n // Handle streaming responses\n if (\"done\" in response) {\n if (response.done) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.resolve(response.result);\n pendingCallsRef.current.delete(response.id);\n pending.stream?.onDone?.(response.result);\n } else {\n pending.stream?.onChunk?.(response.result);\n }\n } else {\n // Non-streaming response\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.resolve(response.result);\n pendingCallsRef.current.delete(response.id);\n }\n return;\n }\n }\n options.onMessage?.(message);\n },\n onClose: (event: CloseEvent) => {\n // Reset ready state for next connection\n resetReady();\n if (mutableAgentRef.current) {\n mutableAgentRef.current.identified = false;\n }\n setIdentity((prev) => ({ ...prev, identified: false }));\n\n // Pause reconnection for async queries until fresh query params are ready\n if (isAsyncQuery) {\n setAwaitingQueryRefresh(true);\n }\n\n // Invalidate cache and trigger re-render to fetch fresh query params\n deleteCacheEntry(cacheKeyRef.current);\n setCacheInvalidatedAt(Date.now());\n\n // Reject all pending calls (consistent with AgentClient behavior)\n const error = new Error(\"Connection closed\");\n for (const pending of pendingCallsRef.current.values()) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.reject(error);\n pending.stream?.onError?.(\"Connection closed\");\n }\n pendingCallsRef.current.clear();\n\n // Call user's onClose if provided\n options.onClose?.(event);\n }\n }) as PartySocket & {\n agent: string;\n name: string;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n };\n // Create the call method\n const call = useCallback(\n <T = unknown,>(\n method: string,\n args: unknown[] = [],\n options?: CallOptions | StreamOptions\n ): Promise<T> => {\n return new Promise((resolve, reject) => {\n const id = crypto.randomUUID();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n // Detect legacy format: { onChunk?, onDone?, onError? } vs new format: { timeout?, stream? }\n const isLegacyFormat =\n options &&\n (\"onChunk\" in options || \"onDone\" in options || \"onError\" in options);\n const streamOptions = isLegacyFormat\n ? (options as StreamOptions)\n : (options as CallOptions | undefined)?.stream;\n const timeout = isLegacyFormat\n ? undefined\n : (options as CallOptions | undefined)?.timeout;\n\n if (timeout) {\n timeoutId = setTimeout(() => {\n const pending = pendingCallsRef.current.get(id);\n pendingCallsRef.current.delete(id);\n const errorMessage = `RPC call to ${method} timed out after ${timeout}ms`;\n pending?.stream?.onError?.(errorMessage);\n reject(new Error(errorMessage));\n }, timeout);\n }\n\n pendingCallsRef.current.set(id, {\n reject,\n resolve: resolve as (value: unknown) => void,\n stream: streamOptions,\n timeoutId\n });\n\n const request: RPCRequest = {\n args,\n id,\n method,\n type: MessageType.RPC\n };\n\n agent.send(JSON.stringify(request));\n });\n },\n [agent]\n );\n\n agent.setState = (newState: State) => {\n agent.send(\n JSON.stringify({ state: newState, type: MessageType.CF_AGENT_STATE })\n );\n setAgentState(newState);\n options.onStateUpdate?.(newState, \"client\");\n };\n\n agent.call = call;\n // Use reactive identity state (updates on identity message)\n agent.agent = identity.agent;\n agent.name = identity.name;\n // Full root-first chain including the leaf. Computed from the\n // user-provided options — the server doesn't need to echo it\n // back because the client already knows. Write past the\n // PartySocket `.path: string` shape via an unknown cast — the\n // overload signatures expose this as `ReadonlyArray<...>`.\n (\n agent as unknown as { path: ReadonlyArray<{ agent: string; name: string }> }\n ).path = fullPath;\n agent.identified = identity.identified;\n agent.ready = readyRef.current!.promise;\n agent.state = agentState;\n mutableAgentRef.current = agent;\n // Memoize stub so it's referentially stable across renders\n // (call is already stable via useCallback)\n const stub = useMemo(() => createStubProxy(call), [call]);\n agent.stub = stub;\n agent.getHttpUrl = () => {\n // TODO: upstream to partysocket — expose an HTTP URL property\n // @ts-expect-error accessing protected PartySocket internals\n const wsUrl: string = (agent._url as string | null) || agent._pkurl || \"\";\n return wsUrl.replace(\"ws://\", \"http://\").replace(\"wss://\", \"https://\");\n };\n\n // warn if agent isn't in lowercase\n if (identity.agent !== identity.agent.toLowerCase()) {\n console.warn(\n \"Agent name: \" +\n identity.agent +\n \" should probably be in lowercase. Received: \" +\n identity.agent\n );\n }\n\n // The overload signatures return `Omit<PartySocket, \"path\"> & { path: ... }`,\n // but `agent` is inferred as the raw PartySocket. Cast to satisfy\n // the overload contract — the runtime override of `agent.path`\n // above ensures the shape matches.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return agent as any;\n}\n\ntype AgentToolEventAgent = Pick<\n PartySocket,\n \"addEventListener\" | \"removeEventListener\"\n>;\n\nfunction agentToolDedupeKey(message: AgentToolEventMessage): string {\n return [\n message.parentToolCallId ?? \"\",\n message.event.runId,\n String(message.sequence)\n ].join(\"\\0\");\n}\n\nexport function useAgentToolEvents(options: { agent: AgentToolEventAgent }): {\n runsById: Record<string, AgentToolRunState>;\n runsByToolCallId: Record<string, AgentToolRunState[]>;\n unboundRuns: AgentToolRunState[];\n getRunsForToolCall(toolCallId: string): AgentToolRunState[];\n resetLocalState(): void;\n} {\n const { agent } = options;\n const [state, setState] = useState<AgentToolEventState>(() =>\n createAgentToolEventState()\n );\n const seenRef = useRef(new Set<string>());\n\n useEffect(() => {\n const onMessage = (event: MessageEvent) => {\n if (typeof event.data !== \"string\") return;\n let message: AgentToolEventMessage;\n try {\n message = JSON.parse(event.data) as AgentToolEventMessage;\n } catch {\n return;\n }\n if (message.type !== \"agent-tool-event\") return;\n const key = agentToolDedupeKey(message);\n if (seenRef.current.has(key)) return;\n seenRef.current.add(key);\n setState((prev) => applyAgentToolEvent(prev, message));\n };\n\n agent.addEventListener(\"message\", onMessage);\n return () => agent.removeEventListener(\"message\", onMessage);\n }, [agent]);\n\n const resetLocalState = useCallback(() => {\n seenRef.current.clear();\n setState(createAgentToolEventState());\n }, []);\n\n const getRunsForToolCall = useCallback(\n (toolCallId: string) => state.runsByToolCallId[toolCallId] ?? [],\n [state.runsByToolCallId]\n );\n\n return {\n ...state,\n getRunsForToolCall,\n resetLocalState\n };\n}\n"],"mappings":";;;;;;;AAgCA,MAAM,6BAAa,IAAI,IAAwB;AAE/C,SAAS,eACP,gBACA,MACA,gBACA,MACQ;CAUR,IAAI,SAAS,KAAA,GACX,OAAO,KAAK,UAAU;EACpB;EACA,QAAQ;EACR,GAAI;CACN,CAAC;CAEH,MAAM,WAAW;CAIjB,IAAI,SAAS,WAAW,GACtB,OAAO,KAAK,UAAU;EAAC;EAAgB,QAAQ;EAAW,GAAG;CAAI,CAAC;CAEpE,OAAO,KAAK,U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SAAS,0BAA0B,CAAC;CACtC,GAAG,CAAC,CAAC;CAEL,MAAM,qBAAqB,aACxB,eAAuB,MAAM,iBAAiB,eAAe,CAAC,GAC/D,CAAC,MAAM,gBAAgB,CACzB;CAEA,OAAO;EACL,GAAG;EACH;EACA;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"react.js","names":[],"sources":["../src/react.tsx"],"sourcesContent":["import type { PartySocket } from \"partysocket\";\nimport { usePartySocket } from \"partysocket/react\";\nimport { useCallback, useRef, use, useMemo, useState, useEffect } from \"react\";\nimport type { MCPServersState, RPCRequest, RPCResponse } from \"./\";\nimport type {\n AgentPromiseReturnType,\n AgentStub,\n CallOptions,\n OptionalAgentMethods,\n RequiredAgentMethods,\n StreamOptions,\n UntypedAgentStub\n} from \"./client\";\nimport type { ClientParameters } from \"./serializable\";\nimport { createStubProxy, DEFAULT_CALL_TIMEOUT_MS } from \"./client\";\nimport { camelCaseToKebabCase } from \"./utils\";\nimport { MessageType } from \"./types\";\nimport {\n applyAgentToolEvent,\n createAgentToolEventState,\n type AgentToolEventMessage,\n type AgentToolEventState,\n type AgentToolRunState\n} from \"./chat/agent-tools\";\n\ntype QueryObject = Record<string, string | null>;\n\ninterface CacheEntry {\n promise: Promise<QueryObject>;\n expiresAt: number;\n}\n\nconst queryCache = new Map<string, CacheEntry>();\n\nfunction createCacheKey(\n agentNamespace: string,\n name: string | undefined,\n subChainOrDeps: ReadonlyArray<{ agent: string; name: string }> | unknown[],\n deps?: unknown[]\n): string {\n // Backwards-compatible overload: if called with 3 args, the third\n // argument is `deps` and `subChain` defaults to empty. With 4 args,\n // the third is the sub-chain. This keeps existing callers (and\n // the `_testUtils` surface) working while letting new callers\n // include the nested chain in the cache key.\n //\n // Empty sub-chain must produce the same key as the old 3-arg\n // form, so nested-addressing code can opt-in without invalidating\n // existing caches.\n if (deps === undefined) {\n return JSON.stringify([\n agentNamespace,\n name || \"default\",\n ...(subChainOrDeps as unknown[])\n ]);\n }\n const subChain = subChainOrDeps as ReadonlyArray<{\n agent: string;\n name: string;\n }>;\n if (subChain.length === 0) {\n return JSON.stringify([agentNamespace, name || \"default\", ...deps]);\n }\n return JSON.stringify([\n agentNamespace,\n name || \"default\",\n subChain.map((s) => [s.agent, s.name]),\n ...deps\n ]);\n}\n\n/** Build a URL path tail `/sub/{agent-kebab}/{name}/...` from a sub chain. */\nfunction buildSubPath(\n subChain: ReadonlyArray<{ agent: string; name: string }>,\n extraPath?: string\n): string {\n if (subChain.length === 0) return extraPath ?? \"\";\n const parts = subChain.flatMap((step) => [\n \"sub\",\n camelCaseToKebabCase(step.agent),\n encodeURIComponent(step.name)\n ]);\n const combined = parts.join(\"/\");\n if (extraPath) {\n const trimmed = extraPath.startsWith(\"/\") ? extraPath.slice(1) : extraPath;\n return `${combined}/${trimmed}`;\n }\n return combined;\n}\n\nfunction getCacheEntry(key: string): CacheEntry | undefined {\n const entry = queryCache.get(key);\n if (!entry) return undefined;\n\n if (Date.now() >= entry.expiresAt) {\n queryCache.delete(key);\n return undefined;\n }\n\n return entry;\n}\n\nfunction setCacheEntry(\n key: string,\n promise: Promise<QueryObject>,\n cacheTtl: number\n): CacheEntry {\n const entry: CacheEntry = {\n promise,\n expiresAt: Date.now() + cacheTtl\n };\n queryCache.set(key, entry);\n return entry;\n}\n\nfunction deleteCacheEntry(key: string): void {\n queryCache.delete(key);\n}\n\n// Export for testing purposes\nexport const _testUtils = {\n queryCache,\n setCacheEntry,\n getCacheEntry,\n deleteCacheEntry,\n clearCache: () => queryCache.clear(),\n createStubProxy,\n createCacheKey\n};\n\n/**\n * Options for the useAgent hook\n * @template State Type of the Agent's state\n */\nexport type UseAgentOptions<State = unknown> = Omit<\n Parameters<typeof usePartySocket>[0],\n \"party\" | \"room\" | \"query\"\n> & {\n /** Name of the agent to connect to (ignored if basePath is set) */\n agent: string;\n /** Name of the specific Agent instance (ignored if basePath is set) */\n name?: string;\n /**\n * Full URL path - bypasses agent/name URL construction.\n * When set, the client connects to this path directly.\n * Server must handle routing manually (e.g., with getAgentByName + fetch).\n * @example\n * // Client connects to /user, server routes based on session\n * useAgent({ agent: \"UserAgent\", basePath: \"user\" })\n */\n basePath?: string;\n /** Query parameters - can be static object or async function */\n query?: QueryObject | (() => Promise<QueryObject>);\n /** Dependencies for async query caching */\n queryDeps?: unknown[];\n /** Cache TTL in milliseconds for auth tokens/time-sensitive data */\n cacheTtl?: number;\n /** Called when the Agent's state is updated */\n onStateUpdate?: (state: State, source: \"server\" | \"client\") => void;\n /** Called when a state update fails (e.g., connection is readonly) */\n onStateUpdateError?: (error: string) => void;\n /** Called when MCP server state is updated */\n onMcpUpdate?: (mcpServers: MCPServersState) => void;\n /**\n * Called when the server sends the agent's identity on connect.\n * Useful when using basePath, as the actual instance name is determined server-side.\n * @param name The actual agent instance name\n * @param agent The agent class name (kebab-case)\n */\n onIdentity?: (name: string, agent: string) => void;\n /**\n * Called when identity changes on reconnect (different instance than before).\n * If not provided and identity changes, a warning will be logged.\n * @param oldName Previous instance name\n * @param newName New instance name\n * @param oldAgent Previous agent class name\n * @param newAgent New agent class name\n */\n onIdentityChange?: (\n oldName: string,\n newName: string,\n oldAgent: string,\n newAgent: string\n ) => void;\n /**\n * Additional path to append to the URL.\n * Works with both standard routing and basePath.\n * @example\n * // With basePath: /user/settings\n * { basePath: \"user\", path: \"settings\" }\n * // Standard: /agents/my-agent/room/settings\n * { agent: \"MyAgent\", name: \"room\", path: \"settings\" }\n */\n path?: string;\n /**\n * Connect to a sub-agent (facet) via its parent. Flat array,\n * root-first. Each step addresses one parent↔child hop.\n *\n * The hook's returned `.agent` / `.name` report the **leaf**\n * identity (the deepest entry in `sub`), so downstream hooks\n * like `useAgentChat` see the child they actually talk to.\n * `.path` exposes the full chain for observability, deep links,\n * and reconnect keying.\n *\n * @example\n * ```ts\n * // Two-level nesting: Inbox (Alice) → Chat (abc)\n * useAgent({\n * agent: \"inbox\", name: userId,\n * sub: [{ agent: \"chat\", name: chatId }]\n * });\n *\n * // Three-level: tenant → inbox → chat\n * useAgent({\n * agent: \"tenant\", name: tenantId,\n * sub: [\n * { agent: \"inbox\", name: userId },\n * { agent: \"chat\", name: chatId }\n * ]\n * });\n * ```\n *\n * @experimental The API surface may change before stabilizing.\n */\n sub?: ReadonlyArray<{ agent: string; name: string }>;\n /**\n * Default timeout (in milliseconds) applied to non-streaming `call()`s\n * that don't pass an explicit `timeout`. Acts as a backstop so calls\n * whose response is lost (e.g. the connection is replaced mid-flight)\n * reject instead of hanging forever.\n *\n * Defaults to 30 000 ms. Set to `0` to disable the default timeout.\n * Streaming calls never get a default timeout (long-lived streams are\n * legitimate); pass an explicit `timeout` to bound them.\n */\n defaultCallTimeout?: number;\n};\n\ntype OptionalArgsAgentMethodCall<AgentT> = <\n K extends keyof OptionalAgentMethods<AgentT>\n>(\n method: K,\n args?: ClientParameters<OptionalAgentMethods<AgentT>[K]>,\n options?: CallOptions | StreamOptions\n) => AgentPromiseReturnType<AgentT, K>;\n\ntype RequiredArgsAgentMethodCall<AgentT> = <\n K extends keyof RequiredAgentMethods<AgentT>\n>(\n method: K,\n args: ClientParameters<RequiredAgentMethods<AgentT>[K]>,\n options?: CallOptions | StreamOptions\n) => AgentPromiseReturnType<AgentT, K>;\n\ntype AgentMethodCall<AgentT> = OptionalArgsAgentMethodCall<AgentT> &\n RequiredArgsAgentMethodCall<AgentT>;\n\ntype UntypedAgentMethodCall = <T = unknown>(\n method: string,\n args?: unknown[],\n options?: CallOptions | StreamOptions\n) => Promise<T>;\n\n/**\n * React hook for connecting to an Agent\n */\nexport function useAgent<State = unknown>(\n options: UseAgentOptions<State>\n): Omit<PartySocket, \"path\"> & {\n agent: string;\n name: string;\n /** Full root-first address chain, including leaf. Single entry when `sub` isn't set. */\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n};\nexport function useAgent<\n AgentT extends {\n get state(): State;\n },\n State\n>(\n options: UseAgentOptions<State>\n): Omit<PartySocket, \"path\"> & {\n agent: string;\n name: string;\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: AgentMethodCall<AgentT>;\n stub: AgentStub<AgentT>;\n getHttpUrl: () => string;\n};\nexport function useAgent<State>(options: UseAgentOptions<unknown>): Omit<\n PartySocket,\n \"path\"\n> & {\n agent: string;\n name: string;\n path: ReadonlyArray<{ agent: string; name: string }>;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall | AgentMethodCall<unknown>;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n} {\n const agentNamespace = camelCaseToKebabCase(options.agent);\n // NOTE: `path` is destructured out (as `userPath`) so it does NOT\n // end up in `restOptions`. Spreading `restOptions` after the\n // computed `path: combinedPath` would otherwise let the user's raw\n // `path` overwrite the combined sub-agent URL, dropping every\n // `/sub/{child}/{name}` segment on the way to the socket.\n const {\n query,\n queryDeps,\n cacheTtl,\n sub: subOption,\n path: userPath,\n defaultCallTimeout,\n ...restOptions\n } = options;\n\n const subChain = useMemo(\n () => (subOption ?? []).map((s) => ({ agent: s.agent, name: s.name })),\n // Stable serialization — deep changes re-memoize.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [JSON.stringify(subOption ?? [])]\n );\n\n // The \"leaf\" is the deepest entry in the chain; it's what\n // downstream code (useAgentChat etc.) should see as the\n // authoritative identity.\n const leafAgent =\n subChain.length > 0 ? subChain[subChain.length - 1].agent : options.agent;\n const leafName =\n subChain.length > 0\n ? subChain[subChain.length - 1].name\n : options.name || \"default\";\n\n // Full root-first chain, including the leaf. Exposed as `.path`\n // and used for cache keying so nested sessions with the same leaf\n // name don't collide.\n const fullPath = useMemo(\n () => [\n { agent: options.agent, name: options.name || \"default\" },\n ...subChain\n ],\n [options.agent, options.name, subChain]\n );\n\n // Keep track of pending RPC calls.\n //\n // Each entry is tagged with the socket the request was transmitted on\n // (`sentOn`). Requests are only handed to a socket once it's OPEN —\n // until then they stay queued here (`sentOn: null`). This matters\n // because `usePartySocket` *replaces* the socket object whenever\n // connection options change (async query refresh, path change, etc.):\n // anything buffered inside a replaced socket is lost forever, and a\n // call transmitted on a replaced socket can never receive its\n // response. Tagging lets us:\n // - flush still-queued requests on whichever socket connects next\n // (safe: they were never transmitted, so no double-execution risk)\n // - reject calls transmitted on a socket that closed or was replaced\n // (their response can never arrive)\n // - avoid rejecting calls in flight on the *new* socket when a stale\n // close event from an old socket trickles in\n const pendingCallsRef = useRef(\n new Map<\n string,\n {\n resolve: (value: unknown) => void;\n reject: (error: Error) => void;\n stream?: StreamOptions;\n timeoutId?: ReturnType<typeof setTimeout>;\n /** Serialized RPC request, kept so it can be (re)transmitted */\n request: string;\n /** Socket the request was transmitted on; null while queued */\n sentOn: PartySocket | null;\n }\n >()\n );\n\n // Always points at the socket from the latest render. `call`,\n // `setState`, and the queue-flushing logic go through this ref so\n // that stale `agent` references held by old effect closures still\n // route their traffic to the live socket instead of a dead one.\n const socketRef = useRef<PartySocket | null>(null);\n\n const defaultCallTimeoutRef = useRef(\n defaultCallTimeout ?? DEFAULT_CALL_TIMEOUT_MS\n );\n defaultCallTimeoutRef.current = defaultCallTimeout ?? DEFAULT_CALL_TIMEOUT_MS;\n\n /** Reject (and remove) every pending call transmitted on `socket`. */\n const rejectCallsSentOn = (socket: PartySocket, reason: string) => {\n const error = new Error(reason);\n for (const [id, pending] of pendingCallsRef.current) {\n if (pending.sentOn === socket) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pendingCallsRef.current.delete(id);\n pending.reject(error);\n pending.stream?.onError?.(reason);\n }\n }\n };\n\n /** Transmit queued (never-sent) calls if the live socket is open. */\n const flushQueuedCalls = () => {\n const socket = socketRef.current;\n if (!socket || socket.readyState !== socket.OPEN) return;\n for (const pending of pendingCallsRef.current.values()) {\n if (pending.sentOn === null) {\n socket.send(pending.request);\n pending.sentOn = socket;\n }\n }\n };\n\n /** Reject (and remove) every still-queued (never transmitted) call. */\n const rejectQueuedCalls = (reason: string) => {\n const error = new Error(reason);\n for (const [id, pending] of pendingCallsRef.current) {\n if (pending.sentOn === null) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pendingCallsRef.current.delete(id);\n pending.reject(error);\n pending.stream?.onError?.(reason);\n }\n }\n };\n\n const cacheKey = useMemo(\n () =>\n createCacheKey(agentNamespace, options.name, subChain, queryDeps || []),\n [agentNamespace, options.name, subChain, queryDeps]\n );\n\n // Track current cache key in a ref for use in onClose handler.\n // This ensures we invalidate the correct cache entry when the connection closes,\n // even if the component re-renders with different props before onClose fires.\n // We update synchronously during render (not in useEffect) to avoid race\n // conditions where onClose could fire before the effect runs.\n const cacheKeyRef = useRef(cacheKey);\n cacheKeyRef.current = cacheKey;\n\n const ttl = cacheTtl ?? 5 * 60 * 1000;\n\n // Track cache invalidation to force re-render when TTL expires\n const [cacheInvalidatedAt, setCacheInvalidatedAt] = useState<number>(0);\n\n // Disable socket while waiting for async query to refresh after disconnect\n const isAsyncQuery = query && typeof query === \"function\";\n const [awaitingQueryRefresh, setAwaitingQueryRefresh] = useState(false);\n\n // Get or create the query promise\n const queryPromise = useMemo(() => {\n // Re-run when cache is invalidated after TTL expiry\n void cacheInvalidatedAt;\n\n if (!query || typeof query !== \"function\") {\n return null;\n }\n\n // Always check cache first to deduplicate concurrent requests\n const cached = getCacheEntry(cacheKey);\n if (cached) {\n return cached.promise;\n }\n\n // Create new promise\n const promise = query().catch((error) => {\n console.error(\n `[useAgent] Query failed for agent \"${options.agent}\":`,\n error\n );\n deleteCacheEntry(cacheKey);\n throw error;\n });\n\n // Always cache to deduplicate concurrent requests\n setCacheEntry(cacheKey, promise, ttl);\n\n return promise;\n }, [cacheKey, query, options.agent, ttl, cacheInvalidatedAt]);\n\n // Schedule cache invalidation when TTL expires\n useEffect(() => {\n if (!queryPromise || ttl <= 0) return;\n\n const entry = getCacheEntry(cacheKey);\n if (!entry) return;\n\n const timeUntilExpiry = entry.expiresAt - Date.now();\n\n // Always set a timer (with min 0ms) to ensure cleanup function is returned\n const timer = setTimeout(\n () => {\n deleteCacheEntry(cacheKey);\n setCacheInvalidatedAt(Date.now());\n },\n Math.max(0, timeUntilExpiry)\n );\n\n return () => clearTimeout(timer);\n }, [cacheKey, queryPromise, ttl]);\n\n let resolvedQuery: QueryObject | undefined;\n\n if (query) {\n if (typeof query === \"function\") {\n // Use React's use() to resolve the promise\n const queryResult = use(queryPromise!);\n\n // Check for non-primitive values and warn\n if (queryResult) {\n for (const [key, value] of Object.entries(queryResult)) {\n if (\n value !== null &&\n value !== undefined &&\n typeof value !== \"string\" &&\n typeof value !== \"number\" &&\n typeof value !== \"boolean\"\n ) {\n console.warn(\n `[useAgent] Query parameter \"${key}\" is an object and will be converted to \"[object Object]\". ` +\n \"Query parameters should be string, number, boolean, or null.\"\n );\n }\n }\n resolvedQuery = queryResult;\n }\n } else {\n // Sync query - use directly\n resolvedQuery = query;\n }\n }\n\n // Re-enable socket after async query resolves\n useEffect(() => {\n if (awaitingQueryRefresh && resolvedQuery !== undefined) {\n setAwaitingQueryRefresh(false);\n }\n }, [awaitingQueryRefresh, resolvedQuery]);\n\n // Track agent state for reactivity — updated on server broadcasts and client setState\n const [agentState, setAgentState] = useState<State | undefined>(undefined);\n\n // Store identity in React state for reactivity. Seed with the\n // leaf's address — what the server will echo back in\n // `cf_agent_identity`.\n const [identity, setIdentity] = useState({\n name: leafName,\n agent: camelCaseToKebabCase(leafAgent),\n identified: false\n });\n\n // Track previous identity for change detection\n const previousIdentityRef = useRef<{\n name: string | null;\n agent: string | null;\n }>({ name: null, agent: null });\n\n // Ready promise - resolves when identity is received, resets on close\n const readyRef = useRef<\n { promise: Promise<void>; resolve: () => void } | undefined\n >(undefined);\n\n const resetReady = () => {\n let resolve: () => void;\n const promise = new Promise<void>((r) => {\n resolve = r;\n });\n readyRef.current = { promise, resolve: resolve! };\n };\n\n if (!readyRef.current) {\n resetReady();\n }\n\n const mutableAgentRef = useRef<{\n agent: string;\n name: string;\n identified: boolean;\n } | null>(null);\n\n // Combine the sub-agent chain with the user-provided `path`.\n // Order matters: `/sub/{child}/{name}/...` comes before `path` so\n // the server sees the hierarchy it expects.\n const combinedPath = useMemo(\n () => buildSubPath(subChain, userPath),\n [subChain, userPath]\n );\n\n // If basePath is provided, use it directly; otherwise construct from agent/name\n const socketOptions = options.basePath\n ? {\n basePath: options.basePath,\n path: combinedPath || undefined,\n query: resolvedQuery,\n ...restOptions\n }\n : {\n party: agentNamespace,\n prefix: \"agents\",\n room: options.name || \"default\",\n path: combinedPath || undefined,\n query: resolvedQuery,\n ...restOptions\n };\n\n const socketEnabled = !awaitingQueryRefresh && (restOptions.enabled ?? true);\n\n // Identifies *which agent instance* this hook is addressing. Queued\n // (never-transmitted) RPC calls are only safe to flush onto a later\n // socket if it still points at the same instance — a call composed for\n // agent \"alpha\" must not execute on agent \"beta\" just because the\n // `name` prop changed while the call was waiting for a connection.\n // Credentials (query params) are deliberately excluded: a token\n // refresh doesn't change where calls go.\n const addressKey = JSON.stringify([\n options.host ?? null,\n options.basePath ?? null,\n agentNamespace,\n options.name || \"default\",\n combinedPath || null\n ]);\n\n const agent = usePartySocket({\n ...socketOptions,\n enabled: socketEnabled,\n onOpen: (event: Event) => {\n // The socket is open: transmit any RPC requests that were issued\n // while disconnected (or while a previous socket was being\n // replaced). They were never handed to a socket before, so this\n // cannot double-execute anything server-side.\n flushQueuedCalls();\n options.onOpen?.(event);\n },\n onMessage: (message) => {\n if (typeof message.data === \"string\") {\n let parsedMessage: Record<string, unknown>;\n try {\n parsedMessage = JSON.parse(message.data);\n } catch (_error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return options.onMessage?.(message);\n }\n if (parsedMessage.type === MessageType.CF_AGENT_IDENTITY) {\n const oldName = previousIdentityRef.current.name;\n const oldAgent = previousIdentityRef.current.agent;\n const newName = parsedMessage.name as string;\n const newAgent = parsedMessage.agent as string;\n\n const currentAgent = mutableAgentRef.current;\n if (currentAgent) {\n currentAgent.name = newName;\n currentAgent.agent = newAgent;\n currentAgent.identified = true;\n }\n\n // Update reactive state (triggers re-render)\n setIdentity({ name: newName, agent: newAgent, identified: true });\n\n // Resolve ready promise\n readyRef.current?.resolve();\n\n // Detect identity change on reconnect\n if (\n oldName !== null &&\n oldAgent !== null &&\n (oldName !== newName || oldAgent !== newAgent)\n ) {\n if (options.onIdentityChange) {\n options.onIdentityChange(oldName, newName, oldAgent, newAgent);\n } else {\n const agentChanged = oldAgent !== newAgent;\n const nameChanged = oldName !== newName;\n let changeDescription = \"\";\n if (agentChanged && nameChanged) {\n changeDescription = `agent \"${oldAgent}\" → \"${newAgent}\", instance \"${oldName}\" → \"${newName}\"`;\n } else if (agentChanged) {\n changeDescription = `agent \"${oldAgent}\" → \"${newAgent}\"`;\n } else {\n changeDescription = `instance \"${oldName}\" → \"${newName}\"`;\n }\n console.warn(\n `[agents] Identity changed on reconnect: ${changeDescription}. ` +\n \"This can happen with server-side routing (e.g., basePath with getAgentByName) \" +\n \"where the instance is determined by auth/session. \" +\n \"Provide onIdentityChange callback to handle this explicitly, \" +\n \"or ignore if this is expected for your routing pattern.\"\n );\n }\n }\n\n // Track for next change detection\n previousIdentityRef.current = { name: newName, agent: newAgent };\n\n // Call onIdentity callback\n options.onIdentity?.(newName, newAgent);\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_STATE) {\n setAgentState(parsedMessage.state as State);\n options.onStateUpdate?.(parsedMessage.state as State, \"server\");\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_STATE_ERROR) {\n options.onStateUpdateError?.(parsedMessage.error as string);\n return;\n }\n if (parsedMessage.type === MessageType.CF_AGENT_MCP_SERVERS) {\n options.onMcpUpdate?.(parsedMessage.mcp as MCPServersState);\n return;\n }\n if (parsedMessage.type === MessageType.RPC) {\n const response = parsedMessage as RPCResponse;\n const pending = pendingCallsRef.current.get(response.id);\n if (!pending) {\n console.warn(\n `[useAgent] Discarded an RPC response with no matching pending call (id \"${response.id}\"). ` +\n \"The call likely timed out or was rejected when its connection closed before the response arrived.\"\n );\n return;\n }\n\n if (!response.success) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.reject(new Error(response.error));\n pendingCallsRef.current.delete(response.id);\n pending.stream?.onError?.(response.error);\n return;\n }\n\n // Handle streaming responses\n if (\"done\" in response) {\n if (response.done) {\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.resolve(response.result);\n pendingCallsRef.current.delete(response.id);\n pending.stream?.onDone?.(response.result);\n } else {\n pending.stream?.onChunk?.(response.result);\n }\n } else {\n // Non-streaming response\n if (pending.timeoutId) clearTimeout(pending.timeoutId);\n pending.resolve(response.result);\n pendingCallsRef.current.delete(response.id);\n }\n return;\n }\n }\n options.onMessage?.(message);\n },\n onClose: (event: CloseEvent) => {\n // Identify which socket actually closed. Close events are\n // dispatched asynchronously, so a close from an old socket that\n // was just replaced can arrive while a new socket is already\n // connecting (or connected). `event.target` is the PartySocket\n // that dispatched the event; fall back to the live socket if the\n // environment doesn't populate it.\n const closedSocket =\n (event.target as PartySocket | null) ?? socketRef.current;\n const isCurrentSocket = closedSocket === socketRef.current;\n\n // Calls transmitted on the closed socket can never receive their\n // response — reject them. Calls still queued (never transmitted)\n // stay pending and are flushed when a socket next opens; calls\n // in flight on a *different* (newer) socket are untouched.\n if (closedSocket) {\n rejectCallsSentOn(closedSocket, \"Connection closed\");\n }\n\n if (isCurrentSocket) {\n // Reset ready state for next connection\n resetReady();\n if (mutableAgentRef.current) {\n mutableAgentRef.current.identified = false;\n }\n setIdentity((prev) => ({ ...prev, identified: false }));\n\n // Pause reconnection for async queries until fresh query params are ready\n if (isAsyncQuery) {\n setAwaitingQueryRefresh(true);\n }\n\n // Invalidate cache and trigger re-render to fetch fresh query params\n deleteCacheEntry(cacheKeyRef.current);\n setCacheInvalidatedAt(Date.now());\n }\n\n // Call user's onClose if provided\n options.onClose?.(event);\n }\n }) as PartySocket & {\n agent: string;\n name: string;\n identified: boolean;\n ready: Promise<void>;\n state: State | undefined;\n setState: (state: State) => void;\n call: UntypedAgentMethodCall;\n stub: UntypedAgentStub;\n getHttpUrl: () => string;\n };\n // Update the live-socket ref before anything below can use it.\n socketRef.current = agent;\n\n // When `usePartySocket` replaces the socket object (connection options\n // changed — async query refresh, path change, enabled toggle, ...) the\n // old socket's event listeners are detached at the same commit, so its\n // final close event may never be observed by our onClose handler.\n // Sweep here instead: anything transmitted on the old socket can never\n // get a response, and the identity it established no longer applies.\n // Queued (never-transmitted) calls survive and flush when the new\n // socket opens.\n const prevSocketRef = useRef<PartySocket | null>(null);\n const prevAddressKeyRef = useRef(addressKey);\n useEffect(() => {\n const prev = prevSocketRef.current;\n prevSocketRef.current = agent;\n const prevAddress = prevAddressKeyRef.current;\n prevAddressKeyRef.current = addressKey;\n\n // Destination guard: if the agent address changed (different agent,\n // name, or path — not just refreshed credentials), calls that are\n // still queued were composed for the *old* instance. Reject them\n // before anything can flush them onto the new instance.\n if (prevAddress !== addressKey) {\n rejectQueuedCalls(\n \"Call discarded: the agent address changed before the request could be sent\"\n );\n }\n\n if (prev && prev !== agent) {\n rejectCallsSentOn(prev, \"Connection closed\");\n resetReady();\n if (mutableAgentRef.current) {\n mutableAgentRef.current.identified = false;\n }\n setIdentity((current) =>\n current.identified ? { ...current, identified: false } : current\n );\n }\n // The helpers only touch refs; re-running on socket/address change is all we need.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [agent, addressKey]);\n\n // Create the call method. Deliberately dependency-free: it routes\n // through refs, so even a stale `agent` reference captured by an old\n // effect closure issues calls against the live socket.\n const call = useCallback(\n <T = unknown,>(\n method: string,\n args: unknown[] = [],\n options?: CallOptions | StreamOptions\n ): Promise<T> => {\n return new Promise((resolve, reject) => {\n const id = crypto.randomUUID();\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n // Detect legacy format: { onChunk?, onDone?, onError? } vs new format: { timeout?, stream? }\n const isLegacyFormat =\n options &&\n (\"onChunk\" in options || \"onDone\" in options || \"onError\" in options);\n const streamOptions = isLegacyFormat\n ? (options as StreamOptions)\n : (options as CallOptions | undefined)?.stream;\n const timeout = isLegacyFormat\n ? undefined\n : (options as CallOptions | undefined)?.timeout;\n\n // Apply the default timeout as a backstop for non-streaming\n // calls so a lost response rejects instead of hanging forever.\n // An explicit `timeout` (including 0 = disabled) always wins.\n const effectiveTimeout =\n timeout !== undefined\n ? timeout\n : streamOptions\n ? undefined\n : defaultCallTimeoutRef.current;\n\n if (effectiveTimeout) {\n timeoutId = setTimeout(() => {\n const pending = pendingCallsRef.current.get(id);\n pendingCallsRef.current.delete(id);\n const errorMessage = `RPC call to ${method} timed out after ${effectiveTimeout}ms`;\n pending?.stream?.onError?.(errorMessage);\n reject(new Error(errorMessage));\n }, effectiveTimeout);\n }\n\n const rpcRequest: RPCRequest = {\n args,\n id,\n method,\n type: MessageType.RPC\n };\n const request = JSON.stringify(rpcRequest);\n\n pendingCallsRef.current.set(id, {\n reject,\n resolve: resolve as (value: unknown) => void,\n stream: streamOptions,\n timeoutId,\n request,\n sentOn: null\n });\n\n // Transmit immediately if the live socket is open; otherwise the\n // request stays queued and is flushed on the next open event.\n // We never hand requests to a non-open socket: its internal\n // buffer is lost forever if the socket gets replaced.\n const socket = socketRef.current;\n if (socket && socket.readyState === socket.OPEN) {\n socket.send(request);\n const pending = pendingCallsRef.current.get(id);\n if (pending) pending.sentOn = socket;\n }\n });\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n\n agent.setState = (newState: State) => {\n // Route through the live socket so stale `agent` references don't\n // write into a replaced socket's dead buffer.\n (socketRef.current ?? agent).send(\n JSON.stringify({ state: newState, type: MessageType.CF_AGENT_STATE })\n );\n setAgentState(newState);\n options.onStateUpdate?.(newState, \"client\");\n };\n\n agent.call = call;\n // Use reactive identity state (updates on identity message)\n agent.agent = identity.agent;\n agent.name = identity.name;\n // Full root-first chain including the leaf. Computed from the\n // user-provided options — the server doesn't need to echo it\n // back because the client already knows. Write past the\n // PartySocket `.path: string` shape via an unknown cast — the\n // overload signatures expose this as `ReadonlyArray<...>`.\n (\n agent as unknown as { path: ReadonlyArray<{ agent: string; name: string }> }\n ).path = fullPath;\n agent.identified = identity.identified;\n agent.ready = readyRef.current!.promise;\n agent.state = agentState;\n mutableAgentRef.current = agent;\n // Memoize stub so it's referentially stable across renders\n // (call is already stable via useCallback)\n const stub = useMemo(() => createStubProxy(call), [call]);\n agent.stub = stub;\n agent.getHttpUrl = () => {\n // TODO: upstream to partysocket — expose an HTTP URL property\n // @ts-expect-error accessing protected PartySocket internals\n const wsUrl: string = (agent._url as string | null) || agent._pkurl || \"\";\n return wsUrl.replace(\"ws://\", \"http://\").replace(\"wss://\", \"https://\");\n };\n\n // warn if agent isn't in lowercase\n if (identity.agent !== identity.agent.toLowerCase()) {\n console.warn(\n \"Agent name: \" +\n identity.agent +\n \" should probably be in lowercase. Received: \" +\n identity.agent\n );\n }\n\n // The overload signatures return `Omit<PartySocket, \"path\"> & { path: ... }`,\n // but `agent` is inferred as the raw PartySocket. Cast to satisfy\n // the overload contract — the runtime override of `agent.path`\n // above ensures the shape matches.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return agent as any;\n}\n\ntype AgentToolEventAgent = Pick<\n PartySocket,\n \"addEventListener\" | \"removeEventListener\"\n>;\n\nfunction agentToolDedupeKey(message: AgentToolEventMessage): string {\n return [\n message.parentToolCallId ?? \"\",\n message.event.runId,\n String(message.sequence)\n ].join(\"\\0\");\n}\n\nexport function useAgentToolEvents(options: { agent: AgentToolEventAgent }): {\n runsById: Record<string, AgentToolRunState>;\n runsByToolCallId: Record<string, AgentToolRunState[]>;\n unboundRuns: AgentToolRunState[];\n getRunsForToolCall(toolCallId: string): AgentToolRunState[];\n resetLocalState(): void;\n} {\n const { agent } = options;\n const [state, setState] = useState<AgentToolEventState>(() =>\n createAgentToolEventState()\n );\n const seenRef = useRef(new Set<string>());\n\n useEffect(() => {\n const onMessage = (event: MessageEvent) => {\n if (typeof event.data !== \"string\") return;\n let message: AgentToolEventMessage;\n try {\n message = JSON.parse(event.data) as AgentToolEventMessage;\n } catch {\n return;\n }\n if (message.type !== \"agent-tool-event\") return;\n const key = agentToolDedupeKey(message);\n if (seenRef.current.has(key)) return;\n seenRef.current.add(key);\n setState((prev) => applyAgentToolEvent(prev, message));\n };\n\n agent.addEventListener(\"message\", onMessage);\n return () => agent.removeEventListener(\"message\", onMessage);\n }, [agent]);\n\n const resetLocalState = useCallback(() => {\n seenRef.current.clear();\n setState(createAgentToolEventState());\n }, []);\n\n const getRunsForToolCall = useCallback(\n (toolCallId: string) => state.runsByToolCallId[toolCallId] ?? [],\n [state.runsByToolCallId]\n );\n\n return {\n ...state,\n getRunsForToolCall,\n resetLocalState\n };\n}\n"],"mappings":";;;;;;;AAgCA,MAAM,6BAAa,IAAI,IAAwB;AAE/C,SAAS,eACP,gBACA,MACA,gBACA,MACQ;CAUR,IAAI,SAAS,KAAA,GACX,OAAO,KAAK,UAAU;EACpB;EACA,QAAQ;EACR,GAAI;CACN,CAAC;CAEH,MAAM,WAAW;CAIjB,IAAI,SAAS,WAAW,GACtB,OAAO,KAAK,UAAU;EAAC;EAAgB,QAAQ;EAAW,GAAG;CAAI,CAAC;CAEpE,OAAO,KAAK,UAAU;EACpB;EACA,QAAQ;EACR,SAAS,KAAK,MAAM,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC;EACrC,GAAG;CACL,CAAC;AACH;;AAGA,SAAS,aACP,UACA,WACQ;CACR,IAAI,SAAS,WAAW,GAAG,OAAO,aAAa;CAM/C,MAAM,WALQ,SAAS,SAAS,SAAS;EACvC;EACA,qBAAqB,KAAK,KAAK;EAC/B,mBAAmB,KAAK,IAAI;CAC9B,CACqB,CAAC,CAAC,KAAK,GAAG;CAC/B,IAAI,WAEF,OAAO,GAAG,SAAS,GADH,UAAU,WAAW,GAAG,IAA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GACvB,UAAU,SAAS,oBAAoB,MAAM,OAAO,CAAC;EACvD;EAEA,MAAM,iBAAiB,WAAW,SAAS;EAC3C,aAAa,MAAM,oBAAoB,WAAW,SAAS;CAC7D,GAAG,CAAC,KAAK,CAAC;CAEV,MAAM,kBAAkB,kBAAkB;EACxC,QAAQ,QAAQ,MAAM;EACtB,SAAS,0BAA0B,CAAC;CACtC,GAAG,CAAC,CAAC;CAEL,MAAM,qBAAqB,aACxB,eAAuB,MAAM,iBAAiB,eAAe,CAAC,GAC/D,CAAC,MAAM,gBAAgB,CACzB;CAEA,OAAO;EACL,GAAG;EACH;EACA;CACF;AACF"}
|
|
@@ -68,12 +68,42 @@ declare function tryN<T>(
|
|
|
68
68
|
* An error is retryable if it has `retryable: true` but is NOT an overloaded error.
|
|
69
69
|
*/
|
|
70
70
|
declare function isErrorRetryable(err: unknown): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Whether an error (or anything in its `cause` chain) is a transient
|
|
73
|
+
* "superseded isolate" failure — see `SUPERSEDED_ISOLATE_PATTERN`. In-process
|
|
74
|
+
* retries are futile for this class; the work must be deferred to a fresh
|
|
75
|
+
* invocation, which runs the new code and succeeds.
|
|
76
|
+
*/
|
|
77
|
+
declare function isDurableObjectCodeUpdateReset(error: unknown): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Whether an error (or anything in its `cause` chain) is a transient failure
|
|
80
|
+
* of the PLATFORM rather than of the code that threw it:
|
|
81
|
+
*
|
|
82
|
+
* - a superseded-isolate reset ("reset because its code was updated" /
|
|
83
|
+
* "this script has been upgraded") — a deploy replaced the isolate;
|
|
84
|
+
* - an error the platform itself flags `retryable: true` (excluding
|
|
85
|
+
* overloaded errors, where retrying the same object won't help) — see
|
|
86
|
+
* `isErrorRetryable`;
|
|
87
|
+
* - "Network connection lost." — the storage/stub connection dropped. The
|
|
88
|
+
* CF `retryable` flag does not survive error wrappers (e.g. `SqlError`
|
|
89
|
+
* copies only the message + `cause`) and is absent in some local-dev
|
|
90
|
+
* shapes, so the verbatim message is matched as well.
|
|
91
|
+
*
|
|
92
|
+
* Used to decide whether failed work should be RE-RUN LATER (platform
|
|
93
|
+
* transient — the same work succeeds once the platform recovers, typically
|
|
94
|
+
* seconds after a deploy) versus ABANDONED as genuinely failing (application
|
|
95
|
+
* error — re-running yields the same failure). A genuine application error
|
|
96
|
+
* carries none of these signals, so it is never misclassified by this check.
|
|
97
|
+
*/
|
|
98
|
+
declare function isPlatformTransientError(error: unknown): boolean;
|
|
71
99
|
//#endregion
|
|
72
100
|
export {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
101
|
+
jitterBackoff as a,
|
|
102
|
+
isPlatformTransientError as i,
|
|
103
|
+
isDurableObjectCodeUpdateReset as n,
|
|
104
|
+
tryN as o,
|
|
105
|
+
isErrorRetryable as r,
|
|
106
|
+
validateRetryOptions as s,
|
|
77
107
|
RetryOptions as t
|
|
78
108
|
};
|
|
79
|
-
//# sourceMappingURL=retries-
|
|
109
|
+
//# sourceMappingURL=retries-CwlpAGet.d.ts.map
|
package/dist/retries.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
|
-
a as
|
|
3
|
-
i as
|
|
4
|
-
n as
|
|
5
|
-
|
|
2
|
+
a as jitterBackoff,
|
|
3
|
+
i as isPlatformTransientError,
|
|
4
|
+
n as isDurableObjectCodeUpdateReset,
|
|
5
|
+
o as tryN,
|
|
6
|
+
r as isErrorRetryable,
|
|
7
|
+
s as validateRetryOptions,
|
|
6
8
|
t as RetryOptions
|
|
7
|
-
} from "./retries-
|
|
9
|
+
} from "./retries-CwlpAGet.js";
|
|
8
10
|
export {
|
|
9
11
|
RetryOptions,
|
|
12
|
+
isDurableObjectCodeUpdateReset,
|
|
10
13
|
isErrorRetryable,
|
|
14
|
+
isPlatformTransientError,
|
|
11
15
|
jitterBackoff,
|
|
12
16
|
tryN,
|
|
13
17
|
validateRetryOptions
|
package/dist/retries.js
CHANGED
|
@@ -76,7 +76,93 @@ function isErrorRetryable(err) {
|
|
|
76
76
|
const typed = err;
|
|
77
77
|
return Boolean(typed.retryable) && !typed.overloaded && !msg.includes("Durable Object is overloaded");
|
|
78
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* The "superseded isolate" platform messages — the invocation is running on an
|
|
81
|
+
* isolate the platform has replaced with a new version (a deploy / code
|
|
82
|
+
* update). For the rest of that invocation every operation throws the same
|
|
83
|
+
* error (code never reloads mid-invocation), so in-process retries are futile;
|
|
84
|
+
* but the next fresh invocation runs the new code and succeeds.
|
|
85
|
+
*
|
|
86
|
+
* workerd surfaces this as a plain `Error` with one of a few messages, all the
|
|
87
|
+
* same failure class — a message match is the only signal:
|
|
88
|
+
* - "Durable Object reset because its code was updated." (DO storage op on a
|
|
89
|
+
* superseded isolate / deploy bounce)
|
|
90
|
+
* - "This script has been upgraded. Please send a new request to connect to
|
|
91
|
+
* the new version." (a stub/connection to a superseded script; the message
|
|
92
|
+
* literally instructs the caller to retry on the new version)
|
|
93
|
+
*
|
|
94
|
+
* The match stays close to the verbatim platform strings (rather than a loose
|
|
95
|
+
* "upgraded"/"reset" substring) so an ordinary application error that happens
|
|
96
|
+
* to mention those words is NOT misclassified as a supersede.
|
|
97
|
+
*/
|
|
98
|
+
const SUPERSEDED_ISOLATE_PATTERN = /reset because its code was updated|this script has been upgraded/i;
|
|
99
|
+
/**
|
|
100
|
+
* The "Network connection lost." platform transient — the connection between
|
|
101
|
+
* the isolate and its storage (or another DO) dropped. Unlike a supersede this
|
|
102
|
+
* MAY succeed on an in-process retry (a momentary blip), so it must not skip
|
|
103
|
+
* the in-process retry budget — but during a deploy-reset window it never
|
|
104
|
+
* succeeds in-process and surfaces interleaved with the supersede messages
|
|
105
|
+
* (SQL ops throw `SqlError: SQL query failed: Network connection lost.` while
|
|
106
|
+
* KV ops throw the reset message), so on retry exhaustion it must be treated
|
|
107
|
+
* as the platform's failure, not the callback's.
|
|
108
|
+
*/
|
|
109
|
+
const CONNECTION_LOST_PATTERN = /network connection lost/i;
|
|
110
|
+
function errorMessageOf(error) {
|
|
111
|
+
return error instanceof Error ? error.message : typeof error === "string" ? error : "";
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Iterate an error and its `cause` chain (depth-limited so a cyclic chain
|
|
115
|
+
* can't spin). Wrappers like `SqlError` carry the original platform error in
|
|
116
|
+
* `cause` and may not propagate signal properties (e.g. the CF `retryable`
|
|
117
|
+
* flag), so classification must look through them.
|
|
118
|
+
*/
|
|
119
|
+
function* selfAndCauses(error) {
|
|
120
|
+
let current = error;
|
|
121
|
+
for (let depth = 0; depth < 8 && current != null; depth++) {
|
|
122
|
+
yield current;
|
|
123
|
+
current = typeof current === "object" ? current.cause : void 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Whether an error (or anything in its `cause` chain) is a transient
|
|
128
|
+
* "superseded isolate" failure — see `SUPERSEDED_ISOLATE_PATTERN`. In-process
|
|
129
|
+
* retries are futile for this class; the work must be deferred to a fresh
|
|
130
|
+
* invocation, which runs the new code and succeeds.
|
|
131
|
+
*/
|
|
132
|
+
function isDurableObjectCodeUpdateReset(error) {
|
|
133
|
+
for (const e of selfAndCauses(error)) if (SUPERSEDED_ISOLATE_PATTERN.test(errorMessageOf(e))) return true;
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Whether an error (or anything in its `cause` chain) is a transient failure
|
|
138
|
+
* of the PLATFORM rather than of the code that threw it:
|
|
139
|
+
*
|
|
140
|
+
* - a superseded-isolate reset ("reset because its code was updated" /
|
|
141
|
+
* "this script has been upgraded") — a deploy replaced the isolate;
|
|
142
|
+
* - an error the platform itself flags `retryable: true` (excluding
|
|
143
|
+
* overloaded errors, where retrying the same object won't help) — see
|
|
144
|
+
* `isErrorRetryable`;
|
|
145
|
+
* - "Network connection lost." — the storage/stub connection dropped. The
|
|
146
|
+
* CF `retryable` flag does not survive error wrappers (e.g. `SqlError`
|
|
147
|
+
* copies only the message + `cause`) and is absent in some local-dev
|
|
148
|
+
* shapes, so the verbatim message is matched as well.
|
|
149
|
+
*
|
|
150
|
+
* Used to decide whether failed work should be RE-RUN LATER (platform
|
|
151
|
+
* transient — the same work succeeds once the platform recovers, typically
|
|
152
|
+
* seconds after a deploy) versus ABANDONED as genuinely failing (application
|
|
153
|
+
* error — re-running yields the same failure). A genuine application error
|
|
154
|
+
* carries none of these signals, so it is never misclassified by this check.
|
|
155
|
+
*/
|
|
156
|
+
function isPlatformTransientError(error) {
|
|
157
|
+
for (const e of selfAndCauses(error)) {
|
|
158
|
+
const message = errorMessageOf(e);
|
|
159
|
+
if (SUPERSEDED_ISOLATE_PATTERN.test(message)) return true;
|
|
160
|
+
if (CONNECTION_LOST_PATTERN.test(message)) return true;
|
|
161
|
+
if (isErrorRetryable(e)) return true;
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
79
165
|
//#endregion
|
|
80
|
-
export { isErrorRetryable, jitterBackoff, tryN, validateRetryOptions };
|
|
166
|
+
export { isDurableObjectCodeUpdateReset, isErrorRetryable, isPlatformTransientError, jitterBackoff, tryN, validateRetryOptions };
|
|
81
167
|
|
|
82
168
|
//# sourceMappingURL=retries.js.map
|