Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | 2x 27x 2x 2x 2x 9x 2x 1x 1x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 9x 9x 9x 9x 9x 9x 9x 9x 18x 15x 1x 27x | import SubPage from "@/components/page/SubPage/SubPage";
import { AvailableSkill } from "@/objects/Skill";
import { AvailableTool } from "@/objects/Tool";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import {
AlertCircle,
Check,
CheckSquare,
GitBranch,
Info,
Loader2,
Sparkles,
Square,
Wrench,
Zap,
} from "lucide-react";
import React, { useState } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { useAuth } from "../../../../../hooks/useAuth";
import { useWorkerPresets } from "@/hooks/useWorkerPresets";
import { useNodeOverrideOptions } from "@/hooks/useNodeOverrideOptions";
import GenerateSkillModal from "./GenerateSkillModal";
import GraphBuilder, { GraphDefinition } from "./GraphBuilder";
import {
SectionCard,
} from "@/components/detail";
interface FormData {
name: string;
description: string;
}
const FieldLabel: React.FC<{
label: string;
description?: string;
required?: boolean;
}> = ({ label, description, required }) => (
<div className="mb-1.5">
<div className="text-sm font-medium text-gray-700 dark:text-gray-300">
{label}
{required && <span className="ml-0.5 text-red-500">*</span>}
</div>
{description && (
<p className="text-xs text-gray-400 dark:text-gray-500 mt-0.5">
{description}
</p>
)}
</div>
);
const inputBase =
"w-full px-3 py-2 text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500 transition-colors";
const errorBase =
"w-full px-3 py-2 text-sm rounded-lg border border-red-300 dark:border-red-700 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-red-200 dark:focus:ring-red-900 transition-colors";
const CharCount: React.FC<{ value: string; max: number }> = ({
value,
max,
}) => (
<div
className={`text-right text-xs mt-1 ${
value.length > max ? "text-red-500" : "text-gray-400 dark:text-gray-500"
}`}
>
{value.length} / {max}
</div>
);
const SkillTemplateToggle: React.FC<{
skill: AvailableSkill;
selected: boolean;
onToggle: () => void;
}> = ({ skill, selected, onToggle }) => (
<button
type="button"
onClick={onToggle}
aria-pressed={selected}
className={`w-full flex items-start gap-3 px-4 py-3 rounded-lg border text-left transition-colors ${
selected
? "border-violet-400 dark:border-violet-500 bg-violet-50 dark:bg-violet-900/20"
: "border-gray-200 dark:border-gray-600 hover:border-gray-300 dark:hover:border-gray-500 bg-white dark:bg-gray-800"
}`}
>
<div className="mt-0.5 shrink-0">
{selected ? (
<CheckSquare className="size-4 text-violet-600 dark:text-violet-400" />
) : (
<Square className="size-4 text-gray-400 dark:text-gray-500" />
)}
</div>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span className="text-sm font-medium text-gray-800 dark:text-gray-200 font-mono">
{skill.display_name}
</span>
{skill.tags.slice(0, 2).map((tag) => (
<span
key={tag}
className="text-[10px] px-1.5 py-0.5 rounded-full bg-gray-100 dark:bg-gray-700 text-gray-500 dark:text-gray-400"
>
{tag}
</span>
))}
</div>
<div className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
{skill.description}
</div>
<div className="flex items-center gap-1 mt-1.5">
<GitBranch className="size-3 text-gray-300 dark:text-gray-600" />
<span className="text-xs text-gray-400 dark:text-gray-500">
{skill.nodes.length} node{skill.nodes.length !== 1 ? "s" : ""} ·{" "}
{skill.edges.length} edge
{skill.edges.length !== 1 ? "s" : ""}
</span>
</div>
</div>
</button>
);
type Mode = "template" | "custom";
const NewSkill: React.FC = () => {
const { user } = useAuth();
const { getApiHandler } = useApiHandler();
const navigate = useNavigate();
// `?template=` preselects a pattern — where an old link to a template's
// own page now lands, since templates are only a way to start a skill.
const [searchParams] = useSearchParams();
const [mode, setMode] = useState<Mode>("template");
const [data, setData] = useState<FormData>({ name: "", description: "" });
const [selectedTemplate, setSelectedTemplate] = useState<string | null>(
searchParams.get("template"),
);
const [customDefinition, setCustomDefinition] =
useState<GraphDefinition | null>(null);
const [errors, setErrors] = useState<Partial<Record<keyof FormData, string>>>(
{},
);
const [submitError, setSubmitError] = useState<string | null>(null);
const [submitting, setSubmitting] = useState(false);
const [showBuilder, setShowBuilder] = useState(false);
// Describe the skill and let the AI adapt the chosen pattern to it.
const [generating, setGenerating] = useState(false);
const { data: availableSkills = [], isLoading: skillsLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.AVAILABLE_SKILLS],
queryFn: () => getApiHandler().skill_router.available(),
});
const { data: agentDefinitions = [], isLoading: defsLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.AGENT_DEFINITIONS],
queryFn: () =>
getApiHandler().skill_router.listAgentDefinitions({ is_builtin: true }),
enabled: mode === "custom",
});
const {
definitions: userWorkerDefinitions,
save: savePreset,
remove: removePreset,
} = useWorkerPresets(mode === "custom");
const { models, mcpServers } = useNodeOverrideOptions(mode === "custom");
const { data: availableTools = [] } = useQuery<AvailableTool[]>({
queryKey: [QUERY_KEYS.GET.AVAILABLE_TOOLS],
queryFn: () => getApiHandler().tool_router.available(),
enabled: mode === "custom",
});
const set = <K extends keyof FormData>(key: K, value: string) => {
setData((prev) => ({ ...prev, [key]: value }));
setErrors((prev) => ({ ...prev, [key]: undefined }));
setSubmitError(null);
};
const handleSubmit = async () => {
if (!data.name.trim()) {
setErrors({ name: "Name is required." });
return;
}
if (data.name.trim().length > 50) {
setErrors({ name: "Name must be 50 characters or fewer." });
return;
}
let graphDefinition: Record<string, unknown>;
if (mode === "template") {
if (!selectedTemplate) {
setSubmitError("Please select a pattern.");
return;
}
const template = availableSkills.find((s) => s.name === selectedTemplate);
if (!template) {
setSubmitError("Selected pattern not found.");
return;
}
graphDefinition = template.graph_definition;
} else {
if (!customDefinition) {
setSubmitError("Please build a skill graph first.");
return;
}
graphDefinition = customDefinition as unknown as Record<string, unknown>;
}
setSubmitting(true);
setSubmitError(null);
try {
const api = getApiHandler();
const templateDescription =
mode === "template"
? (availableSkills.find((s) => s.name === selectedTemplate)
?.description ?? "")
: "";
await api.skill_router.create({
name: data.name.trim(),
description: data.description.trim() || templateDescription,
graph_definition: graphDefinition,
tags: mode === "template" ? ["template"] : ["custom"],
});
void navigate(APP_ROUTES.USER.INFERENCE.skills);
} catch (e) {
const errorMessage =
e instanceof Error
? e.message
: "Something went wrong. Please try again.";
setSubmitError(errorMessage);
} finally {
setSubmitting(false);
}
};
// Full-screen graph builder overlay
Iif (showBuilder) {
return (
<div className="fixed inset-0 z-50 flex flex-col bg-gray-50 dark:bg-gray-900">
<GraphBuilder
builtinDefinitions={agentDefinitions}
userDefinitions={userWorkerDefinitions}
availableTools={availableTools}
initialDefinition={customDefinition ?? undefined}
models={models}
mcpServers={mcpServers}
onSavePreset={savePreset}
onDeletePreset={removePreset}
onDone={(def) => {
setCustomDefinition(def);
setShowBuilder(false);
}}
onCancel={() => setShowBuilder(false)}
/>
</div>
);
}
return (
<>
{generating && (
<GenerateSkillModal
initialTemplate={selectedTemplate}
onClose={() => setGenerating(false)}
/>
)}
<SubPage title="New Skill">
<div className="max-w-2xl space-y-4">
{/* Identity */}
<SectionCard variant="form" icon={Zap} title="Identity">
<div>
<FieldLabel
label="Name"
description="A short, memorable name for this skill."
required
/>
<input
type="text"
value={data.name}
onChange={(e) => set("name", e.target.value)}
placeholder="e.g. My Debugging Skill"
className={errors.name ? errorBase : inputBase}
/>
<CharCount value={data.name} max={50} />
{errors.name && (
<p className="text-xs text-red-500 mt-1">{errors.name}</p>
)}
</div>
<div>
<FieldLabel
label="Description"
description="Optional description."
/>
<textarea
value={data.description}
onChange={(e) => set("description", e.target.value)}
placeholder="Describe what this skill does and when to use it."
rows={3}
className={inputBase + " resize-none"}
/>
</div>
</SectionCard>
{/* Mode tabs */}
<SectionCard variant="form" icon={GitBranch} title="Skill Graph">
<div className="flex rounded-lg border border-gray-200 dark:border-gray-600 overflow-hidden">
{(["template", "custom"] as const).map((m) => (
<button
key={m}
type="button"
onClick={() => setMode(m)}
className={`flex-1 py-2 text-sm font-medium transition-colors ${
mode === m
? "bg-gray-900 dark:bg-white text-white dark:text-gray-900"
: "bg-white dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700"
}`}
>
{m === "template" ? "From a Pattern" : "Custom Graph"}
</button>
))}
</div>
{/* Template mode */}
{mode === "template" && (
<>
<div className="flex items-start justify-between gap-3">
<FieldLabel
label="Start from a pattern"
description="Each pattern is a graph technique worth reusing. It is copied into your skill, which you then edit to fit."
required
/>
<button
type="button"
onClick={() => setGenerating(true)}
className="flex items-center gap-1 shrink-0 text-xs font-medium text-violet-600 dark:text-violet-400 hover:underline"
>
<Sparkles className="size-3.5" />
{selectedTemplate
? "Generate from this pattern"
: "Describe it instead"}
</button>
</div>
{skillsLoading ? (
<div className="space-y-2">
{[1, 2, 3].map((i) => (
<div
key={i}
className="h-20 rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-100 dark:bg-gray-700 animate-pulse"
/>
))}
</div>
) : availableSkills.length === 0 ? (
<p className="text-sm text-gray-400 dark:text-gray-500 italic">
No patterns are available from the AI service.
</p>
) : (
<div className="space-y-2">
{availableSkills.map((skill) => (
<SkillTemplateToggle
key={skill.name}
skill={skill}
selected={selectedTemplate === skill.name}
onToggle={() =>
setSelectedTemplate((prev) =>
prev === skill.name ? null : skill.name,
)
}
/>
))}
</div>
)}
<div className="rounded-lg bg-violet-50 dark:bg-violet-950/30 border border-violet-200 dark:border-violet-800 px-4 py-3 space-y-1.5">
<div className="flex items-center gap-1.5 text-xs font-medium text-violet-700 dark:text-violet-300">
<Info className="size-3.5" />
About patterns
</div>
<ul className="space-y-1">
{[
"A pattern is a starting point: the graph is copied into your skill and is yours to change.",
"Each declares its inputs and outputs, so your skill has a Run form from the start.",
"For a ready-made skill to use as it is, grant a built-in skillset to your agent instead.",
].map((tip) => (
<li
key={tip}
className="flex items-start gap-1.5 text-xs text-violet-600 dark:text-violet-400"
>
<span className="mt-1.5 size-1 rounded-full bg-violet-400 dark:bg-violet-500 shrink-0" />
{tip}
</li>
))}
</ul>
</div>
</>
)}
{/* Custom graph mode */}
{mode === "custom" && (
<>
{customDefinition ? (
<div className="rounded-lg border border-emerald-200 dark:border-emerald-800 bg-emerald-50 dark:bg-emerald-950/30 px-4 py-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Check className="size-4 text-emerald-600 dark:text-emerald-400" />
<span className="text-sm font-medium text-emerald-700 dark:text-emerald-300">
Graph defined
</span>
</div>
<button
type="button"
onClick={() => setShowBuilder(true)}
className="text-xs text-emerald-600 dark:text-emerald-400 hover:underline"
>
Edit graph
</button>
</div>
<div className="mt-1.5 flex gap-3 text-xs text-emerald-600 dark:text-emerald-400">
<span className="flex items-center gap-1">
<GitBranch className="size-3" />
{Object.keys(customDefinition.nodes).length} nodes
</span>
<span className="flex items-center gap-1">
<Wrench className="size-3" />
{customDefinition.edges.length} edges
</span>
</div>
</div>
) : (
<div className="text-center py-8">
{defsLoading ? (
<div className="flex items-center justify-center gap-2 text-sm text-gray-400">
<Loader2 className="size-4 animate-spin" />
Loading agent definitions…
</div>
) : (
<>
<GitBranch className="size-8 text-gray-300 dark:text-gray-600 mx-auto mb-2" />
<p className="text-sm text-gray-500 dark:text-gray-400 mb-3">
Build a custom multi-agent graph from scratch.
</p>
<button
type="button"
onClick={() => setShowBuilder(true)}
className="px-4 py-2 rounded-lg text-sm font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 transition-opacity"
>
Open Graph Builder
</button>
</>
)}
</div>
)}
<div className="rounded-lg bg-violet-50 dark:bg-violet-950/30 border border-violet-200 dark:border-violet-800 px-4 py-3 space-y-1.5">
<div className="flex items-center gap-1.5 text-xs font-medium text-violet-700 dark:text-violet-300">
<Info className="size-3.5" />
Building custom graphs
</div>
<ul className="space-y-1">
{[
"Drag worker agents from the left panel onto the canvas.",
"Connect nodes by dragging from a source handle to a target.",
"Mark exactly one node as the start and at least one as the end.",
"Configure per-node overrides: temperature, thinking, effort level.",
].map((tip) => (
<li
key={tip}
className="flex items-start gap-1.5 text-xs text-violet-600 dark:text-violet-400"
>
<span className="mt-1.5 size-1 rounded-full bg-violet-400 dark:bg-violet-500 shrink-0" />
{tip}
</li>
))}
</ul>
</div>
</>
)}
</SectionCard>
{/* Submit error */}
{submitError && (
<div className="flex items-center gap-2 px-4 py-3 rounded-lg bg-red-50 dark:bg-red-950/40 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300 text-sm">
<AlertCircle className="size-4 shrink-0" />
{submitError}
</div>
)}
{/* Actions */}
<div className="flex items-center justify-end gap-3 pt-2">
<button
type="button"
onClick={() => void navigate(APP_ROUTES.USER.INFERENCE.skills)}
className="px-4 py-2 rounded-lg text-sm font-medium text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
>
Cancel
</button>
<button
type="button"
onClick={() => void handleSubmit()}
disabled={submitting}
className="flex items-center gap-1.5 px-4 py-2 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 disabled:opacity-50 transition-opacity"
>
{submitting ? (
<>
<Loader2 className="size-4 animate-spin" />
Creating…
</>
) : (
<>
<Check className="size-4" />
Create Skill
</>
)}
</button>
</div>
</div>
</SubPage>
</>
);
};
export default NewSkill;
|