Documentation
¶
Overview ¶
Package pi runs pi-ai, the model layer of the pi agent harness, on moejs, a JavaScript runtime in pure Go (docs/adr/0024-pi-moejs-agent-runtime.md). The bundle (engine.js, built from js/ by `mise run ai:bundle`) speaks the model protocols; every HTTP request it makes is sent here, through the caller's client. It must not import package ai.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AgentRequest ¶
type AgentRequest struct {
Request
Messages []json.RawMessage `json:"messages"`
Answers map[string]Answer `json:"answers,omitempty"`
Tools []Tool `json:"tools"`
MaxTurns int `json:"maxTurns"`
LimitNote string `json:"limitNote"`
}
AgentRequest is one turn of a conversation with the service Request names (its Prompt unused): Messages are pi messages (pi-ai's Message, as JSON), ending with the question. Tools are offered to the model; past MaxTurns rounds of calls it answers from what it found, its system prompt ending with LimitNote, and the last round's calls are refused. Answers, if set, resume a held round (AgentResult.Held) instead: Messages end with it, and Answers are the results of its calls, by ID, that held or ran; the others run first.
type AgentResult ¶
type AgentResult struct {
Result
Messages []json.RawMessage `json:"-"`
Raw string `json:"messages"`
Limited, Calls bool
Last int
Held, Deferred []string
}
AgentResult is how a turn ended, its last reply as Result tells it. Messages are the pi messages it added, the question first. Limited: the model reached MaxTurns and the last message is its answer after it; Calls: that message still calls tools. Input and Output sum every reply the turn was billed for; Last is the last request's input. Held: the turn ended at a round whose calls with these IDs wait for the user; none of its calls ran but those that were running, and Deferred were held back with them. Messages then end with the round and every call's result, held ones too.
func Agent ¶
func Agent(ctx context.Context, client *http.Client, req AgentRequest, hooks Hooks) (AgentResult, error)
Agent runs one turn; it returns when the turn ended, or when ctx did (ctx's error then) and the tools it ran have returned.
type Hooks ¶
type Hooks struct {
Emit func(kind, text string)
Args func(args json.RawMessage) (json.RawMessage, error)
Gate func(ctx context.Context, id, name string, args json.RawMessage) (hold bool)
Tool func(ctx context.Context, id, name string, args json.RawMessage) (output json.RawMessage, errMsg string, interrupt bool)
Refused func(id, name string, args json.RawMessage, errMsg string)
Mask func(body string) (string, error)
}
Hooks are what a run calls back on its goroutine, Tool on one of its own per call. Emit gets each text and reasoning delta. Args rewrites a call's arguments before they are validated (an error refuses the call); Gate, given each valid call of a round before any of them runs, holds one for the user (true): the round then stops. Tool runs a call, its output JSON or an error message, or holds it as it runs (interrupt); Refused tells of a call that never reached Tool. Mask, called on a goroutine of its own, rewrites each request body before the request is made, so what net/http sends again on a new connection is rewritten too (docs/adr/0025): an error fails the run, the request unsent.
type JudgeRequest ¶
type JudgeRequest struct {
URL string `json:"url"`
APIKey string `json:"apiKey"`
Body json.RawMessage `json:"body"`
}
JudgeRequest is one judge API call: Body, the API's JSON request, is posted to URL with the key as a bearer token (none when APIKey is "").
type JudgeResult ¶
type JudgeResult struct {
Body json.RawMessage
Status int
Transport error
}
JudgeResult is a judge call's outcome: Body the reply's JSON when the service answered 200, else Status its last HTTP status (0 if none answered: Transport is then what the request failed with, nil when the reply was unreadable).
func Judge ¶
func Judge(ctx context.Context, client *http.Client, req JudgeRequest, maxBody int64, hooks Hooks) (JudgeResult, error)
Judge posts one judge request through pi-ai's classifier transport, which retries as the SDKs do; of hooks only Mask is called. A reply over maxBody bytes fails to read.
type Request ¶
type Request struct {
API string `json:"api"`
BaseURL string `json:"baseUrl"`
Model string `json:"model"`
APIKey string `json:"apiKey"`
System string `json:"system"`
Prompt string `json:"prompt"`
Effort string `json:"effort"`
MaxTokens int64 `json:"maxTokens"`
ThinkingBudget int64 `json:"thinkingBudget"`
Session string `json:"session"`
}
Request is one reply to a one-off prompt. API is the pi-ai API that speaks the service's protocol: openai-completions, anthropic-messages, openai-responses or openai-codex-responses. APIKey "" sends no credential. Effort is "", "low", "medium" or "high". MaxTokens caps the reply (0: none where the API allows it); ThinkingBudget is anthropic-messages' thinking budget for Effort. Session, if set, keys the service's prompt cache, which is then used: the turns of one conversation share it.
type Result ¶
type Result struct {
StopReason string `json:"stopReason"`
Error string `json:"error"`
Code string `json:"code"`
Input, Output int
Cached int
Status int `json:"-"`
Transport error `json:"-"`
}
Result is how a reply ended. StopReason is pi-ai's: "stop" is a whole reply, "length" one cut off at the output limit, "error" a failure (Error says what pi-ai saw); or "blocked", a reply the service withheld, and "incomplete", one it ended early for another reason. Code is the error code the service last answered with (an HTTP error body's or a failed response's), Status the last HTTP status a request got, 0 if none answered; Transport the error a request failed with before any answer. Cached is the part of Input the service read from its prompt cache.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
Strict bool `json:"strict"`
}
Tool is a tool offered to the model; Parameters is its JSON schema, which Strict asks the service to hold calls to (Responses).