Skip to content

Commit 9e0af9e

Browse files
Enhance logging and user settings management; add editor command configuration and tool configuration validation
1 parent 9f04f30 commit 9e0af9e

16 files changed

Lines changed: 215 additions & 62 deletions

File tree

config/examples/tool_config.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"tools": [
3+
{
4+
"toolName": "generateText",
5+
"command": "python generate.py --prompt '{{prompt}}'",
6+
"output": "string",
7+
"description": "Generate text using AI",
8+
"enabled": true,
9+
"inputs": [
10+
{
11+
"name": "prompt",
12+
"type": "string",
13+
"description": "Input prompt",
14+
"required": true
15+
}
16+
]
17+
},
18+
{
19+
"toolName": "multipleGenerate",
20+
"command": "python generate.py --prompt '{{prompt}}' --dir '{{currentDirectory}}' --count {{numberOfGenerations}}",
21+
"output": "string",
22+
"description": "Generate multiple texts",
23+
"enabled": true,
24+
"inputs": [
25+
{
26+
"name": "prompt",
27+
"type": "string",
28+
"description": "Input prompt",
29+
"required": true
30+
},
31+
{
32+
"name": "currentDirectory",
33+
"type": "string",
34+
"description": "Output directory",
35+
"required": true
36+
},
37+
{
38+
"name": "numberOfGenerations",
39+
"type": "number",
40+
"description": "Number of texts to generate",
41+
"required": true
42+
}
43+
]
44+
}
45+
]
46+
}

src/commands/editor.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { loadUserSettings } from "../config/settings.ts";
2+
import { log } from "../config/logging.ts";
3+
import { DEFAULT_EDITOR } from "../config/constants.ts";
4+
5+
export async function openInEditor(filePath: string): Promise<void> {
6+
const settings = await loadUserSettings();
7+
const editorCmd = settings.editorCommand || DEFAULT_EDITOR;
8+
9+
try {
10+
const process = new Deno.Command(editorCmd, {
11+
args: [filePath],
12+
stdin: "inherit",
13+
stdout: "inherit",
14+
stderr: "inherit"
15+
});
16+
17+
await process.output();
18+
} catch (error) {
19+
log.error(`Failed to open editor: ${error}`);
20+
}
21+
}

src/commands/settings.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {parseArgs} from "jsr:@std/cli/parse-args"
22
import {loadUserSettings, saveUserSettings} from "../config/settings.ts"
33
import {parseFlagForHelp, getCommandHelp} from "../utils/functions.ts"
4+
import {DEFAULT_TOOLS_CONFIG_PATH} from "../config/constants.ts"
45

56
export async function handleSettings(args: string[]): Promise<void> {
67
const settingsFlags = {
7-
string: ["set-name", "add-command", "remove-command", "set-jina-key", "set-config"],
8+
string: ["set-name", "add-command", "remove-command", "set-jina-key", "set-config", "set-editor"],
89
boolean: ["list"],
10+
911
}
1012
const flags = parseArgs(args, settingsFlags)
1113

@@ -64,6 +66,10 @@ export async function handleSettings(args: string[]): Promise<void> {
6466
console.log(JSON.stringify(settings, null, 2))
6567
return
6668
}
69+
else if (flags["set-editor"]) {
70+
settings.editorCommand = flags["set-editor"]
71+
console.log(`Editor command set to: ${flags["set-editor"]}`)
72+
}
6773

6874
await saveUserSettings(settings)
6975
}

src/config/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export const EDITOR_DIR = join(homedir(), ".ComputerUseAgent", "editor_dir")
77
export const SESSIONS_DIR = join(homedir(), ".ComputerUseAgent", "sessions")
88
export const LOGS_DIR = join(homedir(), ".ComputerUseAgent", "logs")
99
export const DEFAULT_TOOLS_CONFIG_PATH = join(homedir(), ".ComputerUseAgent", "settings.json")
10+
export const MEMORY_PATH = "/root/memory.json"
11+
12+
export const DEFAULT_EDITOR = Deno.env.get("EDITOR") ?? "nano";
1013

1114
export const EDITOR_SYSTEM_PROMPT = Deno.env.get("EDITOR_SYSTEM_PROMPT") ??
1215
`You are a helpful assistant that helps users edit and work with text files.

src/config/settings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {join} from "jsr:@std/path"
22
import {homedir} from "node:os"
33
import {UserSettings} from "../types/interfaces.ts"
4+
import {DEFAULT_EDITOR} from "./constants.ts"
45

56
const DEFAULT_SETTINGS: UserSettings = {
67
userName: "User",
78
customCommands: [],
89
jinaApiKey: undefined,
9-
toolConfigPath: join(homedir(), ".ComputerUseAgent", "tools.json")
10+
toolConfigPath: join(homedir(), ".ComputerUseAgent", "tools.json"),
11+
editorCommand: "nano"
1012
}
1113

1214
const SETTINGS_PATH = join(homedir(), ".ComputerUseAgent", "settings.json")

src/config/tool_config.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import {ToolConfig} from "../types/interfaces.ts"
22

33
export class ToolConfigManager {
4+
validateToolConfig(config: ToolConfig[]): boolean {
5+
return config.every(tool => {
6+
return tool.toolName &&
7+
tool.command &&
8+
Array.isArray(tool.inputs) &&
9+
tool.inputs.every(input => input.name && input.type);
10+
});
11+
}
12+
413
loadConfig(configPath: string): ToolConfig[] {
514
try {
6-
const configText = Deno.readTextFileSync(configPath)
7-
const config = JSON.parse(configText)
8-
return Array.isArray(config) ? config : config.tools || []
15+
const configText = Deno.readTextFileSync(configPath);
16+
const config = JSON.parse(configText);
17+
const tools = Array.isArray(config) ? config : config.tools || [];
18+
19+
if (!this.validateToolConfig(tools)) {
20+
throw new Error("Invalid tool configuration format");
21+
}
22+
return tools;
923
} catch (error) {
10-
console.warn(`Could not load config from ${configPath}: ${error}`)
11-
return []
24+
console.warn(`Could not load config from ${configPath}: ${error}`);
25+
return [];
1226
}
1327
}
1428
}

src/main.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import {format} from "jsr:@std/datetime"
66
import {crypto} from "jsr:@std/crypto"
77

88
import {setupLogging, log} from "./config/logging.ts"
9-
import {EDITOR_DIR, SESSIONS_DIR} from "./config/constants.ts"
9+
import {EDITOR_DIR, SESSIONS_DIR, DEFAULT_TOOLS_CONFIG_PATH, MEMORY_PATH} from "./config/constants.ts"
1010
import {EditorSession} from "./modules/editor/editor_session.ts"
1111
import {BashSession} from "./modules/bash/bash_session.ts"
1212
import {HybridSession} from "./modules/hybrid/hybrid_session.ts"
1313
import {parseFlagForHelp} from "./utils/functions.ts"
1414
import {handleHistory} from "./commands/history.ts"
1515
import {handleSettings} from "./commands/settings.ts"
16+
import {loadUserSettings} from "./config/settings.ts"
17+
import {openInEditor} from "./commands/editor.ts"
1618

1719
async function main() {
1820
await setupLogging()
@@ -40,6 +42,21 @@ async function main() {
4042
} else if (flags._[0] === "help") {
4143
console.log(parseFlagForHelp(argParseConfig))
4244
return
45+
} else if (flags._[0] === "edit") {
46+
const settings = loadUserSettings()
47+
switch (flags._[1]) {
48+
case "tools":
49+
log.info(`Opening tools config file: ${settings.toolConfigPath}`)
50+
await openInEditor(settings.toolConfigPath || DEFAULT_TOOLS_CONFIG_PATH)
51+
return
52+
case "memory":
53+
log.info(`Opening memory file: ${MEMORY_PATH}`)
54+
await openInEditor(MEMORY_PATH)
55+
return
56+
default:
57+
console.log("Usage: edit [tools|memory]")
58+
}
59+
return
4360
}
4461

4562
if (mode === "editor") {

src/modules/bash/handlers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class BashHandlers {
1212
async handleBashCommand(
1313
toolCall: Record<string, any>,
1414
): Promise<Record<string, any>> {
15+
log.info(`I am running command: ${toolCall.command}`)
1516
try {
1617
const command = toolCall.command as string
1718
const restart = toolCall.restart ?? false

src/modules/clipboard/clipboard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class ClipboardManager {
3939

4040
async readClipboard(): Promise<string> {
4141
try {
42+
log.info(`Reading clipboard`)
4243
const os = Deno.build.os
4344
const isWsl = Deno.env.get("WSL_DISTRO_NAME") !== undefined
4445

src/modules/db/database.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export class PromptDatabase {
6868
cost: data.cost
6969
}
7070
)
71-
log.debug({number})
7271
return number
7372
}
7473

0 commit comments

Comments
 (0)