Feature/ai post suggestions (#40)

* feat: first cut on ai suggestion system for title and summary

* feat: completion of titling/excerpt/slug-suggestion AI quick action

* feat: feeds use existing excerpts. also documentation.

---------

Co-authored-by: hugo <hugoms@me.com>
This commit is contained in:
Georg Bauer
2026-03-07 09:54:13 +01:00
committed by GitHub
parent 72b21ddba7
commit 9871cb827f
30 changed files with 1270 additions and 245 deletions

View File

@@ -183,4 +183,56 @@ describe('GenerationSitemapFeedService', () => {
expect(result.atomXml).toContain('xml:lang="en"');
expect(result.atomXml).toContain('xml:lang="de"');
});
it('uses excerpt instead of full body in feed entry content when excerpt is available', () => {
const publishedPosts = [
makePost({
id: '1',
slug: 'excerpt-post',
title: 'Excerpt Post',
excerpt: 'Short feed summary.',
content: '# Excerpt Post\n\nVery long body that should not appear in feed content.',
}),
];
const result = buildSitemapAndFeeds({
baseUrl: 'https://example.com',
projectName: 'Test Blog',
maxPostsPerPage: 10,
publishedPosts,
publishedListPosts: publishedPosts,
postIndex: buildIndex(publishedPosts),
includeFeeds: true,
});
expect(result.rssXml).toContain('<content:encoded><![CDATA[<p>Short feed summary.</p>]]></content:encoded>');
expect(result.rssXml).not.toContain('Very long body that should not appear in feed content.');
expect(result.atomXml).toContain('<content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"><p>Short feed summary.</p></div></content>');
expect(result.atomXml).not.toContain('Very long body that should not appear in feed content.');
});
it('falls back to the post body in feed entry content when excerpt is missing', () => {
const publishedPosts = [
makePost({
id: '1',
slug: 'body-post',
title: 'Body Post',
content: '# Body Post\n\nBody paragraph used in feed content.',
}),
];
const result = buildSitemapAndFeeds({
baseUrl: 'https://example.com',
projectName: 'Test Blog',
maxPostsPerPage: 10,
publishedPosts,
publishedListPosts: publishedPosts,
postIndex: buildIndex(publishedPosts),
includeFeeds: true,
});
expect(result.rssXml).toContain('Body paragraph used in feed content.');
expect(result.atomXml).toContain('Body paragraph used in feed content.');
});
});

View File

@@ -1028,6 +1028,40 @@ Original content`);
expect(result?.slug).toBe('new-title');
});
it('should honor explicit slug when title and slug both change on a never-published draft', async () => {
const created = await postEngine.createPost({ title: 'Original Title' });
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: created.id,
projectId: created.projectId,
title: created.title,
slug: created.slug,
status: 'draft',
content: created.content || '',
filePath: '',
tags: '[]',
categories: '[]',
createdAt: created.createdAt,
updatedAt: created.updatedAt,
publishedAt: null,
}),
});
return chain;
});
const result = await postEngine.updatePost(created.id, {
title: 'New Title',
slug: 'custom-ai-slug',
});
expect(result).not.toBeNull();
expect(result?.slug).toBe('custom-ai-slug');
});
it('should NOT auto-update slug when title changes on a previously published post', async () => {
const created = await postEngine.createPost({ title: 'Published Post' });
@@ -1059,6 +1093,37 @@ Original content`);
expect(result?.slug).toBe('published-post'); // slug preserved
});
it('should ignore explicit slug changes on a previously published post', async () => {
const created = await postEngine.createPost({ title: 'Published Post' });
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: created.id,
projectId: created.projectId,
title: created.title,
slug: created.slug,
status: 'draft',
content: created.content || '',
filePath: '',
tags: '[]',
categories: '[]',
createdAt: created.createdAt,
updatedAt: created.updatedAt,
publishedAt: new Date('2025-01-01'),
}),
});
return chain;
});
const result = await postEngine.updatePost(created.id, { slug: 'new-slug' });
expect(result).not.toBeNull();
expect(result?.slug).toBe('published-post');
});
it('should allow empty title and use untitled as slug base', async () => {
const created = await postEngine.createPost({ title: '' });
expect(created.title).toBe('');

View File

@@ -0,0 +1,184 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock the AI SDK generateText before importing OneShotTasks
vi.mock('ai', () => ({
generateText: vi.fn(),
}));
// Mock i18n
vi.mock('../../../src/main/shared/i18n', () => ({
resolveSupportedRenderLanguage: vi.fn((lang: string) => lang),
translateRender: vi.fn((_lang: string, key: string) => {
const prompts: Record<string, string> = {
'ai.postAnalysis.system': 'You are a blog editor assistant. Analyze the blog post and suggest improvements. Return JSON with "title", "excerpt", "slug". Respond in en.',
'ai.postAnalysis.user': 'Analyze this blog post.',
};
return prompts[key] || key;
}),
}));
import { OneShotTasks, type PostAnalysisResult } from '../../../src/main/engine/ai/tasks';
import { generateText } from 'ai';
const mockGenerateText = vi.mocked(generateText);
function createMockDeps() {
const chatEngine = {
getSetting: vi.fn().mockResolvedValue(null),
} as any;
const providers = {
detectModelProvider: vi.fn().mockReturnValue('opencode'),
isProviderKeySet: vi.fn().mockReturnValue(true),
getOpencodeKey: vi.fn().mockReturnValue('test-key'),
getMistralKey: vi.fn().mockReturnValue(null),
resolveModel: vi.fn().mockReturnValue('mock-model'),
isOfflineMode: vi.fn().mockReturnValue(false),
isOllamaModel: vi.fn().mockReturnValue(false),
isLmstudioModel: vi.fn().mockReturnValue(false),
getFirstKnownLocalModelId: vi.fn().mockReturnValue(null),
} as any;
const mediaEngine = {} as any;
const postEngine = {
getPost: vi.fn(),
} as any;
return { chatEngine, providers, mediaEngine, postEngine };
}
describe('OneShotTasks.analyzePost', () => {
let deps: ReturnType<typeof createMockDeps>;
let tasks: OneShotTasks;
beforeEach(() => {
vi.clearAllMocks();
deps = createMockDeps();
tasks = new OneShotTasks(deps.providers, deps.chatEngine, deps.mediaEngine, deps.postEngine);
});
it('returns title, excerpt, and slug from AI response', async () => {
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: 'My Post',
slug: 'my-post',
excerpt: '',
content: 'This is the content of my blog post about testing.',
status: 'draft',
});
mockGenerateText.mockResolvedValue({
text: '{"title": "Better Title", "excerpt": "A concise summary of the post.", "slug": "better-title"}',
} as any);
const result: PostAnalysisResult = await tasks.analyzePost('post-1', 'en');
expect(result.success).toBe(true);
expect(result.title).toBe('Better Title');
expect(result.excerpt).toBe('A concise summary of the post.');
expect(result.slug).toBe('better-title');
expect(deps.postEngine.getPost).toHaveBeenCalledWith('post-1');
});
it('returns error when post is not found', async () => {
deps.postEngine.getPost.mockResolvedValue(null);
const result = await tasks.analyzePost('nonexistent', 'en');
expect(result.success).toBe(false);
expect(result.error).toBe('Post not found');
});
it('returns error when post has no content', async () => {
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: '',
slug: 'post-1',
content: '',
status: 'draft',
});
const result = await tasks.analyzePost('post-1', 'en');
expect(result.success).toBe(false);
expect(result.error).toBe('Post has no content to analyze');
});
it('returns error when no API key is configured', async () => {
deps.providers.getOpencodeKey.mockReturnValue(null);
deps.providers.getMistralKey.mockReturnValue(null);
deps.providers.isProviderKeySet.mockReturnValue(false);
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: 'Test',
content: 'Content here',
status: 'draft',
});
const result = await tasks.analyzePost('post-1', 'en');
expect(result.success).toBe(false);
expect(result.error).toContain('API key');
});
it('sanitizes slug to lowercase with hyphens only', async () => {
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: 'Test',
slug: 'test',
content: 'Content here',
status: 'draft',
});
mockGenerateText.mockResolvedValue({
text: '{"title": "New Title", "excerpt": "Summary text.", "slug": "Some Weird Slug!"}',
} as any);
const result = await tasks.analyzePost('post-1', 'en');
expect(result.success).toBe(true);
expect(result.slug).toMatch(/^[a-z0-9-]+$/);
});
it('handles AI response parse errors gracefully', async () => {
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: 'Test',
content: 'Content',
status: 'draft',
});
mockGenerateText.mockResolvedValue({
text: 'not valid json at all',
} as any);
const result = await tasks.analyzePost('post-1', 'en');
expect(result.success).toBe(false);
expect(result.error).toBe('Invalid response format from AI');
});
it('uses title model when configured', async () => {
deps.chatEngine.getSetting.mockResolvedValue('custom-model-id');
deps.providers.detectModelProvider.mockReturnValue('opencode');
deps.providers.isProviderKeySet.mockReturnValue(true);
deps.postEngine.getPost.mockResolvedValue({
id: 'post-1',
title: 'Test',
content: 'Content',
status: 'draft',
});
mockGenerateText.mockResolvedValue({
text: '{"title": "T", "excerpt": "E", "slug": "t"}',
} as any);
await tasks.analyzePost('post-1', 'en');
expect(deps.chatEngine.getSetting).toHaveBeenCalledWith('chat_title_model');
expect(deps.providers.resolveModel).toHaveBeenCalledWith('custom-model-id');
});
});