agents 0.0.0-c3e8618 → 0.0.0-c8f53b8
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/ai-chat-agent.d.ts +47 -23
- package/dist/ai-chat-agent.js +234 -0
- package/dist/ai-chat-agent.js.map +1 -0
- package/dist/ai-react.d.ts +72 -44
- package/dist/ai-react.js +204 -0
- package/dist/ai-react.js.map +1 -0
- package/dist/ai-types.d.ts +65 -40
- package/dist/ai-types.js +1 -0
- package/dist/ai-types.js.map +1 -0
- package/dist/chunk-HD4VEHBA.js +608 -0
- package/dist/chunk-HD4VEHBA.js.map +1 -0
- package/dist/chunk-HMLY7DHA.js +16 -0
- package/dist/chunk-HMLY7DHA.js.map +1 -0
- package/dist/chunk-Q5ZBHY4Z.js +456 -0
- package/dist/chunk-Q5ZBHY4Z.js.map +1 -0
- package/dist/client.d.ts +57 -37
- package/dist/client.js +131 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +234 -179
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +138 -36
- package/dist/mcp/client.js +10 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +107 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +39 -6
- package/dist/mcp/index.js +809 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/react.d.ts +24 -15
- package/dist/react.js +104 -0
- package/dist/react.js.map +1 -0
- package/dist/schedule.d.ts +30 -20
- package/dist/schedule.js +73 -0
- package/dist/schedule.js.map +1 -0
- package/package.json +25 -5
- package/src/index.ts +38 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ai-react.tsx"],"sourcesContent":["import { useChat } from \"@ai-sdk/react\";\nimport type { Message } from \"ai\";\nimport { use, useEffect } from \"react\";\nimport type { OutgoingMessage } from \"./ai-types\";\nimport type { useAgent } from \"./react\";\nimport { nanoid } from \"nanoid\";\n\ntype GetInitialMessagesOptions = {\n agent: string;\n name: string;\n url: string;\n};\n\n/**\n * Options for the useAgentChat hook\n */\ntype UseAgentChatOptions<State> = Omit<\n Parameters<typeof useChat>[0] & {\n /** Agent connection from useAgent */\n agent: ReturnType<typeof useAgent<State>>;\n getInitialMessages?:\n | undefined\n | null\n // | (() => Message[])\n | ((options: GetInitialMessagesOptions) => Promise<Message[]>);\n },\n \"fetch\"\n>;\n\nconst requestCache = new Map<string, Promise<Message[]>>();\n\n/**\n * React hook for building AI chat interfaces using an Agent\n * @param options Chat options including the agent connection\n * @returns Chat interface controls and state with added clearHistory method\n */\nexport function useAgentChat<State = unknown>(\n options: UseAgentChatOptions<State>\n) {\n const { agent, getInitialMessages, ...rest } = options;\n\n const agentUrl = new URL(\n `${// @ts-expect-error we're using a protected _url property that includes query params\n ((agent._url as string | null) || agent._pkurl)\n ?.replace(\"ws://\", \"http://\")\n .replace(\"wss://\", \"https://\")}`\n );\n\n // delete the _pk query param\n agentUrl.searchParams.delete(\"_pk\");\n const agentUrlString = agentUrl.toString();\n\n async function defaultGetInitialMessagesFetch({\n url,\n }: GetInitialMessagesOptions) {\n const getMessagesUrl = new URL(url);\n getMessagesUrl.pathname += \"/get-messages\";\n const response = await fetch(getMessagesUrl.toString(), {\n headers: options.headers,\n credentials: options.credentials,\n });\n return response.json<Message[]>();\n }\n\n const getInitialMessagesFetch =\n getInitialMessages || defaultGetInitialMessagesFetch;\n\n function doGetInitialMessages(\n getInitialMessagesOptions: GetInitialMessagesOptions\n ) {\n if (requestCache.has(agentUrlString)) {\n return requestCache.get(agentUrlString)!;\n }\n const promise = getInitialMessagesFetch(getInitialMessagesOptions);\n // immediately cache the promise so that we don't\n // create multiple requests for the same agent during multiple\n // concurrent renders\n requestCache.set(agentUrlString, promise);\n return promise;\n }\n\n const initialMessagesPromise =\n getInitialMessages === null\n ? null\n : doGetInitialMessages({\n agent: agent.agent,\n name: agent.name,\n url: agentUrlString,\n });\n const initialMessages = initialMessagesPromise\n ? use(initialMessagesPromise)\n : (rest.initialMessages ?? []);\n\n // manages adding and removing the promise from the cache\n useEffect(() => {\n if (!initialMessagesPromise) {\n return;\n }\n // this effect is responsible for removing the promise from the cache\n // when the component unmounts or the promise changes,\n // but that means it also must add the promise to the cache\n // so that multiple arbitrary effect runs produce the expected state\n // when resolved.\n requestCache.set(agentUrlString, initialMessagesPromise!);\n return () => {\n if (requestCache.get(agentUrlString) === initialMessagesPromise) {\n requestCache.delete(agentUrlString);\n }\n };\n }, [agentUrlString, initialMessagesPromise]);\n\n async function aiFetch(\n request: RequestInfo | URL,\n options: RequestInit = {}\n ) {\n // we're going to use a websocket to do the actual \"fetching\"\n // but still satisfy the type signature of the fetch function\n // so we'll return a promise that resolves to a response\n\n const {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n signal,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher, duplex\n } = options;\n const id = nanoid(8);\n const abortController = new AbortController();\n\n signal?.addEventListener(\"abort\", () => {\n // Propagate request cancellation to the Agent\n // We need to communciate cancellation as a websocket message, instead of a request signal\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_request_cancel\",\n id,\n })\n );\n\n // NOTE - If we wanted to, we could preserve the \"interrupted\" message here, with the code below\n // However, I think it might be the responsibility of the library user to implement that behavior manually?\n // Reasoning: This code could be subject to collisions, as it \"force saves\" the messages we have locally\n //\n // agent.send(JSON.stringify({\n // type: \"cf_agent_chat_messages\",\n // messages: ... /* some way of getting current messages ref? */\n // }))\n\n abortController.abort();\n });\n\n agent.addEventListener(\n \"message\",\n (event) => {\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_use_chat_response\") {\n if (data.id === id) {\n controller.enqueue(new TextEncoder().encode(data.body));\n if (data.done) {\n controller.close();\n abortController.abort();\n }\n }\n }\n },\n { signal: abortController.signal }\n );\n\n let controller: ReadableStreamDefaultController;\n\n const stream = new ReadableStream({\n start(c) {\n controller = c;\n },\n });\n\n agent.send(\n JSON.stringify({\n type: \"cf_agent_use_chat_request\",\n id,\n url: request.toString(),\n init: {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n },\n })\n );\n\n return new Response(stream);\n }\n const useChatHelpers = useChat({\n initialMessages,\n sendExtraMessageFields: true,\n fetch: aiFetch,\n ...rest,\n });\n\n useEffect(() => {\n function onClearHistory(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_clear\") {\n useChatHelpers.setMessages([]);\n }\n }\n\n function onMessages(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_messages\") {\n useChatHelpers.setMessages(data.messages);\n }\n }\n\n agent.addEventListener(\"message\", onClearHistory);\n agent.addEventListener(\"message\", onMessages);\n\n return () => {\n agent.removeEventListener(\"message\", onClearHistory);\n agent.removeEventListener(\"message\", onMessages);\n };\n }, [agent, useChatHelpers.setMessages]);\n\n return {\n ...useChatHelpers,\n /**\n * Set the chat messages and synchronize with the Agent\n * @param messages New messages to set\n */\n setMessages: (messages: Message[]) => {\n useChatHelpers.setMessages(messages);\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_messages\",\n messages,\n })\n );\n },\n /**\n * Clear chat history on both client and Agent\n */\n clearHistory: () => {\n useChatHelp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AA,QACJ,KAAK,UAAU;AAAA,UACb,MAAM;AAAA,UACN;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAIA,cAAc,MAAM;AAClB,qBAAe,YAAY,CAAC,CAAC;AAC7B,YAAM;AAAA,QACJ,KAAK,UAAU;AAAA,UACb,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":["options"]}
|
package/dist/ai-types.d.ts
CHANGED
|
@@ -1,49 +1,74 @@
|
|
|
1
|
-
import { Message } from
|
|
1
|
+
import { Message } from "ai";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Types of messages sent from the Agent to clients
|
|
5
5
|
*/
|
|
6
|
-
type OutgoingMessage =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
6
|
+
type OutgoingMessage =
|
|
7
|
+
| {
|
|
8
|
+
/** Indicates this message contains updated chat messages */
|
|
9
|
+
type: "cf_agent_chat_messages";
|
|
10
|
+
/** Array of chat messages */
|
|
11
|
+
messages: Message[];
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
/** Indicates this message is a response to a chat request */
|
|
15
|
+
type: "cf_agent_use_chat_response";
|
|
16
|
+
/** Unique ID of the request this response corresponds to */
|
|
17
|
+
id: string;
|
|
18
|
+
/** Content body of the response */
|
|
19
|
+
body: string;
|
|
20
|
+
/** Whether this is the final chunk of the response */
|
|
21
|
+
done: boolean;
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
/** Indicates this message contains updated chat messages */
|
|
25
|
+
type: "cf_agent_chat_messages";
|
|
26
|
+
/** Array of chat messages */
|
|
27
|
+
messages: Message[];
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
/** Indicates this message is a command to clear chat history */
|
|
31
|
+
type: "cf_agent_chat_clear";
|
|
32
|
+
};
|
|
29
33
|
/**
|
|
30
34
|
* Types of messages sent from clients to the Agent
|
|
31
35
|
*/
|
|
32
|
-
type IncomingMessage =
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
type IncomingMessage =
|
|
37
|
+
| {
|
|
38
|
+
/** Indicates this message is a request to the chat API */
|
|
39
|
+
type: "cf_agent_use_chat_request";
|
|
40
|
+
/** Unique ID for this request */
|
|
41
|
+
id: string;
|
|
42
|
+
/** Request initialization options */
|
|
43
|
+
init: Pick<
|
|
44
|
+
RequestInit,
|
|
45
|
+
| "method"
|
|
46
|
+
| "keepalive"
|
|
47
|
+
| "headers"
|
|
48
|
+
| "body"
|
|
49
|
+
| "redirect"
|
|
50
|
+
| "integrity"
|
|
51
|
+
| "credentials"
|
|
52
|
+
| "mode"
|
|
53
|
+
| "referrer"
|
|
54
|
+
| "referrerPolicy"
|
|
55
|
+
| "window"
|
|
56
|
+
>;
|
|
57
|
+
}
|
|
58
|
+
| {
|
|
59
|
+
/** Indicates this message is a command to clear chat history */
|
|
60
|
+
type: "cf_agent_chat_clear";
|
|
61
|
+
}
|
|
62
|
+
| {
|
|
63
|
+
/** Indicates this message contains updated chat messages */
|
|
64
|
+
type: "cf_agent_chat_messages";
|
|
65
|
+
/** Array of chat messages */
|
|
66
|
+
messages: Message[];
|
|
67
|
+
}
|
|
68
|
+
| {
|
|
69
|
+
/** Indicates the user wants to stop generation of this message */
|
|
70
|
+
type: "cf_agent_chat_request_cancel";
|
|
71
|
+
id: string;
|
|
72
|
+
};
|
|
48
73
|
|
|
49
74
|
export type { IncomingMessage, OutgoingMessage };
|
package/dist/ai-types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=ai-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|