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,620 @@
|
||||
import type { Logger } from '@sim/logger'
|
||||
import { getErrorMessage } from '@sim/utils/errors'
|
||||
import OpenAI from 'openai'
|
||||
import type {
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParamsStreaming,
|
||||
} from 'openai/resources/chat/completions'
|
||||
import type { CompletionUsage } from 'openai/resources/completions'
|
||||
import type { StreamingExecution } from '@/executor/types'
|
||||
import { MAX_TOOL_ITERATIONS } from '@/providers'
|
||||
import { formatMessagesForProvider } from '@/providers/attachments'
|
||||
import { createStreamingExecution } from '@/providers/streaming-execution'
|
||||
import { adaptOpenAIChatToolSchema } from '@/providers/tool-schema-adapter'
|
||||
import { enrichLastModelSegmentFromChatCompletions } from '@/providers/trace-enrichment'
|
||||
import type { Message, ProviderRequest, ProviderResponse, TimeSegment } from '@/providers/types'
|
||||
import { ProviderError } from '@/providers/types'
|
||||
import {
|
||||
calculateCost,
|
||||
generateSchemaInstructions,
|
||||
prepareToolExecution,
|
||||
sumToolCosts,
|
||||
} from '@/providers/utils'
|
||||
import { executeTool } from '@/tools'
|
||||
|
||||
/**
|
||||
* Ollama enforces JSON mode (`json_object`) but ignores `json_schema`, so
|
||||
* structured outputs use JSON mode with the schema described in-prompt. Mutates
|
||||
* `payload.response_format` and returns the messages with instructions appended.
|
||||
*/
|
||||
function applyJsonResponseFormat(
|
||||
payload: { response_format?: unknown },
|
||||
messages: Message[],
|
||||
responseFormat: NonNullable<ProviderRequest['responseFormat']>
|
||||
): Message[] {
|
||||
payload.response_format = { type: 'json_object' }
|
||||
const schema = responseFormat.schema || responseFormat
|
||||
return [
|
||||
...messages,
|
||||
{ role: 'user', content: generateSchemaInstructions(schema, responseFormat.name) },
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-provider hooks for the shared Ollama execution logic. The self-hosted
|
||||
* `ollama` and hosted `ollama-cloud` providers differ only in client
|
||||
* construction and labels; both pass those in here.
|
||||
*/
|
||||
export interface OllamaCoreConfig {
|
||||
/** Provider id used for trace enrichment (`ollama`, `ollama-cloud`). */
|
||||
providerId: string
|
||||
/** Human-readable label used in log messages. */
|
||||
providerLabel: string
|
||||
/** Builds the OpenAI-compatible client (base URL + credentials per provider). */
|
||||
createClient: () => OpenAI
|
||||
createStream: (
|
||||
stream: AsyncIterable<ChatCompletionChunk>,
|
||||
onComplete?: (content: string, usage: CompletionUsage) => void
|
||||
) => ReadableStream<Uint8Array>
|
||||
logger: Logger
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared execution logic for the Ollama-family providers, which speak the same
|
||||
* OpenAI-compatible Ollama API. Ollama ignores `tool_choice`, so tools are sent
|
||||
* as `tool_choice: 'auto'` (forced tools degrade to auto) and the final post-tool
|
||||
* call drops tools entirely rather than relying on `tool_choice: 'none'`.
|
||||
*/
|
||||
export async function executeOllamaProviderRequest(
|
||||
request: ProviderRequest,
|
||||
config: OllamaCoreConfig
|
||||
): Promise<ProviderResponse | StreamingExecution> {
|
||||
const { providerId, providerLabel, logger } = config
|
||||
|
||||
logger.info(`Preparing ${providerLabel} request`, {
|
||||
model: request.model,
|
||||
hasSystemPrompt: !!request.systemPrompt,
|
||||
hasMessages: !!request.messages?.length,
|
||||
hasTools: !!request.tools?.length,
|
||||
toolCount: request.tools?.length || 0,
|
||||
hasResponseFormat: !!request.responseFormat,
|
||||
stream: !!request.stream,
|
||||
})
|
||||
|
||||
const ollama = config.createClient()
|
||||
|
||||
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, providerId) as Message[]
|
||||
|
||||
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_tokens = request.maxTokens
|
||||
|
||||
let hasActiveTools = false
|
||||
if (tools?.length) {
|
||||
const filteredTools = tools.filter((tool) => {
|
||||
const toolId = tool.function?.name
|
||||
const toolConfig = request.tools?.find((t) => t.id === toolId)
|
||||
return toolConfig?.usageControl !== 'none'
|
||||
})
|
||||
|
||||
const hasForcedTools = tools.some((tool) => {
|
||||
const toolId = tool.function?.name
|
||||
const toolConfig = request.tools?.find((t) => t.id === toolId)
|
||||
return toolConfig?.usageControl === 'force'
|
||||
})
|
||||
|
||||
if (hasForcedTools) {
|
||||
logger.warn(
|
||||
`${providerLabel} does not support forced tool selection (tool_choice parameter is ignored). ` +
|
||||
'Tools marked with usageControl="force" will behave as "auto" instead.'
|
||||
)
|
||||
}
|
||||
|
||||
if (filteredTools?.length) {
|
||||
payload.tools = filteredTools
|
||||
payload.tool_choice = 'auto'
|
||||
hasActiveTools = true
|
||||
|
||||
logger.info(`${providerLabel} request configuration:`, {
|
||||
toolCount: filteredTools.length,
|
||||
toolChoice: 'auto',
|
||||
forcedToolsIgnored: hasForcedTools,
|
||||
model: request.model,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// With tools, defer structured output to the final call so JSON mode doesn't preempt tool use.
|
||||
if (request.responseFormat && !hasActiveTools) {
|
||||
payload.messages = applyJsonResponseFormat(payload, payload.messages, request.responseFormat)
|
||||
logger.info(`Added JSON response format to ${providerLabel} request`)
|
||||
}
|
||||
|
||||
const providerStartTime = Date.now()
|
||||
const providerStartTimeISO = new Date(providerStartTime).toISOString()
|
||||
|
||||
try {
|
||||
if (request.stream && (!tools || tools.length === 0 || !hasActiveTools)) {
|
||||
logger.info(`Using streaming response for ${providerLabel} request`)
|
||||
|
||||
const streamingParams: ChatCompletionCreateParamsStreaming = {
|
||||
...payload,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
const streamResponse = await ollama.chat.completions.create(
|
||||
streamingParams,
|
||||
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 },
|
||||
createStream: ({ output, finalizeTiming }) =>
|
||||
config.createStream(streamResponse, (content, usage) => {
|
||||
output.content = content
|
||||
|
||||
if (content && request.responseFormat) {
|
||||
output.content = content.replace(/```json\n?|\n?```/g, '').trim()
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
finalizeTiming()
|
||||
}),
|
||||
})
|
||||
|
||||
return streamingResult
|
||||
}
|
||||
|
||||
const initialCallTime = Date.now()
|
||||
|
||||
let currentResponse = await ollama.chat.completions.create(
|
||||
payload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
const firstResponseTime = Date.now() - initialCallTime
|
||||
|
||||
let content = currentResponse.choices[0]?.message?.content || ''
|
||||
|
||||
if (content && request.responseFormat) {
|
||||
content = content.replace(/```json\n?|\n?```/g, '')
|
||||
content = content.trim()
|
||||
}
|
||||
|
||||
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 modelTime = firstResponseTime
|
||||
let toolsTime = 0
|
||||
|
||||
const timeSegments: TimeSegment[] = [
|
||||
{
|
||||
type: 'model',
|
||||
name: request.model,
|
||||
startTime: initialCallTime,
|
||||
endTime: initialCallTime + firstResponseTime,
|
||||
duration: firstResponseTime,
|
||||
},
|
||||
]
|
||||
|
||||
while (iterationCount < MAX_TOOL_ITERATIONS) {
|
||||
if (currentResponse.choices[0]?.message?.content) {
|
||||
content = currentResponse.choices[0].message.content
|
||||
if (request.responseFormat) {
|
||||
content = content.replace(/```json\n?|\n?```/g, '').trim()
|
||||
}
|
||||
}
|
||||
|
||||
const toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls
|
||||
|
||||
enrichLastModelSegmentFromChatCompletions(
|
||||
timeSegments,
|
||||
currentResponse,
|
||||
toolCallsInResponse,
|
||||
{
|
||||
model: request.model,
|
||||
provider: providerId,
|
||||
}
|
||||
)
|
||||
|
||||
if (!toolCallsInResponse || toolCallsInResponse.length === 0) {
|
||||
break
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`Processing ${toolCallsInResponse.length} tool calls (iteration ${iterationCount + 1}/${MAX_TOOL_ITERATIONS})`
|
||||
)
|
||||
|
||||
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:', { 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,
|
||||
}
|
||||
|
||||
const nextModelStartTime = Date.now()
|
||||
|
||||
currentResponse = await ollama.chat.completions.create(
|
||||
nextPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
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 (request.responseFormat) {
|
||||
content = content.replace(/```json\n?|\n?```/g, '').trim()
|
||||
}
|
||||
}
|
||||
|
||||
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: providerId }
|
||||
)
|
||||
}
|
||||
|
||||
if (request.stream) {
|
||||
logger.info(`Using streaming for final ${providerLabel} response after tool processing`)
|
||||
|
||||
const accumulatedCost = calculateCost(request.model, tokens.input, tokens.output)
|
||||
|
||||
const { tools: _tools, tool_choice: _toolChoice, ...streamPayload } = payload
|
||||
|
||||
const finalMessages = request.responseFormat
|
||||
? applyJsonResponseFormat(streamPayload, currentMessages, request.responseFormat)
|
||||
: currentMessages
|
||||
|
||||
const streamingParams: ChatCompletionCreateParamsStreaming = {
|
||||
...streamPayload,
|
||||
messages: finalMessages,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
}
|
||||
const streamResponse = await ollama.chat.completions.create(
|
||||
streamingParams,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
|
||||
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,
|
||||
total: accumulatedCost.total,
|
||||
},
|
||||
toolCalls:
|
||||
toolCalls.length > 0
|
||||
? {
|
||||
list: toolCalls,
|
||||
count: toolCalls.length,
|
||||
}
|
||||
: undefined,
|
||||
createStream: ({ output }) =>
|
||||
config.createStream(streamResponse, (content, usage) => {
|
||||
output.content = content
|
||||
|
||||
if (content && request.responseFormat) {
|
||||
output.content = content.replace(/```json\n?|\n?```/g, '').trim()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Deferred structured output: one final JSON-mode call now that tools have run.
|
||||
if (request.responseFormat && hasActiveTools) {
|
||||
const finalPayload: any = { model: payload.model }
|
||||
if (payload.temperature !== undefined) finalPayload.temperature = payload.temperature
|
||||
if (payload.max_tokens !== undefined) finalPayload.max_tokens = payload.max_tokens
|
||||
finalPayload.messages = applyJsonResponseFormat(
|
||||
finalPayload,
|
||||
currentMessages,
|
||||
request.responseFormat
|
||||
)
|
||||
|
||||
const finalStartTime = Date.now()
|
||||
const finalResponse = await ollama.chat.completions.create(
|
||||
finalPayload,
|
||||
request.abortSignal ? { signal: request.abortSignal } : undefined
|
||||
)
|
||||
const finalEndTime = Date.now()
|
||||
|
||||
timeSegments.push({
|
||||
type: 'model',
|
||||
name: 'Final structured response',
|
||||
startTime: finalStartTime,
|
||||
endTime: finalEndTime,
|
||||
duration: finalEndTime - finalStartTime,
|
||||
})
|
||||
modelTime += finalEndTime - finalStartTime
|
||||
|
||||
if (finalResponse.choices[0]?.message?.content) {
|
||||
content = finalResponse.choices[0].message.content.replace(/```json\n?|\n?```/g, '').trim()
|
||||
}
|
||||
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: providerId }
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
let errorMessage = getErrorMessage(error, 'Unknown error')
|
||||
let errorType: string | undefined
|
||||
let errorCode: string | undefined
|
||||
let status: number | undefined
|
||||
|
||||
if (error instanceof OpenAI.APIError) {
|
||||
errorMessage = error.message
|
||||
errorType = error.type
|
||||
errorCode = error.code ?? undefined
|
||||
status = error.status
|
||||
}
|
||||
|
||||
logger.error(`Error in ${providerLabel} request:`, {
|
||||
error: errorMessage,
|
||||
errorType,
|
||||
errorCode,
|
||||
status,
|
||||
duration: totalDuration,
|
||||
})
|
||||
|
||||
throw new ProviderError(errorMessage, {
|
||||
startTime: providerStartTimeISO,
|
||||
endTime: providerEndTimeISO,
|
||||
duration: totalDuration,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
type StreamUsage = { prompt_tokens: number; completion_tokens: number; total_tokens: number }
|
||||
|
||||
const { mockCreate, mockExecuteTool, streamOnComplete, MockAPIError } = vi.hoisted(() => {
|
||||
class MockAPIError extends Error {
|
||||
status?: number
|
||||
code?: string | null
|
||||
type?: string
|
||||
constructor(message: string, opts: { status?: number; code?: string; type?: string } = {}) {
|
||||
super(message)
|
||||
this.name = 'APIError'
|
||||
this.status = opts.status
|
||||
this.code = opts.code
|
||||
this.type = opts.type
|
||||
}
|
||||
}
|
||||
return {
|
||||
mockCreate: vi.fn(),
|
||||
mockExecuteTool: vi.fn(),
|
||||
streamOnComplete: {
|
||||
current: undefined as undefined | ((content: string, usage: StreamUsage) => void),
|
||||
},
|
||||
MockAPIError,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('openai', () => {
|
||||
const OpenAI = vi.fn().mockImplementation(
|
||||
class {
|
||||
chat = { completions: { create: mockCreate } }
|
||||
}
|
||||
)
|
||||
;(OpenAI as unknown as { APIError: typeof MockAPIError }).APIError = MockAPIError
|
||||
return { default: OpenAI }
|
||||
})
|
||||
|
||||
vi.mock('@/lib/core/utils/urls', () => ({ getOllamaUrl: () => 'http://localhost:11434' }))
|
||||
vi.mock('@/providers', () => ({ MAX_TOOL_ITERATIONS: 20 }))
|
||||
vi.mock('@/providers/attachments', () => ({
|
||||
formatMessagesForProvider: (messages: unknown) => messages,
|
||||
}))
|
||||
vi.mock('@/providers/trace-enrichment', () => ({
|
||||
enrichLastModelSegmentFromChatCompletions: vi.fn(),
|
||||
}))
|
||||
vi.mock('@/providers/ollama/utils', () => ({
|
||||
createReadableStreamFromOllamaStream: (
|
||||
_stream: unknown,
|
||||
onComplete: (content: string, usage: StreamUsage) => void
|
||||
) => {
|
||||
streamOnComplete.current = onComplete
|
||||
return 'OLLAMA_STREAM'
|
||||
},
|
||||
}))
|
||||
vi.mock('@/providers/utils', () => ({
|
||||
calculateCost: () => ({ input: 0, output: 0, total: 0, pricing: null }),
|
||||
generateSchemaInstructions: () => 'SCHEMA_INSTRUCTIONS',
|
||||
prepareToolExecution: (_tool: unknown, args: Record<string, unknown>) => ({
|
||||
toolParams: args,
|
||||
executionParams: args,
|
||||
}),
|
||||
sumToolCosts: () => 0,
|
||||
}))
|
||||
vi.mock('@/tools', () => ({ executeTool: mockExecuteTool }))
|
||||
vi.mock('@/stores/providers', () => ({
|
||||
useProvidersStore: { getState: () => ({ setProviderModels: vi.fn() }) },
|
||||
}))
|
||||
|
||||
import { ollamaProvider } from '@/providers/ollama'
|
||||
import type { ProviderRequest, ProviderResponse, ProviderToolConfig } from '@/providers/types'
|
||||
|
||||
interface StreamingResult {
|
||||
stream: string
|
||||
execution: {
|
||||
output: {
|
||||
content: string
|
||||
tokens: { input: number; output: number; total: number }
|
||||
toolCalls?: { list: unknown[]; count: number }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ToolCallChunk = { id: string; type: 'function'; function: { name: string; arguments: string } }
|
||||
|
||||
function completion(
|
||||
opts: { content?: string | null; toolCalls?: ToolCallChunk[]; usage?: StreamUsage } = {}
|
||||
) {
|
||||
return {
|
||||
choices: [{ message: { content: opts.content ?? null, tool_calls: opts.toolCalls } }],
|
||||
usage: opts.usage ?? { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 },
|
||||
}
|
||||
}
|
||||
|
||||
function makeTool(id: string, usageControl?: 'auto' | 'force' | 'none'): ProviderToolConfig {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
description: `${id} tool`,
|
||||
params: {},
|
||||
parameters: { type: 'object', properties: {}, required: [] },
|
||||
...(usageControl ? { usageControl } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
const baseRequest: ProviderRequest = {
|
||||
model: 'llama3.2',
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
}
|
||||
|
||||
describe('ollamaProvider.executeRequest', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
streamOnComplete.current = undefined
|
||||
mockCreate.mockResolvedValue(completion({ content: 'hello' }))
|
||||
mockExecuteTool.mockResolvedValue({ success: true, output: { ok: true } })
|
||||
})
|
||||
|
||||
it('assembles system, context, then history in order and forwards params', async () => {
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
systemPrompt: 'be nice',
|
||||
context: 'ctx',
|
||||
temperature: 0.5,
|
||||
maxTokens: 128,
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(result).toMatchObject({ content: 'hello', model: 'llama3.2' })
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.messages).toEqual([
|
||||
{ role: 'system', content: 'be nice' },
|
||||
{ role: 'user', content: 'ctx' },
|
||||
{ role: 'user', content: 'hi' },
|
||||
])
|
||||
expect(payload.model).toBe('llama3.2')
|
||||
expect(payload.temperature).toBe(0.5)
|
||||
expect(payload.max_tokens).toBe(128)
|
||||
})
|
||||
|
||||
it('returns content verbatim (keeps ```json fences) when no responseFormat', async () => {
|
||||
const fenced = '```json\n{"a":1}\n```'
|
||||
mockCreate.mockResolvedValue(completion({ content: fenced }))
|
||||
const result = (await ollamaProvider.executeRequest(baseRequest)) as ProviderResponse
|
||||
expect(result.content).toBe(fenced)
|
||||
})
|
||||
|
||||
it('strips ```json fences and requests JSON mode with schema instructions when responseFormat is set', async () => {
|
||||
mockCreate.mockResolvedValue(completion({ content: '```json\n{"a":1}\n```' }))
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
responseFormat: { name: 'r', schema: { type: 'object' }, strict: true },
|
||||
})) as ProviderResponse
|
||||
expect(result.content).toBe('{"a":1}')
|
||||
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 structured output while tools run, then makes a final JSON-mode call', async () => {
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(
|
||||
completion({
|
||||
toolCalls: [
|
||||
{ id: 'call_1', type: 'function', function: { name: 'mytool', arguments: '{}' } },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(completion({ content: 'intermediate' }))
|
||||
.mockResolvedValueOnce(completion({ content: '{"a":1}' }))
|
||||
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [makeTool('mytool')],
|
||||
responseFormat: { name: 'r', schema: { type: 'object' } },
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(mockCreate).toHaveBeenCalledTimes(3)
|
||||
expect(mockCreate.mock.calls[0][0].response_format).toBeUndefined()
|
||||
expect(mockCreate.mock.calls[0][0].tools).toBeDefined()
|
||||
|
||||
const finalCall = mockCreate.mock.calls[2][0]
|
||||
expect(finalCall.response_format).toEqual({ type: 'json_object' })
|
||||
expect(finalCall.tools).toBeUndefined()
|
||||
expect(finalCall.messages.at(-1)).toEqual({ role: 'user', content: 'SCHEMA_INSTRUCTIONS' })
|
||||
expect(result.content).toBe('{"a":1}')
|
||||
})
|
||||
|
||||
it('runs the tool loop: parses string args, feeds results back, then terminates', async () => {
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(
|
||||
completion({
|
||||
toolCalls: [
|
||||
{ id: 'call_1', type: 'function', function: { name: 'mytool', arguments: '{"x":1}' } },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(completion({ content: 'done' }))
|
||||
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [makeTool('mytool')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(mockExecuteTool).toHaveBeenCalledWith('mytool', { x: 1 }, expect.anything())
|
||||
expect(mockCreate).toHaveBeenCalledTimes(2)
|
||||
expect(result.content).toBe('done')
|
||||
expect(result.toolCalls).toEqual([
|
||||
expect.objectContaining({ name: 'mytool', success: true, arguments: { x: 1 } }),
|
||||
])
|
||||
expect(result.toolResults).toEqual([{ ok: true }])
|
||||
|
||||
const followUp = mockCreate.mock.calls[1][0].messages
|
||||
expect(followUp).toContainEqual(
|
||||
expect.objectContaining({
|
||||
role: 'assistant',
|
||||
content: null,
|
||||
tool_calls: [
|
||||
expect.objectContaining({
|
||||
id: 'call_1',
|
||||
function: { name: 'mytool', arguments: '{"x":1}' },
|
||||
}),
|
||||
],
|
||||
})
|
||||
)
|
||||
expect(followUp).toContainEqual({
|
||||
role: 'tool',
|
||||
tool_call_id: 'call_1',
|
||||
content: JSON.stringify({ ok: true }),
|
||||
})
|
||||
})
|
||||
|
||||
it('records a failed tool result without aborting the loop', async () => {
|
||||
mockExecuteTool.mockResolvedValue({ success: false, error: 'boom' })
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(
|
||||
completion({
|
||||
toolCalls: [
|
||||
{ id: 'call_1', type: 'function', function: { name: 'mytool', arguments: '{}' } },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(completion({ content: 'recovered' }))
|
||||
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [makeTool('mytool')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(result.content).toBe('recovered')
|
||||
expect(result.toolCalls?.[0]).toMatchObject({ name: 'mytool', success: false })
|
||||
const toolMsg = mockCreate.mock.calls[1][0].messages.find(
|
||||
(m: { role: string }) => m.role === 'tool'
|
||||
)
|
||||
expect(JSON.parse(toolMsg.content)).toMatchObject({ error: true, message: 'boom' })
|
||||
})
|
||||
|
||||
it('executes parallel tool calls from a single response', async () => {
|
||||
mockExecuteTool
|
||||
.mockResolvedValueOnce({ success: true, output: { from: 'a' } })
|
||||
.mockResolvedValueOnce({ success: true, output: { from: 'b' } })
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(
|
||||
completion({
|
||||
toolCalls: [
|
||||
{ id: 'call_a', type: 'function', function: { name: 'a', arguments: '{}' } },
|
||||
{ id: 'call_b', type: 'function', function: { name: 'b', arguments: '{}' } },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(completion({ content: 'summary' }))
|
||||
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [makeTool('a'), makeTool('b')],
|
||||
})) as ProviderResponse
|
||||
|
||||
expect(mockExecuteTool).toHaveBeenCalledTimes(2)
|
||||
expect(result.toolCalls?.map((c) => c.name)).toEqual(['a', 'b'])
|
||||
const toolMsgs = mockCreate.mock.calls[1][0].messages.filter(
|
||||
(m: { role: string }) => m.role === 'tool'
|
||||
)
|
||||
expect(toolMsgs.map((m: { tool_call_id: string }) => m.tool_call_id)).toEqual([
|
||||
'call_a',
|
||||
'call_b',
|
||||
])
|
||||
})
|
||||
|
||||
it('filters out tools with usageControl "none"', async () => {
|
||||
await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
tools: [makeTool('keep'), makeTool('drop', 'none')],
|
||||
})
|
||||
const sent = mockCreate.mock.calls[0][0].tools
|
||||
expect(sent.map((t: { function: { name: string } }) => t.function.name)).toEqual(['keep'])
|
||||
})
|
||||
|
||||
it('never forces tools (Ollama ignores tool_choice) and keeps "auto"', async () => {
|
||||
await ollamaProvider.executeRequest({ ...baseRequest, tools: [makeTool('forced', 'force')] })
|
||||
const payload = mockCreate.mock.calls[0][0]
|
||||
expect(payload.tool_choice).toBe('auto')
|
||||
expect(payload.tools.map((t: { function: { name: string } }) => t.function.name)).toEqual([
|
||||
'forced',
|
||||
])
|
||||
})
|
||||
|
||||
it('surfaces an OpenAI APIError message through ProviderError', async () => {
|
||||
mockCreate.mockRejectedValue(
|
||||
new MockAPIError('model not found', {
|
||||
status: 404,
|
||||
code: 'not_found',
|
||||
type: 'invalid_request_error',
|
||||
})
|
||||
)
|
||||
await expect(ollamaProvider.executeRequest(baseRequest)).rejects.toThrow('model not found')
|
||||
})
|
||||
|
||||
it('streams content and usage when no tools are used', async () => {
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
stream: true,
|
||||
})) as unknown as StreamingResult
|
||||
|
||||
expect(result.stream).toBe('OLLAMA_STREAM')
|
||||
expect(mockCreate.mock.calls[0][0].stream_options).toEqual({ include_usage: true })
|
||||
|
||||
streamOnComplete.current?.('streamed text', {
|
||||
prompt_tokens: 4,
|
||||
completion_tokens: 6,
|
||||
total_tokens: 10,
|
||||
})
|
||||
expect(result.execution.output.content).toBe('streamed text')
|
||||
expect(result.execution.output.tokens).toMatchObject({ input: 4, output: 6, total: 10 })
|
||||
})
|
||||
|
||||
it('strips ```json fences from streamed content when responseFormat is set', async () => {
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
stream: true,
|
||||
responseFormat: { name: 'r', schema: { type: 'object' }, strict: true },
|
||||
})) as unknown as StreamingResult
|
||||
|
||||
streamOnComplete.current?.('```json\n{"a":1}\n```', {
|
||||
prompt_tokens: 1,
|
||||
completion_tokens: 2,
|
||||
total_tokens: 3,
|
||||
})
|
||||
expect(result.execution.output.content).toBe('{"a":1}')
|
||||
})
|
||||
|
||||
it('streams the final response after a tool loop, carrying tool calls', async () => {
|
||||
mockCreate
|
||||
.mockResolvedValueOnce(
|
||||
completion({
|
||||
toolCalls: [
|
||||
{ id: 'call_1', type: 'function', function: { name: 'mytool', arguments: '{}' } },
|
||||
],
|
||||
})
|
||||
)
|
||||
.mockResolvedValueOnce(completion({ content: 'intermediate' }))
|
||||
|
||||
const result = (await ollamaProvider.executeRequest({
|
||||
...baseRequest,
|
||||
stream: true,
|
||||
tools: [makeTool('mytool')],
|
||||
})) as unknown as StreamingResult
|
||||
|
||||
expect(result.stream).toBe('OLLAMA_STREAM')
|
||||
expect(mockExecuteTool).toHaveBeenCalledTimes(1)
|
||||
|
||||
const finalCall = mockCreate.mock.calls[2][0]
|
||||
expect(finalCall.tools).toBeUndefined()
|
||||
expect(finalCall.tool_choice).toBeUndefined()
|
||||
|
||||
streamOnComplete.current?.('final answer', {
|
||||
prompt_tokens: 2,
|
||||
completion_tokens: 4,
|
||||
total_tokens: 6,
|
||||
})
|
||||
expect(result.execution.output.content).toBe('final answer')
|
||||
expect(result.execution.output.toolCalls).toMatchObject({ count: 1 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { getErrorMessage } from '@sim/utils/errors'
|
||||
import OpenAI from 'openai'
|
||||
import { getOllamaUrl } from '@/lib/core/utils/urls'
|
||||
import type { StreamingExecution } from '@/executor/types'
|
||||
import { executeOllamaProviderRequest } from '@/providers/ollama/core'
|
||||
import type { ModelsObject } from '@/providers/ollama/types'
|
||||
import { createReadableStreamFromOllamaStream } from '@/providers/ollama/utils'
|
||||
import type { ProviderConfig, ProviderRequest, ProviderResponse } from '@/providers/types'
|
||||
import { useProvidersStore } from '@/stores/providers'
|
||||
|
||||
const logger = createLogger('OllamaProvider')
|
||||
const OLLAMA_HOST = getOllamaUrl()
|
||||
|
||||
export const ollamaProvider: ProviderConfig = {
|
||||
id: 'ollama',
|
||||
name: 'Ollama',
|
||||
description: 'Local Ollama server for LLM inference',
|
||||
version: '1.0.0',
|
||||
models: [],
|
||||
defaultModel: '',
|
||||
|
||||
async initialize() {
|
||||
if (typeof window !== 'undefined') {
|
||||
logger.info('Skipping Ollama initialization on client side to avoid CORS issues')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${OLLAMA_HOST}/api/tags`)
|
||||
if (!response.ok) {
|
||||
await response.text().catch(() => {})
|
||||
useProvidersStore.getState().setProviderModels('ollama', [])
|
||||
logger.warn('Ollama service is not available. The provider will be disabled.')
|
||||
return
|
||||
}
|
||||
const data = (await response.json()) as ModelsObject
|
||||
this.models = data.models.map((model) => model.name)
|
||||
useProvidersStore.getState().setProviderModels('ollama', this.models)
|
||||
} catch (error) {
|
||||
logger.warn('Ollama model instantiation failed. The provider will be disabled.', {
|
||||
error: getErrorMessage(error, 'Unknown error'),
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
executeRequest: async (
|
||||
request: ProviderRequest
|
||||
): Promise<ProviderResponse | StreamingExecution> => {
|
||||
return executeOllamaProviderRequest(request, {
|
||||
providerId: 'ollama',
|
||||
providerLabel: 'Ollama',
|
||||
createClient: () =>
|
||||
new OpenAI({
|
||||
apiKey: 'empty',
|
||||
baseURL: `${OLLAMA_HOST}/v1`,
|
||||
}),
|
||||
createStream: createReadableStreamFromOllamaStream,
|
||||
logger,
|
||||
})
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
interface Model {
|
||||
name: string
|
||||
model: string
|
||||
modified_at: string
|
||||
size: number
|
||||
digest: string
|
||||
details: object
|
||||
}
|
||||
|
||||
export interface ModelsObject {
|
||||
models: Model[]
|
||||
}
|
||||
@@ -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 an Ollama streaming response.
|
||||
* Uses the shared OpenAI-compatible streaming utility.
|
||||
*/
|
||||
export function createReadableStreamFromOllamaStream(
|
||||
ollamaStream: AsyncIterable<ChatCompletionChunk>,
|
||||
onComplete?: (content: string, usage: CompletionUsage) => void
|
||||
): ReadableStream<Uint8Array> {
|
||||
return createOpenAICompatibleStream(ollamaStream, 'Ollama', onComplete)
|
||||
}
|
||||
Reference in New Issue
Block a user