Skip to content

Add a chat region to a page

You build a chat page. An engineer opens one service contract, asks the desk a question about it, and gets an answer from your own tables.

The agent already exists. This course is about the part in front of it: the region, what it sends, what it stores, and what it lets a user see.

By the end you have a chat region that answers about one contract and no other, that keeps each user’s conversation private, and that you can debug when it goes quiet.

You need UC AI 26.3 or later, installed and able to reach a model. If a call works, you are ready. If it does not, the installation guide covers the network and key setup.

The plug-in package calls execute_agent with p_run_context, which is new in 26.3, so it does not compile on an older version.

You also need the plug-in itself. It is attached to the latest UC AI release.

Run the setup script once:

Terminal window
sql your_user/your_password@your_db @00_setup.sql

It ends with a report. Every line must pass before you go on. To remove everything again later, run 00_teardown.sql from the same directory.

Tables : 6 of 6
Package : valid
Tools : 4 of 4
Agent SC_DESK: active
Setup OK. Start lesson 1.
  1. Run ai_tables.sql from the plug-in files. It creates uc_ai_chat_messages, which holds every message of every conversation.

  2. Run uc_ai_chat.pks and then uc_ai_chat.pkb. The package body calls UC AI, so UC AI must be installed first.

  3. Import the plug-in file into your APEX application, the same way you import any other component.

  1. Create a new region on your page. Set its type to UC AI Chat.

  2. Open the Agent group of the region attributes and set Agent Code to SC_DESK.

    This is the only attribute you must fill in. The rest have defaults.

  3. The desk expects three values on the first message: who is asking, what the date is, and the question. Set Agent Input Mapping to:

    {
    "engineer_name": "&APP_USER.",
    "today": "&P10_TODAY.",
    "question": "#USER_MESSAGE#"
    }

    &APP_USER. is the APEX user. #USER_MESSAGE# is what the user typed. Both are put into the JSON safely, so a quote in a question cannot break it.

  4. &P10_TODAY. needs a value. Create a hidden page item P10_TODAY, then add a computation on it with the point Before Header and this expression:

    to_char(sysdate, 'YYYY-MM-DD')

Run the page and ask the desk a question about the contract:

Which invoices on this contract still have an uncredited amount?

Recorded answergpt-5.6-terra3.3s2026-08-25

I can’t list invoices because this conversation isn’t bound to a service contract.

Your wording will differ. What must match is the data, and the checks below.

The turn finished. The model called the tool, the tool refused, and the model explained the refusal.

The chat region on an APEX page. The question is at the top right. Below it a tool call named SC_LIST_INVOICES with the result status refused and reason NO_CONTRACT, then the answer of the assistant.

The tool result:

{
"status": "refused",
"reason": "NO_CONTRACT",
"message": "This conversation is not bound to a service contract, so I cannot list its invoices."
}

The tools of this desk have no contract parameter. The model cannot name a contract, because there is nothing to name it in. The contract comes from the application, and so far the application has not given one.

That is what the next lesson adds.

The answer text will differ on your run. What must match is the sequence of rows the plug-in stored:

select role, turn_status, tool_name, input_tokens, output_tokens
from uc_ai_chat_messages
order by id desc
fetch first 4 rows only;
ROLE TURN_STATUS TOOL_NAME INPUT_TOKENS OUTPUT_TOKENS
assistant 542 20
tool_result SC_LIST_INVOICES
tool_call SC_LIST_INVOICES
user done

Four rows for one message. The user row carries the status of the whole turn, and done means the turn finished. A refusal is a result, not an error.

  • One attribute makes a chat region work: Agent Code.
  • The plug-in stores every part of a turn in uc_ai_chat_messages, one row for each: the question, each tool call, each tool result, and the answer.
  • &P10_TODAY. needs a computation, because the plug-in reads page items on the server: to_char(sysdate, 'YYYY-MM-DD').

Full reference: the APEX Chat plug-in guide lists every region attribute with its type and default.