Raw
1 import * as vscode from "vscode";
2
3 /**
4 * Reads the siGit agent registry and permission settings from the workspace
5 * configuration. Falls back to the on-device `sigit` binary when the registry
6 * is empty — local-first is always the default.
7 */
8
9 export interface AgentDefinition {
10 key: string;
11 name: string;
12 command: string;
13 args: string[];
14 env: Record<string, string>;
15 }
16
17 export type PermissionMode = "prompt" | "allow" | "deny";
18
19 const FALLBACK_AGENT: AgentDefinition = {
20 key: "sigit",
21 name: "siGit (on-device)",
22 command: "sigit",
23 args: [],
24 env: {}
25 };
26
27 interface RawAgent {
28 name?: string;
29 command: string;
30 args?: string[];
31 env?: Record<string, string>;
32 }
33
34 function config(): vscode.WorkspaceConfiguration {
35 return vscode.workspace.getConfiguration("sigit");
36 }
37
38 function normalize(key: string, raw: RawAgent): AgentDefinition {
39 return {
40 key,
41 name: raw.name ?? key,
42 command: raw.command,
43 args: raw.args ?? [],
44 env: raw.env ?? {}
45 };
46 }
47
48 /** All configured agents, never empty (falls back to on-device `sigit`). */
49 export function listAgents(): AgentDefinition[] {
50 const registry = config().get<Record<string, RawAgent>>("agents") ?? {};
51 const entries = Object.entries(registry).filter(([, raw]) => raw && raw.command);
52 if (entries.length === 0) {
53 return [FALLBACK_AGENT];
54 }
55 return entries.map(([key, raw]) => normalize(key, raw));
56 }
57
58 /** Resolve a specific agent by key, or the configured default when omitted. */
59 export function resolveAgent(key?: string): AgentDefinition {
60 const agents = listAgents();
61 const wanted = key ?? config().get<string>("agent.default") ?? FALLBACK_AGENT.key;
62 return agents.find((agent) => agent.key === wanted) ?? agents[0];
63 }
64
65 /** The key of the default agent. */
66 export function defaultAgentKey(): string {
67 return config().get<string>("agent.default") ?? FALLBACK_AGENT.key;
68 }
69
70 /** Current permission handling mode. */
71 export function permissionMode(): PermissionMode {
72 return config().get<PermissionMode>("permission.mode") ?? "prompt";
73 }