Skip to content

Commit 3fa9f9b

Browse files
committed
Transitioned to plugin infra and added File Operations as well
1 parent 0380c70 commit 3fa9f9b

File tree

10 files changed

+274
-74
lines changed

10 files changed

+274
-74
lines changed

app/commands/agents.ts renamed to app/commandPlugins/AgentCommandPlugins.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { LLMMessage, LLMModel, callLLMChatCompletion } from "~/utils/llmUtils";
22
import { Config } from "~/utils/config";
3+
import { CommandPlugin } from "./CommandPlugin";
34

4-
export interface Agent {
5+
interface Agent {
56
name: string;
67
task: string;
78
messages: LLMMessage[];
@@ -11,7 +12,7 @@ export interface Agent {
1112
let nextkey = 0;
1213
const agents: {[key: string]: Agent} = {};
1314

14-
export async function startAgent(
15+
async function startAgent(
1516
name: string,
1617
task: string,
1718
prompt: string,
@@ -25,7 +26,7 @@ export async function startAgent(
2526
return `Agent ${name} created with key ${key}. First response: ${agentResponse}`;
2627
}
2728

28-
export async function createAgent(
29+
async function createAgent(
2930
name: string,
3031
task: string,
3132
prompt: string,
@@ -51,7 +52,7 @@ export async function createAgent(
5152
return { key, agentReply };
5253
}
5354

54-
export async function messageAgent(
55+
async function messageAgent(
5556
key: string,
5657
message: string
5758
): Promise<string> {
@@ -67,15 +68,54 @@ export async function messageAgent(
6768
return agentReply;
6869
}
6970

70-
export function listAgents(): [string, string][] {
71+
function listAgents(): [string, string][] {
7172
return Object.keys(agents).map((key: string) => [key, agents[key].task]);
7273
}
7374

74-
export function deleteAgent(key: string): boolean {
75+
function deleteAgent(key: string): boolean {
7576
if (agents[key]) {
7677
delete agents[key];
7778
return true;
7879
}
7980

8081
return false;
8182
}
83+
84+
const AgentCommandPlugins: CommandPlugin[] = [
85+
{
86+
command: "start_agent",
87+
name: "Start GPT Agent",
88+
arguments: {
89+
name: "name",
90+
task: "short_task_desc",
91+
prompt: "prompt"
92+
},
93+
execute: (args) => startAgent(args["name"], args["task"], args["prompt"])
94+
},
95+
{
96+
command: "message_agent",
97+
name: "Message GPT Agent",
98+
arguments: {
99+
key: "key",
100+
message: "message"
101+
},
102+
execute: (args) => messageAgent(args["key"], args["message"])
103+
},
104+
{
105+
command: "list_agents",
106+
name: "List GPT Agents",
107+
arguments: {},
108+
execute: async (args) => JSON.stringify(listAgents())
109+
},
110+
{
111+
command: "delete_agent",
112+
name: "Delete GPT Agent",
113+
arguments: {
114+
key: "key"
115+
},
116+
execute: async (args) => deleteAgent(args["key"])
117+
? `Agent ${args["key"]} deleted.`
118+
: `Agent ${args["key"]} does not exist.`
119+
}
120+
];
121+
export default AgentCommandPlugins;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export type CommandExecArgs = { [key: string]: string };
3+
export interface CommandPlugin {
4+
command: string;
5+
name: string;
6+
arguments: CommandExecArgs;
7+
execute: (args: CommandExecArgs) => Promise<string>;
8+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { CommandPlugin } from "./CommandPlugin";
2+
3+
let directoryHandle: FileSystemDirectoryHandle | null = null;
4+
5+
async function getDirectoryHandle() {
6+
if (!directoryHandle) {
7+
if (window.showDirectoryPicker) {
8+
directoryHandle = await window.showDirectoryPicker();
9+
} else {
10+
//TODO: Create in-memory directory/file system
11+
}
12+
}
13+
14+
return directoryHandle!;
15+
}
16+
17+
async function readFile(fileName: string) {
18+
try {
19+
const dh = await getDirectoryHandle();
20+
const fileHandle = await dh.getFileHandle(fileName, { create: false });
21+
if (!fileHandle) {
22+
return "Error: Unable to read file";
23+
}
24+
25+
const file = await fileHandle.getFile();
26+
return await file.text();
27+
} catch (error) {
28+
console.error("Error reading from file", error);
29+
return `Error: ${error}`;
30+
}
31+
}
32+
33+
async function writeToFile(fileName: string, text: string) {
34+
try {
35+
const dh = await getDirectoryHandle();
36+
const fileHandle = await dh.getFileHandle(fileName, { create: true });
37+
if (!fileHandle) {
38+
return "Error: Unable to create a file";
39+
}
40+
41+
const file = await fileHandle.createWritable();
42+
await file.write(text);
43+
await file.close();
44+
return "File written to successfully";
45+
} catch (error) {
46+
console.error("Error writing to file", error);
47+
return `Error: ${error}`;
48+
}
49+
}
50+
51+
async function appendToFile(fileName: string, text: string) {
52+
try {
53+
const dh = await getDirectoryHandle();
54+
const fileHandle = await dh.getFileHandle(fileName, { create: true });
55+
if (!fileHandle) {
56+
return "Error: Unable to append to a file";
57+
}
58+
59+
const file = await fileHandle.createWritable({ keepExistingData: true });
60+
let offset = (await fileHandle.getFile()).size;
61+
await file.write({ type: "seek", position: offset });
62+
await file.write(text);
63+
await file.close();
64+
return "Text appended successfully";
65+
} catch (error) {
66+
console.error("Error appending to file", error);
67+
return `Error: ${error}`;
68+
}
69+
}
70+
71+
async function deleteFile(fileName: string) {
72+
try {
73+
const dh = await getDirectoryHandle();
74+
await dh.removeEntry(fileName);
75+
return "File deleted successfully";
76+
} catch (error) {
77+
console.error("Error deleting file", error);
78+
return `Error: ${error}`;
79+
}
80+
}
81+
82+
const FileOperationCommandPlugins: CommandPlugin[] = [
83+
{
84+
command: "write_to_file",
85+
name: "Write to file",
86+
arguments: {
87+
file: "file",
88+
text: "text",
89+
},
90+
execute: (args) => writeToFile(args["file"], args["text"]),
91+
},
92+
{
93+
command: "read_file",
94+
name: "Read file",
95+
arguments: {
96+
file: "file",
97+
},
98+
execute: (args) => readFile(args["file"]),
99+
},
100+
{
101+
command: "append_to_file",
102+
name: "Append to file",
103+
arguments: {
104+
file: "file",
105+
text: "text",
106+
},
107+
execute: (args) => appendToFile(args["file"], args["text"]),
108+
},
109+
{
110+
command: "delete_file",
111+
name: "Delete file",
112+
arguments: {
113+
file: "file",
114+
},
115+
execute: (args) => deleteFile(args["file"]),
116+
},
117+
];
118+
export default FileOperationCommandPlugins;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { CommandPlugin } from "./CommandPlugin";
2+
3+
const permanentMemory: string[] = [];
4+
5+
function commitMemory(val: string) {
6+
permanentMemory.push(val);
7+
8+
return `Committing memory with string "${val}"`;
9+
}
10+
function getMemory(index: number) {
11+
if (index >= permanentMemory.length) {
12+
return "Invalid key, cannot retrieve memory.";
13+
}
14+
return permanentMemory[index];
15+
}
16+
17+
function deleteMemory(index: number) {
18+
if (index >= permanentMemory.length) {
19+
return "Invalid key, cannot delete memory.";
20+
}
21+
22+
permanentMemory.splice(index, 1);
23+
return `Deleting memory with key ${index}.`;
24+
}
25+
26+
function overwriteMemory(index: number, val: string) {
27+
if (index >= permanentMemory.length) {
28+
return "Invalid key, cannote overwrite memory.";
29+
}
30+
31+
permanentMemory[index] = val;
32+
return `Overwriting memory with key ${index} and string "${val}".`;
33+
}
34+
35+
const MemoryCommandPlugins: CommandPlugin[] = [
36+
{
37+
command: "memory_add",
38+
name: "Memory Add",
39+
arguments: {
40+
string: "string",
41+
},
42+
execute: async (args) => commitMemory(args["string"]),
43+
},
44+
{
45+
command: "memory_del",
46+
name: "Memory Delete",
47+
arguments: {
48+
key: "key"
49+
},
50+
execute: async (args) => deleteMemory(parseInt(args["key"]))
51+
},
52+
{
53+
command: "memory_ovr",
54+
name: "Memory Overwrite",
55+
arguments: {
56+
key: "key",
57+
string: "string"
58+
},
59+
execute: async (args) => overwriteMemory(parseInt(args["key"]), args["string"])
60+
},
61+
];
62+
export default MemoryCommandPlugins;

app/commandPlugins/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import AgentCommandPlugins from "./AgentCommandPlugins";
2+
import MemoryCommandPlugins from "./MemoryCommandPlugins";
3+
import FileOperationCommandPlugins from "./FileOperationCommandPlugins";
4+
5+
export const CommandPlugins = [
6+
...MemoryCommandPlugins,
7+
...AgentCommandPlugins,
8+
...FileOperationCommandPlugins
9+
];
10+
11+
export async function executeCommand(
12+
command: string,
13+
args: { [key: string]: string }
14+
): Promise<string> {
15+
try {
16+
const commandPlugin = CommandPlugins.filter(({command: cmd}) => cmd == command)[0];
17+
if (commandPlugin) {
18+
return await commandPlugin.execute(args);
19+
} else {
20+
return `Unknown command ${command}`;
21+
}
22+
} catch (error) {
23+
return `Error: ${error}`;
24+
}
25+
}

app/commands/index.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/commands/memory.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)