@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/dist/agent/index.js
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
Agent,
|
|
3
|
+
AgentRunBlockedError,
|
|
4
|
+
AgentRunCancelledError,
|
|
5
|
+
MaxTurnsError,
|
|
6
|
+
createVectorContext,
|
|
7
|
+
isVectorContext
|
|
8
|
+
} from "../chunk-DBSCKTUL.js";
|
|
6
9
|
import "../chunk-YK4WAAS4.js";
|
|
7
|
-
import "../chunk-
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
13
|
-
import "../chunk-
|
|
14
|
-
import "../chunk-
|
|
15
|
-
import "../chunk-
|
|
16
|
-
import "../chunk-
|
|
17
|
-
import "../chunk-
|
|
18
|
-
import "../chunk-
|
|
19
|
-
import "../chunk-CWUJUSOS.js";
|
|
10
|
+
import "../chunk-EINTGEGY.js";
|
|
11
|
+
import "../chunk-CQNNSZPG.js";
|
|
12
|
+
import "../chunk-D37WMSP6.js";
|
|
13
|
+
import "../chunk-77AIYARB.js";
|
|
14
|
+
import "../chunk-OJH6HWHX.js";
|
|
15
|
+
import "../chunk-CWM7B2OO.js";
|
|
16
|
+
import "../chunk-FMHLROPQ.js";
|
|
17
|
+
import "../chunk-MGNTXGTX.js";
|
|
18
|
+
import "../chunk-KR3RCCHJ.js";
|
|
19
|
+
import "../chunk-TIZYTPJO.js";
|
|
20
|
+
import "../chunk-KIDGIJM7.js";
|
|
21
|
+
import "../chunk-FN7HLLAZ.js";
|
|
20
22
|
export {
|
|
21
|
-
|
|
23
|
+
Agent,
|
|
24
|
+
AgentRunBlockedError,
|
|
25
|
+
AgentRunCancelledError,
|
|
26
|
+
MaxTurnsError,
|
|
27
|
+
createVectorContext,
|
|
28
|
+
isVectorContext
|
|
22
29
|
};
|
|
23
30
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { b as RetrySetting } from './retry-CjvSlKGW.js';
|
|
2
|
+
import { D as Document, C as CompletionModel, P as ProviderTool, J as JsonObject, u as ToolChoice } from './types-CQ_ioXWK.js';
|
|
3
|
+
import { Z as ZodSchema } from './zod-schema-C7F4clpm.js';
|
|
4
|
+
import { GuardrailPolicyInput, GuardrailPolicy } from './guardrails/index.js';
|
|
5
|
+
import { a as McpServer } from './client-B8DT1vUG.js';
|
|
6
|
+
import { j as AgentLifecycle, G as AgentObservabilityOptions, o as AgentRunOptions, n as AgentResult, t as AgentStream, u as AgentStreamEvent, b as AgentApprovalRequiredResult, A as AgentApprovalDecision, a as AgentApprovalRequiredEvent } from './types-3x3mOLHU.js';
|
|
7
|
+
import { A as AgentMiddleware } from './middleware-C7PdmKF7.js';
|
|
8
|
+
import { A as AnyTool, T as Tool, c as ToolCallContext, N as NormalizedToolOutput } from './tool--Mz4v1eL.js';
|
|
9
|
+
import { D as MemoryStore, y as MemorySavePolicy, m as MemoryCompactor, d as MemoryCompactionConflictRetryOptions, x as MemoryOptions } from './types-BVzM4RsC.js';
|
|
10
|
+
import { a as SkillSet } from './types-DiJqJejp.js';
|
|
11
|
+
import { T as ToolIndex } from './dynamic-tools-KwEGRbHp.js';
|
|
12
|
+
import { E as EmbeddingModel, c as SparseEmbeddingModel } from './types-Cr4uiYo5.js';
|
|
13
|
+
import { V as VectorFilter, a as VectorSearchResult, d as VectorStore, H as HybridVectorStore, e as VectorFusion } from './types-Dld1TpWj.js';
|
|
14
|
+
|
|
15
|
+
type VectorContextBaseOptions<T = unknown> = {
|
|
16
|
+
topK: number;
|
|
17
|
+
minScore?: number | undefined;
|
|
18
|
+
filter?: VectorFilter | undefined;
|
|
19
|
+
retries?: RetrySetting | undefined;
|
|
20
|
+
format?(result: VectorSearchResult<T>): Document;
|
|
21
|
+
};
|
|
22
|
+
type CreateVectorContextOptions<T = unknown> = VectorContextBaseOptions<T> & {
|
|
23
|
+
store: VectorStore<T>;
|
|
24
|
+
model: EmbeddingModel;
|
|
25
|
+
models?: never;
|
|
26
|
+
};
|
|
27
|
+
type CreateHybridVectorContextOptions<T = unknown> = VectorContextBaseOptions<T> & {
|
|
28
|
+
store: HybridVectorStore<T>;
|
|
29
|
+
model?: never;
|
|
30
|
+
models: {
|
|
31
|
+
dense: EmbeddingModel;
|
|
32
|
+
sparse: SparseEmbeddingModel;
|
|
33
|
+
};
|
|
34
|
+
fusion?: VectorFusion | undefined;
|
|
35
|
+
};
|
|
36
|
+
type VectorContext<T = unknown> = (CreateVectorContextOptions<T> | CreateHybridVectorContextOptions<T>) & {
|
|
37
|
+
readonly kind: "vector-context";
|
|
38
|
+
};
|
|
39
|
+
declare function createVectorContext<T>(options: CreateVectorContextOptions<T>): VectorContext<T>;
|
|
40
|
+
declare function createVectorContext<T>(options: CreateHybridVectorContextOptions<T>): VectorContext<T>;
|
|
41
|
+
declare function isVectorContext(value: unknown): value is VectorContext;
|
|
42
|
+
|
|
43
|
+
type AgentToolInput = AnyTool | ProviderTool | ToolIndex;
|
|
44
|
+
type AgentContextInput<T = unknown> = Document | VectorContext<T>;
|
|
45
|
+
type RawResponseOf$1<Model> = Model extends CompletionModel<infer RawResponse> ? RawResponse : unknown;
|
|
46
|
+
type AgentOptions<Output = string, M extends CompletionModel = CompletionModel, ContextDocument = unknown> = {
|
|
47
|
+
id: string;
|
|
48
|
+
name?: string | undefined;
|
|
49
|
+
description?: string | undefined;
|
|
50
|
+
model: M;
|
|
51
|
+
instructions?: string | undefined;
|
|
52
|
+
context?: readonly AgentContextInput<ContextDocument>[] | undefined;
|
|
53
|
+
tools?: readonly AgentToolInput[] | undefined;
|
|
54
|
+
mcpServers?: readonly McpServer[] | undefined;
|
|
55
|
+
skills?: SkillSet | undefined;
|
|
56
|
+
temperature?: number | undefined;
|
|
57
|
+
maxTokens?: number | undefined;
|
|
58
|
+
providerOptions?: JsonObject | undefined;
|
|
59
|
+
retries?: RetrySetting | undefined;
|
|
60
|
+
toolChoice?: ToolChoice | undefined;
|
|
61
|
+
maxTurns?: number | undefined;
|
|
62
|
+
lifecycle?: AgentLifecycle<Output, RawResponseOf$1<M>> | undefined;
|
|
63
|
+
outputSchema?: ZodSchema<Output> | undefined;
|
|
64
|
+
observability?: AgentObservabilityOptions | undefined;
|
|
65
|
+
guardrails?: GuardrailPolicyInput | undefined;
|
|
66
|
+
middlewares?: readonly AgentMiddleware[] | undefined;
|
|
67
|
+
memory?: AgentMemoryOptions | undefined;
|
|
68
|
+
};
|
|
69
|
+
type AgentMemoryOptions = MemoryOptions & {
|
|
70
|
+
store: MemoryStore;
|
|
71
|
+
};
|
|
72
|
+
type AgentMemory = {
|
|
73
|
+
store: MemoryStore;
|
|
74
|
+
savePolicy: MemorySavePolicy;
|
|
75
|
+
compaction?: {
|
|
76
|
+
trigger: {
|
|
77
|
+
afterMessages: number;
|
|
78
|
+
};
|
|
79
|
+
retention: {
|
|
80
|
+
recentUserTurns: number;
|
|
81
|
+
};
|
|
82
|
+
compactor: MemoryCompactor;
|
|
83
|
+
conflictRetries: false | MemoryCompactionConflictRetryOptions;
|
|
84
|
+
} | undefined;
|
|
85
|
+
};
|
|
86
|
+
type ResolvedAgentOptions<Output = string, M extends CompletionModel = CompletionModel, ContextDocument = unknown> = {
|
|
87
|
+
id: string;
|
|
88
|
+
name?: string | undefined;
|
|
89
|
+
description?: string | undefined;
|
|
90
|
+
model: M;
|
|
91
|
+
instructions?: string | undefined;
|
|
92
|
+
context?: readonly AgentContextInput<ContextDocument>[] | undefined;
|
|
93
|
+
temperature?: number | undefined;
|
|
94
|
+
maxTokens?: number | undefined;
|
|
95
|
+
providerOptions?: JsonObject | undefined;
|
|
96
|
+
retries?: RetrySetting | undefined;
|
|
97
|
+
tools?: readonly AnyTool[] | undefined;
|
|
98
|
+
mcpServers?: readonly McpServer[] | undefined;
|
|
99
|
+
providerTools?: readonly ProviderTool[] | undefined;
|
|
100
|
+
toolIndexes?: readonly ToolIndex[] | undefined;
|
|
101
|
+
toolChoice?: ToolChoice | undefined;
|
|
102
|
+
defaultMaxTurns?: number | undefined;
|
|
103
|
+
lifecycle?: AgentLifecycle<Output, RawResponseOf$1<M>> | undefined;
|
|
104
|
+
outputSchema?: ZodSchema<Output> | undefined;
|
|
105
|
+
observability?: AgentObservabilityOptions | undefined;
|
|
106
|
+
guardrails?: readonly GuardrailPolicy[] | undefined;
|
|
107
|
+
middlewares?: readonly AgentMiddleware[] | undefined;
|
|
108
|
+
memory?: AgentMemory | undefined;
|
|
109
|
+
};
|
|
110
|
+
type AgentToolOptions = {
|
|
111
|
+
name: string;
|
|
112
|
+
description?: string | undefined;
|
|
113
|
+
maxTurns?: number | undefined;
|
|
114
|
+
stream?: boolean | undefined;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
type RawResponseOf<Model> = Model extends CompletionModel<infer RawResponse> ? RawResponse : unknown;
|
|
118
|
+
declare class Agent<Output = string, M extends CompletionModel = CompletionModel, ContextDocument = unknown> {
|
|
119
|
+
readonly id: string;
|
|
120
|
+
readonly name: string | undefined;
|
|
121
|
+
readonly description: string | undefined;
|
|
122
|
+
readonly model: M;
|
|
123
|
+
readonly instructions: string | undefined;
|
|
124
|
+
readonly context: readonly AgentContextInput<ContextDocument>[];
|
|
125
|
+
readonly temperature: number | undefined;
|
|
126
|
+
readonly maxTokens: number | undefined;
|
|
127
|
+
readonly providerOptions: JsonObject | undefined;
|
|
128
|
+
readonly retries: RetrySetting | undefined;
|
|
129
|
+
readonly mcpServers: readonly McpServer[];
|
|
130
|
+
readonly tools: readonly AnyTool[];
|
|
131
|
+
readonly toolChoice: ToolChoice | undefined;
|
|
132
|
+
readonly defaultMaxTurns: number | undefined;
|
|
133
|
+
readonly lifecycle: AgentLifecycle<Output, RawResponseOf<M>> | undefined;
|
|
134
|
+
readonly outputSchema: ZodSchema<Output> | undefined;
|
|
135
|
+
readonly observability: AgentObservabilityOptions | undefined;
|
|
136
|
+
readonly guardrails: readonly GuardrailPolicy[];
|
|
137
|
+
readonly middlewares: readonly AgentMiddleware[];
|
|
138
|
+
readonly memory: AgentMemory | undefined;
|
|
139
|
+
constructor(options: AgentOptions<Output, M, ContextDocument>);
|
|
140
|
+
generate(options: AgentRunOptions<Output, RawResponseOf<M>>): Promise<AgentResult<Output>>;
|
|
141
|
+
stream(options: AgentRunOptions<Output, RawResponseOf<M>>): AgentStream<AgentStreamEvent<Output, RawResponseOf<M>>>;
|
|
142
|
+
resume(pending: AgentApprovalRequiredResult, decision: AgentApprovalDecision): Promise<AgentResult<Output>>;
|
|
143
|
+
resume(pending: AgentApprovalRequiredEvent, decision: AgentApprovalDecision): AgentStream<AgentStreamEvent<Output, RawResponseOf<M>>>;
|
|
144
|
+
asTool(options: AgentToolOptions): Tool<{
|
|
145
|
+
prompt: string;
|
|
146
|
+
}, Output>;
|
|
147
|
+
getTool(toolName: string): AnyTool | undefined;
|
|
148
|
+
callTool(toolName: string, args: string, context?: ToolCallContext): Promise<NormalizedToolOutput>;
|
|
149
|
+
}
|
|
150
|
+
declare function createResolvedAgent<Output = string, M extends CompletionModel = CompletionModel, ContextDocument = unknown>(options: ResolvedAgentOptions<Output, M, ContextDocument>): Agent<Output, M, ContextDocument>;
|
|
151
|
+
declare function getResolvedAgentOptions<Output, M extends CompletionModel, ContextDocument>(agent: Agent<Output, M, ContextDocument>): ResolvedAgentOptions<Output, M, ContextDocument>;
|
|
152
|
+
|
|
153
|
+
export { Agent as A, type CreateHybridVectorContextOptions as C, type ResolvedAgentOptions as R, type VectorContext as V, type AgentContextInput as a, type AgentMemory as b, type AgentMemoryOptions as c, type AgentOptions as d, type AgentToolInput as e, type AgentToolOptions as f, type CreateVectorContextOptions as g, type VectorContextBaseOptions as h, createVectorContext as i, isVectorContext as j, createResolvedAgent as k, getResolvedAgentOptions as l };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CompletionCapabilityError,
|
|
3
|
+
Usage,
|
|
4
|
+
generateCompletion
|
|
5
|
+
} from "./chunk-KR3RCCHJ.js";
|
|
6
|
+
import {
|
|
7
|
+
toProviderJsonSchema
|
|
8
|
+
} from "./chunk-TIZYTPJO.js";
|
|
9
|
+
import {
|
|
10
|
+
isAbortError,
|
|
11
|
+
resolveRetryOptions,
|
|
12
|
+
retryDelayMs,
|
|
13
|
+
retryOptionsForFailure,
|
|
14
|
+
waitForRetry
|
|
15
|
+
} from "./chunk-FN7HLLAZ.js";
|
|
16
|
+
|
|
17
|
+
// src/extractor/extractor.ts
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
var SUBMIT_TOOL_NAME = "submit";
|
|
20
|
+
var DEFAULT_EXTRACTOR_INSTRUCTIONS = "You are an AI assistant whose purpose is to extract structured data from the provided text.\nYou have access to a `submit` function that defines the structure of the data to extract.\nAlways call the `submit` function with the structured data. Use default or null values when information is missing.";
|
|
21
|
+
var ExtractionError = class extends Error {
|
|
22
|
+
attempts;
|
|
23
|
+
usage;
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
|
|
26
|
+
this.name = "ExtractionError";
|
|
27
|
+
this.attempts = options.attempts;
|
|
28
|
+
this.usage = options.usage;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
async function extract(options) {
|
|
32
|
+
if (!(options.outputSchema instanceof z.ZodType)) {
|
|
33
|
+
throw new TypeError("extract outputSchema must be a Zod schema.");
|
|
34
|
+
}
|
|
35
|
+
if (typeof options.text !== "string") {
|
|
36
|
+
throw new TypeError("extract text must be a string.");
|
|
37
|
+
}
|
|
38
|
+
const retries = resolveExtractionRetries(options.retries);
|
|
39
|
+
const maxAttempts = retries?.maxAttempts ?? 1;
|
|
40
|
+
const submitTool = createSubmitTool(options.outputSchema);
|
|
41
|
+
const instructions = extractionInstructions(options.instructions);
|
|
42
|
+
let usage = Usage.empty();
|
|
43
|
+
let lastError;
|
|
44
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
45
|
+
try {
|
|
46
|
+
const result = await generateCompletion({
|
|
47
|
+
model: options.model,
|
|
48
|
+
prompt: options.text,
|
|
49
|
+
instructions,
|
|
50
|
+
tools: [globalThis.structuredClone(submitTool)],
|
|
51
|
+
temperature: options.temperature,
|
|
52
|
+
maxTokens: options.maxTokens,
|
|
53
|
+
providerOptions: options.providerOptions,
|
|
54
|
+
toolChoice: "required",
|
|
55
|
+
retries: false,
|
|
56
|
+
abortSignal: options.abortSignal
|
|
57
|
+
});
|
|
58
|
+
usage = Usage.add(usage, result.usage);
|
|
59
|
+
return {
|
|
60
|
+
...result,
|
|
61
|
+
output: extractSubmittedData(result.content, options.outputSchema),
|
|
62
|
+
usage
|
|
63
|
+
};
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error instanceof CompletionCapabilityError || isAbortError(error)) {
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
lastError = error;
|
|
69
|
+
const retryOptions = retryOptionsForFailure(retries, {
|
|
70
|
+
error,
|
|
71
|
+
attempt,
|
|
72
|
+
streaming: false
|
|
73
|
+
});
|
|
74
|
+
if (retryOptions === void 0) {
|
|
75
|
+
throw new ExtractionError("No data extracted", {
|
|
76
|
+
cause: lastError,
|
|
77
|
+
attempts: attempt,
|
|
78
|
+
usage
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
await waitForRetry(retryDelayMs(retryOptions, attempt), options.abortSignal);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
throw new ExtractionError("No data extracted", {
|
|
85
|
+
cause: lastError,
|
|
86
|
+
attempts: maxAttempts,
|
|
87
|
+
usage
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function extractionInstructions(instructions) {
|
|
91
|
+
return instructions === void 0 || instructions.length === 0 ? DEFAULT_EXTRACTOR_INSTRUCTIONS : `${DEFAULT_EXTRACTOR_INSTRUCTIONS}
|
|
92
|
+
|
|
93
|
+
${instructions}`;
|
|
94
|
+
}
|
|
95
|
+
function createSubmitTool(outputSchema) {
|
|
96
|
+
return {
|
|
97
|
+
name: SUBMIT_TOOL_NAME,
|
|
98
|
+
description: "Submit the structured data extracted from the provided text.",
|
|
99
|
+
parameters: toProviderJsonSchema(outputSchema, { io: "input" })
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function resolveExtractionRetries(options) {
|
|
103
|
+
if (options === void 0 || options === false) {
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
return resolveRetryOptions({
|
|
107
|
+
...options,
|
|
108
|
+
shouldRetry: options.shouldRetry ?? (() => true)
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function extractSubmittedData(content, schema) {
|
|
112
|
+
const submitted = content.filter((item) => item.type === "tool-call").filter((toolCall) => toolCall.toolName === SUBMIT_TOOL_NAME).at(-1);
|
|
113
|
+
if (submitted === void 0) {
|
|
114
|
+
throw new Error("The model did not call the submit tool");
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
return schema.parse(submitted.input);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw new Error("Submitted data failed output schema validation", { cause: error });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export {
|
|
124
|
+
ExtractionError,
|
|
125
|
+
extract
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=chunk-4WI3VGZK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/extractor/extractor.ts"],"sourcesContent":["import { z } from \"zod\";\nimport {\n type AssistantContentPart,\n CompletionCapabilityError,\n type CompletionModel,\n type CompletionResult,\n generateCompletion,\n type JsonObject,\n type ToolDefinition,\n Usage,\n} from \"../completion/index\";\nimport { isAbortError } from \"../internal/abort\";\nimport {\n type ResolvedRetryOptions,\n type RetrySetting,\n resolveRetryOptions,\n retryDelayMs,\n retryOptionsForFailure,\n waitForRetry,\n} from \"../retry\";\nimport { toProviderJsonSchema, type ZodSchema } from \"../schema/zod-schema\";\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\ntype RawResponseOf<Model> =\n Model extends CompletionModel<infer RawResponse> ? RawResponse : unknown;\n\nexport type ExtractOptions<Output, Model extends CompletionModel = CompletionModel> = {\n model: Model;\n text: string;\n outputSchema: ZodSchema<Output>;\n instructions?: string | undefined;\n retries?: RetrySetting | undefined;\n temperature?: number | undefined;\n maxTokens?: number | undefined;\n providerOptions?: JsonObject | undefined;\n abortSignal?: AbortSignal | undefined;\n};\n\nexport type ExtractionResult<Output, RawResponse = unknown> = Omit<\n CompletionResult<string, RawResponse>,\n \"output\"\n> & {\n output: Output;\n};\n\nexport class ExtractionError extends Error {\n readonly attempts: number;\n readonly usage: Usage;\n\n constructor(\n message: string,\n options: {\n cause?: unknown;\n attempts: number;\n usage: Usage;\n },\n ) {\n super(message, options.cause === undefined ? undefined : { cause: options.cause });\n this.name = \"ExtractionError\";\n this.attempts = options.attempts;\n this.usage = options.usage;\n }\n}\n\n/** Extract structured data from text using a required, schema-backed submit tool. */\nexport async function extract<Output, Model extends CompletionModel>(\n options: ExtractOptions<Output, Model>,\n): Promise<ExtractionResult<Output, RawResponseOf<Model>>> {\n if (!(options.outputSchema instanceof z.ZodType)) {\n throw new TypeError(\"extract outputSchema must be a Zod schema.\");\n }\n if (typeof options.text !== \"string\") {\n throw new TypeError(\"extract text must be a string.\");\n }\n\n const retries = resolveExtractionRetries(options.retries);\n const maxAttempts = retries?.maxAttempts ?? 1;\n const submitTool = createSubmitTool(options.outputSchema);\n const instructions = extractionInstructions(options.instructions);\n let usage = Usage.empty();\n let lastError: unknown;\n\n for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {\n try {\n const result = await generateCompletion({\n model: options.model,\n prompt: options.text,\n instructions,\n tools: [globalThis.structuredClone(submitTool)],\n temperature: options.temperature,\n maxTokens: options.maxTokens,\n providerOptions: options.providerOptions,\n toolChoice: \"required\",\n retries: false,\n abortSignal: options.abortSignal,\n });\n usage = Usage.add(usage, result.usage);\n return {\n ...result,\n output: extractSubmittedData(result.content, options.outputSchema),\n usage,\n };\n } catch (error) {\n if (error instanceof CompletionCapabilityError || isAbortError(error)) {\n throw error;\n }\n lastError = error;\n const retryOptions = retryOptionsForFailure(retries, {\n error,\n attempt,\n streaming: false,\n });\n if (retryOptions === undefined) {\n throw new ExtractionError(\"No data extracted\", {\n cause: lastError,\n attempts: attempt,\n usage,\n });\n }\n await waitForRetry(retryDelayMs(retryOptions, attempt), options.abortSignal);\n }\n }\n\n throw new ExtractionError(\"No data extracted\", {\n cause: lastError,\n attempts: maxAttempts,\n usage,\n });\n}\n\nfunction extractionInstructions(instructions: string | undefined): string {\n return instructions === undefined || instructions.length === 0\n ? DEFAULT_EXTRACTOR_INSTRUCTIONS\n : `${DEFAULT_EXTRACTOR_INSTRUCTIONS}\\n\\n${instructions}`;\n}\n\nfunction createSubmitTool<Output>(outputSchema: ZodSchema<Output>): ToolDefinition {\n return {\n name: SUBMIT_TOOL_NAME,\n description: \"Submit the structured data extracted from the provided text.\",\n parameters: toProviderJsonSchema(outputSchema, { io: \"input\" }),\n };\n}\n\nfunction resolveExtractionRetries(\n options: RetrySetting | undefined,\n): ResolvedRetryOptions | undefined {\n if (options === undefined || options === false) {\n return undefined;\n }\n return resolveRetryOptions({\n ...options,\n shouldRetry: options.shouldRetry ?? (() => true),\n });\n}\n\nfunction extractSubmittedData<Output>(\n content: readonly AssistantContentPart[],\n schema: ZodSchema<Output>,\n): Output {\n const submitted = content\n .filter((item) => item.type === \"tool-call\")\n .filter((toolCall) => toolCall.toolName === SUBMIT_TOOL_NAME)\n .at(-1);\n\n if (submitted === undefined) {\n throw new Error(\"The model did not call the submit tool\");\n }\n\n try {\n return schema.parse(submitted.input);\n } catch (error) {\n throw new Error(\"Submitted data failed output schema validation\", { cause: error });\n }\n}\n"],"mappings":"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","names":[]}
|