@anvia/core 0.11.1 → 0.11.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/dist/agent/index.d.ts +13 -31
- package/dist/agent/index.js +12 -34
- package/dist/{agent-DoF-xWGj.d.ts → agent-Bq2KtSaw.d.ts} +8 -125
- package/dist/audio-generation/index.d.ts +2 -1
- package/dist/audio-generation/index.js +1 -1
- package/dist/audio-generation/index.js.map +1 -1
- package/dist/{chunk-T22FYPE2.js → chunk-5EDHXYH5.js} +6 -6
- package/dist/{chunk-M4T4ID34.js → chunk-62RUYGMM.js} +6 -6
- package/dist/chunk-62RUYGMM.js.map +1 -0
- package/dist/{chunk-AREAH2X5.js → chunk-DOS77JZC.js} +3 -239
- package/dist/chunk-DOS77JZC.js.map +1 -0
- package/dist/{chunk-W4DZU6IW.js → chunk-JS5MGVNM.js} +50 -48
- package/dist/chunk-JS5MGVNM.js.map +1 -0
- package/dist/{chunk-XXT2UCAR.js → chunk-M6RPFHG7.js} +2 -2
- package/dist/chunk-M6RPFHG7.js.map +1 -0
- package/dist/chunk-N6REB3DH.js +205 -0
- package/dist/chunk-N6REB3DH.js.map +1 -0
- package/dist/chunk-NPZDYOE6.js +53 -0
- package/dist/chunk-NPZDYOE6.js.map +1 -0
- package/dist/{chunk-VJ7AZVLS.js → chunk-R75SGHCI.js} +4 -4
- package/dist/chunk-R75SGHCI.js.map +1 -0
- package/dist/{chunk-IFQVPV32.js → chunk-SISUMRUX.js} +308 -539
- package/dist/chunk-SISUMRUX.js.map +1 -0
- package/dist/{chunk-246MPVBE.js → chunk-V3WTT7TE.js} +2 -2
- package/dist/{chunk-BVGDCFQW.js → chunk-X3SWV6W6.js} +6 -6
- package/dist/completion/index.d.ts +3 -3
- package/dist/{create-completion-7GNUka0b.d.ts → create-completion-BEbtU5pX.d.ts} +1 -1
- package/dist/embeddings/index.d.ts +6 -5
- package/dist/embeddings/index.js +1 -1
- package/dist/evals/index.d.ts +9 -9
- package/dist/evals/index.js +16 -14
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +9 -9
- package/dist/extractor/index.js +12 -10
- package/dist/hooks/index.d.ts +12 -0
- package/dist/hooks/index.js +18 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/image-generation/index.d.ts +2 -1
- package/dist/image-generation/index.js +1 -1
- package/dist/image-generation/index.js.map +1 -1
- package/dist/index-CTNzKidE.d.ts +198 -0
- package/dist/index.d.ts +17 -15
- package/dist/index.js +24 -29
- package/dist/internal/agent.d.ts +9 -9
- package/dist/internal/agent.js +9 -7
- package/dist/loaders/index.d.ts +8 -7
- package/dist/loaders/index.js.map +1 -1
- package/dist/mcp/index.d.ts +4 -4
- package/dist/memory/index.d.ts +4 -40
- package/dist/memory/index.js +1 -1
- package/dist/{middleware-D7nRBusA.d.ts → middleware-CG7l0rR3.d.ts} +4 -4
- package/dist/model-listing/index.d.ts +12 -11
- package/dist/model-listing/index.js +1 -1
- package/dist/model-listing/index.js.map +1 -1
- package/dist/observability/index.d.ts +3 -138
- package/dist/observability/index.js.map +1 -1
- package/dist/pipeline/index.d.ts +9 -9
- package/dist/pipeline/index.js +3 -3
- package/dist/request/index.d.ts +27 -0
- package/dist/request/index.js +23 -0
- package/dist/request/index.js.map +1 -0
- package/dist/skills/index.d.ts +4 -4
- package/dist/skills/index.js +6 -6
- package/dist/{think-tool-DmBR-YzU.d.ts → think-tool-B8Rqw2i_.d.ts} +1 -1
- package/dist/tool/index.d.ts +6 -6
- package/dist/tool/index.js +5 -5
- package/dist/{tool-Hlcb459D.d.ts → tool-BLypI31e.d.ts} +1 -1
- package/dist/transcription/index.d.ts +1 -1
- package/dist/{types-CP3iyh6j.d.ts → types-C8loR4il.d.ts} +1 -1
- package/dist/types-CCxnnJjU.d.ts +114 -0
- package/dist/{types-CiFIAGqq.d.ts → types-CIwm9p4w.d.ts} +2 -2
- package/dist/types-CNfjQ5rB.d.ts +42 -0
- package/dist/{types-DrkBkjzk.d.ts → types-D-YTJdLj.d.ts} +1 -1
- package/dist/{types-IB2e9u5M.d.ts → types-DHHZztGO.d.ts} +1 -1
- package/dist/{types-B0cuzkjP.d.ts → types-KKjICcrA.d.ts} +2 -2
- package/dist/ui/index.d.ts +2 -28
- package/dist/ui/index.js +1 -11
- package/dist/vector-store/index.d.ts +5 -5
- package/dist/vector-store/index.js +4 -4
- package/package.json +10 -2
- package/dist/chunk-AREAH2X5.js.map +0 -1
- package/dist/chunk-IFQVPV32.js.map +0 -1
- package/dist/chunk-M4T4ID34.js.map +0 -1
- package/dist/chunk-VJ7AZVLS.js.map +0 -1
- package/dist/chunk-W4DZU6IW.js.map +0 -1
- package/dist/chunk-XXT2UCAR.js.map +0 -1
- package/dist/types-C2ArxcON.d.ts +0 -65
- /package/dist/{chunk-T22FYPE2.js.map → chunk-5EDHXYH5.js.map} +0 -0
- /package/dist/{chunk-246MPVBE.js.map → chunk-V3WTT7TE.js.map} +0 -0
- /package/dist/{chunk-BVGDCFQW.js.map → chunk-X3SWV6W6.js.map} +0 -0
package/dist/extractor/index.js
CHANGED
|
@@ -2,20 +2,22 @@ import {
|
|
|
2
2
|
ExtractionError,
|
|
3
3
|
Extractor,
|
|
4
4
|
ExtractorBuilder
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-
|
|
7
|
-
import "../chunk-
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-XXT2UCAR.js";
|
|
5
|
+
} from "../chunk-R75SGHCI.js";
|
|
6
|
+
import "../chunk-62RUYGMM.js";
|
|
7
|
+
import "../chunk-N6REB3DH.js";
|
|
8
|
+
import "../chunk-SISUMRUX.js";
|
|
10
9
|
import "../chunk-YK4WAAS4.js";
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
13
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-XUUY2L2D.js";
|
|
11
|
+
import "../chunk-X3SWV6W6.js";
|
|
12
|
+
import "../chunk-5EDHXYH5.js";
|
|
13
|
+
import "../chunk-M6RPFHG7.js";
|
|
14
|
+
import "../chunk-JS5MGVNM.js";
|
|
15
|
+
import "../chunk-OIMLU4SF.js";
|
|
14
16
|
import "../chunk-5G3VME2D.js";
|
|
15
17
|
import "../chunk-WJO6NVE2.js";
|
|
16
18
|
import "../chunk-WQKHFADH.js";
|
|
17
|
-
import "../chunk-
|
|
18
|
-
import "../chunk-
|
|
19
|
+
import "../chunk-NPZDYOE6.js";
|
|
20
|
+
import "../chunk-MMHG7WAM.js";
|
|
19
21
|
export {
|
|
20
22
|
ExtractionError,
|
|
21
23
|
Extractor,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { H as HookAction, P as PromptHook, T as ToolApprovalRequestOptions, a as ToolCallHookAction, R as RunControl, b as ToolCallControl } from '../types-CCxnnJjU.js';
|
|
2
|
+
export { C as CompletionCallHookArgs, c as CompletionErrorHookArgs, d as CompletionResponseHookArgs, e as HookResult, f as RunEndHookArgs, g as RunErrorHookArgs, h as RunStartHookArgs, i as ToolCallHookArgs, j as ToolCallHookResult, k as ToolErrorHookArgs, l as ToolHookArgs, m as ToolResultHookArgs, n as TurnEndHookArgs, o as TurnStartHookArgs } from '../types-CCxnnJjU.js';
|
|
3
|
+
import '../types-C8loR4il.js';
|
|
4
|
+
|
|
5
|
+
declare function createHook<RawResponse = unknown>(hook: PromptHook<RawResponse>): PromptHook<RawResponse>;
|
|
6
|
+
declare function cancelPrompt(reason: string): HookAction;
|
|
7
|
+
declare function skipTool(reason: string): ToolCallHookAction;
|
|
8
|
+
declare function requestToolApproval(options?: ToolApprovalRequestOptions): ToolCallHookAction;
|
|
9
|
+
declare const runControl: RunControl;
|
|
10
|
+
declare const toolCallControl: ToolCallControl;
|
|
11
|
+
|
|
12
|
+
export { HookAction, PromptHook, RunControl, ToolApprovalRequestOptions, ToolCallControl, ToolCallHookAction, cancelPrompt, createHook, requestToolApproval, runControl, skipTool, toolCallControl };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cancelPrompt,
|
|
3
|
+
createHook,
|
|
4
|
+
requestToolApproval,
|
|
5
|
+
runControl,
|
|
6
|
+
skipTool,
|
|
7
|
+
toolCallControl
|
|
8
|
+
} from "../chunk-NPZDYOE6.js";
|
|
9
|
+
import "../chunk-MMHG7WAM.js";
|
|
10
|
+
export {
|
|
11
|
+
cancelPrompt,
|
|
12
|
+
createHook,
|
|
13
|
+
requestToolApproval,
|
|
14
|
+
runControl,
|
|
15
|
+
skipTool,
|
|
16
|
+
toolCallControl
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { e as JsonValue } from '../types-
|
|
1
|
+
import { e as JsonValue } from '../types-C8loR4il.js';
|
|
2
2
|
|
|
3
3
|
type ImageGenerationRequest = {
|
|
4
4
|
prompt: string;
|
|
@@ -21,6 +21,7 @@ interface ImageGenerationModel<RawResponse = unknown> {
|
|
|
21
21
|
readonly defaultModel?: string | undefined;
|
|
22
22
|
imageGeneration(request: ImageGenerationRequest): Promise<ImageGenerationResponse<RawResponse>>;
|
|
23
23
|
}
|
|
24
|
+
|
|
24
25
|
declare class ImageGenerationRequestBuilder<Model extends ImageGenerationModel = ImageGenerationModel> {
|
|
25
26
|
private readonly model;
|
|
26
27
|
private request;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/image-generation/
|
|
1
|
+
{"version":3,"sources":["../../src/image-generation/request.ts"],"sourcesContent":["import type { JsonValue } from \"../completion/types\";\nimport type { ImageGenerationModel, ImageGenerationRequest } from \"./types\";\n\nexport class ImageGenerationRequestBuilder<\n Model extends ImageGenerationModel = ImageGenerationModel,\n> {\n private request: ImageGenerationRequest = {\n prompt: \"\",\n width: 1024,\n height: 1024,\n };\n\n constructor(private readonly model: Model) {}\n\n prompt(prompt: string): this {\n this.request = { ...this.request, prompt };\n return this;\n }\n\n width(width: number): this {\n this.request = { ...this.request, width };\n return this;\n }\n\n height(height: number): this {\n this.request = { ...this.request, height };\n return this;\n }\n\n additionalParams(additionalParams: JsonValue): this {\n this.request = { ...this.request, additionalParams };\n return this;\n }\n\n build(): ImageGenerationRequest {\n return { ...this.request };\n }\n\n send(): Promise<Awaited<ReturnType<Model[\"imageGeneration\"]>>> {\n return this.model.imageGeneration(this.build()) as Promise<\n Awaited<ReturnType<Model[\"imageGeneration\"]>>\n >;\n }\n}\n\nexport function imageGenerationRequest<Model extends ImageGenerationModel>(\n model: Model,\n): ImageGenerationRequestBuilder<Model> {\n return new ImageGenerationRequestBuilder(model);\n}\n"],"mappings":";AAGO,IAAM,gCAAN,MAEL;AAAA,EAOA,YAA6B,OAAc;AAAd;AAAA,EAAe;AAAA,EAAf;AAAA,EANrB,UAAkC;AAAA,IACxC,QAAQ;AAAA,IACR,OAAO;AAAA,IACP,QAAQ;AAAA,EACV;AAAA,EAIA,OAAO,QAAsB;AAC3B,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,OAAO;AACzC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,OAAqB;AACzB,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,MAAM;AACxC,WAAO;AAAA,EACT;AAAA,EAEA,OAAO,QAAsB;AAC3B,SAAK,UAAU,EAAE,GAAG,KAAK,SAAS,OAAO;AACzC,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,198 @@
|
|
|
1
|
+
import { x as ReasoningContentType, f as ToolCall, U as Usage, M as Message, k as ToolResultContent, c as CompletionResponse, b as CompletionRequest, J as JsonObject, t as CompletionModelCapabilities, h as ToolDefinition, e as JsonValue } from './types-C8loR4il.js';
|
|
2
|
+
import { g as ToolCallStreamEvent } from './tool-BLypI31e.js';
|
|
3
|
+
|
|
4
|
+
type PromptResponse = {
|
|
5
|
+
output: string;
|
|
6
|
+
usage: Usage;
|
|
7
|
+
messages: Message[];
|
|
8
|
+
trace?: AgentTraceInfo | undefined;
|
|
9
|
+
};
|
|
10
|
+
type AgentDeltaEvent = {
|
|
11
|
+
type: "text_delta";
|
|
12
|
+
delta: string;
|
|
13
|
+
} | {
|
|
14
|
+
type: "reasoning_delta";
|
|
15
|
+
delta: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
contentType?: ReasoningContentType;
|
|
18
|
+
signature?: string;
|
|
19
|
+
} | {
|
|
20
|
+
type: "tool_call";
|
|
21
|
+
toolCall: ToolCall;
|
|
22
|
+
};
|
|
23
|
+
type AgentChildStreamEvent<RawResponse = unknown> = {
|
|
24
|
+
type: "turn_start";
|
|
25
|
+
turn: number;
|
|
26
|
+
prompt: Message;
|
|
27
|
+
history: Message[];
|
|
28
|
+
} | {
|
|
29
|
+
type: "text_delta";
|
|
30
|
+
turn: number;
|
|
31
|
+
delta: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: "reasoning_delta";
|
|
34
|
+
turn: number;
|
|
35
|
+
delta: string;
|
|
36
|
+
id?: string;
|
|
37
|
+
contentType?: ReasoningContentType;
|
|
38
|
+
signature?: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: "tool_call";
|
|
41
|
+
turn: number;
|
|
42
|
+
toolCall: ToolCall;
|
|
43
|
+
} | {
|
|
44
|
+
type: "tool_result";
|
|
45
|
+
turn: number;
|
|
46
|
+
toolName: string;
|
|
47
|
+
toolCallId?: string;
|
|
48
|
+
internalCallId: string;
|
|
49
|
+
args: string;
|
|
50
|
+
result: string;
|
|
51
|
+
structuredResult?: ToolResultContent[] | undefined;
|
|
52
|
+
} | {
|
|
53
|
+
type: "turn_end";
|
|
54
|
+
turn: number;
|
|
55
|
+
response: CompletionResponse<RawResponse>;
|
|
56
|
+
} | {
|
|
57
|
+
type: "final";
|
|
58
|
+
runId: string;
|
|
59
|
+
output: string;
|
|
60
|
+
usage: Usage;
|
|
61
|
+
messages: Message[];
|
|
62
|
+
trace?: AgentTraceInfo | undefined;
|
|
63
|
+
} | {
|
|
64
|
+
type: "error";
|
|
65
|
+
error: unknown;
|
|
66
|
+
};
|
|
67
|
+
type AgentStreamEvent<RawResponse = unknown> = AgentChildStreamEvent<RawResponse> | {
|
|
68
|
+
type: "agent_tool_event";
|
|
69
|
+
turn: number;
|
|
70
|
+
toolName: string;
|
|
71
|
+
toolCallId?: string;
|
|
72
|
+
internalCallId: string;
|
|
73
|
+
agentId: string;
|
|
74
|
+
agentName?: string;
|
|
75
|
+
event: AgentChildStreamEvent<RawResponse>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
type AgentTraceInfo = {
|
|
79
|
+
traceId?: string | undefined;
|
|
80
|
+
observationId?: string | undefined;
|
|
81
|
+
};
|
|
82
|
+
type AgentTraceOptions = {
|
|
83
|
+
name?: string | undefined;
|
|
84
|
+
userId?: string | undefined;
|
|
85
|
+
sessionId?: string | undefined;
|
|
86
|
+
metadata?: Record<string, unknown> | undefined;
|
|
87
|
+
tags?: string[] | undefined;
|
|
88
|
+
version?: string | undefined;
|
|
89
|
+
traceId?: string | undefined;
|
|
90
|
+
failOnObserverError?: boolean | undefined;
|
|
91
|
+
};
|
|
92
|
+
type AgentRunPromptRef = {
|
|
93
|
+
name: string;
|
|
94
|
+
version?: number | undefined;
|
|
95
|
+
};
|
|
96
|
+
type AgentRunStartArgs = {
|
|
97
|
+
agentName?: string | undefined;
|
|
98
|
+
agentDescription?: string | undefined;
|
|
99
|
+
instructions?: string | undefined;
|
|
100
|
+
trace?: AgentTraceOptions | undefined;
|
|
101
|
+
prompt: Message;
|
|
102
|
+
promptRef?: AgentRunPromptRef | undefined;
|
|
103
|
+
history: Message[];
|
|
104
|
+
maxTurns: number;
|
|
105
|
+
};
|
|
106
|
+
type AgentRunEndArgs = {
|
|
107
|
+
output: string;
|
|
108
|
+
usage: Usage;
|
|
109
|
+
messages: Message[];
|
|
110
|
+
};
|
|
111
|
+
type AgentRunErrorArgs = {
|
|
112
|
+
error: unknown;
|
|
113
|
+
usage: Usage;
|
|
114
|
+
messages: Message[];
|
|
115
|
+
};
|
|
116
|
+
type AgentGenerationStartArgs = {
|
|
117
|
+
turn: number;
|
|
118
|
+
request: CompletionRequest;
|
|
119
|
+
providerRequest?: JsonObject | undefined;
|
|
120
|
+
modelInfo?: {
|
|
121
|
+
provider: string;
|
|
122
|
+
defaultModel: string;
|
|
123
|
+
capabilities?: CompletionModelCapabilities | undefined;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
type AgentGenerationEndArgs<RawResponse = unknown> = {
|
|
127
|
+
turn: number;
|
|
128
|
+
response: CompletionResponse<RawResponse>;
|
|
129
|
+
firstDeltaMs?: number | undefined;
|
|
130
|
+
};
|
|
131
|
+
type AgentGenerationErrorArgs = {
|
|
132
|
+
turn: number;
|
|
133
|
+
error: unknown;
|
|
134
|
+
};
|
|
135
|
+
type AgentGenerationUpdateArgs = {
|
|
136
|
+
turn: number;
|
|
137
|
+
delta: AgentDeltaEvent;
|
|
138
|
+
};
|
|
139
|
+
type AgentRunEventArgs = {
|
|
140
|
+
name: string;
|
|
141
|
+
attributes?: Record<string, JsonValue | undefined> | undefined;
|
|
142
|
+
level?: "DEFAULT" | "WARNING" | "ERROR" | undefined;
|
|
143
|
+
timestamp?: Date | string | undefined;
|
|
144
|
+
};
|
|
145
|
+
type AgentToolStartArgs = {
|
|
146
|
+
turn: number;
|
|
147
|
+
toolCall: ToolCall;
|
|
148
|
+
toolName: string;
|
|
149
|
+
args: string;
|
|
150
|
+
internalCallId: string;
|
|
151
|
+
toolCallId?: string | undefined;
|
|
152
|
+
toolDefinition?: ToolDefinition | undefined;
|
|
153
|
+
toolMetadata?: JsonObject | undefined;
|
|
154
|
+
};
|
|
155
|
+
type AgentToolEndArgs = AgentToolStartArgs & {
|
|
156
|
+
result: string;
|
|
157
|
+
structuredResult?: ToolResultContent[] | undefined;
|
|
158
|
+
skipped: boolean;
|
|
159
|
+
};
|
|
160
|
+
type AgentToolErrorArgs = AgentToolStartArgs & {
|
|
161
|
+
error: unknown;
|
|
162
|
+
};
|
|
163
|
+
type AgentToolStreamEventArgs = AgentToolStartArgs & {
|
|
164
|
+
event: ToolCallStreamEvent;
|
|
165
|
+
};
|
|
166
|
+
interface AgentGenerationObserver {
|
|
167
|
+
end(args: AgentGenerationEndArgs): void | Promise<void>;
|
|
168
|
+
error?(args: AgentGenerationErrorArgs): void | Promise<void>;
|
|
169
|
+
update?(args: AgentGenerationUpdateArgs): void | Promise<void>;
|
|
170
|
+
}
|
|
171
|
+
interface AgentToolObserver {
|
|
172
|
+
streamEvent?(args: AgentToolStreamEventArgs): void | Promise<void>;
|
|
173
|
+
end(args: AgentToolEndArgs): void | Promise<void>;
|
|
174
|
+
error?(args: AgentToolErrorArgs): void | Promise<void>;
|
|
175
|
+
}
|
|
176
|
+
interface AgentRunObserver {
|
|
177
|
+
readonly trace?: AgentTraceInfo | undefined;
|
|
178
|
+
startGeneration?(args: AgentGenerationStartArgs): AgentGenerationObserver | undefined | Promise<AgentGenerationObserver | undefined>;
|
|
179
|
+
startTool?(args: AgentToolStartArgs): AgentToolObserver | undefined | Promise<AgentToolObserver | undefined>;
|
|
180
|
+
end(args: AgentRunEndArgs): void | Promise<void>;
|
|
181
|
+
error?(args: AgentRunErrorArgs): void | Promise<void>;
|
|
182
|
+
event?(args: AgentRunEventArgs): void | Promise<void>;
|
|
183
|
+
}
|
|
184
|
+
interface AgentObserver {
|
|
185
|
+
startRun(args: AgentRunStartArgs): AgentRunObserver | undefined | Promise<AgentRunObserver | undefined>;
|
|
186
|
+
flush?(): void | Promise<void>;
|
|
187
|
+
shutdown?(): void | Promise<void>;
|
|
188
|
+
}
|
|
189
|
+
type AgentObserverRegistration = {
|
|
190
|
+
observer: AgentObserver;
|
|
191
|
+
failOnObserverError?: boolean | undefined;
|
|
192
|
+
};
|
|
193
|
+
type ObserveOptions = {
|
|
194
|
+
failOnObserverError?: boolean | undefined;
|
|
195
|
+
};
|
|
196
|
+
declare function createObserver(observer: AgentObserver): AgentObserver;
|
|
197
|
+
|
|
198
|
+
export { type AgentChildStreamEvent as A, type ObserveOptions as O, type PromptResponse as P, type AgentStreamEvent as a, type AgentObserver as b, type AgentTraceOptions as c, type AgentObserverRegistration as d, type AgentDeltaEvent as e, type AgentGenerationEndArgs as f, type AgentGenerationErrorArgs as g, type AgentGenerationObserver as h, type AgentGenerationStartArgs as i, type AgentGenerationUpdateArgs as j, type AgentRunEndArgs as k, type AgentRunErrorArgs as l, type AgentRunEventArgs as m, type AgentRunObserver as n, type AgentRunPromptRef as o, type AgentRunStartArgs as p, type AgentToolEndArgs as q, type AgentToolErrorArgs as r, type AgentToolObserver as s, type AgentToolStartArgs as t, type AgentToolStreamEventArgs as u, type AgentTraceInfo as v, createObserver as w };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
|
-
export { AgentBuilder
|
|
2
|
-
export { C as CreateCompletionBaseOptions, a as CreateCompletionInput, b as CreateCompletionOptions, c as CreateCompletionResult, d as CreateCompletionStreamOptions, e as CreateParsedCompletionOptions, f as CreateParsedCompletionResult, g as createCompletion, h as createCompletionStream, i as createParsedCompletion } from './create-completion-
|
|
3
|
-
export { A as AssistantContent, a as AssistantMessage, C as CompletionModel, b as CompletionRequest, c as CompletionResponse, D as Document, I as ImageContent, J as JsonObject, d as JsonPrimitive, e as JsonValue, M as Message, S as SystemMessage, T as Text, f as ToolCall, g as ToolContent, h as ToolDefinition, i as ToolMessage, j as ToolResult, k as ToolResultContent, U as Usage, l as UserContent, m as UserMessage } from './types-
|
|
4
|
-
export {
|
|
5
|
-
export { MemoryStore } from './
|
|
6
|
-
export {
|
|
1
|
+
export { AgentBuilder } from './agent/index.js';
|
|
2
|
+
export { C as CreateCompletionBaseOptions, a as CreateCompletionInput, b as CreateCompletionOptions, c as CreateCompletionResult, d as CreateCompletionStreamOptions, e as CreateParsedCompletionOptions, f as CreateParsedCompletionResult, g as createCompletion, h as createCompletionStream, i as createParsedCompletion } from './create-completion-BEbtU5pX.js';
|
|
3
|
+
export { A as AssistantContent, a as AssistantMessage, C as CompletionModel, b as CompletionRequest, c as CompletionResponse, D as Document, I as ImageContent, J as JsonObject, d as JsonPrimitive, e as JsonValue, M as Message, S as SystemMessage, T as Text, f as ToolCall, g as ToolContent, h as ToolDefinition, i as ToolMessage, j as ToolResult, k as ToolResultContent, U as Usage, l as UserContent, m as UserMessage } from './types-C8loR4il.js';
|
|
4
|
+
export { cancelPrompt, createHook, requestToolApproval, runControl, skipTool, toolCallControl } from './hooks/index.js';
|
|
5
|
+
export { M as MemoryStore } from './types-CNfjQ5rB.js';
|
|
6
|
+
export { MaxTurnsError, PromptCancelledError, ToolApprovalRequiredError } from './request/index.js';
|
|
7
|
+
export { A as AgentChildStreamEvent, a as AgentStreamEvent, P as PromptResponse } from './index-CTNzKidE.js';
|
|
7
8
|
export { Z as ZodSchema } from './zod-schema-C7F4clpm.js';
|
|
8
9
|
export { loadSkills, skill } from './skills/index.js';
|
|
9
|
-
export { S as SkillValidationError } from './types-
|
|
10
|
-
export { C as CreateToolOptions, c as createThinkTool, a as createTool } from './think-tool-
|
|
11
|
-
export { A as AnyTool, T as Tool, a as ToolApprovalContext, b as ToolApprovalDecision, c as ToolApprovalPolicy, d as ToolApprovalRequest, e as ToolApprovalsOptions, f as ToolCallContext, g as ToolCallStreamEvent } from './tool-
|
|
12
|
-
export { A as AgentMiddleware, C as CompletionRequestMiddlewareArgs, a as CompletionRequestMiddlewareResult, b as CompletionResponseMiddlewareArgs, c as CompletionResponseMiddlewareResult, T as ToolInputMiddlewareArgs, d as ToolInputMiddlewareResult, e as ToolMiddleware, f as ToolOutputMiddlewareArgs, g as ToolOutputMiddlewareResult, h as ToolResultMiddlewareArgs, i as createMiddleware, j as createToolMiddleware } from './middleware-
|
|
13
|
-
export {
|
|
14
|
-
import './
|
|
10
|
+
export { S as SkillValidationError } from './types-D-YTJdLj.js';
|
|
11
|
+
export { C as CreateToolOptions, c as createThinkTool, a as createTool } from './think-tool-B8Rqw2i_.js';
|
|
12
|
+
export { A as AnyTool, T as Tool, a as ToolApprovalContext, b as ToolApprovalDecision, c as ToolApprovalPolicy, d as ToolApprovalRequest, e as ToolApprovalsOptions, f as ToolCallContext, g as ToolCallStreamEvent } from './tool-BLypI31e.js';
|
|
13
|
+
export { A as AgentMiddleware, C as CompletionRequestMiddlewareArgs, a as CompletionRequestMiddlewareResult, b as CompletionResponseMiddlewareArgs, c as CompletionResponseMiddlewareResult, T as ToolInputMiddlewareArgs, d as ToolInputMiddlewareResult, e as ToolMiddleware, f as ToolOutputMiddlewareArgs, g as ToolOutputMiddlewareResult, h as ToolResultMiddlewareArgs, i as createMiddleware, j as createToolMiddleware } from './middleware-CG7l0rR3.js';
|
|
14
|
+
export { UIError, UIMessage, UIMessagePart, UIMessageRole, UIStreamEvent, UIStreamRequest, coreMessagesToUIMessages, uiMessagesToCoreMessages } from './ui/index.js';
|
|
15
|
+
import './agent-Bq2KtSaw.js';
|
|
16
|
+
import './types-CCxnnJjU.js';
|
|
17
|
+
import './types-KKjICcrA.js';
|
|
18
|
+
import './types-DHHZztGO.js';
|
|
19
|
+
import './types-CIwm9p4w.js';
|
|
15
20
|
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
16
21
|
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
17
22
|
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
18
|
-
import './observability/index.js';
|
|
19
|
-
import './types-B0cuzkjP.js';
|
|
20
|
-
import './types-IB2e9u5M.js';
|
|
21
23
|
import 'zod';
|
package/dist/index.js
CHANGED
|
@@ -1,42 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
agentStreamToUIStream,
|
|
3
|
-
completionStreamToUIStream,
|
|
4
|
-
coreMessagesToUIMessages,
|
|
5
|
-
createAgentUIStream,
|
|
6
|
-
createCompletionUIStream,
|
|
7
|
-
uiMessagesToCoreMessages
|
|
8
|
-
} from "./chunk-AREAH2X5.js";
|
|
9
1
|
import {
|
|
10
2
|
SkillValidationError,
|
|
11
3
|
loadSkills,
|
|
12
4
|
skill
|
|
13
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-V3WTT7TE.js";
|
|
6
|
+
import {
|
|
7
|
+
coreMessagesToUIMessages,
|
|
8
|
+
uiMessagesToCoreMessages
|
|
9
|
+
} from "./chunk-DOS77JZC.js";
|
|
14
10
|
import {
|
|
15
11
|
AgentBuilder
|
|
16
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-62RUYGMM.js";
|
|
13
|
+
import "./chunk-N6REB3DH.js";
|
|
17
14
|
import {
|
|
18
15
|
MaxTurnsError,
|
|
19
16
|
PromptCancelledError,
|
|
20
|
-
ToolApprovalRequiredError
|
|
21
|
-
|
|
22
|
-
createHook,
|
|
23
|
-
requestToolApproval,
|
|
24
|
-
runControl,
|
|
25
|
-
skipTool,
|
|
26
|
-
toolCallControl
|
|
27
|
-
} from "./chunk-IFQVPV32.js";
|
|
28
|
-
import "./chunk-XUUY2L2D.js";
|
|
29
|
-
import "./chunk-XXT2UCAR.js";
|
|
17
|
+
ToolApprovalRequiredError
|
|
18
|
+
} from "./chunk-SISUMRUX.js";
|
|
30
19
|
import "./chunk-YK4WAAS4.js";
|
|
20
|
+
import "./chunk-XUUY2L2D.js";
|
|
31
21
|
import {
|
|
32
22
|
createMiddleware,
|
|
33
23
|
createThinkTool,
|
|
34
24
|
createToolMiddleware
|
|
35
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-X3SWV6W6.js";
|
|
36
26
|
import {
|
|
37
27
|
createTool
|
|
38
|
-
} from "./chunk-
|
|
39
|
-
import "./chunk-
|
|
28
|
+
} from "./chunk-5EDHXYH5.js";
|
|
29
|
+
import "./chunk-M6RPFHG7.js";
|
|
30
|
+
import "./chunk-JS5MGVNM.js";
|
|
31
|
+
import "./chunk-OIMLU4SF.js";
|
|
40
32
|
import {
|
|
41
33
|
createCompletion,
|
|
42
34
|
createCompletionStream,
|
|
@@ -50,8 +42,15 @@ import {
|
|
|
50
42
|
UserContent
|
|
51
43
|
} from "./chunk-WJO6NVE2.js";
|
|
52
44
|
import "./chunk-WQKHFADH.js";
|
|
53
|
-
import
|
|
54
|
-
|
|
45
|
+
import {
|
|
46
|
+
cancelPrompt,
|
|
47
|
+
createHook,
|
|
48
|
+
requestToolApproval,
|
|
49
|
+
runControl,
|
|
50
|
+
skipTool,
|
|
51
|
+
toolCallControl
|
|
52
|
+
} from "./chunk-NPZDYOE6.js";
|
|
53
|
+
import "./chunk-MMHG7WAM.js";
|
|
55
54
|
export {
|
|
56
55
|
AgentBuilder,
|
|
57
56
|
AssistantContent,
|
|
@@ -63,14 +62,10 @@ export {
|
|
|
63
62
|
ToolContent,
|
|
64
63
|
Usage,
|
|
65
64
|
UserContent,
|
|
66
|
-
agentStreamToUIStream,
|
|
67
65
|
cancelPrompt,
|
|
68
|
-
completionStreamToUIStream,
|
|
69
66
|
coreMessagesToUIMessages,
|
|
70
|
-
createAgentUIStream,
|
|
71
67
|
createCompletion,
|
|
72
68
|
createCompletionStream,
|
|
73
|
-
createCompletionUIStream,
|
|
74
69
|
createHook,
|
|
75
70
|
createMiddleware,
|
|
76
71
|
createParsedCompletion,
|
package/dist/internal/agent.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { A as Agent,
|
|
2
|
-
import '../types-
|
|
3
|
-
import '../
|
|
4
|
-
import '../
|
|
5
|
-
import '../
|
|
6
|
-
import '../
|
|
7
|
-
import '../
|
|
8
|
-
import '../types-
|
|
9
|
-
import '../types-
|
|
1
|
+
export { A as Agent, d as AgentEventAppendInput, e as AgentEventRecord, b as AgentEventStore, f as AgentEventStoreInclude, c as AgentEventStoreOptions, i as AgentEventStoreRegistration, j as AgentOptions, g as AgentSession, h as AgentToolOptions, k as DEFAULT_MAX_TURNS, D as DynamicContextOptions, l as DynamicContextRegistration, a as DynamicToolOptions, m as DynamicToolRegistration } from '../agent-Bq2KtSaw.js';
|
|
2
|
+
import '../types-C8loR4il.js';
|
|
3
|
+
import '../types-CCxnnJjU.js';
|
|
4
|
+
import '../types-CNfjQ5rB.js';
|
|
5
|
+
import '../index-CTNzKidE.js';
|
|
6
|
+
import '../tool-BLypI31e.js';
|
|
7
|
+
import '../middleware-CG7l0rR3.js';
|
|
8
|
+
import '../types-DHHZztGO.js';
|
|
9
|
+
import '../types-KKjICcrA.js';
|
package/dist/internal/agent.js
CHANGED
|
@@ -2,17 +2,19 @@ import {
|
|
|
2
2
|
Agent,
|
|
3
3
|
AgentSession,
|
|
4
4
|
DEFAULT_MAX_TURNS
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-
|
|
5
|
+
} from "../chunk-N6REB3DH.js";
|
|
6
|
+
import "../chunk-SISUMRUX.js";
|
|
7
7
|
import "../chunk-YK4WAAS4.js";
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
8
|
+
import "../chunk-XUUY2L2D.js";
|
|
9
|
+
import "../chunk-X3SWV6W6.js";
|
|
10
|
+
import "../chunk-5EDHXYH5.js";
|
|
11
|
+
import "../chunk-JS5MGVNM.js";
|
|
12
|
+
import "../chunk-OIMLU4SF.js";
|
|
11
13
|
import "../chunk-5G3VME2D.js";
|
|
12
14
|
import "../chunk-WJO6NVE2.js";
|
|
13
15
|
import "../chunk-WQKHFADH.js";
|
|
14
|
-
import "../chunk-
|
|
15
|
-
import "../chunk-
|
|
16
|
+
import "../chunk-NPZDYOE6.js";
|
|
17
|
+
import "../chunk-MMHG7WAM.js";
|
|
16
18
|
export {
|
|
17
19
|
Agent,
|
|
18
20
|
AgentSession,
|
package/dist/loaders/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { D as Document } from '../types-
|
|
1
|
+
import { D as Document } from '../types-C8loR4il.js';
|
|
2
2
|
|
|
3
3
|
type LoaderResult<T> = {
|
|
4
4
|
ok: true;
|
|
@@ -7,6 +7,11 @@ type LoaderResult<T> = {
|
|
|
7
7
|
ok: false;
|
|
8
8
|
error: unknown;
|
|
9
9
|
};
|
|
10
|
+
type LoaderValue$1<T> = T extends {
|
|
11
|
+
ok: true;
|
|
12
|
+
value: infer Value;
|
|
13
|
+
} ? Value : never;
|
|
14
|
+
type UnwrapLoaderResult$1<T> = [LoaderValue$1<T>] extends [never] ? T : LoaderValue$1<T>;
|
|
10
15
|
type FileSource = {
|
|
11
16
|
path: string;
|
|
12
17
|
} | {
|
|
@@ -17,6 +22,7 @@ type FileReadWithPath = {
|
|
|
17
22
|
path: string;
|
|
18
23
|
text: string;
|
|
19
24
|
};
|
|
25
|
+
type FileMode = "source" | "read" | "readWithPath";
|
|
20
26
|
type PdfSource = {
|
|
21
27
|
path: string;
|
|
22
28
|
} | {
|
|
@@ -44,11 +50,6 @@ declare function pdfLoaderToDocuments(loader: AsyncIterable<PdfReadWithPath>): P
|
|
|
44
50
|
declare function pdfPageToDocument(page: PdfPageWithPath): Document;
|
|
45
51
|
declare function pdfPageLoaderToDocuments(loader: AsyncIterable<PdfPageWithPath>): Promise<Document[]>;
|
|
46
52
|
|
|
47
|
-
type LoaderValue$1<T> = T extends {
|
|
48
|
-
ok: true;
|
|
49
|
-
value: infer Value;
|
|
50
|
-
} ? Value : never;
|
|
51
|
-
type UnwrapLoaderResult$1<T> = [LoaderValue$1<T>] extends [never] ? T : LoaderValue$1<T>;
|
|
52
53
|
declare class FileLoader<T = LoaderResult<FileSource>> implements AsyncIterable<T> {
|
|
53
54
|
private readonly source;
|
|
54
55
|
private readonly mode;
|
|
@@ -87,4 +88,4 @@ declare class PdfFileLoader<T = LoaderResult<PdfSource>> implements AsyncIterabl
|
|
|
87
88
|
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
export { FileLoader, type FileReadWithPath, type FileSource, type LoaderResult, PdfFileLoader, type PdfPage, type PdfPageWithPath, type PdfReadWithPath, type PdfSource, fileLoaderToDocuments, fileToDocument, pdfLoaderToDocuments, pdfPageLoaderToDocuments, pdfPageToDocument, pdfToDocument };
|
|
91
|
+
export { FileLoader, type FileMode, type FileReadWithPath, type FileSource, type LoaderResult, type LoaderValue$1 as LoaderValue, PdfFileLoader, type PdfPage, type PdfPageWithPath, type PdfReadWithPath, type PdfSource, type UnwrapLoaderResult$1 as UnwrapLoaderResult, fileLoaderToDocuments, fileToDocument, pdfLoaderToDocuments, pdfPageLoaderToDocuments, pdfPageToDocument, pdfToDocument };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/loaders/documents.ts","../../src/loaders/file.ts","../../src/loaders/shared.ts","../../src/loaders/pdf.ts"],"sourcesContent":["import type { Document } from \"../completion\";\nimport type { FileReadWithPath, PdfPageWithPath, PdfReadWithPath } from \"./types\";\n\nexport function fileToDocument(file: FileReadWithPath): Document {\n return {\n id: file.path,\n text: file.text,\n additionalProps: {\n source: file.path,\n mediaType: \"text/plain\",\n },\n };\n}\n\nexport async function fileLoaderToDocuments(\n loader: AsyncIterable<FileReadWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const file of loader) {\n documents.push(fileToDocument(file));\n }\n return documents;\n}\n\nexport function pdfToDocument(pdf: PdfReadWithPath): Document {\n return {\n id: pdf.path,\n text: pdf.text,\n additionalProps: {\n source: pdf.path,\n mediaType: \"application/pdf\",\n },\n };\n}\n\nexport async function pdfLoaderToDocuments(\n loader: AsyncIterable<PdfReadWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const pdf of loader) {\n documents.push(pdfToDocument(pdf));\n }\n return documents;\n}\n\nexport function pdfPageToDocument(page: PdfPageWithPath): Document {\n return {\n id: `${page.path}#page=${page.pageNumber}`,\n text: page.text,\n additionalProps: {\n source: page.path,\n mediaType: \"application/pdf\",\n pageNumber: String(page.pageNumber),\n },\n };\n}\n\nexport async function pdfPageLoaderToDocuments(\n loader: AsyncIterable<PdfPageWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const page of loader) {\n documents.push(pdfPageToDocument(page));\n }\n return documents;\n}\n","import { readdir, readFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { err, ok, sortedGlob, toUint8Array } from \"./shared\";\nimport type { FileReadWithPath, FileSource, LoaderResult } from \"./types\";\n\ntype FileMode = \"source\" | \"read\" | \"readWithPath\";\ntype LoaderValue<T> = T extends { ok: true; value: infer Value } ? Value : never;\ntype UnwrapLoaderResult<T> = [LoaderValue<T>] extends [never] ? T : LoaderValue<T>;\n\nexport class FileLoader<T = LoaderResult<FileSource>> implements AsyncIterable<T> {\n private constructor(\n private readonly source: () => AsyncIterable<LoaderResult<FileSource>>,\n private readonly mode: FileMode,\n private readonly skipErrors: boolean,\n ) {}\n\n static withGlob(pattern: string): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n for (const path of await sortedGlob(pattern)) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static withDir(directory: string): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n const entries = await readdir(directory, { withFileTypes: true });\n const files = entries\n .filter((entry) => entry.isFile())\n .map((entry) => join(directory, entry.name))\n .sort((left, right) => left.localeCompare(right));\n for (const path of files) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static fromBytes(bytes: Uint8Array | ArrayBuffer): FileLoader<LoaderResult<FileSource>> {\n return FileLoader.fromBytesMany([bytes]);\n }\n\n static fromBytesMany(\n bytes: Array<Uint8Array | ArrayBuffer>,\n ): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n for (const value of bytes) {\n yield ok({ path: \"<memory>\", bytes: toUint8Array(value) });\n }\n },\n \"source\",\n false,\n );\n }\n\n read(): FileLoader<LoaderResult<string>> {\n return new FileLoader(this.source, \"read\", this.skipErrors);\n }\n\n readWithPath(): FileLoader<LoaderResult<FileReadWithPath>> {\n return new FileLoader(this.source, \"readWithPath\", this.skipErrors);\n }\n\n ignoreErrors(): FileLoader<UnwrapLoaderResult<T>> {\n return new FileLoader(this.source, this.mode, true) as FileLoader<UnwrapLoaderResult<T>>;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<T> {\n for await (const item of this.source()) {\n const result = await mapFileResult(item, this.mode);\n if (!result.ok && this.skipErrors) {\n continue;\n }\n yield (this.skipErrors && result.ok ? result.value : result) as T;\n }\n }\n}\n\nasync function mapFileResult(\n item: LoaderResult<FileSource>,\n mode: FileMode,\n): Promise<LoaderResult<FileSource | string | FileReadWithPath>> {\n if (!item.ok) {\n return item;\n }\n if (mode === \"source\") {\n return item;\n }\n try {\n const text = await readFileSource(item.value);\n return mode === \"read\" ? ok(text) : ok({ path: item.value.path, text });\n } catch (error) {\n return err(error);\n }\n}\n\nasync function readFileSource(source: FileSource): Promise<string> {\n if (\"bytes\" in source) {\n return new TextDecoder().decode(source.bytes);\n }\n return readFile(source.path, \"utf8\");\n}\n","import { isAbsolute } from \"node:path\";\nimport { glob } from \"tinyglobby\";\nimport type { LoaderResult } from \"./types\";\n\nexport function ok<T>(value: T): LoaderResult<T> {\n return { ok: true, value };\n}\n\nexport function err(error: unknown): LoaderResult<never> {\n return { ok: false, error };\n}\n\nexport async function sortedGlob(pattern: string): Promise<string[]> {\n return (await glob(pattern, { absolute: isAbsolute(pattern), onlyFiles: true })).sort(\n (left, right) => left.localeCompare(right),\n );\n}\n\nexport function 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","import { readdir, readFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { err, ok, sortedGlob, toUint8Array } from \"./shared\";\nimport type { LoaderResult, PdfPage, PdfPageWithPath, PdfReadWithPath, PdfSource } from \"./types\";\n\ntype PdfMode = \"source\" | \"read\" | \"readWithPath\" | \"byPage\" | \"byPageWithPath\";\ntype LoaderValue<T> = T extends { ok: true; value: infer Value } ? Value : never;\ntype UnwrapLoaderResult<T> = [LoaderValue<T>] extends [never] ? T : LoaderValue<T>;\n\nexport class PdfFileLoader<T = LoaderResult<PdfSource>> implements AsyncIterable<T> {\n private constructor(\n private readonly source: () => AsyncIterable<LoaderResult<PdfSource>>,\n private readonly mode: PdfMode,\n private readonly skipErrors: boolean,\n ) {}\n\n static withGlob(pattern: string): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n for (const path of await sortedGlob(pattern)) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static withDir(directory: string): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n const entries = await readdir(directory, { withFileTypes: true });\n const files = entries\n .filter((entry) => entry.isFile())\n .map((entry) => join(directory, entry.name))\n .sort((left, right) => left.localeCompare(right));\n for (const path of files) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static fromBytes(bytes: Uint8Array | ArrayBuffer): PdfFileLoader<LoaderResult<PdfSource>> {\n return PdfFileLoader.fromBytesMany([bytes]);\n }\n\n static fromBytesMany(\n bytes: Array<Uint8Array | ArrayBuffer>,\n ): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n for (const value of bytes) {\n yield ok({ path: \"<memory>\", bytes: toUint8Array(value) });\n }\n },\n \"source\",\n false,\n );\n }\n\n read(): PdfFileLoader<LoaderResult<string>> {\n return new PdfFileLoader(this.source, \"read\", this.skipErrors);\n }\n\n readWithPath(): PdfFileLoader<LoaderResult<PdfReadWithPath>> {\n return new PdfFileLoader(this.source, \"readWithPath\", this.skipErrors);\n }\n\n byPage(\n this: PdfFileLoader<LoaderResult<PdfReadWithPath>>,\n ): PdfFileLoader<LoaderResult<PdfPageWithPath>>;\n byPage(this: PdfFileLoader<PdfReadWithPath>): PdfFileLoader<PdfPageWithPath>;\n byPage(): PdfFileLoader<LoaderResult<PdfPage>>;\n byPage(): PdfFileLoader<LoaderResult<PdfPage> | LoaderResult<PdfPageWithPath> | PdfPageWithPath> {\n const mode = this.mode === \"readWithPath\" ? \"byPageWithPath\" : \"byPage\";\n return new PdfFileLoader(this.source, mode, this.skipErrors);\n }\n\n ignoreErrors(): PdfFileLoader<UnwrapLoaderResult<T>> {\n return new PdfFileLoader(this.source, this.mode, true) as PdfFileLoader<UnwrapLoaderResult<T>>;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<T> {\n for await (const item of this.source()) {\n const results = await mapPdfResult(item, this.mode);\n for (const result of results) {\n if (!result.ok && this.skipErrors) {\n continue;\n }\n yield (this.skipErrors && result.ok ? result.value : result) as T;\n }\n }\n }\n}\n\nasync function mapPdfResult(\n item: LoaderResult<PdfSource>,\n mode: PdfMode,\n): Promise<Array<LoaderResult<PdfSource | string | PdfReadWithPath | PdfPage | PdfPageWithPath>>> {\n if (!item.ok) {\n return [item];\n }\n if (mode === \"source\") {\n return [item];\n }\n try {\n const pages = await readPdfPages(item.value);\n if (mode === \"read\") {\n return [ok(pages.map((page) => page.text).join(\"\"))];\n }\n if (mode === \"readWithPath\") {\n return [ok({ path: item.value.path, text: pages.map((page) => page.text).join(\"\") })];\n }\n if (mode === \"byPageWithPath\") {\n return pages.map((page) => ok({ path: item.value.path, ...page }));\n }\n return pages.map(ok);\n } catch (error) {\n return [err(error)];\n }\n}\n\nasync function readPdfPages(source: PdfSource): Promise<PdfPage[]> {\n const pdfjs = await import(\"pdfjs-dist/legacy/build/pdf.mjs\");\n const bytes = \"bytes\" in source ? source.bytes : toUint8Array(await readFile(source.path));\n const loadingTask = pdfjs.getDocument({ data: bytes });\n const document = await loadingTask.promise;\n const pages: PdfPage[] = [];\n try {\n for (let index = 1; index <= document.numPages; index += 1) {\n const page = await document.getPage(index);\n const content = await page.getTextContent();\n const text = content.items\n .flatMap((item) =>\n typeof item === \"object\" && item !== null && \"str\" in item ? [String(item.str)] : [],\n )\n .join(\"\\n\");\n pages.push({ pageNumber: index - 1, text: text.length > 0 ? `${text}\\n` : \"\" });\n }\n } finally {\n await loadingTask.destroy();\n }\n return pages;\n}\n"],"mappings":";AAGO,SAAS,eAAe,MAAkC;AAC/D,SAAO;AAAA,IACL,IAAI,KAAK;AAAA,IACT,MAAM,KAAK;AAAA,IACX,iBAAiB;AAAA,MACf,QAAQ,KAAK;AAAA,MACb,WAAW;AAAA,IACb;AAAA,EACF;AACF;AAEA,eAAsB,sBACpB,QACqB;AACrB,QAAM,YAAwB,CAAC;AAC/B,mBAAiB,QAAQ,QAAQ;AAC/B,cAAU,KAAK,eAAe,IAAI,CAAC;AAAA,EACrC;AACA,SAAO;AACT;AAEO,SAAS,cAAc,KAAgC;AAC5D,SAAO;AAAA,IACL,IAAI,IAAI;AAAA,IACR,MAAM,IAAI;AAAA,IACV,iBAAiB;AAAA,MACf,QAAQ,IAAI;AAAA,MACZ,WAAW;AAAA,IACb;AAAA,EACF;AACF;AAEA,eAAsB,qBACpB,QACqB;AACrB,QAAM,YAAwB,CAAC;AAC/B,mBAAiB,OAAO,QAAQ;AAC9B,cAAU,KAAK,cAAc,GAAG,CAAC;AAAA,EACnC;AACA,SAAO;AACT;AAEO,SAAS,kBAAkB,MAAiC;AACjE,SAAO;AAAA,IACL,IAAI,GAAG,KAAK,IAAI,SAAS,KAAK,UAAU;AAAA,IACxC,MAAM,KAAK;AAAA,IACX,iBAAiB;AAAA,MACf,QAAQ,KA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QAAQ,GAAG,MAAM,KAAK,SAAS,IAAI,GAAG,IAAI;AAAA,IAAO,GAAG,CAAC;AAAA,IAChF;AAAA,EACF,UAAE;AACA,UAAM,YAAY,QAAQ;AAAA,EAC5B;AACA,SAAO;AACT;","names":["readdir","readFile","join","readdir","join","readFile"]}
|
|
1
|
+
{"version":3,"sources":["../../src/loaders/documents.ts","../../src/loaders/file.ts","../../src/loaders/shared.ts","../../src/loaders/pdf.ts"],"sourcesContent":["import type { Document } from \"../completion\";\nimport type { FileReadWithPath, PdfPageWithPath, PdfReadWithPath } from \"./types\";\n\nexport function fileToDocument(file: FileReadWithPath): Document {\n return {\n id: file.path,\n text: file.text,\n additionalProps: {\n source: file.path,\n mediaType: \"text/plain\",\n },\n };\n}\n\nexport async function fileLoaderToDocuments(\n loader: AsyncIterable<FileReadWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const file of loader) {\n documents.push(fileToDocument(file));\n }\n return documents;\n}\n\nexport function pdfToDocument(pdf: PdfReadWithPath): Document {\n return {\n id: pdf.path,\n text: pdf.text,\n additionalProps: {\n source: pdf.path,\n mediaType: \"application/pdf\",\n },\n };\n}\n\nexport async function pdfLoaderToDocuments(\n loader: AsyncIterable<PdfReadWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const pdf of loader) {\n documents.push(pdfToDocument(pdf));\n }\n return documents;\n}\n\nexport function pdfPageToDocument(page: PdfPageWithPath): Document {\n return {\n id: `${page.path}#page=${page.pageNumber}`,\n text: page.text,\n additionalProps: {\n source: page.path,\n mediaType: \"application/pdf\",\n pageNumber: String(page.pageNumber),\n },\n };\n}\n\nexport async function pdfPageLoaderToDocuments(\n loader: AsyncIterable<PdfPageWithPath>,\n): Promise<Document[]> {\n const documents: Document[] = [];\n for await (const page of loader) {\n documents.push(pdfPageToDocument(page));\n }\n return documents;\n}\n","import { readdir, readFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { err, ok, sortedGlob, toUint8Array } from \"./shared\";\nimport type {\n FileMode,\n FileReadWithPath,\n FileSource,\n LoaderResult,\n UnwrapLoaderResult,\n} from \"./types\";\n\nexport class FileLoader<T = LoaderResult<FileSource>> implements AsyncIterable<T> {\n private constructor(\n private readonly source: () => AsyncIterable<LoaderResult<FileSource>>,\n private readonly mode: FileMode,\n private readonly skipErrors: boolean,\n ) {}\n\n static withGlob(pattern: string): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n for (const path of await sortedGlob(pattern)) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static withDir(directory: string): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n const entries = await readdir(directory, { withFileTypes: true });\n const files = entries\n .filter((entry) => entry.isFile())\n .map((entry) => join(directory, entry.name))\n .sort((left, right) => left.localeCompare(right));\n for (const path of files) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static fromBytes(bytes: Uint8Array | ArrayBuffer): FileLoader<LoaderResult<FileSource>> {\n return FileLoader.fromBytesMany([bytes]);\n }\n\n static fromBytesMany(\n bytes: Array<Uint8Array | ArrayBuffer>,\n ): FileLoader<LoaderResult<FileSource>> {\n return new FileLoader(\n async function* () {\n for (const value of bytes) {\n yield ok({ path: \"<memory>\", bytes: toUint8Array(value) });\n }\n },\n \"source\",\n false,\n );\n }\n\n read(): FileLoader<LoaderResult<string>> {\n return new FileLoader(this.source, \"read\", this.skipErrors);\n }\n\n readWithPath(): FileLoader<LoaderResult<FileReadWithPath>> {\n return new FileLoader(this.source, \"readWithPath\", this.skipErrors);\n }\n\n ignoreErrors(): FileLoader<UnwrapLoaderResult<T>> {\n return new FileLoader(this.source, this.mode, true) as FileLoader<UnwrapLoaderResult<T>>;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<T> {\n for await (const item of this.source()) {\n const result = await mapFileResult(item, this.mode);\n if (!result.ok && this.skipErrors) {\n continue;\n }\n yield (this.skipErrors && result.ok ? result.value : result) as T;\n }\n }\n}\n\nasync function mapFileResult(\n item: LoaderResult<FileSource>,\n mode: FileMode,\n): Promise<LoaderResult<FileSource | string | FileReadWithPath>> {\n if (!item.ok) {\n return item;\n }\n if (mode === \"source\") {\n return item;\n }\n try {\n const text = await readFileSource(item.value);\n return mode === \"read\" ? ok(text) : ok({ path: item.value.path, text });\n } catch (error) {\n return err(error);\n }\n}\n\nasync function readFileSource(source: FileSource): Promise<string> {\n if (\"bytes\" in source) {\n return new TextDecoder().decode(source.bytes);\n }\n return readFile(source.path, \"utf8\");\n}\n","import { isAbsolute } from \"node:path\";\nimport { glob } from \"tinyglobby\";\nimport type { LoaderResult } from \"./types\";\n\nexport function ok<T>(value: T): LoaderResult<T> {\n return { ok: true, value };\n}\n\nexport function err(error: unknown): LoaderResult<never> {\n return { ok: false, error };\n}\n\nexport async function sortedGlob(pattern: string): Promise<string[]> {\n return (await glob(pattern, { absolute: isAbsolute(pattern), onlyFiles: true })).sort(\n (left, right) => left.localeCompare(right),\n );\n}\n\nexport function 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","import { readdir, readFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { err, ok, sortedGlob, toUint8Array } from \"./shared\";\nimport type { LoaderResult, PdfPage, PdfPageWithPath, PdfReadWithPath, PdfSource } from \"./types\";\n\ntype PdfMode = \"source\" | \"read\" | \"readWithPath\" | \"byPage\" | \"byPageWithPath\";\ntype LoaderValue<T> = T extends { ok: true; value: infer Value } ? Value : never;\ntype UnwrapLoaderResult<T> = [LoaderValue<T>] extends [never] ? T : LoaderValue<T>;\n\nexport class PdfFileLoader<T = LoaderResult<PdfSource>> implements AsyncIterable<T> {\n private constructor(\n private readonly source: () => AsyncIterable<LoaderResult<PdfSource>>,\n private readonly mode: PdfMode,\n private readonly skipErrors: boolean,\n ) {}\n\n static withGlob(pattern: string): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n for (const path of await sortedGlob(pattern)) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static withDir(directory: string): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n const entries = await readdir(directory, { withFileTypes: true });\n const files = entries\n .filter((entry) => entry.isFile())\n .map((entry) => join(directory, entry.name))\n .sort((left, right) => left.localeCompare(right));\n for (const path of files) {\n yield ok({ path });\n }\n },\n \"source\",\n false,\n );\n }\n\n static fromBytes(bytes: Uint8Array | ArrayBuffer): PdfFileLoader<LoaderResult<PdfSource>> {\n return PdfFileLoader.fromBytesMany([bytes]);\n }\n\n static fromBytesMany(\n bytes: Array<Uint8Array | ArrayBuffer>,\n ): PdfFileLoader<LoaderResult<PdfSource>> {\n return new PdfFileLoader(\n async function* () {\n for (const value of bytes) {\n yield ok({ path: \"<memory>\", bytes: toUint8Array(value) });\n }\n },\n \"source\",\n false,\n );\n }\n\n read(): PdfFileLoader<LoaderResult<string>> {\n return new PdfFileLoader(this.source, \"read\", this.skipErrors);\n }\n\n readWithPath(): PdfFileLoader<LoaderResult<PdfReadWithPath>> {\n return new PdfFileLoader(this.source, \"readWithPath\", this.skipErrors);\n }\n\n byPage(\n this: PdfFileLoader<LoaderResult<PdfReadWithPath>>,\n ): PdfFileLoader<LoaderResult<PdfPageWithPath>>;\n byPage(this: PdfFileLoader<PdfReadWithPath>): PdfFileLoader<PdfPageWithPath>;\n byPage(): PdfFileLoader<LoaderResult<PdfPage>>;\n byPage(): PdfFileLoader<LoaderResult<PdfPage> | LoaderResult<PdfPageWithPath> | PdfPageWithPath> {\n const mode = this.mode === \"readWithPath\" ? \"byPageWithPath\" : \"byPage\";\n return new PdfFileLoader(this.source, mode, this.skipErrors);\n }\n\n ignoreErrors(): PdfFileLoader<UnwrapLoaderResult<T>> {\n return new PdfFileLoader(this.source, this.mode, true) as PdfFileLoader<UnwrapLoaderResult<T>>;\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<T> {\n for await (const item of this.source()) {\n const results = await mapPdfResult(item, this.mode);\n for (const result of results) {\n if (!result.ok && this.skipErrors) {\n continue;\n }\n yield (this.skipErrors && result.ok ? result.value : result) as T;\n }\n }\n }\n}\n\nasync function mapPdfResult(\n item: LoaderResult<PdfSource>,\n mode: PdfMode,\n): Promise<Array<LoaderResult<PdfSource | string | PdfReadWithPath | PdfPage | PdfPageWithPath>>> {\n if (!item.ok) {\n return [item];\n }\n if (mode === \"source\") {\n return [item];\n }\n try {\n const pages = await readPdfPages(item.value);\n if (mode === \"read\") {\n return [ok(pages.map((page) => page.text).join(\"\"))];\n }\n if (mode === \"readWithPath\") {\n return [ok({ path: item.value.path, text: pages.map((page) => page.text).join(\"\") })];\n }\n if (mode === \"byPageWithPath\") {\n return pages.map((page) => ok({ path: item.value.path, ...page }));\n }\n return pages.map(ok);\n } catch (error) {\n return [err(error)];\n }\n}\n\nasync function readPdfPages(source: PdfSource): Promise<PdfPage[]> {\n const pdfjs = await import(\"pdfjs-dist/legacy/build/pdf.mjs\");\n const bytes = \"bytes\" in source ? source.bytes : toUint8Array(await readFile(source.path));\n const loadingTask = pdfjs.getDocument({ data: bytes });\n const document = await loadingTask.promise;\n const pages: PdfPage[] = [];\n try {\n for (let index = 1; index <= document.numPages; index += 1) {\n const page = await document.getPage(index);\n const content = await page.getTextContent();\n const text = content.items\n .flatMap((item) =>\n typeof item === \"object\" && item !== null && \"str\" in item ? [String(item.str)] : [],\n )\n .join(\"\\n\");\n pages.push({ pageNumber: index - 1, text: text.length > 0 ? `${text}\\n` : \"\" });\n }\n } finally {\n await loadingTask.destroy();\n }\n return pages;\n}\n"],"mappings":";AAGO,SAAS,eAAe,MAAkC;AAC/D,SAAO;AAAA,IACL,IAAI,KAAK;AAAA,IACT,MAAM,KAAK;AAAA,IACX,iBAAiB;AAAA,MACf,QAAQ,KAAK;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AAO;AACd,WAAO,CAAC,IAAI,KAAK,CAAC;AAAA,EACpB;AACF;AAEA,eAAe,aAAa,QAAuC;AACjE,QAAM,QAAQ,MAAM,OAAO,iCAAiC;AAC5D,QAAM,QAAQ,WAAW,SAAS,OAAO,QAAQ,aAAa,MAAMC,UAAS,OAAO,IAAI,CAAC;AACzF,QAAM,cAAc,MAAM,YAAY,EAAE,MAAM,MAAM,CAAC;AACrD,QAAM,WAAW,MAAM,YAAY;AACnC,QAAM,QAAmB,CAAC;AAC1B,MAAI;AACF,aAAS,QAAQ,GAAG,SAAS,SAAS,UAAU,SAAS,GAAG;AAC1D,YAAM,OAAO,MAAM,SAAS,QAAQ,KAAK;AACzC,YAAM,UAAU,MAAM,KAAK,eAAe;AAC1C,YAAM,OAAO,QAAQ,MAClB;AAAA,QAAQ,CAAC,SACR,OAAO,SAAS,YAAY,SAAS,QAAQ,SAAS,OAAO,CAAC,OAAO,KAAK,GAAG,CAAC,IAAI,CAAC;AAAA,MACrF,EACC,KAAK,IAAI;AACZ,YAAM,KAAK,EAAE,YAAY,QAAQ,GAAG,MAAM,KAAK,SAAS,IAAI,GAAG,IAAI;AAAA,IAAO,GAAG,CAAC;AAAA,IAChF;AAAA,EACF,UAAE;AACA,UAAM,YAAY,QAAQ;AAAA,EAC5B;AACA,SAAO;AACT;","names":["readdir","readFile","join","readdir","join","readFile"]}
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { M as McpConnection, a as McpServer, b as McpStdioOptions, c as McpHttpOptions, d as McpSseOptions } from '../types-
|
|
2
|
-
export { e as McpClient, f as McpToolCallContent, g as McpToolCallResult, h as McpToolDefinition } from '../types-
|
|
1
|
+
import { M as McpConnection, a as McpServer, b as McpStdioOptions, c as McpHttpOptions, d as McpSseOptions } from '../types-CIwm9p4w.js';
|
|
2
|
+
export { e as McpClient, f as McpToolCallContent, g as McpToolCallResult, h as McpToolDefinition } from '../types-CIwm9p4w.js';
|
|
3
3
|
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
4
4
|
import '@modelcontextprotocol/sdk/client/stdio.js';
|
|
5
5
|
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
6
|
-
import '../types-
|
|
7
|
-
import '../tool-
|
|
6
|
+
import '../types-C8loR4il.js';
|
|
7
|
+
import '../tool-BLypI31e.js';
|
|
8
8
|
|
|
9
9
|
declare function connectMcp(connection: McpConnection): Promise<McpServer>;
|
|
10
10
|
|