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 ColineAppRegistrationPayloadInput, |
| 13 | type CreateNotificationInputInput, |
| 14 | type EmitAmbientEventsInputInput, |
| 15 | } from "./manifest"; |
| 16 | import { ColineApiError } from "./errors"; |
| 17 | import type { components as AppPlatformComponents } from "./generated/app-platform-v1"; |
| 18 | import { |
| 19 | createAppPlatformClient, |
| 20 | type AppPlatformClient, |
| 21 | } from "./app-platform-client"; |
| 22 |
| 23 | interface ApiErrorPayload { |
| 24 | error?: { |
| 25 | code?: string; |
| 26 | message?: string; |
| 27 | }; |
| 28 | } |
| 29 |
| 30 | async function parseJsonResponse<T>(response: Response): Promise<T> { |
| 31 | const payload = (await response.json().catch(() => null)) as T | null; |
| 32 |
| 33 | if (!response.ok) { |
| 34 | const errorMessage = |
| 35 | payload && |
| 36 | typeof payload === "object" && |
| 37 | "error_description" in payload && |
| 38 | typeof (payload as Record<string, unknown>)["error_description"] === "string" |
| 39 | ? ((payload as Record<string, unknown>)["error_description"] as string) |
| 40 | : `Request failed (${response.status}).`; |
| 41 |
| 42 | throw new ColineApiError({ |
| 43 | message: errorMessage, |
| 44 | type: "oauth", |
| 45 | status: response.status, |
| 46 | code: |
| 47 | payload && |
| 48 | typeof payload === "object" && |
| 49 | "error" in payload && |
| 50 | typeof (payload as Record<string, unknown>)["error"] === "string" |
| 51 | ? ((payload as Record<string, unknown>)["error"] as string) |
| 52 | : null, |
| 53 | }); |
| 54 | } |
| 55 |
| 56 | if (!payload) { |
| 57 | throw new ColineApiError({ |
| 58 | message: "Invalid JSON response.", |
| 59 | type: "oauth", |
| 60 | status: response.status, |
| 61 | }); |
| 62 | } |
| 63 |
| 64 | return payload; |
| 65 | } |
| 66 |
| 67 | export interface ColineApiClientOptions { |
| 68 | baseUrl: string; |
| 69 | apiKey?: string; |
| 70 | fetch?: typeof fetch; |
| 71 | headers?: HeadersInit; |
| 72 | } |
| 73 |
| 74 | type Schemas = AppPlatformComponents["schemas"]; |
| 75 | type UnwrapData<TKey extends keyof Schemas> = |
| 76 | Schemas[TKey] extends { data: infer T } ? T : never; |
| 77 |
| 78 | export type RegisteredAppDetail = Schemas["RegisteredAppDetail"]; |
| 79 | export type RegisteredAppRecord = Omit< |
| 80 | RegisteredAppDetail, |
| 81 | "latestPublishedVersionId" |
| 82 | >; |
| 83 | export type RegisteredAppVersionSummary = Schemas["RegisteredAppVersionSummary"]; |
| 84 | export type WorkspaceInstalledApp = Schemas["InstalledAppSummary"]; |
| 85 | export type WorkspaceAppCatalogItem = Schemas["AppCatalogItem"]; |
| 86 | export type WorkspaceAppsResponse = UnwrapData<"WorkspaceAppsResponse">; |
| 87 |
| 88 | export interface InstallWorkspaceAppInput { |
| 89 | appId: string; |
| 90 | pinnedVersionId?: string; |
| 91 | grantedPermissions?: AppPermission[]; |
| 92 | } |
| 93 |
| 94 | export type InstallWorkspaceAppResponse = UnwrapData<"InstallWorkspaceAppResponse">; |
| 95 |
| 96 | export interface SubmitAppVersionForReviewInput { |
| 97 | storeListed?: boolean; |
| 98 | } |
| 99 |
| 100 | export type SubmitAppVersionForReviewResponse = UnwrapData< |
| 101 | "SubmitAppVersionForReviewResponse" |
| 102 | >; |
| 103 | export type WorkspaceAppPermissionsResponse = UnwrapData< |
| 104 | "WorkspaceAppPermissionsResponse" |
| 105 | >; |
| 106 | export type WorkspaceInstalledAppResponse = UnwrapData< |
| 107 | "WorkspaceInstalledAppResponse" |
| 108 | >; |
| 109 | export type WorkspaceAppSecretsResponse = UnwrapData< |
| 110 | "WorkspaceAppSecretsResponse" |
| 111 | >; |
| 112 | export type WorkspaceAppSecretSummary = WorkspaceAppSecretsResponse["secrets"][number]; |
| 113 | export type CreateWorkspaceAppSecretResponse = UnwrapData< |
| 114 | "CreateWorkspaceAppSecretResponse" |
| 115 | >; |
| 116 | export type WorkspaceAppOauthConnectionsResponse = UnwrapData< |
| 117 | "WorkspaceAppOauthConnectionsResponse" |
| 118 | >; |
| 119 | export type WorkspaceAppOauthConnectionSummary = |
| 120 | WorkspaceAppOauthConnectionsResponse["connections"][number]; |
| 121 | export type CreateWorkspaceAppOauthConnectionResponse = UnwrapData< |
| 122 | "CreateWorkspaceAppOauthConnectionResponse" |
| 123 | >; |
| 124 | export type WorkspaceAppDeliveriesResponse = UnwrapData< |
| 125 | "WorkspaceAppDeliveriesResponse" |
| 126 | >; |
| 127 | export type WorkspaceAppDeliverySummary = |
| 128 | WorkspaceAppDeliveriesResponse["deliveries"][number]; |
| 129 | export type SendWorkspaceAppTestDeliveryResponse = UnwrapData< |
| 130 | "SendWorkspaceAppTestDeliveryResponse" |
| 131 | >; |
| 132 | export type AppFilesListResponse = UnwrapData<"AppFilesListResponse">; |
| 133 | export type AppFileDocumentResponse = UnwrapData<"AppFileDocumentResponse">; |
| 134 | export type AppFileDocumentUpdateResponse = UnwrapData< |
| 135 | "AppFileDocumentUpdateResponse" |
| 136 | >; |
| 137 | export type CreateAppFileResponse = UnwrapData<"CreateAppFileResponse">; |
| 138 | export type ExecuteAppActionResponse = UnwrapData<"ExecuteAppActionResponse">; |
| 139 | export type OkResponse = UnwrapData<"OkResponse">; |
| 140 | export type TabModelRecord = Schemas["TabModelRecord"]; |
| 141 | export type TabModelsResponse = UnwrapData<"TabModelsResponse">; |
| 142 | export type TabChatCompletionResponse = UnwrapData< |
| 143 | "TabChatCompletionResponseEnvelope" |
| 144 | >; |
| 145 | export type UpsertAppIndexDocumentsResponse = UnwrapData< |
| 146 | "UpsertAppIndexDocumentsResponse" |
| 147 | >; |
| 148 | export type DeleteAppIndexDocumentsResponse = UnwrapData< |
| 149 | "DeleteAppIndexDocumentsResponse" |
| 150 | >; |
| 151 | export type EmitAppAmbientEventsResponse = UnwrapData< |
| 152 | "EmitAppAmbientEventsResponse" |
| 153 | >; |
| 154 |
| 155 | export interface OAuthTokenResponse { |
| 156 | access_token: string; |
| 157 | token_type: "Bearer"; |
| 158 | expires_in: number; |
| 159 | refresh_token: string; |
| 160 | scope: string; |
| 161 | } |
| 162 |
| 163 | export interface OAuthUserInfoResponse { |
| 164 | sub: string; |
| 165 | email: string; |
| 166 | email_verified: boolean; |
| 167 | name: string; |
| 168 | given_name: string | null; |
| 169 | family_name: string | null; |
| 170 | picture: string | null; |
| 171 | preferred_username: string | null; |
| 172 | scope: string; |
| 173 | } |
| 174 |
| 175 | export interface UpdateWorkspaceAppPermissionsInput { |
| 176 | grantedPermissions: AppPermission[]; |
| 177 | } |
| 178 |
| 179 | export interface CreateAppFileInput { |
| 180 | name: string; |
| 181 | typeKey?: string; |
| 182 | driveId?: string; |
| 183 | parentId?: string | null; |
| 184 | document?: Record<string, unknown>; |
| 185 | } |
| 186 |
| 187 | export interface ExecuteAppActionInput { |
| 188 | action: { |
| 189 | type: string; |
| 190 | payload?: Record<string, unknown>; |
| 191 | }; |
| 192 | fileId?: string | null; |
| 193 | } |
| 194 |
| 195 | // --------------------------------------------------------------------------- |
| 196 | // Core Platform API — Types |
| 197 | // --------------------------------------------------------------------------- |
| 198 |
| 199 | export interface WorkspaceMember { |
| 200 | id: string; |
| 201 | workspaceMemberId: string; |
| 202 | email: string; |
| 203 | firstName: string | null; |
| 204 | lastName: string | null; |
| 205 | avatarUrl: string | null; |
| 206 | joinedAt: string; |
| 207 | roles: string[]; |
| 208 | } |
| 209 |
| 210 | export interface WorkspaceMembersResponse { |
| 211 | members: WorkspaceMember[]; |
| 212 | } |
| 213 |
| 214 | export interface WorkspaceNoteSummary { |
| 215 | id: string; |
| 216 | driveId: string; |
| 217 | parentId: string | null; |
| 218 | title: string; |
| 219 | body: string; |
| 220 | color: string | null; |
| 221 | createdByUserId: string | null; |
| 222 | createdAt: string; |
| 223 | updatedAt: string; |
| 224 | } |
| 225 |
| 226 | export interface WorkspaceNoteDetail extends WorkspaceNoteSummary { |
| 227 | blocks: unknown[]; |
| 228 | } |
| 229 |
| 230 | export interface WorkspaceNotesListResponse { |
| 231 | notes: WorkspaceNoteSummary[]; |
| 232 | page: { limit: number; hasMore: boolean; nextCursor: string | null }; |
| 233 | } |
| 234 |
| 235 | export interface WorkspaceNoteResponse { |
| 236 | note: WorkspaceNoteSummary; |
| 237 | } |
| 238 |
| 239 | export interface WorkspaceNoteDetailResponse { |
| 240 | note: WorkspaceNoteDetail; |
| 241 | } |
| 242 |
| 243 | export interface ListWorkspaceNotesQuery { |
| 244 | q?: string; |
| 245 | limit?: number; |
| 246 | cursor?: string; |
| 247 | } |
| 248 |
| 249 | export interface CreateWorkspaceNoteInput { |
| 250 | title?: string; |
| 251 | body?: string; |
| 252 | blocks?: unknown[]; |
| 253 | driveId?: string; |
| 254 | parentId?: string | null; |
| 255 | } |
| 256 |
| 257 | export interface UpdateWorkspaceNoteInput { |
| 258 | title?: string; |
| 259 | body?: string; |
| 260 | blocks?: unknown[]; |
| 261 | } |
| 262 |
| 263 | export interface CalendarEventLinkInput { |
| 264 | linkType: "file" | "channel" | "url"; |
| 265 | fileId?: string; |
| 266 | channelId?: string; |
| 267 | url?: string; |
| 268 | label?: string | null; |
| 269 | } |
| 270 |
| 271 | export interface CalendarRecurrenceRule { |
| 272 | frequency: "daily" | "weekly" | "monthly" | "yearly"; |
| 273 | interval: number; |
| 274 | byDay?: Array<"MO" | "TU" | "WE" | "TH" | "FR" | "SA" | "SU">; |
| 275 | count?: number; |
| 276 | until?: string; |
| 277 | } |
| 278 |
| 279 | export interface CreateCalendarEventInput { |
| 280 | title: string; |
| 281 | description?: string | null; |
| 282 | location?: string | null; |
| 283 | startsAt: string; |
| 284 | endsAt: string; |
| 285 | timeZone: string; |
| 286 | isAllDay?: boolean; |
| 287 | visibility?: "private" | "invitees" | "workspace"; |
| 288 | attendeeUserIds?: string[]; |
| 289 | linkInputs?: CalendarEventLinkInput[]; |
| 290 | recurrenceRule?: CalendarRecurrenceRule | null; |
| 291 | } |
| 292 |
| 293 | export interface UpdateCalendarEventInput { |
| 294 | title?: string; |
| 295 | description?: string | null; |
| 296 | location?: string | null; |
| 297 | startsAt?: string; |
| 298 | endsAt?: string; |
| 299 | timeZone?: string; |
| 300 | isAllDay?: boolean; |
| 301 | visibility?: "private" | "invitees" | "workspace"; |
| 302 | status?: "active" | "canceled"; |
| 303 | attendeeUserIds?: string[]; |
| 304 | linkInputs?: CalendarEventLinkInput[]; |
| 305 | recurrenceRule?: CalendarRecurrenceRule | null; |
| 306 | } |
| 307 |
| 308 | /** |
| 309 | * Calendar events list response — shape matches the server's `listCalendarEvents` output. |
| 310 | */ |
| 311 | export type CalendarEventsListResponse = Record<string, unknown>; |
| 312 |
| 313 | /** |
| 314 | * Calendar event detail response — shape matches the server's `getCalendarEventDetail` output. |
| 315 | */ |
| 316 | export type CalendarEventDetailResponse = Record<string, unknown>; |
| 317 |
| 318 | export interface WorkspaceSearchInput { |
| 319 | query: string; |
| 320 | sourceKinds?: Array< |
| 321 | "file" | "task" | "container_message" | "kairo_message" | "app_document" |
| 322 | >; |
| 323 | limit?: number; |
| 324 | cursor?: string; |
| 325 | } |
| 326 |
| 327 | export interface WorkspaceSearchResult { |
| 328 | id: string; |
| 329 | title: string | null; |
| 330 | snippet: string | null; |
| 331 | sourceKind: string; |
| 332 | sourceId: string; |
| 333 | url: string | null; |
| 334 | score: number; |
| 335 | } |
| 336 |
| 337 | export interface WorkspaceSearchResponse { |
| 338 | results: WorkspaceSearchResult[]; |
| 339 | nextCursor: string | null; |
| 340 | } |
| 341 |
| 342 | export interface TaskboardStatusSummary { |
| 343 | id: string; |
| 344 | key: string; |
| 345 | name: string; |
| 346 | color: string; |
| 347 | isDefault: boolean; |
| 348 | isCompleted: boolean; |
| 349 | sortOrder: number; |
| 350 | } |
| 351 |
| 352 | export interface TaskboardSummary { |
| 353 | id: string; |
| 354 | name: string; |
| 355 | driveId: string; |
| 356 | color: string | null; |
| 357 | identifierPrefix: string | null; |
| 358 | description: string | null; |
| 359 | defaultView: string | null; |
| 360 | statuses: TaskboardStatusSummary[]; |
| 361 | createdAt: string; |
| 362 | updatedAt: string; |
| 363 | } |
| 364 |
| 365 | export interface WorkspaceTaskboardsResponse { |
| 366 | taskboards: TaskboardSummary[]; |
| 367 | } |
| 368 |
| 369 | export interface TaskAssignee { |
| 370 | id: string; |
| 371 | firstName: string | null; |
| 372 | lastName: string | null; |
| 373 | avatarUrl: string | null; |
| 374 | email: string; |
| 375 | } |
| 376 |
| 377 | export interface TaskSummary { |
| 378 | id: string; |
| 379 | taskboardId: string; |
| 380 | taskNumber: number; |
| 381 | identifier: string | null; |
| 382 | title: string; |
| 383 | description: string | null; |
| 384 | status: { |
| 385 | id: string; |
| 386 | key: string; |
| 387 | name: string; |
| 388 | color: string; |
| 389 | isCompleted: boolean; |
| 390 | } | null; |
| 391 | priority: string; |
| 392 | labels: string[]; |
| 393 | dueDate: string | null; |
| 394 | assignees: TaskAssignee[]; |
| 395 | isCompleted: boolean; |
| 396 | completedAt: string | null; |
| 397 | createdByUserId: string | null; |
| 398 | createdAt: string; |
| 399 | updatedAt: string; |
| 400 | } |
| 401 |
| 402 | export interface TaskboardTasksResponse { |
| 403 | tasks: TaskSummary[]; |
| 404 | } |
| 405 |
| 406 | export interface ListTaskboardTasksQuery { |
| 407 | q?: string; |
| 408 | statusId?: string; |
| 409 | assigneeUserId?: string; |
| 410 | limit?: number; |
| 411 | } |
| 412 |
| 413 | export interface CreateTaskInput { |
| 414 | title: string; |
| 415 | description?: string; |
| 416 | statusId?: string; |
| 417 | priority?: "none" | "low" | "medium" | "high" | "urgent"; |
| 418 | dueDate?: string | null; |
| 419 | labels?: string[]; |
| 420 | assigneeUserIds?: string[]; |
| 421 | } |
| 422 |
| 423 | export interface CreateTaskResponse { |
| 424 | task: { |
| 425 | id: string; |
| 426 | taskboardId: string; |
| 427 | title: string; |
| 428 | description: string; |
| 429 | statusId: string; |
| 430 | priority: string; |
| 431 | labels: string[]; |
| 432 | dueDate: string | null; |
| 433 | assigneeUserIds: string[]; |
| 434 | createdByUserId: string; |
| 435 | createdAt: string; |
| 436 | updatedAt: string; |
| 437 | }; |
| 438 | } |
| 439 |
| 440 | export interface UpdateTaskInput { |
| 441 | title?: string; |
| 442 | description?: string; |
| 443 | statusId?: string; |
| 444 | priority?: "none" | "low" | "medium" | "high" | "urgent"; |
| 445 | dueDate?: string | null; |
| 446 | labels?: string[]; |
| 447 | assigneeUserIds?: string[]; |
| 448 | } |
| 449 |
| 450 | function joinUrl(baseUrl: string, path: string): string { |
| 451 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 452 | } |
| 453 |
| 454 | function toRegisterAppInput(payload: ColineAppRegistrationPayload) { |
| 455 | const notificationChannels = payload.manifest.notificationChannels.map((channel) => ({ |
| 456 | key: channel.key, |
| 457 | name: channel.name, |
| 458 | defaultDeliveries: [...channel.defaultDeliveries], |
| 459 | defaultPriority: channel.defaultPriority, |
| 460 | ...(channel.description ? { description: channel.description } : {}), |
| 461 | })); |
| 462 |
| 463 | const ui = |
| 464 | payload.manifest.ui.mode || payload.manifest.ui.homePath || payload.manifest.ui.filePath |
| 465 | ? { |
| 466 | ...(payload.manifest.ui.mode |
| 467 | ? { mode: payload.manifest.ui.mode } |
| 468 | : {}), |
| 469 | ...(payload.manifest.ui.homePath |
| 470 | ? { homePath: payload.manifest.ui.homePath } |
| 471 | : {}), |
| 472 | ...(payload.manifest.ui.filePath |
| 473 | ? { filePath: payload.manifest.ui.filePath } |
| 474 | : {}), |
| 475 | } |
| 476 | : undefined; |
| 477 |
| 478 | const manifest = { |
| 479 | key: payload.manifest.key, |
| 480 | name: payload.manifest.name, |
| 481 | permissions: payload.manifest.permissions, |
| 482 | notificationChannels, |
| 483 | hosting: { |
| 484 | mode: payload.manifest.hosting.mode, |
| 485 | baseUrl: payload.manifest.hosting.baseUrl, |
| 486 | }, |
| 487 | ...(ui ? { ui } : {}), |
| 488 | ...(payload.manifest.description |
| 489 | ? { description: payload.manifest.description } |
| 490 | : {}), |
| 491 | ...(payload.manifest.iconUrl ? { iconUrl: payload.manifest.iconUrl } : {}), |
| 492 | ...(payload.manifest.kairo |
| 493 | ? { |
| 494 | kairo: JSON.parse(JSON.stringify(payload.manifest.kairo)) as Record< |
| 495 | string, |
| 496 | unknown |
| 497 | >, |
| 498 | } |
| 499 | : {}), |
| 500 | }; |
| 501 |
| 502 | const fileTypes = payload.fileTypes.map((fileType) => ({ |
| 503 | typeKey: fileType.typeKey, |
| 504 | name: fileType.name, |
| 505 | storage: fileType.storage, |
| 506 | indexable: fileType.indexable, |
| 507 | homeRenderMode: fileType.homeRenderMode, |
| 508 | ...(fileType.description ? { description: fileType.description } : {}), |
| 509 | })); |
| 510 |
| 511 | return { |
| 512 | version: payload.version, |
| 513 | manifest, |
| 514 | fileTypes, |
| 515 | }; |
| 516 | } |
| 517 |
| 518 | export class ColineApiClient { |
| 519 | private readonly baseUrl: string; |
| 520 | private readonly apiKey: string | undefined; |
| 521 | private readonly fetchImpl: typeof fetch; |
| 522 | private readonly defaultHeaders: HeadersInit | undefined; |
| 523 | private readonly appPlatformClient: AppPlatformClient; |
| 524 |
| 525 | constructor(options: ColineApiClientOptions) { |
| 526 | this.baseUrl = options.baseUrl; |
| 527 | this.apiKey = options.apiKey; |
| 528 | this.fetchImpl = options.fetch ?? fetch; |
| 529 | this.defaultHeaders = options.headers; |
| 530 | this.appPlatformClient = createAppPlatformClient(options); |
| 531 | } |
| 532 |
| 533 | private async unwrapGeneratedResponse<T>( |
| 534 | promise: Promise<{ |
| 535 | data?: { data: T }; |
| 536 | error?: ApiErrorPayload; |
| 537 | response: Response; |
| 538 | }>, |
| 539 | ): Promise<T> { |
| 540 | const { data, error, response } = await promise; |
| 541 |
| 542 | if (error) { |
| 543 | throw new ColineApiError({ |
| 544 | message: error.error?.message ?? `Request failed (${response.status}).`, |
| 545 | type: "api", |
| 546 | status: response.status, |
| 547 | code: error.error?.code ?? null, |
| 548 | }); |
| 549 | } |
| 550 |
| 551 | if (data === undefined) { |
| 552 | throw new ColineApiError({ |
| 553 | message: "Invalid API response.", |
| 554 | type: "api", |
| 555 | status: response.status, |
| 556 | }); |
| 557 | } |
| 558 |
| 559 | return data.data; |
| 560 | } |
| 561 |
| 562 | withApiKey(apiKey: string): ColineApiClient { |
| 563 | const options: ColineApiClientOptions = { |
| 564 | baseUrl: this.baseUrl, |
| 565 | apiKey, |
| 566 | fetch: this.fetchImpl, |
| 567 | }; |
| 568 |
| 569 | if (this.defaultHeaders !== undefined) { |
| 570 | options.headers = this.defaultHeaders; |
| 571 | } |
| 572 |
| 573 | return new ColineApiClient(options); |
| 574 | } |
| 575 |
| 576 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 577 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 578 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 579 |
| 580 | const options: ColineApiClientOptions = { |
| 581 | baseUrl: this.baseUrl, |
| 582 | fetch: this.fetchImpl, |
| 583 | headers: mergedHeaders, |
| 584 | }; |
| 585 |
| 586 | if (this.apiKey !== undefined) { |
| 587 | options.apiKey = this.apiKey; |
| 588 | } |
| 589 |
| 590 | return new ColineApiClient(options); |
| 591 | } |
| 592 |
| 593 | buildLoginWithColineAuthorizeUrl(input: { |
| 594 | clientId: string; |
| 595 | redirectUri: string; |
| 596 | scope?: string; |
| 597 | state?: string; |
| 598 | codeChallenge?: string; |
| 599 | codeChallengeMethod?: "S256" | "plain"; |
| 600 | }): string { |
| 601 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 602 | url.searchParams.set("response_type", "code"); |
| 603 | url.searchParams.set("client_id", input.clientId); |
| 604 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 605 | if (input.scope) { |
| 606 | url.searchParams.set("scope", input.scope); |
| 607 | } |
| 608 | if (input.state) { |
| 609 | url.searchParams.set("state", input.state); |
| 610 | } |
| 611 | if (input.codeChallenge) { |
| 612 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 613 | } |
| 614 | if (input.codeChallengeMethod) { |
| 615 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 616 | } |
| 617 | return url.toString(); |
| 618 | } |
| 619 |
| 620 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 621 | return this.unwrapGeneratedResponse( |
| 622 | this.appPlatformClient.GET("/api/v1/apps"), |
| 623 | ); |
| 624 | } |
| 625 |
| 626 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 627 | return this.unwrapGeneratedResponse( |
| 628 | this.appPlatformClient.GET("/api/v1/apps/{appKey}", { |
| 629 | params: { path: { appKey } }, |
| 630 | }), |
| 631 | ); |
| 632 | } |
| 633 |
| 634 | async listAppVersions( |
| 635 | appKey: string, |
| 636 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 637 | return this.unwrapGeneratedResponse( |
| 638 | this.appPlatformClient.GET("/api/v1/apps/{appKey}/versions", { |
| 639 | params: { path: { appKey } }, |
| 640 | }), |
| 641 | ); |
| 642 | } |
| 643 |
| 644 | async registerApp( |
| 645 | payload: ColineAppRegistrationPayloadInput, |
| 646 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 647 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 648 |
| 649 | return this.unwrapGeneratedResponse( |
| 650 | this.appPlatformClient.POST("/api/v1/apps", { |
| 651 | body, |
| 652 | }), |
| 653 | ); |
| 654 | } |
| 655 |
| 656 | async createAppVersion( |
| 657 | appKey: string, |
| 658 | payload: ColineAppRegistrationPayloadInput, |
| 659 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 660 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 661 |
| 662 | return this.unwrapGeneratedResponse( |
| 663 | this.appPlatformClient.POST("/api/v1/apps/{appKey}/versions", { |
| 664 | params: { path: { appKey } }, |
| 665 | body, |
| 666 | }), |
| 667 | ); |
| 668 | } |
| 669 |
| 670 | async submitAppVersionForReview( |
| 671 | appKey: string, |
| 672 | versionId: string, |
| 673 | input: SubmitAppVersionForReviewInput = {}, |
| 674 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 675 | return this.unwrapGeneratedResponse( |
| 676 | this.appPlatformClient.POST( |
| 677 | "/api/v1/apps/{appKey}/versions/{versionId}/submit", |
| 678 | { |
| 679 | params: { path: { appKey, versionId } }, |
| 680 | body: { |
| 681 | ...(input.storeListed !== undefined |
| 682 | ? { storeListed: input.storeListed } |
| 683 | : {}), |
| 684 | }, |
| 685 | }, |
| 686 | ), |
| 687 | ); |
| 688 | } |
| 689 |
| 690 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 691 | return this.unwrapGeneratedResponse( |
| 692 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 693 | params: { path: { workspaceSlug } }, |
| 694 | }), |
| 695 | ); |
| 696 | } |
| 697 |
| 698 | async installWorkspaceApp( |
| 699 | workspaceSlug: string, |
| 700 | input: InstallWorkspaceAppInput, |
| 701 | ): Promise<InstallWorkspaceAppResponse> { |
| 702 | const grantedPermissions = input.grantedPermissions |
| 703 | ? input.grantedPermissions.map((permission) => |
| 704 | appPermissionSchema.parse(permission), |
| 705 | ) |
| 706 | : undefined; |
| 707 |
| 708 | return this.unwrapGeneratedResponse( |
| 709 | this.appPlatformClient.POST("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 710 | params: { path: { workspaceSlug } }, |
| 711 | body: { |
| 712 | appId: input.appId, |
| 713 | ...(input.pinnedVersionId |
| 714 | ? { pinnedVersionId: input.pinnedVersionId } |
| 715 | : {}), |
| 716 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 717 | }, |
| 718 | }), |
| 719 | ); |
| 720 | } |
| 721 |
| 722 | async getWorkspaceAppPermissions( |
| 723 | workspaceSlug: string, |
| 724 | appKey: string, |
| 725 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 726 | return this.unwrapGeneratedResponse( |
| 727 | this.appPlatformClient.GET( |
| 728 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 729 | { |
| 730 | params: { path: { workspaceSlug, appKey } }, |
| 731 | }, |
| 732 | ), |
| 733 | ); |
| 734 | } |
| 735 |
| 736 | async getWorkspaceInstalledApp( |
| 737 | workspaceSlug: string, |
| 738 | appKey: string, |
| 739 | ): Promise<WorkspaceInstalledAppResponse> { |
| 740 | return this.unwrapGeneratedResponse( |
| 741 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", { |
| 742 | params: { path: { workspaceSlug, appKey } }, |
| 743 | }), |
| 744 | ); |
| 745 | } |
| 746 |
| 747 | async updateWorkspaceAppPermissions( |
| 748 | workspaceSlug: string, |
| 749 | appKey: string, |
| 750 | input: UpdateWorkspaceAppPermissionsInput, |
| 751 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 752 | return this.unwrapGeneratedResponse( |
| 753 | this.appPlatformClient.PUT( |
| 754 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 755 | { |
| 756 | params: { path: { workspaceSlug, appKey } }, |
| 757 | body: { |
| 758 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 759 | appPermissionSchema.parse(permission), |
| 760 | ), |
| 761 | }, |
| 762 | }, |
| 763 | ), |
| 764 | ); |
| 765 | } |
| 766 |
| 767 | async uninstallWorkspaceApp( |
| 768 | workspaceSlug: string, |
| 769 | appKey: string, |
| 770 | ): Promise<{ ok: true }> { |
| 771 | return this.unwrapGeneratedResponse( |
| 772 | this.appPlatformClient.DELETE( |
| 773 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", |
| 774 | { |
| 775 | params: { path: { workspaceSlug, appKey } }, |
| 776 | }, |
| 777 | ), |
| 778 | ); |
| 779 | } |
| 780 |
| 781 | async listWorkspaceAppSecrets( |
| 782 | workspaceSlug: string, |
| 783 | appKey: string, |
| 784 | ): Promise<WorkspaceAppSecretsResponse> { |
| 785 | return this.unwrapGeneratedResponse( |
| 786 | this.appPlatformClient.GET( |
| 787 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 788 | { |
| 789 | params: { path: { workspaceSlug, appKey } }, |
| 790 | }, |
| 791 | ), |
| 792 | ); |
| 793 | } |
| 794 |
| 795 | async createWorkspaceAppSecret( |
| 796 | workspaceSlug: string, |
| 797 | appKey: string, |
| 798 | input: { key: string; value: string }, |
| 799 | ): Promise<CreateWorkspaceAppSecretResponse> { |
| 800 | return this.unwrapGeneratedResponse( |
| 801 | this.appPlatformClient.POST( |
| 802 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 803 | { |
| 804 | params: { path: { workspaceSlug, appKey } }, |
| 805 | body: input, |
| 806 | }, |
| 807 | ), |
| 808 | ); |
| 809 | } |
| 810 |
| 811 | async deleteWorkspaceAppSecret( |
| 812 | workspaceSlug: string, |
| 813 | appKey: string, |
| 814 | secretId: string, |
| 815 | ): Promise<{ ok: true }> { |
| 816 | return this.unwrapGeneratedResponse( |
| 817 | this.appPlatformClient.DELETE( |
| 818 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets/{secretId}", |
| 819 | { |
| 820 | params: { path: { workspaceSlug, appKey, secretId } }, |
| 821 | }, |
| 822 | ), |
| 823 | ); |
| 824 | } |
| 825 |
| 826 | async listWorkspaceAppOauthConnections( |
| 827 | workspaceSlug: string, |
| 828 | appKey: string, |
| 829 | ): Promise<WorkspaceAppOauthConnectionsResponse> { |
| 830 | return this.unwrapGeneratedResponse( |
| 831 | this.appPlatformClient.GET( |
| 832 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 833 | { |
| 834 | params: { path: { workspaceSlug, appKey } }, |
| 835 | }, |
| 836 | ), |
| 837 | ); |
| 838 | } |
| 839 |
| 840 | async createWorkspaceAppOauthConnection( |
| 841 | workspaceSlug: string, |
| 842 | appKey: string, |
| 843 | input: { |
| 844 | providerKey: string; |
| 845 | externalAccountId?: string | null; |
| 846 | accessToken: string; |
| 847 | refreshToken?: string | null; |
| 848 | scopes: string[]; |
| 849 | tokenExpiresAt?: string | null; |
| 850 | metadata?: Record<string, unknown>; |
| 851 | }, |
| 852 | ): Promise<CreateWorkspaceAppOauthConnectionResponse> { |
| 853 | const body: { |
| 854 | providerKey: string; |
| 855 | accessToken: string; |
| 856 | scopes: string[]; |
| 857 | externalAccountId?: string; |
| 858 | refreshToken?: string; |
| 859 | tokenExpiresAt?: string; |
| 860 | metadata?: Record<string, unknown>; |
| 861 | } = { |
| 862 | providerKey: input.providerKey, |
| 863 | accessToken: input.accessToken, |
| 864 | scopes: input.scopes, |
| 865 | }; |
| 866 |
| 867 | if (input.externalAccountId) { |
| 868 | body.externalAccountId = input.externalAccountId; |
| 869 | } |
| 870 | if (input.refreshToken) { |
| 871 | body.refreshToken = input.refreshToken; |
| 872 | } |
| 873 | if (input.tokenExpiresAt) { |
| 874 | body.tokenExpiresAt = input.tokenExpiresAt; |
| 875 | } |
| 876 | if (input.metadata) { |
| 877 | body.metadata = input.metadata; |
| 878 | } |
| 879 |
| 880 | return this.unwrapGeneratedResponse( |
| 881 | this.appPlatformClient.POST( |
| 882 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 883 | { |
| 884 | params: { path: { workspaceSlug, appKey } }, |
| 885 | body, |
| 886 | }, |
| 887 | ), |
| 888 | ); |
| 889 | } |
| 890 |
| 891 | async deleteWorkspaceAppOauthConnection( |
| 892 | workspaceSlug: string, |
| 893 | appKey: string, |
| 894 | connectionId: string, |
| 895 | ): Promise<{ ok: true }> { |
| 896 | return this.unwrapGeneratedResponse( |
| 897 | this.appPlatformClient.DELETE( |
| 898 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections/{connectionId}", |
| 899 | { |
| 900 | params: { path: { workspaceSlug, appKey, connectionId } }, |
| 901 | }, |
| 902 | ), |
| 903 | ); |
| 904 | } |
| 905 |
| 906 | async listWorkspaceAppDeliveries( |
| 907 | workspaceSlug: string, |
| 908 | appKey: string, |
| 909 | ): Promise<WorkspaceAppDeliveriesResponse> { |
| 910 | return this.unwrapGeneratedResponse( |
| 911 | this.appPlatformClient.GET( |
| 912 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 913 | { |
| 914 | params: { path: { workspaceSlug, appKey } }, |
| 915 | }, |
| 916 | ), |
| 917 | ); |
| 918 | } |
| 919 |
| 920 | async sendWorkspaceAppTestDelivery( |
| 921 | workspaceSlug: string, |
| 922 | appKey: string, |
| 923 | input: { |
| 924 | eventType: string; |
| 925 | targetUrl?: string; |
| 926 | payload: Record<string, unknown>; |
| 927 | }, |
| 928 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 929 | return this.unwrapGeneratedResponse( |
| 930 | this.appPlatformClient.POST( |
| 931 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 932 | { |
| 933 | params: { path: { workspaceSlug, appKey } }, |
| 934 | body: input, |
| 935 | }, |
| 936 | ), |
| 937 | ); |
| 938 | } |
| 939 |
| 940 | async replayWorkspaceAppDelivery( |
| 941 | workspaceSlug: string, |
| 942 | appKey: string, |
| 943 | deliveryId: string, |
| 944 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 945 | return this.unwrapGeneratedResponse( |
| 946 | this.appPlatformClient.POST( |
| 947 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries/{deliveryId}/replay", |
| 948 | { |
| 949 | params: { path: { workspaceSlug, appKey, deliveryId } }, |
| 950 | }, |
| 951 | ), |
| 952 | ); |
| 953 | } |
| 954 |
| 955 | async emitAppAmbientEvents( |
| 956 | workspaceSlug: string, |
| 957 | appKey: string, |
| 958 | input: EmitAmbientEventsInputInput, |
| 959 | ): Promise<EmitAppAmbientEventsResponse> { |
| 960 | const parsed = emitAmbientEventsInputSchema.parse(input); |
| 961 | const body = { |
| 962 | events: parsed.events.map((event) => ({ |
| 963 | sourceKind: event.sourceKind, |
| 964 | action: event.action, |
| 965 | entityType: event.entityType, |
| 966 | entityId: event.entityId, |
| 967 | dedupeKey: event.dedupeKey, |
| 968 | payload: event.payload, |
| 969 | metadata: event.metadata, |
| 970 | schemaVersion: event.schemaVersion, |
| 971 | ...(event.actorUserId ? { actorUserId: event.actorUserId } : {}), |
| 972 | ...(event.containerType ? { containerType: event.containerType } : {}), |
| 973 | ...(event.containerId ? { containerId: event.containerId } : {}), |
| 974 | ...(event.occurredAt |
| 975 | ? { occurredAt: event.occurredAt.toISOString() } |
| 976 | : {}), |
| 977 | })), |
| 978 | }; |
| 979 |
| 980 | return this.unwrapGeneratedResponse( |
| 981 | this.appPlatformClient.POST( |
| 982 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/ambient/events", |
| 983 | { |
| 984 | params: { path: { workspaceSlug, appKey } }, |
| 985 | body, |
| 986 | }, |
| 987 | ), |
| 988 | ); |
| 989 | } |
| 990 |
| 991 | async createAppNotification( |
| 992 | workspaceSlug: string, |
| 993 | appKey: string, |
| 994 | input: CreateNotificationInputInput, |
| 995 | ): Promise<OkResponse> { |
| 996 | const parsed = createNotificationInputSchema.parse(input); |
| 997 | const body = { |
| 998 | channelKey: parsed.channelKey, |
| 999 | typeKey: parsed.typeKey, |
| 1000 | recipients: parsed.recipients.map((recipient) => ({ |
| 1001 | userId: recipient.userId, |
| 1002 | })), |
| 1003 | title: parsed.title, |
| 1004 | priority: parsed.priority, |
| 1005 | metadata: parsed.metadata, |
| 1006 | ...(parsed.body ? { body: parsed.body } : {}), |
| 1007 | ...(parsed.targetUrl ? { targetUrl: parsed.targetUrl } : {}), |
| 1008 | ...(parsed.icon ? { icon: parsed.icon } : {}), |
| 1009 | ...(parsed.dedupeKey ? { dedupeKey: parsed.dedupeKey } : {}), |
| 1010 | }; |
| 1011 |
| 1012 | return this.unwrapGeneratedResponse( |
| 1013 | this.appPlatformClient.POST( |
| 1014 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/notifications", |
| 1015 | { |
| 1016 | params: { path: { workspaceSlug, appKey } }, |
| 1017 | body, |
| 1018 | }, |
| 1019 | ), |
| 1020 | ); |
| 1021 | } |
| 1022 |
| 1023 | async upsertAppIndexDocuments( |
| 1024 | workspaceSlug: string, |
| 1025 | appKey: string, |
| 1026 | input: BatchUpsertAppIndexDocumentsInput, |
| 1027 | ): Promise<UpsertAppIndexDocumentsResponse> { |
| 1028 | const parsed = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 1029 | const body = { |
| 1030 | documents: parsed.documents.map((document) => ({ |
| 1031 | documentKey: document.documentKey, |
| 1032 | documentType: document.documentType, |
| 1033 | title: document.title, |
| 1034 | body: document.body, |
| 1035 | metadata: document.metadata, |
| 1036 | visibilityMode: document.visibilityMode, |
| 1037 | visibility: document.visibility.map((grant) => ({ |
| 1038 | subjectType: grant.subjectType, |
| 1039 | subjectId: grant.subjectId, |
| 1040 | })), |
| 1041 | ...(document.fileId ? { fileId: document.fileId } : {}), |
| 1042 | ...(document.url ? { url: document.url } : {}), |
| 1043 | })), |
| 1044 | }; |
| 1045 |
| 1046 | return this.unwrapGeneratedResponse( |
| 1047 | this.appPlatformClient.POST( |
| 1048 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 1049 | { |
| 1050 | params: { path: { workspaceSlug, appKey } }, |
| 1051 | body, |
| 1052 | }, |
| 1053 | ), |
| 1054 | ); |
| 1055 | } |
| 1056 |
| 1057 | async deleteAppIndexDocuments( |
| 1058 | workspaceSlug: string, |
| 1059 | appKey: string, |
| 1060 | input: BatchDeleteAppIndexDocumentsInput, |
| 1061 | ): Promise<DeleteAppIndexDocumentsResponse> { |
| 1062 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 1063 |
| 1064 | return this.unwrapGeneratedResponse( |
| 1065 | this.appPlatformClient.DELETE( |
| 1066 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 1067 | { |
| 1068 | params: { path: { workspaceSlug, appKey } }, |
| 1069 | body, |
| 1070 | }, |
| 1071 | ), |
| 1072 | ); |
| 1073 | } |
| 1074 |
| 1075 | async listAppFiles( |
| 1076 | workspaceSlug: string, |
| 1077 | appKey: string, |
| 1078 | query?: { |
| 1079 | driveId?: string; |
| 1080 | typeKey?: string; |
| 1081 | limit?: number; |
| 1082 | }, |
| 1083 | ): Promise<AppFilesListResponse> { |
| 1084 | return this.unwrapGeneratedResponse( |
| 1085 | this.appPlatformClient.GET( |
| 1086 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 1087 | { |
| 1088 | params: { |
| 1089 | path: { workspaceSlug, appKey }, |
| 1090 | ...(query |
| 1091 | ? { |
| 1092 | query: { |
| 1093 | ...(query.driveId ? { driveId: query.driveId } : {}), |
| 1094 | ...(query.typeKey ? { typeKey: query.typeKey } : {}), |
| 1095 | ...(query.limit !== undefined ? { limit: query.limit } : {}), |
| 1096 | }, |
| 1097 | } |
| 1098 | : {}), |
| 1099 | }, |
| 1100 | }, |
| 1101 | ), |
| 1102 | ); |
| 1103 | } |
| 1104 |
| 1105 | async createAppFile( |
| 1106 | workspaceSlug: string, |
| 1107 | appKey: string, |
| 1108 | input: CreateAppFileInput, |
| 1109 | ): Promise<CreateAppFileResponse> { |
| 1110 | return this.unwrapGeneratedResponse( |
| 1111 | this.appPlatformClient.POST( |
| 1112 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 1113 | { |
| 1114 | params: { path: { workspaceSlug, appKey } }, |
| 1115 | body: { |
| 1116 | name: input.name, |
| 1117 | ...(input.typeKey ? { typeKey: input.typeKey } : {}), |
| 1118 | ...(input.driveId ? { driveId: input.driveId } : {}), |
| 1119 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 1120 | ...(input.document ? { document: input.document } : {}), |
| 1121 | }, |
| 1122 | }, |
| 1123 | ), |
| 1124 | ); |
| 1125 | } |
| 1126 |
| 1127 | async executeAppAction( |
| 1128 | workspaceSlug: string, |
| 1129 | appKey: string, |
| 1130 | input: ExecuteAppActionInput, |
| 1131 | ): Promise<ExecuteAppActionResponse> { |
| 1132 | return this.unwrapGeneratedResponse( |
| 1133 | this.appPlatformClient.POST( |
| 1134 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/actions", |
| 1135 | { |
| 1136 | params: { path: { workspaceSlug, appKey } }, |
| 1137 | body: { |
| 1138 | action: { |
| 1139 | type: input.action.type, |
| 1140 | ...(input.action.payload ? { payload: input.action.payload } : {}), |
| 1141 | }, |
| 1142 | ...(input.fileId !== undefined ? { fileId: input.fileId } : {}), |
| 1143 | }, |
| 1144 | }, |
| 1145 | ), |
| 1146 | ); |
| 1147 | } |
| 1148 |
| 1149 | async getAppFileDocument( |
| 1150 | workspaceSlug: string, |
| 1151 | appKey: string, |
| 1152 | fileId: string, |
| 1153 | ): Promise<AppFileDocumentResponse> { |
| 1154 | return this.unwrapGeneratedResponse( |
| 1155 | this.appPlatformClient.GET( |
| 1156 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 1157 | { |
| 1158 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 1159 | }, |
| 1160 | ), |
| 1161 | ); |
| 1162 | } |
| 1163 |
| 1164 | async updateAppFileDocument( |
| 1165 | workspaceSlug: string, |
| 1166 | appKey: string, |
| 1167 | fileId: string, |
| 1168 | document: Record<string, unknown>, |
| 1169 | ): Promise<AppFileDocumentUpdateResponse> { |
| 1170 | return this.unwrapGeneratedResponse( |
| 1171 | this.appPlatformClient.PATCH( |
| 1172 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 1173 | { |
| 1174 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 1175 | body: { document }, |
| 1176 | }, |
| 1177 | ), |
| 1178 | ); |
| 1179 | } |
| 1180 |
| 1181 | async listTabModels(): Promise<TabModelsResponse> { |
| 1182 | return this.unwrapGeneratedResponse( |
| 1183 | this.appPlatformClient.GET("/api/v1/tab/models"), |
| 1184 | ); |
| 1185 | } |
| 1186 |
| 1187 | async createTabChatCompletion(input: { |
| 1188 | model: string; |
| 1189 | stream?: boolean; |
| 1190 | tab_context: Record<string, unknown>; |
| 1191 | messages?: Array<{ role?: string; content?: string }>; |
| 1192 | }): Promise<TabChatCompletionResponse> { |
| 1193 | return this.unwrapGeneratedResponse( |
| 1194 | this.appPlatformClient.POST("/api/v1/tab/chat/completions", { |
| 1195 | body: input, |
| 1196 | }), |
| 1197 | ); |
| 1198 | } |
| 1199 |
| 1200 | async exchangeOAuthCode(input: { |
| 1201 | clientId: string; |
| 1202 | clientSecret: string; |
| 1203 | code: string; |
| 1204 | redirectUri: string; |
| 1205 | codeVerifier?: string; |
| 1206 | }): Promise<OAuthTokenResponse> { |
| 1207 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 1208 | method: "POST", |
| 1209 | headers: { |
| 1210 | "Content-Type": "application/json", |
| 1211 | }, |
| 1212 | body: JSON.stringify({ |
| 1213 | grant_type: "authorization_code", |
| 1214 | client_id: input.clientId, |
| 1215 | client_secret: input.clientSecret, |
| 1216 | code: input.code, |
| 1217 | redirect_uri: input.redirectUri, |
| 1218 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 1219 | }), |
| 1220 | }); |
| 1221 |
| 1222 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 1223 | } |
| 1224 |
| 1225 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 1226 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 1227 | method: "GET", |
| 1228 | headers: { |
| 1229 | Authorization: `Bearer ${accessToken}`, |
| 1230 | }, |
| 1231 | }); |
| 1232 |
| 1233 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 1234 | } |
| 1235 |
| 1236 | // --------------------------------------------------------------------------- |
| 1237 | // Core Platform API — internal fetch helper |
| 1238 | // --------------------------------------------------------------------------- |
| 1239 |
| 1240 | private async coreRequest<T>( |
| 1241 | method: string, |
| 1242 | path: string, |
| 1243 | body?: unknown, |
| 1244 | ): Promise<T> { |
| 1245 | const headers: Record<string, string> = {}; |
| 1246 | if (this.apiKey) { |
| 1247 | headers["Authorization"] = `Bearer ${this.apiKey}`; |
| 1248 | } |
| 1249 | if (body !== undefined) { |
| 1250 | headers["Content-Type"] = "application/json"; |
| 1251 | } |
| 1252 | if (this.defaultHeaders) { |
| 1253 | new Headers(this.defaultHeaders).forEach((v, k) => { |
| 1254 | headers[k] = v; |
| 1255 | }); |
| 1256 | } |
| 1257 |
| 1258 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 1259 | method, |
| 1260 | headers, |
| 1261 | ...(body !== undefined ? { body: JSON.stringify(body) } : {}), |
| 1262 | }); |
| 1263 |
| 1264 | if (!response.ok) { |
| 1265 | const payload = (await response.json().catch(() => null)) as ApiErrorPayload | null; |
| 1266 | throw new ColineApiError({ |
| 1267 | message: |
| 1268 | payload?.error?.message ?? `Request failed (${response.status}).`, |
| 1269 | type: "api", |
| 1270 | status: response.status, |
| 1271 | code: payload?.error?.code ?? null, |
| 1272 | }); |
| 1273 | } |
| 1274 |
| 1275 | const json = (await response.json().catch(() => null)) as { |
| 1276 | data: T; |
| 1277 | } | null; |
| 1278 |
| 1279 | if (!json || !("data" in json)) { |
| 1280 | throw new ColineApiError({ |
| 1281 | message: "Invalid API response.", |
| 1282 | type: "api", |
| 1283 | status: response.status, |
| 1284 | }); |
| 1285 | } |
| 1286 |
| 1287 | return json.data; |
| 1288 | } |
| 1289 |
| 1290 | // --------------------------------------------------------------------------- |
| 1291 | // Core Platform API — Workspace Members |
| 1292 | // --------------------------------------------------------------------------- |
| 1293 |
| 1294 | async listWorkspaceMembers( |
| 1295 | workspaceId: string, |
| 1296 | ): Promise<WorkspaceMembersResponse> { |
| 1297 | return this.coreRequest<WorkspaceMembersResponse>( |
| 1298 | "GET", |
| 1299 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/members`, |
| 1300 | ); |
| 1301 | } |
| 1302 |
| 1303 | // --------------------------------------------------------------------------- |
| 1304 | // Core Platform API — Notes |
| 1305 | // --------------------------------------------------------------------------- |
| 1306 |
| 1307 | async listWorkspaceNotes( |
| 1308 | workspaceId: string, |
| 1309 | query?: ListWorkspaceNotesQuery, |
| 1310 | ): Promise<WorkspaceNotesListResponse> { |
| 1311 | const params = new URLSearchParams(); |
| 1312 | if (query?.q) params.set("q", query.q); |
| 1313 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 1314 | if (query?.cursor) params.set("cursor", query.cursor); |
| 1315 | const qs = params.toString(); |
| 1316 | return this.coreRequest<WorkspaceNotesListResponse>( |
| 1317 | "GET", |
| 1318 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes${qs ? `?${qs}` : ""}`, |
| 1319 | ); |
| 1320 | } |
| 1321 |
| 1322 | async createWorkspaceNote( |
| 1323 | workspaceId: string, |
| 1324 | input: CreateWorkspaceNoteInput, |
| 1325 | ): Promise<WorkspaceNoteResponse> { |
| 1326 | return this.coreRequest<WorkspaceNoteResponse>( |
| 1327 | "POST", |
| 1328 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes`, |
| 1329 | { |
| 1330 | ...(input.title !== undefined ? { title: input.title } : {}), |
| 1331 | ...(input.body !== undefined ? { body: input.body } : {}), |
| 1332 | ...(input.blocks !== undefined ? { blocks: input.blocks } : {}), |
| 1333 | ...(input.driveId !== undefined ? { driveId: input.driveId } : {}), |
| 1334 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 1335 | }, |
| 1336 | ); |
| 1337 | } |
| 1338 |
| 1339 | async getWorkspaceNote( |
| 1340 | workspaceId: string, |
| 1341 | noteId: string, |
| 1342 | ): Promise<WorkspaceNoteDetailResponse> { |
| 1343 | return this.coreRequest<WorkspaceNoteDetailResponse>( |
| 1344 | "GET", |
| 1345 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1346 | ); |
| 1347 | } |
| 1348 |
| 1349 | async updateWorkspaceNote( |
| 1350 | workspaceId: string, |
| 1351 | noteId: string, |
| 1352 | input: UpdateWorkspaceNoteInput, |
| 1353 | ): Promise<WorkspaceNoteResponse> { |
| 1354 | return this.coreRequest<WorkspaceNoteResponse>( |
| 1355 | "PUT", |
| 1356 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1357 | input, |
| 1358 | ); |
| 1359 | } |
| 1360 |
| 1361 | async deleteWorkspaceNote( |
| 1362 | workspaceId: string, |
| 1363 | noteId: string, |
| 1364 | ): Promise<{ ok: true }> { |
| 1365 | return this.coreRequest<{ ok: true }>( |
| 1366 | "DELETE", |
| 1367 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1368 | ); |
| 1369 | } |
| 1370 |
| 1371 | // --------------------------------------------------------------------------- |
| 1372 | // Core Platform API — Calendar Events |
| 1373 | // --------------------------------------------------------------------------- |
| 1374 |
| 1375 | async listCalendarEvents( |
| 1376 | workspaceId: string, |
| 1377 | range: { start: string; end: string }, |
| 1378 | ): Promise<CalendarEventsListResponse> { |
| 1379 | const params = new URLSearchParams({ |
| 1380 | start: range.start, |
| 1381 | end: range.end, |
| 1382 | }); |
| 1383 | return this.coreRequest<CalendarEventsListResponse>( |
| 1384 | "GET", |
| 1385 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events?${params}`, |
| 1386 | ); |
| 1387 | } |
| 1388 |
| 1389 | async createCalendarEvent( |
| 1390 | workspaceId: string, |
| 1391 | input: CreateCalendarEventInput, |
| 1392 | ): Promise<CalendarEventDetailResponse> { |
| 1393 | return this.coreRequest<CalendarEventDetailResponse>( |
| 1394 | "POST", |
| 1395 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events`, |
| 1396 | input, |
| 1397 | ); |
| 1398 | } |
| 1399 |
| 1400 | async getCalendarEvent( |
| 1401 | workspaceId: string, |
| 1402 | eventId: string, |
| 1403 | ): Promise<CalendarEventDetailResponse> { |
| 1404 | return this.coreRequest<CalendarEventDetailResponse>( |
| 1405 | "GET", |
| 1406 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 1407 | ); |
| 1408 | } |
| 1409 |
| 1410 | async updateCalendarEvent( |
| 1411 | workspaceId: string, |
| 1412 | eventId: string, |
| 1413 | input: UpdateCalendarEventInput, |
| 1414 | ): Promise<CalendarEventDetailResponse> { |
| 1415 | return this.coreRequest<CalendarEventDetailResponse>( |
| 1416 | "PATCH", |
| 1417 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 1418 | input, |
| 1419 | ); |
| 1420 | } |
| 1421 |
| 1422 | async deleteCalendarEvent( |
| 1423 | workspaceId: string, |
| 1424 | eventId: string, |
| 1425 | ): Promise<{ ok: true }> { |
| 1426 | return this.coreRequest<{ ok: true }>( |
| 1427 | "DELETE", |
| 1428 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 1429 | ); |
| 1430 | } |
| 1431 |
| 1432 | // --------------------------------------------------------------------------- |
| 1433 | // Core Platform API — Search |
| 1434 | // --------------------------------------------------------------------------- |
| 1435 |
| 1436 | async searchWorkspace( |
| 1437 | workspaceId: string, |
| 1438 | input: WorkspaceSearchInput, |
| 1439 | ): Promise<WorkspaceSearchResponse> { |
| 1440 | return this.coreRequest<WorkspaceSearchResponse>( |
| 1441 | "POST", |
| 1442 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/search`, |
| 1443 | input, |
| 1444 | ); |
| 1445 | } |
| 1446 |
| 1447 | // --------------------------------------------------------------------------- |
| 1448 | // Core Platform API — Taskboards |
| 1449 | // --------------------------------------------------------------------------- |
| 1450 |
| 1451 | async listWorkspaceTaskboards( |
| 1452 | workspaceId: string, |
| 1453 | ): Promise<WorkspaceTaskboardsResponse> { |
| 1454 | return this.coreRequest<WorkspaceTaskboardsResponse>( |
| 1455 | "GET", |
| 1456 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards`, |
| 1457 | ); |
| 1458 | } |
| 1459 |
| 1460 | // --------------------------------------------------------------------------- |
| 1461 | // Core Platform API — Tasks |
| 1462 | // --------------------------------------------------------------------------- |
| 1463 |
| 1464 | async listTaskboardTasks( |
| 1465 | workspaceId: string, |
| 1466 | taskboardId: string, |
| 1467 | query?: ListTaskboardTasksQuery, |
| 1468 | ): Promise<TaskboardTasksResponse> { |
| 1469 | const params = new URLSearchParams(); |
| 1470 | if (query?.q) params.set("q", query.q); |
| 1471 | if (query?.statusId) params.set("statusId", query.statusId); |
| 1472 | if (query?.assigneeUserId) params.set("assigneeUserId", query.assigneeUserId); |
| 1473 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 1474 | const qs = params.toString(); |
| 1475 | return this.coreRequest<TaskboardTasksResponse>( |
| 1476 | "GET", |
| 1477 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks${qs ? `?${qs}` : ""}`, |
| 1478 | ); |
| 1479 | } |
| 1480 |
| 1481 | async createTaskboardTask( |
| 1482 | workspaceId: string, |
| 1483 | taskboardId: string, |
| 1484 | input: CreateTaskInput, |
| 1485 | ): Promise<CreateTaskResponse> { |
| 1486 | return this.coreRequest<CreateTaskResponse>( |
| 1487 | "POST", |
| 1488 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks`, |
| 1489 | input, |
| 1490 | ); |
| 1491 | } |
| 1492 |
| 1493 | async updateTaskboardTask( |
| 1494 | workspaceId: string, |
| 1495 | taskboardId: string, |
| 1496 | taskId: string, |
| 1497 | input: UpdateTaskInput, |
| 1498 | ): Promise<{ ok: true }> { |
| 1499 | return this.coreRequest<{ ok: true }>( |
| 1500 | "PATCH", |
| 1501 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks/${encodeURIComponent(taskId)}`, |
| 1502 | input, |
| 1503 | ); |
| 1504 | } |
| 1505 |
| 1506 | async deleteTaskboardTask( |
| 1507 | workspaceId: string, |
| 1508 | taskboardId: string, |
| 1509 | taskId: string, |
| 1510 | ): Promise<{ ok: true }> { |
| 1511 | return this.coreRequest<{ ok: true }>( |
| 1512 | "DELETE", |
| 1513 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks/${encodeURIComponent(taskId)}`, |
| 1514 | ); |
| 1515 | } |
| 1516 | } |
| 1517 |