Skip to content

Conversation

@eiel
Copy link
Contributor

@eiel eiel commented Jul 19, 2025

概要

list_roomsツールにページネーション機能を追加し、APIアクセス削減のためのキャッシュ機能を実装しました。

主な機能

  • ページネーション: offset/limitパラメータによる効率的なデータ取得
  • スマートキャッシュ: 5分間のTTLキャッシュでChatWork API呼び出しを最小化
  • 型安全性: Zodバリデーションによる実行時データ整合性保証

実装詳細

  • Redux Toolkit: 関数型プログラミングアプローチと拡張可能なアーキテクチャのため採用
  • Zodスキーマ: ChatWork Room オブジェクトの完全な型定義と検証
  • 包括的テスト: ページネーションとキャッシュロジックの完全なテストカバレッジ

メリット

  • インテリジェントキャッシュによるAPI負荷軽減
  • 大量ルーム一覧に対するページネーションでUX向上
  • 将来の機能拡張に対応可能なアーキテクチャ

Test plan

  • 新しいページネーションパラメータの動作確認
  • キャッシュの有効期限とデータ取得ロジックの検証
  • Zodスキーマによるデータバリデーションの確認
  • 既存機能への影響がないことの確認

🤖 Generated with Claude Code

以下人間による追加。

動作確認方法として、

npx @modelcontextprotocol/inspector node dist/index.js

を使って動作を確認。

試した引数

  • offset 0, limit 100
  • offset 100, limit 10
  • 取得できたルーム一覧の末尾10件 (offsetは実データによる)

reduxを使ったら理由の補足。classが使われる実装になったため。classの代わりの代替えの方法としては、クロージャかreduxを使う手段が検討できる。クロージャを使う場合も実質オブジェクト指向なので、関数的なreduxを採用している。

@eiel eiel requested review from Copilot and sudame July 19, 2025 10:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds pagination functionality and caching to the list_rooms tool, implementing smart caching with 5-minute TTL to reduce ChatWork API calls and pagination via offset/limit parameters for better user experience when handling large room lists.

  • Implements pagination with offset/limit parameters for efficient data retrieval
  • Adds Redux-based caching system with 5-minute TTL to minimize API calls
  • Introduces comprehensive Zod schema validation for ChatWork Room objects

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/types/room.ts Defines comprehensive Zod schema for ChatWork Room objects with validation helpers
src/toolCallbacks.ts Refactors listRooms to support pagination and caching via Redux store
src/store/roomsSlice.ts Implements Redux slice for room caching with TTL-based expiration
src/store/roomsSlice.test.ts Comprehensive test coverage for Redux slice and pagination logic
src/store/index.ts Configures Redux store with rooms slice and exports convenience functions
src/server.ts Updates server registration to use new pagination schema
src/schema.ts Adds pagination parameters schema for list_rooms tool
package.json Adds Redux Toolkit dependency


role: z
.enum(['admin', 'member', 'readonly'])
.describe('ルールでの自分の権限 (admin: 管理者, member: メンバー, readonly: 閲覧のみ)'),
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

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

The word 'ルール' should be 'ルーム' (room) instead of 'ルール' (rule) in the Japanese description.

Suggested change
.describe('ルールでの自分の権限 (admin: 管理者, member: メンバー, readonly: 閲覧のみ)'),
.describe('ルームでの自分の権限 (admin: 管理者, member: メンバー, readonly: 閲覧のみ)'),

Copilot uses AI. Check for mistakes.
getDefaultMiddleware({
// Disable serializability check for Date objects in TTL
serializableCheck: {
ignoredPaths: ['rooms.rooms.timestamp'],
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

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

[nitpick] The nested path 'rooms.rooms.timestamp' creates confusion due to the redundant naming. Consider renaming the slice or the property to improve clarity.

Suggested change
ignoredPaths: ['rooms.rooms.timestamp'],
ignoredPaths: ['rooms.data.timestamp'],

Copilot uses AI. Check for mistakes.
response: JSON.stringify(minifiedResponse),
};
};
const CACHE_TTL = 5 * 60 * 1000; // 5分
Copy link

Copilot AI Jul 19, 2025

Choose a reason for hiding this comment

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

[nitpick] The cache TTL should be configurable rather than hardcoded. Consider moving this to a configuration file or environment variable.

Suggested change
const CACHE_TTL = 5 * 60 * 1000; // 5分
const CACHE_TTL = Number(process.env.CACHE_TTL_MS) || 5 * 60 * 1000; // Default to 5 minutes

Copilot uses AI. Check for mistakes.
listContacts,
);
server.tool('list_rooms', 'チャット一覧を取得します。', listRooms);
server.registerTool(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

registerToolに変更したのは、こちらのほうが最新のAPIであり、toolはさまざまな引数に対応できるoverloadを実現しているのでやや扱いにくいため、将来的にこちらを利用すべきと考えたため。

eiel and others added 3 commits July 19, 2025 21:53
This change implements pagination for the list_rooms tool to improve performance
and usability when dealing with large numbers of rooms. A TTL-based cache system
was added to prevent unnecessary API calls when fetching room data.

Changes:
- Add pagination parameters (offset, limit) to listRoomsParamsSchema
- Implement Cache class with TTL functionality for efficient data caching
- Update listRooms implementation to use cache and support pagination
- Modify server.ts to use new parameter schema for list_rooms tool

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
… Zod validation

Replace the class-based cache system with Redux Toolkit for functional programming
approach and add comprehensive Zod schema validation for ChatWork Room objects.

Changes:
- Remove src/cache.ts class-based implementation
- Add Redux Toolkit store with TTL-enabled state management
- Implement roomsSlice with pure function reducers and selectors
- Add comprehensive Zod schema for Room type validation (src/types/room.ts)
- Update listRooms to use Redux store and runtime validation
- Add extensive unit tests for Redux slice and selectors
- Maintain pagination functionality with improved type safety

Benefits:
- Functional programming approach with pure functions
- Runtime type validation with Zod schemas
- Immutable state management via Redux Toolkit
- Enhanced extensibility for future middleware additions
- Better testability with predictable state transitions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@eiel eiel force-pushed the feature/rooms-pagination-with-cache branch from bfc607f to d3bb14c Compare July 19, 2025 12:53
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.

2 participants