agents 0.12.4 → 0.13.1

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 (62) hide show
  1. package/README.md +8 -8
  2. package/dist/{agent-tool-types-CM_50fcV.d.ts → agent-tool-types-Dn9n-3SI.d.ts} +234 -85
  3. package/dist/agent-tool-types.d.ts +1 -1
  4. package/dist/{agent-tools-BylX6WXG.d.ts → agent-tools-B1ttU-pq.d.ts} +2 -2
  5. package/dist/agent-tools-BAdX1vdI.js.map +1 -1
  6. package/dist/agent-tools.d.ts +1 -1
  7. package/dist/agent-tools.js.map +1 -1
  8. package/dist/ai-chat-agent.js.map +1 -1
  9. package/dist/ai-chat-v5-migration.js.map +1 -1
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/ai-types.js.map +1 -1
  12. package/dist/browser/ai.js +1 -1
  13. package/dist/browser/ai.js.map +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/tanstack-ai.js +1 -1
  16. package/dist/browser/tanstack-ai.js.map +1 -1
  17. package/dist/chat/index.d.ts +2 -2
  18. package/dist/chat/index.js.map +1 -1
  19. package/dist/{classPrivateFieldGet2-CS51BNGR.js → classPrivateFieldGet2-Evpt0SEr.js} +5 -5
  20. package/dist/cli/index.js.map +1 -1
  21. package/dist/client-D1kFXo80.js.map +1 -1
  22. package/dist/client.d.ts +1 -1
  23. package/dist/client.js.map +1 -1
  24. package/dist/codemode/ai.js.map +1 -1
  25. package/dist/{compaction-helpers-bYvP1o2S.d.ts → compaction-helpers-DAe-xiVY.d.ts} +33 -15
  26. package/dist/compaction-helpers-DvcZnvQ1.js.map +1 -1
  27. package/dist/email.d.ts +1 -1
  28. package/dist/email.js.map +1 -1
  29. package/dist/experimental/memory/session/index.d.ts +247 -34
  30. package/dist/experimental/memory/session/index.js +540 -135
  31. package/dist/experimental/memory/session/index.js.map +1 -1
  32. package/dist/experimental/memory/utils/index.d.ts +1 -1
  33. package/dist/experimental/memory/utils/index.js.map +1 -1
  34. package/dist/experimental/webmcp.js.map +1 -1
  35. package/dist/index.d.ts +71 -57
  36. package/dist/index.js +583 -45
  37. package/dist/index.js.map +1 -1
  38. package/dist/internal_context.js.map +1 -1
  39. package/dist/mcp/client.d.ts +12 -12
  40. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  41. package/dist/mcp/index.d.ts +40 -40
  42. package/dist/mcp/index.js +21 -45
  43. package/dist/mcp/index.js.map +1 -1
  44. package/dist/mcp/x402.js.map +1 -1
  45. package/dist/observability/index.js.map +1 -1
  46. package/dist/react.d.ts +3 -3
  47. package/dist/react.js.map +1 -1
  48. package/dist/retries.js.map +1 -1
  49. package/dist/schedule.js.map +1 -1
  50. package/dist/serializable.d.ts +1 -1
  51. package/dist/{shared-DzJYHisH.js → shared-CiKaIK4h.js} +4 -5
  52. package/dist/{shared-DzJYHisH.js.map → shared-CiKaIK4h.js.map} +1 -1
  53. package/dist/sub-routing.d.ts +6 -6
  54. package/dist/sub-routing.js.map +1 -1
  55. package/dist/tool-output-truncation-CH-khbZ3.js.map +1 -1
  56. package/dist/types.js.map +1 -1
  57. package/dist/utils.js.map +1 -1
  58. package/dist/vite.js.map +1 -1
  59. package/dist/workflow-types.js.map +1 -1
  60. package/dist/workflows.d.ts +1 -1
  61. package/dist/workflows.js.map +1 -1
  62. package/package.json +6 -6
@@ -19,29 +19,56 @@ import {
19
19
  w as SqlProvider,
20
20
  x as SearchProvider,
21
21
  y as isSkillProvider
22
- } from "../../../compaction-helpers-bYvP1o2S.js";
22
+ } from "../../../compaction-helpers-DAe-xiVY.js";
23
23
  import { ToolSet } from "ai";
24
24
 
25
25
  //#region src/experimental/memory/session/session.d.ts
26
26
  type SessionContextOptions = Omit<ContextConfig, "label">;
27
+ type InternalMessageChangeEvent =
28
+ | {
29
+ type: "append";
30
+ message: SessionMessage;
31
+ parentId?: string | null;
32
+ inserted: boolean;
33
+ }
34
+ | {
35
+ type: "update";
36
+ message: SessionMessage;
37
+ }
38
+ | {
39
+ type: "delete";
40
+ messageIds: string[];
41
+ }
42
+ | {
43
+ type: "clear";
44
+ }
45
+ | {
46
+ type: "compact";
47
+ };
27
48
  declare class Session {
28
49
  private storage;
29
50
  private context;
30
51
  private _agent?;
31
52
  private _broadcaster?;
53
+ private _storageProvider?;
32
54
  private _sessionId?;
33
55
  private _pending?;
34
56
  private _cachedPrompt?;
35
57
  private _compactionFn?;
36
58
  private _tokenThreshold?;
37
59
  private _ready;
60
+ private _restorePromise?;
61
+ private _messageChangeListener?;
38
62
  constructor(storage: SessionProvider, options?: SessionOptions);
39
63
  /**
40
- * Chainable session creation with auto-wired SQLite providers.
41
- * Chain methods in any order — providers are resolved lazily on first use.
64
+ * Chainable session creation with auto-wired providers.
65
+ *
66
+ * Pass a `SqlProvider` (Agent with `sql` method) for auto-wired SQLite,
67
+ * or a `SessionProvider` directly for custom storage (Postgres, etc.).
42
68
  *
43
69
  * @example
44
70
  * ```ts
71
+ * // Auto-wired SQLite (DO Agent)
45
72
  * const session = Session.create(this)
46
73
  * .withContext("soul", { provider: { get: async () => "You are helpful." } })
47
74
  * .withContext("memory", { description: "Learned facts", maxTokens: 1100 })
@@ -53,9 +80,17 @@ declare class Session {
53
80
  * provider: new R2SkillProvider(env.SKILLS_BUCKET, { prefix: "skills/" })
54
81
  * })
55
82
  * .withCachedPrompt();
83
+ *
84
+ * // Custom storage provider (Postgres, etc.)
85
+ * const session = Session.create(postgresProvider)
86
+ * .withContext("memory", {
87
+ * maxTokens: 1100,
88
+ * provider: new PostgresContextProvider(conn, "memory")
89
+ * })
90
+ * .withCachedPrompt(new PostgresContextProvider(conn, "_prompt"));
56
91
  * ```
57
92
  */
58
- static create(agent: SqlProvider): Session;
93
+ static create(provider: SqlProvider | SessionProvider): Session;
59
94
  forSession(sessionId: string): this;
60
95
  withContext(label: string, options?: SessionContextOptions): this;
61
96
  withCachedPrompt(provider?: WritableContextProvider): this;
@@ -71,23 +106,52 @@ declare class Session {
71
106
  * Checked after each `appendMessage`. Requires `onCompaction()`.
72
107
  */
73
108
  compactAfter(tokenThreshold: number): this;
109
+ /**
110
+ * @internal
111
+ * Framework hook for cache-owning callers that need to mirror message
112
+ * storage changes. Application code should use the normal Session methods.
113
+ */
114
+ internal_onMessagesChanged(
115
+ listener:
116
+ | ((event: InternalMessageChangeEvent) => void | Promise<void>)
117
+ | null
118
+ ): this;
74
119
  private _ensureReady;
120
+ /**
121
+ * Await the background skill-restore kicked off by `_ensureReady()`.
122
+ * Idempotent and cheap — every async public method calls this so that
123
+ * `_loadedSkills` reflects conversation history before any read or write.
124
+ */
125
+ private _ensureRestored;
126
+ private _notifyMessagesChanged;
75
127
  /**
76
128
  * Reconstruct which skills are loaded by scanning conversation history
77
129
  * for load_context tool results that haven't been unloaded.
78
- * Called during init to survive hibernation/eviction.
130
+ * Runs once per init to survive hibernation / eviction, including for
131
+ * async SessionProviders (e.g. Postgres) where we must `await` history.
79
132
  */
80
133
  private _restoreLoadedSkills;
81
134
  /**
82
- * Replace a load_context tool result in conversation history
83
- * with a short marker to reclaim context space.
135
+ * Reclaim context-window tokens consumed by a previously loaded skill.
136
+ *
137
+ * When a skill is loaded via the `load_context` tool, its full body is
138
+ * embedded as that tool call's `output-available` result inside the
139
+ * assistant message — which means every subsequent turn replays the
140
+ * entire skill as part of the conversation history and pays for it in
141
+ * input tokens.
142
+ *
143
+ * This method walks back through history, finds the matching
144
+ * `load_context` tool result for `(label, key)`, and replaces its bulky
145
+ * `output` with a short marker `[skill unloaded: <key>]`. The skill
146
+ * content is dropped from future turns and the tokens are reclaimed.
147
+ * The skill itself stays available to reload via `load_context`.
84
148
  */
85
149
  private _reclaimLoadedSkill;
86
- getHistory(leafId?: string | null): SessionMessage[];
87
- getMessage(id: string): SessionMessage | null;
88
- getLatestLeaf(): SessionMessage | null;
89
- getBranches(messageId: string): SessionMessage[];
90
- getPathLength(leafId?: string | null): number;
150
+ getHistory(leafId?: string | null): Promise<SessionMessage[]>;
151
+ getMessage(id: string): Promise<SessionMessage | null>;
152
+ getLatestLeaf(): Promise<SessionMessage | null>;
153
+ getBranches(messageId: string): Promise<SessionMessage[]>;
154
+ getPathLength(leafId?: string | null): Promise<number>;
91
155
  private _broadcast;
92
156
  private _emitStatus;
93
157
  private _emitError;
@@ -95,15 +159,16 @@ declare class Session {
95
159
  message: SessionMessage,
96
160
  parentId?: string | null
97
161
  ): Promise<void>;
98
- updateMessage(message: SessionMessage): void;
99
- deleteMessages(messageIds: string[]): void;
100
- clearMessages(): void;
162
+ private _appendMessage;
163
+ updateMessage(message: SessionMessage): Promise<void>;
164
+ deleteMessages(messageIds: string[]): Promise<void>;
165
+ clearMessages(): Promise<void>;
101
166
  addCompaction(
102
167
  summary: string,
103
168
  fromMessageId: string,
104
169
  toMessageId: string
105
- ): StoredCompaction;
106
- getCompactions(): StoredCompaction[];
170
+ ): Promise<StoredCompaction>;
171
+ getCompactions(): Promise<StoredCompaction[]>;
107
172
  /**
108
173
  * Run the registered compaction function and store the result as an overlay.
109
174
  * Requires `onCompaction()` to be called first.
@@ -115,7 +180,12 @@ declare class Session {
115
180
  appendContextBlock(label: string, content: string): Promise<ContextBlock>;
116
181
  /**
117
182
  * Dynamically register a new context block after session initialization.
118
- * Used by extensions to contribute context blocks at runtime.
183
+ *
184
+ * This is a **builder / runtime API**, not an LLM tool. The LLM writes
185
+ * into existing context blocks via the `set_context` tool (see
186
+ * `ContextBlocks.tools()`); it cannot declare new blocks itself. This
187
+ * method is how extension / host code contributes blocks at runtime
188
+ * (e.g. an extension's `onLoad` handler registering its own memory block).
119
189
  *
120
190
  * The block's provider is initialized and loaded immediately.
121
191
  * Call `refreshSystemPrompt()` afterward to include the new block
@@ -141,12 +211,18 @@ declare class Session {
141
211
  /**
142
212
  * Unload a previously loaded skill, reclaiming context space.
143
213
  * The tool result in conversation history is replaced with a short marker.
214
+ *
215
+ * Async so that the session's background skill-state restore (which
216
+ * reads conversation history) is awaited first — otherwise a freshly
217
+ * rehydrated DO could report "not loaded" for a skill that's actually
218
+ * present in history.
144
219
  */
145
- unloadSkill(label: string, key: string): boolean;
220
+ unloadSkill(label: string, key: string): Promise<boolean>;
146
221
  /**
147
222
  * Get currently loaded skill keys (as "label:key" strings).
223
+ * Async for the same reason as `unloadSkill` — must wait for restore.
148
224
  */
149
- getLoadedSkillKeys(): Set<string>;
225
+ getLoadedSkillKeys(): Promise<Set<string>>;
150
226
  freezeSystemPrompt(): Promise<string>;
151
227
  refreshSystemPrompt(): Promise<string>;
152
228
  search(
@@ -154,12 +230,14 @@ declare class Session {
154
230
  options?: {
155
231
  limit?: number;
156
232
  }
157
- ): Array<{
158
- id: string;
159
- role: string;
160
- content: string;
161
- createdAt?: string;
162
- }>;
233
+ ): Promise<
234
+ Array<{
235
+ id: string;
236
+ role: string;
237
+ content: string;
238
+ createdAt?: string;
239
+ }>
240
+ >;
163
241
  /** Returns set_context and load_context tools. */
164
242
  tools(): Promise<ToolSet>;
165
243
  }
@@ -253,7 +331,7 @@ declare class SessionManager {
253
331
  ): SessionInfo;
254
332
  get(sessionId: string): SessionInfo | null;
255
333
  list(): SessionInfo[];
256
- delete(sessionId: string): void;
334
+ delete(sessionId: string): Promise<void>;
257
335
  rename(sessionId: string, name: string): void;
258
336
  append(
259
337
  sessionId: string,
@@ -270,11 +348,11 @@ declare class SessionManager {
270
348
  messages: SessionMessage[],
271
349
  parentId?: string
272
350
  ): Promise<string | null>;
273
- getHistory(sessionId: string, leafId?: string): SessionMessage[];
274
- getMessageCount(sessionId: string): number;
275
- clearMessages(sessionId: string): void;
276
- deleteMessages(sessionId: string, messageIds: string[]): void;
277
- getBranches(sessionId: string, messageId: string): SessionMessage[];
351
+ getHistory(sessionId: string, leafId?: string): Promise<SessionMessage[]>;
352
+ getMessageCount(sessionId: string): Promise<number>;
353
+ clearMessages(sessionId: string): Promise<void>;
354
+ deleteMessages(sessionId: string, messageIds: string[]): Promise<void>;
355
+ getBranches(sessionId: string, messageId: string): Promise<SessionMessage[]>;
278
356
  /**
279
357
  * Fork a session at a specific message, creating a new session
280
358
  * with the history up to that point copied over.
@@ -289,8 +367,8 @@ declare class SessionManager {
289
367
  summary: string,
290
368
  fromId: string,
291
369
  toId: string
292
- ): StoredCompaction;
293
- getCompactions(sessionId: string): StoredCompaction[];
370
+ ): Promise<StoredCompaction>;
371
+ getCompactions(sessionId: string): Promise<StoredCompaction[]>;
294
372
  compactAndSplit(
295
373
  sessionId: string,
296
374
  summary: string,
@@ -329,6 +407,135 @@ declare class AgentContextProvider implements WritableContextProvider {
329
407
  set(content: string): Promise<void>;
330
408
  }
331
409
  //#endregion
410
+ //#region src/experimental/memory/session/providers/postgres-adapter.d.ts
411
+ /**
412
+ * Postgres connection adapter.
413
+ *
414
+ * Lets the Postgres-backed providers accept either:
415
+ * - a raw `pg.Client` (or any client with a compatible `query` method), or
416
+ * - the internal `PostgresConnection` interface used by tests and custom drivers.
417
+ *
418
+ * When a `pg`-style client is passed, `?` placeholders are rewritten to
419
+ * `$1, $2, ...` on the way through, so the providers can keep using the
420
+ * portable `?` syntax internally without users having to write a wrapper.
421
+ */
422
+ /**
423
+ * Minimal connection interface used internally by the Postgres providers.
424
+ * Tests and custom drivers can implement this directly.
425
+ */
426
+ interface PostgresConnection {
427
+ execute(
428
+ query: string,
429
+ args?: (string | number | boolean | null)[]
430
+ ): Promise<{
431
+ rows: Record<string, unknown>[];
432
+ }>;
433
+ }
434
+ /**
435
+ * Structural type matching `pg.Client` (and most `pg`-compatible pools).
436
+ * Accepts the subset of the real client that the providers need, so users
437
+ * can pass `new pg.Client({ connectionString: env.HYPERDRIVE.connectionString })`
438
+ * directly without a wrapper.
439
+ */
440
+ interface PgClientLike {
441
+ query(
442
+ queryText: string,
443
+ values?: readonly unknown[]
444
+ ): Promise<{
445
+ rows: Record<string, unknown>[];
446
+ }>;
447
+ }
448
+ /**
449
+ * Accepted client type across the Postgres providers.
450
+ *
451
+ * Use `pg.Client` (the recommended path for Hyperdrive):
452
+ * ```ts
453
+ * const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
454
+ * await client.connect();
455
+ * new PostgresSessionProvider(client, sessionId);
456
+ * ```
457
+ *
458
+ * Or implement `PostgresConnection` for tests / bespoke drivers.
459
+ */
460
+ type PostgresClient = PostgresConnection | PgClientLike;
461
+ //#endregion
462
+ //#region src/experimental/memory/session/providers/postgres.d.ts
463
+ declare class PostgresSessionProvider implements SessionProvider {
464
+ private conn;
465
+ private sessionId;
466
+ /**
467
+ * @param client A raw `pg.Client` (recommended) or any `PostgresConnection`.
468
+ * Must already be connected — this provider never opens or closes the
469
+ * underlying client.
470
+ * @param sessionId Session identifier. Different ids are fully isolated
471
+ * rows within the shared tables. Defaults to `""`.
472
+ */
473
+ constructor(client: PostgresClient, sessionId?: string);
474
+ getMessage(id: string): Promise<SessionMessage | null>;
475
+ getHistory(leafId?: string | null): Promise<SessionMessage[]>;
476
+ getLatestLeaf(): Promise<SessionMessage | null>;
477
+ getBranches(messageId: string): Promise<SessionMessage[]>;
478
+ getPathLength(leafId?: string | null): Promise<number>;
479
+ appendMessage(
480
+ message: SessionMessage,
481
+ parentId?: string | null
482
+ ): Promise<void>;
483
+ updateMessage(message: SessionMessage): Promise<void>;
484
+ deleteMessages(messageIds: string[]): Promise<void>;
485
+ clearMessages(): Promise<void>;
486
+ addCompaction(
487
+ summary: string,
488
+ fromMessageId: string,
489
+ toMessageId: string
490
+ ): Promise<StoredCompaction>;
491
+ getCompactions(): Promise<StoredCompaction[]>;
492
+ searchMessages(query: string, limit?: number): Promise<SearchResult[]>;
493
+ private latestLeafRow;
494
+ private applyCompactions;
495
+ private parse;
496
+ private parseRows;
497
+ /**
498
+ * Extract just the human-readable text from a message's JSON blob
499
+ * and store it in `text_content`, which feeds the generated `content_tsv`
500
+ * column used for FTS. The full structured message (parts, tool calls,
501
+ * metadata) is still stored verbatim in `content` — this is the source
502
+ * of truth. Indexing the raw JSON would return FTS hits on keys like
503
+ * `"role"`, `"parts"`, `"dynamic-tool"`, etc.
504
+ */
505
+ private extractSearchableText;
506
+ }
507
+ //#endregion
508
+ //#region src/experimental/memory/session/providers/postgres-context.d.ts
509
+ declare class PostgresContextProvider implements WritableContextProvider {
510
+ private conn;
511
+ private label;
512
+ /**
513
+ * @param client A raw `pg.Client` (recommended) or any `PostgresConnection`.
514
+ * Must already be connected.
515
+ * @param label Block label used as the primary key row in
516
+ * `cf_agents_context_blocks`. Pass a session-scoped label (e.g.
517
+ * `` `memory_${sessionId}` ``) for per-session state.
518
+ */
519
+ constructor(client: PostgresClient, label: string);
520
+ get(): Promise<string | null>;
521
+ set(content: string): Promise<void>;
522
+ }
523
+ //#endregion
524
+ //#region src/experimental/memory/session/providers/postgres-search.d.ts
525
+ declare class PostgresSearchProvider implements SearchProvider {
526
+ private conn;
527
+ private label;
528
+ /**
529
+ * @param client A raw `pg.Client` (recommended) or any `PostgresConnection`.
530
+ * Must already be connected.
531
+ */
532
+ constructor(client: PostgresClient);
533
+ init(label: string): void;
534
+ get(): Promise<string | null>;
535
+ search(query: string): Promise<string | null>;
536
+ set(key: string, content: string): Promise<void>;
537
+ }
538
+ //#endregion
332
539
  export {
333
540
  AgentContextProvider,
334
541
  AgentSearchProvider,
@@ -336,6 +543,12 @@ export {
336
543
  type ContextBlock,
337
544
  type ContextConfig,
338
545
  type ContextProvider,
546
+ type PgClientLike,
547
+ type PostgresClient,
548
+ type PostgresConnection,
549
+ PostgresContextProvider,
550
+ PostgresSearchProvider,
551
+ PostgresSessionProvider,
339
552
  R2SkillProvider,
340
553
  type SearchProvider,
341
554
  type SearchResult,