2024-01-19 08:05:38 +01:00
|
|
|
import type { OpenAI } from 'openai'
|
2024-03-01 15:33:22 +01:00
|
|
|
import { toolParametersSchema } from '../shared/parseChatCompletionOptions'
|
2024-01-19 08:05:38 +01:00
|
|
|
import { z } from '@typebot.io/forge/zod'
|
|
|
|
|
|
|
|
export const parseToolParameters = (
|
2024-01-29 14:45:44 +01:00
|
|
|
parameters: z.infer<typeof toolParametersSchema>
|
2024-01-19 08:05:38 +01:00
|
|
|
): OpenAI.FunctionParameters => ({
|
|
|
|
type: 'object',
|
|
|
|
properties: parameters?.reduce<{
|
|
|
|
[x: string]: unknown
|
|
|
|
}>((acc, param) => {
|
|
|
|
if (!param.name) return acc
|
|
|
|
acc[param.name] = {
|
2024-01-29 14:45:44 +01:00
|
|
|
type: param.type === 'enum' ? 'string' : param.type,
|
|
|
|
enum: param.type === 'enum' ? param.values : undefined,
|
2024-01-19 08:05:38 +01:00
|
|
|
description: param.description,
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, {}),
|
|
|
|
required:
|
|
|
|
parameters?.filter((param) => param.required).map((param) => param.name) ??
|
|
|
|
[],
|
|
|
|
})
|