okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(cli): use generated response field metadata7e45521

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