@@ -28,84 +28,24 @@ import (
2828 "github.com/cloudwego/eino/schema"
2929)
3030
31- type DefaultFinalizerConfig [M adk.MessageType ] struct {
32- // PreserveUserMessages controls whether to preserve original user messages in the summary.
33- // When enabled, replaces the <all_user_messages>...</all_user_messages> section in the
34- // model-generated summary with recent original user messages from the conversation.
35- // Optional. Enabled by default when config is nil or Enabled is true.
36- PreserveUserMessages * TypedPreserveUserMessages [M ]
37-
38- // TranscriptFilePath is the path to the file containing the full conversation history.
39- // When set, appends a note to the summary indicating where to find the original context.
40- // Optional.
41- TranscriptFilePath string
42- }
43-
44- // DefaultFinalizer is the default TypedFinalizeFunc implementation, providing the same
45- // summary post-processing as the middleware does.
46- // e.g. replacing user messages in the summary.
47- func DefaultFinalizer [M adk.MessageType ](cfg * DefaultFinalizerConfig [M ]) (TypedFinalizeFunc [M ], error ) {
48- const defaultPreserveUserMessagesMaxTokens = 30000
49-
50- if cfg == nil {
51- cfg = & DefaultFinalizerConfig [M ]{}
52- }
53-
54- preserveEnabled := cfg .PreserveUserMessages == nil || cfg .PreserveUserMessages .Enabled
55- preserveCfg := cfg .PreserveUserMessages
56- transcriptPath := cfg .TranscriptFilePath
57-
58- if preserveCfg != nil && preserveCfg .MaxTokens < 0 {
59- return nil , fmt .Errorf ("preserveUserMessages.MaxTokens must be non-negative" )
31+ // DefaultFinalize is the default TypedFinalizeFunc implementation, providing the same
32+ // summary post-processing as the middleware does internally:
33+ // 1. Replaces the <all_user_messages>...</all_user_messages> section in the model-generated
34+ // summary with recent original user messages from the conversation (up to ~30k tokens).
35+ // 2. Adds a preamble and a postamble around the summary content.
36+ // 3. Converts the summary into a user message, prepended with the original system messages.
37+ func DefaultFinalize [M adk.MessageType ](ctx context.Context , originalMessages []M , summary M ) ([]M , error ) {
38+ systemMsgs , contextMsgs := splitSystemAndContextMsgs (originalMessages )
39+
40+ processed , err := postProcessSummary (ctx , & postProcessSummaryParams [M ]{
41+ contextMsgs : contextMsgs ,
42+ summaryContent : getAssistantTextContent (summary ),
43+ })
44+ if err != nil {
45+ return nil , err
6046 }
6147
62- return func (ctx context.Context , originalMessages []M , summary M ) ([]M , error ) {
63- var systemMsgs []M
64- var contextMsgs []M
65- for i , msg := range originalMessages {
66- if ! isSystemRole (msg ) {
67- systemMsgs = originalMessages [:i ]
68- contextMsgs = originalMessages [i :]
69- break
70- }
71- }
72-
73- content := getUserMsgTextContent (summary )
74-
75- if preserveEnabled && len (contextMsgs ) > 0 {
76- maxTokens := defaultPreserveUserMessagesMaxTokens
77- if preserveCfg != nil && preserveCfg .MaxTokens > 0 {
78- maxTokens = preserveCfg .MaxTokens
79- }
80-
81- var filter TypedUserMessageFilterFunc [M ]
82- if preserveCfg != nil {
83- filter = preserveCfg .Filter
84- }
85-
86- newContent , err := replaceUserMessagesInSummary (ctx , & replaceUserMessagesInSummaryParams [M ]{
87- contextMsgs : contextMsgs ,
88- summaryText : content ,
89- maxTokens : maxTokens ,
90- filter : filter ,
91- tokenCounter : nil ,
92- })
93- if err != nil {
94- return nil , err
95- }
96- content = newContent
97- }
98-
99- if transcriptPath != "" {
100- content = appendSection (content , fmt .Sprintf (getTranscriptPathInstruction (), transcriptPath ))
101- }
102-
103- content = appendSection (getSummaryPreamble (), content )
104-
105- newSummary := overwriteMsgContent (summary , content , getContinueInstruction ())
106-
107- return append (systemMsgs , newSummary ), nil
108- }, nil
48+ return append (systemMsgs , processed ), nil
10949}
11050
11151// TypedFinalizerBuilder builds a TypedFinalizeFunc by chaining handlers
0 commit comments