-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CSV file reading functionality to the workspace file system #740
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,9 +235,9 @@ | |
|
||
## `retrieval` | ||
|
||
``` | ||
Usage: genaiscript retrieval|retreival [options] [command] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code block starting with 'Usage: genaiscript retrieval|retreival [options] [command]' should be fenced with triple backticks to maintain consistency with the document formatting.
|
||
RAG support | ||
RAG support | ||
|
||
Options: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,22 @@ | |
const data = await workspace.readXML("data.xml") | ||
``` | ||
|
||
### `readCSV` | ||
|
||
Reads the content of a file as CSV. | ||
|
||
```ts | ||
const data = await workspace.readCSV("data.csv") | ||
``` | ||
|
||
In Typescript, you can type the output. | ||
|
||
```ts '<{ name: string; value: number }>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect code fence, Typescript type annotation should not be in a string format.
|
||
const data = await workspace.readCSV<{ name: string; value: number }>( | ||
"data.csv" | ||
) | ||
``` | ||
|
||
### `writeText` | ||
|
||
Writes text to a file, relative to the workspace root. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { JSONLineCache } from "./cache" | ||
import { DOT_ENV_REGEX } from "./constants" | ||
import { CSVParse } from "./csv" | ||
import { NotSupportedError, errorMessage } from "./error" | ||
import { resolveFileContent } from "./file" | ||
import { readText, writeText } from "./fs" | ||
|
@@ -68,6 +69,11 @@ export function createFileSystem(): Omit<WorkspaceFileSystem, "grep"> { | |
const res = XMLParse(file.content) | ||
return res | ||
}, | ||
readCSV: async (f: string | Awaitable<WorkspaceFile>, options) => { | ||
const file = await fs.readText(f) | ||
const res = CSVParse(file.content, options) | ||
return res | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no error handling for the async function
|
||
}, | ||
cache: async (name: string) => { | ||
if (!name) throw new NotSupportedError("missing cache name") | ||
const res = JSONLineCache.byName<any, any>(name) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ export async function createPromptContext( | |
readText: (f) => runtimeHost.workspace.readText(f), | ||
readJSON: (f) => runtimeHost.workspace.readJSON(f), | ||
readXML: (f) => runtimeHost.workspace.readXML(f), | ||
readCSV: (f) => runtimeHost.workspace.readCSV(f), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function
|
||
writeText: (f, c) => runtimeHost.workspace.writeText(f, c), | ||
cache: (n) => runtimeHost.workspace.cache(n), | ||
findFiles: async (pattern, options) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -579,6 +579,15 @@ interface WorkspaceFileSystem { | |
*/ | ||
readXML(path: string | Awaitable<WorkspaceFile>): Promise<any> | ||
|
||
/** | ||
* Reads the content of a CSV file. | ||
* @param path | ||
*/ | ||
readCSV<T extends object>( | ||
path: string | Awaitable<WorkspaceFile>, | ||
options?: CSVParseOptions | ||
): Promise<T[]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
/** | ||
* Writes a file as text to the file system | ||
* @param path | ||
|
@@ -969,6 +978,11 @@ interface ParseZipOptions { | |
|
||
type TokenEncoder = (text: string) => number[] | ||
|
||
interface CSVParseOptions { | ||
delimiter?: string | ||
headers?: string[] | ||
} | ||
|
||
interface Parsers { | ||
/** | ||
* Parses text as a JSON5 payload | ||
|
@@ -1034,7 +1048,7 @@ interface Parsers { | |
*/ | ||
CSV( | ||
content: string | WorkspaceFile, | ||
options?: { delimiter?: string; headers?: string[] } | ||
options?: CSVParseOptions | ||
): object[] | undefined | ||
|
||
/** | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a typo here.