@@ -206,9 +206,11 @@ export function buildActionPrompt(context: PromptContext): string {
206206 const lastActionText = context.lastActionResult?.trim()
207207 ? context.lastActionResult.trim()
208208 : "No actions yet.";
209- const forumContextBlock = formatForumContext(
210- context.recentHistory,
209+ const playerName = context.state.player?.username ?? "unknown";
210+ const socialBlock = buildSocialSection(
211+ context.memory,
211212 context.currentTick,
213+ playerName,
212214 );
213215 const helpBlock = `\nGAME INFORMATION:\n${HELP_TEXT}`;
214216 const missionText = context.currentMission?.trim()
@@ -300,7 +302,7 @@ ${memoryText}
300302 LAST ACTION RESULT:
301303 ${lastActionText}
302304
303-${forumContextBlock}
305+${socialBlock}
304306
305307 ACTION SCHEMA (JSON ONLY):
306308 {"mission":"...","action":"travel|jump|dock|undock|mine|attack|scan|buy|sell|refuel|repair|craft|chat|say|faction|msg|create_faction|set_status|set_colors|set_anonymous|status|system|poi|base|skills|recipes|version|nearby|cargo|forum|forum_thread|forum_post|forum_reply|forum_upvote|help|wait","args":{...}}
@@ -616,6 +618,18 @@ function formatWorldSnapshot(
616618 return lines.join("\n");
617619 }
618620
621+// Internal/technical events that should not appear in game memory summaries
622+const INTERNAL_EVENT_TYPES = new Set([
623+ "llm_timeout", // Ollama request timeout
624+ "loop_error", // Action loop error
625+ "llm_invalid_action", // LLM returned invalid action format
626+ "action_sent", // Action confirmation (already filtered separately)
627+ "welcome", // Connection handshake
628+ "registered", // Registration confirmation
629+ "logged_in", // Login confirmation
630+ "version_info", // Server version info
631+]);
632+
619633 export function buildSummaryPrompt(
620634 history: HistoryEntry[],
621635 currentTick: number,
@@ -668,6 +682,11 @@ function formatHistoryForSummary(
668682 continue;
669683 }
670684
685+ // Skip internal/technical events (not game-related)
686+ if (INTERNAL_EVENT_TYPES.has(entry.type)) {
687+ continue;
688+ }
689+
671690 const summary = summarizeEvent(entry);
672691 const tickTime = formatTickTime(entry.tick, currentTick);
673692 if (summary) {
@@ -839,6 +858,7 @@ export function buildLastActionResult(
839858 appended = true;
840859 continue;
841860 }
861+ // Fallback for unhandled result types
842862 lines.push(`- ${result.result_type} received`);
843863 appended = true;
844864 }
@@ -850,35 +870,254 @@ export function buildLastActionResult(
850870 return lines.join("\n");
851871 }
852872
853-function formatForumContext(
854- history: HistoryEntry[],
873+function formatRecentChat(
874+ memory: MemoryStore,
875+ _currentTick: number,
876+ playerName: string,
877+): string[] {
878+ // Get last 5 chat_message events
879+ const allHistory = memory.getRecentHistory(100, 100);
880+ const chatEvents = allHistory
881+ .filter((entry) => entry.kind === "event" && entry.type === "chat_message")
882+ .slice(-5); // Take last 5 (most recent)
883+
884+ const lines: string[] = [];
885+
886+ for (const event of chatEvents) {
887+ const payload = parseJson(event.payload ?? "");
888+ if (!payload || typeof payload !== "object") continue;
889+
890+ const tick = event.tick;
891+ const channel = getStringField(payload, "channel") ?? "unknown";
892+ const sender = getStringField(payload, "sender") ?? "unknown";
893+ const senderId = getStringField(payload, "sender_id") ?? "unknown";
894+ const content = getStringField(payload, "content") ?? "";
895+
896+ const isFromPlayer = sender === playerName;
897+
898+ let formatted = `T${tick} [${channel}] `;
899+
900+ if (channel === "private") {
901+ if (isFromPlayer) {
902+ // Player sent this message - need to find recipient
903+ // For private messages sent by player, we don't have recipient in the payload
904+ // so we'll just show "you -> [private]"
905+ formatted += "you -> [private]";
906+ } else {
907+ // Player received this message
908+ formatted += `${sender} (id=${senderId}) -> you`;
909+ }
910+ } else {
911+ if (isFromPlayer) {
912+ formatted += "you -> all";
913+ } else {
914+ formatted += `${sender} (id=${senderId})`;
915+ }
916+ }
917+
918+ formatted += `: "${truncateText(content, 80)}"`;
919+ lines.push(formatted);
920+ }
921+
922+ return lines;
923+}
924+
925+function formatCreatedThreads(
926+ memory: MemoryStore,
927+ _currentTick: number,
928+): string[] {
929+ const allHistory = memory.getRecentHistory(200, 0);
930+ const forumPosts = allHistory
931+ .filter((entry) => entry.kind === "action" && entry.action === "forum_post")
932+ .slice(-3); // Take last 3 (most recent)
933+
934+ const lines: string[] = [];
935+
936+ for (const post of forumPosts) {
937+ const args = parseActionArgs(post.args);
938+ const category =
939+ typeof args?.category === "string" ? args.category : "unknown";
940+ const title = typeof args?.title === "string" ? args.title : "(untitled)";
941+
942+ // Try to find thread_id from results
943+ const results = memory.getResultsForAction(post.tick, post.action);
944+ let threadId = "unknown";
945+
946+ for (const result of results) {
947+ const payload = parseJson(result.payload ?? "");
948+ if (payload && typeof payload === "object") {
949+ const id =
950+ getStringField(payload, "thread_id") ?? getStringField(payload, "id");
951+ if (id) {
952+ threadId = id;
953+ break;
954+ }
955+ }
956+ }
957+
958+ lines.push(
959+ `[${category}] "${truncateText(title, 50)}" (thread_id=${threadId}, T${post.tick})`,
960+ );
961+ }
962+
963+ return lines;
964+}
965+
966+function formatParticipatedThreads(
967+ memory: MemoryStore,
968+ _currentTick: number,
969+): string[] {
970+ const allHistory = memory.getRecentHistory(200, 0);
971+ const replies = allHistory.filter(
972+ (entry) => entry.kind === "action" && entry.action === "forum_reply",
973+ );
974+
975+ // Deduplicate by thread_id, keep most recent per thread
976+ const threadMap = new Map<string, HistoryEntry>();
977+ for (const reply of replies) {
978+ const args = parseActionArgs(reply.args);
979+ const threadId = typeof args?.thread_id === "string" ? args.thread_id : "";
980+ if (!threadId) continue;
981+
982+ if (!threadMap.has(threadId)) {
983+ threadMap.set(threadId, reply);
984+ }
985+ }
986+
987+ const uniqueReplies = Array.from(threadMap.values()).slice(-3); // Take last 3 (most recent)
988+ const lines: string[] = [];
989+
990+ for (const reply of uniqueReplies) {
991+ const args = parseActionArgs(reply.args);
992+ const threadId =
993+ typeof args?.thread_id === "string" ? args.thread_id : "unknown";
994+
995+ // Count how many times player replied to this thread
996+ const replyCount = replies.filter((r) => {
997+ const a = parseActionArgs(r.args);
998+ return a?.thread_id === threadId;
999+ }).length;
1000+
1001+ lines.push(
1002+ `thread_id=${threadId} (T${reply.tick}, ${replyCount} ${replyCount === 1 ? "reply" : "replies"})`,
1003+ );
1004+ }
1005+
1006+ return lines;
1007+}
1008+
1009+function formatRecentForumBrowse(
1010+ memory: MemoryStore,
1011+ currentTick: number,
1012+): { header: string; threads: string[] } | null {
1013+ // Find most recent forum action
1014+ const allHistory = memory.getRecentHistory(100, 0);
1015+ const forumAction = allHistory.find(
1016+ (entry) => entry.kind === "action" && entry.action === "forum",
1017+ );
1018+
1019+ if (!forumAction) return null;
1020+
1021+ const tickDiff = currentTick - forumAction.tick;
1022+ if (tickDiff > 10) return null; // Too old
1023+
1024+ // Get forum_list results from that action
1025+ const results = memory.getResultsForAction(forumAction.tick, "forum");
1026+ const threads: string[] = [];
1027+
1028+ for (const result of results) {
1029+ if (result.result_type !== "forum_list") continue;
1030+
1031+ const payload = parseJson(result.payload ?? "");
1032+ if (!payload || typeof payload !== "object") continue;
1033+
1034+ const threadList = findArrayField(payload as Record<string, unknown>, [
1035+ "threads",
1036+ "items",
1037+ "results",
1038+ "posts",
1039+ ]);
1040+
1041+ if (!threadList || threadList.length === 0) continue;
1042+
1043+ for (const item of threadList.slice(0, 5)) {
1044+ if (!item || typeof item !== "object") continue;
1045+ const record = item as Record<string, unknown>;
1046+
1047+ const id =
1048+ getStringField(record, "id") ??
1049+ getStringField(record, "thread_id") ??
1050+ "unknown";
1051+ const title =
1052+ getStringField(record, "title") ??
1053+ getStringField(record, "subject") ??
1054+ "(untitled)";
1055+ const category = getStringField(record, "category") ?? "general";
1056+ const author =
1057+ getStringField(record, "author") ??
1058+ getStringField(record, "username") ??
1059+ "unknown";
1060+ const replies =
1061+ getStringField(record, "reply_count") ??
1062+ getStringField(record, "replies") ??
1063+ "0";
1064+
1065+ threads.push(
1066+ `[${category}] "${truncateText(title, 50)}" (thread_id=${id}, by=${author}, replies=${replies})`,
1067+ );
1068+ }
1069+
1070+ break; // Only process first forum_list result
1071+ }
1072+
1073+ const header = `Recent Forum Browse (T${forumAction.tick}, ${tickDiff} ${tickDiff === 1 ? "tick" : "ticks"} ago):`;
1074+
1075+ return threads.length > 0 ? { header, threads } : null;
1076+}
1077+
1078+function buildSocialSection(
1079+ memory: MemoryStore,
8551080 currentTick: number,
1081+ playerName: string,
8561082 ): string {
857- if (history.length === 0) return "";
1083+ const lines: string[] = ["SOCIAL:"];
1084+
1085+ // Chat messages
1086+ const chatLines = formatRecentChat(memory, currentTick, playerName);
1087+ lines.push("", "Recent Chat (last 5 messages):");
1088+ if (chatLines.length === 0) {
1089+ lines.push(" (no recent messages)");
1090+ } else {
1091+ lines.push(...chatLines.map((line) => `- ${line}`));
1092+ }
8581093
859- const lastForumCheck = findLastForumCheck(history, currentTick);
860- const createdPosts = findRecentForumPosts(history, 3);
861- const repliedThreads = findRecentRepliedThreads(history, 3);
1094+ // Forum activity
1095+ lines.push("", "Forum Activity:");
8621096
863- const lines: string[] = ["FORUM INFORMATION:"];
864- lines.push(`- Last forum check: ${formatForumCheckLine(lastForumCheck)}`);
865- lines.push("- Created posts (last 3):");
866- if (createdPosts.length === 0) {
1097+ // Created threads
1098+ const createdThreads = formatCreatedThreads(memory, currentTick);
1099+ lines.push("Created Threads:");
1100+ if (createdThreads.length === 0) {
8671101 lines.push(" (none)");
8681102 } else {
869- for (const post of createdPosts) {
870- const categoryText = post.category ? `[${post.category}] ` : "";
871- lines.push(
872- ` - ${categoryText}${post.title ?? "(untitled)"} (T${post.tick})`,
873- );
874- }
1103+ lines.push(...createdThreads.map((line) => ` - ${line}`));
8751104 }
876- lines.push("- Participated threads (last 3):");
877- if (repliedThreads.length === 0) {
1105+
1106+ // Participated threads
1107+ const participatedThreads = formatParticipatedThreads(memory, currentTick);
1108+ lines.push("Participated Threads:");
1109+ if (participatedThreads.length === 0) {
8781110 lines.push(" (none)");
8791111 } else {
880- for (const thread of repliedThreads) {
881- lines.push(` - ${thread.threadId} (T${thread.tick})`);
1112+ lines.push(...participatedThreads.map((line) => ` - ${line}`));
1113+ }
1114+
1115+ // Recent forum browse (only if within 10 ticks)
1116+ const browseLine = formatRecentForumBrowse(memory, currentTick);
1117+ if (browseLine) {
1118+ lines.push("", browseLine.header);
1119+ if (browseLine.threads.length > 0) {
1120+ lines.push(...browseLine.threads.map((line) => ` - ${line}`));
8821121 }
8831122 }
8841123
@@ -1077,68 +1316,6 @@ function getObjectField(
10771316 return null;
10781317 }
10791318
1080-type ForumCheckInfo = {
1081- tick: number;
1082- relative: string;
1083-};
1084-
1085-type ForumPostInfo = {
1086- tick: number;
1087- category: string | null;
1088- title: string | null;
1089-};
1090-
1091-type ForumThreadInfo = {
1092- tick: number;
1093- threadId: string;
1094-};
1095-
1096-function findLastForumCheck(
1097- history: HistoryEntry[],
1098- currentTick: number,
1099-): ForumCheckInfo | null {
1100- for (const entry of history) {
1101- if (entry.kind !== "action" || entry.action !== "forum") continue;
1102- const relative = formatTickTime(entry.tick, currentTick);
1103- return { tick: entry.tick, relative };
1104- }
1105- return null;
1106-}
1107-
1108-function findRecentForumPosts(
1109- history: HistoryEntry[],
1110- limit: number,
1111-): ForumPostInfo[] {
1112- const posts: ForumPostInfo[] = [];
1113- for (const entry of history) {
1114- if (entry.kind !== "action" || entry.action !== "forum_post") continue;
1115- const args = parseActionArgs(entry.args);
1116- const category = typeof args?.category === "string" ? args.category : null;
1117- const title = typeof args?.title === "string" ? args.title : null;
1118- posts.push({ tick: entry.tick, category, title });
1119- if (posts.length >= limit) break;
1120- }
1121- return posts;
1122-}
1123-
1124-function findRecentRepliedThreads(
1125- history: HistoryEntry[],
1126- limit: number,
1127-): ForumThreadInfo[] {
1128- const threads: ForumThreadInfo[] = [];
1129- const seen = new Set<string>();
1130- for (const entry of history) {
1131- if (entry.kind !== "action" || entry.action !== "forum_reply") continue;
1132- const args = parseActionArgs(entry.args);
1133- const threadId = typeof args?.thread_id === "string" ? args.thread_id : "";
1134- if (!threadId || seen.has(threadId)) continue;
1135- seen.add(threadId);
1136- threads.push({ tick: entry.tick, threadId });
1137- if (threads.length >= limit) break;
1138- }
1139- return threads;
1140-}
1141-
11421319 function parseActionArgs(value: string | null): Record<string, unknown> | null {
11431320 if (!value) return null;
11441321 try {
@@ -1152,11 +1329,6 @@ function parseActionArgs(value: string | null): Record<string, unknown> | null {
11521329 return null;
11531330 }
11541331
1155-function formatForumCheckLine(info: ForumCheckInfo | null): string {
1156- if (!info) return "unknown";
1157- return info.relative;
1158-}
1159-
11601332 function formatTickTime(tick: number, currentTick: number): string {
11611333 if (tick === 0) return "T0 (before game start)";
11621334 const diff = currentTick - tick;