Skip to content

Commit

Permalink
feat(lib): add url supports to file lib
Browse files Browse the repository at this point in the history
  • Loading branch information
snowyu committed Sep 2, 2024
1 parent e748836 commit 7eaf3e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ $ npm install -g @offline-ai/cli
$ ai COMMAND
running command...
$ ai (--version)
@offline-ai/cli/0.4.2 linux-x64 node-v20.14.0
@offline-ai/cli/0.4.3 linux-x64 node-v20.14.0
$ ai --help [COMMAND]
USAGE
$ ai COMMAND
Expand Down Expand Up @@ -396,7 +396,7 @@ EXAMPLES
$ ai agent publish <agent-name>
```

_See code: [src/commands/agent/index.ts](https://github.com/offline-ai/cli/blob/v0.4.2/src/commands/agent/index.ts)_
_See code: [src/commands/agent/index.ts](https://github.com/offline-ai/cli/blob/v0.4.3/src/commands/agent/index.ts)_

## `ai autocomplete [SHELL]`

Expand Down
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ bugs:
✔ `$exec`应该传递`llmStream`事件以便于显示外部脚本过程的流进度. @done(24-08-25 06:01)
✔ can not work on Windows @done(24-08-25 06:01)
☐ ai brain can not list downloaded
☐ ai brain refresh can not work if no maxCount option
21 changes: 14 additions & 7 deletions lib/file.ai.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
version: 0.2.2
version: 0.3.0
type: lib
description: |-
the simple text file loader. You can use environment variables in file path, eg, "$HOME/documents/document.md".
It can be used in prompt. eg, `user: think about the following file content: @file(document.md)`
It can be used in prompt. eg, `user: "think about the following file content: @file(document.md)"`
tag:
- file
- loader
Expand All @@ -22,10 +22,17 @@ output: # the file content
const fs = await import('fs');
const path = await import('path');
let filepath = this.content || this[0]
const filename = path.basename(filepath);
filepath = expandPath(filepath)
const content = fs.readFileSync(filepath, 'utf8');
if (this.onlyContent) return content;
return `filename: ${filename}\nfile content:\n${content}`
if (!filepath) {throw new Error('No file path provided.')}
if (filepath.startsWith('https://') || filepath.startsWith('http://')) {
const content = await fetch(filepath).then(res => res.text());
if (this.onlyContent) return content;
return `url: ${filepath}\nfile content:\n${content}`
} else {
const filename = path.basename(filepath);
filepath = expandPath(filepath)
const content = fs.readFileSync(filepath, 'utf8');
if (this.onlyContent) return content;
return `filename: ${filename}\nfile content:\n${content}`
}
}
$loadFile

0 comments on commit 7eaf3e4

Please sign in to comment.