Skip to content

Result text lost when output_tokens=0 (no stream events emitted) #453

@Miluer-tcq

Description

@Miluer-tcq

Problem

When using claude-agent-acp (v0.22.2) with weixin-acp, every other message returns empty text. The response text exists in message.result but is never forwarded to the client via agent_message_chunk.

Debug findings

Adding logging to the result handler reveals:

[debug] result: subtype=success is_error=false result="**3**。" output_tokens=0 promptReplayed=true

The answer is correct (**3**) but output_tokens=0 and no agent_message_chunk events were emitted before the result. The client receives [no text].

Root Cause

When Claude Code SDK returns a cached or short response, it skips streaming and puts the text directly in message.result with output_tokens=0. The current code in acp-agent.js only emits text via stream_eventstreamEventToAcpNotifications, so if no stream events occur, the client gets nothing.

Suggested Fix

In the result / success handler, add a fallback to emit the result text as an agent_message_chunk when output_tokens === 0 but message.result is non-empty:

case "success": {
  // ... existing checks ...
  
  // Fallback: emit result text if no stream chunks were sent
  if (message.result && message.usage.output_tokens === 0) {
    await this.client.sessionUpdate({
      sessionId: params.sessionId,
      update: {
        sessionUpdate: "agent_message_chunk",
        content: { type: "text", text: message.result },
      },
    });
  }
  return { stopReason: "end_turn", usage };
}

Environment

Related

wong2/weixin-agent-sdk#13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions