@samchon/openapi
Version:
OpenAPI definitions and converters for 'typia' and 'nestia'.
1 lines • 7.56 kB
Source Map (JSON)
{"version":3,"file":"LlmDescriptionInverter.mjs","sources":["../../../src/composers/llm/LlmDescriptionInverter.ts"],"sourcesContent":["import { OpenApi } from \"../../OpenApi\";\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 const exclusiveMinimum: number | undefined = find({\n type: \"number\",\n name: \"exclusiveMinimum\",\n lines,\n });\n const exclusiveMaximum: number | undefined = find({\n type: \"number\",\n name: \"exclusiveMaximum\",\n lines,\n });\n return {\n minimum:\n exclusiveMinimum ??\n find({\n type: \"number\",\n name: \"minimum\",\n lines,\n }),\n maximum:\n exclusiveMaximum ??\n find({\n type: \"number\",\n name: \"maximum\",\n lines,\n }),\n exclusiveMinimum: exclusiveMinimum !== undefined ? true : undefined,\n exclusiveMaximum: exclusiveMaximum !== undefined ? true : undefined,\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\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","exclusiveMinimum","find","type","name","exclusiveMaximum","minimum","maximum","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":"AAEM,IAAWA;;CAAjB,SAAiBA;IACFA,uBAAAC,UACXC;QAUA,IAAIA,gBAAgBC,WAAW,OAAO,CAAE;QAExC,MAAMC,QAAkBF,YAAYG,MAAM;QAC1C,MAAMC,mBAAuCC,KAAK;YAChDC,MAAM;YACNC,MAAM;YACNL;;QAEF,MAAMM,mBAAuCH,KAAK;YAChDC,MAAM;YACNC,MAAM;YACNL;;QAEF,OAAO;YACLO,SACEL,oBACAC,KAAK;gBACHC,MAAM;gBACNC,MAAM;gBACNL;;YAEJQ,SACEF,oBACAH,KAAK;gBACHC,MAAM;gBACNC,MAAM;gBACNL;;YAEJE,kBAAkBA,qBAAqBH,YAAY,OAAOA;YAC1DO,kBAAkBA,qBAAqBP,YAAY,OAAOA;YAC1DU,YAAYN,KAAK;gBACfC,MAAM;gBACNC,MAAM;gBACNL;;YAEFF,aAAaY,SAASV,OAAO,EAC3B,WACA,WACA,oBACA,oBACA;;AAEH;IAGUJ,uBAAAe,SACXb;QAUA,IAAIA,gBAAgBC,WAAW,OAAO,CAAE;QAExC,MAAMC,QAAkBF,YAAYG,MAAM;QAC1C,OAAO;YACLW,QAAQT,KAAK;gBACXC,MAAM;gBACNC,MAAM;gBACNL;;YAEFa,SAASV,KAAK;gBACZC,MAAM;gBACNC,MAAM;gBACNL;;YAEFc,kBAAkBX,KAAK;gBACrBC,MAAM;gBACNC,MAAM;gBACNL;;YAEFe,WAAWZ,KAAK;gBACdC,MAAM;gBACNC,MAAM;gBACNL;;YAEFgB,WAAWb,KAAK;gBACdC,MAAM;gBACNC,MAAM;gBACNL;;YAEFF,aAAaY,SAASV,OAAO,EAC3B,UACA,WACA,oBACA,aACA;;AAEH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