Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions shell/e2e/user-journey.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,82 @@ test.describe('E2E Workspace User Journey', () => {
await expect(previewIframe.locator('body')).toBeVisible({timeout: 10000});
await expect(previewIframe.locator('a2ui-v09-surface').first()).toBeVisible({timeout: 10000});
});

test('should create, overwrite in-place, preview in system instructions, and disable custom instruction presets', async ({
page,
}) => {
await page.addInitScript(() => {
if (window === window.top) {
localStorage.setItem('a2ui_composer_selected_api_key', 'fake');
}
});
await page.goto('/');

const customInstructionsLink = page.locator('.custom-instructions-link');
const systemInstructionsLink = page.locator('.system-instructions-link');

// 1. Open the Gemini chat panel and click "Custom Instructions" next to "Instructions"
await expect(customInstructionsLink).toHaveText('Custom Instructions');
await customInstructionsLink.click();

const dialog = page.locator('a2ui-composer-custom-instructions-dialog');
await expect(dialog).toBeVisible();
const box = await dialog.boundingBox();
expect(box?.width).toBeCloseTo(600, 0);

// 2. Create a named preset, click Save, and verify the link label updates to "Custom Instructions: <Preset Name>"
await dialog.locator('.preset-name-input').fill('Compact Theme');
await dialog
.locator('.instructions-textarea')
.fill('Always use compact spacing and high-contrast cards.');
await dialog.locator('.save-button').click();
await expect(dialog).toBeHidden();

await expect(customInstructionsLink).toHaveText('Custom Instructions: Compact Theme');

// 3. Click "Instructions" and verify "## Custom User Instructions" appears at the end of the system prompt
await systemInstructionsLink.click();
const sysDialog = page.locator('a2ui-composer-system-instructions-dialog');
await expect(sysDialog).toBeVisible();
await expect(sysDialog.locator('.instructions-textarea')).toHaveValue(
/## Custom User Instructions\s+Always use compact spacing and high-contrast cards\./,
);
await sysDialog.getByRole('button', {name: 'Close'}).click();
await expect(sysDialog).toBeHidden();

// 4. Reopen "Custom Instructions", edit the existing preset in-place, click Save, and verify overwrite
await customInstructionsLink.click();
await expect(dialog).toBeVisible();
await dialog.locator('.preset-name-input').fill('Compact Theme v2');
await dialog
.locator('.instructions-textarea')
.fill('Updated: strictly use compact spacing and dark elevation.');
await dialog.locator('.save-button').click();
await expect(dialog).toBeHidden();

await expect(customInstructionsLink).toHaveText('Custom Instructions: Compact Theme v2');

// Verify the overwritten content in SystemInstructionsDialog and after page reload
await page.reload();
await expect(customInstructionsLink).toHaveText('Custom Instructions: Compact Theme v2');
await systemInstructionsLink.click();
await expect(sysDialog.locator('.instructions-textarea')).toHaveValue(
/## Custom User Instructions\s+Updated: strictly use compact spacing and dark elevation\./,
);
await sysDialog.getByRole('button', {name: 'Close'}).click();

// 5. Switch preset dropdown to "None (Off)" and click Save to disable custom instructions
await customInstructionsLink.click();
await dialog.locator('.preset-select').click();
await page.locator('mat-option.preset-option-none').click();
await dialog.locator('.save-button').click();
await expect(dialog).toBeHidden();

await expect(customInstructionsLink).toHaveText('Custom Instructions');
await systemInstructionsLink.click();
await expect(sysDialog.locator('.instructions-textarea')).not.toHaveValue(
/## Custom User Instructions/,
);
await sysDialog.getByRole('button', {name: 'Close'}).click();
});
});
12 changes: 6 additions & 6 deletions shell/src/app/chat/chat-coordinator/chat-coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export class ChatCoordinator {
/** Turn index counter for telemetry. */
readonly currentTurnIndex = signal(0);

/**
* A dynamic, reactive, computed signal property constructing conformed JSON
* catalog schema specifications system instructions.
*/
readonly systemPrompt = this.promptFactory.systemPrompt;

private activePromptId: string | null = null;

constructor() {
Expand Down Expand Up @@ -484,10 +490,4 @@ export class ChatCoordinator {
return updated;
});
}

/**
* A dynamic, reactive, computed signal property constructing conformed JSON
* catalog schema specifications system instructions.
*/
readonly systemPrompt = this.promptFactory.systemPrompt;
}
7 changes: 7 additions & 0 deletions shell/src/app/chat/chat-panel/chat-panel.ng.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@
Instructions
}
</button>
<button type="button" class="custom-instructions-link" (click)="showCustomInstructions()">
@if (hasCustomInstructions()) {
Custom Instructions: {{ activeCustomPreset()?.name }}
} @else {
Custom Instructions
}
</button>
</div>
@if (isLocked()) {
<button
Expand Down
4 changes: 3 additions & 1 deletion shell/src/app/chat/chat-panel/chat-panel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,12 @@
display: flex;
align-items: center;
gap: 12px;
flex-wrap: nowrap;
flex-wrap: wrap;
row-gap: 4px;
overflow: hidden;
}

.custom-instructions-link,
.system-instructions-link {
font-size: 12px;
color: var(--mat-sys-primary);
Expand Down
80 changes: 79 additions & 1 deletion shell/src/app/chat/chat-panel/chat-panel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ import {Catalog} from '../../storage/models/catalog-storage.model';
import {HostCommunication} from '../../shell/host-communication/host-communication';
import {ScreenshotCaptureService} from '../../shell/screenshot/screenshot-capture.service';
import {FailureParseResult} from '../a2ui-payload-parser/a2ui-payload-parser';
import {McpClientManagerService} from '../../mcp/mcp-client-manager.service';
import {ComposerPanelId} from '../../shell/composer-workspace/composer-panel-id';
import {McpClientManagerService} from '../../mcp/mcp-client-manager.service';
import {
ChatPromptFactoryService,
CustomInstructionPreset,
CustomInstructionsState,
} from '../chat-prompt-factory/chat-prompt-factory.service';
import {CustomInstructionsDialogHarness} from '../custom-instructions-dialog/test/custom-instructions-dialog.harness';

class MockChatState {
readonly chatHistory = signal<LlmMessage[]>([]);
Expand Down Expand Up @@ -97,6 +103,22 @@ class MockChatCoordinator {
cancelActiveStream = vi.fn();
}

class MockChatPromptFactoryService {
readonly systemPrompt = signal<string>('Initial system prompt instructions block');
readonly customInstructionsState = signal<CustomInstructionsState>({
presets: [],
activePresetId: null,
});
readonly activePreset = signal<CustomInstructionPreset | null>(null);
readonly hasCustomInstructions = signal<boolean>(false);
setCustomInstructionsState = vi.fn((state: CustomInstructionsState) => {
this.customInstructionsState.set(state);
const preset = state.presets.find(p => p.id === state.activePresetId) ?? null;
this.activePreset.set(preset);
this.hasCustomInstructions.set(!!preset && preset.content.trim().length > 0);
});
}

class MockCatalogManagement {
readonly activeCatalog = signal<Catalog | null>({}); // non-null by default
}
Expand Down Expand Up @@ -126,6 +148,7 @@ describe('ChatPanel Gemini Dialogue Panel Integration', () => {
let configProviderMock: MockAppConfigProvider;
let hostCommunicationMock: MockHostCommunication;
let screenshotServiceMock: ScreenshotCaptureService;
let promptFactoryMock: MockChatPromptFactoryService;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -140,6 +163,7 @@ describe('ChatPanel Gemini Dialogue Panel Integration', () => {
provideNoopAnimations(),
provideRouter([]),
{provide: ChatCoordinator, useClass: MockChatCoordinator},
{provide: ChatPromptFactoryService, useClass: MockChatPromptFactoryService},
{provide: ChatState, useClass: MockChatState},
{provide: CatalogManagement, useClass: MockCatalogManagement},
{provide: StartupResolution, useClass: MockStartupResolution},
Expand All @@ -149,6 +173,9 @@ describe('ChatPanel Gemini Dialogue Panel Integration', () => {
}).compileComponents();

chatServiceMock = TestBed.inject(ChatCoordinator) as unknown as MockChatCoordinator;
promptFactoryMock = TestBed.inject(
ChatPromptFactoryService,
) as unknown as MockChatPromptFactoryService;
chatStateMock = TestBed.inject(ChatState) as unknown as MockChatState;
catalogManagementServiceMock = TestBed.inject(
CatalogManagement,
Expand Down Expand Up @@ -673,6 +700,57 @@ describe('ChatPanel Gemini Dialogue Panel Integration', () => {
expect(await dialog.getTitleText()).toBe('System Instructions');
});

it('renders custom instructions link and updates text based on active custom preset', async () => {
expect(await harness.hasCustomInstructionsLink()).toBe(true);
expect(await harness.getCustomInstructionsLinkText()).toBe('Custom Instructions');

promptFactoryMock.setCustomInstructionsState({
presets: [{id: 'preset-1', name: 'Concise Mode', content: 'Be concise'}],
activePresetId: 'preset-1',
});
fixture.detectChanges();

expect(await harness.getCustomInstructionsLinkText()).toBe('Custom Instructions: Concise Mode');
});

it('opens the custom instructions dialog when the custom instructions link is clicked', async () => {
expect(await harness.hasCustomInstructionsLink()).toBe(true);
const documentRootLoader = TestbedHarnessEnvironment.documentRootLoader(fixture);

await harness.clickCustomInstructionsLink();
fixture.detectChanges();

const dialogs = await documentRootLoader.getAllHarnesses(MatDialogHarness);
expect(dialogs.length).toBe(1);

const dialog = dialogs[0];
expect(await dialog.getTitleText()).toBe('Custom Instructions');
});

it('saves updated custom instructions state to prompt factory when dialog saves', async () => {
const documentRootLoader = TestbedHarnessEnvironment.documentRootLoader(fixture);
await harness.clickCustomInstructionsLink();
fixture.detectChanges();

const customDialog = await documentRootLoader.getHarness(CustomInstructionsDialogHarness);
await customDialog.setPresetName('Dark Theme');
await customDialog.setInstructions('Always generate dark theme.');
await customDialog.clickSave();
fixture.detectChanges();

expect(promptFactoryMock.setCustomInstructionsState).toHaveBeenCalledWith(
expect.objectContaining({
presets: [
expect.objectContaining({
name: 'Dark Theme',
content: 'Always generate dark theme.',
}),
],
activePresetId: expect.any(String),
}),
);
});

it('disables the Send button when the catalog handshake is pending, and enables it when complete', async () => {
// Set activeCatalog to null (handshake pending)
catalogManagementServiceMock.activeCatalog.set(null);
Expand Down
31 changes: 31 additions & 0 deletions shell/src/app/chat/chat-panel/chat-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import {
Component,
computed,
DestroyRef,
Directive,
effect,
ElementRef,
inject,
input,
signal,
} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {FileIngestionService, AttachedFile} from '../file-ingestion/file-ingestion.service';
import {FormsModule} from '@angular/forms';
import {MatButtonModule} from '@angular/material/button';
Expand All @@ -49,6 +51,11 @@ import {ChatState} from '../chat-state/chat-state';
import {LlmMessage, MessageRole} from '../llm-client/llm-client';
import {PipelineStatus} from '../pipeline-status/pipeline-status';
import {SystemInstructionsDialog} from '../system-instructions-dialog/system-instructions-dialog';
import {CustomInstructionsDialog} from '../custom-instructions-dialog/custom-instructions-dialog';
import {
ChatPromptFactoryService,
CustomInstructionsState,
} from '../chat-prompt-factory/chat-prompt-factory.service';
import {McpClientManagerService} from '../../mcp/mcp-client-manager.service';

/**
Expand Down Expand Up @@ -96,6 +103,7 @@ export class AutoScroll {
styleUrl: './chat-panel.scss',
})
export class ChatPanel {
private readonly destroyRef = inject(DestroyRef);
private readonly chatCoordinator = inject(ChatCoordinator);
private readonly chatCleaner = inject(ChatCleaner);
private readonly chatState = inject(ChatState);
Expand All @@ -106,6 +114,7 @@ export class ChatPanel {
private readonly hostCommunication = inject(HostCommunication);
private readonly fileIngestionService = inject(FileIngestionService);
private readonly screenshotCaptureService = inject(ScreenshotCaptureService);
private readonly promptFactory = inject(ChatPromptFactoryService);
protected readonly mcpManager = inject(McpClientManagerService);

protected readonly includeScreenshot = signal<boolean>(false);
Expand All @@ -125,6 +134,8 @@ export class ChatPanel {
* text.
*/
protected readonly systemPrompt = this.chatCoordinator.systemPrompt;
protected readonly hasCustomInstructions = this.promptFactory.hasCustomInstructions;
protected readonly activeCustomPreset = this.promptFactory.activePreset;
protected readonly isHandshakeComplete = computed(
() => this.catalogManagement.activeCatalog() !== null,
);
Expand Down Expand Up @@ -342,6 +353,26 @@ export class ChatPanel {
});
}

/**
* Opens the custom instructions configuration modal dialog.
*/
protected showCustomInstructions(): void {
const dialogRef = this.dialog.open(CustomInstructionsDialog, {
data: this.promptFactory.customInstructionsState(),
width: '600px',
maxWidth: '90vw',
});

dialogRef
.afterClosed()
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((result: CustomInstructionsState | undefined) => {
if (result) {
this.promptFactory.setCustomInstructionsState(result);
}
});
}

/**
* Resets status back to IDLE, dismissing active milestone overlays.
*/
Expand Down
16 changes: 16 additions & 0 deletions shell/src/app/chat/chat-panel/test/chat-panel.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ export class ChatPanelHarness extends ComponentHarness {
return (await link.text()).trim();
}

async clickCustomInstructionsLink(): Promise<void> {
const link = await this.locatorFor('.custom-instructions-link')();
await link.click();
}

async hasCustomInstructionsLink(): Promise<boolean> {
const link = await this.locatorForOptional('.custom-instructions-link')();
return link !== null;
}

async getCustomInstructionsLinkText(): Promise<string | null> {
const link = await this.locatorForOptional('.custom-instructions-link')();
if (!link) return null;
return (await link.text()).trim();
}

async getRetryButtonsCount(): Promise<number> {
const buttons = await this.locatorForAll('.retry-button')();
return buttons.length;
Expand Down
Loading
Loading