@hourslabs/domovoi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +312 -0
- package/dist/cache.d.ts +102 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/calibration/index.d.ts +45 -0
- package/dist/calibration/index.d.ts.map +1 -0
- package/dist/calibration/index.js +95 -0
- package/dist/calibration/index.js.map +1 -0
- package/dist/engine/abort.d.ts +43 -0
- package/dist/engine/abort.d.ts.map +1 -0
- package/dist/engine/config.d.ts +79 -0
- package/dist/engine/config.d.ts.map +1 -0
- package/dist/engine/decide.d.ts +18 -0
- package/dist/engine/decide.d.ts.map +1 -0
- package/dist/engine/distribution.d.ts +18 -0
- package/dist/engine/distribution.d.ts.map +1 -0
- package/dist/engine/error-recording.d.ts +35 -0
- package/dist/engine/error-recording.d.ts.map +1 -0
- package/dist/engine/finalize.d.ts +12 -0
- package/dist/engine/finalize.d.ts.map +1 -0
- package/dist/engine/hooks.d.ts +12 -0
- package/dist/engine/hooks.d.ts.map +1 -0
- package/dist/engine/index.d.ts +7 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/meta.d.ts +37 -0
- package/dist/engine/meta.d.ts.map +1 -0
- package/dist/engine/threshold.d.ts +31 -0
- package/dist/engine/threshold.d.ts.map +1 -0
- package/dist/env.d.ts +46 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/errors.d.ts +66 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/hash.d.ts +27 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1263 -0
- package/dist/index.js.map +1 -0
- package/dist/prompt.d.ts +14 -0
- package/dist/prompt.d.ts.map +1 -0
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +301 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai/adapter.d.ts +25 -0
- package/dist/providers/openai/adapter.d.ts.map +1 -0
- package/dist/providers/openai/distribution.d.ts +26 -0
- package/dist/providers/openai/distribution.d.ts.map +1 -0
- package/dist/providers/openai/factory.d.ts +76 -0
- package/dist/providers/openai/factory.d.ts.map +1 -0
- package/dist/providers/openai/index.d.ts +6 -0
- package/dist/providers/openai/index.d.ts.map +1 -0
- package/dist/providers/provider.d.ts +50 -0
- package/dist/providers/provider.d.ts.map +1 -0
- package/dist/testing/index.d.ts +40 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +34 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tokenizer.d.ts +56 -0
- package/dist/tokenizer.d.ts.map +1 -0
- package/dist/types.d.ts +180 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/validate.d.ts +47 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/verbs/boolean.d.ts +26 -0
- package/dist/verbs/boolean.d.ts.map +1 -0
- package/dist/verbs/classifier.d.ts +63 -0
- package/dist/verbs/classifier.d.ts.map +1 -0
- package/dist/verbs/classify.d.ts +24 -0
- package/dist/verbs/classify.d.ts.map +1 -0
- package/dist/verdict.d.ts +38 -0
- package/dist/verdict.d.ts.map +1 -0
- package/package.json +108 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var DomovoiError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(message, options) {
|
|
5
|
+
super(message, options?.cause !== void 0 ? { cause: options.cause } : void 0);
|
|
6
|
+
this.name = "DomovoiError";
|
|
7
|
+
this.code = options?.code ?? "unspecified";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var ConfigError = class extends DomovoiError {
|
|
11
|
+
constructor(message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = "ConfigError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/calibration/index.ts
|
|
18
|
+
var identity = {
|
|
19
|
+
kind: "identity",
|
|
20
|
+
apply(d) {
|
|
21
|
+
return d;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function temperatureScaling(T) {
|
|
25
|
+
if (Number.isNaN(T) || T <= 0) {
|
|
26
|
+
throw new ConfigError(`temperatureScaling(T): T must be > 0; got ${T}.`, {
|
|
27
|
+
code: "incompatible_calibrator"
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const inverseT = 1 / T;
|
|
31
|
+
return {
|
|
32
|
+
kind: "temperature",
|
|
33
|
+
apply(d) {
|
|
34
|
+
const scaledEntries = Object.entries(d.probs).map(
|
|
35
|
+
([label, prob]) => [label, prob === 0 ? 0 : prob ** inverseT]
|
|
36
|
+
);
|
|
37
|
+
const partitionFn = scaledEntries.reduce(
|
|
38
|
+
(running, [, scaledProb]) => running + scaledProb,
|
|
39
|
+
0
|
|
40
|
+
);
|
|
41
|
+
if (partitionFn === 0) return d;
|
|
42
|
+
const normalizedProbs = Object.fromEntries(
|
|
43
|
+
scaledEntries.map(([label, scaledProb]) => [label, scaledProb / partitionFn])
|
|
44
|
+
);
|
|
45
|
+
return {
|
|
46
|
+
probs: normalizedProbs,
|
|
47
|
+
coverage: d.coverage
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function plattScaling(params) {
|
|
53
|
+
if (Number.isNaN(params.a) || Number.isNaN(params.b) || !Number.isFinite(params.a) || !Number.isFinite(params.b)) {
|
|
54
|
+
throw new ConfigError(
|
|
55
|
+
`plattScaling({ a, b }): a and b must be finite numbers; got a=${params.a}, b=${params.b}.`,
|
|
56
|
+
{ code: "incompatible_calibrator" }
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
kind: "platt",
|
|
61
|
+
apply(d) {
|
|
62
|
+
const labels = Object.keys(d.probs);
|
|
63
|
+
if (labels.length !== 2) {
|
|
64
|
+
throw new ConfigError(
|
|
65
|
+
`plattScaling is binary-only; got distribution with ${labels.length} labels.`,
|
|
66
|
+
{ code: "incompatible_calibrator" }
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const [neg, pos] = labels;
|
|
70
|
+
const probs = d.probs;
|
|
71
|
+
const pPos = probs[pos] ?? 0;
|
|
72
|
+
const eps = 1e-9;
|
|
73
|
+
const clamped = Math.min(1 - eps, Math.max(eps, pPos));
|
|
74
|
+
const logit = Math.log(clamped / (1 - clamped));
|
|
75
|
+
const calibratedPos = sigmoid(params.a * logit + params.b);
|
|
76
|
+
return {
|
|
77
|
+
probs: {
|
|
78
|
+
[neg]: 1 - calibratedPos,
|
|
79
|
+
[pos]: calibratedPos
|
|
80
|
+
},
|
|
81
|
+
coverage: d.coverage
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function sigmoid(x) {
|
|
87
|
+
return 1 / (1 + Math.exp(-x));
|
|
88
|
+
}
|
|
89
|
+
function isIdentityCalibrator(c) {
|
|
90
|
+
return c.kind === "identity";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { identity, isIdentityCalibrator, plattScaling, temperatureScaling };
|
|
94
|
+
//# sourceMappingURL=index.js.map
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts","../../src/calibration/index.ts"],"names":[],"mappings":";AA4CO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EAC7B,IAAA;AAAA,EAET,WAAA,CAAY,SAAiB,OAAA,EAA8C;AACzE,IAAA,KAAA,CAAM,OAAA,EAAS,SAAS,KAAA,KAAU,MAAA,GAAY,EAAE,KAAA,EAAO,OAAA,CAAQ,KAAA,EAAM,GAAI,MAAS,CAAA;AAClF,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,SAAS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AAU,KAAK,GAAA,CAAI,CAAA,GAAI,KAAK,IAAA,CAAK,GAAA,CAAI,GAAA,EAAK,IAAI,CAAC,CAAA;AACrD,MAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,GAAA,CAAI,OAAA,IAAW,IAAI,OAAA,CAAQ,CAAA;AAC9C,MAAA,MAAM,gBAAgB,OAAA,CAAQ,MAAA,CAAO,CAAA,GAAI,KAAA,GAAQ,OAAO,CAAC,CAAA;AACzD,MAAA,OAAO;AAAA,QACL,KAAA,EAAO;AAAA,UACL,CAAC,GAAG,GAAG,CAAA,GAAI,aAAA;AAAA,UACX,CAAC,GAAG,GAAG;AAAA,SACT;AAAA,QACA,UAAU,CAAA,CAAE;AAAA,OACd;AAAA,IACF;AAAA,GACF;AACF;AAEA,SAAS,QAAQ,CAAA,EAAmB;AAClC,EAAA,OAAO,CAAA,IAAK,CAAA,GAAI,IAAA,CAAK,GAAA,CAAI,CAAC,CAAC,CAAA,CAAA;AAC7B;AAEO,SAAS,qBAAqB,CAAA,EAAwB;AAC3D,EAAA,OAAO,EAAE,IAAA,KAAS,UAAA;AACpB","file":"index.js","sourcesContent":["/**\n * Error taxonomy for domovoi.\n *\n * Four classes (`DomovoiError` base + `ProviderError`, `ConfigError`,\n * `BudgetExhaustedError`) with a stable `code` field for fine-grained\n * discrimination. All accept `{ cause }` for ES2022 chaining.\n *\n * The engine canonicalizes anything thrown from `Provider.sample` that isn't\n * already a `DomovoiError` into `ProviderError({ cause })`, so callers always\n * see a known error type. Under the default `onErrorPolicy: \"fallback\"`,\n * runtime errors become `Unknown` Verdict variants rather than throw;\n * `ConfigError` always throws.\n */\n\n/**\n * Stable error codes carried in `error.code`. Discriminate on these rather\n * than on `instanceof` of removed-historical subclasses.\n */\nexport type ErrorCode =\n // ConfigError — construction-time\n | \"decision_space_collision\"\n | \"decision_space_too_large\"\n | \"missing_provider_config\"\n | \"malformed_provider_config\"\n | \"unknown_provider_factory\"\n | \"missing_credential\"\n | \"incompatible_calibrator\"\n | \"invalid_classifier_name\"\n | \"invalid_thresholds\"\n | \"invalid_space\"\n | \"empty_providers\"\n // ProviderError — runtime\n | \"provider_network\"\n | \"provider_rate_limit\"\n | \"provider_timeout\"\n | \"provider_unauthorized\"\n | \"provider_server_error\"\n | \"provider_malformed_response\"\n | \"invalid_distribution\"\n // BudgetExhaustedError — runtime\n | \"per_call_timeout\"\n | \"chain_timeout\"\n | \"max_calls\";\n\nexport class DomovoiError extends Error {\n readonly code: string;\n\n constructor(message: string, options?: { code?: string; cause?: unknown }) {\n super(message, options?.cause !== undefined ? { cause: options.cause } : undefined);\n this.name = \"DomovoiError\";\n this.code = options?.code ?? \"unspecified\";\n }\n}\n\nexport class ProviderError extends DomovoiError {\n constructor(message: string, options?: { code?: string; cause?: unknown }) {\n super(message, options);\n this.name = \"ProviderError\";\n }\n}\n\nexport class ConfigError extends DomovoiError {\n constructor(message: string, options?: { code?: string; cause?: unknown }) {\n super(message, options);\n this.name = \"ConfigError\";\n }\n}\n\nexport class BudgetExhaustedError extends DomovoiError {\n readonly attemptedProviders: readonly string[];\n readonly elapsedMs: number;\n readonly scope: \"per_call_timeout\" | \"chain_timeout\" | \"max_calls\";\n\n constructor(\n message: string,\n options: {\n scope: \"per_call_timeout\" | \"chain_timeout\" | \"max_calls\";\n attemptedProviders: readonly string[];\n elapsedMs: number;\n cause?: unknown;\n },\n ) {\n super(message, { code: options.scope, cause: options.cause });\n this.name = \"BudgetExhaustedError\";\n this.scope = options.scope;\n this.attemptedProviders = options.attemptedProviders;\n this.elapsedMs = options.elapsedMs;\n }\n}\n\n/**\n * Wraps any non-DomovoiError thrown value in `ProviderError({ cause })`.\n * `DomovoiError` subtypes — including `ProviderError` subclasses defined by\n * external Provider implementations — pass through unchanged.\n */\nexport function canonicalizeProviderThrow(thrown: unknown): DomovoiError {\n if (thrown instanceof DomovoiError) return thrown;\n if (thrown instanceof Error) {\n return new ProviderError(thrown.message || \"Provider call failed\", {\n code: \"provider_network\",\n cause: thrown,\n });\n }\n return new ProviderError(String(thrown), {\n code: \"provider_network\",\n cause: thrown,\n });\n}\n\n/**\n * Convert an Error to the JSON-safe shape stored in\n * `Verdict.meta.providerErrors`. Cause chains are preserved recursively.\n */\nexport function serializeError(err: unknown): {\n readonly name: string;\n readonly message: string;\n readonly code?: string;\n readonly cause?: ReturnType<typeof serializeError>;\n readonly stack?: string;\n} {\n if (!(err instanceof Error)) {\n return { name: \"Error\", message: String(err) };\n }\n const code = err instanceof DomovoiError ? err.code : undefined;\n const cause = err.cause !== undefined ? serializeError(err.cause) : undefined;\n return {\n name: err.name,\n message: err.message,\n ...(code !== undefined ? { code } : {}),\n ...(cause !== undefined ? { cause } : {}),\n ...(err.stack !== undefined ? { stack: err.stack } : {}),\n };\n}\n","/**\n * Built-in calibrator factories.\n *\n * - `identity` — pass-through; the default.\n * - `temperatureScaling(T)` — `p^(1/T) / Σ p_j^(1/T)`; equivalent to\n * scaling logits before softmax.\n * - `plattScaling({ a, b })` — sigmoid scaling; binary spaces only.\n *\n * `Calibrator.apply` must be pure and stateless. The engine runs it\n * per-caller after the Distribution is loaded from the cache, so impurity\n * would leak across callers sharing a cache row.\n */\n\nimport { ConfigError } from \"../errors.js\";\nimport type { Distribution } from \"../types.js\";\n\nexport interface Calibrator {\n /** Identifier; `\"identity\"` is reserved and recognized by the engine. */\n readonly kind: string;\n apply<T extends string>(d: Distribution<T>): Distribution<T>;\n}\n\nexport const identity: Calibrator = {\n kind: \"identity\",\n apply<T extends string>(d: Distribution<T>): Distribution<T> {\n return d;\n },\n};\n\n/**\n * - `T = 1` → identity.\n * - `T > 1` → softens (less peaked).\n * - `T < 1` → sharpens (more peaked).\n *\n * Operates on probabilities (not logits) and leaves `coverage` unchanged.\n * Throws `ConfigError` on `T <= 0`.\n *\n * @example\n * calibrator: temperatureScaling(0.85) // user-fit on held-out eval set\n */\nexport function temperatureScaling(T: number): Calibrator {\n if (Number.isNaN(T) || T <= 0) {\n throw new ConfigError(`temperatureScaling(T): T must be > 0; got ${T}.`, {\n code: \"incompatible_calibrator\",\n });\n }\n const inverseT = 1 / T;\n return {\n kind: \"temperature\",\n apply<U extends string>(d: Distribution<U>): Distribution<U> {\n const scaledEntries = (Object.entries(d.probs) as [string, number][]).map(\n ([label, prob]) => [label, prob === 0 ? 0 : prob ** inverseT] as const,\n );\n const partitionFn = scaledEntries.reduce(\n (running, [, scaledProb]) => running + scaledProb,\n 0,\n );\n // Degenerate input (every prob is 0) — return as-is rather than divide by 0.\n if (partitionFn === 0) return d;\n const normalizedProbs = Object.fromEntries(\n scaledEntries.map(([label, scaledProb]) => [label, scaledProb / partitionFn] as const),\n );\n return {\n probs: normalizedProbs as Distribution<U>[\"probs\"],\n coverage: d.coverage,\n };\n },\n };\n}\n\n/**\n * Platt scaling: `sigmoid(a*z + b)` where `z = logit(p_pos)`.\n *\n * Binary-only — the second label in the distribution (in user-given order)\n * is treated as the positive class. Construction-time space-size validation\n * happens in the classifier; `apply()` re-checks at runtime as a defensive\n * guard against direct misuse.\n */\nexport function plattScaling(params: { a: number; b: number }): Calibrator {\n if (\n Number.isNaN(params.a) ||\n Number.isNaN(params.b) ||\n !Number.isFinite(params.a) ||\n !Number.isFinite(params.b)\n ) {\n throw new ConfigError(\n `plattScaling({ a, b }): a and b must be finite numbers; got a=${params.a}, b=${params.b}.`,\n { code: \"incompatible_calibrator\" },\n );\n }\n return {\n kind: \"platt\",\n apply<U extends string>(d: Distribution<U>): Distribution<U> {\n const labels = Object.keys(d.probs);\n if (labels.length !== 2) {\n throw new ConfigError(\n `plattScaling is binary-only; got distribution with ${labels.length} labels.`,\n { code: \"incompatible_calibrator\" },\n );\n }\n const [neg, pos] = labels as [string, string];\n const probs = d.probs as Record<string, number>;\n const pPos = probs[pos] ?? 0;\n // Clamp away from {0, 1} so `Math.log(p / (1 - p))` is finite.\n const eps = 1e-9;\n const clamped = Math.min(1 - eps, Math.max(eps, pPos));\n const logit = Math.log(clamped / (1 - clamped));\n const calibratedPos = sigmoid(params.a * logit + params.b);\n return {\n probs: {\n [neg]: 1 - calibratedPos,\n [pos]: calibratedPos,\n } as Distribution<U>[\"probs\"],\n coverage: d.coverage,\n };\n },\n };\n}\n\nfunction sigmoid(x: number): number {\n return 1 / (1 + Math.exp(-x));\n}\n\nexport function isIdentityCalibrator(c: Calibrator): boolean {\n return c.kind === \"identity\";\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abort and budget utilities.
|
|
3
|
+
*
|
|
4
|
+
* Engine signal merging: user signal + per-call timeout merge into a single
|
|
5
|
+
* `AbortSignal.any` that's passed to the provider. When the merged signal
|
|
6
|
+
* aborts, we discriminate on the abort reason to decide whether the
|
|
7
|
+
* resulting Verdict is `Unknown { cancelled }` (user-initiated) or
|
|
8
|
+
* `Unknown { budget_exhausted, scope: "per_call_timeout" }` (timeout-initiated).
|
|
9
|
+
*
|
|
10
|
+
* AggregateError construction: under `onErrorPolicy: "throw"` and full-chain
|
|
11
|
+
* provider failure, we throw `AggregateError` containing rehydrated `Error`
|
|
12
|
+
* instances. `deserializeForAggregate` walks the SerializableError shape back
|
|
13
|
+
* into Error instances with `cause` chaining preserved.
|
|
14
|
+
*/
|
|
15
|
+
import type { Provider } from "../providers/provider.js";
|
|
16
|
+
import type { SerializableError, Unknown } from "../types.js";
|
|
17
|
+
import type { MetaBuilder } from "./meta.js";
|
|
18
|
+
/**
|
|
19
|
+
* Returns a human-readable string describing why a signal aborted, or
|
|
20
|
+
* `undefined` if the signal isn't aborted (or wasn't supplied).
|
|
21
|
+
*/
|
|
22
|
+
export declare function abortReason(signal: AbortSignal | undefined): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Detects whether a merged signal's abort came from `AbortSignal.timeout(...)`.
|
|
25
|
+
* The timeout path produces a DOMException with `name === "TimeoutError"`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isTimeoutAbort(signal: AbortSignal): boolean;
|
|
28
|
+
export declare function buildCancelledVerdict<T extends string>(meta: MetaBuilder, reason: string, provider: Provider): Unknown<T>;
|
|
29
|
+
type BudgetScope = "per_call_timeout" | "chain_timeout" | "max_calls";
|
|
30
|
+
/**
|
|
31
|
+
* Build `Unknown { budget_exhausted, scope }` for `onErrorPolicy: "fallback"`.
|
|
32
|
+
* Returns `undefined` under `"throw"` policy — the engine throws
|
|
33
|
+
* `BudgetExhaustedError` instead.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildBudgetExhaustedVerdict<T extends string>(meta: MetaBuilder, scope: BudgetScope, policy: "fallback" | "throw", provider?: Provider): Unknown<T> | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Rehydrate a `SerializableError` (the JSON-safe shape stored in
|
|
38
|
+
* `meta.providerErrors`) back into an `Error` instance suitable for
|
|
39
|
+
* `AggregateError`. Cause chains are reconstructed recursively.
|
|
40
|
+
*/
|
|
41
|
+
export declare function deserializeForAggregate(serialized: SerializableError): Error;
|
|
42
|
+
export {};
|
|
43
|
+
//# sourceMappingURL=abort.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abort.d.ts","sourceRoot":"","sources":["../../src/engine/abort.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAG7C;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAO/E;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAG3D;AAED,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,QAAQ,GACjB,OAAO,CAAC,CAAC,CAAC,CAMZ;AAED,KAAK,WAAW,GAAG,kBAAkB,GAAG,eAAe,GAAG,WAAW,CAAC;AAEtE;;;;GAIG;AACH,wBAAgB,2BAA2B,CAAC,CAAC,SAAS,MAAM,EAC1D,IAAI,EAAE,WAAW,EACjB,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,UAAU,GAAG,OAAO,EAC5B,QAAQ,CAAC,EAAE,QAAQ,GAClB,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,CAOxB;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,iBAAiB,GAAG,KAAK,CAY5E"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine configuration: `DecideConfig`, defaults, validation, and the
|
|
3
|
+
* `withDefaults` builder used by verbs and one-shots.
|
|
4
|
+
*
|
|
5
|
+
* `DecideConfig` is the engine's internal contract — verbs (boolean,
|
|
6
|
+
* classify, classifier) translate user-facing options into this shape.
|
|
7
|
+
*/
|
|
8
|
+
import { type Cache } from "../cache.js";
|
|
9
|
+
import type { Calibrator } from "../calibration/index.js";
|
|
10
|
+
import type { ProviderError } from "../errors.js";
|
|
11
|
+
import type { Provider } from "../providers/provider.js";
|
|
12
|
+
import type { Budget, PromptTemplate, Thresholds } from "../types.js";
|
|
13
|
+
export declare const DEFAULT_PER_CALL_TIMEOUT_MS = 10000;
|
|
14
|
+
export declare const DEFAULT_CHAIN_TIMEOUT_MS = 30000;
|
|
15
|
+
type OnProviderErrorHook = (err: ProviderError, ctx: {
|
|
16
|
+
providerId: string;
|
|
17
|
+
attempt: number;
|
|
18
|
+
}) => void | Promise<void>;
|
|
19
|
+
type EngineHooks = {
|
|
20
|
+
onCall?: (...args: unknown[]) => void | Promise<void>;
|
|
21
|
+
onResult?: (...args: unknown[]) => void | Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
export type DecideConfig<T extends string> = {
|
|
24
|
+
readonly space: readonly T[];
|
|
25
|
+
readonly thresholds: Thresholds<readonly T[]>;
|
|
26
|
+
readonly providers: readonly Provider[];
|
|
27
|
+
readonly calibrator: Calibrator;
|
|
28
|
+
readonly cache: Cache;
|
|
29
|
+
readonly template: PromptTemplate;
|
|
30
|
+
readonly question?: string;
|
|
31
|
+
readonly budget?: Budget;
|
|
32
|
+
readonly onErrorPolicy: "fallback" | "throw";
|
|
33
|
+
readonly onProviderError?: OnProviderErrorHook;
|
|
34
|
+
readonly hooks?: EngineHooks;
|
|
35
|
+
/**
|
|
36
|
+
* Stable hash of provider-specific options that affect Distribution shape
|
|
37
|
+
* (e.g. `multiSampleN`). Mixed into the cache key so changing those opts is
|
|
38
|
+
* a cache miss. Empty string when the provider has no such options.
|
|
39
|
+
*/
|
|
40
|
+
readonly providerConfigHash: string;
|
|
41
|
+
readonly temperature: number;
|
|
42
|
+
};
|
|
43
|
+
type DecideConfigInput<T extends string> = {
|
|
44
|
+
readonly space: readonly T[];
|
|
45
|
+
readonly thresholds: Thresholds<readonly T[]>;
|
|
46
|
+
readonly providers: readonly Provider[];
|
|
47
|
+
readonly calibrator?: Calibrator;
|
|
48
|
+
readonly cache?: Cache;
|
|
49
|
+
readonly template?: PromptTemplate;
|
|
50
|
+
readonly question?: string;
|
|
51
|
+
readonly budget?: Budget;
|
|
52
|
+
readonly onErrorPolicy?: "fallback" | "throw";
|
|
53
|
+
readonly onProviderError?: OnProviderErrorHook;
|
|
54
|
+
readonly hooks?: EngineHooks;
|
|
55
|
+
readonly providerConfigHash?: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Materialize a fully-defaulted `DecideConfig` from a partial input. Used by
|
|
59
|
+
* verbs to translate caller-facing options into the engine's internal shape.
|
|
60
|
+
*/
|
|
61
|
+
export declare function withDefaults<T extends string>(input: DecideConfigInput<T>): DecideConfig<T>;
|
|
62
|
+
type ValidateClassifierConfigInput<T extends string> = {
|
|
63
|
+
readonly name?: string;
|
|
64
|
+
readonly space: readonly T[];
|
|
65
|
+
readonly thresholds: Thresholds<readonly T[]>;
|
|
66
|
+
readonly providers: readonly Provider[];
|
|
67
|
+
readonly calibrator: Calibrator;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Validate a classifier configuration at construction. Throws `ConfigError`
|
|
71
|
+
* with the relevant `code` on first failure; never partially configures.
|
|
72
|
+
*
|
|
73
|
+
* Each registered provider's optional `validate(space)` hook fires last —
|
|
74
|
+
* giving tokenizer-aware adapters (e.g. OpenAI) a chance to surface
|
|
75
|
+
* first-token collisions eagerly, before any network I/O.
|
|
76
|
+
*/
|
|
77
|
+
export declare function validateClassifierConfig<T extends string>(input: ValidateClassifierConfigInput<T>): void;
|
|
78
|
+
export {};
|
|
79
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/engine/config.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,KAAK,EAAe,MAAM,aAAa,CAAC;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAStE,eAAO,MAAM,2BAA2B,QAAS,CAAC;AAClD,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAE/C,KAAK,mBAAmB,GAAG,CACzB,GAAG,EAAE,aAAa,EAClB,GAAG,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,KACzC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B,KAAK,WAAW,GAAG;IACjB,MAAM,CAAC,EAAE,CAA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AU,CAAC;IACjC,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,QAAQ,CAAC,QAAQ,CAAC,EAAE,cAAc,CAAC;IACnC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,aAAa,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC;IAC9C,QAAQ,CAAC,eAAe,CAAC,EAAE,mBAAmB,CAAC;IAC/C,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC;CACtC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAgB3F;AAED,KAAK,6BAA6B,CAAC,CAAC,SAAS,MAAM,IAAI;IACrD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IAC9C,QAAQ,CAAC,SAAS,EAAE,SAAS,QAAQ,EAAE,CAAC;IACxC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACjC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,SAAS,MAAM,EACvD,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC,GACtC,IAAI,CASN"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `decide()` — the engine orchestrator. Walks the provider chain, dispatching
|
|
3
|
+
* each attempt's outcome (`Verdict` / `lastUncertain` / `continue`) into the
|
|
4
|
+
* right terminal path.
|
|
5
|
+
*
|
|
6
|
+
* Single-responsibility helpers handle the heavy lifting:
|
|
7
|
+
* - cache + provider call → `engine/distribution.ts`
|
|
8
|
+
* - error → outcome translation → `engine/error-recording.ts`
|
|
9
|
+
* - terminal Verdict construction → `engine/finalize.ts`
|
|
10
|
+
*
|
|
11
|
+
* Errors during the chain become `Unknown` Verdicts under the default
|
|
12
|
+
* `onErrorPolicy: "fallback"`; under `"throw"` they propagate as
|
|
13
|
+
* `BudgetExhaustedError` / `AbortError` / `AggregateError`.
|
|
14
|
+
*/
|
|
15
|
+
import type { Verdict } from "../types.js";
|
|
16
|
+
import { type DecideConfig } from "./config.js";
|
|
17
|
+
export declare function decide<T extends string>(formattedInput: string, config: DecideConfig<T>, signal?: AbortSignal): Promise<Verdict<T>>;
|
|
18
|
+
//# sourceMappingURL=decide.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decide.d.ts","sourceRoot":"","sources":["../../src/engine/decide.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EAAgD,OAAO,EAAE,MAAM,aAAa,CAAC;AAGzF,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,aAAa,CAAC;AAYrB,wBAAsB,MAAM,CAAC,CAAC,SAAS,MAAM,EAC3C,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CA8DrB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache lookup, in-flight dedup, and per-attempt cache-key composition.
|
|
3
|
+
* The provider call itself happens inside `loadDistribution`; the engine
|
|
4
|
+
* orchestrator never invokes `Provider.sample` directly.
|
|
5
|
+
*/
|
|
6
|
+
import type { Provider } from "../providers/provider.js";
|
|
7
|
+
import type { Distribution } from "../types.js";
|
|
8
|
+
import { type DecideConfig } from "./config.js";
|
|
9
|
+
import type { MetaBuilder } from "./meta.js";
|
|
10
|
+
export declare function computeProviderCacheKey<T extends string>(provider: Provider, formattedInput: string, config: DecideConfig<T>): string;
|
|
11
|
+
export declare function mergeSignals(userSignal: AbortSignal | undefined, timeoutSignal: AbortSignal): AbortSignal;
|
|
12
|
+
export declare function loadDistribution<T extends string>(provider: Provider, formattedInput: string, config: DecideConfig<T>, meta: MetaBuilder, signal: AbortSignal, cacheKey: string): Promise<Distribution<T>>;
|
|
13
|
+
/**
|
|
14
|
+
* Drop the in-flight slot for a key after a failed sample so a concurrent
|
|
15
|
+
* caller in another classifier doesn't keep getting served the failed Promise.
|
|
16
|
+
*/
|
|
17
|
+
export declare function forgetInFlight(cacheKey: string): void;
|
|
18
|
+
//# sourceMappingURL=distribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distribution.d.ts","sourceRoot":"","sources":["../../src/engine/distribution.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAA+B,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAC7E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAS7C,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EACtD,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GACtB,MAAM,CAWR;AAED,wBAAgB,YAAY,CAC1B,UAAU,EAAE,WAAW,GAAG,SAAS,EACnC,aAAa,EAAE,WAAW,GACzB,WAAW,CAIb;AAED,wBAAsB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EACrD,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAa1B;AAmBD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAErD"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error-handling helpers for `decide()`'s attempt loop. Translates raw
|
|
3
|
+
* exceptions thrown from the provider/cache/validation path into either
|
|
4
|
+
* a terminal `Verdict` (for cancellation, budget exhaustion) or a
|
|
5
|
+
* "continue chain" signal (for swallowable `ProviderError`).
|
|
6
|
+
*
|
|
7
|
+
* Distinguishes between three error sources: per-call timeout, user
|
|
8
|
+
* cancellation, and provider-side failure. Each produces a different
|
|
9
|
+
* `AttemptOutcome` shape that the orchestrator dispatches on.
|
|
10
|
+
*/
|
|
11
|
+
import type { Provider } from "../providers/provider.js";
|
|
12
|
+
import type { Distribution, Uncertain, Verdict } from "../types.js";
|
|
13
|
+
import type { DecideConfig } from "./config.js";
|
|
14
|
+
import type { MetaBuilder } from "./meta.js";
|
|
15
|
+
/**
|
|
16
|
+
* Outcome of a single provider attempt within `decide()`. The orchestrator
|
|
17
|
+
* dispatches on this:
|
|
18
|
+
* - `verdict`: terminal — return immediately.
|
|
19
|
+
* - `lastUncertain`: terminal-if-last-provider — return Uncertain Verdict.
|
|
20
|
+
* - `continue`: try the next provider (after recording any error in meta).
|
|
21
|
+
*/
|
|
22
|
+
export type AttemptOutcome<T extends string> = {
|
|
23
|
+
kind: "verdict";
|
|
24
|
+
verdict: Verdict<T>;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "lastUncertain";
|
|
27
|
+
verdict: Uncertain<T>;
|
|
28
|
+
calibrated: Distribution<T>;
|
|
29
|
+
} | {
|
|
30
|
+
kind: "continue";
|
|
31
|
+
calibrated?: Distribution<T>;
|
|
32
|
+
};
|
|
33
|
+
export declare function handleDistributionError<T extends string>(err: unknown, provider: Provider, meta: MetaBuilder, config: DecideConfig<T>, userSignal: AbortSignal | undefined, timeoutSignal: AbortSignal, index: number, chainStartMs: number, cacheKey: string): AttemptOutcome<T>;
|
|
34
|
+
export declare function recordValidationError<T extends string>(err: unknown, provider: Provider, meta: MetaBuilder, config: DecideConfig<T>, index: number, cacheKey: string): AttemptOutcome<T>;
|
|
35
|
+
//# sourceMappingURL=error-recording.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-recording.d.ts","sourceRoot":"","sources":["../../src/engine/error-recording.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAQH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAOpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,IACvC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;IAAC,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,CAAA;CAAE,GAC7E;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,UAAU,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAA;CAAE,CAAC;AAEvD,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EACtD,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,UAAU,EAAE,WAAW,GAAG,SAAS,EACnC,aAAa,EAAE,WAAW,EAC1B,KAAK,EAAE,MAAM,EACb,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,MAAM,GACf,cAAc,CAAC,CAAC,CAAC,CAwBnB;AAwBD,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACf,cAAc,CAAC,CAAC,CAAC,CAUnB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal-Verdict construction for `decide()`'s exit paths:
|
|
3
|
+
* chain-budget exhaustion, pre/mid-loop cancellation, and chain-exhausted
|
|
4
|
+
* (every-provider-errored vs last-provider-uncertain).
|
|
5
|
+
*/
|
|
6
|
+
import type { Distribution, Unknown, Verdict } from "../types.js";
|
|
7
|
+
import type { DecideConfig } from "./config.js";
|
|
8
|
+
import { type MetaBuilder } from "./meta.js";
|
|
9
|
+
export declare function checkChainBudget<T extends string>(meta: MetaBuilder, chainStartMs: number, chainTimeoutMs: number, config: DecideConfig<T>): Unknown<T> | undefined;
|
|
10
|
+
export declare function makeCancelledFromMeta<T extends string>(meta: MetaBuilder, reason: string): Unknown<T>;
|
|
11
|
+
export declare function finalizeChainExhausted<T extends string>(meta: MetaBuilder, lastCalibrated: Distribution<T> | undefined, attempts: number, config: DecideConfig<T>): Verdict<T>;
|
|
12
|
+
//# sourceMappingURL=finalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"finalize.d.ts","sourceRoot":"","sources":["../../src/engine/finalize.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAElE,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,IAAI,EAAE,WAAW,EACjB,YAAY,EAAE,MAAM,EACpB,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GACtB,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,CAUxB;AAED,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,CAAC,CAAC,CAMZ;AAED,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,MAAM,EACrD,IAAI,EAAE,WAAW,EACjB,cAAc,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,SAAS,EAC3C,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GACtB,OAAO,CAAC,CAAC,CAAC,CAqCZ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook invocation helper.
|
|
3
|
+
*
|
|
4
|
+
* All observability hooks (`onCall`, `onResult`, `onProviderError`) are
|
|
5
|
+
* fire-and-forget: the engine never awaits them, and sync or async errors
|
|
6
|
+
* inside the hook are swallowed. A slow logger doesn't slow fallback; an
|
|
7
|
+
* unhandled rejection inside a hook can't crash the engine.
|
|
8
|
+
*/
|
|
9
|
+
type FireAndForgetFn = ((...args: never[]) => void | Promise<void>) | undefined;
|
|
10
|
+
export declare function fireAndForget(fn: FireAndForgetFn, ...args: unknown[]): void;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/engine/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,KAAK,eAAe,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,CAAC;AAEhF,wBAAgB,aAAa,CAAC,EAAE,EAAE,eAAe,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAU3E"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine barrel. Verbs (`boolean` / `classify` / `classifier`) import from
|
|
3
|
+
* here; threshold / meta / abort / hooks stay module-private.
|
|
4
|
+
*/
|
|
5
|
+
export { validateClassifierConfig, withDefaults } from "./config.js";
|
|
6
|
+
export { decide } from "./decide.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/engine/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `VerdictMeta` builder — accumulates state across the engine's chain
|
|
3
|
+
* (providers attempted, errors swallowed, latency, cache hits) and produces
|
|
4
|
+
* the final `meta` object on every Verdict variant.
|
|
5
|
+
*/
|
|
6
|
+
import type { Provider } from "../providers/provider.js";
|
|
7
|
+
import type { SerializableError, VerdictMeta } from "../types.js";
|
|
8
|
+
type ProviderErrorRecord = {
|
|
9
|
+
readonly providerId: string;
|
|
10
|
+
readonly error: SerializableError;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Mutable accumulator used by `decide()`. Only the helpers in this file should
|
|
14
|
+
* mutate it; callers append to `providersAttempted` / `providerErrors` and
|
|
15
|
+
* flip `cacheHit` directly.
|
|
16
|
+
*/
|
|
17
|
+
export type MetaBuilder = {
|
|
18
|
+
readonly providersAttempted: string[];
|
|
19
|
+
readonly providerErrors: ProviderErrorRecord[];
|
|
20
|
+
readonly startedAtMs: number;
|
|
21
|
+
cacheHit: boolean;
|
|
22
|
+
};
|
|
23
|
+
export declare function makeMetaBuilder(): MetaBuilder;
|
|
24
|
+
/**
|
|
25
|
+
* Build the final `VerdictMeta` for a Classified or Uncertain Verdict produced
|
|
26
|
+
* by a specific (successful) provider.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildMeta(builder: MetaBuilder, provider: Provider): VerdictMeta;
|
|
29
|
+
/**
|
|
30
|
+
* Build `VerdictMeta` for failure-mode Verdicts (provider_failure,
|
|
31
|
+
* chain_exhausted, cancelled, budget_exhausted) where there's no clear
|
|
32
|
+
* "successful provider" to attribute. Falls back to the last-attempted
|
|
33
|
+
* provider for `providerUsed`, or the empty string when none was attempted.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildMetaForFailure(builder: MetaBuilder, fallbackProvider?: Provider): VerdictMeta;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../src/engine/meta.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElE,KAAK,mBAAmB,GAAG;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,iBAAiB,CAAC;CACnC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,kBAAkB,EAAE,MAAM,EAAE,CAAC;IACtC,QAAQ,CAAC,cAAc,EAAE,mBAAmB,EAAE,CAAC;IAC/C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,wBAAgB,eAAe,IAAI,WAAW,CAO7C;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,GAAG,WAAW,CAU/E;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,WAAW,EACpB,gBAAgB,CAAC,EAAE,QAAQ,GAC1B,WAAW,CAWb"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Threshold application — turn a calibrated Distribution into a per-variant
|
|
3
|
+
* decision. All comparisons are inclusive (`>=`, `<=`).
|
|
4
|
+
*
|
|
5
|
+
* - **Coverage gate first.** If `coverage < coverageMin`, return
|
|
6
|
+
* `out_of_distribution` (engine wraps this into Unknown).
|
|
7
|
+
* - **Binary (N=2):** classic deadband. `top.prob >= high` → top wins;
|
|
8
|
+
* `top.prob <= low` → other wins; else Uncertain.
|
|
9
|
+
* - **Multi-class (N>2):** top-confidence rule. `top.prob >= high` → top wins;
|
|
10
|
+
* else Uncertain. Optional margin rule: requires *both*
|
|
11
|
+
* `top.prob >= high` AND `top.prob - second.prob >= margin`.
|
|
12
|
+
*/
|
|
13
|
+
import type { Distribution, Thresholds } from "../types.js";
|
|
14
|
+
type ThresholdResult<T extends string> = {
|
|
15
|
+
kind: "classified";
|
|
16
|
+
value: T;
|
|
17
|
+
probability: number;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "uncertain";
|
|
20
|
+
top: T;
|
|
21
|
+
probability: number;
|
|
22
|
+
runnerUp: T;
|
|
23
|
+
} | {
|
|
24
|
+
kind: "out_of_distribution";
|
|
25
|
+
coverage: number;
|
|
26
|
+
topIfRenormalized: T;
|
|
27
|
+
probabilityIfRenormalized: number;
|
|
28
|
+
};
|
|
29
|
+
export declare function applyThresholds<T extends string>(d: Distribution<T>, thresholds: Thresholds<readonly T[]>, space: readonly T[]): ThresholdResult<T>;
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=threshold.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"threshold.d.ts","sourceRoot":"","sources":["../../src/engine/threshold.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE5D,KAAK,eAAe,CAAC,CAAC,SAAS,MAAM,IACjC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,KAAK,EAAE,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,GAAG,EAAE,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,CAAC,CAAA;CAAE,GAC/D;IACE,IAAI,EAAE,qBAAqB,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,CAAC,CAAC;IACrB,yBAAyB,EAAE,MAAM,CAAC;CACnC,CAAC;AAWN,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAC9C,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,EAClB,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC,EACpC,KAAK,EAAE,SAAS,CAAC,EAAE,GAClB,eAAe,CAAC,CAAC,CAAC,CAqBpB"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment-driven provider chain resolution.
|
|
3
|
+
*
|
|
4
|
+
* DOMOVOI_PROVIDERS=openai/gpt-4o-mini,openai/gpt-4o,ollama/llama-3.1-70b
|
|
5
|
+
* DOMOVOI_PROVIDERS_ARTICLES=... (override for `classifier({ name: "articles" })`)
|
|
6
|
+
*
|
|
7
|
+
* Format: `factory/model[,factory/model…]`. The first `/` separates factory
|
|
8
|
+
* from model; later slashes belong to the model name (e.g.
|
|
9
|
+
* `openrouter/meta-llama/llama-3.1-70b`). Whitespace is trimmed; empty
|
|
10
|
+
* entries are skipped; an empty / whitespace-only value counts as unset.
|
|
11
|
+
*
|
|
12
|
+
* Per-provider parametric options (`multiSampleN`, custom timeouts) cannot
|
|
13
|
+
* be expressed in env — drop to an explicit `providers: [...]` array for
|
|
14
|
+
* those.
|
|
15
|
+
*/
|
|
16
|
+
import type { Provider } from "./providers/provider.js";
|
|
17
|
+
type ParsedEntry = {
|
|
18
|
+
readonly factory: string;
|
|
19
|
+
readonly model: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Parses a DOMOVOI_PROVIDERS-style string into typed entries. Throws
|
|
23
|
+
* `ConfigError` with `code: "malformed_provider_config"` on a bad entry.
|
|
24
|
+
* Returns `[]` for empty / whitespace-only input — callers treat that as unset.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseProvidersEnv(raw: string | undefined): readonly ParsedEntry[];
|
|
27
|
+
/**
|
|
28
|
+
* Resolves a DOMOVOI_PROVIDERS-style env value into Provider instances.
|
|
29
|
+
*
|
|
30
|
+
* Throws `ConfigError` with `code` ∈ { `missing_provider_config`,
|
|
31
|
+
* `malformed_provider_config`, `unknown_provider_factory` }. Credential
|
|
32
|
+
* presence is not checked here — the underlying SDK surfaces that on first
|
|
33
|
+
* call.
|
|
34
|
+
*/
|
|
35
|
+
export declare function resolveProvidersFromEnv(raw: string | undefined): readonly Provider[];
|
|
36
|
+
/**
|
|
37
|
+
* Resolves the chain for a classifier with optional `name`. Tries
|
|
38
|
+
* `DOMOVOI_PROVIDERS_<NAME>` first (uppercased), then `DOMOVOI_PROVIDERS`.
|
|
39
|
+
* Throws `ConfigError({ code: "missing_provider_config" })` if both are unset.
|
|
40
|
+
*
|
|
41
|
+
* Tests must stub env *before* the first domovoi call — env is read lazily,
|
|
42
|
+
* but only once per classifier resolution.
|
|
43
|
+
*/
|
|
44
|
+
export declare function resolveDefaultProviders(name?: string): readonly Provider[];
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAcxD,KAAK,WAAW,GAAG;IACjB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,WAAW,EAAE,CAUjF;AAqBD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,QAAQ,EAAE,CAmBpF;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,QAAQ,EAAE,CAS1E"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error taxonomy for domovoi.
|
|
3
|
+
*
|
|
4
|
+
* Four classes (`DomovoiError` base + `ProviderError`, `ConfigError`,
|
|
5
|
+
* `BudgetExhaustedError`) with a stable `code` field for fine-grained
|
|
6
|
+
* discrimination. All accept `{ cause }` for ES2022 chaining.
|
|
7
|
+
*
|
|
8
|
+
* The engine canonicalizes anything thrown from `Provider.sample` that isn't
|
|
9
|
+
* already a `DomovoiError` into `ProviderError({ cause })`, so callers always
|
|
10
|
+
* see a known error type. Under the default `onErrorPolicy: "fallback"`,
|
|
11
|
+
* runtime errors become `Unknown` Verdict variants rather than throw;
|
|
12
|
+
* `ConfigError` always throws.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Stable error codes carried in `error.code`. Discriminate on these rather
|
|
16
|
+
* than on `instanceof` of removed-historical subclasses.
|
|
17
|
+
*/
|
|
18
|
+
export type ErrorCode = "decision_space_collision" | "decision_space_too_large" | "missing_provider_config" | "malformed_provider_config" | "unknown_provider_factory" | "missing_credential" | "incompatible_calibrator" | "invalid_classifier_name" | "invalid_thresholds" | "invalid_space" | "empty_providers" | "provider_network" | "provider_rate_limit" | "provider_timeout" | "provider_unauthorized" | "provider_server_error" | "provider_malformed_response" | "invalid_distribution" | "per_call_timeout" | "chain_timeout" | "max_calls";
|
|
19
|
+
export declare class DomovoiError extends Error {
|
|
20
|
+
readonly code: string;
|
|
21
|
+
constructor(message: string, options?: {
|
|
22
|
+
code?: string;
|
|
23
|
+
cause?: unknown;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export declare class ProviderError extends DomovoiError {
|
|
27
|
+
constructor(message: string, options?: {
|
|
28
|
+
code?: string;
|
|
29
|
+
cause?: unknown;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export declare class ConfigError extends DomovoiError {
|
|
33
|
+
constructor(message: string, options?: {
|
|
34
|
+
code?: string;
|
|
35
|
+
cause?: unknown;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export declare class BudgetExhaustedError extends DomovoiError {
|
|
39
|
+
readonly attemptedProviders: readonly string[];
|
|
40
|
+
readonly elapsedMs: number;
|
|
41
|
+
readonly scope: "per_call_timeout" | "chain_timeout" | "max_calls";
|
|
42
|
+
constructor(message: string, options: {
|
|
43
|
+
scope: "per_call_timeout" | "chain_timeout" | "max_calls";
|
|
44
|
+
attemptedProviders: readonly string[];
|
|
45
|
+
elapsedMs: number;
|
|
46
|
+
cause?: unknown;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Wraps any non-DomovoiError thrown value in `ProviderError({ cause })`.
|
|
51
|
+
* `DomovoiError` subtypes — including `ProviderError` subclasses defined by
|
|
52
|
+
* external Provider implementations — pass through unchanged.
|
|
53
|
+
*/
|
|
54
|
+
export declare function canonicalizeProviderThrow(thrown: unknown): DomovoiError;
|
|
55
|
+
/**
|
|
56
|
+
* Convert an Error to the JSON-safe shape stored in
|
|
57
|
+
* `Verdict.meta.providerErrors`. Cause chains are preserved recursively.
|
|
58
|
+
*/
|
|
59
|
+
export declare function serializeError(err: unknown): {
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly message: string;
|
|
62
|
+
readonly code?: string;
|
|
63
|
+
readonly cause?: ReturnType<typeof serializeError>;
|
|
64
|
+
readonly stack?: string;
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;;GAGG;AACH,MAAM,MAAM,SAAS,GAEjB,0BAA0B,GAC1B,0BAA0B,GAC1B,yBAAyB,GACzB,2BAA2B,GAC3B,0BAA0B,GAC1B,oBAAoB,GACpB,yBAAyB,GACzB,yBAAyB,GACzB,oBAAoB,GACpB,eAAe,GACf,iBAAiB,GAEjB,kBAAkB,GAClB,qBAAqB,GACrB,kBAAkB,GAClB,uBAAuB,GACvB,uBAAuB,GACvB,6BAA6B,GAC7B,sBAAsB,GAEtB,kBAAkB,GAClB,eAAe,GACf,WAAW,CAAC;AAEhB,qBAAa,YAAa,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAK1E;AAED,qBAAa,aAAc,SAAQ,YAAY;gBACjC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAA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256 from `node:crypto` (stdlib only; cross-process stable so future
|
|
5
|
+
* persistent backends can share keys).
|
|
6
|
+
* - Canonical JSON serialization with lexicographically sorted object keys.
|
|
7
|
+
* - NFC normalization + trim for input string hashing.
|
|
8
|
+
*/
|
|
9
|
+
/** SHA-256 hex digest of the given string. */
|
|
10
|
+
export declare function sha256(s: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Canonicalize a JSON-serializable value into a deterministic string:
|
|
13
|
+
* - Object keys sorted lexicographically.
|
|
14
|
+
* - Arrays preserve order — decision-space ordering in cache keys honors
|
|
15
|
+
* the user-given order (re-ordering changes the prompt and the model's
|
|
16
|
+
* first-token distribution, so it must change the cache key).
|
|
17
|
+
* - undefined values are omitted (matches JSON.stringify semantics for object values).
|
|
18
|
+
*
|
|
19
|
+
* Throws on non-JSON-serializable inputs (functions, symbols, circular refs).
|
|
20
|
+
*/
|
|
21
|
+
export declare function canonicalJSON(value: unknown): string;
|
|
22
|
+
/**
|
|
23
|
+
* Normalize an input string for hashing: NFC normalization + trim.
|
|
24
|
+
* Used to compute `input_hash` in the cache key composition.
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalizeInput(input: string): string;
|
|
27
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,8CAA8C;AAC9C,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAExC;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEpD;AAeD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD"}
|