Skip to content

AIエージェント送信時のトークン先読みと検索中表示でストリーミング開始までの体感遅延を改善 - #6493

Merged
TinyKitten merged 3 commits into
devfrom
feature/ai-agent-streaming
Jul 31, 2026
Merged

TinyKitten merged 3 commits into
devfrom
feature/ai-agent-streaming

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

概要

AI エージェントのストリーミング対応(#6492)のフォローアップとして、送信からストリーミング開始(最初の delta 受信)までの体感遅延を改善しました。クライアント側で対処できる 2 点です:

  1. セッショントークンの先読み: アプリ起動後の初回送信では /auth/token への往復が送信の直列経路に入っていたため、チャット画面マウント時に先読みしてキャッシュに載せ、送信時の待ちを解消
  2. 駅検索中の状態表示: サーバの tool use ループ中(数秒)は従来「・・・」のままだったため、SSE の tool イベントを受けてタイピングインジケータに「駅を検索しています…」を添えて進捗が見えるように変更

BFF 側の直列レイテンシ削減(前段処理の並列化・レート制限カウンタ書き込みのクリティカルパス除去・OpenAI 系モデルの reasoning 抑制、毎ターン約 150〜450ms + reasoning 分の削減見込み)は TrainLCD/BFF リポジトリ側で実装済みで、別途 PR を作成します。本 PR には対応する設計書の追随を含みます。

変更の種類

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

変更内容

  • src/hooks/useDestinationAgent.ts: マウント時に getSessionToken() を先読みする useEffect を追加(キャッシュ付き・冪等のため StrictMode 再実行にも安全)
  • src/lib/session.ts: 取得中(in-flight)の Promise を共有し、先読みと初回送信が競合しても /auth/token への POST が 1 回になるよう変更(CodeRabbit 指摘対応)
  • src/screens/DestinationAgent/index.tsx: searching state を追加し、onToolStart(世代ガード通過時のみ)で検索中表示を点灯、最初の delta または応答確定で消灯。読み上げは 1 送信 1 回
  • src/screens/DestinationAgent/AgentTypingIndicator.tsx: optional な label prop を追加し、ドットの右に補助文言を表示(LED テーマ対応)
  • assets/translations/ja.json / en.json: destinationAgentSearching キーを追加
  • docs/spec/ai-agent/ui.md: 検索中表示の仕様を追記
  • docs/spec/ai-agent/architecture.md: BFF 側レイテンシ改善(前段処理の並列化・レート制限カウンタのチェック / 消費分離)を設計書に反映
  • src/hooks/useDestinationAgent.test.ts: トークン先読みのテスト 2 件を追加(計 24 件)
  • src/lib/session.test.ts(新規): 並行呼び出しの POST 共有・失敗後の再取得のテスト 3 件

テスト

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

npm test は全体で 199 suites / 2143 tests パス。dev への rebase 後にも npm run typecheck と対象テストの通過を確認済みです。設計書は markdownlint-cli2 0 件。

関連Issue

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

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

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

目的地検索AIに検索中ラベル、検索状態、アクセシビリティ通知を追加しました。useDestinationAgent はマウント時にセッショントークンを先読みします。英語・日本語の翻訳と仕様、テストを更新しました。

Changes

目的地エージェントの読み込み状態

Layer / File(s) Summary
検索中ラベルの表示
src/screens/DestinationAgent/AgentTypingIndicator.tsx, assets/translations/*.json, docs/spec/ai-agent/ui.md
AgentTypingIndicator が任意のラベルを表示します。検索中メッセージの英語・日本語翻訳と仕様を追加しました。
検索中状態と通知の制御
src/screens/DestinationAgent/index.tsx, docs/spec/ai-agent/ui.md
tool 開始時に検索中状態を設定します。最初の delta、完了、送信失敗時に状態を解除します。検索中のアクセシビリティ通知は1回だけ実行します。
セッショントークンの先読み
src/hooks/useDestinationAgent.ts, src/hooks/useDestinationAgent.test.ts
マウント時にセッショントークンを取得します。取得失敗を無視する動作と、チャットAPIを呼ばない動作をテストします。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant DestinationAgentScreen
  participant AgentTypingIndicator
  participant AccessibilityNotification
  DestinationAgentScreen->>DestinationAgentScreen: tool 開始イベントを受信する
  DestinationAgentScreen->>AgentTypingIndicator: destinationAgentSearching を渡す
  AgentTypingIndicator-->>DestinationAgentScreen: 検索中ラベルを表示する
  DestinationAgentScreen->>AccessibilityNotification: 検索中メッセージを1回通知する
  DestinationAgentScreen->>DestinationAgentScreen: 最初の delta、完了、失敗時に状態を解除する
Loading

Possibly related PRs

Poem

うさぎが駅を検索中
点々の横に文字が跳ねる
tool が始まり、声が届く
delta が来れば表示は消える
トークンも先に、ぴょんと準備完了!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed タイトルは、トークン先読みと検索中表示によるストリーミング開始前の体感遅延改善を明確に示しています。
Description check ✅ Passed 概要、変更内容、テスト結果、変更種別、関連Issue、スクリーンショット欄を含み、変更目的と検証結果を十分に説明しています。
✨ 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/ai-agent-streaming

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: 1

🧹 Nitpick comments (1)
src/hooks/useDestinationAgent.test.ts (1)

111-127: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

キャッシュ再利用を検証するテストを追加してください。

このテストは getSessionTokenMock の呼び出しだけを確認しています。モックがキャッシュ処理を置き換えるため、先読み完了後の sendMessages が同じトークンを再利用し、/auth/token を再取得しないことは検証できません。実際のキャッシュ動作、または取得中の Promise の共有を検証するテストを追加してください。

🤖 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/useDestinationAgent.test.ts` around lines 111 - 127,
useDestinationAgent の先読みだけでなく、取得済みトークンのキャッシュ再利用を検証するテストを追加してください。先読み完了後に
sendMessages を実行し、同じトークンが送信に使われることと、getSessionTokenMock および
/auth/token(fetchMock)が追加取得されないことを確認してください。必要に応じて、取得中の Promise
が共有されるケースも検証してください。
🤖 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/useDestinationAgent.ts`:
- Around line 152-161: Update getSessionToken in src/lib/session.ts to share the
in-flight token-fetch Promise, so concurrent preload and sendMessages calls
reuse one /auth/token POST before caching the completed token. Clear the shared
Promise after resolution or rejection while preserving existing cache and error
behavior, and add a test covering concurrent calls and ensuring only one request
is made.

---

Nitpick comments:
In `@src/hooks/useDestinationAgent.test.ts`:
- Around line 111-127: useDestinationAgent
の先読みだけでなく、取得済みトークンのキャッシュ再利用を検証するテストを追加してください。先読み完了後に sendMessages
を実行し、同じトークンが送信に使われることと、getSessionTokenMock および
/auth/token(fetchMock)が追加取得されないことを確認してください。必要に応じて、取得中の Promise
が共有されるケースも検証してください。
🪄 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: 9a662fb1-1cd1-4544-9143-3836192d74ae

📥 Commits

Reviewing files that changed from the base of the PR and between 22ba9de and 93dd8c3.

📒 Files selected for processing (7)
  • assets/translations/en.json
  • assets/translations/ja.json
  • docs/spec/ai-agent/ui.md
  • src/hooks/useDestinationAgent.test.ts
  • src/hooks/useDestinationAgent.ts
  • src/screens/DestinationAgent/AgentTypingIndicator.tsx
  • src/screens/DestinationAgent/index.tsx

Comment thread src/hooks/useDestinationAgent.ts Resolved
@TinyKitten

Copy link
Copy Markdown
Member Author

Fixes Applied Successfully

Fixed 2 file(s) based on 1 CodeRabbit feedback item(s).

Files modified:

  • src/lib/session.ts
  • src/lib/session.test.ts

Commit: 059b494d4

The latest autofix changes are on the feature/ai-agent-streaming branch.

@TinyKitten
TinyKitten merged commit 93fd732 into dev Jul 31, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the feature/ai-agent-streaming branch July 31, 2026 05:03
@TinyKitten TinyKitten mentioned this pull request Jul 31, 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.

1 participant