agents 0.12.0 → 0.12.2

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 CHANGED
@@ -222,6 +222,16 @@ const chat = useAgent({
222
222
 
223
223
  The routed URL becomes `/agents/inbox/{userId}/sub/chat/{chatId}`.
224
224
 
225
+ Child WebSocket clients can use the same URL shape. The parent remains the
226
+ public address, while child agents still receive `onConnect`, `onMessage`,
227
+ `onClose`, `broadcast()`, and `getConnections()` calls scoped to their own
228
+ clients. Parent broadcasts do not leak to child-targeted sockets, and child
229
+ connection tags, readonly state, and protocol-message settings are preserved
230
+ when a connection is resumed from hibernation.
231
+
232
+ Nested sub-agent URLs are supported using repeated `/sub/{agent}/{name}`
233
+ segments, subject to the platform's current facet nesting limits.
234
+
225
235
  ### Agent Tools
226
236
 
227
237
  Run chat-capable sub-agents as tools from a parent chat agent. Think agents and
@@ -25,7 +25,8 @@ import {
25
25
  ConnectionContext as ConnectionContext$1,
26
26
  PartyServerOptions,
27
27
  Server,
28
- WSMessage
28
+ WSMessage,
29
+ WSMessage as WSMessage$1
29
30
  } from "partyserver";
30
31
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
31
32
  import {
@@ -845,7 +846,7 @@ declare class MCPClientConnection {
845
846
  */
846
847
  getTransport(
847
848
  transportType: BaseTransportType
848
- ): RPCClientTransport | SSEClientTransport | StreamableHTTPClientTransport;
849
+ ): StreamableHTTPClientTransport | SSEClientTransport | RPCClientTransport;
849
850
  private tryConnect;
850
851
  private _capabilityErrorHandler;
851
852
  }
@@ -1647,6 +1648,54 @@ declare class SqlError extends Error {
1647
1648
  readonly query: string;
1648
1649
  constructor(query: string, cause: unknown);
1649
1650
  }
1651
+ type SubAgentConnectionMeta = {
1652
+ id: string;
1653
+ uri: string | null;
1654
+ tags: string[];
1655
+ state: unknown;
1656
+ requestHeaders?: [string, string][];
1657
+ };
1658
+ type SubAgentConnectionBridgeLike = {
1659
+ send(message: string | ArrayBuffer | ArrayBufferView): void;
1660
+ close(code?: number, reason?: string): void;
1661
+ setState(state: unknown): unknown;
1662
+ broadcast(
1663
+ ownerPath: ReadonlyArray<{
1664
+ className: string;
1665
+ name: string;
1666
+ }>,
1667
+ message: string | ArrayBuffer | ArrayBufferView,
1668
+ without?: string[]
1669
+ ): void;
1670
+ };
1671
+ declare class SubAgentConnectionBridge
1672
+ extends RpcTarget
1673
+ implements SubAgentConnectionBridgeLike
1674
+ {
1675
+ #private;
1676
+ constructor(
1677
+ connection: Connection,
1678
+ broadcast?: (
1679
+ ownerPath: ReadonlyArray<{
1680
+ className: string;
1681
+ name: string;
1682
+ }>,
1683
+ message: string | ArrayBuffer | ArrayBufferView,
1684
+ without?: string[]
1685
+ ) => void
1686
+ );
1687
+ send(message: string | ArrayBuffer | ArrayBufferView): void;
1688
+ close(code?: number, reason?: string): void;
1689
+ setState(state: unknown): unknown;
1690
+ broadcast(
1691
+ ownerPath: ReadonlyArray<{
1692
+ className: string;
1693
+ name: string;
1694
+ }>,
1695
+ message: string | ArrayBuffer | ArrayBufferView,
1696
+ without?: string[]
1697
+ ): void;
1698
+ }
1650
1699
  /**
1651
1700
  * Constructor type for a sub-agent class.
1652
1701
  * Used by {@link Agent.subAgent} to reference the child class
@@ -1947,6 +1996,8 @@ declare class Agent<
1947
1996
  * parent-owned WebSocket handles during this window.
1948
1997
  */
1949
1998
  private _suppressProtocolBroadcasts;
1999
+ private _cf_currentSubAgentBridge?;
2000
+ private _cf_virtualSubAgentConnections;
1950
2001
  /**
1951
2002
  * Ancestor chain, root-first. Empty for top-level DOs; populated at
1952
2003
  * facet init time from the parent's own `selfPath`. Exposed publicly
@@ -2782,6 +2833,76 @@ declare class Agent<
2782
2833
  * @experimental The API surface may change before stabilizing.
2783
2834
  */
2784
2835
  fetch(request: Request): Promise<Response>;
2836
+ broadcast(
2837
+ msg: string | ArrayBuffer | ArrayBufferView,
2838
+ without?: string[]
2839
+ ): void;
2840
+ getConnection<TState = unknown>(id: string): Connection<TState> | undefined;
2841
+ getConnections<TState = unknown>(tag?: string): Iterable<Connection<TState>>;
2842
+ private _cf_broadcastToParentSubAgent;
2843
+ _cf_broadcastToSubAgent(
2844
+ ownerPath: ReadonlyArray<AgentPathStep>,
2845
+ message: string | ArrayBuffer | ArrayBufferView,
2846
+ without?: string[]
2847
+ ): Promise<void>;
2848
+ _cf_subAgentConnectionMetas(
2849
+ ownerPath: ReadonlyArray<AgentPathStep>
2850
+ ): Promise<SubAgentConnectionMeta[]>;
2851
+ _cf_sendToSubAgentConnection(
2852
+ connectionId: string,
2853
+ message: string | ArrayBuffer | ArrayBufferView
2854
+ ): Promise<void>;
2855
+ _cf_closeSubAgentConnection(
2856
+ connectionId: string,
2857
+ code?: number,
2858
+ reason?: string
2859
+ ): Promise<void>;
2860
+ _cf_setSubAgentConnectionState(
2861
+ connectionId: string,
2862
+ state: unknown
2863
+ ): Promise<unknown>;
2864
+ private _cf_subAgentConnectionMetaForPath;
2865
+ private _cf_subAgentTargetPath;
2866
+ private _cf_subAgentPathFromOuterUri;
2867
+ private _isSameAgentPath;
2868
+ private _cf_connectionHasSubAgentTarget;
2869
+ protected _cf_connectionTargetsSubAgent(connection: Connection): boolean;
2870
+ /**
2871
+ * Returns true when the current request is addressed to a child facet of
2872
+ * this agent rather than to this agent itself.
2873
+ *
2874
+ * Chat-style subclasses wrap `onConnect` before the base Agent forwarding
2875
+ * wrapper runs, so they need a request-level check to avoid sending their
2876
+ * own protocol frames on sockets that are about to be forwarded to a child.
2877
+ */
2878
+ protected _cf_requestTargetsSubAgent(request: Request): boolean;
2879
+ private _cf_forwardSubAgentWebSocketConnect;
2880
+ private _cf_createSubAgentConnectionBridge;
2881
+ private _cf_forwardSubAgentWebSocketMessage;
2882
+ private _cf_forwardSubAgentWebSocketClose;
2883
+ private _cf_resolveSubAgentConnection;
2884
+ _cf_handleSubAgentWebSocketConnect(
2885
+ bridge: SubAgentConnectionBridge,
2886
+ meta: SubAgentConnectionMeta
2887
+ ): Promise<void>;
2888
+ _cf_handleSubAgentWebSocketMessage(
2889
+ message: WSMessage,
2890
+ bridge: SubAgentConnectionBridge,
2891
+ meta: SubAgentConnectionMeta
2892
+ ): Promise<void>;
2893
+ _cf_handleSubAgentWebSocketClose(
2894
+ code: number,
2895
+ reason: string,
2896
+ wasClean: boolean,
2897
+ bridge: SubAgentConnectionBridge,
2898
+ meta: SubAgentConnectionMeta
2899
+ ): Promise<void>;
2900
+ private _cf_runWithSubAgentBridge;
2901
+ private _cf_createSubAgentBridgeConnection;
2902
+ private _cf_storeVirtualSubAgentConnection;
2903
+ protected _cf_hydrateSubAgentConnectionsFromRoot(): Promise<void>;
2904
+ private _cf_getRawConnectionState;
2905
+ private _cf_getForwardedSubAgentState;
2785
2906
  /**
2786
2907
  * Parent-side middleware hook. Fires before a request is
2787
2908
  * forwarded into a facet sub-agent. Mirrors `onBeforeConnect` /
@@ -3872,7 +3993,7 @@ export {
3872
3993
  DEFAULT_AGENT_STATIC_OPTIONS as E,
3873
3994
  SSEEdgeClientTransport as Et,
3874
3995
  RPCRequest as F,
3875
- WSMessage as G,
3996
+ WSMessage$1 as G,
3876
3997
  StreamingResponse as H,
3877
3998
  RPCResponse as I,
3878
3999
  getCurrentAgent as J,
@@ -3950,4 +4071,4 @@ export {
3950
4071
  createMcpHandler as yt,
3951
4072
  SendEmailOptions as z
3952
4073
  };
3953
- //# sourceMappingURL=agent-tool-types-tBGRsPm0.d.ts.map
4074
+ //# sourceMappingURL=agent-tool-types-DSteYkkS.d.ts.map
@@ -14,7 +14,7 @@ import {
14
14
  s as AgentToolRunInfo,
15
15
  t as AgentToolChildAdapter,
16
16
  u as AgentToolRunStatus
17
- } from "./agent-tool-types-tBGRsPm0.js";
17
+ } from "./agent-tool-types-DSteYkkS.js";
18
18
  export {
19
19
  AgentToolChildAdapter,
20
20
  AgentToolDisplayMetadata,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  a as AgentToolEventState,
3
3
  i as AgentToolEventMessage
4
- } from "./agent-tool-types-tBGRsPm0.js";
4
+ } from "./agent-tool-types-DSteYkkS.js";
5
5
 
6
6
  //#region src/chat/agent-tools.d.ts
7
7
  declare function createAgentToolEventState(): AgentToolEventState;
@@ -11,4 +11,4 @@ declare function applyAgentToolEvent(
11
11
  ): AgentToolEventState;
12
12
  //#endregion
13
13
  export { createAgentToolEventState as n, applyAgentToolEvent as t };
14
- //# sourceMappingURL=agent-tools-CIO14miM.d.ts.map
14
+ //# sourceMappingURL=agent-tools-eGTCdVZX.d.ts.map
@@ -14,7 +14,7 @@ import {
14
14
  s as AgentToolRunInfo,
15
15
  t as AgentToolChildAdapter,
16
16
  u as AgentToolRunStatus
17
- } from "./agent-tool-types-tBGRsPm0.js";
17
+ } from "./agent-tool-types-DSteYkkS.js";
18
18
  import { Tool } from "ai";
19
19
 
20
20
  //#region src/agent-tools.d.ts
@@ -1,5 +1,5 @@
1
- import { a as AgentToolEventState, i as AgentToolEventMessage, l as AgentToolRunState, r as AgentToolEvent } from "../agent-tool-types-tBGRsPm0.js";
2
- import { n as createAgentToolEventState, t as applyAgentToolEvent } from "../agent-tools-CIO14miM.js";
1
+ import { a as AgentToolEventState, i as AgentToolEventMessage, l as AgentToolRunState, r as AgentToolEvent } from "../agent-tool-types-DSteYkkS.js";
2
+ import { n as createAgentToolEventState, t as applyAgentToolEvent } from "../agent-tools-eGTCdVZX.js";
3
3
  import { JSONSchema7, Tool, ToolSet, UIMessage } from "ai";
4
4
  import { Connection } from "agents";
5
5
 
@@ -503,6 +503,7 @@ declare class ResumableStream {
503
503
  * When non-null the caller should reconstruct the message from chunks.
504
504
  */
505
505
  replayChunks(connection: Connection, requestId: string): string | null;
506
+ replayCompletedChunksByRequestId(connection: Connection, requestId: string): boolean;
506
507
  /**
507
508
  * Restore active stream state if the agent was restarted during streaming.
508
509
  * All streams are restored regardless of age — stale cleanup happens
@@ -746,6 +746,37 @@ var ResumableStream = class ResumableStream {
746
746
  }));
747
747
  return null;
748
748
  }
749
+ replayCompletedChunksByRequestId(connection, requestId) {
750
+ this.flushBuffer();
751
+ const stream = this.sql`
752
+ select * from cf_ai_chat_stream_metadata
753
+ where request_id = ${requestId}
754
+ and status = 'completed'
755
+ order by created_at desc
756
+ limit 1
757
+ `[0];
758
+ if (!stream) return false;
759
+ const chunks = this.sql`
760
+ select * from cf_ai_chat_stream_chunks
761
+ where stream_id = ${stream.id}
762
+ order by chunk_index asc
763
+ `;
764
+ for (const chunk of chunks || []) connection.send(JSON.stringify({
765
+ body: chunk.body,
766
+ done: false,
767
+ id: requestId,
768
+ type: CHAT_MESSAGE_TYPES.USE_CHAT_RESPONSE,
769
+ replay: true
770
+ }));
771
+ connection.send(JSON.stringify({
772
+ body: "",
773
+ done: true,
774
+ id: requestId,
775
+ type: CHAT_MESSAGE_TYPES.USE_CHAT_RESPONSE,
776
+ replay: true
777
+ }));
778
+ return true;
779
+ }
749
780
  /**
750
781
  * Restore active stream state if the agent was restarted during streaming.
751
782
  * All streams are restored regardless of age — stale cleanup happens