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

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 (151) hide show
  1. package/README.md +344 -85
  2. package/dist/agent/index.d.ts +33 -18
  3. package/dist/agent/index.js +42 -17
  4. package/dist/agent-CXScUOpX.d.ts +150 -0
  5. package/dist/{chunk-R2LJSQUW.js → chunk-3CMWKK32.js} +260 -104
  6. package/dist/chunk-3CMWKK32.js.map +1 -0
  7. package/dist/chunk-3RM57ZT2.js +31 -0
  8. package/dist/chunk-3RM57ZT2.js.map +1 -0
  9. package/dist/chunk-5MK3BISB.js +704 -0
  10. package/dist/chunk-5MK3BISB.js.map +1 -0
  11. package/dist/chunk-AI5JMUW7.js +45 -0
  12. package/dist/chunk-AI5JMUW7.js.map +1 -0
  13. package/dist/{chunk-FNQB2JEH.js → chunk-AZB6N7P4.js} +94 -65
  14. package/dist/chunk-AZB6N7P4.js.map +1 -0
  15. package/dist/chunk-BEYBU7VJ.js +53 -0
  16. package/dist/chunk-BEYBU7VJ.js.map +1 -0
  17. package/dist/chunk-BY4OMNIU.js +4699 -0
  18. package/dist/chunk-BY4OMNIU.js.map +1 -0
  19. package/dist/chunk-D2ECOWN5.js +515 -0
  20. package/dist/chunk-D2ECOWN5.js.map +1 -0
  21. package/dist/{chunk-A3UBYTJT.js → chunk-FN7HLLAZ.js} +60 -6
  22. package/dist/chunk-FN7HLLAZ.js.map +1 -0
  23. package/dist/chunk-GKDERULJ.js +50 -0
  24. package/dist/chunk-GKDERULJ.js.map +1 -0
  25. package/dist/chunk-GQL5KRAH.js +331 -0
  26. package/dist/chunk-GQL5KRAH.js.map +1 -0
  27. package/dist/{chunk-5XKDW36Z.js → chunk-KFR4CDGK.js} +70 -75
  28. package/dist/chunk-KFR4CDGK.js.map +1 -0
  29. package/dist/chunk-KV6QCRQ4.js +171 -0
  30. package/dist/chunk-KV6QCRQ4.js.map +1 -0
  31. package/dist/{chunk-DMPUP4R3.js → chunk-LXMAFD23.js} +5 -5
  32. package/dist/{chunk-I6G7BC42.js → chunk-MGNTXGTX.js} +2 -6
  33. package/dist/chunk-MGNTXGTX.js.map +1 -0
  34. package/dist/chunk-PK5LKOFL.js +239 -0
  35. package/dist/chunk-PK5LKOFL.js.map +1 -0
  36. package/dist/chunk-QOUPWOGW.js +29 -0
  37. package/dist/chunk-QOUPWOGW.js.map +1 -0
  38. package/dist/chunk-TRQ3XRFD.js +54 -0
  39. package/dist/chunk-TRQ3XRFD.js.map +1 -0
  40. package/dist/chunk-VKYZZXP5.js +323 -0
  41. package/dist/chunk-VKYZZXP5.js.map +1 -0
  42. package/dist/chunk-ZCPOIDAQ.js +127 -0
  43. package/dist/chunk-ZCPOIDAQ.js.map +1 -0
  44. package/dist/client-DvUatElp.d.ts +84 -0
  45. package/dist/completion/index.d.ts +5 -4
  46. package/dist/completion/index.js +25 -28
  47. package/dist/documents/index.d.ts +34 -0
  48. package/dist/documents/index.js +315 -0
  49. package/dist/documents/index.js.map +1 -0
  50. package/dist/{dynamic-tools-i9woysFa.d.ts → dynamic-tools-AEBOTgu1.d.ts} +21 -9
  51. package/dist/embeddings/index.d.ts +11 -9
  52. package/dist/embeddings/index.js +3 -3
  53. package/dist/evals/index.d.ts +53 -25
  54. package/dist/evals/index.js +153 -103
  55. package/dist/evals/index.js.map +1 -1
  56. package/dist/extractor/index.d.ts +23 -26
  57. package/dist/extractor/index.js +7 -8
  58. package/dist/guardrails/index.d.ts +7 -129
  59. package/dist/guardrails/index.js +1 -1
  60. package/dist/image-generation/index.d.ts +18 -16
  61. package/dist/image-generation/index.js +2 -2
  62. package/dist/index.d.ts +26 -23
  63. package/dist/index.js +71 -48
  64. package/dist/interactions-csssHYcN.d.ts +174 -0
  65. package/dist/internal/agent.d.ts +36 -29
  66. package/dist/internal/agent.js +48 -15
  67. package/dist/internal/agent.js.map +1 -1
  68. package/dist/mcp/index.d.ts +19 -13
  69. package/dist/mcp/index.js +14 -348
  70. package/dist/mcp/index.js.map +1 -1
  71. package/dist/memory/index.d.ts +20 -6
  72. package/dist/memory/index.js +10 -10
  73. package/dist/message-schema-B2M7AYzR.d.ts +52 -0
  74. package/dist/model-call-options-CZkSw_xN.d.ts +6 -0
  75. package/dist/model-listing/index.d.ts +3 -1
  76. package/dist/observability/index.d.ts +21 -6
  77. package/dist/observability/index.js +8 -5
  78. package/dist/observability/index.js.map +1 -1
  79. package/dist/pipeline/index.d.ts +106 -57
  80. package/dist/pipeline/index.js +419 -236
  81. package/dist/pipeline/index.js.map +1 -1
  82. package/dist/{retry-D3Ruy-ba.d.ts → retry-CjvSlKGW.d.ts} +2 -1
  83. package/dist/skills/index.d.ts +5 -4
  84. package/dist/skills/index.js +8 -7
  85. package/dist/speech-generation/index.d.ts +37 -0
  86. package/dist/speech-generation/index.js +8 -0
  87. package/dist/text-C_6eKrbC.d.ts +32 -0
  88. package/dist/{think-tool-DZqZj2LV.d.ts → think-tool-D8frXxwV.d.ts} +16 -2
  89. package/dist/tool/index.d.ts +9 -7
  90. package/dist/tool/index.js +12 -10
  91. package/dist/{tool-C_8GudXn.d.ts → tool-r62gf3QY.d.ts} +17 -8
  92. package/dist/transcription/index.d.ts +23 -14
  93. package/dist/transcription/index.js +2 -2
  94. package/dist/types-C3gv9EX6.d.ts +130 -0
  95. package/dist/{index-DxB74ybJ.d.ts → types-CElwurHZ.d.ts} +139 -88
  96. package/dist/types-CIPaIbZo.d.ts +105 -0
  97. package/dist/types-Cr4uiYo5.d.ts +99 -0
  98. package/dist/types-D9rKQ_as.d.ts +341 -0
  99. package/dist/types-DbolvLtZ.d.ts +155 -0
  100. package/dist/{types-DhQfwiZn.d.ts → types-RT3cuyRk.d.ts} +1 -1
  101. package/dist/vector-store/index.d.ts +33 -34
  102. package/dist/vector-store/index.js +7 -6
  103. package/package.json +10 -14
  104. package/dist/agent-DGYY_onS.d.ts +0 -145
  105. package/dist/audio-generation/index.d.ts +0 -31
  106. package/dist/audio-generation/index.js +0 -8
  107. package/dist/chunk-5XKDW36Z.js.map +0 -1
  108. package/dist/chunk-6WGGTJE6.js +0 -139
  109. package/dist/chunk-6WGGTJE6.js.map +0 -1
  110. package/dist/chunk-A3UBYTJT.js.map +0 -1
  111. package/dist/chunk-ADH7NNCS.js +0 -512
  112. package/dist/chunk-ADH7NNCS.js.map +0 -1
  113. package/dist/chunk-DZ3CKYTH.js +0 -267
  114. package/dist/chunk-DZ3CKYTH.js.map +0 -1
  115. package/dist/chunk-FNQB2JEH.js.map +0 -1
  116. package/dist/chunk-I6G7BC42.js.map +0 -1
  117. package/dist/chunk-IL3ZSNJI.js +0 -3748
  118. package/dist/chunk-IL3ZSNJI.js.map +0 -1
  119. package/dist/chunk-J6GNQART.js +0 -122
  120. package/dist/chunk-J6GNQART.js.map +0 -1
  121. package/dist/chunk-M3IOWB4Y.js +0 -280
  122. package/dist/chunk-M3IOWB4Y.js.map +0 -1
  123. package/dist/chunk-QY5GZ7HR.js +0 -39
  124. package/dist/chunk-QY5GZ7HR.js.map +0 -1
  125. package/dist/chunk-R2LJSQUW.js.map +0 -1
  126. package/dist/chunk-RFDVKCIE.js +0 -12
  127. package/dist/chunk-RFDVKCIE.js.map +0 -1
  128. package/dist/chunk-RMUBRRSK.js +0 -34
  129. package/dist/chunk-RMUBRRSK.js.map +0 -1
  130. package/dist/chunk-RXKHOYEH.js +0 -466
  131. package/dist/chunk-RXKHOYEH.js.map +0 -1
  132. package/dist/chunk-XDRSPSR7.js +0 -38
  133. package/dist/chunk-XDRSPSR7.js.map +0 -1
  134. package/dist/chunk-XWUC7CIT.js +0 -1
  135. package/dist/chunk-XWUC7CIT.js.map +0 -1
  136. package/dist/errors-CYKUcU89.d.ts +0 -17
  137. package/dist/json-Zo7K3xmt.d.ts +0 -40
  138. package/dist/loaders/index.d.ts +0 -86
  139. package/dist/loaders/index.js +0 -299
  140. package/dist/loaders/index.js.map +0 -1
  141. package/dist/middleware-KynLg_RW.d.ts +0 -56
  142. package/dist/types-B3A4YxaA.d.ts +0 -354
  143. package/dist/types-BBEGKB9m.d.ts +0 -119
  144. package/dist/types-DT3nEemY.d.ts +0 -79
  145. package/dist/types-DulRCYdB.d.ts +0 -70
  146. package/dist/types-p55Jc-sr.d.ts +0 -47
  147. package/dist/ui/index.d.ts +0 -107
  148. package/dist/ui/index.js +0 -10
  149. package/dist/ui/index.js.map +0 -1
  150. /package/dist/{chunk-DMPUP4R3.js.map → chunk-LXMAFD23.js.map} +0 -0
  151. /package/dist/{audio-generation → speech-generation}/index.js.map +0 -0
package/README.md CHANGED
@@ -33,7 +33,7 @@ const client = new OpenAIClient({
33
33
  apiKey,
34
34
  });
35
35
 
36
- const model = client.completionModel("gpt-5");
36
+ const model = client.completionModel({ modelId: "gpt-5", api: "responses" });
37
37
 
38
38
  const lookupOrder = createTool({
39
39
  name: "lookup_order",
@@ -44,141 +44,231 @@ const lookupOrder = createTool({
44
44
 
45
45
  const agent = new Agent({
46
46
  id: "support",
47
- model: model,
47
+ model,
48
48
  instructions: "Help customers with order questions.",
49
49
  maxTurns: 4,
50
50
  tools: [lookupOrder],
51
51
  });
52
52
 
53
- const response = await agent.prompt("What is happening with order A123?").send();
54
-
55
- console.log(response.output);
53
+ const result = await agent.generate({ prompt: "What is happening with order A123?" });
54
+ if (result.status === "completed") console.log(result.output);
56
55
  ```
57
56
 
58
57
  ## Direct Completions
59
58
 
60
- Use `createCompletion` when you want a single provider call without agent turns, memory, or
61
- tool execution:
59
+ Use `generateCompletion` for one provider call without Agent turns, memory, or local tool
60
+ execution. The model and input are part of one options object:
62
61
 
63
62
  ```ts
64
- import { createCompletion } from "@anvia/core";
63
+ import { generateCompletion } from "@anvia/core";
65
64
  import { OpenAIClient } from "@anvia/openai";
66
65
 
67
- const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");
66
+ const model = new OpenAIClient({ apiKey }).completionModel({ modelId: "gpt-5", api: "responses" });
68
67
 
69
- const result = await createCompletion(model, {
70
- input: "Summarize Anvia in one sentence.",
68
+ const result = await generateCompletion({
69
+ model,
70
+ prompt: "Summarize Anvia in one sentence.",
71
71
  instructions: "Answer clearly and concisely.",
72
72
  });
73
73
 
74
- console.log(result.text);
74
+ console.log(result.output); // string
75
75
  ```
76
76
 
77
- Use `messages` when you already own the transcript. If `input` is also provided, it is appended as
78
- the final message:
77
+ Use `messages` when the application already owns the transcript. Exactly one of `prompt` or
78
+ `messages` is required:
79
79
 
80
80
  ```ts
81
- import { Message, createCompletion } from "@anvia/core";
81
+ import { generateCompletion, type Message } from "@anvia/core";
82
82
 
83
- const result = await createCompletion(model, {
83
+ const result = await generateCompletion({
84
+ model,
84
85
  messages: [
85
- Message.system("You are concise."),
86
- Message.user("Explain Anvia."),
87
- ],
86
+ { role: "system", content: "You are concise." },
87
+ { role: "user", content: "Explain Anvia." },
88
+ ] satisfies readonly Message[],
88
89
  maxTokens: 300,
89
- params: {
90
+ providerOptions: {
90
91
  reasoning: { effort: "low" },
91
92
  },
92
93
  });
93
94
  ```
94
95
 
95
- Use `createCompletionStream` to receive raw completion stream events from the model:
96
+ Messages are plain, readonly `{ role, content }` objects. Use structural literals with
97
+ `satisfies Message` or `satisfies readonly Message[]`; there is no message factory namespace.
98
+ Multimodal user content uses `text`, `image`, and `file` parts, while assistant tool calls use
99
+ `{ type: "tool-call", toolCallId, toolName, input }`. At external boundaries, validate with
100
+ `parseMessage`, `parseMessages`, `messageSchema`, or an application-specific
101
+ `createMessageSchema({ metadataSchema })`.
102
+
103
+ Add `outputSchema` to the same function for typed, schema-validated output:
104
+
105
+ ```ts
106
+ import { generateCompletion } from "@anvia/core";
107
+ import { z } from "zod";
108
+
109
+ const result = await generateCompletion({
110
+ model,
111
+ prompt: "Extract: Acme reports a high-priority checkout failure.",
112
+ outputSchema: z.object({
113
+ customer: z.string(),
114
+ priority: z.enum(["low", "medium", "high"]),
115
+ }),
116
+ });
117
+
118
+ console.log(result.output.priority); // fully typed
119
+ ```
120
+
121
+ `CompletionResult` consistently contains `output`, the original `text`, normalized `content`,
122
+ `usage`, and `rawResponse`, plus optional message, context, source, and provider-tool metadata.
123
+
124
+ Use `streamCompletion` for the streaming form:
96
125
 
97
126
  ```ts
98
- import { createCompletionStream } from "@anvia/core";
127
+ import { streamCompletion } from "@anvia/core";
99
128
 
100
- for await (const event of createCompletionStream(model, {
101
- input: "Write a short launch note.",
129
+ for await (const event of streamCompletion({
130
+ model,
131
+ prompt: "Write a short launch note.",
102
132
  })) {
103
133
  if (event.type === "text_delta") process.stdout.write(event.delta);
134
+ if (event.type === "final") console.log(event.result.usage);
135
+ if (event.type === "error") console.error(event.error);
104
136
  }
105
137
  ```
106
138
 
107
- React hooks keep `UIMessage[]` state locally, but send core `Message[]` in their request body. Pass
108
- those messages directly to `createCompletionStream`:
139
+ Tool-call deltas are always emitted when a provider supplies them; there is no opt-in flag. A
140
+ high-level stream emits at most one terminal `error` event and then closes. Provider model adapters
141
+ use the lower-level `CompletionModelStreamEvent`, whose terminal event is `{ type: "final",
142
+ response }`; `streamCompletion` normalizes it to `{ type: "final", result }`.
109
143
 
110
- ```ts
111
- import { createCompletionStream } from "@anvia/core";
112
- import type { UIStreamRequest } from "@anvia/core/ui";
144
+ Client requests carry core `Message[]`, so an endpoint can validate and pass them directly:
113
145
 
114
- const body = (await request.json()) as UIStreamRequest;
146
+ ```ts
147
+ import { completionToClientStream, parseClientStreamRequest } from "@anvia/client";
148
+ import { streamCompletion } from "@anvia/core";
149
+ import { createClientStreamResponse } from "@anvia/server";
115
150
 
116
- const events = createCompletionStream(model, {
117
- messages: body.messages,
151
+ const body = parseClientStreamRequest(await request.json());
152
+ const events = completionToClientStream({
153
+ events: streamCompletion({ model, messages: body.messages }),
118
154
  });
155
+ return createClientStreamResponse({ events });
119
156
  ```
120
157
 
121
- Use `createParsedCompletion` when you want a direct completion to return schema-validated data:
158
+ Core does not own UI messages, HTTP transports, or the public wire protocol. Those boundaries live
159
+ in `@anvia/client`, `@anvia/server`, and framework packages such as `@anvia/react`.
160
+
161
+ ## Retries, Provider Options, and Cancellation
162
+
163
+ Direct completion and media calls accept `retries?: RetryOptions | false`. An omitted or `false`
164
+ value makes one provider attempt; `{}` enables the default retry policy. Only retry-safe provider
165
+ calls are repeated.
122
166
 
123
167
  ```ts
124
- import { createParsedCompletion } from "@anvia/core";
125
- import { z } from "zod";
168
+ const controller = new AbortController();
126
169
 
127
- const eventSchema = z.object({
128
- name: z.string(),
129
- date: z.string(),
170
+ const result = await generateCompletion({
171
+ model,
172
+ prompt: "Summarize this incident.",
173
+ retries: { maxAttempts: 3, initialDelayMs: 100, maxDelayMs: 1_000 },
174
+ abortSignal: controller.signal,
175
+ providerOptions: { reasoning: { effort: "medium" } },
130
176
  });
177
+ ```
131
178
 
132
- const event = await createParsedCompletion(model, {
133
- schema: eventSchema,
134
- input: "Alice and Bob are going to a science fair on Friday.",
179
+ `providerOptions` contains strict JSON passed to an adapter. Canonical Anvia fields such as model,
180
+ input, temperature, tools, dimensions, text, and voice take precedence over conflicting provider
181
+ keys. Cancellation is forwarded to provider SDK calls and is never retried.
182
+
183
+ ## Agents
184
+
185
+ Agents own their default retry policy. A run with no `retries` value inherits the Agent setting;
186
+ `false` disables it for that run; an object replaces it for that run. Retries apply to the current
187
+ provider call only, so completed tools and earlier turns are never replayed.
188
+
189
+ ```ts
190
+ const agent = new Agent({
191
+ id: "support",
192
+ model,
193
+ retries: { maxAttempts: 3 },
135
194
  });
136
195
 
137
- console.log(event.data);
196
+ await agent.generate({ prompt: "Try normally." });
197
+ await agent.generate({ prompt: "Do not retry this run.", retries: false });
198
+ await agent.generate({
199
+ prompt: "Use one custom policy.",
200
+ retries: { maxAttempts: 2, initialDelayMs: 0, maxDelayMs: 0 },
201
+ });
138
202
  ```
139
203
 
140
- ## Prompts and Memory
141
-
142
- Use a plain prompt for stateless calls:
204
+ Agent results are discriminated by `status`. Completed results include typed `output` and `text`;
205
+ guardrail blocks return `status: "blocked"`, `stage`, and `text`; tool approvals and first-class
206
+ questions return a JSON-safe `status: "suspended"` result.
143
207
 
144
208
  ```ts
145
- await agent.prompt("Summarize this ticket.").send();
209
+ const result = await agent.generate({ prompt: "Help with this request." });
210
+
211
+ if (result.status === "completed") console.log(result.output);
212
+ if (result.status === "blocked") console.log(result.stage, result.text);
213
+ if (result.status === "suspended") {
214
+ const resumed = await agent.generate({
215
+ continuation: result.continuation,
216
+ response:
217
+ result.interaction.type === "tool-approval"
218
+ ? { type: "tool-approval", approved: true }
219
+ : {
220
+ type: "tool-question",
221
+ answers: result.interaction.questions.map((question) => ({
222
+ questionId: question.id,
223
+ value: "application-provided answer",
224
+ })),
225
+ },
226
+ });
227
+ console.log(resumed.status, resumed.resumedFrom);
228
+ }
146
229
  ```
147
230
 
148
- Use a message array when you already own the transcript. The last message is the active prompt and earlier messages are request history:
231
+ Keep continuations server-side. A resumed phase receives a new `runId`; Core validates the
232
+ continuation and current Agent/tool registration but does not provide a durable continuation store
233
+ or exactly-once execution. Use `createQuestionTool({ name, description })` when a model must ask for
234
+ structured free-text or choice answers.
235
+
236
+ An Agent with `outputSchema` carries that output type through `generate`, `stream`, `asTool`, and
237
+ Pipeline Agent stages. Agent stream finals use the same result shape:
149
238
 
150
239
  ```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();
240
+ for await (const event of agent.stream({ prompt: "Help with this request." })) {
241
+ if (event.type === "final") {
242
+ if (event.result.status === "completed") console.log(event.result.output);
243
+ else console.log(event.result.stage, event.result.text);
244
+ }
245
+ }
160
246
  ```
161
247
 
162
- Configure durable conversation memory on the agent, then run through a session:
248
+ Pass `abortSignal` on a run to cancel the active provider call, tools, and nested Agent tools.
249
+
250
+ ## Memory
251
+
252
+ Configure durable conversation memory on the Agent, then run through a session:
163
253
 
164
254
  ```ts
165
255
  import { Agent, type MemoryStore, type Message } from "@anvia/core";
166
- import type { MemoryAppendInput, MemoryContext } from "@anvia/core/memory";
256
+ import type { MemoryAppendOptions, MemoryScope } from "@anvia/core/memory";
167
257
 
168
258
  class AppMemoryStore implements MemoryStore {
169
259
  private readonly sessions = new Map<string, Message[]>();
170
260
 
171
- async load(context: MemoryContext): Promise<Message[]> {
172
- return [...(this.sessions.get(context.sessionId) ?? [])];
261
+ async load({ scope }: { scope: MemoryScope }): Promise<Message[]> {
262
+ return [...(this.sessions.get(scope.sessionId) ?? [])];
173
263
  }
174
264
 
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]);
265
+ async append(input: MemoryAppendOptions): Promise<void> {
266
+ const current = this.sessions.get(input.scope.sessionId) ?? [];
267
+ this.sessions.set(input.scope.sessionId, [...current, ...input.messages]);
178
268
  }
179
269
 
180
- async clear(context: MemoryContext): Promise<void> {
181
- this.sessions.delete(context.sessionId);
270
+ async clear({ scope }: { scope: MemoryScope }): Promise<void> {
271
+ this.sessions.delete(scope.sessionId);
182
272
  }
183
273
  }
184
274
 
@@ -189,8 +279,9 @@ const agent = new Agent({
189
279
  memory: { store: memory },
190
280
  });
191
281
 
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();
282
+ const session = { sessionId: "thread_123", userId: "user_456" };
283
+ await agent.generate({ prompt: "Remember my plan.", session });
284
+ await agent.generate({ prompt: "What is my plan?", session });
194
285
  ```
195
286
 
196
287
  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 +294,52 @@ new Agent({
203
294
  });
204
295
  ```
205
296
 
297
+ Without `session`, the same Agent is stateless. Pass `{ messages }` when the caller already owns a
298
+ complete transcript; transcripts cannot be combined with persisted sessions.
299
+
300
+ Compaction is an explicit Agent policy over a store capability. The adapter persists the summary as
301
+ an ordinary system message with `metadata.anvia.memoryCompaction`, so `load()` and inspectors expose
302
+ exactly what future runs receive:
303
+
304
+ ```ts
305
+ import { createSummaryMemoryCompactor } from "@anvia/core/memory";
306
+
307
+ const compactor = createSummaryMemoryCompactor({
308
+ model: summaryModel,
309
+ maxTokens: 1024,
310
+ retries: { maxAttempts: 2 },
311
+ });
312
+
313
+ const agent = new Agent({
314
+ id: "support",
315
+ model,
316
+ memory: {
317
+ store: memory,
318
+ savePolicy: "message",
319
+ compaction: {
320
+ trigger: { afterMessages: 50 },
321
+ retention: { recentUserTurns: 4 },
322
+ compactor,
323
+ conflictRetries: false,
324
+ },
325
+ },
326
+ });
327
+
328
+ for await (const event of agent.stream({ prompt: "What did we decide?", session })) {
329
+ if (event.type === "memory_compaction") {
330
+ console.log(event.compactedMessageCount, event.usage);
331
+ }
332
+ }
333
+ ```
334
+
335
+ The trigger is a threshold, not a hard storage limit. Summary-provider retries belong to the
336
+ compactor; full snapshot-to-replacement conflict retries are separately opt-in.
337
+
206
338
  ## Structured Extraction
207
339
 
208
340
  ```ts
209
- import { ExtractorBuilder } from "@anvia/core/extractor";
341
+ import { extract } from "@anvia/core/extractor";
342
+ import { z } from "zod";
210
343
 
211
344
  const ticketSchema = z.object({
212
345
  customer: z.string(),
@@ -214,45 +347,171 @@ const ticketSchema = z.object({
214
347
  summary: z.string(),
215
348
  });
216
349
 
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
- );
350
+ const { output: ticket } = await extract({
351
+ model,
352
+ text: "Acme Co. reports checkout failures. Priority is high.",
353
+ outputSchema: ticketSchema,
354
+ retries: { maxAttempts: 2 },
355
+ });
222
356
  ```
223
357
 
224
358
  ## Pipelines
225
359
 
226
360
  ```ts
227
- import { PipelineBuilder } from "@anvia/core/pipeline";
361
+ import { Pipeline } from "@anvia/core/pipeline";
228
362
  import { z } from "zod";
229
363
 
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();
364
+ const pipeline = new Pipeline({ id: "support-flow", inputSchema: z.string() })
365
+ .agent({
366
+ id: "draft",
367
+ agent,
368
+ suspension: "reject",
369
+ request: ({ input }) => ({ prompt: `Draft a reply for this ticket:\n\n${input}` }),
370
+ })
371
+ .extract({
372
+ id: "parse",
373
+ model,
374
+ outputSchema: ticketSchema,
375
+ text: ({ input }) => input,
376
+ });
377
+
378
+ const { runId, output } = await pipeline.run({
379
+ input: "Customer cannot complete checkout.",
380
+ });
381
+ ```
382
+
383
+ ## Documents
384
+
385
+ Applications own file discovery, file reads, source metadata, and per-file error policy. Core only
386
+ provides deterministic in-memory chunking and scoped PDF text extraction:
387
+
388
+ ```ts
389
+ import { readFile } from "node:fs/promises";
390
+ import { chunkText, extractPdfText } from "@anvia/core/documents";
391
+
392
+ const text = await readFile("guide.txt", "utf8");
393
+ const chunks = chunkText({
394
+ text,
395
+ strategy: "recursive",
396
+ maxSize: 1_000,
397
+ overlap: 100,
398
+ separators: ["\n\n", "\n", " "],
399
+ });
235
400
 
236
- const result = await pipeline.run("Customer cannot complete checkout.");
401
+ const { pages } = await extractPdfText({
402
+ data: new Uint8Array(await readFile("guide.pdf")),
403
+ abortSignal,
404
+ });
237
405
  ```
238
406
 
407
+ Use `strategy: "fixed"` for deterministic sliding windows. Recursive chunking requires an explicit
408
+ separator order and falls back to fixed-size splitting when none of those separators can divide an
409
+ oversized section. Chunk offsets use JavaScript string indices and always identify the exact source
410
+ slice. PDF pages are one-based, and the parser task is disposed before extraction settles.
411
+ If parsing or abort handling fails together with parser cleanup, extraction rejects with an
412
+ `AggregateError` containing the operation failure first and the cleanup failure second.
413
+
414
+ ## Media
415
+
416
+ Media helpers follow the same one-object API and share `providerOptions`, `retries`, and
417
+ `abortSignal`:
418
+
419
+ ```ts
420
+ import { generateImage, generateSpeech, transcribe } from "@anvia/core";
421
+
422
+ const image = await generateImage({
423
+ model: client.imageGenerationModel({ modelId: "gpt-image-1" }),
424
+ prompt: "A compact robot drawing an architecture diagram.",
425
+ width: 1024,
426
+ height: 1024,
427
+ });
428
+ console.log(image.images[0].data);
429
+
430
+ const speech = await generateSpeech({
431
+ model: client.speechGenerationModel({ modelId: "gpt-4o-mini-tts" }),
432
+ text: "Hello from Anvia.",
433
+ voice: "alloy",
434
+ });
435
+
436
+ const transcript = await transcribe({
437
+ model: client.transcriptionModel({ modelId: "gpt-4o-mini-transcribe" }),
438
+ audio: {
439
+ data: speech.audio.data,
440
+ filename: "speech.mp3",
441
+ mediaType: speech.audio.mediaType,
442
+ },
443
+ });
444
+ console.log(transcript.text);
445
+ ```
446
+
447
+ ## MCP
448
+
449
+ MCP clients own connections. Agents receive immutable server registrations and never own or close
450
+ the underlying transport:
451
+
452
+ ```ts
453
+ import { Agent } from "@anvia/core/agent";
454
+ import { McpClient, McpClientGroup } from "@anvia/core/mcp";
455
+
456
+ const filesystem = new McpClient({
457
+ name: "filesystem",
458
+ transport: {
459
+ type: "stdio",
460
+ command: "npx",
461
+ args: ["-y", "@modelcontextprotocol/server-filesystem", "./workspace"],
462
+ },
463
+ });
464
+ const github = new McpClient({
465
+ name: "github",
466
+ transport: {
467
+ type: "streamableHttp",
468
+ url: "https://mcp.example.com/mcp",
469
+ headers: { authorization: `Bearer ${process.env.MCP_TOKEN}` },
470
+ },
471
+ tools: { prefix: "github_" },
472
+ });
473
+
474
+ const mcp = await McpClientGroup.connect({ clients: [filesystem, github] });
475
+ const agent = new Agent({ id: "assistant", model, mcpServers: mcp.servers });
476
+
477
+ try {
478
+ await agent.generate({ prompt: "Find the issue and update it." });
479
+ } finally {
480
+ await mcp.close();
481
+ }
482
+ ```
483
+
484
+ Construction performs no I/O. `connect()` discovers every tool page once and returns a frozen
485
+ registration snapshot. Reconnect and rebuild the Agent to adopt changed remote tools. Built-in
486
+ Streamable HTTP connections enforce Anvia URL safety by default and do not accept a custom `fetch`.
487
+ Static request headers are explicit transport configuration; arbitrary Fetch `RequestInit` fields
488
+ are not exposed because the MCP transport owns its HTTP method, body, abort signal, session, and
489
+ protocol headers. Configured headers are sent only to the exact MCP endpoint, are not attached to
490
+ OAuth requests, and cause endpoint redirects to fail instead of forwarding credentials. A static
491
+ `authorization` header cannot be combined with `authProvider`.
492
+ For an intentionally local or private-network server, set `ssrfProtection: "disabled"` on that
493
+ transport. This disables hostname and DNS restrictions for the complete transport, including
494
+ redirects and OAuth discovery, while still requiring HTTP(S). Use it only when the application owns
495
+ and trusts that network boundary. MCP server instructions remain inspectable metadata and are not
496
+ added to Agent instructions.
497
+
239
498
  ## Public Areas
240
499
 
241
- - `agent`: agent runtime and the compatibility `AgentBuilder`
500
+ - `agent`: typed Agent runtime, continuations, interactions, retries, and stream events
242
501
  - `tool`: typed tool creation and tool sets
243
- - `completion`: provider-neutral completion request and response types
502
+ - `completion`: direct completion helpers and provider-neutral model contracts
244
503
  - `memory`: durable session memory interfaces and in-memory store
245
504
  - `extractor`: schema-first structured extraction
246
505
  - `pipeline`: typed sequential and parallel workflows
247
506
  - `embeddings`: embedding helpers and document embedding utilities
248
507
  - `vector-store`: in-memory vector search and vector search tools
249
508
  - `streaming`: normalized stream helpers
250
- - `mcp`: MCP server connection helpers
509
+ - `mcp`: lifecycle-owning MCP clients, groups, and immutable Agent registrations
251
510
  - `skills`: local skill loading
252
511
  - `observability`: observer interfaces for runs, generations, and tool calls
253
512
  - `evals`: evaluation helpers and reporters
254
- - `loaders`: document loading utilities
255
- - `audio-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
513
+ - `documents`: in-memory text chunking and PDF text extraction
514
+ - `speech-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
256
515
 
257
516
  ## Development
258
517
 
@@ -1,22 +1,26 @@
1
- export { R as RetryContext, a as RetryOptions } from '../retry-D3Ruy-ba.js';
2
- export { A as Agent, a as AgentContextInput, b as AgentMemoryOptions, c as AgentObserverInput, d as AgentOptions, e as AgentSession, f as AgentToolInput, g as AgentToolOptions, C as ContextIndex, h as CreateContextIndexOptions, i as createContextIndex, j as isContextIndex } from '../agent-DGYY_onS.js';
3
- import { M as Message } from '../types-B3A4YxaA.js';
4
- export { A as AgentApprovalDecision, a as AgentApprovalRequiredEvent, b as AgentApprovalRequiredResult, c as AgentChildStreamEvent, d as AgentChildStreamEventWithToolCallDeltas, e as AgentChildStreamEventWithoutToolCallDeltas, f as AgentDeltaEvent, g as AgentErrorEvent, h as AgentErrorStreamEvent, i as AgentFinishEvent, j as AgentInput, k as AgentLifecycle, l as AgentResponse, m as AgentResult, n as AgentRunOptions, o as AgentStartEvent, p as AgentStepFinishEvent, q as AgentStream, r as AgentStreamEvent, s as AgentStreamEventWithToolCallDeltas, t as AgentStreamEventWithoutToolCallDeltas, u as AgentStreamOptions, v as AgentToolApprovalRequest, w as AgentToolCallDeltaEvent, x as AgentToolFinishEvent, y as AgentToolStartEvent } from '../index-DxB74ybJ.js';
5
- import '../guardrails/index.js';
6
- import '../type-utils-CtHVDRn_.js';
7
- import '../types-BBEGKB9m.js';
8
- import '../middleware-KynLg_RW.js';
9
- import '../tool-C_8GudXn.js';
1
+ export { M as ModelCallOptions } from '../model-call-options-CZkSw_xN.js';
2
+ export { R as RetryContext, a as RetryOptions, b as RetrySetting } from '../retry-CjvSlKGW.js';
3
+ export { A as Agent, a as AgentContextInput, b as AgentMemory, c as AgentMemoryOptions, d as AgentOptions, e as AgentToolInput, f as AgentToolOptions, C as CreateHybridVectorContextOptions, g as CreateVectorContextOptions, V as VectorContext, h as VectorContextBaseOptions, i as createVectorContext, j as isVectorContext } from '../agent-CXScUOpX.js';
4
+ import { M as Message } from '../types-D9rKQ_as.js';
5
+ import { A as AgentBlockedResult, t as AgentSuspendedResult } from '../types-CElwurHZ.js';
6
+ export { a as AgentChildStreamEvent, b as AgentDeltaEvent, c as AgentErrorEvent, d as AgentErrorStreamEvent, e as AgentFinishEvent, f as AgentInput, g as AgentLifecycle, h as AgentMemoryCompactionEvent, i as AgentPrompt, j as AgentResponse, k as AgentResult, l as AgentRunOptions, m as AgentRunSettings, n as AgentStartEvent, o as AgentSteerInput, p as AgentSteerReceipt, q as AgentStepFinishEvent, r as AgentStream, s as AgentStreamEvent, u as AgentToolCallDeltaEvent, v as AgentToolFinishEvent, w as AgentToolStartEvent } from '../types-CElwurHZ.js';
7
+ export { A as AgentContinuation, a as AgentInteractionRequest, b as AgentInteractionResponse, d as AgentQuestionAnswer, e as AgentQuestionChoice, f as AgentQuestionPrompt, g as AgentToolApprovalRequest, h as AgentToolQuestionRequest, p as agentContinuationSchema, q as agentInteractionRequestSchema, r as agentInteractionResponseSchema, w as assertAgentInteractionResponse, t as parseAgentContinuation, u as parseAgentInteractionRequest, v as parseAgentInteractionResponse } from '../interactions-csssHYcN.js';
10
8
  import '../zod-schema-C7F4clpm.js';
11
9
  import 'zod';
12
- import '../types-DulRCYdB.js';
13
- import '@modelcontextprotocol/sdk/client/sse.js';
14
- import '@modelcontextprotocol/sdk/client/stdio.js';
10
+ import '../types-CIPaIbZo.js';
11
+ import '../type-utils-CtHVDRn_.js';
12
+ import '../client-DvUatElp.js';
13
+ import 'node:child_process';
14
+ import 'node:stream';
15
+ import '@modelcontextprotocol/sdk/client/auth.js';
15
16
  import '@modelcontextprotocol/sdk/client/streamableHttp.js';
16
- import '../types-DhQfwiZn.js';
17
- import '../dynamic-tools-i9woysFa.js';
18
- import '../types-p55Jc-sr.js';
19
- import '../types-DT3nEemY.js';
17
+ import '@modelcontextprotocol/sdk/shared/transport.js';
18
+ import '../tool-r62gf3QY.js';
19
+ import '../types-DbolvLtZ.js';
20
+ import '../types-RT3cuyRk.js';
21
+ import '../dynamic-tools-AEBOTgu1.js';
22
+ import '../types-Cr4uiYo5.js';
23
+ import '../types-C3gv9EX6.js';
20
24
 
21
25
  declare class MaxTurnsError extends Error {
22
26
  readonly maxTurns: number;
@@ -27,7 +31,18 @@ declare class MaxTurnsError extends Error {
27
31
  declare class AgentRunCancelledError extends Error {
28
32
  readonly chatHistory: Message[];
29
33
  readonly reason: string;
30
- constructor(chatHistory: Message[], reason: string);
34
+ constructor(chatHistory: Message[], reason: string, options?: ErrorOptions);
35
+ }
36
+ declare class AgentRunBlockedError extends Error {
37
+ readonly result: AgentBlockedResult;
38
+ constructor(result: AgentBlockedResult);
39
+ }
40
+ declare class AgentStreamClosedError extends Error {
41
+ constructor();
42
+ }
43
+ declare class AgentToolSuspensionError extends Error {
44
+ readonly result: AgentSuspendedResult;
45
+ constructor(result: AgentSuspendedResult);
31
46
  }
32
47
 
33
- export { AgentRunCancelledError, MaxTurnsError };
48
+ export { AgentBlockedResult, AgentRunBlockedError, AgentRunCancelledError, AgentStreamClosedError, AgentSuspendedResult, AgentToolSuspensionError, MaxTurnsError };