agents 0.0.0-d40512c → 0.0.0-d4257c1
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 +234 -6
- package/dist/ai-chat-agent.d.ts +20 -18
- package/dist/ai-chat-agent.js +532 -260
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-gdyLiTd8.js +155 -0
- package/dist/ai-chat-v5-migration-gdyLiTd8.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +155 -0
- package/dist/ai-chat-v5-migration.js +3 -0
- package/dist/ai-react.d.ts +73 -85
- package/dist/ai-react.js +260 -199
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-BWW4umHY.d.ts +95 -0
- package/dist/ai-types-UZlfLOYP.js +20 -0
- package/dist/ai-types-UZlfLOYP.js.map +1 -0
- package/dist/ai-types.d.ts +6 -74
- package/dist/ai-types.js +3 -1
- package/dist/client-C-nwz-3N.d.ts +5313 -0
- package/dist/client-CZBVDDoO.js +786 -0
- package/dist/client-CZBVDDoO.js.map +1 -0
- package/dist/client-CmMi85Sj.d.ts +104 -0
- package/dist/client-DjR-lC16.js +117 -0
- package/dist/client-DjR-lC16.js.map +1 -0
- package/dist/client.d.ts +11 -92
- package/dist/client.js +4 -11
- package/dist/codemode/ai.d.ts +27 -0
- package/dist/codemode/ai.js +151 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/do-oauth-client-provider-B2jr6UNq.js +93 -0
- package/dist/do-oauth-client-provider-B2jr6UNq.js.map +1 -0
- package/dist/do-oauth-client-provider-CCwGwnrA.d.ts +55 -0
- package/dist/{index-CITGJflw.d.ts → index-CkQU40oY.d.ts} +219 -147
- package/dist/index-W4JUkafc.d.ts +54 -0
- package/dist/index.d.ts +69 -30
- package/dist/index.js +7 -22
- package/dist/mcp/client.d.ts +4 -1055
- package/dist/mcp/client.js +3 -9
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -41
- package/dist/mcp/do-oauth-client-provider.js +3 -7
- package/dist/mcp/index.d.ts +73 -82
- package/dist/mcp/index.js +836 -772
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +34 -0
- package/dist/mcp/x402.js +194 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BEwaCsxO.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -12
- package/dist/observability/index.js +7 -10
- package/dist/react-B4e1rDid.d.ts +113 -0
- package/dist/react.d.ts +10 -119
- package/dist/react.js +183 -110
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +91 -14
- package/dist/schedule.js +46 -21
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-gtr9YMhp.d.ts +34 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-COfG--3R.js +1179 -0
- package/dist/src-COfG--3R.js.map +1 -0
- package/package.json +45 -10
- package/src/index.ts +853 -187
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-4RBEYCWK.js +0 -469
- package/dist/chunk-4RBEYCWK.js.map +0 -1
- package/dist/chunk-KUH345EY.js +0 -116
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-LU2RSO54.js +0 -910
- package/dist/chunk-LU2RSO54.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js +0 -106
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- 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/serializable.js.map +0 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//#region src/observability/base.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Base event structure for all observability events
|
|
4
|
+
*/
|
|
5
|
+
type BaseEvent<
|
|
6
|
+
T extends string,
|
|
7
|
+
Payload extends Record<string, unknown> = {}
|
|
8
|
+
> = {
|
|
9
|
+
type: T;
|
|
10
|
+
/**
|
|
11
|
+
* The unique identifier for the event
|
|
12
|
+
*/
|
|
13
|
+
id: string;
|
|
14
|
+
/**
|
|
15
|
+
* The message to display in the logs for this event, should the implementation choose to display
|
|
16
|
+
* a human-readable message.
|
|
17
|
+
*/
|
|
18
|
+
displayMessage: string;
|
|
19
|
+
/**
|
|
20
|
+
* The payload of the event
|
|
21
|
+
*/
|
|
22
|
+
payload: Payload & Record<string, unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* The timestamp of the event in milliseconds since epoch
|
|
25
|
+
*/
|
|
26
|
+
timestamp: number;
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/observability/mcp.d.ts
|
|
30
|
+
/**
|
|
31
|
+
* MCP-specific observability events
|
|
32
|
+
* These track the lifecycle of MCP connections and operations
|
|
33
|
+
*/
|
|
34
|
+
type MCPObservabilityEvent =
|
|
35
|
+
| BaseEvent<
|
|
36
|
+
"mcp:client:preconnect",
|
|
37
|
+
{
|
|
38
|
+
serverId: string;
|
|
39
|
+
}
|
|
40
|
+
>
|
|
41
|
+
| BaseEvent<
|
|
42
|
+
"mcp:client:connect",
|
|
43
|
+
{
|
|
44
|
+
url: string;
|
|
45
|
+
transport: string;
|
|
46
|
+
state: string;
|
|
47
|
+
error?: string;
|
|
48
|
+
}
|
|
49
|
+
>
|
|
50
|
+
| BaseEvent<
|
|
51
|
+
"mcp:client:authorize",
|
|
52
|
+
{
|
|
53
|
+
serverId: string;
|
|
54
|
+
authUrl: string;
|
|
55
|
+
clientId?: string;
|
|
56
|
+
}
|
|
57
|
+
>
|
|
58
|
+
| BaseEvent<"mcp:client:discover", {}>;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { BaseEvent, MCPObservabilityEvent };
|
|
61
|
+
//# sourceMappingURL=mcp-BEwaCsxO.d.ts.map
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import '@modelcontextprotocol/sdk/types.js';
|
|
5
|
-
import 'partyserver';
|
|
6
|
-
import '../mcp/client.js';
|
|
7
|
-
import 'zod';
|
|
8
|
-
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
9
|
-
import '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
10
|
-
import '../mcp/do-oauth-client-provider.js';
|
|
11
|
-
import '@modelcontextprotocol/sdk/client/auth.js';
|
|
12
|
-
import '@modelcontextprotocol/sdk/shared/auth.js';
|
|
1
|
+
import "../mcp-BEwaCsxO.js";
|
|
2
|
+
import { Observability, ObservabilityEvent, genericObservability } from "../index-W4JUkafc.js";
|
|
3
|
+
export { Observability, ObservabilityEvent, genericObservability };
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import "../
|
|
5
|
-
import "../
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
genericObservability
|
|
9
|
-
};
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import "../ai-types-UZlfLOYP.js";
|
|
2
|
+
import "../client-DjR-lC16.js";
|
|
3
|
+
import "../client-CZBVDDoO.js";
|
|
4
|
+
import "../do-oauth-client-provider-B2jr6UNq.js";
|
|
5
|
+
import { genericObservability } from "../src-COfG--3R.js";
|
|
6
|
+
|
|
7
|
+
export { genericObservability };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Agent, MCPServersState } from "./index-CkQU40oY.js";
|
|
2
|
+
import { Method, RPCMethod } from "./serializable-gtr9YMhp.js";
|
|
3
|
+
import { StreamOptions } from "./client-CmMi85Sj.js";
|
|
4
|
+
import { PartySocket } from "partysocket";
|
|
5
|
+
import { usePartySocket } from "partysocket/react";
|
|
6
|
+
|
|
7
|
+
//#region src/react.d.ts
|
|
8
|
+
type QueryObject = Record<string, string | null>;
|
|
9
|
+
/**
|
|
10
|
+
* Options for the useAgent hook
|
|
11
|
+
* @template State Type of the Agent's state
|
|
12
|
+
*/
|
|
13
|
+
type UseAgentOptions<State = unknown> = Omit<
|
|
14
|
+
Parameters<typeof usePartySocket>[0],
|
|
15
|
+
"party" | "room" | "query"
|
|
16
|
+
> & {
|
|
17
|
+
/** Name of the agent to connect to */
|
|
18
|
+
agent: string;
|
|
19
|
+
/** Name of the specific Agent instance */
|
|
20
|
+
name?: string;
|
|
21
|
+
/** Query parameters - can be static object or async function */
|
|
22
|
+
query?: QueryObject | (() => Promise<QueryObject>);
|
|
23
|
+
/** Dependencies for async query caching */
|
|
24
|
+
queryDeps?: unknown[];
|
|
25
|
+
/** Cache TTL in milliseconds for auth tokens/time-sensitive data */
|
|
26
|
+
cacheTtl?: number;
|
|
27
|
+
/** Called when the Agent's state is updated */
|
|
28
|
+
onStateUpdate?: (state: State, source: "server" | "client") => void;
|
|
29
|
+
/** Called when MCP server state is updated */
|
|
30
|
+
onMcpUpdate?: (mcpServers: MCPServersState) => void;
|
|
31
|
+
};
|
|
32
|
+
type AllOptional<T> = T extends [infer A, ...infer R]
|
|
33
|
+
? undefined extends A
|
|
34
|
+
? AllOptional<R>
|
|
35
|
+
: false
|
|
36
|
+
: true;
|
|
37
|
+
type RPCMethods<T> = {
|
|
38
|
+
[K in keyof T as T[K] extends RPCMethod<T[K]> ? K : never]: RPCMethod<T[K]>;
|
|
39
|
+
};
|
|
40
|
+
type OptionalParametersMethod<T extends RPCMethod> =
|
|
41
|
+
AllOptional<Parameters<T>> extends true ? T : never;
|
|
42
|
+
type AgentMethods<T> = Omit<RPCMethods<T>, keyof Agent<any, any>>;
|
|
43
|
+
type OptionalAgentMethods<T> = {
|
|
44
|
+
[K in keyof AgentMethods<T> as AgentMethods<T>[K] extends OptionalParametersMethod<
|
|
45
|
+
AgentMethods<T>[K]
|
|
46
|
+
>
|
|
47
|
+
? K
|
|
48
|
+
: never]: OptionalParametersMethod<AgentMethods<T>[K]>;
|
|
49
|
+
};
|
|
50
|
+
type RequiredAgentMethods<T> = Omit<
|
|
51
|
+
AgentMethods<T>,
|
|
52
|
+
keyof OptionalAgentMethods<T>
|
|
53
|
+
>;
|
|
54
|
+
type AgentPromiseReturnType<T, K extends keyof AgentMethods<T>> =
|
|
55
|
+
ReturnType<AgentMethods<T>[K]> extends Promise<any>
|
|
56
|
+
? ReturnType<AgentMethods<T>[K]>
|
|
57
|
+
: Promise<ReturnType<AgentMethods<T>[K]>>;
|
|
58
|
+
type OptionalArgsAgentMethodCall<AgentT> = <
|
|
59
|
+
K extends keyof OptionalAgentMethods<AgentT>
|
|
60
|
+
>(
|
|
61
|
+
method: K,
|
|
62
|
+
args?: Parameters<OptionalAgentMethods<AgentT>[K]>,
|
|
63
|
+
streamOptions?: StreamOptions
|
|
64
|
+
) => AgentPromiseReturnType<AgentT, K>;
|
|
65
|
+
type RequiredArgsAgentMethodCall<AgentT> = <
|
|
66
|
+
K extends keyof RequiredAgentMethods<AgentT>
|
|
67
|
+
>(
|
|
68
|
+
method: K,
|
|
69
|
+
args: Parameters<RequiredAgentMethods<AgentT>[K]>,
|
|
70
|
+
streamOptions?: StreamOptions
|
|
71
|
+
) => AgentPromiseReturnType<AgentT, K>;
|
|
72
|
+
type AgentMethodCall<AgentT> = OptionalArgsAgentMethodCall<AgentT> &
|
|
73
|
+
RequiredArgsAgentMethodCall<AgentT>;
|
|
74
|
+
type UntypedAgentMethodCall = <T = unknown>(
|
|
75
|
+
method: string,
|
|
76
|
+
args?: unknown[],
|
|
77
|
+
streamOptions?: StreamOptions
|
|
78
|
+
) => Promise<T>;
|
|
79
|
+
type AgentStub<T> = {
|
|
80
|
+
[K in keyof AgentMethods<T>]: (
|
|
81
|
+
...args: Parameters<AgentMethods<T>[K]>
|
|
82
|
+
) => AgentPromiseReturnType<AgentMethods<T>, K>;
|
|
83
|
+
};
|
|
84
|
+
type UntypedAgentStub = Record<string, Method>;
|
|
85
|
+
/**
|
|
86
|
+
* React hook for connecting to an Agent
|
|
87
|
+
*/
|
|
88
|
+
declare function useAgent<State = unknown>(
|
|
89
|
+
options: UseAgentOptions<State>
|
|
90
|
+
): PartySocket & {
|
|
91
|
+
agent: string;
|
|
92
|
+
name: string;
|
|
93
|
+
setState: (state: State) => void;
|
|
94
|
+
call: UntypedAgentMethodCall;
|
|
95
|
+
stub: UntypedAgentStub;
|
|
96
|
+
};
|
|
97
|
+
declare function useAgent<
|
|
98
|
+
AgentT extends {
|
|
99
|
+
get state(): State;
|
|
100
|
+
},
|
|
101
|
+
State
|
|
102
|
+
>(
|
|
103
|
+
options: UseAgentOptions<State>
|
|
104
|
+
): PartySocket & {
|
|
105
|
+
agent: string;
|
|
106
|
+
name: string;
|
|
107
|
+
setState: (state: State) => void;
|
|
108
|
+
call: AgentMethodCall<AgentT>;
|
|
109
|
+
stub: AgentStub<AgentT>;
|
|
110
|
+
};
|
|
111
|
+
//#endregion
|
|
112
|
+
export { UseAgentOptions, useAgent };
|
|
113
|
+
//# sourceMappingURL=react-B4e1rDid.d.ts.map
|
package/dist/react.d.ts
CHANGED
|
@@ -1,119 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import "
|
|
7
|
-
import "
|
|
8
|
-
import "
|
|
9
|
-
import "./
|
|
10
|
-
|
|
11
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
12
|
-
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
13
|
-
import "ai";
|
|
14
|
-
import "./mcp/do-oauth-client-provider.js";
|
|
15
|
-
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
16
|
-
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Options for the useAgent hook
|
|
20
|
-
* @template State Type of the Agent's state
|
|
21
|
-
*/
|
|
22
|
-
type UseAgentOptions<State = unknown> = Omit<
|
|
23
|
-
Parameters<typeof usePartySocket>[0],
|
|
24
|
-
"party" | "room"
|
|
25
|
-
> & {
|
|
26
|
-
/** Name of the agent to connect to */
|
|
27
|
-
agent: string;
|
|
28
|
-
/** Name of the specific Agent instance */
|
|
29
|
-
name?: string;
|
|
30
|
-
/** Called when the Agent's state is updated */
|
|
31
|
-
onStateUpdate?: (state: State, source: "server" | "client") => void;
|
|
32
|
-
/** Called when MCP server state is updated */
|
|
33
|
-
onMcpUpdate?: (mcpServers: MCPServersState) => void;
|
|
34
|
-
};
|
|
35
|
-
type AllOptional<T> = T extends [infer A, ...infer R]
|
|
36
|
-
? undefined extends A
|
|
37
|
-
? AllOptional<R>
|
|
38
|
-
: false
|
|
39
|
-
: true;
|
|
40
|
-
type RPCMethods<T> = {
|
|
41
|
-
[K in keyof T as T[K] extends RPCMethod<T[K]> ? K : never]: RPCMethod<T[K]>;
|
|
42
|
-
};
|
|
43
|
-
type OptionalParametersMethod<T extends RPCMethod> =
|
|
44
|
-
AllOptional<Parameters<T>> extends true ? T : never;
|
|
45
|
-
type AgentMethods<T> = Omit<RPCMethods<T>, keyof Agent<any, any>>;
|
|
46
|
-
type OptionalAgentMethods<T> = {
|
|
47
|
-
[K in keyof AgentMethods<T> as AgentMethods<T>[K] extends OptionalParametersMethod<
|
|
48
|
-
AgentMethods<T>[K]
|
|
49
|
-
>
|
|
50
|
-
? K
|
|
51
|
-
: never]: OptionalParametersMethod<AgentMethods<T>[K]>;
|
|
52
|
-
};
|
|
53
|
-
type RequiredAgentMethods<T> = Omit<
|
|
54
|
-
AgentMethods<T>,
|
|
55
|
-
keyof OptionalAgentMethods<T>
|
|
56
|
-
>;
|
|
57
|
-
type AgentPromiseReturnType<T, K extends keyof AgentMethods<T>> =
|
|
58
|
-
ReturnType<AgentMethods<T>[K]> extends Promise<any>
|
|
59
|
-
? ReturnType<AgentMethods<T>[K]>
|
|
60
|
-
: Promise<ReturnType<AgentMethods<T>[K]>>;
|
|
61
|
-
type OptionalArgsAgentMethodCall<AgentT> = <
|
|
62
|
-
K extends keyof OptionalAgentMethods<AgentT>
|
|
63
|
-
>(
|
|
64
|
-
method: K,
|
|
65
|
-
args?: Parameters<OptionalAgentMethods<AgentT>[K]>,
|
|
66
|
-
streamOptions?: StreamOptions
|
|
67
|
-
) => AgentPromiseReturnType<AgentT, K>;
|
|
68
|
-
type RequiredArgsAgentMethodCall<AgentT> = <
|
|
69
|
-
K extends keyof RequiredAgentMethods<AgentT>
|
|
70
|
-
>(
|
|
71
|
-
method: K,
|
|
72
|
-
args: Parameters<RequiredAgentMethods<AgentT>[K]>,
|
|
73
|
-
streamOptions?: StreamOptions
|
|
74
|
-
) => AgentPromiseReturnType<AgentT, K>;
|
|
75
|
-
type AgentMethodCall<AgentT> = OptionalArgsAgentMethodCall<AgentT> &
|
|
76
|
-
RequiredArgsAgentMethodCall<AgentT>;
|
|
77
|
-
type UntypedAgentMethodCall = <T = unknown>(
|
|
78
|
-
method: string,
|
|
79
|
-
args?: unknown[],
|
|
80
|
-
streamOptions?: StreamOptions
|
|
81
|
-
) => Promise<T>;
|
|
82
|
-
type AgentStub<T> = {
|
|
83
|
-
[K in keyof AgentMethods<T>]: (
|
|
84
|
-
...args: Parameters<AgentMethods<T>[K]>
|
|
85
|
-
) => AgentPromiseReturnType<AgentMethods<T>, K>;
|
|
86
|
-
};
|
|
87
|
-
type UntypedAgentStub = Record<string, Method>;
|
|
88
|
-
/**
|
|
89
|
-
* React hook for connecting to an Agent
|
|
90
|
-
* @template State Type of the Agent's state
|
|
91
|
-
* @template Agent Type of the Agent
|
|
92
|
-
* @param options Connection options
|
|
93
|
-
* @returns WebSocket connection with setState and call methods
|
|
94
|
-
*/
|
|
95
|
-
declare function useAgent<State = unknown>(
|
|
96
|
-
options: UseAgentOptions<State>
|
|
97
|
-
): PartySocket & {
|
|
98
|
-
agent: string;
|
|
99
|
-
name: string;
|
|
100
|
-
setState: (state: State) => void;
|
|
101
|
-
call: UntypedAgentMethodCall;
|
|
102
|
-
stub: UntypedAgentStub;
|
|
103
|
-
};
|
|
104
|
-
declare function useAgent<
|
|
105
|
-
AgentT extends {
|
|
106
|
-
get state(): State;
|
|
107
|
-
},
|
|
108
|
-
State
|
|
109
|
-
>(
|
|
110
|
-
options: UseAgentOptions<State>
|
|
111
|
-
): PartySocket & {
|
|
112
|
-
agent: string;
|
|
113
|
-
name: string;
|
|
114
|
-
setState: (state: State) => void;
|
|
115
|
-
call: AgentMethodCall<AgentT>;
|
|
116
|
-
stub: AgentStub<AgentT>;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export { type UseAgentOptions, useAgent };
|
|
1
|
+
import "./client-C-nwz-3N.js";
|
|
2
|
+
import "./mcp-BEwaCsxO.js";
|
|
3
|
+
import "./do-oauth-client-provider-CCwGwnrA.js";
|
|
4
|
+
import "./index-W4JUkafc.js";
|
|
5
|
+
import "./ai-types-BWW4umHY.js";
|
|
6
|
+
import "./index-CkQU40oY.js";
|
|
7
|
+
import "./serializable-gtr9YMhp.js";
|
|
8
|
+
import "./client-CmMi85Sj.js";
|
|
9
|
+
import { UseAgentOptions, useAgent } from "./react-B4e1rDid.js";
|
|
10
|
+
export { UseAgentOptions, useAgent };
|
package/dist/react.js
CHANGED
|
@@ -1,116 +1,189 @@
|
|
|
1
|
-
|
|
1
|
+
import { MessageType } from "./ai-types-UZlfLOYP.js";
|
|
2
|
+
import { use, useCallback, useEffect, useMemo, useRef } from "react";
|
|
2
3
|
import { usePartySocket } from "partysocket/react";
|
|
3
|
-
|
|
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
|
+
*/
|
|
4
11
|
function camelCaseToKebabCase(str) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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(/-$/, "");
|
|
16
|
+
}
|
|
17
|
+
const queryCache = /* @__PURE__ */ new Map();
|
|
18
|
+
function arraysEqual(a, b) {
|
|
19
|
+
if (a === b) return true;
|
|
20
|
+
if (a.length !== b.length) return false;
|
|
21
|
+
for (let i = 0; i < a.length; i++) if (!Object.is(a[i], b[i])) return false;
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function findCacheEntry(targetKey) {
|
|
25
|
+
for (const [existingKey, entry] of queryCache.entries()) if (arraysEqual(existingKey, targetKey)) {
|
|
26
|
+
if (Date.now() > entry.expiresAt) {
|
|
27
|
+
queryCache.delete(existingKey);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
entry.refCount++;
|
|
31
|
+
return entry.promise;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function setCacheEntry(key, value, cacheTtl) {
|
|
35
|
+
for (const [existingKey] of queryCache.entries()) if (arraysEqual(existingKey, key)) {
|
|
36
|
+
queryCache.delete(existingKey);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
const expiresAt = cacheTtl ? Date.now() + cacheTtl : Date.now() + 300 * 1e3;
|
|
40
|
+
queryCache.set(key, {
|
|
41
|
+
promise: value,
|
|
42
|
+
refCount: 1,
|
|
43
|
+
expiresAt,
|
|
44
|
+
cacheTtl
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function decrementCacheEntry(targetKey) {
|
|
48
|
+
for (const [existingKey, entry] of queryCache.entries()) if (arraysEqual(existingKey, targetKey)) {
|
|
49
|
+
entry.refCount--;
|
|
50
|
+
if (entry.refCount <= 0) queryCache.delete(existingKey);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
function createCacheKey(agentNamespace, name, deps) {
|
|
56
|
+
return [
|
|
57
|
+
agentNamespace,
|
|
58
|
+
name || "default",
|
|
59
|
+
...deps
|
|
60
|
+
];
|
|
14
61
|
}
|
|
15
62
|
function useAgent(options) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
63
|
+
const agentNamespace = camelCaseToKebabCase(options.agent);
|
|
64
|
+
const { query, queryDeps, cacheTtl,...restOptions } = options;
|
|
65
|
+
const pendingCallsRef = useRef(/* @__PURE__ */ new Map());
|
|
66
|
+
const cacheKey = useMemo(() => {
|
|
67
|
+
const deps = queryDeps || [];
|
|
68
|
+
return createCacheKey(agentNamespace, options.name, deps);
|
|
69
|
+
}, [
|
|
70
|
+
agentNamespace,
|
|
71
|
+
options.name,
|
|
72
|
+
queryDeps
|
|
73
|
+
]);
|
|
74
|
+
const queryPromise = useMemo(() => {
|
|
75
|
+
if (!query || typeof query !== "function") return null;
|
|
76
|
+
const existingPromise = findCacheEntry(cacheKey);
|
|
77
|
+
if (existingPromise) return existingPromise;
|
|
78
|
+
const promise = query().catch((error) => {
|
|
79
|
+
console.error(`[useAgent] Query failed for agent "${options.agent}":`, error);
|
|
80
|
+
decrementCacheEntry(cacheKey);
|
|
81
|
+
throw error;
|
|
82
|
+
});
|
|
83
|
+
setCacheEntry(cacheKey, promise, cacheTtl);
|
|
84
|
+
return promise;
|
|
85
|
+
}, [
|
|
86
|
+
cacheKey,
|
|
87
|
+
query,
|
|
88
|
+
options.agent,
|
|
89
|
+
cacheTtl
|
|
90
|
+
]);
|
|
91
|
+
let resolvedQuery;
|
|
92
|
+
if (query) if (typeof query === "function") {
|
|
93
|
+
const queryResult = use(queryPromise);
|
|
94
|
+
if (queryResult) {
|
|
95
|
+
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.`);
|
|
96
|
+
resolvedQuery = queryResult;
|
|
97
|
+
}
|
|
98
|
+
} else resolvedQuery = query;
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
return () => {
|
|
101
|
+
if (queryPromise) decrementCacheEntry(cacheKey);
|
|
102
|
+
};
|
|
103
|
+
}, [cacheKey, queryPromise]);
|
|
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;
|
|
112
185
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
};
|
|
186
|
+
|
|
187
|
+
//#endregion
|
|
188
|
+
export { useAgent };
|
|
116
189
|
//# sourceMappingURL=react.js.map
|