-
Notifications
You must be signed in to change notification settings - Fork 0
Add OpenRouter GLM-V CUA support #45
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
Open
rgarcia
wants to merge
2
commits into
main
Choose a base branch
from
hypeship/add-openrouter-glm-v
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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
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,108 @@ | ||
| import { | ||
| computerTools, | ||
| createCuaActionToolExecutors, | ||
| type ComputerToolsOptions, | ||
| type CuaAction, | ||
| type CuaToolExecutorSpec, | ||
| } from "../common"; | ||
| import type { ComputerToolCoordinateSystem, CuaProviderModule } from "../common"; | ||
|
|
||
| export { | ||
| CUA_ACTION_TYPES as OPENROUTER_CUA_ACTION_TYPES, | ||
| computerTools, | ||
| createCuaActionSchema as createActionSchema, | ||
| } from "../common"; | ||
| export type { | ||
| ComputerToolsOptions, | ||
| CuaAction as OpenRouterAction, | ||
| } from "../common"; | ||
|
|
||
| // OpenRouter exposes the GLM V models through its OpenAI-compatible Chat | ||
| // Completions API. The model-specific Z.AI docs describe function calling, but | ||
| // not a provider-native browser coordinate system, so CUA uses screenshot pixel | ||
| // coordinates with the standard canonical action tools. | ||
| export function coordinateSystem(): ComputerToolCoordinateSystem { | ||
| return { type: "pixel" }; | ||
| } | ||
|
|
||
| export const OPENROUTER_COMPUTER_INSTRUCTIONS = `You control a Kernel cloud browser through individual browser tools. Use screenshot pixel coordinates for tool calls, and request screenshots or URL reads when state changes.`; | ||
|
|
||
| export function buildOpenRouterSystemPrompt(opts: { suffix?: string } = {}): string { | ||
| return [OPENROUTER_COMPUTER_INSTRUCTIONS, opts.suffix].filter(Boolean).join("\n\n"); | ||
| } | ||
|
|
||
| /** Build OpenRouter execution adapters and normalize GLM tuple-shaped coordinates before browser execution. */ | ||
| export function computerToolExecutors(options: ComputerToolsOptions = {}): CuaToolExecutorSpec[] { | ||
| return createCuaActionToolExecutors(options.actions).map((executor) => ({ | ||
| ...executor, | ||
| toActions(args: unknown): CuaAction[] { | ||
| return executor.toActions(args).map(normalizeOpenRouterAction); | ||
| }, | ||
| })); | ||
| } | ||
|
|
||
| /** Normalize OpenRouter GLM-V coordinate arguments into canonical CUA numeric coordinates. */ | ||
| export function normalizeOpenRouterAction(action: CuaAction): CuaAction { | ||
| switch (action.type) { | ||
| case "click": | ||
| case "double_click": | ||
| case "mouse_down": | ||
| case "mouse_up": | ||
| case "move": { | ||
| const { x, y, ...rest } = action; | ||
| return { ...rest, ...normalizeRequiredPoint(action.type, x, y) }; | ||
| } | ||
| case "scroll": { | ||
| const { x, y, ...rest } = action; | ||
| return { ...rest, ...normalizeOptionalPoint(action.type, x, y) }; | ||
| } | ||
| case "drag": | ||
| return { | ||
| ...action, | ||
| path: action.path.map((point) => normalizeRequiredPoint("drag path", point.x, point.y)), | ||
| }; | ||
| default: | ||
| return action; | ||
| } | ||
| } | ||
|
|
||
| function normalizeRequiredPoint(label: string, xValue: unknown, yValue: unknown): { x: number; y: number } { | ||
| const x = coordinateNumber(xValue, 0); | ||
| const y = coordinateNumber(yValue, 1) ?? coordinateTupleNumber(xValue, 1); | ||
| if (x === undefined || y === undefined) throw new Error(`invalid OpenRouter ${label} coordinates`); | ||
| return { x, y }; | ||
| } | ||
|
|
||
| function normalizeOptionalPoint(label: string, xValue: unknown, yValue: unknown): { x?: number; y?: number } { | ||
| const x = coordinateNumber(xValue, 0); | ||
| const yFromTuple = coordinateTupleNumber(xValue, 1); | ||
| const y = coordinateNumber(yValue, 1) ?? yFromTuple; | ||
| if (xValue !== undefined && x === undefined) throw new Error(`invalid OpenRouter ${label} x coordinate`); | ||
| if ((yValue !== undefined || yFromTuple !== undefined) && y === undefined) throw new Error(`invalid OpenRouter ${label} y coordinate`); | ||
| return { | ||
| ...(x !== undefined ? { x } : {}), | ||
| ...(y !== undefined ? { y } : {}), | ||
| }; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| function coordinateTupleNumber(value: unknown, index: number): number | undefined { | ||
| if (!Array.isArray(value)) return undefined; | ||
| const next = value[index]; | ||
| return typeof next === "number" && Number.isFinite(next) ? next : undefined; | ||
| } | ||
|
|
||
| export const providerModule = { | ||
| toolDefinitions: computerTools, | ||
| toolExecutors: computerToolExecutors, | ||
| coordinateSystem, | ||
| buildSystemPrompt: buildOpenRouterSystemPrompt, | ||
| } satisfies CuaProviderModule; | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.