verida/data-connector-server

Handle potential invalid promptSearchResult

Opened this issue · 1 comments

When calling llm/personal with a simple prompt, I got a 500 because databases was undefined on the following line.

if (promptSearchResult.databases.indexOf(SearchType.EMAILS) !== -1) {

Here are the logs

[2024-10-31T15:01:54.612] [INFO] default - {
  search_type: 'keywords',
  keywords: [ 'greeting', 'hello' ],
  timeframe: null,
  databases: null,
  sort: null,
  output_type: null,
  profile_information: null
}
[2024-10-31T15:01:54.613] [INFO] default - Searching by keywords: greeting,hello
[2024-10-31T15:01:54.613] [INFO] default - TypeError: Cannot read properties of null (reading 'indexOf')
    at PromptSearchService.<anonymous> (/Users/aurel/dev/verida/data-connector-server/dist/services/assistants/search.js:46:50)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/aurel/dev/verida/data-connector-server/dist/services/assistants/search.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

promptSearchResult comes from

return <PromptSearchLLMResponse> JSON.parse(response.choices[0].message.content!)

The assertion of a JSON.parse is error-prone. In that case it was assumed databases (and other property) is defined as per the PromptSearchLLMResponse type but it was not.

The assertion is not the root cause of why databases is null but that doesn't help in static code analysis.

Yes, I'm aware this isn't great.

I want to move to using zod to verify the LLM response along with user input, but haven't got to that yet.

For now I'll add some basic checks, but will leave this issue open until we do something more robust.