feat(ai): split Ask/Agent action menus with task-oriented Agent actions

* feat(ai): add task-oriented Agent actions with per-action contract

Introduce three Agent-native actions — query, exploreSchema,
executeAndExplain — so Agent mode surfaces task-oriented operations
instead of reusing Ask's SQL-producing action set.

- ai.ts: extend AiAction with the new actions; add ASK_ACTIONS /
  AGENT_ACTIONS and defaultActionForMode / isValidActionForMode
  helpers; raise maxTokens for the explanatory task actions.
- aiSkills.ts: add skill definitions (zh/en) for the three actions.
- aiAgentPlan.ts: task actions no longer emit a misleading
  "unsupported_action" execute step; they execute via the
  execute_query tool events, not the legacy client-side path.
- agent_loop.rs: per-action task-contract rules in both the prompt
  augmentation and repair paths (query -> execute & answer from real
  data; exploreSchema -> list_tables/get_columns; executeAndExplain
  -> run current SQL & explain).
- i18n (en/zh-CN/zh-TW): new action labels, placeholders, a
  generateNoExec label for Agent mode, and a task-oriented agent
  modeHint.

es/it/ja/pt-BR have no `ai` section and fall back to `en`, matching
existing coverage.

* feat(ai): split Ask/Agent action menus with mode-aware defaults

Wire the new Agent task actions into the assistant UI so each mode
shows a distinct, intent-matching action set.

- actionButtons is now a computed returning Ask or Agent buttons
  based on assistantMode. Agent menu: query / exploreSchema /
  executeAndExplain / generate (labeled "生成但不执行" via
  generateNoExec). Ask menu is unchanged.
- Switching mode lands on that mode's default (Ask -> generate,
  Agent -> query); the shared `generate` is not carried across
  because its label/semantics differ per mode. Vector DBs stay on
  `generate` since their action menu is hidden and they lack SQL
  tools (no execute_query contract).
- External Ask-style triggers (Fix with AI, Explain history) switch
  to Ask mode when invoked from Agent mode so the action is valid
  and the menu reflects what runs.
- Reset the active action to the mode default after each send.

* test(ai): cover Agent task-action contract and mode/action mapping

Lock in the deterministic parts of the Ask/Agent split so only the
LLM-behavior criteria need manual verification.

Backend (agent_loop.rs):
- query contract instructs the LLM to call execute_query and is not
  treated as a SQL-producing action.
- exploreSchema contract points to list_tables/get_columns and says
  not to execute data queries.
- executeAndExplain contract tells the LLM to run the current SQL.
- task actions do not require a SQL deliverable (a conclusion without
  a fenced SQL block still satisfies the contract).
- query repair prompt also targets execute_query.

Frontend (aiActions.spec.ts):
- defaultActionForMode: Ask -> generate, Agent -> query (not generate).
- isValidActionForMode: generate valid in both; Ask-only actions
  rejected in Agent mode and vice versa.
- ASK_ACTIONS / AGENT_ACTIONS composition (Agent menu starts with
  query and still offers generate).

* fix(ai): correct action menu on mode-switched triggers and i18n gaps

Issues found during review of the Ask/Agent action split:

- [#I01] triggerAction (Fix with AI / Explain history) invoked from
  Agent mode switched to Ask and set the action, but the mode-switch
  watch then reset activeAction to the Ask default ("generate"),
  overwriting the action just set — so the menu showed "Generate SQL"
  during a fix. Add a suppressModeActionReset flag so programmatic
  mode switches don't clobber the explicitly-set action.
- [#C01] Remove dead `unsupported_action` reason: buildAiAgentPlan no
  longer produces it, so drop the type variant and the unreachable
  skippedTitleKey case.
- [#I02] es/it/ja/pt-BR were missing the new Agent action keys
  (query/exploreSchema/executeAndExplain/generateNoExec) and their
  placeholders, causing English fallback in the Agent menu. Add the
  translations to match each locale's existing style.

* test(ai): align app-tests with task-action split

- aiSkills: add query/exploreSchema/executeAndExplain to the expected
  action set; broaden the id check (new ids are not _sql-suffixed) and
  relax the userInstruction assertion (schema inspection need not mention SQL).
- aiAgentPlan: drop the unsupported_action skipped step for non-generate
  actions (#C01 removed it from the plan and the reason union); add coverage
  for task-oriented Agent actions not driving client-side execution.

* test(ai): pin locale in dialect prompt spec

buildSystemPrompt selects zh/en copy via currentLocale(), which reflects the
host navigator.language. The spec asserts English strings, so it failed on
zh-CN machines (e.g. Windows with a Chinese system locale) while passing on
CI's en-US runners. Pin the locale to en in beforeAll so the assertions are
deterministic regardless of the host OS locale.
This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-06 14:41:39 +08:00
1 parent c819885d2e
commit 8539011f25
17 files changed
+405 -26

No files matched your search

+77 -6
View File
@@ -470,10 +470,14 @@ fn augment_system_prompt_with_task_contract(
let user_request = contract.user_request.as_deref().unwrap_or("(not provided)");
let mode_rule = if action_requires_sql_deliverable(action) {
"This is a SQL-producing action: produce the final SQL in a fenced ```sql code block. Use tools only as intermediate evidence for schema/dialect; do not stop at a tool-result summary. In Agent mode, execute a query only when the original request explicitly asks for real data/results, not when it merely asks to generate SQL."
} else if is_agent_mode {
"For data-query intents, obtain real results with execute_query when safe; otherwise state the blocker."
} else {
"In Ask mode, produce SQL/explanation only and do not claim execution."
match action.to_ascii_lowercase().as_str() {
"query" => "This is a data-query task: call execute_query to obtain real results, then answer based on the actual data. Do not stop after merely outputting SQL text.",
"exploreschema" => "This is a schema-inspection task: use list_tables/get_columns to obtain authoritative structure, then summarize. Do not execute data queries unless the user explicitly asks for data.",
"executeandexplain" => "This is an execute-and-explain task: call execute_query to run the current SQL, then explain the real results.",
_ if is_agent_mode => "For data-query intents, obtain real results with execute_query when safe; otherwise state the blocker.",
_ => "In Ask mode, produce SQL/explanation only and do not claim execution.",
}
};
format!(
@@ -517,10 +521,14 @@ fn build_contract_repair_prompt(task_contract: Option<&AiTaskContract>, is_agent
let user_request = task_contract.and_then(|c| c.user_request.as_deref()).unwrap_or("(not provided)");
let mode_rule = if action_requires_sql_deliverable(action) {
"For this SQL-producing action, produce SQL in a fenced ```sql code block. Tool results are evidence only; do not answer by summarizing schema/tool output. Execute a query only when the original request explicitly asks for real data/results."
} else if is_agent_mode {
"If the original request asks for real data and it can be answered safely, call execute_query before the final answer."
} else {
"In Ask mode, generate SQL and concise explanation only; do not claim the SQL was executed."
match action.to_ascii_lowercase().as_str() {
"query" => "For this data-query task, call execute_query and answer based on real data; do not stop at SQL text or a schema summary.",
"exploreschema" => "For this schema-inspection task, summarize real structure from list_tables/get_columns; do not invent columns.",
"executeandexplain" => "For this execute-and-explain task, run the current SQL via execute_query and explain the real results.",
_ if is_agent_mode => "If the original request asks for real data and it can be answered safely, call execute_query before the final answer.",
_ => "In Ask mode, generate SQL and concise explanation only; do not claim the SQL was executed.",
}
};
format!(
@@ -1284,4 +1292,67 @@ mod tests {
assert_eq!(events.len(), 1);
assert!(matches!(&events[0], AgentEvent::TextDelta { .. }));
}
// --- Agent task-action contract tests (query / exploreSchema / executeAndExplain) ---
fn contract_for(action: &str, user_request: &str, mode: &str) -> AiTaskContract {
AiTaskContract {
action: Some(action.to_string()),
mode: Some(mode.to_string()),
user_request: Some(user_request.to_string()),
}
}
#[test]
fn query_action_contract_requires_execute_query() {
let contract = contract_for("query", "统计今天订单数", "agent");
let prompt = augment_system_prompt_with_task_contract("base", Some(&contract), true);
assert!(prompt.contains("data-query task"), "prompt should mark this as a data-query task");
assert!(prompt.contains("call execute_query"), "prompt should instruct the LLM to call execute_query");
assert!(!prompt.contains("SQL-producing action"), "query must not be treated as a SQL-producing action");
}
#[test]
fn explore_schema_contract_uses_metadata_tools_not_execute_query() {
let contract = contract_for("exploreSchema", "看一下 orders 表的结构", "agent");
let prompt = augment_system_prompt_with_task_contract("base", Some(&contract), true);
assert!(prompt.contains("schema-inspection task"));
assert!(prompt.contains("list_tables/get_columns"));
assert!(prompt.contains("Do not execute data queries"));
}
#[test]
fn execute_and_explain_contract_runs_current_sql() {
let contract = contract_for("executeAndExplain", "执行并解释当前 SQL", "agent");
let prompt = augment_system_prompt_with_task_contract("base", Some(&contract), true);
assert!(prompt.contains("execute-and-explain task"));
assert!(prompt.contains("run the current SQL"));
}
#[test]
fn task_actions_do_not_require_sql_deliverable() {
// query / exploreSchema / executeAndExplain are task-oriented, not SQL-producing:
// a final answer without a fenced SQL block must still satisfy the contract.
let answer_without_sql = "今天共有 42 笔订单。";
for action in ["query", "exploreSchema", "executeAndExplain"] {
let contract = contract_for(action, "统计今天订单数", "agent");
assert_eq!(
validate_final_answer(Some(&contract), answer_without_sql),
FinalAnswerCheck::Satisfied,
"action {action} should not require a SQL deliverable",
);
}
}
#[test]
fn query_action_repair_prompt_targets_execute_query() {
let contract = contract_for("query", "统计今天订单数", "agent");
let repair = build_contract_repair_prompt(Some(&contract), true, "previous answer did not execute");
assert!(repair.contains("data-query task"));
assert!(repair.contains("call execute_query"));
}
}