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.
| * |
| 6 | * - `tree` — tier 1: app logic returns a declarative UI tree; the host |
| 7 | * renders it with its own components on every platform. |
| 8 | * - `react` — tier 2: a Coline-built React bundle runs in the sandboxed |
| 9 | * client runtime. `entry` is the module path within the app |
| 10 | * source. |
| 11 | * - `web` — tier 3: an iframe served from the app's external origin. |
| 12 | * Requires external hosting; excluded from mobile and preview |
| 13 | * composition. |
| 14 | */ |
| 15 |
| 16 | const surfaceEntrySchema = z.string().trim().min(1).max(500); |
| 17 | const surfacePathSchema = z |
| 18 | .string() |
| 19 | .trim() |
| 20 | .min(1) |
| 21 | .max(2000) |
| 22 | .startsWith("/", "Web surface paths must be origin-relative (start with /)."); |
| 23 |
| 24 | export const treeSurfaceSchema = z.object({ |
| 25 | tier: z.literal("tree"), |
| 26 | }); |
| 27 |
| 28 | export const reactSurfaceSchema = z.object({ |
| 29 | tier: z.literal("react"), |
| 30 | entry: surfaceEntrySchema, |
| 31 | }); |
| 32 |
| 33 | export const webSurfaceSchema = z.object({ |
| 34 | tier: z.literal("web"), |
| 35 | path: surfacePathSchema, |
| 36 | }); |
| 37 |
| 38 | export const surfaceVariantSchema = z.discriminatedUnion("tier", [ |
| 39 | treeSurfaceSchema, |
| 40 | reactSurfaceSchema, |
| 41 | webSurfaceSchema, |
| 42 | ]); |
| 43 | export type SurfaceVariant = z.infer<typeof surfaceVariantSchema>; |
| 44 | export type SurfaceTier = SurfaceVariant["tier"]; |
| 45 |
| 46 | /** |
| 47 | * A surface with an optional mobile override (plan §13): universal by |
| 48 | * default; specialize only when the interaction genuinely differs. |
| 49 | */ |
| 50 | export const appSurfaceSchema = z.discriminatedUnion("tier", [ |
| 51 | treeSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 52 | reactSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 53 | webSurfaceSchema.extend({ mobile: surfaceVariantSchema.optional() }), |
| 54 | ]); |
| 55 | export type AppSurface = z.infer<typeof appSurfaceSchema>; |
| 56 |
| 57 | /** |
| 58 | * The site surface (SDK 2.1, Coline Sites): the app served as a website on |
| 59 | * its own origin, `{slug}.coline.space` or a custom domain. |
| 60 | * |
| 61 | * The page is a client-rendered React bundle built from `entry`. Coline |
| 62 | * serves an HTML shell per route with the route's `head`, the bundle as a |
| 63 | * content-hashed script, and the files under `assets` at the site root. |
| 64 | * |
| 65 | * Visitors are a principal of their own, never members and never the app. |
| 66 | * They reach data only through collections whose `access` opens them (see |
| 67 | * `collections.ts`), and logic only through the tools listed in `tools`, |
| 68 | * which run at most at the `write` effect. |
| 69 | */ |
| 70 | const SITE_ROUTE_SEGMENT = String.raw`(?:[A-Za-z0-9._~-]+|:[a-z][A-Za-z0-9_]*)`; |
| 71 | export const siteRoutePathSchema = z |
| 72 | .string() |
| 73 | .trim() |
| 74 | .max(300) |
| 75 | .regex( |
| 76 | new RegExp(`^/(?:${SITE_ROUTE_SEGMENT}(?:/${SITE_ROUTE_SEGMENT})*)?(?:/\\*)?$|^/\\*$`), |
| 77 | 'Route paths start with "/", use plain segments or ":params", and may end with "/*".', |
| 78 | ); |
| 79 |
| 80 | const siteImageSchema = z |
| 81 | .string() |
| 82 | .trim() |
| 83 | .max(2000) |
| 84 | .refine( |
| 85 | (value) => value.startsWith("/") || value.startsWith("https://"), |
| 86 | "Images are a path under the site's assets or an https URL.", |
| 87 | ); |
| 88 |
| 89 | export const siteHeadSchema = z.strictObject({ |
| 90 | title: z.string().trim().min(1).max(120).optional(), |
| 91 | description: z.string().trim().max(500).optional(), |
| 92 | /** Link preview image. */ |
| 93 | image: siteImageSchema.optional(), |
| 94 | }); |
| 95 | export type SiteHead = z.infer<typeof siteHeadSchema>; |
| 96 |
| 97 | export const siteRouteSchema = z.strictObject({ |
| 98 | path: siteRoutePathSchema, |
| 99 | head: siteHeadSchema.optional(), |
| 100 | }); |
| 101 | export type SiteRoute = z.infer<typeof siteRouteSchema>; |
| 102 |
| 103 | /** How visitors sign in. Google needs no Coline account; Coline accounts can offer Google, Microsoft and email. */ |
| 104 | export const SITE_SIGN_IN_PROVIDERS = ["google", "coline"] as const; |
| 105 | export type SiteSignInProvider = (typeof SITE_SIGN_IN_PROVIDERS)[number]; |
| 106 |
| 107 | /** |
| 108 | * Who may use the site's data and tools: |
| 109 | * - `open`: everyone, guests included (the default); |
| 110 | * - `signedIn`: signed-in visitors only; guests can load pages and sign in; |
| 111 | * - `approved`: signed-in visitors the team approved in the console. |
| 112 | */ |
| 113 | export const SITE_VISITOR_ACCESS = ["open", "signedIn", "approved"] as const; |
| 114 | export type SiteVisitorAccess = (typeof SITE_VISITOR_ACCESS)[number]; |
| 115 |
| 116 | const MB = 1024 * 1024; |
| 117 | const mimePatternSchema = z |
| 118 | .string() |
| 119 | .trim() |
| 120 | .toLowerCase() |
| 121 | .regex(/^(?:[a-z0-9][a-z0-9.+-]*\/(?:\*|[a-z0-9][a-z0-9.+-]*))$/, 'Types are MIME types like "image/png" or "image/*".'); |
| 122 |
| 123 | export const siteUploadsSchema = z.strictObject({ |
| 124 | /** Who may upload. Sites deployed without an account never accept uploads. */ |
| 125 | who: z.enum(["visitor", "signedIn"]).default("signedIn"), |
| 126 | /** Largest file, in bytes; the plan caps it (10 MB free, 25 MB paid). */ |
| 127 | maxFileBytes: z.number().int().min(1024).max(25 * MB).default(10 * MB), |
| 128 | /** Accepted types. Scripts, HTML and executables are always refused. */ |
| 129 | types: z.array(mimePatternSchema).min(1).max(30).default(["image/*", "application/pdf", "text/plain"]), |
| 130 | }); |
| 131 | export type SiteUploads = z.infer<typeof siteUploadsSchema>; |
| 132 |
| 133 | /** An app endpoint served at a path on the site's own address. */ |
| 134 | export const siteEndpointRouteSchema = z.strictObject({ |
| 135 | path: siteRoutePathSchema.refine((path) => !path.startsWith("/_coline"), "Paths under /_coline are reserved."), |
| 136 | /** The `endpoints[].id` that handles it. */ |
| 137 | endpoint: z.string().trim().min(1).max(80), |
| 138 | }); |
| 139 | export type SiteEndpointRoute = z.infer<typeof siteEndpointRouteSchema>; |
| 140 |
| 141 | export const siteSurfaceSchema = z |
| 142 | .strictObject({ |
| 143 | /** Module path of the site's React entry, relative to the app root. */ |
| 144 | entry: surfaceEntrySchema, |
| 145 | /** Directory of static files served at the site root (favicon, images, fonts). */ |
| 146 | assets: surfaceEntrySchema.optional(), |
| 147 | /** Site name and default head for routes that set none. */ |
| 148 | title: z.string().trim().min(1).max(120).optional(), |
| 149 | description: z.string().trim().max(500).optional(), |
| 150 | image: siteImageSchema.optional(), |
| 151 | /** Page language, for the `lang` attribute. */ |
| 152 | lang: z.string().trim().regex(/^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/).default("en"), |
| 153 | routes: z.array(siteRouteSchema).min(1).max(200).default([{ path: "/" }]), |
| 154 | visitors: z |
| 155 | .strictObject({ |
| 156 | /** Offer sign-in. Off by default: everyone is a guest. */ |
| 157 | signIn: z.boolean().default(false), |
| 158 | /** Which sign-in options to offer, in order. */ |
| 159 | providers: z |
| 160 | .array(z.enum(SITE_SIGN_IN_PROVIDERS)) |
| 161 | .min(1) |
| 162 | .max(SITE_SIGN_IN_PROVIDERS.length) |
| 163 | .default(["google", "coline"]) |
| 164 | .refine((providers) => new Set(providers).size === providers.length, "List each provider once."), |
| 165 | access: z.enum(SITE_VISITOR_ACCESS).default("open"), |
| 166 | /** |
| 167 | * A tool to run, as the app, after a guest signs in, with input |
| 168 | * `{ visitorId, previousVisitorId }`. Records the guest created have |
| 169 | * already moved to the signed-in visitor; use this to merge anything else. |
| 170 | */ |
| 171 | onSignIn: z.string().trim().min(1).max(160).optional(), |
| 172 | }) |
| 173 | .default({ signIn: false, providers: ["google", "coline"], access: "open" }), |
| 174 | /** Tools visitors may run. Each runs at most at the `write` effect. */ |
| 175 | tools: z.array(z.string().trim().min(1).max(160)).max(20).default([]), |
| 176 | /** Let visitors upload files. Off unless declared. */ |
| 177 | uploads: siteUploadsSchema.optional(), |
| 178 | /** App endpoints served on the site's own address, such as `/api/summary`. */ |
| 179 | endpoints: z.array(siteEndpointRouteSchema).max(20).default([]), |
| 180 | /** |
| 181 | * Register the listed tools with in-browser agents (WebMCP), so an agent |
| 182 | * in the visitor's tab can call them as the visitor. |
| 183 | */ |
| 184 | agentTools: z.boolean().default(false), |
| 185 | }) |
| 186 | .refine( |
| 187 | (site) => new Set(site.routes.map((route) => route.path)).size === site.routes.length, |
| 188 | "Route paths must be unique.", |
| 189 | ) |
| 190 | .refine( |
| 191 | (site) => new Set(site.endpoints.map((route) => route.path)).size === site.endpoints.length, |
| 192 | "Endpoint paths must be unique.", |
| 193 | ) |
| 194 | .refine( |
| 195 | (site) => site.visitors.access === "open" || site.visitors.signIn, |
| 196 | "Requiring sign-in or approval needs visitors.signIn: true.", |
| 197 | ); |
| 198 | export type SiteSurface = z.infer<typeof siteSurfaceSchema>; |
| 199 | export type SiteSurfaceInput = z.input<typeof siteSurfaceSchema>; |
| 200 |
| 201 | export const appSurfacesSchema = z |
| 202 | .object({ |
| 203 | home: appSurfaceSchema.optional(), |
| 204 | site: siteSurfaceSchema.optional(), |
| 205 | }) |
| 206 | .default({}); |
| 207 | export type AppSurfaces = z.infer<typeof appSurfacesSchema>; |
| 208 |