mom: Thread-based tool details, improved README, fixed message ordering

This commit is contained in:
Mario Zechner 2025-11-26 01:48:41 +01:00
parent f53e4fba42
commit 30964e0c33
3 changed files with 224 additions and 5 deletions

View file

@ -160,6 +160,9 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner {
}),
});
// Track pending tool calls to pair args with results
const pendingTools = new Map<string, { toolName: string; args: unknown }>();
// Subscribe to events
agent.subscribe(async (event: AgentEvent) => {
switch (event.type) {
@ -167,6 +170,9 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner {
const args = event.args as { label?: string };
const label = args.label || event.toolName;
// Store args to pair with result later
pendingTools.set(event.toolCallId, { toolName: event.toolName, args: event.args });
// Log to console
console.log(`\n[Tool] ${event.toolName}: ${JSON.stringify(event.args)}`);
@ -179,13 +185,15 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner {
isBot: true,
});
// Show only label to user (italic)
// Show label in main message only
await ctx.respond(`_${label}_`);
break;
}
case "tool_execution_end": {
const resultStr = typeof event.result === "string" ? event.result : JSON.stringify(event.result);
const pending = pendingTools.get(event.toolCallId);
pendingTools.delete(event.toolCallId);
// Log to console
console.log(`[Tool Result] ${event.isError ? "ERROR: " : ""}${truncate(resultStr, 1000)}\n`);
@ -199,7 +207,20 @@ export function createAgentRunner(sandboxConfig: SandboxConfig): AgentRunner {
isBot: true,
});
// Show brief status to user (only on error)
// Post args + result together in thread
const argsStr = pending ? JSON.stringify(pending.args, null, 2) : "(args not found)";
const threadResult = truncate(resultStr, 2000);
await ctx.respondInThread(
`*[${event.toolName}]* ${event.isError ? "❌" : "✓"}\n` +
"```\n" +
argsStr +
"\n```\n" +
"*Result:*\n```\n" +
threadResult +
"\n```",
);
// Show brief error in main message if failed
if (event.isError) {
await ctx.respond(`_Error: ${truncate(resultStr, 200)}_`);
}

View file

@ -16,9 +16,11 @@ export interface SlackMessage {
export interface SlackContext {
message: SlackMessage;
store: ChannelStore;
/** Send a new message */
/** Send/update the main message (accumulates text) */
respond(text: string): Promise<void>;
/** Show/hide typing indicator. If text is provided to respond() after setTyping(true), it updates the typing message instead of posting new. */
/** Post a message in the thread under the main message (for verbose details) */
respondInThread(text: string): Promise<void>;
/** Show/hide typing indicator */
setTyping(isTyping: boolean): Promise<void>;
/** Upload a file to the channel */
uploadFile(filePath: string, title?: string): Promise<void>;
@ -227,6 +229,22 @@ export class MomBot {
await updatePromise;
},
respondInThread: async (threadText: string) => {
// Queue thread posts to maintain order
updatePromise = updatePromise.then(async () => {
if (!messageTs) {
// No main message yet, just skip
return;
}
// Post in thread under the main message
await this.webClient.chat.postMessage({
channel: event.channel,
thread_ts: messageTs,
text: threadText,
});
});
await updatePromise;
},
setTyping: async (isTyping: boolean) => {
if (isTyping && !messageTs) {
// Post initial "thinking" message