diff --git a/actions/appSettings.tsx b/actions/appSettings.tsx index 98b46c5..9d7486e 100644 --- a/actions/appSettings.tsx +++ b/actions/appSettings.tsx @@ -1,8 +1,6 @@ 'use server'; import fs from 'fs'; -import os from 'os'; -import path from 'path'; export type AppSettings = { confirmToolCalls: boolean; @@ -19,7 +17,7 @@ const defaultAppSettings: AppSettings = { browser: { headless: false, useDefaultSession: false, - }, + } as BrowserAppSettings, }; export async function getAppSettings() { @@ -30,7 +28,11 @@ export async function getAppSettings() { const location = process.env.GPTSCRIPT_SETTINGS_FILE; if (fs.existsSync(location)) { const AppSettings = fs.readFileSync(location, 'utf-8'); - return JSON.parse(AppSettings) as AppSettings; + try { + return JSON.parse(AppSettings) as AppSettings; + } catch { + console.error('Malformed settings file, using default settings...'); + } } return defaultAppSettings; } diff --git a/app/settings/page.tsx b/app/settings/page.tsx index 3a84a90..8c2d786 100644 --- a/app/settings/page.tsx +++ b/app/settings/page.tsx @@ -69,7 +69,7 @@ export default function SettingsPage() { onValueChange={(isSelected) => { setPendingSettings((prevState) => ({ ...prevState, - headless: isSelected, + browser: { ...prevState.browser, headless: isSelected }, })); }} > @@ -89,7 +89,10 @@ export default function SettingsPage() { onValueChange={(isSelected) => { setPendingSettings((prevState) => ({ ...prevState, - useDefaultSession: isSelected, + browser: { + ...prevState.browser, + useDefaultSession: isSelected, + }, })); }} > diff --git a/electron/main.mjs b/electron/main.mjs index cbb45a7..66b2917 100644 --- a/electron/main.mjs +++ b/electron/main.mjs @@ -13,15 +13,21 @@ async function startServer() { // Fix path so that tools can find binaries installed on the system. fixPath(); - // Ensure the app's data directory exists + // Ensure the app's data and workspace directories exists ensureDirExists(config.dataDir); - - // Set up the browser tool to run in headless mode. ensureDirExists(config.workspaceDir); - writeFileSync( - join(`${config.workspaceDir}`, 'settings.json'), - JSON.stringify({ headless: true }) - ); + + // If the app settings file doesn't exist, create it with default settings. + if (!existsSync(config.appSettingsFile)) { + writeFileSync( + config.appSettingsFile, // created in config.mjs + JSON.stringify({ + browser: { + headless: true, + }, + }) + ); + } // Project config onto environment variables to configure GPTScript/sdk-server and the Next.js app. process.env.LOGS_DIR = config.logsDir; diff --git a/server/app.mjs b/server/app.mjs index b069c95..3bbe0e7 100644 --- a/server/app.mjs +++ b/server/app.mjs @@ -4,7 +4,7 @@ import nextConfig from '../next.config.js'; import { Server } from 'socket.io'; import { GPTScript, RunEventType } from '@gptscript-ai/gptscript'; import dotenv from 'dotenv'; -import fs from 'fs'; +import fs, { existsSync } from 'fs'; import path from 'path'; import os from 'os'; @@ -135,8 +135,22 @@ const mount = async ( process.env.WORKSPACE_DIR ?? process.env.GPTSCRIPT_WORKSPACE_DIR; const THREADS_DIR = process.env.THREADS_DIR ?? path.join(WORKSPACE_DIR, 'threads'); + + // Get the settings file. If it doesn't exist, use default settings instead. const settingsLocation = process.env.GPTSCRIPT_SETTINGS_FILE; - const settings = JSON.parse(fs.readFileSync(settingsLocation, 'utf8')); + let settings = { + confirmToolCalls: false, + browser: { + headless: true, + }, + }; + if (existsSync(settingsLocation)) { + try { + settings = JSON.parse(fs.readFileSync(settingsLocation, 'utf8')); + } catch { + console.error('Malformed settings file, using default settings...'); + } + } let script; if (typeof location === 'string') {