Compare commits
48 Commits
v0.1.0
...
360d7fe96d
| Author | SHA1 | Date | |
|---|---|---|---|
| 360d7fe96d | |||
| 08da6b76d1 | |||
| 5d4efb467c | |||
| 6c6bcbdb7f | |||
| cfdd2b679c | |||
| 34d4dbbabd | |||
| 78d591b697 | |||
| e95c70d329 | |||
| d8ac088f3a | |||
| 0d7f3c6d14 | |||
| eddcca7533 | |||
| ad06e00f99 | |||
| 5b089392fd | |||
| 02ff3b3256 | |||
| 1d14ddcfe7 | |||
| 05a805eeca | |||
| ebf99d9ff7 | |||
| cf51fd6749 | |||
| bb22857fde | |||
| 5261048d67 | |||
| 36095ad80f | |||
| d06866f501 | |||
| 02e40f6c3c | |||
| de64695ac5 | |||
| dd108b9ab4 | |||
| f3e90df2a0 | |||
| 721e6bbc52 | |||
| 27848bf42e | |||
| 061edcaa78 | |||
| cbb729f377 | |||
| cfb491e127 | |||
| 20808b9b84 | |||
| fd61a36b01 | |||
| c0a7bae977 | |||
| 68e056ac91 | |||
| 77ba13b41b | |||
| 307bb427d6 | |||
| b89503fa8c | |||
| 254da35300 | |||
| 99926cdba2 | |||
| 25f880416a | |||
| 1138148543 | |||
| 4b70b603b3 | |||
| 2e7711fe65 | |||
| 417a57fa00 | |||
| 714fee52b9 | |||
| 133668f5b2 | |||
| 3b81bc9f3d |
29
.env.example
29
.env.example
@@ -62,9 +62,15 @@ OTEL_SERVICE_NAME=mosaic-gateway
|
|||||||
# Comma-separated list of Ollama model IDs to register (default: llama3.2,codellama,mistral)
|
# Comma-separated list of Ollama model IDs to register (default: llama3.2,codellama,mistral)
|
||||||
# OLLAMA_MODELS=llama3.2,codellama,mistral
|
# OLLAMA_MODELS=llama3.2,codellama,mistral
|
||||||
|
|
||||||
# OpenAI — required for embedding and log-summarization features
|
# Anthropic (claude-sonnet-4-6, claude-opus-4-6, claude-haiku-4-5)
|
||||||
|
# ANTHROPIC_API_KEY=sk-ant-...
|
||||||
|
|
||||||
|
# OpenAI (gpt-4o, gpt-4o-mini, o3-mini)
|
||||||
# OPENAI_API_KEY=sk-...
|
# OPENAI_API_KEY=sk-...
|
||||||
|
|
||||||
|
# Z.ai / GLM (glm-4.5, glm-4.5-air, glm-4.5-flash)
|
||||||
|
# ZAI_API_KEY=...
|
||||||
|
|
||||||
# Custom providers — JSON array of provider configs
|
# Custom providers — JSON array of provider configs
|
||||||
# Format: [{"id":"<id>","baseUrl":"<url>","apiKey":"<key>","models":[{"id":"<model-id>","name":"<label>"}]}]
|
# Format: [{"id":"<id>","baseUrl":"<url>","apiKey":"<key>","models":[{"id":"<model-id>","name":"<label>"}]}]
|
||||||
# MOSAIC_CUSTOM_PROVIDERS=
|
# MOSAIC_CUSTOM_PROVIDERS=
|
||||||
@@ -123,7 +129,26 @@ OTEL_SERVICE_NAME=mosaic-gateway
|
|||||||
# TELEGRAM_GATEWAY_URL=http://localhost:4000
|
# TELEGRAM_GATEWAY_URL=http://localhost:4000
|
||||||
|
|
||||||
|
|
||||||
# ─── Authentik SSO (optional — set AUTHENTIK_CLIENT_ID to enable) ────────────
|
# ─── SSO Providers (add credentials to enable) ───────────────────────────────
|
||||||
|
|
||||||
|
# --- Authentik (optional — set AUTHENTIK_CLIENT_ID to enable) ---
|
||||||
# AUTHENTIK_ISSUER=https://auth.example.com/application/o/mosaic/
|
# AUTHENTIK_ISSUER=https://auth.example.com/application/o/mosaic/
|
||||||
# AUTHENTIK_CLIENT_ID=
|
# AUTHENTIK_CLIENT_ID=
|
||||||
# AUTHENTIK_CLIENT_SECRET=
|
# AUTHENTIK_CLIENT_SECRET=
|
||||||
|
|
||||||
|
# --- WorkOS (optional — set WORKOS_CLIENT_ID to enable) ---
|
||||||
|
# WORKOS_ISSUER=https://your-company.authkit.app
|
||||||
|
# WORKOS_CLIENT_ID=client_...
|
||||||
|
# WORKOS_CLIENT_SECRET=sk_live_...
|
||||||
|
|
||||||
|
# --- Keycloak (optional — set KEYCLOAK_CLIENT_ID to enable) ---
|
||||||
|
# KEYCLOAK_ISSUER=https://auth.example.com/realms/master
|
||||||
|
# Legacy alternative if you prefer to compose the issuer from separate vars:
|
||||||
|
# KEYCLOAK_URL=https://auth.example.com
|
||||||
|
# KEYCLOAK_REALM=master
|
||||||
|
# KEYCLOAK_CLIENT_ID=mosaic
|
||||||
|
# KEYCLOAK_CLIENT_SECRET=
|
||||||
|
|
||||||
|
# Feature flags — set to true alongside provider credentials to show SSO buttons in the UI
|
||||||
|
# NEXT_PUBLIC_WORKOS_ENABLED=true
|
||||||
|
# NEXT_PUBLIC_KEYCLOAK_ENABLED=true
|
||||||
|
|||||||
25
AGENTS.md
25
AGENTS.md
@@ -53,3 +53,28 @@ pnpm typecheck && pnpm lint && pnpm format:check # Quality gates
|
|||||||
- ESM everywhere (`"type": "module"`, `.js` extensions in imports)
|
- ESM everywhere (`"type": "module"`, `.js` extensions in imports)
|
||||||
- NodeNext module resolution in all tsconfigs
|
- NodeNext module resolution in all tsconfigs
|
||||||
- Scratchpads are mandatory for non-trivial tasks
|
- Scratchpads are mandatory for non-trivial tasks
|
||||||
|
|
||||||
|
## docs/TASKS.md — Schema (CANONICAL)
|
||||||
|
|
||||||
|
The `agent` column specifies the required model for each task. **This is set at task creation by the orchestrator and must not be changed by workers.**
|
||||||
|
|
||||||
|
| Value | When to use | Budget |
|
||||||
|
| -------- | ----------------------------------------------------------- | -------------------------- |
|
||||||
|
| `codex` | All coding tasks (default for implementation) | OpenAI credits — preferred |
|
||||||
|
| `glm-5` | Cost-sensitive coding where Codex is unavailable | Z.ai credits |
|
||||||
|
| `haiku` | Review gates, verify tasks, status checks, docs-only | Cheapest Claude tier |
|
||||||
|
| `sonnet` | Complex planning, multi-file reasoning, architecture review | Claude quota |
|
||||||
|
| `opus` | Major cross-cutting architecture decisions ONLY | Most expensive — minimize |
|
||||||
|
| `—` | No preference / auto-select cheapest capable | Pipeline decides |
|
||||||
|
|
||||||
|
Pipeline crons read this column and spawn accordingly. Workers never modify `docs/TASKS.md` — only the orchestrator writes it.
|
||||||
|
|
||||||
|
**Full schema:**
|
||||||
|
|
||||||
|
```
|
||||||
|
| id | status | description | issue | agent | repo | branch | depends_on | estimate | notes |
|
||||||
|
```
|
||||||
|
|
||||||
|
- `status`: `not-started` | `in-progress` | `done` | `failed` | `blocked` | `needs-qa`
|
||||||
|
- `agent`: model value from table above (set before spawning)
|
||||||
|
- `estimate`: token budget e.g. `8K`, `25K`
|
||||||
|
|||||||
@@ -12,18 +12,19 @@
|
|||||||
"test": "vitest run --passWithNoTests"
|
"test": "vitest run --passWithNoTests"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@anthropic-ai/sdk": "^0.80.0",
|
||||||
"@fastify/helmet": "^13.0.2",
|
"@fastify/helmet": "^13.0.2",
|
||||||
"@mariozechner/pi-ai": "~0.57.1",
|
"@mariozechner/pi-ai": "~0.57.1",
|
||||||
"@mariozechner/pi-coding-agent": "~0.57.1",
|
"@mariozechner/pi-coding-agent": "~0.57.1",
|
||||||
"@modelcontextprotocol/sdk": "^1.27.1",
|
"@modelcontextprotocol/sdk": "^1.27.1",
|
||||||
"@mosaic/auth": "workspace:^",
|
"@mosaic/auth": "workspace:^",
|
||||||
"@mosaic/queue": "workspace:^",
|
|
||||||
"@mosaic/brain": "workspace:^",
|
"@mosaic/brain": "workspace:^",
|
||||||
"@mosaic/coord": "workspace:^",
|
"@mosaic/coord": "workspace:^",
|
||||||
"@mosaic/db": "workspace:^",
|
"@mosaic/db": "workspace:^",
|
||||||
"@mosaic/discord-plugin": "workspace:^",
|
"@mosaic/discord-plugin": "workspace:^",
|
||||||
"@mosaic/log": "workspace:^",
|
"@mosaic/log": "workspace:^",
|
||||||
"@mosaic/memory": "workspace:^",
|
"@mosaic/memory": "workspace:^",
|
||||||
|
"@mosaic/queue": "workspace:^",
|
||||||
"@mosaic/telegram-plugin": "workspace:^",
|
"@mosaic/telegram-plugin": "workspace:^",
|
||||||
"@mosaic/types": "workspace:^",
|
"@mosaic/types": "workspace:^",
|
||||||
"@nestjs/common": "^11.0.0",
|
"@nestjs/common": "^11.0.0",
|
||||||
@@ -46,6 +47,7 @@
|
|||||||
"dotenv": "^17.3.1",
|
"dotenv": "^17.3.1",
|
||||||
"fastify": "^5.0.0",
|
"fastify": "^5.0.0",
|
||||||
"node-cron": "^4.2.1",
|
"node-cron": "^4.2.1",
|
||||||
|
"openai": "^6.32.0",
|
||||||
"reflect-metadata": "^0.2.0",
|
"reflect-metadata": "^0.2.0",
|
||||||
"rxjs": "^7.8.0",
|
"rxjs": "^7.8.0",
|
||||||
"socket.io": "^4.8.0",
|
"socket.io": "^4.8.0",
|
||||||
|
|||||||
605
apps/gateway/src/__tests__/conversation-persistence.test.ts
Normal file
605
apps/gateway/src/__tests__/conversation-persistence.test.ts
Normal file
@@ -0,0 +1,605 @@
|
|||||||
|
/**
|
||||||
|
* Integration tests for conversation persistence and context resume (M1-008).
|
||||||
|
*
|
||||||
|
* Verifies the full flow end-to-end using in-memory mocks:
|
||||||
|
* 1. User messages are persisted when sent via ChatGateway.
|
||||||
|
* 2. Assistant responses are persisted with metadata on agent:end.
|
||||||
|
* 3. Conversation history is loaded and injected into context on session resume.
|
||||||
|
* 4. The search endpoint returns matching messages.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
||||||
|
import type { ConversationHistoryMessage } from '../agent/agent.service.js';
|
||||||
|
import { ConversationsController } from '../conversations/conversations.controller.js';
|
||||||
|
import type { Message } from '@mosaic/brain';
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Shared test data
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const USER_ID = 'user-test-001';
|
||||||
|
const CONV_ID = 'conv-test-001';
|
||||||
|
|
||||||
|
function makeConversation(overrides?: Record<string, unknown>) {
|
||||||
|
return {
|
||||||
|
id: CONV_ID,
|
||||||
|
userId: USER_ID,
|
||||||
|
title: null,
|
||||||
|
projectId: null,
|
||||||
|
archived: false,
|
||||||
|
createdAt: new Date('2026-01-01T00:00:00Z'),
|
||||||
|
updatedAt: new Date('2026-01-01T00:00:00Z'),
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeMessage(
|
||||||
|
role: 'user' | 'assistant' | 'system',
|
||||||
|
content: string,
|
||||||
|
overrides?: Record<string, unknown>,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
id: `msg-${role}-${Math.random().toString(36).slice(2)}`,
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
role,
|
||||||
|
content,
|
||||||
|
metadata: null,
|
||||||
|
createdAt: new Date('2026-01-01T00:01:00Z'),
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helper: build a mock ConversationsRepo
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function createMockBrain(options?: {
|
||||||
|
conversation?: ReturnType<typeof makeConversation> | undefined;
|
||||||
|
messages?: ReturnType<typeof makeMessage>[];
|
||||||
|
searchResults?: Array<{
|
||||||
|
messageId: string;
|
||||||
|
conversationId: string;
|
||||||
|
conversationTitle: string | null;
|
||||||
|
role: 'user' | 'assistant' | 'system';
|
||||||
|
content: string;
|
||||||
|
createdAt: Date;
|
||||||
|
}>;
|
||||||
|
}) {
|
||||||
|
const conversation = options?.conversation;
|
||||||
|
const messages = options?.messages ?? [];
|
||||||
|
const searchResults = options?.searchResults ?? [];
|
||||||
|
|
||||||
|
return {
|
||||||
|
conversations: {
|
||||||
|
findAll: vi.fn().mockResolvedValue(conversation ? [conversation] : []),
|
||||||
|
findById: vi.fn().mockResolvedValue(conversation),
|
||||||
|
create: vi.fn().mockResolvedValue(conversation ?? makeConversation()),
|
||||||
|
update: vi.fn().mockResolvedValue(conversation),
|
||||||
|
remove: vi.fn().mockResolvedValue(true),
|
||||||
|
findMessages: vi.fn().mockResolvedValue(messages),
|
||||||
|
addMessage: vi.fn().mockImplementation((data: unknown) => {
|
||||||
|
const d = data as {
|
||||||
|
conversationId: string;
|
||||||
|
role: 'user' | 'assistant' | 'system';
|
||||||
|
content: string;
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
|
};
|
||||||
|
return Promise.resolve(makeMessage(d.role, d.content, { metadata: d.metadata ?? null }));
|
||||||
|
}),
|
||||||
|
searchMessages: vi.fn().mockResolvedValue(searchResults),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 1. ConversationsRepo: addMessage persists user message
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('ConversationsRepo.addMessage — user message persistence', () => {
|
||||||
|
it('persists a user message and returns the saved record', async () => {
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation() });
|
||||||
|
|
||||||
|
const result = await brain.conversations.addMessage(
|
||||||
|
{
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
role: 'user',
|
||||||
|
content: 'Hello, agent!',
|
||||||
|
metadata: { timestamp: '2026-01-01T00:01:00.000Z' },
|
||||||
|
},
|
||||||
|
USER_ID,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(brain.conversations.addMessage).toHaveBeenCalledOnce();
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(result!.role).toBe('user');
|
||||||
|
expect(result!.content).toBe('Hello, agent!');
|
||||||
|
expect(result!.conversationId).toBe(CONV_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns undefined when conversation does not belong to the user', async () => {
|
||||||
|
// Simulate the repo enforcement: ownership mismatch returns undefined
|
||||||
|
const brain = createMockBrain({ conversation: undefined });
|
||||||
|
brain.conversations.addMessage = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
|
const result = await brain.conversations.addMessage(
|
||||||
|
{ conversationId: CONV_ID, role: 'user', content: 'Hello' },
|
||||||
|
'other-user',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 2. ConversationsRepo.addMessage — assistant response with metadata
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('ConversationsRepo.addMessage — assistant response metadata', () => {
|
||||||
|
it('persists assistant message with model, provider, tokens and toolCalls metadata', async () => {
|
||||||
|
const assistantMetadata = {
|
||||||
|
timestamp: '2026-01-01T00:02:00.000Z',
|
||||||
|
model: 'claude-3-5-sonnet-20241022',
|
||||||
|
provider: 'anthropic',
|
||||||
|
toolCalls: [
|
||||||
|
{
|
||||||
|
toolCallId: 'tc-001',
|
||||||
|
toolName: 'read_file',
|
||||||
|
args: { path: '/foo/bar.ts' },
|
||||||
|
isError: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
tokenUsage: {
|
||||||
|
input: 1000,
|
||||||
|
output: 250,
|
||||||
|
cacheRead: 0,
|
||||||
|
cacheWrite: 0,
|
||||||
|
total: 1250,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation() });
|
||||||
|
|
||||||
|
const result = await brain.conversations.addMessage(
|
||||||
|
{
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'Here is the file content you requested.',
|
||||||
|
metadata: assistantMetadata,
|
||||||
|
},
|
||||||
|
USER_ID,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(result!.role).toBe('assistant');
|
||||||
|
expect(result!.content).toBe('Here is the file content you requested.');
|
||||||
|
expect(result!.metadata).toMatchObject({
|
||||||
|
model: 'claude-3-5-sonnet-20241022',
|
||||||
|
provider: 'anthropic',
|
||||||
|
tokenUsage: { input: 1000, output: 250, total: 1250 },
|
||||||
|
});
|
||||||
|
expect((result!.metadata as Record<string, unknown>)['toolCalls']).toHaveLength(1);
|
||||||
|
expect(
|
||||||
|
(
|
||||||
|
(result!.metadata as Record<string, unknown>)['toolCalls'] as Array<Record<string, unknown>>
|
||||||
|
)[0]!['toolName'],
|
||||||
|
).toBe('read_file');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 3. ChatGateway.loadConversationHistory — session resume loads history
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('Conversation resume — history loading', () => {
|
||||||
|
it('maps DB messages to ConversationHistoryMessage shape', () => {
|
||||||
|
// Simulate what ChatGateway.loadConversationHistory does:
|
||||||
|
// convert DB Message rows to ConversationHistoryMessage for context injection.
|
||||||
|
const dbMessages = [
|
||||||
|
makeMessage('user', 'What is the capital of France?', {
|
||||||
|
createdAt: new Date('2026-01-01T00:01:00Z'),
|
||||||
|
}),
|
||||||
|
makeMessage('assistant', 'The capital of France is Paris.', {
|
||||||
|
createdAt: new Date('2026-01-01T00:01:05Z'),
|
||||||
|
}),
|
||||||
|
makeMessage('user', 'And Germany?', { createdAt: new Date('2026-01-01T00:02:00Z') }),
|
||||||
|
makeMessage('assistant', 'The capital of Germany is Berlin.', {
|
||||||
|
createdAt: new Date('2026-01-01T00:02:05Z'),
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Replicate the mapping logic from ChatGateway
|
||||||
|
const history: ConversationHistoryMessage[] = dbMessages.map((msg) => ({
|
||||||
|
role: msg.role as 'user' | 'assistant' | 'system',
|
||||||
|
content: msg.content,
|
||||||
|
createdAt: msg.createdAt,
|
||||||
|
}));
|
||||||
|
|
||||||
|
expect(history).toHaveLength(4);
|
||||||
|
expect(history[0]).toEqual({
|
||||||
|
role: 'user',
|
||||||
|
content: 'What is the capital of France?',
|
||||||
|
createdAt: new Date('2026-01-01T00:01:00Z'),
|
||||||
|
});
|
||||||
|
expect(history[1]).toEqual({
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'The capital of France is Paris.',
|
||||||
|
createdAt: new Date('2026-01-01T00:01:05Z'),
|
||||||
|
});
|
||||||
|
expect(history[2]!.role).toBe('user');
|
||||||
|
expect(history[3]!.role).toBe('assistant');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array when conversation has no messages', async () => {
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation(), messages: [] });
|
||||||
|
|
||||||
|
const messages = await brain.conversations.findMessages(CONV_ID, USER_ID);
|
||||||
|
expect(messages).toHaveLength(0);
|
||||||
|
|
||||||
|
// Gateway produces empty history → no context injection
|
||||||
|
const history: ConversationHistoryMessage[] = (messages as Message[]).map((msg) => ({
|
||||||
|
role: msg.role as 'user' | 'assistant' | 'system',
|
||||||
|
content: msg.content,
|
||||||
|
createdAt: msg.createdAt,
|
||||||
|
}));
|
||||||
|
expect(history).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array when conversation does not belong to the user', async () => {
|
||||||
|
const brain = createMockBrain({ conversation: undefined });
|
||||||
|
brain.conversations.findMessages = vi.fn().mockResolvedValue([]);
|
||||||
|
|
||||||
|
const messages = await brain.conversations.findMessages(CONV_ID, 'other-user');
|
||||||
|
expect(messages).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('preserves message order (ascending by createdAt)', async () => {
|
||||||
|
const ordered = [
|
||||||
|
makeMessage('user', 'First', { createdAt: new Date('2026-01-01T00:01:00Z') }),
|
||||||
|
makeMessage('assistant', 'Second', { createdAt: new Date('2026-01-01T00:01:05Z') }),
|
||||||
|
makeMessage('user', 'Third', { createdAt: new Date('2026-01-01T00:02:00Z') }),
|
||||||
|
];
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation(), messages: ordered });
|
||||||
|
|
||||||
|
const messages = await brain.conversations.findMessages(CONV_ID, USER_ID);
|
||||||
|
expect(messages[0]!.content).toBe('First');
|
||||||
|
expect(messages[1]!.content).toBe('Second');
|
||||||
|
expect(messages[2]!.content).toBe('Third');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 4. AgentService.buildHistoryPromptSection — context injection format
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('AgentService — buildHistoryPromptSection (context injection)', () => {
|
||||||
|
/**
|
||||||
|
* Replicate the private method logic to test it in isolation.
|
||||||
|
* The real method lives in AgentService but is private; we mirror the
|
||||||
|
* exact logic here so the test is independent of the service's constructor.
|
||||||
|
*/
|
||||||
|
function buildHistoryPromptSection(
|
||||||
|
history: ConversationHistoryMessage[],
|
||||||
|
contextWindow: number,
|
||||||
|
_sessionId: string,
|
||||||
|
): string {
|
||||||
|
const TOKEN_BUDGET = Math.floor(contextWindow * 0.8);
|
||||||
|
const HISTORY_HEADER = '## Conversation History (resumed session)\n\n';
|
||||||
|
|
||||||
|
const formatMessage = (msg: ConversationHistoryMessage): string => {
|
||||||
|
const roleLabel =
|
||||||
|
msg.role === 'user' ? 'User' : msg.role === 'assistant' ? 'Assistant' : 'System';
|
||||||
|
return `**${roleLabel}:** ${msg.content}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const estimateTokens = (text: string) => Math.ceil(text.length / 4);
|
||||||
|
|
||||||
|
const formatted = history.map((msg) => formatMessage(msg));
|
||||||
|
const fullHistory = formatted.join('\n\n');
|
||||||
|
const fullTokens = estimateTokens(HISTORY_HEADER + fullHistory);
|
||||||
|
|
||||||
|
if (fullTokens <= TOKEN_BUDGET) {
|
||||||
|
return HISTORY_HEADER + fullHistory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// History exceeds budget — summarize oldest messages, keep recent verbatim
|
||||||
|
const SUMMARY_RESERVE = Math.floor(TOKEN_BUDGET * 0.2);
|
||||||
|
const verbatimBudget = TOKEN_BUDGET - SUMMARY_RESERVE;
|
||||||
|
|
||||||
|
let verbatimTokens = 0;
|
||||||
|
let verbatimCutIndex = history.length;
|
||||||
|
for (let i = history.length - 1; i >= 0; i--) {
|
||||||
|
const t = estimateTokens(formatted[i]!);
|
||||||
|
if (verbatimTokens + t > verbatimBudget) break;
|
||||||
|
verbatimTokens += t;
|
||||||
|
verbatimCutIndex = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
const summarizedMessages = history.slice(0, verbatimCutIndex);
|
||||||
|
const verbatimMessages = history.slice(verbatimCutIndex);
|
||||||
|
|
||||||
|
let summaryText = '';
|
||||||
|
if (summarizedMessages.length > 0) {
|
||||||
|
const topics = summarizedMessages
|
||||||
|
.filter((m) => m.role === 'user')
|
||||||
|
.map((m) => m.content.slice(0, 120).replace(/\n/g, ' '))
|
||||||
|
.join('; ');
|
||||||
|
summaryText =
|
||||||
|
`**Previous conversation summary** (${summarizedMessages.length} messages omitted for brevity):\n` +
|
||||||
|
`Topics discussed: ${topics || '(no user messages in summarized portion)'}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const verbatimSection = verbatimMessages.map((m) => formatMessage(m)).join('\n\n');
|
||||||
|
|
||||||
|
const parts: string[] = [HISTORY_HEADER];
|
||||||
|
if (summaryText) parts.push(summaryText);
|
||||||
|
if (verbatimSection) parts.push(verbatimSection);
|
||||||
|
|
||||||
|
return parts.join('\n\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
it('includes header and all messages when history fits within context budget', () => {
|
||||||
|
const history: ConversationHistoryMessage[] = [
|
||||||
|
{ role: 'user', content: 'Hello', createdAt: new Date() },
|
||||||
|
{ role: 'assistant', content: 'Hi there!', createdAt: new Date() },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = buildHistoryPromptSection(history, 8192, 'session-1');
|
||||||
|
|
||||||
|
expect(result).toContain('## Conversation History (resumed session)');
|
||||||
|
expect(result).toContain('**User:** Hello');
|
||||||
|
expect(result).toContain('**Assistant:** Hi there!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('labels roles correctly (user, assistant, system)', () => {
|
||||||
|
const history: ConversationHistoryMessage[] = [
|
||||||
|
{ role: 'system', content: 'You are helpful.', createdAt: new Date() },
|
||||||
|
{ role: 'user', content: 'Ping', createdAt: new Date() },
|
||||||
|
{ role: 'assistant', content: 'Pong', createdAt: new Date() },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = buildHistoryPromptSection(history, 8192, 'session-2');
|
||||||
|
|
||||||
|
expect(result).toContain('**System:** You are helpful.');
|
||||||
|
expect(result).toContain('**User:** Ping');
|
||||||
|
expect(result).toContain('**Assistant:** Pong');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('summarizes old messages when history exceeds 80% of context window', () => {
|
||||||
|
// Create enough messages to exceed a tiny context window budget
|
||||||
|
const longContent = 'A'.repeat(200);
|
||||||
|
const history: ConversationHistoryMessage[] = Array.from({ length: 20 }, (_, i) => ({
|
||||||
|
role: (i % 2 === 0 ? 'user' : 'assistant') as 'user' | 'assistant',
|
||||||
|
content: `${longContent} message ${i}`,
|
||||||
|
createdAt: new Date(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Use a small context window so history definitely exceeds 80%
|
||||||
|
const result = buildHistoryPromptSection(history, 512, 'session-3');
|
||||||
|
|
||||||
|
// Should contain the summary prefix
|
||||||
|
expect(result).toContain('messages omitted for brevity');
|
||||||
|
expect(result).toContain('Topics discussed:');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns only header for empty history', () => {
|
||||||
|
const result = buildHistoryPromptSection([], 8192, 'session-4');
|
||||||
|
// With empty history, the full history join is '' and the section is just the header
|
||||||
|
expect(result).toContain('## Conversation History (resumed session)');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 5. ConversationsController.search — GET /api/conversations/search
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('ConversationsController — search endpoint', () => {
|
||||||
|
let brain: ReturnType<typeof createMockBrain>;
|
||||||
|
let controller: ConversationsController;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const searchResults = [
|
||||||
|
{
|
||||||
|
messageId: 'msg-001',
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
conversationTitle: 'Test Chat',
|
||||||
|
role: 'user' as const,
|
||||||
|
content: 'What is the capital of France?',
|
||||||
|
createdAt: new Date('2026-01-01T00:01:00Z'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
messageId: 'msg-002',
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
conversationTitle: 'Test Chat',
|
||||||
|
role: 'assistant' as const,
|
||||||
|
content: 'The capital of France is Paris.',
|
||||||
|
createdAt: new Date('2026-01-01T00:01:05Z'),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
brain = createMockBrain({ searchResults });
|
||||||
|
controller = new ConversationsController(brain as never);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns matching messages for a valid search query', async () => {
|
||||||
|
const results = await controller.search({ q: 'France' }, { id: USER_ID });
|
||||||
|
|
||||||
|
expect(brain.conversations.searchMessages).toHaveBeenCalledWith(USER_ID, 'France', 20, 0);
|
||||||
|
expect(results).toHaveLength(2);
|
||||||
|
expect(results[0]).toMatchObject({
|
||||||
|
messageId: 'msg-001',
|
||||||
|
role: 'user',
|
||||||
|
content: 'What is the capital of France?',
|
||||||
|
});
|
||||||
|
expect(results[1]).toMatchObject({
|
||||||
|
messageId: 'msg-002',
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'The capital of France is Paris.',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses custom limit and offset when provided', async () => {
|
||||||
|
await controller.search({ q: 'Paris', limit: 5, offset: 10 }, { id: USER_ID });
|
||||||
|
|
||||||
|
expect(brain.conversations.searchMessages).toHaveBeenCalledWith(USER_ID, 'Paris', 5, 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws BadRequestException when query is empty', async () => {
|
||||||
|
await expect(controller.search({ q: '' }, { id: USER_ID })).rejects.toBeInstanceOf(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
await expect(controller.search({ q: ' ' }, { id: USER_ID })).rejects.toBeInstanceOf(
|
||||||
|
BadRequestException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('trims whitespace from query before passing to repo', async () => {
|
||||||
|
await controller.search({ q: ' Berlin ' }, { id: USER_ID });
|
||||||
|
|
||||||
|
expect(brain.conversations.searchMessages).toHaveBeenCalledWith(
|
||||||
|
USER_ID,
|
||||||
|
'Berlin',
|
||||||
|
expect.any(Number),
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array when no messages match', async () => {
|
||||||
|
brain.conversations.searchMessages = vi.fn().mockResolvedValue([]);
|
||||||
|
|
||||||
|
const results = await controller.search({ q: 'xyzzy-no-match' }, { id: USER_ID });
|
||||||
|
|
||||||
|
expect(results).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 6. ConversationsController — messages CRUD
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('ConversationsController — message CRUD', () => {
|
||||||
|
it('listMessages returns 404 when conversation is not owned by user', async () => {
|
||||||
|
const brain = createMockBrain({ conversation: undefined });
|
||||||
|
const controller = new ConversationsController(brain as never);
|
||||||
|
|
||||||
|
await expect(controller.listMessages(CONV_ID, { id: USER_ID })).rejects.toBeInstanceOf(
|
||||||
|
NotFoundException,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('listMessages returns the messages for an owned conversation', async () => {
|
||||||
|
const msgs = [makeMessage('user', 'Test message'), makeMessage('assistant', 'Test reply')];
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation(), messages: msgs });
|
||||||
|
const controller = new ConversationsController(brain as never);
|
||||||
|
|
||||||
|
const result = await controller.listMessages(CONV_ID, { id: USER_ID });
|
||||||
|
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(result[0]!.role).toBe('user');
|
||||||
|
expect(result[1]!.role).toBe('assistant');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('addMessage returns the persisted message', async () => {
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation() });
|
||||||
|
const controller = new ConversationsController(brain as never);
|
||||||
|
|
||||||
|
const result = await controller.addMessage(
|
||||||
|
CONV_ID,
|
||||||
|
{ role: 'user', content: 'Persisted content' },
|
||||||
|
{ id: USER_ID },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(result.role).toBe('user');
|
||||||
|
expect(result.content).toBe('Persisted content');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 7. End-to-end persistence flow simulation
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('End-to-end persistence flow', () => {
|
||||||
|
it('simulates a full conversation: persist user message → persist assistant response → resume with history', async () => {
|
||||||
|
// ── Step 1: Conversation is created ────────────────────────────────────
|
||||||
|
const brain = createMockBrain({ conversation: makeConversation() });
|
||||||
|
|
||||||
|
await brain.conversations.create({ id: CONV_ID, userId: USER_ID });
|
||||||
|
expect(brain.conversations.create).toHaveBeenCalledOnce();
|
||||||
|
|
||||||
|
// ── Step 2: User message is persisted ──────────────────────────────────
|
||||||
|
const userMsg = await brain.conversations.addMessage(
|
||||||
|
{
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
role: 'user',
|
||||||
|
content: 'Explain monads in simple terms.',
|
||||||
|
metadata: { timestamp: '2026-01-01T00:01:00.000Z' },
|
||||||
|
},
|
||||||
|
USER_ID,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(userMsg).toBeDefined();
|
||||||
|
expect(userMsg!.role).toBe('user');
|
||||||
|
|
||||||
|
// ── Step 3: Assistant response is persisted with metadata ───────────────
|
||||||
|
const assistantMeta = {
|
||||||
|
timestamp: '2026-01-01T00:01:10.000Z',
|
||||||
|
model: 'claude-3-5-sonnet-20241022',
|
||||||
|
provider: 'anthropic',
|
||||||
|
toolCalls: [],
|
||||||
|
tokenUsage: { input: 500, output: 120, cacheRead: 0, cacheWrite: 0, total: 620 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const assistantMsg = await brain.conversations.addMessage(
|
||||||
|
{
|
||||||
|
conversationId: CONV_ID,
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'A monad is a design pattern that wraps values in a context...',
|
||||||
|
metadata: assistantMeta,
|
||||||
|
},
|
||||||
|
USER_ID,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(assistantMsg).toBeDefined();
|
||||||
|
expect(assistantMsg!.role).toBe('assistant');
|
||||||
|
|
||||||
|
// ── Step 4: On session resume, history is loaded ────────────────────────
|
||||||
|
const storedMessages = [
|
||||||
|
makeMessage('user', 'Explain monads in simple terms.', {
|
||||||
|
createdAt: new Date('2026-01-01T00:01:00Z'),
|
||||||
|
metadata: { timestamp: '2026-01-01T00:01:00.000Z' },
|
||||||
|
}),
|
||||||
|
makeMessage('assistant', 'A monad is a design pattern that wraps values in a context...', {
|
||||||
|
createdAt: new Date('2026-01-01T00:01:10Z'),
|
||||||
|
metadata: assistantMeta,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
brain.conversations.findMessages = vi.fn().mockResolvedValue(storedMessages);
|
||||||
|
|
||||||
|
const dbMessages = await brain.conversations.findMessages(CONV_ID, USER_ID);
|
||||||
|
expect(dbMessages).toHaveLength(2);
|
||||||
|
|
||||||
|
// ── Step 5: History is mapped for context injection ─────────────────────
|
||||||
|
const history: ConversationHistoryMessage[] = (dbMessages as Message[]).map((msg) => ({
|
||||||
|
role: msg.role as 'user' | 'assistant' | 'system',
|
||||||
|
content: msg.content,
|
||||||
|
createdAt: msg.createdAt,
|
||||||
|
}));
|
||||||
|
|
||||||
|
expect(history[0]).toMatchObject({
|
||||||
|
role: 'user',
|
||||||
|
content: 'Explain monads in simple terms.',
|
||||||
|
});
|
||||||
|
expect(history[1]).toMatchObject({
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'A monad is a design pattern that wraps values in a context...',
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── Step 6: History roles are valid for injection ───────────────────────
|
||||||
|
for (const msg of history) {
|
||||||
|
expect(['user', 'assistant', 'system']).toContain(msg.role);
|
||||||
|
expect(typeof msg.content).toBe('string');
|
||||||
|
expect(msg.createdAt).toBeInstanceOf(Date);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
470
apps/gateway/src/__tests__/cross-user-isolation.test.ts
Normal file
470
apps/gateway/src/__tests__/cross-user-isolation.test.ts
Normal file
@@ -0,0 +1,470 @@
|
|||||||
|
/**
|
||||||
|
* Integration test: Cross-user data isolation (M2-007)
|
||||||
|
*
|
||||||
|
* Verifies that every repository query path is scoped to the requesting user —
|
||||||
|
* no user can read, write, or enumerate another user's records.
|
||||||
|
*
|
||||||
|
* Test strategy:
|
||||||
|
* - Two real users (User A, User B) are inserted directly into the database.
|
||||||
|
* - Realistic data (conversations + messages, agent configs, preferences,
|
||||||
|
* insights) is created for each user.
|
||||||
|
* - A shared system agent is inserted so both users can see it via
|
||||||
|
* findAccessible().
|
||||||
|
* - All assertions are made against the live database (no mocks).
|
||||||
|
* - All inserted rows are cleaned up in the afterAll hook.
|
||||||
|
*
|
||||||
|
* Requires: DATABASE_URL pointing at a running PostgreSQL instance with
|
||||||
|
* pgvector enabled and the Mosaic schema already applied.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||||
|
import { createDb } from '@mosaic/db';
|
||||||
|
import { createConversationsRepo } from '@mosaic/brain';
|
||||||
|
import { createAgentsRepo } from '@mosaic/brain';
|
||||||
|
import { createPreferencesRepo, createInsightsRepo } from '@mosaic/memory';
|
||||||
|
import { users, conversations, messages, agents, preferences, insights } from '@mosaic/db';
|
||||||
|
import { eq } from '@mosaic/db';
|
||||||
|
import type { DbHandle } from '@mosaic/db';
|
||||||
|
|
||||||
|
// ─── Fixed IDs so the afterAll cleanup is deterministic ──────────────────────
|
||||||
|
|
||||||
|
const USER_A_ID = 'test-iso-user-a';
|
||||||
|
const USER_B_ID = 'test-iso-user-b';
|
||||||
|
const CONV_A_ID = 'aaaaaaaa-0000-0000-0000-000000000001';
|
||||||
|
const CONV_B_ID = 'bbbbbbbb-0000-0000-0000-000000000001';
|
||||||
|
const MSG_A_ID = 'aaaaaaaa-0000-0000-0000-000000000002';
|
||||||
|
const MSG_B_ID = 'bbbbbbbb-0000-0000-0000-000000000002';
|
||||||
|
const AGENT_A_ID = 'aaaaaaaa-0000-0000-0000-000000000003';
|
||||||
|
const AGENT_B_ID = 'bbbbbbbb-0000-0000-0000-000000000003';
|
||||||
|
const AGENT_SYS_ID = 'ffffffff-0000-0000-0000-000000000001';
|
||||||
|
const PREF_A_ID = 'aaaaaaaa-0000-0000-0000-000000000004';
|
||||||
|
const PREF_B_ID = 'bbbbbbbb-0000-0000-0000-000000000004';
|
||||||
|
const INSIGHT_A_ID = 'aaaaaaaa-0000-0000-0000-000000000005';
|
||||||
|
const INSIGHT_B_ID = 'bbbbbbbb-0000-0000-0000-000000000005';
|
||||||
|
|
||||||
|
// ─── Test fixture ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
let handle: DbHandle;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
handle = createDb();
|
||||||
|
const db = handle.db;
|
||||||
|
|
||||||
|
// Insert two users
|
||||||
|
await db
|
||||||
|
.insert(users)
|
||||||
|
.values([
|
||||||
|
{
|
||||||
|
id: USER_A_ID,
|
||||||
|
name: 'Isolation Test User A',
|
||||||
|
email: 'test-iso-user-a@example.invalid',
|
||||||
|
emailVerified: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: USER_B_ID,
|
||||||
|
name: 'Isolation Test User B',
|
||||||
|
email: 'test-iso-user-b@example.invalid',
|
||||||
|
emailVerified: false,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
// Conversations — one per user
|
||||||
|
await db
|
||||||
|
.insert(conversations)
|
||||||
|
.values([
|
||||||
|
{ id: CONV_A_ID, userId: USER_A_ID, title: 'User A conversation' },
|
||||||
|
{ id: CONV_B_ID, userId: USER_B_ID, title: 'User B conversation' },
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
// Messages — one per conversation
|
||||||
|
await db
|
||||||
|
.insert(messages)
|
||||||
|
.values([
|
||||||
|
{
|
||||||
|
id: MSG_A_ID,
|
||||||
|
conversationId: CONV_A_ID,
|
||||||
|
role: 'user',
|
||||||
|
content: 'Hello from User A',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: MSG_B_ID,
|
||||||
|
conversationId: CONV_B_ID,
|
||||||
|
role: 'user',
|
||||||
|
content: 'Hello from User B',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
// Agent configs — private agents (one per user) + one system agent
|
||||||
|
await db
|
||||||
|
.insert(agents)
|
||||||
|
.values([
|
||||||
|
{
|
||||||
|
id: AGENT_A_ID,
|
||||||
|
name: 'Agent A (private)',
|
||||||
|
provider: 'test',
|
||||||
|
model: 'test-model',
|
||||||
|
ownerId: USER_A_ID,
|
||||||
|
isSystem: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: AGENT_B_ID,
|
||||||
|
name: 'Agent B (private)',
|
||||||
|
provider: 'test',
|
||||||
|
model: 'test-model',
|
||||||
|
ownerId: USER_B_ID,
|
||||||
|
isSystem: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: AGENT_SYS_ID,
|
||||||
|
name: 'Shared System Agent',
|
||||||
|
provider: 'test',
|
||||||
|
model: 'test-model',
|
||||||
|
ownerId: null,
|
||||||
|
isSystem: true,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
// Preferences — one per user (same key, different values)
|
||||||
|
await db
|
||||||
|
.insert(preferences)
|
||||||
|
.values([
|
||||||
|
{
|
||||||
|
id: PREF_A_ID,
|
||||||
|
userId: USER_A_ID,
|
||||||
|
key: 'theme',
|
||||||
|
value: 'dark',
|
||||||
|
category: 'appearance',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: PREF_B_ID,
|
||||||
|
userId: USER_B_ID,
|
||||||
|
key: 'theme',
|
||||||
|
value: 'light',
|
||||||
|
category: 'appearance',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
|
||||||
|
// Insights — no embedding to keep the fixture simple; embedding-based search
|
||||||
|
// is tested separately with a zero-vector that falls outside maxDistance
|
||||||
|
await db
|
||||||
|
.insert(insights)
|
||||||
|
.values([
|
||||||
|
{
|
||||||
|
id: INSIGHT_A_ID,
|
||||||
|
userId: USER_A_ID,
|
||||||
|
content: 'User A insight',
|
||||||
|
source: 'user',
|
||||||
|
category: 'general',
|
||||||
|
relevanceScore: 1.0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: INSIGHT_B_ID,
|
||||||
|
userId: USER_B_ID,
|
||||||
|
content: 'User B insight',
|
||||||
|
source: 'user',
|
||||||
|
category: 'general',
|
||||||
|
relevanceScore: 1.0,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
.onConflictDoNothing();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (!handle) return;
|
||||||
|
const db = handle.db;
|
||||||
|
|
||||||
|
// Delete in dependency order (FK constraints)
|
||||||
|
await db.delete(messages).where(eq(messages.id, MSG_A_ID));
|
||||||
|
await db.delete(messages).where(eq(messages.id, MSG_B_ID));
|
||||||
|
await db.delete(conversations).where(eq(conversations.id, CONV_A_ID));
|
||||||
|
await db.delete(conversations).where(eq(conversations.id, CONV_B_ID));
|
||||||
|
await db.delete(agents).where(eq(agents.id, AGENT_A_ID));
|
||||||
|
await db.delete(agents).where(eq(agents.id, AGENT_B_ID));
|
||||||
|
await db.delete(agents).where(eq(agents.id, AGENT_SYS_ID));
|
||||||
|
await db.delete(preferences).where(eq(preferences.id, PREF_A_ID));
|
||||||
|
await db.delete(preferences).where(eq(preferences.id, PREF_B_ID));
|
||||||
|
await db.delete(insights).where(eq(insights.id, INSIGHT_A_ID));
|
||||||
|
await db.delete(insights).where(eq(insights.id, INSIGHT_B_ID));
|
||||||
|
await db.delete(users).where(eq(users.id, USER_A_ID));
|
||||||
|
await db.delete(users).where(eq(users.id, USER_B_ID));
|
||||||
|
|
||||||
|
await handle.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Conversations isolation ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('ConversationsRepo — cross-user isolation', () => {
|
||||||
|
it('User A can find their own conversation by id', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const conv = await repo.findById(CONV_A_ID, USER_A_ID);
|
||||||
|
expect(conv).toBeDefined();
|
||||||
|
expect(conv!.id).toBe(CONV_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User B cannot find User A conversation by id (returns undefined)', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const conv = await repo.findById(CONV_A_ID, USER_B_ID);
|
||||||
|
expect(conv).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User A cannot find User B conversation by id (returns undefined)', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const conv = await repo.findById(CONV_B_ID, USER_A_ID);
|
||||||
|
expect(conv).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('findAll returns only own conversations for User A', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const convs = await repo.findAll(USER_A_ID);
|
||||||
|
const ids = convs.map((c) => c.id);
|
||||||
|
expect(ids).toContain(CONV_A_ID);
|
||||||
|
expect(ids).not.toContain(CONV_B_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('findAll returns only own conversations for User B', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const convs = await repo.findAll(USER_B_ID);
|
||||||
|
const ids = convs.map((c) => c.id);
|
||||||
|
expect(ids).toContain(CONV_B_ID);
|
||||||
|
expect(ids).not.toContain(CONV_A_ID);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Messages isolation ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('ConversationsRepo.findMessages — cross-user isolation', () => {
|
||||||
|
it('User A can read messages from their own conversation', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const msgs = await repo.findMessages(CONV_A_ID, USER_A_ID);
|
||||||
|
const ids = msgs.map((m) => m.id);
|
||||||
|
expect(ids).toContain(MSG_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User B cannot read messages from User A conversation (returns empty array)', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const msgs = await repo.findMessages(CONV_A_ID, USER_B_ID);
|
||||||
|
expect(msgs).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User A cannot read messages from User B conversation (returns empty array)', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const msgs = await repo.findMessages(CONV_B_ID, USER_A_ID);
|
||||||
|
expect(msgs).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('addMessage is rejected when user does not own the conversation', async () => {
|
||||||
|
const repo = createConversationsRepo(handle.db);
|
||||||
|
const result = await repo.addMessage(
|
||||||
|
{
|
||||||
|
conversationId: CONV_A_ID,
|
||||||
|
role: 'user',
|
||||||
|
content: 'Attempted injection by User B',
|
||||||
|
},
|
||||||
|
USER_B_ID,
|
||||||
|
);
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Agent configs isolation ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('AgentsRepo.findAccessible — cross-user isolation', () => {
|
||||||
|
it('User A sees their own private agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const accessible = await repo.findAccessible(USER_A_ID);
|
||||||
|
const ids = accessible.map((a) => a.id);
|
||||||
|
expect(ids).toContain(AGENT_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User A does NOT see User B private agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const accessible = await repo.findAccessible(USER_A_ID);
|
||||||
|
const ids = accessible.map((a) => a.id);
|
||||||
|
expect(ids).not.toContain(AGENT_B_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User B does NOT see User A private agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const accessible = await repo.findAccessible(USER_B_ID);
|
||||||
|
const ids = accessible.map((a) => a.id);
|
||||||
|
expect(ids).not.toContain(AGENT_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Both users can see the shared system agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const accessibleA = await repo.findAccessible(USER_A_ID);
|
||||||
|
const accessibleB = await repo.findAccessible(USER_B_ID);
|
||||||
|
expect(accessibleA.map((a) => a.id)).toContain(AGENT_SYS_ID);
|
||||||
|
expect(accessibleB.map((a) => a.id)).toContain(AGENT_SYS_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('findSystem returns the system agent for any caller', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const system = await repo.findSystem();
|
||||||
|
const ids = system.map((a) => a.id);
|
||||||
|
expect(ids).toContain(AGENT_SYS_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('update with ownerId prevents User B from modifying User A agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const result = await repo.update(AGENT_A_ID, { model: 'hacked' }, USER_B_ID);
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
|
||||||
|
// Verify the agent was not actually mutated
|
||||||
|
const unchanged = await repo.findById(AGENT_A_ID);
|
||||||
|
expect(unchanged?.model).toBe('test-model');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('remove prevents User B from deleting User A agent', async () => {
|
||||||
|
const repo = createAgentsRepo(handle.db);
|
||||||
|
const deleted = await repo.remove(AGENT_A_ID, USER_B_ID);
|
||||||
|
expect(deleted).toBe(false);
|
||||||
|
|
||||||
|
// Verify the agent still exists
|
||||||
|
const still = await repo.findById(AGENT_A_ID);
|
||||||
|
expect(still).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Preferences isolation ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('PreferencesRepo — cross-user isolation', () => {
|
||||||
|
it('User A can retrieve their own preferences', async () => {
|
||||||
|
const repo = createPreferencesRepo(handle.db);
|
||||||
|
const prefs = await repo.findByUser(USER_A_ID);
|
||||||
|
const ids = prefs.map((p) => p.id);
|
||||||
|
expect(ids).toContain(PREF_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User A preferences do not contain User B preferences', async () => {
|
||||||
|
const repo = createPreferencesRepo(handle.db);
|
||||||
|
const prefs = await repo.findByUser(USER_A_ID);
|
||||||
|
const ids = prefs.map((p) => p.id);
|
||||||
|
expect(ids).not.toContain(PREF_B_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User B preferences do not contain User A preferences', async () => {
|
||||||
|
const repo = createPreferencesRepo(handle.db);
|
||||||
|
const prefs = await repo.findByUser(USER_B_ID);
|
||||||
|
const ids = prefs.map((p) => p.id);
|
||||||
|
expect(ids).not.toContain(PREF_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('findByUserAndKey is scoped to the requesting user', async () => {
|
||||||
|
const repo = createPreferencesRepo(handle.db);
|
||||||
|
// Both users have key "theme" — each should only see their own value
|
||||||
|
const prefA = await repo.findByUserAndKey(USER_A_ID, 'theme');
|
||||||
|
const prefB = await repo.findByUserAndKey(USER_B_ID, 'theme');
|
||||||
|
|
||||||
|
expect(prefA).toBeDefined();
|
||||||
|
// Drizzle returns JSONB values as parsed JS values; '"dark"' (JSON string) → 'dark'
|
||||||
|
expect(prefA!.value).toBe('dark');
|
||||||
|
expect(prefB).toBeDefined();
|
||||||
|
expect(prefB!.value).toBe('light');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('remove is scoped to the requesting user (cannot delete another user pref)', async () => {
|
||||||
|
const repo = createPreferencesRepo(handle.db);
|
||||||
|
// User B tries to delete User A's "theme" preference — should silently fail
|
||||||
|
const deleted = await repo.remove(USER_B_ID, 'theme');
|
||||||
|
// This only deletes USER_B's own "theme" row; re-insert it for afterAll cleanup
|
||||||
|
expect(deleted).toBe(true); // deletes User B's OWN theme pref
|
||||||
|
|
||||||
|
// User A's theme pref must be untouched
|
||||||
|
const prefA = await repo.findByUserAndKey(USER_A_ID, 'theme');
|
||||||
|
expect(prefA).toBeDefined();
|
||||||
|
|
||||||
|
// Re-insert User B's preference so afterAll cleanup still finds it
|
||||||
|
await repo.upsert({
|
||||||
|
id: PREF_B_ID,
|
||||||
|
userId: USER_B_ID,
|
||||||
|
key: 'theme',
|
||||||
|
value: 'light',
|
||||||
|
category: 'appearance',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Insights isolation ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe('InsightsRepo — cross-user isolation', () => {
|
||||||
|
it('User A can retrieve their own insights', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const list = await repo.findByUser(USER_A_ID);
|
||||||
|
const ids = list.map((i) => i.id);
|
||||||
|
expect(ids).toContain(INSIGHT_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User A insights do not contain User B insights', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const list = await repo.findByUser(USER_A_ID);
|
||||||
|
const ids = list.map((i) => i.id);
|
||||||
|
expect(ids).not.toContain(INSIGHT_B_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('User B insights do not contain User A insights', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const list = await repo.findByUser(USER_B_ID);
|
||||||
|
const ids = list.map((i) => i.id);
|
||||||
|
expect(ids).not.toContain(INSIGHT_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('findById is scoped to the requesting user', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const own = await repo.findById(INSIGHT_A_ID, USER_A_ID);
|
||||||
|
const cross = await repo.findById(INSIGHT_A_ID, USER_B_ID);
|
||||||
|
|
||||||
|
expect(own).toBeDefined();
|
||||||
|
expect(cross).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('searchByEmbedding returns only own insights', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
// Our test insights have no embedding — the query filters WHERE embedding IS NOT NULL
|
||||||
|
// so the result set is empty, which already proves no cross-user leakage.
|
||||||
|
// Using a 1536-dimension zero vector as the query embedding.
|
||||||
|
const zeroVector = Array<number>(1536).fill(0);
|
||||||
|
|
||||||
|
const resultsA = await repo.searchByEmbedding(USER_A_ID, zeroVector, 50, 2.0);
|
||||||
|
const resultsB = await repo.searchByEmbedding(USER_B_ID, zeroVector, 50, 2.0);
|
||||||
|
|
||||||
|
// The raw SQL query returns row objects directly (not wrapped in { insight }).
|
||||||
|
// Cast via unknown to extract id safely regardless of the return shape.
|
||||||
|
const toId = (r: unknown): string =>
|
||||||
|
((r as Record<string, unknown>)['id'] as string | undefined) ??
|
||||||
|
((r as Record<string, Record<string, unknown>>)['insight']?.['id'] as string | undefined) ??
|
||||||
|
'';
|
||||||
|
const idsInA = resultsA.map(toId);
|
||||||
|
const idsInB = resultsB.map(toId);
|
||||||
|
|
||||||
|
// User B's insight must never appear in User A's search results
|
||||||
|
expect(idsInA).not.toContain(INSIGHT_B_ID);
|
||||||
|
// User A's insight must never appear in User B's search results
|
||||||
|
expect(idsInB).not.toContain(INSIGHT_A_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('update is scoped to the requesting user', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const result = await repo.update(INSIGHT_A_ID, USER_B_ID, { content: 'hacked' });
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
|
||||||
|
// Verify the insight was not mutated
|
||||||
|
const unchanged = await repo.findById(INSIGHT_A_ID, USER_A_ID);
|
||||||
|
expect(unchanged?.content).toBe('User A insight');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('remove is scoped to the requesting user', async () => {
|
||||||
|
const repo = createInsightsRepo(handle.db);
|
||||||
|
const deleted = await repo.remove(INSIGHT_A_ID, USER_B_ID);
|
||||||
|
expect(deleted).toBe(false);
|
||||||
|
|
||||||
|
// Verify the insight still exists
|
||||||
|
const still = await repo.findById(INSIGHT_A_ID, USER_A_ID);
|
||||||
|
expect(still).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -57,11 +57,13 @@ function createBrain() {
|
|||||||
describe('Resource ownership checks', () => {
|
describe('Resource ownership checks', () => {
|
||||||
it('forbids access to another user conversation', async () => {
|
it('forbids access to another user conversation', async () => {
|
||||||
const brain = createBrain();
|
const brain = createBrain();
|
||||||
brain.conversations.findById.mockResolvedValue({ id: 'conv-1', userId: 'user-2' });
|
// The repo enforces ownership via the WHERE clause; it returns undefined when the
|
||||||
|
// conversation does not belong to the requesting user.
|
||||||
|
brain.conversations.findById.mockResolvedValue(undefined);
|
||||||
const controller = new ConversationsController(brain as never);
|
const controller = new ConversationsController(brain as never);
|
||||||
|
|
||||||
await expect(controller.findOne('conv-1', { id: 'user-1' })).rejects.toBeInstanceOf(
|
await expect(controller.findOne('conv-1', { id: 'user-1' })).rejects.toBeInstanceOf(
|
||||||
ForbiddenException,
|
NotFoundException,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
143
apps/gateway/src/agent/__tests__/provider.service.test.ts
Normal file
143
apps/gateway/src/agent/__tests__/provider.service.test.ts
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
import { beforeEach, afterEach, describe, expect, it } from 'vitest';
|
||||||
|
import { ProviderService } from '../provider.service.js';
|
||||||
|
|
||||||
|
const ENV_KEYS = [
|
||||||
|
'ANTHROPIC_API_KEY',
|
||||||
|
'OPENAI_API_KEY',
|
||||||
|
'ZAI_API_KEY',
|
||||||
|
'OLLAMA_BASE_URL',
|
||||||
|
'OLLAMA_HOST',
|
||||||
|
'OLLAMA_MODELS',
|
||||||
|
'MOSAIC_CUSTOM_PROVIDERS',
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
type EnvKey = (typeof ENV_KEYS)[number];
|
||||||
|
|
||||||
|
describe('ProviderService', () => {
|
||||||
|
const savedEnv = new Map<EnvKey, string | undefined>();
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
for (const key of ENV_KEYS) {
|
||||||
|
savedEnv.set(key, process.env[key]);
|
||||||
|
delete process.env[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const key of ENV_KEYS) {
|
||||||
|
const value = savedEnv.get(key);
|
||||||
|
if (value === undefined) {
|
||||||
|
delete process.env[key];
|
||||||
|
} else {
|
||||||
|
process.env[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skips API-key providers when env vars are missing (no models become available)', async () => {
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
// Pi's built-in registry may include model definitions for all providers, but
|
||||||
|
// without API keys none of them should be available (usable).
|
||||||
|
const availableModels = service.listAvailableModels();
|
||||||
|
const availableProviderIds = new Set(availableModels.map((m) => m.provider));
|
||||||
|
|
||||||
|
expect(availableProviderIds).not.toContain('anthropic');
|
||||||
|
expect(availableProviderIds).not.toContain('openai');
|
||||||
|
expect(availableProviderIds).not.toContain('zai');
|
||||||
|
|
||||||
|
// Providers list may show built-in providers, but they should not be marked available
|
||||||
|
const providers = service.listProviders();
|
||||||
|
for (const p of providers.filter((p) => ['anthropic', 'openai', 'zai'].includes(p.id))) {
|
||||||
|
expect(p.available).toBe(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers Anthropic provider with correct models when ANTHROPIC_API_KEY is set', async () => {
|
||||||
|
process.env['ANTHROPIC_API_KEY'] = 'test-anthropic';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const providers = service.listProviders();
|
||||||
|
const anthropic = providers.find((p) => p.id === 'anthropic');
|
||||||
|
expect(anthropic).toBeDefined();
|
||||||
|
expect(anthropic!.available).toBe(true);
|
||||||
|
expect(anthropic!.models.map((m) => m.id)).toEqual([
|
||||||
|
'claude-sonnet-4-6',
|
||||||
|
'claude-opus-4-6',
|
||||||
|
'claude-haiku-4-5',
|
||||||
|
]);
|
||||||
|
// contextWindow override from Pi built-in (200000)
|
||||||
|
for (const m of anthropic!.models) {
|
||||||
|
expect(m.contextWindow).toBe(200000);
|
||||||
|
// maxTokens capped at 8192 per task spec
|
||||||
|
expect(m.maxTokens).toBe(8192);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers OpenAI provider with correct models when OPENAI_API_KEY is set', async () => {
|
||||||
|
process.env['OPENAI_API_KEY'] = 'test-openai';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const providers = service.listProviders();
|
||||||
|
const openai = providers.find((p) => p.id === 'openai');
|
||||||
|
expect(openai).toBeDefined();
|
||||||
|
expect(openai!.available).toBe(true);
|
||||||
|
expect(openai!.models.map((m) => m.id)).toEqual(['gpt-4o', 'gpt-4o-mini', 'o3-mini']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers Z.ai provider with correct models when ZAI_API_KEY is set', async () => {
|
||||||
|
process.env['ZAI_API_KEY'] = 'test-zai';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const providers = service.listProviders();
|
||||||
|
const zai = providers.find((p) => p.id === 'zai');
|
||||||
|
expect(zai).toBeDefined();
|
||||||
|
expect(zai!.available).toBe(true);
|
||||||
|
expect(zai!.models.map((m) => m.id)).toEqual(['glm-4.5', 'glm-4.5-air', 'glm-4.5-flash']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers all three providers when all keys are set', async () => {
|
||||||
|
process.env['ANTHROPIC_API_KEY'] = 'test-anthropic';
|
||||||
|
process.env['OPENAI_API_KEY'] = 'test-openai';
|
||||||
|
process.env['ZAI_API_KEY'] = 'test-zai';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const providerIds = service.listProviders().map((p) => p.id);
|
||||||
|
expect(providerIds).toContain('anthropic');
|
||||||
|
expect(providerIds).toContain('openai');
|
||||||
|
expect(providerIds).toContain('zai');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can find registered Anthropic models by provider+id', async () => {
|
||||||
|
process.env['ANTHROPIC_API_KEY'] = 'test-anthropic';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const sonnet = service.findModel('anthropic', 'claude-sonnet-4-6');
|
||||||
|
expect(sonnet).toBeDefined();
|
||||||
|
expect(sonnet!.provider).toBe('anthropic');
|
||||||
|
expect(sonnet!.id).toBe('claude-sonnet-4-6');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can find registered Z.ai models by provider+id', async () => {
|
||||||
|
process.env['ZAI_API_KEY'] = 'test-zai';
|
||||||
|
|
||||||
|
const service = new ProviderService();
|
||||||
|
await service.onModuleInit();
|
||||||
|
|
||||||
|
const glm = service.findModel('zai', 'glm-4.5');
|
||||||
|
expect(glm).toBeDefined();
|
||||||
|
expect(glm!.provider).toBe('zai');
|
||||||
|
expect(glm!.id).toBe('glm-4.5');
|
||||||
|
});
|
||||||
|
});
|
||||||
191
apps/gateway/src/agent/adapters/anthropic.adapter.ts
Normal file
191
apps/gateway/src/agent/adapters/anthropic.adapter.ts
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import Anthropic from '@anthropic-ai/sdk';
|
||||||
|
import type { ModelRegistry } from '@mariozechner/pi-coding-agent';
|
||||||
|
import type {
|
||||||
|
CompletionEvent,
|
||||||
|
CompletionParams,
|
||||||
|
IProviderAdapter,
|
||||||
|
ModelInfo,
|
||||||
|
ProviderHealth,
|
||||||
|
} from '@mosaic/types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Anthropic provider adapter.
|
||||||
|
*
|
||||||
|
* Registers Claude models with the Pi ModelRegistry via the Anthropic SDK.
|
||||||
|
* Configuration is driven by environment variables:
|
||||||
|
* ANTHROPIC_API_KEY — Anthropic API key (required)
|
||||||
|
*/
|
||||||
|
export class AnthropicAdapter implements IProviderAdapter {
|
||||||
|
readonly name = 'anthropic';
|
||||||
|
|
||||||
|
private readonly logger = new Logger(AnthropicAdapter.name);
|
||||||
|
private client: Anthropic | null = null;
|
||||||
|
private registeredModels: ModelInfo[] = [];
|
||||||
|
|
||||||
|
constructor(private readonly registry: ModelRegistry) {}
|
||||||
|
|
||||||
|
async register(): Promise<void> {
|
||||||
|
const apiKey = process.env['ANTHROPIC_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
this.logger.warn('Skipping Anthropic provider registration: ANTHROPIC_API_KEY not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.client = new Anthropic({ apiKey });
|
||||||
|
|
||||||
|
const models: ModelInfo[] = [
|
||||||
|
{
|
||||||
|
id: 'claude-opus-4-6',
|
||||||
|
provider: 'anthropic',
|
||||||
|
name: 'Claude Opus 4.6',
|
||||||
|
reasoning: true,
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxTokens: 32000,
|
||||||
|
inputTypes: ['text', 'image'],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'claude-sonnet-4-6',
|
||||||
|
provider: 'anthropic',
|
||||||
|
name: 'Claude Sonnet 4.6',
|
||||||
|
reasoning: true,
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxTokens: 16000,
|
||||||
|
inputTypes: ['text', 'image'],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'claude-haiku-4-5',
|
||||||
|
provider: 'anthropic',
|
||||||
|
name: 'Claude Haiku 4.5',
|
||||||
|
reasoning: false,
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxTokens: 8192,
|
||||||
|
inputTypes: ['text', 'image'],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
this.registry.registerProvider('anthropic', {
|
||||||
|
apiKey,
|
||||||
|
baseUrl: 'https://api.anthropic.com',
|
||||||
|
api: 'anthropic' as never,
|
||||||
|
models: models.map((m) => ({
|
||||||
|
id: m.id,
|
||||||
|
name: m.name,
|
||||||
|
reasoning: m.reasoning,
|
||||||
|
input: m.inputTypes as ('text' | 'image')[],
|
||||||
|
cost: m.cost,
|
||||||
|
contextWindow: m.contextWindow,
|
||||||
|
maxTokens: m.maxTokens,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.registeredModels = models;
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`Anthropic provider registered with models: ${models.map((m) => m.id).join(', ')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
listModels(): ModelInfo[] {
|
||||||
|
return this.registeredModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
async healthCheck(): Promise<ProviderHealth> {
|
||||||
|
const apiKey = process.env['ANTHROPIC_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
return {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: 'ANTHROPIC_API_KEY not configured',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const client = this.client ?? new Anthropic({ apiKey });
|
||||||
|
await client.models.list({ limit: 1 });
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
return { status: 'healthy', latencyMs, lastChecked: new Date().toISOString() };
|
||||||
|
} catch (err) {
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
const error = err instanceof Error ? err.message : String(err);
|
||||||
|
const status = error.includes('401') || error.includes('403') ? 'degraded' : 'down';
|
||||||
|
return { status, latencyMs, lastChecked: new Date().toISOString(), error };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream a completion from Anthropic using the messages API.
|
||||||
|
* Maps Anthropic streaming events to the CompletionEvent format.
|
||||||
|
*
|
||||||
|
* Note: Currently reserved for future direct-completion use. The Pi SDK
|
||||||
|
* integration routes completions through ModelRegistry / AgentSession.
|
||||||
|
*/
|
||||||
|
async *createCompletion(params: CompletionParams): AsyncIterable<CompletionEvent> {
|
||||||
|
const apiKey = process.env['ANTHROPIC_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
throw new Error('AnthropicAdapter: ANTHROPIC_API_KEY not configured');
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = this.client ?? new Anthropic({ apiKey });
|
||||||
|
|
||||||
|
// Separate system messages from user/assistant messages
|
||||||
|
const systemMessages = params.messages.filter((m) => m.role === 'system');
|
||||||
|
const conversationMessages = params.messages.filter((m) => m.role !== 'system');
|
||||||
|
|
||||||
|
const systemPrompt =
|
||||||
|
systemMessages.length > 0 ? systemMessages.map((m) => m.content).join('\n') : undefined;
|
||||||
|
|
||||||
|
const stream = await client.messages.stream({
|
||||||
|
model: params.model,
|
||||||
|
max_tokens: params.maxTokens ?? 1024,
|
||||||
|
...(systemPrompt !== undefined ? { system: systemPrompt } : {}),
|
||||||
|
messages: conversationMessages.map((m) => ({
|
||||||
|
role: m.role as 'user' | 'assistant',
|
||||||
|
content: m.content,
|
||||||
|
})),
|
||||||
|
...(params.temperature !== undefined ? { temperature: params.temperature } : {}),
|
||||||
|
...(params.tools && params.tools.length > 0
|
||||||
|
? {
|
||||||
|
tools: params.tools.map((t) => ({
|
||||||
|
name: t.name,
|
||||||
|
description: t.description,
|
||||||
|
input_schema: t.parameters as Anthropic.Tool['input_schema'],
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const event of stream) {
|
||||||
|
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
|
||||||
|
yield { type: 'text_delta', content: event.delta.text };
|
||||||
|
} else if (event.type === 'content_block_delta' && event.delta.type === 'input_json_delta') {
|
||||||
|
yield { type: 'tool_call', name: '', arguments: event.delta.partial_json };
|
||||||
|
} else if (event.type === 'message_delta' && event.usage) {
|
||||||
|
yield {
|
||||||
|
type: 'done',
|
||||||
|
usage: {
|
||||||
|
inputTokens:
|
||||||
|
(event as { usage: { input_tokens?: number; output_tokens: number } }).usage
|
||||||
|
.input_tokens ?? 0,
|
||||||
|
outputTokens: event.usage.output_tokens,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit final done event with full usage from the completed message
|
||||||
|
const finalMessage = await stream.finalMessage();
|
||||||
|
yield {
|
||||||
|
type: 'done',
|
||||||
|
usage: {
|
||||||
|
inputTokens: finalMessage.usage.input_tokens,
|
||||||
|
outputTokens: finalMessage.usage.output_tokens,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
4
apps/gateway/src/agent/adapters/index.ts
Normal file
4
apps/gateway/src/agent/adapters/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export { OllamaAdapter } from './ollama.adapter.js';
|
||||||
|
export { AnthropicAdapter } from './anthropic.adapter.js';
|
||||||
|
export { OpenAIAdapter } from './openai.adapter.js';
|
||||||
|
export { OpenRouterAdapter } from './openrouter.adapter.js';
|
||||||
197
apps/gateway/src/agent/adapters/ollama.adapter.ts
Normal file
197
apps/gateway/src/agent/adapters/ollama.adapter.ts
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import type { ModelRegistry } from '@mariozechner/pi-coding-agent';
|
||||||
|
import type {
|
||||||
|
CompletionEvent,
|
||||||
|
CompletionParams,
|
||||||
|
IProviderAdapter,
|
||||||
|
ModelInfo,
|
||||||
|
ProviderHealth,
|
||||||
|
} from '@mosaic/types';
|
||||||
|
|
||||||
|
/** Embedding models that Ollama ships with out of the box */
|
||||||
|
const OLLAMA_EMBEDDING_MODELS: ReadonlyArray<{
|
||||||
|
id: string;
|
||||||
|
contextWindow: number;
|
||||||
|
dimensions: number;
|
||||||
|
}> = [
|
||||||
|
{ id: 'nomic-embed-text', contextWindow: 8192, dimensions: 768 },
|
||||||
|
{ id: 'mxbai-embed-large', contextWindow: 512, dimensions: 1024 },
|
||||||
|
];
|
||||||
|
|
||||||
|
interface OllamaEmbeddingResponse {
|
||||||
|
embedding?: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ollama provider adapter.
|
||||||
|
*
|
||||||
|
* Registers local Ollama models with the Pi ModelRegistry via the OpenAI-compatible
|
||||||
|
* completions API. Also exposes embedding models and an `embed()` method for
|
||||||
|
* vector generation (used by EmbeddingService / M3-009).
|
||||||
|
*
|
||||||
|
* Configuration is driven by environment variables:
|
||||||
|
* OLLAMA_BASE_URL or OLLAMA_HOST — base URL of the Ollama instance
|
||||||
|
* OLLAMA_MODELS — comma-separated list of model IDs (default: llama3.2,codellama,mistral)
|
||||||
|
*/
|
||||||
|
export class OllamaAdapter implements IProviderAdapter {
|
||||||
|
readonly name = 'ollama';
|
||||||
|
|
||||||
|
private readonly logger = new Logger(OllamaAdapter.name);
|
||||||
|
private registeredModels: ModelInfo[] = [];
|
||||||
|
|
||||||
|
constructor(private readonly registry: ModelRegistry) {}
|
||||||
|
|
||||||
|
async register(): Promise<void> {
|
||||||
|
const ollamaUrl = process.env['OLLAMA_BASE_URL'] ?? process.env['OLLAMA_HOST'];
|
||||||
|
if (!ollamaUrl) {
|
||||||
|
this.logger.debug('Skipping Ollama provider registration: OLLAMA_BASE_URL not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const modelsEnv = process.env['OLLAMA_MODELS'] ?? 'llama3.2,codellama,mistral';
|
||||||
|
const modelIds = modelsEnv
|
||||||
|
.split(',')
|
||||||
|
.map((id: string) => id.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
this.registry.registerProvider('ollama', {
|
||||||
|
baseUrl: `${ollamaUrl}/v1`,
|
||||||
|
apiKey: 'ollama',
|
||||||
|
api: 'openai-completions' as never,
|
||||||
|
models: modelIds.map((id) => ({
|
||||||
|
id,
|
||||||
|
name: id,
|
||||||
|
reasoning: false,
|
||||||
|
input: ['text'] as ('text' | 'image')[],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxTokens: 4096,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Chat / completion models
|
||||||
|
const completionModels: ModelInfo[] = modelIds.map((id) => ({
|
||||||
|
id,
|
||||||
|
provider: 'ollama',
|
||||||
|
name: id,
|
||||||
|
reasoning: false,
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxTokens: 4096,
|
||||||
|
inputTypes: ['text'] as ('text' | 'image')[],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Embedding models (tracked in registeredModels but not in Pi registry,
|
||||||
|
// which only handles completion models)
|
||||||
|
const embeddingModels: ModelInfo[] = OLLAMA_EMBEDDING_MODELS.map((em) => ({
|
||||||
|
id: em.id,
|
||||||
|
provider: 'ollama',
|
||||||
|
name: em.id,
|
||||||
|
reasoning: false,
|
||||||
|
contextWindow: em.contextWindow,
|
||||||
|
maxTokens: 0,
|
||||||
|
inputTypes: ['text'] as ('text' | 'image')[],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
}));
|
||||||
|
|
||||||
|
this.registeredModels = [...completionModels, ...embeddingModels];
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`Ollama provider registered at ${ollamaUrl} with models: ${modelIds.join(', ')} ` +
|
||||||
|
`and embedding models: ${OLLAMA_EMBEDDING_MODELS.map((em) => em.id).join(', ')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
listModels(): ModelInfo[] {
|
||||||
|
return this.registeredModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
async healthCheck(): Promise<ProviderHealth> {
|
||||||
|
const ollamaUrl = process.env['OLLAMA_BASE_URL'] ?? process.env['OLLAMA_HOST'];
|
||||||
|
if (!ollamaUrl) {
|
||||||
|
return {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: 'OLLAMA_BASE_URL not configured',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkUrl = `${ollamaUrl}/v1/models`;
|
||||||
|
const start = Date.now();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(checkUrl, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { Accept: 'application/json' },
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
|
});
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
return {
|
||||||
|
status: 'degraded',
|
||||||
|
latencyMs,
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: `HTTP ${res.status}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: 'healthy', latencyMs, lastChecked: new Date().toISOString() };
|
||||||
|
} catch (err) {
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
const error = err instanceof Error ? err.message : String(err);
|
||||||
|
return { status: 'down', latencyMs, lastChecked: new Date().toISOString(), error };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an embedding vector for the given text using Ollama's /api/embeddings endpoint.
|
||||||
|
*
|
||||||
|
* Defaults to 'nomic-embed-text' when no model is specified.
|
||||||
|
* Intended for use by EmbeddingService (M3-009).
|
||||||
|
*
|
||||||
|
* @param text - The input text to embed.
|
||||||
|
* @param model - Optional embedding model ID (default: 'nomic-embed-text').
|
||||||
|
* @returns A float array representing the embedding vector.
|
||||||
|
*/
|
||||||
|
async embed(text: string, model = 'nomic-embed-text'): Promise<number[]> {
|
||||||
|
const ollamaUrl = process.env['OLLAMA_BASE_URL'] ?? process.env['OLLAMA_HOST'];
|
||||||
|
if (!ollamaUrl) {
|
||||||
|
throw new Error('OllamaAdapter: OLLAMA_BASE_URL not configured');
|
||||||
|
}
|
||||||
|
|
||||||
|
const embeddingUrl = `${ollamaUrl}/api/embeddings`;
|
||||||
|
|
||||||
|
const res = await fetch(embeddingUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ model, prompt: text }),
|
||||||
|
signal: AbortSignal.timeout(30000),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`OllamaAdapter.embed: request failed with HTTP ${res.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = (await res.json()) as OllamaEmbeddingResponse;
|
||||||
|
|
||||||
|
if (!Array.isArray(json.embedding)) {
|
||||||
|
throw new Error('OllamaAdapter.embed: unexpected response — missing embedding array');
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.embedding;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* createCompletion is reserved for future direct-completion use.
|
||||||
|
* The current integration routes completions through Pi SDK's ModelRegistry/AgentSession.
|
||||||
|
*/
|
||||||
|
async *createCompletion(_params: CompletionParams): AsyncIterable<CompletionEvent> {
|
||||||
|
throw new Error(
|
||||||
|
'OllamaAdapter.createCompletion is not yet implemented. ' +
|
||||||
|
'Use Pi SDK AgentSession for completions.',
|
||||||
|
);
|
||||||
|
// Satisfy the AsyncGenerator return type — unreachable but required for TypeScript.
|
||||||
|
yield undefined as never;
|
||||||
|
}
|
||||||
|
}
|
||||||
201
apps/gateway/src/agent/adapters/openai.adapter.ts
Normal file
201
apps/gateway/src/agent/adapters/openai.adapter.ts
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import OpenAI from 'openai';
|
||||||
|
import type { ModelRegistry } from '@mariozechner/pi-coding-agent';
|
||||||
|
import type {
|
||||||
|
CompletionEvent,
|
||||||
|
CompletionParams,
|
||||||
|
IProviderAdapter,
|
||||||
|
ModelInfo,
|
||||||
|
ProviderHealth,
|
||||||
|
} from '@mosaic/types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenAI provider adapter.
|
||||||
|
*
|
||||||
|
* Registers OpenAI models (including Codex gpt-5.4) with the Pi ModelRegistry.
|
||||||
|
* Configuration is driven by environment variables:
|
||||||
|
* OPENAI_API_KEY — OpenAI API key (required; adapter skips registration when absent)
|
||||||
|
*/
|
||||||
|
export class OpenAIAdapter implements IProviderAdapter {
|
||||||
|
readonly name = 'openai';
|
||||||
|
|
||||||
|
private readonly logger = new Logger(OpenAIAdapter.name);
|
||||||
|
private registeredModels: ModelInfo[] = [];
|
||||||
|
private client: OpenAI | null = null;
|
||||||
|
|
||||||
|
/** Model ID used for Codex gpt-5.4 in the Pi registry. */
|
||||||
|
static readonly CODEX_MODEL_ID = 'codex-gpt-5-4';
|
||||||
|
|
||||||
|
constructor(private readonly registry: ModelRegistry) {}
|
||||||
|
|
||||||
|
async register(): Promise<void> {
|
||||||
|
const apiKey = process.env['OPENAI_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
this.logger.debug('Skipping OpenAI provider registration: OPENAI_API_KEY not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.client = new OpenAI({ apiKey });
|
||||||
|
|
||||||
|
const codexModel = {
|
||||||
|
id: OpenAIAdapter.CODEX_MODEL_ID,
|
||||||
|
name: 'Codex gpt-5.4',
|
||||||
|
/** OpenAI-compatible completions API */
|
||||||
|
api: 'openai-completions' as never,
|
||||||
|
reasoning: false,
|
||||||
|
input: ['text', 'image'] as ('text' | 'image')[],
|
||||||
|
cost: { input: 0.003, output: 0.012, cacheRead: 0.0015, cacheWrite: 0 },
|
||||||
|
contextWindow: 128_000,
|
||||||
|
maxTokens: 16_384,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.registry.registerProvider('openai', {
|
||||||
|
apiKey,
|
||||||
|
baseUrl: 'https://api.openai.com/v1',
|
||||||
|
models: [codexModel],
|
||||||
|
});
|
||||||
|
|
||||||
|
this.registeredModels = [
|
||||||
|
{
|
||||||
|
id: OpenAIAdapter.CODEX_MODEL_ID,
|
||||||
|
provider: 'openai',
|
||||||
|
name: 'Codex gpt-5.4',
|
||||||
|
reasoning: false,
|
||||||
|
contextWindow: 128_000,
|
||||||
|
maxTokens: 16_384,
|
||||||
|
inputTypes: ['text', 'image'] as ('text' | 'image')[],
|
||||||
|
cost: { input: 0.003, output: 0.012, cacheRead: 0.0015, cacheWrite: 0 },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
this.logger.log(`OpenAI provider registered with model: ${OpenAIAdapter.CODEX_MODEL_ID}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
listModels(): ModelInfo[] {
|
||||||
|
return this.registeredModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
async healthCheck(): Promise<ProviderHealth> {
|
||||||
|
const apiKey = process.env['OPENAI_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
return {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: 'OPENAI_API_KEY not configured',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
|
try {
|
||||||
|
// Lightweight call — list models to verify key validity
|
||||||
|
const res = await fetch('https://api.openai.com/v1/models', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
|
});
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
return {
|
||||||
|
status: 'degraded',
|
||||||
|
latencyMs,
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: `HTTP ${res.status}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: 'healthy', latencyMs, lastChecked: new Date().toISOString() };
|
||||||
|
} catch (err) {
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
const error = err instanceof Error ? err.message : String(err);
|
||||||
|
return { status: 'down', latencyMs, lastChecked: new Date().toISOString(), error };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream a completion from OpenAI using the chat completions API.
|
||||||
|
*
|
||||||
|
* Maps OpenAI streaming chunks to the Mosaic CompletionEvent format.
|
||||||
|
*/
|
||||||
|
async *createCompletion(params: CompletionParams): AsyncIterable<CompletionEvent> {
|
||||||
|
if (!this.client) {
|
||||||
|
throw new Error(
|
||||||
|
'OpenAIAdapter: client not initialized. ' +
|
||||||
|
'Ensure OPENAI_API_KEY is set and register() was called.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = await this.client.chat.completions.create({
|
||||||
|
model: params.model,
|
||||||
|
messages: params.messages.map((m) => ({
|
||||||
|
role: m.role,
|
||||||
|
content: m.content,
|
||||||
|
})),
|
||||||
|
...(params.temperature !== undefined && { temperature: params.temperature }),
|
||||||
|
...(params.maxTokens !== undefined && { max_tokens: params.maxTokens }),
|
||||||
|
...(params.tools &&
|
||||||
|
params.tools.length > 0 && {
|
||||||
|
tools: params.tools.map((t) => ({
|
||||||
|
type: 'function' as const,
|
||||||
|
function: {
|
||||||
|
name: t.name,
|
||||||
|
description: t.description,
|
||||||
|
parameters: t.parameters,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
}),
|
||||||
|
stream: true,
|
||||||
|
stream_options: { include_usage: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
let inputTokens = 0;
|
||||||
|
let outputTokens = 0;
|
||||||
|
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
const choice = chunk.choices[0];
|
||||||
|
|
||||||
|
// Accumulate usage when present (final chunk with stream_options.include_usage)
|
||||||
|
if (chunk.usage) {
|
||||||
|
inputTokens = chunk.usage.prompt_tokens;
|
||||||
|
outputTokens = chunk.usage.completion_tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!choice) continue;
|
||||||
|
|
||||||
|
const delta = choice.delta;
|
||||||
|
|
||||||
|
// Text content delta
|
||||||
|
if (delta.content) {
|
||||||
|
yield { type: 'text_delta', content: delta.content };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tool call delta — emit when arguments are complete
|
||||||
|
if (delta.tool_calls) {
|
||||||
|
for (const toolCallDelta of delta.tool_calls) {
|
||||||
|
if (toolCallDelta.function?.name && toolCallDelta.function.arguments !== undefined) {
|
||||||
|
yield {
|
||||||
|
type: 'tool_call',
|
||||||
|
name: toolCallDelta.function.name,
|
||||||
|
arguments: toolCallDelta.function.arguments,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream finished
|
||||||
|
if (choice.finish_reason === 'stop' || choice.finish_reason === 'tool_calls') {
|
||||||
|
yield {
|
||||||
|
type: 'done',
|
||||||
|
usage: { inputTokens, outputTokens },
|
||||||
|
};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback done event when stream ends without explicit finish_reason
|
||||||
|
yield { type: 'done', usage: { inputTokens, outputTokens } };
|
||||||
|
}
|
||||||
|
}
|
||||||
212
apps/gateway/src/agent/adapters/openrouter.adapter.ts
Normal file
212
apps/gateway/src/agent/adapters/openrouter.adapter.ts
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
import { Logger } from '@nestjs/common';
|
||||||
|
import OpenAI from 'openai';
|
||||||
|
import type {
|
||||||
|
CompletionEvent,
|
||||||
|
CompletionParams,
|
||||||
|
IProviderAdapter,
|
||||||
|
ModelInfo,
|
||||||
|
ProviderHealth,
|
||||||
|
} from '@mosaic/types';
|
||||||
|
|
||||||
|
const OPENROUTER_BASE_URL = 'https://openrouter.ai/api/v1';
|
||||||
|
|
||||||
|
interface OpenRouterModel {
|
||||||
|
id: string;
|
||||||
|
name?: string;
|
||||||
|
context_length?: number;
|
||||||
|
top_provider?: {
|
||||||
|
max_completion_tokens?: number;
|
||||||
|
};
|
||||||
|
pricing?: {
|
||||||
|
prompt?: string | number;
|
||||||
|
completion?: string | number;
|
||||||
|
};
|
||||||
|
architecture?: {
|
||||||
|
input_modalities?: string[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OpenRouterModelsResponse {
|
||||||
|
data?: OpenRouterModel[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OpenRouter provider adapter.
|
||||||
|
*
|
||||||
|
* Routes completions through OpenRouter's OpenAI-compatible API.
|
||||||
|
* Configuration is driven by the OPENROUTER_API_KEY environment variable.
|
||||||
|
*/
|
||||||
|
export class OpenRouterAdapter implements IProviderAdapter {
|
||||||
|
readonly name = 'openrouter';
|
||||||
|
|
||||||
|
private readonly logger = new Logger(OpenRouterAdapter.name);
|
||||||
|
private client: OpenAI | null = null;
|
||||||
|
private registeredModels: ModelInfo[] = [];
|
||||||
|
|
||||||
|
async register(): Promise<void> {
|
||||||
|
const apiKey = process.env['OPENROUTER_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
this.logger.debug('Skipping OpenRouter provider registration: OPENROUTER_API_KEY not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.client = new OpenAI({
|
||||||
|
apiKey,
|
||||||
|
baseURL: OPENROUTER_BASE_URL,
|
||||||
|
defaultHeaders: {
|
||||||
|
'HTTP-Referer': 'https://mosaic.ai',
|
||||||
|
'X-Title': 'Mosaic',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.registeredModels = await this.fetchModels(apiKey);
|
||||||
|
this.logger.log(`OpenRouter provider registered with ${this.registeredModels.length} models`);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.warn(
|
||||||
|
`OpenRouter model discovery failed: ${err instanceof Error ? err.message : String(err)}. Registering with empty model list.`,
|
||||||
|
);
|
||||||
|
this.registeredModels = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
listModels(): ModelInfo[] {
|
||||||
|
return this.registeredModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
async healthCheck(): Promise<ProviderHealth> {
|
||||||
|
const apiKey = process.env['OPENROUTER_API_KEY'];
|
||||||
|
if (!apiKey) {
|
||||||
|
return {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: 'OPENROUTER_API_KEY not configured',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = Date.now();
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${OPENROUTER_BASE_URL}/models`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
Accept: 'application/json',
|
||||||
|
},
|
||||||
|
signal: AbortSignal.timeout(5000),
|
||||||
|
});
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
return {
|
||||||
|
status: 'degraded',
|
||||||
|
latencyMs,
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: `HTTP ${res.status}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status: 'healthy', latencyMs, lastChecked: new Date().toISOString() };
|
||||||
|
} catch (err) {
|
||||||
|
const latencyMs = Date.now() - start;
|
||||||
|
const error = err instanceof Error ? err.message : String(err);
|
||||||
|
return { status: 'down', latencyMs, lastChecked: new Date().toISOString(), error };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream a completion through OpenRouter's OpenAI-compatible API.
|
||||||
|
*/
|
||||||
|
async *createCompletion(params: CompletionParams): AsyncIterable<CompletionEvent> {
|
||||||
|
if (!this.client) {
|
||||||
|
throw new Error('OpenRouterAdapter is not initialized. Ensure OPENROUTER_API_KEY is set.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = await this.client.chat.completions.create({
|
||||||
|
model: params.model,
|
||||||
|
messages: params.messages.map((m) => ({ role: m.role, content: m.content })),
|
||||||
|
temperature: params.temperature,
|
||||||
|
max_tokens: params.maxTokens,
|
||||||
|
stream: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
let inputTokens = 0;
|
||||||
|
let outputTokens = 0;
|
||||||
|
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
const choice = chunk.choices[0];
|
||||||
|
if (!choice) continue;
|
||||||
|
|
||||||
|
const delta = choice.delta;
|
||||||
|
|
||||||
|
if (delta.content) {
|
||||||
|
yield { type: 'text_delta', content: delta.content };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (choice.finish_reason === 'stop') {
|
||||||
|
const usage = (chunk as { usage?: { prompt_tokens?: number; completion_tokens?: number } })
|
||||||
|
.usage;
|
||||||
|
if (usage) {
|
||||||
|
inputTokens = usage.prompt_tokens ?? 0;
|
||||||
|
outputTokens = usage.completion_tokens ?? 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield {
|
||||||
|
type: 'done',
|
||||||
|
usage: { inputTokens, outputTokens },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Private helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private async fetchModels(apiKey: string): Promise<ModelInfo[]> {
|
||||||
|
const res = await fetch(`${OPENROUTER_BASE_URL}/models`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
Accept: 'application/json',
|
||||||
|
},
|
||||||
|
signal: AbortSignal.timeout(10000),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`OpenRouter models endpoint returned HTTP ${res.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = (await res.json()) as OpenRouterModelsResponse;
|
||||||
|
const data = json.data ?? [];
|
||||||
|
|
||||||
|
return data.map((model): ModelInfo => {
|
||||||
|
const inputPrice = model.pricing?.prompt
|
||||||
|
? parseFloat(String(model.pricing.prompt)) * 1000
|
||||||
|
: 0;
|
||||||
|
const outputPrice = model.pricing?.completion
|
||||||
|
? parseFloat(String(model.pricing.completion)) * 1000
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
const inputModalities = model.architecture?.input_modalities ?? ['text'];
|
||||||
|
const inputTypes = inputModalities.includes('image')
|
||||||
|
? (['text', 'image'] as const)
|
||||||
|
: (['text'] as const);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: model.id,
|
||||||
|
provider: 'openrouter',
|
||||||
|
name: model.name ?? model.id,
|
||||||
|
reasoning: false,
|
||||||
|
contextWindow: model.context_length ?? 4096,
|
||||||
|
maxTokens: model.top_provider?.max_completion_tokens ?? 4096,
|
||||||
|
inputTypes: [...inputTypes],
|
||||||
|
cost: {
|
||||||
|
input: inputPrice,
|
||||||
|
output: outputPrice,
|
||||||
|
cacheRead: 0,
|
||||||
|
cacheWrite: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,11 @@ export class AgentConfigsController {
|
|||||||
if (!agent.isSystem && agent.ownerId !== user.id) {
|
if (!agent.isSystem && agent.ownerId !== user.id) {
|
||||||
throw new ForbiddenException('Agent does not belong to the current user');
|
throw new ForbiddenException('Agent does not belong to the current user');
|
||||||
}
|
}
|
||||||
const updated = await this.brain.agents.update(id, dto);
|
|
||||||
|
// Pass ownerId for user agents so the repo WHERE clause enforces ownership.
|
||||||
|
// For system agents (admin path) pass undefined so the WHERE matches only on id.
|
||||||
|
const ownerId = agent.isSystem ? undefined : user.id;
|
||||||
|
const updated = await this.brain.agents.update(id, dto, ownerId);
|
||||||
if (!updated) throw new NotFoundException('Agent not found');
|
if (!updated) throw new NotFoundException('Agent not found');
|
||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
@@ -78,7 +82,8 @@ export class AgentConfigsController {
|
|||||||
if (agent.ownerId !== user.id) {
|
if (agent.ownerId !== user.id) {
|
||||||
throw new ForbiddenException('Agent does not belong to the current user');
|
throw new ForbiddenException('Agent does not belong to the current user');
|
||||||
}
|
}
|
||||||
const deleted = await this.brain.agents.remove(id);
|
// Pass ownerId so the repo WHERE clause enforces ownership at the DB level.
|
||||||
|
const deleted = await this.brain.agents.remove(id, user.id);
|
||||||
if (!deleted) throw new NotFoundException('Agent not found');
|
if (!deleted) throw new NotFoundException('Agent not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,13 @@ import { SystemOverrideService } from '../preferences/system-override.service.js
|
|||||||
import { PreferencesService } from '../preferences/preferences.service.js';
|
import { PreferencesService } from '../preferences/preferences.service.js';
|
||||||
import { SessionGCService } from '../gc/session-gc.service.js';
|
import { SessionGCService } from '../gc/session-gc.service.js';
|
||||||
|
|
||||||
|
/** A single message from DB conversation history, used for context injection. */
|
||||||
|
export interface ConversationHistoryMessage {
|
||||||
|
role: 'user' | 'assistant' | 'system';
|
||||||
|
content: string;
|
||||||
|
createdAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export interface AgentSessionOptions {
|
export interface AgentSessionOptions {
|
||||||
provider?: string;
|
provider?: string;
|
||||||
modelId?: string;
|
modelId?: string;
|
||||||
@@ -60,6 +67,12 @@ export interface AgentSessionOptions {
|
|||||||
agentConfigId?: string;
|
agentConfigId?: string;
|
||||||
/** ID of the user who owns this session. Used for preferences and system override lookups. */
|
/** ID of the user who owns this session. Used for preferences and system override lookups. */
|
||||||
userId?: string;
|
userId?: string;
|
||||||
|
/**
|
||||||
|
* Prior conversation messages to inject as context when resuming a session.
|
||||||
|
* These messages are formatted and prepended to the system prompt so the
|
||||||
|
* agent is aware of what was discussed in previous sessions.
|
||||||
|
*/
|
||||||
|
conversationHistory?: ConversationHistoryMessage[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AgentSession {
|
export interface AgentSession {
|
||||||
@@ -106,17 +119,22 @@ export class AgentService implements OnModuleDestroy {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the full set of custom tools scoped to the given sandbox directory.
|
* Build the full set of custom tools scoped to the given sandbox directory and session user.
|
||||||
* Brain/coord/memory/web tools are stateless with respect to cwd; file/git/shell
|
* Brain/coord/memory/web tools are stateless with respect to cwd; file/git/shell
|
||||||
* tools receive the resolved sandboxDir so they operate within the sandbox.
|
* tools receive the resolved sandboxDir so they operate within the sandbox.
|
||||||
|
* Memory tools are bound to sessionUserId so the LLM cannot access another user's data.
|
||||||
*/
|
*/
|
||||||
private buildToolsForSandbox(sandboxDir: string): ToolDefinition[] {
|
private buildToolsForSandbox(
|
||||||
|
sandboxDir: string,
|
||||||
|
sessionUserId: string | undefined,
|
||||||
|
): ToolDefinition[] {
|
||||||
return [
|
return [
|
||||||
...createBrainTools(this.brain),
|
...createBrainTools(this.brain),
|
||||||
...createCoordTools(this.coordService),
|
...createCoordTools(this.coordService),
|
||||||
...createMemoryTools(
|
...createMemoryTools(
|
||||||
this.memory,
|
this.memory,
|
||||||
this.embeddingService.available ? this.embeddingService : null,
|
this.embeddingService.available ? this.embeddingService : null,
|
||||||
|
sessionUserId,
|
||||||
),
|
),
|
||||||
...createFileTools(sandboxDir),
|
...createFileTools(sandboxDir),
|
||||||
...createGitTools(sandboxDir),
|
...createGitTools(sandboxDir),
|
||||||
@@ -216,8 +234,8 @@ export class AgentService implements OnModuleDestroy {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build per-session tools scoped to the sandbox directory
|
// Build per-session tools scoped to the sandbox directory and authenticated user
|
||||||
const sandboxTools = this.buildToolsForSandbox(sandboxDir);
|
const sandboxTools = this.buildToolsForSandbox(sandboxDir, mergedOptions?.userId);
|
||||||
|
|
||||||
// Combine static tools with dynamically discovered MCP client tools and skill tools
|
// Combine static tools with dynamically discovered MCP client tools and skill tools
|
||||||
const mcpTools = this.mcpClientService.getToolDefinitions();
|
const mcpTools = this.mcpClientService.getToolDefinitions();
|
||||||
@@ -239,8 +257,20 @@ export class AgentService implements OnModuleDestroy {
|
|||||||
// Build system prompt: platform prompt + skill additions appended
|
// Build system prompt: platform prompt + skill additions appended
|
||||||
const platformPrompt =
|
const platformPrompt =
|
||||||
mergedOptions?.systemPrompt ?? process.env['AGENT_SYSTEM_PROMPT'] ?? undefined;
|
mergedOptions?.systemPrompt ?? process.env['AGENT_SYSTEM_PROMPT'] ?? undefined;
|
||||||
const appendSystemPrompt =
|
|
||||||
promptAdditions.length > 0 ? promptAdditions.join('\n\n') : undefined;
|
// Format conversation history for context injection (M1-004 / M1-005)
|
||||||
|
const historyPromptSection = mergedOptions?.conversationHistory?.length
|
||||||
|
? this.buildHistoryPromptSection(
|
||||||
|
mergedOptions.conversationHistory,
|
||||||
|
model?.contextWindow ?? 8192,
|
||||||
|
sessionId,
|
||||||
|
)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const appendParts: string[] = [];
|
||||||
|
if (promptAdditions.length > 0) appendParts.push(promptAdditions.join('\n\n'));
|
||||||
|
if (historyPromptSection) appendParts.push(historyPromptSection);
|
||||||
|
const appendSystemPrompt = appendParts.length > 0 ? appendParts.join('\n\n') : undefined;
|
||||||
|
|
||||||
// Construct a resource loader that injects the configured system prompt
|
// Construct a resource loader that injects the configured system prompt
|
||||||
const resourceLoader = new DefaultResourceLoader({
|
const resourceLoader = new DefaultResourceLoader({
|
||||||
@@ -308,6 +338,92 @@ export class AgentService implements OnModuleDestroy {
|
|||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate token count for a string using a rough 4-chars-per-token heuristic.
|
||||||
|
*/
|
||||||
|
private estimateTokens(text: string): number {
|
||||||
|
return Math.ceil(text.length / 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a conversation history section for injection into the system prompt.
|
||||||
|
* Implements M1-004 (history loading) and M1-005 (context window management).
|
||||||
|
*
|
||||||
|
* - Formats messages as a readable conversation transcript.
|
||||||
|
* - If the full history exceeds 80% of the model's context window, older messages
|
||||||
|
* are summarized and only the most recent messages are kept verbatim.
|
||||||
|
* - Summarization is a simple extractive approach (no LLM required).
|
||||||
|
*/
|
||||||
|
private buildHistoryPromptSection(
|
||||||
|
history: ConversationHistoryMessage[],
|
||||||
|
contextWindow: number,
|
||||||
|
sessionId: string,
|
||||||
|
): string {
|
||||||
|
const TOKEN_BUDGET = Math.floor(contextWindow * 0.8);
|
||||||
|
const HISTORY_HEADER = '## Conversation History (resumed session)\n\n';
|
||||||
|
|
||||||
|
const formatMessage = (msg: ConversationHistoryMessage): string => {
|
||||||
|
const roleLabel =
|
||||||
|
msg.role === 'user' ? 'User' : msg.role === 'assistant' ? 'Assistant' : 'System';
|
||||||
|
return `**${roleLabel}:** ${msg.content}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatted = history.map((msg) => formatMessage(msg));
|
||||||
|
const fullHistory = formatted.join('\n\n');
|
||||||
|
const fullTokens = this.estimateTokens(HISTORY_HEADER + fullHistory);
|
||||||
|
|
||||||
|
if (fullTokens <= TOKEN_BUDGET) {
|
||||||
|
this.logger.debug(
|
||||||
|
`Session ${sessionId}: injecting full history (${history.length} msgs, ~${fullTokens} tokens)`,
|
||||||
|
);
|
||||||
|
return HISTORY_HEADER + fullHistory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// History exceeds budget — summarize oldest messages, keep recent verbatim
|
||||||
|
this.logger.log(
|
||||||
|
`Session ${sessionId}: history (~${fullTokens} tokens) exceeds ${TOKEN_BUDGET} token budget; summarizing oldest messages`,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reserve 20% of the budget for the summary prefix, rest for verbatim messages
|
||||||
|
const SUMMARY_RESERVE = Math.floor(TOKEN_BUDGET * 0.2);
|
||||||
|
const verbatimBudget = TOKEN_BUDGET - SUMMARY_RESERVE;
|
||||||
|
|
||||||
|
let verbatimTokens = 0;
|
||||||
|
let verbatimCutIndex = history.length;
|
||||||
|
for (let i = history.length - 1; i >= 0; i--) {
|
||||||
|
const t = this.estimateTokens(formatted[i]!);
|
||||||
|
if (verbatimTokens + t > verbatimBudget) break;
|
||||||
|
verbatimTokens += t;
|
||||||
|
verbatimCutIndex = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
const summarizedMessages = history.slice(0, verbatimCutIndex);
|
||||||
|
const verbatimMessages = history.slice(verbatimCutIndex);
|
||||||
|
|
||||||
|
let summaryText = '';
|
||||||
|
if (summarizedMessages.length > 0) {
|
||||||
|
const topics = summarizedMessages
|
||||||
|
.filter((m) => m.role === 'user')
|
||||||
|
.map((m) => m.content.slice(0, 120).replace(/\n/g, ' '))
|
||||||
|
.join('; ');
|
||||||
|
summaryText =
|
||||||
|
`**Previous conversation summary** (${summarizedMessages.length} messages omitted for brevity):\n` +
|
||||||
|
`Topics discussed: ${topics || '(no user messages in summarized portion)'}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const verbatimSection = verbatimMessages.map((m) => formatMessage(m)).join('\n\n');
|
||||||
|
|
||||||
|
const parts: string[] = [HISTORY_HEADER];
|
||||||
|
if (summaryText) parts.push(summaryText);
|
||||||
|
if (verbatimSection) parts.push(verbatimSection);
|
||||||
|
|
||||||
|
const result = parts.join('\n\n');
|
||||||
|
this.logger.log(
|
||||||
|
`Session ${sessionId}: summarized ${summarizedMessages.length} messages, kept ${verbatimMessages.length} verbatim (~${this.estimateTokens(result)} tokens)`,
|
||||||
|
);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private resolveModel(options?: AgentSessionOptions) {
|
private resolveModel(options?: AgentSessionOptions) {
|
||||||
if (!options?.provider && !options?.modelId) {
|
if (!options?.provider && !options?.modelId) {
|
||||||
return this.providerService.getDefaultModel() ?? null;
|
return this.providerService.getDefaultModel() ?? null;
|
||||||
|
|||||||
204
apps/gateway/src/agent/model-capabilities.ts
Normal file
204
apps/gateway/src/agent/model-capabilities.ts
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
import type { ModelCapability } from '@mosaic/types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comprehensive capability matrix for all target models.
|
||||||
|
* Cost fields are optional and will be filled in when real pricing data is available.
|
||||||
|
*/
|
||||||
|
export const MODEL_CAPABILITIES: ModelCapability[] = [
|
||||||
|
{
|
||||||
|
id: 'claude-opus-4-6',
|
||||||
|
provider: 'anthropic',
|
||||||
|
displayName: 'Claude Opus 4.6',
|
||||||
|
tier: 'premium',
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxOutputTokens: 32000,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: true,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'claude-sonnet-4-6',
|
||||||
|
provider: 'anthropic',
|
||||||
|
displayName: 'Claude Sonnet 4.6',
|
||||||
|
tier: 'standard',
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxOutputTokens: 16000,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: true,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'claude-haiku-4-5',
|
||||||
|
provider: 'anthropic',
|
||||||
|
displayName: 'Claude Haiku 4.5',
|
||||||
|
tier: 'cheap',
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxOutputTokens: 8192,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'codex-gpt-5.4',
|
||||||
|
provider: 'openai',
|
||||||
|
displayName: 'Codex gpt-5.4',
|
||||||
|
tier: 'premium',
|
||||||
|
contextWindow: 128000,
|
||||||
|
maxOutputTokens: 16384,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: true,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'glm-5',
|
||||||
|
provider: 'zai',
|
||||||
|
displayName: 'GLM-5',
|
||||||
|
tier: 'standard',
|
||||||
|
contextWindow: 128000,
|
||||||
|
maxOutputTokens: 8192,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: false,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'llama3.2',
|
||||||
|
provider: 'ollama',
|
||||||
|
displayName: 'llama3.2',
|
||||||
|
tier: 'local',
|
||||||
|
contextWindow: 128000,
|
||||||
|
maxOutputTokens: 8192,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: false,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'codellama',
|
||||||
|
provider: 'ollama',
|
||||||
|
displayName: 'codellama',
|
||||||
|
tier: 'local',
|
||||||
|
contextWindow: 16000,
|
||||||
|
maxOutputTokens: 4096,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: false,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'mistral',
|
||||||
|
provider: 'ollama',
|
||||||
|
displayName: 'mistral',
|
||||||
|
tier: 'local',
|
||||||
|
contextWindow: 32000,
|
||||||
|
maxOutputTokens: 8192,
|
||||||
|
capabilities: {
|
||||||
|
tools: true,
|
||||||
|
vision: false,
|
||||||
|
streaming: true,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'nomic-embed-text',
|
||||||
|
provider: 'ollama',
|
||||||
|
displayName: 'nomic-embed-text',
|
||||||
|
tier: 'local',
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxOutputTokens: 0,
|
||||||
|
capabilities: {
|
||||||
|
tools: false,
|
||||||
|
vision: false,
|
||||||
|
streaming: false,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'mxbai-embed-large',
|
||||||
|
provider: 'ollama',
|
||||||
|
displayName: 'mxbai-embed-large',
|
||||||
|
tier: 'local',
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxOutputTokens: 0,
|
||||||
|
capabilities: {
|
||||||
|
tools: false,
|
||||||
|
vision: false,
|
||||||
|
streaming: false,
|
||||||
|
reasoning: false,
|
||||||
|
embedding: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up a model by its ID.
|
||||||
|
* Returns undefined if the model is not found.
|
||||||
|
*/
|
||||||
|
export function getModelCapability(modelId: string): ModelCapability | undefined {
|
||||||
|
return MODEL_CAPABILITIES.find((m) => m.id === modelId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find models matching a partial capability filter.
|
||||||
|
* All provided filter keys must match for a model to be included.
|
||||||
|
*/
|
||||||
|
export function findModelsByCapability(
|
||||||
|
filter: Partial<Pick<ModelCapability, 'tier' | 'provider'>> & {
|
||||||
|
capabilities?: Partial<ModelCapability['capabilities']>;
|
||||||
|
},
|
||||||
|
): ModelCapability[] {
|
||||||
|
return MODEL_CAPABILITIES.filter((model) => {
|
||||||
|
if (filter.tier !== undefined && model.tier !== filter.tier) return false;
|
||||||
|
if (filter.provider !== undefined && model.provider !== filter.provider) return false;
|
||||||
|
if (filter.capabilities) {
|
||||||
|
for (const [key, value] of Object.entries(filter.capabilities) as [
|
||||||
|
keyof ModelCapability['capabilities'],
|
||||||
|
boolean,
|
||||||
|
][]) {
|
||||||
|
if (model.capabilities[key] !== value) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all models for a specific provider.
|
||||||
|
*/
|
||||||
|
export function getModelsByProvider(provider: string): ModelCapability[] {
|
||||||
|
return MODEL_CAPABILITIES.filter((m) => m.provider === provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the full list of all known models.
|
||||||
|
*/
|
||||||
|
export function getAllModels(): ModelCapability[] {
|
||||||
|
return MODEL_CAPABILITIES;
|
||||||
|
}
|
||||||
@@ -1,25 +1,212 @@
|
|||||||
import { Injectable, Logger, type OnModuleInit } from '@nestjs/common';
|
import { Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from '@nestjs/common';
|
||||||
import { ModelRegistry, AuthStorage } from '@mariozechner/pi-coding-agent';
|
import { ModelRegistry, AuthStorage } from '@mariozechner/pi-coding-agent';
|
||||||
import type { Model, Api } from '@mariozechner/pi-ai';
|
import { getModel, type Model, type Api } from '@mariozechner/pi-ai';
|
||||||
import type { ModelInfo, ProviderInfo, CustomProviderConfig } from '@mosaic/types';
|
import type {
|
||||||
|
CustomProviderConfig,
|
||||||
|
IProviderAdapter,
|
||||||
|
ModelInfo,
|
||||||
|
ProviderHealth,
|
||||||
|
ProviderInfo,
|
||||||
|
} from '@mosaic/types';
|
||||||
|
import {
|
||||||
|
AnthropicAdapter,
|
||||||
|
OllamaAdapter,
|
||||||
|
OpenAIAdapter,
|
||||||
|
OpenRouterAdapter,
|
||||||
|
} from './adapters/index.js';
|
||||||
import type { TestConnectionResultDto } from './provider.dto.js';
|
import type { TestConnectionResultDto } from './provider.dto.js';
|
||||||
|
|
||||||
|
/** Default health check interval in seconds */
|
||||||
|
const DEFAULT_HEALTH_INTERVAL_SECS = 60;
|
||||||
|
|
||||||
|
/** DI injection token for the provider adapter array. */
|
||||||
|
export const PROVIDER_ADAPTERS = Symbol('PROVIDER_ADAPTERS');
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProviderService implements OnModuleInit {
|
export class ProviderService implements OnModuleInit, OnModuleDestroy {
|
||||||
private readonly logger = new Logger(ProviderService.name);
|
private readonly logger = new Logger(ProviderService.name);
|
||||||
private registry!: ModelRegistry;
|
private registry!: ModelRegistry;
|
||||||
|
|
||||||
onModuleInit(): void {
|
/**
|
||||||
|
* Adapters registered with this service.
|
||||||
|
* Built-in adapters (Ollama) are always present; additional adapters can be
|
||||||
|
* supplied via the PROVIDER_ADAPTERS injection token in the future.
|
||||||
|
*/
|
||||||
|
private adapters: IProviderAdapter[] = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached health status per provider, updated by the health check scheduler.
|
||||||
|
*/
|
||||||
|
private healthCache: Map<string, ProviderHealth & { modelCount: number }> = new Map();
|
||||||
|
|
||||||
|
/** Timer handle for the periodic health check scheduler */
|
||||||
|
private healthCheckTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
|
async onModuleInit(): Promise<void> {
|
||||||
const authStorage = AuthStorage.inMemory();
|
const authStorage = AuthStorage.inMemory();
|
||||||
this.registry = new ModelRegistry(authStorage);
|
this.registry = new ModelRegistry(authStorage);
|
||||||
|
|
||||||
this.registerOllamaProvider();
|
// Build the default set of adapters that rely on the registry
|
||||||
|
this.adapters = [
|
||||||
|
new OllamaAdapter(this.registry),
|
||||||
|
new AnthropicAdapter(this.registry),
|
||||||
|
new OpenAIAdapter(this.registry),
|
||||||
|
new OpenRouterAdapter(),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Run all adapter registrations first (Ollama, Anthropic, and any future adapters)
|
||||||
|
await this.registerAll();
|
||||||
|
|
||||||
|
// Register API-key providers directly (Z.ai, custom)
|
||||||
|
// OpenAI now has a dedicated adapter (M3-003).
|
||||||
|
this.registerZaiProvider();
|
||||||
this.registerCustomProviders();
|
this.registerCustomProviders();
|
||||||
|
|
||||||
const available = this.registry.getAvailable();
|
const available = this.registry.getAvailable();
|
||||||
this.logger.log(`Providers initialized: ${available.length} models available`);
|
this.logger.log(`Providers initialized: ${available.length} models available`);
|
||||||
|
|
||||||
|
// Kick off the health check scheduler
|
||||||
|
this.startHealthCheckScheduler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onModuleDestroy(): void {
|
||||||
|
if (this.healthCheckTimer !== null) {
|
||||||
|
clearInterval(this.healthCheckTimer);
|
||||||
|
this.healthCheckTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Health check scheduler
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start periodic health checks on all adapters.
|
||||||
|
* Interval is configurable via PROVIDER_HEALTH_INTERVAL env (seconds, default 60).
|
||||||
|
*/
|
||||||
|
private startHealthCheckScheduler(): void {
|
||||||
|
const intervalSecs =
|
||||||
|
parseInt(process.env['PROVIDER_HEALTH_INTERVAL'] ?? '', 10) || DEFAULT_HEALTH_INTERVAL_SECS;
|
||||||
|
const intervalMs = intervalSecs * 1000;
|
||||||
|
|
||||||
|
// Run an initial check immediately (non-blocking)
|
||||||
|
void this.runScheduledHealthChecks();
|
||||||
|
|
||||||
|
this.healthCheckTimer = setInterval(() => {
|
||||||
|
void this.runScheduledHealthChecks();
|
||||||
|
}, intervalMs);
|
||||||
|
|
||||||
|
this.logger.log(`Provider health check scheduler started (interval: ${intervalSecs}s)`);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async runScheduledHealthChecks(): Promise<void> {
|
||||||
|
for (const adapter of this.adapters) {
|
||||||
|
try {
|
||||||
|
const health = await adapter.healthCheck();
|
||||||
|
const modelCount = adapter.listModels().length;
|
||||||
|
this.healthCache.set(adapter.name, { ...health, modelCount });
|
||||||
|
this.logger.debug(
|
||||||
|
`Health check [${adapter.name}]: ${health.status} (${health.latencyMs ?? 'n/a'}ms)`,
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
const modelCount = adapter.listModels().length;
|
||||||
|
this.healthCache.set(adapter.name, {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
modelCount,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the cached health status for all adapters.
|
||||||
|
* Format: array of { name, status, latencyMs, lastChecked, modelCount }
|
||||||
|
*/
|
||||||
|
getProvidersHealth(): Array<{
|
||||||
|
name: string;
|
||||||
|
status: string;
|
||||||
|
latencyMs?: number;
|
||||||
|
lastChecked: string;
|
||||||
|
modelCount: number;
|
||||||
|
error?: string;
|
||||||
|
}> {
|
||||||
|
return this.adapters.map((adapter) => {
|
||||||
|
const cached = this.healthCache.get(adapter.name);
|
||||||
|
if (cached) {
|
||||||
|
return {
|
||||||
|
name: adapter.name,
|
||||||
|
status: cached.status,
|
||||||
|
latencyMs: cached.latencyMs,
|
||||||
|
lastChecked: cached.lastChecked,
|
||||||
|
modelCount: cached.modelCount,
|
||||||
|
error: cached.error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// Not yet checked — return a pending placeholder
|
||||||
|
return {
|
||||||
|
name: adapter.name,
|
||||||
|
status: 'unknown',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
modelCount: adapter.listModels().length,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Adapter-pattern API
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call register() on each adapter in order.
|
||||||
|
* Errors from individual adapters are logged and do not abort the others.
|
||||||
|
*/
|
||||||
|
async registerAll(): Promise<void> {
|
||||||
|
for (const adapter of this.adapters) {
|
||||||
|
try {
|
||||||
|
await adapter.register();
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`Adapter "${adapter.name}" registration failed`,
|
||||||
|
err instanceof Error ? err.stack : String(err),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the adapter registered under the given provider name, or undefined.
|
||||||
|
*/
|
||||||
|
getAdapter(providerName: string): IProviderAdapter | undefined {
|
||||||
|
return this.adapters.find((a) => a.name === providerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run healthCheck() on all adapters and return results keyed by provider name.
|
||||||
|
*/
|
||||||
|
async healthCheckAll(): Promise<Record<string, ProviderHealth>> {
|
||||||
|
const results: Record<string, ProviderHealth> = {};
|
||||||
|
await Promise.all(
|
||||||
|
this.adapters.map(async (adapter) => {
|
||||||
|
try {
|
||||||
|
results[adapter.name] = await adapter.healthCheck();
|
||||||
|
} catch (err) {
|
||||||
|
results[adapter.name] = {
|
||||||
|
status: 'down',
|
||||||
|
lastChecked: new Date().toISOString(),
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Legacy / Pi-SDK-facing API (preserved for AgentService and RoutingService)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
getRegistry(): ModelRegistry {
|
getRegistry(): ModelRegistry {
|
||||||
return this.registry;
|
return this.registry;
|
||||||
}
|
}
|
||||||
@@ -66,6 +253,18 @@ export class ProviderService implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async testConnection(providerId: string, baseUrl?: string): Promise<TestConnectionResultDto> {
|
async testConnection(providerId: string, baseUrl?: string): Promise<TestConnectionResultDto> {
|
||||||
|
// Delegate to the adapter when one exists and no URL override is given
|
||||||
|
const adapter = this.getAdapter(providerId);
|
||||||
|
if (adapter && !baseUrl) {
|
||||||
|
const health = await adapter.healthCheck();
|
||||||
|
return {
|
||||||
|
providerId,
|
||||||
|
reachable: health.status !== 'down',
|
||||||
|
latencyMs: health.latencyMs,
|
||||||
|
error: health.error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve baseUrl: explicit override > registered provider > ollama env
|
// Resolve baseUrl: explicit override > registered provider > ollama env
|
||||||
let resolvedUrl = baseUrl;
|
let resolvedUrl = baseUrl;
|
||||||
|
|
||||||
@@ -140,34 +339,29 @@ export class ProviderService implements OnModuleInit {
|
|||||||
this.logger.log(`Registered custom provider: ${config.id} (${config.models.length} models)`);
|
this.logger.log(`Registered custom provider: ${config.id} (${config.models.length} models)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerOllamaProvider(): void {
|
// ---------------------------------------------------------------------------
|
||||||
const ollamaUrl = process.env['OLLAMA_BASE_URL'] ?? process.env['OLLAMA_HOST'];
|
// Private helpers — direct registry registration for providers without adapters yet
|
||||||
if (!ollamaUrl) return;
|
// (Z.ai will move to an adapter in M3-005)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const modelsEnv = process.env['OLLAMA_MODELS'] ?? 'llama3.2,codellama,mistral';
|
private registerZaiProvider(): void {
|
||||||
const modelIds = modelsEnv
|
const apiKey = process.env['ZAI_API_KEY'];
|
||||||
.split(',')
|
if (!apiKey) {
|
||||||
.map((modelId: string) => modelId.trim())
|
this.logger.debug('Skipping Z.ai provider registration: ZAI_API_KEY not set');
|
||||||
.filter(Boolean);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.registry.registerProvider('ollama', {
|
const models = ['glm-4.5', 'glm-4.5-air', 'glm-4.5-flash'].map((id) =>
|
||||||
baseUrl: `${ollamaUrl}/v1`,
|
this.cloneBuiltInModel('zai', id),
|
||||||
apiKey: 'ollama',
|
);
|
||||||
api: 'openai-completions' as never,
|
|
||||||
models: modelIds.map((id) => ({
|
this.registry.registerProvider('zai', {
|
||||||
id,
|
apiKey,
|
||||||
name: id,
|
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
|
||||||
reasoning: false,
|
models,
|
||||||
input: ['text'] as ('text' | 'image')[],
|
|
||||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
||||||
contextWindow: 8192,
|
|
||||||
maxTokens: 4096,
|
|
||||||
})),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.logger.log(
|
this.logger.log('Z.ai provider registered with 3 models');
|
||||||
`Ollama provider registered at ${ollamaUrl} with models: ${modelIds.join(', ')}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerCustomProviders(): void {
|
private registerCustomProviders(): void {
|
||||||
@@ -184,6 +378,19 @@ export class ProviderService implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private cloneBuiltInModel(
|
||||||
|
provider: string,
|
||||||
|
modelId: string,
|
||||||
|
overrides: Partial<Model<Api>> = {},
|
||||||
|
): Model<Api> {
|
||||||
|
const model = getModel(provider as never, modelId as never) as Model<Api> | undefined;
|
||||||
|
if (!model) {
|
||||||
|
throw new Error(`Built-in model not found: ${provider}:${modelId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...model, ...overrides };
|
||||||
|
}
|
||||||
|
|
||||||
private toModelInfo(model: Model<Api>): ModelInfo {
|
private toModelInfo(model: Model<Api>): ModelInfo {
|
||||||
return {
|
return {
|
||||||
id: model.id,
|
id: model.id,
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ export class ProvidersController {
|
|||||||
return this.providerService.listAvailableModels();
|
return this.providerService.listAvailableModels();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('health')
|
||||||
|
health() {
|
||||||
|
return { providers: this.providerService.getProvidersHealth() };
|
||||||
|
}
|
||||||
|
|
||||||
@Post('test')
|
@Post('test')
|
||||||
testConnection(@Body() body: TestConnectionDto): Promise<TestConnectionResultDto> {
|
testConnection(@Body() body: TestConnectionDto): Promise<TestConnectionResultDto> {
|
||||||
return this.providerService.testConnection(body.providerId, body.baseUrl);
|
return this.providerService.testConnection(body.providerId, body.baseUrl);
|
||||||
|
|||||||
@@ -3,23 +3,45 @@ import type { ToolDefinition } from '@mariozechner/pi-coding-agent';
|
|||||||
import type { Memory } from '@mosaic/memory';
|
import type { Memory } from '@mosaic/memory';
|
||||||
import type { EmbeddingProvider } from '@mosaic/memory';
|
import type { EmbeddingProvider } from '@mosaic/memory';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create memory tools bound to the session's authenticated userId.
|
||||||
|
*
|
||||||
|
* SECURITY: userId is resolved from the authenticated session at tool-creation
|
||||||
|
* time and is never accepted as a user-supplied or LLM-supplied parameter.
|
||||||
|
* This prevents cross-user data access via parameter injection.
|
||||||
|
*/
|
||||||
export function createMemoryTools(
|
export function createMemoryTools(
|
||||||
memory: Memory,
|
memory: Memory,
|
||||||
embeddingProvider: EmbeddingProvider | null,
|
embeddingProvider: EmbeddingProvider | null,
|
||||||
|
/** Authenticated user ID from the session. All memory operations are scoped to this user. */
|
||||||
|
sessionUserId: string | undefined,
|
||||||
): ToolDefinition[] {
|
): ToolDefinition[] {
|
||||||
|
/** Return an error result when no session user is bound. */
|
||||||
|
function noUserError() {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'text' as const,
|
||||||
|
text: 'Memory tools unavailable — no authenticated user bound to this session',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
details: undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const searchMemory: ToolDefinition = {
|
const searchMemory: ToolDefinition = {
|
||||||
name: 'memory_search',
|
name: 'memory_search',
|
||||||
label: 'Search Memory',
|
label: 'Search Memory',
|
||||||
description:
|
description:
|
||||||
'Search across stored insights and knowledge using natural language. Returns semantically similar results.',
|
'Search across stored insights and knowledge using natural language. Returns semantically similar results.',
|
||||||
parameters: Type.Object({
|
parameters: Type.Object({
|
||||||
userId: Type.String({ description: 'User ID to search memory for' }),
|
|
||||||
query: Type.String({ description: 'Natural language search query' }),
|
query: Type.String({ description: 'Natural language search query' }),
|
||||||
limit: Type.Optional(Type.Number({ description: 'Max results (default 5)' })),
|
limit: Type.Optional(Type.Number({ description: 'Max results (default 5)' })),
|
||||||
}),
|
}),
|
||||||
async execute(_toolCallId, params) {
|
async execute(_toolCallId, params) {
|
||||||
const { userId, query, limit } = params as {
|
if (!sessionUserId) return noUserError();
|
||||||
userId: string;
|
|
||||||
|
const { query, limit } = params as {
|
||||||
query: string;
|
query: string;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
};
|
};
|
||||||
@@ -37,7 +59,7 @@ export function createMemoryTools(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const embedding = await embeddingProvider.embed(query);
|
const embedding = await embeddingProvider.embed(query);
|
||||||
const results = await memory.insights.searchByEmbedding(userId, embedding, limit ?? 5);
|
const results = await memory.insights.searchByEmbedding(sessionUserId, embedding, limit ?? 5);
|
||||||
return {
|
return {
|
||||||
content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
|
content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
|
||||||
details: undefined,
|
details: undefined,
|
||||||
@@ -48,9 +70,8 @@ export function createMemoryTools(
|
|||||||
const getPreferences: ToolDefinition = {
|
const getPreferences: ToolDefinition = {
|
||||||
name: 'memory_get_preferences',
|
name: 'memory_get_preferences',
|
||||||
label: 'Get User Preferences',
|
label: 'Get User Preferences',
|
||||||
description: 'Retrieve stored preferences for a user.',
|
description: 'Retrieve stored preferences for the current session user.',
|
||||||
parameters: Type.Object({
|
parameters: Type.Object({
|
||||||
userId: Type.String({ description: 'User ID' }),
|
|
||||||
category: Type.Optional(
|
category: Type.Optional(
|
||||||
Type.String({
|
Type.String({
|
||||||
description: 'Filter by category: communication, coding, workflow, appearance, general',
|
description: 'Filter by category: communication, coding, workflow, appearance, general',
|
||||||
@@ -58,11 +79,13 @@ export function createMemoryTools(
|
|||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
async execute(_toolCallId, params) {
|
async execute(_toolCallId, params) {
|
||||||
const { userId, category } = params as { userId: string; category?: string };
|
if (!sessionUserId) return noUserError();
|
||||||
|
|
||||||
|
const { category } = params as { category?: string };
|
||||||
type Cat = 'communication' | 'coding' | 'workflow' | 'appearance' | 'general';
|
type Cat = 'communication' | 'coding' | 'workflow' | 'appearance' | 'general';
|
||||||
const prefs = category
|
const prefs = category
|
||||||
? await memory.preferences.findByUserAndCategory(userId, category as Cat)
|
? await memory.preferences.findByUserAndCategory(sessionUserId, category as Cat)
|
||||||
: await memory.preferences.findByUser(userId);
|
: await memory.preferences.findByUser(sessionUserId);
|
||||||
return {
|
return {
|
||||||
content: [{ type: 'text' as const, text: JSON.stringify(prefs, null, 2) }],
|
content: [{ type: 'text' as const, text: JSON.stringify(prefs, null, 2) }],
|
||||||
details: undefined,
|
details: undefined,
|
||||||
@@ -76,7 +99,6 @@ export function createMemoryTools(
|
|||||||
description:
|
description:
|
||||||
'Store a learned user preference (e.g., "prefers tables over paragraphs", "timezone: America/Chicago").',
|
'Store a learned user preference (e.g., "prefers tables over paragraphs", "timezone: America/Chicago").',
|
||||||
parameters: Type.Object({
|
parameters: Type.Object({
|
||||||
userId: Type.String({ description: 'User ID' }),
|
|
||||||
key: Type.String({ description: 'Preference key' }),
|
key: Type.String({ description: 'Preference key' }),
|
||||||
value: Type.String({ description: 'Preference value (JSON string)' }),
|
value: Type.String({ description: 'Preference value (JSON string)' }),
|
||||||
category: Type.Optional(
|
category: Type.Optional(
|
||||||
@@ -86,8 +108,9 @@ export function createMemoryTools(
|
|||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
async execute(_toolCallId, params) {
|
async execute(_toolCallId, params) {
|
||||||
const { userId, key, value, category } = params as {
|
if (!sessionUserId) return noUserError();
|
||||||
userId: string;
|
|
||||||
|
const { key, value, category } = params as {
|
||||||
key: string;
|
key: string;
|
||||||
value: string;
|
value: string;
|
||||||
category?: string;
|
category?: string;
|
||||||
@@ -100,7 +123,7 @@ export function createMemoryTools(
|
|||||||
parsedValue = value;
|
parsedValue = value;
|
||||||
}
|
}
|
||||||
const pref = await memory.preferences.upsert({
|
const pref = await memory.preferences.upsert({
|
||||||
userId,
|
userId: sessionUserId,
|
||||||
key,
|
key,
|
||||||
value: parsedValue,
|
value: parsedValue,
|
||||||
category: (category as Cat) ?? 'general',
|
category: (category as Cat) ?? 'general',
|
||||||
@@ -119,7 +142,6 @@ export function createMemoryTools(
|
|||||||
description:
|
description:
|
||||||
'Store a learned insight, decision, or knowledge extracted from the current interaction.',
|
'Store a learned insight, decision, or knowledge extracted from the current interaction.',
|
||||||
parameters: Type.Object({
|
parameters: Type.Object({
|
||||||
userId: Type.String({ description: 'User ID' }),
|
|
||||||
content: Type.String({ description: 'The insight or knowledge to store' }),
|
content: Type.String({ description: 'The insight or knowledge to store' }),
|
||||||
category: Type.Optional(
|
category: Type.Optional(
|
||||||
Type.String({
|
Type.String({
|
||||||
@@ -128,8 +150,9 @@ export function createMemoryTools(
|
|||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
async execute(_toolCallId, params) {
|
async execute(_toolCallId, params) {
|
||||||
const { userId, content, category } = params as {
|
if (!sessionUserId) return noUserError();
|
||||||
userId: string;
|
|
||||||
|
const { content, category } = params as {
|
||||||
content: string;
|
content: string;
|
||||||
category?: string;
|
category?: string;
|
||||||
};
|
};
|
||||||
@@ -141,7 +164,7 @@ export function createMemoryTools(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const insight = await memory.insights.create({
|
const insight = await memory.insights.create({
|
||||||
userId,
|
userId: sessionUserId,
|
||||||
content,
|
content,
|
||||||
embedding,
|
embedding,
|
||||||
source: 'agent',
|
source: 'agent',
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ import { createAuth, type Auth } from '@mosaic/auth';
|
|||||||
import type { Db } from '@mosaic/db';
|
import type { Db } from '@mosaic/db';
|
||||||
import { DB } from '../database/database.module.js';
|
import { DB } from '../database/database.module.js';
|
||||||
import { AUTH } from './auth.tokens.js';
|
import { AUTH } from './auth.tokens.js';
|
||||||
|
import { SsoController } from './sso.controller.js';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
|
controllers: [SsoController],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: AUTH,
|
provide: AUTH,
|
||||||
|
|||||||
40
apps/gateway/src/auth/sso.controller.spec.ts
Normal file
40
apps/gateway/src/auth/sso.controller.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import { SsoController } from './sso.controller.js';
|
||||||
|
|
||||||
|
describe('SsoController', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllEnvs();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lists configured OIDC providers', () => {
|
||||||
|
vi.stubEnv('WORKOS_CLIENT_ID', 'workos-client');
|
||||||
|
vi.stubEnv('WORKOS_CLIENT_SECRET', 'workos-secret');
|
||||||
|
vi.stubEnv('WORKOS_ISSUER', 'https://auth.workos.com/sso/client_123');
|
||||||
|
|
||||||
|
const controller = new SsoController();
|
||||||
|
const providers = controller.list();
|
||||||
|
|
||||||
|
expect(providers.find((provider) => provider.id === 'workos')).toMatchObject({
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'oidc',
|
||||||
|
callbackPath: '/api/auth/oauth2/callback/workos',
|
||||||
|
teamSync: { enabled: true, claim: 'organization_id' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('prefers SAML fallback for Keycloak when only the SAML login URL is configured', () => {
|
||||||
|
vi.stubEnv('KEYCLOAK_SAML_LOGIN_URL', 'https://sso.example.com/realms/mosaic/protocol/saml');
|
||||||
|
|
||||||
|
const controller = new SsoController();
|
||||||
|
const providers = controller.list();
|
||||||
|
|
||||||
|
expect(providers.find((provider) => provider.id === 'keycloak')).toMatchObject({
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'saml',
|
||||||
|
samlFallback: {
|
||||||
|
configured: true,
|
||||||
|
loginUrl: 'https://sso.example.com/realms/mosaic/protocol/saml',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
10
apps/gateway/src/auth/sso.controller.ts
Normal file
10
apps/gateway/src/auth/sso.controller.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { Controller, Get } from '@nestjs/common';
|
||||||
|
import { buildSsoDiscovery, type SsoProviderDiscovery } from '@mosaic/auth';
|
||||||
|
|
||||||
|
@Controller('api/sso/providers')
|
||||||
|
export class SsoController {
|
||||||
|
@Get()
|
||||||
|
list(): SsoProviderDiscovery[] {
|
||||||
|
return buildSsoDiscovery();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,15 +12,29 @@ import {
|
|||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
import type { AgentSessionEvent } from '@mariozechner/pi-coding-agent';
|
import type { AgentSessionEvent } from '@mariozechner/pi-coding-agent';
|
||||||
import type { Auth } from '@mosaic/auth';
|
import type { Auth } from '@mosaic/auth';
|
||||||
|
import type { Brain } from '@mosaic/brain';
|
||||||
import type { SetThinkingPayload, SlashCommandPayload, SystemReloadPayload } from '@mosaic/types';
|
import type { SetThinkingPayload, SlashCommandPayload, SystemReloadPayload } from '@mosaic/types';
|
||||||
import { AgentService } from '../agent/agent.service.js';
|
import { AgentService, type ConversationHistoryMessage } from '../agent/agent.service.js';
|
||||||
import { AUTH } from '../auth/auth.tokens.js';
|
import { AUTH } from '../auth/auth.tokens.js';
|
||||||
|
import { BRAIN } from '../brain/brain.tokens.js';
|
||||||
import { CommandRegistryService } from '../commands/command-registry.service.js';
|
import { CommandRegistryService } from '../commands/command-registry.service.js';
|
||||||
import { CommandExecutorService } from '../commands/command-executor.service.js';
|
import { CommandExecutorService } from '../commands/command-executor.service.js';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import { ChatSocketMessageDto } from './chat.dto.js';
|
import { ChatSocketMessageDto } from './chat.dto.js';
|
||||||
import { validateSocketSession } from './chat.gateway-auth.js';
|
import { validateSocketSession } from './chat.gateway-auth.js';
|
||||||
|
|
||||||
|
/** Per-client state tracking streaming accumulation for persistence. */
|
||||||
|
interface ClientSession {
|
||||||
|
conversationId: string;
|
||||||
|
cleanup: () => void;
|
||||||
|
/** Accumulated assistant response text for the current turn. */
|
||||||
|
assistantText: string;
|
||||||
|
/** Tool calls observed during the current turn. */
|
||||||
|
toolCalls: Array<{ toolCallId: string; toolName: string; args: unknown; isError: boolean }>;
|
||||||
|
/** Tool calls in-flight (started but not ended yet). */
|
||||||
|
pendingToolCalls: Map<string, { toolName: string; args: unknown }>;
|
||||||
|
}
|
||||||
|
|
||||||
@WebSocketGateway({
|
@WebSocketGateway({
|
||||||
cors: {
|
cors: {
|
||||||
origin: process.env['GATEWAY_CORS_ORIGIN'] ?? 'http://localhost:3000',
|
origin: process.env['GATEWAY_CORS_ORIGIN'] ?? 'http://localhost:3000',
|
||||||
@@ -32,14 +46,12 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
server!: Server;
|
server!: Server;
|
||||||
|
|
||||||
private readonly logger = new Logger(ChatGateway.name);
|
private readonly logger = new Logger(ChatGateway.name);
|
||||||
private readonly clientSessions = new Map<
|
private readonly clientSessions = new Map<string, ClientSession>();
|
||||||
string,
|
|
||||||
{ conversationId: string; cleanup: () => void }
|
|
||||||
>();
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(AgentService) private readonly agentService: AgentService,
|
@Inject(AgentService) private readonly agentService: AgentService,
|
||||||
@Inject(AUTH) private readonly auth: Auth,
|
@Inject(AUTH) private readonly auth: Auth,
|
||||||
|
@Inject(BRAIN) private readonly brain: Brain,
|
||||||
@Inject(CommandRegistryService) private readonly commandRegistry: CommandRegistryService,
|
@Inject(CommandRegistryService) private readonly commandRegistry: CommandRegistryService,
|
||||||
@Inject(CommandExecutorService) private readonly commandExecutor: CommandExecutorService,
|
@Inject(CommandExecutorService) private readonly commandExecutor: CommandExecutorService,
|
||||||
) {}
|
) {}
|
||||||
@@ -80,6 +92,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
@MessageBody() data: ChatSocketMessageDto,
|
@MessageBody() data: ChatSocketMessageDto,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const conversationId = data.conversationId ?? uuid();
|
const conversationId = data.conversationId ?? uuid();
|
||||||
|
const userId = (client.data.user as { id: string } | undefined)?.id;
|
||||||
|
|
||||||
this.logger.log(`Message from ${client.id} in conversation ${conversationId}`);
|
this.logger.log(`Message from ${client.id} in conversation ${conversationId}`);
|
||||||
|
|
||||||
@@ -87,13 +100,22 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
try {
|
try {
|
||||||
let agentSession = this.agentService.getSession(conversationId);
|
let agentSession = this.agentService.getSession(conversationId);
|
||||||
if (!agentSession) {
|
if (!agentSession) {
|
||||||
const userId = (client.data.user as { id: string } | undefined)?.id;
|
// When resuming an existing conversation, load prior messages to inject as context (M1-004)
|
||||||
|
const conversationHistory = await this.loadConversationHistory(conversationId, userId);
|
||||||
|
|
||||||
agentSession = await this.agentService.createSession(conversationId, {
|
agentSession = await this.agentService.createSession(conversationId, {
|
||||||
provider: data.provider,
|
provider: data.provider,
|
||||||
modelId: data.modelId,
|
modelId: data.modelId,
|
||||||
agentConfigId: data.agentId,
|
agentConfigId: data.agentId,
|
||||||
userId,
|
userId,
|
||||||
|
conversationHistory: conversationHistory.length > 0 ? conversationHistory : undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (conversationHistory.length > 0) {
|
||||||
|
this.logger.log(
|
||||||
|
`Loaded ${conversationHistory.length} prior messages for conversation=${conversationId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
@@ -107,6 +129,33 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure conversation record exists in the DB before persisting messages
|
||||||
|
if (userId) {
|
||||||
|
await this.ensureConversation(conversationId, userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist the user message
|
||||||
|
if (userId) {
|
||||||
|
try {
|
||||||
|
await this.brain.conversations.addMessage(
|
||||||
|
{
|
||||||
|
conversationId,
|
||||||
|
role: 'user',
|
||||||
|
content: data.content,
|
||||||
|
metadata: {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
userId,
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to persist user message for conversation=${conversationId}`,
|
||||||
|
err instanceof Error ? err.stack : String(err),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Always clean up previous listener to prevent leak
|
// Always clean up previous listener to prevent leak
|
||||||
const existing = this.clientSessions.get(client.id);
|
const existing = this.clientSessions.get(client.id);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
@@ -118,7 +167,13 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
this.relayEvent(client, conversationId, event);
|
this.relayEvent(client, conversationId, event);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.clientSessions.set(client.id, { conversationId, cleanup });
|
this.clientSessions.set(client.id, {
|
||||||
|
conversationId,
|
||||||
|
cleanup,
|
||||||
|
assistantText: '',
|
||||||
|
toolCalls: [],
|
||||||
|
pendingToolCalls: new Map(),
|
||||||
|
});
|
||||||
|
|
||||||
// Track channel connection
|
// Track channel connection
|
||||||
this.agentService.addChannel(conversationId, `websocket:${client.id}`);
|
this.agentService.addChannel(conversationId, `websocket:${client.id}`);
|
||||||
@@ -208,6 +263,57 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
this.logger.log('Broadcasted system:reload to all connected clients');
|
this.logger.log('Broadcasted system:reload to all connected clients');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure a conversation record exists in the DB.
|
||||||
|
* Creates it if absent — safe to call concurrently since a duplicate insert
|
||||||
|
* would fail on the PK constraint and be caught here.
|
||||||
|
*/
|
||||||
|
private async ensureConversation(conversationId: string, userId: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const existing = await this.brain.conversations.findById(conversationId, userId);
|
||||||
|
if (!existing) {
|
||||||
|
await this.brain.conversations.create({
|
||||||
|
id: conversationId,
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to ensure conversation record for conversation=${conversationId}`,
|
||||||
|
err instanceof Error ? err.stack : String(err),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load prior conversation messages from DB for context injection on session resume (M1-004).
|
||||||
|
* Returns an empty array when no history exists, the conversation is not owned by the user,
|
||||||
|
* or userId is not provided.
|
||||||
|
*/
|
||||||
|
private async loadConversationHistory(
|
||||||
|
conversationId: string,
|
||||||
|
userId: string | undefined,
|
||||||
|
): Promise<ConversationHistoryMessage[]> {
|
||||||
|
if (!userId) return [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const messages = await this.brain.conversations.findMessages(conversationId, userId);
|
||||||
|
if (messages.length === 0) return [];
|
||||||
|
|
||||||
|
return messages.map((msg) => ({
|
||||||
|
role: msg.role as 'user' | 'assistant' | 'system',
|
||||||
|
content: msg.content,
|
||||||
|
createdAt: msg.createdAt,
|
||||||
|
}));
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to load conversation history for conversation=${conversationId}`,
|
||||||
|
err instanceof Error ? err.stack : String(err),
|
||||||
|
);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private relayEvent(client: Socket, conversationId: string, event: AgentSessionEvent): void {
|
private relayEvent(client: Socket, conversationId: string, event: AgentSessionEvent): void {
|
||||||
if (!client.connected) {
|
if (!client.connected) {
|
||||||
this.logger.warn(
|
this.logger.warn(
|
||||||
@@ -217,9 +323,17 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'agent_start':
|
case 'agent_start': {
|
||||||
|
// Reset accumulation buffers for the new turn
|
||||||
|
const cs = this.clientSessions.get(client.id);
|
||||||
|
if (cs) {
|
||||||
|
cs.assistantText = '';
|
||||||
|
cs.toolCalls = [];
|
||||||
|
cs.pendingToolCalls.clear();
|
||||||
|
}
|
||||||
client.emit('agent:start', { conversationId });
|
client.emit('agent:start', { conversationId });
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'agent_end': {
|
case 'agent_end': {
|
||||||
// Gather usage stats from the Pi session
|
// Gather usage stats from the Pi session
|
||||||
@@ -228,28 +342,79 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
const stats = piSession?.getSessionStats();
|
const stats = piSession?.getSessionStats();
|
||||||
const contextUsage = piSession?.getContextUsage();
|
const contextUsage = piSession?.getContextUsage();
|
||||||
|
|
||||||
|
const usagePayload = stats
|
||||||
|
? {
|
||||||
|
provider: agentSession?.provider ?? 'unknown',
|
||||||
|
modelId: agentSession?.modelId ?? 'unknown',
|
||||||
|
thinkingLevel: piSession?.thinkingLevel ?? 'off',
|
||||||
|
tokens: stats.tokens,
|
||||||
|
cost: stats.cost,
|
||||||
|
context: {
|
||||||
|
percent: contextUsage?.percent ?? null,
|
||||||
|
window: contextUsage?.contextWindow ?? 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: undefined;
|
||||||
|
|
||||||
client.emit('agent:end', {
|
client.emit('agent:end', {
|
||||||
conversationId,
|
conversationId,
|
||||||
usage: stats
|
usage: usagePayload,
|
||||||
? {
|
|
||||||
provider: agentSession?.provider ?? 'unknown',
|
|
||||||
modelId: agentSession?.modelId ?? 'unknown',
|
|
||||||
thinkingLevel: piSession?.thinkingLevel ?? 'off',
|
|
||||||
tokens: stats.tokens,
|
|
||||||
cost: stats.cost,
|
|
||||||
context: {
|
|
||||||
percent: contextUsage?.percent ?? null,
|
|
||||||
window: contextUsage?.contextWindow ?? 0,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Persist the assistant message with metadata
|
||||||
|
const cs = this.clientSessions.get(client.id);
|
||||||
|
const userId = (client.data.user as { id: string } | undefined)?.id;
|
||||||
|
if (cs && userId && cs.assistantText.trim().length > 0) {
|
||||||
|
const metadata: Record<string, unknown> = {
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
model: agentSession?.modelId ?? 'unknown',
|
||||||
|
provider: agentSession?.provider ?? 'unknown',
|
||||||
|
toolCalls: cs.toolCalls,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (stats?.tokens) {
|
||||||
|
metadata['tokenUsage'] = {
|
||||||
|
input: stats.tokens.input,
|
||||||
|
output: stats.tokens.output,
|
||||||
|
cacheRead: stats.tokens.cacheRead,
|
||||||
|
cacheWrite: stats.tokens.cacheWrite,
|
||||||
|
total: stats.tokens.total,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this.brain.conversations
|
||||||
|
.addMessage(
|
||||||
|
{
|
||||||
|
conversationId,
|
||||||
|
role: 'assistant',
|
||||||
|
content: cs.assistantText,
|
||||||
|
metadata,
|
||||||
|
},
|
||||||
|
userId,
|
||||||
|
)
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to persist assistant message for conversation=${conversationId}`,
|
||||||
|
err instanceof Error ? err.stack : String(err),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reset accumulation
|
||||||
|
cs.assistantText = '';
|
||||||
|
cs.toolCalls = [];
|
||||||
|
cs.pendingToolCalls.clear();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'message_update': {
|
case 'message_update': {
|
||||||
const assistantEvent = event.assistantMessageEvent;
|
const assistantEvent = event.assistantMessageEvent;
|
||||||
if (assistantEvent.type === 'text_delta') {
|
if (assistantEvent.type === 'text_delta') {
|
||||||
|
// Accumulate assistant text for persistence
|
||||||
|
const cs = this.clientSessions.get(client.id);
|
||||||
|
if (cs) {
|
||||||
|
cs.assistantText += assistantEvent.delta;
|
||||||
|
}
|
||||||
client.emit('agent:text', {
|
client.emit('agent:text', {
|
||||||
conversationId,
|
conversationId,
|
||||||
text: assistantEvent.delta,
|
text: assistantEvent.delta,
|
||||||
@@ -263,15 +428,36 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'tool_execution_start':
|
case 'tool_execution_start': {
|
||||||
|
// Track pending tool call for later recording
|
||||||
|
const cs = this.clientSessions.get(client.id);
|
||||||
|
if (cs) {
|
||||||
|
cs.pendingToolCalls.set(event.toolCallId, {
|
||||||
|
toolName: event.toolName,
|
||||||
|
args: event.args,
|
||||||
|
});
|
||||||
|
}
|
||||||
client.emit('agent:tool:start', {
|
client.emit('agent:tool:start', {
|
||||||
conversationId,
|
conversationId,
|
||||||
toolCallId: event.toolCallId,
|
toolCallId: event.toolCallId,
|
||||||
toolName: event.toolName,
|
toolName: event.toolName,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 'tool_execution_end':
|
case 'tool_execution_end': {
|
||||||
|
// Finalise tool call record
|
||||||
|
const cs = this.clientSessions.get(client.id);
|
||||||
|
if (cs) {
|
||||||
|
const pending = cs.pendingToolCalls.get(event.toolCallId);
|
||||||
|
cs.toolCalls.push({
|
||||||
|
toolCallId: event.toolCallId,
|
||||||
|
toolName: event.toolName,
|
||||||
|
args: pending?.args ?? null,
|
||||||
|
isError: event.isError,
|
||||||
|
});
|
||||||
|
cs.pendingToolCalls.delete(event.toolCallId);
|
||||||
|
}
|
||||||
client.emit('agent:tool:end', {
|
client.emit('agent:tool:end', {
|
||||||
conversationId,
|
conversationId,
|
||||||
toolCallId: event.toolCallId,
|
toolCallId: event.toolCallId,
|
||||||
@@ -279,6 +465,7 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
|
|||||||
isError: event.isError,
|
isError: event.isError,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,8 +77,8 @@ export class CommandExecutorService {
|
|||||||
message: 'Retry last message requested.',
|
message: 'Retry last message requested.',
|
||||||
};
|
};
|
||||||
case 'gc': {
|
case 'gc': {
|
||||||
// User-scoped sweep for non-admin; system-wide for admin
|
// Admin-only: system-wide GC sweep across all sessions
|
||||||
const result = await this.sessionGC.sweepOrphans(userId);
|
const result = await this.sessionGC.sweepOrphans();
|
||||||
return {
|
return {
|
||||||
command: 'gc',
|
command: 'gc',
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -190,9 +190,9 @@ export class CommandRegistryService implements OnModuleInit {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'gc',
|
name: 'gc',
|
||||||
description: 'Trigger garbage collection sweep (user-scoped)',
|
description: 'Trigger garbage collection sweep (admin only — system-wide)',
|
||||||
aliases: [],
|
aliases: [],
|
||||||
scope: 'core',
|
scope: 'admin',
|
||||||
execution: 'socket',
|
execution: 'socket',
|
||||||
available: true,
|
available: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -166,11 +166,11 @@ describe('CommandExecutorService — integration', () => {
|
|||||||
expect(result.command).toBe('nonexistent');
|
expect(result.command).toBe('nonexistent');
|
||||||
});
|
});
|
||||||
|
|
||||||
// /gc handler calls SessionGCService.sweepOrphans
|
// /gc handler calls SessionGCService.sweepOrphans (admin-only, no userId arg)
|
||||||
it('/gc calls SessionGCService.sweepOrphans with userId', async () => {
|
it('/gc calls SessionGCService.sweepOrphans without arguments', async () => {
|
||||||
const payload: SlashCommandPayload = { command: 'gc', conversationId };
|
const payload: SlashCommandPayload = { command: 'gc', conversationId };
|
||||||
const result = await executor.execute(payload, userId);
|
const result = await executor.execute(payload, userId);
|
||||||
expect(mockSessionGC.sweepOrphans).toHaveBeenCalledWith(userId);
|
expect(mockSessionGC.sweepOrphans).toHaveBeenCalledWith();
|
||||||
expect(result.success).toBe(true);
|
expect(result.success).toBe(true);
|
||||||
expect(result.message).toContain('GC sweep complete');
|
expect(result.message).toContain('GC sweep complete');
|
||||||
expect(result.message).toContain('3 orphaned sessions');
|
expect(result.message).toContain('3 orphaned sessions');
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
|
BadRequestException,
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
|
ForbiddenException,
|
||||||
Get,
|
Get,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
@@ -10,17 +12,18 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Patch,
|
Patch,
|
||||||
Post,
|
Post,
|
||||||
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import type { Brain } from '@mosaic/brain';
|
import type { Brain } from '@mosaic/brain';
|
||||||
import { BRAIN } from '../brain/brain.tokens.js';
|
import { BRAIN } from '../brain/brain.tokens.js';
|
||||||
import { AuthGuard } from '../auth/auth.guard.js';
|
import { AuthGuard } from '../auth/auth.guard.js';
|
||||||
import { CurrentUser } from '../auth/current-user.decorator.js';
|
import { CurrentUser } from '../auth/current-user.decorator.js';
|
||||||
import { assertOwner } from '../auth/resource-ownership.js';
|
|
||||||
import {
|
import {
|
||||||
CreateConversationDto,
|
CreateConversationDto,
|
||||||
UpdateConversationDto,
|
UpdateConversationDto,
|
||||||
SendMessageDto,
|
SendMessageDto,
|
||||||
|
SearchMessagesDto,
|
||||||
} from './conversations.dto.js';
|
} from './conversations.dto.js';
|
||||||
|
|
||||||
@Controller('api/conversations')
|
@Controller('api/conversations')
|
||||||
@@ -33,9 +36,21 @@ export class ConversationsController {
|
|||||||
return this.brain.conversations.findAll(user.id);
|
return this.brain.conversations.findAll(user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('search')
|
||||||
|
async search(@Query() dto: SearchMessagesDto, @CurrentUser() user: { id: string }) {
|
||||||
|
if (!dto.q || dto.q.trim().length === 0) {
|
||||||
|
throw new BadRequestException('Query parameter "q" is required and must not be empty');
|
||||||
|
}
|
||||||
|
const limit = dto.limit ?? 20;
|
||||||
|
const offset = dto.offset ?? 0;
|
||||||
|
return this.brain.conversations.searchMessages(user.id, dto.q.trim(), limit, offset);
|
||||||
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
async findOne(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
async findOne(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
||||||
return this.getOwnedConversation(id, user.id);
|
const conversation = await this.brain.conversations.findById(id, user.id);
|
||||||
|
if (!conversation) throw new NotFoundException('Conversation not found');
|
||||||
|
return conversation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
@@ -53,8 +68,7 @@ export class ConversationsController {
|
|||||||
@Body() dto: UpdateConversationDto,
|
@Body() dto: UpdateConversationDto,
|
||||||
@CurrentUser() user: { id: string },
|
@CurrentUser() user: { id: string },
|
||||||
) {
|
) {
|
||||||
await this.getOwnedConversation(id, user.id);
|
const conversation = await this.brain.conversations.update(id, user.id, dto);
|
||||||
const conversation = await this.brain.conversations.update(id, dto);
|
|
||||||
if (!conversation) throw new NotFoundException('Conversation not found');
|
if (!conversation) throw new NotFoundException('Conversation not found');
|
||||||
return conversation;
|
return conversation;
|
||||||
}
|
}
|
||||||
@@ -62,15 +76,16 @@ export class ConversationsController {
|
|||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@HttpCode(HttpStatus.NO_CONTENT)
|
@HttpCode(HttpStatus.NO_CONTENT)
|
||||||
async remove(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
async remove(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
||||||
await this.getOwnedConversation(id, user.id);
|
const deleted = await this.brain.conversations.remove(id, user.id);
|
||||||
const deleted = await this.brain.conversations.remove(id);
|
|
||||||
if (!deleted) throw new NotFoundException('Conversation not found');
|
if (!deleted) throw new NotFoundException('Conversation not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id/messages')
|
@Get(':id/messages')
|
||||||
async listMessages(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
async listMessages(@Param('id') id: string, @CurrentUser() user: { id: string }) {
|
||||||
await this.getOwnedConversation(id, user.id);
|
// Verify ownership explicitly to return a clear 404 rather than an empty list.
|
||||||
return this.brain.conversations.findMessages(id);
|
const conversation = await this.brain.conversations.findById(id, user.id);
|
||||||
|
if (!conversation) throw new NotFoundException('Conversation not found');
|
||||||
|
return this.brain.conversations.findMessages(id, user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post(':id/messages')
|
@Post(':id/messages')
|
||||||
@@ -79,19 +94,16 @@ export class ConversationsController {
|
|||||||
@Body() dto: SendMessageDto,
|
@Body() dto: SendMessageDto,
|
||||||
@CurrentUser() user: { id: string },
|
@CurrentUser() user: { id: string },
|
||||||
) {
|
) {
|
||||||
await this.getOwnedConversation(id, user.id);
|
const message = await this.brain.conversations.addMessage(
|
||||||
return this.brain.conversations.addMessage({
|
{
|
||||||
conversationId: id,
|
conversationId: id,
|
||||||
role: dto.role,
|
role: dto.role,
|
||||||
content: dto.content,
|
content: dto.content,
|
||||||
metadata: dto.metadata,
|
metadata: dto.metadata,
|
||||||
});
|
},
|
||||||
}
|
user.id,
|
||||||
|
);
|
||||||
private async getOwnedConversation(id: string, userId: string) {
|
if (!message) throw new ForbiddenException('Conversation not found or access denied');
|
||||||
const conversation = await this.brain.conversations.findById(id);
|
return message;
|
||||||
if (!conversation) throw new NotFoundException('Conversation not found');
|
|
||||||
assertOwner(conversation.userId, userId, 'Conversation');
|
|
||||||
return conversation;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,35 @@
|
|||||||
import {
|
import {
|
||||||
IsBoolean,
|
IsBoolean,
|
||||||
IsIn,
|
IsIn,
|
||||||
|
IsInt,
|
||||||
IsObject,
|
IsObject,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
IsUUID,
|
IsUUID,
|
||||||
|
Max,
|
||||||
MaxLength,
|
MaxLength,
|
||||||
|
Min,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
|
export class SearchMessagesDto {
|
||||||
|
@IsString()
|
||||||
|
@MaxLength(500)
|
||||||
|
q!: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt()
|
||||||
|
@Min(1)
|
||||||
|
@Max(100)
|
||||||
|
limit?: number = 20;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt()
|
||||||
|
@Min(0)
|
||||||
|
offset?: number = 0;
|
||||||
|
}
|
||||||
|
|
||||||
export class CreateConversationDto {
|
export class CreateConversationDto {
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { LogService } from '@mosaic/log';
|
|||||||
import { SessionGCService } from './session-gc.service.js';
|
import { SessionGCService } from './session-gc.service.js';
|
||||||
|
|
||||||
type MockRedis = {
|
type MockRedis = {
|
||||||
keys: ReturnType<typeof vi.fn>;
|
scan: ReturnType<typeof vi.fn>;
|
||||||
del: ReturnType<typeof vi.fn>;
|
del: ReturnType<typeof vi.fn>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -14,9 +14,17 @@ describe('SessionGCService', () => {
|
|||||||
let mockRedis: MockRedis;
|
let mockRedis: MockRedis;
|
||||||
let mockLogService: { logs: { promoteToWarm: ReturnType<typeof vi.fn> } };
|
let mockLogService: { logs: { promoteToWarm: ReturnType<typeof vi.fn> } };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: build a scan mock that returns all provided keys in a single
|
||||||
|
* cursor iteration (cursor '0' in → ['0', keys] out).
|
||||||
|
*/
|
||||||
|
function makeScanMock(keys: string[]): ReturnType<typeof vi.fn> {
|
||||||
|
return vi.fn().mockResolvedValue(['0', keys]);
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockRedis = {
|
mockRedis = {
|
||||||
keys: vi.fn().mockResolvedValue([]),
|
scan: makeScanMock([]),
|
||||||
del: vi.fn().mockResolvedValue(0),
|
del: vi.fn().mockResolvedValue(0),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -36,7 +44,7 @@ describe('SessionGCService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('collect() deletes Valkey keys for session', async () => {
|
it('collect() deletes Valkey keys for session', async () => {
|
||||||
mockRedis.keys.mockResolvedValue(['mosaic:session:abc:system', 'mosaic:session:abc:foo']);
|
mockRedis.scan = makeScanMock(['mosaic:session:abc:system', 'mosaic:session:abc:foo']);
|
||||||
const result = await service.collect('abc');
|
const result = await service.collect('abc');
|
||||||
expect(mockRedis.del).toHaveBeenCalledWith(
|
expect(mockRedis.del).toHaveBeenCalledWith(
|
||||||
'mosaic:session:abc:system',
|
'mosaic:session:abc:system',
|
||||||
@@ -46,7 +54,7 @@ describe('SessionGCService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('collect() with no keys returns empty cleaned valkeyKeys', async () => {
|
it('collect() with no keys returns empty cleaned valkeyKeys', async () => {
|
||||||
mockRedis.keys.mockResolvedValue([]);
|
mockRedis.scan = makeScanMock([]);
|
||||||
const result = await service.collect('abc');
|
const result = await service.collect('abc');
|
||||||
expect(result.cleaned.valkeyKeys).toBeUndefined();
|
expect(result.cleaned.valkeyKeys).toBeUndefined();
|
||||||
});
|
});
|
||||||
@@ -57,14 +65,14 @@ describe('SessionGCService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('fullCollect() deletes all session keys', async () => {
|
it('fullCollect() deletes all session keys', async () => {
|
||||||
mockRedis.keys.mockResolvedValue(['mosaic:session:abc:system', 'mosaic:session:xyz:foo']);
|
mockRedis.scan = makeScanMock(['mosaic:session:abc:system', 'mosaic:session:xyz:foo']);
|
||||||
const result = await service.fullCollect();
|
const result = await service.fullCollect();
|
||||||
expect(mockRedis.del).toHaveBeenCalled();
|
expect(mockRedis.del).toHaveBeenCalled();
|
||||||
expect(result.valkeyKeys).toBe(2);
|
expect(result.valkeyKeys).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('fullCollect() with no keys returns 0 valkeyKeys', async () => {
|
it('fullCollect() with no keys returns 0 valkeyKeys', async () => {
|
||||||
mockRedis.keys.mockResolvedValue([]);
|
mockRedis.scan = makeScanMock([]);
|
||||||
const result = await service.fullCollect();
|
const result = await service.fullCollect();
|
||||||
expect(result.valkeyKeys).toBe(0);
|
expect(result.valkeyKeys).toBe(0);
|
||||||
expect(mockRedis.del).not.toHaveBeenCalled();
|
expect(mockRedis.del).not.toHaveBeenCalled();
|
||||||
@@ -76,11 +84,18 @@ describe('SessionGCService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('sweepOrphans() extracts unique session IDs and collects them', async () => {
|
it('sweepOrphans() extracts unique session IDs and collects them', async () => {
|
||||||
mockRedis.keys.mockResolvedValue([
|
// First scan call returns the global session list; subsequent calls return
|
||||||
'mosaic:session:abc:system',
|
// per-session keys during collect().
|
||||||
'mosaic:session:abc:messages',
|
mockRedis.scan = vi
|
||||||
'mosaic:session:xyz:system',
|
.fn()
|
||||||
]);
|
.mockResolvedValueOnce([
|
||||||
|
'0',
|
||||||
|
['mosaic:session:abc:system', 'mosaic:session:abc:messages', 'mosaic:session:xyz:system'],
|
||||||
|
])
|
||||||
|
// collect('abc') scan
|
||||||
|
.mockResolvedValueOnce(['0', ['mosaic:session:abc:system', 'mosaic:session:abc:messages']])
|
||||||
|
// collect('xyz') scan
|
||||||
|
.mockResolvedValueOnce(['0', ['mosaic:session:xyz:system']]);
|
||||||
mockRedis.del.mockResolvedValue(1);
|
mockRedis.del.mockResolvedValue(1);
|
||||||
|
|
||||||
const result = await service.sweepOrphans();
|
const result = await service.sweepOrphans();
|
||||||
@@ -89,7 +104,7 @@ describe('SessionGCService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('sweepOrphans() returns empty when no session keys', async () => {
|
it('sweepOrphans() returns empty when no session keys', async () => {
|
||||||
mockRedis.keys.mockResolvedValue([]);
|
mockRedis.scan = makeScanMock([]);
|
||||||
const result = await service.sweepOrphans();
|
const result = await service.sweepOrphans();
|
||||||
expect(result.orphanedSessions).toBe(0);
|
expect(result.orphanedSessions).toBe(0);
|
||||||
expect(result.totalCleaned).toHaveLength(0);
|
expect(result.totalCleaned).toHaveLength(0);
|
||||||
|
|||||||
@@ -36,16 +36,40 @@ export class SessionGCService implements OnModuleInit {
|
|||||||
@Inject(LOG_SERVICE) private readonly logService: LogService,
|
@Inject(LOG_SERVICE) private readonly logService: LogService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async onModuleInit(): Promise<void> {
|
onModuleInit(): void {
|
||||||
this.logger.log('Running full GC on cold start...');
|
// Fire-and-forget: run full GC asynchronously so it does not block the
|
||||||
const result = await this.fullCollect();
|
// NestJS bootstrap chain. Cold-start GC typically takes 100–500 ms
|
||||||
this.logger.log(
|
// depending on Valkey key count; deferring it removes that latency from
|
||||||
`Full GC complete: ${result.valkeyKeys} Valkey keys, ` +
|
// the TTFB of the first HTTP request.
|
||||||
`${result.logsDemoted} logs demoted, ` +
|
this.fullCollect()
|
||||||
`${result.jobsPurged} jobs purged, ` +
|
.then((result) => {
|
||||||
`${result.tempFilesRemoved} temp dirs removed ` +
|
this.logger.log(
|
||||||
`(${result.duration}ms)`,
|
`Full GC complete: ${result.valkeyKeys} Valkey keys, ` +
|
||||||
);
|
`${result.logsDemoted} logs demoted, ` +
|
||||||
|
`${result.jobsPurged} jobs purged, ` +
|
||||||
|
`${result.tempFilesRemoved} temp dirs removed ` +
|
||||||
|
`(${result.duration}ms)`,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
this.logger.error('Cold-start GC failed', err instanceof Error ? err.stack : String(err));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scan Valkey for all keys matching a pattern using SCAN (non-blocking).
|
||||||
|
* KEYS is avoided because it blocks the Valkey event loop for the full scan
|
||||||
|
* duration, which can cause latency spikes under production key volumes.
|
||||||
|
*/
|
||||||
|
private async scanKeys(pattern: string): Promise<string[]> {
|
||||||
|
const collected: string[] = [];
|
||||||
|
let cursor = '0';
|
||||||
|
do {
|
||||||
|
const [nextCursor, keys] = await this.redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
|
||||||
|
cursor = nextCursor;
|
||||||
|
collected.push(...keys);
|
||||||
|
} while (cursor !== '0');
|
||||||
|
return collected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,7 +80,7 @@ export class SessionGCService implements OnModuleInit {
|
|||||||
|
|
||||||
// 1. Valkey: delete all session-scoped keys
|
// 1. Valkey: delete all session-scoped keys
|
||||||
const pattern = `mosaic:session:${sessionId}:*`;
|
const pattern = `mosaic:session:${sessionId}:*`;
|
||||||
const valkeyKeys = await this.redis.keys(pattern);
|
const valkeyKeys = await this.scanKeys(pattern);
|
||||||
if (valkeyKeys.length > 0) {
|
if (valkeyKeys.length > 0) {
|
||||||
await this.redis.del(...valkeyKeys);
|
await this.redis.del(...valkeyKeys);
|
||||||
result.cleaned.valkeyKeys = valkeyKeys.length;
|
result.cleaned.valkeyKeys = valkeyKeys.length;
|
||||||
@@ -74,14 +98,15 @@ export class SessionGCService implements OnModuleInit {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sweep GC — find orphaned artifacts from dead sessions.
|
* Sweep GC — find orphaned artifacts from dead sessions.
|
||||||
* User-scoped when userId provided; system-wide when null (admin).
|
* System-wide operation: only call from admin-authorized paths or internal
|
||||||
|
* scheduled jobs. Individual session cleanup is handled by collect().
|
||||||
*/
|
*/
|
||||||
async sweepOrphans(_userId?: string): Promise<GCSweepResult> {
|
async sweepOrphans(): Promise<GCSweepResult> {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const cleaned: GCResult[] = [];
|
const cleaned: GCResult[] = [];
|
||||||
|
|
||||||
// 1. Find all session-scoped Valkey keys
|
// 1. Find all session-scoped Valkey keys (non-blocking SCAN)
|
||||||
const allSessionKeys = await this.redis.keys('mosaic:session:*');
|
const allSessionKeys = await this.scanKeys('mosaic:session:*');
|
||||||
|
|
||||||
// Extract unique session IDs from keys
|
// Extract unique session IDs from keys
|
||||||
const sessionIds = new Set<string>();
|
const sessionIds = new Set<string>();
|
||||||
@@ -112,8 +137,8 @@ export class SessionGCService implements OnModuleInit {
|
|||||||
async fullCollect(): Promise<FullGCResult> {
|
async fullCollect(): Promise<FullGCResult> {
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
// 1. Valkey: delete ALL session-scoped keys
|
// 1. Valkey: delete ALL session-scoped keys (non-blocking SCAN)
|
||||||
const sessionKeys = await this.redis.keys('mosaic:session:*');
|
const sessionKeys = await this.scanKeys('mosaic:session:*');
|
||||||
if (sessionKeys.length > 0) {
|
if (sessionKeys.length > 0) {
|
||||||
await this.redis.del(...sessionKeys);
|
await this.redis.del(...sessionKeys);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ export class SummarizationService {
|
|||||||
|
|
||||||
const promoted = await this.logService.logs.promoteToCold(warmCutoff);
|
const promoted = await this.logService.logs.promoteToCold(warmCutoff);
|
||||||
const purged = await this.logService.logs.purge(coldCutoff);
|
const purged = await this.logService.logs.purge(coldCutoff);
|
||||||
const decayed = await this.memory.insights.decayOldInsights(decayCutoff);
|
const decayed = await this.memory.insights.decayAllInsights(decayCutoff);
|
||||||
|
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`Tier management: ${promoted} logs→cold, ${purged} purged, ${decayed} insights decayed`,
|
`Tier management: ${promoted} logs→cold, ${purged} purged, ${decayed} insights decayed`,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { NestFactory } from '@nestjs/core';
|
|||||||
import { Logger, ValidationPipe } from '@nestjs/common';
|
import { Logger, ValidationPipe } from '@nestjs/common';
|
||||||
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
|
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||||
import helmet from '@fastify/helmet';
|
import helmet from '@fastify/helmet';
|
||||||
|
import { listSsoStartupWarnings } from '@mosaic/auth';
|
||||||
import { AppModule } from './app.module.js';
|
import { AppModule } from './app.module.js';
|
||||||
import { mountAuthHandler } from './auth/auth.controller.js';
|
import { mountAuthHandler } from './auth/auth.controller.js';
|
||||||
import { mountMcpHandler } from './mcp/mcp.controller.js';
|
import { mountMcpHandler } from './mcp/mcp.controller.js';
|
||||||
@@ -23,13 +24,8 @@ async function bootstrap(): Promise<void> {
|
|||||||
throw new Error('BETTER_AUTH_SECRET is required');
|
throw new Error('BETTER_AUTH_SECRET is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
for (const warning of listSsoStartupWarnings()) {
|
||||||
process.env['AUTHENTIK_CLIENT_ID'] &&
|
logger.warn(warning);
|
||||||
(!process.env['AUTHENTIK_CLIENT_SECRET'] || !process.env['AUTHENTIK_ISSUER'])
|
|
||||||
) {
|
|
||||||
console.warn(
|
|
||||||
'[warn] AUTHENTIK_CLIENT_ID is set but AUTHENTIK_CLIENT_SECRET or AUTHENTIK_ISSUER is missing — Authentik SSO will not work',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = await NestFactory.create<NestFastifyApplication>(
|
const app = await NestFactory.create<NestFastifyApplication>(
|
||||||
|
|||||||
@@ -1,36 +1,122 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import type { EmbeddingProvider } from '@mosaic/memory';
|
import type { EmbeddingProvider } from '@mosaic/memory';
|
||||||
|
|
||||||
const DEFAULT_MODEL = 'text-embedding-3-small';
|
// ---------------------------------------------------------------------------
|
||||||
const DEFAULT_DIMENSIONS = 1536;
|
// Environment-driven configuration
|
||||||
|
//
|
||||||
|
// EMBEDDING_PROVIDER — 'ollama' (default) | 'openai'
|
||||||
|
// EMBEDDING_MODEL — model id, defaults differ per provider
|
||||||
|
// EMBEDDING_DIMENSIONS — integer, defaults differ per provider
|
||||||
|
// OLLAMA_BASE_URL — base URL for Ollama (used when provider=ollama)
|
||||||
|
// EMBEDDING_API_URL — full base URL for OpenAI-compatible API
|
||||||
|
// OPENAI_API_KEY — required for OpenAI provider
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
interface EmbeddingResponse {
|
const OLLAMA_DEFAULT_MODEL = 'nomic-embed-text';
|
||||||
|
const OLLAMA_DEFAULT_DIMENSIONS = 768;
|
||||||
|
|
||||||
|
const OPENAI_DEFAULT_MODEL = 'text-embedding-3-small';
|
||||||
|
const OPENAI_DEFAULT_DIMENSIONS = 1536;
|
||||||
|
|
||||||
|
/** Known dimension mismatch: warn if pgvector column likely has wrong size */
|
||||||
|
const PGVECTOR_SCHEMA_DIMENSIONS = 1536;
|
||||||
|
|
||||||
|
type EmbeddingBackend = 'ollama' | 'openai';
|
||||||
|
|
||||||
|
interface OllamaEmbeddingResponse {
|
||||||
|
embedding: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OpenAIEmbeddingResponse {
|
||||||
data: Array<{ embedding: number[]; index: number }>;
|
data: Array<{ embedding: number[]; index: number }>;
|
||||||
model: string;
|
model: string;
|
||||||
usage: { prompt_tokens: number; total_tokens: number };
|
usage: { prompt_tokens: number; total_tokens: number };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates embeddings via the OpenAI-compatible embeddings API.
|
* Provider-agnostic embedding service.
|
||||||
* Supports OpenAI, Azure OpenAI, and any provider with a compatible endpoint.
|
*
|
||||||
|
* Defaults to Ollama's native embedding API using nomic-embed-text (768 dims).
|
||||||
|
* Falls back to the OpenAI-compatible API when EMBEDDING_PROVIDER=openai or
|
||||||
|
* when OPENAI_API_KEY is set and EMBEDDING_PROVIDER is not explicitly set to ollama.
|
||||||
|
*
|
||||||
|
* Dimension mismatch detection: if the configured dimensions differ from the
|
||||||
|
* pgvector schema (1536), a warning is logged with re-embedding instructions.
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class EmbeddingService implements EmbeddingProvider {
|
export class EmbeddingService implements EmbeddingProvider {
|
||||||
private readonly logger = new Logger(EmbeddingService.name);
|
private readonly logger = new Logger(EmbeddingService.name);
|
||||||
private readonly apiKey: string | undefined;
|
private readonly backend: EmbeddingBackend;
|
||||||
private readonly baseUrl: string;
|
|
||||||
private readonly model: string;
|
private readonly model: string;
|
||||||
|
readonly dimensions: number;
|
||||||
|
|
||||||
readonly dimensions = DEFAULT_DIMENSIONS;
|
// Ollama-specific
|
||||||
|
private readonly ollamaBaseUrl: string | undefined;
|
||||||
|
|
||||||
|
// OpenAI-compatible
|
||||||
|
private readonly openaiApiKey: string | undefined;
|
||||||
|
private readonly openaiBaseUrl: string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.apiKey = process.env['OPENAI_API_KEY'];
|
// Determine backend
|
||||||
this.baseUrl = process.env['EMBEDDING_API_URL'] ?? 'https://api.openai.com/v1';
|
const providerEnv = process.env['EMBEDDING_PROVIDER'];
|
||||||
this.model = process.env['EMBEDDING_MODEL'] ?? DEFAULT_MODEL;
|
const openaiKey = process.env['OPENAI_API_KEY'];
|
||||||
|
const ollamaUrl = process.env['OLLAMA_BASE_URL'] ?? process.env['OLLAMA_HOST'];
|
||||||
|
|
||||||
|
if (providerEnv === 'openai') {
|
||||||
|
this.backend = 'openai';
|
||||||
|
} else if (providerEnv === 'ollama') {
|
||||||
|
this.backend = 'ollama';
|
||||||
|
} else if (process.env['EMBEDDING_API_URL']) {
|
||||||
|
// Legacy: explicit API URL configured → use openai-compat path
|
||||||
|
this.backend = 'openai';
|
||||||
|
} else if (ollamaUrl) {
|
||||||
|
// Ollama available and no explicit override → prefer Ollama
|
||||||
|
this.backend = 'ollama';
|
||||||
|
} else if (openaiKey) {
|
||||||
|
// OpenAI key present → use OpenAI
|
||||||
|
this.backend = 'openai';
|
||||||
|
} else {
|
||||||
|
// Nothing configured — default to ollama (will return zeros when unavailable)
|
||||||
|
this.backend = 'ollama';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set model and dimension defaults based on backend
|
||||||
|
if (this.backend === 'ollama') {
|
||||||
|
this.model = process.env['EMBEDDING_MODEL'] ?? OLLAMA_DEFAULT_MODEL;
|
||||||
|
this.dimensions =
|
||||||
|
parseInt(process.env['EMBEDDING_DIMENSIONS'] ?? '', 10) || OLLAMA_DEFAULT_DIMENSIONS;
|
||||||
|
this.ollamaBaseUrl = ollamaUrl;
|
||||||
|
this.openaiApiKey = undefined;
|
||||||
|
this.openaiBaseUrl = '';
|
||||||
|
} else {
|
||||||
|
this.model = process.env['EMBEDDING_MODEL'] ?? OPENAI_DEFAULT_MODEL;
|
||||||
|
this.dimensions =
|
||||||
|
parseInt(process.env['EMBEDDING_DIMENSIONS'] ?? '', 10) || OPENAI_DEFAULT_DIMENSIONS;
|
||||||
|
this.ollamaBaseUrl = undefined;
|
||||||
|
this.openaiApiKey = openaiKey;
|
||||||
|
this.openaiBaseUrl = process.env['EMBEDDING_API_URL'] ?? 'https://api.openai.com/v1';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn on dimension mismatch with the current schema
|
||||||
|
if (this.dimensions !== PGVECTOR_SCHEMA_DIMENSIONS) {
|
||||||
|
this.logger.warn(
|
||||||
|
`Embedding dimensions (${this.dimensions}) differ from pgvector schema (${PGVECTOR_SCHEMA_DIMENSIONS}). ` +
|
||||||
|
`If insights already contain ${PGVECTOR_SCHEMA_DIMENSIONS}-dim vectors, similarity search will fail. ` +
|
||||||
|
`To fix: truncate the insights table and re-embed, or run a migration to ALTER COLUMN embedding TYPE vector(${this.dimensions}).`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`EmbeddingService initialized: backend=${this.backend}, model=${this.model}, dimensions=${this.dimensions}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
get available(): boolean {
|
get available(): boolean {
|
||||||
return !!this.apiKey;
|
if (this.backend === 'ollama') {
|
||||||
|
return !!this.ollamaBaseUrl;
|
||||||
|
}
|
||||||
|
return !!this.openaiApiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
async embed(text: string): Promise<number[]> {
|
async embed(text: string): Promise<number[]> {
|
||||||
@@ -39,16 +125,60 @@ export class EmbeddingService implements EmbeddingProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async embedBatch(texts: string[]): Promise<number[][]> {
|
async embedBatch(texts: string[]): Promise<number[][]> {
|
||||||
if (!this.apiKey) {
|
if (!this.available) {
|
||||||
this.logger.warn('No OPENAI_API_KEY configured — returning zero vectors');
|
const reason =
|
||||||
|
this.backend === 'ollama'
|
||||||
|
? 'OLLAMA_BASE_URL not configured'
|
||||||
|
: 'No OPENAI_API_KEY configured';
|
||||||
|
this.logger.warn(`${reason} — returning zero vectors`);
|
||||||
return texts.map(() => new Array<number>(this.dimensions).fill(0));
|
return texts.map(() => new Array<number>(this.dimensions).fill(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`${this.baseUrl}/embeddings`, {
|
if (this.backend === 'ollama') {
|
||||||
|
return this.embedBatchOllama(texts);
|
||||||
|
}
|
||||||
|
return this.embedBatchOpenAI(texts);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Ollama backend
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private async embedBatchOllama(texts: string[]): Promise<number[][]> {
|
||||||
|
const baseUrl = this.ollamaBaseUrl!;
|
||||||
|
const results: number[][] = [];
|
||||||
|
|
||||||
|
// Ollama's /api/embeddings endpoint processes one text at a time
|
||||||
|
for (const text of texts) {
|
||||||
|
const response = await fetch(`${baseUrl}/api/embeddings`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ model: this.model, prompt: text }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const body = await response.text();
|
||||||
|
this.logger.error(`Ollama embedding API error: ${response.status} ${body}`);
|
||||||
|
throw new Error(`Ollama embedding API returned ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = (await response.json()) as OllamaEmbeddingResponse;
|
||||||
|
results.push(json.embedding);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// OpenAI-compatible backend
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
private async embedBatchOpenAI(texts: string[]): Promise<number[][]> {
|
||||||
|
const response = await fetch(`${this.openaiBaseUrl}/embeddings`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${this.apiKey}`,
|
Authorization: `Bearer ${this.openaiApiKey}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: this.model,
|
model: this.model,
|
||||||
@@ -63,7 +193,7 @@ export class EmbeddingService implements EmbeddingProvider {
|
|||||||
throw new Error(`Embedding API returned ${response.status}`);
|
throw new Error(`Embedding API returned ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = (await response.json()) as EmbeddingResponse;
|
const json = (await response.json()) as OpenAIEmbeddingResponse;
|
||||||
return json.data.sort((a, b) => a.index - b.index).map((d) => d.embedding);
|
return json.data.sort((a, b) => a.index - b.index).map((d) => d.embedding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,8 +73,8 @@ export class MemoryController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('insights/:id')
|
@Get('insights/:id')
|
||||||
async getInsight(@Param('id') id: string) {
|
async getInsight(@CurrentUser() user: { id: string }, @Param('id') id: string) {
|
||||||
const insight = await this.memory.insights.findById(id);
|
const insight = await this.memory.insights.findById(id, user.id);
|
||||||
if (!insight) throw new NotFoundException('Insight not found');
|
if (!insight) throw new NotFoundException('Insight not found');
|
||||||
return insight;
|
return insight;
|
||||||
}
|
}
|
||||||
@@ -97,8 +97,8 @@ export class MemoryController {
|
|||||||
|
|
||||||
@Delete('insights/:id')
|
@Delete('insights/:id')
|
||||||
@HttpCode(HttpStatus.NO_CONTENT)
|
@HttpCode(HttpStatus.NO_CONTENT)
|
||||||
async removeInsight(@Param('id') id: string) {
|
async removeInsight(@CurrentUser() user: { id: string }, @Param('id') id: string) {
|
||||||
const deleted = await this.memory.insights.remove(id);
|
const deleted = await this.memory.insights.remove(id, user.id);
|
||||||
if (!deleted) throw new NotFoundException('Insight not found');
|
if (!deleted) throw new NotFoundException('Insight not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,34 +5,28 @@ import type { Db } from '@mosaic/db';
|
|||||||
/**
|
/**
|
||||||
* Build a mock Drizzle DB where the select chain supports:
|
* Build a mock Drizzle DB where the select chain supports:
|
||||||
* db.select().from().where() → resolves to `listRows`
|
* db.select().from().where() → resolves to `listRows`
|
||||||
* db.select().from().where().limit(n) → resolves to `singleRow`
|
* db.insert().values().onConflictDoUpdate() → resolves to []
|
||||||
*/
|
*/
|
||||||
function makeMockDb(
|
function makeMockDb(listRows: Array<{ key: string; value: unknown }> = []): Db {
|
||||||
listRows: Array<{ key: string; value: unknown }> = [],
|
|
||||||
singleRow: Array<{ id: string }> = [],
|
|
||||||
): Db {
|
|
||||||
const chainWithLimit = {
|
const chainWithLimit = {
|
||||||
limit: vi.fn().mockResolvedValue(singleRow),
|
limit: vi.fn().mockResolvedValue([]),
|
||||||
then: (resolve: (v: typeof listRows) => unknown) => Promise.resolve(listRows).then(resolve),
|
then: (resolve: (v: typeof listRows) => unknown) => Promise.resolve(listRows).then(resolve),
|
||||||
};
|
};
|
||||||
const selectFrom = {
|
const selectFrom = {
|
||||||
from: vi.fn().mockReturnThis(),
|
from: vi.fn().mockReturnThis(),
|
||||||
where: vi.fn().mockReturnValue(chainWithLimit),
|
where: vi.fn().mockReturnValue(chainWithLimit),
|
||||||
};
|
};
|
||||||
const updateResult = {
|
|
||||||
set: vi.fn().mockReturnThis(),
|
|
||||||
where: vi.fn().mockResolvedValue([]),
|
|
||||||
};
|
|
||||||
const deleteResult = {
|
const deleteResult = {
|
||||||
where: vi.fn().mockResolvedValue([]),
|
where: vi.fn().mockResolvedValue([]),
|
||||||
};
|
};
|
||||||
|
// Single-round-trip upsert chain: insert().values().onConflictDoUpdate()
|
||||||
const insertResult = {
|
const insertResult = {
|
||||||
values: vi.fn().mockResolvedValue([]),
|
values: vi.fn().mockReturnThis(),
|
||||||
|
onConflictDoUpdate: vi.fn().mockResolvedValue([]),
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
select: vi.fn().mockReturnValue(selectFrom),
|
select: vi.fn().mockReturnValue(selectFrom),
|
||||||
update: vi.fn().mockReturnValue(updateResult),
|
|
||||||
delete: vi.fn().mockReturnValue(deleteResult),
|
delete: vi.fn().mockReturnValue(deleteResult),
|
||||||
insert: vi.fn().mockReturnValue(insertResult),
|
insert: vi.fn().mockReturnValue(insertResult),
|
||||||
} as unknown as Db;
|
} as unknown as Db;
|
||||||
@@ -98,23 +92,14 @@ describe('PreferencesService', () => {
|
|||||||
expect(result.message).toContain('platform enforcement');
|
expect(result.message).toContain('platform enforcement');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('upserts a mutable preference and returns success — insert path', async () => {
|
it('upserts a mutable preference and returns success', async () => {
|
||||||
// singleRow=[] → no existing row → insert path
|
// Single-round-trip INSERT … ON CONFLICT DO UPDATE path.
|
||||||
const db = makeMockDb([], []);
|
const db = makeMockDb([]);
|
||||||
const service = new PreferencesService(db);
|
const service = new PreferencesService(db);
|
||||||
const result = await service.set('user-1', 'agent.thinkingLevel', 'high');
|
const result = await service.set('user-1', 'agent.thinkingLevel', 'high');
|
||||||
expect(result.success).toBe(true);
|
expect(result.success).toBe(true);
|
||||||
expect(result.message).toContain('"agent.thinkingLevel"');
|
expect(result.message).toContain('"agent.thinkingLevel"');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('upserts a mutable preference and returns success — update path', async () => {
|
|
||||||
// singleRow has an id → existing row → update path
|
|
||||||
const db = makeMockDb([], [{ id: 'existing-id' }]);
|
|
||||||
const service = new PreferencesService(db);
|
|
||||||
const result = await service.set('user-1', 'agent.thinkingLevel', 'low');
|
|
||||||
expect(result.success).toBe(true);
|
|
||||||
expect(result.message).toContain('"agent.thinkingLevel"');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('reset', () => {
|
describe('reset', () => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Inject, Injectable, Logger } from '@nestjs/common';
|
import { Inject, Injectable, Logger } from '@nestjs/common';
|
||||||
import { eq, and, type Db, preferences as preferencesTable } from '@mosaic/db';
|
import { eq, and, sql, type Db, preferences as preferencesTable } from '@mosaic/db';
|
||||||
import { DB } from '../database/database.module.js';
|
import { DB } from '../database/database.module.js';
|
||||||
|
|
||||||
export const PLATFORM_DEFAULTS: Record<string, unknown> = {
|
export const PLATFORM_DEFAULTS: Record<string, unknown> = {
|
||||||
@@ -88,25 +88,24 @@ export class PreferencesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async upsertPref(userId: string, key: string, value: unknown): Promise<void> {
|
private async upsertPref(userId: string, key: string, value: unknown): Promise<void> {
|
||||||
const existing = await this.db
|
// Single-round-trip upsert using INSERT … ON CONFLICT DO UPDATE.
|
||||||
.select({ id: preferencesTable.id })
|
// Previously this was two queries (SELECT + INSERT/UPDATE), which doubled
|
||||||
.from(preferencesTable)
|
// the DB round-trips and introduced a TOCTOU window under concurrent writes.
|
||||||
.where(and(eq(preferencesTable.userId, userId), eq(preferencesTable.key, key)))
|
await this.db
|
||||||
.limit(1);
|
.insert(preferencesTable)
|
||||||
|
.values({
|
||||||
if (existing.length > 0) {
|
|
||||||
await this.db
|
|
||||||
.update(preferencesTable)
|
|
||||||
.set({ value: value as never, updatedAt: new Date() })
|
|
||||||
.where(and(eq(preferencesTable.userId, userId), eq(preferencesTable.key, key)));
|
|
||||||
} else {
|
|
||||||
await this.db.insert(preferencesTable).values({
|
|
||||||
userId,
|
userId,
|
||||||
key,
|
key,
|
||||||
value: value as never,
|
value: value as never,
|
||||||
mutable: true,
|
mutable: true,
|
||||||
|
})
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: [preferencesTable.userId, preferencesTable.key],
|
||||||
|
set: {
|
||||||
|
value: sql`excluded.value`,
|
||||||
|
updatedAt: sql`now()`,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
this.logger.debug(`Upserted preference "${key}" for user ${userId}`);
|
this.logger.debug(`Upserted preference "${key}" for user ${userId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,30 @@ import type { NextConfig } from 'next';
|
|||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
output: 'standalone',
|
output: 'standalone',
|
||||||
transpilePackages: ['@mosaic/design-tokens'],
|
transpilePackages: ['@mosaic/design-tokens'],
|
||||||
|
|
||||||
|
// Enable gzip/brotli compression for all responses.
|
||||||
|
compress: true,
|
||||||
|
|
||||||
|
// Reduce bundle size: disable source maps in production builds.
|
||||||
|
productionBrowserSourceMaps: false,
|
||||||
|
|
||||||
|
// Image optimisation: allow the gateway origin as an external image source.
|
||||||
|
images: {
|
||||||
|
formats: ['image/avif', 'image/webp'],
|
||||||
|
remotePatterns: [
|
||||||
|
{
|
||||||
|
protocol: 'https',
|
||||||
|
hostname: '**',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
// Experimental: enable React compiler for automatic memoisation (Next 15+).
|
||||||
|
// Falls back gracefully if the compiler plugin is not installed.
|
||||||
|
experimental: {
|
||||||
|
// Turbopack is the default in dev for Next 15; keep it opt-in for now.
|
||||||
|
// turbo: {},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default nextConfig;
|
export default nextConfig;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
"next": "^16.0.0",
|
"next": "^16.0.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
"react-dom": "^19.0.0",
|
"react-dom": "^19.0.0",
|
||||||
|
"react-markdown": "^10.1.0",
|
||||||
"socket.io-client": "^4.8.0",
|
"socket.io-client": "^4.8.0",
|
||||||
"tailwind-merge": "^3.5.0"
|
"tailwind-merge": "^3.5.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,14 +1,27 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { signIn } from '@/lib/auth-client';
|
import { api } from '@/lib/api';
|
||||||
|
import { authClient, signIn } from '@/lib/auth-client';
|
||||||
|
import type { SsoProviderDiscovery } from '@/lib/sso';
|
||||||
|
import { SsoProviderButtons } from '@/components/auth/sso-provider-buttons';
|
||||||
|
|
||||||
export default function LoginPage(): React.ReactElement {
|
export default function LoginPage(): React.ReactElement {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [ssoProviders, setSsoProviders] = useState<SsoProviderDiscovery[]>([]);
|
||||||
|
const [ssoLoadingProviderId, setSsoLoadingProviderId] = useState<
|
||||||
|
SsoProviderDiscovery['id'] | null
|
||||||
|
>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
api<SsoProviderDiscovery[]>('/api/sso/providers')
|
||||||
|
.catch(() => [] as SsoProviderDiscovery[])
|
||||||
|
.then((providers) => setSsoProviders(providers.filter((provider) => provider.configured)));
|
||||||
|
}, []);
|
||||||
|
|
||||||
async function handleSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
|
async function handleSubmit(e: React.FormEvent<HTMLFormElement>): Promise<void> {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -30,6 +43,27 @@ export default function LoginPage(): React.ReactElement {
|
|||||||
router.push('/chat');
|
router.push('/chat');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleSsoSignIn(providerId: SsoProviderDiscovery['id']): Promise<void> {
|
||||||
|
setError(null);
|
||||||
|
setSsoLoadingProviderId(providerId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await authClient.signIn.oauth2({
|
||||||
|
providerId,
|
||||||
|
callbackURL: '/chat',
|
||||||
|
newUserCallbackURL: '/chat',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
setError(result.error.message ?? `Sign in with ${providerId} failed`);
|
||||||
|
setSsoLoadingProviderId(null);
|
||||||
|
}
|
||||||
|
} catch (err: unknown) {
|
||||||
|
setError(err instanceof Error ? err.message : `Sign in with ${providerId} failed`);
|
||||||
|
setSsoLoadingProviderId(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1 className="text-2xl font-semibold">Sign in</h1>
|
<h1 className="text-2xl font-semibold">Sign in</h1>
|
||||||
@@ -86,6 +120,14 @@ export default function LoginPage(): React.ReactElement {
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<SsoProviderButtons
|
||||||
|
providers={ssoProviders}
|
||||||
|
loadingProviderId={ssoLoadingProviderId}
|
||||||
|
onOidcSignIn={(providerId) => {
|
||||||
|
void handleSsoSignIn(providerId);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
<p className="mt-4 text-center text-sm text-text-muted">
|
<p className="mt-4 text-center text-sm text-text-muted">
|
||||||
Don't have an account?{' '}
|
Don't have an account?{' '}
|
||||||
<Link href="/register" className="text-blue-400 hover:text-blue-300">
|
<Link href="/register" className="text-blue-400 hover:text-blue-300">
|
||||||
|
|||||||
@@ -4,18 +4,42 @@ import { useCallback, useEffect, useRef, useState } from 'react';
|
|||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import { destroySocket, getSocket } from '@/lib/socket';
|
import { destroySocket, getSocket } from '@/lib/socket';
|
||||||
import type { Conversation, Message } from '@/lib/types';
|
import type { Conversation, Message } from '@/lib/types';
|
||||||
import { ConversationList } from '@/components/chat/conversation-list';
|
import {
|
||||||
|
ConversationSidebar,
|
||||||
|
type ConversationSidebarRef,
|
||||||
|
} from '@/components/chat/conversation-sidebar';
|
||||||
import { MessageBubble } from '@/components/chat/message-bubble';
|
import { MessageBubble } from '@/components/chat/message-bubble';
|
||||||
import { ChatInput } from '@/components/chat/chat-input';
|
import { ChatInput } from '@/components/chat/chat-input';
|
||||||
import { StreamingMessage } from '@/components/chat/streaming-message';
|
import { StreamingMessage } from '@/components/chat/streaming-message';
|
||||||
|
|
||||||
|
interface ModelInfo {
|
||||||
|
id: string;
|
||||||
|
provider: string;
|
||||||
|
name: string;
|
||||||
|
reasoning: boolean;
|
||||||
|
contextWindow: number;
|
||||||
|
maxTokens: number;
|
||||||
|
inputTypes: ('text' | 'image')[];
|
||||||
|
cost: { input: number; output: number; cacheRead: number; cacheWrite: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProviderInfo {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
available: boolean;
|
||||||
|
models: ModelInfo[];
|
||||||
|
}
|
||||||
|
|
||||||
export default function ChatPage(): React.ReactElement {
|
export default function ChatPage(): React.ReactElement {
|
||||||
const [conversations, setConversations] = useState<Conversation[]>([]);
|
|
||||||
const [activeId, setActiveId] = useState<string | null>(null);
|
const [activeId, setActiveId] = useState<string | null>(null);
|
||||||
const [messages, setMessages] = useState<Message[]>([]);
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
const [streamingText, setStreamingText] = useState('');
|
const [streamingText, setStreamingText] = useState('');
|
||||||
const [isStreaming, setIsStreaming] = useState(false);
|
const [isStreaming, setIsStreaming] = useState(false);
|
||||||
|
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
|
||||||
|
const [models, setModels] = useState<ModelInfo[]>([]);
|
||||||
|
const [selectedModelId, setSelectedModelId] = useState('');
|
||||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||||
|
const sidebarRef = useRef<ConversationSidebarRef>(null);
|
||||||
|
|
||||||
// Track the active conversation ID in a ref so socket event handlers always
|
// Track the active conversation ID in a ref so socket event handlers always
|
||||||
// see the current value without needing to be re-registered.
|
// see the current value without needing to be re-registered.
|
||||||
@@ -26,11 +50,30 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
// without stale-closure issues.
|
// without stale-closure issues.
|
||||||
const streamingTextRef = useRef('');
|
const streamingTextRef = useRef('');
|
||||||
|
|
||||||
// Load conversations on mount
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
api<Conversation[]>('/api/conversations')
|
const savedState = window.localStorage.getItem('mosaic-sidebar-open');
|
||||||
.then(setConversations)
|
if (savedState !== null) {
|
||||||
.catch(() => {});
|
setIsSidebarOpen(savedState === 'true');
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.localStorage.setItem('mosaic-sidebar-open', String(isSidebarOpen));
|
||||||
|
}, [isSidebarOpen]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
api<ProviderInfo[]>('/api/providers')
|
||||||
|
.then((providers) => {
|
||||||
|
const availableModels = providers
|
||||||
|
.filter((provider) => provider.available)
|
||||||
|
.flatMap((provider) => provider.models);
|
||||||
|
setModels(availableModels);
|
||||||
|
setSelectedModelId((current) => current || availableModels[0]?.id || '');
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
setModels([]);
|
||||||
|
setSelectedModelId('');
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Load messages when active conversation changes
|
// Load messages when active conversation changes
|
||||||
@@ -91,6 +134,7 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
sidebarRef.current?.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,58 +175,27 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleNewConversation = useCallback(async () => {
|
const handleNewConversation = useCallback(async (projectId?: string | null) => {
|
||||||
const conv = await api<Conversation>('/api/conversations', {
|
const conv = await api<Conversation>('/api/conversations', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { title: 'New conversation' },
|
body: { title: 'New conversation', projectId: projectId ?? null },
|
||||||
});
|
});
|
||||||
setConversations((prev) => [conv, ...prev]);
|
|
||||||
|
sidebarRef.current?.addConversation({
|
||||||
|
id: conv.id,
|
||||||
|
title: conv.title,
|
||||||
|
projectId: conv.projectId,
|
||||||
|
updatedAt: conv.updatedAt,
|
||||||
|
archived: conv.archived,
|
||||||
|
});
|
||||||
|
|
||||||
setActiveId(conv.id);
|
setActiveId(conv.id);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
|
setIsSidebarOpen(true);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleRename = useCallback(async (id: string, title: string) => {
|
|
||||||
const updated = await api<Conversation>(`/api/conversations/${id}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
body: { title },
|
|
||||||
});
|
|
||||||
setConversations((prev) => prev.map((c) => (c.id === id ? updated : c)));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handleDelete = useCallback(
|
|
||||||
async (id: string) => {
|
|
||||||
try {
|
|
||||||
await api<void>(`/api/conversations/${id}`, { method: 'DELETE' });
|
|
||||||
setConversations((prev) => prev.filter((c) => c.id !== id));
|
|
||||||
if (activeId === id) {
|
|
||||||
setActiveId(null);
|
|
||||||
setMessages([]);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('[ChatPage] Failed to delete conversation:', err);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[activeId],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleArchive = useCallback(
|
|
||||||
async (id: string, archived: boolean) => {
|
|
||||||
const updated = await api<Conversation>(`/api/conversations/${id}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
body: { archived },
|
|
||||||
});
|
|
||||||
setConversations((prev) => prev.map((c) => (c.id === id ? updated : c)));
|
|
||||||
// If archiving the active conversation, deselect it
|
|
||||||
if (archived && activeId === id) {
|
|
||||||
setActiveId(null);
|
|
||||||
setMessages([]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[activeId],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleSend = useCallback(
|
const handleSend = useCallback(
|
||||||
async (content: string) => {
|
async (content: string, options?: { modelId?: string }) => {
|
||||||
let convId = activeId;
|
let convId = activeId;
|
||||||
|
|
||||||
// Auto-create conversation if none selected
|
// Auto-create conversation if none selected
|
||||||
@@ -192,25 +205,24 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { title: autoTitle },
|
body: { title: autoTitle },
|
||||||
});
|
});
|
||||||
setConversations((prev) => [conv, ...prev]);
|
sidebarRef.current?.addConversation({
|
||||||
|
id: conv.id,
|
||||||
|
title: conv.title,
|
||||||
|
projectId: conv.projectId,
|
||||||
|
updatedAt: conv.updatedAt,
|
||||||
|
archived: conv.archived,
|
||||||
|
});
|
||||||
setActiveId(conv.id);
|
setActiveId(conv.id);
|
||||||
convId = conv.id;
|
convId = conv.id;
|
||||||
} else {
|
} else if (messages.length === 0) {
|
||||||
// Auto-title: if the active conversation still has the default "New
|
// Auto-title the initial placeholder conversation from the first user message.
|
||||||
// conversation" title and this is the first message, update the title
|
const autoTitle = content.slice(0, 60);
|
||||||
// from the message content.
|
api<Conversation>(`/api/conversations/${convId}`, {
|
||||||
const activeConv = conversations.find((c) => c.id === convId);
|
method: 'PATCH',
|
||||||
if (activeConv?.title === 'New conversation' && messages.length === 0) {
|
body: { title: autoTitle },
|
||||||
const autoTitle = content.slice(0, 60);
|
})
|
||||||
api<Conversation>(`/api/conversations/${convId}`, {
|
.then(() => sidebarRef.current?.refresh())
|
||||||
method: 'PATCH',
|
.catch(() => {});
|
||||||
body: { title: autoTitle },
|
|
||||||
})
|
|
||||||
.then((updated) => {
|
|
||||||
setConversations((prev) => prev.map((c) => (c.id === convId ? updated : c)));
|
|
||||||
})
|
|
||||||
.catch(() => {});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimistic user message in local UI state
|
// Optimistic user message in local UI state
|
||||||
@@ -241,24 +253,67 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
if (!socket.connected) {
|
if (!socket.connected) {
|
||||||
socket.connect();
|
socket.connect();
|
||||||
}
|
}
|
||||||
socket.emit('message', { conversationId: convId, content });
|
socket.emit('message', {
|
||||||
|
conversationId: convId,
|
||||||
|
content,
|
||||||
|
modelId: (options?.modelId ?? selectedModelId) || undefined,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[activeId, conversations, messages],
|
[activeId, messages, selectedModelId],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="-m-6 flex h-[calc(100vh-3.5rem)]">
|
<div
|
||||||
<ConversationList
|
className="-m-6 flex h-[calc(100vh-3.5rem)] overflow-hidden"
|
||||||
conversations={conversations}
|
style={{ background: 'var(--bg-deep, var(--color-surface-bg, #0a0f1a))' }}
|
||||||
activeId={activeId}
|
>
|
||||||
onSelect={setActiveId}
|
<ConversationSidebar
|
||||||
onNew={handleNewConversation}
|
ref={sidebarRef}
|
||||||
onRename={handleRename}
|
isOpen={isSidebarOpen}
|
||||||
onDelete={handleDelete}
|
onClose={() => setIsSidebarOpen(false)}
|
||||||
onArchive={handleArchive}
|
currentConversationId={activeId}
|
||||||
|
onSelectConversation={(conversationId) => {
|
||||||
|
setActiveId(conversationId);
|
||||||
|
setMessages([]);
|
||||||
|
if (conversationId && window.innerWidth < 768) {
|
||||||
|
setIsSidebarOpen(false);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onNewConversation={(projectId) => {
|
||||||
|
void handleNewConversation(projectId);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="flex flex-1 flex-col">
|
<div className="flex min-w-0 flex-1 flex-col">
|
||||||
|
<div
|
||||||
|
className="flex items-center gap-3 border-b px-4 py-3"
|
||||||
|
style={{ borderColor: 'var(--border)' }}
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setIsSidebarOpen((open) => !open)}
|
||||||
|
className="rounded-lg border p-2 transition-colors"
|
||||||
|
style={{
|
||||||
|
borderColor: 'var(--border)',
|
||||||
|
background: 'var(--surface)',
|
||||||
|
color: 'var(--text)',
|
||||||
|
}}
|
||||||
|
aria-label={isSidebarOpen ? 'Close conversation sidebar' : 'Open conversation sidebar'}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor">
|
||||||
|
<path strokeWidth="2" strokeLinecap="round" d="M4 7h16M4 12h16M4 17h16" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<div>
|
||||||
|
<h1 className="text-sm font-semibold" style={{ color: 'var(--text)' }}>
|
||||||
|
Mosaic Chat
|
||||||
|
</h1>
|
||||||
|
<p className="text-xs" style={{ color: 'var(--muted)' }}>
|
||||||
|
{activeId ? 'Active conversation selected' : 'Choose or start a conversation'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{activeId ? (
|
{activeId ? (
|
||||||
<>
|
<>
|
||||||
<div className="flex-1 space-y-4 overflow-y-auto p-6">
|
<div className="flex-1 space-y-4 overflow-y-auto p-6">
|
||||||
@@ -268,19 +323,36 @@ export default function ChatPage(): React.ReactElement {
|
|||||||
{isStreaming && <StreamingMessage text={streamingText} />}
|
{isStreaming && <StreamingMessage text={streamingText} />}
|
||||||
<div ref={messagesEndRef} />
|
<div ref={messagesEndRef} />
|
||||||
</div>
|
</div>
|
||||||
<ChatInput onSend={handleSend} disabled={isStreaming} />
|
<ChatInput
|
||||||
|
onSend={handleSend}
|
||||||
|
isStreaming={isStreaming}
|
||||||
|
models={models}
|
||||||
|
selectedModelId={selectedModelId}
|
||||||
|
onModelChange={setSelectedModelId}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-1 items-center justify-center">
|
<div className="flex flex-1 items-center justify-center px-6">
|
||||||
<div className="text-center">
|
<div
|
||||||
<h2 className="text-lg font-medium text-text-secondary">Welcome to Mosaic Chat</h2>
|
className="max-w-md rounded-2xl border px-8 py-10 text-center"
|
||||||
<p className="mt-1 text-sm text-text-muted">
|
style={{
|
||||||
|
borderColor: 'var(--border)',
|
||||||
|
background: 'var(--surface)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<h2 className="text-lg font-medium" style={{ color: 'var(--text)' }}>
|
||||||
|
Welcome to Mosaic Chat
|
||||||
|
</h2>
|
||||||
|
<p className="mt-1 text-sm" style={{ color: 'var(--muted)' }}>
|
||||||
Select a conversation or start a new one
|
Select a conversation or start a new one
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={handleNewConversation}
|
onClick={() => {
|
||||||
className="mt-4 rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-blue-700"
|
void handleNewConversation();
|
||||||
|
}}
|
||||||
|
className="mt-4 rounded-lg px-4 py-2 text-sm font-medium text-white transition-colors"
|
||||||
|
style={{ background: 'var(--primary)' }}
|
||||||
>
|
>
|
||||||
Start new conversation
|
Start new conversation
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import { authClient, useSession } from '@/lib/auth-client';
|
import { authClient, useSession } from '@/lib/auth-client';
|
||||||
|
import type { SsoProviderDiscovery } from '@/lib/sso';
|
||||||
|
import { SsoProviderSection } from '@/components/settings/sso-provider-section';
|
||||||
|
|
||||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -424,7 +426,9 @@ function NotificationsTab(): React.ReactElement {
|
|||||||
|
|
||||||
function ProvidersTab(): React.ReactElement {
|
function ProvidersTab(): React.ReactElement {
|
||||||
const [providers, setProviders] = useState<ProviderInfo[]>([]);
|
const [providers, setProviders] = useState<ProviderInfo[]>([]);
|
||||||
|
const [ssoProviders, setSsoProviders] = useState<SsoProviderDiscovery[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [ssoLoading, setSsoLoading] = useState(true);
|
||||||
const [testStatuses, setTestStatuses] = useState<Record<string, ProviderTestStatus>>({});
|
const [testStatuses, setTestStatuses] = useState<Record<string, ProviderTestStatus>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -434,6 +438,13 @@ function ProvidersTab(): React.ReactElement {
|
|||||||
.finally(() => setLoading(false));
|
.finally(() => setLoading(false));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
api<SsoProviderDiscovery[]>('/api/sso/providers')
|
||||||
|
.catch(() => [] as SsoProviderDiscovery[])
|
||||||
|
.then((providers) => setSsoProviders(providers))
|
||||||
|
.finally(() => setSsoLoading(false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
const testConnection = useCallback(async (providerId: string): Promise<void> => {
|
const testConnection = useCallback(async (providerId: string): Promise<void> => {
|
||||||
setTestStatuses((prev) => ({
|
setTestStatuses((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
@@ -464,35 +475,44 @@ function ProvidersTab(): React.ReactElement {
|
|||||||
.find((m) => providers.find((p) => p.id === m.provider)?.available);
|
.find((m) => providers.find((p) => p.id === m.provider)?.available);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="space-y-4">
|
<section className="space-y-6">
|
||||||
<h2 className="text-lg font-medium text-text-secondary">LLM Providers</h2>
|
<div className="space-y-4">
|
||||||
{loading ? (
|
<h2 className="text-lg font-medium text-text-secondary">SSO Providers</h2>
|
||||||
<p className="text-sm text-text-muted">Loading providers...</p>
|
<SsoProviderSection providers={ssoProviders} loading={ssoLoading} />
|
||||||
) : providers.length === 0 ? (
|
</div>
|
||||||
<div className="rounded-lg border border-surface-border bg-surface-card p-4">
|
|
||||||
<p className="text-sm text-text-muted">
|
<div className="space-y-4">
|
||||||
No providers configured. Set{' '}
|
<h2 className="text-lg font-medium text-text-secondary">LLM Providers</h2>
|
||||||
<code className="rounded bg-surface-elevated px-1 py-0.5 text-xs">OLLAMA_BASE_URL</code>{' '}
|
{loading ? (
|
||||||
or{' '}
|
<p className="text-sm text-text-muted">Loading providers...</p>
|
||||||
<code className="rounded bg-surface-elevated px-1 py-0.5 text-xs">
|
) : providers.length === 0 ? (
|
||||||
MOSAIC_CUSTOM_PROVIDERS
|
<div className="rounded-lg border border-surface-border bg-surface-card p-4">
|
||||||
</code>{' '}
|
<p className="text-sm text-text-muted">
|
||||||
to add providers.
|
No providers configured. Set{' '}
|
||||||
</p>
|
<code className="rounded bg-surface-elevated px-1 py-0.5 text-xs">
|
||||||
</div>
|
OLLAMA_BASE_URL
|
||||||
) : (
|
</code>{' '}
|
||||||
<div className="space-y-4">
|
or{' '}
|
||||||
{providers.map((provider) => (
|
<code className="rounded bg-surface-elevated px-1 py-0.5 text-xs">
|
||||||
<ProviderCard
|
MOSAIC_CUSTOM_PROVIDERS
|
||||||
key={provider.id}
|
</code>{' '}
|
||||||
provider={provider}
|
to add providers.
|
||||||
defaultModel={defaultModel}
|
</p>
|
||||||
testStatus={testStatuses[provider.id] ?? { state: 'idle' }}
|
</div>
|
||||||
onTest={() => void testConnection(provider.id)}
|
) : (
|
||||||
/>
|
<div className="space-y-4">
|
||||||
))}
|
{providers.map((provider) => (
|
||||||
</div>
|
<ProviderCard
|
||||||
)}
|
key={provider.id}
|
||||||
|
provider={provider}
|
||||||
|
defaultModel={defaultModel}
|
||||||
|
testStatus={testStatuses[provider.id] ?? { state: 'idle' }}
|
||||||
|
onTest={() => void testConnection(provider.id)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
77
apps/web/src/app/auth/provider/[provider]/page.tsx
Normal file
77
apps/web/src/app/auth/provider/[provider]/page.tsx
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useParams, useSearchParams } from 'next/navigation';
|
||||||
|
import { signIn } from '@/lib/auth-client';
|
||||||
|
import { getSsoProvider } from '@/lib/sso-providers';
|
||||||
|
|
||||||
|
export default function AuthProviderRedirectPage(): React.ReactElement {
|
||||||
|
const params = useParams<{ provider: string }>();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const providerId = typeof params.provider === 'string' ? params.provider : '';
|
||||||
|
const provider = getSsoProvider(providerId);
|
||||||
|
const callbackURL = searchParams.get('callbackURL') ?? '/chat';
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const currentProvider = provider;
|
||||||
|
|
||||||
|
if (!currentProvider) {
|
||||||
|
setError('Unknown SSO provider.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentProvider.enabled) {
|
||||||
|
setError(`${currentProvider.buttonLabel} is not enabled in this deployment.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const activeProvider = currentProvider;
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
async function redirectToProvider(): Promise<void> {
|
||||||
|
const result = await signIn.oauth2({
|
||||||
|
providerId: activeProvider.id,
|
||||||
|
callbackURL,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!cancelled && result?.error) {
|
||||||
|
setError(result.error.message ?? `${activeProvider.buttonLabel} sign in failed.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void redirectToProvider();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [callbackURL, provider]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx-auto flex min-h-[50vh] max-w-md flex-col justify-center">
|
||||||
|
<h1 className="text-2xl font-semibold text-text-primary">Single sign-on</h1>
|
||||||
|
<p className="mt-2 text-sm text-text-secondary">
|
||||||
|
{provider
|
||||||
|
? `Redirecting you to ${provider.buttonLabel.replace('Continue with ', '')}...`
|
||||||
|
: 'Preparing your sign-in request...'}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{error ? (
|
||||||
|
<div className="mt-6 rounded-lg border border-error/30 bg-error/10 px-4 py-3 text-sm text-error">
|
||||||
|
<p>{error}</p>
|
||||||
|
<Link
|
||||||
|
href="/login"
|
||||||
|
className="mt-3 inline-block font-medium text-blue-400 hover:text-blue-300"
|
||||||
|
>
|
||||||
|
Return to login
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="mt-6 rounded-lg border border-surface-border bg-surface-elevated px-4 py-3 text-sm text-text-secondary">
|
||||||
|
If the redirect does not start automatically, return to the login page and try again.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,101 +1,186 @@
|
|||||||
@import 'tailwindcss';
|
@import 'tailwindcss';
|
||||||
|
|
||||||
/*
|
/* =============================================================================
|
||||||
* Mosaic Stack design tokens mapped to Tailwind v4 theme.
|
MOSAIC DESIGN SYSTEM — Reference token system from dashboard design
|
||||||
* Source: @mosaic/design-tokens (AD-13)
|
============================================================================= */
|
||||||
* Fonts: Outfit (sans), Fira Code (mono)
|
|
||||||
* Palette: deep blue-grays + blue/purple/teal accents
|
|
||||||
* Default: dark theme
|
|
||||||
*/
|
|
||||||
|
|
||||||
@theme {
|
/* -----------------------------------------------------------------------------
|
||||||
/* ─── Fonts ─── */
|
Primitive Tokens (Dark-first — dark is the default theme)
|
||||||
--font-sans: 'Outfit', system-ui, -apple-system, sans-serif;
|
----------------------------------------------------------------------------- */
|
||||||
--font-mono: 'Fira Code', ui-monospace, Menlo, monospace;
|
:root {
|
||||||
|
/* Mosaic design tokens — dark palette (default) */
|
||||||
|
--ms-bg-950: #080b12;
|
||||||
|
--ms-bg-900: #0f141d;
|
||||||
|
--ms-bg-850: #151b26;
|
||||||
|
--ms-surface-800: #1b2331;
|
||||||
|
--ms-surface-750: #232d3f;
|
||||||
|
--ms-border-700: #2f3b52;
|
||||||
|
--ms-text-100: #eef3ff;
|
||||||
|
--ms-text-300: #c5d0e6;
|
||||||
|
--ms-text-500: #8f9db7;
|
||||||
|
--ms-blue-500: #2f80ff;
|
||||||
|
--ms-blue-400: #56a0ff;
|
||||||
|
--ms-red-500: #e5484d;
|
||||||
|
--ms-red-400: #f06a6f;
|
||||||
|
--ms-purple-500: #8b5cf6;
|
||||||
|
--ms-purple-400: #a78bfa;
|
||||||
|
--ms-teal-500: #14b8a6;
|
||||||
|
--ms-teal-400: #2dd4bf;
|
||||||
|
--ms-amber-500: #f59e0b;
|
||||||
|
--ms-amber-400: #fbbf24;
|
||||||
|
--ms-pink-500: #ec4899;
|
||||||
|
--ms-emerald-500: #10b981;
|
||||||
|
--ms-orange-500: #f97316;
|
||||||
|
--ms-cyan-500: #06b6d4;
|
||||||
|
--ms-indigo-500: #6366f1;
|
||||||
|
|
||||||
/* ─── Neutral blue-gray scale ─── */
|
/* Semantic aliases — dark theme is default */
|
||||||
--color-gray-50: #f0f2f5;
|
--bg: var(--ms-bg-900);
|
||||||
--color-gray-100: #dce0e8;
|
--bg-deep: var(--ms-bg-950);
|
||||||
--color-gray-200: #b8c0cc;
|
--bg-mid: var(--ms-bg-850);
|
||||||
--color-gray-300: #8e99a9;
|
--surface: var(--ms-surface-800);
|
||||||
--color-gray-400: #6b7a8d;
|
--surface-2: var(--ms-surface-750);
|
||||||
--color-gray-500: #4e5d70;
|
--border: var(--ms-border-700);
|
||||||
--color-gray-600: #3b4859;
|
--text: var(--ms-text-100);
|
||||||
--color-gray-700: #2a3544;
|
--text-2: var(--ms-text-300);
|
||||||
--color-gray-800: #1c2433;
|
--muted: var(--ms-text-500);
|
||||||
--color-gray-900: #111827;
|
--primary: var(--ms-blue-500);
|
||||||
--color-gray-950: #0a0f1a;
|
--primary-l: var(--ms-blue-400);
|
||||||
|
--danger: var(--ms-red-500);
|
||||||
|
--success: var(--ms-teal-500);
|
||||||
|
--warn: var(--ms-amber-500);
|
||||||
|
--purple: var(--ms-purple-500);
|
||||||
|
|
||||||
/* ─── Primary — blue ─── */
|
/* Typography */
|
||||||
--color-blue-50: #eff4ff;
|
--font: var(--font-outfit, 'Outfit'), system-ui, sans-serif;
|
||||||
--color-blue-100: #dae5ff;
|
--mono: var(--font-fira-code, 'Fira Code'), 'Cascadia Code', monospace;
|
||||||
--color-blue-200: #bdd1ff;
|
|
||||||
--color-blue-300: #8fb4ff;
|
|
||||||
--color-blue-400: #5b8bff;
|
|
||||||
--color-blue-500: #3b6cf7;
|
|
||||||
--color-blue-600: #2551e0;
|
|
||||||
--color-blue-700: #1d40c0;
|
|
||||||
--color-blue-800: #1e369c;
|
|
||||||
--color-blue-900: #1e317b;
|
|
||||||
--color-blue-950: #162050;
|
|
||||||
|
|
||||||
/* ─── Accent — purple ─── */
|
/* Radius scale */
|
||||||
--color-purple-50: #f3f0ff;
|
--r: 8px;
|
||||||
--color-purple-100: #e7dfff;
|
--r-sm: 5px;
|
||||||
--color-purple-200: #d2c3ff;
|
--r-lg: 12px;
|
||||||
--color-purple-300: #b49aff;
|
--r-xl: 16px;
|
||||||
--color-purple-400: #9466ff;
|
|
||||||
--color-purple-500: #7c3aed;
|
|
||||||
--color-purple-600: #6d28d9;
|
|
||||||
--color-purple-700: #5b21b6;
|
|
||||||
--color-purple-800: #4c1d95;
|
|
||||||
--color-purple-900: #3b1578;
|
|
||||||
--color-purple-950: #230d4d;
|
|
||||||
|
|
||||||
/* ─── Accent — teal ─── */
|
/* Layout dimensions */
|
||||||
--color-teal-50: #effcf9;
|
--sidebar-w: 260px;
|
||||||
--color-teal-100: #d0f7ef;
|
--topbar-h: 56px;
|
||||||
--color-teal-200: #a4eddf;
|
--terminal-h: 220px;
|
||||||
--color-teal-300: #6fddcb;
|
|
||||||
--color-teal-400: #3ec5b2;
|
|
||||||
--color-teal-500: #25aa99;
|
|
||||||
--color-teal-600: #1c897e;
|
|
||||||
--color-teal-700: #1b6e66;
|
|
||||||
--color-teal-800: #1a5853;
|
|
||||||
--color-teal-900: #194945;
|
|
||||||
--color-teal-950: #082d2b;
|
|
||||||
|
|
||||||
/* ─── Semantic surface tokens ─── */
|
/* Easing */
|
||||||
--color-surface-bg: #0a0f1a;
|
--ease: cubic-bezier(0.16, 1, 0.3, 1);
|
||||||
--color-surface-card: #111827;
|
|
||||||
--color-surface-elevated: #1c2433;
|
|
||||||
--color-surface-border: #2a3544;
|
|
||||||
|
|
||||||
/* ─── Semantic text tokens ─── */
|
/* Legacy shadow tokens (retained for component compat) */
|
||||||
--color-text-primary: #f0f2f5;
|
--shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.3);
|
||||||
--color-text-secondary: #8e99a9;
|
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.4), 0 2px 4px -2px rgb(0 0 0 / 0.3);
|
||||||
--color-text-muted: #6b7a8d;
|
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.5), 0 4px 6px -4px rgb(0 0 0 / 0.4);
|
||||||
|
|
||||||
/* ─── Status colors ─── */
|
|
||||||
--color-success: #22c55e;
|
|
||||||
--color-warning: #f59e0b;
|
|
||||||
--color-error: #ef4444;
|
|
||||||
--color-info: #3b82f6;
|
|
||||||
|
|
||||||
/* ─── Sidebar width ─── */
|
|
||||||
--spacing-sidebar: 16rem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ─── Base styles ─── */
|
[data-theme='light'] {
|
||||||
body {
|
--ms-bg-950: #f8faff;
|
||||||
background-color: var(--color-surface-bg);
|
--ms-bg-900: #f0f4fc;
|
||||||
color: var(--color-text-primary);
|
--ms-bg-850: #e8edf8;
|
||||||
font-family: var(--font-sans);
|
--ms-surface-800: #dde4f2;
|
||||||
|
--ms-surface-750: #d0d9ec;
|
||||||
|
--ms-border-700: #b8c4de;
|
||||||
|
--ms-text-100: #0f141d;
|
||||||
|
--ms-text-300: #2f3b52;
|
||||||
|
--ms-text-500: #5a6a87;
|
||||||
|
--bg: var(--ms-bg-900);
|
||||||
|
--bg-deep: var(--ms-bg-950);
|
||||||
|
--bg-mid: var(--ms-bg-850);
|
||||||
|
--surface: var(--ms-surface-800);
|
||||||
|
--surface-2: var(--ms-surface-750);
|
||||||
|
--border: var(--ms-border-700);
|
||||||
|
--text: var(--ms-text-100);
|
||||||
|
--text-2: var(--ms-text-300);
|
||||||
|
--muted: var(--ms-text-500);
|
||||||
|
--shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05), 0 1px 3px 0 rgb(0 0 0 / 0.05);
|
||||||
|
--shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.08), 0 2px 4px -2px rgb(0 0 0 / 0.06);
|
||||||
|
--shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
@theme {
|
||||||
|
--font-sans: var(--font);
|
||||||
|
--font-mono: var(--mono);
|
||||||
|
--color-surface-bg: var(--bg);
|
||||||
|
--color-surface-card: var(--surface);
|
||||||
|
--color-surface-elevated: var(--surface-2);
|
||||||
|
--color-surface-border: var(--border);
|
||||||
|
--color-text-primary: var(--text);
|
||||||
|
--color-text-secondary: var(--text-2);
|
||||||
|
--color-text-muted: var(--muted);
|
||||||
|
--color-accent: var(--primary);
|
||||||
|
--color-success: var(--success);
|
||||||
|
--color-warning: var(--warn);
|
||||||
|
--color-error: var(--danger);
|
||||||
|
--color-info: var(--primary-l);
|
||||||
|
--spacing-sidebar: var(--sidebar-w);
|
||||||
|
}
|
||||||
|
|
||||||
|
*,
|
||||||
|
*::before,
|
||||||
|
*::after {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
font-size: 15px;
|
||||||
|
font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11';
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
-moz-osx-font-smoothing: grayscale;
|
-moz-osx-font-smoothing: grayscale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ─── Scrollbar styling ─── */
|
body {
|
||||||
|
font-family: var(--font);
|
||||||
|
background: var(--bg);
|
||||||
|
color: var(--text);
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: inherit;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
font-family: inherit;
|
||||||
|
cursor: pointer;
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
input,
|
||||||
|
select,
|
||||||
|
textarea {
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
ul {
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
body::before {
|
||||||
|
content: '';
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
z-index: 9999;
|
||||||
|
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='1'/%3E%3C/svg%3E");
|
||||||
|
opacity: 0.025;
|
||||||
|
}
|
||||||
|
|
||||||
|
@layer base {
|
||||||
|
:focus-visible {
|
||||||
|
outline: 2px solid var(--ms-blue-400);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
:focus:not(:focus-visible) {
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
::-webkit-scrollbar {
|
::-webkit-scrollbar {
|
||||||
width: 6px;
|
width: 6px;
|
||||||
height: 6px;
|
height: 6px;
|
||||||
@@ -106,10 +191,96 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
::-webkit-scrollbar-thumb {
|
::-webkit-scrollbar-thumb {
|
||||||
background: var(--color-gray-600);
|
background: var(--border);
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
::-webkit-scrollbar-thumb:hover {
|
::-webkit-scrollbar-thumb:hover {
|
||||||
background: var(--color-gray-500);
|
background: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
* {
|
||||||
|
scrollbar-width: thin;
|
||||||
|
scrollbar-color: var(--border) transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-shell {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: var(--sidebar-w) 1fr;
|
||||||
|
grid-template-rows: var(--topbar-h) 1fr;
|
||||||
|
height: 100vh;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-header {
|
||||||
|
grid-column: 1 / -1;
|
||||||
|
grid-row: 1;
|
||||||
|
background: var(--bg-deep);
|
||||||
|
border-bottom: 1px solid var(--border);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 0 20px;
|
||||||
|
gap: 12px;
|
||||||
|
z-index: 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-sidebar {
|
||||||
|
grid-column: 1;
|
||||||
|
grid-row: 2;
|
||||||
|
background: var(--bg-deep);
|
||||||
|
border-right: 1px solid var(--border);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-main {
|
||||||
|
grid-column: 2;
|
||||||
|
grid-row: 2;
|
||||||
|
background: var(--bg);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 767px) {
|
||||||
|
.app-shell {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-sidebar {
|
||||||
|
position: fixed;
|
||||||
|
left: 0;
|
||||||
|
top: var(--topbar-h);
|
||||||
|
bottom: 0;
|
||||||
|
width: 240px;
|
||||||
|
z-index: 150;
|
||||||
|
transform: translateX(-100%);
|
||||||
|
transition: transform 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-sidebar[data-mobile-open='true'] {
|
||||||
|
transform: translateX(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-main,
|
||||||
|
.app-header {
|
||||||
|
grid-column: 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 768px) and (max-width: 1023px) {
|
||||||
|
.app-shell[data-sidebar-hidden='true'] {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-shell[data-sidebar-hidden='true'] .app-sidebar {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-shell[data-sidebar-hidden='true'] .app-main,
|
||||||
|
.app-shell[data-sidebar-hidden='true'] .app-header {
|
||||||
|
grid-column: 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,41 @@
|
|||||||
|
import type { Metadata } from 'next';
|
||||||
import type { ReactNode } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
import { Outfit, Fira_Code } from 'next/font/google';
|
import { ThemeProvider } from '@/providers/theme-provider';
|
||||||
import './globals.css';
|
import './globals.css';
|
||||||
|
|
||||||
const outfit = Outfit({
|
export const metadata: Metadata = {
|
||||||
subsets: ['latin'],
|
|
||||||
variable: '--font-sans',
|
|
||||||
display: 'swap',
|
|
||||||
weight: ['300', '400', '500', '600', '700'],
|
|
||||||
});
|
|
||||||
|
|
||||||
const firaCode = Fira_Code({
|
|
||||||
subsets: ['latin'],
|
|
||||||
variable: '--font-mono',
|
|
||||||
display: 'swap',
|
|
||||||
weight: ['400', '500', '700'],
|
|
||||||
});
|
|
||||||
|
|
||||||
export const metadata = {
|
|
||||||
title: 'Mosaic',
|
title: 'Mosaic',
|
||||||
description: 'Mosaic Stack Dashboard',
|
description: 'Mosaic Stack Dashboard',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function themeScript(): string {
|
||||||
|
return `
|
||||||
|
(function () {
|
||||||
|
try {
|
||||||
|
var theme = window.localStorage.getItem('mosaic-theme') || 'dark';
|
||||||
|
document.documentElement.setAttribute('data-theme', theme === 'light' ? 'light' : 'dark');
|
||||||
|
} catch (error) {
|
||||||
|
document.documentElement.setAttribute('data-theme', 'dark');
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
export default function RootLayout({ children }: { children: ReactNode }): React.ReactElement {
|
export default function RootLayout({ children }: { children: ReactNode }): React.ReactElement {
|
||||||
return (
|
return (
|
||||||
<html lang="en" className={`dark ${outfit.variable} ${firaCode.variable}`}>
|
<html lang="en" suppressHydrationWarning>
|
||||||
<body>{children}</body>
|
<head>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="anonymous" />
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&family=Fira+Code:wght@400;500&display=swap"
|
||||||
|
/>
|
||||||
|
<script dangerouslySetInnerHTML={{ __html: themeScript() }} />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<ThemeProvider>{children}</ThemeProvider>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
45
apps/web/src/components/auth/sso-provider-buttons.spec.tsx
Normal file
45
apps/web/src/components/auth/sso-provider-buttons.spec.tsx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { describe, expect, it, vi } from 'vitest';
|
||||||
|
import { renderToStaticMarkup } from 'react-dom/server';
|
||||||
|
import { SsoProviderButtons } from './sso-provider-buttons.js';
|
||||||
|
|
||||||
|
describe('SsoProviderButtons', () => {
|
||||||
|
it('renders OIDC sign-in buttons and SAML fallback links', () => {
|
||||||
|
const html = renderToStaticMarkup(
|
||||||
|
<SsoProviderButtons
|
||||||
|
providers={[
|
||||||
|
{
|
||||||
|
id: 'workos',
|
||||||
|
name: 'WorkOS',
|
||||||
|
protocols: ['oidc'],
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'oidc',
|
||||||
|
callbackPath: '/api/auth/oauth2/callback/workos',
|
||||||
|
teamSync: { enabled: true, claim: 'organization_id' },
|
||||||
|
samlFallback: { configured: false, loginUrl: null },
|
||||||
|
warnings: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'keycloak',
|
||||||
|
name: 'Keycloak',
|
||||||
|
protocols: ['oidc', 'saml'],
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'saml',
|
||||||
|
callbackPath: null,
|
||||||
|
teamSync: { enabled: true, claim: 'groups' },
|
||||||
|
samlFallback: {
|
||||||
|
configured: true,
|
||||||
|
loginUrl: 'https://sso.example.com/realms/mosaic/protocol/saml',
|
||||||
|
},
|
||||||
|
warnings: [],
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
onOidcSignIn={vi.fn()}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(html).toContain('Continue with WorkOS');
|
||||||
|
expect(html).toContain('Continue with Keycloak (SAML)');
|
||||||
|
expect(html).toContain('https://sso.example.com/realms/mosaic/protocol/saml');
|
||||||
|
});
|
||||||
|
});
|
||||||
55
apps/web/src/components/auth/sso-provider-buttons.tsx
Normal file
55
apps/web/src/components/auth/sso-provider-buttons.tsx
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import type { SsoProviderDiscovery } from '@/lib/sso';
|
||||||
|
|
||||||
|
interface SsoProviderButtonsProps {
|
||||||
|
providers: SsoProviderDiscovery[];
|
||||||
|
loadingProviderId?: string | null;
|
||||||
|
onOidcSignIn: (providerId: SsoProviderDiscovery['id']) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SsoProviderButtons({
|
||||||
|
providers,
|
||||||
|
loadingProviderId = null,
|
||||||
|
onOidcSignIn,
|
||||||
|
}: SsoProviderButtonsProps): React.ReactElement | null {
|
||||||
|
const visibleProviders = providers.filter((provider) => provider.configured);
|
||||||
|
|
||||||
|
if (visibleProviders.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-6 space-y-3 border-t border-surface-border pt-6">
|
||||||
|
<p className="text-sm font-medium text-text-secondary">Single sign-on</p>
|
||||||
|
<div className="space-y-3">
|
||||||
|
{visibleProviders.map((provider) => {
|
||||||
|
if (provider.loginMode === 'saml' && provider.samlFallback.loginUrl) {
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
key={provider.id}
|
||||||
|
href={provider.samlFallback.loginUrl}
|
||||||
|
className="flex w-full items-center justify-center rounded-lg border border-surface-border bg-surface-elevated px-4 py-2.5 text-sm font-medium text-text-primary transition-colors hover:border-accent/50 hover:text-accent"
|
||||||
|
>
|
||||||
|
Continue with {provider.name} (SAML)
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={provider.id}
|
||||||
|
type="button"
|
||||||
|
disabled={loadingProviderId === provider.id}
|
||||||
|
onClick={() => onOidcSignIn(provider.id)}
|
||||||
|
className="flex w-full items-center justify-center rounded-lg border border-surface-border bg-surface-elevated px-4 py-2.5 text-sm font-medium text-text-primary transition-colors hover:border-accent/50 hover:text-accent disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{loadingProviderId === provider.id
|
||||||
|
? `Redirecting to ${provider.name}...`
|
||||||
|
: `Continue with ${provider.name}`}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,52 +1,192 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRef, useState } from 'react';
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
import type { ModelInfo } from '@/lib/types';
|
||||||
|
|
||||||
interface ChatInputProps {
|
interface ChatInputProps {
|
||||||
onSend: (content: string) => void;
|
onSend: (content: string, options?: { modelId?: string }) => void;
|
||||||
disabled?: boolean;
|
onStop?: () => void;
|
||||||
|
isStreaming?: boolean;
|
||||||
|
models: ModelInfo[];
|
||||||
|
selectedModelId: string;
|
||||||
|
onModelChange: (modelId: string) => void;
|
||||||
|
onRequestEditLastMessage?: () => string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ChatInput({ onSend, disabled }: ChatInputProps): React.ReactElement {
|
const MAX_HEIGHT = 220;
|
||||||
|
|
||||||
|
export function ChatInput({
|
||||||
|
onSend,
|
||||||
|
onStop,
|
||||||
|
isStreaming = false,
|
||||||
|
models,
|
||||||
|
selectedModelId,
|
||||||
|
onModelChange,
|
||||||
|
onRequestEditLastMessage,
|
||||||
|
}: ChatInputProps): React.ReactElement {
|
||||||
const [value, setValue] = useState('');
|
const [value, setValue] = useState('');
|
||||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
|
const selectedModel = useMemo(
|
||||||
|
() => models.find((model) => model.id === selectedModelId) ?? models[0],
|
||||||
|
[models, selectedModelId],
|
||||||
|
);
|
||||||
|
|
||||||
function handleSubmit(e: React.FormEvent): void {
|
useEffect(() => {
|
||||||
e.preventDefault();
|
const textarea = textareaRef.current;
|
||||||
|
if (!textarea) return;
|
||||||
|
textarea.style.height = 'auto';
|
||||||
|
textarea.style.height = `${Math.min(textarea.scrollHeight, MAX_HEIGHT)}px`;
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
function handleGlobalFocus(event: KeyboardEvent): void {
|
||||||
|
if (
|
||||||
|
(event.metaKey || event.ctrlKey) &&
|
||||||
|
(event.key === '/' || event.key.toLowerCase() === 'k')
|
||||||
|
) {
|
||||||
|
const target = event.target as HTMLElement | null;
|
||||||
|
if (target?.closest('input, textarea, [contenteditable="true"]')) return;
|
||||||
|
event.preventDefault();
|
||||||
|
textareaRef.current?.focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleGlobalFocus);
|
||||||
|
return () => document.removeEventListener('keydown', handleGlobalFocus);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
function handleSubmit(event: React.FormEvent): void {
|
||||||
|
event.preventDefault();
|
||||||
const trimmed = value.trim();
|
const trimmed = value.trim();
|
||||||
if (!trimmed || disabled) return;
|
if (!trimmed || isStreaming) return;
|
||||||
onSend(trimmed);
|
onSend(trimmed, { modelId: selectedModel?.id });
|
||||||
setValue('');
|
setValue('');
|
||||||
textareaRef.current?.focus();
|
textareaRef.current?.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>): void {
|
function handleKeyDown(event: React.KeyboardEvent<HTMLTextAreaElement>): void {
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
|
||||||
e.preventDefault();
|
event.preventDefault();
|
||||||
handleSubmit(e);
|
handleSubmit(event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === 'ArrowUp' && value.length === 0 && onRequestEditLastMessage) {
|
||||||
|
const lastMessage = onRequestEditLastMessage();
|
||||||
|
if (lastMessage) {
|
||||||
|
event.preventDefault();
|
||||||
|
setValue(lastMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const charCount = value.length;
|
||||||
|
const tokenEstimate = Math.ceil(charCount / 4);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={handleSubmit} className="border-t border-surface-border bg-surface-card p-4">
|
<form
|
||||||
<div className="flex items-end gap-3">
|
onSubmit={handleSubmit}
|
||||||
<textarea
|
className="border-t px-4 py-4 backdrop-blur-xl md:px-6"
|
||||||
ref={textareaRef}
|
style={{
|
||||||
value={value}
|
backgroundColor: 'color-mix(in srgb, var(--color-surface) 88%, transparent)',
|
||||||
onChange={(e) => setValue(e.target.value)}
|
borderColor: 'var(--color-border)',
|
||||||
onKeyDown={handleKeyDown}
|
}}
|
||||||
disabled={disabled}
|
>
|
||||||
rows={1}
|
<div
|
||||||
placeholder="Type a message... (Enter to send, Shift+Enter for newline)"
|
className="rounded-[28px] border p-3 shadow-[var(--shadow-ms-lg)]"
|
||||||
className="max-h-32 min-h-[2.5rem] flex-1 resize-none rounded-lg border border-surface-border bg-surface-elevated px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500 disabled:opacity-50"
|
style={{
|
||||||
/>
|
backgroundColor: 'var(--color-surface-2)',
|
||||||
<button
|
borderColor: 'var(--color-border)',
|
||||||
type="submit"
|
}}
|
||||||
disabled={disabled || !value.trim()}
|
>
|
||||||
className="rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-blue-700 disabled:opacity-50"
|
<div className="mb-3 flex flex-wrap items-center gap-3">
|
||||||
>
|
<label className="flex min-w-0 items-center gap-2 text-xs text-[var(--color-muted)]">
|
||||||
Send
|
<span className="uppercase tracking-[0.18em]">Model</span>
|
||||||
</button>
|
<select
|
||||||
|
value={selectedModelId}
|
||||||
|
onChange={(event) => onModelChange(event.target.value)}
|
||||||
|
className="rounded-full border px-3 py-1.5 text-sm outline-none"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{models.map((model) => (
|
||||||
|
<option key={`${model.provider}:${model.id}`} value={model.id}>
|
||||||
|
{model.name} · {model.provider}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<div className="ml-auto hidden items-center gap-2 text-xs text-[var(--color-muted)] md:flex">
|
||||||
|
<span className="rounded-full border border-[var(--color-border)] px-2 py-1">
|
||||||
|
⌘/ focus
|
||||||
|
</span>
|
||||||
|
<span className="rounded-full border border-[var(--color-border)] px-2 py-1">
|
||||||
|
⌘K focus
|
||||||
|
</span>
|
||||||
|
<span className="rounded-full border border-[var(--color-border)] px-2 py-1">
|
||||||
|
⌘↵ send
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-end gap-3">
|
||||||
|
<textarea
|
||||||
|
ref={textareaRef}
|
||||||
|
value={value}
|
||||||
|
onChange={(event) => setValue(event.target.value)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
disabled={isStreaming}
|
||||||
|
rows={1}
|
||||||
|
placeholder="Ask Mosaic something..."
|
||||||
|
className="min-h-[3.25rem] flex-1 resize-none bg-transparent px-1 py-2 text-sm outline-none placeholder:text-[var(--color-muted)] disabled:opacity-60"
|
||||||
|
style={{
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
maxHeight: `${MAX_HEIGHT}px`,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{isStreaming ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onStop}
|
||||||
|
className="inline-flex h-11 items-center gap-2 rounded-full border px-4 text-sm font-medium transition-colors"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span className="inline-block h-2.5 w-2.5 rounded-sm bg-[var(--color-danger)]" />
|
||||||
|
Stop
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={!value.trim()}
|
||||||
|
className="inline-flex h-11 items-center gap-2 rounded-full px-4 text-sm font-semibold text-white transition-all disabled:cursor-not-allowed disabled:opacity-45"
|
||||||
|
style={{ backgroundColor: 'var(--color-ms-blue-500)' }}
|
||||||
|
>
|
||||||
|
<span>Send</span>
|
||||||
|
<span aria-hidden="true">↗</span>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-3 flex flex-wrap items-center gap-2 text-xs text-[var(--color-muted)]">
|
||||||
|
<span>{charCount.toLocaleString()} chars</span>
|
||||||
|
<span>·</span>
|
||||||
|
<span>~{tokenEstimate.toLocaleString()} tokens</span>
|
||||||
|
{selectedModel ? (
|
||||||
|
<>
|
||||||
|
<span>·</span>
|
||||||
|
<span>{selectedModel.reasoning ? 'Reasoning on' : 'Fast response'}</span>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
<span className="ml-auto">Shift+Enter newline · Arrow ↑ edit last</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import type { Conversation } from '@/lib/types';
|
|||||||
interface ConversationListProps {
|
interface ConversationListProps {
|
||||||
conversations: Conversation[];
|
conversations: Conversation[];
|
||||||
activeId: string | null;
|
activeId: string | null;
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
onSelect: (id: string) => void;
|
onSelect: (id: string) => void;
|
||||||
onNew: () => void;
|
onNew: () => void;
|
||||||
onRename: (id: string, title: string) => void;
|
onRename: (id: string, title: string) => void;
|
||||||
@@ -20,7 +22,6 @@ interface ContextMenuState {
|
|||||||
y: number;
|
y: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Format a date as relative time (e.g. "2h ago", "Yesterday"). */
|
|
||||||
function formatRelativeTime(dateStr: string): string {
|
function formatRelativeTime(dateStr: string): string {
|
||||||
const date = new Date(dateStr);
|
const date = new Date(dateStr);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
@@ -40,6 +41,8 @@ function formatRelativeTime(dateStr: string): string {
|
|||||||
export function ConversationList({
|
export function ConversationList({
|
||||||
conversations,
|
conversations,
|
||||||
activeId,
|
activeId,
|
||||||
|
isOpen,
|
||||||
|
onClose,
|
||||||
onSelect,
|
onSelect,
|
||||||
onNew,
|
onNew,
|
||||||
onRename,
|
onRename,
|
||||||
@@ -54,24 +57,24 @@ export function ConversationList({
|
|||||||
const [showArchived, setShowArchived] = useState(false);
|
const [showArchived, setShowArchived] = useState(false);
|
||||||
const renameInputRef = useRef<HTMLInputElement>(null);
|
const renameInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const activeConversations = conversations.filter((c) => !c.archived);
|
const activeConversations = conversations.filter((conversation) => !conversation.archived);
|
||||||
const archivedConversations = conversations.filter((c) => c.archived);
|
const archivedConversations = conversations.filter((conversation) => conversation.archived);
|
||||||
|
|
||||||
const filteredActive = searchQuery
|
const filteredActive = searchQuery
|
||||||
? activeConversations.filter((c) =>
|
? activeConversations.filter((conversation) =>
|
||||||
(c.title ?? 'Untitled').toLowerCase().includes(searchQuery.toLowerCase()),
|
(conversation.title ?? 'Untitled').toLowerCase().includes(searchQuery.toLowerCase()),
|
||||||
)
|
)
|
||||||
: activeConversations;
|
: activeConversations;
|
||||||
|
|
||||||
const filteredArchived = searchQuery
|
const filteredArchived = searchQuery
|
||||||
? archivedConversations.filter((c) =>
|
? archivedConversations.filter((conversation) =>
|
||||||
(c.title ?? 'Untitled').toLowerCase().includes(searchQuery.toLowerCase()),
|
(conversation.title ?? 'Untitled').toLowerCase().includes(searchQuery.toLowerCase()),
|
||||||
)
|
)
|
||||||
: archivedConversations;
|
: archivedConversations;
|
||||||
|
|
||||||
const handleContextMenu = useCallback((e: React.MouseEvent, conversationId: string) => {
|
const handleContextMenu = useCallback((event: React.MouseEvent, conversationId: string) => {
|
||||||
e.preventDefault();
|
event.preventDefault();
|
||||||
setContextMenu({ conversationId, x: e.clientX, y: e.clientY });
|
setContextMenu({ conversationId, x: event.clientX, y: event.clientY });
|
||||||
setDeleteConfirmId(null);
|
setDeleteConfirmId(null);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -97,7 +100,7 @@ export function ConversationList({
|
|||||||
}
|
}
|
||||||
setRenamingId(null);
|
setRenamingId(null);
|
||||||
setRenameValue('');
|
setRenameValue('');
|
||||||
}, [renamingId, renameValue, onRename]);
|
}, [onRename, renameValue, renamingId]);
|
||||||
|
|
||||||
const cancelRename = useCallback(() => {
|
const cancelRename = useCallback(() => {
|
||||||
setRenamingId(null);
|
setRenamingId(null);
|
||||||
@@ -105,24 +108,20 @@ export function ConversationList({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleRenameKeyDown = useCallback(
|
const handleRenameKeyDown = useCallback(
|
||||||
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
if (e.key === 'Enter') commitRename();
|
if (event.key === 'Enter') commitRename();
|
||||||
if (e.key === 'Escape') cancelRename();
|
if (event.key === 'Escape') cancelRename();
|
||||||
},
|
},
|
||||||
[commitRename, cancelRename],
|
[cancelRename, commitRename],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleDeleteClick = useCallback((id: string) => {
|
|
||||||
setDeleteConfirmId(id);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const confirmDelete = useCallback(
|
const confirmDelete = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
onDelete(id);
|
onDelete(id);
|
||||||
setDeleteConfirmId(null);
|
setDeleteConfirmId(null);
|
||||||
closeContextMenu();
|
closeContextMenu();
|
||||||
},
|
},
|
||||||
[onDelete, closeContextMenu],
|
[closeContextMenu, onDelete],
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleArchiveToggle = useCallback(
|
const handleArchiveToggle = useCallback(
|
||||||
@@ -130,47 +129,59 @@ export function ConversationList({
|
|||||||
onArchive(id, archived);
|
onArchive(id, archived);
|
||||||
closeContextMenu();
|
closeContextMenu();
|
||||||
},
|
},
|
||||||
[onArchive, closeContextMenu],
|
[closeContextMenu, onArchive],
|
||||||
);
|
);
|
||||||
|
|
||||||
const contextConv = contextMenu
|
const contextConversation = contextMenu
|
||||||
? conversations.find((c) => c.id === contextMenu.conversationId)
|
? conversations.find((conversation) => conversation.id === contextMenu.conversationId)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
function renderConversationItem(conv: Conversation): React.ReactElement {
|
function renderConversationItem(conversation: Conversation): React.ReactElement {
|
||||||
const isActive = activeId === conv.id;
|
const isActive = activeId === conversation.id;
|
||||||
const isRenaming = renamingId === conv.id;
|
const isRenaming = renamingId === conversation.id;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key={conv.id} className="group relative">
|
<div key={conversation.id} className="group relative">
|
||||||
{isRenaming ? (
|
{isRenaming ? (
|
||||||
<div className="px-3 py-2">
|
<div className="px-3 py-2">
|
||||||
<input
|
<input
|
||||||
ref={renameInputRef}
|
ref={renameInputRef}
|
||||||
value={renameValue}
|
value={renameValue}
|
||||||
onChange={(e) => setRenameValue(e.target.value)}
|
onChange={(event) => setRenameValue(event.target.value)}
|
||||||
onBlur={commitRename}
|
onBlur={commitRename}
|
||||||
onKeyDown={handleRenameKeyDown}
|
onKeyDown={handleRenameKeyDown}
|
||||||
className="w-full rounded border border-blue-500 bg-surface-elevated px-2 py-0.5 text-sm text-text-primary outline-none"
|
className="w-full rounded-xl border px-3 py-2 text-sm outline-none"
|
||||||
|
style={{
|
||||||
|
borderColor: 'var(--color-ms-blue-500)',
|
||||||
|
backgroundColor: 'var(--color-surface-2)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
maxLength={255}
|
maxLength={255}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => onSelect(conv.id)}
|
onClick={() => {
|
||||||
onDoubleClick={() => startRename(conv.id, conv.title)}
|
onSelect(conversation.id);
|
||||||
onContextMenu={(e) => handleContextMenu(e, conv.id)}
|
if (window.innerWidth < 768) onClose();
|
||||||
|
}}
|
||||||
|
onDoubleClick={() => startRename(conversation.id, conversation.title)}
|
||||||
|
onContextMenu={(event) => handleContextMenu(event, conversation.id)}
|
||||||
className={cn(
|
className={cn(
|
||||||
'w-full px-3 py-2 text-left text-sm transition-colors',
|
'w-full rounded-2xl px-3 py-2 text-left text-sm transition-colors',
|
||||||
isActive
|
isActive ? 'shadow-[var(--shadow-ms-sm)]' : 'hover:bg-white/5',
|
||||||
? 'bg-blue-600/20 text-blue-400'
|
|
||||||
: 'text-text-secondary hover:bg-surface-elevated',
|
|
||||||
)}
|
)}
|
||||||
|
style={{
|
||||||
|
backgroundColor: isActive
|
||||||
|
? 'color-mix(in srgb, var(--color-ms-blue-500) 22%, transparent)'
|
||||||
|
: 'transparent',
|
||||||
|
color: isActive ? 'var(--color-text)' : 'var(--color-text-2)',
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<span className="block truncate">{conv.title ?? 'Untitled'}</span>
|
<span className="block truncate font-medium">{conversation.title ?? 'Untitled'}</span>
|
||||||
<span className="block text-xs text-text-muted">
|
<span className="block text-xs text-[var(--color-muted)]">
|
||||||
{formatRelativeTime(conv.updatedAt)}
|
{formatRelativeTime(conversation.updatedAt)}
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
@@ -180,127 +191,138 @@ export function ConversationList({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* Backdrop to close context menu */}
|
{isOpen ? (
|
||||||
{contextMenu && (
|
<button
|
||||||
<div className="fixed inset-0 z-10" onClick={closeContextMenu} aria-hidden="true" />
|
type="button"
|
||||||
)}
|
className="fixed inset-0 z-20 bg-black/45 md:hidden"
|
||||||
|
onClick={onClose}
|
||||||
|
aria-label="Close conversation sidebar"
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<div className="flex h-full w-64 flex-col border-r border-surface-border bg-surface-card">
|
{contextMenu ? (
|
||||||
{/* Header */}
|
<div className="fixed inset-0 z-10" onClick={closeContextMenu} aria-hidden="true" />
|
||||||
<div className="flex items-center justify-between p-3">
|
) : null}
|
||||||
<h2 className="text-sm font-medium text-text-secondary">Conversations</h2>
|
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
'fixed inset-y-0 left-0 z-30 flex h-full w-[18.5rem] flex-col border-r px-3 py-3 transition-transform duration-200 md:static md:z-auto',
|
||||||
|
isOpen
|
||||||
|
? 'translate-x-0'
|
||||||
|
: '-translate-x-full md:w-0 md:min-w-0 md:overflow-hidden md:border-r-0 md:px-0 md:py-0',
|
||||||
|
)}
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between px-1 pb-3">
|
||||||
|
<h2 className="text-sm font-medium text-[var(--color-text-2)]">Conversations</h2>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={onNew}
|
onClick={onNew}
|
||||||
className="rounded-md px-2 py-1 text-xs text-blue-400 transition-colors hover:bg-surface-elevated"
|
className="rounded-full px-3 py-1 text-xs transition-colors hover:bg-white/5"
|
||||||
|
style={{ color: 'var(--color-ms-blue-400)' }}
|
||||||
>
|
>
|
||||||
+ New
|
+ New
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Search input */}
|
<div className="pb-3">
|
||||||
<div className="px-3 pb-2">
|
|
||||||
<input
|
<input
|
||||||
type="search"
|
type="search"
|
||||||
value={searchQuery}
|
value={searchQuery}
|
||||||
onChange={(e) => setSearchQuery(e.target.value)}
|
onChange={(event) => setSearchQuery(event.target.value)}
|
||||||
placeholder="Search conversations\u2026"
|
placeholder="Search conversations…"
|
||||||
className="w-full rounded-md border border-surface-border bg-surface-elevated px-3 py-1.5 text-xs text-text-primary placeholder:text-text-muted focus:border-blue-500 focus:outline-none"
|
className="w-full rounded-2xl border px-3 py-2 text-xs placeholder:text-[var(--color-muted)] focus:outline-none"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface-2)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Conversation list */}
|
<div className="flex-1 overflow-y-auto space-y-1">
|
||||||
<div className="flex-1 overflow-y-auto">
|
{filteredActive.length === 0 && !searchQuery ? (
|
||||||
{filteredActive.length === 0 && !searchQuery && (
|
<p className="px-1 py-2 text-xs text-[var(--color-muted)]">No conversations yet</p>
|
||||||
<p className="px-3 py-2 text-xs text-text-muted">No conversations yet</p>
|
) : null}
|
||||||
)}
|
{filteredActive.length === 0 && searchQuery ? (
|
||||||
{filteredActive.length === 0 && searchQuery && (
|
<p className="px-1 py-2 text-xs text-[var(--color-muted)]">
|
||||||
<p className="px-3 py-2 text-xs text-text-muted">
|
No results for “{searchQuery}”
|
||||||
No results for “{searchQuery}”
|
|
||||||
</p>
|
</p>
|
||||||
)}
|
) : null}
|
||||||
{filteredActive.map((conv) => renderConversationItem(conv))}
|
{filteredActive.map((conversation) => renderConversationItem(conversation))}
|
||||||
|
|
||||||
{/* Archived section */}
|
{archivedConversations.length > 0 ? (
|
||||||
{archivedConversations.length > 0 && (
|
<div className="pt-2">
|
||||||
<div className="mt-2">
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setShowArchived((v) => !v)}
|
onClick={() => setShowArchived((prev) => !prev)}
|
||||||
className="flex w-full items-center gap-1 px-3 py-1 text-xs text-text-muted transition-colors hover:text-text-secondary"
|
className="flex w-full items-center gap-2 px-1 py-1 text-xs text-[var(--color-muted)] transition-colors hover:text-[var(--color-text-2)]"
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
className={cn(
|
className={cn('inline-block transition-transform', showArchived && 'rotate-90')}
|
||||||
'inline-block transition-transform',
|
|
||||||
showArchived ? 'rotate-90' : '',
|
|
||||||
)}
|
|
||||||
>
|
>
|
||||||
►
|
▶
|
||||||
</span>
|
</span>
|
||||||
Archived ({archivedConversations.length})
|
Archived ({archivedConversations.length})
|
||||||
</button>
|
</button>
|
||||||
{showArchived && (
|
{showArchived ? (
|
||||||
<div className="opacity-60">
|
<div className="mt-1 space-y-1 opacity-70">
|
||||||
{filteredArchived.map((conv) => renderConversationItem(conv))}
|
{filteredArchived.map((conversation) => renderConversationItem(conversation))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
)}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Context menu */}
|
{contextMenu && contextConversation ? (
|
||||||
{contextMenu && contextConv && (
|
|
||||||
<div
|
<div
|
||||||
className="fixed z-20 min-w-36 rounded-md border border-surface-border bg-surface-card py-1 shadow-lg"
|
className="fixed z-30 min-w-40 rounded-2xl border py-1 shadow-[var(--shadow-ms-lg)]"
|
||||||
style={{ top: contextMenu.y, left: contextMenu.x }}
|
style={{
|
||||||
|
top: contextMenu.y,
|
||||||
|
left: contextMenu.x,
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="w-full px-3 py-1.5 text-left text-sm text-text-secondary hover:bg-surface-elevated"
|
className="w-full px-3 py-2 text-left text-sm text-[var(--color-text-2)] transition-colors hover:bg-white/5"
|
||||||
onClick={() => startRename(contextConv.id, contextConv.title)}
|
onClick={() => startRename(contextConversation.id, contextConversation.title)}
|
||||||
>
|
>
|
||||||
Rename
|
Rename
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="w-full px-3 py-1.5 text-left text-sm text-text-secondary hover:bg-surface-elevated"
|
className="w-full px-3 py-2 text-left text-sm text-[var(--color-text-2)] transition-colors hover:bg-white/5"
|
||||||
onClick={() => handleArchiveToggle(contextConv.id, !contextConv.archived)}
|
onClick={() =>
|
||||||
|
handleArchiveToggle(contextConversation.id, !contextConversation.archived)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{contextConv.archived ? 'Unarchive' : 'Archive'}
|
{contextConversation.archived ? 'Restore' : 'Archive'}
|
||||||
</button>
|
</button>
|
||||||
<hr className="my-1 border-surface-border" />
|
{deleteConfirmId === contextConversation.id ? (
|
||||||
{deleteConfirmId === contextConv.id ? (
|
<button
|
||||||
<div className="px-3 py-1.5">
|
type="button"
|
||||||
<p className="mb-1.5 text-xs text-red-400">Delete this conversation?</p>
|
className="w-full px-3 py-2 text-left text-sm text-[var(--color-danger)] transition-colors hover:bg-white/5"
|
||||||
<div className="flex gap-2">
|
onClick={() => confirmDelete(contextConversation.id)}
|
||||||
<button
|
>
|
||||||
type="button"
|
Confirm delete
|
||||||
className="rounded bg-red-600 px-2 py-0.5 text-xs text-white hover:bg-red-700"
|
</button>
|
||||||
onClick={() => confirmDelete(contextConv.id)}
|
|
||||||
>
|
|
||||||
Delete
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="rounded px-2 py-0.5 text-xs text-text-muted hover:bg-surface-elevated"
|
|
||||||
onClick={() => setDeleteConfirmId(null)}
|
|
||||||
>
|
|
||||||
Cancel
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="w-full px-3 py-1.5 text-left text-sm text-red-400 hover:bg-surface-elevated"
|
className="w-full px-3 py-2 text-left text-sm text-[var(--color-danger)] transition-colors hover:bg-white/5"
|
||||||
onClick={() => handleDeleteClick(contextConv.id)}
|
onClick={() => setDeleteConfirmId(contextConversation.id)}
|
||||||
>
|
>
|
||||||
Delete
|
Delete…
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
) : null}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
576
apps/web/src/components/chat/conversation-sidebar.tsx
Normal file
576
apps/web/src/components/chat/conversation-sidebar.tsx
Normal file
@@ -0,0 +1,576 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import {
|
||||||
|
forwardRef,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useImperativeHandle,
|
||||||
|
useMemo,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
|
import { api } from '@/lib/api';
|
||||||
|
import type { Conversation, Project } from '@/lib/types';
|
||||||
|
|
||||||
|
export interface ConversationSummary {
|
||||||
|
id: string;
|
||||||
|
title: string | null;
|
||||||
|
projectId: string | null;
|
||||||
|
updatedAt: string;
|
||||||
|
archived?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ConversationSidebarRef {
|
||||||
|
refresh: () => void;
|
||||||
|
addConversation: (conversation: ConversationSummary) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ConversationSidebarProps {
|
||||||
|
isOpen: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
currentConversationId: string | null;
|
||||||
|
onSelectConversation: (conversationId: string | null) => void;
|
||||||
|
onNewConversation: (projectId?: string | null) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GroupedConversations {
|
||||||
|
key: string;
|
||||||
|
label: string;
|
||||||
|
projectId: string | null;
|
||||||
|
conversations: ConversationSummary[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function toSummary(conversation: Conversation): ConversationSummary {
|
||||||
|
return {
|
||||||
|
id: conversation.id,
|
||||||
|
title: conversation.title,
|
||||||
|
projectId: conversation.projectId,
|
||||||
|
updatedAt: conversation.updatedAt,
|
||||||
|
archived: conversation.archived,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatRelativeTime(dateStr: string): string {
|
||||||
|
const date = new Date(dateStr);
|
||||||
|
const now = new Date();
|
||||||
|
const diffMs = now.getTime() - date.getTime();
|
||||||
|
const diffMinutes = Math.floor(diffMs / 60_000);
|
||||||
|
const diffHours = Math.floor(diffMs / 3_600_000);
|
||||||
|
const diffDays = Math.floor(diffMs / 86_400_000);
|
||||||
|
|
||||||
|
if (diffMinutes < 1) return 'Just now';
|
||||||
|
if (diffMinutes < 60) return `${diffMinutes}m ago`;
|
||||||
|
if (diffHours < 24) return `${diffHours}h ago`;
|
||||||
|
if (diffDays === 1) return 'Yesterday';
|
||||||
|
if (diffDays < 7) return `${diffDays}d ago`;
|
||||||
|
|
||||||
|
return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ConversationSidebar = forwardRef<ConversationSidebarRef, ConversationSidebarProps>(
|
||||||
|
function ConversationSidebar(
|
||||||
|
{ isOpen, onClose, currentConversationId, onSelectConversation, onNewConversation },
|
||||||
|
ref,
|
||||||
|
): React.ReactElement {
|
||||||
|
const [conversations, setConversations] = useState<ConversationSummary[]>([]);
|
||||||
|
const [projects, setProjects] = useState<Project[]>([]);
|
||||||
|
const [searchQuery, setSearchQuery] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [renamingId, setRenamingId] = useState<string | null>(null);
|
||||||
|
const [renameValue, setRenameValue] = useState('');
|
||||||
|
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(null);
|
||||||
|
const [hoveredId, setHoveredId] = useState<string | null>(null);
|
||||||
|
const renameInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const loadSidebarData = useCallback(async (): Promise<void> => {
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
const [loadedConversations, loadedProjects] = await Promise.all([
|
||||||
|
api<Conversation[]>('/api/conversations'),
|
||||||
|
api<Project[]>('/api/projects').catch(() => [] as Project[]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
setConversations(
|
||||||
|
loadedConversations
|
||||||
|
.filter((conversation) => !conversation.archived)
|
||||||
|
.map(toSummary)
|
||||||
|
.sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt)),
|
||||||
|
);
|
||||||
|
setProjects(loadedProjects);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to load conversations');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void loadSidebarData();
|
||||||
|
}, [loadSidebarData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!renamingId) return;
|
||||||
|
const timer = window.setTimeout(() => renameInputRef.current?.focus(), 0);
|
||||||
|
return () => window.clearTimeout(timer);
|
||||||
|
}, [renamingId]);
|
||||||
|
|
||||||
|
useImperativeHandle(
|
||||||
|
ref,
|
||||||
|
() => ({
|
||||||
|
refresh: () => {
|
||||||
|
void loadSidebarData();
|
||||||
|
},
|
||||||
|
addConversation: (conversation) => {
|
||||||
|
setConversations((prev) => {
|
||||||
|
const next = [conversation, ...prev.filter((item) => item.id !== conversation.id)];
|
||||||
|
return next.sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
[loadSidebarData],
|
||||||
|
);
|
||||||
|
|
||||||
|
const filteredConversations = useMemo(() => {
|
||||||
|
const query = searchQuery.trim().toLowerCase();
|
||||||
|
if (!query) return conversations;
|
||||||
|
|
||||||
|
return conversations.filter((conversation) =>
|
||||||
|
(conversation.title ?? 'Untitled conversation').toLowerCase().includes(query),
|
||||||
|
);
|
||||||
|
}, [conversations, searchQuery]);
|
||||||
|
|
||||||
|
const groupedConversations = useMemo<GroupedConversations[]>(() => {
|
||||||
|
if (projects.length === 0) {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
key: 'all',
|
||||||
|
label: 'All conversations',
|
||||||
|
projectId: null,
|
||||||
|
conversations: filteredConversations,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
const byProject = new Map<string | null, ConversationSummary[]>();
|
||||||
|
for (const conversation of filteredConversations) {
|
||||||
|
const key = conversation.projectId ?? null;
|
||||||
|
const items = byProject.get(key) ?? [];
|
||||||
|
items.push(conversation);
|
||||||
|
byProject.set(key, items);
|
||||||
|
}
|
||||||
|
|
||||||
|
const groups: GroupedConversations[] = [];
|
||||||
|
|
||||||
|
for (const project of projects) {
|
||||||
|
const projectConversations = byProject.get(project.id);
|
||||||
|
if (!projectConversations?.length) continue;
|
||||||
|
|
||||||
|
groups.push({
|
||||||
|
key: project.id,
|
||||||
|
label: project.name,
|
||||||
|
projectId: project.id,
|
||||||
|
conversations: projectConversations,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const ungrouped = byProject.get(null);
|
||||||
|
if (ungrouped?.length) {
|
||||||
|
groups.push({
|
||||||
|
key: 'general',
|
||||||
|
label: 'General',
|
||||||
|
projectId: null,
|
||||||
|
conversations: ungrouped,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groups.length === 0) {
|
||||||
|
groups.push({
|
||||||
|
key: 'all',
|
||||||
|
label: 'All conversations',
|
||||||
|
projectId: null,
|
||||||
|
conversations: filteredConversations,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return groups;
|
||||||
|
}, [filteredConversations, projects]);
|
||||||
|
|
||||||
|
const startRename = useCallback((conversation: ConversationSummary): void => {
|
||||||
|
setPendingDeleteId(null);
|
||||||
|
setRenamingId(conversation.id);
|
||||||
|
setRenameValue(conversation.title ?? '');
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const cancelRename = useCallback((): void => {
|
||||||
|
setRenamingId(null);
|
||||||
|
setRenameValue('');
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const commitRename = useCallback(async (): Promise<void> => {
|
||||||
|
if (!renamingId) return;
|
||||||
|
|
||||||
|
const title = renameValue.trim() || 'Untitled conversation';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const updated = await api<Conversation>(`/api/conversations/${renamingId}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
body: { title },
|
||||||
|
});
|
||||||
|
|
||||||
|
const summary = toSummary(updated);
|
||||||
|
setConversations((prev) =>
|
||||||
|
prev
|
||||||
|
.map((conversation) => (conversation.id === renamingId ? summary : conversation))
|
||||||
|
.sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt)),
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to rename conversation');
|
||||||
|
} finally {
|
||||||
|
setRenamingId(null);
|
||||||
|
setRenameValue('');
|
||||||
|
}
|
||||||
|
}, [renameValue, renamingId]);
|
||||||
|
|
||||||
|
const deleteConversation = useCallback(
|
||||||
|
async (conversationId: string): Promise<void> => {
|
||||||
|
try {
|
||||||
|
await api<void>(`/api/conversations/${conversationId}`, { method: 'DELETE' });
|
||||||
|
setConversations((prev) =>
|
||||||
|
prev.filter((conversation) => conversation.id !== conversationId),
|
||||||
|
);
|
||||||
|
if (currentConversationId === conversationId) {
|
||||||
|
onSelectConversation(null);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to delete conversation');
|
||||||
|
} finally {
|
||||||
|
setPendingDeleteId(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[currentConversationId, onSelectConversation],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{isOpen ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label="Close conversation sidebar"
|
||||||
|
className="fixed inset-0 z-30 bg-black/50 md:hidden"
|
||||||
|
onClick={onClose}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<aside
|
||||||
|
aria-label="Conversation sidebar"
|
||||||
|
className="fixed left-0 top-0 z-40 flex h-full flex-col border-r md:relative md:z-0"
|
||||||
|
style={{
|
||||||
|
width: 'var(--sidebar-w)',
|
||||||
|
background: 'var(--bg)',
|
||||||
|
borderColor: 'var(--border)',
|
||||||
|
transform: isOpen ? 'translateX(0)' : 'translateX(calc(-1 * var(--sidebar-w)))',
|
||||||
|
transition: 'transform 220ms var(--ease)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="flex items-center justify-between border-b px-4 py-3"
|
||||||
|
style={{ borderColor: 'var(--border)' }}
|
||||||
|
>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm font-semibold" style={{ color: 'var(--text)' }}>
|
||||||
|
Conversations
|
||||||
|
</p>
|
||||||
|
<p className="text-xs" style={{ color: 'var(--muted)' }}>
|
||||||
|
Search, rename, and manage threads
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onClose}
|
||||||
|
className="rounded-md p-2 md:hidden"
|
||||||
|
style={{ color: 'var(--text-2)' }}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor">
|
||||||
|
<path strokeWidth="2" strokeLinecap="round" d="M6 6l12 12M18 6 6 18" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3 border-b p-3" style={{ borderColor: 'var(--border)' }}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onNewConversation(null)}
|
||||||
|
className="flex w-full items-center justify-center gap-2 rounded-lg border px-3 py-2 text-sm font-medium transition-colors"
|
||||||
|
style={{
|
||||||
|
borderColor: 'var(--primary)',
|
||||||
|
background: 'color-mix(in srgb, var(--primary) 12%, transparent)',
|
||||||
|
color: 'var(--text)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" className="h-4 w-4" fill="none" stroke="currentColor">
|
||||||
|
<path strokeWidth="2" strokeLinecap="round" d="M12 5v14M5 12h14" />
|
||||||
|
</svg>
|
||||||
|
New conversation
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div className="relative">
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
style={{ color: 'var(--muted)' }}
|
||||||
|
>
|
||||||
|
<circle cx="11" cy="11" r="7" strokeWidth="2" />
|
||||||
|
<path d="m20 20-3.5-3.5" strokeWidth="2" strokeLinecap="round" />
|
||||||
|
</svg>
|
||||||
|
<input
|
||||||
|
type="search"
|
||||||
|
value={searchQuery}
|
||||||
|
onChange={(event) => setSearchQuery(event.target.value)}
|
||||||
|
placeholder="Search conversations"
|
||||||
|
className="w-full rounded-lg border px-9 py-2 text-sm outline-none"
|
||||||
|
style={{
|
||||||
|
background: 'var(--surface)',
|
||||||
|
borderColor: 'var(--border)',
|
||||||
|
color: 'var(--text)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex-1 overflow-y-auto p-3">
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="py-8 text-center text-sm" style={{ color: 'var(--muted)' }}>
|
||||||
|
Loading conversations...
|
||||||
|
</div>
|
||||||
|
) : error ? (
|
||||||
|
<div
|
||||||
|
className="space-y-3 rounded-xl border p-4 text-sm"
|
||||||
|
style={{
|
||||||
|
background: 'color-mix(in srgb, var(--danger) 10%, var(--surface))',
|
||||||
|
borderColor: 'color-mix(in srgb, var(--danger) 35%, var(--border))',
|
||||||
|
color: 'var(--text)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p>{error}</p>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void loadSidebarData()}
|
||||||
|
className="rounded-md px-3 py-1.5 text-xs font-medium"
|
||||||
|
style={{ background: 'var(--danger)', color: 'white' }}
|
||||||
|
>
|
||||||
|
Retry
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : filteredConversations.length === 0 ? (
|
||||||
|
<div className="py-10 text-center">
|
||||||
|
<p className="text-sm" style={{ color: 'var(--text-2)' }}>
|
||||||
|
{searchQuery ? 'No matching conversations' : 'No conversations yet'}
|
||||||
|
</p>
|
||||||
|
<p className="mt-1 text-xs" style={{ color: 'var(--muted)' }}>
|
||||||
|
{searchQuery ? 'Try another title search.' : 'Start a new conversation to begin.'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{groupedConversations.map((group) => (
|
||||||
|
<section key={group.key} className="space-y-2">
|
||||||
|
{projects.length > 0 ? (
|
||||||
|
<div className="flex items-center justify-between px-1">
|
||||||
|
<h3
|
||||||
|
className="text-[11px] font-semibold uppercase tracking-[0.16em]"
|
||||||
|
style={{ color: 'var(--muted)' }}
|
||||||
|
>
|
||||||
|
{group.label}
|
||||||
|
</h3>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onNewConversation(group.projectId)}
|
||||||
|
className="rounded-md px-2 py-1 text-[11px] font-medium"
|
||||||
|
style={{ color: 'var(--ms-blue-500)' }}
|
||||||
|
>
|
||||||
|
New
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
{group.conversations.map((conversation) => {
|
||||||
|
const isActive = currentConversationId === conversation.id;
|
||||||
|
const isRenaming = renamingId === conversation.id;
|
||||||
|
const showActions =
|
||||||
|
hoveredId === conversation.id ||
|
||||||
|
isRenaming ||
|
||||||
|
pendingDeleteId === conversation.id;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={conversation.id}
|
||||||
|
onMouseEnter={() => setHoveredId(conversation.id)}
|
||||||
|
onMouseLeave={() =>
|
||||||
|
setHoveredId((current) =>
|
||||||
|
current === conversation.id ? null : current,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
className="rounded-xl border p-2 transition-colors"
|
||||||
|
style={{
|
||||||
|
borderColor: isActive
|
||||||
|
? 'color-mix(in srgb, var(--primary) 60%, var(--border))'
|
||||||
|
: 'transparent',
|
||||||
|
background: isActive ? 'var(--surface-2)' : 'transparent',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isRenaming ? (
|
||||||
|
<input
|
||||||
|
ref={renameInputRef}
|
||||||
|
value={renameValue}
|
||||||
|
onChange={(event) => setRenameValue(event.target.value)}
|
||||||
|
onBlur={() => void commitRename()}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
void commitRename();
|
||||||
|
}
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
event.preventDefault();
|
||||||
|
cancelRename();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
maxLength={255}
|
||||||
|
className="w-full rounded-md border px-2 py-1.5 text-sm outline-none"
|
||||||
|
style={{
|
||||||
|
background: 'var(--surface)',
|
||||||
|
borderColor: 'var(--ms-blue-500)',
|
||||||
|
color: 'var(--text)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => onSelectConversation(conversation.id)}
|
||||||
|
className="block w-full text-left"
|
||||||
|
>
|
||||||
|
<div className="flex items-start gap-2">
|
||||||
|
<div className="min-w-0 flex-1">
|
||||||
|
<p
|
||||||
|
className="truncate text-sm font-medium"
|
||||||
|
style={{
|
||||||
|
color: isActive ? 'var(--text)' : 'var(--text-2)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{conversation.title ?? 'Untitled conversation'}
|
||||||
|
</p>
|
||||||
|
<p className="mt-1 text-xs" style={{ color: 'var(--muted)' }}>
|
||||||
|
{formatRelativeTime(conversation.updatedAt)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{showActions ? (
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
startRename(conversation);
|
||||||
|
}}
|
||||||
|
className="rounded-md p-1.5 transition-colors"
|
||||||
|
style={{ color: 'var(--text-2)' }}
|
||||||
|
aria-label={`Rename ${conversation.title ?? 'conversation'}`}
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
className="h-3.5 w-3.5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
d="M4 20h4l10.5-10.5a1.4 1.4 0 0 0 0-2L16.5 5.5a1.4 1.4 0 0 0-2 0L4 16v4Z"
|
||||||
|
strokeWidth="1.8"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
setPendingDeleteId((current) =>
|
||||||
|
current === conversation.id ? null : conversation.id,
|
||||||
|
);
|
||||||
|
setRenamingId(null);
|
||||||
|
}}
|
||||||
|
className="rounded-md p-1.5 transition-colors"
|
||||||
|
style={{ color: 'var(--danger)' }}
|
||||||
|
aria-label={`Delete ${conversation.title ?? 'conversation'}`}
|
||||||
|
>
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
className="h-3.5 w-3.5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
d="M4 7h16M10 11v6M14 11v6M6 7l1 12h10l1-12M9 7V4h6v3"
|
||||||
|
strokeWidth="1.8"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{pendingDeleteId === conversation.id ? (
|
||||||
|
<div
|
||||||
|
className="mt-2 flex items-center justify-between rounded-lg border px-2 py-2"
|
||||||
|
style={{
|
||||||
|
borderColor:
|
||||||
|
'color-mix(in srgb, var(--danger) 45%, var(--border))',
|
||||||
|
background:
|
||||||
|
'color-mix(in srgb, var(--danger) 10%, var(--surface))',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p className="text-xs" style={{ color: 'var(--text-2)' }}>
|
||||||
|
Delete this conversation?
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setPendingDeleteId(null)}
|
||||||
|
className="rounded-md px-2 py-1 text-xs"
|
||||||
|
style={{ color: 'var(--text-2)' }}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void deleteConversation(conversation.id)}
|
||||||
|
className="rounded-md px-2 py-1 text-xs font-medium"
|
||||||
|
style={{ background: 'var(--danger)', color: 'white' }}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import { useCallback, useMemo, useState } from 'react';
|
||||||
|
import ReactMarkdown from 'react-markdown';
|
||||||
import { cn } from '@/lib/cn';
|
import { cn } from '@/lib/cn';
|
||||||
import type { Message } from '@/lib/types';
|
import type { Message } from '@/lib/types';
|
||||||
|
|
||||||
@@ -9,27 +11,261 @@ interface MessageBubbleProps {
|
|||||||
|
|
||||||
export function MessageBubble({ message }: MessageBubbleProps): React.ReactElement {
|
export function MessageBubble({ message }: MessageBubbleProps): React.ReactElement {
|
||||||
const isUser = message.role === 'user';
|
const isUser = message.role === 'user';
|
||||||
|
const isSystem = message.role === 'system';
|
||||||
|
const [copied, setCopied] = useState(false);
|
||||||
|
const [thinkingExpanded, setThinkingExpanded] = useState(false);
|
||||||
|
const { response, thinking } = useMemo(
|
||||||
|
() => parseThinking(message.content, message.thinking),
|
||||||
|
[message.content, message.thinking],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleCopy = useCallback(async (): Promise<void> => {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(response);
|
||||||
|
setCopied(true);
|
||||||
|
window.setTimeout(() => setCopied(false), 1800);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[MessageBubble] Failed to copy message:', error);
|
||||||
|
}
|
||||||
|
}, [response]);
|
||||||
|
|
||||||
|
if (isSystem) {
|
||||||
|
return (
|
||||||
|
<div className="flex justify-center">
|
||||||
|
<div
|
||||||
|
className="max-w-[42rem] rounded-full border px-3 py-1.5 text-xs backdrop-blur-sm"
|
||||||
|
style={{
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
backgroundColor: 'color-mix(in srgb, var(--color-surface) 70%, transparent)',
|
||||||
|
color: 'var(--color-muted)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span>{response}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('flex', isUser ? 'justify-end' : 'justify-start')}>
|
<div className={cn('group flex', isUser ? 'justify-end' : 'justify-start')}>
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
'max-w-[75%] rounded-xl px-4 py-3 text-sm',
|
'flex max-w-[min(78ch,85%)] flex-col gap-2',
|
||||||
isUser
|
isUser ? 'items-end' : 'items-start',
|
||||||
? 'bg-blue-600 text-white'
|
|
||||||
: 'border border-surface-border bg-surface-elevated text-text-primary',
|
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className="whitespace-pre-wrap break-words">{message.content}</div>
|
<div className={cn('flex items-center gap-2 text-[11px]', isUser && 'flex-row-reverse')}>
|
||||||
|
<span className="font-medium text-[var(--color-text-2)]">
|
||||||
|
{isUser ? 'You' : 'Assistant'}
|
||||||
|
</span>
|
||||||
|
{!isUser && message.model ? (
|
||||||
|
<span
|
||||||
|
className="rounded-full border px-2 py-0.5 font-medium text-[var(--color-text-2)]"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'color-mix(in srgb, var(--color-surface-2) 82%, transparent)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
title={message.provider ? `Provider: ${message.provider}` : undefined}
|
||||||
|
>
|
||||||
|
{message.model}
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
{!isUser && typeof message.totalTokens === 'number' && message.totalTokens > 0 ? (
|
||||||
|
<span
|
||||||
|
className="rounded-full border px-2 py-0.5 text-[var(--color-muted)]"
|
||||||
|
style={{ borderColor: 'var(--color-border)' }}
|
||||||
|
>
|
||||||
|
{formatTokenCount(message.totalTokens)}
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
<span className="text-[var(--color-muted)]">{formatTimestamp(message.createdAt)}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{thinking && !isUser ? (
|
||||||
|
<div
|
||||||
|
className="w-full overflow-hidden rounded-2xl border"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'color-mix(in srgb, var(--color-surface-2) 88%, transparent)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setThinkingExpanded((prev) => !prev)}
|
||||||
|
className="flex w-full items-center gap-2 px-3 py-2 text-left text-xs font-medium text-[var(--color-text-2)] transition-colors hover:bg-black/5"
|
||||||
|
aria-expanded={thinkingExpanded}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
'inline-block text-[10px] transition-transform',
|
||||||
|
thinkingExpanded && 'rotate-90',
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
▶
|
||||||
|
</span>
|
||||||
|
<span>Chain of thought</span>
|
||||||
|
<span className="ml-auto text-[var(--color-muted)]">
|
||||||
|
{thinkingExpanded ? 'Hide' : 'Show'}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
{thinkingExpanded ? (
|
||||||
|
<pre
|
||||||
|
className="overflow-x-auto border-t px-3 py-3 font-mono text-xs leading-6 whitespace-pre-wrap"
|
||||||
|
style={{
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
backgroundColor: 'var(--color-bg-deep)',
|
||||||
|
color: 'var(--color-text-2)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{thinking}
|
||||||
|
</pre>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className={cn('mt-1 text-right text-xs', isUser ? 'text-blue-200' : 'text-text-muted')}
|
className={cn(
|
||||||
|
'relative w-full rounded-3xl px-4 py-3 text-sm shadow-[var(--shadow-ms-sm)]',
|
||||||
|
!isUser && 'border',
|
||||||
|
)}
|
||||||
|
style={{
|
||||||
|
backgroundColor: isUser ? 'var(--color-ms-blue-500)' : 'var(--color-surface)',
|
||||||
|
color: isUser ? '#fff' : 'var(--color-text)',
|
||||||
|
borderColor: isUser ? 'transparent' : 'var(--color-border)',
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{new Date(message.createdAt).toLocaleTimeString([], {
|
<div className="max-w-none">
|
||||||
hour: '2-digit',
|
<ReactMarkdown
|
||||||
minute: '2-digit',
|
components={{
|
||||||
})}
|
p: ({ children }) => <p className="mb-3 leading-7 last:mb-0">{children}</p>,
|
||||||
|
ul: ({ children }) => <ul className="mb-3 list-disc pl-5 last:mb-0">{children}</ul>,
|
||||||
|
ol: ({ children }) => (
|
||||||
|
<ol className="mb-3 list-decimal pl-5 last:mb-0">{children}</ol>
|
||||||
|
),
|
||||||
|
li: ({ children }) => <li className="mb-1">{children}</li>,
|
||||||
|
a: ({ href, children }) => (
|
||||||
|
<a
|
||||||
|
href={href}
|
||||||
|
className="font-medium underline underline-offset-4"
|
||||||
|
target="_blank"
|
||||||
|
rel="noreferrer"
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</a>
|
||||||
|
),
|
||||||
|
pre: ({ children }) => <div className="mb-3 last:mb-0">{children}</div>,
|
||||||
|
code: ({ className, children, ...props }) => {
|
||||||
|
const language = className?.replace('language-', '');
|
||||||
|
const content = String(children).replace(/\n$/, '');
|
||||||
|
const isInline = !className;
|
||||||
|
|
||||||
|
if (isInline) {
|
||||||
|
return (
|
||||||
|
<code
|
||||||
|
className="rounded-md px-1.5 py-0.5 font-mono text-[0.9em]"
|
||||||
|
style={{
|
||||||
|
backgroundColor:
|
||||||
|
'color-mix(in srgb, var(--color-bg-deep) 76%, transparent)',
|
||||||
|
}}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</code>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="overflow-hidden rounded-2xl border"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-bg-deep)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="border-b px-3 py-2 font-mono text-[11px] uppercase tracking-[0.18em] text-[var(--color-muted)]"
|
||||||
|
style={{ borderColor: 'var(--color-border)' }}
|
||||||
|
>
|
||||||
|
{language || 'code'}
|
||||||
|
</div>
|
||||||
|
<pre className="overflow-x-auto p-3">
|
||||||
|
<code
|
||||||
|
className={cn('font-mono text-[13px] leading-6', className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</code>
|
||||||
|
</pre>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
blockquote: ({ children }) => (
|
||||||
|
<blockquote
|
||||||
|
className="mb-3 border-l-2 pl-4 italic last:mb-0"
|
||||||
|
style={{ borderColor: 'var(--color-ms-blue-500)' }}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</blockquote>
|
||||||
|
),
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{response}
|
||||||
|
</ReactMarkdown>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void handleCopy()}
|
||||||
|
className="absolute -right-2 -top-2 rounded-full border p-2 opacity-0 shadow-[var(--shadow-ms-md)] transition-all group-hover:opacity-100 focus:opacity-100"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: copied ? 'var(--color-success)' : 'var(--color-text-2)',
|
||||||
|
}}
|
||||||
|
aria-label={copied ? 'Copied' : 'Copy message'}
|
||||||
|
title={copied ? 'Copied' : 'Copy message'}
|
||||||
|
>
|
||||||
|
{copied ? '✓' : '⧉'}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseThinking(
|
||||||
|
content: string,
|
||||||
|
thinking?: string,
|
||||||
|
): { response: string; thinking: string | null } {
|
||||||
|
if (thinking) {
|
||||||
|
return { response: content, thinking };
|
||||||
|
}
|
||||||
|
|
||||||
|
const regex = /<(?:thinking|think)>([\s\S]*?)<\/(?:thinking|think)>/gi;
|
||||||
|
const matches = [...content.matchAll(regex)];
|
||||||
|
if (matches.length === 0) {
|
||||||
|
return { response: content, thinking: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
response: content.replace(regex, '').trim(),
|
||||||
|
thinking:
|
||||||
|
matches
|
||||||
|
.map((match) => match[1]?.trim() ?? '')
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('\n\n') || null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTimestamp(createdAt: string): string {
|
||||||
|
return new Date(createdAt).toLocaleTimeString([], {
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTokenCount(totalTokens: number): string {
|
||||||
|
if (totalTokens >= 1_000_000) return `${(totalTokens / 1_000_000).toFixed(1)}M tokens`;
|
||||||
|
if (totalTokens >= 1_000) return `${(totalTokens / 1_000).toFixed(1)}k tokens`;
|
||||||
|
return `${totalTokens} tokens`;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,26 +1,97 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
/** Renders an in-progress assistant message from streaming text. */
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
interface StreamingMessageProps {
|
interface StreamingMessageProps {
|
||||||
text: string;
|
text: string;
|
||||||
|
modelName?: string | null;
|
||||||
|
thinking?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StreamingMessage({ text }: StreamingMessageProps): React.ReactElement {
|
const WAITING_QUIPS = [
|
||||||
|
'The AI is warming up... give it a moment.',
|
||||||
|
'Brewing some thoughts...',
|
||||||
|
'Summoning intelligence from the void...',
|
||||||
|
'Consulting the silicon oracle...',
|
||||||
|
'Teaching electrons to think...',
|
||||||
|
];
|
||||||
|
|
||||||
|
const TIMEOUT_QUIPS = [
|
||||||
|
'The model wandered off. Let’s try to find it again.',
|
||||||
|
'Response is taking the scenic route.',
|
||||||
|
'That answer is clearly overthinking things.',
|
||||||
|
'Still working. Either brilliance or a detour.',
|
||||||
|
];
|
||||||
|
|
||||||
|
export function StreamingMessage({
|
||||||
|
text,
|
||||||
|
modelName,
|
||||||
|
thinking,
|
||||||
|
}: StreamingMessageProps): React.ReactElement {
|
||||||
|
const [elapsedMs, setElapsedMs] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setElapsedMs(0);
|
||||||
|
const startedAt = Date.now();
|
||||||
|
const timer = window.setInterval(() => {
|
||||||
|
setElapsedMs(Date.now() - startedAt);
|
||||||
|
}, 1000);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [text, modelName, thinking]);
|
||||||
|
|
||||||
|
const quip = useMemo(() => {
|
||||||
|
if (elapsedMs >= 18_000) {
|
||||||
|
return TIMEOUT_QUIPS[Math.floor((elapsedMs / 1000) % TIMEOUT_QUIPS.length)];
|
||||||
|
}
|
||||||
|
if (elapsedMs >= 4_000) {
|
||||||
|
return WAITING_QUIPS[Math.floor((elapsedMs / 1000) % WAITING_QUIPS.length)];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}, [elapsedMs]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-start">
|
<div className="flex justify-start">
|
||||||
<div className="max-w-[75%] rounded-xl border border-surface-border bg-surface-elevated px-4 py-3 text-sm text-text-primary">
|
<div
|
||||||
|
className="max-w-[min(78ch,85%)] rounded-3xl border px-4 py-3 text-sm shadow-[var(--shadow-ms-sm)]"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="mb-2 flex items-center gap-2 text-[11px]">
|
||||||
|
<span className="font-medium text-[var(--color-text-2)]">Assistant</span>
|
||||||
|
{modelName ? (
|
||||||
|
<span className="rounded-full border border-[var(--color-border)] px-2 py-0.5 text-[var(--color-text-2)]">
|
||||||
|
{modelName}
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
<span className="text-[var(--color-muted)]">{text ? 'Responding…' : 'Thinking…'}</span>
|
||||||
|
</div>
|
||||||
{text ? (
|
{text ? (
|
||||||
<div className="whitespace-pre-wrap break-words">{text}</div>
|
<div className="whitespace-pre-wrap break-words">{text}</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex items-center gap-2 text-text-muted">
|
<div className="flex items-center gap-2 text-[var(--color-muted)]">
|
||||||
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-blue-500" />
|
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-[var(--color-ms-blue-500)]" />
|
||||||
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-blue-500 [animation-delay:0.2s]" />
|
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-[var(--color-ms-blue-500)] [animation-delay:0.2s]" />
|
||||||
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-blue-500 [animation-delay:0.4s]" />
|
<span className="inline-block h-2 w-2 animate-pulse rounded-full bg-[var(--color-ms-blue-500)] [animation-delay:0.4s]" />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div className="mt-1 flex items-center gap-1 text-xs text-text-muted">
|
{thinking ? (
|
||||||
<span className="inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-blue-500" />
|
<div
|
||||||
{text ? 'Responding...' : 'Thinking...'}
|
className="mt-3 rounded-2xl border px-3 py-2 font-mono text-xs whitespace-pre-wrap"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-bg-deep)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text-2)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{thinking}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
<div className="mt-2 flex items-center gap-2 text-xs text-[var(--color-muted)]">
|
||||||
|
<span className="inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-[var(--color-ms-blue-500)]" />
|
||||||
|
<span>{quip ?? (text ? 'Responding…' : 'Thinking…')}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
239
apps/web/src/components/layout/app-header.tsx
Normal file
239
apps/web/src/components/layout/app-header.tsx
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { signOut, useSession } from '@/lib/auth-client';
|
||||||
|
|
||||||
|
interface AppHeaderProps {
|
||||||
|
conversationTitle?: string | null;
|
||||||
|
isSidebarOpen: boolean;
|
||||||
|
onToggleSidebar: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
type ThemeMode = 'dark' | 'light';
|
||||||
|
|
||||||
|
const THEME_STORAGE_KEY = 'mosaic-chat-theme';
|
||||||
|
|
||||||
|
export function AppHeader({
|
||||||
|
conversationTitle,
|
||||||
|
isSidebarOpen,
|
||||||
|
onToggleSidebar,
|
||||||
|
}: AppHeaderProps): React.ReactElement {
|
||||||
|
const { data: session } = useSession();
|
||||||
|
const [currentTime, setCurrentTime] = useState('');
|
||||||
|
const [version, setVersion] = useState<string | null>(null);
|
||||||
|
const [menuOpen, setMenuOpen] = useState(false);
|
||||||
|
const [theme, setTheme] = useState<ThemeMode>('dark');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
function updateTime(): void {
|
||||||
|
setCurrentTime(
|
||||||
|
new Date().toLocaleTimeString([], {
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateTime();
|
||||||
|
const interval = window.setInterval(updateTime, 60_000);
|
||||||
|
return () => window.clearInterval(interval);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetch('/version.json')
|
||||||
|
.then(async (res) => res.json() as Promise<{ version?: string; commit?: string }>)
|
||||||
|
.then((data) => {
|
||||||
|
if (data.version) {
|
||||||
|
setVersion(data.commit ? `${data.version}+${data.commit}` : data.version);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => setVersion(null));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const storedTheme = window.localStorage.getItem(THEME_STORAGE_KEY);
|
||||||
|
const nextTheme = storedTheme === 'light' ? 'light' : 'dark';
|
||||||
|
applyTheme(nextTheme);
|
||||||
|
setTheme(nextTheme);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleThemeToggle = useCallback(() => {
|
||||||
|
const nextTheme = theme === 'dark' ? 'light' : 'dark';
|
||||||
|
applyTheme(nextTheme);
|
||||||
|
window.localStorage.setItem(THEME_STORAGE_KEY, nextTheme);
|
||||||
|
setTheme(nextTheme);
|
||||||
|
}, [theme]);
|
||||||
|
|
||||||
|
const handleSignOut = useCallback(async (): Promise<void> => {
|
||||||
|
await signOut();
|
||||||
|
window.location.href = '/login';
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const userLabel = session?.user.name ?? session?.user.email ?? 'Mosaic User';
|
||||||
|
const initials = useMemo(() => getInitials(userLabel), [userLabel]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<header
|
||||||
|
className="sticky top-0 z-20 border-b backdrop-blur-xl"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'color-mix(in srgb, var(--color-surface) 82%, transparent)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between gap-3 px-4 py-3 md:px-6">
|
||||||
|
<div className="flex min-w-0 items-center gap-3">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onToggleSidebar}
|
||||||
|
className="inline-flex h-10 w-10 items-center justify-center rounded-2xl border transition-colors hover:bg-white/5"
|
||||||
|
style={{ borderColor: 'var(--color-border)', color: 'var(--color-text)' }}
|
||||||
|
aria-label="Toggle conversation sidebar"
|
||||||
|
aria-expanded={isSidebarOpen}
|
||||||
|
>
|
||||||
|
☰
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<Link href="/chat" className="flex min-w-0 items-center gap-3">
|
||||||
|
<div
|
||||||
|
className="flex h-10 w-10 items-center justify-center rounded-2xl text-sm font-semibold text-white shadow-[var(--shadow-ms-md)]"
|
||||||
|
style={{
|
||||||
|
background:
|
||||||
|
'linear-gradient(135deg, var(--color-ms-blue-500), var(--color-ms-teal-500))',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
M
|
||||||
|
</div>
|
||||||
|
<div className="flex min-w-0 items-center gap-3">
|
||||||
|
<div className="text-sm font-semibold text-[var(--color-text)]">Mosaic</div>
|
||||||
|
<div className="hidden h-5 w-px bg-[var(--color-border)] md:block" />
|
||||||
|
<div className="hidden items-center gap-2 md:flex">
|
||||||
|
<span className="relative flex h-2.5 w-2.5">
|
||||||
|
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-[var(--color-ms-teal-500)] opacity-60" />
|
||||||
|
<span className="relative inline-flex h-2.5 w-2.5 rounded-full bg-[var(--color-ms-teal-500)]" />
|
||||||
|
</span>
|
||||||
|
<span className="text-xs uppercase tracking-[0.18em] text-[var(--color-muted)]">
|
||||||
|
Online
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="hidden min-w-0 items-center gap-3 md:flex">
|
||||||
|
<div className="rounded-full border border-[var(--color-border)] px-3 py-1.5 text-xs text-[var(--color-text-2)]">
|
||||||
|
{currentTime || '--:--'}
|
||||||
|
</div>
|
||||||
|
<div className="max-w-[24rem] truncate text-sm font-medium text-[var(--color-text)]">
|
||||||
|
{conversationTitle?.trim() || 'New Session'}
|
||||||
|
</div>
|
||||||
|
{version ? (
|
||||||
|
<div className="rounded-full border border-[var(--color-border)] px-3 py-1.5 text-xs text-[var(--color-muted)]">
|
||||||
|
v{version}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<div className="hidden items-center gap-2 lg:flex">
|
||||||
|
<ShortcutHint label="⌘/" text="focus" />
|
||||||
|
<ShortcutHint label="⌘K" text="focus" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleThemeToggle}
|
||||||
|
className="inline-flex h-10 items-center justify-center rounded-2xl border px-3 text-sm transition-colors hover:bg-white/5"
|
||||||
|
style={{ borderColor: 'var(--color-border)', color: 'var(--color-text)' }}
|
||||||
|
aria-label="Toggle theme"
|
||||||
|
>
|
||||||
|
{theme === 'dark' ? '☀︎' : '☾'}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div className="relative">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setMenuOpen((prev) => !prev)}
|
||||||
|
className="inline-flex h-10 w-10 items-center justify-center rounded-full border text-sm font-semibold transition-colors hover:bg-white/5"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface-2)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
color: 'var(--color-text)',
|
||||||
|
}}
|
||||||
|
aria-expanded={menuOpen}
|
||||||
|
aria-label="Open user menu"
|
||||||
|
>
|
||||||
|
{session?.user.image ? (
|
||||||
|
<img
|
||||||
|
src={session.user.image}
|
||||||
|
alt={userLabel}
|
||||||
|
className="h-full w-full rounded-full object-cover"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
initials
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
{menuOpen ? (
|
||||||
|
<div
|
||||||
|
className="absolute right-0 top-12 min-w-56 rounded-3xl border p-2 shadow-[var(--shadow-ms-lg)]"
|
||||||
|
style={{
|
||||||
|
backgroundColor: 'var(--color-surface)',
|
||||||
|
borderColor: 'var(--color-border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="border-b px-3 py-2" style={{ borderColor: 'var(--color-border)' }}>
|
||||||
|
<div className="text-sm font-medium text-[var(--color-text)]">{userLabel}</div>
|
||||||
|
{session?.user.email ? (
|
||||||
|
<div className="text-xs text-[var(--color-muted)]">{session.user.email}</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
<div className="p-1">
|
||||||
|
<Link
|
||||||
|
href="/settings"
|
||||||
|
className="flex rounded-2xl px-3 py-2 text-sm text-[var(--color-text-2)] transition-colors hover:bg-white/5"
|
||||||
|
onClick={() => setMenuOpen(false)}
|
||||||
|
>
|
||||||
|
Settings
|
||||||
|
</Link>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void handleSignOut()}
|
||||||
|
className="flex w-full rounded-2xl px-3 py-2 text-left text-sm text-[var(--color-text-2)] transition-colors hover:bg-white/5"
|
||||||
|
>
|
||||||
|
Sign out
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ShortcutHint({ label, text }: { label: string; text: string }): React.ReactElement {
|
||||||
|
return (
|
||||||
|
<span className="inline-flex items-center gap-2 rounded-full border border-[var(--color-border)] px-3 py-1.5 text-xs text-[var(--color-muted)]">
|
||||||
|
<span className="font-medium text-[var(--color-text-2)]">{label}</span>
|
||||||
|
<span>{text}</span>
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInitials(label: string): string {
|
||||||
|
const words = label.split(/\s+/).filter(Boolean).slice(0, 2);
|
||||||
|
if (words.length === 0) return 'M';
|
||||||
|
return words.map((word) => word.charAt(0).toUpperCase()).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyTheme(theme: ThemeMode): void {
|
||||||
|
const root = document.documentElement;
|
||||||
|
if (theme === 'light') {
|
||||||
|
root.setAttribute('data-theme', 'light');
|
||||||
|
root.classList.remove('dark');
|
||||||
|
} else {
|
||||||
|
root.removeAttribute('data-theme');
|
||||||
|
root.classList.add('dark');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
import type { ReactNode } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
|
import { SidebarProvider, useSidebar } from './sidebar-context';
|
||||||
import { Sidebar } from './sidebar';
|
import { Sidebar } from './sidebar';
|
||||||
import { Topbar } from './topbar';
|
import { Topbar } from './topbar';
|
||||||
|
|
||||||
@@ -6,14 +9,24 @@ interface AppShellProps {
|
|||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function AppShell({ children }: AppShellProps): React.ReactElement {
|
function AppShellFrame({ children }: AppShellProps): React.ReactElement {
|
||||||
|
const { collapsed, isMobile } = useSidebar();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen">
|
<div className="app-shell" data-sidebar-hidden={!isMobile && collapsed ? 'true' : undefined}>
|
||||||
|
<Topbar />
|
||||||
<Sidebar />
|
<Sidebar />
|
||||||
<div className="pl-sidebar">
|
<div className="app-main">
|
||||||
<Topbar />
|
<main className="h-full overflow-y-auto p-6">{children}</main>
|
||||||
<main className="p-6">{children}</main>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function AppShell({ children }: AppShellProps): React.ReactElement {
|
||||||
|
return (
|
||||||
|
<SidebarProvider>
|
||||||
|
<AppShellFrame>{children}</AppShellFrame>
|
||||||
|
</SidebarProvider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
67
apps/web/src/components/layout/sidebar-context.tsx
Normal file
67
apps/web/src/components/layout/sidebar-context.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
|
||||||
|
|
||||||
|
interface SidebarContextValue {
|
||||||
|
collapsed: boolean;
|
||||||
|
toggleCollapsed: () => void;
|
||||||
|
mobileOpen: boolean;
|
||||||
|
setMobileOpen: (open: boolean) => void;
|
||||||
|
isMobile: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MOBILE_MAX_WIDTH = 767;
|
||||||
|
const SidebarContext = createContext<SidebarContextValue | undefined>(undefined);
|
||||||
|
|
||||||
|
export function SidebarProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||||
|
const [collapsed, setCollapsed] = useState(false);
|
||||||
|
const [mobileOpen, setMobileOpen] = useState(false);
|
||||||
|
const [isMobile, setIsMobile] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const mediaQuery = window.matchMedia(`(max-width: ${String(MOBILE_MAX_WIDTH)}px)`);
|
||||||
|
|
||||||
|
const syncState = (matches: boolean): void => {
|
||||||
|
setIsMobile(matches);
|
||||||
|
if (!matches) {
|
||||||
|
setMobileOpen(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
syncState(mediaQuery.matches);
|
||||||
|
|
||||||
|
const handleChange = (event: MediaQueryListEvent): void => {
|
||||||
|
syncState(event.matches);
|
||||||
|
};
|
||||||
|
|
||||||
|
mediaQuery.addEventListener('change', handleChange);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
mediaQuery.removeEventListener('change', handleChange);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SidebarContext.Provider
|
||||||
|
value={{
|
||||||
|
collapsed,
|
||||||
|
toggleCollapsed: () => setCollapsed((value) => !value),
|
||||||
|
mobileOpen,
|
||||||
|
setMobileOpen,
|
||||||
|
isMobile,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</SidebarContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useSidebar(): SidebarContextValue {
|
||||||
|
const context = useContext(SidebarContext);
|
||||||
|
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('useSidebar must be used within SidebarProvider');
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
@@ -3,58 +3,178 @@
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname } from 'next/navigation';
|
import { usePathname } from 'next/navigation';
|
||||||
import { cn } from '@/lib/cn';
|
import { cn } from '@/lib/cn';
|
||||||
|
import { MosaicLogo } from '@/components/ui/mosaic-logo';
|
||||||
|
import { useSidebar } from './sidebar-context';
|
||||||
|
|
||||||
interface NavItem {
|
interface NavItem {
|
||||||
label: string;
|
label: string;
|
||||||
href: string;
|
href: string;
|
||||||
icon: string;
|
icon: React.JSX.Element;
|
||||||
|
}
|
||||||
|
|
||||||
|
function IconChat(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<path d="M3 4.5A2.5 2.5 0 0 1 5.5 2h5A2.5 2.5 0 0 1 13 4.5v3A2.5 2.5 0 0 1 10.5 10H8l-3.5 3v-3H5.5A2.5 2.5 0 0 1 3 7.5z" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function IconTasks(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<path d="M6 3h7M6 8h7M6 13h7" />
|
||||||
|
<path d="M2.5 3.5 3.5 4.5 5 2.5M2.5 8.5 3.5 9.5 5 7.5M2.5 13.5 3.5 14.5 5 12.5" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function IconProjects(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<path d="M2 4.5A1.5 1.5 0 0 1 3.5 3h3l1.5 1.5h4A1.5 1.5 0 0 1 13.5 6v5.5A1.5 1.5 0 0 1 12 13H3.5A1.5 1.5 0 0 1 2 11.5z" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function IconSettings(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<circle cx="8" cy="8" r="2.25" />
|
||||||
|
<path d="M8 1.5v2M8 12.5v2M1.5 8h2M12.5 8h2M3.05 3.05l1.4 1.4M11.55 11.55l1.4 1.4M3.05 12.95l1.4-1.4M11.55 4.45l1.4-1.4" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function IconAdmin(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<path d="M8 1.75 13 3.5v3.58c0 3.12-1.88 5.94-5 7.17-3.12-1.23-5-4.05-5-7.17V3.5z" />
|
||||||
|
<path d="M6.25 7.75 7.5 9l2.5-2.5" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const navItems: NavItem[] = [
|
const navItems: NavItem[] = [
|
||||||
{ label: 'Chat', href: '/chat', icon: '💬' },
|
{ label: 'Chat', href: '/chat', icon: <IconChat /> },
|
||||||
{ label: 'Tasks', href: '/tasks', icon: '📋' },
|
{ label: 'Tasks', href: '/tasks', icon: <IconTasks /> },
|
||||||
{ label: 'Projects', href: '/projects', icon: '📁' },
|
{ label: 'Projects', href: '/projects', icon: <IconProjects /> },
|
||||||
{ label: 'Settings', href: '/settings', icon: '⚙️' },
|
{ label: 'Settings', href: '/settings', icon: <IconSettings /> },
|
||||||
{ label: 'Admin', href: '/admin', icon: '🛡️' },
|
{ label: 'Admin', href: '/admin', icon: <IconAdmin /> },
|
||||||
];
|
];
|
||||||
|
|
||||||
export function Sidebar(): React.ReactElement {
|
export function Sidebar(): React.ReactElement {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
|
const { mobileOpen, setMobileOpen } = useSidebar();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className="fixed left-0 top-0 z-30 flex h-screen w-sidebar flex-col border-r border-surface-border bg-surface-card">
|
<>
|
||||||
<div className="flex h-14 items-center px-4">
|
<aside
|
||||||
<Link href="/" className="text-lg font-semibold text-text-primary">
|
className="app-sidebar"
|
||||||
Mosaic
|
data-mobile-open={mobileOpen ? 'true' : undefined}
|
||||||
</Link>
|
style={{
|
||||||
</div>
|
width: 'var(--sidebar-w)',
|
||||||
|
background: 'var(--surface)',
|
||||||
|
borderRightColor: 'var(--border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="flex h-16 items-center gap-3 border-b px-5"
|
||||||
|
style={{ borderColor: 'var(--border)' }}
|
||||||
|
>
|
||||||
|
<MosaicLogo size={32} />
|
||||||
|
<div className="flex min-w-0 flex-col">
|
||||||
|
<span className="text-sm font-semibold uppercase tracking-[0.12em] text-[var(--text)]">
|
||||||
|
Mosaic
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-[var(--muted)]">Mission Control</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<nav className="flex-1 space-y-1 px-2 py-2">
|
<nav className="flex-1 px-3 py-4">
|
||||||
{navItems.map((item) => {
|
<div className="mb-3 px-2 text-[11px] font-medium uppercase tracking-[0.18em] text-[var(--muted)]">
|
||||||
const isActive = pathname === item.href || pathname.startsWith(`${item.href}/`);
|
Workspace
|
||||||
return (
|
</div>
|
||||||
<Link
|
<div className="space-y-1.5">
|
||||||
key={item.href}
|
{navItems.map((item) => {
|
||||||
href={item.href}
|
const isActive = pathname === item.href || pathname.startsWith(`${item.href}/`);
|
||||||
className={cn(
|
|
||||||
'flex items-center gap-3 rounded-lg px-3 py-2 text-sm transition-colors',
|
|
||||||
isActive
|
|
||||||
? 'bg-blue-600/20 text-blue-400'
|
|
||||||
: 'text-text-secondary hover:bg-surface-elevated hover:text-text-primary',
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
<span className="text-base" aria-hidden="true">
|
|
||||||
{item.icon}
|
|
||||||
</span>
|
|
||||||
{item.label}
|
|
||||||
</Link>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div className="border-t border-surface-border p-4">
|
return (
|
||||||
<p className="text-xs text-text-muted">Mosaic Stack v0.0.4</p>
|
<Link
|
||||||
</div>
|
key={item.href}
|
||||||
</aside>
|
href={item.href}
|
||||||
|
onClick={() => setMobileOpen(false)}
|
||||||
|
className={cn(
|
||||||
|
'group flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm transition-all duration-150',
|
||||||
|
isActive ? 'font-medium' : 'hover:bg-white/4',
|
||||||
|
)}
|
||||||
|
style={
|
||||||
|
isActive
|
||||||
|
? {
|
||||||
|
background: 'color-mix(in srgb, var(--primary) 18%, transparent)',
|
||||||
|
color: 'var(--primary)',
|
||||||
|
}
|
||||||
|
: { color: 'var(--text-2)' }
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<span className="shrink-0" aria-hidden="true">
|
||||||
|
{item.icon}
|
||||||
|
</span>
|
||||||
|
<span>{item.label}</span>
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<div className="border-t px-5 py-4" style={{ borderColor: 'var(--border)' }}>
|
||||||
|
<p className="text-xs text-[var(--muted)]">Mosaic Stack v0.0.4</p>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
{mobileOpen ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label="Close sidebar"
|
||||||
|
className="fixed inset-0 z-40 bg-black/40 md:hidden"
|
||||||
|
onClick={() => setMobileOpen(false)}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
46
apps/web/src/components/layout/theme-toggle.tsx
Normal file
46
apps/web/src/components/layout/theme-toggle.tsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useTheme } from '@/providers/theme-provider';
|
||||||
|
|
||||||
|
interface ThemeToggleProps {
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ThemeToggle({ className = '' }: ThemeToggleProps): React.JSX.Element {
|
||||||
|
const { theme, toggleTheme } = useTheme();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={toggleTheme}
|
||||||
|
className={`btn-ghost rounded-md p-2 ${className}`}
|
||||||
|
title={`Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`}
|
||||||
|
aria-label={`Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`}
|
||||||
|
>
|
||||||
|
{theme === 'dark' ? (
|
||||||
|
<svg
|
||||||
|
className="h-5 w-5"
|
||||||
|
style={{ color: 'var(--warn)' }}
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.5}
|
||||||
|
>
|
||||||
|
<circle cx="12" cy="12" r="4" />
|
||||||
|
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" />
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
<svg
|
||||||
|
className="h-5 w-5"
|
||||||
|
style={{ color: 'var(--text-2)' }}
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth={1.5}
|
||||||
|
>
|
||||||
|
<path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z" />
|
||||||
|
</svg>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,37 +1,87 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useSession, signOut } from '@/lib/auth-client';
|
import { signOut, useSession } from '@/lib/auth-client';
|
||||||
|
import { ThemeToggle } from './theme-toggle';
|
||||||
|
import { useSidebar } from './sidebar-context';
|
||||||
|
|
||||||
|
function MenuIcon(): React.JSX.Element {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
width="16"
|
||||||
|
height="16"
|
||||||
|
viewBox="0 0 16 16"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="1.5"
|
||||||
|
>
|
||||||
|
<path d="M2 4h12M2 8h12M2 12h12" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function Topbar(): React.ReactElement {
|
export function Topbar(): React.ReactElement {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { isMobile, mobileOpen, setMobileOpen, toggleCollapsed } = useSidebar();
|
||||||
|
|
||||||
async function handleSignOut(): Promise<void> {
|
async function handleSignOut(): Promise<void> {
|
||||||
await signOut();
|
await signOut();
|
||||||
router.replace('/login');
|
router.replace('/login');
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
function handleSidebarToggle(): void {
|
||||||
<header className="sticky top-0 z-20 flex h-14 items-center justify-between border-b border-surface-border bg-surface-card/80 px-6 backdrop-blur-sm">
|
if (isMobile) {
|
||||||
<div />
|
setMobileOpen(!mobileOpen);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
<div className="flex items-center gap-4">
|
toggleCollapsed();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<header
|
||||||
|
className="app-header justify-between border-b px-4 md:px-6"
|
||||||
|
style={{
|
||||||
|
height: 'var(--topbar-h)',
|
||||||
|
background: 'color-mix(in srgb, var(--surface) 88%, transparent)',
|
||||||
|
borderBottomColor: 'var(--border)',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSidebarToggle}
|
||||||
|
className="btn-ghost rounded-lg border p-2"
|
||||||
|
style={{ borderColor: 'var(--border)', color: 'var(--text-2)' }}
|
||||||
|
aria-label="Toggle sidebar"
|
||||||
|
>
|
||||||
|
<MenuIcon />
|
||||||
|
</button>
|
||||||
|
<div className="hidden sm:block">
|
||||||
|
<div className="text-sm font-medium text-[var(--text)]">Workspace</div>
|
||||||
|
<div className="text-xs text-[var(--muted)]">Unified agent operations</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<ThemeToggle />
|
||||||
{session?.user ? (
|
{session?.user ? (
|
||||||
<>
|
<>
|
||||||
<span className="text-sm text-text-secondary">
|
<span className="hidden text-sm text-[var(--text-2)] sm:block">
|
||||||
{session.user.name ?? session.user.email}
|
{session.user.name ?? session.user.email}
|
||||||
</span>
|
</span>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={handleSignOut}
|
onClick={handleSignOut}
|
||||||
className="rounded-md px-3 py-1.5 text-sm text-text-muted transition-colors hover:bg-surface-elevated hover:text-text-primary"
|
className="rounded-md px-3 py-1.5 text-sm transition-colors"
|
||||||
|
style={{ color: 'var(--muted)' }}
|
||||||
>
|
>
|
||||||
Sign out
|
Sign out
|
||||||
</button>
|
</button>
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<span className="text-sm text-text-muted">Not signed in</span>
|
<span className="text-sm text-[var(--muted)]">Not signed in</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { renderToStaticMarkup } from 'react-dom/server';
|
||||||
|
import { SsoProviderSection } from './sso-provider-section.js';
|
||||||
|
|
||||||
|
describe('SsoProviderSection', () => {
|
||||||
|
it('renders configured providers with callback, sync, and fallback details', () => {
|
||||||
|
const html = renderToStaticMarkup(
|
||||||
|
<SsoProviderSection
|
||||||
|
loading={false}
|
||||||
|
providers={[
|
||||||
|
{
|
||||||
|
id: 'workos',
|
||||||
|
name: 'WorkOS',
|
||||||
|
protocols: ['oidc'],
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'oidc',
|
||||||
|
callbackPath: '/api/auth/oauth2/callback/workos',
|
||||||
|
teamSync: { enabled: true, claim: 'organization_id' },
|
||||||
|
samlFallback: { configured: false, loginUrl: null },
|
||||||
|
warnings: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'keycloak',
|
||||||
|
name: 'Keycloak',
|
||||||
|
protocols: ['oidc', 'saml'],
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'saml',
|
||||||
|
callbackPath: null,
|
||||||
|
teamSync: { enabled: true, claim: 'groups' },
|
||||||
|
samlFallback: {
|
||||||
|
configured: true,
|
||||||
|
loginUrl: 'https://sso.example.com/realms/mosaic/protocol/saml',
|
||||||
|
},
|
||||||
|
warnings: [],
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(html).toContain('WorkOS');
|
||||||
|
expect(html).toContain('/api/auth/oauth2/callback/workos');
|
||||||
|
expect(html).toContain('Team sync claim: organization_id');
|
||||||
|
expect(html).toContain('SAML fallback: https://sso.example.com/realms/mosaic/protocol/saml');
|
||||||
|
});
|
||||||
|
});
|
||||||
67
apps/web/src/components/settings/sso-provider-section.tsx
Normal file
67
apps/web/src/components/settings/sso-provider-section.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import type { SsoProviderDiscovery } from '@/lib/sso';
|
||||||
|
|
||||||
|
interface SsoProviderSectionProps {
|
||||||
|
providers: SsoProviderDiscovery[];
|
||||||
|
loading: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SsoProviderSection({
|
||||||
|
providers,
|
||||||
|
loading,
|
||||||
|
}: SsoProviderSectionProps): React.ReactElement {
|
||||||
|
if (loading) {
|
||||||
|
return <p className="text-sm text-text-muted">Loading SSO providers...</p>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const configuredProviders = providers.filter((provider) => provider.configured);
|
||||||
|
|
||||||
|
if (providers.length === 0 || configuredProviders.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className="rounded-lg border border-surface-border bg-surface-card p-4">
|
||||||
|
<p className="text-sm text-text-muted">
|
||||||
|
No SSO providers configured. Set WorkOS or Keycloak environment variables to enable SSO.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{configuredProviders.map((provider) => (
|
||||||
|
<div
|
||||||
|
key={provider.id}
|
||||||
|
className="rounded-lg border border-surface-border bg-surface-card p-4"
|
||||||
|
>
|
||||||
|
<div className="flex items-center justify-between gap-4">
|
||||||
|
<div>
|
||||||
|
<h3 className="text-sm font-medium text-text-primary">{provider.name}</h3>
|
||||||
|
<p className="text-xs text-text-muted">
|
||||||
|
{provider.protocols.join(' + ').toUpperCase()}
|
||||||
|
{provider.loginMode ? ` • primary ${provider.loginMode.toUpperCase()}` : ''}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<span className="rounded-full border border-accent/30 bg-accent/10 px-2 py-1 text-xs font-medium text-accent">
|
||||||
|
Enabled
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-3 space-y-2 text-xs text-text-muted">
|
||||||
|
{provider.callbackPath && <p>Callback: {provider.callbackPath}</p>}
|
||||||
|
{provider.teamSync.enabled && provider.teamSync.claim && (
|
||||||
|
<p>Team sync claim: {provider.teamSync.claim}</p>
|
||||||
|
)}
|
||||||
|
{provider.samlFallback.configured && provider.samlFallback.loginUrl && (
|
||||||
|
<p>SAML fallback: {provider.samlFallback.loginUrl}</p>
|
||||||
|
)}
|
||||||
|
{provider.warnings.map((warning) => (
|
||||||
|
<p key={warning} className="text-warning">
|
||||||
|
{warning}
|
||||||
|
</p>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
89
apps/web/src/components/ui/mosaic-logo.tsx
Normal file
89
apps/web/src/components/ui/mosaic-logo.tsx
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import type { CSSProperties } from 'react';
|
||||||
|
|
||||||
|
export interface MosaicLogoProps {
|
||||||
|
size?: number;
|
||||||
|
spinning?: boolean;
|
||||||
|
spinDuration?: number;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MosaicLogo({
|
||||||
|
size = 36,
|
||||||
|
spinning = false,
|
||||||
|
spinDuration = 20,
|
||||||
|
className = '',
|
||||||
|
}: MosaicLogoProps): React.JSX.Element {
|
||||||
|
const scale = size / 36;
|
||||||
|
const squareSize = Math.round(14 * scale);
|
||||||
|
const circleSize = Math.round(11 * scale);
|
||||||
|
const borderRadius = Math.round(3 * scale);
|
||||||
|
|
||||||
|
const animationValue = spinning
|
||||||
|
? `mosaicLogoSpin ${String(spinDuration)}s linear infinite`
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const containerStyle: CSSProperties = {
|
||||||
|
width: size,
|
||||||
|
height: size,
|
||||||
|
position: 'relative',
|
||||||
|
flexShrink: 0,
|
||||||
|
animation: animationValue,
|
||||||
|
transformOrigin: 'center',
|
||||||
|
};
|
||||||
|
|
||||||
|
const baseSquareStyle: CSSProperties = {
|
||||||
|
position: 'absolute',
|
||||||
|
width: squareSize,
|
||||||
|
height: squareSize,
|
||||||
|
borderRadius,
|
||||||
|
};
|
||||||
|
|
||||||
|
const circleStyle: CSSProperties = {
|
||||||
|
position: 'absolute',
|
||||||
|
top: '50%',
|
||||||
|
left: '50%',
|
||||||
|
transform: 'translate(-50%, -50%)',
|
||||||
|
width: circleSize,
|
||||||
|
height: circleSize,
|
||||||
|
borderRadius: '50%',
|
||||||
|
background: 'var(--ms-pink-500)',
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{spinning ? (
|
||||||
|
<style>{`
|
||||||
|
@keyframes mosaicLogoSpin {
|
||||||
|
from { transform: rotate(0deg); }
|
||||||
|
to { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
`}</style>
|
||||||
|
) : null}
|
||||||
|
<div style={containerStyle} className={className} role="img" aria-label="Mosaic logo">
|
||||||
|
<div style={{ ...baseSquareStyle, top: 0, left: 0, background: 'var(--ms-blue-500)' }} />
|
||||||
|
<div style={{ ...baseSquareStyle, top: 0, right: 0, background: 'var(--ms-purple-500)' }} />
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
...baseSquareStyle,
|
||||||
|
bottom: 0,
|
||||||
|
right: 0,
|
||||||
|
background: 'var(--ms-teal-500)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
...baseSquareStyle,
|
||||||
|
bottom: 0,
|
||||||
|
left: 0,
|
||||||
|
background: 'var(--ms-amber-500)',
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<div style={circleStyle} />
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MosaicLogo;
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
import { createAuthClient } from 'better-auth/react';
|
import { createAuthClient } from 'better-auth/react';
|
||||||
import { adminClient } from 'better-auth/client/plugins';
|
import { adminClient, genericOAuthClient } from 'better-auth/client/plugins';
|
||||||
|
|
||||||
export const authClient = createAuthClient({
|
export const authClient = createAuthClient({
|
||||||
baseURL: process.env['NEXT_PUBLIC_GATEWAY_URL'] ?? 'http://localhost:4000',
|
baseURL: process.env['NEXT_PUBLIC_GATEWAY_URL'] ?? 'http://localhost:4000',
|
||||||
plugins: [adminClient()],
|
plugins: [adminClient(), genericOAuthClient()],
|
||||||
});
|
});
|
||||||
|
|
||||||
export const { useSession, signIn, signUp, signOut } = authClient;
|
export const { useSession, signIn, signUp, signOut } = authClient;
|
||||||
|
|||||||
48
apps/web/src/lib/sso-providers.test.ts
Normal file
48
apps/web/src/lib/sso-providers.test.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
import { getEnabledSsoProviders, getSsoProvider } from './sso-providers';
|
||||||
|
|
||||||
|
describe('sso-providers', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllEnvs();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the enabled providers in login button order', () => {
|
||||||
|
vi.stubEnv('NEXT_PUBLIC_WORKOS_ENABLED', 'true');
|
||||||
|
vi.stubEnv('NEXT_PUBLIC_KEYCLOAK_ENABLED', 'true');
|
||||||
|
|
||||||
|
expect(getEnabledSsoProviders()).toEqual([
|
||||||
|
{
|
||||||
|
id: 'workos',
|
||||||
|
buttonLabel: 'Continue with WorkOS',
|
||||||
|
description: 'Enterprise SSO via WorkOS',
|
||||||
|
enabled: true,
|
||||||
|
href: '/auth/provider/workos',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'keycloak',
|
||||||
|
buttonLabel: 'Continue with Keycloak',
|
||||||
|
description: 'Enterprise SSO via Keycloak',
|
||||||
|
enabled: true,
|
||||||
|
href: '/auth/provider/keycloak',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('marks disabled providers without exposing them in the enabled list', () => {
|
||||||
|
vi.stubEnv('NEXT_PUBLIC_WORKOS_ENABLED', 'true');
|
||||||
|
vi.stubEnv('NEXT_PUBLIC_KEYCLOAK_ENABLED', 'false');
|
||||||
|
|
||||||
|
expect(getEnabledSsoProviders().map((provider) => provider.id)).toEqual(['workos']);
|
||||||
|
expect(getSsoProvider('keycloak')).toEqual({
|
||||||
|
id: 'keycloak',
|
||||||
|
buttonLabel: 'Continue with Keycloak',
|
||||||
|
description: 'Enterprise SSO via Keycloak',
|
||||||
|
enabled: false,
|
||||||
|
href: '/auth/provider/keycloak',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for unknown providers', () => {
|
||||||
|
expect(getSsoProvider('authentik')).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
53
apps/web/src/lib/sso-providers.ts
Normal file
53
apps/web/src/lib/sso-providers.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
export type SsoProviderId = 'workos' | 'keycloak';
|
||||||
|
|
||||||
|
export interface SsoProvider {
|
||||||
|
id: SsoProviderId;
|
||||||
|
buttonLabel: string;
|
||||||
|
description: string;
|
||||||
|
enabled: boolean;
|
||||||
|
href: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PROVIDER_METADATA: Record<SsoProviderId, Omit<SsoProvider, 'enabled' | 'href'>> = {
|
||||||
|
workos: {
|
||||||
|
id: 'workos',
|
||||||
|
buttonLabel: 'Continue with WorkOS',
|
||||||
|
description: 'Enterprise SSO via WorkOS',
|
||||||
|
},
|
||||||
|
keycloak: {
|
||||||
|
id: 'keycloak',
|
||||||
|
buttonLabel: 'Continue with Keycloak',
|
||||||
|
description: 'Enterprise SSO via Keycloak',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getEnabledSsoProviders(): SsoProvider[] {
|
||||||
|
return (Object.keys(PROVIDER_METADATA) as SsoProviderId[])
|
||||||
|
.map((providerId) => getSsoProvider(providerId))
|
||||||
|
.filter((provider): provider is SsoProvider => provider?.enabled === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSsoProvider(providerId: string): SsoProvider | null {
|
||||||
|
if (!isSsoProviderId(providerId)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...PROVIDER_METADATA[providerId],
|
||||||
|
enabled: isSsoProviderEnabled(providerId),
|
||||||
|
href: `/auth/provider/${providerId}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSsoProviderId(value: string): value is SsoProviderId {
|
||||||
|
return value === 'workos' || value === 'keycloak';
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSsoProviderEnabled(providerId: SsoProviderId): boolean {
|
||||||
|
switch (providerId) {
|
||||||
|
case 'workos':
|
||||||
|
return process.env['NEXT_PUBLIC_WORKOS_ENABLED'] === 'true';
|
||||||
|
case 'keycloak':
|
||||||
|
return process.env['NEXT_PUBLIC_KEYCLOAK_ENABLED'] === 'true';
|
||||||
|
}
|
||||||
|
}
|
||||||
20
apps/web/src/lib/sso.ts
Normal file
20
apps/web/src/lib/sso.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
export type SsoProtocol = 'oidc' | 'saml';
|
||||||
|
export type SsoLoginMode = 'oidc' | 'saml' | null;
|
||||||
|
|
||||||
|
export interface SsoProviderDiscovery {
|
||||||
|
id: 'authentik' | 'workos' | 'keycloak';
|
||||||
|
name: string;
|
||||||
|
protocols: SsoProtocol[];
|
||||||
|
configured: boolean;
|
||||||
|
loginMode: SsoLoginMode;
|
||||||
|
callbackPath: string | null;
|
||||||
|
teamSync: {
|
||||||
|
enabled: boolean;
|
||||||
|
claim: string | null;
|
||||||
|
};
|
||||||
|
samlFallback: {
|
||||||
|
configured: boolean;
|
||||||
|
loginUrl: string | null;
|
||||||
|
};
|
||||||
|
warnings: string[];
|
||||||
|
}
|
||||||
@@ -15,10 +15,41 @@ export interface Message {
|
|||||||
conversationId: string;
|
conversationId: string;
|
||||||
role: 'user' | 'assistant' | 'system';
|
role: 'user' | 'assistant' | 'system';
|
||||||
content: string;
|
content: string;
|
||||||
|
thinking?: string;
|
||||||
|
model?: string;
|
||||||
|
provider?: string;
|
||||||
|
promptTokens?: number;
|
||||||
|
completionTokens?: number;
|
||||||
|
totalTokens?: number;
|
||||||
metadata?: Record<string, unknown>;
|
metadata?: Record<string, unknown>;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Model definition returned by provider APIs. */
|
||||||
|
export interface ModelInfo {
|
||||||
|
id: string;
|
||||||
|
provider: string;
|
||||||
|
name: string;
|
||||||
|
reasoning: boolean;
|
||||||
|
contextWindow: number;
|
||||||
|
maxTokens: number;
|
||||||
|
inputTypes: Array<'text' | 'image'>;
|
||||||
|
cost: {
|
||||||
|
input: number;
|
||||||
|
output: number;
|
||||||
|
cacheRead: number;
|
||||||
|
cacheWrite: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Provider with associated models. */
|
||||||
|
export interface ProviderInfo {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
available: boolean;
|
||||||
|
models: ModelInfo[];
|
||||||
|
}
|
||||||
|
|
||||||
/** Task statuses. */
|
/** Task statuses. */
|
||||||
export type TaskStatus = 'not-started' | 'in-progress' | 'blocked' | 'done' | 'cancelled';
|
export type TaskStatus = 'not-started' | 'in-progress' | 'blocked' | 'done' | 'cancelled';
|
||||||
|
|
||||||
|
|||||||
62
apps/web/src/providers/theme-provider.tsx
Normal file
62
apps/web/src/providers/theme-provider.tsx
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { createContext, useContext, useEffect, useMemo, useState, type ReactNode } from 'react';
|
||||||
|
|
||||||
|
export type Theme = 'dark' | 'light';
|
||||||
|
|
||||||
|
interface ThemeContextValue {
|
||||||
|
theme: Theme;
|
||||||
|
toggleTheme: () => void;
|
||||||
|
setTheme: (theme: Theme) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'mosaic-theme';
|
||||||
|
const ThemeContext = createContext<ThemeContextValue | undefined>(undefined);
|
||||||
|
|
||||||
|
function getInitialTheme(): Theme {
|
||||||
|
if (typeof document === 'undefined') {
|
||||||
|
return 'dark';
|
||||||
|
}
|
||||||
|
|
||||||
|
return document.documentElement.getAttribute('data-theme') === 'light' ? 'light' : 'dark';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ThemeProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||||
|
const [theme, setThemeState] = useState<Theme>(getInitialTheme);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
document.documentElement.setAttribute('data-theme', theme);
|
||||||
|
window.localStorage.setItem(STORAGE_KEY, theme);
|
||||||
|
}, [theme]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const storedTheme = window.localStorage.getItem(STORAGE_KEY);
|
||||||
|
if (storedTheme === 'light' || storedTheme === 'dark') {
|
||||||
|
setThemeState(storedTheme);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.documentElement.setAttribute('data-theme', 'dark');
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const value = useMemo<ThemeContextValue>(
|
||||||
|
() => ({
|
||||||
|
theme,
|
||||||
|
toggleTheme: () => setThemeState((current) => (current === 'dark' ? 'light' : 'dark')),
|
||||||
|
setTheme: (nextTheme) => setThemeState(nextTheme),
|
||||||
|
}),
|
||||||
|
[theme],
|
||||||
|
);
|
||||||
|
|
||||||
|
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTheme(): ThemeContextValue {
|
||||||
|
const context = useContext(ThemeContext);
|
||||||
|
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('useTheme must be used within ThemeProvider');
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
@@ -1,45 +1,42 @@
|
|||||||
# Mission Manifest — MVP
|
# Mission Manifest — Harness Foundation
|
||||||
|
|
||||||
> Persistent document tracking full mission scope, status, and session history.
|
> Persistent document tracking full mission scope, status, and session history.
|
||||||
> Updated by the orchestrator at each phase transition and milestone completion.
|
> Updated by the orchestrator at each phase transition and milestone completion.
|
||||||
|
|
||||||
## Mission
|
## Mission
|
||||||
|
|
||||||
**ID:** mvp-20260312
|
**ID:** harness-20260321
|
||||||
**Statement:** Build Mosaic Stack v0.1.0 — a self-hosted, multi-user AI agent platform with web dashboard, TUI, remote control, shared memory, mission orchestration, and extensible skill/plugin architecture. All TypeScript. Pi as agent harness. Brain as knowledge layer. Queue as coordination backbone.
|
**Statement:** Transform Mosaic Stack from a functional demo into a real multi-provider, task-routing AI harness. Persist all conversations, integrate frontier LLM providers (Anthropic, OpenAI, OpenRouter, Z.ai, Ollama), build granular task-aware agent routing, harden agent sessions, replace cron with BullMQ, and design the channel protocol for future Matrix/remote integration.
|
||||||
**Phase:** Complete
|
**Phase:** Execution
|
||||||
**Current Milestone:** Phase 8: Polish & Beta (v0.1.0) — DONE
|
**Current Milestone:** M3: Provider Integration
|
||||||
**Progress:** 9 / 9 milestones
|
**Progress:** 2 / 7 milestones
|
||||||
**Status:** complete
|
**Status:** active
|
||||||
**Last Updated:** 2026-03-16 UTC
|
**Last Updated:** 2026-03-21 UTC
|
||||||
|
|
||||||
## Success Criteria
|
## Success Criteria
|
||||||
|
|
||||||
- [x] AC-1: Core chat flow — login, send message, streamed response, conversations persist
|
- [ ] AC-1: Send messages in TUI → restart TUI → resume conversation → agent has full history and context
|
||||||
- [x] AC-2: TUI integration — `mosaic tui` connects to gateway, same context as web
|
- [ ] AC-2: Route a coding task to Claude Opus 4.6, a simple question to Haiku, a summarization to GLM-5 — all via granular routing rules
|
||||||
- [x] AC-3: Discord remote control — bot responds, routes through gateway, threads work
|
- [ ] AC-3: Two users exist, User A's memory searches never return User B's data
|
||||||
- [x] AC-4: Gateway orchestration — multi-provider routing, fallback, concurrent sessions
|
- [ ] AC-4: `/model claude-sonnet-4-6` in TUI switches the active model for subsequent messages
|
||||||
- [x] AC-5: Task & project management — CRUD, kanban, mission tracking, brain MCP tools
|
- [ ] AC-5: `/agent coding-agent` in TUI switches to a different agent with different system prompt and tools
|
||||||
- [x] AC-6: Memory system — auto-capture, semantic search, preferences, log summarization
|
- [ ] AC-6: BullMQ jobs execute on schedule, failures retry with backoff, admin can inspect via `/api/admin/jobs`
|
||||||
- [x] AC-7: Auth & RBAC — email/password, Authentik SSO, role enforcement
|
- [ ] AC-7: Channel protocol document exists with Matrix integration points defined, reviewed, and approved
|
||||||
- [x] AC-8: Multi-provider LLM — 3+ providers routing correctly
|
- [ ] AC-8: Embeddings run on Ollama local models (no external API dependency for vector operations)
|
||||||
- [x] AC-9: MCP — gateway MCP endpoint, brain + queue tools via MCP
|
- [ ] AC-9: All five providers (Anthropic, OpenAI, OpenRouter, Z.ai, Ollama) connect, list models, and complete chat requests
|
||||||
- [x] AC-10: Deployment — `docker compose up` from clean state, CLI on bare metal
|
- [ ] AC-10: Routing transparency — TUI displays which model was selected and the routing reason for each response
|
||||||
- [x] AC-11: @mosaic/\* packages — all 7 migrated packages build, test, integrate
|
|
||||||
|
|
||||||
## Milestones
|
## Milestones
|
||||||
|
|
||||||
| # | ID | Name | Status | Branch | Issue | Started | Completed |
|
| # | ID | Name | Status | Branch | Issue | Started | Completed |
|
||||||
| --- | ------ | --------------------------------------- | ------ | ------ | ----- | ---------- | ---------- |
|
| --- | ------ | ---------------------------------- | ----------- | ------ | --------- | ---------- | ---------- |
|
||||||
| 0 | ms-157 | Phase 0: Foundation (v0.0.1) | done | — | — | 2026-03-13 | 2026-03-13 |
|
| 1 | ms-166 | Conversation Persistence & Context | done | — | #224–#231 | 2026-03-21 | 2026-03-21 |
|
||||||
| 1 | ms-158 | Phase 1: Core API (v0.0.2) | done | — | — | 2026-03-13 | 2026-03-13 |
|
| 2 | ms-167 | Security & Isolation | done | — | #232–#239 | 2026-03-21 | 2026-03-21 |
|
||||||
| 2 | ms-159 | Phase 2: Agent Layer (v0.0.3) | done | — | — | 2026-03-13 | 2026-03-12 |
|
| 3 | ms-168 | Provider Integration | in-progress | — | #240–#251 | 2026-03-21 | — |
|
||||||
| 3 | ms-160 | Phase 3: Web Dashboard (v0.0.4) | done | — | — | 2026-03-12 | 2026-03-13 |
|
| 4 | ms-169 | Agent Routing Engine | not-started | — | #252–#264 | — | — |
|
||||||
| 4 | ms-161 | Phase 4: Memory & Intelligence (v0.0.5) | done | — | — | 2026-03-13 | 2026-03-13 |
|
| 5 | ms-170 | Agent Session Hardening | not-started | — | #265–#272 | — | — |
|
||||||
| 5 | ms-162 | Phase 5: Remote Control (v0.0.6) | done | — | #99 | 2026-03-14 | 2026-03-14 |
|
| 6 | ms-171 | Job Queue Foundation | not-started | — | #273–#280 | — | — |
|
||||||
| 6 | ms-163 | Phase 6: CLI & Tools (v0.0.7) | done | — | #104 | 2026-03-14 | 2026-03-14 |
|
| 7 | ms-172 | Channel Protocol Design | not-started | — | #281–#288 | — | — |
|
||||||
| 7 | ms-164 | Phase 7: Feature Completion (v0.0.8) | done | — | — | 2026-03-15 | 2026-03-15 |
|
|
||||||
| 8 | ms-165 | Phase 8: Polish & Beta (v0.1.0) | done | — | — | 2026-03-15 | 2026-03-15 |
|
|
||||||
|
|
||||||
## Deployment
|
## Deployment
|
||||||
|
|
||||||
@@ -48,6 +45,12 @@
|
|||||||
| Docker Compose (dev) | localhost | docker compose up |
|
| Docker Compose (dev) | localhost | docker compose up |
|
||||||
| Production | TBD | Docker Swarm via Portainer |
|
| Production | TBD | Docker Swarm via Portainer |
|
||||||
|
|
||||||
|
## Coordination
|
||||||
|
|
||||||
|
- **Primary Agent:** claude-opus-4-6
|
||||||
|
- **Sibling Agents:** codex (for pure coding tasks), sonnet (for review/standard work)
|
||||||
|
- **Shared Contracts:** docs/PRD-Harness_Foundation.md, docs/TASKS.md
|
||||||
|
|
||||||
## Token Budget
|
## Token Budget
|
||||||
|
|
||||||
| Metric | Value |
|
| Metric | Value |
|
||||||
@@ -58,22 +61,10 @@
|
|||||||
|
|
||||||
## Session History
|
## Session History
|
||||||
|
|
||||||
| Session | Runtime | Started | Duration | Ended Reason | Last Task |
|
| Session | Runtime | Started | Duration | Ended Reason | Last Task |
|
||||||
| ------- | ----------------- | -------------------- | -------- | ------------- | ---------------- |
|
| ------- | --------------- | ---------- | -------- | ------------ | ------------- |
|
||||||
| 1 | claude-opus-4-6 | 2026-03-13 01:00 UTC | — | context limit | Planning gate |
|
| 1 | claude-opus-4-6 | 2026-03-21 | — | — | Planning gate |
|
||||||
| 2 | claude-opus-4-6 | 2026-03-13 | — | context limit | P5-002, P6-005 |
|
|
||||||
| 3 | claude-opus-4-6 | 2026-03-13 | — | context limit | P0-006 |
|
|
||||||
| 4 | claude-opus-4-6 | 2026-03-12 | — | context limit | Docker fix |
|
|
||||||
| 5 | claude-opus-4-6 | 2026-03-12 | — | context limit | P1-009 |
|
|
||||||
| 6 | claude-opus-4-6 | 2026-03-12 | — | context limit | P2-006, FIX-01 |
|
|
||||||
| 7 | claude-opus-4-6 | 2026-03-12 | — | context limit | P2-007 |
|
|
||||||
| 8 | claude-opus-4-6 | 2026-03-12 | — | context limit | Phase 2 complete |
|
|
||||||
| 9 | claude-opus-4-6 | 2026-03-12 | — | context limit | P3-007 |
|
|
||||||
| 10 | claude-opus-4-6 | 2026-03-13 | — | context limit | P3-008 |
|
|
||||||
| 11 | claude-opus-4-6 | 2026-03-14 | — | context limit | P7 rescope |
|
|
||||||
| 12 | claude-opus-4-6 | 2026-03-15 | — | context limit | P7 planning |
|
|
||||||
| 13 | claude-sonnet-4-6 | 2026-03-16 | — | complete | P8-019 verify |
|
|
||||||
|
|
||||||
## Scratchpad
|
## Scratchpad
|
||||||
|
|
||||||
Path: `docs/scratchpads/mvp-20260312.md`
|
Path: `docs/scratchpads/harness-20260321.md`
|
||||||
|
|||||||
164
docs/PERFORMANCE.md
Normal file
164
docs/PERFORMANCE.md
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
# Performance Optimization — P8-003
|
||||||
|
|
||||||
|
**Branch:** `feat/p8-003-performance`
|
||||||
|
**Target metrics:** <200 ms TTFB, <2 s page loads
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Was Profiled
|
||||||
|
|
||||||
|
The following areas were reviewed through static analysis and code-path tracing
|
||||||
|
(no production traffic available; findings are based on measurable code-level patterns):
|
||||||
|
|
||||||
|
| Area | Findings |
|
||||||
|
| ---------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `packages/db` | Connection pool unbounded (default 10, no idle/connect timeout) |
|
||||||
|
| `apps/gateway/src/preferences` | N+1 round-trip on every pref upsert (SELECT + INSERT/UPDATE) |
|
||||||
|
| `packages/brain/src/conversations` | Unbounded list queries — no `LIMIT` or `ORDER BY` |
|
||||||
|
| `packages/db/src/schema` | Missing hot-path indexes: auth session lookup, OAuth callback, conversation list, agent-log tier queries |
|
||||||
|
| `apps/gateway/src/gc` | Cold-start GC blocked NestJS bootstrap (synchronous `await` in `onModuleInit`) |
|
||||||
|
| `apps/web/next.config.ts` | Missing `compress: true`, no `productionBrowserSourceMaps: false`, no image format config |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Changes Made
|
||||||
|
|
||||||
|
### 1. DB Connection Pool — `packages/db/src/client.ts`
|
||||||
|
|
||||||
|
**Problem:** `postgres()` was called with no pool config. The default max of 10 connections
|
||||||
|
and no idle/connect timeouts meant the pool could hang indefinitely on a stale TCP connection.
|
||||||
|
|
||||||
|
**Fix:**
|
||||||
|
|
||||||
|
- `max`: 20 connections (configurable via `DB_POOL_MAX`)
|
||||||
|
- `idle_timeout`: 30 s (configurable via `DB_IDLE_TIMEOUT`) — recycle stale connections
|
||||||
|
- `connect_timeout`: 5 s (configurable via `DB_CONNECT_TIMEOUT`) — fail fast on unreachable DB
|
||||||
|
|
||||||
|
**Expected impact:** Eliminates pool exhaustion under moderate concurrency; removes indefinite
|
||||||
|
hangs when the DB is temporarily unreachable.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. Preferences Upsert — `apps/gateway/src/preferences/preferences.service.ts`
|
||||||
|
|
||||||
|
**Problem:** `upsertPref` executed two serial DB round-trips on every preference write:
|
||||||
|
|
||||||
|
```
|
||||||
|
1. SELECT id FROM preferences WHERE user_id = ? AND key = ? (→ check exists)
|
||||||
|
2a. UPDATE preferences SET value = ? … (→ if found)
|
||||||
|
2b. INSERT INTO preferences … (→ if not found)
|
||||||
|
```
|
||||||
|
|
||||||
|
Under concurrency this also had a TOCTOU race window.
|
||||||
|
|
||||||
|
**Fix:** Replaced with single-statement `INSERT … ON CONFLICT DO UPDATE`:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
INSERT INTO preferences (user_id, key, value, mutable)
|
||||||
|
VALUES (?, ?, ?, true)
|
||||||
|
ON CONFLICT (user_id, key) DO UPDATE SET value = excluded.value, updated_at = now();
|
||||||
|
```
|
||||||
|
|
||||||
|
This required promoting `preferences_user_key_idx` from a plain index to a `UNIQUE INDEX`
|
||||||
|
(see migration `0003_p8003_perf_indexes.sql`).
|
||||||
|
|
||||||
|
**Expected impact:** ~50% reduction in DB round-trips for preference writes; eliminates
|
||||||
|
the race window.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Missing DB Indexes — `packages/db/src/schema.ts` + migration
|
||||||
|
|
||||||
|
The following indexes were added or replaced to cover common query patterns:
|
||||||
|
|
||||||
|
| Table | Old indexes | New / changed |
|
||||||
|
| --------------- | ------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
||||||
|
| `sessions` | _(none)_ | `sessions_user_id_idx(user_id)`, `sessions_expires_at_idx(expires_at)` |
|
||||||
|
| `accounts` | _(none)_ | `accounts_provider_account_idx(provider_id, account_id)`, `accounts_user_id_idx(user_id)` |
|
||||||
|
| `conversations` | `(user_id)`, `(archived)` separate | `conversations_user_archived_idx(user_id, archived)` compound |
|
||||||
|
| `agent_logs` | `(session_id)`, `(tier)`, `(created_at)` separate | `agent_logs_session_tier_idx(session_id, tier)`, `agent_logs_tier_created_at_idx(tier, created_at)` |
|
||||||
|
| `preferences` | non-unique `(user_id, key)` | **unique** `(user_id, key)` — required for `ON CONFLICT` |
|
||||||
|
|
||||||
|
**Expected impact:**
|
||||||
|
|
||||||
|
- Auth session validation (hot path on every request): from seq scan → index scan
|
||||||
|
- OAuth callback account lookup: from seq scan → index scan
|
||||||
|
- Conversation list (dashboard load): compound index covers `WHERE user_id = ? ORDER BY updated_at`
|
||||||
|
- Log summarisation cron: `(tier, created_at)` index enables efficient hot→warm promotion query
|
||||||
|
|
||||||
|
All changes are in `packages/db/drizzle/0003_p8003_perf_indexes.sql`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. Conversation Queries — `packages/brain/src/conversations.ts`
|
||||||
|
|
||||||
|
**Problem:** `findAll(userId)` and `findMessages(conversationId)` were unbounded — no `LIMIT`
|
||||||
|
and `findAll` had no `ORDER BY`, so the DB planner may not use the index efficiently.
|
||||||
|
|
||||||
|
**Fix:**
|
||||||
|
|
||||||
|
- `findAll`: `ORDER BY updated_at DESC LIMIT 200` — returns most-recent conversations first
|
||||||
|
- `findMessages`: `ORDER BY created_at ASC LIMIT 500` — chronological message history
|
||||||
|
|
||||||
|
**Expected impact:** Prevents accidental full-table scans on large datasets; ensures the
|
||||||
|
frontend receives a usable, ordered result set regardless of table growth.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. Cold-Start GC — `apps/gateway/src/gc/session-gc.service.ts`
|
||||||
|
|
||||||
|
**Problem:** `onModuleInit()` was `async` and `await`-ed `fullCollect()`, which blocked the
|
||||||
|
NestJS module initialization chain. Full GC — which calls `redis.keys('mosaic:session:*')` and
|
||||||
|
a DB query — typically takes 100–500 ms. This directly added to startup TTFB.
|
||||||
|
|
||||||
|
**Fix:** Made `onModuleInit()` synchronous and used `.then().catch()` to run GC in the
|
||||||
|
background. The first HTTP request is no longer delayed by GC work.
|
||||||
|
|
||||||
|
**Expected impact:** Removes 100–500 ms from cold-start TTFB.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. Next.js Config — `apps/web/next.config.ts`
|
||||||
|
|
||||||
|
**Problem:** `compress: true` was not set, so response payloads were uncompressed. No image
|
||||||
|
format optimization or source-map suppression was configured.
|
||||||
|
|
||||||
|
**Fix:**
|
||||||
|
|
||||||
|
- `compress: true` — enables gzip/brotli for all Next.js responses
|
||||||
|
- `productionBrowserSourceMaps: false` — reduces build output size
|
||||||
|
- `images.formats: ['image/avif', 'image/webp']` — Next.js Image component will serve modern
|
||||||
|
formats to browsers that support them (typically 40–60% smaller than JPEG/PNG)
|
||||||
|
|
||||||
|
**Expected impact:** Typical HTML/JSON gzip savings of 60–80%; image serving cost reduced
|
||||||
|
for any `<Image>` components added in the future.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Was Not Changed (Intentionally)
|
||||||
|
|
||||||
|
- **Caching layer (Valkey/Redis):** The `SystemOverrideService` and GC already use Redis
|
||||||
|
pipelines. `PreferencesService.getEffective()` reads all user prefs in one query — this
|
||||||
|
is appropriate for the data size and doesn't warrant an additional cache layer yet.
|
||||||
|
- **WebSocket backpressure:** The `ChatGateway` already drops events for disconnected clients
|
||||||
|
(`client.connected` check) and cleans up listeners on disconnect. No memory leak was found.
|
||||||
|
- **Plugin/skill loader startup:** `SkillLoaderService.loadForSession()` is called on first
|
||||||
|
session creation, not on startup. Already non-blocking.
|
||||||
|
- **Frontend React memoization:** No specific hot components were identified as causing
|
||||||
|
excessive re-renders without profiling data. No speculative `memo()` calls added.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## How to Apply
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run the DB migration (requires a live DB)
|
||||||
|
pnpm --filter @mosaic/db exec drizzle-kit migrate
|
||||||
|
|
||||||
|
# Or, in Docker/Swarm — migrations run automatically on gateway startup
|
||||||
|
# via runMigrations() in packages/db/src/migrate.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
_Generated by P8-003 performance optimization task — 2026-03-18_
|
||||||
391
docs/PRD-Harness_Foundation.md
Normal file
391
docs/PRD-Harness_Foundation.md
Normal file
@@ -0,0 +1,391 @@
|
|||||||
|
# PRD: Harness Foundation — Phase 9
|
||||||
|
|
||||||
|
## Metadata
|
||||||
|
|
||||||
|
- **Owner:** Jason Woltje
|
||||||
|
- **Date:** 2026-03-21
|
||||||
|
- **Status:** draft
|
||||||
|
- **Phase:** 9 (post-MVP)
|
||||||
|
- **Version Target:** v0.2.0
|
||||||
|
- **Agent Harness:** [Pi SDK](https://github.com/badlogic/pi-mono)
|
||||||
|
- **Best-Guess Mode:** true
|
||||||
|
- **Repo:** `git.mosaicstack.dev/mosaic/mosaic-stack`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Problem Statement
|
||||||
|
|
||||||
|
Mosaic Stack v0.1.0 delivered a functional skeleton — gateway boots, TUI connects, single-agent chat streams, basic auth works. But the system is not usable as a daily-driver harness:
|
||||||
|
|
||||||
|
1. **Chat messages are fire-and-forget.** The WebSocket gateway never calls ConversationsRepo. Context is lost on disconnect. Conversations can't be resumed with history. Cross-interface continuity (TUI → WebUI → Matrix) is impossible.
|
||||||
|
|
||||||
|
2. **Single provider (Ollama) with local models only.** No access to frontier models (Claude Opus 4.6, Codex gpt-5.4, GLM-5). The routing engine exists but has never been tested with real providers.
|
||||||
|
|
||||||
|
3. **No task-aware agent routing.** A coding task and a summarization task route to the same agent with the same model. There is no mechanism to match tasks to agents by capability, cost tier, or specialization.
|
||||||
|
|
||||||
|
4. **Memory is not user-scoped.** Insight vector search returns all users' data. Deploying multi-user is a security violation.
|
||||||
|
|
||||||
|
5. **Agent configs exist in DB but are ignored.** Stored system prompts, model preferences, and tool allowlists don't apply to sessions. The `/model` and `/agent` slash commands are stubbed.
|
||||||
|
|
||||||
|
6. **No job queue.** Background processing (summarization, GC, tier management) runs on fragile cron. No retry, no monitoring, no async task dispatch foundation for future agent orchestration.
|
||||||
|
|
||||||
|
7. **Plugin system is hollow.** Zero implementations. No defined message protocol. Blocks all remote interfaces (Matrix, Discord, Telegram) planned for Phase 10+.
|
||||||
|
|
||||||
|
**What this phase solves:** Transform Mosaic from a demo into a real multi-provider, task-routing AI harness that persists everything, routes intelligently, and is architecturally ready for multi-agent and remote control.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Objectives
|
||||||
|
|
||||||
|
1. **Persistent conversations** — Every message saved, every conversation resumable, full context available across interfaces
|
||||||
|
2. **Multi-provider LLM access** — Anthropic, OpenAI, OpenRouter, Z.ai, Ollama with proper auth flows
|
||||||
|
3. **Task-aware agent routing** — Granular routing rules that match tasks to the right agent + model by capability, cost, and domain
|
||||||
|
4. **Security isolation** — All data queries user-scoped, ready for multi-user deployment
|
||||||
|
5. **Session hardening** — Agent configs apply, model/agent switching works mid-session
|
||||||
|
6. **Reliable background processing** — BullMQ job queue replaces fragile cron
|
||||||
|
7. **Channel protocol design** — Architecture for Matrix and remote interfaces, built into the foundation now
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
### In Scope
|
||||||
|
|
||||||
|
1. Conversation persistence — wire ChatGateway to ConversationsRepo, context loading on resume
|
||||||
|
2. Multi-provider integration — Anthropic, OpenAI, OpenRouter, Z.ai, Ollama with auth flows
|
||||||
|
3. Task-aware agent routing — granular routing rules with task classification and fallback chains
|
||||||
|
4. Security isolation — user-scoped queries on all data paths (memory, conversations, agents)
|
||||||
|
5. Agent session hardening — configs apply, model/agent switching, session resume
|
||||||
|
6. Job queue — BullMQ replacing cron for background processing
|
||||||
|
7. Channel protocol design — architecture document for Matrix and remote interfaces
|
||||||
|
8. Embedding migration — Ollama-local embeddings replacing OpenAI dependency
|
||||||
|
|
||||||
|
### Out of Scope
|
||||||
|
|
||||||
|
1. Matrix homeserver deployment + appservice (Phase 10)
|
||||||
|
2. Multi-agent orchestration / supervisor-worker pattern (Phase 10+)
|
||||||
|
3. WebUI rebuild (future)
|
||||||
|
4. Self-managing memory — compaction, merge, forget (future)
|
||||||
|
5. Team workspace isolation (future)
|
||||||
|
6. Remote channel plugins — WhatsApp, Discord, Telegram (Phase 10+, via Matrix)
|
||||||
|
7. Fine-grained RBAC — project/agent/team roles (future)
|
||||||
|
8. Agent-to-agent communication (Phase 10+)
|
||||||
|
|
||||||
|
## User/Stakeholder Requirements
|
||||||
|
|
||||||
|
1. As a user, I can resume a conversation after closing the TUI and the agent remembers the full context
|
||||||
|
2. As a user, I can use frontier models (Claude Opus 4.6, Codex gpt-5.4) without manual provider configuration
|
||||||
|
3. As a user, the system automatically selects the best model for my task (coding → powerful model, simple question → cheap model)
|
||||||
|
4. As a user, I can override the automatic model selection with `/model <name>` at any time
|
||||||
|
5. As a user, I can switch between specialized agents mid-session with `/agent <name>`
|
||||||
|
6. As an admin, I can define routing rules that control which models handle which task types
|
||||||
|
7. As an admin, I can monitor background job health and retry failed jobs
|
||||||
|
8. As a user, my conversations, memories, and preferences are invisible to other users
|
||||||
|
|
||||||
|
## Functional Requirements
|
||||||
|
|
||||||
|
1. FR-1: ChatGateway persists every message (user, assistant, tool call, thinking) to the conversations/messages tables
|
||||||
|
2. FR-2: On session resume with an existing conversationId, message history is loaded from DB and injected into the agent session context
|
||||||
|
3. FR-3: When conversation history exceeds 80% of the model's context window, older messages are summarized and prepended as a context checkpoint
|
||||||
|
4. FR-4: Five LLM providers are registered with the gateway: Anthropic (Claude Sonnet 4.6, Opus 4.6, Haiku 4.5), OpenAI (Codex gpt-5.4), OpenRouter (dynamic model list), Z.ai (GLM-5), Ollama (local models)
|
||||||
|
5. FR-5: Each provider supports API key auth; Anthropic and OpenAI additionally support OAuth (URL-display + callback pattern)
|
||||||
|
6. FR-6: Provider credentials are stored per-user in the DB (encrypted), not in environment variables
|
||||||
|
7. FR-7: A routing engine classifies each user message by taskType, complexity, domain, and required capabilities, then selects the optimal provider/model via priority-ordered rules
|
||||||
|
8. FR-8: Default routing rules are seeded on first run; admins can customize system-wide rules; users can set per-session overrides
|
||||||
|
9. FR-9: Routing decisions are transparent — the TUI shows which model was selected and why
|
||||||
|
10. FR-10: Agent configs (system prompt, default model, tool allowlist, skills) stored in DB are applied when creating agent sessions
|
||||||
|
11. FR-11: `/model <name>` switches the active model for subsequent messages in the current session
|
||||||
|
12. FR-12: `/agent <name>` switches to a different agent config, loading its system prompt, tools, and default model
|
||||||
|
13. FR-13: All memory queries (insight vector search, preferences) filter by userId
|
||||||
|
14. FR-14: BullMQ handles background jobs (summarization, GC, tier management) with retry, backoff, and monitoring
|
||||||
|
15. FR-15: Embeddings are served locally via Ollama (nomic-embed-text or mxbai-embed-large) with no external API dependency
|
||||||
|
|
||||||
|
## Non-Functional Requirements
|
||||||
|
|
||||||
|
1. **Security:** All data queries include userId filter. Provider credentials encrypted at rest. No cross-user data leakage. OAuth tokens stored securely with refresh handling.
|
||||||
|
2. **Performance:** Message persistence adds <50ms to message relay latency. Routing classification <100ms per message. Provider health checks run on configurable interval (default 60s) without blocking requests.
|
||||||
|
3. **Reliability:** BullMQ jobs retry with exponential backoff (3 attempts default). Provider failover: if primary provider is unhealthy, fallback chain activates automatically. Conversation context survives TUI restart.
|
||||||
|
4. **Observability:** Routing decisions logged with classification details. Job execution logged to agent_logs. Provider health status exposed via `/api/providers/health`. Session metrics (tokens, model switches, duration) persisted in DB.
|
||||||
|
|
||||||
|
## Acceptance Criteria
|
||||||
|
|
||||||
|
- [ ] AC-1: Send messages in TUI → restart TUI → resume conversation → agent has full history and context
|
||||||
|
- [ ] AC-2: Route a coding task to Claude Opus 4.6, a simple question to Haiku, a summarization to GLM-5 — all via granular routing rules
|
||||||
|
- [ ] AC-3: Two users exist, User A's memory searches never return User B's data
|
||||||
|
- [ ] AC-4: `/model claude-sonnet-4-6` in TUI switches the active model for subsequent messages
|
||||||
|
- [ ] AC-5: `/agent coding-agent` in TUI switches to a different agent with different system prompt and tools
|
||||||
|
- [ ] AC-6: BullMQ jobs execute on schedule, failures retry with backoff, admin can inspect via `/api/admin/jobs`
|
||||||
|
- [ ] AC-7: Channel protocol document exists with Matrix integration points defined, reviewed, and approved
|
||||||
|
- [ ] AC-8: Embeddings run on Ollama local models (no external API dependency for vector operations)
|
||||||
|
- [ ] AC-9: All five providers (Anthropic, OpenAI, OpenRouter, Z.ai, Ollama) connect, list models, and complete chat requests
|
||||||
|
- [ ] AC-10: Routing transparency — TUI displays which model was selected and the routing reason for each response
|
||||||
|
|
||||||
|
## Testing and Verification Expectations
|
||||||
|
|
||||||
|
1. **Baseline checks:** `pnpm typecheck`, `pnpm lint`, `pnpm format:check` — all green before any push
|
||||||
|
2. **Unit tests:** Routing engine rules matching, task classifier, provider adapter registration, message persistence
|
||||||
|
3. **Integration tests:** Two-user isolation (M2-007), provider round-trip (M3-012), routing end-to-end (M4-013), session resume with context (M1-008)
|
||||||
|
4. **Situational tests per milestone:** Each milestone has a verify task that exercises the delivered functionality end-to-end
|
||||||
|
5. **Evidence format:** Test output + manual verification notes in scratchpad per milestone
|
||||||
|
|
||||||
|
## Constraints and Dependencies
|
||||||
|
|
||||||
|
| Type | Item | Notes |
|
||||||
|
| ---------- | ------------------------------- | -------------------------------------------------------------------------------------- |
|
||||||
|
| Dependency | `@anthropic-ai/sdk` | npm, required for M3-002 |
|
||||||
|
| Dependency | `openai` | npm, required for M3-003 |
|
||||||
|
| Dependency | `bullmq` | npm, Valkey-compatible, required for M6 |
|
||||||
|
| Dependency | Ollama embedding models | `ollama pull nomic-embed-text`, required for M3-009 |
|
||||||
|
| Dependency | Pi SDK provider adapter support | ASSUMPTION: supported — verify in M3-001 |
|
||||||
|
| External | Anthropic OAuth credentials | Requires Anthropic Console setup |
|
||||||
|
| External | OpenAI OAuth credentials | Requires OpenAI Platform setup |
|
||||||
|
| External | Z.ai API key | Requires Z.ai account |
|
||||||
|
| External | OpenRouter API key | Requires OpenRouter account |
|
||||||
|
| Constraint | Valkey 8 compatibility | BullMQ requires Redis 6+; Valkey 8 is compatible |
|
||||||
|
| Constraint | Embedding dimension migration | Switching from 1536 (OpenAI) to 768/1024 (Ollama) requires re-embedding or fresh start |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Assumptions
|
||||||
|
|
||||||
|
1. ASSUMPTION: Pi SDK supports custom provider adapters for all target LLM providers. If not, adapters wrap native SDKs behind Pi's interface. **Rationale:** Gateway already uses Pi with Ollama via a custom adapter pattern.
|
||||||
|
2. ASSUMPTION: BullMQ is Valkey-compatible. **Rationale:** BullMQ documents Redis 6+ compatibility; Valkey 8 is Redis-compatible.
|
||||||
|
3. ASSUMPTION: Ollama can serve embedding models (nomic-embed-text, mxbai-embed-large) with acceptable quality. **Rationale:** Ollama supports embedding endpoints natively.
|
||||||
|
4. ASSUMPTION: Anthropic and OpenAI OAuth flows can be handled via URL-display + token callback pattern (same as existing provider auth). **Rationale:** Both providers offer standard OAuth 2.0 flows.
|
||||||
|
5. ASSUMPTION: Z.ai GLM-5 uses an API format compatible with OpenAI or has a documented SDK. **Rationale:** Most LLM providers converge on OpenAI-compatible APIs.
|
||||||
|
6. ASSUMPTION: The existing Pi SDK session model supports mid-session model switching without destroying session state. If not, we destroy and recreate with conversation history. **Rationale:** Acceptable fallback — context is persisted in DB.
|
||||||
|
7. ASSUMPTION: Channel protocol design can be completed without a running Matrix homeserver. **Rationale:** Matrix protocol is well-documented; design is architecture, not integration.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Milestones
|
||||||
|
|
||||||
|
### Milestone 1: Conversation Persistence & Context
|
||||||
|
|
||||||
|
**Goal:** Every message persisted. Every conversation resumable with full context.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | ------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| M1-001 | Wire ChatGateway.handleMessage() → ConversationsRepo.addMessage() for user messages |
|
||||||
|
| M1-002 | Wire agent event relay → ConversationsRepo.addMessage() for assistant responses (text, tool calls, thinking) |
|
||||||
|
| M1-003 | Store message metadata: model used, provider, token counts, tool call details, timestamps |
|
||||||
|
| M1-004 | On session resume (existing conversationId), load message history from DB and inject into Pi session context |
|
||||||
|
| M1-005 | Context window management: if history exceeds model context, summarize older messages and prepend summary |
|
||||||
|
| M1-006 | Conversation search: full-text search on messages table via `/api/conversations/search` |
|
||||||
|
| M1-007 | TUI: `/history` command to display conversation message count and context usage |
|
||||||
|
| M1-008 | Verify: send messages → kill TUI → resume with `-c <id>` → agent references prior context |
|
||||||
|
|
||||||
|
### Milestone 2: Security & Isolation
|
||||||
|
|
||||||
|
**Goal:** All data queries user-scoped. Safe for multi-user deployment.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | --------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| M2-001 | Audit InsightsRepo: add `userId` filter to `searchByEmbedding()` vector search |
|
||||||
|
| M2-002 | Audit InsightsRepo: add `userId` filter to `findByUser()`, `decayOldInsights()` |
|
||||||
|
| M2-003 | Audit PreferencesRepo: verify all queries filter by userId |
|
||||||
|
| M2-004 | Audit agent memory tools: verify `memory_search`, `memory_save_*`, `memory_get_*` all scope to session user |
|
||||||
|
| M2-005 | Audit ConversationsRepo: verify ownership check on findById, update, delete, addMessage, findMessages |
|
||||||
|
| M2-006 | Audit AgentsRepo: verify `findAccessible()` returns only user's agents + system agents |
|
||||||
|
| M2-007 | Add integration test: create two users, populate data for each, verify cross-user isolation on every query path |
|
||||||
|
| M2-008 | Audit Valkey keys: verify session keys include userId or are not enumerable across users |
|
||||||
|
|
||||||
|
### Milestone 3: Provider Integration
|
||||||
|
|
||||||
|
**Goal:** Five providers operational with proper auth, health checking, and capability metadata.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| M3-001 | Refactor ProviderService into provider adapter pattern: `IProviderAdapter` interface with `register()`, `listModels()`, `healthCheck()`, `createClient()` |
|
||||||
|
| M3-002 | Anthropic adapter: `@anthropic-ai/sdk`, register Claude Sonnet 4.6 + Opus 4.6, OAuth flow (URL display + callback), API key fallback |
|
||||||
|
| M3-003 | OpenAI adapter: `openai` SDK, register Codex gpt-5.4, OAuth flow, API key fallback |
|
||||||
|
| M3-004 | OpenRouter adapter: OpenAI-compatible client, API key auth, dynamic model list from `/api/v1/models` |
|
||||||
|
| M3-005 | Z.ai GLM adapter: register GLM-5, API key auth, research and implement API format |
|
||||||
|
| M3-006 | Ollama adapter: refactor existing Ollama integration into adapter pattern, add embedding model support |
|
||||||
|
| M3-007 | Provider health check: periodic probe (configurable interval), status per provider, expose via `/api/providers/health` |
|
||||||
|
| M3-008 | Model capability matrix: define per-model metadata (tier, context window, tool support, vision, streaming, embedding capable) |
|
||||||
|
| M3-009 | Refactor EmbeddingService: replace OpenAI-hardcoded client with provider-agnostic interface, Ollama as default (nomic-embed-text or mxbai-embed-large) |
|
||||||
|
| M3-010 | OAuth token storage: persist provider tokens per user in DB (encrypted), refresh flow |
|
||||||
|
| M3-011 | Provider config UI support: `/api/providers` CRUD for user-scoped provider credentials |
|
||||||
|
| M3-012 | Verify: each provider connects, lists models, completes a chat request, handles errors gracefully |
|
||||||
|
|
||||||
|
### Milestone 4: Agent Routing Engine
|
||||||
|
|
||||||
|
**Goal:** Granular, rule-based routing that matches tasks to the right agent and model by capability, cost, and domain specialization.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| M4-001 | Define routing rule schema: `RoutingRule { name, priority, conditions[], action }` stored in DB |
|
||||||
|
| M4-002 | Condition types: `taskType` (coding, research, summarization, conversation, analysis, creative), `complexity` (simple, moderate, complex), `domain` (frontend, backend, devops, docs, general), `costTier` (cheap, standard, premium), `requiredCapabilities` (tools, vision, long-context, reasoning) |
|
||||||
|
| M4-003 | Action types: `routeTo { provider, model, agentConfigId?, systemPromptOverride?, toolAllowlist? }` |
|
||||||
|
| M4-004 | Default routing rules (seed data): coding → Opus 4.6, simple Q&A → Sonnet 4.6, summarization → GLM-5, research → Codex gpt-5.4, local/offline → Ollama llama3.2 |
|
||||||
|
| M4-005 | Task classification: lightweight classifier that infers taskType + complexity from user message (can be rule-based regex/keyword initially, LLM-assisted later) |
|
||||||
|
| M4-006 | Routing decision pipeline: classify task → match rules by priority → select best available provider/model → fallback chain if primary unavailable |
|
||||||
|
| M4-007 | Routing override: user can force a specific model via `/model <name>` regardless of routing rules |
|
||||||
|
| M4-008 | Routing transparency: include routing decision in `session:info` event (why this model was selected) |
|
||||||
|
| M4-009 | Routing rules CRUD: `/api/routing/rules` — list, create, update, delete, reorder priority |
|
||||||
|
| M4-010 | Per-user routing overrides: users can customize default rules for their sessions |
|
||||||
|
| M4-011 | Agent specialization: agents can declare capabilities in their config (domains, preferred models, tool sets) |
|
||||||
|
| M4-012 | Routing integration: wire routing engine into ChatGateway — every new message triggers routing decision before agent dispatch |
|
||||||
|
| M4-013 | Verify: send a coding question → routed to Opus; send "summarize this" → routed to GLM-5; send "what time is it" → routed to cheap tier |
|
||||||
|
|
||||||
|
### Milestone 5: Agent Session Hardening
|
||||||
|
|
||||||
|
**Goal:** Agent configs apply to sessions. Model and agent switching work mid-session.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| M5-001 | Wire ChatGateway: on session create, load agent config from DB (system prompt, model, provider, tool allowlist, skills) |
|
||||||
|
| M5-002 | `/model <name>` command: end-to-end wiring — TUI → socket `command:execute` → gateway switches provider/model → new messages use new model |
|
||||||
|
| M5-003 | `/agent <name>` command: switch to different agent config mid-session — loads new system prompt, tools, and default model |
|
||||||
|
| M5-004 | Session ↔ conversation binding: persist sessionId on conversation record, allow session resume via conversation ID |
|
||||||
|
| M5-005 | Session info broadcast: on model/agent switch, emit `session:info` with updated provider, model, agent name |
|
||||||
|
| M5-006 | Agent creation from TUI: `/agent new` command creates agent config via gateway API |
|
||||||
|
| M5-007 | Session metrics: track per-session token usage, model switches, duration — persist in DB |
|
||||||
|
| M5-008 | Verify: start TUI → `/model claude-opus-4-6` → verify response uses Opus → `/agent research-bot` → verify system prompt changes |
|
||||||
|
|
||||||
|
### Milestone 6: Job Queue Foundation
|
||||||
|
|
||||||
|
**Goal:** Reliable background processing via BullMQ. Foundation for future agent task orchestration.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | ------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| M6-001 | Add BullMQ dependency, configure with Valkey connection |
|
||||||
|
| M6-002 | Create queue service: typed job definitions, worker registration, error handling with exponential backoff |
|
||||||
|
| M6-003 | Migrate summarization cron → BullMQ repeatable job |
|
||||||
|
| M6-004 | Migrate GC (session cleanup) → BullMQ repeatable job |
|
||||||
|
| M6-005 | Migrate tier management (log archival) → BullMQ repeatable job |
|
||||||
|
| M6-006 | Admin jobs API: `GET /api/admin/jobs` — list active/completed/failed jobs, retry failed, pause/resume queues |
|
||||||
|
| M6-007 | Job event logging: emit job start/complete/fail events to agent_logs for observability |
|
||||||
|
| M6-008 | Verify: jobs execute on schedule, deliberate failure retries with backoff, admin endpoint shows job history |
|
||||||
|
|
||||||
|
### Milestone 7: Channel Protocol Design
|
||||||
|
|
||||||
|
**Goal:** Architecture document defining how remote interfaces (Matrix, Discord, Telegram) will integrate. No code — design only. Built into foundation now so Phase 10+ doesn't require gateway rewrites.
|
||||||
|
|
||||||
|
| Task | Description |
|
||||||
|
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
|
| M7-001 | Define `IChannelAdapter` interface: lifecycle (connect, disconnect, health), message flow (receiveMessage → gateway, sendMessage ← gateway), identity mapping (channel user ↔ Mosaic user) |
|
||||||
|
| M7-002 | Define channel message protocol: canonical message format that all adapters translate to/from (content, metadata, attachments, thread context) |
|
||||||
|
| M7-003 | Design Matrix integration: appservice registration, room ↔ conversation mapping, space ↔ team mapping, agent ghost users, power levels for human observation |
|
||||||
|
| M7-004 | Design conversation multiplexing: same conversation accessible from TUI + WebUI + Matrix simultaneously, real-time sync via gateway events |
|
||||||
|
| M7-005 | Design remote auth bridging: how a Matrix/Discord message authenticates to Mosaic (token linking, OAuth bridge, invite-based provisioning) |
|
||||||
|
| M7-006 | Design agent-to-agent communication via Matrix rooms: room per agent pair, human can join to observe, message format for structured agent dialogue |
|
||||||
|
| M7-007 | Design multi-user isolation in Matrix: space-per-team, room visibility rules, encryption considerations, admin visibility |
|
||||||
|
| M7-008 | Publish architecture doc: `docs/architecture/channel-protocol.md` — reviewed and approved before Phase 10 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Technical Approach
|
||||||
|
|
||||||
|
### Pi SDK Provider Adapter Pattern
|
||||||
|
|
||||||
|
The agent layer stays on Pi SDK. Provider diversity is solved at the adapter layer below Pi:
|
||||||
|
|
||||||
|
```
|
||||||
|
Provider SDKs (@anthropic-ai/sdk, openai, etc.)
|
||||||
|
→ IProviderAdapter implementations
|
||||||
|
→ ProviderRegistry (Pi SDK compatible)
|
||||||
|
→ Agent Session (Pi SDK) — tool loops, streaming, context
|
||||||
|
→ AgentService — lifecycle, routing, events
|
||||||
|
→ ChatGateway — WebSocket to all interfaces
|
||||||
|
```
|
||||||
|
|
||||||
|
Adding a provider means implementing `IProviderAdapter`. Everything above stays unchanged.
|
||||||
|
|
||||||
|
### Routing Decision Flow
|
||||||
|
|
||||||
|
```
|
||||||
|
User sends message
|
||||||
|
→ Task classifier (regex/keyword, optionally LLM-assisted)
|
||||||
|
→ { taskType, complexity, domain, requiredCapabilities }
|
||||||
|
→ RoutingEngine.resolve(classification, userOverrides, availableProviders)
|
||||||
|
→ Match rules by priority
|
||||||
|
→ Check provider health
|
||||||
|
→ Apply fallback chain
|
||||||
|
→ Return { provider, model, agentConfigId }
|
||||||
|
→ AgentService.createOrResumeSession(routingResult)
|
||||||
|
→ Session uses selected provider/model
|
||||||
|
→ Emit session:info with routing decision explanation
|
||||||
|
```
|
||||||
|
|
||||||
|
### Embedding Strategy
|
||||||
|
|
||||||
|
Replace OpenAI-hardcoded embedding service with provider-agnostic interface:
|
||||||
|
|
||||||
|
- **Default:** Ollama serving `nomic-embed-text` (768-dim) or `mxbai-embed-large` (1024-dim)
|
||||||
|
- **Fallback:** Any OpenAI-compatible embedding API
|
||||||
|
- **Migration:** Update pgvector column dimension if switching from 1536 (OpenAI) to 768/1024 (Ollama models)
|
||||||
|
- **No external API dependency** for vector operations in default configuration
|
||||||
|
|
||||||
|
### Context Window Management
|
||||||
|
|
||||||
|
When conversation history exceeds model context:
|
||||||
|
|
||||||
|
1. Calculate token count of full history
|
||||||
|
2. If exceeds 80% of model context window, trigger summarization
|
||||||
|
3. Summarize oldest N messages into a condensed context block
|
||||||
|
4. Prepend summary + keep recent messages within context budget
|
||||||
|
5. Store summary as a "context checkpoint" message in DB
|
||||||
|
|
||||||
|
### Model Reference
|
||||||
|
|
||||||
|
| Provider | Model | Tier | Context | Tools | Vision | Embedding |
|
||||||
|
| ---------- | ----------------- | ---------- | ------- | ------ | ------ | -------------- |
|
||||||
|
| Anthropic | Claude Opus 4.6 | premium | 200K | yes | yes | no |
|
||||||
|
| Anthropic | Claude Sonnet 4.6 | standard | 200K | yes | yes | no |
|
||||||
|
| Anthropic | Claude Haiku 4.5 | cheap | 200K | yes | yes | no |
|
||||||
|
| OpenAI | Codex gpt-5.4 | premium | 128K+ | yes | yes | no |
|
||||||
|
| Z.ai | GLM-5 | standard | TBD | TBD | TBD | no |
|
||||||
|
| OpenRouter | varies | varies | varies | varies | varies | no |
|
||||||
|
| Ollama | llama3.2 | local/free | 128K | yes | no | no |
|
||||||
|
| Ollama | nomic-embed-text | — | — | — | — | yes (768-dim) |
|
||||||
|
| Ollama | mxbai-embed-large | — | — | — | — | yes (1024-dim) |
|
||||||
|
|
||||||
|
### Default Routing Rules (Seed Data)
|
||||||
|
|
||||||
|
| Priority | Condition | Route To |
|
||||||
|
| -------- | ------------------------------------------------------------- | ------------- |
|
||||||
|
| 1 | taskType=coding AND complexity=complex | Opus 4.6 |
|
||||||
|
| 2 | taskType=coding AND complexity=moderate | Sonnet 4.6 |
|
||||||
|
| 3 | taskType=coding AND complexity=simple | Codex gpt-5.4 |
|
||||||
|
| 4 | taskType=research | Codex gpt-5.4 |
|
||||||
|
| 5 | taskType=summarization | GLM-5 |
|
||||||
|
| 6 | taskType=analysis AND requiredCapabilities includes reasoning | Opus 4.6 |
|
||||||
|
| 7 | taskType=conversation | Sonnet 4.6 |
|
||||||
|
| 8 | taskType=creative | Sonnet 4.6 |
|
||||||
|
| 9 | costTier=cheap OR domain=general | Haiku 4.5 |
|
||||||
|
| 10 | fallback (no rule matched) | Sonnet 4.6 |
|
||||||
|
| 99 | provider=ollama forced OR offline mode | llama3.2 |
|
||||||
|
|
||||||
|
Rules are user-customizable. Admins set system defaults; users override for their sessions.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Risks and Open Questions
|
||||||
|
|
||||||
|
| Risk | Impact | Mitigation |
|
||||||
|
| ------------------------------------------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| Pi SDK doesn't support custom provider adapters cleanly | High — blocks M3 | Verify in M3-001; fallback: wrap native SDKs and bypass Pi's registry, feeding responses into Pi's session format |
|
||||||
|
| BullMQ + Valkey incompatibility | Medium — blocks M6 | Test in M6-001 before migrating jobs; fallback: use `bullmq` with `ioredis` directly |
|
||||||
|
| Embedding dimension migration (1536 → 768/1024) | Medium — data migration | Run migration script to re-embed existing insights; or start fresh if insight count is low |
|
||||||
|
| Z.ai GLM-5 API undocumented | Low — blocks one provider | Deprioritize; other 4 providers cover all use cases |
|
||||||
|
| Context window summarization quality | Medium — affects UX | Start with simple truncation; add LLM summarization iteratively |
|
||||||
|
| OAuth flow complexity in TUI (no browser redirect) | Medium | URL-display + clipboard + Valkey poll token pattern (already designed in P8-012) |
|
||||||
|
|
||||||
|
### Open Questions
|
||||||
|
|
||||||
|
1. What is the Z.ai GLM-5 API format? OpenAI-compatible or custom SDK? (Research in M3-005)
|
||||||
|
2. Should routing classification use LLM-assisted classification from the start, or rule-based only? (ASSUMPTION: rule-based first, LLM-assisted later)
|
||||||
|
3. What Ollama embedding model provides the best quality/performance tradeoff? (Test nomic-embed-text vs mxbai-embed-large in M3-009)
|
||||||
|
4. Should provider credentials be stored in DB per-user, or remain environment-variable based for system-wide providers? (ASSUMPTION: hybrid — env vars for system defaults, DB for per-user overrides)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Milestone / Delivery Intent
|
||||||
|
|
||||||
|
1. **Target version:** v0.2.0
|
||||||
|
2. **Milestone count:** 7
|
||||||
|
3. **Definition of done:** All 10 acceptance criteria verified with evidence, all quality gates green, PRD status updated to `completed`
|
||||||
|
4. **Delivery order:** M1 (persistence) → M2 (security) → M3 (providers) → M4 (routing) → M5 (sessions) → M6 (jobs) → M7 (channel design)
|
||||||
|
5. **M1 and M2 are prerequisites** — no provider or routing work begins until conversations persist and data is user-scoped
|
||||||
111
docs/SSO-PROVIDERS.md
Normal file
111
docs/SSO-PROVIDERS.md
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
# SSO Providers
|
||||||
|
|
||||||
|
Mosaic Stack supports optional enterprise single sign-on through Better Auth's generic OAuth flow. The gateway mounts Better Auth under `/api/auth`, so every provider callback terminates at:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{BETTER_AUTH_URL}/api/auth/oauth2/callback/{providerId}
|
||||||
|
```
|
||||||
|
|
||||||
|
For the providers in this document:
|
||||||
|
|
||||||
|
- Authentik: `{BETTER_AUTH_URL}/api/auth/oauth2/callback/authentik`
|
||||||
|
- WorkOS: `{BETTER_AUTH_URL}/api/auth/oauth2/callback/workos`
|
||||||
|
- Keycloak: `{BETTER_AUTH_URL}/api/auth/oauth2/callback/keycloak`
|
||||||
|
|
||||||
|
## Required environment variables
|
||||||
|
|
||||||
|
### Authentik
|
||||||
|
|
||||||
|
```bash
|
||||||
|
AUTHENTIK_ISSUER=https://auth.example.com/application/o/mosaic
|
||||||
|
AUTHENTIK_CLIENT_ID=...
|
||||||
|
AUTHENTIK_CLIENT_SECRET=...
|
||||||
|
```
|
||||||
|
|
||||||
|
### WorkOS
|
||||||
|
|
||||||
|
```bash
|
||||||
|
WORKOS_ISSUER=https://your-company.authkit.app
|
||||||
|
WORKOS_CLIENT_ID=client_...
|
||||||
|
WORKOS_CLIENT_SECRET=...
|
||||||
|
NEXT_PUBLIC_WORKOS_ENABLED=true
|
||||||
|
```
|
||||||
|
|
||||||
|
`WORKOS_ISSUER` should be the WorkOS AuthKit issuer or custom auth domain, not the raw REST API hostname. Mosaic derives the OIDC discovery URL from that issuer.
|
||||||
|
|
||||||
|
### Keycloak
|
||||||
|
|
||||||
|
```bash
|
||||||
|
KEYCLOAK_ISSUER=https://auth.example.com/realms/master
|
||||||
|
KEYCLOAK_CLIENT_ID=mosaic
|
||||||
|
KEYCLOAK_CLIENT_SECRET=...
|
||||||
|
NEXT_PUBLIC_KEYCLOAK_ENABLED=true
|
||||||
|
```
|
||||||
|
|
||||||
|
If you prefer, you can keep the issuer split as:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
KEYCLOAK_URL=https://auth.example.com
|
||||||
|
KEYCLOAK_REALM=master
|
||||||
|
```
|
||||||
|
|
||||||
|
The auth package will derive `KEYCLOAK_ISSUER` from those two values.
|
||||||
|
|
||||||
|
## WorkOS setup
|
||||||
|
|
||||||
|
1. In WorkOS, create or select the application that will back Mosaic login.
|
||||||
|
2. Configure an AuthKit domain or custom authentication domain for the application.
|
||||||
|
3. Add the redirect URI:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{BETTER_AUTH_URL}/api/auth/oauth2/callback/workos
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Copy the application's `client_id` and `client_secret` into `WORKOS_CLIENT_ID` and `WORKOS_CLIENT_SECRET`.
|
||||||
|
5. Set `WORKOS_ISSUER` to the AuthKit domain from step 2.
|
||||||
|
6. Create the WorkOS organization and attach the enterprise SSO connection you want Mosaic to use.
|
||||||
|
7. Set `NEXT_PUBLIC_WORKOS_ENABLED=true` in the web deployment so the login button is rendered.
|
||||||
|
|
||||||
|
## Keycloak setup
|
||||||
|
|
||||||
|
1. Start from an existing Keycloak realm or create a dedicated realm for Mosaic.
|
||||||
|
2. Create a confidential OIDC client named `mosaic` or your preferred client ID.
|
||||||
|
3. Set the valid redirect URI to:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{BETTER_AUTH_URL}/api/auth/oauth2/callback/keycloak
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Set the web origin to the public Mosaic web URL.
|
||||||
|
5. Copy the client secret into `KEYCLOAK_CLIENT_SECRET`.
|
||||||
|
6. Set either `KEYCLOAK_ISSUER` directly or `KEYCLOAK_URL` + `KEYCLOAK_REALM`.
|
||||||
|
7. Set `NEXT_PUBLIC_KEYCLOAK_ENABLED=true` in the web deployment so the login button is rendered.
|
||||||
|
|
||||||
|
### Local Keycloak smoke test
|
||||||
|
|
||||||
|
If you want to test locally with Docker:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --rm --name mosaic-keycloak \
|
||||||
|
-p 8080:8080 \
|
||||||
|
-e KEYCLOAK_ADMIN=admin \
|
||||||
|
-e KEYCLOAK_ADMIN_PASSWORD=admin \
|
||||||
|
quay.io/keycloak/keycloak:26.1 start-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Then configure:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
KEYCLOAK_ISSUER=http://localhost:8080/realms/master
|
||||||
|
KEYCLOAK_CLIENT_ID=mosaic
|
||||||
|
KEYCLOAK_CLIENT_SECRET=...
|
||||||
|
NEXT_PUBLIC_KEYCLOAK_ENABLED=true
|
||||||
|
```
|
||||||
|
|
||||||
|
## Web flow
|
||||||
|
|
||||||
|
The web login page renders provider buttons from `NEXT_PUBLIC_*_ENABLED` flags. Each button links to `/auth/provider/{providerId}`, and that page initiates Better Auth's `signIn.oauth2` flow before handing off to the provider.
|
||||||
|
|
||||||
|
## Failure mode
|
||||||
|
|
||||||
|
Provider config is optional, but partial config is rejected at startup. If any provider-specific env var is present without the full required set, `@mosaic/auth` throws a bootstrap error with the missing keys instead of silently registering a broken provider.
|
||||||
164
docs/TASKS.md
164
docs/TASKS.md
@@ -1,96 +1,74 @@
|
|||||||
# Tasks — MVP
|
# Tasks — Harness Foundation
|
||||||
|
|
||||||
> Single-writer: orchestrator only. Workers read but never modify.
|
> Single-writer: orchestrator only. Workers read but never modify.
|
||||||
|
>
|
||||||
|
> **`agent` column values:** `codex` | `sonnet` | `haiku` | `glm-5` | `opus` | `—` (auto/default)
|
||||||
|
> Pipeline crons pick the cheapest capable model. Override with a specific value when a task genuinely needs it.
|
||||||
|
|
||||||
| id | status | milestone | description | pr | notes |
|
| id | status | agent | milestone | description | pr | notes |
|
||||||
| ------ | ----------- | --------- | -------------------------------------------------------------------------------------------------- | ---- | ------------- |
|
| ------ | ----------- | ------ | ------------------ | --------------------------------------------------------------------------------------------------------------- | ---- | ------------------------------------------------ |
|
||||||
| P0-001 | done | Phase 0 | Scaffold monorepo | #60 | #1 |
|
| M1-001 | done | sonnet | M1: Persistence | Wire ChatGateway.handleMessage() → ConversationsRepo.addMessage() for user messages | #292 | #224 closed |
|
||||||
| P0-002 | done | Phase 0 | @mosaic/types — migrate and extend shared types | #65 | #2 |
|
| M1-002 | done | sonnet | M1: Persistence | Wire agent event relay → ConversationsRepo.addMessage() for assistant responses (text, tool calls, thinking) | #292 | #225 closed |
|
||||||
| P0-003 | done | Phase 0 | @mosaic/db — Drizzle schema and PG connection | #67 | #3 |
|
| M1-003 | done | sonnet | M1: Persistence | Store message metadata: model used, provider, token counts, tool call details, timestamps | #292 | #226 closed |
|
||||||
| P0-004 | done | Phase 0 | @mosaic/auth — BetterAuth email/password setup | #68 | #4 |
|
| M1-004 | done | sonnet | M1: Persistence | On session resume, load message history from DB and inject into Pi session context | #301 | #227 closed |
|
||||||
| P0-005 | done | Phase 0 | Docker Compose — PG 17, Valkey 8, SigNoz | #65 | #5 |
|
| M1-005 | done | sonnet | M1: Persistence | Context window management: summarize older messages when history exceeds 80% of model context | #301 | #228 closed |
|
||||||
| P0-006 | done | Phase 0 | OTEL foundation — OpenTelemetry SDK setup | #65 | #6 |
|
| M1-006 | done | sonnet | M1: Persistence | Conversation search: full-text search on messages table via /api/conversations/search | #299 | #229 closed |
|
||||||
| P0-007 | done | Phase 0 | CI pipeline — Woodpecker config | #69 | #7 |
|
| M1-007 | done | sonnet | M1: Persistence | TUI: /history command to display conversation message count and context usage | #297 | #230 closed |
|
||||||
| P0-008 | done | Phase 0 | Project docs — AGENTS.md, CLAUDE.md, README | #69 | #8 |
|
| M1-008 | done | sonnet | M1: Persistence | Verify: send messages → kill TUI → resume with -c → agent references prior context | #304 | #231 closed — 20 integration tests |
|
||||||
| P0-009 | done | Phase 0 | Verify Phase 0 — CI green, all packages build | #70 | #9 |
|
| M2-001 | done | sonnet | M2: Security | Audit InsightsRepo: add userId filter to searchByEmbedding() vector search | #290 | #232 closed |
|
||||||
| P1-001 | done | Phase 1 | apps/gateway scaffold — NestJS + Fastify adapter | #61 | #10 |
|
| M2-002 | done | sonnet | M2: Security | Audit InsightsRepo: add userId filter to findByUser(), decayOldInsights() | #290 | #233 closed |
|
||||||
| P1-002 | done | Phase 1 | Auth middleware — BetterAuth session validation | #71 | #11 |
|
| M2-003 | done | sonnet | M2: Security | Audit PreferencesRepo: verify all queries filter by userId | #294 | #234 closed — already scoped |
|
||||||
| P1-003 | done | Phase 1 | @mosaic/brain — migrate from v0, PG backend | #71 | #12 |
|
| M2-004 | done | sonnet | M2: Security | Audit agent memory tools: verify memory*search, memory_save*_, memory*get*_ scope to session user | #294 | #235 closed — FIXED userId injection |
|
||||||
| P1-004 | done | Phase 1 | @mosaic/queue — migrate from v0 | #71 | #13 |
|
| M2-005 | done | sonnet | M2: Security | Audit ConversationsRepo: verify ownership check on findById, update, delete, addMessage, findMessages | #293 | #236 closed |
|
||||||
| P1-005 | done | Phase 1 | Gateway routes — conversations CRUD + messages | #72 | #14 |
|
| M2-006 | done | sonnet | M2: Security | Audit AgentsRepo: verify findAccessible() returns only user's agents + system agents | #293 | #237 closed |
|
||||||
| P1-006 | done | Phase 1 | Gateway routes — tasks, projects, missions CRUD | #72 | #15 |
|
| M2-007 | done | sonnet | M2: Security | Integration test: create two users, populate data, verify cross-user isolation on every query path | #305 | #238 closed — 28 integration tests |
|
||||||
| P1-007 | done | Phase 1 | WebSocket server — chat streaming | #61 | #16 |
|
| M2-008 | done | sonnet | M2: Security | Audit Valkey keys: verify session keys include userId or are not enumerable across users | #298 | #239 closed — SCAN replaces KEYS, /gc admin-only |
|
||||||
| P1-008 | done | Phase 1 | Basic agent dispatch — single provider | #61 | #17 |
|
| M3-001 | not-started | opus | M3: Providers | Refactor ProviderService into IProviderAdapter pattern: register(), listModels(), healthCheck(), createClient() | — | #240 Verify Pi SDK compat |
|
||||||
| P1-009 | done | Phase 1 | Verify Phase 1 — gateway functional, API tested | #73 | #18 |
|
| M3-002 | not-started | sonnet | M3: Providers | Anthropic adapter: @anthropic-ai/sdk, Claude Sonnet 4.6 + Opus 4.6 + Haiku 4.5, OAuth + API key | — | #241 |
|
||||||
| P2-001 | done | Phase 2 | @mosaic/agent — Pi SDK integration + agent pool | #61 | #19 |
|
| M3-003 | not-started | sonnet | M3: Providers | OpenAI adapter: openai SDK, Codex gpt-5.4, OAuth + API key | — | #242 |
|
||||||
| P2-002 | done | Phase 2 | Multi-provider support — Anthropic + Ollama | #74 | #20 |
|
| M3-004 | not-started | sonnet | M3: Providers | OpenRouter adapter: OpenAI-compatible client, API key, dynamic model list from /api/v1/models | — | #243 |
|
||||||
| P2-003 | done | Phase 2 | Agent routing engine — cost/capability matrix | #75 | #21 |
|
| M3-005 | not-started | sonnet | M3: Providers | Z.ai GLM adapter: GLM-5, API key, research API format | — | #244 |
|
||||||
| P2-004 | done | Phase 2 | Tool registration — brain, queue, memory tools | #76 | #22 |
|
| M3-006 | not-started | sonnet | M3: Providers | Ollama adapter: refactor existing integration into adapter pattern, add embedding model support | — | #245 |
|
||||||
| P2-005 | done | Phase 2 | @mosaic/coord — migrate from v0, gateway integration | #77 | #23 |
|
| M3-007 | not-started | sonnet | M3: Providers | Provider health check: periodic probe, configurable interval, status per provider, /api/providers/health | — | #246 |
|
||||||
| P2-006 | done | Phase 2 | Agent session management — tmux + monitoring | #78 | #24 |
|
| M3-008 | done | sonnet | M3: Providers | Model capability matrix: per-model metadata (tier, context window, tool support, vision, streaming, embedding) | #303 | #247 closed |
|
||||||
| P2-007 | done | Phase 2 | Verify Phase 2 — multi-provider routing works | #79 | #25 |
|
| M3-009 | not-started | sonnet | M3: Providers | Refactor EmbeddingService: provider-agnostic interface, Ollama default (nomic-embed-text or mxbai-embed-large) | — | #248 Dim migration |
|
||||||
| P3-001 | done | Phase 3 | apps/web scaffold — Next.js 16 + BetterAuth + Tailwind | #82 | #26 |
|
| M3-010 | not-started | sonnet | M3: Providers | OAuth token storage: persist provider tokens per user in DB (encrypted), refresh flow | — | #249 |
|
||||||
| P3-002 | done | Phase 3 | Auth pages — login, registration, SSO redirect | #83 | #27 |
|
| M3-011 | not-started | sonnet | M3: Providers | Provider config UI support: /api/providers CRUD for user-scoped provider credentials | — | #250 |
|
||||||
| P3-003 | done | Phase 3 | Chat UI — conversations, messages, streaming | #84 | #28 |
|
| M3-012 | not-started | haiku | M3: Providers | Verify: each provider connects, lists models, completes chat request, handles errors | — | #251 |
|
||||||
| P3-004 | done | Phase 3 | Task management — list view + kanban board | #86 | #29 |
|
| M4-001 | not-started | opus | M4: Routing | Define routing rule schema: RoutingRule { name, priority, conditions[], action } stored in DB | — | #252 DB migration |
|
||||||
| P3-005 | done | Phase 3 | Project & mission views — dashboard + PRD viewer | #87 | #30 |
|
| M4-002 | not-started | opus | M4: Routing | Condition types: taskType, complexity, domain, costTier, requiredCapabilities | — | #253 |
|
||||||
| P3-006 | done | Phase 3 | Settings — provider config, profile, integrations | #88 | #31 |
|
| M4-003 | not-started | opus | M4: Routing | Action types: routeTo { provider, model, agentConfigId?, systemPromptOverride?, toolAllowlist? } | — | #254 |
|
||||||
| P3-007 | done | Phase 3 | Admin panel — user management, RBAC | #89 | #32 |
|
| M4-004 | not-started | sonnet | M4: Routing | Default routing rules seed data: coding→Opus, Q&A→Sonnet, summarization→GLM-5, research→Codex, offline→Ollama | — | #255 |
|
||||||
| P3-008 | done | Phase 3 | Verify Phase 3 — web dashboard functional E2E | — | #33 |
|
| M4-005 | not-started | opus | M4: Routing | Task classification: infer taskType + complexity from user message (regex/keyword first, LLM-assisted later) | — | #256 |
|
||||||
| P4-001 | done | Phase 4 | @mosaic/memory — preference + insight stores | — | #34 |
|
| M4-006 | not-started | opus | M4: Routing | Routing decision pipeline: classify → match rules → check health → fallback chain → return result | — | #257 |
|
||||||
| P4-002 | done | Phase 4 | Semantic search — pgvector embeddings + search API | — | #35 |
|
| M4-007 | not-started | sonnet | M4: Routing | Routing override: /model forces specific model regardless of routing rules | — | #258 |
|
||||||
| P4-003 | done | Phase 4 | @mosaic/log — log ingest, parsing, tiered storage | — | #36 |
|
| M4-008 | not-started | sonnet | M4: Routing | Routing transparency: include routing decision in session:info event (model + reason) | — | #259 |
|
||||||
| P4-004 | done | Phase 4 | Summarization pipeline — Haiku-tier LLM + cron | — | #37 |
|
| M4-009 | not-started | sonnet | M4: Routing | Routing rules CRUD: /api/routing/rules — list, create, update, delete, reorder priority | — | #260 |
|
||||||
| P4-005 | done | Phase 4 | Memory integration — inject into agent sessions | — | #38 |
|
| M4-010 | not-started | sonnet | M4: Routing | Per-user routing overrides: users customize default rules for their sessions | — | #261 |
|
||||||
| P4-006 | done | Phase 4 | Skill management — catalog, install, config | — | #39 |
|
| M4-011 | not-started | sonnet | M4: Routing | Agent specialization: agents declare capabilities in config (domains, preferred models, tool sets) | — | #262 |
|
||||||
| P4-007 | done | Phase 4 | Verify Phase 4 — memory + log pipeline working | — | #40 |
|
| M4-012 | not-started | sonnet | M4: Routing | Routing integration: wire into ChatGateway — every message triggers routing before agent dispatch | — | #263 |
|
||||||
| P5-001 | done | Phase 5 | Plugin host — gateway plugin loading + channel interface | — | #41 |
|
| M4-013 | not-started | haiku | M4: Routing | Verify: coding→Opus, summarize→GLM-5, simple→Haiku, override via /model works | — | #264 |
|
||||||
| P5-002 | done | Phase 5 | @mosaic/discord-plugin — Discord bot + channel plugin | #61 | #42 |
|
| M5-001 | not-started | sonnet | M5: Sessions | Wire ChatGateway: on session create, load agent config from DB (system prompt, model, provider, tools, skills) | — | #265 |
|
||||||
| P5-003 | done | Phase 5 | @mosaic/telegram-plugin — Telegraf bot + channel plugin | — | #43 |
|
| M5-002 | not-started | sonnet | M5: Sessions | /model command: end-to-end wiring — TUI → socket → gateway switches provider/model → new messages use it | — | #266 |
|
||||||
| P5-004 | done | Phase 5 | SSO — Authentik OIDC adapter end-to-end | — | #44 |
|
| M5-003 | not-started | sonnet | M5: Sessions | /agent command: switch agent config mid-session — loads new system prompt, tools, default model | — | #267 |
|
||||||
| P5-005 | done | Phase 5 | Verify Phase 5 — Discord + Telegram + SSO working | #99 | #45 |
|
| M5-004 | not-started | sonnet | M5: Sessions | Session ↔ conversation binding: persist sessionId on conversation record, resume via conversationId | — | #268 |
|
||||||
| P6-001 | done | Phase 6 | @mosaic/cli — unified CLI binary + subcommands | #104 | #46 |
|
| M5-005 | not-started | sonnet | M5: Sessions | Session info broadcast: on model/agent switch, emit session:info with updated state | — | #269 |
|
||||||
| P6-002 | done | Phase 6 | @mosaic/prdy — migrate PRD wizard from v0 | #101 | #47 |
|
| M5-006 | not-started | sonnet | M5: Sessions | Agent creation from TUI: /agent new command creates agent config via gateway API | — | #270 |
|
||||||
| P6-003 | done | Phase 6 | @mosaic/quality-rails — migrate scaffolder from v0 | #100 | #48 |
|
| M5-007 | not-started | sonnet | M5: Sessions | Session metrics: per-session token usage, model switches, duration — persist in DB | — | #271 |
|
||||||
| P6-004 | done | Phase 6 | @mosaic/mosaic — install wizard for v1 | #103 | #49 |
|
| M5-008 | not-started | haiku | M5: Sessions | Verify: /model switches model, /agent switches agent, session resume loads config | — | #272 |
|
||||||
| P6-005 | done | Phase 6 | Pi TUI integration — mosaic tui | #61 | #50 |
|
| M6-001 | not-started | sonnet | M6: Jobs | Add BullMQ dependency, configure with Valkey connection | — | #273 Test compat first |
|
||||||
| P6-006 | done | Phase 6 | Verify Phase 6 — CLI functional, all subcommands | — | #51 |
|
| M6-002 | not-started | sonnet | M6: Jobs | Create queue service: typed job definitions, worker registration, error handling with exponential backoff | — | #274 |
|
||||||
| P7-009 | done | Phase 7 | Web chat — WebSocket integration, streaming, conversation switching | #136 | #120 W1 done |
|
| M6-003 | not-started | sonnet | M6: Jobs | Migrate summarization cron → BullMQ repeatable job | — | #275 |
|
||||||
| P7-001 | done | Phase 7 | MCP endpoint hardening — streamable HTTP transport | #137 | #52 W1 done |
|
| M6-004 | not-started | sonnet | M6: Jobs | Migrate GC (session cleanup) → BullMQ repeatable job | — | #276 |
|
||||||
| P7-010 | done | Phase 7 | Web conversation management — list, search, rename, delete, archive | #139 | #121 W2 done |
|
| M6-005 | not-started | sonnet | M6: Jobs | Migrate tier management (log archival) → BullMQ repeatable job | — | #277 |
|
||||||
| P7-015 | done | Phase 7 | Agent tool expansion — file ops, git, shell exec, web fetch | #138 | #126 W2 done |
|
| M6-006 | not-started | sonnet | M6: Jobs | Admin jobs API: GET /api/admin/jobs — list, status, retry, pause/resume queues | — | #278 |
|
||||||
| P7-011 | done | Phase 7 | Web project detail views — missions, tasks, PRDs, dashboards | #140 | #122 W3 done |
|
| M6-007 | not-started | sonnet | M6: Jobs | Job event logging: emit job start/complete/fail events to agent_logs | — | #279 |
|
||||||
| P7-016 | done | Phase 7 | MCP client — gateway connects to external MCP servers as tools | #141 | #127 W3 done |
|
| M6-008 | not-started | haiku | M6: Jobs | Verify: jobs execute on schedule, failure retries with backoff, admin endpoint shows history | — | #280 |
|
||||||
| P7-012 | done | Phase 7 | Web provider management UI — add, configure, test LLM providers | #142 | #123 W4 done |
|
| M7-001 | not-started | opus | M7: Channel Design | Define IChannelAdapter interface: lifecycle, message flow, identity mapping | — | #281 Architecture |
|
||||||
| P7-017 | done | Phase 7 | Agent skill invocation — load and execute skills from catalog | #143 | #128 W4 done |
|
| M7-002 | not-started | opus | M7: Channel Design | Define channel message protocol: canonical format all adapters translate to/from | — | #282 Architecture |
|
||||||
| P7-013 | done | Phase 7 | Web settings persistence — profile, preferences save to DB | #145 | #124 W5 done |
|
| M7-003 | not-started | opus | M7: Channel Design | Design Matrix integration: appservice, room↔conversation, space↔team, agent ghosts, power levels | — | #283 Architecture |
|
||||||
| P7-018 | done | Phase 7 | CLI model/provider switching — --model, --provider, /model in TUI | #144 | #129 W5 done |
|
| M7-004 | not-started | opus | M7: Channel Design | Design conversation multiplexing: same conversation from TUI+WebUI+Matrix, real-time sync | — | #284 Architecture |
|
||||||
| P7-014 | done | Phase 7 | Web admin panel — user CRUD, role assignment, system health | #150 | #125 W6 done |
|
| M7-005 | not-started | opus | M7: Channel Design | Design remote auth bridging: Matrix/Discord auth → Mosaic identity (token linking, OAuth bridge) | — | #285 Architecture |
|
||||||
| P7-019 | done | Phase 7 | CLI session management — list, resume, destroy sessions | #146 | #130 W6 done |
|
| M7-006 | not-started | opus | M7: Channel Design | Design agent-to-agent communication via Matrix rooms: room per agent pair, human observation | — | #286 Architecture |
|
||||||
| P7-020 | done | Phase 7 | Coord DB migration — project-scoped missions, multi-tenant RBAC | #149 | #131 W7 done |
|
| M7-007 | not-started | opus | M7: Channel Design | Design multi-user isolation in Matrix: space-per-team, room visibility, encryption, admin access | — | #287 Architecture |
|
||||||
| FIX-02 | done | Backlog | TUI agent:end — fix React state updater side-effect | #147 | #133 W8 done |
|
| M7-008 | not-started | haiku | M7: Channel Design | Publish docs/architecture/channel-protocol.md — reviewed and approved | — | #288 |
|
||||||
| FIX-03 | done | Backlog | Agent session — cwd sandbox, system prompt, tool restrictions | #148 | #134 W8 done |
|
|
||||||
| P7-004 | done | Phase 7 | E2E test suite — Playwright critical paths | #152 | #55 W9 done |
|
|
||||||
| P7-006 | done | Phase 7 | Documentation — user guide, admin guide, dev guide | #151 | #57 W9 done |
|
|
||||||
| P7-007 | done | Phase 7 | Bare-metal deployment docs + .env.example | #153 | #58 W9 done |
|
|
||||||
| P7-021 | done | Phase 7 | Verify Phase 7 — feature-complete platform E2E | — | #132 W10 done |
|
|
||||||
| P8-005 | done | Phase 8 | CLI command architecture — DB schema + brain repo + gateway endpoints | #158 | |
|
|
||||||
| P8-006 | done | Phase 8 | CLI command architecture — agent, mission, prdy commands + TUI mods | #158 | |
|
|
||||||
| P8-007 | done | Phase 8 | DB migrations — preferences.mutable + teams + team_members + projects.teamId | #175 | #160 |
|
|
||||||
| P8-008 | done | Phase 8 | @mosaic/types — CommandDef, CommandManifest, new socket events | #174 | #161 |
|
|
||||||
| P8-009 | done | Phase 8 | TUI Phase 1 — slash command parsing, local commands, system message rendering, InputBar wiring | #176 | #162 |
|
|
||||||
| P8-010 | done | Phase 8 | Gateway Phase 2 — CommandRegistryService, CommandExecutorService, socket + REST commands | #178 | #163 |
|
|
||||||
| P8-011 | done | Phase 8 | Gateway Phase 3 — PreferencesService, /preferences REST, /system Valkey override, prompt injection | #180 | #164 |
|
|
||||||
| P8-012 | done | Phase 8 | Gateway Phase 4 — /agent, /provider (URL+clipboard), /mission, /prdy, /tools commands | #181 | #165 |
|
|
||||||
| P8-013 | done | Phase 8 | Gateway Phase 5 — MosaicPlugin lifecycle, ReloadService, hot reload, system:reload TUI | #182 | #166 |
|
|
||||||
| P8-014 | done | Phase 8 | Gateway Phase 6 — SessionGCService (all tiers), /gc command, cron integration | #179 | #167 |
|
|
||||||
| P8-015 | done | Phase 8 | Gateway Phase 7 — WorkspaceService, ProjectBootstrapService, teams project ownership | #183 | #168 |
|
|
||||||
| P8-016 | done | Phase 8 | Security — file/git/shell tool strict path hardening, sandbox escape prevention | #177 | #169 |
|
|
||||||
| P8-017 | done | Phase 8 | TUI Phase 8 — autocomplete sidebar, fuzzy match, arg hints, up-arrow history | #184 | #170 |
|
|
||||||
| P8-018 | done | Phase 8 | Spin-off plan stubs — Gatekeeper, Task Queue Unification, Chroot Sandboxing | — | #171 |
|
|
||||||
| P8-019 | done | Phase 8 | Verify Platform Architecture — integration + E2E verification | #185 | #172 |
|
|
||||||
| P8-001 | not-started | Phase 8 | Additional SSO providers — WorkOS + Keycloak | — | #53 |
|
|
||||||
| P8-002 | not-started | Phase 8 | Additional LLM providers — Codex, Z.ai, LM Studio, llama.cpp | — | #54 |
|
|
||||||
| P8-003 | not-started | Phase 8 | Performance optimization | — | #56 |
|
|
||||||
| P8-004 | not-started | Phase 8 | Beta release gate — v0.1.0 tag | — | #59 |
|
|
||||||
| FIX-01 | done | Backlog | Call piSession.dispose() in AgentService.destroySession | #78 | #62 |
|
|
||||||
|
|||||||
@@ -237,14 +237,23 @@ external clients. Authentication requires a valid BetterAuth session (cookie or
|
|||||||
|
|
||||||
### SSO (Optional)
|
### SSO (Optional)
|
||||||
|
|
||||||
| Variable | Description |
|
| Variable | Description |
|
||||||
| ------------------------- | ------------------------------ |
|
| --------------------------- | ---------------------------------------------------------------------------- |
|
||||||
| `AUTHENTIK_CLIENT_ID` | Authentik OAuth2 client ID |
|
| `AUTHENTIK_CLIENT_ID` | Authentik OAuth2 client ID |
|
||||||
| `AUTHENTIK_CLIENT_SECRET` | Authentik OAuth2 client secret |
|
| `AUTHENTIK_CLIENT_SECRET` | Authentik OAuth2 client secret |
|
||||||
| `AUTHENTIK_ISSUER` | Authentik OIDC issuer URL |
|
| `AUTHENTIK_ISSUER` | Authentik OIDC issuer URL |
|
||||||
|
| `AUTHENTIK_TEAM_SYNC_CLAIM` | Optional claim used to derive team sync data (defaults to `groups`) |
|
||||||
|
| `WORKOS_CLIENT_ID` | WorkOS OAuth client ID |
|
||||||
|
| `WORKOS_CLIENT_SECRET` | WorkOS OAuth client secret |
|
||||||
|
| `WORKOS_ISSUER` | WorkOS OIDC issuer URL |
|
||||||
|
| `WORKOS_TEAM_SYNC_CLAIM` | Optional claim used to derive team sync data (defaults to `organization_id`) |
|
||||||
|
| `KEYCLOAK_CLIENT_ID` | Keycloak OAuth client ID |
|
||||||
|
| `KEYCLOAK_CLIENT_SECRET` | Keycloak OAuth client secret |
|
||||||
|
| `KEYCLOAK_ISSUER` | Keycloak realm issuer URL |
|
||||||
|
| `KEYCLOAK_TEAM_SYNC_CLAIM` | Optional claim used to derive team sync data (defaults to `groups`) |
|
||||||
|
| `KEYCLOAK_SAML_LOGIN_URL` | Optional SAML login URL used when OIDC is unavailable |
|
||||||
|
|
||||||
All three Authentik variables must be set together. If only `AUTHENTIK_CLIENT_ID`
|
Each OIDC provider requires its client ID, client secret, and issuer URL together. If only part of a provider configuration is set, gateway startup logs a warning and that provider is skipped. Keycloak can fall back to SAML when `KEYCLOAK_SAML_LOGIN_URL` is configured.
|
||||||
is set, a warning is logged and SSO is disabled.
|
|
||||||
|
|
||||||
### Agent
|
### Agent
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
# BUG-CLI Scratchpad
|
# BUG-CLI Scratchpad
|
||||||
|
|
||||||
## Objective
|
## Objective
|
||||||
|
|
||||||
Fix 4 CLI/TUI polish bugs in a single PR (issues #192, #193, #194, #199).
|
Fix 4 CLI/TUI polish bugs in a single PR (issues #192, #193, #194, #199).
|
||||||
|
|
||||||
## Issues
|
## Issues
|
||||||
|
|
||||||
- #192: Ctrl+T leaks 't' into input
|
- #192: Ctrl+T leaks 't' into input
|
||||||
- #193: Duplicate React keys in CommandAutocomplete
|
- #193: Duplicate React keys in CommandAutocomplete
|
||||||
- #194: /provider login false clipboard claim
|
- #194: /provider login false clipboard claim
|
||||||
@@ -12,28 +14,33 @@ Fix 4 CLI/TUI polish bugs in a single PR (issues #192, #193, #194, #199).
|
|||||||
## Plan and Fixes
|
## Plan and Fixes
|
||||||
|
|
||||||
### Bug #192 — Ctrl+T character leak
|
### Bug #192 — Ctrl+T character leak
|
||||||
|
|
||||||
- Location: `packages/cli/src/tui/app.tsx`
|
- Location: `packages/cli/src/tui/app.tsx`
|
||||||
- Fix: Added `ctrlJustFired` ref. Set synchronously in Ctrl+T/L/N/K handlers, cleared via microtask.
|
- Fix: Added `ctrlJustFired` ref. Set synchronously in Ctrl+T/L/N/K handlers, cleared via microtask.
|
||||||
In the `onChange` wrapper passed to `InputBar`, if `ctrlJustFired.current` is true, suppress the
|
In the `onChange` wrapper passed to `InputBar`, if `ctrlJustFired.current` is true, suppress the
|
||||||
leaked character and return early.
|
leaked character and return early.
|
||||||
|
|
||||||
### Bug #193 — Duplicate React keys
|
### Bug #193 — Duplicate React keys
|
||||||
|
|
||||||
- Location: `packages/cli/src/tui/components/command-autocomplete.tsx`
|
- Location: `packages/cli/src/tui/components/command-autocomplete.tsx`
|
||||||
- Fix: Changed `key={cmd.name}` to `key={`${cmd.execution}-${cmd.name}`}` for uniqueness.
|
- Fix: Changed `key={cmd.name}` to `key={`${cmd.execution}-${cmd.name}`}` for uniqueness.
|
||||||
- Also: `packages/cli/src/tui/commands/registry.ts` — `getAll()` now deduplicates gateway commands
|
- Also: `packages/cli/src/tui/commands/registry.ts` — `getAll()` now deduplicates gateway commands
|
||||||
that share a name with local commands. Local commands take precedence.
|
that share a name with local commands. Local commands take precedence.
|
||||||
|
|
||||||
### Bug #194 — False clipboard claim
|
### Bug #194 — False clipboard claim
|
||||||
|
|
||||||
- Location: `apps/gateway/src/commands/command-executor.service.ts`
|
- Location: `apps/gateway/src/commands/command-executor.service.ts`
|
||||||
- Fix: Removed the `\n\n(URL copied to clipboard)` suffix from the provider login message.
|
- Fix: Removed the `\n\n(URL copied to clipboard)` suffix from the provider login message.
|
||||||
|
|
||||||
### Bug #199 — Hardcoded version "0.0.0"
|
### Bug #199 — Hardcoded version "0.0.0"
|
||||||
|
|
||||||
- Location: `packages/cli/src/cli.ts` + `packages/cli/src/tui/app.tsx`
|
- Location: `packages/cli/src/cli.ts` + `packages/cli/src/tui/app.tsx`
|
||||||
- Fix: `cli.ts` reads version from `../package.json` via `createRequire`. Passes `version: CLI_VERSION`
|
- Fix: `cli.ts` reads version from `../package.json` via `createRequire`. Passes `version: CLI_VERSION`
|
||||||
to TuiApp in both render calls. TuiApp has new optional `version` prop (defaults to '0.0.0'),
|
to TuiApp in both render calls. TuiApp has new optional `version` prop (defaults to '0.0.0'),
|
||||||
passes it to TopBar instead of hardcoded `"0.0.0"`.
|
passes it to TopBar instead of hardcoded `"0.0.0"`.
|
||||||
|
|
||||||
## Quality Gates
|
## Quality Gates
|
||||||
|
|
||||||
- CLI typecheck: PASSED
|
- CLI typecheck: PASSED
|
||||||
- CLI lint: PASSED
|
- CLI lint: PASSED
|
||||||
- Prettier format:check: PASSED
|
- Prettier format:check: PASSED
|
||||||
|
|||||||
60
docs/scratchpads/harness-20260321.md
Normal file
60
docs/scratchpads/harness-20260321.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# Mission Scratchpad — Harness Foundation
|
||||||
|
|
||||||
|
> Append-only log. NEVER delete entries. NEVER overwrite sections.
|
||||||
|
> This is the orchestrator's working memory across sessions.
|
||||||
|
|
||||||
|
## Original Mission Prompt
|
||||||
|
|
||||||
|
```
|
||||||
|
Jason wants to get the gateway and TUI working as a real daily-driver harness.
|
||||||
|
The system needs: multi-provider LLM access, task-aware agent routing, conversation persistence,
|
||||||
|
security isolation, session hardening, job queue foundation, and channel protocol design for
|
||||||
|
future Matrix/remote integration.
|
||||||
|
|
||||||
|
Provider decisions: Anthropic (Sonnet 4.6, Opus 4.6), OpenAI (Codex gpt-5.4), Z.ai (GLM-5),
|
||||||
|
OpenRouter, Ollama. Embeddings via Ollama local models.
|
||||||
|
|
||||||
|
Pi SDK stays as agent runtime. Build with Matrix integration in mind but foundation first.
|
||||||
|
Agent routing per task with granular specification is required.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Planning Decisions
|
||||||
|
|
||||||
|
### 2026-03-21 — Phase 9 PRD and mission setup
|
||||||
|
|
||||||
|
- PRD created as `docs/PRD-Harness_Foundation.md` with canonical Mosaic template format
|
||||||
|
- 7 milestones, 71 tasks total
|
||||||
|
- Milestone order: M1 (persistence) → M2 (security) → M3 (providers) → M4 (routing) → M5 (sessions) → M6 (jobs) → M7 (channel design)
|
||||||
|
- M1 and M2 are hard prerequisites — no provider or routing work until conversations persist and data is user-scoped
|
||||||
|
- Pi SDK kept as agent runtime; providers plug in via adapter pattern underneath
|
||||||
|
- Embeddings migrated from OpenAI to Ollama local (nomic-embed-text or mxbai-embed-large)
|
||||||
|
- BullMQ chosen for job queue (Valkey-compatible, TypeScript-native)
|
||||||
|
- Channel protocol is design-only in this phase; Matrix implementation deferred to Phase 10
|
||||||
|
- Models confirmed: Claude Sonnet 4.6, Opus 4.6, Haiku 4.5, Codex gpt-5.4, GLM-5, Ollama locals
|
||||||
|
- Routing engine: rule-based classification first, LLM-assisted later
|
||||||
|
- Default routing: coding-complex→Opus, coding-moderate→Sonnet, coding-simple→Codex, research→Codex, summarization→GLM-5, conversation→Sonnet, cheap/general→Haiku, offline→Ollama
|
||||||
|
|
||||||
|
### Architecture decisions
|
||||||
|
|
||||||
|
- Provider adapter pattern: each provider implements IProviderAdapter, registered in Pi SDK's provider registry
|
||||||
|
- Routing flow: classify message → match rules by priority → check provider health → fallback chain → dispatch
|
||||||
|
- Context window management: summarize older messages when history exceeds 80% of model context
|
||||||
|
- OAuth pattern: URL-display + clipboard + Valkey poll token (same as P8-012 design)
|
||||||
|
- Embedding dimension: migration from 1536 (OpenAI) to 768/1024 (Ollama) — may require re-embedding existing insights
|
||||||
|
|
||||||
|
## Session Log
|
||||||
|
|
||||||
|
| Session | Date | Milestone | Tasks Done | Outcome |
|
||||||
|
| ------- | ---------- | --------- | -------------------------------- | ---------------------------------------------- |
|
||||||
|
| 1 | 2026-03-21 | Planning | PRD, manifest, tasks, scratchpad | Mission initialized, planning gate in progress |
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. Z.ai GLM-5 API format — OpenAI-compatible or custom? (Research in M3-005)
|
||||||
|
2. Which Ollama embedding model: nomic-embed-text (768-dim) vs mxbai-embed-large (1024-dim)? (Test in M3-009)
|
||||||
|
3. Provider credentials: env vars for system defaults + DB for per-user overrides? (ASSUMPTION: hybrid)
|
||||||
|
4. Pi SDK provider adapter support — needs verification in M3-001 before committing to adapter pattern
|
||||||
|
|
||||||
|
## Corrections
|
||||||
|
|
||||||
|
<!-- Record any corrections to earlier decisions or assumptions. -->
|
||||||
55
docs/scratchpads/m3-001-provider-adapter.md
Normal file
55
docs/scratchpads/m3-001-provider-adapter.md
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# M3-001 Provider Adapter Pattern — Scratchpad
|
||||||
|
|
||||||
|
## Objective
|
||||||
|
|
||||||
|
Refactor ProviderService into an IProviderAdapter pattern without breaking existing Ollama flow.
|
||||||
|
|
||||||
|
## Plan
|
||||||
|
|
||||||
|
1. Add `IProviderAdapter` interface and supporting types to `@mosaic/types` provider package
|
||||||
|
2. Create `apps/gateway/src/agent/adapters/` directory with:
|
||||||
|
- `provider-adapter.interface.ts` — IProviderAdapter + ProviderHealth + CompletionParams + CompletionEvent
|
||||||
|
- `ollama.adapter.ts` — extract existing Ollama logic
|
||||||
|
3. Refactor ProviderService:
|
||||||
|
- Accept `IProviderAdapter[]` (injected via DI token)
|
||||||
|
- `registerAll()` / `listModels()` aggregates from all adapters
|
||||||
|
- `getAdapter(name)` — lookup by name
|
||||||
|
- `healthCheckAll()` — check all adapters
|
||||||
|
- Keep Pi ModelRegistry wiring (required by AgentService)
|
||||||
|
4. Wire up in AgentModule
|
||||||
|
|
||||||
|
## Key Findings
|
||||||
|
|
||||||
|
### Pi SDK Compatibility
|
||||||
|
|
||||||
|
- Pi SDK uses `ModelRegistry` as central registry; ProviderService wraps it
|
||||||
|
- `ModelRegistry.registerProvider()` is the integration point — adapters call this
|
||||||
|
- Pi doesn't have a native "IProviderAdapter" concept — adapters are a Mosaic abstraction on top
|
||||||
|
- The `createAgentSession()` call in AgentService uses `modelRegistry: this.providerService.getRegistry()`
|
||||||
|
- OllamaAdapter should call `registry.registerProvider('ollama', {...})` same as today
|
||||||
|
- CompletionParams/CompletionEvent: Pi SDK streams via `AgentSession.prompt()`, not raw completion
|
||||||
|
— IProviderAdapter.createCompletion() is for future direct use; for now stub or leave as interface-only
|
||||||
|
— ASSUMPTION: createCompletion is reserved for future M3+ work; Pi SDK owns the actual streaming
|
||||||
|
|
||||||
|
## Implementation Notes
|
||||||
|
|
||||||
|
- ESM: use `.js` extensions in all imports
|
||||||
|
- NestJS: use `@Inject()` explicitly
|
||||||
|
- Keep RoutingService working — it only uses `providerService.listAvailableModels()`
|
||||||
|
- Keep AgentService working — it uses `providerService.getRegistry()`, `findModel()`, `getDefaultModel()`, `listAvailableModels()`
|
||||||
|
|
||||||
|
## Progress
|
||||||
|
|
||||||
|
- [ ] Add types to @mosaic/types
|
||||||
|
- [ ] Create adapters/ directory
|
||||||
|
- [ ] Create IProviderAdapter interface file
|
||||||
|
- [ ] Create OllamaAdapter
|
||||||
|
- [ ] Refactor ProviderService
|
||||||
|
- [ ] Update AgentModule
|
||||||
|
- [ ] Run tests
|
||||||
|
- [ ] Run quality gates
|
||||||
|
|
||||||
|
## Risks
|
||||||
|
|
||||||
|
- Pi SDK doesn't natively support IProviderAdapter — adapters are a layer on top
|
||||||
|
- createCompletion() is architecturally sound but requires Pi session bypass (future work)
|
||||||
65
docs/scratchpads/p8-001-sso-providers.md
Normal file
65
docs/scratchpads/p8-001-sso-providers.md
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
# P8-001 — WorkOS + Keycloak SSO Providers
|
||||||
|
|
||||||
|
**Branch:** feat/p8-001-sso-providers
|
||||||
|
**Started:** 2026-03-18
|
||||||
|
**Mode:** Delivery
|
||||||
|
|
||||||
|
## Objective
|
||||||
|
|
||||||
|
Add WorkOS and Keycloak as optional SSO providers to the BetterAuth configuration, following the existing Authentik pattern.
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
| Surface | Change |
|
||||||
|
| ---------------------------------------- | ----------------------------------------------------------------------- |
|
||||||
|
| `packages/auth/src/auth.ts` | Refactor provider array, add WorkOS + Keycloak conditional registration |
|
||||||
|
| `apps/web/src/lib/auth-client.ts` | Add `genericOAuthClient()` plugin |
|
||||||
|
| `apps/web/src/app/(auth)/login/page.tsx` | WorkOS + Keycloak SSO buttons gated by `NEXT_PUBLIC_*` env vars |
|
||||||
|
| `.env.example` | Document WorkOS + Keycloak env vars |
|
||||||
|
| `packages/auth/src/auth.test.ts` | Unit tests verifying env-var gating |
|
||||||
|
|
||||||
|
## Plan
|
||||||
|
|
||||||
|
1. ✅ Refactor `createAuth` to build `oauthProviders[]` conditionally
|
||||||
|
2. ✅ Add WorkOS provider (explicit URLs, no discovery)
|
||||||
|
3. ✅ Add Keycloak provider (discoveryUrl pattern)
|
||||||
|
4. ✅ Add `genericOAuthClient()` to auth-client.ts
|
||||||
|
5. ✅ Add SSO buttons to login page gated by `NEXT_PUBLIC_WORKOS_ENABLED` / `NEXT_PUBLIC_KEYCLOAK_ENABLED`
|
||||||
|
6. ✅ Update `.env.example`
|
||||||
|
7. ⏳ Write `auth.test.ts` with env-var gating tests
|
||||||
|
8. ⏳ Quality gates: typecheck + lint + format:check + test
|
||||||
|
9. ⏳ Commit + push + PR
|
||||||
|
|
||||||
|
## Decisions
|
||||||
|
|
||||||
|
- **WorkOS**: Uses explicit `authorizationUrl`, `tokenUrl`, `userInfoUrl` (no discovery endpoint available)
|
||||||
|
- **Keycloak**: Uses `discoveryUrl` pattern (`{URL}/realms/{REALM}/.well-known/openid-configuration`)
|
||||||
|
- **UI gating**: Login page uses `NEXT_PUBLIC_WORKOS_ENABLED` / `NEXT_PUBLIC_KEYCLOAK_ENABLED` feature flags (safer than exposing secret env var names client-side)
|
||||||
|
- **Refactor**: Authentik moved into same `oauthProviders[]` array pattern — cleaner, more extensible
|
||||||
|
- **Feature flag design**: `NEXT_PUBLIC_*` flags are opt-in alongside credentials (prevents accidental button render when creds not set)
|
||||||
|
|
||||||
|
## Assumptions
|
||||||
|
|
||||||
|
- `ASSUMPTION:` WorkOS OIDC discovery URL is not publicly documented; using direct URL pattern from WorkOS SSO docs.
|
||||||
|
- `ASSUMPTION:` `NEXT_PUBLIC_WORKOS_ENABLED=true` must be explicitly set — this is intentional (credential presence alone doesn't enable the button since NEXT_PUBLIC vars are baked at build time).
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
- `auth.test.ts`: Mocks betterAuth stack, verifies WorkOS included/excluded based on env var
|
||||||
|
- `auth.test.ts`: Verifies Keycloak discoveryUrl constructed correctly
|
||||||
|
|
||||||
|
## Quality Gate Results
|
||||||
|
|
||||||
|
| Gate | Status |
|
||||||
|
| ------------------- | -------------------------------------------- |
|
||||||
|
| typecheck | ✅ 32/32 cached green |
|
||||||
|
| lint | ✅ 18/18 cached green |
|
||||||
|
| format:check | ✅ All matched files use Prettier code style |
|
||||||
|
| test (@mosaic/auth) | ✅ 8/8 tests passed |
|
||||||
|
|
||||||
|
## Verification Evidence
|
||||||
|
|
||||||
|
- `pnpm typecheck` — FULL TURBO, 32 tasks successful
|
||||||
|
- `pnpm lint` — FULL TURBO, 18 tasks successful
|
||||||
|
- `pnpm format:check` — All matched files use Prettier code style!
|
||||||
|
- `pnpm --filter=@mosaic/auth test` — 8 tests passed, 0 failed
|
||||||
161
packages/auth/src/auth.test.ts
Normal file
161
packages/auth/src/auth.test.ts
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
|
import { buildOAuthProviders } from './auth.js';
|
||||||
|
|
||||||
|
describe('buildOAuthProviders', () => {
|
||||||
|
const originalEnv = process.env;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env = { ...originalEnv };
|
||||||
|
delete process.env['AUTHENTIK_CLIENT_ID'];
|
||||||
|
delete process.env['AUTHENTIK_CLIENT_SECRET'];
|
||||||
|
delete process.env['AUTHENTIK_ISSUER'];
|
||||||
|
delete process.env['WORKOS_CLIENT_ID'];
|
||||||
|
delete process.env['WORKOS_CLIENT_SECRET'];
|
||||||
|
delete process.env['WORKOS_ISSUER'];
|
||||||
|
delete process.env['KEYCLOAK_CLIENT_ID'];
|
||||||
|
delete process.env['KEYCLOAK_CLIENT_SECRET'];
|
||||||
|
delete process.env['KEYCLOAK_ISSUER'];
|
||||||
|
delete process.env['KEYCLOAK_URL'];
|
||||||
|
delete process.env['KEYCLOAK_REALM'];
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
process.env = originalEnv;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array when no SSO env vars are set', () => {
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
expect(providers).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('WorkOS', () => {
|
||||||
|
it('includes workos provider when all required env vars are set', () => {
|
||||||
|
process.env['WORKOS_CLIENT_ID'] = 'client_test123';
|
||||||
|
process.env['WORKOS_CLIENT_SECRET'] = 'sk_live_test';
|
||||||
|
process.env['WORKOS_ISSUER'] = 'https://example.authkit.app/';
|
||||||
|
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const workos = providers.find((p) => p.providerId === 'workos');
|
||||||
|
|
||||||
|
expect(workos).toBeDefined();
|
||||||
|
expect(workos?.clientId).toBe('client_test123');
|
||||||
|
expect(workos?.issuer).toBe('https://example.authkit.app');
|
||||||
|
expect(workos?.discoveryUrl).toBe(
|
||||||
|
'https://example.authkit.app/.well-known/openid-configuration',
|
||||||
|
);
|
||||||
|
expect(workos?.scopes).toEqual(['openid', 'email', 'profile']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws when WorkOS is partially configured', () => {
|
||||||
|
process.env['WORKOS_CLIENT_ID'] = 'client_test123';
|
||||||
|
|
||||||
|
expect(() => buildOAuthProviders()).toThrow(
|
||||||
|
'@mosaic/auth: WorkOS SSO requires WORKOS_ISSUER, WORKOS_CLIENT_ID, WORKOS_CLIENT_SECRET.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludes workos provider when WorkOS is not configured', () => {
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const workos = providers.find((p) => p.providerId === 'workos');
|
||||||
|
expect(workos).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Keycloak', () => {
|
||||||
|
it('includes keycloak provider when KEYCLOAK_ISSUER is set', () => {
|
||||||
|
process.env['KEYCLOAK_CLIENT_ID'] = 'mosaic';
|
||||||
|
process.env['KEYCLOAK_CLIENT_SECRET'] = 'secret123';
|
||||||
|
process.env['KEYCLOAK_ISSUER'] = 'https://auth.example.com/realms/myrealm/';
|
||||||
|
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const keycloakProvider = providers.find((p) => p.providerId === 'keycloak');
|
||||||
|
|
||||||
|
expect(keycloakProvider).toBeDefined();
|
||||||
|
expect(keycloakProvider?.clientId).toBe('mosaic');
|
||||||
|
expect(keycloakProvider?.discoveryUrl).toBe(
|
||||||
|
'https://auth.example.com/realms/myrealm/.well-known/openid-configuration',
|
||||||
|
);
|
||||||
|
expect(keycloakProvider?.scopes).toEqual(['openid', 'email', 'profile']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('supports deriving the Keycloak issuer from KEYCLOAK_URL and KEYCLOAK_REALM', () => {
|
||||||
|
process.env['KEYCLOAK_CLIENT_ID'] = 'mosaic';
|
||||||
|
process.env['KEYCLOAK_CLIENT_SECRET'] = 'secret123';
|
||||||
|
process.env['KEYCLOAK_URL'] = 'https://auth.example.com/';
|
||||||
|
process.env['KEYCLOAK_REALM'] = 'myrealm';
|
||||||
|
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const keycloakProvider = providers.find((p) => p.providerId === 'keycloak');
|
||||||
|
|
||||||
|
expect(keycloakProvider?.discoveryUrl).toBe(
|
||||||
|
'https://auth.example.com/realms/myrealm/.well-known/openid-configuration',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws when Keycloak is partially configured', () => {
|
||||||
|
process.env['KEYCLOAK_CLIENT_ID'] = 'mosaic';
|
||||||
|
process.env['KEYCLOAK_CLIENT_SECRET'] = 'secret123';
|
||||||
|
|
||||||
|
expect(() => buildOAuthProviders()).toThrow(
|
||||||
|
'@mosaic/auth: Keycloak SSO requires KEYCLOAK_CLIENT_ID, KEYCLOAK_CLIENT_SECRET, KEYCLOAK_ISSUER.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludes keycloak provider when Keycloak is not configured', () => {
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const keycloakProvider = providers.find((p) => p.providerId === 'keycloak');
|
||||||
|
expect(keycloakProvider).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Authentik', () => {
|
||||||
|
it('includes authentik provider when all required env vars are set', () => {
|
||||||
|
process.env['AUTHENTIK_CLIENT_ID'] = 'authentik-client';
|
||||||
|
process.env['AUTHENTIK_CLIENT_SECRET'] = 'authentik-secret';
|
||||||
|
process.env['AUTHENTIK_ISSUER'] = 'https://auth.example.com/application/o/mosaic/';
|
||||||
|
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const authentik = providers.find((p) => p.providerId === 'authentik');
|
||||||
|
|
||||||
|
expect(authentik).toBeDefined();
|
||||||
|
expect(authentik?.clientId).toBe('authentik-client');
|
||||||
|
expect(authentik?.issuer).toBe('https://auth.example.com/application/o/mosaic');
|
||||||
|
expect(authentik?.discoveryUrl).toBe(
|
||||||
|
'https://auth.example.com/application/o/mosaic/.well-known/openid-configuration',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws when Authentik is partially configured', () => {
|
||||||
|
process.env['AUTHENTIK_CLIENT_ID'] = 'authentik-client';
|
||||||
|
|
||||||
|
expect(() => buildOAuthProviders()).toThrow(
|
||||||
|
'@mosaic/auth: Authentik SSO requires AUTHENTIK_ISSUER, AUTHENTIK_CLIENT_ID, AUTHENTIK_CLIENT_SECRET.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('excludes authentik provider when Authentik is not configured', () => {
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
const authentik = providers.find((p) => p.providerId === 'authentik');
|
||||||
|
expect(authentik).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('registers all three providers when all env vars are set', () => {
|
||||||
|
process.env['AUTHENTIK_CLIENT_ID'] = 'a-id';
|
||||||
|
process.env['AUTHENTIK_CLIENT_SECRET'] = 'a-secret';
|
||||||
|
process.env['AUTHENTIK_ISSUER'] = 'https://auth.example.com/application/o/mosaic';
|
||||||
|
process.env['WORKOS_CLIENT_ID'] = 'w-id';
|
||||||
|
process.env['WORKOS_CLIENT_SECRET'] = 'w-secret';
|
||||||
|
process.env['WORKOS_ISSUER'] = 'https://example.authkit.app';
|
||||||
|
process.env['KEYCLOAK_CLIENT_ID'] = 'k-id';
|
||||||
|
process.env['KEYCLOAK_CLIENT_SECRET'] = 'k-secret';
|
||||||
|
process.env['KEYCLOAK_ISSUER'] = 'https://kc.example.com/realms/test';
|
||||||
|
|
||||||
|
const providers = buildOAuthProviders();
|
||||||
|
expect(providers).toHaveLength(3);
|
||||||
|
const ids = providers.map((p) => p.providerId);
|
||||||
|
expect(ids).toContain('authentik');
|
||||||
|
expect(ids).toContain('workos');
|
||||||
|
expect(ids).toContain('keycloak');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import { betterAuth } from 'better-auth';
|
import { betterAuth } from 'better-auth';
|
||||||
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
|
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
|
||||||
import { admin, genericOAuth } from 'better-auth/plugins';
|
import { admin } from 'better-auth/plugins';
|
||||||
|
import { genericOAuth, type GenericOAuthConfig } from 'better-auth/plugins/generic-oauth';
|
||||||
import type { Db } from '@mosaic/db';
|
import type { Db } from '@mosaic/db';
|
||||||
|
import { buildGenericOidcProviderConfigs } from './sso.js';
|
||||||
|
|
||||||
export interface AuthConfig {
|
export interface AuthConfig {
|
||||||
db: Db;
|
db: Db;
|
||||||
@@ -9,35 +11,21 @@ export interface AuthConfig {
|
|||||||
secret?: string;
|
secret?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildOAuthProviders(): GenericOAuthConfig[] {
|
||||||
|
return buildGenericOidcProviderConfigs() as GenericOAuthConfig[];
|
||||||
|
}
|
||||||
|
|
||||||
export function createAuth(config: AuthConfig) {
|
export function createAuth(config: AuthConfig) {
|
||||||
const { db, baseURL, secret } = config;
|
const { db, baseURL, secret } = config;
|
||||||
const authentikIssuer = process.env['AUTHENTIK_ISSUER'];
|
const oidcConfigs = buildOAuthProviders();
|
||||||
const authentikClientId = process.env['AUTHENTIK_CLIENT_ID'];
|
const plugins =
|
||||||
const authentikClientSecret = process.env['AUTHENTIK_CLIENT_SECRET'];
|
oidcConfigs.length > 0
|
||||||
const plugins = authentikClientId
|
? [
|
||||||
? [
|
genericOAuth({
|
||||||
genericOAuth({
|
config: oidcConfigs,
|
||||||
config: [
|
}),
|
||||||
{
|
]
|
||||||
providerId: 'authentik',
|
: undefined;
|
||||||
clientId: authentikClientId,
|
|
||||||
clientSecret: authentikClientSecret ?? '',
|
|
||||||
discoveryUrl: authentikIssuer
|
|
||||||
? `${authentikIssuer}/.well-known/openid-configuration`
|
|
||||||
: undefined,
|
|
||||||
authorizationUrl: authentikIssuer
|
|
||||||
? `${authentikIssuer}/application/o/authorize/`
|
|
||||||
: undefined,
|
|
||||||
tokenUrl: authentikIssuer ? `${authentikIssuer}/application/o/token/` : undefined,
|
|
||||||
userInfoUrl: authentikIssuer
|
|
||||||
? `${authentikIssuer}/application/o/userinfo/`
|
|
||||||
: undefined,
|
|
||||||
scopes: ['openid', 'email', 'profile'],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const corsOrigin = process.env['GATEWAY_CORS_ORIGIN'] ?? 'http://localhost:3000';
|
const corsOrigin = process.env['GATEWAY_CORS_ORIGIN'] ?? 'http://localhost:3000';
|
||||||
const trustedOrigins = corsOrigin.split(',').map((o) => o.trim());
|
const trustedOrigins = corsOrigin.split(',').map((o) => o.trim());
|
||||||
|
|||||||
@@ -1 +1,12 @@
|
|||||||
export { createAuth, type Auth, type AuthConfig } from './auth.js';
|
export { createAuth, type Auth, type AuthConfig } from './auth.js';
|
||||||
|
export {
|
||||||
|
buildGenericOidcProviderConfigs,
|
||||||
|
buildSsoDiscovery,
|
||||||
|
listSsoStartupWarnings,
|
||||||
|
type GenericOidcProviderConfig,
|
||||||
|
type SsoLoginMode,
|
||||||
|
type SsoProtocol,
|
||||||
|
type SsoProviderDiscovery,
|
||||||
|
type SsoTeamSyncConfig,
|
||||||
|
type SupportedSsoProviderId,
|
||||||
|
} from './sso.js';
|
||||||
|
|||||||
62
packages/auth/src/sso.spec.ts
Normal file
62
packages/auth/src/sso.spec.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import {
|
||||||
|
buildGenericOidcProviderConfigs,
|
||||||
|
buildSsoDiscovery,
|
||||||
|
listSsoStartupWarnings,
|
||||||
|
} from './sso.js';
|
||||||
|
|
||||||
|
describe('SSO provider config helpers', () => {
|
||||||
|
it('builds OIDC configs for Authentik, WorkOS, and Keycloak when fully configured', () => {
|
||||||
|
const configs = buildGenericOidcProviderConfigs({
|
||||||
|
AUTHENTIK_CLIENT_ID: 'authentik-client',
|
||||||
|
AUTHENTIK_CLIENT_SECRET: 'authentik-secret',
|
||||||
|
AUTHENTIK_ISSUER: 'https://authentik.example.com',
|
||||||
|
WORKOS_CLIENT_ID: 'workos-client',
|
||||||
|
WORKOS_CLIENT_SECRET: 'workos-secret',
|
||||||
|
WORKOS_ISSUER: 'https://auth.workos.com/sso/client_123',
|
||||||
|
KEYCLOAK_CLIENT_ID: 'keycloak-client',
|
||||||
|
KEYCLOAK_CLIENT_SECRET: 'keycloak-secret',
|
||||||
|
KEYCLOAK_ISSUER: 'https://sso.example.com/realms/mosaic',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(configs.map((config) => config.providerId)).toEqual(['authentik', 'workos', 'keycloak']);
|
||||||
|
expect(configs.find((config) => config.providerId === 'workos')).toMatchObject({
|
||||||
|
discoveryUrl: 'https://auth.workos.com/sso/client_123/.well-known/openid-configuration',
|
||||||
|
pkce: true,
|
||||||
|
requireIssuerValidation: true,
|
||||||
|
});
|
||||||
|
expect(configs.find((config) => config.providerId === 'keycloak')).toMatchObject({
|
||||||
|
discoveryUrl: 'https://sso.example.com/realms/mosaic/.well-known/openid-configuration',
|
||||||
|
pkce: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exposes Keycloak SAML fallback when OIDC is not configured', () => {
|
||||||
|
const providers = buildSsoDiscovery({
|
||||||
|
KEYCLOAK_SAML_LOGIN_URL: 'https://sso.example.com/realms/mosaic/protocol/saml',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(providers.find((provider) => provider.id === 'keycloak')).toMatchObject({
|
||||||
|
configured: true,
|
||||||
|
loginMode: 'saml',
|
||||||
|
samlFallback: {
|
||||||
|
configured: true,
|
||||||
|
loginUrl: 'https://sso.example.com/realms/mosaic/protocol/saml',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reports partial provider configuration as startup warnings', () => {
|
||||||
|
const warnings = listSsoStartupWarnings({
|
||||||
|
WORKOS_CLIENT_ID: 'workos-client',
|
||||||
|
KEYCLOAK_CLIENT_ID: 'keycloak-client',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(warnings).toContain(
|
||||||
|
'workos OIDC is partially configured. Missing: WORKOS_CLIENT_SECRET, WORKOS_ISSUER',
|
||||||
|
);
|
||||||
|
expect(warnings).toContain(
|
||||||
|
'keycloak OIDC is partially configured. Missing: KEYCLOAK_CLIENT_SECRET, KEYCLOAK_ISSUER',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
241
packages/auth/src/sso.ts
Normal file
241
packages/auth/src/sso.ts
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
export type SupportedSsoProviderId = 'authentik' | 'workos' | 'keycloak';
|
||||||
|
export type SsoProtocol = 'oidc' | 'saml';
|
||||||
|
export type SsoLoginMode = 'oidc' | 'saml' | null;
|
||||||
|
|
||||||
|
type EnvMap = Record<string, string | undefined>;
|
||||||
|
|
||||||
|
export interface GenericOidcProviderConfig {
|
||||||
|
providerId: SupportedSsoProviderId;
|
||||||
|
clientId: string;
|
||||||
|
clientSecret: string;
|
||||||
|
discoveryUrl?: string;
|
||||||
|
issuer?: string;
|
||||||
|
authorizationUrl?: string;
|
||||||
|
tokenUrl?: string;
|
||||||
|
userInfoUrl?: string;
|
||||||
|
scopes: string[];
|
||||||
|
pkce?: boolean;
|
||||||
|
requireIssuerValidation?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SsoTeamSyncConfig {
|
||||||
|
enabled: boolean;
|
||||||
|
claim: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SsoProviderDiscovery {
|
||||||
|
id: SupportedSsoProviderId;
|
||||||
|
name: string;
|
||||||
|
protocols: SsoProtocol[];
|
||||||
|
configured: boolean;
|
||||||
|
loginMode: SsoLoginMode;
|
||||||
|
callbackPath: string | null;
|
||||||
|
teamSync: SsoTeamSyncConfig;
|
||||||
|
samlFallback: {
|
||||||
|
configured: boolean;
|
||||||
|
loginUrl: string | null;
|
||||||
|
};
|
||||||
|
warnings: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_SCOPES = ['openid', 'email', 'profile'];
|
||||||
|
|
||||||
|
function readEnv(env: EnvMap, key: string): string | undefined {
|
||||||
|
const value = env[key]?.trim();
|
||||||
|
return value ? value : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toDiscoveryUrl(issuer: string): string {
|
||||||
|
return `${issuer.replace(/\/$/, '')}/.well-known/openid-configuration`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTeamSyncClaim(env: EnvMap, envKey: string, fallbackClaim?: string): SsoTeamSyncConfig {
|
||||||
|
const claim = readEnv(env, envKey) ?? fallbackClaim ?? null;
|
||||||
|
return {
|
||||||
|
enabled: claim !== null,
|
||||||
|
claim,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildAuthentikConfig(env: EnvMap): GenericOidcProviderConfig | null {
|
||||||
|
const issuer = readEnv(env, 'AUTHENTIK_ISSUER');
|
||||||
|
const clientId = readEnv(env, 'AUTHENTIK_CLIENT_ID');
|
||||||
|
const clientSecret = readEnv(env, 'AUTHENTIK_CLIENT_SECRET');
|
||||||
|
|
||||||
|
const fields = [issuer, clientId, clientSecret];
|
||||||
|
const presentCount = fields.filter(Boolean).length;
|
||||||
|
if (presentCount > 0 && presentCount < fields.length) {
|
||||||
|
throw new Error(
|
||||||
|
'@mosaic/auth: Authentik SSO requires AUTHENTIK_ISSUER, AUTHENTIK_CLIENT_ID, AUTHENTIK_CLIENT_SECRET.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!issuer || !clientId || !clientSecret) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseIssuer = issuer.replace(/\/$/, '');
|
||||||
|
|
||||||
|
return {
|
||||||
|
providerId: 'authentik',
|
||||||
|
issuer: baseIssuer,
|
||||||
|
clientId,
|
||||||
|
clientSecret,
|
||||||
|
discoveryUrl: toDiscoveryUrl(baseIssuer),
|
||||||
|
authorizationUrl: `${baseIssuer}/application/o/authorize/`,
|
||||||
|
tokenUrl: `${baseIssuer}/application/o/token/`,
|
||||||
|
userInfoUrl: `${baseIssuer}/application/o/userinfo/`,
|
||||||
|
scopes: DEFAULT_SCOPES,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildWorkosConfig(env: EnvMap): GenericOidcProviderConfig | null {
|
||||||
|
const issuer = readEnv(env, 'WORKOS_ISSUER');
|
||||||
|
const clientId = readEnv(env, 'WORKOS_CLIENT_ID');
|
||||||
|
const clientSecret = readEnv(env, 'WORKOS_CLIENT_SECRET');
|
||||||
|
|
||||||
|
const fields = [issuer, clientId, clientSecret];
|
||||||
|
const presentCount = fields.filter(Boolean).length;
|
||||||
|
if (presentCount > 0 && presentCount < fields.length) {
|
||||||
|
throw new Error(
|
||||||
|
'@mosaic/auth: WorkOS SSO requires WORKOS_ISSUER, WORKOS_CLIENT_ID, WORKOS_CLIENT_SECRET.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!issuer || !clientId || !clientSecret) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedIssuer = issuer.replace(/\/$/, '');
|
||||||
|
|
||||||
|
return {
|
||||||
|
providerId: 'workos',
|
||||||
|
issuer: normalizedIssuer,
|
||||||
|
clientId,
|
||||||
|
clientSecret,
|
||||||
|
discoveryUrl: toDiscoveryUrl(normalizedIssuer),
|
||||||
|
scopes: DEFAULT_SCOPES,
|
||||||
|
pkce: true,
|
||||||
|
requireIssuerValidation: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildKeycloakConfig(env: EnvMap): GenericOidcProviderConfig | null {
|
||||||
|
const explicitIssuer = readEnv(env, 'KEYCLOAK_ISSUER');
|
||||||
|
const keycloakUrl = readEnv(env, 'KEYCLOAK_URL');
|
||||||
|
const keycloakRealm = readEnv(env, 'KEYCLOAK_REALM');
|
||||||
|
const clientId = readEnv(env, 'KEYCLOAK_CLIENT_ID');
|
||||||
|
const clientSecret = readEnv(env, 'KEYCLOAK_CLIENT_SECRET');
|
||||||
|
|
||||||
|
// Derive issuer from KEYCLOAK_URL + KEYCLOAK_REALM if KEYCLOAK_ISSUER not set
|
||||||
|
const issuer =
|
||||||
|
explicitIssuer ??
|
||||||
|
(keycloakUrl && keycloakRealm
|
||||||
|
? `${keycloakUrl.replace(/\/$/, '')}/realms/${keycloakRealm}`
|
||||||
|
: undefined);
|
||||||
|
|
||||||
|
const anySet = !!(issuer || clientId || clientSecret);
|
||||||
|
if (anySet && (!issuer || !clientId || !clientSecret)) {
|
||||||
|
throw new Error(
|
||||||
|
'@mosaic/auth: Keycloak SSO requires KEYCLOAK_CLIENT_ID, KEYCLOAK_CLIENT_SECRET, KEYCLOAK_ISSUER.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!issuer || !clientId || !clientSecret) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedIssuer = issuer.replace(/\/$/, '');
|
||||||
|
|
||||||
|
return {
|
||||||
|
providerId: 'keycloak',
|
||||||
|
issuer: normalizedIssuer,
|
||||||
|
clientId,
|
||||||
|
clientSecret,
|
||||||
|
discoveryUrl: toDiscoveryUrl(normalizedIssuer),
|
||||||
|
scopes: DEFAULT_SCOPES,
|
||||||
|
pkce: true,
|
||||||
|
requireIssuerValidation: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectWarnings(env: EnvMap, provider: SupportedSsoProviderId): string[] {
|
||||||
|
const prefix = provider.toUpperCase();
|
||||||
|
const oidcFields = [
|
||||||
|
`${prefix}_CLIENT_ID`,
|
||||||
|
`${prefix}_CLIENT_SECRET`,
|
||||||
|
`${prefix}_ISSUER`,
|
||||||
|
] as const;
|
||||||
|
const presentOidcFields = oidcFields.filter((field) => readEnv(env, field));
|
||||||
|
const warnings: string[] = [];
|
||||||
|
|
||||||
|
if (presentOidcFields.length > 0 && presentOidcFields.length < oidcFields.length) {
|
||||||
|
const missing = oidcFields.filter((field) => !readEnv(env, field));
|
||||||
|
warnings.push(`${provider} OIDC is partially configured. Missing: ${missing.join(', ')}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return warnings;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildGenericOidcProviderConfigs(
|
||||||
|
env: EnvMap = process.env,
|
||||||
|
): GenericOidcProviderConfig[] {
|
||||||
|
return [buildAuthentikConfig(env), buildWorkosConfig(env), buildKeycloakConfig(env)].filter(
|
||||||
|
(config): config is GenericOidcProviderConfig => config !== null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function listSsoStartupWarnings(env: EnvMap = process.env): string[] {
|
||||||
|
return ['authentik', 'workos', 'keycloak'].flatMap((provider) =>
|
||||||
|
collectWarnings(env, provider as SupportedSsoProviderId),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildSsoDiscovery(env: EnvMap = process.env): SsoProviderDiscovery[] {
|
||||||
|
const oidcConfigs = new Map(
|
||||||
|
buildGenericOidcProviderConfigs(env).map((config) => [config.providerId, config]),
|
||||||
|
);
|
||||||
|
const keycloakSamlLoginUrl = readEnv(env, 'KEYCLOAK_SAML_LOGIN_URL') ?? null;
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
id: 'authentik',
|
||||||
|
name: 'Authentik',
|
||||||
|
protocols: ['oidc'],
|
||||||
|
configured: oidcConfigs.has('authentik'),
|
||||||
|
loginMode: oidcConfigs.has('authentik') ? 'oidc' : null,
|
||||||
|
callbackPath: oidcConfigs.has('authentik') ? '/api/auth/oauth2/callback/authentik' : null,
|
||||||
|
teamSync: getTeamSyncClaim(env, 'AUTHENTIK_TEAM_SYNC_CLAIM', 'groups'),
|
||||||
|
samlFallback: {
|
||||||
|
configured: false,
|
||||||
|
loginUrl: null,
|
||||||
|
},
|
||||||
|
warnings: collectWarnings(env, 'authentik'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'workos',
|
||||||
|
name: 'WorkOS',
|
||||||
|
protocols: ['oidc'],
|
||||||
|
configured: oidcConfigs.has('workos'),
|
||||||
|
loginMode: oidcConfigs.has('workos') ? 'oidc' : null,
|
||||||
|
callbackPath: oidcConfigs.has('workos') ? '/api/auth/oauth2/callback/workos' : null,
|
||||||
|
teamSync: getTeamSyncClaim(env, 'WORKOS_TEAM_SYNC_CLAIM', 'organization_id'),
|
||||||
|
samlFallback: {
|
||||||
|
configured: false,
|
||||||
|
loginUrl: null,
|
||||||
|
},
|
||||||
|
warnings: collectWarnings(env, 'workos'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'keycloak',
|
||||||
|
name: 'Keycloak',
|
||||||
|
protocols: ['oidc', 'saml'],
|
||||||
|
configured: oidcConfigs.has('keycloak') || keycloakSamlLoginUrl !== null,
|
||||||
|
loginMode: oidcConfigs.has('keycloak') ? 'oidc' : keycloakSamlLoginUrl ? 'saml' : null,
|
||||||
|
callbackPath: oidcConfigs.has('keycloak') ? '/api/auth/oauth2/callback/keycloak' : null,
|
||||||
|
teamSync: getTeamSyncClaim(env, 'KEYCLOAK_TEAM_SYNC_CLAIM', 'groups'),
|
||||||
|
samlFallback: {
|
||||||
|
configured: keycloakSamlLoginUrl !== null,
|
||||||
|
loginUrl: keycloakSamlLoginUrl,
|
||||||
|
},
|
||||||
|
warnings: collectWarnings(env, 'keycloak'),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { eq, or, type Db, agents } from '@mosaic/db';
|
import { eq, and, or, type Db, agents } from '@mosaic/db';
|
||||||
|
|
||||||
export type Agent = typeof agents.$inferSelect;
|
export type Agent = typeof agents.$inferSelect;
|
||||||
export type NewAgent = typeof agents.$inferInsert;
|
export type NewAgent = typeof agents.$inferInsert;
|
||||||
@@ -27,6 +27,10 @@ export function createAgentsRepo(db: Db) {
|
|||||||
return db.select().from(agents).where(eq(agents.isSystem, true));
|
return db.select().from(agents).where(eq(agents.isSystem, true));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return only agents the user may access: their own agents plus all system agents.
|
||||||
|
* Never returns other users' private agents.
|
||||||
|
*/
|
||||||
async findAccessible(ownerId: string): Promise<Agent[]> {
|
async findAccessible(ownerId: string): Promise<Agent[]> {
|
||||||
return db
|
return db
|
||||||
.select()
|
.select()
|
||||||
@@ -39,17 +43,44 @@ export function createAgentsRepo(db: Db) {
|
|||||||
return rows[0]!;
|
return rows[0]!;
|
||||||
},
|
},
|
||||||
|
|
||||||
async update(id: string, data: Partial<NewAgent>): Promise<Agent | undefined> {
|
/**
|
||||||
|
* Update an agent.
|
||||||
|
*
|
||||||
|
* For user-owned agents pass `ownerId` — the WHERE clause will enforce ownership so that
|
||||||
|
* one user cannot overwrite another user's agent. For system agents the caller must
|
||||||
|
* omit `ownerId` (admin-only path) and the WHERE clause only matches on `id`.
|
||||||
|
*
|
||||||
|
* Returns undefined when no row was matched (not found or ownership mismatch).
|
||||||
|
*/
|
||||||
|
async update(
|
||||||
|
id: string,
|
||||||
|
data: Partial<NewAgent>,
|
||||||
|
ownerId?: string,
|
||||||
|
): Promise<Agent | undefined> {
|
||||||
|
const condition =
|
||||||
|
ownerId !== undefined
|
||||||
|
? and(eq(agents.id, id), eq(agents.ownerId, ownerId))
|
||||||
|
: eq(agents.id, id);
|
||||||
|
|
||||||
const rows = await db
|
const rows = await db
|
||||||
.update(agents)
|
.update(agents)
|
||||||
.set({ ...data, updatedAt: new Date() })
|
.set({ ...data, updatedAt: new Date() })
|
||||||
.where(eq(agents.id, id))
|
.where(condition)
|
||||||
.returning();
|
.returning();
|
||||||
return rows[0];
|
return rows[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
async remove(id: string): Promise<boolean> {
|
/**
|
||||||
const rows = await db.delete(agents).where(eq(agents.id, id)).returning();
|
* Delete a user-owned agent, scoped to the given owner.
|
||||||
|
* Will not match system agents even if the id is correct, because system agents have
|
||||||
|
* `ownerId = null` which cannot equal a real user id.
|
||||||
|
* Returns false when no row was matched (not found, wrong owner, or system agent).
|
||||||
|
*/
|
||||||
|
async remove(id: string, ownerId: string): Promise<boolean> {
|
||||||
|
const rows = await db
|
||||||
|
.delete(agents)
|
||||||
|
.where(and(eq(agents.id, id), eq(agents.ownerId, ownerId)))
|
||||||
|
.returning();
|
||||||
return rows.length > 0;
|
return rows.length > 0;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,18 +1,44 @@
|
|||||||
import { eq, type Db, conversations, messages } from '@mosaic/db';
|
import { eq, and, asc, desc, ilike, type Db, conversations, messages } from '@mosaic/db';
|
||||||
|
|
||||||
|
/** Maximum number of conversations returned per list query. */
|
||||||
|
const MAX_CONVERSATIONS = 200;
|
||||||
|
/** Maximum number of messages returned per conversation history query. */
|
||||||
|
const MAX_MESSAGES = 500;
|
||||||
|
|
||||||
export type Conversation = typeof conversations.$inferSelect;
|
export type Conversation = typeof conversations.$inferSelect;
|
||||||
export type NewConversation = typeof conversations.$inferInsert;
|
export type NewConversation = typeof conversations.$inferInsert;
|
||||||
export type Message = typeof messages.$inferSelect;
|
export type Message = typeof messages.$inferSelect;
|
||||||
export type NewMessage = typeof messages.$inferInsert;
|
export type NewMessage = typeof messages.$inferInsert;
|
||||||
|
|
||||||
|
export interface MessageSearchResult {
|
||||||
|
messageId: string;
|
||||||
|
conversationId: string;
|
||||||
|
conversationTitle: string | null;
|
||||||
|
role: 'user' | 'assistant' | 'system';
|
||||||
|
content: string;
|
||||||
|
createdAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export function createConversationsRepo(db: Db) {
|
export function createConversationsRepo(db: Db) {
|
||||||
return {
|
return {
|
||||||
async findAll(userId: string): Promise<Conversation[]> {
|
async findAll(userId: string): Promise<Conversation[]> {
|
||||||
return db.select().from(conversations).where(eq(conversations.userId, userId));
|
return db
|
||||||
|
.select()
|
||||||
|
.from(conversations)
|
||||||
|
.where(eq(conversations.userId, userId))
|
||||||
|
.orderBy(desc(conversations.updatedAt))
|
||||||
|
.limit(MAX_CONVERSATIONS);
|
||||||
},
|
},
|
||||||
|
|
||||||
async findById(id: string): Promise<Conversation | undefined> {
|
/**
|
||||||
const rows = await db.select().from(conversations).where(eq(conversations.id, id));
|
* Find a conversation by ID, scoped to the given user.
|
||||||
|
* Returns undefined if the conversation does not exist or belongs to a different user.
|
||||||
|
*/
|
||||||
|
async findById(id: string, userId: string): Promise<Conversation | undefined> {
|
||||||
|
const rows = await db
|
||||||
|
.select()
|
||||||
|
.from(conversations)
|
||||||
|
.where(and(eq(conversations.id, id), eq(conversations.userId, userId)));
|
||||||
return rows[0];
|
return rows[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -21,25 +47,97 @@ export function createConversationsRepo(db: Db) {
|
|||||||
return rows[0]!;
|
return rows[0]!;
|
||||||
},
|
},
|
||||||
|
|
||||||
async update(id: string, data: Partial<NewConversation>): Promise<Conversation | undefined> {
|
/**
|
||||||
|
* Update a conversation, scoped to the given user.
|
||||||
|
* Returns undefined if the conversation does not exist or belongs to a different user.
|
||||||
|
*/
|
||||||
|
async update(
|
||||||
|
id: string,
|
||||||
|
userId: string,
|
||||||
|
data: Partial<NewConversation>,
|
||||||
|
): Promise<Conversation | undefined> {
|
||||||
const rows = await db
|
const rows = await db
|
||||||
.update(conversations)
|
.update(conversations)
|
||||||
.set({ ...data, updatedAt: new Date() })
|
.set({ ...data, updatedAt: new Date() })
|
||||||
.where(eq(conversations.id, id))
|
.where(and(eq(conversations.id, id), eq(conversations.userId, userId)))
|
||||||
.returning();
|
.returning();
|
||||||
return rows[0];
|
return rows[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
async remove(id: string): Promise<boolean> {
|
/**
|
||||||
const rows = await db.delete(conversations).where(eq(conversations.id, id)).returning();
|
* Delete a conversation, scoped to the given user.
|
||||||
|
* Returns false if the conversation does not exist or belongs to a different user.
|
||||||
|
*/
|
||||||
|
async remove(id: string, userId: string): Promise<boolean> {
|
||||||
|
const rows = await db
|
||||||
|
.delete(conversations)
|
||||||
|
.where(and(eq(conversations.id, id), eq(conversations.userId, userId)))
|
||||||
|
.returning();
|
||||||
return rows.length > 0;
|
return rows.length > 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
async findMessages(conversationId: string): Promise<Message[]> {
|
/**
|
||||||
return db.select().from(messages).where(eq(messages.conversationId, conversationId));
|
* Find messages for a conversation, scoped to the given user.
|
||||||
|
* Returns an empty array if the conversation does not exist or belongs to a different user.
|
||||||
|
*/
|
||||||
|
async findMessages(conversationId: string, userId: string): Promise<Message[]> {
|
||||||
|
// Verify ownership of the parent conversation before returning messages.
|
||||||
|
const conv = await db
|
||||||
|
.select()
|
||||||
|
.from(conversations)
|
||||||
|
.where(and(eq(conversations.id, conversationId), eq(conversations.userId, userId)));
|
||||||
|
if (conv.length === 0) return [];
|
||||||
|
|
||||||
|
return db
|
||||||
|
.select()
|
||||||
|
.from(messages)
|
||||||
|
.where(eq(messages.conversationId, conversationId))
|
||||||
|
.orderBy(asc(messages.createdAt))
|
||||||
|
.limit(MAX_MESSAGES);
|
||||||
},
|
},
|
||||||
|
|
||||||
async addMessage(data: NewMessage): Promise<Message> {
|
/**
|
||||||
|
* Search messages by content across all conversations belonging to the user.
|
||||||
|
* Uses ILIKE for case-insensitive substring matching.
|
||||||
|
*/
|
||||||
|
async searchMessages(
|
||||||
|
userId: string,
|
||||||
|
query: string,
|
||||||
|
limit: number,
|
||||||
|
offset: number,
|
||||||
|
): Promise<MessageSearchResult[]> {
|
||||||
|
const rows = await db
|
||||||
|
.select({
|
||||||
|
messageId: messages.id,
|
||||||
|
conversationId: conversations.id,
|
||||||
|
conversationTitle: conversations.title,
|
||||||
|
role: messages.role,
|
||||||
|
content: messages.content,
|
||||||
|
createdAt: messages.createdAt,
|
||||||
|
})
|
||||||
|
.from(messages)
|
||||||
|
.innerJoin(conversations, eq(messages.conversationId, conversations.id))
|
||||||
|
.where(and(eq(conversations.userId, userId), ilike(messages.content, `%${query}%`)))
|
||||||
|
.orderBy(desc(messages.createdAt))
|
||||||
|
.limit(limit)
|
||||||
|
.offset(offset);
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a message to a conversation, scoped to the given user.
|
||||||
|
* Verifies the parent conversation belongs to the user before inserting.
|
||||||
|
* Returns undefined if the conversation does not exist or belongs to a different user.
|
||||||
|
*/
|
||||||
|
async addMessage(data: NewMessage, userId: string): Promise<Message | undefined> {
|
||||||
|
// Verify ownership of the parent conversation before inserting the message.
|
||||||
|
const conv = await db
|
||||||
|
.select()
|
||||||
|
.from(conversations)
|
||||||
|
.where(and(eq(conversations.id, data.conversationId), eq(conversations.userId, userId)));
|
||||||
|
if (conv.length === 0) return undefined;
|
||||||
|
|
||||||
const rows = await db.insert(messages).values(data).returning();
|
const rows = await db.insert(messages).values(data).returning();
|
||||||
return rows[0]!;
|
return rows[0]!;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ export {
|
|||||||
type NewConversation,
|
type NewConversation,
|
||||||
type Message,
|
type Message,
|
||||||
type NewMessage,
|
type NewMessage,
|
||||||
|
type MessageSearchResult,
|
||||||
} from './conversations.js';
|
} from './conversations.js';
|
||||||
export {
|
export {
|
||||||
createAgentsRepo,
|
createAgentsRepo,
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import { useViewport } from './hooks/use-viewport.js';
|
|||||||
import { useAppMode } from './hooks/use-app-mode.js';
|
import { useAppMode } from './hooks/use-app-mode.js';
|
||||||
import { useConversations } from './hooks/use-conversations.js';
|
import { useConversations } from './hooks/use-conversations.js';
|
||||||
import { useSearch } from './hooks/use-search.js';
|
import { useSearch } from './hooks/use-search.js';
|
||||||
import { executeHelp, executeStatus, commandRegistry } from './commands/index.js';
|
import { executeHelp, executeStatus, executeHistory, commandRegistry } from './commands/index.js';
|
||||||
|
import { fetchConversationMessages } from './gateway-api.js';
|
||||||
|
|
||||||
export interface TuiAppProps {
|
export interface TuiAppProps {
|
||||||
gatewayUrl: string;
|
gatewayUrl: string;
|
||||||
@@ -133,6 +134,23 @@ export function TuiApp({
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'history':
|
||||||
|
case 'hist': {
|
||||||
|
void executeHistory({
|
||||||
|
conversationId: socket.conversationId,
|
||||||
|
gatewayUrl,
|
||||||
|
sessionCookie,
|
||||||
|
fetchMessages: fetchConversationMessages,
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
socket.addSystemMessage(result);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
const msg = err instanceof Error ? err.message : String(err);
|
||||||
|
socket.addSystemMessage(`Failed to fetch history: ${msg}`);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
socket.addSystemMessage(`Local command not implemented: /${parsed.command}`);
|
socket.addSystemMessage(`Local command not implemented: /${parsed.command}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,3 +3,5 @@ export { commandRegistry, CommandRegistry } from './registry.js';
|
|||||||
export { executeHelp } from './local/help.js';
|
export { executeHelp } from './local/help.js';
|
||||||
export { executeStatus } from './local/status.js';
|
export { executeStatus } from './local/status.js';
|
||||||
export type { StatusContext } from './local/status.js';
|
export type { StatusContext } from './local/status.js';
|
||||||
|
export { executeHistory } from './local/history.js';
|
||||||
|
export type { HistoryContext } from './local/history.js';
|
||||||
|
|||||||
53
packages/cli/src/tui/commands/local/history.ts
Normal file
53
packages/cli/src/tui/commands/local/history.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import type { ConversationMessage } from '../../gateway-api.js';
|
||||||
|
|
||||||
|
const CONTEXT_WINDOW = 200_000;
|
||||||
|
const CHARS_PER_TOKEN = 4;
|
||||||
|
|
||||||
|
function estimateTokens(messages: ConversationMessage[]): number {
|
||||||
|
const totalChars = messages.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);
|
||||||
|
return Math.round(totalChars / CHARS_PER_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HistoryContext {
|
||||||
|
conversationId: string | undefined;
|
||||||
|
conversationTitle?: string | null;
|
||||||
|
gatewayUrl: string;
|
||||||
|
sessionCookie: string | undefined;
|
||||||
|
fetchMessages: (
|
||||||
|
gatewayUrl: string,
|
||||||
|
sessionCookie: string,
|
||||||
|
conversationId: string,
|
||||||
|
) => Promise<ConversationMessage[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function executeHistory(ctx: HistoryContext): Promise<string> {
|
||||||
|
const { conversationId, conversationTitle, gatewayUrl, sessionCookie, fetchMessages } = ctx;
|
||||||
|
|
||||||
|
if (!conversationId) {
|
||||||
|
return 'No active conversation.';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sessionCookie) {
|
||||||
|
return 'Not authenticated — cannot fetch conversation messages.';
|
||||||
|
}
|
||||||
|
|
||||||
|
const messages = await fetchMessages(gatewayUrl, sessionCookie, conversationId);
|
||||||
|
|
||||||
|
const userMessages = messages.filter((m) => m.role === 'user').length;
|
||||||
|
const assistantMessages = messages.filter((m) => m.role === 'assistant').length;
|
||||||
|
const totalMessages = messages.length;
|
||||||
|
|
||||||
|
const estimatedTokens = estimateTokens(messages);
|
||||||
|
const contextPercent = Math.round((estimatedTokens / CONTEXT_WINDOW) * 100);
|
||||||
|
|
||||||
|
const label = conversationTitle ?? conversationId;
|
||||||
|
|
||||||
|
const lines = [
|
||||||
|
`Conversation: ${label}`,
|
||||||
|
`Messages: ${totalMessages} (${userMessages} user, ${assistantMessages} assistant)`,
|
||||||
|
`Estimated tokens: ~${estimatedTokens.toLocaleString()}`,
|
||||||
|
`Context usage: ~${contextPercent}% of ${(CONTEXT_WINDOW / 1000).toFixed(0)}K`,
|
||||||
|
];
|
||||||
|
|
||||||
|
return lines.join('\n');
|
||||||
|
}
|
||||||
@@ -38,6 +38,15 @@ const LOCAL_COMMANDS: CommandDef[] = [
|
|||||||
available: true,
|
available: true,
|
||||||
scope: 'core',
|
scope: 'core',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'history',
|
||||||
|
description: 'Show conversation message count and context usage',
|
||||||
|
aliases: ['hist'],
|
||||||
|
args: undefined,
|
||||||
|
execution: 'local',
|
||||||
|
available: true,
|
||||||
|
scope: 'core',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'clear',
|
name: 'clear',
|
||||||
description: 'Clear the current conversation display',
|
description: 'Clear the current conversation display',
|
||||||
@@ -64,6 +73,7 @@ const ALIASES: Record<string, string> = {
|
|||||||
a: 'agent',
|
a: 'agent',
|
||||||
s: 'status',
|
s: 'status',
|
||||||
h: 'help',
|
h: 'help',
|
||||||
|
hist: 'history',
|
||||||
pref: 'preferences',
|
pref: 'preferences',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -361,6 +361,31 @@ export async function deleteMission(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Conversation Message types ──
|
||||||
|
|
||||||
|
export interface ConversationMessage {
|
||||||
|
id: string;
|
||||||
|
role: 'user' | 'assistant' | 'system' | 'tool';
|
||||||
|
content: string;
|
||||||
|
createdAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Conversation Message endpoints ──
|
||||||
|
|
||||||
|
export async function fetchConversationMessages(
|
||||||
|
gatewayUrl: string,
|
||||||
|
sessionCookie: string,
|
||||||
|
conversationId: string,
|
||||||
|
): Promise<ConversationMessage[]> {
|
||||||
|
const res = await fetch(
|
||||||
|
`${gatewayUrl}/api/conversations/${encodeURIComponent(conversationId)}/messages`,
|
||||||
|
{
|
||||||
|
headers: headers(sessionCookie, gatewayUrl),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return handleResponse<ConversationMessage[]>(res, 'Failed to fetch conversation messages');
|
||||||
|
}
|
||||||
|
|
||||||
// ── Mission Task endpoints ──
|
// ── Mission Task endpoints ──
|
||||||
|
|
||||||
export async function fetchMissionTasks(
|
export async function fetchMissionTasks(
|
||||||
|
|||||||
14
packages/db/drizzle/0003_p8003_perf_indexes.sql
Normal file
14
packages/db/drizzle/0003_p8003_perf_indexes.sql
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
DROP INDEX "agent_logs_session_id_idx";--> statement-breakpoint
|
||||||
|
DROP INDEX "agent_logs_tier_idx";--> statement-breakpoint
|
||||||
|
DROP INDEX "agent_logs_created_at_idx";--> statement-breakpoint
|
||||||
|
DROP INDEX "conversations_user_id_idx";--> statement-breakpoint
|
||||||
|
DROP INDEX "conversations_archived_idx";--> statement-breakpoint
|
||||||
|
DROP INDEX "preferences_user_key_idx";--> statement-breakpoint
|
||||||
|
CREATE INDEX "accounts_provider_account_idx" ON "accounts" USING btree ("provider_id","account_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "accounts_user_id_idx" ON "accounts" USING btree ("user_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "agent_logs_session_tier_idx" ON "agent_logs" USING btree ("session_id","tier");--> statement-breakpoint
|
||||||
|
CREATE INDEX "agent_logs_tier_created_at_idx" ON "agent_logs" USING btree ("tier","created_at");--> statement-breakpoint
|
||||||
|
CREATE INDEX "conversations_user_archived_idx" ON "conversations" USING btree ("user_id","archived");--> statement-breakpoint
|
||||||
|
CREATE INDEX "sessions_user_id_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "sessions_expires_at_idx" ON "sessions" USING btree ("expires_at");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "preferences_user_key_idx" ON "preferences" USING btree ("user_id","key");
|
||||||
2491
packages/db/drizzle/meta/0003_snapshot.json
Normal file
2491
packages/db/drizzle/meta/0003_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -22,6 +22,13 @@
|
|||||||
"when": 1773625181629,
|
"when": 1773625181629,
|
||||||
"tag": "0002_nebulous_mimic",
|
"tag": "0002_nebulous_mimic",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 3,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1773887085247,
|
||||||
|
"tag": "0003_p8003_perf_indexes",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,15 @@ export interface DbHandle {
|
|||||||
|
|
||||||
export function createDb(url?: string): DbHandle {
|
export function createDb(url?: string): DbHandle {
|
||||||
const connectionString = url ?? process.env['DATABASE_URL'] ?? DEFAULT_DATABASE_URL;
|
const connectionString = url ?? process.env['DATABASE_URL'] ?? DEFAULT_DATABASE_URL;
|
||||||
const sql = postgres(connectionString);
|
const sql = postgres(connectionString, {
|
||||||
|
// Pool sizing: allow up to 20 concurrent connections per gateway instance.
|
||||||
|
// Each NestJS module (brain, preferences, memory, coord) shares this pool.
|
||||||
|
max: Number(process.env['DB_POOL_MAX'] ?? 20),
|
||||||
|
// Recycle idle connections after 30 s to avoid stale TCP state.
|
||||||
|
idle_timeout: Number(process.env['DB_IDLE_TIMEOUT'] ?? 30),
|
||||||
|
// Fail fast (5 s) on connection problems rather than hanging indefinitely.
|
||||||
|
connect_timeout: Number(process.env['DB_CONNECT_TIMEOUT'] ?? 5),
|
||||||
|
});
|
||||||
const db = drizzle(sql, { schema });
|
const db = drizzle(sql, { schema });
|
||||||
return { db, close: () => sql.end() };
|
return { db, close: () => sql.end() };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,5 @@ export {
|
|||||||
lt,
|
lt,
|
||||||
gte,
|
gte,
|
||||||
lte,
|
lte,
|
||||||
|
ilike,
|
||||||
} from 'drizzle-orm';
|
} from 'drizzle-orm';
|
||||||
|
|||||||
@@ -33,36 +33,54 @@ export const users = pgTable('users', {
|
|||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const sessions = pgTable('sessions', {
|
export const sessions = pgTable(
|
||||||
id: text('id').primaryKey(),
|
'sessions',
|
||||||
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
|
{
|
||||||
token: text('token').notNull().unique(),
|
id: text('id').primaryKey(),
|
||||||
ipAddress: text('ip_address'),
|
expiresAt: timestamp('expires_at', { withTimezone: true }).notNull(),
|
||||||
userAgent: text('user_agent'),
|
token: text('token').notNull().unique(),
|
||||||
userId: text('user_id')
|
ipAddress: text('ip_address'),
|
||||||
.notNull()
|
userAgent: text('user_agent'),
|
||||||
.references(() => users.id, { onDelete: 'cascade' }),
|
userId: text('user_id')
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
.notNull()
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
.references(() => users.id, { onDelete: 'cascade' }),
|
||||||
});
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(t) => [
|
||||||
|
// Auth hot path: look up all sessions for a user (BetterAuth session list).
|
||||||
|
index('sessions_user_id_idx').on(t.userId),
|
||||||
|
// Session expiry cleanup queries.
|
||||||
|
index('sessions_expires_at_idx').on(t.expiresAt),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
export const accounts = pgTable('accounts', {
|
export const accounts = pgTable(
|
||||||
id: text('id').primaryKey(),
|
'accounts',
|
||||||
accountId: text('account_id').notNull(),
|
{
|
||||||
providerId: text('provider_id').notNull(),
|
id: text('id').primaryKey(),
|
||||||
userId: text('user_id')
|
accountId: text('account_id').notNull(),
|
||||||
.notNull()
|
providerId: text('provider_id').notNull(),
|
||||||
.references(() => users.id, { onDelete: 'cascade' }),
|
userId: text('user_id')
|
||||||
accessToken: text('access_token'),
|
.notNull()
|
||||||
refreshToken: text('refresh_token'),
|
.references(() => users.id, { onDelete: 'cascade' }),
|
||||||
idToken: text('id_token'),
|
accessToken: text('access_token'),
|
||||||
accessTokenExpiresAt: timestamp('access_token_expires_at', { withTimezone: true }),
|
refreshToken: text('refresh_token'),
|
||||||
refreshTokenExpiresAt: timestamp('refresh_token_expires_at', { withTimezone: true }),
|
idToken: text('id_token'),
|
||||||
scope: text('scope'),
|
accessTokenExpiresAt: timestamp('access_token_expires_at', { withTimezone: true }),
|
||||||
password: text('password'),
|
refreshTokenExpiresAt: timestamp('refresh_token_expires_at', { withTimezone: true }),
|
||||||
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
scope: text('scope'),
|
||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
password: text('password'),
|
||||||
});
|
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(t) => [
|
||||||
|
// BetterAuth looks up accounts by (provider_id, account_id) on OAuth callback.
|
||||||
|
index('accounts_provider_account_idx').on(t.providerId, t.accountId),
|
||||||
|
// Also used in session validation to find linked accounts for a user.
|
||||||
|
index('accounts_user_id_idx').on(t.userId),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
export const verifications = pgTable('verifications', {
|
export const verifications = pgTable('verifications', {
|
||||||
id: text('id').primaryKey(),
|
id: text('id').primaryKey(),
|
||||||
@@ -306,10 +324,10 @@ export const conversations = pgTable(
|
|||||||
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
|
||||||
},
|
},
|
||||||
(t) => [
|
(t) => [
|
||||||
index('conversations_user_id_idx').on(t.userId),
|
// Compound index for the most common query: conversations for a user filtered by archived.
|
||||||
|
index('conversations_user_archived_idx').on(t.userId, t.archived),
|
||||||
index('conversations_project_id_idx').on(t.projectId),
|
index('conversations_project_id_idx').on(t.projectId),
|
||||||
index('conversations_agent_id_idx').on(t.agentId),
|
index('conversations_agent_id_idx').on(t.agentId),
|
||||||
index('conversations_archived_idx').on(t.archived),
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -369,7 +387,8 @@ export const preferences = pgTable(
|
|||||||
},
|
},
|
||||||
(t) => [
|
(t) => [
|
||||||
index('preferences_user_id_idx').on(t.userId),
|
index('preferences_user_id_idx').on(t.userId),
|
||||||
index('preferences_user_key_idx').on(t.userId, t.key),
|
// Unique constraint enables single-round-trip INSERT … ON CONFLICT DO UPDATE.
|
||||||
|
uniqueIndex('preferences_user_key_idx').on(t.userId, t.key),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -431,10 +450,11 @@ export const agentLogs = pgTable(
|
|||||||
archivedAt: timestamp('archived_at', { withTimezone: true }),
|
archivedAt: timestamp('archived_at', { withTimezone: true }),
|
||||||
},
|
},
|
||||||
(t) => [
|
(t) => [
|
||||||
index('agent_logs_session_id_idx').on(t.sessionId),
|
// Compound index for session log queries (most common: session + tier filter).
|
||||||
|
index('agent_logs_session_tier_idx').on(t.sessionId, t.tier),
|
||||||
index('agent_logs_user_id_idx').on(t.userId),
|
index('agent_logs_user_id_idx').on(t.userId),
|
||||||
index('agent_logs_tier_idx').on(t.tier),
|
// Used by summarization cron to find hot logs older than a cutoff.
|
||||||
index('agent_logs_created_at_idx').on(t.createdAt),
|
index('agent_logs_tier_created_at_idx').on(t.tier, t.createdAt),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,11 @@ export function createInsightsRepo(db: Db) {
|
|||||||
.limit(limit);
|
.limit(limit);
|
||||||
},
|
},
|
||||||
|
|
||||||
async findById(id: string): Promise<Insight | undefined> {
|
async findById(id: string, userId: string): Promise<Insight | undefined> {
|
||||||
const rows = await db.select().from(insights).where(eq(insights.id, id));
|
const rows = await db
|
||||||
|
.select()
|
||||||
|
.from(insights)
|
||||||
|
.where(and(eq(insights.id, id), eq(insights.userId, userId)));
|
||||||
return rows[0];
|
return rows[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -29,17 +32,24 @@ export function createInsightsRepo(db: Db) {
|
|||||||
return rows[0]!;
|
return rows[0]!;
|
||||||
},
|
},
|
||||||
|
|
||||||
async update(id: string, data: Partial<NewInsight>): Promise<Insight | undefined> {
|
async update(
|
||||||
|
id: string,
|
||||||
|
userId: string,
|
||||||
|
data: Partial<NewInsight>,
|
||||||
|
): Promise<Insight | undefined> {
|
||||||
const rows = await db
|
const rows = await db
|
||||||
.update(insights)
|
.update(insights)
|
||||||
.set({ ...data, updatedAt: new Date() })
|
.set({ ...data, updatedAt: new Date() })
|
||||||
.where(eq(insights.id, id))
|
.where(and(eq(insights.id, id), eq(insights.userId, userId)))
|
||||||
.returning();
|
.returning();
|
||||||
return rows[0];
|
return rows[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
async remove(id: string): Promise<boolean> {
|
async remove(id: string, userId: string): Promise<boolean> {
|
||||||
const rows = await db.delete(insights).where(eq(insights.id, id)).returning();
|
const rows = await db
|
||||||
|
.delete(insights)
|
||||||
|
.where(and(eq(insights.id, id), eq(insights.userId, userId)))
|
||||||
|
.returning();
|
||||||
return rows.length > 0;
|
return rows.length > 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -70,8 +80,33 @@ export function createInsightsRepo(db: Db) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Decay relevance scores for old insights that haven't been accessed recently.
|
* Decay relevance scores for old insights that haven't been accessed recently.
|
||||||
|
* Scoped to a specific user to prevent cross-user data mutation.
|
||||||
*/
|
*/
|
||||||
async decayOldInsights(olderThan: Date, decayFactor = 0.95): Promise<number> {
|
async decayOldInsights(userId: string, olderThan: Date, decayFactor = 0.95): Promise<number> {
|
||||||
|
const result = await db
|
||||||
|
.update(insights)
|
||||||
|
.set({
|
||||||
|
relevanceScore: sql`${insights.relevanceScore} * ${decayFactor}`,
|
||||||
|
decayedAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(insights.userId, userId),
|
||||||
|
lt(insights.updatedAt, olderThan),
|
||||||
|
sql`${insights.relevanceScore} > 0.1`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.returning();
|
||||||
|
return result.length;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decay relevance scores for all users' old insights.
|
||||||
|
* This is a system-level maintenance operation intended for scheduled cron jobs only.
|
||||||
|
* Do NOT expose this through user-facing API endpoints.
|
||||||
|
*/
|
||||||
|
async decayAllInsights(olderThan: Date, decayFactor = 0.95): Promise<number> {
|
||||||
const result = await db
|
const result = await db
|
||||||
.update(insights)
|
.update(insights)
|
||||||
.set({
|
.set({
|
||||||
|
|||||||
@@ -1,3 +1,22 @@
|
|||||||
|
/** Per-model capability metadata used by the routing engine */
|
||||||
|
export interface ModelCapability {
|
||||||
|
id: string;
|
||||||
|
provider: string;
|
||||||
|
displayName: string;
|
||||||
|
tier: 'cheap' | 'standard' | 'premium' | 'local';
|
||||||
|
contextWindow: number;
|
||||||
|
maxOutputTokens: number;
|
||||||
|
capabilities: {
|
||||||
|
tools: boolean;
|
||||||
|
vision: boolean;
|
||||||
|
streaming: boolean;
|
||||||
|
reasoning: boolean;
|
||||||
|
embedding: boolean;
|
||||||
|
};
|
||||||
|
costPer1kInput?: number;
|
||||||
|
costPer1kOutput?: number;
|
||||||
|
}
|
||||||
|
|
||||||
/** Known built-in LLM provider identifiers */
|
/** Known built-in LLM provider identifiers */
|
||||||
export type KnownProvider =
|
export type KnownProvider =
|
||||||
| 'anthropic'
|
| 'anthropic'
|
||||||
@@ -52,3 +71,100 @@ export interface CustomProviderConfig {
|
|||||||
maxTokens?: number;
|
maxTokens?: number;
|
||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// IProviderAdapter pattern — M3-001
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Health status of a provider */
|
||||||
|
export type ProviderHealthStatus = 'healthy' | 'degraded' | 'down';
|
||||||
|
|
||||||
|
/** Result of a provider health check */
|
||||||
|
export interface ProviderHealth {
|
||||||
|
status: ProviderHealthStatus;
|
||||||
|
/** Round-trip latency in milliseconds (undefined when provider is down) */
|
||||||
|
latencyMs?: number;
|
||||||
|
/** ISO-8601 timestamp of the check */
|
||||||
|
lastChecked: string;
|
||||||
|
/** Human-readable error message (defined when status is not healthy) */
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A single message in a completion request */
|
||||||
|
export interface CompletionMessage {
|
||||||
|
role: 'system' | 'user' | 'assistant';
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tool definition for completion requests */
|
||||||
|
export interface CompletionTool {
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
parameters: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parameters for a completion request */
|
||||||
|
export interface CompletionParams {
|
||||||
|
model: string;
|
||||||
|
messages: CompletionMessage[];
|
||||||
|
tools?: CompletionTool[];
|
||||||
|
temperature?: number;
|
||||||
|
maxTokens?: number;
|
||||||
|
stream?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Usage statistics for a completion event */
|
||||||
|
export interface CompletionUsage {
|
||||||
|
inputTokens: number;
|
||||||
|
outputTokens: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A streamed completion event */
|
||||||
|
export type CompletionEvent =
|
||||||
|
| { type: 'text_delta'; content: string }
|
||||||
|
| { type: 'tool_call'; name: string; arguments: string }
|
||||||
|
| { type: 'done'; usage?: CompletionUsage };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pluggable provider adapter interface.
|
||||||
|
*
|
||||||
|
* Each LLM provider (Anthropic, OpenAI, Ollama, etc.) implements this interface
|
||||||
|
* to integrate with Mosaic's provider layer. The ProviderService aggregates all
|
||||||
|
* registered adapters and routes requests accordingly.
|
||||||
|
*
|
||||||
|
* Note on createCompletion: this method is part of the interface for future
|
||||||
|
* direct-completion use cases. The current Pi SDK integration routes completions
|
||||||
|
* through the Pi session/ModelRegistry layer rather than calling adapters directly.
|
||||||
|
* Adapters MUST still implement register() and healthCheck() correctly — those are
|
||||||
|
* used by ProviderService today.
|
||||||
|
*/
|
||||||
|
export interface IProviderAdapter {
|
||||||
|
/** Unique provider identifier (e.g. 'anthropic', 'openai', 'ollama') */
|
||||||
|
readonly name: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the provider — connect, discover models, register with the
|
||||||
|
* Pi ModelRegistry. Called once at module startup by ProviderService.registerAll().
|
||||||
|
*/
|
||||||
|
register(): Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the list of models this adapter makes available.
|
||||||
|
* Returns an empty array when the provider is not configured.
|
||||||
|
*/
|
||||||
|
listModels(): ModelInfo[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the provider endpoint is reachable and responsive.
|
||||||
|
*/
|
||||||
|
healthCheck(): Promise<ProviderHealth>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream a completion from the provider.
|
||||||
|
*
|
||||||
|
* Note: Currently reserved for future use. The Pi SDK integration routes
|
||||||
|
* completions through ModelRegistry / AgentSession rather than this method.
|
||||||
|
* Implementations may throw NotImplementedError until M3+ tasks wire this up.
|
||||||
|
*/
|
||||||
|
createCompletion(params: CompletionParams): AsyncIterable<CompletionEvent>;
|
||||||
|
}
|
||||||
|
|||||||
700
pnpm-lock.yaml
generated
700
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user