agents 0.0.0-06b2ab0 → 0.0.0-087264c
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/README.md +126 -3
- package/dist/ai-chat-agent.d.ts +234 -29
- package/dist/ai-chat-agent.js +1118 -644
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +7 -4
- package/dist/ai-chat-v5-migration.js +153 -18
- package/dist/ai-chat-v5-migration.js.map +1 -1
- package/dist/ai-react.d.ts +144 -29
- package/dist/ai-react.js +572 -350
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-0OnT3FHg.d.ts +127 -0
- package/dist/ai-types-DEtF_8Km.js +28 -0
- package/dist/ai-types-DEtF_8Km.js.map +1 -0
- package/dist/ai-types.d.ts +6 -92
- package/dist/ai-types.js +3 -8
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +28 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client-CdM5I962.d.ts +104 -0
- package/dist/client-DFotUKH_.d.ts +834 -0
- package/dist/client-DjTPRM8-.js +117 -0
- package/dist/client-DjTPRM8-.js.map +1 -0
- package/dist/client-QZa2Rq0l.js +1105 -0
- package/dist/client-QZa2Rq0l.js.map +1 -0
- package/dist/client.d.ts +10 -92
- package/dist/client.js +3 -13
- package/dist/codemode/ai.d.ts +19 -17
- package/dist/codemode/ai.js +90 -5139
- package/dist/codemode/ai.js.map +1 -1
- package/dist/context-BkKbAa1R.js +8 -0
- package/dist/context-BkKbAa1R.js.map +1 -0
- package/dist/context-DcbQ8o7k.d.ts +24 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +3 -0
- package/dist/do-oauth-client-provider--To1Tsjj.d.ts +70 -0
- package/dist/do-oauth-client-provider-B1fVIshX.js +155 -0
- package/dist/do-oauth-client-provider-B1fVIshX.js.map +1 -0
- package/dist/index-BaskqQ6f.d.ts +576 -0
- package/dist/index-DLuxm_9W.d.ts +58 -0
- package/dist/index.d.ts +52 -542
- package/dist/index.js +7 -32
- package/dist/mcp/client.d.ts +2 -12
- package/dist/mcp/client.js +4 -10
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -42
- package/dist/mcp/do-oauth-client-provider.js +3 -8
- package/dist/mcp/index.d.ts +198 -77
- package/dist/mcp/index.js +1423 -1080
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +22 -27
- package/dist/mcp/x402.js +190 -3187
- package/dist/mcp/x402.js.map +1 -1
- package/dist/{mcp-BH1fJeiU.d.ts → mcp-CPSfGUgd.d.ts} +6 -3
- package/dist/observability/index.d.ts +2 -34
- package/dist/observability/index.js +7 -12
- package/dist/react.d.ts +40 -33
- package/dist/react.js +179 -206
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +23 -74
- package/dist/schedule.js +43 -33
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-Crsj26mx.d.ts +39 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-BZDh910Z.js +1181 -0
- package/dist/src-BZDh910Z.js.map +1 -0
- package/package.json +75 -34
- package/dist/_esm-LV5FJ3HK.js +0 -3922
- package/dist/_esm-LV5FJ3HK.js.map +0 -1
- package/dist/ai-types.js.map +0 -1
- package/dist/ccip-CMBYN64O.js +0 -15
- package/dist/ccip-CMBYN64O.js.map +0 -1
- package/dist/chunk-5Y6BEZDY.js +0 -276
- package/dist/chunk-5Y6BEZDY.js.map +0 -1
- package/dist/chunk-BER7KXUJ.js +0 -18
- package/dist/chunk-BER7KXUJ.js.map +0 -1
- package/dist/chunk-C2OEBJZ2.js +0 -113
- package/dist/chunk-C2OEBJZ2.js.map +0 -1
- package/dist/chunk-JJBFIGUC.js +0 -5202
- package/dist/chunk-JJBFIGUC.js.map +0 -1
- package/dist/chunk-PR4QN5HX.js +0 -43
- package/dist/chunk-PR4QN5HX.js.map +0 -1
- package/dist/chunk-QEVM4BVL.js +0 -116
- package/dist/chunk-QEVM4BVL.js.map +0 -1
- package/dist/chunk-TYAY6AU6.js +0 -159
- package/dist/chunk-TYAY6AU6.js.map +0 -1
- package/dist/chunk-UJVEAURM.js +0 -150
- package/dist/chunk-UJVEAURM.js.map +0 -1
- package/dist/chunk-XGMKNUJA.js +0 -1343
- package/dist/chunk-XGMKNUJA.js.map +0 -1
- package/dist/chunk-ZMMHNOMZ.js +0 -942
- package/dist/chunk-ZMMHNOMZ.js.map +0 -1
- package/dist/client-DVoPb3-C.d.ts +0 -5120
- package/dist/client.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp/client.js.map +0 -1
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/dist/observability/index.js.map +0 -1
- package/dist/secp256k1-M22GZP2U.js +0 -2193
- package/dist/secp256k1-M22GZP2U.js.map +0 -1
- package/dist/serializable.js.map +0 -1
- package/src/index.ts +0 -2031
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/observability/base.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Base event structure for all observability events
|
|
3
4
|
*/
|
|
@@ -24,7 +25,8 @@ type BaseEvent<
|
|
|
24
25
|
*/
|
|
25
26
|
timestamp: number;
|
|
26
27
|
};
|
|
27
|
-
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/observability/mcp.d.ts
|
|
28
30
|
/**
|
|
29
31
|
* MCP-specific observability events
|
|
30
32
|
* These track the lifecycle of MCP connections and operations
|
|
@@ -54,5 +56,6 @@ type MCPObservabilityEvent =
|
|
|
54
56
|
}
|
|
55
57
|
>
|
|
56
58
|
| BaseEvent<"mcp:client:discover", {}>;
|
|
57
|
-
|
|
58
|
-
export
|
|
59
|
+
//#endregion
|
|
60
|
+
export { BaseEvent as n, MCPObservabilityEvent as t };
|
|
61
|
+
//# sourceMappingURL=mcp-CPSfGUgd.d.ts.map
|
|
@@ -1,34 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Agent-specific observability events
|
|
5
|
-
* These track the lifecycle and operations of an Agent
|
|
6
|
-
*/
|
|
7
|
-
type AgentObservabilityEvent = BaseEvent<"state:update", {}> | BaseEvent<"rpc", {
|
|
8
|
-
method: string;
|
|
9
|
-
streaming?: boolean;
|
|
10
|
-
}> | BaseEvent<"message:request" | "message:response", {}> | BaseEvent<"message:clear"> | BaseEvent<"schedule:create" | "schedule:execute" | "schedule:cancel", {
|
|
11
|
-
callback: string;
|
|
12
|
-
id: string;
|
|
13
|
-
}> | BaseEvent<"destroy"> | BaseEvent<"connect", {
|
|
14
|
-
connectionId: string;
|
|
15
|
-
}>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Union of all observability event types from different domains
|
|
19
|
-
*/
|
|
20
|
-
type ObservabilityEvent = AgentObservabilityEvent | MCPObservabilityEvent;
|
|
21
|
-
interface Observability {
|
|
22
|
-
/**
|
|
23
|
-
* Emit an event for the Agent's observability implementation to handle.
|
|
24
|
-
* @param event - The event to emit
|
|
25
|
-
* @param ctx - The execution context of the invocation (optional)
|
|
26
|
-
*/
|
|
27
|
-
emit(event: ObservabilityEvent, ctx?: DurableObjectState): void;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* A generic observability implementation that logs events to the console.
|
|
31
|
-
*/
|
|
32
|
-
declare const genericObservability: Observability;
|
|
33
|
-
|
|
34
|
-
export { type Observability, type ObservabilityEvent, genericObservability };
|
|
1
|
+
import { n as ObservabilityEvent, r as genericObservability, t as Observability } from "../index-DLuxm_9W.js";
|
|
2
|
+
export { Observability, ObservabilityEvent, genericObservability };
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import "../
|
|
5
|
-
import "../
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import "../chunk-PR4QN5HX.js";
|
|
9
|
-
export {
|
|
10
|
-
genericObservability
|
|
11
|
-
};
|
|
12
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import "../context-BkKbAa1R.js";
|
|
2
|
+
import "../client-DjTPRM8-.js";
|
|
3
|
+
import "../client-QZa2Rq0l.js";
|
|
4
|
+
import "../do-oauth-client-provider-B1fVIshX.js";
|
|
5
|
+
import { f as genericObservability } from "../src-BZDh910Z.js";
|
|
6
|
+
|
|
7
|
+
export { genericObservability };
|
package/dist/react.d.ts
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
|
+
import "./context-DcbQ8o7k.js";
|
|
2
|
+
import "./client-DFotUKH_.js";
|
|
3
|
+
import "./ai-types-0OnT3FHg.js";
|
|
4
|
+
import { p as MCPServersState, t as Agent } from "./index-BaskqQ6f.js";
|
|
5
|
+
import { n as RPCMethod, t as Method } from "./serializable-Crsj26mx.js";
|
|
6
|
+
import { i as StreamOptions } from "./client-CdM5I962.js";
|
|
1
7
|
import { PartySocket } from "partysocket";
|
|
2
8
|
import { usePartySocket } from "partysocket/react";
|
|
3
|
-
import { MCPServersState, Agent } from "./index.js";
|
|
4
|
-
import { StreamOptions } from "./client.js";
|
|
5
|
-
import { Method, RPCMethod } from "./serializable.js";
|
|
6
|
-
import "cloudflare:workers";
|
|
7
|
-
import "@modelcontextprotocol/sdk/client/index.js";
|
|
8
|
-
import "@modelcontextprotocol/sdk/types.js";
|
|
9
|
-
import "partyserver";
|
|
10
|
-
import "./client-DVoPb3-C.js";
|
|
11
|
-
import "zod";
|
|
12
|
-
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
13
|
-
import "ai";
|
|
14
|
-
import "./mcp-BH1fJeiU.js";
|
|
15
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
16
|
-
import "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
17
|
-
import "./mcp/do-oauth-client-provider.js";
|
|
18
|
-
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
19
|
-
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
20
|
-
import "./observability/index.js";
|
|
21
|
-
import "./ai-types.js";
|
|
22
9
|
|
|
10
|
+
//#region src/react.d.ts
|
|
23
11
|
type QueryObject = Record<string, string | null>;
|
|
12
|
+
interface CacheEntry {
|
|
13
|
+
promise: Promise<QueryObject>;
|
|
14
|
+
expiresAt: number;
|
|
15
|
+
}
|
|
16
|
+
declare function getCacheEntry(key: string): CacheEntry | undefined;
|
|
17
|
+
declare function setCacheEntry(
|
|
18
|
+
key: string,
|
|
19
|
+
promise: Promise<QueryObject>,
|
|
20
|
+
cacheTtl: number
|
|
21
|
+
): CacheEntry;
|
|
22
|
+
declare function deleteCacheEntry(key: string): void;
|
|
23
|
+
declare const _testUtils: {
|
|
24
|
+
queryCache: Map<string, CacheEntry>;
|
|
25
|
+
setCacheEntry: typeof setCacheEntry;
|
|
26
|
+
getCacheEntry: typeof getCacheEntry;
|
|
27
|
+
deleteCacheEntry: typeof deleteCacheEntry;
|
|
28
|
+
clearCache: () => void;
|
|
29
|
+
};
|
|
24
30
|
/**
|
|
25
31
|
* Options for the useAgent hook
|
|
26
32
|
* @template State Type of the Agent's state
|
|
@@ -66,24 +72,24 @@ type RequiredAgentMethods<T> = Omit<
|
|
|
66
72
|
AgentMethods<T>,
|
|
67
73
|
keyof OptionalAgentMethods<T>
|
|
68
74
|
>;
|
|
69
|
-
type AgentPromiseReturnType<T, K extends keyof AgentMethods<T>> =
|
|
70
|
-
ReturnType<AgentMethods<T>[K]> extends Promise<any>
|
|
71
|
-
? ReturnType<AgentMethods<T>[K]>
|
|
72
|
-
: Promise<ReturnType<AgentMethods<T>[K]>>;
|
|
75
|
+
type AgentPromiseReturnType<T, K$1 extends keyof AgentMethods<T>> =
|
|
76
|
+
ReturnType<AgentMethods<T>[K$1]> extends Promise<any>
|
|
77
|
+
? ReturnType<AgentMethods<T>[K$1]>
|
|
78
|
+
: Promise<ReturnType<AgentMethods<T>[K$1]>>;
|
|
73
79
|
type OptionalArgsAgentMethodCall<AgentT> = <
|
|
74
|
-
K extends keyof OptionalAgentMethods<AgentT>
|
|
80
|
+
K$1 extends keyof OptionalAgentMethods<AgentT>
|
|
75
81
|
>(
|
|
76
|
-
method: K,
|
|
77
|
-
args?: Parameters<OptionalAgentMethods<AgentT>[K]>,
|
|
82
|
+
method: K$1,
|
|
83
|
+
args?: Parameters<OptionalAgentMethods<AgentT>[K$1]>,
|
|
78
84
|
streamOptions?: StreamOptions
|
|
79
|
-
) => AgentPromiseReturnType<AgentT, K>;
|
|
85
|
+
) => AgentPromiseReturnType<AgentT, K$1>;
|
|
80
86
|
type RequiredArgsAgentMethodCall<AgentT> = <
|
|
81
|
-
K extends keyof RequiredAgentMethods<AgentT>
|
|
87
|
+
K$1 extends keyof RequiredAgentMethods<AgentT>
|
|
82
88
|
>(
|
|
83
|
-
method: K,
|
|
84
|
-
args: Parameters<RequiredAgentMethods<AgentT>[K]>,
|
|
89
|
+
method: K$1,
|
|
90
|
+
args: Parameters<RequiredAgentMethods<AgentT>[K$1]>,
|
|
85
91
|
streamOptions?: StreamOptions
|
|
86
|
-
) => AgentPromiseReturnType<AgentT, K>;
|
|
92
|
+
) => AgentPromiseReturnType<AgentT, K$1>;
|
|
87
93
|
type AgentMethodCall<AgentT> = OptionalArgsAgentMethodCall<AgentT> &
|
|
88
94
|
RequiredArgsAgentMethodCall<AgentT>;
|
|
89
95
|
type UntypedAgentMethodCall = <T = unknown>(
|
|
@@ -123,5 +129,6 @@ declare function useAgent<
|
|
|
123
129
|
call: AgentMethodCall<AgentT>;
|
|
124
130
|
stub: AgentStub<AgentT>;
|
|
125
131
|
};
|
|
126
|
-
|
|
127
|
-
export {
|
|
132
|
+
//#endregion
|
|
133
|
+
export { UseAgentOptions, _testUtils, useAgent };
|
|
134
|
+
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.js
CHANGED
|
@@ -1,216 +1,189 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import "
|
|
3
|
-
|
|
4
|
-
// src/react.tsx
|
|
1
|
+
import { t as MessageType } from "./ai-types-DEtF_8Km.js";
|
|
2
|
+
import { use, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
3
|
import { usePartySocket } from "partysocket/react";
|
|
6
|
-
|
|
4
|
+
|
|
5
|
+
//#region src/react.tsx
|
|
6
|
+
/**
|
|
7
|
+
* Convert a camelCase string to a kebab-case string
|
|
8
|
+
* @param str The string to convert
|
|
9
|
+
* @returns The kebab-case string
|
|
10
|
+
*/
|
|
7
11
|
function camelCaseToKebabCase(str) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/[A-Z]/g,
|
|
13
|
-
(letter) => `-${letter.toLowerCase()}`
|
|
14
|
-
);
|
|
15
|
-
kebabified = kebabified.startsWith("-") ? kebabified.slice(1) : kebabified;
|
|
16
|
-
return kebabified.replace(/_/g, "-").replace(/-$/, "");
|
|
12
|
+
if (str === str.toUpperCase() && str !== str.toLowerCase()) return str.toLowerCase().replace(/_/g, "-");
|
|
13
|
+
let kebabified = str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
14
|
+
kebabified = kebabified.startsWith("-") ? kebabified.slice(1) : kebabified;
|
|
15
|
+
return kebabified.replace(/_/g, "-").replace(/-$/, "");
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
function findCacheEntry(targetKey) {
|
|
28
|
-
for (const [existingKey, entry] of queryCache.entries()) {
|
|
29
|
-
if (arraysEqual(existingKey, targetKey)) {
|
|
30
|
-
if (Date.now() > entry.expiresAt) {
|
|
31
|
-
queryCache.delete(existingKey);
|
|
32
|
-
return void 0;
|
|
33
|
-
}
|
|
34
|
-
entry.refCount++;
|
|
35
|
-
return entry.promise;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return void 0;
|
|
17
|
+
const queryCache = /* @__PURE__ */ new Map();
|
|
18
|
+
function createCacheKey(agentNamespace, name, deps) {
|
|
19
|
+
return JSON.stringify([
|
|
20
|
+
agentNamespace,
|
|
21
|
+
name || "default",
|
|
22
|
+
...deps
|
|
23
|
+
]);
|
|
39
24
|
}
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
queryCache.set(key, { promise: value, refCount: 1, expiresAt, cacheTtl });
|
|
25
|
+
function getCacheEntry(key) {
|
|
26
|
+
const entry = queryCache.get(key);
|
|
27
|
+
if (!entry) return void 0;
|
|
28
|
+
if (Date.now() >= entry.expiresAt) {
|
|
29
|
+
queryCache.delete(key);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
return entry;
|
|
49
33
|
}
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return false;
|
|
34
|
+
function setCacheEntry(key, promise, cacheTtl) {
|
|
35
|
+
const entry = {
|
|
36
|
+
promise,
|
|
37
|
+
expiresAt: Date.now() + cacheTtl
|
|
38
|
+
};
|
|
39
|
+
queryCache.set(key, entry);
|
|
40
|
+
return entry;
|
|
61
41
|
}
|
|
62
|
-
function
|
|
63
|
-
|
|
42
|
+
function deleteCacheEntry(key) {
|
|
43
|
+
queryCache.delete(key);
|
|
64
44
|
}
|
|
45
|
+
const _testUtils = {
|
|
46
|
+
queryCache,
|
|
47
|
+
setCacheEntry,
|
|
48
|
+
getCacheEntry,
|
|
49
|
+
deleteCacheEntry,
|
|
50
|
+
clearCache: () => queryCache.clear()
|
|
51
|
+
};
|
|
65
52
|
function useAgent(options) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
{
|
|
199
|
-
get: (_target, method) => {
|
|
200
|
-
return (...args) => {
|
|
201
|
-
return call(method, args);
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
);
|
|
206
|
-
if (agent.agent !== agent.agent.toLowerCase()) {
|
|
207
|
-
console.warn(
|
|
208
|
-
`Agent name: ${agent.agent} should probably be in lowercase. Received: ${agent.agent}`
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
return agent;
|
|
53
|
+
const agentNamespace = camelCaseToKebabCase(options.agent);
|
|
54
|
+
const { query, queryDeps, cacheTtl, ...restOptions } = options;
|
|
55
|
+
const pendingCallsRef = useRef(/* @__PURE__ */ new Map());
|
|
56
|
+
const cacheKey = useMemo(() => createCacheKey(agentNamespace, options.name, queryDeps || []), [
|
|
57
|
+
agentNamespace,
|
|
58
|
+
options.name,
|
|
59
|
+
queryDeps
|
|
60
|
+
]);
|
|
61
|
+
const ttl = cacheTtl ?? 300 * 1e3;
|
|
62
|
+
const [cacheInvalidatedAt, setCacheInvalidatedAt] = useState(0);
|
|
63
|
+
const queryPromise = useMemo(() => {
|
|
64
|
+
if (!query || typeof query !== "function") return null;
|
|
65
|
+
const cached = getCacheEntry(cacheKey);
|
|
66
|
+
if (cached) return cached.promise;
|
|
67
|
+
const promise = query().catch((error) => {
|
|
68
|
+
console.error(`[useAgent] Query failed for agent "${options.agent}":`, error);
|
|
69
|
+
deleteCacheEntry(cacheKey);
|
|
70
|
+
throw error;
|
|
71
|
+
});
|
|
72
|
+
setCacheEntry(cacheKey, promise, ttl);
|
|
73
|
+
return promise;
|
|
74
|
+
}, [
|
|
75
|
+
cacheKey,
|
|
76
|
+
query,
|
|
77
|
+
options.agent,
|
|
78
|
+
ttl,
|
|
79
|
+
cacheInvalidatedAt
|
|
80
|
+
]);
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (!queryPromise || ttl <= 0) return;
|
|
83
|
+
const entry = getCacheEntry(cacheKey);
|
|
84
|
+
if (!entry) return;
|
|
85
|
+
const timeUntilExpiry = entry.expiresAt - Date.now();
|
|
86
|
+
const timer = setTimeout(() => {
|
|
87
|
+
deleteCacheEntry(cacheKey);
|
|
88
|
+
setCacheInvalidatedAt(Date.now());
|
|
89
|
+
}, Math.max(0, timeUntilExpiry));
|
|
90
|
+
return () => clearTimeout(timer);
|
|
91
|
+
}, [
|
|
92
|
+
cacheKey,
|
|
93
|
+
queryPromise,
|
|
94
|
+
ttl
|
|
95
|
+
]);
|
|
96
|
+
let resolvedQuery;
|
|
97
|
+
if (query) if (typeof query === "function") {
|
|
98
|
+
const queryResult = use(queryPromise);
|
|
99
|
+
if (queryResult) {
|
|
100
|
+
for (const [key, value] of Object.entries(queryResult)) if (value !== null && value !== void 0 && typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") console.warn(`[useAgent] Query parameter "${key}" is an object and will be converted to "[object Object]". Query parameters should be string, number, boolean, or null.`);
|
|
101
|
+
resolvedQuery = queryResult;
|
|
102
|
+
}
|
|
103
|
+
} else resolvedQuery = query;
|
|
104
|
+
const agent = usePartySocket({
|
|
105
|
+
party: agentNamespace,
|
|
106
|
+
prefix: "agents",
|
|
107
|
+
room: options.name || "default",
|
|
108
|
+
query: resolvedQuery,
|
|
109
|
+
...restOptions,
|
|
110
|
+
onMessage: (message) => {
|
|
111
|
+
if (typeof message.data === "string") {
|
|
112
|
+
let parsedMessage;
|
|
113
|
+
try {
|
|
114
|
+
parsedMessage = JSON.parse(message.data);
|
|
115
|
+
} catch (_error) {
|
|
116
|
+
return options.onMessage?.(message);
|
|
117
|
+
}
|
|
118
|
+
if (parsedMessage.type === MessageType.CF_AGENT_STATE) {
|
|
119
|
+
options.onStateUpdate?.(parsedMessage.state, "server");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (parsedMessage.type === MessageType.CF_AGENT_MCP_SERVERS) {
|
|
123
|
+
options.onMcpUpdate?.(parsedMessage.mcp);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (parsedMessage.type === MessageType.RPC) {
|
|
127
|
+
const response = parsedMessage;
|
|
128
|
+
const pending = pendingCallsRef.current.get(response.id);
|
|
129
|
+
if (!pending) return;
|
|
130
|
+
if (!response.success) {
|
|
131
|
+
pending.reject(new Error(response.error));
|
|
132
|
+
pendingCallsRef.current.delete(response.id);
|
|
133
|
+
pending.stream?.onError?.(response.error);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if ("done" in response) if (response.done) {
|
|
137
|
+
pending.resolve(response.result);
|
|
138
|
+
pendingCallsRef.current.delete(response.id);
|
|
139
|
+
pending.stream?.onDone?.(response.result);
|
|
140
|
+
} else pending.stream?.onChunk?.(response.result);
|
|
141
|
+
else {
|
|
142
|
+
pending.resolve(response.result);
|
|
143
|
+
pendingCallsRef.current.delete(response.id);
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
options.onMessage?.(message);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
const call = useCallback((method, args = [], streamOptions) => {
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
const id = Math.random().toString(36).slice(2);
|
|
154
|
+
pendingCallsRef.current.set(id, {
|
|
155
|
+
reject,
|
|
156
|
+
resolve,
|
|
157
|
+
stream: streamOptions
|
|
158
|
+
});
|
|
159
|
+
const request = {
|
|
160
|
+
args,
|
|
161
|
+
id,
|
|
162
|
+
method,
|
|
163
|
+
type: MessageType.RPC
|
|
164
|
+
};
|
|
165
|
+
agent.send(JSON.stringify(request));
|
|
166
|
+
});
|
|
167
|
+
}, [agent]);
|
|
168
|
+
agent.setState = (state) => {
|
|
169
|
+
agent.send(JSON.stringify({
|
|
170
|
+
state,
|
|
171
|
+
type: MessageType.CF_AGENT_STATE
|
|
172
|
+
}));
|
|
173
|
+
options.onStateUpdate?.(state, "client");
|
|
174
|
+
};
|
|
175
|
+
agent.call = call;
|
|
176
|
+
agent.agent = agentNamespace;
|
|
177
|
+
agent.name = options.name || "default";
|
|
178
|
+
agent.stub = new Proxy({}, { get: (_target, method) => {
|
|
179
|
+
return (...args) => {
|
|
180
|
+
return call(method, args);
|
|
181
|
+
};
|
|
182
|
+
} });
|
|
183
|
+
if (agent.agent !== agent.agent.toLowerCase()) console.warn(`Agent name: ${agent.agent} should probably be in lowercase. Received: ${agent.agent}`);
|
|
184
|
+
return agent;
|
|
212
185
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
};
|
|
186
|
+
|
|
187
|
+
//#endregion
|
|
188
|
+
export { _testUtils, useAgent };
|
|
216
189
|
//# sourceMappingURL=react.js.map
|