{"version":3,"file":"unify-adapter.cjs","names":["convertActionInputToOpenAITool","convertMessageToOpenAIMessage"],"sources":["../../../src/service-adapters/unify/unify-adapter.ts"],"sourcesContent":["/**\n * CopilotKit Adapter for Unify\n *\n * <RequestExample>\n * ```jsx CopilotRuntime Example\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(req, new UnifyAdapter());\n * ```\n * </RequestExample>\n *\n * You can easily set the model to use by passing it to the constructor.\n * ```jsx\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(\n *   req,\n *   new UnifyAdapter({ model: \"llama-3-8b-chat@fireworks-ai\" }),\n * );\n * ```\n */\nimport {\n  CopilotRuntimeChatCompletionRequest,\n  CopilotRuntimeChatCompletionResponse,\n  CopilotServiceAdapter,\n} from \"../service-adapter\";\nimport { randomId, randomUUID } from \"@copilotkit/shared\";\nimport {\n  convertActionInputToOpenAITool,\n  convertMessageToOpenAIMessage,\n} from \"../openai/utils\";\n\nexport interface UnifyAdapterParams {\n  apiKey?: string;\n  model: string;\n}\n\nexport class UnifyAdapter implements CopilotServiceAdapter {\n  private apiKey: string;\n  public model: string;\n  private start: boolean;\n  public provider = \"unify\";\n\n  public get name() {\n    return \"UnifyAdapter\";\n  }\n\n  constructor(options?: UnifyAdapterParams) {\n    if (options?.apiKey) {\n      this.apiKey = options.apiKey;\n    } else {\n      this.apiKey = \"UNIFY_API_KEY\";\n    }\n    this.model = options?.model;\n    this.start = true;\n  }\n\n  async process(\n    request: CopilotRuntimeChatCompletionRequest,\n  ): Promise<CopilotRuntimeChatCompletionResponse> {\n    const tools = request.actions.map(convertActionInputToOpenAITool);\n    // Lazy require for optional peer dependency\n    // eslint-disable-next-line @typescript-eslint/no-var-requires\n    const OpenAI = require(\"openai\").default;\n    const openai = new OpenAI({\n      apiKey: this.apiKey,\n      baseURL: \"https://api.unify.ai/v0/\",\n    });\n    const forwardedParameters = request.forwardedParameters;\n\n    const messages = request.messages.map((m) =>\n      convertMessageToOpenAIMessage(m),\n    );\n\n    const stream = await openai.chat.completions.create({\n      model: this.model,\n      messages: messages,\n      stream: true,\n      ...(tools.length > 0 && { tools }),\n      ...(forwardedParameters?.temperature && {\n        temperature: forwardedParameters.temperature,\n      }),\n    });\n\n    let model = null;\n    let currentMessageId: string;\n    let currentToolCallId: string;\n    request.eventSource.stream(async (eventStream$) => {\n      let mode: \"function\" | \"message\" | null = null;\n      for await (const chunk of stream) {\n        if (this.start) {\n          model = chunk.model;\n          currentMessageId = randomId();\n          eventStream$.sendTextMessageStart({ messageId: currentMessageId });\n          eventStream$.sendTextMessageContent({\n            messageId: currentMessageId,\n            content: `Model used: ${model}\\n`,\n          });\n          eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n          this.start = false;\n        }\n        const toolCall = chunk.choices[0].delta.tool_calls?.[0];\n        const content = chunk.choices[0].delta.content;\n\n        // When switching from message to function or vice versa,\n        // send the respective end event.\n        // If toolCall?.id is defined, it means a new tool call starts.\n        if (mode === \"message\" && toolCall?.id) {\n          mode = null;\n          eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n        } else if (\n          mode === \"function\" &&\n          (toolCall === undefined || toolCall?.id)\n        ) {\n          mode = null;\n          eventStream$.sendActionExecutionEnd({\n            actionExecutionId: currentToolCallId,\n          });\n        }\n\n        // If we send a new message type, send the appropriate start event.\n        if (mode === null) {\n          if (toolCall?.id) {\n            mode = \"function\";\n            currentToolCallId = toolCall!.id;\n            eventStream$.sendActionExecutionStart({\n              actionExecutionId: currentToolCallId,\n              actionName: toolCall!.function!.name,\n            });\n          } else if (content) {\n            mode = \"message\";\n            currentMessageId = chunk.id;\n            eventStream$.sendTextMessageStart({ messageId: currentMessageId });\n          }\n        }\n\n        // send the content events\n        if (mode === \"message\" && content) {\n          eventStream$.sendTextMessageContent({\n            messageId: currentMessageId,\n            content: content,\n          });\n        } else if (mode === \"function\" && toolCall?.function?.arguments) {\n          eventStream$.sendActionExecutionArgs({\n            actionExecutionId: currentToolCallId,\n            args: toolCall.function.arguments,\n          });\n        }\n      }\n\n      // send the end events\n      if (mode === \"message\") {\n        eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n      } else if (mode === \"function\") {\n        eventStream$.sendActionExecutionEnd({\n          actionExecutionId: currentToolCallId,\n        });\n      }\n\n      eventStream$.complete();\n    });\n\n    return {\n      threadId: request.threadId || randomUUID(),\n    };\n  }\n}\n"],"mappings":";;;;;;AAmCA,IAAa,eAAb,MAA2D;CAMzD,IAAW,OAAO;AAChB,SAAO;;CAGT,YAAY,SAA8B;kBANxB;AAOhB,MAAI,SAAS,OACX,MAAK,SAAS,QAAQ;MAEtB,MAAK,SAAS;AAEhB,OAAK,QAAQ,SAAS;AACtB,OAAK,QAAQ;;CAGf,MAAM,QACJ,SAC+C;EAC/C,MAAM,QAAQ,QAAQ,QAAQ,IAAIA,6CAA+B;EAGjE,MAAM,SAAS,QAAQ,SAAS,CAAC;EACjC,MAAM,SAAS,IAAI,OAAO;GACxB,QAAQ,KAAK;GACb,SAAS;GACV,CAAC;EACF,MAAM,sBAAsB,QAAQ;EAEpC,MAAM,WAAW,QAAQ,SAAS,KAAK,MACrCC,4CAA8B,EAAE,CACjC;EAED,MAAM,SAAS,MAAM,OAAO,KAAK,YAAY,OAAO;GAClD,OAAO,KAAK;GACF;GACV,QAAQ;GACR,GAAI,MAAM,SAAS,KAAK,EAAE,OAAO;GACjC,GAAI,qBAAqB,eAAe,EACtC,aAAa,oBAAoB,aAClC;GACF,CAAC;EAEF,IAAI,QAAQ;EACZ,IAAI;EACJ,IAAI;AACJ,UAAQ,YAAY,OAAO,OAAO,iBAAiB;GACjD,IAAI,OAAsC;AAC1C,cAAW,MAAM,SAAS,QAAQ;AAChC,QAAI,KAAK,OAAO;AACd,aAAQ,MAAM;AACd,0DAA6B;AAC7B,kBAAa,qBAAqB,EAAE,WAAW,kBAAkB,CAAC;AAClE,kBAAa,uBAAuB;MAClC,WAAW;MACX,SAAS,eAAe,MAAM;MAC/B,CAAC;AACF,kBAAa,mBAAmB,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