!752 !include #1002943 // Pivo Logic (include) static S logName = "log"; static long requestID, logLength; static L showableFiles = litlist(logName); // ... static new L history; static class Entry { long time; S cookie; S userSaid, botSaid; long logID; } static new L bots; static class Bot { // n = where new stuff starts void learnFrom(L history, int n) {} S answer(S question) { ret null; } void load(LoadEnv env) {} void save(LoadEnv env) {} } p { pivoLoad(); load("requestID"); L log = scanMapLog(logName); logLength += l(log); addToHistory(makeHistory(log)); } static S rawLink(S pageName) { ret "/" + parseSnippetID(getProgramID()) + "/raw/" + pageName; } static long newRequestID() { long id = ++requestID; save("requestID"); ret id; } static S html(S uri, Map params) { if (eq(uri, "/log")) ret htable(scanMapLog(logName)); if (eq(uri, "/has-speech")) { O cookieHandler = call(getMainBot(), "getCookies"); S cookieMD5 = md5((S) call(cookieHandler, "read", "cookie")); S language = getString(params, "language"); long requestID = newRequestID(); S s = params.get("q"); Map logData = litmap("ip", getClientIP(), "thread", threadID(), "cookie", cookieMD5, "language", language, "heard", s); log(mapPlus(logData, "starting", now())); ((ThreadLocal) get(getMainBot(), "attn")).set(true); pivoMode.set(true); S text = //askSelf(s); callStaticAnswerMethod(mc(), s); if (empty(text)) { log(mapPlus(logData, "done", now(), "saying", "-")); ret ""; } S voice = eq(language, "6") ? "William" : "Alex"; S soundURL = getSoundURLCereproc(text, voice); S html = [[ ]]; html = html.replace("SRC", htmlencode(soundURL)); log(mapPlus(logData, "done", now(), "saying", text)); ret "Speech (" + voice + "): " + htmlencode(text) + "
" + html; } if (eq(uri, "/empty")) { ret ""; } if (eq(uri, "/upper")) { S html = [=[ TinyBrain Speech Demo

Click on the microphone icon and begin speaking.

Speak after the bleep.

No speech was detected

Click the "Allow" button.

Permission to use microphone was denied.

Permission to use microphone is blocked. To change, go to chrome://settings/contentExceptions#media-stream

Web Speech API is not supported by this browser. Upgrade to Chrome version 25 or later.

     
]=]; ret render(html); } // frame set ret render([[ TITLE ]]); } static S render(S html) { ret html.replace("LINK/", rawLink("")).replace("TITLE", getProgramName()); } static L makeHistory(L log) { new L history; for (Map map : log) { S in = cast map.get("heard"); S out = cast map.get("saying"); if (nempty(in) && nempty(out)) { new Entry e; e.userSaid = in; e.botSaid = out; e.time = toLong(map.get("done")); e.cookie = (S) map.get("cookie"); e.logID = toLong(map.get("_id")); history.add(e); } } ret history; } static void addToHistory(L l) { int n = l(history); history.addAll(l); learnFrom(n); } static void learnFrom(int n) { for (Bot bot : bots) pcall { bot.learnFrom(history, n); } } static void log(Map map) { map.put("_id", logLength+1); logMap(logName, map); ++logLength; addToHistory(makeHistory(litlist(map))); }