|
1 | | -import type { IChatTransport, ChatEvent, ContentPart, Attachment, SessionItem, CreateSessionOpts, StoredMessage, UsageInfo, AppSettings, SessionStatus, ToolApprovalPayload, AskAnswerPayload, DirListResult, DriveEntry, QuickDir, FsTreeResult, FsReadResult, GitStatusResult, GitDiffResult, RemoteEntry, FsUploadOptions } from '@sbot/chat-ui' |
| 1 | +import type { IChatTransport, ChatEvent, ContentPart, Attachment, SessionItem, CreateSessionOpts, StoredMessage, UsageInfo, AppSettings, SessionStatus, ToolApprovalPayload, AskAnswerPayload, DirListResult, DriveEntry, QuickDir, FsTreeResult, FsReadResult, FsWriteResult, GitStatusResult, GitDiffResult, RemoteEntry, FsUploadOptions, FsUploadProgress } from '@sbot/chat-ui' |
2 | 2 |
|
3 | 3 | declare function acquireVsCodeApi(): { postMessage(msg: any): void } |
4 | 4 | const vscode = acquireVsCodeApi() |
5 | 5 |
|
6 | 6 | type EventHandler = (event: ChatEvent) => void |
| 7 | +type VscodeUploadFilePayload = { name: string; type: string; size: number; dataUrl: string } |
7 | 8 |
|
8 | 9 | let rpcId = 0 |
9 | 10 | const pending = new Map<number, { resolve: (v: any) => void; reject: (e: Error) => void }>() |
@@ -34,6 +35,22 @@ function cmd(method: string, ...args: any[]) { |
34 | 35 | vscode.postMessage({ type: 'cmd', method, args: JSON.parse(JSON.stringify(args)) }) |
35 | 36 | } |
36 | 37 |
|
| 38 | +function readFileAsDataUrl(file: File, onProgress?: (progress: FsUploadProgress) => void): Promise<string> { |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + const reader = new FileReader() |
| 41 | + reader.onprogress = (evt) => { |
| 42 | + const total = evt.lengthComputable ? evt.total : file.size |
| 43 | + const loaded = Math.min(evt.loaded, total || evt.loaded) |
| 44 | + const percent = total > 0 ? Math.min(95, Math.round((loaded / total) * 95)) : 95 |
| 45 | + onProgress?.({ loaded, total, percent }) |
| 46 | + } |
| 47 | + reader.onload = () => resolve(String(reader.result ?? '')) |
| 48 | + reader.onerror = () => reject(reader.error ?? new Error('Failed to read file')) |
| 49 | + reader.onabort = () => reject(new Error('File read aborted')) |
| 50 | + reader.readAsDataURL(file) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
37 | 54 | export class VsCodeTransport implements IChatTransport { |
38 | 55 | connect(): void { cmd('connect') } |
39 | 56 | disconnect(): void { cmd('disconnect') } |
@@ -63,11 +80,21 @@ export class VsCodeTransport implements IChatTransport { |
63 | 80 | listDrives(): Promise<DriveEntry[]> { return rpc('listDrives') } |
64 | 81 | mkdir(path: string): Promise<{ path: string }> { return rpc('mkdir', path) } |
65 | 82 | deleteEntry(path: string): Promise<{ path: string }> { return rpc('deleteEntry', path) } |
66 | | - uploadFile(_parentDir: string, _file: File, _options?: FsUploadOptions): Promise<{ path: string; size: number }> { |
67 | | - return Promise.reject(new Error('uploadFile is not supported in vscode webview')) |
| 83 | + async uploadFile(parentDir: string, file: File, options: FsUploadOptions = {}): Promise<{ path: string; size: number }> { |
| 84 | + options.onProgress?.({ loaded: 0, total: file.size, percent: 0 }) |
| 85 | + const payload: VscodeUploadFilePayload = { |
| 86 | + name: file.name, |
| 87 | + type: file.type, |
| 88 | + size: file.size, |
| 89 | + dataUrl: await readFileAsDataUrl(file, options.onProgress), |
| 90 | + } |
| 91 | + const result = await rpc('uploadFile', parentDir, payload, { overwrite: Boolean(options.overwrite) }) |
| 92 | + options.onProgress?.({ loaded: file.size, total: file.size, percent: 100 }) |
| 93 | + return result |
68 | 94 | } |
69 | 95 | listTree(path: string): Promise<FsTreeResult> { return rpc('listTree', path) } |
70 | 96 | readFile(path: string): Promise<FsReadResult> { return rpc('readFile', path) } |
| 97 | + writeFile(path: string, content: string, expectedMtime?: number): Promise<FsWriteResult> { return rpc('writeFile', path, content, expectedMtime) } |
71 | 98 | getRawFileUrl(_path: string): string { return '' } |
72 | 99 | gitStatus(root: string): Promise<GitStatusResult> { return rpc('gitStatus', root) } |
73 | 100 | gitDiff(root: string, path: string, fullContent = false): Promise<GitDiffResult> { return rpc('gitDiff', root, path, fullContent) } |
|
0 commit comments