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

workspace cache #632

Merged
merged 4 commits into from
Aug 20, 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
31 changes: 31 additions & 0 deletions docs/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 docs/src/content/docs/reference/scripts/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@
- cache
- summary.jsonl

</FileTree>
</FileTree>

## Programmatic cache

You can instantiate a custom cache object to manage the cache programmatically.

```js
const cache = await workspace.cache("summary")

Check failure on line 77 in docs/src/content/docs/reference/scripts/cache.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect use of 'await' outside of an async function.

Choose a reason for hiding this comment

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

Incorrect use of 'await' outside of an async function.

generated by pr-docs-review-commit await_usage

// write entries
await cache.set("file.txt", "...")

Check failure on line 79 in docs/src/content/docs/reference/scripts/cache.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect use of 'await' outside of an async function.

Choose a reason for hiding this comment

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

Incorrect use of 'await' outside of an async function.

generated by pr-docs-review-commit await_usage

// read entries
const content = await cache.get("file.txt")

Check failure on line 81 in docs/src/content/docs/reference/scripts/cache.mdx

View workflow job for this annotation

GitHub Actions / build

Incorrect use of 'await' outside of an async function.

Choose a reason for hiding this comment

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

Incorrect use of 'await' outside of an async function.

generated by pr-docs-review-commit await_usage

// list entries
const entries = await cache.values()
```
31 changes: 31 additions & 0 deletions genaisrc/genaiscript.d.ts

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

11 changes: 11 additions & 0 deletions packages/core/src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JSONLineCache } from "./cache"
import { DOT_ENV_REGEX } from "./constants"
import { NotSupportedError, errorMessage } from "./error"
import { resolveFileContent } from "./file"
Expand Down Expand Up @@ -67,6 +68,16 @@
const res = XMLParse(file.content)
return res
},
cache: async (name: string) => {
if (!name) throw new NotSupportedError("missing cache name")
const res = JSONLineCache.byName<any, any>(name)
return <WorkspaceFileCache<any, any>>{
get: async (key: any) => res.get(key),
set: async (key: any, val: any) => res.set(key, val),
values: async () =>
res.entries().then((es) => es.map((e) => e.val)),
}
},

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

View workflow job for this annotation

GitHub Actions / build

There is no error handling for the `JSONLineCache.byName<any, any>(name)` function call. If this function fails, it could lead to unexpected behavior. Consider adding error handling to improve the robustness of your code. 😊

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 JSONLineCache.byName<any, any>(name) function call. If this function fails, it could lead to unexpected behavior. Consider adding error handling to improve the robustness of your code. 😊

generated by pr-review-commit missing_error_handling

}
;(fs as any).readFile = readText
return Object.freeze(fs)
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/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 packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { createParsers } from "./parsers"
import { readText } from "./fs"
import {
PromptImage,
PromptNode,
appendChild,
createFileMergeNode,
Expand Down Expand Up @@ -105,6 +104,7 @@
readJSON: (f) => runtimeHost.workspace.readJSON(f),
readXML: (f) => runtimeHost.workspace.readXML(f),
writeText: (f, c) => runtimeHost.workspace.writeText(f, c),
cache: (n) => runtimeHost.workspace.cache(n),

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

View workflow job for this annotation

GitHub Actions / build

The `runtimeHost.workspace.cache(n)` function call lacks error handling. If this function fails, it could lead to unexpected behavior. It's a good practice to handle potential errors to make your code more reliable. 😊

Choose a reason for hiding this comment

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

The runtimeHost.workspace.cache(n) function call lacks error handling. If this function fails, it could lead to unexpected behavior. It's a good practice to handle potential errors to make your code more reliable. 😊

generated by pr-review-commit missing_error_handling

findFiles: async (pattern, options) => {
const res = await runtimeHost.workspace.findFiles(pattern, options)
trace.files(res, {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,25 @@

type ToolCallOutput = string | ToolCallContent | ShellOutput | WorkspaceFile

interface WorkspaceFileCache<K, V> {
/**
* Gets the value associated with the key, or undefined if there is none.
* @param key
*/
get(key: K): Promise<V | undefined>
/**
* Sets the value associated with the key.
* @param key
* @param value
*/
set(key: K, value: V): Promise<void>

/**
* List the values in the cache.
*/
values(): Promise<V[]>
}

interface WorkspaceFileSystem {
/**
* Searches for files using the glob pattern and returns a list of files.
Expand Down Expand Up @@ -524,6 +543,15 @@
* @param content
*/
writeText(path: string, content: string): Promise<void>

/**
* Opens a key-value cache for the given cache name.
* The cache is persisted accross runs of the script. Entries are dropped when the cache grows too large.
* @param cacheName
*/
cache<K = any, V = any>(
cacheName: string
): Promise<WorkspaceFileCache<K, V>>

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

View workflow job for this annotation

GitHub Actions / build

The `cache<K = any, V = any>(cacheName: string)` function does not validate the types of its parameters. This could lead to unexpected behavior if the function is called with incorrect types. Consider adding type validation to improve the robustness of your code. 😊

Choose a reason for hiding this comment

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

The cache<K = any, V = any>(cacheName: string) function does not validate the types of its parameters. This could lead to unexpected behavior if the function is called with incorrect types. Consider adding type validation to improve the robustness of your code. 😊

generated by pr-review-commit missing_type_validation

}

interface ToolCallContext {
Expand Down Expand Up @@ -1641,6 +1669,9 @@
path: Path
parsers: Parsers
retrieval: Retrieval
/**
* @deprecated Use `workspace` instead
*/
fs: WorkspaceFileSystem
workspace: WorkspaceFileSystem
YAML: YAML
Expand Down
17 changes: 17 additions & 0 deletions packages/sample/genaisrc/cache.genai.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
script({
model: "openai:gpt-3.5-turbo",
tests: {},
})

const cache = await workspace.cache<number, number>("test-cache")
const key = Math.random()
const value = Math.random()

await cache.set(key, value)
const result = await cache.get(key)
if (result !== value) throw new Error(`unexpected value: ${result}`)

const values = await cache.values()
if (!values.includes(value)) throw new Error(`unexpected values: ${values}`)

console.log(`cache test passed`)
31 changes: 31 additions & 0 deletions packages/sample/genaisrc/genaiscript.d.ts

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

31 changes: 31 additions & 0 deletions packages/sample/genaisrc/node/genaiscript.d.ts

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

Loading