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 OAuthTokenResponse { |
| 177 | access_token: string; |
| 178 | token_type: "Bearer"; |
| 179 | expires_in: number; |
| 180 | refresh_token: string; |
| 181 | scope: string; |
| 182 | } |
| 183 |
| 184 | export interface OAuthUserInfoResponse { |
| 185 | sub: string; |
| 186 | email: string; |
| 187 | email_verified: boolean; |
| 188 | name: string; |
| 189 | given_name: string | null; |
| 190 | family_name: string | null; |
| 191 | picture: string | null; |
| 192 | preferred_username: string | null; |
| 193 | scope: string; |
| 194 | } |
| 195 |
| 196 | export interface UpdateWorkspaceAppPermissionsInput { |
| 197 | grantedPermissions: AppPermission[]; |
| 198 | } |
| 199 |
| 200 | function joinUrl(baseUrl: string, path: string): string { |
| 201 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 202 | } |
| 203 |
| 204 | export class ColineApiClient { |
| 205 | private readonly baseUrl: string; |
| 206 | private readonly apiKey: string | undefined; |
| 207 | private readonly fetchImpl: typeof fetch; |
| 208 |
| 209 | constructor(options: ColineApiClientOptions) { |
| 210 | this.baseUrl = options.baseUrl; |
| 211 | this.apiKey = options.apiKey; |
| 212 | this.fetchImpl = options.fetch ?? fetch; |
| 213 | } |
| 214 |
| 215 | private async request<T>(path: string, init?: RequestInit): Promise<T> { |
| 216 | const headers = new Headers(init?.headers); |
| 217 |
| 218 | if (!headers.has("Content-Type") && init?.body) { |
| 219 | headers.set("Content-Type", "application/json"); |
| 220 | } |
| 221 |
| 222 | if (this.apiKey && !headers.has("Authorization")) { |
| 223 | headers.set("Authorization", `Bearer ${this.apiKey}`); |
| 224 | } |
| 225 |
| 226 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 227 | ...init, |
| 228 | headers, |
| 229 | }); |
| 230 |
| 231 | return parseApiResponse<T>(response); |
| 232 | } |
| 233 |
| 234 | buildLoginWithColineAuthorizeUrl(input: { |
| 235 | clientId: string; |
| 236 | redirectUri: string; |
| 237 | scope?: string; |
| 238 | state?: string; |
| 239 | codeChallenge?: string; |
| 240 | codeChallengeMethod?: "S256" | "plain"; |
| 241 | }): string { |
| 242 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 243 | url.searchParams.set("response_type", "code"); |
| 244 | url.searchParams.set("client_id", input.clientId); |
| 245 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 246 | if (input.scope) { |
| 247 | url.searchParams.set("scope", input.scope); |
| 248 | } |
| 249 | if (input.state) { |
| 250 | url.searchParams.set("state", input.state); |
| 251 | } |
| 252 | if (input.codeChallenge) { |
| 253 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 254 | } |
| 255 | if (input.codeChallengeMethod) { |
| 256 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 257 | } |
| 258 | return url.toString(); |
| 259 | } |
| 260 |
| 261 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 262 | return this.request<{ apps: RegisteredAppRecord[] }>("/api/v1/apps"); |
| 263 | } |
| 264 |
| 265 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 266 | return this.request<{ app: RegisteredAppDetail }>(`/api/v1/apps/${appKey}`); |
| 267 | } |
| 268 |
| 269 | async listAppVersions( |
| 270 | appKey: string, |
| 271 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 272 | return this.request<{ versions: RegisteredAppVersionSummary[] }>( |
| 273 | `/api/v1/apps/${appKey}/versions`, |
| 274 | ); |
| 275 | } |
| 276 |
| 277 | async registerApp( |
| 278 | payload: ColineAppRegistrationPayload, |
| 279 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 280 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 281 |
| 282 | return this.request<{ |
| 283 | appId: string; |
| 284 | appKey: string; |
| 285 | versionId: string; |
| 286 | version: string; |
| 287 | }>("/api/apps", { |
| 288 | method: "POST", |
| 289 | body: JSON.stringify(body), |
| 290 | }); |
| 291 | } |
| 292 |
| 293 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 294 | return this.request<WorkspaceAppsResponse>( |
| 295 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 296 | ); |
| 297 | } |
| 298 |
| 299 | async installWorkspaceApp( |
| 300 | workspaceSlug: string, |
| 301 | input: InstallWorkspaceAppInput, |
| 302 | ): Promise<InstallWorkspaceAppResponse> { |
| 303 | const grantedPermissions = input.grantedPermissions |
| 304 | ? input.grantedPermissions.map((permission) => |
| 305 | appPermissionSchema.parse(permission), |
| 306 | ) |
| 307 | : undefined; |
| 308 |
| 309 | return this.request<InstallWorkspaceAppResponse>( |
| 310 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 311 | { |
| 312 | method: "POST", |
| 313 | body: JSON.stringify({ |
| 314 | appId: input.appId, |
| 315 | ...(input.pinnedVersionId |
| 316 | ? { pinnedVersionId: input.pinnedVersionId } |
| 317 | : {}), |
| 318 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 319 | }), |
| 320 | }, |
| 321 | ); |
| 322 | } |
| 323 |
| 324 | async getWorkspaceAppPermissions( |
| 325 | workspaceSlug: string, |
| 326 | appKey: string, |
| 327 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 328 | return this.request<WorkspaceAppPermissionsResponse>( |
| 329 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 330 | ); |
| 331 | } |
| 332 |
| 333 | async getWorkspaceInstalledApp( |
| 334 | workspaceSlug: string, |
| 335 | appKey: string, |
| 336 | ): Promise<WorkspaceInstalledAppResponse> { |
| 337 | return this.request<WorkspaceInstalledAppResponse>( |
| 338 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 339 | ); |
| 340 | } |
| 341 |
| 342 | async updateWorkspaceAppPermissions( |
| 343 | workspaceSlug: string, |
| 344 | appKey: string, |
| 345 | input: UpdateWorkspaceAppPermissionsInput, |
| 346 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 347 | return this.request<WorkspaceAppPermissionsResponse>( |
| 348 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 349 | { |
| 350 | method: "PUT", |
| 351 | body: JSON.stringify({ |
| 352 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 353 | appPermissionSchema.parse(permission), |
| 354 | ), |
| 355 | }), |
| 356 | }, |
| 357 | ); |
| 358 | } |
| 359 |
| 360 | async uninstallWorkspaceApp( |
| 361 | workspaceSlug: string, |
| 362 | appKey: string, |
| 363 | ): Promise<{ ok: true }> { |
| 364 | return this.request<{ ok: true }>( |
| 365 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 366 | { |
| 367 | method: "DELETE", |
| 368 | }, |
| 369 | ); |
| 370 | } |
| 371 |
| 372 | async emitAppAmbientEvents( |
| 373 | workspaceSlug: string, |
| 374 | appKey: string, |
| 375 | input: EmitAmbientEventsInputInput, |
| 376 | ): Promise<{ events: Array<{ id: string; dedupeKey: string }> }> { |
| 377 | const body = emitAmbientEventsInputSchema.parse(input); |
| 378 |
| 379 | return this.request<{ events: Array<{ id: string; dedupeKey: string }> }>( |
| 380 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/ambient/events`, |
| 381 | { |
| 382 | method: "POST", |
| 383 | body: JSON.stringify(body), |
| 384 | }, |
| 385 | ); |
| 386 | } |
| 387 |
| 388 | async createAppNotification( |
| 389 | workspaceSlug: string, |
| 390 | appKey: string, |
| 391 | input: CreateNotificationInputInput, |
| 392 | ): Promise<{ ok: true }> { |
| 393 | const body = createNotificationInputSchema.parse(input); |
| 394 |
| 395 | return this.request<{ ok: true }>( |
| 396 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/notifications`, |
| 397 | { |
| 398 | method: "POST", |
| 399 | body: JSON.stringify(body), |
| 400 | }, |
| 401 | ); |
| 402 | } |
| 403 |
| 404 | async upsertAppIndexDocuments( |
| 405 | workspaceSlug: string, |
| 406 | appKey: string, |
| 407 | input: BatchUpsertAppIndexDocumentsInput, |
| 408 | ): Promise<{ documents: Array<{ id: string; documentKey: string; visibilityMode: string }> }> { |
| 409 | const body = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 410 |
| 411 | return this.request<{ |
| 412 | documents: Array<{ id: string; documentKey: string; visibilityMode: string }>; |
| 413 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, { |
| 414 | method: "POST", |
| 415 | body: JSON.stringify(body), |
| 416 | }); |
| 417 | } |
| 418 |
| 419 | async deleteAppIndexDocuments( |
| 420 | workspaceSlug: string, |
| 421 | appKey: string, |
| 422 | input: BatchDeleteAppIndexDocumentsInput, |
| 423 | ): Promise<{ deleted: Array<{ id: string; documentKey: string }> }> { |
| 424 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 425 |
| 426 | return this.request<{ deleted: Array<{ id: string; documentKey: string }> }>( |
| 427 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, |
| 428 | { |
| 429 | method: "DELETE", |
| 430 | body: JSON.stringify(body), |
| 431 | }, |
| 432 | ); |
| 433 | } |
| 434 |
| 435 | async getAppFileDocument( |
| 436 | workspaceSlug: string, |
| 437 | appKey: string, |
| 438 | fileId: string, |
| 439 | ): Promise<AppFileDocumentResponse> { |
| 440 | return this.request<AppFileDocumentResponse>( |
| 441 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, |
| 442 | ); |
| 443 | } |
| 444 |
| 445 | async updateAppFileDocument( |
| 446 | workspaceSlug: string, |
| 447 | appKey: string, |
| 448 | fileId: string, |
| 449 | document: Record<string, unknown>, |
| 450 | ): Promise<{ document: Record<string, unknown>; version: number; updatedAt: string }> { |
| 451 | return this.request<{ |
| 452 | document: Record<string, unknown>; |
| 453 | version: number; |
| 454 | updatedAt: string; |
| 455 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, { |
| 456 | method: "PATCH", |
| 457 | body: JSON.stringify({ document }), |
| 458 | }); |
| 459 | } |
| 460 |
| 461 | async listTabModels(): Promise<{ object: "list"; data: TabModelRecord[] }> { |
| 462 | return this.request<{ object: "list"; data: TabModelRecord[] }>( |
| 463 | "/api/v1/tab/models", |
| 464 | ); |
| 465 | } |
| 466 |
| 467 | async createTabChatCompletion(input: { |
| 468 | model: string; |
| 469 | stream?: boolean; |
| 470 | tab_context: Record<string, unknown>; |
| 471 | messages?: Array<{ role?: string; content?: string }>; |
| 472 | }): Promise<{ |
| 473 | id: string; |
| 474 | object: "chat.completion"; |
| 475 | created: number; |
| 476 | model: string; |
| 477 | choices: Array<{ |
| 478 | index: number; |
| 479 | message: { role: "assistant"; content: string }; |
| 480 | finish_reason: "stop"; |
| 481 | }>; |
| 482 | usage: { |
| 483 | prompt_tokens: number; |
| 484 | completion_tokens: number; |
| 485 | total_tokens: number; |
| 486 | }; |
| 487 | }> { |
| 488 | return this.request("/api/v1/tab/chat/completions", { |
| 489 | method: "POST", |
| 490 | body: JSON.stringify(input), |
| 491 | }); |
| 492 | } |
| 493 |
| 494 | async exchangeOAuthCode(input: { |
| 495 | clientId: string; |
| 496 | clientSecret: string; |
| 497 | code: string; |
| 498 | redirectUri: string; |
| 499 | codeVerifier?: string; |
| 500 | }): Promise<OAuthTokenResponse> { |
| 501 | return this.request<OAuthTokenResponse>("/api/v1/oauth/token", { |
| 502 | method: "POST", |
| 503 | body: JSON.stringify({ |
| 504 | grant_type: "authorization_code", |
| 505 | client_id: input.clientId, |
| 506 | client_secret: input.clientSecret, |
| 507 | code: input.code, |
| 508 | redirect_uri: input.redirectUri, |
| 509 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 510 | }), |
| 511 | }); |
| 512 | } |
| 513 |
| 514 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 515 | return this.request<OAuthUserInfoResponse>("/api/v1/oauth/userinfo", { |
| 516 | method: "GET", |
| 517 | headers: { |
| 518 | Authorization: `Bearer ${accessToken}`, |
| 519 | }, |
| 520 | }); |
| 521 | } |
| 522 | } |
| 523 |