form.svelte 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <script lang="ts">
  2. import { Card } from '$lib/components/common/card';
  3. import { Icon } from '$lib/components/common/icon';
  4. import { Button } from '$lib/components/controls/button';
  5. import * as Field from '$lib/components/controls/field';
  6. import { Input } from '$lib/components/controls/input';
  7. import { Spinner } from '$lib/components/controls/spinner';
  8. import { Textarea } from '$lib/components/controls/textarea';
  9. import { ArrayInput } from '$lib/components/forms/array-input';
  10. import { DeploymentForm } from '$lib/components/forms/deployment';
  11. import { IconPicker } from '$lib/components/forms/icon-picker';
  12. import { Optional } from '$lib/components/forms/optional';
  13. import { KBSelectDialog } from '$lib/components/kbs/select';
  14. import { ModelSelectDialog } from '$lib/components/models/select';
  15. import { ToolSelectDialog } from '$lib/components/tools/select';
  16. import type { KnowledgeBase, Model, Tool } from '$lib/types/entities';
  17. import { type AgentFormValue, DEFAULT_AGENT_FORM_VALUE } from './types';
  18. let {
  19. workspaceId,
  20. disabled = false,
  21. submitting = false,
  22. value,
  23. cancelHref,
  24. onsubmit,
  25. }: {
  26. workspaceId: string;
  27. disabled?: boolean;
  28. submitting?: boolean;
  29. value?: AgentFormValue;
  30. cancelHref: string;
  31. onsubmit: (value: AgentFormValue) => void;
  32. } = $props();
  33. const isEdit = $derived(value !== undefined);
  34. let form = $state<AgentFormValue>({ ...DEFAULT_AGENT_FORM_VALUE });
  35. let errors = $state<{
  36. name?: string;
  37. }>({});
  38. let modelSelectionShown = $state(false);
  39. let kbSelectionShown = $state(false);
  40. let toolSelectionShown = $state(false);
  41. const initForm = (value?: AgentFormValue) => {
  42. if (!value) {
  43. form = { ...DEFAULT_AGENT_FORM_VALUE };
  44. } else {
  45. form = {
  46. ...$state.snapshot(value),
  47. };
  48. }
  49. };
  50. $effect(() => initForm(value));
  51. const onModelSelect = (model: Model) => {
  52. form.model = { id: model.id, type: model.mode, icon: model.icon, name: model.name };
  53. modelSelectionShown = false;
  54. };
  55. const onKbSelect = (kbs: KnowledgeBase[]): KnowledgeBase[] => {
  56. form.kbs = kbs.map((kb) => ({ id: kb.id, type: kb.mode, icon: kb.icon, name: kb.name }));
  57. return kbs;
  58. };
  59. const onToolSelect = (tools: Tool[]): Tool[] => {
  60. form.tools = tools.map((tool) => ({
  61. id: tool.id,
  62. type: tool.mode,
  63. icon: tool.icon,
  64. name: tool.name,
  65. }));
  66. return tools;
  67. };
  68. const handleSubmit = async () => {
  69. onsubmit(form);
  70. };
  71. </script>
  72. {#snippet panel()}
  73. <div class="flex items-center gap-2">
  74. <Button href={cancelHref} size="sm" variant="outline">Cancel</Button>
  75. <Button disabled={submitting} size="sm" onclick={handleSubmit}>
  76. {#if submitting}
  77. <Spinner />
  78. {/if}
  79. {isEdit ? 'Save Changes' : 'Create'}
  80. </Button>
  81. </div>
  82. {/snippet}
  83. <div class="mx-auto max-w-2xl p-6">
  84. <!-- Page header -->
  85. <div class="mb-4 flex items-center justify-between gap-4">
  86. <div>
  87. <h1 class="text-xl font-semibold text-white/90">
  88. {isEdit ? 'Edit Agent' : 'New Agent'}
  89. </h1>
  90. </div>
  91. {@render panel()}
  92. </div>
  93. <!-- Basic Information panel (shared) -->
  94. <Card class="mb-4" title="Basic Information">
  95. <div class="mb-4 flex gap-8">
  96. <!-- Icon -->
  97. <div class="shrink-0">
  98. <IconPicker bind:value={form.icon} />
  99. </div>
  100. <!-- Name -->
  101. <div class="min-w-0 flex-1 space-y-4">
  102. <Field.Set>
  103. <Field.Field>
  104. <Field.Label for="name">Name</Field.Label>
  105. <Input id="name" maxlength={64} bind:value={form.name} />
  106. <Field.Error>{errors.name}</Field.Error>
  107. </Field.Field>
  108. <Field.Field>
  109. <Field.Label for="description">Description</Field.Label>
  110. <Textarea
  111. id="description"
  112. placeholder="What does this tool do?"
  113. rows={3}
  114. bind:value={form.description}
  115. />
  116. <Field.Description>
  117. Provide a brief description that can also help AI assistant to understand the agent's
  118. functionality.
  119. </Field.Description>
  120. </Field.Field>
  121. </Field.Set>
  122. </div>
  123. </div>
  124. <Optional title="Tags">
  125. <Field.Field>
  126. <ArrayInput {disabled} bind:value={form.tags} />
  127. <Field.Description>
  128. Tags can be used for filtering and searching in the list view.
  129. </Field.Description>
  130. </Field.Field>
  131. </Optional>
  132. </Card>
  133. <Card class="mb-4" title="Configuration">
  134. <Field.Set>
  135. <Field.Field>
  136. <Field.Label for="systemPrompt">System Prompt</Field.Label>
  137. <Textarea
  138. id="systemPrompt"
  139. placeholder="You are a helpful assistant..."
  140. rows={5}
  141. bind:value={form.systemPrompt}
  142. />
  143. </Field.Field>
  144. <Field.Field>
  145. <Field.Label>Language Model</Field.Label>
  146. <Field.Content>
  147. <div class="flex flex-wrap items-center gap-2">
  148. <Button size="sm" variant="outline" onclick={() => (modelSelectionShown = true)}>
  149. {#if form.model}
  150. <Icon icon={form.model.icon} size={14} />
  151. <div>{form.model.name}</div>
  152. {:else}
  153. Select model...
  154. {/if}
  155. </Button>
  156. </div>
  157. </Field.Content>
  158. </Field.Field>
  159. <Field.Field>
  160. <Field.Label>Tools</Field.Label>
  161. <Field.Content>
  162. <div class="flex flex-wrap items-center gap-2">
  163. {#each form.tools as tool (tool.id)}
  164. <Button size="sm" variant="outline" onclick={() => (toolSelectionShown = true)}>
  165. <Icon icon={tool.icon} size={14} />
  166. <div>{tool.name}</div>
  167. </Button>
  168. {:else}
  169. <Button size="sm" variant="outline" onclick={() => (toolSelectionShown = true)}>
  170. Select tools...
  171. </Button>
  172. {/each}
  173. </div>
  174. </Field.Content>
  175. </Field.Field>
  176. <Field.Field>
  177. <Field.Label>Knowledge Bases</Field.Label>
  178. <Field.Content>
  179. <div class="flex flex-wrap items-center gap-2">
  180. {#each form.kbs as kb (kb.id)}
  181. <Button size="sm" variant="outline" onclick={() => (kbSelectionShown = true)}>
  182. <Icon icon={kb.icon} size={14} />
  183. <div>{kb.name}</div>
  184. </Button>
  185. {:else}
  186. <Button size="sm" variant="outline" onclick={() => (kbSelectionShown = true)}>
  187. Select knowledge bases...
  188. </Button>
  189. {/each}
  190. </div>
  191. </Field.Content>
  192. </Field.Field>
  193. </Field.Set>
  194. </Card>
  195. <Card class="mb-4" title="Deployment">
  196. <Optional title="Runtime Settings">
  197. <DeploymentForm {disabled} bind:form />
  198. </Optional>
  199. </Card>
  200. <!-- Footer actions -->
  201. <div class="flex justify-end">
  202. {@render panel()}
  203. </div>
  204. </div>
  205. <ModelSelectDialog
  206. category="language"
  207. {workspaceId}
  208. bind:open={modelSelectionShown}
  209. onselect={(model) => onModelSelect(model)}
  210. />
  211. <KBSelectDialog {workspaceId} bind:open={kbSelectionShown} onselect={(kbs) => onKbSelect(kbs)} />
  212. <ToolSelectDialog
  213. {workspaceId}
  214. bind:open={toolSelectionShown}
  215. onselect={(tools) => onToolSelect(tools)}
  216. />