The minimalistic document archiving platform.
refactor(auto-tagging): decouple use case wiring (#1575)
Corentin Thomasset38c056dc3e49
diff --git a/apps/papra-server/src/modules/auto-tagging/auto-tagging.models.ts b/apps/papra-server/src/modules/auto-tagging/auto-tagging.models.tsindex c4dc5700..a7748fd2 100644--- a/apps/papra-server/src/modules/auto-tagging/auto-tagging.models.ts+++ b/apps/papra-server/src/modules/auto-tagging/auto-tagging.models.ts@@ -83,6 +83,11 @@ export function buildAutoTaggingSchema({});}+export type AutoTaggingActions = {+ tagIdsToAdd: string[];+ tagsToCreate: { name: string; description?: string }[];+};+export function getTagsActions({autoTaggingResponse,existingTags,@@ -91,10 +96,7 @@ export function getTagsActions({autoTaggingResponse: AutoTaggingResponse;existingTags: { name: string; id: string }[];maxTags: number;-}): {- tagIdsToAdd: string[];- tagsToCreate: { name: string; description?: string }[];-} {+}): AutoTaggingActions {const { existingTags: requestedExistingTagNames = [], newTags = [] } = autoTaggingResponse;const existingTagNamesMap = new Map(existingTags.map((tag) => [tag.name, tag.id]));diff --git a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.test.ts b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.test.tsindex 8a2999a7..a5d12ab0 100644--- a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.test.ts+++ b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.test.ts@@ -1,15 +1,23 @@import { describe, expect, test, vi } from 'vitest';+import * as v from 'valibot';import { createInMemoryDatabase } from '../app/database/database.test-utils';import { createTestEventServices } from '../app/events/events.test-utils';import type { AiServices } from '../ai/ai.services';import { overrideConfig } from '../config/config.test-utils';+import { createDocumentNotFoundError } from '../documents/documents.errors';import { createDocumentsRepository } from '../documents/documents.repository';import type { ResolveOrganizationSettingsUsecase } from '../organizations/organization-settings/organization-settings.usecases';import { createTestLogger } from '../shared/logger/logger.test-utils';import { createTagsRepository } from '../tags/tags.repository';import { documentsTagsTable, tagsTable } from '../tags/tags.table';-import { autoTagDocument } from './auto-tagging.usecases';+import { applyTagsToDocuments, createTag } from '../tags/tags.usecases';+import {+ buildApplyAutoTaggingActions,+ buildAutoTagDocument,+ buildPromptForAutoTagging,+} from './auto-tagging.usecases';import type { AutoTaggingResponse } from './auto-tagging.models';+import { buildAutoTaggingSystemPrompt, buildAutoTaggingUserPrompt } from './auto-tagging.models';function createTestResolveOrganizationSettings(autoTagging: Partial<{ isEnabled: boolean; canCreateNewTags: boolean; maxTags: number }> = {},@@ -30,7 +38,9 @@ function createTestResolveOrganizationSettings(}function createTestAiServices({ response }: { response: AutoTaggingResponse }) {- const generateStructuredData = vi.fn().mockResolvedValue(response);+ const generateStructuredData = vi+ .fn<AiServices['generateStructuredData']>()+ .mockResolvedValue(response);return {aiServices: { generateStructuredData } as AiServices,@@ -38,15 +48,26 @@ function createTestAiServices({ response }: { response: AutoTaggingResponse }) {};}-async function createTestDeps(seedOptions: Parameters<typeof createInMemoryDatabase>[0]) {+async function createTestDeps(+ seedOptions: Parameters<typeof createInMemoryDatabase>[0],+ config = overrideConfig(),+) {const { db } = await createInMemoryDatabase(seedOptions);+ const tagsRepository = createTagsRepository({ db });+ const eventServices = createTestEventServices();+ const { logger } = createTestLogger();return {db,documentsRepository: createDocumentsRepository({ db }),- tagsRepository: createTagsRepository({ db }),- eventServices: createTestEventServices(),- config: overrideConfig(),+ tagsRepository,+ eventServices,+ logger,+ applyAutoTaggingActions: buildApplyAutoTaggingActions({+ createTag: async (args) => createTag({ ...args, config, tagsRepository }),+ applyTagsToDocuments: async (args) =>+ applyTagsToDocuments({ ...args, tagsRepository, eventServices, logger }),+ }),};}@@ -61,139 +82,298 @@ const baseDocument = {content: 'Some document content',};+const financeTag = {+ id: 'tag_1',+ name: 'Finance',+ normalizedName: 'finance',+ color: '#000000',+ organizationId: 'org_1',+};++const householdTag = {+ id: 'tag_2',+ name: 'Household',+ normalizedName: 'household',+ color: '#000000',+ organizationId: 'org_1',+};++const documentIds = { documentId: 'doc_1', organizationId: 'org_1' };++const seed = {+ organizations: [{ id: 'org_1', name: 'Org 1' }],+ documents: [baseDocument],+ tags: [financeTag, householdTag],+};+describe('auto-tagging usecases', () => {- describe('autoTagDocument', () => {- test('when auto-tagging is disabled in the organization settings, the document is not tagged and the AI is not called', async () => {- const { logger } = createTestLogger();- const deps = await createTestDeps({- organizations: [{ id: 'org_1', name: 'Org 1' }],- documents: [baseDocument],- tags: [- {- id: 'tag_1',- name: 'Finance',- normalizedName: 'finance',- color: '#000000',- organizationId: 'org_1',- },- ],+ describe('buildPromptForAutoTagging', () => {+ test('analysis uses the existing prompts and accounting source and returns bounded tag actions without applying them', async () => {+ const { aiServices, generateStructuredData } = createTestAiServices({+ response: {+ existingTags: ['Finance', 'Finance', 'Unknown'],+ newTags: [{ name: 'Invoice', description: 'Invoices and receipts' }, { name: 'Other' }],+ },+ });+ const promptForAutoTagging = buildPromptForAutoTagging({ aiServices });+ const options = { existingTags: seed.tags, canCreateNewTags: true, maxTags: 2 };++ expect(+ await promptForAutoTagging({+ document: baseDocument,+ modelId: 'openai://gpt-4',+ ...options,+ }),+ ).toEqual({+ tagIdsToAdd: ['tag_1'],+ tagsToCreate: [{ name: 'Invoice', description: 'Invoices and receipts' }],});+ expect(generateStructuredData).toHaveBeenCalledTimes(1);+ const [request] = generateStructuredData.mock.calls[0]!;+ expect(request).toMatchObject({+ organizationId: 'org_1',+ modelId: 'openai://gpt-4',+ source: 'auto-tagging',+ systemPrompt: buildAutoTaggingSystemPrompt(options),+ userPrompt: buildAutoTaggingUserPrompt({ document: baseDocument }),+ });+ expect(+ v.safeParse(request.schema!, { existingTags: ['Finance'], newTags: [] }).success,+ ).toEqual(true);+ expect(+ v.safeParse(request.schema!, { existingTags: ['Unknown'], newTags: [] }).success,+ ).toEqual(false);+ });+ });++ describe('buildAutoTagDocument', () => {+ test('when auto-tagging is disabled in the organization settings, the document is not tagged and the AI is not called', async () => {+ const deps = await createTestDeps(seed);const { aiServices, generateStructuredData } = createTestAiServices({response: { existingTags: ['Finance'] },});-- await autoTagDocument({+ const autoTagDocument = buildAutoTagDocument({...deps,- aiServices,- logger,- documentId: 'doc_1',- organizationId: 'org_1',+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),resolveOrganizationSettings: createTestResolveOrganizationSettings({ isEnabled: false }),});+ await autoTagDocument(documentIds);+expect(generateStructuredData).not.toHaveBeenCalled();- expect(await deps.db.select().from(documentsTagsTable)).to.eql([]);+ expect(await deps.db.select().from(documentsTagsTable)).toEqual([]);});test('when the organization has no existing tags and cannot create new ones, the document is not tagged and the AI is not called', async () => {- const { logger } = createTestLogger();const deps = await createTestDeps({- organizations: [{ id: 'org_1', name: 'Org 1' }],- documents: [baseDocument],+ organizations: seed.organizations,+ documents: seed.documents,});const { aiServices, generateStructuredData } = createTestAiServices({response: { existingTags: [] },});-- await autoTagDocument({+ const autoTagDocument = buildAutoTagDocument({...deps,- aiServices,- logger,- documentId: 'doc_1',- organizationId: 'org_1',+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),resolveOrganizationSettings: createTestResolveOrganizationSettings({isEnabled: true,canCreateNewTags: false,}),});+ await autoTagDocument(documentIds);+expect(generateStructuredData).not.toHaveBeenCalled();- expect(await deps.db.select().from(documentsTagsTable)).to.eql([]);+ expect(await deps.db.select().from(documentsTagsTable)).toEqual([]);});test('when the AI selects existing tags, they are applied to the document', async () => {- const { logger } = createTestLogger();- const deps = await createTestDeps({- organizations: [{ id: 'org_1', name: 'Org 1' }],- documents: [baseDocument],- tags: [- {- id: 'tag_1',- name: 'Finance',- normalizedName: 'finance',- color: '#000000',- organizationId: 'org_1',- },- {- id: 'tag_2',- name: 'Household',- normalizedName: 'household',- color: '#000000',- organizationId: 'org_1',- },- ],- });+ const deps = await createTestDeps(seed);const { aiServices } = createTestAiServices({ response: { existingTags: ['Finance'] } });-- await autoTagDocument({+ const autoTagDocument = buildAutoTagDocument({...deps,- aiServices,- logger,- documentId: 'doc_1',- organizationId: 'org_1',- resolveOrganizationSettings: createTestResolveOrganizationSettings({- isEnabled: true,- canCreateNewTags: false,- }),+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),+ resolveOrganizationSettings: createTestResolveOrganizationSettings(),});- expect(await deps.db.select().from(documentsTagsTable)).to.eql([+ await autoTagDocument(documentIds);++ expect(await deps.db.select().from(documentsTagsTable)).toEqual([{ documentId: 'doc_1', tagId: 'tag_1' },]);});test('when the AI proposes new tags, they are created and applied to the document', async () => {- const { logger } = createTestLogger();const deps = await createTestDeps({- organizations: [{ id: 'org_1', name: 'Org 1' }],- documents: [baseDocument],+ organizations: seed.organizations,+ documents: seed.documents,});const { aiServices } = createTestAiServices({response: { newTags: [{ name: 'Invoice', description: 'Invoices and receipts' }] },});-- await autoTagDocument({+ const autoTagDocument = buildAutoTagDocument({...deps,- aiServices,- logger,- documentId: 'doc_1',- organizationId: 'org_1',+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),resolveOrganizationSettings: createTestResolveOrganizationSettings({- isEnabled: true,canCreateNewTags: true,}),});+ await autoTagDocument(documentIds);+const createdTags = await deps.db.select().from(tagsTable);- expect(createdTags.length).to.eql(1);+ expect(createdTags.length).toEqual(1);expect(createdTags[0]).toMatchObject({name: 'Invoice',description: 'Invoices and receipts',+ color: '#CCCCCC',+ organizationId: 'org_1',});-- expect(await deps.db.select().from(documentsTagsTable)).to.eql([+ expect(await deps.db.select().from(documentsTagsTable)).toEqual([{ documentId: 'doc_1', tagId: createdTags[0]!.id },]);});++ test('the organization model and tag settings are passed to analysis and its actions are passed to application', async () => {+ const deps = await createTestDeps(seed);+ const actions = { tagIdsToAdd: ['tag_1'], tagsToCreate: [{ name: 'Invoice' }] };+ const promptForAutoTagging = vi+ .fn<ReturnType<typeof buildPromptForAutoTagging>>()+ .mockResolvedValue(actions);+ const applyAutoTaggingActions = vi+ .fn<ReturnType<typeof buildApplyAutoTaggingActions>>()+ .mockResolvedValue();+ const autoTagDocument = buildAutoTagDocument({+ ...deps,+ promptForAutoTagging,+ applyAutoTaggingActions,+ resolveOrganizationSettings: createTestResolveOrganizationSettings({+ maxTags: 2,+ canCreateNewTags: true,+ }),+ });++ await autoTagDocument(documentIds);++ expect(promptForAutoTagging).toHaveBeenCalledTimes(1);+ const [request] = promptForAutoTagging.mock.calls[0]!;+ expect(request).toMatchObject({+ document: baseDocument,+ canCreateNewTags: true,+ maxTags: 2,+ modelId: 'gpt-4',+ });+ expect(request.existingTags.toSorted((a, b) => a.id.localeCompare(b.id))).toMatchObject(+ seed.tags,+ );+ expect(applyAutoTaggingActions).toHaveBeenCalledExactlyOnceWith({+ ...documentIds,+ ...actions,+ });+ });++ test('a missing document fails before analysis', async () => {+ const deps = await createTestDeps({ organizations: seed.organizations, tags: seed.tags });+ const promptForAutoTagging = vi.fn<ReturnType<typeof buildPromptForAutoTagging>>();+ const autoTagDocument = buildAutoTagDocument({+ ...deps,+ promptForAutoTagging,+ resolveOrganizationSettings: createTestResolveOrganizationSettings(),+ });++ await expect(autoTagDocument(documentIds)).rejects.toEqual(createDocumentNotFoundError());+ expect(promptForAutoTagging).not.toHaveBeenCalled();+ });++ test('an inference failure is propagated without applying tags', async () => {+ const deps = await createTestDeps(seed);+ const error = new Error('Inference failed');+ const { aiServices, generateStructuredData } = createTestAiServices({ response: {} });+ generateStructuredData.mockRejectedValue(error);+ const applyAutoTaggingActions = vi.fn<ReturnType<typeof buildApplyAutoTaggingActions>>();+ const autoTagDocument = buildAutoTagDocument({+ ...deps,+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),+ applyAutoTaggingActions,+ resolveOrganizationSettings: createTestResolveOrganizationSettings(),+ });++ await expect(autoTagDocument(documentIds)).rejects.toEqual(error);+ expect(applyAutoTaggingActions).not.toHaveBeenCalled();+ });+ });++ describe('buildApplyAutoTaggingActions', () => {+ test('existing and successfully created tags are applied despite a creation failure, preserving current tags and emitting the domain event', async () => {+ const deps = await createTestDeps(seed);+ await deps.db.insert(documentsTagsTable).values({ documentId: 'doc_1', tagId: 'tag_2' });++ await deps.applyAutoTaggingActions({+ ...documentIds,+ tagIdsToAdd: ['tag_1'],+ tagsToCreate: [+ { name: 'Finance' },+ { name: 'Invoice', description: 'Invoices and receipts' },+ ],+ });++ const tags = await deps.db.select().from(tagsTable);+ expect(tags.length).toEqual(3);+ const invoiceTag = tags.find((tag) => tag.name === 'Invoice')!;+ expect(invoiceTag).toMatchObject({+ description: 'Invoices and receipts',+ color: '#CCCCCC',+ organizationId: 'org_1',+ });+ const documentTags = await deps.db.select().from(documentsTagsTable);+ expect(documentTags.map(({ tagId }) => tagId).sort()).toEqual(+ ['tag_1', 'tag_2', invoiceTag.id].sort(),+ );+ expect(deps.eventServices.getEmittedEvents()).toEqual([+ {+ eventName: 'document.tags.changed',+ payload: {+ organizationId: 'org_1',+ userId: undefined,+ addedPairs: [+ { documentId: 'doc_1', tagId: 'tag_1', tagName: 'Finance' },+ { documentId: 'doc_1', tagId: invoiceTag.id, tagName: 'Invoice' },+ ],+ removedPairs: [],+ },+ },+ ]);+ });++ test('the organization tag limit prevents new tags without preventing existing tags from being applied', async () => {+ const deps = await createTestDeps(+ seed,+ overrideConfig({ tags: { maxTagsPerOrganization: 2 } }),+ );++ await deps.applyAutoTaggingActions({+ ...documentIds,+ tagIdsToAdd: ['tag_1'],+ tagsToCreate: [{ name: 'Invoice' }],+ });++ expect((await deps.db.select().from(tagsTable)).length).toEqual(2);+ expect(await deps.db.select().from(documentsTagsTable)).toEqual([+ { documentId: 'doc_1', tagId: 'tag_1' },+ ]);+ });++ test('empty actions leave existing tags untouched and emit no event', async () => {+ const deps = await createTestDeps(seed);+ await deps.db.insert(documentsTagsTable).values({ documentId: 'doc_1', tagId: 'tag_2' });++ await deps.applyAutoTaggingActions({ ...documentIds, tagIdsToAdd: [], tagsToCreate: [] });++ expect((await deps.db.select().from(tagsTable)).length).toEqual(2);+ expect(await deps.db.select().from(documentsTagsTable)).toEqual([+ { documentId: 'doc_1', tagId: 'tag_2' },+ ]);+ expect(deps.eventServices.getEmittedEvents()).toEqual([]);+ });});});diff --git a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.tsindex fe9163f4..a7472dcd 100644--- a/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts+++ b/apps/papra-server/src/modules/auto-tagging/auto-tagging.usecases.ts@@ -1,15 +1,17 @@import type { AiServices } from '../ai/ai.services';import type { TagsRepository } from '../tags/tags.repository';+import type { Tag } from '../tags/tags.types';import type { ResolveOrganizationSettingsUsecase } from '../organizations/organization-settings/organization-settings.usecases';import type { DocumentsRepository } from '../documents/documents.repository';import { createDocumentNotFoundError } from '../documents/documents.errors';import type { Logger } from '@crowlog/logger';import { createLogger } from '../shared/logger/logger';-import type { Tag } from '../tags/tags.types';-import { applyTagsToDocuments, createTag } from '../tags/tags.usecases';+import type {+ applyTagsToDocuments as applyTagsToDocumentsUsecase,+ createTag as createTagUsecase,+} from '../tags/tags.usecases';import pLimit from 'p-limit';-import type { Config } from '../config/config.types';-import type { EventServices } from '../app/events/events.services';+import type { AutoTaggingActions } from './auto-tagging.models';import {buildAutoTaggingSchema,buildAutoTaggingSystemPrompt,@@ -18,137 +20,152 @@ import {} from './auto-tagging.models';import { ensureModelId } from '../ai/ai.models';-export async function promptForAutoTagging({- aiServices,- document,- existingTags,- canCreateNewTags,- maxTags,- modelId,+export function buildPromptForAutoTagging({ aiServices }: { aiServices: AiServices }) {+ return async ({+ document,+ existingTags,+ canCreateNewTags,+ maxTags,+ modelId,+ }: {+ document: { content: string; name: string; organizationId: string };+ existingTags: { id: string; name: string; description?: string | null }[];+ canCreateNewTags: boolean;+ maxTags: number;+ modelId: string;+ }): Promise<AutoTaggingActions> => {+ const autoTaggingResponse = await aiServices.generateStructuredData({+ modelId,+ organizationId: document.organizationId,+ source: 'auto-tagging',+ schema: buildAutoTaggingSchema({ existingTags, canCreateNewTags }),+ systemPrompt: buildAutoTaggingSystemPrompt({ existingTags, canCreateNewTags, maxTags }),+ userPrompt: buildAutoTaggingUserPrompt({ document }),+ });++ return getTagsActions({+ autoTaggingResponse,+ existingTags,+ maxTags,+ });+ };+}++export function buildApplyAutoTaggingActions({+ createTag,+ applyTagsToDocuments,}: {- aiServices: AiServices;- document: { content: string; name: string; organizationId: string };- existingTags: { id: string; name: string; description?: string | null }[];- canCreateNewTags: boolean;- maxTags: number;- modelId: string;+ createTag: (+ args: Pick<+ Parameters<typeof createTagUsecase>[0],+ 'organizationId' | 'name' | 'description' | 'color'+ >,+ ) => ReturnType<typeof createTagUsecase>;+ applyTagsToDocuments: (+ args: Pick<+ Parameters<typeof applyTagsToDocumentsUsecase>[0],+ 'documentIds' | 'addTagIds' | 'removeTagIds' | 'organizationId' | 'userId'+ >,+ ) => ReturnType<typeof applyTagsToDocumentsUsecase>;}) {- const autoTaggingResponse = await aiServices.generateStructuredData({- modelId,- organizationId: document.organizationId,- source: 'auto-tagging',- schema: buildAutoTaggingSchema({ existingTags, canCreateNewTags }),- systemPrompt: buildAutoTaggingSystemPrompt({ existingTags, canCreateNewTags, maxTags }),- userPrompt: buildAutoTaggingUserPrompt({ document }),- });+ return async ({+ documentId,+ organizationId,+ tagIdsToAdd,+ tagsToCreate,+ }: AutoTaggingActions & { documentId: string; organizationId: string }) => {+ const limit = pLimit(5);- const { tagIdsToAdd, tagsToCreate } = getTagsActions({- autoTaggingResponse,- existingTags,- maxTags,- });+ // TODO: introduce a createTags method with a new checkIfOrganizationCanCreateNewTag+ const createdTagsResults = await Promise.allSettled(+ tagsToCreate.map(async ({ name, description }) =>+ limit(async () =>+ createTag({+ organizationId,+ color: '#CCCCCC',+ name,+ description,+ }),+ ),+ ),+ );++ const createdTags = createdTagsResults+ .filter(+ (result): result is PromiseFulfilledResult<{ tag: Tag }> => result.status === 'fulfilled',+ )+ .map((result) => result.value.tag);- return { tagIdsToAdd, tagsToCreate };+ await applyTagsToDocuments({+ documentIds: [documentId],+ addTagIds: [...tagIdsToAdd, ...createdTags.map((tag) => tag.id)],+ removeTagIds: [],+ organizationId,+ userId: undefined,+ });+ };}-export async function autoTagDocument({- aiServices,- documentId,- organizationId,+export function buildAutoTagDocument({+ promptForAutoTagging,+ applyAutoTaggingActions,documentsRepository,tagsRepository,resolveOrganizationSettings,- eventServices,- config,logger = createLogger({ namespace: 'autoTagDocument' }),}: {- aiServices: AiServices;- documentId: string;- organizationId: string;- documentsRepository: DocumentsRepository;- tagsRepository: TagsRepository;+ promptForAutoTagging: ReturnType<typeof buildPromptForAutoTagging>;+ applyAutoTaggingActions: ReturnType<typeof buildApplyAutoTaggingActions>;+ documentsRepository: Pick<DocumentsRepository, 'getDocumentById'>;+ tagsRepository: Pick<TagsRepository, 'getOrganizationTags'>;resolveOrganizationSettings: ResolveOrganizationSettingsUsecase;- eventServices: EventServices;- config: Config;logger?: Logger;}) {- const [{ document }, { tags: existingTags }, { organizationSettings }] = await Promise.all([- documentsRepository.getDocumentById({ documentId, organizationId }),- tagsRepository.getOrganizationTags({ organizationId }),- resolveOrganizationSettings({ organizationId }),- ]);+ return async ({ documentId, organizationId }: { documentId: string; organizationId: string }) => {+ const [{ document }, { tags: existingTags }, { organizationSettings }] = await Promise.all([+ documentsRepository.getDocumentById({ documentId, organizationId }),+ tagsRepository.getOrganizationTags({ organizationId }),+ resolveOrganizationSettings({ organizationId }),+ ]);- const orgHasNoExistingTags = existingTags.length === 0;- const {- canCreateNewTags,- maxTags,- isEnabled: isAutoTaggingEnabled,- } = organizationSettings.ai.autoTagging;+ const orgHasNoExistingTags = existingTags.length === 0;+ const {+ canCreateNewTags,+ maxTags,+ isEnabled: isAutoTaggingEnabled,+ } = organizationSettings.ai.autoTagging;- if (orgHasNoExistingTags && !canCreateNewTags) {- logger.info(- { documentId, organizationId },- 'No tags available and cannot create new tags. Skipping auto-tagging.',- );- return;- }+ if (orgHasNoExistingTags && !canCreateNewTags) {+ logger.info(+ { documentId, organizationId },+ 'No tags available and cannot create new tags. Skipping auto-tagging.',+ );+ return;+ }- if (!isAutoTaggingEnabled) {- logger.info(- { documentId, organizationId },- 'Auto-tagging is disabled in organization settings. Skipping auto-tagging.',- );- return;- }+ if (!isAutoTaggingEnabled) {+ logger.info(+ { documentId, organizationId },+ 'Auto-tagging is disabled in organization settings. Skipping auto-tagging.',+ );+ return;+ }- if (!document) {- throw createDocumentNotFoundError();- }+ if (!document) {+ throw createDocumentNotFoundError();+ }- const startedAt = Date.now();- const { tagIdsToAdd, tagsToCreate } = await promptForAutoTagging({- aiServices,- document,- existingTags,- canCreateNewTags,- maxTags,- modelId: ensureModelId(organizationSettings.ai.autoTagging.modelId),- });- const durationMs = Date.now() - startedAt;- logger.info({ documentId, organizationId, durationMs }, 'Auto-tagging completed');+ const startedAt = Date.now();+ const actions = await promptForAutoTagging({+ document,+ existingTags,+ canCreateNewTags,+ maxTags,+ modelId: ensureModelId(organizationSettings.ai.autoTagging.modelId),+ });+ const durationMs = Date.now() - startedAt;+ logger.info({ documentId, organizationId, durationMs }, 'Auto-tagging completed');- const limit = pLimit(5);-- // TODO: introduce a createTags method with a new checkIfOrganizationCanCreateNewTag- const createdTagsResults = await Promise.allSettled(- tagsToCreate.map(async ({ name, description }) =>- limit(async () =>- createTag({- organizationId,- color: '#CCCCCC',- name,- description,- config,- tagsRepository,- }),- ),- ),- );-- const createdTags = createdTagsResults- .filter(- (result): result is PromiseFulfilledResult<{ tag: Tag }> => result.status === 'fulfilled',- )- .map((result) => result.value.tag);-- await applyTagsToDocuments({- documentIds: [documentId],- addTagIds: [...tagIdsToAdd, ...createdTags.map((tag) => tag.id)],- removeTagIds: [],- organizationId,- userId: undefined,- tagsRepository,- eventServices,- logger,- });+ await applyAutoTaggingActions({ documentId, organizationId, ...actions });+ };}diff --git a/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts b/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.tsindex 59d41ac4..18851c2d 100644--- a/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts+++ b/apps/papra-server/src/modules/auto-tagging/int-tests/auto-tagging.int.test-suites.ts@@ -1,7 +1,7 @@import { expect, test } from 'vitest';import type { Config } from '../../config/config.types';import { createAiServices } from '../../ai/ai.services';-import { promptForAutoTagging } from '../auto-tagging.usecases';+import { buildPromptForAutoTagging } from '../auto-tagging.usecases';import type { Logger } from '@crowlog/logger';import { createAiCreditsRepository } from '../../ai-credits/ai-credits.repository';import { createInMemoryDatabase } from '../../app/database/database.test-utils';@@ -51,11 +51,12 @@ export async function runAutoTaggingTestSuite({planEntitlementDefinitionRegistry: createPlanEntitlementDefinitionRegistry({ config }),});+ const promptForAutoTagging = buildPromptForAutoTagging({ aiServices });+test('auto-tagging should correctly identify tags for a document', { timeout }, async () => {const { tagIdsToAdd, tagsToCreate } = await promptForAutoTagging({existingTags,modelId,- aiServices,canCreateNewTags: false,document,maxTags: 3,diff --git a/apps/papra-server/src/modules/auto-tagging/tasks/auto-tag-document.task.ts b/apps/papra-server/src/modules/auto-tagging/tasks/auto-tag-document.task.tsindex c9039981..a201bb7e 100644--- a/apps/papra-server/src/modules/auto-tagging/tasks/auto-tag-document.task.ts+++ b/apps/papra-server/src/modules/auto-tagging/tasks/auto-tag-document.task.ts@@ -1,7 +1,13 @@import type { Database } from '../../app/database/database.types';import type { TaskServices } from '../../tasks/tasks.services';import { createTagsRepository } from '../../tags/tags.repository';-import { autoTagDocument } from '../../auto-tagging/auto-tagging.usecases';+import {+ buildApplyAutoTaggingActions,+ buildAutoTagDocument,+ buildPromptForAutoTagging,+} from '../../auto-tagging/auto-tagging.usecases';+import { applyTagsToDocuments, createTag } from '../../tags/tags.usecases';+import { createLogger } from '../../shared/logger/logger';import { createResolveOrganizationSettingsUsecase } from '../../organizations/organization-settings/organization-settings.usecases';import { createOrganizationSettingsRepository } from '../../organizations/organization-settings/organization-settings.repository';import type { Config } from '../../config/config.types';@@ -41,19 +47,24 @@ export async function registerAutoTagDocumentTask({organizationId: string;};- await autoTagDocument({- documentId,- organizationId,- aiServices,+ const logger = createLogger({ namespace: 'autoTagDocument' });+ const autoTagDocument = buildAutoTagDocument({documentsRepository,tagsRepository,- config,+ logger,+ promptForAutoTagging: buildPromptForAutoTagging({ aiServices }),+ applyAutoTaggingActions: buildApplyAutoTaggingActions({+ createTag: async (args) => createTag({ ...args, config, tagsRepository }),+ applyTagsToDocuments: async (args) =>+ applyTagsToDocuments({ ...args, tagsRepository, eventServices, logger }),+ }),resolveOrganizationSettings: createResolveOrganizationSettingsUsecase({config,organizationSettingsRepository,}),- eventServices,});++ await autoTagDocument({ documentId, organizationId });},});}