Skip to content

Commit 8754557

Browse files
authored
{session/summary, examples}: introduce session summary and add summarization example (#256)
Introduce a powerful session summary.
1 parent 03c18d7 commit 8754557

30 files changed

+4871
-81
lines changed

agent/llmagent/llm_agent.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ func WithAddContextPrefix(addPrefix bool) Option {
250250
}
251251
}
252252

253+
// WithAddSessionSummary controls whether to prepend the current-branch summary
254+
// as a system message in the request context when available.
255+
func WithAddSessionSummary(addSummary bool) Option {
256+
return func(opts *Options) {
257+
opts.AddSessionSummary = addSummary
258+
}
259+
}
260+
261+
// WithMaxHistoryRuns sets the maximum number of history messages when AddSessionSummary is false.
262+
// When 0 (default), no limit is applied.
263+
func WithMaxHistoryRuns(maxRuns int) Option {
264+
return func(opts *Options) {
265+
opts.MaxHistoryRuns = maxRuns
266+
}
267+
}
268+
253269
// WithKnowledgeFilter sets the knowledge filter for the knowledge base.
254270
func WithKnowledgeFilter(filter map[string]interface{}) Option {
255271
return func(opts *Options) {
@@ -344,6 +360,14 @@ type Options struct {
344360
// When false, foreign agent events are passed directly without the prefix.
345361
AddContextPrefix bool
346362

363+
// AddSessionSummary controls whether to prepend the current branch summary
364+
// as a system message when available (default: false).
365+
AddSessionSummary bool
366+
367+
// MaxHistoryRuns sets the maximum number of history messages when AddSessionSummary is false.
368+
// When 0 (default), no limit is applied.
369+
MaxHistoryRuns int
370+
347371
// StructuredOutput defines how the model should produce structured output in normal runs.
348372
StructuredOutput *model.StructuredOutput
349373
// StructuredOutputType is the reflect.Type of the example pointer used to generate the schema.
@@ -525,6 +549,8 @@ func buildRequestProcessors(name string, options *Options) []flow.RequestProcess
525549
// 6. Content processor - handles messages from invocation.
526550
contentProcessor := processor.NewContentRequestProcessor(
527551
processor.WithAddContextPrefix(options.AddContextPrefix),
552+
processor.WithAddSessionSummary(options.AddSessionSummary),
553+
processor.WithMaxHistoryRuns(options.MaxHistoryRuns),
528554
)
529555
requestProcessors = append(requestProcessors, contentProcessor)
530556

agent/llmagent/llm_agent_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"trpc.group/trpc-go/trpc-agent-go/agent"
2121
"trpc.group/trpc-go/trpc-agent-go/event"
22+
"trpc.group/trpc-go/trpc-agent-go/internal/flow/processor"
2223
"trpc.group/trpc-go/trpc-agent-go/knowledge"
2324
"trpc.group/trpc-go/trpc-agent-go/knowledge/document"
2425
"trpc.group/trpc-go/trpc-agent-go/model"
@@ -45,6 +46,90 @@ func TestLLMAgent_SubAgents(t *testing.T) {
4546
}
4647
}
4748

49+
// Test that buildRequestProcessors wires AddSessionSummary into
50+
// ContentRequestProcessor correctly.
51+
func TestBuildRequestProcessors_AddSessionSummaryWiring(t *testing.T) {
52+
// true case - test that WithAddSessionSummary(true) is properly wired.
53+
optsTrue := &Options{}
54+
WithAddSessionSummary(true)(optsTrue)
55+
procs := buildRequestProcessors("test-agent", optsTrue)
56+
var crp *processor.ContentRequestProcessor
57+
for _, p := range procs {
58+
if v, ok := p.(*processor.ContentRequestProcessor); ok {
59+
crp = v
60+
}
61+
}
62+
require.NotNil(t, crp)
63+
require.True(t, crp.AddSessionSummary)
64+
65+
// false case - test that WithAddSessionSummary(false) is properly wired.
66+
optsFalse := &Options{}
67+
WithAddSessionSummary(false)(optsFalse)
68+
procs = buildRequestProcessors("test-agent", optsFalse)
69+
crp = nil
70+
for _, p := range procs {
71+
if v, ok := p.(*processor.ContentRequestProcessor); ok {
72+
crp = v
73+
}
74+
}
75+
require.NotNil(t, crp)
76+
require.False(t, crp.AddSessionSummary)
77+
}
78+
79+
// Test that buildRequestProcessors wires MaxHistoryRuns into
80+
// ContentRequestProcessor correctly.
81+
func TestBuildRequestProcessors_MaxHistoryRunsWiring(t *testing.T) {
82+
// Test with MaxHistoryRuns set - test that WithMaxHistoryRuns(10) is properly wired.
83+
optsWithMax := &Options{}
84+
WithMaxHistoryRuns(10)(optsWithMax)
85+
procs := buildRequestProcessors("test-agent", optsWithMax)
86+
var crp *processor.ContentRequestProcessor
87+
for _, p := range procs {
88+
if v, ok := p.(*processor.ContentRequestProcessor); ok {
89+
crp = v
90+
}
91+
}
92+
require.NotNil(t, crp)
93+
require.Equal(t, 10, crp.MaxHistoryRuns)
94+
95+
// Test with default value (0) - test that WithMaxHistoryRuns(0) is properly wired.
96+
optsDefault := &Options{}
97+
WithMaxHistoryRuns(0)(optsDefault)
98+
procs = buildRequestProcessors("test-agent", optsDefault)
99+
crp = nil
100+
for _, p := range procs {
101+
if v, ok := p.(*processor.ContentRequestProcessor); ok {
102+
crp = v
103+
}
104+
}
105+
require.NotNil(t, crp)
106+
require.Equal(t, 0, crp.MaxHistoryRuns)
107+
}
108+
109+
// Test that WithAddSessionSummary option sets the AddSessionSummary field correctly.
110+
func TestWithAddSessionSummary_Option(t *testing.T) {
111+
opts := &Options{}
112+
WithAddSessionSummary(true)(opts)
113+
require.True(t, opts.AddSessionSummary)
114+
115+
// Test with false value
116+
opts = &Options{}
117+
WithAddSessionSummary(false)(opts)
118+
require.False(t, opts.AddSessionSummary)
119+
}
120+
121+
// Test that WithMaxHistoryRuns option sets the MaxHistoryRuns field correctly.
122+
func TestWithMaxHistoryRuns_Option(t *testing.T) {
123+
opts := &Options{}
124+
WithMaxHistoryRuns(5)(opts)
125+
require.Equal(t, 5, opts.MaxHistoryRuns)
126+
127+
// Test with zero value
128+
opts = &Options{}
129+
WithMaxHistoryRuns(0)(opts)
130+
require.Equal(t, 0, opts.MaxHistoryRuns)
131+
}
132+
48133
func TestLLMAgent_Run_BeforeAgentShort(t *testing.T) {
49134
// BeforeAgentCallback returns a custom response, should short-circuit.
50135
agentCallbacks := agent.NewCallbacks()

0 commit comments

Comments
 (0)