integration.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. export * as Integration from "./integration"
  2. import {
  3. Cause,
  4. Clock,
  5. Context,
  6. Duration,
  7. Effect,
  8. Exit,
  9. Layer,
  10. Schedule,
  11. Schema,
  12. Scope,
  13. SynchronizedRef,
  14. Types,
  15. } from "effect"
  16. import { Integration } from "@opencode-ai/schema/integration"
  17. import { Credential } from "./credential"
  18. import { State } from "./state"
  19. import { EventV2 } from "./event"
  20. import { IntegrationConnection } from "./integration/connection"
  21. export const ID = Integration.ID
  22. export type ID = Integration.ID
  23. export const MethodID = Integration.MethodID
  24. export type MethodID = Integration.MethodID
  25. export const AttemptID = Integration.AttemptID
  26. export type AttemptID = typeof AttemptID.Type
  27. export const When = Integration.When
  28. export type When = Integration.When
  29. export const TextPrompt = Integration.TextPrompt
  30. export type TextPrompt = Integration.TextPrompt
  31. export const SelectPrompt = Integration.SelectPrompt
  32. export type SelectPrompt = Integration.SelectPrompt
  33. export const Prompt = Integration.Prompt
  34. export type Prompt = Integration.Prompt
  35. export const OAuthMethod = Integration.OAuthMethod
  36. export type OAuthMethod = Integration.OAuthMethod
  37. export const KeyMethod = Integration.KeyMethod
  38. export type KeyMethod = Integration.KeyMethod
  39. export const EnvMethod = Integration.EnvMethod
  40. export type EnvMethod = Integration.EnvMethod
  41. export const Method = Integration.Method
  42. export type Method = Integration.Method
  43. export const Info = Integration.Info
  44. export type Info = Integration.Info
  45. export const Inputs = Integration.Inputs
  46. export type Inputs = Integration.Inputs
  47. export type OAuthAuthorization = {
  48. readonly url: string
  49. readonly instructions: string
  50. } & (
  51. | {
  52. readonly mode: "auto"
  53. readonly callback: Effect.Effect<Credential.OAuth, unknown>
  54. }
  55. | {
  56. readonly mode: "code"
  57. readonly callback: (code: string) => Effect.Effect<Credential.OAuth, unknown>
  58. }
  59. )
  60. export interface OAuthImplementation {
  61. readonly integrationID: ID
  62. readonly method: OAuthMethod
  63. readonly authorize: (inputs: Inputs) => Effect.Effect<OAuthAuthorization, unknown, Scope.Scope>
  64. readonly refresh?: (credential: Credential.OAuth) => Effect.Effect<Credential.OAuth, unknown>
  65. readonly label?: (credential: Credential.OAuth) => string | undefined
  66. }
  67. export interface KeyImplementation {
  68. readonly integrationID: ID
  69. readonly method: KeyMethod
  70. }
  71. export interface EnvImplementation {
  72. readonly integrationID: ID
  73. readonly method: EnvMethod
  74. }
  75. export type Implementation = OAuthImplementation | KeyImplementation | EnvImplementation
  76. export const Attempt = Integration.Attempt
  77. export type Attempt = Integration.Attempt
  78. export const AttemptStatus = Integration.AttemptStatus
  79. export type AttemptStatus = typeof AttemptStatus.Type
  80. export class CodeRequiredError extends Schema.TaggedErrorClass<CodeRequiredError>()("Integration.CodeRequired", {
  81. attemptID: AttemptID,
  82. }) {}
  83. export class AuthorizationError extends Schema.TaggedErrorClass<AuthorizationError>()("Integration.Authorization", {
  84. cause: Schema.Defect(),
  85. }) {}
  86. export type Error = CodeRequiredError | AuthorizationError
  87. export const Event = Integration.Event
  88. export const Ref = Integration.Ref
  89. export type Ref = Integration.Ref
  90. type Entry = {
  91. ref: Types.DeepMutable<Ref>
  92. methods: Types.DeepMutable<Method>[]
  93. implementations: Map<MethodID, Types.DeepMutable<OAuthImplementation>>
  94. }
  95. type Data = {
  96. integrations: Map<ID, Entry>
  97. }
  98. export type Draft = {
  99. list: () => readonly Ref[]
  100. get: (id: ID) => Ref | undefined
  101. update: (id: ID, update: (integration: Types.DeepMutable<Ref>) => void) => void
  102. remove: (id: ID) => void
  103. method: {
  104. list: (integrationID: ID) => readonly Method[]
  105. update: (implementation: Implementation) => void
  106. remove: (integrationID: ID, method: Method) => void
  107. }
  108. }
  109. export interface Interface extends State.Transformable<Draft> {
  110. /** Registers a scoped transform over the integration registry. */
  111. /** Returns one integration with its methods and current connections. */
  112. readonly get: (id: ID) => Effect.Effect<Info | undefined>
  113. /** Returns all integrations with their methods and current connections. */
  114. readonly list: () => Effect.Effect<Info[]>
  115. readonly connection: {
  116. /** Returns the active connection for one integration. */
  117. readonly active: (id: ID) => Effect.Effect<IntegrationConnection.Info | undefined>
  118. /** Resolves a connection into usable credential material. */
  119. readonly resolve: (
  120. connection: IntegrationConnection.Info,
  121. ) => Effect.Effect<Credential.Value | undefined, AuthorizationError>
  122. /** Runs a key method and stores the resulting credential. */
  123. readonly key: (input: {
  124. /** Integration receiving the credential. */
  125. readonly integrationID: ID
  126. /** Secret entered by the user. */
  127. readonly key: string
  128. /** User-facing label for the stored credential. */
  129. readonly label?: string
  130. }) => Effect.Effect<void, AuthorizationError>
  131. /** Starts a stateful OAuth attempt. */
  132. readonly oauth: (input: {
  133. /** Integration being authenticated. */
  134. readonly integrationID: ID
  135. /** OAuth method selected by the caller. */
  136. readonly methodID: MethodID
  137. /** Answers to the method's optional prompts. */
  138. readonly inputs: Inputs
  139. /** User-facing label for the credential created on completion. */
  140. readonly label?: string
  141. }) => Effect.Effect<Attempt, AuthorizationError>
  142. /** Updates a stored credential exposed as a connection. */
  143. readonly update: (
  144. credentialID: Credential.ID,
  145. updates: Partial<Pick<Credential.Info, "label">>,
  146. ) => Effect.Effect<void>
  147. /** Removes a stored credential connection. */
  148. readonly remove: (credentialID: Credential.ID) => Effect.Effect<void>
  149. }
  150. readonly attempt: {
  151. /** Returns the current state of an OAuth attempt. */
  152. readonly status: (attemptID: AttemptID) => Effect.Effect<AttemptStatus>
  153. /** Completes the attempt and stores its credential. */
  154. readonly complete: (input: {
  155. /** Opaque handle returned by `oauth`. */
  156. readonly attemptID: AttemptID
  157. /** Authorization code required by attempts in code mode. */
  158. readonly code?: string
  159. }) => Effect.Effect<void, CodeRequiredError | AuthorizationError>
  160. /** Cancels an attempt and releases its resources. */
  161. readonly cancel: (attemptID: AttemptID) => Effect.Effect<void>
  162. }
  163. }
  164. export class Service extends Context.Service<Service, Interface>()("@opencode/v2/Integration") {}
  165. const attemptLifetime = Duration.toMillis(Duration.minutes(10))
  166. const terminalRetention = Duration.toMillis(Duration.minutes(1))
  167. const scrubInterval = Duration.seconds(30)
  168. type AttemptTime = { created: number; expires: number }
  169. type PendingAttempt = {
  170. status: "pending"
  171. completing: boolean
  172. authorization: OAuthAuthorization
  173. integrationID: ID
  174. methodID: MethodID
  175. label?: string
  176. scope: Scope.Closeable
  177. time: AttemptTime
  178. }
  179. type TerminalAttempt = {
  180. status: "complete" | "failed" | "expired"
  181. message?: string
  182. removeAt: number
  183. time: AttemptTime
  184. }
  185. type AttemptEntry = PendingAttempt | TerminalAttempt
  186. export const locationLayer = Layer.effect(
  187. Service,
  188. Effect.gen(function* () {
  189. const credentials = yield* Credential.Service
  190. const events = yield* EventV2.Service
  191. const scope = yield* Scope.Scope
  192. const attempts = SynchronizedRef.makeUnsafe(new Map<AttemptID, AttemptEntry>())
  193. const state = State.create<Data, Draft>({
  194. initial: () => ({ integrations: new Map<ID, Entry>() }),
  195. draft: (draft) => ({
  196. list: () => Array.from(draft.integrations.values(), (entry) => entry.ref) as Ref[],
  197. get: (id) => draft.integrations.get(id)?.ref as Ref | undefined,
  198. update: (id, update) => {
  199. const current = draft.integrations.get(id) ?? {
  200. ref: { id, name: id },
  201. methods: [],
  202. implementations: new Map(),
  203. }
  204. if (!draft.integrations.has(id)) draft.integrations.set(id, current)
  205. update(current.ref)
  206. current.ref.id = id
  207. },
  208. remove: (id) => draft.integrations.delete(id),
  209. method: {
  210. list: (integrationID) => (draft.integrations.get(integrationID)?.methods as Method[] | undefined) ?? [],
  211. update: (implementation) => {
  212. const current = draft.integrations.get(implementation.integrationID) ?? {
  213. ref: {
  214. id: implementation.integrationID,
  215. name: implementation.integrationID,
  216. },
  217. methods: [],
  218. implementations: new Map<MethodID, Types.DeepMutable<OAuthImplementation>>(),
  219. }
  220. if (!draft.integrations.has(implementation.integrationID)) {
  221. draft.integrations.set(implementation.integrationID, current)
  222. }
  223. const index = current.methods.findIndex((method) => {
  224. if (method.type !== implementation.method.type) return false
  225. if (method.type !== "oauth" || implementation.method.type !== "oauth") return true
  226. return method.id === implementation.method.id
  227. })
  228. if (index === -1) current.methods.push(implementation.method as Types.DeepMutable<Method>)
  229. else current.methods[index] = implementation.method as Types.DeepMutable<Method>
  230. if (implementation.method.type === "oauth") {
  231. current.implementations.set(
  232. implementation.method.id,
  233. implementation as Types.DeepMutable<OAuthImplementation>,
  234. )
  235. }
  236. },
  237. remove: (integrationID, method) => {
  238. const current = draft.integrations.get(integrationID)
  239. if (!current) return
  240. const index = current.methods.findIndex((candidate) => {
  241. if (candidate.type !== method.type) return false
  242. if (candidate.type !== "oauth" || method.type !== "oauth") return true
  243. return candidate.id === method.id
  244. })
  245. if (index !== -1) current.methods.splice(index, 1)
  246. if (method.type === "oauth") current.implementations.delete(method.id)
  247. },
  248. },
  249. }),
  250. finalize: () => events.publish(Event.Updated, {}).pipe(Effect.asVoid),
  251. })
  252. const resolveConnections = (entry: Entry | undefined, saved: readonly Credential.Info[]) => {
  253. const credentials = saved
  254. .map((credential) => ({
  255. type: "credential" as const,
  256. id: credential.id,
  257. label: credential.label,
  258. }))
  259. .toReversed()
  260. const env = (entry?.methods ?? [])
  261. .filter((method) => method.type === "env")
  262. .flatMap((method) => method.names.filter((name) => process.env[name]))
  263. .map((name) => ({ type: "env" as const, name }))
  264. return [...credentials, ...env]
  265. }
  266. const project = (entry: Entry, connections: IntegrationConnection.Info[]) =>
  267. new Info({
  268. id: entry.ref.id,
  269. name: entry.ref.name,
  270. methods: entry.methods,
  271. connections,
  272. })
  273. const authorize = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
  274. effect.pipe(Effect.mapError((cause) => new AuthorizationError({ cause })))
  275. const close = (attemptScope: Scope.Closeable) =>
  276. Scope.close(attemptScope, Exit.void).pipe(Effect.forkIn(scope, { startImmediately: true }), Effect.asVoid)
  277. const message = (cause: Cause.Cause<unknown>) => {
  278. const error = Cause.squash(cause)
  279. return error instanceof Error ? error.message : String(error)
  280. }
  281. const settle = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit<Credential.OAuth, unknown>) {
  282. const now = yield* Clock.currentTimeMillis
  283. const result = yield* SynchronizedRef.modify(attempts, (current) => {
  284. const attempt = current.get(attemptID)
  285. if (!attempt || attempt.status !== "pending") return [undefined, current]
  286. const terminal: TerminalAttempt = Exit.isSuccess(exit)
  287. ? { status: "complete", time: attempt.time, removeAt: now + terminalRetention }
  288. : { status: "failed", message: message(exit.cause), time: attempt.time, removeAt: now + terminalRetention }
  289. return [attempt, new Map(current).set(attemptID, terminal)]
  290. })
  291. if (!result) return
  292. if (Exit.isSuccess(exit)) {
  293. const implementation = state.get().integrations.get(result.integrationID)?.implementations.get(result.methodID)
  294. yield* credentials.create({
  295. integrationID: result.integrationID,
  296. label: result.label ?? implementation?.label?.(exit.value),
  297. value: exit.value,
  298. })
  299. yield* events.publish(Event.ConnectionUpdated, { integrationID: result.integrationID })
  300. yield* events.publish(Event.Updated, {})
  301. }
  302. yield* close(result.scope)
  303. })
  304. const scrub = Effect.fnUntraced(function* () {
  305. const now = yield* Clock.currentTimeMillis
  306. const expired = yield* SynchronizedRef.modify(attempts, (current) => {
  307. const next = new Map(current)
  308. const scopes: Scope.Closeable[] = []
  309. for (const [id, attempt] of current) {
  310. if (attempt.status === "pending" && attempt.time.expires <= now) {
  311. scopes.push(attempt.scope)
  312. next.set(id, { status: "expired", time: attempt.time, removeAt: now + terminalRetention })
  313. continue
  314. }
  315. if (attempt.status !== "pending" && attempt.removeAt <= now) next.delete(id)
  316. }
  317. return [scopes, next]
  318. })
  319. yield* Effect.forEach(expired, close, { discard: true })
  320. })
  321. yield* scrub().pipe(Effect.repeat(Schedule.spaced(scrubInterval)), Effect.forkIn(scope))
  322. return Service.of({
  323. transform: state.transform,
  324. reload: state.reload,
  325. get: Effect.fn("Integration.get")(function* (id) {
  326. const entry = state.get().integrations.get(id)
  327. if (!entry) return undefined
  328. return project(entry, resolveConnections(entry, yield* credentials.list(id)))
  329. }),
  330. list: Effect.fn("Integration.list")(function* () {
  331. const saved = Map.groupBy(yield* credentials.all(), (credential) => credential.integrationID)
  332. return Array.from(state.get().integrations.values(), (entry) =>
  333. project(entry, resolveConnections(entry, saved.get(entry.ref.id) ?? [])),
  334. ).toSorted((a, b) => a.name.localeCompare(b.name))
  335. }),
  336. connection: {
  337. active: Effect.fn("Integration.connection.active")(function* (id) {
  338. const entry = state.get().integrations.get(id)
  339. return resolveConnections(entry, yield* credentials.list(id))[0]
  340. }),
  341. resolve: Effect.fn("Integration.connection.resolve")(function* (connection) {
  342. if (connection.type === "env") {
  343. const key = process.env[connection.name]
  344. return key ? Credential.Key.make({ type: "key", key }) : undefined
  345. }
  346. const credential = yield* credentials.get(connection.id)
  347. if (!credential) return undefined
  348. if (credential.value.type === "key") return credential.value
  349. const implementation = state
  350. .get()
  351. .integrations.get(credential.integrationID)
  352. ?.implementations.get(credential.value.methodID)
  353. if (!implementation?.refresh) return credential.value
  354. const now = yield* Clock.currentTimeMillis
  355. if (credential.value.expires > now + Duration.toMillis(Duration.minutes(5))) return credential.value
  356. const value = yield* authorize(implementation.refresh(credential.value))
  357. yield* credentials.update(credential.id, { value })
  358. return value
  359. }),
  360. key: Effect.fn("Integration.connection.key")(function* (input) {
  361. const method = state
  362. .get()
  363. .integrations.get(input.integrationID)
  364. ?.methods.some((method) => method.type === "key")
  365. if (!method) return yield* Effect.die(`Key method not found: ${input.integrationID}`)
  366. yield* credentials.create({
  367. integrationID: input.integrationID,
  368. label: input.label,
  369. value: Credential.Key.make({ type: "key", key: input.key }),
  370. })
  371. yield* events.publish(Event.ConnectionUpdated, { integrationID: input.integrationID })
  372. yield* events.publish(Event.Updated, {})
  373. }),
  374. oauth: Effect.fn("Integration.connection.oauth")(function* (input) {
  375. const method = state.get().integrations.get(input.integrationID)?.implementations.get(input.methodID)
  376. if (!method) {
  377. return yield* Effect.die(`OAuth method not found: ${input.integrationID}/${input.methodID}`)
  378. }
  379. const attemptScope = yield* Scope.fork(scope)
  380. const authorization = yield* authorize(method.authorize(input.inputs)).pipe(
  381. Scope.provide(attemptScope),
  382. Effect.onExit((exit) => (Exit.isFailure(exit) ? Scope.close(attemptScope, exit) : Effect.void)),
  383. )
  384. const id = AttemptID.create()
  385. const created = yield* Clock.currentTimeMillis
  386. const time = { created, expires: created + attemptLifetime }
  387. yield* SynchronizedRef.update(attempts, (current) =>
  388. new Map(current).set(id, {
  389. status: "pending",
  390. completing: authorization.mode === "auto",
  391. authorization,
  392. integrationID: input.integrationID,
  393. methodID: input.methodID,
  394. label: input.label,
  395. scope: attemptScope,
  396. time,
  397. }),
  398. )
  399. if (authorization.mode === "auto") {
  400. yield* authorization.callback.pipe(
  401. Effect.exit,
  402. Effect.flatMap((exit) => settle(id, exit)),
  403. Effect.forkIn(attemptScope, { startImmediately: true }),
  404. )
  405. }
  406. return new Attempt({
  407. attemptID: id,
  408. url: authorization.url,
  409. instructions: authorization.instructions,
  410. mode: authorization.mode,
  411. time,
  412. })
  413. }),
  414. update: Effect.fn("Integration.connection.update")(function* (credentialID, updates) {
  415. const credential = yield* credentials.get(credentialID)
  416. yield* credentials.update(credentialID, updates)
  417. if (credential) {
  418. yield* events.publish(Event.ConnectionUpdated, { integrationID: credential.integrationID })
  419. }
  420. yield* events.publish(Event.Updated, {})
  421. }),
  422. remove: Effect.fn("Integration.connection.remove")(function* (credentialID) {
  423. const credential = yield* credentials.get(credentialID)
  424. yield* credentials.remove(credentialID)
  425. if (credential) {
  426. yield* events.publish(Event.ConnectionUpdated, { integrationID: credential.integrationID })
  427. }
  428. yield* events.publish(Event.Updated, {})
  429. }),
  430. },
  431. attempt: {
  432. status: Effect.fn("Integration.attempt.status")(function* (attemptID) {
  433. const attempt = (yield* SynchronizedRef.get(attempts)).get(attemptID)
  434. if (!attempt) return yield* Effect.die(`OAuth attempt not found: ${attemptID}`)
  435. if (attempt.status === "failed") {
  436. return { status: attempt.status, message: attempt.message ?? "Authorization failed", time: attempt.time }
  437. }
  438. return { status: attempt.status, time: attempt.time }
  439. }),
  440. complete: Effect.fn("Integration.attempt.complete")(function* (input) {
  441. const attempt = yield* SynchronizedRef.modify(attempts, (current) => {
  442. const match = current.get(input.attemptID)
  443. if (!match || match.status !== "pending" || match.completing) return [match, current]
  444. if (match.authorization.mode === "code" && input.code === undefined) return [match, current]
  445. return [match, new Map(current).set(input.attemptID, { ...match, completing: true })]
  446. })
  447. if (!attempt) return yield* Effect.die(`OAuth attempt not found: ${input.attemptID}`)
  448. if (attempt.status !== "pending") return
  449. if (attempt.authorization.mode === "code" && input.code === undefined) {
  450. return yield* new CodeRequiredError({ attemptID: input.attemptID })
  451. }
  452. if (attempt.completing) return yield* Effect.die(`OAuth attempt already completing: ${input.attemptID}`)
  453. const callback =
  454. attempt.authorization.mode === "auto"
  455. ? attempt.authorization.callback
  456. : attempt.authorization.callback(input.code as string)
  457. const exit = yield* authorize(callback).pipe(Effect.exit)
  458. yield* settle(input.attemptID, exit)
  459. if (Exit.isFailure(exit)) return yield* exit
  460. }),
  461. cancel: Effect.fn("Integration.attempt.cancel")(function* (attemptID) {
  462. const attempt = yield* SynchronizedRef.modify(attempts, (current) => {
  463. const match = current.get(attemptID)
  464. if (!match || match.status !== "pending") return [undefined, current]
  465. const next = new Map(current)
  466. next.delete(attemptID)
  467. return [match, next]
  468. })
  469. if (attempt) yield* Scope.close(attempt.scope, Exit.void)
  470. }),
  471. },
  472. })
  473. }),
  474. )