@loomwork/llm 0.0.0-stage → 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.
Files changed (79) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +8 -0
  3. package/README.md +80 -2
  4. package/dist/assembleStream.d.ts +95 -0
  5. package/dist/assembleStream.d.ts.map +1 -0
  6. package/dist/assembleStream.js +180 -0
  7. package/dist/assembleStream.js.map +1 -0
  8. package/dist/capabilities.d.ts +43 -0
  9. package/dist/capabilities.d.ts.map +1 -0
  10. package/dist/capabilities.js +77 -0
  11. package/dist/capabilities.js.map +1 -0
  12. package/dist/createLlmEffect.d.ts +110 -0
  13. package/dist/createLlmEffect.d.ts.map +1 -0
  14. package/dist/createLlmEffect.js +177 -0
  15. package/dist/createLlmEffect.js.map +1 -0
  16. package/dist/errors.d.ts +96 -0
  17. package/dist/errors.d.ts.map +1 -0
  18. package/dist/errors.js +280 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/index.d.ts +24 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +71 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/parts.d.ts +220 -0
  25. package/dist/parts.d.ts.map +1 -0
  26. package/dist/parts.js +231 -0
  27. package/dist/parts.js.map +1 -0
  28. package/dist/providers/AnthropicProvider.d.ts +53 -0
  29. package/dist/providers/AnthropicProvider.d.ts.map +1 -0
  30. package/dist/providers/AnthropicProvider.js +443 -0
  31. package/dist/providers/AnthropicProvider.js.map +1 -0
  32. package/dist/providers/GeminiProvider.d.ts +66 -0
  33. package/dist/providers/GeminiProvider.d.ts.map +1 -0
  34. package/dist/providers/GeminiProvider.js +281 -0
  35. package/dist/providers/GeminiProvider.js.map +1 -0
  36. package/dist/providers/OpenAiProvider.d.ts +56 -0
  37. package/dist/providers/OpenAiProvider.d.ts.map +1 -0
  38. package/dist/providers/OpenAiProvider.js +402 -0
  39. package/dist/providers/OpenAiProvider.js.map +1 -0
  40. package/dist/providers/extraHeaders.d.ts +7 -0
  41. package/dist/providers/extraHeaders.d.ts.map +1 -0
  42. package/dist/providers/extraHeaders.js +40 -0
  43. package/dist/providers/extraHeaders.js.map +1 -0
  44. package/dist/providers/systemPrompt.d.ts +10 -0
  45. package/dist/providers/systemPrompt.d.ts.map +1 -0
  46. package/dist/providers/systemPrompt.js +22 -0
  47. package/dist/providers/systemPrompt.js.map +1 -0
  48. package/dist/reasoning.d.ts +154 -0
  49. package/dist/reasoning.d.ts.map +1 -0
  50. package/dist/reasoning.js +256 -0
  51. package/dist/reasoning.js.map +1 -0
  52. package/dist/resolveContent.d.ts +74 -0
  53. package/dist/resolveContent.d.ts.map +1 -0
  54. package/dist/resolveContent.js +335 -0
  55. package/dist/resolveContent.js.map +1 -0
  56. package/dist/types.d.ts +152 -0
  57. package/dist/types.d.ts.map +1 -0
  58. package/dist/types.js +3 -0
  59. package/dist/types.js.map +1 -0
  60. package/dist/untrusted.d.ts +91 -0
  61. package/dist/untrusted.d.ts.map +1 -0
  62. package/dist/untrusted.js +385 -0
  63. package/dist/untrusted.js.map +1 -0
  64. package/package.json +54 -3
  65. package/src/assembleStream.ts +219 -0
  66. package/src/capabilities.ts +96 -0
  67. package/src/createLlmEffect.ts +287 -0
  68. package/src/errors.ts +269 -0
  69. package/src/index.ts +137 -0
  70. package/src/parts.ts +409 -0
  71. package/src/providers/AnthropicProvider.ts +550 -0
  72. package/src/providers/GeminiProvider.ts +380 -0
  73. package/src/providers/OpenAiProvider.ts +511 -0
  74. package/src/providers/extraHeaders.ts +41 -0
  75. package/src/providers/systemPrompt.ts +19 -0
  76. package/src/reasoning.ts +386 -0
  77. package/src/resolveContent.ts +425 -0
  78. package/src/types.ts +175 -0
  79. package/src/untrusted.ts +440 -0
@@ -0,0 +1,219 @@
1
+ import { fail } from "@loomwork/core";
2
+ import type { LlmResponse, LlmStreamChunk } from "./types";
3
+
4
+ // ── The streamed-≡-completed contract ─────────────────────────────────────────
5
+ //
6
+ // Two paths to one outcome need a rule saying they agree, or they drift and the
7
+ // drift shows up as an agent that behaves differently once streaming is switched
8
+ // on. ADR 0059 D1 set the precedent for content: a streamed address MUST equal
9
+ // the whole-buffer address. This is the same discipline for the model call —
10
+ // assembling a stream MUST produce what `complete` would have returned.
11
+ //
12
+ // Assembly is deliberately dumb: concatenate the deltas, take the outcome fields
13
+ // from the terminal chunk. Anything cleverer would be a place for the two paths
14
+ // to disagree.
15
+
16
+ /** Thrown when a stream violates its shape contract (no terminal chunk, or more than one). */
17
+ export class MalformedStreamError extends Error {
18
+ constructor(message: string) {
19
+ super(`Malformed LLM stream: ${message}`);
20
+ this.name = "MalformedStreamError";
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Fold a completed stream into the `LlmResponse` it represents.
26
+ *
27
+ * `stopReason` defaults to `"end_turn"` when the provider does not report one —
28
+ * the same value `complete` yields for an unrecognised stop reason, so a
29
+ * text-only streaming provider assembles to the same shape it always would.
30
+ * `toolCalls` and `usage` stay ABSENT rather than becoming empty: `undefined`
31
+ * means "not reported", `[]` would mean "reported none", and metering must be
32
+ * able to tell a provider that omits usage from one that charged nothing.
33
+ */
34
+ export function assembleStream(chunks: readonly LlmStreamChunk[]): LlmResponse {
35
+ const terminals = chunks.filter((c) => c.done);
36
+ if (terminals.length === 0) {
37
+ throw new MalformedStreamError("no terminal chunk (none had done: true)");
38
+ }
39
+ if (terminals.length > 1) {
40
+ throw new MalformedStreamError(`${terminals.length} terminal chunks (exactly one is allowed)`);
41
+ }
42
+ const last = chunks[chunks.length - 1];
43
+ if (last?.done !== true) {
44
+ throw new MalformedStreamError("the terminal chunk is not last");
45
+ }
46
+
47
+ const content = chunks.map((c) => c.delta).join("");
48
+ return {
49
+ content,
50
+ ...(last.toolCalls !== undefined && { toolCalls: last.toolCalls }),
51
+ stopReason: last.stopReason ?? "end_turn",
52
+ ...(last.usage !== undefined && { usage: last.usage }),
53
+ ...(last.reasoning !== undefined && { reasoning: last.reasoning }),
54
+ ...(last.layout !== undefined && { layout: last.layout }),
55
+ };
56
+ }
57
+
58
+ /**
59
+ * The channel an interrupted generation fails on (ADR 0060). Its payload is
60
+ * {@link InterruptedGeneration} — the text produced before the abort, which is
61
+ * usually the most expensive artifact the turn made and was previously discarded:
62
+ * `complete` + abort throws with the response body unread, so there is nothing to
63
+ * keep. Only a call that has been accumulating deltas has a partial to hand back.
64
+ *
65
+ * Declare it in the owning contract's `errors:` map to get the ADR 0060 treatment
66
+ * (recorded as `STEP_FAILED { channel, payload }`, substituted on replay);
67
+ * undeclared, it degrades to an ordinary failure exactly as before.
68
+ */
69
+ export const LLM_INTERRUPTED = "llmInterrupted";
70
+
71
+ export interface InterruptedGeneration {
72
+ /** Deltas received before the abort, concatenated. May be empty. */
73
+ content: string;
74
+ /** Usage, if the provider reported any before the interruption. */
75
+ usage?: { inputTokens: number; outputTokens: number };
76
+ }
77
+
78
+ /** What a stream has produced so far, offered to {@link CollectStreamOptions.guard}. */
79
+ export interface StreamProgress {
80
+ /** Everything received so far, concatenated. */
81
+ text: string;
82
+ /** Number of deltas received. */
83
+ deltas: number;
84
+ /** Usage, if the provider has reported any yet. Most report only at the end. */
85
+ usage?: { inputTokens: number; outputTokens: number };
86
+ }
87
+
88
+ export interface CollectStreamOptions {
89
+ /** When this is aborted, the partial is captured instead of being thrown away. */
90
+ signal?: AbortSignal;
91
+ /** Transient sink for deltas as they arrive. Never journaled by this function. */
92
+ onDelta?: (delta: string) => void;
93
+ /**
94
+ * ADR 0133 D6 - transient sink for readable reasoning increments, a channel SEPARATE from
95
+ * `onDelta` (and never part of `guard`'s `text`, nor of the interrupted partial). Never journaled.
96
+ */
97
+ onReasoningDelta?: (delta: string) => void;
98
+ /** R5 — transient sink for tool-call input fragments. Never journaled. */
99
+ onToolDelta?: (delta: { id: string; name?: string; inputDelta: string }) => void;
100
+ /**
101
+ * Called after every delta. THROW to sever the generation mid-flight — the
102
+ * throw propagates, the `for await` exits, and the underlying HTTP call is
103
+ * released.
104
+ *
105
+ * This is the seam K8.4 needs and the only one a stream can offer: providers
106
+ * report usage once at the end, so the sole thing observable *while* a
107
+ * generation runs is its own output. A guard that throws here is the
108
+ * difference between paying for a runaway generation and stopping it.
109
+ *
110
+ * Deliberately generic — no budget vocabulary lives in this package.
111
+ * `@loomwork/middleware`'s `streamTokenBudget` builds the budget-shaped one.
112
+ */
113
+ guard?: (progress: StreamProgress) => void;
114
+ }
115
+
116
+ /**
117
+ * `assembleStream` over a live stream — drains it, feeds deltas out as they
118
+ * arrive, and folds what arrived.
119
+ *
120
+ * With a `signal`, an abort mid-generation is NOT a lost generation: what was
121
+ * produced is captured as an {@link LLM_INTERRUPTED} typed failure. Anything else
122
+ * propagates untyped, so a transport failure stays one — K9 retries the turn
123
+ * whole, and a network blip never reads as a user stop.
124
+ *
125
+ * Shared by `createLlmEffect` and `AgentRunner` deliberately: two copies of an
126
+ * accumulate-and-capture loop is two chances to differ about what "interrupted"
127
+ * means, and the whole point is that the partial survives identically either way.
128
+ */
129
+ export async function collectStream(
130
+ stream: AsyncIterable<LlmStreamChunk>,
131
+ options: CollectStreamOptions = {}
132
+ ): Promise<LlmResponse> {
133
+ // Only three facts about the stream survive the fold — the text, the terminal
134
+ // chunk's outcome, and the shape checks — so nothing retains the chunks
135
+ // themselves. A long generation would otherwise hold one small object per
136
+ // delta for its whole duration, times every concurrent stream.
137
+ let count = 0;
138
+ let terminals = 0;
139
+ let last: LlmStreamChunk | undefined;
140
+ // Running state, maintained per chunk rather than recomputed per chunk. The
141
+ // first cut rebuilt the text with `chunks.map(...).join("")` and rescanned for
142
+ // usage on EVERY delta, which is O(n²) over a generation: 4,000 deltas cost
143
+ // ~90ms of pure copying, and `streamTokenBudget`'s `checkEvery` could not help
144
+ // because the expensive part ran before the guard was ever consulted.
145
+ // Appending to a string is amortized O(1); tracking the last reported usage is
146
+ // O(1). The guard is now the only cost per delta.
147
+ let text = "";
148
+ let usage: LlmStreamChunk["usage"];
149
+ // A guard breach is NOT an interruption, and the two are easy to confuse: a
150
+ // guard that severs typically aborts the execution too, so by the time the
151
+ // catch below runs, `signal.aborted` is true and the budget error would be
152
+ // rewritten into "interrupted" — losing the reason the generation stopped.
153
+ // The caller has to be able to tell "a human stopped this" from "this cost too
154
+ // much", so the guard's own error is remembered and rethrown untouched.
155
+ let guardError: unknown;
156
+ try {
157
+ for await (const chunk of stream) {
158
+ count += 1;
159
+ last = chunk;
160
+ if (chunk.done) terminals += 1;
161
+ text += chunk.delta;
162
+ // Providers report usage late; keep the most recent one seen.
163
+ if (chunk.usage !== undefined) usage = chunk.usage;
164
+ // The guard runs BEFORE delivery: a delta that breaches it severs the stream
165
+ // and is never handed to a sink (ADR 0119 — a live tail must not see what the
166
+ // guard exists to stop).
167
+ if (options.guard !== undefined) {
168
+ try {
169
+ options.guard({
170
+ text,
171
+ deltas: count,
172
+ ...(usage !== undefined && { usage }),
173
+ });
174
+ } catch (err) {
175
+ guardError = err;
176
+ throw err;
177
+ }
178
+ }
179
+ // A provider that ignores the signal keeps yielding after an abort; checking
180
+ // here severs it at the next chunk instead of delivering text to a caller who
181
+ // has gone (the catch below turns it into the LLM_INTERRUPTED partial).
182
+ if (options.signal?.aborted === true) {
183
+ throw options.signal.reason instanceof Error
184
+ ? options.signal.reason
185
+ : new Error("generation aborted");
186
+ }
187
+ if (chunk.delta !== "") options.onDelta?.(chunk.delta);
188
+ if (chunk.toolDelta !== undefined) options.onToolDelta?.(chunk.toolDelta);
189
+ if (chunk.reasoningDelta !== undefined && chunk.reasoningDelta !== "") {
190
+ options.onReasoningDelta?.(chunk.reasoningDelta);
191
+ }
192
+ }
193
+ } catch (err) {
194
+ if (guardError !== undefined) throw guardError;
195
+ if (options.signal?.aborted === true) {
196
+ const partial: InterruptedGeneration = {
197
+ content: text,
198
+ ...(usage !== undefined && { usage }),
199
+ };
200
+ fail(LLM_INTERRUPTED, partial, "generation interrupted");
201
+ }
202
+ throw err;
203
+ }
204
+ // Same contract as `assembleStream`, checked against the counters rather than
205
+ // a retained array.
206
+ if (terminals === 0) throw new MalformedStreamError("no terminal chunk (none had done: true)");
207
+ if (terminals > 1) {
208
+ throw new MalformedStreamError(`${terminals} terminal chunks (exactly one is allowed)`);
209
+ }
210
+ if (last?.done !== true) throw new MalformedStreamError("the terminal chunk is not last");
211
+ return {
212
+ content: text,
213
+ ...(last.toolCalls !== undefined && { toolCalls: last.toolCalls }),
214
+ stopReason: last.stopReason ?? "end_turn",
215
+ ...(last.usage !== undefined && { usage: last.usage }),
216
+ ...(last.reasoning !== undefined && { reasoning: last.reasoning }),
217
+ ...(last.layout !== undefined && { layout: last.layout }),
218
+ };
219
+ }
@@ -0,0 +1,96 @@
1
+ import type { LlmContentPart, LlmMediaPart, LlmModality } from "./parts";
2
+ import type { LlmRequest } from "./types";
3
+
4
+ // ── R5 — what a provider accepts, declared, and refused before the wire ─────
5
+
6
+ /**
7
+ * The media types a provider accepts for one modality: a list of exact types
8
+ * (`image/png`), type wildcards (`image/*`), or `"*"` for any.
9
+ */
10
+ export type MediaTypeSet = readonly string[];
11
+
12
+ /** Text and tool calls are always supported; media is opt-in per modality. */
13
+ export interface LlmCapabilities {
14
+ image?: MediaTypeSet;
15
+ audio?: MediaTypeSet;
16
+ file?: MediaTypeSet;
17
+ /** ADR 0133 - the provider can return model reasoning. Drives the boot warning when no reasoning store is configured. */
18
+ reasoning?: boolean;
19
+ }
20
+
21
+ /** Thrown, before any network call, when a request carries a modality the provider did not declare. */
22
+ export class UnsupportedModalityError extends Error {
23
+ readonly modality: LlmModality;
24
+ readonly mediaType: string;
25
+ readonly provider: string | undefined;
26
+ constructor(modality: LlmModality, mediaType: string, provider?: string) {
27
+ super(
28
+ `${provider ?? "This LLM provider"} does not accept ${modality} content of type "${mediaType}" (not in its declared capabilities). The request was refused before any network call.`
29
+ );
30
+ this.name = "UnsupportedModalityError";
31
+ this.modality = modality;
32
+ this.mediaType = mediaType;
33
+ this.provider = provider;
34
+ }
35
+ }
36
+
37
+ export function mediaTypeAccepted(set: MediaTypeSet | undefined, mediaType: string): boolean {
38
+ if (set === undefined) return false;
39
+ const mt = mediaType.toLowerCase();
40
+ for (const allowed of set) {
41
+ if (allowed === "*" || allowed.toLowerCase() === mt) return true;
42
+ if (allowed.endsWith("/*") && mt.startsWith(allowed.slice(0, -1).toLowerCase())) return true;
43
+ }
44
+ return false;
45
+ }
46
+
47
+ function* mediaParts(parts: readonly LlmContentPart[]): Generator<LlmMediaPart> {
48
+ for (const p of parts) {
49
+ if (p.type === "image" || p.type === "audio" || p.type === "file") yield p;
50
+ else if (p.type === "tool_result" && typeof p.content !== "string") {
51
+ for (const r of p.content) if (r.type !== "text") yield r;
52
+ }
53
+ }
54
+ }
55
+
56
+ /** Every media part in a request, in order (including those inside `tool_result`s). */
57
+ export function* requestMediaParts(request: Pick<LlmRequest, "messages">): Generator<LlmMediaPart> {
58
+ for (const m of request.messages) {
59
+ if (typeof m.content !== "string") yield* mediaParts(m.content);
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Refuse a request that uses a modality / media type the provider has not declared.
65
+ * Pure and cheap (one walk); providers call it at the top of `complete` / `stream` and
66
+ * `createLlmEffect` calls it before resolving any content, so the refusal precedes
67
+ * every byte of I/O.
68
+ */
69
+ export function assertRequestSupported(
70
+ request: Pick<LlmRequest, "messages">,
71
+ capabilities: LlmCapabilities | undefined,
72
+ provider?: string
73
+ ): void {
74
+ for (const p of requestMediaParts(request)) {
75
+ if (!mediaTypeAccepted(capabilities?.[p.type], p.mediaType)) {
76
+ throw new UnsupportedModalityError(p.type, p.mediaType, provider);
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * The modalities a shipped provider declares for `model`: an ALLOWLIST. `base` only when the id
83
+ * matches a family known to take media (`allow`); every other id — unknown, older, a local alias —
84
+ * declares nothing, so a model that cannot take an image is refused locally instead of 400'd by the
85
+ * API, and a new family needs one line here before it is trusted with media. `config.capabilities`
86
+ * always wins (`capabilities: {}` = text-only). A provider on a non-default `baseUrl` (an
87
+ * OpenAI-compatible server) declares nothing either, since the model id says nothing about what is
88
+ * behind it; pass `capabilities` to turn media on.
89
+ */
90
+ export function capabilitiesForModel(
91
+ model: string,
92
+ base: LlmCapabilities,
93
+ allow: readonly RegExp[]
94
+ ): LlmCapabilities {
95
+ return allow.some((re) => re.test(model)) ? base : {};
96
+ }
@@ -0,0 +1,287 @@
1
+ import { type TaintedValue, tainted, unwrapTainted } from "@loomwork/classify";
2
+ import { LlmEffect } from "@loomwork/effect-runtime";
3
+ import type {
4
+ EffectContext,
5
+ EffectDelta,
6
+ EffectReasoningDefaults,
7
+ EffectReasoningHook,
8
+ } from "@loomwork/effect-runtime";
9
+ import { collectStream } from "./assembleStream";
10
+ import type { StreamProgress } from "./assembleStream";
11
+ import { assertRequestSupported } from "./capabilities";
12
+ import { type LlmReasoningOptions, captureReasoning, reasoningStoreResolver } from "./reasoning";
13
+ import { type ContentResolver, resolveRequestContent } from "./resolveContent";
14
+ import type { LlmCapturedResponse, LlmProvider, LlmRequest, LlmRequestInput } from "./types";
15
+ import { guardRequest } from "./untrusted";
16
+
17
+ export interface CreateLlmEffectOptions<TIn, TOut> {
18
+ provider: LlmProvider;
19
+ buildRequest: (input: TIn, ctx: EffectContext) => LlmRequestInput;
20
+ parseResponse?: (response: LlmCapturedResponse, input: TIn) => TOut;
21
+ /**
22
+ * ADR 0133 - model reasoning as audit evidence. Readable text goes to `store` (a sidecar, never
23
+ * the step value), `parseResponse` sees only recorded `reasoning` parts (kind, provider,
24
+ * signature, reference, digest), and one `LLM_REASONING_CAPTURED` event names references and
25
+ * digests. `capture` defaults to on iff a store is configured; `live` streams readable reasoning
26
+ * deltas through the governed per-call sink as `{ reasoning }` chunks (off by default).
27
+ * Unset, an app sees no change except that `response.reasoning` parts appear (signatures only)
28
+ * when a provider returns reasoning.
29
+ */
30
+ reasoning?: LlmReasoningOptions;
31
+ /**
32
+ * Consume the provider's `stream` instead of `complete` (R9). OPT-IN: the two
33
+ * paths return the same outcome by the R8 parity contract, but they are
34
+ * different wire calls, and an effect that silently changed which one it makes
35
+ * would be a surprise for anyone already running this.
36
+ *
37
+ * Falls back to `complete` when the provider implements no `stream`, so a
38
+ * provider swap cannot turn this into a runtime failure.
39
+ *
40
+ * What streaming buys, given the outcome is identical: deltas reach
41
+ * {@link onDelta} as they arrive, and an interrupted generation keeps its
42
+ * partial text ({@link LLM_INTERRUPTED}).
43
+ */
44
+ stream?: boolean;
45
+ /**
46
+ * R5 — fetch the bytes behind a `{ kind: "ref" }` media source, at call time. The
47
+ * journaled request carries only the address; this is the one place bytes appear, and
48
+ * only on a LIVE call (a replay is served from the log and never reaches it). Typically
49
+ * `(ref) => contentStore.get({ __loomContent: ref.address })`. Without it, a request
50
+ * with a ref is refused (`UnresolvedContentError`) before any network call.
51
+ */
52
+ resolveContent?: ContentResolver;
53
+ /** R5 — cap on one inline media part, decoded. Default 64 KiB (`MAX_INLINE_BYTES`). */
54
+ maxInlineBytes?: number;
55
+ /** R5 — cap on ALL inline media in one request. Default 256 KiB (`MAX_INLINE_TOTAL_BYTES`). */
56
+ maxInlineTotalBytes?: number;
57
+ /** R5 — cap on the total bytes `resolveContent` may return for one request. Default 32 MiB. */
58
+ maxResolvedBytes?: number;
59
+ /**
60
+ * R5 — a sink for streamed tool-call INPUT fragments (`id`, `name` on the first, then
61
+ * partial JSON). Transient and bare: it is NOT routed through the governed `ctx.onDelta`
62
+ * channel (ADR 0119), because middleware delta guards inspect text. Streaming only.
63
+ */
64
+ onToolDelta?: (
65
+ delta: { id: string; name?: string; inputDelta: string },
66
+ ctx: EffectContext
67
+ ) => void;
68
+ /**
69
+ * R5 — a retried attempt `attempt` (> 1) is about to stream: discard the fragments
70
+ * {@link onToolDelta} delivered so far (the same contract as {@link onReset} for text). Called
71
+ * once per retry, via the same runtime reset chunk as `onReset`. Like `onToolDelta`, it sits OUTSIDE the governed
72
+ * channel: no middleware delta guard or output validator sees tool-input fragments, and
73
+ * `streamTokenBudget` meters the text only.
74
+ */
75
+ onToolReset?: (attempt: number, ctx: EffectContext) => void;
76
+ /**
77
+ * Where deltas go. TRANSIENT by construction — this effect never journals them,
78
+ * and must not: chunks-as-events is the fold-economics problem ST-R rejected,
79
+ * and `ctx.stream` is durable PER DELTA (one permanent event each), so routing
80
+ * a token stream through it is a choice an author makes deliberately, at a
81
+ * granularity they pick.
82
+ *
83
+ * The natural sink is `progressHub` (or the app's own `onDelta` handler). `@loomwork/live-query`
84
+ * is NOT such a sink: it has no delta-push API — `subscribe` re-folds DURABLE events
85
+ * (`snapshot` + `EventStore.subscribe`), so there is nothing there to push a per-token
86
+ * delta into (2026-09-27 audit).
87
+ *
88
+ * Not called on replay: a completed effect is served from the log and never
89
+ * re-invoked, so a tail attached to a replay sees silence.
90
+ *
91
+ * ADR 0119 — under an `EffectRuntime` this sink is GOVERNED: it receives a delta only
92
+ * after every middleware delta guard accepted it (a PII/output validator severs the
93
+ * stream before the breaching delta), on durable and transient calls alike, and it
94
+ * receives NOTHING when a middleware `inspectsOutput` without a delta form (the call
95
+ * completes non-streamed). Used bare (no runtime) it is called directly.
96
+ *
97
+ * A per-call sink (`EffectContext.onDelta` — what `DecideScope.invokeTransient`
98
+ * `{ onDelta }` sets) is called IN ADDITION to this one, with `{ delta }` per delta and a
99
+ * final `{ delta: "", done: true, usage }`.
100
+ */
101
+ onDelta?: (delta: string, ctx: EffectContext) => void;
102
+ /**
103
+ * ADR 0119 — called when a retried attempt `attempt` (> 1) begins: discard what
104
+ * {@link onDelta} delivered so far. Same governed delivery as `onDelta`.
105
+ */
106
+ onReset?: (attempt: number, ctx: EffectContext) => void;
107
+ /**
108
+ * K8.4 — a mid-flight guard, called after every delta; THROW to sever the
109
+ * generation. Curried over the effect context so it can read `ctx.resources`
110
+ * and emit a durable fact before throwing.
111
+ *
112
+ * `@loomwork/middleware`'s `streamTokenBudget(budget)` is the budget-shaped one.
113
+ * Streaming-only by nature: `complete` produces nothing to observe until it is
114
+ * already finished, which is the whole reason K8.4 waited on streaming effects.
115
+ */
116
+ guard?: (ctx: EffectContext) => (progress: StreamProgress) => void;
117
+ }
118
+
119
+ function reasoningResolverOf(r: LlmReasoningOptions | undefined): ContentResolver | undefined {
120
+ return r?.resolve ?? (r?.store !== undefined ? reasoningStoreResolver(r.store) : undefined);
121
+ }
122
+
123
+ export function createLlmEffect<TIn = string, TOut = string>(
124
+ options: CreateLlmEffectOptions<TIn, TOut>
125
+ ): LlmEffect<TIn, TOut> {
126
+ const parse = options.parseResponse ?? ((r: LlmCapturedResponse) => r.content as unknown as TOut);
127
+ // ADR 0133 D7 - the app's defaults (`observability.reasoning`) are adopted at boot; the effect's own
128
+ // options win field by field. Resolved per call, so an adoption after construction takes effect.
129
+ let adopted: EffectReasoningDefaults | false | undefined;
130
+ const reasoningOf = (): LlmReasoningOptions | undefined =>
131
+ adopted === undefined && options.reasoning === undefined
132
+ ? undefined
133
+ : adopted === false
134
+ ? // The app-wide `false` is a default for effects with NO policy of their own: an effect that
135
+ // configured its own store or capture choice keeps it entirely.
136
+ options.reasoning?.store !== undefined || options.reasoning?.capture !== undefined
137
+ ? options.reasoning
138
+ : { capture: false, ...options.reasoning }
139
+ : { ...adopted, ...options.reasoning };
140
+ const reasoningHook: EffectReasoningHook = {
141
+ capable: options.provider.capabilities?.reasoning === true,
142
+ posture() {
143
+ const r = reasoningOf();
144
+ if (r?.capture === false) return "off";
145
+ return r?.store !== undefined ? "keeps" : "unconfigured";
146
+ },
147
+ adopt(defaults) {
148
+ adopted = defaults;
149
+ },
150
+ };
151
+ return new LlmEffect<TIn, TOut>(
152
+ async (input: TIn, ctx: EffectContext) => {
153
+ // The integrity check at the prompt boundary: tainted content is refused
154
+ // outside a `user` message and fenced inside one, and what comes back is
155
+ // plain strings — so no provider has to know taint exists.
156
+ const guarded = guardRequest(options.buildRequest(input, ctx), {
157
+ ...(options.maxInlineBytes !== undefined && { maxInlineBytes: options.maxInlineBytes }),
158
+ ...(options.maxInlineTotalBytes !== undefined && {
159
+ maxInlineTotalBytes: options.maxInlineTotalBytes,
160
+ }),
161
+ });
162
+ // R5 — fail closed on an undeclared modality BEFORE fetching any content or touching the
163
+ // network, then turn claim-check refs into bytes (live calls only).
164
+ assertRequestSupported(guarded, options.provider.capabilities);
165
+ const reasoningResolver = reasoningResolverOf(reasoningOf());
166
+ const request = await resolveRequestContent(guarded, options.resolveContent, {
167
+ ...(options.maxResolvedBytes !== undefined && { maxBytes: options.maxResolvedBytes }),
168
+ ...(ctx.signal !== undefined && { signal: ctx.signal }),
169
+ context: {
170
+ executionId: ctx.executionId,
171
+ workflowId: ctx.workflowId,
172
+ stepId: ctx.stepId,
173
+ ...(ctx.principal !== undefined && { principal: ctx.principal }),
174
+ },
175
+ // ADR 0133 D5 - reasoning text lives where capture put it, and is read back through an
176
+ // AUTHORIZING resolver (the ADR 0120 seam, origin "reasoning"): the configured one, else the
177
+ // sidecar read that serves a blob only to the execution it is sealed to.
178
+ ...(reasoningResolver !== undefined && { reasoningResolver }),
179
+ });
180
+ // K8.1 — forward the effect's cooperative-cancellation signal to the
181
+ // provider (unless buildRequest already set one), so a cancelled/timed-out
182
+ // execution severs the in-flight HTTP call instead of orphaning it.
183
+ if (request.signal === undefined && ctx.signal !== undefined) {
184
+ request.signal = ctx.signal;
185
+ }
186
+
187
+ // ONE durable outcome either way. The streamed path returns the assembled
188
+ // response, which the R8 conformance contract pins equal to what `complete`
189
+ // would have returned — so the journal shape does not depend on which path
190
+ // ran, and switching `stream` on cannot change what replay serves.
191
+ const reasoningSink = reasoningOf()?.onDelta;
192
+ const live =
193
+ reasoningOf()?.live === true && (ctx.onDelta !== undefined || reasoningSink !== undefined);
194
+ const raw = await (options.stream === true && options.provider.stream !== undefined
195
+ ? collectStream(options.provider.stream(request as LlmRequest), {
196
+ ...(ctx.signal !== undefined && { signal: ctx.signal }),
197
+ // ADR 0119 — the per-call sink (ctx.onDelta, set by a decider's scope) AND the
198
+ // configured one both fire; neither replaces the other. The per-call sink goes
199
+ // FIRST: it is the governed one, and a middleware delta guard severs the stream
200
+ // by THROWING from it — which must happen before any other tail sees the delta.
201
+ // Do not catch around it. The text is wrapped as untrusted content (ADR 0085).
202
+ // The configured sink is NOT called here under a runtime: ctx.onDelta is the
203
+ // guarded channel and the runtime forwards to `deltaSink` after the guards.
204
+ ...((options.onDelta !== undefined || ctx.onDelta !== undefined) && {
205
+ onDelta: (delta: string) => {
206
+ if (ctx.onDelta !== undefined) {
207
+ ctx.onDelta({ delta: tainted(delta, `llm:${ctx.stepId}`) });
208
+ } else options.onDelta?.(delta, ctx);
209
+ },
210
+ }),
211
+ // ADR 0133 D6 - readable reasoning rides its own field on the governed per-call sink
212
+ // (so the runtime's delta guards see it, and a guard that throws severs the stream).
213
+ // Off unless `live`; never the configured `onDelta`, which is text-only.
214
+ ...(live && {
215
+ onReasoningDelta: (delta: string) => {
216
+ const reasoning = tainted(delta, `llm-reasoning:${ctx.stepId}`);
217
+ if (ctx.onDelta !== undefined) {
218
+ ctx.onDelta({ delta: tainted("", `llm:${ctx.stepId}`), reasoning });
219
+ } else {
220
+ try {
221
+ reasoningSink?.(reasoning, ctx);
222
+ } catch {
223
+ // A live tail is a convenience: it cannot fail the model call (the durable record is D3).
224
+ }
225
+ }
226
+ },
227
+ }),
228
+ ...(options.onToolDelta !== undefined && {
229
+ onToolDelta: (d: { id: string; name?: string; inputDelta: string }) =>
230
+ options.onToolDelta?.(d, ctx),
231
+ }),
232
+ ...(options.guard !== undefined && { guard: options.guard(ctx) }),
233
+ })
234
+ : options.provider.complete(request));
235
+ // ADR 0119 — hand the runtime the stream's end + usage. The runtime HOLDS this and
236
+ // sends the caller's terminal chunk only once the whole call has succeeded
237
+ // (middleware output phase and parse included), so `done` never precedes a failure.
238
+ // Streamed path only: a `complete` fallback delivered no deltas to terminate.
239
+ if (options.stream === true && options.provider.stream !== undefined) {
240
+ ctx.onDelta?.({
241
+ delta: tainted("", `llm:${ctx.stepId}`),
242
+ done: true,
243
+ ...(raw.usage !== undefined && { usage: raw.usage }),
244
+ });
245
+ }
246
+
247
+ // ADR 0133 D3 - readable reasoning is reduced to its recorded form BEFORE parse, so text
248
+ // never reaches a step value. A response with no reasoning is passed through untouched (G2).
249
+ // The same object goes through when there is none (callers compare responses by identity).
250
+ let response: LlmCapturedResponse = raw as LlmCapturedResponse;
251
+ if (raw.reasoning !== undefined) {
252
+ const { reasoning: returned, ...rest } = raw;
253
+ response =
254
+ returned.length > 0
255
+ ? { ...rest, reasoning: await captureReasoning(returned, reasoningOf(), ctx) }
256
+ : rest;
257
+ }
258
+ return parse(response, input);
259
+ },
260
+ // Wire pre-warm to the provider's own warm hook (connection/model cache),
261
+ // only when the provider implements one.
262
+ {
263
+ ...(options.provider.warm !== undefined && { warm: () => options.provider.warm?.() }),
264
+ // ADR 0133 D6 - the configured reasoning sink is reached through the runtime's governed pipe
265
+ // (delta guards first), only when the app turned live reasoning on.
266
+ reasoning: reasoningHook,
267
+ ...(options.reasoning?.onDelta !== undefined && {
268
+ reasoningSink: (delta: TaintedValue<string>, ctx: EffectContext) => {
269
+ if (reasoningOf()?.live === true) options.reasoning?.onDelta?.(delta, ctx);
270
+ },
271
+ }),
272
+ ...((options.onDelta !== undefined ||
273
+ options.onReset !== undefined ||
274
+ options.onToolReset !== undefined) && {
275
+ deltaSink: (chunk: EffectDelta, ctx: EffectContext) => {
276
+ if (chunk.reset === true) {
277
+ options.onReset?.(chunk.attempt ?? 2, ctx);
278
+ options.onToolReset?.(chunk.attempt ?? 2, ctx);
279
+ } else if (chunk.done !== true && chunk.reasoning === undefined) {
280
+ // A reasoning chunk (ADR 0133) is not text: the configured sink is text-only.
281
+ options.onDelta?.(unwrapTainted(chunk.delta), ctx);
282
+ }
283
+ },
284
+ }),
285
+ }
286
+ );
287
+ }