|
1 |
| -import './fetch-polyfill.js' |
2 |
| -import {Options} from './options.js' |
3 |
| -import * as core from '@actions/core' |
4 |
| -import { |
5 |
| - ChatGPTAPI, |
6 |
| - ChatGPTUnofficialProxyAPI, |
7 |
| - ChatMessage, |
8 |
| - SendMessageBrowserOptions, |
9 |
| - SendMessageOptions |
10 |
| -} from 'chatgpt' |
| 1 | +import "./fetch-polyfill.js"; |
| 2 | +import { Options } from "./options.js"; |
| 3 | +import * as core from "@actions/core"; |
| 4 | +import { ChatGPTAPI, ChatMessage, SendMessageOptions } from "chatgpt"; |
11 | 5 |
|
12 | 6 | // define type to save parentMessageId and conversationId
|
13 | 7 | export type Ids = {
|
14 |
| - parentMessageId?: string |
15 |
| - conversationId?: string |
16 |
| -} |
| 8 | + parentMessageId?: string; |
| 9 | + conversationId?: string; |
| 10 | +}; |
17 | 11 |
|
18 | 12 | export class Bot {
|
19 |
| - private bot: ChatGPTUnofficialProxyAPI | null = null // free |
20 |
| - private turbo: ChatGPTAPI | null = null // not free |
| 13 | + private turbo: ChatGPTAPI | null = null; // not free |
21 | 14 |
|
22 |
| - private options: Options |
| 15 | + private options: Options; |
23 | 16 |
|
24 | 17 | constructor(options: Options) {
|
25 |
| - this.options = options |
26 |
| - if (process.env.CHATGPT_ACCESS_TOKEN) { |
27 |
| - this.bot = new ChatGPTUnofficialProxyAPI({ |
28 |
| - accessToken: process.env.CHATGPT_ACCESS_TOKEN, |
29 |
| - apiReverseProxyUrl: options.chatgpt_reverse_proxy, |
30 |
| - debug: options.debug |
31 |
| - }) |
32 |
| - } else if (process.env.OPENAI_API_KEY) { |
| 18 | + this.options = options; |
| 19 | + if (process.env.OPENAI_API_KEY) { |
33 | 20 | this.turbo = new ChatGPTAPI({
|
34 | 21 | systemMessage: options.system_message,
|
35 | 22 | apiKey: process.env.OPENAI_API_KEY,
|
36 | 23 | debug: options.debug,
|
37 | 24 | completionParams: {
|
38 |
| - temperature: options.temperature |
39 |
| - } |
| 25 | + temperature: options.temperature, |
| 26 | + }, |
40 | 27 | // assistantLabel: " ",
|
41 | 28 | // userLabel: " ",
|
42 |
| - }) |
| 29 | + }); |
43 | 30 | } else {
|
44 | 31 | const err =
|
45 |
| - "Unable to initialize the chatgpt API, both 'CHATGPT_ACCESS_TOKEN' " + |
46 |
| - "and 'OPENAI_API_KEY' environment variable are not available" |
47 |
| - throw new Error(err) |
| 32 | + "Unable to initialize the chatgpt API, both 'OPENAI_API_KEY' environment variable are not available"; |
| 33 | + throw new Error(err); |
48 | 34 | }
|
49 | 35 | }
|
50 | 36 |
|
51 | 37 | chat = async (message: string, ids: Ids): Promise<[string, Ids]> => {
|
52 |
| - let new_ids: Ids = {} |
53 |
| - let response = '' |
| 38 | + let new_ids: Ids = {}; |
| 39 | + let response = ""; |
54 | 40 | try {
|
55 |
| - ;[response, new_ids] = await this.chat_(message, ids) |
| 41 | + [response, new_ids] = await this.chat_(message, ids); |
56 | 42 | } catch (e: any) {
|
57 |
| - core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`) |
| 43 | + core.warning(`Failed to chat: ${e}, backtrace: ${e.stack}`); |
58 | 44 | } finally {
|
59 |
| - return [response, new_ids] |
| 45 | + return [response, new_ids]; |
60 | 46 | }
|
61 |
| - } |
| 47 | + }; |
62 | 48 |
|
63 | 49 | private chat_ = async (message: string, ids: Ids): Promise<[string, Ids]> => {
|
64 | 50 | if (!message) {
|
65 |
| - return ['', {}] |
| 51 | + return ["", {}]; |
66 | 52 | }
|
67 | 53 | if (this.options.debug) {
|
68 |
| - core.info(`sending to chatgpt: ${message}`) |
| 54 | + core.info(`sending to chatgpt: ${message}`); |
69 | 55 | }
|
70 | 56 |
|
71 |
| - let response: ChatMessage | null = null |
72 |
| - if (this.bot) { |
73 |
| - let opts: SendMessageBrowserOptions = {} |
74 |
| - if (ids.parentMessageId && ids.conversationId) { |
75 |
| - opts.parentMessageId = ids.parentMessageId |
76 |
| - opts.conversationId = ids.conversationId |
77 |
| - } |
78 |
| - core.info('opts: ' + JSON.stringify(opts)) |
79 |
| - response = await this.bot.sendMessage(message, opts) |
80 |
| - try { |
81 |
| - core.info(`response: ${JSON.stringify(response)}`) |
82 |
| - } catch (e: any) { |
83 |
| - core.info( |
84 |
| - `response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}` |
85 |
| - ) |
86 |
| - } |
87 |
| - } else if (this.turbo) { |
88 |
| - let opts: SendMessageOptions = {} |
| 57 | + let response: ChatMessage | null = null; |
| 58 | + if (this.turbo) { |
| 59 | + let opts: SendMessageOptions = {}; |
89 | 60 | if (ids.parentMessageId) {
|
90 |
| - opts.parentMessageId = ids.parentMessageId |
| 61 | + opts.parentMessageId = ids.parentMessageId; |
91 | 62 | }
|
92 |
| - response = await this.turbo.sendMessage(message, opts) |
| 63 | + response = await this.turbo.sendMessage(message, opts); |
93 | 64 | try {
|
94 |
| - core.info(`response: ${JSON.stringify(response)}`) |
| 65 | + core.info(`response: ${JSON.stringify(response)}`); |
95 | 66 | } catch (e: any) {
|
96 | 67 | core.info(
|
97 |
| - `response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}` |
98 |
| - ) |
| 68 | + `response: ${response}, failed to stringify: ${e}, backtrace: ${e.stack}`, |
| 69 | + ); |
99 | 70 | }
|
100 | 71 | } else {
|
101 |
| - core.setFailed('The chatgpt API is not initialized') |
| 72 | + core.setFailed("The chatgpt API is not initialized"); |
102 | 73 | }
|
103 |
| - let response_text = '' |
| 74 | + let response_text = ""; |
104 | 75 | if (response) {
|
105 |
| - response_text = response.text |
| 76 | + response_text = response.text; |
106 | 77 | } else {
|
107 |
| - core.warning('chatgpt response is null') |
| 78 | + core.warning("chatgpt response is null"); |
108 | 79 | }
|
109 | 80 | // remove the prefix "with " in the response
|
110 |
| - if (response_text.startsWith('with ')) { |
111 |
| - response_text = response_text.substring(5) |
| 81 | + if (response_text.startsWith("with ")) { |
| 82 | + response_text = response_text.substring(5); |
112 | 83 | }
|
113 | 84 | if (this.options.debug) {
|
114 |
| - core.info(`chatgpt responses: ${response_text}`) |
| 85 | + core.info(`chatgpt responses: ${response_text}`); |
115 | 86 | }
|
116 | 87 | const new_ids: Ids = {
|
117 | 88 | parentMessageId: response?.id,
|
118 |
| - conversationId: response?.conversationId |
119 |
| - } |
120 |
| - return [response_text, new_ids] |
121 |
| - } |
| 89 | + conversationId: response?.conversationId, |
| 90 | + }; |
| 91 | + return [response_text, new_ids]; |
| 92 | + }; |
122 | 93 | }
|
0 commit comments