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.
| 4 | import { appConnectionsSchema } from "./connections"; |
| 5 | import { appEndpointsSchema } from "./endpoints"; |
| 6 | import { appFileTypeV2Schema } from "./file-type"; |
| 7 | import { appHostingV2Schema, appNetworkPolicySchema } from "./hosting"; |
| 8 | import { appJobsSchema } from "./jobs"; |
| 9 | import { appNotificationsV2Schema } from "./notifications"; |
| 10 | import { appPermissionV2ListSchema } from "./permissions"; |
| 11 | import { appSurfacesSchema } from "./surfaces"; |
| 12 | import { appToolMetadataSchema } from "./tool"; |
| 13 | import { |
| 14 | appTriggersV2Schema, |
| 15 | isPlatformEventNamespace, |
| 16 | PLATFORM_EVENT_TYPES, |
| 17 | eventPatternMatches, |
| 18 | platformEventPermission, |
| 19 | } from "./triggers"; |
| 20 |
| 21 | function hostCoveredByAllowlist(host: string, allowedHosts: readonly string[]): boolean { |
| 22 | return allowedHosts.some((allowed) => { |
| 23 | if (allowed === host) return true; |
| 24 | if (!allowed.startsWith("*.")) return false; |
| 25 | const suffix = allowed.slice(1); |
| 26 | const candidate = host.startsWith("*.") ? host.slice(1) : host; |
| 27 | return candidate.endsWith(suffix) && candidate.length >= suffix.length; |
| 28 | }); |
| 29 | } |
| 30 |
| 31 | /** |
| 32 | * The Coline App manifest, v2 (plan §6). |
| 33 | * |
| 34 | * The manifest is the serializable artifact of an app definition: it tells |
| 35 | * Coline everything the app contributes without executing app code. It is |
| 36 | * produced by the extraction step (`define/extraction.ts`), re-verified at |
| 37 | * publish time, and stored on `app_versions`. |
| 38 | */ |
| 39 |
| 40 | export const appKeySchema = z |
| 41 | .string() |
| 42 | .trim() |
| 43 | .regex(/^[a-z][a-z0-9-]{1,63}$/, "App keys are lowercase kebab-case, 2-64 chars."); |
| 44 |
| 45 | /** |
| 46 | * SDK 2.1 retired 0.6 public pages. Versions stored before then still carry |
| 47 | * `public.pages`, `surfaces.public` and a `public` hosting override; drop |
| 48 | * them so the rest of those apps (home, tools, jobs) keeps working. |
| 49 | */ |
| 50 | function withoutRetiredPublicPages(input: unknown): unknown { |
| 51 | if (!isPlainObject(input)) return input; |
| 52 | const next: Record<string, unknown> = { ...input }; |
| 53 | if (Array.isArray(next.permissions)) { |
| 54 | next.permissions = next.permissions.filter((permission) => permission !== "public.pages"); |
| 55 | } |
| 56 | if (isPlainObject(next.surfaces) && "public" in next.surfaces) { |
| 57 | const { public: _retired, ...surfaces } = next.surfaces; |
| 58 | next.surfaces = surfaces; |
| 59 | } |
| 60 | if (isPlainObject(next.hosting) && isPlainObject(next.hosting.overrides) && "public" in next.hosting.overrides) { |
| 61 | const { public: _retired, ...overrides } = next.hosting.overrides; |
| 62 | next.hosting = { ...next.hosting, overrides }; |
| 63 | } |
| 64 | return next; |
| 65 | } |
| 66 |
| 67 | function isPlainObject(value: unknown): value is Record<string, unknown> { |
| 68 | return typeof value === "object" && value !== null && !Array.isArray(value); |
| 69 | } |
| 70 |
| 71 | const manifestObjectSchema = z |
| 72 | .strictObject({ |
| 73 | key: appKeySchema, |
| 74 | name: z.string().trim().min(1).max(120), |
| 75 | description: z.string().trim().max(1000).optional(), |
| 76 | iconUrl: z.url().max(2000).optional(), |
| 77 | icon: z.string().trim().max(120).optional(), |
| 78 | permissions: appPermissionV2ListSchema, |
| 79 | surfaces: appSurfacesSchema, |
| 80 | files: z |
| 81 | .array(appFileTypeV2Schema) |
| 82 | .max(50) |
| 83 | .default([]) |
| 84 | .refine( |
| 85 | (files) => new Set(files.map((file) => file.typeKey)).size === files.length, |
| 86 | "File type keys must be unique.", |
| 87 | ), |
| 88 | tools: z |
| 89 | .array(appToolMetadataSchema) |
| 90 | .max(100) |
| 91 | .default([]) |
| 92 | .refine( |
| 93 | (tools) => new Set(tools.map((tool) => tool.name)).size === tools.length, |
| 94 | "Tool names must be unique.", |
| 95 | ), |
| 96 | commands: z |
| 97 | .array(appCommandSchema) |
| 98 | .max(50) |
| 99 | .default([]) |
| 100 | .refine( |
| 101 | (commands) => new Set(commands.map((command) => command.id)).size === commands.length, |
| 102 | "Command ids must be unique.", |
| 103 | ), |
| 104 | notifications: appNotificationsV2Schema, |
| 105 | hosting: appHostingV2Schema, |
| 106 | network: appNetworkPolicySchema, |
| 107 | kairo: z |
| 108 | .strictObject({ |
| 109 | enabled: z.boolean().default(true), |
| 110 | description: z.string().trim().min(1).max(500).optional(), |
| 111 | instructions: z.string().trim().min(1).max(4000).optional(), |
| 112 | }) |
| 113 | .optional(), |
| 114 | triggers: appTriggersV2Schema, |
| 115 | endpoints: appEndpointsSchema, |
| 116 | jobs: appJobsSchema, |
| 117 | connections: appConnectionsSchema, |
| 118 | collections: appCollectionsSchema, |
| 119 | }) |
| 120 | .superRefine((manifest, ctx) => { |
| 121 | const prefix = `${manifest.key}.`; |
| 122 | const requirePermission = ( |
| 123 | permission: (typeof manifest.permissions)[number], |
| 124 | reason: string, |
| 125 | ) => { |
| 126 | if (!manifest.permissions.includes(permission)) { |
| 127 | ctx.addIssue({ |
| 128 | code: "custom", |
| 129 | path: ["permissions"], |
| 130 | message: `${reason} requires the "${permission}" permission.`, |
| 131 | }); |
| 132 | } |
| 133 | }; |
| 134 |
| 135 | if (manifest.endpoints.length > 0) requirePermission("endpoints.receive", "Declaring endpoints"); |
| 136 | if (manifest.jobs.length > 0) requirePermission("jobs.run", "Declaring background jobs"); |
| 137 | if (manifest.connections.length > 0) { |
| 138 | requirePermission("connections.use", "Declaring account connections"); |
| 139 | requirePermission("network.external", "Declaring account connections"); |
| 140 | } |
| 141 | for (const [index, connection] of manifest.connections.entries()) { |
| 142 | for (const host of connection.hosts) { |
| 143 | if (!hostCoveredByAllowlist(host, manifest.network.allowedHosts)) { |
| 144 | ctx.addIssue({ |
| 145 | code: "custom", |
| 146 | path: ["connections", index, "hosts"], |
| 147 | message: `Connection host "${host}" must also be listed in network.allowedHosts.`, |
| 148 | }); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 |
| 153 | const siteSurface = manifest.surfaces.site; |
| 154 | if (siteSurface) { |
| 155 | requirePermission("site.serve", "A site surface"); |
| 156 | const declaredTools = new Set(manifest.tools.map((tool) => tool.name)); |
| 157 | for (const [index, tool] of siteSurface.tools.entries()) { |
| 158 | if (!declaredTools.has(tool)) { |
| 159 | ctx.addIssue({ |
| 160 | code: "custom", |
| 161 | path: ["surfaces", "site", "tools", index], |
| 162 | message: `The site lists undeclared tool "${tool}".`, |
| 163 | }); |
| 164 | } |
| 165 | } |
| 166 | const onSignIn = siteSurface.visitors.onSignIn; |
| 167 | if (onSignIn && !declaredTools.has(onSignIn)) { |
| 168 | ctx.addIssue({ |
| 169 | code: "custom", |
| 170 | path: ["surfaces", "site", "visitors", "onSignIn"], |
| 171 | message: `onSignIn names undeclared tool "${onSignIn}".`, |
| 172 | }); |
| 173 | } |
| 174 | if (siteSurface.uploads) requirePermission("storage.app", "Accepting uploads"); |
| 175 | const endpointIds = new Set(manifest.endpoints.map((endpoint) => endpoint.id)); |
| 176 | for (const [index, route] of siteSurface.endpoints.entries()) { |
| 177 | if (!endpointIds.has(route.endpoint)) { |
| 178 | ctx.addIssue({ |
| 179 | code: "custom", |
| 180 | path: ["surfaces", "site", "endpoints", index, "endpoint"], |
| 181 | message: `The site routes ${route.path} to undeclared endpoint "${route.endpoint}".`, |
| 182 | }); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | const openCollections = visitorReachableCollections(manifest.collections); |
| 187 | if (openCollections.length > 0) { |
| 188 | requirePermission("storage.app", "Opening collections to visitors"); |
| 189 | if (!siteSurface) { |
| 190 | ctx.addIssue({ |
| 191 | code: "custom", |
| 192 | path: ["collections"], |
| 193 | message: `Collections open to visitors (${openCollections.join(", ")}) need a site surface.`, |
| 194 | }); |
| 195 | } |
| 196 | } |
| 197 |
| 198 | for (const [index, subscription] of manifest.triggers.webhooks.entries()) { |
| 199 | const pattern = subscription.event; |
| 200 | if (!isPlatformEventNamespace(pattern)) continue; |
| 201 | const matches = PLATFORM_EVENT_TYPES.filter((type) => eventPatternMatches(pattern, type)); |
| 202 | if (matches.length === 0) { |
| 203 | ctx.addIssue({ |
| 204 | code: "custom", |
| 205 | path: ["triggers", "webhooks", index, "event"], |
| 206 | message: `"${pattern}" is not a Coline event. Known events: ${PLATFORM_EVENT_TYPES.join(", ")}.`, |
| 207 | }); |
| 208 | continue; |
| 209 | } |
| 210 | for (const type of matches) { |
| 211 | const permission = platformEventPermission(type); |
| 212 | if (permission && !manifest.permissions.includes(permission)) { |
| 213 | ctx.addIssue({ |
| 214 | code: "custom", |
| 215 | path: ["triggers", "webhooks", index, "event"], |
| 216 | message: `Receiving "${type}" requires the "${permission}" permission.`, |
| 217 | }); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 |
| 223 | for (const [index, tool] of manifest.tools.entries()) { |
| 224 | if (!tool.name.startsWith(prefix)) { |
| 225 | ctx.addIssue({ |
| 226 | code: "custom", |
| 227 | path: ["tools", index, "name"], |
| 228 | message: `Tool names must be namespaced under the app key ("${prefix}…").`, |
| 229 | }); |
| 230 | } |
| 231 | } |
| 232 |
| 233 | for (const [index, file] of manifest.files.entries()) { |
| 234 | if (!file.typeKey.startsWith(prefix)) { |
| 235 | ctx.addIssue({ |
| 236 | code: "custom", |
| 237 | path: ["files", index, "typeKey"], |
| 238 | message: `File type keys must be namespaced under the app key ("${prefix}…").`, |
| 239 | }); |
| 240 | } |
| 241 | } |
| 242 |
| 243 | const toolNames = new Set(manifest.tools.map((tool) => tool.name)); |
| 244 | for (const [index, command] of manifest.commands.entries()) { |
| 245 | if (command.target.kind === "tool" && !toolNames.has(command.target.tool)) { |
| 246 | ctx.addIssue({ |
| 247 | code: "custom", |
| 248 | path: ["commands", index, "target", "tool"], |
| 249 | message: `Command targets undeclared tool "${command.target.tool}".`, |
| 250 | }); |
| 251 | } |
| 252 | } |
| 253 |
| 254 | if (manifest.tools.length > 0 && !manifest.permissions.includes("ai.tools")) { |
| 255 | ctx.addIssue({ |
| 256 | code: "custom", |
| 257 | path: ["permissions"], |
| 258 | message: 'Declaring tools requires the "ai.tools" permission.', |
| 259 | }); |
| 260 | } |
| 261 | if (manifest.commands.length > 0 && !manifest.permissions.includes("commands.register")) { |
| 262 | ctx.addIssue({ |
| 263 | code: "custom", |
| 264 | path: ["permissions"], |
| 265 | message: 'Declaring commands requires the "commands.register" permission.', |
| 266 | }); |
| 267 | } |
| 268 | if ( |
| 269 | manifest.triggers.webhooks.length > 0 && |
| 270 | !manifest.permissions.includes("webhooks.subscribe") |
| 271 | ) { |
| 272 | ctx.addIssue({ |
| 273 | code: "custom", |
| 274 | path: ["permissions"], |
| 275 | message: 'Declaring webhook triggers requires the "webhooks.subscribe" permission.', |
| 276 | }); |
| 277 | } |
| 278 | if (manifest.notifications.channels.length > 0 && !manifest.permissions.includes("notifications.write")) { |
| 279 | ctx.addIssue({ |
| 280 | code: "custom", |
| 281 | path: ["permissions"], |
| 282 | message: 'Declaring notification channels requires the "notifications.write" permission.', |
| 283 | }); |
| 284 | } |
| 285 |
| 286 | const fileTypeKeys = new Set(manifest.files.map((file) => file.typeKey)); |
| 287 | const validOverrideKeys = new Set<string>([ |
| 288 | "home", |
| 289 | "events", |
| 290 | ...toolNames, |
| 291 | ...fileTypeKeys, |
| 292 | ...manifest.endpoints.map((endpoint) => `endpoint:${endpoint.id}`), |
| 293 | ...manifest.jobs.map((job) => `job:${job.name}`), |
| 294 | ]); |
| 295 | for (const key of Object.keys(manifest.hosting.overrides)) { |
| 296 | if (!validOverrideKeys.has(key)) { |
| 297 | ctx.addIssue({ |
| 298 | code: "custom", |
| 299 | path: ["hosting", "overrides", key], |
| 300 | message: |
| 301 | 'Hosting override keys must name a declared tool, file type, `endpoint:<id>`, `job:<name>`, "home", or "events".', |
| 302 | }); |
| 303 | } |
| 304 | } |
| 305 |
| 306 | const webSurfaceTiers: string[] = []; |
| 307 | if (manifest.surfaces.home?.tier === "web") webSurfaceTiers.push("surfaces.home"); |
| 308 | for (const file of manifest.files) { |
| 309 | if (file.surfaces.editor?.tier === "web") { |
| 310 | webSurfaceTiers.push(`files.${file.typeKey}.editor`); |
| 311 | } |
| 312 | } |
| 313 | if (webSurfaceTiers.length > 0 && !manifest.hosting.external) { |
| 314 | ctx.addIssue({ |
| 315 | code: "custom", |
| 316 | path: ["hosting", "external"], |
| 317 | message: `Web-tier surfaces (${webSurfaceTiers.join(", ")}) require external hosting.`, |
| 318 | }); |
| 319 | } |
| 320 | }); |
| 321 |
| 322 | export const colineAppManifestV2Schema = z.preprocess(withoutRetiredPublicPages, manifestObjectSchema); |
| 323 |
| 324 | export type ColineAppManifestV2 = z.infer<typeof manifestObjectSchema>; |
| 325 | export type ColineAppManifestV2Input = z.input<typeof manifestObjectSchema>; |
| 326 |
| 327 | /** Registration payload: what `coline apps publish` submits (plan §5.3, §6.2). */ |
| 328 | export const colineAppRegistrationPayloadV2Schema = z.strictObject({ |
| 329 | protocolVersion: z.literal(2), |
| 330 | manifest: colineAppManifestV2Schema, |
| 331 | }); |
| 332 | export type ColineAppRegistrationPayloadV2 = z.infer< |
| 333 | typeof colineAppRegistrationPayloadV2Schema |
| 334 | >; |
| 335 |