~kris/9p

llm9p

ref: 4191d4e78cf8495fd39a4588ec5649646ae2df88 llm9p/internal/llmfs/session_compact.go -rw-r--r-- 1.4 KiB
4191d4e7 — pdfinn fix(llmfs): correct model alias IDs to match actual Anthropic API 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package llmfs

import (
	"context"
	"io"

	"github.com/NERVsystems/llm9p/internal/llm"
	"github.com/NERVsystems/llm9p/internal/protocol"
)

// SessionCompactFile triggers conversation compaction for a session: /n/llm/N/compact
// Write any content to trigger; read returns "ok\n" or an error message.
type SessionCompactFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionCompactFile creates a compact trigger file for the given session.
func NewSessionCompactFile(sm *llm.SessionManager, id int) *SessionCompactFile {
	return &SessionCompactFile{
		BaseFile: protocol.NewBaseFile("compact", 0644),
		sm:       sm,
		id:       id,
	}
}

// Read returns a status line.
func (f *SessionCompactFile) Read(p []byte, offset int64) (int, error) {
	content := "write to compact conversation\n"
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// Write triggers compaction of the session's conversation history.
func (f *SessionCompactFile) Write(p []byte, offset int64) (int, error) {
	if err := f.sm.Compact(context.Background(), f.id); err != nil {
		return 0, protocol.Error("compact: " + err.Error())
	}
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionCompactFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	s.Length = uint64(len("write to compact conversation\n"))
	return s
}