The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| } from "../capabilities/ai"; |
| 6 | import type { ColineCapabilities } from "../capabilities/types"; |
| 7 | import { createCapabilityGate, type CapabilityGateConfig } from "../capabilities/gate"; |
| 8 |
| 9 | /** Apply the production permission/effect table to every fake call. */ |
| 10 | export function guardTestCapabilities( |
| 11 | capabilities: ColineCapabilities, |
| 12 | config: CapabilityGateConfig, |
| 13 | options: { interactive?: boolean; appPrincipal?: boolean } = {}, |
| 14 | ): ColineCapabilities { |
| 15 | const gate = createCapabilityGate(config); |
| 16 | function wrap(value: object, path: string): object { |
| 17 | return new Proxy(value, { |
| 18 | get(target, key, receiver): unknown { |
| 19 | const child: unknown = Reflect.get(target, key, receiver); |
| 20 | if (typeof key !== "string") return child; |
| 21 | const operation = path ? `${path}.${key}` : key; |
| 22 | if (typeof child === "function") { |
| 23 | if (operation === "storage.collection") { |
| 24 | return (...args: unknown[]) => |
| 25 | wrap(Reflect.apply(child, target, args) as object, "storage.records"); |
| 26 | } |
| 27 | return async (...args: unknown[]) => { |
| 28 | gate.assert(operation); |
| 29 | if (operation === "ai.run" || operation === "ai.images.generate") { |
| 30 | const request = |
| 31 | operation === "ai.run" |
| 32 | ? (args[0] as ColineAiRequest) |
| 33 | : { kind: "image" as const, input: args[0] as ColineImageOptions }; |
| 34 | for (const required of requiredAiOperations(request)) gate.assert(required); |
| 35 | } |
| 36 | if ( |
| 37 | !options.interactive && |
| 38 | (operation.startsWith("pickers.") || |
| 39 | operation.startsWith("navigation.") || |
| 40 | operation === "realtime.subscribe" || |
| 41 | operation === "tools.invoke") |
| 42 | ) { |
| 43 | throw Object.assign(new Error(`${operation} requires an interactive surface.`), { |
| 44 | code: "capability_unavailable", |
| 45 | }); |
| 46 | } |
| 47 | if ( |
| 48 | options.appPrincipal && |
| 49 | /^(files|members|search\.query|tasks|calendar|messages|references)\./.test( |
| 50 | `${operation}.`, |
| 51 | ) |
| 52 | ) { |
| 53 | throw Object.assign(new Error(`${operation} requires a user principal.`), { |
| 54 | code: "requires_user_principal", |
| 55 | }); |
| 56 | } |
| 57 | return Reflect.apply(child, target, args) as unknown; |
| 58 | }; |
| 59 | } |
| 60 | return child !== null && typeof child === "object" ? wrap(child, operation) : child; |
| 61 | }, |
| 62 | }); |
| 63 | } |
| 64 | return wrap(capabilities, "") as ColineCapabilities; |
| 65 | } |
| 66 |