{"version":3,"file":"ollama-adapter.cjs","names":[],"sources":["../../../../src/service-adapters/experimental/ollama/ollama-adapter.ts"],"sourcesContent":["/**\n * CopilotKit Adapter for Ollama\n *\n * <RequestExample>\n * ```jsx CopilotRuntime Example\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(req, new OllamaAdapter());\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 OllamaAdapter({ model: \"llama3-70b-8192\" }),\n * );\n * ```\n */\nimport { TextMessage } from \"../../../graphql/types/converted\";\nimport {\n  CopilotServiceAdapter,\n  CopilotRuntimeChatCompletionRequest,\n  CopilotRuntimeChatCompletionResponse,\n} from \"../../service-adapter\";\nimport { randomId, randomUUID } from \"@copilotkit/shared\";\n\nconst DEFAULT_MODEL = \"llama3:latest\";\n\ninterface OllamaAdapterOptions {\n  model?: string;\n}\n\nexport class ExperimentalOllamaAdapter implements CopilotServiceAdapter {\n  public model: string;\n  public provider = \"ollama\";\n  public get name() {\n    return \"OllamaAdapter\";\n  }\n\n  constructor(options?: OllamaAdapterOptions) {\n    if (options?.model) {\n      this.model = options.model;\n    } else {\n      this.model = DEFAULT_MODEL;\n    }\n  }\n\n  async process(\n    request: CopilotRuntimeChatCompletionRequest,\n  ): Promise<CopilotRuntimeChatCompletionResponse> {\n    const { messages, actions, eventSource } = request;\n    // const messages = this.transformMessages(forwardedProps.messages);\n\n    // eslint-disable-next-line @typescript-eslint/no-var-requires\n    const { Ollama } = require(\"@langchain/community/llms/ollama\");\n    const ollama = new Ollama({\n      model: this.model,\n    });\n    const contents = (\n      messages.filter((m) => m.isTextMessage()) as TextMessage[]\n    ).map((m) => m.content);\n    const _stream = await ollama.stream(contents); // [TODO] role info is dropped...\n\n    eventSource.stream(async (eventStream$) => {\n      const currentMessageId = randomId();\n      eventStream$.sendTextMessageStart({ messageId: currentMessageId });\n      for await (const chunkText of _stream) {\n        eventStream$.sendTextMessageContent({\n          messageId: currentMessageId,\n          content: chunkText,\n        });\n      }\n      eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n      // we may need to add this later.. [nc]\n      // let calls = (await result.response).functionCalls();\n\n      eventStream$.complete();\n    });\n    return {\n      threadId: request.threadId || randomUUID(),\n    };\n  }\n}\n"],"mappings":";;;;;AA2BA,MAAM,gBAAgB;AAMtB,IAAa,4BAAb,MAAwE;CAGtE,IAAW,OAAO;AAChB,SAAO;;CAGT,YAAY,SAAgC;kBAL1B;AAMhB,MAAI,SAAS,MACX,MAAK,QAAQ,QAAQ;MAErB,MAAK,QAAQ;;CAIjB,MAAM,QACJ,SAC+C;EAC/C,MAAM,EAAE,UAAU,SAAS,gBAAgB;EAI3C,MAAM,EAAE,WAAW,QAAQ,mCAAmC;EAC9D,MAAM,SAAS,IAAI,OAAO,EACxB,OAAO,KAAK,OACb,CAAC;EACF,MAAM,WACJ,SAAS,QAAQ,MAAM,EAAE,eAAe,CAAC,CACzC,KAAK,MAAM,EAAE,QAAQ;EACvB,MAAM,UAAU,MAAM,OAAO,OAAO,SAAS;AAE7C,cAAY,OAAO,OAAO,iBAAiB;GACzC,MAAM,qDAA6B;AACnC,gBAAa,qBAAqB,EAAE,WAAW,kBAAkB,CAAC;AAClE,cAAW,MAAM,aAAa,QAC5B,cAAa,uBAAuB;IAClC,WAAW;IACX,SAAS;IACV,CAAC;AAEJ,gBAAa,mBAAmB,EAAE,WAAW,kBAAkB,CAAC;AAIhE,gBAAa,UAAU;IACvB;AACF,SAAO,EACL,UAAU,QAAQ,gDAAwB,EAC3C"}