@compilr-dev/agents 0.3.1 → 0.3.4
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/dist/agent.d.ts +107 -2
- package/dist/agent.js +151 -22
- package/dist/context/manager.d.ts +8 -0
- package/dist/context/manager.js +25 -2
- package/dist/errors.d.ts +20 -1
- package/dist/errors.js +44 -2
- package/dist/index.d.ts +16 -1
- package/dist/index.js +13 -1
- package/dist/messages/index.d.ts +12 -5
- package/dist/messages/index.js +53 -15
- package/dist/providers/claude.js +7 -0
- package/dist/providers/fireworks.d.ts +86 -0
- package/dist/providers/fireworks.js +123 -0
- package/dist/providers/gemini-native.d.ts +86 -0
- package/dist/providers/gemini-native.js +374 -0
- package/dist/providers/groq.d.ts +86 -0
- package/dist/providers/groq.js +123 -0
- package/dist/providers/index.d.ts +17 -2
- package/dist/providers/index.js +13 -2
- package/dist/providers/openai-compatible.js +12 -1
- package/dist/providers/openrouter.d.ts +95 -0
- package/dist/providers/openrouter.js +138 -0
- package/dist/providers/perplexity.d.ts +86 -0
- package/dist/providers/perplexity.js +123 -0
- package/dist/providers/together.d.ts +86 -0
- package/dist/providers/together.js +123 -0
- package/dist/providers/types.d.ts +19 -0
- package/dist/state/agent-state.d.ts +1 -0
- package/dist/state/agent-state.js +2 -0
- package/dist/state/serializer.js +20 -2
- package/dist/state/types.d.ts +5 -0
- package/dist/tools/builtin/ask-user-simple.js +1 -0
- package/dist/tools/builtin/ask-user.js +1 -0
- package/dist/tools/builtin/bash.js +123 -2
- package/dist/tools/builtin/shell-manager.d.ts +15 -0
- package/dist/tools/builtin/shell-manager.js +51 -0
- package/dist/tools/builtin/suggest.js +1 -0
- package/dist/tools/builtin/todo.js +2 -0
- package/dist/tools/define.d.ts +6 -0
- package/dist/tools/define.js +1 -0
- package/dist/tools/types.d.ts +19 -0
- package/dist/utils/index.d.ts +119 -4
- package/dist/utils/index.js +164 -13
- package/package.json +7 -1
package/dist/index.d.ts
CHANGED
|
@@ -15,14 +15,29 @@ export { OpenAIProvider, createOpenAIProvider } from './providers/index.js';
|
|
|
15
15
|
export type { OpenAIProviderConfig } from './providers/index.js';
|
|
16
16
|
export { GeminiProvider, createGeminiProvider } from './providers/index.js';
|
|
17
17
|
export type { GeminiProviderConfig } from './providers/index.js';
|
|
18
|
+
export { GeminiNativeProvider, createGeminiNativeProvider } from './providers/index.js';
|
|
19
|
+
export type { GeminiNativeProviderConfig } from './providers/index.js';
|
|
20
|
+
export { GeminiLegacyProvider, createGeminiLegacyProvider } from './providers/index.js';
|
|
21
|
+
export type { GeminiLegacyProviderConfig } from './providers/index.js';
|
|
18
22
|
export { OpenAICompatibleProvider } from './providers/index.js';
|
|
19
23
|
export type { OpenAICompatibleConfig } from './providers/index.js';
|
|
24
|
+
export { TogetherProvider, createTogetherProvider } from './providers/index.js';
|
|
25
|
+
export type { TogetherProviderConfig } from './providers/index.js';
|
|
26
|
+
export { GroqProvider, createGroqProvider } from './providers/index.js';
|
|
27
|
+
export type { GroqProviderConfig } from './providers/index.js';
|
|
28
|
+
export { FireworksProvider, createFireworksProvider } from './providers/index.js';
|
|
29
|
+
export type { FireworksProviderConfig } from './providers/index.js';
|
|
30
|
+
export { PerplexityProvider, createPerplexityProvider } from './providers/index.js';
|
|
31
|
+
export type { PerplexityProviderConfig } from './providers/index.js';
|
|
32
|
+
export { OpenRouterProvider, createOpenRouterProvider } from './providers/index.js';
|
|
33
|
+
export type { OpenRouterProviderConfig } from './providers/index.js';
|
|
20
34
|
export type { Tool, ToolHandler, ToolRegistry, ToolInputSchema, ToolExecutionResult, ToolRegistryOptions, DefineToolOptions, ReadFileInput, WriteFileInput, BashInput, BashResult, FifoDetectionResult, GrepInput, GlobInput, EditInput, TodoWriteInput, TodoReadInput, TodoItem, TodoStatus, TodoContextCleanupOptions, TaskInput, TaskResult, AgentTypeConfig, TaskToolOptions, ContextMode, ThoroughnessLevel, SubAgentEventInfo, SuggestInput, SuggestToolOptions, } from './tools/index.js';
|
|
21
35
|
export { defineTool, createSuccessResult, createErrorResult, wrapToolExecute, DefaultToolRegistry, createToolRegistry, } from './tools/index.js';
|
|
22
36
|
export { readFileTool, createReadFileTool, writeFileTool, createWriteFileTool, bashTool, createBashTool, execStream, detectFifoUsage, bashOutputTool, createBashOutputTool, killShellTool, createKillShellTool, ShellManager, getDefaultShellManager, setDefaultShellManager, grepTool, createGrepTool, globTool, createGlobTool, editTool, createEditTool, todoWriteTool, todoReadTool, createTodoTools, TodoStore, resetDefaultTodoStore, getDefaultTodoStore, createIsolatedTodoStore, cleanupTodoContextMessages, getTodoContextStats, webFetchTool, createWebFetchTool, createTaskTool, defaultAgentTypes, suggestTool, createSuggestTool, builtinTools, allBuiltinTools, TOOL_NAMES, TOOL_SETS, } from './tools/index.js';
|
|
23
37
|
export { userMessage, assistantMessage, systemMessage, textBlock, toolUseBlock, toolResultBlock, getTextContent, getToolUses, getToolResults, hasToolUses, validateToolUseResultPairing, repairToolPairing, ensureMessageContent, normalizeMessages, } from './messages/index.js';
|
|
24
38
|
export type { ToolPairingValidation } from './messages/index.js';
|
|
25
|
-
export { generateId, sleep, retry, truncate } from './utils/index.js';
|
|
39
|
+
export { generateId, sleep, retry, truncate, withRetryGenerator, calculateBackoffDelay, DEFAULT_RETRY_CONFIG, } from './utils/index.js';
|
|
40
|
+
export type { RetryConfig as LLMRetryConfig, WithRetryOptions } from './utils/index.js';
|
|
26
41
|
export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError, ValidationError, MaxIterationsError, AbortError, ContextOverflowError, isAgentError, isProviderError, isToolError, isToolTimeoutError, isToolLoopError, isContextOverflowError, wrapError, } from './errors.js';
|
|
27
42
|
export { ContextManager, DEFAULT_CONTEXT_CONFIG, FileAccessTracker, createFileTrackingHook, TRACKED_TOOLS, } from './context/index.js';
|
|
28
43
|
export type { ContextManagerOptions, ContextCategory, BudgetAllocation, CategoryBudgetInfo, PreflightResult, VerbosityLevel, VerbosityConfig, ContextConfig, FilteringConfig, CompactionConfig, SummarizationConfig, CompactionResult, SummarizationResult, FilteringResult, ContextEvent, ContextEventHandler, ContextStats, FileAccessType, FileAccess, FileAccessTrackerOptions, FormatHintsOptions, FileAccessStats, } from './context/index.js';
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,17 @@ export { ClaudeProvider, createClaudeProvider } from './providers/index.js';
|
|
|
11
11
|
export { OllamaProvider, createOllamaProvider } from './providers/index.js';
|
|
12
12
|
export { OpenAIProvider, createOpenAIProvider } from './providers/index.js';
|
|
13
13
|
export { GeminiProvider, createGeminiProvider } from './providers/index.js';
|
|
14
|
+
// Also export native Gemini provider explicitly for clarity
|
|
15
|
+
export { GeminiNativeProvider, createGeminiNativeProvider } from './providers/index.js';
|
|
16
|
+
// Legacy Gemini provider (OpenAI-compatible endpoint)
|
|
17
|
+
export { GeminiLegacyProvider, createGeminiLegacyProvider } from './providers/index.js';
|
|
14
18
|
export { OpenAICompatibleProvider } from './providers/index.js';
|
|
19
|
+
// "Others" providers - OpenAI-compatible cloud APIs
|
|
20
|
+
export { TogetherProvider, createTogetherProvider } from './providers/index.js';
|
|
21
|
+
export { GroqProvider, createGroqProvider } from './providers/index.js';
|
|
22
|
+
export { FireworksProvider, createFireworksProvider } from './providers/index.js';
|
|
23
|
+
export { PerplexityProvider, createPerplexityProvider } from './providers/index.js';
|
|
24
|
+
export { OpenRouterProvider, createOpenRouterProvider } from './providers/index.js';
|
|
15
25
|
// Tool utilities
|
|
16
26
|
export { defineTool, createSuccessResult, createErrorResult, wrapToolExecute, DefaultToolRegistry, createToolRegistry, } from './tools/index.js';
|
|
17
27
|
// Built-in tools
|
|
@@ -25,7 +35,9 @@ TOOL_NAMES, TOOL_SETS, } from './tools/index.js';
|
|
|
25
35
|
// Message utilities
|
|
26
36
|
export { userMessage, assistantMessage, systemMessage, textBlock, toolUseBlock, toolResultBlock, getTextContent, getToolUses, getToolResults, hasToolUses, validateToolUseResultPairing, repairToolPairing, ensureMessageContent, normalizeMessages, } from './messages/index.js';
|
|
27
37
|
// Utilities
|
|
28
|
-
export { generateId, sleep,
|
|
38
|
+
export { generateId, sleep,
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated -- Kept for backward compatibility
|
|
40
|
+
retry, truncate, withRetryGenerator, calculateBackoffDelay, DEFAULT_RETRY_CONFIG, } from './utils/index.js';
|
|
29
41
|
// Errors
|
|
30
42
|
export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError, ValidationError, MaxIterationsError, AbortError, ContextOverflowError, isAgentError, isProviderError, isToolError, isToolTimeoutError, isToolLoopError, isContextOverflowError, wrapError, } from './errors.js';
|
|
31
43
|
// Context management
|
package/dist/messages/index.d.ts
CHANGED
|
@@ -83,13 +83,20 @@ export declare function normalizeMessages(messages: Message[]): Message[];
|
|
|
83
83
|
/**
|
|
84
84
|
* Repair tool use/result pairing issues in a message array.
|
|
85
85
|
*
|
|
86
|
-
* This function
|
|
87
|
-
*
|
|
86
|
+
* This function handles BOTH types of orphans with POSITIONAL requirements:
|
|
87
|
+
* 1. Orphaned tool_result blocks (those without a matching tool_use BEFORE them)
|
|
88
|
+
* - Causes: "function_response.name: Name cannot be empty" in Gemini
|
|
89
|
+
* 2. Orphaned tool_use blocks (those without tool_result in IMMEDIATELY NEXT message)
|
|
90
|
+
* - Causes: "tool_use ids were found without tool_result blocks" in Claude
|
|
88
91
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
92
|
+
* CRITICAL: Claude API requires tool_result to be in the IMMEDIATELY NEXT message
|
|
93
|
+
* after the assistant message containing tool_use. This function validates that
|
|
94
|
+
* positional requirement, not just existence anywhere in the array.
|
|
95
|
+
*
|
|
96
|
+
* This is particularly useful after context compaction/summarization, which may
|
|
97
|
+
* remove messages and break tool_use/tool_result pairing.
|
|
91
98
|
*
|
|
92
99
|
* @param messages - Array of messages to repair
|
|
93
|
-
* @returns New array with orphaned
|
|
100
|
+
* @returns New array with orphaned tool_use and tool_result blocks removed
|
|
94
101
|
*/
|
|
95
102
|
export declare function repairToolPairing(messages: Message[]): Message[];
|
package/dist/messages/index.js
CHANGED
|
@@ -156,39 +156,77 @@ export function normalizeMessages(messages) {
|
|
|
156
156
|
/**
|
|
157
157
|
* Repair tool use/result pairing issues in a message array.
|
|
158
158
|
*
|
|
159
|
-
* This function
|
|
160
|
-
*
|
|
159
|
+
* This function handles BOTH types of orphans with POSITIONAL requirements:
|
|
160
|
+
* 1. Orphaned tool_result blocks (those without a matching tool_use BEFORE them)
|
|
161
|
+
* - Causes: "function_response.name: Name cannot be empty" in Gemini
|
|
162
|
+
* 2. Orphaned tool_use blocks (those without tool_result in IMMEDIATELY NEXT message)
|
|
163
|
+
* - Causes: "tool_use ids were found without tool_result blocks" in Claude
|
|
161
164
|
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
165
|
+
* CRITICAL: Claude API requires tool_result to be in the IMMEDIATELY NEXT message
|
|
166
|
+
* after the assistant message containing tool_use. This function validates that
|
|
167
|
+
* positional requirement, not just existence anywhere in the array.
|
|
168
|
+
*
|
|
169
|
+
* This is particularly useful after context compaction/summarization, which may
|
|
170
|
+
* remove messages and break tool_use/tool_result pairing.
|
|
164
171
|
*
|
|
165
172
|
* @param messages - Array of messages to repair
|
|
166
|
-
* @returns New array with orphaned
|
|
173
|
+
* @returns New array with orphaned tool_use and tool_result blocks removed
|
|
167
174
|
*/
|
|
168
175
|
export function repairToolPairing(messages) {
|
|
169
|
-
// First
|
|
170
|
-
|
|
171
|
-
|
|
176
|
+
// First pass: collect all tool_use IDs that appear BEFORE their tool_result
|
|
177
|
+
// Also track which tool_use IDs have their result in the IMMEDIATELY NEXT message
|
|
178
|
+
const validToolUseIds = new Set();
|
|
179
|
+
const allToolUseIds = new Set();
|
|
180
|
+
for (let i = 0; i < messages.length; i++) {
|
|
181
|
+
const message = messages[i];
|
|
172
182
|
if (typeof message.content === 'string')
|
|
173
183
|
continue;
|
|
174
|
-
for
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
// Check for tool_use blocks in assistant messages
|
|
185
|
+
if (message.role === 'assistant') {
|
|
186
|
+
const toolUseIdsInThisMessage = [];
|
|
187
|
+
for (const block of message.content) {
|
|
188
|
+
if (block.type === 'tool_use') {
|
|
189
|
+
toolUseIdsInThisMessage.push(block.id);
|
|
190
|
+
allToolUseIds.add(block.id);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Check if the NEXT message contains tool_result for ALL these tool_use IDs
|
|
194
|
+
if (toolUseIdsInThisMessage.length > 0) {
|
|
195
|
+
// Next message may not exist if we're at the end of the array
|
|
196
|
+
const nextMessage = messages[i + 1];
|
|
197
|
+
if (nextMessage && typeof nextMessage.content !== 'string') {
|
|
198
|
+
const toolResultIdsInNext = new Set();
|
|
199
|
+
for (const block of nextMessage.content) {
|
|
200
|
+
if (block.type === 'tool_result') {
|
|
201
|
+
toolResultIdsInNext.add(block.toolUseId);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Mark tool_use IDs as valid only if their result is in the next message
|
|
205
|
+
for (const id of toolUseIdsInThisMessage) {
|
|
206
|
+
if (toolResultIdsInNext.has(id)) {
|
|
207
|
+
validToolUseIds.add(id);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
177
211
|
}
|
|
178
212
|
}
|
|
179
213
|
}
|
|
180
|
-
//
|
|
214
|
+
// Second pass: filter out orphaned blocks
|
|
181
215
|
const repairedMessages = [];
|
|
182
216
|
for (const message of messages) {
|
|
183
217
|
if (typeof message.content === 'string') {
|
|
184
218
|
repairedMessages.push(message);
|
|
185
219
|
continue;
|
|
186
220
|
}
|
|
187
|
-
// Filter content blocks to remove orphaned
|
|
221
|
+
// Filter content blocks to remove orphaned tool_use and tool_result
|
|
188
222
|
const filteredBlocks = message.content.filter((block) => {
|
|
189
223
|
if (block.type === 'tool_result') {
|
|
190
|
-
// Only keep if there's a matching tool_use
|
|
191
|
-
return
|
|
224
|
+
// Only keep if there's a valid matching tool_use (that also has its result properly placed)
|
|
225
|
+
return validToolUseIds.has(block.toolUseId);
|
|
226
|
+
}
|
|
227
|
+
if (block.type === 'tool_use') {
|
|
228
|
+
// Only keep if this tool_use has its result in the immediately next message
|
|
229
|
+
return validToolUseIds.has(block.id);
|
|
192
230
|
}
|
|
193
231
|
return true;
|
|
194
232
|
});
|
package/dist/providers/claude.js
CHANGED
|
@@ -43,6 +43,12 @@ export class ClaudeProvider {
|
|
|
43
43
|
const { systemPrompt, anthropicMessages } = this.convertMessages(messages);
|
|
44
44
|
const tools = this.convertTools(options?.tools);
|
|
45
45
|
const thinking = this.convertThinking(options?.thinking);
|
|
46
|
+
// Calculate payload sizes for debugging (same as gemini-native.ts)
|
|
47
|
+
const debugPayload = {
|
|
48
|
+
systemChars: systemPrompt.length,
|
|
49
|
+
contentsChars: JSON.stringify(anthropicMessages).length,
|
|
50
|
+
toolsChars: JSON.stringify(tools).length,
|
|
51
|
+
};
|
|
46
52
|
try {
|
|
47
53
|
// Build request parameters
|
|
48
54
|
const params = {
|
|
@@ -99,6 +105,7 @@ export class ClaudeProvider {
|
|
|
99
105
|
outputTokens: usage.output_tokens,
|
|
100
106
|
cacheReadTokens: usageWithCache.cache_read_input_tokens,
|
|
101
107
|
cacheCreationTokens: usageWithCache.cache_creation_input_tokens,
|
|
108
|
+
debugPayload,
|
|
102
109
|
},
|
|
103
110
|
};
|
|
104
111
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fireworks AI LLM Provider
|
|
3
|
+
*
|
|
4
|
+
* Implements LLMProvider interface for Fireworks AI models.
|
|
5
|
+
* Extends OpenAICompatibleProvider for shared functionality.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const provider = createFireworksProvider({
|
|
10
|
+
* model: 'accounts/fireworks/models/llama-v3p1-70b-instruct',
|
|
11
|
+
* apiKey: process.env.FIREWORKS_API_KEY
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* - Requires valid Fireworks AI API key
|
|
17
|
+
* - Default model is accounts/fireworks/models/llama-v3p1-8b-instruct
|
|
18
|
+
* - Supports Llama, Mixtral, and fine-tuned models
|
|
19
|
+
*/
|
|
20
|
+
import type { ChatOptions } from './types.js';
|
|
21
|
+
import { ProviderError } from '../errors.js';
|
|
22
|
+
import { OpenAICompatibleProvider } from './openai-compatible.js';
|
|
23
|
+
/**
|
|
24
|
+
* Configuration for FireworksProvider
|
|
25
|
+
*/
|
|
26
|
+
export interface FireworksProviderConfig {
|
|
27
|
+
/** Fireworks AI API key (falls back to FIREWORKS_API_KEY env var) */
|
|
28
|
+
apiKey?: string;
|
|
29
|
+
/** Base URL for Fireworks API (default: https://api.fireworks.ai/inference) */
|
|
30
|
+
baseUrl?: string;
|
|
31
|
+
/** Default model to use (default: accounts/fireworks/models/llama-v3p1-8b-instruct) */
|
|
32
|
+
model?: string;
|
|
33
|
+
/** Default max tokens (default: 4096) */
|
|
34
|
+
maxTokens?: number;
|
|
35
|
+
/** Request timeout in milliseconds (default: 120000) */
|
|
36
|
+
timeout?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Fireworks AI LLM Provider
|
|
40
|
+
*
|
|
41
|
+
* Provides streaming chat completion using Fireworks AI.
|
|
42
|
+
* Supports Llama, Mixtral, and custom fine-tuned models.
|
|
43
|
+
*/
|
|
44
|
+
export declare class FireworksProvider extends OpenAICompatibleProvider {
|
|
45
|
+
readonly name = "fireworks";
|
|
46
|
+
private readonly apiKey;
|
|
47
|
+
constructor(config?: FireworksProviderConfig);
|
|
48
|
+
/**
|
|
49
|
+
* Fireworks AI authentication with Bearer token
|
|
50
|
+
*/
|
|
51
|
+
protected getAuthHeaders(): Record<string, string>;
|
|
52
|
+
/**
|
|
53
|
+
* Fireworks AI chat completions endpoint (OpenAI-compatible)
|
|
54
|
+
*/
|
|
55
|
+
protected getEndpointPath(): string;
|
|
56
|
+
/**
|
|
57
|
+
* Fireworks AI uses standard OpenAI body format
|
|
58
|
+
*/
|
|
59
|
+
protected buildProviderSpecificBody(_options?: ChatOptions): Record<string, unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* Map HTTP errors with Fireworks AI-specific messages
|
|
62
|
+
*/
|
|
63
|
+
protected mapHttpError(status: number, body: string, _model: string): ProviderError;
|
|
64
|
+
/**
|
|
65
|
+
* Map connection errors with Fireworks AI-specific messages
|
|
66
|
+
*/
|
|
67
|
+
protected mapConnectionError(_error: Error): ProviderError;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create a Fireworks AI provider instance
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Using environment variable (FIREWORKS_API_KEY)
|
|
75
|
+
* const provider = createFireworksProvider();
|
|
76
|
+
*
|
|
77
|
+
* // With explicit API key
|
|
78
|
+
* const provider = createFireworksProvider({ apiKey: 'fw_...' });
|
|
79
|
+
*
|
|
80
|
+
* // With custom model
|
|
81
|
+
* const provider = createFireworksProvider({
|
|
82
|
+
* model: 'accounts/fireworks/models/llama-v3p1-70b-instruct'
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function createFireworksProvider(config?: FireworksProviderConfig): FireworksProvider;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fireworks AI LLM Provider
|
|
3
|
+
*
|
|
4
|
+
* Implements LLMProvider interface for Fireworks AI models.
|
|
5
|
+
* Extends OpenAICompatibleProvider for shared functionality.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const provider = createFireworksProvider({
|
|
10
|
+
* model: 'accounts/fireworks/models/llama-v3p1-70b-instruct',
|
|
11
|
+
* apiKey: process.env.FIREWORKS_API_KEY
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* - Requires valid Fireworks AI API key
|
|
17
|
+
* - Default model is accounts/fireworks/models/llama-v3p1-8b-instruct
|
|
18
|
+
* - Supports Llama, Mixtral, and fine-tuned models
|
|
19
|
+
*/
|
|
20
|
+
import { ProviderError } from '../errors.js';
|
|
21
|
+
import { OpenAICompatibleProvider } from './openai-compatible.js';
|
|
22
|
+
// Default configuration
|
|
23
|
+
const DEFAULT_MODEL = 'accounts/fireworks/models/llama-v3p1-8b-instruct';
|
|
24
|
+
const DEFAULT_BASE_URL = 'https://api.fireworks.ai/inference';
|
|
25
|
+
/**
|
|
26
|
+
* Fireworks AI LLM Provider
|
|
27
|
+
*
|
|
28
|
+
* Provides streaming chat completion using Fireworks AI.
|
|
29
|
+
* Supports Llama, Mixtral, and custom fine-tuned models.
|
|
30
|
+
*/
|
|
31
|
+
export class FireworksProvider extends OpenAICompatibleProvider {
|
|
32
|
+
name = 'fireworks';
|
|
33
|
+
apiKey;
|
|
34
|
+
constructor(config = {}) {
|
|
35
|
+
const apiKey = config.apiKey ?? process.env.FIREWORKS_API_KEY;
|
|
36
|
+
if (!apiKey) {
|
|
37
|
+
throw new ProviderError('Fireworks AI API key not found. Set FIREWORKS_API_KEY environment variable or pass apiKey in config.', 'fireworks');
|
|
38
|
+
}
|
|
39
|
+
const baseConfig = {
|
|
40
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
41
|
+
model: config.model ?? DEFAULT_MODEL,
|
|
42
|
+
maxTokens: config.maxTokens,
|
|
43
|
+
timeout: config.timeout,
|
|
44
|
+
};
|
|
45
|
+
super(baseConfig);
|
|
46
|
+
this.apiKey = apiKey;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Fireworks AI authentication with Bearer token
|
|
50
|
+
*/
|
|
51
|
+
getAuthHeaders() {
|
|
52
|
+
return {
|
|
53
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Fireworks AI chat completions endpoint (OpenAI-compatible)
|
|
58
|
+
*/
|
|
59
|
+
getEndpointPath() {
|
|
60
|
+
return '/v1/chat/completions';
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Fireworks AI uses standard OpenAI body format
|
|
64
|
+
*/
|
|
65
|
+
buildProviderSpecificBody(_options) {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Map HTTP errors with Fireworks AI-specific messages
|
|
70
|
+
*/
|
|
71
|
+
mapHttpError(status, body, _model) {
|
|
72
|
+
let message = `Fireworks AI error (${String(status)})`;
|
|
73
|
+
try {
|
|
74
|
+
const parsed = JSON.parse(body);
|
|
75
|
+
if (parsed.error?.message) {
|
|
76
|
+
message = parsed.error.message;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
message = body || message;
|
|
81
|
+
}
|
|
82
|
+
switch (status) {
|
|
83
|
+
case 401:
|
|
84
|
+
return new ProviderError('Invalid Fireworks AI API key. Check your FIREWORKS_API_KEY.', 'fireworks', 401);
|
|
85
|
+
case 403:
|
|
86
|
+
return new ProviderError('Access denied. Check your Fireworks AI API key permissions.', 'fireworks', 403);
|
|
87
|
+
case 429:
|
|
88
|
+
return new ProviderError('Fireworks AI rate limit exceeded. Please wait and try again.', 'fireworks', 429);
|
|
89
|
+
case 500:
|
|
90
|
+
case 502:
|
|
91
|
+
case 503:
|
|
92
|
+
return new ProviderError('Fireworks AI service temporarily unavailable. Please try again later.', 'fireworks', status);
|
|
93
|
+
default:
|
|
94
|
+
return new ProviderError(message, 'fireworks', status);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Map connection errors with Fireworks AI-specific messages
|
|
99
|
+
*/
|
|
100
|
+
mapConnectionError(_error) {
|
|
101
|
+
return new ProviderError('Failed to connect to Fireworks AI API. Check your internet connection.', 'fireworks');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a Fireworks AI provider instance
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* // Using environment variable (FIREWORKS_API_KEY)
|
|
110
|
+
* const provider = createFireworksProvider();
|
|
111
|
+
*
|
|
112
|
+
* // With explicit API key
|
|
113
|
+
* const provider = createFireworksProvider({ apiKey: 'fw_...' });
|
|
114
|
+
*
|
|
115
|
+
* // With custom model
|
|
116
|
+
* const provider = createFireworksProvider({
|
|
117
|
+
* model: 'accounts/fireworks/models/llama-v3p1-70b-instruct'
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function createFireworksProvider(config = {}) {
|
|
122
|
+
return new FireworksProvider(config);
|
|
123
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GeminiNativeProvider - Native Google Gen AI SDK implementation
|
|
3
|
+
*
|
|
4
|
+
* Uses the native @google/genai SDK instead of OpenAI-compatible endpoint.
|
|
5
|
+
* This enables proper support for Gemini 3 models with thought signatures.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const provider = new GeminiNativeProvider({
|
|
10
|
+
* apiKey: process.env.GOOGLE_AI_API_KEY,
|
|
11
|
+
* });
|
|
12
|
+
*
|
|
13
|
+
* const agent = new Agent({ provider });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import type { LLMProvider, Message, ChatOptions, StreamChunk } from './types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for GeminiNativeProvider
|
|
19
|
+
*/
|
|
20
|
+
export interface GeminiNativeProviderConfig {
|
|
21
|
+
/**
|
|
22
|
+
* Google AI API key
|
|
23
|
+
*/
|
|
24
|
+
apiKey: string;
|
|
25
|
+
/**
|
|
26
|
+
* Default model to use
|
|
27
|
+
* @default 'gemini-2.5-flash'
|
|
28
|
+
*/
|
|
29
|
+
model?: string;
|
|
30
|
+
/**
|
|
31
|
+
* Default max tokens
|
|
32
|
+
* @default 4096
|
|
33
|
+
*/
|
|
34
|
+
maxTokens?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* GeminiNativeProvider implements LLMProvider using the native Google Gen AI SDK
|
|
38
|
+
*
|
|
39
|
+
* Benefits over OpenAI-compatible endpoint:
|
|
40
|
+
* - Automatic thought signature handling for Gemini 3
|
|
41
|
+
* - Native streaming support
|
|
42
|
+
* - Full access to Gemini-specific features
|
|
43
|
+
*/
|
|
44
|
+
export declare class GeminiNativeProvider implements LLMProvider {
|
|
45
|
+
readonly name = "gemini";
|
|
46
|
+
private readonly client;
|
|
47
|
+
private readonly defaultModel;
|
|
48
|
+
private readonly defaultMaxTokens;
|
|
49
|
+
constructor(config: GeminiNativeProviderConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Send messages and stream the response
|
|
52
|
+
*/
|
|
53
|
+
chat(messages: Message[], options?: ChatOptions): AsyncIterable<StreamChunk>;
|
|
54
|
+
/**
|
|
55
|
+
* Count tokens in messages (approximation)
|
|
56
|
+
*/
|
|
57
|
+
countTokens(messages: Message[]): Promise<number>;
|
|
58
|
+
/**
|
|
59
|
+
* Convert our Message format to Google's format
|
|
60
|
+
*/
|
|
61
|
+
private convertMessages;
|
|
62
|
+
/**
|
|
63
|
+
* Convert content to Google's Part format
|
|
64
|
+
*/
|
|
65
|
+
private convertContent;
|
|
66
|
+
/**
|
|
67
|
+
* Extract tool name from tool_use block that matches the given toolUseId
|
|
68
|
+
*/
|
|
69
|
+
private extractToolName;
|
|
70
|
+
/**
|
|
71
|
+
* Convert our ToolDefinition to Google's FunctionDeclaration format
|
|
72
|
+
*/
|
|
73
|
+
private convertTools;
|
|
74
|
+
/**
|
|
75
|
+
* Process a streaming chunk into StreamChunks
|
|
76
|
+
*/
|
|
77
|
+
private processChunk;
|
|
78
|
+
/**
|
|
79
|
+
* Map errors to ProviderError
|
|
80
|
+
*/
|
|
81
|
+
private mapError;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create a GeminiNativeProvider with API key from environment
|
|
85
|
+
*/
|
|
86
|
+
export declare function createGeminiNativeProvider(config?: Partial<GeminiNativeProviderConfig>): GeminiNativeProvider;
|