|
| 1 | +import { |
| 2 | + Button, |
| 3 | + CaretUpIcon, |
| 4 | + KubernetesIcon, |
| 5 | + ListBoxItem, |
| 6 | + RocketIcon, |
| 7 | + SearchIcon, |
| 8 | + Select, |
| 9 | + Toast, |
| 10 | + Tooltip, |
| 11 | +} from '@pluralsh/design-system' |
| 12 | +import { useChatbot } from 'components/ai/AIContext' |
| 13 | +import { TRUNCATE } from 'components/utils/truncate.ts' |
| 14 | +import { |
| 15 | + AgentSessionType, |
| 16 | + useUpdateChatThreadMutation, |
| 17 | +} from 'generated/graphql' |
| 18 | +import { capitalize } from 'lodash' |
| 19 | +import { useCallback, useState } from 'react' |
| 20 | + |
| 21 | +export function AgentSessionTypeSelect() { |
| 22 | + const { currentThread } = useChatbot() |
| 23 | + const [open, setOpen] = useState(false) |
| 24 | + |
| 25 | + const [mutation, { loading: loading, error }] = useUpdateChatThreadMutation() |
| 26 | + const updateSessionType = useCallback( |
| 27 | + (type: AgentSessionType | null) => { |
| 28 | + if (!currentThread?.id || type === currentThread?.session?.type) return |
| 29 | + mutation({ |
| 30 | + variables: { |
| 31 | + id: currentThread?.id, |
| 32 | + attributes: { session: { type }, summary: currentThread?.summary }, |
| 33 | + }, |
| 34 | + }) |
| 35 | + }, |
| 36 | + [currentThread, mutation] |
| 37 | + ) |
| 38 | + |
| 39 | + const curType = currentThread?.session?.type |
| 40 | + |
| 41 | + if ( |
| 42 | + !curType || |
| 43 | + curType === AgentSessionType.Kubernetes || |
| 44 | + curType === AgentSessionType.Terraform |
| 45 | + ) |
| 46 | + return null |
| 47 | + |
| 48 | + return ( |
| 49 | + <> |
| 50 | + <Select |
| 51 | + isOpen={open} |
| 52 | + onOpenChange={setOpen} |
| 53 | + selectedKey={curType ?? ''} |
| 54 | + onSelectionChange={(key) => |
| 55 | + updateSessionType(key ? (key as AgentSessionType) : null) |
| 56 | + } |
| 57 | + width={260} |
| 58 | + triggerButton={ |
| 59 | + <Button |
| 60 | + small |
| 61 | + loading={loading} |
| 62 | + secondary |
| 63 | + startIcon={options.find((o) => o.type === curType)?.icon} |
| 64 | + endIcon={ |
| 65 | + <CaretUpIcon |
| 66 | + style={{ |
| 67 | + pointerEvents: 'none', |
| 68 | + transition: 'transform 0.2s ease-in-out', |
| 69 | + transform: open ? 'scaleY(1)' : 'scaleY(-1)', |
| 70 | + }} |
| 71 | + /> |
| 72 | + } |
| 73 | + > |
| 74 | + <Tooltip |
| 75 | + css={{ maxWidth: 500 }} |
| 76 | + placement="top" |
| 77 | + label="Change the type of session you are having with Plural Copilot" |
| 78 | + > |
| 79 | + <span css={{ ...TRUNCATE }}> |
| 80 | + {capitalize(curType ?? 'Change session type')} |
| 81 | + </span> |
| 82 | + </Tooltip> |
| 83 | + </Button> |
| 84 | + } |
| 85 | + > |
| 86 | + {options.map(({ type, description, icon }) => ( |
| 87 | + <ListBoxItem |
| 88 | + key={type} |
| 89 | + label={capitalize(type)} |
| 90 | + description={description} |
| 91 | + leftContent={icon} |
| 92 | + /> |
| 93 | + ))} |
| 94 | + </Select> |
| 95 | + <Toast |
| 96 | + show={!!error} |
| 97 | + closeTimeout={5000} |
| 98 | + severity="danger" |
| 99 | + position="bottom" |
| 100 | + marginBottom="medium" |
| 101 | + > |
| 102 | + <strong>Error updating session type:</strong> {error?.message} |
| 103 | + </Toast> |
| 104 | + </> |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +const options = [ |
| 109 | + { |
| 110 | + type: AgentSessionType.Manifests, |
| 111 | + description: 'Author K8s yaml', |
| 112 | + icon: <KubernetesIcon />, |
| 113 | + }, |
| 114 | + { |
| 115 | + type: AgentSessionType.Provisioning, |
| 116 | + description: 'Provision new infra', |
| 117 | + icon: <RocketIcon />, |
| 118 | + }, |
| 119 | + { |
| 120 | + type: AgentSessionType.Search, |
| 121 | + description: 'Query your existing infra', |
| 122 | + icon: <SearchIcon />, |
| 123 | + }, |
| 124 | +] |
0 commit comments