-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
87 lines (75 loc) · 2.25 KB
/
index.ts
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
import * as vscode from "vscode";
import { randomUUID } from "crypto";
import { ChatMessage, getPromptChat } from "../prompt/promptChat";
import Logger from "../logger";
import { sendChatRequestLocal as sendChatRequestLocal } from "./localChat";
import { configuration } from "../utils/configuration";
import statusBar from "../statusBar";
import { sendChatRequestCloud } from "./cloudChat";
import {
getHighlightedTextDetails,
humanMessageWithCodePrompt,
systemTemplate,
} from "./utils/useHighlightedTextAsContext";
const logCompletion = () => {
const uuid = randomUUID();
return {
info: (text: any) =>
Logger.info(text, { component: `Chat: ${uuid.slice(-8)}` }),
uuid: () => uuid,
};
};
export async function* chat(
history: ChatMessage[],
config?: {
provideHighlightedText?: boolean;
abortController: AbortController;
}
) {
const loggerCompletion = logCompletion();
loggerCompletion.info("Chat: started");
const parameters = {
n_predict: 4096,
stop: [],
temperature: 0.7,
controller: config?.abortController,
};
const { stopTask } = statusBar.startTask();
try {
Logger.info(`Start request;`, {
component: "chat",
sendTelemetry: true,
});
if (config?.provideHighlightedText) {
const highlighted = getHighlightedTextDetails();
if (highlighted !== null) {
const firstMessage = history.shift();
history.unshift({
role: "user",
content: await humanMessageWithCodePrompt.format({
highlightedCode: highlighted.content,
question: firstMessage?.content,
}),
});
history.unshift({
role: "system",
content: systemTemplate,
});
}
}
if (configuration.get("cloud.use") && configuration.get("cloud.chat.use")) {
yield* await sendChatRequestCloud(history, parameters);
} else {
const prompt = getPromptChat(history);
yield* sendChatRequestLocal(prompt, parameters, loggerCompletion.uuid());
}
loggerCompletion.info("Request: finished");
} catch (error) {
const Error = error as Error;
Logger.error(error);
const errorMessage = Error.message;
vscode.window.showErrorMessage(errorMessage);
} finally {
stopTask();
}
}