@townco/agent 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,97 +2,105 @@ import { spawn } from "node:child_process";
2
2
  import { chmod, mkdir, writeFile } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { agentExists, ensureAgentsDir, getAgentPath } from "../storage";
5
- import { generateAgentJson, generateBinTs, generateEnvExample, generateGitignore, generateIndexTs, generatePackageJson, generateReadme, generateTsConfig, getTemplateVars, } from "../templates";
5
+ import {
6
+ generateAgentJson,
7
+ generateBinTs,
8
+ generateEnvExample,
9
+ generateGitignore,
10
+ generateIndexTs,
11
+ generatePackageJson,
12
+ generateReadme,
13
+ generateTsConfig,
14
+ getTemplateVars,
15
+ } from "../templates";
6
16
  import { bundleAgentDependencies } from "./bundle";
7
17
  import { copyGuiApp } from "./copy-gui";
8
18
  /**
9
19
  * Scaffold a new agent package
10
20
  */
11
21
  export async function scaffoldAgent(options) {
12
- const { name, definition, overwrite = false, includeGui = true } = options;
13
- try {
14
- // Ensure base directory exists
15
- await ensureAgentsDir();
16
- // Check if agent already exists
17
- const exists = await agentExists(name);
18
- if (exists && !overwrite) {
19
- return {
20
- success: false,
21
- path: getAgentPath(name),
22
- error: `Agent "${name}" already exists. Use overwrite option to replace it.`,
23
- };
24
- }
25
- const agentPath = getAgentPath(name);
26
- // Create the agent directory
27
- await mkdir(agentPath, { recursive: true });
28
- const vars = getTemplateVars(name, definition);
29
- // Generate all template files
30
- const files = [
31
- { path: "package.json", content: generatePackageJson(vars) },
32
- { path: "agent.json", content: generateAgentJson(vars) },
33
- { path: "index.ts", content: generateIndexTs() },
34
- { path: "bin.ts", content: generateBinTs(), executable: true },
35
- { path: "tsconfig.json", content: generateTsConfig() },
36
- { path: "README.md", content: generateReadme(vars) },
37
- { path: ".gitignore", content: generateGitignore() },
38
- ];
39
- // Add .env.example if needed
40
- const envExample = generateEnvExample(vars);
41
- if (envExample) {
42
- files.push({ path: ".env.example", content: envExample });
43
- }
44
- // Write all files
45
- for (const file of files) {
46
- const filePath = join(agentPath, file.path);
47
- await writeFile(filePath, file.content, "utf-8");
48
- // Make executable if needed
49
- if (file.executable) {
50
- await chmod(filePath, 0o755);
51
- }
52
- }
53
- // Bundle agent dependencies (copy lib files)
54
- await bundleAgentDependencies(agentPath);
55
- // Copy GUI app if requested
56
- if (includeGui) {
57
- await copyGuiApp(agentPath);
58
- // Run bun install in the GUI directory
59
- const guiPath = join(agentPath, "gui");
60
- await runBunInstall(guiPath);
61
- }
62
- // Run bun install in agent root
63
- await runBunInstall(agentPath);
64
- return {
65
- success: true,
66
- path: agentPath,
67
- };
68
- }
69
- catch (error) {
70
- return {
71
- success: false,
72
- path: getAgentPath(name),
73
- error: error instanceof Error ? error.message : String(error),
74
- };
75
- }
22
+ const { name, definition, overwrite = false, includeGui = true } = options;
23
+ try {
24
+ // Ensure base directory exists
25
+ await ensureAgentsDir();
26
+ // Check if agent already exists
27
+ const exists = await agentExists(name);
28
+ if (exists && !overwrite) {
29
+ return {
30
+ success: false,
31
+ path: getAgentPath(name),
32
+ error: `Agent "${name}" already exists. Use overwrite option to replace it.`,
33
+ };
34
+ }
35
+ const agentPath = getAgentPath(name);
36
+ // Create the agent directory
37
+ await mkdir(agentPath, { recursive: true });
38
+ const vars = getTemplateVars(name, definition);
39
+ // Generate all template files
40
+ const files = [
41
+ { path: "package.json", content: generatePackageJson(vars) },
42
+ { path: "agent.json", content: generateAgentJson(vars) },
43
+ { path: "index.ts", content: generateIndexTs() },
44
+ { path: "bin.ts", content: generateBinTs(), executable: true },
45
+ { path: "tsconfig.json", content: generateTsConfig() },
46
+ { path: "README.md", content: generateReadme(vars) },
47
+ { path: ".gitignore", content: generateGitignore() },
48
+ ];
49
+ // Add .env.example if needed
50
+ const envExample = generateEnvExample(vars);
51
+ if (envExample) {
52
+ files.push({ path: ".env.example", content: envExample });
53
+ }
54
+ // Write all files
55
+ for (const file of files) {
56
+ const filePath = join(agentPath, file.path);
57
+ await writeFile(filePath, file.content, "utf-8");
58
+ // Make executable if needed
59
+ if (file.executable) {
60
+ await chmod(filePath, 0o755);
61
+ }
62
+ }
63
+ // Bundle agent dependencies (copy lib files)
64
+ await bundleAgentDependencies(agentPath);
65
+ // Copy GUI app if requested
66
+ if (includeGui) {
67
+ await copyGuiApp(agentPath);
68
+ // Run bun install in the GUI directory
69
+ const guiPath = join(agentPath, "gui");
70
+ await runBunInstall(guiPath);
71
+ }
72
+ // Run bun install in agent root
73
+ await runBunInstall(agentPath);
74
+ return {
75
+ success: true,
76
+ path: agentPath,
77
+ };
78
+ } catch (error) {
79
+ return {
80
+ success: false,
81
+ path: getAgentPath(name),
82
+ error: error instanceof Error ? error.message : String(error),
83
+ };
84
+ }
76
85
  }
77
86
  /**
78
87
  * Run bun install in the agent directory
79
88
  */
80
89
  function runBunInstall(agentPath) {
81
- return new Promise((resolve, reject) => {
82
- const bunInstall = spawn("bun", ["install"], {
83
- cwd: agentPath,
84
- stdio: "ignore",
85
- });
86
- bunInstall.on("close", (code) => {
87
- if (code === 0) {
88
- resolve();
89
- }
90
- else {
91
- reject(new Error(`bun install failed with code ${code}`));
92
- }
93
- });
94
- bunInstall.on("error", (error) => {
95
- reject(error);
96
- });
97
- });
90
+ return new Promise((resolve, reject) => {
91
+ const bunInstall = spawn("bun", ["install"], {
92
+ cwd: agentPath,
93
+ stdio: "ignore",
94
+ });
95
+ bunInstall.on("close", (code) => {
96
+ if (code === 0) {
97
+ resolve();
98
+ } else {
99
+ reject(new Error(`bun install failed with code ${code}`));
100
+ }
101
+ });
102
+ bunInstall.on("error", (error) => {
103
+ reject(error);
104
+ });
105
+ });
98
106
  }
@@ -1,58 +1,57 @@
1
1
  import { mkdir, readdir, rm, stat } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
3
  import { join } from "node:path";
4
+
4
5
  const AGENTS_DIR = join(homedir(), ".config", "town", "agents");
5
6
  /**
6
7
  * Get the base directory where all agents are stored
7
8
  */
8
9
  export function getAgentsDir() {
9
- return AGENTS_DIR;
10
+ return AGENTS_DIR;
10
11
  }
11
12
  /**
12
13
  * Get the directory path for a specific agent
13
14
  */
14
15
  export function getAgentPath(name) {
15
- return join(AGENTS_DIR, name);
16
+ return join(AGENTS_DIR, name);
16
17
  }
17
18
  /**
18
19
  * Check if an agent exists
19
20
  */
20
21
  export async function agentExists(name) {
21
- try {
22
- const agentPath = getAgentPath(name);
23
- const stats = await stat(agentPath);
24
- return stats.isDirectory();
25
- }
26
- catch {
27
- return false;
28
- }
22
+ try {
23
+ const agentPath = getAgentPath(name);
24
+ const stats = await stat(agentPath);
25
+ return stats.isDirectory();
26
+ } catch {
27
+ return false;
28
+ }
29
29
  }
30
30
  /**
31
31
  * List all created agents
32
32
  */
33
33
  export async function listAgents() {
34
- try {
35
- await mkdir(AGENTS_DIR, { recursive: true });
36
- const entries = await readdir(AGENTS_DIR, { withFileTypes: true });
37
- return entries
38
- .filter((entry) => entry.isDirectory())
39
- .map((entry) => entry.name);
40
- }
41
- catch (error) {
42
- console.error("Error listing agents:", error);
43
- return [];
44
- }
34
+ try {
35
+ await mkdir(AGENTS_DIR, { recursive: true });
36
+ const entries = await readdir(AGENTS_DIR, { withFileTypes: true });
37
+ return entries
38
+ .filter((entry) => entry.isDirectory())
39
+ .map((entry) => entry.name);
40
+ } catch (error) {
41
+ console.error("Error listing agents:", error);
42
+ return [];
43
+ }
45
44
  }
46
45
  /**
47
46
  * Delete an agent
48
47
  */
49
48
  export async function deleteAgent(name) {
50
- const agentPath = getAgentPath(name);
51
- await rm(agentPath, { recursive: true, force: true });
49
+ const agentPath = getAgentPath(name);
50
+ await rm(agentPath, { recursive: true, force: true });
52
51
  }
53
52
  /**
54
53
  * Ensure the agents directory exists
55
54
  */
56
55
  export async function ensureAgentsDir() {
57
- await mkdir(AGENTS_DIR, { recursive: true });
56
+ await mkdir(AGENTS_DIR, { recursive: true });
58
57
  }
@@ -1,17 +1,18 @@
1
1
  import { LangchainAgent } from "./runner/langchain";
2
+
2
3
  const agent = new LangchainAgent({
3
- model: "claude-sonnet-4-5-20250929",
4
- systemPrompt: "You are a helpful assistant.",
5
- tools: ["todo_write"],
4
+ model: "claude-sonnet-4-5-20250929",
5
+ systemPrompt: "You are a helpful assistant.",
6
+ tools: ["todo_write"],
6
7
  });
7
8
  for await (const event of agent.invoke({
8
- prompt: [
9
- {
10
- type: "text",
11
- text: "Whats the weather in Tokyo?",
12
- },
13
- ],
14
- sessionId: "test-session",
9
+ prompt: [
10
+ {
11
+ type: "text",
12
+ text: "Whats the weather in Tokyo?",
13
+ },
14
+ ],
15
+ sessionId: "test-session",
15
16
  })) {
16
- console.log(event);
17
+ console.log(event);
17
18
  }