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