Skip to content

Commit

Permalink
fix: add hasInited and filter USER_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Sep 29, 2024
1 parent 6b46c6f commit dd7ad7b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/lib/run-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path'
import colors from 'ansi-colors'
// import cliSpinners from 'cli-spinners'
import _logUpdate from 'log-update'
import { get as getByPath } from 'lodash-es'
import { get as getByPath, omitBy } from 'lodash-es'
import {
ConfigFile,
countRegexMatches,
Expand Down Expand Up @@ -170,6 +170,8 @@ function renameOldFile(filename: string, backupChat?: boolean) {
}
}
}

let hasInited = false
export async function runScript(filename: string, options: IRunScriptOptions) {
// initTools(options)

Expand Down Expand Up @@ -199,20 +201,27 @@ export async function runScript(filename: string, options: IRunScriptOptions) {

let script
const aborter = new AbortController()
process.once('SIGINT', () => {
aborter.abort()
})
if (!hasInited) {
process.once('SIGINT', () => {
aborter.abort()
})

process.once('exit', function () {
aborter.abort()
})
process.once('exit', function () {
aborter.abort()
})
}

try {
const USER_ENV = omitBy(options, (v, k) => {
const vT = typeof v
return k?.[0] === '_' || v == null || vT === 'function' || (vT === 'object' && !Array.isArray(v)) || vT === 'symbol'
})

script = await AIScriptEx.loadFile(filename,
{chatsDir: options.chatsDir},
{
ABORT_SEARCH_SCRIPTS_SIGNAL: aborter.signal,
USER_ENV: options,
USER_ENV,
FUNC_SCOPE: {
expandPath: function(path: string) {
// for formatting
Expand Down Expand Up @@ -277,8 +286,11 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
console.log('quit for interrupted.')
process.exit(0)
}
process.once('SIGINT', interrupted)
process.once('beforeExit', saveChatHistory)

if (!hasInited) {
process.once('SIGINT', interrupted)
process.once('beforeExit', saveChatHistory)
}

let llmLastContent = ''
let retryCount = 0
Expand Down Expand Up @@ -330,6 +342,8 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
})
}

hasInited = true

let lastError: any
try {
if (aiPreferredLanguage && options.data) {
Expand Down

0 comments on commit dd7ad7b

Please sign in to comment.