!7 concept AToken { S token; long lastSeen; } concept ALine { S token; S line, language; } concept AEvent { S token; S what; } concept ALineToSend > ALine {} concept AIncomingLine > ALine { bool typed; } concept AAction > ALine { bool verified; } cmodule AssistantWebServer > DynPrintLog { int httpPort = 8083, interval = 200, longPollTimeout = 60000; S token, line; start-thread { importantDB(); print("Tokens:"); printIndentLines(collect token(sortByFieldDesc lastSeen(list(AToken)))); dm_serveHttpFromFunction(httpPort, func(S uri, SS params) enter { S token = params.get('token); if (l(token) == 24) { bumpToken(token); S shortenedToken = assistant_shortenToken(token); if (eq(uri, "/poll")) { ret serveLongPoll(longPollTimeout, interval, func { temp enter(); S text = withDBLock(func -> S { L l = conceptsWhere(ALineToSend, +token); if (empty(l)) null; deleteConcepts(l); S text = lines_rtrim(collect line(sortConceptsByID(l))); ret text; }); if (text != null) printWithTime("Served output to " + token + ": " + text); ret text; }); } if (eq(uri, "/heard")) { S line = params.get('line), language = params.get('language); bool typed = eq("1", params.get('typed)); if (nempty(line)) { cnew(AIncomingLine, +token, +line, +typed, +language); print("Heard from " + token + ": " + line); vmBus_send phoneCatHeard(litmap(module := dm_moduleID(), +token, +line, +typed, +language)); } ret "OK"; } if (eq(uri, "/event")) { S what = params.get('what); if (nempty(what)) { cnew(AEvent, +token, +what); print("Event from " + shortenedToken + ": " + what); vmBus_send phoneCatEvent(litmap(module := dm_moduleID(), +token, +what)); ret "OK"; } } } // end of have-token block ret serveText("hä"); }); } void bumpToken(S token) { cset(uniq_sync(AToken, +token), lastSeen := now()); } visual centerAndSouth(super, westCenterAndEastWithMargins( withLabel("Token:", jMinWidth(150, dm_textField('token))), withLabel("Text:", dm_textField('line)), jbutton("Send", rThread guiSendLine))); void guiSendLine enter { dm_trimFields('line, 'token); if (l(token) < 24) { L tokens = filter(list(AToken), t -> startsWithIC(t.token, token)); if (l(tokens) > 1) ret with infoBox("Multiple tokens found, please give more chars"); if (l(tokens) == 1) setField(token := first(tokens).token); } if (l(token) != 24) ret with infoBox("Bad token"); if (empty(line)) ret; cnew(ALineToSend, +token, +line); print("Line sent"); } // API void sendToUser(S token, S line) { cnew(ALineToSend, +token, +line); } }