letta-city-cli 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 ADDED
@@ -0,0 +1,43 @@
1
+ # lcity CLI (Node.js)
2
+
3
+ Shared command-line tool layer for all Letta agents in `letta-city-sim`.
4
+
5
+ This package is structured to be published independently.
6
+
7
+ - entrypoint: `bin/lcity.mjs`
8
+ - command implementation: `src/cli.mjs`
9
+ - package metadata: `package.json`
10
+
11
+ ## Run directly
12
+
13
+ ```powershell
14
+ node .\lcity\bin\lcity.mjs health_check
15
+ ```
16
+
17
+ ## Use `.lcity/agent_id`
18
+
19
+ ```powershell
20
+ New-Item -ItemType Directory -Force .lcity | Out-Null
21
+ Set-Content .lcity\agent_id "eddy_lin"
22
+ node .\lcity\bin\lcity.mjs health_check
23
+ ```
24
+
25
+ ## Install locally as command
26
+
27
+ ```powershell
28
+ npm --prefix .\lcity install
29
+ npm --prefix .\lcity link
30
+ lcity health_check
31
+ ```
32
+
33
+ Optional base URL override:
34
+
35
+ ```powershell
36
+ node .\lcity\bin\lcity.mjs --api-base http://localhost:3001 health_check
37
+ ```
38
+
39
+ Output is always JSON:
40
+
41
+ ```json
42
+ {"ok":true,"status_code":200,"data":{"status":"ok","agent_id":"eddy_lin","letta_agent_id":"...","current_location_id":"lin_bedroom","state":"idle"}}
43
+ ```
package/bin/lcity.mjs ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ import { run } from "../src/cli.mjs";
3
+
4
+ const code = await run(process.argv.slice(2));
5
+ process.exit(code);
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "letta-city-cli",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "bin": {
7
+ "lcity": "./bin/lcity.mjs"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "src",
12
+ "README.md"
13
+ ]
14
+ }
package/src/cli.mjs ADDED
@@ -0,0 +1,118 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ function parseArgs(argv) {
5
+ const args = {
6
+ command: null,
7
+ apiBase: process.env.LCITY_API_BASE || "http://localhost:3001",
8
+ agentIdFile: path.join(".lcity", "agent_id"),
9
+ };
10
+
11
+ const tokens = [...argv];
12
+ while (tokens.length > 0) {
13
+ const token = tokens.shift();
14
+
15
+ if (!args.command && !token.startsWith("--")) {
16
+ args.command = token;
17
+ continue;
18
+ }
19
+
20
+ if (token === "--api-base") {
21
+ args.apiBase = tokens.shift() || args.apiBase;
22
+ continue;
23
+ }
24
+
25
+ if (token === "--agent-id-file") {
26
+ args.agentIdFile = tokens.shift() || args.agentIdFile;
27
+ continue;
28
+ }
29
+ }
30
+
31
+ return args;
32
+ }
33
+
34
+ function resolveAgentId(agentIdFile) {
35
+ if (fs.existsSync(agentIdFile)) {
36
+ try {
37
+ const fileAgentId = fs.readFileSync(agentIdFile, "utf8").trim();
38
+ if (fileAgentId) {
39
+ return { agentId: fileAgentId, error: null };
40
+ }
41
+ } catch (err) {
42
+ return { agentId: null, error: `failed to read agent id file: ${err.message}` };
43
+ }
44
+ }
45
+
46
+ return {
47
+ agentId: null,
48
+ error: "missing ./.lcity/agent_id (or pass --agent-id-file)",
49
+ };
50
+ }
51
+
52
+ async function requestJson(url, { method = "GET", headers = {} } = {}) {
53
+ try {
54
+ const response = await fetch(url, { method, headers });
55
+ const text = await response.text();
56
+ let data = {};
57
+ if (text) {
58
+ try {
59
+ data = JSON.parse(text);
60
+ } catch {
61
+ data = { error: text };
62
+ }
63
+ }
64
+
65
+ return { statusCode: response.status, data };
66
+ } catch (err) {
67
+ return { statusCode: 0, data: { error: `network error: ${err.message}` } };
68
+ }
69
+ }
70
+
71
+ async function healthCheck(apiBase, agentId) {
72
+ const { statusCode, data } = await requestJson(`${apiBase.replace(/\/$/, "")}/agents/health`, {
73
+ method: "GET",
74
+ headers: {
75
+ "x-agent-id": agentId,
76
+ },
77
+ });
78
+
79
+ const output = {
80
+ ok: statusCode === 200,
81
+ status_code: statusCode,
82
+ data,
83
+ };
84
+
85
+ console.log(JSON.stringify(output));
86
+ return statusCode === 200 ? 0 : 1;
87
+ }
88
+
89
+ export async function run(argv) {
90
+ const args = parseArgs(argv);
91
+
92
+ if (!args.command || args.command === "help" || args.command === "--help") {
93
+ console.log(
94
+ JSON.stringify({
95
+ ok: true,
96
+ usage: [
97
+ "lcity health_check",
98
+ "lcity --api-base http://localhost:3001 health_check",
99
+ "lcity health_check --agent-id-file .lcity/agent_id",
100
+ ],
101
+ }),
102
+ );
103
+ return 0;
104
+ }
105
+
106
+ if (args.command === "health_check") {
107
+ const { agentId, error } = resolveAgentId(args.agentIdFile);
108
+ if (error) {
109
+ console.log(JSON.stringify({ ok: false, error }));
110
+ return 1;
111
+ }
112
+
113
+ return healthCheck(args.apiBase, agentId);
114
+ }
115
+
116
+ console.log(JSON.stringify({ ok: false, error: `unknown command: ${args.command}` }));
117
+ return 1;
118
+ }