@economic/agents-react 1.7.0 → 2.0.0-alpha.38489283394.1.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/README.md +1 -1
- package/dist/index.d.mts +44 -6
- package/dist/index.mjs +55 -16
- package/dist/v1.d.mts +4 -6
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -212,4 +212,4 @@ The package also exports the handler types `AssistantActions`, `GetChatsHandler`
|
|
|
212
212
|
|
|
213
213
|
## Authentication
|
|
214
214
|
|
|
215
|
-
Pass a JWT as `authToken`. It is sent both as an `Authorization: Bearer …` header and as a WebSocket subprotocol (`["bearer", token]`), matching the server-side [`
|
|
215
|
+
Pass a JWT as `authToken`. It is sent both as an `Authorization: Bearer …` header and as a WebSocket subprotocol (`["bearer", token]`), matching the server-side [`getAuthenticator(env)`](../agents/README.md#authentication). When verification fails, the connection status becomes `"unauthorized"`.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useAgent as useAgent$1 } from "agents/react";
|
|
2
2
|
import { getToolApproval, getToolCallId, getToolPartState, useAgentChat } from "@cloudflare/ai-chat/react";
|
|
3
|
-
|
|
3
|
+
import { UIMessage } from "ai";
|
|
4
4
|
//#region src/types.d.ts
|
|
5
5
|
type _CfAgent = ReturnType<typeof useAgent$1>;
|
|
6
6
|
type Agent = Omit<_CfAgent, "call"> & {
|
|
@@ -10,7 +10,8 @@ type Chat = ReturnType<typeof useAgentChat>;
|
|
|
10
10
|
type AgentConnectionStatus = "connecting" | "reconnecting" | "connected" | "disconnected" | "unauthorized" | "error";
|
|
11
11
|
type AgentConnectionType = "agent" | "chat" | "assistant";
|
|
12
12
|
type ChatSummary = {
|
|
13
|
-
name: string;
|
|
13
|
+
name: string;
|
|
14
|
+
/** True when this entry only represents the active, unregistered chat facet. */
|
|
14
15
|
isTemporary?: boolean;
|
|
15
16
|
title?: string;
|
|
16
17
|
summary?: string;
|
|
@@ -38,6 +39,16 @@ type CreateChatHandler = () => Promise<string>;
|
|
|
38
39
|
type OpenChatHandler = (chatId: string) => void;
|
|
39
40
|
type DeleteChatHandler = (chatId: string) => Promise<void>;
|
|
40
41
|
type RenameChatHandler = (chatId: string, title: string) => Promise<void>;
|
|
42
|
+
/** How an agent's `run` ended: with its answer, waiting for an approval, or without one. */
|
|
43
|
+
type RunResult = {
|
|
44
|
+
status: "completed";
|
|
45
|
+
output: unknown;
|
|
46
|
+
} | {
|
|
47
|
+
status: "waiting";
|
|
48
|
+
} | {
|
|
49
|
+
status: "error" | "skipped" | "aborted";
|
|
50
|
+
error?: string;
|
|
51
|
+
};
|
|
41
52
|
//#endregion
|
|
42
53
|
//#region src/useAgent.d.ts
|
|
43
54
|
interface UseAgentReturn {
|
|
@@ -61,7 +72,7 @@ interface UseAgentOptions {
|
|
|
61
72
|
onError?: (event: ErrorEvent) => void;
|
|
62
73
|
onMessage?: (event: MessageEvent) => void;
|
|
63
74
|
}
|
|
64
|
-
declare function useAgent(options: UseAgentOptions): UseAgentReturn;
|
|
75
|
+
export declare function useAgent(options: UseAgentOptions): UseAgentReturn;
|
|
65
76
|
//#endregion
|
|
66
77
|
//#region src/useChat.d.ts
|
|
67
78
|
interface UseChatReturn {
|
|
@@ -79,7 +90,7 @@ interface UseChatOptions<ToolContext extends Record<string, unknown>> extends Us
|
|
|
79
90
|
/** Include internal SDK messages such as compaction summaries. Defaults to false. */
|
|
80
91
|
showInternalMessages?: boolean;
|
|
81
92
|
}
|
|
82
|
-
declare function useChat<ToolContext extends Record<string, unknown>>(options: UseChatOptions<ToolContext>): UseChatReturn;
|
|
93
|
+
export declare function useChat<ToolContext extends Record<string, unknown>>(options: UseChatOptions<ToolContext>): UseChatReturn;
|
|
83
94
|
//#endregion
|
|
84
95
|
//#region src/useAssistant.d.ts
|
|
85
96
|
interface Assistant {
|
|
@@ -100,6 +111,33 @@ interface UseAssistantReturn {
|
|
|
100
111
|
interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> extends Omit<UseChatOptions<ToolContext>, "actorId" | "getInitialMessages"> {
|
|
101
112
|
initialChatId?: string;
|
|
102
113
|
}
|
|
103
|
-
declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): UseAssistantReturn;
|
|
114
|
+
export declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): UseAssistantReturn;
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/useTask.d.ts
|
|
117
|
+
interface UseTaskOptions<ToolContext extends Record<string, unknown>> extends Omit<UseAgentOptions, "name" | "enabled"> {
|
|
118
|
+
/** The agent's name for one run, from that run's new ID. */
|
|
119
|
+
name: (runId: string) => string;
|
|
120
|
+
toolContext?: ToolContext;
|
|
121
|
+
}
|
|
122
|
+
interface UseTaskReturn {
|
|
123
|
+
/** Starts a new run on `input`, apart from any earlier one. */
|
|
124
|
+
run: (input: unknown) => void;
|
|
125
|
+
/** The current run's ID, once one has started. */
|
|
126
|
+
runId: string | undefined;
|
|
127
|
+
/** The current run's messages as they stream: its input, tool calls and answer. */
|
|
128
|
+
messages: UIMessage[];
|
|
129
|
+
/** How the current run ended, once it has. */
|
|
130
|
+
result: RunResult | undefined;
|
|
131
|
+
running: boolean;
|
|
132
|
+
status: AgentConnectionStatus;
|
|
133
|
+
agent: Agent;
|
|
134
|
+
/** The run's chat, to answer its tool approvals. */
|
|
135
|
+
chat: Chat;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Runs an agent once per input, each run on an agent of its own, so runs
|
|
139
|
+
* don't share history. Nothing is loaded from earlier runs.
|
|
140
|
+
*/
|
|
141
|
+
export declare function useTask<ToolContext extends Record<string, unknown>>(options: UseTaskOptions<ToolContext>): UseTaskReturn;
|
|
104
142
|
//#endregion
|
|
105
|
-
export { type Agent, type AgentConnectionStatus, type AgentConnectionType, type Assistant, type Chat, type ChatSummary, type CreateChatHandler, type DeleteChatHandler, type GetChatsHandler, type GetInvocableSkillsHandler, type GetMessageFeedbackHandler, type InvocableSkill, type MessageFeedback, type MessageFeedbackHandler, type OpenChatHandler, type RenameChatHandler, type UseAgentReturn, type UseAssistantReturn, type UseChatReturn,
|
|
143
|
+
export { type Agent, type AgentConnectionStatus, type AgentConnectionType, type Assistant, type Chat, type ChatSummary, type CreateChatHandler, type DeleteChatHandler, type GetChatsHandler, type GetInvocableSkillsHandler, type GetMessageFeedbackHandler, type InvocableSkill, type MessageFeedback, type MessageFeedbackHandler, type OpenChatHandler, type RenameChatHandler, type RunResult, type UseAgentReturn, type UseAssistantReturn, type UseChatReturn, type UseTaskOptions, type UseTaskReturn, getToolApproval, getToolCallId, getToolPartState };
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,6 @@ import { n as visibleChatMessages, t as getTokenArgs } from "./util-tBvVuxIJ.mjs
|
|
|
2
2
|
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { useAgent as useAgent$1 } from "agents/react";
|
|
4
4
|
import { getToolApproval, getToolCallId, getToolPartState, useAgentChat } from "@cloudflare/ai-chat/react";
|
|
5
|
-
import { agentFetch } from "agents/client";
|
|
6
5
|
//#region src/useAgent.ts
|
|
7
6
|
function getAgentConnectionStatus(state, connectionId) {
|
|
8
7
|
return state.connectionId === connectionId ? state.status : "connecting";
|
|
@@ -204,19 +203,6 @@ function useAgent(options) {
|
|
|
204
203
|
}
|
|
205
204
|
//#endregion
|
|
206
205
|
//#region src/useChat.ts
|
|
207
|
-
async function fetchInvocableSkills(agent) {
|
|
208
|
-
const target = getAgentSocketTarget(agent);
|
|
209
|
-
if (!target) throw new Error("Cannot determine the connected agent");
|
|
210
|
-
const path = target.subPath ? `${target.subPath}/getInvocableSkills` : "getInvocableSkills";
|
|
211
|
-
const response = await agentFetch({
|
|
212
|
-
agent: target.agentName,
|
|
213
|
-
host: target.host,
|
|
214
|
-
name: target.name,
|
|
215
|
-
path
|
|
216
|
-
}, { headers: target.authToken ? { Authorization: `Bearer ${target.authToken}` } : void 0 });
|
|
217
|
-
if (!response.ok) throw new Error(`Failed to get invocable skills (${response.status})`);
|
|
218
|
-
return response.json();
|
|
219
|
-
}
|
|
220
206
|
function useChat(options) {
|
|
221
207
|
const { toolContext, welcomeMessage, showInternalMessages = false, ...agentOptions } = options;
|
|
222
208
|
const { agent, status } = useAgent({
|
|
@@ -226,9 +212,10 @@ function useChat(options) {
|
|
|
226
212
|
const chat = useAgentChat({
|
|
227
213
|
agent,
|
|
228
214
|
body: toolContext ?? {},
|
|
215
|
+
headers: agentOptions.authToken ? { Authorization: `Bearer ${agentOptions.authToken}` } : void 0,
|
|
229
216
|
getInitialMessages: !!agentOptions.name && status === "connected" ? void 0 : null
|
|
230
217
|
});
|
|
231
|
-
const getInvocableSkills = useCallback(() =>
|
|
218
|
+
const getInvocableSkills = useCallback(async () => await agent.call("getInvocableSkills"), [agent]);
|
|
232
219
|
const hasSentWelcome = useRef(false);
|
|
233
220
|
useEffect(() => {
|
|
234
221
|
if (welcomeMessage && status === "connected" && chat.messages.length === 0 && !hasSentWelcome.current) {
|
|
@@ -405,4 +392,56 @@ function useAssistant(options) {
|
|
|
405
392
|
};
|
|
406
393
|
}
|
|
407
394
|
//#endregion
|
|
408
|
-
|
|
395
|
+
//#region src/useTask.ts
|
|
396
|
+
/**
|
|
397
|
+
* Runs an agent once per input, each run on an agent of its own, so runs
|
|
398
|
+
* don't share history. Nothing is loaded from earlier runs.
|
|
399
|
+
*/
|
|
400
|
+
function useTask(options) {
|
|
401
|
+
const { name, toolContext, ...agentOptions } = options;
|
|
402
|
+
const [run, setRun] = useState();
|
|
403
|
+
const [result, setResult] = useState();
|
|
404
|
+
const started = useRef(void 0);
|
|
405
|
+
const { agent, status } = useAgent({
|
|
406
|
+
...agentOptions,
|
|
407
|
+
name: run ? name(run.id) : "",
|
|
408
|
+
enabled: !!run
|
|
409
|
+
});
|
|
410
|
+
const chat = useAgentChat({
|
|
411
|
+
agent,
|
|
412
|
+
getInitialMessages: null
|
|
413
|
+
});
|
|
414
|
+
useEffect(() => {
|
|
415
|
+
if (!run || status !== "connected" || started.current === run.id) return;
|
|
416
|
+
started.current = run.id;
|
|
417
|
+
agent.call("run", [run.input, toolContext]).catch((error) => ({
|
|
418
|
+
status: "error",
|
|
419
|
+
error: error instanceof Error ? error.message : String(error)
|
|
420
|
+
})).then((ended) => setResult({
|
|
421
|
+
runId: run.id,
|
|
422
|
+
result: ended
|
|
423
|
+
}));
|
|
424
|
+
}, [
|
|
425
|
+
agent,
|
|
426
|
+
run,
|
|
427
|
+
status,
|
|
428
|
+
toolContext
|
|
429
|
+
]);
|
|
430
|
+
const start = useCallback((input) => setRun({
|
|
431
|
+
id: crypto.randomUUID(),
|
|
432
|
+
input
|
|
433
|
+
}), []);
|
|
434
|
+
const current = run && result?.runId === run.id ? result.result : void 0;
|
|
435
|
+
return {
|
|
436
|
+
run: start,
|
|
437
|
+
runId: run?.id,
|
|
438
|
+
messages: run ? visibleChatMessages(chat.messages, false) : [],
|
|
439
|
+
result: current,
|
|
440
|
+
running: !!run && !current && (status === "connecting" || status === "reconnecting" || status === "connected"),
|
|
441
|
+
status,
|
|
442
|
+
agent,
|
|
443
|
+
chat
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
//#endregion
|
|
447
|
+
export { getToolApproval, getToolCallId, getToolPartState, useAgent, useAssistant, useChat, useTask };
|
package/dist/v1.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { useAgent } from "agents/react";
|
|
2
2
|
import { useAgentChat } from "@cloudflare/ai-chat/react";
|
|
3
|
-
|
|
4
3
|
//#region src/v1/index.d.ts
|
|
5
|
-
type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
|
|
6
|
-
interface UseAIChatAgentOptions {
|
|
4
|
+
export type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
|
|
5
|
+
export interface UseAIChatAgentOptions {
|
|
7
6
|
agent: string;
|
|
8
7
|
host: string;
|
|
9
8
|
basePath?: string;
|
|
@@ -27,6 +26,5 @@ type UseAIChatAgentResult = {
|
|
|
27
26
|
chat: ReturnType<typeof useAgentChat>;
|
|
28
27
|
connectionStatus: AgentConnectionStatus;
|
|
29
28
|
};
|
|
30
|
-
declare function useAIChatAgent(options: UseAIChatAgentOptions): UseAIChatAgentResult;
|
|
31
|
-
//#endregion
|
|
32
|
-
export { AgentConnectionStatus, UseAIChatAgentOptions, useAIChatAgent };
|
|
29
|
+
export declare function useAIChatAgent(options: UseAIChatAgentOptions): UseAIChatAgentResult;
|
|
30
|
+
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@economic/agents-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.38489283394.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -20,18 +20,18 @@
|
|
|
20
20
|
"prepublishOnly": "npm run build"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@ai-sdk/react": "^
|
|
24
|
-
"@cloudflare/ai-chat": "^0.
|
|
25
|
-
"agents": "^0.
|
|
26
|
-
"ai": "^
|
|
27
|
-
"zod": "^4.
|
|
23
|
+
"@ai-sdk/react": "^4.0.140",
|
|
24
|
+
"@cloudflare/ai-chat": "^0.12.1",
|
|
25
|
+
"agents": "^0.28.0",
|
|
26
|
+
"ai": "^7.0.137",
|
|
27
|
+
"zod": "^4.6.5"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@cloudflare/workers-types": "^
|
|
31
|
-
"@types/node": "^
|
|
32
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
33
|
-
"tsdown": "^0.
|
|
34
|
-
"typescript": "^
|
|
30
|
+
"@cloudflare/workers-types": "^5.20261009.1",
|
|
31
|
+
"@types/node": "^26.6.4",
|
|
32
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
33
|
+
"tsdown": "^0.23.0",
|
|
34
|
+
"typescript": "^7.0.2",
|
|
35
35
|
"vitest": "^4.1.4"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|