mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
feat(providers): add Meta Muse Spark 1.1 provider (#5538)
* feat(providers): add Meta Muse Spark 1.1 provider - New BYOK-only meta provider for Meta's Model API (launched today) - muse-spark-1.1: 1M context, streaming, tool-calling, reasoning_effort (minimal->xhigh), structured output - Meta icon mark only (no wordmark), theme-safe gradient IDs via useId() - Not added to hosted models list - BYOK only, no auto-billing * fix(providers): stop sending unsupported tool_choice values to Meta Meta's Chat Completions endpoint only supports tool_choice: "auto" - "none", "required", and named-function choices all return HTTP 400 (confirmed against the official meta-model-cookbook tool-calling recipe). Never set tool_choice on the request (auto is already the default; forced-tool usage control degrades gracefully to auto with a warning log instead of failing every tool-using run), and drop `tools` entirely from the two post-tool-loop tool-free completion calls instead of trying to force tool_choice: "none".
This commit is contained in:
@@ -3687,6 +3687,62 @@ export const SakanaIcon = (props: SVGProps<SVGSVGElement>) => (
|
||||
</svg>
|
||||
)
|
||||
|
||||
export function MetaIcon(props: SVGProps<SVGSVGElement>) {
|
||||
const id = useId()
|
||||
const gradient1Id = `meta_gradient_1_${id}`
|
||||
const gradient2Id = `meta_gradient_2_${id}`
|
||||
|
||||
return (
|
||||
<svg
|
||||
{...props}
|
||||
height='1em'
|
||||
viewBox='0 0 265 165'
|
||||
width='1em'
|
||||
xmlns='http://www.w3.org/2000/svg'
|
||||
>
|
||||
<title>Meta</title>
|
||||
<defs>
|
||||
<linearGradient
|
||||
id={gradient1Id}
|
||||
x1='61'
|
||||
x2='259'
|
||||
y1='117'
|
||||
y2='127'
|
||||
gradientUnits='userSpaceOnUse'
|
||||
>
|
||||
<stop offset='0' stopColor='#0064e1' />
|
||||
<stop offset='0.4' stopColor='#0064e1' />
|
||||
<stop offset='0.83' stopColor='#0073ee' />
|
||||
<stop offset='1' stopColor='#0082fb' />
|
||||
</linearGradient>
|
||||
<linearGradient
|
||||
id={gradient2Id}
|
||||
x1='45'
|
||||
x2='45'
|
||||
y1='139'
|
||||
y2='66'
|
||||
gradientUnits='userSpaceOnUse'
|
||||
>
|
||||
<stop offset='0' stopColor='#0082fb' />
|
||||
<stop offset='1' stopColor='#0064e0' />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<path
|
||||
d='m31.06,125.96c0,10.98 2.41,19.41 5.56,24.51 4.13,6.68 10.29,9.51 16.57,9.51 8.1,0 15.51-2.01 29.79-21.76 11.44-15.83 24.92-38.05 33.99-51.98l15.36-23.6c10.67-16.39 23.02-34.61 37.18-46.96 11.56-10.08 24.03-15.68 36.58-15.68 21.07,0 41.14,12.21 56.5,35.11 16.81,25.08 24.97,56.67 24.97,89.27 0,19.38-3.82,33.62-10.32,44.87-6.28,10.88-18.52,21.75-39.11,21.75l0-31.02c17.63,0 22.03-16.2 22.03-34.74 0-26.42-6.16-55.74-19.73-76.69-9.63-14.86-22.11-23.94-35.84-23.94-14.85,0-26.8,11.2-40.23,31.17-7.14,10.61-14.47,23.54-22.7,38.13l-9.06,16.05c-18.2,32.27-22.81,39.62-31.91,51.75-15.95,21.24-29.57,29.29-47.5,29.29-21.27,0-34.72-9.21-43.05-23.09-6.8-11.31-10.14-26.15-10.14-43.06z'
|
||||
fill='#0081fb'
|
||||
/>
|
||||
<path
|
||||
d='m24.49,37.3c14.24-21.95 34.79-37.3 58.36-37.3 13.65,0 27.22,4.04 41.39,15.61 15.5,12.65 32.02,33.48 52.63,67.81l7.39,12.32c17.84,29.72 27.99,45.01 33.93,52.22 7.64,9.26 12.99,12.02 19.94,12.02 17.63,0 22.03-16.2 22.03-34.74l27.4-.86c0,19.38-3.82,33.62-10.32,44.87-6.28,10.88-18.52,21.75-39.11,21.75-12.8,0-24.14-2.78-36.68-14.61-9.64-9.08-20.91-25.21-29.58-39.71l-25.79-43.08c-12.94-21.62-24.81-37.74-31.68-45.04-7.39-7.85-16.89-17.33-32.05-17.33-12.27,0-22.69,8.61-31.41,21.78z'
|
||||
fill={`url(#${gradient1Id})`}
|
||||
/>
|
||||
<path
|
||||
d='m82.35,31.23c-12.27,0-22.69,8.61-31.41,21.78-12.33,18.61-19.88,46.33-19.88,72.95 0,10.98 2.41,19.41 5.56,24.51l-26.48,17.44c-6.8-11.31-10.14-26.15-10.14-43.06 0-30.75 8.44-62.8 24.49-87.55 14.24-21.95 34.79-37.3 58.36-37.3z'
|
||||
fill={`url(#${gradient2Id})`}
|
||||
/>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export function GeminiIcon(props: SVGProps<SVGSVGElement>) {
|
||||
const id = useId()
|
||||
const gradientId = `gemini_gradient_${id}`
|
||||
|
||||
@@ -61,6 +61,11 @@ export const TOKENIZATION_CONFIG = {
|
||||
confidence: 'medium',
|
||||
supportedMethods: ['heuristic', 'fallback'],
|
||||
},
|
||||
meta: {
|
||||
avgCharsPerToken: 4,
|
||||
confidence: 'medium',
|
||||
supportedMethods: ['heuristic', 'fallback'],
|
||||
},
|
||||
ollama: {
|
||||
avgCharsPerToken: 4,
|
||||
confidence: 'low',
|
||||
|
||||
@@ -36,6 +36,7 @@ export type AttachmentProvider =
|
||||
| 'deepseek'
|
||||
| 'cerebras'
|
||||
| 'sakana'
|
||||
| 'meta'
|
||||
|
||||
export interface PreparedProviderAttachment {
|
||||
file: UserFile
|
||||
@@ -119,7 +120,12 @@ const BEDROCK_DOCUMENT_FORMATS = new Set([
|
||||
const BEDROCK_IMAGE_FORMATS = new Set(['png', 'jpeg', 'jpg', 'gif', 'webp'])
|
||||
const BEDROCK_VIDEO_FORMATS = new Set(['mp4', 'mov', 'mkv', 'webm'])
|
||||
|
||||
const UNSUPPORTED_FILE_PROVIDERS = new Set<AttachmentProvider>(['deepseek', 'cerebras', 'sakana'])
|
||||
const UNSUPPORTED_FILE_PROVIDERS = new Set<AttachmentProvider>([
|
||||
'deepseek',
|
||||
'cerebras',
|
||||
'sakana',
|
||||
'meta',
|
||||
])
|
||||
|
||||
const PROVIDER_SUPPORTED_LABELS: Record<AttachmentProvider, string> = {
|
||||
openai: 'images and documents through the Responses API input_image/input_file parts',
|
||||
@@ -139,6 +145,7 @@ const PROVIDER_SUPPORTED_LABELS: Record<AttachmentProvider, string> = {
|
||||
deepseek: 'no file attachments in the current API adapter',
|
||||
cerebras: 'no file attachments in the current API adapter',
|
||||
sakana: 'no file attachments in the current API adapter',
|
||||
meta: 'no file attachments in the current API adapter',
|
||||
}
|
||||
|
||||
export function getAttachmentProvider(providerId: ProviderId | string): AttachmentProvider | null {
|
||||
@@ -159,6 +166,7 @@ export function getAttachmentProvider(providerId: ProviderId | string): Attachme
|
||||
if (providerId === 'deepseek') return 'deepseek'
|
||||
if (providerId === 'cerebras') return 'cerebras'
|
||||
if (providerId === 'sakana') return 'sakana'
|
||||
if (providerId === 'meta') return 'meta'
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -307,6 +315,7 @@ function isMimeTypeSupportedByProvider(
|
||||
case 'deepseek':
|
||||
case 'cerebras':
|
||||
case 'sakana':
|
||||
case 'meta':
|
||||
return false
|
||||
default: {
|
||||
const _exhaustive: never = provider
|
||||
|
||||
@@ -0,0 +1,647 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { getErrorMessage, toError } from '@sim/utils/errors'
|
||||
import OpenAI from 'openai'
|
||||
import type { StreamingExecution } from '@/executor/types'
|
||||
import { MAX_TOOL_ITERATIONS } from '@/providers'
|
||||
import { formatMessagesForProvider } from '@/providers/attachments'
|
||||
import { createReadableStreamFromMetaStream } from '@/providers/meta/utils'
|
||||
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
|
||||
import { createStreamingExecution } from '@/providers/streaming-execution'
|
||||
import { adaptOpenAIChatToolSchema } from '@/providers/tool-schema-adapter'
|
||||
import { enrichLastModelSegmentFromChatCompletions } from '@/providers/trace-enrichment'
|
||||
import type {
|
||||
ProviderConfig,
|
||||
ProviderRequest,
|
||||
ProviderResponse,
|
||||
TimeSegment,
|
||||
} from '@/providers/types'
|
||||
import { ProviderError } from '@/providers/types'
|
||||
import {
|
||||
calculateCost,
|
||||
prepareToolExecution,
|
||||
prepareToolsWithUsageControl,
|
||||
sumToolCosts,
|
||||
trackForcedToolUsage,
|
||||
} from '@/providers/utils'
|
||||
import { executeTool } from '@/tools'
|
||||
|
||||
const logger = createLogger('MetaProvider')
|
||||
|
||||
const META_BASE_URL = 'https://api.meta.ai/v1'
|
||||
|
||||
export const metaProvider: ProviderConfig = {
|
||||
id: 'meta',
|
||||
name: 'Meta',
|
||||
description: "Meta's Muse Spark models via the Meta Model API (OpenAI-compatible)",
|
||||
version: '1.0.0',
|
||||
models: getProviderModels('meta'),
|
||||
defaultModel: getProviderDefaultModel('meta'),
|
||||
|
||||
executeRequest: async (
|
||||
request: ProviderRequest
|
||||
): Promise<ProviderResponse | StreamingExecution> => {
|
||||
if (!request.apiKey) {
|
||||
throw new Error('API key is required for Meta')
|
||||
}
|
||||
|
||||
const providerStartTime = Date.now()
|
||||
const providerStartTimeISO = new Date(providerStartTime).toISOString()
|
||||
|
||||
try {
|
||||
const meta = new OpenAI({
|
||||
apiKey: request.apiKey,
|
||||
baseURL: META_BASE_URL,
|
||||
})
|
||||
|
||||
const allMessages = []
|
||||
|
||||
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, 'meta')
|
||||
|
||||
const tools = request.tools?.length
|
||||
? request.tools.map((tool) => adaptOpenAIChatToolSchema(tool))
|
||||
: undefined
|
||||
|
||||
const payload: any = {
|
||||
model: request.model,
|
||||
messages: formattedMessages,
|
||||
}
|
||||
|
||||
if (request.temperature !== undefined) payload.temperature = request.temperature
|
||||
if (request.maxTokens != null) payload.max_completion_tokens = request.maxTokens
|
||||
if (request.reasoningEffort !== undefined && request.reasoningEffort !== 'auto') {
|
||||
payload.reasoning_effort = request.reasoningEffort
|
||||
}
|
||||
|
||||
const responseFormatPayload = request.responseFormat
|
||||
? {
|
||||
type: 'json_schema' as const,
|
||||
json_schema: {
|
||||
name: request.responseFormat.name || 'response_schema',
|
||||
schema: request.responseFormat.schema || request.responseFormat,
|
||||
strict: request.responseFormat.strict !== false,
|
||||
},
|
||||
}
|
||||
: undefined
|
||||
|
||||
let preparedTools: ReturnType<typeof prepareToolsWithUsageControl> | null = null
|
||||
let hasActiveTools = false
|
||||
|
||||
if (tools?.length) {
|
||||
preparedTools = prepareToolsWithUsageControl(tools, request.tools, logger, 'openai')
|
||||
const { tools: filteredTools, toolChoice } = preparedTools
|
||||
|
||||
if (filteredTools?.length && toolChoice) {
|
||||
payload.tools = filteredTools
|
||||
hasActiveTools = true
|
||||
|
||||
// Meta's Chat Completions endpoint only supports tool_choice: "auto" —
|
||||
// "none", "required", and named-function choices all return HTTP 400
|
||||
// (confirmed via the official meta-model-cookbook tool-calling recipe).
|
||||
// "auto" is already the endpoint default, so we never set the field; a
|
||||
// forced tool choice degrades to auto rather than failing the request.
|
||||
if (typeof toolChoice === 'object') {
|
||||
logger.warn(
|
||||
'Meta does not support forcing a specific tool; falling back to auto tool_choice',
|
||||
{ requestedTool: toolChoice.function.name, model: request.model }
|
||||
)
|
||||
}
|
||||
|
||||
logger.info('Meta request configuration:', {
|
||||
toolCount: filteredTools.length,
|
||||
toolChoice:
|
||||
typeof toolChoice === 'string'
|
||||
? toolChoice
|
||||
: toolChoice.type === 'function'
|
||||
? `force:${toolChoice.function.name}`
|
||||
: 'unknown',
|
||||
model: request.model,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Structured output and tool calling cannot be sent together — OpenAI-compatible
|
||||
// backends reject a request that carries both `response_format` and active
|
||||
// `tools`/`tool_choice`. Defer the schema until after the tool loop completes.
|
||||
const deferResponseFormat = !!responseFormatPayload && hasActiveTools
|
||||
if (responseFormatPayload && !deferResponseFormat) {
|
||||
payload.response_format = responseFormatPayload
|
||||
}
|
||||
|
||||
if (request.stream && (!tools || tools.length === 0 || !hasActiveTools)) {
|
||||
logger.info('Using streaming response for Meta request (no tools)')
|
||||
|
||||
const streamResponse = await meta.chat.completions.create(
|
||||
{
|
||||
...payload,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
},
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
const streamingResult = createStreamingExecution({
|
||||
model: request.model,
|
||||
providerStartTime,
|
||||
providerStartTimeISO,
|
||||
timing: { kind: 'simple', segmentName: request.model },
|
||||
initialTokens: { input: 0, output: 0, total: 0 },
|
||||
initialCost: { input: 0, output: 0, total: 0 },
|
||||
isStreaming: true,
|
||||
createStream: ({ output }) =>
|
||||
createReadableStreamFromMetaStream(streamResponse as any, (content, usage) => {
|
||||
output.content = content
|
||||
output.tokens = {
|
||||
input: usage.prompt_tokens,
|
||||
output: usage.completion_tokens,
|
||||
total: usage.total_tokens,
|
||||
}
|
||||
|
||||
const costResult = calculateCost(
|
||||
request.model,
|
||||
usage.prompt_tokens,
|
||||
usage.completion_tokens
|
||||
)
|
||||
output.cost = {
|
||||
input: costResult.input,
|
||||
output: costResult.output,
|
||||
total: costResult.total,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
return streamingResult
|
||||
}
|
||||
|
||||
const initialCallTime = Date.now()
|
||||
const originalToolChoice = payload.tool_choice
|
||||
const forcedTools = preparedTools?.forcedTools || []
|
||||
let usedForcedTools: string[] = []
|
||||
|
||||
let currentResponse = await meta.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 = []
|
||||
const toolResults: Record<string, unknown>[] = []
|
||||
const currentMessages = [...formattedMessages]
|
||||
let iterationCount = 0
|
||||
let hasUsedForcedTool = false
|
||||
let modelTime = firstResponseTime
|
||||
let toolsTime = 0
|
||||
|
||||
const timeSegments: TimeSegment[] = [
|
||||
{
|
||||
type: 'model',
|
||||
name: request.model,
|
||||
startTime: initialCallTime,
|
||||
endTime: initialCallTime + firstResponseTime,
|
||||
duration: firstResponseTime,
|
||||
},
|
||||
]
|
||||
|
||||
if (
|
||||
typeof originalToolChoice === 'object' &&
|
||||
currentResponse.choices[0]?.message?.tool_calls
|
||||
) {
|
||||
const toolCallsResponse = currentResponse.choices[0].message.tool_calls
|
||||
const result = trackForcedToolUsage(
|
||||
toolCallsResponse,
|
||||
originalToolChoice,
|
||||
logger,
|
||||
'openai',
|
||||
forcedTools,
|
||||
usedForcedTools
|
||||
)
|
||||
hasUsedForcedTool = result.hasUsedForcedTool
|
||||
usedForcedTools = result.usedForcedTools
|
||||
}
|
||||
|
||||
try {
|
||||
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: 'meta' }
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
// Every tool_call in the assistant message must be answered by a matching
|
||||
// `tool` message, or the next request violates the OpenAI message contract.
|
||||
// Emit an error result for an unknown tool rather than dropping it.
|
||||
if (!tool) {
|
||||
const toolCallEndTime = Date.now()
|
||||
return {
|
||||
toolCall,
|
||||
toolName,
|
||||
toolParams: {},
|
||||
result: {
|
||||
success: false,
|
||||
output: undefined,
|
||||
error: `Tool "${toolName}" is not available`,
|
||||
},
|
||||
startTime: toolCallStartTime,
|
||||
endTime: toolCallEndTime,
|
||||
duration: toolCallEndTime - toolCallStartTime,
|
||||
}
|
||||
}
|
||||
|
||||
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:', { error, 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] },
|
||||
}
|
||||
logger.info(`Forcing next tool: ${remainingTools[0]}`)
|
||||
} else {
|
||||
nextPayload.tool_choice = 'auto'
|
||||
logger.info('All forced tools have been used, switching to auto tool_choice')
|
||||
}
|
||||
}
|
||||
|
||||
const nextModelStartTime = Date.now()
|
||||
currentResponse = await meta.chat.completions.create(
|
||||
nextPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
if (
|
||||
typeof nextPayload.tool_choice === 'object' &&
|
||||
currentResponse.choices[0]?.message?.tool_calls
|
||||
) {
|
||||
const toolCallsResponse = currentResponse.choices[0].message.tool_calls
|
||||
const result = trackForcedToolUsage(
|
||||
toolCallsResponse,
|
||||
nextPayload.tool_choice,
|
||||
logger,
|
||||
'openai',
|
||||
forcedTools,
|
||||
usedForcedTools
|
||||
)
|
||||
hasUsedForcedTool = result.hasUsedForcedTool
|
||||
usedForcedTools = result.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: 'meta' }
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error in Meta request:', { error })
|
||||
throw error
|
||||
}
|
||||
|
||||
if (request.stream) {
|
||||
logger.info('Using streaming for final Meta response after tool processing')
|
||||
|
||||
// The tool loop is complete: this final pass only produces the textual answer.
|
||||
// Meta rejects tool_choice: "none" (only "auto" is supported), so instead of
|
||||
// forcing tool_choice we omit `tools` from this call entirely — with no tools
|
||||
// declared, the model cannot emit a fresh tool call for the text-only adapter to drop.
|
||||
const { tools: _omittedTools, ...streamingBasePayload } = payload
|
||||
const streamingPayload: any = {
|
||||
...streamingBasePayload,
|
||||
messages: currentMessages,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
if (deferResponseFormat && responseFormatPayload) {
|
||||
streamingPayload.response_format = responseFormatPayload
|
||||
}
|
||||
|
||||
const streamResponse = await meta.chat.completions.create(
|
||||
streamingPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
const accumulatedCost = calculateCost(request.model, tokens.input, tokens.output)
|
||||
|
||||
const streamingResult = createStreamingExecution({
|
||||
model: request.model,
|
||||
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,
|
||||
toolCost: undefined as number | undefined,
|
||||
total: accumulatedCost.total,
|
||||
},
|
||||
toolCalls:
|
||||
toolCalls.length > 0
|
||||
? {
|
||||
list: toolCalls,
|
||||
count: toolCalls.length,
|
||||
}
|
||||
: undefined,
|
||||
isStreaming: true,
|
||||
createStream: ({ output }) =>
|
||||
createReadableStreamFromMetaStream(streamResponse as any, (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(
|
||||
request.model,
|
||||
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
|
||||
}
|
||||
|
||||
// Tools were active, so `response_format` was withheld from the loop. Make one final
|
||||
// tool-free call to obtain the structured response now that the tool work is done.
|
||||
// Meta rejects tool_choice: "none", so `tools` is dropped from this payload instead
|
||||
// (see the streaming pass above for the same constraint).
|
||||
if (deferResponseFormat && responseFormatPayload) {
|
||||
logger.info('Applying deferred JSON schema response format after tool processing')
|
||||
|
||||
const finalFormatStartTime = Date.now()
|
||||
const { tools: _omittedDeferredTools, ...deferredBasePayload } = payload
|
||||
const finalPayload: any = {
|
||||
...deferredBasePayload,
|
||||
messages: currentMessages,
|
||||
response_format: responseFormatPayload,
|
||||
}
|
||||
|
||||
currentResponse = await meta.chat.completions.create(
|
||||
finalPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
const finalFormatEndTime = Date.now()
|
||||
timeSegments.push({
|
||||
type: 'model',
|
||||
name: request.model,
|
||||
startTime: finalFormatStartTime,
|
||||
endTime: finalFormatEndTime,
|
||||
duration: finalFormatEndTime - finalFormatStartTime,
|
||||
})
|
||||
modelTime += finalFormatEndTime - finalFormatStartTime
|
||||
|
||||
const formattedContent = currentResponse.choices[0]?.message?.content
|
||||
if (formattedContent) {
|
||||
content = formattedContent
|
||||
}
|
||||
|
||||
if (currentResponse.usage) {
|
||||
tokens.input += currentResponse.usage.prompt_tokens || 0
|
||||
tokens.output += currentResponse.usage.completion_tokens || 0
|
||||
tokens.total += currentResponse.usage.total_tokens || 0
|
||||
}
|
||||
|
||||
enrichLastModelSegmentFromChatCompletions(
|
||||
timeSegments,
|
||||
currentResponse,
|
||||
currentResponse.choices[0]?.message?.tool_calls,
|
||||
{ model: request.model, provider: 'meta' }
|
||||
)
|
||||
}
|
||||
|
||||
const providerEndTime = Date.now()
|
||||
const providerEndTimeISO = new Date(providerEndTime).toISOString()
|
||||
const totalDuration = providerEndTime - providerStartTime
|
||||
|
||||
return {
|
||||
content,
|
||||
model: request.model,
|
||||
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
|
||||
|
||||
logger.error('Error in Meta request:', {
|
||||
error,
|
||||
duration: totalDuration,
|
||||
})
|
||||
|
||||
throw new ProviderError(toError(error).message, {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: providerEndTimeISO,
|
||||
duration: totalDuration,
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { ChatCompletionChunk } from 'openai/resources/chat/completions'
|
||||
import type { CompletionUsage } from 'openai/resources/completions'
|
||||
import { createOpenAICompatibleStream } from '@/providers/utils'
|
||||
|
||||
/**
|
||||
* Creates a ReadableStream from a Meta Model API streaming response.
|
||||
* Uses the shared OpenAI-compatible streaming utility.
|
||||
*/
|
||||
export function createReadableStreamFromMetaStream(
|
||||
metaStream: AsyncIterable<ChatCompletionChunk>,
|
||||
onComplete?: (content: string, usage: CompletionUsage) => void
|
||||
): ReadableStream<Uint8Array> {
|
||||
return createOpenAICompatibleStream(metaStream, 'Meta', onComplete)
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
GeminiIcon,
|
||||
GroqIcon,
|
||||
LitellmIcon,
|
||||
MetaIcon,
|
||||
MistralIcon,
|
||||
OllamaIcon,
|
||||
OpenAIIcon,
|
||||
@@ -2352,6 +2353,38 @@ export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
|
||||
},
|
||||
],
|
||||
},
|
||||
meta: {
|
||||
id: 'meta',
|
||||
name: 'Meta',
|
||||
description: "Meta's Muse Spark models via the Meta Model API (OpenAI-compatible)",
|
||||
defaultModel: 'muse-spark-1.1',
|
||||
modelPatterns: [/^muse-spark/],
|
||||
icon: MetaIcon,
|
||||
color: '#0082FB',
|
||||
capabilities: {
|
||||
temperature: { min: 0, max: 2 },
|
||||
toolUsageControl: true,
|
||||
},
|
||||
models: [
|
||||
{
|
||||
id: 'muse-spark-1.1',
|
||||
pricing: {
|
||||
input: 1.25,
|
||||
cachedInput: 0.15,
|
||||
output: 4.25,
|
||||
updatedAt: '2026-07-09',
|
||||
},
|
||||
capabilities: {
|
||||
reasoningEffort: {
|
||||
values: ['minimal', 'low', 'medium', 'high', 'xhigh'],
|
||||
},
|
||||
},
|
||||
contextWindow: 1048576,
|
||||
releaseDate: '2026-07-09',
|
||||
recommended: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
mistral: {
|
||||
id: 'mistral',
|
||||
name: 'Mistral AI',
|
||||
|
||||
@@ -11,6 +11,7 @@ import { fireworksProvider } from '@/providers/fireworks'
|
||||
import { googleProvider } from '@/providers/google'
|
||||
import { groqProvider } from '@/providers/groq'
|
||||
import { litellmProvider } from '@/providers/litellm'
|
||||
import { metaProvider } from '@/providers/meta'
|
||||
import { mistralProvider } from '@/providers/mistral'
|
||||
import { ollamaProvider } from '@/providers/ollama'
|
||||
import { ollamaCloudProvider } from '@/providers/ollama-cloud'
|
||||
@@ -36,6 +37,7 @@ const providerRegistry: Record<ProviderId, ProviderConfig> = {
|
||||
cerebras: cerebrasProvider,
|
||||
groq: groqProvider,
|
||||
sakana: sakanaProvider,
|
||||
meta: metaProvider,
|
||||
vllm: vllmProvider,
|
||||
litellm: litellmProvider,
|
||||
mistral: mistralProvider,
|
||||
|
||||
@@ -12,6 +12,7 @@ export type ProviderId =
|
||||
| 'cerebras'
|
||||
| 'groq'
|
||||
| 'sakana'
|
||||
| 'meta'
|
||||
| 'mistral'
|
||||
| 'ollama'
|
||||
| 'ollama-cloud'
|
||||
|
||||
@@ -155,6 +155,7 @@ export const providers: Record<ProviderId, ProviderMetadata> = {
|
||||
cerebras: buildProviderMetadata('cerebras'),
|
||||
groq: buildProviderMetadata('groq'),
|
||||
sakana: buildProviderMetadata('sakana'),
|
||||
meta: buildProviderMetadata('meta'),
|
||||
mistral: buildProviderMetadata('mistral'),
|
||||
bedrock: buildProviderMetadata('bedrock'),
|
||||
openrouter: buildProviderMetadata('openrouter'),
|
||||
|
||||
Reference in New Issue
Block a user