mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
feat: introduce DCP pipeline layer for unified context assembly (#329)
* refactor: introduce DCP pipeline layer for unified context assembly
Introduce a Deterministic Context Pipeline (DCP) inspired by Cahciua,
providing event-driven context assembly for LLM conversations.
- Add `internal/pipeline/` package with Canonical Event types, Projection
(reduce), Rendering (XML RC), Pipeline manager, and EventStore persistence
- Change user message format from YAML front-matter to XML `<message>` tags
with self-contained attributes (sender, channel, conversation, type)
- Merge CLI/Web dual API into single `/local/` endpoint, remove CLI handler
- Add `bot_session_events` table for event persistence and cold-start replay
- Add `discuss` session type (reserved for future Cahciua-style mode)
- Wire pipeline into HandleInbound: adapt → persist → push on every message
- Lazy cold-start replay: load events from DB on first session access
* feat: implement discuss mode with reactive driver and probe gate
Add discuss session mode where the bot autonomously decides when to speak
in group chats via tool-gated output (send tool only, no direct text reply).
- Add discuss driver (per-session goroutine, RC watch, step loop via
agent.Generate, TR persistence, late-binding prompt with mention hints)
- Add system_discuss.md prompt template ("text = inner monologue, send = speak")
- Add context composition (MergeContext, ComposeContext, TrimContext) for
RC + assistant/tool message interleaving by timestamp
- Add probe gate: when discuss_probe_model_id is set, cheap model pre-filters
group messages; no tool calls = silence, tool calls = activate primary
- Add /new [chat|discuss] command: explicit mode selection, defaults to
discuss in groups, chat in DMs, chat-only for WebUI
- Add ResolveRunConfig on flow.Resolver for discuss driver to reuse
model/tools/system-prompt resolution without reimplementing
- Fix send tool for discuss mode: same-conversation sends now go through
SendDirect (channel adapter) instead of the local emitter shortcut
- Add target attribute to XML message format (reply_target for routing)
- Add discuss_probe_model_id to bots table settings
- Remove pipeline compaction (SetCompactCursor) — reuse existing compaction.Service
- Persist full SDK messages (including tool calls) in discuss mode
* refactor: unify DCP event layer, fix persistence and local channel
- Fix bot_session_events dedup index to include event_kind so that
message + edit events for the same external_message_id coexist.
- Change CreateSessionEvent from :one to :exec so ON CONFLICT DO NOTHING
does not produce spurious errors on duplicate delivery.
- Move ACL evaluation before event ingest; denied messages no longer
enter bot_session_events or the in-memory pipeline.
- Let chat mode consume RenderedContext from the DCP pipeline when
available, sharing the same event-driven context assembly as discuss.
- Collapse local WebSocket handler to route through HandleInbound
instead of directly calling StreamChatWS, eliminating the dual
business entry point.
- Extract buildBaseRunConfig shared builder so resolve() and
ResolveRunConfig() no longer duplicate model/credentials/skills setup.
- Add StoreRound to RunConfigResolver interface so discuss driver
persists assistant output with full metadata, usage, and memory
extraction (same quality as chat mode).
- Fix discuss driver context: use context.Background() instead of the
short-lived HTTP request context that was getting cancelled.
- Fix model ID passed to StoreRound: return database UUID from
ResolveRunConfig instead of SDK model name.
- Remove dead CLIAdapter/CLIType and update legacy web/cli references
in tests and comments.
* fix: stop idle discuss goroutines after 10min timeout
Discuss session goroutines were never cleaned up when a session became
inactive (e.g. after /new). Add a 10-minute idle timer that auto-exits
the goroutine and removes it from the sessions map when no new RC
arrives.
* refactor: pipeline details — event types, structured reply, display content
- Remove [User sent N attachments] placeholder text from buildInboundQuery;
attachment info is now expressed via pipeline <attachment> tags.
- Unify in-reply-to as structured ReplyRef (Sender/Preview fields) across
Telegram, Discord, Feishu, and Matrix adapters instead of prepending
[Reply to ...] text into the message body. Remove now-unused
buildTelegramQuotedText, buildDiscordQuotedText, buildMatrixQuotedText.
- Make AdaptInbound return CanonicalEvent interface and dispatch to
adaptMessage/adaptEdit/adaptService based on metadata["event_type"].
- Add event_id column to bot_history_messages (migration 0059) so user
messages can reference their canonical pipeline event.
- PersistEvent now returns the event UUID; HandleInbound passes it through
to both persistPassiveMessage and ChatRequest.EventID for storeRound.
- Add FillDisplayContent to message service: extracts plain text from
event_data for clean frontend display.
- Frontend extractMessageText prefers display_content when available,
falling back to legacy strip logic for old messages.
- Fix: always generate headerifiedQuery for storage even when usePipeline
is true, so user messages are persisted via storeRound in chat mode.
* fix: use json.Marshal for pipeline context content serialization
The manual string escaping in buildMessagesFromPipeline only handled
double quotes but not newlines, backslashes, and other JSON special
characters, producing invalid json.RawMessage values. The LLM then
received empty/malformed context and complained about having no history.
* fix: restore WebSocket handler to use StreamChatWS directly
The previous refactoring replaced the WS handler with HandleInbound +
RouteHub subscription, which broke streaming because RouteHub events
use a different format (channel.StreamEvent) than what the frontend
expects (flow.WSStreamEvent with text_delta, tool_call_start, etc.).
Restore the original direct StreamChatWS call path so WebUI streaming
works again. The WS handler now matches the pre-refactoring behavior
while all other changes (pipeline, ACL, event types, etc.) are kept.
* feat: store display_text directly in bot_history_messages
Instead of computing display content at API response time by querying
bot_session_events via event_id, store the raw user text in a dedicated
display_text column at write time. This works for all paths including
the WebSocket handler which does not go through the pipeline/event layer.
- Migration 0060: add display_text TEXT column
- PersistInput gains DisplayText; filled from trimmedText (passive) and
req.Query (storeRound)
- toMessageFields reads display_text into DisplayContent
- Remove FillDisplayContent runtime query and ListSessionEventsByEventID
- Frontend already prefers display_content when available (no change)
* fix: display_text should contain raw user text, not XML-wrapped query
req.Query gets overwritten to headerifiedQuery (with XML <message> tags)
before storeRound runs. Add RawQuery field to ChatRequest to preserve
the original user text, and use it for display_text in storeMessages.
* fix(web): show discuss sessions
* refactor: introduce DCP pipeline layer for unified context assembly
Introduce a Deterministic Context Pipeline (DCP) inspired by Cahciua,
providing event-driven context assembly for LLM conversations.
- Add `internal/pipeline/` package with Canonical Event types, Projection
(reduce), Rendering (XML RC), Pipeline manager, and EventStore persistence
- Change user message format from YAML front-matter to XML `<message>` tags
with self-contained attributes (sender, channel, conversation, type)
- Merge CLI/Web dual API into single `/local/` endpoint, remove CLI handler
- Add `bot_session_events` table for event persistence and cold-start replay
- Add `discuss` session type (reserved for future Cahciua-style mode)
- Wire pipeline into HandleInbound: adapt → persist → push on every message
- Lazy cold-start replay: load events from DB on first session access
* feat: implement discuss mode with reactive driver and probe gate
Add discuss session mode where the bot autonomously decides when to speak
in group chats via tool-gated output (send tool only, no direct text reply).
- Add discuss driver (per-session goroutine, RC watch, step loop via
agent.Generate, TR persistence, late-binding prompt with mention hints)
- Add system_discuss.md prompt template ("text = inner monologue, send = speak")
- Add context composition (MergeContext, ComposeContext, TrimContext) for
RC + assistant/tool message interleaving by timestamp
- Add probe gate: when discuss_probe_model_id is set, cheap model pre-filters
group messages; no tool calls = silence, tool calls = activate primary
- Add /new [chat|discuss] command: explicit mode selection, defaults to
discuss in groups, chat in DMs, chat-only for WebUI
- Add ResolveRunConfig on flow.Resolver for discuss driver to reuse
model/tools/system-prompt resolution without reimplementing
- Fix send tool for discuss mode: same-conversation sends now go through
SendDirect (channel adapter) instead of the local emitter shortcut
- Add target attribute to XML message format (reply_target for routing)
- Add discuss_probe_model_id to bots table settings
- Remove pipeline compaction (SetCompactCursor) — reuse existing compaction.Service
- Persist full SDK messages (including tool calls) in discuss mode
* refactor: unify DCP event layer, fix persistence and local channel
- Fix bot_session_events dedup index to include event_kind so that
message + edit events for the same external_message_id coexist.
- Change CreateSessionEvent from :one to :exec so ON CONFLICT DO NOTHING
does not produce spurious errors on duplicate delivery.
- Move ACL evaluation before event ingest; denied messages no longer
enter bot_session_events or the in-memory pipeline.
- Let chat mode consume RenderedContext from the DCP pipeline when
available, sharing the same event-driven context assembly as discuss.
- Collapse local WebSocket handler to route through HandleInbound
instead of directly calling StreamChatWS, eliminating the dual
business entry point.
- Extract buildBaseRunConfig shared builder so resolve() and
ResolveRunConfig() no longer duplicate model/credentials/skills setup.
- Add StoreRound to RunConfigResolver interface so discuss driver
persists assistant output with full metadata, usage, and memory
extraction (same quality as chat mode).
- Fix discuss driver context: use context.Background() instead of the
short-lived HTTP request context that was getting cancelled.
- Fix model ID passed to StoreRound: return database UUID from
ResolveRunConfig instead of SDK model name.
- Remove dead CLIAdapter/CLIType and update legacy web/cli references
in tests and comments.
* fix: stop idle discuss goroutines after 10min timeout
Discuss session goroutines were never cleaned up when a session became
inactive (e.g. after /new). Add a 10-minute idle timer that auto-exits
the goroutine and removes it from the sessions map when no new RC
arrives.
* refactor: pipeline details — event types, structured reply, display content
- Remove [User sent N attachments] placeholder text from buildInboundQuery;
attachment info is now expressed via pipeline <attachment> tags.
- Unify in-reply-to as structured ReplyRef (Sender/Preview fields) across
Telegram, Discord, Feishu, and Matrix adapters instead of prepending
[Reply to ...] text into the message body. Remove now-unused
buildTelegramQuotedText, buildDiscordQuotedText, buildMatrixQuotedText.
- Make AdaptInbound return CanonicalEvent interface and dispatch to
adaptMessage/adaptEdit/adaptService based on metadata["event_type"].
- Add event_id column to bot_history_messages (migration 0059) so user
messages can reference their canonical pipeline event.
- PersistEvent now returns the event UUID; HandleInbound passes it through
to both persistPassiveMessage and ChatRequest.EventID for storeRound.
- Add FillDisplayContent to message service: extracts plain text from
event_data for clean frontend display.
- Frontend extractMessageText prefers display_content when available,
falling back to legacy strip logic for old messages.
- Fix: always generate headerifiedQuery for storage even when usePipeline
is true, so user messages are persisted via storeRound in chat mode.
* fix: use json.Marshal for pipeline context content serialization
The manual string escaping in buildMessagesFromPipeline only handled
double quotes but not newlines, backslashes, and other JSON special
characters, producing invalid json.RawMessage values. The LLM then
received empty/malformed context and complained about having no history.
* fix: restore WebSocket handler to use StreamChatWS directly
The previous refactoring replaced the WS handler with HandleInbound +
RouteHub subscription, which broke streaming because RouteHub events
use a different format (channel.StreamEvent) than what the frontend
expects (flow.WSStreamEvent with text_delta, tool_call_start, etc.).
Restore the original direct StreamChatWS call path so WebUI streaming
works again. The WS handler now matches the pre-refactoring behavior
while all other changes (pipeline, ACL, event types, etc.) are kept.
* feat: store display_text directly in bot_history_messages
Instead of computing display content at API response time by querying
bot_session_events via event_id, store the raw user text in a dedicated
display_text column at write time. This works for all paths including
the WebSocket handler which does not go through the pipeline/event layer.
- Migration 0060: add display_text TEXT column
- PersistInput gains DisplayText; filled from trimmedText (passive) and
req.Query (storeRound)
- toMessageFields reads display_text into DisplayContent
- Remove FillDisplayContent runtime query and ListSessionEventsByEventID
- Frontend already prefers display_content when available (no change)
* fix: display_text should contain raw user text, not XML-wrapped query
req.Query gets overwritten to headerifiedQuery (with XML <message> tags)
before storeRound runs. Add RawQuery field to ChatRequest to preserve
the original user text, and use it for display_text in storeMessages.
* fix(web): show discuss sessions
* chore(feishu): change discuss output to stream card
* fix(channel): unify discuss/chat send path and card markdown delivery
* feat(discuss): switch to stream execution with RouteHub broadcasting
* refactor(pipeline): remove context trimming from ComposeContext
The pipeline path should not trim context by token budget — the
upstream IC/RC already bounds the event window. Remove TrimContext,
FindWorkingWindowCursor, EstimateTokens, FormatLastProcessedMs (all
unused or only used for trimming), the maxTokens parameter from
ComposeContext, and MaxContextTokens from DiscussSessionConfig.
---------
Co-authored-by: 晨苒 <16112591+chen-ran@users.noreply.github.com>
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
messagepkg "github.com/memohai/memoh/internal/message"
|
||||
messageevent "github.com/memohai/memoh/internal/message/event"
|
||||
"github.com/memohai/memoh/internal/models"
|
||||
pipelinepkg "github.com/memohai/memoh/internal/pipeline"
|
||||
"github.com/memohai/memoh/internal/providers"
|
||||
"github.com/memohai/memoh/internal/settings"
|
||||
)
|
||||
@@ -72,6 +73,7 @@ type Resolver struct {
|
||||
eventPublisher messageevent.Publisher
|
||||
skillLoader SkillLoader
|
||||
assetLoader gatewayAssetLoader
|
||||
pipeline *pipelinepkg.Pipeline
|
||||
timeout time.Duration
|
||||
clockLocation *time.Location
|
||||
logger *slog.Logger
|
||||
@@ -131,6 +133,18 @@ func (r *Resolver) SetCompactionService(s *compaction.Service) {
|
||||
r.compactionService = s
|
||||
}
|
||||
|
||||
// SetPipeline configures the DCP pipeline for RC-based context assembly.
|
||||
// When set, resolve() will use RC from the pipeline instead of loading
|
||||
// history from bot_history_messages for sessions that have pipeline data.
|
||||
func (r *Resolver) SetPipeline(p *pipelinepkg.Pipeline) {
|
||||
r.pipeline = p
|
||||
}
|
||||
|
||||
// Pipeline returns the configured pipeline, or nil.
|
||||
func (r *Resolver) Pipeline() *pipelinepkg.Pipeline {
|
||||
return r.pipeline
|
||||
}
|
||||
|
||||
type usageInfo struct {
|
||||
InputTokens *int `json:"inputTokens"`
|
||||
OutputTokens *int `json:"outputTokens"`
|
||||
@@ -155,26 +169,21 @@ func (r *Resolver) resolve(ctx context.Context, req conversation.ChatRequest) (r
|
||||
return resolvedContext{}, errors.New("chat id is required")
|
||||
}
|
||||
|
||||
botSettings, err := r.loadBotSettings(ctx, req.BotID)
|
||||
runCfg, chatModel, provider, err := r.buildBaseRunConfig(ctx, baseRunConfigParams{
|
||||
BotID: req.BotID,
|
||||
ChatID: req.ChatID,
|
||||
SessionID: req.SessionID,
|
||||
UserID: req.UserID,
|
||||
ChannelIdentityID: req.SourceChannelIdentityID,
|
||||
CurrentPlatform: req.CurrentChannel,
|
||||
ReplyTarget: req.ReplyTarget,
|
||||
ConversationType: req.ConversationType,
|
||||
SessionToken: req.ChatToken,
|
||||
ReasoningEffort: req.ReasoningEffort,
|
||||
})
|
||||
if err != nil {
|
||||
return resolvedContext{}, err
|
||||
}
|
||||
loopDetectionEnabled := r.loadBotLoopDetectionEnabled(ctx, req.BotID)
|
||||
userTimezoneName, userClockLocation := r.resolveTimezone(ctx, req.BotID, req.UserID)
|
||||
|
||||
var chatSettings conversation.Settings
|
||||
if r.conversationSvc != nil {
|
||||
chatSettings, err = r.conversationSvc.GetSettings(ctx, req.ChatID)
|
||||
if err != nil {
|
||||
return resolvedContext{}, err
|
||||
}
|
||||
}
|
||||
|
||||
chatModel, provider, err := r.selectChatModel(ctx, req, botSettings, chatSettings)
|
||||
if err != nil {
|
||||
return resolvedContext{}, err
|
||||
}
|
||||
clientType := provider.ClientType
|
||||
|
||||
memoryMsg := r.loadMemoryContextMessage(ctx, req)
|
||||
reqMessages := pruneMessagesForGateway(nonNilModelMessages(req.Messages))
|
||||
@@ -183,8 +192,21 @@ func (r *Resolver) resolve(ctx context.Context, req conversation.ChatRequest) (r
|
||||
memoryMsg = &pruned
|
||||
}
|
||||
|
||||
// When the DCP pipeline has data for this session, build context from
|
||||
// the rendered event stream (RC) + bot turn responses (TR) instead of
|
||||
// loading raw history from bot_history_messages. The current inbound
|
||||
// message is already in the RC, so it must not be appended again.
|
||||
usePipeline := r.pipeline != nil && strings.TrimSpace(req.SessionID) != ""
|
||||
if usePipeline {
|
||||
if _, loaded := r.pipeline.GetIC(strings.TrimSpace(req.SessionID)); !loaded {
|
||||
usePipeline = false
|
||||
}
|
||||
}
|
||||
|
||||
var messages []conversation.ModelMessage
|
||||
if r.conversationSvc != nil {
|
||||
if usePipeline {
|
||||
messages = r.buildMessagesFromPipeline(ctx, req)
|
||||
} else if r.conversationSvc != nil {
|
||||
loaded, loadErr := r.loadMessages(ctx, req.ChatID, req.SessionID, defaultMaxContextMinutes)
|
||||
if loadErr != nil {
|
||||
return resolvedContext{}, loadErr
|
||||
@@ -197,30 +219,18 @@ func (r *Resolver) resolve(ctx context.Context, req conversation.ChatRequest) (r
|
||||
if memoryMsg != nil {
|
||||
messages = append(messages, *memoryMsg)
|
||||
}
|
||||
messages = append(messages, reqMessages...)
|
||||
if !usePipeline {
|
||||
messages = append(messages, reqMessages...)
|
||||
}
|
||||
messages = sanitizeMessages(messages)
|
||||
var agentSkills []agentpkg.SkillEntry
|
||||
if r.skillLoader != nil {
|
||||
entries, err := r.skillLoader.LoadSkills(ctx, req.BotID)
|
||||
if err != nil {
|
||||
r.logger.Warn("failed to load usable skills", slog.String("bot_id", req.BotID), slog.Any("error", err))
|
||||
} else {
|
||||
agentSkills = make([]agentpkg.SkillEntry, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
skill, ok := normalizeGatewaySkill(e)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
agentSkills = append(agentSkills, skill)
|
||||
}
|
||||
}
|
||||
}
|
||||
if agentSkills == nil {
|
||||
agentSkills = []agentpkg.SkillEntry{}
|
||||
}
|
||||
|
||||
displayName := r.resolveDisplayName(ctx, req)
|
||||
mergedAttachments := r.routeAndMergeAttachments(ctx, chatModel, req)
|
||||
|
||||
tz := runCfg.Identity.TimezoneLocation
|
||||
if tz == nil {
|
||||
tz = time.UTC
|
||||
}
|
||||
headerifiedQuery := FormatUserHeader(UserMessageHeaderInput{
|
||||
MessageID: strings.TrimSpace(req.ExternalMessageID),
|
||||
ChannelIdentityID: strings.TrimSpace(req.SourceChannelIdentityID),
|
||||
@@ -228,70 +238,19 @@ func (r *Resolver) resolve(ctx context.Context, req conversation.ChatRequest) (r
|
||||
Channel: req.CurrentChannel,
|
||||
ConversationType: strings.TrimSpace(req.ConversationType),
|
||||
ConversationName: strings.TrimSpace(req.ConversationName),
|
||||
Target: strings.TrimSpace(req.ReplyTarget),
|
||||
AttachmentPaths: extractAttachmentPaths(mergedAttachments),
|
||||
Time: time.Now().In(userClockLocation),
|
||||
Timezone: userTimezoneName,
|
||||
Time: time.Now().In(tz),
|
||||
Timezone: runCfg.Identity.Timezone,
|
||||
}, req.Query)
|
||||
inlineImages := extractNativeImageParts(mergedAttachments)
|
||||
|
||||
reasoningEffort := ""
|
||||
if chatModel.HasCompatibility(models.CompatReasoning) {
|
||||
if re := strings.TrimSpace(req.ReasoningEffort); re != "" {
|
||||
reasoningEffort = re
|
||||
} else if botSettings.ReasoningEnabled {
|
||||
reasoningEffort = botSettings.ReasoningEffort
|
||||
}
|
||||
}
|
||||
|
||||
var reasoningConfig *models.ReasoningConfig
|
||||
if reasoningEffort != "" {
|
||||
reasoningConfig = &models.ReasoningConfig{
|
||||
Enabled: true,
|
||||
Effort: reasoningEffort,
|
||||
}
|
||||
}
|
||||
|
||||
authResolver := providers.NewService(nil, r.queries, "")
|
||||
creds, err := authResolver.ResolveModelCredentials(ctx, provider)
|
||||
if err != nil {
|
||||
return resolvedContext{}, fmt.Errorf("resolve provider credentials: %w", err)
|
||||
}
|
||||
|
||||
modelCfg := models.SDKModelConfig{
|
||||
ModelID: chatModel.ModelID,
|
||||
ClientType: clientType,
|
||||
APIKey: creds.APIKey,
|
||||
CodexAccountID: creds.CodexAccountID,
|
||||
BaseURL: provider.BaseUrl,
|
||||
ReasoningConfig: reasoningConfig,
|
||||
}
|
||||
|
||||
sdkModel := models.NewSDKChatModel(modelCfg)
|
||||
sdkMessages := modelMessagesToSDKMessages(nonNilModelMessages(messages))
|
||||
|
||||
runCfg := agentpkg.RunConfig{
|
||||
Model: sdkModel,
|
||||
ReasoningEffort: reasoningEffort,
|
||||
Messages: sdkMessages,
|
||||
Query: headerifiedQuery,
|
||||
SupportsImageInput: chatModel.HasCompatibility(models.CompatVision),
|
||||
SupportsToolCall: chatModel.HasCompatibility(models.CompatToolCall),
|
||||
InlineImages: inlineImages,
|
||||
Identity: agentpkg.SessionContext{
|
||||
BotID: req.BotID,
|
||||
ChatID: req.ChatID,
|
||||
SessionID: req.SessionID,
|
||||
ChannelIdentityID: strings.TrimSpace(req.SourceChannelIdentityID),
|
||||
CurrentPlatform: req.CurrentChannel,
|
||||
ReplyTarget: strings.TrimSpace(req.ReplyTarget),
|
||||
ConversationType: strings.TrimSpace(req.ConversationType),
|
||||
Timezone: userTimezoneName,
|
||||
TimezoneLocation: userClockLocation,
|
||||
SessionToken: req.ChatToken,
|
||||
},
|
||||
Skills: agentSkills,
|
||||
LoopDetection: agentpkg.LoopDetectionConfig{Enabled: loopDetectionEnabled},
|
||||
runCfg.Messages = modelMessagesToSDKMessages(nonNilModelMessages(messages))
|
||||
// When using the pipeline the user message is already in the RC;
|
||||
// don't send it to the LLM again. headerifiedQuery is still kept
|
||||
// for storeRound so the user message gets persisted.
|
||||
if !usePipeline {
|
||||
runCfg.Query = headerifiedQuery
|
||||
}
|
||||
runCfg.InlineImages = extractNativeImageParts(mergedAttachments)
|
||||
|
||||
var injectedRecords *[]conversation.InjectedMessageRecord
|
||||
if req.InjectCh != nil {
|
||||
@@ -335,6 +294,9 @@ func (r *Resolver) Chat(ctx context.Context, req conversation.ChatRequest) (conv
|
||||
if err != nil {
|
||||
return conversation.ChatResponse{}, err
|
||||
}
|
||||
if req.RawQuery == "" {
|
||||
req.RawQuery = strings.TrimSpace(req.Query)
|
||||
}
|
||||
req.Query = rc.query
|
||||
|
||||
go r.maybeGenerateSessionTitle(context.WithoutCancel(ctx), req, req.Query)
|
||||
@@ -364,6 +326,146 @@ func (r *Resolver) Chat(ctx context.Context, req conversation.ChatRequest) (conv
|
||||
}, nil
|
||||
}
|
||||
|
||||
// baseRunConfigParams holds parameters for buildBaseRunConfig that differ
|
||||
// between chat and discuss callers.
|
||||
type baseRunConfigParams struct {
|
||||
BotID string
|
||||
ChatID string
|
||||
SessionID string
|
||||
UserID string
|
||||
ChannelIdentityID string
|
||||
CurrentPlatform string
|
||||
ReplyTarget string
|
||||
ConversationType string
|
||||
SessionToken string //nolint:gosec // session credential material, not a hardcoded secret
|
||||
SessionType string
|
||||
ReasoningEffort string // caller-provided override (empty = use bot default)
|
||||
}
|
||||
|
||||
// buildBaseRunConfig creates a RunConfig with model, credentials, skills,
|
||||
// identity and system prompt — everything except Messages/Query/InlineImages.
|
||||
// Both resolve() and ResolveRunConfig() delegate to this shared builder.
|
||||
func (r *Resolver) buildBaseRunConfig(ctx context.Context, p baseRunConfigParams) (agentpkg.RunConfig, models.GetResponse, sqlc.LlmProvider, error) {
|
||||
botSettings, err := r.loadBotSettings(ctx, p.BotID)
|
||||
if err != nil {
|
||||
return agentpkg.RunConfig{}, models.GetResponse{}, sqlc.LlmProvider{}, err
|
||||
}
|
||||
loopDetectionEnabled := r.loadBotLoopDetectionEnabled(ctx, p.BotID)
|
||||
userTimezoneName, userClockLocation := r.resolveTimezone(ctx, p.BotID, p.UserID)
|
||||
|
||||
chatID := p.ChatID
|
||||
if chatID == "" {
|
||||
chatID = p.BotID
|
||||
}
|
||||
|
||||
req := conversation.ChatRequest{
|
||||
BotID: p.BotID,
|
||||
ChatID: chatID,
|
||||
SessionID: p.SessionID,
|
||||
CurrentChannel: p.CurrentPlatform,
|
||||
}
|
||||
|
||||
chatModel, provider, err := r.selectChatModel(ctx, req, botSettings, conversation.Settings{})
|
||||
if err != nil {
|
||||
return agentpkg.RunConfig{}, models.GetResponse{}, sqlc.LlmProvider{}, err
|
||||
}
|
||||
|
||||
reasoningEffort := p.ReasoningEffort
|
||||
if reasoningEffort == "" && chatModel.HasCompatibility(models.CompatReasoning) && botSettings.ReasoningEnabled {
|
||||
reasoningEffort = botSettings.ReasoningEffort
|
||||
}
|
||||
var reasoningConfig *models.ReasoningConfig
|
||||
if reasoningEffort != "" {
|
||||
reasoningConfig = &models.ReasoningConfig{Enabled: true, Effort: reasoningEffort}
|
||||
}
|
||||
|
||||
authResolver := providers.NewService(nil, r.queries, "")
|
||||
creds, err := authResolver.ResolveModelCredentials(ctx, provider)
|
||||
if err != nil {
|
||||
return agentpkg.RunConfig{}, models.GetResponse{}, sqlc.LlmProvider{}, fmt.Errorf("resolve provider credentials: %w", err)
|
||||
}
|
||||
|
||||
sdkModel := models.NewSDKChatModel(models.SDKModelConfig{
|
||||
ModelID: chatModel.ModelID,
|
||||
ClientType: provider.ClientType,
|
||||
APIKey: creds.APIKey,
|
||||
CodexAccountID: creds.CodexAccountID,
|
||||
BaseURL: provider.BaseUrl,
|
||||
ReasoningConfig: reasoningConfig,
|
||||
})
|
||||
|
||||
var agentSkills []agentpkg.SkillEntry
|
||||
if r.skillLoader != nil {
|
||||
entries, skillErr := r.skillLoader.LoadSkills(ctx, p.BotID)
|
||||
if skillErr != nil {
|
||||
r.logger.Warn("failed to load skills", slog.String("bot_id", p.BotID), slog.Any("error", skillErr))
|
||||
} else {
|
||||
for _, e := range entries {
|
||||
if skill, ok := normalizeGatewaySkill(e); ok {
|
||||
agentSkills = append(agentSkills, skill)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if agentSkills == nil {
|
||||
agentSkills = []agentpkg.SkillEntry{}
|
||||
}
|
||||
|
||||
cfg := agentpkg.RunConfig{
|
||||
Model: sdkModel,
|
||||
ReasoningEffort: reasoningEffort,
|
||||
SessionType: p.SessionType,
|
||||
SupportsImageInput: chatModel.HasCompatibility(models.CompatVision),
|
||||
SupportsToolCall: chatModel.HasCompatibility(models.CompatToolCall),
|
||||
Identity: agentpkg.SessionContext{
|
||||
BotID: p.BotID,
|
||||
ChatID: chatID,
|
||||
SessionID: p.SessionID,
|
||||
ChannelIdentityID: strings.TrimSpace(p.ChannelIdentityID),
|
||||
CurrentPlatform: p.CurrentPlatform,
|
||||
ReplyTarget: strings.TrimSpace(p.ReplyTarget),
|
||||
ConversationType: strings.TrimSpace(p.ConversationType),
|
||||
Timezone: userTimezoneName,
|
||||
TimezoneLocation: userClockLocation,
|
||||
SessionToken: p.SessionToken,
|
||||
},
|
||||
Skills: agentSkills,
|
||||
LoopDetection: agentpkg.LoopDetectionConfig{Enabled: loopDetectionEnabled},
|
||||
}
|
||||
|
||||
return cfg, chatModel, provider, nil
|
||||
}
|
||||
|
||||
// ResolveRunConfig builds a complete RunConfig (model, system prompt, tools,
|
||||
// identity) for a bot+session without loading messages or requiring a query.
|
||||
// The caller is responsible for filling RunConfig.Messages.
|
||||
// Used by the discuss driver to reuse the resolver's model/tools/prompt pipeline.
|
||||
func (r *Resolver) ResolveRunConfig(ctx context.Context, botID, sessionID, channelIdentityID, currentPlatform, replyTarget, conversationType, chatToken string) (pipelinepkg.ResolveRunConfigResult, error) {
|
||||
if strings.TrimSpace(botID) == "" {
|
||||
return pipelinepkg.ResolveRunConfigResult{}, errors.New("bot id is required")
|
||||
}
|
||||
|
||||
cfg, chatModel, _, err := r.buildBaseRunConfig(ctx, baseRunConfigParams{
|
||||
BotID: botID,
|
||||
SessionID: sessionID,
|
||||
ChannelIdentityID: channelIdentityID,
|
||||
CurrentPlatform: currentPlatform,
|
||||
ReplyTarget: replyTarget,
|
||||
ConversationType: conversationType,
|
||||
SessionToken: chatToken,
|
||||
SessionType: "discuss",
|
||||
})
|
||||
if err != nil {
|
||||
return pipelinepkg.ResolveRunConfigResult{}, err
|
||||
}
|
||||
|
||||
cfg = r.prepareRunConfig(ctx, cfg)
|
||||
return pipelinepkg.ResolveRunConfigResult{
|
||||
RunConfig: cfg,
|
||||
ModelID: chatModel.ID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// prepareRunConfig generates the system prompt and appends the user message.
|
||||
func (r *Resolver) prepareRunConfig(ctx context.Context, cfg agentpkg.RunConfig) agentpkg.RunConfig {
|
||||
supportsImageInput := cfg.SupportsImageInput
|
||||
|
||||
Reference in New Issue
Block a user