~kris/9p

llm9p

ref: c94202ec097d36893d9ee9ff9b0914c2576cfb13 llm9p/internal/llmfs/tokens.go -rw-r--r-- 958 bytes
c94202ec — pdfinn fix(llm): harden tool-use history to prevent cascading failures 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
package llmfs

import (
	"fmt"
	"io"

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

// TokensFile exposes the last response token count (read-only)
type TokensFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewTokensFile creates the tokens file
func NewTokensFile(client llm.Backend) *TokensFile {
	return &TokensFile{
		BaseFile: protocol.NewBaseFile("tokens", 0444),
		client:   client,
	}
}

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

func (f *TokensFile) Write(p []byte, offset int64) (int, error) {
	return 0, protocol.ErrPermission
}

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