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 AppFileDocumentResponse = UnwrapData<"AppFileDocumentResponse">; |
| 132 | export type AppFileDocumentUpdateResponse = UnwrapData< |
| 133 | "AppFileDocumentUpdateResponse" |
| 134 | >; |
| 135 | export type OkResponse = UnwrapData<"OkResponse">; |
| 136 | export type TabModelRecord = Schemas["TabModelRecord"]; |
| 137 | export type TabModelsResponse = UnwrapData<"TabModelsResponse">; |
| 138 | export type TabChatCompletionResponse = UnwrapData< |
| 139 | "TabChatCompletionResponseEnvelope" |
| 140 | >; |
| 141 | export type UpsertAppIndexDocumentsResponse = UnwrapData< |
| 142 | "UpsertAppIndexDocumentsResponse" |
| 143 | >; |
| 144 | export type DeleteAppIndexDocumentsResponse = UnwrapData< |
| 145 | "DeleteAppIndexDocumentsResponse" |
| 146 | >; |
| 147 | export type EmitAppAmbientEventsResponse = UnwrapData< |
| 148 | "EmitAppAmbientEventsResponse" |
| 149 | >; |
| 150 |
| 151 | export interface OAuthTokenResponse { |
| 152 | access_token: string; |
| 153 | token_type: "Bearer"; |
| 154 | expires_in: number; |
| 155 | refresh_token: string; |
| 156 | scope: string; |
| 157 | } |
| 158 |
| 159 | export interface OAuthUserInfoResponse { |
| 160 | sub: string; |
| 161 | email: string; |
| 162 | email_verified: boolean; |
| 163 | name: string; |
| 164 | given_name: string | null; |
| 165 | family_name: string | null; |
| 166 | picture: string | null; |
| 167 | preferred_username: string | null; |
| 168 | scope: string; |
| 169 | } |
| 170 |
| 171 | export interface UpdateWorkspaceAppPermissionsInput { |
| 172 | grantedPermissions: AppPermission[]; |
| 173 | } |
| 174 |
| 175 | function joinUrl(baseUrl: string, path: string): string { |
| 176 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 177 | } |
| 178 |
| 179 | function toRegisterAppInput(payload: ColineAppRegistrationPayload) { |
| 180 | const notificationChannels = payload.manifest.notificationChannels.map((channel) => ({ |
| 181 | key: channel.key, |
| 182 | name: channel.name, |
| 183 | defaultDeliveries: [...channel.defaultDeliveries], |
| 184 | defaultPriority: channel.defaultPriority, |
| 185 | ...(channel.description ? { description: channel.description } : {}), |
| 186 | })); |
| 187 |
| 188 | const manifest = { |
| 189 | key: payload.manifest.key, |
| 190 | name: payload.manifest.name, |
| 191 | permissions: payload.manifest.permissions, |
| 192 | notificationChannels, |
| 193 | hosting: { |
| 194 | mode: payload.manifest.hosting.mode, |
| 195 | baseUrl: payload.manifest.hosting.baseUrl, |
| 196 | }, |
| 197 | ...(payload.manifest.description |
| 198 | ? { description: payload.manifest.description } |
| 199 | : {}), |
| 200 | ...(payload.manifest.iconUrl ? { iconUrl: payload.manifest.iconUrl } : {}), |
| 201 | ...(payload.manifest.kairo |
| 202 | ? { |
| 203 | kairo: JSON.parse(JSON.stringify(payload.manifest.kairo)) as Record< |
| 204 | string, |
| 205 | unknown |
| 206 | >, |
| 207 | } |
| 208 | : {}), |
| 209 | }; |
| 210 |
| 211 | const fileTypes = payload.fileTypes.map((fileType) => ({ |
| 212 | typeKey: fileType.typeKey, |
| 213 | name: fileType.name, |
| 214 | storage: fileType.storage, |
| 215 | indexable: fileType.indexable, |
| 216 | homeRenderMode: fileType.homeRenderMode, |
| 217 | ...(fileType.description ? { description: fileType.description } : {}), |
| 218 | })); |
| 219 |
| 220 | return { |
| 221 | version: payload.version, |
| 222 | manifest, |
| 223 | fileTypes, |
| 224 | }; |
| 225 | } |
| 226 |
| 227 | export class ColineApiClient { |
| 228 | private readonly baseUrl: string; |
| 229 | private readonly apiKey: string | undefined; |
| 230 | private readonly fetchImpl: typeof fetch; |
| 231 | private readonly defaultHeaders: HeadersInit | undefined; |
| 232 | private readonly appPlatformClient: AppPlatformClient; |
| 233 |
| 234 | constructor(options: ColineApiClientOptions) { |
| 235 | this.baseUrl = options.baseUrl; |
| 236 | this.apiKey = options.apiKey; |
| 237 | this.fetchImpl = options.fetch ?? fetch; |
| 238 | this.defaultHeaders = options.headers; |
| 239 | this.appPlatformClient = createAppPlatformClient(options); |
| 240 | } |
| 241 |
| 242 | private async unwrapGeneratedResponse<T>( |
| 243 | promise: Promise<{ |
| 244 | data?: { data: T }; |
| 245 | error?: ApiErrorPayload; |
| 246 | response: Response; |
| 247 | }>, |
| 248 | ): Promise<T> { |
| 249 | const { data, error, response } = await promise; |
| 250 |
| 251 | if (error) { |
| 252 | throw new ColineApiError({ |
| 253 | message: error.error?.message ?? `Request failed (${response.status}).`, |
| 254 | type: "api", |
| 255 | status: response.status, |
| 256 | code: error.error?.code ?? null, |
| 257 | }); |
| 258 | } |
| 259 |
| 260 | if (data === undefined) { |
| 261 | throw new ColineApiError({ |
| 262 | message: "Invalid API response.", |
| 263 | type: "api", |
| 264 | status: response.status, |
| 265 | }); |
| 266 | } |
| 267 |
| 268 | return data.data; |
| 269 | } |
| 270 |
| 271 | withApiKey(apiKey: string): ColineApiClient { |
| 272 | const options: ColineApiClientOptions = { |
| 273 | baseUrl: this.baseUrl, |
| 274 | apiKey, |
| 275 | fetch: this.fetchImpl, |
| 276 | }; |
| 277 |
| 278 | if (this.defaultHeaders !== undefined) { |
| 279 | options.headers = this.defaultHeaders; |
| 280 | } |
| 281 |
| 282 | return new ColineApiClient(options); |
| 283 | } |
| 284 |
| 285 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 286 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 287 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 288 |
| 289 | const options: ColineApiClientOptions = { |
| 290 | baseUrl: this.baseUrl, |
| 291 | fetch: this.fetchImpl, |
| 292 | headers: mergedHeaders, |
| 293 | }; |
| 294 |
| 295 | if (this.apiKey !== undefined) { |
| 296 | options.apiKey = this.apiKey; |
| 297 | } |
| 298 |
| 299 | return new ColineApiClient(options); |
| 300 | } |
| 301 |
| 302 | buildLoginWithColineAuthorizeUrl(input: { |
| 303 | clientId: string; |
| 304 | redirectUri: string; |
| 305 | scope?: string; |
| 306 | state?: string; |
| 307 | codeChallenge?: string; |
| 308 | codeChallengeMethod?: "S256" | "plain"; |
| 309 | }): string { |
| 310 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 311 | url.searchParams.set("response_type", "code"); |
| 312 | url.searchParams.set("client_id", input.clientId); |
| 313 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 314 | if (input.scope) { |
| 315 | url.searchParams.set("scope", input.scope); |
| 316 | } |
| 317 | if (input.state) { |
| 318 | url.searchParams.set("state", input.state); |
| 319 | } |
| 320 | if (input.codeChallenge) { |
| 321 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 322 | } |
| 323 | if (input.codeChallengeMethod) { |
| 324 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 325 | } |
| 326 | return url.toString(); |
| 327 | } |
| 328 |
| 329 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 330 | return this.unwrapGeneratedResponse( |
| 331 | this.appPlatformClient.GET("/api/v1/apps"), |
| 332 | ); |
| 333 | } |
| 334 |
| 335 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 336 | return this.unwrapGeneratedResponse( |
| 337 | this.appPlatformClient.GET("/api/v1/apps/{appKey}", { |
| 338 | params: { path: { appKey } }, |
| 339 | }), |
| 340 | ); |
| 341 | } |
| 342 |
| 343 | async listAppVersions( |
| 344 | appKey: string, |
| 345 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 346 | return this.unwrapGeneratedResponse( |
| 347 | this.appPlatformClient.GET("/api/v1/apps/{appKey}/versions", { |
| 348 | params: { path: { appKey } }, |
| 349 | }), |
| 350 | ); |
| 351 | } |
| 352 |
| 353 | async registerApp( |
| 354 | payload: ColineAppRegistrationPayload, |
| 355 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 356 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 357 |
| 358 | return this.unwrapGeneratedResponse( |
| 359 | this.appPlatformClient.POST("/api/v1/apps", { |
| 360 | body, |
| 361 | }), |
| 362 | ); |
| 363 | } |
| 364 |
| 365 | async createAppVersion( |
| 366 | appKey: string, |
| 367 | payload: ColineAppRegistrationPayload, |
| 368 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 369 | const body = toRegisterAppInput(colineAppRegistrationPayloadSchema.parse(payload)); |
| 370 |
| 371 | return this.unwrapGeneratedResponse( |
| 372 | this.appPlatformClient.POST("/api/v1/apps/{appKey}/versions", { |
| 373 | params: { path: { appKey } }, |
| 374 | body, |
| 375 | }), |
| 376 | ); |
| 377 | } |
| 378 |
| 379 | async submitAppVersionForReview( |
| 380 | appKey: string, |
| 381 | versionId: string, |
| 382 | input: SubmitAppVersionForReviewInput = {}, |
| 383 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 384 | return this.unwrapGeneratedResponse( |
| 385 | this.appPlatformClient.POST( |
| 386 | "/api/v1/apps/{appKey}/versions/{versionId}/submit", |
| 387 | { |
| 388 | params: { path: { appKey, versionId } }, |
| 389 | body: { |
| 390 | ...(input.storeListed !== undefined |
| 391 | ? { storeListed: input.storeListed } |
| 392 | : {}), |
| 393 | }, |
| 394 | }, |
| 395 | ), |
| 396 | ); |
| 397 | } |
| 398 |
| 399 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 400 | return this.unwrapGeneratedResponse( |
| 401 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 402 | params: { path: { workspaceSlug } }, |
| 403 | }), |
| 404 | ); |
| 405 | } |
| 406 |
| 407 | async installWorkspaceApp( |
| 408 | workspaceSlug: string, |
| 409 | input: InstallWorkspaceAppInput, |
| 410 | ): Promise<InstallWorkspaceAppResponse> { |
| 411 | const grantedPermissions = input.grantedPermissions |
| 412 | ? input.grantedPermissions.map((permission) => |
| 413 | appPermissionSchema.parse(permission), |
| 414 | ) |
| 415 | : undefined; |
| 416 |
| 417 | return this.unwrapGeneratedResponse( |
| 418 | this.appPlatformClient.POST("/api/v1/workspaces/{workspaceSlug}/apps", { |
| 419 | params: { path: { workspaceSlug } }, |
| 420 | body: { |
| 421 | appId: input.appId, |
| 422 | ...(input.pinnedVersionId |
| 423 | ? { pinnedVersionId: input.pinnedVersionId } |
| 424 | : {}), |
| 425 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 426 | }, |
| 427 | }), |
| 428 | ); |
| 429 | } |
| 430 |
| 431 | async getWorkspaceAppPermissions( |
| 432 | workspaceSlug: string, |
| 433 | appKey: string, |
| 434 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 435 | return this.unwrapGeneratedResponse( |
| 436 | this.appPlatformClient.GET( |
| 437 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 438 | { |
| 439 | params: { path: { workspaceSlug, appKey } }, |
| 440 | }, |
| 441 | ), |
| 442 | ); |
| 443 | } |
| 444 |
| 445 | async getWorkspaceInstalledApp( |
| 446 | workspaceSlug: string, |
| 447 | appKey: string, |
| 448 | ): Promise<WorkspaceInstalledAppResponse> { |
| 449 | return this.unwrapGeneratedResponse( |
| 450 | this.appPlatformClient.GET("/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", { |
| 451 | params: { path: { workspaceSlug, appKey } }, |
| 452 | }), |
| 453 | ); |
| 454 | } |
| 455 |
| 456 | async updateWorkspaceAppPermissions( |
| 457 | workspaceSlug: string, |
| 458 | appKey: string, |
| 459 | input: UpdateWorkspaceAppPermissionsInput, |
| 460 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 461 | return this.unwrapGeneratedResponse( |
| 462 | this.appPlatformClient.PUT( |
| 463 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/permissions", |
| 464 | { |
| 465 | params: { path: { workspaceSlug, appKey } }, |
| 466 | body: { |
| 467 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 468 | appPermissionSchema.parse(permission), |
| 469 | ), |
| 470 | }, |
| 471 | }, |
| 472 | ), |
| 473 | ); |
| 474 | } |
| 475 |
| 476 | async uninstallWorkspaceApp( |
| 477 | workspaceSlug: string, |
| 478 | appKey: string, |
| 479 | ): Promise<{ ok: true }> { |
| 480 | return this.unwrapGeneratedResponse( |
| 481 | this.appPlatformClient.DELETE( |
| 482 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}", |
| 483 | { |
| 484 | params: { path: { workspaceSlug, appKey } }, |
| 485 | }, |
| 486 | ), |
| 487 | ); |
| 488 | } |
| 489 |
| 490 | async listWorkspaceAppSecrets( |
| 491 | workspaceSlug: string, |
| 492 | appKey: string, |
| 493 | ): Promise<WorkspaceAppSecretsResponse> { |
| 494 | return this.unwrapGeneratedResponse( |
| 495 | this.appPlatformClient.GET( |
| 496 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 497 | { |
| 498 | params: { path: { workspaceSlug, appKey } }, |
| 499 | }, |
| 500 | ), |
| 501 | ); |
| 502 | } |
| 503 |
| 504 | async createWorkspaceAppSecret( |
| 505 | workspaceSlug: string, |
| 506 | appKey: string, |
| 507 | input: { key: string; value: string }, |
| 508 | ): Promise<CreateWorkspaceAppSecretResponse> { |
| 509 | return this.unwrapGeneratedResponse( |
| 510 | this.appPlatformClient.POST( |
| 511 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets", |
| 512 | { |
| 513 | params: { path: { workspaceSlug, appKey } }, |
| 514 | body: input, |
| 515 | }, |
| 516 | ), |
| 517 | ); |
| 518 | } |
| 519 |
| 520 | async deleteWorkspaceAppSecret( |
| 521 | workspaceSlug: string, |
| 522 | appKey: string, |
| 523 | secretId: string, |
| 524 | ): Promise<{ ok: true }> { |
| 525 | return this.unwrapGeneratedResponse( |
| 526 | this.appPlatformClient.DELETE( |
| 527 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/secrets/{secretId}", |
| 528 | { |
| 529 | params: { path: { workspaceSlug, appKey, secretId } }, |
| 530 | }, |
| 531 | ), |
| 532 | ); |
| 533 | } |
| 534 |
| 535 | async listWorkspaceAppOauthConnections( |
| 536 | workspaceSlug: string, |
| 537 | appKey: string, |
| 538 | ): Promise<WorkspaceAppOauthConnectionsResponse> { |
| 539 | return this.unwrapGeneratedResponse( |
| 540 | this.appPlatformClient.GET( |
| 541 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 542 | { |
| 543 | params: { path: { workspaceSlug, appKey } }, |
| 544 | }, |
| 545 | ), |
| 546 | ); |
| 547 | } |
| 548 |
| 549 | async createWorkspaceAppOauthConnection( |
| 550 | workspaceSlug: string, |
| 551 | appKey: string, |
| 552 | input: { |
| 553 | providerKey: string; |
| 554 | externalAccountId?: string | null; |
| 555 | accessToken: string; |
| 556 | refreshToken?: string | null; |
| 557 | scopes: string[]; |
| 558 | tokenExpiresAt?: string | null; |
| 559 | metadata?: Record<string, unknown>; |
| 560 | }, |
| 561 | ): Promise<CreateWorkspaceAppOauthConnectionResponse> { |
| 562 | const body: { |
| 563 | providerKey: string; |
| 564 | accessToken: string; |
| 565 | scopes: string[]; |
| 566 | externalAccountId?: string; |
| 567 | refreshToken?: string; |
| 568 | tokenExpiresAt?: string; |
| 569 | metadata?: Record<string, unknown>; |
| 570 | } = { |
| 571 | providerKey: input.providerKey, |
| 572 | accessToken: input.accessToken, |
| 573 | scopes: input.scopes, |
| 574 | }; |
| 575 |
| 576 | if (input.externalAccountId) { |
| 577 | body.externalAccountId = input.externalAccountId; |
| 578 | } |
| 579 | if (input.refreshToken) { |
| 580 | body.refreshToken = input.refreshToken; |
| 581 | } |
| 582 | if (input.tokenExpiresAt) { |
| 583 | body.tokenExpiresAt = input.tokenExpiresAt; |
| 584 | } |
| 585 | if (input.metadata) { |
| 586 | body.metadata = input.metadata; |
| 587 | } |
| 588 |
| 589 | return this.unwrapGeneratedResponse( |
| 590 | this.appPlatformClient.POST( |
| 591 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections", |
| 592 | { |
| 593 | params: { path: { workspaceSlug, appKey } }, |
| 594 | body, |
| 595 | }, |
| 596 | ), |
| 597 | ); |
| 598 | } |
| 599 |
| 600 | async deleteWorkspaceAppOauthConnection( |
| 601 | workspaceSlug: string, |
| 602 | appKey: string, |
| 603 | connectionId: string, |
| 604 | ): Promise<{ ok: true }> { |
| 605 | return this.unwrapGeneratedResponse( |
| 606 | this.appPlatformClient.DELETE( |
| 607 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/oauth-connections/{connectionId}", |
| 608 | { |
| 609 | params: { path: { workspaceSlug, appKey, connectionId } }, |
| 610 | }, |
| 611 | ), |
| 612 | ); |
| 613 | } |
| 614 |
| 615 | async listWorkspaceAppDeliveries( |
| 616 | workspaceSlug: string, |
| 617 | appKey: string, |
| 618 | ): Promise<WorkspaceAppDeliveriesResponse> { |
| 619 | return this.unwrapGeneratedResponse( |
| 620 | this.appPlatformClient.GET( |
| 621 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 622 | { |
| 623 | params: { path: { workspaceSlug, appKey } }, |
| 624 | }, |
| 625 | ), |
| 626 | ); |
| 627 | } |
| 628 |
| 629 | async sendWorkspaceAppTestDelivery( |
| 630 | workspaceSlug: string, |
| 631 | appKey: string, |
| 632 | input: { |
| 633 | eventType: string; |
| 634 | targetUrl?: string; |
| 635 | payload: Record<string, unknown>; |
| 636 | }, |
| 637 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 638 | return this.unwrapGeneratedResponse( |
| 639 | this.appPlatformClient.POST( |
| 640 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries", |
| 641 | { |
| 642 | params: { path: { workspaceSlug, appKey } }, |
| 643 | body: input, |
| 644 | }, |
| 645 | ), |
| 646 | ); |
| 647 | } |
| 648 |
| 649 | async replayWorkspaceAppDelivery( |
| 650 | workspaceSlug: string, |
| 651 | appKey: string, |
| 652 | deliveryId: string, |
| 653 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 654 | return this.unwrapGeneratedResponse( |
| 655 | this.appPlatformClient.POST( |
| 656 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/deliveries/{deliveryId}/replay", |
| 657 | { |
| 658 | params: { path: { workspaceSlug, appKey, deliveryId } }, |
| 659 | }, |
| 660 | ), |
| 661 | ); |
| 662 | } |
| 663 |
| 664 | async emitAppAmbientEvents( |
| 665 | workspaceSlug: string, |
| 666 | appKey: string, |
| 667 | input: EmitAmbientEventsInputInput, |
| 668 | ): Promise<EmitAppAmbientEventsResponse> { |
| 669 | const parsed = emitAmbientEventsInputSchema.parse(input); |
| 670 | const body = { |
| 671 | events: parsed.events.map((event) => ({ |
| 672 | sourceKind: event.sourceKind, |
| 673 | action: event.action, |
| 674 | entityType: event.entityType, |
| 675 | entityId: event.entityId, |
| 676 | dedupeKey: event.dedupeKey, |
| 677 | payload: event.payload, |
| 678 | metadata: event.metadata, |
| 679 | schemaVersion: event.schemaVersion, |
| 680 | ...(event.actorUserId ? { actorUserId: event.actorUserId } : {}), |
| 681 | ...(event.containerType ? { containerType: event.containerType } : {}), |
| 682 | ...(event.containerId ? { containerId: event.containerId } : {}), |
| 683 | ...(event.occurredAt |
| 684 | ? { occurredAt: event.occurredAt.toISOString() } |
| 685 | : {}), |
| 686 | })), |
| 687 | }; |
| 688 |
| 689 | return this.unwrapGeneratedResponse( |
| 690 | this.appPlatformClient.POST( |
| 691 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/ambient/events", |
| 692 | { |
| 693 | params: { path: { workspaceSlug, appKey } }, |
| 694 | body, |
| 695 | }, |
| 696 | ), |
| 697 | ); |
| 698 | } |
| 699 |
| 700 | async createAppNotification( |
| 701 | workspaceSlug: string, |
| 702 | appKey: string, |
| 703 | input: CreateNotificationInputInput, |
| 704 | ): Promise<OkResponse> { |
| 705 | const parsed = createNotificationInputSchema.parse(input); |
| 706 | const body = { |
| 707 | channelKey: parsed.channelKey, |
| 708 | typeKey: parsed.typeKey, |
| 709 | recipients: parsed.recipients.map((recipient) => ({ |
| 710 | userId: recipient.userId, |
| 711 | })), |
| 712 | title: parsed.title, |
| 713 | priority: parsed.priority, |
| 714 | metadata: parsed.metadata, |
| 715 | ...(parsed.body ? { body: parsed.body } : {}), |
| 716 | ...(parsed.targetUrl ? { targetUrl: parsed.targetUrl } : {}), |
| 717 | ...(parsed.icon ? { icon: parsed.icon } : {}), |
| 718 | ...(parsed.dedupeKey ? { dedupeKey: parsed.dedupeKey } : {}), |
| 719 | }; |
| 720 |
| 721 | return this.unwrapGeneratedResponse( |
| 722 | this.appPlatformClient.POST( |
| 723 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/notifications", |
| 724 | { |
| 725 | params: { path: { workspaceSlug, appKey } }, |
| 726 | body, |
| 727 | }, |
| 728 | ), |
| 729 | ); |
| 730 | } |
| 731 |
| 732 | async upsertAppIndexDocuments( |
| 733 | workspaceSlug: string, |
| 734 | appKey: string, |
| 735 | input: BatchUpsertAppIndexDocumentsInput, |
| 736 | ): Promise<UpsertAppIndexDocumentsResponse> { |
| 737 | const parsed = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 738 | const body = { |
| 739 | documents: parsed.documents.map((document) => ({ |
| 740 | documentKey: document.documentKey, |
| 741 | documentType: document.documentType, |
| 742 | title: document.title, |
| 743 | body: document.body, |
| 744 | metadata: document.metadata, |
| 745 | visibilityMode: document.visibilityMode, |
| 746 | visibility: document.visibility.map((grant) => ({ |
| 747 | subjectType: grant.subjectType, |
| 748 | subjectId: grant.subjectId, |
| 749 | })), |
| 750 | ...(document.fileId ? { fileId: document.fileId } : {}), |
| 751 | ...(document.url ? { url: document.url } : {}), |
| 752 | })), |
| 753 | }; |
| 754 |
| 755 | return this.unwrapGeneratedResponse( |
| 756 | this.appPlatformClient.POST( |
| 757 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 758 | { |
| 759 | params: { path: { workspaceSlug, appKey } }, |
| 760 | body, |
| 761 | }, |
| 762 | ), |
| 763 | ); |
| 764 | } |
| 765 |
| 766 | async deleteAppIndexDocuments( |
| 767 | workspaceSlug: string, |
| 768 | appKey: string, |
| 769 | input: BatchDeleteAppIndexDocumentsInput, |
| 770 | ): Promise<DeleteAppIndexDocumentsResponse> { |
| 771 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 772 |
| 773 | return this.unwrapGeneratedResponse( |
| 774 | this.appPlatformClient.DELETE( |
| 775 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/index-documents", |
| 776 | { |
| 777 | params: { path: { workspaceSlug, appKey } }, |
| 778 | body, |
| 779 | }, |
| 780 | ), |
| 781 | ); |
| 782 | } |
| 783 |
| 784 | async getAppFileDocument( |
| 785 | workspaceSlug: string, |
| 786 | appKey: string, |
| 787 | fileId: string, |
| 788 | ): Promise<AppFileDocumentResponse> { |
| 789 | return this.unwrapGeneratedResponse( |
| 790 | this.appPlatformClient.GET( |
| 791 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 792 | { |
| 793 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 794 | }, |
| 795 | ), |
| 796 | ); |
| 797 | } |
| 798 |
| 799 | async updateAppFileDocument( |
| 800 | workspaceSlug: string, |
| 801 | appKey: string, |
| 802 | fileId: string, |
| 803 | document: Record<string, unknown>, |
| 804 | ): Promise<AppFileDocumentUpdateResponse> { |
| 805 | return this.unwrapGeneratedResponse( |
| 806 | this.appPlatformClient.PATCH( |
| 807 | "/api/v1/workspaces/{workspaceSlug}/apps/{appKey}/files/{fileId}", |
| 808 | { |
| 809 | params: { path: { workspaceSlug, appKey, fileId } }, |
| 810 | body: { document }, |
| 811 | }, |
| 812 | ), |
| 813 | ); |
| 814 | } |
| 815 |
| 816 | async listTabModels(): Promise<TabModelsResponse> { |
| 817 | return this.unwrapGeneratedResponse( |
| 818 | this.appPlatformClient.GET("/api/v1/tab/models"), |
| 819 | ); |
| 820 | } |
| 821 |
| 822 | async createTabChatCompletion(input: { |
| 823 | model: string; |
| 824 | stream?: boolean; |
| 825 | tab_context: Record<string, unknown>; |
| 826 | messages?: Array<{ role?: string; content?: string }>; |
| 827 | }): Promise<TabChatCompletionResponse> { |
| 828 | return this.unwrapGeneratedResponse( |
| 829 | this.appPlatformClient.POST("/api/v1/tab/chat/completions", { |
| 830 | body: input, |
| 831 | }), |
| 832 | ); |
| 833 | } |
| 834 |
| 835 | async exchangeOAuthCode(input: { |
| 836 | clientId: string; |
| 837 | clientSecret: string; |
| 838 | code: string; |
| 839 | redirectUri: string; |
| 840 | codeVerifier?: string; |
| 841 | }): Promise<OAuthTokenResponse> { |
| 842 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 843 | method: "POST", |
| 844 | headers: { |
| 845 | "Content-Type": "application/json", |
| 846 | }, |
| 847 | body: JSON.stringify({ |
| 848 | grant_type: "authorization_code", |
| 849 | client_id: input.clientId, |
| 850 | client_secret: input.clientSecret, |
| 851 | code: input.code, |
| 852 | redirect_uri: input.redirectUri, |
| 853 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 854 | }), |
| 855 | }); |
| 856 |
| 857 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 858 | } |
| 859 |
| 860 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 861 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 862 | method: "GET", |
| 863 | headers: { |
| 864 | Authorization: `Bearer ${accessToken}`, |
| 865 | }, |
| 866 | }); |
| 867 |
| 868 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 869 | } |
| 870 | } |
| 871 |