mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-24 23:04:55 +00:00
Co-authored-by: adamdotdevin <2363879+adamdottv@users.noreply.github.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: GitHub Action <action@github.com>
30 lines
781 B
TypeScript
30 lines
781 B
TypeScript
import { createContext, useContext, type ParentProps } from "solid-js"
|
|
import { createOpencodeClient } from "@opencode-ai/sdk/client"
|
|
|
|
const host = import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "127.0.0.1"
|
|
const port = import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"
|
|
|
|
function init() {
|
|
const client = createOpencodeClient({
|
|
baseUrl: `http://${host}:${port}`,
|
|
})
|
|
return client
|
|
}
|
|
|
|
type SDKContext = ReturnType<typeof init>
|
|
|
|
const ctx = createContext<SDKContext>()
|
|
|
|
export function SDKProvider(props: ParentProps) {
|
|
const value = init()
|
|
return <ctx.Provider value={value}>{props.children}</ctx.Provider>
|
|
}
|
|
|
|
export function useSDK() {
|
|
const value = useContext(ctx)
|
|
if (!value) {
|
|
throw new Error("useSDK must be used within a SDKProvider")
|
|
}
|
|
return value
|
|
}
|