-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
584 lines (521 loc) · 18.2 KB
/
main.go
File metadata and controls
584 lines (521 loc) · 18.2 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Package main demonstrates comprehensive usage of the AgentsService
// from the go-contextforge SDK. This example highlights A2A (Agent-to-Agent)
// agent management including unique features like skip/limit pagination and
// agent invocation. Uses a mock HTTP server for self-contained demonstration.
//
// Run: go run examples/agents/main.go
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/leefowlercu/go-contextforge/contextforge"
)
func main() {
// Create mock server with all necessary endpoints
mux := http.NewServeMux()
setupMockEndpoints(mux)
server := httptest.NewServer(mux)
defer server.Close()
fmt.Println("=== ContextForge SDK - Agents Service Example ===")
// Step 1: Authentication
fmt.Println("1. Authenticating...")
token := authenticate(server.URL)
fmt.Printf(" ✓ Obtained JWT token: %s...\n\n", token[:20])
// Step 2: Create client
// To use a real ContextForge instance, replace server.URL with:
// "https://your-contextforge-instance.com"
client, err := contextforge.NewClient(nil, server.URL, token)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Step 3: Create a basic agent
fmt.Println("2. Creating a basic A2A agent...")
// Note: Create uses snake_case fields (endpoint_url, agent_type, team_id)
newAgent := &contextforge.AgentCreate{
Name: "data-processor",
EndpointURL: "https://agent1.example.com/a2a",
Description: contextforge.String("Processes and transforms data records"),
AgentType: "generic",
ProtocolVersion: "1.0",
Capabilities: map[string]any{
"streaming": true,
"batch": true,
},
Config: map[string]any{
"timeout": 30,
"retries": 3,
},
Tags: []string{"data", "processing"},
}
createdAgent1, resp, err := client.Agents.Create(ctx, newAgent, nil)
if err != nil {
log.Fatalf("Failed to create agent: %v", err)
}
fmt.Printf(" ✓ Created agent: %s (ID: %s)\n", createdAgent1.Name, createdAgent1.ID)
fmt.Printf(" ✓ Endpoint: %s\n", createdAgent1.EndpointURL)
fmt.Printf(" ✓ Type: %s, Protocol: %s\n", createdAgent1.AgentType, createdAgent1.ProtocolVersion)
fmt.Printf(" ✓ Enabled: %v, Reachable: %v\n", createdAgent1.Enabled, createdAgent1.Reachable)
fmt.Printf(" ✓ Rate limit: %d/%d remaining\n\n", resp.Rate.Remaining, resp.Rate.Limit)
// Step 4: Create an agent with authentication
fmt.Println("3. Creating an agent with API key authentication...")
authAgent := &contextforge.AgentCreate{
Name: "secure-analyzer",
EndpointURL: "https://agent2.example.com/a2a",
Description: contextforge.String("Secure data analyzer with API key authentication"),
AgentType: "analyzer",
ProtocolVersion: "1.0",
AuthType: contextforge.String("api_key"),
AuthValue: contextforge.String("secret-key-12345"), // Will be encrypted by API
Tags: []string{"security", "analysis"},
}
createdAgent2, _, err := client.Agents.Create(ctx, authAgent, nil)
if err != nil {
log.Fatalf("Failed to create agent with auth: %v", err)
}
fmt.Printf(" ✓ Created agent: %s (ID: %s)\n", createdAgent2.Name, createdAgent2.ID)
if createdAgent2.AuthType != nil {
fmt.Printf(" ✓ Auth Type: %s\n", *createdAgent2.AuthType)
fmt.Println(" ✓ Auth Value encrypted by API (not returned in response)")
}
fmt.Println()
// Step 5: Get a specific agent
fmt.Println("4. Retrieving agent by ID...")
retrievedAgent, _, err := client.Agents.Get(ctx, createdAgent1.ID)
if err != nil {
log.Fatalf("Failed to get agent: %v", err)
}
fmt.Printf(" ✓ Retrieved: %s\n", retrievedAgent.Name)
fmt.Printf(" ✓ Slug: %s\n", retrievedAgent.Slug)
if retrievedAgent.Description != nil {
fmt.Printf(" ✓ Description: %s\n", *retrievedAgent.Description)
}
if retrievedAgent.Capabilities != nil {
fmt.Printf(" ✓ Capabilities: %+v\n", retrievedAgent.Capabilities)
}
if retrievedAgent.Config != nil {
fmt.Printf(" ✓ Config: %+v\n", retrievedAgent.Config)
}
fmt.Println()
// Step 6: List all agents
fmt.Println("5. Listing all agents...")
agents, _, err := client.Agents.List(ctx, nil)
if err != nil {
log.Fatalf("Failed to list agents: %v", err)
}
fmt.Printf(" ✓ Found %d agent(s)\n", len(agents))
for i, agent := range agents {
fmt.Printf(" %d. %s (ID: %s, Enabled: %v)\n", i+1, agent.Name, agent.ID, agent.Enabled)
}
fmt.Println()
// Step 7: List agents with filtering
fmt.Println("6. Listing agents with filters...")
listOpts := &contextforge.AgentListOptions{
IncludeInactive: true,
Tags: "data,processing",
Visibility: "public",
}
filteredAgents, _, err := client.Agents.List(ctx, listOpts)
if err != nil {
log.Fatalf("Failed to list filtered agents: %v", err)
}
fmt.Printf(" ✓ Found %d agent(s) with filters\n", len(filteredAgents))
for i, agent := range filteredAgents {
fmt.Printf(" %d. %s (Tags: %v)\n", i+1, agent.Name, agent.Tags)
}
fmt.Println()
// Step 8: Demonstrate skip/limit pagination (unique to agents)
fmt.Println("7. Demonstrating skip/limit pagination...")
fmt.Println(" NOTE: Agents use skip/limit (offset-based) pagination")
fmt.Println(" This differs from cursor-based pagination in other services")
page := 1
for skip := 0; skip < 4; skip += 2 {
pageOpts := &contextforge.AgentListOptions{
Skip: skip,
Limit: 2,
}
pageAgents, _, err := client.Agents.List(ctx, pageOpts)
if err != nil {
log.Fatalf("Failed to list page: %v", err)
}
fmt.Printf(" Page %d (skip=%d, limit=2): %d agent(s)\n", page, skip, len(pageAgents))
if len(pageAgents) == 0 {
break
}
page++
}
fmt.Println()
// Step 9: Update an agent
fmt.Println("8. Updating agent...")
// Note: Update uses camelCase fields (inconsistent with Create)
updateAgent := &contextforge.AgentUpdate{
Description: contextforge.String("Advanced data processor with enhanced capabilities"),
Tags: []string{"data", "processing", "advanced"},
Capabilities: map[string]any{
"streaming": true,
"batch": true,
"parallel": true,
},
}
updatedAgent, _, err := client.Agents.Update(ctx, createdAgent1.ID, updateAgent)
if err != nil {
log.Fatalf("Failed to update agent: %v", err)
}
fmt.Printf(" ✓ Updated description: %s\n", *updatedAgent.Description)
fmt.Printf(" ✓ Updated tags: %v\n", updatedAgent.Tags)
if updatedAgent.Capabilities != nil {
fmt.Printf(" ✓ Updated capabilities: %+v\n", updatedAgent.Capabilities)
}
fmt.Println()
// Step 10: Toggle agent (disable)
fmt.Println("9. Toggling agent (disabling)...")
toggledAgent, _, err := client.Agents.Toggle(ctx, createdAgent1.ID, false)
if err != nil {
log.Fatalf("Failed to toggle agent: %v", err)
}
fmt.Printf(" ✓ Agent is now enabled: %v\n", toggledAgent.Enabled)
fmt.Printf(" ✓ Agent reachable status: %v (read-only, not affected by toggle)\n\n", toggledAgent.Reachable)
// Step 11: Toggle agent (enable)
fmt.Println("10. Toggling agent (enabling)...")
toggledAgent, _, err = client.Agents.Toggle(ctx, createdAgent1.ID, true)
if err != nil {
log.Fatalf("Failed to toggle agent: %v", err)
}
fmt.Printf(" ✓ Agent is now enabled: %v\n\n", toggledAgent.Enabled)
// Step 12: Invoke an agent
fmt.Println("11. Invoking agent with parameters...")
fmt.Println(" NOTE: Invoke uses agent name (not ID) as identifier")
invokeReq := &contextforge.AgentInvokeRequest{
Parameters: map[string]any{
"input": "sample data to process",
"options": map[string]any{
"format": "json",
"validate": true,
},
},
InteractionType: "query",
}
result, _, err := client.Agents.Invoke(ctx, createdAgent1.Name, invokeReq)
if err != nil {
// In this mock example, invoke might succeed or fail depending on mock implementation
fmt.Printf(" ⚠ Invoke returned error (expected in mock): %v\n", err)
} else {
fmt.Printf(" ✓ Invoke succeeded with result:\n")
if status, ok := result["status"]; ok {
fmt.Printf(" Status: %v\n", status)
}
if data, ok := result["result"]; ok {
fmt.Printf(" Result: %v\n", data)
}
if execTime, ok := result["execution_time"]; ok {
fmt.Printf(" Execution time: %v ms\n", execTime)
}
}
fmt.Println()
// Step 13: Error handling example
fmt.Println("12. Demonstrating error handling...")
_, _, err = client.Agents.Get(ctx, "non-existent-agent-id")
if err != nil {
if apiErr, ok := err.(*contextforge.ErrorResponse); ok {
fmt.Printf(" ✓ Caught expected error: HTTP %d - %s\n",
apiErr.Response.StatusCode, apiErr.Message)
} else {
fmt.Printf(" ✓ Caught error: %v\n", err)
}
}
fmt.Println()
// Step 14: Delete agents
fmt.Println("13. Deleting agents...")
for _, id := range []string{createdAgent1.ID, createdAgent2.ID} {
_, err = client.Agents.Delete(ctx, id)
if err != nil {
log.Fatalf("Failed to delete agent %s: %v", id, err)
}
fmt.Printf(" ✓ Deleted agent: %s\n", id)
}
fmt.Println()
fmt.Println("=== Example completed successfully! ===")
fmt.Println("\nKey Features Demonstrated:")
fmt.Println("• A2A agent CRUD operations")
fmt.Println("• Skip/limit (offset-based) pagination instead of cursor-based")
fmt.Println("• Agent invocation with parameters (uses name, not ID)")
fmt.Println("• Authentication configuration (AuthType/AuthValue)")
fmt.Println("• Complex types (Capabilities and Config maps)")
fmt.Println("• Agent types and protocol versions")
fmt.Println("• Toggle enabled/disabled state")
fmt.Println("• Enabled vs Reachable distinction")
fmt.Println("\nAPI Inconsistencies:")
fmt.Println("• Create uses snake_case (endpoint_url, agent_type, team_id)")
fmt.Println("• Update uses camelCase (endpointUrl, agentType, teamId)")
fmt.Println("• Create request is wrapped: {\"agent\": {...}}")
fmt.Println("• Update request is unwrapped (direct body)")
fmt.Println("\nTo use with a real ContextForge instance:")
fmt.Println("1. Replace server.URL with your ContextForge address")
fmt.Println("2. Use real authentication credentials")
fmt.Println("3. Adjust agent endpoints to match real A2A agents")
}
// authenticate performs mock authentication and returns a JWT token
func authenticate(address string) string {
loginURL := address + "/auth/login"
payload := strings.NewReader(`{"email":"admin@example.com","password":"secret"}`)
resp, err := http.Post(loginURL, "application/json", payload)
if err != nil {
log.Fatalf("Authentication failed: %v", err)
}
defer resp.Body.Close()
var authResp struct {
AccessToken string `json:"access_token"`
}
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
log.Fatalf("Failed to decode auth response: %v", err)
}
return authResp.AccessToken
}
// setupMockEndpoints configures all the mock HTTP endpoints
func setupMockEndpoints(mux *http.ServeMux) {
// Mock authentication endpoint
mux.HandleFunc("/auth/login", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"access_token": "mock-jwt-token-99999",
"token_type": "bearer",
})
})
// Mock agent storage (in-memory)
agents := make(map[string]*contextforge.Agent)
agentsByName := make(map[string]*contextforge.Agent)
var agentCounter int
// POST /a2a - Create agent
// GET /a2a - List agents
mux.HandleFunc("/a2a", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
var req struct {
Agent *contextforge.AgentCreate `json:"agent"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
agentCounter++
id := fmt.Sprintf("agent-%d", agentCounter)
now := time.Now()
// Generate slug from name if not provided
slug := req.Agent.Name
if req.Agent.Slug != nil {
slug = *req.Agent.Slug
}
agent := &contextforge.Agent{
ID: id,
Name: req.Agent.Name,
Slug: slug,
Description: req.Agent.Description,
EndpointURL: req.Agent.EndpointURL,
AgentType: req.Agent.AgentType,
ProtocolVersion: req.Agent.ProtocolVersion,
Capabilities: req.Agent.Capabilities,
Config: req.Agent.Config,
AuthType: req.Agent.AuthType,
// Don't return AuthValue (it's encrypted by API)
Enabled: true,
Reachable: true, // Simulated connectivity status
Tags: contextforge.NewTags(req.Agent.Tags),
TeamID: req.Agent.TeamID,
Visibility: req.Agent.Visibility,
CreatedAt: &contextforge.Timestamp{Time: now},
UpdatedAt: &contextforge.Timestamp{Time: now},
Metrics: &contextforge.AgentMetrics{
TotalExecutions: 0,
SuccessfulExecutions: 0,
FailedExecutions: 0,
FailureRate: 0.0,
},
}
agents[id] = agent
agentsByName[agent.Name] = agent
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-RateLimit-Limit", "1000")
w.Header().Set("X-RateLimit-Remaining", "995")
w.Header().Set("X-RateLimit-Reset", fmt.Sprintf("%d", now.Add(time.Hour).Unix()))
// Return agent directly (not wrapped)
json.NewEncoder(w).Encode(agent)
case http.MethodGet:
query := r.URL.Query()
result := []*contextforge.Agent{}
for _, agent := range agents {
// Apply filters
if query.Get("include_inactive") != "true" && !agent.Enabled {
continue
}
if teamID := query.Get("team_id"); teamID != "" && agent.TeamID != nil && *agent.TeamID != teamID {
continue
}
if visibility := query.Get("visibility"); visibility != "" && agent.Visibility != nil && *agent.Visibility != visibility {
continue
}
result = append(result, agent)
}
// Handle skip/limit pagination
skip := 0
if s := query.Get("skip"); s != "" {
fmt.Sscanf(s, "%d", &skip)
}
limit := 100
if l := query.Get("limit"); l != "" {
fmt.Sscanf(l, "%d", &limit)
}
// Apply skip and limit
if skip >= len(result) {
result = []*contextforge.Agent{}
} else {
result = result[skip:]
if len(result) > limit {
result = result[:limit]
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}
})
// GET /a2a/{id} - Get agent
// PUT /a2a/{id} - Update agent
// DELETE /a2a/{id} - Delete agent
// POST /a2a/{id}/toggle - Toggle agent
// POST /a2a/{name}/invoke - Invoke agent
mux.HandleFunc("/a2a/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 3 {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
identifier := parts[2] // Could be ID or name
// Handle toggle endpoint
if len(parts) == 4 && parts[3] == "toggle" {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
agent, exists := agents[identifier]
if !exists {
http.Error(w, `{"message":"Agent not found"}`, http.StatusNotFound)
return
}
// Extract activate parameter from query string
activate := r.URL.Query().Get("activate") == "true"
agent.Enabled = activate
agent.UpdatedAt = &contextforge.Timestamp{Time: time.Now()}
w.Header().Set("Content-Type", "application/json")
// Return agent directly (not wrapped)
json.NewEncoder(w).Encode(agent)
return
}
// Handle invoke endpoint (uses name, not ID)
if len(parts) == 4 && parts[3] == "invoke" {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Look up by name
agent, exists := agentsByName[identifier]
if !exists {
http.Error(w, `{"message":"Agent not found"}`, http.StatusNotFound)
return
}
// Parse invoke request
var invokeReq contextforge.AgentInvokeRequest
if r.Body != nil {
json.NewDecoder(r.Body).Decode(&invokeReq)
}
// Update agent metrics and last interaction
agent.LastInteraction = &contextforge.Timestamp{Time: time.Now()}
if agent.Metrics != nil {
agent.Metrics.TotalExecutions++
agent.Metrics.SuccessfulExecutions++
}
// Return mock invoke response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"status": "success",
"result": map[string]any{
"processed": true,
"data": "transformed output from " + agent.Name,
},
"execution_time": 123,
})
return
}
// Handle standard CRUD operations (by ID)
switch r.Method {
case http.MethodGet:
agent, exists := agents[identifier]
if !exists {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]any{
"message": "Agent not found",
})
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agent)
case http.MethodPut:
agent, exists := agents[identifier]
if !exists {
http.Error(w, `{"message":"Agent not found"}`, http.StatusNotFound)
return
}
// Update request is NOT wrapped (unlike Create)
var req contextforge.AgentUpdate
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Update fields
if req.Name != nil {
agent.Name = *req.Name
}
if req.Description != nil {
agent.Description = req.Description
}
if req.EndpointURL != nil {
agent.EndpointURL = *req.EndpointURL
}
if req.AgentType != nil {
agent.AgentType = *req.AgentType
}
if req.ProtocolVersion != nil {
agent.ProtocolVersion = *req.ProtocolVersion
}
if req.Capabilities != nil {
agent.Capabilities = req.Capabilities
}
if req.Config != nil {
agent.Config = req.Config
}
if req.Tags != nil {
agent.Tags = contextforge.NewTags(req.Tags)
}
agent.UpdatedAt = &contextforge.Timestamp{Time: time.Now()}
w.Header().Set("Content-Type", "application/json")
// Return agent directly (not wrapped)
json.NewEncoder(w).Encode(agent)
case http.MethodDelete:
if _, exists := agents[identifier]; !exists {
http.Error(w, `{"message":"Agent not found"}`, http.StatusNotFound)
return
}
// Remove from both maps
if agent, ok := agents[identifier]; ok {
delete(agentsByName, agent.Name)
}
delete(agents, identifier)
w.WriteHeader(http.StatusNoContent)
}
})
}