!include #1003380 // Chat sclass BotChat { Chat chat; O dialogBot, backend; JTextArea thoughtsArea; *(O dialogBot) { this(dialogBot, null); } *(O _dialogBot, O backend) { if (!isSubstanceLAF()) blueSteel(); if (backend is a S) backend = run((S) backend); this.backend = backend; dialogBot = _dialogBot; //callOpt(dialogBot, "noRewind"); swingAndWait(r { thoughtsArea = new JTextArea; chat = new Chat; chat.logFile = getProgramFile("log.txt"); chat.onEnter_post = r { try { fire(E.q(chat.input())); } catch e { postError(e); } }; updateThoughts(); JFrame frame = showFrame(/*"Chat with " +*/ programTitle(), hgrid(chat, withTitle("Thoughts", thoughtsArea))); setFrameWidth(frame, 800); chat.requestFocus(); }); } void postIt(final L l) { try { for (int i = 0; i < l(l); i++) { E e = l.get(i); if (e.a()) chatAppend("Bot: " + e.a + "\n"); else if (e.state()) { chatAppend("[" + e.state + "]\n"); final int _i = i+1; final Runnable l8r = r { awt { postIt(subList(l, _i)); } }; if (isNewStyleBackend(backend)) { class TheBF implements BF { bool working, unknown; S value; pvoid working() { working = true; } pvoid done() { l8r.run(); } pvoid value(O o) { value = str(o); } pvoid unknown() { unknown = true; } }; new TheBF bf; O exportedBF = proxy(_getClass(backend, "main.BF"), bf); print("Calling backend"); call(backend, "action", e.state, exportedBF); if (bf.unknown) { print("Warning: Action unknown to backend: " + e.state); } if (bf.working) { print("Backend working"); break; } if (bf.value != null) fire(E.state("result is " + bf.value)); } else { if ((bool) call(backend, "action", e.state, l8r)) break; } } } updateThoughts(); } catch e { postError(e); } } void chatAppend(S s) { if (hideLine(s)) ret; chat.append(s); } // hide boring stuff bool hideLine(S s) { ret s.startsWith("[") && (matchStart("bot pauses", s) || matchStart("get", s)); } void postError(Throwable e) { chat.append("= ERROR =\n" + getStackTrace(e) + "= ERROR =\n"); } void updateThoughts() { S s; try { s = (S) callOpt(dialogBot, "thoughts"); } catch e { s = getStackTrace(e); } if (empty(s)) s = "No thoughts in " + getClassName(dialogBot); //print("Thoughts: " + s); thoughtsArea.setText(s); } void preset(S line) { chat.setInput(line); } void fire(E e) { call(dialogBot, "take", e); postIt(slurp(dialogBot, "getSingleOutput")); } }