name: pi-agent-integration
description: Integrate the latest @earendil-works/pi-agent-core APIs into an app, library, runtime, or agent harness. Use for Pi Agent, AgentHarness, streaming bridges, tool execution hooks, convertToLlm/transformContext, queueing via steer/followUp, continue() semantics, streamFn/streamProxy, timeout/abort, session, skill, or compaction behavior.
Implement Pi-agent consumers against the latest published Pi API with stable streaming, correct queue semantics, and minimal wrapper surface area.
Step 1: Classify the request
Pick the path before editing:
| Request type | Read first |
|---|---|
Wiring or updating Agent, loop, provider, stream, or tool APIs |
references/api-surface.md |
| Adding Pi behavior in a consuming app, library, or runtime | references/common-use-cases.md |
| Using Pi's built-in harness, sessions, skills, resources, or compaction | references/harness.md |
| Debugging broken streaming, tools, queues, continuation, proxy, or abort behavior | references/troubleshooting-workarounds.md |
If a task spans multiple categories, load only the relevant references above. Keep guidance Pi-specific unless the user explicitly asks about a consuming product.
Step 2: Apply integration guardrails
- Treat npm
latestfor@earendil-works/pi-agent-coreas the source of truth before relying on a contract. - Use
Agentwhen event handling must be awaited as part of run settlement; use low-levelagentLooponly when an observational event stream is enough. - Stream user-visible text only from
message_updatewhereassistantMessageEvent.type === "text_delta". - Preserve assistant message boundaries deliberately when forwarding multi-message output.
- Do not call
prompt()orcontinue()while an agent is active; queue mid-run input withsteer()orfollowUp(). - Treat normal
continue()as a resume from a non-emptyuserortoolResulttail. Anassistanttail can only drain queued steering/follow-up messages, otherwise it throws. - Keep
streamFn,convertToLlm,transformContext,getApiKey, queue providers, and loop hooks no-throw for expected request/runtime failures; return safe values or encode failures in protocol events. - Keep tool calls, tool progress, tool results, thinking deltas, and provider payloads internal unless the product UX explicitly exposes them.
- Prefer Pi's built-in harness when sessions, skills, prompt templates, resources, filesystem/shell environment, compaction, or tree navigation are required.
Step 3: Implement with minimal surface
- Prefer Pi options over custom wrapper state machines:
streamFn,getApiKey,sessionId,thinkingBudgets,transport,maxRetryDelayMs,onPayload,onResponse,beforeToolCall,afterToolCall,prepareNextTurn,toolExecution,steeringMode, andfollowUpMode. - Mutate
Agentstate throughagent.stateproperties andreset(); do not invent setter wrappers unless the consumer API needs them. - Use
transformContextfor message-level pruning/injection andconvertToLlmfor provider-compatible role conversion/filtering. - Keep queue modes explicit (
"one-at-a-time"or"all") when ordering or batching matters. - For server-proxied model access, use
streamFnwithstreamProxy-style behavior instead of provider logic scattered through consumers. - For tool policy, use
toolExecution, per-toolexecutionMode,beforeToolCall,afterToolCall, thrown tool errors, andterminatebefore adding a custom tool runner. - Keep timeout/abort paths observable and make sure streams/iterables settle cleanly.
Step 4: Verify behavior
- Verify the event-to-stream bridge emits only text deltas, preserves intended boundaries, and closes on success, error, and abort.
- Verify
prompt()/continue()race handling and queuedsteer()/followUp()behavior. - Verify
continue()preconditions for empty history,usertail,toolResulttail, andassistanttail with and without queued messages. - Verify custom message types remain in agent state while
convertToLlmemits only provider-compatible messages. - Verify
streamFnencodes expected provider failures instead of throwing/rejecting. - Verify tool execution ordering under default parallel mode, sequential overrides, hook blocking/patching, progress updates, and
terminatebehavior. - Verify
Agent.subscribe()listener settlement andwaitForIdle()behavior when listeners perform async work. - Verify
AgentHarnesssession, resource, hook, compaction, and abort behavior when the harness path is used.
Step 5: Version discipline
- Target the latest published Pi package only.
- Re-check the latest package metadata and declarations before material API updates.
- Do not add backward-compatibility shims or old package-name guidance unless the user explicitly asks for a migration.