Skip to content

JSON Parsing Error When AI Prompt Includes Double Quotes #3

@hunterphillips

Description

@hunterphillips

Issue Summary:
When using Digi in a ServiceNow script field, typing @digi and pressing TAB initiates a request that never completes. The request remains in the "New" state and is not processed. This appears to be due to a malformed JSON error in the DigiAPI script include, triggered by double quotes within a configured AI Prompt.

When using the OpenAI - 1.0.3 AI Engine and gpt-3.5-turbo model.

Steps to Reproduce:

  1. In a script field, type @digi <your request>, then press TAB.

  2. Observe the "Processing Digi Request" modal remains indefinitely.

Image
  1. The corresponding Request (x_radi_digi_request) remains in the New state.
Image
  1. Run the following background script to attempt to manually process the request:
var reqId = '<request_sys_id>';
var digiReq = new x_radi_digi.DigiRequest(reqId);
digiReq.process();
  1. The following error is thrown:
Script execution error: Script Identifier: null.null.script, Error Description: Unexpected token in object literal
Evaluator: com.glide.script.RhinoEcmaError: Unexpected token in object literal
   at sys_script_include.9797c7c41b3e2110d4e599f91d4bcb21.script:128 (_getParamValue)

Root Cause:
The error occurs in the DigiAPI Script Include, in the _getParamValue method:

if (paramType === 'array') {
    var anArr = JSON.parse(paramValue);
    return anArr;
}

In this case, an AI Prompt (x_radi_digi_ai_prompt) contains unescaped quotes (") in the Text field:

Type: Format
Text: If the answer includes javascript code, begin the code block with "<getBlockDelimiterBegin>" and end the code block with "<getBlockDelimiterEnd>". 

Proposed Solution:
Handle unescaped quotes when replacing the <getPrompt> embedding.

In DigiAIModel Script Include --> _replaceEmbeddedValues(aString, prompt)

if (strSource.indexOf('<getPrompt>')) {
    if(prompt && typeof prompt === 'string'){
        // Escape quotes in the prompt to prevent JSON parsing errors
        prompt = prompt
            .replace(/\\"/g, "__ESCAPED_QUOTE__") // preserve already escaped quotes
            .replace(/"/g, '\\"')
            .replace(/__ESCAPED_QUOTE__/g, '\\"');
    }
    aString = aString.replace('<getPrompt>', prompt);
}
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions