~kris/9p

llm9p

llm9p/internal/llmfs/stream.go -rw-r--r-- 2.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
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
package llmfs

import (
	"context"
	"io"
	"strings"

	"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.Backend
}

// NewChunkFile creates the stream/chunk file
func NewChunkFile(client llm.Backend) *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
}

// StreamAskFile starts a streaming request
// Write a prompt to start streaming, then read chunks from stream/chunk
type StreamAskFile struct {
	*protocol.BaseFile
	client llm.Backend
}

// NewStreamAskFile creates the stream/ask file
func NewStreamAskFile(client llm.Backend) *StreamAskFile {
	return &StreamAskFile{
		BaseFile: protocol.NewBaseFile("ask", 0222), // write-only
		client:   client,
	}
}

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

func (f *StreamAskFile) Write(p []byte, offset int64) (int, error) {
	prompt := strings.TrimSpace(string(p))
	if prompt == "" {
		return len(p), nil
	}

	// Start streaming - chunks will be available via stream/chunk
	err := f.client.StartStream(context.Background(), prompt)
	if err != nil {
		// Return error to indicate stream failed to start
		return 0, err
	}

	return len(p), nil
}

func (f *StreamAskFile) Stat() protocol.Stat {
	return f.BaseFile.Stat()
}