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 | /** Maximum number of automatic retries on 429 / 5xx responses (default: 0). */ |
| 73 | maxRetries?: number; |
| 74 | /** Log requests and responses to console for debugging. */ |
| 75 | debug?: boolean; |
| 76 | } |
| 77 |
| 78 | type Schemas = AppPlatformComponents["schemas"]; |
| 79 | type UnwrapData<TKey extends keyof Schemas> = |
| 80 | Schemas[TKey] extends { data: infer T } ? T : never; |
| 81 |
| 82 | export type RegisteredAppDetail = Schemas["RegisteredAppDetail"]; |
| 83 | export type RegisteredAppRecord = Omit< |
| 84 | RegisteredAppDetail, |
| 85 | "latestPublishedVersionId" |
| 86 | >; |
| 87 | export type RegisteredAppVersionSummary = Schemas["RegisteredAppVersionSummary"]; |
| 88 | export type WorkspaceInstalledApp = Schemas["InstalledAppSummary"]; |
| 89 | export type WorkspaceAppCatalogItem = Schemas["AppCatalogItem"]; |
| 90 | export type WorkspaceAppsResponse = UnwrapData<"WorkspaceAppsResponse">; |
| 91 |
| 92 | export interface InstallWorkspaceAppInput { |
| 93 | appId: string; |
| 94 | pinnedVersionId?: string; |
| 95 | grantedPermissions?: AppPermission[]; |
| 96 | } |
| 97 |
| 98 | export type InstallWorkspaceAppResponse = UnwrapData<"InstallWorkspaceAppResponse">; |
| 99 |
| 100 | export interface SubmitAppVersionForReviewInput { |
| 101 | storeListed?: boolean; |
| 102 | } |
| 103 |
| 104 | export type SubmitAppVersionForReviewResponse = UnwrapData< |
| 105 | "SubmitAppVersionForReviewResponse" |
| 106 | >; |
| 107 | export type WorkspaceAppPermissionsResponse = UnwrapData< |
| 108 | "WorkspaceAppPermissionsResponse" |
| 109 | >; |
| 110 | export type WorkspaceInstalledAppResponse = UnwrapData< |
| 111 | "WorkspaceInstalledAppResponse" |
| 112 | >; |
| 113 | export type WorkspaceAppSecretsResponse = UnwrapData< |
| 114 | "WorkspaceAppSecretsResponse" |
| 115 | >; |
| 116 | export type WorkspaceAppSecretSummary = WorkspaceAppSecretsResponse["secrets"][number]; |
| 117 | export type CreateWorkspaceAppSecretResponse = UnwrapData< |
| 118 | "CreateWorkspaceAppSecretResponse" |
| 119 | >; |
| 120 | export type WorkspaceAppOauthConnectionsResponse = UnwrapData< |
| 121 | "WorkspaceAppOauthConnectionsResponse" |
| 122 | >; |
| 123 | export type WorkspaceAppOauthConnectionSummary = |
| 124 | WorkspaceAppOauthConnectionsResponse["connections"][number]; |
| 125 | export type CreateWorkspaceAppOauthConnectionResponse = UnwrapData< |
| 126 | "CreateWorkspaceAppOauthConnectionResponse" |
| 127 | >; |
| 128 | export type WorkspaceAppDeliveriesResponse = UnwrapData< |
| 129 | "WorkspaceAppDeliveriesResponse" |
| 130 | >; |
| 131 | export type WorkspaceAppDeliverySummary = |
| 132 | WorkspaceAppDeliveriesResponse["deliveries"][number]; |
| 133 | export type SendWorkspaceAppTestDeliveryResponse = UnwrapData< |
| 134 | "SendWorkspaceAppTestDeliveryResponse" |
| 135 | >; |
| 136 | export type AppFilesListResponse = UnwrapData<"AppFilesListResponse">; |
| 137 | export type AppFileDocumentResponse = UnwrapData<"AppFileDocumentResponse">; |
| 138 | export type AppFileDocumentUpdateResponse = UnwrapData< |
| 139 | "AppFileDocumentUpdateResponse" |
| 140 | >; |
| 141 | export type CreateAppFileResponse = UnwrapData<"CreateAppFileResponse">; |
| 142 | export type ExecuteAppActionResponse = UnwrapData<"ExecuteAppActionResponse">; |
| 143 | export type OkResponse = UnwrapData<"OkResponse">; |
| 144 | export type TabModelRecord = Schemas["TabModelRecord"]; |
| 145 | export type TabModelsResponse = UnwrapData<"TabModelsResponse">; |
| 146 | export type TabChatCompletionResponse = UnwrapData< |
| 147 | "TabChatCompletionResponseEnvelope" |
| 148 | >; |
| 149 | export type UpsertAppIndexDocumentsResponse = UnwrapData< |
| 150 | "UpsertAppIndexDocumentsResponse" |
| 151 | >; |
| 152 | export type DeleteAppIndexDocumentsResponse = UnwrapData< |
| 153 | "DeleteAppIndexDocumentsResponse" |
| 154 | >; |
| 155 | export type EmitAppAmbientEventsResponse = UnwrapData< |
| 156 | "EmitAppAmbientEventsResponse" |
| 157 | >; |
| 158 |
| 159 | export interface OAuthTokenResponse { |
| 160 | access_token: string; |
| 161 | token_type: "Bearer"; |
| 162 | expires_in: number; |
| 163 | refresh_token: string; |
| 164 | scope: string; |
| 165 | } |
| 166 |
| 167 | export interface OAuthUserInfoResponse { |
| 168 | sub: string; |
| 169 | email: string; |
| 170 | email_verified: boolean; |
| 171 | name: string; |
| 172 | given_name: string | null; |
| 173 | family_name: string | null; |
| 174 | picture: string | null; |
| 175 | preferred_username: string | null; |
| 176 | scope: string; |
| 177 | } |
| 178 |
| 179 | export interface UpdateWorkspaceAppPermissionsInput { |
| 180 | grantedPermissions: AppPermission[]; |
| 181 | } |
| 182 |
| 183 | export interface CreateAppFileInput { |
| 184 | name: string; |
| 185 | typeKey?: string; |
| 186 | driveId?: string; |
| 187 | parentId?: string | null; |
| 188 | document?: Record<string, unknown>; |
| 189 | } |
| 190 |
| 191 | export interface ExecuteAppActionInput { |
| 192 | action: { |
| 193 | type: string; |
| 194 | payload?: Record<string, unknown>; |
| 195 | }; |
| 196 | fileId?: string | null; |
| 197 | } |
| 198 |
| 199 | // --------------------------------------------------------------------------- |
| 200 | // Core Platform API — Types |
| 201 | // --------------------------------------------------------------------------- |
| 202 |
| 203 | export interface WorkspaceMember { |
| 204 | id: string; |
| 205 | workspaceMemberId: string; |
| 206 | email: string; |
| 207 | firstName: string | null; |
| 208 | lastName: string | null; |
| 209 | avatarUrl: string | null; |
| 210 | joinedAt: string; |
| 211 | roles: string[]; |
| 212 | } |
| 213 |
| 214 | export interface WorkspaceMembersResponse { |
| 215 | members: WorkspaceMember[]; |
| 216 | } |
| 217 |
| 218 | export interface WorkspaceNoteSummary { |
| 219 | id: string; |
| 220 | driveId: string; |
| 221 | parentId: string | null; |
| 222 | title: string; |
| 223 | body: string; |
| 224 | color: string | null; |
| 225 | createdByUserId: string | null; |
| 226 | createdAt: string; |
| 227 | updatedAt: string; |
| 228 | } |
| 229 |
| 230 | export interface WorkspaceNoteDetail extends WorkspaceNoteSummary { |
| 231 | blocks: unknown[]; |
| 232 | } |
| 233 |
| 234 | export interface WorkspaceNotesListResponse { |
| 235 | notes: WorkspaceNoteSummary[]; |
| 236 | page: { limit: number; hasMore: boolean; nextCursor: string | null }; |
| 237 | } |
| 238 |
| 239 | export interface WorkspaceNoteResponse { |
| 240 | note: WorkspaceNoteSummary; |
| 241 | } |
| 242 |
| 243 | export interface WorkspaceNoteDetailResponse { |
| 244 | note: WorkspaceNoteDetail; |
| 245 | } |
| 246 |
| 247 | export interface ListWorkspaceNotesQuery { |
| 248 | q?: string; |
| 249 | limit?: number; |
| 250 | cursor?: string; |
| 251 | } |
| 252 |
| 253 | export interface CreateWorkspaceNoteInput { |
| 254 | title?: string; |
| 255 | body?: string; |
| 256 | blocks?: unknown[]; |
| 257 | driveId?: string; |
| 258 | parentId?: string | null; |
| 259 | } |
| 260 |
| 261 | export interface UpdateWorkspaceNoteInput { |
| 262 | title?: string; |
| 263 | body?: string; |
| 264 | blocks?: unknown[]; |
| 265 | } |
| 266 |
| 267 | export interface CalendarEventLinkInput { |
| 268 | linkType: "file" | "channel" | "url"; |
| 269 | fileId?: string; |
| 270 | channelId?: string; |
| 271 | url?: string; |
| 272 | label?: string | null; |
| 273 | } |
| 274 |
| 275 | // --------------------------------------------------------------------------- |
| 276 | // Core Platform API — Docs Types |
| 277 | // --------------------------------------------------------------------------- |
| 278 |
| 279 | export interface DocLayout { |
| 280 | mode: "pages"; |
| 281 | pageSize: "letter"; |
| 282 | orientation: "portrait" | "landscape"; |
| 283 | margins: "normal" | "narrow" | "wide"; |
| 284 | backgroundColor: string | null; |
| 285 | showPageNumbers: boolean; |
| 286 | } |
| 287 |
| 288 | export interface WorkspaceDocSummary { |
| 289 | id: string; |
| 290 | driveId: string; |
| 291 | parentId: string | null; |
| 292 | title: string; |
| 293 | excerpt: string; |
| 294 | color: string | null; |
| 295 | layout: DocLayout; |
| 296 | createdByUserId: string | null; |
| 297 | createdAt: string; |
| 298 | updatedAt: string; |
| 299 | } |
| 300 |
| 301 | export interface WorkspaceDocDetail extends Omit<WorkspaceDocSummary, "excerpt"> { |
| 302 | content: Record<string, unknown>; |
| 303 | plainText: string; |
| 304 | } |
| 305 |
| 306 | export interface WorkspaceDocsListResponse { |
| 307 | docs: WorkspaceDocSummary[]; |
| 308 | page: { limit: number; hasMore: boolean; nextCursor: string | null }; |
| 309 | } |
| 310 |
| 311 | export interface WorkspaceDocResponse { |
| 312 | doc: WorkspaceDocSummary; |
| 313 | } |
| 314 |
| 315 | export interface WorkspaceDocDetailResponse { |
| 316 | doc: WorkspaceDocDetail; |
| 317 | } |
| 318 |
| 319 | export interface ListWorkspaceDocsQuery { |
| 320 | q?: string; |
| 321 | limit?: number; |
| 322 | cursor?: string; |
| 323 | } |
| 324 |
| 325 | export interface CreateWorkspaceDocInput { |
| 326 | title?: string; |
| 327 | content?: Record<string, unknown>; |
| 328 | layout?: { |
| 329 | orientation?: "portrait" | "landscape"; |
| 330 | margins?: "normal" | "narrow" | "wide"; |
| 331 | backgroundColor?: string | null; |
| 332 | showPageNumbers?: boolean; |
| 333 | }; |
| 334 | driveId?: string; |
| 335 | parentId?: string | null; |
| 336 | } |
| 337 |
| 338 | export interface UpdateWorkspaceDocInput { |
| 339 | title?: string; |
| 340 | content?: Record<string, unknown>; |
| 341 | layout?: { |
| 342 | orientation?: "portrait" | "landscape"; |
| 343 | margins?: "normal" | "narrow" | "wide"; |
| 344 | backgroundColor?: string | null; |
| 345 | showPageNumbers?: boolean; |
| 346 | }; |
| 347 | } |
| 348 |
| 349 | // --------------------------------------------------------------------------- |
| 350 | // Core Platform API — Drives & Files Types |
| 351 | // --------------------------------------------------------------------------- |
| 352 |
| 353 | export interface DriveMountSummary { |
| 354 | type: "native" | "integration"; |
| 355 | providerKey: string | null; |
| 356 | syncStatus: "idle" | "syncing" | "errored" | "disabled"; |
| 357 | } |
| 358 |
| 359 | export interface WorkspaceDriveSummary { |
| 360 | id: string; |
| 361 | name: string; |
| 362 | slug: string; |
| 363 | kind: "personal" | "shared"; |
| 364 | accessMode: "workspace_default" | "restricted"; |
| 365 | isDefault: boolean; |
| 366 | ownerUserId: string | null; |
| 367 | mount: DriveMountSummary; |
| 368 | createdAt: string; |
| 369 | updatedAt: string; |
| 370 | } |
| 371 |
| 372 | export interface WorkspaceDrivesListResponse { |
| 373 | drives: WorkspaceDriveSummary[]; |
| 374 | } |
| 375 |
| 376 | export interface DriveFileSummary { |
| 377 | id: string; |
| 378 | driveId: string; |
| 379 | parentId: string | null; |
| 380 | name: string; |
| 381 | fileType: string; |
| 382 | mimeType: string | null; |
| 383 | sizeBytes: number | null; |
| 384 | color: string | null; |
| 385 | createdByUserId: string | null; |
| 386 | createdAt: string; |
| 387 | updatedAt: string; |
| 388 | } |
| 389 |
| 390 | export interface DriveFilesListResponse { |
| 391 | files: DriveFileSummary[]; |
| 392 | page: { limit: number; hasMore: boolean; nextCursor: string | null }; |
| 393 | } |
| 394 |
| 395 | export interface ListDriveFilesQuery { |
| 396 | parentId?: string; |
| 397 | fileType?: string; |
| 398 | trashed?: boolean; |
| 399 | limit?: number; |
| 400 | cursor?: string; |
| 401 | } |
| 402 |
| 403 | export interface FileDetail { |
| 404 | id: string; |
| 405 | driveId: string; |
| 406 | parentId: string | null; |
| 407 | name: string; |
| 408 | fileType: string; |
| 409 | mimeType: string | null; |
| 410 | sizeBytes: number | null; |
| 411 | color: string | null; |
| 412 | accessMode: string; |
| 413 | isTrashed: boolean; |
| 414 | createdByUserId: string | null; |
| 415 | createdAt: string; |
| 416 | updatedAt: string; |
| 417 | } |
| 418 |
| 419 | export interface FileDetailResponse { |
| 420 | file: FileDetail; |
| 421 | } |
| 422 |
| 423 | export interface UpdateFileInput { |
| 424 | name?: string; |
| 425 | parentId?: string | null; |
| 426 | color?: "red" | "orange" | "yellow" | "green" | "blue" | "purple" | "pink" | "gray" | null; |
| 427 | } |
| 428 |
| 429 | // --------------------------------------------------------------------------- |
| 430 | // Channels |
| 431 | // --------------------------------------------------------------------------- |
| 432 |
| 433 | export interface ChannelSummary { |
| 434 | id: string; |
| 435 | name: string; |
| 436 | slug: string; |
| 437 | description: string | null; |
| 438 | topic: string | null; |
| 439 | type: "public" | "private"; |
| 440 | isDefault: boolean; |
| 441 | createdAt: string; |
| 442 | updatedAt: string; |
| 443 | } |
| 444 |
| 445 | export interface ChannelListResponse { |
| 446 | channels: ChannelSummary[]; |
| 447 | } |
| 448 |
| 449 | // --------------------------------------------------------------------------- |
| 450 | // Direct Messages |
| 451 | // --------------------------------------------------------------------------- |
| 452 |
| 453 | export interface DmMemberSummary { |
| 454 | userId: string; |
| 455 | firstName: string | null; |
| 456 | lastName: string | null; |
| 457 | email: string; |
| 458 | } |
| 459 |
| 460 | export interface DmSummary { |
| 461 | id: string; |
| 462 | isGroup: boolean; |
| 463 | name: string | null; |
| 464 | members: DmMemberSummary[]; |
| 465 | createdAt: string; |
| 466 | updatedAt: string; |
| 467 | } |
| 468 |
| 469 | export interface DmListResponse { |
| 470 | dms: DmSummary[]; |
| 471 | } |
| 472 |
| 473 | // --------------------------------------------------------------------------- |
| 474 | // Messages |
| 475 | // --------------------------------------------------------------------------- |
| 476 |
| 477 | export interface MessageTextPart { |
| 478 | type: "text"; |
| 479 | text: string; |
| 480 | } |
| 481 |
| 482 | export interface MessageMentionPart { |
| 483 | type: "mention"; |
| 484 | userId: string; |
| 485 | displayName: string; |
| 486 | } |
| 487 |
| 488 | export type MessageContentPart = MessageTextPart | MessageMentionPart; |
| 489 |
| 490 | export interface ContainerMessageSummary { |
| 491 | id: string; |
| 492 | authorUserId: string | null; |
| 493 | messageKind: string; |
| 494 | plaintext: string; |
| 495 | content: MessageContentPart[]; |
| 496 | replyToMessageId: string | null; |
| 497 | editedAt: string | null; |
| 498 | createdAt: string; |
| 499 | updatedAt: string; |
| 500 | } |
| 501 |
| 502 | export interface MessagesListResponse { |
| 503 | messages: ContainerMessageSummary[]; |
| 504 | page: { |
| 505 | limit: number; |
| 506 | hasMore: boolean; |
| 507 | nextCursor: string | null; |
| 508 | }; |
| 509 | } |
| 510 |
| 511 | export interface SendMessageInput { |
| 512 | content: MessageContentPart[]; |
| 513 | replyToMessageId?: string | null; |
| 514 | threadRootMessageId?: string | null; |
| 515 | } |
| 516 |
| 517 | export interface SendMessageResponse { |
| 518 | message: ContainerMessageSummary; |
| 519 | } |
| 520 |
| 521 | export interface MessageDetailResponse { |
| 522 | message: ContainerMessageSummary & { |
| 523 | containerType: string; |
| 524 | containerId: string; |
| 525 | }; |
| 526 | } |
| 527 |
| 528 | export interface EditMessageInput { |
| 529 | content: MessageContentPart[]; |
| 530 | } |
| 531 |
| 532 | export interface ThreadResponse { |
| 533 | rootMessage: MessageDetailResponse["message"]; |
| 534 | replies: ContainerMessageSummary[]; |
| 535 | } |
| 536 |
| 537 | export interface ThreadReplyInput { |
| 538 | content: MessageContentPart[]; |
| 539 | } |
| 540 |
| 541 | export interface ReactionResponse { |
| 542 | messageId: string; |
| 543 | emoji: string; |
| 544 | count: number; |
| 545 | } |
| 546 |
| 547 | export interface PinResponse { |
| 548 | messageId: string; |
| 549 | pinned: boolean; |
| 550 | } |
| 551 |
| 552 | export interface CalendarRecurrenceRule { |
| 553 | frequency: "daily" | "weekly" | "monthly" | "yearly"; |
| 554 | interval: number; |
| 555 | byDay?: Array<"MO" | "TU" | "WE" | "TH" | "FR" | "SA" | "SU">; |
| 556 | count?: number; |
| 557 | until?: string; |
| 558 | } |
| 559 |
| 560 | export interface CreateCalendarEventInput { |
| 561 | title: string; |
| 562 | description?: string | null; |
| 563 | location?: string | null; |
| 564 | startsAt: string; |
| 565 | endsAt: string; |
| 566 | timeZone: string; |
| 567 | isAllDay?: boolean; |
| 568 | visibility?: "private" | "invitees" | "workspace"; |
| 569 | attendeeUserIds?: string[]; |
| 570 | linkInputs?: CalendarEventLinkInput[]; |
| 571 | recurrenceRule?: CalendarRecurrenceRule | null; |
| 572 | } |
| 573 |
| 574 | export interface UpdateCalendarEventInput { |
| 575 | title?: string; |
| 576 | description?: string | null; |
| 577 | location?: string | null; |
| 578 | startsAt?: string; |
| 579 | endsAt?: string; |
| 580 | timeZone?: string; |
| 581 | isAllDay?: boolean; |
| 582 | visibility?: "private" | "invitees" | "workspace"; |
| 583 | status?: "active" | "canceled"; |
| 584 | attendeeUserIds?: string[]; |
| 585 | linkInputs?: CalendarEventLinkInput[]; |
| 586 | recurrenceRule?: CalendarRecurrenceRule | null; |
| 587 | } |
| 588 |
| 589 | /** |
| 590 | * Calendar events list response — shape matches the server's `listCalendarEvents` output. |
| 591 | */ |
| 592 | export type CalendarEventsListResponse = Record<string, unknown>; |
| 593 |
| 594 | /** |
| 595 | * Calendar event detail response — shape matches the server's `getCalendarEventDetail` output. |
| 596 | */ |
| 597 | export type CalendarEventDetailResponse = Record<string, unknown>; |
| 598 |
| 599 | export interface WorkspaceSearchInput { |
| 600 | query: string; |
| 601 | sourceKinds?: Array< |
| 602 | "file" | "task" | "container_message" | "kairo_message" | "app_document" |
| 603 | >; |
| 604 | limit?: number; |
| 605 | cursor?: string; |
| 606 | } |
| 607 |
| 608 | export interface WorkspaceSearchResult { |
| 609 | id: string; |
| 610 | title: string | null; |
| 611 | snippet: string | null; |
| 612 | sourceKind: string; |
| 613 | sourceId: string; |
| 614 | url: string | null; |
| 615 | score: number; |
| 616 | } |
| 617 |
| 618 | export interface WorkspaceSearchResponse { |
| 619 | results: WorkspaceSearchResult[]; |
| 620 | nextCursor: string | null; |
| 621 | } |
| 622 |
| 623 | export interface TaskboardStatusSummary { |
| 624 | id: string; |
| 625 | key: string; |
| 626 | name: string; |
| 627 | color: string; |
| 628 | isDefault: boolean; |
| 629 | isCompleted: boolean; |
| 630 | sortOrder: number; |
| 631 | } |
| 632 |
| 633 | export interface TaskboardSummary { |
| 634 | id: string; |
| 635 | name: string; |
| 636 | driveId: string; |
| 637 | color: string | null; |
| 638 | identifierPrefix: string | null; |
| 639 | description: string | null; |
| 640 | defaultView: string | null; |
| 641 | statuses: TaskboardStatusSummary[]; |
| 642 | createdAt: string; |
| 643 | updatedAt: string; |
| 644 | } |
| 645 |
| 646 | export interface WorkspaceTaskboardsResponse { |
| 647 | taskboards: TaskboardSummary[]; |
| 648 | } |
| 649 |
| 650 | export interface TaskAssignee { |
| 651 | id: string; |
| 652 | firstName: string | null; |
| 653 | lastName: string | null; |
| 654 | avatarUrl: string | null; |
| 655 | email: string; |
| 656 | } |
| 657 |
| 658 | export interface TaskSummary { |
| 659 | id: string; |
| 660 | taskboardId: string; |
| 661 | taskNumber: number; |
| 662 | identifier: string | null; |
| 663 | title: string; |
| 664 | description: string | null; |
| 665 | status: { |
| 666 | id: string; |
| 667 | key: string; |
| 668 | name: string; |
| 669 | color: string; |
| 670 | isCompleted: boolean; |
| 671 | } | null; |
| 672 | priority: string; |
| 673 | labels: string[]; |
| 674 | dueDate: string | null; |
| 675 | assignees: TaskAssignee[]; |
| 676 | isCompleted: boolean; |
| 677 | completedAt: string | null; |
| 678 | createdByUserId: string | null; |
| 679 | createdAt: string; |
| 680 | updatedAt: string; |
| 681 | } |
| 682 |
| 683 | export interface TaskboardTasksResponse { |
| 684 | tasks: TaskSummary[]; |
| 685 | } |
| 686 |
| 687 | export interface ListTaskboardTasksQuery { |
| 688 | q?: string; |
| 689 | statusId?: string; |
| 690 | assigneeUserId?: string; |
| 691 | limit?: number; |
| 692 | } |
| 693 |
| 694 | export interface CreateTaskInput { |
| 695 | title: string; |
| 696 | description?: string; |
| 697 | statusId?: string; |
| 698 | priority?: "none" | "low" | "medium" | "high" | "urgent"; |
| 699 | dueDate?: string | null; |
| 700 | labels?: string[]; |
| 701 | assigneeUserIds?: string[]; |
| 702 | } |
| 703 |
| 704 | export interface CreateTaskResponse { |
| 705 | task: { |
| 706 | id: string; |
| 707 | taskboardId: string; |
| 708 | title: string; |
| 709 | description: string; |
| 710 | statusId: string; |
| 711 | priority: string; |
| 712 | labels: string[]; |
| 713 | dueDate: string | null; |
| 714 | assigneeUserIds: string[]; |
| 715 | createdByUserId: string; |
| 716 | createdAt: string; |
| 717 | updatedAt: string; |
| 718 | }; |
| 719 | } |
| 720 |
| 721 | export interface UpdateTaskInput { |
| 722 | title?: string; |
| 723 | description?: string; |
| 724 | statusId?: string; |
| 725 | priority?: "none" | "low" | "medium" | "high" | "urgent"; |
| 726 | dueDate?: string | null; |
| 727 | labels?: string[]; |
| 728 | assigneeUserIds?: string[]; |
| 729 | } |
| 730 |
| 731 | // --------------------------------------------------------------------------- |
| 732 | // Pagination helper |
| 733 | // --------------------------------------------------------------------------- |
| 734 |
| 735 | /** |
| 736 | * A page from a paginated endpoint. |
| 737 | */ |
| 738 | export interface Page<T> { |
| 739 | items: T[]; |
| 740 | nextCursor: string | null; |
| 741 | hasMore: boolean; |
| 742 | } |
| 743 |
| 744 | /** |
| 745 | * Async generator that auto-fetches subsequent pages from a paginated Coline |
| 746 | * endpoint. Yields one item at a time. |
| 747 | * |
| 748 | * @example |
| 749 | * ```ts |
| 750 | * const ws = coline.workspace("ws_123"); |
| 751 | * for await (const note of ws.paginateNotes({ limit: 50 })) { |
| 752 | * console.log(note.title); |
| 753 | * } |
| 754 | * ``` |
| 755 | */ |
| 756 | export async function* paginate<T>( |
| 757 | fetchPage: (cursor: string | undefined) => Promise<Page<T>>, |
| 758 | ): AsyncGenerator<T, void, undefined> { |
| 759 | let cursor: string | undefined = undefined; |
| 760 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 761 | while (true) { |
| 762 | const page = await fetchPage(cursor); |
| 763 | for (const item of page.items) { |
| 764 | yield item; |
| 765 | } |
| 766 | if (!page.hasMore || page.nextCursor === null) break; |
| 767 | cursor = page.nextCursor; |
| 768 | } |
| 769 | } |
| 770 |
| 771 | // --------------------------------------------------------------------------- |
| 772 | // Retry + backoff helper |
| 773 | // --------------------------------------------------------------------------- |
| 774 |
| 775 | function isRetryableStatus(status: number): boolean { |
| 776 | return status === 429 || status >= 500; |
| 777 | } |
| 778 |
| 779 | async function sleep(ms: number): Promise<void> { |
| 780 | return new Promise((resolve) => setTimeout(resolve, ms)); |
| 781 | } |
| 782 |
| 783 | function joinUrl(baseUrl: string, path: string): string { |
| 784 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 785 | } |
| 786 |
| 787 | function toRegisterAppInput(payload: ColineAppRegistrationPayload) { |
| 788 | const notificationChannels = payload.manifest.notificationChannels.map((channel) => ({ |
| 789 | key: channel.key, |
| 790 | name: channel.name, |
| 791 | defaultDeliveries: [...channel.defaultDeliveries], |
| 792 | defaultPriority: channel.defaultPriority, |
| 793 | ...(channel.description ? { description: channel.description } : {}), |
| 794 | })); |
| 795 |
| 796 | const ui = |
| 797 | payload.manifest.ui.mode || payload.manifest.ui.homePath || payload.manifest.ui.filePath |
| 798 | ? { |
| 799 | ...(payload.manifest.ui.mode |
| 800 | ? { mode: payload.manifest.ui.mode } |
| 801 | : {}), |
| 802 | ...(payload.manifest.ui.homePath |
| 803 | ? { homePath: payload.manifest.ui.homePath } |
| 804 | : {}), |
| 805 | ...(payload.manifest.ui.filePath |
| 806 | ? { filePath: payload.manifest.ui.filePath } |
| 807 | : {}), |
| 808 | } |
| 809 | : undefined; |
| 810 |
| 811 | const manifest = { |
| 812 | key: payload.manifest.key, |
| 813 | name: payload.manifest.name, |
| 814 | permissions: payload.manifest.permissions, |
| 815 | notificationChannels, |
| 816 | hosting: { |
| 817 | mode: payload.manifest.hosting.mode, |
| 818 | baseUrl: payload.manifest.hosting.baseUrl, |
| 819 | }, |
| 820 | ...(ui ? { ui } : {}), |
| 821 | ...(payload.manifest.description |
| 822 | ? { description: payload.manifest.description } |
| 823 | : {}), |
| 824 | ...(payload.manifest.iconUrl ? { iconUrl: payload.manifest.iconUrl } : {}), |
| 825 | ...(payload.manifest.kairo |
| 826 | ? { |
| 827 | kairo: JSON.parse(JSON.stringify(payload.manifest.kairo)) as Record< |
| 828 | string, |
| 829 | unknown |
| 830 | >, |
| 831 | } |
| 832 | : {}), |
| 833 | }; |
| 834 |
| 835 | const fileTypes = payload.fileTypes.map((fileType) => ({ |
| 836 | typeKey: fileType.typeKey, |
| 837 | name: fileType.name, |
| 838 | storage: fileType.storage, |
| 839 | indexable: fileType.indexable, |
| 840 | homeRenderMode: fileType.homeRenderMode, |
| 841 | ...(fileType.description ? { description: fileType.description } : {}), |
| 842 | })); |
| 843 |
| 844 | return { |
| 845 | version: payload.version, |
| 846 | manifest, |
| 847 | fileTypes, |
| 848 | }; |
| 849 | } |
| 850 |
| 851 | export class ColineApiClient { |
| 852 | private readonly baseUrl: string; |
| 853 | private readonly apiKey: string | undefined; |
| 854 | private readonly fetchImpl: typeof fetch; |
| 855 | private readonly defaultHeaders: HeadersInit | undefined; |
| 856 | private readonly appPlatformClient: AppPlatformClient; |
| 857 | private readonly maxRetries: number; |
| 858 | private readonly debug: boolean; |
| 859 |
| 860 | constructor(options: ColineApiClientOptions) { |
| 861 | this.baseUrl = options.baseUrl; |
| 862 | this.apiKey = options.apiKey; |
| 863 | this.fetchImpl = options.fetch ?? fetch; |
| 864 | this.defaultHeaders = options.headers; |
| 865 | this.maxRetries = options.maxRetries ?? 0; |
| 866 | this.debug = options.debug ?? false; |
| 867 | this.appPlatformClient = createAppPlatformClient(options); |
| 868 | } |
| 869 |
| 870 | private async unwrapGeneratedResponse<T>( |
| 871 | promise: Promise<{ |
| 872 | data?: { data: T }; |
| 873 | error?: ApiErrorPayload; |
| 874 | response: Response; |
| 875 | }>, |
| 876 | ): Promise<T> { |
| 877 | const { data, error, response } = await promise; |
| 878 |
| 879 | if (error) { |
| 880 | throw new ColineApiError({ |
| 881 | message: error.error?.message ?? `Request failed (${response.status}).`, |
| 882 | type: "api", |
| 883 | status: response.status, |
| 884 | code: error.error?.code ?? null, |
| 885 | }); |
| 886 | } |
| 887 |
| 888 | if (data === undefined) { |
| 889 | throw new ColineApiError({ |
| 890 | message: "Invalid API response.", |
| 891 | type: "api", |
| 892 | status: response.status, |
| 893 | }); |
| 894 | } |
| 895 |
| 896 | return data.data; |
| 897 | } |
| 898 |
| 899 | withApiKey(apiKey: string): ColineApiClient { |
| 900 | const options: ColineApiClientOptions = { |
| 901 | baseUrl: this.baseUrl, |
| 902 | apiKey, |
| 903 | fetch: this.fetchImpl, |
| 904 | maxRetries: this.maxRetries, |
| 905 | debug: this.debug, |
| 906 | }; |
| 907 |
| 908 | if (this.defaultHeaders !== undefined) { |
| 909 | options.headers = this.defaultHeaders; |
| 910 | } |
| 911 |
| 912 | return new ColineApiClient(options); |
| 913 | } |
| 914 |
| 915 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 916 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 917 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 918 |
| 919 | const options: ColineApiClientOptions = { |
| 920 | baseUrl: this.baseUrl, |
| 921 | fetch: this.fetchImpl, |
| 922 | headers: mergedHeaders, |
| 923 | maxRetries: this.maxRetries, |
| 924 | debug: this.debug, |
| 925 | }; |
| 926 |
| 927 | if (this.apiKey !== undefined) { |
| 928 | options.apiKey = this.apiKey; |
| 929 | } |
| 930 |
| 931 | return new ColineApiClient(options); |
| 932 | } |
| 933 |
| 934 | buildLoginWithColineAuthorizeUrl(input: { |
| 935 | clientId: string; |
| 936 | redirectUri: string; |
| 937 | scope?: string; |
| 938 | state?: string; |
| 939 | codeChallenge?: string; |
| 940 | codeChallengeMethod?: "S256" | "plain"; |
| 941 | }): string { |
| 942 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 943 | url.searchParams.set("response_type", "code"); |
| 944 | url.searchParams.set("client_id", input.clientId); |
| 945 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 946 | if (input.scope) { |
| 947 | url.searchParams.set("scope", input.scope); |
| 948 | } |
| 949 | if (input.state) { |
| 950 | url.searchParams.set("state", input.state); |
| 951 | } |
| 952 | if (input.codeChallenge) { |
| 953 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 954 | } |
| 955 | if (input.codeChallengeMethod) { |
| 956 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 957 | } |
| 958 | return url.toString(); |
| 959 | } |
| 960 |
| 961 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 962 | return this.unwrapGeneratedResponse( |
| 963 | this.appPlatformClient.GET("/api/v1/apps"), |
| 964 | ); |
| 965 | } |
| 966 |
| 967 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 968 | return this.unwrapGeneratedResponse( |
| 969 | this.appPlatformClient.GET("/api/v1/apps/{appKey}", { |
| 970 | params: { path: { appKey } }, |
| 971 | }), |
| 972 | ); |
| 973 | } |
| 974 |
| 975 | async listAppVersions( |
| 976 | appKey: string, |
| 977 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 978 | return this.unwrapGeneratedResponse( |
| 979 | this.appPlatformClient.GET("/api/v1/apps/{appKey}/versions", { |
| 980 | params: { path: { appKey } }, |
| 981 | }), |
| 982 | ); |
| 983 | } |
| 984 |
| 985 | async registerApp( |
| 986 | payload: ColineAppRegistrationPayloadInput, |
| 987 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 988 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 989 |
| 990 | return this.unwrapGeneratedResponse( |
| 991 | this.appPlatformClient.POST("/api/v1/apps", { |
| 992 | body, |
| 993 | }), |
| 994 | ); |
| 995 | } |
| 996 |
| 997 | async createAppVersion( |
| 998 | appKey: string, |
| 999 | payload: ColineAppRegistrationPayloadInput, |
| 1000 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 1001 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 1002 |
| 1003 | return this.unwrapGeneratedResponse( |
| 1004 | this.appPlatformClient.POST("/api/v1/apps/{appKey}/versions", { |
| 1005 | params: { path: { appKey } }, |
| 1006 | body, |
| 1007 | }), |
| 1008 | ); |
| 1009 | } |
| 1010 |
| 1011 | async submitAppVersionForReview( |
| 1012 | appKey: string, |
| 1013 | versionId: string, |
| 1014 | input: SubmitAppVersionForReviewInput = {}, |
| 1015 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 1016 | return this.unwrapGeneratedResponse( |
| 1017 | this.appPlatformClient.POST( |
| 1018 | "/api/v1/apps/{appKey}/versions/{versionId}/submit", |
| 1019 | { |
| 1020 | params: { path: { appKey, versionId } }, |
| 1021 | body: { |
| 1022 | ...(input.storeListed !== undefined |
| 1023 | ? { storeListed: input.storeListed } |
| 1024 | : {}), |
| 1025 | }, |
| 1026 | }, |
| 1027 | ), |
| 1028 | ); |
| 1029 | } |
| 1030 |
| 1031 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 1032 | return this.unwrapGeneratedResponse( |
| 1033 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 1034 | params: { path: { workspaceSlug } }, |
| 1035 | }), |
| 1036 | ); |
| 1037 | } |
| 1038 |
| 1039 | async installWorkspaceApp( |
| 1040 | workspaceSlug: string, |
| 1041 | input: InstallWorkspaceAppInput, |
| 1042 | ): Promise<InstallWorkspaceAppResponse> { |
| 1043 | const grantedPermissions = input.grantedPermissions |
| 1044 | ? input.grantedPermissions.map((permission) => |
| 1045 | appPermissionSchema.parse(permission), |
| 1046 | ) |
| 1047 | : undefined; |
| 1048 |
| 1049 | return this.unwrapGeneratedResponse( |
| 1050 | this.appPlatformClient.POST("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 1051 | params: { path: { workspaceSlug } }, |
| 1052 | body: { |
| 1053 | appId: input.appId, |
| 1054 | ...(input.pinnedVersionId |
| 1055 | ? { pinnedVersionId: input.pinnedVersionId } |
| 1056 | : {}), |
| 1057 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 1058 | }, |
| 1059 | }), |
| 1060 | ); |
| 1061 | } |
| 1062 |
| 1063 | async getWorkspaceAppPermissions( |
| 1064 | workspaceSlug: string, |
| 1065 | appKey: string, |
| 1066 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 1067 | return this.unwrapGeneratedResponse( |
| 1068 | this.appPlatformClient.GET( |
| 1069 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 1070 | { |
| 1071 | params: { path: { workspaceSlug, appKey } }, |
| 1072 | }, |
| 1073 | ), |
| 1074 | ); |
| 1075 | } |
| 1076 |
| 1077 | async getWorkspaceInstalledApp( |
| 1078 | workspaceSlug: string, |
| 1079 | appKey: string, |
| 1080 | ): Promise<WorkspaceInstalledAppResponse> { |
| 1081 | return this.unwrapGeneratedResponse( |
| 1082 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", { |
| 1083 | params: { path: { workspaceSlug, appKey } }, |
| 1084 | }), |
| 1085 | ); |
| 1086 | } |
| 1087 |
| 1088 | async updateWorkspaceAppPermissions( |
| 1089 | workspaceSlug: string, |
| 1090 | appKey: string, |
| 1091 | input: UpdateWorkspaceAppPermissionsInput, |
| 1092 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 1093 | return this.unwrapGeneratedResponse( |
| 1094 | this.appPlatformClient.PUT( |
| 1095 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 1096 | { |
| 1097 | params: { path: { workspaceSlug, appKey } }, |
| 1098 | body: { |
| 1099 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 1100 | appPermissionSchema.parse(permission), |
| 1101 | ), |
| 1102 | }, |
| 1103 | }, |
| 1104 | ), |
| 1105 | ); |
| 1106 | } |
| 1107 |
| 1108 | async uninstallWorkspaceApp( |
| 1109 | workspaceSlug: string, |
| 1110 | appKey: string, |
| 1111 | ): Promise<{ ok: true }> { |
| 1112 | return this.unwrapGeneratedResponse( |
| 1113 | this.appPlatformClient.DELETE( |
| 1114 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", |
| 1115 | { |
| 1116 | params: { path: { workspaceSlug, appKey } }, |
| 1117 | }, |
| 1118 | ), |
| 1119 | ); |
| 1120 | } |
| 1121 |
| 1122 | async listWorkspaceAppSecrets( |
| 1123 | workspaceSlug: string, |
| 1124 | appKey: string, |
| 1125 | ): Promise<WorkspaceAppSecretsResponse> { |
| 1126 | return this.unwrapGeneratedResponse( |
| 1127 | this.appPlatformClient.GET( |
| 1128 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 1129 | { |
| 1130 | params: { path: { workspaceSlug, appKey } }, |
| 1131 | }, |
| 1132 | ), |
| 1133 | ); |
| 1134 | } |
| 1135 |
| 1136 | async createWorkspaceAppSecret( |
| 1137 | workspaceSlug: string, |
| 1138 | appKey: string, |
| 1139 | input: { key: string; value: string }, |
| 1140 | ): Promise<CreateWorkspaceAppSecretResponse> { |
| 1141 | return this.unwrapGeneratedResponse( |
| 1142 | this.appPlatformClient.POST( |
| 1143 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 1144 | { |
| 1145 | params: { path: { workspaceSlug, appKey } }, |
| 1146 | body: input, |
| 1147 | }, |
| 1148 | ), |
| 1149 | ); |
| 1150 | } |
| 1151 |
| 1152 | async deleteWorkspaceAppSecret( |
| 1153 | workspaceSlug: string, |
| 1154 | appKey: string, |
| 1155 | secretId: string, |
| 1156 | ): Promise<{ ok: true }> { |
| 1157 | return this.unwrapGeneratedResponse( |
| 1158 | this.appPlatformClient.DELETE( |
| 1159 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets/{secretId}", |
| 1160 | { |
| 1161 | params: { path: { workspaceSlug, appKey, secretId } }, |
| 1162 | }, |
| 1163 | ), |
| 1164 | ); |
| 1165 | } |
| 1166 |
| 1167 | async listWorkspaceAppOauthConnections( |
| 1168 | workspaceSlug: string, |
| 1169 | appKey: string, |
| 1170 | ): Promise<WorkspaceAppOauthConnectionsResponse> { |
| 1171 | return this.unwrapGeneratedResponse( |
| 1172 | this.appPlatformClient.GET( |
| 1173 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 1174 | { |
| 1175 | params: { path: { workspaceSlug, appKey } }, |
| 1176 | }, |
| 1177 | ), |
| 1178 | ); |
| 1179 | } |
| 1180 |
| 1181 | async createWorkspaceAppOauthConnection( |
| 1182 | workspaceSlug: string, |
| 1183 | appKey: string, |
| 1184 | input: { |
| 1185 | providerKey: string; |
| 1186 | externalAccountId?: string | null; |
| 1187 | accessToken: string; |
| 1188 | refreshToken?: string | null; |
| 1189 | scopes: string[]; |
| 1190 | tokenExpiresAt?: string | null; |
| 1191 | metadata?: Record<string, unknown>; |
| 1192 | }, |
| 1193 | ): Promise<CreateWorkspaceAppOauthConnectionResponse> { |
| 1194 | const body: { |
| 1195 | providerKey: string; |
| 1196 | accessToken: string; |
| 1197 | scopes: string[]; |
| 1198 | externalAccountId?: string; |
| 1199 | refreshToken?: string; |
| 1200 | tokenExpiresAt?: string; |
| 1201 | metadata?: Record<string, unknown>; |
| 1202 | } = { |
| 1203 | providerKey: input.providerKey, |
| 1204 | accessToken: input.accessToken, |
| 1205 | scopes: input.scopes, |
| 1206 | }; |
| 1207 |
| 1208 | if (input.externalAccountId) { |
| 1209 | body.externalAccountId = input.externalAccountId; |
| 1210 | } |
| 1211 | if (input.refreshToken) { |
| 1212 | body.refreshToken = input.refreshToken; |
| 1213 | } |
| 1214 | if (input.tokenExpiresAt) { |
| 1215 | body.tokenExpiresAt = input.tokenExpiresAt; |
| 1216 | } |
| 1217 | if (input.metadata) { |
| 1218 | body.metadata = input.metadata; |
| 1219 | } |
| 1220 |
| 1221 | return this.unwrapGeneratedResponse( |
| 1222 | this.appPlatformClient.POST( |
| 1223 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 1224 | { |
| 1225 | params: { path: { workspaceSlug, appKey } }, |
| 1226 | body, |
| 1227 | }, |
| 1228 | ), |
| 1229 | ); |
| 1230 | } |
| 1231 |
| 1232 | async deleteWorkspaceAppOauthConnection( |
| 1233 | workspaceSlug: string, |
| 1234 | appKey: string, |
| 1235 | connectionId: string, |
| 1236 | ): Promise<{ ok: true }> { |
| 1237 | return this.unwrapGeneratedResponse( |
| 1238 | this.appPlatformClient.DELETE( |
| 1239 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections/{connectionId}", |
| 1240 | { |
| 1241 | params: { path: { workspaceSlug, appKey, connectionId } }, |
| 1242 | }, |
| 1243 | ), |
| 1244 | ); |
| 1245 | } |
| 1246 |
| 1247 | async listWorkspaceAppDeliveries( |
| 1248 | workspaceSlug: string, |
| 1249 | appKey: string, |
| 1250 | ): Promise<WorkspaceAppDeliveriesResponse> { |
| 1251 | return this.unwrapGeneratedResponse( |
| 1252 | this.appPlatformClient.GET( |
| 1253 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 1254 | { |
| 1255 | params: { path: { workspaceSlug, appKey } }, |
| 1256 | }, |
| 1257 | ), |
| 1258 | ); |
| 1259 | } |
| 1260 |
| 1261 | async sendWorkspaceAppTestDelivery( |
| 1262 | workspaceSlug: string, |
| 1263 | appKey: string, |
| 1264 | input: { |
| 1265 | eventType: string; |
| 1266 | targetUrl?: string; |
| 1267 | payload: Record<string, unknown>; |
| 1268 | }, |
| 1269 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 1270 | return this.unwrapGeneratedResponse( |
| 1271 | this.appPlatformClient.POST( |
| 1272 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 1273 | { |
| 1274 | params: { path: { workspaceSlug, appKey } }, |
| 1275 | body: input, |
| 1276 | }, |
| 1277 | ), |
| 1278 | ); |
| 1279 | } |
| 1280 |
| 1281 | async replayWorkspaceAppDelivery( |
| 1282 | workspaceSlug: string, |
| 1283 | appKey: string, |
| 1284 | deliveryId: string, |
| 1285 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 1286 | return this.unwrapGeneratedResponse( |
| 1287 | this.appPlatformClient.POST( |
| 1288 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries/{deliveryId}/replay", |
| 1289 | { |
| 1290 | params: { path: { workspaceSlug, appKey, deliveryId } }, |
| 1291 | }, |
| 1292 | ), |
| 1293 | ); |
| 1294 | } |
| 1295 |
| 1296 | async emitAppAmbientEvents( |
| 1297 | workspaceSlug: string, |
| 1298 | appKey: string, |
| 1299 | input: EmitAmbientEventsInputInput, |
| 1300 | ): Promise<EmitAppAmbientEventsResponse> { |
| 1301 | const parsed = emitAmbientEventsInputSchema.parse(input); |
| 1302 | const body = { |
| 1303 | events: parsed.events.map((event) => ({ |
| 1304 | sourceKind: event.sourceKind, |
| 1305 | action: event.action, |
| 1306 | entityType: event.entityType, |
| 1307 | entityId: event.entityId, |
| 1308 | dedupeKey: event.dedupeKey, |
| 1309 | payload: event.payload, |
| 1310 | metadata: event.metadata, |
| 1311 | schemaVersion: event.schemaVersion, |
| 1312 | ...(event.actorUserId ? { actorUserId: event.actorUserId } : {}), |
| 1313 | ...(event.containerType ? { containerType: event.containerType } : {}), |
| 1314 | ...(event.containerId ? { containerId: event.containerId } : {}), |
| 1315 | ...(event.occurredAt |
| 1316 | ? { occurredAt: event.occurredAt.toISOString() } |
| 1317 | : {}), |
| 1318 | })), |
| 1319 | }; |
| 1320 |
| 1321 | return this.unwrapGeneratedResponse( |
| 1322 | this.appPlatformClient.POST( |
| 1323 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/ambient/events", |
| 1324 | { |
| 1325 | params: { path: { workspaceSlug, appKey } }, |
| 1326 | body, |
| 1327 | }, |
| 1328 | ), |
| 1329 | ); |
| 1330 | } |
| 1331 |
| 1332 | async createAppNotification( |
| 1333 | workspaceSlug: string, |
| 1334 | appKey: string, |
| 1335 | input: CreateNotificationInputInput, |
| 1336 | ): Promise<OkResponse> { |
| 1337 | const parsed = createNotificationInputSchema.parse(input); |
| 1338 | const body = { |
| 1339 | channelKey: parsed.channelKey, |
| 1340 | typeKey: parsed.typeKey, |
| 1341 | recipients: parsed.recipients.map((recipient) => ({ |
| 1342 | userId: recipient.userId, |
| 1343 | })), |
| 1344 | title: parsed.title, |
| 1345 | priority: parsed.priority, |
| 1346 | metadata: parsed.metadata, |
| 1347 | ...(parsed.body ? { body: parsed.body } : {}), |
| 1348 | ...(parsed.targetUrl ? { targetUrl: parsed.targetUrl } : {}), |
| 1349 | ...(parsed.icon ? { icon: parsed.icon } : {}), |
| 1350 | ...(parsed.dedupeKey ? { dedupeKey: parsed.dedupeKey } : {}), |
| 1351 | }; |
| 1352 |
| 1353 | return this.unwrapGeneratedResponse( |
| 1354 | this.appPlatformClient.POST( |
| 1355 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/notifications", |
| 1356 | { |
| 1357 | params: { path: { workspaceSlug, appKey } }, |
| 1358 | body, |
| 1359 | }, |
| 1360 | ), |
| 1361 | ); |
| 1362 | } |
| 1363 |
| 1364 | async upsertAppIndexDocuments( |
| 1365 | workspaceSlug: string, |
| 1366 | appKey: string, |
| 1367 | input: BatchUpsertAppIndexDocumentsInput, |
| 1368 | ): Promise<UpsertAppIndexDocumentsResponse> { |
| 1369 | const parsed = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 1370 | const body = { |
| 1371 | documents: parsed.documents.map((document) => ({ |
| 1372 | documentKey: document.documentKey, |
| 1373 | documentType: document.documentType, |
| 1374 | title: document.title, |
| 1375 | body: document.body, |
| 1376 | metadata: document.metadata, |
| 1377 | visibilityMode: document.visibilityMode, |
| 1378 | visibility: document.visibility.map((grant) => ({ |
| 1379 | subjectType: grant.subjectType, |
| 1380 | subjectId: grant.subjectId, |
| 1381 | })), |
| 1382 | ...(document.fileId ? { fileId: document.fileId } : {}), |
| 1383 | ...(document.url ? { url: document.url } : {}), |
| 1384 | })), |
| 1385 | }; |
| 1386 |
| 1387 | return this.unwrapGeneratedResponse( |
| 1388 | this.appPlatformClient.POST( |
| 1389 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 1390 | { |
| 1391 | params: { path: { workspaceSlug, appKey } }, |
| 1392 | body, |
| 1393 | }, |
| 1394 | ), |
| 1395 | ); |
| 1396 | } |
| 1397 |
| 1398 | async deleteAppIndexDocuments( |
| 1399 | workspaceSlug: string, |
| 1400 | appKey: string, |
| 1401 | input: BatchDeleteAppIndexDocumentsInput, |
| 1402 | ): Promise<DeleteAppIndexDocumentsResponse> { |
| 1403 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 1404 |
| 1405 | return this.unwrapGeneratedResponse( |
| 1406 | this.appPlatformClient.DELETE( |
| 1407 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 1408 | { |
| 1409 | params: { path: { workspaceSlug, appKey } }, |
| 1410 | body, |
| 1411 | }, |
| 1412 | ), |
| 1413 | ); |
| 1414 | } |
| 1415 |
| 1416 | async listAppFiles( |
| 1417 | workspaceSlug: string, |
| 1418 | appKey: string, |
| 1419 | query?: { |
| 1420 | driveId?: string; |
| 1421 | typeKey?: string; |
| 1422 | limit?: number; |
| 1423 | }, |
| 1424 | ): Promise<AppFilesListResponse> { |
| 1425 | return this.unwrapGeneratedResponse( |
| 1426 | this.appPlatformClient.GET( |
| 1427 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 1428 | { |
| 1429 | params: { |
| 1430 | path: { workspaceSlug, appKey }, |
| 1431 | ...(query |
| 1432 | ? { |
| 1433 | query: { |
| 1434 | ...(query.driveId ? { driveId: query.driveId } : {}), |
| 1435 | ...(query.typeKey ? { typeKey: query.typeKey } : {}), |
| 1436 | ...(query.limit !== undefined ? { limit: query.limit } : {}), |
| 1437 | }, |
| 1438 | } |
| 1439 | : {}), |
| 1440 | }, |
| 1441 | }, |
| 1442 | ), |
| 1443 | ); |
| 1444 | } |
| 1445 |
| 1446 | async createAppFile( |
| 1447 | workspaceSlug: string, |
| 1448 | appKey: string, |
| 1449 | input: CreateAppFileInput, |
| 1450 | ): Promise<CreateAppFileResponse> { |
| 1451 | return this.unwrapGeneratedResponse( |
| 1452 | this.appPlatformClient.POST( |
| 1453 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 1454 | { |
| 1455 | params: { path: { workspaceSlug, appKey } }, |
| 1456 | body: { |
| 1457 | name: input.name, |
| 1458 | ...(input.typeKey ? { typeKey: input.typeKey } : {}), |
| 1459 | ...(input.driveId ? { driveId: input.driveId } : {}), |
| 1460 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 1461 | ...(input.document ? { document: input.document } : {}), |
| 1462 | }, |
| 1463 | }, |
| 1464 | ), |
| 1465 | ); |
| 1466 | } |
| 1467 |
| 1468 | async executeAppAction( |
| 1469 | workspaceSlug: string, |
| 1470 | appKey: string, |
| 1471 | input: ExecuteAppActionInput, |
| 1472 | ): Promise<ExecuteAppActionResponse> { |
| 1473 | return this.unwrapGeneratedResponse( |
| 1474 | this.appPlatformClient.POST( |
| 1475 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/actions", |
| 1476 | { |
| 1477 | params: { path: { workspaceSlug, appKey } }, |
| 1478 | body: { |
| 1479 | action: { |
| 1480 | type: input.action.type, |
| 1481 | ...(input.action.payload ? { payload: input.action.payload } : {}), |
| 1482 | }, |
| 1483 | ...(input.fileId !== undefined ? { fileId: input.fileId } : {}), |
| 1484 | }, |
| 1485 | }, |
| 1486 | ), |
| 1487 | ); |
| 1488 | } |
| 1489 |
| 1490 | async getAppFileDocument( |
| 1491 | workspaceSlug: string, |
| 1492 | appKey: string, |
| 1493 | fileId: string, |
| 1494 | ): Promise<AppFileDocumentResponse> { |
| 1495 | return this.unwrapGeneratedResponse( |
| 1496 | this.appPlatformClient.GET( |
| 1497 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 1498 | { |
| 1499 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 1500 | }, |
| 1501 | ), |
| 1502 | ); |
| 1503 | } |
| 1504 |
| 1505 | async updateAppFileDocument( |
| 1506 | workspaceSlug: string, |
| 1507 | appKey: string, |
| 1508 | fileId: string, |
| 1509 | document: Record<string, unknown>, |
| 1510 | ): Promise<AppFileDocumentUpdateResponse> { |
| 1511 | return this.unwrapGeneratedResponse( |
| 1512 | this.appPlatformClient.PATCH( |
| 1513 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 1514 | { |
| 1515 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 1516 | body: { document }, |
| 1517 | }, |
| 1518 | ), |
| 1519 | ); |
| 1520 | } |
| 1521 |
| 1522 | async listTabModels(): Promise<TabModelsResponse> { |
| 1523 | return this.unwrapGeneratedResponse( |
| 1524 | this.appPlatformClient.GET("/api/v1/tab/models"), |
| 1525 | ); |
| 1526 | } |
| 1527 |
| 1528 | async createTabChatCompletion(input: { |
| 1529 | model: string; |
| 1530 | stream?: boolean; |
| 1531 | tab_context: Record<string, unknown>; |
| 1532 | messages?: Array<{ role?: string; content?: string }>; |
| 1533 | }): Promise<TabChatCompletionResponse> { |
| 1534 | return this.unwrapGeneratedResponse( |
| 1535 | this.appPlatformClient.POST("/api/v1/tab/chat/completions", { |
| 1536 | body: input, |
| 1537 | }), |
| 1538 | ); |
| 1539 | } |
| 1540 |
| 1541 | async exchangeOAuthCode(input: { |
| 1542 | clientId: string; |
| 1543 | clientSecret: string; |
| 1544 | code: string; |
| 1545 | redirectUri: string; |
| 1546 | codeVerifier?: string; |
| 1547 | }): Promise<OAuthTokenResponse> { |
| 1548 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 1549 | method: "POST", |
| 1550 | headers: { |
| 1551 | "Content-Type": "application/json", |
| 1552 | }, |
| 1553 | body: JSON.stringify({ |
| 1554 | grant_type: "authorization_code", |
| 1555 | client_id: input.clientId, |
| 1556 | client_secret: input.clientSecret, |
| 1557 | code: input.code, |
| 1558 | redirect_uri: input.redirectUri, |
| 1559 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 1560 | }), |
| 1561 | }); |
| 1562 |
| 1563 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 1564 | } |
| 1565 |
| 1566 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 1567 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 1568 | method: "GET", |
| 1569 | headers: { |
| 1570 | Authorization: `Bearer ${accessToken}`, |
| 1571 | }, |
| 1572 | }); |
| 1573 |
| 1574 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 1575 | } |
| 1576 |
| 1577 | // --------------------------------------------------------------------------- |
| 1578 | // Core Platform API — internal fetch helper |
| 1579 | // --------------------------------------------------------------------------- |
| 1580 |
| 1581 | private async coreRequest<T>( |
| 1582 | method: string, |
| 1583 | path: string, |
| 1584 | body?: unknown, |
| 1585 | ): Promise<T> { |
| 1586 | const url = joinUrl(this.baseUrl, path); |
| 1587 | const headers: Record<string, string> = {}; |
| 1588 | if (this.apiKey) { |
| 1589 | headers["Authorization"] = `Bearer ${this.apiKey}`; |
| 1590 | } |
| 1591 | if (body !== undefined) { |
| 1592 | headers["Content-Type"] = "application/json"; |
| 1593 | } |
| 1594 | if (this.defaultHeaders) { |
| 1595 | new Headers(this.defaultHeaders).forEach((v, k) => { |
| 1596 | headers[k] = v; |
| 1597 | }); |
| 1598 | } |
| 1599 |
| 1600 | const requestInit: RequestInit = { |
| 1601 | method, |
| 1602 | headers, |
| 1603 | ...(body !== undefined ? { body: JSON.stringify(body) } : {}), |
| 1604 | }; |
| 1605 |
| 1606 | if (this.debug) { |
| 1607 | console.debug(`[coline-sdk] ${method} ${path}`, body !== undefined ? { body } : ""); |
| 1608 | } |
| 1609 |
| 1610 | let lastError: ColineApiError | undefined; |
| 1611 |
| 1612 | for (let attempt = 0; attempt <= this.maxRetries; attempt++) { |
| 1613 | if (attempt > 0) { |
| 1614 | // Exponential backoff: 500ms, 1s, 2s, 4s ... |
| 1615 | const delayMs = Math.min(500 * Math.pow(2, attempt - 1), 30_000); |
| 1616 | if (this.debug) { |
| 1617 | console.debug(`[coline-sdk] retry ${attempt}/${this.maxRetries} after ${delayMs}ms`); |
| 1618 | } |
| 1619 | await sleep(delayMs); |
| 1620 | } |
| 1621 |
| 1622 | const response = await this.fetchImpl(url, requestInit); |
| 1623 |
| 1624 | if (!response.ok) { |
| 1625 | const payload = (await response.json().catch(() => null)) as ApiErrorPayload | null; |
| 1626 | const error = new ColineApiError({ |
| 1627 | message: |
| 1628 | payload?.error?.message ?? `Request failed (${response.status}).`, |
| 1629 | type: "api", |
| 1630 | status: response.status, |
| 1631 | code: payload?.error?.code ?? null, |
| 1632 | }); |
| 1633 |
| 1634 | // Retry on 429 (rate limit) and 5xx (server errors) |
| 1635 | if (isRetryableStatus(response.status) && attempt < this.maxRetries) { |
| 1636 | lastError = error; |
| 1637 | continue; |
| 1638 | } |
| 1639 |
| 1640 | if (this.debug) { |
| 1641 | console.debug(`[coline-sdk] ${method} ${path} → ${response.status}`, payload); |
| 1642 | } |
| 1643 |
| 1644 | throw error; |
| 1645 | } |
| 1646 |
| 1647 | const json = (await response.json().catch(() => null)) as { |
| 1648 | data: T; |
| 1649 | } | null; |
| 1650 |
| 1651 | if (!json || !("data" in json)) { |
| 1652 | throw new ColineApiError({ |
| 1653 | message: "Invalid API response.", |
| 1654 | type: "api", |
| 1655 | status: response.status, |
| 1656 | }); |
| 1657 | } |
| 1658 |
| 1659 | if (this.debug) { |
| 1660 | console.debug(`[coline-sdk] ${method} ${path} → ${response.status} OK`); |
| 1661 | } |
| 1662 |
| 1663 | return json.data; |
| 1664 | } |
| 1665 |
| 1666 | // Should not reach here, but safety net |
| 1667 | throw lastError ?? new ColineApiError({ |
| 1668 | message: "Max retries exceeded.", |
| 1669 | type: "api", |
| 1670 | status: 0, |
| 1671 | }); |
| 1672 | } |
| 1673 |
| 1674 | // --------------------------------------------------------------------------- |
| 1675 | // Core Platform API — Workspace Members |
| 1676 | // --------------------------------------------------------------------------- |
| 1677 |
| 1678 | async listWorkspaceMembers( |
| 1679 | workspaceId: string, |
| 1680 | ): Promise<WorkspaceMembersResponse> { |
| 1681 | return this.coreRequest<WorkspaceMembersResponse>( |
| 1682 | "GET", |
| 1683 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/members`, |
| 1684 | ); |
| 1685 | } |
| 1686 |
| 1687 | // --------------------------------------------------------------------------- |
| 1688 | // Core Platform API — Notes |
| 1689 | // --------------------------------------------------------------------------- |
| 1690 |
| 1691 | async listWorkspaceNotes( |
| 1692 | workspaceId: string, |
| 1693 | query?: ListWorkspaceNotesQuery, |
| 1694 | ): Promise<WorkspaceNotesListResponse> { |
| 1695 | const params = new URLSearchParams(); |
| 1696 | if (query?.q) params.set("q", query.q); |
| 1697 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 1698 | if (query?.cursor) params.set("cursor", query.cursor); |
| 1699 | const qs = params.toString(); |
| 1700 | return this.coreRequest<WorkspaceNotesListResponse>( |
| 1701 | "GET", |
| 1702 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes${qs ? `?${qs}` : ""}`, |
| 1703 | ); |
| 1704 | } |
| 1705 |
| 1706 | async createWorkspaceNote( |
| 1707 | workspaceId: string, |
| 1708 | input: CreateWorkspaceNoteInput, |
| 1709 | ): Promise<WorkspaceNoteResponse> { |
| 1710 | return this.coreRequest<WorkspaceNoteResponse>( |
| 1711 | "POST", |
| 1712 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes`, |
| 1713 | { |
| 1714 | ...(input.title !== undefined ? { title: input.title } : {}), |
| 1715 | ...(input.body !== undefined ? { body: input.body } : {}), |
| 1716 | ...(input.blocks !== undefined ? { blocks: input.blocks } : {}), |
| 1717 | ...(input.driveId !== undefined ? { driveId: input.driveId } : {}), |
| 1718 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 1719 | }, |
| 1720 | ); |
| 1721 | } |
| 1722 |
| 1723 | async getWorkspaceNote( |
| 1724 | workspaceId: string, |
| 1725 | noteId: string, |
| 1726 | ): Promise<WorkspaceNoteDetailResponse> { |
| 1727 | return this.coreRequest<WorkspaceNoteDetailResponse>( |
| 1728 | "GET", |
| 1729 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1730 | ); |
| 1731 | } |
| 1732 |
| 1733 | async updateWorkspaceNote( |
| 1734 | workspaceId: string, |
| 1735 | noteId: string, |
| 1736 | input: UpdateWorkspaceNoteInput, |
| 1737 | ): Promise<WorkspaceNoteResponse> { |
| 1738 | return this.coreRequest<WorkspaceNoteResponse>( |
| 1739 | "PUT", |
| 1740 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1741 | input, |
| 1742 | ); |
| 1743 | } |
| 1744 |
| 1745 | async deleteWorkspaceNote( |
| 1746 | workspaceId: string, |
| 1747 | noteId: string, |
| 1748 | ): Promise<{ ok: true }> { |
| 1749 | return this.coreRequest<{ ok: true }>( |
| 1750 | "DELETE", |
| 1751 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/notes/${encodeURIComponent(noteId)}`, |
| 1752 | ); |
| 1753 | } |
| 1754 |
| 1755 | // --------------------------------------------------------------------------- |
| 1756 | // Core Platform API — Docs |
| 1757 | // --------------------------------------------------------------------------- |
| 1758 |
| 1759 | async listWorkspaceDocs( |
| 1760 | workspaceId: string, |
| 1761 | query?: ListWorkspaceDocsQuery, |
| 1762 | ): Promise<WorkspaceDocsListResponse> { |
| 1763 | const params = new URLSearchParams(); |
| 1764 | if (query?.q) params.set("q", query.q); |
| 1765 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 1766 | if (query?.cursor) params.set("cursor", query.cursor); |
| 1767 | const qs = params.toString(); |
| 1768 | return this.coreRequest<WorkspaceDocsListResponse>( |
| 1769 | "GET", |
| 1770 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/docs${qs ? `?${qs}` : ""}`, |
| 1771 | ); |
| 1772 | } |
| 1773 |
| 1774 | async createWorkspaceDoc( |
| 1775 | workspaceId: string, |
| 1776 | input: CreateWorkspaceDocInput, |
| 1777 | ): Promise<WorkspaceDocResponse> { |
| 1778 | return this.coreRequest<WorkspaceDocResponse>( |
| 1779 | "POST", |
| 1780 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/docs`, |
| 1781 | { |
| 1782 | ...(input.title !== undefined ? { title: input.title } : {}), |
| 1783 | ...(input.content !== undefined ? { content: input.content } : {}), |
| 1784 | ...(input.layout !== undefined ? { layout: input.layout } : {}), |
| 1785 | ...(input.driveId !== undefined ? { driveId: input.driveId } : {}), |
| 1786 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 1787 | }, |
| 1788 | ); |
| 1789 | } |
| 1790 |
| 1791 | async getWorkspaceDoc( |
| 1792 | workspaceId: string, |
| 1793 | docId: string, |
| 1794 | ): Promise<WorkspaceDocDetailResponse> { |
| 1795 | return this.coreRequest<WorkspaceDocDetailResponse>( |
| 1796 | "GET", |
| 1797 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/docs/${encodeURIComponent(docId)}`, |
| 1798 | ); |
| 1799 | } |
| 1800 |
| 1801 | async updateWorkspaceDoc( |
| 1802 | workspaceId: string, |
| 1803 | docId: string, |
| 1804 | input: UpdateWorkspaceDocInput, |
| 1805 | ): Promise<WorkspaceDocDetailResponse> { |
| 1806 | return this.coreRequest<WorkspaceDocDetailResponse>( |
| 1807 | "PATCH", |
| 1808 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/docs/${encodeURIComponent(docId)}`, |
| 1809 | input, |
| 1810 | ); |
| 1811 | } |
| 1812 |
| 1813 | async deleteWorkspaceDoc( |
| 1814 | workspaceId: string, |
| 1815 | docId: string, |
| 1816 | ): Promise<{ ok: true }> { |
| 1817 | return this.coreRequest<{ ok: true }>( |
| 1818 | "DELETE", |
| 1819 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/docs/${encodeURIComponent(docId)}`, |
| 1820 | ); |
| 1821 | } |
| 1822 |
| 1823 | // --------------------------------------------------------------------------- |
| 1824 | // Core Platform API — Drives |
| 1825 | // --------------------------------------------------------------------------- |
| 1826 |
| 1827 | async listWorkspaceDrives( |
| 1828 | workspaceId: string, |
| 1829 | ): Promise<WorkspaceDrivesListResponse> { |
| 1830 | return this.coreRequest<WorkspaceDrivesListResponse>( |
| 1831 | "GET", |
| 1832 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/drives`, |
| 1833 | ); |
| 1834 | } |
| 1835 |
| 1836 | // --------------------------------------------------------------------------- |
| 1837 | // Core Platform API — Files |
| 1838 | // --------------------------------------------------------------------------- |
| 1839 |
| 1840 | async listDriveFiles( |
| 1841 | workspaceId: string, |
| 1842 | driveId: string, |
| 1843 | query?: ListDriveFilesQuery, |
| 1844 | ): Promise<DriveFilesListResponse> { |
| 1845 | const params = new URLSearchParams(); |
| 1846 | if (query?.parentId) params.set("parentId", query.parentId); |
| 1847 | if (query?.fileType) params.set("fileType", query.fileType); |
| 1848 | if (query?.trashed !== undefined) params.set("trashed", String(query.trashed)); |
| 1849 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 1850 | if (query?.cursor) params.set("cursor", query.cursor); |
| 1851 | const qs = params.toString(); |
| 1852 | return this.coreRequest<DriveFilesListResponse>( |
| 1853 | "GET", |
| 1854 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/drives/${encodeURIComponent(driveId)}/files${qs ? `?${qs}` : ""}`, |
| 1855 | ); |
| 1856 | } |
| 1857 |
| 1858 | async getFile( |
| 1859 | workspaceId: string, |
| 1860 | fileId: string, |
| 1861 | ): Promise<FileDetailResponse> { |
| 1862 | return this.coreRequest<FileDetailResponse>( |
| 1863 | "GET", |
| 1864 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/files/${encodeURIComponent(fileId)}`, |
| 1865 | ); |
| 1866 | } |
| 1867 |
| 1868 | async updateFile( |
| 1869 | workspaceId: string, |
| 1870 | fileId: string, |
| 1871 | input: UpdateFileInput, |
| 1872 | ): Promise<FileDetailResponse> { |
| 1873 | return this.coreRequest<FileDetailResponse>( |
| 1874 | "PATCH", |
| 1875 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/files/${encodeURIComponent(fileId)}`, |
| 1876 | input, |
| 1877 | ); |
| 1878 | } |
| 1879 |
| 1880 | async deleteFile( |
| 1881 | workspaceId: string, |
| 1882 | fileId: string, |
| 1883 | ): Promise<{ ok: true }> { |
| 1884 | return this.coreRequest<{ ok: true }>( |
| 1885 | "DELETE", |
| 1886 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/files/${encodeURIComponent(fileId)}`, |
| 1887 | ); |
| 1888 | } |
| 1889 |
| 1890 | // --------------------------------------------------------------------------- |
| 1891 | // Core Platform API — Channels |
| 1892 | // --------------------------------------------------------------------------- |
| 1893 |
| 1894 | async listWorkspaceChannels( |
| 1895 | workspaceId: string, |
| 1896 | ): Promise<ChannelListResponse> { |
| 1897 | return this.coreRequest<ChannelListResponse>( |
| 1898 | "GET", |
| 1899 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/channels`, |
| 1900 | ); |
| 1901 | } |
| 1902 |
| 1903 | async listChannelMessages( |
| 1904 | workspaceId: string, |
| 1905 | channelId: string, |
| 1906 | options?: { limit?: number; cursor?: string }, |
| 1907 | ): Promise<MessagesListResponse> { |
| 1908 | const params = new URLSearchParams(); |
| 1909 | if (options?.limit !== undefined) params.set("limit", String(options.limit)); |
| 1910 | if (options?.cursor) params.set("cursor", options.cursor); |
| 1911 | const qs = params.toString(); |
| 1912 | return this.coreRequest<MessagesListResponse>( |
| 1913 | "GET", |
| 1914 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/channels/${encodeURIComponent(channelId)}/messages${qs ? `?${qs}` : ""}`, |
| 1915 | ); |
| 1916 | } |
| 1917 |
| 1918 | // --------------------------------------------------------------------------- |
| 1919 | // Core Platform API — Direct Messages |
| 1920 | // --------------------------------------------------------------------------- |
| 1921 |
| 1922 | async listWorkspaceDms( |
| 1923 | workspaceId: string, |
| 1924 | ): Promise<DmListResponse> { |
| 1925 | return this.coreRequest<DmListResponse>( |
| 1926 | "GET", |
| 1927 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/dms`, |
| 1928 | ); |
| 1929 | } |
| 1930 |
| 1931 | async listDmMessages( |
| 1932 | workspaceId: string, |
| 1933 | dmId: string, |
| 1934 | options?: { limit?: number; cursor?: string }, |
| 1935 | ): Promise<MessagesListResponse> { |
| 1936 | const params = new URLSearchParams(); |
| 1937 | if (options?.limit !== undefined) params.set("limit", String(options.limit)); |
| 1938 | if (options?.cursor) params.set("cursor", options.cursor); |
| 1939 | const qs = params.toString(); |
| 1940 | return this.coreRequest<MessagesListResponse>( |
| 1941 | "GET", |
| 1942 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/dms/${encodeURIComponent(dmId)}/messages${qs ? `?${qs}` : ""}`, |
| 1943 | ); |
| 1944 | } |
| 1945 |
| 1946 | async sendDmMessage( |
| 1947 | workspaceId: string, |
| 1948 | dmId: string, |
| 1949 | input: SendMessageInput, |
| 1950 | ): Promise<SendMessageResponse> { |
| 1951 | return this.coreRequest<SendMessageResponse>( |
| 1952 | "POST", |
| 1953 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/dms/${encodeURIComponent(dmId)}/messages`, |
| 1954 | input, |
| 1955 | ); |
| 1956 | } |
| 1957 |
| 1958 | async sendChannelMessage( |
| 1959 | workspaceId: string, |
| 1960 | channelId: string, |
| 1961 | input: SendMessageInput, |
| 1962 | ): Promise<SendMessageResponse> { |
| 1963 | return this.coreRequest<SendMessageResponse>( |
| 1964 | "POST", |
| 1965 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/channels/${encodeURIComponent(channelId)}/messages`, |
| 1966 | input, |
| 1967 | ); |
| 1968 | } |
| 1969 |
| 1970 | // --------------------------------------------------------------------------- |
| 1971 | // Core Platform API — Messages (cross-container) |
| 1972 | // --------------------------------------------------------------------------- |
| 1973 |
| 1974 | async getMessage( |
| 1975 | workspaceId: string, |
| 1976 | messageId: string, |
| 1977 | ): Promise<MessageDetailResponse> { |
| 1978 | return this.coreRequest<MessageDetailResponse>( |
| 1979 | "GET", |
| 1980 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}`, |
| 1981 | ); |
| 1982 | } |
| 1983 |
| 1984 | async editMessage( |
| 1985 | workspaceId: string, |
| 1986 | messageId: string, |
| 1987 | input: EditMessageInput, |
| 1988 | ): Promise<MessageDetailResponse> { |
| 1989 | return this.coreRequest<MessageDetailResponse>( |
| 1990 | "PATCH", |
| 1991 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}`, |
| 1992 | input, |
| 1993 | ); |
| 1994 | } |
| 1995 |
| 1996 | async deleteMessage( |
| 1997 | workspaceId: string, |
| 1998 | messageId: string, |
| 1999 | ): Promise<{ ok: true }> { |
| 2000 | return this.coreRequest<{ ok: true }>( |
| 2001 | "DELETE", |
| 2002 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}`, |
| 2003 | ); |
| 2004 | } |
| 2005 |
| 2006 | async getThread( |
| 2007 | workspaceId: string, |
| 2008 | messageId: string, |
| 2009 | ): Promise<ThreadResponse> { |
| 2010 | return this.coreRequest<ThreadResponse>( |
| 2011 | "GET", |
| 2012 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/thread`, |
| 2013 | ); |
| 2014 | } |
| 2015 |
| 2016 | async replyToThread( |
| 2017 | workspaceId: string, |
| 2018 | messageId: string, |
| 2019 | input: ThreadReplyInput, |
| 2020 | ): Promise<SendMessageResponse> { |
| 2021 | return this.coreRequest<SendMessageResponse>( |
| 2022 | "POST", |
| 2023 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/thread`, |
| 2024 | input, |
| 2025 | ); |
| 2026 | } |
| 2027 |
| 2028 | async addReaction( |
| 2029 | workspaceId: string, |
| 2030 | messageId: string, |
| 2031 | emoji: string, |
| 2032 | ): Promise<ReactionResponse> { |
| 2033 | return this.coreRequest<ReactionResponse>( |
| 2034 | "PUT", |
| 2035 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/reactions/${encodeURIComponent(emoji)}`, |
| 2036 | ); |
| 2037 | } |
| 2038 |
| 2039 | async removeReaction( |
| 2040 | workspaceId: string, |
| 2041 | messageId: string, |
| 2042 | emoji: string, |
| 2043 | ): Promise<{ ok: true }> { |
| 2044 | return this.coreRequest<{ ok: true }>( |
| 2045 | "DELETE", |
| 2046 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/reactions/${encodeURIComponent(emoji)}`, |
| 2047 | ); |
| 2048 | } |
| 2049 |
| 2050 | async pinMessage( |
| 2051 | workspaceId: string, |
| 2052 | messageId: string, |
| 2053 | ): Promise<PinResponse> { |
| 2054 | return this.coreRequest<PinResponse>( |
| 2055 | "PUT", |
| 2056 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/pin`, |
| 2057 | ); |
| 2058 | } |
| 2059 |
| 2060 | async unpinMessage( |
| 2061 | workspaceId: string, |
| 2062 | messageId: string, |
| 2063 | ): Promise<PinResponse> { |
| 2064 | return this.coreRequest<PinResponse>( |
| 2065 | "DELETE", |
| 2066 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/messages/${encodeURIComponent(messageId)}/pin`, |
| 2067 | ); |
| 2068 | } |
| 2069 |
| 2070 | // --------------------------------------------------------------------------- |
| 2071 | // Core Platform API — Calendar Events |
| 2072 | // --------------------------------------------------------------------------- |
| 2073 |
| 2074 | async listCalendarEvents( |
| 2075 | workspaceId: string, |
| 2076 | range: { start: string; end: string }, |
| 2077 | ): Promise<CalendarEventsListResponse> { |
| 2078 | const params = new URLSearchParams({ |
| 2079 | start: range.start, |
| 2080 | end: range.end, |
| 2081 | }); |
| 2082 | return this.coreRequest<CalendarEventsListResponse>( |
| 2083 | "GET", |
| 2084 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events?${params}`, |
| 2085 | ); |
| 2086 | } |
| 2087 |
| 2088 | async createCalendarEvent( |
| 2089 | workspaceId: string, |
| 2090 | input: CreateCalendarEventInput, |
| 2091 | ): Promise<CalendarEventDetailResponse> { |
| 2092 | return this.coreRequest<CalendarEventDetailResponse>( |
| 2093 | "POST", |
| 2094 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events`, |
| 2095 | input, |
| 2096 | ); |
| 2097 | } |
| 2098 |
| 2099 | async getCalendarEvent( |
| 2100 | workspaceId: string, |
| 2101 | eventId: string, |
| 2102 | ): Promise<CalendarEventDetailResponse> { |
| 2103 | return this.coreRequest<CalendarEventDetailResponse>( |
| 2104 | "GET", |
| 2105 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 2106 | ); |
| 2107 | } |
| 2108 |
| 2109 | async updateCalendarEvent( |
| 2110 | workspaceId: string, |
| 2111 | eventId: string, |
| 2112 | input: UpdateCalendarEventInput, |
| 2113 | ): Promise<CalendarEventDetailResponse> { |
| 2114 | return this.coreRequest<CalendarEventDetailResponse>( |
| 2115 | "PATCH", |
| 2116 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 2117 | input, |
| 2118 | ); |
| 2119 | } |
| 2120 |
| 2121 | async deleteCalendarEvent( |
| 2122 | workspaceId: string, |
| 2123 | eventId: string, |
| 2124 | ): Promise<{ ok: true }> { |
| 2125 | return this.coreRequest<{ ok: true }>( |
| 2126 | "DELETE", |
| 2127 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/calendar/events/${encodeURIComponent(eventId)}`, |
| 2128 | ); |
| 2129 | } |
| 2130 |
| 2131 | // --------------------------------------------------------------------------- |
| 2132 | // Core Platform API — Search |
| 2133 | // --------------------------------------------------------------------------- |
| 2134 |
| 2135 | async searchWorkspace( |
| 2136 | workspaceId: string, |
| 2137 | input: WorkspaceSearchInput, |
| 2138 | ): Promise<WorkspaceSearchResponse> { |
| 2139 | return this.coreRequest<WorkspaceSearchResponse>( |
| 2140 | "POST", |
| 2141 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/search`, |
| 2142 | input, |
| 2143 | ); |
| 2144 | } |
| 2145 |
| 2146 | // --------------------------------------------------------------------------- |
| 2147 | // Core Platform API — Taskboards |
| 2148 | // --------------------------------------------------------------------------- |
| 2149 |
| 2150 | async listWorkspaceTaskboards( |
| 2151 | workspaceId: string, |
| 2152 | ): Promise<WorkspaceTaskboardsResponse> { |
| 2153 | return this.coreRequest<WorkspaceTaskboardsResponse>( |
| 2154 | "GET", |
| 2155 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards`, |
| 2156 | ); |
| 2157 | } |
| 2158 |
| 2159 | // --------------------------------------------------------------------------- |
| 2160 | // Core Platform API — Tasks |
| 2161 | // --------------------------------------------------------------------------- |
| 2162 |
| 2163 | async listTaskboardTasks( |
| 2164 | workspaceId: string, |
| 2165 | taskboardId: string, |
| 2166 | query?: ListTaskboardTasksQuery, |
| 2167 | ): Promise<TaskboardTasksResponse> { |
| 2168 | const params = new URLSearchParams(); |
| 2169 | if (query?.q) params.set("q", query.q); |
| 2170 | if (query?.statusId) params.set("statusId", query.statusId); |
| 2171 | if (query?.assigneeUserId) params.set("assigneeUserId", query.assigneeUserId); |
| 2172 | if (query?.limit !== undefined) params.set("limit", String(query.limit)); |
| 2173 | const qs = params.toString(); |
| 2174 | return this.coreRequest<TaskboardTasksResponse>( |
| 2175 | "GET", |
| 2176 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks${qs ? `?${qs}` : ""}`, |
| 2177 | ); |
| 2178 | } |
| 2179 |
| 2180 | async createTaskboardTask( |
| 2181 | workspaceId: string, |
| 2182 | taskboardId: string, |
| 2183 | input: CreateTaskInput, |
| 2184 | ): Promise<CreateTaskResponse> { |
| 2185 | return this.coreRequest<CreateTaskResponse>( |
| 2186 | "POST", |
| 2187 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks`, |
| 2188 | input, |
| 2189 | ); |
| 2190 | } |
| 2191 |
| 2192 | async updateTaskboardTask( |
| 2193 | workspaceId: string, |
| 2194 | taskboardId: string, |
| 2195 | taskId: string, |
| 2196 | input: UpdateTaskInput, |
| 2197 | ): Promise<{ ok: true }> { |
| 2198 | return this.coreRequest<{ ok: true }>( |
| 2199 | "PATCH", |
| 2200 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks/${encodeURIComponent(taskId)}`, |
| 2201 | input, |
| 2202 | ); |
| 2203 | } |
| 2204 |
| 2205 | async deleteTaskboardTask( |
| 2206 | workspaceId: string, |
| 2207 | taskboardId: string, |
| 2208 | taskId: string, |
| 2209 | ): Promise<{ ok: true }> { |
| 2210 | return this.coreRequest<{ ok: true }>( |
| 2211 | "DELETE", |
| 2212 | `/api/v1/workspaces/${encodeURIComponent(workspaceId)}/taskboards/${encodeURIComponent(taskboardId)}/tasks/${encodeURIComponent(taskId)}`, |
| 2213 | ); |
| 2214 | } |
| 2215 |
| 2216 | // --------------------------------------------------------------------------- |
| 2217 | // Workspace-scoped client |
| 2218 | // --------------------------------------------------------------------------- |
| 2219 |
| 2220 | /** |
| 2221 | * Returns a workspace-scoped client that drops the `workspaceId` first |
| 2222 | * argument from every workspace method. |
| 2223 | * |
| 2224 | * @example |
| 2225 | * ```ts |
| 2226 | * const ws = coline.workspace("ws_abc123"); |
| 2227 | * const notes = await ws.listNotes({ limit: 10 }); |
| 2228 | * const thread = await ws.getThread(messageId); |
| 2229 | * for await (const note of ws.paginateNotes()) { |
| 2230 | * console.log(note.title); |
| 2231 | * } |
| 2232 | * ``` |
| 2233 | */ |
| 2234 | workspace(workspaceId: string): ColineWorkspace { |
| 2235 | return new ColineWorkspace(this, workspaceId); |
| 2236 | } |
| 2237 | } |
| 2238 |
| 2239 | // --------------------------------------------------------------------------- |
| 2240 | // Workspace-scoped client — removes workspaceId boilerplate |
| 2241 | // --------------------------------------------------------------------------- |
| 2242 |
| 2243 | /** |
| 2244 | * A convenience wrapper around `ColineApiClient` that binds a workspace ID |
| 2245 | * so you don't have to pass it to every call. |
| 2246 | * |
| 2247 | * Get one via `client.workspace("ws_abc123")`. |
| 2248 | */ |
| 2249 | export class ColineWorkspace { |
| 2250 | constructor( |
| 2251 | private readonly client: ColineApiClient, |
| 2252 | readonly workspaceId: string, |
| 2253 | ) {} |
| 2254 |
| 2255 | // -- Members --------------------------------------------------------------- |
| 2256 |
| 2257 | listMembers() { |
| 2258 | return this.client.listWorkspaceMembers(this.workspaceId); |
| 2259 | } |
| 2260 |
| 2261 | // -- Notes ----------------------------------------------------------------- |
| 2262 |
| 2263 | listNotes(query?: ListWorkspaceNotesQuery) { |
| 2264 | return this.client.listWorkspaceNotes(this.workspaceId, query); |
| 2265 | } |
| 2266 |
| 2267 | createNote(input: CreateWorkspaceNoteInput) { |
| 2268 | return this.client.createWorkspaceNote(this.workspaceId, input); |
| 2269 | } |
| 2270 |
| 2271 | getNote(noteId: string) { |
| 2272 | return this.client.getWorkspaceNote(this.workspaceId, noteId); |
| 2273 | } |
| 2274 |
| 2275 | updateNote(noteId: string, input: UpdateWorkspaceNoteInput) { |
| 2276 | return this.client.updateWorkspaceNote(this.workspaceId, noteId, input); |
| 2277 | } |
| 2278 |
| 2279 | deleteNote(noteId: string) { |
| 2280 | return this.client.deleteWorkspaceNote(this.workspaceId, noteId); |
| 2281 | } |
| 2282 |
| 2283 | /** Async generator that auto-paginates through all notes. */ |
| 2284 | paginateNotes(query?: Omit<ListWorkspaceNotesQuery, "cursor">) { |
| 2285 | return paginate<WorkspaceNoteSummary>(async (cursor) => { |
| 2286 | const res = await this.listNotes({ ...query, ...(cursor !== undefined ? { cursor } : {}) }); |
| 2287 | return { |
| 2288 | items: res.notes, |
| 2289 | nextCursor: res.page.nextCursor, |
| 2290 | hasMore: res.page.hasMore, |
| 2291 | }; |
| 2292 | }); |
| 2293 | } |
| 2294 |
| 2295 | // -- Docs ------------------------------------------------------------------ |
| 2296 |
| 2297 | listDocs(query?: ListWorkspaceDocsQuery) { |
| 2298 | return this.client.listWorkspaceDocs(this.workspaceId, query); |
| 2299 | } |
| 2300 |
| 2301 | createDoc(input: CreateWorkspaceDocInput) { |
| 2302 | return this.client.createWorkspaceDoc(this.workspaceId, input); |
| 2303 | } |
| 2304 |
| 2305 | getDoc(docId: string) { |
| 2306 | return this.client.getWorkspaceDoc(this.workspaceId, docId); |
| 2307 | } |
| 2308 |
| 2309 | updateDoc(docId: string, input: UpdateWorkspaceDocInput) { |
| 2310 | return this.client.updateWorkspaceDoc(this.workspaceId, docId, input); |
| 2311 | } |
| 2312 |
| 2313 | deleteDoc(docId: string) { |
| 2314 | return this.client.deleteWorkspaceDoc(this.workspaceId, docId); |
| 2315 | } |
| 2316 |
| 2317 | /** Async generator that auto-paginates through all docs. */ |
| 2318 | paginateDocs(query?: Omit<ListWorkspaceDocsQuery, "cursor">) { |
| 2319 | return paginate<WorkspaceDocSummary>(async (cursor) => { |
| 2320 | const res = await this.listDocs({ ...query, ...(cursor !== undefined ? { cursor } : {}) }); |
| 2321 | return { |
| 2322 | items: res.docs, |
| 2323 | nextCursor: res.page.nextCursor, |
| 2324 | hasMore: res.page.hasMore, |
| 2325 | }; |
| 2326 | }); |
| 2327 | } |
| 2328 |
| 2329 | // -- Drives & Files -------------------------------------------------------- |
| 2330 |
| 2331 | listDrives() { |
| 2332 | return this.client.listWorkspaceDrives(this.workspaceId); |
| 2333 | } |
| 2334 |
| 2335 | listDriveFiles(driveId: string, query?: ListDriveFilesQuery) { |
| 2336 | return this.client.listDriveFiles(this.workspaceId, driveId, query); |
| 2337 | } |
| 2338 |
| 2339 | getFile(fileId: string) { |
| 2340 | return this.client.getFile(this.workspaceId, fileId); |
| 2341 | } |
| 2342 |
| 2343 | updateFile(fileId: string, input: UpdateFileInput) { |
| 2344 | return this.client.updateFile(this.workspaceId, fileId, input); |
| 2345 | } |
| 2346 |
| 2347 | deleteFile(fileId: string) { |
| 2348 | return this.client.deleteFile(this.workspaceId, fileId); |
| 2349 | } |
| 2350 |
| 2351 | // -- Channels -------------------------------------------------------------- |
| 2352 |
| 2353 | listChannels() { |
| 2354 | return this.client.listWorkspaceChannels(this.workspaceId); |
| 2355 | } |
| 2356 |
| 2357 | listChannelMessages(channelId: string, query?: { cursor?: string; limit?: number }) { |
| 2358 | return this.client.listChannelMessages(this.workspaceId, channelId, query); |
| 2359 | } |
| 2360 |
| 2361 | sendChannelMessage(channelId: string, input: SendMessageInput) { |
| 2362 | return this.client.sendChannelMessage(this.workspaceId, channelId, input); |
| 2363 | } |
| 2364 |
| 2365 | // -- Direct Messages ------------------------------------------------------- |
| 2366 |
| 2367 | listDms() { |
| 2368 | return this.client.listWorkspaceDms(this.workspaceId); |
| 2369 | } |
| 2370 |
| 2371 | listDmMessages(dmId: string, query?: { cursor?: string; limit?: number }) { |
| 2372 | return this.client.listDmMessages(this.workspaceId, dmId, query); |
| 2373 | } |
| 2374 |
| 2375 | sendDmMessage(dmId: string, input: SendMessageInput) { |
| 2376 | return this.client.sendDmMessage(this.workspaceId, dmId, input); |
| 2377 | } |
| 2378 |
| 2379 | // -- Messages -------------------------------------------------------------- |
| 2380 |
| 2381 | getMessage(messageId: string) { |
| 2382 | return this.client.getMessage(this.workspaceId, messageId); |
| 2383 | } |
| 2384 |
| 2385 | editMessage(messageId: string, input: EditMessageInput) { |
| 2386 | return this.client.editMessage(this.workspaceId, messageId, input); |
| 2387 | } |
| 2388 |
| 2389 | deleteMessage(messageId: string) { |
| 2390 | return this.client.deleteMessage(this.workspaceId, messageId); |
| 2391 | } |
| 2392 |
| 2393 | getThread(messageId: string) { |
| 2394 | return this.client.getThread(this.workspaceId, messageId); |
| 2395 | } |
| 2396 |
| 2397 | replyToThread(messageId: string, input: ThreadReplyInput) { |
| 2398 | return this.client.replyToThread(this.workspaceId, messageId, input); |
| 2399 | } |
| 2400 |
| 2401 | addReaction(messageId: string, emoji: string) { |
| 2402 | return this.client.addReaction(this.workspaceId, messageId, emoji); |
| 2403 | } |
| 2404 |
| 2405 | removeReaction(messageId: string, emoji: string) { |
| 2406 | return this.client.removeReaction(this.workspaceId, messageId, emoji); |
| 2407 | } |
| 2408 |
| 2409 | pinMessage(messageId: string) { |
| 2410 | return this.client.pinMessage(this.workspaceId, messageId); |
| 2411 | } |
| 2412 |
| 2413 | unpinMessage(messageId: string) { |
| 2414 | return this.client.unpinMessage(this.workspaceId, messageId); |
| 2415 | } |
| 2416 |
| 2417 | // -- Calendar Events ------------------------------------------------------- |
| 2418 |
| 2419 | listCalendarEvents(range: { start: string; end: string }) { |
| 2420 | return this.client.listCalendarEvents(this.workspaceId, range); |
| 2421 | } |
| 2422 |
| 2423 | createCalendarEvent(input: CreateCalendarEventInput) { |
| 2424 | return this.client.createCalendarEvent(this.workspaceId, input); |
| 2425 | } |
| 2426 |
| 2427 | getCalendarEvent(eventId: string) { |
| 2428 | return this.client.getCalendarEvent(this.workspaceId, eventId); |
| 2429 | } |
| 2430 |
| 2431 | updateCalendarEvent(eventId: string, input: UpdateCalendarEventInput) { |
| 2432 | return this.client.updateCalendarEvent(this.workspaceId, eventId, input); |
| 2433 | } |
| 2434 |
| 2435 | deleteCalendarEvent(eventId: string) { |
| 2436 | return this.client.deleteCalendarEvent(this.workspaceId, eventId); |
| 2437 | } |
| 2438 |
| 2439 | // -- Search ---------------------------------------------------------------- |
| 2440 |
| 2441 | search(input: WorkspaceSearchInput) { |
| 2442 | return this.client.searchWorkspace(this.workspaceId, input); |
| 2443 | } |
| 2444 |
| 2445 | // -- Taskboards & Tasks ---------------------------------------------------- |
| 2446 |
| 2447 | listTaskboards() { |
| 2448 | return this.client.listWorkspaceTaskboards(this.workspaceId); |
| 2449 | } |
| 2450 |
| 2451 | listTasks(taskboardId: string, query?: ListTaskboardTasksQuery) { |
| 2452 | return this.client.listTaskboardTasks(this.workspaceId, taskboardId, query); |
| 2453 | } |
| 2454 |
| 2455 | createTask(taskboardId: string, input: CreateTaskInput) { |
| 2456 | return this.client.createTaskboardTask(this.workspaceId, taskboardId, input); |
| 2457 | } |
| 2458 |
| 2459 | updateTask(taskboardId: string, taskId: string, input: UpdateTaskInput) { |
| 2460 | return this.client.updateTaskboardTask(this.workspaceId, taskboardId, taskId, input); |
| 2461 | } |
| 2462 |
| 2463 | deleteTask(taskboardId: string, taskId: string) { |
| 2464 | return this.client.deleteTaskboardTask(this.workspaceId, taskboardId, taskId); |
| 2465 | } |
| 2466 |
| 2467 | // --------------------------------------------------------------------------- |
| 2468 | // Resource handles — chainable accessors for individual resources |
| 2469 | // --------------------------------------------------------------------------- |
| 2470 |
| 2471 | /** |
| 2472 | * Get a handle to a specific note. |
| 2473 | * @example |
| 2474 | * ```ts |
| 2475 | * const body = await ws.note("note_123").get(); |
| 2476 | * await ws.note("note_123").update({ title: "New title" }); |
| 2477 | * await ws.note("note_123").delete(); |
| 2478 | * ``` |
| 2479 | */ |
| 2480 | note(noteId: string): NoteHandle { |
| 2481 | return new NoteHandle(this, noteId); |
| 2482 | } |
| 2483 |
| 2484 | /** |
| 2485 | * Get a handle to a specific doc. |
| 2486 | * @example |
| 2487 | * ```ts |
| 2488 | * const doc = await ws.doc("doc_456").get(); |
| 2489 | * await ws.doc("doc_456").update({ title: "Updated" }); |
| 2490 | * ``` |
| 2491 | */ |
| 2492 | doc(docId: string): DocHandle { |
| 2493 | return new DocHandle(this, docId); |
| 2494 | } |
| 2495 |
| 2496 | /** |
| 2497 | * Get a handle to a specific file. |
| 2498 | * @example |
| 2499 | * ```ts |
| 2500 | * const file = await ws.file("file_789").get(); |
| 2501 | * await ws.file("file_789").update({ name: "renamed.pdf" }); |
| 2502 | * await ws.file("file_789").delete(); |
| 2503 | * ``` |
| 2504 | */ |
| 2505 | file(fileId: string): FileHandle { |
| 2506 | return new FileHandle(this, fileId); |
| 2507 | } |
| 2508 |
| 2509 | /** |
| 2510 | * Get a handle to a specific message. |
| 2511 | * @example |
| 2512 | * ```ts |
| 2513 | * const msg = await ws.message("msg_123").get(); |
| 2514 | * await ws.message("msg_123").edit({ content: Message.text("updated").build() }); |
| 2515 | * await ws.message("msg_123").react("👍"); |
| 2516 | * await ws.message("msg_123").pin(); |
| 2517 | * const thread = await ws.message("msg_123").thread(); |
| 2518 | * await ws.message("msg_123").reply(Message.text("reply!").toInput()); |
| 2519 | * ``` |
| 2520 | */ |
| 2521 | message(messageId: string): MessageHandle { |
| 2522 | return new MessageHandle(this, messageId); |
| 2523 | } |
| 2524 |
| 2525 | /** |
| 2526 | * Get a handle to a specific channel. |
| 2527 | * @example |
| 2528 | * ```ts |
| 2529 | * const messages = await ws.channel("ch_123").messages(); |
| 2530 | * await ws.channel("ch_123").send(Message.text("hello!").toInput()); |
| 2531 | * ``` |
| 2532 | */ |
| 2533 | channel(channelId: string): ChannelHandle { |
| 2534 | return new ChannelHandle(this, channelId); |
| 2535 | } |
| 2536 |
| 2537 | /** |
| 2538 | * Get a handle to a specific DM. |
| 2539 | * @example |
| 2540 | * ```ts |
| 2541 | * await ws.dm("dm_456").send(Message.text("hey!").toInput()); |
| 2542 | * ``` |
| 2543 | */ |
| 2544 | dm(dmId: string): DmHandle { |
| 2545 | return new DmHandle(this, dmId); |
| 2546 | } |
| 2547 |
| 2548 | /** |
| 2549 | * Get a handle to a specific calendar event. |
| 2550 | * @example |
| 2551 | * ```ts |
| 2552 | * const event = await ws.event("evt_789").get(); |
| 2553 | * await ws.event("evt_789").update({ title: "Moved" }); |
| 2554 | * await ws.event("evt_789").delete(); |
| 2555 | * ``` |
| 2556 | */ |
| 2557 | event(eventId: string): CalendarEventHandle { |
| 2558 | return new CalendarEventHandle(this, eventId); |
| 2559 | } |
| 2560 |
| 2561 | /** |
| 2562 | * Get a handle to a specific taskboard. |
| 2563 | * @example |
| 2564 | * ```ts |
| 2565 | * const tasks = await ws.taskboard("tb_123").listTasks(); |
| 2566 | * await ws.taskboard("tb_123").createTask({ title: "New task" }); |
| 2567 | * await ws.taskboard("tb_123").task("task_456").update({ priority: "high" }); |
| 2568 | * ``` |
| 2569 | */ |
| 2570 | taskboard(taskboardId: string): TaskboardHandle { |
| 2571 | return new TaskboardHandle(this, taskboardId); |
| 2572 | } |
| 2573 | } |
| 2574 |
| 2575 | // --------------------------------------------------------------------------- |
| 2576 | // Resource handles |
| 2577 | // --------------------------------------------------------------------------- |
| 2578 |
| 2579 | export class NoteHandle { |
| 2580 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2581 | get() { return this.ws.getNote(this.id); } |
| 2582 | update(input: UpdateWorkspaceNoteInput) { return this.ws.updateNote(this.id, input); } |
| 2583 | delete() { return this.ws.deleteNote(this.id); } |
| 2584 | } |
| 2585 |
| 2586 | export class DocHandle { |
| 2587 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2588 | get() { return this.ws.getDoc(this.id); } |
| 2589 | update(input: UpdateWorkspaceDocInput) { return this.ws.updateDoc(this.id, input); } |
| 2590 | delete() { return this.ws.deleteDoc(this.id); } |
| 2591 | } |
| 2592 |
| 2593 | export class FileHandle { |
| 2594 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2595 | get() { return this.ws.getFile(this.id); } |
| 2596 | update(input: UpdateFileInput) { return this.ws.updateFile(this.id, input); } |
| 2597 | delete() { return this.ws.deleteFile(this.id); } |
| 2598 | } |
| 2599 |
| 2600 | export class MessageHandle { |
| 2601 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2602 | get() { return this.ws.getMessage(this.id); } |
| 2603 | edit(input: EditMessageInput) { return this.ws.editMessage(this.id, input); } |
| 2604 | delete() { return this.ws.deleteMessage(this.id); } |
| 2605 | thread() { return this.ws.getThread(this.id); } |
| 2606 | reply(input: ThreadReplyInput) { return this.ws.replyToThread(this.id, input); } |
| 2607 | react(emoji: string) { return this.ws.addReaction(this.id, emoji); } |
| 2608 | unreact(emoji: string) { return this.ws.removeReaction(this.id, emoji); } |
| 2609 | pin() { return this.ws.pinMessage(this.id); } |
| 2610 | unpin() { return this.ws.unpinMessage(this.id); } |
| 2611 | } |
| 2612 |
| 2613 | export class ChannelHandle { |
| 2614 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2615 | messages(query?: { cursor?: string; limit?: number }) { |
| 2616 | return this.ws.listChannelMessages(this.id, query); |
| 2617 | } |
| 2618 | send(input: SendMessageInput) { return this.ws.sendChannelMessage(this.id, input); } |
| 2619 | } |
| 2620 |
| 2621 | export class DmHandle { |
| 2622 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2623 | messages(query?: { cursor?: string; limit?: number }) { |
| 2624 | return this.ws.listDmMessages(this.id, query); |
| 2625 | } |
| 2626 | send(input: SendMessageInput) { return this.ws.sendDmMessage(this.id, input); } |
| 2627 | } |
| 2628 |
| 2629 | export class CalendarEventHandle { |
| 2630 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2631 | get() { return this.ws.getCalendarEvent(this.id); } |
| 2632 | update(input: UpdateCalendarEventInput) { return this.ws.updateCalendarEvent(this.id, input); } |
| 2633 | delete() { return this.ws.deleteCalendarEvent(this.id); } |
| 2634 | } |
| 2635 |
| 2636 | export class TaskboardHandle { |
| 2637 | constructor(private readonly ws: ColineWorkspace, readonly id: string) {} |
| 2638 | listTasks(query?: ListTaskboardTasksQuery) { return this.ws.listTasks(this.id, query); } |
| 2639 | createTask(input: CreateTaskInput) { return this.ws.createTask(this.id, input); } |
| 2640 | task(taskId: string): TaskHandle { return new TaskHandle(this.ws, this.id, taskId); } |
| 2641 | } |
| 2642 |
| 2643 | export class TaskHandle { |
| 2644 | constructor( |
| 2645 | private readonly ws: ColineWorkspace, |
| 2646 | private readonly taskboardId: string, |
| 2647 | readonly id: string, |
| 2648 | ) {} |
| 2649 | update(input: UpdateTaskInput) { return this.ws.updateTask(this.taskboardId, this.id, input); } |
| 2650 | delete() { return this.ws.deleteTask(this.taskboardId, this.id); } |
| 2651 | } |
| 2652 |