Skip to content

Commit dd7ad7b

Browse files
committed
fix: add hasInited and filter USER_ENV
1 parent 6b46c6f commit dd7ad7b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/lib/run-script.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from 'path'
77
import colors from 'ansi-colors'
88
// import cliSpinners from 'cli-spinners'
99
import _logUpdate from 'log-update'
10-
import { get as getByPath } from 'lodash-es'
10+
import { get as getByPath, omitBy } from 'lodash-es'
1111
import {
1212
ConfigFile,
1313
countRegexMatches,
@@ -170,6 +170,8 @@ function renameOldFile(filename: string, backupChat?: boolean) {
170170
}
171171
}
172172
}
173+
174+
let hasInited = false
173175
export async function runScript(filename: string, options: IRunScriptOptions) {
174176
// initTools(options)
175177

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

200202
let script
201203
const aborter = new AbortController()
202-
process.once('SIGINT', () => {
203-
aborter.abort()
204-
})
204+
if (!hasInited) {
205+
process.once('SIGINT', () => {
206+
aborter.abort()
207+
})
205208

206-
process.once('exit', function () {
207-
aborter.abort()
208-
})
209+
process.once('exit', function () {
210+
aborter.abort()
211+
})
212+
}
209213

210214
try {
215+
const USER_ENV = omitBy(options, (v, k) => {
216+
const vT = typeof v
217+
return k?.[0] === '_' || v == null || vT === 'function' || (vT === 'object' && !Array.isArray(v)) || vT === 'symbol'
218+
})
219+
211220
script = await AIScriptEx.loadFile(filename,
212221
{chatsDir: options.chatsDir},
213222
{
214223
ABORT_SEARCH_SCRIPTS_SIGNAL: aborter.signal,
215-
USER_ENV: options,
224+
USER_ENV,
216225
FUNC_SCOPE: {
217226
expandPath: function(path: string) {
218227
// for formatting
@@ -277,8 +286,11 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
277286
console.log('quit for interrupted.')
278287
process.exit(0)
279288
}
280-
process.once('SIGINT', interrupted)
281-
process.once('beforeExit', saveChatHistory)
289+
290+
if (!hasInited) {
291+
process.once('SIGINT', interrupted)
292+
process.once('beforeExit', saveChatHistory)
293+
}
282294

283295
let llmLastContent = ''
284296
let retryCount = 0
@@ -330,6 +342,8 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
330342
})
331343
}
332344

345+
hasInited = true
346+
333347
let lastError: any
334348
try {
335349
if (aiPreferredLanguage && options.data) {

0 commit comments

Comments
 (0)