~kris/9p

llm9p

ref: a3dc06aa1a37febcbe7d8d340be3d9b5a2a3f6ea llm9p/internal/llmfs/prefill.go -rw-r--r-- 1.2 KiB
a3dc06aa — pdfinn feat(llm9p): Implement clone-based session architecture 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
package llmfs

import (
	"io"
	"strings"

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

// PrefillFile exposes the assistant response prefill (read/write).
// Prefill helps keep the model in character by prepending a string
// to the assistant's response (e.g., "[Veltro] ").
type PrefillFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewPrefillFile creates the prefill file
func NewPrefillFile(client llm.Backend) *PrefillFile {
	return &PrefillFile{
		BaseFile: protocol.NewBaseFile("prefill", 0666),
		client:   client,
	}
}

func (f *PrefillFile) Read(p []byte, offset int64) (int, error) {
	content := f.client.Prefill()
	if content != "" {
		content += "\n"
	}
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	n := copy(p, content[offset:])
	return n, nil
}

func (f *PrefillFile) Write(p []byte, offset int64) (int, error) {
	prefill := strings.TrimSpace(string(p))
	f.client.SetPrefill(prefill)
	return len(p), nil
}

func (f *PrefillFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	content := f.client.Prefill()
	if content != "" {
		s.Length = uint64(len(content) + 1) // +1 for newline
	} else {
		s.Length = 0
	}
	return s
}