feat(plugin): provide SDK v2#12042
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: PR #8380: feat(plugin): use SDK v2
This PR appears to be related as it addresses the same feature (SDK v2 plugin support). You may want to review its status and any discussion to understand how it relates to the current PR #12042. |
|
The bot is right, it's the same PR. I changed base repos so I can maintain my fork easier. |
680c143 to
e6d4558
Compare
|
Until this is merged, I have found a very hacky solution: import { OpencodeClient as V2OpencodeClient } from "@opencode-ai/sdk/v2";
type V2CtorInput = NonNullable<ConstructorParameters<typeof V2OpencodeClient>[0]>;
type V2RawClient = V2CtorInput["client"];
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function getSharedRawClient(value: unknown): V2RawClient | undefined {
if (!isRecord(value)) {
return undefined;
}
const rawClient = value["_client"];
if (!isRecord(rawClient)) {
return undefined;
}
return rawClient as V2RawClient;
}
/**
* Attempts to extract a v2 OpencodeClient from plugin context client.
*
* The plugin system currently exposes v1 client structure which contains
* an internal `_client` property. This helper bridges to v2 by wrapping
* that raw client in a v2 OpencodeClient instance.
*
* @param client - Plugin ctx.client (v1 structure, unknown shape)
* @returns v2 OpencodeClient instance, or undefined if extraction fails
*
* @deprecated Remove once plugin context exposes native v2 client directly.
*/
export function unstable_getV2Client(
client: unknown,
): V2OpencodeClient | undefined {
const rawClient = getSharedRawClient(client);
if (!rawClient) {
return undefined;
}
try {
return new V2OpencodeClient({ client: rawClient });
} catch {
return undefined;
}
}This then enables stuff like auto-rejecting tools with a message if I am away: await v2Client.permission.reply({
requestID: permissionID,
reply: "reject",
message,
}); |
a79486a to
09bc1e9
Compare
73d7d85 to
9474209
Compare
9474209 to
f6dca77
Compare
8c3a2fb to
c7c87e0
Compare
The v2 SDK types `fetch` as `typeof fetch` which includes Bun's static `preconnect` property and the two-argument overload. Hono's .fetch() only accepts Request. Functionally compatible at runtime.
|
Is there any progress |
I was told the goal of the maintainers is to focus on V2 for OpenCode which will fix the SDK issues. It's likely this won't ever merge and version 2.0.0 of OpenCode will solve the SDK woes. |
What does this PR do?
Fixes #7641
Provides two SDK clients (v1 and v2) to avoid backwards-compatibility issues and allow plugin authors to utilize the latest SDK.
This keeps the original
clientintact, allowing plugin authors to adopt SDK v2 incrementally without forcing immediate migration. Breaking changes until the next major release.@aryasaatvik has closed #7639 in favor of working together on this one. Thanks for the help!
Important
This is not a breaking change. That said, we likely wouldn't want a
clientNextandclientforever. I would be glad to help plan out a full migration down the road, assuming this gets merged.How did you verify your code works?