xonecas.io
5875
Os

ollama-space-molt-player / commit 58a3110

feat: persist truncated thinking messages to database

- Add thinking column to actions table (TEXT, nullable)
- Move truncateThinking() to shared utils (200 char, sentence-aware)
- Update memory store to accept thinking parameter in appendAction methods
- Store truncated thinking (200 chars) when saving actions
- Update prompt generation script to read thinking from database
- Thinking now appears in LAST DECISION REASONING section of prompts

xonecas2026-02-02 58a311033d37798c5c42d1772a5e02aba78781ac parent 94477d4

5 files changed

scripts/generate-prompt-from-db.ts+7−6
@@ -39,6 +39,7 @@ interface StoredAction {
3939 action: string;
4040 args: string | null;
4141 model_raw: string | null;
42+ thinking: string | null;
4243 }
4344
4445 interface AnalysisResult {
@@ -434,13 +435,13 @@ async function buildPromptContext(
434435 // Detect repetition
435436 const repetitionWarning = detectRepetition(db, tick);
436437
437- // Extract thinking from previous action
438+ // Get thinking from previous action
438439 const prevAction = db
439440 .query(
440- "SELECT model_raw FROM actions WHERE tick < ? ORDER BY tick DESC LIMIT 1",
441+ "SELECT thinking FROM actions WHERE tick < ? ORDER BY tick DESC LIMIT 1",
441442 )
442- .get(tick) as { model_raw: string | null } | undefined;
443- const lastThinking = extractThinking(prevAction?.model_raw || null);
443+ .get(tick) as { thinking: string | null } | undefined;
444+ const lastThinking = prevAction?.thinking || null;
444445
445446 // Build world snapshot
446447 const worldSnapshot: WorldSnapshot = {
@@ -1046,7 +1047,7 @@ async function main() {
10461047 if (args.actionId !== null) {
10471048 const action = db
10481049 .query(
1049- "SELECT id, ts, tick, action, args, model_raw FROM actions WHERE id = ?",
1050+ "SELECT id, ts, tick, action, args, model_raw, thinking FROM actions WHERE id = ?",
10501051 )
10511052 .get(args.actionId) as StoredAction | undefined;
10521053
@@ -1060,7 +1061,7 @@ async function main() {
10601061 } else {
10611062 actionsToAnalyze = db
10621063 .query(
1063- "SELECT id, ts, tick, action, args, model_raw FROM actions ORDER BY id DESC LIMIT ?",
1064+ "SELECT id, ts, tick, action, args, model_raw, thinking FROM actions ORDER BY id DESC LIMIT ?",
10641065 )
10651066 .all(args.count) as StoredAction[];
10661067
src/index.ts+7−1
@@ -45,7 +45,12 @@ import type {
4545 RegistrationOverrides,
4646 SpeechStyleType,
4747 } from "./types";
48-import { detectRepetition, isNearbyTarget, sleep } from "./utils";
48+import {
49+ detectRepetition,
50+ isNearbyTarget,
51+ sleep,
52+ truncateThinking,
53+} from "./utils";
4954
5055 const memory = new MemoryStore(config.memoryPath);
5156 const ollama = new OllamaAgent(
@@ -423,6 +428,7 @@ async function startActionLoop(): Promise<void> {
423428 validation.action.args ?? {},
424429 promptExcerpt,
425430 result.raw,
431+ result.thinking ? truncateThinking(result.thinking, 200) : null,
426432 );
427433 gameState.pendingActionId = actionId;
428434 const pendingDelayMs = Math.max(3000, config.tickDelayMs * 2);
src/memory.ts+9−3
@@ -24,6 +24,7 @@ export interface StoredAction {
2424 args: string | null;
2525 prompt_excerpt: string | null;
2626 model_raw: string | null;
27+ thinking: string | null;
2728 }
2829
2930 export interface StoredEvent {
@@ -78,7 +79,8 @@ export class MemoryStore {
7879 action TEXT NOT NULL,
7980 args TEXT,
8081 prompt_excerpt TEXT,
81- model_raw TEXT
82+ model_raw TEXT,
83+ thinking TEXT
8284 );
8385 CREATE TABLE IF NOT EXISTS action_results (
8486 id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -254,10 +256,11 @@ export class MemoryStore {
254256 args: unknown,
255257 promptExcerpt: string,
256258 modelRaw: string,
259+ thinking: string | null,
257260 ): void {
258261 this.db
259262 .query(
260- "INSERT INTO actions (ts, tick, action, args, prompt_excerpt, model_raw) VALUES (?, ?, ?, ?, ?, ?)",
263+ "INSERT INTO actions (ts, tick, action, args, prompt_excerpt, model_raw, thinking) VALUES (?, ?, ?, ?, ?, ?, ?)",
261264 )
262265 .run(
263266 nowIso(),
@@ -266,6 +269,7 @@ export class MemoryStore {
266269 toJson(args),
267270 truncate(promptExcerpt),
268271 truncate(modelRaw),
272+ thinking,
269273 );
270274 }
271275
@@ -275,10 +279,11 @@ export class MemoryStore {
275279 args: unknown,
276280 promptExcerpt: string,
277281 modelRaw: string,
282+ thinking: string | null,
278283 ): number {
279284 const result = this.db
280285 .query(
281- "INSERT INTO actions (ts, tick, action, args, prompt_excerpt, model_raw) VALUES (?, ?, ?, ?, ?, ?)",
286+ "INSERT INTO actions (ts, tick, action, args, prompt_excerpt, model_raw, thinking) VALUES (?, ?, ?, ?, ?, ?, ?)",
282287 )
283288 .run(
284289 nowIso(),
@@ -287,6 +292,7 @@ export class MemoryStore {
287292 toJson(args),
288293 truncate(promptExcerpt),
289294 truncate(modelRaw),
295+ thinking,
290296 );
291297 return Number(result.lastInsertRowid);
292298 }
src/tui/panels/context.ts+1−41
@@ -1,50 +1,10 @@
11 // Context panel - warnings, memory summary, and forum status
22
3+import { truncateThinking } from "../../utils";
34 import type { TuiContext } from "../index";
45 import { PALETTE, THEME } from "../theme";
56 import { applyColor, renderSectionTitle } from "../widgets";
67
7-function truncateThinking(text: string, maxChars = 200): string {
8- const trimmed = text.trim();
9-
10- // If short enough, return as-is
11- if (trimmed.length <= maxChars) {
12- return trimmed;
13- }
14-
15- // Try sentence-based approach
16- const sentences = trimmed.match(/[^.!?]+[.!?]+/g);
17-
18- if (sentences && sentences.length > 0) {
19- // Collect sentences from end backwards
20- const collected: string[] = [];
21- let totalLength = 0;
22-
23- for (let i = sentences.length - 1; i >= 0; i--) {
24- const sentence = sentences[i].trim();
25- const newLength =
26- totalLength + sentence.length + (collected.length > 0 ? 1 : 0); // +1 for space
27-
28- if (newLength > maxChars) {
29- break;
30- }
31-
32- collected.unshift(sentence);
33- totalLength = newLength;
34- }
35-
36- if (collected.length > 0) {
37- const result = collected.join(" ");
38- const isPartial = collected.length < sentences.length;
39- return isPartial ? `[...] ${result}` : result;
40- }
41- }
42-
43- // Fallback: character truncation
44- const excerpt = trimmed.slice(-maxChars).trim();
45- return `[...] ${excerpt}`;
46-}
47-
488 export function renderContextPanel(context: TuiContext | undefined): string {
499 const lines: string[] = [];
5010
src/utils.ts+41−0
@@ -64,3 +64,44 @@ export function detectRepetition(
6464 }
6565 return null;
6666 }
67+
68+export function truncateThinking(text: string, maxChars = 200): string {
69+ const trimmed = text.trim();
70+
71+ // If short enough, return as-is
72+ if (trimmed.length <= maxChars) {
73+ return trimmed;
74+ }
75+
76+ // Try sentence-based approach
77+ const sentences = trimmed.match(/[^.!?]+[.!?]+/g);
78+
79+ if (sentences && sentences.length > 0) {
80+ // Collect sentences from end backwards
81+ const collected: string[] = [];
82+ let totalLength = 0;
83+
84+ for (let i = sentences.length - 1; i >= 0; i--) {
85+ const sentence = sentences[i].trim();
86+ const newLength =
87+ totalLength + sentence.length + (collected.length > 0 ? 1 : 0); // +1 for space
88+
89+ if (newLength > maxChars) {
90+ break;
91+ }
92+
93+ collected.unshift(sentence);
94+ totalLength = newLength;
95+ }
96+
97+ if (collected.length > 0) {
98+ const result = collected.join(" ");
99+ const isPartial = collected.length < sentences.length;
100+ return isPartial ? `[...] ${result}` : result;
101+ }
102+ }
103+
104+ // Fallback: character truncation
105+ const excerpt = trimmed.slice(-maxChars).trim();
106+ return `[...] ${excerpt}`;
107+}