~kris/9p

llm9p

ref: 68199d2ad65152fa84d9c9be5008dc7659afdda0 llm9p/internal/llmfs/stream.go -rw-r--r-- 1.1 KiB
68199d2a — pdfinn feat: Initial implementation of llm9p - LLM as 9P filesystem 7 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
package llmfs

import (
	"io"

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

// ChunkFile provides streaming access to LLM responses
// Reading blocks until the next chunk is available, then returns it
// Returns EOF when the stream is complete
type ChunkFile struct {
	*protocol.BaseFile
	client *llm.Client
}

// NewChunkFile creates the stream/chunk file
func NewChunkFile(client *llm.Client) *ChunkFile {
	return &ChunkFile{
		BaseFile: protocol.NewBaseFile("chunk", 0444),
		client:   client,
	}
}

func (f *ChunkFile) Read(p []byte, offset int64) (int, error) {
	// If no stream is active, return EOF
	if !f.client.IsStreaming() {
		return 0, io.EOF
	}

	// Block until we get a chunk
	chunk, ok := f.client.ReadStreamChunk()
	if !ok {
		// Stream ended
		return 0, io.EOF
	}

	// Copy the chunk to the buffer
	n := copy(p, chunk)
	return n, nil
}

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

func (f *ChunkFile) Stat() protocol.Stat {
	s := f.BaseFile.Stat()
	// Length is unknown for streaming
	s.Length = 0
	return s
}