| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- import { createGroupResultDelivery } from "#src/bots/groupResultDelivery.js";
- import { describe, expect, it, vi } from "vitest";
- import { replyToBotChannel } from "../src/bots/channelReply.js";
- import type { BotsStateFile, BotsConfigFile, BotGroupDelivery } from "@zcode/shared";
- const mention = {
- type: "channelMention" as const,
- refId: "m1",
- name: "Ryan Bot",
- channel: "feishu" as const,
- targetId: "ou_target",
- idType: "open_id" as const,
- entityType: "unknown" as const,
- };
- function fixture() {
- const source = {
- authorizationId: "auth",
- botId: "bot",
- appId: "app",
- provider: "feishu",
- chatId: "oc_group",
- senderId: "member",
- senderName: "Member",
- messageId: "om_input",
- contentParts: [mention],
- };
- const state = {
- version: 3,
- bots: {
- group: {
- botId: "bot",
- workspacePath: "/work",
- activeTaskId: "task",
- group: {
- enabled: true,
- ownerId: "owner",
- chatId: "oc_group",
- authorizationId: "auth",
- inputs: { input: { taskId: "task", admission: "accepted", source } },
- deliveries: {},
- },
- },
- },
- } as unknown as BotsStateFile;
- const config = {
- bots: [
- { id: "bot", enabled: true, provider: "feishu", providerUserId: "owner", feishuAppId: "app" },
- ],
- } as BotsConfigFile;
- const deliver = vi.fn(async (_bot, _chat, _thread, result: BotGroupDelivery) => {
- state.bots.group!.group!.deliveries![result.id] ??= {
- ...result,
- status: "sent",
- providerMessageId: "om_sent",
- };
- });
- const deps = { readState: async () => state, readConfig: async () => config, deliver };
- const request = {
- taskId: "task",
- inputId: "input",
- toolCallId: "call",
- authorizationId: "auth",
- workspacePath: "/work",
- parts: [
- { type: "mention" as const, refId: "m1" },
- { type: "text" as const, text: " 请确认" },
- ],
- };
- return { state, config, deps, request };
- }
- describe("authorized native channel reply", () => {
- it.each([false, true])(
- "waits for active delivery/retry (%s), but never resends a recovered unknown",
- async (retrying) => {
- const f = fixture();
- const started = Promise.withResolvers<void>();
- const release = Promise.withResolvers<void>();
- const joined = Promise.withResolvers<void>();
- const records = f.state.bots.group!.group!.deliveries!;
- let attempts = 0;
- const send = vi.fn(async () => {
- if (retrying && attempts++ === 0)
- throw Object.assign(new Error("rate limited"), {
- deliveryRejected: true,
- retryAfterMs: 250,
- });
- started.resolve();
- await release.promise;
- return { providerMessageId: "om_sent" };
- });
- const owner = createGroupResultDelivery({
- read: async (id) => records[id],
- write: async (result) => {
- records[result.id] = result;
- },
- authorized: async () => true,
- send,
- delay: async () => {
- started.resolve();
- await release.promise;
- },
- });
- let calls = 0;
- const deps = {
- ...f.deps,
- deliver: async (
- _bot: unknown,
- _chat: string,
- _thread: string | undefined,
- result: BotGroupDelivery,
- ) => {
- const pending = owner(result);
- if (++calls === 2) joined.resolve();
- await pending;
- },
- };
- const first = replyToBotChannel(f.request, deps);
- await started.promise;
- const second = replyToBotChannel(f.request, deps);
- try {
- expect(
- await Promise.race([second.then(() => "settled"), joined.promise.then(() => "waiting")]),
- ).toBe("waiting");
- } finally {
- release.resolve();
- }
- expect(await first).toMatchObject({ status: "sent" });
- expect(await second).toEqual(await first);
- expect(send).toHaveBeenCalledTimes(retrying ? 2 : 1);
- const record = Object.values(records)[0]!;
- records[record.id] = { ...record, status: "unknown", providerMessageId: undefined };
- const restarted = createGroupResultDelivery({
- read: async (id) => records[id],
- write: async (result) => {
- records[result.id] = result;
- },
- authorized: async () => true,
- send,
- });
- expect(
- await replyToBotChannel(f.request, {
- ...deps,
- deliver: async (_bot, _chat, _thread, result) => restarted(result),
- }),
- ).toMatchObject({ status: "unknown" });
- expect(send).toHaveBeenCalledTimes(retrying ? 2 : 1);
- },
- );
- it("resolves a unique typed name and replays the receipt without a new lookup", async () => {
- const f = fixture();
- const listMembers = vi.fn(async () => ({ ou_alice: "Alice" }));
- const deps = { ...f.deps, listMembers };
- const request = { ...f.request, parts: [{ type: "mentionName" as const, name: " Alice " }] };
- const first = await replyToBotChannel(request, deps);
- expect(first.status).toBe("sent");
- expect(f.deps.deliver.mock.calls[0]?.[3].contentParts).toEqual([
- expect.objectContaining({ type: "channelMention", targetId: "ou_alice", name: "Alice" }),
- ]);
- listMembers.mockRejectedValue(new Error("unavailable"));
- expect(await replyToBotChannel(request, deps)).toEqual(first);
- expect(listMembers).toHaveBeenCalledTimes(1);
- expect(f.deps.deliver).toHaveBeenCalledTimes(1);
- await expect(
- replyToBotChannel({ ...request, parts: [{ type: "text", text: "changed" }] }, deps),
- ).rejects.toThrow("idempotency");
- });
- it("returns ambiguous candidates without delivery and accepts only a valid selected candidate", async () => {
- const f = fixture();
- const deps = { ...f.deps, listMembers: async () => ({ ou_a: "Alex", ou_b: "Alex" }) };
- const request = { ...f.request, parts: [{ type: "mentionName" as const, name: "Alex" }] };
- const result = await replyToBotChannel(request, deps);
- expect(result.status).toBe("needs_clarification");
- expect(f.deps.deliver).not.toHaveBeenCalled();
- if (result.status !== "needs_clarification") throw new Error("Expected clarification");
- expect(result.unresolved[0]?.candidates).toHaveLength(2);
- const candidateRef = result.unresolved[0]!.candidates[1]!.ref;
- expect(candidateRef).not.toContain("ou_b");
- // 用户在下一条输入选择候选,标识应跨输入保留,但不能跨授权。
- const group = f.state.bots.group!.group!;
- group.inputs!.followup = {
- ...structuredClone(group.inputs!.input!),
- source: { ...group.inputs!.input!.source, messageId: "om_choice", contentParts: [] },
- };
- const selected = await replyToBotChannel(
- {
- ...request,
- inputId: "followup",
- toolCallId: "selected",
- parts: [{ type: "mentionName", name: "Alex", candidateRef }],
- },
- deps,
- );
- expect(selected.status).toBe("sent");
- expect(f.deps.deliver.mock.calls[0]?.[3].contentParts?.[0]).toMatchObject({ targetId: "ou_b" });
- expect(
- (
- await replyToBotChannel(
- { ...request, parts: [{ type: "mentionName", name: "Alex", candidateRef: "invented" }] },
- deps,
- )
- ).status,
- ).toBe("needs_clarification");
- expect(f.deps.deliver).toHaveBeenCalledTimes(1);
- });
- it("resolves a known bot name from trusted task mentions", async () => {
- const f = fixture();
- await replyToBotChannel(
- { ...f.request, parts: [{ type: "mentionName", name: "Ryan Bot" }] },
- {
- ...f.deps,
- listMembers: async () => ({}),
- },
- );
- expect(f.deps.deliver.mock.calls[0]?.[3].contentParts?.[0]).toMatchObject({
- targetId: "ou_target",
- });
- });
- it("resolves names from an earlier task without authorizing its mention refs", async () => {
- const f = fixture();
- const group = f.state.bots.group!.group!;
- group.inputs!.old = {
- ...structuredClone(group.inputs!.input!),
- taskId: "previous-task",
- progress: { status: "stopped" },
- };
- group.inputs!.input!.source.contentParts = [{ type: "text", text: "Find ryan" }];
- expect(
- await replyToBotChannel(
- { ...f.request, parts: [{ type: "mentionName", name: "ryan" }] },
- { ...f.deps, listMembers: async () => ({}) },
- ),
- ).toMatchObject({ status: "sent" });
- expect(f.deps.deliver.mock.calls[0]?.[3].contentParts?.[0]).toMatchObject({
- targetId: "ou_target",
- name: "Ryan Bot",
- });
- await expect(
- replyToBotChannel({ ...f.request, toolCallId: "old-ref" }, f.deps),
- ).rejects.toThrow();
- });
- it.each(["authorizationId", "chatId", "appId", "botId", "provider"] as const)(
- "excludes historical names from a different %s",
- async (field) => {
- const f = fixture();
- const group = f.state.bots.group!.group!;
- group.inputs!.old = { ...structuredClone(group.inputs!.input!), taskId: "previous-task" };
- Object.assign(group.inputs!.old.source, { [field]: "different" });
- group.inputs!.input!.source.contentParts = [{ type: "text", text: "Find ryan" }];
- const result = await replyToBotChannel(
- { ...f.request, parts: [{ type: "mentionName", name: "ryan" }] },
- { ...f.deps, listMembers: async () => ({}) },
- );
- expect(result).toMatchObject({
- status: "needs_clarification",
- unresolved: [{ reason: "not_found", candidates: [] }],
- });
- expect(f.deps.deliver).not.toHaveBeenCalled();
- },
- );
- it("treats a directory user and trusted bot with the same name as ambiguous", async () => {
- const f = fixture();
- const result = await replyToBotChannel(
- { ...f.request, parts: [{ type: "mentionName", name: "ryan bot" }] },
- { ...f.deps, listMembers: async () => ({ ou_person: "Ryan Bot" }) },
- );
- expect(result.status).toBe("needs_clarification");
- if (result.status !== "needs_clarification") throw new Error("Expected ambiguity");
- expect(result.unresolved[0]?.candidates).toHaveLength(2);
- expect(f.deps.deliver).not.toHaveBeenCalled();
- });
- it("does not partially deliver mixed resolved and missing recipients", async () => {
- const f = fixture();
- expect(
- (
- await replyToBotChannel(
- {
- ...f.request,
- parts: [
- { type: "mention", refId: "m1" },
- { type: "mentionName", name: "Unknown" },
- ],
- },
- { ...f.deps, listMembers: async () => ({}) },
- )
- ).status,
- ).toBe("needs_clarification");
- expect(f.deps.deliver).not.toHaveBeenCalled();
- });
- it("bounds duplicate-name output and rejects a candidate from another credential scope", async () => {
- const f = fixture();
- const deps = {
- ...f.deps,
- listMembers: async () =>
- Object.fromEntries(Array.from({ length: 12 }, (_, i) => [`ou_member_${i}`, "Alex"])),
- };
- const request = { ...f.request, parts: [{ type: "mentionName" as const, name: "Alex" }] };
- const result = await replyToBotChannel(request, deps);
- if (result.status !== "needs_clarification") throw new Error("Expected ambiguity");
- expect(result.unresolved[0]?.candidates).toHaveLength(10);
- expect(result.unresolved[0]?.truncated).toBe(true);
- f.config.bots[0]!.credentialRef = "new-credential";
- const selected = await replyToBotChannel(
- {
- ...request,
- parts: [
- {
- type: "mentionName",
- name: "Alex",
- candidateRef: result.unresolved[0]!.candidates[0]!.ref,
- },
- ],
- },
- deps,
- );
- expect(selected).toMatchObject({
- status: "needs_clarification",
- unresolved: [expect.objectContaining({ reason: "selection_expired" })],
- });
- expect(f.deps.deliver).not.toHaveBeenCalled();
- });
- it("rejects binding changes during lookup even when the directory returns a unique match", async () => {
- const f = fixture();
- await expect(
- replyToBotChannel(
- { ...f.request, parts: [{ type: "mentionName", name: "Alice" }] },
- {
- ...f.deps,
- listMembers: async () => {
- f.config.bots[0]!.credentialRef = "changed";
- return { ou_alice: "Alice" };
- },
- },
- ),
- ).rejects.toThrow("authorization changed");
- expect(f.deps.deliver).not.toHaveBeenCalled();
- });
- it.each(["missing", "denied", "revoked"])("never sends for %s name lookup", async (kind) => {
- const f = fixture();
- const request = { ...f.request, parts: [{ type: "mentionName" as const, name: "Alice" }] };
- const deps = {
- ...f.deps,
- listMembers: async () => {
- if (kind === "denied") throw new Error("permission denied");
- if (kind === "revoked") f.state.bots.group!.group!.enabled = false;
- return kind === "missing" ? {} : { ou_alice: "Alice" };
- },
- };
- if (kind === "revoked") await expect(replyToBotChannel(request, deps)).rejects.toThrow();
- else expect((await replyToBotChannel(request, deps)).status).toBe("needs_clarification");
- expect(f.deps.deliver).not.toHaveBeenCalled();
- });
- it("uses trusted targets and returns the saved provider receipt", async () => {
- const { deps, request } = fixture();
- const result = await replyToBotChannel(request, deps);
- expect(result).toMatchObject({ status: "sent", providerMessageId: "om_sent" });
- expect(deps.deliver.mock.calls[0]?.[3]).toMatchObject({
- replyToMessageId: "om_input",
- contentParts: [mention, { type: "text", text: " 请确认" }],
- });
- expect(await replyToBotChannel(request, deps)).toEqual(result);
- await expect(
- replyToBotChannel({ ...request, parts: [{ type: "text", text: "different" }] }, deps),
- ).rejects.toThrow("idempotency");
- });
- it.each([undefined, "thread-1"])(
- "resolves desktop follow-up from task binding in %s",
- async (threadId) => {
- const { deps, request, state } = fixture();
- const group = state.bots.group!.group!;
- group.threadId = threadId;
- group.inputs!.input!.source.threadId = threadId;
- const { authorizationId: _authorizationId, ...followup } = request;
- const result = await replyToBotChannel({ ...followup, inputId: "desktop-input" }, deps);
- expect(result.status).toBe("sent");
- expect(deps.deliver.mock.calls[0]?.[2]).toBe(threadId);
- expect(deps.deliver.mock.calls[0]?.[3].contentParts?.[0]).toEqual(mention);
- },
- );
- it("reuses earlier native references while replying to the current input", async () => {
- const { deps, request, state } = fixture();
- const group = state.bots.group!.group!;
- group.inputs!.followup = {
- ...structuredClone(group.inputs!.input!),
- source: { ...group.inputs!.input!.source, messageId: "om_followup", contentParts: [] },
- };
- await replyToBotChannel({ ...request, inputId: "followup" }, deps);
- expect(deps.deliver.mock.calls[0]?.[3]).toMatchObject({
- replyToMessageId: "om_followup",
- contentParts: [mention, { type: "text", text: " 请确认" }],
- });
- });
- it("does not borrow targets from a different task even with a valid binding", async () => {
- const { deps, request, state } = fixture();
- const group = state.bots.group!.group!;
- group.inputs!.old = { ...structuredClone(group.inputs!.input!), taskId: "other-task" };
- group.inputs!.input!.source.contentParts = [];
- await expect(replyToBotChannel(request, deps)).rejects.toThrow("Unknown channel mention");
- expect(deps.deliver).not.toHaveBeenCalled();
- });
- it.each(["task", "thread", "oldAuthorization", "ambiguous", "identity"])(
- "rejects mismatched binding %s",
- async (kind) => {
- const { deps, request, state } = fixture();
- const group = state.bots.group!.group!;
- if (kind === "task") group.inputs!.input!.taskId = "other-task";
- if (kind === "thread") group.inputs!.input!.source.threadId = "other-thread";
- if (kind === "oldAuthorization") group.inputs!.input!.source.authorizationId = "old";
- if (kind === "ambiguous") state.bots.duplicate = structuredClone(state.bots.group!);
- if (kind === "identity") state.bots.group!.workspaceIdentity = "remote:other";
- await expect(
- replyToBotChannel({ ...request, inputId: "desktop-input" }, deps),
- ).rejects.toThrow();
- expect(deps.deliver).not.toHaveBeenCalled();
- },
- );
- it.each(["workspace", "app", "authorization", "unknownRef", "disabled", "stopped"])(
- "rejects %s without sending",
- async (kind) => {
- const { deps, request, state, config } = fixture();
- if (kind === "workspace") request.workspacePath = "/other";
- if (kind === "app") config.bots[0]!.feishuAppId = "other";
- if (kind === "authorization") request.authorizationId = "other";
- if (kind === "unknownRef") request.parts = [{ type: "mention", refId: "invented" }];
- if (kind === "stopped")
- state.bots.group!.group!.inputs!.input!.progress = { status: "stopped" };
- if (kind === "disabled") state.bots.group!.group!.enabled = false;
- await expect(replyToBotChannel(request, deps)).rejects.toThrow();
- expect(deps.deliver).not.toHaveBeenCalled();
- },
- );
- });
|