( properties: Properties, gptOutput: string, arrayIdx: number | null = null, )
| 371 | } |
| 372 | |
| 373 | export function addGPTdataToProperties( |
| 374 | properties: Properties, |
| 375 | gptOutput: string, |
| 376 | arrayIdx: number | null = null, |
| 377 | ): Properties { |
| 378 | /** |
| 379 | Add the data outputted by gpt to the properties object. |
| 380 | |
| 381 | If working with an array type (and gpt has output a comma separated list of values) |
| 382 | pass arrayIdx to select which value in an semi-colon separate list to use. |
| 383 | |
| 384 | TODO: currently always treats data as a string. |
| 385 | **/ |
| 386 | |
| 387 | gptOutput.split("\n").forEach((line) => { |
| 388 | let [key, value] = [ |
| 389 | line.slice(0, line.indexOf(":")).trim(), |
| 390 | line.slice(line.indexOf(":") + 1).trim(), |
| 391 | ]; |
| 392 | |
| 393 | if (key in properties) { |
| 394 | if (arrayIdx !== null) { |
| 395 | value = value |
| 396 | .replace("[", "") |
| 397 | .replaceAll("]", "") |
| 398 | .split(",") |
| 399 | [arrayIdx].trim(); // TODO: Add fallback for if the commaIdx is out of range |
| 400 | } |
| 401 | |
| 402 | if (!value) return; |
| 403 | |
| 404 | // Remove leading or trailing single or double quotes |
| 405 | value = value.replace(/^["'](.+(?=["']$))["']$/, "$1"); |
| 406 | try { |
| 407 | value = JSON.parse(value); |
| 408 | } catch {} |
| 409 | properties[key].data = value; |
| 410 | } |
| 411 | }); |
| 412 | |
| 413 | return properties; |
| 414 | } |
| 415 | |
| 416 | export function propertiesToChunks(properties: Properties): Chunk[] { |
| 417 | /** |
no outgoing calls
no test coverage detected