import {z} from 'zod' export type ChatoptChatOption = {enable_dependency_cache?: boolean | undefined, is_need_doc_retrival_post_process?: boolean | undefined, is_need_history?: boolean | undefined, is_need_progress?: boolean | undefined}; export type ComvalobjLLMMessage = {content?: string | undefined, role?: string | undefined}; export type V2agentexecutordtoInterruptConfig = {confirmation_message?: string | undefined, requires_confirmation?: boolean | undefined}; export type V2agentexecutordtoToolArg = {key?: string | undefined, type?: string | undefined, value: unknown}; export type V2agentexecutordtoInterruptData = {interrupt_config?: V2agentexecutordtoInterruptConfig | undefined, tool_args?: Array | undefined, tool_description?: string | undefined, tool_name?: string | undefined}; export type V2agentexecutordtoModifiedArg = {key?: string | undefined, value: unknown}; export type V2agentexecutordtoInterruptHandle = {current_block?: number | undefined, frame_id?: string | undefined, interrupt_type?: string | undefined, restart_block?: boolean | undefined, resume_token?: string | undefined, snapshot_id?: string | undefined}; export type V2agentexecutordtoAgentResumeInfo = {action?: string | undefined, data?: V2agentexecutordtoInterruptData | undefined, modified_args?: Array | undefined, resume_handle?: V2agentexecutordtoInterruptHandle | undefined}; export type AgentreqSelectedFile = {file_name: string}; export type ValueobjectTempFile = {details: unknown, id?: string | undefined, name?: string | undefined, type?: string | undefined}; export type SwaggerChatReq = {agent_app_key?: string | undefined, agent_id?: string | undefined, agent_key?: string | undefined, agent_run_id?: string | undefined, agent_version?: string | undefined, chat_mode?: string | undefined, chat_option?: ChatoptChatOption | undefined, conversation_id?: string | undefined, custom_querys?: {[key: string]: unknown} | undefined, executor_version?: string | undefined, history?: Array | undefined, inc_stream?: boolean | undefined, interrupted_assistant_message_id?: string | undefined, model_name?: string | undefined, query?: string | undefined, regenerate_assistant_message_id?: string | undefined, regenerate_user_message_id?: string | undefined, resume_interrupt_info?: V2agentexecutordtoAgentResumeInfo | undefined, selected_files?: Array | undefined, stream?: boolean | undefined, temp_files?: Array | undefined}; export type CreateApiV1AppAppKeyChatCompletionArgs = {app_key: string, body: SwaggerChatReq}; export type CreateApiV1AppAppKeyChatCompletionOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const restBaseError = z.object({description: z.string().optional(), error_code: z.string().optional(), error_details: z.unknown(), error_link: z.string().optional(), solution: z.string().optional()}); export const restHTTPError = z.object({baseError: restBaseError.optional(), httpcode: z.number().int().optional(), language: z.string().optional()}); export const valueobjectAgentInfo = z.object({agent_id: z.string().optional(), agent_name: z.string().optional(), agent_status: z.string().optional(), agent_version: z.string().optional()}); export const chatresenumAnswerType = z.enum(["prompt", "explore", "other", "explore", "prompt", "other"]); export const agentresperrRespErrorType = z.enum(["agent_factory", "agent_executor"]); export const agentresperrRespError = z.object({error: z.unknown(), type: agentresperrRespErrorType.optional()}); export const v2agentexecutordtoInterruptConfig = z.object({confirmation_message: z.string().optional(), requires_confirmation: z.boolean().optional()}); export const v2agentexecutordtoToolArg = z.object({key: z.string().optional(), type: z.string().optional(), value: z.unknown()}); export const v2agentexecutordtoInterruptData = z.object({interrupt_config: v2agentexecutordtoInterruptConfig.optional(), tool_args: z.array(v2agentexecutordtoToolArg).optional(), tool_description: z.string().optional(), tool_name: z.string().optional()}); export const v2agentexecutordtoInterruptHandle = z.object({current_block: z.number().int().optional(), frame_id: z.string().optional(), interrupt_type: z.string().optional(), restart_block: z.boolean().optional(), resume_token: z.string().optional(), snapshot_id: z.string().optional()}); export const v2agentexecutordtoToolInterruptInfo = z.object({data: v2agentexecutordtoInterruptData.optional(), handle: v2agentexecutordtoInterruptHandle.optional()}); export const conversationmsgvoMessageExt = z.object({agent_run_id: z.string().optional(), error: agentresperrRespError.optional(), interrupt_info: v2agentexecutordtoToolInterruptInfo.optional(), related_queries: z.array(z.string()).optional(), total_time: z.number().optional(), total_tokens: z.number().int().optional(), ttft: z.number().int().optional()}); export const cdaenumConversationMsgRole = z.enum(["user", "assistant"]); export const cdaenumConversationMsgStatus = z.enum(["received", "processed", "processing", "succeded", "failed", "cancelled"]); export const conversationmsgvoMessage = z.object({agent_info: valueobjectAgentInfo.optional(), content: z.unknown(), content_type: chatresenumAnswerType.optional(), conversation_id: z.string().optional(), ext: conversationmsgvoMessageExt.optional(), id: z.string().optional(), index: z.number().int().optional(), reply_id: z.string().optional(), role: cdaenumConversationMsgRole.optional(), status: cdaenumConversationMsgStatus.optional()}); export const swaggerChatResp = z.object({agent_run_id: z.string().optional(), assistant_message_id: z.string().optional(), conversation_id: z.string().optional(), error: restHTTPError.optional(), message: conversationmsgvoMessage.optional(), user_message_id: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * 对话 * * POST /v1/app/{app_key}/chat/completion */ export const createApiV1AppAppKeyChatCompletion = async (args: CreateApiV1AppAppKeyChatCompletionArgs, options: CreateApiV1AppAppKeyChatCompletionOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set("Authorization", BearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/app/${encodeURIComponent(String(args.app_key))}/chat/completion${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(swaggerChatResp, await res.json()) };