~kris/9p

llm9p

llm9p/internal/llmfs/session_ctl.go -rw-r--r-- 1.1 KiB
cc50c152 — Kris Yotam docs: update repo URL to sr.ht 4 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"
)

// SessionCtlFile is the control file for a session: /n/llm/N/ctl
// Supports commands: "reset" (clear history), "close" (remove session)
type SessionCtlFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionCtlFile creates a ctl file for the given session.
func NewSessionCtlFile(sm *llm.SessionManager, id int) *SessionCtlFile {
	return &SessionCtlFile{
		BaseFile: protocol.NewBaseFile("ctl", 0222),
		sm:       sm,
		id:       id,
	}
}

// Read returns empty for the control file.
func (f *SessionCtlFile) Read(p []byte, offset int64) (int, error) {
	return 0, io.EOF
}

// Write processes control commands.
func (f *SessionCtlFile) Write(p []byte, offset int64) (int, error) {
	cmd := strings.TrimSpace(string(p))

	switch cmd {
	case "reset":
		f.sm.Reset(f.id)
	case "close":
		f.sm.Close(f.id)
	default:
		return 0, protocol.Error("unknown command: " + cmd)
	}

	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionCtlFile) Stat() protocol.Stat {
	return f.BaseFile.Stat()
}