~kris/9p

llm9p

ref: 42c2e6958db4e870f21ce0b60b7522975cd8757f llm9p/internal/llmfs/session_settings.go -rw-r--r-- 8.0 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package llmfs

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

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

// SessionModelFile controls the model for a session: /n/llm/N/model
type SessionModelFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionModelFile creates a model file for the given session.
func NewSessionModelFile(sm *llm.SessionManager, id int) *SessionModelFile {
	return &SessionModelFile{
		BaseFile: protocol.NewBaseFile("model", 0666),
		sm:       sm,
		id:       id,
	}
}

// Read returns the current model name.
func (f *SessionModelFile) Read(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	content := session.Model() + "\n"
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// modelAliases maps short convenience names to full Anthropic model IDs.
var modelAliases = map[string]string{
	"haiku":  "claude-haiku-4-5-20251001",
	"sonnet": "claude-sonnet-4-5-20250929",
	"opus":   "claude-opus-4-5-20251101",
}

// resolveModel expands a short alias to its full model ID, or returns the
// input unchanged if it is already a full ID (or unknown).
func resolveModel(name string) string {
	if full, ok := modelAliases[strings.ToLower(name)]; ok {
		return full
	}
	return name
}

// Write sets the model name.
func (f *SessionModelFile) Write(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	model := resolveModel(strings.TrimSpace(string(p)))
	if model != "" {
		session.SetModel(model)
	}
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionModelFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	session := f.sm.Get(f.id)
	if session != nil {
		s.Length = uint64(len(session.Model()) + 1)
	}
	return s
}

// SessionTemperatureFile controls the temperature for a session: /n/llm/N/temperature
type SessionTemperatureFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionTemperatureFile creates a temperature file for the given session.
func NewSessionTemperatureFile(sm *llm.SessionManager, id int) *SessionTemperatureFile {
	return &SessionTemperatureFile{
		BaseFile: protocol.NewBaseFile("temperature", 0666),
		sm:       sm,
		id:       id,
	}
}

// Read returns the current temperature.
func (f *SessionTemperatureFile) Read(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	content := fmt.Sprintf("%.2f\n", session.Temperature())
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// Write sets the temperature.
func (f *SessionTemperatureFile) Write(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	temp, err := strconv.ParseFloat(strings.TrimSpace(string(p)), 64)
	if err != nil {
		return 0, protocol.Error("invalid temperature: " + err.Error())
	}
	if temp < 0.0 || temp > 2.0 {
		return 0, protocol.Error("temperature must be between 0.0 and 2.0")
	}
	session.SetTemperature(temp)
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionTemperatureFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	session := f.sm.Get(f.id)
	if session != nil {
		s.Length = uint64(len(fmt.Sprintf("%.2f\n", session.Temperature())))
	}
	return s
}

// SessionSystemFile controls the system prompt for a session: /n/llm/N/system
type SessionSystemFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionSystemFile creates a system file for the given session.
func NewSessionSystemFile(sm *llm.SessionManager, id int) *SessionSystemFile {
	return &SessionSystemFile{
		BaseFile: protocol.NewBaseFile("system", 0666),
		sm:       sm,
		id:       id,
	}
}

// Read returns the current system prompt.
func (f *SessionSystemFile) Read(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	content := session.SystemPrompt()
	if content != "" && !strings.HasSuffix(content, "\n") {
		content += "\n"
	}
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// Write sets the system prompt.
func (f *SessionSystemFile) Write(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	prompt := strings.TrimSpace(string(p))
	session.SetSystemPrompt(prompt)
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionSystemFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	session := f.sm.Get(f.id)
	if session != nil {
		content := session.SystemPrompt()
		if content != "" {
			s.Length = uint64(len(content) + 1)
		}
	}
	return s
}

// SessionThinkingFile controls the thinking token budget: /n/llm/N/thinking
type SessionThinkingFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionThinkingFile creates a thinking file for the given session.
func NewSessionThinkingFile(sm *llm.SessionManager, id int) *SessionThinkingFile {
	return &SessionThinkingFile{
		BaseFile: protocol.NewBaseFile("thinking", 0666),
		sm:       sm,
		id:       id,
	}
}

// Read returns the current thinking token budget.
func (f *SessionThinkingFile) Read(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	tokens := session.ThinkingTokens()
	var content string
	switch {
	case tokens < 0:
		content = "max\n"
	case tokens == 0:
		content = "disabled\n"
	default:
		content = fmt.Sprintf("%d\n", tokens)
	}

	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// Write sets the thinking token budget.
func (f *SessionThinkingFile) Write(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	value := strings.TrimSpace(string(p))
	switch value {
	case "max", "-1":
		session.SetThinkingTokens(-1)
	case "disabled", "off", "0":
		session.SetThinkingTokens(0)
	default:
		tokens, err := strconv.Atoi(value)
		if err != nil {
			return 0, protocol.Error("invalid thinking budget: " + err.Error())
		}
		session.SetThinkingTokens(tokens)
	}
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionThinkingFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	// Estimate length
	s.Length = 16
	return s
}

// SessionPrefillFile controls the response prefill: /n/llm/N/prefill
type SessionPrefillFile struct {
	*protocol.BaseFile
	sm *llm.SessionManager
	id int
}

// NewSessionPrefillFile creates a prefill file for the given session.
func NewSessionPrefillFile(sm *llm.SessionManager, id int) *SessionPrefillFile {
	return &SessionPrefillFile{
		BaseFile: protocol.NewBaseFile("prefill", 0666),
		sm:       sm,
		id:       id,
	}
}

// Read returns the current prefill string.
func (f *SessionPrefillFile) Read(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	content := session.Prefill()
	if content != "" && !strings.HasSuffix(content, "\n") {
		content += "\n"
	}
	if offset >= int64(len(content)) {
		return 0, io.EOF
	}
	return copy(p, content[offset:]), nil
}

// Write sets the prefill string.
func (f *SessionPrefillFile) Write(p []byte, offset int64) (int, error) {
	session := f.sm.Get(f.id)
	if session == nil {
		return 0, protocol.ErrNotFound
	}

	// Don't trim - prefill may have intentional trailing space
	prefill := string(p)
	// But do remove trailing newline since shell adds it
	prefill = strings.TrimSuffix(prefill, "\n")
	session.SetPrefill(prefill)
	return len(p), nil
}

// Stat returns the file's metadata.
func (f *SessionPrefillFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	session := f.sm.Get(f.id)
	if session != nil {
		content := session.Prefill()
		if content != "" {
			s.Length = uint64(len(content) + 1)
		}
	}
	return s
}