-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathask-code-minimax.ts
More file actions
179 lines (158 loc) · 5.34 KB
/
Copy pathask-code-minimax.ts
File metadata and controls
179 lines (158 loc) · 5.34 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type { BrowserWindow } from 'electron';
import { debug as logDebug } from '../log.js';
interface MinimaxAskCodeRequest {
requestId: string;
channelId: string;
prompt: string;
}
const MAX_PROMPT_LENGTH = 50_000;
const MAX_CONCURRENT = 5;
const TIMEOUT_MS = 120_000;
const MINIMAX_API_URL = 'https://api.minimax.io/v1/chat/completions';
export const MINIMAX_MODEL = 'MiniMax-M2.7';
const activeRequests = new Map<string, AbortController>();
const activeTimers = new Map<string, ReturnType<typeof setTimeout>>();
/** Main-process storage for the MiniMax API key. Never sent back to the renderer. */
let storedApiKey = '';
export function setMinimaxApiKey(key: string): void {
storedApiKey = key.trim();
}
export function askAboutCodeMinimax(win: BrowserWindow, args: MinimaxAskCodeRequest): void {
const { requestId, channelId, prompt } = args;
const apiKey = storedApiKey;
if (!apiKey) {
throw new Error('MiniMax API key is not set. Please configure it in Settings.');
}
if (prompt.length > MAX_PROMPT_LENGTH) {
throw new Error(`Prompt too long (${prompt.length} chars, max ${MAX_PROMPT_LENGTH})`);
}
if (activeRequests.size >= MAX_CONCURRENT) {
throw new Error('Too many concurrent ask-about-code requests');
}
cancelAskAboutCodeMinimax(requestId);
const controller = new AbortController();
activeRequests.set(requestId, controller);
const send = (msg: unknown) => {
if (!win.isDestroyed()) {
win.webContents.send(`channel:${channelId}`, msg);
}
};
let finished = false;
function cleanup() {
activeRequests.delete(requestId);
const timer = activeTimers.get(requestId);
if (timer) {
clearTimeout(timer);
activeTimers.delete(requestId);
}
}
// Safety timeout: kill after 2 minutes
const timer = setTimeout(() => {
activeTimers.delete(requestId);
if (activeRequests.has(requestId)) {
finished = true;
send({ type: 'error', text: 'Request timed out after 2 minutes.' });
cancelAskAboutCodeMinimax(requestId);
send({ type: 'done', exitCode: 1 });
}
}, TIMEOUT_MS);
activeTimers.set(requestId, timer);
fetch(MINIMAX_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: MINIMAX_MODEL,
messages: [
{
role: 'system',
content: 'Answer concisely about the selected code. Use markdown.',
},
{ role: 'user', content: prompt },
],
// MiniMax temperature must be in (0.0, 1.0]
temperature: 0.3,
max_tokens: 2048,
stream: true,
}),
signal: controller.signal,
})
.then(async (res) => {
if (!res.ok || !res.body) {
const text = await res.text().catch(() => `HTTP ${res.status}`);
throw new Error(`MiniMax API error (${res.status}): ${text}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
// When the AbortController fires, cancel the reader so reader.read() resolves
let aborted = false;
const onAbort = () => {
aborted = true;
reader.cancel().catch((err) => {
logDebug('askCode.minimax', 'reader.cancel rejected', { err: String(err) });
});
};
controller.signal.addEventListener('abort', onAbort, { once: true });
try {
while (true) {
const { done, value } = await reader.read();
if (done || aborted) break;
buf += decoder.decode(value, { stream: true });
const lines = buf.split('\n');
buf = lines.pop() ?? '';
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed === 'data: [DONE]') continue;
if (!trimmed.startsWith('data:')) continue;
try {
const json = JSON.parse(trimmed.slice(5).trim()) as {
choices?: Array<{ delta?: { content?: string } }>;
};
const delta = json.choices?.[0]?.delta?.content;
if (delta) send({ type: 'chunk', text: delta });
} catch {
// ignore parse errors in SSE stream
}
}
}
} finally {
controller.signal.removeEventListener('abort', onAbort);
}
cleanup();
if (!finished) {
finished = true;
send({ type: 'done', exitCode: 0, cancelled: aborted });
}
})
.catch((err: unknown) => {
cleanup();
if (!finished) {
finished = true;
if (err instanceof Error && err.name === 'AbortError') {
// request was cancelled — send done without error, neutral exit code
send({ type: 'done', exitCode: 0, cancelled: true });
} else {
send({ type: 'error', text: err instanceof Error ? err.message : String(err) });
send({ type: 'done', exitCode: 1 });
}
}
});
}
export function cancelAskAboutCodeMinimax(requestId: string): void {
const controller = activeRequests.get(requestId);
if (controller) {
controller.abort();
activeRequests.delete(requestId);
}
const timer = activeTimers.get(requestId);
if (timer) {
clearTimeout(timer);
activeTimers.delete(requestId);
}
}
export function isMinimaxRequestActive(requestId: string): boolean {
return activeRequests.has(requestId);
}