mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 15:28:48 +03:00
feat(plugin): expose credential.activate to server plugins (#52797)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
06425d2bd7
commit
d78267503d
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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" })
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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<CredentialApi<unknown>, "activate">
|
||||
readonly event: EventDomain
|
||||
readonly experimental: {
|
||||
readonly terminal: Pick<ExperimentalApi<unknown>["persistentPty"], "read">
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<CredentialApi, "activate">
|
||||
readonly event: EventDomain
|
||||
readonly experimental: {
|
||||
readonly terminal: Pick<OpenCodeClient["experimental"]["persistentPty"], "read">
|
||||
|
||||
@@ -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<void>
|
||||
}
|
||||
```
|
||||
|
||||
### MCP
|
||||
|
||||
List MCP servers and their current connection state.
|
||||
|
||||
Reference in New Issue
Block a user