Skip to content

Commit 943b0d5

Browse files
Add user settings management with load and save functionality-Architect Claude
1 parent 785402f commit 943b0d5

6 files changed

Lines changed: 96 additions & 1 deletion

File tree

src/commands/settings.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { parseArgs } from "jsr:@std/cli/parse-args";
2+
import { loadUserSettings, saveUserSettings } from "../config/settings.ts";
3+
4+
export async function handleSettings(args: string[]): Promise<void> {
5+
const flags = parseArgs(args, {
6+
string: ["set-name", "add-command", "remove-command"],
7+
boolean: ["list"],
8+
});
9+
10+
const settings = await loadUserSettings();
11+
12+
if (flags["set-name"]) {
13+
settings.userName = flags["set-name"];
14+
}
15+
else if (flags["add-command"]) {
16+
const [name, description, helpCmd, ...helpFlags] = flags["add-command"].split(",");
17+
settings.customCommands.push({
18+
name,
19+
description,
20+
helpCommand: helpCmd,
21+
helpFlags: helpFlags
22+
});
23+
}
24+
else if (flags["remove-command"]) {
25+
const index = settings.customCommands.findIndex(cmd => cmd.name === flags["remove-command"]);
26+
if (index >= 0) settings.customCommands.splice(index, 1);
27+
}
28+
else if (flags.list) {
29+
console.log(JSON.stringify(settings, null, 2));
30+
return;
31+
}
32+
33+
await saveUserSettings(settings);
34+
}

src/config/constants.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {join} from "jsr:@std/path"
22
import Anthropic from "npm:@anthropic-ai/sdk"
33
import {homedir} from "node:os"
4+
import { loadUserSettings } from "./settings.ts";
45

56
export const EDITOR_DIR = join(homedir(), ".ComputerUseAgent", "editor_dir")
67
export const SESSIONS_DIR = join(homedir(), ".ComputerUseAgent", "sessions")
@@ -147,4 +148,20 @@ export const CLIPBOARD_TOOLS: Anthropic.Beta.BetaTool[] = [
147148
input_schema: {type: "object", properties: {}, required: []}
148149

149150
}
150-
]
151+
]
152+
153+
export async function getSystemContext(basePrompt: string): Promise<string> {
154+
const settings = await loadUserSettings();
155+
const customCommandsContext = settings.customCommands.length > 0
156+
? "\nCustom Commands:\n" + settings.customCommands
157+
.map(cmd => `- ${cmd.name}: ${cmd.description}${
158+
cmd.helpCommand ? `\n Help: ${cmd.helpCommand}` : ''
159+
}${
160+
cmd.helpFlags ? `\n Flags: ${cmd.helpFlags.join(', ')}` : ''
161+
}`).join('\n')
162+
: '';
163+
164+
return `${basePrompt}
165+
User Context:
166+
- Name: ${settings.userName}${customCommandsContext}`;
167+
}

src/config/settings.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { join } from "jsr:@std/path";
2+
import { homedir } from "node:os";
3+
import { UserSettings } from "../types/interfaces.ts";
4+
5+
const DEFAULT_SETTINGS: UserSettings = {
6+
userName: "User",
7+
customCommands: []
8+
};
9+
10+
const SETTINGS_PATH = join(homedir(), ".ComputerUseAgent", "settings.json");
11+
12+
export async function loadUserSettings(): Promise<UserSettings> {
13+
try {
14+
const content = await Deno.readTextFile(SETTINGS_PATH);
15+
return { ...DEFAULT_SETTINGS, ...JSON.parse(content) };
16+
} catch {
17+
return DEFAULT_SETTINGS;
18+
}
19+
}
20+
21+
export async function saveUserSettings(settings: UserSettings): Promise<void> {
22+
await Deno.writeTextFile(SETTINGS_PATH, JSON.stringify(settings, null, 2));
23+
}

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {BashSession} from "./modules/bash/bash_session.ts"
1212
import {HybridSession} from "./modules/hybrid/hybrid_session.ts"
1313
import {determineIntent} from "./utils/intent.ts"
1414
import {handleHistory} from "./commands/history.ts"
15+
import { handleSettings } from "./commands/settings.ts"
1516

1617
async function main() {
1718
await setupLogging()
@@ -35,6 +36,11 @@ async function main() {
3536
return
3637
}
3738

39+
if (flags._[0] === "settings") {
40+
await handleSettings(flags._.slice(1));
41+
return;
42+
}
43+
3844
if (mode === "editor") {
3945
const session = new EditorSession(sessionId)
4046
await session.processEdit(prompt)

src/types/interfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ export interface Memory {
3838
export interface MemoryFile {
3939
memories: Memory[];
4040
}
41+
42+
export interface UserSettings {
43+
userName: string;
44+
customCommands: {
45+
name: string;
46+
description: string;
47+
helpCommand?: string;
48+
helpFlags?: string[];
49+
}[];
50+
}

src/utils/session.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {format} from "jsr:@std/datetime"
33
import {crypto} from "jsr:@std/crypto"
44
import {log} from "../config/logging.ts"
55
import {PromptDatabase} from "../modules/db/database.ts"
6+
import { getSystemContext } from "../config/constants.ts";
67

78
export class SessionLogger {
89
private sessionId: string
@@ -91,4 +92,8 @@ export class BaseSession {
9192
cost: totalCost
9293
})
9394
}
95+
96+
protected async getSystemPrompt(basePrompt: string): Promise<string> {
97+
return await getSystemContext(basePrompt);
98+
}
9499
}

0 commit comments

Comments
 (0)