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 parseJsonResponse<T>(response: Response): Promise<T> { |
| 24 | const payload = (await response.json().catch(() => null)) as T | null; |
| 25 |
| 26 | if (!response.ok) { |
| 27 | const errorMessage = |
| 28 | payload && |
| 29 | typeof payload === "object" && |
| 30 | "error_description" in payload && |
| 31 | typeof (payload as Record<string, unknown>)["error_description"] === "string" |
| 32 | ? ((payload as Record<string, unknown>)["error_description"] as string) |
| 33 | : `Request failed (${response.status}).`; |
| 34 |
| 35 | throw new Error(errorMessage); |
| 36 | } |
| 37 |
| 38 | if (!payload) { |
| 39 | throw new Error("Invalid JSON response."); |
| 40 | } |
| 41 |
| 42 | return payload; |
| 43 | } |
| 44 |
| 45 | async function parseApiResponse<T>(response: Response): Promise<T> { |
| 46 | const payload = (await response.json().catch(() => null)) as |
| 47 | | { data?: T; error?: ApiErrorPayload["error"] } |
| 48 | | null; |
| 49 |
| 50 | if (!response.ok) { |
| 51 | throw new Error( |
| 52 | payload?.error?.message ?? `Request failed (${response.status}).`, |
| 53 | ); |
| 54 | } |
| 55 |
| 56 | if (!payload || !("data" in payload)) { |
| 57 | throw new Error("Invalid API response."); |
| 58 | } |
| 59 |
| 60 | return payload.data as T; |
| 61 | } |
| 62 |
| 63 | export interface ColineApiClientOptions { |
| 64 | baseUrl: string; |
| 65 | apiKey?: string; |
| 66 | fetch?: typeof fetch; |
| 67 | } |
| 68 |
| 69 | export interface RegisteredAppRecord { |
| 70 | appId: string; |
| 71 | appKey: string; |
| 72 | name: string; |
| 73 | description: string | null; |
| 74 | iconUrl: string | null; |
| 75 | status: "draft" | "active" | "disabled" | "archived"; |
| 76 | ownerUserId: string | null; |
| 77 | latestPublishedVersion: string | null; |
| 78 | } |
| 79 |
| 80 | export interface RegisteredAppDetail extends RegisteredAppRecord { |
| 81 | latestPublishedVersionId: string | null; |
| 82 | } |
| 83 |
| 84 | export interface RegisteredAppVersionSummary { |
| 85 | versionId: string; |
| 86 | version: string; |
| 87 | status: "draft" | "published" | "deprecated"; |
| 88 | apiBaseUrl: string; |
| 89 | permissions: string[]; |
| 90 | fileTypes: Array<{ |
| 91 | typeKey: string; |
| 92 | name: string; |
| 93 | description: string | null; |
| 94 | }>; |
| 95 | createdAt: string; |
| 96 | updatedAt: string; |
| 97 | } |
| 98 |
| 99 | export interface WorkspaceInstalledApp { |
| 100 | installId: string; |
| 101 | appId: string; |
| 102 | appKey: string; |
| 103 | name: string; |
| 104 | description: string | null; |
| 105 | iconUrl: string | null; |
| 106 | status: "active" | "suspended" | "revoked"; |
| 107 | installScope: "workspace" | "user"; |
| 108 | scopeSubjectId: string; |
| 109 | version: string | null; |
| 110 | } |
| 111 |
| 112 | export interface WorkspaceAppCatalogItem { |
| 113 | appId: string; |
| 114 | appKey: string; |
| 115 | name: string; |
| 116 | description: string | null; |
| 117 | iconUrl: string | null; |
| 118 | status: "draft" | "active" | "disabled" | "archived"; |
| 119 | latestPublishedVersion: string | null; |
| 120 | latestPublishedVersionId: string | null; |
| 121 | isInstalled: boolean; |
| 122 | } |
| 123 |
| 124 | export interface WorkspaceAppsResponse { |
| 125 | installedApps: WorkspaceInstalledApp[]; |
| 126 | catalog: WorkspaceAppCatalogItem[]; |
| 127 | permissions: { |
| 128 | canManageApps: boolean; |
| 129 | }; |
| 130 | } |
| 131 |
| 132 | export interface InstallWorkspaceAppInput { |
| 133 | appId: string; |
| 134 | pinnedVersionId?: string; |
| 135 | grantedPermissions?: AppPermission[]; |
| 136 | } |
| 137 |
| 138 | export interface InstallWorkspaceAppResponse { |
| 139 | installId: string; |
| 140 | appId: string; |
| 141 | appKey: string; |
| 142 | version: string; |
| 143 | grantedPermissions: AppPermission[]; |
| 144 | } |
| 145 |
| 146 | export interface WorkspaceAppPermissionsResponse { |
| 147 | install: WorkspaceInstalledApp; |
| 148 | availablePermissions: string[]; |
| 149 | grantedPermissions: string[]; |
| 150 | permissions: { |
| 151 | canManageApps: boolean; |
| 152 | }; |
| 153 | } |
| 154 |
| 155 | export interface WorkspaceInstalledAppResponse { |
| 156 | install: WorkspaceInstalledApp; |
| 157 | permissions: { |
| 158 | canManageApps: boolean; |
| 159 | }; |
| 160 | } |
| 161 |
| 162 | export interface AppFileDocumentResponse { |
| 163 | file: { |
| 164 | id: string; |
| 165 | name: string; |
| 166 | fileType: string; |
| 167 | appKey: string; |
| 168 | appName: string; |
| 169 | typeLabel: string; |
| 170 | openHref: string; |
| 171 | updatedAt: string; |
| 172 | }; |
| 173 | document: Record<string, unknown>; |
| 174 | version: number; |
| 175 | capabilities: { |
| 176 | canRead: boolean; |
| 177 | canWrite: boolean; |
| 178 | canManage: boolean; |
| 179 | }; |
| 180 | } |
| 181 |
| 182 | export interface TabModelRecord { |
| 183 | id: string; |
| 184 | object: "model"; |
| 185 | created: number; |
| 186 | owned_by: string; |
| 187 | providerModelId: string; |
| 188 | pricing: { |
| 189 | inputPerMillionUsd: number; |
| 190 | outputPerMillionUsd: number; |
| 191 | }; |
| 192 | capabilities: { |
| 193 | streaming: boolean; |
| 194 | surfaces: string[]; |
| 195 | }; |
| 196 | } |
| 197 |
| 198 | export interface OAuthTokenResponse { |
| 199 | access_token: string; |
| 200 | token_type: "Bearer"; |
| 201 | expires_in: number; |
| 202 | refresh_token: string; |
| 203 | scope: string; |
| 204 | } |
| 205 |
| 206 | export interface OAuthUserInfoResponse { |
| 207 | sub: string; |
| 208 | email: string; |
| 209 | email_verified: boolean; |
| 210 | name: string; |
| 211 | given_name: string | null; |
| 212 | family_name: string | null; |
| 213 | picture: string | null; |
| 214 | preferred_username: string | null; |
| 215 | scope: string; |
| 216 | } |
| 217 |
| 218 | export interface UpdateWorkspaceAppPermissionsInput { |
| 219 | grantedPermissions: AppPermission[]; |
| 220 | } |
| 221 |
| 222 | function joinUrl(baseUrl: string, path: string): string { |
| 223 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 224 | } |
| 225 |
| 226 | export class ColineApiClient { |
| 227 | private readonly baseUrl: string; |
| 228 | private readonly apiKey: string | undefined; |
| 229 | private readonly fetchImpl: typeof fetch; |
| 230 |
| 231 | constructor(options: ColineApiClientOptions) { |
| 232 | this.baseUrl = options.baseUrl; |
| 233 | this.apiKey = options.apiKey; |
| 234 | this.fetchImpl = options.fetch ?? fetch; |
| 235 | } |
| 236 |
| 237 | private async request<T>(path: string, init?: RequestInit): Promise<T> { |
| 238 | const headers = new Headers(init?.headers); |
| 239 |
| 240 | if (!headers.has("Content-Type") && init?.body) { |
| 241 | headers.set("Content-Type", "application/json"); |
| 242 | } |
| 243 |
| 244 | if (this.apiKey && !headers.has("Authorization")) { |
| 245 | headers.set("Authorization", `Bearer ${this.apiKey}`); |
| 246 | } |
| 247 |
| 248 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 249 | ...init, |
| 250 | headers, |
| 251 | }); |
| 252 |
| 253 | return parseApiResponse<T>(response); |
| 254 | } |
| 255 |
| 256 | buildLoginWithColineAuthorizeUrl(input: { |
| 257 | clientId: string; |
| 258 | redirectUri: string; |
| 259 | scope?: string; |
| 260 | state?: string; |
| 261 | codeChallenge?: string; |
| 262 | codeChallengeMethod?: "S256" | "plain"; |
| 263 | }): string { |
| 264 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 265 | url.searchParams.set("response_type", "code"); |
| 266 | url.searchParams.set("client_id", input.clientId); |
| 267 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 268 | if (input.scope) { |
| 269 | url.searchParams.set("scope", input.scope); |
| 270 | } |
| 271 | if (input.state) { |
| 272 | url.searchParams.set("state", input.state); |
| 273 | } |
| 274 | if (input.codeChallenge) { |
| 275 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 276 | } |
| 277 | if (input.codeChallengeMethod) { |
| 278 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 279 | } |
| 280 | return url.toString(); |
| 281 | } |
| 282 |
| 283 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 284 | return this.request<{ apps: RegisteredAppRecord[] }>("/api/v1/apps"); |
| 285 | } |
| 286 |
| 287 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 288 | return this.request<{ app: RegisteredAppDetail }>(`/api/v1/apps/${appKey}`); |
| 289 | } |
| 290 |
| 291 | async listAppVersions( |
| 292 | appKey: string, |
| 293 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 294 | return this.request<{ versions: RegisteredAppVersionSummary[] }>( |
| 295 | `/api/v1/apps/${appKey}/versions`, |
| 296 | ); |
| 297 | } |
| 298 |
| 299 | async registerApp( |
| 300 | payload: ColineAppRegistrationPayload, |
| 301 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 302 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 303 |
| 304 | return this.request<{ |
| 305 | appId: string; |
| 306 | appKey: string; |
| 307 | versionId: string; |
| 308 | version: string; |
| 309 | }>("/api/apps", { |
| 310 | method: "POST", |
| 311 | body: JSON.stringify(body), |
| 312 | }); |
| 313 | } |
| 314 |
| 315 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 316 | return this.request<WorkspaceAppsResponse>( |
| 317 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 318 | ); |
| 319 | } |
| 320 |
| 321 | async installWorkspaceApp( |
| 322 | workspaceSlug: string, |
| 323 | input: InstallWorkspaceAppInput, |
| 324 | ): Promise<InstallWorkspaceAppResponse> { |
| 325 | const grantedPermissions = input.grantedPermissions |
| 326 | ? input.grantedPermissions.map((permission) => |
| 327 | appPermissionSchema.parse(permission), |
| 328 | ) |
| 329 | : undefined; |
| 330 |
| 331 | return this.request<InstallWorkspaceAppResponse>( |
| 332 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 333 | { |
| 334 | method: "POST", |
| 335 | body: JSON.stringify({ |
| 336 | appId: input.appId, |
| 337 | ...(input.pinnedVersionId |
| 338 | ? { pinnedVersionId: input.pinnedVersionId } |
| 339 | : {}), |
| 340 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 341 | }), |
| 342 | }, |
| 343 | ); |
| 344 | } |
| 345 |
| 346 | async getWorkspaceAppPermissions( |
| 347 | workspaceSlug: string, |
| 348 | appKey: string, |
| 349 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 350 | return this.request<WorkspaceAppPermissionsResponse>( |
| 351 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 352 | ); |
| 353 | } |
| 354 |
| 355 | async getWorkspaceInstalledApp( |
| 356 | workspaceSlug: string, |
| 357 | appKey: string, |
| 358 | ): Promise<WorkspaceInstalledAppResponse> { |
| 359 | return this.request<WorkspaceInstalledAppResponse>( |
| 360 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 361 | ); |
| 362 | } |
| 363 |
| 364 | async updateWorkspaceAppPermissions( |
| 365 | workspaceSlug: string, |
| 366 | appKey: string, |
| 367 | input: UpdateWorkspaceAppPermissionsInput, |
| 368 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 369 | return this.request<WorkspaceAppPermissionsResponse>( |
| 370 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 371 | { |
| 372 | method: "PUT", |
| 373 | body: JSON.stringify({ |
| 374 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 375 | appPermissionSchema.parse(permission), |
| 376 | ), |
| 377 | }), |
| 378 | }, |
| 379 | ); |
| 380 | } |
| 381 |
| 382 | async uninstallWorkspaceApp( |
| 383 | workspaceSlug: string, |
| 384 | appKey: string, |
| 385 | ): Promise<{ ok: true }> { |
| 386 | return this.request<{ ok: true }>( |
| 387 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 388 | { |
| 389 | method: "DELETE", |
| 390 | }, |
| 391 | ); |
| 392 | } |
| 393 |
| 394 | async emitAppAmbientEvents( |
| 395 | workspaceSlug: string, |
| 396 | appKey: string, |
| 397 | input: EmitAmbientEventsInputInput, |
| 398 | ): Promise<{ events: Array<{ id: string; dedupeKey: string }> }> { |
| 399 | const body = emitAmbientEventsInputSchema.parse(input); |
| 400 |
| 401 | return this.request<{ events: Array<{ id: string; dedupeKey: string }> }>( |
| 402 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/ambient/events`, |
| 403 | { |
| 404 | method: "POST", |
| 405 | body: JSON.stringify(body), |
| 406 | }, |
| 407 | ); |
| 408 | } |
| 409 |
| 410 | async createAppNotification( |
| 411 | workspaceSlug: string, |
| 412 | appKey: string, |
| 413 | input: CreateNotificationInputInput, |
| 414 | ): Promise<{ ok: true }> { |
| 415 | const body = createNotificationInputSchema.parse(input); |
| 416 |
| 417 | return this.request<{ ok: true }>( |
| 418 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/notifications`, |
| 419 | { |
| 420 | method: "POST", |
| 421 | body: JSON.stringify(body), |
| 422 | }, |
| 423 | ); |
| 424 | } |
| 425 |
| 426 | async upsertAppIndexDocuments( |
| 427 | workspaceSlug: string, |
| 428 | appKey: string, |
| 429 | input: BatchUpsertAppIndexDocumentsInput, |
| 430 | ): Promise<{ documents: Array<{ id: string; documentKey: string; visibilityMode: string }> }> { |
| 431 | const body = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 432 |
| 433 | return this.request<{ |
| 434 | documents: Array<{ id: string; documentKey: string; visibilityMode: string }>; |
| 435 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, { |
| 436 | method: "POST", |
| 437 | body: JSON.stringify(body), |
| 438 | }); |
| 439 | } |
| 440 |
| 441 | async deleteAppIndexDocuments( |
| 442 | workspaceSlug: string, |
| 443 | appKey: string, |
| 444 | input: BatchDeleteAppIndexDocumentsInput, |
| 445 | ): Promise<{ deleted: Array<{ id: string; documentKey: string }> }> { |
| 446 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 447 |
| 448 | return this.request<{ deleted: Array<{ id: string; documentKey: string }> }>( |
| 449 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, |
| 450 | { |
| 451 | method: "DELETE", |
| 452 | body: JSON.stringify(body), |
| 453 | }, |
| 454 | ); |
| 455 | } |
| 456 |
| 457 | async getAppFileDocument( |
| 458 | workspaceSlug: string, |
| 459 | appKey: string, |
| 460 | fileId: string, |
| 461 | ): Promise<AppFileDocumentResponse> { |
| 462 | return this.request<AppFileDocumentResponse>( |
| 463 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, |
| 464 | ); |
| 465 | } |
| 466 |
| 467 | async updateAppFileDocument( |
| 468 | workspaceSlug: string, |
| 469 | appKey: string, |
| 470 | fileId: string, |
| 471 | document: Record<string, unknown>, |
| 472 | ): Promise<{ document: Record<string, unknown>; version: number; updatedAt: string }> { |
| 473 | return this.request<{ |
| 474 | document: Record<string, unknown>; |
| 475 | version: number; |
| 476 | updatedAt: string; |
| 477 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, { |
| 478 | method: "PATCH", |
| 479 | body: JSON.stringify({ document }), |
| 480 | }); |
| 481 | } |
| 482 |
| 483 | async listTabModels(): Promise<{ object: "list"; data: TabModelRecord[] }> { |
| 484 | return this.request<{ object: "list"; data: TabModelRecord[] }>( |
| 485 | "/api/v1/tab/models", |
| 486 | ); |
| 487 | } |
| 488 |
| 489 | async createTabChatCompletion(input: { |
| 490 | model: string; |
| 491 | stream?: boolean; |
| 492 | tab_context: Record<string, unknown>; |
| 493 | messages?: Array<{ role?: string; content?: string }>; |
| 494 | }): Promise<{ |
| 495 | id: string; |
| 496 | object: "chat.completion"; |
| 497 | created: number; |
| 498 | model: string; |
| 499 | choices: Array<{ |
| 500 | index: number; |
| 501 | message: { role: "assistant"; content: string }; |
| 502 | finish_reason: "stop"; |
| 503 | }>; |
| 504 | usage: { |
| 505 | prompt_tokens: number; |
| 506 | completion_tokens: number; |
| 507 | total_tokens: number; |
| 508 | }; |
| 509 | }> { |
| 510 | return this.request("/api/v1/tab/chat/completions", { |
| 511 | method: "POST", |
| 512 | body: JSON.stringify(input), |
| 513 | }); |
| 514 | } |
| 515 |
| 516 | async exchangeOAuthCode(input: { |
| 517 | clientId: string; |
| 518 | clientSecret: string; |
| 519 | code: string; |
| 520 | redirectUri: string; |
| 521 | codeVerifier?: string; |
| 522 | }): Promise<OAuthTokenResponse> { |
| 523 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 524 | method: "POST", |
| 525 | headers: { |
| 526 | "Content-Type": "application/json", |
| 527 | }, |
| 528 | body: JSON.stringify({ |
| 529 | grant_type: "authorization_code", |
| 530 | client_id: input.clientId, |
| 531 | client_secret: input.clientSecret, |
| 532 | code: input.code, |
| 533 | redirect_uri: input.redirectUri, |
| 534 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 535 | }), |
| 536 | }); |
| 537 |
| 538 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 539 | } |
| 540 |
| 541 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 542 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 543 | method: "GET", |
| 544 | headers: { |
| 545 | Authorization: `Bearer ${accessToken}`, |
| 546 | }, |
| 547 | }); |
| 548 |
| 549 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 550 | } |
| 551 | } |
| 552 |