@anvia/core 1.0.0-rc.1 → 1.0.0-rc.11

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 (162) hide show
  1. package/README.md +433 -85
  2. package/dist/agent/index.d.ts +57 -19
  3. package/dist/agent/index.js +32 -17
  4. package/dist/agent/interactions/index.d.ts +122 -0
  5. package/dist/agent/interactions/index.js +20 -0
  6. package/dist/agent-qRKa3ibC.d.ts +154 -0
  7. package/dist/chunk-3RM57ZT2.js +31 -0
  8. package/dist/chunk-3RM57ZT2.js.map +1 -0
  9. package/dist/chunk-3RWESPUG.js +331 -0
  10. package/dist/chunk-3RWESPUG.js.map +1 -0
  11. package/dist/{chunk-DMPUP4R3.js → chunk-3SKZ2BKP.js} +7 -5
  12. package/dist/chunk-3SKZ2BKP.js.map +1 -0
  13. package/dist/chunk-3XQGVDU5.js +69 -0
  14. package/dist/chunk-3XQGVDU5.js.map +1 -0
  15. package/dist/chunk-3ZLM6OAZ.js +4773 -0
  16. package/dist/chunk-3ZLM6OAZ.js.map +1 -0
  17. package/dist/chunk-6BBOCFHV.js +323 -0
  18. package/dist/chunk-6BBOCFHV.js.map +1 -0
  19. package/dist/chunk-7JLAIN6E.js +15 -0
  20. package/dist/chunk-7JLAIN6E.js.map +1 -0
  21. package/dist/chunk-7PHMK7W4.js +1516 -0
  22. package/dist/chunk-7PHMK7W4.js.map +1 -0
  23. package/dist/chunk-AHLKV6KP.js +86 -0
  24. package/dist/chunk-AHLKV6KP.js.map +1 -0
  25. package/dist/{chunk-FNQB2JEH.js → chunk-AZB6N7P4.js} +94 -65
  26. package/dist/chunk-AZB6N7P4.js.map +1 -0
  27. package/dist/{chunk-I6G7BC42.js → chunk-D4PACGPA.js} +2 -6
  28. package/dist/chunk-D4PACGPA.js.map +1 -0
  29. package/dist/chunk-EFLT7XZD.js +51 -0
  30. package/dist/chunk-EFLT7XZD.js.map +1 -0
  31. package/dist/{chunk-R2LJSQUW.js → chunk-EMZKPRE6.js} +267 -107
  32. package/dist/chunk-EMZKPRE6.js.map +1 -0
  33. package/dist/chunk-I3Q7XSBB.js +59 -0
  34. package/dist/chunk-I3Q7XSBB.js.map +1 -0
  35. package/dist/chunk-J6LVLV6P.js +13 -0
  36. package/dist/chunk-J6LVLV6P.js.map +1 -0
  37. package/dist/chunk-LTXABCOO.js +127 -0
  38. package/dist/chunk-LTXABCOO.js.map +1 -0
  39. package/dist/chunk-OJBFDBLG.js +164 -0
  40. package/dist/chunk-OJBFDBLG.js.map +1 -0
  41. package/dist/chunk-QGX73TSQ.js +177 -0
  42. package/dist/chunk-QGX73TSQ.js.map +1 -0
  43. package/dist/chunk-SJMSS5SI.js +171 -0
  44. package/dist/chunk-SJMSS5SI.js.map +1 -0
  45. package/dist/chunk-SPD4XVR7.js +29 -0
  46. package/dist/chunk-SPD4XVR7.js.map +1 -0
  47. package/dist/chunk-T2C3CGDQ.js +56 -0
  48. package/dist/chunk-T2C3CGDQ.js.map +1 -0
  49. package/dist/{chunk-5XKDW36Z.js → chunk-YA7BMX7D.js} +99 -57
  50. package/dist/chunk-YA7BMX7D.js.map +1 -0
  51. package/dist/chunk-ZA564323.js +331 -0
  52. package/dist/chunk-ZA564323.js.map +1 -0
  53. package/dist/completion/index.d.ts +5 -4
  54. package/dist/completion/index.js +34 -28
  55. package/dist/documents/index.d.ts +34 -0
  56. package/dist/documents/index.js +340 -0
  57. package/dist/documents/index.js.map +1 -0
  58. package/dist/{dynamic-tools-i9woysFa.d.ts → dynamic-tools-aIZlg5x1.d.ts} +21 -9
  59. package/dist/embeddings/index.d.ts +11 -9
  60. package/dist/embeddings/index.js +3 -3
  61. package/dist/evals/index.d.ts +54 -25
  62. package/dist/evals/index.js +197 -114
  63. package/dist/evals/index.js.map +1 -1
  64. package/dist/extractor/index.d.ts +23 -26
  65. package/dist/extractor/index.js +9 -8
  66. package/dist/guardrails/index.d.ts +7 -129
  67. package/dist/guardrails/index.js +1 -1
  68. package/dist/image-generation/index.d.ts +18 -16
  69. package/dist/image-generation/index.js +4 -2
  70. package/dist/index.d.ts +24 -25
  71. package/dist/index.js +80 -56
  72. package/dist/internal/agent.d.ts +33 -30
  73. package/dist/internal/agent.js +50 -15
  74. package/dist/internal/agent.js.map +1 -1
  75. package/dist/mcp/index.d.ts +7 -15
  76. package/dist/mcp/index.js +3 -348
  77. package/dist/mcp/index.js.map +1 -1
  78. package/dist/memory/index.d.ts +23 -6
  79. package/dist/memory/index.js +14 -10
  80. package/dist/{middleware-KynLg_RW.d.ts → middleware-kcF8AusP.d.ts} +10 -7
  81. package/dist/model-call-options-CZkSw_xN.d.ts +6 -0
  82. package/dist/model-listing/index.d.ts +3 -1
  83. package/dist/observability/index.d.ts +22 -6
  84. package/dist/observability/index.js +10 -5
  85. package/dist/observability/index.js.map +1 -1
  86. package/dist/pipeline/index.d.ts +103 -58
  87. package/dist/pipeline/index.js +421 -236
  88. package/dist/pipeline/index.js.map +1 -1
  89. package/dist/provider-output-error-BEwDgVv3.d.ts +99 -0
  90. package/dist/{retry-D3Ruy-ba.d.ts → retry-CjvSlKGW.d.ts} +2 -1
  91. package/dist/skills/index.d.ts +5 -4
  92. package/dist/skills/index.js +8 -7
  93. package/dist/speech-generation/index.d.ts +37 -0
  94. package/dist/speech-generation/index.js +10 -0
  95. package/dist/text-ZLECpQE7.d.ts +32 -0
  96. package/dist/{think-tool-DZqZj2LV.d.ts → think-tool-BwCsJ365.d.ts} +16 -2
  97. package/dist/tool/index.d.ts +10 -7
  98. package/dist/tool/index.js +13 -10
  99. package/dist/{tool-C_8GudXn.d.ts → tool-BpqpoRSE.d.ts} +17 -8
  100. package/dist/transcription/index.d.ts +23 -14
  101. package/dist/transcription/index.js +4 -2
  102. package/dist/{index-DxB74ybJ.d.ts → types-CLCLFpAL.d.ts} +153 -91
  103. package/dist/types-CSYt7-It.d.ts +170 -0
  104. package/dist/types-CXNE592e.d.ts +130 -0
  105. package/dist/types-Cr4uiYo5.d.ts +99 -0
  106. package/dist/{types-DhQfwiZn.d.ts → types-DC1U1XwW.d.ts} +1 -1
  107. package/dist/types-DgvozfPc.d.ts +349 -0
  108. package/dist/types-DhkodEft.d.ts +105 -0
  109. package/dist/types-DjqRHeAi.d.ts +31 -0
  110. package/dist/vector-store/index.d.ts +34 -34
  111. package/dist/vector-store/index.js +10 -6
  112. package/package.json +22 -16
  113. package/dist/agent-DGYY_onS.d.ts +0 -145
  114. package/dist/audio-generation/index.d.ts +0 -31
  115. package/dist/audio-generation/index.js +0 -8
  116. package/dist/chunk-5XKDW36Z.js.map +0 -1
  117. package/dist/chunk-6WGGTJE6.js +0 -139
  118. package/dist/chunk-6WGGTJE6.js.map +0 -1
  119. package/dist/chunk-A3UBYTJT.js +0 -157
  120. package/dist/chunk-A3UBYTJT.js.map +0 -1
  121. package/dist/chunk-ADH7NNCS.js +0 -512
  122. package/dist/chunk-ADH7NNCS.js.map +0 -1
  123. package/dist/chunk-CQNNSZPG.js +0 -9
  124. package/dist/chunk-CQNNSZPG.js.map +0 -1
  125. package/dist/chunk-DMPUP4R3.js.map +0 -1
  126. package/dist/chunk-DZ3CKYTH.js +0 -267
  127. package/dist/chunk-DZ3CKYTH.js.map +0 -1
  128. package/dist/chunk-FNQB2JEH.js.map +0 -1
  129. package/dist/chunk-I6G7BC42.js.map +0 -1
  130. package/dist/chunk-IL3ZSNJI.js +0 -3748
  131. package/dist/chunk-IL3ZSNJI.js.map +0 -1
  132. package/dist/chunk-J6GNQART.js +0 -122
  133. package/dist/chunk-J6GNQART.js.map +0 -1
  134. package/dist/chunk-M3IOWB4Y.js +0 -280
  135. package/dist/chunk-M3IOWB4Y.js.map +0 -1
  136. package/dist/chunk-QY5GZ7HR.js +0 -39
  137. package/dist/chunk-QY5GZ7HR.js.map +0 -1
  138. package/dist/chunk-R2LJSQUW.js.map +0 -1
  139. package/dist/chunk-RFDVKCIE.js +0 -12
  140. package/dist/chunk-RFDVKCIE.js.map +0 -1
  141. package/dist/chunk-RMUBRRSK.js +0 -34
  142. package/dist/chunk-RMUBRRSK.js.map +0 -1
  143. package/dist/chunk-RXKHOYEH.js +0 -466
  144. package/dist/chunk-RXKHOYEH.js.map +0 -1
  145. package/dist/chunk-XDRSPSR7.js +0 -38
  146. package/dist/chunk-XDRSPSR7.js.map +0 -1
  147. package/dist/chunk-XWUC7CIT.js +0 -1
  148. package/dist/chunk-XWUC7CIT.js.map +0 -1
  149. package/dist/errors-CYKUcU89.d.ts +0 -17
  150. package/dist/json-Zo7K3xmt.d.ts +0 -40
  151. package/dist/loaders/index.d.ts +0 -86
  152. package/dist/loaders/index.js +0 -299
  153. package/dist/loaders/index.js.map +0 -1
  154. package/dist/types-B3A4YxaA.d.ts +0 -354
  155. package/dist/types-BBEGKB9m.d.ts +0 -119
  156. package/dist/types-DT3nEemY.d.ts +0 -79
  157. package/dist/types-DulRCYdB.d.ts +0 -70
  158. package/dist/types-p55Jc-sr.d.ts +0 -47
  159. package/dist/ui/index.d.ts +0 -107
  160. package/dist/ui/index.js +0 -10
  161. /package/dist/{audio-generation → agent/interactions}/index.js.map +0 -0
  162. /package/dist/{ui → speech-generation}/index.js.map +0 -0
package/README.md CHANGED
@@ -12,10 +12,17 @@ The package is dependency-injection oriented: create provider models, typed tool
12
12
 
13
13
  ## Installation
14
14
 
15
+ Anvia 1.0 is currently published on npm under the `rc` tag. The unqualified `latest` tag remains
16
+ on the maintenance-only v0 line until 1.0 reaches general availability. Keep Core and every Anvia
17
+ adapter on the same release channel.
18
+
15
19
  ```sh
16
- pnpm add @anvia/core
20
+ pnpm add @anvia/core@rc
17
21
  ```
18
22
 
23
+ See the repository's [1.0 release policy](../../docs/releases/v1.md) for channel and support
24
+ details.
25
+
19
26
  In this monorepo, the package is available through the workspace:
20
27
 
21
28
  ```sh
@@ -33,7 +40,7 @@ const client = new OpenAIClient({
33
40
  apiKey,
34
41
  });
35
42
 
36
- const model = client.completionModel("gpt-5");
43
+ const model = client.completionModel({ modelId: "gpt-5", api: "responses" });
37
44
 
38
45
  const lookupOrder = createTool({
39
46
  name: "lookup_order",
@@ -44,141 +51,305 @@ const lookupOrder = createTool({
44
51
 
45
52
  const agent = new Agent({
46
53
  id: "support",
47
- model: model,
54
+ model,
48
55
  instructions: "Help customers with order questions.",
49
56
  maxTurns: 4,
50
57
  tools: [lookupOrder],
51
58
  });
52
59
 
53
- const response = await agent.prompt("What is happening with order A123?").send();
54
-
55
- console.log(response.output);
60
+ const result = await agent.generate({ prompt: "What is happening with order A123?" });
61
+ if (result.type === "response") console.log(result.output);
56
62
  ```
57
63
 
58
64
  ## Direct Completions
59
65
 
60
- Use `createCompletion` when you want a single provider call without agent turns, memory, or
61
- tool execution:
66
+ Use `generateCompletion` for one provider call without Agent turns, memory, or local tool
67
+ execution. The model and input are part of one options object:
62
68
 
63
69
  ```ts
64
- import { createCompletion } from "@anvia/core";
70
+ import { generateCompletion } from "@anvia/core";
65
71
  import { OpenAIClient } from "@anvia/openai";
66
72
 
67
- const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");
73
+ const model = new OpenAIClient({ apiKey }).completionModel({ modelId: "gpt-5", api: "responses" });
68
74
 
69
- const result = await createCompletion(model, {
70
- input: "Summarize Anvia in one sentence.",
75
+ const result = await generateCompletion({
76
+ model,
77
+ prompt: "Summarize Anvia in one sentence.",
71
78
  instructions: "Answer clearly and concisely.",
72
79
  });
73
80
 
74
- console.log(result.text);
81
+ console.log(result.output); // string
75
82
  ```
76
83
 
77
- Use `messages` when you already own the transcript. If `input` is also provided, it is appended as
78
- the final message:
84
+ Use `messages` when the application already owns the transcript. Exactly one of `prompt` or
85
+ `messages` is required:
79
86
 
80
87
  ```ts
81
- import { Message, createCompletion } from "@anvia/core";
88
+ import { generateCompletion, type Message } from "@anvia/core";
82
89
 
83
- const result = await createCompletion(model, {
90
+ const result = await generateCompletion({
91
+ model,
84
92
  messages: [
85
- Message.system("You are concise."),
86
- Message.user("Explain Anvia."),
87
- ],
93
+ { role: "system", content: "You are concise." },
94
+ { role: "user", content: "Explain Anvia." },
95
+ ] satisfies readonly Message[],
88
96
  maxTokens: 300,
89
- params: {
97
+ providerOptions: {
90
98
  reasoning: { effort: "low" },
91
99
  },
92
100
  });
93
101
  ```
94
102
 
95
- Use `createCompletionStream` to receive raw completion stream events from the model:
103
+ Messages are plain, readonly `{ role, content }` objects. Use structural literals with
104
+ `satisfies Message` or `satisfies readonly Message[]`; there is no message factory namespace.
105
+ Multimodal user content uses `text`, `image`, and `file` parts, while assistant tool calls use
106
+ `{ type: "tool-call", toolCallId, toolName, input }`. At external boundaries, validate with
107
+ `parseMessage`, `parseMessages`, `messageSchema`, or an application-specific
108
+ `createMessageSchema({ metadataSchema })`.
109
+
110
+ Add `outputSchema` to the same function for typed, schema-validated output:
111
+
112
+ ```ts
113
+ import { generateCompletion } from "@anvia/core";
114
+ import { z } from "zod";
115
+
116
+ const result = await generateCompletion({
117
+ model,
118
+ prompt: "Extract: Acme reports a high-priority checkout failure.",
119
+ outputSchema: z.object({
120
+ customer: z.string(),
121
+ priority: z.enum(["low", "medium", "high"]),
122
+ }),
123
+ });
124
+
125
+ console.log(result.output.priority); // fully typed
126
+ ```
127
+
128
+ `CompletionResult` consistently contains `output`, the original `text`, normalized `content`,
129
+ `usage`, and `rawResponse`, plus optional message, context, source, provider-tool, and finish-reason
130
+ metadata. First-party adapters normalize provider termination into `finishReason` (`stop`, `length`,
131
+ `content-filter`, `tool-calls`, or `other`) while preserving the provider value separately as
132
+ `providerFinishReason`.
133
+
134
+ Schema-backed completion checks termination before JSON parsing. `finishReason: "length"` throws
135
+ `CompletionStructuredOutputError` with `phase: "truncated"`; `finishReason: "content-filter"`
136
+ throws it with `phase: "content-filter"` and is not retried by the default policy. Ordinary text
137
+ completion remains intentional: partial text is returned unchanged with its finish reason so the
138
+ application decides whether to display, continue, or discard it. A direct structured stream can
139
+ similarly end with one typed error event after any already-emitted deltas; it does not silently
140
+ retract public stream progress.
141
+
142
+ Use `streamCompletion` for the streaming form:
96
143
 
97
144
  ```ts
98
- import { createCompletionStream } from "@anvia/core";
145
+ import { streamCompletion } from "@anvia/core";
99
146
 
100
- for await (const event of createCompletionStream(model, {
101
- input: "Write a short launch note.",
147
+ for await (const event of streamCompletion({
148
+ model,
149
+ prompt: "Write a short launch note.",
102
150
  })) {
103
151
  if (event.type === "text_delta") process.stdout.write(event.delta);
152
+ if (event.type === "final") console.log(event.result.usage);
153
+ if (event.type === "error") console.error(event.error);
104
154
  }
105
155
  ```
106
156
 
107
- React hooks keep `UIMessage[]` state locally, but send core `Message[]` in their request body. Pass
108
- those messages directly to `createCompletionStream`:
157
+ Tool-call deltas are always emitted when a provider supplies them; there is no opt-in flag. A
158
+ high-level stream emits at most one terminal `error` event and then closes. Provider model adapters
159
+ use the lower-level `CompletionModelStreamEvent`, whose terminal event is `{ type: "final",
160
+ response }`; `streamCompletion` normalizes it to `{ type: "final", result }`.
109
161
 
110
- ```ts
111
- import { createCompletionStream } from "@anvia/core";
112
- import type { UIStreamRequest } from "@anvia/core/ui";
162
+ Client requests carry core `Message[]`, so an endpoint can validate and pass them directly:
113
163
 
114
- const body = (await request.json()) as UIStreamRequest;
164
+ ```ts
165
+ import { completionToClientStream, parseClientStreamRequest } from "@anvia/client";
166
+ import { streamCompletion } from "@anvia/core";
167
+ import { createClientStreamResponse } from "@anvia/server";
115
168
 
116
- const events = createCompletionStream(model, {
117
- messages: body.messages,
169
+ const body = parseClientStreamRequest(await request.json());
170
+ const events = completionToClientStream({
171
+ events: streamCompletion({ model, messages: body.messages }),
118
172
  });
173
+ return createClientStreamResponse({ events });
119
174
  ```
120
175
 
121
- Use `createParsedCompletion` when you want a direct completion to return schema-validated data:
176
+ Core does not own UI messages, HTTP transports, or the public wire protocol. Those boundaries live
177
+ in `@anvia/client`, `@anvia/server`, and framework packages such as `@anvia/react`.
178
+
179
+ ## Retries, Provider Options, and Cancellation
180
+
181
+ Direct completion and media calls accept `retries?: RetryOptions | false`. An omitted or `false`
182
+ value makes one provider attempt; `{}` enables the default retry policy. Only retry-safe provider
183
+ calls are repeated.
122
184
 
123
185
  ```ts
124
- import { createParsedCompletion } from "@anvia/core";
125
- import { z } from "zod";
186
+ const controller = new AbortController();
187
+
188
+ const result = await generateCompletion({
189
+ model,
190
+ prompt: "Summarize this incident.",
191
+ retries: { maxAttempts: 3, initialDelayMs: 100, maxDelayMs: 1_000 },
192
+ abortSignal: controller.signal,
193
+ providerOptions: { reasoning: { effort: "medium" } },
194
+ });
195
+ ```
196
+
197
+ `providerOptions` contains a strict JSON object passed to an adapter. Runtime values such as
198
+ `undefined`, non-finite numbers, cycles, a top-level array, and class instances are rejected rather
199
+ than coerced. Nested arrays are valid JSON. Canonical Anvia fields such as model, input,
200
+ temperature, tools, dimensions, text, and voice take precedence over conflicting provider keys.
201
+ Cancellation is forwarded to provider SDK calls and is never retried.
202
+
203
+ ## Agents
204
+
205
+ Agents own their default retry policy. A run with no `retries` value inherits the Agent setting;
206
+ `false` disables it for that run; an object replaces it for that run. Retries apply to the current
207
+ completion only, so completed tools and earlier turns are never replayed. `maxAttempts` is the
208
+ total number of model attempts for that completion, including the initial attempt; it is not the
209
+ number of additional retries.
126
210
 
127
- const eventSchema = z.object({
128
- name: z.string(),
129
- date: z.string(),
211
+ ```ts
212
+ const agent = new Agent({
213
+ id: "support",
214
+ model,
215
+ retries: { maxAttempts: 3 },
130
216
  });
131
217
 
132
- const event = await createParsedCompletion(model, {
133
- schema: eventSchema,
134
- input: "Alice and Bob are going to a science fair on Friday.",
218
+ await agent.generate({ prompt: "Try normally." });
219
+ await agent.generate({ prompt: "Do not retry this run.", retries: false });
220
+ await agent.generate({
221
+ prompt: "Use one custom policy.",
222
+ retries: { maxAttempts: 2, initialDelayMs: 0, maxDelayMs: 0 },
135
223
  });
224
+ ```
136
225
 
137
- console.log(event.data);
226
+ When an Agent has `outputSchema`, truncation, JSON parsing, and schema-validation failures use the
227
+ same retry budget when retries are explicitly enabled. Each retry starts from the original request
228
+ rather than recursively accumulating failed attempts. Truncated output and reasoning are omitted;
229
+ parse and schema repairs include only a bounded, text-only preview of the latest failed response.
230
+ The correction asks for shorter raw JSON matching the schema, without Markdown or commentary.
231
+ Transport failures and structured-output failures therefore cannot exceed `maxAttempts` in total.
232
+ Agent response outcomes expose the last generation's `finishReason` and `providerFinishReason`, and
233
+ the same fields remain attached to each assistant message's Anvia generation metadata.
234
+
235
+ Malformed, incomplete, or non-JSON provider tool arguments fail with
236
+ `CompletionProviderOutputError`. The default policy retries only explicitly retry-safe provider
237
+ output failures when retries are enabled; it does not make arbitrary `SyntaxError` instances
238
+ retryable. Tool calls are validated as a complete set before any tool executes, and filtered tool
239
+ calls are never retried by default. Raw provider arguments are not retained in the error or retry
240
+ metadata. A no-argument tool call must still contain the JSON value `{}`; blank argument text is
241
+ malformed provider output and is never replaced with invented input.
242
+
243
+ Streaming attempts are retried only before any provider event has been exposed. Once progress has
244
+ been emitted, Anvia returns the failure without starting another provider attempt, because replaying
245
+ the stream could duplicate text, tool-call deltas, or other observable events. Applications that
246
+ want post-progress recovery must buffer attempts and define their own reset or resume protocol.
247
+
248
+ Structured output remains strict: Core trims surrounding whitespace, parses JSON, and validates the
249
+ result with the configured Zod schema. As a compatibility fallback for OpenAI-compatible providers
250
+ that violate strict JSON mode, Core also accepts a response consisting entirely of one lowercase
251
+ `json` Markdown fence or one unlabeled Markdown fence. It does not search prose for JSON, accept
252
+ content before or after a fence, or bypass schema validation. `AgentStructuredOutputError` reports
253
+ the `truncated`, `content-filter`, `parse`, or `schema` phase, attempt counts, output length/format
254
+ metadata, per-attempt and cumulative usage, finish reasons, and the original `cause` when one
255
+ exists; its message never contains the rejected model response. The `completion.retry` observer
256
+ event records the same safe diagnostics and whether failed output was omitted or previewed, never
257
+ the model output itself.
258
+
259
+ Agent outcomes are discriminated by `type`. Successful responses include typed `output` and `text`;
260
+ guardrail blocks return `type: "blocked"`, `stage`, `reason`, and an optional `message`; tool
261
+ approvals and first-class questions return a JSON-safe `type: "interaction"` outcome.
262
+
263
+ ```ts
264
+ const outcome = await agent.generate({ prompt: "Help with this request." });
265
+
266
+ switch (outcome.type) {
267
+ case "response":
268
+ console.log(outcome.output);
269
+ break;
270
+ case "blocked":
271
+ console.log(outcome.stage, outcome.reason);
272
+ break;
273
+ case "interaction": {
274
+ const response =
275
+ outcome.interaction.type === "tool-approval"
276
+ ? { type: "tool-approval" as const, approved: true }
277
+ : {
278
+ type: "tool-question" as const,
279
+ answers: outcome.interaction.questions.map((question) => ({
280
+ questionId: question.id,
281
+ value: "application-provided answer",
282
+ })),
283
+ };
284
+ const resumed = await agent.resume(outcome.continuation, response);
285
+ console.log(resumed.type, resumed.resumedFrom);
286
+ break;
287
+ }
288
+ }
138
289
  ```
139
290
 
140
- ## Prompts and Memory
291
+ Keep continuations server-side. A resumed phase receives a new `runId`; Core validates the
292
+ continuation and current Agent/tool registration but does not provide a durable continuation store
293
+ or exactly-once execution. Use `createQuestionTool({ name, description })` when a model must ask for
294
+ structured free-text or choice answers.
141
295
 
142
- Use a plain prompt for stateless calls:
296
+ Import JSON-safe interaction contracts and parsers from their browser-safe subpath. This entrypoint
297
+ does not load the Agent runtime, MCP clients, or Node infrastructure:
143
298
 
144
299
  ```ts
145
- await agent.prompt("Summarize this ticket.").send();
300
+ import {
301
+ type AgentInteractionResponse,
302
+ parseAgentInteractionRequest,
303
+ parseAgentInteractionResponse,
304
+ } from "@anvia/core/agent/interactions";
146
305
  ```
147
306
 
148
- Use a message array when you already own the transcript. The last message is the active prompt and earlier messages are request history:
307
+ An Agent with `outputSchema` carries that output type through `generate`, `stream`, `asTool`, and
308
+ Pipeline Agent stages. `stream()` returns a handle with one event consumer plus final-value
309
+ promises, steering, and cancellation. Terminal agent events are the flattened outcomes themselves;
310
+ there is no extra `final.result` wrapper:
149
311
 
150
312
  ```ts
151
- import { Message } from "@anvia/core";
152
-
153
- await agent
154
- .prompt([
155
- Message.user("My project is named Anvia."),
156
- Message.assistant("Noted."),
157
- Message.user("What is my project named?"),
158
- ])
159
- .send();
313
+ const stream = agent.stream({ prompt: "Help with this request." });
314
+
315
+ for await (const event of stream.events) {
316
+ if (event.type === "text_delta") process.stdout.write(event.delta);
317
+ if (event.type === "response") console.log(event.output);
318
+ if (event.type === "interaction") console.log(event.interaction);
319
+ if (event.type === "blocked") console.log(event.reason);
320
+ }
321
+
322
+ const outcome = await stream.result;
323
+ console.log(outcome.type, await stream.text);
160
324
  ```
161
325
 
162
- Configure durable conversation memory on the agent, then run through a session:
326
+ Use `stream.textStream` instead of `stream.events` when only text deltas are needed. Event and text
327
+ iteration share one underlying stream and therefore cannot be consumed independently.
328
+
329
+ Pass `abortSignal` on a run to cancel the active provider call, tools, and nested Agent tools.
330
+
331
+ ## Memory
332
+
333
+ Configure durable conversation memory on the Agent, then run through a session:
163
334
 
164
335
  ```ts
165
336
  import { Agent, type MemoryStore, type Message } from "@anvia/core";
166
- import type { MemoryAppendInput, MemoryContext } from "@anvia/core/memory";
337
+ import type { MemoryAppendOptions, MemoryScope } from "@anvia/core/memory";
167
338
 
168
339
  class AppMemoryStore implements MemoryStore {
169
340
  private readonly sessions = new Map<string, Message[]>();
170
341
 
171
- async load(context: MemoryContext): Promise<Message[]> {
172
- return [...(this.sessions.get(context.sessionId) ?? [])];
342
+ async load({ scope }: { scope: MemoryScope }): Promise<Message[]> {
343
+ return [...(this.sessions.get(scope.sessionId) ?? [])];
173
344
  }
174
345
 
175
- async append(input: MemoryAppendInput): Promise<void> {
176
- const current = this.sessions.get(input.context.sessionId) ?? [];
177
- this.sessions.set(input.context.sessionId, [...current, ...input.messages]);
346
+ async append(input: MemoryAppendOptions): Promise<void> {
347
+ const current = this.sessions.get(input.scope.sessionId) ?? [];
348
+ this.sessions.set(input.scope.sessionId, [...current, ...input.messages]);
178
349
  }
179
350
 
180
- async clear(context: MemoryContext): Promise<void> {
181
- this.sessions.delete(context.sessionId);
351
+ async clear({ scope }: { scope: MemoryScope }): Promise<void> {
352
+ this.sessions.delete(scope.sessionId);
182
353
  }
183
354
  }
184
355
 
@@ -189,8 +360,9 @@ const agent = new Agent({
189
360
  memory: { store: memory },
190
361
  });
191
362
 
192
- await agent.session("thread_123", { userId: "user_456" }).prompt("Remember my plan.").send();
193
- await agent.session("thread_123", { userId: "user_456" }).prompt("What is my plan?").send();
363
+ const session = { sessionId: "thread_123", userId: "user_456" };
364
+ await agent.generate({ prompt: "Remember my plan.", session });
365
+ await agent.generate({ prompt: "What is my plan?", session });
194
366
  ```
195
367
 
196
368
  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:
@@ -203,10 +375,89 @@ new Agent({
203
375
  });
204
376
  ```
205
377
 
378
+ Without `session`, the same Agent is stateless. Pass `{ messages }` when the caller already owns a
379
+ complete transcript; transcripts cannot be combined with persisted sessions.
380
+
381
+ Compaction is an explicit Agent policy over a store capability. The adapter persists the summary as
382
+ an ordinary system message with `metadata.anvia.memoryCompaction`, so `load()` and inspectors expose
383
+ exactly what future runs receive:
384
+
385
+ ```ts
386
+ import { createSummaryMemoryCompactor } from "@anvia/core/memory";
387
+
388
+ const compactor = createSummaryMemoryCompactor({
389
+ model: summaryModel,
390
+ maxTokens: 1024,
391
+ retries: { maxAttempts: 2 },
392
+ });
393
+
394
+ const agent = new Agent({
395
+ id: "support",
396
+ model,
397
+ memory: {
398
+ store: memory,
399
+ savePolicy: "message",
400
+ compaction: {
401
+ trigger: { afterTokens: 32_000 },
402
+ retention: { recentTokens: 8_000 },
403
+ compactor,
404
+ conflictRetries: false,
405
+ },
406
+ },
407
+ });
408
+
409
+ for await (const event of agent.stream({ prompt: "What did we decide?", session })) {
410
+ if (event.type === "memory_compaction") {
411
+ console.log({
412
+ messages: event.compactedMessageCount,
413
+ beforeTokens: event.originalTokenCount,
414
+ afterTokens: event.resultTokenCount,
415
+ usage: event.usage,
416
+ });
417
+ }
418
+ }
419
+ ```
420
+
421
+ The trigger includes the stored transcript and the incoming user message. Retention keeps as many
422
+ recent complete user-led turns as fit the configured budget; the newest complete turn is always
423
+ kept and never split, even when it exceeds that budget. When `retention` is omitted, it defaults to
424
+ 25% of `afterTokens`.
425
+
426
+ The built-in token counter is a lightweight provider-neutral estimate. Pass `tokenCounter` when you
427
+ have a model-specific tokenizer or counting service. A custom counter may be async, can be called
428
+ multiple times, and must return deterministic, monotonic, nonnegative safe integers:
429
+
430
+ ```ts
431
+ compaction: {
432
+ trigger: { afterTokens: 32_000 },
433
+ retention: { recentTokens: 8_000 },
434
+ tokenCounter: (messages) => tokenizer.count(messages),
435
+ compactor,
436
+ }
437
+ ```
438
+
439
+ You can force the configured policy without starting an Agent run. The result makes a no-op
440
+ explicit rather than throwing:
441
+
442
+ ```ts
443
+ const result = await agent.compactMemory({ session });
444
+
445
+ if (result.type === "compacted") {
446
+ console.log(result.originalTokenCount, result.resultTokenCount);
447
+ } else {
448
+ console.log(result.reason); // "nothing_to_compact"
449
+ }
450
+ ```
451
+
452
+ The automatic trigger is a threshold, not a hard storage limit. Summary-provider retries belong to
453
+ the compactor; full snapshot-to-replacement conflict retries are separately opt-in. A streamed
454
+ `memory_compaction` event is emitted only after the store atomically commits the replacement.
455
+
206
456
  ## Structured Extraction
207
457
 
208
458
  ```ts
209
- import { ExtractorBuilder } from "@anvia/core/extractor";
459
+ import { extract } from "@anvia/core/extractor";
460
+ import { z } from "zod";
210
461
 
211
462
  const ticketSchema = z.object({
212
463
  customer: z.string(),
@@ -214,45 +465,142 @@ const ticketSchema = z.object({
214
465
  summary: z.string(),
215
466
  });
216
467
 
217
- const extractor = new ExtractorBuilder(model, ticketSchema).retries(1).build();
218
-
219
- const ticket = await extractor.extract(
220
- "Acme Co. reports checkout failures. Priority is high.",
221
- );
468
+ const { output: ticket } = await extract({
469
+ model,
470
+ text: "Acme Co. reports checkout failures. Priority is high.",
471
+ outputSchema: ticketSchema,
472
+ retries: { maxAttempts: 2 },
473
+ });
222
474
  ```
223
475
 
224
476
  ## Pipelines
225
477
 
226
478
  ```ts
227
- import { PipelineBuilder } from "@anvia/core/pipeline";
479
+ import { Pipeline } from "@anvia/core/pipeline";
228
480
  import { z } from "zod";
229
481
 
230
- const pipeline = new PipelineBuilder(z.string())
231
- .step((input) => `Extract this support ticket:\n\n${input}`)
232
- .prompt(agent)
233
- .extract(extractor)
234
- .build();
482
+ const pipeline = new Pipeline({ id: "support-flow", inputSchema: z.string() })
483
+ .agent({
484
+ id: "draft",
485
+ agent,
486
+ suspension: "reject",
487
+ request: ({ input }) => ({ prompt: `Draft a reply for this ticket:\n\n${input}` }),
488
+ })
489
+ .extract({
490
+ id: "parse",
491
+ model,
492
+ outputSchema: ticketSchema,
493
+ text: ({ input }) => input,
494
+ });
495
+
496
+ const { runId, output } = await pipeline.run({
497
+ input: "Customer cannot complete checkout.",
498
+ });
499
+ ```
500
+
501
+ ## Documents
235
502
 
236
- const result = await pipeline.run("Customer cannot complete checkout.");
503
+ Applications own file discovery, file reads, source metadata, and per-file error policy. Core only
504
+ provides deterministic in-memory chunking and scoped PDF text extraction:
505
+
506
+ PDF extraction uses the optional `pdfjs-dist` peer dependency. Install it in applications that call
507
+ `extractPdfText`:
508
+
509
+ ```sh
510
+ pnpm add pdfjs-dist
511
+ ```
512
+
513
+ ```ts
514
+ import { readFile } from "node:fs/promises";
515
+ import { chunkText, extractPdfText } from "@anvia/core/documents";
516
+
517
+ const text = await readFile("guide.txt", "utf8");
518
+ const chunks = chunkText({
519
+ text,
520
+ strategy: "recursive",
521
+ maxSize: 1_000,
522
+ overlap: 100,
523
+ separators: ["\n\n", "\n", " "],
524
+ });
525
+
526
+ const { pages } = await extractPdfText({
527
+ data: new Uint8Array(await readFile("guide.pdf")),
528
+ abortSignal,
529
+ });
530
+ ```
531
+
532
+ Use `strategy: "fixed"` for deterministic sliding windows. Recursive chunking requires an explicit
533
+ separator order and falls back to fixed-size splitting when none of those separators can divide an
534
+ oversized section. Chunk offsets use JavaScript string indices and always identify the exact source
535
+ slice. PDF pages are one-based, and the parser task is disposed before extraction settles.
536
+ If parsing or abort handling fails together with parser cleanup, extraction rejects with an
537
+ `AggregateError` containing the operation failure first and the cleanup failure second.
538
+
539
+ ## Media
540
+
541
+ Media helpers follow the same one-object API and share `providerOptions`, `retries`, and
542
+ `abortSignal`:
543
+
544
+ ```ts
545
+ import { generateImage, generateSpeech, transcribe } from "@anvia/core";
546
+
547
+ const image = await generateImage({
548
+ model: client.imageGenerationModel({ modelId: "gpt-image-1" }),
549
+ prompt: "A compact robot drawing an architecture diagram.",
550
+ width: 1024,
551
+ height: 1024,
552
+ });
553
+ console.log(image.images[0].data);
554
+
555
+ const speech = await generateSpeech({
556
+ model: client.speechGenerationModel({ modelId: "gpt-4o-mini-tts" }),
557
+ text: "Hello from Anvia.",
558
+ voice: "alloy",
559
+ });
560
+
561
+ const transcript = await transcribe({
562
+ model: client.transcriptionModel({ modelId: "gpt-4o-mini-transcribe" }),
563
+ audio: {
564
+ data: speech.audio.data,
565
+ filename: "speech.mp3",
566
+ mediaType: speech.audio.mediaType,
567
+ },
568
+ });
569
+ console.log(transcript.text);
237
570
  ```
238
571
 
572
+ ## MCP
573
+
574
+ MCP clients live in the optional `@anvia/mcp` package. Core retains the lightweight registration
575
+ contracts that let agents receive immutable MCP server snapshots without owning or closing their
576
+ underlying transports:
577
+
578
+ ```sh
579
+ pnpm add @anvia/mcp
580
+ ```
581
+
582
+ Import `McpClient` and `McpClientGroup` from `@anvia/mcp`, connect them, and pass the resulting
583
+ `servers` to `new Agent({ mcpServers })`. See the `@anvia/mcp` README for transport configuration,
584
+ connection ownership, URL safety, and cleanup.
585
+
239
586
  ## Public Areas
240
587
 
241
- - `agent`: agent runtime and the compatibility `AgentBuilder`
588
+ - `agent`: typed Agent runtime, retries, and stream events
589
+ - `agent/interactions`: browser-safe interaction contracts, schemas, assertions, and parsers
242
590
  - `tool`: typed tool creation and tool sets
243
- - `completion`: provider-neutral completion request and response types
591
+ - `completion`: direct completion helpers and provider-neutral model contracts
244
592
  - `memory`: durable session memory interfaces and in-memory store
245
593
  - `extractor`: schema-first structured extraction
246
594
  - `pipeline`: typed sequential and parallel workflows
247
595
  - `embeddings`: embedding helpers and document embedding utilities
248
596
  - `vector-store`: in-memory vector search and vector search tools
249
597
  - `streaming`: normalized stream helpers
250
- - `mcp`: MCP server connection helpers
598
+ - `mcp`: lightweight MCP tool and server registration contracts used by Agent
251
599
  - `skills`: local skill loading
252
600
  - `observability`: observer interfaces for runs, generations, and tool calls
253
601
  - `evals`: evaluation helpers and reporters
254
- - `loaders`: document loading utilities
255
- - `audio-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
602
+ - `documents`: in-memory text chunking and PDF text extraction
603
+ - `speech-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
256
604
 
257
605
  ## Development
258
606