!include #1003380 // Chat sclass BotChat { Chat chat; O dialogBot, backend; JTextArea thoughtsArea; *(O dialogBot) { this(dialogBot, null); } *(O _dialogBot, O backend) { 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 { call(dialogBot, "take", E.q(chat.input())); postIt(slurp(dialogBot, "getSingleOutput")); }; updateThoughts(); JFrame frame = showFrame(makeTitle(), hgrid(chat, withTitle("Thoughts", thoughtsArea))); setFrameWidth(frame, 800); chat.requestFocus(); }); } S makeTitle() { ret programTitle(); } void postIt(final L l) { 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; Runnable l8r = r { awt { postIt(subList(l, _i)); } }; if ((bool) call(backend, "action", e.state, l8r)) break; } } updateThoughts(); } void chatAppend(S s) { if (s.startsWith("[") && matchStart("bot pauses", s)) ret; chat.append(s); } 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); } }