(path: string)
| 184 | * This function wraps the result uniformly as ReadFileResult |
| 185 | */ |
| 186 | export const readFile = async (path: string): Promise<ReadFileResult> => { |
| 187 | const manager = getClientComManager(); |
| 188 | const normalized = normalizePath(path); |
| 189 | // file_path without leading slash, e.g. "posts/tweet_123.json" |
| 190 | const filePath = normalized.replace(/^\//, ''); |
| 191 | const startTime = ts(); |
| 192 | const t0 = performance.now(); |
| 193 | const result = await manager.getFile<unknown>({ file_path: filePath }); |
| 194 | console.info( |
| 195 | `[FileApi][${startTime}] readFile "${filePath}" — ${(performance.now() - t0).toFixed(1)}ms`, |
| 196 | ); |
| 197 | |
| 198 | if (result === null || result === undefined) { |
| 199 | return { content: undefined, metadata: {} }; |
| 200 | } |
| 201 | |
| 202 | // Check if the return value is a ReadFileResult structure (has both content and metadata fields, and metadata is an object) |
| 203 | if ( |
| 204 | typeof result === 'object' && |
| 205 | !Array.isArray(result) && |
| 206 | 'content' in result && |
| 207 | 'metadata' in result && |
| 208 | typeof (result as Record<string, unknown>).metadata === 'object' |
| 209 | ) { |
| 210 | return result as ReadFileResult; |
| 211 | } |
| 212 | |
| 213 | // Server returned file content directly; wrap as ReadFileResult |
| 214 | return { content: result, metadata: {} }; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * Write file |
no test coverage detected