gitlab.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os from "os"
  2. import { InstallationVersion } from "../../installation/version"
  3. import { Effect } from "effect"
  4. import { PluginV2 } from "../../plugin"
  5. import { ProviderV2 } from "../../provider"
  6. export const GitLabPlugin = PluginV2.define({
  7. id: PluginV2.ID.make("gitlab"),
  8. effect: Effect.gen(function* () {
  9. return {
  10. "aisdk.sdk": Effect.fn(function* (evt) {
  11. if (evt.package !== "gitlab-ai-provider") return
  12. const mod = yield* Effect.promise(() => import("gitlab-ai-provider"))
  13. evt.sdk = mod.createGitLab({
  14. ...evt.options,
  15. instanceUrl:
  16. typeof evt.options.instanceUrl === "string"
  17. ? evt.options.instanceUrl
  18. : (process.env.GITLAB_INSTANCE_URL ?? "https://gitlab.com"),
  19. apiKey: typeof evt.options.apiKey === "string" ? evt.options.apiKey : process.env.GITLAB_TOKEN,
  20. aiGatewayHeaders: {
  21. "User-Agent": `opencode/${InstallationVersion} gitlab-ai-provider/${mod.VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`,
  22. "anthropic-beta": "context-1m-2025-08-07",
  23. ...evt.options.aiGatewayHeaders,
  24. },
  25. featureFlags: {
  26. duo_agent_platform_agentic_chat: true,
  27. duo_agent_platform: true,
  28. ...evt.options.featureFlags,
  29. },
  30. })
  31. }),
  32. "aisdk.language": Effect.fn(function* (evt) {
  33. if (evt.model.providerID !== ProviderV2.ID.gitlab) return
  34. const featureFlags = typeof evt.options.featureFlags === "object" && evt.options.featureFlags ? evt.options.featureFlags : {}
  35. if (evt.model.apiID.startsWith("duo-workflow-")) {
  36. const gitlab = yield* Effect.promise(() => import("gitlab-ai-provider")).pipe(Effect.orDie)
  37. const workflowRef =
  38. typeof evt.model.options.aisdk.request.workflowRef === "string"
  39. ? evt.model.options.aisdk.request.workflowRef
  40. : undefined
  41. const workflowDefinition =
  42. typeof evt.model.options.aisdk.request.workflowDefinition === "string"
  43. ? evt.model.options.aisdk.request.workflowDefinition
  44. : undefined
  45. const language = evt.sdk.workflowChat(
  46. gitlab.isWorkflowModel(evt.model.apiID) ? evt.model.apiID : "duo-workflow",
  47. {
  48. featureFlags,
  49. workflowDefinition,
  50. },
  51. )
  52. if (workflowRef) language.selectedModelRef = workflowRef
  53. evt.language = language
  54. return
  55. }
  56. evt.language = evt.sdk.agenticChat(evt.model.apiID, {
  57. aiGatewayHeaders: evt.options.aiGatewayHeaders,
  58. featureFlags,
  59. })
  60. }),
  61. }
  62. }),
  63. })