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

Universal compiler #12

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ This is just a small example of what PromptL can do. It is a powerful tool that
## Links

[Website](https://promptl.ai/) | [Documentation](https://docs.latitude.so/promptl/getting-started/introduction)

## Development

- To build the JavaScript library, run `pnpm build:lib`
- To build the universal WASM module with RPC, first install [`javy`](https://github.com/bytecodealliance/javy/releases) and then run `pnpm build:rpc`
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default [
globals: {
...globals.node,
...globals.browser,
Javy: 'readonly',
},

parser: tsParser,
Expand Down
3 changes: 2 additions & 1 deletion examples/start.ts → examples/lib.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Run `pnpm build:lib` before running this example

import { Chain } from '../dist/index.js'

const prompt = `
Expand All @@ -7,7 +9,6 @@ const prompt = `
`

const chain = new Chain({ prompt })

const step = await chain.step()

console.log(step)
110 changes: 110 additions & 0 deletions examples/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Run `pnpm build:rpc` before running this example

import { FileHandle, mkdir, open, readFile, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { WASI } from 'node:wasi'

const result = await execute('./dist/promptl.wasm', [
{
id: 1,
procedure: 'scan',
parameters: {
prompt: `Hello world!`,
},
},
{
id: 2,
procedure: 'compile',
parameters: {
prompt: 'Hello {{ name }}!',
parameters: {
name: 'Alex',
},
},
},
])

console.log(JSON.stringify(result, null, 2))

// Utility functions

async function execute(wasm: string, data: any): Promise<any> {
const dir = tmpdir()
const stdin_path = join(dir, `stdin`)
const stdout_path = join(dir, `stdout`)
const stderr_path = join(dir, `stderr`)

await mkdir(dir, { recursive: true })
await writeFile(stdin_path, '')
await writeFile(stdout_path, '')
await writeFile(stderr_path, '')

let stdin: FileHandle | undefined
let stdout: FileHandle | undefined
let stderr: FileHandle | undefined

let wasmStdin: FileHandle | undefined
let wasmStdout: FileHandle | undefined
let wasmStderr: FileHandle | undefined

try {
stdin = await open(stdin_path, 'w')
stdout = await open(stdout_path, 'r')
stderr = await open(stderr_path, 'r')

wasmStdin = await open(stdin_path, 'r')
wasmStdout = await open(stdout_path, 'w')
wasmStderr = await open(stderr_path, 'w')

const wasi = new WASI({
version: 'preview1',
args: [],
env: {},
stdin: wasmStdin.fd,
stdout: wasmStdout.fd,
stderr: wasmStderr.fd,
returnOnExit: true,
})

const bytes = await readFile(wasm)
WebAssembly.validate(bytes)
const module = await WebAssembly.compile(bytes)
const instance = await WebAssembly.instantiate(
module,
wasi.getImportObject(),
)

await send(stdin, data)

wasi.start(instance)

const [out, err] = await Promise.all([receive(stdout), receive(stderr)])
if (err) throw new Error(err)

return out
} finally {
await Promise.all([
stdin?.close(),
stdout?.close(),
stderr?.close(),
wasmStdin?.close(),
wasmStdout?.close(),
wasmStderr?.close(),
])
}
}

async function send(file: FileHandle, data: any) {
await writeFile(file, JSON.stringify(data), { encoding: 'utf8' })
}

async function receive(file: FileHandle): Promise<any> {
const data = (await readFile(file, 'utf8')).trim()

try {
return JSON.parse(data)
} catch {
return data
}
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c",
"build:lib": "rollup -c",
"build:rpc": "rollup -c rollup.config.rpc.mjs",
"test": "vitest run",
"test:watch": "vitest",
"prettier": "prettier --write src/**/*.ts",
Expand All @@ -35,24 +36,28 @@
"dependencies": {
"acorn": "^8.9.0",
"code-red": "^1.0.3",
"fast-sha256": "^1.3.0",
"locate-character": "^3.0.0",
"yaml": "^2.4.5",
"zod": "^3.23.8"
},
"devDependencies": {
"eslint": "^9.17.0",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.17.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/estree": "^1.0.1",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^8.19.0",
"eslint": "^9.17.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.14.0",
"prettier": "^3.4.2",
"rollup": "^4.10.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-execute": "^1.1.1",
"tslib": "^2.8.1",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
Expand Down
Loading
Loading