Skip to content

Commit

Permalink
feat: add maxSize to file and url rt lib
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Nov 1, 2024
1 parent 3343b35 commit 252564b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/file.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/load-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://')) {
Expand All @@ -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}`
}
Expand Down
8 changes: 5 additions & 3 deletions lib/load-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions lib/url.ai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/lib/run-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 252564b

Please sign in to comment.