5875
Os
ollama-space-molt-player / files
ollama-space-molt-player / packages / client / src / response-summarizer.ts
9.9 KB
1type SummarizerFn = (content: unknown) => unknown; 2 3export function summarizeToolResult(toolName: string, content: unknown, success: boolean): unknown { 4 if (!success) { 5 return content; 6 } 7 8 const summarizer = TOOL_SUMMARIZERS[toolName]; 9 if (summarizer) { 10 try { 11 return summarizer(content); 12 } catch { 13 return content; 14 } 15 } 16 17 return summarizeGeneric(content); 18} 19 20function summarizeGeneric(content: unknown): unknown { 21 if (!content || typeof content !== "object") { 22 return content; 23 } 24 25 if (Array.isArray(content)) { 26 return content.slice(0, 5).map((item) => summarizeGeneric(item)); 27 } 28 29 const obj = content as Record<string, unknown>; 30 const result: Record<string, unknown> = {}; 31 32 for (const [key, value] of Object.entries(obj)) { 33 if (SKIP_FIELDS.has(key)) { 34 continue; 35 } 36 37 if (value && typeof value === "object") { 38 result[key] = summarizeGeneric(value); 39 } else { 40 result[key] = value; 41 } 42 } 43 44 return result; 45} 46 47const SKIP_FIELDS = new Set([ 48 "created_at", 49 "last_login_at", 50 "last_active_at", 51 "joined_at", 52 "last_seen", 53 "timestamp", 54 "discovered_at", 55 56 "primary_color", 57 "secondary_color", 58 59 "status_message", 60 "clan_tag", 61 "description", 62 63 "stats", 64 "skill_xp", 65 "skills", 66 "experience", 67 68 "discovered_systems", 69 70 "position", 71]); 72 73const TOOL_SUMMARIZERS: Record<string, SummarizerFn> = { 74 get_status: (content) => { 75 const c = content as Record<string, unknown>; 76 const player = c.player as Record<string, unknown> | undefined; 77 const ship = c.ship as Record<string, unknown> | undefined; 78 79 return { 80 player: player 81 ? { 82 username: player.username, 83 credits: player.credits, 84 current_system: player.current_system, 85 current_poi: player.current_poi, 86 docked_at_base: player.docked_at_base || null, 87 home_base: player.home_base, 88 } 89 : undefined, 90 ship: ship 91 ? { 92 name: ship.name, 93 class_id: ship.class_id, 94 hull: ship.hull, 95 max_hull: ship.max_hull, 96 fuel: ship.fuel, 97 max_fuel: ship.max_fuel, 98 cargo_used: ship.cargo_used, 99 cargo_capacity: ship.cargo_capacity, 100 modules: ship.modules, 101 } 102 : undefined, 103 }; 104 }, 105 106 register: (content) => { 107 const c = content as Record<string, unknown>; 108 const player = c.player as Record<string, unknown> | undefined; 109 const ship = c.ship as Record<string, unknown> | undefined; 110 const system = c.system as Record<string, unknown> | undefined; 111 const poi = c.poi as Record<string, unknown> | undefined; 112 113 return { 114 message: c.message, 115 player_id: c.player_id, 116 player: player 117 ? { 118 username: player.username, 119 empire: player.empire, 120 credits: player.credits, 121 current_system: player.current_system, 122 current_poi: player.current_poi, 123 docked_at_base: player.docked_at_base, 124 } 125 : undefined, 126 ship: ship 127 ? { 128 name: ship.name, 129 class_id: ship.class_id, 130 hull: ship.hull, 131 max_hull: ship.max_hull, 132 fuel: ship.fuel, 133 max_fuel: ship.max_fuel, 134 } 135 : undefined, 136 system: system 137 ? { 138 id: system.id, 139 name: system.name, 140 connections: system.connections, 141 pois: system.pois, 142 } 143 : undefined, 144 poi: poi 145 ? { 146 id: poi.id, 147 name: poi.name, 148 type: poi.type, 149 base_id: poi.base_id, 150 } 151 : undefined, 152 password: c.password, 153 }; 154 }, 155 156 login: (content) => { 157 const c = content as Record<string, unknown>; 158 const player = c.player as Record<string, unknown> | undefined; 159 const ship = c.ship as Record<string, unknown> | undefined; 160 161 return { 162 message: c.message, 163 player: player 164 ? { 165 username: player.username, 166 credits: player.credits, 167 current_system: player.current_system, 168 current_poi: player.current_poi, 169 docked_at_base: player.docked_at_base, 170 } 171 : undefined, 172 ship: ship 173 ? { 174 name: ship.name, 175 hull: ship.hull, 176 max_hull: ship.max_hull, 177 fuel: ship.fuel, 178 max_fuel: ship.max_fuel, 179 } 180 : undefined, 181 }; 182 }, 183 184 get_nearby: (content) => { 185 const c = content as Record<string, unknown>; 186 const nearby = c.nearby as Array<Record<string, unknown>> | undefined; 187 188 return { 189 count: c.count, 190 nearby: nearby 191 ? nearby.slice(0, 10).map((p) => ({ 192 username: p.username, 193 player_id: p.player_id, 194 ship_class: p.ship_class, 195 in_combat: p.in_combat, 196 })) 197 : [], 198 }; 199 }, 200 201 get_notifications: (content) => { 202 const c = content as Record<string, unknown>; 203 const notifications = c.notifications as Array<Record<string, unknown>> | undefined; 204 205 return { 206 count: c.count, 207 remaining: c.remaining, 208 notifications: notifications 209 ? notifications.slice(0, 10).map((n) => ({ 210 type: n.type, 211 message: n.message, 212 data: n.data, 213 })) 214 : [], 215 }; 216 }, 217 218 travel: (content) => { 219 const c = content as Record<string, unknown>; 220 return { 221 action: c.action || "travel", 222 ticks: c.ticks, 223 destination: c.destination || c.target_poi, 224 message: c.message, 225 }; 226 }, 227 228 jump: (content) => { 229 const c = content as Record<string, unknown>; 230 return { 231 action: c.action || "jump", 232 ticks: c.ticks, 233 destination: c.destination || c.target_system, 234 message: c.message, 235 }; 236 }, 237 238 dock: (content) => { 239 const c = content as Record<string, unknown>; 240 return { 241 action: c.action || "dock", 242 base_id: c.base_id, 243 base_name: c.base_name, 244 message: c.message, 245 }; 246 }, 247 248 undock: (content) => { 249 const c = content as Record<string, unknown>; 250 return { 251 action: c.action || "undock", 252 message: c.message, 253 }; 254 }, 255 256 attack: (content) => { 257 const c = content as Record<string, unknown>; 258 return { 259 action: c.action || "attack", 260 target: c.target, 261 damage: c.damage, 262 result: c.result, 263 message: c.message, 264 }; 265 }, 266 267 mine: (content) => { 268 const c = content as Record<string, unknown>; 269 return { 270 action: c.action || "mine", 271 ore_type: c.ore_type, 272 quantity: c.quantity, 273 message: c.message, 274 }; 275 }, 276 277 get_system: (content) => { 278 const c = content as Record<string, unknown>; 279 return { 280 id: c.id, 281 name: c.name, 282 empire: c.empire, 283 connections: c.connections, 284 pois: c.pois, 285 police_level: c.police_level, 286 }; 287 }, 288 289 get_poi: (content) => { 290 const c = content as Record<string, unknown>; 291 return { 292 id: c.id, 293 name: c.name, 294 type: c.type, 295 system_id: c.system_id, 296 base_id: c.base_id || null, 297 }; 298 }, 299 300 faction_info: (content) => { 301 const c = content as Record<string, unknown>; 302 const members = c.members as Array<Record<string, unknown>> | undefined; 303 304 return { 305 id: c.id, 306 name: c.name, 307 tag: c.tag, 308 leader_username: c.leader_username, 309 member_count: c.member_count, 310 members: members 311 ? members.slice(0, 5).map((m) => ({ 312 username: m.username, 313 role: m.role, 314 })) 315 : undefined, 316 is_member: c.is_member, 317 at_war: c.at_war, 318 }; 319 }, 320 321 create_faction: (content) => { 322 const c = content as Record<string, unknown>; 323 return { 324 action: c.action || "create_faction", 325 faction_id: c.faction_id, 326 name: c.name, 327 }; 328 }, 329 330 chat: (content) => { 331 const c = content as Record<string, unknown>; 332 const notification = c.notification as Record<string, unknown> | undefined; 333 const message = notification?.Message as Record<string, unknown> | undefined; 334 335 return { 336 message: c.message, 337 sent_at: c.sent_at, 338 channel: notification?.Channel, 339 content: message?.content, 340 }; 341 }, 342 343 forum_list: (content) => { 344 const c = content as Record<string, unknown>; 345 const threads = c.threads as Array<Record<string, unknown>> | undefined; 346 347 return { 348 count: c.count, 349 threads: threads 350 ? threads.slice(0, 5).map((t) => ({ 351 id: t.id, 352 title: t.title, 353 author: t.author, 354 replies: t.reply_count || t.replies, 355 })) 356 : [], 357 }; 358 }, 359 360 forum_get_thread: (content) => { 361 const c = content as Record<string, unknown>; 362 const replies = c.replies as Array<Record<string, unknown>> | undefined; 363 364 return { 365 id: c.id, 366 title: c.title, 367 author: c.author, 368 content: c.content, 369 reply_count: replies?.length || c.reply_count, 370 replies: replies 371 ? replies.slice(0, 5).map((r) => ({ 372 author: r.author, 373 content: typeof r.content === "string" ? r.content.slice(0, 200) : r.content, 374 })) 375 : [], 376 }; 377 }, 378 379 forum_create_thread: (content) => { 380 const c = content as Record<string, unknown>; 381 return { 382 message: c.message, 383 thread_id: c.thread_id, 384 title: c.title, 385 }; 386 }, 387 388 forum_reply: (content) => { 389 const c = content as Record<string, unknown>; 390 return { 391 message: c.message, 392 reply_id: c.reply_id, 393 }; 394 }, 395 396 get_market: (content) => { 397 const c = content as Record<string, unknown>; 398 const listings = c.listings as Array<Record<string, unknown>> | undefined; 399 400 return { 401 count: c.count, 402 listings: listings 403 ? listings.slice(0, 10).map((l) => ({ 404 id: l.id, 405 item_id: l.item_id, 406 item_name: l.item_name, 407 quantity: l.quantity, 408 price: l.price, 409 seller: l.seller, 410 })) 411 : [], 412 }; 413 }, 414 415 get_ship: (content) => { 416 const c = content as Record<string, unknown>; 417 const modules = c.modules as Array<Record<string, unknown>> | undefined; 418 const cargo = c.cargo as Array<Record<string, unknown>> | undefined; 419 420 return { 421 id: c.id, 422 name: c.name, 423 class_id: c.class_id, 424 hull: c.hull, 425 max_hull: c.max_hull, 426 shield: c.shield, 427 max_shield: c.max_shield, 428 fuel: c.fuel, 429 max_fuel: c.max_fuel, 430 cargo_used: c.cargo_used, 431 cargo_capacity: c.cargo_capacity, 432 modules: modules 433 ? modules.map((m) => ({ 434 id: m.id, 435 type: m.type, 436 name: m.name, 437 })) 438 : c.modules, 439 cargo: cargo 440 ? cargo.map((item) => ({ 441 id: item.id, 442 name: item.name, 443 quantity: item.quantity, 444 })) 445 : [], 446 }; 447 }, 448 449 captains_log_add: (content) => { 450 const c = content as Record<string, unknown>; 451 return { 452 message: c.message, 453 entry_id: c.entry_id, 454 }; 455 }, 456 457 captains_log_get: (content) => { 458 const c = content as Record<string, unknown>; 459 return { 460 entry: c.entry, 461 index: c.index, 462 }; 463 }, 464 465 help: (content) => content, 466};