mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
fix(cli): harden ACP/Codex event handling (#211)
Co-authored-by: Lihengwannafly <Lihengwannafly@users.noreply.github.com>
This commit is contained in:
co-authored by
Lihengwannafly
parent
77cfa715f9
commit
10f7e1404e
@@ -76,17 +76,187 @@ function extractChanges(value: unknown): Record<string, unknown> | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractTextFromContent(value: unknown): string | null {
|
||||
if (typeof value === 'string' && value.length > 0) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const chunks: string[] = [];
|
||||
for (const entry of value) {
|
||||
const record = asRecord(entry);
|
||||
if (!record) continue;
|
||||
const text = asString(record.text ?? record.message ?? record.content);
|
||||
if (text) {
|
||||
chunks.push(text);
|
||||
}
|
||||
}
|
||||
|
||||
if (chunks.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return chunks.join('');
|
||||
}
|
||||
|
||||
function extractItemText(item: Record<string, unknown>): string | null {
|
||||
return asString(item.text ?? item.message) ?? extractTextFromContent(item.content);
|
||||
}
|
||||
|
||||
function extractReasoningText(item: Record<string, unknown>): string | null {
|
||||
const direct = extractItemText(item);
|
||||
if (direct) {
|
||||
return direct;
|
||||
}
|
||||
|
||||
const summary = item.summary_text ?? item.summaryText;
|
||||
if (Array.isArray(summary)) {
|
||||
const chunks = summary.filter((part): part is string => typeof part === 'string' && part.length > 0);
|
||||
if (chunks.length > 0) {
|
||||
return chunks.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export class AppServerEventConverter {
|
||||
private readonly agentMessageBuffers = new Map<string, string>();
|
||||
private readonly reasoningBuffers = new Map<string, string>();
|
||||
private readonly commandOutputBuffers = new Map<string, string>();
|
||||
private readonly commandMeta = new Map<string, Record<string, unknown>>();
|
||||
private readonly fileChangeMeta = new Map<string, Record<string, unknown>>();
|
||||
private readonly completedAgentMessageItems = new Set<string>();
|
||||
private readonly completedReasoningItems = new Set<string>();
|
||||
private readonly reasoningSectionBreakKeys = new Set<string>();
|
||||
private readonly lastAgentMessageDeltaByItemId = new Map<string, string>();
|
||||
private readonly lastReasoningDeltaByItemId = new Map<string, string>();
|
||||
private readonly lastCommandOutputDeltaByItemId = new Map<string, string>();
|
||||
|
||||
private handleWrappedCodexEvent(paramsRecord: Record<string, unknown>): ConvertedEvent[] | null {
|
||||
const msg = asRecord(paramsRecord.msg);
|
||||
if (!msg) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const msgType = asString(msg.type);
|
||||
if (!msgType) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (msgType === 'item_started' || msgType === 'item_completed') {
|
||||
const itemMethod = msgType === 'item_started' ? 'item/started' : 'item/completed';
|
||||
const item = asRecord(msg.item) ?? {};
|
||||
const params: Record<string, unknown> = {
|
||||
item,
|
||||
itemId: asString(msg.item_id ?? msg.itemId ?? item.id),
|
||||
threadId: asString(msg.thread_id ?? msg.threadId),
|
||||
turnId: asString(msg.turn_id ?? msg.turnId)
|
||||
};
|
||||
return this.handleNotification(itemMethod, params);
|
||||
}
|
||||
|
||||
if (
|
||||
msgType === 'task_started' ||
|
||||
msgType === 'task_complete' ||
|
||||
msgType === 'turn_aborted' ||
|
||||
msgType === 'task_failed'
|
||||
) {
|
||||
const turnId = asString(msg.turn_id ?? msg.turnId);
|
||||
if ((msgType === 'task_complete' || msgType === 'turn_aborted' || msgType === 'task_failed') && !turnId) {
|
||||
logger.debug('[AppServerEventConverter] Ignoring wrapped terminal event without turn_id', { msgType });
|
||||
return [];
|
||||
}
|
||||
|
||||
const event: ConvertedEvent = { type: msgType };
|
||||
if (turnId) {
|
||||
event.turn_id = turnId;
|
||||
}
|
||||
if (msgType === 'task_failed') {
|
||||
const error = asString(msg.error ?? msg.message ?? asRecord(msg.error)?.message);
|
||||
if (error) {
|
||||
event.error = error;
|
||||
}
|
||||
}
|
||||
return [event];
|
||||
}
|
||||
|
||||
if (msgType === 'agent_message_delta' || msgType === 'agent_message_content_delta') {
|
||||
const itemId = asString(msg.item_id ?? msg.itemId ?? msg.id) ?? 'agent-message';
|
||||
const delta = asString(msg.delta ?? msg.text ?? msg.message);
|
||||
if (!delta) return [];
|
||||
return this.handleNotification('item/agentMessage/delta', { itemId, delta });
|
||||
}
|
||||
|
||||
if (msgType === 'reasoning_content_delta') {
|
||||
const itemId = asString(msg.item_id ?? msg.itemId ?? msg.id) ?? 'reasoning';
|
||||
const delta = asString(msg.delta ?? msg.text ?? msg.message);
|
||||
if (!delta) return [];
|
||||
return this.handleNotification('item/reasoning/summaryTextDelta', { itemId, delta });
|
||||
}
|
||||
|
||||
if (msgType === 'agent_reasoning_section_break') {
|
||||
const itemId = asString(msg.item_id ?? msg.itemId ?? msg.id) ?? 'reasoning';
|
||||
const summaryIndex = asNumber(msg.summary_index ?? msg.summaryIndex);
|
||||
return this.handleNotification('item/reasoning/summaryPartAdded', {
|
||||
itemId,
|
||||
...(summaryIndex !== null ? { summaryIndex } : {})
|
||||
});
|
||||
}
|
||||
|
||||
if (msgType === 'agent_reasoning_delta' || msgType === 'agent_reasoning' || msgType === 'agent_message') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (msgType === 'exec_command_output_delta') {
|
||||
const itemId = asString(msg.call_id ?? msg.callId ?? msg.item_id ?? msg.itemId ?? msg.id);
|
||||
const delta = asString(msg.delta ?? msg.output ?? msg.stdout ?? msg.text);
|
||||
if (!itemId || !delta) return [];
|
||||
return this.handleNotification('item/commandExecution/outputDelta', { itemId, delta });
|
||||
}
|
||||
|
||||
if (msgType === 'error') {
|
||||
const errorRecord = asRecord(msg.error);
|
||||
const willRetry = asBoolean(msg.will_retry ?? msg.willRetry ?? errorRecord?.will_retry ?? errorRecord?.willRetry) ?? false;
|
||||
if (willRetry) {
|
||||
return [];
|
||||
}
|
||||
const error = asString(msg.message ?? msg.reason ?? errorRecord?.message);
|
||||
return error ? [{ type: 'task_failed', error }] : [];
|
||||
}
|
||||
|
||||
if (
|
||||
msgType === 'mcp_startup_update' ||
|
||||
msgType === 'mcp_startup_complete' ||
|
||||
msgType === 'plan_update' ||
|
||||
msgType === 'skills_update_available' ||
|
||||
msgType === 'stream_error' ||
|
||||
msgType === 'warning' ||
|
||||
msgType === 'context_compacted' ||
|
||||
msgType === 'terminal_interaction' ||
|
||||
msgType === 'user_message'
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [msg as ConvertedEvent];
|
||||
}
|
||||
|
||||
handleNotification(method: string, params: unknown): ConvertedEvent[] {
|
||||
const events: ConvertedEvent[] = [];
|
||||
const paramsRecord = asRecord(params) ?? {};
|
||||
|
||||
if (method.startsWith('codex/event/')) {
|
||||
return this.handleWrappedCodexEvent(paramsRecord) ?? events;
|
||||
}
|
||||
|
||||
if (method === 'account/rateLimits/updated' || method === 'turn/plan/updated' || method === 'thread/compacted') {
|
||||
return events;
|
||||
}
|
||||
|
||||
if (method === 'thread/started' || method === 'thread/resumed') {
|
||||
const thread = asRecord(paramsRecord.thread) ?? paramsRecord;
|
||||
const threadId = asString(thread.threadId ?? thread.thread_id ?? thread.id);
|
||||
@@ -152,16 +322,26 @@ export class AppServerEventConverter {
|
||||
const itemId = extractItemId(paramsRecord);
|
||||
const delta = asString(paramsRecord.delta ?? paramsRecord.text ?? paramsRecord.message);
|
||||
if (itemId && delta) {
|
||||
const lastDelta = this.lastAgentMessageDeltaByItemId.get(itemId);
|
||||
if (lastDelta === delta) {
|
||||
return events;
|
||||
}
|
||||
this.lastAgentMessageDeltaByItemId.set(itemId, delta);
|
||||
const prev = this.agentMessageBuffers.get(itemId) ?? '';
|
||||
this.agentMessageBuffers.set(itemId, prev + delta);
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
if (method === 'item/reasoning/textDelta') {
|
||||
if (method === 'item/reasoning/textDelta' || method === 'item/reasoning/summaryTextDelta') {
|
||||
const itemId = extractItemId(paramsRecord) ?? 'reasoning';
|
||||
const delta = asString(paramsRecord.delta ?? paramsRecord.text ?? paramsRecord.message);
|
||||
if (delta) {
|
||||
const lastDelta = this.lastReasoningDeltaByItemId.get(itemId);
|
||||
if (lastDelta === delta) {
|
||||
return events;
|
||||
}
|
||||
this.lastReasoningDeltaByItemId.set(itemId, delta);
|
||||
const prev = this.reasoningBuffers.get(itemId) ?? '';
|
||||
this.reasoningBuffers.set(itemId, prev + delta);
|
||||
events.push({ type: 'agent_reasoning_delta', delta });
|
||||
@@ -170,6 +350,15 @@ export class AppServerEventConverter {
|
||||
}
|
||||
|
||||
if (method === 'item/reasoning/summaryPartAdded') {
|
||||
const itemId = extractItemId(paramsRecord) ?? 'reasoning';
|
||||
const summaryIndex = asNumber(paramsRecord.summaryIndex ?? paramsRecord.summary_index);
|
||||
if (summaryIndex !== null) {
|
||||
const key = `${itemId}:${summaryIndex}`;
|
||||
if (this.reasoningSectionBreakKeys.has(key)) {
|
||||
return events;
|
||||
}
|
||||
this.reasoningSectionBreakKeys.add(key);
|
||||
}
|
||||
events.push({ type: 'agent_reasoning_section_break' });
|
||||
return events;
|
||||
}
|
||||
@@ -178,6 +367,11 @@ export class AppServerEventConverter {
|
||||
const itemId = extractItemId(paramsRecord);
|
||||
const delta = asString(paramsRecord.delta ?? paramsRecord.text ?? paramsRecord.output ?? paramsRecord.stdout);
|
||||
if (itemId && delta) {
|
||||
const lastDelta = this.lastCommandOutputDeltaByItemId.get(itemId);
|
||||
if (lastDelta === delta) {
|
||||
return events;
|
||||
}
|
||||
this.lastCommandOutputDeltaByItemId.set(itemId, delta);
|
||||
const prev = this.commandOutputBuffers.get(itemId) ?? '';
|
||||
this.commandOutputBuffers.set(itemId, prev + delta);
|
||||
}
|
||||
@@ -197,22 +391,32 @@ export class AppServerEventConverter {
|
||||
|
||||
if (itemType === 'agentmessage') {
|
||||
if (method === 'item/completed') {
|
||||
const text = asString(item.text ?? item.message ?? item.content) ?? this.agentMessageBuffers.get(itemId);
|
||||
if (this.completedAgentMessageItems.has(itemId)) {
|
||||
return events;
|
||||
}
|
||||
const text = extractItemText(item) ?? this.agentMessageBuffers.get(itemId);
|
||||
if (text) {
|
||||
events.push({ type: 'agent_message', message: text });
|
||||
this.completedAgentMessageItems.add(itemId);
|
||||
this.agentMessageBuffers.delete(itemId);
|
||||
}
|
||||
this.agentMessageBuffers.delete(itemId);
|
||||
this.lastAgentMessageDeltaByItemId.delete(itemId);
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
if (itemType === 'reasoning') {
|
||||
if (method === 'item/completed') {
|
||||
const text = asString(item.text ?? item.message ?? item.content) ?? this.reasoningBuffers.get(itemId);
|
||||
if (this.completedReasoningItems.has(itemId)) {
|
||||
return events;
|
||||
}
|
||||
const text = extractReasoningText(item) ?? this.reasoningBuffers.get(itemId);
|
||||
if (text) {
|
||||
events.push({ type: 'agent_reasoning', text });
|
||||
this.completedReasoningItems.add(itemId);
|
||||
this.reasoningBuffers.delete(itemId);
|
||||
}
|
||||
this.reasoningBuffers.delete(itemId);
|
||||
this.lastReasoningDeltaByItemId.delete(itemId);
|
||||
}
|
||||
return events;
|
||||
}
|
||||
@@ -256,6 +460,7 @@ export class AppServerEventConverter {
|
||||
|
||||
this.commandMeta.delete(itemId);
|
||||
this.commandOutputBuffers.delete(itemId);
|
||||
this.lastCommandOutputDeltaByItemId.delete(itemId);
|
||||
}
|
||||
|
||||
return events;
|
||||
@@ -309,5 +514,11 @@ export class AppServerEventConverter {
|
||||
this.commandOutputBuffers.clear();
|
||||
this.commandMeta.clear();
|
||||
this.fileChangeMeta.clear();
|
||||
this.completedAgentMessageItems.clear();
|
||||
this.completedReasoningItems.clear();
|
||||
this.reasoningSectionBreakKeys.clear();
|
||||
this.lastAgentMessageDeltaByItemId.clear();
|
||||
this.lastReasoningDeltaByItemId.clear();
|
||||
this.lastCommandOutputDeltaByItemId.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user