diff --git a/packages/core/src/integration.ts b/packages/core/src/integration.ts index 824d2975abd..fe616aad983 100644 --- a/packages/core/src/integration.ts +++ b/packages/core/src/integration.ts @@ -292,14 +292,9 @@ type PendingCommandAttempt = { scope: Scope.Closeable time: AttemptTime } -type TerminalCommandAttempt = { - status: "complete" | "failed" | "expired" - integrationID: ID - message?: string - removeAt: number - time: AttemptTime -} -type CommandAttemptEntry = PendingCommandAttempt | TerminalCommandAttempt +type CommandAttemptEntry = PendingCommandAttempt | TerminalAttempt + +type AttemptRef = SynchronizedRef.SynchronizedRef> const layer = Layer.effect( Service, @@ -317,65 +312,46 @@ const layer = Layer.effect( const state = State.create({ name: "integration", initial: () => ({ integrations: new Map() }), - editor: (editor) => ({ - list: () => Array.from(editor.integrations.values(), (entry) => entry.ref), - get: (id) => editor.integrations.get(id)?.ref, - update: (id, update) => { + editor: (editor) => { + const ensure = (id: ID): Entry => { const current = editor.integrations.get(id) ?? { ref: { id, name: id }, methods: [], implementations: new Map(), } if (!editor.integrations.has(id)) editor.integrations.set(id, current) - update(current.ref) - current.ref.id = id - }, - remove: (id) => editor.integrations.delete(id), - method: { - list: (integrationID) => editor.integrations.get(integrationID)?.methods ?? [], - update: (implementation) => { - const current = editor.integrations.get(implementation.integrationID) ?? { - ref: { - id: implementation.integrationID, - name: implementation.integrationID, - }, - methods: [], - implementations: new Map(), - } - if (!editor.integrations.has(implementation.integrationID)) { - editor.integrations.set(implementation.integrationID, current) - } - const index = current.methods.findIndex((method) => { - if (method.type !== implementation.method.type) return false - if (method.type === "oauth" && implementation.method.type === "oauth") - return method.id === implementation.method.id - if (method.type === "command" && implementation.method.type === "command") - return method.id === implementation.method.id - if (method.type === "external" && implementation.method.type === "external") - return method.id === implementation.method.id - return true - }) - if (index === -1) current.methods.push(implementation.method) - else current.methods[index] = implementation.method - if ("authorize" in implementation) { - current.implementations.set(implementation.method.id, implementation) - } + return current + } + return { + list: () => Array.from(editor.integrations.values(), (entry) => entry.ref), + get: (id) => editor.integrations.get(id)?.ref, + update: (id, update) => { + const current = ensure(id) + update(current.ref) + current.ref.id = id }, - remove: (integrationID, method) => { - const current = editor.integrations.get(integrationID) - if (!current) return - const index = current.methods.findIndex((candidate) => { - if (candidate.type !== method.type) return false - if (candidate.type === "oauth" && method.type === "oauth") return candidate.id === method.id - if (candidate.type === "command" && method.type === "command") return candidate.id === method.id - if (candidate.type === "external" && method.type === "external") return candidate.id === method.id - return true - }) - if (index !== -1) current.methods.splice(index, 1) - if (method.type === "oauth") current.implementations.delete(method.id) + remove: (id) => editor.integrations.delete(id), + method: { + list: (integrationID) => editor.integrations.get(integrationID)?.methods ?? [], + update: (implementation) => { + const current = ensure(implementation.integrationID) + const index = current.methods.findIndex((method) => sameMethod(method, implementation.method)) + if (index === -1) current.methods.push(implementation.method) + else current.methods[index] = implementation.method + if ("authorize" in implementation) { + current.implementations.set(implementation.method.id, implementation) + } + }, + remove: (integrationID, method) => { + const current = editor.integrations.get(integrationID) + if (!current) return + const index = current.methods.findIndex((candidate) => sameMethod(candidate, method)) + if (index !== -1) current.methods.splice(index, 1) + if (method.type === "oauth") current.implementations.delete(method.id) + }, }, - }, - }), + } + }, notify: State.changes( (data) => Array.from(data.integrations.values(), (entry) => project(entry, resolveConnections(entry, []))).toSorted( @@ -445,70 +421,20 @@ const layer = Layer.effect( return error instanceof Error ? error.message : String(error) } - const settle = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit) { + // Claims a pending attempt for settlement. A failed exit records the failure and closes the attempt. + const claim = Effect.fnUntraced(function* ( + ref: AttemptRef, + attemptID: AttemptID, + exit: Exit.Exit, + ) { const now = yield* Clock.currentTimeMillis - const attempt = yield* SynchronizedRef.modify(attempts, (current) => { + const attempt = yield* SynchronizedRef.modify(ref, (current) => { const match = current.get(attemptID) if (!match || match.status !== "pending" || match.persisting) return [undefined, current] - const next = Exit.isSuccess(exit) + const next: Pending | TerminalAttempt = Exit.isSuccess(exit) ? { ...match, persisting: true } - : { - status: "failed" as const, - integrationID: match.integrationID, - message: message(exit.cause), - time: match.time, - removeAt: now + terminalRetention, - } - return [match, new Map(current).set(attemptID, next)] - }) - if (!attempt) return - if (Exit.isFailure(exit)) { - yield* close(attempt.scope) - return - } - - yield* Effect.gen(function* () { - const persistence = yield* Effect.suspend(() => { - const implementation = state - .get() - .integrations.get(attempt.integrationID) - ?.implementations.get(attempt.methodID) - return createCredential({ - integrationID: attempt.integrationID, - label: attempt.label ?? implementation?.label?.(exit.value), - value: exit.value, - }) - }).pipe(Effect.asVoid, Effect.exit) - const settledAt = yield* Clock.currentTimeMillis - const terminal: TerminalAttempt = Exit.isSuccess(persistence) - ? { - status: "complete", - integrationID: attempt.integrationID, - time: attempt.time, - removeAt: settledAt + terminalRetention, - } : { status: "failed", - integrationID: attempt.integrationID, - message: message(persistence.cause), - time: attempt.time, - removeAt: settledAt + terminalRetention, - } - // Persisting attempts cannot be cancelled, expired, or claimed again. - yield* SynchronizedRef.update(attempts, (current) => new Map(current).set(attemptID, terminal)) - yield* persistence - }).pipe(Effect.ensuring(close(attempt.scope))) - }, Effect.uninterruptible) - - const settleCommand = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit) { - const now = yield* Clock.currentTimeMillis - const attempt = yield* SynchronizedRef.modify(commandAttempts, (current) => { - const match = current.get(attemptID) - if (!match || match.status !== "pending" || match.persisting) return [undefined, current] - const next = Exit.isSuccess(exit) - ? { ...match, persisting: true } - : { - status: "failed" as const, integrationID: match.integrationID, message: message(exit.cause), time: match.time, @@ -516,19 +442,18 @@ const layer = Layer.effect( } return [match, new Map(current).set(attemptID, next)] }) - if (!attempt) return - if (Exit.isFailure(exit)) { - yield* close(attempt.scope) - return - } + if (attempt && Exit.isFailure(exit)) yield* close(attempt.scope) + return attempt + }) - const persistence = yield* createCredential({ - integrationID: attempt.integrationID, - label: attempt.label, - value: Credential.Key.make({ type: "key", key: exit.value }), - }).pipe(Effect.asVoid, Effect.exit) + const record = Effect.fnUntraced(function* ( + ref: AttemptRef, + attemptID: AttemptID, + attempt: Pending, + persistence: Exit.Exit, + ) { const settledAt = yield* Clock.currentTimeMillis - const terminal: TerminalCommandAttempt = Exit.isSuccess(persistence) + const terminal: TerminalAttempt = Exit.isSuccess(persistence) ? { status: "complete", integrationID: attempt.integrationID, @@ -542,18 +467,53 @@ const layer = Layer.effect( time: attempt.time, removeAt: settledAt + terminalRetention, } - yield* SynchronizedRef.update(commandAttempts, (current) => new Map(current).set(attemptID, terminal)) + yield* SynchronizedRef.update(ref, (current) => new Map(current).set(attemptID, terminal)) + }) + + const settle = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit) { + const attempt = yield* claim(attempts, attemptID, exit) + if (!attempt || Exit.isFailure(exit)) return + yield* Effect.gen(function* () { + const persistence = yield* Effect.suspend(() => { + const implementation = state + .get() + .integrations.get(attempt.integrationID) + ?.implementations.get(attempt.methodID) + return createCredential({ + integrationID: attempt.integrationID, + label: attempt.label ?? implementation?.label?.(exit.value), + value: exit.value, + }) + }).pipe(Effect.asVoid, Effect.exit) + // Persisting attempts cannot be cancelled, expired, or claimed again. + yield* record(attempts, attemptID, attempt, persistence) + yield* persistence + }).pipe(Effect.ensuring(close(attempt.scope))) + }, Effect.uninterruptible) + + const settleCommand = Effect.fnUntraced(function* (attemptID: AttemptID, exit: Exit.Exit) { + const attempt = yield* claim(commandAttempts, attemptID, exit) + if (!attempt || Exit.isFailure(exit)) return + const persistence = yield* createCredential({ + integrationID: attempt.integrationID, + label: attempt.label, + value: Credential.Key.make({ type: "key", key: exit.value }), + }).pipe(Effect.asVoid, Effect.exit) + yield* record(commandAttempts, attemptID, attempt, persistence) yield* close(attempt.scope) }, Effect.uninterruptible) - const scrub = Effect.fnUntraced(function* () { + const scrub = Effect.fnUntraced(function* ( + ref: AttemptRef, + expire: (attemptID: AttemptID, attempt: Pending) => Effect.Effect, + ) { const now = yield* Clock.currentTimeMillis - const expired = yield* SynchronizedRef.modify(attempts, (current) => { + const expired = yield* SynchronizedRef.modify(ref, (current) => { const next = new Map(current) - const closed: { attemptID: AttemptID; integrationID: ID; scope: Scope.Closeable }[] = [] + const closed: [AttemptID, Pending][] = [] for (const [id, attempt] of current) { if (attempt.status === "pending" && !attempt.persisting && attempt.time.expires <= now) { - closed.push({ attemptID: id, integrationID: attempt.integrationID, scope: attempt.scope }) + closed.push([id, attempt]) next.set(id, { status: "expired", integrationID: attempt.integrationID, @@ -566,43 +526,38 @@ const layer = Layer.effect( } return [closed, next] }) - yield* Effect.forEach( - expired, - (attempt) => - Effect.logInfo("integration oauth attempt closed", { - integrationID: attempt.integrationID, - attemptID: attempt.attemptID, - reason: "expired", - }).pipe(Effect.andThen(close(attempt.scope))), - { discard: true }, - ) + yield* Effect.forEach(expired, ([id, attempt]) => expire(id, attempt), { discard: true }) }) - const scrubCommands = Effect.fnUntraced(function* () { - const now = yield* Clock.currentTimeMillis - const expired = yield* SynchronizedRef.modify(commandAttempts, (current) => { + const cancel = Effect.fnUntraced(function* ( + ref: AttemptRef, + input: { readonly integrationID: ID; readonly attemptID: AttemptID }, + before: Effect.Effect = Effect.void, + ) { + const attempt = yield* SynchronizedRef.modify(ref, (current) => { + const match = current.get(input.attemptID) + if (!match || match.integrationID !== input.integrationID || match.status !== "pending" || match.persisting) + return [undefined, current] const next = new Map(current) - const scopes: Scope.Closeable[] = [] - for (const [id, attempt] of current) { - if (attempt.status === "pending" && !attempt.persisting && attempt.time.expires <= now) { - scopes.push(attempt.scope) - next.set(id, { - status: "expired", - integrationID: attempt.integrationID, - time: attempt.time, - removeAt: now + terminalRetention, - }) - continue - } - if (attempt.status !== "pending" && attempt.removeAt <= now) next.delete(id) - } - return [scopes, next] + next.delete(input.attemptID) + return [match, next] }) - yield* Effect.forEach(expired, close, { discard: true }) + if (!attempt) return + yield* before + yield* Scope.close(attempt.scope, Exit.void) }) - yield* scrub().pipe(Effect.repeat(Schedule.spaced(scrubInterval)), Effect.forkIn(scope)) - yield* scrubCommands().pipe(Effect.repeat(Schedule.spaced(scrubInterval)), Effect.forkIn(scope)) + yield* scrub(attempts, (attemptID, attempt) => + Effect.logInfo("integration oauth attempt closed", { + integrationID: attempt.integrationID, + attemptID, + reason: "expired", + }).pipe(Effect.andThen(close(attempt.scope))), + ).pipe(Effect.repeat(Schedule.spaced(scrubInterval)), Effect.forkIn(scope)) + yield* scrub(commandAttempts, (_, attempt) => close(attempt.scope)).pipe( + Effect.repeat(Schedule.spaced(scrubInterval)), + Effect.forkIn(scope), + ) const connectOAuth = Effect.fn("Integration.oauth.connect")(function* (input: { readonly integrationID: ID @@ -615,10 +570,8 @@ const layer = Layer.effect( return yield* Effect.die(new Error(`OAuth method not found: ${input.integrationID}/${input.methodID}`)) } const answer = input.answer ?? {} - if (method.method.form) { - const invalid = Form.validateFields(method.method.form) ?? Form.validateAnswer(method.method.form, answer) - if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) - } + const invalid = answerError(method.method.form, answer) + if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) const attemptScope = yield* Scope.fork(scope) const authorization = yield* authorize(method.authorize(answer)).pipe( Scope.provide(attemptScope), @@ -779,13 +732,8 @@ const layer = Layer.effect( ?.methods.find((method) => method.type === "key") if (!method) return yield* Effect.die(new Error(`Key method not found: ${input.integrationID}`)) const answer = input.answer ?? {} - if (method.form) { - const invalid = Form.validateFields(method.form) ?? Form.validateAnswer(method.form, answer) - if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) - } - if (!method.form && Object.keys(answer).length > 0) { - return yield* new AuthorizationError({ cause: new Error("Key method does not accept a form answer") }) - } + const invalid = answerError(method.form, answer, "Key") + if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) yield* createCredential({ integrationID: input.integrationID, label: input.label, @@ -804,13 +752,8 @@ const layer = Layer.effect( if (method?.type !== "external") return yield* new AuthorizationError({ cause: new Error(`External method not found: ${input.methodID}`) }) const answer = input.answer ?? {} - if (method.form) { - const invalid = Form.validateFields(method.form) ?? Form.validateAnswer(method.form, answer) - if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) - } - if (!method.form && Object.keys(answer).length > 0) { - return yield* new AuthorizationError({ cause: new Error("External method does not accept a form answer") }) - } + const invalid = answerError(method.form, answer, "External") + if (invalid) return yield* new AuthorizationError({ cause: new Error(invalid) }) yield* createCredential({ integrationID: input.integrationID, label: input.label, @@ -867,23 +810,17 @@ const layer = Layer.effect( yield* settle(input.attemptID, exit) if (Exit.isFailure(exit)) return yield* exit }), - cancel: Effect.fn("Integration.oauth.cancel")(function* (input) { - const attempt = yield* SynchronizedRef.modify(attempts, (current) => { - const match = current.get(input.attemptID) - if (!match || match.integrationID !== input.integrationID || match.status !== "pending" || match.persisting) - return [undefined, current] - const next = new Map(current) - next.delete(input.attemptID) - return [match, next] - }) - if (!attempt) return - yield* Effect.logInfo("integration oauth attempt closed", { - integrationID: input.integrationID, - attemptID: input.attemptID, - reason: "cancelled", - }) - yield* Scope.close(attempt.scope, Exit.void) - }), + cancel: Effect.fn("Integration.oauth.cancel")((input) => + cancel( + attempts, + input, + Effect.logInfo("integration oauth attempt closed", { + integrationID: input.integrationID, + attemptID: input.attemptID, + reason: "cancelled", + }), + ), + ), }, command: { connect: connectCommand, @@ -902,22 +839,20 @@ const layer = Layer.effect( } return { status: attempt.status, time: attempt.time } }), - cancel: Effect.fn("Integration.command.cancel")(function* (input) { - const attempt = yield* SynchronizedRef.modify(commandAttempts, (current) => { - const match = current.get(input.attemptID) - if (!match || match.integrationID !== input.integrationID || match.status !== "pending" || match.persisting) - return [undefined, current] - const next = new Map(current) - next.delete(input.attemptID) - return [match, next] - }) - if (attempt) yield* Scope.close(attempt.scope, Exit.void) - }), + cancel: Effect.fn("Integration.command.cancel")((input) => cancel(commandAttempts, input)), }, }) }), ) +const sameMethod = (a: Method, b: Method) => a.type === b.type && (!("id" in a) || !("id" in b) || a.id === b.id) + +// Methods without a form reject answers when named; OAuth ignores answers it has no form for. +const answerError = (form: ReadonlyArray | undefined, answer: Form.Answer, method?: string) => { + if (form) return Form.validateFields(form) ?? Form.validateAnswer(form, answer) + if (method && Object.keys(answer).length > 0) return `${method} method does not accept a form answer` +} + export const node = makeLocationNode({ service: Service, layer,