-
Notifications
You must be signed in to change notification settings - Fork 126
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
workspace cache #632
Changes from all 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 |
---|---|---|
|
@@ -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") | ||
// write entries | ||
await cache.set("file.txt", "...") | ||
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 use of 'await' outside of an async function.
|
||
// read entries | ||
const content = await cache.get("file.txt") | ||
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 use of 'await' outside of an async function.
|
||
// list entries | ||
const entries = await cache.values() | ||
``` |
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,3 +1,4 @@ | ||
import { JSONLineCache } from "./cache" | ||
import { DOT_ENV_REGEX } from "./constants" | ||
import { NotSupportedError, errorMessage } from "./error" | ||
import { resolveFileContent } from "./file" | ||
|
@@ -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 GitHub Actions / build
|
||
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
|
||
} | ||
;(fs as any).readFile = readText | ||
return Object.freeze(fs) | ||
|
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 |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
import { createParsers } from "./parsers" | ||
import { readText } from "./fs" | ||
import { | ||
PromptImage, | ||
PromptNode, | ||
appendChild, | ||
createFileMergeNode, | ||
|
@@ -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 GitHub Actions / build
|
||
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
|
||
findFiles: async (pattern, options) => { | ||
const res = await runtimeHost.workspace.findFiles(pattern, options) | ||
trace.files(res, { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 GitHub Actions / build
|
||
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
|
||
} | ||
|
||
interface ToolCallContext { | ||
|
@@ -1641,6 +1669,9 @@ | |
path: Path | ||
parsers: Parsers | ||
retrieval: Retrieval | ||
/** | ||
* @deprecated Use `workspace` instead | ||
*/ | ||
fs: WorkspaceFileSystem | ||
workspace: WorkspaceFileSystem | ||
YAML: YAML | ||
|
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`) |
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.
Incorrect use of 'await' outside of an async function.