The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 6 | emitAmbientEventsInputSchema, |
| 7 | appPermissionSchema, |
| 8 | type AppPermission, |
| 9 | type BatchDeleteAppIndexDocumentsInput, |
| 10 | type BatchUpsertAppIndexDocumentsInput, |
| 11 | type ColineAppRegistrationPayload, |
| 12 | type CreateNotificationInputInput, |
| 13 | type EmitAmbientEventsInputInput, |
| 14 | } from "./manifest"; |
| 15 | import { ColineApiError } from "./errors"; |
| 16 | import type { components as AppPlatformComponents } from "./generated/app-platform-v1"; |
| 17 | import { |
| 18 | createAppPlatformClient, |
| 19 | type AppPlatformClient, |
| 20 | } from "./app-platform-client"; |
| 21 |
| 22 | interface ApiErrorPayload { |
| 23 | error?: { |
| 24 | code?: string; |
| 25 | message?: string; |
| 26 | }; |
| 27 | } |
| 28 |
| 29 | async function parseJsonResponse<T>(response: Response): Promise<T> { |
| 30 | const payload = (await response.json().catch(() => null)) as T | null; |
| 31 |
| 32 | if (!response.ok) { |
| 33 | const errorMessage = |
| 34 | payload && |
| 35 | typeof payload === "object" && |
| 36 | "error_description" in payload && |
| 37 | typeof (payload as Record<string, unknown>)["error_description"] === "string" |
| 38 | ? ((payload as Record<string, unknown>)["error_description"] as string) |
| 39 | : `Request failed (${response.status}).`; |
| 40 |
| 41 | throw new ColineApiError({ |
| 42 | message: errorMessage, |
| 43 | type: "oauth", |
| 44 | status: response.status, |
| 45 | code: |
| 46 | payload && |
| 47 | typeof payload === "object" && |
| 48 | "error" in payload && |
| 49 | typeof (payload as Record<string, unknown>)["error"] === "string" |
| 50 | ? ((payload as Record<string, unknown>)["error"] as string) |
| 51 | : null, |
| 52 | }); |
| 53 | } |
| 54 |
| 55 | if (!payload) { |
| 56 | throw new ColineApiError({ |
| 57 | message: "Invalid JSON response.", |
| 58 | type: "oauth", |
| 59 | status: response.status, |
| 60 | }); |
| 61 | } |
| 62 |
| 63 | return payload; |
| 64 | } |
| 65 |
| 66 | export interface ColineApiClientOptions { |
| 67 | baseUrl: string; |
| 68 | apiKey?: string; |
| 69 | fetch?: typeof fetch; |
| 70 | headers?: HeadersInit; |
| 71 | } |
| 72 |
| 73 | type Schemas = AppPlatformComponents["schemas"]; |
| 74 | type UnwrapData<TKey extends keyof Schemas> = |
| 75 | Schemas[TKey] extends { data: infer T } ? T : never; |
| 76 |
| 77 | export type RegisteredAppDetail = Schemas["RegisteredAppDetail"]; |
| 78 | export type RegisteredAppRecord = Omit< |
| 79 | RegisteredAppDetail, |
| 80 | "latestPublishedVersionId" |
| 81 | >; |
| 82 | export type RegisteredAppVersionSummary = Schemas["RegisteredAppVersionSummary"]; |
| 83 | export type WorkspaceInstalledApp = Schemas["InstalledAppSummary"]; |
| 84 | export type WorkspaceAppCatalogItem = Schemas["AppCatalogItem"]; |
| 85 | export type WorkspaceAppsResponse = UnwrapData<"WorkspaceAppsResponse">; |
| 86 |
| 87 | export interface InstallWorkspaceAppInput { |
| 88 | appId: string; |
| 89 | pinnedVersionId?: string; |
| 90 | grantedPermissions?: AppPermission[]; |
| 91 | } |
| 92 |
| 93 | export type InstallWorkspaceAppResponse = UnwrapData<"InstallWorkspaceAppResponse">; |
| 94 |
| 95 | export interface SubmitAppVersionForReviewInput { |
| 96 | storeListed?: boolean; |
| 97 | } |
| 98 |
| 99 | export type SubmitAppVersionForReviewResponse = UnwrapData< |
| 100 | "SubmitAppVersionForReviewResponse" |
| 101 | >; |
| 102 | export type WorkspaceAppPermissionsResponse = UnwrapData< |
| 103 | "WorkspaceAppPermissionsResponse" |
| 104 | >; |
| 105 | export type WorkspaceInstalledAppResponse = UnwrapData< |
| 106 | "WorkspaceInstalledAppResponse" |
| 107 | >; |
| 108 | export type WorkspaceAppSecretsResponse = UnwrapData< |
| 109 | "WorkspaceAppSecretsResponse" |
| 110 | >; |
| 111 | export type WorkspaceAppSecretSummary = WorkspaceAppSecretsResponse["secrets"][number]; |
| 112 | export type CreateWorkspaceAppSecretResponse = UnwrapData< |
| 113 | "CreateWorkspaceAppSecretResponse" |
| 114 | >; |
| 115 | export type WorkspaceAppOauthConnectionsResponse = UnwrapData< |
| 116 | "WorkspaceAppOauthConnectionsResponse" |
| 117 | >; |
| 118 | export type WorkspaceAppOauthConnectionSummary = |
| 119 | WorkspaceAppOauthConnectionsResponse["connections"][number]; |
| 120 | export type CreateWorkspaceAppOauthConnectionResponse = UnwrapData< |
| 121 | "CreateWorkspaceAppOauthConnectionResponse" |
| 122 | >; |
| 123 | export type WorkspaceAppDeliveriesResponse = UnwrapData< |
| 124 | "WorkspaceAppDeliveriesResponse" |
| 125 | >; |
| 126 | export type WorkspaceAppDeliverySummary = |
| 127 | WorkspaceAppDeliveriesResponse["deliveries"][number]; |
| 128 | export type SendWorkspaceAppTestDeliveryResponse = UnwrapData< |
| 129 | "SendWorkspaceAppTestDeliveryResponse" |
| 130 | >; |
| 131 | export type AppFilesListResponse = UnwrapData<"AppFilesListResponse">; |
| 132 | export type AppFileDocumentResponse = UnwrapData<"AppFileDocumentResponse">; |
| 133 | export type AppFileDocumentUpdateResponse = UnwrapData< |
| 134 | "AppFileDocumentUpdateResponse" |
| 135 | >; |
| 136 | export type CreateAppFileResponse = UnwrapData<"CreateAppFileResponse">; |
| 137 | export type ExecuteAppActionResponse = UnwrapData<"ExecuteAppActionResponse">; |
| 138 | export type OkResponse = UnwrapData<"OkResponse">; |
| 139 | export type TabModelRecord = Schemas["TabModelRecord"]; |
| 140 | export type TabModelsResponse = UnwrapData<"TabModelsResponse">; |
| 141 | export type TabChatCompletionResponse = UnwrapData< |
| 142 | "TabChatCompletionResponseEnvelope" |
| 143 | >; |
| 144 | export type UpsertAppIndexDocumentsResponse = UnwrapData< |
| 145 | "UpsertAppIndexDocumentsResponse" |
| 146 | >; |
| 147 | export type DeleteAppIndexDocumentsResponse = UnwrapData< |
| 148 | "DeleteAppIndexDocumentsResponse" |
| 149 | >; |
| 150 | export type EmitAppAmbientEventsResponse = UnwrapData< |
| 151 | "EmitAppAmbientEventsResponse" |
| 152 | >; |
| 153 |
| 154 | export interface OAuthTokenResponse { |
| 155 | access_token: string; |
| 156 | token_type: "Bearer"; |
| 157 | expires_in: number; |
| 158 | refresh_token: string; |
| 159 | scope: string; |
| 160 | } |
| 161 |
| 162 | export interface OAuthUserInfoResponse { |
| 163 | sub: string; |
| 164 | email: string; |
| 165 | email_verified: boolean; |
| 166 | name: string; |
| 167 | given_name: string | null; |
| 168 | family_name: string | null; |
| 169 | picture: string | null; |
| 170 | preferred_username: string | null; |
| 171 | scope: string; |
| 172 | } |
| 173 |
| 174 | export interface UpdateWorkspaceAppPermissionsInput { |
| 175 | grantedPermissions: AppPermission[]; |
| 176 | } |
| 177 |
| 178 | export interface CreateAppFileInput { |
| 179 | name: string; |
| 180 | typeKey?: string; |
| 181 | driveId?: string; |
| 182 | parentId?: string | null; |
| 183 | document?: Record<string, unknown>; |
| 184 | } |
| 185 |
| 186 | export interface ExecuteAppActionInput { |
| 187 | action: { |
| 188 | type: string; |
| 189 | payload?: Record<string, unknown>; |
| 190 | }; |
| 191 | fileId?: string | null; |
| 192 | } |
| 193 |
| 194 | function joinUrl(baseUrl: string, path: string): string { |
| 195 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 196 | } |
| 197 |
| 198 | function toRegisterAppInput(payload: ColineAppRegistrationPayload) { |
| 199 | const notificationChannels = payload.manifest.notificationChannels.map((channel) => ({ |
| 200 | key: channel.key, |
| 201 | name: channel.name, |
| 202 | defaultDeliveries: [...channel.defaultDeliveries], |
| 203 | defaultPriority: channel.defaultPriority, |
| 204 | ...(channel.description ? { description: channel.description } : {}), |
| 205 | })); |
| 206 |
| 207 | const manifest = { |
| 208 | key: payload.manifest.key, |
| 209 | name: payload.manifest.name, |
| 210 | permissions: payload.manifest.permissions, |
| 211 | notificationChannels, |
| 212 | hosting: { |
| 213 | mode: payload.manifest.hosting.mode, |
| 214 | baseUrl: payload.manifest.hosting.baseUrl, |
| 215 | }, |
| 216 | ...(payload.manifest.description |
| 217 | ? { description: payload.manifest.description } |
| 218 | : {}), |
| 219 | ...(payload.manifest.iconUrl ? { iconUrl: payload.manifest.iconUrl } : {}), |
| 220 | ...(payload.manifest.kairo |
| 221 | ? { |
| 222 | kairo: JSON.parse(JSON.stringify(payload.manifest.kairo)) as Record< |
| 223 | string, |
| 224 | unknown |
| 225 | >, |
| 226 | } |
| 227 | : {}), |
| 228 | }; |
| 229 |
| 230 | const fileTypes = payload.fileTypes.map((fileType) => ({ |
| 231 | typeKey: fileType.typeKey, |
| 232 | name: fileType.name, |
| 233 | storage: fileType.storage, |
| 234 | indexable: fileType.indexable, |
| 235 | homeRenderMode: fileType.homeRenderMode, |
| 236 | ...(fileType.description ? { description: fileType.description } : {}), |
| 237 | })); |
| 238 |
| 239 | return { |
| 240 | version: payload.version, |
| 241 | manifest, |
| 242 | fileTypes, |
| 243 | }; |
| 244 | } |
| 245 |
| 246 | export class ColineApiClient { |
| 247 | private readonly baseUrl: string; |
| 248 | private readonly apiKey: string | undefined; |
| 249 | private readonly fetchImpl: typeof fetch; |
| 250 | private readonly defaultHeaders: HeadersInit | undefined; |
| 251 | private readonly appPlatformClient: AppPlatformClient; |
| 252 |
| 253 | constructor(options: ColineApiClientOptions) { |
| 254 | this.baseUrl = options.baseUrl; |
| 255 | this.apiKey = options.apiKey; |
| 256 | this.fetchImpl = options.fetch ?? fetch; |
| 257 | this.defaultHeaders = options.headers; |
| 258 | this.appPlatformClient = createAppPlatformClient(options); |
| 259 | } |
| 260 |
| 261 | private async unwrapGeneratedResponse<T>( |
| 262 | promise: Promise<{ |
| 263 | data?: { data: T }; |
| 264 | error?: ApiErrorPayload; |
| 265 | response: Response; |
| 266 | }>, |
| 267 | ): Promise<T> { |
| 268 | const { data, error, response } = await promise; |
| 269 |
| 270 | if (error) { |
| 271 | throw new ColineApiError({ |
| 272 | message: error.error?.message ?? `Request failed (${response.status}).`, |
| 273 | type: "api", |
| 274 | status: response.status, |
| 275 | code: error.error?.code ?? null, |
| 276 | }); |
| 277 | } |
| 278 |
| 279 | if (data === undefined) { |
| 280 | throw new ColineApiError({ |
| 281 | message: "Invalid API response.", |
| 282 | type: "api", |
| 283 | status: response.status, |
| 284 | }); |
| 285 | } |
| 286 |
| 287 | return data.data; |
| 288 | } |
| 289 |
| 290 | withApiKey(apiKey: string): ColineApiClient { |
| 291 | const options: ColineApiClientOptions = { |
| 292 | baseUrl: this.baseUrl, |
| 293 | apiKey, |
| 294 | fetch: this.fetchImpl, |
| 295 | }; |
| 296 |
| 297 | if (this.defaultHeaders !== undefined) { |
| 298 | options.headers = this.defaultHeaders; |
| 299 | } |
| 300 |
| 301 | return new ColineApiClient(options); |
| 302 | } |
| 303 |
| 304 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 305 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 306 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 307 |
| 308 | const options: ColineApiClientOptions = { |
| 309 | baseUrl: this.baseUrl, |
| 310 | fetch: this.fetchImpl, |
| 311 | headers: mergedHeaders, |
| 312 | }; |
| 313 |
| 314 | if (this.apiKey !== undefined) { |
| 315 | options.apiKey = this.apiKey; |
| 316 | } |
| 317 |
| 318 | return new ColineApiClient(options); |
| 319 | } |
| 320 |
| 321 | buildLoginWithColineAuthorizeUrl(input: { |
| 322 | clientId: string; |
| 323 | redirectUri: string; |
| 324 | scope?: string; |
| 325 | state?: string; |
| 326 | codeChallenge?: string; |
| 327 | codeChallengeMethod?: "S256" | "plain"; |
| 328 | }): string { |
| 329 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 330 | url.searchParams.set("response_type", "code"); |
| 331 | url.searchParams.set("client_id", input.clientId); |
| 332 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 333 | if (input.scope) { |
| 334 | url.searchParams.set("scope", input.scope); |
| 335 | } |
| 336 | if (input.state) { |
| 337 | url.searchParams.set("state", input.state); |
| 338 | } |
| 339 | if (input.codeChallenge) { |
| 340 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 341 | } |
| 342 | if (input.codeChallengeMethod) { |
| 343 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 344 | } |
| 345 | return url.toString(); |
| 346 | } |
| 347 |
| 348 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 349 | return this.unwrapGeneratedResponse( |
| 350 | this.appPlatformClient.GET("/api/v1/apps"), |
| 351 | ); |
| 352 | } |
| 353 |
| 354 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 355 | return this.unwrapGeneratedResponse( |
| 356 | this.appPlatformClient.GET("/api/v1/apps/{appKey}", { |
| 357 | params: { path: { appKey } }, |
| 358 | }), |
| 359 | ); |
| 360 | } |
| 361 |
| 362 | async listAppVersions( |
| 363 | appKey: string, |
| 364 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 365 | return this.unwrapGeneratedResponse( |
| 366 | this.appPlatformClient.GET("/api/v1/apps/{appKey}/versions", { |
| 367 | params: { path: { appKey } }, |
| 368 | }), |
| 369 | ); |
| 370 | } |
| 371 |
| 372 | async registerApp( |
| 373 | payload: ColineAppRegistrationPayload, |
| 374 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 375 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 376 |
| 377 | return this.unwrapGeneratedResponse( |
| 378 | this.appPlatformClient.POST("/api/v1/apps", { |
| 379 | body, |
| 380 | }), |
| 381 | ); |
| 382 | } |
| 383 |
| 384 | async createAppVersion( |
| 385 | appKey: string, |
| 386 | payload: ColineAppRegistrationPayload, |
| 387 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 388 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 389 |
| 390 | return this.unwrapGeneratedResponse( |
| 391 | this.appPlatformClient.POST("/api/v1/apps/{appKey}/versions", { |
| 392 | params: { path: { appKey } }, |
| 393 | body, |
| 394 | }), |
| 395 | ); |
| 396 | } |
| 397 |
| 398 | async submitAppVersionForReview( |
| 399 | appKey: string, |
| 400 | versionId: string, |
| 401 | input: SubmitAppVersionForReviewInput = {}, |
| 402 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 403 | return this.unwrapGeneratedResponse( |
| 404 | this.appPlatformClient.POST( |
| 405 | "/api/v1/apps/{appKey}/versions/{versionId}/submit", |
| 406 | { |
| 407 | params: { path: { appKey, versionId } }, |
| 408 | body: { |
| 409 | ...(input.storeListed !== undefined |
| 410 | ? { storeListed: input.storeListed } |
| 411 | : {}), |
| 412 | }, |
| 413 | }, |
| 414 | ), |
| 415 | ); |
| 416 | } |
| 417 |
| 418 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 419 | return this.unwrapGeneratedResponse( |
| 420 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 421 | params: { path: { workspaceSlug } }, |
| 422 | }), |
| 423 | ); |
| 424 | } |
| 425 |
| 426 | async installWorkspaceApp( |
| 427 | workspaceSlug: string, |
| 428 | input: InstallWorkspaceAppInput, |
| 429 | ): Promise<InstallWorkspaceAppResponse> { |
| 430 | const grantedPermissions = input.grantedPermissions |
| 431 | ? input.grantedPermissions.map((permission) => |
| 432 | appPermissionSchema.parse(permission), |
| 433 | ) |
| 434 | : undefined; |
| 435 |
| 436 | return this.unwrapGeneratedResponse( |
| 437 | this.appPlatformClient.POST("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 438 | params: { path: { workspaceSlug } }, |
| 439 | body: { |
| 440 | appId: input.appId, |
| 441 | ...(input.pinnedVersionId |
| 442 | ? { pinnedVersionId: input.pinnedVersionId } |
| 443 | : {}), |
| 444 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 445 | }, |
| 446 | }), |
| 447 | ); |
| 448 | } |
| 449 |
| 450 | async getWorkspaceAppPermissions( |
| 451 | workspaceSlug: string, |
| 452 | appKey: string, |
| 453 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 454 | return this.unwrapGeneratedResponse( |
| 455 | this.appPlatformClient.GET( |
| 456 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 457 | { |
| 458 | params: { path: { workspaceSlug, appKey } }, |
| 459 | }, |
| 460 | ), |
| 461 | ); |
| 462 | } |
| 463 |
| 464 | async getWorkspaceInstalledApp( |
| 465 | workspaceSlug: string, |
| 466 | appKey: string, |
| 467 | ): Promise<WorkspaceInstalledAppResponse> { |
| 468 | return this.unwrapGeneratedResponse( |
| 469 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", { |
| 470 | params: { path: { workspaceSlug, appKey } }, |
| 471 | }), |
| 472 | ); |
| 473 | } |
| 474 |
| 475 | async updateWorkspaceAppPermissions( |
| 476 | workspaceSlug: string, |
| 477 | appKey: string, |
| 478 | input: UpdateWorkspaceAppPermissionsInput, |
| 479 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 480 | return this.unwrapGeneratedResponse( |
| 481 | this.appPlatformClient.PUT( |
| 482 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 483 | { |
| 484 | params: { path: { workspaceSlug, appKey } }, |
| 485 | body: { |
| 486 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 487 | appPermissionSchema.parse(permission), |
| 488 | ), |
| 489 | }, |
| 490 | }, |
| 491 | ), |
| 492 | ); |
| 493 | } |
| 494 |
| 495 | async uninstallWorkspaceApp( |
| 496 | workspaceSlug: string, |
| 497 | appKey: string, |
| 498 | ): Promise<{ ok: true }> { |
| 499 | return this.unwrapGeneratedResponse( |
| 500 | this.appPlatformClient.DELETE( |
| 501 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", |
| 502 | { |
| 503 | params: { path: { workspaceSlug, appKey } }, |
| 504 | }, |
| 505 | ), |
| 506 | ); |
| 507 | } |
| 508 |
| 509 | async listWorkspaceAppSecrets( |
| 510 | workspaceSlug: string, |
| 511 | appKey: string, |
| 512 | ): Promise<WorkspaceAppSecretsResponse> { |
| 513 | return this.unwrapGeneratedResponse( |
| 514 | this.appPlatformClient.GET( |
| 515 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 516 | { |
| 517 | params: { path: { workspaceSlug, appKey } }, |
| 518 | }, |
| 519 | ), |
| 520 | ); |
| 521 | } |
| 522 |
| 523 | async createWorkspaceAppSecret( |
| 524 | workspaceSlug: string, |
| 525 | appKey: string, |
| 526 | input: { key: string; value: string }, |
| 527 | ): Promise<CreateWorkspaceAppSecretResponse> { |
| 528 | return this.unwrapGeneratedResponse( |
| 529 | this.appPlatformClient.POST( |
| 530 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 531 | { |
| 532 | params: { path: { workspaceSlug, appKey } }, |
| 533 | body: input, |
| 534 | }, |
| 535 | ), |
| 536 | ); |
| 537 | } |
| 538 |
| 539 | async deleteWorkspaceAppSecret( |
| 540 | workspaceSlug: string, |
| 541 | appKey: string, |
| 542 | secretId: string, |
| 543 | ): Promise<{ ok: true }> { |
| 544 | return this.unwrapGeneratedResponse( |
| 545 | this.appPlatformClient.DELETE( |
| 546 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets/{secretId}", |
| 547 | { |
| 548 | params: { path: { workspaceSlug, appKey, secretId } }, |
| 549 | }, |
| 550 | ), |
| 551 | ); |
| 552 | } |
| 553 |
| 554 | async listWorkspaceAppOauthConnections( |
| 555 | workspaceSlug: string, |
| 556 | appKey: string, |
| 557 | ): Promise<WorkspaceAppOauthConnectionsResponse> { |
| 558 | return this.unwrapGeneratedResponse( |
| 559 | this.appPlatformClient.GET( |
| 560 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 561 | { |
| 562 | params: { path: { workspaceSlug, appKey } }, |
| 563 | }, |
| 564 | ), |
| 565 | ); |
| 566 | } |
| 567 |
| 568 | async createWorkspaceAppOauthConnection( |
| 569 | workspaceSlug: string, |
| 570 | appKey: string, |
| 571 | input: { |
| 572 | providerKey: string; |
| 573 | externalAccountId?: string | null; |
| 574 | accessToken: string; |
| 575 | refreshToken?: string | null; |
| 576 | scopes: string[]; |
| 577 | tokenExpiresAt?: string | null; |
| 578 | metadata?: Record<string, unknown>; |
| 579 | }, |
| 580 | ): Promise<CreateWorkspaceAppOauthConnectionResponse> { |
| 581 | const body: { |
| 582 | providerKey: string; |
| 583 | accessToken: string; |
| 584 | scopes: string[]; |
| 585 | externalAccountId?: string; |
| 586 | refreshToken?: string; |
| 587 | tokenExpiresAt?: string; |
| 588 | metadata?: Record<string, unknown>; |
| 589 | } = { |
| 590 | providerKey: input.providerKey, |
| 591 | accessToken: input.accessToken, |
| 592 | scopes: input.scopes, |
| 593 | }; |
| 594 |
| 595 | if (input.externalAccountId) { |
| 596 | body.externalAccountId = input.externalAccountId; |
| 597 | } |
| 598 | if (input.refreshToken) { |
| 599 | body.refreshToken = input.refreshToken; |
| 600 | } |
| 601 | if (input.tokenExpiresAt) { |
| 602 | body.tokenExpiresAt = input.tokenExpiresAt; |
| 603 | } |
| 604 | if (input.metadata) { |
| 605 | body.metadata = input.metadata; |
| 606 | } |
| 607 |
| 608 | return this.unwrapGeneratedResponse( |
| 609 | this.appPlatformClient.POST( |
| 610 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 611 | { |
| 612 | params: { path: { workspaceSlug, appKey } }, |
| 613 | body, |
| 614 | }, |
| 615 | ), |
| 616 | ); |
| 617 | } |
| 618 |
| 619 | async deleteWorkspaceAppOauthConnection( |
| 620 | workspaceSlug: string, |
| 621 | appKey: string, |
| 622 | connectionId: string, |
| 623 | ): Promise<{ ok: true }> { |
| 624 | return this.unwrapGeneratedResponse( |
| 625 | this.appPlatformClient.DELETE( |
| 626 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections/{connectionId}", |
| 627 | { |
| 628 | params: { path: { workspaceSlug, appKey, connectionId } }, |
| 629 | }, |
| 630 | ), |
| 631 | ); |
| 632 | } |
| 633 |
| 634 | async listWorkspaceAppDeliveries( |
| 635 | workspaceSlug: string, |
| 636 | appKey: string, |
| 637 | ): Promise<WorkspaceAppDeliveriesResponse> { |
| 638 | return this.unwrapGeneratedResponse( |
| 639 | this.appPlatformClient.GET( |
| 640 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 641 | { |
| 642 | params: { path: { workspaceSlug, appKey } }, |
| 643 | }, |
| 644 | ), |
| 645 | ); |
| 646 | } |
| 647 |
| 648 | async sendWorkspaceAppTestDelivery( |
| 649 | workspaceSlug: string, |
| 650 | appKey: string, |
| 651 | input: { |
| 652 | eventType: string; |
| 653 | targetUrl?: string; |
| 654 | payload: Record<string, unknown>; |
| 655 | }, |
| 656 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 657 | return this.unwrapGeneratedResponse( |
| 658 | this.appPlatformClient.POST( |
| 659 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 660 | { |
| 661 | params: { path: { workspaceSlug, appKey } }, |
| 662 | body: input, |
| 663 | }, |
| 664 | ), |
| 665 | ); |
| 666 | } |
| 667 |
| 668 | async replayWorkspaceAppDelivery( |
| 669 | workspaceSlug: string, |
| 670 | appKey: string, |
| 671 | deliveryId: string, |
| 672 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 673 | return this.unwrapGeneratedResponse( |
| 674 | this.appPlatformClient.POST( |
| 675 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries/{deliveryId}/replay", |
| 676 | { |
| 677 | params: { path: { workspaceSlug, appKey, deliveryId } }, |
| 678 | }, |
| 679 | ), |
| 680 | ); |
| 681 | } |
| 682 |
| 683 | async emitAppAmbientEvents( |
| 684 | workspaceSlug: string, |
| 685 | appKey: string, |
| 686 | input: EmitAmbientEventsInputInput, |
| 687 | ): Promise<EmitAppAmbientEventsResponse> { |
| 688 | const parsed = emitAmbientEventsInputSchema.parse(input); |
| 689 | const body = { |
| 690 | events: parsed.events.map((event) => ({ |
| 691 | sourceKind: event.sourceKind, |
| 692 | action: event.action, |
| 693 | entityType: event.entityType, |
| 694 | entityId: event.entityId, |
| 695 | dedupeKey: event.dedupeKey, |
| 696 | payload: event.payload, |
| 697 | metadata: event.metadata, |
| 698 | schemaVersion: event.schemaVersion, |
| 699 | ...(event.actorUserId ? { actorUserId: event.actorUserId } : {}), |
| 700 | ...(event.containerType ? { containerType: event.containerType } : {}), |
| 701 | ...(event.containerId ? { containerId: event.containerId } : {}), |
| 702 | ...(event.occurredAt |
| 703 | ? { occurredAt: event.occurredAt.toISOString() } |
| 704 | : {}), |
| 705 | })), |
| 706 | }; |
| 707 |
| 708 | return this.unwrapGeneratedResponse( |
| 709 | this.appPlatformClient.POST( |
| 710 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/ambient/events", |
| 711 | { |
| 712 | params: { path: { workspaceSlug, appKey } }, |
| 713 | body, |
| 714 | }, |
| 715 | ), |
| 716 | ); |
| 717 | } |
| 718 |
| 719 | async createAppNotification( |
| 720 | workspaceSlug: string, |
| 721 | appKey: string, |
| 722 | input: CreateNotificationInputInput, |
| 723 | ): Promise<OkResponse> { |
| 724 | const parsed = createNotificationInputSchema.parse(input); |
| 725 | const body = { |
| 726 | channelKey: parsed.channelKey, |
| 727 | typeKey: parsed.typeKey, |
| 728 | recipients: parsed.recipients.map((recipient) => ({ |
| 729 | userId: recipient.userId, |
| 730 | })), |
| 731 | title: parsed.title, |
| 732 | priority: parsed.priority, |
| 733 | metadata: parsed.metadata, |
| 734 | ...(parsed.body ? { body: parsed.body } : {}), |
| 735 | ...(parsed.targetUrl ? { targetUrl: parsed.targetUrl } : {}), |
| 736 | ...(parsed.icon ? { icon: parsed.icon } : {}), |
| 737 | ...(parsed.dedupeKey ? { dedupeKey: parsed.dedupeKey } : {}), |
| 738 | }; |
| 739 |
| 740 | return this.unwrapGeneratedResponse( |
| 741 | this.appPlatformClient.POST( |
| 742 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/notifications", |
| 743 | { |
| 744 | params: { path: { workspaceSlug, appKey } }, |
| 745 | body, |
| 746 | }, |
| 747 | ), |
| 748 | ); |
| 749 | } |
| 750 |
| 751 | async upsertAppIndexDocuments( |
| 752 | workspaceSlug: string, |
| 753 | appKey: string, |
| 754 | input: BatchUpsertAppIndexDocumentsInput, |
| 755 | ): Promise<UpsertAppIndexDocumentsResponse> { |
| 756 | const parsed = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 757 | const body = { |
| 758 | documents: parsed.documents.map((document) => ({ |
| 759 | documentKey: document.documentKey, |
| 760 | documentType: document.documentType, |
| 761 | title: document.title, |
| 762 | body: document.body, |
| 763 | metadata: document.metadata, |
| 764 | visibilityMode: document.visibilityMode, |
| 765 | visibility: document.visibility.map((grant) => ({ |
| 766 | subjectType: grant.subjectType, |
| 767 | subjectId: grant.subjectId, |
| 768 | })), |
| 769 | ...(document.fileId ? { fileId: document.fileId } : {}), |
| 770 | ...(document.url ? { url: document.url } : {}), |
| 771 | })), |
| 772 | }; |
| 773 |
| 774 | return this.unwrapGeneratedResponse( |
| 775 | this.appPlatformClient.POST( |
| 776 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 777 | { |
| 778 | params: { path: { workspaceSlug, appKey } }, |
| 779 | body, |
| 780 | }, |
| 781 | ), |
| 782 | ); |
| 783 | } |
| 784 |
| 785 | async deleteAppIndexDocuments( |
| 786 | workspaceSlug: string, |
| 787 | appKey: string, |
| 788 | input: BatchDeleteAppIndexDocumentsInput, |
| 789 | ): Promise<DeleteAppIndexDocumentsResponse> { |
| 790 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 791 |
| 792 | return this.unwrapGeneratedResponse( |
| 793 | this.appPlatformClient.DELETE( |
| 794 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 795 | { |
| 796 | params: { path: { workspaceSlug, appKey } }, |
| 797 | body, |
| 798 | }, |
| 799 | ), |
| 800 | ); |
| 801 | } |
| 802 |
| 803 | async listAppFiles( |
| 804 | workspaceSlug: string, |
| 805 | appKey: string, |
| 806 | query?: { |
| 807 | driveId?: string; |
| 808 | typeKey?: string; |
| 809 | limit?: number; |
| 810 | }, |
| 811 | ): Promise<AppFilesListResponse> { |
| 812 | return this.unwrapGeneratedResponse( |
| 813 | this.appPlatformClient.GET( |
| 814 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 815 | { |
| 816 | params: { |
| 817 | path: { workspaceSlug, appKey }, |
| 818 | ...(query |
| 819 | ? { |
| 820 | query: { |
| 821 | ...(query.driveId ? { driveId: query.driveId } : {}), |
| 822 | ...(query.typeKey ? { typeKey: query.typeKey } : {}), |
| 823 | ...(query.limit !== undefined ? { limit: query.limit } : {}), |
| 824 | }, |
| 825 | } |
| 826 | : {}), |
| 827 | }, |
| 828 | }, |
| 829 | ), |
| 830 | ); |
| 831 | } |
| 832 |
| 833 | async createAppFile( |
| 834 | workspaceSlug: string, |
| 835 | appKey: string, |
| 836 | input: CreateAppFileInput, |
| 837 | ): Promise<CreateAppFileResponse> { |
| 838 | return this.unwrapGeneratedResponse( |
| 839 | this.appPlatformClient.POST( |
| 840 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files", |
| 841 | { |
| 842 | params: { path: { workspaceSlug, appKey } }, |
| 843 | body: { |
| 844 | name: input.name, |
| 845 | ...(input.typeKey ? { typeKey: input.typeKey } : {}), |
| 846 | ...(input.driveId ? { driveId: input.driveId } : {}), |
| 847 | ...(input.parentId !== undefined ? { parentId: input.parentId } : {}), |
| 848 | ...(input.document ? { document: input.document } : {}), |
| 849 | }, |
| 850 | }, |
| 851 | ), |
| 852 | ); |
| 853 | } |
| 854 |
| 855 | async executeAppAction( |
| 856 | workspaceSlug: string, |
| 857 | appKey: string, |
| 858 | input: ExecuteAppActionInput, |
| 859 | ): Promise<ExecuteAppActionResponse> { |
| 860 | return this.unwrapGeneratedResponse( |
| 861 | this.appPlatformClient.POST( |
| 862 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/actions", |
| 863 | { |
| 864 | params: { path: { workspaceSlug, appKey } }, |
| 865 | body: { |
| 866 | action: { |
| 867 | type: input.action.type, |
| 868 | ...(input.action.payload ? { payload: input.action.payload } : {}), |
| 869 | }, |
| 870 | ...(input.fileId !== undefined ? { fileId: input.fileId } : {}), |
| 871 | }, |
| 872 | }, |
| 873 | ), |
| 874 | ); |
| 875 | } |
| 876 |
| 877 | async getAppFileDocument( |
| 878 | workspaceSlug: string, |
| 879 | appKey: string, |
| 880 | fileId: string, |
| 881 | ): Promise<AppFileDocumentResponse> { |
| 882 | return this.unwrapGeneratedResponse( |
| 883 | this.appPlatformClient.GET( |
| 884 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 885 | { |
| 886 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 887 | }, |
| 888 | ), |
| 889 | ); |
| 890 | } |
| 891 |
| 892 | async updateAppFileDocument( |
| 893 | workspaceSlug: string, |
| 894 | appKey: string, |
| 895 | fileId: string, |
| 896 | document: Record<string, unknown>, |
| 897 | ): Promise<AppFileDocumentUpdateResponse> { |
| 898 | return this.unwrapGeneratedResponse( |
| 899 | this.appPlatformClient.PATCH( |
| 900 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 901 | { |
| 902 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 903 | body: { document }, |
| 904 | }, |
| 905 | ), |
| 906 | ); |
| 907 | } |
| 908 |
| 909 | async listTabModels(): Promise<TabModelsResponse> { |
| 910 | return this.unwrapGeneratedResponse( |
| 911 | this.appPlatformClient.GET("/api/v1/tab/models"), |
| 912 | ); |
| 913 | } |
| 914 |
| 915 | async createTabChatCompletion(input: { |
| 916 | model: string; |
| 917 | stream?: boolean; |
| 918 | tab_context: Record<string, unknown>; |
| 919 | messages?: Array<{ role?: string; content?: string }>; |
| 920 | }): Promise<TabChatCompletionResponse> { |
| 921 | return this.unwrapGeneratedResponse( |
| 922 | this.appPlatformClient.POST("/api/v1/tab/chat/completions", { |
| 923 | body: input, |
| 924 | }), |
| 925 | ); |
| 926 | } |
| 927 |
| 928 | async exchangeOAuthCode(input: { |
| 929 | clientId: string; |
| 930 | clientSecret: string; |
| 931 | code: string; |
| 932 | redirectUri: string; |
| 933 | codeVerifier?: string; |
| 934 | }): Promise<OAuthTokenResponse> { |
| 935 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 936 | method: "POST", |
| 937 | headers: { |
| 938 | "Content-Type": "application/json", |
| 939 | }, |
| 940 | body: JSON.stringify({ |
| 941 | grant_type: "authorization_code", |
| 942 | client_id: input.clientId, |
| 943 | client_secret: input.clientSecret, |
| 944 | code: input.code, |
| 945 | redirect_uri: input.redirectUri, |
| 946 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 947 | }), |
| 948 | }); |
| 949 |
| 950 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 951 | } |
| 952 |
| 953 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 954 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 955 | method: "GET", |
| 956 | headers: { |
| 957 | Authorization: `Bearer ${accessToken}`, |
| 958 | }, |
| 959 | }); |
| 960 |
| 961 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 962 | } |
| 963 | } |
| 964 |