Skip to content

Add OpenRouter GLM-V CUA support#45

Open
rgarcia wants to merge 2 commits into
mainfrom
hypeship/add-openrouter-glm-v
Open

Add OpenRouter GLM-V CUA support#45
rgarcia wants to merge 2 commits into
mainfrom
hypeship/add-openrouter-glm-v

Conversation

@rgarcia

@rgarcia rgarcia commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add OpenRouter as a curated CUA provider and expose openrouter:z-ai/glm-5v-turbo / openrouter:z-ai/glm-4.6v
  • wire provider runtime, env key lookup, CLI docs/help, supported-model docs, and CI OPENROUTER_API_KEY env
  • add OpenRouter provider tests plus live integration/e2e coverage, including coordinate normalization for GLM-4.6V tuple-shaped arguments

Tests

  • npm run build --workspace @onkernel/cua-ai
  • npm test --workspace @onkernel/cua-ai
  • env -u OPENAI_API_KEY -u ANTHROPIC_API_KEY -u GOOGLE_API_KEY -u GEMINI_API_KEY -u TZAFON_API_KEY -u YUTORI_API_KEY OPENROUTER_API_KEY=*** npm run test:integration --workspace @onkernel/cua-ai -- test/computer-tool.integration.test.ts
  • npm run build --workspace @onkernel/cua-agent && npx tsc -b
  • env -u OPENAI_API_KEY -u ANTHROPIC_API_KEY -u GOOGLE_API_KEY -u GEMINI_API_KEY -u TZAFON_API_KEY -u YUTORI_API_KEY OPENROUTER_API_KEY=*** CUA_E2E_LIVE=1 npm test --workspace @onkernel/cua-agent -- test/e2e.live.test.ts -t openrouter
  • npm test --workspace @onkernel/cua-agent -- --exclude "**/*.live.test.ts"
  • npm test --workspace @onkernel/cua-cli
  • git diff --check

OpenRouter live tests require the OPENROUTER_API_KEY secret. The agent e2e OpenRouter case is gated by CUA_E2E_OPENROUTER.


Note

Medium Risk
Adds a new model provider path and coordinate normalization on the browser execution hot path; mistakes could mis-click or break GLM-V tool calls, but scope is additive with dedicated tests and no changes to existing providers' payload hooks.

Overview
Adds OpenRouter as a first-class CUA provider so agents and the CLI can drive openrouter:z-ai/glm-5v-turbo and openrouter:z-ai/glm-4.6v via OpenAI-compatible chat completions, canonical browser tools, and OPENROUTER_API_KEY auth.

The new openrouter provider module uses pixel coordinates and wraps tool executors with normalizeOpenRouterAction so GLM-V models that emit tuple-shaped x/y arguments still map to numeric click/scroll/drag actions before Kernel execution. Model catalog, resolveCuaRuntimeSpec, and API-key helpers are extended accordingly (no payload middleware, unlike Tzafon/Yutori).

CI passes OPENROUTER_API_KEY into integration and agent live e2e jobs; agent smoke adds an OpenRouter case gated by CUA_E2E_OPENROUTER. Integration tests assert click tool calls through executors (not raw arguments) for both GLM-V refs. Docs, quickstart, and cua help/README document the new model refs and env vars.

Reviewed by Cursor Bugbot for commit faf93f5. Bugbot is set up for automated code reviews on this repo. Configure here.

@rgarcia rgarcia marked this pull request as ready for review June 27, 2026 17:11

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 4 potential issues.

Fix All in Cursor

Bugbot Autofix prepared fixes for all 4 issues found in the latest run.

  • ✅ Fixed: OpenRouter y fallback copies scalar x
    • I changed y normalization to use tuple-only fallback from x so scalar x values no longer get copied into y when y is missing.
  • ✅ Fixed: Arrays pass through coordinate normalization
    • Normalization now either converts tuple/scalar coordinates to finite numbers, clears invalid optional scroll coordinates, or throws for invalid required coordinates so arrays are never forwarded.
  • ✅ Fixed: Duplicate OpenRouter integration test titles
    • I updated the integration test title template to include modelRef, making the two OpenRouter cases uniquely identifiable in Vitest output.
  • ✅ Fixed: Exported OpenRouter APIs lack TSDoc
    • I added TSDoc comments to the exported OpenRouter executor override and normalization helper, including context about the GLM tuple behavior and a provider doc link.

Create PR

Or push these changes by commenting:

@cursor push 167c37b0d4
Preview (167c37b0d4)
diff --git a/packages/ai/src/providers/openrouter/index.ts b/packages/ai/src/providers/openrouter/index.ts
--- a/packages/ai/src/providers/openrouter/index.ts
+++ b/packages/ai/src/providers/openrouter/index.ts
@@ -31,6 +31,13 @@
 	return [OPENROUTER_COMPUTER_INSTRUCTIONS, opts.suffix].filter(Boolean).join("\n\n");
 }
 
+/**
+ * Wrap the shared canonical executors with OpenRouter-specific normalization.
+ *
+ * OpenRouter's GLM-V models can emit tuple-shaped coordinates (`[x, y]`) for
+ * scalar fields, so this override unwraps those payloads before browser
+ * execution.
+ */
 export function computerToolExecutors(options: ComputerToolsOptions = {}): CuaToolExecutorSpec[] {
 	return createCuaActionToolExecutors(options.actions).map((executor) => ({
 		...executor,
@@ -40,6 +47,12 @@
 	}));
 }
 
+/**
+ * Normalize OpenRouter canonical actions into finite scalar coordinates.
+ *
+ * Source: OpenRouter exposes Z.AI GLM-V models that can return tuple-like
+ * coordinate arguments; see https://openrouter.ai/z-ai/glm-4.6v.
+ */
 export function normalizeOpenRouterAction(action: CuaAction): CuaAction {
 	// GLM-4.6V via OpenRouter can return [x, y] tuples in scalar coordinate
 	// fields even when the function schema asks for numbers.
@@ -48,25 +61,35 @@
 		case "double_click":
 		case "mouse_down":
 		case "mouse_up":
-		case "move":
+		case "move": {
+			const x = coordinateNumber(action.x, 0);
+			const y = coordinateNumber(action.y, 1) ?? coordinateTupleNumber(action.x, 1);
 			return {
 				...action,
-				x: coordinateNumber(action.x, 0) ?? action.x,
-				y: coordinateNumber(action.y, 1) ?? coordinateNumber(action.x, 1) ?? action.y,
+				x: requiredCoordinate(x, action.type, "x"),
+				y: requiredCoordinate(y, action.type, "y"),
 			};
-		case "scroll":
+		}
+		case "scroll": {
+			const x = coordinateNumber(action.x, 0);
+			const y = coordinateNumber(action.y, 1) ?? coordinateTupleNumber(action.x, 1);
 			return {
 				...action,
-				x: coordinateNumber(action.x, 0) ?? action.x,
-				y: coordinateNumber(action.y, 1) ?? coordinateNumber(action.x, 1) ?? action.y,
+				x,
+				y,
 			};
+		}
 		case "drag":
 			return {
 				...action,
-				path: action.path.map((point) => ({
-					x: coordinateNumber(point.x, 0) ?? point.x,
-					y: coordinateNumber(point.y, 1) ?? coordinateNumber(point.x, 1) ?? point.y,
-				})),
+				path: action.path.map((point) => {
+					const x = coordinateNumber(point.x, 0);
+					const y = coordinateNumber(point.y, 1) ?? coordinateTupleNumber(point.x, 1);
+					return {
+						x: requiredCoordinate(x, action.type, "x"),
+						y: requiredCoordinate(y, action.type, "y"),
+					};
+				}),
 			};
 		default:
 			return action;
@@ -74,14 +97,28 @@
 }
 
 function coordinateNumber(value: unknown, index: number): number | undefined {
-	if (typeof value === "number" && Number.isFinite(value)) return value;
-	if (Array.isArray(value)) {
-		const next = value[index] ?? value[0];
-		return typeof next === "number" && Number.isFinite(next) ? next : undefined;
-	}
-	return undefined;
+	return finiteNumber(value) ?? coordinateTupleNumber(value, index);
 }
 
+function coordinateTupleNumber(value: unknown, index: number): number | undefined {
+	if (!Array.isArray(value)) return undefined;
+	const next = value[index] ?? value[0];
+	return finiteNumber(next);
+}
+
+function finiteNumber(value: unknown): number | undefined {
+	return typeof value === "number" && Number.isFinite(value) ? value : undefined;
+}
+
+function requiredCoordinate(
+	value: number | undefined,
+	actionType: CuaAction["type"],
+	axis: "x" | "y",
+): number {
+	if (value !== undefined) return value;
+	throw new Error(`OpenRouter ${actionType} action is missing a finite ${axis} coordinate`);
+}
+
 export const providerModule = {
 	toolDefinitions: computerTools,
 	toolExecutors: computerToolExecutors,

diff --git a/packages/ai/test/computer-tool.integration.test.ts b/packages/ai/test/computer-tool.integration.test.ts
--- a/packages/ai/test/computer-tool.integration.test.ts
+++ b/packages/ai/test/computer-tool.integration.test.ts
@@ -130,7 +130,7 @@
 		const ciEnabled = !c.ciOptInEnvVar || !process.env.CI || process.env[c.ciOptInEnvVar] === "1";
 		const test = hasKey ? it : it.skip;
 
-		(ciEnabled ? test : it.skip)(`${c.provider} returns a canonical click tool call`, async () => {
+		(ciEnabled ? test : it.skip)(`${c.modelRef} returns a canonical click tool call`, async () => {
 			const model = getCuaModel(c.modelRef);
 			const context = await buildContext(c.tools);
 			const response = await complete(model, context, {

diff --git a/packages/ai/test/openrouter-provider.test.ts b/packages/ai/test/openrouter-provider.test.ts
--- a/packages/ai/test/openrouter-provider.test.ts
+++ b/packages/ai/test/openrouter-provider.test.ts
@@ -12,6 +12,41 @@
 		expect(openrouter.normalizeOpenRouterAction(action)).toEqual({ type: "click", x: 929, y: 294 });
 	});
 
+	it("does not copy scalar x into missing y", () => {
+		const action = {
+			type: "click",
+			x: 929,
+		} as unknown as openrouter.OpenRouterAction;
+
+		expect(() => openrouter.normalizeOpenRouterAction(action)).toThrow(/missing a finite y coordinate/);
+	});
+
+	it("rejects invalid coordinate arrays for required actions", () => {
+		const action = {
+			type: "click",
+			x: ["bad", "data"],
+			y: ["still", "bad"],
+		} as unknown as openrouter.OpenRouterAction;
+
+		expect(() => openrouter.normalizeOpenRouterAction(action)).toThrow(/missing a finite x coordinate/);
+	});
+
+	it("drops invalid scroll coordinate arrays instead of passing arrays through", () => {
+		const action = {
+			type: "scroll",
+			x: ["bad", "data"],
+			y: ["still", "bad"],
+			scroll_y: 120,
+		} as unknown as openrouter.OpenRouterAction;
+
+		expect(openrouter.normalizeOpenRouterAction(action)).toEqual({
+			type: "scroll",
+			x: undefined,
+			y: undefined,
+			scroll_y: 120,
+		});
+	});
+
 	it("applies the coordinate normalization through tool executors", () => {
 		const click = openrouter.computerToolExecutors({ actions: ["click"] })[0]!;
 		expect(click.toActions({ x: [929, 294], y: [294, 294] })).toEqual([{ type: "click", x: 929, y: 294 }]);

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 72565d2. Configure here.

Comment thread packages/ai/src/providers/openrouter/index.ts Outdated
Comment thread packages/ai/src/providers/openrouter/index.ts Outdated
Comment thread packages/ai/test/computer-tool.integration.test.ts
Comment thread packages/ai/src/providers/openrouter/index.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant