-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathusePromptVersionsWithLabels.ts
More file actions
73 lines (63 loc) · 2.08 KB
/
Copy pathusePromptVersionsWithLabels.ts
File metadata and controls
73 lines (63 loc) · 2.08 KB
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
import { useMemo } from "react";
import usePromptVersionsById from "@/api/prompts/usePromptVersionsById";
import { PromptVersion } from "@/types/prompts";
import { pickHighestStage } from "@/utils/version-stages";
export type PromptVersionDescriptor = {
version: PromptVersion;
index: number;
label: string;
stage: string | undefined;
};
type Options = {
enabled?: boolean;
staleTime?: number;
/**
* Result page size. Default of 100 matches the previous in-place fetches in
* PromptCard / AgentRunnerPromptCard. Note this still caps how far back
* `getDescriptor` can resolve a label; older versions return `undefined`.
*/
size?: number;
};
const SORTING = [{ id: "created_at", desc: true }];
/**
* Loads versions for a prompt and produces stable descriptors (`vN` label +
* highest-priority stage tag), so the trace prompt card and agent-runner card
* stay consistent without each rolling its own indexing logic.
*/
export function usePromptVersionsWithLabels(
promptId: string,
{ enabled = true, staleTime = 60_000, size = 100 }: Options = {},
) {
const { data, isLoading } = usePromptVersionsById(
{ promptId, page: 1, size, sorting: SORTING },
{ enabled: enabled && Boolean(promptId), staleTime },
);
const versions = useMemo(() => data?.content ?? [], [data?.content]);
const total = data?.total ?? versions.length;
const descriptors = useMemo<PromptVersionDescriptor[]>(
() =>
versions.map((version, index) => ({
version,
index,
label: version.version_number ?? version.commit,
stage: pickHighestStage(version.tags),
})),
[versions],
);
const getDescriptor = useMemo(() => {
const byId = new Map<string, PromptVersionDescriptor>();
descriptors.forEach((d) => byId.set(d.version.id, d));
return (
versionId: string | undefined,
): PromptVersionDescriptor | undefined =>
versionId ? byId.get(versionId) : undefined;
}, [descriptors]);
return {
versions,
descriptors,
total,
isLoading,
getDescriptor,
};
}
export default usePromptVersionsWithLabels;