agentfootprint 7.8.0 → 7.10.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 +2 -2
- package/dist/core-flow/Workflow.js +210 -0
- package/dist/core-flow/Workflow.js.map +1 -0
- package/dist/embedders/index.js +74 -26
- package/dist/embedders/index.js.map +1 -1
- package/dist/esm/core-flow/Workflow.d.ts +146 -0
- package/dist/esm/core-flow/Workflow.js +205 -0
- package/dist/esm/core-flow/Workflow.js.map +1 -0
- package/dist/esm/embedders/index.d.ts +96 -1
- package/dist/esm/embedders/index.js +74 -26
- package/dist/esm/embedders/index.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/patterns/LlmRouter.d.ts +221 -0
- package/dist/esm/patterns/LlmRouter.js +400 -0
- package/dist/esm/patterns/LlmRouter.js.map +1 -0
- package/dist/esm/patterns/LlmSwarm.d.ts +100 -0
- package/dist/esm/patterns/LlmSwarm.js +109 -0
- package/dist/esm/patterns/LlmSwarm.js.map +1 -0
- package/dist/esm/patterns/index.d.ts +2 -0
- package/dist/esm/patterns/index.js +2 -0
- package/dist/esm/patterns/index.js.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/patterns/LlmRouter.js +406 -0
- package/dist/patterns/LlmRouter.js.map +1 -0
- package/dist/patterns/LlmSwarm.js +113 -0
- package/dist/patterns/LlmSwarm.js.map +1 -0
- package/dist/patterns/index.js +6 -1
- package/dist/patterns/index.js.map +1 -1
- package/dist/types/core-flow/Workflow.d.ts +147 -0
- package/dist/types/core-flow/Workflow.d.ts.map +1 -0
- package/dist/types/embedders/index.d.ts +96 -1
- package/dist/types/embedders/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/patterns/LlmRouter.d.ts +222 -0
- package/dist/types/patterns/LlmRouter.d.ts.map +1 -0
- package/dist/types/patterns/LlmSwarm.d.ts +101 -0
- package/dist/types/patterns/LlmSwarm.d.ts.map +1 -0
- package/dist/types/patterns/index.d.ts +2 -0
- package/dist/types/patterns/index.d.ts.map +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* workflow() — sequential steps whose hand-offs are checked by the compiler.
|
|
3
|
+
*
|
|
4
|
+
* WHY this exists: `Sequence` is the workhorse for "A, then B, then C",
|
|
5
|
+
* and every step it accepts has the same shape — takes `{ message }`,
|
|
6
|
+
* returns `string`. That is exactly right for chaining LLM calls, and
|
|
7
|
+
* exactly wrong the moment a step wants to hand the next one something
|
|
8
|
+
* structured: `Sequence` coerces any non-string step output to `''`
|
|
9
|
+
* (Sequence.ts, the step `outputMapper`), so a step that returns a parsed
|
|
10
|
+
* ticket silently hands the next step nothing at all. The mistake shows up
|
|
11
|
+
* as an empty prompt three steps later, at runtime, in production.
|
|
12
|
+
*
|
|
13
|
+
* `workflow()` closes that gap from both ends:
|
|
14
|
+
*
|
|
15
|
+
* - **At compile time** — step N's OUTPUT type must be what step N+1
|
|
16
|
+
* accepts. A `Runner<{ message: string }, Ticket>` followed by a
|
|
17
|
+
* `Runner<{ orderId: string }, string>` does not compile. The chain is
|
|
18
|
+
* proven before you run it, not debugged after.
|
|
19
|
+
* - **At run time** — a step's value is handed to the next step
|
|
20
|
+
* UNCHANGED. Objects stay objects. The one convenience is the house
|
|
21
|
+
* convention: a step that returns a `string` feeds the next step's
|
|
22
|
+
* `{ message }`, because that is what every LLM runner here wants.
|
|
23
|
+
*
|
|
24
|
+
* Pattern: Adapter over footprintjs's `addSubFlowChartNext`, with the
|
|
25
|
+
* type-level handoff proof carried by overloads (1–8 steps).
|
|
26
|
+
* Role: core-flow/ layer, alongside Sequence/Parallel/Conditional/Loop.
|
|
27
|
+
* Pure control flow — no LLM dependency.
|
|
28
|
+
* Emits: agentfootprint.composition.enter / exit, reported as kind
|
|
29
|
+
* `'Sequence'` — a workflow IS a sequential composition, and
|
|
30
|
+
* widening the public `CompositionKind` union would break
|
|
31
|
+
* exhaustive switches in consumer code for no behavioural gain.
|
|
32
|
+
*
|
|
33
|
+
* THREE HONEST LIMITS, all inherited from the engine and all verified in
|
|
34
|
+
* `test/core-flow/scenario/Workflow.test.ts` — worth knowing before you
|
|
35
|
+
* put rich objects on the wire:
|
|
36
|
+
*
|
|
37
|
+
* 1. Only PLAIN DATA crosses a step boundary. A value with a prototype
|
|
38
|
+
* (Date, Map, Set, a class instance) arrives as `{}`, and `undefined`
|
|
39
|
+
* fields are dropped. Send strings, numbers, arrays and plain
|
|
40
|
+
* objects; send a timestamp as an ISO string, not a `Date`.
|
|
41
|
+
* 2. A step must RETURN its output — the value handed forward is the
|
|
42
|
+
* step chart's traversal result. A step whose last stage returns
|
|
43
|
+
* nothing hands its whole scope forward instead.
|
|
44
|
+
* 3. The workflow's own input keys stay visible to LATER steps too
|
|
45
|
+
* (footprintjs's `getArgs()` inherits the run's arguments). A key the
|
|
46
|
+
* previous step actually produced always wins; a key it did NOT
|
|
47
|
+
* produce can still be read from the original input rather than
|
|
48
|
+
* coming back `undefined`.
|
|
49
|
+
*
|
|
50
|
+
* @example a typed three-step chain
|
|
51
|
+
* ```ts
|
|
52
|
+
* interface Ticket { orderId: string; angry: boolean }
|
|
53
|
+
*
|
|
54
|
+
* const parse: Runner<{ message: string }, Ticket> = …;
|
|
55
|
+
* const lookup: Runner<Ticket, { refundUsd: number }> = …;
|
|
56
|
+
* const reply: Runner<{ refundUsd: number }, string> = …;
|
|
57
|
+
*
|
|
58
|
+
* const intake = workflow(parse, lookup, reply);
|
|
59
|
+
* const answer = await intake.run({ message: 'where is my refund?' });
|
|
60
|
+
* // ^? string — the chain's last output type
|
|
61
|
+
*
|
|
62
|
+
* workflow(parse, reply); // ✗ compile error: Ticket is not { refundUsd }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
import { FlowChartExecutor, flowChart, } from 'footprintjs';
|
|
66
|
+
import { RunnerBase, makeRunId } from '../core/RunnerBase.js';
|
|
67
|
+
import { agentRecorder } from '../recorders/core/AgentRecorder.js';
|
|
68
|
+
import { compositionRecorder } from '../recorders/core/CompositionRecorder.js';
|
|
69
|
+
import { ContextRecorder } from '../recorders/core/ContextRecorder.js';
|
|
70
|
+
import { streamRecorder } from '../recorders/core/StreamRecorder.js';
|
|
71
|
+
import { typedEmit } from '../recorders/core/typedEmit.js';
|
|
72
|
+
/**
|
|
73
|
+
* Hand the previous step's value to the next step as its input args.
|
|
74
|
+
*
|
|
75
|
+
* `string` → `{ message }` (the house convention). Plain object → itself.
|
|
76
|
+
* Anything else is a broken hand-off and says so loudly: the alternative
|
|
77
|
+
* is an empty input three steps downstream with nothing pointing back
|
|
78
|
+
* here.
|
|
79
|
+
*/
|
|
80
|
+
function toStepArgs(value, stepNumber) {
|
|
81
|
+
if (typeof value === 'string')
|
|
82
|
+
return { message: value };
|
|
83
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
84
|
+
return { ...value };
|
|
85
|
+
}
|
|
86
|
+
const got = value === null ? 'null' : Array.isArray(value) ? 'an array' : typeof value;
|
|
87
|
+
throw new Error(`workflow: step ${stepNumber - 1} handed forward ${got}, but step ${stepNumber} needs an ` +
|
|
88
|
+
'object (or a string, which arrives as { message }). Make each step return its output.');
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A sequential composition that passes values through untouched. Build one
|
|
92
|
+
* with {@link workflow} — that factory carries the type-level chain proof.
|
|
93
|
+
*/
|
|
94
|
+
export class Workflow extends RunnerBase {
|
|
95
|
+
name;
|
|
96
|
+
id;
|
|
97
|
+
steps;
|
|
98
|
+
opts;
|
|
99
|
+
currentRunContext = {
|
|
100
|
+
runStartMs: 0,
|
|
101
|
+
runId: 'pending',
|
|
102
|
+
compositionPath: [],
|
|
103
|
+
};
|
|
104
|
+
constructor(steps, opts = {}) {
|
|
105
|
+
super();
|
|
106
|
+
if (steps.length === 0) {
|
|
107
|
+
throw new Error('Workflow: must have at least one step');
|
|
108
|
+
}
|
|
109
|
+
this.opts = opts;
|
|
110
|
+
this.name = opts.name ?? 'Workflow';
|
|
111
|
+
this.id = opts.id ?? 'workflow';
|
|
112
|
+
this.steps = steps;
|
|
113
|
+
// Eager chart construction — see `RunnerBase.initChart` JSDoc.
|
|
114
|
+
this.initChart(() => this.buildChart());
|
|
115
|
+
}
|
|
116
|
+
async run(input, options) {
|
|
117
|
+
const executor = this.createExecutor();
|
|
118
|
+
this.lastExecutor = executor;
|
|
119
|
+
const result = await executor.run({ input: { ...input }, ...(options ?? {}) });
|
|
120
|
+
return this.finalizeResult(executor, result);
|
|
121
|
+
}
|
|
122
|
+
async resume(checkpoint, input, options) {
|
|
123
|
+
this.emitPauseResume(checkpoint, input);
|
|
124
|
+
const executor = this.createExecutor();
|
|
125
|
+
this.lastExecutor = executor;
|
|
126
|
+
const result = await executor.resume(checkpoint, input, options);
|
|
127
|
+
return this.finalizeResult(executor, result);
|
|
128
|
+
}
|
|
129
|
+
createExecutor() {
|
|
130
|
+
this.currentRunContext = {
|
|
131
|
+
runStartMs: Date.now(),
|
|
132
|
+
runId: makeRunId(),
|
|
133
|
+
compositionPath: [`Workflow:${this.id}`],
|
|
134
|
+
};
|
|
135
|
+
const executor = new FlowChartExecutor(this.getSpec());
|
|
136
|
+
const dispatcher = this.getDispatcher();
|
|
137
|
+
const getRunCtx = () => this.currentRunContext;
|
|
138
|
+
executor.attachCombinedRecorder(new ContextRecorder({ dispatcher, getRunContext: getRunCtx }));
|
|
139
|
+
executor.attachCombinedRecorder(streamRecorder({ dispatcher, getRunContext: getRunCtx }));
|
|
140
|
+
executor.attachCombinedRecorder(agentRecorder({ dispatcher, getRunContext: getRunCtx }));
|
|
141
|
+
executor.attachCombinedRecorder(compositionRecorder({ dispatcher, getRunContext: getRunCtx }));
|
|
142
|
+
for (const r of this.attachedRecorders)
|
|
143
|
+
executor.attachCombinedRecorder(r);
|
|
144
|
+
return executor;
|
|
145
|
+
}
|
|
146
|
+
finalizeResult(executor, result) {
|
|
147
|
+
const paused = this.detectPause(executor, result);
|
|
148
|
+
if (paused)
|
|
149
|
+
return paused;
|
|
150
|
+
if (result instanceof Error)
|
|
151
|
+
throw result;
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
buildChart() {
|
|
155
|
+
const steps = this.steps;
|
|
156
|
+
const compositionId = this.id;
|
|
157
|
+
const compositionName = this.name;
|
|
158
|
+
const seed = (scope) => {
|
|
159
|
+
// The workflow's own input IS step 1's input — no unwrapping, no
|
|
160
|
+
// re-wrapping; that is the whole point of the typed chain.
|
|
161
|
+
scope.current = scope.$getArgs();
|
|
162
|
+
typedEmit(scope, 'agentfootprint.composition.enter', {
|
|
163
|
+
kind: 'Sequence',
|
|
164
|
+
id: compositionId,
|
|
165
|
+
name: compositionName,
|
|
166
|
+
childCount: steps.length,
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
// Root description prefix `Sequence:` is the taxonomy marker every
|
|
170
|
+
// consumer (Lens, FlowchartRecorder.mapTopologyToSteps) already reads.
|
|
171
|
+
let builder = flowChart('Seed', seed, 'seed', {
|
|
172
|
+
...(this.opts.structureRecorders !== undefined && {
|
|
173
|
+
structureRecorders: [...this.opts.structureRecorders],
|
|
174
|
+
}),
|
|
175
|
+
description: `Sequence: ${steps.length}-step typed workflow`,
|
|
176
|
+
});
|
|
177
|
+
steps.forEach((step, index) => {
|
|
178
|
+
const stepNumber = index + 1;
|
|
179
|
+
builder = builder.addSubFlowChartNext(`step-${stepNumber}`, step.getSpec(), `Step ${stepNumber}`, {
|
|
180
|
+
inputMapper: (parent) => toStepArgs(parent.current, stepNumber),
|
|
181
|
+
// Untouched: whatever the step's chart returned is what the next
|
|
182
|
+
// step (or the caller) receives. No string coercion.
|
|
183
|
+
outputMapper: (sfOutput) => ({ current: sfOutput }),
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
builder = builder.addFunction('Finalize', (scope) => {
|
|
187
|
+
typedEmit(scope, 'agentfootprint.composition.exit', {
|
|
188
|
+
kind: 'Sequence',
|
|
189
|
+
id: compositionId,
|
|
190
|
+
name: compositionName,
|
|
191
|
+
status: 'ok',
|
|
192
|
+
durationMs: Date.now() - this.currentRunContext.runStartMs,
|
|
193
|
+
});
|
|
194
|
+
return scope.current;
|
|
195
|
+
}, 'finalize', 'Workflow finalize');
|
|
196
|
+
return builder.build();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
export function workflow(...steps) {
|
|
200
|
+
if (steps.length === 0) {
|
|
201
|
+
throw new Error('workflow(): needs at least one step');
|
|
202
|
+
}
|
|
203
|
+
return new Workflow(steps);
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=Workflow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Workflow.js","sourceRoot":"","sources":["../../../src/core-flow/Workflow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AAEH,OAAO,EACL,iBAAiB,EACjB,SAAS,GAMV,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,gCAAgC,CAAC;AAoC3D;;;;;;;GAOG;AACH,SAAS,UAAU,CAAC,KAAc,EAAE,UAAkB;IACpD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IACzD,IAA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EACV,mBAAmB,CACpB,CAAC;QAEF,OAAO,OAAO,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF;AA+ED,MAAM,UAAU,QAAQ,CAAC,GAAG,KAAyB;IACnD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,QAAQ,CAAC,KAAK,CAA2B,CAAC;AACvD,CAAC"}
|
|
@@ -16,19 +16,74 @@
|
|
|
16
16
|
* All three satisfy the same `Embedder` shape, so they drop into
|
|
17
17
|
* `toolChoiceRecorder({ embedder })` / `semanticPipeline({ embedder })` etc.
|
|
18
18
|
* unchanged. Dimensions differ per model — never mix two in one store.
|
|
19
|
+
*
|
|
20
|
+
* ─── Bundlers / browsers: pass `backend` ────────────────────────────────
|
|
21
|
+
*
|
|
22
|
+
* The lazy `import(spec)` above keeps the peer deps optional, but a BUNDLER
|
|
23
|
+
* cannot see through a variable specifier: the bare name survives into the
|
|
24
|
+
* output and the browser throws
|
|
25
|
+
* `TypeError: Failed to resolve module specifier '@huggingface/transformers'`
|
|
26
|
+
* at first embed. So both on-device factories also accept an ALREADY-IMPORTED
|
|
27
|
+
* module — a static import your own bundler resolves:
|
|
28
|
+
*
|
|
29
|
+
* import * as transformers from '@huggingface/transformers';
|
|
30
|
+
* const embedder = localEmbedder({ backend: transformers });
|
|
31
|
+
*
|
|
32
|
+
* Same mechanism as the `client` option on the store adapters (RedisStore,
|
|
33
|
+
* AgentCoreStore): the library states the surface it needs, the host owns the
|
|
34
|
+
* construction. Nothing changes for Node callers who pass nothing.
|
|
19
35
|
*/
|
|
36
|
+
export type { Embedder } from '../memory/embedding/types.js';
|
|
20
37
|
import type { Embedder } from '../memory/embedding/types.js';
|
|
21
38
|
export interface OpenAIEmbedderOptions {
|
|
22
39
|
/** Default: process.env.OPENAI_API_KEY. */
|
|
23
40
|
readonly apiKey?: string;
|
|
24
41
|
/** Default: 'text-embedding-3-small'. */
|
|
25
42
|
readonly model?: string;
|
|
26
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Shorten the vectors the model returns (OpenAI's Matryoshka truncation).
|
|
45
|
+
*
|
|
46
|
+
* When set, the value is SENT as the `dimensions` request parameter AND
|
|
47
|
+
* reported as `.dimensions` — the two can never disagree. Only supported on
|
|
48
|
+
* `text-embedding-3` and later models; ada-002 rejects it, which is exactly
|
|
49
|
+
* why nothing is sent unless you ask.
|
|
50
|
+
*
|
|
51
|
+
* Leave it unset to get the model's native size (looked up from
|
|
52
|
+
* {@link NATIVE_DIMENSIONS}). Required for a model this library doesn't know
|
|
53
|
+
* — see {@link openaiEmbedder}.
|
|
54
|
+
*/
|
|
27
55
|
readonly dimensions?: number;
|
|
28
56
|
/** Override the API base (Azure/OpenAI-compatible gateways). */
|
|
29
57
|
readonly baseURL?: string;
|
|
30
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* OpenAI's hosted embeddings endpoint.
|
|
61
|
+
*
|
|
62
|
+
* `.dimensions` is the length callers WILL get back, never an assumption:
|
|
63
|
+
* an explicit `{ dimensions }` is sent to the API and reported; otherwise the
|
|
64
|
+
* model's documented native size is reported. A model outside
|
|
65
|
+
* {@link NATIVE_DIMENSIONS} (a gateway, a self-hosted model behind `baseURL`,
|
|
66
|
+
* an Azure deployment name, a future OpenAI model) has no size this library can
|
|
67
|
+
* know, so it is a construction-time error rather than a guess that a vector
|
|
68
|
+
* store would silently trust.
|
|
69
|
+
*
|
|
70
|
+
* @throws if there is no API key, or if `model` is unknown and `dimensions`
|
|
71
|
+
* was not supplied.
|
|
72
|
+
*/
|
|
31
73
|
export declare function openaiEmbedder(options?: OpenAIEmbedderOptions): Embedder;
|
|
74
|
+
/**
|
|
75
|
+
* The slice of `@huggingface/transformers` {@link localEmbedder} uses.
|
|
76
|
+
*
|
|
77
|
+
* Structural, so `await import('@huggingface/transformers')` (or a stub, or a
|
|
78
|
+
* pinned fork) satisfies it without this package taking a hard type dependency
|
|
79
|
+
* on the optional peer.
|
|
80
|
+
*/
|
|
81
|
+
export interface TransformersBackend {
|
|
82
|
+
/** transformers.js `pipeline(task, model, options)`. */
|
|
83
|
+
pipeline(task: string, model?: string, options?: Record<string, unknown>): Promise<unknown>;
|
|
84
|
+
/** transformers.js `env` — mutated only when `cacheDir` is set. */
|
|
85
|
+
env?: unknown;
|
|
86
|
+
}
|
|
32
87
|
export interface LocalEmbedderOptions {
|
|
33
88
|
/** ONNX model id. Default 'Xenova/all-MiniLM-L6-v2' (384-dim). */
|
|
34
89
|
readonly model?: string;
|
|
@@ -38,12 +93,52 @@ export interface LocalEmbedderOptions {
|
|
|
38
93
|
readonly dtype?: string;
|
|
39
94
|
/** On-disk model cache directory. */
|
|
40
95
|
readonly cacheDir?: string;
|
|
96
|
+
/**
|
|
97
|
+
* An ALREADY-IMPORTED `@huggingface/transformers`. Supply this and the lazy
|
|
98
|
+
* `import('@huggingface/transformers')` never happens — which is what makes
|
|
99
|
+
* the embedder work in a BUNDLED app, where a bare specifier reaches the
|
|
100
|
+
* browser unresolved:
|
|
101
|
+
*
|
|
102
|
+
* import * as transformers from '@huggingface/transformers';
|
|
103
|
+
* localEmbedder({ backend: transformers });
|
|
104
|
+
*
|
|
105
|
+
* Your bundler resolves that static import; the peer dep stays optional for
|
|
106
|
+
* everyone who doesn't.
|
|
107
|
+
*/
|
|
108
|
+
readonly backend?: TransformersBackend;
|
|
41
109
|
}
|
|
42
110
|
export declare function localEmbedder(options?: LocalEmbedderOptions): Embedder;
|
|
111
|
+
/**
|
|
112
|
+
* The slice of a Model2Vec package {@link staticEmbedder} uses: a batch
|
|
113
|
+
* `embed`/`encode`, on the module or on its default export.
|
|
114
|
+
*
|
|
115
|
+
* Structural, so `await import('@yarflam/potion-base-8m')` — or any other
|
|
116
|
+
* Model2Vec build with one of those shapes — satisfies it.
|
|
117
|
+
*/
|
|
118
|
+
export interface Model2VecBackend {
|
|
119
|
+
/** Batch embed: `embed(texts) => vectors` (may be async). */
|
|
120
|
+
embed?(texts: readonly string[]): unknown;
|
|
121
|
+
/** Alternative name some builds use. */
|
|
122
|
+
encode?(texts: readonly string[]): unknown;
|
|
123
|
+
/** A default export that is the fn, or carries `embed`/`encode`. */
|
|
124
|
+
readonly default?: unknown;
|
|
125
|
+
}
|
|
43
126
|
export interface StaticEmbedderOptions {
|
|
44
127
|
/** Vector length of the bundled model. Default 256 (potion-base-8m). */
|
|
45
128
|
readonly dimensions?: number;
|
|
46
129
|
/** Override the package specifier for a different Model2Vec build. */
|
|
47
130
|
readonly module?: string;
|
|
131
|
+
/**
|
|
132
|
+
* An ALREADY-IMPORTED Model2Vec module. Supply this and no dynamic import
|
|
133
|
+
* happens — the only way this embedder can run in a BUNDLED app, since a
|
|
134
|
+
* bundler cannot resolve the specifier `module` names:
|
|
135
|
+
*
|
|
136
|
+
* import * as potion from '@yarflam/potion-base-8m';
|
|
137
|
+
* staticEmbedder({ backend: potion });
|
|
138
|
+
*
|
|
139
|
+
* Takes precedence over `module`. (The potion backend itself is Node-only
|
|
140
|
+
* today — see the embedders guide.)
|
|
141
|
+
*/
|
|
142
|
+
readonly backend?: Model2VecBackend;
|
|
48
143
|
}
|
|
49
144
|
export declare function staticEmbedder(options?: StaticEmbedderOptions): Embedder;
|
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native output size of every OpenAI embedding model, so `.dimensions` reports
|
|
3
|
+
* the truth instead of one hard-coded guess.
|
|
4
|
+
*
|
|
5
|
+
* Sources: OpenAI embeddings guide — text-embedding-3-small "By default, the
|
|
6
|
+
* length of the embedding vector is 1536", text-embedding-3-large "3072"
|
|
7
|
+
* (https://developers.openai.com/api/docs/guides/embeddings); Microsoft Learn's
|
|
8
|
+
* Azure OpenAI model table, "Output Dimensions" column, for ada-002 = 1,536
|
|
9
|
+
* (https://learn.microsoft.com/en-us/azure/ai-foundry/openai/concepts/models).
|
|
10
|
+
*/
|
|
11
|
+
const NATIVE_DIMENSIONS = {
|
|
12
|
+
'text-embedding-3-small': 1536,
|
|
13
|
+
'text-embedding-3-large': 3072,
|
|
14
|
+
'text-embedding-ada-002': 1536,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* OpenAI's hosted embeddings endpoint.
|
|
18
|
+
*
|
|
19
|
+
* `.dimensions` is the length callers WILL get back, never an assumption:
|
|
20
|
+
* an explicit `{ dimensions }` is sent to the API and reported; otherwise the
|
|
21
|
+
* model's documented native size is reported. A model outside
|
|
22
|
+
* {@link NATIVE_DIMENSIONS} (a gateway, a self-hosted model behind `baseURL`,
|
|
23
|
+
* an Azure deployment name, a future OpenAI model) has no size this library can
|
|
24
|
+
* know, so it is a construction-time error rather than a guess that a vector
|
|
25
|
+
* store would silently trust.
|
|
26
|
+
*
|
|
27
|
+
* @throws if there is no API key, or if `model` is unknown and `dimensions`
|
|
28
|
+
* was not supplied.
|
|
29
|
+
*/
|
|
1
30
|
export function openaiEmbedder(options = {}) {
|
|
2
31
|
const apiKey = options.apiKey ??
|
|
3
32
|
(typeof process !== 'undefined' ? process.env?.['OPENAI_API_KEY'] : undefined);
|
|
@@ -5,13 +34,22 @@ export function openaiEmbedder(options = {}) {
|
|
|
5
34
|
throw new Error('openaiEmbedder: no API key — set OPENAI_API_KEY or pass { apiKey }.');
|
|
6
35
|
}
|
|
7
36
|
const model = options.model ?? 'text-embedding-3-small';
|
|
8
|
-
|
|
37
|
+
// Only an EXPLICIT request is sent. Defaulting it and sending that would
|
|
38
|
+
// break ada-002 (which rejects the parameter) for callers who asked for
|
|
39
|
+
// nothing — the request body stays byte-identical unless you opt in.
|
|
40
|
+
const requested = options.dimensions;
|
|
41
|
+
const dimensions = requested ?? NATIVE_DIMENSIONS[model];
|
|
42
|
+
if (dimensions === undefined) {
|
|
43
|
+
throw new Error(`openaiEmbedder: unknown model '${model}' — its vector length is not something this ` +
|
|
44
|
+
`library can know, and reporting a wrong .dimensions silently corrupts a vector store. ` +
|
|
45
|
+
`Pass { dimensions } with the length that model returns.`);
|
|
46
|
+
}
|
|
9
47
|
const url = `${options.baseURL ?? 'https://api.openai.com/v1'}/embeddings`;
|
|
10
48
|
async function call(input, signal) {
|
|
11
49
|
const res = await fetch(url, {
|
|
12
50
|
method: 'POST',
|
|
13
51
|
headers: { 'content-type': 'application/json', authorization: `Bearer ${apiKey}` },
|
|
14
|
-
body: JSON.stringify({ model, input }),
|
|
52
|
+
body: JSON.stringify(requested === undefined ? { model, input } : { model, input, dimensions: requested }),
|
|
15
53
|
...(signal ? { signal } : {}),
|
|
16
54
|
});
|
|
17
55
|
if (!res.ok)
|
|
@@ -34,17 +72,22 @@ export function localEmbedder(options = {}) {
|
|
|
34
72
|
const dimensions = options.dimensions ?? 384;
|
|
35
73
|
const dtype = options.dtype ?? 'q8';
|
|
36
74
|
let pipe;
|
|
75
|
+
const build = (m) => {
|
|
76
|
+
if (options.cacheDir && m.env && typeof m.env === 'object') {
|
|
77
|
+
m.env['cacheDir'] = options.cacheDir;
|
|
78
|
+
}
|
|
79
|
+
return m.pipeline('feature-extraction', model, { dtype });
|
|
80
|
+
};
|
|
37
81
|
const getPipe = () => {
|
|
82
|
+
const injected = options.backend;
|
|
83
|
+
if (injected)
|
|
84
|
+
return (pipe ??= build(injected));
|
|
38
85
|
// Variable specifier so the compiler/bundler does NOT resolve the module at
|
|
39
86
|
// build time — @huggingface/transformers stays an optional peer dep, loaded
|
|
40
|
-
// only when localEmbedder is actually used.
|
|
87
|
+
// only when localEmbedder is actually used. A bundler cannot see through
|
|
88
|
+
// this; bundled apps pass { backend } instead.
|
|
41
89
|
const spec = '@huggingface/transformers';
|
|
42
|
-
return (pipe ??= import(spec).then((mod) =>
|
|
43
|
-
const m = mod;
|
|
44
|
-
if (options.cacheDir)
|
|
45
|
-
m.env['cacheDir'] = options.cacheDir;
|
|
46
|
-
return m.pipeline('feature-extraction', model, { dtype });
|
|
47
|
-
}));
|
|
90
|
+
return (pipe ??= import(spec).then((mod) => build(mod)));
|
|
48
91
|
};
|
|
49
92
|
return {
|
|
50
93
|
dimensions,
|
|
@@ -64,24 +107,29 @@ export function staticEmbedder(options = {}) {
|
|
|
64
107
|
const dimensions = options.dimensions ?? 256;
|
|
65
108
|
const spec = options.module ?? '@yarflam/potion-base-8m';
|
|
66
109
|
let embedFn;
|
|
110
|
+
// potion-base-8m exports `embed(texts) => Promise<Float32Array[]>` (a batch
|
|
111
|
+
// async fn, also on its default export). Accept a small set of shapes so
|
|
112
|
+
// other Model2Vec builds slot in: a named `embed`/`encode` on the module or
|
|
113
|
+
// its default, or a default export that IS the fn.
|
|
114
|
+
const pick = (mod, source) => {
|
|
115
|
+
const m = mod;
|
|
116
|
+
const d = (m.default ?? {});
|
|
117
|
+
const fn = m['embed'] ??
|
|
118
|
+
d['embed'] ??
|
|
119
|
+
m['encode'] ??
|
|
120
|
+
d['encode'] ??
|
|
121
|
+
(typeof m.default === 'function' ? m.default : undefined);
|
|
122
|
+
if (!fn) {
|
|
123
|
+
throw new Error(`staticEmbedder: no embed()/encode() export on ${source}. Pass { module } or wrap it in your own Embedder.`);
|
|
124
|
+
}
|
|
125
|
+
return fn;
|
|
126
|
+
};
|
|
67
127
|
const getEmbed = () => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const m = mod;
|
|
74
|
-
const d = (m.default ?? {});
|
|
75
|
-
const fn = m['embed'] ??
|
|
76
|
-
d['embed'] ??
|
|
77
|
-
m['encode'] ??
|
|
78
|
-
d['encode'] ??
|
|
79
|
-
(typeof m.default === 'function' ? m.default : undefined);
|
|
80
|
-
if (!fn) {
|
|
81
|
-
throw new Error(`staticEmbedder: no embed()/encode() export on '${spec}'. Pass { module } or wrap it in your own Embedder.`);
|
|
82
|
-
}
|
|
83
|
-
return fn;
|
|
84
|
-
}));
|
|
128
|
+
const injected = options.backend;
|
|
129
|
+
if (injected) {
|
|
130
|
+
return (embedFn ??= Promise.resolve(pick(injected, 'the module passed as { backend }')));
|
|
131
|
+
}
|
|
132
|
+
return (embedFn ??= import(spec).then((mod) => pick(mod, `'${spec}'`)));
|
|
85
133
|
};
|
|
86
134
|
// Normalize a batch result into number[][] (one row per input). Handles
|
|
87
135
|
// Float32Array[] (potion), number[][], and a single flat vector for the call.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/embedders/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/embedders/index.ts"],"names":[],"mappings":"AAgEA;;;;;;;;;GASG;AACH,MAAM,iBAAiB,GAAqC;IAC1D,wBAAwB,EAAE,IAAI;IAC9B,wBAAwB,EAAE,IAAI;IAC9B,wBAAwB,EAAE,IAAI;CAC/B,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,cAAc,CAAC,UAAiC,EAAE;IAChE,MAAM,MAAM,GACV,OAAO,CAAC,MAAM;QACd,CAAC,OAAO,OAAO,KAAK,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IACjF,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,qEAAqE,CAAC,CAAC;IACzF,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,wBAAwB,CAAC;IACxD,yEAAyE;IACzE,wEAAwE;IACxE,qEAAqE;IACrE,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;IACrC,MAAM,UAAU,GAAG,SAAS,IAAI,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACzD,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CACb,kCAAkC,KAAK,8CAA8C;YACnF,wFAAwF;YACxF,yDAAyD,CAC5D,C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EAAE,IAAI,EAAE;YAClB,MAAM,EAAE,GAAG,MAAM,QAAQ,EAAE,CAAC;YAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACtC,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACvB,CAAC;QACD,KAAK,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE;YACxB,MAAM,EAAE,GAAG,MAAM,QAAQ,EAAE,CAAC;YAC5B,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACtC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export { Sequence, SequenceBuilder, type SequenceInput, type SequenceOptions, ty
|
|
|
42
42
|
export { Parallel, ParallelBuilder, type BranchOutcome, type MergeFn, type MergeOutcomesFn, type MergeWithLLMOptions, type ParallelBranchOptions, type ParallelInput, type ParallelOptions, type ParallelOutput, } from './core-flow/Parallel.js';
|
|
43
43
|
export { Conditional, ConditionalBuilder, type ConditionalInput, type ConditionalOptions, type ConditionalOutput, type Predicate, } from './core-flow/Conditional.js';
|
|
44
44
|
export { Loop, LoopBuilder, type LoopInput, type LoopOptions, type LoopOutput, type UntilGuard, } from './core-flow/Loop.js';
|
|
45
|
+
export { workflow, Workflow, type NextStepInput, type WorkflowOptions, } from './core-flow/Workflow.js';
|
|
45
46
|
export { providerFromEnv, type ProviderKind, type CreateProviderOptions, type ProviderFromEnv, } from './adapters/llm/createProvider.js';
|
|
46
47
|
export * from './patterns/index.js';
|
|
47
48
|
export { defineRAG, type DefineRAGOptions, indexDocuments, type IndexDocumentsOptions, type RagDocument, } from './lib/rag/index.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -102,6 +102,7 @@ export { Sequence, SequenceBuilder, } from './core-flow/Sequence.js';
|
|
|
102
102
|
export { Parallel, ParallelBuilder, } from './core-flow/Parallel.js';
|
|
103
103
|
export { Conditional, ConditionalBuilder, } from './core-flow/Conditional.js';
|
|
104
104
|
export { Loop, LoopBuilder, } from './core-flow/Loop.js';
|
|
105
|
+
export { workflow, Workflow, } from './core-flow/Workflow.js';
|
|
105
106
|
// Adapters — LLM providers
|
|
106
107
|
// `mock(...)` is the lowercase factory equivalent to `new MockProvider(...)`.
|
|
107
108
|
// `anthropic(...)` is the real Claude provider via `@anthropic-ai/sdk`.
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,oEAAoE;AACpE,8DAA8D;AAC9D,uEAAuE;AACvE,kEAAkE;AAClE,sEAAsE;AACtE,sDAAsD;AACtD,OAAO,8CAA8C,CAAC;AACtD,OAAO,2CAA2C,CAAC;AACnD,OAAO,4CAA4C,CAAC;AAsCpD,6BAA6B;AAC7B,cAAc,qBAAqB,CAAC;AAEpC,yEAAyE;AACzE,uDAAuD;AACvD,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,cAAc;AAEd,yEAAyE;AACzE,wEAAwE;AACxE,yEAAyE;AACzE,gBAAgB;AAChB,SAAS;AAET,2EAA2E;AAC3E,6EAA6E;AAC7E,6EAA6E;AAC7E,6DAA6D;AAC7D,YAAY,GAGb,MAAM,kBAAkB,CAAC;AAC1B,oEAAoE;AACpE,yEAAyE;AACzE,wEAAwE;AACxE,qEAAqE;AACrE,kDAAkD;AAClD,OAAO,EACL,gBAAgB,GAMjB,MAAM,2BAA2B,CAAC;AAKnC,yCAAyC;AACzC,EAAE;AACF,yEAAyE;AACzE,kEAAkE;AAClE,oEAAoE;AACpE,qEAAqE;AACrE,2EAA2E;AAC3E,8EAA8E;AAC9E,0DAA0D;AAC1D,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,gB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