Skip to content

Commit 4156e21

Browse files
committed
improvement(cli): name the metadata command when an embedded download refuses binary output
1 parent ff67ee3 commit 4156e21

3 files changed

Lines changed: 41 additions & 4 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { runEmbeddedCli } from '#sim-cli/embed'
3+
4+
const identity = { endpoint: 'https://sim.test', apiKey: 'fixture', workspaceId: 'workspace' }
5+
6+
describe('an embedded binary refusal names the metadata command', () => {
7+
it.each([
8+
[['files', 'get', 'wf_a'], 'sim files describe wf_a'],
9+
[['knowledge', 'export', 'kb_a', '-o', '-'], 'sim knowledge get kb_a'],
10+
])('%j points at %s', async (argv, metadataCommand) => {
11+
const result = await runEmbeddedCli(argv, {
12+
...identity,
13+
transport: async () =>
14+
new Response(new Uint8Array([0x89, 0x50]), { headers: { 'content-type': 'image/png' } }),
15+
})
16+
expect(result.exitCode).toBe(1)
17+
expect(result.stderr).toContain('Use --output-file <path>')
18+
expect(result.stderr).toContain(`run \`${metadataCommand}\` for its metadata`)
19+
})
20+
})

‎packages/sim-cli/src/commands/protocol/files-get.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ interface DownloadOutputOptions {
289289
force?: boolean
290290
}
291291

292+
/**
293+
* Where an embedded caller refused binary bytes on stdout should go instead. A model that asked
294+
* for a file's content usually wanted its metadata, so the refusal names the command that has it.
295+
*/
296+
interface DownloadRefusalHint {
297+
metadataCommand?: string
298+
}
299+
292300
type DownloadOperation =
293301
| 'downloadFile'
294302
| 'downloadFileVersion'
@@ -307,7 +315,8 @@ async function downloadToOutput<Operation extends DownloadOperation>(
307315
pathParams: Record<string, string>,
308316
options: DownloadOutputOptions,
309317
scope: 'workspace' | 'owner',
310-
requestInput: Pick<RequestOptions, 'query' | 'body'> = {}
318+
requestInput: Pick<RequestOptions, 'query' | 'body'> = {},
319+
{ metadataCommand }: DownloadRefusalHint = {}
311320
): Promise<void> {
312321
const target = options.outputFile
313322
const writesToStdout = target === undefined || target === '-'
@@ -332,7 +341,7 @@ async function downloadToOutput<Operation extends DownloadOperation>(
332341
await response.body.cancel()
333342
throw new SimApiError(
334343
embedded
335-
? `Refusing to put ${contentType ?? 'unknown content'} in a text result. Use --output-file <path>.`
344+
? `Refusing to put ${contentType ?? 'unknown content'} in a text result. Use --output-file <path>${metadataCommand ? `, or run \`sim ${metadataCommand}\` for its metadata` : ''}.`
336345
: `Refusing to write ${contentType ?? 'unknown content'} to an interactive terminal. Use --output-file <path> or pipe stdout.`,
337346
0
338347
)
@@ -426,7 +435,15 @@ export function attachFileGet(files: Command): void {
426435
.option('-o, --output-file <path>', 'Write content to a file instead of stdout')
427436
.option('--force', 'Overwrite --output-file if it already exists')
428437
.action((fileId: string, options: DownloadOutputOptions) =>
429-
downloadToOutput(connect, 'downloadFile', { fileId }, options, 'workspace')
438+
downloadToOutput(
439+
connect,
440+
'downloadFile',
441+
{ fileId },
442+
options,
443+
'workspace',
444+
{},
445+
{ metadataCommand: `files describe ${fileId}` }
446+
)
430447
)
431448
}
432449

‎packages/sim-cli/src/commands/protocol/knowledge-export.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function attachKnowledgeExport(knowledge: Command): void {
8080
await response.body.cancel()
8181
throw new SimApiError(
8282
embedded
83-
? `Refusing to put ${contentType ?? 'unknown content'} in a text result. Use --output-file <path>.`
83+
? `Refusing to put ${contentType ?? 'unknown content'} in a text result. Use --output-file <path>, or run \`sim knowledge get ${knowledgeBaseId}\` for its metadata.`
8484
: `Refusing to write ${contentType ?? 'unknown content'} to an interactive terminal. Use --output-file <path> or pipe stdout.`,
8585
0
8686
)

0 commit comments

Comments
 (0)