Documentation
¶
Overview ¶
Package pithsdk is a minimal, OpenAI Agents-style SDK for Go app developers. It wraps github.com/chinudotdev/pith primitives so you can define an agent, run it, and get text back without wiring gateways, ModelDescriptors, or EventBus.
Quick start:
client, _ := pithsdk.NewClient(pithsdk.ClientConfig{APIKey: os.Getenv("OPENAI_API_KEY")})
agent, _ := pithsdk.NewAgent(pithsdk.AgentConfig{
Instructions: "You are helpful.",
Model: "gpt-4o-mini",
})
session, _ := client.NewSession(agent)
result, _ := session.Run(ctx, "Hello!")
Import with the recommended alias:
import pithsdk "github.com/chinudotdev/pith-sdk"
See the examples/ directory and https://github.com/chinudotdev/pith-sdk for more.
Index ¶
- type AfterToolContext
- type AfterToolResult
- type Agent
- type AgentConfig
- type BeforeToolContext
- type BeforeToolResult
- type Client
- type ClientConfig
- type Hooks
- type MessageSummary
- type ModelPreset
- type ModelSettings
- type ProviderRegistration
- type RunOption
- type RunOptions
- type RunResult
- type Session
- type SessionOption
- type TextChunk
- type Tool
- type ToolContext
- type TurnContext
- type UsageSummary
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AfterToolContext ¶ added in v0.2.0
type AfterToolContext struct {
// RunID is the unique identifier for the current run.
RunID string
// SessionID is the unique identifier for the current session.
SessionID string
// AgentName is the name of the agent running this tool.
AgentName string
// ToolName is the name of the tool that was called.
ToolName string
// CallID is the provider-assigned tool call identifier.
CallID string
// Args is the parsed tool arguments.
Args map[string]any
// Result is the text output from the tool.
Result string
// Error is non-nil when the tool returned an error.
Error error
}
AfterToolContext is passed to the AfterToolCall hook.
type AfterToolResult ¶ added in v0.2.0
type AfterToolResult struct {
// OverrideResult replaces the tool's output text when non-empty.
OverrideResult string
}
AfterToolResult controls tool result override from the AfterToolCall hook.
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent is a specialist definition: name, instructions, model, tools, and settings. Agents are immutable configuration; create a Session to run them.
func NewAgent ¶
func NewAgent(cfg AgentConfig) (*Agent, error)
NewAgent creates an agent definition.
type AgentConfig ¶
type AgentConfig struct {
// Name is an optional display name for the agent.
Name string
// Instructions is the system prompt sent to the model.
Instructions string
// Model is a bare ID (e.g. "gpt-4o-mini") or provider/model (e.g. "anthropic/claude-...").
Model string
// Tools are custom tools available during runs.
Tools []Tool
// Settings overrides client-level generation defaults for this agent.
Settings *ModelSettings
}
AgentConfig configures a new Agent.
type BeforeToolContext ¶ added in v0.2.0
type BeforeToolContext struct {
// RunID is the unique identifier for the current run.
RunID string
// SessionID is the unique identifier for the current session.
SessionID string
// AgentName is the name of the agent running this tool.
AgentName string
// ToolName is the name of the tool being called.
ToolName string
// CallID is the provider-assigned tool call identifier.
CallID string
// Args is the parsed tool arguments.
Args map[string]any
}
BeforeToolContext is passed to the BeforeToolCall hook.
type BeforeToolResult ¶ added in v0.2.0
type BeforeToolResult struct {
// Block prevents the tool from executing when true.
Block bool
// Reason is the message returned when blocking a tool call.
Reason string
}
BeforeToolResult controls tool execution from the BeforeToolCall hook.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client holds gateway configuration, credentials, and defaults for running agents.
func NewClient ¶
func NewClient(cfg ClientConfig) (*Client, error)
NewClient creates a client with a built-in OpenAI-compatible provider. An OpenAI API key is optional at construction time; it is required when using the default OpenAI provider at run time.
func NewClientFromGateway ¶
func NewClientFromGateway(gw *gateway.LLMGateway) *Client
NewClientFromGateway wraps a pre-wired gateway (e.g. for testing or custom setups).
func (*Client) NewSession ¶
func (c *Client) NewSession(agent *Agent, opts ...SessionOption) (*Session, error)
NewSession creates a session for the given agent definition.
func (*Client) RegisterProvider ¶
func (c *Client) RegisterProvider(reg ProviderRegistration) error
RegisterProvider registers a custom provider, its models, and credentials.
type ClientConfig ¶
type ClientConfig struct {
// APIKey is the OpenAI-compatible API key. When empty, OPENAI_API_KEY is used at run time.
APIKey string
// DefaultProvider is the provider ID for bare model strings (default "openai").
DefaultProvider string
// DefaultModel is used when an Agent has no Model set (default "gpt-4o-mini").
DefaultModel string
// DefaultSettings applies generation defaults to all agents unless overridden.
DefaultSettings *ModelSettings
}
ClientConfig configures a new Client.
type Hooks ¶ added in v0.2.0
type Hooks struct {
// BeforeToolCall is called before each tool execution.
// Return a result with Block=true to prevent execution.
BeforeToolCall func(ctx BeforeToolContext) (*BeforeToolResult, error)
// AfterToolCall is called after each tool execution.
// Use OverrideResult to replace the tool's output.
AfterToolCall func(ctx AfterToolContext) (*AfterToolResult, error)
// ShouldStopAfterTurn is called after each agent turn.
// Return true to stop the run after this turn.
ShouldStopAfterTurn func(ctx TurnContext) bool
}
Hooks are lifecycle callbacks for a single Session.Run call. All hooks are optional; nil hooks are skipped.
type MessageSummary ¶
type MessageSummary struct {
// Role is the message role (e.g. "user", "assistant", "toolResult").
Role string
// Text is the message text content.
Text string
}
MessageSummary is a simplified view of a transcript message.
type ModelPreset ¶
type ModelPreset struct {
// ID is the model identifier used in provider/model strings.
ID string
// Name is an optional display name; defaults to ID.
Name string
// BaseURL is an optional API base URL; the provider may use its own default.
BaseURL string
// ContextWindow is the model context size; zero uses the SDK default.
ContextWindow int
// MaxTokens is the default max output tokens; zero uses the SDK default.
MaxTokens int
}
ModelPreset describes a model to register in the gateway catalog.
type ModelSettings ¶
type ModelSettings struct {
// Temperature controls randomness. Nil leaves the provider default.
Temperature *float64
// MaxTokens caps output tokens. Nil leaves the provider default.
MaxTokens *int
}
ModelSettings configures generation parameters for an agent run.
type ProviderRegistration ¶
type ProviderRegistration struct {
// Provider implements gateway.ProviderPort for the custom API.
Provider gateway.ProviderPort
// Credentials — set exactly one of the following:
// APIKey is a static API key string.
APIKey string
// APIKeyEnv names an environment variable holding the API key (e.g. "ANTHROPIC_API_KEY").
APIKeyEnv string
// Credential is a custom resolver returning the API key for the provider ID.
Credential func(providerID string) (string, error)
// Models lists model presets to register in the gateway catalog.
Models []ModelPreset
}
ProviderRegistration registers a custom LLM provider and its models on the client.
type RunOption ¶
type RunOption func(*RunOptions)
RunOption configures a Session.Run call.
func WithContext ¶
WithContext sets run-scoped local dependencies available as ToolContext.Local.
func WithInstructions ¶
WithInstructions overrides the agent's system prompt for this run only.
func WithMaxTurns ¶
WithMaxTurns limits how many agent loop turns a single Run may take. Zero uses the SDK default of 10.
func WithRunID ¶ added in v0.2.0
WithRunID sets an explicit run identifier. When omitted, a UUID is generated per Run.
func WithStream ¶
WithStream registers a callback for streaming assistant text deltas during the run. When nil (default), Run blocks until the full response is ready.
type RunOptions ¶
type RunOptions struct {
// RunID overrides the auto-generated run identifier. Empty triggers auto-generation.
RunID string
// Context holds run-scoped local dependencies exposed as ToolContext.Local.
Context any
// Instructions overrides the agent system prompt for this run only.
Instructions string
// Stream receives assistant text deltas during the run; nil blocks until complete.
Stream func(chunk TextChunk)
// MaxTurns limits tool-calling loop iterations for this run. Zero uses defaultMaxTurns (10).
MaxTurns int
// Hooks are lifecycle callbacks for this run.
Hooks *Hooks
}
RunOptions configures a single run. Apply via RunOption functions.
type RunResult ¶
type RunResult struct {
// RunID is the unique identifier for this run, auto-generated unless overridden via WithRunID.
RunID string
// Text is the final assistant response text.
Text string
// Messages is the session transcript after this run, as simplified summaries.
Messages []MessageSummary
// Usage reports token usage from the last assistant response, if available.
Usage *UsageSummary
}
RunResult is the outcome of a single Session.Run call.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session runs an agent and holds its transcript across multiple Run calls.
func (*Session) Messages ¶
func (s *Session) Messages() []MessageSummary
Messages returns the current session transcript.
func (*Session) Reset ¶
func (s *Session) Reset()
Reset clears the session transcript and queued messages.
func (*Session) Run ¶
Run sends input to the agent and returns the final text result.
Concurrent Run calls on the same Session are not supported; serialize calls or use separate sessions.
When ShouldStopAfterTurn returns true, Run stops gracefully and returns nil error along with partial results accumulated so far.
type SessionOption ¶ added in v0.2.0
type SessionOption func(*sessionOpts)
SessionOption configures session creation via Client.NewSession.
func WithSessionID ¶ added in v0.2.0
func WithSessionID(id string) SessionOption
WithSessionID sets an explicit session identifier. When omitted, a UUID is generated.
type TextChunk ¶
type TextChunk struct {
// Delta is the incremental text from the latest EventTextDelta.
Delta string
// Text is the accumulated assistant text so far.
Text string
}
TextChunk is a streaming text delta from the assistant response.
type Tool ¶
type Tool struct {
// contains filtered or unexported fields
}
Tool is an opaque tool definition. Create with NewTool, NewDynamicTool, or RawTool.
func NewDynamicTool ¶ added in v0.2.0
func NewDynamicTool(name, description string, schema map[string]any, fn func(context.Context, map[string]any) (string, error)) Tool
NewDynamicTool creates a schema-driven tool with untyped map arguments. The handler receives the active Session.Run context at invoke time. Errors returned by the handler are converted to text in the wire layer, consistent with NewTool.
type ToolContext ¶
type ToolContext struct {
// Run is the context for the current Session.Run call.
Run context.Context
// RunID is the unique identifier for the current run.
RunID string
// SessionID is the unique identifier for the current session.
SessionID string
// Local holds run-scoped dependencies from WithContext; not sent to the model.
Local any
// ToolName is the name of the tool being invoked.
ToolName string
// CallID is the provider-assigned tool call identifier.
CallID string
}
ToolContext is passed to tool handlers during execution.
type TurnContext ¶ added in v0.2.0
type TurnContext struct {
// RunID is the unique identifier for the current run.
RunID string
// SessionID is the unique identifier for the current session.
SessionID string
// AgentName is the name of the running agent.
AgentName string
// TurnNumber is the 1-based turn count within the current run.
TurnNumber int
}
TurnContext is passed to the ShouldStopAfterTurn hook.
type UsageSummary ¶
type UsageSummary struct {
// Input is the number of input/prompt tokens.
Input int
// Output is the number of output/completion tokens.
Output int
// Total is the combined token count when reported by the provider.
Total int
}
UsageSummary reports token usage from the last assistant response.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
01-hello
command
Example 01: Hello — minimal agent run with the pith-sdk.
|
Example 01: Hello — minimal agent run with the pith-sdk. |
|
02-tools
command
Example 02: Tools — agent with a custom tool the model can call.
|
Example 02: Tools — agent with a custom tool the model can call. |
|
03-multi-turn
command
Example 03: Multi-turn — conversation across multiple Session.Run calls.
|
Example 03: Multi-turn — conversation across multiple Session.Run calls. |
|
04-anthropic-provider
command
Example 04: Custom Anthropic provider via pith-sdk RegisterProvider.
|
Example 04: Custom Anthropic provider via pith-sdk RegisterProvider. |
|
05-mcp
command
05-mcp demonstrates composing local and MCP tools and running an agent session.
|
05-mcp demonstrates composing local and MCP tools and running an agent session. |
|
05-mcp/echo-server
command
Minimal stdio MCP server with an echo tool.
|
Minimal stdio MCP server with an echo tool. |
|
Package mcp discovers tools from MCP (Model Context Protocol) servers and converts them to pith-sdk Tools for use in agent sessions.
|
Package mcp discovers tools from MCP (Model Context Protocol) servers and converts them to pith-sdk Tools for use in agent sessions. |