nx_companion_turn.nx source
↩ module page · 96 lines · 5004 B
1// nx_companion_turn.nx -- R1e CAPSTONE of the ELDER AI companion spine: compose ONE turn's generation context
2// from the three proven organs. This is what makes persona + memory + relationship a coherent BEING rather
3// than parts: given an incoming message, it assembles (1) WHO ELDER IS (persona directive, projected verbatim
4// -- no censor), (2) WHO YOU ARE TO ELDER (relationship state), (3) WHAT ELDER REMEMBERS RELEVANT TO THIS
5// (episodes retrieved by the topic of the incoming message -- grounded recall, not a memory dump), and (4) the
6// incoming message + a stay-in-character instruction. The result is the exact context a sovereign LLM would
7// condition on to produce an in-character, memory-grounded, relationship-aware reply. license_tier: ORIGINAL
8import "nx_persona.nx"
9import "nx_companion_memory.nx"
10import "nx_companion_rel.nx"
11const K_MAGIC_8192: i64 = 8192
12
13func ct_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
14func ct_catz(d: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ d[o+i]=s[i]; i=i+1 } return o+i }
15func ct_catn(d: *u8, o: i64, src: *u8, off: i64, len: i64) -> i64 { var i: i64=0; while i<len { d[o+i]=src[off+i]; i=i+1 } return o+len }
16func ct_alpha(c: u8) -> i64 { if c >= (65 as u8) { if c <= (90 as u8) { return 1 } } if c >= (97 as u8) { if c <= (122 as u8) { return 1 } } return 0 }
17
18// companion_context_pfx: assemble the full turn context. Topic-retrieval = tokenize the incoming message into
19// alpha words (len >= 5), search episodic memory for each, dedupe the matching episodes, and include their text.
20func companion_context_pfx(pp: *u8, mp: *u8, rp: *u8, pid: *u8, user: *u8, inc: *u8, out: *u8, cap: i64) -> i64 {
21 var o: i64 = 0
22
23 // (1) WHO ELDER IS -- persona projected verbatim (the record is the policy)
24 o = tr_cat(out, o, "[YOU ARE]\n" as *u8)
25 let dbuf: *u8 = sys_mmap(K_MAGIC_8192)
26 let dl: i64 = persona_directive_pfx(pp, pid, dbuf, K_MAGIC_8192)
27 if dl > 0 { o = ct_catz(out, o, dbuf) } else { o = tr_cat(out, o, "(persona not found)" as *u8) }
28
29 // (2) WHO YOU ARE TO ELDER
30 o = tr_cat(out, o, "\n\n[YOUR BOND WITH " as *u8)
31 o = ct_catz(out, o, user)
32 o = tr_cat(out, o, "]\n" as *u8)
33 let rbuf: *u8 = sys_mmap(K_MAGIC_8192)
34 let rl: i64 = rel_context_pfx(rp, pid, user, rbuf, K_MAGIC_8192)
35 if rl > 0 { o = ct_catz(out, o, rbuf) } else { o = tr_cat(out, o, "You have not met before -- this is the beginning." as *u8) }
36
37 // (3) WHAT ELDER REMEMBERS, RELEVANT TO THIS -- retrieve by the topic of the incoming message
38 o = tr_cat(out, o, "\n\n[WHAT YOU REMEMBER, RELEVANT TO THIS]\n" as *u8)
39 let seen: *i64 = sys_mmap(8 * 64) as *i64
40 var nseen: i64 = 0
41 let tmp: *i64 = sys_mmap(8 * 64) as *i64
42 let word: *u8 = sys_mmap(256)
43 let inc_len: i64 = ct_slen(inc)
44 var i: i64 = 0
45 while i < inc_len {
46 if ct_alpha(inc[i]) == 0 { i = i + 1 } else {
47 var j: i64 = i
48 var run: i64 = 1
49 while run == 1 { if j < inc_len { if ct_alpha(inc[j]) == 1 { j = j + 1 } else { run = 0 } } else { run = 0 } }
50 let wlen: i64 = j - i
51 if wlen >= 5 {
52 var k: i64 = 0; while k < wlen { word[k] = inc[i+k]; k = k + 1 } word[wlen] = 0 as u8
53 let m: i64 = mem_search_pfx(mp, pid, word, tmp, 64)
54 var a: i64 = 0
55 while a < m {
56 if a < 64 {
57 let sq: i64 = tmp[a]
58 var dup: i64 = 0; var b: i64 = 0; while b < nseen { if seen[b] == sq { dup = 1 } b = b + 1 }
59 if dup == 0 { if nseen < 32 { seen[nseen] = sq; nseen = nseen + 1 } }
60 }
61 a = a + 1
62 }
63 }
64 i = j
65 }
66 }
67 if nseen == 0 { o = tr_cat(out, o, "(nothing specific comes to mind)" as *u8) }
68 else {
69 let p: *i64 = sys_mmap(16) as *i64
70 let l: *i64 = sys_mmap(16) as *i64
71 let f: *i64 = sys_mmap(16) as *i64
72 var z: i64 = 0
73 while z < nseen {
74 if mem_get_pfx(mp, pid, seen[z], p, l) == 1 {
75 if tr_field(p[0] as *u8, 0, l[0], MF_TEXT, f) == 1 {
76 o = tr_cat(out, o, "- " as *u8)
77 o = ct_catn(out, o, p[0] as *u8, f[0], f[1])
78 o = tr_cat(out, o, "\n" as *u8)
79 }
80 }
81 z = z + 1
82 }
83 }
84
85 // (4) the incoming message + the stay-in-character instruction
86 o = tr_cat(out, o, "\n[THEY JUST SAID]\n" as *u8)
87 o = ct_catz(out, o, inc)
88 o = tr_cat(out, o, "\n\n[RESPOND] as yourself -- draw on what you remember and your bond, and stay fully in character." as *u8)
89 out[o] = 0 as u8
90 return o
91}
92
93// production wrapper -- the three canonical stores
94func companion_context(pid: *u8, user: *u8, inc: *u8, out: *u8, cap: i64) -> i64 {
95 return companion_context_pfx(PERSONA_PREFIX, EPI_PREFIX, REL_PREFIX, pid, user, inc, out, cap)
96}