xonecas.io
5875
Os

ollama-space-molt-player / commit bf6b624

feat: add character customization options to swarm mode

xonecas2026-02-01 bf6b62453a16347f4ec22331eac8c45241cc35f6 parent 13da8ee

1 file changed

src/swarm.ts+86−3
@@ -1,5 +1,6 @@
1+import { Database } from "bun:sqlite";
12 import { spawn } from "node:child_process";
2-import { appendFileSync } from "node:fs";
3+import { appendFileSync, existsSync } from "node:fs";
34 import { parseArgs } from "node:util";
45
56 import { isValidAlignment, isValidEmpire, isValidPersonality } from "./utils";
@@ -8,6 +9,9 @@ const DEFAULT_COUNT = 5;
89 const DEFAULT_PREFIX = "swarm";
910 const DEFAULT_RESTART_DELAY_MS = 10000;
1011 const SWARM_LOG_PATH = "swarm.log";
12+const GOAL_TICK_DELAY_MS = 11000;
13+const GOAL_EVERY_TICKS = 3;
14+const GOAL_INTERVAL_MS = GOAL_TICK_DELAY_MS * GOAL_EVERY_TICKS;
1115
1216 const { values } = parseArgs({
1317 args: process.argv.slice(2),
@@ -146,6 +150,9 @@ const swarmConfig: SwarmConfig = {
146150
147151 const children = new Map<string, ReturnType<typeof spawn>>();
148152 const restartTimers = new Map<string, ReturnType<typeof setTimeout>>();
153+const goalDbs = new Map<string, Database>();
154+let swarmMembers: string[] = [];
155+let goalTimer: ReturnType<typeof setInterval> | null = null;
149156 let stopping = false;
150157
151158 function buildInstanceName(
@@ -158,6 +165,14 @@ function buildInstanceName(
158165 return `${prefix}-${padded}`;
159166 }
160167
168+function buildSwarmMembers(): string[] {
169+ const names: string[] = [];
170+ for (let i = 1; i <= swarmConfig.count; i += 1) {
171+ names.push(buildInstanceName(i, swarmConfig.count, swarmConfig.prefix));
172+ }
173+ return names;
174+}
175+
161176 function buildArgs(instanceName: string): string[] {
162177 const args = [
163178 "run",
@@ -178,6 +193,58 @@ function buildArgs(instanceName: string): string[] {
178193 return args;
179194 }
180195
196+function getMemoryPath(instanceName: string): string {
197+ return `memory-${instanceName}.sqlite`;
198+}
199+
200+function getGoalDb(instanceName: string): Database | null {
201+ const existing = goalDbs.get(instanceName);
202+ if (existing) return existing;
203+ const memoryPath = getMemoryPath(instanceName);
204+ if (!existsSync(memoryPath)) return null;
205+ const db = new Database(memoryPath);
206+ goalDbs.set(instanceName, db);
207+ return db;
208+}
209+
210+function readLatestGoal(
211+ instanceName: string,
212+):
213+ | { status: "ok"; goal: string | null }
214+ | { status: "missing" }
215+ | { status: "error"; message: string } {
216+ const db = getGoalDb(instanceName);
217+ if (!db) return { status: "missing" };
218+ try {
219+ const row = db
220+ .query("SELECT goal FROM llm_goals ORDER BY id DESC LIMIT 1")
221+ .get() as { goal?: string } | undefined;
222+ return { status: "ok", goal: row?.goal ?? null };
223+ } catch (error) {
224+ return { status: "error", message: (error as Error).message };
225+ }
226+}
227+
228+function logGoalsOnce(): void {
229+ if (stopping) return;
230+ for (const name of swarmMembers) {
231+ const result = readLatestGoal(name);
232+ if (result.status === "missing") {
233+ logSwarm(`goal ${name}: <db missing>`);
234+ continue;
235+ }
236+ if (result.status === "error") {
237+ logSwarm(`goal ${name}: error (${result.message})`);
238+ continue;
239+ }
240+ if (result.goal) {
241+ logSwarm(`goal ${name}: ${result.goal}`);
242+ continue;
243+ }
244+ logSwarm(`goal ${name}: <none yet>`);
245+ }
246+}
247+
181248 function spawnBot(instanceName: string): void {
182249 if (stopping) return;
183250 const args = buildArgs(instanceName);
@@ -215,10 +282,22 @@ function shutdown(reason: string): void {
215282 if (stopping) return;
216283 stopping = true;
217284 logSwarm(`shutdown ${reason}`);
285+ if (goalTimer) {
286+ clearInterval(goalTimer);
287+ goalTimer = null;
288+ }
218289 for (const timer of restartTimers.values()) {
219290 clearTimeout(timer);
220291 }
221292 restartTimers.clear();
293+ for (const [name, db] of goalDbs.entries()) {
294+ try {
295+ db.close();
296+ } catch (error) {
297+ logSwarm(`failed to close goal db ${name} (${(error as Error).message})`);
298+ }
299+ }
300+ goalDbs.clear();
222301 for (const [name, child] of children.entries()) {
223302 try {
224303 child.kill("SIGTERM");
@@ -239,10 +318,14 @@ function startSwarm(): void {
239318 if (swarmConfig.personality)
240319 logSwarm(`override personality=${swarmConfig.personality}`);
241320
242- for (let i = 1; i <= swarmConfig.count; i += 1) {
243- const name = buildInstanceName(i, swarmConfig.count, swarmConfig.prefix);
321+ swarmMembers = buildSwarmMembers();
322+ for (const name of swarmMembers) {
244323 spawnBot(name);
245324 }
325+ if (!goalTimer) {
326+ goalTimer = setInterval(logGoalsOnce, GOAL_INTERVAL_MS);
327+ logSwarm(`goals every ${GOAL_EVERY_TICKS} ticks (${GOAL_INTERVAL_MS}ms)`);
328+ }
246329 }
247330
248331 process.on("SIGINT", () => shutdown("SIGINT"));