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.
| 5 | ColineRecordEnvelope, |
| 6 | ColineRecordQuery, |
| 7 | } from "./types"; |
| 8 |
| 9 | /** |
| 10 | * The HTTP transport for `coline.*` (plan §8): capability calls become |
| 11 | * `POST { operation, args }` against the invocation's capability endpoint, |
| 12 | * authorized by the invocation token. Used by external app backends and by |
| 13 | * the hosted Dynamic Worker bootstrap — one wire contract for both. |
| 14 | * |
| 15 | * Client-side interaction capabilities (pickers, navigation, realtime) do |
| 16 | * not exist in server contexts and reject immediately with |
| 17 | * `capability_unavailable`. |
| 18 | */ |
| 19 |
| 20 | export interface CapabilityEndpoint { |
| 21 | url: string; |
| 22 | token: string; |
| 23 | fetchImpl?: typeof fetch | undefined; |
| 24 | } |
| 25 |
| 26 | export class ColineCapabilityCallError extends Error { |
| 27 | readonly code: string; |
| 28 | readonly operation: string; |
| 29 |
| 30 | constructor(operation: string, code: string, message: string) { |
| 31 | super(message); |
| 32 | this.name = "ColineCapabilityCallError"; |
| 33 | this.code = code; |
| 34 | this.operation = operation; |
| 35 | } |
| 36 | } |
| 37 |
| 38 | async function callCapability<T>( |
| 39 | endpoint: CapabilityEndpoint, |
| 40 | operation: string, |
| 41 | args: unknown[], |
| 42 | ): Promise<T> { |
| 43 | const fetchImpl = endpoint.fetchImpl ?? fetch; |
| 44 | let response: Response; |
| 45 | try { |
| 46 | response = await fetchImpl(endpoint.url, { |
| 47 | method: "POST", |
| 48 | headers: { |
| 49 | "content-type": "application/json", |
| 50 | authorization: `Bearer ${endpoint.token}`, |
| 51 | }, |
| 52 | body: JSON.stringify({ operation, args }), |
| 53 | }); |
| 54 | } catch (error) { |
| 55 | throw new ColineCapabilityCallError( |
| 56 | operation, |
| 57 | "network_error", |
| 58 | `Capability call failed to reach Coline: ${error instanceof Error ? error.message : String(error)}`, |
| 59 | ); |
| 60 | } |
| 61 |
| 62 | let parsed: unknown; |
| 63 | try { |
| 64 | parsed = await response.json(); |
| 65 | } catch { |
| 66 | throw new ColineCapabilityCallError( |
| 67 | operation, |
| 68 | "invalid_response", |
| 69 | `Capability endpoint returned a non-JSON response (${response.status}).`, |
| 70 | ); |
| 71 | } |
| 72 |
| 73 | const record = parsed as { data?: unknown; error?: { code?: string; message?: string } }; |
| 74 | if (record.error) { |
| 75 | throw new ColineCapabilityCallError( |
| 76 | operation, |
| 77 | record.error.code ?? "capability_error", |
| 78 | record.error.message ?? "Capability call failed.", |
| 79 | ); |
| 80 | } |
| 81 | if (!response.ok) { |
| 82 | throw new ColineCapabilityCallError( |
| 83 | operation, |
| 84 | "capability_error", |
| 85 | `Capability endpoint returned ${response.status}.`, |
| 86 | ); |
| 87 | } |
| 88 | return record.data as T; |
| 89 | } |
| 90 |
| 91 | function unavailable(operation: string): never { |
| 92 | throw new ColineCapabilityCallError( |
| 93 | operation, |
| 94 | "capability_unavailable", |
| 95 | `"${operation}" is a client-context capability and is not available to server-side app logic.`, |
| 96 | ); |
| 97 | } |
| 98 |
| 99 | function createHttpCollection<TData>( |
| 100 | endpoint: CapabilityEndpoint, |
| 101 | name: string, |
| 102 | ): ColineCollection<TData> { |
| 103 | return { |
| 104 | insert: (data) => |
| 105 | callCapability<ColineRecordEnvelope<TData>>(endpoint, "storage.records.insert", [name, data]), |
| 106 | get: (recordId) => |
| 107 | callCapability<ColineRecordEnvelope<TData> | null>(endpoint, "storage.records.get", [ |
| 108 | name, |
| 109 | recordId, |
| 110 | ]), |
| 111 | update: (recordId, data) => |
| 112 | callCapability<ColineRecordEnvelope<TData>>(endpoint, "storage.records.update", [ |
| 113 | name, |
| 114 | recordId, |
| 115 | data, |
| 116 | ]), |
| 117 | delete: (recordId) => |
| 118 | callCapability<void>(endpoint, "storage.records.delete", [name, recordId]), |
| 119 | query: (query?: ColineRecordQuery<TData>) => |
| 120 | callCapability<{ |
| 121 | records: Array<ColineRecordEnvelope<TData>>; |
| 122 | nextCursor: string | null; |
| 123 | }>(endpoint, "storage.records.query", [name, query ?? {}]), |
| 124 | }; |
| 125 | } |
| 126 |
| 127 | export function createHttpCapabilities(endpoint: CapabilityEndpoint): ColineCapabilities { |
| 128 | return { |
| 129 | ...createWorkspaceCapabilities((operation, args) => callCapability(endpoint, operation, args)), |
| 130 | tools: { invoke: () => unavailable("tools.invoke") }, |
| 131 | workspace: { |
| 132 | get: () => callCapability(endpoint, "workspace.get", []), |
| 133 | }, |
| 134 | members: { |
| 135 | list: (input) => callCapability(endpoint, "members.list", [input ?? {}]), |
| 136 | get: (userId) => callCapability(endpoint, "members.get", [userId]), |
| 137 | }, |
| 138 | files: { |
| 139 | list: (input) => callCapability(endpoint, "files.list", [input ?? {}]), |
| 140 | get: (fileId) => callCapability(endpoint, "files.get", [fileId]), |
| 141 | create: (input) => callCapability(endpoint, "files.create", [input]), |
| 142 | update: (fileId, input) => callCapability(endpoint, "files.update", [fileId, input]), |
| 143 | trash: (fileId) => callCapability(endpoint, "files.trash", [fileId]), |
| 144 | getDocument: (fileId) => callCapability(endpoint, "files.getDocument", [fileId]), |
| 145 | updateDocument: (fileId, document, options) => |
| 146 | callCapability(endpoint, "files.updateDocument", [fileId, document, options ?? {}]), |
| 147 | }, |
| 148 | drives: { |
| 149 | getAppDrive: () => callCapability(endpoint, "drives.getAppDrive", []), |
| 150 | }, |
| 151 | storage: { |
| 152 | kv: { |
| 153 | get: (key) => callCapability(endpoint, "storage.kv.get", [key]), |
| 154 | set: (key, value) => callCapability(endpoint, "storage.kv.set", [key, value]), |
| 155 | delete: (key) => callCapability(endpoint, "storage.kv.delete", [key]), |
| 156 | list: (input) => callCapability(endpoint, "storage.kv.list", [input ?? {}]), |
| 157 | }, |
| 158 | collection: <TData>(name: string) => createHttpCollection<TData>(endpoint, name), |
| 159 | }, |
| 160 | search: { |
| 161 | query: (input) => callCapability(endpoint, "search.query", [input]), |
| 162 | index: { |
| 163 | upsert: (input) => callCapability(endpoint, "search.index.upsert", [input]), |
| 164 | delete: (input) => callCapability(endpoint, "search.index.delete", [input]), |
| 165 | }, |
| 166 | }, |
| 167 | notifications: { |
| 168 | create: (input) => callCapability(endpoint, "notifications.create", [input]), |
| 169 | }, |
| 170 | ai: { |
| 171 | generate: (prompt, options) => |
| 172 | callCapability(endpoint, "ai.generate", [prompt, options ?? {}]), |
| 173 | }, |
| 174 | net: { |
| 175 | fetch: (input) => callCapability(endpoint, "net.fetch", [input]), |
| 176 | }, |
| 177 | pickers: { |
| 178 | file: () => unavailable("pickers.file"), |
| 179 | member: () => unavailable("pickers.member"), |
| 180 | confirm: () => unavailable("pickers.confirm"), |
| 181 | }, |
| 182 | navigation: { |
| 183 | openFile: () => unavailable("navigation.openFile"), |
| 184 | openAppHome: () => unavailable("navigation.openAppHome"), |
| 185 | navigate: () => unavailable("navigation.navigate"), |
| 186 | openReference: () => unavailable("navigation.openReference"), |
| 187 | }, |
| 188 | realtime: { |
| 189 | // Server-side logic cannot hold a socket; it publishes, clients |
| 190 | // subscribe (tier-2 via the bridge). |
| 191 | subscribe: () => unavailable("realtime.subscribe"), |
| 192 | publish: (event, data) => |
| 193 | callCapability(endpoint, "realtime.publish", [{ event, data }]) as Promise<{ |
| 194 | delivered: boolean; |
| 195 | }>, |
| 196 | }, |
| 197 | events: { |
| 198 | emit: (event, data) => |
| 199 | callCapability(endpoint, "events.emit", [{ event, data }]) as Promise<{ |
| 200 | emitted: boolean; |
| 201 | }>, |
| 202 | }, |
| 203 | }; |
| 204 | } |
| 205 |