refactor(ai): move Ask AI domain logic out of the provider adapter - #666
refactor(ai): move Ask AI domain logic out of the provider adapter#666Reversean wants to merge 3 commits into
Conversation
The Ask AI prompt assembly, the model instruction and the event serialisation lived in src/integrations/vercel-ai/, a directory meant for adapters to external services, even though none of them referenced the provider. Replacing the provider therefore dragged the domain along, and anything applied around the model call could only be reused by importing from another adapter's internals. The domain now lives in src/services/askAi/ and VercelAIApi is reduced to a transport taking a system/prompt pair. Orchestration sits in AIService, which already owned the event lookup. Anything applied to the model's input or output can now sit above the transport, where a provider swap cannot silently drop it. Behaviour is unchanged: AIService.generateSuggestion keeps its signature and its only caller is untouched.
71629b8 to
16fd7a6
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the “Ask AI” feature by moving prompt/instruction assembly out of the Vercel AI integration adapter and into a dedicated domain area under src/services/askAi/, leaving src/integrations/vercel-ai/ as a thin transport layer.
Changes:
- Moved Ask AI domain prompt/instruction modules into
src/services/askAi/and updatedAIService.generateSuggestionto assemble{ system, prompt }there. - Simplified the Vercel adapter API to
complete({ system, prompt }): Promise<string>and added unit coverage for it. - Added unit tests to validate
AIService.generateSuggestionorchestration and the Vercel transport behavior; bumped package version.
Reviewed changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/services/ai.ts |
Moves orchestration into AIService and calls the adapter via complete({ system, prompt }). |
src/integrations/vercel-ai/index.ts |
Refactors the adapter to a transport-style complete method that forwards to generateText. |
src/services/askAi/instructions/cto.ts |
Adds the CTO system instruction in the new domain location. |
src/services/askAi/inputs/eventSolving.ts |
Adds event prompt serialization in the new domain location. |
test/services/askAi.test.ts |
Adds unit tests asserting AIService.generateSuggestion calls the transport with the assembled system/prompt. |
test/integrations/vercel-ai.test.ts |
Adds unit tests asserting adapter forwards to generateText and returns text. |
package.json |
Bumps version to 1.5.10. |
Comments suppressed due to low confidence (1)
src/integrations/vercel-ai/index.ts:39
- Continuation of the indentation issue: this docblock/method body is indented differently from the surrounding codebase (2-space blocks). Reformatting here will keep the file consistent and avoid noisy diffs from auto-formatting.
* Send a system/prompt pair to the model and return the generated text
*
* @param {CompletionParams} params - system instruction and prompt to complete
* @returns {Promise<string>} text generated by the model
* @todo add defence against invalid prompt injection
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const result = await aiService.generateSuggestion(eventsFactoryWithPayload(), testEventId, testOriginalEventId); | ||
|
|
||
| expect(vercelAIApi.complete).toHaveBeenCalledWith({ | ||
| system: ctoInstruction, | ||
| prompt: eventSolvingInput(testPayload), |
There was a problem hiding this comment.
Added. Verified it fails if the two ids are swapped.
| } | ||
|
|
There was a problem hiding this comment.
Fixed. Though nothing would have reformatted it — no prettier, and the base indent rule reports 0 here (only @typescript-eslint/indent flags it). Enabling that repo-wide is a separate PR.
The suggestion test mocked the events factory but never checked how it was called, so swapping eventId and originalEventId in AIService.generateSuggestion kept it green. That is the one piece of behaviour this refactor moved out of the adapter, so it is the piece worth pinning. Also annotate the factory stub's return type, which the linter flagged.
The class body was indented with four spaces while .editorconfig and the rest of the repo use two. Whitespace only, no code change. Fixing it at the root of the stack means the branches based on this one pick it up on rebase instead of adding more four-space code on top. eslint-config-codex does not catch this: the base indent rule reports nothing here, only @typescript-eslint/indent does. Enabling it repo-wide is a separate change - six other files, all auto-fixable.
src/integrations/holds adapters to external services. The Ask AI prompt assembly, themodel instruction and the event serialisation live there today, inside the Vercel adapter,
even though none of them import anything from the provider. They are domain code that ended
up in an adapter directory.
This has two consequences. Replacing the provider drags the domain along with it, and any
policy applied around the model call can only be reused by importing from another adapter's
internals.
Move the domain modules to
src/services/askAi/and reduceVercelAIApito a transport thattakes a system/prompt pair and returns text:
Orchestration moves to
AIService, which already owns the event lookup.instructions/cto.tsandinputs/eventSolving.tsare moved unchanged.This also places anything applied to the model's input or output above the transport, so
swapping the provider cannot silently drop it.
No behaviour change:
AIService.generateSuggestionkeeps its signature and its only caller,src/resolvers/event.js, is untouched.