~kris/9p

llm9p

ref: ed43a61bf88f3238d4844391e1f7926d8490ae50 llm9p/internal/llmfs/context.go -rw-r--r-- 2.2 KiB
ed43a61b — pdfinn feat(llm9p): Add per-fid session isolation and prefill support 7 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package llmfs

import (
	"io"
	"strings"

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

// ContextFile exposes the conversation history.
// It implements FidAwareFile to provide per-fid session isolation.
// Read: returns JSON of conversation history for this fid's session
// Write: appends a system message to this fid's session context
type ContextFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
}

// NewContextFile creates the context file
func NewContextFile(sm *llm.SessionManager) *ContextFile {
	return &ContextFile{
		BaseFile: protocol.NewBaseFile("context", 0666),
		sm:       sm,
	}
}

// Read implements File.Read (fallback for non-fid-aware access)
func (f *ContextFile) Read(p []byte, offset int64) (int, error) {
	// Without fid context, return empty
	return 0, io.EOF
}

// Write implements File.Write (fallback for non-fid-aware access)
func (f *ContextFile) Write(p []byte, offset int64) (int, error) {
	// Without fid context, we can't add to a specific session
	return 0, protocol.ErrPermission
}

// ReadFid implements FidAwareFile.ReadFid
func (f *ContextFile) ReadFid(fid uint32, p []byte, offset int64) (int, error) {
	session := f.sm.GetOrCreate(fid)
	content, err := session.MessagesJSON()
	if err != nil {
		return 0, err
	}
	// Add newline
	content = append(content, '\n')

	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	n := copy(p, content[offset:])
	return n, nil
}

// WriteFid implements FidAwareFile.WriteFid
func (f *ContextFile) WriteFid(fid uint32, p []byte, offset int64) (int, error) {
	// Writing appends a system message to this fid's session context
	msg := strings.TrimSpace(string(p))
	if msg != "" {
		session := f.sm.GetOrCreate(fid)
		session.AddSystemMessage(msg)
	}
	return len(p), nil
}

// CloseFid implements FidAwareFile.CloseFid
func (f *ContextFile) CloseFid(fid uint32) error {
	// No per-fid state to clean up for this file
	// (session cleanup is handled by AskFile)
	return nil
}

// Stat returns the file's metadata
func (f *ContextFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	// Length is dynamic based on session, but without fid context we return 0
	s.Length = 0
	return s
}