Skip to content

Commit

Permalink
container copy (#557)
Browse files Browse the repository at this point in the history
* add crypto test

* add copy

* fix copy

* renamed function
  • Loading branch information
pelikhan committed Jun 20, 2024
1 parent 931ba7f commit 2f7ee91
Show file tree
Hide file tree
Showing 22 changed files with 170 additions and 20 deletions.
2 changes: 1 addition & 1 deletion demo/genaisrc/az-poetry.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defTool("search_files", "search files in the workspace", {
}
}
}, async args => {
const { search} = args
const { search } = args
const files = await workspace.findFiles(search)
return files.map(f => f.filename).join("\n")
})
Expand Down
9 changes: 8 additions & 1 deletion demo/genaisrc/genaiscript.d.ts

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

9 changes: 8 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.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Dockerized Tools
title: Containerized Tools
sidebar:
order: 10
---
Expand Down
13 changes: 13 additions & 0 deletions docs/src/content/docs/reference/scripts/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ The container has a volume mounted in the host file system, which allows to read
await container.writeText("hello.txt", "Hello, world!")
const content = await container.readText("hello.txt")
```

## Copy files to container

You can also copy files from the host to the container.

```js
// src/* -> ./src/*
await container.copyTo("src/**", ".")
```

## Using containers in tools

The [containerized tools](/genaiscript/guides/containerized-tools) guide shows how to use containers in tools to handle untrusted text securely.
9 changes: 8 additions & 1 deletion genaisrc/genaiscript.d.ts

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

14 changes: 13 additions & 1 deletion packages/cli/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
host,
installImport,
logError,
logVerbose,
} from "genaiscript-core"
import { finished } from "stream/promises"
import { ensureDir, remove } from "fs-extra"
import { randomBytes } from "node:crypto"
import { readFile, writeFile } from "fs/promises"
import { copyFile, readFile, writeFile } from "fs/promises"
import { DOCKERODE_VERSION } from "./version"

type DockerodeType = import("dockerode")
Expand Down Expand Up @@ -248,6 +249,16 @@ export class DockerManager {
return await readFile(hostFilename, { encoding: "utf8" })
}

const copyTo = async (from: string | string[], to: string) => {
const files = await host.findFiles(from)
for (const file of files) {
const source = host.path.resolve(file)
const target = host.path.resolve(hostPath, to, file)
await ensureDir(host.path.dirname(target))
await copyFile(source, target)
}
}

const c = <ContainerHost>{
id: container.id,
disablePurge: !!options.disablePurge,
Expand All @@ -256,6 +267,7 @@ export class DockerManager {
exec,
writeText,
readText,
copyTo,
}
this.containers.push(c)
await container.start()
Expand Down
9 changes: 8 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.

9 changes: 8 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ interface ContainerHost extends ShellHost {
containerPath: string

/**
* Writes a file as text to the file system
* Writes a file as text to the container file system
* @param path
* @param content
*/
Expand All @@ -1535,6 +1535,13 @@ interface ContainerHost extends ShellHost {
* @param path
*/
readText(path: string): Promise<string>

/**
* Copies a set of files into the container
* @param fromHost glob matching files
* @param toContainer directory in the container
*/
copyTo(fromHost: string | string[], toContainer: string): Promise<void>
}

interface PromptContext extends ChatGenerationContext {
Expand Down
2 changes: 2 additions & 0 deletions packages/sample/genaisrc/container.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ if (version.stdout !== fversion)
throw new Error(
`writetext/readtext error, expected '${version.stdout}', got '${fversion}'`
)
await container.copyTo("src/rag/*", "copied")
if (!await container.readText("copied/src/rag/markdown.md")) throw new Error("copy failed")
await container.writeText("main.py", 'print("hello")')
const hello = await container.exec("python", ["main.py"])
if (hello.exitCode) throw new Error("python script failed")
Expand Down
5 changes: 5 additions & 0 deletions packages/sample/genaisrc/crypto.genai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script({ tests: {} })
const data = new Uint8Array(16)
const rnd = crypto.getRandomValues(data)

$`Print "${rnd}".`
9 changes: 8 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.

9 changes: 8 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.

9 changes: 8 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.

9 changes: 8 additions & 1 deletion packages/sample/genaisrc/style/genaiscript.d.ts

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

9 changes: 8 additions & 1 deletion packages/sample/src/aici/genaiscript.d.ts

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

9 changes: 8 additions & 1 deletion packages/sample/src/errors/genaiscript.d.ts

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

9 changes: 8 additions & 1 deletion packages/sample/src/makecode/genaiscript.d.ts

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

9 changes: 8 additions & 1 deletion packages/sample/src/tla/genaiscript.d.ts

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

Loading

0 comments on commit 2f7ee91

Please sign in to comment.