Skip to content

Add Piyo Chat feature for textbook sections#9847

Open
komagata wants to merge 1 commit intofeature/textbookfrom
feature/textbook-piyo-chat
Open

Add Piyo Chat feature for textbook sections#9847
komagata wants to merge 1 commit intofeature/textbookfrom
feature/textbook-piyo-chat

Conversation

@komagata
Copy link
Copy Markdown
Member

概要

textbookのセクション読書中にピヨルドとAIチャットができる機能を追加しました。

デモ: セクション右下のピヨルド🐥をクリック → チャットパネルが開く → セクションの内容について質問できる

実装内容

🗃 データベース

  • piyo_chat_messages テーブル: user × section × chat履歴
  • PiyoChatMessage モデル(role: user/assistant, content, etc.)

🤖 バックエンド

  • PiyoChatService: RubyLLM + セクション文脈でAI応答
    • セクションタイトル・章・教科書名・学習目標・重要用語をAIプロンプトに含める
    • 会話履歴(直近10件)も含めて文脈を保持
  • API Controller: チャット履歴取得 + メッセージ送信

🎨 フロントエンド

  • piyo_chat_controller.js (Stimulus): チャットパネルUI
    • メッセージ履歴表示、送信、ローディング状態
    • Enter送信、Shift+Enterで改行
    • 未読バッジ機能
  • 既存ピヨルド強化: クリック→チャットパネル開閉に変更

🔗 統合

  • セクション読書画面 (/textbooks/:id/chapters/:cid/sections/:sid) でのみ有効
  • セクションごとに会話履歴が分離
  • エラーハンドリング、レスポンシブ対応

スクリーンショット

(レビューアプリで確認可能)

テスト

  • モデルvalidation テスト
  • サービス層 テスト(RubyLLM mock)
  • フィクスチャ完備

全テスト成功、RuboCop clean 確認済み。

次のステップ

この基盤を使って以下の機能を順次追加予定:

  1. テキスト選択→質問: 本文選択時「ピヨルドに聞く」ボタン
  2. ミニクイズ: セクション末尾でAI生成クイズ
  3. つまずき検知: 読了パターンから「ここ難しいよね」提案

@komagata レビューお願いします! 🐾

## Overview
Enables AI-powered chat with Piyo mascot while reading textbook sections.
Users can ask questions about the current section and get contextual responses.

## Implementation

### Database
- piyo_chat_messages table: stores chat history per user/section
- PiyoChatMessage model with validations

### Backend
- PiyoChatService: contextual AI responses using RubyLLM + section info
- API::Textbooks::PiyoChatMessagesController: chat history + message creation

### Frontend
- piyo_chat_controller.js: chat panel UI with message rendering
- Enhanced piyo companion: click to toggle chat panel
- CSS for chat panel and message bubbles

### Integration
- Extends existing piyo companion partial with section-specific chat
- Maintains conversation history per section
- Error handling and loading states

## Files Added
- db/migrate/20260329000000_create_piyo_chat_messages.rb
- app/models/piyo_chat_message.rb
- app/services/piyo_chat_service.rb
- app/controllers/api/textbooks/piyo_chat_messages_controller.rb
- app/javascript/controllers/piyo_chat_controller.js
- test/models/piyo_chat_message_test.rb
- test/services/piyo_chat_service_test.rb
- test/fixtures/piyo_chat_messages.yml

## Files Modified
- config/routes/textbook.rb: added piyo_chat_messages routes
- app/models/user.rb: has_many piyo_chat_messages
- app/views/shared/_piyo_companion.html.slim: chat panel UI
- app/views/textbooks/sections/show.html.slim: pass section_id
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 29, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2d82104a-4233-4673-bb43-6fda162d6e93

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/textbook-piyo-chat

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant