























@@ -19,7 +19,7 @@ OpenClaw uses the pi SDK to embed an AI coding agent into its messaging gateway
1919- Multi-account auth profile rotation with failover
2020- Provider-agnostic model switching
212122-## Package Dependencies
22+## Package dependencies
23232424```json
2525{
@@ -37,7 +37,7 @@ OpenClaw uses the pi SDK to embed an AI coding agent into its messaging gateway
3737| `pi-coding-agent` | High-level SDK: `createAgentSession`, `SessionManager`, `AuthStorage`, `ModelRegistry`, built-in tools |
3838| `pi-tui` | Terminal UI components (used in OpenClaw's local TUI mode) |
393940-## File Structure
40+## File structure
41414242```
4343src/agents/
@@ -239,9 +239,9 @@ Image injection is prompt-local: OpenClaw loads image refs from the current prom
239239passes them via `images` for that turn only. It does not re-scan older history turns
240240to re-inject image payloads.
241241242-## Tool Architecture
242+## Tool architecture
243243244-### Tool Pipeline
244+### Tool pipeline
2452452462461. **Base Tools**: pi's `codingTools` (read, bash, edit, write)
2472472. **Custom Replacements**: OpenClaw replaces bash with `exec`/`process`, customizes read/edit/write for sandbox
@@ -251,7 +251,7 @@ to re-inject image payloads.
2512516. **Schema Normalization**: Schemas cleaned for Gemini/OpenAI quirks
2522527. **AbortSignal Wrapping**: Tools wrapped to respect abort signals
253253254-### Tool Definition Adapter
254+### Tool definition adapter
255255256256pi-agent-core's `AgentTool` has a different `execute` signature than pi-coding-agent's `ToolDefinition`. The adapter in `pi-tool-definition-adapter.ts` bridges this:
257257@@ -270,7 +270,7 @@ export function toToolDefinitions(tools: AnyAgentTool[]): ToolDefinition[] {
270270}
271271```
272272273-### Tool Split Strategy
273+### Tool split strategy
274274275275`splitSdkTools()` passes all tools via `customTools`:
276276@@ -296,9 +296,9 @@ const systemPromptOverride = createSystemPromptOverride(appendPrompt);
296296applySystemPromptOverrideToSession(session, systemPromptOverride);
297297```
298298299-## Session Management
299+## Session management
300300301-### Session Files
301+### Session files
302302303303Sessions are JSONL files with tree structure (id/parentId linking). Pi's `SessionManager` handles persistence:
304304@@ -308,7 +308,7 @@ const sessionManager = SessionManager.open(params.sessionFile);
308308309309OpenClaw wraps this with `guardSessionManager()` for tool result safety.
310310311-### Session Caching
311+### Session caching
312312313313`session-manager-cache.ts` caches SessionManager instances to avoid repeated file parsing:
314314@@ -318,7 +318,7 @@ sessionManager = SessionManager.open(params.sessionFile);
318318trackSessionManagerAccess(params.sessionFile);
319319```
320320321-### History Limiting
321+### History limiting
322322323323`limitHistoryTurns()` trims conversation history based on channel type (DM vs group).
324324@@ -339,7 +339,7 @@ const compactResult = await compactEmbeddedPiSessionDirect({
339339340340## Authentication & Model Resolution
341341342-### Auth Profiles
342+### Auth profiles
343343344344OpenClaw maintains an auth profile store with multiple API keys per provider:
345345@@ -355,7 +355,7 @@ await markAuthProfileFailure({ store, profileId, reason, cfg, agentDir });
355355const rotated = await advanceAuthProfile();
356356```
357357358-### Model Resolution
358+### Model resolution
359359360360```typescript
361361import { resolveModel } from "./pi-embedded-runner/model.js";
@@ -387,11 +387,11 @@ if (fallbackConfigured && isFailoverErrorMessage(errorText)) {
387387}
388388```
389389390-## Pi Extensions
390+## Pi extensions
391391392392OpenClaw loads custom pi extensions for specialized behavior:
393393394-### Compaction Safeguard
394+### Compaction safeguard
395395396396`src/agents/pi-hooks/compaction-safeguard.ts` adds guardrails to compaction, including adaptive token budgeting plus tool failure and file operation summaries:
397397@@ -402,7 +402,7 @@ if (resolveCompactionMode(params.cfg) === "safeguard") {
402402}
403403```
404404405-### Context Pruning
405+### Context pruning
406406407407`src/agents/pi-hooks/context-pruning.ts` implements cache-TTL based context pruning:
408408@@ -420,7 +420,7 @@ if (cfg?.agents?.defaults?.contextPruning?.mode === "cache-ttl") {
420420421421## Streaming & Block Replies
422422423-### Block Chunking
423+### Block chunking
424424425425`EmbeddedBlockChunker` manages streaming text into discrete reply blocks:
426426@@ -439,17 +439,17 @@ const stripBlockTags = (text: string, state: { thinking: boolean; final: boolean
439439};
440440```
441441442-### Reply Directives
442+### Reply directives
443443444444Reply directives like `[[media:url]]`, `[[voice]]`, `[[reply:id]]` are parsed and extracted:
445445446446```typescript
447447const { text: cleanedText, mediaUrls, audioAsVoice, replyToId } = consumeReplyDirectives(chunk);
448448```
449449450-## Error Handling
450+## Error handling
451451452-### Error Classification
452+### Error classification
453453454454`pi-embedded-helpers.ts` classifies errors for appropriate handling:
455455@@ -462,7 +462,7 @@ isFailoverAssistantError(...) // Should failover
462462classifyFailoverReason(errorText) // "auth" | "rate_limit" | "quota" | "timeout" | ...
463463```
464464465-### Thinking Level Fallback
465+### Thinking level fallback
466466467467If a thinking level is unsupported, it falls back:
468468@@ -477,7 +477,7 @@ if (fallbackThinking) {
477477}
478478```
479479480-## Sandbox Integration
480+## Sandbox integration
481481482482When sandbox mode is enabled, tools and paths are constrained:
483483@@ -523,7 +523,7 @@ import { ... } from "@mariozechner/pi-tui";
523523524524This provides the interactive terminal experience similar to pi's native mode.
525525526-## Key Differences from Pi CLI
526+## Key differences from Pi CLI
527527528528| Aspect | Pi CLI | OpenClaw Embedded |
529529| --------------- | ----------------------- | ---------------------------------------------------------------------------------------------- |
@@ -535,7 +535,7 @@ This provides the interactive terminal experience similar to pi's native mode.
535535| Extensions | Loaded from disk | Programmatic + disk paths |
536536| Event handling | TUI rendering | Callback-based (onBlockReply, etc.) |
537537538-## Future Considerations
538+## Future considerations
539539540540Areas for potential rework:
541541此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。