Add account-backed artifact sharing (#13012)
This commit is contained in:
@@ -154,6 +154,7 @@ export function supportsBrowserPageFlag(commandPath: string[]): boolean {
|
||||
if (
|
||||
[
|
||||
'account',
|
||||
'artifacts',
|
||||
'automations',
|
||||
'project',
|
||||
'repo',
|
||||
@@ -201,6 +202,7 @@ export function isCommandGroup(commandPath: string[]): boolean {
|
||||
(commandPath.length === 1 &&
|
||||
[
|
||||
'account',
|
||||
'artifacts',
|
||||
'automations',
|
||||
'project',
|
||||
'repo',
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { ArtifactListItem, ArtifactListPage } from '../shared/artifacts'
|
||||
|
||||
export function formatArtifactList(artifacts: readonly ArtifactListItem[]): string {
|
||||
if (artifacts.length === 0) {
|
||||
return 'No shared artifacts.'
|
||||
}
|
||||
return artifacts
|
||||
.map(({ artifact, shareUrl }) => {
|
||||
const name = artifact.title || artifact.originalFileName || artifact.slug
|
||||
return `${name}\n id: ${artifact.slug}\n updated: ${artifact.updatedAt}\n url: ${shareUrl}`
|
||||
})
|
||||
.join('\n\n')
|
||||
}
|
||||
|
||||
export function formatArtifactListPage(page: ArtifactListPage): string {
|
||||
const rows = formatArtifactList(page.artifacts)
|
||||
return page.nextCursor ? `${rows}\nMore artifacts: --cursor ${page.nextCursor}` : rows
|
||||
}
|
||||
|
||||
export function formatArtifactShared(item: ArtifactListItem): string {
|
||||
return item.shareUrl
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -22,6 +22,17 @@ export const HANDLER_GROUPS: readonly HandlerGroup[] = [
|
||||
keys: ['account add', 'account list'],
|
||||
load: async () => (await import('./handlers/account.js')).ACCOUNT_HANDLERS
|
||||
},
|
||||
{
|
||||
name: 'artifacts',
|
||||
keys: [
|
||||
'artifacts list',
|
||||
'artifacts share',
|
||||
'artifacts update',
|
||||
'artifacts unshare',
|
||||
'artifacts delete'
|
||||
],
|
||||
load: async () => (await import('./handlers/artifacts.js')).ARTIFACT_HANDLERS
|
||||
},
|
||||
{
|
||||
name: 'automations',
|
||||
keys: [
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import { mkdtemp, open, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { ArtifactListItem } from '../../shared/artifacts'
|
||||
import { ARTIFACT_HANDLERS } from './artifacts'
|
||||
import { ARTIFACT_CLI_MAX_RPC_BYTES } from '../../shared/artifacts'
|
||||
|
||||
const item: ArtifactListItem = {
|
||||
artifact: {
|
||||
version: 1,
|
||||
slug: 'artifact-1',
|
||||
title: null,
|
||||
originalFileName: 'report.html',
|
||||
sourceContentType: 'text/html',
|
||||
renderedContentType: 'text/html',
|
||||
createdAt: '2026-08-06T00:00:00.000Z',
|
||||
updatedAt: '2026-08-06T00:00:00.000Z',
|
||||
expiresAt: '2026-09-06T00:00:00.000Z',
|
||||
byteSize: 12,
|
||||
deletedAt: null
|
||||
},
|
||||
shareUrl: 'https://share.onorca.dev/a/artifact-1'
|
||||
}
|
||||
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
describe('artifact CLI handlers', () => {
|
||||
it('reads a relative HTML file and sends sanitized content to the runtime', async () => {
|
||||
const cwd = await mkdtemp(join(tmpdir(), 'orca-artifact-cli-'))
|
||||
await writeFile(join(cwd, 'report.html'), '<h1>Hi</h1>', 'utf8')
|
||||
const call = vi.fn().mockResolvedValue({
|
||||
id: 'request-1',
|
||||
ok: true,
|
||||
result: { status: 'ok', value: item },
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})
|
||||
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
|
||||
|
||||
await ARTIFACT_HANDLERS['artifacts share']!({
|
||||
client: { call } as never,
|
||||
cwd,
|
||||
flags: new Map([['file', 'report.html']]),
|
||||
json: false
|
||||
})
|
||||
|
||||
expect(call).toHaveBeenCalledWith(
|
||||
'artifacts.share',
|
||||
expect.objectContaining({
|
||||
sourceKey: join(cwd, 'report.html'),
|
||||
content: '<h1>Hi</h1>',
|
||||
contentType: 'text/html',
|
||||
fileName: 'report.html'
|
||||
})
|
||||
)
|
||||
expect(log).toHaveBeenCalledWith(item.shareUrl)
|
||||
})
|
||||
|
||||
it('rejects unsupported file extensions before calling the runtime', async () => {
|
||||
const cwd = await mkdtemp(join(tmpdir(), 'orca-artifact-cli-'))
|
||||
await writeFile(join(cwd, 'report.txt'), 'hello', 'utf8')
|
||||
const call = vi.fn()
|
||||
|
||||
await expect(
|
||||
ARTIFACT_HANDLERS['artifacts share']!({
|
||||
client: { call } as never,
|
||||
cwd,
|
||||
flags: new Map([['file', 'report.txt']]),
|
||||
json: false
|
||||
})
|
||||
).rejects.toThrow(/HTML or Markdown/)
|
||||
expect(call).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects a sparse oversized file before attempting the RPC', async () => {
|
||||
const cwd = await mkdtemp(join(tmpdir(), 'orca-artifact-cli-'))
|
||||
const handle = await open(join(cwd, 'oversized.html'), 'w')
|
||||
await handle.truncate(ARTIFACT_CLI_MAX_RPC_BYTES + 1)
|
||||
await handle.close()
|
||||
const call = vi.fn()
|
||||
|
||||
await expect(
|
||||
ARTIFACT_HANDLERS['artifacts share']!({
|
||||
client: { call } as never,
|
||||
cwd,
|
||||
flags: new Map([['file', 'oversized.html']]),
|
||||
json: false
|
||||
})
|
||||
).rejects.toThrow(/too large/)
|
||||
expect(call).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('passes an opaque list cursor through and prints the next cursor', async () => {
|
||||
const call = vi.fn().mockResolvedValue({
|
||||
id: 'request-1',
|
||||
ok: true,
|
||||
result: {
|
||||
status: 'ok',
|
||||
value: { artifacts: [item], nextCursor: 'next opaque page' }
|
||||
},
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})
|
||||
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined)
|
||||
|
||||
await ARTIFACT_HANDLERS['artifacts list']!({
|
||||
client: { call } as never,
|
||||
cwd: '/repo',
|
||||
flags: new Map([['cursor', 'current opaque page']]),
|
||||
json: false
|
||||
})
|
||||
|
||||
expect(call).toHaveBeenCalledWith('artifacts.list', { cursor: 'current opaque page' })
|
||||
expect(log).toHaveBeenCalledWith(
|
||||
expect.stringContaining('More artifacts: --cursor next opaque page')
|
||||
)
|
||||
})
|
||||
|
||||
it.each(['environment', 'pairing-code'])(
|
||||
'rejects explicit remote selector --%s',
|
||||
async (flag) => {
|
||||
const call = vi.fn()
|
||||
|
||||
await expect(
|
||||
ARTIFACT_HANDLERS['artifacts list']!({
|
||||
client: { call } as never,
|
||||
cwd: '/repo',
|
||||
flags: new Map([[flag, 'remote-host']]),
|
||||
json: false
|
||||
})
|
||||
).rejects.toThrow(/does not retarget artifact commands/)
|
||||
expect(call).not.toHaveBeenCalled()
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,187 @@
|
||||
import { basename, extname, resolve } from 'node:path'
|
||||
import type {
|
||||
ArtifactCloudOperation,
|
||||
ArtifactCloudOptions,
|
||||
ArtifactListPage,
|
||||
ArtifactListItem,
|
||||
ArtifactWriteRequest
|
||||
} from '../../shared/artifacts'
|
||||
import { ARTIFACT_CLI_MAX_RPC_BYTES } from '../../shared/artifacts'
|
||||
import {
|
||||
parseRemoteArtifactInput,
|
||||
REMOTE_ARTIFACT_INPUT_ENV
|
||||
} from '../../shared/artifact-cli-bridge'
|
||||
import { readArtifactFileWithinLimit } from '../../shared/artifact-file-read'
|
||||
import type { CommandHandler, HandlerContext } from '../dispatch'
|
||||
import { RuntimeClientError } from '../runtime-client'
|
||||
import { formatArtifactListPage, formatArtifactShared } from '../artifact-format'
|
||||
import { printResult } from '../format'
|
||||
|
||||
function stringFlag(ctx: HandlerContext, name: string): string | undefined {
|
||||
const value = ctx.flags.get(name)
|
||||
return typeof value === 'string' && value.trim() ? value.trim() : undefined
|
||||
}
|
||||
|
||||
function requireStringFlag(ctx: HandlerContext, name: string): string {
|
||||
const value = stringFlag(ctx, name)
|
||||
if (!value) {
|
||||
throw new RuntimeClientError('invalid_argument', `Missing required ${name}.`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function cloudOptions(ctx: HandlerContext): ArtifactCloudOptions {
|
||||
const apiUrl = stringFlag(ctx, 'api-url') ?? process.env.ORCA_ARTIFACTS_API_URL?.trim()
|
||||
const authToken = process.env.ORCA_CLOUD_AUTH_TOKEN?.trim()
|
||||
return {
|
||||
...(apiUrl ? { apiUrl } : {}),
|
||||
...(authToken ? { authToken } : {})
|
||||
}
|
||||
}
|
||||
|
||||
function rejectRemoteSelectionFlags(ctx: HandlerContext): void {
|
||||
for (const flag of ['environment', 'pairing-code']) {
|
||||
if (ctx.flags.has(flag)) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
`\`--${flag}\` does not retarget artifact commands; artifacts use the signed-in desktop account.`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function artifactContentType(path: string): ArtifactWriteRequest['contentType'] | null {
|
||||
const extension = extname(path).toLowerCase()
|
||||
return ['.html', '.htm'].includes(extension)
|
||||
? 'text/html'
|
||||
: ['.md', '.markdown'].includes(extension)
|
||||
? 'text/markdown'
|
||||
: null
|
||||
}
|
||||
|
||||
async function readStdinWithinLimit(maxBytes: number): Promise<string> {
|
||||
const chunks: Buffer[] = []
|
||||
let bytes = 0
|
||||
for await (const chunk of process.stdin) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
|
||||
bytes += buffer.length
|
||||
if (bytes > maxBytes) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Artifact is too large for the Orca CLI transport. Use the browser upload page instead.'
|
||||
)
|
||||
}
|
||||
chunks.push(buffer)
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8')
|
||||
}
|
||||
|
||||
async function readArtifactRequest(ctx: HandlerContext): Promise<ArtifactWriteRequest> {
|
||||
const remoteInput = parseRemoteArtifactInput(process.env[REMOTE_ARTIFACT_INPUT_ENV])
|
||||
const sourceKey = remoteInput?.sourceKey ?? resolve(ctx.cwd, requireStringFlag(ctx, 'file'))
|
||||
const contentType = remoteInput?.contentType ?? artifactContentType(sourceKey)
|
||||
if (!contentType) {
|
||||
throw new RuntimeClientError('invalid_argument', 'Artifacts must be HTML or Markdown files.')
|
||||
}
|
||||
const localRead = remoteInput
|
||||
? null
|
||||
: await readArtifactFileWithinLimit(sourceKey, ARTIFACT_CLI_MAX_RPC_BYTES)
|
||||
if (localRead?.status === 'not-file') {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Artifact file was not found or is not a file.'
|
||||
)
|
||||
}
|
||||
if (localRead?.status === 'too-large') {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Artifact is too large for the Orca CLI transport. Use the browser upload page instead.'
|
||||
)
|
||||
}
|
||||
const content = remoteInput
|
||||
? await readStdinWithinLimit(ARTIFACT_CLI_MAX_RPC_BYTES)
|
||||
: localRead?.status === 'ok'
|
||||
? localRead.content
|
||||
: ''
|
||||
if (!content) {
|
||||
throw new RuntimeClientError('invalid_argument', 'Artifact file is empty.')
|
||||
}
|
||||
const request: ArtifactWriteRequest = {
|
||||
sourceKey,
|
||||
content,
|
||||
contentType,
|
||||
fileName: remoteInput?.fileName ?? basename(sourceKey),
|
||||
...cloudOptions(ctx)
|
||||
}
|
||||
if (Buffer.byteLength(JSON.stringify(request), 'utf8') > ARTIFACT_CLI_MAX_RPC_BYTES) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Artifact is too large for the Orca CLI transport. Use the browser upload page instead.'
|
||||
)
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
function requireOperation<T>(operation: ArtifactCloudOperation<T>): T {
|
||||
if (operation.status === 'ok') {
|
||||
return operation.value
|
||||
}
|
||||
if (operation.status === 'reconnect-required') {
|
||||
throw new RuntimeClientError('authentication_required', 'Sign in to Orca and try again.')
|
||||
}
|
||||
throw new RuntimeClientError('authentication_unconfigured', operation.message)
|
||||
}
|
||||
|
||||
export const ARTIFACT_HANDLERS: Record<string, CommandHandler> = {
|
||||
'artifacts list': async (ctx) => {
|
||||
rejectRemoteSelectionFlags(ctx)
|
||||
const cursor = stringFlag(ctx, 'cursor')
|
||||
const response = await ctx.client.call<ArtifactCloudOperation<ArtifactListPage>>(
|
||||
'artifacts.list',
|
||||
{
|
||||
...cloudOptions(ctx),
|
||||
...(cursor ? { cursor } : {})
|
||||
}
|
||||
)
|
||||
const value = requireOperation(response.result)
|
||||
printResult({ ...response, result: value }, ctx.json, formatArtifactListPage)
|
||||
},
|
||||
'artifacts share': async (ctx) => {
|
||||
rejectRemoteSelectionFlags(ctx)
|
||||
const response = await ctx.client.call<ArtifactCloudOperation<ArtifactListItem>>(
|
||||
'artifacts.share',
|
||||
await readArtifactRequest(ctx)
|
||||
)
|
||||
const value = requireOperation(response.result)
|
||||
printResult({ ...response, result: value }, ctx.json, formatArtifactShared)
|
||||
},
|
||||
'artifacts update': async (ctx) => {
|
||||
rejectRemoteSelectionFlags(ctx)
|
||||
const response = await ctx.client.call<ArtifactCloudOperation<ArtifactListItem>>(
|
||||
'artifacts.update',
|
||||
await readArtifactRequest(ctx)
|
||||
)
|
||||
const value = requireOperation(response.result)
|
||||
printResult({ ...response, result: value }, ctx.json, formatArtifactShared)
|
||||
},
|
||||
'artifacts unshare': async (ctx) => {
|
||||
rejectRemoteSelectionFlags(ctx)
|
||||
const remoteInput = parseRemoteArtifactInput(process.env[REMOTE_ARTIFACT_INPUT_ENV])
|
||||
const sourceKey = remoteInput?.sourceKey ?? resolve(ctx.cwd, requireStringFlag(ctx, 'file'))
|
||||
const response = await ctx.client.call<ArtifactCloudOperation<void>>('artifacts.unshare', {
|
||||
sourceKey,
|
||||
...cloudOptions(ctx)
|
||||
})
|
||||
requireOperation(response.result)
|
||||
printResult({ ...response, result: { deleted: true } }, ctx.json, () => 'Artifact deleted.')
|
||||
},
|
||||
'artifacts delete': async (ctx) => {
|
||||
rejectRemoteSelectionFlags(ctx)
|
||||
const response = await ctx.client.call<ArtifactCloudOperation<void>>('artifacts.delete', {
|
||||
id: requireStringFlag(ctx, 'id'),
|
||||
...cloudOptions(ctx)
|
||||
})
|
||||
requireOperation(response.result)
|
||||
printResult({ ...response, result: { deleted: true } }, ctx.json, () => 'Artifact deleted.')
|
||||
}
|
||||
}
|
||||
@@ -448,6 +448,9 @@ function formatCommandFlagHelp(flag: string, commandPath: string[]): string {
|
||||
if (command === 'linear list-issues' && flag === 'cursor') {
|
||||
return '--cursor <cursor> Opaque cursor returned by a previous list-issues page'
|
||||
}
|
||||
if (command === 'artifacts list' && flag === 'cursor') {
|
||||
return '--cursor <cursor> Opaque cursor returned by a previous artifacts page'
|
||||
}
|
||||
if (command === 'orchestration worker-read' && flag === 'cursor') {
|
||||
return '--cursor <cursor> Opaque cursor returned by a previous worker-read page'
|
||||
}
|
||||
|
||||
+23
-1
@@ -41,7 +41,7 @@ vi.mock('./runtime-client', async () => {
|
||||
remotePairingCode?: string | null,
|
||||
environmentSelector?: string | null
|
||||
) {
|
||||
runtimeClientConstructorMock()
|
||||
runtimeClientConstructorMock(remotePairingCode, environmentSelector)
|
||||
const effectivePairingCode =
|
||||
remotePairingCode === undefined
|
||||
? (process.env.ORCA_PAIRING_CODE ?? process.env.ORCA_REMOTE_PAIRING)
|
||||
@@ -212,6 +212,28 @@ describe('command aliases dispatch to the canonical handler', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('artifact runtime routing', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
vi.restoreAllMocks()
|
||||
process.exitCode = 0
|
||||
})
|
||||
|
||||
it('uses the desktop runtime despite remote-selection environment fallbacks', async () => {
|
||||
vi.stubEnv('ORCA_ENVIRONMENT', 'remote-environment')
|
||||
vi.stubEnv('ORCA_PAIRING_CODE', 'remote-pairing-code')
|
||||
vi.spyOn(console, 'log').mockImplementation(() => undefined)
|
||||
callMock.mockResolvedValue(okFixture('artifact-list', { status: 'ok', value: [] }))
|
||||
runtimeClientConstructorMock.mockClear()
|
||||
|
||||
await main(['artifacts', 'list', '--json'], '/folder-workspace')
|
||||
|
||||
expect(process.exitCode).not.toBe(1)
|
||||
expect(runtimeClientConstructorMock).toHaveBeenCalledWith(null, null)
|
||||
expect(callMock).toHaveBeenCalledWith('artifacts.list', {})
|
||||
})
|
||||
})
|
||||
|
||||
describe('unknown command surfaces a suggestion', () => {
|
||||
let errorSpy: ReturnType<typeof vi.spyOn>
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ const COMMAND_PATHS = COMMAND_SPECS.flatMap((spec) => specPaths(spec))
|
||||
function shouldIgnoreRemoteSelection(commandPath: string[]): boolean {
|
||||
return (
|
||||
commandPath[0] === 'account' ||
|
||||
commandPath[0] === 'artifacts' ||
|
||||
commandPath[0] === 'environment' ||
|
||||
commandPath[0] === 'serve' ||
|
||||
commandPath[0] === 'agent' ||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { CommandSpec } from '../args'
|
||||
import { GLOBAL_FLAGS } from '../args'
|
||||
|
||||
const CLOUD_FLAGS = ['api-url']
|
||||
|
||||
export const ARTIFACT_COMMAND_SPECS: CommandSpec[] = [
|
||||
{
|
||||
path: ['artifacts', 'share'],
|
||||
summary: 'Share an HTML or Markdown file with your Orca account',
|
||||
usage: 'orca artifacts share <file> [--api-url <url>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, ...CLOUD_FLAGS, 'file'],
|
||||
positionalArgs: ['file'],
|
||||
examples: ['orca artifacts share ./report.html', 'orca artifacts share ./notes.md --json']
|
||||
},
|
||||
{
|
||||
path: ['artifacts', 'update'],
|
||||
summary: 'Update a file previously shared from this Orca profile',
|
||||
usage: 'orca artifacts update <file> [--api-url <url>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, ...CLOUD_FLAGS, 'file'],
|
||||
positionalArgs: ['file']
|
||||
},
|
||||
{
|
||||
path: ['artifacts', 'unshare'],
|
||||
destructive: true,
|
||||
summary: 'Delete the artifact associated with a previously shared file',
|
||||
usage: 'orca artifacts unshare <file> [--api-url <url>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, ...CLOUD_FLAGS, 'file'],
|
||||
positionalArgs: ['file']
|
||||
},
|
||||
{
|
||||
path: ['artifacts', 'list'],
|
||||
summary: 'List artifacts owned by the signed-in Orca account',
|
||||
usage: 'orca artifacts list [--cursor <cursor>] [--api-url <url>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, ...CLOUD_FLAGS, 'cursor']
|
||||
},
|
||||
{
|
||||
path: ['artifacts', 'delete'],
|
||||
aliases: [['artifacts', 'rm']],
|
||||
destructive: true,
|
||||
summary: 'Delete an artifact owned by the signed-in Orca account',
|
||||
usage: 'orca artifacts delete <id> [--api-url <url>] [--json]',
|
||||
allowedFlags: [...GLOBAL_FLAGS, ...CLOUD_FLAGS, 'id'],
|
||||
positionalArgs: ['id']
|
||||
}
|
||||
]
|
||||
@@ -16,9 +16,11 @@ import { INTROSPECTION_COMMAND_SPECS } from './introspection'
|
||||
import { LINEAR_COMMAND_SPECS } from './linear'
|
||||
import { VM_COMMAND_SPECS } from './vm'
|
||||
import { SKILL_COMMAND_SPECS } from './skills'
|
||||
import { ARTIFACT_COMMAND_SPECS } from './artifacts'
|
||||
|
||||
export const COMMAND_SPECS: CommandSpec[] = [
|
||||
...CORE_COMMAND_SPECS,
|
||||
...ARTIFACT_COMMAND_SPECS,
|
||||
...ACCOUNT_COMMAND_SPECS,
|
||||
...PROJECT_COMMAND_SPECS,
|
||||
...FILE_COMMAND_SPECS,
|
||||
|
||||
Reference in New Issue
Block a user