import type {
PlaygroundPlatformRegistration,
PlaygroundSessionField,
PlaygroundSessionSetup,
} from '@midscene/playground';
import { Alert, Form, Input, InputNumber, Radio, Select } from 'antd';
import type { FormInstance } from 'antd';
import type { PlaygroundFormValues } from './controller/types';
import DropdownChevron from './icons/dropdown-chevron.svg';
import MidsceneLogo from './icons/midscene-logo.svg';
import type { PlaygroundSessionViewState } from './session-state';
import './SessionSetupPanel.less';
function getPlatformSelectorFieldKey(
setup: PlaygroundSessionSetup | null,
): string | undefined {
return setup?.platformSelector?.fieldKey;
}
export function getPlatformSelectorOptions(
field: PlaygroundSessionField,
setup: PlaygroundSessionSetup | null,
): PlaygroundSessionField['options'] {
if (
getPlatformSelectorFieldKey(setup) !== field.key ||
!setup?.platformRegistry?.length
) {
return field.options;
}
const registryOptions = setup.platformRegistry.map(
(platform: PlaygroundPlatformRegistration) => ({
label: platform.label,
value: platform.id,
description:
[platform.description, platform.unavailableReason]
.filter(Boolean)
.join(' ยท ') || undefined,
}),
);
return registryOptions.length > 0 ? registryOptions : field.options;
}
function DropdownSuffix() {
return (
);
}
function renderSessionField(
field: PlaygroundSessionField,
sessionSetup: PlaygroundSessionSetup | null,
) {
if (field.type === 'number') {
return (
);
}
if (field.type === 'select') {
const platformSelectorFieldKey = getPlatformSelectorFieldKey(sessionSetup);
const platformOptions = getPlatformSelectorOptions(field, sessionSetup);
const shouldRenderPlatformSelector =
platformSelectorFieldKey === field.key &&
sessionSetup?.platformSelector?.variant === 'cards';
if (shouldRenderPlatformSelector) {
return (
{(platformOptions || []).map((option) => (
{option.label}
{option.description ? (
{option.description}
) : null}
))}
);
}
return (
}
options={(platformOptions ?? field.options ?? []).map((option) => ({
label: option.label,
value: option.value,
description: option.description,
}))}
optionRender={(option) => {
const description = option.data.description as string | undefined;
if (!description) {
return option.data.label;
}
return (
{option.data.label}
{description}
);
}}
/>
);
}
return ;
}
export interface SessionSetupPanelProps {
form: FormInstance;
sessionSetup: PlaygroundSessionSetup | null;
sessionSetupError: string | null;
sessionViewState: PlaygroundSessionViewState;
sessionLoading: boolean;
sessionMutating: boolean;
onCreateSession: () => void | Promise;
}
const DEFAULT_TITLE = 'Create Agent';
const DEFAULT_DESCRIPTION = 'Create a platform session before running actions.';
export function SessionSetupPanel({
form,
sessionSetup,
sessionSetupError,
sessionViewState,
sessionLoading,
sessionMutating,
onCreateSession,
}: SessionSetupPanelProps) {
const submitDisabled =
sessionMutating ||
sessionLoading ||
sessionViewState.setupState === 'blocked';
const primaryLabel = sessionSetup?.primaryActionLabel ?? DEFAULT_TITLE;
const title = sessionSetup?.title ?? DEFAULT_TITLE;
const description = sessionSetup?.description ?? DEFAULT_DESCRIPTION;
return (
{title}
{description}
{sessionViewState.setupState === 'blocked' &&
sessionViewState.setupBlockingReason && (
)}
{sessionSetupError ? (
) : null}
{sessionSetup?.notice ? (
) : null}
{renderSessionField(field, sessionSetup)}
))}
);
}