Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix app.mjs panic and create settings.json on startup #481

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions actions/appSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use server';

import fs from 'fs';
import os from 'os';
import path from 'path';

export type AppSettings = {
confirmToolCalls: boolean;
Expand All @@ -19,7 +17,7 @@ const defaultAppSettings: AppSettings = {
browser: {
headless: false,
useDefaultSession: false,
},
} as BrowserAppSettings,
};

export async function getAppSettings() {
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions app/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function SettingsPage() {
onValueChange={(isSelected) => {
setPendingSettings((prevState) => ({
...prevState,
headless: isSelected,
browser: { ...prevState.browser, headless: isSelected },
}));
}}
>
Expand All @@ -89,7 +89,10 @@ export default function SettingsPage() {
onValueChange={(isSelected) => {
setPendingSettings((prevState) => ({
...prevState,
useDefaultSession: isSelected,
browser: {
...prevState.browser,
useDefaultSession: isSelected,
},
}));
}}
>
Expand Down
20 changes: 13 additions & 7 deletions electron/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this comment is out of date?

Suggested change
config.appSettingsFile, // created in config.mjs
config.appSettingsFile,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its new, I added it just now. What's wrong with it?

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;
Expand Down
18 changes: 16 additions & 2 deletions server/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: probably more safe to use default setting when file exists and parsing is correct, otherwise fallback to default file. In case the setting file is corrupted...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StrongMonkey Can you take another look? I added some things to defend against malformed files.

try {
settings = JSON.parse(fs.readFileSync(settingsLocation, 'utf8'));
} catch {
console.error('Malformed settings file, using default settings...');
}
}

let script;
if (typeof location === 'string') {
Expand Down
Loading