@samchon/openapi
Version:
Universal OpenAPI to LLM function calling schemas. Transform any Swagger/OpenAPI document into type-safe schemas for OpenAI, Claude, Qwen, and more.
1 lines • 7.33 kB
Source Map (JSON)
{"version":3,"file":"LlmDescriptionInverter.mjs","sources":["../../src/composers/LlmDescriptionInverter.ts"],"sourcesContent":["import { OpenApi } from \"../OpenApi\";\nimport { OpenApiExclusiveEmender } from \"../utils/OpenApiExclusiveEmender\";\n\nexport namespace LlmDescriptionInverter {\n export const numeric = (\n description: string | undefined,\n ): Pick<\n OpenApi.IJsonSchema.INumber,\n | \"minimum\"\n | \"maximum\"\n | \"exclusiveMinimum\"\n | \"exclusiveMaximum\"\n | \"multipleOf\"\n | \"description\"\n > => {\n if (description === undefined) return {};\n\n const lines: string[] = description.split(\"\\n\");\n return OpenApiExclusiveEmender.emend({\n minimum: find({\n type: \"number\",\n name: \"minimum\",\n lines,\n }),\n maximum: find({\n type: \"number\",\n name: \"maximum\",\n lines,\n }),\n exclusiveMinimum: find({\n type: \"number\",\n name: \"exclusiveMinimum\",\n lines,\n }),\n exclusiveMaximum: find({\n type: \"number\",\n name: \"exclusiveMaximum\",\n lines,\n }),\n multipleOf: find({\n type: \"number\",\n name: \"multipleOf\",\n lines,\n }),\n description: describe(lines, [\n \"minimum\",\n \"maximum\",\n \"exclusiveMinimum\",\n \"exclusiveMaximum\",\n \"multipleOf\",\n ]),\n });\n };\n\n export const string = (\n description: string | undefined,\n ): Pick<\n OpenApi.IJsonSchema.IString,\n | \"format\"\n | \"pattern\"\n | \"contentMediaType\"\n | \"minLength\"\n | \"maxLength\"\n | \"description\"\n > => {\n if (description === undefined) return {};\n\n const lines: string[] = description.split(\"\\n\");\n return {\n format: find({\n type: \"string\",\n name: \"format\",\n lines,\n }),\n pattern: find({\n type: \"string\",\n name: \"pattern\",\n lines,\n }),\n contentMediaType: find({\n type: \"string\",\n name: \"contentMediaType\",\n lines,\n }),\n minLength: find({\n type: \"number\",\n name: \"minLength\",\n lines,\n }),\n maxLength: find({\n type: \"number\",\n name: \"maxLength\",\n lines,\n }),\n description: describe(lines, [\n \"format\",\n \"pattern\",\n \"contentMediaType\",\n \"minLength\",\n \"maxLength\",\n ]),\n };\n };\n\n export const array = (\n description: string | undefined,\n ): Pick<\n OpenApi.IJsonSchema.IArray,\n \"minItems\" | \"maxItems\" | \"uniqueItems\" | \"description\"\n > => {\n if (description === undefined) return {};\n\n const lines: string[] = description.split(\"\\n\");\n return {\n minItems: find({\n type: \"number\",\n name: \"minItems\",\n lines,\n }),\n maxItems: find({\n type: \"number\",\n name: \"maxItems\",\n lines,\n }),\n uniqueItems: find({\n type: \"boolean\",\n name: \"uniqueItems\",\n lines,\n }),\n description: describe(lines, [\"minItems\", \"maxItems\", \"uniqueItems\"]),\n };\n };\n\n const find = <Type extends \"boolean\" | \"number\" | \"string\">(props: {\n type: Type;\n name: string;\n lines: string[];\n }):\n | (Type extends \"boolean\" ? true : Type extends \"number\" ? number : string)\n | undefined => {\n if (props.type === \"boolean\")\n return props.lines.some((line) => line.startsWith(`@${props.name}`))\n ? (true as any)\n : (undefined as any);\n for (const line of props.lines) {\n if (line.startsWith(`@${props.name} `) === false) continue;\n const value: string = line.replace(`@${props.name} `, \"\").trim();\n if (props.type === \"number\")\n return (isNaN(Number(value)) ? undefined : Number(value)) satisfies\n | number\n | undefined as any;\n return value as any;\n }\n return undefined as any;\n };\n\n const describe = (lines: string[], tags: string[]): string | undefined => {\n const ret: string = trimArray(\n lines\n .map((str) => str.trim())\n .filter((str) =>\n tags.every((tag) => str.startsWith(`@${tag}`) === false),\n ),\n ).join(\"\\n\");\n return ret.length === 0 ? undefined : ret;\n };\n\n const trimArray = (array: string[]): string[] => {\n let first: number = 0;\n let last: number = array.length - 1;\n\n for (; first < array.length; ++first)\n if (array[first]!.trim().length !== 0) break;\n for (; last >= 0; --last) if (array[last]!.trim().length !== 0) break;\n return array.slice(first, last + 1);\n };\n}\n"],"names":["LlmDescriptionInverter","numeric","description","undefined","lines","split","OpenApiExclusiveEmender","emend","minimum","find","type","name","maximum","exclusiveMinimum","exclusiveMaximum","multipleOf","describe","string","format","pattern","contentMediaType","minLength","maxLength","array","minItems","maxItems","uniqueItems","props","some","line","startsWith","value","replace","trim","isNaN","Number","tags","ret","trimArray","map","str","filter","every","tag","join","length","first","last","slice"],"mappings":";;AAGM,IAAWA;;CAAjB,SAAiBA;IACFA,uBAAAC,UACXC;QAUA,IAAIA,gBAAgBC,WAAW,OAAO,CAAA;QAEtC,MAAMC,QAAkBF,YAAYG,MAAM;QAC1C,OAAOC,wBAAwBC,MAAM;YACnCC,SAASC,KAAK;gBACZC,MAAM;gBACNC,MAAM;gBACNP;;YAEFQ,SAASH,KAAK;gBACZC,MAAM;gBACNC,MAAM;gBACNP;;YAEFS,kBAAkBJ,KAAK;gBACrBC,MAAM;gBACNC,MAAM;gBACNP;;YAEFU,kBAAkBL,KAAK;gBACrBC,MAAM;gBACNC,MAAM;gBACNP;;YAEFW,YAAYN,KAAK;gBACfC,MAAM;gBACNC,MAAM;gBACNP;;YAEFF,aAAac,SAASZ,OAAO,EAC3B,WACA,WACA,oBACA,oBACA;;;IAKOJ,uBAAAiB,SACXf;QAUA,IAAIA,gBAAgBC,WAAW,OAAO,CAAA;QAEtC,MAAMC,QAAkBF,YAAYG,MAAM;QAC1C,OAAO;YACLa,QAAQT,KAAK;gBACXC,MAAM;gBACNC,MAAM;gBACNP;;YAEFe,SAASV,KAAK;gBACZC,MAAM;gBACNC,MAAM;gBACNP;;YAEFgB,kBAAkBX,KAAK;gBACrBC,MAAM;gBACNC,MAAM;gBACNP;;YAEFiB,WAAWZ,KAAK;gBACdC,MAAM;gBACNC,MAAM;gBACNP;;YAEFkB,WAAWb,KAAK;gBACdC,MAAM;gBACNC,MAAM;gBACNP;;YAEFF,aAAac,SAASZ,OAAO,EAC3B,UACA,WACA,oBACA,aACA;;;IAKOJ,uBAAAuB,QACXrB;QAKA,IAAIA,gBAAgBC,WAAW,OAAO,CAAA;QAEtC,MAAMC,QA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