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 | 34x 34x 34x 34x 9x 34x 34x 33x 4x 4x 34x 33x 4x 4x 34x 2x 34x 2x 34x 1x 34x 2x 2x 1x 1x 34x 2x 2x 1x 1x 1x 34x | import { AgentDefinition } from "@/objects/Skill";
import { CustomWorker } from "@/objects/Worker";
import { WorkerPresetDraft } from "@/apps/user/pages/agents/skills/GraphBuilder";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useAuth } from "@/hooks/useAuth";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback, useMemo } from "react";
/**
* The user's saved worker presets, as graph-palette definitions.
*
* A preset is a `CustomWorker` row registered in svc-ai under its own
* definition name — the name a graph node must reference. Presets are created
* and removed from inside the graph builder, because everything a preset
* changes about a node (prompt, tool setting, typed interface) is already
* editable on the node itself; the preset only names that configuration so it
* can be reused. Both builder hosts — a new skill and an existing one — need
* the same three things, so they live here rather than in either page.
*
* `save` and `remove` resolve to an error message, or null on success: the
* builder renders the failure inline next to the control that caused it rather
* than throwing out of a drag-and-drop surface.
*/
export function useWorkerPresets(enabled: boolean = true) {
const { getApiHandler } = useApiHandler();
const { user } = useAuth();
const queryClient = useQueryClient();
const { data } = useQuery<CustomWorker[]>({
queryKey: [QUERY_KEYS.GET.USER_WORKERS, user?.user_id],
queryFn: () => getApiHandler().worker_router.list(),
enabled: enabled && !!user?.user_id,
});
// Bound to an explicitly typed const: the pinned TypeScript cannot infer
// react-query v5's return type, so `data` arrives as `any` and every
// callback below would land as an implicit any.
const workers: CustomWorker[] = data ?? [];
/**
* Only a registered worker is referencable as a graph `agent_name`, so an
* unregistered row is deliberately dropped rather than offered as a node
* that would fail to resolve at execution time.
*/
const definitions = useMemo(
(): AgentDefinition[] =>
workers
.filter((w) => !!w.definition_name)
.map((w) => ({
name: w.definition_name!,
display_name: w.name,
description:
w.description ??
(w.base_worker
? `Custom variant of ${w.base_worker}`
: "Custom worker"),
role: w.role ?? "worker",
tags: w.tags ?? [],
default_tools: [],
tools_enabled: w.tools_enabled,
input_schema: w.input_schema ?? [],
output_schema: w.output_schema ?? [],
})),
[workers],
);
/** definition_name → the row id, so a palette card can delete its preset. */
const idByDefinitionName = useMemo(
() =>
Object.fromEntries(
workers
.filter((w) => !!w.definition_name)
.map((w) => [w.definition_name!, w.id]),
),
[workers],
);
const invalidate = useCallback(
() =>
queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.USER_WORKERS],
}),
[queryClient],
);
const createMutation = useMutation({
mutationFn: (draft: WorkerPresetDraft) =>
getApiHandler().worker_router.create({
name: draft.name,
description: draft.description || undefined,
base_worker: draft.base_worker,
role: draft.role,
system_prompt: draft.system_prompt || undefined,
tags: [],
tools_enabled: draft.tools_enabled,
input_schema: draft.input_schema,
output_schema: draft.output_schema,
}),
onSuccess: invalidate,
});
const deleteMutation = useMutation({
mutationFn: (id: string) => getApiHandler().worker_router.delete(id),
onSuccess: invalidate,
});
const save = useCallback(
async (draft: WorkerPresetDraft): Promise<string | null> => {
try {
const created = await createMutation.mutateAsync(draft);
return created ? null : "Could not save the preset.";
} catch (e: unknown) {
return (e as { message?: string })?.message ?? "Could not save the preset.";
}
},
[createMutation],
);
const remove = useCallback(
async (defn: AgentDefinition): Promise<string | null> => {
const id = idByDefinitionName[defn.name];
// A builtin has no row behind it, and neither does a preset this build
// has not refetched yet — refusing is better than deleting by guess.
if (!id) return "This worker cannot be deleted here.";
try {
const ok = await deleteMutation.mutateAsync(id);
return ok ? null : "Could not delete the preset.";
} catch (e: unknown) {
return (
(e as { message?: string })?.message ?? "Could not delete the preset."
);
}
},
[deleteMutation, idByDefinitionName],
);
return { definitions, save, remove };
}
export default useWorkerPresets;
|