hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Pi Coding Agent Support

Open zhushanwen321 opened this issue 5 months ago • 3 comments

Pi Coding Agent Support

I'm a Pi coding agent user and I'd love to control it from my phone via HAPI. After seeing #375 get abandoned and multiple requests (#335, #620, #770) go unanswered, I implemented it myself. I have a working branch — want to get feedback before opening a PR.

Approach

Pi has a built-in --mode rpc that speaks JSONL over stdin/stdout. The integration follows the same process model as ACP — spawn an agent subprocess, communicate via stdio pipes, receive real-time event stream. But Pi uses its own simpler JSONL protocol instead of JSON-RPC 2.0 (no request IDs, no jsonrpc field).

What's shared with ACP:

  • Spawn subprocess + stdio bidirectional pipe (stdin.write / stdout.on('data'))
  • Real-time event-driven streaming (not file polling like Codex)
  • Reuses HAPI's Runner lifecycle: bootstrapSession → createRunnerLifecycle → registerKillSessionHandler

What's different from ACP:

  • Custom JSONL protocol instead of JSON-RPC 2.0 — commands are { type: 'prompt', message: '...' }, responses are { type: 'response', success: true }, events are { type: 'turn_end', ... }
  • No AgentBackend interface — Pi has its own runPi.ts runner, similar to how Codex has runCodex.ts
  • No permission request/response mechanism
  • No MCP bridge injection

Borrowed from Codex:

  • Delta accumulation for streaming text (PiMessageAccumulator mirrors Codex's ReasoningProcessor — accumulate deltas locally, flush complete messages to web client to avoid duplicate rendering)

What works

I've been testing this locally:

  • Streaming responses (text, reasoning/thinking, tool calls)
  • Model switching and abort
  • Session lifecycle (spawn/kill/restart, no orphan processes)
  • Web UI renders Pi's output correctly

Known limitation

Pi's RPC mode has no tool permission approval mechanism. HAPI's "approve tool calls from phone" won't work for Pi — tools execute automatically. This is similar to Gemini in yolo mode.

Ask

Does this approach make sense for HAPI? If so, I'll clean up the branch and submit a PR.


Related: #335 #620 #770 #375

zhushanwen321 avatar Jun 07 '26 15:06 zhushanwen321

Yes, the approach looks compatible with HAPI's current architecture. I did not find existing Pi support in the current tree, so this would need to land as a new agent flavor/command, not only a backend file.

For a non-ACP JSONL protocol, a dedicated runPi.ts is reasonable: HAPI already has standalone per-agent runners where the protocol does not fit the generic ACP backend path. The important PR surface is likely:

  • add pi to the shared agent flavor/schema/capability lists
  • add a CLI command and runner spawn mapping
  • decide whether Pi needs its own session id metadata field for resume support
  • map Pi events into the existing HAPI message shapes (message, reasoning, tool-call, tool-call-result, token_count, etc.)
  • make the no-phone-approval limitation explicit in the UI/docs/config, because current permission approval depends on agent-side permission request plumbing

The permission limitation you described is consistent with the current design: HAPI can route permission approvals when the runner/backend exposes pending permission requests, but if Pi RPC mode executes tools automatically then HAPI cannot provide phone approval for those calls.

Relevant code:

  • cli/src/codex/runCodex.ts:46 - Codex uses a dedicated runner path with HAPI session bootstrap.
  • cli/src/codex/runCodex.ts:85 - Codex wires the shared runner lifecycle cleanup path.
  • cli/src/codex/runCodex.ts:92 - Codex registers the shared kill-session handler.
  • cli/src/codex/runCodex.ts:300 - Codex handles runtime session config changes over RPC.
  • cli/src/agent/runners/runAgentSession.ts:68 - generic ACP runner creates an AgentBackend via registry.
  • cli/src/agent/types.ts:88 - AgentBackend expects JSON-RPC/ACP-like session, prompt, cancel, permission, and disconnect methods.
  • cli/src/agent/messageConverter.ts:37 - generic agent messages are converted to HAPI's existing web message format.
  • shared/src/modes.ts:10 - supported agent flavors are enumerated in shared protocol.
  • shared/src/flavors.ts:12 - flavor capabilities are configured per agent.
  • shared/src/apiTypes.ts:177 - web/hub spawn requests validate agent with AgentFlavorSchema.
  • cli/src/commands/registry.ts:18 - CLI subcommands are registered explicitly.
  • cli/src/runner/run.ts:903 - runner maps agent names to CLI args for remote spawning.
  • shared/src/schemas.ts:36 - session metadata currently has per-agent session id fields for Claude/Codex/Gemini/OpenCode/Cursor/Kimi.
  • web/src/lib/sessionResume.ts:7 - web resume logic resolves agent-specific session ids from metadata.

Need more info:

  • Does Pi RPC expose a stable session/thread id that HAPI can store and resume later?
  • Does Pi RPC support cancellation/abort for an in-flight prompt?
  • What exact JSONL event types does Pi emit for text, reasoning, tool calls, tool results, usage, and turn end?

HAPI Bot

github-actions[bot] avatar Jun 07 '26 15:06 github-actions[bot]

Yes, the approach looks compatible with HAPI's current architecture. I did not find existing Pi support in the current tree, so this would need to land as a new agent flavor/command, not only a backend file.

For a non-ACP JSONL protocol, a dedicated runPi.ts is reasonable: HAPI already has standalone per-agent runners where the protocol does not fit the generic ACP backend path. The important PR surface is likely:

  • add pi to the shared agent flavor/schema/capability lists
  • add a CLI command and runner spawn mapping
  • decide whether Pi needs its own session id metadata field for resume support
  • map Pi events into the existing HAPI message shapes (message, reasoning, tool-call, tool-call-result, token_count, etc.)
  • make the no-phone-approval limitation explicit in the UI/docs/config, because current permission approval depends on agent-side permission request plumbing

The permission limitation you described is consistent with the current design: HAPI can route permission approvals when the runner/backend exposes pending permission requests, but if Pi RPC mode executes tools automatically then HAPI cannot provide phone approval for those calls.

Relevant code:

  • cli/src/codex/runCodex.ts:46 - Codex uses a dedicated runner path with HAPI session bootstrap.
  • cli/src/codex/runCodex.ts:85 - Codex wires the shared runner lifecycle cleanup path.
  • cli/src/codex/runCodex.ts:92 - Codex registers the shared kill-session handler.
  • cli/src/codex/runCodex.ts:300 - Codex handles runtime session config changes over RPC.
  • cli/src/agent/runners/runAgentSession.ts:68 - generic ACP runner creates an AgentBackend via registry.
  • cli/src/agent/types.ts:88 - AgentBackend expects JSON-RPC/ACP-like session, prompt, cancel, permission, and disconnect methods.
  • cli/src/agent/messageConverter.ts:37 - generic agent messages are converted to HAPI's existing web message format.
  • shared/src/modes.ts:10 - supported agent flavors are enumerated in shared protocol.
  • shared/src/flavors.ts:12 - flavor capabilities are configured per agent.
  • shared/src/apiTypes.ts:177 - web/hub spawn requests validate agent with AgentFlavorSchema.
  • cli/src/commands/registry.ts:18 - CLI subcommands are registered explicitly.
  • cli/src/runner/run.ts:903 - runner maps agent names to CLI args for remote spawning.
  • shared/src/schemas.ts:36 - session metadata currently has per-agent session id fields for Claude/Codex/Gemini/OpenCode/Cursor/Kimi.
  • web/src/lib/sessionResume.ts:7 - web resume logic resolves agent-specific session ids from metadata.

Need more info:

  • Does Pi RPC expose a stable session/thread id that HAPI can store and resume later?
  • Does Pi RPC support cancellation/abort for an in-flight prompt?
  • What exact JSONL event types does Pi emit for text, reasoning, tool calls, tool results, usage, and turn end?

HAPI Bot

Here's the info you asked about:

Session resume: Yes, Pi RPC supports this. The get_state command returns a sessionFile path (JSONL) and a sessionId. On restart, Pi can resume via pi --session-id <id> CLI flag, or the new_session RPC command with a session ID. So we can store Pi's session ID in the metadata and resume later.

Abort: Yes. Pi RPC has an { type: 'abort' } command that cancels the in-flight prompt. The agent stops generation and fires a turn_end event.

Pi RPC event types for the message shapes:

Pi RPC event HAPI message shape
message_end (flushes accumulated text) message
message_end (flushes accumulated reasoning) reasoning
tool_execution_start tool-call
tool_execution_end tool-call-result
turn_end with usage field token_count + turn_complete

Text and reasoning don't map 1:1 — Pi streams them as text_delta / thinking_delta fragments inside message_update events. I accumulate these deltas locally (PiMessageAccumulator, same pattern as Codex's ReasoningProcessor) and flush complete message / reasoning snapshots on message_end. This avoids duplicate rendering on the web client.

zhushanwen321 avatar Jun 07 '26 16:06 zhushanwen321

Sure, PRs welcome :)

tiann avatar Jun 08 '26 05:06 tiann