Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client/src/effect/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export type Endpoint10_2Input = {
readonly integrationID: Endpoint10_2Request["params"]["integrationID"]
readonly location?: Endpoint10_2Request["query"]["location"]
readonly key: Endpoint10_2Request["payload"]["key"]
readonly inputs?: Endpoint10_2Request["payload"]["inputs"]
readonly label?: Endpoint10_2Request["payload"]["label"]
}
export type Endpoint10_2Output = EffectValue<ReturnType<RawClient["server.integration"]["integration.connect.key"]>>
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/effect/generated/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,14 @@ type Endpoint10_2Input = {
readonly integrationID: Endpoint10_2Request["params"]["integrationID"]
readonly location?: Endpoint10_2Request["query"]["location"]
readonly key: Endpoint10_2Request["payload"]["key"]
readonly inputs?: Endpoint10_2Request["payload"]["inputs"]
readonly label?: Endpoint10_2Request["payload"]["label"]
}
const Endpoint10_2 = (raw: RawClient["server.integration"]) => (input: Endpoint10_2Input) =>
raw["integration.connect.key"]({
params: { integrationID: input["integrationID"] },
query: { location: input["location"] },
payload: { key: input["key"], label: input["label"] },
payload: { key: input["key"], inputs: input["inputs"], label: input["label"] },
}).pipe(Effect.mapError(mapClientError))

type Endpoint10_3Request = Parameters<RawClient["server.integration"]["integration.connect.oauth"]>[0]
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/promise/generated/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ export function make(options: ClientOptions) {
method: "POST",
path: `/api/integration/${encodeURIComponent(input.integrationID)}/connect/key`,
query: { location: input["location"] },
body: { key: input["key"], label: input["label"] },
body: { key: input["key"], inputs: input["inputs"], label: input["label"] },
successStatus: 204,
declaredStatuses: [400, 401],
empty: true,
Expand Down
25 changes: 21 additions & 4 deletions packages/client/src/promise/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,6 @@ export type ProviderV2Info = {

export type IntegrationWhen = { key: string; op: "eq" | "neq"; value: string }

export type IntegrationKeyMethod = { type: "key"; label?: string }

export type IntegrationEnvMethod = { type: "env"; names: Array<string> }

export type ConnectionCredentialInfo = { type: "credential"; id: string; label: string }
Expand Down Expand Up @@ -1868,6 +1866,12 @@ export type IntegrationOAuthMethod = {
prompts?: Array<IntegrationTextPrompt | IntegrationSelectPrompt>
}

export type IntegrationKeyMethod = {
type: "key"
label?: string
prompts?: Array<IntegrationTextPrompt | IntegrationSelectPrompt>
}

export type FormField =
| FormStringField
| FormNumberField
Expand Down Expand Up @@ -3276,8 +3280,21 @@ export type IntegrationConnectKeyInput = {
readonly location?: {
readonly location?: { readonly directory?: string | undefined; readonly workspace?: string | undefined } | undefined
}["location"]
readonly key: { readonly key: string; readonly label?: string | undefined }["key"]
readonly label?: { readonly key: string; readonly label?: string | undefined }["label"]
readonly key: {
readonly key: string
readonly inputs?: { readonly [x: string]: string } | undefined
readonly label?: string | undefined
}["key"]
readonly inputs?: {
readonly key: string
readonly inputs?: { readonly [x: string]: string } | undefined
readonly label?: string | undefined
}["inputs"]
readonly label?: {
readonly key: string
readonly inputs?: { readonly [x: string]: string } | undefined
readonly label?: string | undefined
}["label"]
}

export type IntegrationConnectKeyOutput = void
Expand Down
31 changes: 21 additions & 10 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface OAuthImplementation {
export interface KeyImplementation {
readonly integrationID: ID
readonly method: KeyMethod
readonly authorize?: (key: string, inputs: Inputs) => Effect.Effect<Credential.Key, unknown>
}

export interface EnvImplementation {
Expand Down Expand Up @@ -119,6 +120,7 @@ type Entry = {
ref: Types.DeepMutable<Ref>
methods: Types.DeepMutable<Method>[]
implementations: Map<MethodID, Types.DeepMutable<OAuthImplementation>>
key?: Types.DeepMutable<KeyImplementation>
}

type Data = {
Expand Down Expand Up @@ -156,6 +158,8 @@ export interface Interface extends State.Transformable<Draft> {
readonly integrationID: ID
/** Secret entered by the user. */
readonly key: string
/** Answers to the method's optional prompts. */
readonly inputs?: Inputs
/** User-facing label for the stored credential. */
readonly label?: string
}) => Effect.Effect<void, AuthorizationError>
Expand Down Expand Up @@ -237,6 +241,7 @@ const layer = Layer.effect(
ref: { id, name: id },
methods: [],
implementations: new Map(),
key: undefined,
}
if (!draft.integrations.has(id)) draft.integrations.set(id, current)
update(current.ref)
Expand All @@ -253,6 +258,7 @@ const layer = Layer.effect(
},
methods: [],
implementations: new Map<MethodID, Types.DeepMutable<OAuthImplementation>>(),
key: undefined,
}
if (!draft.integrations.has(implementation.integrationID)) {
draft.integrations.set(implementation.integrationID, current)
Expand All @@ -270,6 +276,9 @@ const layer = Layer.effect(
implementation as Types.DeepMutable<OAuthImplementation>,
)
}
if (implementation.method.type === "key") {
current.key = implementation as Types.DeepMutable<KeyImplementation>
}
},
remove: (integrationID, method) => {
const current = draft.integrations.get(integrationID)
Expand All @@ -281,6 +290,7 @@ const layer = Layer.effect(
})
if (index !== -1) current.methods.splice(index, 1)
if (method.type === "oauth") current.implementations.delete(method.id)
if (method.type === "key") current.key = undefined
},
},
}),
Expand Down Expand Up @@ -365,10 +375,10 @@ const layer = Layer.effect(
? { status: "complete", time: attempt.time, removeAt: settledAt + terminalRetention }
: {
status: "failed",
message: message(persistence.cause),
time: attempt.time,
removeAt: settledAt + terminalRetention,
}
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))
if (Exit.isFailure(persistence)) yield* Effect.failCause(persistence.cause)
Expand Down Expand Up @@ -438,15 +448,16 @@ const layer = Layer.effect(
return value
}),
key: Effect.fn("Integration.connection.key")(function* (input) {
const method = state
.get()
.integrations.get(input.integrationID)
?.methods.some((method) => method.type === "key")
if (!method) return yield* Effect.die(new Error(`Key method not found: ${input.integrationID}`))
const entry = state.get().integrations.get(input.integrationID)
const method = entry?.methods.some((method) => method.type === "key")
if (!entry || !method) return yield* Effect.die(new Error(`Key method not found: ${input.integrationID}`))
const value = entry.key?.authorize
? yield* authorize(entry.key.authorize(input.key, input.inputs ?? {}))
: Credential.Key.make({ type: "key", key: input.key })
yield* credentials.create({
integrationID: input.integrationID,
label: input.label,
value: Credential.Key.make({ type: "key", key: input.key }),
value,
})
yield* events.publish(Event.ConnectionUpdated, { integrationID: input.integrationID })
yield* events.publish(Event.Updated, {})
Expand Down
36 changes: 26 additions & 10 deletions packages/core/src/plugin/host.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export * as PluginHost from "./host"

import type { Plugin } from "@opencode-ai/plugin/v2/effect"
import type {
IntegrationInputs,
IntegrationKeyMethodRegistration,
IntegrationOAuthMethodRegistration,
} from "@opencode-ai/plugin/v2/effect/integration"
import { EventManifest } from "@opencode-ai/schema/event-manifest"
import { Effect, Schema, Stream } from "effect"
import { AgentV2 } from "../agent"
Expand Down Expand Up @@ -171,6 +176,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
integration.connection.key({
integrationID: Integration.ID.make(input.integrationID),
key: input.key,
inputs: input.inputs,
label: input.label,
}),
oauth: (input) =>
Expand Down Expand Up @@ -207,14 +213,15 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
method: {
list: (id) => mutable(draft.method.list(Integration.ID.make(id))),
update: (input) => {
if ("authorize" in input) {
const methodID = Integration.MethodID.make(input.method.id)
const refresh = input.refresh
if (input.method.type === "oauth") {
const oauth = input as IntegrationOAuthMethodRegistration
const methodID = Integration.MethodID.make(oauth.method.id)
const refresh = oauth.refresh
draft.method.update({
integrationID: Integration.ID.make(input.integrationID),
method: { ...input.method, id: methodID },
authorize: (inputs) =>
input.authorize(inputs).pipe(
integrationID: Integration.ID.make(oauth.integrationID),
method: { ...oauth.method, id: methodID },
authorize: (inputs: IntegrationInputs) =>
oauth.authorize(inputs).pipe(
Effect.map((authorization) => {
if (authorization.mode === "auto") {
return {
Expand Down Expand Up @@ -256,7 +263,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
),
}
: {}),
...(input.label ? { label: input.label } : {}),
...(oauth.label ? { label: oauth.label } : {}),
})
return
}
Expand All @@ -267,9 +274,18 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
})
return
}
const registration = input as IntegrationKeyMethodRegistration
draft.method.update({
integrationID: Integration.ID.make(input.integrationID),
method: { type: "key", label: input.method.label },
integrationID: Integration.ID.make(registration.integrationID),
method: { type: "key", label: registration.method.label, prompts: registration.method.prompts },
...(registration.authorize
? {
authorize: (key, inputs) =>
registration.authorize!(key, inputs).pipe(
Effect.map((credential) => Schema.decodeUnknownSync(Credential.Key)(credential)),
),
}
: {}),
})
},
remove: (id, method) =>
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/plugin/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CloudflareAIGatewayPlugin } from "./provider/cloudflare-ai-gateway"
import { CloudflareWorkersAIPlugin } from "./provider/cloudflare-workers-ai"
import { CoherePlugin } from "./provider/cohere"
import { DeepInfraPlugin } from "./provider/deepinfra"
import { DigitalOceanPlugin } from "./provider/digitalocean"
import { DynamicProviderPlugin } from "./provider/dynamic"
import { GatewayPlugin } from "./provider/gateway"
import { GithubCopilotPlugin } from "./provider/github-copilot"
Expand All @@ -24,6 +25,7 @@ import { OpenAICompatiblePlugin } from "./provider/openai-compatible"
import { OpencodePlugin } from "./provider/opencode"
import { OpenRouterPlugin } from "./provider/openrouter"
import { PerplexityPlugin } from "./provider/perplexity"
import { PoePlugin } from "./provider/poe"
import { SapAICorePlugin } from "./provider/sap-ai-core"
import { TogetherAIPlugin } from "./provider/togetherai"
import { VercelPlugin } from "./provider/vercel"
Expand All @@ -43,6 +45,7 @@ export const ProviderPlugins: PluginInternal.InternalPlugin[] = [
CloudflareWorkersAIPlugin,
CoherePlugin,
DeepInfraPlugin,
DigitalOceanPlugin,
GatewayPlugin,
GithubCopilotPlugin,
GitLabPlugin,
Expand All @@ -60,6 +63,7 @@ export const ProviderPlugins: PluginInternal.InternalPlugin[] = [
OpenAIPlugin,
OpenRouterPlugin,
PerplexityPlugin,
PoePlugin,
SapAICorePlugin,
TogetherAIPlugin,
VercelPlugin,
Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/plugin/provider/azure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Effect } from "effect"
import { define } from "@opencode-ai/plugin/v2/effect/plugin"
import { Credential } from "../../credential"
import { Integration } from "../../integration"
import { ProviderV2 } from "../../provider"

function selectLanguage(sdk: any, modelID: string, useChat: boolean) {
Expand All @@ -13,6 +15,34 @@ function selectLanguage(sdk: any, modelID: string, useChat: boolean) {
export const AzurePlugin = define({
id: "opencode.provider.azure",
effect: Effect.fn(function* (ctx) {
const integration = yield* Integration.Service
yield* integration.transform((draft) => {
draft.method.update({
integrationID: Integration.ID.make("azure"),
method: {
type: "key",
label: "API key",
prompts: process.env.AZURE_RESOURCE_NAME
? []
: [
{
type: "text",
key: "resourceName",
message: "Enter Azure Resource Name",
placeholder: "e.g. my-models",
},
],
},
authorize: (key, inputs) =>
Effect.succeed(
Credential.Key.make({
type: "key",
key,
...(inputs.resourceName ? { metadata: { resourceName: inputs.resourceName } } : {}),
}),
),
})
})
yield* ctx.catalog.transform((evt) => {
for (const item of evt.provider.list()) {
if (!ProviderV2.isAISDK(item.provider.package)) continue
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/plugin/provider/cloudflare-ai-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,54 @@ import os from "os"
import { InstallationVersion } from "../../installation/version"
import { Effect, Option, Schema } from "effect"
import { define } from "@opencode-ai/plugin/v2/effect/plugin"
import { Credential } from "../../credential"
import { Integration } from "../../integration"

const integrationID = Integration.ID.make("cloudflare-ai-gateway")

export const CloudflareAIGatewayPlugin = define({
id: "opencode.provider.cloudflare-ai-gateway",
effect: Effect.fn(function* (ctx) {
const integration = yield* Integration.Service
yield* integration.transform((draft) => {
draft.method.update({
integrationID,
method: {
type: "key",
label: "Gateway API token",
prompts: [
...(process.env.CLOUDFLARE_ACCOUNT_ID
? []
: [
{
type: "text" as const,
key: "accountId",
message: "Enter your Cloudflare Account ID",
placeholder: "e.g. 1234567890abcdef1234567890abcdef",
},
]),
...(process.env.CLOUDFLARE_GATEWAY_ID
? []
: [
{
type: "text" as const,
key: "gatewayId",
message: "Enter your Cloudflare AI Gateway ID",
placeholder: "e.g. my-gateway",
},
]),
],
},
authorize: (key, inputs) =>
Effect.succeed(
Credential.Key.make({
type: "key",
key,
...(Object.keys(inputs).length ? { metadata: inputs } : {}),
}),
),
})
})
yield* ctx.aisdk.hook(
"sdk",
Effect.fn(function* (evt) {
Expand Down
Loading
Loading