Skip to content
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

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@

## `retrieval`

```
Usage: genaiscript retrieval|retreival [options] [command]

Check failure on line 239 in docs/src/content/docs/reference/cli/commands.md

View workflow job for this annotation

GitHub Actions / build

There seems to be a typo here. The correct spelling is 'retrieval', not 'retreival'.

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.

generated by pr-docs-review-commit typo


Check notice on line 240 in docs/src/content/docs/reference/cli/commands.md

View workflow job for this annotation

GitHub Actions / build

The code block starting with 'Usage: genaiscript retrieval|retreival [options] [command]' should be fenced with triple backticks to maintain consistency with the document formatting.

Choose a reason for hiding this comment

The 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.

generated by pr-docs-review-commit missing_code_fence

RAG support
RAG support

Options:
Expand Down
16 changes: 16 additions & 0 deletions docs/src/content/docs/reference/scripts/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ Reads the content of a file as XML.
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 }>'

Choose a reason for hiding this comment

The 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.

generated by pr-docs-review-commit incorrect_code_fence

const data = await workspace.readCSV<{ name: string; value: number }>(
"data.csv"
)
```

### `writeText`

Writes text to a file, relative to the workspace root.
Expand Down
16 changes: 15 additions & 1 deletion genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/core/src/filesystem.ts
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"
Expand Down Expand Up @@ -68,6 +69,14 @@
const res = XMLParse(file.content)
return res
},
readCSV: async <T extends object>(
f: string | Awaitable<WorkspaceFile>,
options?: CSVParseOptions
): Promise<T[]> => {
const file = await fs.readText(f)
const res = CSVParse(file.content, options) as T[]
return res

Check failure on line 78 in packages/core/src/filesystem.ts

View workflow job for this annotation

GitHub Actions / build

There is no error handling for the async function `readCSV`. If the file read operation or CSV parsing fails, it could lead to unhandled promise rejection. Consider adding a try-catch block. 😊

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error handling for the async function readCSV. If the file read operation or CSV parsing fails, it could lead to unhandled promise rejection. Consider adding a try-catch block. 😊

generated by pr-review-commit missing_error_handling

},
cache: async (name: string) => {
if (!name) throw new NotSupportedError("missing cache name")
const res = JSONLineCache.byName<any, any>(name)
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
readText: (f) => runtimeHost.workspace.readText(f),
readJSON: (f) => runtimeHost.workspace.readJSON(f),
readXML: (f) => runtimeHost.workspace.readXML(f),
readCSV: (f) => runtimeHost.workspace.readCSV(f),

Check failure on line 67 in packages/core/src/promptcontext.ts

View workflow job for this annotation

GitHub Actions / build

The function `readCSV` is missing type annotations for its argument and return value. This could lead to confusion about the expected input and output. Please add type annotations. 😊

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function readCSV is missing type annotations for its argument and return value. This could lead to confusion about the expected input and output. Please add type annotations. 😊

generated by pr-review-commit missing_type

writeText: (f, c) => runtimeHost.workspace.writeText(f, c),
cache: (n) => runtimeHost.workspace.cache(n),
findFiles: async (pattern, options) => {
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@
*/
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[]>

Check failure on line 589 in packages/core/src/types/prompt_template.d.ts

View workflow job for this annotation

GitHub Actions / build

The `readCSV` function in the `WorkspaceFileSystem` interface is missing a detailed comment explaining its purpose, parameters, and return value. This could make it harder for other developers to understand its usage. Please add a descriptive comment. 😊

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readCSV function in the WorkspaceFileSystem interface is missing a detailed comment explaining its purpose, parameters, and return value. This could make it harder for other developers to understand its usage. Please add a descriptive comment. 😊

generated by pr-review-commit missing_comment


/**
* Writes a file as text to the file system
* @param path
Expand Down Expand Up @@ -969,6 +978,11 @@

type TokenEncoder = (text: string) => number[]

interface CSVParseOptions {
delimiter?: string
headers?: string[]
}

interface Parsers {
/**
* Parses text as a JSON5 payload
Expand Down Expand Up @@ -1034,7 +1048,7 @@
*/
CSV(
content: string | WorkspaceFile,
options?: { delimiter?: string; headers?: string[] }
options?: CSVParseOptions
): object[] | undefined

/**
Expand Down
16 changes: 15 additions & 1 deletion packages/sample/genaisrc/blog/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading