chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,351 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const {
|
||||
mockCreate,
|
||||
mockExecuteTool,
|
||||
mockSupportsNative,
|
||||
mockPrepareTools,
|
||||
mockCheckForced,
|
||||
mockCreateStream,
|
||||
} = vi.hoisted(() => ({
|
||||
mockCreate: vi.fn(),
|
||||
mockExecuteTool: vi.fn(),
|
||||
mockSupportsNative: vi.fn(),
|
||||
mockPrepareTools: vi.fn((tools: unknown) => ({
|
||||
tools,
|
||||
toolChoice: 'auto',
|
||||
forcedTools: [],
|
||||
hasFilteredTools: false,
|
||||
})),
|
||||
mockCheckForced: vi.fn(() => ({ hasUsedForcedTool: false, usedForcedTools: [] })),
|
||||
mockCreateStream: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('openai', () => ({
|
||||
default: vi.fn().mockImplementation(
|
||||
class {
|
||||
chat = { completions: { create: mockCreate } }
|
||||
}
|
||||
),
|
||||
}))
|
||||
|
||||
vi.mock('@/providers', () => ({ MAX_TOOL_ITERATIONS: 10 }))
|
||||
|
||||
vi.mock('@/tools', () => ({ executeTool: mockExecuteTool }))
|
||||
|
||||
vi.mock('@/providers/models', () => ({
|
||||
getProviderFileAttachment: vi
|
||||
.fn()
|
||||
.mockReturnValue({ maxBytes: 10 * 1024 * 1024, strategy: 'inline' }),
|
||||
INLINE_ATTACHMENT_MAX_BYTES: 10 * 1024 * 1024,
|
||||
getProviderModels: vi.fn().mockReturnValue([]),
|
||||
getProviderDefaultModel: vi.fn().mockReturnValue(''),
|
||||
}))
|
||||
|
||||
vi.mock('@/providers/attachments', () => ({
|
||||
formatMessagesForProvider: vi.fn((messages: unknown) => messages),
|
||||
}))
|
||||
|
||||
vi.mock('@/providers/openrouter/utils', () => ({
|
||||
supportsNativeStructuredOutputs: mockSupportsNative,
|
||||
createReadableStreamFromOpenAIStream: mockCreateStream,
|
||||
checkForForcedToolUsage: mockCheckForced,
|
||||
}))
|
||||
|
||||
vi.mock('@/providers/trace-enrichment', () => ({
|
||||
enrichLastModelSegmentFromChatCompletions: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/providers/utils', () => ({
|
||||
calculateCost: vi.fn(() => ({ input: 0, output: 0, total: 0 })),
|
||||
prepareToolsWithUsageControl: mockPrepareTools,
|
||||
prepareToolExecution: vi.fn((_tool: unknown, toolArgs: Record<string, unknown>) => ({
|
||||
toolParams: toolArgs,
|
||||
executionParams: toolArgs,
|
||||
})),
|
||||
sumToolCosts: vi.fn(() => 0),
|
||||
generateSchemaInstructions: vi.fn(() => 'SCHEMA_INSTRUCTIONS'),
|
||||
}))
|
||||
|
||||
import { openRouterProvider } from '@/providers/openrouter/index'
|
||||
import type { ProviderRequest, ProviderResponse, ProviderToolConfig } from '@/providers/types'
|
||||
|
||||
interface Usage {
|
||||
prompt_tokens: number
|
||||
completion_tokens: number
|
||||
total_tokens: number
|
||||
}
|
||||
|
||||
function textResponse(
|
||||
content: string,
|
||||
usage: Usage = { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }
|
||||
) {
|
||||
return {
|
||||
choices: [{ message: { content, tool_calls: undefined }, finish_reason: 'stop' }],
|
||||
usage,
|
||||
}
|
||||
}
|
||||
|
||||
function toolCallResponse(name: string, args: Record<string, unknown>, id = 'call_1') {
|
||||
return {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{ id, type: 'function', function: { name, arguments: JSON.stringify(args) } },
|
||||
],
|
||||
},
|
||||
finish_reason: 'tool_calls',
|
||||
},
|
||||
],
|
||||
usage: { prompt_tokens: 8, completion_tokens: 4, total_tokens: 12 },
|
||||
}
|
||||
}
|
||||
|
||||
function tool(id: string): ProviderToolConfig {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
description: 'test tool',
|
||||
params: {},
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
}
|
||||
}
|
||||
|
||||
const baseRequest: ProviderRequest = {
|
||||
apiKey: 'sk-or-test',
|
||||
model: 'openrouter/anthropic/claude-3.5-sonnet',
|
||||
systemPrompt: 'You are helpful.',
|
||||
messages: [{ role: 'user', content: 'Hello' }],
|
||||
}
|
||||
|
||||
describe('openRouterProvider.executeRequest', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockCreate.mockReset()
|
||||
mockExecuteTool.mockReset()
|
||||
mockSupportsNative.mockResolvedValue(false)
|
||||
})
|
||||
|
||||
it('requires an API key', async () => {
|
||||
await expect(
|
||||
openRouterProvider.executeRequest({ model: 'openrouter/x', messages: [] })
|
||||
).rejects.toThrow('API key is required for OpenRouter')
|
||||
})
|
||||
|
||||
it('strips the openrouter/ prefix and returns content + tokens', async () => {
|
||||
mockCreate.mockResolvedValueOnce(textResponse('Hi there'))
|
||||
|
||||
const res = (await openRouterProvider.executeRequest(baseRequest)) as ProviderResponse
|
||||
|
||||
expect(res.content).toBe('Hi there')
|
||||
expect(res.model).toBe('anthropic/claude-3.5-sonnet')
|
||||
expect(res.tokens).toEqual({ input: 10, output: 5, total: 15 })
|
||||
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.model).toBe('anthropic/claude-3.5-sonnet')
|
||||
expect(payload.messages[0]).toEqual({ role: 'system', content: 'You are helpful.' })
|
||||
expect(payload.messages.at(-1)).toEqual({ role: 'user', content: 'Hello' })
|
||||
})
|
||||
|
||||
it('inserts context as a user message between system and history', async () => {
|
||||
mockCreate.mockResolvedValueOnce(textResponse('ok'))
|
||||
|
||||
await openRouterProvider.executeRequest({ ...baseRequest, context: 'CTX' })
|
||||
|
||||
const { messages } = mockCreate.mock.calls[0][0]
|
||||
expect(messages[0]).toEqual({ role: 'system', content: 'You are helpful.' })
|
||||
expect(messages[1]).toEqual({ role: 'user', content: 'CTX' })
|
||||
expect(messages[2]).toEqual({ role: 'user', content: 'Hello' })
|
||||
})
|
||||
|
||||
it('forwards maxTokens as max_tokens and temperature', async () => {
|
||||
mockCreate.mockResolvedValueOnce(textResponse('ok'))
|
||||
|
||||
await openRouterProvider.executeRequest({ ...baseRequest, maxTokens: 256, temperature: 0.4 })
|
||||
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.max_tokens).toBe(256)
|
||||
expect(payload.temperature).toBe(0.4)
|
||||
})
|
||||
|
||||
it('runs the tool loop: executes the tool, echoes tool_calls, returns the tool result, sums tokens', async () => {
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(toolCallResponse('get_weather', { city: 'SF' }))
|
||||
.mockResolvedValueOnce(
|
||||
textResponse('It is sunny', { prompt_tokens: 20, completion_tokens: 6, total_tokens: 26 })
|
||||
)
|
||||
mockExecuteTool.mockResolvedValueOnce({ success: true, output: { temp: 70 } })
|
||||
|
||||
const res = (await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [tool('get_weather')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(mockExecuteTool).toHaveBeenCalledWith('get_weather', { city: 'SF' }, expect.anything())
|
||||
expect(res.content).toBe('It is sunny')
|
||||
expect(res.toolCalls?.[0]).toMatchObject({
|
||||
name: 'get_weather',
|
||||
result: { temp: 70 },
|
||||
success: true,
|
||||
})
|
||||
expect(res.toolResults).toEqual([{ temp: 70 }])
|
||||
expect(res.tokens).toEqual({ input: 28, output: 10, total: 38 })
|
||||
|
||||
const secondMessages = mockCreate.mock.calls[1][0].messages
|
||||
const assistant = secondMessages.find((m: { role: string }) => m.role === 'assistant')
|
||||
expect(assistant).toMatchObject({
|
||||
content: null,
|
||||
tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'get_weather' } }],
|
||||
})
|
||||
const toolMsg = secondMessages.find((m: { role: string }) => m.role === 'tool')
|
||||
expect(toolMsg).toEqual({
|
||||
role: 'tool',
|
||||
tool_call_id: 'call_1',
|
||||
content: JSON.stringify({ temp: 70 }),
|
||||
})
|
||||
})
|
||||
|
||||
it('reports a failed tool result as an error payload to the model', async () => {
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(toolCallResponse('get_weather', { city: 'SF' }))
|
||||
.mockResolvedValueOnce(textResponse('done'))
|
||||
mockExecuteTool.mockResolvedValueOnce({ success: false, output: undefined, error: 'boom' })
|
||||
|
||||
const res = (await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [tool('get_weather')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(res.toolResults).toBeUndefined()
|
||||
expect(res.toolCalls?.[0]).toMatchObject({ success: false })
|
||||
const toolMsg = mockCreate.mock.calls[1][0].messages.find(
|
||||
(m: { role: string }) => m.role === 'tool'
|
||||
)
|
||||
expect(JSON.parse(toolMsg.content)).toEqual({
|
||||
error: true,
|
||||
message: 'boom',
|
||||
tool: 'get_weather',
|
||||
})
|
||||
})
|
||||
|
||||
it('applies native structured outputs (json_schema + require_parameters) when no tools are active', async () => {
|
||||
mockSupportsNative.mockResolvedValue(true)
|
||||
mockCreate.mockResolvedValueOnce(textResponse('{"x":1}'))
|
||||
|
||||
await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
responseFormat: {
|
||||
name: 'out',
|
||||
schema: { type: 'object', properties: { x: { type: 'number' } } },
|
||||
strict: true,
|
||||
},
|
||||
})
|
||||
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.response_format).toMatchObject({
|
||||
type: 'json_schema',
|
||||
json_schema: { name: 'out', strict: true },
|
||||
})
|
||||
expect(payload.provider).toMatchObject({ require_parameters: true })
|
||||
})
|
||||
|
||||
it('falls back to json_object + prompt instructions when native structured outputs are unsupported', async () => {
|
||||
mockSupportsNative.mockResolvedValue(false)
|
||||
mockCreate.mockResolvedValueOnce(textResponse('{"x":1}'))
|
||||
|
||||
await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
responseFormat: { name: 'out', schema: { type: 'object' } },
|
||||
})
|
||||
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.response_format).toEqual({ type: 'json_object' })
|
||||
expect(payload.messages.at(-1)).toEqual({ role: 'user', content: 'SCHEMA_INSTRUCTIONS' })
|
||||
})
|
||||
|
||||
it('defers response_format until after the tool loop when tools are active', async () => {
|
||||
mockSupportsNative.mockResolvedValue(true)
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(textResponse('interim'))
|
||||
.mockResolvedValueOnce(textResponse('{"x":1}'))
|
||||
|
||||
const res = (await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [tool('get_weather')],
|
||||
responseFormat: { name: 'out', schema: { type: 'object' }, strict: true },
|
||||
})) as ProviderResponse
|
||||
|
||||
const toolCall = mockCreate.mock.calls[0][0]
|
||||
expect(toolCall.tools).toBeDefined()
|
||||
expect(toolCall.response_format).toBeUndefined()
|
||||
|
||||
const finalCall = mockCreate.mock.calls[1][0]
|
||||
expect(finalCall.response_format).toMatchObject({ type: 'json_schema' })
|
||||
expect(finalCall.tools).toBeUndefined()
|
||||
expect(finalCall.tool_choice).toBeUndefined()
|
||||
expect(res.content).toBe('{"x":1}')
|
||||
})
|
||||
|
||||
it('forces the next tool after a forced tool is used', async () => {
|
||||
mockPrepareTools.mockReturnValueOnce({
|
||||
tools: [tool('a')],
|
||||
toolChoice: { type: 'function', function: { name: 'a' } },
|
||||
forcedTools: ['a', 'b'],
|
||||
hasFilteredTools: false,
|
||||
})
|
||||
mockCheckForced.mockReturnValueOnce({ hasUsedForcedTool: true, usedForcedTools: ['a'] })
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(toolCallResponse('a', {}))
|
||||
.mockResolvedValueOnce(textResponse('done'))
|
||||
mockExecuteTool.mockResolvedValueOnce({ success: true, output: {} })
|
||||
|
||||
await openRouterProvider.executeRequest({ ...baseRequest, tools: [tool('a'), tool('b')] })
|
||||
|
||||
expect(mockCreate.mock.calls[0][0].tool_choice).toEqual({
|
||||
type: 'function',
|
||||
function: { name: 'a' },
|
||||
})
|
||||
expect(mockCreate.mock.calls[1][0].tool_choice).toEqual({
|
||||
type: 'function',
|
||||
function: { name: 'b' },
|
||||
})
|
||||
})
|
||||
|
||||
it('streams directly when there are no tools and sends usage opt-in', async () => {
|
||||
mockCreate.mockResolvedValueOnce({})
|
||||
|
||||
const res = await openRouterProvider.executeRequest({ ...baseRequest, stream: true })
|
||||
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.stream).toBe(true)
|
||||
expect(payload.stream_options).toEqual({ include_usage: true })
|
||||
expect(mockCreateStream).toHaveBeenCalledTimes(1)
|
||||
expect(res).toHaveProperty('stream')
|
||||
expect(res).toHaveProperty('execution.output.model', 'anthropic/claude-3.5-sonnet')
|
||||
})
|
||||
|
||||
it('stops the tool loop at MAX_TOOL_ITERATIONS', async () => {
|
||||
mockCreate.mockResolvedValue(toolCallResponse('looping', {}))
|
||||
mockExecuteTool.mockResolvedValue({ success: true, output: {} })
|
||||
|
||||
const res = (await openRouterProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [tool('looping')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(mockCreate).toHaveBeenCalledTimes(11)
|
||||
expect(mockExecuteTool).toHaveBeenCalledTimes(10)
|
||||
expect(res.toolCalls?.length).toBe(10)
|
||||
})
|
||||
|
||||
it('wraps SDK errors in a ProviderError', async () => {
|
||||
mockCreate.mockRejectedValueOnce(new Error('rate limited'))
|
||||
|
||||
await expect(openRouterProvider.executeRequest(baseRequest)).rejects.toThrow('rate limited')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,601 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { getErrorMessage, toError } from '@sim/utils/errors'
|
||||
import OpenAI from 'openai'
|
||||
import type { ChatCompletionCreateParamsStreaming } from 'openai/resources/chat/completions'
|
||||
import type { StreamingExecution } from '@/executor/types'
|
||||
import { MAX_TOOL_ITERATIONS } from '@/providers'
|
||||
import { formatMessagesForProvider } from '@/providers/attachments'
|
||||
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
|
||||
import {
|
||||
checkForForcedToolUsage,
|
||||
createReadableStreamFromOpenAIStream,
|
||||
supportsNativeStructuredOutputs,
|
||||
} from '@/providers/openrouter/utils'
|
||||
import { createStreamingExecution } from '@/providers/streaming-execution'
|
||||
import { adaptOpenAIChatToolSchema } from '@/providers/tool-schema-adapter'
|
||||
import { enrichLastModelSegmentFromChatCompletions } from '@/providers/trace-enrichment'
|
||||
import type {
|
||||
FunctionCallResponse,
|
||||
Message,
|
||||
ProviderConfig,
|
||||
ProviderRequest,
|
||||
ProviderResponse,
|
||||
TimeSegment,
|
||||
} from '@/providers/types'
|
||||
import { ProviderError } from '@/providers/types'
|
||||
import {
|
||||
calculateCost,
|
||||
generateSchemaInstructions,
|
||||
prepareToolExecution,
|
||||
prepareToolsWithUsageControl,
|
||||
sumToolCosts,
|
||||
} from '@/providers/utils'
|
||||
import { executeTool } from '@/tools'
|
||||
|
||||
const logger = createLogger('OpenRouterProvider')
|
||||
|
||||
/**
|
||||
* Applies structured output configuration to a payload based on model capabilities.
|
||||
* Uses json_schema with require_parameters for supported models, falls back to json_object with prompt instructions.
|
||||
*/
|
||||
async function applyResponseFormat(
|
||||
targetPayload: any,
|
||||
messages: any[],
|
||||
responseFormat: any,
|
||||
model: string
|
||||
): Promise<any[]> {
|
||||
const useNative = await supportsNativeStructuredOutputs(model)
|
||||
|
||||
if (useNative) {
|
||||
logger.info('Using native structured outputs for OpenRouter model', { model })
|
||||
targetPayload.response_format = {
|
||||
type: 'json_schema',
|
||||
json_schema: {
|
||||
name: responseFormat.name || 'response_schema',
|
||||
schema: responseFormat.schema || responseFormat,
|
||||
strict: responseFormat.strict !== false,
|
||||
},
|
||||
}
|
||||
targetPayload.provider = { ...targetPayload.provider, require_parameters: true }
|
||||
return messages
|
||||
}
|
||||
|
||||
logger.info('Using json_object mode with prompt instructions for OpenRouter model', { model })
|
||||
const schema = responseFormat.schema || responseFormat
|
||||
const schemaInstructions = generateSchemaInstructions(schema, responseFormat.name)
|
||||
targetPayload.response_format = { type: 'json_object' }
|
||||
return [...messages, { role: 'user', content: schemaInstructions }]
|
||||
}
|
||||
|
||||
export const openRouterProvider: ProviderConfig = {
|
||||
id: 'openrouter',
|
||||
name: 'OpenRouter',
|
||||
description: 'Unified access to many models via OpenRouter',
|
||||
version: '1.0.0',
|
||||
models: getProviderModels('openrouter'),
|
||||
defaultModel: getProviderDefaultModel('openrouter'),
|
||||
|
||||
executeRequest: async (
|
||||
request: ProviderRequest
|
||||
): Promise<ProviderResponse | StreamingExecution> => {
|
||||
if (!request.apiKey) {
|
||||
throw new Error('API key is required for OpenRouter')
|
||||
}
|
||||
|
||||
const client = new OpenAI({
|
||||
apiKey: request.apiKey,
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
})
|
||||
|
||||
const requestedModel = request.model.replace(/^openrouter\//, '')
|
||||
|
||||
logger.info('Preparing OpenRouter request', {
|
||||
model: requestedModel,
|
||||
hasSystemPrompt: !!request.systemPrompt,
|
||||
hasMessages: !!request.messages?.length,
|
||||
hasTools: !!request.tools?.length,
|
||||
toolCount: request.tools?.length || 0,
|
||||
hasResponseFormat: !!request.responseFormat,
|
||||
stream: !!request.stream,
|
||||
})
|
||||
|
||||
const allMessages: Message[] = []
|
||||
|
||||
if (request.systemPrompt) {
|
||||
allMessages.push({ role: 'system', content: request.systemPrompt })
|
||||
}
|
||||
|
||||
if (request.context) {
|
||||
allMessages.push({ role: 'user', content: request.context })
|
||||
}
|
||||
|
||||
if (request.messages) {
|
||||
allMessages.push(...request.messages)
|
||||
}
|
||||
const formattedMessages = formatMessagesForProvider(allMessages, 'openrouter') as Message[]
|
||||
|
||||
const tools = request.tools?.length
|
||||
? request.tools.map((tool) => adaptOpenAIChatToolSchema(tool))
|
||||
: undefined
|
||||
|
||||
const payload: any = {
|
||||
model: requestedModel,
|
||||
messages: formattedMessages,
|
||||
}
|
||||
|
||||
if (request.temperature !== undefined) payload.temperature = request.temperature
|
||||
if (request.maxTokens != null) payload.max_tokens = request.maxTokens
|
||||
|
||||
let preparedTools: ReturnType<typeof prepareToolsWithUsageControl> | null = null
|
||||
let hasActiveTools = false
|
||||
if (tools?.length) {
|
||||
preparedTools = prepareToolsWithUsageControl(tools, request.tools, logger, 'openrouter')
|
||||
const { tools: filteredTools, toolChoice } = preparedTools
|
||||
if (filteredTools?.length && toolChoice) {
|
||||
payload.tools = filteredTools
|
||||
payload.tool_choice = toolChoice
|
||||
hasActiveTools = true
|
||||
}
|
||||
}
|
||||
|
||||
const providerStartTime = Date.now()
|
||||
const providerStartTimeISO = new Date(providerStartTime).toISOString()
|
||||
|
||||
try {
|
||||
if (request.responseFormat && !hasActiveTools) {
|
||||
payload.messages = await applyResponseFormat(
|
||||
payload,
|
||||
payload.messages,
|
||||
request.responseFormat,
|
||||
requestedModel
|
||||
)
|
||||
}
|
||||
|
||||
if (request.stream && (!tools || tools.length === 0 || !hasActiveTools)) {
|
||||
const streamingParams: ChatCompletionCreateParamsStreaming = {
|
||||
...payload,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
const streamResponse = await client.chat.completions.create(
|
||||
streamingParams,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
const streamingResult = createStreamingExecution({
|
||||
model: requestedModel,
|
||||
providerStartTime,
|
||||
providerStartTimeISO,
|
||||
timing: { kind: 'simple', segmentName: request.model },
|
||||
initialTokens: { input: 0, output: 0, total: 0 },
|
||||
initialCost: { input: 0, output: 0, total: 0 },
|
||||
createStream: ({ output, finalizeTiming }) =>
|
||||
createReadableStreamFromOpenAIStream(streamResponse, (content, usage) => {
|
||||
output.content = content
|
||||
output.tokens = {
|
||||
input: usage.prompt_tokens,
|
||||
output: usage.completion_tokens,
|
||||
total: usage.total_tokens,
|
||||
}
|
||||
|
||||
const costResult = calculateCost(
|
||||
requestedModel,
|
||||
usage.prompt_tokens,
|
||||
usage.completion_tokens
|
||||
)
|
||||
output.cost = {
|
||||
input: costResult.input,
|
||||
output: costResult.output,
|
||||
total: costResult.total,
|
||||
}
|
||||
|
||||
finalizeTiming()
|
||||
}),
|
||||
})
|
||||
|
||||
return streamingResult
|
||||
}
|
||||
|
||||
const initialCallTime = Date.now()
|
||||
const originalToolChoice = payload.tool_choice
|
||||
const forcedTools = preparedTools?.forcedTools || []
|
||||
let usedForcedTools: string[] = []
|
||||
|
||||
let currentResponse = await client.chat.completions.create(
|
||||
payload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
const firstResponseTime = Date.now() - initialCallTime
|
||||
|
||||
let content = currentResponse.choices[0]?.message?.content || ''
|
||||
const tokens = {
|
||||
input: currentResponse.usage?.prompt_tokens || 0,
|
||||
output: currentResponse.usage?.completion_tokens || 0,
|
||||
total: currentResponse.usage?.total_tokens || 0,
|
||||
}
|
||||
const toolCalls: FunctionCallResponse[] = []
|
||||
const toolResults: Record<string, unknown>[] = []
|
||||
const currentMessages = [...formattedMessages]
|
||||
let iterationCount = 0
|
||||
let modelTime = firstResponseTime
|
||||
let toolsTime = 0
|
||||
let hasUsedForcedTool = false
|
||||
const timeSegments: TimeSegment[] = [
|
||||
{
|
||||
type: 'model',
|
||||
name: request.model,
|
||||
startTime: initialCallTime,
|
||||
endTime: initialCallTime + firstResponseTime,
|
||||
duration: firstResponseTime,
|
||||
},
|
||||
]
|
||||
|
||||
const forcedToolResult = checkForForcedToolUsage(
|
||||
currentResponse,
|
||||
originalToolChoice,
|
||||
forcedTools,
|
||||
usedForcedTools
|
||||
)
|
||||
hasUsedForcedTool = forcedToolResult.hasUsedForcedTool
|
||||
usedForcedTools = forcedToolResult.usedForcedTools
|
||||
|
||||
while (iterationCount < MAX_TOOL_ITERATIONS) {
|
||||
if (currentResponse.choices[0]?.message?.content) {
|
||||
content = currentResponse.choices[0].message.content
|
||||
}
|
||||
|
||||
const toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls
|
||||
|
||||
enrichLastModelSegmentFromChatCompletions(
|
||||
timeSegments,
|
||||
currentResponse,
|
||||
toolCallsInResponse,
|
||||
{ model: request.model, provider: 'openrouter' }
|
||||
)
|
||||
|
||||
if (!toolCallsInResponse || toolCallsInResponse.length === 0) {
|
||||
break
|
||||
}
|
||||
|
||||
const toolsStartTime = Date.now()
|
||||
|
||||
const toolExecutionPromises = toolCallsInResponse.map(async (toolCall) => {
|
||||
const toolCallStartTime = Date.now()
|
||||
const toolName = toolCall.function.name
|
||||
|
||||
try {
|
||||
const toolArgs = JSON.parse(toolCall.function.arguments)
|
||||
const tool = request.tools?.find((t) => t.id === toolName)
|
||||
|
||||
if (!tool) return null
|
||||
|
||||
const { toolParams, executionParams } = prepareToolExecution(tool, toolArgs, request)
|
||||
const result = await executeTool(toolName, executionParams, {
|
||||
signal: request.abortSignal,
|
||||
})
|
||||
const toolCallEndTime = Date.now()
|
||||
|
||||
return {
|
||||
toolCall,
|
||||
toolName,
|
||||
toolParams,
|
||||
result,
|
||||
startTime: toolCallStartTime,
|
||||
endTime: toolCallEndTime,
|
||||
duration: toolCallEndTime - toolCallStartTime,
|
||||
}
|
||||
} catch (error) {
|
||||
const toolCallEndTime = Date.now()
|
||||
logger.error('Error processing tool call (OpenRouter):', {
|
||||
error: toError(error).message,
|
||||
toolName,
|
||||
})
|
||||
|
||||
return {
|
||||
toolCall,
|
||||
toolName,
|
||||
toolParams: {},
|
||||
result: {
|
||||
success: false,
|
||||
output: undefined,
|
||||
error: getErrorMessage(error, 'Tool execution failed'),
|
||||
},
|
||||
startTime: toolCallStartTime,
|
||||
endTime: toolCallEndTime,
|
||||
duration: toolCallEndTime - toolCallStartTime,
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const executionResults = await Promise.allSettled(toolExecutionPromises)
|
||||
|
||||
currentMessages.push({
|
||||
role: 'assistant',
|
||||
content: null,
|
||||
tool_calls: toolCallsInResponse.map((tc) => ({
|
||||
id: tc.id,
|
||||
type: 'function',
|
||||
function: {
|
||||
name: tc.function.name,
|
||||
arguments: tc.function.arguments,
|
||||
},
|
||||
})),
|
||||
})
|
||||
|
||||
for (const settledResult of executionResults) {
|
||||
if (settledResult.status === 'rejected' || !settledResult.value) continue
|
||||
|
||||
const { toolCall, toolName, toolParams, result, startTime, endTime, duration } =
|
||||
settledResult.value
|
||||
|
||||
timeSegments.push({
|
||||
type: 'tool',
|
||||
name: toolName,
|
||||
startTime: startTime,
|
||||
endTime: endTime,
|
||||
duration: duration,
|
||||
toolCallId: toolCall.id,
|
||||
})
|
||||
|
||||
let resultContent: any
|
||||
if (result.success && result.output) {
|
||||
toolResults.push(result.output)
|
||||
resultContent = result.output
|
||||
} else {
|
||||
resultContent = {
|
||||
error: true,
|
||||
message: result.error || 'Tool execution failed',
|
||||
tool: toolName,
|
||||
}
|
||||
}
|
||||
|
||||
toolCalls.push({
|
||||
name: toolName,
|
||||
arguments: toolParams,
|
||||
startTime: new Date(startTime).toISOString(),
|
||||
endTime: new Date(endTime).toISOString(),
|
||||
duration: duration,
|
||||
result: resultContent,
|
||||
success: result.success,
|
||||
})
|
||||
|
||||
currentMessages.push({
|
||||
role: 'tool',
|
||||
tool_call_id: toolCall.id,
|
||||
content: JSON.stringify(resultContent),
|
||||
})
|
||||
}
|
||||
|
||||
const thisToolsTime = Date.now() - toolsStartTime
|
||||
toolsTime += thisToolsTime
|
||||
|
||||
const nextPayload = {
|
||||
...payload,
|
||||
messages: currentMessages,
|
||||
}
|
||||
|
||||
if (typeof originalToolChoice === 'object' && hasUsedForcedTool && forcedTools.length > 0) {
|
||||
const remainingTools = forcedTools.filter((tool) => !usedForcedTools.includes(tool))
|
||||
if (remainingTools.length > 0) {
|
||||
nextPayload.tool_choice = { type: 'function', function: { name: remainingTools[0] } }
|
||||
} else {
|
||||
nextPayload.tool_choice = 'auto'
|
||||
}
|
||||
}
|
||||
|
||||
const nextModelStartTime = Date.now()
|
||||
currentResponse = await client.chat.completions.create(
|
||||
nextPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
const nextForcedToolResult = checkForForcedToolUsage(
|
||||
currentResponse,
|
||||
nextPayload.tool_choice,
|
||||
forcedTools,
|
||||
usedForcedTools
|
||||
)
|
||||
hasUsedForcedTool = nextForcedToolResult.hasUsedForcedTool
|
||||
usedForcedTools = nextForcedToolResult.usedForcedTools
|
||||
const nextModelEndTime = Date.now()
|
||||
const thisModelTime = nextModelEndTime - nextModelStartTime
|
||||
timeSegments.push({
|
||||
type: 'model',
|
||||
name: request.model,
|
||||
startTime: nextModelStartTime,
|
||||
endTime: nextModelEndTime,
|
||||
duration: thisModelTime,
|
||||
})
|
||||
modelTime += thisModelTime
|
||||
if (currentResponse.choices[0]?.message?.content) {
|
||||
content = currentResponse.choices[0].message.content
|
||||
}
|
||||
if (currentResponse.usage) {
|
||||
tokens.input += currentResponse.usage.prompt_tokens || 0
|
||||
tokens.output += currentResponse.usage.completion_tokens || 0
|
||||
tokens.total += currentResponse.usage.total_tokens || 0
|
||||
}
|
||||
iterationCount++
|
||||
}
|
||||
|
||||
if (iterationCount === MAX_TOOL_ITERATIONS) {
|
||||
enrichLastModelSegmentFromChatCompletions(
|
||||
timeSegments,
|
||||
currentResponse,
|
||||
currentResponse.choices[0]?.message?.tool_calls,
|
||||
{ model: request.model, provider: 'openrouter' }
|
||||
)
|
||||
}
|
||||
|
||||
if (request.stream) {
|
||||
const accumulatedCost = calculateCost(requestedModel, tokens.input, tokens.output)
|
||||
|
||||
const streamingParams: ChatCompletionCreateParamsStreaming & { provider?: any } = {
|
||||
...payload,
|
||||
messages: [...currentMessages],
|
||||
tool_choice: 'auto',
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
|
||||
if (request.responseFormat) {
|
||||
;(streamingParams as any).messages = await applyResponseFormat(
|
||||
streamingParams as any,
|
||||
streamingParams.messages,
|
||||
request.responseFormat,
|
||||
requestedModel
|
||||
)
|
||||
}
|
||||
|
||||
const streamResponse = await client.chat.completions.create(
|
||||
streamingParams,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
const streamingResult = createStreamingExecution({
|
||||
model: requestedModel,
|
||||
providerStartTime,
|
||||
providerStartTimeISO,
|
||||
timing: {
|
||||
kind: 'accumulated',
|
||||
modelTime,
|
||||
toolsTime,
|
||||
firstResponseTime,
|
||||
iterations: iterationCount + 1,
|
||||
timeSegments,
|
||||
},
|
||||
initialTokens: { input: tokens.input, output: tokens.output, total: tokens.total },
|
||||
initialCost: {
|
||||
input: accumulatedCost.input,
|
||||
output: accumulatedCost.output,
|
||||
total: accumulatedCost.total,
|
||||
},
|
||||
toolCalls:
|
||||
toolCalls.length > 0 ? { list: toolCalls, count: toolCalls.length } : undefined,
|
||||
createStream: ({ output }) =>
|
||||
createReadableStreamFromOpenAIStream(streamResponse, (content, usage) => {
|
||||
output.content = content
|
||||
output.tokens = {
|
||||
input: tokens.input + usage.prompt_tokens,
|
||||
output: tokens.output + usage.completion_tokens,
|
||||
total: tokens.total + usage.total_tokens,
|
||||
}
|
||||
|
||||
const streamCost = calculateCost(
|
||||
requestedModel,
|
||||
usage.prompt_tokens,
|
||||
usage.completion_tokens
|
||||
)
|
||||
const tc = sumToolCosts(toolResults)
|
||||
output.cost = {
|
||||
input: accumulatedCost.input + streamCost.input,
|
||||
output: accumulatedCost.output + streamCost.output,
|
||||
toolCost: tc || undefined,
|
||||
total: accumulatedCost.total + streamCost.total + tc,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
return streamingResult
|
||||
}
|
||||
|
||||
if (request.responseFormat && hasActiveTools) {
|
||||
const finalPayload: any = {
|
||||
model: payload.model,
|
||||
messages: [...currentMessages],
|
||||
}
|
||||
if (payload.temperature !== undefined) {
|
||||
finalPayload.temperature = payload.temperature
|
||||
}
|
||||
if (payload.max_tokens !== undefined) {
|
||||
finalPayload.max_tokens = payload.max_tokens
|
||||
}
|
||||
|
||||
finalPayload.messages = await applyResponseFormat(
|
||||
finalPayload,
|
||||
finalPayload.messages,
|
||||
request.responseFormat,
|
||||
requestedModel
|
||||
)
|
||||
|
||||
const finalStartTime = Date.now()
|
||||
const finalResponse = await client.chat.completions.create(
|
||||
finalPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
const finalEndTime = Date.now()
|
||||
const finalDuration = finalEndTime - finalStartTime
|
||||
|
||||
timeSegments.push({
|
||||
type: 'model',
|
||||
name: 'Final structured response',
|
||||
startTime: finalStartTime,
|
||||
endTime: finalEndTime,
|
||||
duration: finalDuration,
|
||||
})
|
||||
modelTime += finalDuration
|
||||
|
||||
if (finalResponse.choices[0]?.message?.content) {
|
||||
content = finalResponse.choices[0].message.content
|
||||
}
|
||||
if (finalResponse.usage) {
|
||||
tokens.input += finalResponse.usage.prompt_tokens || 0
|
||||
tokens.output += finalResponse.usage.completion_tokens || 0
|
||||
tokens.total += finalResponse.usage.total_tokens || 0
|
||||
}
|
||||
|
||||
enrichLastModelSegmentFromChatCompletions(
|
||||
timeSegments,
|
||||
finalResponse,
|
||||
finalResponse.choices[0]?.message?.tool_calls,
|
||||
{ model: request.model, provider: 'openrouter' }
|
||||
)
|
||||
}
|
||||
|
||||
const providerEndTime = Date.now()
|
||||
const providerEndTimeISO = new Date(providerEndTime).toISOString()
|
||||
const totalDuration = providerEndTime - providerStartTime
|
||||
|
||||
return {
|
||||
content,
|
||||
model: requestedModel,
|
||||
tokens,
|
||||
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
||||
toolResults: toolResults.length > 0 ? toolResults : undefined,
|
||||
timing: {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: providerEndTimeISO,
|
||||
duration: totalDuration,
|
||||
modelTime: modelTime,
|
||||
toolsTime: toolsTime,
|
||||
firstResponseTime: firstResponseTime,
|
||||
iterations: iterationCount + 1,
|
||||
timeSegments: timeSegments,
|
||||
},
|
||||
}
|
||||
} catch (error) {
|
||||
const providerEndTime = Date.now()
|
||||
const providerEndTimeISO = new Date(providerEndTime).toISOString()
|
||||
const totalDuration = providerEndTime - providerStartTime
|
||||
|
||||
const errorDetails: Record<string, any> = {
|
||||
error: toError(error).message,
|
||||
duration: totalDuration,
|
||||
}
|
||||
if (error && typeof error === 'object') {
|
||||
const err = error as any
|
||||
if (err.status) errorDetails.status = err.status
|
||||
if (err.code) errorDetails.code = err.code
|
||||
if (err.type) errorDetails.type = err.type
|
||||
if (err.error?.message) errorDetails.providerMessage = err.error.message
|
||||
if (err.error?.metadata) errorDetails.metadata = err.error.metadata
|
||||
}
|
||||
|
||||
logger.error('Error in OpenRouter request:', errorDetails)
|
||||
throw new ProviderError(toError(error).message, {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: providerEndTimeISO,
|
||||
duration: totalDuration,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import type { ChatCompletionChunk } from 'openai/resources/chat/completions'
|
||||
import type { CompletionUsage } from 'openai/resources/completions'
|
||||
import { checkForForcedToolUsageOpenAI, createOpenAICompatibleStream } from '@/providers/utils'
|
||||
|
||||
const logger = createLogger('OpenRouterUtils')
|
||||
|
||||
interface OpenRouterModelData {
|
||||
id: string
|
||||
supported_parameters?: string[]
|
||||
}
|
||||
|
||||
interface ModelCapabilities {
|
||||
supportsStructuredOutputs: boolean
|
||||
supportsTools: boolean
|
||||
}
|
||||
|
||||
let modelCapabilitiesCache: Map<string, ModelCapabilities> | null = null
|
||||
let cacheTimestamp = 0
|
||||
const CACHE_TTL_MS = 5 * 60 * 1000 // 5 minutes
|
||||
|
||||
async function fetchModelCapabilities(): Promise<Map<string, ModelCapabilities>> {
|
||||
try {
|
||||
const response = await fetch('https://openrouter.ai/api/v1/models', {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
await response.text().catch(() => {})
|
||||
logger.warn('Failed to fetch OpenRouter model capabilities', {
|
||||
status: response.status,
|
||||
})
|
||||
return new Map()
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
const capabilities = new Map<string, ModelCapabilities>()
|
||||
|
||||
for (const model of (data.data ?? []) as OpenRouterModelData[]) {
|
||||
const supportedParams = model.supported_parameters ?? []
|
||||
capabilities.set(model.id, {
|
||||
supportsStructuredOutputs: supportedParams.includes('structured_outputs'),
|
||||
supportsTools: supportedParams.includes('tools'),
|
||||
})
|
||||
}
|
||||
|
||||
logger.info('Cached OpenRouter model capabilities', {
|
||||
modelCount: capabilities.size,
|
||||
withStructuredOutputs: Array.from(capabilities.values()).filter(
|
||||
(c) => c.supportsStructuredOutputs
|
||||
).length,
|
||||
})
|
||||
|
||||
return capabilities
|
||||
} catch (error) {
|
||||
logger.error('Error fetching OpenRouter model capabilities', {
|
||||
error: toError(error).message,
|
||||
})
|
||||
return new Map()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets capabilities for a specific OpenRouter model.
|
||||
* Fetches from API if cache is stale or empty.
|
||||
*/
|
||||
export async function getOpenRouterModelCapabilities(
|
||||
modelId: string
|
||||
): Promise<ModelCapabilities | null> {
|
||||
const now = Date.now()
|
||||
|
||||
if (!modelCapabilitiesCache || now - cacheTimestamp > CACHE_TTL_MS) {
|
||||
modelCapabilitiesCache = await fetchModelCapabilities()
|
||||
cacheTimestamp = now
|
||||
}
|
||||
|
||||
const normalizedId = modelId.replace(/^openrouter\//, '')
|
||||
return modelCapabilitiesCache.get(normalizedId) ?? null
|
||||
}
|
||||
|
||||
export async function supportsNativeStructuredOutputs(modelId: string): Promise<boolean> {
|
||||
const capabilities = await getOpenRouterModelCapabilities(modelId)
|
||||
return capabilities?.supportsStructuredOutputs ?? false
|
||||
}
|
||||
|
||||
export function createReadableStreamFromOpenAIStream(
|
||||
openaiStream: AsyncIterable<ChatCompletionChunk>,
|
||||
onComplete?: (content: string, usage: CompletionUsage) => void
|
||||
): ReadableStream<Uint8Array> {
|
||||
return createOpenAICompatibleStream(openaiStream, 'OpenRouter', onComplete)
|
||||
}
|
||||
|
||||
export function checkForForcedToolUsage(
|
||||
response: any,
|
||||
toolChoice: string | { type: string; function?: { name: string }; name?: string; any?: any },
|
||||
forcedTools: string[],
|
||||
usedForcedTools: string[]
|
||||
): { hasUsedForcedTool: boolean; usedForcedTools: string[] } {
|
||||
return checkForForcedToolUsageOpenAI(
|
||||
response,
|
||||
toolChoice,
|
||||
'OpenRouter',
|
||||
forcedTools,
|
||||
usedForcedTools
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user