Skip to content

refactor(builder): move ai feature data access into business services - #1096

Open
realcodesiman wants to merge 1 commit into
mainfrom
refactor/data-access-builder-ai
Open

refactor(builder): move ai feature data access into business services#1096
realcodesiman wants to merge 1 commit into
mainfrom
refactor/data-access-builder-ai

Conversation

@realcodesiman

Copy link
Copy Markdown
Contributor

Summary

  • Removes every direct db.* call from the builder AI surface (Claude/DeepSeek/Gemini/OpenAI connect actions and queries, ai-files, ai-functions, ai-mcp-servers, ai-triggers, personas) per .agents/rules/data-access.md, so the logic is reusable from the worker and public APIs and mockable at one boundary.
  • One of seven parallel scope PRs following feat(contacts): cover full public API for MCP, move logic into business services #1093 (contacts); all are based on main and only append to the shared barrels.

Changes

  • packages/business/src/integration-{claude,deepseek,gemini,openai}/service.ts: new connect() mirroring integrationOpenRouterService.connect (audit stays unconditional as before; OpenAI records audit with an explicit workspaceId because authActionClient has no workspace in the audit context).
  • New packages/business/src/ai-file/ (create, delete, listWithEmbeddingStatus) and packages/business/src/ai-trigger/ (list, create, duplicate); aiFunctionService.list; integrationMessengerRepository.listPersonasByWorkspaceId.
  • Builder actions/queries are thin wrappers; AI cache invalidation stays in the builder because business must not import @chatbotx.io/ai.
  • Tests moved to the service boundary (packages/business/__tests__/{integration-llm-connect,ai-file,ai-trigger}.service.test.ts, apps/builder/__tests__/{integration-llm-connect-actions,ai-triggers-actions}.test.ts).

Notes for reviewers

  • The four connect() update paths now run the UPDATE outside a transaction (the insert path is still transactional); the old action wrapped the read+update in one transaction. Idempotent by existing.id, so no data-loss window, but flagging the shape change.
  • Pre-existing bug preserved verbatim (not fixed here): aiFileService.delete deletes embeddings by aiEmbeddingModel.id instead of aiFileId (harmless via FK cascade).
  • Follow-ups listed in the plan: integration-*/actions/update.action.ts and ai-triggers/actions/{update,delete}.action.ts still use db; connectOpenAIAction uses authActionClient and does not verify workspace membership.

Test plan

  • pnpm --filter @chatbotx.io/database check-types && test
  • pnpm --filter @chatbotx.io/business check-types && test (only the pre-existing ads-conversion-rule.service.test.ts failures remain, also red on main)
  • pnpm --filter builder check-types && test
  • pnpm lint
  • Manual: connect + reconnect each provider in Settings → Integrations; upload/delete a Knowledge file; create/duplicate an AI trigger; Set Persona picker lists personas

Removes direct db usage from the builder AI surface (Claude, DeepSeek,
Gemini, OpenAI connect actions and queries, ai-files, ai-functions,
ai-mcp-servers, ai-triggers, personas) per .agents/rules/data-access.md.

- add connect() to the four LLM provider services (mirrors openrouter)
- add aiFileService (create/delete/listWithEmbeddingStatus) and
  aiTriggerService (list/create/duplicate)
- add aiFunctionService.list and
  integrationMessengerRepository.listPersonasByWorkspaceId
- actions/queries become thin wrappers; cache invalidation stays in the
  builder because business must not import @chatbotx.io/ai
- OpenAI connect audits via dispatchAuditRecord with an explicit
  workspaceId (authActionClient has no workspace in the audit context)
@github-actions github-actions Bot added the improvement Refactor or performance improvement label Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Refactor or performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant