Skip to content

Commit

Permalink
🔧 refactor(Dockerfile, package.json, botservice.ts, mm-client.ts, Ima…
Browse files Browse the repository at this point in the history
…gePlugin.ts, PluginBase.ts, vite.config.ts): package updates and code improvements

In Dockerfile, updated the node version from 16 to 20. This allows us to use the latest features and security patches.

In package.json, updated several packages and added "node-fetch". This allows us to use the latest features and improves the stability and performance of the code.

In botservice.ts, mm-client.ts, ImagePlugin.ts, PluginBase.ts, made several improvements to improve the readability and maintainability of the code.
  • Loading branch information
takuya-o committed Aug 16, 2023
1 parent fde42d4 commit faf00a1
Show file tree
Hide file tree
Showing 9 changed files with 1,383 additions and 250 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NPM builder image
FROM node:16-slim as npm_builder
FROM node:20-slim as npm_builder
#20.3.0-bookworm-slim (Debian 12)

WORKDIR /app
Expand All @@ -17,7 +17,7 @@ RUN rm -rf node_modules/ && npm ci --omit dev


# NPM runtime image
FROM node:16-slim as npm_runtime
FROM node:20-slim as npm_runtime

WORKDIR /app

Expand Down
25 changes: 14 additions & 11 deletions dist/botservice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,21 @@ async function createImage(prompt) {
}

// src/mm-client.ts
import Log2 from "debug-level";
import { WebSocket } from "ws";
import fetch from "node-fetch";
import pkg from "@mattermost/client";
var { Client4, WebSocketClient } = pkg;
if (!global.WebSocket) {
global.WebSocket = WebSocket;
}
var { Client4, WebSocketClient } = pkg;
var log = new Log2("bot");
global.fetch = fetch;
var mattermostToken = process.env["MATTERMOST_TOKEN"];
var matterMostURLString = process.env["MATTERMOST_URL"];
if (!mattermostToken || !matterMostURLString) {
log.error("MATTERMOST_TOKEN or MATTERMOST_URL is undefined");
botLog.error("MATTERMOST_TOKEN or MATTERMOST_URL is undefined");
throw new Error("MATTERMOST_TOKEN or MATTERMOST_URL is undefined");
}
log.trace("Configuring Mattermost URL to " + matterMostURLString);
botLog.trace("Configuring Mattermost URL to " + matterMostURLString);
var mmClient = new Client4();
mmClient.setUrl(matterMostURLString);
mmClient.setToken(mattermostToken);
Expand All @@ -188,7 +188,7 @@ new Promise((_resolve, reject) => {
}).then(() => {
process.exit(0);
}).catch((reason) => {
log.error(reason);
botLog.error(reason);
process.exit(-1);
});
function workaroundWebsocketPackageLostIssue(webSocketClient) {
Expand All @@ -204,13 +204,12 @@ workaroundWebsocketPackageLostIssue(wsClient);
wsClient.initialize(wsUrl.toString(), mattermostToken);

// src/plugins/PluginBase.ts
import { Log as Log3 } from "debug-level";
var PluginBase = class {
constructor(key, description) {
this.key = key;
this.description = description;
}
log = new Log3("bot");
log = botLog;
pluginArguments = {};
requiredArguments = [];
setup() {
Expand Down Expand Up @@ -241,7 +240,7 @@ import FormData3 from "form-data";
// src/plugins/GraphPlugin.ts
import { ChatCompletionRequestMessageRoleEnum } from "openai";
import FormData from "form-data";
import fetch from "node-fetch";
import fetch2 from "node-fetch";
var GraphPlugin = class extends PluginBase {
yFilesGPTServerUrl = process.env["YFILES_SERVER_URL"];
yFilesEndpoint = this.yFilesGPTServerUrl ? new URL("/json-to-svg", this.yFilesGPTServerUrl) : void 0;
Expand Down Expand Up @@ -311,7 +310,7 @@ ${graphContent}`);
return result;
}
async generateSvg(jsonString) {
return fetch(this.yFilesEndpoint, {
return fetch2(this.yFilesEndpoint, {
method: "POST",
body: jsonString,
headers: {
Expand Down Expand Up @@ -356,7 +355,11 @@ var ImagePlugin = class extends PluginBase {
const imagePrompt = await this.createImagePrompt(args.imageDescription);
if (imagePrompt) {
this.log.trace({ imageInputPrompt: args.imageDescription, imageOutputPrompt: imagePrompt });
const base64Image = await createImage(imagePrompt);
const base64Image = (
/*this.img256 //*/
/*this.sampleB64String */
await createImage(imagePrompt)
);
if (base64Image) {
const fileId = await this.base64ToFile(base64Image, msgData.post.channel_id);
aiResponse.message = "Here is the image you requested: " + imagePrompt;
Expand Down
Loading

0 comments on commit faf00a1

Please sign in to comment.