@@ -1,4 +1,5 @@
11 import { SpaceMoltClient } from "../client/src/client";
2+import type { ClientState } from "../client/src/client";
23 import type {
34 WelcomePayload,
45 RegisteredPayload,
@@ -8,6 +9,7 @@ import type {
89 ChatMessage,
910 ScanResultPayload,
1011 EmpireID,
12+ CargoItem,
1113 } from "../client/src/types";
1214 import { config } from "./config";
1315 import { MemoryStore } from "./memory";
@@ -21,11 +23,11 @@ type RegistrationChoice = { username: string; empire: EmpireID };
2123
2224 const memory = new MemoryStore(config.memoryPath);
2325 const ollama = new OllamaAgent(config.ollamaUrl, config.ollamaModel, config.ollamaTimeoutMs);
24-const tui = new Tui();
26+const tui = new Tui(config.debug);
2527
2628 const client = new SpaceMoltClient({
2729 url: config.spacemoltUrl,
28- debug: config.debug,
30+ debug: false, //config.debug,
2931 reconnect: true,
3032 });
3133
@@ -36,6 +38,7 @@ let actionLoopRunning = false;
3638 let worldSnapshot: WorldSnapshot = {};
3739 let registrationRetries = 0;
3840 let travelInProgress = false;
41+let lastTravelTarget: string | null = null;
3942 const MAX_REGISTRATION_RETRIES = 3;
4043 const failedRegistrationNames: string[] = [];
4144
@@ -64,27 +67,13 @@ function updateStatus(): void {
6467 const state = client.state;
6568 if (!state.player || !state.ship) {
6669 tui.setStatus("Not logged in");
70+ tui.setSidebar("Not logged in.");
6771 return;
6872 }
69- const location = `${state.system?.name ?? state.player.current_system} - ${
70- state.poi?.name ?? state.player.current_poi
71- }`;
72- const status = [
73- `Player ${state.player.username} [${state.player.empire}]`,
74- `Cr ${state.player.credits}`,
75- `Hull ${state.ship.hull}/${state.ship.max_hull}`,
76- `Shield ${state.ship.shield}/${state.ship.max_shield}`,
77- `Fuel ${state.ship.fuel}/${state.ship.max_fuel}`,
78- `Traveling ${travelInProgress ? "Y" : "N"}`,
79- `Docked ${state.player.docked_at_base ? "Y" : "N"}`,
80- `Tick ${state.currentTick}`,
81- location,
82- ].join(" | ");
83- const cargoList = state.ship.cargo?.length
84- ? state.ship.cargo.map((item) => `${item.item_id}(${item.quantity})`).join(",")
85- : "none";
86- const statusWithCargo = `${status} | Cargo ${state.ship.cargo_used}/${state.ship.cargo_capacity} [${cargoList}]`;
87- tui.setStatus(statusWithCargo);
73+ const statusText = buildStatusText(state, worldSnapshot, travelInProgress, lastTravelTarget);
74+ const sidebarText = formatSidebar(state, worldSnapshot, travelInProgress, lastTravelTarget);
75+ tui.setStatus(statusText);
76+ tui.setSidebar(sidebarText);
8877 }
8978
9079 function saveSnapshot(): void {
@@ -106,6 +95,7 @@ async function runRegistrationFlow(): Promise<void> {
10695 attempts += 1;
10796 try {
10897 const prompt = buildRegistrationPrompt(true, failedRegistrationNames);
98+ tui.setPrompt(prompt);
10999 const result = await ollama.generateJson<RegistrationChoice>(prompt);
110100 const username = String(result.json.username ?? "").trim();
111101 const empire = String(result.json.empire ?? "").trim() as EmpireID;
@@ -141,16 +131,16 @@ async function startActionLoop(): Promise<void> {
141131 await sleep(config.tickDelayMs);
142132 continue;
143133 }
144- const recentActions = memory.getRecentActions(config.maxContextActions);
145- const recentEvents = memory.getRecentEvents(config.maxContextEvents);
134+ const recentHistory = memory.getRecentHistory(config.maxContextActions, config.maxContextEvents);
146135 const prompt = buildActionPrompt({
147136 state: client.state,
148137 worldSnapshot,
149- recentActions,
150- recentEvents,
138+ recentHistory,
151139 includeHelp: includeHelpInPrompt,
152140 });
153141
142+ tui.setPrompt(prompt);
143+
154144 const promptExcerpt = prompt.slice(0, 1000);
155145 const result = await ollama.generateJson(prompt);
156146 const validation = validateAction(result.json);
@@ -178,21 +168,24 @@ async function startActionLoop(): Promise<void> {
178168 continue;
179169 }
180170
181- memory.appendAction(
182- validation.action.action,
183- validation.action.args ?? {},
184- promptExcerpt,
185- result.raw
186- );
187- includeHelpInPrompt = false;
188-
189- const actionLog = dispatchAction(client, validation.action);
190- log(`Action: ${actionLog}`);
191- memory.appendEvent("action_sent", { action: validation.action });
192-
193- if (validation.action.action === "travel") {
194- client.getSystem();
195- client.getPOI();
171+ const actionName = validation.action.action;
172+ memory.appendAction(actionName, validation.action.args ?? {}, promptExcerpt, result.raw);
173+ includeHelpInPrompt = actionName === "help";
174+
175+ if (actionName === "help") {
176+ log("Action: help (showing help menu)");
177+ } else {
178+ if (actionName === "travel") {
179+ lastTravelTarget = String(validation.action.args?.target_poi ?? "").trim() || null;
180+ }
181+ const actionLog = dispatchAction(client, validation.action);
182+ log(`Action: ${actionLog}`);
183+ memory.appendEvent("action_sent", { action: validation.action });
184+
185+ if (actionName === "travel") {
186+ client.getSystem();
187+ client.getPOI();
188+ }
196189 }
197190 } catch (error) {
198191 const message = (error as Error).message;
@@ -373,6 +366,7 @@ client.on("ok", (data: Record<string, unknown>) => {
373366 }
374367 if (action === "arrived") {
375368 travelInProgress = false;
369+ lastTravelTarget = null;
376370 client.getSystem();
377371 client.getPOI();
378372 }
@@ -401,3 +395,186 @@ function updateWorldSnapshotFromOk(data: Record<string, unknown>): void {
401395 worldSnapshot.base = data.base as WorldSnapshot["base"];
402396 }
403397 }
398+
399+const SIDEBAR_WIDTH = 30;
400+const SIDEBAR_CONTENT_WIDTH = SIDEBAR_WIDTH - 2;
401+
402+function buildStatusText(
403+ state: ClientState,
404+ snapshot: WorldSnapshot,
405+ traveling: boolean,
406+ travelTargetId: string | null
407+): string {
408+ if (!state.player || !state.ship) return "Not logged in";
409+ if (traveling) {
410+ const destination = resolvePoiName(travelTargetId, snapshot, state);
411+ return `Traveling to ${destination}`;
412+ }
413+ if (state.player.docked_at_base) return "Docked";
414+ const currentPoi = resolvePoiName(state.player.current_poi, snapshot, state);
415+ return `At ${currentPoi}`;
416+}
417+
418+function formatSidebar(
419+ state: ClientState,
420+ snapshot: WorldSnapshot,
421+ traveling: boolean,
422+ travelTargetId: string | null
423+): string {
424+ if (!state.player || !state.ship) return "Not logged in.";
425+
426+ const width = SIDEBAR_CONTENT_WIDTH;
427+ const lines: string[] = [];
428+ const pushSection = (title: string, sectionLines: string[]): void => {
429+ if (lines.length) lines.push("");
430+ const titleText = truncate(`== ${title} ==`, width);
431+ lines.push(`{cyan-fg}${titleText}{/cyan-fg}`);
432+ for (const line of sectionLines) {
433+ const truncated = truncate(line, width);
434+ const colored = colorizeLabel(truncated);
435+ lines.push(styleLine(colored));
436+ }
437+ };
438+
439+ const system = snapshot.system ?? state.system;
440+ const poi = snapshot.poi ?? state.poi;
441+ const base = snapshot.base ?? state.base;
442+ const pois = snapshot.pois ?? [];
443+
444+ pushSection("Player", [
445+ formatLine("Name", state.player.username, width),
446+ formatLine("Empire", state.player.empire, width),
447+ formatLine("Credits", String(state.player.credits), width),
448+ formatLine("Docked", state.player.docked_at_base ? "Y" : "N", width),
449+ formatLine("Tick", String(state.currentTick), width),
450+ ]);
451+
452+ pushSection("Ship", [
453+ formatLine("Hull", `${state.ship.hull}/${state.ship.max_hull}`, width),
454+ formatLine("Shield", `${state.ship.shield}/${state.ship.max_shield}`, width),
455+ formatLine("Fuel", `${state.ship.fuel}/${state.ship.max_fuel}`, width),
456+ formatLine("Cargo", `${state.ship.cargo_used}/${state.ship.cargo_capacity}`, width),
457+ ]);
458+
459+ const locationLines = [
460+ formatLine("System", system?.name ?? state.player.current_system ?? "-", width),
461+ formatLine("POI", poi?.name ?? state.player.current_poi ?? "-", width),
462+ formatLine("Travel", traveling ? "Y" : "N", width),
463+ ];
464+ if (traveling) {
465+ locationLines.push(formatLine("Dest", resolvePoiName(travelTargetId, snapshot, state), width));
466+ }
467+ pushSection("Location", locationLines);
468+
469+ const poiLines = formatPoiLines(pois, system?.pois, width);
470+ pushSection("POIs", poiLines);
471+
472+ const cargoLines = formatCargoLines(state.ship.cargo ?? [], width);
473+ pushSection("Cargo", cargoLines);
474+
475+ const baseLines = formatBaseLines(base, width);
476+ pushSection("Base", baseLines);
477+
478+ const nearbyLines = formatNearbyLines(state.nearby ?? [], width);
479+ pushSection("Nearby", nearbyLines);
480+
481+ return lines.join("\n");
482+}
483+
484+function formatLine(label: string, value: string, width: number): string {
485+ return `${label}: ${value}`;
486+}
487+
488+function truncate(value: string, width: number): string {
489+ if (value.length <= width) return value;
490+ if (width <= 3) return value.slice(0, width);
491+ return `${value.slice(0, width - 3)}...`;
492+}
493+
494+function colorizeLabel(line: string): string {
495+ const index = line.indexOf(":");
496+ if (index <= 0) return line;
497+ const label = line.slice(0, index);
498+ const rest = line.slice(index);
499+ return `{yellow-fg}${label}{/yellow-fg}${rest}`;
500+}
501+
502+function styleLine(line: string): string {
503+ if (line.includes("{")) return line;
504+ if (line === "none" || line === "-") return `{gray-fg}${line}{/gray-fg}`;
505+ return line;
506+}
507+
508+function resolvePoiName(
509+ poiId: string | null | undefined,
510+ snapshot: WorldSnapshot,
511+ state: ClientState
512+): string {
513+ if (!poiId) return "Unknown";
514+ const snapshotPoi = snapshot.pois?.find((entry) => entry.id === poiId);
515+ if (snapshotPoi?.name) return snapshotPoi.name;
516+ if (snapshot.poi?.id === poiId && snapshot.poi.name) return snapshot.poi.name;
517+ if (state.poi?.id === poiId && state.poi.name) return state.poi.name;
518+ return poiId;
519+}
520+
521+function formatPoiLines(
522+ pois: WorldSnapshot["pois"],
523+ systemPois: string[] | undefined,
524+ width: number
525+): string[] {
526+ if (pois && pois.length > 0) {
527+ const maxItems = 5;
528+ const lines = pois.slice(0, maxItems).map((poi) => truncate(poi.name || poi.id, width));
529+ if (pois.length > maxItems) {
530+ lines.push(truncate(`+${pois.length - maxItems} more`, width));
531+ }
532+ return lines;
533+ }
534+ if (systemPois && systemPois.length > 0) {
535+ const maxItems = 5;
536+ const lines = systemPois.slice(0, maxItems).map((poiId) => truncate(poiId, width));
537+ if (systemPois.length > maxItems) {
538+ lines.push(truncate(`+${systemPois.length - maxItems} more`, width));
539+ }
540+ return lines;
541+ }
542+ return ["none"];
543+}
544+
545+function formatCargoLines(cargo: CargoItem[] | undefined, width: number): string[] {
546+ if (!cargo || cargo.length === 0) return ["none"];
547+ const maxItems = 6;
548+ const lines = cargo.slice(0, maxItems).map((item) =>
549+ truncate(`${item.item_id} x${item.quantity}`, width)
550+ );
551+ if (cargo.length > maxItems) {
552+ lines.push(truncate(`+${cargo.length - maxItems} more`, width));
553+ }
554+ return lines;
555+}
556+
557+function formatBaseLines(base: WorldSnapshot["base"], width: number): string[] {
558+ if (!base) return ["none"];
559+ const services = Object.keys(base.services)
560+ .filter((key) => (base.services as Record<string, boolean>)[key])
561+ .join(", ");
562+ return [
563+ formatLine("Name", base.name || base.id, width),
564+ formatLine("Services", services || "none", width),
565+ ];
566+}
567+
568+function formatNearbyLines(nearby: ClientState["nearby"], width: number): string[] {
569+ if (!nearby || nearby.length === 0) return ["none"];
570+ const maxItems = 6;
571+ const lines = [formatLine("Count", String(nearby.length), width)];
572+ for (const entry of nearby.slice(0, maxItems)) {
573+ const name = entry.username ?? entry.player_id ?? "unknown";
574+ lines.push(truncate(name, width));
575+ }
576+ if (nearby.length > maxItems) {
577+ lines.push(truncate(`+${nearby.length - maxItems} more`, width));
578+ }
579+ return lines;
580+}