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 |
| 18 | interface ApiErrorPayload { |
| 19 | error?: { |
| 20 | code?: string; |
| 21 | message?: string; |
| 22 | }; |
| 23 | } |
| 24 |
| 25 | async function parseJsonResponse<T>(response: Response): Promise<T> { |
| 26 | const payload = (await response.json().catch(() => null)) as T | null; |
| 27 |
| 28 | if (!response.ok) { |
| 29 | const errorMessage = |
| 30 | payload && |
| 31 | typeof payload === "object" && |
| 32 | "error_description" in payload && |
| 33 | typeof (payload as Record<string, unknown>)["error_description"] === "string" |
| 34 | ? ((payload as Record<string, unknown>)["error_description"] as string) |
| 35 | : `Request failed (${response.status}).`; |
| 36 |
| 37 | throw new ColineApiError({ |
| 38 | message: errorMessage, |
| 39 | type: "oauth", |
| 40 | status: response.status, |
| 41 | code: |
| 42 | payload && |
| 43 | typeof payload === "object" && |
| 44 | "error" in payload && |
| 45 | typeof (payload as Record<string, unknown>)["error"] === "string" |
| 46 | ? ((payload as Record<string, unknown>)["error"] as string) |
| 47 | : null, |
| 48 | }); |
| 49 | } |
| 50 |
| 51 | if (!payload) { |
| 52 | throw new ColineApiError({ |
| 53 | message: "Invalid JSON response.", |
| 54 | type: "oauth", |
| 55 | status: response.status, |
| 56 | }); |
| 57 | } |
| 58 |
| 59 | return payload; |
| 60 | } |
| 61 |
| 62 | async function parseApiResponse<T>(response: Response): Promise<T> { |
| 63 | const payload = (await response.json().catch(() => null)) as |
| 64 | | { data?: T; error?: ApiErrorPayload["error"] } |
| 65 | | null; |
| 66 |
| 67 | if (!response.ok) { |
| 68 | throw new ColineApiError({ |
| 69 | message: payload?.error?.message ?? `Request failed (${response.status}).`, |
| 70 | type: "api", |
| 71 | status: response.status, |
| 72 | code: payload?.error?.code ?? null, |
| 73 | }); |
| 74 | } |
| 75 |
| 76 | if (!payload || !("data" in payload)) { |
| 77 | throw new ColineApiError({ |
| 78 | message: "Invalid API response.", |
| 79 | type: "api", |
| 80 | status: response.status, |
| 81 | }); |
| 82 | } |
| 83 |
| 84 | return payload.data as T; |
| 85 | } |
| 86 |
| 87 | export interface ColineApiClientOptions { |
| 88 | baseUrl: string; |
| 89 | apiKey?: string; |
| 90 | fetch?: typeof fetch; |
| 91 | headers?: HeadersInit; |
| 92 | } |
| 93 |
| 94 | type Schemas = AppPlatformComponents["schemas"]; |
| 95 | type UnwrapData<TKey extends keyof Schemas> = |
| 96 | Schemas[TKey] extends { data: infer T } ? T : never; |
| 97 |
| 98 | export type RegisteredAppDetail = Schemas["RegisteredAppDetail"]; |
| 99 | export type RegisteredAppRecord = Omit< |
| 100 | RegisteredAppDetail, |
| 101 | "latestPublishedVersionId" |
| 102 | >; |
| 103 | export type RegisteredAppVersionSummary = Schemas["RegisteredAppVersionSummary"]; |
| 104 | export type WorkspaceInstalledApp = Schemas["InstalledAppSummary"]; |
| 105 | export type WorkspaceAppCatalogItem = Schemas["AppCatalogItem"]; |
| 106 | export type WorkspaceAppsResponse = UnwrapData<"WorkspaceAppsResponse">; |
| 107 |
| 108 | export interface InstallWorkspaceAppInput { |
| 109 | appId: string; |
| 110 | pinnedVersionId?: string; |
| 111 | grantedPermissions?: AppPermission[]; |
| 112 | } |
| 113 |
| 114 | export type InstallWorkspaceAppResponse = UnwrapData<"InstallWorkspaceAppResponse">; |
| 115 |
| 116 | export interface SubmitAppVersionForReviewInput { |
| 117 | storeListed?: boolean; |
| 118 | } |
| 119 |
| 120 | export type SubmitAppVersionForReviewResponse = UnwrapData< |
| 121 | "SubmitAppVersionForReviewResponse" |
| 122 | >; |
| 123 | export type WorkspaceAppPermissionsResponse = UnwrapData< |
| 124 | "WorkspaceAppPermissionsResponse" |
| 125 | >; |
| 126 | export type WorkspaceInstalledAppResponse = UnwrapData< |
| 127 | "WorkspaceInstalledAppResponse" |
| 128 | >; |
| 129 | export type WorkspaceAppSecretsResponse = UnwrapData< |
| 130 | "WorkspaceAppSecretsResponse" |
| 131 | >; |
| 132 | export type WorkspaceAppSecretSummary = WorkspaceAppSecretsResponse["secrets"][number]; |
| 133 | export type CreateWorkspaceAppSecretResponse = UnwrapData< |
| 134 | "CreateWorkspaceAppSecretResponse" |
| 135 | >; |
| 136 | export type WorkspaceAppOauthConnectionsResponse = UnwrapData< |
| 137 | "WorkspaceAppOauthConnectionsResponse" |
| 138 | >; |
| 139 | export type WorkspaceAppOauthConnectionSummary = |
| 140 | WorkspaceAppOauthConnectionsResponse["connections"][number]; |
| 141 | export type CreateWorkspaceAppOauthConnectionResponse = UnwrapData< |
| 142 | "CreateWorkspaceAppOauthConnectionResponse" |
| 143 | >; |
| 144 | export type WorkspaceAppDeliveriesResponse = UnwrapData< |
| 145 | "WorkspaceAppDeliveriesResponse" |
| 146 | >; |
| 147 | export type WorkspaceAppDeliverySummary = |
| 148 | WorkspaceAppDeliveriesResponse["deliveries"][number]; |
| 149 | export type SendWorkspaceAppTestDeliveryResponse = UnwrapData< |
| 150 | "SendWorkspaceAppTestDeliveryResponse" |
| 151 | >; |
| 152 |
| 153 | export interface AppFileDocumentResponse { |
| 154 | file: { |
| 155 | id: string; |
| 156 | name: string; |
| 157 | fileType: string; |
| 158 | appKey: string; |
| 159 | appName: string; |
| 160 | typeLabel: string; |
| 161 | openHref: string; |
| 162 | updatedAt: string; |
| 163 | }; |
| 164 | document: Record<string, unknown>; |
| 165 | version: number; |
| 166 | capabilities: { |
| 167 | canRead: boolean; |
| 168 | canWrite: boolean; |
| 169 | canManage: boolean; |
| 170 | }; |
| 171 | } |
| 172 |
| 173 | export interface TabModelRecord { |
| 174 | id: string; |
| 175 | object: "model"; |
| 176 | created: number; |
| 177 | owned_by: string; |
| 178 | providerModelId: string; |
| 179 | pricing: { |
| 180 | inputPerMillionUsd: number; |
| 181 | outputPerMillionUsd: number; |
| 182 | }; |
| 183 | capabilities: { |
| 184 | streaming: boolean; |
| 185 | surfaces: string[]; |
| 186 | }; |
| 187 | } |
| 188 |
| 189 | export interface OAuthTokenResponse { |
| 190 | access_token: string; |
| 191 | token_type: "Bearer"; |
| 192 | expires_in: number; |
| 193 | refresh_token: string; |
| 194 | scope: string; |
| 195 | } |
| 196 |
| 197 | export interface OAuthUserInfoResponse { |
| 198 | sub: string; |
| 199 | email: string; |
| 200 | email_verified: boolean; |
| 201 | name: string; |
| 202 | given_name: string | null; |
| 203 | family_name: string | null; |
| 204 | picture: string | null; |
| 205 | preferred_username: string | null; |
| 206 | scope: string; |
| 207 | } |
| 208 |
| 209 | export interface UpdateWorkspaceAppPermissionsInput { |
| 210 | grantedPermissions: AppPermission[]; |
| 211 | } |
| 212 |
| 213 | function joinUrl(baseUrl: string, path: string): string { |
| 214 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 215 | } |
| 216 |
| 217 | export class ColineApiClient { |
| 218 | private readonly baseUrl: string; |
| 219 | private readonly apiKey: string | undefined; |
| 220 | private readonly fetchImpl: typeof fetch; |
| 221 | private readonly defaultHeaders: HeadersInit | undefined; |
| 222 |
| 223 | constructor(options: ColineApiClientOptions) { |
| 224 | this.baseUrl = options.baseUrl; |
| 225 | this.apiKey = options.apiKey; |
| 226 | this.fetchImpl = options.fetch ?? fetch; |
| 227 | this.defaultHeaders = options.headers; |
| 228 | } |
| 229 |
| 230 | private async request<T>(path: string, init?: RequestInit): Promise<T> { |
| 231 | const headers = new Headers(this.defaultHeaders); |
| 232 | const requestHeaders = new Headers(init?.headers); |
| 233 | requestHeaders.forEach((value, key) => headers.set(key, value)); |
| 234 |
| 235 | if (!headers.has("Content-Type") && init?.body) { |
| 236 | headers.set("Content-Type", "application/json"); |
| 237 | } |
| 238 |
| 239 | if (this.apiKey && !headers.has("Authorization")) { |
| 240 | headers.set("Authorization", `Bearer ${this.apiKey}`); |
| 241 | } |
| 242 |
| 243 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 244 | ...init, |
| 245 | headers, |
| 246 | }); |
| 247 |
| 248 | return parseApiResponse<T>(response); |
| 249 | } |
| 250 |
| 251 | withApiKey(apiKey: string): ColineApiClient { |
| 252 | const options: ColineApiClientOptions = { |
| 253 | baseUrl: this.baseUrl, |
| 254 | apiKey, |
| 255 | fetch: this.fetchImpl, |
| 256 | }; |
| 257 |
| 258 | if (this.defaultHeaders !== undefined) { |
| 259 | options.headers = this.defaultHeaders; |
| 260 | } |
| 261 |
| 262 | return new ColineApiClient(options); |
| 263 | } |
| 264 |
| 265 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 266 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 267 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 268 |
| 269 | const options: ColineApiClientOptions = { |
| 270 | baseUrl: this.baseUrl, |
| 271 | fetch: this.fetchImpl, |
| 272 | headers: mergedHeaders, |
| 273 | }; |
| 274 |
| 275 | if (this.apiKey !== undefined) { |
| 276 | options.apiKey = this.apiKey; |
| 277 | } |
| 278 |
| 279 | return new ColineApiClient(options); |
| 280 | } |
| 281 |
| 282 | buildLoginWithColineAuthorizeUrl(input: { |
| 283 | clientId: string; |
| 284 | redirectUri: string; |
| 285 | scope?: string; |
| 286 | state?: string; |
| 287 | codeChallenge?: string; |
| 288 | codeChallengeMethod?: "S256" | "plain"; |
| 289 | }): string { |
| 290 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 291 | url.searchParams.set("response_type", "code"); |
| 292 | url.searchParams.set("client_id", input.clientId); |
| 293 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 294 | if (input.scope) { |
| 295 | url.searchParams.set("scope", input.scope); |
| 296 | } |
| 297 | if (input.state) { |
| 298 | url.searchParams.set("state", input.state); |
| 299 | } |
| 300 | if (input.codeChallenge) { |
| 301 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 302 | } |
| 303 | if (input.codeChallengeMethod) { |
| 304 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 305 | } |
| 306 | return url.toString(); |
| 307 | } |
| 308 |
| 309 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 310 | return this.request<{ apps: RegisteredAppRecord[] }>("/api/v1/apps"); |
| 311 | } |
| 312 |
| 313 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 314 | return this.request<{ app: RegisteredAppDetail }>(`/api/v1/apps/${appKey}`); |
| 315 | } |
| 316 |
| 317 | async listAppVersions( |
| 318 | appKey: string, |
| 319 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 320 | return this.request<{ versions: RegisteredAppVersionSummary[] }>( |
| 321 | `/api/v1/apps/${appKey}/versions`, |
| 322 | ); |
| 323 | } |
| 324 |
| 325 | async registerApp( |
| 326 | payload: ColineAppRegistrationPayload, |
| 327 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 328 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 329 |
| 330 | return this.request<{ |
| 331 | appId: string; |
| 332 | appKey: string; |
| 333 | versionId: string; |
| 334 | version: string; |
| 335 | }>("/api/v1/apps", { |
| 336 | method: "POST", |
| 337 | body: JSON.stringify(body), |
| 338 | }); |
| 339 | } |
| 340 |
| 341 | async createAppVersion( |
| 342 | appKey: string, |
| 343 | payload: ColineAppRegistrationPayload, |
| 344 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 345 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 346 |
| 347 | return this.request<{ |
| 348 | appId: string; |
| 349 | appKey: string; |
| 350 | versionId: string; |
| 351 | version: string; |
| 352 | }>(`/api/v1/apps/${appKey}/versions`, { |
| 353 | method: "POST", |
| 354 | body: JSON.stringify(body), |
| 355 | }); |
| 356 | } |
| 357 |
| 358 | async submitAppVersionForReview( |
| 359 | appKey: string, |
| 360 | versionId: string, |
| 361 | input: SubmitAppVersionForReviewInput = {}, |
| 362 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 363 | return this.request<SubmitAppVersionForReviewResponse>( |
| 364 | `/api/v1/apps/${appKey}/versions/${versionId}/submit`, |
| 365 | { |
| 366 | method: "POST", |
| 367 | body: JSON.stringify({ |
| 368 | ...(input.storeListed !== undefined |
| 369 | ? { storeListed: input.storeListed } |
| 370 | : {}), |
| 371 | }), |
| 372 | }, |
| 373 | ); |
| 374 | } |
| 375 |
| 376 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 377 | return this.request<WorkspaceAppsResponse>( |
| 378 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 379 | ); |
| 380 | } |
| 381 |
| 382 | async installWorkspaceApp( |
| 383 | workspaceSlug: string, |
| 384 | input: InstallWorkspaceAppInput, |
| 385 | ): Promise<InstallWorkspaceAppResponse> { |
| 386 | const grantedPermissions = input.grantedPermissions |
| 387 | ? input.grantedPermissions.map((permission) => |
| 388 | appPermissionSchema.parse(permission), |
| 389 | ) |
| 390 | : undefined; |
| 391 |
| 392 | return this.request<InstallWorkspaceAppResponse>( |
| 393 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 394 | { |
| 395 | method: "POST", |
| 396 | body: JSON.stringify({ |
| 397 | appId: input.appId, |
| 398 | ...(input.pinnedVersionId |
| 399 | ? { pinnedVersionId: input.pinnedVersionId } |
| 400 | : {}), |
| 401 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 402 | }), |
| 403 | }, |
| 404 | ); |
| 405 | } |
| 406 |
| 407 | async getWorkspaceAppPermissions( |
| 408 | workspaceSlug: string, |
| 409 | appKey: string, |
| 410 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 411 | return this.request<WorkspaceAppPermissionsResponse>( |
| 412 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 413 | ); |
| 414 | } |
| 415 |
| 416 | async getWorkspaceInstalledApp( |
| 417 | workspaceSlug: string, |
| 418 | appKey: string, |
| 419 | ): Promise<WorkspaceInstalledAppResponse> { |
| 420 | return this.request<WorkspaceInstalledAppResponse>( |
| 421 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 422 | ); |
| 423 | } |
| 424 |
| 425 | async updateWorkspaceAppPermissions( |
| 426 | workspaceSlug: string, |
| 427 | appKey: string, |
| 428 | input: UpdateWorkspaceAppPermissionsInput, |
| 429 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 430 | return this.request<WorkspaceAppPermissionsResponse>( |
| 431 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 432 | { |
| 433 | method: "PUT", |
| 434 | body: JSON.stringify({ |
| 435 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 436 | appPermissionSchema.parse(permission), |
| 437 | ), |
| 438 | }), |
| 439 | }, |
| 440 | ); |
| 441 | } |
| 442 |
| 443 | async uninstallWorkspaceApp( |
| 444 | workspaceSlug: string, |
| 445 | appKey: string, |
| 446 | ): Promise<{ ok: true }> { |
| 447 | return this.request<{ ok: true }>( |
| 448 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 449 | { |
| 450 | method: "DELETE", |
| 451 | }, |
| 452 | ); |
| 453 | } |
| 454 |
| 455 | async listWorkspaceAppSecrets( |
| 456 | workspaceSlug: string, |
| 457 | appKey: string, |
| 458 | ): Promise<WorkspaceAppSecretsResponse> { |
| 459 | return this.request<WorkspaceAppSecretsResponse>( |
| 460 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/secrets`, |
| 461 | ); |
| 462 | } |
| 463 |
| 464 | async createWorkspaceAppSecret( |
| 465 | workspaceSlug: string, |
| 466 | appKey: string, |
| 467 | input: { key: string; value: string }, |
| 468 | ): Promise<CreateWorkspaceAppSecretResponse> { |
| 469 | return this.request<CreateWorkspaceAppSecretResponse>( |
| 470 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/secrets`, |
| 471 | { |
| 472 | method: "POST", |
| 473 | body: JSON.stringify(input), |
| 474 | }, |
| 475 | ); |
| 476 | } |
| 477 |
| 478 | async deleteWorkspaceAppSecret( |
| 479 | workspaceSlug: string, |
| 480 | appKey: string, |
| 481 | secretId: string, |
| 482 | ): Promise<{ ok: true }> { |
| 483 | return this.request<{ ok: true }>( |
| 484 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/secrets/${secretId}`, |
| 485 | { |
| 486 | method: "DELETE", |
| 487 | }, |
| 488 | ); |
| 489 | } |
| 490 |
| 491 | async listWorkspaceAppOauthConnections( |
| 492 | workspaceSlug: string, |
| 493 | appKey: string, |
| 494 | ): Promise<WorkspaceAppOauthConnectionsResponse> { |
| 495 | return this.request<WorkspaceAppOauthConnectionsResponse>( |
| 496 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/oauth-connections`, |
| 497 | ); |
| 498 | } |
| 499 |
| 500 | async createWorkspaceAppOauthConnection( |
| 501 | workspaceSlug: string, |
| 502 | appKey: string, |
| 503 | input: { |
| 504 | providerKey: string; |
| 505 | externalAccountId?: string | null; |
| 506 | accessToken: string; |
| 507 | refreshToken?: string | null; |
| 508 | scopes: string[]; |
| 509 | tokenExpiresAt?: string | null; |
| 510 | metadata?: Record<string, unknown>; |
| 511 | }, |
| 512 | ): Promise<CreateWorkspaceAppOauthConnectionResponse> { |
| 513 | return this.request<CreateWorkspaceAppOauthConnectionResponse>( |
| 514 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/oauth-connections`, |
| 515 | { |
| 516 | method: "POST", |
| 517 | body: JSON.stringify(input), |
| 518 | }, |
| 519 | ); |
| 520 | } |
| 521 |
| 522 | async deleteWorkspaceAppOauthConnection( |
| 523 | workspaceSlug: string, |
| 524 | appKey: string, |
| 525 | connectionId: string, |
| 526 | ): Promise<{ ok: true }> { |
| 527 | return this.request<{ ok: true }>( |
| 528 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/oauth-connections/${connectionId}`, |
| 529 | { |
| 530 | method: "DELETE", |
| 531 | }, |
| 532 | ); |
| 533 | } |
| 534 |
| 535 | async listWorkspaceAppDeliveries( |
| 536 | workspaceSlug: string, |
| 537 | appKey: string, |
| 538 | ): Promise<WorkspaceAppDeliveriesResponse> { |
| 539 | return this.request<WorkspaceAppDeliveriesResponse>( |
| 540 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/deliveries`, |
| 541 | ); |
| 542 | } |
| 543 |
| 544 | async sendWorkspaceAppTestDelivery( |
| 545 | workspaceSlug: string, |
| 546 | appKey: string, |
| 547 | input: { |
| 548 | eventType: string; |
| 549 | targetUrl?: string; |
| 550 | payload: Record<string, unknown>; |
| 551 | }, |
| 552 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 553 | return this.request<SendWorkspaceAppTestDeliveryResponse>( |
| 554 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/deliveries`, |
| 555 | { |
| 556 | method: "POST", |
| 557 | body: JSON.stringify(input), |
| 558 | }, |
| 559 | ); |
| 560 | } |
| 561 |
| 562 | async replayWorkspaceAppDelivery( |
| 563 | workspaceSlug: string, |
| 564 | appKey: string, |
| 565 | deliveryId: string, |
| 566 | ): Promise<SendWorkspaceAppTestDeliveryResponse> { |
| 567 | return this.request<SendWorkspaceAppTestDeliveryResponse>( |
| 568 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/deliveries/${deliveryId}/replay`, |
| 569 | { |
| 570 | method: "POST", |
| 571 | }, |
| 572 | ); |
| 573 | } |
| 574 |
| 575 | async emitAppAmbientEvents( |
| 576 | workspaceSlug: string, |
| 577 | appKey: string, |
| 578 | input: EmitAmbientEventsInputInput, |
| 579 | ): Promise<{ events: Array<{ id: string; dedupeKey: string }> }> { |
| 580 | const body = emitAmbientEventsInputSchema.parse(input); |
| 581 |
| 582 | return this.request<{ events: Array<{ id: string; dedupeKey: string }> }>( |
| 583 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/ambient/events`, |
| 584 | { |
| 585 | method: "POST", |
| 586 | body: JSON.stringify(body), |
| 587 | }, |
| 588 | ); |
| 589 | } |
| 590 |
| 591 | async createAppNotification( |
| 592 | workspaceSlug: string, |
| 593 | appKey: string, |
| 594 | input: CreateNotificationInputInput, |
| 595 | ): Promise<{ ok: true }> { |
| 596 | const body = createNotificationInputSchema.parse(input); |
| 597 |
| 598 | return this.request<{ ok: true }>( |
| 599 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/notifications`, |
| 600 | { |
| 601 | method: "POST", |
| 602 | body: JSON.stringify(body), |
| 603 | }, |
| 604 | ); |
| 605 | } |
| 606 |
| 607 | async upsertAppIndexDocuments( |
| 608 | workspaceSlug: string, |
| 609 | appKey: string, |
| 610 | input: BatchUpsertAppIndexDocumentsInput, |
| 611 | ): Promise<{ documents: Array<{ id: string; documentKey: string; visibilityMode: string }> }> { |
| 612 | const body = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 613 |
| 614 | return this.request<{ |
| 615 | documents: Array<{ id: string; documentKey: string; visibilityMode: string }>; |
| 616 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, { |
| 617 | method: "POST", |
| 618 | body: JSON.stringify(body), |
| 619 | }); |
| 620 | } |
| 621 |
| 622 | async deleteAppIndexDocuments( |
| 623 | workspaceSlug: string, |
| 624 | appKey: string, |
| 625 | input: BatchDeleteAppIndexDocumentsInput, |
| 626 | ): Promise<{ deleted: Array<{ id: string; documentKey: string }> }> { |
| 627 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 628 |
| 629 | return this.request<{ deleted: Array<{ id: string; documentKey: string }> }>( |
| 630 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, |
| 631 | { |
| 632 | method: "DELETE", |
| 633 | body: JSON.stringify(body), |
| 634 | }, |
| 635 | ); |
| 636 | } |
| 637 |
| 638 | async getAppFileDocument( |
| 639 | workspaceSlug: string, |
| 640 | appKey: string, |
| 641 | fileId: string, |
| 642 | ): Promise<AppFileDocumentResponse> { |
| 643 | return this.request<AppFileDocumentResponse>( |
| 644 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, |
| 645 | ); |
| 646 | } |
| 647 |
| 648 | async updateAppFileDocument( |
| 649 | workspaceSlug: string, |
| 650 | appKey: string, |
| 651 | fileId: string, |
| 652 | document: Record<string, unknown>, |
| 653 | ): Promise<{ document: Record<string, unknown>; version: number; updatedAt: string }> { |
| 654 | return this.request<{ |
| 655 | document: Record<string, unknown>; |
| 656 | version: number; |
| 657 | updatedAt: string; |
| 658 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, { |
| 659 | method: "PATCH", |
| 660 | body: JSON.stringify({ document }), |
| 661 | }); |
| 662 | } |
| 663 |
| 664 | async listTabModels(): Promise<{ object: "list"; data: TabModelRecord[] }> { |
| 665 | return this.request<{ object: "list"; data: TabModelRecord[] }>( |
| 666 | "/api/v1/tab/models", |
| 667 | ); |
| 668 | } |
| 669 |
| 670 | async createTabChatCompletion(input: { |
| 671 | model: string; |
| 672 | stream?: boolean; |
| 673 | tab_context: Record<string, unknown>; |
| 674 | messages?: Array<{ role?: string; content?: string }>; |
| 675 | }): Promise<{ |
| 676 | id: string; |
| 677 | object: "chat.completion"; |
| 678 | created: number; |
| 679 | model: string; |
| 680 | choices: Array<{ |
| 681 | index: number; |
| 682 | message: { role: "assistant"; content: string }; |
| 683 | finish_reason: "stop"; |
| 684 | }>; |
| 685 | usage: { |
| 686 | prompt_tokens: number; |
| 687 | completion_tokens: number; |
| 688 | total_tokens: number; |
| 689 | }; |
| 690 | }> { |
| 691 | return this.request("/api/v1/tab/chat/completions", { |
| 692 | method: "POST", |
| 693 | body: JSON.stringify(input), |
| 694 | }); |
| 695 | } |
| 696 |
| 697 | async exchangeOAuthCode(input: { |
| 698 | clientId: string; |
| 699 | clientSecret: string; |
| 700 | code: string; |
| 701 | redirectUri: string; |
| 702 | codeVerifier?: string; |
| 703 | }): Promise<OAuthTokenResponse> { |
| 704 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 705 | method: "POST", |
| 706 | headers: { |
| 707 | "Content-Type": "application/json", |
| 708 | }, |
| 709 | body: JSON.stringify({ |
| 710 | grant_type: "authorization_code", |
| 711 | client_id: input.clientId, |
| 712 | client_secret: input.clientSecret, |
| 713 | code: input.code, |
| 714 | redirect_uri: input.redirectUri, |
| 715 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 716 | }), |
| 717 | }); |
| 718 |
| 719 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 720 | } |
| 721 |
| 722 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 723 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 724 | method: "GET", |
| 725 | headers: { |
| 726 | Authorization: `Bearer ${accessToken}`, |
| 727 | }, |
| 728 | }); |
| 729 |
| 730 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 731 | } |
| 732 | } |
| 733 |