Files
vscode/extensions/copilot/test/simulation/workbench/components/toolbar.tsx
T
a97573159d Add Adhoc Request Sender Mode with Tag Highlighting (#323100)
* Agent Host changes for agents/adhoc-request-sender-mode-extension-55e2bb6f

* Remove unconfigured react-hooks/exhaustive-deps eslint directive

The eslint-disable directive referenced a rule that isn't registered in
this repo's ESLint config, which caused ESLint to error with
"Definition for rule 'react-hooks/exhaustive-deps' was not found" and
failed the Compile & Hygiene and Copilot - Test CI checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Coalesce adhoc tag-decoration rescans with requestAnimationFrame

Rescanning the whole editor text on every content change is wasteful for
bursty updates (e.g. a streamed response). Debounce the decoration update
to at most once per animation frame and cancel any pending frame during
cleanup so the callback can't run after the editor is disposed. The
initial scan stays synchronous so tags are highlighted immediately on mount.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address PR feedback: dispose token source; validate adhoc request JSON

- adhocRequestSender: always dispose the per-send CancellationTokenSource
  in the finally block (separate from the current-send guard) so its
  cancellation listeners don't leak across repeated Send/Stop cycles.
- simulationMain: validate and normalize the adhoc request JSON before use
  so malformed input (missing/null/wrong-typed model/user/system) yields a
  focused error message instead of a thrown stack trace.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-26 20:36:56 +05:00

160 lines
5.5 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Select, Text, ToggleButton, Tooltip } from '@fluentui/react-components';
import { WeatherMoon20Regular, WeatherSunny20Regular } from '@fluentui/react-icons';
import * as mobx from 'mobx';
import * as mobxlite from 'mobx-react-lite';
import * as React from 'react';
import { InitArgs } from '../initArgs';
import { AMLProvider } from '../stores/amlSimulations';
import { NesExternalOptions } from '../stores/nesExternalOptions';
import { RunnerOptions } from '../stores/runnerOptions';
import { SimulationRunsProvider } from '../stores/simulationBaseline';
import { SimulationRunner } from '../stores/simulationRunner';
import { SimulationTestsProvider } from '../stores/simulationTestsProvider';
import { TestSource, TestSourceValue } from '../stores/testSource';
import { WorkbenchModeValue } from '../stores/workbenchMode';
import { AMLModeToolbar } from './amlModeToolbar';
import { ThemeKind } from './app';
import { LocalModeToolbar } from './localModeToolbar';
import { NesExternalModeToolbar } from './nesExternalModeToolbar';
import { TestFilterer } from './testFilterer';
type ToolbarProps = {
initArgs: InitArgs | undefined;
runner: SimulationRunner;
runnerOptions: RunnerOptions;
nesExternalOptions: NesExternalOptions;
amlProvider: AMLProvider;
simulationRunsProvider: SimulationRunsProvider;
simulationTestsProvider: SimulationTestsProvider;
workbenchMode: WorkbenchModeValue;
testSource: TestSourceValue;
onFiltererChange: (filter: TestFilterer | undefined) => void;
allLanguageIds: readonly string[];
theme: ThemeKind;
toggleTheme: () => void;
};
export const Toolbar = mobxlite.observer(
({
initArgs,
runner,
runnerOptions,
nesExternalOptions,
amlProvider,
simulationRunsProvider,
simulationTestsProvider,
workbenchMode,
testSource,
onFiltererChange,
allLanguageIds,
theme,
toggleTheme,
}: ToolbarProps) => {
const toolbarContent = (() => {
if (workbenchMode.value === 'adhocRequest') {
return (
<div className='toolbar' style={{ display: 'flex', alignItems: 'center' }}>
<Text size={400}>Adhoc request sender</Text>
</div>
);
}
switch (testSource.value) {
case TestSource.Local:
return (
<LocalModeToolbar
initArgs={initArgs}
runner={runner}
runnerOptions={runnerOptions}
simulationRunsProvider={simulationRunsProvider}
simulationTestsProvider={simulationTestsProvider}
onFiltererChange={onFiltererChange}
/>
);
case TestSource.NesExternal:
return (
<NesExternalModeToolbar
runner={runner}
runnerOptions={runnerOptions}
nesExternalOptions={nesExternalOptions}
simulationRunsProvider={simulationRunsProvider}
onFiltererChange={onFiltererChange}
/>
);
case TestSource.External:
return (
<AMLModeToolbar
amlProvider={amlProvider}
simulationTestsProvider={simulationTestsProvider}
onFiltererChange={onFiltererChange}
allLanguageIds={allLanguageIds}
/>
);
}
})();
return (
<div style={{ padding: '5px', display: 'flex' }}>
{toolbarContent}
<div style={{ display: 'flex', justifyContent: 'end', maxHeight: '35px' }}>
<ThemeToggler theme={theme} toggleTheme={toggleTheme} />
<ModeToggler workbenchMode={workbenchMode} testSource={testSource} onFiltererChange={onFiltererChange} />
</div>
</div>
);
}
);
const ThemeToggler = ({ theme, toggleTheme }: { theme: ThemeKind; toggleTheme: () => void }) => (
<Tooltip content='Toggle workbench theme' relationship='label'>
<ToggleButton
appearance='subtle'
icon={theme === 'dark' ? <WeatherSunny20Regular /> : <WeatherMoon20Regular />}
onClick={toggleTheme}
/>
</Tooltip>
);
const ADHOC_REQUEST_OPTION = 'adhocRequest';
/**
* Options for the workbench mode selector. The test-source entries put the
* workbench into 'tests' mode and select a {@link TestSource}; the adhoc entry
* switches to the standalone "Adhoc request sender" mode.
*/
const modeOptions: { value: string; label: string; source?: TestSource }[] = [
{ value: String(TestSource.Local), label: 'Local', source: TestSource.Local },
{ value: String(TestSource.NesExternal), label: 'NES External', source: TestSource.NesExternal },
{ value: String(TestSource.External), label: 'AML', source: TestSource.External },
{ value: ADHOC_REQUEST_OPTION, label: 'Adhoc Request' },
];
const ModeToggler = ({ workbenchMode, testSource, onFiltererChange }: { workbenchMode: WorkbenchModeValue; testSource: TestSourceValue; onFiltererChange: (filter: TestFilterer | undefined) => void }) => (
<Select
style={{ marginLeft: '8px', minWidth: '140px' }}
value={workbenchMode.value === 'adhocRequest' ? ADHOC_REQUEST_OPTION : String(testSource.value)}
onChange={mobx.action((_e, data) => {
const selected = modeOptions.find(o => o.value === data.value);
if (!selected) {
return;
}
if (selected.source === undefined) {
workbenchMode.value = 'adhocRequest';
} else {
workbenchMode.value = 'tests';
testSource.value = selected.source;
}
onFiltererChange(undefined);
})}
>
{modeOptions.map(o => (
<option key={o.value} value={o.value}>{o.label}</option>
))}
</Select>
);