-
Notifications
You must be signed in to change notification settings - Fork 2
feat: improve browser extension sidepanel config #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
contrueCT
merged 4 commits into
contrueCT:main
from
1923213756:private/feat-browser-extension-sidepanel-config
May 10, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d35c2fa
feat: improve browser extension sidepanel config
1923213756 8564556
fix: remount extension iframe after reconnect
1923213756 b71bac2
Merge origin/main into private/feat-browser-extension-sidepanel-config
1923213756 4229ba6
fix: address browser extension PR review comments
1923213756 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
opencode/packages/opencode/src/server/nine1bot-browser-extension-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { readFile, writeFile } from "fs/promises" | ||
| import { parse as parseJsonc } from "jsonc-parser" | ||
| import z from "zod" | ||
| import { RuntimeControllerProtocol } from "@/runtime/controller/protocol" | ||
|
|
||
| export const BrowserExtensionModel = z.object({ | ||
| providerID: z.string().min(1), | ||
| modelID: z.string().min(1), | ||
| }) | ||
| export type BrowserExtensionModel = z.infer<typeof BrowserExtensionModel> | ||
|
|
||
| export const BrowserExtensionConfigPatch = z.object({ | ||
| model: BrowserExtensionModel.nullable().optional(), | ||
| prompt: z.string().nullable().optional(), | ||
| mcpServers: z.array(z.string()).nullable().optional(), | ||
| skills: z.array(z.string()).nullable().optional(), | ||
| }) | ||
| export type BrowserExtensionConfigPatch = z.infer<typeof BrowserExtensionConfigPatch> | ||
|
|
||
| export interface BrowserExtensionConfig { | ||
| model?: BrowserExtensionModel | ||
| prompt?: string | ||
| mcpServers?: string[] | ||
| skills?: string[] | ||
| } | ||
|
|
||
| export function isBrowserExtensionEntry(entry?: RuntimeControllerProtocol.Entry): boolean { | ||
| return entry?.source === "browser-extension" || entry?.mode === "browser-sidepanel" | ||
| } | ||
|
|
||
| function configPath() { | ||
| return process.env.NINE1BOT_CONFIG_PATH || "" | ||
| } | ||
|
|
||
| async function readNine1botConfig(pathname: string): Promise<Record<string, any>> { | ||
| const text = await readFile(pathname, "utf-8") | ||
| return (parseJsonc(text) || {}) as Record<string, any> | ||
| } | ||
|
|
||
| async function writeNine1botConfig(pathname: string, nextConfig: Record<string, any>) { | ||
| await writeFile(pathname, JSON.stringify(nextConfig, null, 2)) | ||
| } | ||
|
|
||
| function parseModelString(value: unknown): BrowserExtensionModel | undefined { | ||
| if (typeof value !== "string") return undefined | ||
| const trimmed = value.trim() | ||
| if (!trimmed.includes("/")) return undefined | ||
| const [providerID, ...modelParts] = trimmed.split("/") | ||
| const modelID = modelParts.join("/") | ||
| if (!providerID || !modelID) return undefined | ||
| return { providerID, modelID } | ||
| } | ||
|
|
||
| function formatModelString(model: BrowserExtensionModel) { | ||
| return `${model.providerID}/${model.modelID}` | ||
| } | ||
|
|
||
| function normalizeStringList(value: unknown): string[] { | ||
| if (!Array.isArray(value)) return [] | ||
| return [...new Set(value | ||
| .filter((item): item is string => typeof item === "string") | ||
| .map((item) => item.trim()) | ||
| .filter(Boolean))] | ||
| } | ||
|
|
||
| function normalizeBrowserExtensionConfig(config: Record<string, any>): BrowserExtensionConfig { | ||
| const sidepanel = config.browser?.sidepanel | ||
| if (!sidepanel || typeof sidepanel !== "object" || Array.isArray(sidepanel)) return {} | ||
|
|
||
| const model = parseModelString(sidepanel.model) | ||
| const prompt = typeof sidepanel.prompt === "string" && sidepanel.prompt.trim() | ||
| ? sidepanel.prompt | ||
| : undefined | ||
| const mcpServers = normalizeStringList(sidepanel.mcpServers) | ||
| const skills = normalizeStringList(sidepanel.skills) | ||
|
|
||
| return { | ||
| ...(model ? { model } : {}), | ||
| ...(prompt ? { prompt } : {}), | ||
| ...(mcpServers.length > 0 ? { mcpServers } : {}), | ||
| ...(skills.length > 0 ? { skills } : {}), | ||
| } | ||
| } | ||
|
|
||
| export async function readBrowserExtensionConfig(): Promise<BrowserExtensionConfig> { | ||
| const pathname = configPath() | ||
| if (!pathname) return {} | ||
| const config = (await readNine1botConfig(pathname).catch(() => ({}))) as Record<string, any> | ||
| return normalizeBrowserExtensionConfig(config) | ||
| } | ||
|
|
||
| export async function patchBrowserExtensionConfig(patch: BrowserExtensionConfigPatch): Promise<BrowserExtensionConfig> { | ||
| const pathname = configPath() | ||
| if (!pathname) { | ||
| throw new Error("No config path") | ||
| } | ||
|
|
||
| const existing = (await readNine1botConfig(pathname).catch(() => ({}))) as Record<string, any> | ||
| const browser = existing.browser && typeof existing.browser === "object" && !Array.isArray(existing.browser) | ||
| ? { ...existing.browser } | ||
| : {} | ||
| const sidepanel = browser.sidepanel && typeof browser.sidepanel === "object" && !Array.isArray(browser.sidepanel) | ||
| ? { ...browser.sidepanel } | ||
| : {} | ||
|
|
||
| if ("model" in patch) { | ||
| if (patch.model === null) delete sidepanel.model | ||
| else if (patch.model) sidepanel.model = formatModelString(patch.model) | ||
| } | ||
|
|
||
| if ("prompt" in patch) { | ||
| const prompt = patch.prompt?.trim() | ||
| if (prompt) sidepanel.prompt = prompt | ||
| else delete sidepanel.prompt | ||
| } | ||
|
|
||
| if ("mcpServers" in patch) { | ||
| const mcpServers = normalizeStringList(patch.mcpServers) | ||
| if (mcpServers.length > 0) sidepanel.mcpServers = mcpServers | ||
| else delete sidepanel.mcpServers | ||
| } | ||
|
|
||
| if ("skills" in patch) { | ||
| const skills = normalizeStringList(patch.skills) | ||
| if (skills.length > 0) sidepanel.skills = skills | ||
| else delete sidepanel.skills | ||
| } | ||
|
|
||
| browser.sidepanel = sidepanel | ||
| const nextConfig = { | ||
| ...existing, | ||
| browser, | ||
| } | ||
|
|
||
| await writeNine1botConfig(pathname, nextConfig) | ||
| return normalizeBrowserExtensionConfig(nextConfig) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.