@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.
- package/README.md +303 -94
- package/dist/agent/index.d.ts +36 -70
- package/dist/agent/index.js +25 -18
- package/dist/agent-yBG8e9Jj.d.ts +153 -0
- package/dist/chunk-4WI3VGZK.js +127 -0
- package/dist/chunk-4WI3VGZK.js.map +1 -0
- package/dist/chunk-77AIYARB.js +654 -0
- package/dist/chunk-77AIYARB.js.map +1 -0
- package/dist/chunk-AI5JMUW7.js +45 -0
- package/dist/chunk-AI5JMUW7.js.map +1 -0
- package/dist/chunk-BEYBU7VJ.js +53 -0
- package/dist/chunk-BEYBU7VJ.js.map +1 -0
- package/dist/chunk-CWM7B2OO.js +315 -0
- package/dist/chunk-CWM7B2OO.js.map +1 -0
- package/dist/chunk-D37WMSP6.js +131 -0
- package/dist/chunk-D37WMSP6.js.map +1 -0
- package/dist/chunk-DBSCKTUL.js +3793 -0
- package/dist/chunk-DBSCKTUL.js.map +1 -0
- package/dist/chunk-EINTGEGY.js +629 -0
- package/dist/chunk-EINTGEGY.js.map +1 -0
- package/dist/chunk-FMHLROPQ.js +351 -0
- package/dist/chunk-FMHLROPQ.js.map +1 -0
- package/dist/chunk-FN7HLLAZ.js +211 -0
- package/dist/chunk-FN7HLLAZ.js.map +1 -0
- package/dist/chunk-GKDERULJ.js +50 -0
- package/dist/chunk-GKDERULJ.js.map +1 -0
- package/dist/{chunk-CWUJUSOS.js → chunk-KIDGIJM7.js} +26 -5
- package/dist/chunk-KIDGIJM7.js.map +1 -0
- package/dist/chunk-KR3RCCHJ.js +500 -0
- package/dist/chunk-KR3RCCHJ.js.map +1 -0
- package/dist/chunk-MGNTXGTX.js +37 -0
- package/dist/chunk-MGNTXGTX.js.map +1 -0
- package/dist/{chunk-MRQLEK2B.js → chunk-OJH6HWHX.js} +63 -51
- package/dist/chunk-OJH6HWHX.js.map +1 -0
- package/dist/chunk-QOUPWOGW.js +29 -0
- package/dist/chunk-QOUPWOGW.js.map +1 -0
- package/dist/chunk-TIZYTPJO.js +208 -0
- package/dist/chunk-TIZYTPJO.js.map +1 -0
- package/dist/{chunk-NCPN4SJR.js → chunk-XKYYBABH.js} +13 -13
- package/dist/chunk-XKYYBABH.js.map +1 -0
- package/dist/client-B8DT1vUG.d.ts +83 -0
- package/dist/completion/index.d.ts +6 -36
- package/dist/completion/index.js +25 -29
- package/dist/dynamic-tools-KwEGRbHp.d.ts +45 -0
- package/dist/embeddings/index.d.ts +11 -9
- package/dist/embeddings/index.js +2 -4
- package/dist/evals/index.d.ts +47 -20
- package/dist/evals/index.js +104 -65
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +27 -42
- package/dist/extractor/index.js +7 -22
- package/dist/guardrails/index.d.ts +3 -2
- package/dist/guardrails/index.js +1 -1
- package/dist/image-generation/index.d.ts +23 -23
- package/dist/image-generation/index.js +5 -39
- package/dist/image-generation/index.js.map +1 -1
- package/dist/index.d.ts +25 -20
- package/dist/index.js +60 -63
- package/dist/internal/agent.d.ts +163 -9
- package/dist/internal/agent.js +25 -18
- package/dist/loaders/index.d.ts +6 -10
- package/dist/loaders/index.js.map +1 -1
- package/dist/mcp/index.d.ts +19 -13
- package/dist/mcp/index.js +13 -348
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory/index.d.ts +20 -6
- package/dist/memory/index.js +9 -10
- package/dist/message-schema-Cn7ezRfJ.d.ts +52 -0
- package/dist/middleware-C7PdmKF7.d.ts +56 -0
- package/dist/model-call-options-CZkSw_xN.d.ts +6 -0
- package/dist/model-listing/index.d.ts +3 -1
- package/dist/observability/index.d.ts +20 -3
- package/dist/observability/index.js +8 -5
- package/dist/observability/index.js.map +1 -1
- package/dist/pipeline/index.d.ts +114 -72
- package/dist/pipeline/index.js +478 -286
- package/dist/pipeline/index.js.map +1 -1
- package/dist/retry-CjvSlKGW.d.ts +16 -0
- package/dist/skills/index.d.ts +5 -4
- package/dist/skills/index.js +7 -8
- package/dist/speech-generation/index.d.ts +37 -0
- package/dist/speech-generation/index.js +8 -0
- package/dist/streaming/index.js +37 -3
- package/dist/streaming/index.js.map +1 -1
- package/dist/{think-tool-DfCMuO8e.d.ts → think-tool-6H2QNQzQ.d.ts} +5 -5
- package/dist/tool/index.d.ts +10 -6
- package/dist/tool/index.js +15 -21
- package/dist/tool--Mz4v1eL.d.ts +57 -0
- package/dist/transcription/index.d.ts +29 -22
- package/dist/transcription/index.js +5 -57
- package/dist/transcription/index.js.map +1 -1
- package/dist/type-utils-CtHVDRn_.d.ts +6 -0
- package/dist/types-3x3mOLHU.d.ts +379 -0
- package/dist/types-BVzM4RsC.d.ts +155 -0
- package/dist/types-CQ_ioXWK.d.ts +319 -0
- package/dist/types-Cr4uiYo5.d.ts +99 -0
- package/dist/{types-04LqWrmy.d.ts → types-DiJqJejp.d.ts} +1 -1
- package/dist/types-Dld1TpWj.d.ts +130 -0
- package/dist/vector-store/index.d.ts +33 -34
- package/dist/vector-store/index.js +6 -6
- package/package.json +6 -18
- package/dist/agent-x3YTyv5i.d.ts +0 -222
- package/dist/audio-generation/index.d.ts +0 -33
- package/dist/audio-generation/index.js +0 -42
- package/dist/audio-generation/index.js.map +0 -1
- package/dist/chunk-2ODTMRHP.js +0 -54
- package/dist/chunk-2ODTMRHP.js.map +0 -1
- package/dist/chunk-ADH7NNCS.js +0 -512
- package/dist/chunk-ADH7NNCS.js.map +0 -1
- package/dist/chunk-BALBBVI3.js +0 -2732
- package/dist/chunk-BALBBVI3.js.map +0 -1
- package/dist/chunk-CWUJUSOS.js.map +0 -1
- package/dist/chunk-DALYP4PX.js +0 -225
- package/dist/chunk-DALYP4PX.js.map +0 -1
- package/dist/chunk-HYUYZEAJ.js +0 -223
- package/dist/chunk-HYUYZEAJ.js.map +0 -1
- package/dist/chunk-I6XOQ4LK.js +0 -466
- package/dist/chunk-I6XOQ4LK.js.map +0 -1
- package/dist/chunk-JI5YZWNT.js +0 -220
- package/dist/chunk-JI5YZWNT.js.map +0 -1
- package/dist/chunk-MC3CCKEB.js +0 -221
- package/dist/chunk-MC3CCKEB.js.map +0 -1
- package/dist/chunk-MELYDKWQ.js +0 -337
- package/dist/chunk-MELYDKWQ.js.map +0 -1
- package/dist/chunk-MNWK2USR.js +0 -243
- package/dist/chunk-MNWK2USR.js.map +0 -1
- package/dist/chunk-MRQLEK2B.js.map +0 -1
- package/dist/chunk-NCPN4SJR.js.map +0 -1
- package/dist/chunk-OIMLU4SF.js +0 -20
- package/dist/chunk-OIMLU4SF.js.map +0 -1
- package/dist/chunk-Q25IWLBT.js +0 -138
- package/dist/chunk-Q25IWLBT.js.map +0 -1
- package/dist/chunk-UQX6EXWG.js +0 -33
- package/dist/chunk-UQX6EXWG.js.map +0 -1
- package/dist/chunk-WQKHFADH.js +0 -12
- package/dist/chunk-WQKHFADH.js.map +0 -1
- package/dist/chunk-XUUY2L2D.js +0 -42
- package/dist/chunk-XUUY2L2D.js.map +0 -1
- package/dist/chunk-XWUC7CIT.js +0 -1
- package/dist/chunk-XWUC7CIT.js.map +0 -1
- package/dist/chunk-YLMDBQO4.js +0 -141
- package/dist/chunk-YLMDBQO4.js.map +0 -1
- package/dist/errors-2moLDuuN.d.ts +0 -17
- package/dist/hooks/index.d.ts +0 -12
- package/dist/hooks/index.js +0 -17
- package/dist/index-BxSbmOMr.d.ts +0 -257
- package/dist/json-BVPssigK.d.ts +0 -39
- package/dist/middleware-Dp3d_pxi.d.ts +0 -90
- package/dist/request/index.d.ts +0 -28
- package/dist/request/index.js +0 -25
- package/dist/request/index.js.map +0 -1
- package/dist/tool-Cohdv5GC.d.ts +0 -65
- package/dist/types-BCTRUGex.d.ts +0 -47
- package/dist/types-BrLbKnEz.d.ts +0 -119
- package/dist/types-D9JpeAPF.d.ts +0 -114
- package/dist/types-G12i56s2.d.ts +0 -354
- package/dist/types-JJCxRnaZ.d.ts +0 -70
- package/dist/types-O-abTIin.d.ts +0 -79
- package/dist/ui/index.d.ts +0 -107
- package/dist/ui/index.js +0 -10
- package/dist/ui/index.js.map +0 -1
- /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 {
|
|
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
|
-
|
|
41
|
+
inputSchema: z.object({ orderId: z.string() }),
|
|
42
42
|
execute: async ({ orderId }) => ({ orderId, status: "processing" }),
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
-
const agent = new
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
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 `
|
|
59
|
-
|
|
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 {
|
|
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
|
|
68
|
-
|
|
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.
|
|
74
|
+
console.log(result.output); // string
|
|
73
75
|
```
|
|
74
76
|
|
|
75
|
-
Use `messages` when
|
|
76
|
-
|
|
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 {
|
|
81
|
+
import { generateCompletion, type Message } from "@anvia/core";
|
|
80
82
|
|
|
81
|
-
const result = await
|
|
83
|
+
const result = await generateCompletion({
|
|
84
|
+
model,
|
|
82
85
|
messages: [
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
],
|
|
86
|
+
{ role: "system", content: "You are concise." },
|
|
87
|
+
{ role: "user", content: "Explain Anvia." },
|
|
88
|
+
] satisfies readonly Message[],
|
|
86
89
|
maxTokens: 300,
|
|
87
|
-
|
|
90
|
+
providerOptions: {
|
|
88
91
|
reasoning: { effort: "low" },
|
|
89
92
|
},
|
|
90
93
|
});
|
|
91
94
|
```
|
|
92
95
|
|
|
93
|
-
|
|
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 {
|
|
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
|
-
|
|
99
|
-
|
|
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
|
-
|
|
106
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
115
|
-
|
|
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
|
-
|
|
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
|
-
|
|
122
|
-
import { createParsedCompletion } from "@anvia/core";
|
|
123
|
-
import { z } from "zod";
|
|
161
|
+
## Retries, Provider Options, and Cancellation
|
|
124
162
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
209
|
+
const result = await agent.generate({ prompt: "Help with this request." });
|
|
150
210
|
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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(
|
|
170
|
-
return [...(this.sessions.get(
|
|
240
|
+
async load({ scope }: { scope: MemoryScope }): Promise<Message[]> {
|
|
241
|
+
return [...(this.sessions.get(scope.sessionId) ?? [])];
|
|
171
242
|
}
|
|
172
243
|
|
|
173
|
-
async append(input:
|
|
174
|
-
const current = this.sessions.get(input.
|
|
175
|
-
this.sessions.set(input.
|
|
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(
|
|
179
|
-
this.sessions.delete(
|
|
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
|
|
255
|
+
const agent = new Agent({
|
|
256
|
+
id: "support",
|
|
257
|
+
model: model,
|
|
258
|
+
memory: { store: memory },
|
|
259
|
+
});
|
|
185
260
|
|
|
186
|
-
|
|
187
|
-
await agent.
|
|
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
|
|
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 {
|
|
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
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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 {
|
|
340
|
+
import { Pipeline } from "@anvia/core/pipeline";
|
|
218
341
|
import { z } from "zod";
|
|
219
342
|
|
|
220
|
-
const pipeline = new
|
|
221
|
-
.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
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`:
|
|
440
|
+
- `agent`: typed Agent runtime, run results, approvals, retries, and stream events
|
|
232
441
|
- `tool`: typed tool creation and tool sets
|
|
233
|
-
- `completion`:
|
|
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
|
|
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
|
-
- `
|
|
454
|
+
- `speech-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
|
|
246
455
|
|
|
247
456
|
## Development
|
|
248
457
|
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,75 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import
|
|
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 '
|
|
16
|
-
import '
|
|
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 '
|
|
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
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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 {
|
|
41
|
+
export { AgentBlockedResult, AgentRunBlockedError, AgentRunCancelledError, MaxTurnsError };
|