@jaypie/llm 1.2.39 → 1.2.41

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,72 @@
1
+ /**
2
+ * LLM Observability span kinds supported by Datadog's `llmobs` SDK.
3
+ * @see https://docs.datadoghq.com/llm_observability/setup/sdk/nodejs/
4
+ */
5
+ export type LlmObsSpanKind = "agent" | "embedding" | "llm" | "retrieval" | "task" | "tool" | "workflow";
6
+ export interface LlmObsSpanOptions {
7
+ kind: LlmObsSpanKind;
8
+ modelName?: string;
9
+ modelProvider?: string;
10
+ name: string;
11
+ }
12
+ export interface LlmObsAnnotation {
13
+ inputData?: unknown;
14
+ metadata?: Record<string, unknown>;
15
+ metrics?: Record<string, number>;
16
+ outputData?: unknown;
17
+ tags?: Record<string, unknown>;
18
+ }
19
+ /** Manual span handle for streaming, where a callback cannot enclose the work. */
20
+ export interface LlmObsSpanHandle {
21
+ annotate: (annotation: LlmObsAnnotation) => void;
22
+ finish: () => void;
23
+ }
24
+ /** Minimal shape of the dd-trace `llmobs` SDK surface this module uses. */
25
+ interface LlmObsSdk {
26
+ annotate: (span: unknown, annotation: LlmObsAnnotation) => void;
27
+ trace: (options: LlmObsSpanOptions, fn: (span: unknown) => unknown) => unknown;
28
+ }
29
+ /** Reset the cached SDK resolution. Exposed for tests. */
30
+ export declare function _resetLlmObs(): void;
31
+ /**
32
+ * Inject an SDK to bypass dd-trace resolution. Test-only: dd-trace is not a
33
+ * dependency, so the enabled path cannot otherwise be exercised in unit tests.
34
+ */
35
+ export declare function _setLlmObs(sdk: LlmObsSdk | null): void;
36
+ /**
37
+ * True when LLM Observability emission is opted in via `DD_LLMOBS_ENABLED`.
38
+ * Accepts any truthy value except "false"/"0".
39
+ */
40
+ export declare function isLlmObsEnabled(): boolean;
41
+ /**
42
+ * Run `fn` inside an LLM Observability span. When emission is disabled or
43
+ * dd-trace is absent, `fn` is invoked directly with no overhead. The active
44
+ * span is established for the duration of `fn`, so nested `withLlmObsSpan`
45
+ * calls (and `annotateLlmObs`) attach as children/annotations automatically.
46
+ */
47
+ export declare function withLlmObsSpan<T>(options: LlmObsSpanOptions, fn: () => Promise<T>): Promise<T>;
48
+ /**
49
+ * Annotate the currently active LLM Observability span. No-op when disabled.
50
+ */
51
+ export declare function annotateLlmObs(annotation: LlmObsAnnotation): void;
52
+ /**
53
+ * Open a manual span that is finished later. Used by streaming, where work is
54
+ * yielded incrementally and cannot be enclosed in a single callback. The span
55
+ * is kept open via a deferred promise resolved by `finish()`. Returns null when
56
+ * emission is disabled or dd-trace is absent.
57
+ *
58
+ * Note: because the span is held open across `yield` boundaries (outside the
59
+ * SDK's AsyncLocalStorage scope), manual spans are emitted flat rather than
60
+ * nested. Annotations are applied to the explicit span reference.
61
+ */
62
+ export declare function openLlmObsSpan(options: LlmObsSpanOptions): LlmObsSpanHandle | null;
63
+ /**
64
+ * Sum an `LlmUsage` array into LLM Observability metric keys. Returns undefined
65
+ * when there is no usage data so callers can omit the `metrics` field.
66
+ */
67
+ export declare function usageToLlmObsMetrics(usage?: Array<{
68
+ input?: number;
69
+ output?: number;
70
+ total?: number;
71
+ }>): Record<string, number> | undefined;
72
+ export {};
@@ -34,6 +34,12 @@ export declare class OperateLoop {
34
34
  private createContext;
35
35
  private buildInitialRequest;
36
36
  private executeOneTurn;
37
+ /**
38
+ * Backfill declared array fields when a `format` is supplied. A declared
39
+ * `format` is a schema contract: an empty array field should surface as `[]`
40
+ * rather than be dropped by a provider/model that omits empty lists.
41
+ */
42
+ private applyFormatArrayDefaults;
37
43
  /**
38
44
  * Sync the current input state from the updated provider request.
39
45
  * This is necessary because appendToolResult modifies the provider-specific request,
@@ -0,0 +1,17 @@
1
+ import { z } from "zod/v4";
2
+ import { JsonObject, NaturalSchema } from "@jaypie/types";
3
+ type Format = JsonObject | NaturalSchema | z.ZodType;
4
+ /**
5
+ * Ensure every array field declared in `format` is present in `content` as an
6
+ * array. A declared `format` is a schema contract: an empty list should surface
7
+ * as `[]`, not be dropped from the response. Some providers/models omit empty
8
+ * array fields entirely, leaving consumers to read `.length` on `undefined`.
9
+ *
10
+ * Only mutates a (cloned) structured object; strings and non-objects pass
11
+ * through untouched.
12
+ */
13
+ export declare function fillFormatArrays({ content, format, }: {
14
+ content: JsonObject;
15
+ format: Format;
16
+ }): JsonObject;
17
+ export {};
@@ -1,5 +1,6 @@
1
1
  export * from "./determineModelProvider.js";
2
2
  export * from "./extractReasoning.js";
3
+ export * from "./fillFormatArrays.js";
3
4
  export * from "./formatOperateInput.js";
4
5
  export * from "./formatOperateMessage.js";
5
6
  export * from "./jsonSchemaToOpenApi3.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.2.39",
3
+ "version": "1.2.41",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",