Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions apps/cli/commands/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ export async function runCommand( options: {
const config = await readCliConfig();
let showCapabilitiesOnConnect = ! config.aiProvider;

// Studio Code Desktop defaults to WordPress.com provider.
if ( isJsonMode && showCapabilitiesOnConnect ) {
await switchProvider( 'wpcom', false );
showCapabilitiesOnConnect = false;
}

if ( showCapabilitiesOnConnect ) {
ui.showOnboarding();

Expand Down
103 changes: 103 additions & 0 deletions apps/cli/commands/ai/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { readAuthToken } from '@studio/common/lib/shared-config';
import { vi, type Mock } from 'vitest';
import { resolveInitialAiProvider, saveSelectedAiProvider } from 'cli/ai/auth';
import { JsonAdapter } from 'cli/ai/output-adapter';
import { runStudioAgentTurn } from 'cli/ai/runtimes/pi';
import { createStudioSession } from 'cli/ai/sessions/pi-session';
import { readCliConfig } from 'cli/lib/cli-config/core';
import { runCommand } from '../index';

vi.mock( '@studio/common/lib/shared-config', () => ( {
readAuthToken: vi.fn(),
} ) );
vi.mock( 'cli/ai/auth', () => ( {
resolveInitialAiProvider: vi.fn(),
saveSelectedAiProvider: vi.fn(),
getAvailableAiProviders: vi.fn( () => [ 'wpcom' ] ),
isAiProviderReady: vi.fn(),
prepareAiProvider: vi.fn(),
resolveAiEnvironment: vi.fn(),
resolveUnavailableAiProvider: vi.fn(),
} ) );
vi.mock( 'cli/ai/providers', () => ( {
AI_PROVIDERS: { wpcom: 'WordPress.com', 'anthropic-api-key': 'Anthropic · API key' },
getAiProviderDefinition: () => ( {
supportsModel: () => true,
defaultModel: 'claude-default',
} ),
} ) );
vi.mock( 'cli/lib/cli-config/core', () => ( {
readCliConfig: vi.fn(),
} ) );
vi.mock( 'cli/lib/cli-config/sites', () => ( {
findSiteByFolder: vi.fn(),
} ) );
vi.mock( 'cli/ai/sessions/context', () => ( {
resolveResumeSessionContext: () => ( { provider: undefined, model: undefined } ),
} ) );
vi.mock( 'cli/ai/sessions/pi-session', () => ( {
createStudioSession: vi.fn(),
openStudioSession: vi.fn(),
listStudioSessionFiles: vi.fn(),
} ) );
vi.mock( 'cli/ai/sessions/replay', () => ( { replaySessionHistory: vi.fn() } ) );
vi.mock( 'cli/ai/sessions/paths', () => ( { getAiSessionsRootDirectory: vi.fn() } ) );
vi.mock( 'cli/ai/runtimes/pi', () => ( {
// A completed turn so the JSON single-turn path returns instead of looping.
runStudioAgentTurn: vi.fn( () => ( { result: Promise.resolve(), interrupt: vi.fn() } ) ),
} ) );
vi.mock( 'cli/ai/slash-commands', () => ( { getActiveSlashCommands: vi.fn( () => [] ) } ) );
vi.mock( 'cli/ai/browser-utils', () => ( { closeSharedBrowser: vi.fn() } ) );
vi.mock( 'cli/ai/chat-artifacts', () => ( { setChatArtifactCallback: vi.fn() } ) );
vi.mock( 'cli/ai/site-selection', () => ( { setLocalSiteSelectedCallback: vi.fn() } ) );
vi.mock( 'cli/ai/daemon-status-poll', () => ( {
startDaemonStatusPolling: vi.fn( () => vi.fn() ),
} ) );
vi.mock( 'cli/commands/auth/login', () => ( { runCommand: vi.fn() } ) );
vi.mock( 'cli/ai/ui', () => ( { AiChatUI: class AiChatUI {} } ) );
vi.mock( 'cli/logger', () => ( {
Logger: class {},
LoggerError: class LoggerError extends Error {},
setProgressCallback: vi.fn(),
} ) );

describe( 'AI runCommand — Desktop (JSON mode) provider default', () => {
let stdoutSpy: ReturnType< typeof vi.spyOn >;

beforeEach( () => {
vi.clearAllMocks();
( createStudioSession as Mock ).mockResolvedValue( {
appendCustomEntry: vi.fn( () => 'entry-id' ),
getSessionId: () => 'session-id',
getEntries: () => [],
} );
( readAuthToken as Mock ).mockResolvedValue( null );
// Silence the NDJSON the adapter writes to stdout.
stdoutSpy = vi.spyOn( process.stdout, 'write' ).mockImplementation( () => true );
} );

afterEach( () => {
stdoutSpy.mockRestore();
} );

it( 'defaults to the wpcom provider on first run when none is configured', async () => {
( readCliConfig as Mock ).mockResolvedValue( { aiProvider: undefined } );
( resolveInitialAiProvider as Mock ).mockResolvedValue( 'wpcom' );

await runCommand( { adapter: new JsonAdapter(), initialMessage: 'hello' } );

expect( runStudioAgentTurn ).toHaveBeenCalled();
expect( saveSelectedAiProvider ).toHaveBeenCalledTimes( 1 );
expect( saveSelectedAiProvider ).toHaveBeenCalledWith( 'wpcom' );
} );

it( 'does not override an already-configured provider', async () => {
( readCliConfig as Mock ).mockResolvedValue( { aiProvider: 'anthropic-api-key' } );
( resolveInitialAiProvider as Mock ).mockResolvedValue( 'anthropic-api-key' );

await runCommand( { adapter: new JsonAdapter(), initialMessage: 'hello' } );

expect( runStudioAgentTurn ).toHaveBeenCalled();
expect( saveSelectedAiProvider ).not.toHaveBeenCalled();
} );
} );