Skip to content

Commit 269f9dd

Browse files
authored
Merge pull request #8558 from sagemathinc/fix-hint-button-sagews-8546
frontend/sagews: add "give me a hint" button to aid users
2 parents 2f3f0ae + 726c836 commit 269f9dd

File tree

3 files changed

+91
-42
lines changed

3 files changed

+91
-42
lines changed

src/packages/frontend/frame-editors/llm/help-me-fix-utils.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface GetHelpOptions {
1818
redux: any;
1919
prioritize?: "start" | "start-end" | "end";
2020
model: string;
21+
isHint?: boolean;
2122
}
2223

2324
export interface CreateMessageOpts {
@@ -35,23 +36,22 @@ export interface CreateMessageOpts {
3536
isHint?: boolean;
3637
}
3738

38-
export async function getHelp(options: GetHelpOptions) {
39-
const {
40-
project_id,
41-
path,
42-
tag,
43-
line = "",
44-
error,
45-
input,
46-
task,
47-
language,
48-
extraFileInfo,
49-
redux,
50-
prioritize,
51-
model,
52-
} = options;
53-
54-
const solutionText = createMessage({
39+
export async function getHelp({
40+
project_id,
41+
path,
42+
tag,
43+
line = "",
44+
error,
45+
input,
46+
task,
47+
language,
48+
extraFileInfo,
49+
redux,
50+
prioritize,
51+
model,
52+
isHint = false,
53+
}: GetHelpOptions) {
54+
const messageText = createMessage({
5555
error,
5656
task,
5757
line,
@@ -60,17 +60,18 @@ export async function getHelp(options: GetHelpOptions) {
6060
extraFileInfo,
6161
model,
6262
prioritize,
63-
open: false,
64-
full: false,
65-
isHint: false,
63+
open: true,
64+
full: true,
65+
isHint,
6666
});
6767

6868
try {
6969
const actions = await getChatActions(redux, project_id, path);
7070
setTimeout(() => actions.scrollToBottom(), 100);
71+
const tagSuffix = isHint ? "hint" : "solution";
7172
await actions.sendChat({
72-
input: solutionText,
73-
tag: `help-me-fix-solution${tag ? `:${tag}` : ""}`,
73+
input: messageText,
74+
tag: `help-me-fix-${tagSuffix}${tag ? `:${tag}` : ""}`,
7475
noNotification: true,
7576
});
7677
} catch (err) {
@@ -139,8 +140,8 @@ export function createMessage({
139140
prioritize === "start"
140141
? "starts"
141142
: prioritize === "end"
142-
? "ends"
143-
: "starts and ends";
143+
? "ends"
144+
: "starts and ends";
144145
message.push(
145146
`My ${
146147
extraFileInfo ?? ""

src/packages/frontend/sagews/chatgpt.ts

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,41 @@ import { SETTINGS_LANGUAGE_MODEL_KEY } from "../account/useLanguageModelSetting"
77
export function isEnabled(project_id: string): boolean {
88
return redux
99
.getStore("projects")
10-
.hasLanguageModelEnabled(project_id, "help-me-fix");
10+
.hasLanguageModelEnabled(project_id, "help-me-fix-solution");
1111
}
12-
export function helpMeFix({
13-
codemirror,
14-
stderr,
15-
uuid,
16-
project_id,
17-
path,
18-
}): void {
12+
13+
export function isHintEnabled(project_id: string): boolean {
14+
return redux
15+
.getStore("projects")
16+
.hasLanguageModelEnabled(project_id, "help-me-fix-hint");
17+
}
18+
19+
interface HelpParams {
20+
codemirror: any;
21+
stderr: string;
22+
uuid: string;
23+
project_id: string;
24+
path: string;
25+
}
26+
27+
function getHelpCommon(params: HelpParams, isHint: boolean): void {
28+
const { codemirror, stderr, uuid, project_id, path } = params;
29+
30+
// Show confirmation dialog
31+
const action = isHint ? "get a hint" : "get help to fix this error";
32+
const confirmMessage = `This will query a language model to ${action}. The error message and your code will be sent to the AI service for analysis. Do you want to continue?`;
33+
34+
if (!window.confirm(confirmMessage)) {
35+
return; // User cancelled
36+
}
37+
1938
const val = codemirror.getValue();
2039
const i = val.indexOf(uuid);
2140
if (i == -1) return;
2241
const j = val.lastIndexOf(MARKERS.cell, i);
2342
const k = val.lastIndexOf(MARKERS.output, i);
2443
const input = val.slice(j + 1, k).trim();
44+
2545
// use the currently set language model from the account store
2646
// https://github.com/sagemathinc/cocalc/pull/7278
2747
const other_settings = redux.getStore("account").get("other_settings");
@@ -41,6 +61,7 @@ export function helpMeFix({
4161
custom_openai: Object.keys(customOpenAI),
4262
selectable_llms: selectableLLMs,
4363
});
64+
4465
getHelp({
4566
project_id,
4667
path,
@@ -53,5 +74,14 @@ export function helpMeFix({
5374
redux,
5475
prioritize: "end",
5576
model,
77+
isHint,
5678
});
5779
}
80+
81+
export function helpMeFix(params: HelpParams): void {
82+
getHelpCommon(params, false);
83+
}
84+
85+
export function giveMeAHint(params: HelpParams): void {
86+
getHelpCommon(params, true);
87+
}

src/packages/frontend/sagews/sagews.coffee

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,23 +1261,41 @@ class SynchronizedWorksheet extends SynchronizedDocument2
12611261
output.append($("<span class='sagews-output-stdout'>").text(mesg.stdout))
12621262

12631263
if mesg.stderr?
1264-
# This is entirely for the ChatGPT help button:
1265-
# TODO: don't show if chatgpt disabled.
1266-
if chatgpt.isEnabled(@project_id)
1267-
cur = output.data('stderr')
1268-
if not cur
1269-
button = $("<div><span title='@ChatGPT, help fix this...' style='font-family:sans-serif;' class='btn btn-default'>Help me fix this...</span></div>")
1270-
button.click () =>
1264+
# This is entirely for the ChatGPT help buttons:
1265+
cur = output.data('stderr')
1266+
if not cur
1267+
buttonsContainer = $("<div style='margin: 10px 0;'>")
1268+
1269+
# Add hint button if enabled
1270+
if chatgpt.isHintEnabled(@project_id)
1271+
hintButton = $("<span title='Get a hint to help fix this...' style='font-family:sans-serif; margin-right: 5px;' class='btn btn-default'>Give me a hint...</span>")
1272+
hintButton.click () =>
1273+
chatgpt.giveMeAHint
1274+
codemirror : @focused_codemirror()
1275+
stderr : output.data('stderr')
1276+
uuid : output.data('uuid')
1277+
project_id : @project_id
1278+
path : @filename
1279+
buttonsContainer.append(hintButton)
1280+
1281+
# Add solution button if enabled
1282+
if chatgpt.isEnabled(@project_id)
1283+
solutionButton = $("<span title='Get help to fix this...' style='font-family:sans-serif;' class='btn btn-default'>Help me fix this...</span>")
1284+
solutionButton.click () =>
12711285
chatgpt.helpMeFix
12721286
codemirror : @focused_codemirror()
12731287
stderr : output.data('stderr')
12741288
uuid : output.data('uuid')
12751289
project_id : @project_id
12761290
path : @filename
1277-
output.append(button);
1291+
buttonsContainer.append(solutionButton)
1292+
1293+
# Only append container if it has buttons
1294+
if buttonsContainer.children().length > 0
1295+
output.append(buttonsContainer)
12781296
output.data('stderr', mesg.stderr)
1279-
else
1280-
output.data('stderr', cur + mesg.stderr)
1297+
else
1298+
output.data('stderr', cur + mesg.stderr)
12811299

12821300
output.append($("<span class='sagews-output-stderr'>").text(mesg.stderr))
12831301

0 commit comments

Comments
 (0)