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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 | 2x 2x 1x 2x 4x 4x 4x 4x 2x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 5x 11x 11x 11x 11x 4x 4x 11x 5x 11x 11x 11x 5x 11x 11x 11x 11x 11x 11x 11x 1x 10x 11x 11x 11x 11x 11x 11x 11x 10x 12x 12x 2x | import { MoveToShared, WorkspaceGuard } from "@/components/owner";
import BuiltInBadge from "@/components/badge/BuiltInBadge";
import GeneratedMark from "@/components/badge/GeneratedMark";
import { ResourceCostCard, TokenStepsCard } from "@/components/cost";
import AIProvenance from "@/components/badge/AIProvenance";
import ResourceHistorySection from "@/components/diff/ResourceHistorySection/ResourceHistorySection";
import InlineEdit from "@/components/field/InlineEdit/InlineEdit";
import SubPage from "@/components/page/SubPage/SubPage";
import {
AgentDefinition,
ConditionalEdgeConfig,
FieldSpec,
GraphDefinition,
GraphNodeConfig,
NodeConfigOverrides,
SkillInterface,
parseGraphDefinition,
} from "@/objects/Skill";
import { isSystemManaged } from "@/objects/Doc";
import { AvailableTool } from "@/objects/Tool";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
Background,
BackgroundVariant,
Controls,
ReactFlow,
ReactFlowProvider,
} from "@xyflow/react";
import "@xyflow/react/dist/style.css";
import {
ArrowDownToLine,
ArrowRight,
ArrowUpFromLine,
Braces,
Calendar,
Copy,
Cpu,
Edit2,
GitBranch,
Hand,
Info,
MessageCircleQuestion,
Plus,
Server,
Shuffle,
Sliders,
Sparkles,
Tag,
Trash2,
Wrench,
X,
Zap,
} from "lucide-react";
import React, { useMemo, useState } from "react";
import { Navigate, useLocation, useNavigate, useParams } from "react-router-dom";
import { useAuth } from "../../../../../hooks/useAuth";
import { useWorkerPresets } from "@/hooks/useWorkerPresets";
import { useNodeOverrideOptions } from "@/hooks/useNodeOverrideOptions";
import GraphBuilder, {
describeCondition,
edgeTypes,
hydrateFromDefinition,
nodeTypes,
} from "./GraphBuilder";
import {
CopyButton,
MetaItem,
SectionCard,
Skeleton,
TagList,
} from "@/components/detail";
import { parseServerDate } from "@/util/time";
import { useVersionParam } from "@/hooks/useVersionParam";
import SkillRunForm, { RunSeed } from "./run/SkillRunForm";
import SkillRunsList from "./run/SkillRunsList";
// ─── Primitives ───────────────────────────────────────────────────────────────
const ROLE_COLORS: Record<string, string> = {
planner: "bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300",
analyzer:
"bg-purple-100 text-purple-700 dark:bg-purple-900/40 dark:text-purple-300",
validator:
"bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-300",
researcher:
"bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300",
reviewer: "bg-pink-100 text-pink-700 dark:bg-pink-900/40 dark:text-pink-300",
loop: "bg-orange-100 text-orange-700 dark:bg-orange-900/40 dark:text-orange-300",
router: "bg-cyan-100 text-cyan-700 dark:bg-cyan-900/40 dark:text-cyan-300",
worker: "bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300",
};
const roleColor = (r: string) =>
ROLE_COLORS[r?.toLowerCase()] ?? ROLE_COLORS.worker;
// ─── Tag editor ──────────────────────────────────────────────────────────────
const TagEditor: React.FC<{
tags: string[];
onSave: (tags: string[]) => void;
saving?: boolean;
}> = ({ tags, onSave, saving }) => {
const [draft, setDraft] = useState("");
const add = () => {
const t = draft.trim().toLowerCase();
if (!t || tags.includes(t)) {
setDraft("");
return;
}
onSave([...tags, t]);
setDraft("");
};
const remove = (tag: string) => onSave(tags.filter((t) => t !== tag));
return (
<div className="space-y-2">
<div className="flex flex-wrap gap-1.5 min-h-[22px]">
{tags.length === 0 && (
<span className="text-xs text-gray-400 italic">No tags</span>
)}
{tags.map((tag) => (
<span
key={tag}
className="flex items-center gap-0.5 text-xs px-2 py-0.5 rounded-full bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 border border-gray-200 dark:border-gray-600"
>
<Tag className="size-2.5 text-gray-400" />
{tag}
<button
type="button"
onClick={() => remove(tag)}
disabled={saving}
className="ml-0.5 text-gray-400 hover:text-red-500 transition-colors disabled:opacity-50"
>
<X className="size-2.5" />
</button>
</span>
))}
</div>
<div className="flex items-center gap-1.5">
<input
type="text"
value={draft}
onChange={(e) => setDraft(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
add();
}
}}
placeholder="Add tag…"
className="flex-1 text-xs px-2 py-1 rounded-md border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-1 focus:ring-violet-300"
/>
<button
type="button"
onClick={add}
disabled={!draft.trim() || saving}
className="flex items-center gap-0.5 px-2 py-1 rounded-md text-xs font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-40 transition-opacity"
>
<Plus className="size-3" />
Add
</button>
</div>
</div>
);
};
// ─── Node card ────────────────────────────────────────────────────────────────
const FieldChips: React.FC<{
fields: FieldSpec[];
tone: "in" | "out";
bound?: Record<string, unknown>;
}> = ({ fields, tone, bound }) => (
<div className="flex flex-wrap gap-1 items-center">
{tone === "in" ? (
<ArrowDownToLine className="size-3 text-gray-400" />
) : (
<ArrowUpFromLine className="size-3 text-gray-400" />
)}
{fields.map((f) => (
<span
key={f.name}
title={`${f.type}${f.values?.length ? ` (${f.values.join(" | ")})` : ""}${
f.description ? " — " + f.description : ""
}`}
className={`text-[10px] font-mono px-1.5 py-0.5 rounded border ${
tone === "out"
? "bg-violet-50 dark:bg-violet-950/40 border-violet-200 dark:border-violet-800 text-violet-700 dark:text-violet-300"
: bound && bound[f.name] !== undefined
? "bg-emerald-50 dark:bg-emerald-950/40 border-emerald-200 dark:border-emerald-800 text-emerald-700 dark:text-emerald-300"
: "bg-white dark:bg-gray-700 border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300"
}`}
>
{f.name}
<span className="opacity-60">:{f.type}</span>
</span>
))}
</div>
);
const NodeCard: React.FC<{
nodeId: string;
cfg: GraphNodeConfig;
defn?: AgentDefinition;
isStart: boolean;
isEnd: boolean;
}> = ({ nodeId, cfg, defn, isStart, isEnd }) => {
const type = cfg.type ?? "agent";
const ov: NodeConfigOverrides = cfg.config_overrides ?? {};
const hasOverrides = Object.values(ov).some(
(v) => v !== undefined && v !== "" && !(Array.isArray(v) && v.length === 0),
);
const inputSchema = cfg.input_schema ?? defn?.input_schema ?? [];
const outputSchema = cfg.output_schema ?? defn?.output_schema ?? [];
const bindings = Object.entries(cfg.inputs ?? {});
const TypeIcon =
type === "tool" ? Wrench : type === "transform" ? Shuffle : Sparkles;
return (
<div className="rounded-lg border border-gray-200 dark:border-gray-600 bg-gray-50 dark:bg-gray-800/60 overflow-hidden">
{/* Header */}
<div className="flex items-start justify-between gap-3 px-4 py-3 border-b border-gray-100 dark:border-gray-700/60">
<div className="min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-semibold text-gray-800 dark:text-gray-100 font-mono">
{nodeId}
</span>
<span
className={`flex items-center gap-0.5 text-[10px] font-medium px-1.5 py-0.5 rounded-full uppercase tracking-wide ${roleColor(
type === "agent" ? (defn?.role ?? "worker") : type,
)}`}
>
<TypeIcon className="size-2.5" />
{type === "agent" ? (defn?.role ?? "worker") : type}
</span>
{isStart && (
<span className="text-[10px] font-bold bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300 px-1.5 py-0.5 rounded uppercase tracking-wide">
Start
</span>
)}
{isEnd && (
<span className="text-[10px] font-bold bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300 px-1.5 py-0.5 rounded uppercase tracking-wide">
End
</span>
)}
{cfg.output_key && (
<span className="flex items-center gap-0.5 text-[10px] font-mono bg-amber-50 dark:bg-amber-950/40 text-amber-700 dark:text-amber-300 border border-amber-200 dark:border-amber-800 px-1.5 py-0.5 rounded">
<Braces className="size-2.5" />
{cfg.output_key}
</span>
)}
{cfg.approval && (
<span
title={cfg.approval.message || "Pauses for approval before running"}
className="flex items-center gap-0.5 text-[10px] font-medium bg-orange-50 dark:bg-orange-950/40 text-orange-700 dark:text-orange-300 border border-orange-200 dark:border-orange-800 px-1.5 py-0.5 rounded"
>
<Hand className="size-2.5" />
Approval
</span>
)}
{cfg.ask_user && (
<span
title="May ask the person running the skill clarifying questions"
className="flex items-center gap-0.5 text-[10px] font-medium bg-sky-50 dark:bg-sky-950/40 text-sky-700 dark:text-sky-300 border border-sky-200 dark:border-sky-800 px-1.5 py-0.5 rounded"
>
<MessageCircleQuestion className="size-2.5" />
Asks
</span>
)}
</div>
<div className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
{type === "agent" && (
<>
worker:{" "}
<span className="font-mono text-gray-700 dark:text-gray-300">
{defn?.display_name ?? cfg.agent_name}
</span>
</>
)}
{type === "tool" && (
<>
tool:{" "}
<span className="font-mono text-gray-700 dark:text-gray-300">
{cfg.tool?.name}
</span>
</>
)}
{type === "transform" && (
<>
transform:{" "}
<span className="font-mono text-gray-700 dark:text-gray-300">
{cfg.operation?.op}
</span>
</>
)}
</div>
{cfg.description && (
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1 italic">
{cfg.description}
</p>
)}
</div>
</div>
{/* Definition info */}
{type === "agent" && defn && (
<div className="px-4 py-2.5 border-b border-gray-100 dark:border-gray-700/60">
<p className="text-xs text-gray-500 dark:text-gray-400 leading-relaxed">
{defn.description}
</p>
{defn.default_tools.length > 0 && (
<div className="flex flex-wrap gap-1 mt-2">
{defn.default_tools.map((t) => (
<span
key={t.name}
className="flex items-center gap-0.5 text-[10px] bg-white dark:bg-gray-700 border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 px-1.5 py-0.5 rounded"
>
<Wrench className="size-2.5" />
{t.name}
</span>
))}
</div>
)}
</div>
)}
{/* Typed interface + bindings */}
{(inputSchema.length > 0 ||
outputSchema.length > 0 ||
bindings.length > 0) && (
<div className="px-4 py-2.5 border-b border-gray-100 dark:border-gray-700/60 space-y-1.5">
{inputSchema.length > 0 && (
<FieldChips fields={inputSchema} tone="in" bound={cfg.inputs} />
)}
{bindings.length > 0 && (
<div className="space-y-0.5">
{bindings.map(([k, v]) => (
<div
key={k}
className="text-[11px] font-mono text-gray-600 dark:text-gray-400"
>
<span className="text-gray-800 dark:text-gray-200">{k}</span>
{" ← "}
<span className="text-emerald-700 dark:text-emerald-300">
{typeof v === "string" ? v : JSON.stringify(v)}
</span>
</div>
))}
</div>
)}
{outputSchema.length > 0 && (
<FieldChips fields={outputSchema} tone="out" />
)}
</div>
)}
{/* Tool args / transform operation */}
{type === "tool" &&
cfg.tool &&
Object.keys(cfg.tool.args ?? {}).length > 0 && (
<div className="px-4 py-2.5 border-b border-gray-100 dark:border-gray-700/60">
<pre className="text-[11px] font-mono text-gray-600 dark:text-gray-400 whitespace-pre-wrap">
{JSON.stringify(cfg.tool.args, null, 2)}
</pre>
</div>
)}
{type === "transform" && cfg.operation && (
<div className="px-4 py-2.5 border-b border-gray-100 dark:border-gray-700/60">
<pre className="text-[11px] font-mono text-gray-600 dark:text-gray-400 whitespace-pre-wrap">
{JSON.stringify(
Object.fromEntries(
Object.entries(cfg.operation).filter(([k]) => k !== "op"),
),
null,
2,
)}
</pre>
</div>
)}
{cfg.prompt_template && (
<div className="px-4 py-2.5 border-b border-gray-100 dark:border-gray-700/60">
<div className="text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide mb-1">
Prompt template
</div>
<pre className="text-[11px] font-mono text-gray-600 dark:text-gray-400 whitespace-pre-wrap max-h-32 overflow-y-auto">
{cfg.prompt_template}
</pre>
</div>
)}
{/* Config overrides */}
{hasOverrides && (
<div className="px-4 py-2.5">
<div className="flex items-center gap-1 mb-1.5 text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide">
<Sliders className="size-3" />
Overrides
</div>
<div className="flex flex-wrap gap-x-4 gap-y-1">
{ov.temperature !== undefined && (
<span className="text-xs text-gray-600 dark:text-gray-400">
temp{" "}
<span className="font-mono text-gray-800 dark:text-gray-200">
{ov.temperature}
</span>
</span>
)}
{ov.max_tokens !== undefined && (
<span className="text-xs text-gray-600 dark:text-gray-400">
max_tokens{" "}
<span className="font-mono text-gray-800 dark:text-gray-200">
{ov.max_tokens}
</span>
</span>
)}
{ov.effort && (
<span className="text-xs text-gray-600 dark:text-gray-400">
effort{" "}
<span className="font-mono text-gray-800 dark:text-gray-200">
{ov.effort}
</span>
</span>
)}
{ov.provider && ov.model && (
<span className="flex items-center gap-0.5 text-xs text-violet-600 dark:text-violet-400">
<Cpu className="size-3" />
<span className="font-mono">{ov.model}</span>
</span>
)}
{!!ov.mcp_server_ids?.length && (
<span className="flex items-center gap-0.5 text-xs text-blue-600 dark:text-blue-400">
<Server className="size-3" />
{ov.mcp_server_ids.length} MCP
</span>
)}
{ov.system_prompt && (
<span className="text-xs text-gray-500 dark:text-gray-400 italic truncate max-w-xs">
custom prompt
</span>
)}
</div>
</div>
)}
</div>
);
};
// ─── Conditional routing display ─────────────────────────────────────────────
const ConditionalRoutingCard: React.FC<{
nodeId: string;
config: ConditionalEdgeConfig;
defnMap: Record<string, AgentDefinition>;
graphNodes: Record<string, GraphNodeConfig>;
}> = ({ nodeId, config, defnMap, graphNodes }) => {
const resolveLabel = (id: string) => {
if (id === "end") return "END";
const cfg = graphNodes[id];
if (!cfg) return id;
if ((cfg.type ?? "agent") !== "agent") return id;
return defnMap[cfg.agent_name ?? ""]?.display_name ?? cfg.agent_name ?? id;
};
const mode = config.branches?.length
? "Conditions"
: config.route_field
? `Output field ${nodeId}.${config.route_field}`
: "LLM router";
const rows: Array<{ key: string; target: string; when: string }> = config
.branches?.length
? config.branches.map((b) => ({
key: b.key,
target: b.target,
when: describeCondition(b.when),
}))
: (config.route_keys ?? []).map((k) => ({
key: k,
target: config.edge_map?.[k] ?? "",
when: config.route_field
? `${config.route_field} = ${k}`
: "router picks",
}));
return (
<div className="rounded-lg border border-cyan-200 dark:border-cyan-800 bg-cyan-50 dark:bg-cyan-950/30 overflow-hidden">
<div className="flex items-center justify-between gap-2 px-4 py-2.5 border-b border-cyan-100 dark:border-cyan-800/60">
<div className="flex items-center gap-2 min-w-0">
<GitBranch className="size-3.5 text-cyan-600 dark:text-cyan-400 shrink-0" />
<span className="text-xs font-semibold text-cyan-700 dark:text-cyan-300 truncate">
Conditional routing from <span className="font-mono">{nodeId}</span>
</span>
</div>
<span className="text-[10px] font-medium text-cyan-600 dark:text-cyan-400 uppercase tracking-wide shrink-0">
{mode}
</span>
</div>
<div className="px-4 py-3 space-y-1.5">
{rows.map((r) => (
<div key={r.key} className="flex items-start gap-2 text-xs">
<span className="inline-flex items-center justify-center px-2 py-0.5 rounded-full bg-cyan-100 dark:bg-cyan-900/60 text-cyan-700 dark:text-cyan-300 border border-cyan-200 dark:border-cyan-700 font-medium font-mono whitespace-nowrap">
{r.key}
</span>
<span
className="text-gray-500 dark:text-gray-400 font-mono text-[11px] flex-1 min-w-0 truncate pt-0.5"
title={r.when}
>
{r.when}
</span>
<ArrowRight className="size-3 text-gray-400 shrink-0 mt-0.5" />
<span className="text-gray-700 dark:text-gray-300 font-medium truncate">
{r.target ? (
resolveLabel(r.target)
) : (
<span className="italic text-gray-400">unmapped</span>
)}
</span>
</div>
))}
{config.default && (
<div className="flex items-start gap-2 text-xs pt-1 border-t border-cyan-100 dark:border-cyan-800/60">
<span className="inline-flex items-center justify-center px-2 py-0.5 rounded-full bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-300 border border-gray-300 dark:border-gray-600 font-medium font-mono whitespace-nowrap">
default
</span>
<span className="text-gray-500 dark:text-gray-400 text-[11px] flex-1 pt-0.5">
otherwise
</span>
<ArrowRight className="size-3 text-gray-400 shrink-0 mt-0.5" />
<span className="text-gray-700 dark:text-gray-300 font-medium truncate">
{resolveLabel(config.default)}
</span>
</div>
)}
</div>
</div>
);
};
// ─── Interface card ─────────────────────────────────────────────
const InterfaceCard: React.FC<{
iface: SkillInterface;
state?: GraphDefinition["state"];
}> = ({ iface, state }) => {
const inputs = iface.inputs ?? [];
const outputs = iface.outputs ?? [];
const stateKeys = Object.entries(state ?? {});
Iif (inputs.length === 0 && outputs.length === 0 && stateKeys.length === 0)
return null;
return (
<SectionCard
icon={Braces}
title="Interface"
subtitle="typed contract"
className="mb-4"
>
<div className="grid sm:grid-cols-2 gap-4">
<div>
<div className="text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide mb-1.5">
Inputs
</div>
{inputs.length === 0 ? (
<p className="text-xs text-gray-400 italic">Task text only.</p>
) : (
<FieldChips fields={inputs} tone="in" />
)}
</div>
<div>
<div className="text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide mb-1.5">
Outputs
</div>
{outputs.length === 0 ? (
<p className="text-xs text-gray-400 italic">
Final answer text only.
</p>
) : (
<div className="space-y-0.5">
{outputs.map((o) => (
<div
key={o.name}
className="text-[11px] font-mono text-gray-600 dark:text-gray-400"
>
<span className="text-violet-700 dark:text-violet-300">
{o.name}
</span>
{" ← "}
{o.from || o.name}
</div>
))}
</div>
)}
</div>
{stateKeys.length > 0 && (
<div className="sm:col-span-2">
<div className="text-[10px] font-medium text-gray-400 dark:text-gray-500 uppercase tracking-wide mb-1.5">
State variables
</div>
<div className="flex flex-wrap gap-1">
{stateKeys.map(([k, v]) => (
<span
key={k}
title={v.description}
className="text-[10px] font-mono px-1.5 py-0.5 rounded border bg-amber-50 dark:bg-amber-950/40 border-amber-200 dark:border-amber-800 text-amber-700 dark:text-amber-300"
>
{k}
</span>
))}
</div>
</div>
)}
</div>
</SectionCard>
);
};
// ─── Read-only graph preview ──────────────────────────────────────────────────
const GraphPreview: React.FC<{
definition: GraphDefinition;
defnMap: Record<string, AgentDefinition>;
}> = ({ definition, defnMap }) => {
const { initNodes, initEdges } = React.useMemo(
() => hydrateFromDefinition(definition, defnMap),
[definition, defnMap],
);
return (
<div className="h-80 w-full rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
<ReactFlowProvider>
<ReactFlow
nodes={initNodes}
edges={initEdges}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
fitView
fitViewOptions={{ padding: 0.3 }}
nodesDraggable={false}
nodesConnectable={false}
elementsSelectable={false}
panOnDrag={true}
zoomOnScroll={true}
defaultEdgeOptions={{ animated: true }}
>
<Background
variant={BackgroundVariant.Dots}
gap={20}
size={1}
className="!bg-gray-50 dark:!bg-gray-900"
/>
<Controls
showInteractive={false}
className="!shadow-none !border-gray-200 dark:!border-gray-700"
/>
</ReactFlow>
</ReactFlowProvider>
</div>
);
};
// ─── Main page ────────────────────────────────────────────────────────────────
const SkillViewer: React.FC = () => {
const { skillId } = useParams();
const { getApiHandler } = useApiHandler();
const { user } = useAuth();
const queryClient = useQueryClient();
const navigate = useNavigate();
const [confirmDelete, setConfirmDelete] = useState(false);
const [showEditGraph, setShowEditGraph] = useState(false);
const versionFromQuery = useVersionParam();
// "Run again" on a run page lands here with that run's values.
const location = useLocation();
const rerun = (location.state as { rerun?: RunSeed } | null)?.rerun ?? null;
const runFormRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
Iif (rerun) runFormRef.current?.scrollIntoView({ block: "start" });
}, [rerun]);
const decoded = skillId ? atob(skillId) : "";
// `s:<name>` was a template's own page. Templates are now only a way to
// start a skill, so an old link opens New Skill with that pattern chosen.
const templateName = decoded.startsWith("s:") ? decoded.slice(2) : null;
const decodedId = templateName ? null : decoded || null;
const {
data: skill,
error,
isLoading,
} = useQuery({
queryKey: [QUERY_KEYS.GET.SKILL_BY_ID, decodedId],
queryFn: async () => {
Iif (!decodedId) return null;
return getApiHandler().skill_router.getById(decodedId);
},
enabled: !!decodedId,
});
const { data: agentDefinitions = [] } = useQuery({
queryKey: [QUERY_KEYS.GET.AGENT_DEFINITIONS],
queryFn: () =>
getApiHandler().skill_router.listAgentDefinitions({ is_builtin: true }),
});
const {
definitions: userWorkerDefinitions,
save: savePreset,
remove: removePreset,
} = useWorkerPresets();
const { models, mcpServers } = useNodeOverrideOptions();
const { data: availableTools = [] } = useQuery<AvailableTool[]>({
queryKey: [QUERY_KEYS.GET.AVAILABLE_TOOLS],
queryFn: () => getApiHandler().tool_router.available(),
});
const defnMap = React.useMemo(
() =>
Object.fromEntries(
[...agentDefinitions, ...userWorkerDefinitions].map((d) => [d.name, d]),
),
[agentDefinitions, userWorkerDefinitions],
);
type SkillPatch = Partial<
Pick<
NonNullable<typeof skill>,
"name" | "description" | "tags" | "graph_definition"
>
>;
const updateMutation = useMutation({
mutationFn: (body: SkillPatch) =>
getApiHandler().skill_router.update(decodedId!, body),
onSuccess: () =>
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SKILL_BY_ID, decodedId],
}),
});
const deleteMutation = useMutation({
mutationFn: () => getApiHandler().skill_router.delete(decodedId!),
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SKILLS] });
void navigate(APP_ROUTES.USER.INFERENCE.skills);
},
});
Iif (!skillId)
return <div className="p-4 text-red-500">Invalid skill ID.</div>;
Iif (error)
return <div className="p-4 text-red-500">Error: {error.toString()}</div>;
if (templateName) {
return (
<Navigate
to={`${APP_ROUTES.USER.INFERENCE.SKILLS.new}?template=${encodeURIComponent(templateName)}`}
replace
/>
);
}
// Parse graph definition (v1 / v2 / v3)
const currentGraphDef: GraphDefinition | undefined = parseGraphDefinition(
skill?.graph_definition,
);
const graphNodes = Object.entries(currentGraphDef?.nodes ?? {});
const graphEdges = currentGraphDef?.edges ?? [];
const startNode = currentGraphDef?.start_node ?? "";
const endNodes = currentGraphDef?.end_nodes ?? [];
// A built-in skill (one of the built-in skillsets'): runnable and
// readable, never editable — the API refuses every write.
const readOnly = isSystemManaged(skill);
const actionLinks = isLoading
? []
: [
<CopyButton key="copy" value={skill?.name ?? ""} label="Copy Name" />,
!readOnly && skill && (
<MoveToShared key="move" kind="skill" resource={skill} />
),
readOnly ? (
<BuiltInBadge key="built-in" />
) : (
<button
key="del"
type="button"
onClick={() => setConfirmDelete(true)}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-red-200 dark:border-red-800 text-red-500 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/30 transition-colors"
>
<Trash2 className="size-3" />
Delete
</button>
),
];
// Full-screen graph editor overlay
Iif (showEditGraph) {
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={currentGraphDef}
models={models}
mcpServers={mcpServers}
onSavePreset={savePreset}
onDeletePreset={removePreset}
onDone={(def) => {
updateMutation.mutate({
graph_definition: def as unknown as Record<string, unknown>,
});
setShowEditGraph(false);
}}
onCancel={() => setShowEditGraph(false)}
/>
</div>
);
}
return (
<>
<SubPage
title={
isLoading ? <Skeleton className="h-7 w-48" /> : (skill?.name ?? "—")
}
title_icon={
<GeneratedMark
generated={skill?.is_ai_generated}
size="lg"
label="AI generated skill"
>
<Zap className="size-12 text-violet-400 dark:text-violet-500" />
</GeneratedMark>
}
links={actionLinks}
back={APP_ROUTES.USER.INFERENCE.skills}
>
<WorkspaceGuard resource={skill} noun="skill" copyKind="skill" />
{skill?.is_ai_generated && (
<AIProvenance
resourceLabel="skill"
generationId={skill.generation_id}
generatedByAgentId={skill.generated_by_agent_id}
resourceId={skill.id}
className="mb-4"
/>
)}
{readOnly && (
<p
role="note"
className="mb-4 text-xs leading-relaxed text-indigo-900 dark:text-indigo-200 rounded-xl border border-indigo-200 dark:border-indigo-800/70 bg-indigo-50/70 dark:bg-indigo-950/30 px-4 py-3"
>
A built-in skill, maintained by CHAIN-R and updated with platform
releases. Run it below, or grant its skillset to your own agents.
It cannot be edited, but you can bundle it into a skillset of your
own.
</p>
)}
{/* Metadata strip */}
<div className="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm mb-4 divide-y divide-gray-100 dark:divide-gray-700">
<div className="grid grid-cols-2 sm:grid-cols-3 gap-5 p-5">
{isLoading ? (
Array.from({ length: 3 }).map((_, i) => (
<div key={i} className="flex flex-col gap-1.5">
<Skeleton className="h-3 w-16" />
<Skeleton className="h-4 w-24" />
</div>
))
) : (
<>
<MetaItem
icon={Zap}
label="Name"
value={
readOnly ? (
skill?.name
) : (
<InlineEdit
value={skill?.name ?? ""}
placeholder="Unnamed skill"
saving={updateMutation.isPending}
onSave={(v) => updateMutation.mutate({ name: v })}
/>
)
}
/>
<MetaItem
icon={Info}
label="Description"
value={
readOnly ? (
skill?.description
) : (
<InlineEdit
value={skill?.description ?? ""}
placeholder="No description"
multiline
saving={updateMutation.isPending}
onSave={(v) => updateMutation.mutate({ description: v })}
/>
)
}
/>
<MetaItem
icon={Calendar}
label="Created"
value={
skill?.created_at
? parseServerDate(skill.created_at).toLocaleDateString(
undefined,
{
year: "numeric",
month: "short",
day: "numeric",
},
)
: "—"
}
/>
</>
)}
</div>
{/* Tags row */}
<div className="px-5 py-4">
<div className="flex items-center gap-1 text-xs text-gray-400 dark:text-gray-500 uppercase tracking-wide font-medium mb-2">
<Tag className="size-3" />
Tags
</div>
{isLoading ? (
<div className="flex gap-1.5">
<Skeleton className="h-5 w-14 rounded-full" />
<Skeleton className="h-5 w-20 rounded-full" />
</div>
) : readOnly ? (
<TagList tags={skill?.tags} size="sm" hideIcon />
) : (
<TagEditor
tags={skill?.tags ?? []}
saving={updateMutation.isPending}
onSave={(tags) => updateMutation.mutate({ tags })}
/>
)}
</div>
</div>
{/* Run — the skill used as a structured workflow */}
{!isLoading && decodedId && graphNodes.length > 0 && (
<div ref={runFormRef} className="scroll-mt-4">
<SkillRunForm skillId={decodedId} seed={rerun} className="mb-4" />
<SkillRunsList skillId={decodedId} className="mb-4" />
<ResourceCostCard
source="skill"
sourceId={decodedId}
noun="skill"
className="mb-4"
/>
<TokenStepsCard
source="skill"
sourceId={decodedId}
noun="skill"
className="mb-4"
/>
</div>
)}
{/* Graph section */}
<SectionCard
icon={GitBranch}
title="Graph"
subtitle={
graphNodes.length > 0
? `${graphNodes.length} nodes · ${graphEdges.length} edges`
: undefined
}
className="mb-4"
action={
!isLoading &&
!readOnly && (
<button
type="button"
onClick={() => setShowEditGraph(true)}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
>
<Edit2 className="size-3" />
Edit Graph
</button>
)
}
>
{isLoading ? (
<div className="space-y-2">
{[1, 2, 3].map((i) => (
<Skeleton key={i} className="h-20 w-full" />
))}
</div>
) : graphNodes.length === 0 ? (
<div className="text-center py-6">
<GitBranch className="size-6 text-gray-300 dark:text-gray-600 mx-auto mb-2" />
<p className="text-sm text-gray-400 dark:text-gray-500 italic mb-3">
No graph definition found.
</p>
<button
type="button"
onClick={() => setShowEditGraph(true)}
className="px-3 py-1.5 text-sm font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 rounded-lg hover:opacity-80 transition-opacity"
>
Build Graph
</button>
</div>
) : (
<div className="space-y-4">
{/* Visual graph preview */}
{currentGraphDef && (
<GraphPreview definition={currentGraphDef} defnMap={defnMap} />
)}
{/* Node cards */}
<div className="space-y-2">
{graphNodes.map(([nodeId, nodeCfg]) => (
<NodeCard
key={nodeId}
nodeId={nodeId}
cfg={nodeCfg}
defn={defnMap[nodeCfg.agent_name ?? ""]}
isStart={nodeId === startNode}
isEnd={endNodes.includes(nodeId)}
/>
))}
</div>
{/* Conditional routing */}
{currentGraphDef?.conditional_edges &&
Object.keys(currentGraphDef.conditional_edges).length > 0 && (
<div className="space-y-2">
{Object.entries(currentGraphDef.conditional_edges).map(
([nodeId, ce]) => (
<ConditionalRoutingCard
key={nodeId}
nodeId={nodeId}
config={ce}
defnMap={defnMap}
graphNodes={currentGraphDef.nodes}
/>
),
)}
</div>
)}
{/* Raw JSON toggle */}
<details className="mt-1">
<summary className="text-xs text-gray-400 dark:text-gray-500 cursor-pointer hover:text-gray-600 dark:hover:text-gray-300 transition-colors select-none">
View raw JSON
</summary>
<div className="relative mt-2">
<pre className="text-xs text-gray-600 dark:text-gray-400 bg-gray-50 dark:bg-gray-700/50 rounded-lg p-4 overflow-x-auto max-h-64 font-mono leading-relaxed">
{JSON.stringify(skill?.graph_definition, null, 2)}
</pre>
<div className="absolute top-2 right-2">
<CopyButton
value={JSON.stringify(skill?.graph_definition, null, 2)}
label="Copy JSON"
/>
</div>
</div>
</details>
</div>
)}
</SectionCard>
{/* Typed interface */}
{!isLoading && currentGraphDef && (
<InterfaceCard
iface={currentGraphDef.interface ?? {}}
state={currentGraphDef.state}
/>
)}
{/* Edit history */}
<ResourceHistorySection
className="mb-4"
source={getApiHandler().skill_router}
resourceId={decodedId}
initialVersion={versionFromQuery}
queryKey={QUERY_KEYS.GET.SKILL_HISTORY}
fieldLabels={{
name: "Name",
description: "Description",
graph_definition: "Graph Definition",
tags: "Tags",
}}
/>
</SubPage>
{/* Delete modal */}
{confirmDelete && (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/40">
<div className="w-full max-w-sm rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 shadow-xl p-6 space-y-4">
<div className="flex items-center gap-2">
<Trash2 className="size-5 text-red-500" />
<h2 className="text-sm font-semibold text-gray-800 dark:text-gray-200">
Delete Skill
</h2>
</div>
<p className="text-sm text-gray-500 dark:text-gray-400">
Are you sure you want to delete{" "}
<span className="font-medium text-gray-800 dark:text-gray-200">
{skill?.name}
</span>
? This will also remove it from any agents it is assigned to.
</p>
<div className="flex items-center justify-end gap-3">
<button
type="button"
onClick={() => setConfirmDelete(false)}
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={() => deleteMutation.mutate()}
disabled={deleteMutation.isPending}
className="flex items-center gap-1.5 px-4 py-2 rounded-lg bg-red-600 text-white text-sm font-medium hover:opacity-80 disabled:opacity-50 transition-opacity"
>
{deleteMutation.isPending ? "Deleting…" : "Delete"}
</button>
</div>
</div>
</div>
)}
</>
);
};
export default SkillViewer;
|