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