| import { randomUUID } from "crypto"; |
| import { BaseExecutor } from "./base.js"; |
| import { PROVIDERS } from "../config/providers.js"; |
| import { commandCodeToOpenAIResponse } from "../translator/response/commandcode-to-openai.js"; |
| import { SSE_DONE } from "../utils/sseConstants.js"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export class CommandCodeExecutor extends BaseExecutor { |
| constructor() { |
| super("commandcode", PROVIDERS.commandcode); |
| } |
|
|
| transformRequest(model, body, stream, credentials) { |
| body.stream = true; |
| return body; |
| } |
|
|
| buildHeaders(credentials, stream = true) { |
| const headers = { |
| "Content-Type": "application/json", |
| ...(this.config.headers || {}), |
| "x-session-id": randomUUID(), |
| }; |
|
|
| const token = credentials?.apiKey || credentials?.accessToken; |
| if (token) headers["Authorization"] = `Bearer ${token}`; |
|
|
| if (stream) headers["Accept"] = "text/event-stream"; |
| return headers; |
| } |
|
|
| async execute(opts) { |
| const result = await super.execute(opts); |
| if (!result?.response?.ok || !result.response.body) return result; |
| result.response = wrapNdjsonAsOpenAISse(result.response, opts.model); |
| return result; |
| } |
| } |
|
|
| function wrapNdjsonAsOpenAISse(originalResponse, model) { |
| const decoder = new TextDecoder(); |
| const encoder = new TextEncoder(); |
| let buffer = ""; |
| const state = { model }; |
|
|
| const emitChunks = (chunks, controller) => { |
| if (!chunks) return; |
| const list = Array.isArray(chunks) ? chunks : [chunks]; |
| for (const c of list) { |
| if (c == null) continue; |
| controller.enqueue(encoder.encode(`data: ${JSON.stringify(c)}\n\n`)); |
| } |
| }; |
|
|
| const transform = new TransformStream({ |
| transform(chunk, controller) { |
| buffer += decoder.decode(chunk, { stream: true }); |
| const lines = buffer.split("\n"); |
| buffer = lines.pop() || ""; |
| for (const line of lines) { |
| const trimmed = line.trim(); |
| if (!trimmed) continue; |
| |
| emitChunks(commandCodeToOpenAIResponse(trimmed, state), controller); |
| } |
| }, |
| flush(controller) { |
| const trimmed = buffer.trim(); |
| if (trimmed) { |
| emitChunks(commandCodeToOpenAIResponse(trimmed, state), controller); |
| } |
| controller.enqueue(encoder.encode(SSE_DONE)); |
| }, |
| }); |
|
|
| const newBody = originalResponse.body.pipeThrough(transform); |
| return new Response(newBody, { |
| status: originalResponse.status, |
| statusText: originalResponse.statusText, |
| headers: originalResponse.headers, |
| }); |
| } |
|
|
| export default CommandCodeExecutor; |
|
|