This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.go
More file actions
397 lines (321 loc) · 9.38 KB
/
Copy pathsdk.go
File metadata and controls
397 lines (321 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package sdk
import (
"context"
"time"
logger "github.com/xraph/go-utils/log"
"github.com/xraph/go-utils/metrics"
)
// SDK version.
const Version = "2.0.0"
// Generator provides the main entry point for AI operations.
type Generator interface {
// Generate performs simple text generation
Generate(ctx context.Context) *GenerateBuilder
// Stream performs streaming generation with reasoning steps
Stream(ctx context.Context) *StreamBuilder
// NewAgent creates a new agent
NewAgent(name string) *AgentBuilder
// NewWorkflow creates a new workflow
NewWorkflow(name string) *WorkflowBuilder
}
// Note: GenerateObjectBuilder is a generic type and should be used directly:
// sdk.NewGenerateObjectBuilder[YourType](ctx, llm, logger, metrics)
// Options configures the SDK.
type Options struct {
// LLM configuration
DefaultProvider string
DefaultModel string
APIKey map[string]string // provider -> key
// Observability
Logger logger.Logger
Metrics metrics.Metrics
Tracer Tracer
// Storage
StateStore StateStore
VectorStore VectorStore
CacheStore CacheStore
// Limits
DefaultTimeout time.Duration
MaxRetries int
RateLimit RateLimitConfig
// Cost management
CostManager CostManager
// Safety
Guardrails []Guardrail
// Health
HealthManager metrics.HealthManager
}
// Tracer interface for distributed tracing.
type Tracer interface {
StartSpan(ctx context.Context, name string) (context.Context, Span)
}
// Span represents a trace span.
type Span interface {
End()
SetAttribute(key string, value any)
SetError(err error)
Context() context.Context
}
// StateStore interface for agent state persistence.
type StateStore interface {
// Save saves the agent state
Save(ctx context.Context, state *AgentState) error
// Load loads the agent state
Load(ctx context.Context, agentID, sessionID string) (*AgentState, error)
// Delete deletes the agent state
Delete(ctx context.Context, agentID, sessionID string) error
// List lists all sessions for an agent
List(ctx context.Context, agentID string) ([]string, error)
}
// VectorStore interface for embeddings and semantic search.
type VectorStore interface {
Upsert(ctx context.Context, vectors []Vector) error
Query(ctx context.Context, vector []float64, limit int, filter map[string]any) ([]VectorMatch, error)
Delete(ctx context.Context, ids []string) error
}
// Vector represents a vector with metadata.
type Vector struct {
ID string
Values []float64
Metadata map[string]any
}
// VectorMatch represents a search result.
type VectorMatch struct {
ID string
Score float64
Metadata map[string]any
}
// CacheStore interface for caching.
type CacheStore interface {
Get(ctx context.Context, key string) ([]byte, bool, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Clear(ctx context.Context) error
}
// RateLimitConfig configures rate limiting.
type RateLimitConfig struct {
RequestsPerMinute int
TokensPerMinute int
BurstSize int
}
// CostManager interface for cost tracking and optimization.
type CostManager interface {
RecordUsage(ctx context.Context, usage Usage) error
GetInsights() CostInsights
CheckBudget(ctx context.Context) error
}
// Usage represents resource usage.
type Usage struct {
Provider string
Model string
InputTokens int
OutputTokens int
TotalTokens int
Cost float64
Timestamp time.Time
}
// CostInsights provides cost analytics.
type CostInsights struct {
CostToday float64
CostThisMonth float64
ProjectedMonthly float64
CacheHitRate float64
PotentialSavings float64
TopExpensiveModels []ModelCost
}
// ModelCost represents cost by model.
type ModelCost struct {
Model string
Cost float64
Calls int64
}
// Guardrail interface for safety checks.
type Guardrail interface {
Name() string
ValidateInput(ctx context.Context, input string) error
ValidateOutput(ctx context.Context, output string) error
}
// GenerateResult represents a generation result.
// Renamed from Result for clarity.
type GenerateResult struct {
Content string
Metadata map[string]any
Usage *Usage
FinishReason string
ToolCalls []ToolOutput
Reasoning []string
Error error
}
// Result is an alias for GenerateResult for backward compatibility.
// Deprecated: Use GenerateResult instead.
type Result = GenerateResult
// ToolOutput represents a tool call from the LLM.
// Renamed from ToolCallResult for clarity.
type ToolOutput struct {
Name string
Arguments map[string]any
}
// ToolCallResult is an alias for ToolOutput for backward compatibility.
// Deprecated: Use ToolOutput instead.
type ToolCallResult = ToolOutput
// F creates a log field. This is a convenience wrapper around logger.F.
// Deprecated: Use logger.String, logger.Int, logger.Bool, etc. directly for type safety.
func F(key string, value any) logger.Field {
return logger.F(key, value)
}
// SDK provides a unified entry point for all AI operations.
// It implements the Generator interface and manages all SDK dependencies.
type SDK struct {
// LLM configuration
llmManager LLMManager
defaultProvider string
defaultModel string
// Observability
logger logger.Logger
metrics metrics.Metrics
tracer Tracer
// Storage
stateStore StateStore
vectorStore VectorStore
cacheStore CacheStore
// Limits
defaultTimeout time.Duration
maxRetries int
rateLimit RateLimitConfig
// Cost management
costManager CostManager
// Safety
guardrails []Guardrail
// Health
healthManager metrics.HealthManager
}
// New creates a new SDK instance with the provided options.
func New(llmManager LLMManager, opts *Options) *SDK {
s := &SDK{
llmManager: llmManager,
defaultTimeout: 30 * time.Second,
maxRetries: 3,
}
if opts != nil {
s.defaultProvider = opts.DefaultProvider
s.defaultModel = opts.DefaultModel
s.logger = opts.Logger
s.metrics = opts.Metrics
s.tracer = opts.Tracer
s.stateStore = opts.StateStore
s.vectorStore = opts.VectorStore
s.cacheStore = opts.CacheStore
s.costManager = opts.CostManager
s.guardrails = opts.Guardrails
s.healthManager = opts.HealthManager
if opts.DefaultTimeout > 0 {
s.defaultTimeout = opts.DefaultTimeout
}
if opts.MaxRetries > 0 {
s.maxRetries = opts.MaxRetries
}
s.rateLimit = opts.RateLimit
}
return s
}
// Generate creates a new GenerateBuilder for text generation.
func (s *SDK) Generate(ctx context.Context) *GenerateBuilder {
builder := NewGenerateBuilder(ctx, s.llmManager, s.logger, s.metrics)
// Apply defaults
if s.defaultProvider != "" {
builder.WithProvider(s.defaultProvider)
}
if s.defaultModel != "" {
builder.WithModel(s.defaultModel)
}
if s.defaultTimeout > 0 {
builder.WithTimeout(s.defaultTimeout)
}
return builder
}
// Stream creates a new StreamBuilder for streaming generation.
func (s *SDK) Stream(ctx context.Context) *StreamBuilder {
builder := NewStreamBuilder(ctx, s.llmManager, s.logger, s.metrics)
// Apply defaults
if s.defaultProvider != "" {
builder.WithProvider(s.defaultProvider)
}
if s.defaultModel != "" {
builder.WithModel(s.defaultModel)
}
if s.defaultTimeout > 0 {
builder.WithTimeout(s.defaultTimeout)
}
return builder
}
// NewAgent creates a new AgentBuilder for building agents.
func (s *SDK) NewAgent(name string) *AgentBuilder {
builder := NewAgentBuilder().
WithName(name).
WithLLMManager(s.llmManager).
WithLogger(s.logger).
WithMetrics(s.metrics)
// Apply defaults
if s.defaultProvider != "" {
builder.WithProvider(s.defaultProvider)
}
if s.defaultModel != "" {
builder.WithModel(s.defaultModel)
}
if s.stateStore != nil {
builder.WithStateStore(s.stateStore)
}
return builder
}
// NewWorkflow creates a new WorkflowBuilder for building workflows.
func (s *SDK) NewWorkflow(name string) *WorkflowBuilder {
return NewWorkflowBuilder().
WithName(name).
WithLogger(s.logger).
WithMetrics(s.metrics)
}
// GenerateObject creates a new GenerateObjectBuilder for structured output generation.
// Note: This is a convenience method; for full type safety, use NewGenerateObjectBuilder directly.
func (s *SDK) GenerateObject(ctx context.Context) *GenerateObjectBuilder[map[string]any] {
builder := NewGenerateObjectBuilder[map[string]any](ctx, s.llmManager, s.logger, s.metrics)
if s.defaultProvider != "" {
builder.WithProvider(s.defaultProvider)
}
if s.defaultModel != "" {
builder.WithModel(s.defaultModel)
}
if s.defaultTimeout > 0 {
builder.WithTimeout(s.defaultTimeout)
}
return builder
}
// MultiModal creates a new MultiModalBuilder for multi-modal generation.
func (s *SDK) MultiModal(ctx context.Context) *MultiModalBuilder {
builder := NewMultiModalBuilder(ctx, s.llmManager, s.logger, s.metrics)
if s.defaultModel != "" {
builder.WithModel(s.defaultModel)
}
return builder
}
// LLMManager returns the SDK's LLM manager.
func (s *SDK) LLMManager() LLMManager {
return s.llmManager
}
// StateStore returns the SDK's state store.
func (s *SDK) StateStore() StateStore {
return s.stateStore
}
// VectorStore returns the SDK's vector store.
func (s *SDK) VectorStore() VectorStore {
return s.vectorStore
}
// Logger returns the SDK's logger.
func (s *SDK) Logger() logger.Logger {
return s.logger
}
// Metrics returns the SDK's metrics.
func (s *SDK) Metrics() metrics.Metrics {
return s.metrics
}
// Ensure SDK implements Generator interface.
var _ Generator = (*SDK)(nil)