feat: multi-agent progress tracking with per-subagent display

Track each subagent separately via agentId from SDK task events.
Single subagent shows detailed view with activity sub-lines,
multiple subagents show compact one-line-each format.
This commit is contained in:
Eyejoker
2026-03-23 19:46:08 +09:00
parent 8c98ad9faf
commit 21a6be84b4
3 changed files with 147 additions and 31 deletions

View File

@@ -33,6 +33,9 @@ interface ContainerInput {
interface ContainerOutput {
status: 'success' | 'error';
phase?: 'progress' | 'final' | 'tool-activity' | 'intermediate';
agentId?: string;
agentLabel?: string;
agentDone?: boolean;
result: string | null;
newSessionId?: string;
error?: string;
@@ -561,10 +564,21 @@ async function runQuery(
if (message.type === 'system' && (message as { subtype?: string }).subtype === 'task_notification') {
const tn = message as { task_id: string; status: string; summary: string };
log(`Task notification: task=${tn.task_id} status=${tn.status} summary=${tn.summary}`);
if (tn.status === 'completed' || tn.status === 'error' || tn.status === 'cancelled') {
writeOutput({
status: 'success',
phase: 'progress',
agentId: tn.task_id,
agentDone: true,
result: tn.summary || null,
newSessionId,
});
}
}
if (message.type === 'system' && (message as { subtype?: string }).subtype === 'task_progress') {
const tp = message as Record<string, unknown>;
const taskId = typeof tp.task_id === 'string' ? tp.task_id : undefined;
const summary = typeof tp.summary === 'string' ? tp.summary : '';
const description = typeof tp.description === 'string' ? tp.description : '';
if (description) {
@@ -572,6 +586,7 @@ async function runQuery(
status: 'success',
phase: 'tool-activity',
result: description,
agentId: taskId,
newSessionId,
});
}
@@ -586,6 +601,8 @@ async function runQuery(
status: 'success',
phase: 'progress',
result: `🔄 ${desc}`,
agentId: ts.task_id,
agentLabel: desc,
newSessionId,
});
}

View File

@@ -42,6 +42,9 @@ export interface AgentOutput {
status: 'success' | 'error';
result: string | null;
phase?: 'progress' | 'final' | 'tool-activity' | 'intermediate';
agentId?: string;
agentLabel?: string;
agentDone?: boolean;
newSessionId?: string;
error?: string;
}

View File

@@ -7,6 +7,11 @@ import { type Channel, type RegisteredGroup } from './types.js';
export type VisiblePhase = 'silent' | 'progress' | 'final';
interface SubagentTrack {
label: string;
activities: string[];
}
interface MessageTurnControllerOptions {
chatJid: string;
group: RegisteredGroup;
@@ -36,6 +41,7 @@ export class MessageTurnController {
private progressTicker: ReturnType<typeof setInterval> | null = null;
private progressEditFailCount = 0;
private latestProgressTextForFinal: string | null = null;
private subagents = new Map<string, SubagentTrack>();
private poisonedSessionDetected = false;
private closeRequested = false;
@@ -117,20 +123,29 @@ export class MessageTurnController {
}
if (result.phase === 'tool-activity') {
// Ensure a progress message exists for tool activity sub-lines
if (!this.progressMessageId && !this.progressCreating) {
this.progressCreating = true;
const heading = this.pendingProgressText || '작업 중...';
this.sendProgressMessage(heading).then(() => {
this.progressCreating = false;
this.ensureProgressTicker();
// Replay any queued tool activities now that the message exists
if (this.toolActivities.length > 0 && this.progressMessageId) {
void this.syncTrackedProgressMessage();
if (result.agentId) {
// Subagent tool activity
let track = this.subagents.get(result.agentId);
if (!track) {
track = { label: '작업 중...', activities: [] };
this.subagents.set(result.agentId, track);
}
if (text) {
const MAX = 2;
track.activities.push(text);
if (track.activities.length > MAX) {
track.activities = track.activities.slice(-MAX);
}
});
this.pendingProgressText = null;
}
this.ensureProgressMessageExists();
this.ensureProgressTicker();
if (!this.poisonedSessionDetected) {
this.resetIdleTimer();
}
return;
}
// Main agent tool activity
this.ensureProgressMessageExists();
if (text) {
this.addToolActivity(text);
}
@@ -141,6 +156,39 @@ export class MessageTurnController {
}
if (result.phase === 'progress') {
if (result.agentId) {
if (result.agentDone) {
this.subagents.delete(result.agentId);
} else {
const label =
text ||
(result.agentLabel ? `🔄 ${result.agentLabel}` : '작업 중...');
const existing = this.subagents.get(result.agentId);
if (existing) {
existing.label = label;
existing.activities = [];
} else {
this.subagents.set(result.agentId, { label, activities: [] });
}
}
if (!this.latestProgressText) {
this.latestProgressText = '작업 중...';
this.latestProgressTextForFinal = '작업 중...';
}
this.ensureProgressMessageExists();
this.ensureProgressTicker();
if (this.progressMessageId) {
void this.syncTrackedProgressMessage();
}
if (!this.poisonedSessionDetected) {
this.resetIdleTimer();
}
if (result.status === 'error') {
this.hadError = true;
}
return;
}
// Main agent progress
if (text) {
if (this.progressMessageId) {
// Progress message already visible — update heading directly
@@ -262,27 +310,48 @@ export class MessageTurnController {
if (minutes > 0) elapsedParts.push(`${minutes}`);
elapsedParts.push(`${seconds}`);
const activityLines =
this.toolActivities.length > 0
? '\n' +
this.toolActivities
.map((a, i) => {
const isLast = i === this.toolActivities.length - 1;
const connector = isLast ? '└' : '├';
const isSummary = a.startsWith('📋');
return isSummary ? `${connector} ${a}` : `${connector} ${a}`;
})
.join('\n')
: '';
const suffix = `\n\n${elapsedParts.join(' ')}`;
const maxText =
2000 -
TASK_STATUS_MESSAGE_PREFIX.length -
activityLines.length -
suffix.length;
let body: string;
if (this.subagents.size > 1) {
// Compact: one line per subagent with latest activity
const lines: string[] = [];
for (const [, track] of this.subagents) {
const latest = track.activities[track.activities.length - 1];
lines.push(latest ? `${track.label} · ${latest}` : track.label);
}
body = lines.join('\n');
} else if (this.subagents.size === 1) {
// Single subagent: detailed view with activity sub-lines
const [, track] = this.subagents.entries().next().value!;
const lines: string[] = [track.label];
for (let i = 0; i < track.activities.length; i++) {
const isLast = i === track.activities.length - 1;
lines.push(`${isLast ? '└' : '├'} ${track.activities[i]}`);
}
body = lines.join('\n');
} else {
// Single agent rendering
const activityLines =
this.toolActivities.length > 0
? '\n' +
this.toolActivities
.map((a, i) => {
const isLast = i === this.toolActivities.length - 1;
const connector = isLast ? '└' : '├';
const isSummary = a.startsWith('📋');
return isSummary ? `${connector} ${a}` : `${connector} ${a}`;
})
.join('\n')
: '';
body = text + activityLines;
}
const maxBody =
2000 - TASK_STATUS_MESSAGE_PREFIX.length - suffix.length;
const truncated =
text.length > maxText ? text.slice(0, maxText - 1) + '…' : text;
return `${TASK_STATUS_MESSAGE_PREFIX}${truncated}${activityLines}${suffix}`;
body.length > maxBody ? body.slice(0, maxBody - 1) + '…' : body;
return `${TASK_STATUS_MESSAGE_PREFIX}${truncated}${suffix}`;
}
private clearProgressTicker(): void {
@@ -296,6 +365,7 @@ export class MessageTurnController {
this.pendingProgressText = null;
this.progressCreating = false;
this.toolActivities = [];
this.subagents.clear();
this.latestProgressText = null;
this.previousProgressText = null;
this.latestProgressRendered = null;
@@ -304,6 +374,32 @@ export class MessageTurnController {
this.progressEditFailCount = 0;
}
/**
* Ensure a progress message exists in Discord.
* Creates one if needed, using pending or default text.
*/
private ensureProgressMessageExists(): void {
if (this.progressMessageId || this.progressCreating) return;
this.progressCreating = true;
const heading =
this.pendingProgressText || this.latestProgressText || '작업 중...';
if (!this.latestProgressText) {
this.latestProgressText = heading;
this.latestProgressTextForFinal = heading;
}
void this.sendProgressMessage(heading).then(() => {
this.progressCreating = false;
this.ensureProgressTicker();
if (
(this.toolActivities.length > 0 || this.subagents.size > 0) &&
this.progressMessageId
) {
void this.syncTrackedProgressMessage();
}
});
this.pendingProgressText = null;
}
/**
* Buffer a progress update. The previous pending text gets flushed
* immediately, and the new text waits until the next event arrives.