~kris/9p

llm9p

ref: 35c20f3e6e008970ccdc9ea460682c578bd6d35a llm9p/internal/llmfs/system.go -rw-r--r-- 1.1 KiB
35c20f3e — pdfinn fix(client): replace empty text content block with placeholder 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
51
52
package llmfs

import (
	"io"
	"strings"

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

// SystemFile exposes the system prompt (read/write)
type SystemFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewSystemFile creates the system file
func NewSystemFile(client llm.Backend) *SystemFile {
	return &SystemFile{
		BaseFile: protocol.NewBaseFile("system", 0666),
		client:   client,
	}
}

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

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

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