@anvia/core 0.1.0 → 0.1.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 +64 -0
- package/dist/agent/index.d.ts +11 -4
- package/dist/agent/index.js +8 -1
- package/dist/{agent-C6h6YrRU.d.ts → agent-D5KKP9_z.d.ts} +38 -5
- package/dist/{chunk-IQBY2GCF.js → chunk-BIJ5ZBCO.js} +262 -27
- package/dist/chunk-BIJ5ZBCO.js.map +1 -0
- package/dist/{chunk-X6FBOU2P.js → chunk-J23SIK6Y.js} +7 -1
- package/dist/chunk-J23SIK6Y.js.map +1 -0
- package/dist/{chunk-KSIY7KJA.js → chunk-JDPPZCOY.js} +53 -42
- package/dist/chunk-JDPPZCOY.js.map +1 -0
- package/dist/{chunk-LMBOJMNB.js → chunk-U524PWC6.js} +2 -2
- package/dist/chunk-XXT2UCAR.js +11 -0
- package/dist/chunk-XXT2UCAR.js.map +1 -0
- package/dist/chunk-YK4WAAS4.js +18 -0
- package/dist/chunk-YK4WAAS4.js.map +1 -0
- package/dist/{chunk-IA76K5UX.js → chunk-ZCKIAMMR.js} +2 -2
- package/dist/evals/index.d.ts +3 -2
- package/dist/evals/index.js +6 -4
- package/dist/extractor/index.d.ts +3 -2
- package/dist/extractor/index.js +5 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.js +18 -9
- package/dist/memory/index.d.ts +43 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/pipeline/index.d.ts +3 -2
- package/dist/skills/index.js +3 -2
- package/dist/tool/index.d.ts +2 -2
- package/dist/tool/index.js +3 -1
- package/dist/{zod-schema-DJTEgQBq.d.ts → zod-schema-Cq_9zlmJ.d.ts} +15 -1
- package/package.json +6 -2
- package/dist/chunk-IQBY2GCF.js.map +0 -1
- package/dist/chunk-KSIY7KJA.js.map +0 -1
- package/dist/chunk-X6FBOU2P.js.map +0 -1
- /package/dist/{chunk-LMBOJMNB.js.map → chunk-U524PWC6.js.map} +0 -0
- /package/dist/{chunk-IA76K5UX.js.map → chunk-ZCKIAMMR.js.map} +0 -0
package/README.md
CHANGED
|
@@ -47,6 +47,69 @@ const response = await agent.prompt("What is happening with order A123?").send()
|
|
|
47
47
|
console.log(response.output);
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
## Prompts and Memory
|
|
51
|
+
|
|
52
|
+
Use a plain prompt for stateless calls:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
await agent.prompt("Summarize this ticket.").send();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Use a message array when you already own the transcript. The last message is the active prompt and earlier messages are request history:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { Message } from "@anvia/core";
|
|
62
|
+
|
|
63
|
+
await agent
|
|
64
|
+
.prompt([
|
|
65
|
+
Message.user("My project is named Anvia."),
|
|
66
|
+
Message.assistant("Noted."),
|
|
67
|
+
Message.user("What is my project named?"),
|
|
68
|
+
])
|
|
69
|
+
.send();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Configure durable conversation memory on the agent, then run through a session:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import {
|
|
76
|
+
AgentBuilder,
|
|
77
|
+
type MemoryAppendInput,
|
|
78
|
+
type MemoryContext,
|
|
79
|
+
type MemoryStore,
|
|
80
|
+
type Message,
|
|
81
|
+
} from "@anvia/core";
|
|
82
|
+
|
|
83
|
+
class AppMemoryStore implements MemoryStore {
|
|
84
|
+
private readonly sessions = new Map<string, Message[]>();
|
|
85
|
+
|
|
86
|
+
async load(context: MemoryContext): Promise<Message[]> {
|
|
87
|
+
return [...(this.sessions.get(context.sessionId) ?? [])];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async append(input: MemoryAppendInput): Promise<void> {
|
|
91
|
+
const current = this.sessions.get(input.context.sessionId) ?? [];
|
|
92
|
+
this.sessions.set(input.context.sessionId, [...current, ...input.messages]);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async clear(context: MemoryContext): Promise<void> {
|
|
96
|
+
this.sessions.delete(context.sessionId);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const memory = new AppMemoryStore();
|
|
101
|
+
const agent = new AgentBuilder("support", model).memory(memory).build();
|
|
102
|
+
|
|
103
|
+
await agent.session("thread_123", { userId: "user_456" }).prompt("Remember my plan.").send();
|
|
104
|
+
await agent.session("thread_123", { userId: "user_456" }).prompt("What is my plan?").send();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Memory defaults to `savePolicy: "message"`, which saves the user prompt, each completed assistant message, and each completed tool result as soon as they are ready. You can choose `"turn"` or `"run"` at configuration time:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
new AgentBuilder("support", model).memory(memory, { savePolicy: "turn" });
|
|
111
|
+
```
|
|
112
|
+
|
|
50
113
|
## Structured Extraction
|
|
51
114
|
|
|
52
115
|
```ts
|
|
@@ -80,6 +143,7 @@ const result = await pipeline.run("Customer cannot complete checkout.");
|
|
|
80
143
|
- `agent`: agent runtime and `AgentBuilder`
|
|
81
144
|
- `tool`: typed tool creation and tool sets
|
|
82
145
|
- `completion`: provider-neutral completion request and response types
|
|
146
|
+
- `memory`: durable session memory interfaces and in-memory store
|
|
83
147
|
- `extractor`: schema-first structured extraction
|
|
84
148
|
- `pipeline`: typed sequential and parallel workflows
|
|
85
149
|
- `embeddings`: embedding helpers and document embedding utilities
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { MemoryStore, MemoryOptions } from '../memory/index.js';
|
|
2
|
+
export { MemoryAppendInput, MemoryContext, MemoryErrorInput, MemoryRegistration, MemorySavePolicy, ResolvedMemoryOptions, SessionOptions, resolveMemoryOptions } from '../memory/index.js';
|
|
3
|
+
import { f as DynamicContextOptions, h as DynamicToolOptions, P as PromptHook, A as Agent } from '../agent-D5KKP9_z.js';
|
|
4
|
+
export { a as AgentOptions, b as AgentSession, c as AgentStreamEvent, d as AgentToolOptions, C as CompletionCallHookArgs, e as CompletionResponseHookArgs, D as DEFAULT_MAX_TURNS, g as DynamicContextRegistration, i as DynamicToolRegistration, H as HookAction, j as HookResult, k as PromptRequest, l as PromptResponse, R as RunControl, T as ToolCallControl, m as ToolCallHookAction, n as ToolCallHookArgs, o as ToolCallHookResult, p as ToolHookArgs, q as ToolResultHookArgs, r as cancelPrompt, s as createHook, t as runControl, u as skipTool, v as toolCallControl } from '../agent-D5KKP9_z.js';
|
|
3
5
|
import { b as CompletionModel, j as JsonValue, o as ToolChoice, M as Message } from '../types-BrxLd7ay.js';
|
|
4
6
|
import { c as McpServer } from '../types-B5B8Sdl4.js';
|
|
5
7
|
import { AgentObserver, ObserveOptions } from '../observability/index.js';
|
|
6
|
-
import {
|
|
8
|
+
import { b as ToolSearchDocument, c as ToolSet, T as ToolMiddleware, Z as ZodSchema } from '../zod-schema-Cq_9zlmJ.js';
|
|
7
9
|
import { b as SkillSet } from '../types-HvopERm0.js';
|
|
8
10
|
import { A as AnyTool } from '../tool-DhuBQ3yb.js';
|
|
9
11
|
import { VectorSearchIndex } from '../vector-store/index.js';
|
|
@@ -30,6 +32,8 @@ declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
|
|
|
30
32
|
private observerRegistrations;
|
|
31
33
|
private dynamicContextRegistrations;
|
|
32
34
|
private dynamicToolRegistrations;
|
|
35
|
+
private middlewareRegistrations;
|
|
36
|
+
private memoryRegistration;
|
|
33
37
|
private activeToolSet;
|
|
34
38
|
constructor(agentId: string, completionModel: M);
|
|
35
39
|
name(name: string): this;
|
|
@@ -49,7 +53,10 @@ declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
|
|
|
49
53
|
toolChoice(toolChoice: ToolChoice): this;
|
|
50
54
|
defaultMaxTurns(defaultMaxTurns: number): this;
|
|
51
55
|
hook(hook: PromptHook): this;
|
|
56
|
+
toolMiddleware(middleware: ToolMiddleware): this;
|
|
57
|
+
toolMiddlewares(middlewares: ToolMiddleware[]): this;
|
|
52
58
|
observe(observer: AgentObserver, options?: ObserveOptions): this;
|
|
59
|
+
memory(store: MemoryStore, options?: MemoryOptions): this;
|
|
53
60
|
outputSchema(schema: ZodSchema): this;
|
|
54
61
|
build(): Agent<M>;
|
|
55
62
|
private buildInstructions;
|
|
@@ -67,4 +74,4 @@ declare class PromptCancelledError extends Error {
|
|
|
67
74
|
constructor(chatHistory: Message[], reason: string);
|
|
68
75
|
}
|
|
69
76
|
|
|
70
|
-
export { Agent, AgentBuilder, DynamicContextOptions, DynamicToolOptions, MaxTurnsError, PromptCancelledError, PromptHook };
|
|
77
|
+
export { Agent, AgentBuilder, DynamicContextOptions, DynamicToolOptions, MaxTurnsError, MemoryOptions, MemoryStore, PromptCancelledError, PromptHook };
|
package/dist/agent/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
3
|
AgentBuilder,
|
|
4
|
+
AgentSession,
|
|
4
5
|
DEFAULT_MAX_TURNS,
|
|
5
6
|
MaxTurnsError,
|
|
6
7
|
PromptCancelledError,
|
|
@@ -10,7 +11,11 @@ import {
|
|
|
10
11
|
runControl,
|
|
11
12
|
skipTool,
|
|
12
13
|
toolCallControl
|
|
13
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-BIJ5ZBCO.js";
|
|
15
|
+
import {
|
|
16
|
+
resolveMemoryOptions
|
|
17
|
+
} from "../chunk-XXT2UCAR.js";
|
|
18
|
+
import "../chunk-YK4WAAS4.js";
|
|
14
19
|
import "../chunk-XUUY2L2D.js";
|
|
15
20
|
import "../chunk-GNWMOSNR.js";
|
|
16
21
|
import "../chunk-B4QHQN5K.js";
|
|
@@ -18,12 +23,14 @@ import "../chunk-CP47FBJV.js";
|
|
|
18
23
|
export {
|
|
19
24
|
Agent,
|
|
20
25
|
AgentBuilder,
|
|
26
|
+
AgentSession,
|
|
21
27
|
DEFAULT_MAX_TURNS,
|
|
22
28
|
MaxTurnsError,
|
|
23
29
|
PromptCancelledError,
|
|
24
30
|
PromptRequest,
|
|
25
31
|
cancelPrompt,
|
|
26
32
|
createHook,
|
|
33
|
+
resolveMemoryOptions,
|
|
27
34
|
runControl,
|
|
28
35
|
skipTool,
|
|
29
36
|
toolCallControl
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { M as Message, e as CompletionResponse, b as CompletionModel, U as Usage, l as ReasoningContentType, n as ToolCall, D as Document, j as JsonValue, o as ToolChoice, J as JsonObject } from './types-BrxLd7ay.js';
|
|
2
|
+
import { MemoryContext, MemoryRegistration, SessionOptions } from './memory/index.js';
|
|
2
3
|
import { AgentTraceOptions, AgentTraceInfo, AgentObserverRegistration } from './observability/index.js';
|
|
3
|
-
import {
|
|
4
|
+
import { T as ToolMiddleware, c as ToolSet, b as ToolSearchDocument } from './zod-schema-Cq_9zlmJ.js';
|
|
4
5
|
import { T as Tool, A as AnyTool } from './tool-DhuBQ3yb.js';
|
|
5
6
|
import { VectorSearchIndex, VectorFilter, VectorSearchResult } from './vector-store/index.js';
|
|
6
7
|
|
|
@@ -118,23 +119,30 @@ type AgentStreamEvent<RawResponse = unknown> = {
|
|
|
118
119
|
declare class PromptRequest<M extends CompletionModel = CompletionModel> {
|
|
119
120
|
private readonly agent;
|
|
120
121
|
private readonly promptMessage;
|
|
122
|
+
private readonly initialHistory;
|
|
123
|
+
private readonly memoryContext;
|
|
121
124
|
private chatHistory;
|
|
122
125
|
private maxTurnCount;
|
|
123
126
|
private activeHook;
|
|
124
127
|
private concurrency;
|
|
125
128
|
private traceOptions;
|
|
129
|
+
private requestToolMiddlewares;
|
|
126
130
|
private constructor();
|
|
127
|
-
static fromAgent<M extends CompletionModel>(agent: Agent<M>, prompt: string | Message
|
|
128
|
-
|
|
131
|
+
static fromAgent<M extends CompletionModel>(agent: Agent<M>, prompt: string | Message | Message[], options?: {
|
|
132
|
+
memoryContext?: MemoryContext | undefined;
|
|
133
|
+
}): PromptRequest<M>;
|
|
129
134
|
maxTurns(maxTurns: number): this;
|
|
130
135
|
requestHook(hook: PromptHook): this;
|
|
131
136
|
withToolConcurrency(concurrency: number): this;
|
|
137
|
+
withToolMiddleware(middleware: ToolMiddleware): this;
|
|
138
|
+
withToolMiddlewares(middlewares: ToolMiddleware[]): this;
|
|
132
139
|
withTrace(trace: AgentTraceOptions): this;
|
|
133
140
|
send(): Promise<PromptResponse>;
|
|
134
141
|
stream(): AsyncIterable<AgentStreamEvent>;
|
|
135
142
|
readableStream(): ReadableStream<Uint8Array>;
|
|
136
143
|
private runCompletion;
|
|
137
144
|
private executeToolCalls;
|
|
145
|
+
private runToolResultMiddlewares;
|
|
138
146
|
private startRunObservers;
|
|
139
147
|
private fetchDynamicContext;
|
|
140
148
|
private fetchToolDefinitions;
|
|
@@ -142,6 +150,13 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
|
|
|
142
150
|
private runCompletionCallHook;
|
|
143
151
|
private runCompletionResponseHook;
|
|
144
152
|
private cancelled;
|
|
153
|
+
private memory;
|
|
154
|
+
private memoryPolicy;
|
|
155
|
+
private prepareMemoryRun;
|
|
156
|
+
private commitMemoryMessages;
|
|
157
|
+
private commitCompletedMemoryTurn;
|
|
158
|
+
private commitCompletedMemoryRun;
|
|
159
|
+
private recordMemoryError;
|
|
145
160
|
}
|
|
146
161
|
|
|
147
162
|
type AgentOptions<M extends CompletionModel = CompletionModel> = {
|
|
@@ -162,6 +177,8 @@ type AgentOptions<M extends CompletionModel = CompletionModel> = {
|
|
|
162
177
|
observers?: AgentObserverRegistration[] | undefined;
|
|
163
178
|
dynamicContexts?: DynamicContextRegistration[] | undefined;
|
|
164
179
|
dynamicTools?: DynamicToolRegistration[] | undefined;
|
|
180
|
+
toolMiddlewares?: ToolMiddleware[] | undefined;
|
|
181
|
+
memory?: MemoryRegistration | undefined;
|
|
165
182
|
};
|
|
166
183
|
declare const DEFAULT_MAX_TURNS = 20;
|
|
167
184
|
type AgentToolOptions = {
|
|
@@ -206,13 +223,29 @@ declare class Agent<M extends CompletionModel = CompletionModel> {
|
|
|
206
223
|
readonly observers: AgentObserverRegistration[];
|
|
207
224
|
readonly dynamicContexts: DynamicContextRegistration[];
|
|
208
225
|
readonly dynamicTools: DynamicToolRegistration[];
|
|
226
|
+
readonly toolMiddlewares: ToolMiddleware[];
|
|
227
|
+
readonly memory: MemoryRegistration | undefined;
|
|
209
228
|
constructor(options: AgentOptions<M>);
|
|
210
|
-
prompt(prompt: string | Message): PromptRequest<M>;
|
|
229
|
+
prompt(prompt: string | Message | Message[]): PromptRequest<M>;
|
|
230
|
+
session(sessionId: string, options?: SessionOptions): AgentSession<M>;
|
|
211
231
|
asTool(options: AgentToolOptions): Tool<{
|
|
212
232
|
prompt: string;
|
|
213
233
|
}, string>;
|
|
214
234
|
getTool(toolName: string): AnyTool | undefined;
|
|
215
235
|
callTool(toolName: string, args: string): Promise<string>;
|
|
236
|
+
shouldApplyToolMiddleware(toolName: string): boolean;
|
|
237
|
+
}
|
|
238
|
+
declare class AgentSession<M extends CompletionModel = CompletionModel> {
|
|
239
|
+
private readonly agent;
|
|
240
|
+
private readonly context;
|
|
241
|
+
constructor(agent: Agent<M>, context: {
|
|
242
|
+
sessionId: string;
|
|
243
|
+
userId?: string | undefined;
|
|
244
|
+
metadata?: JsonObject | undefined;
|
|
245
|
+
});
|
|
246
|
+
prompt(prompt: string | Message): PromptRequest<M>;
|
|
247
|
+
messages(): Promise<Message[]>;
|
|
248
|
+
clear(): Promise<void>;
|
|
216
249
|
}
|
|
217
250
|
|
|
218
|
-
export { Agent as A, type CompletionCallHookArgs as C, DEFAULT_MAX_TURNS as D, type HookAction as H, type PromptHook as P, type RunControl as R, type ToolCallControl as T, type AgentOptions as a, type AgentStreamEvent as
|
|
251
|
+
export { Agent as A, type CompletionCallHookArgs as C, DEFAULT_MAX_TURNS as D, type HookAction as H, type PromptHook as P, type RunControl as R, type ToolCallControl as T, type AgentOptions as a, AgentSession as b, type AgentStreamEvent as c, type AgentToolOptions as d, type CompletionResponseHookArgs as e, type DynamicContextOptions as f, type DynamicContextRegistration as g, type DynamicToolOptions as h, type DynamicToolRegistration as i, type HookResult as j, PromptRequest as k, type PromptResponse as l, type ToolCallHookAction as m, type ToolCallHookArgs as n, type ToolCallHookResult as o, type ToolHookArgs as p, type ToolResultHookArgs as q, cancelPrompt as r, createHook as s, runControl as t, skipTool as u, toolCallControl as v };
|