|
| 1 | +var GetServiceDeskAgentHelpAIUtil = Class.create(); |
| 2 | +GetServiceDeskAgentHelpAIUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, { |
| 3 | + |
| 4 | + /** |
| 5 | + * Main function that processes a search term from a client-side call, |
| 6 | + * sends it to a Databricks-powered AI, and returns the response. |
| 7 | + * This function is intended to assist Service Desk agents. |
| 8 | + * |
| 9 | + * @returns {string} A JSON string containing the AI's response, model details, and metrics. |
| 10 | + */ |
| 11 | + getSearchResults: function() { |
| 12 | + // Defines the use case for logging and metric purposes. |
| 13 | + var usecase = "ServiceDesk Helper"; |
| 14 | + // Gets the current user's Sys ID, though it is not used in the current implementation. |
| 15 | + var user = gs.getUserID(); |
| 16 | + // Retrieves the search term passed from the client-side script. |
| 17 | + var searchText = this.getParameter("sysparm_search_key"); |
| 18 | + // Replaces double quotes with single quotes in the search text to prevent JSON parsing issues. |
| 19 | + searchText = searchText.replaceAll('"', "'"); |
| 20 | + |
| 21 | + var searchObj = { |
| 22 | + "searchValue": searchText.toString() |
| 23 | + }; |
| 24 | + |
| 25 | + // Extracts the raw search value from the object. |
| 26 | + var search = searchObj["searchValue"]; |
| 27 | + |
| 28 | + // This object is structured to create a prompt for another potential AI endpoint (possibly for a brief statement), |
| 29 | + // but it is currently not used. |
| 30 | + var brief_statement_payload = { |
| 31 | + "messages": [{ |
| 32 | + "role": "system", |
| 33 | + "content": "You are an Expert ServiceNow bot that helps the users to create an incident" |
| 34 | + }, |
| 35 | + { |
| 36 | + "role": "user", |
| 37 | + "content": gs.getProperty('user.prompt') + search |
| 38 | + } |
| 39 | + ] |
| 40 | + }; |
| 41 | + |
| 42 | + var databricks_model_response = {}; |
| 43 | + // Calls the internal method to get the response from the Databricks model. |
| 44 | + var response = this.getDataBricksModelResponse(search); |
| 45 | + // UNCOMMENT THIS WHEN WE HAVE A PROPER SOLUTION FOR BRIEF RESPONSE GENERATION |
| 46 | + // var brief_response = this.getBriefResponse(brief_statement_payload); |
| 47 | + // The brief response is hardcoded to an empty JSON object |
| 48 | + // Assigns the model response to the output object. |
| 49 | + databricks_model_response.modelResponse = response; |
| 50 | + // Assigns a hardcoded model ID. |
| 51 | + databricks_model_response.model_id = "Databricks Runbook"; |
| 52 | + |
| 53 | + // Converts the final response object to a JSON string for client-side processing. |
| 54 | + databricks_model_response = JSON.stringify(databricks_model_response); |
| 55 | + // Logs the final JSON string for debugging purposes. |
| 56 | + gs.info("Service Desk Helper Results: Testing value of the final databricks response being sent: " + databricks_model_response); |
| 57 | + |
| 58 | + // Returns the JSON string to the calling client script. |
| 59 | + return databricks_model_response; |
| 60 | + }, |
| 61 | + |
| 62 | + /** |
| 63 | + * This function calls the Databricks endpoint via a Flow Designer action |
| 64 | + * to generate an answer for the user's query. |
| 65 | + * |
| 66 | + * @param {string} search - The user's search query. |
| 67 | + * @returns {string} A JSON string containing the AI's response, the current date, and a trace ID. |
| 68 | + */ |
| 69 | + getDataBricksModelResponse: function(search) { |
| 70 | + try { |
| 71 | + var inputs = {}; |
| 72 | + // Maps the search query to the input expected by the flow action. |
| 73 | + inputs['search_query'] = search; |
| 74 | + |
| 75 | + // Executes the specified Flow Designer action with the provided inputs. |
| 76 | + // The action is run in the foreground, meaning the script will wait for a response. |
| 77 | + var result = sn_fd.FlowAPI.getRunner().action('global.genai_action').inForeground().withInputs(inputs).run(); |
| 78 | + // Retrieves the outputs from the completed flow action. |
| 79 | + var outputs = result.getOutputs(); |
| 80 | + |
| 81 | + // Extracts the model output from the flow action outputs. |
| 82 | + var model_output = outputs['model_output']; |
| 83 | + // Attempts to parse and extract vector response data, though the variable is not used after this line. |
| 84 | + var databricks_vector_response = JSON.parse(model_output).databricks_output.trace.data.spans; |
| 85 | + |
| 86 | + // Logs the raw response from the Databricks model for debugging. |
| 87 | + gs.info("Helper Results: Databricks flow action response: " + JSON.stringify(model_output)); |
| 88 | + |
| 89 | + var current_date = new GlideDateTime(); |
| 90 | + var output = {}; |
| 91 | + // Parses the model output to extract the AI's content. |
| 92 | + output.response = JSON.parse(model_output).choices[0].message.content; |
| 93 | + // Adds the current date to the output object. |
| 94 | + output.date = current_date.getDisplayValue(); |
| 95 | + // Logs the trace ID for tracking purposes. |
| 96 | + gs.info("Helper Results: GEN AI flow action TraceID value: " + JSON.parse(model_output).id); |
| 97 | + // Adds the trace ID to the output object. |
| 98 | + output.traceID = JSON.parse(model_output).id; |
| 99 | + // Returns the constructed output object as a JSON string. |
| 100 | + return JSON.stringify(output); |
| 101 | + |
| 102 | + } catch (ex) { |
| 103 | + // Catches any exceptions during the flow execution and logs an error. |
| 104 | + var message = ex.getMessage(); |
| 105 | + gs.error(message); |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + type: 'GetServiceDeskAgentHelpAIUtil' |
| 110 | +}); |
0 commit comments