Skip to content

Commit 2060913

Browse files
committed
fix: hoted workflow
1 parent dec29da commit 2060913

File tree

2 files changed

+10
-77
lines changed

2 files changed

+10
-77
lines changed

sim/lib/utils.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,34 +197,3 @@ export function formatDuration(durationMs: number): string {
197197
export function generateApiKey(): string {
198198
return `sim_${nanoid(32)}`
199199
}
200-
201-
/**
202-
* Rotates through available API keys for a provider
203-
* @param provider - The provider to get a key for (e.g., 'openai')
204-
* @returns The selected API key
205-
* @throws Error if no API keys are configured for rotation
206-
*/
207-
export function getRotatingApiKey(provider: string): string {
208-
if (provider !== 'openai') {
209-
throw new Error(`No rotation implemented for provider: ${provider}`)
210-
}
211-
212-
// Get all OpenAI keys from environment
213-
const keys = []
214-
215-
// Add keys if they exist in environment variables
216-
if (process.env.OPENAI_API_KEY_1) keys.push(process.env.OPENAI_API_KEY_1)
217-
if (process.env.OPENAI_API_KEY_2) keys.push(process.env.OPENAI_API_KEY_2)
218-
if (process.env.OPENAI_API_KEY_3) keys.push(process.env.OPENAI_API_KEY_3)
219-
220-
if (keys.length === 0) {
221-
throw new Error('No API keys configured for rotation. Please configure OPENAI_API_KEY_1, OPENAI_API_KEY_2, or OPENAI_API_KEY_3.')
222-
}
223-
224-
// Simple round-robin rotation based on current minute
225-
// This distributes load across keys and is stateless
226-
const currentMinute = new Date().getMinutes()
227-
const keyIndex = currentMinute % keys.length
228-
229-
return keys[keyIndex]
230-
}

sim/providers/openai/index.ts

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,10 @@
11
import OpenAI from 'openai'
22
import { createLogger } from '@/lib/logs/console-logger'
3-
import { getRotatingApiKey } from '@/lib/utils'
43
import { executeTool } from '@/tools'
54
import { ProviderConfig, ProviderRequest, ProviderResponse, TimeSegment } from '../types'
65

76
const logger = createLogger('OpenAI Provider')
87

9-
/**
10-
* Helper function to handle API key rotation for GPT-4o
11-
* @param apiKey - The original API key from the request
12-
* @param model - The model being used
13-
* @returns The API key to use (original or rotating)
14-
*/
15-
async function getApiKey(apiKey: string | undefined, model: string): Promise<string> {
16-
// Check if we should use a rotating key
17-
const isHostedVersion = process.env.NEXT_PUBLIC_APP_URL === 'https://www.simstudio.ai'
18-
const isGPT4o = model === 'gpt-4o'
19-
20-
// On hosted version, always use rotating key for GPT-4o models
21-
// This handles both null/undefined API keys and user-provided API keys
22-
if (isHostedVersion && isGPT4o) {
23-
try {
24-
// Use the rotating key mechanism
25-
return getRotatingApiKey('openai')
26-
} catch (error) {
27-
logger.warn('Failed to get rotating API key', {
28-
error: error instanceof Error ? error.message : String(error)
29-
})
30-
31-
// If we couldn't get a rotating key and have a user key, use it as fallback
32-
// This should only happen if rotation system is misconfigured
33-
if (apiKey) {
34-
logger.info('Falling back to user-provided API key')
35-
return apiKey
36-
}
37-
38-
// No rotating key and no user key - throw specific error
39-
throw new Error('No API key available for OpenAI. Please configure API key rotation or provide a valid API key.')
40-
}
41-
}
42-
43-
// For non-hosted versions or non-GPT4o models, require the provided key
44-
if (!apiKey) {
45-
throw new Error('API key is required for OpenAI')
46-
}
47-
48-
return apiKey
49-
}
50-
518
/**
529
* OpenAI provider configuration
5310
*/
@@ -69,11 +26,18 @@ export const openaiProvider: ProviderConfig = {
6926
hasResponseFormat: !!request.responseFormat,
7027
})
7128

72-
// Get the appropriate API key based on our rotation logic
73-
const apiKey = await getApiKey(request.apiKey, request.model || 'gpt-4o')
29+
if (!request.apiKey) {
30+
logger.error('OpenAI API key missing in request', {
31+
hasModel: !!request.model,
32+
hasSystemPrompt: !!request.systemPrompt,
33+
hasMessages: !!request.messages,
34+
hasTools: !!request.tools,
35+
})
36+
throw new Error('API key is required for OpenAI')
37+
}
7438

7539
const openai = new OpenAI({
76-
apiKey: apiKey,
40+
apiKey: request.apiKey,
7741
dangerouslyAllowBrowser: true,
7842
})
7943

0 commit comments

Comments
 (0)