2026-07-03 00:14:56 +01:00
import { readFile } from "node:fs/promises" ;
2026-04-24 13:17:00 +01:00
import {
type Actor ,
type ContextBundle ,
type GuildContext ,
type ProviderAdapter ,
2026-07-03 00:14:56 +01:00
type ProviderArtifact ,
2026-04-24 13:17:00 +01:00
type ProviderRequest ,
type ProviderResponse ,
} from "@voidbot/shared" ;
2026-07-15 20:23:41 +01:00
import {
executeToolCall ,
buildToolDefinitions ,
} from "./local-llm-tools" ;
import {
MAX_TOOL_TURNS ,
type LocalLlmToolbox ,
type ToolTraceRecord ,
} from "./local-llm-shared" ;
2026-07-03 00:14:56 +01:00
export interface OpenAiApiProviderOptions {
enabled : boolean ;
baseUrl : string ;
apiKey? : string ;
apiKeyFile? : string ;
model : string ;
timeoutMs : number ;
authHeader : string ;
maxCompletionTokens : number ;
2026-07-15 20:23:41 +01:00
toolbox? : LocalLlmToolbox ;
2026-07-03 00:14:56 +01:00
}
interface ChatMessage {
2026-07-15 20:23:41 +01:00
role : "user" | "assistant" | "system" | "tool" ;
content : string | null ;
tool_call_id? : string ;
tool_calls? : ChatToolCall [ ] ;
}
interface ChatToolCall {
id? : string ;
type ? : "function" ;
function ? : {
name? : unknown ;
arguments? : unknown ;
} ;
2026-07-03 00:14:56 +01:00
}
interface ChatCompletionResponse {
choices? : Array < {
message ? : {
content? : unknown ;
2026-07-15 20:23:41 +01:00
tool_calls? : unknown ;
reasoning_content? : unknown ;
2026-07-03 00:14:56 +01:00
} ;
finish_reason? : unknown ;
} > ;
usage ? : {
prompt_tokens? : unknown ;
completion_tokens? : unknown ;
total_tokens? : unknown ;
} ;
id? : unknown ;
model? : unknown ;
}
2026-04-24 13:17:00 +01:00
export class OpenAiApiProvider implements ProviderAdapter {
2026-07-03 00:14:56 +01:00
public constructor ( private readonly options : OpenAiApiProviderOptions ) { }
2026-04-24 13:17:00 +01:00
public getName ( ) : "openai_api" {
return "openai_api" ;
}
public getCapabilities ( ) : string [ ] {
return [
2026-07-03 00:14:56 +01:00
"openai_compatible_chat_completions" ,
"direct_api_generation" ,
"persona_runtime" ,
2026-04-24 13:17:00 +01:00
] ;
}
public isEnabled ( ) : boolean {
2026-07-03 00:14:56 +01:00
return this . options . enabled ;
2026-04-24 13:17:00 +01:00
}
public isAllowedForActor ( _actor : Actor , _guildContext : GuildContext ) : boolean {
2026-07-03 00:14:56 +01:00
return this . options . enabled ;
2026-04-24 13:17:00 +01:00
}
public buildRequest (
contextBundle : ContextBundle ,
options? : Record < string , unknown > ,
) : ProviderRequest {
return {
provider : "openai_api" ,
contextBundle ,
options ,
} ;
}
2026-07-03 00:14:56 +01:00
public async execute ( request : ProviderRequest ) : Promise < ProviderResponse > {
const model = readStringOption ( request . options ? . model ) ? ? this . options . model ;
const messages : ChatMessage [ ] = [
{
role : "user" ,
content : request.contextBundle.prompt ,
} ,
] ;
2026-07-15 20:23:41 +01:00
const tools = this . options . toolbox ? buildToolDefinitions ( ) : undefined ;
const toolTrace : ToolTraceRecord [ ] = [ ] ;
let finalPayload : ChatCompletionResponse | undefined ;
let finalRequestBody : Record < string , unknown > | undefined ;
let outputText : string | undefined ;
2026-07-03 00:14:56 +01:00
const requestBody = {
model ,
messages ,
2026-07-15 20:23:41 +01:00
. . . ( tools ? { tools } : { } ) ,
2026-07-03 00:14:56 +01:00
stream : false ,
max_completion_tokens : readPositiveIntegerOption ( request . options ? . maxCompletionTokens ) ? ?
this . options . maxCompletionTokens ,
} ;
const controller = new AbortController ( ) ;
const timeoutHandle = setTimeout ( ( ) = > controller . abort ( ) , this . options . timeoutMs ) ;
try {
const apiKey = await this . loadApiKey ( ) ;
2026-07-15 20:23:41 +01:00
for ( let turn = 0 ; turn <= MAX_TOOL_TURNS ; turn += 1 ) {
const currentRequestBody = {
. . . requestBody ,
messages ,
} ;
finalRequestBody = currentRequestBody ;
const response = await fetch ( ` ${ normalizeBaseUrl ( this . options . baseUrl ) } /chat/completions ` , {
method : "POST" ,
headers : this.buildHeaders ( apiKey ) ,
body : JSON.stringify ( currentRequestBody ) ,
signal : controller.signal ,
} ) ;
const responseText = await response . text ( ) ;
if ( ! response . ok ) {
throw new Error (
` OpenAI-compatible chat request failed with ${ response . status } : ${ redactSecret ( responseText , apiKey ) . slice ( 0 , 2000 ) } ` ,
) ;
}
const payload = JSON . parse ( responseText ) as ChatCompletionResponse ;
finalPayload = payload ;
const assistantMessage = normalizeAssistantMessage ( payload ) ;
messages . push ( assistantMessage ) ;
const toolCalls = assistantMessage . tool_calls ? ? [ ] ;
if ( toolCalls . length === 0 ) {
outputText = normalizeModelText ( assistantMessage . content ) ;
break ;
}
if ( ! this . options . toolbox ) {
outputText =
"The OpenAI-compatible model asked for tools, but this lane was not given any. That is a wiring problem." ;
break ;
}
2026-07-03 00:14:56 +01:00
2026-07-15 20:23:41 +01:00
if ( turn >= MAX_TOOL_TURNS ) {
outputText =
"The OpenAI-compatible model kept reaching for more archive context than this lane can chase in one pass. Ask more narrowly or use the deeper Codex lane." ;
break ;
}
for ( const toolCall of toolCalls ) {
const execution = await executeToolCall (
this . options . toolbox ,
request . contextBundle ,
toolCall ,
) ;
toolTrace . push ( execution . trace ) ;
messages . push ( {
role : "tool" ,
tool_call_id : toolCall.id ? ? ` ${ execution . trace . tool } - ${ toolTrace . length } ` ,
content : JSON.stringify ( execution . payload , null , 2 ) ,
} ) ;
}
}
2026-07-03 00:14:56 +01:00
if ( ! outputText ) {
2026-07-15 20:23:41 +01:00
const finishReason = normalizeModelText ( finalPayload ? . choices ? . [ 0 ] ? . finish_reason ) ? ? "unknown" ;
2026-07-03 00:14:56 +01:00
throw new Error (
` OpenAI-compatible chat response did not include assistant content (finish_reason= ${ finishReason } ). ` ,
) ;
}
return {
status : "completed" ,
summary : ` OpenAI-compatible API response generated with ${ model } . ` ,
outputText ,
2026-07-15 20:23:41 +01:00
artifacts : buildArtifacts ( finalRequestBody ? ? requestBody , finalPayload ? ? { } , toolTrace ) ,
2026-07-03 00:14:56 +01:00
metadata : {
model ,
2026-07-15 20:23:41 +01:00
provider_model : normalizeModelText ( finalPayload ? . model ) ? ? model ,
finish_reason : normalizeModelText ( finalPayload ? . choices ? . [ 0 ] ? . finish_reason ) ? ? "unknown" ,
tool_calls : String ( toolTrace . length ) ,
prompt_tokens : readNumericMetadata ( finalPayload ? . usage ? . prompt_tokens ) ,
completion_tokens : readNumericMetadata ( finalPayload ? . usage ? . completion_tokens ) ,
total_tokens : readNumericMetadata ( finalPayload ? . usage ? . total_tokens ) ,
2026-07-03 00:14:56 +01:00
} ,
} ;
} catch ( error ) {
if ( ( error as Error ) . name === "AbortError" ) {
throw new Error (
` Timed out waiting for OpenAI-compatible model " ${ model } " at ${ this . options . baseUrl } . ` ,
) ;
}
throw error ;
} finally {
clearTimeout ( timeoutHandle ) ;
}
2026-04-24 13:17:00 +01:00
}
public async estimateCost ( _request : ProviderRequest ) : Promise < number > {
return 0 ;
}
public getAuditRedactions ( ) : string [ ] {
2026-07-03 00:14:56 +01:00
return [ "api_key" , "authorization" ] ;
}
private async loadApiKey ( ) : Promise < string > {
const direct = this . options . apiKey ? . trim ( ) ;
if ( direct ) {
return direct ;
}
if ( this . options . apiKeyFile ) {
const fromFile = ( await readFile ( this . options . apiKeyFile , "utf8" ) ) . trim ( ) ;
if ( fromFile ) {
return fromFile ;
}
}
throw new Error ( "OpenAI-compatible API provider is enabled but no API key is configured." ) ;
2026-04-24 13:17:00 +01:00
}
2026-07-03 00:14:56 +01:00
private buildHeaders ( apiKey : string ) : Record < string , string > {
const headers : Record < string , string > = {
"Content-Type" : "application/json" ,
} ;
const authHeader = this . options . authHeader . trim ( ) ;
if ( authHeader . toLowerCase ( ) === "authorization" ) {
headers . Authorization = ` Bearer ${ apiKey } ` ;
} else {
headers [ authHeader ] = apiKey ;
}
return headers ;
}
}
function buildArtifacts (
requestBody : Record < string , unknown > ,
payload : ChatCompletionResponse ,
2026-07-15 20:23:41 +01:00
toolTrace : ToolTraceRecord [ ] ,
2026-07-03 00:14:56 +01:00
) : ProviderArtifact [ ] {
return [
{
name : "openai-api-request.json" ,
contentType : "json" ,
content : ` ${ JSON . stringify ( requestBody , null , 2 ) } \ n ` ,
} ,
{
name : "openai-api-response.json" ,
contentType : "json" ,
content : ` ${ JSON . stringify ( payload , null , 2 ) } \ n ` ,
} ,
2026-07-15 20:23:41 +01:00
{
name : "openai-api-tool-trace.json" ,
contentType : "json" ,
content : ` ${ JSON . stringify ( toolTrace , null , 2 ) } \ n ` ,
} ,
2026-07-03 00:14:56 +01:00
] ;
}
2026-07-15 20:23:41 +01:00
function normalizeAssistantMessage ( payload : ChatCompletionResponse ) : ChatMessage {
const message = payload . choices ? . [ 0 ] ? . message ? ? { } ;
return {
role : "assistant" ,
content : normalizeModelText ( message . content ) ? ? "" ,
tool_calls : normalizeChatToolCalls ( message . tool_calls ) ,
} ;
}
function normalizeChatToolCalls ( value : unknown ) : ChatToolCall [ ] {
if ( ! Array . isArray ( value ) ) {
return [ ] ;
}
return value . filter ( ( call ) : call is ChatToolCall = > {
if ( typeof call !== "object" || call === null ) {
return false ;
}
const candidate = call as ChatToolCall ;
return (
typeof candidate . function === "object" &&
candidate . function !== null &&
typeof candidate . function . name === "string"
) ;
} ) ;
}
2026-07-03 00:14:56 +01:00
function normalizeBaseUrl ( value : string ) : string {
return value . replace ( /\/+$/ , "" ) ;
}
function normalizeModelText ( value : unknown ) : string | undefined {
return typeof value === "string" && value . trim ( ) . length > 0 ? value . trim ( ) : undefined ;
2026-04-24 13:17:00 +01:00
}
2026-07-03 00:14:56 +01:00
function readStringOption ( value : unknown ) : string | undefined {
return normalizeModelText ( value ) ;
}
function readPositiveIntegerOption ( value : unknown ) : number | undefined {
if ( typeof value === "number" && Number . isInteger ( value ) && value > 0 ) {
return value ;
}
if ( typeof value === "string" && /^\d+$/ . test ( value . trim ( ) ) ) {
const parsed = Number ( value ) ;
return parsed > 0 ? parsed : undefined ;
}
return undefined ;
}
function readNumericMetadata ( value : unknown ) : string {
return typeof value === "number" && Number . isFinite ( value ) ? String ( value ) : "0" ;
}
function redactSecret ( text : string , secret : string ) : string {
return secret ? text . split ( secret ) . join ( "[redacted]" ) : text ;
}