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 { createColineToolHandler } from "./app-server"; |
| 6 | import { |
| 7 | buildSignedProtocolRequest, |
| 8 | type ColineProtocolEnvelope, |
| 9 | } from "./protocol"; |
| 10 | import { TOOL_INVOKE_KIND, type ToolInvokePayload } from "./tool-invoke"; |
| 11 |
| 12 | const SECRET = "delivery-secret-of-sufficient-length"; |
| 13 |
| 14 | const createContact = defineTool({ |
| 15 | name: "crm.create_contact", |
| 16 | description: "Create a contact.", |
| 17 | input: z.object({ name: z.string() }), |
| 18 | effect: "write", |
| 19 | execute: async (input, context) => { |
| 20 | const file = await context.coline.files.create({ |
| 21 | typeKey: "crm.contact", |
| 22 | name: input.name, |
| 23 | }); |
| 24 | return { |
| 25 | output: { fileId: file.fileId }, |
| 26 | card: ui.stack([ui.text(`Created ${input.name}`)]), |
| 27 | }; |
| 28 | }, |
| 29 | }); |
| 30 |
| 31 | const badCardTool = defineTool({ |
| 32 | name: "crm.bad_card", |
| 33 | description: "Returns a disallowed card.", |
| 34 | input: z.object({}), |
| 35 | effect: "read", |
| 36 | execute: () => ({ |
| 37 | card: ui.input({ name: "nope" }), |
| 38 | }), |
| 39 | }); |
| 40 |
| 41 | const app = defineApp({ |
| 42 | key: "crm", |
| 43 | name: "CRM", |
| 44 | permissions: ["files.read", "files.write", "ai.tools"], |
| 45 | tools: [createContact, badCardTool], |
| 46 | }); |
| 47 |
| 48 | function buildEnvelope(payload: ToolInvokePayload): ColineProtocolEnvelope { |
| 49 | return { |
| 50 | protocol: 2, |
| 51 | deliveryId: "dlv_0123456789", |
| 52 | sentAt: new Date().toISOString(), |
| 53 | workspace: { workspaceId: "ws_1", workspaceSlug: "acme" }, |
| 54 | app: { appKey: "crm", installId: "inst_1" }, |
| 55 | actor: { kind: "user", userId: "user_1" }, |
| 56 | kind: TOOL_INVOKE_KIND, |
| 57 | payload, |
| 58 | }; |
| 59 | } |
| 60 |
| 61 | async function invoke(payload: ToolInvokePayload, options?: { secret?: string }) { |
| 62 | const signed = await buildSignedProtocolRequest({ |
| 63 | secret: options?.secret ?? SECRET, |
| 64 | envelope: buildEnvelope(payload), |
| 65 | }); |
| 66 | const handler = createColineToolHandler({ app, secret: SECRET }); |
| 67 | const response = await handler( |
| 68 | new Request("https://crm.example.com/coline/tools", { |
| 69 | method: "POST", |
| 70 | headers: signed.headers, |
| 71 | body: signed.body, |
| 72 | }), |
| 73 | ); |
| 74 | return { status: response.status, body: (await response.json()) as Record<string, unknown> }; |
| 75 | } |
| 76 |
| 77 | const capabilities = { url: "https://coline.test/api/v1/apps/capability", token: "tok_0123456789abcdef" }; |
| 78 |
| 79 | describe("createColineToolHandler", () => { |
| 80 | it("executes a tool with capability-backed context and returns output + card", async () => { |
| 81 | const originalFetch = globalThis.fetch; |
| 82 | globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 83 | const body = JSON.parse(String(init?.body)) as { operation: string; args: unknown[] }; |
| 84 | expect(String(input)).toBe(capabilities.url); |
| 85 | expect(body.operation).toBe("files.create"); |
| 86 | expect((init?.headers as Record<string, string>).authorization).toBe( |
| 87 | `Bearer ${capabilities.token}`, |
| 88 | ); |
| 89 | return new Response( |
| 90 | JSON.stringify({ data: { fileId: "file_9", name: "Sarah", typeKey: "crm.contact" } }), |
| 91 | { headers: { "content-type": "application/json" } }, |
| 92 | ); |
| 93 | }) as typeof fetch; |
| 94 |
| 95 | try { |
| 96 | const { status, body } = await invoke({ |
| 97 | tool: "crm.create_contact", |
| 98 | input: { name: "Sarah" }, |
| 99 | capabilities, |
| 100 | }); |
| 101 | expect(status).toBe(200); |
| 102 | const result = body.result as { output: { fileId: string }; card: unknown }; |
| 103 | expect(result.output.fileId).toBe("file_9"); |
| 104 | expect(result.card).toBeDefined(); |
| 105 | } finally { |
| 106 | globalThis.fetch = originalFetch; |
| 107 | } |
| 108 | }); |
| 109 |
| 110 | it("rejects bad signatures", async () => { |
| 111 | const { status, body } = await invoke( |
| 112 | { tool: "crm.create_contact", input: { name: "Sarah" }, capabilities }, |
| 113 | { secret: "the-wrong-secret-of-sufficient-length" }, |
| 114 | ); |
| 115 | expect(status).toBe(401); |
| 116 | expect((body.error as { code: string }).code).toBe("invalid_signature"); |
| 117 | }); |
| 118 |
| 119 | it("rejects unknown tools", async () => { |
| 120 | const { status, body } = await invoke({ |
| 121 | tool: "crm.nonexistent", |
| 122 | input: {}, |
| 123 | capabilities, |
| 124 | }); |
| 125 | expect(status).toBe(404); |
| 126 | expect((body.error as { code: string }).code).toBe("unknown_tool"); |
| 127 | }); |
| 128 |
| 129 | it("rejects invalid input with issue detail", async () => { |
| 130 | const { status, body } = await invoke({ |
| 131 | tool: "crm.create_contact", |
| 132 | input: { name: 42 }, |
| 133 | capabilities, |
| 134 | }); |
| 135 | expect(status).toBe(400); |
| 136 | expect((body.error as { code: string }).code).toBe("invalid_input"); |
| 137 | }); |
| 138 |
| 139 | it("rejects cards outside the chat subset", async () => { |
| 140 | const { status, body } = await invoke({ |
| 141 | tool: "crm.bad_card", |
| 142 | input: {}, |
| 143 | capabilities, |
| 144 | }); |
| 145 | expect(status).toBe(400); |
| 146 | expect((body.error as { code: string }).code).toBe("invalid_card"); |
| 147 | }); |
| 148 | }); |
| 149 |