feat: gated project experience & prompt intelligence

Implements the full gated session flow and prompt intelligence system:

- Prisma schema: add gated, priority, summary, chapters, linkTarget fields
- Session gate: state machine (gated → begin_session → ungated) with LLM-powered
  tool selection based on prompt index
- Tag matcher: intelligent prompt-to-tool matching with project/server/action tags
- LLM selector: tiered provider selection (fast for gating, heavy for complex tasks)
- Link resolver: cross-project MCP resource references (project/server:uri format)
- Prompt summary service: LLM-generated summaries and chapter extraction
- System project bootstrap: ensures default project exists on startup
- Structural link health checks: enrichWithLinkStatus on prompt GET endpoints
- CLI: create prompt --priority/--link, create project --gated/--no-gated,
  describe project shows prompts section, get prompts shows PRI/LINK/STATUS
- Apply/edit: priority, linkTarget, gated fields supported
- Shell completions: fish updated with new flags
- 1,253 tests passing across all packages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Michal
2026-02-25 23:22:42 +00:00
parent 3782bcf9d7
commit ecc9c48597
46 changed files with 4946 additions and 105 deletions

View File

@@ -11,6 +11,10 @@ function makePrompt(overrides: Partial<Prompt> = {}): Prompt {
name: 'test-prompt',
content: 'Hello world',
projectId: null,
priority: 5,
summary: null,
chapters: null,
linkTarget: null,
version: 1,
createdAt: new Date(),
updatedAt: new Date(),
@@ -24,6 +28,7 @@ function makePromptRequest(overrides: Partial<PromptRequest> = {}): PromptReques
name: 'test-request',
content: 'Proposed content',
projectId: null,
priority: 5,
createdBySession: 'session-abc',
createdByUserId: null,
createdAt: new Date(),
@@ -38,6 +43,7 @@ function makeProject(overrides: Partial<Project> = {}): Project {
description: '',
prompt: '',
proxyMode: 'direct',
gated: true,
llmProvider: null,
llmModel: null,
ownerId: 'user-1',
@@ -50,6 +56,7 @@ function makeProject(overrides: Partial<Project> = {}): Project {
function mockPromptRepo(): IPromptRepository {
return {
findAll: vi.fn(async () => []),
findGlobal: vi.fn(async () => []),
findById: vi.fn(async () => null),
findByNameAndProject: vi.fn(async () => null),
create: vi.fn(async (data) => makePrompt(data)),
@@ -61,10 +68,12 @@ function mockPromptRepo(): IPromptRepository {
function mockPromptRequestRepo(): IPromptRequestRepository {
return {
findAll: vi.fn(async () => []),
findGlobal: vi.fn(async () => []),
findById: vi.fn(async () => null),
findByNameAndProject: vi.fn(async () => null),
findBySession: vi.fn(async () => []),
create: vi.fn(async (data) => makePromptRequest(data)),
update: vi.fn(async (id, data) => makePromptRequest({ id, ...data })),
delete: vi.fn(async () => {}),
};
}
@@ -111,6 +120,17 @@ describe('PromptService', () => {
});
});
describe('listGlobalPrompts', () => {
it('should return only global prompts', async () => {
const globalPrompts = [makePrompt({ name: 'global-rule', projectId: null })];
vi.mocked(promptRepo.findGlobal).mockResolvedValue(globalPrompts);
const result = await service.listGlobalPrompts();
expect(result).toEqual(globalPrompts);
expect(promptRepo.findGlobal).toHaveBeenCalled();
});
});
describe('getPrompt', () => {
it('should return a prompt by id', async () => {
const prompt = makePrompt();
@@ -173,6 +193,21 @@ describe('PromptService', () => {
it('should throw for missing prompt', async () => {
await expect(service.deletePrompt('nope')).rejects.toThrow('Prompt not found');
});
it('should reject deletion of system prompts', async () => {
vi.mocked(promptRepo.findById).mockResolvedValue(makePrompt({ projectId: 'sys-proj' }));
vi.mocked(projectRepo.findById).mockResolvedValue(makeProject({ id: 'sys-proj', name: 'mcpctl-system' }));
await expect(service.deletePrompt('prompt-1')).rejects.toThrow('Cannot delete system prompts');
});
it('should allow deletion of non-system project prompts', async () => {
vi.mocked(promptRepo.findById).mockResolvedValue(makePrompt({ projectId: 'proj-1' }));
vi.mocked(projectRepo.findById).mockResolvedValue(makeProject({ id: 'proj-1', name: 'my-project' }));
await service.deletePrompt('prompt-1');
expect(promptRepo.delete).toHaveBeenCalledWith('prompt-1');
});
});
// ── PromptRequest CRUD ──
@@ -267,6 +302,90 @@ describe('PromptService', () => {
});
});
// ── Priority ──
describe('prompt priority', () => {
it('creates prompt with explicit priority', async () => {
const result = await service.createPrompt({ name: 'high-pri', content: 'x', priority: 8 });
expect(promptRepo.create).toHaveBeenCalledWith(expect.objectContaining({ priority: 8 }));
expect(result.priority).toBe(8);
});
it('uses default priority 5 when not specified', async () => {
const result = await service.createPrompt({ name: 'default-pri', content: 'x' });
// Default in schema is 5 — create is called without priority
const createArg = vi.mocked(promptRepo.create).mock.calls[0]![0];
expect(createArg.priority).toBeUndefined();
});
it('rejects priority below 1', async () => {
await expect(
service.createPrompt({ name: 'bad-pri', content: 'x', priority: 0 }),
).rejects.toThrow();
});
it('rejects priority above 10', async () => {
await expect(
service.createPrompt({ name: 'bad-pri', content: 'x', priority: 11 }),
).rejects.toThrow();
});
it('updates prompt priority', async () => {
vi.mocked(promptRepo.findById).mockResolvedValue(makePrompt());
await service.updatePrompt('prompt-1', { priority: 3 });
expect(promptRepo.update).toHaveBeenCalledWith('prompt-1', expect.objectContaining({ priority: 3 }));
});
});
// ── Link Target ──
describe('prompt links', () => {
it('creates linked prompt with valid linkTarget', async () => {
const result = await service.createPrompt({
name: 'linked',
content: 'link content',
linkTarget: 'other-project/docmost-mcp:docmost://pages/abc',
});
expect(promptRepo.create).toHaveBeenCalledWith(
expect.objectContaining({ linkTarget: 'other-project/docmost-mcp:docmost://pages/abc' }),
);
});
it('rejects invalid link format', async () => {
await expect(
service.createPrompt({ name: 'bad-link', content: 'x', linkTarget: 'invalid-format' }),
).rejects.toThrow();
});
it('rejects link without server part', async () => {
await expect(
service.createPrompt({ name: 'bad-link', content: 'x', linkTarget: 'project:uri' }),
).rejects.toThrow();
});
it('approve carries priority from request to prompt', async () => {
const req = makePromptRequest({ id: 'req-1', name: 'high-pri', content: 'x', projectId: 'proj-1', priority: 9 });
vi.mocked(promptRequestRepo.findById).mockResolvedValue(req);
await service.approve('req-1');
expect(promptRepo.create).toHaveBeenCalledWith(
expect.objectContaining({ priority: 9 }),
);
});
it('propose passes priority through', async () => {
const result = await service.propose({
name: 'pri-req',
content: 'x',
priority: 7,
});
expect(promptRequestRepo.create).toHaveBeenCalledWith(
expect.objectContaining({ priority: 7 }),
);
});
});
// ── Visibility ──
describe('getVisiblePrompts', () => {

View File

@@ -0,0 +1,110 @@
import { describe, it, expect, vi } from 'vitest';
import {
PromptSummaryService,
extractFirstSentence,
extractHeadings,
type LlmSummaryGenerator,
} from '../../src/services/prompt-summary.service.js';
describe('extractFirstSentence', () => {
it('extracts first sentence from plain text', () => {
const result = extractFirstSentence('This is the first sentence. And this is the second.', 20);
expect(result).toBe('This is the first sentence.');
});
it('truncates to maxWords', () => {
const long = 'word '.repeat(30).trim();
const result = extractFirstSentence(long, 5);
expect(result).toBe('word word word word word...');
});
it('skips markdown headings to find content', () => {
const content = '# Title\n\n## Subtitle\n\nActual content here. More text.';
expect(extractFirstSentence(content, 20)).toBe('Actual content here.');
});
it('falls back to first heading if no content lines', () => {
const content = '# Only Headings\n## Nothing Else';
expect(extractFirstSentence(content, 20)).toBe('Only Headings');
});
it('strips markdown formatting', () => {
const content = 'This has **bold** and *italic* and `code` and [link](http://example.com).';
expect(extractFirstSentence(content, 20)).toBe('This has bold and italic and code and link.');
});
it('handles empty content', () => {
expect(extractFirstSentence('', 20)).toBe('');
expect(extractFirstSentence(' ', 20)).toBe('');
});
it('handles content with no sentence boundary', () => {
const content = 'No period at the end';
expect(extractFirstSentence(content, 20)).toBe('No period at the end');
});
it('handles exclamation and question marks', () => {
expect(extractFirstSentence('Is this a question? Yes it is.', 20)).toBe('Is this a question?');
expect(extractFirstSentence('Watch out! Be careful.', 20)).toBe('Watch out!');
});
});
describe('extractHeadings', () => {
it('extracts all levels of markdown headings', () => {
const content = '# H1\n## H2\n### H3\nSome text\n#### H4';
expect(extractHeadings(content)).toEqual(['H1', 'H2', 'H3', 'H4']);
});
it('returns empty array for content without headings', () => {
expect(extractHeadings('Just plain text\nMore text')).toEqual([]);
});
it('handles empty content', () => {
expect(extractHeadings('')).toEqual([]);
});
it('trims heading text', () => {
const content = '# Spaced Heading \n## Another ';
expect(extractHeadings(content)).toEqual(['Spaced Heading', 'Another']);
});
});
describe('PromptSummaryService', () => {
it('uses regex fallback when no LLM', async () => {
const service = new PromptSummaryService(null);
const result = await service.generateSummary('# Overview\n\nThis is a test document. It has content.\n\n## Section One\n\n## Section Two');
expect(result.summary).toBe('This is a test document.');
expect(result.chapters).toEqual(['Overview', 'Section One', 'Section Two']);
});
it('uses LLM when available', async () => {
const mockLlm: LlmSummaryGenerator = {
generate: vi.fn(async () => ({
summary: 'LLM-generated summary',
chapters: ['LLM Chapter 1'],
})),
};
const service = new PromptSummaryService(mockLlm);
const result = await service.generateSummary('Some content');
expect(result.summary).toBe('LLM-generated summary');
expect(result.chapters).toEqual(['LLM Chapter 1']);
expect(mockLlm.generate).toHaveBeenCalledWith('Some content');
});
it('falls back to regex on LLM failure', async () => {
const mockLlm: LlmSummaryGenerator = {
generate: vi.fn(async () => { throw new Error('LLM unavailable'); }),
};
const service = new PromptSummaryService(mockLlm);
const result = await service.generateSummary('Fallback content here. Second sentence.');
expect(result.summary).toBe('Fallback content here.');
expect(mockLlm.generate).toHaveBeenCalled();
});
it('generateWithRegex works directly', () => {
const service = new PromptSummaryService(null);
const result = service.generateWithRegex('# Title\n\nContent line. More.\n\n## Chapter A\n\n## Chapter B');
expect(result.summary).toBe('Content line.');
expect(result.chapters).toEqual(['Title', 'Chapter A', 'Chapter B']);
});
});