okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
7e45521
main
1package daemon 2 3import ( 4"context" 5"io" 6"net/http" 7 8"albedo/cli/internal/daemon/protocol" 9) 10 11type ContextSection struct { 12ID ,Label ,Kind ,Source ,Preview string 13ItemCount ,ByteCount ,Pages int 14} 15type CompactionState struct { 16TriggerFreePercent * float64 17InputLimitTokens ,EstimatedInputTokens ,ProviderInputTokens ,ProviderCachedInputTokens ,BeforeItems ,AfterItems * int 18Strategy ,Status ,Source ,EstimateMethod string 19} 20type ContextSnapshot struct { 21SnapshotID string 22Compaction CompactionState 23CapturedAt * int64 24ContextWindowTokens * int 25State ,Reason ,Provider ,Model ,Protocol string 26Sections []ContextSection 27} 28type ContextPage struct { 29SnapshotID ,Section ,Content ,Omitted string 30Page ,Pages int 31} 32 33func GetContextSnapshot (ctx context.Context ,conn * Connection ,id string ) (ContextSnapshot ,error ) { 34var result ContextSnapshot 35err := executeRead (ctx ,conn ,operation {Capability :"context" ,Name :"read prepared context" ,BuildRequest :func (base string ,body io.Reader ) (* http.Request ,error ) { 36return protocol .NewGetContextRequest (base ,id ,& protocol.GetContextParams {View :new ("summary ")}) 37 },Policy :readRecovery },func (data []byte )error { 38var w protocol.ContextSummary 39if err := decodeRequired (data ,& w );err != nil { 40return err 41 } 42if w .State != "ready" && w .State != "pending" { 43return fieldError ("context state" ) 44 } 45if w .State == "ready" && value (w .SnapshotID )== "" { 46return fieldError ("context snapshot ID" ) 47 } 48c := w .Compaction 49result = ContextSnapshot {SnapshotID :value (w .SnapshotID ),CapturedAt :timestampMilliseconds (value (w .CapturedAt )),ContextWindowTokens :intPointer (w .ContextWindowTokens ),State :w .State ,Provider :value (w .Provider ),Model :value (w .Model ),Protocol :value (w .Protocol ),Compaction :CompactionState {TriggerFreePercent :c .TriggerFreePercent ,InputLimitTokens :intPointer (c .InputLimitTokens ),EstimatedInputTokens :intPointer (c .EstimatedInputTokens ),ProviderInputTokens :intPointer (c .ProviderInputTokens ),ProviderCachedInputTokens :intPointer (c .ProviderCachedInputTokens ),BeforeItems :intPointer (c .BeforeItems ),AfterItems :intPointer (c .AfterItems ),Strategy :value (c .Strategy ),Status :c .Status ,Source :value (c .Source ),EstimateMethod :value (c .EstimateMethod )}} 50if w .Reason != nil { 51result .Reason = w .Reason .Detail 52 } 53for _ ,section := range w .Sections { 54result .Sections = append (result .Sections ,ContextSection {ID :section .ID ,Label :section .Label ,Kind :section .Kind ,Source :value (section .Source ),Preview :section .Preview .Text ,ItemCount :int (section .ItemCount ),ByteCount :int (section .UTF8Bytes ),Pages :int (section .PageCount )}) 55 } 56return nil 57 }) 58return result ,err 59} 60func GetContextPage (ctx context.Context ,conn * Connection ,id ,snapshotID ,section string ,page int ) (ContextPage ,error ) { 61if snapshotID == "" { 62return ContextPage {},fieldError ("context snapshot ID" ) 63 } 64params := protocol.GetContextParams {View :new ("section "),SnapshotID :& snapshotID ,SectionID :& section ,Page :new (int64 (page ))} 65var result ContextPage 66err := executeRead (ctx ,conn ,operation {Capability :"context" ,Name :"read prepared context section" ,BuildRequest :func (base string ,body io.Reader ) (* http.Request ,error ) { 67return protocol .NewGetContextRequest (base ,id ,& params ) 68 },Policy :readRecovery },func (data []byte )error { 69var w protocol.ContextSectionPage 70if err := decodeRequired (data ,& w );err != nil { 71return err 72 } 73if w .SnapshotID != snapshotID || w .SectionID != section || w .Page != int64 (page )|| len (w .Text )> 32768 || w .PageCount < 1 || w .Page >= w .PageCount { 74return fieldError ("context section" ) 75 } 76result = ContextPage {SnapshotID :w .SnapshotID ,Section :w .SectionID ,Content :w .Text ,Omitted :value (w .Omitted ),Page :int (w .Page ),Pages :int (w .PageCount )} 77return nil 78 }) 79return result ,err 80} 81func (c * ChatClient )ContextWindow (ctx context.Context ) (* int ,error ) { 82snapshot ,err := GetContextSnapshot (ctx ,c .conn ,c .agentID ) 83return snapshot .ContextWindowTokens ,err 84}