Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 6dafc50

Browse files
authored
refactor fetch-pollyfill (#13)
This pull request includes updates to imports and types from `chatgpt` and `options.js`, removes import of `node-fetch`, and sets global variables for `fetch`, `Headers`, `Request`, and `Response`. No new features were added, but these changes improve the organization and maintainability of the code.
1 parent df60b03 commit 6dafc50

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

src/bot.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import "./fetch-polyfill.js";
2-
import { Options } from "./options.js";
31
import * as core from "@actions/core";
4-
import { ChatGPTAPI, ChatMessage, SendMessageOptions } from "chatgpt";
2+
import * as chatgpt from "chatgpt";
3+
import "./fetch-polyfill.js";
4+
import * as optionsJs from "./options.js";
55

66
// define type to save parentMessageId and conversationId
77
export type Ids = {
@@ -10,14 +10,14 @@ export type Ids = {
1010
};
1111

1212
export class Bot {
13-
private turbo: ChatGPTAPI | null = null; // not free
13+
private turbo: chatgpt.ChatGPTAPI | null = null; // not free
1414

15-
private options: Options;
15+
private options: optionsJs.Options;
1616

17-
constructor(options: Options) {
17+
constructor(options: optionsJs.Options) {
1818
this.options = options;
1919
if (process.env.OPENAI_API_KEY) {
20-
this.turbo = new ChatGPTAPI({
20+
this.turbo = new chatgpt.ChatGPTAPI({
2121
systemMessage: options.system_message,
2222
apiKey: process.env.OPENAI_API_KEY,
2323
debug: options.debug,
@@ -54,9 +54,9 @@ export class Bot {
5454
core.info(`sending to chatgpt: ${message}`);
5555
}
5656

57-
let response: ChatMessage | null = null;
57+
let response: chatgpt.ChatMessage | null = null;
5858
if (this.turbo) {
59-
let opts: SendMessageOptions = {};
59+
let opts: chatgpt.SendMessageOptions = {};
6060
if (ids.parentMessageId) {
6161
opts.parentMessageId = ids.parentMessageId;
6262
}

src/fetch-polyfill.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/fetch-polyfill.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import fetch, * as nodeFetch from "node-fetch";
2+
3+
if (!globalThis.fetch) {
4+
globalThis.fetch = fetch;
5+
globalThis.Headers = nodeFetch.Headers;
6+
globalThis.Request = nodeFetch.Request;
7+
globalThis.Response = nodeFetch.Response;
8+
}

0 commit comments

Comments
 (0)