From e326ff0f8ab808d9e493717fe1d364a7e25ca666 Mon Sep 17 00:00:00 2001 From: Gabe Date: Fri, 9 Jan 2026 11:48:05 +0100 Subject: [PATCH] The UI package has been simplified. Here's a summary of the changes made: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Changes Made ### 1. `src/index.ts` - Consolidated duplicate imports for cleaner organization ### 2. `src/client.ts` - Simplified optional file method handling by using `.bind(adapter)` instead of wrapper functions with redundant null checks ### 3. `src/detection.ts` - Inlined the `isChatGPTSandbox()` helper function into `detectProtocol()` since it was only used once ### 4. `src/adapters/mcp.ts` - Eliminated a nested ternary operator in `mapHostContext()` for better readability - Simplified console log mapping ### 5. `src/adapters/mock.ts` - Simplified console log mapping ### 6. `src/adapters/openai.ts` - Reformatted console log mapping for consistency ## Verification All checks pass: - ✅ 174 tests pass - ✅ TypeScript compilation - ✅ ESLint - ✅ Build succeeds --- packages/ui/src/adapters/mcp.ts | 23 +++++++++---------- packages/ui/src/adapters/mock.ts | 19 +++++----------- packages/ui/src/adapters/openai.ts | 10 +++------ packages/ui/src/client.ts | 14 ++---------- packages/ui/src/detection.ts | 36 +++++++++--------------------- packages/ui/src/index.ts | 8 ++----- 6 files changed, 34 insertions(+), 76 deletions(-) diff --git a/packages/ui/src/adapters/mcp.ts b/packages/ui/src/adapters/mcp.ts index ea83595f..a8bbf827 100644 --- a/packages/ui/src/adapters/mcp.ts +++ b/packages/ui/src/adapters/mcp.ts @@ -239,7 +239,10 @@ export class McpAdapter implements ProtocolAdapter { // Keep defaults, overlay with host values when present. const base = this.createDefaultContext(); - const theme = ctx.theme === "dark" ? "dark" : ctx.theme === "light" ? "light" : base.theme; + let theme: "light" | "dark" = base.theme; + if (ctx.theme === "dark" || ctx.theme === "light") { + theme = ctx.theme; + } const displayMode = ctx.displayMode === "fullscreen" || ctx.displayMode === "pip" || ctx.displayMode === "inline" ? (ctx.displayMode as HostContext["displayMode"]) @@ -459,18 +462,12 @@ export class McpAdapter implements ProtocolAdapter { } // Fallback logging when MCP logging unavailable - const logMapping: Record = { - // eslint-disable-next-line no-console - debug: console.debug, - // eslint-disable-next-line no-console - info: console.info, - // eslint-disable-next-line no-console - warning: console.warn, - // eslint-disable-next-line no-console - error: console.error, - }; - // eslint-disable-next-line no-console - const logFn = logMapping[level] ?? console.log; + /* eslint-disable no-console */ + const logFn = + { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[ + level + ] ?? console.log; + /* eslint-enable no-console */ logFn("[MCP Apps]", data); } diff --git a/packages/ui/src/adapters/mock.ts b/packages/ui/src/adapters/mock.ts index 01dadbb8..3c4cd50e 100644 --- a/packages/ui/src/adapters/mock.ts +++ b/packages/ui/src/adapters/mock.ts @@ -178,19 +178,12 @@ export class MockAdapter implements ProtocolAdapter { // === Logging === log(level: string, data: unknown): void { - // Mock adapter uses console for logging - const logMapping: Record = { - // eslint-disable-next-line no-console - debug: console.debug, - // eslint-disable-next-line no-console - info: console.info, - // eslint-disable-next-line no-console - warning: console.warn, - // eslint-disable-next-line no-console - error: console.error, - }; - // eslint-disable-next-line no-console - const logFn = logMapping[level] ?? console.log; + /* eslint-disable no-console */ + const logFn = + { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[ + level + ] ?? console.log; + /* eslint-enable no-console */ logFn("[MockAdapter]", data); } diff --git a/packages/ui/src/adapters/openai.ts b/packages/ui/src/adapters/openai.ts index 35ccc74d..c3fdf817 100644 --- a/packages/ui/src/adapters/openai.ts +++ b/packages/ui/src/adapters/openai.ts @@ -608,14 +608,10 @@ export class OpenAIAdapter implements ProtocolAdapter { log(level: string, data: unknown): void { /* eslint-disable no-console */ const logFn = - { - debug: console.debug, - info: console.info, - warning: console.warn, - error: console.error, - }[level] ?? console.log; + { debug: console.debug, info: console.info, warning: console.warn, error: console.error }[ + level + ] ?? console.log; /* eslint-enable no-console */ - logFn("[ChatGPT Apps]", data); } diff --git a/packages/ui/src/client.ts b/packages/ui/src/client.ts index 1d0d5596..2661c263 100644 --- a/packages/ui/src/client.ts +++ b/packages/ui/src/client.ts @@ -144,20 +144,10 @@ export function createAppsClient( // === Files (Optional) === ...(adapter.uploadFile && { - uploadFile: (file: File) => { - if (adapter.uploadFile) { - return adapter.uploadFile(file); - } - throw new Error("uploadFile not supported"); - }, + uploadFile: adapter.uploadFile.bind(adapter), }), ...(adapter.getFileDownloadUrl && { - getFileDownloadUrl: (fileId: string) => { - if (adapter.getFileDownloadUrl) { - return adapter.getFileDownloadUrl(fileId); - } - throw new Error("getFileDownloadUrl not supported"); - }, + getFileDownloadUrl: adapter.getFileDownloadUrl.bind(adapter), }), // === Resources === diff --git a/packages/ui/src/detection.ts b/packages/ui/src/detection.ts index 0e86909d..9587efdd 100644 --- a/packages/ui/src/detection.ts +++ b/packages/ui/src/detection.ts @@ -6,30 +6,6 @@ import type { DetectedProtocol } from "./types"; -/** - * Check if we're in a ChatGPT sandbox environment - */ -function isChatGPTSandbox(): boolean { - // Check URL patterns that indicate ChatGPT sandbox - const url = window.location.href; - if (url.includes("/api/apps/chatgpt/") || url.includes("chatgpt")) { - return true; - } - - // Check for ChatGPT-specific sandbox proxy indicators - if (url.includes("sandbox-proxy") || url.includes("widget-content")) { - return true; - } - - // Check referrer - const referrer = document.referrer; - if (referrer.includes("chatgpt") || referrer.includes("openai.com")) { - return true; - } - - return false; -} - /** * Detect the current host protocol * @@ -55,7 +31,17 @@ export function detectProtocol(): DetectedProtocol { } // Check for ChatGPT sandbox environment (SDK will be injected) - if (isChatGPTSandbox()) { + const url = window.location.href; + const referrer = document.referrer; + const isChatGPTSandbox = + url.includes("/api/apps/chatgpt/") || + url.includes("chatgpt") || + url.includes("sandbox-proxy") || + url.includes("widget-content") || + referrer.includes("chatgpt") || + referrer.includes("openai.com"); + + if (isChatGPTSandbox) { return "openai"; } diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index ffdf3415..10c688e2 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -107,12 +107,6 @@ export { detectProtocol } from "./detection"; // CLIENT FACTORY (INTERNAL) // ============================================================================= -export { createAppsClient } from "./client"; - -// ============================================================================= -// CLIENT FACTORY -// ============================================================================= - import type { AppsClient, CreateClientOptions, ToolDefs } from "./types"; import type { ProtocolAdapter } from "./adapters/types"; import { detectProtocol } from "./detection"; @@ -122,6 +116,8 @@ import { OpenAIAdapter } from "./adapters/openai"; import { createAppsClient } from "./client"; import { clientDebugLogger } from "./debug/logger"; +export { createAppsClient } from "./client"; + /** * Create an adapter based on detected or forced protocol */