~kris/9p

llm9p

ref: 6da61fd486865181bee4d27e82eb163154171892 llm9p/internal/llmfs/state.go -rw-r--r-- 2.1 KiB
6da61fd4 — pdfinn feat(llm9p): per-session compact and usage files for context window management 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
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
82
83
84
85
86
87
88
89
90
91
package llmfs

import (
	"fmt"
	"io"
	"strconv"
	"strings"

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

// ModelFile exposes the current model name (read/write)
type ModelFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewModelFile creates the model file
func NewModelFile(client llm.Backend) *ModelFile {
	return &ModelFile{
		BaseFile: protocol.NewBaseFile("model", 0666),
		client:   client,
	}
}

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

func (f *ModelFile) Write(p []byte, offset int64) (int, error) {
	model := strings.TrimSpace(string(p))
	if model == "" {
		return 0, fmt.Errorf("model name cannot be empty")
	}
	f.client.SetModel(model)
	return len(p), nil
}

func (f *ModelFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	s.Length = uint64(len(f.client.Model()) + 1) // +1 for newline
	return s
}

// TemperatureFile exposes the current temperature (read/write)
type TemperatureFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewTemperatureFile creates the temperature file
func NewTemperatureFile(client llm.Backend) *TemperatureFile {
	return &TemperatureFile{
		BaseFile: protocol.NewBaseFile("temperature", 0666),
		client:   client,
	}
}

func (f *TemperatureFile) Read(p []byte, offset int64) (int, error) {
	content := fmt.Sprintf("%.2f\n", f.client.Temperature())
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	n := copy(p, content[offset:])
	return n, nil
}

func (f *TemperatureFile) Write(p []byte, offset int64) (int, error) {
	tempStr := strings.TrimSpace(string(p))
	temp, err := strconv.ParseFloat(tempStr, 64)
	if err != nil {
		return 0, fmt.Errorf("invalid temperature: %w", err)
	}
	if err := f.client.SetTemperature(temp); err != nil {
		return 0, err
	}
	return len(p), nil
}

func (f *TemperatureFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	content := fmt.Sprintf("%.2f\n", f.client.Temperature())
	s.Length = uint64(len(content))
	return s
}