11import { app , shell , BrowserWindow } from 'electron' ;
2- import { getPort } from 'get-port-please' ;
32import { startAppServer } from '../server/app.mjs' ;
43import { join , dirname } from 'path' ;
54import { existsSync , mkdirSync , writeFileSync } from 'fs' ;
65import fixPath from 'fix-path' ;
76import os from 'os' ;
7+ import { config } from './config.mjs' ;
88
9- const appName = 'Acorn' ;
10- const gatewayUrl =
11- process . env . GPTSCRIPT_GATEWAY_URL || 'https://gateway-api.gptscript.ai' ;
12- const resourcesDir = dirname ( app . getAppPath ( ) ) ;
13- const dataDir = getDataDir ( appName ) ;
14-
15- function getDataDir ( appName ) {
16- const userDataPath = app . getPath ( 'userData' ) ;
17- return join ( userDataPath , appName ) ;
18- }
19-
20- function ensureDirExists ( dir ) {
21- if ( ! existsSync ( dir ) ) mkdirSync ( dir , { recursive : true } ) ;
22- }
9+ app . on ( 'window-all-closed' , ( ) => app . quit ( ) ) ;
10+ app . on ( 'ready' , ( ) => {
11+ startServer ( app . isPackaged ) ;
12+ } ) ;
2313
2414async function startServer ( isPackaged ) {
25- const port = isPackaged
26- ? await getPort ( { portRange : [ 30000 , 40000 ] } )
27- : process . env . PORT || 3000 ;
28- const gptscriptBin = join (
29- isPackaged ? join ( resourcesDir , 'app.asar.unpacked' ) : '' ,
30- 'node_modules' ,
31- '@gptscript-ai' ,
32- 'gptscript' ,
33- 'bin' ,
34- `gptscript${ process . platform === 'win32' ? '.exe' : '' } `
35- ) ;
36-
37- process . env . GPTSCRIPT_BIN = process . env . GPTSCRIPT_BIN || gptscriptBin ;
38- process . env . THREADS_DIR = process . env . THREADS_DIR || join ( dataDir , 'threads' ) ;
39- process . env . WORKSPACE_DIR =
40- process . env . WORKSPACE_DIR || join ( dataDir , 'workspace' ) ;
41- process . env . GPTSCRIPT_GATEWAY_URL =
42- process . env . GPTSCRIPT_GATEWAY_URL || gatewayUrl ;
43- process . env . GPTSCRIPT_OPENAPI_REVAMP = 'true' ;
15+ // Fix path so that tools can find binaries installed on the system.
16+ fixPath ( ) ;
4417
45- console . log (
46- `Starting app server with GPTSCRIPT_BIN="${ process . env . GPTSCRIPT_BIN } "`
47- ) ;
18+ // Ensure the app's data directory exists
19+ ensureDirExists ( config . dataDir ) ;
4820
4921 // Set up the browser tool to run in headless mode.
50- ensureDirExists ( process . env . WORKSPACE_DIR ) ;
22+ ensureDirExists ( config . workspaceDir ) ;
5123 writeFileSync (
52- `${ process . env . WORKSPACE_DIR } /browsersettings.json` ,
24+ `${ config . workspaceDir } /browsersettings.json` ,
5325 JSON . stringify ( { headless : true } )
5426 ) ;
5527
28+ // Project config onto environment variables to configure GPTScript/sdk-server and the Next.js app.
29+ process . env . GPTSCRIPT_BIN = config . gptscriptBin ;
30+ process . env . THREADS_DIR = config . threadsDir ;
31+ process . env . WORKSPACE_DIR = config . workspaceDir ;
32+ process . env . GPTSCRIPT_GATEWAY_URL = config . gatewayUrl ;
33+ process . env . GPTSCRIPT_OPENAPI_REVAMP = 'true' ;
34+
5635 try {
5736 const url = await startAppServer ( {
58- dev : ! isPackaged ,
37+ dev : config . dev ,
5938 hostname : 'localhost' ,
60- port,
61- dir : app . getAppPath ( ) ,
39+ port : config . port ,
40+ appDir : config . appDir ,
6241 } ) ;
6342 console . log ( `> ${ isPackaged ? '' : 'Dev ' } Electron app started at ${ url } ` ) ;
6443 createWindow ( url ) ;
@@ -73,9 +52,9 @@ function createWindow(url) {
7352 const win = new BrowserWindow ( {
7453 width : 1024 ,
7554 height : 720 ,
76- frame : isMac ? false : true , // Use frame: true for Windows and Linux
55+ frame : ! isMac ,
7756 webPreferences : {
78- preload : join ( app . getAppPath ( ) , 'electron/preload.js ' ) ,
57+ preload : join ( config . appDir , 'electron/preload.mjs ' ) ,
7958 nodeIntegration : true ,
8059 allowRunningInsecureContent : true ,
8160 webSecurity : false ,
@@ -88,7 +67,7 @@ function createWindow(url) {
8867 win . setWindowButtonVisibility ( true ) ;
8968 }
9069
91- // Open the NextJS app
70+ // Open the Next.js app
9271 win . loadURL ( url ) ;
9372
9473 win . webContents . on ( 'did-fail-load' , ( ) =>
@@ -108,10 +87,6 @@ function createWindow(url) {
10887 } ) ;
10988}
11089
111- app . on ( 'ready' , ( ) => {
112- fixPath ( ) ;
113- ensureDirExists ( dataDir ) ;
114- startServer ( app . isPackaged ) ;
115- } ) ;
116-
117- app . on ( 'window-all-closed' , ( ) => app . quit ( ) ) ;
90+ function ensureDirExists ( dir ) {
91+ if ( ! existsSync ( dir ) ) mkdirSync ( dir , { recursive : true } ) ;
92+ }
0 commit comments