返回 DeepSeek-Reasonix
compress.go
根目录 / internal / tool / compress.go
1 package tool
2
3 import "context"
4
5 // CompressRequest describes one explicit, model-requested context fold.
6 type CompressRequest struct {
7 Direction string
8 Anchor string
9 Focus string
10 }
11
12 // CompressResult is the stable model-facing outcome of a context fold.
13 type CompressResult struct {
14 Status string `json:"status"`
15 Direction string `json:"direction"`
16 Anchor string `json:"anchor"`
17 Messages int `json:"messages"`
18 SourceTokens int `json:"source_tokens"`
19 ProjectionTokens int `json:"projection_tokens"`
20 Mode string `json:"mode"`
21 Reason string `json:"reason"`
22 }
23
24 // ContextCompressor is supplied by the Agent that owns the active session.
25 // Keeping the binding on the call context lets parent and child agents share a
26 // stable tool schema without sharing projection state.
27 type ContextCompressor interface {
28 CompressContext(context.Context, CompressRequest) (CompressResult, error)
29 }
30
31 type contextCompressorKey struct{}
32
33 // WithContextCompressor binds the active session's compressor to a tool call.
34 func WithContextCompressor(ctx context.Context, compressor ContextCompressor) context.Context {
35 if compressor == nil {
36 return ctx
37 }
38 return context.WithValue(ctx, contextCompressorKey{}, compressor)
39 }
40
41 // ContextCompressorFromContext returns the compressor bound to this tool call.
42 func ContextCompressorFromContext(ctx context.Context) (ContextCompressor, bool) {
43 compressor, ok := ctx.Value(contextCompressorKey{}).(ContextCompressor)
44 return compressor, ok && compressor != nil
45 }
46
46 lines GO