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