feat(loop): agent loop(8 轮 + checkpoint + 流式 + 记忆注入/提取 + trace + 工具超时回喂)
This commit is contained in:
1 parent
01a4e29f84
commit
71697ca4f8
12 files changed
+1292
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package loop
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/agent"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
)
|
||||
|
||||
// streamStep drives a streaming model call. It is the streaming counterpart of
|
||||
// agent.RunStep: it builds the request from the agent's public surface, runs the
|
||||
// loop's input guardrail, streams deltas through OnStream, runs the output
|
||||
// guardrail and decodes any tool calls. Because RunStep does not expose a stream
|
||||
// path, this method is owned by the loop; it mirrors the agent's semantics so the
|
||||
// two behave identically apart from the delta callback.
|
||||
func (l *Loop) streamStep(ctx context.Context, messages []model.Message) (*stepOutcome, error) {
|
||||
adapter := l.agent.Adapter()
|
||||
if adapter == nil {
|
||||
return nil, fmt.Errorf("loop: stream step: %w", ErrNoAdapter)
|
||||
}
|
||||
req := l.buildRequest(messages)
|
||||
|
||||
if l.guards != nil {
|
||||
verdict, err := l.guards.CheckInput(ctx, &req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loop: check input: %w", err)
|
||||
}
|
||||
if verdict != nil && !verdict.Allow {
|
||||
return &stepOutcome{final: true, text: verdict.Reason, stopReason: agent.StopReasonGuardrailInput}, nil
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := adapter.ChatStream(ctx, req, func(delta string) {
|
||||
if l.onStream != nil {
|
||||
l.onStream(delta)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loop: chat stream: %w", err)
|
||||
}
|
||||
|
||||
if l.guards != nil {
|
||||
verdict, err := l.guards.CheckOutput(ctx, &resp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loop: check output: %w", err)
|
||||
}
|
||||
if verdict != nil && !verdict.Allow {
|
||||
return &stepOutcome{final: true, text: verdict.Reason, stopReason: agent.StopReasonGuardrailOutput}, nil
|
||||
}
|
||||
}
|
||||
|
||||
if calls, ok := decodeToolCalls(resp); ok {
|
||||
return &stepOutcome{final: false, toolCalls: calls, stopReason: agent.StopReasonToolCalls}, nil
|
||||
}
|
||||
return &stepOutcome{final: true, text: resp.Content, stopReason: agent.StopReasonStop}, nil
|
||||
}
|
||||
Reference in new issue
Block a user