@mclawnet/agent 0.2.0 → 0.2.1

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/cli.js CHANGED
@@ -1,82 +1,74 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- /**
4
- * clawnet-agent CLI
5
- *
6
- * Usage:
7
- * clawnet-agent start [options] Start OpenClaw gateway + connect to Hub
8
- * clawnet-agent stop Stop running agent
9
- * clawnet-agent status Show agent status
10
- * clawnet-agent --version Show version
11
- * clawnet-agent --help Show help
12
- *
13
- * Start options:
14
- * --hub <url> Hub WebSocket URL (default: built-in, overridable)
15
- * --token <token> Instance token from Hub dashboard (clw_xxx)
16
- * --port <port> OpenClaw gateway local port (default: 18789)
17
- * --name <name> Instance display name
18
- */
19
-
20
- import { readFileSync } from "node:fs";
21
- import { fileURLToPath } from "node:url";
22
- import { dirname, join } from "node:path";
23
-
24
- const __dirname = dirname(fileURLToPath(import.meta.url));
25
- const pkg = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8"));
26
-
27
3
  const args = process.argv.slice(2);
4
+ const command = args[0] || "start";
28
5
 
29
- if (args.includes("--version") || args.includes("-v")) {
30
- console.log(pkg.version);
31
- process.exit(0);
32
- }
33
-
34
- if (args.includes("--help") || args.includes("-h") || args.length === 0) {
35
- console.log(`
36
- clawnet-agent v${pkg.version}
37
-
38
- Usage:
39
- clawnet-agent start [options] Start OpenClaw gateway + connect to Hub
40
- clawnet-agent status Show agent status
41
- clawnet-agent --version Show version
6
+ if (command === "start") {
7
+ // Parse CLI flags
8
+ const opts = {};
9
+ for (let i = 1; i < args.length; i++) {
10
+ if (args[i] === "--hub-url" && args[i + 1]) {
11
+ opts.hubUrl = args[++i];
12
+ } else if (args[i] === "--token" && args[i + 1]) {
13
+ opts.token = args[++i];
14
+ } else if (args[i] === "--name" && args[i + 1]) {
15
+ opts.name = args[++i];
16
+ } else if (args[i] === "--backend" && args[i + 1]) {
17
+ opts.backendType = args[++i];
18
+ }
19
+ }
42
20
 
43
- Start options:
44
- --hub <url> Hub WebSocket URL (env: CLAWNET_HUB_URL)
45
- --token <token> Instance token from Hub (env: CLAWNET_TOKEN)
46
- --port <port> OpenClaw gateway port (env: CLAWNET_PORT)
47
- Default: 18789
48
- --name <name> Instance display name (env: CLAWNET_NAME)
21
+ // Default to claude-code adapter
22
+ const backendType = opts.backendType || "claude-code";
49
23
 
50
- Examples:
51
- clawnet-agent start --token clw_abc123
52
- clawnet-agent start --hub ws://hub.example.com:3000/ws/agent --token clw_abc123
53
- CLAWNET_TOKEN=clw_abc123 clawnet-agent start
54
- `);
55
- process.exit(0);
56
- }
24
+ async function main() {
25
+ let adapter;
57
26
 
58
- const command = args[0];
27
+ if (backendType === "claude-code") {
28
+ try {
29
+ const { ClaudeCodeAdapter } = await import("@mclawnet/claude-adapter");
30
+ adapter = new ClaudeCodeAdapter();
31
+ } catch (err) {
32
+ console.error("[clawnet] Failed to load @mclawnet/claude-adapter:", err.message);
33
+ console.error("[clawnet] Install it: pnpm add @mclawnet/claude-adapter");
34
+ process.exit(1);
35
+ }
36
+ } else {
37
+ console.error(`[clawnet] Unknown backend type: ${backendType}`);
38
+ process.exit(1);
39
+ }
59
40
 
60
- function getArg(flag) {
61
- const idx = args.indexOf(flag);
62
- return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : undefined;
63
- }
64
-
65
- if (command === "start") {
66
- const opts = {
67
- hubUrl: getArg("--hub") || process.env.CLAWNET_HUB_URL,
68
- token: getArg("--token") || process.env.CLAWNET_TOKEN,
69
- port: getArg("--port") || process.env.CLAWNET_PORT,
70
- name: getArg("--name") || process.env.CLAWNET_NAME,
71
- };
41
+ const { startAgent } = await import("./dist/start.js");
42
+ await startAgent({ config: opts, adapter });
43
+ }
72
44
 
73
- const { startAgent } = await import("./dist/start.js");
74
- await startAgent(opts);
75
- } else if (command === "status") {
76
- const { showStatus } = await import("./dist/start.js");
77
- showStatus();
45
+ main().catch((err) => {
46
+ console.error("[clawnet] Fatal:", err);
47
+ process.exit(1);
48
+ });
49
+ } else if (command === "config") {
50
+ const subCmd = args[1];
51
+ import("./dist/index.js").then(({ loadConfig, saveConfig }) => {
52
+ if (subCmd === "set" && args[2] && args[3]) {
53
+ saveConfig({ [args[2]]: args[3] });
54
+ console.log(`Set ${args[2]} = ${args[3]}`);
55
+ } else if (subCmd === "show") {
56
+ console.log(JSON.stringify(loadConfig(), null, 2));
57
+ } else {
58
+ console.log("Usage: clawnet-agent config set <key> <value>");
59
+ console.log(" clawnet-agent config show");
60
+ }
61
+ });
78
62
  } else {
79
- console.error(`Unknown command: ${command}`);
80
- console.error('Run "clawnet-agent --help" for usage.');
81
- process.exit(1);
63
+ console.log("Usage: clawnet-agent [start|config] [options]");
64
+ console.log("");
65
+ console.log("Commands:");
66
+ console.log(" start Start the agent (default)");
67
+ console.log(" config View/set configuration");
68
+ console.log("");
69
+ console.log("Options:");
70
+ console.log(" --hub-url <url> Hub WebSocket URL");
71
+ console.log(" --token <token> Agent token");
72
+ console.log(" --name <name> Agent name");
73
+ console.log(" --backend <type> Backend type (claude-code)");
82
74
  }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * BackendAdapter — pluggable backend for AI model interaction.
3
+ *
4
+ * Implementations:
5
+ * - ClaudeCodeAdapter (@mclawnet/claude-adapter): spawn `claude` CLI
6
+ * - OpenClawAdapter: spawn `openclaw gateway` (legacy)
7
+ * - CodexAdapter: future
8
+ */
9
+ export interface BackendProcess {
10
+ /** Unique identifier (e.g., Claude session ID) */
11
+ id: string;
12
+ /** Working directory for this process */
13
+ workDir: string;
14
+ /** Kill the backend process */
15
+ kill(): Promise<void>;
16
+ }
17
+ export interface SpawnOptions {
18
+ /** ClawNet session ID */
19
+ sessionId: string;
20
+ /** Working directory for the backend process */
21
+ workDir?: string;
22
+ /** Resume a previous session (e.g., Claude --resume UUID) */
23
+ resumeId?: string;
24
+ /** System prompt to append (e.g., role prompt for swarm mode) */
25
+ systemPrompt?: string;
26
+ /** Load BrainCore + CoreSkill via --add-dir */
27
+ useBrainCore?: boolean;
28
+ }
29
+ export interface BackendAdapter {
30
+ /** Backend type identifier */
31
+ type: string;
32
+ /** Spawn a new backend process */
33
+ spawn(options: SpawnOptions): Promise<BackendProcess>;
34
+ /** Stop a running backend process */
35
+ stop(process: BackendProcess): Promise<void>;
36
+ /** Send user input to the backend process */
37
+ send(process: BackendProcess, input: string): void;
38
+ /** Register output handler for a backend process */
39
+ onOutput(process: BackendProcess, handler: (msg: unknown) => void): void;
40
+ /** Register turn-complete handler */
41
+ onTurnComplete?(process: BackendProcess, handler: (info: {
42
+ claudeSessionId?: string;
43
+ cost?: number;
44
+ duration?: number;
45
+ contextUsage?: {
46
+ used: number;
47
+ total: number;
48
+ };
49
+ }) => void): void;
50
+ /** Register error handler */
51
+ onError?(process: BackendProcess, handler: (error: Error) => void): void;
52
+ }
53
+ //# sourceMappingURL=backend-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend-adapter.d.ts","sourceRoot":"","sources":["../src/backend-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,cAAc;IAC7B,kDAAkD;IAClD,EAAE,EAAE,MAAM,CAAC;IACX,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,+BAA+B;IAC/B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,yBAAyB;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iEAAiE;IACjE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,+CAA+C;IAC/C,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAC;IAEb,kC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