agents 0.8.5 → 0.8.7

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 (40) hide show
  1. package/dist/client.d.ts +2 -2
  2. package/dist/compaction-helpers-BFTBIzpK.js +312 -0
  3. package/dist/compaction-helpers-BFTBIzpK.js.map +1 -0
  4. package/dist/compaction-helpers-DkJreaDR.d.ts +139 -0
  5. package/dist/{do-oauth-client-provider-D7F2Pw40.d.ts → do-oauth-client-provider-C2jurFjW.d.ts} +1 -1
  6. package/dist/{email-YAQhwwXb.d.ts → email-DwPlM0bQ.d.ts} +1 -1
  7. package/dist/email.d.ts +2 -2
  8. package/dist/experimental/forever.d.ts +1 -1
  9. package/dist/experimental/memory/session/index.d.ts +193 -203
  10. package/dist/experimental/memory/session/index.js +673 -294
  11. package/dist/experimental/memory/session/index.js.map +1 -1
  12. package/dist/experimental/memory/utils/index.d.ts +59 -0
  13. package/dist/experimental/memory/utils/index.js +69 -0
  14. package/dist/experimental/memory/utils/index.js.map +1 -0
  15. package/dist/{index-DynYigzs.d.ts → index-C-6EMK-E.d.ts} +11 -11
  16. package/dist/{index-OtkSCU2A.d.ts → index-Ua2Nfvbm.d.ts} +1 -1
  17. package/dist/index.d.ts +7 -5
  18. package/dist/index.js +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/{internal_context-DgcmHqS1.d.ts → internal_context-DT8RxmAN.d.ts} +1 -1
  21. package/dist/internal_context.d.ts +1 -1
  22. package/dist/mcp/client.d.ts +1 -1
  23. package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
  24. package/dist/mcp/index.d.ts +1 -1
  25. package/dist/mcp/index.js +45 -3
  26. package/dist/mcp/index.js.map +1 -1
  27. package/dist/observability/index.d.ts +1 -1
  28. package/dist/react.d.ts +1 -1
  29. package/dist/{retries-JxhDYtYL.d.ts → retries-DXMQGhG3.d.ts} +1 -1
  30. package/dist/retries.d.ts +1 -1
  31. package/dist/{serializable-Ch19yA6_.d.ts → serializable-8Jt1B04R.d.ts} +1 -1
  32. package/dist/serializable.d.ts +1 -1
  33. package/dist/{types-2lHHE_uh.d.ts → types-C-m0II8i.d.ts} +3 -1
  34. package/dist/types.d.ts +1 -1
  35. package/dist/types.js +2 -0
  36. package/dist/types.js.map +1 -1
  37. package/dist/{workflow-types-BVKtSaA7.d.ts → workflow-types-CZNXKj_D.d.ts} +1 -1
  38. package/dist/workflow-types.d.ts +1 -1
  39. package/dist/workflows.d.ts +2 -2
  40. package/package.json +12 -7
@@ -1,251 +1,241 @@
1
- import { UIMessage } from "ai";
1
+ import { r as CompactResult } from "../../../compaction-helpers-DkJreaDR.js";
2
+ import { ToolSet, UIMessage } from "ai";
2
3
 
3
- //#region src/experimental/memory/session/types.d.ts
4
+ //#region src/experimental/memory/session/context.d.ts
4
5
  /**
5
- * Options for querying messages
6
+ * Storage interface for a single context block.
7
+ * Each block can have its own backing store (R2, SQLite, KV, in-memory, etc.)
8
+ * If `set` is omitted, the block is readonly.
6
9
  */
7
- interface MessageQueryOptions {
8
- /** Maximum number of messages to return */
9
- limit?: number;
10
- /** Number of messages to skip */
11
- offset?: number;
12
- /** Only return messages created before this timestamp */
13
- before?: Date;
14
- /** Only return messages created after this timestamp */
15
- after?: Date;
16
- /** Filter by role */
17
- role?: "user" | "assistant" | "system";
10
+ interface ContextProvider {
11
+ get(): Promise<string | null>;
12
+ set?(content: string): Promise<void>;
18
13
  }
19
14
  /**
20
- * Granular microCompaction rules.
21
- * Each rule can be true (use default), false (disable), or a number (custom threshold).
15
+ * Configuration for a context block.
22
16
  */
23
- interface MicroCompactionRules {
24
- /**
25
- * Truncate tool outputs over this size (in chars).
26
- * @default 30000 chars
27
- */
28
- truncateToolOutputs?: boolean | number;
29
- /**
30
- * Truncate text parts over this size in older messages (in chars).
31
- * @default 10000 chars
32
- */
33
- truncateText?: boolean | number;
34
- /**
35
- * Number of recent messages to keep intact (not truncated).
36
- * @default 4
37
- */
38
- keepRecent?: number;
17
+ interface ContextConfig {
18
+ /** Block label — used as key and in tool descriptions */
19
+ label: string;
20
+ /** Human-readable description (shown to AI in tool) */
21
+ description?: string;
22
+ /** Initial content — used when provider returns null or is absent */
23
+ initialContent?: string;
24
+ /** Maximum tokens allowed. Enforced on set. */
25
+ maxTokens?: number;
26
+ /** If true, AI cannot modify this block via tools */
27
+ readonly?: boolean;
28
+ /** Storage provider. If omitted, auto-wired to SQLite when using builder. */
29
+ provider?: ContextProvider;
39
30
  }
40
31
  /**
41
- * Compaction function - user implements this to decide how to compact messages.
42
- * Could summarize with LLM, truncate, filter, or anything else.
43
- *
44
- * @param messages Current messages in the session
45
- * @returns New messages to replace the current ones
32
+ * A loaded context block with computed token count.
46
33
  */
47
- type CompactFunction = (messages: UIMessage[]) => Promise<UIMessage[]>;
48
- /**
49
- * Configuration for full compaction (LLM summarization)
50
- */
51
- interface CompactionConfig {
52
- /**
53
- * Token threshold for automatic compaction.
54
- * When estimated tokens exceed this, compact() is called automatically on append().
55
- * If not set, auto-compaction is disabled (you can still call compact() manually).
56
- */
57
- tokenThreshold?: number;
58
- /**
59
- * Function to compact messages.
60
- * Receives current messages as stored, returns new messages.
61
- */
62
- fn: CompactFunction;
34
+ interface ContextBlock {
35
+ label: string;
36
+ description?: string;
37
+ content: string;
38
+ tokens: number;
39
+ maxTokens?: number;
40
+ readonly?: boolean;
63
41
  }
42
+ //#endregion
43
+ //#region src/experimental/memory/session/types.d.ts
64
44
  /**
65
- * Result of compaction operation
45
+ * Options for querying messages
66
46
  */
67
- interface CompactResult {
68
- /** Whether compaction succeeded */
69
- success: boolean;
70
- /** Error message if compaction failed */
71
- error?: string;
47
+ interface MessageQueryOptions {
48
+ limit?: number;
49
+ offset?: number;
50
+ before?: Date;
51
+ after?: Date;
52
+ role?: "user" | "assistant" | "system";
72
53
  }
73
54
  /**
74
- * Options for creating a session provider
55
+ * Options for creating a Session.
75
56
  */
76
- interface SessionProviderOptions {
77
- /**
78
- * Lightweight compaction that doesn't require LLM calls.
79
- * Truncates tool outputs and long text in older messages.
80
- *
81
- * Runs automatically on every `append()` — older messages (beyond `keepRecent`)
82
- * are truncated in storage.
83
- * This is a destructive operation: original content is permanently replaced.
84
- * `getMessages()` returns stored content as-is (already compacted).
85
- *
86
- * - `true` - enable with default rules
87
- * - `false` - disable
88
- * - `{ ... }` - enable with custom rules
89
- *
90
- * @default true
91
- */
92
- microCompaction?: boolean | MicroCompactionRules;
93
- /**
94
- * Full compaction with custom function (typically LLM summarization).
95
- */
96
- compaction?: CompactionConfig;
57
+ interface SessionOptions {
58
+ /** Context blocks for the system prompt. */
59
+ context?: ContextConfig[];
60
+ /** Provider for persisting the frozen system prompt. */
61
+ promptStore?: ContextProvider;
97
62
  }
98
63
  //#endregion
99
64
  //#region src/experimental/memory/session/provider.d.ts
65
+ interface SearchResult {
66
+ id: string;
67
+ role: string;
68
+ content: string;
69
+ createdAt?: string;
70
+ sessionId?: string;
71
+ }
72
+ interface StoredCompaction {
73
+ id: string;
74
+ summary: string;
75
+ fromMessageId: string;
76
+ toMessageId: string;
77
+ createdAt: string;
78
+ }
100
79
  /**
101
- * Session storage provider interface.
102
- *
103
- * Implement this interface to create custom session storage backends.
104
- * Providers handle CRUD only — compaction is orchestrated by the Session wrapper.
80
+ * Session storage provider.
81
+ * Messages are tree-structured via parentId for branching.
105
82
  */
106
83
  interface SessionProvider {
107
- /**
108
- * Get messages with optional filtering
109
- */
110
- getMessages(options?: MessageQueryOptions): UIMessage[];
111
- /**
112
- * Get a single message by ID
113
- */
114
84
  getMessage(id: string): UIMessage | null;
115
85
  /**
116
- * Get the last N messages (most recent)
117
- */
118
- getLastMessages(n: number): UIMessage[];
119
- /**
120
- * Append one or more messages to storage.
86
+ * Get conversation as a path from root to leaf.
87
+ * Applies compaction overlays. If leafId is null, uses the latest leaf.
121
88
  */
122
- appendMessages(messages: UIMessage | UIMessage[]): Promise<void>;
89
+ getHistory(leafId?: string | null): UIMessage[];
90
+ getLatestLeaf(): UIMessage | null;
91
+ getBranches(messageId: string): UIMessage[];
92
+ getPathLength(leafId?: string | null): number;
123
93
  /**
124
- * Update an existing message
94
+ * Append a message. Parented to the latest leaf unless parentId is provided.
95
+ * Idempotent — same message.id twice is a no-op.
125
96
  */
97
+ appendMessage(message: UIMessage, parentId?: string | null): void;
126
98
  updateMessage(message: UIMessage): void;
127
- /**
128
- * Delete messages by ID
129
- */
130
99
  deleteMessages(messageIds: string[]): void;
131
- /**
132
- * Clear all messages
133
- */
134
100
  clearMessages(): void;
135
- /**
136
- * Fetch messages outside the recent window (for microCompaction).
137
- * Returns all messages except the most recent `keepRecent`.
138
- */
139
- getOlderMessages(keepRecent: number): UIMessage[];
140
- /**
141
- * Bulk replace all messages (used by compact).
142
- * Clears existing messages and inserts the new ones,
143
- * preserving original created_at timestamps where possible.
144
- */
145
- replaceMessages(messages: UIMessage[]): Promise<void>;
146
- }
147
- //#endregion
148
- //#region src/experimental/memory/session/session.d.ts
149
- declare class Session {
150
- private storage;
151
- private microCompactionRules;
152
- private compactionConfig;
153
- constructor(storage: SessionProvider, options?: SessionProviderOptions);
154
- getMessages(options?: MessageQueryOptions): UIMessage[];
155
- getMessage(id: string): UIMessage | null;
156
- getLastMessages(n: number): UIMessage[];
157
- append(messages: UIMessage | UIMessage[]): Promise<void>;
158
- updateMessage(message: UIMessage): void;
159
- deleteMessages(messageIds: string[]): void;
160
- clearMessages(): void;
161
- compact(): Promise<CompactResult>;
162
- /**
163
- * Pre-check for auto-compaction using token estimate heuristic.
164
- */
165
- private shouldAutoCompact;
101
+ addCompaction(summary: string, fromMessageId: string, toMessageId: string): StoredCompaction;
102
+ getCompactions(): StoredCompaction[];
103
+ searchMessages?(query: string, limit?: number): SearchResult[];
166
104
  }
167
105
  //#endregion
168
106
  //#region src/experimental/memory/session/providers/agent.d.ts
169
- /**
170
- * Interface for objects that provide a sql tagged template method.
171
- * This matches the Agent class's sql method signature.
172
- */
173
107
  interface SqlProvider {
174
108
  sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
175
109
  }
176
- /**
177
- * Session provider that wraps an Agent's SQLite storage.
178
- * Provides pure CRUD — compaction is handled by the Session wrapper.
179
- *
180
- * @example
181
- * ```typescript
182
- * import { Session, AgentSessionProvider } from "agents/experimental/memory/session";
183
- *
184
- * // In your Agent class:
185
- * session = new Session(new AgentSessionProvider(this));
186
- *
187
- * // With compaction options:
188
- * session = new Session(new AgentSessionProvider(this), {
189
- * microCompaction: { truncateToolOutputs: 2000, keepRecent: 10 },
190
- * compaction: { tokenThreshold: 20000, fn: summarize }
191
- * });
192
- * ```
193
- */
194
110
  declare class AgentSessionProvider implements SessionProvider {
195
111
  private agent;
196
112
  private initialized;
197
- constructor(agent: SqlProvider);
113
+ private sessionId;
198
114
  /**
199
- * Ensure the messages table exists
115
+ * @param agent - Agent or any object with a `sql` tagged template method
116
+ * @param sessionId - Optional session ID to isolate multiple sessions in the same DO.
117
+ * Messages are filtered by session_id within shared tables.
200
118
  */
119
+ constructor(agent: SqlProvider, sessionId?: string);
201
120
  private ensureTable;
202
- /**
203
- * Get all messages in AI SDK format
204
- */
205
- getMessages(options?: MessageQueryOptions): UIMessage[];
206
- /**
207
- * Append one or more messages to storage.
208
- */
209
- appendMessages(messages: UIMessage | UIMessage[]): Promise<void>;
210
- /**
211
- * Update an existing message
212
- */
121
+ getMessage(id: string): UIMessage | null;
122
+ getHistory(leafId?: string | null): UIMessage[];
123
+ getLatestLeaf(): UIMessage | null;
124
+ getBranches(messageId: string): UIMessage[];
125
+ getPathLength(leafId?: string | null): number;
126
+ appendMessage(message: UIMessage, parentId?: string | null): void;
213
127
  updateMessage(message: UIMessage): void;
214
- /**
215
- * Delete messages by their IDs
216
- */
217
128
  deleteMessages(messageIds: string[]): void;
218
- /**
219
- * Clear all messages from the session
220
- */
221
129
  clearMessages(): void;
222
- /**
223
- * Get a single message by ID
224
- */
225
- getMessage(id: string): UIMessage | null;
226
- /**
227
- * Get the last N messages (most recent)
228
- */
229
- getLastMessages(n: number): UIMessage[];
230
- /**
231
- * Fetch messages outside the recent window (for microCompaction).
232
- * Returns all messages except the most recent `keepRecent`.
233
- */
234
- getOlderMessages(keepRecent: number): UIMessage[];
235
- /**
236
- * Bulk replace all messages.
237
- * Preserves original created_at timestamps for surviving messages.
238
- */
239
- replaceMessages(messages: UIMessage[]): Promise<void>;
240
- /**
241
- * Validate message structure
242
- */
243
- private isValidMessage;
244
- /**
245
- * Parse message rows from SQL results into UIMessages.
246
- */
130
+ addCompaction(summary: string, fromMessageId: string, toMessageId: string): StoredCompaction;
131
+ getCompactions(): StoredCompaction[];
132
+ searchMessages(query: string, limit?: number): SearchResult[];
133
+ private latestLeafRow;
134
+ private indexFTS;
135
+ private deleteFTS;
136
+ private applyCompactions;
137
+ private parse;
247
138
  private parseRows;
248
139
  }
249
140
  //#endregion
250
- export { AgentSessionProvider, type CompactFunction, type CompactResult, type CompactionConfig, type MessageQueryOptions, type MicroCompactionRules, Session, type SessionProvider, type SessionProviderOptions, type SqlProvider };
141
+ //#region src/experimental/memory/session/session.d.ts
142
+ type SessionContextOptions = Omit<ContextConfig, "label">;
143
+ declare class Session {
144
+ private storage;
145
+ private context;
146
+ private _agent?;
147
+ private _broadcaster?;
148
+ private _sessionId?;
149
+ private _pending?;
150
+ private _cachedPrompt?;
151
+ private _compactionFn?;
152
+ private _tokenThreshold?;
153
+ private _ready;
154
+ constructor(storage: SessionProvider, options?: SessionOptions);
155
+ /**
156
+ * Chainable session creation with auto-wired SQLite providers.
157
+ * Chain methods in any order — providers are resolved lazily on first use.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * const session = Session.create(this)
162
+ * .withContext("soul", { initialContent: "You are helpful.", readonly: true })
163
+ * .withContext("memory", { description: "Learned facts", maxTokens: 1100 })
164
+ * .withCachedPrompt();
165
+ *
166
+ * // Custom storage (R2, KV, etc.)
167
+ * const session = Session.create(this)
168
+ * .withContext("workspace", {
169
+ * provider: {
170
+ * get: () => env.BUCKET.get("ws.md").then(o => o?.text() ?? null),
171
+ * set: (c) => env.BUCKET.put("ws.md", c),
172
+ * }
173
+ * })
174
+ * .withCachedPrompt();
175
+ * ```
176
+ */
177
+ static create(agent: SqlProvider): Session;
178
+ forSession(sessionId: string): this;
179
+ withContext(label: string, options?: SessionContextOptions): this;
180
+ withCachedPrompt(provider?: ContextProvider): this;
181
+ /**
182
+ * Register a compaction function. Called by `compact()` to compress
183
+ * message history into a summary overlay.
184
+ */
185
+ onCompaction(fn: (messages: UIMessage[]) => Promise<CompactResult | null>): this;
186
+ /**
187
+ * Auto-compact when estimated token count exceeds the threshold.
188
+ * Checked after each `appendMessage`. Requires `onCompaction()`.
189
+ */
190
+ compactAfter(tokenThreshold: number): this;
191
+ private _ensureReady;
192
+ getHistory(leafId?: string | null): UIMessage[];
193
+ getMessage(id: string): UIMessage | null;
194
+ getLatestLeaf(): UIMessage | null;
195
+ getBranches(messageId: string): UIMessage[];
196
+ getPathLength(leafId?: string | null): number;
197
+ private _broadcast;
198
+ private _emitStatus;
199
+ private _emitError;
200
+ appendMessage(message: UIMessage, parentId?: string | null): Promise<void>;
201
+ updateMessage(message: UIMessage): void;
202
+ deleteMessages(messageIds: string[]): void;
203
+ clearMessages(): void;
204
+ addCompaction(summary: string, fromMessageId: string, toMessageId: string): StoredCompaction;
205
+ getCompactions(): StoredCompaction[];
206
+ /**
207
+ * Run the registered compaction function and store the result as an overlay.
208
+ * Requires `onCompaction()` to be called first.
209
+ */
210
+ compact(): Promise<CompactResult | null>;
211
+ getContextBlock(label: string): ContextBlock | null;
212
+ getContextBlocks(): ContextBlock[];
213
+ replaceContextBlock(label: string, content: string): Promise<ContextBlock>;
214
+ appendContextBlock(label: string, content: string): Promise<ContextBlock>;
215
+ freezeSystemPrompt(): Promise<string>;
216
+ refreshSystemPrompt(): Promise<string>;
217
+ search(query: string, options?: {
218
+ limit?: number;
219
+ }): Array<{
220
+ id: string;
221
+ role: string;
222
+ content: string;
223
+ createdAt?: string;
224
+ }>;
225
+ /** Returns update_context tool for writing to context blocks. */
226
+ tools(): Promise<ToolSet>;
227
+ }
228
+ //#endregion
229
+ //#region src/experimental/memory/session/providers/agent-context.d.ts
230
+ declare class AgentContextProvider implements ContextProvider {
231
+ private agent;
232
+ private label;
233
+ private initialized;
234
+ constructor(agent: SqlProvider, label: string);
235
+ private ensureTable;
236
+ get(): Promise<string | null>;
237
+ set(content: string): Promise<void>;
238
+ }
239
+ //#endregion
240
+ export { AgentContextProvider, AgentSessionProvider, type ContextBlock, type ContextConfig, type MessageQueryOptions, type SearchResult, Session, type SessionContextOptions, type SessionOptions, type SessionProvider, type SqlProvider, type StoredCompaction };
251
241
  //# sourceMappingURL=index.d.ts.map