From 252564bf1e1660c5371653d514e96ae413297693 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Fri, 1 Nov 2024 09:34:37 +0800 Subject: [PATCH] feat: add maxSize to file and url rt lib --- lib/file.ai.yaml | 5 +++-- lib/load-file.js | 4 +++- lib/load-url.js | 8 +++++--- lib/url.ai.yaml | 4 ++-- src/lib/run-script.ts | 3 ++- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/file.ai.yaml b/lib/file.ai.yaml index 090f5e8..ccd6819 100644 --- a/lib/file.ai.yaml +++ b/lib/file.ai.yaml @@ -7,12 +7,13 @@ description: |- It can be used in prompt. eg, `user: "think about the following file content: @file(document.md)"` tag: - file + - url - loader - prompt - lib input: - - 0 # The file path passed by the prompt(position argument) - - content # The file path passed by the prompt + - content: {index: 0} # The file path passed by the prompt, index: the position argument + - maxSize: {type: "number"} # truncate the file content to this size if exists - sslVerify: {type: "boolean"} - onlyContent: {type: "boolean"} # Only return the content output: # the file content diff --git a/lib/load-file.js b/lib/load-file.js index 23ba82b..0251794 100644 --- a/lib/load-file.js +++ b/lib/load-file.js @@ -3,9 +3,10 @@ import path from 'path' import { expandPath } from '@offline-ai/cli-common' import { loadUrl } from './load-url.js' export async function loadFile(filepath, onlyContent) { - filepath = filepath ? filepath : this.content || this[0] + filepath = filepath ? filepath : this.content if (!filepath) {throw new Error('No file path provided.')} onlyContent = typeof onlyContent === 'boolean' ? onlyContent : this.onlyContent + const maxSize = this.maxSize let content if (filepath.startsWith('https://') || filepath.startsWith('http://')) { @@ -17,6 +18,7 @@ export async function loadFile(filepath, onlyContent) { const filename = path.basename(filepath); filepath = expandPath(filepath) content = fs.readFileSync(filepath, 'utf8'); + if (maxSize > 0) {content = content.slice(0, maxSize)} if (!onlyContent) { content = `filename: ${filename}\nfile content:\n${content}` } diff --git a/lib/load-url.js b/lib/load-url.js index 4a8a96c..6e74be8 100644 --- a/lib/load-url.js +++ b/lib/load-url.js @@ -9,7 +9,9 @@ export async function loadUrl(url, onlyContent, sslVerify) { const agent = new https.Agent({ rejectUnauthorized: false }) options.agent = agent } - const content = await fetch(url, options).then(res => res.text()) - if (onlyContent) return content; - return `web url: ${url}\nweb content:\n${content}` + let content = await fetch(url, options).then(res => res.text()) + const maxSize = this.maxSize + if (maxSize > 0) {content = content.slice(0, maxSize)} + if (!onlyContent) {content = `web url: ${url}\nweb content:\n${content}`;} + return content } diff --git a/lib/url.ai.yaml b/lib/url.ai.yaml index 64d4559..26069ab 100644 --- a/lib/url.ai.yaml +++ b/lib/url.ai.yaml @@ -11,8 +11,8 @@ tag: - prompt - lib input: - - 0 # The url passed by the prompt(position argument) - - content # The url passed by the prompt + - content: {index: 0} # The url passed by the prompt, index: the position argument + - maxSize: {type: "number"} # truncate the content to this size if exists - sslVerify: {type: "boolean"} - onlyContent: {type: "boolean"} # Only return the content output: # the web page content diff --git a/src/lib/run-script.ts b/src/lib/run-script.ts index 2adff99..d98ce2b 100644 --- a/src/lib/run-script.ts +++ b/src/lib/run-script.ts @@ -204,6 +204,7 @@ export async function runScript(filename: string, options: IRunScriptOptions) { const aborter = new AbortController() if (!hasInited) { process.once('SIGINT', () => { + console.log('🚀 ~ process.once ~ SIGINT!') aborter.abort() }) @@ -304,7 +305,7 @@ export async function runScript(filename: string, options: IRunScriptOptions) { if (quit) { runtime.abortTool('quit') - process.emit('SIGINT') + process.exit() } if (options.streamEcho === false) {return}