Files
Memoh/internal/agent/spawn_adapter.go
Acbox fc2b603018 fix(agent): skip tools for models without tool-call capability and parse image output
- Add SupportsToolCall to RunConfig; only inject tools into SDK when set
- Update twilight-ai to 497ad09 which adds SSE scanner 10MB buffer
  (fixes token-too-long on large image payloads) and parses the images
  array from OpenAI-compatible chat completions into StreamFilePart
2026-04-03 00:01:14 +08:00

88 lines
2.4 KiB
Go

package agent
import (
"context"
"net/http"
sdk "github.com/memohai/twilight-ai/sdk"
"github.com/memohai/memoh/internal/agent/tools"
"github.com/memohai/memoh/internal/models"
)
// SpawnAdapter wraps *Agent to satisfy tools.SpawnAgent without creating
// an import cycle (tools -> agent).
type SpawnAdapter struct {
agent *Agent
}
// NewSpawnAdapter creates a SpawnAdapter from the given Agent.
func NewSpawnAdapter(a *Agent) *SpawnAdapter {
return &SpawnAdapter{agent: a}
}
func (s *SpawnAdapter) Generate(ctx context.Context, cfg tools.SpawnRunConfig) (*tools.SpawnResult, error) {
messages := cfg.Messages
if cfg.Query != "" {
messages = append(messages, sdk.Message{
Role: sdk.MessageRoleUser,
Content: []sdk.MessagePart{sdk.TextPart{Text: cfg.Query}},
})
}
rc := RunConfig{
Model: cfg.Model,
System: cfg.System,
Query: cfg.Query,
SessionType: cfg.SessionType,
Messages: messages,
ReasoningEffort: cfg.ReasoningEffort,
SupportsToolCall: true,
Identity: SessionContext{
BotID: cfg.Identity.BotID,
ChatID: cfg.Identity.ChatID,
SessionID: cfg.Identity.SessionID,
ChannelIdentityID: cfg.Identity.ChannelIdentityID,
CurrentPlatform: cfg.Identity.CurrentPlatform,
SessionToken: cfg.Identity.SessionToken,
IsSubagent: cfg.Identity.IsSubagent,
},
LoopDetection: LoopDetectionConfig{
Enabled: cfg.LoopDetection.Enabled,
},
}
result, err := s.agent.Generate(ctx, rc)
if err != nil {
return nil, err
}
return &tools.SpawnResult{
Messages: result.Messages,
Text: result.Text,
Usage: result.Usage,
}, nil
}
// SpawnSystemPrompt returns the system prompt for a given session type.
func SpawnSystemPrompt(sessionType string) string {
return GenerateSystemPrompt(SystemPromptParams{
SessionType: sessionType,
})
}
// SpawnModelCreatorFunc returns a tools.ModelCreator backed by the shared SDK model factory.
// This keeps subagent model creation aligned with the shared SDK model factory.
func SpawnModelCreatorFunc() tools.ModelCreator {
return func(modelID, clientType, apiKey, codexAccountID, baseURL string, httpClient *http.Client) *sdk.Model {
return models.NewSDKChatModel(models.SDKModelConfig{
ModelID: modelID,
ClientType: clientType,
APIKey: apiKey,
CodexAccountID: codexAccountID,
BaseURL: baseURL,
HTTPClient: httpClient,
})
}
}