xonecas.io
5875
Os

ollama-space-molt-player / commit a87eb52

feat: add travel state tracking and enhance action validation

xonecas2026-02-01 a87eb524945ada8140c9178c0363478df5fe0b9a parent 68d40c7

2 files changed

src/index.ts+50−1
@@ -35,6 +35,7 @@ let includeHelpInPrompt = true;
3535 let actionLoopRunning = false;
3636 let worldSnapshot: WorldSnapshot = {};
3737 let registrationRetries = 0;
38+let travelInProgress = false;
3839 const MAX_REGISTRATION_RETRIES = 3;
3940 const failedRegistrationNames: string[] = [];
4041
@@ -74,11 +75,16 @@ function updateStatus(): void {
7475 `Hull ${state.ship.hull}/${state.ship.max_hull}`,
7576 `Shield ${state.ship.shield}/${state.ship.max_shield}`,
7677 `Fuel ${state.ship.fuel}/${state.ship.max_fuel}`,
78+ `Traveling ${travelInProgress ? "Y" : "N"}`,
7779 `Docked ${state.player.docked_at_base ? "Y" : "N"}`,
7880 `Tick ${state.currentTick}`,
7981 location,
8082 ].join(" | ");
81- tui.setStatus(status);
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);
8288 }
8389
8490 function saveSnapshot(): void {
@@ -131,6 +137,10 @@ async function startActionLoop(): Promise<void> {
131137
132138 while (client.state.authenticated) {
133139 try {
140+ if (travelInProgress) {
141+ await sleep(config.tickDelayMs);
142+ continue;
143+ }
134144 const recentActions = memory.getRecentActions(config.maxContextActions);
135145 const recentEvents = memory.getRecentEvents(config.maxContextEvents);
136146 const prompt = buildActionPrompt({
@@ -153,6 +163,21 @@ async function startActionLoop(): Promise<void> {
153163 continue;
154164 }
155165
166+ if (
167+ (validation.action.action === "scan" || validation.action.action === "attack") &&
168+ !isNearbyTarget(client.state.nearby, validation.action.args?.target_id)
169+ ) {
170+ const target = String(validation.action.args?.target_id ?? "").trim();
171+ const message = target
172+ ? `Invalid action from LLM: Unknown nearby target: ${target}`
173+ : "Invalid action from LLM: Missing target_id";
174+ log(message);
175+ memory.appendEvent("llm_invalid_action", { error: message, raw: result.raw });
176+ includeHelpInPrompt = true;
177+ await sleep(config.tickDelayMs);
178+ continue;
179+ }
180+
156181 memory.appendAction(
157182 validation.action.action,
158183 validation.action.args ?? {},
@@ -187,6 +212,21 @@ async function startActionLoop(): Promise<void> {
187212 actionLoopRunning = false;
188213 }
189214
215+function isNearbyTarget(
216+ nearby: { player_id?: string; username?: string }[] | undefined,
217+ targetId: unknown
218+): boolean {
219+ if (!nearby || nearby.length === 0) return false;
220+ const target = String(targetId ?? "").trim();
221+ if (!target) return false;
222+ const normalized = target.toLowerCase();
223+ return nearby.some((player) => {
224+ const id = player.player_id ? player.player_id.toLowerCase() : "";
225+ const name = player.username ? player.username.toLowerCase() : "";
226+ return normalized === id || normalized === name;
227+ });
228+}
229+
190230 function sleep(ms: number): Promise<void> {
191231 return new Promise((resolve) => setTimeout(resolve, ms));
192232 }
@@ -327,6 +367,15 @@ client.on("ok", (data: Record<string, unknown>) => {
327367 log(formatOkPayload(data));
328368 memory.appendEvent("ok", data);
329369 updateWorldSnapshotFromOk(data);
370+ const action = typeof data.action === "string" ? data.action : null;
371+ if (action === "travel") {
372+ travelInProgress = true;
373+ }
374+ if (action === "arrived") {
375+ travelInProgress = false;
376+ client.getSystem();
377+ client.getPOI();
378+ }
330379 });
331380
332381 client.on("version_info", (data: Record<string, unknown>) => {
src/prompt.ts+47−5
@@ -102,7 +102,17 @@ REQUIRED ARGS:
102102
103103 NOTES:
104104 - Use only the listed actions.
105+- If an action requires args and you do not know valid values, use {"action":"wait"}.
106+- Never omit required args or leave them blank.
107+- IDs must come from the current state or world snapshot.
105108 - If unsure, use {"action":"wait"}.
109+
110+EXAMPLES (valid JSON only):
111+{"action":"wait"}
112+{"action":"mine"}
113+{"action":"travel","args":{"target_poi":"poi_id_here"}}
114+{"action":"sell","args":{"item_id":"ore_iron","quantity":10}}
115+{"action":"chat","args":{"channel":"local","content":"Hello"}}
106116 `;
107117 }
108118
@@ -142,16 +152,39 @@ function formatState(state: ClientState): string {
142152 lines.push(`Docked: ${player.docked_at_base ? "yes" : "no"} | In combat: ${state.inCombat ? "yes" : "no"}`);
143153 lines.push(`Ship: hull ${ship.hull}/${ship.max_hull} shield ${ship.shield}/${ship.max_shield} fuel ${ship.fuel}/${ship.max_fuel}`);
144154 lines.push(`Cargo: ${ship.cargo_used}/${ship.cargo_capacity}`);
155+ if (ship.cargo?.length) {
156+ lines.push(
157+ `Cargo items: ${ship.cargo.map((item) => `${item.item_id}(${item.quantity})`).join(", ")}`
158+ );
159+ } else {
160+ lines.push("Cargo items: none");
161+ }
145162 if (base) {
146163 lines.push(`Base: ${base.name} services=${Object.keys(base.services).filter((k) => (base.services as Record<string, boolean>)[k]).join(",")}`);
147164 }
165+ if (poi?.resources?.length) {
166+ lines.push(
167+ `Current POI resources: ${poi.resources
168+ .map((r) => `${r.resource_id}(${r.richness},${r.remaining})`)
169+ .join(", ")}`
170+ );
171+ } else {
172+ lines.push("Current POI resources: none");
173+ }
148174 if (nearby.length > 0) {
149175 const list = nearby
150- .map((p) => `${p.username ?? p.player_id ?? "unknown"}${p.in_combat ? "(combat)" : ""}`)
151- .join(", ");
152- lines.push(`Nearby: ${list}`);
176+ .map((p) => {
177+ const name = p.username ?? "unknown";
178+ const id = p.player_id ?? "unknown";
179+ const ship = p.ship_class ? ` ship=${p.ship_class}` : "";
180+ const faction = p.faction_tag ? ` faction=${p.faction_tag}` : "";
181+ const combat = p.in_combat ? " combat" : "";
182+ return `${name} id=${id}${ship}${faction}${combat}`.trim();
183+ })
184+ .join(" | ");
185+ lines.push(`Nearby targets: ${list}`);
153186 } else {
154- lines.push("Nearby: none");
187+ lines.push("Nearby targets: none");
155188 }
156189
157190 return lines.join("\n");
@@ -177,6 +210,15 @@ function formatWorldSnapshot(state: ClientState, snapshot?: WorldSnapshot): stri
177210
178211 if (currentPoi) {
179212 lines.push(`Current POI: ${currentPoi.name} (${currentPoi.id}) type=${currentPoi.type}`);
213+ if (currentPoi.resources?.length) {
214+ lines.push(
215+ `Current POI resources: ${currentPoi.resources
216+ .map((r) => `${r.resource_id}(${r.richness},${r.remaining})`)
217+ .join(", ")}`
218+ );
219+ } else {
220+ lines.push("Current POI resources: none");
221+ }
180222 } else if (state.player?.current_poi) {
181223 lines.push(`Current POI: ${state.player.current_poi} (details unavailable)`);
182224 }
@@ -208,7 +250,7 @@ function formatWorldSnapshot(state: ClientState, snapshot?: WorldSnapshot): stri
208250
209251 if (state.nearby?.length) {
210252 const targets = state.nearby
211- .map((p) => `${p.username ?? "unknown"} (${p.player_id ?? "unknown"})`)
253+ .map((p) => `${p.username ?? "unknown"} id=${p.player_id ?? "unknown"}`)
212254 .join(", ");
213255 lines.push(`Nearby players: ${targets}`);
214256 } else {