mirror of
https://github.com/mark3labs/kit.git
synced 2026-06-14 03:30:26 +00:00
6d4e8bcec5
- Add StreamCallback parameter to compaction.Compact() for streaming text deltas - Update generateSummary() to use fantasy.Agent.Stream() when callback provided - Fix compactSplitTurn() to stream both history and turn prefix summaries - Add SDK event subscription in CompactConversation() goroutine - Update UI to handle streaming compaction like regular assistant messages - Compaction summaries now stream word-by-word instead of appearing all at once Fixes issue where compaction would show incomplete context (e.g. only 'nce') by ensuring both history summary and turn prefix are streamed to the UI.
KIT SDK
The KIT SDK (pkg/kit) lets you embed Kit's full agent capabilities — LLM interactions, tool execution, session management, streaming, hooks — into any Go application.
Installation
go get github.com/mark3labs/kit
Basic Usage
package main
import (
"context"
"fmt"
"log"
kit "github.com/mark3labs/kit/pkg/kit"
)
func main() {
ctx := context.Background()
// Create Kit instance with default configuration
host, err := kit.New(ctx, nil)
if err != nil {
log.Fatal(err)
}
defer func() { _ = host.Close() }()
// Send a prompt
response, err := host.Prompt(ctx, "What is 2+2?")
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}
Configuration
The SDK behaves identically to the CLI:
- Loads configuration from
~/.kit.ymlby default - Creates default configuration if none exists
- Respects all environment variables (
KIT_*) - Uses the same defaults as the CLI
Options
You can override specific settings:
host, err := kit.New(ctx, &kit.Options{
Model: "ollama/llama3", // Override model
SystemPrompt: "You are a helpful bot", // Override system prompt
ConfigFile: "/path/to/config.yml", // Use specific config file
MaxSteps: 10, // Override max steps
Streaming: true, // Enable streaming
Quiet: true, // Suppress debug output
// Session options
SessionPath: "./session.jsonl", // Open specific session
Continue: true, // Resume most recent session
NoSession: true, // Ephemeral mode
// Tool options
Tools: []kit.Tool{kit.NewBashTool()}, // Replace default tool set
ExtraTools: []kit.Tool{myTool}, // Add alongside defaults
// Compaction
AutoCompact: true, // Auto-compact near context limit
})
Advanced Usage
With Tool Callbacks
Monitor tool execution in real-time:
unsub := host.OnToolCall(func(e kit.ToolCallEvent) {
fmt.Printf("Calling tool: %s\n", e.ToolName)
})
defer unsub()
unsub2 := host.OnToolResult(func(e kit.ToolResultEvent) {
if e.IsError {
fmt.Printf("Tool %s failed: %s\n", e.ToolName, e.Result)
} else {
fmt.Printf("Tool %s succeeded\n", e.ToolName)
}
})
defer unsub2()
unsub3 := host.OnStreaming(func(e kit.MessageUpdateEvent) {
fmt.Print(e.Chunk)
})
defer unsub3()
response, err := host.Prompt(
ctx,
"List files in the current directory",
)
Session Management
Maintain conversation context:
// First message
host.Prompt(ctx, "My name is Alice")
// Second message (remembers context)
response, _ := host.Prompt(ctx, "What's my name?")
// Response: "Your name is Alice"
// Clear conversation history
host.ClearSession()
Re-exported Types
The SDK re-exports types so you don't need direct internal imports:
// Message types
kit.Message, kit.MessageRole, kit.ContentPart
kit.TextContent, kit.ReasoningContent, kit.ToolCall, kit.ToolResult, kit.Finish
kit.RoleUser, kit.RoleAssistant, kit.RoleTool, kit.RoleSystem
// LLM types — concrete Kit-owned structs, no external library dependency
kit.LLMMessage // {Role LLMMessageRole, Content string}
kit.LLMMessageRole // "user" | "assistant" | "system" | "tool"
kit.LLMUsage // {InputTokens, OutputTokens, TotalTokens, ...}
kit.LLMResponse // {Content, FinishReason, Usage}
kit.LLMFilePart // {Filename, Data []byte, MediaType}
// Conversion helpers
msgs := kit.ConvertToLLMMessages(&msg) // SDK Message → []LLMMessage
msg := kit.ConvertFromLLMMessage(lMsg) // LLMMessage → SDK Message
API Reference
Types
Kit- Main SDK typeOptions- Configuration optionsMessage- Conversation message with typed content partsTool- Agent tool interfaceTurnResult- Full result from a prompt including usage stats
Key Methods
New(ctx, opts)- Create new Kit instancePrompt(ctx, message)- Send message and get response stringPromptResult(ctx, message)- Send message and get full TurnResultPromptWithOptions(ctx, message, opts)- Prompt with per-call optionsSteer(ctx, instruction)- System-level steeringFollowUp(ctx, text)- Continue without new user inputSetModel(ctx, model)- Switch model at runtimeGetModelString()- Get current model stringGetModelInfo()- Get model capabilities and limitsClearSession()- Clear conversation historyGetSessionPath()- Get session file pathGetSessionID()- Get session UUIDClose()- Clean up resources
Environment Variables
All CLI environment variables work with the SDK:
KIT_MODEL- Override modelANTHROPIC_API_KEY- Anthropic API keyOPENAI_API_KEY- OpenAI API keyGEMINI_API_KEY- Google API key- etc.
License
Same as KIT CLI