-
Notifications
You must be signed in to change notification settings - Fork 12
CS-10677: Add boxel run-command CLI command
#4421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FadhlanR
wants to merge
3
commits into
main
Choose a base branch
from
cs-10677-add-boxel-run-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+378
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import type { Command } from 'commander'; | ||
| import { getProfileManager, type ProfileManager } from '../lib/profile-manager'; | ||
| import { FG_GREEN, FG_RED, FG_CYAN, DIM, RESET } from '../lib/colors'; | ||
|
|
||
| export interface RunCommandResult { | ||
| status: 'ready' | 'error' | 'unusable'; | ||
| result?: string | null; | ||
| error?: string | null; | ||
| } | ||
|
|
||
| export interface RunCommandOptions { | ||
| input?: Record<string, unknown>; | ||
| json?: boolean; | ||
| profileManager?: ProfileManager; | ||
| } | ||
|
|
||
| interface RunCommandCliOptions { | ||
| realm: string; | ||
| input?: string; | ||
| json?: boolean; | ||
| } | ||
|
|
||
| export async function runCommand( | ||
| commandSpecifier: string, | ||
| realmUrl: string, | ||
| options?: RunCommandOptions, | ||
| ): Promise<RunCommandResult> { | ||
| let pm = options?.profileManager ?? getProfileManager(); | ||
| let active = pm.getActiveProfile(); | ||
| if (!active) { | ||
| throw new Error( | ||
| 'No active profile. Run `boxel profile add` to create one.', | ||
| ); | ||
| } | ||
|
|
||
| let realmServerUrl = active.profile.realmServerUrl.replace(/\/$/, ''); | ||
| let url = `${realmServerUrl}/_run-command`; | ||
|
|
||
| let body = { | ||
| data: { | ||
| type: 'run-command', | ||
| attributes: { | ||
| realmURL: realmUrl, | ||
| command: commandSpecifier, | ||
| commandInput: options?.input ?? null, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| let response: Response; | ||
| try { | ||
| response = await pm.authedRealmServerFetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/vnd.api+json', | ||
| Accept: 'application/vnd.api+json', | ||
| }, | ||
| body: JSON.stringify(body), | ||
| }); | ||
| } catch (err) { | ||
| return { | ||
| status: 'error', | ||
| error: `run-command fetch failed: ${err instanceof Error ? err.message : String(err)}`, | ||
| }; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| let text = await response.text().catch(() => '(no body)'); | ||
| return { | ||
| status: 'error', | ||
| error: `run-command HTTP ${response.status}: ${text}`, | ||
| }; | ||
| } | ||
|
FadhlanR marked this conversation as resolved.
|
||
|
|
||
| let json: { | ||
| data?: { | ||
| attributes?: { | ||
| status?: string; | ||
| cardResultString?: string | null; | ||
| error?: string | null; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| try { | ||
| json = await response.json(); | ||
| } catch { | ||
| return { | ||
| status: 'error', | ||
| error: `run-command response was not valid JSON (HTTP ${response.status})`, | ||
| }; | ||
| } | ||
|
|
||
| let attrs = json.data?.attributes; | ||
| return { | ||
| status: (attrs?.status as RunCommandResult['status']) ?? 'error', | ||
| result: attrs?.cardResultString ?? null, | ||
| error: attrs?.error ?? null, | ||
| }; | ||
| } | ||
|
|
||
| export function registerRunCommand(program: Command): void { | ||
| program | ||
| .command('run-command') | ||
| .description( | ||
| 'Execute a host command on the realm server via the prerenderer', | ||
| ) | ||
| .argument( | ||
| '<command-specifier>', | ||
| 'Command module path (e.g. @cardstack/boxel-host/commands/get-card-type-schema/default)', | ||
| ) | ||
| .requiredOption( | ||
| '--realm <realm-url>', | ||
| 'The realm URL context for the command', | ||
| ) | ||
| .option('--input <json>', 'JSON string of command input') | ||
| .option('--json', 'Output raw JSON response') | ||
| .action(async (commandSpecifier: string, opts: RunCommandCliOptions) => { | ||
| let input: Record<string, unknown> | undefined; | ||
| if (opts.input) { | ||
| try { | ||
| let parsed = JSON.parse(opts.input); | ||
| if ( | ||
| typeof parsed !== 'object' || | ||
| parsed === null || | ||
| Array.isArray(parsed) | ||
| ) { | ||
| console.error( | ||
| `${FG_RED}Error:${RESET} --input must be a JSON object, got ${Array.isArray(parsed) ? 'array' : typeof parsed}`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| input = parsed; | ||
| } catch { | ||
|
FadhlanR marked this conversation as resolved.
|
||
| console.error( | ||
| `${FG_RED}Error:${RESET} --input is not valid JSON: ${opts.input}`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
FadhlanR marked this conversation as resolved.
|
||
| } | ||
|
|
||
| let result: RunCommandResult; | ||
| try { | ||
| result = await runCommand(commandSpecifier, opts.realm, { input }); | ||
| } catch (err) { | ||
| console.error( | ||
| `${FG_RED}Error:${RESET} ${err instanceof Error ? err.message : String(err)}`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| if (opts.json) { | ||
| console.log(JSON.stringify(result, null, 2)); | ||
| } else { | ||
| console.log( | ||
| `${DIM}Status:${RESET} ${statusColor(result.status)}${result.status}${RESET}`, | ||
| ); | ||
| if (result.result) { | ||
| console.log(`${DIM}Result:${RESET}`); | ||
| try { | ||
| console.log(JSON.stringify(JSON.parse(result.result), null, 2)); | ||
| } catch { | ||
| console.log(result.result); | ||
| } | ||
| } | ||
| if (result.error) { | ||
| console.error(`${FG_RED}Error:${RESET} ${result.error}`); | ||
| } | ||
| } | ||
|
|
||
| if (result.status === 'error' || result.status === 'unusable') { | ||
| process.exit(1); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function statusColor(status: string): string { | ||
| switch (status) { | ||
| case 'ready': | ||
| return FG_GREEN; | ||
| case 'error': | ||
| return FG_RED; | ||
| default: | ||
| return FG_CYAN; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
packages/boxel-cli/tests/integration/run-command.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import '../helpers/setup-realm-server'; | ||
| import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; | ||
| import * as fs from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
| import { runCommand } from '../../src/commands/run-command'; | ||
| import { createRealm } from '../../src/commands/realm/create'; | ||
| import { ProfileManager } from '../../src/lib/profile-manager'; | ||
| import { | ||
| startTestRealmServer, | ||
| stopTestRealmServer, | ||
| createTestProfileDir, | ||
| setupTestProfile, | ||
| uniqueRealmName, | ||
| } from '../helpers/integration'; | ||
|
|
||
| let profileManager: ProfileManager; | ||
| let cleanupProfile: () => void; | ||
| let realmUrl: string; | ||
|
|
||
| async function createTestRealm(): Promise<string> { | ||
| let name = uniqueRealmName(); | ||
| await createRealm(name, `Test ${name}`, { profileManager }); | ||
|
|
||
| let realmTokens = | ||
| profileManager.getActiveProfile()!.profile.realmTokens ?? {}; | ||
| let entry = Object.entries(realmTokens).find(([url]) => url.includes(name)); | ||
| if (!entry) { | ||
| throw new Error(`No realm JWT stored for ${name}`); | ||
| } | ||
| return entry[0]; | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| await startTestRealmServer(); | ||
|
|
||
| let testProfile = createTestProfileDir(); | ||
| profileManager = testProfile.profileManager; | ||
| cleanupProfile = testProfile.cleanup; | ||
| await setupTestProfile(profileManager); | ||
|
|
||
| realmUrl = await createTestRealm(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| cleanupProfile?.(); | ||
| await stopTestRealmServer(); | ||
| }); | ||
|
|
||
| describe('run-command (integration)', () => { | ||
| it('executes a command and returns a ready result', async () => { | ||
| let result = await runCommand( | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| realmUrl, | ||
| { profileManager }, | ||
| ); | ||
|
|
||
| expect(result.status).toBe('ready'); | ||
| }); | ||
|
|
||
| it('sends correct JSON:API request shape', async () => { | ||
| let fetchSpy = vi.spyOn(profileManager, 'authedRealmServerFetch'); | ||
| try { | ||
| await runCommand( | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| realmUrl, | ||
| { | ||
| input: { cardURL: `${realmUrl}MyCard` }, | ||
| profileManager, | ||
| }, | ||
| ); | ||
|
|
||
| expect(fetchSpy).toHaveBeenCalledOnce(); | ||
| let [url, init] = fetchSpy.mock.calls[0]; | ||
| expect(url).toContain('/_run-command'); | ||
| expect(init!.method).toBe('POST'); | ||
| let headers = init!.headers as Record<string, string>; | ||
| expect(headers['Content-Type']).toBe('application/vnd.api+json'); | ||
| expect(headers['Accept']).toBe('application/vnd.api+json'); | ||
|
|
||
| let body = JSON.parse(init!.body as string); | ||
| expect(body).toEqual({ | ||
| data: { | ||
| type: 'run-command', | ||
| attributes: { | ||
| realmURL: realmUrl, | ||
| command: | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| commandInput: { cardURL: `${realmUrl}MyCard` }, | ||
| }, | ||
| }, | ||
| }); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('sends null commandInput when no input provided', async () => { | ||
| let fetchSpy = vi.spyOn(profileManager, 'authedRealmServerFetch'); | ||
| try { | ||
| await runCommand( | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| realmUrl, | ||
| { profileManager }, | ||
| ); | ||
|
|
||
| let body = JSON.parse(fetchSpy.mock.calls[0][1]!.body as string); | ||
| expect(body.data.attributes.commandInput).toBeNull(); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('throws when no active profile', async () => { | ||
| let emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'boxel-empty-')); | ||
| let emptyManager = new ProfileManager(emptyDir); | ||
|
|
||
| await expect( | ||
| runCommand( | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| realmUrl, | ||
| { profileManager: emptyManager }, | ||
| ), | ||
| ).rejects.toThrow('No active profile'); | ||
|
|
||
| fs.rmSync(emptyDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('returns error status on non-2xx HTTP response', async () => { | ||
| let fetchSpy = vi | ||
| .spyOn(profileManager, 'authedRealmServerFetch') | ||
| .mockResolvedValueOnce(new Response('Not Found', { status: 404 })); | ||
| try { | ||
| let result = await runCommand('some/command', realmUrl, { | ||
| profileManager, | ||
| }); | ||
| expect(result.status).toBe('error'); | ||
| expect(result.error).toContain('404'); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('returns error status when response body is not valid JSON', async () => { | ||
| let fetchSpy = vi | ||
| .spyOn(profileManager, 'authedRealmServerFetch') | ||
| .mockResolvedValueOnce(new Response('not json', { status: 200 })); | ||
| try { | ||
| let result = await runCommand('some/command', realmUrl, { | ||
| profileManager, | ||
| }); | ||
| expect(result.status).toBe('error'); | ||
| expect(result.error).toContain('not valid JSON'); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('returns error status when fetch throws', async () => { | ||
| let fetchSpy = vi | ||
| .spyOn(profileManager, 'authedRealmServerFetch') | ||
| .mockRejectedValueOnce(new Error('network failure')); | ||
| try { | ||
| let result = await runCommand('some/command', realmUrl, { | ||
| profileManager, | ||
| }); | ||
| expect(result.status).toBe('error'); | ||
| expect(result.error).toContain('network failure'); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('strips trailing slash from realm server URL before appending endpoint', async () => { | ||
| let fetchSpy = vi.spyOn(profileManager, 'authedRealmServerFetch'); | ||
| try { | ||
| await runCommand( | ||
| '@cardstack/boxel-host/commands/get-card-type-schema/default', | ||
| realmUrl, | ||
| { profileManager }, | ||
| ); | ||
|
|
||
| let [url] = fetchSpy.mock.calls[0]; | ||
| expect(url).toContain('/_run-command'); | ||
| expect(url).not.toContain('//_run-command'); | ||
| } finally { | ||
| fetchSpy.mockRestore(); | ||
| } | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this know to use the server token and not a realm specific token? and is there a test that can prove that by making sure these two tokens are actually different in the real realm test. (i find that the LLM gets confused here and tries to make these 2 the same token all the time)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that the LLM itself determines which tokens should be used? If so, it is not needed, because we store the realm server token and realm-specific tokens in the profile.json file, since they are generated after the LLM executes the boxel-profile command. Additionally, there are two fetch functions in the Boxel CLI, authedRealmServerFetch and authedRealmFetch, which are used by the commands to retrieve data using one of those two tokens, depending on the command.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no. i mean the LLM that you are using to help you code this feature up. (assuming claude code). its really bad at telling the difference between server tokens and realm tokens. for me it's introduced dozens of bugs around this that have wasted many hours for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah i see--since you are using
authedRealmServerFetchthat is how we know to chose the correct token 👍