# uuid=53cd37cc-4563-47bf-b278-832e09df45b3
if version\0 < 1:
from llm import agent
type User:
var name
var context
var family
var default_channel
one have_met
type Family:
val uuid
type Channel:
var active_thread
type Thread:
val channel
val messages
type Message:
val from
val when
val text
val channel
one thread
val in_reply_to
families = [:]
def format_message(m):
return "[{format_time(m.when)}] {m.from.name}: {sindent(m.text, ' '*(30+slen(m.from.name)))}"
def format_messages(l):
return joinlines([format_message(m) for m in l])
def get_family(uuid):
if not uuid?:
return none
if (fam = families[uuid])?:
return fam
print("DEBUG: Creating new family {uuid}")
families[uuid] = fam = Family(uuid=uuid)
return fam
import telegram
def send_message_to_channel(chan, text):
return telegram.send_message_to_telegram_channel(chan, text)
def send_message_to_thread(thread, text):
m = send_message_to_channel(thread.channel, text)
m.thread = thread
append(thread.messages, m)
{|
| Wait for one or more (usually just one) message to come in on a thread.
|}
def wait_for_new_messages_on_thread(thread):
q = thread.messages
ref = length(q)
wait until length(q) > ref
{|
| Dispatch a Message to a Thread.
|}
def dispatch_message_to_thread(m, thread):
print("CHANNELS: Dispatching message {m} to thread {thread}")
m.thread = thread
append(thread.messages, m)
{|
| Dispatch a Message to the Channel it belongs to.
|}
def dispatch_message_to_channel(m, chan):
print("CHANNELS: Dispatching message {m} to channel {chan}")
if (thread = m.in_reply_to.thread)?:
FIXME
print("CHANNELS: Routing to direct-reply thread {thread}")
dispatch_message_to_thread(m, thread)
else if (thread = chan.active_thread)?:
print("CHANNELS: Routing to currently active thread {thread}")
dispatch_message_to_thread(m, thread)
else:
print("CHANNELS: Creating new thread")
thread = m.thread = chan.active_thread = Thread(channel=chan, messages=[m])
spawn dispatch_new_thread(thread, m.from)
{|
| Decide what the goal of a new thread is, and invoke that goal.
|}
def dispatch_new_thread(thread, user):
import skills NOTE
msg = $"We need to dispatch a new message to an appropriate goal handler.
About the user ({user.name}):
{user.context}
Which goal should we direct this new user thread to?
User thread:
{format_messages(thread.messages)}
Available goals:
{joinlines(["[{i}] {goal.summary}" for i:goal in skills.things_I_can_do])}
If one of those goals sounds applicable, reply with action=<the number of the applicable goal>
If none of those goals sounds applicable reply with action=-1
"$
option = agent(msg).action
if option < 0 or not (goal = skills.things_I_can_do[option])?:
send_message_to_thread(thread, "I have no response to that.")
thread.channel.active_thread = none
return
spawn:
goal(thread, user)
print("CHANNELS: {goal} completed. Releasing channel.")
thread.channel.active_thread = none