-
-
Notifications
You must be signed in to change notification settings - Fork 434
fix(ui): restore ChatGPT cards after revisit #210
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
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,46 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { | ||
| cardFromOpenAIToolGlobals, | ||
| persistedCardFromOpenAIHost, | ||
| persistedCardFromWidgetState, | ||
| widgetStateWithPersistedCard, | ||
| } from "./card-persistence.js"; | ||
| import type { ToolResultCard } from "./card-types.js"; | ||
|
|
||
| test("card state round-trips without clobbering host state", () => { | ||
| const card: ToolResultCard = { tool: "read", path: "README.md" }; | ||
| const state = widgetStateWithPersistedCard( | ||
| { modelContent: "keep", privateContent: { selectedTab: "files" } }, | ||
| card, | ||
| ); | ||
|
|
||
| assert.equal(state.modelContent, "keep"); | ||
| assert.equal((state.privateContent as Record<string, unknown>).selectedTab, "files"); | ||
| assert.deepEqual(persistedCardFromWidgetState(state), card); | ||
| }); | ||
|
|
||
| test("persisted state wins over ChatGPT globals", () => { | ||
| const card: ToolResultCard = { tool: "read", path: "README.md" }; | ||
| assert.deepEqual( | ||
| persistedCardFromOpenAIHost({ | ||
| widgetState: widgetStateWithPersistedCard(undefined, card), | ||
| toolOutput: { path: "other.txt" }, | ||
| }), | ||
| card, | ||
| ); | ||
| }); | ||
|
|
||
| test("ChatGPT globals can rehydrate a historical card", () => { | ||
| assert.deepEqual( | ||
| cardFromOpenAIToolGlobals( | ||
| { workspaceId: "ws_history" }, | ||
| { | ||
| mcp_tool_result: { | ||
| _meta: { tool: "show_changes", card: { workspaceId: "ws_history" } }, | ||
| }, | ||
| }, | ||
| ), | ||
| { tool: "show_changes", workspaceId: "ws_history" }, | ||
| ); | ||
| }); | ||
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,96 @@ | ||
| import { | ||
| isToolName, | ||
| isToolResultCard, | ||
| type ToolResultCard, | ||
| } from "./card-types.js"; | ||
|
|
||
| const PERSISTED_CARD_KEY = "devspaceCard"; | ||
| const PERSISTED_CARD_VERSION = 1; | ||
|
|
||
| export interface OpenAIWidgetStateBridge { | ||
| toolOutput?: unknown; | ||
| toolResponseMetadata?: unknown; | ||
| widgetState?: unknown; | ||
| setWidgetState?: (state: unknown) => void; | ||
| } | ||
|
|
||
| interface PersistedCardEnvelope { | ||
| version: number; | ||
| card: ToolResultCard; | ||
| } | ||
|
|
||
| function asRecord(value: unknown): Record<string, unknown> | undefined { | ||
| return value && typeof value === "object" && !Array.isArray(value) | ||
| ? value as Record<string, unknown> | ||
| : undefined; | ||
| } | ||
|
|
||
| export function persistedCardFromWidgetState(widgetState: unknown): ToolResultCard | undefined { | ||
| const state = asRecord(widgetState); | ||
| const privateContent = asRecord(state?.privateContent); | ||
| const envelope = asRecord(privateContent?.[PERSISTED_CARD_KEY]); | ||
|
|
||
| if (envelope?.version !== PERSISTED_CARD_VERSION) return undefined; | ||
|
|
||
| const candidate = asRecord(envelope.card); | ||
| if (!candidate || !isToolName(candidate.tool) || !isToolResultCard(candidate)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return candidate as unknown as ToolResultCard; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export function cardFromOpenAIToolGlobals( | ||
| toolOutput: unknown, | ||
| toolResponseMetadata: unknown, | ||
| ): ToolResultCard | undefined { | ||
| const responseMetadata = asRecord(toolResponseMetadata); | ||
| const result = asRecord(responseMetadata?.mcp_tool_result) | ||
| ?? asRecord(responseMetadata?.call_tool_result); | ||
| const resultMeta = asRecord(result?._meta); | ||
| const metaCard = asRecord(resultMeta?.card); | ||
| const structuredContent = asRecord(toolOutput) | ||
| ?? asRecord(result?.structuredContent) | ||
| ?? {}; | ||
| const tool = resultMeta?.tool; | ||
|
|
||
| if (!isToolName(tool)) return undefined; | ||
|
|
||
| const candidate = { | ||
| ...structuredContent, | ||
| ...(metaCard ?? {}), | ||
| tool, | ||
| }; | ||
| if (!isToolResultCard(candidate)) return undefined; | ||
|
|
||
| return candidate as unknown as ToolResultCard; | ||
| } | ||
|
|
||
| export function persistedCardFromOpenAIHost( | ||
| bridge: OpenAIWidgetStateBridge | undefined, | ||
| ): ToolResultCard | undefined { | ||
| if (!bridge) return undefined; | ||
|
|
||
| return persistedCardFromWidgetState(bridge.widgetState) | ||
| ?? cardFromOpenAIToolGlobals(bridge.toolOutput, bridge.toolResponseMetadata); | ||
| } | ||
|
|
||
| export function widgetStateWithPersistedCard( | ||
| widgetState: unknown, | ||
| card: ToolResultCard, | ||
| ): Record<string, unknown> { | ||
| const currentState = asRecord(widgetState) ?? {}; | ||
| const currentPrivateContent = asRecord(currentState.privateContent) ?? {}; | ||
| const persisted: PersistedCardEnvelope = { | ||
| version: PERSISTED_CARD_VERSION, | ||
| card, | ||
| }; | ||
|
|
||
| return { | ||
| ...currentState, | ||
| privateContent: { | ||
| ...currentPrivateContent, | ||
| [PERSISTED_CARD_KEY]: persisted, | ||
| }, | ||
| }; | ||
| } | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Provide a valid conflicting ChatGPT card in this precedence test.
toolResponseMetadatais absent.cardFromOpenAIToolGlobalsthen cannot obtain_meta.tooland returnsundefined. The test only verifies that a persisted card is returned. It does not verify that persisted state overrides a valid ChatGPT fallback card.Add
mcp_tool_result._meta.tool: "read"totoolResponseMetadata. KeeptoolOutput.pathdifferent from the persisted card path.Proposed test change
persistedCardFromOpenAIHost({ widgetState: widgetStateWithPersistedCard(undefined, card), toolOutput: { path: "other.txt" }, + toolResponseMetadata: { + mcp_tool_result: { _meta: { tool: "read" } }, + }, }),📝 Committable suggestion
🤖 Prompt for AI Agents