Skip to content

Commit fa8e521

Browse files
mitya52alashchev17
andauthored
UI fixes 19 02 25 (#509)
* ordered code_completion_models and code_chat_models * pass default system prompt into UI * chore: fix prettier errors * fix: tool use changing based on if models support tools and agent & fixes for types and stubs in caps * fix: proper checks for default system prompt * fix: interacted state for switching tool use manually * fix: setting local state for first tool use set if no models, then user can click and change tool use * fix: setting wasAdjusted even if no tool use prederermined --------- Co-authored-by: alashchev17 <[email protected]>
1 parent 2458213 commit fa8e521

File tree

5 files changed

+91
-17
lines changed

5 files changed

+91
-17
lines changed

refact-agent/engine/src/caps.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::PathBuf;
22
use std::collections::HashMap;
3+
use indexmap::IndexMap;
34
use std::fs::File;
45
use std::io::Read;
56
use std::sync::Arc;
@@ -25,7 +26,7 @@ pub struct ModelRecord {
2526
#[serde(default)]
2627
pub n_ctx: usize,
2728
#[serde(default)]
28-
pub supports_scratchpads: HashMap<String, serde_json::Value>,
29+
pub supports_scratchpads: HashMap<String, Value>,
2930
#[serde(default)]
3031
pub default_scratchpad: String,
3132
#[serde(default)]
@@ -42,8 +43,8 @@ pub struct ModelRecord {
4243

4344
#[derive(Debug, Deserialize)]
4445
pub struct ModelsOnly {
45-
pub code_completion_models: HashMap<String, ModelRecord>,
46-
pub code_chat_models: HashMap<String, ModelRecord>,
46+
pub code_completion_models: IndexMap<String, ModelRecord>,
47+
pub code_chat_models: IndexMap<String, ModelRecord>,
4748
pub tokenizer_rewrite_path: HashMap<String, String>,
4849
}
4950

@@ -110,7 +111,7 @@ pub struct CodeAssistantCaps {
110111
#[serde(default = "default_telemetry_basic_retrieve_my_own")]
111112
pub telemetry_basic_retrieve_my_own: String,
112113
#[serde(default)]
113-
pub code_completion_models: HashMap<String, ModelRecord>,
114+
pub code_completion_models: IndexMap<String, ModelRecord>,
114115
#[serde(default)]
115116
#[serde(alias = "completion_model")]
116117
pub code_completion_default_model: String,
@@ -121,7 +122,7 @@ pub struct CodeAssistantCaps {
121122
#[serde(alias = "completion_n_ctx")]
122123
pub code_completion_n_ctx: usize,
123124
#[serde(default)]
124-
pub code_chat_models: HashMap<String, ModelRecord>,
125+
pub code_chat_models: IndexMap<String, ModelRecord>,
125126
#[serde(default)]
126127
#[serde(alias = "chat_model")]
127128
pub code_chat_default_model: String,
@@ -147,8 +148,8 @@ pub struct CodeAssistantCaps {
147148
pub running_models: Vec<String>, // check there if a model is available or not, not in other places
148149
#[serde(default)]
149150
pub caps_version: i64, // need to reload if it increases on server, that happens when server configuration changes
150-
// #[serde(default)]
151-
// pub code_chat_default_system_prompt: String,
151+
#[serde(default)]
152+
pub code_chat_default_system_prompt: String,
152153

153154
#[serde(default)]
154155
pub customization: String, // on self-hosting server, allows to customize yaml_configs & friends for all engineers
@@ -463,7 +464,7 @@ fn _inherit_r1_from_r0(
463464
}
464465

465466
pub fn which_model_to_use<'a>(
466-
models: &'a HashMap<String, ModelRecord>,
467+
models: &'a IndexMap<String, ModelRecord>,
467468
user_wants_model: &str,
468469
default_model: &str,
469470
) -> Result<(String, &'a ModelRecord), String> {
@@ -473,15 +474,15 @@ pub fn which_model_to_use<'a>(
473474
}
474475
let no_finetune = strip_model_from_finetune(&take_this_one.to_string());
475476
if let Some(model_rec) = models.get(&take_this_one.to_string()) {
476-
return Ok((take_this_one.to_string(), model_rec));
477+
Ok((take_this_one.to_string(), model_rec))
477478
} else if let Some(model_rec) = models.get(&no_finetune) {
478-
return Ok((take_this_one.to_string(), model_rec));
479+
Ok((take_this_one.to_string(), model_rec))
479480
} else {
480-
return Err(format!(
481+
Err(format!(
481482
"Model '{}' not found. Server has these models: {:?}",
482483
take_this_one,
483484
models.keys()
484-
));
485+
))
485486
}
486487
}
487488

refact-agent/gui/src/__fixtures__/caps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,15 @@ export const STUB_CAPS_RESPONSE: CapsResponse = {
339339
"groq-llama-3.1-8b",
340340
"groq-llama-3.1-70b",
341341
],
342+
code_chat_default_system_prompt: "default",
342343
caps_version: 0,
343344
};
344345

345346
export const EMPTY_CAPS_RESPONSE: CapsResponse = {
346347
caps_version: 0,
347348
cloud_name: "",
348349
code_chat_default_model: "",
350+
code_chat_default_system_prompt: "",
349351
code_chat_models: {},
350352
code_completion_default_model: "",
351353
code_completion_models: {},

refact-agent/gui/src/components/ChatForm/PromptSelect.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useAppDispatch,
77
useAppSelector,
88
useGetPromptsQuery,
9+
useGetCapsQuery,
910
} from "../../hooks";
1011
import { getSelectedSystemPrompt } from "../../features/Chat/Thread/selectors";
1112
import { setSystemPrompt } from "../../features/Chat/Thread/actions";
@@ -30,9 +31,21 @@ export const PromptSelect: React.FC = () => {
3031
[onSetSelectedSystemPrompt, promptsRequest.data],
3132
);
3233

34+
const caps = useGetCapsQuery();
35+
36+
const default_system_prompt = useMemo(() => {
37+
if (
38+
caps.data?.code_chat_default_system_prompt &&
39+
caps.data.code_chat_default_system_prompt !== ""
40+
) {
41+
return caps.data.code_chat_default_system_prompt;
42+
}
43+
return "default";
44+
}, [caps.data?.code_chat_default_system_prompt]);
45+
3346
const val = useMemo(
34-
() => Object.keys(selectedSystemPrompt)[0] ?? "default",
35-
[selectedSystemPrompt],
47+
() => Object.keys(selectedSystemPrompt)[0] ?? default_system_prompt,
48+
[selectedSystemPrompt, default_system_prompt],
3649
);
3750

3851
const options = useMemo(() => {
@@ -44,6 +57,12 @@ export const PromptSelect: React.FC = () => {
4457
});
4558
}, [promptsRequest.data]);
4659

60+
const isLoading = useMemo(
61+
() =>
62+
promptsRequest.isLoading || promptsRequest.isFetching || caps.isLoading,
63+
[promptsRequest.isLoading, promptsRequest.isFetching, caps.isLoading],
64+
);
65+
4766
return (
4867
<Flex
4968
gap="2"
@@ -56,7 +75,7 @@ export const PromptSelect: React.FC = () => {
5675
<Text size="2" wrap="nowrap">
5776
System Prompt:
5877
</Text>
59-
<Skeleton loading={promptsRequest.isLoading || promptsRequest.isFetching}>
78+
<Skeleton loading={isLoading}>
6079
<Box flexGrow="1" flexShrink="0">
6180
<Select
6281
name="system prompt"

refact-agent/gui/src/hooks/useCapsForToolUse.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useMemo } from "react";
1+
import { useCallback, useEffect, useMemo, useState } from "react";
22
import { selectThreadToolUse } from "../features/Chat/Thread/selectors";
33
import {
44
useAppSelector,
@@ -8,7 +8,12 @@ import {
88
useGetUser,
99
} from ".";
1010

11-
import { getSelectedChatModel, setChatModel } from "../features/Chat";
11+
import {
12+
getSelectedChatModel,
13+
setChatModel,
14+
setToolUse,
15+
ToolUse,
16+
} from "../features/Chat";
1217

1318
// TODO: hard coded for now.
1419
const PAID_AGENT_LIST = [
@@ -20,6 +25,7 @@ const PAID_AGENT_LIST = [
2025
];
2126

2227
export function useCapsForToolUse() {
28+
const [wasAdjusted, setWasAdjusted] = useState(false);
2329
const caps = useGetCapsQuery();
2430
const toolUse = useAppSelector(selectThreadToolUse);
2531
const usage = useAgentUsage();
@@ -48,6 +54,20 @@ export function useCapsForToolUse() {
4854
return true;
4955
}, [caps.data?.code_chat_models, currentModel]);
5056

57+
const modelsSupportingTools = useMemo(() => {
58+
const models = caps.data?.code_chat_models ?? {};
59+
return Object.entries(models)
60+
.filter(([_, value]) => value.supports_tools)
61+
.map(([key]) => key);
62+
}, [caps.data?.code_chat_models]);
63+
64+
const modelsSupportingAgent = useMemo(() => {
65+
const models = caps.data?.code_chat_models ?? {};
66+
return Object.entries(models)
67+
.filter(([_, value]) => value.supports_agent)
68+
.map(([key]) => key);
69+
}, [caps.data?.code_chat_models]);
70+
5171
const usableModels = useMemo(() => {
5272
const models = caps.data?.code_chat_models ?? {};
5373
const items = Object.entries(models).reduce<string[]>(
@@ -95,6 +115,37 @@ export function useCapsForToolUse() {
95115
}
96116
}, [currentModel, setCapModel, usableModels, usableModelsForPlan]);
97117

118+
useEffect(() => {
119+
const determineNewToolUse = (): ToolUse | null => {
120+
if (toolUse === "agent" && modelsSupportingAgent.length === 0) {
121+
return "explore";
122+
}
123+
if (toolUse === "explore" && modelsSupportingTools.length === 0) {
124+
return "quick";
125+
}
126+
return null;
127+
};
128+
129+
const handleAutomaticToolUseChange = () => {
130+
if (!caps.isSuccess || wasAdjusted) return;
131+
132+
const newToolUse = determineNewToolUse();
133+
if (newToolUse) {
134+
dispatch(setToolUse(newToolUse));
135+
}
136+
setWasAdjusted(true);
137+
};
138+
139+
handleAutomaticToolUseChange();
140+
}, [
141+
dispatch,
142+
wasAdjusted,
143+
caps.isSuccess,
144+
toolUse,
145+
modelsSupportingAgent,
146+
modelsSupportingTools,
147+
]);
148+
98149
return {
99150
usableModels,
100151
usableModelsForPlan,

refact-agent/gui/src/services/refact/caps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export type CapsResponse = {
7979
caps_version: number;
8080
cloud_name: string;
8181
code_chat_default_model: string;
82+
code_chat_default_system_prompt: string;
8283
code_chat_models: Record<string, CodeChatModel>;
8384
code_completion_default_model: string;
8485
code_completion_models: Record<string, CodeCompletionModel>;

0 commit comments

Comments
 (0)