From f80aa1c0e57c10f05062320587134aad36b8331c Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Thu, 15 Oct 2026 14:06:19 +0000 Subject: [PATCH] docs: document mobile session hooks, plugin test helpers, and viewport detection --- backend/src/services/opencode-manager-tool-plugin.ts | 4 ++++ backend/test/helpers/opencode-plugin-context.ts | 5 +++++ backend/test/services/opencode-manager-tool-plugin.test.ts | 1 + frontend/src/api/opencode.ts | 1 + frontend/src/hooks/useMobile.ts | 3 +++ 5 files changed, 14 insertions(+) diff --git a/backend/src/services/opencode-manager-tool-plugin.ts b/backend/src/services/opencode-manager-tool-plugin.ts index 4d68ca15..c48758c9 100644 --- a/backend/src/services/opencode-manager-tool-plugin.ts +++ b/backend/src/services/opencode-manager-tool-plugin.ts @@ -147,6 +147,7 @@ function buildManagerToolDescription(): string { .join('\n') } +/** Builds the standalone manager plugin with API actions and mobile session hooks. */ export function buildManagerToolPluginSource(id: string): string { return `var REQUEST_TIMEOUT_MS = ${MANAGER_TOOL_REQUEST_TIMEOUT_MS} @@ -174,6 +175,7 @@ var MOBILE_OUTPUT_INSTRUCTIONS = ${JSON.stringify(MANAGER_MOBILE_OUTPUT_INSTRUCT var MOBILE_SESSIONS = new Set() +/** Updates mobile mode from explicit client metadata, preserving it when metadata is absent. */ function trackMobileSession(event) { var client = event.metadata && event.metadata[CLIENT_METADATA_KEY] if (client === MOBILE_CLIENT) { @@ -183,6 +185,7 @@ function trackMobileSession(event) { } } +/** Appends mobile reply guidance to the context of a tracked mobile session. */ function addMobileOutputInstructions(event) { if (MOBILE_SESSIONS.has(event.sessionID)) { event.system.push({ type: 'text', text: MOBILE_OUTPUT_INSTRUCTIONS }) @@ -329,6 +332,7 @@ async function runAction(input, context) { export default { id: '${id}', + /** Registers session hooks and exposes the manager tool through the plugin context. */ async setup(ctx) { await ctx.session.hook('prompt', trackMobileSession) await ctx.session.hook('context', addMobileOutputInstructions) diff --git a/backend/test/helpers/opencode-plugin-context.ts b/backend/test/helpers/opencode-plugin-context.ts index d26f8847..6d9fbbbc 100644 --- a/backend/test/helpers/opencode-plugin-context.ts +++ b/backend/test/helpers/opencode-plugin-context.ts @@ -106,6 +106,7 @@ function createRegistry(name: string) { } } +/** Creates a test plugin context with hook registries, event triggers, and tool transforms. */ function createPluginContext() { const shellCreateBefore = createRegistry('create.before') const toolExecuteAfter = createRegistry('execute.after') @@ -138,6 +139,7 @@ function createPluginContext() { }, }, session: { + /** Registers prompt or context callbacks, rejecting unsupported session hook names. */ hook: async (name, callback) => { if (name === 'prompt') return sessionPrompt.register(callback as SessionPromptHookCallback) if (name === 'context') return sessionContext.register(callback as SessionContextHookCallback) @@ -151,7 +153,9 @@ function createPluginContext() { triggerShellCreateBefore: (event: ShellCreateBeforeEvent) => shellCreateBefore.trigger(event), triggerToolExecuteAfter: (event: ToolExecuteAfterEvent) => toolExecuteAfter.trigger(event), triggerPermissionEvaluate: (event: PermissionEvaluateEvent) => permissionEvaluate.trigger(event), + /** Dispatches a prompt event to the registered session callbacks. */ triggerSessionPrompt: (event: SessionPromptEvent) => sessionPrompt.trigger(event), + /** Dispatches a context event so registered callbacks can update it. */ triggerSessionContext: (event: SessionContextEvent) => sessionContext.trigger(event), registeredTools: () => { const tools: GeneratedTool[] = [] @@ -164,6 +168,7 @@ function createPluginContext() { let importCount = 0 +/** Imports a fresh plugin instance, validates its definition, and runs setup in the test context. */ export async function loadGeneratedPlugin(file: string): Promise { importCount += 1 const imported = (await import(`${pathToFileURL(file).href}?ocm-test=${importCount}`)) as { default?: unknown } diff --git a/backend/test/services/opencode-manager-tool-plugin.test.ts b/backend/test/services/opencode-manager-tool-plugin.test.ts index b1142fe3..65c3e590 100644 --- a/backend/test/services/opencode-manager-tool-plugin.test.ts +++ b/backend/test/services/opencode-manager-tool-plugin.test.ts @@ -33,6 +33,7 @@ type JsonSchema = { minLength?: number } +/** Loads the installed manager plugin from the supplied configuration home for hook tests. */ async function loadManagerPlugin(configHome: string): Promise { return loadGeneratedPlugin(path.join(getOpenCodePluginDir(configHome), 'ocm-manager.js')) } diff --git a/frontend/src/api/opencode.ts b/frontend/src/api/opencode.ts index 9d96c2f7..0630b25d 100644 --- a/frontend/src/api/opencode.ts +++ b/frontend/src/api/opencode.ts @@ -183,6 +183,7 @@ export async function switchSessionAgent(sessionID: string, agent: string): Prom await callOpenCode((api) => api.session.switchAgent({ sessionID, agent })) } +/** Sends a session prompt with client metadata derived from the current viewport. */ export async function sendPrompt(input: SendPromptInput): Promise { return callOpenCode((api) => api.session.prompt({ diff --git a/frontend/src/hooks/useMobile.ts b/frontend/src/hooks/useMobile.ts index 56e600cc..63b7ceb6 100644 --- a/frontend/src/hooks/useMobile.ts +++ b/frontend/src/hooks/useMobile.ts @@ -1,14 +1,17 @@ import { useState, useEffect, useCallback, useRef, type CSSProperties } from 'react' import { useSwipeNavigation } from '@/contexts/SwipeNavigationContext' +/** Returns whether the viewport is narrower than 768 pixels, or false without a browser window. */ export function isMobileViewport(): boolean { return typeof window !== 'undefined' ? window.innerWidth < 768 : false } +/** Tracks whether the current viewport is mobile, updating the value on window resize. */ export function useMobile() { const [isMobile, setIsMobile] = useState(isMobileViewport) useEffect(() => { + /** Refreshes the mobile state from the current viewport width. */ const checkMobile = () => { setIsMobile(isMobileViewport()) }