AI Configuration & AI Flex Generation
Overview
The AI module does two things: it stores the AI provider configuration per LINE OA or per organization in the ai_config table, and it exposes AI content generation. Today the production use case is generating a Flex Message from a plain-language instruction, which saves the team from assembling message cards by hand.
Only Gemini is implemented as a provider so far, though the code is structured so additional providers can be added. The Workflow module also calls AI to draft flows, but it does so through its own path in workflow/ai.go.
All routes in this module sit on the public group behind JwtLoginAuth, so they can be called right after login without first selecting a LINE OA.
Business Flow
- Configure AI — An administrator calls
PUT /api/ai-configwithCreateOrUpdateAiConfigDto, which carries the provider, model, API key, and generation parameters. The operation is an upsert: it creates the record if none exists and updates it otherwise. - Read the current configuration —
GET /api/ai-configsupplies the values shown on the settings screen. - Ask AI for a card — While composing a rich message, the user describes the card they want and the client calls
POST /api/ai-config/generate-flexwith a body shaped like{"prompt": "..."}. - Service processing — The
generate()function proceeds as follows:- Load the active
ai_configfor the current LINE OA or organization. - If the provider is Gemini, issue an HTTP request to the Gemini API.
- Other providers are not implemented yet.
- Options the caller omits fall back to defaults. The Go implementation uses pointer fields to distinguish "not supplied" from "supplied as zero", matching the semantics of the original TypeScript
??and||operators.
- Load the active
- Return the result — The Flex Message JSON is returned to cms-web, which drops it into the rich message editor.
Key Files & Functions
The code lives in internal/modules/ai/, made up of controller.go, service.go, and dto.go.
| Method | Route | Handler | Guard / Policy |
|---|---|---|---|
| GET | /api/ai-config | ct.getConfig | JwtLoginAuth + read line-oa |
| PUT | /api/ai-config | ct.upsertConfig | JwtLoginAuth + update line-oa |
| POST | /api/ai-config/generate-flex | ct.generateFlex | JwtLoginAuth + read line-oa |
All three routes are registered on the public group; the _ = authed line in the code confirms the authed group is deliberately unused here.
Two structures are worth knowing: AiGenerateOptions, which uses pointer fields to model TypeScript's optional fields, and Service.generate(), the single place where provider dispatch logic lives.
Connections to Other Services
- Access control —
JwtLoginAuthonly. NolineOaIdis required in the token and there is noModuleGatewrapper. The policy metadata referencesPolicyModuleLineOabut is not enforced. - Tables —
ai_configandline_oa - External services — Gemini API over HTTP
- Related modules — Rich Message (the destination for generated Flex content), Workflow (AI-assisted flow drafting), and the Knowledge Base (the bot's source of knowledge)