-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathuseCapsForToolUse.ts
More file actions
103 lines (91 loc) · 3.06 KB
/
useCapsForToolUse.ts
File metadata and controls
103 lines (91 loc) · 3.06 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
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
import { useCallback, useEffect, useMemo } from "react";
import { selectThreadToolUse } from "../features/Chat/Thread/selectors";
import {
useAppSelector,
useGetCapsQuery,
useAgentUsage,
useAppDispatch,
} from ".";
import { getSelectedChatModel, setChatModel } from "../features/Chat";
// TODO: hard coded for now.
const PAID_AGENT_LIST = [
"gpt-4o",
"claude-3-5-sonnet",
"grok-2-1212",
"grok-beta",
"gemini-2.0-flash-exp",
];
export function useCapsForToolUse() {
const caps = useGetCapsQuery();
const toolUse = useAppSelector(selectThreadToolUse);
const usage = useAgentUsage();
const dispatch = useAppDispatch();
const defaultCap = caps.data?.code_chat_default_model ?? "";
const selectedModel = useAppSelector(getSelectedChatModel);
const currentModel = selectedModel || defaultCap;
const setCapModel = useCallback(
(value: string) => {
const model = caps.data?.code_chat_default_model === value ? "" : value;
const action = setChatModel(model);
dispatch(action);
},
[caps.data?.code_chat_default_model, dispatch],
);
const isMultimodalitySupportedForCurrentModel = useMemo(() => {
const models = caps.data?.code_chat_models;
const item = models?.[currentModel];
if (!item) return false;
if (!item.supports_multimodality) return false;
return true;
}, [caps.data?.code_chat_models, currentModel]);
const usableModels = useMemo(() => {
const models = caps.data?.code_chat_models ?? {};
const items = Object.entries(models).reduce<string[]>(
(acc, [key, value]) => {
if (toolUse === "explore" && value.supports_tools) {
return [...acc, key];
}
if (toolUse === "agent" && value.supports_agent) return [...acc, key];
if (toolUse === "quick") return [...acc, key];
return acc;
},
[],
);
return items;
}, [caps.data?.code_chat_models, toolUse]);
const usableModelsForPlan = useMemo(() => {
if (!usage.aboveUsageLimit && toolUse === "agent") return usableModels;
return usableModels.map((model) => {
if (!PAID_AGENT_LIST.includes(model)) return model;
return {
value: model,
disabled: true,
textValue:
toolUse !== "agent" ? `${model} (Available in agent)` : undefined,
};
});
}, [usableModels, usage.aboveUsageLimit, toolUse]);
useEffect(() => {
if (
usableModelsForPlan.length > 0 &&
usableModelsForPlan.some((elem) => typeof elem === "string") &&
!usableModelsForPlan.includes(currentModel)
) {
const models: string[] = usableModelsForPlan.filter(
(elem): elem is string => typeof elem === "string",
);
const toChange =
models.find((elem) => currentModel.startsWith(elem)) ??
(models[0] || "");
setCapModel(toChange);
}
}, [currentModel, setCapModel, usableModels, usableModelsForPlan]);
return {
usableModels,
usableModelsForPlan,
currentModel,
setCapModel,
isMultimodalitySupportedForCurrentModel,
loading: !caps.data && (caps.isFetching || caps.isLoading),
};
}