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.
| 6 | emitAmbientEventsInputSchema, |
| 7 | appPermissionSchema, |
| 8 | type AppPermission, |
| 9 | type BatchDeleteAppIndexDocumentsInput, |
| 10 | type BatchUpsertAppIndexDocumentsInput, |
| 11 | type ColineAppRegistrationPayload, |
| 12 | type CreateNotificationInputInput, |
| 13 | type EmitAmbientEventsInputInput, |
| 14 | } from "./manifest"; |
| 15 |
| 16 | interface ApiErrorPayload { |
| 17 | error?: { |
| 18 | code?: string; |
| 19 | message?: string; |
| 20 | }; |
| 21 | } |
| 22 |
| 23 | async function parseApiResponse<T>(response: Response): Promise<T> { |
| 24 | const payload = (await response.json().catch(() => null)) as |
| 25 | | { data?: T; error?: ApiErrorPayload["error"] } |
| 26 | | null; |
| 27 |
| 28 | if (!response.ok) { |
| 29 | throw new Error( |
| 30 | payload?.error?.message ?? `Request failed (${response.status}).`, |
| 31 | ); |
| 32 | } |
| 33 |
| 34 | if (!payload || !("data" in payload)) { |
| 35 | throw new Error("Invalid API response."); |
| 36 | } |
| 37 |
| 38 | return payload.data as T; |
| 39 | } |
| 40 |
| 41 | export interface ColineApiClientOptions { |
| 42 | baseUrl: string; |
| 43 | apiKey?: string; |
| 44 | fetch?: typeof fetch; |
| 45 | } |
| 46 |
| 47 | export interface RegisteredAppRecord { |
| 48 | appId: string; |
| 49 | appKey: string; |
| 50 | name: string; |
| 51 | description: string | null; |
| 52 | iconUrl: string | null; |
| 53 | status: "draft" | "active" | "disabled" | "archived"; |
| 54 | ownerUserId: string | null; |
| 55 | latestPublishedVersion: string | null; |
| 56 | } |
| 57 |
| 58 | export interface RegisteredAppDetail extends RegisteredAppRecord { |
| 59 | latestPublishedVersionId: string | null; |
| 60 | } |
| 61 |
| 62 | export interface RegisteredAppVersionSummary { |
| 63 | versionId: string; |
| 64 | version: string; |
| 65 | status: "draft" | "published" | "deprecated"; |
| 66 | apiBaseUrl: string; |
| 67 | permissions: string[]; |
| 68 | fileTypes: Array<{ |
| 69 | typeKey: string; |
| 70 | name: string; |
| 71 | description: string | null; |
| 72 | }>; |
| 73 | createdAt: string; |
| 74 | updatedAt: string; |
| 75 | } |
| 76 |
| 77 | export interface WorkspaceInstalledApp { |
| 78 | installId: string; |
| 79 | appId: string; |
| 80 | appKey: string; |
| 81 | name: string; |
| 82 | description: string | null; |
| 83 | iconUrl: string | null; |
| 84 | status: "active" | "suspended" | "revoked"; |
| 85 | installScope: "workspace" | "user"; |
| 86 | scopeSubjectId: string; |
| 87 | version: string | null; |
| 88 | } |
| 89 |
| 90 | export interface WorkspaceAppCatalogItem { |
| 91 | appId: string; |
| 92 | appKey: string; |
| 93 | name: string; |
| 94 | description: string | null; |
| 95 | iconUrl: string | null; |
| 96 | status: "draft" | "active" | "disabled" | "archived"; |
| 97 | latestPublishedVersion: string | null; |
| 98 | latestPublishedVersionId: string | null; |
| 99 | isInstalled: boolean; |
| 100 | } |
| 101 |
| 102 | export interface WorkspaceAppsResponse { |
| 103 | installedApps: WorkspaceInstalledApp[]; |
| 104 | catalog: WorkspaceAppCatalogItem[]; |
| 105 | permissions: { |
| 106 | canManageApps: boolean; |
| 107 | }; |
| 108 | } |
| 109 |
| 110 | export interface InstallWorkspaceAppInput { |
| 111 | appId: string; |
| 112 | pinnedVersionId?: string; |
| 113 | grantedPermissions?: AppPermission[]; |
| 114 | } |
| 115 |
| 116 | export interface InstallWorkspaceAppResponse { |
| 117 | installId: string; |
| 118 | appId: string; |
| 119 | appKey: string; |
| 120 | version: string; |
| 121 | grantedPermissions: AppPermission[]; |
| 122 | } |
| 123 |
| 124 | export interface WorkspaceAppPermissionsResponse { |
| 125 | install: WorkspaceInstalledApp; |
| 126 | availablePermissions: string[]; |
| 127 | grantedPermissions: string[]; |
| 128 | permissions: { |
| 129 | canManageApps: boolean; |
| 130 | }; |
| 131 | } |
| 132 |
| 133 | export interface WorkspaceInstalledAppResponse { |
| 134 | install: WorkspaceInstalledApp; |
| 135 | permissions: { |
| 136 | canManageApps: boolean; |
| 137 | }; |
| 138 | } |
| 139 |
| 140 | export interface AppFileDocumentResponse { |
| 141 | file: { |
| 142 | id: string; |
| 143 | name: string; |
| 144 | fileType: string; |
| 145 | appKey: string; |
| 146 | appName: string; |
| 147 | typeLabel: string; |
| 148 | openHref: string; |
| 149 | updatedAt: string; |
| 150 | }; |
| 151 | document: Record<string, unknown>; |
| 152 | version: number; |
| 153 | capabilities: { |
| 154 | canRead: boolean; |
| 155 | canWrite: boolean; |
| 156 | canManage: boolean; |
| 157 | }; |
| 158 | } |
| 159 |
| 160 | export interface TabModelRecord { |
| 161 | id: string; |
| 162 | object: "model"; |
| 163 | created: number; |
| 164 | owned_by: string; |
| 165 | providerModelId: string; |
| 166 | pricing: { |
| 167 | inputPerMillionUsd: number; |
| 168 | outputPerMillionUsd: number; |
| 169 | }; |
| 170 | capabilities: { |
| 171 | streaming: boolean; |
| 172 | surfaces: string[]; |
| 173 | }; |
| 174 | } |
| 175 |
| 176 | export interface UpdateWorkspaceAppPermissionsInput { |
| 177 | grantedPermissions: AppPermission[]; |
| 178 | } |
| 179 |
| 180 | function joinUrl(baseUrl: string, path: string): string { |
| 181 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 182 | } |
| 183 |
| 184 | export class ColineApiClient { |
| 185 | private readonly baseUrl: string; |
| 186 | private readonly apiKey: string | undefined; |
| 187 | private readonly fetchImpl: typeof fetch; |
| 188 |
| 189 | constructor(options: ColineApiClientOptions) { |
| 190 | this.baseUrl = options.baseUrl; |
| 191 | this.apiKey = options.apiKey; |
| 192 | this.fetchImpl = options.fetch ?? fetch; |
| 193 | } |
| 194 |
| 195 | private async request<T>(path: string, init?: RequestInit): Promise<T> { |
| 196 | const headers = new Headers(init?.headers); |
| 197 |
| 198 | if (!headers.has("Content-Type") && init?.body) { |
| 199 | headers.set("Content-Type", "application/json"); |
| 200 | } |
| 201 |
| 202 | if (this.apiKey && !headers.has("Authorization")) { |
| 203 | headers.set("Authorization", `Bearer ${this.apiKey}`); |
| 204 | } |
| 205 |
| 206 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 207 | ...init, |
| 208 | headers, |
| 209 | }); |
| 210 |
| 211 | return parseApiResponse<T>(response); |
| 212 | } |
| 213 |
| 214 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 215 | return this.request<{ apps: RegisteredAppRecord[] }>("/api/v1/apps"); |
| 216 | } |
| 217 |
| 218 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 219 | return this.request<{ app: RegisteredAppDetail }>(`/api/v1/apps/${appKey}`); |
| 220 | } |
| 221 |
| 222 | async listAppVersions( |
| 223 | appKey: string, |
| 224 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 225 | return this.request<{ versions: RegisteredAppVersionSummary[] }>( |
| 226 | `/api/v1/apps/${appKey}/versions`, |
| 227 | ); |
| 228 | } |
| 229 |
| 230 | async registerApp( |
| 231 | payload: ColineAppRegistrationPayload, |
| 232 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 233 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 234 |
| 235 | return this.request<{ |
| 236 | appId: string; |
| 237 | appKey: string; |
| 238 | versionId: string; |
| 239 | version: string; |
| 240 | }>("/api/apps", { |
| 241 | method: "POST", |
| 242 | body: JSON.stringify(body), |
| 243 | }); |
| 244 | } |
| 245 |
| 246 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 247 | return this.request<WorkspaceAppsResponse>( |
| 248 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 249 | ); |
| 250 | } |
| 251 |
| 252 | async installWorkspaceApp( |
| 253 | workspaceSlug: string, |
| 254 | input: InstallWorkspaceAppInput, |
| 255 | ): Promise<InstallWorkspaceAppResponse> { |
| 256 | const grantedPermissions = input.grantedPermissions |
| 257 | ? input.grantedPermissions.map((permission) => |
| 258 | appPermissionSchema.parse(permission), |
| 259 | ) |
| 260 | : undefined; |
| 261 |
| 262 | return this.request<InstallWorkspaceAppResponse>( |
| 263 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 264 | { |
| 265 | method: "POST", |
| 266 | body: JSON.stringify({ |
| 267 | appId: input.appId, |
| 268 | ...(input.pinnedVersionId |
| 269 | ? { pinnedVersionId: input.pinnedVersionId } |
| 270 | : {}), |
| 271 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 272 | }), |
| 273 | }, |
| 274 | ); |
| 275 | } |
| 276 |
| 277 | async getWorkspaceAppPermissions( |
| 278 | workspaceSlug: string, |
| 279 | appKey: string, |
| 280 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 281 | return this.request<WorkspaceAppPermissionsResponse>( |
| 282 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 283 | ); |
| 284 | } |
| 285 |
| 286 | async getWorkspaceInstalledApp( |
| 287 | workspaceSlug: string, |
| 288 | appKey: string, |
| 289 | ): Promise<WorkspaceInstalledAppResponse> { |
| 290 | return this.request<WorkspaceInstalledAppResponse>( |
| 291 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 292 | ); |
| 293 | } |
| 294 |
| 295 | async updateWorkspaceAppPermissions( |
| 296 | workspaceSlug: string, |
| 297 | appKey: string, |
| 298 | input: UpdateWorkspaceAppPermissionsInput, |
| 299 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 300 | return this.request<WorkspaceAppPermissionsResponse>( |
| 301 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 302 | { |
| 303 | method: "PUT", |
| 304 | body: JSON.stringify({ |
| 305 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 306 | appPermissionSchema.parse(permission), |
| 307 | ), |
| 308 | }), |
| 309 | }, |
| 310 | ); |
| 311 | } |
| 312 |
| 313 | async uninstallWorkspaceApp( |
| 314 | workspaceSlug: string, |
| 315 | appKey: string, |
| 316 | ): Promise<{ ok: true }> { |
| 317 | return this.request<{ ok: true }>( |
| 318 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 319 | { |
| 320 | method: "DELETE", |
| 321 | }, |
| 322 | ); |
| 323 | } |
| 324 |
| 325 | async emitAppAmbientEvents( |
| 326 | workspaceSlug: string, |
| 327 | appKey: string, |
| 328 | input: EmitAmbientEventsInputInput, |
| 329 | ): Promise<{ events: Array<{ id: string; dedupeKey: string }> }> { |
| 330 | const body = emitAmbientEventsInputSchema.parse(input); |
| 331 |
| 332 | return this.request<{ events: Array<{ id: string; dedupeKey: string }> }>( |
| 333 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/ambient/events`, |
| 334 | { |
| 335 | method: "POST", |
| 336 | body: JSON.stringify(body), |
| 337 | }, |
| 338 | ); |
| 339 | } |
| 340 |
| 341 | async createAppNotification( |
| 342 | workspaceSlug: string, |
| 343 | appKey: string, |
| 344 | input: CreateNotificationInputInput, |
| 345 | ): Promise<{ ok: true }> { |
| 346 | const body = createNotificationInputSchema.parse(input); |
| 347 |
| 348 | return this.request<{ ok: true }>( |
| 349 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/notifications`, |
| 350 | { |
| 351 | method: "POST", |
| 352 | body: JSON.stringify(body), |
| 353 | }, |
| 354 | ); |
| 355 | } |
| 356 |
| 357 | async upsertAppIndexDocuments( |
| 358 | workspaceSlug: string, |
| 359 | appKey: string, |
| 360 | input: BatchUpsertAppIndexDocumentsInput, |
| 361 | ): Promise<{ documents: Array<{ id: string; documentKey: string; visibilityMode: string }> }> { |
| 362 | const body = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 363 |
| 364 | return this.request<{ |
| 365 | documents: Array<{ id: string; documentKey: string; visibilityMode: string }>; |
| 366 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, { |
| 367 | method: "POST", |
| 368 | body: JSON.stringify(body), |
| 369 | }); |
| 370 | } |
| 371 |
| 372 | async deleteAppIndexDocuments( |
| 373 | workspaceSlug: string, |
| 374 | appKey: string, |
| 375 | input: BatchDeleteAppIndexDocumentsInput, |
| 376 | ): Promise<{ deleted: Array<{ id: string; documentKey: string }> }> { |
| 377 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 378 |
| 379 | return this.request<{ deleted: Array<{ id: string; documentKey: string }> }>( |
| 380 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, |
| 381 | { |
| 382 | method: "DELETE", |
| 383 | body: JSON.stringify(body), |
| 384 | }, |
| 385 | ); |
| 386 | } |
| 387 |
| 388 | async getAppFileDocument( |
| 389 | workspaceSlug: string, |
| 390 | appKey: string, |
| 391 | fileId: string, |
| 392 | ): Promise<AppFileDocumentResponse> { |
| 393 | return this.request<AppFileDocumentResponse>( |
| 394 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, |
| 395 | ); |
| 396 | } |
| 397 |
| 398 | async updateAppFileDocument( |
| 399 | workspaceSlug: string, |
| 400 | appKey: string, |
| 401 | fileId: string, |
| 402 | document: Record<string, unknown>, |
| 403 | ): Promise<{ document: Record<string, unknown>; version: number; updatedAt: string }> { |
| 404 | return this.request<{ |
| 405 | document: Record<string, unknown>; |
| 406 | version: number; |
| 407 | updatedAt: string; |
| 408 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, { |
| 409 | method: "PATCH", |
| 410 | body: JSON.stringify({ document }), |
| 411 | }); |
| 412 | } |
| 413 |
| 414 | async listTabModels(): Promise<{ object: "list"; data: TabModelRecord[] }> { |
| 415 | return this.request<{ object: "list"; data: TabModelRecord[] }>( |
| 416 | "/api/v1/tab/models", |
| 417 | ); |
| 418 | } |
| 419 |
| 420 | async createTabChatCompletion(input: { |
| 421 | model: string; |
| 422 | stream?: boolean; |
| 423 | tab_context: Record<string, unknown>; |
| 424 | messages?: Array<{ role?: string; content?: string }>; |
| 425 | }): Promise<{ |
| 426 | id: string; |
| 427 | object: "chat.completion"; |
| 428 | created: number; |
| 429 | model: string; |
| 430 | choices: Array<{ |
| 431 | index: number; |
| 432 | message: { role: "assistant"; content: string }; |
| 433 | finish_reason: "stop"; |
| 434 | }>; |
| 435 | usage: { |
| 436 | prompt_tokens: number; |
| 437 | completion_tokens: number; |
| 438 | total_tokens: number; |
| 439 | }; |
| 440 | }> { |
| 441 | return this.request("/api/v1/tab/chat/completions", { |
| 442 | method: "POST", |
| 443 | body: JSON.stringify(input), |
| 444 | }); |
| 445 | } |
| 446 | } |
| 447 |