mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
b3a39ad93d
* refactor: replace persistent subagents with ephemeral spawn tool (#subagent) - Drop subagents table, remove all persistent subagent infrastructure - Add 'subagent' session type with parent_session_id on bot_sessions - Rewrite subagent tool as single 'spawn' tool with parallel execution - Create system_subagent.md prompt, add _subagent.md include for chat - Limit subagent tools to file, exec, web_search, web_fetch only - Merge subagent token usage into parent chat session in reporting - Remove frontend subagent management page, update chat UI for spawn - Fix UTF-8 truncation in session title, fix query not passed to agent * refactor: remove history message page
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
|
|
sdk "github.com/memohai/twilight-ai/sdk"
|
|
|
|
"github.com/memohai/memoh/internal/agent/tools"
|
|
)
|
|
|
|
// 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,
|
|
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 that delegates to agent.CreateModel.
|
|
func SpawnModelCreatorFunc() tools.ModelCreator {
|
|
return func(modelID, clientType, apiKey, baseURL string) *sdk.Model {
|
|
return CreateModel(ModelConfig{
|
|
ModelID: modelID,
|
|
ClientType: clientType,
|
|
APIKey: apiKey,
|
|
BaseURL: baseURL,
|
|
})
|
|
}
|
|
}
|