worldagents 0.1.0
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.
- package/README.md +21 -0
- package/package.json +23 -0
- package/src/cli.js +104 -0
- package/src/create-agent.js +68 -0
- package/src/index.js +5 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# worldagents
|
|
2
|
+
|
|
3
|
+
WorldAgents cli.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g worldagents
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or run it without installing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx worldagents@latest create my-agent
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## CLI
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
worldagents create my-agent
|
|
21
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "worldagents",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "WorldAgents cli",
|
|
6
|
+
"bin": {
|
|
7
|
+
"worldagents": "./src/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.js",
|
|
11
|
+
"./cli": "./src/cli.js",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"README.md",
|
|
16
|
+
"src/"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"agent",
|
|
20
|
+
"cli",
|
|
21
|
+
"worldagents"
|
|
22
|
+
]
|
|
23
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { CreateAgentError, createAgent } from "./create-agent.js";
|
|
6
|
+
|
|
7
|
+
const USAGE = `Usage:
|
|
8
|
+
worldagents create [path] [--agent-name <name>] [--force]
|
|
9
|
+
|
|
10
|
+
Commands:
|
|
11
|
+
create Create a minimal agent stub.
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
-n, --agent-name <name> Agent name written into the scaffold.
|
|
15
|
+
-f, --force Allow writing into a non-empty directory.
|
|
16
|
+
-h, --help Show help.
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
const argv = process.argv.slice(2);
|
|
21
|
+
const [command, ...commandArgs] = argv;
|
|
22
|
+
|
|
23
|
+
if (!command || command === "-h" || command === "--help") {
|
|
24
|
+
printUsage();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (command === "create") {
|
|
29
|
+
await handleCreate(commandArgs);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
throw usageError(`Unknown command: ${command}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function handleCreate(args) {
|
|
37
|
+
const { values, positionals } = parseCommandArgs(args, {
|
|
38
|
+
"agent-name": { type: "string", short: "n" },
|
|
39
|
+
force: { type: "boolean", short: "f" },
|
|
40
|
+
help: { type: "boolean", short: "h" },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (values.help) {
|
|
44
|
+
printUsage();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (positionals.length > 1) {
|
|
49
|
+
throw usageError("Too many positional arguments for create.");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const targetPath = path.resolve(positionals[0] ?? process.cwd());
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const created = await createAgent({
|
|
56
|
+
path: targetPath,
|
|
57
|
+
agentName: values["agent-name"],
|
|
58
|
+
force: values.force,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
logInfo(`Created agent stub at: ${created.agentPath}`);
|
|
62
|
+
logInfo(`Agent name: ${created.agentName}`);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error instanceof CreateAgentError) {
|
|
65
|
+
throw usageError(error.message);
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseCommandArgs(args, options) {
|
|
72
|
+
try {
|
|
73
|
+
return parseArgs({ args, allowPositionals: true, options });
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw usageError(error.message);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function usageError(message) {
|
|
80
|
+
const error = new Error(message);
|
|
81
|
+
error.exitCode = 2;
|
|
82
|
+
return error;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function printUsage() {
|
|
86
|
+
console.log(USAGE.trim());
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function logInfo(message) {
|
|
90
|
+
console.log(message);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function logError(message) {
|
|
94
|
+
console.error(`error: ${message}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
main().catch((error) => {
|
|
98
|
+
logError(error.message);
|
|
99
|
+
if (error.exitCode === 2) {
|
|
100
|
+
console.error("");
|
|
101
|
+
printUsage();
|
|
102
|
+
}
|
|
103
|
+
process.exitCode = error.exitCode ?? 1;
|
|
104
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const CONFIG_FILE_NAME = "worldagents.config.json";
|
|
5
|
+
|
|
6
|
+
export class CreateAgentError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "CreateAgentError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function createAgent(options) {
|
|
14
|
+
if (!options || typeof options !== "object") {
|
|
15
|
+
throw new CreateAgentError("Options object is required.");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (typeof options.path !== "string" || options.path.trim() === "") {
|
|
19
|
+
throw new CreateAgentError("path is required.");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (options.force != null && typeof options.force !== "boolean") {
|
|
23
|
+
throw new CreateAgentError("force must be a boolean when provided.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const agentPath = path.resolve(options.path);
|
|
27
|
+
const existing = await statIfExists(agentPath);
|
|
28
|
+
|
|
29
|
+
if (existing && !existing.isDirectory()) {
|
|
30
|
+
throw new CreateAgentError(`Path exists and is not a directory: ${agentPath}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (existing && !options.force) {
|
|
34
|
+
const entries = await fs.readdir(agentPath);
|
|
35
|
+
if (entries.length > 0) {
|
|
36
|
+
throw new CreateAgentError(`Directory is not empty: ${agentPath}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await fs.mkdir(agentPath, { recursive: true });
|
|
41
|
+
|
|
42
|
+
const agentName =
|
|
43
|
+
typeof options.agentName === "string" && options.agentName.trim() !== ""
|
|
44
|
+
? options.agentName.trim()
|
|
45
|
+
: path.basename(agentPath);
|
|
46
|
+
|
|
47
|
+
await fs.writeFile(
|
|
48
|
+
path.join(agentPath, CONFIG_FILE_NAME),
|
|
49
|
+
`${JSON.stringify({ name: agentName }, null, 2)}\n`,
|
|
50
|
+
"utf8",
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
agentPath,
|
|
55
|
+
agentName,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function statIfExists(filePath) {
|
|
60
|
+
try {
|
|
61
|
+
return await fs.stat(filePath);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error && error.code === "ENOENT") {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|