@anvia/core 0.26.0 → 1.0.0-rc.2

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 +303 -94
  2. package/dist/agent/index.d.ts +36 -70
  3. package/dist/agent/index.js +25 -18
  4. package/dist/agent-yBG8e9Jj.d.ts +153 -0
  5. package/dist/chunk-4WI3VGZK.js +127 -0
  6. package/dist/chunk-4WI3VGZK.js.map +1 -0
  7. package/dist/chunk-77AIYARB.js +654 -0
  8. package/dist/chunk-77AIYARB.js.map +1 -0
  9. package/dist/chunk-AI5JMUW7.js +45 -0
  10. package/dist/chunk-AI5JMUW7.js.map +1 -0
  11. package/dist/chunk-BEYBU7VJ.js +53 -0
  12. package/dist/chunk-BEYBU7VJ.js.map +1 -0
  13. package/dist/chunk-CWM7B2OO.js +315 -0
  14. package/dist/chunk-CWM7B2OO.js.map +1 -0
  15. package/dist/chunk-D37WMSP6.js +131 -0
  16. package/dist/chunk-D37WMSP6.js.map +1 -0
  17. package/dist/chunk-DBSCKTUL.js +3793 -0
  18. package/dist/chunk-DBSCKTUL.js.map +1 -0
  19. package/dist/chunk-EINTGEGY.js +629 -0
  20. package/dist/chunk-EINTGEGY.js.map +1 -0
  21. package/dist/chunk-FMHLROPQ.js +351 -0
  22. package/dist/chunk-FMHLROPQ.js.map +1 -0
  23. package/dist/chunk-FN7HLLAZ.js +211 -0
  24. package/dist/chunk-FN7HLLAZ.js.map +1 -0
  25. package/dist/chunk-GKDERULJ.js +50 -0
  26. package/dist/chunk-GKDERULJ.js.map +1 -0
  27. package/dist/{chunk-CWUJUSOS.js → chunk-KIDGIJM7.js} +26 -5
  28. package/dist/chunk-KIDGIJM7.js.map +1 -0
  29. package/dist/chunk-KR3RCCHJ.js +500 -0
  30. package/dist/chunk-KR3RCCHJ.js.map +1 -0
  31. package/dist/chunk-MGNTXGTX.js +37 -0
  32. package/dist/chunk-MGNTXGTX.js.map +1 -0
  33. package/dist/{chunk-MRQLEK2B.js → chunk-OJH6HWHX.js} +63 -51
  34. package/dist/chunk-OJH6HWHX.js.map +1 -0
  35. package/dist/chunk-QOUPWOGW.js +29 -0
  36. package/dist/chunk-QOUPWOGW.js.map +1 -0
  37. package/dist/chunk-TIZYTPJO.js +208 -0
  38. package/dist/chunk-TIZYTPJO.js.map +1 -0
  39. package/dist/{chunk-NCPN4SJR.js → chunk-XKYYBABH.js} +13 -13
  40. package/dist/chunk-XKYYBABH.js.map +1 -0
  41. package/dist/client-B8DT1vUG.d.ts +83 -0
  42. package/dist/completion/index.d.ts +6 -36
  43. package/dist/completion/index.js +25 -29
  44. package/dist/dynamic-tools-KwEGRbHp.d.ts +45 -0
  45. package/dist/embeddings/index.d.ts +11 -9
  46. package/dist/embeddings/index.js +2 -4
  47. package/dist/evals/index.d.ts +47 -20
  48. package/dist/evals/index.js +104 -65
  49. package/dist/evals/index.js.map +1 -1
  50. package/dist/extractor/index.d.ts +27 -42
  51. package/dist/extractor/index.js +7 -22
  52. package/dist/guardrails/index.d.ts +3 -2
  53. package/dist/guardrails/index.js +1 -1
  54. package/dist/image-generation/index.d.ts +23 -23
  55. package/dist/image-generation/index.js +5 -39
  56. package/dist/image-generation/index.js.map +1 -1
  57. package/dist/index.d.ts +25 -20
  58. package/dist/index.js +60 -63
  59. package/dist/internal/agent.d.ts +163 -9
  60. package/dist/internal/agent.js +25 -18
  61. package/dist/loaders/index.d.ts +6 -10
  62. package/dist/loaders/index.js.map +1 -1
  63. package/dist/mcp/index.d.ts +19 -13
  64. package/dist/mcp/index.js +13 -348
  65. package/dist/mcp/index.js.map +1 -1
  66. package/dist/memory/index.d.ts +20 -6
  67. package/dist/memory/index.js +9 -10
  68. package/dist/message-schema-Cn7ezRfJ.d.ts +52 -0
  69. package/dist/middleware-C7PdmKF7.d.ts +56 -0
  70. package/dist/model-call-options-CZkSw_xN.d.ts +6 -0
  71. package/dist/model-listing/index.d.ts +3 -1
  72. package/dist/observability/index.d.ts +20 -3
  73. package/dist/observability/index.js +8 -5
  74. package/dist/observability/index.js.map +1 -1
  75. package/dist/pipeline/index.d.ts +114 -72
  76. package/dist/pipeline/index.js +478 -286
  77. package/dist/pipeline/index.js.map +1 -1
  78. package/dist/retry-CjvSlKGW.d.ts +16 -0
  79. package/dist/skills/index.d.ts +5 -4
  80. package/dist/skills/index.js +7 -8
  81. package/dist/speech-generation/index.d.ts +37 -0
  82. package/dist/speech-generation/index.js +8 -0
  83. package/dist/streaming/index.js +37 -3
  84. package/dist/streaming/index.js.map +1 -1
  85. package/dist/{think-tool-DfCMuO8e.d.ts → think-tool-6H2QNQzQ.d.ts} +5 -5
  86. package/dist/tool/index.d.ts +10 -6
  87. package/dist/tool/index.js +15 -21
  88. package/dist/tool--Mz4v1eL.d.ts +57 -0
  89. package/dist/transcription/index.d.ts +29 -22
  90. package/dist/transcription/index.js +5 -57
  91. package/dist/transcription/index.js.map +1 -1
  92. package/dist/type-utils-CtHVDRn_.d.ts +6 -0
  93. package/dist/types-3x3mOLHU.d.ts +379 -0
  94. package/dist/types-BVzM4RsC.d.ts +155 -0
  95. package/dist/types-CQ_ioXWK.d.ts +319 -0
  96. package/dist/types-Cr4uiYo5.d.ts +99 -0
  97. package/dist/{types-04LqWrmy.d.ts → types-DiJqJejp.d.ts} +1 -1
  98. package/dist/types-Dld1TpWj.d.ts +130 -0
  99. package/dist/vector-store/index.d.ts +33 -34
  100. package/dist/vector-store/index.js +6 -6
  101. package/package.json +6 -18
  102. package/dist/agent-x3YTyv5i.d.ts +0 -222
  103. package/dist/audio-generation/index.d.ts +0 -33
  104. package/dist/audio-generation/index.js +0 -42
  105. package/dist/audio-generation/index.js.map +0 -1
  106. package/dist/chunk-2ODTMRHP.js +0 -54
  107. package/dist/chunk-2ODTMRHP.js.map +0 -1
  108. package/dist/chunk-ADH7NNCS.js +0 -512
  109. package/dist/chunk-ADH7NNCS.js.map +0 -1
  110. package/dist/chunk-BALBBVI3.js +0 -2732
  111. package/dist/chunk-BALBBVI3.js.map +0 -1
  112. package/dist/chunk-CWUJUSOS.js.map +0 -1
  113. package/dist/chunk-DALYP4PX.js +0 -225
  114. package/dist/chunk-DALYP4PX.js.map +0 -1
  115. package/dist/chunk-HYUYZEAJ.js +0 -223
  116. package/dist/chunk-HYUYZEAJ.js.map +0 -1
  117. package/dist/chunk-I6XOQ4LK.js +0 -466
  118. package/dist/chunk-I6XOQ4LK.js.map +0 -1
  119. package/dist/chunk-JI5YZWNT.js +0 -220
  120. package/dist/chunk-JI5YZWNT.js.map +0 -1
  121. package/dist/chunk-MC3CCKEB.js +0 -221
  122. package/dist/chunk-MC3CCKEB.js.map +0 -1
  123. package/dist/chunk-MELYDKWQ.js +0 -337
  124. package/dist/chunk-MELYDKWQ.js.map +0 -1
  125. package/dist/chunk-MNWK2USR.js +0 -243
  126. package/dist/chunk-MNWK2USR.js.map +0 -1
  127. package/dist/chunk-MRQLEK2B.js.map +0 -1
  128. package/dist/chunk-NCPN4SJR.js.map +0 -1
  129. package/dist/chunk-OIMLU4SF.js +0 -20
  130. package/dist/chunk-OIMLU4SF.js.map +0 -1
  131. package/dist/chunk-Q25IWLBT.js +0 -138
  132. package/dist/chunk-Q25IWLBT.js.map +0 -1
  133. package/dist/chunk-UQX6EXWG.js +0 -33
  134. package/dist/chunk-UQX6EXWG.js.map +0 -1
  135. package/dist/chunk-WQKHFADH.js +0 -12
  136. package/dist/chunk-WQKHFADH.js.map +0 -1
  137. package/dist/chunk-XUUY2L2D.js +0 -42
  138. package/dist/chunk-XUUY2L2D.js.map +0 -1
  139. package/dist/chunk-XWUC7CIT.js +0 -1
  140. package/dist/chunk-XWUC7CIT.js.map +0 -1
  141. package/dist/chunk-YLMDBQO4.js +0 -141
  142. package/dist/chunk-YLMDBQO4.js.map +0 -1
  143. package/dist/errors-2moLDuuN.d.ts +0 -17
  144. package/dist/hooks/index.d.ts +0 -12
  145. package/dist/hooks/index.js +0 -17
  146. package/dist/index-BxSbmOMr.d.ts +0 -257
  147. package/dist/json-BVPssigK.d.ts +0 -39
  148. package/dist/middleware-Dp3d_pxi.d.ts +0 -90
  149. package/dist/request/index.d.ts +0 -28
  150. package/dist/request/index.js +0 -25
  151. package/dist/request/index.js.map +0 -1
  152. package/dist/tool-Cohdv5GC.d.ts +0 -65
  153. package/dist/types-BCTRUGex.d.ts +0 -47
  154. package/dist/types-BrLbKnEz.d.ts +0 -119
  155. package/dist/types-D9JpeAPF.d.ts +0 -114
  156. package/dist/types-G12i56s2.d.ts +0 -354
  157. package/dist/types-JJCxRnaZ.d.ts +0 -70
  158. package/dist/types-O-abTIin.d.ts +0 -79
  159. package/dist/ui/index.d.ts +0 -107
  160. package/dist/ui/index.js +0 -10
  161. package/dist/ui/index.js.map +0 -1
  162. /package/dist/{hooks → speech-generation}/index.js.map +0 -0
package/README.md CHANGED
@@ -26,177 +26,299 @@ pnpm --filter @anvia/core build
26
26
 
27
27
  ```ts
28
28
  import { z } from "zod";
29
- import { AgentBuilder, createTool } from "@anvia/core";
29
+ import { Agent, createTool } from "@anvia/core";
30
30
  import { OpenAIClient } from "@anvia/openai";
31
31
 
32
32
  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",
40
40
  description: "Look up an order by id.",
41
- input: z.object({ orderId: z.string() }),
41
+ inputSchema: z.object({ orderId: z.string() }),
42
42
  execute: async ({ orderId }) => ({ orderId, status: "processing" }),
43
43
  });
44
44
 
45
- const agent = new AgentBuilder("support", model)
46
- .instructions("Help customers with order questions.")
47
- .tool(lookupOrder)
48
- .defaultMaxTurns(4)
49
- .build();
50
-
51
- const response = await agent.prompt("What is happening with order A123?").send();
45
+ const agent = new Agent({
46
+ id: "support",
47
+ model,
48
+ instructions: "Help customers with order questions.",
49
+ maxTurns: 4,
50
+ tools: [lookupOrder],
51
+ });
52
52
 
53
- 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);
54
55
  ```
55
56
 
56
57
  ## Direct Completions
57
58
 
58
- Use `createCompletion` when you want a single provider call without agent turns, memory, or
59
- 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:
60
61
 
61
62
  ```ts
62
- import { createCompletion } from "@anvia/core";
63
+ import { generateCompletion } from "@anvia/core";
63
64
  import { OpenAIClient } from "@anvia/openai";
64
65
 
65
- const model = new OpenAIClient({ apiKey }).completionModel("gpt-5");
66
+ const model = new OpenAIClient({ apiKey }).completionModel({ modelId: "gpt-5", api: "responses" });
66
67
 
67
- const result = await createCompletion(model, {
68
- input: "Summarize Anvia in one sentence.",
68
+ const result = await generateCompletion({
69
+ model,
70
+ prompt: "Summarize Anvia in one sentence.",
69
71
  instructions: "Answer clearly and concisely.",
70
72
  });
71
73
 
72
- console.log(result.text);
74
+ console.log(result.output); // string
73
75
  ```
74
76
 
75
- Use `messages` when you already own the transcript. If `input` is also provided, it is appended as
76
- the final message:
77
+ Use `messages` when the application already owns the transcript. Exactly one of `prompt` or
78
+ `messages` is required:
77
79
 
78
80
  ```ts
79
- import { Message, createCompletion } from "@anvia/core";
81
+ import { generateCompletion, type Message } from "@anvia/core";
80
82
 
81
- const result = await createCompletion(model, {
83
+ const result = await generateCompletion({
84
+ model,
82
85
  messages: [
83
- Message.system("You are concise."),
84
- Message.user("Explain Anvia."),
85
- ],
86
+ { role: "system", content: "You are concise." },
87
+ { role: "user", content: "Explain Anvia." },
88
+ ] satisfies readonly Message[],
86
89
  maxTokens: 300,
87
- params: {
90
+ providerOptions: {
88
91
  reasoning: { effort: "low" },
89
92
  },
90
93
  });
91
94
  ```
92
95
 
93
- 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:
94
104
 
95
105
  ```ts
96
- import { createCompletionStream } from "@anvia/core";
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:
97
125
 
98
- for await (const event of createCompletionStream(model, {
99
- input: "Write a short launch note.",
126
+ ```ts
127
+ import { streamCompletion } from "@anvia/core";
128
+
129
+ for await (const event of streamCompletion({
130
+ model,
131
+ prompt: "Write a short launch note.",
100
132
  })) {
101
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);
102
136
  }
103
137
  ```
104
138
 
105
- React hooks keep `UIMessage[]` state locally, but send core `Message[]` in their request body. Pass
106
- 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 }`.
107
143
 
108
- ```ts
109
- import { createCompletionStream } from "@anvia/core";
110
- import type { UIStreamRequest } from "@anvia/core/ui";
144
+ Client requests carry core `Message[]`, so an endpoint can validate and pass them directly:
111
145
 
112
- 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";
113
150
 
114
- const events = createCompletionStream(model, {
115
- messages: body.messages,
151
+ const body = parseClientStreamRequest(await request.json());
152
+ const events = completionToClientStream({
153
+ events: streamCompletion({ model, messages: body.messages }),
116
154
  });
155
+ return createClientStreamResponse({ events });
117
156
  ```
118
157
 
119
- 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`.
120
160
 
121
- ```ts
122
- import { createParsedCompletion } from "@anvia/core";
123
- import { z } from "zod";
161
+ ## Retries, Provider Options, and Cancellation
124
162
 
125
- const eventSchema = z.object({
126
- name: z.string(),
127
- date: z.string(),
128
- });
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.
129
166
 
130
- const event = await createParsedCompletion(model, {
131
- schema: eventSchema,
132
- input: "Alice and Bob are going to a science fair on Friday.",
167
+ ```ts
168
+ const controller = new AbortController();
169
+
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" } },
133
176
  });
134
-
135
- console.log(event.data);
136
177
  ```
137
178
 
138
- ## Prompts and Memory
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.
139
182
 
140
- Use a plain prompt for stateless calls:
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.
141
188
 
142
189
  ```ts
143
- await agent.prompt("Summarize this ticket.").send();
190
+ const agent = new Agent({
191
+ id: "support",
192
+ model,
193
+ retries: { maxAttempts: 3 },
194
+ });
195
+
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
+ });
144
202
  ```
145
203
 
146
- Use a message array when you already own the transcript. The last message is the active prompt and earlier messages are request history:
204
+ Agent results are discriminated by `status`. Completed results include typed `output` and `text`;
205
+ guardrail blocks return `status: "blocked"`, `stage`, and `text`; tool approval can return
206
+ `status: "approval_required"`.
147
207
 
148
208
  ```ts
149
- import { Message } from "@anvia/core";
209
+ const result = await agent.generate({ prompt: "Help with this request." });
150
210
 
151
- await agent
152
- .prompt([
153
- Message.user("My project is named Anvia."),
154
- Message.assistant("Noted."),
155
- Message.user("What is my project named?"),
156
- ])
157
- .send();
211
+ if (result.status === "completed") console.log(result.output);
212
+ if (result.status === "blocked") console.log(result.stage, result.text);
158
213
  ```
159
214
 
160
- Configure durable conversation memory on the agent, then run through a session:
215
+ An Agent with `outputSchema` carries that output type through `generate`, `stream`, `asTool`, and
216
+ Pipeline Agent stages. Agent stream finals use the same result shape:
161
217
 
162
218
  ```ts
163
- import { AgentBuilder, type MemoryStore, type Message } from "@anvia/core";
164
- import type { MemoryAppendInput, MemoryContext } from "@anvia/core/memory";
219
+ for await (const event of agent.stream({ prompt: "Help with this request." })) {
220
+ if (event.type === "final") {
221
+ if (event.result.status === "completed") console.log(event.result.output);
222
+ else console.log(event.result.stage, event.result.text);
223
+ }
224
+ }
225
+ ```
226
+
227
+ Pass `abortSignal` on a run to cancel the active provider call, tools, and nested Agent tools.
228
+
229
+ ## Memory
230
+
231
+ Configure durable conversation memory on the Agent, then run through a session:
232
+
233
+ ```ts
234
+ import { Agent, type MemoryStore, type Message } from "@anvia/core";
235
+ import type { MemoryAppendOptions, MemoryScope } from "@anvia/core/memory";
165
236
 
166
237
  class AppMemoryStore implements MemoryStore {
167
238
  private readonly sessions = new Map<string, Message[]>();
168
239
 
169
- async load(context: MemoryContext): Promise<Message[]> {
170
- return [...(this.sessions.get(context.sessionId) ?? [])];
240
+ async load({ scope }: { scope: MemoryScope }): Promise<Message[]> {
241
+ return [...(this.sessions.get(scope.sessionId) ?? [])];
171
242
  }
172
243
 
173
- async append(input: MemoryAppendInput): Promise<void> {
174
- const current = this.sessions.get(input.context.sessionId) ?? [];
175
- this.sessions.set(input.context.sessionId, [...current, ...input.messages]);
244
+ async append(input: MemoryAppendOptions): Promise<void> {
245
+ const current = this.sessions.get(input.scope.sessionId) ?? [];
246
+ this.sessions.set(input.scope.sessionId, [...current, ...input.messages]);
176
247
  }
177
248
 
178
- async clear(context: MemoryContext): Promise<void> {
179
- this.sessions.delete(context.sessionId);
249
+ async clear({ scope }: { scope: MemoryScope }): Promise<void> {
250
+ this.sessions.delete(scope.sessionId);
180
251
  }
181
252
  }
182
253
 
183
254
  const memory = new AppMemoryStore();
184
- const agent = new AgentBuilder("support", model).memory(memory).build();
255
+ const agent = new Agent({
256
+ id: "support",
257
+ model: model,
258
+ memory: { store: memory },
259
+ });
185
260
 
186
- await agent.session("thread_123", { userId: "user_456" }).prompt("Remember my plan.").send();
187
- await agent.session("thread_123", { userId: "user_456" }).prompt("What is my plan?").send();
261
+ const session = { sessionId: "thread_123", userId: "user_456" };
262
+ await agent.generate({ prompt: "Remember my plan.", session });
263
+ await agent.generate({ prompt: "What is my plan?", session });
188
264
  ```
189
265
 
190
266
  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:
191
267
 
192
268
  ```ts
193
- new AgentBuilder("support", model).memory(memory, { savePolicy: "turn" });
269
+ new Agent({
270
+ id: "support",
271
+ model,
272
+ memory: { store: memory, savePolicy: "turn" },
273
+ });
194
274
  ```
195
275
 
276
+ Without `session`, the same Agent is stateless. Pass `{ messages }` when the caller already owns a
277
+ complete transcript; transcripts cannot be combined with persisted sessions.
278
+
279
+ Compaction is an explicit Agent policy over a store capability. The adapter persists the summary as
280
+ an ordinary system message with `metadata.anvia.memoryCompaction`, so `load()` and inspectors expose
281
+ exactly what future runs receive:
282
+
283
+ ```ts
284
+ import { createSummaryMemoryCompactor } from "@anvia/core/memory";
285
+
286
+ const compactor = createSummaryMemoryCompactor({
287
+ model: summaryModel,
288
+ maxTokens: 1024,
289
+ retries: { maxAttempts: 2 },
290
+ });
291
+
292
+ const agent = new Agent({
293
+ id: "support",
294
+ model,
295
+ memory: {
296
+ store: memory,
297
+ savePolicy: "message",
298
+ compaction: {
299
+ trigger: { afterMessages: 50 },
300
+ retention: { recentUserTurns: 4 },
301
+ compactor,
302
+ conflictRetries: false,
303
+ },
304
+ },
305
+ });
306
+
307
+ for await (const event of agent.stream({ prompt: "What did we decide?", session })) {
308
+ if (event.type === "memory_compaction") {
309
+ console.log(event.compactedMessageCount, event.usage);
310
+ }
311
+ }
312
+ ```
313
+
314
+ The trigger is a threshold, not a hard storage limit. Summary-provider retries belong to the
315
+ compactor; full snapshot-to-replacement conflict retries are separately opt-in.
316
+
196
317
  ## Structured Extraction
197
318
 
198
319
  ```ts
199
- import { ExtractorBuilder } from "@anvia/core/extractor";
320
+ import { extract } from "@anvia/core/extractor";
321
+ import { z } from "zod";
200
322
 
201
323
  const ticketSchema = z.object({
202
324
  customer: z.string(),
@@ -204,45 +326,132 @@ const ticketSchema = z.object({
204
326
  summary: z.string(),
205
327
  });
206
328
 
207
- const extractor = new ExtractorBuilder(model, ticketSchema).retries(1).build();
208
-
209
- const ticket = await extractor.extract(
210
- "Acme Co. reports checkout failures. Priority is high.",
211
- );
329
+ const { output: ticket } = await extract({
330
+ model,
331
+ text: "Acme Co. reports checkout failures. Priority is high.",
332
+ outputSchema: ticketSchema,
333
+ retries: { maxAttempts: 2 },
334
+ });
212
335
  ```
213
336
 
214
337
  ## Pipelines
215
338
 
216
339
  ```ts
217
- import { PipelineBuilder } from "@anvia/core/pipeline";
340
+ import { Pipeline } from "@anvia/core/pipeline";
218
341
  import { z } from "zod";
219
342
 
220
- const pipeline = new PipelineBuilder(z.string())
221
- .step((input) => `Extract this support ticket:\n\n${input}`)
222
- .prompt(agent)
223
- .extract(extractor)
224
- .build();
343
+ const pipeline = new Pipeline({ id: "support-flow", inputSchema: z.string() })
344
+ .agent({
345
+ id: "draft",
346
+ agent,
347
+ approval: "reject",
348
+ request: ({ input }) => ({ prompt: `Draft a reply for this ticket:\n\n${input}` }),
349
+ })
350
+ .extract({
351
+ id: "parse",
352
+ model,
353
+ outputSchema: ticketSchema,
354
+ text: ({ input }) => input,
355
+ });
356
+
357
+ const { runId, output } = await pipeline.run({
358
+ input: "Customer cannot complete checkout.",
359
+ });
360
+ ```
361
+
362
+ ## Media
225
363
 
226
- const result = await pipeline.run("Customer cannot complete checkout.");
364
+ Media helpers follow the same one-object API and share `providerOptions`, `retries`, and
365
+ `abortSignal`:
366
+
367
+ ```ts
368
+ import { generateImage, generateSpeech, transcribe } from "@anvia/core";
369
+
370
+ const image = await generateImage({
371
+ model: client.imageGenerationModel({ modelId: "gpt-image-1" }),
372
+ prompt: "A compact robot drawing an architecture diagram.",
373
+ width: 1024,
374
+ height: 1024,
375
+ });
376
+ console.log(image.images[0].data);
377
+
378
+ const speech = await generateSpeech({
379
+ model: client.speechGenerationModel({ modelId: "gpt-4o-mini-tts" }),
380
+ text: "Hello from Anvia.",
381
+ voice: "alloy",
382
+ });
383
+
384
+ const transcript = await transcribe({
385
+ model: client.transcriptionModel({ modelId: "gpt-4o-mini-transcribe" }),
386
+ audio: {
387
+ data: speech.audio.data,
388
+ filename: "speech.mp3",
389
+ mediaType: speech.audio.mediaType,
390
+ },
391
+ });
392
+ console.log(transcript.text);
393
+ ```
394
+
395
+ ## MCP
396
+
397
+ MCP clients own connections. Agents receive immutable server registrations and never own or close
398
+ the underlying transport:
399
+
400
+ ```ts
401
+ import { Agent } from "@anvia/core/agent";
402
+ import { McpClient, McpClientGroup } from "@anvia/core/mcp";
403
+
404
+ const filesystem = new McpClient({
405
+ name: "filesystem",
406
+ transport: {
407
+ type: "stdio",
408
+ command: "npx",
409
+ args: ["-y", "@modelcontextprotocol/server-filesystem", "./workspace"],
410
+ },
411
+ });
412
+ const github = new McpClient({
413
+ name: "github",
414
+ transport: {
415
+ type: "streamableHttp",
416
+ url: "https://mcp.example.com/mcp",
417
+ requestInit: { headers: { authorization: `Bearer ${process.env.MCP_TOKEN}` } },
418
+ },
419
+ tools: { prefix: "github_" },
420
+ });
421
+
422
+ const mcp = await McpClientGroup.connect({ clients: [filesystem, github] });
423
+ const agent = new Agent({ id: "assistant", model, mcpServers: mcp.servers });
424
+
425
+ try {
426
+ await agent.generate({ prompt: "Find the issue and update it." });
427
+ } finally {
428
+ await mcp.close();
429
+ }
227
430
  ```
228
431
 
432
+ Construction performs no I/O. `connect()` discovers every tool page once and returns a frozen
433
+ registration snapshot. Reconnect and rebuild the Agent to adopt changed remote tools. Built-in
434
+ Streamable HTTP connections enforce Anvia URL safety and do not accept a custom `fetch`; use the
435
+ explicit caller-owned `custom` transport only when you intentionally own that security boundary.
436
+ MCP server instructions remain inspectable metadata and are not added to Agent instructions.
437
+
229
438
  ## Public Areas
230
439
 
231
- - `agent`: agent runtime and `AgentBuilder`
440
+ - `agent`: typed Agent runtime, run results, approvals, retries, and stream events
232
441
  - `tool`: typed tool creation and tool sets
233
- - `completion`: provider-neutral completion request and response types
442
+ - `completion`: direct completion helpers and provider-neutral model contracts
234
443
  - `memory`: durable session memory interfaces and in-memory store
235
444
  - `extractor`: schema-first structured extraction
236
445
  - `pipeline`: typed sequential and parallel workflows
237
446
  - `embeddings`: embedding helpers and document embedding utilities
238
447
  - `vector-store`: in-memory vector search and vector search tools
239
448
  - `streaming`: normalized stream helpers
240
- - `mcp`: MCP server connection helpers
449
+ - `mcp`: lifecycle-owning MCP clients, groups, and immutable Agent registrations
241
450
  - `skills`: local skill loading
242
451
  - `observability`: observer interfaces for runs, generations, and tool calls
243
452
  - `evals`: evaluation helpers and reporters
244
453
  - `loaders`: document loading utilities
245
- - `audio-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
454
+ - `speech-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
246
455
 
247
456
  ## Development
248
457
 
@@ -1,75 +1,41 @@
1
- import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-x3YTyv5i.js';
2
- export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-x3YTyv5i.js';
3
- import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
4
- import { C as CompletionModel, P as ProviderTool, l as JsonValue, L as ToolChoice } from '../types-G12i56s2.js';
5
- import { GuardrailPolicyInput } from '../guardrails/index.js';
6
- import { P as PromptHook } from '../types-D9JpeAPF.js';
7
- import { a as McpServer } from '../types-JJCxRnaZ.js';
8
- import { m as MemoryStore, n as MemoryOptions } from '../types-BrLbKnEz.js';
9
- import { i as AgentObserver, O as ObserveOptions } from '../index-BxSbmOMr.js';
10
- import { a as SkillSet } from '../types-04LqWrmy.js';
11
- import { i as ToolSearchDocument, j as ToolSet, A as AgentMiddleware } from '../middleware-Dp3d_pxi.js';
12
- import { A as AnyTool, e as ToolApprovalsOptions } from '../tool-Cohdv5GC.js';
13
- import { a as VectorSearchIndex } from '../types-O-abTIin.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-yBG8e9Jj.js';
4
+ import { M as Message } from '../types-CQ_ioXWK.js';
5
+ import { c as AgentBlockedResult } from '../types-3x3mOLHU.js';
6
+ export { A as AgentApprovalDecision, a as AgentApprovalRequiredEvent, b as AgentApprovalRequiredResult, d as AgentChildStreamEvent, e as AgentDeltaEvent, f as AgentErrorEvent, g as AgentErrorStreamEvent, h as AgentFinishEvent, i as AgentInput, j as AgentLifecycle, k as AgentMemoryCompactionEvent, l as AgentPrompt, m as AgentResponse, n as AgentResult, o as AgentRunOptions, p as AgentRunSettings, q as AgentStartEvent, r as AgentSteerInput, s as AgentStepFinishEvent, t as AgentStream, u as AgentStreamEvent, v as AgentToolApprovalRequest, w as AgentToolCallDeltaEvent, x as AgentToolFinishEvent, y as AgentToolStartEvent } from '../types-3x3mOLHU.js';
7
+ import '../zod-schema-C7F4clpm.js';
14
8
  import 'zod';
15
- import '@modelcontextprotocol/sdk/client/sse.js';
16
- import '@modelcontextprotocol/sdk/client/stdio.js';
9
+ import '../guardrails/index.js';
10
+ import '../type-utils-CtHVDRn_.js';
11
+ import '../client-B8DT1vUG.js';
12
+ import 'node:child_process';
13
+ import 'node:stream';
14
+ import '@modelcontextprotocol/sdk/client/auth.js';
17
15
  import '@modelcontextprotocol/sdk/client/streamableHttp.js';
18
- import '../types-BCTRUGex.js';
16
+ import '@modelcontextprotocol/sdk/shared/transport.js';
17
+ import '../tool--Mz4v1eL.js';
18
+ import '../middleware-C7PdmKF7.js';
19
+ import '../types-BVzM4RsC.js';
20
+ import '../types-DiJqJejp.js';
21
+ import '../dynamic-tools-KwEGRbHp.js';
22
+ import '../types-Cr4uiYo5.js';
23
+ import '../types-Dld1TpWj.js';
19
24
 
20
- declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
21
- private readonly completionModel;
22
- private readonly agentId;
23
- private agentName;
24
- private agentDescription;
25
- private instructionBlocks;
26
- private contextDocs;
27
- private temp;
28
- private maxTokenCount;
29
- private params;
30
- private choice;
31
- private turns;
32
- private requestHook;
33
- private schema;
34
- private approvalOptions;
35
- private guardrailPolicies;
36
- private skillInstructionBlocks;
37
- private observerRegistrations;
38
- private dynamicContextRegistrations;
39
- private dynamicToolRegistrations;
40
- private middlewareRegistrations;
41
- private memoryRegistration;
42
- private eventStoreRegistration;
43
- private activeToolSet;
44
- private providerToolDefs;
45
- constructor(agentId: string, completionModel: M);
46
- name(name: string): this;
47
- description(description: string): this;
48
- instructions(instructions: string): this;
49
- context(text: string, id?: string): this;
50
- dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this;
51
- dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this;
52
- tool(tool: AnyTool | ProviderTool): this;
53
- tools(tools: Array<AnyTool | ProviderTool>): this;
54
- mcp(servers: McpServer[]): this;
55
- skills(skillSet: SkillSet): this;
56
- useToolSet(toolSet: ToolSet): this;
57
- temperature(temperature: number): this;
58
- maxTokens(maxTokens: number): this;
59
- additionalParams(params: JsonValue): this;
60
- toolChoice(toolChoice: ToolChoice): this;
61
- defaultMaxTurns(defaultMaxTurns: number): this;
62
- hook(hook: PromptHook): this;
63
- middleware(middleware: AgentMiddleware): this;
64
- middlewares(middlewares: AgentMiddleware[]): this;
65
- observe(observer: AgentObserver, options?: ObserveOptions): this;
66
- approvals(options: ToolApprovalsOptions): this;
67
- guardrails(policies: GuardrailPolicyInput): this;
68
- memory(store: MemoryStore, options?: MemoryOptions): this;
69
- eventStore(store: AgentEventStore, options?: AgentEventStoreOptions): this;
70
- outputSchema(schema: ZodSchema): this;
71
- build(): Agent<M>;
72
- private buildInstructions;
25
+ declare class MaxTurnsError extends Error {
26
+ readonly maxTurns: number;
27
+ readonly chatHistory: Message[];
28
+ readonly prompt: Message;
29
+ constructor(maxTurns: number, chatHistory: Message[], prompt: Message);
30
+ }
31
+ declare class AgentRunCancelledError extends Error {
32
+ readonly chatHistory: Message[];
33
+ readonly 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);
73
39
  }
74
40
 
75
- export { Agent, AgentBuilder, AgentEventStore, AgentEventStoreOptions, DynamicContextOptions, DynamicToolOptions };
41
+ export { AgentBlockedResult, AgentRunBlockedError, AgentRunCancelledError, MaxTurnsError };