mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-03 13:52:09 +03:00
fix(cli-tools): preserve opencode config and raw key copy (#1626)
- Use jsonc-parser to update only provider.omniroute in opencode.json,
preserving MCP servers, comments, and other provider entries
- Add /api/cli-tools/keys endpoint returning raw keys for CLI tools UI
(session-auth protected via requireCliToolsAuth)
- Fix OpenCode guide step 3 to use ICU-style {baseUrl} placeholder
instead of broken {{baseUrl}} across all 40+ locales
- Restore valid OpenCode light/dark SVG logos (were broken HTML downloads)
- Add customCliTab translation key to en.json
- Add modelLabels support for human-readable model names in config
- Fix 9 additional locale files (bn, fa, gu, in, mr, sw, ta, te, ur)
that were added after the original PR and still had double-braces
Co-authored-by: JasonLandbridge <JasonLandbridge@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { applyEdits, modify, parse } from "jsonc-parser";
|
||||
|
||||
type OpenCodeConfigInput = {
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
model?: string;
|
||||
models?: string[];
|
||||
modelLabels?: Record<string, string>;
|
||||
};
|
||||
|
||||
const OPENCODE_DEFAULT_MODELS = [
|
||||
@@ -22,17 +25,32 @@ const normalizeModels = (models: unknown): string[] => {
|
||||
return [...new Set(models.map((model) => normalizeValue(model)).filter(Boolean))];
|
||||
};
|
||||
|
||||
const normalizeModelLabels = (labels: unknown): Record<string, string> => {
|
||||
if (!labels || typeof labels !== "object" || Array.isArray(labels)) return {};
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(labels)
|
||||
.map(([key, value]) => [normalizeValue(key), String(value || "").trim()])
|
||||
.filter(([key, value]) => key && value)
|
||||
);
|
||||
};
|
||||
|
||||
const getModelEntryName = (modelId: string, labels: Record<string, string>) =>
|
||||
labels[modelId] || modelId;
|
||||
|
||||
export const buildOpenCodeProviderConfig = ({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
model,
|
||||
models,
|
||||
modelLabels,
|
||||
}: OpenCodeConfigInput): Record<string, any> => {
|
||||
const normalizedBaseUrl = String(baseUrl || "")
|
||||
.trim()
|
||||
.replace(/\/+$/, "");
|
||||
const normalizedModel = normalizeValue(model);
|
||||
const normalizedModels = normalizeModels(models);
|
||||
const normalizedLabels = normalizeModelLabels(modelLabels);
|
||||
|
||||
const uniqueModels =
|
||||
normalizedModels.length > 0
|
||||
@@ -42,7 +60,7 @@ export const buildOpenCodeProviderConfig = ({
|
||||
const modelsRecord: Record<string, { name: string }> = {};
|
||||
for (const m of uniqueModels) {
|
||||
if (m) {
|
||||
modelsRecord[m] = { name: m };
|
||||
modelsRecord[m] = { name: getModelEntryName(m, normalizedLabels) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,3 +100,41 @@ export const mergeOpenCodeConfig = (
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const mergeOpenCodeConfigText = (
|
||||
existingText: string | null | undefined,
|
||||
input: OpenCodeConfigInput
|
||||
) => {
|
||||
const providerConfig = buildOpenCodeProviderConfig(input);
|
||||
const content = typeof existingText === "string" ? existingText : "";
|
||||
const trimmedContent = content.trim();
|
||||
|
||||
if (!trimmedContent) {
|
||||
return JSON.stringify(buildOpenCodeConfigDocument(input), null, 2);
|
||||
}
|
||||
|
||||
const errors: { error: number }[] = [];
|
||||
const parsed = parse(content, errors, { allowTrailingComma: true, disallowComments: false });
|
||||
|
||||
if (errors.length > 0 || !parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
return JSON.stringify(mergeOpenCodeConfig({}, input), null, 2);
|
||||
}
|
||||
|
||||
let nextText = content;
|
||||
|
||||
const schemaEdits = modify(
|
||||
nextText,
|
||||
["$schema"],
|
||||
parsed.$schema || "https://opencode.ai/config.json",
|
||||
{
|
||||
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
||||
}
|
||||
);
|
||||
nextText = applyEdits(nextText, schemaEdits);
|
||||
|
||||
const providerEdits = modify(nextText, ["provider", "omniroute"], providerConfig, {
|
||||
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
||||
});
|
||||
|
||||
return applyEdits(nextText, providerEdits);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user