// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../../core/resource'; import * as FeedbackAPI from './feedback'; import { Feedback, FeedbackSaveParams, FeedbackSaveResponse } from './feedback'; import { APIPromise } from '../../core/api-promise'; import { type Uploadable } from '../../core/uploads'; import { RequestOptions } from '../../internal/request-options'; import { multipartFormRequestOptions } from '../../internal/uploads'; import { path } from '../../internal/utils/path'; import { GLChatMessageChunk } from '../../resources'; import { safeJSON } from '../../internal/utils'; import { castToError } from '../../internal/errors'; import { APIError } from '../../error'; import { processGLChatChunk } from '../../custom/message/chunk'; import { buildHeaders } from '../../internal/headers'; export class MessageResource extends APIResource { feedback: FeedbackAPI.Feedback = new FeedbackAPI.Feedback(this._client); /** * Generate a response for the user message. Default is SSE; set form field * stream=false for JSON. */ async create( params: MessageCreateParams, options?: RequestOptions, ): Promise> { const { 'Accept-Language': acceptLanguage, 'X-API-Key': xAPIKey, ...body } = params; const headers = { Accept: 'text/event-stream', Authorization: `Bearer ${this._client.apiKey}`, ...(acceptLanguage ? { 'Accept-Language': acceptLanguage } : {}), ...(xAPIKey ? { 'X-API-Key': xAPIKey } : {}), ...(this._client.tenantID ? { 'X-Tenant-ID': this._client.tenantID } : {}), ...(options?.headers ?? {}), }; const requestOptions = await multipartFormRequestOptions( { body, ...options, headers, }, this._client, ); const abortController = new AbortController(); const url = new URL('message', options?.defaultBaseURL ?? this._client.baseURL); const response = await this._client.fetchWithTimeout( url, { method: 'POST', ...requestOptions, } as RequestInit, options?.timeout ?? 60_000, abortController, ); if (!response.ok) { const errText = await response.text().catch((err: any) => castToError(err).message); const errJSON = safeJSON(errText); throw APIError.generate(response.status, errJSON, errJSON ? undefined : errText, response.headers); } const reader = response.body?.getReader(); const decoder = new TextDecoder(); return { async *[Symbol.asyncIterator]() { if (!reader) { throw new Error('Failed to create stream reader: response body is missing'); } try { let buffer = ''; while (true) { const { done, value } = await reader.read(); if (done) { break; } buffer += decoder.decode(value, { stream: true }); let lines = buffer.split('\n'); buffer = lines.pop() ?? ''; for (const line of lines) { const trimmedLine = line.trim(); if (!trimmedLine.length) { continue; } yield processGLChatChunk(trimmedLine); } } const finalChunk = buffer.trim(); if (finalChunk.length) { yield processGLChatChunk(finalChunk); } } finally { reader.releaseLock(); } }, }; } /** * Delete one or more messages by their IDs and remove associated attachments. */ delete(params: MessageDeleteParams, options?: RequestOptions): APIPromise { const { message_ids, 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat } = params; return this._client.delete('/message', { query: { message_ids }, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } /** * Get a deanonymized message by message ID, conversation ID, and user ID from chat * history. */ deanonymize( messageID: string, params: MessageDeanonymizeParams, options?: RequestOptions, ): APIPromise { const { 'Accept-Language': acceptLanguage, 'X-Error-Format': xErrorFormat, ...query } = params; return this._client.get(path`/message/${messageID}/deanonymize`, { query, ...options, headers: buildHeaders([ { ...(acceptLanguage != null ? { 'Accept-Language': acceptLanguage } : undefined), ...(xErrorFormat != null ? { 'X-Error-Format': xErrorFormat } : undefined), }, options?.headers, ]), }); } } /** * Message model. */ export interface Message { conversation_id: string; created_time: string; is_active: boolean; /** * Enum for Message Type. */ role: 'user' | 'assistant'; id?: string | null; content?: string | null; deanonymized_content?: string | null; feedback?: string | null; metadata_?: { [key: string]: unknown } | null; parent_id?: string | null; source?: string | null; } /** * The type of search to perform. * * Attributes: NORMAL: Get answer from chatbot knowledge. SEARCH: Get answer from * various connectors. SQL_SEARCH: Get answer from SQL-based database. * AGENT_CONNECTOR: Execute a scoped glaip coordinator with per-request connector * selection. No request_connector or connector_management tooling — missing * connectors yield a plain-text response from the coordinator. WEB: Get more * relevant information from the web. (DEPRECATED) Web Search uses real-time data. * Agent selection isn't available in this mode. DEEP_RESEARCH: Get answer from * Deep Research Agent. ESSENTIALS_DEEP_RESEARCH: Get answer from Deep Research * with Essentials mode. Provides key points and core insights without noise, * optimized for speed and clarity. Ideal for quick decision-making support and * fast orientation. COMPREHENSIVE_DEEP_RESEARCH: Get answer from Deep Research * with Comprehensive mode. Delivers the full picture with depth and thoroughness, * covering all relevant angles, details, and supporting data. Suitable for * professional research tasks where precision matters more than speed. * PRIVATE_DEEP_RESEARCH: Get answer from Deep Research with private deep research * providers. INTERNAL_DEEP_RESEARCH: Retrieves answers using Deep Research with * internal data sources. (DEPRECATED) Accesses internal repositories such as * Google Drive and utilizes the same processing engine as Essentials Deep Research * mode. */ export type SearchType = | 'normal' | 'search' | 'sql_search' | 'agent_connector' | 'web' | 'deep_research' | 'essentials_deep_research' | 'comprehensive_deep_research' | 'private_deep_research' | 'internal_deep_research'; export type MessageDeleteResponse = unknown; /** * Response model for getting a deanonymized message. * * Attributes: message (Message): The deanonymized message. */ export interface MessageDeanonymizeResponse { /** * Message model. */ message: Message; } export interface MessageCreateParams { /** * Body param */ chatbot_id: string; /** * Body param */ agent_ids?: string; /** * Body param */ anonymize_em?: boolean | null; /** * Body param */ anonymize_lm?: boolean | null; /** * Body param */ assistant_message_id?: string | null; /** * Body param */ chat_history?: string | null; /** * Body param */ connectors?: string; /** * Body param */ conversation_id?: string | null; /** * Body param */ digital_employee_id?: string | null; /** * Body param */ exclude_events?: Array; /** * Body param */ exclude_prefix?: boolean; /** * Body param */ files?: Array; /** * Body param */ filters?: string | null; /** * Body param */ include_pipeline_trace?: boolean; /** * Body param */ include_states?: boolean; /** * Body param */ is_edited?: boolean; /** * Body param */ is_regenerated?: boolean; /** * Body param */ is_retried?: boolean; /** * Body param */ message?: string | null; /** * Body param */ metadata?: string | null; /** * Body param */ model_name?: string | null; /** * Body param */ parent_id?: string | null; /** * Body param */ quote?: string | null; /** * Body param */ regenerated_message_id?: string | null; /** * Body param: The type of search to perform. * * Attributes: NORMAL: Get answer from chatbot knowledge. SEARCH: Get answer from * various connectors. SQL_SEARCH: Get answer from SQL-based database. * AGENT_CONNECTOR: Execute a scoped glaip coordinator with per-request connector * selection. No request_connector or connector_management tooling — missing * connectors yield a plain-text response from the coordinator. WEB: Get more * relevant information from the web. (DEPRECATED) Web Search uses real-time data. * Agent selection isn't available in this mode. DEEP_RESEARCH: Get answer from * Deep Research Agent. ESSENTIALS_DEEP_RESEARCH: Get answer from Deep Research * with Essentials mode. Provides key points and core insights without noise, * optimized for speed and clarity. Ideal for quick decision-making support and * fast orientation. COMPREHENSIVE_DEEP_RESEARCH: Get answer from Deep Research * with Comprehensive mode. Delivers the full picture with depth and thoroughness, * covering all relevant angles, details, and supporting data. Suitable for * professional research tasks where precision matters more than speed. * PRIVATE_DEEP_RESEARCH: Get answer from Deep Research with private deep research * providers. INTERNAL_DEEP_RESEARCH: Retrieves answers using Deep Research with * internal data sources. (DEPRECATED) Accesses internal repositories such as * Google Drive and utilizes the same processing engine as Essentials Deep Research * mode. */ search_type?: SearchType | null; /** * Body param */ skill_ids?: string; /** * Body param */ smart_mode?: boolean; /** * Body param */ source?: string | null; /** * Body param */ stream?: boolean; /** * Body param */ stream_id?: string | null; /** * Body param */ stream_message_only?: boolean; /** * Body param */ temporary_file_ids?: string[] | null; /** * Body param */ use_cache?: boolean | null; /** * Body param */ user_id?: string | null; /** * Body param */ user_message_id?: string | null; /** * Header param */ 'Accept-Language'?: string; /** * Header param */ 'X-API-Key'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface MessageDeleteParams { /** * Query param */ message_ids: Array; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } export interface MessageDeanonymizeParams { /** * Query param */ conversation_id: string; /** * Header param */ 'Accept-Language'?: string; /** * Header param: Set to `structured` to receive errors in the structured envelope. * Omit it to keep the legacy error body. */ 'X-Error-Format'?: string; } MessageResource.Feedback = Feedback; export declare namespace MessageResource { export { type Message as Message, type SearchType as SearchType, type MessageDeleteResponse as MessageDeleteResponse, type MessageDeanonymizeResponse as MessageDeanonymizeResponse, type MessageCreateParams as MessageCreateParams, type MessageDeleteParams as MessageDeleteParams, type MessageDeanonymizeParams as MessageDeanonymizeParams, }; export { Feedback as Feedback, type FeedbackSaveResponse as FeedbackSaveResponse, type FeedbackSaveParams as FeedbackSaveParams, }; }