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

Commit

Permalink
refactor fetch-pollyfill (#13)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harjotgill committed Mar 12, 2023
1 parent df60b03 commit 6dafc50
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
18 changes: 9 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./fetch-polyfill.js";
import { Options } from "./options.js";
import * as core from "@actions/core";
import { ChatGPTAPI, ChatMessage, SendMessageOptions } from "chatgpt";
import * as chatgpt from "chatgpt";
import "./fetch-polyfill.js";
import * as optionsJs from "./options.js";

// define type to save parentMessageId and conversationId
export type Ids = {
Expand All @@ -10,14 +10,14 @@ export type Ids = {
};

export class Bot {
private turbo: ChatGPTAPI | null = null; // not free
private turbo: chatgpt.ChatGPTAPI | null = null; // not free

private options: Options;
private options: optionsJs.Options;

constructor(options: Options) {
constructor(options: optionsJs.Options) {
this.options = options;
if (process.env.OPENAI_API_KEY) {
this.turbo = new ChatGPTAPI({
this.turbo = new chatgpt.ChatGPTAPI({
systemMessage: options.system_message,
apiKey: process.env.OPENAI_API_KEY,
debug: options.debug,
Expand Down Expand Up @@ -54,9 +54,9 @@ export class Bot {
core.info(`sending to chatgpt: ${message}`);
}

let response: ChatMessage | null = null;
let response: chatgpt.ChatMessage | null = null;
if (this.turbo) {
let opts: SendMessageOptions = {};
let opts: chatgpt.SendMessageOptions = {};
if (ids.parentMessageId) {
opts.parentMessageId = ids.parentMessageId;
}
Expand Down
19 changes: 0 additions & 19 deletions src/fetch-polyfill.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/fetch-polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fetch, * as nodeFetch from "node-fetch";

if (!globalThis.fetch) {
globalThis.fetch = fetch;
globalThis.Headers = nodeFetch.Headers;
globalThis.Request = nodeFetch.Request;
globalThis.Response = nodeFetch.Response;
}

0 comments on commit 6dafc50

Please sign in to comment.