@intx/inference 0.1.2 → 0.3.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 +176 -0
- package/dist/actions.d.ts +16 -0
- package/dist/actions.js +200 -0
- package/dist/adapter.d.ts +40 -0
- package/dist/adapter.js +31 -0
- package/dist/assembly.d.ts +75 -0
- package/dist/assembly.js +133 -0
- package/dist/audit-collector.d.ts +10 -0
- package/dist/audit-collector.js +139 -0
- package/dist/auth.d.ts +24 -0
- package/{src/auth.ts → dist/auth.js} +13 -19
- package/dist/authz-extension.d.ts +46 -0
- package/dist/authz-extension.js +184 -0
- package/dist/correlation.d.ts +26 -0
- package/dist/correlation.js +39 -0
- package/dist/default-director.d.ts +111 -0
- package/dist/default-director.js +228 -0
- package/dist/director.d.ts +6 -0
- package/dist/director.js +56 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +83 -0
- package/dist/gates.d.ts +28 -0
- package/dist/gates.js +103 -0
- package/dist/harness.d.ts +147 -0
- package/dist/harness.js +1407 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +21 -0
- package/dist/manifest.d.ts +31 -0
- package/dist/manifest.js +44 -0
- package/dist/providers/anthropic.d.ts +37 -0
- package/dist/providers/anthropic.js +917 -0
- package/dist/providers/google-genai-files.d.ts +48 -0
- package/dist/providers/google-genai-files.js +205 -0
- package/dist/providers/google-genai.d.ts +5 -0
- package/dist/providers/google-genai.js +1205 -0
- package/dist/providers/index.d.ts +38 -0
- package/dist/providers/index.js +56 -0
- package/dist/providers/openai.d.ts +9 -0
- package/dist/providers/openai.js +903 -0
- package/dist/reactor.d.ts +50 -0
- package/dist/reactor.js +1233 -0
- package/dist/retry-policy.d.ts +31 -0
- package/{src/retry-policy.ts → dist/retry-policy.js} +41 -53
- package/dist/sse.d.ts +1 -0
- package/dist/sse.js +63 -0
- package/dist/state.d.ts +23 -0
- package/dist/state.js +100 -0
- package/dist/tool-name.d.ts +6 -0
- package/dist/tool-name.js +110 -0
- package/dist/transform.d.ts +11 -0
- package/dist/transform.js +132 -0
- package/dist/transforms/index.d.ts +2 -0
- package/dist/transforms/index.js +1 -0
- package/dist/transforms/size-cap.d.ts +12 -0
- package/dist/transforms/size-cap.js +80 -0
- package/dist/turns.d.ts +21 -0
- package/dist/turns.js +135 -0
- package/package.json +22 -6
- package/src/actions.ts +0 -245
- package/src/adapter.ts +0 -57
- package/src/assembly.test.ts +0 -728
- package/src/assembly.ts +0 -250
- package/src/audit-collector.test.ts +0 -332
- package/src/audit-collector.ts +0 -172
- package/src/auth.test.ts +0 -117
- package/src/authz-extension.test.ts +0 -269
- package/src/authz-extension.ts +0 -145
- package/src/correlation.ts +0 -61
- package/src/default-director.test.ts +0 -314
- package/src/default-director.ts +0 -344
- package/src/director.ts +0 -87
- package/src/errors.test.ts +0 -133
- package/src/errors.ts +0 -115
- package/src/gates.ts +0 -128
- package/src/harness.test.ts +0 -655
- package/src/harness.ts +0 -1571
- package/src/index.ts +0 -76
- package/src/providers/anthropic.test.ts +0 -771
- package/src/providers/anthropic.ts +0 -810
- package/src/providers/google-genai-files.ts +0 -289
- package/src/providers/google-genai.ts +0 -1518
- package/src/providers/openai.ts +0 -719
- package/src/providers/registry.ts +0 -33
- package/src/reactor.test.ts +0 -3660
- package/src/reactor.ts +0 -1058
- package/src/scheduler.test.ts +0 -41
- package/src/sse.test.ts +0 -133
- package/src/sse.ts +0 -76
- package/src/state.ts +0 -135
- package/src/transform.test.ts +0 -207
- package/src/transform.ts +0 -159
- package/src/transforms/index.ts +0 -2
- package/src/transforms/size-cap.test.ts +0 -172
- package/src/transforms/size-cap.ts +0 -110
- package/src/turns.ts +0 -54
- package/tsconfig.json +0 -4
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { ConversationTurn, InferenceEvent, InferenceOptions, InferenceSource } from "@intx/types/runtime";
|
|
2
|
+
import type { AdapterRegistry } from "./adapter.js";
|
|
3
|
+
/**
|
|
4
|
+
* Default per-call inactivity timeout (ms). Two minutes is conservative
|
|
5
|
+
* for reasoning-heavy models that emit `inference.thinking.delta` tokens
|
|
6
|
+
* regularly when actually working — sustained silence past this means
|
|
7
|
+
* the provider stream has genuinely stalled, not that the model is
|
|
8
|
+
* thinking. Operators can tune via `InferenceOptions.inactivityTimeoutMs`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_INACTIVITY_TIMEOUT_MS = 120000;
|
|
11
|
+
/**
|
|
12
|
+
* Default per-call total wall-clock cap (ms). Matches Anthropic's
|
|
13
|
+
* documented per-call recommendation and fits within typical CI
|
|
14
|
+
* timeouts. Operators can tune via `InferenceOptions.totalTimeoutMs`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_TOTAL_TIMEOUT_MS = 600000;
|
|
17
|
+
export declare const HarnessId: unique symbol;
|
|
18
|
+
/**
|
|
19
|
+
* Runtime dependencies injected into `runInference`. Code-only — not part of
|
|
20
|
+
* any persisted schema. Test harnesses substitute `fetch` (and stamp the
|
|
21
|
+
* `[HarnessId]` tag for per-harness identity) so production `runInference`
|
|
22
|
+
* never reaches `globalThis.fetch`.
|
|
23
|
+
*
|
|
24
|
+
* `fetch` is intentionally typed as a plain function rather than
|
|
25
|
+
* `typeof globalThis.fetch` — the latter is augmented per-runtime (Bun adds
|
|
26
|
+
* `preconnect`; Node and the DOM lib do not) and `runInference` only ever
|
|
27
|
+
* invokes the call signature.
|
|
28
|
+
*
|
|
29
|
+
* The `[HarnessId]` tag is enumerable via `Object.getOwnPropertySymbols`
|
|
30
|
+
* (and `Reflect.ownKeys`, which is the superset). Do not pass `Dependencies`
|
|
31
|
+
* instances through reflective serializers or expose them across trust
|
|
32
|
+
* boundaries. (`JSON.stringify` is safe — it walks string keys only.)
|
|
33
|
+
*/
|
|
34
|
+
export type Dependencies = {
|
|
35
|
+
readonly fetch: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
36
|
+
/**
|
|
37
|
+
* Time-based scheduler used by the harness's per-call timeouts (see
|
|
38
|
+
* `InferenceOptions.inactivityTimeoutMs` / `totalTimeoutMs`). Production
|
|
39
|
+
* passes the default wrapper around `setTimeout` / `clearTimeout`; the
|
|
40
|
+
* deterministic test harness injects a scheduler that wraps its virtual
|
|
41
|
+
* clock so timeout tests fire at virtual-time-N without sleeping real
|
|
42
|
+
* wall-clock. Required — every caller must make an explicit choice
|
|
43
|
+
* between the production scheduler and a virtual one. Use
|
|
44
|
+
* `createDefaultScheduler()` for the production default.
|
|
45
|
+
*/
|
|
46
|
+
readonly scheduler: Scheduler;
|
|
47
|
+
/**
|
|
48
|
+
* Registry resolving an inference source to its provider adapter.
|
|
49
|
+
* `runSingleAttempt` consults this on every call via `adapters.resolve`,
|
|
50
|
+
* so it is required — the caller makes an explicit choice of provider set.
|
|
51
|
+
* Construct it via `createDependencies(adapters)` (core) or, for the
|
|
52
|
+
* built-in set, `@intx/inference/providers`' `createDefaultDependencies()`.
|
|
53
|
+
*/
|
|
54
|
+
readonly adapters: AdapterRegistry;
|
|
55
|
+
readonly [HarnessId]?: symbol;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Minimal scheduling abstraction. `setTimeout` returns a canceller; the
|
|
59
|
+
* canceller is idempotent (multiple calls are safe). The harness uses
|
|
60
|
+
* this for both the inactivity timer (which is re-armed on every event)
|
|
61
|
+
* and the total wall-clock cap. `now()` is a monotonic time source in
|
|
62
|
+
* the same `delayMs` units `setTimeout` accepts — deltas across two
|
|
63
|
+
* `now()` reads describe elapsed time the same way `setTimeout(...,
|
|
64
|
+
* delta)` would have measured it.
|
|
65
|
+
*/
|
|
66
|
+
export type Scheduler = {
|
|
67
|
+
setTimeout(callback: () => void, delayMs: number): () => void;
|
|
68
|
+
now(): number;
|
|
69
|
+
};
|
|
70
|
+
export declare function createDefaultScheduler(): Scheduler;
|
|
71
|
+
/**
|
|
72
|
+
* Construct runtime dependencies for `runInference` from an explicit adapter
|
|
73
|
+
* registry, binding `fetch` to `globalThis.fetch` and `scheduler` to the
|
|
74
|
+
* production wrapper. The registry is required so the caller makes an explicit
|
|
75
|
+
* choice of provider set; `@intx/inference/providers`' zero-arg
|
|
76
|
+
* `createDefaultDependencies()` is the honest default that supplies the
|
|
77
|
+
* built-in registry.
|
|
78
|
+
*
|
|
79
|
+
* @param adapters - Registry resolving inference sources to provider adapters
|
|
80
|
+
* @returns Fully-populated dependencies
|
|
81
|
+
*/
|
|
82
|
+
export declare function createDependencies(adapters: AdapterRegistry): Dependencies;
|
|
83
|
+
export type InferenceHarnessOptions = {
|
|
84
|
+
turns: ConversationTurn[];
|
|
85
|
+
source: InferenceSource;
|
|
86
|
+
inferenceOptions?: InferenceOptions;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
nextSeq: () => number;
|
|
89
|
+
deps: Dependencies;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Run a single inference call with mechanical retry. Wraps
|
|
93
|
+
* `runSingleAttempt` and consults the configured `RetryPolicy` (or the
|
|
94
|
+
* default from `createDefaultRetryPolicy`) on every `inference.error`.
|
|
95
|
+
*
|
|
96
|
+
* Events from each attempt are buffered until the attempt terminates;
|
|
97
|
+
* the wrapper only flushes them to the caller once it knows whether
|
|
98
|
+
* the attempt resolved (`inference.done` or a policy-approved abort)
|
|
99
|
+
* or whether the attempt's events should be discarded in favour of a
|
|
100
|
+
* retry. The buffer-and-flush model is what guarantees the caller
|
|
101
|
+
* sees a single clean event stream — exactly one `inference.start`,
|
|
102
|
+
* no orphaned partial deltas, no leaked `inference.error`s from
|
|
103
|
+
* attempts the policy chose to retry. The cost is that no events
|
|
104
|
+
* reach the caller until the wrapper knows the attempt's terminal
|
|
105
|
+
* shape, even on a successful first attempt. That trade-off is the
|
|
106
|
+
* deliberate consequence of making "one clean stream" a hard contract
|
|
107
|
+
* rather than a best-effort one. Consumers that need token-by-token
|
|
108
|
+
* partials must pin a custom non-buffering wrapper — no streaming-
|
|
109
|
+
* partials emission API exists today.
|
|
110
|
+
*
|
|
111
|
+
* The buffer is per-call and bounded by the size of one attempt's
|
|
112
|
+
* event stream — no cross-call accumulation.
|
|
113
|
+
*
|
|
114
|
+
* Caller-visible seqs stay contiguous across retries. Each attempt
|
|
115
|
+
* runs against a private seq allocator; on flush the wrapper
|
|
116
|
+
* re-stamps the buffered events with seqs from the caller's
|
|
117
|
+
* `nextSeq`, so a retry that discards an attempt does not leave a
|
|
118
|
+
* gap in the consumer's seq stream.
|
|
119
|
+
*
|
|
120
|
+
* Between attempts the wrapper emits one `inference.retry` event with
|
|
121
|
+
* the failed attempt's number, the policy-chosen `delayMs`, and the
|
|
122
|
+
* classified error that triggered the retry. The `setTimeout` await
|
|
123
|
+
* is driven by `deps.scheduler`, so virtual-clock test harnesses
|
|
124
|
+
* advance retry delays without sleeping real wall-clock. The
|
|
125
|
+
* caller-supplied `signal` short-circuits the retry delay: aborting
|
|
126
|
+
* the signal mid-delay wakes the await immediately and the next
|
|
127
|
+
* `runSingleAttempt` invocation surfaces `inference.error` of
|
|
128
|
+
* category `aborted` from its entry-time signal check, which the
|
|
129
|
+
* default policy aborts on.
|
|
130
|
+
*
|
|
131
|
+
* Policy-failure handling: if the policy throws synchronously or its
|
|
132
|
+
* returned Promise rejects, the wrapper treats the failure as
|
|
133
|
+
* `{ kind: "abort" }` and surfaces the *original* `inference.error`
|
|
134
|
+
* to the caller. The policy's own exception is logged at `warn` so
|
|
135
|
+
* operators can see when a custom policy is failing under load, and
|
|
136
|
+
* dropped — the inference error is what the caller needs to act on,
|
|
137
|
+
* not the bug in the policy callback.
|
|
138
|
+
*
|
|
139
|
+
* Synchronous throws from `runSingleAttempt` (`ProtocolMismatchError`
|
|
140
|
+
* raised by the streaming parse or the finalization walk, etc.)
|
|
141
|
+
* propagate out of `runInference`. The current attempt's buffered
|
|
142
|
+
* events are discarded along with the throw — those represent
|
|
143
|
+
* protocol bugs the policy mechanism is not equipped to absorb, and
|
|
144
|
+
* the caller's `for await` rejects so the failure surfaces rather
|
|
145
|
+
* than being silently buffered.
|
|
146
|
+
*/
|
|
147
|
+
export declare function runInference(opts: InferenceHarnessOptions): AsyncIterable<InferenceEvent>;
|