Repository navigation
feat(web): add configurable tool grouping mode #1157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3545084
feat(web): add configurable tool grouping mode
techotaku39 a0b1335
fix(web): default tool grouping to grouped
techotaku39 d6c8483
fix(web): sync tool grouping mode in the same window
techotaku39 b62788b
fix(web): reset tool group ids when mode changes
techotaku39 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { act, renderHook } from '@testing-library/react' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { | ||
| DEFAULT_TOOL_GROUPING_MODE, | ||
| getInitialToolGroupingMode, | ||
| getToolGroupingModeOptions, | ||
| useToolGroupingMode, | ||
| } from './useToolGroupingMode' | ||
|
|
||
| describe('useToolGroupingMode helpers', () => { | ||
| beforeEach(() => { | ||
| window.localStorage.clear() | ||
| }) | ||
|
|
||
| it('returns grouped and classified options', () => { | ||
| expect(getToolGroupingModeOptions()).toEqual([ | ||
| { value: 'grouped', labelKey: 'settings.chat.toolGrouping.grouped' }, | ||
| { value: 'classified', labelKey: 'settings.chat.toolGrouping.classified' }, | ||
| ]) | ||
| }) | ||
|
|
||
| it('defaults to grouped for missing or invalid values', () => { | ||
| expect(getInitialToolGroupingMode()).toBe(DEFAULT_TOOL_GROUPING_MODE) | ||
| window.localStorage.setItem('hapi-tool-grouping-mode', 'invalid') | ||
| expect(getInitialToolGroupingMode()).toBe(DEFAULT_TOOL_GROUPING_MODE) | ||
| }) | ||
|
|
||
| it('reads the classified preference', () => { | ||
| window.localStorage.setItem('hapi-tool-grouping-mode', 'classified') | ||
| expect(getInitialToolGroupingMode()).toBe('classified') | ||
| }) | ||
|
|
||
| it('syncs changes between hook consumers in the same window', () => { | ||
| const first = renderHook(() => useToolGroupingMode()) | ||
| const second = renderHook(() => useToolGroupingMode()) | ||
|
|
||
| act(() => first.result.current.setToolGroupingMode('classified')) | ||
|
|
||
| expect(first.result.current.toolGroupingMode).toBe('classified') | ||
| expect(second.result.current.toolGroupingMode).toBe('classified') | ||
|
|
||
| act(() => second.result.current.setToolGroupingMode('grouped')) | ||
|
|
||
| expect(first.result.current.toolGroupingMode).toBe('grouped') | ||
| expect(second.result.current.toolGroupingMode).toBe('grouped') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { useCallback, useEffect, useState } from 'react' | ||
| import type { ToolGroupingMode } from '@/chat/toolGroups' | ||
|
|
||
| export const DEFAULT_TOOL_GROUPING_MODE: ToolGroupingMode = 'grouped' | ||
|
|
||
| export function getToolGroupingModeOptions(): ReadonlyArray<{ value: ToolGroupingMode; labelKey: string }> { | ||
| return [ | ||
| { value: 'grouped', labelKey: 'settings.chat.toolGrouping.grouped' }, | ||
| { value: 'classified', labelKey: 'settings.chat.toolGrouping.classified' }, | ||
| ] | ||
| } | ||
|
|
||
| const STORAGE_KEY = 'hapi-tool-grouping-mode' | ||
| const CHANGE_EVENT = 'hapi-tool-grouping-mode-change' | ||
|
|
||
| function isBrowser(): boolean { | ||
| return typeof window !== 'undefined' && typeof document !== 'undefined' | ||
| } | ||
|
|
||
| function parseToolGroupingMode(raw: string | null): ToolGroupingMode { | ||
| return raw === 'grouped' || raw === 'classified' | ||
| ? raw | ||
| : DEFAULT_TOOL_GROUPING_MODE | ||
| } | ||
|
|
||
| export function getInitialToolGroupingMode(): ToolGroupingMode { | ||
| if (!isBrowser()) return DEFAULT_TOOL_GROUPING_MODE | ||
| try { | ||
| return parseToolGroupingMode(localStorage.getItem(STORAGE_KEY)) | ||
| } catch { | ||
| return DEFAULT_TOOL_GROUPING_MODE | ||
| } | ||
| } | ||
|
|
||
| export function useToolGroupingMode(): { | ||
| toolGroupingMode: ToolGroupingMode | ||
| setToolGroupingMode: (mode: ToolGroupingMode) => void | ||
| } { | ||
| const [toolGroupingMode, setToolGroupingModeState] = useState<ToolGroupingMode>(getInitialToolGroupingMode) | ||
|
|
||
| useEffect(() => { | ||
| if (!isBrowser()) return | ||
| const onStorage = (event: StorageEvent) => { | ||
| if (event.key === STORAGE_KEY) { | ||
| setToolGroupingModeState(parseToolGroupingMode(event.newValue)) | ||
| } | ||
| } | ||
| const onChange = () => { | ||
| setToolGroupingModeState(getInitialToolGroupingMode()) | ||
| } | ||
| window.addEventListener('storage', onStorage) | ||
| window.addEventListener(CHANGE_EVENT, onChange) | ||
| return () => { | ||
| window.removeEventListener('storage', onStorage) | ||
| window.removeEventListener(CHANGE_EVENT, onChange) | ||
| } | ||
| }, []) | ||
|
|
||
| const setToolGroupingMode = useCallback((mode: ToolGroupingMode) => { | ||
| setToolGroupingModeState(mode) | ||
| try { | ||
| if (mode === DEFAULT_TOOL_GROUPING_MODE) { | ||
| localStorage.removeItem(STORAGE_KEY) | ||
| } else { | ||
| localStorage.setItem(STORAGE_KEY, mode) | ||
| } | ||
| window.dispatchEvent(new Event(CHANGE_EVENT)) | ||
| } catch { | ||
| // Ignore storage errors | ||
| } | ||
| }, []) | ||
|
|
||
| return { toolGroupingMode, setToolGroupingMode } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MINOR] Keep the custom notification independent of localStorage success
The event is dispatched inside the try block. If localStorage.setItem/removeItem throws (blocked storage or quota failure), this hook updates its own state at line 60, but the already-mounted SessionChat hook never receives the new mode. The Settings radio can therefore show Classified while the chat stays Grouped.
Suggested fix:
Please add a two-consumer regression test with localStorage.setItem throwing.