// Dragon Dialog - part of the Dragon Library for a Dragon AI Connect agent. Put it next to dragon_api.lsl. // The Library is a set of small scripts, one per kind of ability, each with its own 64 KB: add only the ones // you want. No logic and no secrets: the server decides, this script shows and asks. Guide: https://dragonaiconnect.com/docs/runtime/library // // Hands: show_menu, text_box (the answer goes back to the AI as their next message), im (by key or by // name), set_text (floating sign), open_url. // 9110 <- API actions; 9119 -> API a menu / text box answer; 9112 hello -> API; 9113 <- API integer gCh; list gLookups; // strided [dataserver key, text, name] for IMs by name hello(string why) { string m = "dragon-dialog, " + (string)(llGetFreeMemory() / 1024) + " KB free"; if (why == "status") { llOwnerSay(m); return; } llMessageLinked(LINK_THIS, 9112, m, NULL_KEY); } im(key to, string name, string t) { if (to) { llInstantMessage(to, t); return; } if (name == "") return; name = llDumpList2String(llParseString2List(llToLower(llStringTrim(name, STRING_TRIM)), [" "], []), "."); if (llGetSubString(name, -9, -1) == ".resident") name = llGetSubString(name, 0, -10); gLookups = llList2List(gLookups + [llRequestUserKey(name), t, name], -15, -1); // answer in dataserver() } perform(string a) { string w = llJsonGetValue(a, ["do"]); string t = llJsonGetValue(a, ["text"]); key to = (key)llJsonGetValue(a, ["to"]); if (w == "show_menu") { if (to) llDialog(to, llGetSubString(llJsonGetValue(a, ["prompt"]), 0, 500), llJson2List(llJsonGetValue(a, ["buttons"])), gCh); } else if (w == "text_box") { if (to) llTextBox(to, llGetSubString(llJsonGetValue(a, ["prompt"]), 0, 500), gCh); } else if (w == "im") im(to, llJsonGetValue(a, ["to_name"]), t); else if (w == "set_text") { // the owner's sign: kept where the status can put it back llLinksetDataWrite("slm_sign", t); // "" deletes it if (t == "") llSetText(" ", ZERO_VECTOR, 0.0); else llSetText(t, <1.0, 1.0, 1.0>, 1.0); } else if (w == "open_url") { if (to) llLoadURL(to, llGetSubString(t, 0, 200), llJsonGetValue(a, ["url"])); } } default { state_entry() { gCh = -2000000 - (integer)llFrand(1000000000.0); llListen(gCh, "", NULL_KEY, ""); hello(""); } on_rez(integer p) { llResetScript(); } changed(integer c) { if (c & CHANGED_OWNER) llResetScript(); } listen(integer ch, string name, key id, string msg) // a menu button or a text box answer { llMessageLinked(LINK_THIS, 9119, llList2Json(JSON_OBJECT, ["prompt", msg, "speaker", id, "speaker_name", name]), NULL_KEY); } dataserver(key q, string data) { integer i = llListFindList(gLookups, [q]); if (i == -1) return; string t = llList2String(gLookups, i + 1); string who = llList2String(gLookups, i + 2); gLookups = llDeleteSubList(gLookups, i, i + 2); if ((key)data) llInstantMessage((key)data, t); else llMessageLinked(LINK_THIS, 9118, llList2Json(JSON_OBJECT, // the agent hears it failed ["type", "dialog", "event", "im_failed", "t", who]), NULL_KEY); } link_message(integer sender, integer num, string str, key id) { if (num == 9113) { hello(str); return; } if (num != 9110) return; integer i; string a = llJsonGetValue(str, [0]); while (a != JSON_INVALID && i < 8) { perform(a); a = llJsonGetValue(str, [++i]); } } }