Read 实现 BackendProtocol.Read
(ctx context.Context, path string, offset, limit int)
| 66 | |
| 67 | // Read 实现 BackendProtocol.Read |
| 68 | func (b *FilesystemBackend) Read(ctx context.Context, path string, offset, limit int) (string, error) { |
| 69 | // 使用 SandboxFS 读取文件 |
| 70 | content, err := b.fs.Read(ctx, path) |
| 71 | if err != nil { |
| 72 | return "", err |
| 73 | } |
| 74 | |
| 75 | // 分割成行 |
| 76 | lines := strings.Split(content, "\n") |
| 77 | totalLines := len(lines) |
| 78 | |
| 79 | // 处理 offset |
| 80 | if offset < 0 { |
| 81 | offset = 0 |
| 82 | } |
| 83 | if offset >= totalLines { |
| 84 | return "", nil |
| 85 | } |
| 86 | |
| 87 | // 处理 limit |
| 88 | endLine := totalLines |
| 89 | if limit > 0 { |
| 90 | endLine = min(offset+limit, totalLines) |
| 91 | } |
| 92 | |
| 93 | selectedLines := lines[offset:endLine] |
| 94 | return strings.Join(selectedLines, "\n"), nil |
| 95 | } |
| 96 | |
| 97 | // Write 实现 BackendProtocol.Write |
| 98 | func (b *FilesystemBackend) Write(ctx context.Context, path, content string) (*WriteResult, error) { |