@anvia/core 0.1.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 +101 -0
- package/dist/agent/index.d.ts +70 -0
- package/dist/agent/index.js +31 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent-C6h6YrRU.d.ts +218 -0
- package/dist/audio-generation/index.d.ts +32 -0
- package/dist/audio-generation/index.js +9 -0
- package/dist/audio-generation/index.js.map +1 -0
- package/dist/chunk-7QI6ZAFI.js +61 -0
- package/dist/chunk-7QI6ZAFI.js.map +1 -0
- package/dist/chunk-A7VDIZQN.js +145 -0
- package/dist/chunk-A7VDIZQN.js.map +1 -0
- package/dist/chunk-B24Q2ZYM.js +43 -0
- package/dist/chunk-B24Q2ZYM.js.map +1 -0
- package/dist/chunk-B4QHQN5K.js +37 -0
- package/dist/chunk-B4QHQN5K.js.map +1 -0
- package/dist/chunk-CP47FBJV.js +324 -0
- package/dist/chunk-CP47FBJV.js.map +1 -0
- package/dist/chunk-FI2BTRT5.js +86 -0
- package/dist/chunk-FI2BTRT5.js.map +1 -0
- package/dist/chunk-GNWMOSNR.js +113 -0
- package/dist/chunk-GNWMOSNR.js.map +1 -0
- package/dist/chunk-I77SDTFE.js +43 -0
- package/dist/chunk-I77SDTFE.js.map +1 -0
- package/dist/chunk-IA76K5UX.js +142 -0
- package/dist/chunk-IA76K5UX.js.map +1 -0
- package/dist/chunk-IQBY2GCF.js +1207 -0
- package/dist/chunk-IQBY2GCF.js.map +1 -0
- package/dist/chunk-KSIY7KJA.js +387 -0
- package/dist/chunk-KSIY7KJA.js.map +1 -0
- package/dist/chunk-LMBOJMNB.js +304 -0
- package/dist/chunk-LMBOJMNB.js.map +1 -0
- package/dist/chunk-S55WOHX5.js +9 -0
- package/dist/chunk-S55WOHX5.js.map +1 -0
- package/dist/chunk-SRGJPXKT.js +256 -0
- package/dist/chunk-SRGJPXKT.js.map +1 -0
- package/dist/chunk-WZTPK5HV.js +125 -0
- package/dist/chunk-WZTPK5HV.js.map +1 -0
- package/dist/chunk-X6FBOU2P.js +96 -0
- package/dist/chunk-X6FBOU2P.js.map +1 -0
- package/dist/chunk-XUUY2L2D.js +42 -0
- package/dist/chunk-XUUY2L2D.js.map +1 -0
- package/dist/completion/index.d.ts +36 -0
- package/dist/completion/index.js +29 -0
- package/dist/completion/index.js.map +1 -0
- package/dist/embeddings/index.d.ts +34 -0
- package/dist/embeddings/index.js +23 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/evals/index.d.ts +153 -0
- package/dist/evals/index.js +30 -0
- package/dist/evals/index.js.map +1 -0
- package/dist/extractor/index.d.ts +45 -0
- package/dist/extractor/index.js +19 -0
- package/dist/extractor/index.js.map +1 -0
- package/dist/image-generation/index.d.ts +37 -0
- package/dist/image-generation/index.js +9 -0
- package/dist/image-generation/index.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +183 -0
- package/dist/index.js.map +1 -0
- package/dist/loaders/index.d.ts +82 -0
- package/dist/loaders/index.js +287 -0
- package/dist/loaders/index.js.map +1 -0
- package/dist/mcp/index.d.ts +15 -0
- package/dist/mcp/index.js +9 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/observability/index.d.ts +93 -0
- package/dist/observability/index.js +7 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/pipeline/index.d.ts +51 -0
- package/dist/pipeline/index.js +9 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/skills/index.d.ts +12 -0
- package/dist/skills/index.js +16 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/streaming/index.d.ts +6 -0
- package/dist/streaming/index.js +7 -0
- package/dist/streaming/index.js.map +1 -0
- package/dist/tool/index.d.ts +42 -0
- package/dist/tool/index.js +33 -0
- package/dist/tool/index.js.map +1 -0
- package/dist/tool-DhuBQ3yb.d.ts +35 -0
- package/dist/transcription/index.d.ts +35 -0
- package/dist/transcription/index.js +9 -0
- package/dist/transcription/index.js.map +1 -0
- package/dist/types-B5B8Sdl4.d.ts +64 -0
- package/dist/types-BrxLd7ay.d.ts +250 -0
- package/dist/types-HvopERm0.d.ts +30 -0
- package/dist/vector-store/index.d.ts +111 -0
- package/dist/vector-store/index.js +15 -0
- package/dist/vector-store/index.js.map +1 -0
- package/dist/zod-schema-DJTEgQBq.d.ts +40 -0
- package/package.json +102 -0
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# @anvia/core
|
|
2
|
+
|
|
3
|
+
Core runtime primitives for Anvia agents, tools, structured extraction, pipelines, streaming, RAG, MCP, skills, and observability.
|
|
4
|
+
|
|
5
|
+
This package is provider-neutral. Pair it with a provider adapter such as `@anvia/openai`, `@anvia/anthropic`, or `@anvia/gemini` to create runnable models.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @anvia/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In this monorepo, the package is available through the workspace:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm --filter @anvia/core build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
import { AgentBuilder, ExtractorBuilder, PipelineBuilder, createTool } from "@anvia/core";
|
|
24
|
+
import { OpenAIClient } from "@anvia/openai";
|
|
25
|
+
|
|
26
|
+
const client = new OpenAIClient({
|
|
27
|
+
apiKey,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const model = client.completionModel("gpt-5");
|
|
31
|
+
|
|
32
|
+
const lookupOrder = createTool({
|
|
33
|
+
name: "lookup_order",
|
|
34
|
+
description: "Look up an order by id.",
|
|
35
|
+
input: z.object({ orderId: z.string() }),
|
|
36
|
+
execute: async ({ orderId }) => ({ orderId, status: "processing" }),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const agent = new AgentBuilder("support", model)
|
|
40
|
+
.instructions("Help customers with order questions.")
|
|
41
|
+
.tool(lookupOrder)
|
|
42
|
+
.defaultMaxTurns(4)
|
|
43
|
+
.build();
|
|
44
|
+
|
|
45
|
+
const response = await agent.prompt("What is happening with order A123?").send();
|
|
46
|
+
|
|
47
|
+
console.log(response.output);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Structured Extraction
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const ticketSchema = z.object({
|
|
54
|
+
customer: z.string(),
|
|
55
|
+
priority: z.enum(["low", "medium", "high"]),
|
|
56
|
+
summary: z.string(),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const extractor = new ExtractorBuilder(model, ticketSchema).retries(1).build();
|
|
60
|
+
|
|
61
|
+
const ticket = await extractor.extract(
|
|
62
|
+
"Acme Co. reports checkout failures. Priority is high.",
|
|
63
|
+
);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Pipelines
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const pipeline = new PipelineBuilder<string>()
|
|
70
|
+
.step((input) => `Extract this support ticket:\n\n${input}`)
|
|
71
|
+
.prompt(agent)
|
|
72
|
+
.extract(extractor)
|
|
73
|
+
.build();
|
|
74
|
+
|
|
75
|
+
const result = await pipeline.run("Customer cannot complete checkout.");
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Public Areas
|
|
79
|
+
|
|
80
|
+
- `agent`: agent runtime and `AgentBuilder`
|
|
81
|
+
- `tool`: typed tool creation and tool sets
|
|
82
|
+
- `completion`: provider-neutral completion request and response types
|
|
83
|
+
- `extractor`: schema-first structured extraction
|
|
84
|
+
- `pipeline`: typed sequential and parallel workflows
|
|
85
|
+
- `embeddings`: embedding helpers and document embedding utilities
|
|
86
|
+
- `vector-store`: in-memory vector search and vector search tools
|
|
87
|
+
- `streaming`: normalized stream helpers
|
|
88
|
+
- `mcp`: MCP server connection helpers
|
|
89
|
+
- `skills`: local skill loading
|
|
90
|
+
- `observability`: observer interfaces for runs, generations, and tool calls
|
|
91
|
+
- `evals`: evaluation helpers and reporters
|
|
92
|
+
- `loaders`: document loading utilities
|
|
93
|
+
- `audio-generation`, `image-generation`, `transcription`: provider-neutral media interfaces
|
|
94
|
+
|
|
95
|
+
## Development
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
pnpm --filter @anvia/core typecheck
|
|
99
|
+
pnpm --filter @anvia/core test
|
|
100
|
+
pnpm --filter @anvia/core build
|
|
101
|
+
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { e as DynamicContextOptions, g as DynamicToolOptions, P as PromptHook, A as Agent } from '../agent-C6h6YrRU.js';
|
|
2
|
+
export { a as AgentOptions, b as AgentStreamEvent, c as AgentToolOptions, C as CompletionCallHookArgs, d as CompletionResponseHookArgs, D as DEFAULT_MAX_TURNS, f as DynamicContextRegistration, h as DynamicToolRegistration, H as HookAction, i as HookResult, j as PromptRequest, k as PromptResponse, R as RunControl, T as ToolCallControl, l as ToolCallHookAction, m as ToolCallHookArgs, n as ToolCallHookResult, o as ToolHookArgs, p as ToolResultHookArgs, q as cancelPrompt, r as createHook, s as runControl, t as skipTool, u as toolCallControl } from '../agent-C6h6YrRU.js';
|
|
3
|
+
import { b as CompletionModel, j as JsonValue, o as ToolChoice, M as Message } from '../types-BrxLd7ay.js';
|
|
4
|
+
import { c as McpServer } from '../types-B5B8Sdl4.js';
|
|
5
|
+
import { AgentObserver, ObserveOptions } from '../observability/index.js';
|
|
6
|
+
import { T as ToolSearchDocument, a as ToolSet, Z as ZodSchema } from '../zod-schema-DJTEgQBq.js';
|
|
7
|
+
import { b as SkillSet } from '../types-HvopERm0.js';
|
|
8
|
+
import { A as AnyTool } from '../tool-DhuBQ3yb.js';
|
|
9
|
+
import { VectorSearchIndex } from '../vector-store/index.js';
|
|
10
|
+
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
11
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
12
|
+
import '../embeddings/index.js';
|
|
13
|
+
import 'zod';
|
|
14
|
+
|
|
15
|
+
declare class AgentBuilder<M extends CompletionModel = CompletionModel> {
|
|
16
|
+
private readonly completionModel;
|
|
17
|
+
private readonly agentId;
|
|
18
|
+
private agentName;
|
|
19
|
+
private agentDescription;
|
|
20
|
+
private instructionBlocks;
|
|
21
|
+
private contextDocs;
|
|
22
|
+
private temp;
|
|
23
|
+
private maxTokenCount;
|
|
24
|
+
private params;
|
|
25
|
+
private choice;
|
|
26
|
+
private turns;
|
|
27
|
+
private requestHook;
|
|
28
|
+
private schema;
|
|
29
|
+
private skillInstructionBlocks;
|
|
30
|
+
private observerRegistrations;
|
|
31
|
+
private dynamicContextRegistrations;
|
|
32
|
+
private dynamicToolRegistrations;
|
|
33
|
+
private activeToolSet;
|
|
34
|
+
constructor(agentId: string, completionModel: M);
|
|
35
|
+
name(name: string): this;
|
|
36
|
+
description(description: string): this;
|
|
37
|
+
instructions(instructions: string): this;
|
|
38
|
+
context(text: string, id?: string): this;
|
|
39
|
+
dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this;
|
|
40
|
+
dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this;
|
|
41
|
+
tool(tool: AnyTool): this;
|
|
42
|
+
tools(tools: AnyTool[]): this;
|
|
43
|
+
mcp(servers: McpServer[]): this;
|
|
44
|
+
skills(skillSet: SkillSet): this;
|
|
45
|
+
useToolSet(toolSet: ToolSet): this;
|
|
46
|
+
temperature(temperature: number): this;
|
|
47
|
+
maxTokens(maxTokens: number): this;
|
|
48
|
+
additionalParams(params: JsonValue): this;
|
|
49
|
+
toolChoice(toolChoice: ToolChoice): this;
|
|
50
|
+
defaultMaxTurns(defaultMaxTurns: number): this;
|
|
51
|
+
hook(hook: PromptHook): this;
|
|
52
|
+
observe(observer: AgentObserver, options?: ObserveOptions): this;
|
|
53
|
+
outputSchema(schema: ZodSchema): this;
|
|
54
|
+
build(): Agent<M>;
|
|
55
|
+
private buildInstructions;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class MaxTurnsError extends Error {
|
|
59
|
+
readonly maxTurns: number;
|
|
60
|
+
readonly chatHistory: Message[];
|
|
61
|
+
readonly prompt: Message;
|
|
62
|
+
constructor(maxTurns: number, chatHistory: Message[], prompt: Message);
|
|
63
|
+
}
|
|
64
|
+
declare class PromptCancelledError extends Error {
|
|
65
|
+
readonly chatHistory: Message[];
|
|
66
|
+
readonly reason: string;
|
|
67
|
+
constructor(chatHistory: Message[], reason: string);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { Agent, AgentBuilder, DynamicContextOptions, DynamicToolOptions, MaxTurnsError, PromptCancelledError, PromptHook };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Agent,
|
|
3
|
+
AgentBuilder,
|
|
4
|
+
DEFAULT_MAX_TURNS,
|
|
5
|
+
MaxTurnsError,
|
|
6
|
+
PromptCancelledError,
|
|
7
|
+
PromptRequest,
|
|
8
|
+
cancelPrompt,
|
|
9
|
+
createHook,
|
|
10
|
+
runControl,
|
|
11
|
+
skipTool,
|
|
12
|
+
toolCallControl
|
|
13
|
+
} from "../chunk-IQBY2GCF.js";
|
|
14
|
+
import "../chunk-XUUY2L2D.js";
|
|
15
|
+
import "../chunk-GNWMOSNR.js";
|
|
16
|
+
import "../chunk-B4QHQN5K.js";
|
|
17
|
+
import "../chunk-CP47FBJV.js";
|
|
18
|
+
export {
|
|
19
|
+
Agent,
|
|
20
|
+
AgentBuilder,
|
|
21
|
+
DEFAULT_MAX_TURNS,
|
|
22
|
+
MaxTurnsError,
|
|
23
|
+
PromptCancelledError,
|
|
24
|
+
PromptRequest,
|
|
25
|
+
cancelPrompt,
|
|
26
|
+
createHook,
|
|
27
|
+
runControl,
|
|
28
|
+
skipTool,
|
|
29
|
+
toolCallControl
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,218 @@
|
|
|
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 { AgentTraceOptions, AgentTraceInfo, AgentObserverRegistration } from './observability/index.js';
|
|
3
|
+
import { a as ToolSet, T as ToolSearchDocument } from './zod-schema-DJTEgQBq.js';
|
|
4
|
+
import { T as Tool, A as AnyTool } from './tool-DhuBQ3yb.js';
|
|
5
|
+
import { VectorSearchIndex, VectorFilter, VectorSearchResult } from './vector-store/index.js';
|
|
6
|
+
|
|
7
|
+
type HookAction = {
|
|
8
|
+
type: "continue";
|
|
9
|
+
} | {
|
|
10
|
+
type: "terminate";
|
|
11
|
+
reason: string;
|
|
12
|
+
};
|
|
13
|
+
type ToolCallHookAction = {
|
|
14
|
+
type: "continue";
|
|
15
|
+
} | {
|
|
16
|
+
type: "skip";
|
|
17
|
+
reason: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: "terminate";
|
|
20
|
+
reason: string;
|
|
21
|
+
};
|
|
22
|
+
type RunControl = {
|
|
23
|
+
continue(): HookAction;
|
|
24
|
+
cancel(reason: string): HookAction;
|
|
25
|
+
};
|
|
26
|
+
type ToolCallControl = {
|
|
27
|
+
run(): ToolCallHookAction;
|
|
28
|
+
skip(reason: string): ToolCallHookAction;
|
|
29
|
+
cancel(reason: string): ToolCallHookAction;
|
|
30
|
+
};
|
|
31
|
+
type HookResult = HookAction | undefined;
|
|
32
|
+
type ToolCallHookResult = ToolCallHookAction | undefined;
|
|
33
|
+
type HookCallback<Args> = (args: Args) => HookAction | Promise<HookAction | undefined> | Promise<void> | void;
|
|
34
|
+
type ToolCallHookCallback<Args> = (args: Args) => ToolCallHookAction | Promise<ToolCallHookAction | undefined> | Promise<void> | void;
|
|
35
|
+
type CompletionCallHookArgs = {
|
|
36
|
+
prompt: Message;
|
|
37
|
+
history: Message[];
|
|
38
|
+
run: RunControl;
|
|
39
|
+
};
|
|
40
|
+
type CompletionResponseHookArgs<RawResponse = unknown> = {
|
|
41
|
+
prompt: Message;
|
|
42
|
+
response: CompletionResponse<RawResponse>;
|
|
43
|
+
run: RunControl;
|
|
44
|
+
};
|
|
45
|
+
type ToolHookArgs = {
|
|
46
|
+
toolName: string;
|
|
47
|
+
toolCallId?: string;
|
|
48
|
+
internalCallId: string;
|
|
49
|
+
args: string;
|
|
50
|
+
};
|
|
51
|
+
type ToolCallHookArgs = ToolHookArgs & {
|
|
52
|
+
tool: ToolCallControl;
|
|
53
|
+
};
|
|
54
|
+
type ToolResultHookArgs = ToolHookArgs & {
|
|
55
|
+
result: string;
|
|
56
|
+
run: RunControl;
|
|
57
|
+
};
|
|
58
|
+
declare function createHook<RawResponse = unknown>(hook: PromptHook<RawResponse>): PromptHook<RawResponse>;
|
|
59
|
+
declare function cancelPrompt(reason: string): HookAction;
|
|
60
|
+
declare function skipTool(reason: string): ToolCallHookAction;
|
|
61
|
+
declare const runControl: RunControl;
|
|
62
|
+
declare const toolCallControl: ToolCallControl;
|
|
63
|
+
interface PromptHook<RawResponse = unknown> {
|
|
64
|
+
onCompletionCall?: HookCallback<CompletionCallHookArgs>;
|
|
65
|
+
onCompletionResponse?: HookCallback<CompletionResponseHookArgs<RawResponse>>;
|
|
66
|
+
onToolCall?: ToolCallHookCallback<ToolCallHookArgs>;
|
|
67
|
+
onToolResult?: HookCallback<ToolResultHookArgs>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type PromptResponse = {
|
|
71
|
+
output: string;
|
|
72
|
+
usage: Usage;
|
|
73
|
+
messages: Message[];
|
|
74
|
+
trace?: AgentTraceInfo | undefined;
|
|
75
|
+
};
|
|
76
|
+
type AgentStreamEvent<RawResponse = unknown> = {
|
|
77
|
+
type: "turn_start";
|
|
78
|
+
turn: number;
|
|
79
|
+
prompt: Message;
|
|
80
|
+
history: Message[];
|
|
81
|
+
} | {
|
|
82
|
+
type: "text_delta";
|
|
83
|
+
turn: number;
|
|
84
|
+
delta: string;
|
|
85
|
+
} | {
|
|
86
|
+
type: "reasoning_delta";
|
|
87
|
+
turn: number;
|
|
88
|
+
delta: string;
|
|
89
|
+
id?: string;
|
|
90
|
+
contentType?: ReasoningContentType;
|
|
91
|
+
signature?: string;
|
|
92
|
+
} | {
|
|
93
|
+
type: "tool_call";
|
|
94
|
+
turn: number;
|
|
95
|
+
toolCall: ToolCall;
|
|
96
|
+
} | {
|
|
97
|
+
type: "tool_result";
|
|
98
|
+
turn: number;
|
|
99
|
+
toolName: string;
|
|
100
|
+
toolCallId?: string;
|
|
101
|
+
internalCallId: string;
|
|
102
|
+
args: string;
|
|
103
|
+
result: string;
|
|
104
|
+
} | {
|
|
105
|
+
type: "turn_end";
|
|
106
|
+
turn: number;
|
|
107
|
+
response: CompletionResponse<RawResponse>;
|
|
108
|
+
} | {
|
|
109
|
+
type: "final";
|
|
110
|
+
output: string;
|
|
111
|
+
usage: Usage;
|
|
112
|
+
messages: Message[];
|
|
113
|
+
trace?: AgentTraceInfo | undefined;
|
|
114
|
+
} | {
|
|
115
|
+
type: "error";
|
|
116
|
+
error: unknown;
|
|
117
|
+
};
|
|
118
|
+
declare class PromptRequest<M extends CompletionModel = CompletionModel> {
|
|
119
|
+
private readonly agent;
|
|
120
|
+
private readonly promptMessage;
|
|
121
|
+
private chatHistory;
|
|
122
|
+
private maxTurnCount;
|
|
123
|
+
private activeHook;
|
|
124
|
+
private concurrency;
|
|
125
|
+
private traceOptions;
|
|
126
|
+
private constructor();
|
|
127
|
+
static fromAgent<M extends CompletionModel>(agent: Agent<M>, prompt: string | Message): PromptRequest<M>;
|
|
128
|
+
withHistory(history: Message[]): this;
|
|
129
|
+
maxTurns(maxTurns: number): this;
|
|
130
|
+
requestHook(hook: PromptHook): this;
|
|
131
|
+
withToolConcurrency(concurrency: number): this;
|
|
132
|
+
withTrace(trace: AgentTraceOptions): this;
|
|
133
|
+
send(): Promise<PromptResponse>;
|
|
134
|
+
stream(): AsyncIterable<AgentStreamEvent>;
|
|
135
|
+
readableStream(): ReadableStream<Uint8Array>;
|
|
136
|
+
private runCompletion;
|
|
137
|
+
private executeToolCalls;
|
|
138
|
+
private startRunObservers;
|
|
139
|
+
private fetchDynamicContext;
|
|
140
|
+
private fetchToolDefinitions;
|
|
141
|
+
private recordToolError;
|
|
142
|
+
private runCompletionCallHook;
|
|
143
|
+
private runCompletionResponseHook;
|
|
144
|
+
private cancelled;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
type AgentOptions<M extends CompletionModel = CompletionModel> = {
|
|
148
|
+
id: string;
|
|
149
|
+
name?: string | undefined;
|
|
150
|
+
description?: string | undefined;
|
|
151
|
+
model: M;
|
|
152
|
+
instructions?: string | undefined;
|
|
153
|
+
staticContext?: Document[];
|
|
154
|
+
temperature?: number | undefined;
|
|
155
|
+
maxTokens?: number | undefined;
|
|
156
|
+
additionalParams?: JsonValue | undefined;
|
|
157
|
+
toolSet?: ToolSet | undefined;
|
|
158
|
+
toolChoice?: ToolChoice | undefined;
|
|
159
|
+
defaultMaxTurns?: number | undefined;
|
|
160
|
+
hook?: PromptHook | undefined;
|
|
161
|
+
outputSchema?: JsonObject | undefined;
|
|
162
|
+
observers?: AgentObserverRegistration[] | undefined;
|
|
163
|
+
dynamicContexts?: DynamicContextRegistration[] | undefined;
|
|
164
|
+
dynamicTools?: DynamicToolRegistration[] | undefined;
|
|
165
|
+
};
|
|
166
|
+
declare const DEFAULT_MAX_TURNS = 20;
|
|
167
|
+
type AgentToolOptions = {
|
|
168
|
+
name: string;
|
|
169
|
+
description?: string | undefined;
|
|
170
|
+
maxTurns?: number | undefined;
|
|
171
|
+
};
|
|
172
|
+
type DynamicContextOptions<T = unknown> = {
|
|
173
|
+
topK: number;
|
|
174
|
+
threshold?: number | undefined;
|
|
175
|
+
filter?: VectorFilter | undefined;
|
|
176
|
+
format?: ((result: VectorSearchResult<T>) => Document) | undefined;
|
|
177
|
+
};
|
|
178
|
+
type DynamicContextRegistration<T = unknown> = {
|
|
179
|
+
index: VectorSearchIndex<T>;
|
|
180
|
+
options: DynamicContextOptions<T>;
|
|
181
|
+
};
|
|
182
|
+
type DynamicToolOptions = {
|
|
183
|
+
topK: number;
|
|
184
|
+
threshold?: number | undefined;
|
|
185
|
+
filter?: VectorFilter | undefined;
|
|
186
|
+
};
|
|
187
|
+
type DynamicToolRegistration = {
|
|
188
|
+
index: VectorSearchIndex<ToolSearchDocument>;
|
|
189
|
+
options: DynamicToolOptions;
|
|
190
|
+
};
|
|
191
|
+
declare class Agent<M extends CompletionModel = CompletionModel> {
|
|
192
|
+
readonly id: string;
|
|
193
|
+
readonly name: string | undefined;
|
|
194
|
+
readonly description: string | undefined;
|
|
195
|
+
readonly model: M;
|
|
196
|
+
readonly instructions: string | undefined;
|
|
197
|
+
readonly staticContext: Document[];
|
|
198
|
+
readonly temperature: number | undefined;
|
|
199
|
+
readonly maxTokens: number | undefined;
|
|
200
|
+
readonly additionalParams: JsonValue | undefined;
|
|
201
|
+
readonly toolSet: ToolSet;
|
|
202
|
+
readonly toolChoice: ToolChoice | undefined;
|
|
203
|
+
readonly defaultMaxTurns: number | undefined;
|
|
204
|
+
readonly hook: PromptHook | undefined;
|
|
205
|
+
readonly outputSchema: JsonObject | undefined;
|
|
206
|
+
readonly observers: AgentObserverRegistration[];
|
|
207
|
+
readonly dynamicContexts: DynamicContextRegistration[];
|
|
208
|
+
readonly dynamicTools: DynamicToolRegistration[];
|
|
209
|
+
constructor(options: AgentOptions<M>);
|
|
210
|
+
prompt(prompt: string | Message): PromptRequest<M>;
|
|
211
|
+
asTool(options: AgentToolOptions): Tool<{
|
|
212
|
+
prompt: string;
|
|
213
|
+
}, string>;
|
|
214
|
+
getTool(toolName: string): AnyTool | undefined;
|
|
215
|
+
callTool(toolName: string, args: string): Promise<string>;
|
|
216
|
+
}
|
|
217
|
+
|
|
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 b, type AgentToolOptions as c, type CompletionResponseHookArgs as d, type DynamicContextOptions as e, type DynamicContextRegistration as f, type DynamicToolOptions as g, type DynamicToolRegistration as h, type HookResult as i, PromptRequest as j, type PromptResponse as k, type ToolCallHookAction as l, type ToolCallHookArgs as m, type ToolCallHookResult as n, type ToolHookArgs as o, type ToolResultHookArgs as p, cancelPrompt as q, createHook as r, runControl as s, skipTool as t, toolCallControl as u };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { j as JsonValue } from '../types-BrxLd7ay.js';
|
|
2
|
+
|
|
3
|
+
type AudioGenerationRequest = {
|
|
4
|
+
text: string;
|
|
5
|
+
voice: string;
|
|
6
|
+
speed: number;
|
|
7
|
+
additionalParams?: JsonValue | undefined;
|
|
8
|
+
};
|
|
9
|
+
type AudioGenerationResponse<RawResponse = unknown> = {
|
|
10
|
+
audio: Uint8Array;
|
|
11
|
+
mediaType?: string | undefined;
|
|
12
|
+
rawResponse: RawResponse;
|
|
13
|
+
};
|
|
14
|
+
interface AudioGenerationModel<RawResponse = unknown> {
|
|
15
|
+
readonly provider?: string | undefined;
|
|
16
|
+
readonly defaultModel?: string | undefined;
|
|
17
|
+
audioGeneration(request: AudioGenerationRequest): Promise<AudioGenerationResponse<RawResponse>>;
|
|
18
|
+
}
|
|
19
|
+
declare class AudioGenerationRequestBuilder<Model extends AudioGenerationModel = AudioGenerationModel> {
|
|
20
|
+
private readonly model;
|
|
21
|
+
private request;
|
|
22
|
+
constructor(model: Model);
|
|
23
|
+
text(text: string): this;
|
|
24
|
+
voice(voice: string): this;
|
|
25
|
+
speed(speed: number): this;
|
|
26
|
+
additionalParams(additionalParams: JsonValue): this;
|
|
27
|
+
build(): AudioGenerationRequest;
|
|
28
|
+
send(): Promise<Awaited<ReturnType<Model["audioGeneration"]>>>;
|
|
29
|
+
}
|
|
30
|
+
declare function audioGenerationRequest<Model extends AudioGenerationModel>(model: Model): AudioGenerationRequestBuilder<Model>;
|
|
31
|
+
|
|
32
|
+
export { type AudioGenerationModel, type AudioGenerationRequest, AudioGenerationRequestBuilder, type AudioGenerationResponse, audioGenerationRequest };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/transcription/index.ts
|
|
2
|
+
var TranscriptionRequestBuilder = class {
|
|
3
|
+
constructor(model) {
|
|
4
|
+
this.model = model;
|
|
5
|
+
}
|
|
6
|
+
model;
|
|
7
|
+
request = {
|
|
8
|
+
data: new Uint8Array(),
|
|
9
|
+
filename: "file"
|
|
10
|
+
};
|
|
11
|
+
data(data) {
|
|
12
|
+
this.request = { ...this.request, data: toUint8Array(data) };
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
filename(filename) {
|
|
16
|
+
this.request = { ...this.request, filename };
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
language(language) {
|
|
20
|
+
this.request = { ...this.request, language };
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
prompt(prompt) {
|
|
24
|
+
this.request = { ...this.request, prompt };
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
temperature(temperature) {
|
|
28
|
+
this.request = { ...this.request, temperature };
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
additionalParams(additionalParams) {
|
|
32
|
+
this.request = { ...this.request, additionalParams };
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
build() {
|
|
36
|
+
if (this.request.data.byteLength === 0) {
|
|
37
|
+
throw new Error("Transcription data cannot be empty.");
|
|
38
|
+
}
|
|
39
|
+
return { ...this.request, data: toUint8Array(this.request.data) };
|
|
40
|
+
}
|
|
41
|
+
send() {
|
|
42
|
+
return this.model.transcription(this.build());
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
function transcriptionRequest(model) {
|
|
46
|
+
return new TranscriptionRequestBuilder(model);
|
|
47
|
+
}
|
|
48
|
+
function toUint8Array(bytes) {
|
|
49
|
+
if (bytes instanceof Uint8Array) {
|
|
50
|
+
return new Uint8Array(
|
|
51
|
+
bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return new Uint8Array(bytes);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export {
|
|
58
|
+
TranscriptionRequestBuilder,
|
|
59
|
+
transcriptionRequest
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=chunk-7QI6ZAFI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/transcription/index.ts"],"sourcesContent":["import type { JsonValue } from \"../completion\";\n\nexport type TranscriptionRequest = {\n data: Uint8Array;\n filename: string;\n language?: string | undefined;\n prompt?: string | undefined;\n temperature?: number | undefined;\n additionalParams?: JsonValue | undefined;\n};\n\nexport type TranscriptionResponse<RawResponse = unknown> = {\n text: string;\n rawResponse: RawResponse;\n};\n\nexport interface TranscriptionModel<RawResponse = unknown> {\n readonly provider?: string | undefined;\n readonly defaultModel?: string | undefined;\n transcription(request: TranscriptionRequest): Promise<TranscriptionResponse<RawResponse>>;\n}\n\nexport class TranscriptionRequestBuilder<Model extends TranscriptionModel = TranscriptionModel> {\n private request: TranscriptionRequest = {\n data: new Uint8Array(),\n filename: \"file\",\n };\n\n constructor(private readonly model: Model) {}\n\n data(data: Uint8Array | ArrayBuffer): this {\n this.request = { ...this.request, data: toUint8Array(data) };\n return this;\n }\n\n filename(filename: string): this {\n this.request = { ...this.request, filename };\n return this;\n }\n\n language(language: string): this {\n this.request = { ...this.request, language };\n return this;\n }\n\n prompt(prompt: string): this {\n this.request = { ...this.request, prompt };\n return this;\n }\n\n temperature(temperature: number): this {\n this.request = { ...this.request, temperature };\n return this;\n }\n\n additionalParams(additionalParams: JsonValue): this {\n this.request = { ...this.request, additionalParams };\n return this;\n }\n\n build(): TranscriptionRequest {\n if (this.request.data.byteLength === 0) {\n throw new Error(\"Transcription data cannot be empty.\");\n }\n return { ...this.request, data: toUint8Array(this.request.data) };\n }\n\n send(): Promise<Awaited<ReturnType<Model[\"transcription\"]>>> {\n return this.model.transcription(this.build()) as Promise<\n Awaited<ReturnType<Model[\"transcription\"]>>\n >;\n }\n}\n\nexport function transcriptionRequest<Model extends TranscriptionModel>(\n model: Model,\n): TranscriptionRequestBuilder<Model> {\n return new TranscriptionRequestBuilder(model);\n}\n\nfunction toUint8Array(bytes: Uint8Array | ArrayBuffer): Uint8Array {\n if (bytes instanceof Uint8Array) {\n return new Uint8Array(\n bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength),\n );\n }\n return new Uint8Array(bytes);\n}\n"],"mappings":";AAsBO,IAAM,8BAAN,MAAyF;AAAA,EAM9F,YAA6B,OAAc;AAAd;AAAA,EAAe;AAAA,EAAf;AAAA,EALrB,UAAgC;AAAA,IACtC,MAAM,IAAI,WAAW;AAAA,IACrB,UAAU;AAAA,EACZ;AAAA,EAIA,KAAK,MAAsC;AACzC,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,MAAM,aAAa,IAAI,EAAE;AAC3D,WAAO;AAAA,EACT;AAAA,EAEA,SAAS,UAAwB;AAC/B,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,SAAS;AAC3C,WAAO;AAAA,EACT;AAAA,EAEA,SAAS,UAAwB;AAC/B,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,SAAS;AAC3C,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,QAAsB;AAC3B,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,OAAO;AACzC,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,aAA2B;AACrC,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,YAAY;AAC9C,WAAO;AAAA,EACT;AAAA,EAEA,iBAAiB,kBAAmC;AAClD,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,iBAAiB;AACnD,WAAO;AAAA,EACT;AAAA,EAEA,QAA8B;AAC5B,QAAI,KAAK,QAAQ,KAAK,eAAe,GAAG;AACtC,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AACA,WAAO,EAAE,GAAG,KAAK,SAAS,MAAM,aAAa,KAAK,QAAQ,IAAI,EAAE;AAAA,EAClE;AAAA,EAEA,OAA6D;AAC3D,WAAO,KAAK,MAAM,cAAc,KAAK,MAAM,CAAC;AAAA,EAG9C;AACF;AAEO,SAAS,qBACd,OACoC;AACpC,SAAO,IAAI,4BAA4B,KAAK;AAC9C;AAEA,SAAS,aAAa,OAA6C;AACjE,MAAI,iBAAiB,YAAY;AAC/B,WAAO,IAAI;AAAA,MACT,MAAM,OAAO,MAAM,MAAM,YAAY,MAAM,aAAa,MAAM,UAAU;AAAA,IAC1E;AAAA,EACF;AACA,SAAO,IAAI,WAAW,KAAK;AAC7B;","names":[]}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// src/embeddings/index.ts
|
|
2
|
+
async function embedText(model, text) {
|
|
3
|
+
const embeddings = await embedTexts(model, [text]);
|
|
4
|
+
const embedding = embeddings[0];
|
|
5
|
+
if (embedding === void 0) {
|
|
6
|
+
throw new Error("Embedding model returned no embeddings");
|
|
7
|
+
}
|
|
8
|
+
return embedding;
|
|
9
|
+
}
|
|
10
|
+
async function embedTexts(model, texts) {
|
|
11
|
+
if (texts.length === 0) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
const maxBatchSize = Math.max(1, Math.trunc(model.maxBatchSize ?? texts.length));
|
|
15
|
+
const batches = [];
|
|
16
|
+
for (let index = 0; index < texts.length; index += maxBatchSize) {
|
|
17
|
+
batches.push(texts.slice(index, index + maxBatchSize));
|
|
18
|
+
}
|
|
19
|
+
const results = await mapWithConcurrency(batches, 1, (batch) => model.embedTexts(batch));
|
|
20
|
+
const embeddings = results.flat();
|
|
21
|
+
if (embeddings.length !== texts.length) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Embedding model returned ${embeddings.length} embeddings for ${texts.length} texts`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return embeddings;
|
|
27
|
+
}
|
|
28
|
+
async function embedDocuments(model, documents, options) {
|
|
29
|
+
const prepared = documents.map((document, index) => {
|
|
30
|
+
const content = options.content(document, index);
|
|
31
|
+
const texts = Array.isArray(content) ? content : [content];
|
|
32
|
+
return {
|
|
33
|
+
id: options.id?.(document, index) ?? `doc${index}`,
|
|
34
|
+
document,
|
|
35
|
+
metadata: options.metadata?.(document, index),
|
|
36
|
+
texts
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
const flatTexts = prepared.flatMap(
|
|
40
|
+
(item, documentIndex) => item.texts.map((text) => ({ documentIndex, text }))
|
|
41
|
+
);
|
|
42
|
+
const embeddings = await mapWithConcurrency(
|
|
43
|
+
chunk(flatTexts, Math.max(1, Math.trunc(model.maxBatchSize ?? (flatTexts.length || 1)))),
|
|
44
|
+
Math.max(1, Math.trunc(options.concurrency ?? 1)),
|
|
45
|
+
async (batch) => {
|
|
46
|
+
const batchEmbeddings = await model.embedTexts(batch.map((item) => item.text));
|
|
47
|
+
if (batchEmbeddings.length !== batch.length) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Embedding model returned ${batchEmbeddings.length} embeddings for ${batch.length} texts`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return batch.map((item, index) => ({
|
|
53
|
+
documentIndex: item.documentIndex,
|
|
54
|
+
embedding: batchEmbeddings[index]
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
const byDocument = /* @__PURE__ */ new Map();
|
|
59
|
+
for (const item of embeddings.flat()) {
|
|
60
|
+
const list = byDocument.get(item.documentIndex) ?? [];
|
|
61
|
+
list.push(item.embedding);
|
|
62
|
+
byDocument.set(item.documentIndex, list);
|
|
63
|
+
}
|
|
64
|
+
return prepared.map((item, index) => ({
|
|
65
|
+
id: item.id,
|
|
66
|
+
document: item.document,
|
|
67
|
+
...item.metadata === void 0 ? {} : { metadata: item.metadata },
|
|
68
|
+
embeddings: byDocument.get(index) ?? []
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
function dotProduct(left, right) {
|
|
72
|
+
assertSameDimensions(left, right);
|
|
73
|
+
return left.reduce((sum, value, index) => sum + value * right[index], 0);
|
|
74
|
+
}
|
|
75
|
+
function cosineSimilarity(left, right) {
|
|
76
|
+
assertSameDimensions(left, right);
|
|
77
|
+
const leftMagnitude = magnitude(left);
|
|
78
|
+
const rightMagnitude = magnitude(right);
|
|
79
|
+
if (leftMagnitude === 0 || rightMagnitude === 0) {
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
return dotProduct(left, right) / (leftMagnitude * rightMagnitude);
|
|
83
|
+
}
|
|
84
|
+
function angularDistance(left, right) {
|
|
85
|
+
const similarity = Math.max(-1, Math.min(1, cosineSimilarity(left, right)));
|
|
86
|
+
return Math.acos(similarity) / Math.PI;
|
|
87
|
+
}
|
|
88
|
+
function euclideanDistance(left, right) {
|
|
89
|
+
assertSameDimensions(left, right);
|
|
90
|
+
return Math.sqrt(
|
|
91
|
+
left.reduce((sum, value, index) => sum + (value - right[index]) ** 2, 0)
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function manhattanDistance(left, right) {
|
|
95
|
+
assertSameDimensions(left, right);
|
|
96
|
+
return left.reduce((sum, value, index) => sum + Math.abs(value - right[index]), 0);
|
|
97
|
+
}
|
|
98
|
+
function chebyshevDistance(left, right) {
|
|
99
|
+
assertSameDimensions(left, right);
|
|
100
|
+
return left.reduce(
|
|
101
|
+
(max, value, index) => Math.max(max, Math.abs(value - right[index])),
|
|
102
|
+
0
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
function magnitude(vector) {
|
|
106
|
+
return Math.sqrt(vector.reduce((sum, value) => sum + value ** 2, 0));
|
|
107
|
+
}
|
|
108
|
+
function assertSameDimensions(left, right) {
|
|
109
|
+
if (left.length !== right.length) {
|
|
110
|
+
throw new Error(`Vector dimension mismatch: ${left.length} !== ${right.length}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function chunk(items, size) {
|
|
114
|
+
const chunks = [];
|
|
115
|
+
for (let index = 0; index < items.length; index += size) {
|
|
116
|
+
chunks.push(items.slice(index, index + size));
|
|
117
|
+
}
|
|
118
|
+
return chunks;
|
|
119
|
+
}
|
|
120
|
+
async function mapWithConcurrency(items, concurrency, mapper) {
|
|
121
|
+
const results = new Array(items.length);
|
|
122
|
+
let next = 0;
|
|
123
|
+
async function worker() {
|
|
124
|
+
while (next < items.length) {
|
|
125
|
+
const index = next;
|
|
126
|
+
next += 1;
|
|
127
|
+
results[index] = await mapper(items[index]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => worker()));
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export {
|
|
135
|
+
embedText,
|
|
136
|
+
embedTexts,
|
|
137
|
+
embedDocuments,
|
|
138
|
+
dotProduct,
|
|
139
|
+
cosineSimilarity,
|
|
140
|
+
angularDistance,
|
|
141
|
+
euclideanDistance,
|
|
142
|
+
manhattanDistance,
|
|
143
|
+
chebyshevDistance
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=chunk-A7VDIZQN.js.map
|