prompt --application/pages/page_00010
begin
--   Manifest
--     PAGE: 00010
--   Manifest End
wwv_flow_imp.component_begin (
 p_version_yyyy_mm_dd=>'2024.05.31'
,p_release=>'24.1.2'
,p_default_workspace_id=>5893639279394007
,p_default_application_id=>20011
,p_default_id_offset=>0
,p_default_owner=>'DEMO'
);
wwv_flow_imp_page.create_page(
 p_id=>10
,p_name=>'Chat'
,p_alias=>'CHAT'
,p_step_title=>'Chat'
,p_allow_duplicate_submissions=>'N'
,p_reload_on_submit=>'A'
,p_warn_on_unsaved_changes=>'N'
,p_autocomplete_on_off=>'ON'
,p_javascript_code=>wwv_flow_string.join(wwv_flow_t_varchar2(
'const outputDom = document.getElementById(''output'')',
'outputDom.innerHTML = '''';',
'',
'',
'function complete() {',
'',
'    enableChat();',
'',
'    // console.log(''outputDom'', document.getElementById(''outputDom'').innerHTML)',
'    apex.item(''P10_HIDDEN_OUTPUT'').setValue(',
'        outputDom.innerHTML',
'    );',
'    outputDom.style.display = ''none'';',
'',
'    apex.server.process(',
'        "STORE_CHAT_HISTORY",',
'        {',
'            pageItems: "#P10_HIDDEN_OUTPUT,#P10_MESSAGE_INPUT"',
'        },',
'        {',
'            success: function(data) {',
unistr('                // \5904\7406\6210\529F\540E\7684\64CD\4F5C'),
'                // alert(''success'')',
'                apex.item(''P10_HIDDEN_OUTPUT'').setValue('''');',
'                apex.item(''P10_MESSAGE_INPUT'').setValue('''');',
'                outputDom.innerHTML = '''';',
'                apex.region(''chat-messages'').refresh();',
'            }',
'        }',
'    );',
'',
'}',
'',
'function enableChat() {',
'',
'    chatSubmitButton = document.getElementById(''post-message-btn'')',
'    chatSubmitButton.disabled = false;',
'    apex.item(''P10_MESSAGE_INPUT'').enable();',
'    ',
'',
'}',
'',
'function disableChat() {',
'',
'    chatSubmitButton = document.getElementById(''post-message-btn'')',
'    chatSubmitButton.disabled = true;',
'    apex.item(''P10_MESSAGE_INPUT'').disable();',
unistr('    outputDom.style.display = ''block''; // \663E\793A\8F93\51FA\52A8\753B'),
'',
'}',
'',
'async function postMessage(url, requestPayload) {',
'',
'    const response = await fetch(',
'        url,',
'        {',
unistr('            method: ''POST'', // \8BF7\6C42\65B9\6CD5'),
'            headers: {',
unistr('            ''Content-Type'': ''application/json'', // \8BF7\6C42\5934\FF0C\544A\8BC9\670D\52A1\5668\6570\636E\7684\683C\5F0F'),
'        },',
unistr('        body: JSON.stringify(requestPayload), // \8BF7\6C42\4F53\FF0C\6570\636E\9700\8981\5E8F\5217\5316\4E3A JSON \5B57\7B26\4E32'),
'    });',
'',
'    if (!response.ok) {',
unistr('        // \5982\679C\54CD\5E94\72B6\6001\4E0D\662F 200-299\FF0C\629B\51FA\9519\8BEF'),
'        throw new Error(''Network response was not ok'');',
'    }',
'',
'    console.log(''response'', response)',
'',
'    const responseData = await response.json();',
'',
unistr('    // \68C0\67E5\54CD\5E94\4E2D\7684 result \5B57\6BB5'),
'    if (responseData.result === 0) {',
unistr('        // \7ED3\679C\4E3A 0\FF0C\8868\793A\6210\529F\FF0C\89E3\6790 uuid'),
'        const uuid = responseData.uuid;',
'        console.log(''Success! UUID:'', uuid);',
'        return uuid;',
'    } else {',
unistr('        // \7ED3\679C\975E 0\FF0C\8868\793A\5931\8D25\FF0C\5904\7406\9519\8BEF\4FE1\606F'),
'        console.error(''Error:'', responseData.result_message);',
'        return undefined;',
'    }',
'',
'}',
'',
'const messageQueue = [];',
'let processing = false;',
'',
'function print_animation(data) {',
'',
'    return new Promise((resolve) => {',
'',
unistr('        let delay = 20; // \6BCF\4E2A\5B57\7B26\4E4B\95F4\7684\5EF6\8FDF\65F6\95F4\FF0C\5355\4F4D\662F\6BEB\79D2'),
'        let lastTimestamp = 0;',
'',
'        if (!data) {',
'            resolve();',
'            return;',
'        }',
'',
'        let characters = data.split('''');',
'        let index = 0;',
'',
'        const addNextCharacter = (timestamp) => {',
'            if (!lastTimestamp) lastTimestamp = timestamp;',
'            const elapsed = timestamp - lastTimestamp;',
'',
'            if (elapsed > delay) {',
'                if (index < characters.length) {',
'',
unistr('                    // \62FC\63A5\8F93\51FA'),
'                    outputDom.innerHTML += characters[index];',
'                    ',
'                    index++;',
'                    lastTimestamp = timestamp;',
'                }',
'            }',
'',
'            if (index < characters.length) {',
unistr('                requestAnimationFrame(addNextCharacter); // \8BF7\6C42\4E0B\4E00\5E27'),
'            } else {',
unistr('                resolve(); // \5F53\6240\6709\5B57\7B26\5904\7406\5B8C\540E\89E3\51B3 Promise'),
'            }',
'        };',
'',
unistr('        requestAnimationFrame(addNextCharacter); // \5F00\59CB\5904\7406\7B2C\4E00\4E2A\5B57\7B26'),
'',
unistr('        // console.log(''print_animation\5904\7406\5B8C\6210'')'),
'',
'        outputDom.scrollTop = outputDom.scrollHeight;',
'    });',
'}',
'',
'function processQueue() {',
'    if (processing) {',
unistr('        // console.log(''\5DF2\7ECF\6709\4E8B\4EF6\5904\7406\4E2D\FF0C\8FD4\56DE'')'),
unistr('        return;  // \68C0\67E5 processing \6807\5FD7'),
'    } else {',
unistr('        // console.log(''\6CA1\6709\4E8B\4EF6\5904\7406\4E2D\FF0C\53EF\4EE5\5F00\59CB\5904\7406'')'),
'    }',
unistr('    processing = true;       // \8BBE\7F6E processing \6807\5FD7\4E3A true'),
'    ',
'    (async function() {',
'        while (messageQueue.length > 0) {',
'            // console.log(''messageQueue.length'', messageQueue.length);',
unistr('            const event = messageQueue.shift();  // \4ECE\961F\5217\4E2D\79FB\9664\7B2C\4E00\4E2A\4E8B\4EF6'),
'            if (event.type === ''message'') {',
'                // console.log(''Processing message:'', event.data);',
unistr('                // \6A21\62DF\8017\65F6\5904\7406'),
'                await print_animation(event.data);',
unistr('                // console.log(''Promise\5904\7406\5B8C\6210'')'),
'                //print_animation(event.data)',
'            } else if (event.type === ''end'') {',
'                // console.log(''Processing end event:'', event.data);',
'                complete()',
unistr('                eventSource.close();  // \5173\95ED EventSource \8FDE\63A5'),
'                // console.log(''EventSource closed'');',
'            }',
'        }',
unistr('        // console.log(''\672C\6B21\5904\7406\5B8C\6210\FF0C\6E05\7406\6807\8BC6\4F4D'')'),
unistr('        processing = false;  // \5904\7406\5B8C\6240\6709\4E8B\4EF6\540E\91CD\7F6E processing \6807\5FD7'),
'    })();',
'}',
'',
'async function getAnswer(url, uuid) {',
'',
'    eventSource = new EventSource(url + ''?uuid='' + uuid)',
'',
'    eventSource.addEventListener(''end'', function(event) {',
'        // console.log(''Stream ending:'', event.data);',
unistr('        // \5C06\6253\5370\52A8\753B\6DFB\52A0\5230\961F\5217'),
'        messageQueue.push({ type: ''end'', data: event.data });',
'        processQueue();',
'    });',
'',
'    eventSource.onmessage = function(event) {',
'        // console.log(''Stream data:'',event.data)',
unistr('        // \5C06\6253\5370\52A8\753B\6DFB\52A0\5230\961F\5217'),
'        messageQueue.push({ type: ''message'', data: event.data });',
'        processQueue();',
'    }',
'',
'    eventSource.onerror = function(event) {',
'',
'        // console.error("Error from server: ", event);',
'',
'        switch (this.readyState) {',
'            /*case EventSource.CONNECTING:',
'                console.error(''Connecting state, possibly a temporary network issue.'');',
'                break;',
'            case EventSource.OPEN:',
'                console.error(''Open state, this should not trigger an error event.'');',
'                break;*/',
'            case EventSource.CLOSED:',
'                console.error(''Closed state, connection is likely permanently broken.'');',
'',
'                complete();',
'                eventSource.close();',
'                break;',
'        }',
'    };',
'',
'}'))
,p_inline_css=>wwv_flow_string.join(wwv_flow_t_varchar2(
'/* Scroll Results Only in Side Column */',
'.t-Body-side {',
'    display: flex;',
'    flex-direction: column;',
'    overflow: hidden;',
'}',
'.search-results {',
'    flex: 1;',
'    overflow: auto;',
'}',
'/* Format Search Region */',
'.search-region {',
'    border-bottom: 1px solid rgba(0,0,0,.1);',
'    flex-shrink: 0;',
'}',
'',
'/* BEGIN comments/chat region styles */',
'.t-Chat .t-Chat--own .t-Comments-icon {',
'  margin-right: 0;',
'  margin-left: 12px;',
'  fill: red;',
'}',
'',
'.t-Chat .t-Chat--own {',
'  flex-direction: row-reverse;',
'}',
'',
'.t-Chat .t-Chat--own .t-Comments-body {',
'  align-items: flex-end;',
'}',
'',
'.t-Chat .t-Chat--own .t-Comments-comment:after {',
'  border-left-color: var(--ut-comment-chat-background-color);',
'  border-right-color: rgba(0,0,0,0);',
'  right: none;',
'  left: 100%;',
'}',
'',
'.t-Chat .t-Comments {',
'   height: 470px;',
'   overflow: scroll;',
'}',
'/* END comments/chat region styles */ ',
'',
'',
'#output {',
unistr('    white-space: pre-wrap; /* \4FDD\7559\6362\884C\548C\7A7A\683C */'),
'    border: 1px solid #ccc;',
'    padding: 10px;',
'    height: 300px;',
'    overflow-y: auto;',
'}'))
,p_step_template=>wwv_flow_imp.id(10042885068629219)
,p_page_template_options=>'#DEFAULT#'
,p_page_is_public_y_n=>'Y'
,p_protection_level=>'C'
,p_page_component_map=>'03'
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10395373409674633)
,p_plug_name=>'Chat Message Region'
,p_region_template_options=>'#DEFAULT#:js-showMaximizeButton:t-Region--scrollBody'
,p_plug_template=>wwv_flow_imp.id(10132850710629585)
,p_plug_display_sequence=>60
,p_location=>null
,p_plug_display_condition_type=>'ITEM_IS_NOT_NULL'
,p_plug_display_when_condition=>'P10_CHAT_ID'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'expand_shortcuts', 'N',
  'output_as', 'HTML')).to_clob
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10395478526674634)
,p_plug_name=>'Buttons'
,p_parent_plug_id=>wwv_flow_imp.id(10395373409674633)
,p_region_template_options=>'#DEFAULT#'
,p_plug_template=>wwv_flow_imp.id(10066208585629345)
,p_plug_display_sequence=>20
,p_plug_display_point=>'SUB_REGIONS'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'expand_shortcuts', 'N',
  'output_as', 'HTML')).to_clob
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10637253369007730)
,p_plug_name=>'P10_OUTPUT_RN'
,p_parent_plug_id=>wwv_flow_imp.id(10395478526674634)
,p_region_template_options=>'#DEFAULT#:t-Region--removeHeader js-removeLandmark:t-Region--noBorder:t-Region--scrollBody:margin-left-none:margin-right-none'
,p_plug_template=>wwv_flow_imp.id(10132850710629585)
,p_plug_display_sequence=>10
,p_location=>null
,p_plug_source=>'<div id="output" placeholder="This will output" style="height: 150px; display: none; overflow-y: auto;"></div>'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'expand_shortcuts', 'N',
  'output_as', 'HTML')).to_clob
);
wwv_flow_imp_page.create_report_region(
 p_id=>wwv_flow_imp.id(10472895874210821)
,p_name=>'Chat Messages'
,p_region_name=>'chat-messages'
,p_parent_plug_id=>wwv_flow_imp.id(10395373409674633)
,p_template=>wwv_flow_imp.id(10066208585629345)
,p_display_sequence=>10
,p_region_css_classes=>'t-Chat'
,p_region_template_options=>'#DEFAULT#'
,p_component_template_options=>'#DEFAULT#:t-Comments--chat:t-Comments--iconsRounded'
,p_display_point=>'SUB_REGIONS'
,p_source_type=>'NATIVE_SQL_REPORT'
,p_query_type=>'SQL'
,p_source=>wwv_flow_string.join(wwv_flow_t_varchar2(
'SELECT',
'    NULL                    AS user_name,',
'    apex_string.get_initials(ccm.user_name)           AS user_icon,',
'    ccm.message_text        AS comment_text,',
'    TO_CHAR(ccm.message_time, ''YYYY-MM-DD HH24:MI:SS'')        AS comment_date,',
'    CASE ccm.message_type',
'        WHEN ''M'' THEN ''t-Chat--own''',
'        ELSE NULL',
'    END                     AS comment_modifiers,',
'    NULL                    AS actions,',
'    NULL                    AS attribute_1,',
'    NULL                    AS attribute_2,',
'    NULL                    AS attribute_3,',
'    NULL                    AS attribute_4',
'FROM',
'    cb_chat_messages ccm',
'WHERE',
'    ccm.chat_id = :P10_CHAT_ID',
'ORDER BY',
'    ccm.message_time'))
,p_display_when_condition=>'P10_CHAT_ID'
,p_display_condition_type=>'ITEM_IS_NOT_NULL'
,p_ajax_enabled=>'Y'
,p_lazy_loading=>false
,p_query_row_template=>wwv_flow_imp.id(10162452709629662)
,p_query_num_rows=>10000
,p_query_options=>'DERIVED_REPORT_COLUMNS'
,p_query_no_data_found=>'No data found.'
,p_csv_output=>'N'
,p_prn_output=>'N'
,p_sort_null=>'L'
,p_plug_query_strip_html=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10475670930210835)
,p_query_column_id=>1
,p_column_alias=>'USER_NAME'
,p_column_display_sequence=>6
,p_column_heading=>'User Name'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394090300674620)
,p_query_column_id=>2
,p_column_alias=>'USER_ICON'
,p_column_display_sequence=>16
,p_column_heading=>'User Icon'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394136149674621)
,p_query_column_id=>3
,p_column_alias=>'COMMENT_TEXT'
,p_column_display_sequence=>26
,p_column_heading=>'Comment Text'
,p_heading_alignment=>'LEFT'
,p_display_as=>'RICH_TEXT'
,p_attribute_01=>'MARKDOWN'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394259068674622)
,p_query_column_id=>4
,p_column_alias=>'COMMENT_DATE'
,p_column_display_sequence=>36
,p_column_heading=>'Comment Date'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394319005674623)
,p_query_column_id=>5
,p_column_alias=>'COMMENT_MODIFIERS'
,p_column_display_sequence=>46
,p_column_heading=>'Comment Modifiers'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394461299674624)
,p_query_column_id=>6
,p_column_alias=>'ACTIONS'
,p_column_display_sequence=>56
,p_column_heading=>'Actions'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394581055674625)
,p_query_column_id=>7
,p_column_alias=>'ATTRIBUTE_1'
,p_column_display_sequence=>66
,p_column_heading=>'Attribute 1'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394651971674626)
,p_query_column_id=>8
,p_column_alias=>'ATTRIBUTE_2'
,p_column_display_sequence=>76
,p_column_heading=>'Attribute 2'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394792492674627)
,p_query_column_id=>9
,p_column_alias=>'ATTRIBUTE_3'
,p_column_display_sequence=>86
,p_column_heading=>'Attribute 3'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10394877901674628)
,p_query_column_id=>10
,p_column_alias=>'ATTRIBUTE_4'
,p_column_display_sequence=>96
,p_column_heading=>'Attribute 4'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10456004438210708)
,p_plug_name=>'Breadcrumb'
,p_region_template_options=>'#DEFAULT#:t-BreadcrumbRegion--useBreadcrumbTitle'
,p_component_template_options=>'#DEFAULT#'
,p_plug_template=>wwv_flow_imp.id(10145238625629609)
,p_plug_display_sequence=>10
,p_plug_display_point=>'REGION_POSITION_01'
,p_menu_id=>wwv_flow_imp.id(10029580740629034)
,p_plug_source_type=>'NATIVE_BREADCRUMB'
,p_menu_template_id=>wwv_flow_imp.id(10207649640629828)
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10457552002210717)
,p_plug_name=>'Search'
,p_region_css_classes=>'search-region padding-md'
,p_region_template_options=>'#DEFAULT#:t-Form--stretchInputs'
,p_plug_template=>wwv_flow_imp.id(10066208585629345)
,p_plug_display_sequence=>10
,p_plug_display_point=>'REGION_POSITION_02'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'expand_shortcuts', 'N',
  'output_as', 'HTML',
  'show_line_breaks', 'Y')).to_clob
);
wwv_flow_imp_page.create_report_region(
 p_id=>wwv_flow_imp.id(10458304850210724)
,p_name=>'Master Records'
,p_template=>wwv_flow_imp.id(10067642234629347)
,p_display_sequence=>20
,p_region_css_classes=>'search-results'
,p_region_template_options=>'#DEFAULT#'
,p_component_template_options=>'t-MediaList--showDesc:t-MediaList--stack'
,p_display_point=>'REGION_POSITION_02'
,p_source_type=>'NATIVE_SQL_REPORT'
,p_query_type=>'SQL'
,p_source=>wwv_flow_string.join(wwv_flow_t_varchar2(
'select "CHAT_ID",',
'    null LINK_CLASS,',
'    apex_page.get_url(p_items => ''P10_CHAT_ID'', p_values => "CHAT_ID") LINK,',
'    null ICON_CLASS,',
'    null LINK_ATTR,',
'    null ICON_COLOR_CLASS,',
'    case when coalesce(:P10_CHAT_ID,''0'') = "CHAT_ID"',
'      then ''is-active'' ',
'      else '' ''',
'    end LIST_CLASS,',
'    (substr("CHAT_NAME", 1, 50)||( case when length("CHAT_NAME") > 50 then ''...'' else '''' end )) LIST_TITLE,',
'    (substr("CHAT_START_DATE", 1, 50)||( case when length("CHAT_START_DATE") > 50 then ''...'' else '''' end )) LIST_TEXT,',
'    null LIST_BADGE',
'from "CB_CHATS" x',
'where (:P10_SEARCH is null',
'        or upper(x."CHAT_NAME") like ''%''||upper(:P10_SEARCH)||''%''',
'        or upper(x."CHAT_START_DATE") like ''%''||upper(:P10_SEARCH)||''%''',
'    )',
'order by "CHAT_NAME"'))
,p_ajax_enabled=>'Y'
,p_ajax_items_to_submit=>'P10_SEARCH'
,p_lazy_loading=>false
,p_query_row_template=>wwv_flow_imp.id(10167801265629674)
,p_query_num_rows=>1000
,p_query_options=>'DERIVED_REPORT_COLUMNS'
,p_query_no_data_found=>'<div class="u-tC">No data found.</div>'
,p_query_row_count_max=>500
,p_csv_output=>'N'
,p_prn_output=>'N'
,p_sort_null=>'L'
,p_plug_query_strip_html=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10459089505210740)
,p_query_column_id=>1
,p_column_alias=>'CHAT_ID'
,p_column_display_sequence=>1
,p_column_heading=>'CHAT_ID'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10459442327210741)
,p_query_column_id=>2
,p_column_alias=>'LINK_CLASS'
,p_column_display_sequence=>2
,p_column_heading=>'LINK_CLASS'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10459842053210742)
,p_query_column_id=>3
,p_column_alias=>'LINK'
,p_column_display_sequence=>3
,p_column_heading=>'LINK'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10460232727210743)
,p_query_column_id=>4
,p_column_alias=>'ICON_CLASS'
,p_column_display_sequence=>4
,p_column_heading=>'ICON_CLASS'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10460685887210744)
,p_query_column_id=>5
,p_column_alias=>'LINK_ATTR'
,p_column_display_sequence=>5
,p_column_heading=>'LINK_ATTR'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10461042409210745)
,p_query_column_id=>6
,p_column_alias=>'ICON_COLOR_CLASS'
,p_column_display_sequence=>6
,p_column_heading=>'ICON_COLOR_CLASS'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10461443009210746)
,p_query_column_id=>7
,p_column_alias=>'LIST_CLASS'
,p_column_display_sequence=>7
,p_column_heading=>'LIST_CLASS'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10461890924210747)
,p_query_column_id=>8
,p_column_alias=>'LIST_TITLE'
,p_column_display_sequence=>8
,p_column_heading=>'LIST_TITLE'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10462261827210748)
,p_query_column_id=>9
,p_column_alias=>'LIST_TEXT'
,p_column_display_sequence=>9
,p_column_heading=>'LIST_TEXT'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10462689445210749)
,p_query_column_id=>10
,p_column_alias=>'LIST_BADGE'
,p_column_display_sequence=>10
,p_column_heading=>'LIST_BADGE'
,p_use_as_row_header=>'N'
);
wwv_flow_imp_page.create_report_region(
 p_id=>wwv_flow_imp.id(10463098458210771)
,p_name=>'Chat'
,p_template=>wwv_flow_imp.id(10132850710629585)
,p_display_sequence=>40
,p_region_css_classes=>'js-master-region'
,p_region_template_options=>'#DEFAULT#:t-Region--noPadding:t-Region--scrollBody'
,p_component_template_options=>'#DEFAULT#:t-AVPList--leftAligned'
,p_source_type=>'NATIVE_SQL_REPORT'
,p_query_type=>'SQL'
,p_source=>wwv_flow_string.join(wwv_flow_t_varchar2(
'SELECT',
'    CHAT_ID AS PK_CHAT_ID,',
'    CHAT_NAME,',
'    MODEL_ID,',
'    (',
'        SELECT',
'            cb.model_name',
'        FROM',
'            CB_MODELS cb',
'        WHERE',
'            cb.model_id = cc.model_id',
'    ) AS MODEL_NAME,',
'    CHAT_START_DATE,',
'    CREATED_BY,',
'    CREATION_DATE,',
'    LAST_UPDATED_BY,',
'    LAST_UPDATE_DATE',
'FROM',
'    CB_CHATS cc',
'WHERE',
'    cc."CHAT_ID" = :P10_CHAT_ID'))
,p_display_when_condition=>'P10_CHAT_ID'
,p_display_condition_type=>'ITEM_IS_NOT_NULL'
,p_ajax_enabled=>'Y'
,p_lazy_loading=>false
,p_query_row_template=>wwv_flow_imp.id(10174042807629688)
,p_query_num_rows=>15
,p_query_options=>'DERIVED_REPORT_COLUMNS'
,p_query_num_rows_type=>'NEXT_PREVIOUS_LINKS'
,p_pagination_display_position=>'BOTTOM_RIGHT'
,p_csv_output=>'N'
,p_prn_output=>'N'
,p_sort_null=>'L'
,p_plug_query_strip_html=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10635844185007716)
,p_query_column_id=>1
,p_column_alias=>'PK_CHAT_ID'
,p_column_display_sequence=>10
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10635905991007717)
,p_query_column_id=>2
,p_column_alias=>'CHAT_NAME'
,p_column_display_sequence=>20
,p_column_heading=>'Chat Name'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_disable_sort_column=>'N'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636054020007718)
,p_query_column_id=>3
,p_column_alias=>'MODEL_ID'
,p_column_display_sequence=>30
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636119781007719)
,p_query_column_id=>4
,p_column_alias=>'MODEL_NAME'
,p_column_display_sequence=>40
,p_column_heading=>'Model Name'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_disable_sort_column=>'N'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636282310007720)
,p_query_column_id=>5
,p_column_alias=>'CHAT_START_DATE'
,p_column_display_sequence=>50
,p_column_heading=>'Chat Start Date'
,p_use_as_row_header=>'N'
,p_heading_alignment=>'LEFT'
,p_disable_sort_column=>'N'
,p_derived_column=>'N'
,p_include_in_export=>'Y'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636335895007721)
,p_query_column_id=>6
,p_column_alias=>'CREATED_BY'
,p_column_display_sequence=>60
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636403008007722)
,p_query_column_id=>7
,p_column_alias=>'CREATION_DATE'
,p_column_display_sequence=>70
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636546097007723)
,p_query_column_id=>8
,p_column_alias=>'LAST_UPDATED_BY'
,p_column_display_sequence=>80
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_report_columns(
 p_id=>wwv_flow_imp.id(10636614032007724)
,p_query_column_id=>9
,p_column_alias=>'LAST_UPDATE_DATE'
,p_column_display_sequence=>90
,p_hidden_column=>'Y'
,p_derived_column=>'N'
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10472411468210819)
,p_plug_name=>'Region Display Selector'
,p_region_css_classes=>'js-detail-rds'
,p_region_template_options=>'#DEFAULT#:margin-bottom-md'
,p_plug_template=>wwv_flow_imp.id(10066208585629345)
,p_plug_display_sequence=>50
,p_include_in_reg_disp_sel_yn=>'Y'
,p_location=>null
,p_plug_source_type=>'NATIVE_DISPLAY_SELECTOR'
,p_plug_query_num_rows=>15
,p_plug_display_condition_type=>'ITEM_IS_NOT_NULL'
,p_plug_display_when_condition=>'P10_CHAT_ID'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'display_region_icons', 'N',
  'include_show_all', 'Y',
  'rds_mode', 'STANDARD',
  'remember_selection', 'N')).to_clob
);
wwv_flow_imp_page.create_page_plug(
 p_id=>wwv_flow_imp.id(10499288964210986)
,p_plug_name=>'No Record Selected'
,p_region_template_options=>'#DEFAULT#'
,p_plug_template=>wwv_flow_imp.id(10066208585629345)
,p_plug_display_sequence=>70
,p_location=>null
,p_plug_source=>'No Record Selected'
,p_plug_display_condition_type=>'ITEM_IS_NULL'
,p_plug_display_when_condition=>'P10_CHAT_ID'
,p_attributes=>wwv_flow_t_plugin_attributes(wwv_flow_t_varchar2(
  'expand_shortcuts', 'N',
  'output_as', 'HTML',
  'show_line_breaks', 'Y')).to_clob
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(10393635094674616)
,p_button_sequence=>40
,p_button_plug_id=>wwv_flow_imp.id(10395478526674634)
,p_button_name=>'SUBMIT_MESSAGE'
,p_button_static_id=>'post-message-btn'
,p_button_action=>'DEFINED_BY_DA'
,p_button_template_options=>'#DEFAULT#:t-Button--large'
,p_button_template_id=>wwv_flow_imp.id(10205325854629788)
,p_button_is_hot=>'Y'
,p_button_image_alt=>'Submit Message'
,p_warn_on_unsaved_changes=>null
,p_icon_css_classes=>'fa-chevron-circle-right'
,p_grid_column_attributes=>'style="align-self: center;"'
,p_grid_column_css_classes=>'col col-2 col-xxs-2 col-xs-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 col-xxl-1 col-end'
,p_grid_new_row=>'N'
,p_grid_new_column=>'Y'
,p_grid_column_span=>2
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(10637622415007734)
,p_button_sequence=>50
,p_button_plug_id=>wwv_flow_imp.id(10395478526674634)
,p_button_name=>'SUBMIT_MESSAGE_SSE'
,p_button_static_id=>'ChatSubmitButton'
,p_button_action=>'DEFINED_BY_DA'
,p_button_template_options=>'#DEFAULT#'
,p_button_template_id=>wwv_flow_imp.id(10206077575629823)
,p_button_image_alt=>'Submit Message Sse'
,p_warn_on_unsaved_changes=>null
,p_button_condition_type=>'NEVER'
,p_grid_new_row=>'Y'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(10499745412210994)
,p_button_sequence=>20
,p_button_plug_id=>wwv_flow_imp.id(10463098458210771)
,p_button_name=>'EDIT'
,p_button_static_id=>'edit_master_btn'
,p_button_action=>'REDIRECT_PAGE'
,p_button_template_options=>'#DEFAULT#:t-Button--noUI:t-Button--iconLeft'
,p_button_template_id=>wwv_flow_imp.id(10206146706629823)
,p_button_image_alt=>'Edit'
,p_button_position=>'EDIT'
,p_button_alignment=>'RIGHT'
,p_button_redirect_url=>'f?p=&APP_ID.:15:&APP_SESSION.::&DEBUG.:RP,15:P15_CHAT_ID:&P10_CHAT_ID.'
,p_icon_css_classes=>'fa-pencil-square-o'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(10456874210210716)
,p_button_sequence=>10
,p_button_plug_id=>wwv_flow_imp.id(10456004438210708)
,p_button_name=>'RESET'
,p_button_action=>'REDIRECT_PAGE'
,p_button_template_options=>'#DEFAULT#:t-Button--noUI:t-Button--iconLeft:t-Button--gapRight'
,p_button_template_id=>wwv_flow_imp.id(10206146706629823)
,p_button_image_alt=>'Reset'
,p_button_position=>'NEXT'
,p_button_alignment=>'RIGHT'
,p_button_redirect_url=>'f?p=&APP_ID.:10:&APP_SESSION.:RESET:&DEBUG.:RP,10::'
,p_icon_css_classes=>'fa-undo-alt'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(10457206785210716)
,p_button_sequence=>20
,p_button_plug_id=>wwv_flow_imp.id(10456004438210708)
,p_button_name=>'CREATE'
,p_button_action=>'REDIRECT_PAGE'
,p_button_template_options=>'#DEFAULT#:t-Button--iconLeft'
,p_button_template_id=>wwv_flow_imp.id(10206146706629823)
,p_button_is_hot=>'Y'
,p_button_image_alt=>'Create'
,p_button_position=>'NEXT'
,p_button_alignment=>'RIGHT'
,p_button_redirect_url=>'f?p=&APP_ID.:15:&APP_SESSION.::&DEBUG.:RP,15::'
,p_icon_css_classes=>'fa-plus'
);
wwv_flow_imp_page.create_page_button(
 p_id=>wwv_flow_imp.id(5832206404016036)
,p_button_sequence=>10
,p_button_plug_id=>wwv_flow_imp.id(10472895874210821)
,p_button_name=>'CLEAR'
,p_button_action=>'DEFINED_BY_DA'
,p_button_template_options=>'#DEFAULT#:t-Button--tiny'
,p_button_template_id=>wwv_flow_imp.id(10205325854629788)
,p_button_is_hot=>'Y'
,p_button_image_alt=>'Clear'
,p_button_position=>'TOP'
,p_button_alignment=>'RIGHT'
,p_warn_on_unsaved_changes=>null
,p_confirm_message=>'Do you want to clear chat history?'
,p_confirm_style=>'danger'
,p_icon_css_classes=>'fa-remove'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10393541218674615)
,p_name=>'P10_MESSAGE_INPUT'
,p_data_type=>'CLOB'
,p_item_sequence=>30
,p_item_plug_id=>wwv_flow_imp.id(10395478526674634)
,p_placeholder=>'Please input you question here...'
,p_display_as=>'NATIVE_TEXTAREA'
,p_cSize=>30
,p_cHeight=>3
,p_field_template=>wwv_flow_imp.id(10203583736629779)
,p_item_template_options=>'#DEFAULT#'
,p_attribute_01=>'Y'
,p_attribute_02=>'Y'
,p_attribute_03=>'Y'
,p_attribute_04=>'BOTH'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10458056311210724)
,p_name=>'P10_SEARCH'
,p_item_sequence=>10
,p_item_plug_id=>wwv_flow_imp.id(10457552002210717)
,p_prompt=>'Search'
,p_placeholder=>'Search...'
,p_display_as=>'NATIVE_TEXT_FIELD'
,p_cSize=>30
,p_label_alignment=>'RIGHT'
,p_field_template=>wwv_flow_imp.id(10203229260629774)
,p_item_icon_css_classes=>'fa-search'
,p_item_template_options=>'#DEFAULT#:t-Form-fieldContainer--large:t-Form-fieldContainer--postTextBlock'
,p_attribute_01=>'N'
,p_attribute_02=>'N'
,p_attribute_03=>'N'
,p_attribute_04=>'TEXT'
,p_attribute_05=>'BOTH'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10472110895210818)
,p_name=>'P10_CHAT_ID'
,p_item_sequence=>10
,p_display_as=>'NATIVE_HIDDEN'
,p_protection_level=>'S'
,p_attribute_01=>'Y'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10638005879007738)
,p_name=>'P10_HIDDEN_OUTPUT'
,p_item_sequence=>20
,p_item_plug_id=>wwv_flow_imp.id(10395478526674634)
,p_display_as=>'NATIVE_HIDDEN'
,p_attribute_01=>'Y'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10638344214007741)
,p_name=>'P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL'
,p_item_sequence=>20
,p_use_cache_before_default=>'NO'
,p_item_default=>wwv_flow_string.join(wwv_flow_t_varchar2(
'SELECT',
'    APEX_APP_SETTING.GET_VALUE(''GEN_AI_GET_ANSWER_FROM_TONGYI_URL'')',
'FROM',
'    dual'))
,p_item_default_type=>'SQL_QUERY'
,p_display_as=>'NATIVE_HIDDEN'
,p_attribute_01=>'Y'
);
wwv_flow_imp_page.create_page_item(
 p_id=>wwv_flow_imp.id(10638450444007742)
,p_name=>'P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL'
,p_item_sequence=>30
,p_use_cache_before_default=>'NO'
,p_item_default=>wwv_flow_string.join(wwv_flow_t_varchar2(
'SELECT',
'    APEX_APP_SETTING.GET_VALUE(''GEN_AI_SEND_QUESTION_TO_TONGYI_URL'')',
'FROM',
'    dual'))
,p_item_default_type=>'SQL_QUERY'
,p_display_as=>'NATIVE_HIDDEN'
,p_attribute_01=>'Y'
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10500071184210994)
,p_name=>'Dialog Closed'
,p_event_sequence=>10
,p_triggering_element_type=>'REGION'
,p_triggering_region_id=>wwv_flow_imp.id(10463098458210771)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'apexafterclosedialog'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10500650879210995)
,p_event_id=>wwv_flow_imp.id(10500071184210994)
,p_event_result=>'TRUE'
,p_action_sequence=>20
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_REFRESH'
,p_affected_elements_type=>'REGION'
,p_affected_region_id=>wwv_flow_imp.id(10463098458210771)
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10501152899210996)
,p_event_id=>wwv_flow_imp.id(10500071184210994)
,p_event_result=>'TRUE'
,p_action_sequence=>30
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>'apex.message.showPageSuccess(''Cb Chats row(s) updated.'');'
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10472956325210822)
,p_name=>'Dialog Closed'
,p_event_sequence=>40
,p_triggering_element_type=>'REGION'
,p_triggering_region_id=>wwv_flow_imp.id(10472895874210821)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'apexafterclosedialog'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10483435196210872)
,p_event_id=>wwv_flow_imp.id(10472956325210822)
,p_event_result=>'TRUE'
,p_action_sequence=>10
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_REFRESH'
,p_affected_elements_type=>'REGION'
,p_affected_region_id=>wwv_flow_imp.id(10472895874210821)
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10483932672210874)
,p_event_id=>wwv_flow_imp.id(10472956325210822)
,p_event_result=>'TRUE'
,p_action_sequence=>20
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>'apex.message.showPageSuccess(''Cb Chat Messages row(s) updated.'');'
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10500148767210994)
,p_name=>'Perform Search'
,p_event_sequence=>150
,p_triggering_element_type=>'ITEM'
,p_triggering_element=>'P10_SEARCH'
,p_triggering_condition_type=>'JAVASCRIPT_EXPRESSION'
,p_triggering_expression=>'this.browserEvent.which === apex.jQuery.ui.keyCode.ENTER'
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'keypress'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10501841758210998)
,p_event_id=>wwv_flow_imp.id(10500148767210994)
,p_event_result=>'TRUE'
,p_action_sequence=>10
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_REFRESH'
,p_affected_elements_type=>'REGION'
,p_affected_region_id=>wwv_flow_imp.id(10458304850210724)
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10502364986210999)
,p_event_id=>wwv_flow_imp.id(10500148767210994)
,p_event_result=>'TRUE'
,p_action_sequence=>30
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_CANCEL_EVENT'
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10393731072674617)
,p_name=>'Post Message on button click'
,p_event_sequence=>160
,p_triggering_element_type=>'BUTTON'
,p_triggering_button_id=>wwv_flow_imp.id(10393635094674616)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'click'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5831601942016030)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>10
,p_execute_on_page_init=>'N'
,p_name=>'Disable Message Input'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>'apex.item("P10_MESSAGE_INPUT").disable();'
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5831741657016031)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>20
,p_execute_on_page_init=>'Y'
,p_name=>'Disable Submit Message'
,p_action=>'NATIVE_DISABLE'
,p_affected_elements_type=>'BUTTON'
,p_affected_button_id=>wwv_flow_imp.id(10393635094674616)
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10393838366674618)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>40
,p_execute_on_page_init=>'N'
,p_name=>'Send Message'
,p_action=>'NATIVE_EXECUTE_PLSQL_CODE'
,p_attribute_01=>wwv_flow_string.join(wwv_flow_t_varchar2(
'DECLARE',
'    lv_model_name       CB_MODELS.MODEL_NAME%TYPE;',
'    lc_message_text     CLOB;',
'    lv_retcode          VARCHAR2(1);',
'    lv_errmsg           VARCHAR2(2000);',
'BEGIN',
'',
'    lc_message_text := :P10_MESSAGE_INPUT;',
'',
'    -- Insert Question',
'    INSERT INTO cb_chat_messages(',
'        chat_id,',
'        message_type,',
'        message_text,',
'        user_name,',
'        created_by,',
'        last_updated_by',
'    )',
'    VALUES(',
'        :P10_CHAT_ID,       -- chat_id',
'        ''U'',                -- message_type,',
'        lc_message_text,    -- message_text,',
'        :APP_USER,          -- user_name,',
'        :APP_USER,          -- created_by,',
'        :APP_USER           -- last_updated_by',
'    );',
'',
'    SELECT',
'        cb.model_name',
'    INTO',
'        lv_model_name',
'    FROM',
'        cb_models cb,',
'        cb_chats cc',
'    WHERE',
'        cb.model_id = cc.model_id',
'    AND cc.chat_id = :P10_CHAT_ID;',
'',
'    SEND_MESSAGE_TO_GEN_AI(',
'        ov_retcode      => lv_retcode,',
'        ov_errmsg       => lv_errmsg,',
'        in_chat_id      => :P10_CHAT_ID,',
'        in_chat_model   => lv_model_name,',
'        iv_app_user     => :APP_USER',
'    );',
'',
'    IF (lv_retcode = ''0'') THEN',
'        NULL;',
'    ELSE',
'        APEX_ERROR.ADD_ERROR(',
'            p_message      => ''Exception -> '' || lv_errmsg,',
'            p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'        );',
'    END IF;',
'',
'    -- COMMIT;',
'',
'EXCEPTION',
'    WHEN OTHERS THEN',
'        APEX_ERROR.ADD_ERROR(',
'            p_message      => ''Exception -> '' || SQLERRM || dbms_utility.format_error_backtrace,',
'            p_display_location => APEX_ERROR.C_INLINE_IN_NOTIFICATION',
'        );',
'',
'END;'))
,p_attribute_02=>'P10_MESSAGE_INPUT'
,p_attribute_05=>'PLSQL'
,p_stop_execution_on_error=>'N'
,p_wait_for_result=>'Y'
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10395267346674632)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>50
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>wwv_flow_string.join(wwv_flow_t_varchar2(
'apex.message.showPageSuccess("Message sent.");',
'apex.region("chat-messages").refresh();',
'apex.item("P10_MESSAGE_INPUT").setValue("");',
'apex.item("P10_MESSAGE_INPUT").enable();',
'apex.item("P10_MESSAGE_INPUT").setFocus();',
''))
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5831800843016032)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>60
,p_execute_on_page_init=>'Y'
,p_name=>'Enable Submit Message'
,p_action=>'NATIVE_ENABLE'
,p_affected_elements_type=>'BUTTON'
,p_affected_button_id=>wwv_flow_imp.id(10393635094674616)
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5832640676016040)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>70
,p_execute_on_page_init=>'N'
,p_name=>'Clear Message Input'
,p_action=>'NATIVE_CLEAR'
,p_affected_elements_type=>'ITEM'
,p_affected_elements=>'P10_MESSAGE_INPUT'
,p_server_condition_type=>'NEVER'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10637888411007736)
,p_event_id=>wwv_flow_imp.id(10393731072674617)
,p_event_result=>'TRUE'
,p_action_sequence=>80
,p_execute_on_page_init=>'N'
,p_name=>'Submit Message SSE'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>wwv_flow_string.join(wwv_flow_t_varchar2(
'async function callGenAIStream(requestPayload) {',
'',
'    console.log(''callGenAIStream'')',
'    // console.log(''P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL'', apex.item(''P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL'').getValue(''''));',
'    // console.log(''P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL'', apex.item(''P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL'').getValue(''''));',
'',
unistr('    // \68C0\67E5Web Service\7684URL\662F\5426\8BBE\5B9A'),
'    const send_question_url = apex.item(''P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL'').getValue('''')',
'    const get_answer_url = apex.item(''P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL'').getValue('''')',
'',
'    if (send_question_url === null || send_question_url === '''') {',
'        alert(''GEN_AI_SEND_QUESTION_TO_TONGYI_URL not set!'')',
'        return;',
'    }',
'',
'    if (get_answer_url === null || get_answer_url === '''') {',
'        alert(''GEN_AI_GET_ANSWER_FROM_TONGYI_URL not set!'')',
'        return;',
'    }',
'',
'    disableChat();',
'',
'    uuid = await postMessage(',
'        apex.item(''P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL'').getValue(''''),',
'        requestPayload',
'    )',
'',
'    if (uuid) {',
'        await getAnswer(',
'            apex.item(''P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL'').getValue(''''),',
'            uuid',
'        )',
'    }',
'',
'}',
'',
'',
unistr('// \793A\4F8B\7528\6237\8F93\5165'),
'let requestPayload = {',
'    model: ''qwen-turbo'',',
'    messages: [',
'        { role: ''user'', text: apex.item(''P10_MESSAGE_INPUT'').getValue() }',
'    ]',
'};',
'',
unistr('// \53D6\5F97\62A5\6587'),
'apex.server.process(',
'    "EDIT_REQUEST_PAYLOAD",',
'    {',
'        pageItems: "#P10_MESSAGE_INPUT,#P10_GEN_AI_GET_ANSWER_FROM_TONGYI_URL,#P10_GEN_AI_SEND_QUESTION_TO_TONGYI_URL"',
'    },',
'    {',
'        success: function(data) {',
unistr('            // \5904\7406\6210\529F\540E\7684\64CD\4F5C'),
'            console.log(''EDIT_REQUEST_PAYLOAD'', data)',
'            requestPayload = data',
'',
unistr('            // \8C03\7528\51FD\6570\542F\52A8SSE'),
'            callGenAIStream(requestPayload);',
'        }',
'    }',
');'))
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10395677498674636)
,p_name=>'after Chat refresh - scroll to bottom'
,p_event_sequence=>170
,p_triggering_element_type=>'REGION'
,p_triggering_region_id=>wwv_flow_imp.id(10472895874210821)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'apexafterrefresh'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(10395736717674637)
,p_event_id=>wwv_flow_imp.id(10395677498674636)
,p_event_result=>'TRUE'
,p_action_sequence=>10
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_JAVASCRIPT_CODE'
,p_attribute_01=>'$("#chat-messages .t-Comments").scrollTop($("#chat-messages .t-Comments")[0].scrollHeight);'
,p_server_condition_type=>'EXISTS'
,p_server_condition_expr1=>wwv_flow_string.join(wwv_flow_t_varchar2(
'SELECT',
'    1',
'FROM',
'    cb_chat_messages ccm',
'WHERE',
'    ccm.chat_id = :P10_CHAT_ID'))
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(5832303921016037)
,p_name=>'Clicked'
,p_event_sequence=>180
,p_triggering_element_type=>'BUTTON'
,p_triggering_button_id=>wwv_flow_imp.id(5832206404016036)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'click'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5832434485016038)
,p_event_id=>wwv_flow_imp.id(5832303921016037)
,p_event_result=>'TRUE'
,p_action_sequence=>10
,p_execute_on_page_init=>'N'
,p_action=>'NATIVE_EXECUTE_PLSQL_CODE'
,p_attribute_01=>wwv_flow_string.join(wwv_flow_t_varchar2(
'DELETE FROM',
'    cb_chat_messages ccm',
'WHERE',
'    ccm.chat_id = :P10_CHAT_ID;'))
,p_attribute_05=>'PLSQL'
,p_wait_for_result=>'Y'
);
wwv_flow_imp_page.create_page_da_action(
 p_id=>wwv_flow_imp.id(5832536792016039)
,p_event_id=>wwv_flow_imp.id(5832303921016037)
,p_event_result=>'TRUE'
,p_action_sequence=>20
,p_execute_on_page_init=>'N'
,p_name=>'Refresh'
,p_action=>'NATIVE_REFRESH'
,p_affected_elements_type=>'REGION'
,p_affected_region_id=>wwv_flow_imp.id(10472895874210821)
);
wwv_flow_imp_page.create_page_da_event(
 p_id=>wwv_flow_imp.id(10637763104007735)
,p_name=>'Submit'
,p_event_sequence=>190
,p_triggering_element_type=>'BUTTON'
,p_triggering_button_id=>wwv_flow_imp.id(10637622415007734)
,p_bind_type=>'bind'
,p_execution_type=>'IMMEDIATE'
,p_bind_event_type=>'click'
);
wwv_flow_imp_page.create_page_process(
 p_id=>wwv_flow_imp.id(10638101291007739)
,p_process_sequence=>10
,p_process_point=>'ON_DEMAND'
,p_process_type=>'NATIVE_PLSQL'
,p_process_name=>'STORE_CHAT_HISTORY'
,p_process_sql_clob=>wwv_flow_string.join(wwv_flow_t_varchar2(
'DECLARE',
'',
'    lc_json_output   CLOB;',
'',
'BEGIN',
'    -- Insert Question',
'    INSERT INTO cb_chat_messages(',
'        chat_id,',
'        message_type,',
'        message_text,',
'        user_name,',
'        created_by,',
'        last_updated_by',
'    )',
'    VALUES(',
'        :P10_CHAT_ID,       -- chat_id',
'        ''U'',                -- message_type, User',
'        :P10_MESSAGE_INPUT,    -- message_text,',
'        :APP_USER,          -- user_name,',
'        :APP_USER,          -- created_by,',
'        :APP_USER           -- last_updated_by',
'    );',
'',
'    -- Insert Answer',
'    INSERT INTO cb_chat_messages(',
'        chat_id,',
'        message_type,',
'        message_text,',
'        user_name,',
'        created_by,',
'        last_updated_by',
'    )',
'    VALUES(',
'        :P10_CHAT_ID,       -- chat_id',
'        ''M'',                -- message_type, Model',
'        :P10_HIDDEN_OUTPUT,    -- message_text,',
'        ''Model'',            -- user_name,',
'        :APP_USER,          -- created_by,',
'        :APP_USER           -- last_updated_by',
'    );',
'',
'    lc_json_output := json_object(',
'        ''status'' VALUE ''success'',',
'        ''message'' VALUE ''Chat history stored successfully''',
'    );',
'',
unistr('    -- \8F93\51FA JSON \6570\636E'),
'    htp.p(lc_json_output);',
'',
'END;',
''))
,p_process_clob_language=>'PLSQL'
,p_internal_uid=>10638101291007739
);
wwv_flow_imp_page.create_page_process(
 p_id=>wwv_flow_imp.id(10638257706007740)
,p_process_sequence=>20
,p_process_point=>'ON_DEMAND'
,p_process_type=>'NATIVE_PLSQL'
,p_process_name=>'EDIT_REQUEST_PAYLOAD'
,p_process_sql_clob=>wwv_flow_string.join(wwv_flow_t_varchar2(
'DECLARE',
'',
'    lj_payload      JSON_OBJECT_T;',
'    lj_messages     JSON_ARRAY_T;',
'    lj_message      JSON_OBJECT_T;',
'    lv_model_name   cb_models.model_name%TYPE;',
'',
'    CURSOR cur_get_messages IS',
'        SELECT',
'            ccm.message_type        AS message_type,',
'            CASE',
'                ccm.message_type',
'            WHEN ''U'' THEN ''user''',
'            WHEN ''M'' THEN ''assistant''',
'            END                     AS message_role,',
'            ccm.message_text        AS message_text',
'        FROM',
'            cb_chat_messages ccm',
'        WHERE',
'            ccm.chat_id = :P10_CHAT_ID',
'        ORDER BY',
'            ccm.message_id;',
'',
'BEGIN',
'',
unistr('    -- \521D\59CB\5316\6700\7EC8\62A5\6587'),
'    lj_payload  := JSON_OBJECT_T();',
'',
'    SELECT',
'        cm.model_name',
'    INTO',
'        lv_model_name',
'    FROM',
'        cb_chats cc,',
'        cb_models cm',
'    WHERE',
'        cc.model_id = cm.model_id',
'    AND cc.chat_id = :P10_CHAT_ID;',
'    ',
'    lj_payload.put(''model'', lv_model_name);',
'',
unistr('    -- \521D\59CB\5316\6D88\606F\5217\8868'),
'    lj_messages := JSON_ARRAY_T();',
'',
unistr('    -- \521D\59CB\5316\7CFB\7EDF\63D0\793A'),
'    lj_message  := JSON_OBJECT_T();',
'',
unistr('    -- \6DFB\52A0\7CFB\7EDF\63D0\793A\5230\6D88\606F\5217\8868'),
'    lj_message.put(''role'', ''system'');',
'    lj_message.put(''text'', ''You are a helpful assistant. Please don''''t answer if you don''''t know the correct answer'');',
'    lj_messages.append(lj_message);',
'',
unistr('    -- \95EE\7B54\5386\53F2'),
'    FOR rec_get_messages IN cur_get_messages LOOP',
'',
unistr('        -- \521D\59CB\5316\95EE\7B54'),
'        lj_message  := JSON_OBJECT_T();',
'        lj_message.put(''role'', rec_get_messages.message_role);',
'        lj_message.put(''text'', rec_get_messages.message_text);',
'',
unistr('        -- \6DFB\52A0\95EE\7B54\5230\6D88\606F\5217\8868'),
'        lj_messages.append(lj_message);',
'',
'    END LOOP;',
'',
unistr('    -- \6700\65B0\95EE\9898'),
'    lj_message  := JSON_OBJECT_T();',
'    lj_message.put(''role'', ''user'');',
'    lj_message.put(''text'', :P10_MESSAGE_INPUT);',
'    lj_messages.append(lj_message);',
'',
unistr('    -- \6DFB\52A0\6D88\606F\5217\8868\5230\6700\7EC8\62A5\6587'),
'    lj_payload.put(''messages'', lj_messages);',
'',
unistr('    -- \8F93\51FA JSON \6570\636E'),
'    htp.p(lj_payload.to_clob);',
'',
'END;',
''))
,p_process_clob_language=>'PLSQL'
,p_internal_uid=>10638257706007740
);
wwv_flow_imp.component_end;
end;
/
