Skip to content

AIエージェントに行き先を相談できるチャット画面とエントリバナーを追加 - #6479

Merged
TinyKitten merged 5 commits into
devfrom
feature/6475-ai-agent-app
Jul 29, 2026
Merged

TinyKitten merged 5 commits into
devfrom
feature/6475-ai-agent-app

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

概要

Issue-driven 開発の親タスク #6473 で定義された「駅名を知らなくても連想できる言葉で行き先を決定できる」体験の PoC として、AI エージェントと対話して行き先を決めるチャット画面をアプリ側に実装しました。設計は docs/spec/ai-agent/architecture.md(#6476)/ docs/spec/ai-agent/ui.md(#6478)で FIX 済みの内容に、Figma の確定デザイン(v10 ファイル node 1650-91)を反映しています。

BFF 側(/agent/chat)は TrainLCD/BFF リポジトリで別途実装予定のためスコープ外です。機能は Remote Config ai_agent_enabled(既定 false)と isDevApp の二重ゲートで守られており、本番ビルドには露出しません。

変更の種類

  • バグ修正
  • 新機能
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • src/screens/DestinationAgent/(新規): チャット画面 DestinationAgentScreen と専用コンポーネント(AgentHeader / AgentMessageBubble / AgentTypingIndicator / AgentEmptyState / AgentInputBar / AgentEntryBanner)を co-locate。空状態(例文チップ・免責)、タイピングインジケータ、LED テーマ分岐、タブレット最大幅 700 対応を含む
  • AgentMessageBubble は AI 応答の Markdown 強調(**text**)を太字としてインライン描画(フル Markdown は解釈しない)
  • src/hooks/useDestinationAgent.ts(新規): /agent/chat 呼び出しフック。callable 互換ワイヤ形式 + セッション JWT、履歴 12 件 / 500 字のクライアント側切り詰め、30 秒タイムアウト、rateLimited / timeout / network のエラー分類
  • src/hooks/useDestinationSelection.ts(新規): RouteSearchScreen の行き先決定ロジック(GET_ROUTE_TYPES_LIGHT → 種別決定 → pendingStations 構築)を共有フックへ抽出し、検索結果タップと提案カードタップを同一の SelectBoundModal フローへ合流
  • src/hooks/useAIAgentFeatureEnabled.ts(新規): ai_agent_enabled の useSyncExternalStore 購読 + PoC 期間中の isDevApp AND ゲート
  • src/lib/remoteConfig.ts: ai_agent_enabled キー追加(フォールバック false のキルスイッチ)
  • src/screens/RouteSearchScreen.tsx: 選択ロジックを共有フックへ移設し、検索バー直下に AI 相談エントリバナーを追加(フラグ off 時は非表示・従来レイアウト維持)
  • src/stacks/MainStack.tsx: DestinationAgent 画面を登録
  • assets/translations/{ja,en}.json: UI 文言 15 キー追加
  • src/components/Chip.tsx: accessibilityRole="button" を付与
  • 提案駅は GET_STATIONS_BY_IDS で Station 完全体を再取得してから CommonCard で描画(lineNames の名称一致による路線解決は不使用)。ユーザバブルは WCAG AA 準拠の #0071CE
  • テスト追加: useDestinationAgent.test.ts(正常 / refused / 429 / 5xx / ネットワーク / タイムアウト / 切り詰め)、remoteConfig.test.ts に 5 ケース

リグレッションリスクと緩和

  • RouteSearchScreen の選択ロジック共有フック化が最大のリスク。ロジックは挙動を変えずに移設し、既存 RouteSearchScreen.test.tsx が無修正で通過することを確認済み
  • 新機能側は二重フラグゲートにより off が既定。障害時は KV の ai_agent_enabled を false にするだけで全クライアントから機能が消える

開発プロセス(#6475 の実装備考)

  • 実装: Claude Opus 5 / ローカルレビュー: Claude Fable 5(レビューで Figma 余白差異・出現アニメーション・LED チップ背景・リセット競合の 4 点を修正)
  • CodeRabbit CLI は実行環境未導入のため未実施。本 PR 上の CodeRabbit レビューで代替

テスト

  • npm run lint が通ること
  • npm test が通ること
  • npm run typecheck が通ること

ローカルで npm run lint(596 ファイル指摘なし)/ npx jest(195 suites / 2104 tests 全通過)/ npm run typecheck(エラーなし)を実行。BFF 未実装のため実機での結合確認と UI 手動 QA(キーボード追従・LED テーマ・multiline 高さ)は未実施で、dev ビルドで別途行います。

関連Issue

Closes #6475

Refs #6473

スクリーンショット(任意)

🤖 Generated with Claude Code

https://claude.ai/code/session_017SRZ31WjnRgkhn3N9LoNyY

Summary by CodeRabbit

  • 新機能
    • 目的地をAIに相談し、駅候補やエラー時の再試行を行える「目的地エージェント」画面を追加しました。
    • ルート検索画面にAI相談バナーの導線を追加しました。
  • アクセシビリティ
    • ボタン要素に適切なロール/ラベルを設定しました。
  • テスト
    • 会話履歴のトリム、送信結果(429/ネットワーク/タイムアウト等)、候補抽出、太字装飾の挙動を検証するテストを追加しました。
  • ローカライズ
    • 英語・日本語のUI文言を追加しました。

@TinyKitten TinyKitten self-assigned this Jul 29, 2026
@github-actions github-actions Bot added the react label Jul 29, 2026
@coderabbitai

coderabbitai Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 70677d6f-d770-426d-b96d-35b65e9a4d3e

📥 Commits

Reviewing files that changed from the base of the PR and between 87e9c3c and 80ef01f.

📒 Files selected for processing (1)
  • src/screens/DestinationAgent/index.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/screens/DestinationAgent/index.tsx

📝 Walkthrough

Walkthrough

AI目的地案内機能を追加しました。リモート設定による公開制御、チャット通信、候補駅表示、目的地選択の共通化、専用画面とナビゲーション、英語・日本語リソースを実装しています。

Changes

AI目的地案内

Layer / File(s) Summary
機能フラグと表示リソース
assets/translations/*.json, src/lib/remoteConfig.*, src/hooks/useAIAgentFeatureEnabled.ts, src/components/Chip.tsx
AIエージェント用のリモート設定、購読フック、翻訳文言、ボタンのアクセシビリティロールを追加しました。
エージェント通信契約と検証
src/hooks/useDestinationAgent.ts, src/hooks/useDestinationAgent.test.ts
履歴制限、認証付きチャット送信、タイムアウト、応答検証、候補フィルタリング、通信エラー分類を実装し、テストを追加しました。
目的地選択フローの共通化
src/hooks/useDestinationSelection.ts, src/screens/RouteSearchScreen.tsx
目的地駅・列車種別の取得とモーダル状態を新しいフックへ集約し、検索画面とモーダルに接続しました。
目的地案内画面とナビゲーション
src/screens/DestinationAgent/*, src/stacks/MainStack.tsx
チャット画面、入力・メッセージ・候補表示、リセット処理、テーマ対応UI、専用ルートを追加しました。
メッセージ表示形式の検証
src/screens/DestinationAgent/AgentMessageBubble.tsx, src/screens/DestinationAgent/AgentMessageBubble.test.ts
AI応答内の太字形式を表示用セグメントへ分割し、複数形式のテストを追加しました。

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant 利用者
  participant DestinationAgentScreen
  participant useDestinationAgent
  participant workerApi
  participant GraphQL
  利用者->>DestinationAgentScreen: 行き先を入力して送信
  DestinationAgentScreen->>useDestinationAgent: 会話履歴を渡す
  useDestinationAgent->>workerApi: POST /agent/chat
  workerApi-->>useDestinationAgent: 返信と駅候補
  useDestinationAgent-->>DestinationAgentScreen: 応答結果
  DestinationAgentScreen->>GraphQL: 候補駅情報を取得
  GraphQL-->>DestinationAgentScreen: 駅・路線情報
Loading

Poem

ぴょんと相談、駅を探す
AIの返事が道を照らす
候補のカードを並べたら
列車の選択もおまかせ
うさぎも安心、出発だ!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 変更内容の中心であるAIエージェントの行き先相談用チャット画面とエントリバナーを的確に要約しています。
Description check ✅ Passed 必須の概要、変更内容、テスト、関連Issueが揃っており、任意項目以外も十分に記載されています。
Linked Issues check ✅ Passed 直接Issue #6475 のアプリ側実装要件に沿って、チャット画面、関連フック、画面登録、翻訳、機能ゲートを実装しています。
Out of Scope Changes check ✅ Passed 追加されたアクセシビリティや共通化の変更も目的のAIエージェント機能と導線に付随しており、明確な逸脱は見当たりません。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/6475-ai-agent-app

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (5)
src/hooks/useDestinationSelection.ts (2)

155-162: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

冗長なガードとコメント不一致。

Line 156-160 で selectedStation.line?.id を既に確認済みのため、Line 176-178 の再チェックは到達不能です。またコメント(Line 155)は「lineId と stationId」と書かれていますが実際には groupId を検証しています。

Also applies to: 175-178

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/useDestinationSelection.ts` around lines 155 - 162,
useDestinationSelection の選択処理で、既に確認済みの selectedStation.line?.id
を重複して検証している到達不能なガードを削除または統合してください。あわせて、ガード直前のコメントを実際に検証している groupId
の内容と一致するよう更新してください。

133-274: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

連続タップ時に古い選択の結果が後勝ちで atom を上書きする可能性があります。

useLazyGraphQLQuery は内部 state を requestId で保護しますが、execute の戻り値は古いリクエストでも解決されるため、この関数は await 後に pendingStations / pendingStation / pendingLine を無条件で書き込みます。候補カードを素早く連続タップすると、先行選択のレスポンスが後の選択の状態を上書きし得ます。呼び出しごとの世代カウンタ(DestinationAgentScreen の conversationGenRef と同様の ref)で古い結果を破棄する形が安全です。

♻️ 世代カウンタによるガード例
+  const selectionGenRef = useRef(0);
+
   const handleDestinationSelected = useCallback(
     async (selectedStation: Station) => {
+      const gen = ++selectionGenRef.current;
       setSelectBoundModalVisible(true);

以降の各 await の直後で if (gen !== selectionGenRef.current) return; を挟みます。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/useDestinationSelection.ts` around lines 133 - 274, Protect
handleDestinationSelected against stale results from rapid consecutive
selections by adding a ref-backed selection generation counter and incrementing
it for each invocation. Capture the generation at the start, then after every
awaited fetch—fetchRouteTypes, fetchStationsByLineId, and
fetchStationsByLineGroupId—return immediately when it no longer matches the
current generation before updating pendingStation, pendingStations, pendingLine,
or navigation state; include the ref in the callback dependencies as
appropriate.
src/screens/DestinationAgent/AgentInputBar.tsx (2)

88-91: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

isJapanese 相当のセッション定数ではないため useMemo は妥当ですが、簡素化できます。

三項演算子1つのため useMemo のコストのほうが大きい可能性があります。素の式で十分です。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/screens/DestinationAgent/AgentInputBar.tsx` around lines 88 - 91,
Simplify the fontFamily assignment in the AgentInputBar component by removing
the useMemo wrapper and using the existing isLEDTheme ternary expression
directly. Remove any now-unused useMemo import while preserving the selected
FONTS.JFDotJiskan24h and FONTS.RobotoRegular values.

19-20: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

閾値は AGENT_MAX_MESSAGE_LENGTH から導出してください。

450 は AGENT_MAX_MESSAGE_LENGTH が 500 であることを前提としたマジックナンバーで、上限を変更すると無言でずれます。

♻️ 提案
-// 残量が見えないと困る領域に入ってから出す(500 - 50)
-const COUNTER_VISIBLE_THRESHOLD = 450;
+// 残り 50 文字を切ったら残量を表示する
+const COUNTER_VISIBLE_THRESHOLD = AGENT_MAX_MESSAGE_LENGTH - 50;

AGENT_MAX_MESSAGE_LENGTH の import はファイル先頭にあるものをそのまま使えます。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/screens/DestinationAgent/AgentInputBar.tsx` around lines 19 - 20, Update
COUNTER_VISIBLE_THRESHOLD in AgentInputBar so it is derived from the imported
AGENT_MAX_MESSAGE_LENGTH rather than the hard-coded 450 value, preserving the
existing 50-character margin.
src/screens/DestinationAgent/AgentMessageBubble.tsx (1)

63-79: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

スクリーンリーダーで発話者が判別できません。

視覚的には左右の配置と色で user / assistant を区別していますが、読み上げでは本文のみになります。accessibilityLabel に話者を含めると会話の流れが伝わります。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/screens/DestinationAgent/AgentMessageBubble.tsx` around lines 63 - 79,
Update the accessible label on the message container in AgentMessageBubble so it
includes the speaker identity (user or assistant) together with the message
content. Preserve the existing visual styles and ensure the screen reader
announces the speaker before the message text.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/hooks/useDestinationSelection.ts`:
- Around line 86-90: Update the train-type selection flow in this hook so an
explicit user action sets trainTypeListModalVisible to true, providing the
missing opening path for TrainTypeListModal. Reuse the existing handler or add a
dedicated handler near handleTrainTypeSelected, and preserve the existing
select-bound modal behavior where applicable.

In `@src/screens/DestinationAgent/index.tsx`:
- Around line 305-312:
エラー復帰処理のsetInputText(text)を、現在の入力欄が空の場合にのみ実行するようDestinationAgentの送信失敗ブロックを更新してください。送信中にユーザが新しく入力した内容は保持し、既存のentries削除とエラー通知は変更しないでください。
- Around line 260-267: Update the sending flow in DestinationAgent around
sendMessages so sendingRef.current and setSending(false) execute in a finally
block, including when sendMessages rejects. Keep the existing successful send
behavior unchanged while ensuring the typing indicator and send button are
always reset.

---

Nitpick comments:
In `@src/hooks/useDestinationSelection.ts`:
- Around line 155-162: useDestinationSelection の選択処理で、既に確認済みの
selectedStation.line?.id
を重複して検証している到達不能なガードを削除または統合してください。あわせて、ガード直前のコメントを実際に検証している groupId
の内容と一致するよう更新してください。
- Around line 133-274: Protect handleDestinationSelected against stale results
from rapid consecutive selections by adding a ref-backed selection generation
counter and incrementing it for each invocation. Capture the generation at the
start, then after every awaited fetch—fetchRouteTypes, fetchStationsByLineId,
and fetchStationsByLineGroupId—return immediately when it no longer matches the
current generation before updating pendingStation, pendingStations, pendingLine,
or navigation state; include the ref in the callback dependencies as
appropriate.

In `@src/screens/DestinationAgent/AgentInputBar.tsx`:
- Around line 88-91: Simplify the fontFamily assignment in the AgentInputBar
component by removing the useMemo wrapper and using the existing isLEDTheme
ternary expression directly. Remove any now-unused useMemo import while
preserving the selected FONTS.JFDotJiskan24h and FONTS.RobotoRegular values.
- Around line 19-20: Update COUNTER_VISIBLE_THRESHOLD in AgentInputBar so it is
derived from the imported AGENT_MAX_MESSAGE_LENGTH rather than the hard-coded
450 value, preserving the existing 50-character margin.

In `@src/screens/DestinationAgent/AgentMessageBubble.tsx`:
- Around line 63-79: Update the accessible label on the message container in
AgentMessageBubble so it includes the speaker identity (user or assistant)
together with the message content. Preserve the existing visual styles and
ensure the screen reader announces the speaker before the message text.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3885cda3-a462-41b2-ac28-a489a22ac7ff

📥 Commits

Reviewing files that changed from the base of the PR and between 9e59232 and 5ccb7ca.

📒 Files selected for processing (18)
  • assets/translations/en.json
  • assets/translations/ja.json
  • src/components/Chip.tsx
  • src/hooks/useAIAgentFeatureEnabled.ts
  • src/hooks/useDestinationAgent.test.ts
  • src/hooks/useDestinationAgent.ts
  • src/hooks/useDestinationSelection.ts
  • src/lib/remoteConfig.test.ts
  • src/lib/remoteConfig.ts
  • src/screens/DestinationAgent/AgentEmptyState.tsx
  • src/screens/DestinationAgent/AgentEntryBanner.tsx
  • src/screens/DestinationAgent/AgentHeader.tsx
  • src/screens/DestinationAgent/AgentInputBar.tsx
  • src/screens/DestinationAgent/AgentMessageBubble.tsx
  • src/screens/DestinationAgent/AgentTypingIndicator.tsx
  • src/screens/DestinationAgent/index.tsx
  • src/screens/RouteSearchScreen.tsx
  • src/stacks/MainStack.tsx

Comment thread src/hooks/useDestinationSelection.ts Resolved
Comment thread src/screens/DestinationAgent/index.tsx Resolved
Comment thread src/screens/DestinationAgent/index.tsx Resolved
@TinyKitten

Copy link
Copy Markdown
Member Author

CodeRabbit Autofix 適用結果

未解決スレッド 3 件を検証し、1 件を修正しました。

適用(コミット d3bdc5698):

  • src/screens/DestinationAgent/index.tsx: sendMessages を try/finally で包み、例外時も送信フラグ(sendingRef / sending)が確実に解除されるように修正

スキップ:

  • TrainTypeListModal を開く導線(useDestinationSelection.ts): リファクタ前の dev にも setTrainTypeListModalVisible(true) は存在せず、既存挙動の忠実な移設。挙動変更は本 PR のスコープ外のため別課題とする
  • エラー復帰時の setInputText 上書きガード(DestinationAgent/index.tsx): レビューの上、今回は見送り

検証: npm run lint / npm run typecheck / 関連 Jest(29 tests)すべて通過。

@TinyKitten
TinyKitten merged commit 176a8ad into dev Jul 29, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the feature/6475-ai-agent-app branch July 29, 2026 13:53
@TinyKitten TinyKitten mentioned this pull request Jul 29, 2026
5 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AI PoC] アプリ実装

1 participant