feat: added providers management settings with ability to add or remove providers (#76)
* feat: implement adding opencode providers in openchamber settings * feat: implement provider authentication management with removal functionality
This commit is contained in:
committed by
GitHub
parent
ff874cc33d
commit
8f9facb561
@@ -3,6 +3,7 @@ import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import type { OpenCodeManager } from './opencode';
|
||||
import { createAgent, createCommand, deleteAgent, deleteCommand, getAgentSources, getCommandSources, updateAgent, updateCommand } from './opencodeConfig';
|
||||
import { removeProviderAuth } from './opencodeAuth';
|
||||
|
||||
export interface BridgeRequest {
|
||||
id: string;
|
||||
@@ -807,6 +808,36 @@ export async function handleBridgeMessage(message: BridgeRequest, ctx?: BridgeCo
|
||||
}
|
||||
}
|
||||
|
||||
case 'api:provider/auth:delete': {
|
||||
const { providerId } = (payload || {}) as { providerId?: string };
|
||||
if (!providerId) {
|
||||
return { id, type, success: false, error: 'Provider ID is required' };
|
||||
}
|
||||
try {
|
||||
const removed = removeProviderAuth(providerId);
|
||||
if (removed) {
|
||||
await ctx?.manager?.restart();
|
||||
}
|
||||
return {
|
||||
id,
|
||||
type,
|
||||
success: true,
|
||||
data: {
|
||||
success: true,
|
||||
removed,
|
||||
requiresReload: removed,
|
||||
message: removed
|
||||
? `Provider ${providerId} disconnected successfully. Reloading interface…`
|
||||
: `Provider ${providerId} was not configured.`,
|
||||
reloadDelayMs: removed ? CLIENT_RELOAD_DELAY_MS : undefined,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return { id, type, success: false, error: errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return { id, type, success: false, error: `Unknown message type: ${type}` };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
|
||||
const OPENCODE_DATA_DIR = path.join(os.homedir(), '.local', 'share', 'opencode');
|
||||
const AUTH_FILE = path.join(OPENCODE_DATA_DIR, 'auth.json');
|
||||
|
||||
type AuthEntry = Record<string, unknown>;
|
||||
type AuthFile = Record<string, AuthEntry>;
|
||||
|
||||
const readAuthFile = (): AuthFile => {
|
||||
if (!fs.existsSync(AUTH_FILE)) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
const content = fs.readFileSync(AUTH_FILE, 'utf8');
|
||||
const trimmed = content.trim();
|
||||
if (!trimmed) {
|
||||
return {};
|
||||
}
|
||||
return JSON.parse(trimmed) as AuthFile;
|
||||
} catch (error) {
|
||||
console.error('Failed to read auth file:', error);
|
||||
throw new Error('Failed to read OpenCode auth configuration');
|
||||
}
|
||||
};
|
||||
|
||||
const writeAuthFile = (auth: AuthFile): void => {
|
||||
try {
|
||||
if (!fs.existsSync(OPENCODE_DATA_DIR)) {
|
||||
fs.mkdirSync(OPENCODE_DATA_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
if (fs.existsSync(AUTH_FILE)) {
|
||||
const backupFile = `${AUTH_FILE}.openchamber.backup`;
|
||||
fs.copyFileSync(AUTH_FILE, backupFile);
|
||||
}
|
||||
|
||||
fs.writeFileSync(AUTH_FILE, JSON.stringify(auth, null, 2), 'utf8');
|
||||
} catch (error) {
|
||||
console.error('Failed to write auth file:', error);
|
||||
throw new Error('Failed to write OpenCode auth configuration');
|
||||
}
|
||||
};
|
||||
|
||||
export const removeProviderAuth = (providerId: string): boolean => {
|
||||
if (!providerId || typeof providerId !== 'string') {
|
||||
throw new Error('Provider ID is required');
|
||||
}
|
||||
|
||||
const auth = readAuthFile();
|
||||
|
||||
if (!auth[providerId]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
delete auth[providerId];
|
||||
writeAuthFile(auth);
|
||||
return true;
|
||||
};
|
||||
|
||||
export const getProviderAuth = (providerId: string): AuthEntry | null => {
|
||||
const auth = readAuthFile();
|
||||
return auth[providerId] || null;
|
||||
};
|
||||
|
||||
export const listProviderAuths = (): string[] => {
|
||||
const auth = readAuthFile();
|
||||
return Object.keys(auth);
|
||||
};
|
||||
|
||||
export { AUTH_FILE, OPENCODE_DATA_DIR };
|
||||
Reference in New Issue
Block a user