agents 0.0.47 → 0.0.48

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.
@@ -1,36 +1,31 @@
1
- import { Agent, AgentContext } from "./index.js";
2
- import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
3
- import { Connection, WSMessage } from "partyserver";
4
- import "node:async_hooks";
5
- import "cloudflare:workers";
1
+ import { Agent, AgentContext } from './index.js';
2
+ import { Message, StreamTextOnFinishCallback, ToolSet } from 'ai';
3
+ import { Connection, WSMessage } from 'partyserver';
4
+ import 'node:async_hooks';
5
+ import 'cloudflare:workers';
6
6
 
7
7
  /**
8
8
  * Extension of Agent with built-in chat capabilities
9
9
  * @template Env Environment type containing bindings
10
10
  */
11
- declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
12
- Env,
13
- State
14
- > {
15
- #private;
16
- /** Array of chat messages for the current conversation */
17
- messages: Message[];
18
- constructor(ctx: AgentContext, env: Env);
19
- onMessage(connection: Connection, message: WSMessage): Promise<void>;
20
- onRequest(request: Request): Promise<Response>;
21
- /**
22
- * Handle incoming chat messages and generate a response
23
- * @param onFinish Callback to be called when the response is finished
24
- * @returns Response to send to the client or undefined
25
- */
26
- onChatMessage(
27
- onFinish: StreamTextOnFinishCallback<ToolSet>
28
- ): Promise<Response | undefined>;
29
- /**
30
- * Save messages on the server side and trigger AI response
31
- * @param messages Chat messages to save
32
- */
33
- saveMessages(messages: Message[]): Promise<void>;
11
+ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<Env, State> {
12
+ #private;
13
+ /** Array of chat messages for the current conversation */
14
+ messages: Message[];
15
+ constructor(ctx: AgentContext, env: Env);
16
+ onMessage(connection: Connection, message: WSMessage): Promise<void>;
17
+ onRequest(request: Request): Promise<Response>;
18
+ /**
19
+ * Handle incoming chat messages and generate a response
20
+ * @param onFinish Callback to be called when the response is finished
21
+ * @returns Response to send to the client or undefined
22
+ */
23
+ onChatMessage(onFinish: StreamTextOnFinishCallback<ToolSet>): Promise<Response | undefined>;
24
+ /**
25
+ * Save messages on the server side and trigger AI response
26
+ * @param messages Chat messages to save
27
+ */
28
+ saveMessages(messages: Message[]): Promise<void>;
34
29
  }
35
30
 
36
31
  export { AIChatAgent };
@@ -1,88 +1,60 @@
1
- import * as ai from "ai";
2
- import { Message } from "ai";
3
- import { useChat } from "@ai-sdk/react";
4
- import { useAgent } from "./react.js";
5
- import "partysocket";
6
- import "partysocket/react";
7
- import "./client.js";
1
+ import * as ai from 'ai';
2
+ import { Message } from 'ai';
3
+ import { useChat } from '@ai-sdk/react';
4
+ import { useAgent } from './react.js';
5
+ import 'partysocket';
6
+ import 'partysocket/react';
7
+ import './client.js';
8
8
 
9
9
  type GetInitialMessagesOptions = {
10
- agent: string;
11
- name: string;
12
- url: string;
10
+ agent: string;
11
+ name: string;
12
+ url: string;
13
13
  };
14
14
  /**
15
15
  * Options for the useAgentChat hook
16
16
  */
17
- type UseAgentChatOptions<State> = Omit<
18
- Parameters<typeof useChat>[0] & {
17
+ type UseAgentChatOptions<State> = Omit<Parameters<typeof useChat>[0] & {
19
18
  /** Agent connection from useAgent */
20
19
  agent: ReturnType<typeof useAgent<State>>;
21
- getInitialMessages?:
22
- | undefined
23
- | null
24
- | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
25
- },
26
- "fetch"
27
- >;
20
+ getInitialMessages?: undefined | null | ((options: GetInitialMessagesOptions) => Promise<Message[]>);
21
+ }, "fetch">;
28
22
  /**
29
23
  * React hook for building AI chat interfaces using an Agent
30
24
  * @param options Chat options including the agent connection
31
25
  * @returns Chat interface controls and state with added clearHistory method
32
26
  */
33
- declare function useAgentChat<State = unknown>(
34
- options: UseAgentChatOptions<State>
35
- ): {
36
- /**
37
- * Set the chat messages and synchronize with the Agent
38
- * @param messages New messages to set
39
- */
40
- setMessages: (messages: Message[]) => void;
41
- /**
42
- * Clear chat history on both client and Agent
43
- */
44
- clearHistory: () => void;
45
- messages: ai.UIMessage[];
46
- error: undefined | Error;
47
- append: (
48
- message: Message | ai.CreateMessage,
49
- chatRequestOptions?: ai.ChatRequestOptions
50
- ) => Promise<string | null | undefined>;
51
- reload: (
52
- chatRequestOptions?: ai.ChatRequestOptions
53
- ) => Promise<string | null | undefined>;
54
- stop: () => void;
55
- input: string;
56
- setInput: React.Dispatch<React.SetStateAction<string>>;
57
- handleInputChange: (
58
- e:
59
- | React.ChangeEvent<HTMLInputElement>
60
- | React.ChangeEvent<HTMLTextAreaElement>
61
- ) => void;
62
- handleSubmit: (
63
- event?: {
64
- preventDefault?: () => void;
65
- },
66
- chatRequestOptions?: ai.ChatRequestOptions
67
- ) => void;
68
- metadata?: Object;
69
- isLoading: boolean;
70
- status: "submitted" | "streaming" | "ready" | "error";
71
- data?: ai.JSONValue[];
72
- setData: (
73
- data:
74
- | ai.JSONValue[]
75
- | undefined
76
- | ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)
77
- ) => void;
78
- id: string;
79
- addToolResult: ({
80
- toolCallId,
81
- result,
82
- }: {
83
- toolCallId: string;
84
- result: any;
85
- }) => void;
27
+ declare function useAgentChat<State = unknown>(options: UseAgentChatOptions<State>): {
28
+ /**
29
+ * Set the chat messages and synchronize with the Agent
30
+ * @param messages New messages to set
31
+ */
32
+ setMessages: (messages: Message[]) => void;
33
+ /**
34
+ * Clear chat history on both client and Agent
35
+ */
36
+ clearHistory: () => void;
37
+ messages: ai.UIMessage[];
38
+ error: undefined | Error;
39
+ append: (message: Message | ai.CreateMessage, chatRequestOptions?: ai.ChatRequestOptions) => Promise<string | null | undefined>;
40
+ reload: (chatRequestOptions?: ai.ChatRequestOptions) => Promise<string | null | undefined>;
41
+ stop: () => void;
42
+ input: string;
43
+ setInput: React.Dispatch<React.SetStateAction<string>>;
44
+ handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
45
+ handleSubmit: (event?: {
46
+ preventDefault?: () => void;
47
+ }, chatRequestOptions?: ai.ChatRequestOptions) => void;
48
+ metadata?: Object;
49
+ isLoading: boolean;
50
+ status: "submitted" | "streaming" | "ready" | "error";
51
+ data?: ai.JSONValue[];
52
+ setData: (data: ai.JSONValue[] | undefined | ((data: ai.JSONValue[] | undefined) => ai.JSONValue[] | undefined)) => void;
53
+ id: string;
54
+ addToolResult: ({ toolCallId, result, }: {
55
+ toolCallId: string;
56
+ result: any;
57
+ }) => void;
86
58
  };
87
59
 
88
60
  export { useAgentChat };
@@ -1,69 +1,49 @@
1
- import { Message } from "ai";
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
- /** 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
- };
6
+ type OutgoingMessage = {
7
+ /** Indicates this message contains updated chat messages */
8
+ type: "cf_agent_chat_messages";
9
+ /** Array of chat messages */
10
+ messages: Message[];
11
+ } | {
12
+ /** Indicates this message is a response to a chat request */
13
+ type: "cf_agent_use_chat_response";
14
+ /** Unique ID of the request this response corresponds to */
15
+ id: string;
16
+ /** Content body of the response */
17
+ body: string;
18
+ /** Whether this is the final chunk of the response */
19
+ done: boolean;
20
+ } | {
21
+ /** Indicates this message contains updated chat messages */
22
+ type: "cf_agent_chat_messages";
23
+ /** Array of chat messages */
24
+ messages: Message[];
25
+ } | {
26
+ /** Indicates this message is a command to clear chat history */
27
+ type: "cf_agent_chat_clear";
28
+ };
33
29
  /**
34
30
  * Types of messages sent from clients to the Agent
35
31
  */
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
- };
32
+ type IncomingMessage = {
33
+ /** Indicates this message is a request to the chat API */
34
+ type: "cf_agent_use_chat_request";
35
+ /** Unique ID for this request */
36
+ id: string;
37
+ /** Request initialization options */
38
+ init: Pick<RequestInit, "method" | "keepalive" | "headers" | "body" | "redirect" | "integrity" | "credentials" | "mode" | "referrer" | "referrerPolicy" | "window">;
39
+ } | {
40
+ /** Indicates this message is a command to clear chat history */
41
+ type: "cf_agent_chat_clear";
42
+ } | {
43
+ /** Indicates this message contains updated chat messages */
44
+ type: "cf_agent_chat_messages";
45
+ /** Array of chat messages */
46
+ messages: Message[];
47
+ };
68
48
 
69
49
  export type { IncomingMessage, OutgoingMessage };
package/dist/client.d.ts CHANGED
@@ -1,68 +1,57 @@
1
- import {
2
- PartySocketOptions,
3
- PartyFetchOptions,
4
- PartySocket,
5
- } from "partysocket";
1
+ import { PartySocketOptions, PartyFetchOptions, PartySocket } from 'partysocket';
6
2
 
7
3
  /**
8
4
  * Options for creating an AgentClient
9
5
  */
10
- type AgentClientOptions<State = unknown> = Omit<
11
- PartySocketOptions,
12
- "party" | "room"
13
- > & {
14
- /** Name of the agent to connect to */
15
- agent: string;
16
- /** Name of the specific Agent instance */
17
- name?: string;
18
- /** Called when the Agent's state is updated */
19
- onStateUpdate?: (state: State, source: "server" | "client") => void;
6
+ type AgentClientOptions<State = unknown> = Omit<PartySocketOptions, "party" | "room"> & {
7
+ /** Name of the agent to connect to */
8
+ agent: string;
9
+ /** Name of the specific Agent instance */
10
+ name?: string;
11
+ /** Called when the Agent's state is updated */
12
+ onStateUpdate?: (state: State, source: "server" | "client") => void;
20
13
  };
21
14
  /**
22
15
  * Options for streaming RPC calls
23
16
  */
24
17
  type StreamOptions = {
25
- /** Called when a chunk of data is received */
26
- onChunk?: (chunk: unknown) => void;
27
- /** Called when the stream ends */
28
- onDone?: (finalChunk: unknown) => void;
29
- /** Called when an error occurs */
30
- onError?: (error: string) => void;
18
+ /** Called when a chunk of data is received */
19
+ onChunk?: (chunk: unknown) => void;
20
+ /** Called when the stream ends */
21
+ onDone?: (finalChunk: unknown) => void;
22
+ /** Called when an error occurs */
23
+ onError?: (error: string) => void;
31
24
  };
32
25
  /**
33
26
  * Options for the agentFetch function
34
27
  */
35
28
  type AgentClientFetchOptions = Omit<PartyFetchOptions, "party" | "room"> & {
36
- /** Name of the agent to connect to */
37
- agent: string;
38
- /** Name of the specific Agent instance */
39
- name?: string;
29
+ /** Name of the agent to connect to */
30
+ agent: string;
31
+ /** Name of the specific Agent instance */
32
+ name?: string;
40
33
  };
41
34
  /**
42
35
  * WebSocket client for connecting to an Agent
43
36
  */
44
37
  declare class AgentClient<State = unknown> extends PartySocket {
45
- #private;
46
- /**
47
- * @deprecated Use agentFetch instead
48
- */
49
- static fetch(_opts: PartyFetchOptions): Promise<Response>;
50
- agent: string;
51
- name: string;
52
- constructor(options: AgentClientOptions<State>);
53
- setState(state: State): void;
54
- /**
55
- * Call a method on the Agent
56
- * @param method Name of the method to call
57
- * @param args Arguments to pass to the method
58
- * @param streamOptions Options for handling streaming responses
59
- * @returns Promise that resolves with the method's return value
60
- */
61
- call<T = unknown>(
62
- method: string,
63
- args?: unknown[],
64
- streamOptions?: StreamOptions
65
- ): Promise<T>;
38
+ #private;
39
+ /**
40
+ * @deprecated Use agentFetch instead
41
+ */
42
+ static fetch(_opts: PartyFetchOptions): Promise<Response>;
43
+ agent: string;
44
+ name: string;
45
+ constructor(options: AgentClientOptions<State>);
46
+ setState(state: State): void;
47
+ /**
48
+ * Call a method on the Agent
49
+ * @param method Name of the method to call
50
+ * @param args Arguments to pass to the method
51
+ * @param streamOptions Options for handling streaming responses
52
+ * @returns Promise that resolves with the method's return value
53
+ */
54
+ call<T = unknown>(method: string, args?: unknown[], streamOptions?: StreamOptions): Promise<T>;
66
55
  }
67
56
  /**
68
57
  * Make an HTTP request to an Agent
@@ -70,15 +59,6 @@ declare class AgentClient<State = unknown> extends PartySocket {
70
59
  * @param init Request initialization options
71
60
  * @returns Promise resolving to a Response
72
61
  */
73
- declare function agentFetch(
74
- opts: AgentClientFetchOptions,
75
- init?: RequestInit
76
- ): Promise<Response>;
62
+ declare function agentFetch(opts: AgentClientFetchOptions, init?: RequestInit): Promise<Response>;
77
63
 
78
- export {
79
- AgentClient,
80
- type AgentClientFetchOptions,
81
- type AgentClientOptions,
82
- type StreamOptions,
83
- agentFetch,
84
- };
64
+ export { AgentClient, type AgentClientFetchOptions, type AgentClientOptions, type StreamOptions, agentFetch };