import {z} from 'zod' export type CreateApiApiAgentsArgs = {body: {data: {type: string, attributes: {name: string, projectId: string, modelType: string, maxToolIterations?: number | undefined, temperature?: number | undefined, variables?: (Array<{name: string, targetKnowledgeBaseId?: string | undefined, targetFunction: string, toolTargetId: string, toolTargetType: string, functionArgumentsText?: string | undefined}>) | undefined, reasoningEffort?: ('NONE' | 'MINIMAL' | 'LOW' | 'MEDIUM' | 'HIGH') | undefined, systemPrompt?: (string | null) | undefined, starterMessage?: (string | null) | undefined, outputs?: (Array<{name: string, functionName: string, functionDescription?: string | undefined, jsonSchema: string, artifactType?: ('IMAGE_ID_OUTPUT' | 'MARKDOWN_TEXT_OUTPUT' | 'REACT_OUTPUT' | 'PRESENTATION') | undefined, artifactContentType?: ('IMAGE_ID_OUTPUT' | 'MARKDOWN_TEXT_OUTPUT' | 'REACT_OUTPUT' | 'PRESENTATION') | undefined}>) | undefined, tools?: (Array<{toolTargetId: string, toolTargetType: string, targetFunction: string, functionName: string, functionDescription?: string | undefined, agentOutputMode?: ('RETURN' | 'SHOW' | null) | undefined, agentPromptSchema?: string | undefined, agentPromptTemplate?: string | undefined, inheritChatFiles?: boolean | undefined}>) | undefined}}}}; export type CreateApiApiAgentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiAgentsResponse = z.object({data: z.object({id: z.string(), type: z.string(), links: z.object({self: z.string()}), attributes: z.object({createdAt: z.string(), updatedAt: z.string(), projectId: z.string(), name: z.string(), systemPrompt: z.string(), starterMessage: z.string().nullable(), modelType: z.string(), maxToolIterations: z.number().int(), temperature: z.number(), reasoningEffort: z.enum(["NONE", "MINIMAL", "LOW", "MEDIUM", "HIGH"]), isManaged: z.boolean(), outputs: z.array(z.object({name: z.string(), functionName: z.string(), functionDescription: z.string().nullable(), jsonSchema: z.string(), artifactType: z.enum(["IMAGE_ID_OUTPUT", "MARKDOWN_TEXT_OUTPUT", "REACT_OUTPUT", "PRESENTATION"]).nullable(), artifactContentType: z.enum(["IMAGE_ID_OUTPUT", "MARKDOWN_TEXT_OUTPUT", "REACT_OUTPUT", "PRESENTATION"]).nullable()})), tools: z.array(z.object({toolTargetId: z.string(), targetFunction: z.string(), functionName: z.string(), functionDescription: z.string().nullable(), agentOutputMode: z.enum(["RETURN", "SHOW"]), agentPromptSchema: z.string().nullable(), agentPromptTemplate: z.string().nullable(), toolTargetType: z.string(), inheritChatFiles: z.boolean(), targetType: z.string(), targetKnowledgeBaseId: z.string().nullable(), targetAgentId: z.string().nullable()})), variables: z.array(z.object({name: z.string(), toolTargetId: z.string(), targetFunction: z.string(), functionArgumentsText: z.string().nullable().optional(), toolTargetType: z.string(), targetKnowledgeBaseId: z.string().nullable().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 }; /** * create * * POST /api/agents */ export const createApiApiAgents = async (args: CreateApiApiAgentsArgs, options?: CreateApiApiAgentsOptions) => { const {baseUrl = "https://llm-api.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/agents${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(createApiApiAgentsResponse, await res.json()) };