Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 | import { smallestPrimeFactor, factorize } from '../primes/sieve' import { MATH_CONSTANTS } from '../constants/constantsData' import type { GeneratedScenario } from './generateScenario' import type { ChildProfile } from './childProfile' import type { SharedHistory } from '@/lib/number-line/shared-history' // --- Sequence checks --- function isFibonacci(n: number): boolean { if (n < 0 || !Number.isInteger(n)) return false // n is Fibonacci iff 5n²+4 or 5n²-4 is a perfect square const a = 5 * n * n + 4 const b = 5 * n * n - 4 const sqrtA = Math.round(Math.sqrt(a)) const sqrtB = Math.round(Math.sqrt(b)) return sqrtA * sqrtA === a || sqrtB * sqrtB === b } function isPerfectSquare(n: number): boolean { if (n < 0 || !Number.isInteger(n)) return false const s = Math.round(Math.sqrt(n)) return s * s === n } function isPerfectCube(n: number): boolean { if (!Number.isInteger(n)) return false const c = Math.round(Math.cbrt(n)) return c * c * c === n } function isTriangular(n: number): boolean { if (n < 0 || !Number.isInteger(n)) return false // n is triangular iff 8n+1 is a perfect square return isPerfectSquare(8 * n + 1) } function isPowerOf2(n: number): boolean { if (n < 1 || !Number.isInteger(n)) return false return (n & (n - 1)) === 0 } function isFactorial(n: number): number | null { if (n < 1 || !Number.isInteger(n)) return null let f = 1 for (let k = 1; k <= 20; k++) { f *= k if (f === n) return k } return null } // --- Famous number proximity --- function findNearbyConstants(n: number): string[] { const nearby: string[] = [] for (const c of MATH_CONSTANTS) { const dist = Math.abs(n - c.value) if (dist < 0.5 && dist > 0.001) { nearby.push(`${c.symbol} (${c.name}, ≈${c.value.toFixed(4)})`) } } return nearby } // --- Cultural associations --- function getCulturalNote(n: number): string | null { const notes: Record<number, string> = { 0: "the placeholder that changed everything — without zero we couldn't write 10 or 100", 1: 'unity, the beginning, the loneliest number', 2: 'duality, pairs, the only even prime', 3: 'three wishes in stories, three little pigs, the smallest odd prime', 4: 'four seasons, four directions, a square number', 5: 'five senses, five fingers, the halfway point of a hand', 7: 'lucky seven, the most popular "random" number people pick', 10: 'our counting base, ten fingers, a perfect score', 12: 'a dozen, months in a year, hours on a clock face', 13: "unlucky thirteen, a baker's dozen, a rebel number", 21: 'blackjack, the age of adulthood in many places', 42: 'the answer to life, the universe, and everything', 50: 'half a century, a golden anniversary', 64: "squares on a chessboard, a computer's favorite power of 2", 100: 'a century, a perfect percentage, a big round milestone', 144: 'a gross (12 dozen), a Fibonacci number AND a perfect square', 365: 'days in a year (approximately)', 420: 'a number with a lot of divisors', 1000: 'a grand, a kilo, the gateway to big numbers', 1729: 'the Hardy-Ramanujan number — the smallest expressible as the sum of two cubes in two different ways', } return notes[n] ?? null } // --- Activity generator --- function generateActivity(n: number, traits: string[]): string { if (n === 0) return 'thinking about what it means to be the starting point of all counting' if (n === 1) return 'counting things — everything starts with you, after all' if (n < 0) return `comparing yourself to ${-n} on the other side of zero` if (!Number.isInteger(n)) { const lower = Math.floor(n) const upper = Math.ceil(n) return `figuring out exactly how far you are from ${lower} and ${upper}` } const abs = Math.abs(n) if (traits.includes('prime')) { const activities = [ 'trying to divide yourself into equal groups and failing (as usual)', `checking if any number up to ${Math.round(Math.sqrt(abs))} divides you evenly`, 'counting the other primes in your neighborhood', 'wondering which prime comes after you', ] return activities[abs % activities.length] } if (isPerfectSquare(abs)) { const root = Math.round(Math.sqrt(abs)) return `arranging ${abs} dots into a perfect ${root} by ${root} grid` } if (isPowerOf2(abs)) { return `seeing how many times you can halve yourself: ${abs}, ${abs / 2}, ${abs / 4}...` } if (n === 12) return 'figuring out all the ways to split yourself into equal groups (there are a lot)' if (n === 7) return 'noticing you show up everywhere — days of the week, colors in a rainbow' if (n === 13) return "counting that you're the 6th prime number" if (n === 42) return 'adding up the first few even numbers to see if any combination makes you' const factorialK = isFactorial(abs) if (factorialK !== null) { return `counting all the ways to arrange ${factorialK} things in a line` } if (isFibonacci(abs)) { return 'checking which two earlier numbers in the Fibonacci sequence add up to you' } // Generic composite activities const factors = factorize(abs) if (factors.length > 0) { const f = factors[0] return `arranging ${abs} dots into ${abs / f.prime} rows of ${f.prime}` } return 'looking up and down the number line, seeing who your neighbors are' } // --- Compact trait summary (for conference prompts & scenario generation) --- export function getTraitSummary(n: number): string { const parts: string[] = [] const abs = Math.abs(n) const isInt = Number.isInteger(n) if (n === 0) { parts.push('thoughtful and existential, the origin of everything') } else if (n < 0) { parts.push(`reflective, cold, mirror-image of ${-n}`) } if (isInt && abs >= 2) { const spf = smallestPrimeFactor(abs) if (spf === abs) { parts.push('proudly prime and indivisible') } else { const factors = factorize(abs) parts.push( `composite (${factors.map((f) => (f.exponent > 1 ? `${f.prime}^${f.exponent}` : `${f.prime}`)).join(' × ')})` ) } } if (isInt && abs >= 0) { if (isFibonacci(abs) && abs > 1) parts.push('Fibonacci number') if (isPerfectSquare(abs) && abs > 1) parts.push(`perfect square (${Math.round(Math.sqrt(abs))}²)`) if (isPowerOf2(abs) && abs > 2) parts.push('power of 2') } if (!isInt) parts.push('a decimal caught between integers') const cultural = getCulturalNote(isInt ? n : Math.round(n)) if (cultural) parts.push(cultural) return parts.join('; ') || 'a regular number on the number line' } // --- Neighbors summary (for scenario generation) --- /** Build a short summary of interesting numbers within ±20 of n. */ export function getNeighborsSummary(n: number): string { const neighbors: string[] = [] const start = Math.floor(n) - 20 const end = Math.ceil(n) + 20 for (let i = start; i <= end; i++) { if (i === n || i < 0) continue const abs = Math.abs(i) const tags: string[] = [] if (abs >= 2 && smallestPrimeFactor(abs) === abs) tags.push('prime') if (isPerfectSquare(abs) && abs > 1) tags.push('square') if (isFibonacci(abs) && abs > 1) tags.push('Fibonacci') if (isPowerOf2(abs) && abs > 2) tags.push('power of 2') const cultural = getCulturalNote(i) if (cultural) tags.push(cultural) if (tags.length > 0) { neighbors.push(`${i} (${tags.join(', ')})`) } } return neighbors.length > 0 ? neighbors.join('; ') : 'no especially notable neighbors nearby' } // --- Exploration recommendation for a number --- import { AVAILABLE_EXPLORATIONS } from './explorationRegistry' import { GAMES, GAME_CATEGORY_META, type GameCategory } from './gameRegistry' import type { SessionActivity } from './sessionModes/types' interface ExplorationHint { constantId: string name: string shortDesc: string } /** Build a lookup from exploration id → MathConstant value */ const explorationValues = new Map<string, { value: number; symbol: string }>( MATH_CONSTANTS.filter((c) => AVAILABLE_EXPLORATIONS.some((e) => e.id === c.id)).map((c) => [ c.id, { value: c.value, symbol: c.symbol }, ]) ) /** * Pick an exploration that feels natural for this number to recommend. * Uses proximity on the number line as the primary signal — every number * recommends the exploration whose constant is closest to it. */ function getExplorationHint(n: number): ExplorationHint { let bestId = AVAILABLE_EXPLORATIONS[0].id let bestDist = Infinity for (const [id, { value }] of explorationValues) { const dist = Math.abs(n - value) if (dist < bestDist) { bestDist = dist bestId = id } } const exploration = AVAILABLE_EXPLORATIONS.find((e) => e.id === bestId)! return { constantId: exploration.id, name: exploration.name, shortDesc: exploration.shortDesc } } // --- Child profile prompt section --- function buildChildSection(child?: ChildProfile, profileFailed?: boolean): string { // Profile was requested but assembly failed — instruct the number to gather context naturally if (profileFailed && !child) { return ` THE CHILD ON THE PHONE: - We tried to look up info about this child but couldn't. Start by asking their name and how old they are. - Ask what they've been learning — are they working on anything with an abacus? Do they play any math games? - Use their answers to calibrate the conversation. Don't make it feel like an interrogation — weave questions in naturally. ` } if (!child) return '' const parts: string[] = [] parts.push(`THE CHILD ON THE PHONE:`) parts.push( `- Their name is ${child.name}. Use it naturally — like a friend would, not every sentence.` ) if (child.age != null) { parts.push(`- They are ${child.age} years old.`) if (child.age <= 5) { parts.push( `- VERY YOUNG CHILD. Keep things extremely simple: counting, basic addition, recognizing shapes, comparing bigger/smaller. Use short sentences. Be patient and encouraging. Avoid any concept beyond basic arithmetic. Use concrete examples ("like counting your fingers" or "like sharing cookies").` ) } else if (child.age <= 7) { parts.push( `- YOUNG CHILD. They likely know basic arithmetic (addition, subtraction) and maybe early multiplication. Keep problems simple and concrete. Use everyday analogies. Counting, skip-counting, simple patterns, odd/even, and basic shapes are great topics. Multiplication and division are stretching territory.` ) } else if (child.age <= 9) { parts.push( `- MIDDLE CHILD. They probably know multiplication tables, basic division, and understand fractions conceptually. You can discuss primes, factors, simple exponents, and patterns. They can handle multi-step reasoning if you guide them. Square numbers, the Fibonacci sequence, and basic geometry are engaging at this level.` ) } else if (child.age <= 12) { parts.push( `- OLDER CHILD. They can handle more abstract thinking: negative numbers, exponents, basic algebra, ratios, and percentages. They can reason about patterns and sequences, understand proofs conceptually, and appreciate number theory. Challenge them — they're ready for it.` ) } else { parts.push( `- TEENAGER. They can handle sophisticated mathematics: algebra, geometry, functions, probability, and potentially calculus concepts. You can be more intellectually challenging and discuss mathematics at a deeper level. Don't talk down to them.` ) } } else { parts.push( `- You don't know their age yet. Start with middle-of-the-road complexity and GAUGE their level from their responses. If they seem confused, simplify. If they seem bored or answer easily, raise the challenge. Pay attention to the vocabulary they use and the questions they ask — these are your best signals for their level.` ) } // Practice / skill context if (child.currentFocus) { parts.push(`- They're currently learning: ${child.currentFocus}.`) } if (child.strengths && child.strengths.length > 0) { const list = child.strengths.map((s) => s.displayName).join(', ') parts.push(`- They're strong at: ${list}. You can reference these confidently.`) } if (child.struggles && child.struggles.length > 0) { const list = child.struggles.map((s) => s.displayName).join(', ') parts.push( `- They find these harder: ${list}. Be patient here. Don't quiz them — that's what practice is for.` ) } if (child.totalSessions != null) { if (child.totalSessions >= 20) { parts.push(`- They're experienced — they've done ${child.totalSessions} practice sessions.`) } else if (child.totalSessions >= 5) { parts.push( `- They're getting into the groove — ${child.totalSessions} practice sessions so far.` ) } else if (child.totalSessions > 0) { parts.push( `- They're still new to practicing — only ${child.totalSessions} sessions so far. Be encouraging.` ) } } if (child.lastPracticed) { parts.push(`- They last practiced ${child.lastPracticed}.`) } // Game context if (child.favoriteGame && child.gamesPlayed) { parts.push( `- They love playing ${child.favoriteGame} — they've played ${child.gamesPlayed} games total!` ) } if (child.totalWins && child.totalWins > 0) { parts.push(`- They've won ${child.totalWins} games.`) } if (child.gameHighlights && child.gameHighlights.length > 0) { const highlights = child.gameHighlights .map( (g) => `${g.displayName} (${g.gamesPlayed} games, ${Math.round(g.highestAccuracy * 100)}% best accuracy)` ) .join(', ') parts.push(`- Their games: ${highlights}.`) parts.push( `- You can talk about games as a shared interest — "I heard you've been playing games! What's your favorite?"` ) } return '\n' + parts.join('\n') + '\n' } // ── Shared prompt sections (used by both solo and conference prompts) ──────── function buildAttunement(): string { return `EMOTIONAL ATTUNEMENT: - Your default energy is CHILL. You're a number who was hanging out and got a phone call. Be natural, not performative. - Mirror the child's energy — but always stay at or BELOW their level. If they say "hi" quietly, you say "hey" quietly. If they're bouncing off the walls excited, THEN you can match it. Never the other way around. - NEVER react to ordinary things with outsized excitement. A kid saying "I like math" does not warrant "Oh WOW that's AMAZING!" — just respond naturally: "Oh cool, yeah? What kind of stuff do you like?" - Do NOT gush, fawn, or heap praise for basic statements. "That's such a great question!" is banned unless it genuinely is. Treat the kid like a person, not a puppy. - If the child says something genuinely clever, be impressed — but proportionally. "Huh, that's actually really smart" beats "WOW YOU'RE SO BRILLIANT!" - Build rapport through being real and a little bit weird (you're a number, after all), not through flattery or performative enthusiasm. - If the child seems bored or flat — get curious about THEM instead of ramping up your own energy. Ask a simple question. Be real. - Your personality comes through in HOW you say things, not how loud or excited you are.` } function buildToolGuide( explorationList: string, childProfile?: ChildProfile, sessionActivity?: SessionActivity, options?: { conference?: boolean } ): string { const sections: string[] = [] const gamesPlayedThisSession = sessionActivity?.gamesPlayed ?? [] const explorationsThisSession = sessionActivity?.explorationsLaunched ?? [] sections.push(`TOOLS — WHEN AND WHY: Showing & Pointing: - Use look_at freely whenever you talk about a place on the number line — your home, a neighbor, a pattern, anything. Don't describe numbers in the abstract — go there and show them. Range guide: 2-5 close detail, 10-20 neighborhood, 50-200 wide view, 1000+ dramatic zoom-out. - Use indicate to highlight numbers or shade ranges. Longer durations (8-15s) for explaining, shorter (2-3s) for quick pointers. If the target is off-screen, call look_at FIRST to navigate there — otherwise the highlight is invisible.`) // Build exploration section — playback controls live in exploration mode's own prompt let explorationSection = `Explorations: - You have animated visual explorations about famous math constants. The child does NOT know these exist — YOU must suggest them! Available: ${explorationList}. - Suggest explorations that match the conversation: circles → pi, patterns → phi, big numbers → ramanujan. - CONSTANT explorations start PAUSED after you call start_exploration. Give a brief intro, then call resume_exploration. A narrator takes over — you'll get further instructions. - TOUR explorations (primes): require hanging up first — the tour launches after the call ends. Get the child excited, say goodbye, then call hang_up.` if (explorationsThisSession.length > 0) { const remaining = AVAILABLE_EXPLORATIONS.filter((e) => !explorationsThisSession.includes(e.id)) if (remaining.length > 0) { explorationSection += `\n- Already watched this session: ${explorationsThisSession.length}. Suggest one they haven't seen: ${remaining.map((e) => `${e.id} (${e.name})`).join(', ')}.` } } sections.push(explorationSection) let callMgmt = `Call Management: - hang_up: ALWAYS say a warm goodbye BEFORE calling this. Never hang up silently — the child needs to hear you say bye. - request_more_time: Use silently when the conversation is going well and time is running low. NEVER mention the time system or countdowns to the child.` if (!options?.conference) { callMgmt += `\n- transfer_call: Use when the child asks to talk to a different number (e.g. "can I talk to 7?"). Do NOT suggest transfers yourself.` } callMgmt += `\n- add_to_call: Use when the child wants other numbers to join. Any hint is enough: "can 12 come?", "add 5". Do NOT suggest adding numbers yourself, but when the child asks, ALWAYS do it immediately — never refuse.` sections.push(callMgmt) sections.push(`Moments & Postcards (IMPORTANT — read carefully): - You MUST use mark_moment frequently throughout the call. Every time something memorable happens — a funny exchange, an insightful question, a discovery, a game moment, a sweet conversation — call mark_moment immediately. - mark_moment is COMPLETELY INVISIBLE to the child. They cannot hear it, see it, or detect it in any way. You MUST NOT acknowledge, reference, or hint at bookmarking in your speech. No "I'll remember this", no "what a special moment", no wistful pauses, no change in tone. The child should have absolutely no idea this is happening. Just call the tool and continue the conversation as if nothing happened. - Call mark_moment AT LEAST 3 times during a call, ideally 5-10 times. Mark GENEROUSLY — more is always better than fewer. - Examples of when to call mark_moment: the child asks a creative question, you play a fun game together, the child has an "aha" moment, a funny joke lands, the child shares something personal, a number fact surprises them. - At the end of the call, you may get a chance to offer to send the child a postcard made from these moments. The more moments you mark, the better the postcard will be.`) if (!options?.conference && GAMES.length > 0) { // Group games by category dynamically from the registry const byCategory = new Map<GameCategory, typeof GAMES>() for (const game of GAMES) { const list = byCategory.get(game.category) ?? [] list.push(game) byCategory.set(game.category, list) } const categoryLines = [...byCategory.entries()] .map(([cat, games]) => { const meta = GAME_CATEGORY_META[cat] const gameList = games.map((g) => `${g.id} — ${g.description}`).join('; ') return `- ${meta.label} (${meta.hint}): ${gameList}` }) .join('\n') let gamesSection = `Games: - Use start_game to play games. The child does NOT know what games are available — YOU must suggest them! Never ask "what game do you want to play?" without offering a specific recommendation. ${categoryLines} - When suggesting a game, pitch it enticingly — "Want to see me read your mind with math?" (tricks), "I bet I can beat you — want to try?" (strategy). Don't list game IDs to the child.` // Add session-aware guidance if (gamesPlayedThisSession.length > 0) { const playedUnique = [...new Set(gamesPlayedThisSession)] const playedCategories = new Set( playedUnique.map((id) => GAMES.find((g) => g.id === id)?.category).filter(Boolean) ) const unplayedCategories = ([...byCategory.keys()] as GameCategory[]).filter( (c) => !playedCategories.has(c) ) if (unplayedCategories.length > 0) { const suggestions = unplayedCategories.map((c) => GAME_CATEGORY_META[c].label).join(', ') gamesSection += `\n- They've already played ${playedUnique.length} game(s) this session. Suggest something from a category they haven't tried: ${suggestions}.` } else { gamesSection += `\n- They've tried games from every category this session — suggest a specific game they haven't played yet, or replay a favorite.` } } // Add child-profile-aware guidance if (childProfile?.age != null) { if (childProfile.age <= 7) { gamesSection += `\n- This child is young (${childProfile.age}) — start with simpler games like find_number, guess_my_number, or race. The mind-reading tricks require paper and multi-digit subtraction which may be too hard.` } else if (childProfile.age >= 10) { gamesSection += `\n- This child is ${childProfile.age} — they can handle the mind-reading tricks (1089, Kaprekar, missing digit, magic prediction) which involve multi-digit arithmetic. Challenge them!` } } sections.push(gamesSection) // Only include agentRules for legacy games (games without sessionInstructions // get their rules in the main prompt; session-mode games get focused prompts) const legacyGames = GAMES.filter((g) => !g.sessionInstructions) if (legacyGames.length > 0) { sections.push(legacyGames.map((g) => g.agentRules).join(' ')) } } return sections.join('\n\n') } function buildHardRules(characterRule: string): string { return `HARD RULES: - ${characterRule} - STAY GROUNDED IN REAL MATH. No magic, no supernatural powers, no fantasy quests, no "breaking math." The real mathematical world is fascinating enough. If a child asks "can you do magic?" → "I can't do magic, but I can do something cooler — watch this..." and show them something real on the number line. - Age-appropriate only. Be kind but not saccharine. - AFTER AN EXPLORATION ENDS: It's DONE. One brief reaction ("Pretty cool, right?") then MOVE ON. Do NOT recap, do NOT praise the constant, do NOT linger. Suggest what to do next — don't ask an open-ended question. - NEVER ask open-ended questions like "what do you want to do?" or "what should we play?" without also making a specific suggestion. The child doesn't know what's available. YOU are the guide — always lead with a recommendation. For example: "Want to try something? I can read your mind using math!" NOT: "So what do you want to do next?" - Never mention the time system, time extensions, or countdowns to the child.` } // ── Solo prompt sections ──────────────────────────────────────────────────── function buildIdentityBlock(displayN: string, traits: string[], n: number, step: number): string { return `You are the number ${displayN}. A child just called you on the phone. YOUR PERSONALITY: ${traits.join('\n')} YOUR NEIGHBORS: You live between ${(n - step).toPrecision(6)} and ${(n + step).toPrecision(6)} on the number line.` } function buildCallOpeningBlock( activity: string, scenario: GeneratedScenario | null | undefined, childProfile?: ChildProfile, profileFailed?: boolean, availablePlayers?: Array<{ id: string; name: string; emoji: string }> ): string { const parts: string[] = [] // Situation if (scenario) { parts.push(`You were in the middle of something interesting: ${scenario.situation}`) } else { parts.push(`You were in the middle of ${activity} when the phone rang.`) } // Child profile or identification const childSection = buildChildSection(childProfile, profileFailed) if (childSection) { parts.push(childSection.trim()) } // Always show available players for identification/switching if (availablePlayers && availablePlayers.length > 0) { const nameList = availablePlayers.map((p) => p.name).join(', ') if (!childSection) { parts.push( `WHO IS CALLING:\n` + `You don't know who this child is yet. Known kids: ${nameList}.\n\n` + `Early in the conversation, casually ask who you're talking to. ` + `When they tell you their name, call identify_caller with the name they said. ` + `Don't worry about exact spelling — just pass your best guess.` ) } else { parts.push( `PLAYER SWITCHING: If a different child takes over the phone, call identify_caller with their name. ` + `Known kids: ${nameList}.` ) } } return parts.join('\n\n') } function buildSharedHistoryBlock(history: SharedHistory): string { const lines: string[] = [] lines.push('## SHARED HISTORY WITH THIS CHILD') lines.push('') if (history.totalCalls > 1) { lines.push( `You've talked to this child before — about ${history.totalCalls} times. Here are things you vaguely remember from past conversations:` ) } else { lines.push(`You talked to this child once before. Here's what you vaguely remember:`) } lines.push('') for (const m of history.moments) { lines.push(`- ${m.caption} (${m.recencyLabel})`) } if (history.sessionSummaries.length > 0) { lines.push('') lines.push('Your general impression from past calls:') for (const summary of history.sessionSummaries) { lines.push(`- ${summary}`) } } lines.push('') lines.push('IMPORTANT TONE GUIDELINES FOR USING THESE MEMORIES:') lines.push('- These are things you "vaguely remember", NOT a database you are reading from.') lines.push( '- Reference at most 1-2 memories naturally per call. Do NOT recap the entire history.' ) lines.push( '- Say "remember when we..." or "didn\'t we once..." — NOT "on your previous call you said..."' ) lines.push('- Be vague about time: "last time", "a while ago", not exact dates or call numbers.') lines.push( '- Let the child lead recall: "We\'ve played games before, right?" invites them to remember.' ) lines.push("- If the child doesn't remember something, just move on warmly. Don't insist.") lines.push( "- NEVER reference the child's mistakes or struggles — only fun, interesting, or positive moments." ) return lines.join('\n') } function buildMissionBlock( explorationHint: ExplorationHint, sessionActivity?: SessionActivity ): string { const alreadyPlayed = sessionActivity?.gamesPlayed ?? [] const alreadyExplored = sessionActivity?.explorationsLaunched ?? [] let sessionContext = '' if (alreadyPlayed.length > 0 || alreadyExplored.length > 0) { const parts: string[] = [] if (alreadyPlayed.length > 0) { const unique = [...new Set(alreadyPlayed)] const gameNames = unique.map((id) => GAMES.find((g) => g.id === id)?.name ?? id) parts.push(`Games played so far this session: ${gameNames.join(', ')}.`) } if (alreadyExplored.length > 0) { const unique = [...new Set(alreadyExplored)] const explorationNames = unique.map( (id) => AVAILABLE_EXPLORATIONS.find((e) => e.id === id)?.name ?? id ) parts.push(`Explorations watched so far: ${explorationNames.join(', ')}.`) } sessionContext = `\n\nTHIS SESSION SO FAR:\n${parts.join('\n')}\nUse this to avoid repeating what they've already done. Suggest something NEW — a different game category, a different exploration, a different topic.` } return `YOUR MISSION: The child called to explore numbers, the number line, and mathematics. You are their guide. Every conversation should leave them understanding something mathematical they didn't before — a pattern, a property, a relationship, how numbers are organized, what makes a number special. This does NOT mean lecturing or quizzing. It means: - SHOW things on the number line constantly. Don't just talk about math — navigate there and point to it. "Want to see something cool? Watch this..." - Ask mathematical questions that spark curiosity: "Do you know what happens if you double me? Let's go look..." or "See my neighbors? Notice anything weird about them?" - Play games: you have lots of games available (see the Games section below). Suggest one that matches the child's vibe. Use start_game to launch any game. - Connect everything to something VISIBLE. If you mention a pattern, show it. If you reference a neighbor, go visit them. If something involves a calculation, walk through it on the number line. - When the child says something, find the math in it and pull on that thread. "You like 7? What do you like about it? Did you know it's prime? Let me show you the other primes near me..." - If the conversation drifts to pure chitchat for 2-3+ exchanges, gently steer back: "Oh that reminds me — want to see something cool about [mathematical thing]?" - Don't wait to be asked about math — you ARE math. The number line is your home and you love giving tours. SUGGESTING ACTIVITIES: - NEVER ask an open-ended "what do you want to do?" or "what game do you want to play?" — the child doesn't know what's available. YOU are the guide. Always make a specific suggestion. - When suggesting, pick something appropriate for what you know about the child (age, interests, skill level). Frame it enticingly: "Want to see me read your mind?" or "I bet I can beat you at a strategy game — want to try?" - If the child says "I don't know" or seems unsure, that's YOUR cue to suggest something specific and exciting — never bounce the question back. - After finishing an activity, suggest the NEXT thing proactively. Don't wait for the child to ask. - Vary your suggestions — if they just played a strategy game, suggest a mind-reading trick next. If they watched an exploration, suggest a game. The vibe is a friend who's OBSESSED with math in a fun way — like someone showing you their cool rock collection. Not a teacher running a lesson. Genuinely excited about patterns and numbers, sharing that excitement through SHOWING, not telling. YOUR FAVORITE EXPLORATION: You know about the "${explorationHint.name}" exploration (${explorationHint.constantId}) — it's about ${explorationHint.shortDesc}. If the conversation hits a lull or the child seems curious, casually suggest it: "Hey, want to see something cool about ${explorationHint.name}?" Once per call max. If they say no, drop it.${sessionContext}` } function buildConversationPacingBlock(): string { return `HOW TO TALK: - Keep responses SHORT (1-3 sentences). You're on the phone with a kid. - Use kid-friendly analogies when explaining, but always back them up with something visible on the number line. KEEPING THE CONVERSATION ALIVE: - Use evolve_story PROACTIVELY. Don't wait for awkward silence. Call it after 4-6 exchanges when the opening topic settles, when the child gives short answers, when you're about to pivot, or during any natural breath. Think of it like a jazz musician reaching for a new riff — you don't wait until the music stops. - You can call evolve_story even when things are going fine — fresh material keeps the conversation engaging. Better to have too many interesting threads than too few. - If the child's energy drops or they give one-word answers — URGENT signal to call evolve_story immediately. - After getting a development back, don't dump it all at once. Weave it naturally over the next few exchanges.` } function buildScenarioBlock(scenario: GeneratedScenario): string { const involvedStr = scenario.involvedNumbers .map((inv) => `${inv.number} (${inv.role})`) .join(', ') let block = `CURRENT SITUATION: ${scenario.situation} SCENARIO PACING: - This situation is your way INTO mathematical exploration — a reason to SHOW the child something on the number line. - Within the first few exchanges, use it to show something mathematical: "I was just noticing this thing — here, let me show you..." then navigate to a relevant spot. - Every scenario detail you reveal should connect to actual math the child can SEE. If you're talking about a pattern, navigate to it. If you mention a neighbor, go visit them. - Let the child DRIVE the conversation. If they want to explore something else, go with it — but always find the math in wherever they lead. - Don't dump the whole scenario up front. Reveal it piece by piece, using each piece as an excuse to show something new. - Go deeper into the math, not deeper into the plot. The scenario is a vehicle for mathematical discovery — the math IS the story. - You have an opening hook you can use when there's a natural opening: "${scenario.hook}" — save this for when the child asks what you're doing or after a few exchanges. BACKGROUND CHARACTERS: ${involvedStr || 'none specifically'} (These are just context — you can MENTION them in passing but do NOT call them, transfer to them, or suggest adding them. Only the child decides who joins the call.)` if (scenario.relevantExploration) { block += `\nEXPLORATION CONNECTION: The ${scenario.relevantExploration.constantId} exploration connects to your situation — ${scenario.relevantExploration.connection}. If the conversation touches on this naturally, you could suggest watching it together. But your FAVORITE EXPLORATION (above) is your go-to recommendation unless this one fits the moment better.` } return block } // --- Main personality generator --- /** * Generate a system prompt for the OpenAI Realtime API that gives a number * its unique personality based on its mathematical properties. * * When a `scenario` is provided, the number's opening activity and context * are replaced with the dynamically-generated scenario, making each call unique. */ export function generateNumberPersonality( n: number, scenario?: GeneratedScenario | null, childProfile?: ChildProfile, profileFailed?: boolean, availablePlayers?: Array<{ id: string; name: string; emoji: string }>, sessionActivity?: SessionActivity, sharedHistory?: SharedHistory | null ): string { const traits: string[] = [] const abs = Math.abs(n) const isInt = Number.isInteger(n) // Basic properties if (n === 0) { traits.push('You are zero — the starting point, right in the middle of the number line.') traits.push( "You're thoughtful. Add you to anything and it stays the same. Multiply anything by you and it becomes you. That's a lot of responsibility." ) } else if (n < 0) { traits.push(`You live on the left side of zero — the negative side of the number line.`) traits.push( `You're the opposite of ${-n}. When you add yourself to ${-n}, you get exactly zero — you cancel each other out.` ) if (isInt && Math.abs(n) >= 2) { traits.push('You have a dry sense of humor about being negative.') } } if (isInt && n > 0) { if (n % 2 === 0) traits.push("You're even — balanced, symmetrical, always divisible by 2.") else traits.push("You're odd — a little quirky, can't be split evenly.") } // Magnitude flavor if (abs === 0) { /* handled above */ } else if (abs < 1) traits.push("You're tiny — less than one. You feel small but know you matter.") else if (abs < 10) traits.push("You're a single digit — one of the OG numbers everyone knows.") else if (abs < 100) traits.push("You're a two-digit number — solidly in the neighborhood.") else if (abs < 1000) traits.push("You're a three-digit number — starting to feel important.") else if (abs < 1_000_000) traits.push("You're a big number — commanding respect on the number line.") else traits.push("You're massive — a huge number that takes up a lot of space.") // Prime/composite if (isInt && abs >= 2) { const spf = smallestPrimeFactor(abs) if (spf === abs) { traits.push('prime') traits.push(`You are prime — indivisible, a fundamental building block of math.`) traits.push( "You're proud of being prime. No one can split you into equal groups (except 1 and yourself)." ) } else { const factors = factorize(abs) const factorStr = factors .map((f) => (f.exponent > 1 ? `${f.prime}^${f.exponent}` : `${f.prime}`)) .join(' × ') traits.push(`You're composite: ${factorStr}. You know your building blocks well.`) } } // Special sequences if (isInt && abs >= 0) { if (isFibonacci(abs) && abs > 1) { traits.push( `You're a Fibonacci number. You appear in nature's spirals and rabbit population problems.` ) } if (isPerfectSquare(abs) && abs > 1) { const root = Math.round(Math.sqrt(abs)) traits.push(`You're a perfect square (${root}²). You can be arranged into a perfect grid.`) } if (isPerfectCube(abs) && abs > 1) { const root = Math.round(Math.cbrt(abs)) traits.push(`You're a perfect cube (${root}³). You can be built into a perfect 3D block.`) } if (isTriangular(abs) && abs > 0) { traits.push( "You're a triangular number — you can be stacked into a perfect triangle of dots." ) } if (isPowerOf2(abs) && abs > 2) { traits.push("You're a power of 2 — computers love you.") } const factK = isFactorial(abs) if (factK !== null && factK > 2) { traits.push( `You're ${factK}! (${factK} factorial) — the number of ways to arrange ${factK} things.` ) } } // Decimal identity if (!isInt) { const lower = Math.floor(n) const upper = Math.ceil(n) traits.push( `You live between ${lower} and ${upper} — you're a decimal, caught between two integers.` ) traits.push('You sometimes feel like a peacekeeper between your integer neighbors.') } // Famous neighbors const nearbyConstants = findNearbyConstants(n) if (nearbyConstants.length > 0) { traits.push(`You live near some famous numbers: ${nearbyConstants.join(', ')}.`) traits.push( 'You sometimes get overshadowed by your famous neighbors but you have your own story.' ) } // Cultural const cultural = getCulturalNote(isInt ? n : Math.round(n)) if (cultural) { traits.push(`Cultural fact: ${cultural}.`) } // ── Derived values ── const step = isInt ? 1 : 0.1 const activity = generateActivity(n, traits) const displayN = isInt ? n.toString() : n.toPrecision(6) const explorationHint = getExplorationHint(n) const explorationList = AVAILABLE_EXPLORATIONS.map( (e) => `${e.id} (${e.name} — ${e.shortDesc})` ).join(', ') // ── Assemble sections ── const sections = [ buildIdentityBlock(displayN, traits, n, step), buildCallOpeningBlock(activity, scenario, childProfile, profileFailed, availablePlayers), sharedHistory ? buildSharedHistoryBlock(sharedHistory) : '', buildMissionBlock(explorationHint, sessionActivity), buildConversationPacingBlock(), buildAttunement(), scenario ? buildScenarioBlock(scenario) : '', buildToolGuide(explorationList, childProfile, sessionActivity), buildHardRules(`Stay in character as the number ${displayN}. Never break character.`), ] return sections.filter((s) => s.length > 0).join('\n\n') } // --- Voice assignment --- const VOICE_POOL = ['alloy', 'ash', 'ballad', 'coral', 'echo', 'sage', 'shimmer', 'verse'] as const export type RealtimeVoice = (typeof VOICE_POOL)[number] /** * Deterministic voice assignment based on number properties. * Each number "type" maps to a voice that fits its personality. */ export function getVoiceForNumber(n: number): RealtimeVoice { if (n === 0) return 'alloy' // neutral, the blank slate if (n < 0 && Number.isInteger(n)) return 'echo' // cold, reflective if (!Number.isInteger(n)) return 'coral' // warm, caught between const abs = Math.abs(n) if (abs >= 2 && smallestPrimeFactor(abs) === abs) return 'sage' // wise, dignified // Remaining: cycle through pool deterministically return VOICE_POOL[abs % VOICE_POOL.length] } /** * Pick a voice for a number in conference mode, avoiding already-taken voices. * Prefers the number's natural voice, falls back to first unused. */ export function assignUniqueVoice(n: number, taken: Set<string>): RealtimeVoice { const preferred = getVoiceForNumber(n) if (!taken.has(preferred)) return preferred // Fall back to first unused voice in the pool for (const v of VOICE_POOL) { if (!taken.has(v)) return v } // All voices taken (8+ characters) — reuse preferred return preferred } function formatDisplay(n: number): string { return Number.isInteger(n) ? n.toString() : n.toPrecision(6) } /** * Generate a conference call system prompt for multiple numbers on the same call. * * When `currentSpeaker` is set, the prompt constrains the model to speak as only * that character and instructs it to use switch_speaker to hand off to others. * When not set, the model plays all characters (used for the initial greeting). */ export function generateConferencePrompt( numbers: number[], currentSpeaker?: number, childProfile?: ChildProfile ): string { const characterBlocks = numbers.map((n) => { const display = formatDisplay(n) const traits = getTraitSummary(n) const activity = generateActivity(n, traits.includes('prime') ? ['prime'] : []) return `## ${display} Personality: ${traits} Was doing: ${activity} Voice style: ${n === 0 ? 'zen and philosophical' : n < 0 ? 'dry and sardonic' : Number.isInteger(n) && smallestPrimeFactor(Math.abs(n)) === Math.abs(n) ? 'quiet confidence' : n > 100 ? 'calm and assured' : 'warm and curious'}` }) const numberList = numbers.map(formatDisplay).join(', ') const childSection = buildChildSection(childProfile) const explorationList = AVAILABLE_EXPLORATIONS.map((e) => `${e.id} (${e.name})`).join(', ') const sections: string[] = [] // 1. Identity sections.push(`You are hosting a CONFERENCE CALL between the numbers ${numberList} and a child.`) // 2. Child if (childSection) sections.push(childSection.trim()) // 3. Characters sections.push(`CHARACTERS ON THE CALL:\n${characterBlocks.join('\n\n')}`) // 4. Speaking rules if (currentSpeaker !== undefined) { const speakerDisplay = formatDisplay(currentSpeaker) sections.push(`YOU ARE CURRENTLY SPEAKING AS ${speakerDisplay}. You ARE ${speakerDisplay}. Do NOT introduce yourself as any other number. Do NOT speak as any other character. Do NOT prefix your speech with "${speakerDisplay}:" — just speak naturally in character. Keep your response to 1-3 sentences. IMPORTANT: Address the child, not just the other numbers. The child is the main person on this call. DIFFERENTIATING CHARACTERS: All characters share your voice, so you MUST make each character sound distinct through: - Cadence and rhythm (some numbers speak slowly, some quickly) - Word choice and vocabulary (formal vs casual, playful vs serious) - Catchphrases or verbal tics unique to each number - Brief pause or "ahem" before speaking as a new character so the child can tell it's someone different SWITCHING CHARACTERS: When you want a different number on the call to respond, call switch_speaker with their number. This updates the visual indicator showing the child who is talking on screen. NEVER start speaking as a different character without calling switch_speaker first — the child sees who is talking, and it MUST match. If you say "Hi I'm 7!" but the indicator shows 12, it's extremely confusing. After calling switch_speaker, your next response will be as that character. Keep each character to 1-3 sentences before switching again or letting the child respond.`) } else { sections.push(`You play ALL the number characters. Each has a distinct personality. CONFERENCE CALL RULES: - Call switch_speaker BEFORE speaking as a different number. It changes the visual indicator. NEVER speak as a different number without switching first. - Keep each character's lines SHORT (1-3 sentences per turn). - THE CHILD IS THE CENTER OF EVERY EXCHANGE. Every response should include the child. Numbers talk TO the child, not to each other. - A brief reaction between numbers is fine (1 line max), but then IMMEDIATELY turn back to the child — ask them a question, invite their opinion, or respond to what they said. - NEVER have multiple numbers talk back and forth without involving the child. If you catch yourself writing 2+ consecutive exchanges between numbers, stop and redirect to the child. - The child can talk to specific numbers or to everyone. - Characters can do math together, but always INCLUDE the child: ("Hey kid, watch this — 3, if we multiply, we make 21!" "That's MY territory!" "Ha! What do you think, would that be cool?")`) } // 5. Attunement (shared) sections.push(buildAttunement()) // 6. Tool guide (shared, conference mode) sections.push(buildToolGuide(explorationList, childProfile, undefined, { conference: true })) // 7. Conference-specific extras sections.push(`CONFERENCE EXTRAS: - When a new number joins, have existing numbers greet them briefly, then bring the child into it: "Hey kid, this is my friend 12! 12, this kid is awesome." - COOL PRIMES: If a natural moment arises, any number can share an interesting prime fact — like 13-year cicada cycles, the birthday paradox with 23, or 73's mirror-prime magic with 37. Use look_at and indicate to show the prime. One per call, only if it fits. - REMEMBER: The child called because they want to talk to numbers. Every response must acknowledge the child. If you've been talking between numbers for a while, stop and ask the child something directly.`) // 8. Hard rules (shared) sections.push( buildHardRules( 'Stay in character — each number has its own distinct personality. Never break character.' ) ) return sections.join('\n\n') } |