Files
NetMesh/components/settings/tabs/ai/codebuddyConfigEnv.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

74 lines
2.6 KiB
TypeScript

/**
* Pure helpers for the CodeBuddy card's environment variables editor.
* The managed CodeBuddy agent stores everything in its
* ExternalAgentConfig.env; this splits that into the editable pieces and
* recombines them.
*
* CODEBUDDY_CODE_PATH is owned by path discovery, so it is preserved across
* edits but never shown in the env editor.
*
* The SDK supports CODEBUDDY_API_KEY (via options.env), but the CLI itself
* does not. CODEBUDDY_INTERNET_ENVIRONMENT is managed as a first-class field.
* Users who need CODEBUDDY_API_KEY or CODEBUDDY_AUTH_TOKEN should set them
* in the free-text environment editor or in their shell profile.
*/
const INTERNET_ENV_VAR = "CODEBUDDY_INTERNET_ENVIRONMENT";
const CODE_PATH_KEY = "CODEBUDDY_CODE_PATH";
const MANAGED_KEYS = new Set([INTERNET_ENV_VAR, CODE_PATH_KEY]);
export function parseEnvLines(text: string): Record<string, string> {
const out: Record<string, string> = {};
for (const rawLine of String(text || "").split("\n")) {
const line = rawLine.trim();
if (!line || line.startsWith("#")) continue;
const eq = line.indexOf("=");
if (eq <= 0) continue;
const key = line.slice(0, eq).trim();
const value = line.slice(eq + 1).trim();
if (key) out[key] = value;
}
return out;
}
export function serializeEnvLines(env: Record<string, string>): string {
return Object.entries(env)
.map(([k, v]) => `${k}=${v}`)
.join("\n");
}
export function splitCodebuddyEnv(
env: Record<string, string> | undefined,
): { internetEnv: string; envText: string } {
if (!env) return { internetEnv: "", envText: "" };
const internetEnv = env[INTERNET_ENV_VAR] ?? "";
const rest: Record<string, string> = {};
for (const [k, v] of Object.entries(env)) {
if (MANAGED_KEYS.has(k)) continue;
rest[k] = v;
}
return { internetEnv, envText: serializeEnvLines(rest) };
}
export function buildCodebuddyEnv(
prevEnv: Record<string, string> | undefined,
internetEnv: string,
envText: string,
): Record<string, string> | undefined {
const next: Record<string, string> = {};
const trimmedInternetEnv = String(internetEnv || "").trim();
if (trimmedInternetEnv) next[INTERNET_ENV_VAR] = trimmedInternetEnv;
// Preserve auto-injected CODEBUDDY_CODE_PATH across edits.
const codePath = prevEnv?.[CODE_PATH_KEY];
if (codePath) next[CODE_PATH_KEY] = codePath;
// Drop managed keys if a user typed them into the free-text editor
const parsed = parseEnvLines(envText);
for (const key of MANAGED_KEYS) delete parsed[key];
Object.assign(next, parsed);
return Object.keys(next).length > 0 ? next : undefined;
}