AI & Agents (pkg/ai)
Import paths:
github.com/krewire/krewire/pkg/aigithub.com/krewire/krewire/pkg/ai/providers
The ai package provides a unified, provider-agnostic Go SDK for integrating Large Language Models into Krewire web apps, background workers, and autonomous agent workflows with zero external runtime dependencies.
1. Supported Providers
The SDK supports leading commercial and local open-source LLM providers out of the box:
- Google Gemini (
providers.NewGemini(apiKey)) - Anthropic Claude (
providers.NewAnthropic(apiKey)) - OpenAI & Compatible (
providers.NewOpenAI(apiKey, opts...)โ works with OpenAI, Groq, DeepSeek, vLLM) - Ollama (
providers.NewOllama(opts...)โ local Llama 3, Qwen, Mistral, DeepSeek) - Mock Provider (
providers.NewMock(content)โ 100% offline unit test fixture)
2. Basic Chat & Prompt Completion
package main
import (
"context"
"fmt"
"os"
"github.com/krewire/krewire/pkg/ai"
"github.com/krewire/krewire/pkg/ai/providers"
)
func main() {
provider := providers.NewOpenAI(os.Getenv("OPENAI_API_KEY"))
client := ai.NewClient(provider, "gpt-4o").
WithSystem("You are a helpful software architecture assistant.")
// Simple completion
answer, err := client.Complete(context.Background(), "Explain Krewire downward dependencies.")
if err != nil {
panic(err)
}
fmt.Println(answer)
}
3. Type-Safe Structured Outputs (GenerateObject[T])
Extract strictly typed Go structs directly from LLM completions using reflection and JSON schema generation:
type UserReport struct {
Summary string `json:"summary" description:"Executive summary of the user issue"`
Severity string `json:"severity" description:"low, medium, high, critical"`
Categories []string `json:"categories"`
Actionable bool `json:"actionable"`
}
report, err := ai.GenerateObject[UserReport](
ctx,
provider,
"claude-3-5-sonnet-20241022",
"Analyze error logs: database timeout on pool exhaustion",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Severity: %s (Actionable: %v)\n", report.Severity, report.Actionable)
4. Reflection-Based Tool & Function Calling
Register standard Go functions as LLM tools without manually writing JSON Schema definitions:
type SearchQuery struct {
Keywords []string `json:"keywords" description:"Search keywords"`
Limit int `json:"limit" description:"Maximum results to return"`
}
func searchDocs(ctx context.Context, args SearchQuery) ([]string, error) {
// Query local documentation index...
return []string{"Result 1", "Result 2"}, nil
}
// Convert Go func into a validated AI Tool
searchTool, err := ai.NewTool("search_docs", "Search documentation index", searchDocs)
// Pass tool to client or completion request
client.WithTools(searchTool)
5. Streaming & Server-Sent Events (SSE)
Stream tokens in real-time to web components and HTTP route handlers:
func handleStream(w http.ResponseWriter, r *http.Request) {
stream, err := client.Stream(r.Context(), []ai.Message{
ai.UserMessage("Stream an explanation of Go concurrency"),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Flushes SSE events: event: delta, event: tool_call, event: done
_ = ai.ServeSSE(w, stream)
}
6. Autonomous AI Agent Engine
While pkg/ai provides low-level LLM abstractions, krew is Krewire's autonomous AI agent platform:
- Standalone CLI (
cmd/krew): Dedicated terminal binary (krew run,krew session,krew exec,krew doctor). - HTTP/SSE Transport Daemon:
transport.NewHTTPServersecured with bearer tokens from Krewire Auth and sandboxed tenant workspaces. - Subagent Delegation: Automated task decomposition with structured execution summaries.
- Tool-Level RBAC & Audit: Integration with Krewire Auth evaluating
ToolPermissionpolicies and generating forensic audit logs. - MCP Integration: Native support for the Model Context Protocol.
7. AI-Powered Virtual Workspace (Krewire Dash)
Krewire Dash is the web application platform providing an AI-native virtual workspace (available at dash.krewire.com):
- Monaco Code Editor: Syntax highlighting and real-time buffer editing.
- Interactive PTY Terminal: xterm.js terminal connected over secure WebSockets.
- Embedded AI Pair Programmer: Connects to Krew via streaming SSE, performing AST semantic searches and generating interactive diff proposals with one-click Accept/Reject controls.
- Collaborative Presence: Real-time presence roster and multi-user cursor sync.