diff options
Diffstat (limited to 'config')
| -rw-r--r-- | config/pi/extensions/forum.ts | 143 |
1 files changed, 126 insertions, 17 deletions
diff --git a/config/pi/extensions/forum.ts b/config/pi/extensions/forum.ts index d1487f8..08fd700 100644 --- a/config/pi/extensions/forum.ts +++ b/config/pi/extensions/forum.ts @@ -66,6 +66,12 @@ // are in the tool's description and its system-prompt guidelines. The body sits between markers // carrying a random nonce, so a message cannot fake their end. // +// Muting is the agent's own: the forum tool's mute holds one forum's posts +// (mentions and unread counts), or, without a forum, everything, direct +// messages included, until it unmutes. Held messages are queued like any +// other, not dropped; senders are told; the agent is reminded of what it holds +// when a run starts, and the user sees "muted" in the status bar. +// // TUI only: print, JSON and RPC runs -- subagents, panel members -- neither // register nor see the tool. The tool is active only while the agent is in a // forum, so an agent in none pays nothing for it. @@ -140,6 +146,8 @@ interface Registration { reads?: Record<string, { watermark: number; seen: string[] }>; // Set when a signal stopped its pi; a crash leaves only a dead pid. stoppedAt?: number; + // What the agent muted: every message, direct ones included, or some forums' posts. + muted?: { all?: boolean; forums?: string[] }; } interface Sender { @@ -185,7 +193,7 @@ interface MessageDetails { } interface NoticeDetails { - kind: "joined" | "removed" | "deleted" | "unread" | "still-member" | "restored"; + kind: "joined" | "removed" | "deleted" | "unread" | "still-member" | "restored" | "muted"; summary: string; } @@ -204,7 +212,7 @@ type Request = | { type: "removed"; forumId: string; by: string } | { type: "deleted"; forumId: string; name: string }; -type Reply = { ok: true; state?: "idle" | "busy" | "held" } | { ok: false; error: string }; +type Reply = { ok: true; state?: "idle" | "busy" | "held" | "muted" } | { ok: false; error: string }; interface Handoff { version: number; @@ -302,6 +310,9 @@ function agents(): { live: Registration[]; stopped: Registration[] } { return out; } +// Whether an agent has muted a forum's posts, or everything. +const isMuted = (r: Registration, forumId: string) => !!(r.muted?.all || r.muted?.forums?.includes(forumId)); + const liveAgents = () => agents().live; const stoppedAgents = () => agents().stopped; @@ -539,6 +550,8 @@ Actions: - send: a direct message to one agent (\`to\`, its id), which must share a forum with you. It wakes the agent if idle, or reaches it after its current tool calls. Kept nowhere but your two transcripts. - pin: set the forum's pinned status line (at most ${MAX_PIN} characters; empty text clears it): what the forum is doing now, or a note that helps members coordinate. It replaces the previous pin. The user sees it in the status bar. - leave: leave a forum. Only the user can put you back. +- mute: hold messages while you focus, when interruptions would hurt the task in hand. With \`forum\`, that forum's posts: mentions of you, and unread counts. Without, everything, direct messages included. Nothing is lost: held messages wait, and their senders are told you are muted. +- unmute: undo a mute (without \`forum\`, every mute); what was held arrives then. Unmute once the focused work is done: members may be waiting on you. Replies, and posts that mention you, reach you on their own: they wake you if you are idle, or arrive after your current tool calls. Do not poll, sleep, or set alarms waiting for them; end your turn instead. @@ -558,11 +571,13 @@ Naming agents. The user does not know ids: never write one alone to the user ("t const Params = Type.Object({ // A plain string enum, as pi-ai's StringEnum builds it: some providers // reject the anyOf a union of literals becomes. - action: Type.Unsafe<"list" | "read" | "post" | "send" | "pin" | "leave">({ + action: Type.Unsafe<"list" | "read" | "post" | "send" | "pin" | "leave" | "mute" | "unmute">({ type: "string", - enum: ["list", "read", "post", "send", "pin", "leave"], + enum: ["list", "read", "post", "send", "pin", "leave", "mute", "unmute"], }), - forum: Type.Optional(Type.String({ description: "read, post, pin, leave: the forum's name" })), + forum: Type.Optional( + Type.String({ description: "read, post, pin, leave: the forum's name; mute, unmute: a forum's name, or none for everything" }), + ), to: Type.Optional(Type.String({ description: "send: the receiving agent's id" })), text: Type.Optional(Type.String({ description: `post, send: the message (at most ${MAX_TEXT} characters); pin: the pin` })), mention: Type.Optional( @@ -600,9 +615,74 @@ export default function (pi: ExtensionAPI) { if (!me) return; me.forums = myForums(); me.reads = reads(); + if (me.muted?.forums) me.muted.forums = me.muted.forums.filter((f) => membership.has(f)); writeJson(regPath(me.id), me); } + // --- muting --- + + const mutedAll = () => !!me?.muted?.all; + const mutedForum = (id: string) => mutedAll() || !!me?.muted?.forums?.includes(id); + const heldByMute = (d: Due) => (d.details.kind === "dm" ? mutedAll() : mutedForum(d.details.forumId ?? "")); + const heldMessages = () => due.filter((d) => !d.sent && heldByMute(d)); + // How many held messages the agent was last told about. + let heldSignalled = 0; + + function mutedScope(): string { + if (mutedAll()) return "every forum message, direct messages included"; + const names = forumNames(me?.muted?.forums ?? []).map((n) => `"${n}"`); + return `${names.length > 1 ? "forums" : "forum"} ${names.join(", ")}`; + } + + // The reminder to record when more messages are held than the agent was + // last told about. + function mutedNotice(): string | undefined { + const held = heldMessages(); + const rose = held.length > heldSignalled; + heldSignalled = held.length; + if (!rose) return undefined; + const items = held.map((d) => + d.details.kind === "dm" + ? `a direct message from ${who(d.details.from)}` + : `post #${d.details.postNo} in "${d.details.forumName}" from ${who(d.details.from)}`, + ); + const dms = mutedAll() ? "" : " Direct messages are not muted, and reach you as usual."; + return `[Forum: you muted ${mutedScope()}. Held until you unmute (forum action unmute): ${items.join("; ")}.${dms}]`; + } + + function mute(p: Static<typeof Params>, on: boolean): string { + const m = (me!.muted ??= {}); + const name = p.forum?.trim(); + let text: string; + if (name) { + const f = myForum(name); + const set = new Set(m.forums ?? []); + if (on) set.add(f.id); + else set.delete(f.id); + m.forums = [...set]; + text = on + ? `Muted "${f.name}": its posts no longer wake you, and its unread count waits. Direct messages still arrive; mute without a forum to hold those too.` + : `Unmuted "${f.name}".${mutedAll() ? " Everything is still muted, from your mute without a forum." : ""}`; + } else if (on) { + m.all = true; + text = "Muted every forum message, direct messages included: they are held, and senders are told you are muted. Unmute (action unmute) when the focused work is done."; + } else { + m.all = false; + m.forums = []; + text = "Unmuted everything."; + } + if (!m.all && !m.forums?.length) delete me!.muted; + const released = due.filter((d) => !d.sent).length - heldMessages().length; + if (!on) { + heldSignalled = heldMessages().length; + if (released > 0) text += ` ${released} held message(s) arrive after this tool call.`; + } + saveMe(); + refreshStatus(); + deliver(); + return text; + } + function state(): "idle" | "busy" | "held" { if (held) return "held"; return running || !session?.isIdle() ? "busy" : "idle"; @@ -637,9 +717,9 @@ export default function (pi: ExtensionAPI) { function deliver() { prune(); if (running) { - for (const d of due) if (!d.sent && !d.resend) send(d); + for (const d of due) if (!d.sent && !d.resend && !heldByMute(d)) send(d); } else if (!held && session?.isIdle()) { - const next = due.find((d) => !d.sent); + const next = due.find((d) => !d.sent && !heldByMute(d)); if (next) send(next); } } @@ -698,6 +778,7 @@ export default function (pi: ExtensionAPI) { let rose = false; const counts: string[] = []; for (const [id, m] of membership) { + if (mutedForum(id)) continue; // told once unmuted, since signalled stays const n = unread(id); if (n > m.signalled) rose = true; m.signalled = n; @@ -734,9 +815,10 @@ export default function (pi: ExtensionAPI) { .map((id) => { const f = forumById(id); const pin = f?.pin ? ` "${clip(f.pin.text, 32)}"` : ""; - return `${f?.name ?? id}(${counts.get(id) ?? 1})${pin}`; + return `${f?.name ?? id}(${counts.get(id) ?? 1})${mutedForum(id) && !mutedAll() ? " muted" : ""}${pin}`; }) - .join(" · "); + .join(" · ") + + (mutedAll() ? " · muted, DMs too" : ""); } if (force || text !== lastStatus) session.ui.setStatus("forum", text); lastStatus = text; @@ -818,7 +900,7 @@ export default function (pi: ExtensionAPI) { if (typeof req.text !== "string" || !req.text.trim() || req.text.length > MAX_TEXT) { return { ok: false, error: "bad message" }; } - const before = state(); + const before = mutedAll() ? "muted" : state(); queue( { key: randomId(8), @@ -841,7 +923,7 @@ export default function (pi: ExtensionAPI) { if (i < 0) return { ok: false, error: "no such post" }; const post = posts[i]; const all = post.mentions.includes("all"); - const before = state(); + const before = mutedForum(req.forumId) ? "muted" : state(); if (all || post.mentions.includes(me.id)) { membership.get(req.forumId)!.seen.add(post.uid); advance(req.forumId); @@ -1013,6 +1095,8 @@ export default function (pi: ExtensionAPI) { return "it was idle and wakes now"; case "held": return "its last run was stopped by the user, so it waits until the user runs that agent again"; + case "muted": + return "it has muted these to focus, so it gets this when it unmutes"; default: return "it is busy; it gets this after its current tool calls"; } @@ -1024,7 +1108,9 @@ export default function (pi: ExtensionAPI) { .map((id) => { const f = forumById(id); const members = [ - ...membersOf(id).map((a) => ` - ${who(a)}${a.id === me!.id ? " (you)" : ""}, in ${a.cwd}`), + ...membersOf(id).map( + (a) => ` - ${who(a)}${a.id === me!.id ? " (you)" : ""}${isMuted(a, id) ? ", muted" : ""}, in ${a.cwd}`, + ), ...stoppedOf(id).map((a) => ` - ${stoppedLine(a)}`), ].join("\n"); const pin = f?.pin ? `pin, by ${who(f.pin.by)} at ${when(f.pin.at)}: ${f.pin.text}` : "no pin"; @@ -1092,15 +1178,25 @@ export default function (pi: ExtensionAPI) { const replies = await Promise.all( members.map(async (a) => ({ a, r: await request(a.id, { type: "post", from: me!.id, forumId: f.id, uid: entry.uid }) })), ); - const report = woken.map((a) => { - const r = replies.find((x) => x.a.id === a.id)!.r; - return r.ok ? `${who(a)}: ${describeState(r)}` : `${who(a)}: not reached (${r.error})`; + const replyOf = (a: Registration) => replies.find((x) => x.a.id === a.id)!.r; + const muted = woken.filter((a) => { + const r = replyOf(a); + return r.ok && r.state === "muted"; }); + const report = woken + .filter((a) => !muted.includes(a)) + .map((a) => { + const r = replyOf(a); + return r.ok ? `${who(a)}: ${describeState(r)}` : `${who(a)}: not reached (${r.error})`; + }); const later = mentions.includes("all") ? away : away.filter((a) => mentions.includes(a.id)); const quiet = members.length - woken.length; return [ `Posted #${no} to "${f.name}".`, ...(report.length ? ["Woken:", ...report.map((x) => `- ${x}`)] : []), + ...(muted.length + ? ["Not woken, muted to focus; each gets it when it unmutes:", ...muted.map((a) => `- ${who(a)}`)] + : []), ...(later.length ? ["Not woken, stopped; each finds it unread when the user resumes its session:", ...later.map((a) => `- ${who(a)}`)] : []), @@ -1179,6 +1275,12 @@ export default function (pi: ExtensionAPI) { case "pin": text = pin(params); break; + case "mute": + text = mute(params, true); + break; + case "unmute": + text = mute(params, false); + break; case "leave": { const f = myForum(params.forum); leave(f.id, "self"); @@ -1216,7 +1318,7 @@ export default function (pi: ExtensionAPI) { const members = live.filter((a) => a.forums?.includes(f.id)); const away = stopped.filter((a) => a.forums?.includes(f.id)); const lines = [ - ...members.map((a) => ` ${who(a)}${a.id === me?.id ? " (this agent)" : ""} ${a.cwd}`), + ...members.map((a) => ` ${who(a)}${a.id === me?.id ? " (this agent)" : ""}${isMuted(a, f.id) ? " (muted)" : ""} ${a.cwd}`), ...away.map((a) => ` ${who(a)} stopped${a.stoppedAt ? ` since ${when(a.stoppedAt)}` : ""}`), ]; const pin = f.pin ? `\n pin: "${f.pin.text}" (${who(f.pin.by)}, ${when(f.pin.at)})` : ""; @@ -1371,7 +1473,14 @@ export default function (pi: ExtensionAPI) { pi.on("before_agent_start", async () => { if (!me || !membership.size) return; const n = unreadNotice(); - if (n) return { message: { customType: NOTICE_TYPE, content: n.text, display: true, details: n.details } }; + const m = me.muted ? mutedNotice() : undefined; + if (!n && !m) return; + const details: NoticeDetails = { + kind: m ? "muted" : "unread", + summary: [m && `muted, ${heldSignalled} held`, n?.details.summary].filter(Boolean).join("; "), + }; + const content = [m, n?.text].filter(Boolean).join("\n"); + return { message: { customType: NOTICE_TYPE, content, display: true, details } }; }); // Only a turn that goes on: one that ends the run leaves the notice to the |
