UNPKG

@n8n-plus/n8n-plus

Version:

n8n Workflow Automation Tool (plus edition)

96 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.reverseTranslateOpenAiRequest = reverseTranslateOpenAiRequest; exports.extractRequestModel = extractRequestModel; exports.forwardTranslateToChatCompletion = forwardTranslateToChatCompletion; exports.buildOpenAiErrorEnvelope = buildOpenAiErrorEnvelope; const node_crypto_1 = require("node:crypto"); const OPENAI_SYNTHETIC_URL = 'https://api.openai.com/v1/chat/completions'; const DEFAULT_MODEL = 'gpt-4o-mini'; function reverseTranslateOpenAiRequest(body) { return { url: OPENAI_SYNTHETIC_URL, method: 'POST', body: body ?? {}, }; } function extractRequestModel(body) { if (typeof body !== 'object' || body === null) return DEFAULT_MODEL; const model = body.model; return typeof model === 'string' && model.length > 0 ? model : DEFAULT_MODEL; } function forwardTranslateToChatCompletion(mockResponse, model) { const content = extractAssistantContent(mockResponse?.body); const finishReason = extractFinishReason(mockResponse?.body); return { id: `chatcmpl-${(0, node_crypto_1.randomUUID)()}`, object: 'chat.completion', created: Math.floor(Date.now() / 1000), model, choices: [ { index: 0, message: { role: 'assistant', content }, finish_reason: finishReason, }, ], usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0, }, system_fingerprint: 'eval-wire-server', }; } function buildOpenAiErrorEnvelope(message) { return { error: { message, type: 'eval_wire_server_error', param: null, code: 'eval_mock_generation_failed', }, }; } function extractAssistantContent(body) { if (body === null || body === undefined) return ''; if (typeof body === 'string') return body; if (typeof body !== 'object') return String(body); const obj = body; const choices = obj.choices; if (Array.isArray(choices) && choices.length > 0) { const first = choices[0]; if (typeof first === 'object' && first !== null) { const message = first.message; if (typeof message === 'object' && message !== null) { const inner = message.content; if (typeof inner === 'string') return inner; } } } if (typeof obj.content === 'string') return obj.content; if (typeof obj.message === 'string') return obj.message; return JSON.stringify(body); } function extractFinishReason(body) { if (typeof body !== 'object' || body === null) return 'stop'; const choices = body.choices; if (Array.isArray(choices) && choices.length > 0) { const first = choices[0]; if (typeof first === 'object' && first !== null) { const reason = first.finish_reason; if (typeof reason === 'string' && reason.length > 0) return reason; } } return 'stop'; } //# sourceMappingURL=openai-envelope.js.map