diff --git a/packages/client/src/promise/api.ts b/packages/client/src/promise/api.ts index 2c4c1cde121..ed9b4bd9570 100644 --- a/packages/client/src/promise/api.ts +++ b/packages/client/src/promise/api.ts @@ -7,6 +7,7 @@ export type { RpcApi, RpcCallOptions, RpcClient, RpcEventPayload } from "./rpc.j export type AgentApi = Client["agent"] export type CommandApi = Client["command"] export type ConfigApi = Client["config"] +export type CredentialApi = Client["credential"] export type EventApi = Client["event"] export type GenerateApi = Client["generate"] export type IntegrationApi = Client["integration"] diff --git a/packages/core/src/plugin/host.ts b/packages/core/src/plugin/host.ts index d6d2781ad2f..7477c16ff52 100644 --- a/packages/core/src/plugin/host.ts +++ b/packages/core/src/plugin/host.ts @@ -252,6 +252,9 @@ export const make = Effect.fn("PluginHost.make")(function* ( reload: commands.reload, transform: commands.transform, }, + credential: { + activate: (input) => integration.connection.activate(input.credentialID), + }, event: { subscribe: () => bus diff --git a/packages/core/test/plugin.test.ts b/packages/core/test/plugin.test.ts index 74fd92ead9a..96723ea86a5 100644 --- a/packages/core/test/plugin.test.ts +++ b/packages/core/test/plugin.test.ts @@ -608,3 +608,39 @@ it.effect("refreshes expired OAuth credentials through the context during activa expect(refreshes).toEqual([expired]) }), ) + +it.effect("activates a saved credential through the context", () => + Effect.gen(function* () { + const plugins = yield* Plugin.Service + const credentials = yield* Credential.Service + const integrations = yield* Integration.Service + const integrationID = Integration.ID.make("activate-fixture") + const first = yield* credentials.create({ + integrationID, + label: "First", + value: Credential.Key.make({ type: "key", key: "first" }), + }) + yield* credentials.create({ + integrationID, + label: "Second", + value: Credential.Key.make({ type: "key", key: "second" }), + }) + expect(yield* integrations.connection.active(integrationID)).toMatchObject({ label: "Second" }) + + yield* plugins.activate([ + { + ...fromPromise({ + id: "activate-credential", + async setup(ctx) { + await ctx.credential.activate({ credentialID: first.id }) + }, + }), + revision: "1", + }, + ]) + yield* plugins.awaitActivation + + expect(yield* plugins.list()).toMatchObject([{ id: "activate-credential", state: { status: "active" } }]) + expect(yield* integrations.connection.active(integrationID)).toMatchObject({ id: first.id, label: "First" }) + }), +) diff --git a/packages/core/test/plugin/host.ts b/packages/core/test/plugin/host.ts index 108846d5acd..00838dcaf15 100644 --- a/packages/core/test/plugin/host.ts +++ b/packages/core/test/plugin/host.ts @@ -62,6 +62,9 @@ export function host(overrides: Overrides = {}): Plugin.Context { transform: () => Effect.die("unused command.transform"), reload: () => Effect.die("unused command.reload"), }, + credential: overrides.credential ?? { + activate: () => Effect.die("unused credential.activate"), + }, event: overrides.event ?? { subscribe: () => Stream.empty, }, diff --git a/packages/plugin/src/effect/plugin.ts b/packages/plugin/src/effect/plugin.ts index b19fac7e572..3ed3b699911 100644 --- a/packages/plugin/src/effect/plugin.ts +++ b/packages/plugin/src/effect/plugin.ts @@ -1,4 +1,4 @@ -import type { ExperimentalApi, GenerateApi, PluginApi } from "@opencode/client/effect/api" +import type { CredentialApi, ExperimentalApi, GenerateApi, PluginApi } from "@opencode/client/effect/api" import type { Location } from "@opencode/schema/location" import type { Effect, Scope } from "effect" import type { PluginOptions } from "../options.js" @@ -30,6 +30,7 @@ export interface Context { readonly agent: AgentDomain readonly aisdk: AISDKDomain readonly command: CommandDomain + readonly credential: Pick, "activate"> readonly event: EventDomain readonly experimental: { readonly terminal: Pick["persistentPty"], "read"> diff --git a/packages/plugin/src/promise/adapter.ts b/packages/plugin/src/promise/adapter.ts index 7ef0a264eb0..76b40b3e430 100644 --- a/packages/plugin/src/promise/adapter.ts +++ b/packages/plugin/src/promise/adapter.ts @@ -223,6 +223,7 @@ export function fromPromise(plugin: Plugin) { ) const AgentEndpoints = ClientApi.groups["server.agent"].endpoints const CommandEndpoints = ClientApi.groups["server.command"].endpoints + const CredentialEndpoints = ClientApi.groups["server.credential"].endpoints const ExperimentalEndpoints = ClientApi.groups["server.experimental"].endpoints const GenerateEndpoints = ClientApi.groups["server.generate"].endpoints const IntegrationEndpoints = ClientApi.groups["server.integration"].endpoints @@ -321,6 +322,9 @@ export function fromPromise(plugin: Plugin) { ), reload: () => run(host.command.reload()), }, + credential: { + activate: adaptApiMethod(CredentialEndpoints["credential.activate"], host.credential.activate), + }, event: { subscribe: (options) => streams( diff --git a/packages/plugin/src/promise/plugin.ts b/packages/plugin/src/promise/plugin.ts index ed94b093020..3bb8ddf4eff 100644 --- a/packages/plugin/src/promise/plugin.ts +++ b/packages/plugin/src/promise/plugin.ts @@ -1,5 +1,5 @@ import type { OpenCodeClient } from "@opencode/client" -import type { GenerateApi, PluginApi } from "@opencode/client/promise/api" +import type { CredentialApi, GenerateApi, PluginApi } from "@opencode/client/promise/api" import type { Location } from "@opencode/schema/location" import type { PluginOptions } from "../options.js" import type { App } from "../app.js" @@ -30,6 +30,7 @@ export interface Context { readonly agent: AgentDomain readonly aisdk: AISDKDomain readonly command: CommandDomain + readonly credential: Pick readonly event: EventDomain readonly experimental: { readonly terminal: Pick diff --git a/services/www/src/docs/content/build/plugins/index.mdx b/services/www/src/docs/content/build/plugins/index.mdx index 4d30eb234f5..06ff5174812 100644 --- a/services/www/src/docs/content/build/plugins/index.mdx +++ b/services/www/src/docs/content/build/plugins/index.mdx @@ -591,6 +591,22 @@ interface IntegrationEditor { } ``` +### Credentials + +Make a saved credential the active one for its integration. + +```ts +await ctx.credential.activate({ credentialID }) +``` + +#### Reference + +```ts +interface CredentialContext { + activate(input: CredentialActivateInput, requestOptions?: RequestOptions): Promise +} +``` + ### MCP List MCP servers and their current connection state.