~kris/9p

llm9p

ref: 42c2e6958db4e870f21ce0b60b7522975cd8757f llm9p/internal/llmfs/thinking.go -rw-r--r-- 1.8 KiB
42c2e695 — pdfinn fix: extract tool args and fix tool result history for OpenAI path 5 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
package llmfs

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

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

// ThinkingFile exposes the thinking token budget (read/write)
// Values: -1 = max (31999), 0 = disabled, >0 = specific budget
// Only effective with CLI backend; API backend ignores this setting.
type ThinkingFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewThinkingFile creates the thinking file
func NewThinkingFile(client llm.Backend) *ThinkingFile {
	return &ThinkingFile{
		BaseFile: protocol.NewBaseFile("thinking", 0666),
		client:   client,
	}
}

func (f *ThinkingFile) Read(p []byte, offset int64) (int, error) {
	tokens := f.client.ThinkingTokens()
	var content string
	switch {
	case tokens < 0:
		content = "max\n"
	case tokens == 0:
		content = "off\n"
	default:
		content = fmt.Sprintf("%d\n", tokens)
	}
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	n := copy(p, content[offset:])
	return n, nil
}

func (f *ThinkingFile) Write(p []byte, offset int64) (int, error) {
	input := strings.TrimSpace(string(p))
	input = strings.ToLower(input)

	var tokens int
	switch input {
	case "max", "on", "true", "enabled", "-1":
		tokens = -1
	case "off", "false", "disabled", "0":
		tokens = 0
	default:
		var err error
		tokens, err = strconv.Atoi(input)
		if err != nil {
			return 0, fmt.Errorf("invalid thinking value: use 'max', 'off', or a number")
		}
		if tokens < 0 {
			tokens = -1 // Treat any negative as max
		}
	}

	f.client.SetThinkingTokens(tokens)
	return len(p), nil
}

func (f *ThinkingFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	tokens := f.client.ThinkingTokens()
	var content string
	switch {
	case tokens < 0:
		content = "max\n"
	case tokens == 0:
		content = "off\n"
	default:
		content = fmt.Sprintf("%d\n", tokens)
	}
	s.Length = uint64(len(content))
	return s
}