@anvia/core 0.3.0 → 0.4.0
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 +7 -8
- package/dist/agent/index.d.ts +9 -9
- package/dist/agent/index.js +7 -16
- package/dist/{agent-D_jXqPAZ.d.ts → agent-0UeJ9Rad.d.ts} +5 -5
- package/dist/audio-generation/index.d.ts +1 -1
- package/dist/audio-generation/index.js +37 -4
- package/dist/audio-generation/index.js.map +1 -1
- package/dist/chunk-6U6PZ5MD.js +197 -0
- package/dist/chunk-6U6PZ5MD.js.map +1 -0
- package/dist/{chunk-QRZ2HPUX.js → chunk-PP4VIN3Y.js} +2 -2
- package/dist/chunk-PP4VIN3Y.js.map +1 -0
- package/dist/{chunk-GSFQZYS7.js → chunk-TP32W7XT.js} +69 -255
- package/dist/chunk-TP32W7XT.js.map +1 -0
- package/dist/completion/index.d.ts +2 -2
- package/dist/evals/index.d.ts +5 -4
- package/dist/evals/index.js +294 -13
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +6 -5
- package/dist/extractor/index.js +5 -4
- package/dist/image-generation/index.d.ts +1 -1
- package/dist/image-generation/index.js +37 -4
- package/dist/image-generation/index.js.map +1 -1
- package/dist/index.d.ts +12 -21
- package/dist/index.js +10 -157
- package/dist/internal/agent.d.ts +8 -0
- package/dist/internal/agent.js +17 -0
- package/dist/internal/agent.js.map +1 -0
- package/dist/loaders/index.d.ts +1 -1
- package/dist/loaders/index.js +3 -2
- package/dist/loaders/index.js.map +1 -1
- package/dist/mcp/index.d.ts +4 -4
- package/dist/mcp/index.js +140 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory/index.d.ts +1 -1
- package/dist/{middleware-2FR89muX.d.ts → middleware-BQ7fkEEe.d.ts} +3 -6
- package/dist/model-listing/index.js +13 -3
- package/dist/model-listing/index.js.map +1 -1
- package/dist/observability/index.d.ts +2 -2
- package/dist/observability/index.js +4 -3
- package/dist/observability/index.js.map +1 -1
- package/dist/pipeline/index.d.ts +6 -5
- package/dist/pipeline/index.js +339 -4
- package/dist/pipeline/index.js.map +1 -1
- package/dist/skills/index.d.ts +4 -4
- package/dist/think-tool-ByArsrxe.d.ts +26 -0
- package/dist/tool/index.d.ts +7 -28
- package/dist/{tool-ClZYES-Z.d.ts → tool-C3ciF-VG.d.ts} +2 -2
- package/dist/transcription/index.d.ts +1 -1
- package/dist/transcription/index.js +55 -4
- package/dist/transcription/index.js.map +1 -1
- package/dist/{types-BM8-Y8Hy.d.ts → types-C54aNoCd.d.ts} +1 -1
- package/dist/{types-B0iXLkqi.d.ts → types-T9rlOIUc.d.ts} +2 -2
- package/dist/{types-fLi8uM5R.d.ts → types-UhcgW8Fo.d.ts} +3 -3
- package/dist/vector-store/index.d.ts +2 -2
- package/dist/zod-schema-C7F4clpm.d.ts +5 -0
- package/package.json +8 -4
- package/dist/chunk-6JCCM2K4.js +0 -304
- package/dist/chunk-6JCCM2K4.js.map +0 -1
- package/dist/chunk-7QI6ZAFI.js +0 -61
- package/dist/chunk-7QI6ZAFI.js.map +0 -1
- package/dist/chunk-B24Q2ZYM.js +0 -43
- package/dist/chunk-B24Q2ZYM.js.map +0 -1
- package/dist/chunk-GSFQZYS7.js.map +0 -1
- package/dist/chunk-I77SDTFE.js +0 -43
- package/dist/chunk-I77SDTFE.js.map +0 -1
- package/dist/chunk-NYUOBGVI.js +0 -146
- package/dist/chunk-NYUOBGVI.js.map +0 -1
- package/dist/chunk-QRZ2HPUX.js.map +0 -1
- package/dist/chunk-RTOGIJH2.js +0 -345
- package/dist/chunk-RTOGIJH2.js.map +0 -1
- package/dist/chunk-WJT22R3T.js +0 -18
- package/dist/chunk-WJT22R3T.js.map +0 -1
- package/dist/chunk-ZSYIQYTV.js +0 -9
- package/dist/chunk-ZSYIQYTV.js.map +0 -1
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ pnpm --filter @anvia/core build
|
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
import { z } from "zod";
|
|
23
|
-
import { AgentBuilder,
|
|
23
|
+
import { AgentBuilder, createTool } from "@anvia/core";
|
|
24
24
|
import { OpenAIClient } from "@anvia/openai";
|
|
25
25
|
|
|
26
26
|
const client = new OpenAIClient({
|
|
@@ -72,13 +72,8 @@ await agent
|
|
|
72
72
|
Configure durable conversation memory on the agent, then run through a session:
|
|
73
73
|
|
|
74
74
|
```ts
|
|
75
|
-
import {
|
|
76
|
-
|
|
77
|
-
type MemoryAppendInput,
|
|
78
|
-
type MemoryContext,
|
|
79
|
-
type MemoryStore,
|
|
80
|
-
type Message,
|
|
81
|
-
} from "@anvia/core";
|
|
75
|
+
import { AgentBuilder, type MemoryStore, type Message } from "@anvia/core";
|
|
76
|
+
import type { MemoryAppendInput, MemoryContext } from "@anvia/core/memory";
|
|
82
77
|
|
|
83
78
|
class AppMemoryStore implements MemoryStore {
|
|
84
79
|
private readonly sessions = new Map<string, Message[]>();
|
|
@@ -113,6 +108,8 @@ new AgentBuilder("support", model).memory(memory, { savePolicy: "turn" });
|
|
|
113
108
|
## Structured Extraction
|
|
114
109
|
|
|
115
110
|
```ts
|
|
111
|
+
import { ExtractorBuilder } from "@anvia/core/extractor";
|
|
112
|
+
|
|
116
113
|
const ticketSchema = z.object({
|
|
117
114
|
customer: z.string(),
|
|
118
115
|
priority: z.enum(["low", "medium", "high"]),
|
|
@@ -129,6 +126,8 @@ const ticket = await extractor.extract(
|
|
|
129
126
|
## Pipelines
|
|
130
127
|
|
|
131
128
|
```ts
|
|
129
|
+
import { PipelineBuilder } from "@anvia/core/pipeline";
|
|
130
|
+
|
|
132
131
|
const pipeline = new PipelineBuilder<string>()
|
|
133
132
|
.step((input) => `Extract this support ticket:\n\n${input}`)
|
|
134
133
|
.prompt(agent)
|
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
import { D as DynamicContextOptions, f as DynamicToolOptions, g as PromptHook, h as AgentEventStore, i as AgentEventStoreOptions, e as Agent } from '../agent-0UeJ9Rad.js';
|
|
2
|
+
export { A as AgentChildStreamEvent, j as AgentEventAppendInput, k as AgentEventRecord, l as AgentEventStoreInclude, a as AgentStreamEvent, C as CompletionCallHookArgs, m as CompletionResponseHookArgs, H as HookAction, n as HookResult, P as PromptResponse, R as RunControl, T as ToolApprovalRequestOptions, o as ToolCallControl, p as ToolCallHookAction, q as ToolCallHookArgs, u as ToolCallHookResult, v as ToolHookArgs, w as ToolResultHookArgs, c as cancelPrompt, b as createHook, r as requestToolApproval, d as runControl, s as skipTool, t as toolCallControl } from '../agent-0UeJ9Rad.js';
|
|
3
|
+
import { C as CompletionModel, e as JsonValue, m as ToolChoice, M as Message } from '../types-C54aNoCd.js';
|
|
4
|
+
import { a as McpServer } from '../types-UhcgW8Fo.js';
|
|
1
5
|
import { MemoryStore, MemoryOptions } from '../memory/index.js';
|
|
2
|
-
export { MemoryAppendInput, MemoryContext, MemoryErrorInput, MemoryRegistration, MemorySavePolicy, ResolvedMemoryOptions, SessionOptions, resolveMemoryOptions } from '../memory/index.js';
|
|
3
|
-
import { m as DynamicContextOptions, o as DynamicToolOptions, P as PromptHook, d as AgentEventStore, f as AgentEventStoreOptions, A as Agent } from '../agent-D_jXqPAZ.js';
|
|
4
|
-
export { a as AgentChildStreamEvent, b as AgentEventAppendInput, c as AgentEventRecord, e as AgentEventStoreInclude, g as AgentEventStoreRegistration, h as AgentOptions, i as AgentSession, j as AgentStreamEvent, k as AgentToolOptions, C as CompletionCallHookArgs, l as CompletionResponseHookArgs, D as DEFAULT_MAX_TURNS, n as DynamicContextRegistration, p as DynamicToolRegistration, H as HookAction, q as HookResult, r as PromptRequest, s as PromptResponse, R as RunControl, T as ToolApprovalRequestOptions, t as ToolCallControl, u as ToolCallHookAction, v as ToolCallHookArgs, w as ToolCallHookResult, x as ToolHookArgs, y as ToolResultHookArgs, z as cancelPrompt, B as createHook, E as requestToolApproval, F as runControl, G as skipTool, I as toolCallControl } from '../agent-D_jXqPAZ.js';
|
|
5
|
-
import { b as CompletionModel, j as JsonValue, o as ToolChoice, M as Message } from '../types-BM8-Y8Hy.js';
|
|
6
|
-
import { c as McpServer } from '../types-fLi8uM5R.js';
|
|
7
6
|
import { AgentObserver, ObserveOptions } from '../observability/index.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
7
|
+
import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
|
|
8
|
+
import { a as SkillSet } from '../types-T9rlOIUc.js';
|
|
9
|
+
import { b as ToolSearchDocument, c as ToolSet, T as ToolMiddleware } from '../middleware-BQ7fkEEe.js';
|
|
10
|
+
import { A as AnyTool } from '../tool-C3ciF-VG.js';
|
|
11
11
|
import { VectorSearchIndex } from '../vector-store/index.js';
|
|
12
12
|
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
13
13
|
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
@@ -77,4 +77,4 @@ declare class PromptCancelledError extends Error {
|
|
|
77
77
|
constructor(chatHistory: Message[], reason: string);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
export {
|
|
80
|
+
export { AgentBuilder, AgentEventStore, AgentEventStoreOptions, MaxTurnsError, PromptCancelledError, PromptHook };
|
package/dist/agent/index.js
CHANGED
|
@@ -1,39 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
DEFAULT_MAX_TURNS,
|
|
2
|
+
AgentBuilder
|
|
3
|
+
} from "../chunk-6U6PZ5MD.js";
|
|
4
|
+
import {
|
|
6
5
|
MaxTurnsError,
|
|
7
6
|
PromptCancelledError,
|
|
8
|
-
PromptRequest,
|
|
9
7
|
cancelPrompt,
|
|
10
8
|
createHook,
|
|
11
9
|
requestToolApproval,
|
|
12
10
|
runControl,
|
|
13
11
|
skipTool,
|
|
14
12
|
toolCallControl
|
|
15
|
-
} from "../chunk-
|
|
16
|
-
import
|
|
17
|
-
|
|
18
|
-
} from "../chunk-XXT2UCAR.js";
|
|
13
|
+
} from "../chunk-TP32W7XT.js";
|
|
14
|
+
import "../chunk-XUUY2L2D.js";
|
|
15
|
+
import "../chunk-XXT2UCAR.js";
|
|
19
16
|
import "../chunk-YK4WAAS4.js";
|
|
20
17
|
import "../chunk-I2HOMD3R.js";
|
|
21
18
|
import "../chunk-65QV627O.js";
|
|
22
|
-
import "../chunk-XUUY2L2D.js";
|
|
23
|
-
import "../chunk-6GJDBBDC.js";
|
|
24
19
|
import "../chunk-A7VDIZQN.js";
|
|
20
|
+
import "../chunk-6GJDBBDC.js";
|
|
25
21
|
export {
|
|
26
|
-
Agent,
|
|
27
22
|
AgentBuilder,
|
|
28
|
-
AgentSession,
|
|
29
|
-
DEFAULT_MAX_TURNS,
|
|
30
23
|
MaxTurnsError,
|
|
31
24
|
PromptCancelledError,
|
|
32
|
-
PromptRequest,
|
|
33
25
|
cancelPrompt,
|
|
34
26
|
createHook,
|
|
35
27
|
requestToolApproval,
|
|
36
|
-
resolveMemoryOptions,
|
|
37
28
|
runControl,
|
|
38
29
|
skipTool,
|
|
39
30
|
toolCallControl
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { M as Message,
|
|
1
|
+
import { M as Message, c as CompletionResponse, j as ToolResultContent, C as CompletionModel, U as Usage, t as ReasoningContentType, f as ToolCall, D as Document, e as JsonValue, m as ToolChoice, J as JsonObject } from './types-C54aNoCd.js';
|
|
2
2
|
import { MemoryContext, MemoryRegistration, SessionOptions } from './memory/index.js';
|
|
3
3
|
import { AgentTraceOptions, AgentTraceInfo, AgentObserverRegistration } from './observability/index.js';
|
|
4
|
-
import { T as ToolMiddleware, c as ToolSet, b as ToolSearchDocument } from './middleware-
|
|
5
|
-
import { T as Tool, A as AnyTool,
|
|
6
|
-
import {
|
|
4
|
+
import { T as ToolMiddleware, c as ToolSet, b as ToolSearchDocument } from './middleware-BQ7fkEEe.js';
|
|
5
|
+
import { T as Tool, A as AnyTool, c as ToolCallContext, N as NormalizedToolOutput } from './tool-C3ciF-VG.js';
|
|
6
|
+
import { VectorFilter, VectorSearchResult, VectorSearchIndex } from './vector-store/index.js';
|
|
7
7
|
|
|
8
8
|
type HookAction = {
|
|
9
9
|
type: "continue";
|
|
@@ -301,4 +301,4 @@ declare class AgentSession<M extends CompletionModel = CompletionModel> {
|
|
|
301
301
|
clear(): Promise<void>;
|
|
302
302
|
}
|
|
303
303
|
|
|
304
|
-
export {
|
|
304
|
+
export { type AgentChildStreamEvent as A, type AgentToolOptions as B, type CompletionCallHookArgs as C, type DynamicContextOptions as D, DEFAULT_MAX_TURNS as E, type DynamicContextRegistration as F, type DynamicToolRegistration as G, type HookAction as H, type PromptResponse as P, type RunControl as R, type ToolApprovalRequestOptions as T, type AgentStreamEvent as a, createHook as b, cancelPrompt as c, runControl as d, Agent as e, type DynamicToolOptions as f, type PromptHook as g, type AgentEventStore as h, type AgentEventStoreOptions as i, type AgentEventAppendInput as j, type AgentEventRecord as k, type AgentEventStoreInclude as l, type CompletionResponseHookArgs as m, type HookResult as n, type ToolCallControl as o, type ToolCallHookAction as p, type ToolCallHookArgs as q, requestToolApproval as r, skipTool as s, toolCallControl as t, type ToolCallHookResult as u, type ToolHookArgs as v, type ToolResultHookArgs as w, type AgentEventStoreRegistration as x, type AgentOptions as y, AgentSession as z };
|
|
@@ -1,7 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/audio-generation/index.ts
|
|
2
|
+
var AudioGenerationRequestBuilder = class {
|
|
3
|
+
constructor(model) {
|
|
4
|
+
this.model = model;
|
|
5
|
+
}
|
|
6
|
+
model;
|
|
7
|
+
request = {
|
|
8
|
+
text: "",
|
|
9
|
+
voice: "",
|
|
10
|
+
speed: 1
|
|
11
|
+
};
|
|
12
|
+
text(text) {
|
|
13
|
+
this.request = { ...this.request, text };
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
voice(voice) {
|
|
17
|
+
this.request = { ...this.request, voice };
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
speed(speed) {
|
|
21
|
+
this.request = { ...this.request, speed };
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
additionalParams(additionalParams) {
|
|
25
|
+
this.request = { ...this.request, additionalParams };
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
build() {
|
|
29
|
+
return { ...this.request };
|
|
30
|
+
}
|
|
31
|
+
send() {
|
|
32
|
+
return this.model.audioGeneration(this.build());
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function audioGenerationRequest(model) {
|
|
36
|
+
return new AudioGenerationRequestBuilder(model);
|
|
37
|
+
}
|
|
5
38
|
export {
|
|
6
39
|
AudioGenerationRequestBuilder,
|
|
7
40
|
audioGenerationRequest
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/audio-generation/index.ts"],"sourcesContent":["import type { JsonValue } from \"../completion\";\n\nexport type AudioGenerationRequest = {\n text: string;\n voice: string;\n speed: number;\n additionalParams?: JsonValue | undefined;\n};\n\nexport type AudioGenerationResponse<RawResponse = unknown> = {\n audio: Uint8Array;\n mediaType?: string | undefined;\n rawResponse: RawResponse;\n};\n\nexport interface AudioGenerationModel<RawResponse = unknown> {\n readonly provider?: string | undefined;\n readonly defaultModel?: string | undefined;\n audioGeneration(request: AudioGenerationRequest): Promise<AudioGenerationResponse<RawResponse>>;\n}\n\nexport class AudioGenerationRequestBuilder<\n Model extends AudioGenerationModel = AudioGenerationModel,\n> {\n private request: AudioGenerationRequest = {\n text: \"\",\n voice: \"\",\n speed: 1,\n };\n\n constructor(private readonly model: Model) {}\n\n text(text: string): this {\n this.request = { ...this.request, text };\n return this;\n }\n\n voice(voice: string): this {\n this.request = { ...this.request, voice };\n return this;\n }\n\n speed(speed: number): this {\n this.request = { ...this.request, speed };\n return this;\n }\n\n additionalParams(additionalParams: JsonValue): this {\n this.request = { ...this.request, additionalParams };\n return this;\n }\n\n build(): AudioGenerationRequest {\n return { ...this.request };\n }\n\n send(): Promise<Awaited<ReturnType<Model[\"audioGeneration\"]>>> {\n return this.model.audioGeneration(this.build()) as Promise<\n Awaited<ReturnType<Model[\"audioGeneration\"]>>\n >;\n }\n}\n\nexport function audioGenerationRequest<Model extends AudioGenerationModel>(\n model: Model,\n): AudioGenerationRequestBuilder<Model> {\n return new AudioGenerationRequestBuilder(model);\n}\n"],"mappings":";AAqBO,IAAM,gCAAN,MAEL;AAAA,EAOA,YAA6B,OAAc;AAAd;AAAA,EAAe;AAAA,EAAf;AAAA,EANrB,UAAkC;AAAA,IACxC,MAAM;AAAA,IACN,OAAO;AAAA,IACP,OAAO;AAAA,EACT;AAAA,EAIA,KAAK,MAAoB;AACvB,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,KAAK;AACvC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,OAAqB;AACzB,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,MAAM;AACxC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,OAAqB;AACzB,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,MAAM;AACxC,WAAO;AAAA,EACT;AAAA,EAEA,iBAAiB,kBAAmC;AAClD,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,iBAAiB;AACnD,WAAO;AAAA,EACT;AAAA,EAEA,QAAgC;AAC9B,WAAO,EAAE,GAAG,KAAK,QAAQ;AAAA,EAC3B;AAAA,EAEA,OAA+D;AAC7D,WAAO,KAAK,MAAM,gBAAgB,KAAK,MAAM,CAAC;AAAA,EAGhD;AACF;AAEO,SAAS,uBACd,OACsC;AACtC,SAAO,IAAI,8BAA8B,KAAK;AAChD;","names":[]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Agent
|
|
3
|
+
} from "./chunk-TP32W7XT.js";
|
|
4
|
+
import {
|
|
5
|
+
resolveMemoryOptions
|
|
6
|
+
} from "./chunk-XXT2UCAR.js";
|
|
7
|
+
import {
|
|
8
|
+
ToolSet
|
|
9
|
+
} from "./chunk-I2HOMD3R.js";
|
|
10
|
+
import {
|
|
11
|
+
toProviderJsonSchema
|
|
12
|
+
} from "./chunk-65QV627O.js";
|
|
13
|
+
|
|
14
|
+
// src/agent/builder.ts
|
|
15
|
+
var AgentBuilder = class {
|
|
16
|
+
constructor(agentId, completionModel) {
|
|
17
|
+
this.completionModel = completionModel;
|
|
18
|
+
this.agentId = normalizeAgentId(agentId);
|
|
19
|
+
}
|
|
20
|
+
completionModel;
|
|
21
|
+
agentId;
|
|
22
|
+
agentName;
|
|
23
|
+
agentDescription;
|
|
24
|
+
instructionBlocks = [];
|
|
25
|
+
contextDocs = [];
|
|
26
|
+
temp;
|
|
27
|
+
maxTokenCount;
|
|
28
|
+
params;
|
|
29
|
+
choice;
|
|
30
|
+
turns;
|
|
31
|
+
requestHook;
|
|
32
|
+
schema;
|
|
33
|
+
skillInstructionBlocks = [];
|
|
34
|
+
observerRegistrations = [];
|
|
35
|
+
dynamicContextRegistrations = [];
|
|
36
|
+
dynamicToolRegistrations = [];
|
|
37
|
+
middlewareRegistrations = [];
|
|
38
|
+
memoryRegistration;
|
|
39
|
+
eventStoreRegistration;
|
|
40
|
+
activeToolSet = new ToolSet();
|
|
41
|
+
name(name) {
|
|
42
|
+
this.agentName = name;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
description(description) {
|
|
46
|
+
this.agentDescription = description;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
instructions(instructions) {
|
|
50
|
+
if (instructions.length > 0) {
|
|
51
|
+
this.instructionBlocks.push(instructions);
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
context(text, id = `static_doc_${this.contextDocs.length}`) {
|
|
56
|
+
this.contextDocs.push({ id, text });
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
dynamicContext(index, options) {
|
|
60
|
+
this.dynamicContextRegistrations.push({ index, options });
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
dynamicTools(index, options) {
|
|
64
|
+
this.dynamicToolRegistrations.push({ index, options });
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
tool(tool) {
|
|
68
|
+
this.activeToolSet.addTool(tool);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
tools(tools) {
|
|
72
|
+
this.activeToolSet.addTools(tools);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
mcp(servers) {
|
|
76
|
+
for (const server of servers) {
|
|
77
|
+
this.activeToolSet.addTools(server.tools);
|
|
78
|
+
}
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
skills(skillSet) {
|
|
82
|
+
if (skillSet.instructions.length > 0) {
|
|
83
|
+
this.skillInstructionBlocks.push(skillSet.instructions);
|
|
84
|
+
}
|
|
85
|
+
this.activeToolSet.addTools(skillSet.tools);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
useToolSet(toolSet) {
|
|
89
|
+
toolSet.addTools(this.activeToolSet);
|
|
90
|
+
this.activeToolSet = toolSet;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
temperature(temperature) {
|
|
94
|
+
this.temp = temperature;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
maxTokens(maxTokens) {
|
|
98
|
+
this.maxTokenCount = maxTokens;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
additionalParams(params) {
|
|
102
|
+
this.params = params;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
toolChoice(toolChoice) {
|
|
106
|
+
this.choice = toolChoice;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
defaultMaxTurns(defaultMaxTurns) {
|
|
110
|
+
this.turns = defaultMaxTurns;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
hook(hook) {
|
|
114
|
+
this.requestHook = hook;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
toolMiddleware(middleware) {
|
|
118
|
+
this.middlewareRegistrations.push(middleware);
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
toolMiddlewares(middlewares) {
|
|
122
|
+
this.middlewareRegistrations.push(...middlewares);
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
observe(observer, options = {}) {
|
|
126
|
+
this.observerRegistrations.push({
|
|
127
|
+
observer,
|
|
128
|
+
failOnObserverError: options.failOnObserverError
|
|
129
|
+
});
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
memory(store, options = {}) {
|
|
133
|
+
this.memoryRegistration = {
|
|
134
|
+
store,
|
|
135
|
+
options: resolveMemoryOptions(options)
|
|
136
|
+
};
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
eventStore(store, options = {}) {
|
|
140
|
+
this.eventStoreRegistration = {
|
|
141
|
+
store,
|
|
142
|
+
options: {
|
|
143
|
+
include: options.include ?? "all"
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
outputSchema(schema) {
|
|
149
|
+
this.schema = toProviderJsonSchema(schema);
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
build() {
|
|
153
|
+
return new Agent({
|
|
154
|
+
id: this.agentId,
|
|
155
|
+
name: this.agentName,
|
|
156
|
+
description: this.agentDescription,
|
|
157
|
+
model: this.completionModel,
|
|
158
|
+
instructions: this.buildInstructions(),
|
|
159
|
+
staticContext: this.contextDocs,
|
|
160
|
+
temperature: this.temp,
|
|
161
|
+
maxTokens: this.maxTokenCount,
|
|
162
|
+
additionalParams: this.params,
|
|
163
|
+
toolSet: this.activeToolSet,
|
|
164
|
+
toolChoice: this.choice,
|
|
165
|
+
defaultMaxTurns: this.turns,
|
|
166
|
+
hook: this.requestHook,
|
|
167
|
+
outputSchema: this.schema,
|
|
168
|
+
observers: this.observerRegistrations,
|
|
169
|
+
dynamicContexts: this.dynamicContextRegistrations,
|
|
170
|
+
dynamicTools: this.dynamicToolRegistrations,
|
|
171
|
+
toolMiddlewares: this.middlewareRegistrations,
|
|
172
|
+
memory: this.memoryRegistration,
|
|
173
|
+
eventStore: this.eventStoreRegistration
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
buildInstructions() {
|
|
177
|
+
const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(
|
|
178
|
+
(part) => part !== void 0 && part.length > 0
|
|
179
|
+
);
|
|
180
|
+
return parts.length === 0 ? void 0 : parts.join("\n\n");
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
function normalizeAgentId(id) {
|
|
184
|
+
if (typeof id !== "string") {
|
|
185
|
+
throw new TypeError("Agent id must be a string.");
|
|
186
|
+
}
|
|
187
|
+
const normalized = id.trim();
|
|
188
|
+
if (normalized.length === 0) {
|
|
189
|
+
throw new TypeError("Agent id must be a non-empty string.");
|
|
190
|
+
}
|
|
191
|
+
return normalized;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export {
|
|
195
|
+
AgentBuilder
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=chunk-6U6PZ5MD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent/builder.ts"],"sourcesContent":["import type { CompletionModel, Document, JsonObject, JsonValue, ToolChoice } from \"../completion\";\nimport type { McpServer } from \"../mcp\";\nimport {\n type MemoryOptions,\n type MemoryRegistration,\n type MemoryStore,\n resolveMemoryOptions,\n} from \"../memory\";\nimport type { AgentObserver, AgentObserverRegistration, ObserveOptions } from \"../observability\";\nimport { toProviderJsonSchema, type ZodSchema } from \"../schema/zod-schema\";\nimport type { SkillSet } from \"../skills\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { ToolMiddleware } from \"../tool/middleware\";\nimport type { AnyTool } from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport {\n Agent,\n type AgentEventStore,\n type AgentEventStoreOptions,\n type AgentEventStoreRegistration,\n type DynamicContextOptions,\n type DynamicContextRegistration,\n type DynamicToolOptions,\n type DynamicToolRegistration,\n} from \"./agent\";\nimport type { PromptHook } from \"./hooks\";\n\nexport class AgentBuilder<M extends CompletionModel = CompletionModel> {\n private readonly agentId: string;\n private agentName: string | undefined;\n private agentDescription: string | undefined;\n private instructionBlocks: string[] = [];\n private contextDocs: Document[] = [];\n private temp: number | undefined;\n private maxTokenCount: number | undefined;\n private params: JsonValue | undefined;\n private choice: ToolChoice | undefined;\n private turns: number | undefined;\n private requestHook: PromptHook | undefined;\n private schema: JsonObject | undefined;\n private skillInstructionBlocks: string[] = [];\n private observerRegistrations: AgentObserverRegistration[] = [];\n private dynamicContextRegistrations: DynamicContextRegistration[] = [];\n private dynamicToolRegistrations: DynamicToolRegistration[] = [];\n private middlewareRegistrations: ToolMiddleware[] = [];\n private memoryRegistration: MemoryRegistration | undefined;\n private eventStoreRegistration: AgentEventStoreRegistration | undefined;\n private activeToolSet = new ToolSet();\n\n constructor(\n agentId: string,\n private readonly completionModel: M,\n ) {\n this.agentId = normalizeAgentId(agentId);\n }\n\n name(name: string): this {\n this.agentName = name;\n return this;\n }\n\n description(description: string): this {\n this.agentDescription = description;\n return this;\n }\n\n instructions(instructions: string): this {\n if (instructions.length > 0) {\n this.instructionBlocks.push(instructions);\n }\n return this;\n }\n\n context(text: string, id = `static_doc_${this.contextDocs.length}`): this {\n this.contextDocs.push({ id, text });\n return this;\n }\n\n dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this {\n this.dynamicContextRegistrations.push({ index, options } as DynamicContextRegistration);\n return this;\n }\n\n dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this {\n this.dynamicToolRegistrations.push({ index, options });\n return this;\n }\n\n tool(tool: AnyTool): this {\n this.activeToolSet.addTool(tool);\n return this;\n }\n\n tools(tools: AnyTool[]): this {\n this.activeToolSet.addTools(tools);\n return this;\n }\n\n mcp(servers: McpServer[]): this {\n for (const server of servers) {\n this.activeToolSet.addTools(server.tools);\n }\n return this;\n }\n\n skills(skillSet: SkillSet): this {\n if (skillSet.instructions.length > 0) {\n this.skillInstructionBlocks.push(skillSet.instructions);\n }\n this.activeToolSet.addTools(skillSet.tools);\n return this;\n }\n\n useToolSet(toolSet: ToolSet): this {\n toolSet.addTools(this.activeToolSet);\n this.activeToolSet = toolSet;\n return this;\n }\n\n temperature(temperature: number): this {\n this.temp = temperature;\n return this;\n }\n\n maxTokens(maxTokens: number): this {\n this.maxTokenCount = maxTokens;\n return this;\n }\n\n additionalParams(params: JsonValue): this {\n this.params = params;\n return this;\n }\n\n toolChoice(toolChoice: ToolChoice): this {\n this.choice = toolChoice;\n return this;\n }\n\n defaultMaxTurns(defaultMaxTurns: number): this {\n this.turns = defaultMaxTurns;\n return this;\n }\n\n hook(hook: PromptHook): this {\n this.requestHook = hook;\n return this;\n }\n\n toolMiddleware(middleware: ToolMiddleware): this {\n this.middlewareRegistrations.push(middleware);\n return this;\n }\n\n toolMiddlewares(middlewares: ToolMiddleware[]): this {\n this.middlewareRegistrations.push(...middlewares);\n return this;\n }\n\n observe(observer: AgentObserver, options: ObserveOptions = {}): this {\n this.observerRegistrations.push({\n observer,\n failOnObserverError: options.failOnObserverError,\n });\n return this;\n }\n\n memory(store: MemoryStore, options: MemoryOptions = {}): this {\n this.memoryRegistration = {\n store,\n options: resolveMemoryOptions(options),\n };\n return this;\n }\n\n eventStore(store: AgentEventStore, options: AgentEventStoreOptions = {}): this {\n this.eventStoreRegistration = {\n store,\n options: {\n include: options.include ?? \"all\",\n },\n };\n return this;\n }\n\n outputSchema(schema: ZodSchema): this {\n this.schema = toProviderJsonSchema(schema);\n return this;\n }\n\n build(): Agent<M> {\n return new Agent({\n id: this.agentId,\n name: this.agentName,\n description: this.agentDescription,\n model: this.completionModel,\n instructions: this.buildInstructions(),\n staticContext: this.contextDocs,\n temperature: this.temp,\n maxTokens: this.maxTokenCount,\n additionalParams: this.params,\n toolSet: this.activeToolSet,\n toolChoice: this.choice,\n defaultMaxTurns: this.turns,\n hook: this.requestHook,\n outputSchema: this.schema,\n observers: this.observerRegistrations,\n dynamicContexts: this.dynamicContextRegistrations,\n dynamicTools: this.dynamicToolRegistrations,\n toolMiddlewares: this.middlewareRegistrations,\n memory: this.memoryRegistration,\n eventStore: this.eventStoreRegistration,\n });\n }\n\n private buildInstructions(): string | undefined {\n const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(\n (part): part is string => part !== undefined && part.length > 0,\n );\n return parts.length === 0 ? undefined : parts.join(\"\\n\\n\");\n }\n}\n\nfunction normalizeAgentId(id: string): string {\n if (typeof id !== \"string\") {\n throw new TypeError(\"Agent id must be a string.\");\n }\n\n const normalized = id.trim();\n if (normalized.length === 0) {\n throw new TypeError(\"Agent id must be a non-empty string.\");\n }\n\n return normalized;\n}\n"],"mappings":";;;;;;;;;;;;;;AA4BO,IAAM,eAAN,MAAgE;AAAA,EAsBrE,YACE,SACiB,iBACjB;AADiB;AAEjB,SAAK,UAAU,iBAAiB,OAAO;AAAA,EACzC;AAAA,EAHmB;AAAA,EAvBF;AAAA,EACT;AAAA,EACA;AAAA,EACA,oBAA8B,CAAC;AAAA,EAC/B,cAA0B,CAAC;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,yBAAmC,CAAC;AAAA,EACpC,wBAAqD,CAAC;AAAA,EACtD,8BAA4D,CAAC;AAAA,EAC7D,2BAAsD,CAAC;AAAA,EACvD,0BAA4C,CAAC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA,gBAAgB,IAAI,QAAQ;AAAA,EASpC,KAAK,MAAoB;AACvB,SAAK,YAAY;AACjB,WAAO;AAAA,EACT;AAAA,EAEA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,kBAAkB,KAAK;AAAA,MACvB,SAAS,KAAK;AAAA,MACd,YAAY,KAAK;AAAA,MACjB,iBAAiB,KAAK;AAAA,MACtB,MAAM,KAAK;AAAA,MACX,cAAc,KAAK;AAAA,MACnB,WAAW,KAAK;AAAA,MAChB,iBAAiB,KAAK;AAAA,MACtB,cAAc,KAAK;AAAA,MACnB,iBAAiB,KAAK;AAAA,MACtB,QAAQ,KAAK;AAAA,MACb,YAAY,KAAK;AAAA,IACnB,CAAC;AAAA,EACH;AAAA,EAEQ,oBAAwC;AAC9C,UAAM,QAAQ,CAAC,GAAG,KAAK,mBAAmB,GAAG,KAAK,sBAAsB,EAAE;AAAA,MACxE,CAAC,SAAyB,SAAS,UAAa,KAAK,SAAS;AAAA,IAChE;AACA,WAAO,MAAM,WAAW,IAAI,SAAY,MAAM,KAAK,MAAM;AAAA,EAC3D;AACF;AAEA,SAAS,iBAAiB,IAAoB;AAC5C,MAAI,OAAO,OAAO,UAAU;AAC1B,UAAM,IAAI,UAAU,4BAA4B;AAAA,EAClD;AAEA,QAAM,aAAa,GAAG,KAAK;AAC3B,MAAI,WAAW,WAAW,GAAG;AAC3B,UAAM,IAAI,UAAU,sCAAsC;AAAA,EAC5D;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AgentBuilder
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-6U6PZ5MD.js";
|
|
4
4
|
import {
|
|
5
5
|
createTool
|
|
6
6
|
} from "./chunk-65QV627O.js";
|
|
@@ -139,4 +139,4 @@ export {
|
|
|
139
139
|
Extractor,
|
|
140
140
|
ExtractorBuilder
|
|
141
141
|
};
|
|
142
|
-
//# sourceMappingURL=chunk-
|
|
142
|
+
//# sourceMappingURL=chunk-PP4VIN3Y.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/extractor/extractor.ts"],"sourcesContent":["import type { Agent } from \"../agent/agent\";\nimport { AgentBuilder } from \"../agent/builder\";\nimport {\n CompletionCapabilityError,\n type CompletionModel,\n CompletionRequestBuilder,\n type CompletionResponse,\n type JsonValue,\n type Message,\n Message as MessageFactory,\n type ToolChoice,\n Usage,\n} from \"../completion/index\";\nimport type { ZodSchema } from \"../schema/zod-schema\";\nimport { createTool } from \"../tool/index\";\n\nconst SUBMIT_TOOL_NAME = \"submit\";\n\nconst DEFAULT_EXTRACTOR_INSTRUCTIONS =\n \"You are an AI assistant whose purpose is to extract structured data from the provided text.\\n\" +\n \"You have access to a `submit` function that defines the structure of the data to extract.\\n\" +\n \"Always call the `submit` function with the structured data. Use default or null values when information is missing.\";\n\nexport type ExtractionResponse<T> = {\n data: T;\n usage: Usage;\n messages: Message[];\n};\n\nexport class ExtractionError extends Error {\n constructor(\n message: string,\n readonly cause?: unknown,\n ) {\n super(message);\n this.name = \"ExtractionError\";\n }\n}\n\nexport class Extractor<T, M extends CompletionModel = CompletionModel> {\n constructor(\n private readonly agent: Agent<M>,\n private readonly schema: ZodSchema<T>,\n private readonly retryCount: number,\n ) {}\n\n async extract(text: string | Message): Promise<T> {\n return (await this.extractWithUsage(text)).data;\n }\n\n async extractWithUsage(text: string | Message): Promise<ExtractionResponse<T>> {\n return this.run(text);\n }\n\n async extractWithHistory(text: string | Message, history: Message[]): Promise<T> {\n return (await this.run(text, history)).data;\n }\n\n getInner(): Agent<M> {\n return this.agent;\n }\n\n private async run(text: string | Message, history?: Message[]): Promise<ExtractionResponse<T>> {\n let usage = Usage.empty();\n let lastError: unknown;\n const prompt = typeof text === \"string\" ? MessageFactory.user(text) : text;\n\n for (let attempt = 0; attempt <= this.retryCount; attempt += 1) {\n try {\n const toolDefs = await this.agent.toolSet.getToolDefinitions(extractRagText(prompt));\n const response = await new CompletionRequestBuilder(this.agent.model, prompt)\n .instructions(this.agent.instructions)\n .messages(history ?? [])\n .documents(this.agent.staticContext)\n .tools(toolDefs)\n .temperature(this.agent.temperature)\n .maxTokens(this.agent.maxTokens)\n .additionalParams(this.agent.additionalParams)\n .toolChoice(this.agent.toolChoice)\n .send();\n usage = Usage.add(usage, response.usage);\n const data = extractSubmittedData(response, this.schema);\n return {\n data,\n usage,\n messages: [\n ...(history ?? []),\n prompt,\n MessageFactory.assistant(response.choice, response.messageId),\n ],\n };\n } catch (error) {\n if (error instanceof CompletionCapabilityError) {\n throw error;\n }\n lastError = error;\n }\n }\n\n throw new ExtractionError(\"No data extracted\", lastError);\n }\n}\n\nexport class ExtractorBuilder<T, M extends CompletionModel = CompletionModel> {\n private readonly agentBuilder: AgentBuilder<M>;\n private retryCount = 0;\n\n constructor(\n model: M,\n private readonly schema: ZodSchema<T>,\n ) {\n this.agentBuilder = new AgentBuilder(\"extractor\", model)\n .instructions(DEFAULT_EXTRACTOR_INSTRUCTIONS)\n .tool(\n createTool({\n name: SUBMIT_TOOL_NAME,\n description: \"Submit the structured data extracted from the provided text.\",\n input: schema,\n output: schema,\n execute: (args) => args,\n }),\n )\n .toolChoice(\"required\");\n }\n\n instructions(instructions: string): this {\n this.agentBuilder.instructions(instructions);\n return this;\n }\n\n context(text: string, id?: string): this {\n this.agentBuilder.context(text, id);\n return this;\n }\n\n temperature(temperature: number): this {\n this.agentBuilder.temperature(temperature);\n return this;\n }\n\n maxTokens(maxTokens: number): this {\n this.agentBuilder.maxTokens(maxTokens);\n return this;\n }\n\n additionalParams(params: JsonValue): this {\n this.agentBuilder.additionalParams(params);\n return this;\n }\n\n toolChoice(toolChoice: ToolChoice): this {\n this.agentBuilder.toolChoice(toolChoice);\n return this;\n }\n\n retries(retries: number): this {\n this.retryCount = Math.max(0, Math.trunc(retries));\n return this;\n }\n\n build(): Extractor<T, M> {\n return new Extractor(this.agentBuilder.build(), this.schema, this.retryCount);\n }\n}\n\nfunction extractSubmittedData<T>(response: CompletionResponse, schema: ZodSchema<T>): T {\n const submitted = response.choice\n .filter((content) => content.type === \"tool_call\")\n .filter((toolCall) => toolCall.function.name === SUBMIT_TOOL_NAME)\n .at(-1);\n\n if (submitted === undefined) {\n throw new ExtractionError(\"The model did not call the submit tool\");\n }\n\n return schema.parse(submitted.function.arguments);\n}\n\nfunction extractRagText(message: Message): string | undefined {\n if (message.role === \"user\") {\n return message.content.flatMap((item) => (item.type === \"text\" ? [item.text] : [])).join(\"\\n\");\n }\n\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,IAAM,mBAAmB;AAEzB,IAAM,iCACJ;AAUK,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC,YACE,SACS,OACT;AACA,UAAM,OAAO;AAFJ;AAGT,SAAK,OAAO;AAAA,EACd;AAAA,EAJW;AAKb;AAEO,IAAM,YAAN,MAAgE;AAAA,EACrE,YACmB,OACA,QACA,YACjB;AAHiB;AACA;AACA;AAAA,EAChB;AAAA,EAHgB;AAAA,EACA;AAAA,EACA;AAAA,EAGnB,MAAM,QAAQ,MAAoC;AAChD,YAAQ,MAAM,KAAK,iBAAiB,IAAI,GAAG;AAAA,EAC7C;AAAA,EAEA,MAAM,iBAAiB,MAAwD;AAC7E,WAAO,KAAK,IAAI,IAAI;AAAA,EACtB;AAAA,EAEA,MAAM,mBAAmB,MAAwB,SAAgC;AAC/E,YAAQ,MAAM,KAAK,IAAI,MAAM,OAAO,GAAG;AAAA,EACzC;AAAA,EAEA,WAAqB;AACnB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAc,IAAI,MAAwB,SAAqD;AAC7F,QAAI,QAAQ,MAAM,MAAM;AACxB,QAAI;AACJ,UAAM,SAAS,OAAO,SAAS,WAAW,QAAe,KAAK,IAAI,IAAI;AAEtE,aAAS,UAAU,GAAG,WAAW,KAAK,YAAY,WAAW,GAAG;AAC9D,UAAI;AACF,cAAM,WAAW,MAAM,KAAK,MAAM,QAAQ,mBAAmB,eAAe,MAAM,CAAC;AACnF,cAAM,WAAW,MAAM,IAAI,yBAAyB,KAAK,MAAM,OAAO,MAAM,EACzE,aAAa,KAAK,MAAM,YAAY,EACpC,SAAS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