142 lines
4.8 KiB
Go
142 lines
4.8 KiB
Go
package multiagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"cyberstrike-ai/internal/agent"
|
|
"cyberstrike-ai/internal/agentruntime"
|
|
"cyberstrike-ai/internal/config"
|
|
"cyberstrike-ai/internal/openai"
|
|
"github.com/google/uuid"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func CompleteBackgroundStep(ctx context.Context, oa config.OpenAIConfig, msgs []agent.ChatMessage, tools []agent.Tool, maxTokens int, logger *zap.Logger) (*agent.ChatMessage, int, error) {
|
|
return CompleteBackgroundStepWithRuntime(ctx, config.AgentRuntimeConfig{}, oa, msgs, tools, maxTokens, logger)
|
|
}
|
|
|
|
// Generate one proposal only. The caller owns tool execution,
|
|
// approvals, retries, budgets and transcript persistence; the SDK cannot execute here.
|
|
func CompleteBackgroundStepWithRuntime(ctx context.Context, runtime config.AgentRuntimeConfig, oa config.OpenAIConfig, msgs []agent.ChatMessage, tools []agent.Tool, maxTokens int, logger *zap.Logger) (*agent.ChatMessage, int, error) {
|
|
if err := runtime.Validate(); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if maxTokens <= 0 {
|
|
return nil, 0, fmt.Errorf("background generation budget is exhausted")
|
|
}
|
|
if oa.MaxCompletionTokensEffective() > maxTokens {
|
|
oa.MaxCompletionTokens = maxTokens
|
|
}
|
|
var system []string
|
|
var history []agent.ChatMessage
|
|
for _, m := range msgs {
|
|
if m.Role == "system" {
|
|
system = append(system, m.Content)
|
|
} else {
|
|
history = append(history, m)
|
|
}
|
|
}
|
|
raw, err := piHistoryJSON(history)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defs := make([]agentruntime.Tool, 0, len(tools))
|
|
for _, t := range tools {
|
|
defs = append(defs, agentruntime.Tool{Name: t.Function.Name, Description: t.Function.Description, Parameters: t.Function.Parameters})
|
|
}
|
|
req := agentruntime.Request{RunID: uuid.NewString(), GenerateOnly: true, SystemPrompt: strings.Join(system, "\n\n"), History: raw, Model: piModelConfig(oa, nil, openai.SessionIDFromContext(ctx)), Tools: defs, MaxTurns: 1}
|
|
result, err := (agentruntime.Pi{NodePath: runtime.NodePath, WorkerPath: runtime.WorkerPath}).Run(ctx, req, nil, nil, nil)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var messages []agent.ChatMessage
|
|
if err = json.Unmarshal(result.History, &messages); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if len(messages) != 1 {
|
|
return nil, 0, fmt.Errorf("Pi returned no background proposal")
|
|
}
|
|
return &messages[0], result.TotalTokens, nil
|
|
}
|
|
|
|
// SingleShot is one bounded generation with no host tool execution.
|
|
type SingleShot struct {
|
|
SystemPrompt string
|
|
Message string
|
|
History []agent.ChatMessage
|
|
Images []agentruntime.Image
|
|
MaxTokens int
|
|
ExtraFields map[string]any
|
|
}
|
|
|
|
// CompleteTextWithRuntime runs a text-only single shot through the Pi runtime.
|
|
func CompleteTextWithRuntime(ctx context.Context, runtime config.AgentRuntimeConfig, oa config.OpenAIConfig, req SingleShot) (string, error) {
|
|
req.Images = nil
|
|
return completeSingleShot(ctx, runtime, oa, req)
|
|
}
|
|
|
|
// CompleteVisionWithRuntime runs a multimodal single shot through the Pi runtime.
|
|
func CompleteVisionWithRuntime(ctx context.Context, runtime config.AgentRuntimeConfig, oa config.OpenAIConfig, req SingleShot) (string, error) {
|
|
oa.ImageInput = "enabled"
|
|
return completeSingleShot(ctx, runtime, oa, req)
|
|
}
|
|
|
|
func completeSingleShot(ctx context.Context, runtime config.AgentRuntimeConfig, oa config.OpenAIConfig, req SingleShot) (string, error) {
|
|
if err := runtime.Validate(); err != nil {
|
|
return "", err
|
|
}
|
|
if req.MaxTokens > 0 && oa.MaxCompletionTokensEffective() > req.MaxTokens {
|
|
oa.MaxCompletionTokens = req.MaxTokens
|
|
}
|
|
model := piModelConfig(oa, nil, openai.SessionIDFromContext(ctx))
|
|
if len(req.ExtraFields) > 0 {
|
|
// The model may alias the channel's own map; never write through it.
|
|
merged := make(map[string]any, len(model.ExtraFields)+len(req.ExtraFields))
|
|
for key, value := range model.ExtraFields {
|
|
merged[key] = value
|
|
}
|
|
for key, value := range req.ExtraFields {
|
|
if key == "temperature" && model.API != "openai-completions" {
|
|
if temp, ok := numericFloat64(value); ok {
|
|
model.Temperature = &temp
|
|
continue
|
|
}
|
|
}
|
|
merged[key] = value
|
|
}
|
|
model.ExtraFields = merged
|
|
}
|
|
history, err := piHistoryJSON(req.History)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
result, err := (agentruntime.Pi{NodePath: runtime.NodePath, WorkerPath: runtime.WorkerPath}).Run(ctx, agentruntime.Request{
|
|
RunID: uuid.NewString(), GenerateOnly: true, SystemPrompt: req.SystemPrompt, Message: req.Message,
|
|
History: history, Images: req.Images, Model: model, Tools: []agentruntime.Tool{}, MaxTurns: 1,
|
|
}, nil, nil, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if strings.TrimSpace(result.Response) == "" {
|
|
return "", fmt.Errorf("Pi returned an empty completion")
|
|
}
|
|
return strings.TrimSpace(result.Response), nil
|
|
}
|
|
|
|
func numericFloat64(v any) (float64, bool) {
|
|
switch n := v.(type) {
|
|
case float64:
|
|
return n, true
|
|
case float32:
|
|
return float64(n), true
|
|
case int:
|
|
return float64(n), true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|