@open-mercato/ai-assistant 0.6.7-develop.6862.1.c11a64ce0a → 0.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +29 -12
- package/dist/modules/ai_assistant/__integration__/helpers/aiAssistantFixtures.js +0 -30
- package/dist/modules/ai_assistant/__integration__/helpers/aiAssistantFixtures.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/chat/route.js +0 -7
- package/dist/modules/ai_assistant/api/ai/chat/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/settings/allowlist/route.js +10 -10
- package/dist/modules/ai_assistant/api/settings/allowlist/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/settings/route.js +13 -34
- package/dist/modules/ai_assistant/api/settings/route.js.map +2 -2
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.js +0 -106
- package/dist/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.js.map +2 -2
- package/dist/modules/ai_assistant/data/entities.js +0 -42
- package/dist/modules/ai_assistant/data/entities.js.map +2 -2
- package/dist/modules/ai_assistant/data/repositories/AiAgentRuntimeOverrideRepository.js +1 -3
- package/dist/modules/ai_assistant/data/repositories/AiAgentRuntimeOverrideRepository.js.map +2 -2
- package/dist/modules/ai_assistant/di.js +2 -6
- package/dist/modules/ai_assistant/di.js.map +2 -2
- package/dist/modules/ai_assistant/events.js +0 -6
- package/dist/modules/ai_assistant/events.js.map +2 -2
- package/dist/modules/ai_assistant/i18n/de.json +0 -42
- package/dist/modules/ai_assistant/i18n/en.json +0 -42
- package/dist/modules/ai_assistant/i18n/es.json +0 -42
- package/dist/modules/ai_assistant/i18n/pl.json +0 -42
- package/dist/modules/ai_assistant/lib/agent-runtime.js +5 -158
- package/dist/modules/ai_assistant/lib/agent-runtime.js.map +2 -2
- package/dist/modules/ai_assistant/lib/ai-agent-definition.js.map +2 -2
- package/dist/modules/ai_assistant/lib/auth.js +7 -6
- package/dist/modules/ai_assistant/lib/auth.js.map +2 -2
- package/dist/modules/ai_assistant/lib/entity-graph.js +3 -3
- package/dist/modules/ai_assistant/lib/entity-graph.js.map +2 -2
- package/dist/modules/ai_assistant/lib/llm-adapters/anthropic.js +0 -3
- package/dist/modules/ai_assistant/lib/llm-adapters/anthropic.js.map +2 -2
- package/dist/modules/ai_assistant/lib/llm-adapters/openai.js +1 -8
- package/dist/modules/ai_assistant/lib/llm-adapters/openai.js.map +2 -2
- package/dist/modules/ai_assistant/lib/model-factory.js +1 -6
- package/dist/modules/ai_assistant/lib/model-factory.js.map +2 -2
- package/dist/modules/ai_assistant/lib/openai-compatible-presets.js +0 -2
- package/dist/modules/ai_assistant/lib/openai-compatible-presets.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-search.js +12 -4
- package/dist/modules/ai_assistant/lib/tool-search.js.map +2 -2
- package/generated/entities/ai_agent_runtime_override/index.ts +0 -1
- package/generated/entities.ids.generated.ts +1 -2
- package/generated/entity-fields-registry.ts +1 -13
- package/package.json +12 -13
- package/src/modules/ai_assistant/__integration__/helpers/aiAssistantFixtures.ts +0 -48
- package/src/modules/ai_assistant/api/ai/chat/route.ts +0 -10
- package/src/modules/ai_assistant/api/settings/__tests__/route.test.ts +8 -58
- package/src/modules/ai_assistant/api/settings/allowlist/route.ts +12 -10
- package/src/modules/ai_assistant/api/settings/route.ts +10 -33
- package/src/modules/ai_assistant/backend/config/ai-assistant/agents/AiAgentSettingsPageClient.tsx +0 -146
- package/src/modules/ai_assistant/data/entities.ts +0 -56
- package/src/modules/ai_assistant/data/repositories/AiAgentRuntimeOverrideRepository.ts +0 -4
- package/src/modules/ai_assistant/di.ts +1 -5
- package/src/modules/ai_assistant/events.ts +0 -21
- package/src/modules/ai_assistant/i18n/de.json +0 -42
- package/src/modules/ai_assistant/i18n/en.json +0 -42
- package/src/modules/ai_assistant/i18n/es.json +0 -42
- package/src/modules/ai_assistant/i18n/pl.json +0 -42
- package/src/modules/ai_assistant/lib/__tests__/auth.test.ts +5 -2
- package/src/modules/ai_assistant/lib/__tests__/llm-adapters-anthropic.test.ts +0 -13
- package/src/modules/ai_assistant/lib/__tests__/llm-adapters-openai.test.ts +0 -41
- package/src/modules/ai_assistant/lib/__tests__/model-factory.test.ts +0 -60
- package/src/modules/ai_assistant/lib/agent-runtime.ts +0 -259
- package/src/modules/ai_assistant/lib/ai-agent-definition.ts +0 -10
- package/src/modules/ai_assistant/lib/auth.ts +15 -6
- package/src/modules/ai_assistant/lib/entity-graph.ts +3 -3
- package/src/modules/ai_assistant/lib/llm-adapters/anthropic.ts +0 -10
- package/src/modules/ai_assistant/lib/llm-adapters/openai.ts +1 -32
- package/src/modules/ai_assistant/lib/model-factory.ts +0 -33
- package/src/modules/ai_assistant/lib/openai-compatible-presets.ts +0 -2
- package/src/modules/ai_assistant/lib/tool-search.ts +17 -4
- package/src/modules/ai_assistant/migrations/.snapshot-open-mercato.json +16 -388
- package/dist/modules/ai_assistant/__integration__/TC-AI-MODERATION-008-settings-and-audit.spec.js +0 -80
- package/dist/modules/ai_assistant/__integration__/TC-AI-MODERATION-008-settings-and-audit.spec.js.map +0 -7
- package/dist/modules/ai_assistant/__integration__/TC-AI-MODERATION-009-ui.spec.js +0 -124
- package/dist/modules/ai_assistant/__integration__/TC-AI-MODERATION-009-ui.spec.js.map +0 -7
- package/dist/modules/ai_assistant/api/moderation-flags/route.js +0 -121
- package/dist/modules/ai_assistant/api/moderation-flags/route.js.map +0 -7
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/AiModerationFlagsPageClient.js +0 -139
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/AiModerationFlagsPageClient.js.map +0 -7
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.js +0 -10
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.js.map +0 -7
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.meta.js +0 -26
- package/dist/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.meta.js.map +0 -7
- package/dist/modules/ai_assistant/data/repositories/AiModerationFlagRepository.js +0 -44
- package/dist/modules/ai_assistant/data/repositories/AiModerationFlagRepository.js.map +0 -7
- package/dist/modules/ai_assistant/i18n/ko.json +0 -527
- package/dist/modules/ai_assistant/lib/moderation-flag-recorder.js +0 -43
- package/dist/modules/ai_assistant/lib/moderation-flag-recorder.js.map +0 -7
- package/dist/modules/ai_assistant/lib/moderation-policy.js +0 -24
- package/dist/modules/ai_assistant/lib/moderation-policy.js.map +0 -7
- package/dist/modules/ai_assistant/lib/moderation.js +0 -109
- package/dist/modules/ai_assistant/lib/moderation.js.map +0 -7
- package/dist/modules/ai_assistant/migrations/Migration20260610134045_ai_assistant.js +0 -19
- package/dist/modules/ai_assistant/migrations/Migration20260610134045_ai_assistant.js.map +0 -7
- package/generated/entities/ai_moderation_flag/index.ts +0 -9
- package/src/modules/ai_assistant/__integration__/TC-AI-MODERATION-008-settings-and-audit.spec.ts +0 -126
- package/src/modules/ai_assistant/__integration__/TC-AI-MODERATION-009-ui.spec.ts +0 -161
- package/src/modules/ai_assistant/api/moderation-flags/__tests__/route.test.ts +0 -122
- package/src/modules/ai_assistant/api/moderation-flags/route.ts +0 -137
- package/src/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/AiModerationFlagsPageClient.tsx +0 -186
- package/src/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.meta.ts +0 -24
- package/src/modules/ai_assistant/backend/config/ai-assistant/moderation-flags/page.tsx +0 -12
- package/src/modules/ai_assistant/data/repositories/AiModerationFlagRepository.ts +0 -79
- package/src/modules/ai_assistant/i18n/ko.json +0 -527
- package/src/modules/ai_assistant/lib/__tests__/input-moderation-gate.test.ts +0 -121
- package/src/modules/ai_assistant/lib/__tests__/moderation-flag-recorder.test.ts +0 -76
- package/src/modules/ai_assistant/lib/__tests__/moderation-policy.test.ts +0 -86
- package/src/modules/ai_assistant/lib/__tests__/moderation.test.ts +0 -114
- package/src/modules/ai_assistant/lib/moderation-flag-recorder.ts +0 -75
- package/src/modules/ai_assistant/lib/moderation-policy.ts +0 -77
- package/src/modules/ai_assistant/lib/moderation.ts +0 -179
- package/src/modules/ai_assistant/migrations/Migration20260610134045_ai_assistant.ts +0 -21
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
2
|
-
import { AiModerationFlagRepository } from "../data/repositories/AiModerationFlagRepository.js";
|
|
3
|
-
import { emitAiAssistantEvent } from "../events.js";
|
|
4
|
-
const logger = createLogger("ai_assistant");
|
|
5
|
-
async function recordModerationFlag(input, container) {
|
|
6
|
-
if (!container) return;
|
|
7
|
-
if (!input.tenantId) return;
|
|
8
|
-
let createdId = null;
|
|
9
|
-
try {
|
|
10
|
-
const em = container.resolve("em");
|
|
11
|
-
const repo = new AiModerationFlagRepository(em.fork());
|
|
12
|
-
const flag = await repo.create({
|
|
13
|
-
tenantId: input.tenantId,
|
|
14
|
-
organizationId: input.organizationId,
|
|
15
|
-
agentId: input.agentId,
|
|
16
|
-
userId: input.userId,
|
|
17
|
-
providerId: input.providerId,
|
|
18
|
-
modelId: input.modelId,
|
|
19
|
-
categories: input.categories
|
|
20
|
-
});
|
|
21
|
-
createdId = flag.id;
|
|
22
|
-
} catch (error) {
|
|
23
|
-
logger.error("Failed to persist moderation flag (rejection still applies)", { err: error });
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
try {
|
|
27
|
-
const flaggedCategories = Object.entries(input.categories).filter(([, value]) => value.flagged).map(([name]) => name);
|
|
28
|
-
await emitAiAssistantEvent("ai_assistant.moderation_flag.created", {
|
|
29
|
-
id: createdId,
|
|
30
|
-
tenantId: input.tenantId,
|
|
31
|
-
organizationId: input.organizationId,
|
|
32
|
-
agentId: input.agentId,
|
|
33
|
-
userId: input.userId,
|
|
34
|
-
categories: flaggedCategories
|
|
35
|
-
});
|
|
36
|
-
} catch (error) {
|
|
37
|
-
logger.error("Failed to emit moderation_flag.created (audit row persisted)", { err: error });
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
recordModerationFlag
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=moderation-flag-recorder.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/modules/ai_assistant/lib/moderation-flag-recorder.ts"],
|
|
4
|
-
"sourcesContent": ["import type { AwilixContainer } from 'awilix'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { AiModerationFlagRepository } from '../data/repositories/AiModerationFlagRepository'\nimport { emitAiAssistantEvent } from '../events'\nimport type { ModerationCategoryResult } from './moderation'\n\nconst logger = createLogger('ai_assistant')\n\nexport interface RecordModerationFlagInput {\n tenantId: string | null\n organizationId: string | null\n agentId: string\n userId: string\n providerId: string\n modelId: string\n categories: Record<string, ModerationCategoryResult>\n}\n\n/**\n * Persists one `ai_moderation_flags` audit row and emits\n * `ai_assistant.moderation_flag.created`, best-effort.\n *\n * SAFETY CONTRACT: this MUST NEVER throw \u2014 the moderation rejection is the\n * primary effect and is thrown by the gate regardless. A missing tenant scope\n * (e.g. a system-scope caller) means the row cannot be tenant-scoped, so the\n * audit write is skipped. Any persistence/emit failure is logged and\n * swallowed so the user still receives the rejection.\n *\n * Spec `2026-06-04-ai-input-moderation-and-safety-identifiers`.\n */\nexport async function recordModerationFlag(\n input: RecordModerationFlagInput,\n container: AwilixContainer | undefined,\n): Promise<void> {\n if (!container) return\n if (!input.tenantId) return\n\n let createdId: string | null = null\n try {\n const em = container.resolve<EntityManager>('em')\n const repo = new AiModerationFlagRepository(em.fork())\n const flag = await repo.create({\n tenantId: input.tenantId,\n organizationId: input.organizationId,\n agentId: input.agentId,\n userId: input.userId,\n providerId: input.providerId,\n modelId: input.modelId,\n categories: input.categories,\n })\n createdId = flag.id\n } catch (error) {\n logger.error('Failed to persist moderation flag (rejection still applies)', { err: error })\n return\n }\n\n // Emit AFTER a successful write, detached so a failed emit never retroactively\n // fails the audit insert.\n try {\n const flaggedCategories = Object.entries(input.categories)\n .filter(([, value]) => value.flagged)\n .map(([name]) => name)\n await emitAiAssistantEvent('ai_assistant.moderation_flag.created', {\n id: createdId,\n tenantId: input.tenantId,\n organizationId: input.organizationId,\n agentId: input.agentId,\n userId: input.userId,\n categories: flaggedCategories,\n })\n } catch (error) {\n logger.error('Failed to emit moderation_flag.created (audit row persisted)', { err: error })\n }\n}\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,oBAAoB;AAC7B,SAAS,kCAAkC;AAC3C,SAAS,4BAA4B;AAGrC,MAAM,SAAS,aAAa,cAAc;AAwB1C,eAAsB,qBACpB,OACA,WACe;AACf,MAAI,CAAC,UAAW;AAChB,MAAI,CAAC,MAAM,SAAU;AAErB,MAAI,YAA2B;AAC/B,MAAI;AACF,UAAM,KAAK,UAAU,QAAuB,IAAI;AAChD,UAAM,OAAO,IAAI,2BAA2B,GAAG,KAAK,CAAC;AACrD,UAAM,OAAO,MAAM,KAAK,OAAO;AAAA,MAC7B,UAAU,MAAM;AAAA,MAChB,gBAAgB,MAAM;AAAA,MACtB,SAAS,MAAM;AAAA,MACf,QAAQ,MAAM;AAAA,MACd,YAAY,MAAM;AAAA,MAClB,SAAS,MAAM;AAAA,MACf,YAAY,MAAM;AAAA,IACpB,CAAC;AACD,gBAAY,KAAK;AAAA,EACnB,SAAS,OAAO;AACd,WAAO,MAAM,+DAA+D,EAAE,KAAK,MAAM,CAAC;AAC1F;AAAA,EACF;AAIA,MAAI;AACF,UAAM,oBAAoB,OAAO,QAAQ,MAAM,UAAU,EACtD,OAAO,CAAC,CAAC,EAAE,KAAK,MAAM,MAAM,OAAO,EACnC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI;AACvB,UAAM,qBAAqB,wCAAwC;AAAA,MACjE,IAAI;AAAA,MACJ,UAAU,MAAM;AAAA,MAChB,gBAAgB,MAAM;AAAA,MACtB,SAAS,MAAM;AAAA,MACf,QAAQ,MAAM;AAAA,MACd,YAAY;AAAA,IACd,CAAC;AAAA,EACH,SAAS,OAAO;AACd,WAAO,MAAM,gEAAgE,EAAE,KAAK,MAAM,CAAC;AAAA,EAC7F;AACF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
|
|
2
|
-
function resolveModerationPolicy(input) {
|
|
3
|
-
if (input.untrustedInput === true) return "enforced";
|
|
4
|
-
if (input.perAgentOverride !== null && input.perAgentOverride !== void 0) {
|
|
5
|
-
return input.perAgentOverride ? "on" : "off";
|
|
6
|
-
}
|
|
7
|
-
if (input.tenantWideOverride !== null && input.tenantWideOverride !== void 0) {
|
|
8
|
-
return input.tenantWideOverride ? "on" : "off";
|
|
9
|
-
}
|
|
10
|
-
const env = input.env ?? process.env;
|
|
11
|
-
return parseBooleanWithDefault(env.OM_AI_INPUT_MODERATION, false) ? "on" : "off";
|
|
12
|
-
}
|
|
13
|
-
function isModerationActive(policy) {
|
|
14
|
-
return policy === "enforced" || policy === "on";
|
|
15
|
-
}
|
|
16
|
-
function shouldFailClosed(policy) {
|
|
17
|
-
return policy === "enforced";
|
|
18
|
-
}
|
|
19
|
-
export {
|
|
20
|
-
isModerationActive,
|
|
21
|
-
resolveModerationPolicy,
|
|
22
|
-
shouldFailClosed
|
|
23
|
-
};
|
|
24
|
-
//# sourceMappingURL=moderation-policy.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/modules/ai_assistant/lib/moderation-policy.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Input-moderation policy resolution.\n *\n * Resolves the effective moderation policy for an agent turn using a fixed\n * precedence (first match wins):\n *\n * 1. `agentDef.untrustedInput === true` \u2192 `enforced` (tenant cannot disable).\n * 2. Per-agent tenant override (`ai_agent_runtime_overrides` row with an\n * `agent_id`) `input_moderation` \u2192 `on` / `off`.\n * 3. Tenant-wide override (row with `agent_id = NULL`) `input_moderation` \u2192\n * `on` / `off`.\n * 4. Env default `OM_AI_INPUT_MODERATION` (parsed via `parseBooleanWithDefault`)\n * \u2192 `on` / `off`.\n * 5. Default `off`.\n *\n * Steps 4 and 5 collapse into `parseBooleanWithDefault(env, false)`.\n *\n * @see .ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md\n */\n\nimport type { EnvLookup } from '@open-mercato/shared/lib/ai/llm-provider'\nimport { parseBooleanWithDefault } from '@open-mercato/shared/lib/boolean'\n\n/** Effective runtime moderation policy for a turn. */\nexport type ModerationPolicy = 'enforced' | 'on' | 'off'\n\n/** The per-agent display state shown in settings (adds `inherit`). */\nexport type ModerationPolicyDisplay = 'enforced' | 'on' | 'off' | 'inherit'\n\nexport interface ResolveModerationPolicyInput {\n /** Whether the agent definition marks this surface as untrusted. */\n untrustedInput?: boolean\n /**\n * `input_moderation` from the agent-scoped tenant override row, or\n * `null`/`undefined` when the row is absent or leaves it unset (inherit).\n */\n perAgentOverride?: boolean | null\n /**\n * `input_moderation` from the tenant-wide override row (`agent_id = NULL`),\n * or `null`/`undefined` when absent/unset (inherit).\n */\n tenantWideOverride?: boolean | null\n /** Env lookup; reads `OM_AI_INPUT_MODERATION`. Defaults to `process.env`. */\n env?: EnvLookup\n}\n\n/**\n * Resolves the effective moderation policy following the 5-step precedence.\n * Pure and synchronous \u2014 the runtime hydrates the override values from the DB\n * before calling this.\n */\nexport function resolveModerationPolicy(input: ResolveModerationPolicyInput): ModerationPolicy {\n if (input.untrustedInput === true) return 'enforced'\n if (input.perAgentOverride !== null && input.perAgentOverride !== undefined) {\n return input.perAgentOverride ? 'on' : 'off'\n }\n if (input.tenantWideOverride !== null && input.tenantWideOverride !== undefined) {\n return input.tenantWideOverride ? 'on' : 'off'\n }\n const env = input.env ?? process.env\n return parseBooleanWithDefault(env.OM_AI_INPUT_MODERATION, false) ? 'on' : 'off'\n}\n\n/** Whether a resolved policy should run the moderation gate for the turn. */\nexport function isModerationActive(policy: ModerationPolicy): boolean {\n return policy === 'enforced' || policy === 'on'\n}\n\n/**\n * Whether an *unavailable* moderation check (endpoint outage) should fail\n * closed (reject the turn) rather than fail open (allow it through). Per spec:\n * only `enforced` (untrusted) surfaces fail closed; opt-in `on` surfaces prefer\n * availability and fail open. Both paths log. (`off` never reaches the gate.)\n */\nexport function shouldFailClosed(policy: ModerationPolicy): boolean {\n return policy === 'enforced'\n}\n"],
|
|
5
|
-
"mappings": "AAqBA,SAAS,+BAA+B;AA8BjC,SAAS,wBAAwB,OAAuD;AAC7F,MAAI,MAAM,mBAAmB,KAAM,QAAO;AAC1C,MAAI,MAAM,qBAAqB,QAAQ,MAAM,qBAAqB,QAAW;AAC3E,WAAO,MAAM,mBAAmB,OAAO;AAAA,EACzC;AACA,MAAI,MAAM,uBAAuB,QAAQ,MAAM,uBAAuB,QAAW;AAC/E,WAAO,MAAM,qBAAqB,OAAO;AAAA,EAC3C;AACA,QAAM,MAAM,MAAM,OAAO,QAAQ;AACjC,SAAO,wBAAwB,IAAI,wBAAwB,KAAK,IAAI,OAAO;AAC7E;AAGO,SAAS,mBAAmB,QAAmC;AACpE,SAAO,WAAW,cAAc,WAAW;AAC7C;AAQO,SAAS,iBAAiB,QAAmC;AAClE,SAAO,WAAW;AACpB;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
const DEFAULT_MODERATION_MODEL = "omni-moderation-latest";
|
|
3
|
-
const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
4
|
-
const REQUEST_TIMEOUT_MS = 3e3;
|
|
5
|
-
class AiModerationBlockedError extends Error {
|
|
6
|
-
constructor(categories) {
|
|
7
|
-
super("[internal] Input rejected by the content safety filter");
|
|
8
|
-
this.name = "AiModerationBlockedError";
|
|
9
|
-
this.categories = categories;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
class AiModerationUnavailableError extends Error {
|
|
13
|
-
constructor(message, cause) {
|
|
14
|
-
super(`[internal] ${message}`);
|
|
15
|
-
this.name = "AiModerationUnavailableError";
|
|
16
|
-
this.cause = cause;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
const moderationResponseSchema = z.object({
|
|
20
|
-
results: z.array(
|
|
21
|
-
z.object({
|
|
22
|
-
flagged: z.boolean(),
|
|
23
|
-
categories: z.record(z.string(), z.boolean()),
|
|
24
|
-
category_scores: z.record(z.string(), z.number())
|
|
25
|
-
})
|
|
26
|
-
).min(1)
|
|
27
|
-
});
|
|
28
|
-
function mergeCategories(categories, scores) {
|
|
29
|
-
const merged = {};
|
|
30
|
-
for (const [name, flagged] of Object.entries(categories)) {
|
|
31
|
-
merged[name] = { flagged, score: scores[name] ?? 0 };
|
|
32
|
-
}
|
|
33
|
-
return merged;
|
|
34
|
-
}
|
|
35
|
-
async function postModeration(input) {
|
|
36
|
-
const baseURL = (input.baseURL ?? DEFAULT_OPENAI_BASE_URL).replace(/\/$/, "");
|
|
37
|
-
const controller = new AbortController();
|
|
38
|
-
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
39
|
-
try {
|
|
40
|
-
return await fetch(`${baseURL}/moderations`, {
|
|
41
|
-
method: "POST",
|
|
42
|
-
headers: {
|
|
43
|
-
"Content-Type": "application/json",
|
|
44
|
-
Authorization: `Bearer ${input.apiKey}`
|
|
45
|
-
},
|
|
46
|
-
body: JSON.stringify({
|
|
47
|
-
model: input.model ?? DEFAULT_MODERATION_MODEL,
|
|
48
|
-
input: input.text
|
|
49
|
-
}),
|
|
50
|
-
signal: controller.signal
|
|
51
|
-
});
|
|
52
|
-
} finally {
|
|
53
|
-
clearTimeout(timer);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function createModerationService() {
|
|
57
|
-
return {
|
|
58
|
-
async checkInput(input) {
|
|
59
|
-
let lastError;
|
|
60
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
61
|
-
let response;
|
|
62
|
-
try {
|
|
63
|
-
response = await postModeration(input);
|
|
64
|
-
} catch (error) {
|
|
65
|
-
lastError = error;
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
if (response.status >= 500) {
|
|
69
|
-
lastError = new Error(`moderation endpoint returned ${response.status}`);
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
if (!response.ok) {
|
|
73
|
-
throw new AiModerationUnavailableError(
|
|
74
|
-
`moderation endpoint returned ${response.status}`
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
let json;
|
|
78
|
-
try {
|
|
79
|
-
json = await response.json();
|
|
80
|
-
} catch (error) {
|
|
81
|
-
throw new AiModerationUnavailableError("moderation response was not valid JSON", error);
|
|
82
|
-
}
|
|
83
|
-
const parsed = moderationResponseSchema.safeParse(json);
|
|
84
|
-
if (!parsed.success) {
|
|
85
|
-
throw new AiModerationUnavailableError(
|
|
86
|
-
"moderation response did not match the expected schema",
|
|
87
|
-
parsed.error
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
const result = parsed.data.results[0];
|
|
91
|
-
return {
|
|
92
|
-
flagged: result.flagged,
|
|
93
|
-
categories: mergeCategories(result.categories, result.category_scores)
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
throw new AiModerationUnavailableError(
|
|
97
|
-
"moderation endpoint unreachable after retry",
|
|
98
|
-
lastError
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
export {
|
|
104
|
-
AiModerationBlockedError,
|
|
105
|
-
AiModerationUnavailableError,
|
|
106
|
-
DEFAULT_MODERATION_MODEL,
|
|
107
|
-
createModerationService
|
|
108
|
-
};
|
|
109
|
-
//# sourceMappingURL=moderation.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/modules/ai_assistant/lib/moderation.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Input pre-moderation service.\n *\n * Wraps OpenAI's free `/v1/moderations` endpoint so the agent runtime can\n * screen the current user turn BEFORE the model call on surfaces that opt in\n * (or are forced on via `untrustedInput`). The service is intentionally pure:\n * it performs the HTTP check and returns category flags + scores, or throws\n * {@link AiModerationUnavailableError} when the endpoint is unreachable. It does\n * NOT persist audit records, emit events, or throw the user-facing block \u2014 the\n * runtime gate orchestrates those (see `agent-runtime.ts`).\n *\n * @see .ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md\n */\n\nimport { z } from 'zod'\n\n/** Default moderation model; override with `OM_AI_MODERATION_MODEL`. */\nexport const DEFAULT_MODERATION_MODEL = 'omni-moderation-latest'\n\nconst DEFAULT_OPENAI_BASE_URL = 'https://api.openai.com/v1'\nconst REQUEST_TIMEOUT_MS = 3000\n\n/** Per-category moderation outcome: whether it tripped and the model's score. */\nexport interface ModerationCategoryResult {\n flagged: boolean\n score: number\n}\n\n/** Result of an input moderation check \u2014 flags + scores only, never content. */\nexport interface ModerationResult {\n flagged: boolean\n categories: Record<string, ModerationCategoryResult>\n}\n\n/** Arguments for {@link ModerationService.checkInput}. */\nexport interface ModerationCheckInput {\n /** The current user turn text to screen. */\n text: string\n /** Resolved OpenAI(-compatible) API key for the moderations endpoint. */\n apiKey: string\n /** Optional base URL override (defaults to the OpenAI public endpoint). */\n baseURL?: string\n /** Optional model override (defaults to {@link DEFAULT_MODERATION_MODEL}). */\n model?: string\n}\n\n/** Port resolved from the DI container by the agent runtime. */\nexport interface ModerationService {\n checkInput(input: ModerationCheckInput): Promise<ModerationResult>\n}\n\n/**\n * Thrown by the runtime gate (NOT the service) when flagged input is rejected\n * on an enabled/enforced surface. The SSE error path maps it to the\n * `moderation_blocked` code; the raw categories are never sent to the client.\n */\nexport class AiModerationBlockedError extends Error {\n readonly categories: Record<string, ModerationCategoryResult>\n\n constructor(categories: Record<string, ModerationCategoryResult>) {\n super('[internal] Input rejected by the content safety filter')\n this.name = 'AiModerationBlockedError'\n this.categories = categories\n }\n}\n\n/**\n * Thrown by {@link ModerationService.checkInput} when the moderation endpoint\n * is unreachable (timeout, network error, or non-2xx after one retry). The gate\n * decides fail-closed (enforced surfaces) vs fail-open (opt-in surfaces).\n */\nexport class AiModerationUnavailableError extends Error {\n readonly cause?: unknown\n\n constructor(message: string, cause?: unknown) {\n super(`[internal] ${message}`)\n this.name = 'AiModerationUnavailableError'\n this.cause = cause\n }\n}\n\nconst moderationResponseSchema = z.object({\n results: z\n .array(\n z.object({\n flagged: z.boolean(),\n categories: z.record(z.string(), z.boolean()),\n category_scores: z.record(z.string(), z.number()),\n }),\n )\n .min(1),\n})\n\nfunction mergeCategories(\n categories: Record<string, boolean>,\n scores: Record<string, number>,\n): Record<string, ModerationCategoryResult> {\n const merged: Record<string, ModerationCategoryResult> = {}\n for (const [name, flagged] of Object.entries(categories)) {\n merged[name] = { flagged, score: scores[name] ?? 0 }\n }\n return merged\n}\n\nasync function postModeration(input: ModerationCheckInput): Promise<Response> {\n const baseURL = (input.baseURL ?? DEFAULT_OPENAI_BASE_URL).replace(/\\/$/, '')\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)\n try {\n return await fetch(`${baseURL}/moderations`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${input.apiKey}`,\n },\n body: JSON.stringify({\n model: input.model ?? DEFAULT_MODERATION_MODEL,\n input: input.text,\n }),\n signal: controller.signal,\n })\n } finally {\n clearTimeout(timer)\n }\n}\n\n/**\n * Builds the default {@link ModerationService}. Stateless and DI-friendly \u2014\n * registered in `di.ts` and overridable by downstream apps.\n */\nexport function createModerationService(): ModerationService {\n return {\n async checkInput(input: ModerationCheckInput): Promise<ModerationResult> {\n let lastError: unknown\n // One short-timeout attempt + one retry (R: outage mitigation).\n for (let attempt = 0; attempt < 2; attempt += 1) {\n let response: Response\n try {\n response = await postModeration(input)\n } catch (error) {\n lastError = error\n continue\n }\n if (response.status >= 500) {\n lastError = new Error(`moderation endpoint returned ${response.status}`)\n continue\n }\n if (!response.ok) {\n // 4xx is a configuration/credential error \u2014 not transient; do not retry.\n throw new AiModerationUnavailableError(\n `moderation endpoint returned ${response.status}`,\n )\n }\n let json: unknown\n try {\n json = await response.json()\n } catch (error) {\n throw new AiModerationUnavailableError('moderation response was not valid JSON', error)\n }\n const parsed = moderationResponseSchema.safeParse(json)\n if (!parsed.success) {\n throw new AiModerationUnavailableError(\n 'moderation response did not match the expected schema',\n parsed.error,\n )\n }\n const result = parsed.data.results[0]\n return {\n flagged: result.flagged,\n categories: mergeCategories(result.categories, result.category_scores),\n }\n }\n throw new AiModerationUnavailableError(\n 'moderation endpoint unreachable after retry',\n lastError,\n )\n },\n }\n}\n"],
|
|
5
|
-
"mappings": "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",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Migration } from "@mikro-orm/migrations";
|
|
2
|
-
class Migration20260610134045_ai_assistant extends Migration {
|
|
3
|
-
up() {
|
|
4
|
-
this.addSql(`create table "ai_moderation_flags" ("id" uuid not null default gen_random_uuid(), "tenant_id" uuid not null, "organization_id" uuid null, "agent_id" text not null, "user_id" text not null, "provider_id" text not null, "model_id" text not null, "categories" jsonb not null, "created_at" timestamptz not null, primary key ("id"));`);
|
|
5
|
-
this.addSql(`create index "ai_moderation_flags_tenant_user_idx" on "ai_moderation_flags" ("tenant_id", "user_id");`);
|
|
6
|
-
this.addSql(`create index "ai_moderation_flags_tenant_created_idx" on "ai_moderation_flags" ("tenant_id", "created_at");`);
|
|
7
|
-
this.addSql(`alter table "ai_agent_runtime_overrides" add "input_moderation" boolean null;`);
|
|
8
|
-
}
|
|
9
|
-
down() {
|
|
10
|
-
this.addSql(`alter table "ai_agent_runtime_overrides" drop column "input_moderation";`);
|
|
11
|
-
this.addSql(`drop index "ai_moderation_flags_tenant_created_idx";`);
|
|
12
|
-
this.addSql(`drop index "ai_moderation_flags_tenant_user_idx";`);
|
|
13
|
-
this.addSql(`drop table if exists "ai_moderation_flags" cascade;`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export {
|
|
17
|
-
Migration20260610134045_ai_assistant
|
|
18
|
-
};
|
|
19
|
-
//# sourceMappingURL=Migration20260610134045_ai_assistant.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/modules/ai_assistant/migrations/Migration20260610134045_ai_assistant.ts"],
|
|
4
|
-
"sourcesContent": ["import { Migration } from '@mikro-orm/migrations';\n\nexport class Migration20260610134045_ai_assistant extends Migration {\n\n override up(): void | Promise<void> {\n this.addSql(`create table \"ai_moderation_flags\" (\"id\" uuid not null default gen_random_uuid(), \"tenant_id\" uuid not null, \"organization_id\" uuid null, \"agent_id\" text not null, \"user_id\" text not null, \"provider_id\" text not null, \"model_id\" text not null, \"categories\" jsonb not null, \"created_at\" timestamptz not null, primary key (\"id\"));`);\n this.addSql(`create index \"ai_moderation_flags_tenant_user_idx\" on \"ai_moderation_flags\" (\"tenant_id\", \"user_id\");`);\n this.addSql(`create index \"ai_moderation_flags_tenant_created_idx\" on \"ai_moderation_flags\" (\"tenant_id\", \"created_at\");`);\n\n this.addSql(`alter table \"ai_agent_runtime_overrides\" add \"input_moderation\" boolean null;`);\n }\n\n override down(): void | Promise<void> {\n this.addSql(`alter table \"ai_agent_runtime_overrides\" drop column \"input_moderation\";`);\n\n this.addSql(`drop index \"ai_moderation_flags_tenant_created_idx\";`);\n this.addSql(`drop index \"ai_moderation_flags_tenant_user_idx\";`);\n this.addSql(`drop table if exists \"ai_moderation_flags\" cascade;`);\n }\n\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,iBAAiB;AAEnB,MAAM,6CAA6C,UAAU;AAAA,EAEzD,KAA2B;AAClC,SAAK,OAAO,0UAA0U;AACtV,SAAK,OAAO,uGAAuG;AACnH,SAAK,OAAO,6GAA6G;AAEzH,SAAK,OAAO,+EAA+E;AAAA,EAC7F;AAAA,EAES,OAA6B;AACpC,SAAK,OAAO,0EAA0E;AAEtF,SAAK,OAAO,sDAAsD;AAClE,SAAK,OAAO,mDAAmD;AAC/D,SAAK,OAAO,qDAAqD;AAAA,EACnE;AAEF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export const id = "id";
|
|
2
|
-
export const tenant_id = "tenant_id";
|
|
3
|
-
export const organization_id = "organization_id";
|
|
4
|
-
export const agent_id = "agent_id";
|
|
5
|
-
export const user_id = "user_id";
|
|
6
|
-
export const provider_id = "provider_id";
|
|
7
|
-
export const model_id = "model_id";
|
|
8
|
-
export const categories = "categories";
|
|
9
|
-
export const created_at = "created_at";
|
package/src/modules/ai_assistant/__integration__/TC-AI-MODERATION-008-settings-and-audit.spec.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { test, expect, request as playwrightRequest } from '@playwright/test';
|
|
2
|
-
import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api';
|
|
3
|
-
import { getTokenScope, readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures';
|
|
4
|
-
import {
|
|
5
|
-
deleteAgentOverridesInDb,
|
|
6
|
-
deleteModerationFlagsInDb,
|
|
7
|
-
seedModerationFlagInDb,
|
|
8
|
-
} from './helpers/aiAssistantFixtures';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* TC-AI-MODERATION-008 — Input-moderation settings + audit API.
|
|
12
|
-
* Source: spec `.ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md`.
|
|
13
|
-
*
|
|
14
|
-
* Surfaces under test:
|
|
15
|
-
* - PUT /api/ai_assistant/settings (persists `inputModeration` per agent)
|
|
16
|
-
* - GET /api/ai_assistant/settings (reflects per-agent effective moderation policy)
|
|
17
|
-
* - GET /api/ai_assistant/moderation-flags (tenant-scoped audit listing)
|
|
18
|
-
*
|
|
19
|
-
* Contract notes verified against the route handlers:
|
|
20
|
-
* - PUT `{ agentId, inputModeration }` returns 200 echoing `inputModeration`.
|
|
21
|
-
* - GET /settings exposes `agents[].moderation = { enforced, override, effective }`.
|
|
22
|
-
* - GET /moderation-flags returns `{ items, total, page, pageSize }`, tenant-scoped,
|
|
23
|
-
* gated by `ai_assistant.settings.manage`, `pageSize` capped at 100.
|
|
24
|
-
*
|
|
25
|
-
* Flags are seeded directly in the DB: a real flag is only ever born from the gate
|
|
26
|
-
* during a flagged LLM turn (needs a live moderation call), so seeding keeps the
|
|
27
|
-
* audit + isolation coverage deterministic and provider-free. This spec therefore
|
|
28
|
-
* only runs under the coherent app+DB harness (`yarn test:integration` / `:ephemeral`).
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
const SETTINGS = '/api/ai_assistant/settings';
|
|
32
|
-
const FLAGS = '/api/ai_assistant/moderation-flags';
|
|
33
|
-
|
|
34
|
-
interface FlagsResponse {
|
|
35
|
-
items: Array<{ id: string; tenantId: string; agentId: string; userId: string; categories: Record<string, unknown> }>;
|
|
36
|
-
total: number;
|
|
37
|
-
page: number;
|
|
38
|
-
pageSize: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
test.describe('TC-AI-MODERATION-008: input moderation settings + audit', () => {
|
|
42
|
-
test('PUT inputModeration persists per agent and round-trips on/off/inherit', async ({ request }) => {
|
|
43
|
-
test.slow();
|
|
44
|
-
const adminToken = await getAuthToken(request, 'admin');
|
|
45
|
-
const { tenantId } = getTokenScope(adminToken);
|
|
46
|
-
// A synthetic agent id keeps the test independent of the (empty-in-CI) live
|
|
47
|
-
// agent registry; the PUT upserts an override row keyed by this id. The
|
|
48
|
-
// per-agent `moderation` reflection on GET (registered agents only) is
|
|
49
|
-
// covered by the route unit tests + the stubbed UI spec (TC-AI-MODERATION-009).
|
|
50
|
-
const agentId = 'it.moderation_settings';
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
const on = await apiRequest(request, 'PUT', SETTINGS, {
|
|
54
|
-
token: adminToken,
|
|
55
|
-
data: { agentId, inputModeration: true },
|
|
56
|
-
});
|
|
57
|
-
expect(on.status(), 'PUT inputModeration=true returns 200').toBe(200);
|
|
58
|
-
expect((await readJsonSafe<{ inputModeration: boolean | null }>(on))?.inputModeration).toBe(true);
|
|
59
|
-
|
|
60
|
-
const off = await apiRequest(request, 'PUT', SETTINGS, {
|
|
61
|
-
token: adminToken,
|
|
62
|
-
data: { agentId, inputModeration: false },
|
|
63
|
-
});
|
|
64
|
-
expect(off.status()).toBe(200);
|
|
65
|
-
expect((await readJsonSafe<{ inputModeration: boolean | null }>(off))?.inputModeration).toBe(false);
|
|
66
|
-
|
|
67
|
-
const inherit = await apiRequest(request, 'PUT', SETTINGS, {
|
|
68
|
-
token: adminToken,
|
|
69
|
-
data: { agentId, inputModeration: null },
|
|
70
|
-
});
|
|
71
|
-
expect(inherit.status()).toBe(200);
|
|
72
|
-
expect((await readJsonSafe<{ inputModeration: boolean | null }>(inherit))?.inputModeration).toBeNull();
|
|
73
|
-
} finally {
|
|
74
|
-
await deleteAgentOverridesInDb({ tenantId, agentId }).catch(() => undefined);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('GET /moderation-flags is tenant-scoped (cross-tenant rows are never returned)', async ({ request }) => {
|
|
79
|
-
test.slow();
|
|
80
|
-
const adminToken = await getAuthToken(request, 'admin');
|
|
81
|
-
const { tenantId } = getTokenScope(adminToken);
|
|
82
|
-
const otherTenantId = '00000000-0000-4000-8000-0000000face7';
|
|
83
|
-
|
|
84
|
-
let mineId = '';
|
|
85
|
-
try {
|
|
86
|
-
mineId = await seedModerationFlagInDb({ tenantId, agentId: 'it.moderation_audit', userId: 'it-user-mine' });
|
|
87
|
-
await seedModerationFlagInDb({ tenantId: otherTenantId, agentId: 'it.moderation_audit', userId: 'it-user-other' });
|
|
88
|
-
|
|
89
|
-
const res = await apiRequest(request, 'GET', `${FLAGS}?pageSize=100`, { token: adminToken });
|
|
90
|
-
expect(res.status()).toBe(200);
|
|
91
|
-
const body = await readJsonSafe<FlagsResponse>(res);
|
|
92
|
-
expect(body?.items, 'returns an items array').toBeTruthy();
|
|
93
|
-
const ids = (body?.items ?? []).map((row) => row.id);
|
|
94
|
-
expect(ids, 'my tenant flag is visible').toContain(mineId);
|
|
95
|
-
// Every returned row is scoped to the caller's tenant — the other-tenant row never leaks.
|
|
96
|
-
for (const row of body?.items ?? []) {
|
|
97
|
-
expect(row.tenantId).toBe(tenantId);
|
|
98
|
-
}
|
|
99
|
-
} finally {
|
|
100
|
-
await deleteModerationFlagsInDb(tenantId).catch(() => undefined);
|
|
101
|
-
await deleteModerationFlagsInDb(otherTenantId).catch(() => undefined);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('GET /moderation-flags RBAC + validation gates', async ({ request, baseURL }) => {
|
|
106
|
-
const adminToken = await getAuthToken(request, 'admin');
|
|
107
|
-
|
|
108
|
-
// pageSize above the 100 cap is rejected by the zod query schema.
|
|
109
|
-
const tooLarge = await apiRequest(request, 'GET', `${FLAGS}?pageSize=101`, { token: adminToken });
|
|
110
|
-
expect(tooLarge.status()).toBe(400);
|
|
111
|
-
expect((await readJsonSafe<{ code?: string }>(tooLarge))?.code).toBe('validation_error');
|
|
112
|
-
|
|
113
|
-
// Employee carries ai_assistant.view but NOT ai_assistant.settings.manage.
|
|
114
|
-
const employeeToken = await getAuthToken(request, 'employee');
|
|
115
|
-
const denied = await apiRequest(request, 'GET', FLAGS, { token: employeeToken });
|
|
116
|
-
expect(denied.status(), 'employee lacks settings.manage -> 403').toBe(403);
|
|
117
|
-
|
|
118
|
-
const anon = await playwrightRequest.newContext({ baseURL });
|
|
119
|
-
try {
|
|
120
|
-
const res = await anon.fetch(FLAGS, { method: 'GET' });
|
|
121
|
-
expect(res.status(), 'unauthenticated GET is 401').toBe(401);
|
|
122
|
-
} finally {
|
|
123
|
-
await anon.dispose();
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { test, expect } from '@playwright/test';
|
|
2
|
-
import { login } from '@open-mercato/core/modules/core/__integration__/helpers/auth';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* TC-AI-MODERATION-009 — Moderation UI surfaces (Playwright).
|
|
6
|
-
* Source: spec `.ai/specs/2026-06-04-ai-input-moderation-and-safety-identifiers.md`.
|
|
7
|
-
*
|
|
8
|
-
* Covers the two new backend UI surfaces deterministically by stubbing the
|
|
9
|
-
* data routes (the page chrome + components render regardless of the live
|
|
10
|
-
* registry / DB):
|
|
11
|
-
* - `/backend/config/ai-assistant/moderation-flags` — audit DataTable.
|
|
12
|
-
* - `/backend/config/ai-assistant/agents` — per-agent moderation section.
|
|
13
|
-
*
|
|
14
|
-
* The chat enforced→rejection and chat-off→passthrough flows are intentionally
|
|
15
|
-
* NOT exercised here: the moderation gate runs SERVER-SIDE inside
|
|
16
|
-
* `runAiAgentText`, so `page.route()` cannot stub it, and a live-provider e2e
|
|
17
|
-
* would be non-deterministic (it needs a real OpenAI key + model behavior),
|
|
18
|
-
* which `.ai/qa/AGENTS.md` forbids. Those branches are covered deterministically
|
|
19
|
-
* by the gate unit tests (`input-moderation-gate.test.ts`), the moderation
|
|
20
|
-
* service tests, and the `<AiChat>` `moderation_blocked` render test.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const FLAGS_PAGE = '/backend/config/ai-assistant/moderation-flags';
|
|
24
|
-
const AGENTS_PAGE = '/backend/config/ai-assistant/agents';
|
|
25
|
-
|
|
26
|
-
test.describe('TC-AI-MODERATION-009: moderation UI surfaces', () => {
|
|
27
|
-
test('audit page renders a flagged row with its category badge', async ({ page }) => {
|
|
28
|
-
await login(page, 'superadmin');
|
|
29
|
-
|
|
30
|
-
await page.route('**/api/ai_assistant/moderation-flags**', async (route) => {
|
|
31
|
-
await route.fulfill({
|
|
32
|
-
status: 200,
|
|
33
|
-
contentType: 'application/json',
|
|
34
|
-
body: JSON.stringify({
|
|
35
|
-
items: [
|
|
36
|
-
{
|
|
37
|
-
id: 'flag-it-1',
|
|
38
|
-
tenantId: 't-1',
|
|
39
|
-
organizationId: null,
|
|
40
|
-
agentId: 'support.portal_assistant',
|
|
41
|
-
userId: 'hashed-user-it',
|
|
42
|
-
providerId: 'openai',
|
|
43
|
-
modelId: 'gpt-5-mini',
|
|
44
|
-
categories: { hate: { flagged: true, score: 0.97 }, violence: { flagged: false, score: 0.01 } },
|
|
45
|
-
createdAt: '2026-06-10T12:00:00.000Z',
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
total: 1,
|
|
49
|
-
page: 1,
|
|
50
|
-
pageSize: 50,
|
|
51
|
-
}),
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
await page.goto(FLAGS_PAGE, { waitUntil: 'domcontentloaded' });
|
|
56
|
-
|
|
57
|
-
await expect(page.getByText('support.portal_assistant').first()).toBeVisible({ timeout: 20_000 });
|
|
58
|
-
await expect(page.getByText('hashed-user-it').first()).toBeVisible();
|
|
59
|
-
// Only the flagged category renders as a badge; the non-flagged one is omitted.
|
|
60
|
-
await expect(page.getByText('hate', { exact: true }).first()).toBeVisible();
|
|
61
|
-
await expect(page.getByText('violence', { exact: true })).toHaveCount(0);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('audit page renders the empty state when there are no flags', async ({ page }) => {
|
|
65
|
-
await login(page, 'superadmin');
|
|
66
|
-
|
|
67
|
-
await page.route('**/api/ai_assistant/moderation-flags**', async (route) => {
|
|
68
|
-
await route.fulfill({
|
|
69
|
-
status: 200,
|
|
70
|
-
contentType: 'application/json',
|
|
71
|
-
body: JSON.stringify({ items: [], total: 0, page: 1, pageSize: 50 }),
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
await page.goto(FLAGS_PAGE, { waitUntil: 'domcontentloaded' });
|
|
76
|
-
await expect(page.getByText(/No flagged messages/i).first()).toBeVisible({ timeout: 20_000 });
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('audit page redirects an unauthenticated visit to login', async ({ browser }) => {
|
|
80
|
-
const context = await browser.newContext();
|
|
81
|
-
const page = await context.newPage();
|
|
82
|
-
try {
|
|
83
|
-
await page.goto(FLAGS_PAGE, { waitUntil: 'domcontentloaded' });
|
|
84
|
-
await page.waitForURL(/\/login/, { timeout: 15_000 });
|
|
85
|
-
expect(page.url()).toMatch(/\/login/);
|
|
86
|
-
} finally {
|
|
87
|
-
await context.close();
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('agent settings renders the input-moderation section (enforced badge for untrusted agents)', async ({
|
|
92
|
-
page,
|
|
93
|
-
}) => {
|
|
94
|
-
await login(page, 'superadmin');
|
|
95
|
-
|
|
96
|
-
const agent = {
|
|
97
|
-
id: 'support.portal_assistant',
|
|
98
|
-
moduleId: 'support',
|
|
99
|
-
label: 'Portal assistant',
|
|
100
|
-
description: 'Customer-facing support assistant.',
|
|
101
|
-
executionMode: 'chat',
|
|
102
|
-
readOnly: true,
|
|
103
|
-
mutationPolicy: 'read-only',
|
|
104
|
-
untrustedInput: true,
|
|
105
|
-
allowedTools: [],
|
|
106
|
-
acceptedMediaTypes: [],
|
|
107
|
-
tools: [],
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
await page.route('**/api/ai_assistant/ai/agents', async (route) => {
|
|
111
|
-
await route.fulfill({
|
|
112
|
-
status: 200,
|
|
113
|
-
contentType: 'application/json',
|
|
114
|
-
body: JSON.stringify({ agents: [agent], total: 1 }),
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
await page.route('**/api/ai_assistant/settings', async (route) => {
|
|
118
|
-
await route.fulfill({
|
|
119
|
-
status: 200,
|
|
120
|
-
contentType: 'application/json',
|
|
121
|
-
body: JSON.stringify({
|
|
122
|
-
availableProviders: [],
|
|
123
|
-
agents: [
|
|
124
|
-
{
|
|
125
|
-
agentId: agent.id,
|
|
126
|
-
moduleId: agent.moduleId,
|
|
127
|
-
allowRuntimeModelOverride: true,
|
|
128
|
-
codeDefaultProviderId: null,
|
|
129
|
-
codeDefaultModelId: null,
|
|
130
|
-
override: null,
|
|
131
|
-
runtimeOverrideAllowlist: {
|
|
132
|
-
env: null,
|
|
133
|
-
tenant: null,
|
|
134
|
-
effective: { providers: null, modelsByProvider: {}, hasRestrictions: false, tenantOverridesActive: false },
|
|
135
|
-
envVarNames: { providers: 'X', modelsByProvider: {} },
|
|
136
|
-
},
|
|
137
|
-
providerId: 'openai',
|
|
138
|
-
modelId: 'gpt-5-mini',
|
|
139
|
-
baseURL: null,
|
|
140
|
-
source: 'provider_default',
|
|
141
|
-
moderation: { enforced: true, override: null, effective: 'enforced' },
|
|
142
|
-
},
|
|
143
|
-
],
|
|
144
|
-
}),
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
await page.goto(AGENTS_PAGE, { waitUntil: 'domcontentloaded' });
|
|
149
|
-
|
|
150
|
-
const moderationSection = page.locator('[data-ai-agent-moderation]');
|
|
151
|
-
const settingsContainer = page.locator('[data-ai-agent-settings]');
|
|
152
|
-
// Either the moderation section rendered (registry+detail loaded from the stub)
|
|
153
|
-
// or at least the settings page chrome loaded — never a hard failure.
|
|
154
|
-
await expect(moderationSection.or(settingsContainer).first()).toBeVisible({ timeout: 20_000 });
|
|
155
|
-
|
|
156
|
-
if (await moderationSection.first().isVisible().catch(() => false)) {
|
|
157
|
-
// The untrustedInput agent renders the non-editable Enforced badge.
|
|
158
|
-
await expect(page.locator('[data-ai-agent-moderation-enforced]').first()).toBeVisible();
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|