Files
openmotion/shared/webmcp-tools.ts
open-design-crew[bot] 2baa19acac refactor(cloud): remove hardcoded production Studio subdomain from runtime and install guide (#1184)
* refactor(cloud): remove hardcoded production Studio subdomain from runtime and install guide

Avoid tying the server, Studio frontend, WebMCP tools, and onboarding/install guides to a fixed production Studio subdomain so domain switches only require deployment configuration updates:

- Replace hardcoded Studio origin in docs/install.md with the {{ORIGIN}} placeholder rendered from APP_PUBLIC_ORIGIN, and require a non-empty publicOrigin in cloud-onboarding.ts instead of falling back to a hardcoded host.
- Use relative paths instead of a hardcoded production Studio origin fallback in campaignPricingHref and WebMCP cloud tool descriptors when no origin is available.
- Reference the active deployment <ORIGIN> in onboarding and entry Skill docs instead of hardcoding a specific Studio subdomain.

* docs(onboarding): add signed-config bootstrap origin discovery for GitHub readers

Keep the unrendered GitHub docs/install.md and Skill entry points self-contained without hardcoding a Studio subdomain:

- Document dynamic bootstrap discovery via https://releases.open-design.ai/open-motion/desktop/config/stable.json (payload.studioUrl) when reading unrendered GitHub docs with no user-supplied origin.
- Extend tests/cloud/onboarding.test.ts to assert bootstrap origin discovery in docs/install.md, skills/openmotion/SKILL.md, and .codex-plugin/onboarding/SKILL.md.

---------

Co-authored-by: heggria <bshengtao@gmail.com>
2026-11-29 21:12:46 -08:00

55 lines
3.1 KiB
TypeScript

import { ToolInputError } from './tool-input-error.ts';
import { cloudPhase1Tools, phase1Tools, validateInput } from './phase1-tools.ts';
import { assetReadToolResult } from './asset-library.ts';
function failure(message: string, applied: boolean | 'unknown', fallback = 'WEBMCP_CALL_FAILED', validation?: { invalid_field?: string; validation_rule?: string }) {
const code = /^([A-Z][A-Z_]+)(?::|$)/.exec(message)?.[1] ?? fallback;
return { isError: true, error: { code, message, applied, ...(validation?.invalid_field ? { invalid_field: validation.invalid_field, validation_rule: validation.validation_rule } : {}) } };
}
export interface OpenMotionWebMcpToolOptions {
readonly cloud?: boolean;
readonly publicOrigin?: string;
}
/** Page registration reuses the public contract and the existing service authority. */
export function buildOpenMotionWebMcpTools(
request: (url: string, init?: RequestInit) => Promise<Response>,
options?: OpenMotionWebMcpToolOptions,
) {
const resolvedOrigin =
options?.publicOrigin ?? (typeof location !== 'undefined' && location.origin ? location.origin : undefined);
const sourceTools = options?.cloud
? cloudPhase1Tools(resolvedOrigin)
: phase1Tools;
// Game tools serve the user's external Agent over MCP; the in-page WebMCP surface does not expose them.
return sourceTools.filter(tool => !tool.name.startsWith('openmotion_game_')).map(tool => ({
name: tool.name,
title: tool.title,
description: tool.description + ' WebMCP returns the existing service result on success, or isError=true with error={code,message,applied}. After an uncertain write, read the project or original job before retrying; retain the original requestId.',
inputSchema: tool.inputSchema,
annotations: { readOnlyHint: tool.annotations.readOnlyHint, untrustedContentHint: true },
execute: async (input: object, options?: { signal: AbortSignal }) => {
let dispatched = false;
try {
options?.signal.throwIfAborted();
validateInput(tool.inputSchema, input);
const body = JSON.stringify({ name: tool.name, arguments: input });
dispatched = true;
const response = await request('/motion-api/phase1/call', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body, signal: options?.signal,
});
const result = await response.json();
if (!response.ok) return { ...result, ...failure(typeof result.error === 'string' ? result.error : `HTTP ${response.status}`,
typeof result.applied === 'boolean' ? result.applied : 'unknown', result.code ?? 'WEBMCP_CALL_FAILED', result) };
return tool.name === 'openmotion_assets_use' && (input as { action?: string }).action === 'read'
? assetReadToolResult(result) : result;
} catch (error) {
return failure(error instanceof Error ? error.message : String(error), dispatched ? 'unknown' : false,
options?.signal.aborted ? 'ABORTED' : dispatched ? 'WEBMCP_TRANSPORT_ERROR' : 'INVALID_ARGUMENT',
error instanceof ToolInputError ? error : undefined);
}
},
}));
}