diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/README.md b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/README.md new file mode 100644 index 0000000..4e7594b --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/README.md @@ -0,0 +1,75 @@ +# Vercel AI SDK `generateObject` schema selection + +This example shows the TypeScript counterpart to the Python schema-selection +examples in this repository. + +The enforcement point is schema selection. + +Flow: + +`compiler state -> host selects schema -> host builds generateObject request -> downstream model call` + +## What this example demonstrates + +- Compiler-only state drives host schema selection. +- The host chooses which structured-output schema to offer. +- `generateObject` is the downstream host behavior, not the authority layer. +- No model output mutates compiler state. +- If compiler state does not authorize a schema, the host omits schema selection. + +## Boundary + +- `@rlippmann/context-compiler` owns authoritative state transitions. +- The host reads compiler state and selects a Zod schema, or no schema. +- The host may pass that schema into Vercel AI SDK `generateObject`. +- The compiler does not select schemas dynamically. +- The compiler does not derive state from model output. + +## Deterministic behavior + +Given policy state: + +```text +use python_script +prohibit shell_command +``` + +the host offers the `python_script` schema and does not offer the +`shell_command` schema. + +If state prohibits every known schema, the host omits schema selection and does +not build a `generateObject` request. + +## Test coverage + +Tests assert: + +- compiler state -> selected schema +- selected schema -> request config +- omit schema when state does not authorize one + +Primary tests are deterministic and do not call a model. + +## Install + +```bash +cd typescript/examples/schema_selection/vercel_ai_sdk_generate_object +npm install +``` + +## Validate + +```bash +npm run build +npm run typecheck +npm test +``` + +## Run the example + +```bash +npm run example +``` + +The example uses a stubbed `generateObject` implementation so the downstream +behavior stays observable without a live model. diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package-lock.json b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package-lock.json new file mode 100644 index 0000000..6a66f27 --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package-lock.json @@ -0,0 +1,585 @@ +{ + "name": "context-compiler-example-vercel-ai-sdk-generate-object", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "context-compiler-example-vercel-ai-sdk-generate-object", + "version": "0.0.1", + "dependencies": { + "@rlippmann/context-compiler": "^0.7.5", + "zod": "^3.25.76" + }, + "devDependencies": { + "@types/node": "^24.10.0", + "tsx": "^4.20.6", + "typescript": "^5.9.3" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.1.tgz", + "integrity": "sha512-Svl7tq8k/08+p6CXPpRjQ1fKX+1odH/BQbb48fV6fj3CWHhsoIOoY87w1oHXm0qEpkIK3ZfVgp0hed3XBXzXMQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.1.tgz", + "integrity": "sha512-0k2F129Xdio1TdJfzJ8sy1Q47vUD2NnwdhiAf7drUN1EBTfPf4hsFCtmMgu/6m8JSzsBrlmVjudMBQqOfG8usQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.1.tgz", + "integrity": "sha512-34EGEbCIAgosYz6goLcopX6Mo7NyGv9tfwEM2/7Ce2VcVRk568iSvniGWcUXIy7wEDR1wzolcxcriFVrWYcwBg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.1.tgz", + "integrity": "sha512-dbwY7ltSMDWsRatcRpCnES4F+im88OCUgGZjy52shC7GqHRE/cYlxNbB4Z4UpJswpcc4Qxd2oE/ufM0p61IKng==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.1.tgz", + "integrity": "sha512-TZbWkQY7kvTAXbXUT7uVACR5cMHsDiSz9z7ZKAX/RTq/WJEk3QyRr0wZpNhBDX+/0CtdqUIJlOiodQcta6tY3Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.1.tgz", + "integrity": "sha512-zfdzgK9ACBNZLI/CyHTOx81SyNbM6YXn7rxSgX97VjyiPl9W1i4Ka4fgKECEoFCKGpvBj5qArWIGgQjOwkgskQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.1.tgz", + "integrity": "sha512-wG2EA8ENdEI0qhkSZMjfqrdY+ziCYCPMmtZjjIwOmXFjmyzEHn+UUxk5of+SYsjtfs3VpnlC7QLzSI5hY/rOAw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.1.tgz", + "integrity": "sha512-i7dZ9vQgnvSCzi/rYCXNgtF/U+eKZNJBzu3eTQbRgHnM7tNSizLOkRFAl3qzVc/Op/u5YkHHa4pf/3DOYHthLQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.1.tgz", + "integrity": "sha512-qVXBOHQS+d5Y722GwJzJUtOLlX7km3CraOaGormF1pDtPd2C/l1SHRPgjLunLGe51Sh5YYWKMFDyV4SxgMQYTQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.1.tgz", + "integrity": "sha512-yHs+0uc8+nvEAfAfxrWQKK5peSNzBc4PegcMO0EJ2hT71uA7vB8Ihg2e77R2P7SG5uYjPbHlLLmve4LLLRCf0g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.1.tgz", + "integrity": "sha512-d1z4ZuP0ajrfz/FhGT4vv278rX8KnPPJx8i5+AtK7TYbx9Le9F1hyzurZpkEyjkGa9dUGhQow4C1NmeGvqxN2w==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.1.tgz", + "integrity": "sha512-M5sRjUVZrkm1OAPR3dlOYzNmN+loZKGVi1VUQGrwuqLcbR6qeAz+famMhjASeH3YVKvZz+zT1jlh/keC3Rj/lg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.1.tgz", + "integrity": "sha512-mRObBZeHh2OxcBFPWE/FjylkRgZdYuiTR3vaTozquCGOH14iP9oN4x4Ge81CoIDYQrXmIxpFumJBu5MtZpnQJQ==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.1.tgz", + "integrity": "sha512-slScBsMAb3GFDcdrCgLwZtPYRoH2H/youv10QiZyRjmsP48fznoveWytSgCI/R0ZcUgpc0ZhIUEx6LHts8yrfQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.1.tgz", + "integrity": "sha512-kw0owk1o0GFETUJyW0jc0G4Yzs0BHZn0JDZ8JRT088vjJYX777BAs1fDGxAC+q831qOs2DTC96mNsG2opdfyyQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.1.tgz", + "integrity": "sha512-/lAIjX8aYFRByhh6L5rYtPEDRqa9de/4V/juOXcta5frjvzXO4/sqEtyytse0g3zZFuWu5cDN0MkLz2qRDD2Ag==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.1.tgz", + "integrity": "sha512-u/anNYF2mmVOEDwLtnQ1wOr3EZ9sTNGLWrsYGYwHWzGA3Si84IOkHXlbWTD1NB+9/1lcnweYKO54uhxZydNzfA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.1.tgz", + "integrity": "sha512-oks0DYbLwWMmaakTsCb+zL4E+aHRVLom9IJZOAthMQEPiQmydXHkziYEsGYRx0uNV/IjEKGAV941JzH02pflqw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.1.tgz", + "integrity": "sha512-aeL6lAnN89Hz43Mlh1G8ARasbuoYvSITDEx0tHh5b7jJnHcssqgjy9Yx430GDpmCa6OyrKoS0aNRjKundRizGg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.1.tgz", + "integrity": "sha512-MEFJe5C3R8pwXdZ5Y21oo6m7ePiS0d9pWucn99O/wvyJZChoIQKrQDxKrGeW8F5+T0okTHesAmDeiHDTIq0V/Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.1.tgz", + "integrity": "sha512-i/ZLIOafE0Z8cI/XANJAixoJL/uRAoS2xOA3rb0xN+KK0K177cMAsQYkzHtBrtMXAKuAc7HGgcWiZ/sRC1Nxgw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.1.tgz", + "integrity": "sha512-ge+Z7EXFNt2BO1oAMsVpiQ8EwndV9i1xXerAeTIK7AtPs3bKFXQM7nlRxDSIUIMeueR1CNXxqztLzdNeReKBJg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.1.tgz", + "integrity": "sha512-BEjgtECkL3vY+SaSQ6nzVfiALUeFxpawyp8Jmf5PtYhf1Ug40N1h/hxlhts+f1FvSvarEigdxS3BlSMI2PJLcQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.1.tgz", + "integrity": "sha512-lCv9eK/H6ZJWbE7bh2nw54CZ9M2nupBxJcTsdk/QQnWkdSjKGuxmmH8/GWrlT1eMmZfn4dGcCjRte397WqfQXA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.1.tgz", + "integrity": "sha512-zvb/mB2bSCoJOpoCBgYKKpX6YM6mJBlBUVUtVj41DlZJVEB6/0CKlRYxP5wWl1C1ILiCoAU5wZZ4q1P3qeS6Eg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.1.tgz", + "integrity": "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@rlippmann/context-compiler": { + "version": "0.7.5", + "resolved": "https://registry.npmjs.org/@rlippmann/context-compiler/-/context-compiler-0.7.5.tgz", + "integrity": "sha512-E9p/rxA72z5NW1AnLf+NJ5w9yiSP1papEAjIbLnGje1+xM0JHFb8KuI2bp66u2KC8Y7hRN/EmzYcP2AP7yf3Ew==", + "license": "Apache-2.0" + }, + "node_modules/@types/node": { + "version": "24.13.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.13.2.tgz", + "integrity": "sha512-fRa09kZTgu8o71KFcDjUFuc7F+dEbZYZmkI0mg5YBTRs0yMKjYHsq/c0urDKeDb+D5qVgXOdFcuu+DZPKOITwA==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~7.18.0" + } + }, + "node_modules/esbuild": { + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.1.tgz", + "integrity": "sha512-HrJrvZv5ayxBzPfwphOoNzkzOIIlifzk0KJrGK2c8R4+LKpMtpYLQeUdjnwjWv/LZlkH2laZk+4w78pi99D4Vw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.28.1", + "@esbuild/android-arm": "0.28.1", + "@esbuild/android-arm64": "0.28.1", + "@esbuild/android-x64": "0.28.1", + "@esbuild/darwin-arm64": "0.28.1", + "@esbuild/darwin-x64": "0.28.1", + "@esbuild/freebsd-arm64": "0.28.1", + "@esbuild/freebsd-x64": "0.28.1", + "@esbuild/linux-arm": "0.28.1", + "@esbuild/linux-arm64": "0.28.1", + "@esbuild/linux-ia32": "0.28.1", + "@esbuild/linux-loong64": "0.28.1", + "@esbuild/linux-mips64el": "0.28.1", + "@esbuild/linux-ppc64": "0.28.1", + "@esbuild/linux-riscv64": "0.28.1", + "@esbuild/linux-s390x": "0.28.1", + "@esbuild/linux-x64": "0.28.1", + "@esbuild/netbsd-arm64": "0.28.1", + "@esbuild/netbsd-x64": "0.28.1", + "@esbuild/openbsd-arm64": "0.28.1", + "@esbuild/openbsd-x64": "0.28.1", + "@esbuild/openharmony-arm64": "0.28.1", + "@esbuild/sunos-x64": "0.28.1", + "@esbuild/win32-arm64": "0.28.1", + "@esbuild/win32-ia32": "0.28.1", + "@esbuild/win32-x64": "0.28.1" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/tsx": { + "version": "4.22.4", + "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.22.4.tgz", + "integrity": "sha512-X8EX+XV4QR5xCsrgxaED954zTDfY8KqlDtskKEL0cHhyS/P8b4IFOvGDQpsC9Q1XnLq915wEfwwY/zzskCtmhg==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "~0.28.0" + }, + "bin": { + "tsx": "dist/cli.mjs" + }, + "engines": { + "node": ">=18.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + } + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "7.18.2", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz", + "integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==", + "dev": true, + "license": "MIT" + }, + "node_modules/zod": { + "version": "3.25.76", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz", + "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + } + } +} diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package.json b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package.json new file mode 100644 index 0000000..2477c7f --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/package.json @@ -0,0 +1,21 @@ +{ + "name": "context-compiler-example-vercel-ai-sdk-generate-object", + "private": true, + "version": "0.0.1", + "type": "module", + "scripts": { + "build": "tsc -p tsconfig.json", + "typecheck": "tsc --noEmit -p tsconfig.json", + "test": "node --import tsx --test tests/*.test.ts", + "example": "node dist/src/index.js" + }, + "dependencies": { + "@rlippmann/context-compiler": "^0.7.5", + "zod": "^3.25.76" + }, + "devDependencies": { + "@types/node": "^24.10.0", + "tsx": "^4.20.6", + "typescript": "^5.9.3" + } +} diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/src/index.ts b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/src/index.ts new file mode 100644 index 0000000..7b1b749 --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/src/index.ts @@ -0,0 +1,152 @@ +import { + POLICY_PROHIBIT, + POLICY_USE, + createEngine, + getPolicyItems, + type EngineState +} from "@rlippmann/context-compiler"; +import { z, type ZodTypeAny } from "zod"; + +declare const process: { argv: string[]; exitCode?: number }; + +export type StructuredSchemaName = "python_script" | "shell_command"; + +export type StructuredSchema = { + name: StructuredSchemaName; + description: string; + schema: ZodTypeAny; +}; + +export type GenerateObjectRequest = { + prompt: string; + schemaName: StructuredSchemaName; + schema: StructuredSchema; +}; + +export type GenerateObjectLike = ( + request: GenerateObjectRequest +) => Promise<{ object: TObject }>; + +const PYTHON_SCRIPT_SCHEMA = z.object({ + code: z.string().describe("A complete Python script.") +}); + +const SHELL_COMMAND_SCHEMA = z.object({ + command: z.string().describe("A single shell command.") +}); + +const SCHEMA_REGISTRY: Record = { + python_script: { + name: "python_script", + description: "Generate a Python script object.", + schema: PYTHON_SCRIPT_SCHEMA + }, + shell_command: { + name: "shell_command", + description: "Generate a shell command object.", + schema: SHELL_COMMAND_SCHEMA + } +}; + +const KNOWN_SCHEMAS: readonly StructuredSchemaName[] = [ + "python_script", + "shell_command" +]; + +export function selectStructuredSchemasFromState( + state: EngineState +): StructuredSchema[] { + const useItems = getPolicyItems(state, POLICY_USE).filter( + (item): item is StructuredSchemaName => + KNOWN_SCHEMAS.includes(item as StructuredSchemaName) + ); + const prohibitItems = new Set(getPolicyItems(state, POLICY_PROHIBIT)); + + if (useItems.length > 0) { + return useItems + .filter((item) => !prohibitItems.has(item)) + .map((item) => SCHEMA_REGISTRY[item]); + } + + return []; +} + +export function buildGenerateObjectRequest( + state: EngineState, + prompt: string +): GenerateObjectRequest | null { + const availableSchemas = selectStructuredSchemasFromState(state); + const selected = availableSchemas[0]; + + if (!selected) { + return null; + } + + return { + prompt, + schemaName: selected.name, + schema: selected + }; +} + +export async function generateStructuredObject( + state: EngineState, + prompt: string, + generateObject: GenerateObjectLike +): Promise<{ request: GenerateObjectRequest; object: TObject } | null> { + const request = buildGenerateObjectRequest(state, prompt); + if (request === null) { + return null; + } + + const result = await generateObject(request); + return { + request, + object: result.object + }; +} + +export async function runExample(): Promise<{ + availableSchemaNames: StructuredSchemaName[]; + requestBuilt: boolean; + object: { code: string } | null; +}> { + const engine = createEngine(); + engine.step("use python_script"); + engine.step("prohibit shell_command"); + + const availableSchemas = selectStructuredSchemasFromState(engine.state); + const generated = await generateStructuredObject<{ code: string }>( + engine.state, + "Write a short Python script that prints hello.", + async (request) => ({ + object: { + code: `# schema=${request.schemaName}\nprint("hello")` + } + }) + ); + + return { + availableSchemaNames: availableSchemas.map((schema) => schema.name), + requestBuilt: generated !== null, + object: generated?.object ?? null + }; +} + +if ( + typeof process !== "undefined" && + process.argv[1] && + import.meta.url === new URL(process.argv[1], "file://").href +) { + runExample() + .then((result) => { + console.log( + "integration example: schema selection with vercel ai sdk generateObject" + ); + console.log(JSON.stringify(result, null, 2)); + }) + .catch((error: unknown) => { + console.error(error); + process.exitCode = 1; + }); +} diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tests/index.test.ts b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tests/index.test.ts new file mode 100644 index 0000000..7579a98 --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tests/index.test.ts @@ -0,0 +1,59 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createEngine } from "@rlippmann/context-compiler"; + +import { + buildGenerateObjectRequest, + generateStructuredObject, + selectStructuredSchemasFromState +} from "../src/index.js"; + +test("compiler state selects only the authorized schema", () => { + const engine = createEngine(); + engine.step("use python_script"); + engine.step("prohibit shell_command"); + + const selected = selectStructuredSchemasFromState(engine.state); + + assert.deepEqual( + selected.map((schema) => schema.name), + ["python_script"] + ); +}); + +test("selected schema becomes generateObject request config", () => { + const engine = createEngine(); + engine.step("use python_script"); + + const request = buildGenerateObjectRequest( + engine.state, + "Write a short Python script that prints hello." + ); + + assert.ok(request !== null); + assert.equal(request.schemaName, "python_script"); + assert.equal( + request.schema.schema.safeParse({ code: "print('hello')" }).success, + true + ); +}); + +test("omit schema when state does not authorize one", async () => { + const engine = createEngine(); + engine.step("prohibit python_script"); + engine.step("prohibit shell_command"); + + const request = buildGenerateObjectRequest( + engine.state, + "Write a short Python script that prints hello." + ); + let called = false; + const result = await generateStructuredObject(engine.state, "ignored", async () => { + called = true; + return { object: { code: "print('hello')" } }; + }); + + assert.equal(request, null); + assert.equal(result, null); + assert.equal(called, false); +}); diff --git a/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tsconfig.json b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tsconfig.json new file mode 100644 index 0000000..cf22cbc --- /dev/null +++ b/typescript/examples/schema_selection/vercel_ai_sdk_generate_object/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "types": ["node"], + "outDir": "dist", + "rootDir": ".", + "declaration": true + }, + "include": ["src/**/*.ts", "tests/**/*.ts"] +}