-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: render diagrams from text with a yFiles diagram server (Chat…
…GPT plugin)
- Loading branch information
Showing
6 changed files
with
146 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
{ | ||
"name": "chatgpt-mattermost-bot", | ||
"version": "1.0.1", | ||
"version": "1.2.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node ./src/botservice.js" | ||
}, | ||
"dependencies": { | ||
"openai": "^3.2.1", | ||
"@mattermost/client": "^7.8.0", | ||
"babel-polyfill": "^6.26.0", | ||
"debug-level": "3.0.0", | ||
"form-data": "^4.0.0", | ||
"isomorphic-fetch": "^3.0.0", | ||
"ws": "^8.12.1", | ||
"debug-level": "3.0.0" | ||
"openai": "^3.2.1", | ||
"ws": "^8.12.1" | ||
}, | ||
"engines" : { | ||
"npm" : ">=8.0.0", | ||
"node" : ">=16.0.0" | ||
"engines": { | ||
"npm": ">=8.0.0", | ||
"node": ">=16.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const Client4 = require('@mattermost/client').Client4 | ||
const WebSocketClient = require('@mattermost/client').WebSocketClient | ||
const { Log } = require('debug-level') | ||
const log = new Log('bot') | ||
|
||
if (!global.WebSocket) { | ||
global.WebSocket = require('ws'); | ||
} | ||
|
||
const mattermostToken = process.env['MATTERMOST_TOKEN'] | ||
const matterMostURLString = process.env['MATTERMOST_URL'] | ||
|
||
const client = new Client4() | ||
client.setUrl(matterMostURLString) | ||
client.setToken(mattermostToken) | ||
|
||
const wsClient = new WebSocketClient(); | ||
let matterMostURL = new URL(matterMostURLString); | ||
const wsUrl = `${matterMostURL.protocol === 'https:' ? 'wss' : 'ws'}://${matterMostURL.host}/api/v4/websocket` | ||
|
||
new Promise((resolve, reject) => { | ||
wsClient.addCloseListener(connectFailCount => reject()) | ||
wsClient.addErrorListener(event => { reject(event) }) | ||
}).then(() => process.exit(0)).catch(reason => { log.error(reason); process.exit(-1)}) | ||
|
||
wsClient.initialize(wsUrl, mattermostToken) | ||
|
||
module.exports = { | ||
mmClient: client, | ||
wsClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { mmClient } = require('./mm-client') | ||
const FormData = require('form-data'); | ||
const { Log } = require('debug-level') | ||
const log = new Log('bot') | ||
|
||
const yFilesGPTServerUrl = process.env['YFILES_SERVER_URL'] | ||
const yFilesEndpoint = new URL('/json-to-svg', yFilesGPTServerUrl) | ||
|
||
/**\ | ||
* @param {string} content | ||
* @param {string} channelId | ||
* @returns {Promise<{message, fileId}>} | ||
*/ | ||
async function processGraphResponse (content, channelId) { | ||
const result = { | ||
message: content, | ||
} | ||
if (!yFilesGPTServerUrl) { | ||
return result | ||
} | ||
const replaceStart = content.match(/<graph>/i)?.index | ||
let replaceEnd = content.match(/<\/graph>/i)?.index | ||
if (replaceEnd) { | ||
replaceEnd += '</graph>'.length | ||
} | ||
if (replaceStart && replaceEnd) { | ||
const graphContent = content.substring(replaceStart, replaceEnd).replace(/<\/?graph>/gi, '').trim() | ||
|
||
try { | ||
const sanitized = JSON.parse(graphContent) | ||
const fileId = await jsonToFileId(JSON.stringify(sanitized), channelId) | ||
const pre = content.substring(0, replaceStart) | ||
const post = content.substring(replaceEnd) | ||
|
||
result.message = `${pre} [see attached image] ${post}` | ||
result.fileId = fileId | ||
} catch (e) { | ||
log.error(e) | ||
log.error(`The input was:\n\n${graphContent}`) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
async function generateSvg(jsonString) { | ||
return fetch(yFilesEndpoint, { | ||
method: 'POST', | ||
body: jsonString, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error("Bad response from server"); | ||
} | ||
return response.text(); | ||
}) | ||
} | ||
|
||
async function jsonToFileId (jsonString, channelId) { | ||
const svgString = await generateSvg(jsonString) | ||
const form = new FormData() | ||
form.append('channel_id', channelId); | ||
form.append('files', Buffer.from(svgString), 'diagram.svg'); | ||
const response = await mmClient.uploadFile(form) | ||
return response.file_infos[0].id | ||
} | ||
|
||
module.exports = { | ||
processGraphResponse | ||
} |