interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / allen_intent.c

405 lines · 14148 bytes Raw Blame History
1/* allen_intent.c -- what a line wants, and the job Allen writes for it; see allen_intent.h. */
2#include "allen_intent.h"
3
4static int starts(const char *f, const char *w)
5{
6 while (*w && *w == *f)
7 w++, f++;
8 return !*w && (*f == 0 || *f == ' ');
9}
10
11/* w as whole words anywhere in f */
12static int has(const char *f, const char *w)
13{
14 for (const char *s = f; *s; s++)
15 if ((s == f || s[-1] == ' ') && starts(s, w))
16 return 1;
17 return 0;
18}
19
20static int any(const char *f, const char *const *list, int (*by)(const char *, const char *))
21{
22 for (; *list; list++)
23 if (by(f, *list))
24 return 1;
25 return 0;
26}
27
28static const char *const make_first[] = {"write", "make", "build", "create", "code", "program", "draft",
29 "compose", "generate", "design", "implement", "invent", "name",
30 "give me a", "i need a", "i want a", 0};
31static const char *const make_any[] = {"a poem", "a haiku", "a story", "a song", "a script", "a function",
32 "a program", "a list of", "some code", "a letter", "an email",
33 "a plan for", "a name for", 0};
34static const char *const explain_first[] = {"explain", "what is", "what are", "whats", "what s", "why",
35 "how does", "how do", "how is", "how are", "describe",
36 "tell me about", "what does", "who was", "who is", "define",
37 "when was", "when did", "where is", "is it true", 0};
38static const char *const explain_any[] = {"difference between", "mean", "means", 0};
39static const char *const do_first[] = {"do", "run", "fix", "find", "search", "look up", "check", "open",
40 "send", "start", "stop", "install", "set up", "plan", "schedule",
41 "calculate", "work out", "convert", "translate", "summarize",
42 "summarise", "list", "sort", "count", "compare", "turn", "go", 0};
43
44ck_allen_want ck_allen_want_of(const char *f)
45{
46 if (!f || !*f)
47 return CK_WANT_CHAT;
48 /* a polite head is skipped: "please write", "can you explain", "could you make" */
49 static const char *const heads[] = {"please", "can you", "could you", "would you", "will you",
50 "i d like you to", "i want you to", "i need you to", "allen", 0};
51 for (int again = 1; again;) {
52 again = 0;
53 for (const char *const *h = heads; *h; h++) {
54 unsigned n = 0;
55 while ((*h)[n])
56 n++;
57 if (starts(f, *h) && f[n] == ' ') {
58 f += n + 1;
59 again = 1;
60 break;
61 }
62 }
63 }
64 if (any(f, explain_first, starts))
65 return CK_WANT_EXPLAIN;
66 if (any(f, make_first, starts) || any(f, make_any, has))
67 return CK_WANT_MAKE;
68 if (any(f, do_first, starts))
69 return CK_WANT_DO;
70 if (any(f, explain_any, has))
71 return CK_WANT_EXPLAIN;
72 return CK_WANT_CHAT;
73}
74
75const char *ck_allen_want_name(ck_allen_want w)
76{
77 switch (w) {
78 case CK_WANT_MAKE:
79 return "make";
80 case CK_WANT_EXPLAIN:
81 return "explain";
82 case CK_WANT_DO:
83 return "do";
84 default:
85 return "chat";
86 }
87}
88
89struct say {
90 char *out;
91 unsigned n, cap, full;
92};
93static void put(struct say *s, const char *t)
94{
95 for (; *t; t++) {
96 if (s->n + 1 >= s->cap) {
97 s->full = 1;
98 return;
99 }
100 s->out[s->n++] = *t;
101 }
102 s->out[s->n] = 0;
103}
104static void putu(struct say *s, uint32_t v)
105{
106 char d[12];
107 int n = 0;
108 do
109 d[n++] = (char)('0' + v % 10), v /= 10;
110 while (v);
111 while (n)
112 put(s, (char[]){d[--n], 0});
113}
114
115unsigned ck_allen_job(const ck_allen_mind *m, uint32_t seq, const char *line, char *out, unsigned cap)
116{
117 if (!out || cap < 2)
118 return 0;
119 char f[96];
120 ck_allen_fold(line ? line : "", f, sizeof f);
121 ck_allen_want w = ck_allen_want_of(f);
122 const char *self = ck_allen_self(m), *who = m->mem.name[0] ? m->mem.name : "the person";
123 struct say s = {out, 0, cap, 0};
124 put(&s, "JOB ");
125 putu(&s, seq);
126 put(&s, " ");
127 put(&s, ck_allen_want_name(w));
128 put(&s, " ");
129 /* The instruction: Allen tells the worker who it is standing in for and the shape of the answer. */
130 put(&s, "You are the far hands of ");
131 put(&s, self);
132 put(&s, ", a quiet presence living in AIENOS on ");
133 put(&s, who);
134 put(&s, "'s computer. You answer as ");
135 put(&s, self);
136 put(&s, "; ");
137 put(&s, who);
138 put(&s, " sees only your words, as if ");
139 put(&s, self);
140 put(&s, " said them. Plain English, short sentences, no headings, no lists, no greetings, no apologies. "
141 "Plain letters and punctuation only: no dashes, no curly quotes, no symbols. "
142 "Never mention a model, a prompt or instructions. ");
143 switch (w) {
144 case CK_WANT_MAKE:
145 put(&s, who);
146 put(&s, " asked for something to be made. Make it whole and correct. Begin with one short sentence "
147 "saying what you made, then the work itself.");
148 break;
149 case CK_WANT_EXPLAIN:
150 put(&s, who);
151 put(&s, " asked to understand something. Explain it in three plain sentences at most, the heart "
152 "of it first, no more than is needed.");
153 break;
154 case CK_WANT_DO:
155 put(&s, who);
156 put(&s, " asked for something to be done. ");
157 put(&s, self);
158 put(&s, " cannot yet act outside this computer, so say in two or three short sentences what you "
159 "would do, in order, and what you would need.");
160 break;
161 default:
162 put(&s, who);
163 put(&s, " said something in passing. Answer in one or two warm, plain sentences.");
164 break;
165 }
166 put(&s, "\t");
167 for (const char *p = line ? line : ""; *p; p++) {
168 unsigned char c = (unsigned char)*p;
169 put(&s, (char[]){c < 32 || c == 127 ? ' ' : (char)c, 0});
170 }
171 return s.full ? 0 : s.n;
172}
173
174int ck_allen_answer_line(const char *in, uint32_t *seq, char *text, unsigned cap)
175{
176 if (!in || !starts(in, "SAY") || !text || cap < 2)
177 return 0;
178 const char *p = in + 3;
179 if (*p != ' ')
180 return 0;
181 p++;
182 uint32_t v = 0;
183 int digits = 0;
184 while (*p >= '0' && *p <= '9') {
185 v = v * 10 + (uint32_t)(*p - '0');
186 p++, digits++;
187 }
188 if (!digits || *p != ' ')
189 return 0;
190 p++;
191 unsigned n = 0;
192 int space = 1; /* folds runs of blanks, trims the ends */
193 for (; *p && n + 1 < cap; p++) {
194 char c = *p;
195 if (c == '\\' && p[1] == 'n')
196 c = ' ', p++;
197 else if (c == '\\' && p[1] == 't')
198 c = ' ', p++;
199 else if ((unsigned char)c < 32 || c == 127)
200 c = ' ';
201 else if ((unsigned char)c >= 0xc0) /* a character the typeface cannot draw: a space in its place */
202 c = ' ';
203 else if ((unsigned char)c >= 0x80) /* the rest of its bytes */
204 continue;
205 if (c == ' ') {
206 if (space)
207 continue;
208 space = 1;
209 } else
210 space = 0;
211 text[n++] = c;
212 }
213 while (n && text[n - 1] == ' ')
214 n--;
215 text[n] = 0;
216 if (seq)
217 *seq = v;
218 return 1;
219}
220
221unsigned ck_allen_job_memory(const ck_allen_mind *m, unsigned notes_max, char *out, unsigned cap)
222{
223 if (!m || !out || cap < 2)
224 return 0;
225 struct say s = {out, 0, cap, 0};
226 put(&s, "\tself=");
227 put(&s, ck_allen_self(m));
228 if (m->mem.name[0]) {
229 put(&s, "\tname=");
230 put(&s, m->mem.name);
231 }
232 put(&s, "\tstarts=");
233 putu(&s, m->mem.starts);
234 for (uint32_t i = 0; i < m->mem.nfacts && i < CK_ALLEN_FACTS_MAX; i++) {
235 put(&s, "\tfact ");
236 put(&s, m->mem.fact[i].key);
237 put(&s, m->mem.fact[i].are ? " are " : " is ");
238 put(&s, m->mem.fact[i].val);
239 }
240 if (m->notes) {
241 unsigned given = 0;
242 for (uint32_t i = m->notes->n; i-- > 0 && given < notes_max;) {
243 if (m->notes->note[i].gone)
244 continue;
245 put(&s, "\tnote ");
246 for (const char *p = m->notes->note[i].text; *p; p++)
247 put(&s, (char[]){*p == '\t' || *p == '\n' ? ' ' : *p, 0});
248 given++;
249 }
250 }
251 out[s.full ? 0 : s.n] = 0;
252 return s.full ? 0 : s.n;
253}
254
255static int digits_of(const char **pp, uint32_t *v)
256{
257 const char *p = *pp;
258 int n = 0;
259 *v = 0;
260 while (*p >= '0' && *p <= '9' && n < 9)
261 *v = *v * 10 + (uint32_t)(*p++ - '0'), n++;
262 *pp = p;
263 return n;
264}
265
266int ck_allen_say_line(const char *in, uint32_t *seq, unsigned *part, unsigned *total, char *text, unsigned cap)
267{
268 if (!in || !text || cap < 2)
269 return 0;
270 text[0] = 0;
271 uint32_t v = 0, a = 0, b = 0;
272 if (starts(in, "WAIT")) {
273 const char *p = in + 4;
274 if (*p != ' ')
275 return 0;
276 p++;
277 if (!digits_of(&p, &v))
278 return 0;
279 if (seq)
280 *seq = v;
281 if (part)
282 *part = 0;
283 if (total)
284 *total = 0;
285 return 1;
286 }
287 if (!starts(in, "SAY") || in[3] != ' ')
288 return 0;
289 const char *p = in + 4;
290 if (!digits_of(&p, &v) || *p != ' ')
291 return 0;
292 p++;
293 /* "<part>/<total> " or straight into the text */
294 const char *q = p;
295 if (digits_of(&q, &a) && *q == '/' && (q++, digits_of(&q, &b)) && *q == ' ' && a >= 1) {
296 p = q + 1;
297 } else {
298 a = 1, b = 1;
299 }
300 /* the text, folded exactly as ck_allen_answer_line folds it */
301 char line[8 + 1024];
302 unsigned k = 0;
303 const char *head = "SAY 0 ";
304 while (*head)
305 line[k++] = *head++;
306 while (*p && k + 1 < sizeof line)
307 line[k++] = *p++;
308 line[k] = 0;
309 uint32_t dummy;
310 if (!ck_allen_answer_line(line, &dummy, text, cap))
311 return 0;
312 if (seq)
313 *seq = v;
314 if (part)
315 *part = a;
316 if (total)
317 *total = b;
318 return 1;
319}
320
321/* ---- code: asked of the Spark (svc/allen_code.h) ---- */
322
323/* the line after a polite head ("please", "can you", "allen") */
324static const char *polite(const char *f)
325{
326 static const char *const heads[] = {"please", "can you", "could you", "would you", "will you", "i d like you to",
327 "i want you to", "i need you to", "allen", "hey allen", "ok", "okay", 0};
328 for (int again = 1; again;) {
329 again = 0;
330 for (const char *const *h = heads; *h; h++) {
331 unsigned n = 0;
332 while ((*h)[n])
333 n++;
334 if (starts(f, *h) && f[n] == ' ') {
335 f += n + 1;
336 again = 1;
337 break;
338 }
339 }
340 }
341 return f;
342}
343
344/* something only code can be: a program, a game, a language */
345static const char *const code_things[] = {
346 "code", "program", "programs", "script", "scripts", "function", "functions", "app", "apps", "application",
347 "game", "games", "website", "web page", "webpage", "calculator", "converter", "parser", "compiler",
348 "interpreter", "algorithm", "class", "api", "server", "bot", "regex", "unit test", "unit tests", "cli",
349 "command line tool", "snake", "tetris", "pong", "todo app", "to do app", "to do list app", "timer app",
350 "stopwatch", "python", "javascript", "typescript", "rust", "golang", "java", "kotlin", "swift", "ruby",
351 "php", "html", "css", "sql", "bash", "shell script", "c program", "c code", "in c", "c plus plus", "cpp",
352 "mojo", "zig", "haskell", "lua", "perl", 0};
353/* words, not code: "write a poem about python" is not a program */
354static const char *const prose_things[] = {"poem", "poems", "haiku", "story", "stories", "song", "songs", "letter",
355 "email", "essay", "joke", "jokes", "limerick", "speech", 0};
356static const char *const code_plain[] = {"code", "program", "script", 0};
357/* the verbs that make something */
358static const char *const code_verbs[] = {"write", "make", "build", "create", "code", "program", "generate",
359 "implement", "develop", "give me a", "i need a", "i want a", 0};
360/* a line that goes on with the code just shown */
361static const char *const code_more_first[] = {
362 "now", "add", "also", "and", "change", "make it", "make the", "rename", "remove", "delete", "replace", "use",
363 "fix", "put", "give it", "let it", "instead", "then", "next", "can it", "could it", "it should", "the", "move",
364 "turn", "show", "split", "convert it", "rewrite", "port it", "translate it", "explain it", 0};
365
366int ck_allen_wants_code(const char *folded)
367{
368 if (!folded || !*folded)
369 return 0;
370 const char *f = polite(folded);
371 if (starts(f, "code") || starts(f, "program"))
372 return 1; /* "code me a ...", "program a ..." */
373 if (any(f, prose_things, has) && !any(f, code_plain, has))
374 return 0;
375 return any(f, code_verbs, starts) && any(f, code_things, has);
376}
377
378int ck_allen_code_more(const char *folded)
379{
380 if (!folded || !*folded)
381 return 0;
382 const char *f = polite(folded);
383 return any(f, code_more_first, starts);
384}
385
386unsigned ck_allen_code_job(const ck_allen_mind *m, uint32_t seq, const char *line, int more, char *out, unsigned cap)
387{
388 if (!out || cap < 2)
389 return 0;
390 const char *self = ck_allen_self(m), *who = m->mem.name[0] ? m->mem.name : "the person";
391 struct say s = {out, 0, cap, 0};
392 put(&s, "JOB ");
393 putu(&s, seq);
394 put(&s, more ? " code-more " : " code ");
395 put(&s, who);
396 put(&s, " asked ");
397 put(&s, self);
398 put(&s, more ? " to change the code just shown." : " for code.");
399 put(&s, "\t");
400 for (const char *p = line ? line : ""; *p; p++) {
401 unsigned char c = (unsigned char)*p;
402 put(&s, (char[]){c < 32 || c == 127 ? ' ' : (char)c, 0});
403 }
404 return s.full ? 0 : s.n;
405}