@jaypie/llm 1.3.13 → 1.3.14

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.
@@ -0,0 +1,20 @@
1
+ import { LlmExchangeEnvelope } from "../types/LlmProvider.interface.js";
2
+ /** Minimal shape of the @jaypie/dynamodb surface this module uses. */
3
+ interface ExchangeStoreSdk {
4
+ storeExchange: (envelope: LlmExchangeEnvelope) => Promise<unknown>;
5
+ }
6
+ /** Reset the cached resolution. Exposed for tests. */
7
+ export declare function _resetExchangeStore(): void;
8
+ /**
9
+ * Inject a store to bypass @jaypie/dynamodb resolution. Test-only: the peer
10
+ * is optional, so the enabled path cannot otherwise be exercised in unit
11
+ * tests without it installed.
12
+ */
13
+ export declare function _setExchangeStore(sdk: ExchangeStoreSdk | null): void;
14
+ /**
15
+ * Persist an exchange envelope via @jaypie/dynamodb when
16
+ * LLM_EXCHANGE_ENABLED is set. Silent no-op when the flag is unset or the
17
+ * peer is absent; persister failures are logged and never thrown.
18
+ */
19
+ export declare function persistExchange(envelope: LlmExchangeEnvelope): Promise<void>;
20
+ export {};
@@ -0,0 +1,16 @@
1
+ import { LlmExchangeEnvelope, LlmHistory, LlmInputMessage, LlmOperateInput, LlmOperateOptions, LlmOperateResponse } from "../../types/LlmProvider.interface.js";
2
+ import { OperateLoopState } from "../types.js";
3
+ /**
4
+ * Assemble the serializable exchange envelope for one operate() settlement.
5
+ * The `resolution` block is stamped by the Llm facade, which is the layer
6
+ * that knows fallback outcome; the loop fills provider/model best-effort.
7
+ */
8
+ export declare function buildExchangeEnvelope({ duration, initialHistoryLength, input, options, response, startedAt, state, }: {
9
+ duration: number;
10
+ initialHistoryLength: number;
11
+ input: string | LlmHistory | LlmInputMessage | LlmOperateInput;
12
+ options: LlmOperateOptions;
13
+ response: LlmOperateResponse;
14
+ startedAt: string;
15
+ state: OperateLoopState;
16
+ }): LlmExchangeEnvelope;
@@ -0,0 +1,20 @@
1
+ import { LlmExchangeCallback, LlmExchangeEnvelope } from "../../types/LlmProvider.interface.js";
2
+ /**
3
+ * Truthy-except-"false"/"0" gate on LLM_EXCHANGE_ENABLED, matching the
4
+ * DD_LLMOBS_ENABLED convention in observability/llmobs.ts.
5
+ */
6
+ export declare function isExchangeStoreEnabled(): boolean;
7
+ /**
8
+ * Whether the loop should assemble an exchange envelope at settlement.
9
+ */
10
+ export declare function isExchangeRequested({ onExchange, }: {
11
+ onExchange?: LlmExchangeCallback;
12
+ }): boolean;
13
+ /**
14
+ * Deliver an exchange envelope to a callback. Errors thrown by the callback
15
+ * are logged and swallowed — exchange capture must never interrupt the call.
16
+ */
17
+ export declare function emitExchange({ envelope, onExchange, }: {
18
+ envelope: LlmExchangeEnvelope;
19
+ onExchange?: LlmExchangeCallback;
20
+ }): Promise<void>;
@@ -0,0 +1,2 @@
1
+ export { buildExchangeEnvelope } from "./buildExchangeEnvelope.js";
2
+ export { emitExchange, isExchangeRequested, isExchangeStoreEnabled, } from "./emitExchange.js";
@@ -150,6 +150,10 @@ export interface OperateLoopState {
150
150
  currentTurn: number;
151
151
  /** Formatted output schema for structured output */
152
152
  formattedFormat?: JsonObject;
153
+ /** Stop reason from the most recent model response */
154
+ lastStopReason?: string;
155
+ /** Model-request retries across all turns (exchange envelope) */
156
+ retries: number;
153
157
  /** Formatted tools for the provider */
154
158
  formattedTools?: ProviderToolDefinition[];
155
159
  /** Maximum allowed turns */
@@ -26,7 +26,7 @@ export declare enum LlmResponseStatus {
26
26
  Incomplete = "incomplete",
27
27
  InProgress = "in_progress"
28
28
  }
29
- interface LlmError {
29
+ export interface LlmError {
30
30
  detail?: string;
31
31
  status: number | string;
32
32
  title: string;
@@ -212,6 +212,81 @@ export interface LlmProgressEvent {
212
212
  usage?: LlmUsage;
213
213
  }
214
214
  export type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise<unknown>;
215
+ /**
216
+ * Serializable snapshot of the request side of one operate() call.
217
+ * `input` is the raw, pre-interpolation input with multimodal parts preserved.
218
+ */
219
+ export interface LlmExchangeRequest {
220
+ data?: NaturalMap;
221
+ effort?: LlmEffort;
222
+ explain?: boolean;
223
+ /** Format normalized to JSON Schema (Zod/Natural already rendered) */
224
+ format?: JsonObject;
225
+ input: string | LlmHistory | LlmInputMessage | LlmOperateInput;
226
+ instructions?: string;
227
+ /** Requested model (pre-fallback) */
228
+ model?: string;
229
+ placeholders?: {
230
+ input?: boolean;
231
+ instructions?: boolean;
232
+ system?: boolean;
233
+ };
234
+ providerOptions?: JsonObject;
235
+ system?: string;
236
+ temperature?: number;
237
+ /** Names of tools offered to the model */
238
+ tools?: string[];
239
+ turns?: boolean | number;
240
+ user?: string;
241
+ }
242
+ /**
243
+ * Serializable snapshot of the response side of one operate() call.
244
+ */
245
+ export interface LlmExchangeResponse {
246
+ content?: string | JsonObject;
247
+ error?: LlmError;
248
+ /** Turns this call added to history (tool calls/results included); never the resent history */
249
+ historyDelta: LlmHistory;
250
+ reasoning?: string[];
251
+ status: LlmResponseStatus;
252
+ stopReason?: string;
253
+ /** Per-model-call usage items (provider/model on each item) */
254
+ usage: LlmUsage;
255
+ /** Cumulative usage keyed `provider:model` */
256
+ usageTotals?: Record<string, LlmUsageItem>;
257
+ }
258
+ /**
259
+ * How the call was actually served after fallback and retry resolution.
260
+ */
261
+ export interface LlmExchangeResolution {
262
+ fallbackAttempts?: number;
263
+ fallbackUsed?: boolean;
264
+ /** Served model */
265
+ model?: string;
266
+ /** Served provider */
267
+ provider?: string;
268
+ /** Model-request retries within the served attempt */
269
+ retries?: number;
270
+ }
271
+ export interface LlmExchangeTiming {
272
+ /** Milliseconds from start to settlement */
273
+ duration: number;
274
+ /** ISO 8601 */
275
+ startedAt: string;
276
+ }
277
+ /**
278
+ * One serializable request/response envelope per operate() settlement
279
+ * (success or failure). Contains no functions.
280
+ */
281
+ export interface LlmExchangeEnvelope {
282
+ /** Provider response id(s) per model turn, when available */
283
+ ids?: string[];
284
+ request: LlmExchangeRequest;
285
+ resolution: LlmExchangeResolution;
286
+ response: LlmExchangeResponse;
287
+ timing: LlmExchangeTiming;
288
+ }
289
+ export type LlmExchangeCallback = (envelope: LlmExchangeEnvelope) => unknown | Promise<unknown>;
215
290
  export interface LlmOperateOptions {
216
291
  data?: NaturalMap;
217
292
  /**
@@ -276,6 +351,12 @@ export interface LlmOperateOptions {
276
351
  };
277
352
  instructions?: string;
278
353
  model?: string;
354
+ /**
355
+ * Fires once per operate() settlement (success or failure) with a fully
356
+ * serializable request/response envelope. Errors thrown by the callback
357
+ * are logged and never interrupt the call.
358
+ */
359
+ onExchange?: LlmExchangeCallback;
279
360
  /**
280
361
  * Receives lightweight progress events as the operate loop runs:
281
362
  * start, model_request, model_response, tool_call, tool_result,
@@ -322,6 +403,11 @@ export type LlmUsage = LlmUsageItem[];
322
403
  export interface LlmOperateResponse {
323
404
  content?: string | JsonObject;
324
405
  error?: LlmError;
406
+ /**
407
+ * Serializable exchange envelope for this call. Present only when
408
+ * `onExchange` was passed or exchange persistence is enabled.
409
+ */
410
+ exchange?: LlmExchangeEnvelope;
325
411
  /** Number of providers attempted (1 = primary only, >1 = fallback(s) used) */
326
412
  fallbackAttempts?: number;
327
413
  /** Whether a fallback provider was used instead of the primary */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.3.13",
3
+ "version": "1.3.14",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -55,12 +55,16 @@
55
55
  },
56
56
  "peerDependencies": {
57
57
  "@aws-sdk/client-bedrock-runtime": "^3.1045.0",
58
+ "@jaypie/dynamodb": "*",
58
59
  "@jaypie/kit": "*",
59
60
  "@jaypie/logger": "*"
60
61
  },
61
62
  "peerDependenciesMeta": {
62
63
  "@aws-sdk/client-bedrock-runtime": {
63
64
  "optional": true
65
+ },
66
+ "@jaypie/dynamodb": {
67
+ "optional": true
64
68
  }
65
69
  },
66
70
  "publishConfig": {