-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathagent_stream.go
More file actions
167 lines (145 loc) · 4.33 KB
/
agent_stream.go
File metadata and controls
167 lines (145 loc) · 4.33 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
package agent
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/Protocol-Lattice/go-agent/src/memory"
"github.com/Protocol-Lattice/go-agent/src/models"
)
// GenerateStream provides a streaming interface for the agent's generation process.
// It follows the same logic as Generate but returns a channel of chunks.
func (a *Agent) GenerateStream(ctx context.Context, sessionID, userInput string) (<-chan models.StreamChunk, error) {
trimmed := strings.TrimSpace(userInput)
if trimmed == "" {
return nil, errors.New("user input is empty")
}
// -------------------------------------------------------------
// PREFETCH: Start context retrieval and tool discovery in parallel
// -------------------------------------------------------------
var (
prefetchWG sync.WaitGroup
records []memory.MemoryRecord
)
prefetchWG.Add(1)
go func() {
defer prefetchWG.Done()
records, _ = a.retrieveContext(ctx, sessionID, userInput, a.contextLimit)
}()
// ToolSpecs discovery is internally cached and thread-safe.
_ = a.ToolSpecs()
// Helper to wrap immediate result in a stream
immediateStream := func(val any, err error) (<-chan models.StreamChunk, error) {
ch := make(chan models.StreamChunk, 1)
if err != nil {
ch <- models.StreamChunk{Err: err, Done: true}
} else {
str := fmt.Sprint(val)
ch <- models.StreamChunk{Delta: str, FullText: str, Done: true}
}
close(ch)
return ch, nil
}
// 0. DIRECT TOOL INVOCATION
if toolName, args, ok := a.detectDirectToolCall(trimmed); ok {
result, err := a.executeTool(ctx, sessionID, toolName, args)
return immediateStream(result, err)
}
// 1. SUBAGENT COMMANDS
if handled, out, meta, err := a.handleCommand(ctx, sessionID, userInput); handled {
if err != nil {
return nil, err
}
a.storeMemory(sessionID, "subagent", out, meta)
return immediateStream(out, nil)
}
// 2. CODEMODE
if a.CodeMode != nil {
if handled, output, err := a.CodeMode.CallTool(ctx, userInput); handled {
return immediateStream(output, err)
}
}
// 3. Chain Orchestrator
if handled, output, err := a.codeChainOrchestrator(ctx, sessionID, userInput); handled {
if err == nil && a.Guardrails != nil {
validated, gErr := a.Guardrails.ValidateAndRepair(ctx, output)
if gErr != nil {
return immediateStream("", gErr)
}
output = validated
}
return immediateStream(output, err)
}
// 4. TOOL ORCHESTRATOR
prefetchWG.Wait()
if handled, output, err := a.toolOrchestrator(ctx, sessionID, userInput, records); handled {
return immediateStream(output, err)
}
// 5. STORE USER MEMORY
a.storeMemory(sessionID, "user", userInput, nil)
// If it looked like a tool call but wasn't handled, return empty
if a.userLooksLikeToolCall(trimmed) {
return immediateStream("", nil)
}
// 6. LLM COMPLETION (Streaming)
// Build prompt manually to use pre-fetched records
var sb strings.Builder
sb.Grow(4096)
sb.WriteString(a.systemPrompt)
sb.WriteString("\n\nConversation memory (TOON):\n")
sb.WriteString(a.renderMemory(records))
sb.WriteString("\n\nUser: ")
sb.WriteString(sanitizeInput(userInput))
sb.WriteString("\n\n")
prompt := sb.String()
stream, err := a.model.GenerateStream(ctx, prompt)
if err != nil {
return nil, err
}
// Wrap the stream to intercept and store memory
outCh := make(chan models.StreamChunk)
if a.Guardrails != nil {
go func() {
defer close(outCh)
var full strings.Builder
for chunk := range stream {
if chunk.Err != nil {
outCh <- chunk
return
}
if chunk.Delta != "" {
full.WriteString(chunk.Delta)
}
}
finalText := full.String()
validatedText, gErr := a.Guardrails.ValidateAndRepair(ctx, finalText)
if gErr != nil {
outCh <- models.StreamChunk{Err: gErr, Done: true}
return
}
// Stream out the validated text as one chunk
outCh <- models.StreamChunk{Delta: validatedText, FullText: validatedText, Done: true}
a.storeMemory(sessionID, "assistant", validatedText, nil)
}()
} else {
go func() {
defer close(outCh)
var full strings.Builder
for chunk := range stream {
if chunk.Err != nil {
outCh <- chunk
return
}
if chunk.Delta != "" {
full.WriteString(chunk.Delta)
}
outCh <- chunk
}
// Store memory after completion
finalText := full.String()
a.storeMemory(sessionID, "assistant", finalText, nil)
}()
}
return outCh, nil
}