-
Notifications
You must be signed in to change notification settings - Fork 2
feature: added preview option for reviewing the component #68
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
Open
yugo-ibuki
wants to merge
1
commit into
ssssota:main
Choose a base branch
from
yugo-ibuki:feature/preview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,5 +50,8 @@ export default defineTool( | |
| </ul> | ||
| </div> | ||
| ), | ||
| preview: { | ||
| toolOutput: { todos }, | ||
| }, | ||
| }, | ||
| ); | ||
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,97 @@ | ||
| import { | ||
| type DisplayMode, | ||
| SET_GLOBALS_EVENT_TYPE, | ||
| type OpenAiGlobals, | ||
| } from "./openai.js"; | ||
|
|
||
| export type Preview = { | ||
| toolInput?: Record<string, unknown>; | ||
| toolOutput?: Record<string, unknown>; | ||
| toolResponseMetadata?: Record<string, unknown>; | ||
| }; | ||
|
|
||
| export type PreviewDefaults = { | ||
| theme?: "light" | "dark"; | ||
| locale?: string; | ||
| maxHeight?: number; | ||
| displayMode?: DisplayMode; | ||
| }; | ||
|
|
||
| const defaultPreviewGlobals: PreviewDefaults = { | ||
| theme: "light", | ||
| locale: "en-US", | ||
| maxHeight: 600, | ||
| displayMode: "inline", | ||
| }; | ||
|
|
||
| /** | ||
| * Check if running in development mode. | ||
| * Returns false in production to prevent preview stubs from being used. | ||
| */ | ||
| function isDevMode(): boolean { | ||
| try { | ||
| // Vite injects import.meta.env.DEV | ||
| // biome-ignore lint/suspicious/noExplicitAny: Vite-specific property | ||
| return (import.meta as any).env?.DEV === true; | ||
| } catch { | ||
| // Fallback: assume production if import.meta.env is not available | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialize window.openai with preview data for local development. | ||
| * Only initializes in development mode when preview data is provided | ||
| * and window.openai is not already set by the host. | ||
| */ | ||
| export function initializePreview( | ||
| preview?: Preview, | ||
| defaults?: PreviewDefaults, | ||
| ): void { | ||
| // Only initialize preview in development mode | ||
| if (!preview || window.openai || !isDevMode()) return; | ||
|
|
||
| const config = { ...defaultPreviewGlobals, ...defaults }; | ||
|
|
||
| // Create a mutable state object for widgetState | ||
| let widgetState: Record<string, unknown> | null = null; | ||
|
|
||
| window.openai = { | ||
| theme: config.theme ?? "light", | ||
| userAgent: { | ||
| device: { type: "desktop" }, | ||
| capabilities: { hover: true, touch: false }, | ||
| }, | ||
| locale: config.locale ?? "en-US", | ||
| maxHeight: config.maxHeight ?? 600, | ||
| displayMode: config.displayMode ?? "inline", | ||
| safeArea: { insets: { top: 0, bottom: 0, left: 0, right: 0 } }, | ||
| toolInput: preview.toolInput ?? {}, | ||
| toolOutput: preview.toolOutput ?? null, | ||
| toolResponseMetadata: preview.toolResponseMetadata ?? null, | ||
| get widgetState() { | ||
| return widgetState; | ||
| }, | ||
| // Stub API methods for preview | ||
| callTool: async () => { | ||
| throw new Error("callTool is not available in preview mode"); | ||
| }, | ||
| sendFollowUpMessage: async () => { | ||
| throw new Error("sendFollowUpMessage is not available in preview mode"); | ||
| }, | ||
| openExternal: () => { | ||
| throw new Error("openExternal is not available in preview mode"); | ||
| }, | ||
| requestDisplayMode: async () => ({ mode: config.displayMode ?? "inline" }), | ||
| setWidgetState: async (state) => { | ||
| widgetState = state as Record<string, unknown>; | ||
| // Dispatch event to notify subscribers (like useWidgetState hook) | ||
| window.dispatchEvent( | ||
| new CustomEvent<{ globals: Partial<OpenAiGlobals> }>( | ||
| SET_GLOBALS_EVENT_TYPE, | ||
| { detail: { globals: { widgetState } } }, | ||
| ), | ||
| ); | ||
| }, | ||
| }; | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -50,5 +50,8 @@ export default defineTool( | |
| </ul> | ||
| </div> | ||
| ), | ||
| preview: { | ||
| toolOutput: { todos }, | ||
| }, | ||
| }, | ||
| ); | ||
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 |
|---|---|---|
|
|
@@ -50,5 +50,8 @@ export default defineTool( | |
| </ul> | ||
| </div> | ||
| ), | ||
| preview: { | ||
| toolOutput: { todos }, | ||
| }, | ||
| }, | ||
| ); | ||
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 |
|---|---|---|
|
|
@@ -50,5 +50,8 @@ export default defineTool( | |
| </ul> | ||
| </div> | ||
| ), | ||
| preview: { | ||
| toolOutput: { todos }, | ||
| }, | ||
| }, | ||
| ); | ||
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 |
|---|---|---|
|
|
@@ -50,5 +50,8 @@ export default defineTool( | |
| </ul> | ||
| </div> | ||
| ), | ||
| preview: { | ||
| toolOutput: { todos }, | ||
| }, | ||
| }, | ||
| ); | ||
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.
Preview data will be included in production bundles.
The preview field is a welcome addition for development, but since it's part of the widget configuration type without build-time stripping, all preview data (toolInput, toolOutput, toolResponseMetadata) will be bundled into production builds even though they're only used when
import.meta.env.DEVis true.Consider these approaches to prevent preview data from reaching production:
*.preview.ts) that can be excluded by bundler configurationif (import.meta.env.DEV)blocks so dead-code elimination can remove itAdditionally, the preview types use
Shape<T>which is correct, but the actual Preview type in preview.ts usesRecord<string, unknown>, losing the type safety provided by the schemas. Consider tightening the Preview type to preserve schema types if feasible.Would you like me to generate examples showing how to structure preview data for better tree-shaking, or help document best practices for keeping preview data minimal?