ctx.ai
Use ctx.ai in RunJS to trigger AI employee tasks. It works well in JSBlock, JSAction, and other interactions where a button, form, or business flow needs to hand work to a specific AI employee.
ctx.ai uploads AI task attachments and triggers tasks. File uploads can be awaited, but task triggering does not return the execution result of the AI employee task. After the call, the task enters the AI employee conversation flow, and the result is handled by the AI employee session.
ctx.ai is provided by the AI plugin. If the AI plugin is not enabled, or the current RunJS environment has not loaded the corresponding client capability, ctx.ai may not exist. You can check ctx.ai?.uploadFile, ctx.ai?.triggerTask, or ctx.ai?.triggerModelTask before calling it.
Methods
ctx.ai.uploadFile()
Upload one file and return an attachment object that can be passed directly to an AI employee task.
The upload uses the file storage configured by the AI plugin and creates a record in aiFiles. The returned object includes fields such as id, filename, url, and source:
The Promise is rejected when the upload fails. Removing an attachment from the local UI does not delete the record already created in aiFiles, matching the behavior of the default AI chat window.
ctx.ai.triggerTask()
Trigger an AI employee task directly.
Common Task fields:
Track the Response Loading State
Pass onResponseLoadingChange in the top-level options to track the model response loading state for this task. The callback receives true when NocoBase starts waiting for the model response, and false when the response completes, is canceled, or fails. If the React component has declared setResponseLoading with useState, you can write:
onResponseLoadingChange only tracks the model response started directly by this triggerTask() call. With autoSend: false, the task is placed in the chat draft and the callback does not run. If the user sends the draft later, that manual send does not reuse this callback.
In a React component inside a JS block, this state update rerenders the component while it remains mounted.
Target an AI Chat Box
Set chatBoxUid on the top-level triggerTask() options to trigger the task in a mounted AI Chat Box block instead of the global AI employee dialog.
The uid must belong to the outer AI Chat Box block currently mounted on the page. Do not put this routing value inside tasks. If the target block cannot be found, NocoBase reports an error and does not fall back to the global dialog. When chatBoxUid is omitted, the task uses the global AI employee dialog.
Upload and Send Attachments in JSBlock
The following example renders file upload, task instructions, and a send button in JSBlock. Uploaded files are passed to the AI employee through message.attachments:
With autoSend: false, the attachments and task instructions are placed in the AI chat draft instead of being sent immediately.
Add Page Block Context
message.workContext currently passes page blocks. Put the FlowModel uid of the target page block into it:
If you want to use the current JSBlock itself as context, use the current model uid:
Usually, message.workContext should be placed inside the message of a concrete task. triggerModelTask() reuses the task configured on the target AI employee action, so it also reads message.workContext from that task.
Specify Model
model specifies the model for a single task. If omitted, the AI employee's default model configuration is used. Passing null means no task-level model is specified.
Configure Skills / Tools
skillSettings specifies the skills and tools available to a single task. If omitted, the AI employee's own capability configuration is used.
To explicitly disable all skills or tools for this task, pass empty arrays and keep the version fields:
The following example asks viz to generate a handoff brief and opens the AI employee conversation panel:
If aiEmployee is a string, NocoBase matches accessible AI employees by username exactly. If no match is found, the task is not triggered and a warning is printed to the console.
ctx.ai.triggerModelTask()
Read a task from an AI employee action model on the page and trigger it.
This method reads the AI employee and task configuration from the target model. It is useful when the task has already been configured on an AI employee action on the page, and RunJS only needs to trigger it.
The public triggerModelTask() options do not accept chatBoxUid. To target an AI Chat Box, configure chatBoxUid on the preset task of the AI employee action. triggerModelTask() continues to reuse that preset value.
options.onResponseLoadingChange behaves the same as the option on triggerTask(). Whether it runs depends on the configured task's autoSend value. It does not run when that task uses autoSend: false.
If the target model does not exist, has no AI employee configured, or the specified index has no task, no task is triggered and a warning is printed to the console.
Which Method to Use
Notes
triggerTask()andtriggerModelTask()are fire-and-forget. They do not return the execution result of the AI employee task.uploadFile()returns a Promise. Wait for the upload to finish before triggering a task that uses the attachment.aiEmployeestrings only matchAIEmployee.usernameexactly. They do not match nicknames, job titles, or translated names.triggerModelTask()uses a0-basedtaskIndex.triggerModelTask()reads task configuration from the target AI employee action model. If the task needs work context, configuremessage.workContexton that task.- Top-level
triggerTask().chatBoxUidmust reference an AI Chat Box block currently mounted on the page. triggerModelTask()keeps usingchatBoxUidconfigured on its preset task.- Dynamic
triggerModelTask()attachments are appended to the preset task's existingmessage.attachmentswithout changing the saved task configuration. onResponseLoadingChangeonly tracks a model response automatically sent by the current call. It does not track a message the user sends manually later.
Related
- ctx.message: Show lightweight prompts before and after triggering tasks.
- ctx.render: Render buttons or forms in JSBlock.
- ctx.model: Get current FlowModel information.

