@jaypie/llm 1.3.11 → 1.3.13

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,152 @@
1
+ import { JsonObject, NaturalSchema } from "@jaypie/types";
2
+ import { z } from "zod/v4";
3
+ import { Toolkit } from "../../tools/Toolkit.class.js";
4
+ import { LlmHistory, LlmOperateOptions, LlmUsageItem } from "../../types/LlmProvider.interface.js";
5
+ import { LlmStreamChunk } from "../../types/LlmStreamChunk.interface.js";
6
+ import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult } from "../types.js";
7
+ import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
8
+ interface FireworksMessage {
9
+ role: "system" | "user" | "assistant" | "tool";
10
+ content?: string | FireworksContentPart[] | null;
11
+ toolCalls?: FireworksToolCall[];
12
+ toolCallId?: string;
13
+ }
14
+ interface FireworksToolCall {
15
+ id: string;
16
+ type: "function";
17
+ function: {
18
+ name: string;
19
+ arguments: string;
20
+ };
21
+ }
22
+ interface FireworksResponseMessage {
23
+ role: "assistant";
24
+ content?: string | null;
25
+ toolCalls?: FireworksToolCall[];
26
+ refusal?: string | null;
27
+ reasoning?: string | null;
28
+ reasoning_content?: string | null;
29
+ }
30
+ interface FireworksChoice {
31
+ index: number;
32
+ message: FireworksResponseMessage;
33
+ finishReason: string | null;
34
+ finish_reason?: string | null;
35
+ }
36
+ interface FireworksUsage {
37
+ promptTokens: number;
38
+ completionTokens: number;
39
+ totalTokens: number;
40
+ prompt_tokens?: number;
41
+ completion_tokens?: number;
42
+ total_tokens?: number;
43
+ completionTokensDetails?: {
44
+ reasoningTokens?: number;
45
+ };
46
+ }
47
+ interface FireworksResponse {
48
+ id: string;
49
+ object: string;
50
+ created: number;
51
+ model: string;
52
+ choices: FireworksChoice[];
53
+ usage?: FireworksUsage;
54
+ }
55
+ interface FireworksTool {
56
+ type: "function";
57
+ function: {
58
+ name: string;
59
+ description: string;
60
+ parameters: JsonObject;
61
+ };
62
+ }
63
+ interface FireworksJsonSchemaConfig {
64
+ name: string;
65
+ description?: string;
66
+ schema: JsonObject;
67
+ strict?: boolean;
68
+ }
69
+ type FireworksResponseFormat = {
70
+ type: "json_schema";
71
+ json_schema: FireworksJsonSchemaConfig;
72
+ } | {
73
+ type: "json_object";
74
+ } | {
75
+ type: "text";
76
+ };
77
+ interface FireworksRequest {
78
+ model: string;
79
+ messages: FireworksMessage[];
80
+ tools?: FireworksTool[];
81
+ tool_choice?: "auto" | "none" | "required";
82
+ response_format?: FireworksResponseFormat;
83
+ reasoning_effort?: string;
84
+ user?: string;
85
+ }
86
+ /**
87
+ * Fireworks content part types. Fireworks follows the OpenAI Chat Completions
88
+ * multimodal schema for `image_url` parts (URL or base64 data URI). There is
89
+ * no OpenAI-style `file` part, and the API rejects `data:` URIs for documents
90
+ * ("Unsupported URL scheme 'data'"), so file inputs cannot be delivered.
91
+ */
92
+ type FireworksContentPart = {
93
+ type: "text";
94
+ text: string;
95
+ } | {
96
+ type: "image_url";
97
+ imageUrl: {
98
+ url: string;
99
+ };
100
+ };
101
+ /**
102
+ * FireworksAdapter implements the ProviderAdapter interface for Fireworks AI's
103
+ * OpenAI-compatible Chat Completions API. It handles request building,
104
+ * response parsing, and error classification specific to Fireworks.
105
+ */
106
+ export declare class FireworksAdapter extends BaseProviderAdapter {
107
+ readonly name: "fireworks";
108
+ readonly defaultModel: string;
109
+ readonly supportsStructuredOutputRetry = true;
110
+ private runtimeNoStructuredOutputModels;
111
+ rememberModelRejectsStructuredOutput(model: string): void;
112
+ clearRuntimeNoStructuredOutputModels(): void;
113
+ private supportsStructuredOutput;
114
+ private runtimeNoTemperatureModels;
115
+ rememberModelRejectsTemperature(model: string): void;
116
+ clearRuntimeNoTemperatureModels(): void;
117
+ private supportsTemperature;
118
+ buildRequest(request: OperateRequest): FireworksRequest;
119
+ formatTools(toolkit: Toolkit, _outputSchema?: JsonObject): ProviderToolDefinition[];
120
+ formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
121
+ executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<FireworksResponse>;
122
+ /**
123
+ * Serialize the internal request into the OpenAI-compatible wire body for
124
+ * Fireworks' Chat Completions endpoint. Top-level fields (model, tools,
125
+ * tool_choice, response_format, reasoning_effort, user, temperature, and any
126
+ * providerOptions) are already wire-shaped (snake_case); only messages carry
127
+ * camelCase tool fields that must become snake_case on the wire.
128
+ */
129
+ private toWireBody;
130
+ /**
131
+ * Rebuild a structured-output request without `response_format`, swapping in
132
+ * the legacy fake-tool emulation. Used as a runtime fallback when a model
133
+ * rejects native json_schema.
134
+ */
135
+ private toFallbackStructuredOutputRequest;
136
+ executeStreamRequest(client: unknown, request: unknown, signal?: AbortSignal): AsyncIterable<LlmStreamChunk>;
137
+ parseResponse(response: unknown, _options?: LlmOperateOptions): ParsedResponse;
138
+ extractToolCalls(response: unknown): StandardToolCall[];
139
+ extractUsage(response: unknown, model: string): LlmUsageItem;
140
+ formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): FireworksMessage;
141
+ appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): FireworksRequest;
142
+ responseToHistoryItems(response: unknown): LlmHistory;
143
+ classifyError(error: unknown): ClassifiedError;
144
+ isComplete(response: unknown): boolean;
145
+ hasStructuredOutput(response: unknown): boolean;
146
+ extractStructuredOutput(response: unknown): JsonObject | undefined;
147
+ private hasToolCalls;
148
+ private extractContent;
149
+ private convertMessagesToFireworks;
150
+ }
151
+ export declare const fireworksAdapter: FireworksAdapter;
152
+ export {};
@@ -20,6 +20,12 @@ export interface ProviderAdapter {
20
20
  * Default model for this provider
21
21
  */
22
22
  readonly defaultModel: string;
23
+ /**
24
+ * Whether OperateLoop may take a corrective turn when a `format` request
25
+ * completes with prose instead of structured output (tool-emulation
26
+ * providers opt in).
27
+ */
28
+ readonly supportsStructuredOutputRetry?: boolean;
23
29
  /**
24
30
  * Build a provider-specific request from the standardized format
25
31
  *
@@ -169,6 +175,13 @@ export declare abstract class BaseProviderAdapter implements ProviderAdapter {
169
175
  abstract responseToHistoryItems(response: unknown): LlmHistory;
170
176
  abstract classifyError(error: unknown): ClassifiedError;
171
177
  abstract isComplete(response: unknown): boolean;
178
+ /**
179
+ * Whether OperateLoop may take a corrective turn when a `format` request
180
+ * completes with prose instead of structured output. Providers whose
181
+ * structured output rides a tool emulation (where compliance is a model
182
+ * decision) opt in; native grammar-constrained providers do not need it.
183
+ */
184
+ readonly supportsStructuredOutputRetry: boolean;
172
185
  /**
173
186
  * Default implementation checks if error is retryable via classifyError
174
187
  */
@@ -2,6 +2,7 @@ export { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
2
2
  export type { ProviderAdapter } from "./ProviderAdapter.interface.js";
3
3
  export { AnthropicAdapter, anthropicAdapter } from "./AnthropicAdapter.js";
4
4
  export { BedrockAdapter, bedrockAdapter } from "./BedrockAdapter.js";
5
+ export { FireworksAdapter, fireworksAdapter } from "./FireworksAdapter.js";
5
6
  export { GoogleAdapter, googleAdapter } from "./GoogleAdapter.js";
6
7
  export { OpenAiAdapter, openAiAdapter } from "./OpenAiAdapter.js";
7
8
  export { OpenRouterAdapter, openRouterAdapter } from "./OpenRouterAdapter.js";
@@ -1,4 +1,4 @@
1
- export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, BedrockAdapter, bedrockAdapter, GoogleAdapter, googleAdapter, OpenAiAdapter, openAiAdapter, OpenRouterAdapter, openRouterAdapter, XaiAdapter, xaiAdapter, } from "./adapters/index.js";
1
+ export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, BedrockAdapter, bedrockAdapter, FireworksAdapter, fireworksAdapter, GoogleAdapter, googleAdapter, OpenAiAdapter, openAiAdapter, OpenRouterAdapter, openRouterAdapter, XaiAdapter, xaiAdapter, } from "./adapters/index.js";
2
2
  export type { ProviderAdapter } from "./adapters/index.js";
3
3
  export { createOperateLoop, OperateLoop } from "./OperateLoop.js";
4
4
  export type { OperateLoopConfig } from "./OperateLoop.js";
@@ -77,6 +77,13 @@ export interface OperateRequest {
77
77
  stream?: boolean;
78
78
  /** Sampling temperature (0-2 for most providers) */
79
79
  temperature?: number;
80
+ /**
81
+ * Set by OperateLoop on a corrective turn after a format request came back
82
+ * as prose. Adapters that support the retry (see
83
+ * `ProviderAdapter.supportsStructuredOutputRetry`) should offer only the
84
+ * structured-output mechanism on this turn.
85
+ */
86
+ structuredOutputRetry?: boolean;
80
87
  /** User identifier for tracking */
81
88
  user?: string;
82
89
  }
@@ -135,6 +142,8 @@ import type { ResponseBuilder } from "./response/ResponseBuilder.js";
135
142
  export interface OperateLoopState {
136
143
  /** Count of consecutive tool errors (resets on success) */
137
144
  consecutiveToolErrors: number;
145
+ /** Set when the loop has taken a corrective structured-output turn */
146
+ structuredOutputRetry?: boolean;
138
147
  /** Current conversation input/messages */
139
148
  currentInput: LlmHistory;
140
149
  /** Current turn number (0-indexed, incremented at start of each turn) */
@@ -0,0 +1,21 @@
1
+ import { JsonObject } from "@jaypie/types";
2
+ import { LlmHistory, LlmInputMessage, LlmMessageOptions, LlmOperateOptions, LlmOperateResponse, LlmProvider } from "../../types/LlmProvider.interface.js";
3
+ import { LlmStreamChunk } from "../../types/LlmStreamChunk.interface.js";
4
+ export declare class FireworksProvider implements LlmProvider {
5
+ private model;
6
+ private _client?;
7
+ private _operateLoop?;
8
+ private _streamLoop?;
9
+ private apiKey?;
10
+ private log;
11
+ private conversationHistory;
12
+ constructor(model?: string, { apiKey }?: {
13
+ apiKey?: string;
14
+ });
15
+ private getClient;
16
+ private getOperateLoop;
17
+ private getStreamLoop;
18
+ send(message: string, options?: LlmMessageOptions): Promise<string | JsonObject>;
19
+ operate(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
20
+ stream(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
21
+ }
@@ -0,0 +1,37 @@
1
+ import { JsonObject } from "@jaypie/types";
2
+ export interface FireworksClientOptions {
3
+ apiKey: string;
4
+ baseURL?: string;
5
+ }
6
+ export interface ChatCompletionOptions {
7
+ signal?: AbortSignal;
8
+ }
9
+ /**
10
+ * HTTP error carrying the upstream status and parsed API message. The
11
+ * FireworksAdapter classifies errors by reading `.status` / `.statusCode` and
12
+ * `.message` / `.error.message`.
13
+ */
14
+ export declare class FireworksHttpError extends Error {
15
+ readonly status: number;
16
+ readonly statusCode: number;
17
+ readonly error?: {
18
+ message?: string;
19
+ };
20
+ constructor(status: number, message: string, error?: {
21
+ message?: string;
22
+ });
23
+ }
24
+ /**
25
+ * Minimal `fetch`-based client for Fireworks AI's OpenAI-compatible Chat
26
+ * Completions endpoint. The adapter only needs a single POST (streaming and
27
+ * non-streaming), header auth, and HTTP error surfacing.
28
+ */
29
+ export declare class FireworksClient {
30
+ private readonly apiKey;
31
+ private readonly baseURL;
32
+ constructor({ apiKey, baseURL, }: FireworksClientOptions);
33
+ private headers;
34
+ private toError;
35
+ chatCompletion(body: Record<string, unknown>, { signal }?: ChatCompletionOptions): Promise<JsonObject>;
36
+ streamChatCompletion(body: Record<string, unknown>, { signal }?: ChatCompletionOptions): AsyncIterable<JsonObject>;
37
+ }
@@ -0,0 +1,2 @@
1
+ export { FireworksProvider } from "./FireworksProvider.class.js";
2
+ export * from "./utils.js";
@@ -0,0 +1,15 @@
1
+ import { createLogger } from "@jaypie/logger";
2
+ import { LlmMessageOptions } from "../../types/LlmProvider.interface.js";
3
+ import { FireworksClient } from "./client.js";
4
+ export declare const getLogger: () => ReturnType<typeof createLogger>;
5
+ export declare function initializeClient({ apiKey, }?: {
6
+ apiKey?: string;
7
+ }): Promise<FireworksClient>;
8
+ export declare function getDefaultModel(): string;
9
+ export interface ChatMessage {
10
+ role: "system" | "user" | "assistant";
11
+ content: string;
12
+ }
13
+ export declare function formatSystemMessage(systemPrompt: string, { data, placeholders }?: Pick<LlmMessageOptions, "data" | "placeholders">): ChatMessage;
14
+ export declare function formatUserMessage(message: string, { data, placeholders }?: Pick<LlmMessageOptions, "data" | "placeholders">): ChatMessage;
15
+ export declare function prepareMessages(message: string, { system, data, placeholders }?: LlmMessageOptions): ChatMessage[];
@@ -39,4 +39,5 @@ export declare function toXaiEffort(effort: LlmEffort): LlmEffortMapping;
39
39
  export declare function toAnthropicEffort(effort: LlmEffort): LlmEffortMapping;
40
40
  export declare function toGeminiThinkingLevel(effort: LlmEffort): LlmEffortMapping;
41
41
  export declare function toGeminiThinkingBudget(effort: LlmEffort): LlmEffortMapping<number>;
42
+ export declare function toFireworksEffort(effort: LlmEffort): LlmEffortMapping;
42
43
  export declare function toOpenRouterEffort(effort: LlmEffort): LlmEffortMapping;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.3.11",
3
+ "version": "1.3.13",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",