Migrate UI to v2 SDK client
Use v2 OpencodeClient with normalized request handling and rehydrate pending permissions via GET /permission on instance hydration.
This commit is contained in:
37
packages/ui/src/lib/opencode-api.ts
Normal file
37
packages/ui/src/lib/opencode-api.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { OpencodeClient } from "@opencode-ai/sdk/v2/client"
|
||||
|
||||
export class OpencodeApiError extends Error {
|
||||
constructor(message: string, options?: { cause?: unknown }) {
|
||||
super(message)
|
||||
this.name = "OpencodeApiError"
|
||||
if (options && "cause" in options) {
|
||||
;(this as any).cause = options.cause
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type RequestResultLike<T> =
|
||||
| {
|
||||
data: T
|
||||
error?: undefined
|
||||
}
|
||||
| {
|
||||
data?: undefined
|
||||
error: unknown
|
||||
}
|
||||
|
||||
export async function requestData<T>(
|
||||
promise: Promise<RequestResultLike<T> | undefined>,
|
||||
label: string,
|
||||
): Promise<T> {
|
||||
const result = await promise
|
||||
if (!result) {
|
||||
throw new OpencodeApiError(`${label} returned no result`)
|
||||
}
|
||||
if ((result as any).error) {
|
||||
throw new OpencodeApiError(`${label} failed`, { cause: (result as any).error })
|
||||
}
|
||||
return (result as any).data as T
|
||||
}
|
||||
|
||||
export type { OpencodeClient }
|
||||
Reference in New Issue
Block a user