kind: AdaptiveDialog
inputs:
  - kind: AutomaticTaskInput
    propertyName: Query
    description: The query that will be used to retrieve knowledge
    entity: StringPrebuiltEntity
    shouldPromptUser: true

  - kind: AutomaticTaskInput
    propertyName: RecordLimit
    description: The maximum number of KB articles to retrieve
    entity: NumberPrebuiltEntity
    shouldPromptUser: true

  - kind: AutomaticTaskInput
    propertyName: KBtoSearch
    name: KBtoSearch
    description: The identifier for the KB to search
    entity: StringPrebuiltEntity
    shouldPromptUser: true

beginDialog:
  kind: OnRedirect
  id: main
  actions:
    - kind: InvokeConnectorAction
      id: invokeConnectorAction_F8zz0n
      input:
        binding:
          fields: text
          kb: =Topic.KBtoSearch
          limit: =Topic.RecordLimit
          query: =Topic.Query

      output:
        kind: SingleVariableOutputBinding
        variable: Topic.GetKnowledgeArticles

      connectionReference: crb50_hrSelfService.shared_service-now.shared-service-now-e874ff4e-674f-4646-85af-62d958aaafe1
      connectionProperties:
        mode: Maker

      operationId: GetKnowledgeArticles

    - kind: ParseValue
      id: Gg2WxN
      variable: Topic.KBReturnData
      valueType:
        kind: Record
        properties:
          articles:
            type:
              kind: Table
              properties:
                fields:
                  type:
                    kind: Record
                    properties:
                      text:
                        type:
                          kind: Record
                          properties:
                            display_value: String
                            label: String
                            name: String
                            type: String
                            value: String

                id: String
                link: String
                number: String
                rank: Number
                score: Number
                snippet: String
                title: String

          meta:
            type:
              kind: Record
              properties:
                properties:
                  type:
                    kind: Record
                    properties:
                      count: Number
                      end: Number
                      fields: String
                      filter: String
                      kb: String
                      language: String
                      query: String
                      start: Number
                      status:
                        type:
                          kind: Record
                          properties:
                            code: Number

                      ts_query_id: String

                type: String

      value: "=\"[]\""

    - kind: SetVariable
      id: setVariable_WqldHY
      variable: Topic.KBReturnData
      value: =Topic.GetKnowledgeArticles

    - kind: SetVariable
      id: setVariable_CFMJ5M
      variable: Topic.KBUsableResponse
      value: "=ForAll(Topic.KBReturnData.articles, {Content: Concatenate(title, \" - \", Substitute(PlainText(fields.text.display_value), \"\\n\\r\", \"\")), ContentLocation: Concatenate(\"https://\",Env.cto_ServiceNowInstanceID,\".service-now.com/kb\", link)})"

    - kind: SearchAndSummarizeContent
      id: 4kMiMT
      variable: Topic.Answer
      userInput: =Topic.Query
      moderationLevel: Medium
      additionalInstructions: When possible, make use of bulleted lists, and use emphasis where appropriate as well.
      customDataSource:
        searchResults: =Topic.KBUsableResponse

inputType:
  properties:
    KBtoSearch:
      displayName: KBtoSearch
      description: The identifier for the KB to search
      type: String

    Query:
      displayName: Query
      description: The query that will be used to retrieve knowledge
      type: String

    RecordLimit:
      displayName: RecordLimit
      description: The maximum number of KB articles to retrieve
      type: Number

outputType:
  properties:
    Answer:
      displayName: Answer
      description: The answer provided by GenAI on ServiceNow knowledge
      type: String