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.
| type: z.string().trim().min(1).max(120), |
| 5 | payload: z.record(z.string(), z.unknown()).optional(), |
| 6 | }); |
| 7 |
| 8 | export type ColineUiAction = z.infer<typeof uiActionSchema>; |
| 9 |
| 10 | const uiTextNodeSchema = z.object({ |
| 11 | type: z.literal("Text"), |
| 12 | props: z.object({ |
| 13 | value: z.string(), |
| 14 | tone: z.enum(["default", "muted", "positive", "warning", "danger"]).optional(), |
| 15 | }), |
| 16 | }); |
| 17 |
| 18 | const uiHeadingNodeSchema = z.object({ |
| 19 | type: z.literal("Heading"), |
| 20 | props: z.object({ |
| 21 | value: z.string().min(1).max(500), |
| 22 | level: z.int().min(1).max(4).default(2), |
| 23 | }), |
| 24 | }); |
| 25 |
| 26 | const uiBadgeNodeSchema = z.object({ |
| 27 | type: z.literal("Badge"), |
| 28 | props: z.object({ |
| 29 | value: z.string().min(1).max(120), |
| 30 | tone: z.enum(["default", "muted", "positive", "warning", "danger"]).default("default"), |
| 31 | }), |
| 32 | }); |
| 33 |
| 34 | const uiButtonNodeSchema = z.object({ |
| 35 | type: z.literal("Button"), |
| 36 | props: z.object({ |
| 37 | label: z.string().min(1).max(120), |
| 38 | variant: z.enum(["primary", "secondary", "ghost", "danger"]).default("primary"), |
| 39 | action: uiActionSchema, |
| 40 | }), |
| 41 | }); |
| 42 |
| 43 | const uiDividerNodeSchema = z.object({ |
| 44 | type: z.literal("Divider"), |
| 45 | props: z.object({}).default({}), |
| 46 | }); |
| 47 |
| 48 | const uiFileCardNodeSchema = z.object({ |
| 49 | type: z.literal("FileCard"), |
| 50 | props: z.object({ |
| 51 | title: z.string().min(1).max(200), |
| 52 | subtitle: z.string().max(500).optional(), |
| 53 | fileId: z.string().trim().min(1).max(200).optional(), |
| 54 | action: uiActionSchema.optional(), |
| 55 | }), |
| 56 | }); |
| 57 |
| 58 | const uiUserChipNodeSchema = z.object({ |
| 59 | type: z.literal("UserChip"), |
| 60 | props: z.object({ |
| 61 | label: z.string().min(1).max(120), |
| 62 | userId: z.string().trim().min(1).max(200).optional(), |
| 63 | }), |
| 64 | }); |
| 65 |
| 66 | const uiEmptyStateNodeSchema = z.object({ |
| 67 | type: z.literal("EmptyState"), |
| 68 | props: z.object({ |
| 69 | title: z.string().min(1).max(160), |
| 70 | description: z.string().max(1000).optional(), |
| 71 | action: uiActionSchema.optional(), |
| 72 | }), |
| 73 | }); |
| 74 |
| 75 | const uiImageNodeSchema = z.object({ |
| 76 | type: z.literal("Image"), |
| 77 | props: z.object({ |
| 78 | src: z.string().url().max(2000), |
| 79 | alt: z.string().max(500).default(""), |
| 80 | width: z.number().int().min(1).max(4096).optional(), |
| 81 | height: z.number().int().min(1).max(4096).optional(), |
| 82 | }), |
| 83 | }); |
| 84 |
| 85 | const uiLinkNodeSchema = z.object({ |
| 86 | type: z.literal("Link"), |
| 87 | props: z.object({ |
| 88 | label: z.string().min(1).max(500), |
| 89 | href: z.string().url().max(2000), |
| 90 | external: z.boolean().default(false), |
| 91 | }), |
| 92 | }); |
| 93 |
| 94 | const uiCodeBlockNodeSchema = z.object({ |
| 95 | type: z.literal("CodeBlock"), |
| 96 | props: z.object({ |
| 97 | code: z.string().max(50000), |
| 98 | language: z.string().max(50).optional(), |
| 99 | }), |
| 100 | }); |
| 101 |
| 102 | const uiInputNodeSchema = z.object({ |
| 103 | type: z.literal("Input"), |
| 104 | props: z.object({ |
| 105 | name: z.string().min(1).max(120), |
| 106 | label: z.string().max(200).optional(), |
| 107 | placeholder: z.string().max(200).optional(), |
| 108 | defaultValue: z.string().max(5000).optional(), |
| 109 | type: z.enum(["text", "number", "email", "url", "textarea"]).default("text"), |
| 110 | required: z.boolean().default(false), |
| 111 | }), |
| 112 | }); |
| 113 |
| 114 | const uiSelectNodeSchema = z.object({ |
| 115 | type: z.literal("Select"), |
| 116 | props: z.object({ |
| 117 | name: z.string().min(1).max(120), |
| 118 | label: z.string().max(200).optional(), |
| 119 | options: z.array( |
| 120 | z.object({ |
| 121 | value: z.string().min(1).max(200), |
| 122 | label: z.string().min(1).max(200), |
| 123 | }), |
| 124 | ).min(1).max(100), |
| 125 | defaultValue: z.string().max(200).optional(), |
| 126 | required: z.boolean().default(false), |
| 127 | }), |
| 128 | }); |
| 129 |
| 130 | const uiTableNodeSchema = z.object({ |
| 131 | type: z.literal("Table"), |
| 132 | props: z.object({ |
| 133 | columns: z.array( |
| 134 | z.object({ |
| 135 | key: z.string().min(1).max(120), |
| 136 | label: z.string().min(1).max(200), |
| 137 | align: z.enum(["left", "center", "right"]).default("left"), |
| 138 | }), |
| 139 | ).min(1).max(20), |
| 140 | rows: z.array( |
| 141 | z.record(z.string(), z.string()), |
| 142 | ).max(200), |
| 143 | }), |
| 144 | }); |
| 145 |
| 146 | type ColineUiNodeBase = |
| 147 | | z.infer<typeof uiTextNodeSchema> |
| 148 | | z.infer<typeof uiHeadingNodeSchema> |
| 149 | | z.infer<typeof uiBadgeNodeSchema> |
| 150 | | z.infer<typeof uiButtonNodeSchema> |
| 151 | | z.infer<typeof uiDividerNodeSchema> |
| 152 | | z.infer<typeof uiFileCardNodeSchema> |
| 153 | | z.infer<typeof uiUserChipNodeSchema> |
| 154 | | z.infer<typeof uiEmptyStateNodeSchema> |
| 155 | | z.infer<typeof uiImageNodeSchema> |
| 156 | | z.infer<typeof uiLinkNodeSchema> |
| 157 | | z.infer<typeof uiCodeBlockNodeSchema> |
| 158 | | z.infer<typeof uiInputNodeSchema> |
| 159 | | z.infer<typeof uiSelectNodeSchema> |
| 160 | | z.infer<typeof uiTableNodeSchema>; |
| 161 |
| 162 | export type ColineUiNode = ColineUiNodeBase | ColineUiStackNode; |
| 163 |
| 164 | export interface ColineUiStackNode { |
| 165 | type: "Stack"; |
| 166 | props: { |
| 167 | direction?: "vertical" | "horizontal"; |
| 168 | gap?: "xs" | "sm" | "md" | "lg"; |
| 169 | children: ColineUiNode[]; |
| 170 | }; |
| 171 | } |
| 172 |
| 173 | const uiNodeLazySchema: z.ZodType<ColineUiNode> = z.lazy(() => |
| 174 | z.union([ |
| 175 | uiTextNodeSchema, |
| 176 | uiHeadingNodeSchema, |
| 177 | uiBadgeNodeSchema, |
| 178 | uiButtonNodeSchema, |
| 179 | uiDividerNodeSchema, |
| 180 | uiFileCardNodeSchema, |
| 181 | uiUserChipNodeSchema, |
| 182 | uiEmptyStateNodeSchema, |
| 183 | uiImageNodeSchema, |
| 184 | uiLinkNodeSchema, |
| 185 | uiCodeBlockNodeSchema, |
| 186 | uiInputNodeSchema, |
| 187 | uiSelectNodeSchema, |
| 188 | uiTableNodeSchema, |
| 189 | z.object({ |
| 190 | type: z.literal("Stack"), |
| 191 | props: z.object({ |
| 192 | direction: z.enum(["vertical", "horizontal"]).default("vertical"), |
| 193 | gap: z.enum(["xs", "sm", "md", "lg"]).default("md"), |
| 194 | children: z.array(uiNodeLazySchema), |
| 195 | }), |
| 196 | }), |
| 197 | ]), |
| 198 | ); |
| 199 |
| 200 | export const colineUiNodeSchema = uiNodeLazySchema; |
| 201 | export const colineUiActionSchema = uiActionSchema; |
| 202 |
| 203 | export const actions = { |
| 204 | openFile(fileId: string, payload: Record<string, unknown> = {}): ColineUiAction { |
| 205 | return colineUiActionSchema.parse({ |
| 206 | type: "open_file", |
| 207 | payload: { |
| 208 | fileId, |
| 209 | ...payload, |
| 210 | }, |
| 211 | }); |
| 212 | }, |
| 213 |
| 214 | openAppHome(payload: Record<string, unknown> = {}): ColineUiAction { |
| 215 | return colineUiActionSchema.parse({ |
| 216 | type: "open_app_home", |
| 217 | payload, |
| 218 | }); |
| 219 | }, |
| 220 |
| 221 | navigate(href: string, payload: Record<string, unknown> = {}): ColineUiAction { |
| 222 | return colineUiActionSchema.parse({ |
| 223 | type: "navigate", |
| 224 | payload: { |
| 225 | href, |
| 226 | ...payload, |
| 227 | }, |
| 228 | }); |
| 229 | }, |
| 230 |
| 231 | createFile(input: { |
| 232 | name: string; |
| 233 | typeKey: string; |
| 234 | driveId?: string; |
| 235 | parentId?: string | null; |
| 236 | document?: Record<string, unknown>; |
| 237 | }): ColineUiAction { |
| 238 | const payload: Record<string, unknown> = { |
| 239 | name: input.name, |
| 240 | typeKey: input.typeKey, |
| 241 | }; |
| 242 |
| 243 | if (input.driveId) { |
| 244 | payload["driveId"] = input.driveId; |
| 245 | } |
| 246 |
| 247 | if (input.parentId !== undefined) { |
| 248 | payload["parentId"] = input.parentId; |
| 249 | } |
| 250 |
| 251 | if (input.document) { |
| 252 | payload["document"] = input.document; |
| 253 | } |
| 254 |
| 255 | return colineUiActionSchema.parse({ |
| 256 | type: "create_file", |
| 257 | payload, |
| 258 | }); |
| 259 | }, |
| 260 |
| 261 | custom(type: string, payload: Record<string, unknown> = {}): ColineUiAction { |
| 262 | return colineUiActionSchema.parse({ |
| 263 | type, |
| 264 | payload, |
| 265 | }); |
| 266 | }, |
| 267 | }; |
| 268 |
| 269 | export const ui = { |
| 270 | text( |
| 271 | value: string, |
| 272 | options: { |
| 273 | tone?: "default" | "muted" | "positive" | "warning" | "danger"; |
| 274 | } = {}, |
| 275 | ): ColineUiNode { |
| 276 | return colineUiNodeSchema.parse({ |
| 277 | type: "Text", |
| 278 | props: { |
| 279 | value, |
| 280 | ...(options.tone ? { tone: options.tone } : {}), |
| 281 | }, |
| 282 | }); |
| 283 | }, |
| 284 |
| 285 | heading( |
| 286 | value: string, |
| 287 | options: { |
| 288 | level?: 1 | 2 | 3 | 4; |
| 289 | } = {}, |
| 290 | ): ColineUiNode { |
| 291 | return colineUiNodeSchema.parse({ |
| 292 | type: "Heading", |
| 293 | props: { |
| 294 | value, |
| 295 | ...(options.level ? { level: options.level } : {}), |
| 296 | }, |
| 297 | }); |
| 298 | }, |
| 299 |
| 300 | badge( |
| 301 | value: string, |
| 302 | options: { |
| 303 | tone?: "default" | "muted" | "positive" | "warning" | "danger"; |
| 304 | } = {}, |
| 305 | ): ColineUiNode { |
| 306 | return colineUiNodeSchema.parse({ |
| 307 | type: "Badge", |
| 308 | props: { |
| 309 | value, |
| 310 | ...(options.tone ? { tone: options.tone } : {}), |
| 311 | }, |
| 312 | }); |
| 313 | }, |
| 314 |
| 315 | button( |
| 316 | label: string, |
| 317 | options: { |
| 318 | variant?: "primary" | "secondary" | "ghost" | "danger"; |
| 319 | action: ColineUiAction; |
| 320 | }, |
| 321 | ): ColineUiNode { |
| 322 | return colineUiNodeSchema.parse({ |
| 323 | type: "Button", |
| 324 | props: { |
| 325 | label, |
| 326 | action: options.action, |
| 327 | ...(options.variant ? { variant: options.variant } : {}), |
| 328 | }, |
| 329 | }); |
| 330 | }, |
| 331 |
| 332 | divider(): ColineUiNode { |
| 333 | return colineUiNodeSchema.parse({ |
| 334 | type: "Divider", |
| 335 | props: {}, |
| 336 | }); |
| 337 | }, |
| 338 |
| 339 | fileCard(input: { |
| 340 | title: string; |
| 341 | subtitle?: string; |
| 342 | fileId?: string; |
| 343 | action?: ColineUiAction; |
| 344 | }): ColineUiNode { |
| 345 | return colineUiNodeSchema.parse({ |
| 346 | type: "FileCard", |
| 347 | props: { |
| 348 | title: input.title, |
| 349 | ...(input.subtitle ? { subtitle: input.subtitle } : {}), |
| 350 | ...(input.fileId ? { fileId: input.fileId } : {}), |
| 351 | ...(input.action ? { action: input.action } : {}), |
| 352 | }, |
| 353 | }); |
| 354 | }, |
| 355 |
| 356 | userChip(input: { |
| 357 | label: string; |
| 358 | userId?: string; |
| 359 | }): ColineUiNode { |
| 360 | return colineUiNodeSchema.parse({ |
| 361 | type: "UserChip", |
| 362 | props: { |
| 363 | label: input.label, |
| 364 | ...(input.userId ? { userId: input.userId } : {}), |
| 365 | }, |
| 366 | }); |
| 367 | }, |
| 368 |
| 369 | emptyState(input: { |
| 370 | title: string; |
| 371 | description?: string; |
| 372 | action?: ColineUiAction; |
| 373 | }): ColineUiNode { |
| 374 | return colineUiNodeSchema.parse({ |
| 375 | type: "EmptyState", |
| 376 | props: { |
| 377 | title: input.title, |
| 378 | ...(input.description ? { description: input.description } : {}), |
| 379 | ...(input.action ? { action: input.action } : {}), |
| 380 | }, |
| 381 | }); |
| 382 | }, |
| 383 |
| 384 | stack( |
| 385 | children: ColineUiNode[], |
| 386 | options: { |
| 387 | direction?: "vertical" | "horizontal"; |
| 388 | gap?: "xs" | "sm" | "md" | "lg"; |
| 389 | } = {}, |
| 390 | ): ColineUiNode { |
| 391 | return colineUiNodeSchema.parse({ |
| 392 | type: "Stack", |
| 393 | props: { |
| 394 | children, |
| 395 | ...(options.direction ? { direction: options.direction } : {}), |
| 396 | ...(options.gap ? { gap: options.gap } : {}), |
| 397 | }, |
| 398 | }); |
| 399 | }, |
| 400 |
| 401 | /** Alias for `stack(children, { direction: "horizontal", ...options })`. */ |
| 402 | row( |
| 403 | children: ColineUiNode[], |
| 404 | options: { |
| 405 | gap?: "xs" | "sm" | "md" | "lg"; |
| 406 | } = {}, |
| 407 | ): ColineUiNode { |
| 408 | return colineUiNodeSchema.parse({ |
| 409 | type: "Stack", |
| 410 | props: { |
| 411 | children, |
| 412 | direction: "horizontal", |
| 413 | ...(options.gap ? { gap: options.gap } : {}), |
| 414 | }, |
| 415 | }); |
| 416 | }, |
| 417 |
| 418 | image(input: { |
| 419 | src: string; |
| 420 | alt?: string; |
| 421 | width?: number; |
| 422 | height?: number; |
| 423 | }): ColineUiNode { |
| 424 | return colineUiNodeSchema.parse({ |
| 425 | type: "Image", |
| 426 | props: { |
| 427 | src: input.src, |
| 428 | ...(input.alt ? { alt: input.alt } : {}), |
| 429 | ...(input.width ? { width: input.width } : {}), |
| 430 | ...(input.height ? { height: input.height } : {}), |
| 431 | }, |
| 432 | }); |
| 433 | }, |
| 434 |
| 435 | link( |
| 436 | label: string, |
| 437 | href: string, |
| 438 | options: { external?: boolean } = {}, |
| 439 | ): ColineUiNode { |
| 440 | return colineUiNodeSchema.parse({ |
| 441 | type: "Link", |
| 442 | props: { |
| 443 | label, |
| 444 | href, |
| 445 | ...(options.external ? { external: true } : {}), |
| 446 | }, |
| 447 | }); |
| 448 | }, |
| 449 |
| 450 | codeBlock( |
| 451 | code: string, |
| 452 | options: { language?: string } = {}, |
| 453 | ): ColineUiNode { |
| 454 | return colineUiNodeSchema.parse({ |
| 455 | type: "CodeBlock", |
| 456 | props: { |
| 457 | code, |
| 458 | ...(options.language ? { language: options.language } : {}), |
| 459 | }, |
| 460 | }); |
| 461 | }, |
| 462 |
| 463 | input(input: { |
| 464 | name: string; |
| 465 | label?: string; |
| 466 | placeholder?: string; |
| 467 | defaultValue?: string; |
| 468 | type?: "text" | "number" | "email" | "url" | "textarea"; |
| 469 | required?: boolean; |
| 470 | }): ColineUiNode { |
| 471 | return colineUiNodeSchema.parse({ |
| 472 | type: "Input", |
| 473 | props: { |
| 474 | name: input.name, |
| 475 | ...(input.label ? { label: input.label } : {}), |
| 476 | ...(input.placeholder ? { placeholder: input.placeholder } : {}), |
| 477 | ...(input.defaultValue ? { defaultValue: input.defaultValue } : {}), |
| 478 | ...(input.type ? { type: input.type } : {}), |
| 479 | ...(input.required ? { required: true } : {}), |
| 480 | }, |
| 481 | }); |
| 482 | }, |
| 483 |
| 484 | select(input: { |
| 485 | name: string; |
| 486 | label?: string; |
| 487 | options: Array<{ value: string; label: string }>; |
| 488 | defaultValue?: string; |
| 489 | required?: boolean; |
| 490 | }): ColineUiNode { |
| 491 | return colineUiNodeSchema.parse({ |
| 492 | type: "Select", |
| 493 | props: { |
| 494 | name: input.name, |
| 495 | options: input.options, |
| 496 | ...(input.label ? { label: input.label } : {}), |
| 497 | ...(input.defaultValue ? { defaultValue: input.defaultValue } : {}), |
| 498 | ...(input.required ? { required: true } : {}), |
| 499 | }, |
| 500 | }); |
| 501 | }, |
| 502 |
| 503 | table(input: { |
| 504 | columns: Array<{ |
| 505 | key: string; |
| 506 | label: string; |
| 507 | align?: "left" | "center" | "right"; |
| 508 | }>; |
| 509 | rows: Array<Record<string, string>>; |
| 510 | }): ColineUiNode { |
| 511 | return colineUiNodeSchema.parse({ |
| 512 | type: "Table", |
| 513 | props: { |
| 514 | columns: input.columns, |
| 515 | rows: input.rows, |
| 516 | }, |
| 517 | }); |
| 518 | }, |
| 519 | }; |
| 520 |