@anvia/core 0.1.0 → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Indra Zulfi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -47,6 +47,69 @@ const response = await agent.prompt("What is happening with order A123?").send()
47
47
  console.log(response.output);
48
48
  ```
49
49
 
50
+ ## Prompts and Memory
51
+
52
+ Use a plain prompt for stateless calls:
53
+
54
+ ```ts
55
+ await agent.prompt("Summarize this ticket.").send();
56
+ ```
57
+
58
+ Use a message array when you already own the transcript. The last message is the active prompt and earlier messages are request history:
59
+
60
+ ```ts
61
+ import { Message } from "@anvia/core";
62
+
63
+ await agent
64
+ .prompt([
65
+ Message.user("My project is named Anvia."),
66
+ Message.assistant("Noted."),
67
+ Message.user("What is my project named?"),
68
+ ])
69
+ .send();
70
+ ```
71
+
72
+ Configure durable conversation memory on the agent, then run through a session:
73
+
74
+ ```ts
75
+ import {
76
+ AgentBuilder,
77
+ type MemoryAppendInput,
78
+ type MemoryContext,
79
+ type MemoryStore,
80
+ type Message,
81
+ } from "@anvia/core";
82
+
83
+ class AppMemoryStore implements MemoryStore {
84
+ private readonly sessions = new Map<string, Message[]>();
85
+
86
+ async load(context: MemoryContext): Promise<Message[]> {
87
+ return [...(this.sessions.get(context.sessionId) ?? [])];
88
+ }
89
+
90
+ async append(input: MemoryAppendInput): Promise<void> {
91
+ const current = this.sessions.get(input.context.sessionId) ?? [];
92
+ this.sessions.set(input.context.sessionId, [...current, ...input.messages]);
93
+ }
94
+
95
+ async clear(context: MemoryContext): Promise<void> {
96
+ this.sessions.delete(context.sessionId);
97
+ }
98
+ }
99
+
100
+ const memory = new AppMemoryStore();
101
+ const agent = new AgentBuilder("support", model).memory(memory).build();
102
+
103
+ await agent.session("thread_123", { userId: "user_456" }).prompt("Remember my plan.").send();
104
+ await agent.session("thread_123", { userId: "user_456" }).prompt("What is my plan?").send();
105
+ ```
106
+
107
+ Memory defaults to `savePolicy: "message"`, which saves the user prompt, each completed assistant message, and each completed tool result as soon as they are ready. You can choose `"turn"` or `"run"` at configuration time:
108
+
109
+ ```ts
110
+ new AgentBuilder("support", model).memory(memory, { savePolicy: "turn" });
111
+ ```
112
+
50
113
  ## Structured Extraction
51
114
 
52
115
  ```ts
@@ -80,6 +143,7 @@ const result = await pipeline.run("Customer cannot complete checkout.");
80
143
  - `agent`: agent runtime and `AgentBuilder`
81
144
  - `tool`: typed tool creation and tool sets
82
145
  - `completion`: provider-neutral completion request and response types
146
+ - `memory`: durable session memory interfaces and in-memory store
83
147
  - `extractor`: schema-first structured extraction
84
148
  - `pipeline`: typed sequential and parallel workflows
85
149
  - `embeddings`: embedding helpers and document embedding utilities
@@ -1,5 +1,7 @@
1
- import { e as DynamicContextOptions, g as DynamicToolOptions, P as PromptHook, A as Agent } from '../agent-C6h6YrRU.js';
2
- export { a as AgentOptions, b as AgentStreamEvent, c as AgentToolOptions, C as CompletionCallHookArgs, d as CompletionResponseHookArgs, D as DEFAULT_MAX_TURNS, f as DynamicContextRegistration, h as DynamicToolRegistration, H as HookAction, i as HookResult, j as PromptRequest, k as PromptResponse, R as RunControl, T as ToolCallControl, l as ToolCallHookAction, m as ToolCallHookArgs, n as ToolCallHookResult, o as ToolHookArgs, p as ToolResultHookArgs, q as cancelPrompt, r as createHook, s as runControl, t as skipTool, u as toolCallControl } from '../agent-C6h6YrRU.js';
1
+ import { MemoryStore, MemoryOptions } from '../memory/index.js';
2
+ export { MemoryAppendInput, MemoryContext, MemoryErrorInput, MemoryRegistration, MemorySavePolicy, ResolvedMemoryOptions, SessionOptions, resolveMemoryOptions } from '../memory/index.js';
3
+ import { f as DynamicContextOptions, h as DynamicToolOptions, P as PromptHook, A as Agent } from '../agent-Bj0UqYMp.js';
4
+ export { a as AgentOptions, b as AgentSession, c as AgentStreamEvent, d as AgentToolOptions, C as CompletionCallHookArgs, e as CompletionResponseHookArgs, D as DEFAULT_MAX_TURNS, g as DynamicContextRegistration, i as DynamicToolRegistration, H as HookAction, j as HookResult, k as PromptRequest, l as PromptResponse, R as RunControl, T as ToolCallControl, m as ToolCallHookAction, n as ToolCallHookArgs, o as ToolCallHookResult, p as ToolHookArgs, q as ToolResultHookArgs, r as cancelPrompt, s as createHook, t as runControl, u as skipTool, v as toolCallControl } from '../agent-Bj0UqYMp.js';
3
5
  import { b as CompletionModel, j as JsonValue, o as ToolChoice, M as Message } from '../types-BrxLd7ay.js';
4
6
  import { c as McpServer } from '../types-B5B8Sdl4.js';
5
7
  import { AgentObserver, ObserveOptions } from '../observability/index.js';
@@ -30,6 +32,7 @@ declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
30
32
  private observerRegistrations;
31
33
  private dynamicContextRegistrations;
32
34
  private dynamicToolRegistrations;
35
+ private memoryRegistration;
33
36
  private activeToolSet;
34
37
  constructor(agentId: string, completionModel: M);
35
38
  name(name: string): this;
@@ -50,6 +53,7 @@ declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
50
53
  defaultMaxTurns(defaultMaxTurns: number): this;
51
54
  hook(hook: PromptHook): this;
52
55
  observe(observer: AgentObserver, options?: ObserveOptions): this;
56
+ memory(store: MemoryStore, options?: MemoryOptions): this;
53
57
  outputSchema(schema: ZodSchema): this;
54
58
  build(): Agent<M>;
55
59
  private buildInstructions;
@@ -67,4 +71,4 @@ declare class PromptCancelledError extends Error {
67
71
  constructor(chatHistory: Message[], reason: string);
68
72
  }
69
73
 
70
- export { Agent, AgentBuilder, DynamicContextOptions, DynamicToolOptions, MaxTurnsError, PromptCancelledError, PromptHook };
74
+ export { Agent, AgentBuilder, DynamicContextOptions, DynamicToolOptions, MaxTurnsError, MemoryOptions, MemoryStore, PromptCancelledError, PromptHook };
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Agent,
3
3
  AgentBuilder,
4
+ AgentSession,
4
5
  DEFAULT_MAX_TURNS,
5
6
  MaxTurnsError,
6
7
  PromptCancelledError,
@@ -10,7 +11,10 @@ import {
10
11
  runControl,
11
12
  skipTool,
12
13
  toolCallControl
13
- } from "../chunk-IQBY2GCF.js";
14
+ } from "../chunk-JQCRURY2.js";
15
+ import {
16
+ resolveMemoryOptions
17
+ } from "../chunk-XXT2UCAR.js";
14
18
  import "../chunk-XUUY2L2D.js";
15
19
  import "../chunk-GNWMOSNR.js";
16
20
  import "../chunk-B4QHQN5K.js";
@@ -18,12 +22,14 @@ import "../chunk-CP47FBJV.js";
18
22
  export {
19
23
  Agent,
20
24
  AgentBuilder,
25
+ AgentSession,
21
26
  DEFAULT_MAX_TURNS,
22
27
  MaxTurnsError,
23
28
  PromptCancelledError,
24
29
  PromptRequest,
25
30
  cancelPrompt,
26
31
  createHook,
32
+ resolveMemoryOptions,
27
33
  runControl,
28
34
  skipTool,
29
35
  toolCallControl
@@ -1,4 +1,5 @@
1
1
  import { M as Message, e as CompletionResponse, b as CompletionModel, U as Usage, l as ReasoningContentType, n as ToolCall, D as Document, j as JsonValue, o as ToolChoice, J as JsonObject } from './types-BrxLd7ay.js';
2
+ import { MemoryContext, MemoryRegistration, SessionOptions } from './memory/index.js';
2
3
  import { AgentTraceOptions, AgentTraceInfo, AgentObserverRegistration } from './observability/index.js';
3
4
  import { a as ToolSet, T as ToolSearchDocument } from './zod-schema-DJTEgQBq.js';
4
5
  import { T as Tool, A as AnyTool } from './tool-DhuBQ3yb.js';
@@ -118,14 +119,17 @@ type AgentStreamEvent<RawResponse = unknown> = {
118
119
  declare class PromptRequest<M extends CompletionModel = CompletionModel> {
119
120
  private readonly agent;
120
121
  private readonly promptMessage;
122
+ private readonly initialHistory;
123
+ private readonly memoryContext;
121
124
  private chatHistory;
122
125
  private maxTurnCount;
123
126
  private activeHook;
124
127
  private concurrency;
125
128
  private traceOptions;
126
129
  private constructor();
127
- static fromAgent<M extends CompletionModel>(agent: Agent<M>, prompt: string | Message): PromptRequest<M>;
128
- withHistory(history: Message[]): this;
130
+ static fromAgent<M extends CompletionModel>(agent: Agent<M>, prompt: string | Message | Message[], options?: {
131
+ memoryContext?: MemoryContext | undefined;
132
+ }): PromptRequest<M>;
129
133
  maxTurns(maxTurns: number): this;
130
134
  requestHook(hook: PromptHook): this;
131
135
  withToolConcurrency(concurrency: number): this;
@@ -142,6 +146,13 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
142
146
  private runCompletionCallHook;
143
147
  private runCompletionResponseHook;
144
148
  private cancelled;
149
+ private memory;
150
+ private memoryPolicy;
151
+ private prepareMemoryRun;
152
+ private commitMemoryMessages;
153
+ private commitCompletedMemoryTurn;
154
+ private commitCompletedMemoryRun;
155
+ private recordMemoryError;
145
156
  }
146
157
 
147
158
  type AgentOptions<M extends CompletionModel = CompletionModel> = {
@@ -162,6 +173,7 @@ type AgentOptions<M extends CompletionModel = CompletionModel> = {
162
173
  observers?: AgentObserverRegistration[] | undefined;
163
174
  dynamicContexts?: DynamicContextRegistration[] | undefined;
164
175
  dynamicTools?: DynamicToolRegistration[] | undefined;
176
+ memory?: MemoryRegistration | undefined;
165
177
  };
166
178
  declare const DEFAULT_MAX_TURNS = 20;
167
179
  type AgentToolOptions = {
@@ -206,13 +218,27 @@ declare class Agent<M extends CompletionModel = CompletionModel> {
206
218
  readonly observers: AgentObserverRegistration[];
207
219
  readonly dynamicContexts: DynamicContextRegistration[];
208
220
  readonly dynamicTools: DynamicToolRegistration[];
221
+ readonly memory: MemoryRegistration | undefined;
209
222
  constructor(options: AgentOptions<M>);
210
- prompt(prompt: string | Message): PromptRequest<M>;
223
+ prompt(prompt: string | Message | Message[]): PromptRequest<M>;
224
+ session(sessionId: string, options?: SessionOptions): AgentSession<M>;
211
225
  asTool(options: AgentToolOptions): Tool<{
212
226
  prompt: string;
213
227
  }, string>;
214
228
  getTool(toolName: string): AnyTool | undefined;
215
229
  callTool(toolName: string, args: string): Promise<string>;
216
230
  }
231
+ declare class AgentSession<M extends CompletionModel = CompletionModel> {
232
+ private readonly agent;
233
+ private readonly context;
234
+ constructor(agent: Agent<M>, context: {
235
+ sessionId: string;
236
+ userId?: string | undefined;
237
+ metadata?: JsonObject | undefined;
238
+ });
239
+ prompt(prompt: string | Message): PromptRequest<M>;
240
+ messages(): Promise<Message[]>;
241
+ clear(): Promise<void>;
242
+ }
217
243
 
218
- export { Agent as A, type CompletionCallHookArgs as C, DEFAULT_MAX_TURNS as D, type HookAction as H, type PromptHook as P, type RunControl as R, type ToolCallControl as T, type AgentOptions as a, type AgentStreamEvent as b, type AgentToolOptions as c, type CompletionResponseHookArgs as d, type DynamicContextOptions as e, type DynamicContextRegistration as f, type DynamicToolOptions as g, type DynamicToolRegistration as h, type HookResult as i, PromptRequest as j, type PromptResponse as k, type ToolCallHookAction as l, type ToolCallHookArgs as m, type ToolCallHookResult as n, type ToolHookArgs as o, type ToolResultHookArgs as p, cancelPrompt as q, createHook as r, runControl as s, skipTool as t, toolCallControl as u };
244
+ export { Agent as A, type CompletionCallHookArgs as C, DEFAULT_MAX_TURNS as D, type HookAction as H, type PromptHook as P, type RunControl as R, type ToolCallControl as T, type AgentOptions as a, AgentSession as b, type AgentStreamEvent as c, type AgentToolOptions as d, type CompletionResponseHookArgs as e, type DynamicContextOptions as f, type DynamicContextRegistration as g, type DynamicToolOptions as h, type DynamicToolRegistration as i, type HookResult as j, PromptRequest as k, type PromptResponse as l, type ToolCallHookAction as m, type ToolCallHookArgs as n, type ToolCallHookResult as o, type ToolHookArgs as p, type ToolResultHookArgs as q, cancelPrompt as r, createHook as s, runControl as t, skipTool as u, toolCallControl as v };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ExtractorBuilder
3
- } from "./chunk-IA76K5UX.js";
3
+ } from "./chunk-O3TUS5RB.js";
4
4
  import {
5
5
  cosineSimilarity,
6
6
  embedText
@@ -301,4 +301,4 @@ export {
301
301
  llmScore,
302
302
  agentEvalTarget
303
303
  };
304
- //# sourceMappingURL=chunk-LMBOJMNB.js.map
304
+ //# sourceMappingURL=chunk-C7XTEV5W.js.map
@@ -1,3 +1,6 @@
1
+ import {
2
+ resolveMemoryOptions
3
+ } from "./chunk-XXT2UCAR.js";
1
4
  import {
2
5
  toReadableStream
3
6
  } from "./chunk-XUUY2L2D.js";
@@ -453,25 +456,27 @@ function reasoningDeltaEvent(event) {
453
456
 
454
457
  // src/agent/request.ts
455
458
  var PromptRequest = class _PromptRequest {
456
- constructor(agent, promptMessage) {
459
+ constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
457
460
  this.agent = agent;
458
461
  this.promptMessage = promptMessage;
462
+ this.initialHistory = initialHistory;
463
+ this.memoryContext = memoryContext;
464
+ this.chatHistory = initialHistory;
459
465
  this.maxTurnCount = agent.defaultMaxTurns ?? 0;
460
466
  this.activeHook = agent.hook;
461
467
  }
462
468
  agent;
463
469
  promptMessage;
470
+ initialHistory;
471
+ memoryContext;
464
472
  chatHistory;
465
473
  maxTurnCount;
466
474
  activeHook;
467
475
  concurrency = 1;
468
476
  traceOptions;
469
- static fromAgent(agent, prompt) {
470
- return new _PromptRequest(agent, typeof prompt === "string" ? Message.user(prompt) : prompt);
471
- }
472
- withHistory(history) {
473
- this.chatHistory = history;
474
- return this;
477
+ static fromAgent(agent, prompt, options = {}) {
478
+ const normalized = normalizePromptInput(prompt);
479
+ return new _PromptRequest(agent, normalized.prompt, normalized.history, options.memoryContext);
475
480
  }
476
481
  maxTurns(maxTurns) {
477
482
  this.maxTurnCount = maxTurns;
@@ -490,7 +495,10 @@ var PromptRequest = class _PromptRequest {
490
495
  return this;
491
496
  }
492
497
  async send() {
498
+ const runId = globalThis.crypto.randomUUID();
493
499
  const newMessages = [this.promptMessage];
500
+ await this.prepareMemoryRun(runId, newMessages);
501
+ const pendingTurnMessages = this.memoryPolicy() === "turn" ? [...newMessages] : [];
494
502
  let usage = Usage.empty();
495
503
  let currentTurns = 0;
496
504
  let lastPrompt = this.promptMessage;
@@ -503,7 +511,7 @@ var PromptRequest = class _PromptRequest {
503
511
  }
504
512
  lastPrompt = prompt;
505
513
  currentTurns += 1;
506
- const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
514
+ const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
507
515
  await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
508
516
  const ragText = extractRagText(prompt);
509
517
  const dynamicContext = await this.fetchDynamicContext(ragText);
@@ -512,11 +520,24 @@ var PromptRequest = class _PromptRequest {
512
520
  const response = await this.runCompletion(request, currentTurns, runObservers);
513
521
  usage = Usage.add(usage, response.usage);
514
522
  await this.runCompletionResponseHook(prompt, response, newMessages);
515
- newMessages.push(Message.assistant(response.choice, response.messageId));
523
+ const assistantMessage = Message.assistant(response.choice, response.messageId);
524
+ newMessages.push(assistantMessage);
525
+ await this.commitMemoryMessages(
526
+ runId,
527
+ currentTurns,
528
+ [assistantMessage],
529
+ pendingTurnMessages
530
+ );
516
531
  const toolCalls = response.choice.filter(
517
532
  (item) => item.type === "tool_call"
518
533
  );
519
534
  if (toolCalls.length === 0) {
535
+ await this.commitCompletedMemoryRun(
536
+ runId,
537
+ currentTurns,
538
+ newMessages,
539
+ pendingTurnMessages
540
+ );
520
541
  const result = {
521
542
  output: textFromAssistantContent(response.choice),
522
543
  usage,
@@ -530,15 +551,15 @@ var PromptRequest = class _PromptRequest {
530
551
  turn: currentTurns,
531
552
  runObservers
532
553
  });
533
- newMessages.push(Message.tool(toolResults));
554
+ const toolMessage = Message.tool(toolResults);
555
+ newMessages.push(toolMessage);
556
+ await this.commitMemoryMessages(runId, currentTurns, [toolMessage], pendingTurnMessages);
557
+ await this.commitCompletedMemoryTurn(runId, currentTurns, pendingTurnMessages);
534
558
  }
535
- throw new MaxTurnsError(
536
- this.maxTurnCount,
537
- [...this.chatHistory ?? [], ...newMessages],
538
- lastPrompt
539
- );
559
+ throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
540
560
  } catch (error) {
541
561
  await runObservers.error({ error, usage, messages: [...newMessages] });
562
+ await this.recordMemoryError(runId, error, newMessages);
542
563
  throw error;
543
564
  }
544
565
  }
@@ -546,7 +567,10 @@ var PromptRequest = class _PromptRequest {
546
567
  if (!this.agent.model.capabilities.streaming || !isStreamingCompletionModel(this.agent.model)) {
547
568
  throw new Error("This completion model does not support streaming");
548
569
  }
570
+ const runId = globalThis.crypto.randomUUID();
549
571
  const newMessages = [this.promptMessage];
572
+ await this.prepareMemoryRun(runId, newMessages);
573
+ const pendingTurnMessages = this.memoryPolicy() === "turn" ? [...newMessages] : [];
550
574
  let usage = Usage.empty();
551
575
  let currentTurns = 0;
552
576
  let lastPrompt = this.promptMessage;
@@ -559,7 +583,7 @@ var PromptRequest = class _PromptRequest {
559
583
  }
560
584
  lastPrompt = prompt;
561
585
  currentTurns += 1;
562
- const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
586
+ const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
563
587
  yield {
564
588
  type: "turn_start",
565
589
  turn: currentTurns,
@@ -604,7 +628,14 @@ var PromptRequest = class _PromptRequest {
604
628
  });
605
629
  usage = Usage.add(usage, response.usage);
606
630
  await this.runCompletionResponseHook(prompt, response, newMessages);
607
- newMessages.push(Message.assistant(response.choice, response.messageId));
631
+ const assistantMessage = Message.assistant(response.choice, response.messageId);
632
+ newMessages.push(assistantMessage);
633
+ await this.commitMemoryMessages(
634
+ runId,
635
+ currentTurns,
636
+ [assistantMessage],
637
+ pendingTurnMessages
638
+ );
608
639
  const toolCalls = response.choice.filter(
609
640
  (item) => item.type === "tool_call"
610
641
  );
@@ -614,6 +645,12 @@ var PromptRequest = class _PromptRequest {
614
645
  yield { type: "turn_end", turn: currentTurns, response };
615
646
  if (toolCalls.length === 0) {
616
647
  const output = textFromAssistantContent(response.choice);
648
+ await this.commitCompletedMemoryRun(
649
+ runId,
650
+ currentTurns,
651
+ newMessages,
652
+ pendingTurnMessages
653
+ );
617
654
  yield {
618
655
  type: "final",
619
656
  output,
@@ -644,15 +681,15 @@ var PromptRequest = class _PromptRequest {
644
681
  yield { type: "tool_result", turn: currentTurns, ...result };
645
682
  }
646
683
  const toolResults = await toolResultsPromise;
647
- newMessages.push(Message.tool(toolResults));
684
+ const toolMessage = Message.tool(toolResults);
685
+ newMessages.push(toolMessage);
686
+ await this.commitMemoryMessages(runId, currentTurns, [toolMessage], pendingTurnMessages);
687
+ await this.commitCompletedMemoryTurn(runId, currentTurns, pendingTurnMessages);
648
688
  }
649
- throw new MaxTurnsError(
650
- this.maxTurnCount,
651
- [...this.chatHistory ?? [], ...newMessages],
652
- lastPrompt
653
- );
689
+ throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
654
690
  } catch (error) {
655
691
  await runObservers.error({ error, usage, messages: [...newMessages] });
692
+ await this.recordMemoryError(runId, error, newMessages);
656
693
  yield { type: "error", error };
657
694
  throw error;
658
695
  }
@@ -760,7 +797,7 @@ var PromptRequest = class _PromptRequest {
760
797
  instructions: this.agent.instructions,
761
798
  trace: this.traceOptions,
762
799
  prompt: this.promptMessage,
763
- history: this.chatHistory ?? [],
800
+ history: this.chatHistory,
764
801
  maxTurns: this.maxTurnCount
765
802
  },
766
803
  failOnObserverError
@@ -850,9 +887,105 @@ var PromptRequest = class _PromptRequest {
850
887
  }
851
888
  }
852
889
  cancelled(newMessages, reason) {
853
- return new PromptCancelledError([...this.chatHistory ?? [], ...newMessages], reason);
890
+ return new PromptCancelledError([...this.chatHistory, ...newMessages], reason);
891
+ }
892
+ memory() {
893
+ return this.memoryContext === void 0 ? void 0 : this.agent.memory;
894
+ }
895
+ memoryPolicy() {
896
+ return this.memory()?.options.savePolicy;
897
+ }
898
+ async prepareMemoryRun(runId, newMessages) {
899
+ const memory = this.memory();
900
+ if (memory === void 0 || this.memoryContext === void 0) {
901
+ this.chatHistory = this.initialHistory;
902
+ return;
903
+ }
904
+ const memoryHistory = await memory.store.load(this.memoryContext);
905
+ this.chatHistory = [...memoryHistory, ...this.initialHistory];
906
+ if (memory.options.savePolicy === "message") {
907
+ await memory.store.append({
908
+ context: this.memoryContext,
909
+ runId,
910
+ turn: 1,
911
+ messages: newMessages
912
+ });
913
+ }
914
+ }
915
+ async commitMemoryMessages(runId, turn, messages, pendingTurnMessages) {
916
+ const memory = this.memory();
917
+ if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
918
+ return;
919
+ }
920
+ if (memory.options.savePolicy === "message") {
921
+ await memory.store.append({
922
+ context: this.memoryContext,
923
+ runId,
924
+ turn,
925
+ messages
926
+ });
927
+ } else if (memory.options.savePolicy === "turn") {
928
+ pendingTurnMessages.push(...messages);
929
+ }
930
+ }
931
+ async commitCompletedMemoryTurn(runId, turn, pendingTurnMessages) {
932
+ const memory = this.memory();
933
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
934
+ return;
935
+ }
936
+ await memory.store.append({
937
+ context: this.memoryContext,
938
+ runId,
939
+ turn,
940
+ messages: [...pendingTurnMessages]
941
+ });
942
+ pendingTurnMessages.length = 0;
943
+ }
944
+ async commitCompletedMemoryRun(runId, turn, newMessages, pendingTurnMessages) {
945
+ await this.commitCompletedMemoryTurn(runId, turn, pendingTurnMessages);
946
+ const memory = this.memory();
947
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
948
+ return;
949
+ }
950
+ await memory.store.append({
951
+ context: this.memoryContext,
952
+ runId,
953
+ turn,
954
+ messages: [...newMessages]
955
+ });
956
+ }
957
+ async recordMemoryError(runId, error, newMessages) {
958
+ const memory = this.memory();
959
+ if (memory === void 0 || this.memoryContext === void 0) {
960
+ return;
961
+ }
962
+ await memory.store.recordError?.({
963
+ context: this.memoryContext,
964
+ runId,
965
+ error,
966
+ messages: [...newMessages]
967
+ });
854
968
  }
855
969
  };
970
+ function normalizePromptInput(prompt) {
971
+ if (typeof prompt === "string") {
972
+ return { prompt: Message.user(prompt), history: [] };
973
+ }
974
+ if (!Array.isArray(prompt)) {
975
+ return { prompt, history: [] };
976
+ }
977
+ if (prompt.length === 0) {
978
+ throw new TypeError("Prompt transcript must contain at least one message.");
979
+ }
980
+ const activePrompt = prompt.at(-1);
981
+ if (activePrompt === void 0) {
982
+ throw new TypeError("Prompt transcript must contain at least one message.");
983
+ }
984
+ return {
985
+ prompt: activePrompt,
986
+ history: prompt.slice(0, -1)
987
+ };
988
+ }
856
989
  function createAsyncQueue() {
857
990
  const values = [];
858
991
  const waiters = [];
@@ -962,6 +1095,7 @@ var Agent = class {
962
1095
  observers;
963
1096
  dynamicContexts;
964
1097
  dynamicTools;
1098
+ memory;
965
1099
  constructor(options) {
966
1100
  this.id = normalizeAgentId(options.id);
967
1101
  this.name = options.name;
@@ -980,10 +1114,25 @@ var Agent = class {
980
1114
  this.observers = options.observers ?? [];
981
1115
  this.dynamicContexts = options.dynamicContexts ?? [];
982
1116
  this.dynamicTools = options.dynamicTools ?? [];
1117
+ this.memory = options.memory;
983
1118
  }
984
1119
  prompt(prompt) {
985
1120
  return PromptRequest.fromAgent(this, prompt);
986
1121
  }
1122
+ session(sessionId, options = {}) {
1123
+ if (this.memory === void 0) {
1124
+ throw new Error(`Agent "${this.id}" has no memory store configured.`);
1125
+ }
1126
+ const normalized = sessionId.trim();
1127
+ if (normalized.length === 0) {
1128
+ throw new TypeError("Session id must be a non-empty string.");
1129
+ }
1130
+ return new AgentSession(this, {
1131
+ sessionId: normalized,
1132
+ ...options.userId === void 0 ? {} : { userId: options.userId },
1133
+ ...options.metadata === void 0 ? {} : { metadata: options.metadata }
1134
+ });
1135
+ }
987
1136
  asTool(options) {
988
1137
  const description = options.description ?? this.description ?? `Prompt the ${options.name} agent.`;
989
1138
  return createTool({
@@ -1026,6 +1175,34 @@ var Agent = class {
1026
1175
  return this.toolSet.call(toolName, args);
1027
1176
  }
1028
1177
  };
1178
+ var AgentSession = class {
1179
+ constructor(agent, context) {
1180
+ this.agent = agent;
1181
+ this.context = context;
1182
+ }
1183
+ agent;
1184
+ context;
1185
+ prompt(prompt) {
1186
+ if (Array.isArray(prompt)) {
1187
+ throw new TypeError("AgentSession.prompt does not accept Message[] transcripts.");
1188
+ }
1189
+ return PromptRequest.fromAgent(this.agent, prompt, { memoryContext: this.context });
1190
+ }
1191
+ async messages() {
1192
+ const memory = this.agent.memory;
1193
+ if (memory === void 0) {
1194
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1195
+ }
1196
+ return memory.store.load(this.context);
1197
+ }
1198
+ async clear() {
1199
+ const memory = this.agent.memory;
1200
+ if (memory === void 0) {
1201
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1202
+ }
1203
+ await memory.store.clear(this.context);
1204
+ }
1205
+ };
1029
1206
  function dynamicToolSetFromIndex(index) {
1030
1207
  const maybeIndex = index;
1031
1208
  return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : void 0;
@@ -1064,6 +1241,7 @@ var AgentBuilder = class {
1064
1241
  observerRegistrations = [];
1065
1242
  dynamicContextRegistrations = [];
1066
1243
  dynamicToolRegistrations = [];
1244
+ memoryRegistration;
1067
1245
  activeToolSet = new ToolSet();
1068
1246
  name(name) {
1069
1247
  this.agentName = name;
@@ -1148,6 +1326,13 @@ var AgentBuilder = class {
1148
1326
  });
1149
1327
  return this;
1150
1328
  }
1329
+ memory(store, options = {}) {
1330
+ this.memoryRegistration = {
1331
+ store,
1332
+ options: resolveMemoryOptions(options)
1333
+ };
1334
+ return this;
1335
+ }
1151
1336
  outputSchema(schema) {
1152
1337
  this.schema = toProviderJsonSchema(schema);
1153
1338
  return this;
@@ -1170,7 +1355,8 @@ var AgentBuilder = class {
1170
1355
  outputSchema: this.schema,
1171
1356
  observers: this.observerRegistrations,
1172
1357
  dynamicContexts: this.dynamicContextRegistrations,
1173
- dynamicTools: this.dynamicToolRegistrations
1358
+ dynamicTools: this.dynamicToolRegistrations,
1359
+ memory: this.memoryRegistration
1174
1360
  });
1175
1361
  }
1176
1362
  buildInstructions() {
@@ -1202,6 +1388,7 @@ export {
1202
1388
  PromptRequest,
1203
1389
  DEFAULT_MAX_TURNS,
1204
1390
  Agent,
1391
+ AgentSession,
1205
1392
  AgentBuilder
1206
1393
  };
1207
- //# sourceMappingURL=chunk-IQBY2GCF.js.map
1394
+ //# sourceMappingURL=chunk-JQCRURY2.js.map