-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammargpt.js
130 lines (112 loc) · 3.26 KB
/
grammargpt.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.actions = exports.getErrorInfo = void 0;
const axios_1 = require("axios");
const messages = [];
let defalutPrompt =
"You will be provided with statements, and your task is to convert them to standard English.";
function isEmpty(str) {
return !str || str.length === 0;
}
// if option is empty, return default value
function optionOrDefault(str, defalutValue) {
if (isEmpty(str) == true) {
return defalutValue;
} else {
return str;
}
}
function wait(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
// get the content of the last `n` messages from the chat, trimmed and separated by double newlines
function getTranscript(n) {
return messages
.slice(-n)
.map((m) => m.content.trim())
.join("\n\n");
}
// the main action
const action = async (input, options) => {
messages.length = 0;
if (isEmpty(input.text)) {
popclip.showText("⚠️ No input text");
return;
}
print("input.text: " + input.text);
// if options baseurl not empty using this or openai official api url
const baseUrl = optionOrDefault(options.baseurl, "https://api.openai.com/v1");
const openai = axios_1.default.create({
baseURL: baseUrl,
headers: { Authorization: `Bearer ${options.apikey}` },
});
messages.push({
role: "system",
content: optionOrDefault(options.prompt, defalutPrompt),
});
const inputText = input.text.trim();
const quotedText =
(!inputText.startsWith("$") ? "$" : "") +
inputText +
(!inputText.endsWith("$") ? "$" : "");
messages.push({ role: "user", content: quotedText });
print(messages);
// send the whole message history to OpenAI
try {
const { data } = await openai.post("chat/completions", {
model: options.model || "gpt-4o-mini",
messages,
temperature: 0,
});
// add the response to the history
messages.push(data.choices[0].message);
// if holding shift, copy just the response.
// else, show correction result.
popclip.showText(getTranscript(1));
switch (options.didGetResponse) {
case "Paste":
popclip.pasteText(getTranscript(1));
break;
case "Append":
popclip.pasteText(input.text + "\n\n" + getTranscript(1) + "\n");
break;
case "Copy":
popclip.copyText(getTranscript(1));
break;
default:
break;
}
} catch (e) {
popclip.showText(getErrorInfo(e));
}
};
function getErrorInfo(error) {
if (typeof error === "object" && error !== null && "response" in error) {
const response = error.response;
//return JSON.stringify(response);
return `Message from OpenAI (code ${response.status}): ${response.data.error.message}`;
} else {
return String(error);
}
}
const pastLastResult = async (input, options) => {
if (isEmpty(getTranscript(1))) {
popclip.showText("⚠️ No previous result");
} else {
popclip.pasteText(getTranscript(1));
}
};
exports.getErrorInfo = getErrorInfo;
// export the actions
exports.actions = [
{
title: "GrammarGPT: Correction",
code: action,
},
{
title: "GrammarGPT: Paste last",
icon: "icon-paste.svg",
code: pastLastResult,
requirements: ["option-showPaste=1"],
},
];