(chunks: Chunk[])
| 318 | } |
| 319 | |
| 320 | export function chunksToProperties(chunks: Chunk[]): Properties { |
| 321 | /** |
| 322 | Once a 'properties' field has been deconstructed into chunks, it looks like this: |
| 323 | |
| 324 | { path: [ 'id', 'type' ], data: 'integer' }, |
| 325 | { path: [ 'id', 'description' ], data: 'Customer id' }, |
| 326 | { path: [ 'id', 'format' ], data: 'int64' }, |
| 327 | |
| 328 | We need to give this information to GPT in the form: |
| 329 | variableName (<type>): <description> |
| 330 | |
| 331 | So we transform it into a key value pair where the key is the name of the variable and |
| 332 | the value is an object containing the type and description (and the path for later use) |
| 333 | |
| 334 | e.g. for the above example: |
| 335 | { id: { type: 'integer', description: 'Customer id', path: [ 'id' ] } } |
| 336 | **/ |
| 337 | |
| 338 | const properties: Properties = {}; |
| 339 | for (const chunk of chunks) { |
| 340 | const fieldName = chunk.path.slice(0, -1).join("."); |
| 341 | const chunkType = chunk.path[chunk.path.length - 1]; |
| 342 | |
| 343 | const existingProperty = properties[fieldName] ?? { |
| 344 | path: chunk.path.slice(0, -1), |
| 345 | }; |
| 346 | |
| 347 | if (["type", "description"].includes(chunkType?.toString() ?? "")) { |
| 348 | // If statement below is true when the chunk is an array of primitives. Without this check, |
| 349 | // we ask gpt for a value for the key "item" which is part of the schema |
| 350 | // structure, not data we want returned. |
| 351 | // TODO: this doesn't treat the array as an array currently |
| 352 | if (fieldName === "items" && !["object", "array"].includes(chunk.data)) { |
| 353 | properties[chunk.path[chunk.path.length - 3]] = { |
| 354 | path: chunk.path, |
| 355 | [chunkType]: chunk.data, |
| 356 | }; |
| 357 | } else { |
| 358 | properties[fieldName] = { |
| 359 | ...existingProperty, |
| 360 | [chunkType]: chunk.data, |
| 361 | }; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | return properties; |
| 366 | } |
| 367 | |
| 368 | export function chunkToString(chunk: Chunk): string { |
| 369 | if (chunk.path.length === 0) return ""; |
no outgoing calls
no test coverage detected