初始提交

This commit is contained in:
2026-11-04 19:58:07 +08:00
commit 53b7d22429
829 changed files with 469986 additions and 0 deletions

View File

@@ -0,0 +1,249 @@
import {
click,
contains,
insertText,
setupChatHub,
start,
startServer,
} from "@mail/../tests/mail_test_helpers";
import { describe, expect, test } from "@odoo/hoot";
import { press } from "@odoo/hoot-dom";
import { onRpc } from "@web/../tests/web_test_helpers";
import { createAIChat, defineAIModels } from "../ai_test_helpers";
describe.current.tags("desktop");
defineAIModels();
const COMPOSER_SELECTOR = ".o-mail-Composer";
const INPUT_REQUEST_SELECTOR = ".o_ai_user_input_request";
const EXTERNAL_WAIT_SELECTOR = ".o_ai_external_pending_tool";
async function startAIChat(sessionValues, messageBodies = ["What should I do?"]) {
const pyEnv = await startServer();
const agentPartnerId = pyEnv["res.partner"].create({ name: "AI Agent" });
const agentId = pyEnv["ai.agent"].create({
name: "AI Agent",
partner_id: agentPartnerId,
});
const { channelId, sessionId } = createAIChat(pyEnv, agentId);
const userInputRequest = sessionValues.user_input_request;
if (userInputRequest) {
sessionValues = {
loop_state: userInputRequest.type === "confirmation" ? "waiting_confirmation" : "waiting_answer",
...sessionValues,
user_input_request: {
resumeToken: "durable-user-input-token",
...userInputRequest,
},
};
}
pyEnv["ai.session"].write([sessionId], sessionValues);
for (const body of messageBodies) {
pyEnv["mail.message"].create({
author_id: agentPartnerId,
body,
message_type: "comment",
model: "discuss.channel",
res_id: channelId,
});
}
setupChatHub({ opened: [channelId] });
await start();
await contains(".o-mail-ChatWindow");
return { channelId, sessionId, pyEnv };
}
test("request is rendered once separately from AI messages", async () => {
await startAIChat(
{
user_input_request: {
choices: [
{ label: "One", value: "one" },
{ label: "Two", value: "two" },
],
multiSelect: false,
type: "question",
},
},
["Older AI message", "Newest AI message"],
);
await contains(".o-mail-Message", { count: 2 });
await contains(INPUT_REQUEST_SELECTOR, { count: 1 });
await contains(`.o-mail-Message ${INPUT_REQUEST_SELECTOR}`, { count: 0 });
await contains(".o-mail-Message:has(:text('Newest AI message')) .o-mail-Message-actions", {
count: 0,
});
await contains(".o-mail-Message:has(:text('Older AI message')) .o-mail-Message-actions");
});
test("pending external tool freezes the composer while showing a progress indicator", async () => {
await startAIChat(
{
external_pending_tool: true,
loop_state: "waiting_external_result",
resume_token: "durable-external-tool-token",
},
["Older AI message", "The external task has started."],
);
await contains(EXTERNAL_WAIT_SELECTOR, { count: 1 });
await contains(
`.o-mail-Message:has(:text('The external task has started.')) ${EXTERNAL_WAIT_SELECTOR} .oi-spin`
);
await contains(INPUT_REQUEST_SELECTOR, { count: 0 });
await contains(COMPOSER_SELECTOR, { count: 0 });
// Cancel is the only way out of a wait whose external result never arrives.
await contains(`${EXTERNAL_WAIT_SELECTOR} .o_ai_cancel_external`);
});
test("cancelling a pending external tool aborts it and frees the composer", async () => {
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.channel_id).toBe(channelId);
expect(params.session_id).toBe(sessionId);
expect(params.resume_token).toBe("durable-external-tool-token");
expect(params.response).toEqual({ kind: "skip" });
expect.step("pending tools aborted");
return { loop_state: "ready", interactionConsumed: true };
});
const { channelId, sessionId } = await startAIChat(
{
external_pending_tool: true,
loop_state: "waiting_external_result",
resume_token: "durable-external-tool-token",
},
["The external task has started."],
);
await click(`${EXTERNAL_WAIT_SELECTOR} .o_ai_cancel_external`);
await expect.waitForSteps(["pending tools aborted"]);
await contains(EXTERNAL_WAIT_SELECTOR, { count: 0 });
// the composer is usable again, so the chat is not frozen on a dead wait
await contains(COMPOSER_SELECTOR);
});
test("single choice submits the question answer and hides the choices", async () => {
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.response).toEqual({
kind: "question",
value: ["draft"],
});
expect(params.resume_token).toBe("durable-user-input-token");
expect.step("answer submitted");
return { loop_state: "waiting_model" };
});
await startAIChat({
user_input_request: {
choices: [
{ label: "Create a draft", value: "draft" },
{ label: "Send it now", value: "send" },
],
multiSelect: false,
allowFreeText: true,
type: "question",
},
});
await contains(`${INPUT_REQUEST_SELECTOR} button:contains('Create a draft')`);
await contains(`${INPUT_REQUEST_SELECTOR} button:contains('Send it now')`);
await contains(`${INPUT_REQUEST_SELECTOR} input[placeholder='Something else']`);
await contains(".o-mail-Message-actions", { count: 0 });
// the composer is hidden while a question is pending
await contains(COMPOSER_SELECTOR, { count: 0 });
await click(`${INPUT_REQUEST_SELECTOR} button:contains('Create a draft')`);
await expect.waitForSteps(["answer submitted"]);
await contains(INPUT_REQUEST_SELECTOR, { count: 0 });
await contains(COMPOSER_SELECTOR);
await contains(".o-mail-Message-actions");
});
test("free text submits the question answer with the typed value", async () => {
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.response).toEqual({
kind: "question",
value: ["Neither, cancel it"],
});
expect.step("answer submitted");
return { loop_state: "waiting_model" };
});
await startAIChat({
user_input_request: {
choices: [
{ label: "Create a draft", value: "draft" },
{ label: "Send it now", value: "send" },
],
multiSelect: false,
allowFreeText: true,
type: "question",
},
});
await insertText(
`${INPUT_REQUEST_SELECTOR} input[placeholder='Something else']`,
"Neither, cancel it",
);
await press("Enter");
await expect.waitForSteps(["answer submitted"]);
await contains(INPUT_REQUEST_SELECTOR, { count: 0 });
});
test("multi-select submits selected values in choice order", async () => {
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.response).toEqual({
kind: "question",
value: ["first", "third"],
});
expect.step("multi-select submitted");
return { loop_state: "waiting_model" };
});
await startAIChat({
user_input_request: {
choices: [
{ label: "First", value: "first" },
{ label: "Second", value: "second" },
{ label: "Third", value: "third" },
],
multiSelect: true,
type: "question",
},
});
await contains(`${INPUT_REQUEST_SELECTOR} button:contains('Confirm'):disabled`);
await click(`${INPUT_REQUEST_SELECTOR} button:contains('Third')`);
await click(`${INPUT_REQUEST_SELECTOR} button:contains('First')`);
await click(`${INPUT_REQUEST_SELECTOR} button:contains('Confirm'):enabled`);
await expect.waitForSteps(["multi-select submitted"]);
await contains(INPUT_REQUEST_SELECTOR, { count: 0 });
});
test("skip resumes the durable request without an answer", async () => {
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.response).toEqual({ kind: "skip" });
expect(params.resume_token).toBe("durable-user-input-token");
expect.step("question skipped");
return { loop_state: "ready" };
});
await startAIChat({
user_input_request: {
choices: [
{ label: "Create a draft", value: "draft" },
{ label: "Send it now", value: "send" },
],
multiSelect: false,
type: "question",
},
});
await contains(`${INPUT_REQUEST_SELECTOR} button:contains('Skip')`);
await click(`${INPUT_REQUEST_SELECTOR} button:contains('Skip')`);
await expect.waitForSteps(["question skipped"]);
await contains(INPUT_REQUEST_SELECTOR, { count: 0 });
});

View File

@@ -0,0 +1,131 @@
import {
click,
contains,
MENU_ACTIVE_IDS,
openDiscuss,
openMessagingMenu,
start,
startServer,
} from "@mail/../tests/mail_test_helpers";
import { test } from "@odoo/hoot";
import { Command, serverState } from "@web/../tests/web_test_helpers";
import { defineAIModels } from "../ai_test_helpers";
defineAIModels();
test("Can open a new AI chat from the messaging menu AI tab", async () => {
await start();
await openMessagingMenu(MENU_ACTIVE_IDS.AI_CHAT);
await click("button:has([data-icon='add']):text('Chat')");
await contains(".o-mail-ChatWindow-displayName:text('Test AI Agent')");
});
test("AI tab has an unread filter", async () => {
const pyEnv = await startServer();
const [alphaPartnerId, betaPartnerId] = pyEnv["res.partner"].create([
{ name: "Alpha AI" },
{ name: "Beta AI" },
]);
const [alphaAgentId, betaAgentId] = pyEnv["ai.agent"].create([
{ name: "Alpha AI", partner_id: alphaPartnerId },
{ name: "Beta AI", partner_id: betaPartnerId },
]);
const [alphaChannelId, betaChannelId] = pyEnv["discuss.channel"].create([
{
ai_agent_id: alphaAgentId,
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: alphaPartnerId }),
],
channel_type: "ai_chat",
name: "Alpha AI",
},
{
ai_agent_id: betaAgentId,
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: betaPartnerId }),
],
channel_type: "ai_chat",
name: "Beta AI",
},
]);
const [, betaMessageId] = pyEnv["mail.message"].create([
{
author_id: alphaPartnerId,
body: "hello",
model: "discuss.channel",
res_id: alphaChannelId,
},
{ author_id: betaPartnerId, body: "hi", model: "discuss.channel", res_id: betaChannelId },
]);
const [betaMemberId] = pyEnv["discuss.channel.member"].search([
["channel_id", "=", betaChannelId],
["partner_id", "=", serverState.partnerId],
]);
// Beta AI is marked read: its separator is past its last message.
pyEnv["discuss.channel.member"].write([betaMemberId], {
new_message_separator: betaMessageId + 1,
});
await start();
await openMessagingMenu(MENU_ACTIVE_IDS.AI_CHAT);
await contains(".o-mail-NotificationItem", { count: 2 });
await contains(".o-mail-NotificationItem:has(:text('Alpha AI'))");
await contains(".o-mail-NotificationItem:has(:text('Beta AI'))");
await click("button:text('Unread')");
await contains("button.o-active:text('Unread')");
await contains(".o-mail-NotificationItem", { count: 1 });
await contains(".o-mail-NotificationItem:has(:text('Alpha AI'))");
});
test("AI tab agent plugin filter works alongside chip filters", async () => {
const pyEnv = await startServer();
const [alphaPartnerId, betaPartnerId] = pyEnv["res.partner"].create([
{ name: "Alpha AI" },
{ name: "Beta AI" },
]);
const [alphaAgentId, betaAgentId] = pyEnv["ai.agent"].create([
{ name: "Alpha AI", partner_id: alphaPartnerId },
{ name: "Beta AI", partner_id: betaPartnerId },
]);
const alphaChannelIds = pyEnv["discuss.channel"].create(
Array.from({ length: 22 }, (_, i) => ({
ai_agent_id: alphaAgentId,
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: alphaPartnerId }),
],
channel_type: "ai_chat",
name: `Alpha chat ${i}`,
}))
);
pyEnv["discuss.channel"].create({
ai_agent_id: betaAgentId,
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: betaPartnerId }),
],
channel_type: "ai_chat",
name: "Beta chat",
});
pyEnv["mail.message"].create({
author_id: alphaPartnerId,
body: "hello",
model: "discuss.channel",
res_id: alphaChannelIds[0],
});
await start();
await openDiscuss(MENU_ACTIVE_IDS.AI_CHAT);
await contains(".o-mail-NotificationItem", { count: 20 });
await click("button[aria-label='Filters']");
await click(".dropdown-item:text('Beta AI')");
await contains(".o-mail-NotificationItem");
await contains(".o-mail-NotificationItem:has(:text('Beta chat'))");
await click("button[aria-label='Filters']");
await click(".dropdown-item:text('Alpha AI')");
await contains(".o-mail-NotificationItem:contains('Alpha chat')", { count: 22 });
await click("button:text('Unread')");
await contains("button.o-active:text('Unread')");
await contains(".o-mail-NotificationItem", { count: 1 });
await contains(".o-mail-NotificationItem:has(:text('Alpha chat 0'))");
});

View File

@@ -0,0 +1,152 @@
import { insertText, setupChatHub, start, startServer } from "@mail/../tests/mail_test_helpers";
import { waitFor, waitForNone, describe, expect, test } from "@odoo/hoot";
import { press } from "@odoo/hoot-dom";
import { Command, contains, getService, onRpc, serverState } from "@web/../tests/web_test_helpers";
import { defineAIModels } from "../ai_test_helpers";
describe.current.tags("desktop");
defineAIModels();
test("posting in ai chat starts callback-driven response generation", async () => {
const pyEnv = await startServer();
const partnerId = pyEnv["res.partner"].create({ name: "Agent Partner" });
const aiAgentId = pyEnv["ai.agent"].create({
name: "Agent Partner",
partner_id: partnerId,
sources_ids: [Command.create({ name: "instructions.txt" })],
});
const channelId = pyEnv["discuss.channel"].create({
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: partnerId }),
],
channel_type: "ai_chat",
ai_agent_id: aiAgentId,
});
pyEnv["ai.session"].create({
agent_id: aiAgentId,
channel_id: channelId,
loop_state: "ready",
});
onRpc("/ai/start_session_advance", () => {
expect.step("start session advance");
return {
loop_state: "waiting_model",
};
});
setupChatHub({ opened: [channelId] });
await start();
await waitFor(".o-mail-ChatWindow");
await contains("button[title='More Actions']").click();
// Test session options buttons
await contains("button[name='toggle-web-search']").click();
await waitFor("button[name='toggle-web-search'] input[type='checkbox']:checked");
await contains("button[name='toggle-restrict-to-resources']").click();
await waitFor("button[name='toggle-web-search'] input[type='checkbox']:not(:checked)");
await waitFor("button[name='toggle-restrict-to-resources'] input[type='checkbox']:checked");
await contains("button[name='toggle-think-longer']").click();
await waitFor("button[name='toggle-think-longer'] input[type='checkbox']:checked");
await contains("button[name='toggle-action-auto-approve']").click();
await waitFor("button[name='toggle-action-auto-approve'] input[type='checkbox']:checked");
await contains(".o-mail-ChatWindow .o-mail-Composer-input").click();
await insertText(".o-mail-ChatWindow .o-mail-Composer-input", "AI is bad at writing code");
await press("Enter");
await expect.waitForSteps(["start session advance"]);
});
test("loop state controls sending while the agent or its subagent is busy", async () => {
const pyEnv = await startServer();
const partnerId = pyEnv["res.partner"].create({ name: "Agent Partner" });
const aiAgentId = pyEnv["ai.agent"].create({
name: "Agent Partner",
partner_id: partnerId,
});
const channelId = pyEnv["discuss.channel"].create({
channel_member_ids: [
Command.create({ partner_id: serverState.partnerId }),
Command.create({ partner_id: partnerId }),
],
channel_type: "ai_chat",
ai_agent_id: aiAgentId,
});
const sessionId = pyEnv["ai.session"].create({
agent_id: aiAgentId,
channel_id: channelId,
loop_state: "ready",
});
onRpc("/ai/start_session_advance", () => {
expect.step("start session advance");
return {
loop_state: "waiting_model",
};
});
setupChatHub({ opened: [channelId] });
await start();
await waitFor(".o-mail-ChatWindow");
await contains(".o-mail-ChatWindow .o-mail-Composer-input").click();
await insertText(".o-mail-ChatWindow .o-mail-Composer-input", "First message");
await press("Enter");
await expect.waitForSteps(["start session advance"]);
await insertText(".o-mail-ChatWindow .o-mail-Composer-input", "Second message");
await waitFor(".o-mail-Composer button[name='send-message']:disabled");
await waitFor(".o-ai-tool-status:contains(Thinking)");
await waitForNone(".o-mail-ChatWindow-typing");
const store = getService("mail.store");
for (const loop_state of ["waiting_child", "waiting_client_result"]) {
store["ai.session"].insert({ id: sessionId, loop_state });
await waitFor(".o-mail-Composer button[name='send-message']:disabled");
await waitFor(".o-ai-tool-status:contains(Thinking)");
}
for (const loop_state of ["waiting_confirmation", "waiting_answer", "ready"]) {
store["ai.session"].insert({ id: sessionId, loop_state });
await waitFor(".o-mail-Composer button[name='send-message']:enabled");
await waitForNone(".o-ai-tool-status");
}
const child = store["ai.session"].insert({
id: sessionId + 1,
channel_id: channelId,
parent_session_id: sessionId,
});
for (const loop_state of ["waiting_confirmation", "waiting_answer"]) {
child.loop_state = loop_state;
await waitFor(".o-mail-Composer button[name='send-message']:disabled");
}
child.loop_state = "ready";
await waitFor(".o-mail-Composer button[name='send-message']:enabled");
onRpc("/ai/resume_pending_interaction", async (request) => {
const { params } = await request.json();
expect(params.session_id).toBe(child.id);
expect(params.resume_token).toBe("child-confirmation");
expect(params.response).toEqual({ kind: "confirmation", value: "confirm_once" });
expect.step("child confirmed");
return { loop_state: "ready", interactionConsumed: true };
});
child.loop_state = "waiting_confirmation";
child.userInputRequest = {
type: "confirmation",
body: "Create the contact?",
choices: [{ label: "Yes, do it", value: "confirm_once" }],
resumeToken: "child-confirmation",
};
const prompt = ".o_ai_user_input_request";
await waitFor(prompt);
expect(prompt + " .fw-bold").toHaveText("Agent Partner");
await contains(prompt + " button:contains('Yes, do it')").click();
await expect.waitForSteps(["child confirmed"]);
await waitForNone(prompt);
// Livechat retains the native typing indicator while callbacks are pending.
store["discuss.channel"].get(channelId).channel_type = "livechat";
child.loop_state = "waiting_model";
await waitFor(".o-mail-ChatWindow-typing");
child.loop_state = "ready";
await waitForNone(".o-mail-ChatWindow-typing");
});