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 { ui } from "../tree/tree"; |
| 5 | import { createTestWorkspace } from "./test-workspace"; |
| 6 |
| 7 | const createContact = defineTool({ |
| 8 | name: "crm.create_contact", |
| 9 | description: "Create a contact.", |
| 10 | input: z.object({ name: z.string(), email: z.string().optional() }), |
| 11 | effect: "write", |
| 12 | execute: async (input, context) => { |
| 13 | const file = await context.coline.files.create({ |
| 14 | typeKey: "crm.contact", |
| 15 | name: input.name, |
| 16 | document: { name: input.name, email: input.email ?? null }, |
| 17 | }); |
| 18 | await context.coline.search.index.upsert({ |
| 19 | documents: [ |
| 20 | { |
| 21 | documentKey: `contact:${file.fileId}`, |
| 22 | documentType: "contact", |
| 23 | fileId: file.fileId, |
| 24 | title: input.name, |
| 25 | body: input.email ?? "", |
| 26 | }, |
| 27 | ], |
| 28 | }); |
| 29 | return { |
| 30 | output: { fileId: file.fileId }, |
| 31 | card: ui.stack([ui.reference({ kind: "file", id: file.fileId })]), |
| 32 | }; |
| 33 | }, |
| 34 | }); |
| 35 |
| 36 | const app = defineApp({ |
| 37 | key: "crm", |
| 38 | name: "CRM", |
| 39 | permissions: ["files.read", "files.write", "storage.app", "ai.tools", "search.index"], |
| 40 | surfaces: { home: { tier: "tree" } }, |
| 41 | files: [ |
| 42 | defineFileType({ |
| 43 | metadata: { |
| 44 | typeKey: "crm.contact", |
| 45 | name: "Contact", |
| 46 | indexable: false, |
| 47 | surfaces: { preview: { tier: "tree" }, inline: { tier: "tree" } }, |
| 48 | }, |
| 49 | runtime: { |
| 50 | renderPreview: (context) => ui.heading(String(context.document.name ?? "Contact")), |
| 51 | renderInline: (context) => ui.text(String(context.document.name ?? "Contact")), |
| 52 | }, |
| 53 | }), |
| 54 | ], |
| 55 | tools: [createContact], |
| 56 | triggers: { schedules: [{ id: "digest", cron: "0 9 * * 1-5" }] }, |
| 57 | handlers: { |
| 58 | renderHome: async (context) => { |
| 59 | const { files } = await context.coline.files.list({ typeKey: "crm.contact" }); |
| 60 | return ui.stack([ui.heading("CRM"), ui.text(`${files.length} contacts`)]); |
| 61 | }, |
| 62 | onSchedule: async (context) => { |
| 63 | await context.coline.storage.kv.set("last-digest", context.firedAt); |
| 64 | }, |
| 65 | }, |
| 66 | }); |
| 67 |
| 68 | describe("createTestWorkspace", () => { |
| 69 | it("invokes tools through the schema and captures state", async () => { |
| 70 | const workspace = createTestWorkspace(app); |
| 71 | const result = await workspace.invokeTool("crm.create_contact", { name: "Sarah" }); |
| 72 | expect(result.output).toMatchObject({ fileId: expect.stringContaining("file_test") }); |
| 73 | expect(result.card?.type).toBe("Stack"); |
| 74 | expect(workspace.files.byType("crm.contact")).toHaveLength(1); |
| 75 | expect(workspace.state.indexDocuments.size).toBe(1); |
| 76 | }); |
| 77 |
| 78 | it("rejects invalid tool input with schema detail", async () => { |
| 79 | const workspace = createTestWorkspace(app); |
| 80 | await expect(workspace.invokeTool("crm.create_contact", { name: 42 })).rejects.toThrow( |
| 81 | /invalid/i, |
| 82 | ); |
| 83 | }); |
| 84 |
| 85 | it("renders home against live fake state", async () => { |
| 86 | const workspace = createTestWorkspace(app); |
| 87 | await workspace.invokeTool("crm.create_contact", { name: "A" }); |
| 88 | await workspace.invokeTool("crm.create_contact", { name: "B" }); |
| 89 | const tree = await workspace.renderHome(); |
| 90 | expect(JSON.stringify(tree)).toContain("2 contacts"); |
| 91 | }); |
| 92 |
| 93 | it("renders file surfaces and enforces the inline subset", async () => { |
| 94 | const workspace = createTestWorkspace(app); |
| 95 | const preview = await workspace.renderFileSurface( |
| 96 | "preview", |
| 97 | { fileId: "f1", name: "Sarah", typeKey: "crm.contact" }, |
| 98 | { name: "Sarah Chen" }, |
| 99 | ); |
| 100 | expect(JSON.stringify(preview)).toContain("Sarah Chen"); |
| 101 | }); |
| 102 |
| 103 | it("fires declared schedules with the schedule context", async () => { |
| 104 | const workspace = createTestWorkspace(app); |
| 105 | const at = new Date("2026-08-10T09:00:00Z"); |
| 106 | await workspace.fireSchedule("digest", at); |
| 107 | expect(workspace.state.kv.get("last-digest")).toBe(at.toISOString()); |
| 108 | }); |
| 109 |
| 110 | it("rejects undeclared schedules", async () => { |
| 111 | const workspace = createTestWorkspace(app); |
| 112 | await expect(workspace.fireSchedule("nope")).rejects.toThrow(/not declared/); |
| 113 | }); |
| 114 |
| 115 | }); |
| 116 |