diff --git a/server/app.mjs b/server/app.mjs index 09cbdc68..80019f30 100644 --- a/server/app.mjs +++ b/server/app.mjs @@ -50,11 +50,17 @@ export const startAppServer = ({dev, hostname, port, dir}) => { .catch((err) => { console.error('Error initializing GPTScript config:', err); reject(err); - return; }); } Promise.resolve(gptscriptInitPromise).then(() => { + setInterval(() => { + initGPTScriptConfig(gptscript) + .catch(err => { + console.error('Error updating GPTScript config:', err); + }); + }, 30 * 60 * 1000); // Pull the config from GitHub every 30 minutes + app.prepare().then(() => { const httpServer = createServer(handler); const io = new Server(httpServer); @@ -104,9 +110,9 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp prompt: true, confirm: true, }; - + if (tool) opts.subTool = tool; - + let state = {}; let statePath = ''; if (threadID) statePath = path.join(THREADS_DIR, threadID, STATE_FILE); @@ -178,7 +184,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp await runningScript.close(); runningScript = null; } - + // find the root tool and then add the new tool for (let block of script) { if (block.type === "tool") { @@ -188,7 +194,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp } } - /* + /* note(tylerslaton) this is a hacky way to add a tool to the chat state. When GPTScript does a run, it will @@ -203,7 +209,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp socket.emit("addingTool"); const currentState = JSON.parse(state.chatState); - + opts.chatState = undefined; // clear the chat state so we can get the new tool mappings const newStateRun = await gptscript.evaluate(script, opts) await newStateRun.text(); @@ -213,7 +219,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp opts.chatState = JSON.stringify(currentState); state.tools = [...new Set([...state.tools || [], tool])]; - + if (threadID) { fs.writeFile(statePath, JSON.stringify(state), (err) => { if (err) { @@ -230,7 +236,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp await runningScript.close(); runningScript = null; } - + // find the root tool and then remove the tool for (let block of script) { if (block.type === "tool") { @@ -241,7 +247,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp } } - /* + /* note(tylerslaton) this is a hacky way to remove a tool from the chat state. When GPTScript does a run, it will @@ -256,7 +262,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp socket.emit("removingTool"); const currentState = JSON.parse(state.chatState); - + opts.chatState = undefined; // clear the chat state so we can get the new tool mappings const newStateRun = await gptscript.evaluate(script, opts) await newStateRun.text(); @@ -266,7 +272,7 @@ const mount = async (location, tool, args, scriptWorkspace, socket, threadID, gp opts.chatState = JSON.stringify(currentState); state.tools = state.tools.filter(t => t !== tool); - + if (threadID) { fs.writeFile(statePath, JSON.stringify(state), (err) => { if (err) { @@ -356,48 +362,34 @@ const initGPTScriptConfig = async (gptscript) => { const configPath = gptscriptConfigPath(); - fs.readFile(configPath, 'utf8', (err, data) => { - if (err) { - console.error('Error reading config file:', err); - return; - } + let config; + try { + config = JSON.parse(fs.readFileSync(configPath, 'utf8')) + } catch (err) { + console.error('Error reading config file:', err); + return; + } - let config; - try { - config = JSON.parse(data); - } catch (parseErr) { - throw new Error(`Error parsing config file: ${parseErr}`); - } + const response = await fetch('https://raw.githubusercontent.com/gptscript-ai/gateway-config/main/config.json') + const defaultConfig = await response.json() - // Default values to add if they don't exist - const defaultConfig = { - gatewayURL: 'https://gateway-api.gptscript.ai', - integrations: { - microsoft365: 'microsoft365', - slack: 'slack', - notion: 'notion', - GitLab: 'gitlab' - } - }; - - // Update the config object with default values if they don't exist - config = { - ...defaultConfig, - ...config, - integrations: { - ...defaultConfig.integrations, - ...config.integrations - } - }; + // Update the config object with default values if they don't exist + config = { + ...defaultConfig, + ...config, + integrations: { + ...defaultConfig.integrations, + ...config.integrations + } + }; - // Write the updated config back to the file - fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8', (writeErr) => { - if (writeErr) { - console.error('Error writing config file:', writeErr); - return; - } - console.log('Config file updated successfully'); - }); + // Write the updated config back to the file + fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8', (writeErr) => { + if (writeErr) { + console.error('Error writing config file:', writeErr); + return; + } + console.log('Config file updated successfully'); }); }