Architecture & choosing a layer
PiSharp is organized as composable .NET 10 libraries rather than a single monolithic agent framework. You can reference only the provider client (PiSharp.AI), drop the in-memory agent loop into an existing service (PiSharp.Agent), embed Pi-compatible JSONL session trees (PiSharp.CodingAgent), or run crash-safe durable workflows and subagent teams (PiSharp.Durable and PiSharp.CustomAgents).
This track explains how the pieces work together from top to bottom. For exact package dependency trees and every experimental diagnostic ID, see Architecture and Use the SDK.
Two execution stacks
Section titled “Two execution stacks”PiSharp intentionally maintains two distinct agent stacks over the same provider layer (PiSharp.AI and PiSharp.Contracts):
| Dimension | Classic stack (Agent + Sessions + CodingAgent) |
Durable stack (Durable + CustomAgents) |
|---|---|---|
| Execution model | In-memory turn loop (Agent) optionally backed by append-only Pi JSONL session trees (PiSharp.Sessions). |
Commit-first state machine (IHarness): every turn, streamed progress, tool call, document and timer commits before display. |
| Crash mid-turn | Loses the in-flight turn; committed prior entries remain in the JSONL tree. | Reopening the SQLite/JSONL/Chord store resumes unfinished turns and tasks automatically. |
| Interoperability with Pi | Shares ~/.pi/agent, .pi, JSONL session format, RPC wire protocol and TypeScript extensions with Pi. |
Ports pi-durable (pisharp durable); PiSharp.CustomAgents adds C# workflows and teams on top. |
| API stability | Stable (1.6.0.1 on NuGet). |
Experimental (PISHARP_DURABLE001, PISHARP_AGENTS001). |
Pick the right layer for your project
Section titled “Pick the right layer for your project”| What you are building | Start with | Read next | Try a sample |
|---|---|---|---|
| Interactive coding CLI or RPC backend | PiSharp.Cli (dotnet tool install -g PiSharp.Cli) or PiSharp.Rpc |
Quickstart, RPC host | pisharp, pisharp --mode rpc |
| Custom single-agent app with your own domain tools | PiSharp.Agent (+ PiSharp.AI) |
Anatomy of an agent turn, Agent loop SDK | 05-PersistentTaskManager |
| Embedded coding agent with branching history & skills | PiSharp.CodingAgent + PiSharp.Tools (+ PiSharp.Tools.Skia) |
Sessions, branches & durability, Coding agent SDK | 28-ResearchWorkbench |
| Crash-safe agent or long-running service | PiSharp.Durable + PiSharp.Durable.Sqlite (+ PiSharp.Durable.Tools) |
Sessions, branches & durability, Durable library | Durable Examples 00–47 |
| Typed multi-step workflows, approvals or subagent teams | PiSharp.CustomAgents (+ .JavaScript / .Scripting) |
Workflows & multi-agent teams, Custom agents | Hybrid Orders Workflow |
| Shared reactive host, native plugins or remote execution | PiSharp.Chord, PiSharp.Chord.Hosting, PiSharp.Durable.Store, PiSharp.Env |
Chord & hosting, Remote environments | Durable + Chord 35–37 |
| Reusable tool, command or hook package for PiSharp | PiSharp.Extensions.Abstractions (or Pi TypeScript via PiSharp.Compatibility.Node) |
Tools, schemas & trust, Build an extension | StatefulTodo & SessionCheckpoint |
Minimal mental model per layer
Section titled “Minimal mental model per layer”1. Streaming chat only (PiSharp.AI)
Section titled “1. Streaming chat only (PiSharp.AI)”PiSharp.AI knows nothing about tool execution loops or files. You pass a ModelDescriptor, a provider transport (NativeProviderFactory), and transcript messages; it streams typed StreamEvent deltas (TextDelta, ToolCallDelta, StreamDone, StreamError).
2. In-memory agent loop (PiSharp.Agent)
Section titled “2. In-memory agent loop (PiSharp.Agent)”Agent wraps TurnRunner, ToolBatchScheduler, and AgentPendingInputQueue. It sends the transcript, awaits your IAgentEventSink, runs tool calls in parallel or sequentially through IToolHooks, checks steering/follow-up queues, and loops until the model stops or hits a configured limit.
var config = new AgentConfiguration(model, provider, [new ToolDefinition("get_time", new TimeTool())]);await using var agent = new Agent(config, Clock, new ConsoleSink());var result = await agent.PromptAsync([systemEntry, userEntry]);3. Persistent sessions & built-in tools (PiSharp.CodingAgent + PiSharp.Tools)
Section titled “3. Persistent sessions & built-in tools (PiSharp.CodingAgent + PiSharp.Tools)”PiSharp.CodingAgent couples the loop to PiSharp.Sessions (append-only JSONL trees with branching and compaction), settings, skills, and prompt templates. Built-in file/shell tools live in PiSharp.Tools and require an explicit IToolActionPolicy when embedded so your host controls every path and command.
4. Commit-first durable agents & workflows (PiSharp.Durable + PiSharp.CustomAgents)
Section titled “4. Commit-first durable agents & workflows (PiSharp.Durable + PiSharp.CustomAgents)”PiSharp.Durable turns turns, tool calls, subagents, documents, and timers into persisted tasks over SQLite, JSONL, or Chord StoreStorage. PiSharp.CustomAgents layers typed Workflow.For<TData> state machines, AgentTeam orchestration, typed C# / JS / Roslyn tools, plugins, and FauxAgentModels offline testing on top of that harness.
var summarize = Workflow.For<Note>("summarize") .Then("clean", note => note.Text = note.Text.Trim()) .Prompt("summary", note => $"Summarize in one line: {note.Text}", (note, text) => note.Summary = text) .Build();Five design rules across every layer
Section titled “Five design rules across every layer”- Streaming is explicit: partial tool calls are surfaced for UI previews (
StreamingJsonParser) and are never executed until parsed and validated. - Cancellation flows everywhere: every transport, tool, hook, and process execution accepts a
CancellationTokenand terminates with an explicit outcome. - Errors go back to the model by default: invalid tool arguments, policy denials, and handler exceptions normally become structured error tool results so the model can recover instead of crashing the host.
- Trust boundaries are explicit: built-in tools enforce an action policy, JavaScript tools run in a Jint sandbox (and worker process for model-written code), and C# scripts / native plugins require explicit host trust.
- Everything can be tested offline: both stacks support deterministic scripted models (
FauxAgentModels,ScriptedModels, and the fixture host used across all 62 practical agent samples) so CI and local development need no provider credentials.