@mtkn/mega-agent 0.2.0 → 0.4.0
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/README.md +113 -3
- package/dist/artifact/artifact.repository.d.ts +2 -1
- package/dist/artifact/artifact.repository.d.ts.map +1 -1
- package/dist/artifact/artifact.repository.js +12 -2
- package/dist/artifact/artifact.repository.js.map +1 -1
- package/dist/artifact/artifact.service.d.ts +1 -0
- package/dist/artifact/artifact.service.d.ts.map +1 -1
- package/dist/artifact/artifact.service.js +11 -1
- package/dist/artifact/artifact.service.js.map +1 -1
- package/dist/artifact/index.d.ts +2 -2
- package/dist/artifact/index.d.ts.map +1 -1
- package/dist/artifact/index.js +2 -2
- package/dist/artifact/index.js.map +1 -1
- package/dist/chat/chat-memory.integration.d.ts +1 -0
- package/dist/chat/chat-memory.integration.d.ts.map +1 -1
- package/dist/chat/chat-memory.integration.js +9 -0
- package/dist/chat/chat-memory.integration.js.map +1 -1
- package/dist/chat/chat.repository.d.ts +2 -1
- package/dist/chat/chat.repository.d.ts.map +1 -1
- package/dist/chat/chat.repository.js +12 -2
- package/dist/chat/chat.repository.js.map +1 -1
- package/dist/chat/chat.service.d.ts +3 -1
- package/dist/chat/chat.service.d.ts.map +1 -1
- package/dist/chat/chat.service.js +34 -10
- package/dist/chat/chat.service.js.map +1 -1
- package/dist/chat/chat.types.d.ts +0 -2
- package/dist/chat/chat.types.d.ts.map +1 -1
- package/dist/chat/index.d.ts +3 -3
- package/dist/chat/index.d.ts.map +1 -1
- package/dist/chat/index.js +3 -3
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/pending-approval.store.d.ts +19 -0
- package/dist/chat/pending-approval.store.d.ts.map +1 -0
- package/dist/chat/pending-approval.store.js +41 -0
- package/dist/chat/pending-approval.store.js.map +1 -0
- package/dist/common/errors.d.ts +9 -0
- package/dist/common/errors.d.ts.map +1 -1
- package/dist/common/errors.js +18 -0
- package/dist/common/errors.js.map +1 -1
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.d.ts.map +1 -1
- package/dist/common/index.js +1 -0
- package/dist/common/index.js.map +1 -1
- package/dist/common/url-validator.d.ts +10 -0
- package/dist/common/url-validator.d.ts.map +1 -0
- package/dist/common/url-validator.js +27 -0
- package/dist/common/url-validator.js.map +1 -0
- package/dist/create-mega-agent.d.ts +15 -2
- package/dist/create-mega-agent.d.ts.map +1 -1
- package/dist/create-mega-agent.js +251 -97
- package/dist/create-mega-agent.js.map +1 -1
- package/dist/embedding/embedding.service.d.ts +1 -0
- package/dist/embedding/embedding.service.d.ts.map +1 -1
- package/dist/embedding/embedding.service.js +9 -0
- package/dist/embedding/embedding.service.js.map +1 -1
- package/dist/embedding/index.d.ts +1 -1
- package/dist/embedding/index.d.ts.map +1 -1
- package/dist/embedding/index.js +1 -1
- package/dist/embedding/index.js.map +1 -1
- package/dist/http/chat.controller.d.ts +1 -1
- package/dist/http/chat.controller.d.ts.map +1 -1
- package/dist/http/chat.controller.js +15 -3
- package/dist/http/chat.controller.js.map +1 -1
- package/dist/http/chat.routes.d.ts.map +1 -1
- package/dist/http/chat.routes.js +3 -3
- package/dist/http/chat.routes.js.map +1 -1
- package/dist/http/chat.validation.d.ts +14 -0
- package/dist/http/chat.validation.d.ts.map +1 -1
- package/dist/http/chat.validation.js +6 -0
- package/dist/http/chat.validation.js.map +1 -1
- package/dist/http/error-handler.middleware.d.ts +9 -0
- package/dist/http/error-handler.middleware.d.ts.map +1 -0
- package/dist/http/error-handler.middleware.js +21 -0
- package/dist/http/error-handler.middleware.js.map +1 -0
- package/dist/incident/incident.correlator.d.ts +1 -0
- package/dist/incident/incident.correlator.d.ts.map +1 -1
- package/dist/incident/incident.correlator.js +9 -0
- package/dist/incident/incident.correlator.js.map +1 -1
- package/dist/incident/incident.detector.d.ts +1 -0
- package/dist/incident/incident.detector.d.ts.map +1 -1
- package/dist/incident/incident.detector.js +9 -0
- package/dist/incident/incident.detector.js.map +1 -1
- package/dist/incident/incident.repository.d.ts +2 -1
- package/dist/incident/incident.repository.d.ts.map +1 -1
- package/dist/incident/incident.repository.js +17 -7
- package/dist/incident/incident.repository.js.map +1 -1
- package/dist/incident/incident.service.d.ts +1 -0
- package/dist/incident/incident.service.d.ts.map +1 -1
- package/dist/incident/incident.service.js +11 -1
- package/dist/incident/incident.service.js.map +1 -1
- package/dist/incident/index.d.ts +4 -4
- package/dist/incident/index.d.ts.map +1 -1
- package/dist/incident/index.js +4 -4
- package/dist/incident/index.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/llm/index.d.ts +2 -2
- package/dist/llm/index.d.ts.map +1 -1
- package/dist/llm/index.js +1 -1
- package/dist/llm/index.js.map +1 -1
- package/dist/llm/llm.service.d.ts +1 -0
- package/dist/llm/llm.service.d.ts.map +1 -1
- package/dist/llm/llm.service.js +9 -0
- package/dist/llm/llm.service.js.map +1 -1
- package/dist/llm/llm.types.d.ts +14 -8
- package/dist/llm/llm.types.d.ts.map +1 -1
- package/dist/llm/providers/anthropic.provider.d.ts.map +1 -1
- package/dist/llm/providers/anthropic.provider.js +64 -5
- package/dist/llm/providers/anthropic.provider.js.map +1 -1
- package/dist/llm/providers/openai.provider.d.ts +6 -0
- package/dist/llm/providers/openai.provider.d.ts.map +1 -1
- package/dist/llm/providers/openai.provider.js +202 -11
- package/dist/llm/providers/openai.provider.js.map +1 -1
- package/dist/mcp/index.d.ts +3 -3
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +3 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/mcp.service.d.ts +9 -0
- package/dist/mcp/mcp.service.d.ts.map +1 -1
- package/dist/mcp/mcp.service.js +31 -0
- package/dist/mcp/mcp.service.js.map +1 -1
- package/dist/mcp/source.repository.d.ts +2 -1
- package/dist/mcp/source.repository.d.ts.map +1 -1
- package/dist/mcp/source.repository.js +14 -4
- package/dist/mcp/source.repository.js.map +1 -1
- package/dist/mcp/source.service.d.ts +1 -0
- package/dist/mcp/source.service.d.ts.map +1 -1
- package/dist/mcp/source.service.js +11 -1
- package/dist/mcp/source.service.js.map +1 -1
- package/dist/memory/index.d.ts +2 -2
- package/dist/memory/index.d.ts.map +1 -1
- package/dist/memory/index.js +2 -2
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/memory.repository.d.ts +2 -1
- package/dist/memory/memory.repository.d.ts.map +1 -1
- package/dist/memory/memory.repository.js +19 -9
- package/dist/memory/memory.repository.js.map +1 -1
- package/dist/memory/memory.service.d.ts +2 -1
- package/dist/memory/memory.service.d.ts.map +1 -1
- package/dist/memory/memory.service.js +13 -2
- package/dist/memory/memory.service.js.map +1 -1
- package/dist/qdrant/qdrant.service.d.ts +1 -0
- package/dist/qdrant/qdrant.service.d.ts.map +1 -1
- package/dist/qdrant/qdrant.service.js +11 -1
- package/dist/qdrant/qdrant.service.js.map +1 -1
- package/dist/qdrant/qdrant.types.d.ts +5 -0
- package/dist/qdrant/qdrant.types.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/prisma-helpers.d.ts +18 -0
- package/dist/types/prisma-helpers.d.ts.map +1 -0
- package/dist/types/prisma-helpers.js +8 -0
- package/dist/types/prisma-helpers.js.map +1 -0
- package/dist/workflow/actions/base.action.d.ts +3 -1
- package/dist/workflow/actions/base.action.d.ts.map +1 -1
- package/dist/workflow/actions/base.action.js +2 -0
- package/dist/workflow/actions/base.action.js.map +1 -1
- package/dist/workflow/actions/notify.action.d.ts.map +1 -1
- package/dist/workflow/actions/notify.action.js +15 -5
- package/dist/workflow/actions/notify.action.js.map +1 -1
- package/dist/workflow/index.d.ts +2 -2
- package/dist/workflow/index.d.ts.map +1 -1
- package/dist/workflow/index.js +2 -2
- package/dist/workflow/index.js.map +1 -1
- package/dist/workflow/workflow.repository.d.ts +1 -0
- package/dist/workflow/workflow.repository.d.ts.map +1 -1
- package/dist/workflow/workflow.repository.js +13 -3
- package/dist/workflow/workflow.repository.js.map +1 -1
- package/dist/workflow/workflow.service.d.ts +1 -0
- package/dist/workflow/workflow.service.d.ts.map +1 -1
- package/dist/workflow/workflow.service.js +44 -20
- package/dist/workflow/workflow.service.js.map +1 -1
- package/dist/workflow/workflow.types.d.ts +17 -0
- package/dist/workflow/workflow.types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -250,6 +250,10 @@ export async function initChatModule(app: Express): Promise<void> {
|
|
|
250
250
|
});
|
|
251
251
|
}
|
|
252
252
|
|
|
253
|
+
if (providers.length === 0) {
|
|
254
|
+
throw new Error('At least one LLM API key must be set (ANTHROPIC_API_KEY or OPENAI_API_KEY)');
|
|
255
|
+
}
|
|
256
|
+
|
|
253
257
|
// Memory config (optional — requires OpenAI key + Qdrant)
|
|
254
258
|
const memory = process.env.OPENAI_API_KEY
|
|
255
259
|
? {
|
|
@@ -450,6 +454,29 @@ Content-Type: application/json
|
|
|
450
454
|
}
|
|
451
455
|
```
|
|
452
456
|
|
|
457
|
+
**Response (when a write tool requires approval):**
|
|
458
|
+
|
|
459
|
+
```json
|
|
460
|
+
{
|
|
461
|
+
"success": true,
|
|
462
|
+
"data": {
|
|
463
|
+
"userMessage": { "id": "...", "role": "USER", "content": "..." },
|
|
464
|
+
"assistantMessage": { "id": "...", "role": "ASSISTANT", "content": "I want to execute **create_order**..." },
|
|
465
|
+
"pendingApproval": {
|
|
466
|
+
"id": "a1b2c3d4-...",
|
|
467
|
+
"chatId": "chat-id",
|
|
468
|
+
"toolName": "create_order",
|
|
469
|
+
"toolDescription": "Create a new order. Requires approval.",
|
|
470
|
+
"arguments": { "productId": 42, "quantity": 10 }
|
|
471
|
+
},
|
|
472
|
+
"tokens": { "prompt": 1400, "completion": 180, "total": 1580 },
|
|
473
|
+
"latencyMs": 1820
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
Use the `pendingApproval.id` value as the `approvalId` when calling the approve or reject endpoints.
|
|
479
|
+
|
|
453
480
|
**SSE Streaming:**
|
|
454
481
|
|
|
455
482
|
When `stream: true`, the response uses Server-Sent Events:
|
|
@@ -463,6 +490,56 @@ data: {"type":"tool_call","name":"get_dashboard","args":{}}
|
|
|
463
490
|
data: {"type":"done","tokens":{"prompt":1200,"completion":350}}
|
|
464
491
|
```
|
|
465
492
|
|
|
493
|
+
### Approve Tool Call
|
|
494
|
+
|
|
495
|
+
```bash
|
|
496
|
+
POST /api/chat/:id/approve
|
|
497
|
+
Content-Type: application/json
|
|
498
|
+
|
|
499
|
+
{
|
|
500
|
+
"approvalId": "a1b2c3d4-..."
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
The server executes the tool using the original `toolName` and `arguments` stored server-side, then resumes the agent conversation. The client cannot modify which tool runs or its parameters.
|
|
505
|
+
|
|
506
|
+
**Response:**
|
|
507
|
+
|
|
508
|
+
```json
|
|
509
|
+
{
|
|
510
|
+
"success": true,
|
|
511
|
+
"data": {
|
|
512
|
+
"assistantMessage": { "id": "...", "role": "ASSISTANT", "content": "Order created successfully..." },
|
|
513
|
+
"tokens": { "prompt": 2100, "completion": 220, "total": 2320 },
|
|
514
|
+
"latencyMs": 3100
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
### Reject Tool Call
|
|
520
|
+
|
|
521
|
+
```bash
|
|
522
|
+
POST /api/chat/:id/reject
|
|
523
|
+
Content-Type: application/json
|
|
524
|
+
|
|
525
|
+
{
|
|
526
|
+
"approvalId": "a1b2c3d4-..."
|
|
527
|
+
}
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
Removes the pending approval from the server-side store. The approval expires automatically after 10 minutes if not acted upon.
|
|
531
|
+
|
|
532
|
+
**Response:**
|
|
533
|
+
|
|
534
|
+
```json
|
|
535
|
+
{
|
|
536
|
+
"success": true,
|
|
537
|
+
"data": {
|
|
538
|
+
"message": "Tool call rejected by user."
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
```
|
|
542
|
+
|
|
466
543
|
## Chat Modes
|
|
467
544
|
|
|
468
545
|
Each chat session has a mode that shapes the agent's behavior:
|
|
@@ -494,13 +571,15 @@ Agent processes with LLM
|
|
|
494
571
|
└── Uses write tool → pauses
|
|
495
572
|
│
|
|
496
573
|
▼
|
|
497
|
-
Returns
|
|
574
|
+
Returns pendingApproval { id, chatId, toolName, toolDescription, arguments }
|
|
498
575
|
│
|
|
499
|
-
├── POST /:id/approve → executes tool → agent continues
|
|
576
|
+
├── POST /:id/approve { approvalId } → executes tool → agent continues
|
|
500
577
|
│
|
|
501
|
-
└── POST /:id/reject
|
|
578
|
+
└── POST /:id/reject { approvalId } → approval cancelled → agent informed
|
|
502
579
|
```
|
|
503
580
|
|
|
581
|
+
Conversation state (LLM message history) is stored **server-side** in an in-memory store with a 10-minute TTL. The client only receives an opaque `approvalId` — never the raw conversation state.
|
|
582
|
+
|
|
504
583
|
## Prisma Models
|
|
505
584
|
|
|
506
585
|
The package uses 12 models and 16 enums:
|
|
@@ -555,6 +634,12 @@ interface MegaAgent {
|
|
|
555
634
|
|
|
556
635
|
/** Create Express router with all chat endpoints */
|
|
557
636
|
createChatRouter(authMiddleware: AuthMiddleware): Router;
|
|
637
|
+
|
|
638
|
+
/** Create Express error-handler middleware that formats MegaAgentError responses */
|
|
639
|
+
createErrorHandler(): ErrorRequestHandler;
|
|
640
|
+
|
|
641
|
+
/** Dispose all services and reset module state. Must be called before re-initializing. */
|
|
642
|
+
dispose(): void;
|
|
558
643
|
}
|
|
559
644
|
```
|
|
560
645
|
|
|
@@ -612,6 +697,31 @@ All service methods accept `UserId` for the `userId` parameter, so your app work
|
|
|
612
697
|
|
|
613
698
|
## Changelog
|
|
614
699
|
|
|
700
|
+
### v0.4.0
|
|
701
|
+
|
|
702
|
+
- **Breaking: Zero LLM providers now throws** — `createMegaAgent()` throws `ConfigValidationError` if `llm.providers` is empty. Previously it only logged a warning, causing all chat operations to fail at runtime.
|
|
703
|
+
- **Config validation** — `prisma`, `logger`, and `llm.providers` are validated at startup. Missing required fields throw `ConfigValidationError` with a descriptive message.
|
|
704
|
+
- **Race condition fix** — Concurrent `createMegaAgent()` calls are now blocked by an `_megaAgentInitializing` guard, preventing double initialization.
|
|
705
|
+
- **Init rollback** — If any service fails during initialization, all previously initialized services are disposed in reverse order. No more orphaned services.
|
|
706
|
+
- **Idempotent dispose** — `dispose()` immediately sets `_megaAgentInitialized = false` before teardown (flag-first pattern). Double-calling `dispose()` is a safe no-op.
|
|
707
|
+
- **Type safety: discriminated unions** — `LLMContentBlock` is now a discriminated union type instead of a single interface with optional fields. Non-null assertions (`!`) removed from both OpenAI and Anthropic providers.
|
|
708
|
+
- **New export:** `ConfigValidationError` error class.
|
|
709
|
+
- **New method:** `createErrorHandler()` — Express error-handler middleware that formats `MegaAgentError` responses and hides stack traces in production.
|
|
710
|
+
|
|
711
|
+
### v0.3.1
|
|
712
|
+
|
|
713
|
+
- **Init guard** — `createMegaAgent()` now throws `AlreadyInitializedError` if called twice without calling `dispose()` first. All 19 service modules have the same guard.
|
|
714
|
+
- **Dispose lifecycle** — `MegaAgent.dispose()` resets all module state in reverse initialization order, allowing safe re-initialization.
|
|
715
|
+
- **MCP instance guard** — `new McpService()` throws if an instance already exists. `disposeMcpService()` is async and disconnects all active servers.
|
|
716
|
+
|
|
717
|
+
### v0.3.0
|
|
718
|
+
|
|
719
|
+
- **Security: Server-side conversation state** — LLM message history is no longer sent to the client. Conversation state is stored in an in-memory store with a 10-minute TTL. Clients receive only an opaque `approvalId`.
|
|
720
|
+
- **Security: Ownership check on approve/reject** — `POST /:id/approve` and `POST /:id/reject` now verify that the authenticated user owns the chat. Unauthorized requests return 404.
|
|
721
|
+
- **Security: Body validation on approve/reject** — Both endpoints now validate the request body with Zod schemas. Invalid or missing `approvalId` returns 400.
|
|
722
|
+
- **Security: Tool substitution prevention** — The approve endpoint no longer accepts `toolName` or `arguments` from the client. Tool execution uses the original values stored server-side, preventing tool substitution attacks.
|
|
723
|
+
- **Breaking:** Approve/reject request body changed from `{ toolName, arguments, conversationState }` to `{ approvalId }`. The `conversationState` field is removed from `pendingApproval` responses.
|
|
724
|
+
|
|
615
725
|
### v0.2.0
|
|
616
726
|
|
|
617
727
|
- **ID type selection** — `prisma-sync` CLI now supports `--id-type` (cuid/uuid/autoincrement) and `--user-id-type` (int/string) with interactive prompts
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { AiArtifact } from '@prisma/client';
|
|
6
6
|
import type { CreateArtifactDto, UpdateArtifactDto, ArtifactListQuery, ArtifactServiceDependencies } from './artifact.types.js';
|
|
7
|
-
import type
|
|
7
|
+
import { type UserId } from '../types/index.js';
|
|
8
8
|
export declare function initArtifactRepository(deps: ArtifactServiceDependencies): void;
|
|
9
|
+
export declare function disposeArtifactRepository(): void;
|
|
9
10
|
export declare class ArtifactRepository {
|
|
10
11
|
private get prisma();
|
|
11
12
|
create(data: CreateArtifactDto): Promise<AiArtifact>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"artifact.repository.d.ts","sourceRoot":"","sources":["../../src/artifact/artifact.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAChI,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"artifact.repository.d.ts","sourceRoot":"","sources":["../../src/artifact/artifact.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAChI,OAAO,EAAE,KAAK,MAAM,EAAkB,MAAM,mBAAmB,CAAC;AAMhE,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,2BAA2B,GAAG,IAAI,CAI9E;AAED,wBAAgB,yBAAyB,IAAI,IAAI,CAGhD;AAED,qBAAa,kBAAkB;IAC7B,OAAO,KAAK,MAAM,GAGjB;IAEK,MAAM,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAgBpD,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAIhD,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAY5E,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAOjD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAahE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGxC"}
|
|
@@ -2,9 +2,19 @@
|
|
|
2
2
|
* Artifact Repository
|
|
3
3
|
* Data access layer for artifact operations
|
|
4
4
|
*/
|
|
5
|
+
import { toPrismaUserId } from '../types/index.js';
|
|
6
|
+
import { AlreadyInitializedError } from '../common/errors.js';
|
|
5
7
|
let _deps = null;
|
|
8
|
+
let _initialized = false;
|
|
6
9
|
export function initArtifactRepository(deps) {
|
|
10
|
+
if (_initialized)
|
|
11
|
+
throw new AlreadyInitializedError('ArtifactRepository');
|
|
7
12
|
_deps = deps;
|
|
13
|
+
_initialized = true;
|
|
14
|
+
}
|
|
15
|
+
export function disposeArtifactRepository() {
|
|
16
|
+
_deps = null;
|
|
17
|
+
_initialized = false;
|
|
8
18
|
}
|
|
9
19
|
export class ArtifactRepository {
|
|
10
20
|
get prisma() {
|
|
@@ -15,7 +25,7 @@ export class ArtifactRepository {
|
|
|
15
25
|
async create(data) {
|
|
16
26
|
return this.prisma.aiArtifact.create({
|
|
17
27
|
data: {
|
|
18
|
-
userId: data.userId,
|
|
28
|
+
userId: toPrismaUserId(data.userId),
|
|
19
29
|
type: data.type,
|
|
20
30
|
title: data.title,
|
|
21
31
|
description: data.description,
|
|
@@ -33,7 +43,7 @@ export class ArtifactRepository {
|
|
|
33
43
|
async findByUser(userId, query) {
|
|
34
44
|
return this.prisma.aiArtifact.findMany({
|
|
35
45
|
where: {
|
|
36
|
-
userId: userId,
|
|
46
|
+
userId: toPrismaUserId(userId),
|
|
37
47
|
...(query?.type && { type: query.type }),
|
|
38
48
|
},
|
|
39
49
|
orderBy: { createdAt: 'desc' },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"artifact.repository.js","sourceRoot":"","sources":["../../src/artifact/artifact.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"artifact.repository.js","sourceRoot":"","sources":["../../src/artifact/artifact.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAe,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,IAAI,KAAK,GAAuC,IAAI,CAAC;AACrD,IAAI,YAAY,GAAG,KAAK,CAAC;AAEzB,MAAM,UAAU,sBAAsB,CAAC,IAAiC;IACtE,IAAI,YAAY;QAAE,MAAM,IAAI,uBAAuB,CAAC,oBAAoB,CAAC,CAAC;IAC1E,KAAK,GAAG,IAAI,CAAC;IACb,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,yBAAyB;IACvC,KAAK,GAAG,IAAI,CAAC;IACb,YAAY,GAAG,KAAK,CAAC;AACvB,CAAC;AAED,MAAM,OA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"}
|
|
@@ -6,6 +6,7 @@ import type { AiArtifact } from '@prisma/client';
|
|
|
6
6
|
import type { CreateArtifactDto, UpdateArtifactDto, ArtifactListQuery, ArtifactServiceDependencies } from './artifact.types.js';
|
|
7
7
|
import type { UserId } from '../types/index.js';
|
|
8
8
|
export declare function initArtifactService(deps: ArtifactServiceDependencies): void;
|
|
9
|
+
export declare function disposeArtifactService(): void;
|
|
9
10
|
export declare function getArtifactServiceDeps(): ArtifactServiceDependencies | null;
|
|
10
11
|
export declare class ArtifactService {
|
|
11
12
|
private repository;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"artifact.service.d.ts","sourceRoot":"","sources":["../../src/artifact/artifact.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"artifact.service.d.ts","sourceRoot":"","sources":["../../src/artifact/artifact.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAMhD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,2BAA2B,GAAG,IAAI,CAK3E;AAED,wBAAgB,sBAAsB,IAAI,IAAI,CAI7C;AAED,wBAAgB,sBAAsB,IAAI,2BAA2B,GAAG,IAAI,CAE3E;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,UAAU,CAAqB;;IAQvC,OAAO,KAAK,MAAM,GAA4B;IAIxC,MAAM,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAMpD,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC;IAI/C,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAI5E,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;IAIjD,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC;IAIhE,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOjC,iBAAiB,CACrB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,OAAO,CAAC,iBAAiB,CAAC,GAC/B,OAAO,CAAC,UAAU,CAAC;IAahB,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC;CA6BzE"}
|
|
@@ -2,11 +2,21 @@
|
|
|
2
2
|
* Artifact Service
|
|
3
3
|
* Core service for managing chat-generated artifacts
|
|
4
4
|
*/
|
|
5
|
-
import { ArtifactRepository, initArtifactRepository } from './artifact.repository.js';
|
|
5
|
+
import { ArtifactRepository, initArtifactRepository, disposeArtifactRepository } from './artifact.repository.js';
|
|
6
|
+
import { AlreadyInitializedError } from '../common/errors.js';
|
|
6
7
|
let _deps = null;
|
|
8
|
+
let _initialized = false;
|
|
7
9
|
export function initArtifactService(deps) {
|
|
10
|
+
if (_initialized)
|
|
11
|
+
throw new AlreadyInitializedError('ArtifactService');
|
|
8
12
|
_deps = deps;
|
|
9
13
|
initArtifactRepository(deps);
|
|
14
|
+
_initialized = true;
|
|
15
|
+
}
|
|
16
|
+
export function disposeArtifactService() {
|
|
17
|
+
disposeArtifactRepository();
|
|
18
|
+
_deps = null;
|
|
19
|
+
_initialized = false;
|
|
10
20
|
}
|
|
11
21
|
export function getArtifactServiceDeps() {
|
|
12
22
|
return _deps;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"artifact.service.js","sourceRoot":"","sources":["../../src/artifact/artifact.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"artifact.service.js","sourceRoot":"","sources":["../../src/artifact/artifact.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAEjH,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,IAAI,KAAK,GAAuC,IAAI,CAAC;AACrD,IAAI,YAAY,GAAG,KAAK,CAAC;AAEzB,MAAM,UAAU,mBAAmB,CAAC,IAAiC;IACnE,IAAI,YAAY;QAAE,MAAM,IAAI,uBAAuB,CAAC,iBAAiB,CAAC,CAAC;IACvE,KAAK,GAAG,IAAI,CAAC;IACb,sBAAsB,CAAC,IAAI,CAAC,CAAC;IAC7B,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,yBAAyB,EAAE,CAAC;IAC5B,KAAK,GAAG,IAAI,CAAC;IACb,YAAY,GAAG,KAAK,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,OAAO,eAAe;IAClB,UAAU,CAAqB;IAEvC;QACE,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;QAClG,I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tifactListQuery, ArtifactServiceDependencies } from './artifact.types.js';
|
|
2
2
|
export { ArtifactType } from './artifact.types.js';
|
|
3
3
|
export type { AiArtifact } from './artifact.types.js';
|
|
4
|
-
export { ArtifactService, initArtifactService, getArtifactServiceDeps } from './artifact.service.js';
|
|
5
|
-
export { ArtifactRepository, initArtifactRepository } from './artifact.repository.js';
|
|
4
|
+
export { ArtifactService, initArtifactService, getArtifactServiceDeps, disposeArtifactService } from './artifact.service.js';
|
|
5
|
+
export { ArtifactRepository, initArtifactRepository, disposeArtifactRepository } from './artifact.repository.js';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/artifact/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAChI,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/artifact/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAChI,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC7H,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/artifact/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { ArtifactType } from './artifact.types.js';
|
|
2
|
-
export { ArtifactService, initArtifactService, getArtifactServiceDeps } from './artifact.service.js';
|
|
3
|
-
export { ArtifactRepository, initArtifactRepository } from './artifact.repository.js';
|
|
2
|
+
export { ArtifactService, initArtifactService, getArtifactServiceDeps, disposeArtifactService } from './artifact.service.js';
|
|
3
|
+
export { ArtifactRepository, initArtifactRepository, disposeArtifactRepository } from './artifact.repository.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/artifact/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/artifact/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC7H,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -10,6 +10,7 @@ import type { UserId } from '../types/index.js';
|
|
|
10
10
|
* Initialize the Chat Memory Integration with dependencies
|
|
11
11
|
*/
|
|
12
12
|
export declare function initChatMemoryIntegration(deps: ChatMemoryIntegrationDependencies): void;
|
|
13
|
+
export declare function disposeChatMemoryIntegration(): void;
|
|
13
14
|
/**
|
|
14
15
|
* Get the current integration dependencies
|
|
15
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-memory.integration.d.ts","sourceRoot":"","sources":["../../src/chat/chat-memory.integration.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EACV,iCAAiC,EAGjC,kBAAkB,EAClB,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"chat-memory.integration.d.ts","sourceRoot":"","sources":["../../src/chat/chat-memory.integration.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EACV,iCAAiC,EAGjC,kBAAkB,EAClB,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAQhD;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,iCAAiC,GAAG,IAAI,CAIvF;AAED,wBAAgB,4BAA4B,IAAI,IAAI,CAGnD;AAED;;GAEG;AACH,wBAAgB,4BAA4B,IAAI,iCAAiC,GAAG,IAAI,CAEvF;AAID;;;GAGG;AACH,qBAAa,qBAAqB;IAChC,yDAAyD;IACzD,MAAM,CAAC,QAAQ,CAAC,mBAAmB,QAAQ;IAE3C,8CAA8C;IAC9C,MAAM,CAAC,QAAQ,CAAC,oBAAoB,KAAK;;IAQzC,OAAO,KAAK,MAAM,GAEjB;IAED,OAAO,KAAK,aAAa,GAExB;IAED;;;OAGG;IACG,mBAAmB,CACvB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAqBhC;;;OAGG;IACH,kBAAkB,CAAC,QAAQ,EAAE,kBAAkB,EAAE,GAAG,MAAM;IAsB1D;;;OAGG;IACH,uBAAuB,CAAC,QAAQ,EAAE,aAAa,EAAE,EAAE,WAAW,GAAE,MAAU,GAAG,MAAM;IAQnF;;;OAGG;IACG,iBAAiB,CACrB,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;IAIhB;;;OAGG;IACH,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,sBAAsB;IAuBhE;;;OAGG;IACG,qBAAqB,CACzB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,IAAI,GAAE,UAAsB,GAC3B,OAAO,CAAC,IAAI,CAAC;CAmBjB"}
|
|
@@ -3,13 +3,22 @@
|
|
|
3
3
|
* Integrates the Memory System into the Chat Service
|
|
4
4
|
* Handles memory retrieval, context injection, and memory suggestion creation
|
|
5
5
|
*/
|
|
6
|
+
import { AlreadyInitializedError } from '../common/errors.js';
|
|
6
7
|
// ==================== MODULE STATE ====================
|
|
7
8
|
let _deps = null;
|
|
9
|
+
let _initialized = false;
|
|
8
10
|
/**
|
|
9
11
|
* Initialize the Chat Memory Integration with dependencies
|
|
10
12
|
*/
|
|
11
13
|
export function initChatMemoryIntegration(deps) {
|
|
14
|
+
if (_initialized)
|
|
15
|
+
throw new AlreadyInitializedError('ChatMemoryIntegration');
|
|
12
16
|
_deps = deps;
|
|
17
|
+
_initialized = true;
|
|
18
|
+
}
|
|
19
|
+
export function disposeChatMemoryIntegration() {
|
|
20
|
+
_deps = null;
|
|
21
|
+
_initialized = false;
|
|
13
22
|
}
|
|
14
23
|
/**
|
|
15
24
|
* Get the current integration dependencies
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-memory.integration.js","sourceRoot":"","sources":["../../src/chat/chat-memory.integration.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"chat-memory.integration.js","sourceRoot":"","sources":["../../src/chat/chat-memory.integ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YAClC,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,qCAAqC,EAAE;YACtD,MAAM;YACN,SAAS;YACT,IAAI;SACL,CAAC,CAAC;IACL,CAAC"}
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { AiChat, AiChatMessage } from '@prisma/client';
|
|
6
6
|
import type { CreateChatDto, UpdateChatDto, ChatServiceDependencies } from './chat.types.js';
|
|
7
|
-
import type
|
|
7
|
+
import { type UserId } from '../types/index.js';
|
|
8
8
|
/**
|
|
9
9
|
* Initialize the Chat repository with dependencies
|
|
10
10
|
*/
|
|
11
11
|
export declare function initChatRepository(deps: ChatServiceDependencies): void;
|
|
12
|
+
export declare function disposeChatRepository(): void;
|
|
12
13
|
export declare class ChatRepository {
|
|
13
14
|
private get prisma();
|
|
14
15
|
private get logger();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.repository.d.ts","sourceRoot":"","sources":["../../src/chat/chat.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC7F,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"chat.repository.d.ts","sourceRoot":"","sources":["../../src/chat/chat.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC7F,OAAO,EAAE,KAAK,MAAM,EAAkB,MAAM,mBAAmB,CAAC;AAMhE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,uBA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GACZ,OAAO,CAAC,aAAa,EAAE,CAAC;IAW3B;;OAEG;IACG,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAGvD"}
|
|
@@ -2,12 +2,22 @@
|
|
|
2
2
|
* Chat Repository
|
|
3
3
|
* Data access layer for chat operations
|
|
4
4
|
*/
|
|
5
|
+
import { toPrismaUserId } from '../types/index.js';
|
|
6
|
+
import { AlreadyInitializedError } from '../common/errors.js';
|
|
5
7
|
let _deps = null;
|
|
8
|
+
let _initialized = false;
|
|
6
9
|
/**
|
|
7
10
|
* Initialize the Chat repository with dependencies
|
|
8
11
|
*/
|
|
9
12
|
export function initChatRepository(deps) {
|
|
13
|
+
if (_initialized)
|
|
14
|
+
throw new AlreadyInitializedError('ChatRepository');
|
|
10
15
|
_deps = deps;
|
|
16
|
+
_initialized = true;
|
|
17
|
+
}
|
|
18
|
+
export function disposeChatRepository() {
|
|
19
|
+
_deps = null;
|
|
20
|
+
_initialized = false;
|
|
11
21
|
}
|
|
12
22
|
export class ChatRepository {
|
|
13
23
|
get prisma() {
|
|
@@ -26,7 +36,7 @@ export class ChatRepository {
|
|
|
26
36
|
async createChat(data) {
|
|
27
37
|
const chat = await this.prisma.aiChat.create({
|
|
28
38
|
data: {
|
|
29
|
-
userId: data.userId,
|
|
39
|
+
userId: toPrismaUserId(data.userId),
|
|
30
40
|
title: data.title,
|
|
31
41
|
mode: data.mode || 'EXPLORE',
|
|
32
42
|
model: data.model || process.env.ANTHROPIC_DEFAULT_MODEL || 'claude-opus-4-6',
|
|
@@ -62,7 +72,7 @@ export class ChatRepository {
|
|
|
62
72
|
*/
|
|
63
73
|
async findByUser(userId, limit) {
|
|
64
74
|
return this.prisma.aiChat.findMany({
|
|
65
|
-
where: { userId: userId },
|
|
75
|
+
where: { userId: toPrismaUserId(userId) },
|
|
66
76
|
orderBy: { updatedAt: 'desc' },
|
|
67
77
|
take: limit,
|
|
68
78
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.repository.js","sourceRoot":"","sources":["../../src/chat/chat.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"chat.repository.js","sourceRoot":"","sources":["../../src/chat/chat.repository.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAe,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,IAAI,KAAK,GAAmC,IAAI,CAAC;AACjD,IAAI,YAAY,GAAG,KAAK,CAAC;AAEzB;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAA6B;IAC9D,IAAI,YAAY;QAAE,MAAM,IAAI,uBAAuB,CAAC,gBAAgB,CAAC,CAAC;IACtE,KAAK,GAAG,IAAI,CAAC;IACb,YAAY,GAAG,IAAI,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,qBAAqB;IACnC,KAAK,GAAG,IAAI,CAAC;IACb,YAAY,GAAG,KAAK,CAAC;AACvB,CAAC;AAED,MAAM,OAAO,cAAc;IACzB,IAAY,MAAM;QAChB,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAChE,OAAO,KAAK,CAAC,MAAM,CAAC;IACtB,CAAC;IAED,IAAY,MAAM;QAChB,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAChE,OAAO,KAAK,CAAC,MAAM,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,UAAU,CAAC,IAAmB;QAClC,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC;YAC3C,IAAI,EAAE;gBACJ,MAAM,EAAE,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC;gBACnC,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,IAAI,EAAE,IAAI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AE,MAAM,EAAE,EAAE,CAAC;aAC7C;YACD,OAAO,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE;YAC9B,IAAI,EAAE,KAAK;SACZ,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,eAAe,CAAC,MAAc;QAClC,OAAO,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;IAChE,CAAC;CACF"}
|
|
@@ -5,6 +5,7 @@ import type { AiChat } from '@prisma/client';
|
|
|
5
5
|
import type { CreateChatDto, UpdateChatDto, SendMessageDto, ChatMessageResponse, ChatStreamChunk, ChatContext, ChatServiceDependencies } from './chat.types.js';
|
|
6
6
|
import type { UserId } from '../types/index.js';
|
|
7
7
|
export declare function initChatService(deps: ChatServiceDependencies): void;
|
|
8
|
+
export declare function disposeChatService(): void;
|
|
8
9
|
export declare function getChatServiceDeps(): ChatServiceDependencies | null;
|
|
9
10
|
export declare class ChatService {
|
|
10
11
|
private repository;
|
|
@@ -15,7 +16,8 @@ export declare class ChatService {
|
|
|
15
16
|
private get llmService();
|
|
16
17
|
createChat(data: CreateChatDto): Promise<AiChat>;
|
|
17
18
|
sendMessage(dto: SendMessageDto): Promise<ChatMessageResponse>;
|
|
18
|
-
approveToolCall(chatId: string,
|
|
19
|
+
approveToolCall(chatId: string, approvalId: string, userId: UserId): Promise<ChatMessageResponse>;
|
|
20
|
+
rejectToolCall(approvalId: string): void;
|
|
19
21
|
streamMessage(dto: SendMessageDto): AsyncGenerator<ChatStreamChunk>;
|
|
20
22
|
getChat(id: string): Promise<AiChat | null>;
|
|
21
23
|
getChatWithMessages(id: string): Promise<ChatContext | null>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.service.d.ts","sourceRoot":"","sources":["../../src/chat/chat.service.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EA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AG,IAAI;IAMjC,aAAa,CAAC,GAAG,EAAE,cAAc,GAAG,cAAc,CAAC,eAAe,CAAC;IAgFpE,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAC3C,mBAAmB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAI5D,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAC/D,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAC5D,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAErC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAYpD,OAAO,CAAC,oBAAoB;YAId,qBAAqB;CAOpC"}
|
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Chat Service — Agent Loop with Tool Calling
|
|
3
3
|
*/
|
|
4
|
-
import { ChatRepository, initChatRepository } from './chat.repository.js';
|
|
4
|
+
import { ChatRepository, initChatRepository, disposeChatRepository } from './chat.repository.js';
|
|
5
5
|
import { buildSystemPrompt } from './prompts/system.prompts.js';
|
|
6
6
|
import { ChatMemoryIntegration, getChatMemoryIntegrationDeps } from './chat-memory.integration.js';
|
|
7
7
|
import { MemoryDetector } from './memory-detector.js';
|
|
8
8
|
import { ResponseParser } from './response-parser.js';
|
|
9
9
|
import { getAllTools, executeTool, requiresApproval, toAnthropicTools } from '../tools/index.js';
|
|
10
|
+
import { pendingApprovalStore } from './pending-approval.store.js';
|
|
11
|
+
import { ApprovalNotFoundError, AlreadyInitializedError } from '../common/errors.js';
|
|
10
12
|
let _deps = null;
|
|
11
|
-
|
|
13
|
+
let _initialized = false;
|
|
14
|
+
export function initChatService(deps) {
|
|
15
|
+
if (_initialized)
|
|
16
|
+
throw new AlreadyInitializedError('ChatService');
|
|
17
|
+
_deps = deps;
|
|
18
|
+
initChatRepository(deps);
|
|
19
|
+
_initialized = true;
|
|
20
|
+
}
|
|
21
|
+
export function disposeChatService() {
|
|
22
|
+
disposeChatRepository();
|
|
23
|
+
_deps = null;
|
|
24
|
+
_initialized = false;
|
|
25
|
+
}
|
|
12
26
|
export function getChatServiceDeps() { return _deps; }
|
|
13
27
|
export class ChatService {
|
|
14
28
|
repository;
|
|
@@ -122,14 +136,17 @@ export class ChatService {
|
|
|
122
136
|
}
|
|
123
137
|
llmMessages.push({ role: 'user', content: resultBlocks });
|
|
124
138
|
const tool = getAllTools().find(t => t.name === writeToolCall.name);
|
|
139
|
+
const approvalId = pendingApprovalStore.set({
|
|
140
|
+
chatId: dto.chatId, userId: chat.userId, toolName: writeToolCall.name,
|
|
141
|
+
toolArgs: writeToolCall.input, llmMessages,
|
|
142
|
+
});
|
|
125
143
|
const approvalMsg = await this.repository.addMessage(dto.chatId, 'ASSISTANT', `I want to execute **${writeToolCall.name}**: \`\`\`json\n${JSON.stringify(writeToolCall.input, null, 2)}\n\`\`\`\nPlease confirm or reject.`);
|
|
126
144
|
return {
|
|
127
145
|
message: userMessage, assistantMessage: approvalMsg, usedMemories: usedMemoryIds,
|
|
128
146
|
tokens: totalTokens, latencyMs: Date.now() - startTime,
|
|
129
147
|
pendingApproval: {
|
|
130
|
-
id:
|
|
148
|
+
id: approvalId, chatId: dto.chatId, toolName: writeToolCall.name,
|
|
131
149
|
toolDescription: tool?.description || writeToolCall.name, arguments: writeToolCall.input,
|
|
132
|
-
conversationState: JSON.stringify(llmMessages),
|
|
133
150
|
},
|
|
134
151
|
};
|
|
135
152
|
}
|
|
@@ -153,14 +170,17 @@ export class ChatService {
|
|
|
153
170
|
return { message: userMessage, assistantMessage, usedMemories: usedMemoryIds, tokens: totalTokens, latencyMs: Date.now() - startTime, structured };
|
|
154
171
|
}
|
|
155
172
|
// ── Approve pending tool call ──────────────────────────────────
|
|
156
|
-
async approveToolCall(chatId,
|
|
173
|
+
async approveToolCall(chatId, approvalId, userId) {
|
|
174
|
+
const approval = pendingApprovalStore.get(approvalId);
|
|
175
|
+
if (approval.chatId !== chatId)
|
|
176
|
+
throw new ApprovalNotFoundError(approvalId);
|
|
157
177
|
const startTime = Date.now();
|
|
158
|
-
const result = await executeTool(toolName,
|
|
159
|
-
this.logger.info('Approved tool executed', { tool: toolName, success: result.success });
|
|
178
|
+
const result = await executeTool(approval.toolName, approval.toolArgs, userId);
|
|
179
|
+
this.logger.info('Approved tool executed', { tool: approval.toolName, success: result.success });
|
|
160
180
|
// Resume conversation with tool result
|
|
161
|
-
const llmMessages =
|
|
181
|
+
const llmMessages = [...approval.llmMessages];
|
|
162
182
|
const toolId = `tool-${Date.now()}`;
|
|
163
|
-
llmMessages.push({ role: 'assistant', content: [{ type: 'tool_use', id: toolId, name: toolName, input:
|
|
183
|
+
llmMessages.push({ role: 'assistant', content: [{ type: 'tool_use', id: toolId, name: approval.toolName, input: approval.toolArgs }] });
|
|
164
184
|
llmMessages.push({
|
|
165
185
|
role: 'user',
|
|
166
186
|
content: [{
|
|
@@ -170,13 +190,17 @@ export class ChatService {
|
|
|
170
190
|
is_error: !result.success,
|
|
171
191
|
}],
|
|
172
192
|
});
|
|
193
|
+
pendingApprovalStore.delete(approvalId);
|
|
173
194
|
const chat = await this.repository.findByIdWithMessages(chatId, 3);
|
|
174
195
|
const res = await this.llmService.complete({ messages: llmMessages, model: chat?.model, tools: toAnthropicTools() });
|
|
175
196
|
const assistantMessage = await this.repository.addMessage(chatId, 'ASSISTANT', res.content, { model: res.model, tokens: res.tokens.total, latencyMs: res.latencyMs });
|
|
176
|
-
const ctx = { memoriesUsed: [], toolsCalled: [{ name: toolName, durationMs: Date.now() - startTime }], modelUsed: res.model, tokens: res.tokens, latencyMs: Date.now() - startTime };
|
|
197
|
+
const ctx = { memoriesUsed: [], toolsCalled: [{ name: approval.toolName, durationMs: Date.now() - startTime }], modelUsed: res.model, tokens: res.tokens, latencyMs: Date.now() - startTime };
|
|
177
198
|
const structured = ResponseParser.parse(res.content, ctx);
|
|
178
199
|
return { message: assistantMessage, assistantMessage, usedMemories: [], tokens: res.tokens, latencyMs: Date.now() - startTime, structured };
|
|
179
200
|
}
|
|
201
|
+
rejectToolCall(approvalId) {
|
|
202
|
+
pendingApprovalStore.delete(approvalId);
|
|
203
|
+
}
|
|
180
204
|
// ── Stream (hybrid: non-streaming tool loop + streaming final response) ──
|
|
181
205
|
async *streamMessage(dto) {
|
|
182
206
|
const chat = await this.repository.findByIdWithMessages(dto.chatId, ChatService.MAX_CONTEXT_MESSAGES);
|