@aeye/core 0.3.1 → 0.3.4
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.d.ts +16 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +16 -1
- package/dist/agent.js.map +1 -1
- package/dist/browser.d.ts +11 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +11 -0
- package/dist/browser.js.map +1 -0
- package/dist/common.d.ts +16 -70
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +75 -311
- package/dist/common.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/prompt.d.ts +28 -5
- package/dist/prompt.d.ts.map +1 -1
- package/dist/prompt.js +63 -16
- package/dist/prompt.js.map +1 -1
- package/dist/schema.js +15 -0
- package/dist/schema.js.map +1 -1
- package/dist/to.d.ts +71 -0
- package/dist/to.d.ts.map +1 -0
- package/dist/to.js +311 -0
- package/dist/to.js.map +1 -0
- package/dist/tool.d.ts +22 -1
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +24 -1
- package/dist/tool.js.map +1 -1
- package/dist/types.d.ts +44 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +13 -1
package/dist/agent.d.ts
CHANGED
|
@@ -21,6 +21,10 @@ export interface AgentInput<TContext = {}, TMetadata = {}, TName extends string
|
|
|
21
21
|
call: (input: TInput, refs: TRefs, ctx: Context<TContext, TMetadata>) => TOutput;
|
|
22
22
|
/** Optional function to determine if the component is applicable in the given context */
|
|
23
23
|
applicable?: (ctx: Context<TContext, TMetadata>) => boolean | Promise<boolean>;
|
|
24
|
+
/** Metadata about the agent to be passed during execution/streaming. Typically contains requirements, configuration, etc. */
|
|
25
|
+
metadata?: TMetadata;
|
|
26
|
+
/** A function/promise that returns metadata about the agent to be passed during execution/streaming. */
|
|
27
|
+
metadataFn?: (input: TInput, ctx: Context<TContext, TMetadata>) => TMetadata | Promise<TMetadata>;
|
|
24
28
|
/** Optional way to explicitly declare the types used in this component */
|
|
25
29
|
types?: {
|
|
26
30
|
input?: TInput;
|
|
@@ -54,12 +58,13 @@ export interface AgentInput<TContext = {}, TMetadata = {}, TName extends string
|
|
|
54
58
|
*/
|
|
55
59
|
export declare class Agent<TContext = {}, TMetadata = {}, TName extends string = string, TInput extends object = {}, TOutput = string, TRefs extends Tuple<ComponentCompatible<TContext, TMetadata>> = []> implements Component<TContext, TMetadata, TName, TInput, TOutput, TRefs> {
|
|
56
60
|
input: AgentInput<TContext, TMetadata, TName, TInput, TOutput, TRefs>;
|
|
61
|
+
private metadataFn;
|
|
57
62
|
/**
|
|
58
63
|
* Creates a new Agent instance.
|
|
59
64
|
*
|
|
60
65
|
* @param input - The agent input configuration.
|
|
61
66
|
*/
|
|
62
|
-
constructor(input: AgentInput<TContext, TMetadata, TName, TInput, TOutput, TRefs>);
|
|
67
|
+
constructor(input: AgentInput<TContext, TMetadata, TName, TInput, TOutput, TRefs>, metadataFn?: (input: TInput, ctx: Context<TContext, TMetadata>) => Promise<TMetadata | undefined>);
|
|
63
68
|
get kind(): 'agent';
|
|
64
69
|
get name(): TName;
|
|
65
70
|
get description(): string;
|
|
@@ -81,5 +86,15 @@ export declare class Agent<TContext = {}, TMetadata = {}, TName extends string =
|
|
|
81
86
|
* @returns A promise that resolves to true if the agent is applicable, false otherwise.
|
|
82
87
|
*/
|
|
83
88
|
applicable<TRuntimeContext extends TContext, TRuntimeMetadata extends TMetadata, TCoreContext extends Context<TRuntimeContext, TRuntimeMetadata>>(...[contextMaybe]: OptionalParams<[TCoreContext]>): Promise<boolean>;
|
|
89
|
+
/**
|
|
90
|
+
* Returns metadata for the agent based on the input and context.
|
|
91
|
+
* Combines static metadata with dynamically generated metadata from metadataFn.
|
|
92
|
+
*
|
|
93
|
+
* @param input - The input for the agent.
|
|
94
|
+
* @param ctx - The context for the agent's operation.
|
|
95
|
+
* @returns The metadata for the agent.
|
|
96
|
+
*/
|
|
97
|
+
metadata(): TMetadata;
|
|
98
|
+
metadata<TRuntimeContext extends TContext, TRuntimeMetadata extends TMetadata, TCoreContext extends Context<TRuntimeContext, TRuntimeMetadata>>(input?: TInput, ctx?: TCoreContext): Promise<TMetadata>;
|
|
84
99
|
}
|
|
85
100
|
//# sourceMappingURL=agent.d.ts.map
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,mBAAmB,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEvG;;;;;;;;;;GAUG;AACH,MAAM,WAAW,UAAU,CACzB,QAAQ,GAAG,EAAE,EACb,SAAS,GAAG,EAAE,EACd,KAAK,SAAS,MAAM,GAAG,MAAM,EAC7B,MAAM,SAAS,MAAM,GAAG,EAAE,EAC1B,OAAO,GAAG,MAAM,EAChB,KAAK,SAAS,KAAK,CAAC,YAAY,CAAC,GAAG,EAAE;IAEtC,mCAAmC;IACnC,IAAI,EAAE,KAAK,CAAC;IACZ,oEAAoE;IACpE,WAAW,EAAE,MAAM,CAAC;IACpB,uFAAuF;IACvF,IAAI,EAAE,KAAK,CAAC;IACZ,wDAAwD;IACxD,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC,KAAK,OAAO,CAAC;IACjF,yFAAyF;IACzF,UAAU,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,QAAQ,EAAE,SAAS,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/E,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OG;IACH,GAAG,CACD,eAAe,SAAS,QAAQ,EAChC,gBAAgB,SAAS,SAAS,EAClC,YAAY,SAAS,OAAO,CAAC,eAAe,EAAE,gBAAgB,CAAC,EAC/D,GAAG,CAAC,UAAU,EAAE,YAAY,CAAC,EAAE,cAAc,CAAC,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,GAAG,OAAO;IAUjF;;;;;;OAMG;IACG,UAAU,CACd,eAAe,SAAS,QAAQ,EAChC,gBAAgB,SAAS,SAAS,EAClC,YAAY,SAAS,OAAO,CAAC,eAAe,EAAE,gBAAgB,CAAC,EAC/D,GAAG,CAAC,YAAY,CAAC,EAAE,cAAc,CAAC,CAAC,YAAY,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IAetE;;;;;;;OAOG;IACH,QAAQ,IAAI,SAAS;IACrB,QAAQ,CACN,eAAe,SAAS,QAAQ,EAChC,gBAAgB,SAAS,SAAS,EAClC,YAAY,SAAS,OAAO,CAAC,eAAe,EAAE,gBAAgB,CAAC,EAC/D,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC;CAoB1D"}
|
package/dist/agent.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveFn } from "./common";
|
|
1
2
|
/**
|
|
2
3
|
* An Agent component that orchestrates complex workflows by composing other AI components.
|
|
3
4
|
* Agents can utilize tools, prompts, and other agents to accomplish sophisticated tasks.
|
|
@@ -23,13 +24,15 @@
|
|
|
23
24
|
*/
|
|
24
25
|
export class Agent {
|
|
25
26
|
input;
|
|
27
|
+
metadataFn;
|
|
26
28
|
/**
|
|
27
29
|
* Creates a new Agent instance.
|
|
28
30
|
*
|
|
29
31
|
* @param input - The agent input configuration.
|
|
30
32
|
*/
|
|
31
|
-
constructor(input) {
|
|
33
|
+
constructor(input, metadataFn = resolveFn(input.metadataFn)) {
|
|
32
34
|
this.input = input;
|
|
35
|
+
this.metadataFn = metadataFn;
|
|
33
36
|
}
|
|
34
37
|
get kind() {
|
|
35
38
|
return 'agent';
|
|
@@ -77,5 +80,17 @@ export class Agent {
|
|
|
77
80
|
}
|
|
78
81
|
return await Promise.all(this.refs.map(ref => ref.applicable(ctx))).then(results => results.some(r => r));
|
|
79
82
|
}
|
|
83
|
+
metadata(input, ctx) {
|
|
84
|
+
// If both input and context are not specified, just return static metadata
|
|
85
|
+
if (input === undefined && ctx === undefined) {
|
|
86
|
+
return (this.input.metadata || {});
|
|
87
|
+
}
|
|
88
|
+
const actualInput = (input || {});
|
|
89
|
+
const actualCtx = (ctx || {});
|
|
90
|
+
return this.metadataFn(actualInput, actualCtx).then(dynamicMetadata => ({
|
|
91
|
+
...(this.input.metadata || {}),
|
|
92
|
+
...(dynamicMetadata || {}),
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
80
95
|
}
|
|
81
96
|
//# sourceMappingURL=agent.js.map
|
package/dist/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AA6CrC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,KAAK;IAeP;IACC;IAPV;;;;OAIG;IACH,YACS,KAAqE,EACpE,aAAa,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC;QADzC,UAAK,GAAL,KAAK,CAAgE;QACpE,eAAU,GAAV,UAAU,CAA8B;IAC9C,CAAC;IAEL,IAAI,IAAI;QACN,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IACzB,CAAC;IAED,IAAI,WAAW;QACb,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAA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,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5G,CAAC;IAgBD,QAAQ,CAIN,KAAc,EAAE,GAAkB;QAClC,2EAA2E;QAC3E,IAAI,KAAK,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YAC7C,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAc,CAAC;QAClD,CAAC;QAED,MAAM,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE,CAAW,CAAC;QAC5C,MAAM,SAAS,GAAG,CAAC,GAAG,IAAI,EAAE,CAAiC,CAAC;QAE9D,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;YACtE,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAe,CAAC;YAC3C,GAAG,CAAC,eAAe,IAAI,EAAE,CAAC;SACb,CAAA,CAAC,CAAC;IACnB,CAAC;CAEF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe exports from @aeye/core.
|
|
3
|
+
* This entry point excludes Node.js-only utilities (to.ts) that depend on fs, path, and stream.
|
|
4
|
+
*/
|
|
5
|
+
export * from './types';
|
|
6
|
+
export * from './common';
|
|
7
|
+
export * from './schema';
|
|
8
|
+
export * from './tool';
|
|
9
|
+
export * from './prompt';
|
|
10
|
+
export * from './agent';
|
|
11
|
+
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AACA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe exports from @aeye/core.
|
|
3
|
+
* This entry point excludes Node.js-only utilities (to.ts) that depend on fs, path, and stream.
|
|
4
|
+
*/
|
|
5
|
+
export * from './types';
|
|
6
|
+
export * from './common';
|
|
7
|
+
export * from './schema';
|
|
8
|
+
export * from './tool';
|
|
9
|
+
export * from './prompt';
|
|
10
|
+
export * from './agent';
|
|
11
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AACA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
|
package/dist/common.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Chunk, Model, ModelInput, Response, Runner, Usage
|
|
2
|
-
import { Readable } from 'stream';
|
|
1
|
+
import { Chunk, Events, Model, ModelInput, Reasoning, Response, Runner, Usage } from "./types";
|
|
3
2
|
/**
|
|
4
3
|
* A flexible function type that can be:
|
|
5
4
|
* - A direct value of type R
|
|
@@ -96,74 +95,6 @@ export declare function consumeAll<E>(gen: AsyncGenerator<E, any, any>): Promise
|
|
|
96
95
|
* Resolves the type R from a value passed to `resolve`.
|
|
97
96
|
*/
|
|
98
97
|
export type Resolved<T> = T extends Promise<infer U> ? Resolved<U> : T extends AsyncGenerator<any, infer U, any> ? Resolved<U> : T;
|
|
99
|
-
/**
|
|
100
|
-
* Determines the ideal format of a given resource.
|
|
101
|
-
*
|
|
102
|
-
* @param resource
|
|
103
|
-
* @returns
|
|
104
|
-
*/
|
|
105
|
-
export declare function getResourceFormat(resource: Resource): 'url' | 'base64' | 'stream';
|
|
106
|
-
/**
|
|
107
|
-
* Converts the input resource to a URL string.
|
|
108
|
-
*
|
|
109
|
-
* For input string:
|
|
110
|
-
* - If input is already a URL or string, it is returned as-is.
|
|
111
|
-
* - If input is a data URL (e.g. base64), it is returned as-is.
|
|
112
|
-
* - Otherwise it is base64-encoded and returned as a data URL.
|
|
113
|
-
*
|
|
114
|
-
* For URL:
|
|
115
|
-
* - It is returned with toString().
|
|
116
|
-
*
|
|
117
|
-
* For Uint8Array/File/ReadStream/ReadableStream:
|
|
118
|
-
* - It is base64-encoded and returned as a data URL.
|
|
119
|
-
*
|
|
120
|
-
* @param input - The resource to convert.
|
|
121
|
-
* @param mimeType - Optional MIME type for the data URL if one cannot be determined.
|
|
122
|
-
* @param fallback - Optional fallback string to return if input type is invalid.
|
|
123
|
-
* @returns http(s) URL or data URL string
|
|
124
|
-
*/
|
|
125
|
-
export declare function toURL(input: Resource, mimeType?: string, fallback?: string): Promise<string>;
|
|
126
|
-
/**
|
|
127
|
-
* Converts the input resource to a Base64 data URL string.
|
|
128
|
-
* This will represent the content of the resouce as a data URL.
|
|
129
|
-
*
|
|
130
|
-
* @param input - The resource to convert.
|
|
131
|
-
* @param mimeType - Optional MIME type for the data URL if one cannot be determined.
|
|
132
|
-
* @param fallback - Optional fallback string to return if input type is invalid.
|
|
133
|
-
* @returns - A promise that resolves to a Base64 data URL string.
|
|
134
|
-
*/
|
|
135
|
-
export declare function toBase64(input: Resource, mimeType?: string, fallback?: string): Promise<string>;
|
|
136
|
-
/**
|
|
137
|
-
* Converts the input resource to a text string. It assumes it points to resource
|
|
138
|
-
* that is text or points to a text file.
|
|
139
|
-
*
|
|
140
|
-
* @param input - The resource to convert.
|
|
141
|
-
* @param fallback - Optional fallback string to return if conversion fails.
|
|
142
|
-
* @returns - A promise that resolves to a text string.
|
|
143
|
-
*/
|
|
144
|
-
export declare function toText(input: Resource, fallback?: string): Promise<string>;
|
|
145
|
-
/**
|
|
146
|
-
* Converts the input resource to an AsyncIterable stream of Uint8Array chunks.
|
|
147
|
-
*
|
|
148
|
-
* @param input - The resource to convert.
|
|
149
|
-
* @returns A promise that resolves to an AsyncIterable of Uint8Array chunks, or null if conversion fails.
|
|
150
|
-
*/
|
|
151
|
-
export declare function toStream(input: Resource, fallback?: Readable): Promise<Readable>;
|
|
152
|
-
/**
|
|
153
|
-
* Converts the input resource to a ReadableStream.
|
|
154
|
-
*
|
|
155
|
-
* @param input - The resource to convert.
|
|
156
|
-
* @param fallback - Optional fallback ReadableStream to use if conversion fails.
|
|
157
|
-
* @returns A promise that resolves to a ReadableStream.
|
|
158
|
-
*/
|
|
159
|
-
export declare function toReadableStream(input: Resource, fallback?: ReadableStream): Promise<ReadableStream>;
|
|
160
|
-
/**
|
|
161
|
-
* Converts the input resource to a File.
|
|
162
|
-
*
|
|
163
|
-
* @param input - The resource to convert.
|
|
164
|
-
* @returns A promise that resolves to an AsyncIterable of Uint8Array chunks, or null if conversion fails.
|
|
165
|
-
*/
|
|
166
|
-
export declare function toFile(input: Resource, mimeType?: string, filename?: string): Promise<File>;
|
|
167
98
|
/**
|
|
168
99
|
* Accumulates usage statistics by adding values from one Usage object to another.
|
|
169
100
|
* Handles the nested structure matching ModelPricing.
|
|
@@ -214,6 +145,21 @@ export declare function getModel(input: ModelInput | undefined): Model | undefin
|
|
|
214
145
|
* @returns The aggregated Response object.
|
|
215
146
|
*/
|
|
216
147
|
export declare function getResponseFromChunks(chunks: Chunk[], model?: ModelInput): Response;
|
|
148
|
+
/**
|
|
149
|
+
* Generates a plain text representation of reasoning data.
|
|
150
|
+
*
|
|
151
|
+
* @param reasoning - The reasoning data to convert.
|
|
152
|
+
* @returns A plain text string representing the reasoning.
|
|
153
|
+
*/
|
|
154
|
+
export declare function getReasoningText(reasoning: Reasoning | undefined): string;
|
|
155
|
+
/**
|
|
156
|
+
* Accumulates reasoning data by merging an additional Reasoning object into a target Reasoning object.
|
|
157
|
+
*
|
|
158
|
+
* @param target - The target Reasoning object to accumulate into.
|
|
159
|
+
* @param add - The Reasoning object to add from.
|
|
160
|
+
* @returns - The updated Reasoning object.
|
|
161
|
+
*/
|
|
162
|
+
export declare function accumulateReasoning(target: Reasoning | undefined, add?: Reasoning): Reasoning | undefined;
|
|
217
163
|
/**
|
|
218
164
|
* Converts a Response object into an array of Chunk objects.
|
|
219
165
|
*
|
package/dist/common.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAmB,MAAM,EAAY,KAAK,EAAE,UAAU,EAAE,SAAS,EAAY,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEpI;;;;;GAKG;AACH,MAAM,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEzG;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAChD,CAAC,GACD,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,MAAM,CAAC,GACnC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GACxB,CAAC,GACD,CAAC,GACH,CAAC,CAAC;AAER;;GAEG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,KAAK,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,E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K,GAAG,MAAM,CAOpD;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAKrD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAYpD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK,CAAC;AACnD,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,CAAC;AAK3E;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,KAAK,GAAE,UAAsB,GAAG,QAAQ,CAiC9F;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,GAAG,MAAM,CAoBzE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,EAAE,GAAG,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAgDzG;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,QAAQ,GAAG,KAAK,EAAE,CAsBjE;AAED;;;;;GAKG,CACH,wBAAgB,UAAU,CAAC,KAAK,SAAS,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAgHpF"}
|
package/dist/common.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import url from 'url';
|
|
4
|
-
import { Readable } from 'stream';
|
|
5
1
|
export function resolveFn(fn, reprocess) {
|
|
6
2
|
if (!fn) {
|
|
7
3
|
return async () => undefined;
|
|
@@ -131,312 +127,6 @@ export async function consumeAll(gen) {
|
|
|
131
127
|
}
|
|
132
128
|
return results;
|
|
133
129
|
}
|
|
134
|
-
function hasUrl(x) {
|
|
135
|
-
return x && typeof x.url === 'function';
|
|
136
|
-
}
|
|
137
|
-
function hasBlob(x) {
|
|
138
|
-
return x && typeof x.blob === 'function';
|
|
139
|
-
}
|
|
140
|
-
function hasRead(x) {
|
|
141
|
-
return x && typeof x.read === 'function';
|
|
142
|
-
}
|
|
143
|
-
function isAsyncIterable(x) {
|
|
144
|
-
return x && typeof x[Symbol.asyncIterator] === 'function';
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Determines the ideal format of a given resource.
|
|
148
|
-
*
|
|
149
|
-
* @param resource
|
|
150
|
-
* @returns
|
|
151
|
-
*/
|
|
152
|
-
export function getResourceFormat(resource) {
|
|
153
|
-
if (typeof resource === 'string') {
|
|
154
|
-
if (resource.startsWith('data:')) {
|
|
155
|
-
return 'base64';
|
|
156
|
-
}
|
|
157
|
-
else if (resource.startsWith('http://') || resource.startsWith('https://')) {
|
|
158
|
-
return 'url';
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
return 'base64';
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (hasUrl(resource) || resource instanceof URL) {
|
|
165
|
-
return 'url';
|
|
166
|
-
}
|
|
167
|
-
return 'stream';
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Converts the input resource to a URL string.
|
|
171
|
-
*
|
|
172
|
-
* For input string:
|
|
173
|
-
* - If input is already a URL or string, it is returned as-is.
|
|
174
|
-
* - If input is a data URL (e.g. base64), it is returned as-is.
|
|
175
|
-
* - Otherwise it is base64-encoded and returned as a data URL.
|
|
176
|
-
*
|
|
177
|
-
* For URL:
|
|
178
|
-
* - It is returned with toString().
|
|
179
|
-
*
|
|
180
|
-
* For Uint8Array/File/ReadStream/ReadableStream:
|
|
181
|
-
* - It is base64-encoded and returned as a data URL.
|
|
182
|
-
*
|
|
183
|
-
* @param input - The resource to convert.
|
|
184
|
-
* @param mimeType - Optional MIME type for the data URL if one cannot be determined.
|
|
185
|
-
* @param fallback - Optional fallback string to return if input type is invalid.
|
|
186
|
-
* @returns http(s) URL or data URL string
|
|
187
|
-
*/
|
|
188
|
-
export async function toURL(input, mimeType, fallback) {
|
|
189
|
-
if (input instanceof URL) {
|
|
190
|
-
return input.toString();
|
|
191
|
-
}
|
|
192
|
-
if (typeof input === 'string' && (input.startsWith('data:') || input.startsWith('http://') || input.startsWith('https://'))) {
|
|
193
|
-
return input;
|
|
194
|
-
}
|
|
195
|
-
if (hasUrl(input)) {
|
|
196
|
-
return input.url();
|
|
197
|
-
}
|
|
198
|
-
return toBase64(input, mimeType, fallback);
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Converts the input resource to a Base64 data URL string.
|
|
202
|
-
* This will represent the content of the resouce as a data URL.
|
|
203
|
-
*
|
|
204
|
-
* @param input - The resource to convert.
|
|
205
|
-
* @param mimeType - Optional MIME type for the data URL if one cannot be determined.
|
|
206
|
-
* @param fallback - Optional fallback string to return if input type is invalid.
|
|
207
|
-
* @returns - A promise that resolves to a Base64 data URL string.
|
|
208
|
-
*/
|
|
209
|
-
export async function toBase64(input, mimeType, fallback) {
|
|
210
|
-
if (typeof input === 'string') {
|
|
211
|
-
if (input.startsWith('data:')) {
|
|
212
|
-
return input;
|
|
213
|
-
}
|
|
214
|
-
if (input.startsWith('http://') || input.startsWith('https://')) {
|
|
215
|
-
input = new URL(input);
|
|
216
|
-
}
|
|
217
|
-
else if (input.startsWith('file://')) {
|
|
218
|
-
const filePath = url.fileURLToPath(input);
|
|
219
|
-
input = fs.createReadStream(filePath);
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
return `data:${mimeType || 'text/plain'};base64,${Buffer.from(input).toString('base64')}`;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (hasUrl(input)) {
|
|
226
|
-
input = new URL(input.url());
|
|
227
|
-
}
|
|
228
|
-
if (input instanceof URL) {
|
|
229
|
-
input = await (await fetch(input)).blob();
|
|
230
|
-
}
|
|
231
|
-
if (typeof input !== 'object') {
|
|
232
|
-
if (fallback !== undefined) {
|
|
233
|
-
return fallback;
|
|
234
|
-
}
|
|
235
|
-
throw new Error('Invalid input type');
|
|
236
|
-
}
|
|
237
|
-
if (hasBlob(input)) {
|
|
238
|
-
input = await input.blob();
|
|
239
|
-
}
|
|
240
|
-
if (hasRead(input)) {
|
|
241
|
-
input = await input.read();
|
|
242
|
-
}
|
|
243
|
-
if ('type' in input && typeof input.type === 'string' && input.type.length > 0) {
|
|
244
|
-
mimeType = input.type;
|
|
245
|
-
}
|
|
246
|
-
if ('mimeType' in input && typeof input.mimeType === 'string' && input.mimeType.length > 0) {
|
|
247
|
-
mimeType = input.mimeType;
|
|
248
|
-
}
|
|
249
|
-
if (input instanceof Blob) {
|
|
250
|
-
input = await input.arrayBuffer();
|
|
251
|
-
}
|
|
252
|
-
if (input instanceof DataView) {
|
|
253
|
-
input = Buffer.from(input.buffer);
|
|
254
|
-
}
|
|
255
|
-
if (input instanceof ArrayBuffer) {
|
|
256
|
-
input = Buffer.from(input);
|
|
257
|
-
}
|
|
258
|
-
if (isAsyncIterable(input)) {
|
|
259
|
-
const chunks = [];
|
|
260
|
-
for await (const chunk of input) {
|
|
261
|
-
chunks.push(chunk);
|
|
262
|
-
}
|
|
263
|
-
input = Buffer.concat(chunks);
|
|
264
|
-
}
|
|
265
|
-
const buffer = Buffer.isBuffer(input)
|
|
266
|
-
? input
|
|
267
|
-
: Buffer.from(input);
|
|
268
|
-
const base64 = buffer.toString('base64');
|
|
269
|
-
return `data:${mimeType || 'application/octet-stream'};base64,${base64}`;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Converts the input resource to a text string. It assumes it points to resource
|
|
273
|
-
* that is text or points to a text file.
|
|
274
|
-
*
|
|
275
|
-
* @param input - The resource to convert.
|
|
276
|
-
* @param fallback - Optional fallback string to return if conversion fails.
|
|
277
|
-
* @returns - A promise that resolves to a text string.
|
|
278
|
-
*/
|
|
279
|
-
export async function toText(input, fallback = 'Unable to convert resource to text') {
|
|
280
|
-
if (typeof input === 'string') {
|
|
281
|
-
if (input.startsWith('data:')) {
|
|
282
|
-
const commaIndex = input.indexOf(',');
|
|
283
|
-
if (commaIndex >= 0) {
|
|
284
|
-
const base64Part = input.substring(commaIndex + 1);
|
|
285
|
-
const buffer = Buffer.from(base64Part, 'base64');
|
|
286
|
-
return buffer.toString('utf-8');
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return input;
|
|
290
|
-
}
|
|
291
|
-
const stream = await toStream(input);
|
|
292
|
-
if (!stream) {
|
|
293
|
-
return fallback;
|
|
294
|
-
}
|
|
295
|
-
const decoded = new TextDecoder();
|
|
296
|
-
let text = '';
|
|
297
|
-
for await (const chunk of stream) {
|
|
298
|
-
text += decoded.decode(chunk);
|
|
299
|
-
}
|
|
300
|
-
return text;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Converts the input resource to an AsyncIterable stream of Uint8Array chunks.
|
|
304
|
-
*
|
|
305
|
-
* @param input - The resource to convert.
|
|
306
|
-
* @returns A promise that resolves to an AsyncIterable of Uint8Array chunks, or null if conversion fails.
|
|
307
|
-
*/
|
|
308
|
-
export async function toStream(input, fallback) {
|
|
309
|
-
if (typeof input === 'string') {
|
|
310
|
-
if (input.startsWith('data:')) {
|
|
311
|
-
const commaIndex = input.indexOf(',');
|
|
312
|
-
if (commaIndex >= 0) {
|
|
313
|
-
const base64Part = input.substring(commaIndex + 1);
|
|
314
|
-
const buffer = Buffer.from(base64Part, 'base64');
|
|
315
|
-
return Readable.from([buffer]);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
if (input.startsWith('http://') || input.startsWith('https://')) {
|
|
319
|
-
input = new URL(input);
|
|
320
|
-
}
|
|
321
|
-
else if (input.startsWith('file://')) {
|
|
322
|
-
const filePath = url.fileURLToPath(input);
|
|
323
|
-
input = fs.createReadStream(filePath);
|
|
324
|
-
}
|
|
325
|
-
else {
|
|
326
|
-
return Readable.from([Buffer.from(input, 'utf-8')]);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
if (hasUrl(input)) {
|
|
330
|
-
input = new URL(input.url());
|
|
331
|
-
}
|
|
332
|
-
if (input instanceof URL) {
|
|
333
|
-
const response = await fetch(input);
|
|
334
|
-
if (!response.ok) {
|
|
335
|
-
if (fallback) {
|
|
336
|
-
return fallback;
|
|
337
|
-
}
|
|
338
|
-
throw new Error(`Failed to fetch ${input}: ${response.status} ${response.statusText}`);
|
|
339
|
-
}
|
|
340
|
-
if (!response.body) {
|
|
341
|
-
if (fallback) {
|
|
342
|
-
return fallback;
|
|
343
|
-
}
|
|
344
|
-
throw new Error(`No response body from ${input}`);
|
|
345
|
-
}
|
|
346
|
-
return Readable.from(response.body);
|
|
347
|
-
}
|
|
348
|
-
if (hasRead(input)) {
|
|
349
|
-
input = await input.read();
|
|
350
|
-
}
|
|
351
|
-
if (hasBlob(input)) {
|
|
352
|
-
input = await input.blob();
|
|
353
|
-
}
|
|
354
|
-
if (input instanceof Blob) {
|
|
355
|
-
input = input.stream();
|
|
356
|
-
}
|
|
357
|
-
if (input instanceof DataView) {
|
|
358
|
-
input = Buffer.from(input.buffer);
|
|
359
|
-
}
|
|
360
|
-
if (input instanceof ArrayBuffer) {
|
|
361
|
-
input = Buffer.from(input);
|
|
362
|
-
}
|
|
363
|
-
return Readable.from(input);
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Converts the input resource to a ReadableStream.
|
|
367
|
-
*
|
|
368
|
-
* @param input - The resource to convert.
|
|
369
|
-
* @param fallback - Optional fallback ReadableStream to use if conversion fails.
|
|
370
|
-
* @returns A promise that resolves to a ReadableStream.
|
|
371
|
-
*/
|
|
372
|
-
export async function toReadableStream(input, fallback) {
|
|
373
|
-
return ReadableStream.from(await toStream(input, fallback ? Readable.fromWeb(fallback) : undefined));
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Converts the input resource to a File.
|
|
377
|
-
*
|
|
378
|
-
* @param input - The resource to convert.
|
|
379
|
-
* @returns A promise that resolves to an AsyncIterable of Uint8Array chunks, or null if conversion fails.
|
|
380
|
-
*/
|
|
381
|
-
export async function toFile(input, mimeType, filename) {
|
|
382
|
-
if (typeof input === 'string') {
|
|
383
|
-
if (input.startsWith('data:')) {
|
|
384
|
-
const [, type, base64Part] = input.substring(5).split(';base64,', 2);
|
|
385
|
-
mimeType = type || mimeType;
|
|
386
|
-
input = Buffer.from(base64Part, 'base64');
|
|
387
|
-
}
|
|
388
|
-
else if (input.startsWith('http://') || input.startsWith('https://')) {
|
|
389
|
-
input = new URL(input);
|
|
390
|
-
}
|
|
391
|
-
else if (input.startsWith('file://')) {
|
|
392
|
-
const filePath = url.fileURLToPath(input);
|
|
393
|
-
input = await fs.openAsBlob(filePath);
|
|
394
|
-
if (!filename) {
|
|
395
|
-
filename = path.basename(filePath);
|
|
396
|
-
}
|
|
397
|
-
if (input.type && !mimeType) {
|
|
398
|
-
mimeType = input.type;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
else {
|
|
402
|
-
if (!mimeType) {
|
|
403
|
-
mimeType = 'text/plain';
|
|
404
|
-
}
|
|
405
|
-
input = Buffer.from(input, 'utf-8');
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
if (input instanceof File) {
|
|
409
|
-
return input;
|
|
410
|
-
}
|
|
411
|
-
if (hasUrl(input)) {
|
|
412
|
-
input = new URL(input.url());
|
|
413
|
-
}
|
|
414
|
-
if (input instanceof URL) {
|
|
415
|
-
if (!filename) {
|
|
416
|
-
filename = path.basename(input.pathname);
|
|
417
|
-
}
|
|
418
|
-
input = await fetch(input);
|
|
419
|
-
}
|
|
420
|
-
if (hasBlob(input)) {
|
|
421
|
-
input = await input.blob();
|
|
422
|
-
}
|
|
423
|
-
if (hasRead(input)) {
|
|
424
|
-
input = await input.read();
|
|
425
|
-
}
|
|
426
|
-
if (input instanceof ArrayBuffer) {
|
|
427
|
-
input = new Blob([input]);
|
|
428
|
-
}
|
|
429
|
-
if (isAsyncIterable(input)) {
|
|
430
|
-
const chunks = [];
|
|
431
|
-
for await (const chunk of input) {
|
|
432
|
-
chunks.push(chunk);
|
|
433
|
-
}
|
|
434
|
-
input = new Blob(chunks);
|
|
435
|
-
}
|
|
436
|
-
return new File([input], filename || 'file', {
|
|
437
|
-
type: mimeType || 'application/octet-stream',
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
130
|
/**
|
|
441
131
|
* Accumulates usage statistics by adding values from one Usage object to another.
|
|
442
132
|
* Handles the nested structure matching ModelPricing.
|
|
@@ -613,7 +303,7 @@ export function getResponseFromChunks(chunks, model = 'unknown') {
|
|
|
613
303
|
resp.finishReason = chunk.finishReason;
|
|
614
304
|
}
|
|
615
305
|
if (chunk.reasoning) {
|
|
616
|
-
resp.reasoning = (resp.reasoning
|
|
306
|
+
resp.reasoning = accumulateReasoning(resp.reasoning, chunk.reasoning);
|
|
617
307
|
}
|
|
618
308
|
if (chunk.model) {
|
|
619
309
|
resp.model = chunk.model;
|
|
@@ -632,6 +322,80 @@ export function getResponseFromChunks(chunks, model = 'unknown') {
|
|
|
632
322
|
}
|
|
633
323
|
return resp;
|
|
634
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Generates a plain text representation of reasoning data.
|
|
327
|
+
*
|
|
328
|
+
* @param reasoning - The reasoning data to convert.
|
|
329
|
+
* @returns A plain text string representing the reasoning.
|
|
330
|
+
*/
|
|
331
|
+
export function getReasoningText(reasoning) {
|
|
332
|
+
if (!reasoning) {
|
|
333
|
+
return '';
|
|
334
|
+
}
|
|
335
|
+
const parts = [];
|
|
336
|
+
if (reasoning.content) {
|
|
337
|
+
parts.push(reasoning.content);
|
|
338
|
+
}
|
|
339
|
+
else if (reasoning?.details) {
|
|
340
|
+
for (const detail of reasoning.details) {
|
|
341
|
+
if (detail.summary) {
|
|
342
|
+
parts.push(detail.summary);
|
|
343
|
+
}
|
|
344
|
+
if (detail.text) {
|
|
345
|
+
parts.push(detail.text);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return parts.join('\n\n');
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Accumulates reasoning data by merging an additional Reasoning object into a target Reasoning object.
|
|
353
|
+
*
|
|
354
|
+
* @param target - The target Reasoning object to accumulate into.
|
|
355
|
+
* @param add - The Reasoning object to add from.
|
|
356
|
+
* @returns - The updated Reasoning object.
|
|
357
|
+
*/
|
|
358
|
+
export function accumulateReasoning(target, add) {
|
|
359
|
+
if (!target) {
|
|
360
|
+
return add;
|
|
361
|
+
}
|
|
362
|
+
if (!add) {
|
|
363
|
+
return target;
|
|
364
|
+
}
|
|
365
|
+
if (add.content) {
|
|
366
|
+
target.content = (target.content || '') + add.content;
|
|
367
|
+
}
|
|
368
|
+
if (add.details?.length) {
|
|
369
|
+
target.details = target.details || [];
|
|
370
|
+
for (const detail of add.details) {
|
|
371
|
+
let existing = target.details.find(d => (detail.id && d.id && detail.id === d.id) ||
|
|
372
|
+
(detail.index !== undefined && d.index !== undefined && detail.index === d.index));
|
|
373
|
+
const missingIdentifiers = add.details.every(d => !d.id && d.index === undefined) &&
|
|
374
|
+
target.details.every(d => !d.id && d.index === undefined);
|
|
375
|
+
if (missingIdentifiers) {
|
|
376
|
+
existing = target.details.find(d => d.type === detail.type);
|
|
377
|
+
}
|
|
378
|
+
if (existing) {
|
|
379
|
+
if (detail.data) {
|
|
380
|
+
existing.data = (existing.data || '') + detail.data;
|
|
381
|
+
}
|
|
382
|
+
if (detail.text) {
|
|
383
|
+
existing.text = (existing.text || '') + detail.text;
|
|
384
|
+
if (detail.signature) {
|
|
385
|
+
existing.signature = detail.signature;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (detail.summary) {
|
|
389
|
+
existing.summary = (existing.summary || '') + detail.summary;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
target.details.push(detail);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return target;
|
|
398
|
+
}
|
|
635
399
|
/**
|
|
636
400
|
* Converts a Response object into an array of Chunk objects.
|
|
637
401
|
*
|