feat: add persona context to delivery messages

This commit is contained in:
Goon committed 2026-06-12 15:17:57 +07:00
1 parent 79bc490786
commit a76f443411
8 files changed
+440 -34

No files matched your search

+45 -2
View File
@@ -45,7 +45,7 @@ func TestBuildDeliveryRuntimeUsesResolvedDeliveryProviderModels(t *testing.T) {
TenantID: tenantID,
Content: "kiểm tra giúp tôi",
Metadata: map[string]string{"locale": "vi"},
}, "dm", "telegram", "agent")
}, "user-1", "dm", "telegram", "agent")
quick, ok := runtime.QuickAckGenerator.(channels.ProviderDeliveryMessageGenerator)
if !ok {
@@ -82,7 +82,7 @@ func TestBuildDeliveryRuntimeFallsBackToAgentProviderModelWhenUnset(t *testing.T
Enabled: true,
Mode: channels.IntermediateModeSidecar,
},
}, bus.InboundMessage{TenantID: tenantID}, "dm", "telegram", "agent")
}, bus.InboundMessage{TenantID: tenantID}, "user-1", "dm", "telegram", "agent")
quick, ok := runtime.QuickAckGenerator.(channels.ProviderDeliveryMessageGenerator)
if !ok {
@@ -100,6 +100,40 @@ func TestBuildDeliveryRuntimeFallsBackToAgentProviderModelWhenUnset(t *testing.T
}
}
func TestBuildDeliveryRuntimeIncludesAgentPersonaBrief(t *testing.T) {
agent := deliveryRuntimePersonaTestAgent{
deliveryRuntimeTestAgent: deliveryRuntimeTestAgent{
id: "agent",
uuid: uuid.New(),
model: "agent-model",
providerName: "agent-provider",
provider: deliveryRuntimeTestProvider{name: "agent-provider", model: "agent-default"},
},
personaByUserID: map[string]string{
"raw-user": "wrong persona",
"resolved-user": "Style: concise, warm",
},
}
runtime := buildDeliveryRuntime(context.Background(), &ConsumerDeps{}, deliveryRuntimePersonaTestAgent{
deliveryRuntimeTestAgent: agent.deliveryRuntimeTestAgent,
personaByUserID: agent.personaByUserID,
}, channels.ResolvedChatBehavior{
Enabled: true,
QuickAck: channels.ResolvedQuickAckConfig{
Enabled: true,
Mode: channels.QuickAckModeSidecar,
},
IntermediateReplies: channels.ResolvedIntermediateRepliesConfig{
Enabled: true,
Mode: channels.IntermediateModeSidecar,
},
}, bus.InboundMessage{UserID: "raw-user"}, "resolved-user", "dm", "telegram", "agent")
if runtime.PersonaBrief != "Style: concise, warm" {
t.Fatalf("runtime persona brief = %q, want agent persona", runtime.PersonaBrief)
}
}
type deliveryRuntimeTestAgent struct {
id string
uuid uuid.UUID
@@ -120,6 +154,15 @@ func (a deliveryRuntimeTestAgent) Model() string { return a.model
func (a deliveryRuntimeTestAgent) ProviderName() string { return a.providerName }
func (a deliveryRuntimeTestAgent) Provider() providers.Provider { return a.provider }
type deliveryRuntimePersonaTestAgent struct {
deliveryRuntimeTestAgent
personaByUserID map[string]string
}
func (a deliveryRuntimePersonaTestAgent) DeliveryPersonaBrief(_ context.Context, userID string) string {
return a.personaByUserID[userID]
}
type deliveryRuntimeTestProvider struct {
name string
model string
+22 -7
View File
@@ -249,7 +249,7 @@ func processNormalMessage(
chatBehavior = deps.ChannelMgr.ResolveChatBehaviorWithAgent(msg.Channel, workspaceBehavior, agentBehavior)
}
blockReply := deps.ChannelMgr != nil && chatBehavior.IntermediateReplies.Enabled
deliveryRuntime := buildDeliveryRuntime(ctx, deps, agentLoop, chatBehavior, msg, peerKind, resolveChannelType(deps.ChannelMgr, msg.Channel), agentID)
deliveryRuntime := buildDeliveryRuntime(ctx, deps, agentLoop, chatBehavior, msg, userID, peerKind, resolveChannelType(deps.ChannelMgr, msg.Channel), agentID)
toolStatus := deps.Cfg.Gateway.ToolStatus == nil || *deps.Cfg.Gateway.ToolStatus // default true
if deps.ChannelMgr != nil {
deps.ChannelMgr.RegisterRunWithDelivery(runID, msg.Channel, chatIDForRun, messageID, outMeta, msg.TenantID, channelStream, blockReply, toolStatus, chatBehavior, deliveryRuntime, reasoningDelivery)
@@ -611,17 +611,18 @@ func processNormalMessage(
}(agentID, msg.Channel, msg.ChatID, sessionKey, runID, peerKind, msg.Content, outMeta, blockReply, chatBehavior, channelStream, ptd, msg.TenantID, agentLoop.UUID(), agentLoop.OtherConfig())
}
func buildDeliveryRuntime(ctx context.Context, deps *ConsumerDeps, agentLoop agent.Agent, behavior channels.ResolvedChatBehavior, msg bus.InboundMessage, peerKind, channelType, agentKey string) channels.DeliveryRuntime {
func buildDeliveryRuntime(ctx context.Context, deps *ConsumerDeps, agentLoop agent.Agent, behavior channels.ResolvedChatBehavior, msg bus.InboundMessage, userID, peerKind, channelType, agentKey string) channels.DeliveryRuntime {
locale := msg.Metadata["locale"]
if locale == "" {
locale = "auto"
}
runtime := channels.DeliveryRuntime{
Locale: locale,
Inbound: msg.Content,
PeerKind: peerKind,
Channel: channelType,
AgentName: agentKey,
Locale: locale,
Inbound: msg.Content,
PeerKind: peerKind,
Channel: channelType,
AgentName: agentKey,
PersonaBrief: deliveryPersonaBrief(ctx, agentLoop, userID),
}
if behavior.Enabled && behavior.QuickAck.Enabled {
switch behavior.QuickAck.Mode {
@@ -638,6 +639,20 @@ func buildDeliveryRuntime(ctx context.Context, deps *ConsumerDeps, agentLoop age
return runtime
}
type deliveryPersonaProvider interface {
DeliveryPersonaBrief(context.Context, string) string
}
func deliveryPersonaBrief(ctx context.Context, agentLoop agent.Agent, userID string) string {
if agentLoop == nil {
return ""
}
if provider, ok := agentLoop.(deliveryPersonaProvider); ok {
return provider.DeliveryPersonaBrief(ctx, userID)
}
return ""
}
func buildDeliveryGenerator(ctx context.Context, deps *ConsumerDeps, agentLoop agent.Agent, tenantID uuid.UUID, providerName, model string) channels.DeliveryMessageGenerator {
provider := agentLoop.Provider()
resolvedProviderName := agentLoop.ProviderName()