@open-mercato/ai-assistant 0.6.6-develop.6465.1.019f0fb26f → 0.6.6-develop.6471.1.9ab5bdd79a
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/dist/frontend/hooks/useCommandPalette.js +16 -14
- package/dist/frontend/hooks/useCommandPalette.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/actions/[id]/cancel/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/actions/[id]/cancel/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/actions/[id]/confirm/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/actions/[id]/confirm/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/actions/[id]/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/actions/[id]/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/loop-override/route.js +5 -3
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/loop-override/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/models/route.js +4 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/models/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.js +5 -3
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.js +4 -2
- package/dist/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/agents/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/agents/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/chat/route.js +6 -7
- package/dist/modules/ai_assistant/api/ai/chat/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/conversations/[conversationId]/route.js +5 -3
- package/dist/modules/ai_assistant/api/ai/conversations/[conversationId]/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/conversations/import/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/conversations/import/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/conversations/route.js +4 -2
- package/dist/modules/ai_assistant/api/ai/conversations/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/ai/run-object/route.js +3 -1
- package/dist/modules/ai_assistant/api/ai/run-object/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/chat/route.js +23 -14
- package/dist/modules/ai_assistant/api/chat/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/health/route.js +3 -1
- package/dist/modules/ai_assistant/api/health/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/mcp-key/route.js +3 -1
- package/dist/modules/ai_assistant/api/mcp-key/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/route/route.js +11 -9
- package/dist/modules/ai_assistant/api/route/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/session-key/route.js +3 -1
- package/dist/modules/ai_assistant/api/session-key/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/settings/allowlist/route.js +4 -2
- package/dist/modules/ai_assistant/api/settings/allowlist/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/settings/route.js +6 -4
- package/dist/modules/ai_assistant/api/settings/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/tools/execute/route.js +3 -1
- package/dist/modules/ai_assistant/api/tools/execute/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/tools/route.js +3 -1
- package/dist/modules/ai_assistant/api/tools/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/usage/daily/route.js +3 -1
- package/dist/modules/ai_assistant/api/usage/daily/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/usage/sessions/[sessionId]/route.js +3 -1
- package/dist/modules/ai_assistant/api/usage/sessions/[sessionId]/route.js.map +2 -2
- package/dist/modules/ai_assistant/api/usage/sessions/route.js +3 -1
- package/dist/modules/ai_assistant/api/usage/sessions/route.js.map +2 -2
- package/dist/modules/ai_assistant/cli.js +3 -0
- package/dist/modules/ai_assistant/cli.js.map +2 -2
- package/dist/modules/ai_assistant/lib/agent-policy.js +7 -5
- package/dist/modules/ai_assistant/lib/agent-policy.js.map +2 -2
- package/dist/modules/ai_assistant/lib/agent-registry.js +11 -13
- package/dist/modules/ai_assistant/lib/agent-registry.js.map +2 -2
- package/dist/modules/ai_assistant/lib/agent-runtime.js +21 -44
- package/dist/modules/ai_assistant/lib/agent-runtime.js.map +2 -2
- package/dist/modules/ai_assistant/lib/agent-tools.js +5 -10
- package/dist/modules/ai_assistant/lib/agent-tools.js.map +2 -2
- package/dist/modules/ai_assistant/lib/ai-overrides.js +5 -9
- package/dist/modules/ai_assistant/lib/ai-overrides.js.map +2 -2
- package/dist/modules/ai_assistant/lib/api-endpoint-index.js +22 -22
- package/dist/modules/ai_assistant/lib/api-endpoint-index.js.map +2 -2
- package/dist/modules/ai_assistant/lib/attachment-parts.js +10 -26
- package/dist/modules/ai_assistant/lib/attachment-parts.js.map +2 -2
- package/dist/modules/ai_assistant/lib/auth.js +3 -1
- package/dist/modules/ai_assistant/lib/auth.js.map +2 -2
- package/dist/modules/ai_assistant/lib/codemode-tools.js +15 -17
- package/dist/modules/ai_assistant/lib/codemode-tools.js.map +2 -2
- package/dist/modules/ai_assistant/lib/entity-index.js +7 -5
- package/dist/modules/ai_assistant/lib/entity-index.js.map +2 -2
- package/dist/modules/ai_assistant/lib/generated-registry-loader.js +3 -4
- package/dist/modules/ai_assistant/lib/generated-registry-loader.js.map +2 -2
- package/dist/modules/ai_assistant/lib/http-server.js +45 -48
- package/dist/modules/ai_assistant/lib/http-server.js.map +2 -2
- package/dist/modules/ai_assistant/lib/llm-bootstrap.js +5 -12
- package/dist/modules/ai_assistant/lib/llm-bootstrap.js.map +2 -2
- package/dist/modules/ai_assistant/lib/mcp-client.js +3 -1
- package/dist/modules/ai_assistant/lib/mcp-client.js.map +2 -2
- package/dist/modules/ai_assistant/lib/mcp-dev-key-resolution.js +3 -1
- package/dist/modules/ai_assistant/lib/mcp-dev-key-resolution.js.map +2 -2
- package/dist/modules/ai_assistant/lib/mcp-dev-server.js +3 -1
- package/dist/modules/ai_assistant/lib/mcp-dev-server.js.map +2 -2
- package/dist/modules/ai_assistant/lib/mcp-server.js +24 -19
- package/dist/modules/ai_assistant/lib/mcp-server.js.map +2 -2
- package/dist/modules/ai_assistant/lib/mcp-tool-adapter.js +3 -1
- package/dist/modules/ai_assistant/lib/mcp-tool-adapter.js.map +2 -2
- package/dist/modules/ai_assistant/lib/model-factory.js +4 -2
- package/dist/modules/ai_assistant/lib/model-factory.js.map +2 -2
- package/dist/modules/ai_assistant/lib/opencode-client.js +5 -3
- package/dist/modules/ai_assistant/lib/opencode-client.js.map +2 -2
- package/dist/modules/ai_assistant/lib/opencode-handlers.js +30 -12
- package/dist/modules/ai_assistant/lib/opencode-handlers.js.map +2 -2
- package/dist/modules/ai_assistant/lib/pending-action-cancel.js +3 -1
- package/dist/modules/ai_assistant/lib/pending-action-cancel.js.map +2 -2
- package/dist/modules/ai_assistant/lib/pending-action-executor.js +3 -1
- package/dist/modules/ai_assistant/lib/pending-action-executor.js.map +2 -2
- package/dist/modules/ai_assistant/lib/prepare-mutation.js +4 -6
- package/dist/modules/ai_assistant/lib/prepare-mutation.js.map +2 -2
- package/dist/modules/ai_assistant/lib/schema-utils.js +3 -1
- package/dist/modules/ai_assistant/lib/schema-utils.js.map +2 -2
- package/dist/modules/ai_assistant/lib/token-usage-recorder.js +4 -8
- package/dist/modules/ai_assistant/lib/token-usage-recorder.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-executor.js +3 -1
- package/dist/modules/ai_assistant/lib/tool-executor.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-loader.js +18 -25
- package/dist/modules/ai_assistant/lib/tool-loader.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-registry.js +3 -1
- package/dist/modules/ai_assistant/lib/tool-registry.js.map +2 -2
- package/dist/modules/ai_assistant/lib/tool-test-runner.js +3 -1
- package/dist/modules/ai_assistant/lib/tool-test-runner.js.map +2 -2
- package/dist/modules/ai_assistant/setup.js +4 -8
- package/dist/modules/ai_assistant/setup.js.map +2 -2
- package/dist/modules/ai_assistant/subscribers/conversation-shared-notify.js +3 -1
- package/dist/modules/ai_assistant/subscribers/conversation-shared-notify.js.map +2 -2
- package/dist/modules/ai_assistant/workers/ai-pending-action-cleanup.js +7 -19
- package/dist/modules/ai_assistant/workers/ai-pending-action-cleanup.js.map +2 -2
- package/dist/modules/ai_assistant/workers/ai-token-usage-prune.js +5 -11
- package/dist/modules/ai_assistant/workers/ai-token-usage-prune.js.map +2 -2
- package/package.json +6 -6
- package/src/frontend/hooks/useCommandPalette.ts +17 -16
- package/src/modules/ai_assistant/__tests__/events.test.ts +23 -5
- package/src/modules/ai_assistant/__tests__/integration/ws-c-attachment-bridge.test.ts +21 -3
- package/src/modules/ai_assistant/__tests__/integration/ws-c-policy-and-tools.test.ts +22 -3
- package/src/modules/ai_assistant/__tests__/token-usage-recorder.test.ts +26 -5
- package/src/modules/ai_assistant/api/ai/actions/[id]/cancel/route.ts +4 -1
- package/src/modules/ai_assistant/api/ai/actions/[id]/confirm/route.ts +4 -1
- package/src/modules/ai_assistant/api/ai/actions/[id]/route.ts +4 -1
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/loop-override/route.ts +6 -3
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/models/route.ts +5 -2
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/mutation-policy/route.ts +6 -3
- package/src/modules/ai_assistant/api/ai/agents/[agentId]/prompt-override/route.ts +5 -2
- package/src/modules/ai_assistant/api/ai/agents/route.ts +4 -1
- package/src/modules/ai_assistant/api/ai/chat/route.ts +7 -7
- package/src/modules/ai_assistant/api/ai/conversations/[conversationId]/route.ts +6 -3
- package/src/modules/ai_assistant/api/ai/conversations/import/route.ts +4 -1
- package/src/modules/ai_assistant/api/ai/conversations/route.ts +5 -2
- package/src/modules/ai_assistant/api/ai/run-object/route.ts +4 -1
- package/src/modules/ai_assistant/api/chat/route.ts +24 -15
- package/src/modules/ai_assistant/api/health/route.ts +4 -1
- package/src/modules/ai_assistant/api/mcp-key/route.ts +4 -1
- package/src/modules/ai_assistant/api/route/route.ts +12 -9
- package/src/modules/ai_assistant/api/session-key/route.ts +4 -1
- package/src/modules/ai_assistant/api/settings/allowlist/route.ts +5 -2
- package/src/modules/ai_assistant/api/settings/route.ts +7 -4
- package/src/modules/ai_assistant/api/tools/execute/route.ts +4 -1
- package/src/modules/ai_assistant/api/tools/route.ts +4 -1
- package/src/modules/ai_assistant/api/usage/daily/route.ts +4 -1
- package/src/modules/ai_assistant/api/usage/sessions/[sessionId]/route.ts +4 -1
- package/src/modules/ai_assistant/api/usage/sessions/route.ts +4 -1
- package/src/modules/ai_assistant/cli.ts +5 -0
- package/src/modules/ai_assistant/lib/__tests__/agent-policy.mutation-override.test.ts +20 -2
- package/src/modules/ai_assistant/lib/__tests__/agent-registry.test.ts +25 -5
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime-loop-phase0.test.ts +21 -2
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime-parity.test.ts +23 -3
- package/src/modules/ai_assistant/lib/__tests__/agent-runtime-phase4a.test.ts +20 -2
- package/src/modules/ai_assistant/lib/__tests__/agent-tools.test.ts +22 -3
- package/src/modules/ai_assistant/lib/__tests__/ai-overrides.test.ts +23 -3
- package/src/modules/ai_assistant/lib/__tests__/attachment-parts.test.ts +25 -7
- package/src/modules/ai_assistant/lib/__tests__/model-factory.test.ts +25 -5
- package/src/modules/ai_assistant/lib/__tests__/pending-action-cancel.test.ts +19 -1
- package/src/modules/ai_assistant/lib/__tests__/prepare-mutation.test.ts +22 -3
- package/src/modules/ai_assistant/lib/agent-policy.ts +8 -5
- package/src/modules/ai_assistant/lib/agent-registry.ts +12 -15
- package/src/modules/ai_assistant/lib/agent-runtime.ts +23 -46
- package/src/modules/ai_assistant/lib/agent-tools.ts +7 -11
- package/src/modules/ai_assistant/lib/ai-overrides.ts +6 -9
- package/src/modules/ai_assistant/lib/api-endpoint-index.ts +23 -22
- package/src/modules/ai_assistant/lib/attachment-parts.ts +14 -29
- package/src/modules/ai_assistant/lib/auth.ts +4 -1
- package/src/modules/ai_assistant/lib/codemode-tools.ts +17 -17
- package/src/modules/ai_assistant/lib/entity-index.ts +8 -5
- package/src/modules/ai_assistant/lib/generated-registry-loader.ts +4 -4
- package/src/modules/ai_assistant/lib/http-server.ts +46 -48
- package/src/modules/ai_assistant/lib/llm-bootstrap.ts +7 -13
- package/src/modules/ai_assistant/lib/mcp-client.ts +4 -1
- package/src/modules/ai_assistant/lib/mcp-dev-key-resolution.ts +4 -1
- package/src/modules/ai_assistant/lib/mcp-dev-server.ts +4 -1
- package/src/modules/ai_assistant/lib/mcp-server.ts +27 -19
- package/src/modules/ai_assistant/lib/mcp-tool-adapter.ts +4 -1
- package/src/modules/ai_assistant/lib/model-factory.ts +6 -3
- package/src/modules/ai_assistant/lib/opencode-client.ts +6 -3
- package/src/modules/ai_assistant/lib/opencode-handlers.ts +32 -12
- package/src/modules/ai_assistant/lib/pending-action-cancel.ts +4 -1
- package/src/modules/ai_assistant/lib/pending-action-executor.ts +4 -1
- package/src/modules/ai_assistant/lib/prepare-mutation.ts +5 -6
- package/src/modules/ai_assistant/lib/schema-utils.ts +4 -1
- package/src/modules/ai_assistant/lib/token-usage-recorder.ts +5 -8
- package/src/modules/ai_assistant/lib/tool-executor.ts +4 -1
- package/src/modules/ai_assistant/lib/tool-loader.ts +19 -25
- package/src/modules/ai_assistant/lib/tool-registry.ts +4 -1
- package/src/modules/ai_assistant/lib/tool-test-runner.ts +4 -1
- package/src/modules/ai_assistant/setup.ts +5 -8
- package/src/modules/ai_assistant/subscribers/conversation-shared-notify.ts +4 -1
- package/src/modules/ai_assistant/workers/ai-pending-action-cleanup.ts +8 -19
- package/src/modules/ai_assistant/workers/ai-token-usage-prune.ts +6 -11
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
@@ -15,6 +16,8 @@ import {
|
|
|
15
16
|
serializeAiChatMessage,
|
|
16
17
|
} from '../../../../lib/conversation-storage'
|
|
17
18
|
|
|
19
|
+
const logger = createLogger('ai_assistant')
|
|
20
|
+
|
|
18
21
|
const REQUIRED_FEATURE = 'ai_assistant.view'
|
|
19
22
|
const MANAGE_CONVERSATIONS_FEATURE = 'ai_assistant.conversations.manage'
|
|
20
23
|
|
|
@@ -225,7 +228,7 @@ export async function GET(req: NextRequest, context: RouteContext): Promise<Resp
|
|
|
225
228
|
nextCursor: transcript.nextCursor,
|
|
226
229
|
})
|
|
227
230
|
} catch (error) {
|
|
228
|
-
|
|
231
|
+
logger.error('AI Conversation GET — Failure', { err: error })
|
|
229
232
|
return jsonError(
|
|
230
233
|
500,
|
|
231
234
|
error instanceof Error ? error.message : 'Failed to load conversation.',
|
|
@@ -280,7 +283,7 @@ export async function PATCH(req: NextRequest, context: RouteContext): Promise<Re
|
|
|
280
283
|
if (error instanceof Error && error.name === 'AiChatConversationAccessError') {
|
|
281
284
|
return jsonError(404, 'Conversation not found.', 'conversation_not_found')
|
|
282
285
|
}
|
|
283
|
-
|
|
286
|
+
logger.error('AI Conversation PATCH — Failure', { err: error })
|
|
284
287
|
return jsonError(
|
|
285
288
|
500,
|
|
286
289
|
error instanceof Error ? error.message : 'Failed to update conversation.',
|
|
@@ -318,7 +321,7 @@ export async function DELETE(req: NextRequest, context: RouteContext): Promise<R
|
|
|
318
321
|
if (error instanceof Error && error.name === 'AiChatConversationAccessError') {
|
|
319
322
|
return jsonError(404, 'Conversation not found.', 'conversation_not_found')
|
|
320
323
|
}
|
|
321
|
-
|
|
324
|
+
logger.error('AI Conversation DELETE — Failure', { err: error })
|
|
322
325
|
return jsonError(
|
|
323
326
|
500,
|
|
324
327
|
error instanceof Error ? error.message : 'Failed to delete conversation.',
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
@@ -10,6 +11,8 @@ import {
|
|
|
10
11
|
serializeAiChatConversation,
|
|
11
12
|
} from '../../../../lib/conversation-storage'
|
|
12
13
|
|
|
14
|
+
const logger = createLogger('ai_assistant')
|
|
15
|
+
|
|
13
16
|
const REQUIRED_FEATURE = 'ai_assistant.view'
|
|
14
17
|
|
|
15
18
|
export const openApi: OpenApiRouteDoc = {
|
|
@@ -112,7 +115,7 @@ export async function POST(req: NextRequest): Promise<Response> {
|
|
|
112
115
|
if (error instanceof Error && error.name === 'AiChatConversationAccessError') {
|
|
113
116
|
return jsonError(404, error.message, 'conversation_not_found')
|
|
114
117
|
}
|
|
115
|
-
|
|
118
|
+
logger.error('AI Conversation Import — Failure', { err: error })
|
|
116
119
|
return jsonError(
|
|
117
120
|
500,
|
|
118
121
|
error instanceof Error ? error.message : 'Failed to import conversation.',
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
@@ -13,6 +14,8 @@ import {
|
|
|
13
14
|
serializeAiChatConversation,
|
|
14
15
|
} from '../../../lib/conversation-storage'
|
|
15
16
|
|
|
17
|
+
const logger = createLogger('ai_assistant')
|
|
18
|
+
|
|
16
19
|
const REQUIRED_FEATURE = 'ai_assistant.view'
|
|
17
20
|
const MANAGE_CONVERSATIONS_FEATURE = 'ai_assistant.conversations.manage'
|
|
18
21
|
|
|
@@ -171,7 +174,7 @@ export async function GET(req: NextRequest): Promise<Response> {
|
|
|
171
174
|
nextCursor: result.nextCursor,
|
|
172
175
|
})
|
|
173
176
|
} catch (error) {
|
|
174
|
-
|
|
177
|
+
logger.error('AI Conversations GET — Failure', { err: error })
|
|
175
178
|
return jsonError(
|
|
176
179
|
500,
|
|
177
180
|
error instanceof Error ? error.message : 'Failed to list conversations.',
|
|
@@ -234,7 +237,7 @@ export async function POST(req: NextRequest): Promise<Response> {
|
|
|
234
237
|
if (error instanceof Error && error.name === 'AiChatConversationAccessError') {
|
|
235
238
|
return jsonError(404, error.message, 'conversation_not_found')
|
|
236
239
|
}
|
|
237
|
-
|
|
240
|
+
logger.error('AI Conversations POST — Failure', { err: error })
|
|
238
241
|
return jsonError(
|
|
239
242
|
500,
|
|
240
243
|
error instanceof Error ? error.message : 'Failed to create conversation.',
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { UIMessage } from 'ai'
|
|
3
4
|
import { z } from 'zod'
|
|
@@ -10,6 +11,8 @@ import { checkAgentPolicy, type AgentPolicyDenyCode } from '../../../lib/agent-p
|
|
|
10
11
|
import { runAiAgentObject } from '../../../lib/agent-runtime'
|
|
11
12
|
import { AgentPolicyError } from '../../../lib/agent-tools'
|
|
12
13
|
|
|
14
|
+
const logger = createLogger('ai_assistant')
|
|
15
|
+
|
|
13
16
|
const MAX_MESSAGES = 100
|
|
14
17
|
|
|
15
18
|
const agentIdPattern = /^[a-z0-9_]+\.[a-z0-9_]+$/
|
|
@@ -194,7 +197,7 @@ export async function POST(req: NextRequest): Promise<Response> {
|
|
|
194
197
|
if (error instanceof AgentPolicyError) {
|
|
195
198
|
return jsonError(statusForDenyCode(error.code), error.message, error.code)
|
|
196
199
|
}
|
|
197
|
-
|
|
200
|
+
logger.error('AI Run Object — Dispatch failure', { err: error })
|
|
198
201
|
return jsonError(
|
|
199
202
|
500,
|
|
200
203
|
error instanceof Error ? error.message : 'Agent object dispatch failed.',
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
@@ -19,6 +20,8 @@ import { UserRole } from '@open-mercato/core/modules/auth/data/entities'
|
|
|
19
20
|
import { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
20
21
|
import { checkAiChatRateLimit } from '../../lib/rate-limit'
|
|
21
22
|
|
|
23
|
+
const logger = createLogger('ai_assistant')
|
|
24
|
+
|
|
22
25
|
/**
|
|
23
26
|
* System instructions injected at the start of new chat sessions.
|
|
24
27
|
* These ensure the AI follows the correct workflow for data operations.
|
|
@@ -206,7 +209,7 @@ export async function POST(req: NextRequest) {
|
|
|
206
209
|
await writer.write(encoder.encode(`data: ${jsonStr}\n\n`))
|
|
207
210
|
} catch (err) {
|
|
208
211
|
// Writer may have been closed by client disconnect
|
|
209
|
-
|
|
212
|
+
logger.warn('Failed to write SSE event', { eventType: event.type })
|
|
210
213
|
}
|
|
211
214
|
}
|
|
212
215
|
|
|
@@ -271,7 +274,7 @@ export async function POST(req: NextRequest) {
|
|
|
271
274
|
// security intent that the answerQuestion short-circuit MUST NOT leak
|
|
272
275
|
// whether the failure was authorization vs backend connectivity vs
|
|
273
276
|
// implementation error. The real reason is captured in server logs.
|
|
274
|
-
|
|
277
|
+
logger.error('AI Chat — Answer error', { err: error })
|
|
275
278
|
return NextResponse.json({ error: 'Session not available' }, { status: 403 })
|
|
276
279
|
}
|
|
277
280
|
}
|
|
@@ -299,9 +302,11 @@ export async function POST(req: NextRequest) {
|
|
|
299
302
|
if (rateLimited) return rateLimited
|
|
300
303
|
|
|
301
304
|
const chatStartTime = Date.now()
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
+
logger.info('Chat request', {
|
|
306
|
+
userId: auth.sub,
|
|
307
|
+
sessionId: sessionId ? sessionId.slice(0, 16) : 'new',
|
|
308
|
+
messageChars: lastUserMessage.length,
|
|
309
|
+
})
|
|
305
310
|
|
|
306
311
|
// For new sessions, create an ephemeral API key that inherits user permissions
|
|
307
312
|
// The API key secret is encrypted and stored; MCP server recovers it via session token
|
|
@@ -321,9 +326,9 @@ export async function POST(req: NextRequest) {
|
|
|
321
326
|
organizationId: auth.orgId,
|
|
322
327
|
ttlMinutes: 120,
|
|
323
328
|
})
|
|
324
|
-
|
|
329
|
+
logger.info('Created session token')
|
|
325
330
|
} catch (error) {
|
|
326
|
-
|
|
331
|
+
logger.error('AI Chat — Failed to create session key', { err: error })
|
|
327
332
|
// Continue without session key - tools will use static API key auth
|
|
328
333
|
}
|
|
329
334
|
}
|
|
@@ -354,7 +359,7 @@ export async function POST(req: NextRequest) {
|
|
|
354
359
|
try {
|
|
355
360
|
// Emit session-authorized event first (if we have a token)
|
|
356
361
|
if (sessionToken) {
|
|
357
|
-
|
|
362
|
+
logger.info('AI Chat — Emitting session-authorized event')
|
|
358
363
|
await writeSSE({
|
|
359
364
|
type: 'session-authorized',
|
|
360
365
|
sessionToken: sessionToken.slice(0, 12) + '...',
|
|
@@ -389,10 +394,7 @@ export async function POST(req: NextRequest) {
|
|
|
389
394
|
} catch (bindErr) {
|
|
390
395
|
// The response stream is already in-flight — surface the
|
|
391
396
|
// failure to logs without disturbing the SSE pipeline.
|
|
392
|
-
|
|
393
|
-
'[AI Chat] Failed to bind OpenCode session to api_key:',
|
|
394
|
-
bindErr
|
|
395
|
-
)
|
|
397
|
+
logger.error('AI Chat — Failed to bind OpenCode session to api_key', { err: bindErr })
|
|
396
398
|
}
|
|
397
399
|
}
|
|
398
400
|
}
|
|
@@ -401,14 +403,21 @@ export async function POST(req: NextRequest) {
|
|
|
401
403
|
}
|
|
402
404
|
)
|
|
403
405
|
} catch (error) {
|
|
404
|
-
|
|
406
|
+
logger.error('AI Chat — OpenCode error', { err: error })
|
|
405
407
|
await writeSSE({
|
|
406
408
|
type: 'error',
|
|
407
409
|
error: error instanceof Error ? error.message : 'OpenCode request failed',
|
|
408
410
|
})
|
|
409
411
|
} finally {
|
|
410
412
|
const durationMs = Date.now() - chatStartTime
|
|
411
|
-
|
|
413
|
+
logger.info('Chat complete', {
|
|
414
|
+
userId: auth.sub,
|
|
415
|
+
sessionId: (resultSessionId || sessionId || 'unknown').slice(0, 16),
|
|
416
|
+
durationMs,
|
|
417
|
+
toolCalls: toolCallCount,
|
|
418
|
+
inputTokens: lastTokens?.input || 0,
|
|
419
|
+
outputTokens: lastTokens?.output || 0,
|
|
420
|
+
})
|
|
412
421
|
await closeWriter()
|
|
413
422
|
}
|
|
414
423
|
})()
|
|
@@ -421,7 +430,7 @@ export async function POST(req: NextRequest) {
|
|
|
421
430
|
},
|
|
422
431
|
})
|
|
423
432
|
} catch (error) {
|
|
424
|
-
|
|
433
|
+
logger.error('AI Chat — Error', { err: error })
|
|
425
434
|
return NextResponse.json({ error: 'Chat request failed' }, { status: 500 })
|
|
426
435
|
}
|
|
427
436
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
4
5
|
import { handleOpenCodeHealth } from '../../lib/opencode-handlers'
|
|
5
6
|
|
|
7
|
+
const logger = createLogger('ai_assistant')
|
|
8
|
+
|
|
6
9
|
export const openApi: OpenApiRouteDoc = {
|
|
7
10
|
tag: 'AI Assistant',
|
|
8
11
|
summary: 'AI assistant health check',
|
|
@@ -30,7 +33,7 @@ export async function GET(req: NextRequest) {
|
|
|
30
33
|
const health = await handleOpenCodeHealth()
|
|
31
34
|
return NextResponse.json(health)
|
|
32
35
|
} catch (error) {
|
|
33
|
-
|
|
36
|
+
logger.error('AI Health — Error', { err: error })
|
|
34
37
|
return NextResponse.json(
|
|
35
38
|
{ error: 'Failed to check health', message: error instanceof Error ? error.message : 'Unknown error' },
|
|
36
39
|
{ status: 500 }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
@@ -8,6 +9,8 @@ import { UserRole } from '@open-mercato/core/modules/auth/data/entities'
|
|
|
8
9
|
import { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
9
10
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
10
11
|
|
|
12
|
+
const logger = createLogger('ai_assistant')
|
|
13
|
+
|
|
11
14
|
export const metadata = {
|
|
12
15
|
POST: { requireAuth: true, requireFeatures: ['api_keys.create'] },
|
|
13
16
|
}
|
|
@@ -84,7 +87,7 @@ export async function POST(req: NextRequest) {
|
|
|
84
87
|
roles: roleIds,
|
|
85
88
|
})
|
|
86
89
|
} catch (error) {
|
|
87
|
-
|
|
90
|
+
logger.error('MCP Key — Error', { err: error })
|
|
88
91
|
return NextResponse.json(
|
|
89
92
|
{ error: 'Failed to create MCP API key' },
|
|
90
93
|
{ status: 500 },
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { generateObject } from '../../lib/ai-sdk'
|
|
@@ -13,6 +14,8 @@ import {
|
|
|
13
14
|
} from '../../lib/chat-config'
|
|
14
15
|
import { createModelFactory, AiModelFactoryError } from '../../lib/model-factory'
|
|
15
16
|
|
|
17
|
+
const logger = createLogger('ai_assistant')
|
|
18
|
+
|
|
16
19
|
export const openApi: OpenApiRouteDoc = {
|
|
17
20
|
tag: 'AI Assistant',
|
|
18
21
|
summary: 'AI query routing',
|
|
@@ -86,8 +89,8 @@ export async function POST(req: NextRequest) {
|
|
|
86
89
|
availableTools: Array<{ name: string; description: string }>
|
|
87
90
|
}
|
|
88
91
|
|
|
89
|
-
|
|
90
|
-
|
|
92
|
+
logger.debug('Routing query received', { queryChars: query?.length ?? 0 })
|
|
93
|
+
logger.debug('Available tools count', { count: availableTools?.length })
|
|
91
94
|
|
|
92
95
|
if (!query || typeof query !== 'string') {
|
|
93
96
|
return NextResponse.json({ error: 'query is required' }, { status: 400 })
|
|
@@ -126,10 +129,10 @@ export async function POST(req: NextRequest) {
|
|
|
126
129
|
throw error
|
|
127
130
|
}
|
|
128
131
|
|
|
129
|
-
|
|
132
|
+
logger.debug('Using provider', { providerId: factoryResolution.providerId })
|
|
130
133
|
|
|
131
134
|
const modelWithProvider = `${factoryResolution.providerId}/${factoryResolution.modelId}`
|
|
132
|
-
|
|
135
|
+
logger.debug('Calling generateObject', { model: modelWithProvider })
|
|
133
136
|
|
|
134
137
|
const result = await generateObject({
|
|
135
138
|
model: factoryResolution.model as Parameters<typeof generateObject>[0]['model'],
|
|
@@ -148,11 +151,11 @@ Respond with:
|
|
|
148
151
|
- reasoning: brief explanation`,
|
|
149
152
|
})
|
|
150
153
|
|
|
151
|
-
|
|
154
|
+
logger.debug('Routing result', { resultKeys: Object.keys(result.object ?? {}).join(',') })
|
|
152
155
|
return NextResponse.json(result.object)
|
|
153
156
|
}
|
|
154
157
|
|
|
155
|
-
|
|
158
|
+
logger.debug('Using provider', { providerId: config.providerId })
|
|
156
159
|
|
|
157
160
|
// Verify the configured provider is still available
|
|
158
161
|
if (!isProviderConfigured(config.providerId)) {
|
|
@@ -169,7 +172,7 @@ Respond with:
|
|
|
169
172
|
.map((t) => `- ${t.name}: ${t.description}`)
|
|
170
173
|
.join('\n')
|
|
171
174
|
|
|
172
|
-
|
|
175
|
+
logger.debug('Calling generateObject', { model: modelWithProvider })
|
|
173
176
|
|
|
174
177
|
const result = await generateObject({
|
|
175
178
|
model,
|
|
@@ -188,10 +191,10 @@ Respond with:
|
|
|
188
191
|
- reasoning: brief explanation`,
|
|
189
192
|
})
|
|
190
193
|
|
|
191
|
-
|
|
194
|
+
logger.debug('Routing result', { resultKeys: Object.keys(result.object ?? {}).join(',') })
|
|
192
195
|
return NextResponse.json(result.object)
|
|
193
196
|
} catch (error) {
|
|
194
|
-
|
|
197
|
+
logger.error('AI Route — Error routing query', { err: error })
|
|
195
198
|
return NextResponse.json(
|
|
196
199
|
{ error: 'Routing request failed' },
|
|
197
200
|
{ status: 500 }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
@@ -11,6 +12,8 @@ import { UserRole } from '@open-mercato/core/modules/auth/data/entities'
|
|
|
11
12
|
import { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
12
13
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
13
14
|
|
|
15
|
+
const logger = createLogger('ai_assistant')
|
|
16
|
+
|
|
14
17
|
export const metadata = {
|
|
15
18
|
POST: { requireAuth: true, requireFeatures: ['ai_assistant.view'] },
|
|
16
19
|
}
|
|
@@ -77,7 +80,7 @@ export async function POST(req: NextRequest) {
|
|
|
77
80
|
expiresAt: expiresAt.toISOString(),
|
|
78
81
|
})
|
|
79
82
|
} catch (error) {
|
|
80
|
-
|
|
83
|
+
logger.error('Session Key — Error', { err: error })
|
|
81
84
|
return NextResponse.json(
|
|
82
85
|
{ error: 'Failed to create session key' },
|
|
83
86
|
{ status: 500 }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
@@ -14,6 +15,8 @@ import {
|
|
|
14
15
|
modelAllowlistEnvVarName,
|
|
15
16
|
} from '../../../lib/model-allowlist'
|
|
16
17
|
|
|
18
|
+
const logger = createLogger('ai_assistant')
|
|
19
|
+
|
|
17
20
|
const allowlistUpsertSchema = z.object({
|
|
18
21
|
allowedProviders: z.array(z.string().min(1).max(64)).nullable().optional(),
|
|
19
22
|
allowedModelsByProvider: z
|
|
@@ -177,7 +180,7 @@ export async function PUT(req: NextRequest) {
|
|
|
177
180
|
updatedAt: row.updatedAt,
|
|
178
181
|
})
|
|
179
182
|
} catch (error) {
|
|
180
|
-
|
|
183
|
+
logger.error('AI Settings Allowlist — PUT error', { err: error })
|
|
181
184
|
return NextResponse.json(
|
|
182
185
|
{ error: 'Failed to save tenant allowlist.' },
|
|
183
186
|
{ status: 500 },
|
|
@@ -212,7 +215,7 @@ export async function DELETE(req: NextRequest) {
|
|
|
212
215
|
})
|
|
213
216
|
return NextResponse.json({ cleared })
|
|
214
217
|
} catch (error) {
|
|
215
|
-
|
|
218
|
+
logger.error('AI Settings Allowlist — DELETE error', { err: error })
|
|
216
219
|
return NextResponse.json(
|
|
217
220
|
{ error: 'Failed to clear tenant allowlist.' },
|
|
218
221
|
{ status: 500 },
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
@@ -36,6 +37,8 @@ import {
|
|
|
36
37
|
type TenantAllowlistSnapshot,
|
|
37
38
|
} from '../../lib/model-allowlist'
|
|
38
39
|
|
|
40
|
+
const logger = createLogger('ai_assistant')
|
|
41
|
+
|
|
39
42
|
function modelCatalogWithAllowlistFallback(
|
|
40
43
|
models: ReadonlyArray<{ id: string; name: string; contextWindow?: number | null; tags?: readonly string[] }>,
|
|
41
44
|
allowlistModelIds: string[] | undefined,
|
|
@@ -345,7 +348,7 @@ export async function GET(req: NextRequest) {
|
|
|
345
348
|
}
|
|
346
349
|
} catch (overrideError) {
|
|
347
350
|
// Phase 4a fields are best-effort — log and continue returning the base response
|
|
348
|
-
|
|
351
|
+
logger.warn('AI Settings — Failed to compute Phase 4a override fields', { err: overrideError })
|
|
349
352
|
}
|
|
350
353
|
|
|
351
354
|
// Build availableProviders with Phase 4a defaultModels, then clip to the
|
|
@@ -458,7 +461,7 @@ export async function GET(req: NextRequest) {
|
|
|
458
461
|
agents: agentResolutions,
|
|
459
462
|
})
|
|
460
463
|
} catch (error) {
|
|
461
|
-
|
|
464
|
+
logger.error('AI Settings — GET error', { err: error })
|
|
462
465
|
return NextResponse.json({ error: 'Failed to fetch settings' }, { status: 500 })
|
|
463
466
|
}
|
|
464
467
|
}
|
|
@@ -716,7 +719,7 @@ export async function PUT(req: NextRequest) {
|
|
|
716
719
|
if (error instanceof AiAgentRuntimeOverrideValidationError) {
|
|
717
720
|
return NextResponse.json({ error: error.message, code: 'provider_unknown' }, { status: 400 })
|
|
718
721
|
}
|
|
719
|
-
|
|
722
|
+
logger.error('AI Settings — PUT error', { err: error })
|
|
720
723
|
return NextResponse.json({ error: 'Failed to save runtime override.' }, { status: 500 })
|
|
721
724
|
}
|
|
722
725
|
}
|
|
@@ -771,7 +774,7 @@ export async function DELETE(req: NextRequest) {
|
|
|
771
774
|
})
|
|
772
775
|
return NextResponse.json({ cleared })
|
|
773
776
|
} catch (error) {
|
|
774
|
-
|
|
777
|
+
logger.error('AI Settings — DELETE error', { err: error })
|
|
775
778
|
return NextResponse.json({ error: 'Failed to clear runtime override.' }, { status: 500 })
|
|
776
779
|
}
|
|
777
780
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'
|
|
@@ -7,6 +8,8 @@ import { loadAllModuleTools } from '../../../lib/tool-loader'
|
|
|
7
8
|
import type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'
|
|
8
9
|
import type { McpToolContext } from '../../../lib/types'
|
|
9
10
|
|
|
11
|
+
const logger = createLogger('ai_assistant')
|
|
12
|
+
|
|
10
13
|
export const openApi: OpenApiRouteDoc = {
|
|
11
14
|
tag: 'AI Assistant',
|
|
12
15
|
summary: 'Execute AI tool',
|
|
@@ -71,7 +74,7 @@ export async function POST(req: NextRequest) {
|
|
|
71
74
|
result: result.result,
|
|
72
75
|
})
|
|
73
76
|
} catch (error) {
|
|
74
|
-
|
|
77
|
+
logger.error('AI Tools — Error executing tool', { err: error })
|
|
75
78
|
return NextResponse.json(
|
|
76
79
|
{ success: false, error: 'Tool execution failed' },
|
|
77
80
|
{ status: 500 }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'
|
|
3
4
|
import { zodToJsonSchema } from 'zod-to-json-schema'
|
|
@@ -8,6 +9,8 @@ import { loadAllModuleTools } from '../../lib/tool-loader'
|
|
|
8
9
|
import { hasRequiredFeatures } from '../../lib/auth'
|
|
9
10
|
import type { RbacService } from '@open-mercato/core/modules/auth/services/rbacService'
|
|
10
11
|
|
|
12
|
+
const logger = createLogger('ai_assistant')
|
|
13
|
+
|
|
11
14
|
export const openApi: OpenApiRouteDoc = {
|
|
12
15
|
tag: 'AI Assistant',
|
|
13
16
|
summary: 'List AI tools',
|
|
@@ -60,7 +63,7 @@ export async function GET(req: NextRequest) {
|
|
|
60
63
|
|
|
61
64
|
return NextResponse.json({ tools })
|
|
62
65
|
} catch (error) {
|
|
63
|
-
|
|
66
|
+
logger.error('AI Tools — Error listing tools', { err: error })
|
|
64
67
|
return NextResponse.json({ error: 'Failed to list tools' }, { status: 500 })
|
|
65
68
|
}
|
|
66
69
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
@@ -9,6 +10,8 @@ import { AiTokenUsageRepository } from '../../../data/repositories/AiTokenUsageR
|
|
|
9
10
|
import { hasRequiredFeatures } from '../../../lib/auth'
|
|
10
11
|
import { toDateString, toIntegerString, toIsoString } from '../../../lib/usage-serialization'
|
|
11
12
|
|
|
13
|
+
const logger = createLogger('ai_assistant')
|
|
14
|
+
|
|
12
15
|
const REQUIRED_FEATURE = 'ai_assistant.settings.manage'
|
|
13
16
|
|
|
14
17
|
const querySchema = z.object({
|
|
@@ -120,7 +123,7 @@ export async function GET(req: NextRequest): Promise<Response> {
|
|
|
120
123
|
|
|
121
124
|
return NextResponse.json({ rows: serialized, total: serialized.length })
|
|
122
125
|
} catch (error) {
|
|
123
|
-
|
|
126
|
+
logger.error('AI Usage Daily — GET error', { err: error })
|
|
124
127
|
return jsonError(500, 'Failed to fetch daily usage data.', 'internal_error')
|
|
125
128
|
}
|
|
126
129
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
@@ -9,6 +10,8 @@ import { AiTokenUsageRepository } from '../../../../data/repositories/AiTokenUsa
|
|
|
9
10
|
import { hasRequiredFeatures } from '../../../../lib/auth'
|
|
10
11
|
import { toInteger, toIsoString } from '../../../../lib/usage-serialization'
|
|
11
12
|
|
|
13
|
+
const logger = createLogger('ai_assistant')
|
|
14
|
+
|
|
12
15
|
const REQUIRED_FEATURE = 'ai_assistant.settings.manage'
|
|
13
16
|
|
|
14
17
|
const sessionIdParamSchema = z.object({
|
|
@@ -124,7 +127,7 @@ export async function GET(req: NextRequest, context: RouteContext): Promise<Resp
|
|
|
124
127
|
|
|
125
128
|
return NextResponse.json({ events: serialized, total: serialized.length, sessionId })
|
|
126
129
|
} catch (error) {
|
|
127
|
-
|
|
130
|
+
logger.error('AI Usage Session Detail — GET error', { err: error })
|
|
128
131
|
return jsonError(500, 'Failed to fetch session event data.', 'internal_error')
|
|
129
132
|
}
|
|
130
133
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
1
2
|
import { NextResponse, type NextRequest } from 'next/server'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
@@ -8,6 +9,8 @@ import type { RbacService } from '@open-mercato/core/modules/auth/services/rbacS
|
|
|
8
9
|
import { hasRequiredFeatures } from '../../../lib/auth'
|
|
9
10
|
import { toInteger, toIsoString } from '../../../lib/usage-serialization'
|
|
10
11
|
|
|
12
|
+
const logger = createLogger('ai_assistant')
|
|
13
|
+
|
|
11
14
|
const REQUIRED_FEATURE = 'ai_assistant.settings.manage'
|
|
12
15
|
|
|
13
16
|
const MAX_PAGE_SIZE = 100
|
|
@@ -178,7 +181,7 @@ export async function GET(req: NextRequest): Promise<Response> {
|
|
|
178
181
|
|
|
179
182
|
return NextResponse.json({ sessions, total, limit, offset })
|
|
180
183
|
} catch (error) {
|
|
181
|
-
|
|
184
|
+
logger.error('AI Usage Sessions — GET error', { err: error })
|
|
182
185
|
return jsonError(500, 'Failed to fetch session usage data.', 'internal_error')
|
|
183
186
|
}
|
|
184
187
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ModuleCli } from '@open-mercato/shared/modules/registry'
|
|
2
2
|
import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
|
|
3
|
+
import { resetServerLoggerCache } from '@open-mercato/shared/lib/logger'
|
|
3
4
|
/**
|
|
4
5
|
* Ensure app bootstrap is called before creating DI container.
|
|
5
6
|
* Uses the shared generated-bootstrap loader so the command works both from
|
|
@@ -46,6 +47,10 @@ function parseArgs(rest: string[]): Record<string, string | boolean> {
|
|
|
46
47
|
const mcpServe: ModuleCli = {
|
|
47
48
|
command: 'mcp:serve',
|
|
48
49
|
async run(rest) {
|
|
50
|
+
// stdout carries the MCP JSON-RPC protocol (StdioServerTransport) — route all
|
|
51
|
+
// facade logs to stderr and rebuild any pino root created earlier in this process.
|
|
52
|
+
process.env.OM_LOG_DESTINATION = 'stderr'
|
|
53
|
+
resetServerLoggerCache()
|
|
49
54
|
const args = parseArgs(rest)
|
|
50
55
|
// Prefer the OPEN_MERCATO_API_KEY env var so the secret never has to be
|
|
51
56
|
// placed on the command line (argv is world-readable via ps / /proc).
|
|
@@ -9,6 +9,23 @@ import {
|
|
|
9
9
|
import { resetAgentRegistryForTests, seedAgentRegistryForTests } from '../agent-registry'
|
|
10
10
|
import { registerMcpTool, toolRegistry } from '../tool-registry'
|
|
11
11
|
|
|
12
|
+
jest.mock('@open-mercato/shared/lib/logger', () => {
|
|
13
|
+
const mocked = {
|
|
14
|
+
debug: jest.fn(),
|
|
15
|
+
info: jest.fn(),
|
|
16
|
+
warn: jest.fn(),
|
|
17
|
+
error: jest.fn(),
|
|
18
|
+
child: jest.fn(),
|
|
19
|
+
}
|
|
20
|
+
mocked.child.mockImplementation(() => mocked)
|
|
21
|
+
return { createLogger: jest.fn(() => mocked) }
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const testLogger = jest
|
|
25
|
+
.requireMock('@open-mercato/shared/lib/logger')
|
|
26
|
+
.createLogger('test') as Record<'debug' | 'info' | 'warn' | 'error', jest.Mock>
|
|
27
|
+
|
|
28
|
+
|
|
12
29
|
function makeAgent(
|
|
13
30
|
overrides: Partial<AiAgentDefinition> & Pick<AiAgentDefinition, 'id' | 'moduleId'>,
|
|
14
31
|
): AiAgentDefinition {
|
|
@@ -35,12 +52,13 @@ function makeTool(
|
|
|
35
52
|
const baseAuth = { userFeatures: ['*'] as string[], isSuperAdmin: false }
|
|
36
53
|
|
|
37
54
|
describe('agent-policy mutation override (Step 5.4)', () => {
|
|
38
|
-
let warnSpy: jest.
|
|
55
|
+
let warnSpy: jest.Mock
|
|
39
56
|
|
|
40
57
|
beforeEach(() => {
|
|
41
58
|
resetAgentRegistryForTests()
|
|
42
59
|
toolRegistry.clear()
|
|
43
|
-
warnSpy =
|
|
60
|
+
warnSpy = testLogger.warn
|
|
61
|
+
warnSpy.mockClear()
|
|
44
62
|
})
|
|
45
63
|
|
|
46
64
|
afterEach(() => {
|