Skip to main content

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

  1. Configure AI — An administrator calls PUT /api/ai-config with CreateOrUpdateAiConfigDto, 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.
  2. Read the current configurationGET /api/ai-config supplies the values shown on the settings screen.
  3. 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-flex with a body shaped like {"prompt": "..."}.
  4. Service processing — The generate() function proceeds as follows:
    • Load the active ai_config for 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.
  5. 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.

MethodRouteHandlerGuard / Policy
GET/api/ai-configct.getConfigJwtLoginAuth + read line-oa
PUT/api/ai-configct.upsertConfigJwtLoginAuth + update line-oa
POST/api/ai-config/generate-flexct.generateFlexJwtLoginAuth + 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 controlJwtLoginAuth only. No lineOaId is required in the token and there is no ModuleGate wrapper. The policy metadata references PolicyModuleLineOa but is not enforced.
  • Tablesai_config and line_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)