agents 0.21.0 → 0.23.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.
Files changed (258) hide show
  1. package/README.md +21 -1
  2. package/dist/{agent-tool-types-CzGGB-20.d.ts → agent-routing-DE5zmCQ8.d.ts} +1844 -1336
  3. package/dist/agent-routing.d.ts +14 -0
  4. package/dist/agent-routing.js +187 -0
  5. package/dist/agent-routing.js.map +1 -0
  6. package/dist/agent-tool-types.d.ts +26 -26
  7. package/dist/{agent-tools-zR2d5uij.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
  8. package/dist/agent-tools.d.ts +21 -21
  9. package/dist/agent-tools.js +2 -1
  10. package/dist/agent-tools.js.map +1 -1
  11. package/dist/browser/ai.js +6 -2
  12. package/dist/browser/ai.js.map +1 -1
  13. package/dist/browser/tanstack-ai.js.map +1 -1
  14. package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
  15. package/dist/callable-decorator.d.ts +18 -0
  16. package/dist/callable-decorator.js +71 -0
  17. package/dist/callable-decorator.js.map +1 -0
  18. package/dist/capability-BjSKYpzg.js +42 -0
  19. package/dist/capability-BjSKYpzg.js.map +1 -0
  20. package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
  21. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  22. package/dist/channels/ai-sdk.d.ts +46 -0
  23. package/dist/channels/ai-sdk.js +120 -0
  24. package/dist/channels/ai-sdk.js.map +1 -0
  25. package/dist/channels/email.d.ts +95 -0
  26. package/dist/channels/email.js +323 -0
  27. package/dist/channels/email.js.map +1 -0
  28. package/dist/channels/index.d.ts +233 -0
  29. package/dist/channels/index.js +608 -0
  30. package/dist/channels/index.js.map +1 -0
  31. package/dist/channels/slack.d.ts +140 -0
  32. package/dist/channels/slack.js +614 -0
  33. package/dist/channels/slack.js.map +1 -0
  34. package/dist/channels/tanstack-ai.d.ts +39 -0
  35. package/dist/channels/tanstack-ai.js +17 -0
  36. package/dist/channels/tanstack-ai.js.map +1 -0
  37. package/dist/channels/telegram.d.ts +106 -0
  38. package/dist/channels/telegram.js +427 -0
  39. package/dist/channels/telegram.js.map +1 -0
  40. package/dist/channels/voice.d.ts +45 -0
  41. package/dist/channels/voice.js +122 -0
  42. package/dist/channels/voice.js.map +1 -0
  43. package/dist/chat/index.d.ts +2328 -2015
  44. package/dist/chat/index.js +891 -521
  45. package/dist/chat/index.js.map +1 -1
  46. package/dist/chat/react.d.ts +14 -1
  47. package/dist/chat/react.js +82 -52
  48. package/dist/chat/react.js.map +1 -1
  49. package/dist/chat/transport.js +1 -1
  50. package/dist/chat-sdk/index.d.ts +7 -7
  51. package/dist/chat-sdk/index.js +1 -1
  52. package/dist/{client-zqKcsyFa.js → client-jagG8a9_.js} +129 -37
  53. package/dist/client-jagG8a9_.js.map +1 -0
  54. package/dist/client.d.ts +1 -1
  55. package/dist/client.js +1 -1
  56. package/dist/{cloudflare-BduZwmYK.js → cloudflare-Dzvc7V2N.js} +10 -3
  57. package/dist/{cloudflare-BduZwmYK.js.map → cloudflare-Dzvc7V2N.js.map} +1 -1
  58. package/dist/context/index.d.ts +216 -0
  59. package/dist/context/index.js +454 -0
  60. package/dist/context/index.js.map +1 -0
  61. package/dist/current-agent-Da_C9a3b.d.ts +266 -0
  62. package/dist/current-agent-DhoDkSnH.js +51 -0
  63. package/dist/current-agent-DhoDkSnH.js.map +1 -0
  64. package/dist/diagnostics-BzvaX2UT.js +45 -0
  65. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  66. package/dist/diagnostics-C4jcz3VK.js +360 -0
  67. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  68. package/dist/{do-oauth-client-provider-VTZj2VtM.d.ts → do-oauth-client-provider-Tmf1vgKz.d.ts} +2 -2
  69. package/dist/{email-CL27preh.d.ts → email-7TatiTnl.d.ts} +38 -9
  70. package/dist/email-send.d.ts +15 -0
  71. package/dist/email-send.js +32 -0
  72. package/dist/email-send.js.map +1 -0
  73. package/dist/email.d.ts +14 -10
  74. package/dist/email.js.map +1 -1
  75. package/dist/{handler-stateless-C_bo-Ytq.d.ts → handler-stateless-DxYpJ_XF.d.ts} +3 -3
  76. package/dist/{handler-stateless-CIkKPETH.js → handler-stateless-VvrWSAVA.js} +5 -5
  77. package/dist/handler-stateless-VvrWSAVA.js.map +1 -0
  78. package/dist/index-BB0kqhIz.d.ts +101 -0
  79. package/dist/index-XDkuQ7zm.d.ts +89 -0
  80. package/dist/{index-BRnybD6X.d.ts → index-YSKgfgg9.d.ts} +21 -31
  81. package/dist/index.d.ts +113 -101
  82. package/dist/index.js +11 -7234
  83. package/dist/ingress-BfetZbMO.js +83 -0
  84. package/dist/ingress-BfetZbMO.js.map +1 -0
  85. package/dist/internal-CYlgHl1l.js +59 -0
  86. package/dist/internal-CYlgHl1l.js.map +1 -0
  87. package/dist/internal_context-BlxFEWfn.d.ts +19 -0
  88. package/dist/internal_context.d.ts +10 -4
  89. package/dist/internal_context.js +1 -10
  90. package/dist/{client-invoker-BNSZxAkv.d.ts → invoker-CG0_p_Wq.d.ts} +2 -2
  91. package/dist/{client-invoker-VNZ7X0nn.js → invoker-CHMnoxIA.js} +2 -2
  92. package/dist/invoker-CHMnoxIA.js.map +1 -0
  93. package/dist/lifecycle/index.d.ts +66 -0
  94. package/dist/lifecycle/index.js +4 -0
  95. package/dist/lifecycle-CMRGjZdw.js +1299 -0
  96. package/dist/lifecycle-CMRGjZdw.js.map +1 -0
  97. package/dist/mcp/{do-oauth-client-provider.d.ts → client/do-oauth-client-provider.d.ts} +1 -1
  98. package/dist/mcp/{do-oauth-client-provider.js → client/do-oauth-client-provider.js} +1 -1
  99. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -0
  100. package/dist/mcp/client/index.d.ts +42 -0
  101. package/dist/mcp/{client.js → client/index.js} +1 -1
  102. package/dist/mcp/{x402.d.ts → client/x402.d.ts} +2 -2
  103. package/dist/mcp/{x402.js → client/x402.js} +2 -2
  104. package/dist/mcp/client/x402.js.map +1 -0
  105. package/dist/mcp/index.d.ts +36 -36
  106. package/dist/mcp/index.js +14 -16
  107. package/dist/mcp/index.js.map +1 -1
  108. package/dist/mcp/{server.d.ts → server/index.d.ts} +1 -1
  109. package/dist/mcp/{server.js → server/index.js} +1 -1
  110. package/dist/observability/ai/index.js +50 -35
  111. package/dist/observability/ai/index.js.map +1 -1
  112. package/dist/observability/index.d.ts +4 -4
  113. package/dist/observability/index.js +3 -50
  114. package/dist/observability/index.js.map +1 -1
  115. package/dist/{protocol-Dqc2MQxo.js → protocol-B0nh6KNf.js} +19 -21
  116. package/dist/protocol-B0nh6KNf.js.map +1 -0
  117. package/dist/react.d.ts +4 -4
  118. package/dist/react.js +1 -1
  119. package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
  120. package/dist/retries.d.ts +8 -6
  121. package/dist/retries.js +13 -1
  122. package/dist/retries.js.map +1 -1
  123. package/dist/routing/index.d.ts +137 -0
  124. package/dist/routing/index.js +244 -0
  125. package/dist/routing/index.js.map +1 -0
  126. package/dist/sanitize-D9TujEK8.js +79 -0
  127. package/dist/sanitize-D9TujEK8.js.map +1 -0
  128. package/dist/schedule.d.ts +25 -94
  129. package/dist/schedule.js +1 -98
  130. package/dist/schedule.js.map +1 -1
  131. package/dist/scheduler-DD9NdYbF.js +665 -0
  132. package/dist/scheduler-DD9NdYbF.js.map +1 -0
  133. package/dist/scheduler-Dwh85ZGl.d.ts +223 -0
  134. package/dist/schedules/index.d.ts +22 -0
  135. package/dist/schedules/index.js +2 -0
  136. package/dist/schedules/parser.d.ts +79 -0
  137. package/dist/schedules/parser.js +103 -0
  138. package/dist/schedules/parser.js.map +1 -0
  139. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  140. package/dist/serializable.d.ts +1 -1
  141. package/dist/sessions/index.d.ts +441 -0
  142. package/dist/sessions/index.js +2063 -0
  143. package/dist/sessions/index.js.map +1 -0
  144. package/dist/skills/index.d.ts +99 -0
  145. package/dist/skills/index.js +254 -5
  146. package/dist/skills/index.js.map +1 -1
  147. package/dist/sql-error-CPY-GXyI.d.ts +12 -0
  148. package/dist/sql-error.d.ts +2 -0
  149. package/dist/sql-error.js +16 -0
  150. package/dist/sql-error.js.map +1 -0
  151. package/dist/src-DlSHshb2.js +6963 -0
  152. package/dist/src-DlSHshb2.js.map +1 -0
  153. package/dist/streams/index.d.ts +120 -0
  154. package/dist/streams/index.js +107 -0
  155. package/dist/streams/index.js.map +1 -0
  156. package/dist/streams-D6tJ0NN9.d.ts +370 -0
  157. package/dist/streams-DZKgAj9b.js +709 -0
  158. package/dist/streams-DZKgAj9b.js.map +1 -0
  159. package/dist/sub-routing.d.ts +12 -12
  160. package/dist/surface-bZZJqBka.js +17 -0
  161. package/dist/surface-bZZJqBka.js.map +1 -0
  162. package/dist/tasks/index.d.ts +64 -0
  163. package/dist/tasks/index.js +2 -0
  164. package/dist/tasks-BRJ5zgya.d.ts +517 -0
  165. package/dist/tasks-ylZgBjhj.js +1656 -0
  166. package/dist/tasks-ylZgBjhj.js.map +1 -0
  167. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  168. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  169. package/dist/text-stream-CpdiKrJB.js +272 -0
  170. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  171. package/dist/tokens-nHAKcN6M.js +52 -0
  172. package/dist/tokens-nHAKcN6M.js.map +1 -0
  173. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  174. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  175. package/dist/types-B7LojTe4.d.ts +202 -0
  176. package/dist/types-_Faxb570.d.ts +439 -0
  177. package/dist/voice/client.d.ts +226 -0
  178. package/dist/voice/client.js +932 -0
  179. package/dist/voice/client.js.map +1 -0
  180. package/dist/voice/errors.d.ts +43 -0
  181. package/dist/voice/errors.js +41 -0
  182. package/dist/voice/errors.js.map +1 -0
  183. package/dist/voice/index.d.ts +271 -0
  184. package/dist/voice/index.js +1812 -0
  185. package/dist/voice/index.js.map +1 -0
  186. package/dist/voice/react.d.ts +167 -0
  187. package/dist/voice/react.js +234 -0
  188. package/dist/voice/react.js.map +1 -0
  189. package/dist/voice/sfu.d.ts +71 -0
  190. package/dist/voice/sfu.js +157 -0
  191. package/dist/voice/sfu.js.map +1 -0
  192. package/dist/voice/text.d.ts +6 -0
  193. package/dist/voice/text.js +2 -0
  194. package/dist/voice/types.d.ts +58 -0
  195. package/dist/voice/types.js +18 -0
  196. package/dist/voice/types.js.map +1 -0
  197. package/dist/voice/workers-ai.d.ts +136 -0
  198. package/dist/voice/workers-ai.js +568 -0
  199. package/dist/voice/workers-ai.js.map +1 -0
  200. package/dist/websockets/index.d.ts +192 -0
  201. package/dist/websockets/index.js +2 -0
  202. package/dist/websockets-DUfRHPRq.js +502 -0
  203. package/dist/websockets-DUfRHPRq.js.map +1 -0
  204. package/dist/workflow-types.d.ts +25 -25
  205. package/dist/workflows.d.ts +22 -22
  206. package/dist/workflows.js +2 -1
  207. package/dist/workflows.js.map +1 -1
  208. package/dist/{ws-chat-transport-CIoOBbO7.js → ws-chat-transport-rWwta645.js} +152 -15
  209. package/dist/ws-chat-transport-rWwta645.js.map +1 -0
  210. package/docs/agent-class.md +29 -87
  211. package/docs/agent-tools.md +2 -1
  212. package/docs/channels.md +323 -0
  213. package/docs/chat-agents.md +19 -25
  214. package/docs/context.md +131 -0
  215. package/docs/durable-execution.md +1 -1
  216. package/docs/http-websockets.md +1 -11
  217. package/docs/human-in-the-loop.md +1 -1
  218. package/docs/index.md +16 -12
  219. package/docs/lifecycle.md +370 -0
  220. package/docs/long-running-agents.md +4 -6
  221. package/docs/mcp-client.md +55 -0
  222. package/docs/mcp-servers.md +5 -1
  223. package/docs/observability.md +11 -11
  224. package/docs/resumable-streaming.md +2 -2
  225. package/docs/routing.md +105 -0
  226. package/docs/scheduling.md +175 -15
  227. package/docs/server-driven-messages.md +1 -1
  228. package/docs/sessions.md +237 -871
  229. package/docs/streams.md +213 -0
  230. package/docs/sub-agents.md +185 -125
  231. package/docs/tasks.md +246 -0
  232. package/docs/voice.md +745 -0
  233. package/package.json +144 -33
  234. package/dist/cli/index.js +0 -26
  235. package/dist/cli/index.js.map +0 -1
  236. package/dist/client-invoker-VNZ7X0nn.js.map +0 -1
  237. package/dist/client-zqKcsyFa.js.map +0 -1
  238. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  239. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  240. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  241. package/dist/experimental/memory/session/index.d.ts +0 -670
  242. package/dist/experimental/memory/session/index.js +0 -2374
  243. package/dist/experimental/memory/session/index.js.map +0 -1
  244. package/dist/experimental/memory/utils/index.d.ts +0 -96
  245. package/dist/experimental/memory/utils/index.js +0 -79
  246. package/dist/experimental/memory/utils/index.js.map +0 -1
  247. package/dist/handler-stateless-CIkKPETH.js.map +0 -1
  248. package/dist/index.js.map +0 -1
  249. package/dist/internal_context-Dg4Cgjcu.d.ts +0 -37
  250. package/dist/internal_context.js.map +0 -1
  251. package/dist/mcp/client.d.ts +0 -42
  252. package/dist/mcp/do-oauth-client-provider.js.map +0 -1
  253. package/dist/mcp/x402.js.map +0 -1
  254. package/dist/protocol-Dqc2MQxo.js.map +0 -1
  255. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  256. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
  257. package/dist/ws-chat-transport-CIoOBbO7.js.map +0 -1
  258. /package/dist/{cli/index.d.ts → index-BVVgDSdq.d.ts} +0 -0
@@ -1,621 +0,0 @@
1
- import { ToolSet } from "ai";
2
-
3
- //#region src/experimental/memory/session/types.d.ts
4
- /**
5
- * Minimal message part shape used by Session internals.
6
- * Vercel AI SDK's `UIMessagePart` is structurally compatible.
7
- */
8
- interface SessionMessagePart {
9
- type: string;
10
- text?: string;
11
- reasoning?: string;
12
- toolCallId?: string;
13
- toolName?: string;
14
- input?: unknown;
15
- output?: unknown;
16
- state?: string;
17
- result?: unknown;
18
- }
19
- interface SessionTokenCounterInput {
20
- /** Messages returned by `session.getHistory()` for the active branch. */
21
- messages: SessionMessage[];
22
- /** Frozen system prompt managed by the Session context system. */
23
- systemPrompt: string;
24
- /** Loaded context blocks that were used to build `systemPrompt`. */
25
- contextBlocks: ContextBlock[];
26
- }
27
- type SessionTokenCounter = (
28
- input: SessionTokenCounterInput
29
- ) => number | Promise<number>;
30
- interface CompactAfterOptions {
31
- /**
32
- * Override the token estimate used by auto-compaction and status broadcasts.
33
- *
34
- * The default is a Workers-safe heuristic over message parts plus the
35
- * Session-managed frozen system prompt. Callers that have model-reported
36
- * usage or a tokenizer can provide a more precise counter here.
37
- */
38
- tokenCounter?: SessionTokenCounter;
39
- }
40
- /**
41
- * Context the Session passes to the registered compaction function. Lets the
42
- * same authoritative token accounting drive BOTH the "should we compact?"
43
- * (`compactAfter`) and "what should we compact?" (boundary) decisions, so a
44
- * consumer that wires a `tokenCounter` once doesn't hit the failure mode where
45
- * compaction fires every turn but silently no-ops because the boundary logic
46
- * used a different (under-counting) estimate.
47
- */
48
- interface CompactContext {
49
- /** The Session's token counter (from `compactAfter`/options), if configured. */
50
- tokenCounter?: SessionTokenCounter;
51
- }
52
- type CompactionErrorHandler = (error: unknown) => void | Promise<void>;
53
- /**
54
- * Minimal message shape used by Session internals.
55
- * Vercel AI SDK's `UIMessage` is structurally compatible — you can pass
56
- * `UIMessage` objects directly without conversion.
57
- */
58
- interface SessionMessage {
59
- id: string;
60
- role: string;
61
- parts: SessionMessagePart[];
62
- createdAt?: Date;
63
- }
64
- /**
65
- * Options for creating a Session.
66
- */
67
- interface SessionOptions {
68
- /** Context blocks for the system prompt. */
69
- context?: ContextConfig[];
70
- /** Provider for persisting the frozen system prompt. */
71
- promptStore?: WritableContextProvider;
72
- /** Custom token counter for auto-compaction/status estimates. */
73
- tokenCounter?: SessionTokenCounter;
74
- /** Called when automatic compaction fails after a threshold trigger. */
75
- onCompactionError?: CompactionErrorHandler;
76
- }
77
- //#endregion
78
- //#region src/experimental/memory/session/provider.d.ts
79
- interface SearchResult {
80
- id: string;
81
- role: string;
82
- content: string;
83
- createdAt?: string;
84
- sessionId?: string;
85
- }
86
- interface StoredCompaction {
87
- id: string;
88
- summary: string;
89
- fromMessageId: string;
90
- toMessageId: string;
91
- createdAt: string;
92
- }
93
- /** Per-row info for the active branch path, root → leaf order. */
94
- interface HistoryRowStat {
95
- id: string;
96
- /** Stored message role (e.g. "user" / "assistant"). */
97
- role: string;
98
- /** Serialized content size of the stored row in bytes. */
99
- bytes: number;
100
- }
101
- /** Result of a byte-budgeted history read. */
102
- interface RecentHistoryResult {
103
- /**
104
- * The most recent messages on the active branch path whose summed stored
105
- * content size fits `maxContentBytes`, root → leaf order, with compaction
106
- * overlays applied within the window. The window always covers at least
107
- * the leaf row (and `minRecentMessages` rows when requested), but rows
108
- * whose stored content fails to parse are skipped — so a corrupt leaf can
109
- * yield fewer messages than the window covers.
110
- */
111
- messages: SessionMessage[];
112
- /** True when older messages were left out to satisfy the byte budget. */
113
- truncated: boolean;
114
- /** Summed stored content size of the FULL path, in bytes. */
115
- totalContentBytes: number;
116
- }
117
- /**
118
- * Session storage provider.
119
- * Messages are tree-structured via parentId for branching.
120
- */
121
- interface SessionProvider {
122
- getMessage(
123
- id: string
124
- ): SessionMessage | null | Promise<SessionMessage | null>;
125
- /**
126
- * Get conversation as a path from root to leaf.
127
- * Applies compaction overlays. If leafId is null, uses the latest leaf.
128
- */
129
- getHistory(
130
- leafId?: string | null
131
- ): SessionMessage[] | Promise<SessionMessage[]>;
132
- getLatestLeaf(): SessionMessage | null | Promise<SessionMessage | null>;
133
- getBranches(messageId: string): SessionMessage[] | Promise<SessionMessage[]>;
134
- getPathLength(leafId?: string | null): number | Promise<number>;
135
- /**
136
- * Optional: byte-budgeted read of the most recent messages on the active
137
- * branch path. Lets hosts hydrate a bounded window instead of the full
138
- * transcript, so wake-time memory scales with the budget rather than total
139
- * session history (#1710). Providers that don't implement it fall back to
140
- * a full `getHistory()` read in `Session.getRecentHistory()`.
141
- *
142
- * `minRecentMessages` (default 1) is a floor on the window size: the most
143
- * recent N rows are always included even when they exceed the byte budget.
144
- * Hosts use this to guarantee the window never shrinks below the recent
145
- * span their model context assembly expects (rows are individually capped
146
- * at write time, so the floor keeps memory bounded).
147
- */
148
- getRecentHistory?(
149
- leafId: string | null | undefined,
150
- maxContentBytes: number,
151
- minRecentMessages?: number
152
- ): RecentHistoryResult | Promise<RecentHistoryResult>;
153
- /**
154
- * Optional: per-row stored sizes for the active branch path (root → leaf),
155
- * WITHOUT loading message content. Lets hosts find oversized rows (e.g.
156
- * inline base64 media) and process them one at a time with bounded memory.
157
- */
158
- getHistoryRowStats?(
159
- leafId?: string | null
160
- ): HistoryRowStat[] | Promise<HistoryRowStat[]>;
161
- /**
162
- * Append a message.
163
- *
164
- * `parentId` semantics:
165
- * - `undefined` / omitted → auto-detect: attach to the current latest leaf.
166
- * - `null` → create a root message with no parent.
167
- * - string → attach to the given parent id (provider may
168
- * fall back to root if the parent doesn't
169
- * belong to this session).
170
- *
171
- * Idempotent — appending the same `message.id` twice is a no-op.
172
- */
173
- appendMessage(
174
- message: SessionMessage,
175
- parentId?: string | null
176
- ): void | Promise<void>;
177
- updateMessage(message: SessionMessage): void | Promise<void>;
178
- deleteMessages(messageIds: string[]): void | Promise<void>;
179
- clearMessages(): void | Promise<void>;
180
- addCompaction(
181
- summary: string,
182
- fromMessageId: string,
183
- toMessageId: string
184
- ): StoredCompaction | Promise<StoredCompaction>;
185
- getCompactions(): StoredCompaction[] | Promise<StoredCompaction[]>;
186
- searchMessages?(
187
- query: string,
188
- limit?: number
189
- ): SearchResult[] | Promise<SearchResult[]>;
190
- }
191
- //#endregion
192
- //#region src/experimental/memory/session/providers/agent.d.ts
193
- interface SqlProvider {
194
- sql<T = Record<string, string | number | boolean | null>>(
195
- strings: TemplateStringsArray,
196
- ...values: (string | number | boolean | null)[]
197
- ): T[];
198
- }
199
- declare class AgentSessionProvider implements SessionProvider {
200
- private agent;
201
- private initialized;
202
- private sessionId;
203
- /**
204
- * Cached id of the active branch tip (latest leaf). `undefined` means "not
205
- * cached" (cold, or last lookup found the session empty).
206
- *
207
- * Finding the tip from scratch is an anti-join over every row in the
208
- * session (`latestLeafRow`), which is O(rows). It runs on every hydration
209
- * AND every auto-parent append, so on a long transcript it dominates the
210
- * read cost of a wake. The tip is maintained in place on append/delete/
211
- * clear, and a cached id is re-validated on read with an O(1) existence +
212
- * still-childless check before it's trusted — so the cache self-heals if
213
- * something else mutates the cached tip: a deleted tip or a tip that gained
214
- * a child fails the check and triggers a single recompute. Direct SQL or a
215
- * second provider instance that creates a newer leaf without touching the
216
- * cached tip is outside the supported writer model and will be observed on
217
- * the next cold lookup. The full scan therefore never runs more often than
218
- * the original unconditional version did. Reads are synchronous and the DO
219
- * is single-threaded, so no locking is needed.
220
- */
221
- private activeLeafId;
222
- /**
223
- * @param agent - Agent or any object with a `sql` tagged template method
224
- * @param sessionId - Optional session ID to isolate multiple sessions in the same DO.
225
- * Messages are filtered by session_id within shared tables.
226
- */
227
- constructor(agent: SqlProvider, sessionId?: string);
228
- private ensureTable;
229
- getMessage(id: string): SessionMessage | null;
230
- getHistory(leafId?: string | null): SessionMessage[];
231
- getRecentHistory(
232
- leafId: string | null | undefined,
233
- maxContentBytes: number,
234
- minRecentMessages?: number
235
- ): RecentHistoryResult;
236
- getHistoryRowStats(leafId?: string | null): HistoryRowStat[];
237
- getLatestLeaf(): SessionMessage | null;
238
- getBranches(messageId: string): SessionMessage[];
239
- getPathLength(leafId?: string | null): number;
240
- appendMessage(message: SessionMessage, parentId?: string | null): void;
241
- updateMessage(message: SessionMessage): void;
242
- deleteMessages(messageIds: string[]): void;
243
- clearMessages(): void;
244
- addCompaction(
245
- summary: string,
246
- fromMessageId: string,
247
- toMessageId: string
248
- ): StoredCompaction;
249
- getCompactions(): StoredCompaction[];
250
- searchMessages(query: string, limit?: number): SearchResult[];
251
- private latestLeafRow;
252
- private leafRowById;
253
- /**
254
- * The active branch path as (id, role, content size) rows, root → leaf.
255
- *
256
- * Recurses over (id, parent_id) only. Carrying `content` through the
257
- * recursive queue AND the ORDER BY sorter materializes the entire
258
- * transcript several times over inside SQLite's allocator, which in
259
- * workerd shares the isolate's memory budget with the JS heap — large
260
- * media-heavy sessions then fail with SQLITE_NOMEM on wake (#1710).
261
- * Content is fetched separately in bounded chunks (`messagesByPathStats`).
262
- */
263
- private pathRowStats;
264
- /**
265
- * Fetch and parse message content for an ordered list of path rows.
266
- *
267
- * Content is read in chunks bounded by both row count and cumulative
268
- * stored bytes (no ORDER BY — SQLite streams rows without materializing
269
- * the result set) and reassembled in path order. Rows that fail to parse
270
- * are skipped, matching previous behavior.
271
- */
272
- private messagesByPathStats;
273
- private indexFTS;
274
- private deleteFTS;
275
- private applyCompactions;
276
- private parse;
277
- private parseRows;
278
- }
279
- //#endregion
280
- //#region src/experimental/memory/session/search.d.ts
281
- /**
282
- * Storage interface for searchable context.
283
- *
284
- * - `get()` returns a summary of indexed content (rendered into system prompt)
285
- * - `search(query)` full-text search (via search_context tool)
286
- * - `set(key, content)` indexes content under a key (via set_context tool)
287
- */
288
- interface SearchProvider extends ContextProvider {
289
- search(query: string): Promise<string | null>;
290
- set?(key: string, content: string): Promise<void>;
291
- }
292
- /**
293
- * Check if a provider is a SearchProvider (has a `search` method).
294
- */
295
- declare function isSearchProvider(
296
- provider: unknown
297
- ): provider is SearchProvider;
298
- /**
299
- * SearchProvider backed by Durable Object SQLite with FTS5.
300
- *
301
- * - `get()` returns a count of indexed entries
302
- * - `search(query)` full-text search using FTS5
303
- * - `set(key, content)` indexes or replaces content under a key
304
- *
305
- * Each instance uses a namespaced FTS5 table to avoid collisions
306
- * with the session message search.
307
- *
308
- * @example
309
- * ```ts
310
- * Session.create(this)
311
- * .withContext("knowledge", {
312
- * provider: new AgentSearchProvider(this)
313
- * })
314
- * ```
315
- */
316
- declare class AgentSearchProvider implements SearchProvider {
317
- private agent;
318
- private label;
319
- private initialized;
320
- constructor(agent: SqlProvider);
321
- init(label: string): void;
322
- private ensureTable;
323
- get(): Promise<string | null>;
324
- search(query: string): Promise<string | null>;
325
- set(key: string, content: string): Promise<void>;
326
- private deleteFTS;
327
- }
328
- //#endregion
329
- //#region src/experimental/memory/session/skills.d.ts
330
- /**
331
- * Storage interface for skill collections.
332
- *
333
- * - `get()` returns metadata listing (rendered into system prompt)
334
- * - `load(key)` fetches full content (via load_context tool)
335
- * - `set(key, content, description?)` writes an entry (via set_context tool)
336
- */
337
- interface SkillProvider extends ContextProvider {
338
- load(key: string): Promise<string | null>;
339
- set?(key: string, content: string, description?: string): Promise<void>;
340
- }
341
- /**
342
- * Check if a provider is a SkillProvider (has a `load` method).
343
- */
344
- declare function isSkillProvider(provider: unknown): provider is SkillProvider;
345
- /**
346
- * SkillProvider backed by an R2 bucket.
347
- *
348
- * - `get()` returns a metadata listing of all skills (key + description)
349
- * - `load(key)` fetches a skill's full content
350
- * - `set(key, content, description?)` writes a skill
351
- *
352
- * Descriptions are pulled from R2 custom metadata (`description` key).
353
- * If a prefix is provided, it is prepended on storage operations and
354
- * stripped from keys in metadata. `keys`, when provided, is matched against
355
- * these prefix-relative keys.
356
- *
357
- * @example
358
- * ```ts
359
- * const skills = new R2SkillProvider(env.SKILLS_BUCKET, {
360
- * prefix: "skills/",
361
- * keys: ["code-review", "debugging"]
362
- * });
363
- * ```
364
- */
365
- declare class R2SkillProvider implements SkillProvider {
366
- private bucket;
367
- private prefix;
368
- private keys;
369
- constructor(
370
- bucket: R2Bucket,
371
- options?: {
372
- prefix?: string;
373
- keys?: string[];
374
- }
375
- );
376
- get(): Promise<string | null>;
377
- load(key: string): Promise<string | null>;
378
- set(key: string, content: string, description?: string): Promise<void>;
379
- private allowsKey;
380
- }
381
- //#endregion
382
- //#region src/experimental/memory/session/context.d.ts
383
- /**
384
- * Base storage interface for a context block.
385
- * A provider with only `get()` is readonly.
386
- */
387
- interface ContextProvider {
388
- get(): Promise<string | null>;
389
- /** Called by the context system to provide the block label before first use. */
390
- init?(label: string): void;
391
- }
392
- /**
393
- * Writable context provider — extends ContextProvider with `set()`.
394
- * Blocks backed by this provider are writable via the `set_context` tool.
395
- */
396
- interface WritableContextProvider extends ContextProvider {
397
- set(content: string): Promise<void>;
398
- }
399
- /**
400
- * Check if a provider is writable (has a `set` method).
401
- */
402
- declare function isWritableProvider(
403
- provider: unknown
404
- ): provider is WritableContextProvider;
405
- /**
406
- * Configuration for a context block.
407
- */
408
- interface ContextConfig {
409
- /** Block label — used as key and in tool descriptions */
410
- label: string;
411
- /** Human-readable description (shown to AI in tool) */
412
- description?: string;
413
- /** Maximum tokens allowed. Enforced on set. */
414
- maxTokens?: number;
415
- /** Storage provider. Determines block behavior:
416
- * - ContextProvider (get only) → readonly
417
- * - WritableContextProvider (get+set) → writable via set_context
418
- * - SkillProvider (get+load+set?) → on-demand via load_context
419
- * - SearchProvider (get+search+set?) → searchable via search_context
420
- * If omitted, auto-wired to writable SQLite when using builder. */
421
- provider?:
422
- | ContextProvider
423
- | WritableContextProvider
424
- | SkillProvider
425
- | SearchProvider;
426
- }
427
- /**
428
- * A loaded context block with computed token count.
429
- */
430
- interface ContextBlock {
431
- label: string;
432
- description?: string;
433
- content: string;
434
- tokens: number;
435
- maxTokens?: number;
436
- /** True if provider is writable (has set) */
437
- writable: boolean;
438
- /** True if backed by a SkillProvider */
439
- isSkill: boolean;
440
- /** True if backed by a SearchProvider */
441
- isSearchable: boolean;
442
- }
443
- //#endregion
444
- //#region src/experimental/memory/utils/compaction-helpers.d.ts
445
- type CompactTokenCounter = (
446
- messages: SessionMessage[]
447
- ) => number | Promise<number>;
448
- /** Prefix for all compaction messages (overlays and summaries) */
449
- declare const COMPACTION_PREFIX = "compaction_";
450
- /** Check if a message is a compaction message */
451
- declare function isCompactionMessage(msg: SessionMessage): boolean;
452
- /**
453
- * Align a boundary index forward to avoid splitting tool call/result groups.
454
- * If the boundary falls between an assistant message with tool calls and its
455
- * tool results, move it forward past the results.
456
- */
457
- declare function alignBoundaryForward(
458
- messages: SessionMessage[],
459
- idx: number
460
- ): number;
461
- /**
462
- * Align a boundary index backward to avoid splitting tool call/result groups.
463
- * If the boundary falls in the middle of tool results, move it backward to
464
- * include the assistant message that made the calls.
465
- */
466
- declare function alignBoundaryBackward(
467
- messages: SessionMessage[],
468
- idx: number
469
- ): number;
470
- /**
471
- * Find the compression end boundary using a token budget for the tail.
472
- * Walks backward from the end, accumulating tokens until budget is reached.
473
- * Returns the index where compression should stop (everything from this
474
- * index onward is protected).
475
- *
476
- * @param messages All messages
477
- * @param headEnd Index where the protected head ends (compression starts here)
478
- * @param tailTokenBudget Maximum tokens to keep in the tail
479
- * @param minTailMessages Minimum messages to protect in the tail (fallback)
480
- */
481
- declare function findTailCutByTokens(
482
- messages: SessionMessage[],
483
- headEnd: number,
484
- tailTokenBudget?: number,
485
- minTailMessages?: number
486
- ): number;
487
- /**
488
- * Fix orphaned tool call/result pairs after compaction.
489
- *
490
- * Two failure modes:
491
- * 1. Tool result references a call_id whose assistant tool_call was removed
492
- * → Remove the orphaned result
493
- * 2. Assistant has tool_calls whose results were dropped
494
- * → Add stub results so the API doesn't error
495
- *
496
- * @param messages Messages after compaction
497
- * @returns Sanitized messages with no orphaned pairs
498
- */
499
- declare function sanitizeToolPairs(
500
- messages: SessionMessage[]
501
- ): SessionMessage[];
502
- /**
503
- * Compute a summary token budget based on the content being compressed.
504
- * 20% of the compressed content, clamped to 2K-8K tokens.
505
- */
506
- declare function computeSummaryBudget(messages: SessionMessage[]): number;
507
- /**
508
- * Build a prompt for LLM summarization of compressed messages.
509
- *
510
- * @param messages Messages to summarize
511
- * @param previousSummary Previous summary for iterative updates (or null for first compaction)
512
- * @param budget Target token count for the summary
513
- */
514
- declare function buildSummaryPrompt(
515
- messages: SessionMessage[],
516
- previousSummary: string | null,
517
- budget: number
518
- ): string;
519
- /**
520
- * Result of a compaction function — describes the overlay to store.
521
- */
522
- interface CompactResult {
523
- /** First message ID in the compacted range */
524
- fromMessageId: string;
525
- /** Last message ID in the compacted range */
526
- toMessageId: string;
527
- /** Summary text to store as the overlay */
528
- summary: string;
529
- }
530
- interface CompactOptions {
531
- /**
532
- * Function to call the LLM for summarization.
533
- * Takes a user prompt string, returns the LLM's text response.
534
- */
535
- summarize: (prompt: string) => Promise<string>;
536
- /** Number of head messages to protect (default: 2) */
537
- protectHead?: number;
538
- /** Token budget for tail protection (default: 20000) */
539
- tailTokenBudget?: number;
540
- /** Minimum tail messages to protect (default: 2) */
541
- minTailMessages?: number;
542
- /**
543
- * Optional counter for tail-budget decisions. Use this when a tokenizer or
544
- * model-reported accounting is available; otherwise the Workers-safe
545
- * heuristic is used.
546
- */
547
- tokenCounter?: CompactTokenCounter;
548
- }
549
- /**
550
- * Reference compaction implementation.
551
- *
552
- * Implements the full hermes-style compaction algorithm:
553
- * 1. Protect head messages (first N)
554
- * 2. Protect tail by token budget (walk backward)
555
- * 3. Align boundaries to tool call groups
556
- * 4. Summarize middle section with LLM (structured format)
557
- * 5. Sanitize orphaned tool pairs
558
- * 6. Iterative summary updates on subsequent compactions
559
- *
560
- * @example
561
- * ```typescript
562
- * import { createCompactFunction } from "agents/experimental/memory/utils";
563
- *
564
- * const session = new Session(provider, {
565
- * compaction: {
566
- * tokenThreshold: 100000,
567
- * fn: createCompactFunction({
568
- * summarize: (prompt) => generateText({ model, prompt }).then(r => r.text)
569
- * })
570
- * }
571
- * });
572
- * ```
573
- */
574
- declare function createCompactFunction(
575
- opts: CompactOptions
576
- ): (
577
- messages: SessionMessage[],
578
- context?: CompactContext
579
- ) => Promise<CompactResult | null>;
580
- //#endregion
581
- export {
582
- StoredCompaction as A,
583
- isSearchProvider as C,
584
- RecentHistoryResult as D,
585
- HistoryRowStat as E,
586
- SessionMessagePart as F,
587
- SessionOptions as I,
588
- SessionTokenCounter as L,
589
- CompactContext as M,
590
- CompactionErrorHandler as N,
591
- SearchResult as O,
592
- SessionMessage as P,
593
- SessionTokenCounterInput as R,
594
- SearchProvider as S,
595
- SqlProvider as T,
596
- isWritableProvider as _,
597
- alignBoundaryBackward as a,
598
- isSkillProvider as b,
599
- computeSummaryBudget as c,
600
- isCompactionMessage as d,
601
- sanitizeToolPairs as f,
602
- WritableContextProvider as g,
603
- ContextProvider as h,
604
- CompactTokenCounter as i,
605
- CompactAfterOptions as j,
606
- SessionProvider as k,
607
- createCompactFunction as l,
608
- ContextConfig as m,
609
- CompactOptions as n,
610
- alignBoundaryForward as o,
611
- ContextBlock as p,
612
- CompactResult as r,
613
- buildSummaryPrompt as s,
614
- COMPACTION_PREFIX as t,
615
- findTailCutByTokens as u,
616
- R2SkillProvider as v,
617
- AgentSessionProvider as w,
618
- AgentSearchProvider as x,
619
- SkillProvider as y
620
- };
621
- //# sourceMappingURL=compaction-helpers-wUz6M3us.d.ts.map