@alvin0/ai-agent-sdk-core 0.1.0 → 0.1.1
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/define/session/model-config.js +1 -1
- package/dist/agent/define/session/model-config.js.map +1 -1
- package/dist/agent/define/session/types.d.ts +21 -0
- package/dist/agent/define/session/types.d.ts.map +1 -1
- package/dist/agent/define/session.d.ts +2 -0
- package/dist/agent/define/session.d.ts.map +1 -1
- package/dist/agent/define/session.js +1 -1
- package/dist/agent/define/session.js.map +1 -1
- package/dist/agent/history/validation.js +1 -1
- package/dist/agent/history/validation.js.map +1 -1
- package/dist/agent/loop/events.d.ts +5 -0
- package/dist/agent/loop/events.d.ts.map +1 -1
- package/dist/agent/loop/schedule.js +1 -1
- package/dist/agent/loop/schedule.js.map +1 -1
- package/dist/agent/loop/turn/model-round.js +1 -1
- package/dist/agent/loop/turn/model-round.js.map +1 -1
- package/dist/agent/loop/turn/types.d.ts +2 -0
- package/dist/agent/loop/turn/types.d.ts.map +1 -1
- package/dist/agent/loop/turn/validation.js +1 -1
- package/dist/agent/loop/turn/validation.js.map +1 -1
- package/dist/agent/memory/compaction.d.ts +10 -1
- package/dist/agent/memory/compaction.d.ts.map +1 -1
- package/dist/agent/memory/compaction.js +2 -2
- package/dist/agent/memory/compaction.js.map +1 -1
- package/dist/agent/memory/token-estimator.js +1 -1
- package/dist/agent/memory/token-estimator.js.map +1 -1
- package/dist/agent/mode/run-agent.js +1 -1
- package/dist/agent/mode/run-agent.js.map +1 -1
- package/dist/composition/agent/options.js +1 -1
- package/dist/composition/agent/options.js.map +1 -1
- package/dist/composition/agent/session.js +1 -1
- package/dist/composition/agent/session.js.map +1 -1
- package/dist/composition/agent/types.d.ts +43 -1
- package/dist/composition/agent/types.d.ts.map +1 -1
- package/dist/composition/common/errors.d.ts.map +1 -1
- package/dist/composition/common/errors.js +1 -1
- package/dist/composition/common/errors.js.map +1 -1
- package/dist/composition/embedding/activation.js +2 -0
- package/dist/composition/embedding/activation.js.map +1 -0
- package/dist/composition/embedding/cache.js +2 -0
- package/dist/composition/embedding/cache.js.map +1 -0
- package/dist/composition/embedding/definition.d.ts +7 -0
- package/dist/composition/embedding/definition.d.ts.map +1 -0
- package/dist/composition/embedding/definition.js +2 -0
- package/dist/composition/embedding/definition.js.map +1 -0
- package/dist/composition/embedding/handle.js +2 -0
- package/dist/composition/embedding/handle.js.map +1 -0
- package/dist/composition/embedding/limiter.js +2 -0
- package/dist/composition/embedding/limiter.js.map +1 -0
- package/dist/composition/embedding/manager.js +2 -0
- package/dist/composition/embedding/manager.js.map +1 -0
- package/dist/composition/embedding/observation.js +2 -0
- package/dist/composition/embedding/observation.js.map +1 -0
- package/dist/composition/embedding/planner.js +2 -0
- package/dist/composition/embedding/planner.js.map +1 -0
- package/dist/composition/embedding/plugin-types.d.ts +48 -0
- package/dist/composition/embedding/plugin-types.d.ts.map +1 -0
- package/dist/composition/embedding/plugin-types.js +2 -0
- package/dist/composition/embedding/plugin-types.js.map +1 -0
- package/dist/composition/embedding/preflight.js +2 -0
- package/dist/composition/embedding/preflight.js.map +1 -0
- package/dist/composition/embedding/registry.js +2 -0
- package/dist/composition/embedding/registry.js.map +1 -0
- package/dist/composition/embedding/retry.js +2 -0
- package/dist/composition/embedding/retry.js.map +1 -0
- package/dist/composition/embedding/usage.js +2 -0
- package/dist/composition/embedding/usage.js.map +1 -0
- package/dist/composition/lifecycle/types.d.ts +5 -1
- package/dist/composition/lifecycle/types.d.ts.map +1 -1
- package/dist/composition/lifecycle/types.js +1 -1
- package/dist/composition/lifecycle/types.js.map +1 -1
- package/dist/composition/preflight.js +1 -1
- package/dist/composition/preflight.js.map +1 -1
- package/dist/composition/provider/activation.js +1 -1
- package/dist/composition/provider/activation.js.map +1 -1
- package/dist/composition/provider/model-selection.js +1 -1
- package/dist/composition/provider/model-selection.js.map +1 -1
- package/dist/composition/provider/preflight.js +1 -1
- package/dist/composition/provider/preflight.js.map +1 -1
- package/dist/composition/runtime/owner.js +1 -1
- package/dist/composition/runtime/owner.js.map +1 -1
- package/dist/composition/runtime/public.js +1 -1
- package/dist/composition/runtime/public.js.map +1 -1
- package/dist/composition/runtime/types.d.ts +25 -2
- package/dist/composition/runtime/types.d.ts.map +1 -1
- package/dist/composition/startup.js +1 -1
- package/dist/composition/startup.js.map +1 -1
- package/dist/composition/team/options.js +1 -1
- package/dist/composition/team/options.js.map +1 -1
- package/dist/composition/team/runtime.js +1 -1
- package/dist/composition/team/runtime.js.map +1 -1
- package/dist/composition/team/types.d.ts +3 -0
- package/dist/composition/team/types.d.ts.map +1 -1
- package/dist/contract/generate-options.d.ts +2 -0
- package/dist/contract/generate-options.d.ts.map +1 -1
- package/dist/contract/model-info.d.ts +12 -2
- package/dist/contract/model-info.d.ts.map +1 -1
- package/dist/embedding/adapter.d.ts +104 -0
- package/dist/embedding/adapter.d.ts.map +1 -0
- package/dist/embedding/adapter.js +2 -0
- package/dist/embedding/adapter.js.map +1 -0
- package/dist/embedding/catalog.d.ts +82 -0
- package/dist/embedding/catalog.d.ts.map +1 -0
- package/dist/embedding/catalog.js +2 -0
- package/dist/embedding/catalog.js.map +1 -0
- package/dist/embedding/errors.d.ts +98 -0
- package/dist/embedding/errors.d.ts.map +1 -0
- package/dist/embedding/errors.js +2 -0
- package/dist/embedding/errors.js.map +1 -0
- package/dist/embedding/handle.d.ts +76 -0
- package/dist/embedding/handle.d.ts.map +1 -0
- package/dist/embedding/limits.d.ts +47 -0
- package/dist/embedding/limits.d.ts.map +1 -0
- package/dist/embedding/limits.js +2 -0
- package/dist/embedding/limits.js.map +1 -0
- package/dist/embedding/profile.d.ts +89 -0
- package/dist/embedding/profile.d.ts.map +1 -0
- package/dist/embedding/profile.js +2 -0
- package/dist/embedding/profile.js.map +1 -0
- package/dist/embedding/purpose.d.ts +37 -0
- package/dist/embedding/purpose.d.ts.map +1 -0
- package/dist/embedding/request.d.ts +54 -0
- package/dist/embedding/request.d.ts.map +1 -0
- package/dist/embedding/request.js +2 -0
- package/dist/embedding/request.js.map +1 -0
- package/dist/embedding/result.d.ts +50 -0
- package/dist/embedding/result.d.ts.map +1 -0
- package/dist/embedding/usage.d.ts +64 -0
- package/dist/embedding/usage.d.ts.map +1 -0
- package/dist/embedding/usage.js +2 -0
- package/dist/embedding/usage.js.map +1 -0
- package/dist/embedding/validation.d.ts +68 -0
- package/dist/embedding/validation.d.ts.map +1 -0
- package/dist/embedding/validation.js +2 -0
- package/dist/embedding/validation.js.map +1 -0
- package/dist/embedding.d.ts +12 -0
- package/dist/embedding.js +1 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.js +1 -1
- package/dist/message/content.d.ts +71 -1
- package/dist/message/content.d.ts.map +1 -1
- package/dist/message/projection.d.ts +28 -2
- package/dist/message/projection.d.ts.map +1 -1
- package/dist/message/projection.js +1 -1
- package/dist/message/projection.js.map +1 -1
- package/dist/observability/bus.js +1 -1
- package/dist/observation/event.d.ts +1 -1
- package/dist/observation/event.d.ts.map +1 -1
- package/dist/observation/event.js.map +1 -1
- package/dist/observation/port.d.ts +1 -1
- package/dist/observation/port.d.ts.map +1 -1
- package/dist/observation/port.js.map +1 -1
- package/dist/observation/privacy.js +1 -1
- package/dist/observation/privacy.js.map +1 -1
- package/dist/observation/report.d.ts +2 -0
- package/dist/observation/report.d.ts.map +1 -1
- package/dist/observation/report.js.map +1 -1
- package/dist/primitives/version.d.ts +1 -1
- package/dist/primitives/version.js +1 -1
- package/dist/primitives/version.js.map +1 -1
- package/dist/provider.d.ts +3 -1
- package/dist/provider.js +1 -1
- package/dist/runtime/model-call-handle.js +1 -1
- package/dist/runtime/model-call-handle.js.map +1 -1
- package/dist/runtime/model-metadata.js +1 -1
- package/dist/runtime/model-metadata.js.map +1 -1
- package/dist/runtime/model-stream.js +1 -1
- package/dist/runtime/model-stream.js.map +1 -1
- package/dist/runtime/registry.js +1 -1
- package/dist/runtime/with-retry.d.ts.map +1 -1
- package/dist/runtime/with-retry.js +1 -1
- package/dist/runtime/with-retry.js.map +1 -1
- package/dist/stream/assembler.d.ts.map +1 -1
- package/dist/stream/assembler.js +1 -1
- package/dist/stream/assembler.js.map +1 -1
- package/dist/stream/chunk.d.ts +6 -0
- package/dist/stream/chunk.d.ts.map +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { EmbeddingSpaceId } from "./profile.js";
|
|
2
|
+
import { EmbeddingPurpose } from "./purpose.js";
|
|
3
|
+
import { ResolvedEmbeddingBatchLimits } from "./limits.js";
|
|
4
|
+
import { EmbeddingContentPart, EmbeddingTruncation } from "./request.js";
|
|
5
|
+
import { EmbeddingManyResult, EmbeddingResult } from "./result.js";
|
|
6
|
+
//#region src/embedding/handle.d.ts
|
|
7
|
+
/** One cached vector, stored with the space identity it was produced in. */
|
|
8
|
+
interface EmbeddingCacheEntry {
|
|
9
|
+
readonly values: readonly number[];
|
|
10
|
+
/**
|
|
11
|
+
* `Space_Id` at the time of writing. A read whose space does not match the
|
|
12
|
+
* current `PreparedEmbeddingCall` is discarded rather than trusted.
|
|
13
|
+
*/
|
|
14
|
+
readonly space: EmbeddingSpaceId;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Caller-supplied cache backing. Sync or async implementations are both
|
|
18
|
+
* accepted so an in-process `Map` needs no promise ceremony.
|
|
19
|
+
*/
|
|
20
|
+
interface EmbeddingCacheStore {
|
|
21
|
+
get(key: string): Promise<EmbeddingCacheEntry | undefined> | EmbeddingCacheEntry | undefined;
|
|
22
|
+
set(key: string, entry: EmbeddingCacheEntry): Promise<void> | void;
|
|
23
|
+
}
|
|
24
|
+
/** Opt-in embedding cache. Disabled unless supplied. */
|
|
25
|
+
interface EmbeddingCacheOptions {
|
|
26
|
+
readonly store: EmbeddingCacheStore;
|
|
27
|
+
/**
|
|
28
|
+
* REQUIRED security scope, no default (DD-8). It is the first component of
|
|
29
|
+
* every cache key, so a missing scope is a configuration error rather than
|
|
30
|
+
* something the runtime silently fills in.
|
|
31
|
+
*/
|
|
32
|
+
readonly scope: string;
|
|
33
|
+
}
|
|
34
|
+
/** Configuration of one `EmbeddingModelHandle`. */
|
|
35
|
+
interface EmbeddingModelOptions {
|
|
36
|
+
/** Provider route key that must own an `Embedding_Adapter`. */
|
|
37
|
+
readonly provider: string;
|
|
38
|
+
readonly model: string;
|
|
39
|
+
/** Requested dimensions; absent means the model default. */
|
|
40
|
+
readonly dimensions?: number;
|
|
41
|
+
/** SDK default is `'reject'`, even where the provider default is to cut. */
|
|
42
|
+
readonly truncation?: EmbeddingTruncation;
|
|
43
|
+
/** Expected `Space_Id`; an incompatible resolution rejects the call. */
|
|
44
|
+
readonly expectedSpace?: EmbeddingSpaceId;
|
|
45
|
+
/** Upper bound on in-flight `Physical_Batch`es of one `Logical_Call`. */
|
|
46
|
+
readonly concurrency?: number;
|
|
47
|
+
readonly batchLimits?: Partial<ResolvedEmbeddingBatchLimits>;
|
|
48
|
+
readonly cache?: EmbeddingCacheOptions;
|
|
49
|
+
}
|
|
50
|
+
/** Input of {@link EmbeddingModelHandle.embed}: exactly one object to embed. */
|
|
51
|
+
interface EmbedOneInput {
|
|
52
|
+
readonly value: string | readonly EmbeddingContentPart[];
|
|
53
|
+
readonly purpose: EmbeddingPurpose;
|
|
54
|
+
readonly signal?: AbortSignal;
|
|
55
|
+
readonly expectedSpace?: EmbeddingSpaceId;
|
|
56
|
+
}
|
|
57
|
+
/** Input of {@link EmbeddingModelHandle.embedMany}; output follows this order. */
|
|
58
|
+
interface EmbedManyInput {
|
|
59
|
+
readonly values: readonly (string | readonly EmbeddingContentPart[])[];
|
|
60
|
+
readonly purpose: EmbeddingPurpose;
|
|
61
|
+
readonly signal?: AbortSignal;
|
|
62
|
+
readonly expectedSpace?: EmbeddingSpaceId;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* What `runtime.embeddingModel()` hands back.
|
|
66
|
+
*
|
|
67
|
+
* Both methods are one `Logical_Call` each: batching, retry, concurrency and
|
|
68
|
+
* order restoration happen behind them and are never the caller's concern.
|
|
69
|
+
*/
|
|
70
|
+
interface EmbeddingModelHandle {
|
|
71
|
+
embed(input: EmbedOneInput): Promise<EmbeddingResult>;
|
|
72
|
+
embedMany(input: EmbedManyInput): Promise<EmbeddingManyResult>;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { EmbedManyInput, EmbedOneInput, EmbeddingCacheEntry, EmbeddingCacheOptions, EmbeddingCacheStore, EmbeddingModelHandle, EmbeddingModelOptions };
|
|
76
|
+
//# sourceMappingURL=handle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handle.d.ts","names":[],"sources":["../../src/embedding/handle.ts"],"mappings":";;;;;;;UA+BiB;WACN;;;;;WAKA,OAAO;;;;;;UAOD;EACf,IAAI,cAAc,QAAQ,mCAAmC;EAC7D,IAAI,aAAa,OAAO,sBAAsB;;;UAI/B;WACN,OAAO;;;;;;WAMP;;;UAIM;;WAEN;WACA;;WAEA;;WAEA,aAAa;;WAEb,gBAAgB;;WAEhB;WACA,cAAc,QAAQ;WACtB,QAAQ;;;UAIF;WACN,yBAAyB;WACzB,SAAS;WACT,SAAS;WACT,gBAAgB;;;UAIV;WACN,oCAAoC;WACpC,SAAS;WACT,SAAS;WACT,gBAAgB;;;;;;;;UASV;EACf,MAAM,OAAO,gBAAgB,QAAQ;EACrC,UAAU,OAAO,iBAAiB,QAAQ"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ResolvedEmbeddingModelInfo } from "./catalog.js";
|
|
2
|
+
//#region src/embedding/limits.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Safe fallbacks used for BATCHING when a catalog capability is not a declared
|
|
5
|
+
* number.
|
|
6
|
+
*
|
|
7
|
+
* Batching always needs a finite upper bound, otherwise the memory of one
|
|
8
|
+
* `Logical_Call` is unbounded (Requirement 17.4). Validation is the opposite
|
|
9
|
+
* case: an `unknown` capability is NEVER a reason to reject a request, so these
|
|
10
|
+
* values must not be read as limits the provider claimed (DD-6).
|
|
11
|
+
*/
|
|
12
|
+
declare const EMBEDDING_BATCH_DEFAULTS: Readonly<{
|
|
13
|
+
readonly maxItems: 96;
|
|
14
|
+
readonly maxTokens: 100000;
|
|
15
|
+
readonly maxBytes: number;
|
|
16
|
+
}>;
|
|
17
|
+
/** Fully decided batch bounds plus the estimator that measures against them. */
|
|
18
|
+
interface ResolvedEmbeddingBatchLimits {
|
|
19
|
+
readonly maxItems: number;
|
|
20
|
+
readonly maxTokens: number;
|
|
21
|
+
readonly maxBytes: number;
|
|
22
|
+
readonly estimateTokens: (text: string) => number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Shared token estimate for batching and for input-length checks.
|
|
26
|
+
*
|
|
27
|
+
* Default `ceil(utf8Bytes / 4)`. This is an ESTIMATE, not a provider tokenizer:
|
|
28
|
+
* it is used only to split batches and to reject an input when `maxInputTokens`
|
|
29
|
+
* is `supported` and the estimate already exceeds it. An adapter with a closer
|
|
30
|
+
* estimator passes it through `limits.estimateTokens`.
|
|
31
|
+
*
|
|
32
|
+
* There is exactly ONE owner of this heuristic — this function — so a batch
|
|
33
|
+
* split and a length check can never disagree about the size of the same text.
|
|
34
|
+
*/
|
|
35
|
+
declare function estimateTokens(text: string): number;
|
|
36
|
+
/**
|
|
37
|
+
* Merges caller overrides over the catalog's declared bounds, falling back to
|
|
38
|
+
* {@link EMBEDDING_BATCH_DEFAULTS} for anything the catalog leaves `unknown`.
|
|
39
|
+
*
|
|
40
|
+
* Precedence is override → catalog → default. Because the fallback is total, the
|
|
41
|
+
* result is always finite, which is what lets a model id outside the catalog be
|
|
42
|
+
* batched without being rejected.
|
|
43
|
+
*/
|
|
44
|
+
declare function resolveBatchLimits(model: ResolvedEmbeddingModelInfo, overrides?: Partial<ResolvedEmbeddingBatchLimits>): ResolvedEmbeddingBatchLimits;
|
|
45
|
+
//#endregion
|
|
46
|
+
export { EMBEDDING_BATCH_DEFAULTS, ResolvedEmbeddingBatchLimits, estimateTokens, resolveBatchLimits };
|
|
47
|
+
//# sourceMappingURL=limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.d.ts","names":[],"sources":["../../src/embedding/limits.ts"],"mappings":";;;;;;;;;;;cAuBa,0BAAwB;WACzB;WACC;;;;UAKI;WACN;WACA;WACA;WACA,iBAAiB;;;;;;;;;;;;;iBAmBZ,eAAe;;;;;;;;;iBAwBf,mBACd,OAAO,4BACP,YAAY,QAAQ,gCACnB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=Object.freeze({maxItems:96,maxTokens:1e5,maxBytes:1048576}),t=new TextEncoder;function n(e){return Math.ceil(t.encode(e).byteLength/4)}function r(e){if(e.state===`supported`)return i(e.value)}function i(e){if(!(e===void 0||!Number.isInteger(e)||e<=0))return e}function a(t,a){return{maxItems:i(a?.maxItems)??r(t.maxBatchItems)??e.maxItems,maxTokens:i(a?.maxTokens)??r(t.maxBatchTokens)??e.maxTokens,maxBytes:i(a?.maxBytes)??r(t.maxBatchBytes)??e.maxBytes,estimateTokens:a?.estimateTokens??n}}export{e as EMBEDDING_BATCH_DEFAULTS,n as estimateTokens,a as resolveBatchLimits};
|
|
2
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.js","names":[],"sources":["../../src/embedding/limits.ts"],"sourcesContent":["/**\n * Batch limits and the one shared token estimator.\n *\n * These are contract DATA, not a composition detail: `prepareEmbeddingCall()`\n * and `PreparedEmbeddingCall` both carry them. Putting them in\n * `composition/embedding/planner.ts` would force `embedding/` to import from\n * `composition/`, the exact reversed dependency Requirements 1.6 and 19.6\n * forbid (DD-12). So they live here, and the planner consumes them.\n *\n * @module ai-agent-sdk/core/embedding/limits\n */\n\nimport type { EmbeddingCapability, ResolvedEmbeddingModelInfo } from './catalog.ts'\n\n/**\n * Safe fallbacks used for BATCHING when a catalog capability is not a declared\n * number.\n *\n * Batching always needs a finite upper bound, otherwise the memory of one\n * `Logical_Call` is unbounded (Requirement 17.4). Validation is the opposite\n * case: an `unknown` capability is NEVER a reason to reject a request, so these\n * values must not be read as limits the provider claimed (DD-6).\n */\nexport const EMBEDDING_BATCH_DEFAULTS = Object.freeze({\n maxItems: 96,\n maxTokens: 100_000,\n maxBytes: 1024 * 1024,\n} as const)\n\n/** Fully decided batch bounds plus the estimator that measures against them. */\nexport interface ResolvedEmbeddingBatchLimits {\n readonly maxItems: number\n readonly maxTokens: number\n readonly maxBytes: number\n readonly estimateTokens: (text: string) => number\n}\n\nconst ENCODER = new TextEncoder()\n\n/** Bytes per estimated token in the default heuristic. */\nconst BYTES_PER_TOKEN = 4\n\n/**\n * Shared token estimate for batching and for input-length checks.\n *\n * Default `ceil(utf8Bytes / 4)`. This is an ESTIMATE, not a provider tokenizer:\n * it is used only to split batches and to reject an input when `maxInputTokens`\n * is `supported` and the estimate already exceeds it. An adapter with a closer\n * estimator passes it through `limits.estimateTokens`.\n *\n * There is exactly ONE owner of this heuristic — this function — so a batch\n * split and a length check can never disagree about the size of the same text.\n */\nexport function estimateTokens(text: string): number {\n return Math.ceil(ENCODER.encode(text).byteLength / BYTES_PER_TOKEN)\n}\n\n/** A declared positive integer bound, or `undefined` when there is none to use. */\nfunction declared(capability: EmbeddingCapability<number>): number | undefined {\n if (capability.state !== 'supported') return undefined\n return override(capability.value)\n}\n\n/** A caller override wins outright, when it is a usable positive integer. */\nfunction override(value: number | undefined): number | undefined {\n if (value === undefined || !Number.isInteger(value) || value <= 0) return undefined\n return value\n}\n\n/**\n * Merges caller overrides over the catalog's declared bounds, falling back to\n * {@link EMBEDDING_BATCH_DEFAULTS} for anything the catalog leaves `unknown`.\n *\n * Precedence is override → catalog → default. Because the fallback is total, the\n * result is always finite, which is what lets a model id outside the catalog be\n * batched without being rejected.\n */\nexport function resolveBatchLimits(\n model: ResolvedEmbeddingModelInfo,\n overrides?: Partial<ResolvedEmbeddingBatchLimits>,\n): ResolvedEmbeddingBatchLimits {\n return {\n maxItems:\n override(overrides?.maxItems)\n ?? declared(model.maxBatchItems)\n ?? EMBEDDING_BATCH_DEFAULTS.maxItems,\n maxTokens:\n override(overrides?.maxTokens)\n ?? declared(model.maxBatchTokens)\n ?? EMBEDDING_BATCH_DEFAULTS.maxTokens,\n maxBytes:\n override(overrides?.maxBytes)\n ?? declared(model.maxBatchBytes)\n ?? EMBEDDING_BATCH_DEFAULTS.maxBytes,\n estimateTokens: overrides?.estimateTokens ?? estimateTokens,\n }\n}\n"],"mappings":"AAuBA,MAAa,EAA2B,OAAO,OAAO,CACpD,SAAU,GACV,UAAW,IACX,SAAU,OACZ,CAAU,EAUJ,EAAU,IAAI,YAgBpB,SAAgB,EAAe,EAAsB,CACnD,OAAO,KAAK,KAAK,EAAQ,OAAO,CAAI,CAAC,CAAC,WAAa,CAAe,CACpE,CAGA,SAAS,EAAS,EAA6D,CACzE,KAAW,QAAU,YACzB,OAAO,EAAS,EAAW,KAAK,CAClC,CAGA,SAAS,EAAS,EAA+C,CAC3D,SAAU,IAAA,IAAa,CAAC,OAAO,UAAU,CAAK,GAAK,GAAS,GAChE,OAAO,CACT,CAUA,SAAgB,EACd,EACA,EAC8B,CAC9B,MAAO,CACL,SACE,EAAS,GAAW,QAAQ,GACzB,EAAS,EAAM,aAAa,GAC5B,EAAyB,SAC9B,UACE,EAAS,GAAW,SAAS,GAC1B,EAAS,EAAM,cAAc,GAC7B,EAAyB,UAC9B,SACE,EAAS,GAAW,QAAQ,GACzB,EAAS,EAAM,aAAa,GAC5B,EAAyB,SAC9B,eAAgB,GAAW,gBAAkB,CAC/C,CACF"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ResolvedEmbeddingModelInfo } from "./catalog.js";
|
|
2
|
+
//#region src/embedding/profile.d.ts
|
|
3
|
+
/** Output representation of a vector. v1 declares exactly one. */
|
|
4
|
+
type EmbeddingRepresentation = 'dense-float32';
|
|
5
|
+
/**
|
|
6
|
+
* Whether vectors arrive normalized.
|
|
7
|
+
*
|
|
8
|
+
* `'unknown'` is a first-class value: a route that has not documented its
|
|
9
|
+
* normalization must say so rather than have `'none'` assumed for it.
|
|
10
|
+
*/
|
|
11
|
+
type EmbeddingNormalization = 'unit-l2' | 'none' | 'unknown';
|
|
12
|
+
/** Post-processing is version-managed; it is never an implicit slice or pad. */
|
|
13
|
+
interface EmbeddingPostProcessing {
|
|
14
|
+
readonly kind: 'l2-renormalize';
|
|
15
|
+
readonly revision: string;
|
|
16
|
+
}
|
|
17
|
+
/** Everything about one call configuration that a vector's meaning depends on. */
|
|
18
|
+
interface EmbeddingProfile {
|
|
19
|
+
/** `${route}:${modelId}` — a model identity, NOT a space identity. */
|
|
20
|
+
readonly modelIdentity: string;
|
|
21
|
+
readonly modelRevision?: string;
|
|
22
|
+
readonly dimensions: number;
|
|
23
|
+
readonly representation: EmbeddingRepresentation;
|
|
24
|
+
readonly normalization: EmbeddingNormalization;
|
|
25
|
+
readonly postProcessing?: EmbeddingPostProcessing;
|
|
26
|
+
/** Recorded for traceability; excluded from the `Space_Id` derivation (DD-7). */
|
|
27
|
+
readonly documentRecipeRevision: string;
|
|
28
|
+
/** Recorded for traceability; excluded from the `Space_Id` derivation (DD-7). */
|
|
29
|
+
readonly queryRecipeRevision: string;
|
|
30
|
+
/** The provider's declaration about the embedding space. Declared by adapter/route. */
|
|
31
|
+
readonly compatibilityIdentity: string;
|
|
32
|
+
readonly profileRevision: string;
|
|
33
|
+
}
|
|
34
|
+
/** Canonical identifier of an embedding space. */
|
|
35
|
+
type EmbeddingSpaceId = string & {
|
|
36
|
+
readonly __brand: 'EmbeddingSpaceId';
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Call configuration a caller supplies on top of resolved catalog metadata.
|
|
40
|
+
*
|
|
41
|
+
* Every field is optional: an adapter that knows nothing beyond the model id
|
|
42
|
+
* still gets a usable profile out of {@link defaultEmbeddingProfile}.
|
|
43
|
+
*/
|
|
44
|
+
interface EmbeddingProfileInput {
|
|
45
|
+
/** Requested dimensions; absent means the model default. */
|
|
46
|
+
readonly dimensions?: number;
|
|
47
|
+
/** Revision of the rule turning a document's content parts into wire input. */
|
|
48
|
+
readonly documentRecipeRevision?: string;
|
|
49
|
+
/** Revision of the rule turning a query's content parts into wire input. */
|
|
50
|
+
readonly queryRecipeRevision?: string;
|
|
51
|
+
/** Bumped when any profile-affecting configuration changes. */
|
|
52
|
+
readonly profileRevision?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Derives the `Space_Id` from what decides whether two vectors are comparable.
|
|
56
|
+
*
|
|
57
|
+
* SYNCHRONOUS and UNHASHED: a `Space_Id` is a canonical string, not a digest.
|
|
58
|
+
* It is an identifier for comparison, not a secret to hide, so no hash function
|
|
59
|
+
* is needed and `packages/core` takes on no `crypto.subtle` dependency (DD-11).
|
|
60
|
+
* Being synchronous, `prepareEmbeddingCall` calls it directly without `await`.
|
|
61
|
+
*
|
|
62
|
+
* Format: `emb:1|{compatibilityIdentity}|{dimensions}|{representation}|
|
|
63
|
+
* {normalization}|{postProcessing.kind}:{postProcessing.revision}|{profileRevision}`,
|
|
64
|
+
* where each component has `|` and `\` escaped before joining, so two different
|
|
65
|
+
* component tuples cannot produce the same string.
|
|
66
|
+
*
|
|
67
|
+
* NOTE: `documentRecipeRevision` and `queryRecipeRevision` are recorded on the
|
|
68
|
+
* profile but take NO part in the derivation. That is exactly what puts a query
|
|
69
|
+
* and a document in the same `Space_Id` when they share a retrieval profile.
|
|
70
|
+
*/
|
|
71
|
+
declare function deriveSpaceId(profile: EmbeddingProfile): EmbeddingSpaceId;
|
|
72
|
+
/**
|
|
73
|
+
* Space compatibility is its own concept: decided by the declared compatibility
|
|
74
|
+
* identity, independent of comparing model names and independent of comparing
|
|
75
|
+
* dimension counts.
|
|
76
|
+
*/
|
|
77
|
+
declare function isSpaceCompatible(a: EmbeddingProfile, b: EmbeddingProfile): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* A usable default for `EmbeddingAdapter.embeddingProfile()`: compatibility
|
|
80
|
+
* identity derived from `${route}:${modelId}` when the catalog declares none,
|
|
81
|
+
* normalization `'unknown'`, no post-processing. This default is what keeps
|
|
82
|
+
* `EmbeddingAdapter` at exactly one abstract method (Requirement 1.2); an adapter
|
|
83
|
+
* that knows what its provider declares about the embedding space MUST override
|
|
84
|
+
* to state the real identity.
|
|
85
|
+
*/
|
|
86
|
+
declare function defaultEmbeddingProfile(model: ResolvedEmbeddingModelInfo, request: EmbeddingProfileInput): EmbeddingProfile;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { EmbeddingNormalization, EmbeddingPostProcessing, EmbeddingProfile, EmbeddingProfileInput, EmbeddingRepresentation, EmbeddingSpaceId, defaultEmbeddingProfile, deriveSpaceId, isSpaceCompatible };
|
|
89
|
+
//# sourceMappingURL=profile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.d.ts","names":[],"sources":["../../src/embedding/profile.ts"],"mappings":";;;KAYY;;;;;;;KAQA;;UAGK;WACN;WACA;;;UAIM;;WAEN;WACA;WACA;WACA,gBAAgB;WAChB,eAAe;WACf,iBAAiB;;WAEjB;;WAEA;;WAEA;WACA;;;KAIC;WAAuC;;;;;;;;UAQlC;;WAEN;;WAEA;;WAEA;;WAEA;;;;;;;;;;;;;;;;;;;iBA6DK,cAAc,SAAS,mBAAmB;;;;;;iBAS1C,kBAAkB,GAAG,kBAAkB,GAAG;;;;;;;;;iBAc1C,wBACd,OAAO,4BACP,SAAS,wBACR"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function e(e){return e.replace(/\\/g,`\\\\`).replace(/\|/g,`\\|`)}function t(t){return t===void 0?`none`:`${e(t.kind)}:${e(t.revision)}`}function n(n){return[e(n.compatibilityIdentity),String(n.dimensions),e(n.representation),e(n.normalization),t(n.postProcessing),e(n.profileRevision)]}function r(e){return[`emb:1`,...n(e)].join(`|`)}function i(e,t){let r=n(e),i=n(t);return r.every((e,t)=>e===i[t])}function a(e,t){let n=`${e.provider}:${e.id}`,r=e.compatibilityIdentity.state===`supported`?e.compatibilityIdentity.value:n,i=e.representation.state===`supported`?e.representation.value:`dense-float32`,a=e.defaultDimensions.state===`supported`?e.defaultDimensions.value:void 0;return{modelIdentity:n,...e.modelRevision===void 0?{}:{modelRevision:e.modelRevision},dimensions:t.dimensions??a??0,representation:i,normalization:`unknown`,documentRecipeRevision:t.documentRecipeRevision??`1`,queryRecipeRevision:t.queryRecipeRevision??`1`,compatibilityIdentity:r,profileRevision:t.profileRevision??`1`}}export{a as defaultEmbeddingProfile,r as deriveSpaceId,i as isSpaceCompatible};
|
|
2
|
+
//# sourceMappingURL=profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.js","names":[],"sources":["../../src/embedding/profile.ts"],"sourcesContent":["/**\n * `Embedding_Profile` and the derived `Space_Id`.\n *\n * Two vectors with the same dimension count are NOT compatible for that reason\n * alone. This module owns the one place where compatibility is decided.\n *\n * @module ai-agent-sdk/core/embedding/profile\n */\n\nimport type { ResolvedEmbeddingModelInfo } from './catalog.ts'\n\n/** Output representation of a vector. v1 declares exactly one. */\nexport type EmbeddingRepresentation = 'dense-float32'\n\n/**\n * Whether vectors arrive normalized.\n *\n * `'unknown'` is a first-class value: a route that has not documented its\n * normalization must say so rather than have `'none'` assumed for it.\n */\nexport type EmbeddingNormalization = 'unit-l2' | 'none' | 'unknown'\n\n/** Post-processing is version-managed; it is never an implicit slice or pad. */\nexport interface EmbeddingPostProcessing {\n readonly kind: 'l2-renormalize'\n readonly revision: string\n}\n\n/** Everything about one call configuration that a vector's meaning depends on. */\nexport interface EmbeddingProfile {\n /** `${route}:${modelId}` — a model identity, NOT a space identity. */\n readonly modelIdentity: string\n readonly modelRevision?: string\n readonly dimensions: number\n readonly representation: EmbeddingRepresentation\n readonly normalization: EmbeddingNormalization\n readonly postProcessing?: EmbeddingPostProcessing\n /** Recorded for traceability; excluded from the `Space_Id` derivation (DD-7). */\n readonly documentRecipeRevision: string\n /** Recorded for traceability; excluded from the `Space_Id` derivation (DD-7). */\n readonly queryRecipeRevision: string\n /** The provider's declaration about the embedding space. Declared by adapter/route. */\n readonly compatibilityIdentity: string\n readonly profileRevision: string\n}\n\n/** Canonical identifier of an embedding space. */\nexport type EmbeddingSpaceId = string & { readonly __brand: 'EmbeddingSpaceId' }\n\n/**\n * Call configuration a caller supplies on top of resolved catalog metadata.\n *\n * Every field is optional: an adapter that knows nothing beyond the model id\n * still gets a usable profile out of {@link defaultEmbeddingProfile}.\n */\nexport interface EmbeddingProfileInput {\n /** Requested dimensions; absent means the model default. */\n readonly dimensions?: number\n /** Revision of the rule turning a document's content parts into wire input. */\n readonly documentRecipeRevision?: string\n /** Revision of the rule turning a query's content parts into wire input. */\n readonly queryRecipeRevision?: string\n /** Bumped when any profile-affecting configuration changes. */\n readonly profileRevision?: string\n}\n\n/** Version prefix of the canonical `Space_Id` string. */\nconst SPACE_ID_VERSION = 'emb:1'\n\n/** Revision used when the caller declares none. */\nconst DEFAULT_REVISION = '1'\n\n/**\n * Dimensions recorded when neither the caller nor the catalog declares a count.\n *\n * `0` is not a legal vector width, so it cannot collide with a real declaration.\n */\nconst UNDECLARED_DIMENSIONS = 0\n\n/**\n * Escapes `|` and `\\` so no two distinct component tuples can join to the same\n * string.\n */\nfunction escapeComponent(value: string): string {\n return value.replace(/\\\\/g, '\\\\\\\\').replace(/\\|/g, '\\\\|')\n}\n\n/** The post-processing component, present or explicitly absent. */\nfunction postProcessingComponent(\n postProcessing: EmbeddingPostProcessing | undefined,\n): string {\n if (postProcessing === undefined) return 'none'\n return `${escapeComponent(postProcessing.kind)}:${escapeComponent(postProcessing.revision)}`\n}\n\n/** The exact component tuple that decides space identity, in fixed order. */\nfunction spaceComponents(profile: EmbeddingProfile): readonly string[] {\n return [\n escapeComponent(profile.compatibilityIdentity),\n String(profile.dimensions),\n escapeComponent(profile.representation),\n escapeComponent(profile.normalization),\n postProcessingComponent(profile.postProcessing),\n escapeComponent(profile.profileRevision),\n ]\n}\n\n/**\n * Derives the `Space_Id` from what decides whether two vectors are comparable.\n *\n * SYNCHRONOUS and UNHASHED: a `Space_Id` is a canonical string, not a digest.\n * It is an identifier for comparison, not a secret to hide, so no hash function\n * is needed and `packages/core` takes on no `crypto.subtle` dependency (DD-11).\n * Being synchronous, `prepareEmbeddingCall` calls it directly without `await`.\n *\n * Format: `emb:1|{compatibilityIdentity}|{dimensions}|{representation}|\n * {normalization}|{postProcessing.kind}:{postProcessing.revision}|{profileRevision}`,\n * where each component has `|` and `\\` escaped before joining, so two different\n * component tuples cannot produce the same string.\n *\n * NOTE: `documentRecipeRevision` and `queryRecipeRevision` are recorded on the\n * profile but take NO part in the derivation. That is exactly what puts a query\n * and a document in the same `Space_Id` when they share a retrieval profile.\n */\nexport function deriveSpaceId(profile: EmbeddingProfile): EmbeddingSpaceId {\n return [SPACE_ID_VERSION, ...spaceComponents(profile)].join('|') as EmbeddingSpaceId\n}\n\n/**\n * Space compatibility is its own concept: decided by the declared compatibility\n * identity, independent of comparing model names and independent of comparing\n * dimension counts.\n */\nexport function isSpaceCompatible(a: EmbeddingProfile, b: EmbeddingProfile): boolean {\n const left = spaceComponents(a)\n const right = spaceComponents(b)\n return left.every((component, index) => component === right[index])\n}\n\n/**\n * A usable default for `EmbeddingAdapter.embeddingProfile()`: compatibility\n * identity derived from `${route}:${modelId}` when the catalog declares none,\n * normalization `'unknown'`, no post-processing. This default is what keeps\n * `EmbeddingAdapter` at exactly one abstract method (Requirement 1.2); an adapter\n * that knows what its provider declares about the embedding space MUST override\n * to state the real identity.\n */\nexport function defaultEmbeddingProfile(\n model: ResolvedEmbeddingModelInfo,\n request: EmbeddingProfileInput,\n): EmbeddingProfile {\n const modelIdentity = `${model.provider}:${model.id}`\n const compatibilityIdentity =\n model.compatibilityIdentity.state === 'supported'\n ? model.compatibilityIdentity.value\n : modelIdentity\n const representation =\n model.representation.state === 'supported'\n ? model.representation.value\n : 'dense-float32'\n const declaredDefault =\n model.defaultDimensions.state === 'supported'\n ? model.defaultDimensions.value\n : undefined\n\n return {\n modelIdentity,\n ...(model.modelRevision === undefined ? {} : { modelRevision: model.modelRevision }),\n dimensions: request.dimensions ?? declaredDefault ?? UNDECLARED_DIMENSIONS,\n representation,\n // Never inferred from the dimension count or the provider's reputation.\n normalization: 'unknown',\n documentRecipeRevision: request.documentRecipeRevision ?? DEFAULT_REVISION,\n queryRecipeRevision: request.queryRecipeRevision ?? DEFAULT_REVISION,\n compatibilityIdentity,\n profileRevision: request.profileRevision ?? DEFAULT_REVISION,\n }\n}\n"],"mappings":"AAmFA,SAAS,EAAgB,EAAuB,CAC9C,OAAO,EAAM,QAAQ,MAAO,MAAM,CAAC,CAAC,QAAQ,MAAO,KAAK,CAC1D,CAGA,SAAS,EACP,EACQ,CAER,OADI,IAAmB,IAAA,GAAkB,OAClC,GAAG,EAAgB,EAAe,IAAI,EAAE,GAAG,EAAgB,EAAe,QAAQ,GAC3F,CAGA,SAAS,EAAgB,EAA8C,CACrE,MAAO,CACL,EAAgB,EAAQ,qBAAqB,EAC7C,OAAO,EAAQ,UAAU,EACzB,EAAgB,EAAQ,cAAc,EACtC,EAAgB,EAAQ,aAAa,EACrC,EAAwB,EAAQ,cAAc,EAC9C,EAAgB,EAAQ,eAAe,CACzC,CACF,CAmBA,SAAgB,EAAc,EAA6C,CACzE,MAAO,CAAC,QAAkB,GAAG,EAAgB,CAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CACjE,CAOA,SAAgB,EAAkB,EAAqB,EAA8B,CACnF,IAAM,EAAO,EAAgB,CAAC,EACxB,EAAQ,EAAgB,CAAC,EAC/B,OAAO,EAAK,OAAO,EAAW,IAAU,IAAc,EAAM,EAAM,CACpE,CAUA,SAAgB,EACd,EACA,EACkB,CAClB,IAAM,EAAgB,GAAG,EAAM,SAAS,GAAG,EAAM,KAC3C,EACJ,EAAM,sBAAsB,QAAU,YAClC,EAAM,sBAAsB,MAC5B,EACA,EACJ,EAAM,eAAe,QAAU,YAC3B,EAAM,eAAe,MACrB,gBACA,EACJ,EAAM,kBAAkB,QAAU,YAC9B,EAAM,kBAAkB,MACxB,IAAA,GAEN,MAAO,CACL,gBACA,GAAI,EAAM,gBAAkB,IAAA,GAAY,CAAC,EAAI,CAAE,cAAe,EAAM,aAAc,EAClF,WAAY,EAAQ,YAAc,GAAmB,EACrD,iBAEA,cAAe,UACf,uBAAwB,EAAQ,wBAA0B,IAC1D,oBAAqB,EAAQ,qBAAuB,IACpD,wBACA,gBAAiB,EAAQ,iBAAmB,GAC9C,CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/embedding/purpose.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Purpose of an embedding input, declared once at the shared API.
|
|
4
|
+
*
|
|
5
|
+
* @module ai-agent-sdk/core/embedding/purpose
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Why an input is being embedded.
|
|
9
|
+
*
|
|
10
|
+
* Exactly two values. Purpose is REQUIRED on every `embed()`/`embedMany()` call,
|
|
11
|
+
* and translating it to a provider mechanism belongs entirely to the adapter.
|
|
12
|
+
*/
|
|
13
|
+
type EmbeddingPurpose = 'retrieval-query' | 'retrieval-document';
|
|
14
|
+
/**
|
|
15
|
+
* How one route expresses {@link EmbeddingPurpose} on the wire.
|
|
16
|
+
*
|
|
17
|
+
* A route whose handling is `unknown` or `{ kind: 'none' }` gets the text sent
|
|
18
|
+
* verbatim: an adapter never adds a prefix the provider has not documented.
|
|
19
|
+
*/
|
|
20
|
+
type EmbeddingPurposeHandling =
|
|
21
|
+
/** Provider has a dedicated wire parameter, for example Gemini `taskType`. */
|
|
22
|
+
{
|
|
23
|
+
readonly kind: 'wire-parameter';
|
|
24
|
+
readonly parameter: string;
|
|
25
|
+
} |
|
|
26
|
+
/** Provider requires an adapter-inserted prefix, per provider documentation. */
|
|
27
|
+
{
|
|
28
|
+
readonly kind: 'adapter-prefix';
|
|
29
|
+
readonly documented: true;
|
|
30
|
+
} |
|
|
31
|
+
/** Provider exposes no mechanism; the adapter does NOT invent a prefix. */
|
|
32
|
+
{
|
|
33
|
+
readonly kind: 'none';
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
export { EmbeddingPurpose, EmbeddingPurposeHandling };
|
|
37
|
+
//# sourceMappingURL=purpose.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"purpose.d.ts","names":[],"sources":["../../src/embedding/purpose.ts"],"mappings":";;;;;;;;;;;;KAYY;;;;;;;KAQA;;;WAEG;WAAiC;;;;WAEjC;WAAiC;;;;WAEjC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { EmbeddingPurpose } from "./purpose.js";
|
|
2
|
+
//#region src/embedding/request.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* One component of a single object to embed.
|
|
5
|
+
*
|
|
6
|
+
* v1 scope is text only; the union exists so multimodal parts can be added later
|
|
7
|
+
* without changing the surrounding contract.
|
|
8
|
+
*/
|
|
9
|
+
type EmbeddingContentPart = {
|
|
10
|
+
readonly type: 'text';
|
|
11
|
+
readonly text: string;
|
|
12
|
+
};
|
|
13
|
+
/** One object embedded independently of the others in the same call. */
|
|
14
|
+
interface EmbeddingItem {
|
|
15
|
+
/**
|
|
16
|
+
* Index within the `Logical_Call`, NOT the index within the `Physical_Batch`.
|
|
17
|
+
*
|
|
18
|
+
* Result order is restored from this value, so it survives batching, retries and
|
|
19
|
+
* out-of-order settlement.
|
|
20
|
+
*/
|
|
21
|
+
readonly index: number;
|
|
22
|
+
/** Components of the SAME object; a provider returns exactly one vector per item. */
|
|
23
|
+
readonly contentParts: readonly EmbeddingContentPart[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Whether the caller accepts provider-side input truncation.
|
|
27
|
+
*
|
|
28
|
+
* `'reject'` means an over-length input is a structured error, not data to cut.
|
|
29
|
+
*/
|
|
30
|
+
type EmbeddingTruncation = 'reject' | 'allow';
|
|
31
|
+
/**
|
|
32
|
+
* SDK default truncation.
|
|
33
|
+
*
|
|
34
|
+
* Stays `'reject'` even where the provider default is on: silently shortening an
|
|
35
|
+
* input changes the vector without telling the caller.
|
|
36
|
+
*/
|
|
37
|
+
declare const DEFAULT_EMBEDDING_TRUNCATION: EmbeddingTruncation;
|
|
38
|
+
/** Exactly one physical embedding request handed to an adapter. */
|
|
39
|
+
interface EmbeddingBatchRequest {
|
|
40
|
+
/** Provider route key that owns this call. */
|
|
41
|
+
readonly provider: string;
|
|
42
|
+
/** Model id as passed by the caller. */
|
|
43
|
+
readonly model: string;
|
|
44
|
+
readonly purpose: EmbeddingPurpose;
|
|
45
|
+
readonly items: readonly EmbeddingItem[];
|
|
46
|
+
/** Requested dimensions; absent means the model default. */
|
|
47
|
+
readonly dimensions?: number;
|
|
48
|
+
/** Resolved from the caller, defaulting to {@link DEFAULT_EMBEDDING_TRUNCATION}. */
|
|
49
|
+
readonly truncation: EmbeddingTruncation;
|
|
50
|
+
readonly signal?: AbortSignal;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { DEFAULT_EMBEDDING_TRUNCATION, EmbeddingBatchRequest, EmbeddingContentPart, EmbeddingItem, EmbeddingTruncation };
|
|
54
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","names":[],"sources":["../../src/embedding/request.ts"],"mappings":";;;;;;;;KAcY;WACG;WAAuB;;;UAGrB;;;;;;;WAON;;WAEA,uBAAuB;;;;;;;KAQtB;;;;;;;cAQC,8BAA8B;;UAG1B;;WAEN;;WAEA;WACA,SAAS;WACT,gBAAgB;;WAEhB;;WAEA,YAAY;WACZ,SAAS"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","names":[],"sources":["../../src/embedding/request.ts"],"sourcesContent":["/**\n * Request shapes for one physical embedding batch.\n *\n * @module ai-agent-sdk/core/embedding/request\n */\n\nimport type { EmbeddingPurpose } from './purpose.ts'\n\n/**\n * One component of a single object to embed.\n *\n * v1 scope is text only; the union exists so multimodal parts can be added later\n * without changing the surrounding contract.\n */\nexport type EmbeddingContentPart =\n | { readonly type: 'text'; readonly text: string }\n\n/** One object embedded independently of the others in the same call. */\nexport interface EmbeddingItem {\n /**\n * Index within the `Logical_Call`, NOT the index within the `Physical_Batch`.\n *\n * Result order is restored from this value, so it survives batching, retries and\n * out-of-order settlement.\n */\n readonly index: number\n /** Components of the SAME object; a provider returns exactly one vector per item. */\n readonly contentParts: readonly EmbeddingContentPart[]\n}\n\n/**\n * Whether the caller accepts provider-side input truncation.\n *\n * `'reject'` means an over-length input is a structured error, not data to cut.\n */\nexport type EmbeddingTruncation = 'reject' | 'allow'\n\n/**\n * SDK default truncation.\n *\n * Stays `'reject'` even where the provider default is on: silently shortening an\n * input changes the vector without telling the caller.\n */\nexport const DEFAULT_EMBEDDING_TRUNCATION: EmbeddingTruncation = 'reject'\n\n/** Exactly one physical embedding request handed to an adapter. */\nexport interface EmbeddingBatchRequest {\n /** Provider route key that owns this call. */\n readonly provider: string\n /** Model id as passed by the caller. */\n readonly model: string\n readonly purpose: EmbeddingPurpose\n readonly items: readonly EmbeddingItem[]\n /** Requested dimensions; absent means the model default. */\n readonly dimensions?: number\n /** Resolved from the caller, defaulting to {@link DEFAULT_EMBEDDING_TRUNCATION}. */\n readonly truncation: EmbeddingTruncation\n readonly signal?: AbortSignal\n}\n"],"mappings":"AA2CA,MAAa,EAAoD"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { UsageCounters } from "../observation/usage.js";
|
|
2
|
+
import { EmbeddingProfile, EmbeddingSpaceId } from "./profile.js";
|
|
3
|
+
import { EmbeddingUsageReport } from "./usage.js";
|
|
4
|
+
//#region src/embedding/result.d.ts
|
|
5
|
+
/** One vector, mapped back to the input it belongs to. */
|
|
6
|
+
interface EmbeddingVector {
|
|
7
|
+
/** MUST match the `index` of an item in the request. */
|
|
8
|
+
readonly index: number;
|
|
9
|
+
/**
|
|
10
|
+
* The values exactly as the provider returned them, apart from a post-processing
|
|
11
|
+
* step recorded in {@link EmbeddingProfile}. Never sliced or padded.
|
|
12
|
+
*/
|
|
13
|
+
readonly values: readonly number[];
|
|
14
|
+
/** Provider reported this input was cut; only valid when `truncation === 'allow'`. */
|
|
15
|
+
readonly truncated?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** What one `Provider_Attempt` produced for one `Physical_Batch`. */
|
|
18
|
+
interface EmbeddingBatchResult {
|
|
19
|
+
readonly vectors: readonly EmbeddingVector[];
|
|
20
|
+
/** Raw provider evidence. The runtime does NOT infer 0 when this is absent. */
|
|
21
|
+
readonly usage?: UsageCounters;
|
|
22
|
+
readonly providerRequestId?: string;
|
|
23
|
+
readonly warnings?: readonly EmbeddingWarning[];
|
|
24
|
+
}
|
|
25
|
+
/** A fact worth surfacing that is not a failure. */
|
|
26
|
+
interface EmbeddingWarning {
|
|
27
|
+
readonly code: 'input-truncated' | 'usage-unreported' | 'usage-malformed';
|
|
28
|
+
/** Affected input indexes, in `Logical_Call` numbering. */
|
|
29
|
+
readonly itemIndexes?: readonly number[];
|
|
30
|
+
readonly message: string;
|
|
31
|
+
}
|
|
32
|
+
/** What `embed()` returns for one input. */
|
|
33
|
+
interface EmbeddingResult {
|
|
34
|
+
readonly embedding: readonly number[];
|
|
35
|
+
readonly space: EmbeddingSpaceId;
|
|
36
|
+
readonly profile: EmbeddingProfile;
|
|
37
|
+
readonly usage: EmbeddingUsageReport;
|
|
38
|
+
readonly warnings: readonly EmbeddingWarning[];
|
|
39
|
+
}
|
|
40
|
+
/** What `embedMany()` returns; `embeddings` follows input order. */
|
|
41
|
+
interface EmbeddingManyResult {
|
|
42
|
+
readonly embeddings: readonly (readonly number[])[];
|
|
43
|
+
readonly space: EmbeddingSpaceId;
|
|
44
|
+
readonly profile: EmbeddingProfile;
|
|
45
|
+
readonly usage: EmbeddingUsageReport;
|
|
46
|
+
readonly warnings: readonly EmbeddingWarning[];
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
export { EmbeddingBatchResult, EmbeddingManyResult, EmbeddingResult, EmbeddingVector, EmbeddingWarning };
|
|
50
|
+
//# sourceMappingURL=result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","names":[],"sources":["../../src/embedding/result.ts"],"mappings":";;;;;UAciB;;WAEN;;;;;WAKA;;WAEA;;;UAIM;WACN,kBAAkB;;WAElB,QAAQ;WACR;WACA,oBAAoB;;;UAId;WACN;;WAEA;WACA;;;UAIM;WACN;WACA,OAAO;WACP,SAAS;WACT,OAAO;WACP,mBAAmB;;;UAIb;WACN;WACA,OAAO;WACP,SAAS;WACT,OAAO;WACP,mBAAmB"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/embedding/usage.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Usage accounting for embedding.
|
|
4
|
+
*
|
|
5
|
+
* Embedding has no output tokens, so it does NOT reuse `UsageCounters` /
|
|
6
|
+
* `validateUsageCounters`: that shape only reports `complete` once
|
|
7
|
+
* `outputTokens` is present, which for embedding could only be satisfied by
|
|
8
|
+
* inventing a `0`. Usage honesty forbids that. Missing or malformed usage stays
|
|
9
|
+
* missing here — it remains evidence of a `Provider_Attempt`, but never leaves
|
|
10
|
+
* the runtime as a published number.
|
|
11
|
+
*
|
|
12
|
+
* @module ai-agent-sdk/core/embedding/usage
|
|
13
|
+
*/
|
|
14
|
+
/** Tokens a provider actually reported for embedding work. Never contains `outputTokens`. */
|
|
15
|
+
interface EmbeddingTokenUsage {
|
|
16
|
+
readonly inputTokens: number;
|
|
17
|
+
readonly totalTokens?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Coverage of a `Logical_Call`'s usage.
|
|
21
|
+
*
|
|
22
|
+
* `complete` only when every batch sent to the provider returned readable usage.
|
|
23
|
+
*/
|
|
24
|
+
type EmbeddingUsageStatus = 'complete' | 'partial' | 'missing';
|
|
25
|
+
/** What the runtime publishes about the usage of one `Logical_Call`. */
|
|
26
|
+
interface EmbeddingUsageReport {
|
|
27
|
+
readonly status: EmbeddingUsageStatus;
|
|
28
|
+
/** Present only when `status === 'complete'`. Never a `TokenUsage`. */
|
|
29
|
+
readonly tokens?: EmbeddingTokenUsage;
|
|
30
|
+
readonly batches: number;
|
|
31
|
+
readonly batchesWithUsage: number;
|
|
32
|
+
readonly providerAttempts: number;
|
|
33
|
+
readonly inputsFromCache: number;
|
|
34
|
+
readonly inputsFromProvider: number;
|
|
35
|
+
}
|
|
36
|
+
declare const COUNTER_KEYS: readonly ['inputTokens', 'totalTokens'];
|
|
37
|
+
type EmbeddingCounterKey = typeof COUNTER_KEYS[number];
|
|
38
|
+
/** Counterpart of `UsageValidationResult` for embedding. */
|
|
39
|
+
interface EmbeddingUsageValidation {
|
|
40
|
+
/**
|
|
41
|
+
* Absent when the provider reported no readable `inputTokens`. No branch
|
|
42
|
+
* substitutes a `0`: an unreported counter is unknown, not zero.
|
|
43
|
+
*/
|
|
44
|
+
readonly reported?: EmbeddingTokenUsage;
|
|
45
|
+
readonly invalidFields: readonly EmbeddingCounterKey[];
|
|
46
|
+
readonly complete: boolean;
|
|
47
|
+
readonly overflow: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Rejects invalid fields individually and keeps only counters the provider
|
|
51
|
+
* genuinely reported. A hostile or malformed payload yields
|
|
52
|
+
* `reported === undefined` rather than a fabricated total.
|
|
53
|
+
*/
|
|
54
|
+
declare function validateEmbeddingUsage(value: unknown): EmbeddingUsageValidation;
|
|
55
|
+
/** True when the provider reported at least one readable embedding counter. */
|
|
56
|
+
declare function hasEmbeddingUsage(value: EmbeddingTokenUsage | undefined): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Applies the status rule to batches that were actually sent to the provider.
|
|
59
|
+
* Batches served from cache are not evidence of unreported usage.
|
|
60
|
+
*/
|
|
61
|
+
declare function classifyEmbeddingUsageStatus(batchesSent: number, batchesWithUsage: number): EmbeddingUsageStatus;
|
|
62
|
+
//#endregion
|
|
63
|
+
export { EmbeddingCounterKey, EmbeddingTokenUsage, EmbeddingUsageReport, EmbeddingUsageStatus, EmbeddingUsageValidation, classifyEmbeddingUsageStatus, hasEmbeddingUsage, validateEmbeddingUsage };
|
|
64
|
+
//# sourceMappingURL=usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.d.ts","names":[],"sources":["../../src/embedding/usage.ts"],"mappings":";;;;;;;;;;;;;;UAciB;WACN;WACA;;;;;;;KAQC;;UAGK;WACN,QAAQ;;WAER,SAAS;WACT;WACA;WACA;WACA;WACA;;cAGL;KACM,6BAA6B;;UAGxB;;;;;WAKN,WAAW;WACX,wBAAwB;WACxB;WACA;;;;;;;iBAoBK,uBAAuB,iBAAiB;;iBA0CxC,kBAAkB,OAAO;;;;;iBAQzB,6BAA6B,qBAAqB,2BAA2B"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=[`inputTokens`,`totalTokens`];function t(e){return typeof e==`number`&&Number.isSafeInteger(e)&&e>=0}function n(e,t){try{return{value:Reflect.get(e,t),unreadable:!1}}catch{return{unreadable:!0}}}function r(r){let i=typeof r==`object`&&r?r:{},a={},o=[],s=!1;for(let r of e){let e=n(i,r);if(e.unreadable){o.push(r);continue}let c=e.value;c!==void 0&&(t(c)?a[r]=c:(typeof c==`number`&&c>2**53-1&&(s=!0),o.push(r)))}a.inputTokens!==void 0&&a.totalTokens!==void 0&&a.totalTokens<a.inputTokens&&(delete a.totalTokens,o.push(`totalTokens`));let c=a.inputTokens,l=c===void 0?void 0:Object.freeze(a.totalTokens===void 0?{inputTokens:c}:{inputTokens:c,totalTokens:a.totalTokens});return Object.freeze({...l===void 0?{}:{reported:l},invalidFields:Object.freeze([...new Set(o)]),complete:l!==void 0&&o.length===0&&!s,overflow:s})}function i(t){return t!==void 0&&e.some(e=>t[e]!==void 0)}function a(e,t){return t<=0?`missing`:t>=e?`complete`:`partial`}export{a as classifyEmbeddingUsageStatus,i as hasEmbeddingUsage,r as validateEmbeddingUsage};
|
|
2
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","names":[],"sources":["../../src/embedding/usage.ts"],"sourcesContent":["/**\n * Usage accounting for embedding.\n *\n * Embedding has no output tokens, so it does NOT reuse `UsageCounters` /\n * `validateUsageCounters`: that shape only reports `complete` once\n * `outputTokens` is present, which for embedding could only be satisfied by\n * inventing a `0`. Usage honesty forbids that. Missing or malformed usage stays\n * missing here — it remains evidence of a `Provider_Attempt`, but never leaves\n * the runtime as a published number.\n *\n * @module ai-agent-sdk/core/embedding/usage\n */\n\n/** Tokens a provider actually reported for embedding work. Never contains `outputTokens`. */\nexport interface EmbeddingTokenUsage {\n readonly inputTokens: number\n readonly totalTokens?: number\n}\n\n/**\n * Coverage of a `Logical_Call`'s usage.\n *\n * `complete` only when every batch sent to the provider returned readable usage.\n */\nexport type EmbeddingUsageStatus = 'complete' | 'partial' | 'missing'\n\n/** What the runtime publishes about the usage of one `Logical_Call`. */\nexport interface EmbeddingUsageReport {\n readonly status: EmbeddingUsageStatus\n /** Present only when `status === 'complete'`. Never a `TokenUsage`. */\n readonly tokens?: EmbeddingTokenUsage\n readonly batches: number\n readonly batchesWithUsage: number\n readonly providerAttempts: number\n readonly inputsFromCache: number\n readonly inputsFromProvider: number\n}\n\nconst COUNTER_KEYS = ['inputTokens', 'totalTokens'] as const\nexport type EmbeddingCounterKey = typeof COUNTER_KEYS[number]\n\n/** Counterpart of `UsageValidationResult` for embedding. */\nexport interface EmbeddingUsageValidation {\n /**\n * Absent when the provider reported no readable `inputTokens`. No branch\n * substitutes a `0`: an unreported counter is unknown, not zero.\n */\n readonly reported?: EmbeddingTokenUsage\n readonly invalidFields: readonly EmbeddingCounterKey[]\n readonly complete: boolean\n readonly overflow: boolean\n}\n\nfunction validCounter(value: unknown): value is number {\n return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0\n}\n\nfunction readCounter(source: Readonly<Record<string, unknown>>, key: EmbeddingCounterKey): { value?: unknown; unreadable: boolean } {\n try {\n return { value: Reflect.get(source, key), unreadable: false }\n } catch {\n return { unreadable: true }\n }\n}\n\n/**\n * Rejects invalid fields individually and keeps only counters the provider\n * genuinely reported. A hostile or malformed payload yields\n * `reported === undefined` rather than a fabricated total.\n */\nexport function validateEmbeddingUsage(value: unknown): EmbeddingUsageValidation {\n const source = typeof value === 'object' && value !== null ? value as Readonly<Record<string, unknown>> : {}\n const counters: Partial<Record<EmbeddingCounterKey, number>> = {}\n const invalid: EmbeddingCounterKey[] = []\n let overflow = false\n for (const key of COUNTER_KEYS) {\n const read = readCounter(source, key)\n if (read.unreadable) {\n invalid.push(key)\n continue\n }\n const field = read.value\n if (field === undefined) continue\n if (validCounter(field)) counters[key] = field\n else {\n // A count past safe-integer precision is authority lost, not merely a bad field.\n if (typeof field === 'number' && field > Number.MAX_SAFE_INTEGER) overflow = true\n invalid.push(key)\n }\n }\n // A total below the only disjoint bucket cannot describe the same call.\n if (counters.inputTokens !== undefined && counters.totalTokens !== undefined\n && counters.totalTokens < counters.inputTokens) {\n delete counters.totalTokens\n invalid.push('totalTokens')\n }\n const inputTokens = counters.inputTokens\n // Without a reported input bucket there is nothing honest to publish.\n const reported = inputTokens === undefined\n ? undefined\n : Object.freeze<EmbeddingTokenUsage>(counters.totalTokens === undefined\n ? { inputTokens }\n : { inputTokens, totalTokens: counters.totalTokens })\n return Object.freeze({\n ...(reported === undefined ? {} : { reported }),\n invalidFields: Object.freeze([...new Set(invalid)]),\n complete: reported !== undefined && invalid.length === 0 && !overflow,\n overflow,\n })\n}\n\n/** True when the provider reported at least one readable embedding counter. */\nexport function hasEmbeddingUsage(value: EmbeddingTokenUsage | undefined): boolean {\n return value !== undefined && COUNTER_KEYS.some(key => value[key] !== undefined)\n}\n\n/**\n * Applies the status rule to batches that were actually sent to the provider.\n * Batches served from cache are not evidence of unreported usage.\n */\nexport function classifyEmbeddingUsageStatus(batchesSent: number, batchesWithUsage: number): EmbeddingUsageStatus {\n if (batchesWithUsage <= 0) return 'missing'\n return batchesWithUsage >= batchesSent ? 'complete' : 'partial'\n}\n"],"mappings":"AAsCA,MAAM,EAAe,CAAC,cAAe,aAAa,EAelD,SAAS,EAAa,EAAiC,CACrD,OAAO,OAAO,GAAU,UAAY,OAAO,cAAc,CAAK,GAAK,GAAS,CAC9E,CAEA,SAAS,EAAY,EAA2C,EAAoE,CAClI,GAAI,CACF,MAAO,CAAE,MAAO,QAAQ,IAAI,EAAQ,CAAG,EAAG,WAAY,EAAM,CAC9D,MAAQ,CACN,MAAO,CAAE,WAAY,EAAK,CAC5B,CACF,CAOA,SAAgB,EAAuB,EAA0C,CAC/E,IAAM,EAAS,OAAO,GAAU,UAAY,EAAiB,EAA6C,CAAC,EACrG,EAAyD,CAAC,EAC1D,EAAiC,CAAC,EACpC,EAAW,GACf,IAAK,IAAM,KAAO,EAAc,CAC9B,IAAM,EAAO,EAAY,EAAQ,CAAG,EACpC,GAAI,EAAK,WAAY,CACnB,EAAQ,KAAK,CAAG,EAChB,QACF,CACA,IAAM,EAAQ,EAAK,MACf,IAAU,IAAA,KACV,EAAa,CAAK,EAAG,EAAS,GAAO,GAGnC,OAAO,GAAU,UAAY,YAAiC,EAAW,IAC7E,EAAQ,KAAK,CAAG,GAEpB,CAEI,EAAS,cAAgB,IAAA,IAAa,EAAS,cAAgB,IAAA,IAC9D,EAAS,YAAc,EAAS,cACnC,OAAO,EAAS,YAChB,EAAQ,KAAK,aAAa,GAE5B,IAAM,EAAc,EAAS,YAEvB,EAAW,IAAgB,IAAA,GAC7B,IAAA,GACA,OAAO,OAA4B,EAAS,cAAgB,IAAA,GAC1D,CAAE,aAAY,EACd,CAAE,cAAa,YAAa,EAAS,WAAY,CAAC,EACxD,OAAO,OAAO,OAAO,CACnB,GAAI,IAAa,IAAA,GAAY,CAAC,EAAI,CAAE,UAAS,EAC7C,cAAe,OAAO,OAAO,CAAC,GAAG,IAAI,IAAI,CAAO,CAAC,CAAC,EAClD,SAAU,IAAa,IAAA,IAAa,EAAQ,SAAW,GAAK,CAAC,EAC7D,UACF,CAAC,CACH,CAGA,SAAgB,EAAkB,EAAiD,CACjF,OAAO,IAAU,IAAA,IAAa,EAAa,KAAK,GAAO,EAAM,KAAS,IAAA,EAAS,CACjF,CAMA,SAAgB,EAA6B,EAAqB,EAAgD,CAEhH,OADI,GAAoB,EAAU,UAC3B,GAAoB,EAAc,WAAa,SACxD"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { EmbeddingProfile, EmbeddingSpaceId } from "./profile.js";
|
|
2
|
+
import { EmbeddingPurpose } from "./purpose.js";
|
|
3
|
+
import { EmbeddingCapability } from "./catalog.js";
|
|
4
|
+
import { EmbeddingBatchRequest, EmbeddingItem, EmbeddingTruncation } from "./request.js";
|
|
5
|
+
import { EmbeddingBatchResult } from "./result.js";
|
|
6
|
+
import { PreparedEmbeddingCall } from "./adapter.js";
|
|
7
|
+
//#region src/embedding/validation.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* One `Logical_Call` as it stands just before the first dispatch.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately NOT an {@link EmbeddingBatchRequest}: validation runs once per
|
|
12
|
+
* logical call, over ALL items, before any batch exists.
|
|
13
|
+
*/
|
|
14
|
+
interface PreDispatchRequest {
|
|
15
|
+
readonly purpose: EmbeddingPurpose;
|
|
16
|
+
/** Every item of the logical call, in input order. */
|
|
17
|
+
readonly items: readonly EmbeddingItem[];
|
|
18
|
+
/** Caller-requested width; absent means the model default. */
|
|
19
|
+
readonly dimensions?: number;
|
|
20
|
+
/** Resolved truncation preference. */
|
|
21
|
+
readonly truncation: EmbeddingTruncation;
|
|
22
|
+
/** Space the caller demands; incompatibility is a rejection. */
|
|
23
|
+
readonly expectedSpace?: EmbeddingSpaceId;
|
|
24
|
+
/**
|
|
25
|
+
* The profile behind {@link expectedSpace}, when the caller has it.
|
|
26
|
+
*
|
|
27
|
+
* Present ⇒ compatibility is decided by {@link isSpaceCompatible}; absent ⇒ by
|
|
28
|
+
* canonical `Space_Id` equality. Both answer the same question, since the
|
|
29
|
+
* `Space_Id` is derived from exactly the components that decide compatibility.
|
|
30
|
+
*/
|
|
31
|
+
readonly expectedProfile?: EmbeddingProfile;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the route has a truncation parameter at all.
|
|
34
|
+
*
|
|
35
|
+
* `'unsupported'` is the route positively stating the provider exposes no such
|
|
36
|
+
* parameter, which is the only state that rejects `truncation: 'allow'`.
|
|
37
|
+
* `'unknown'` never rejects.
|
|
38
|
+
*/
|
|
39
|
+
readonly truncationSupport?: EmbeddingCapability<boolean>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validates one `Logical_Call` against the SAME `PreparedEmbeddingCall` that will
|
|
43
|
+
* dispatch it, BEFORE any `Physical_Batch` goes out.
|
|
44
|
+
*
|
|
45
|
+
* Every rejection here happens with 0 `Provider_Attempt`. Checks run cheapest
|
|
46
|
+
* first, so a malformed request never pays for token estimation.
|
|
47
|
+
*
|
|
48
|
+
* @param request - the logical call as resolved from the caller.
|
|
49
|
+
* @param prepared - the generation whose metadata, profile and limits will be used.
|
|
50
|
+
* @throws EmbeddingError with a code from `EMBEDDING_ERROR_CODES` on any violation.
|
|
51
|
+
*/
|
|
52
|
+
declare function validatePreDispatch(request: PreDispatchRequest, prepared: PreparedEmbeddingCall): void;
|
|
53
|
+
/**
|
|
54
|
+
* Validates one `Provider_Attempt`'s response against the batch that produced it.
|
|
55
|
+
*
|
|
56
|
+
* Order is fixed — count, indexes, values, width, truncation reports — so the
|
|
57
|
+
* SAME malformed response yields the SAME code across providers, which is what
|
|
58
|
+
* lets one contract-test suite run against all of them. Nothing here infers or
|
|
59
|
+
* repairs: a response that breaks the contract is a protocol error.
|
|
60
|
+
*
|
|
61
|
+
* @param batch - the physical batch that was dispatched.
|
|
62
|
+
* @param result - what the adapter parsed out of exactly one provider attempt.
|
|
63
|
+
* @throws EmbeddingError with a vector- or response-level code on any violation.
|
|
64
|
+
*/
|
|
65
|
+
declare function validateBatchResult(batch: EmbeddingBatchRequest, result: EmbeddingBatchResult): void;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { PreDispatchRequest, validateBatchResult, validatePreDispatch };
|
|
68
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","names":[],"sources":["../../src/embedding/validation.ts"],"mappings":";;;;;;;;;;;;;UA4CiB;WACN,SAAS;;WAET,gBAAgB;;WAEhB;;WAEA,YAAY;;WAEZ,gBAAgB;;;;;;;;WAQhB,kBAAkB;;;;;;;;WAQlB,oBAAoB;;;;;;;;;;;;;iBAyJf,oBACd,SAAS,oBACT,UAAU;;;;;;;;;;;;;iBAwII,oBACd,OAAO,uBACP,QAAQ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{EMBEDDING_ERROR_CODES as e,EmbeddingError as t}from"./errors.js";import{isSpaceCompatible as n}from"./profile.js";const r=Object.freeze([`retrieval-query`,`retrieval-document`]);function i(e){let t=``;for(let n of e.contentParts)n.type===`text`&&(t+=n.text);return t}function a(n,i){if(!r.includes(n.purpose))throw new t(`Embedding purpose must be "retrieval-query" or "retrieval-document"`,e.REQUEST_INVALID,d(i))}function o(n,r){if(n.items.length===0)throw new t(`Embedding request carries no input values`,e.REQUEST_INVALID,d(r));let a=[];for(let e of n.items)(e.contentParts.length===0||i(e).length===0)&&a.push(e.index);if(a.length>0)throw new t(`Embedding input is empty at ${a.length} item(s)`,e.REQUEST_INVALID,{...d(r),itemIndexes:a})}function s(n,r){let i=n.dimensions;if(i===void 0)return;if(!Number.isInteger(i)||i<=0)throw new t(`Embedding dimensions must be a positive integer`,e.REQUEST_INVALID,d(r));let a=r.model.dimensions;if(a.state===`supported`&&!a.value.includes(i))throw new t(`Embedding model does not support ${i} dimensions`,e.DIMENSIONS_UNSUPPORTED,d(r))}function c(n,r){let a=r.model.maxInputTokens;if(a.state!==`supported`)return;let o=a.value;if(!Number.isFinite(o)||o<=0)return;let s=[];for(let e of n.items)r.limits.estimateTokens(i(e))>o&&s.push(e.index);if(s.length!==0)throw new t(`Embedding input exceeds the declared limit of ${o} tokens`,e.INPUT_TOO_LARGE,{...d(r),itemIndexes:s,limit:o})}function l(r,i){let a=r.expectedSpace,o=r.expectedProfile;if((a!==void 0||o!==void 0)&&!(o===void 0?a===i.spaceId:n(o,i.profile)))throw new t(`Expected embedding space is incompatible with the prepared call`,e.SPACE_INCOMPATIBLE,{...d(i),space:i.spaceId})}function u(n,r){if(n.truncation===`allow`&&n.truncationSupport?.state===`unsupported`)throw new t(`Provider route exposes no truncation parameter`,e.TRUNCATION_UNSUPPORTED,d(r))}function d(e){return{provider:e.model.provider,model:e.model.id}}function f(e,t){a(e,t),o(e,t),s(e,t),u(e,t),l(e,t),c(e,t)}function p(n,r){if(r.vectors.length!==n.items.length)throw new t(`Provider returned ${r.vectors.length} vectors for ${n.items.length} inputs`,e.VECTOR_COUNT_MISMATCH,{provider:n.provider,model:n.model})}function m(n,r){let i=new Set(n.items.map(e=>e.index)),a=new Set;for(let o of r.vectors){if(!Number.isInteger(o.index)||!i.has(o.index)||a.has(o.index))throw new t(`Provider returned a duplicate, missing or out-of-range vector index`,e.VECTOR_INDEX_INVALID,{provider:n.provider,model:n.model});a.add(o.index)}}function h(n,r){for(let i of r.vectors){if(!Array.isArray(i.values))throw new t(`Provider returned a vector whose values are not an array`,e.RESPONSE_MALFORMED,{provider:n.provider,model:n.model,itemIndexes:[i.index]});for(let r of i.values)if(!(typeof r==`number`&&Number.isFinite(r)))throw new t(`Provider returned a vector containing a non-finite value`,e.VECTOR_VALUE_INVALID,{provider:n.provider,model:n.model,itemIndexes:[i.index]})}}function g(n,r){let i=n.dimensions;if(i!==void 0){for(let a of r.vectors)if(a.values.length!==i)throw new t(`Provider returned a ${a.values.length}-dimensional vector, expected ${i}`,e.VECTOR_DIMENSIONS_MISMATCH,{provider:n.provider,model:n.model,itemIndexes:[a.index],limit:i})}}function _(n,r){if(n.truncation!==`reject`)return;let i=r.vectors.filter(e=>e.truncated===!0);if(i.length!==0)throw new t(`Provider reported truncation although truncation was rejected`,e.RESPONSE_MALFORMED,{provider:n.provider,model:n.model,itemIndexes:i.map(e=>e.index)})}function v(e,t){p(e,t),m(e,t),h(e,t),g(e,t),_(e,t)}export{v as validateBatchResult,f as validatePreDispatch};
|
|
2
|
+
//# sourceMappingURL=validation.js.map
|