import type { ChoreFacts, ConversationEnded, CreatureHost, CreatureLedger, CreatureLedgerEntry, CreatureLedgerPort, CreatureOrigin, LedgerConversationEnding, LedgerConversationTag, QuestionAnswer, RoutineService, ThreadKind, ThreadReport, } from "@keywork/engine"; import type { AskQueue, EventLog, WirePayload } from "@keywork/server"; import { toError } from "@keywork/shared"; import { firstSentenceOf } from "./serve-creatures.ts"; export interface CreatureSessionFacts { creature: string; directory: string; parent: string | null; } export function followAsks( asks: Pick, ledger: Pick, sessionOf: (id: string) => CreatureSessionFacts | undefined, ): () => void { return asks.follow({ asked: (ask) => { const facts = sessionOf(ask.sessionId); if (facts === undefined) return; ledger.asked({ ...facts, session: ask.sessionId, callId: ask.callId, tool: ask.tool, arguments: ask.arguments, }); }, settled: (ask, ending) => ledger.askSettled(ask.sessionId, ask.callId, ending), }); } export function followLedgerEvents( log: Pick, ledger: Pick< CreatureLedger, "choreFinished" | "choreNeeds" | "sessionRemoved" | "sessionRestored" >, notice: (line: string) => void, ): () => void { return log.subscribe(({ type, payload }) => { switch (type) { case "routine.finished": ledger.choreFinished((payload as WirePayload<"routine.finished">).run); return; case "routine.needs": { const needs = payload as WirePayload<"routine.needs">; ledger.choreNeeds(needs).catch((cause: unknown) => notice(unheardNeeds(needs, cause))); return; } case "session.removed": ledger.sessionRemoved((payload as WirePayload<"session.removed">).sessionId); return; case "session.restored": ledger.sessionRestored((payload as WirePayload<"session.restored">).sessionId); return; } }); } export function ledgeredCreatures>( creatures: Host, ledger: Pick, directory: string, ): Host { return { ...creatures, openThread: async (creature: string, kind: ThreadKind) => { const session = await creatures.openThread(creature, kind); if (kind === "thread") { ledger.threadOpened({ creature, session, directory, title: untitledThread, about: { kind: "thread" }, }); } return session; }, finish: async (creature: string, thread: string, report: ThreadReport) => { const outcome = await creatures.finish(creature, thread, report); ledger.threadReported(thread, report); return outcome; }, }; } export function choreFactsOver( routines: () => Pick | undefined, workspace: string, ): (creature: string, routineId: string) => Promise { return async (creature, routineId) => { const view = await routines()?.read(creature, routineId); if (view === undefined || typeof view === "string") return undefined; if (view.routine === null || view.grants === null) return undefined; return { title: view.routine.title, posture: view.grants.posture, accepted: view.grants.accepted, directory: workspace, }; }; } const untitledThread = "untitled thread"; function unheardNeeds(needs: WirePayload<"routine.needs">, cause: unknown): string { return `the ledger missed what chore ${needs.routineId} of ${needs.creature} needs: ${toError(cause).message}`; } export interface LedgerConversationSessions { open(creature: string): Promise; begin(session: string, about: LedgerConversationTag, title: string): Promise; frame(session: string, text: string, origin: CreatureOrigin): Promise; lastReply(session: string): Promise; end(session: string, ended: ConversationEnded): Promise; note(creature: string, text: string, origin: CreatureOrigin): Promise; discard(creature: string, session: string): Promise; threadsChanged(creature: string): void; } export function conversingLedger( ledger: CreatureLedger, sessions: Pick, ): CreatureLedgerPort { return { read: (creature) => ledger.read(creature), seen: (creature, id) => ledger.seen(creature, id), subscribe: (listener) => ledger.subscribe(listener), converse: (creature, id) => ledger.conversationAbout(creature, id, async (_creature, entry) => { const session = await sessions.open(creature); try { await sessions.begin(session, { creature, entry: entry.id }, conversationTitleOf(entry)); await sessions.frame(session, frameOf(entry), { kind: "creature", creature, thread: session, }); } catch (cause) { await sessions.discard(creature, session).catch(() => undefined); throw cause; } return session; }), closeConversation: (creature, id) => ledger.endConversation(creature, id), }; } export function conversationEnder( sessions: Omit, notice: (line: string) => void, ): (ending: LedgerConversationEnding) => Promise { return (ending) => endConversation(sessions, ending).catch((cause: unknown) => notice( `the conversation about ledger entry ${ending.entry.id} of ${ending.creature} did not end cleanly: ${toError(cause).message}`, ), ); } async function endConversation( sessions: Omit, { creature, entry, conversation }: LedgerConversationEnding, ): Promise { const { session, endedBy, endedAt } = conversation; const by = endedBy ?? "keeper"; const decided = by === "answer" ? answeredWords(entry) : closedWords(await sessions.lastReply(session)); await sessions.end(session, { at: endedAt ?? new Date().toISOString(), by, decided }); const line = `The conversation about "${oneLine(entry.title)}" ended: ${decided}.`; await sessions.note(creature, line, { kind: "creature", creature, thread: session }); sessions.threadsChanged(creature); } function conversationTitleOf(entry: CreatureLedgerEntry): string { return `About: ${oneLine(entry.title)}`; } function frameOf(entry: CreatureLedgerEntry): string { const where = entry.session === null ? "" : ` It belongs to session ${entry.session}.`; const ask = entry.kind === "question" ? "Tell them in a few sentences what it is for and what happens either way. You cannot answer it here: they answer it where it was asked, and this conversation ends when they do." : "Tell them in a few sentences where it stands and what you need from them. This conversation ends when they close it."; return `The keeper opened a short conversation with you about one row of your ledger, the ${entry.kind} "${oneLine(entry.title)}", which is ${entry.state}.${where} ${ask} Nothing said here grants or allows anything.`; } const answers: Readonly> = { granted: "the keeper allowed it", denied: "the keeper said no", lapsed: "nobody answered it in time", }; function answeredWords(entry: CreatureLedgerEntry): string { return entry.answer === null ? "the question was answered" : answers[entry.answer]; } function closedWords(lastReply: string): string { const said = firstSentenceOf(lastReply); return said === "" ? "the keeper closed it" : `the keeper closed it after "${oneLine(said)}"`; } function oneLine(text: string): string { return text.replace(/\s+/g, " ").trim(); }