xonecas.io
5875
Os

ollama-space-molt-player / files

ollama-space-molt-player / packages / client / src / cli.ts

4.7 KB
1import { listArchetypes } from "./archetypes.ts";
2import { listInstances } from "./db.ts";
3import { getModelListText } from "./model-config.ts";
4import type { ClientConfig } from "./types.ts";
5
6export interface ParsedArgs {
7	command: "start" | "update-hint" | "update-prompt" | "list-instances" | "help";
8	config?: ClientConfig;
9	instanceId?: string;
10	hint?: string;
11	prompt?: string;
12	username?: string;
13}
14
15export function parseArgs(args: string[]): ParsedArgs {
16	const cleanArgs = args;
17
18	if (cleanArgs.length === 0 || cleanArgs[0] === "help" || cleanArgs[0] === "--help") {
19		return { command: "help" };
20	}
21
22	const command = cleanArgs[0];
23
24	if (command === "list-instances") {
25		return { command: "list-instances" };
26	}
27
28	if (command === "update-hint") {
29		const instanceId = getArg(cleanArgs, "--instance", "-i");
30		const hint = getArg(cleanArgs, "--hint");
31
32		if (!instanceId || !hint) {
33			throw new Error("update-hint requires --instance and --hint");
34		}
35
36		return {
37			command: "update-hint",
38			instanceId,
39			hint,
40		};
41	}
42
43	if (command === "update-prompt") {
44		const instanceId = getArg(cleanArgs, "--instance", "-i");
45		const prompt = getArg(cleanArgs, "--prompt");
46		const username = getArg(cleanArgs, "--username", "-u");
47
48		if (!instanceId || !prompt || !username) {
49			throw new Error("update-prompt requires --instance, --username, and --prompt");
50		}
51
52		return {
53			command: "update-prompt",
54			instanceId,
55			prompt,
56			username,
57		};
58	}
59
60	if (command === "start" || command.startsWith("-")) {
61		const contextWindowArg = getArg(cleanArgs, "--context-window", "-cw");
62		const config: ClientConfig = {
63			instanceId: getArg(cleanArgs, "--instance", "-i"),
64			hint: getArg(cleanArgs, "--hint"),
65			model: getArg(cleanArgs, "--model", "-m") || undefined,
66			verbose: cleanArgs.includes("--verbose"),
67			archetype: getArg(cleanArgs, "--archetype", "-a"),
68			tickRate: 20000,
69			ollamaTimeout: Number.parseInt(getArg(cleanArgs, "--ollama-timeout") || "30000"),
70			serverUrl: getArg(cleanArgs, "--server", "-s"),
71			contextWindowSize: contextWindowArg ? Number.parseInt(contextWindowArg) : undefined,
72		};
73
74		return {
75			command: "start",
76			config,
77		};
78	}
79
80	throw new Error(`Unknown command: ${command}`);
81}
82
83function getArg(args: string[], ...flags: string[]): string | undefined {
84	for (const flag of flags) {
85		const index = args.indexOf(flag);
86		if (index !== -1 && index + 1 < args.length) {
87			return args[index + 1];
88		}
89	}
90	return undefined;
91}
92
93export function printHelp(): void {
94	console.log(`
95SpaceMolt LLM Game Client
96
97USAGE:
98  bun start [OPTIONS]
99  bun start update-hint --instance <id> --hint <text>
100  bun start update-prompt --instance <id> --username <name> --prompt <text>
101  bun start list-instances
102  bun start help
103
104COMMANDS:
105  start              Start the game client (default)
106  update-hint        Update hint for an existing instance
107  update-prompt      Update prompt for a specific username
108  list-instances     List all database instances
109  help               Show this help message
110
111OPTIONS:
112  -i, --instance <id>         Instance ID (4 chars). Auto-generated if not provided.
113  --hint <text>               Hint message to inject into LLM context each tick
114  -m, --model <name>          Model configuration name (see MODELS below, default in player-models.json)
115  --verbose                   Enable detailed logging (shows full message payloads)
116  -a, --archetype <name>      Restrict LLM to single archetype choice (diplomat, opportunist, agitator)
117  --ollama-timeout <ms>       Ollama API timeout in milliseconds (default: 30000)
118  -s, --server <url>          MCP server URL (default: https://game.spacemolt.com/mcp)
119  -cw, --context-window <n>   Number of recent messages to include in LLM context (default: 20)
120
121ARCHETYPES:
122${listArchetypes()
123	.map((a) => `  ${a.name.padEnd(15)} ${a.description}`)
124	.join("\n")}
125
126MODELS:
127${getModelListText()}
128
129  For full model details, see player-models.json at repo root.
130
131EXAMPLES:
132  # Start new game with default model (qwen3-8b)
133  bun start
134
135  # Start with specific instance ID and archetype
136  bun start --instance abc1 --archetype diplomat
137
138  # Use a different model configuration
139  bun start --model llama3.1-8b --archetype diplomat
140
141  # Start with hint
142  bun start --hint "Focus on mining and trading"
143
144  # Update hint for existing instance
145  bun start update-hint --instance abc1 --hint "New strategy: explore"
146
147  # Update prompt for specific username
148  bun start update-prompt --instance abc1 --username PlayerOne --prompt "You are a ruthless trader"
149
150  # List all instances
151  bun start list-instances
152`);
153}
154
155export function printInstances(): void {
156	const instances = listInstances();
157	if (instances.length === 0) {
158		console.log("No instances found.");
159		return;
160	}
161
162	console.log("Available instances:");
163	for (const id of instances) {
164		console.log(`  - ${id}`);
165	}
166}