@abhishek944/pi-classifier 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/COMPUTER_USE.md +117 -0
- package/README.md +82 -6
- package/dist/box-tool.d.ts +26 -0
- package/dist/box-tool.js +109 -0
- package/dist/box.d.ts +32 -0
- package/dist/box.js +148 -0
- package/dist/classify.d.ts +3 -0
- package/dist/classify.js +1 -1
- package/dist/computer-use-state.d.ts +9 -0
- package/dist/computer-use-state.js +160 -0
- package/dist/computer-use-tool.d.ts +52 -0
- package/dist/computer-use-tool.js +78 -0
- package/dist/computer-use-types.d.ts +132 -0
- package/dist/computer-use-types.js +1 -0
- package/dist/computer-use.d.ts +22 -0
- package/dist/computer-use.js +223 -0
- package/dist/config.js +4 -0
- package/dist/extension.d.ts +38 -10
- package/dist/extension.js +36 -11
- package/dist/image-content.d.ts +12 -0
- package/dist/image-content.js +90 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +2 -0
- package/dist/inputs.d.ts +9 -2
- package/dist/inputs.js +154 -130
- package/dist/providers.js +90 -87
- package/dist/types.d.ts +30 -12
- package/dist/validation.js +63 -38
- package/package.json +2 -1
- package/src/box-tool.ts +145 -0
- package/src/box.ts +253 -0
- package/src/choices.ts +2 -1
- package/src/classify.ts +4 -2
- package/src/computer-use-state.ts +145 -0
- package/src/computer-use-tool.ts +76 -0
- package/src/computer-use-types.ts +127 -0
- package/src/computer-use.ts +216 -0
- package/src/config.ts +4 -0
- package/src/extension.ts +39 -12
- package/src/image-content.ts +77 -0
- package/src/index.ts +10 -0
- package/src/inputs.ts +163 -185
- package/src/providers.ts +92 -105
- package/src/types.ts +20 -9
- package/src/validation.ts +62 -94
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { ClassifierError, isRecord } from "./types.js";
|
|
2
|
+
export const KEY_NAMES = ["return", "tab", "escape", "up", "down", "left", "right", "space", "home", "end", "pageup", "pagedown"];
|
|
3
|
+
const SECRET = /secure|password|passcode|one.time|\botp\b|secret|security.code|api.key|access.token|auth.token|recovery.code|credit.card|card.number|\bcvv\b/i;
|
|
4
|
+
const EDITABLE = /^(AXTextField|AXTextArea|AXComboBox|textbox|combobox|text field|text area)$/i;
|
|
5
|
+
const CLICKABLE = /^(AXButton|AXCheckBox|AXRadioButton|AXMenuItem|AXLink|AXPopUpButton|AXTab|AXDisclosureTriangle|AXRow|button|checkbox|radio|menuitem|link|tab|switch)$/i;
|
|
6
|
+
export function validateComputerParams(params) {
|
|
7
|
+
const invalid = () => { throw new ClassifierError("input", "Invalid computer-use goal, exact target, controls, values, expectations or limits."); };
|
|
8
|
+
if (!isRecord(params) || typeof params.goal !== "string" || !params.goal.trim() || params.goal.length > 4096 ||
|
|
9
|
+
!isRecord(params.target) || !Number.isSafeInteger(params.target.pid) || params.target.pid < 1 ||
|
|
10
|
+
!Number.isSafeInteger(params.target.window_id) || params.target.window_id < 0 ||
|
|
11
|
+
!Array.isArray(params.controls) || params.controls.length < 1 || params.controls.length > 120)
|
|
12
|
+
invalid();
|
|
13
|
+
const ids = new Set();
|
|
14
|
+
for (const c of params.controls) {
|
|
15
|
+
if (!isRecord(c) || typeof c.id !== "string" || !/^[a-zA-Z0-9_-]{1,64}$/.test(c.id) || ids.has(c.id) ||
|
|
16
|
+
!isRecord(c.selector) || typeof c.selector.role !== "string" || !c.selector.role || c.selector.role.length > 128 ||
|
|
17
|
+
typeof c.selector.label !== "string" || !c.selector.label.trim() || c.selector.label.length > 512 ||
|
|
18
|
+
!["click", "fill", "key"].includes(c.operation))
|
|
19
|
+
invalid();
|
|
20
|
+
ids.add(c.id);
|
|
21
|
+
if (c.operation === "fill" && (typeof c.valueKey !== "string" || !/^[a-zA-Z0-9_-]{1,64}$/.test(c.valueKey)))
|
|
22
|
+
invalid();
|
|
23
|
+
if (c.operation === "key" && !KEY_NAMES.includes(c.key))
|
|
24
|
+
invalid();
|
|
25
|
+
if (SECRET.test(c.selector.role + " " + c.selector.label))
|
|
26
|
+
throw new ClassifierError("input", "Credential and secure controls must remain with the user, outside the classifier loop.");
|
|
27
|
+
}
|
|
28
|
+
if (params.values !== undefined && (!isRecord(params.values) || Object.keys(params.values).length > 64 ||
|
|
29
|
+
Object.entries(params.values).some(([k, v]) => !/^[a-zA-Z0-9_-]{1,64}$/.test(k) || typeof v !== "string" || v.length > 4096 || /[\r\n\t\x00-\x1f]/.test(v))))
|
|
30
|
+
invalid();
|
|
31
|
+
if (params.expect !== undefined && (!Array.isArray(params.expect) || params.expect.length < 1 || params.expect.length > 8))
|
|
32
|
+
invalid();
|
|
33
|
+
for (const p of params.expect ?? []) {
|
|
34
|
+
const e = p?.element;
|
|
35
|
+
if (!isRecord(e) || !isRecord(e.selector) || typeof e.selector.role !== "string" || !e.selector.role ||
|
|
36
|
+
typeof e.selector.label_contains !== "string" || !e.selector.label_contains ||
|
|
37
|
+
e.selector.role.length > 128 || e.selector.label_contains.length > 512 ||
|
|
38
|
+
(e.exists !== undefined && e.exists !== true) ||
|
|
39
|
+
(e.value_equals !== undefined && (typeof e.value_equals !== "string" || e.value_equals.length > 4096)) ||
|
|
40
|
+
(e.enabled !== undefined && typeof e.enabled !== "boolean") ||
|
|
41
|
+
(e.selected !== undefined && typeof e.selected !== "boolean") ||
|
|
42
|
+
[e.exists, e.value_equals, e.enabled, e.selected].every((v) => v === undefined))
|
|
43
|
+
invalid();
|
|
44
|
+
}
|
|
45
|
+
if ((params.context !== undefined && (typeof params.context !== "string" || params.context.length > 4096)) ||
|
|
46
|
+
(params.previousActions !== undefined && (!Array.isArray(params.previousActions) || params.previousActions.length > 20 || params.previousActions.some((v) => typeof v !== "string" || v.length > 256))) ||
|
|
47
|
+
(params.maxActions !== undefined && (!Number.isSafeInteger(params.maxActions) || params.maxActions < 1 || params.maxActions > 100)) ||
|
|
48
|
+
(params.timeoutMs !== undefined && (!Number.isSafeInteger(params.timeoutMs) || params.timeoutMs < 1000 || params.timeoutMs > 300000)) ||
|
|
49
|
+
(params.deliveryMode !== undefined && !["background", "foreground"].includes(params.deliveryMode)))
|
|
50
|
+
invalid();
|
|
51
|
+
}
|
|
52
|
+
export function driverState(reply) {
|
|
53
|
+
if (reply.isError)
|
|
54
|
+
throw new ClassifierError("driver", "Driver call failed or was blocked. Inspect the target and permissions before resuming.");
|
|
55
|
+
if (!isRecord(reply.structuredContent))
|
|
56
|
+
throw new ClassifierError("driver", "Driver returned no structured state; no input was attempted from that response.");
|
|
57
|
+
return reply.structuredContent;
|
|
58
|
+
}
|
|
59
|
+
export function observation(reply, target) {
|
|
60
|
+
const state = driverState(reply);
|
|
61
|
+
if (state.pid !== target.pid || state.window_id !== target.window_id || !Array.isArray(state.elements))
|
|
62
|
+
throw new ClassifierError("driver", "Driver observation did not establish the exact requested window and element list.");
|
|
63
|
+
const elements = [];
|
|
64
|
+
const menuIndices = new Set();
|
|
65
|
+
for (const raw of state.elements) {
|
|
66
|
+
if (!isRecord(raw))
|
|
67
|
+
continue;
|
|
68
|
+
if (raw.role === "AXMenuBar" || (typeof raw.parent_index === "number" && menuIndices.has(raw.parent_index))) {
|
|
69
|
+
if (typeof raw.element_index === "number")
|
|
70
|
+
menuIndices.add(raw.element_index);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const raw of state.elements.slice(0, 200)) {
|
|
74
|
+
if (isRecord(raw) && typeof raw.element_index === "number" && menuIndices.has(raw.element_index))
|
|
75
|
+
continue;
|
|
76
|
+
if (!isRecord(raw) || typeof raw.role !== "string" || SECRET.test(raw.role + " " + String(raw.label ?? "")))
|
|
77
|
+
continue;
|
|
78
|
+
const e = {
|
|
79
|
+
role: raw.role.slice(0, 128), label: typeof raw.label === "string" ? raw.label.slice(0, 1024) : "",
|
|
80
|
+
actions: Array.isArray(raw.actions) ? raw.actions.filter((v) => typeof v === "string").slice(0, 20) : [],
|
|
81
|
+
index: typeof raw.element_index === "number" ? raw.element_index : -1,
|
|
82
|
+
};
|
|
83
|
+
if (typeof raw.value === "string")
|
|
84
|
+
e.value = raw.value.slice(0, 4096);
|
|
85
|
+
if (typeof raw.enabled === "boolean")
|
|
86
|
+
e.enabled = raw.enabled;
|
|
87
|
+
if (typeof raw.selected === "boolean")
|
|
88
|
+
e.selected = raw.selected;
|
|
89
|
+
if (typeof raw.element_token === "string" && /^s[0-9a-f]{8}:[0-9]+$/.test(raw.element_token))
|
|
90
|
+
e.token = raw.element_token;
|
|
91
|
+
elements.push(e);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
elements, truncated: state.truncated === true || state.elements.length > 200,
|
|
95
|
+
degraded: state.degraded === true || Boolean(state.degraded_reason),
|
|
96
|
+
title: typeof state.window_title === "string" ? state.window_title.slice(0, 512) : "",
|
|
97
|
+
// Driver indexes actionable rows only; retain passive state without the global menu subtree.
|
|
98
|
+
text: windowText(state.tree_markdown),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Root order changes when an app backgrounds; select the window branch, not a prefix. */
|
|
102
|
+
function windowText(markdown) {
|
|
103
|
+
if (typeof markdown !== "string")
|
|
104
|
+
return "";
|
|
105
|
+
let inWindow = false;
|
|
106
|
+
const lines = [];
|
|
107
|
+
for (const line of markdown.split("\n")) {
|
|
108
|
+
if (/^- /.test(line))
|
|
109
|
+
inWindow = /^- (?:\[\d+\] )?AXWindow\b/.test(line);
|
|
110
|
+
if (inWindow && !SECRET.test(line))
|
|
111
|
+
lines.push(line);
|
|
112
|
+
}
|
|
113
|
+
return lines.join("\n").slice(0, 16000);
|
|
114
|
+
}
|
|
115
|
+
export function candidates(params, state) {
|
|
116
|
+
// Truncated trees cannot establish selector uniqueness; hand back instead of guessing.
|
|
117
|
+
if (state.truncated || state.degraded)
|
|
118
|
+
return [];
|
|
119
|
+
const result = [];
|
|
120
|
+
for (const control of params.controls) {
|
|
121
|
+
const matches = state.elements.filter((e) => e.role === control.selector.role && e.label === control.selector.label);
|
|
122
|
+
if (matches.length !== 1)
|
|
123
|
+
continue;
|
|
124
|
+
const element = matches[0];
|
|
125
|
+
if (!element.token || element.enabled === false || !compatible(control, element))
|
|
126
|
+
continue;
|
|
127
|
+
const missingValue = control.operation === "fill" && !Object.hasOwn(params.values ?? {}, control.valueKey);
|
|
128
|
+
if (control.operation === "fill" && !missingValue && element.value === params.values[control.valueKey])
|
|
129
|
+
continue;
|
|
130
|
+
result.push({
|
|
131
|
+
id: control.id, control, element, missingValue,
|
|
132
|
+
description: `${control.operation} ${element.role} ${JSON.stringify(element.label)}` +
|
|
133
|
+
(control.operation === "fill" ? ` using supplied value ${control.valueKey}${missingValue ? " (MISSING: hand back for text)" : ""}` : control.operation === "key" ? ` ${control.key}` : ""),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
function compatible(control, element) {
|
|
139
|
+
if (control.operation === "fill")
|
|
140
|
+
return EDITABLE.test(element.role);
|
|
141
|
+
if (control.operation === "key")
|
|
142
|
+
return EDITABLE.test(element.role) || CLICKABLE.test(element.role);
|
|
143
|
+
return CLICKABLE.test(element.role) || element.actions.some((a) => /^(AXPress|press|invoke)$/i.test(a));
|
|
144
|
+
}
|
|
145
|
+
export function modelElements(state) {
|
|
146
|
+
return state.elements.map(({ token: _token, actions: _actions, index: _index, ...e }) => e);
|
|
147
|
+
}
|
|
148
|
+
export function captureImage(reply) {
|
|
149
|
+
const blocks = (reply.content ?? []).filter((b) => isRecord(b) && b.type === "image");
|
|
150
|
+
if (blocks.length !== 1)
|
|
151
|
+
throw new ClassifierError("driver", "Expected one window screenshot. No image was silently omitted.");
|
|
152
|
+
const block = blocks[0];
|
|
153
|
+
if (!isRecord(block) || !["image/png", "image/jpeg", "image/webp"].includes(String(block.mimeType)) ||
|
|
154
|
+
typeof block.data !== "string" || block.data.length > 6990508 || (block.data.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(block.data)))
|
|
155
|
+
throw new ClassifierError("input", "Driver screenshot exceeds image limits or is not canonical base64.");
|
|
156
|
+
const bytes = Buffer.from(block.data, "base64");
|
|
157
|
+
if (bytes.toString("base64") !== block.data)
|
|
158
|
+
throw new ClassifierError("input", "Driver image encoding was not canonical base64.");
|
|
159
|
+
return [{ bytes, mimeType: block.mimeType }];
|
|
160
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
export declare const computerUseParameters: Type.TObject<{
|
|
4
|
+
goal: Type.TString;
|
|
5
|
+
target: Type.TObject<{
|
|
6
|
+
pid: Type.TInteger;
|
|
7
|
+
window_id: Type.TInteger;
|
|
8
|
+
}>;
|
|
9
|
+
controls: Type.TArray<Type.TUnion<[Type.TObject<{
|
|
10
|
+
id: Type.TString;
|
|
11
|
+
selector: Type.TObject<{
|
|
12
|
+
role: Type.TString;
|
|
13
|
+
label: Type.TString;
|
|
14
|
+
}>;
|
|
15
|
+
operation: Type.TLiteral<"click">;
|
|
16
|
+
}>, Type.TObject<{
|
|
17
|
+
id: Type.TString;
|
|
18
|
+
selector: Type.TObject<{
|
|
19
|
+
role: Type.TString;
|
|
20
|
+
label: Type.TString;
|
|
21
|
+
}>;
|
|
22
|
+
operation: Type.TLiteral<"fill">;
|
|
23
|
+
valueKey: Type.TString;
|
|
24
|
+
}>, Type.TObject<{
|
|
25
|
+
id: Type.TString;
|
|
26
|
+
selector: Type.TObject<{
|
|
27
|
+
role: Type.TString;
|
|
28
|
+
label: Type.TString;
|
|
29
|
+
}>;
|
|
30
|
+
operation: Type.TLiteral<"key">;
|
|
31
|
+
key: Type.TUnion<Type.TLiteral<string>[]>;
|
|
32
|
+
}>]>>;
|
|
33
|
+
values: Type.TOptional<Type.TRecord<"^.*$", Type.TString>>;
|
|
34
|
+
expect: Type.TOptional<Type.TArray<Type.TObject<{
|
|
35
|
+
element: Type.TObject<{
|
|
36
|
+
selector: Type.TObject<{
|
|
37
|
+
role: Type.TString;
|
|
38
|
+
label_contains: Type.TString;
|
|
39
|
+
}>;
|
|
40
|
+
exists: Type.TOptional<Type.TLiteral<true>>;
|
|
41
|
+
value_equals: Type.TOptional<Type.TString>;
|
|
42
|
+
enabled: Type.TOptional<Type.TBoolean>;
|
|
43
|
+
selected: Type.TOptional<Type.TBoolean>;
|
|
44
|
+
}>;
|
|
45
|
+
}>>>;
|
|
46
|
+
context: Type.TOptional<Type.TString>;
|
|
47
|
+
previousActions: Type.TOptional<Type.TArray<Type.TString>>;
|
|
48
|
+
maxActions: Type.TOptional<Type.TInteger>;
|
|
49
|
+
timeoutMs: Type.TOptional<Type.TInteger>;
|
|
50
|
+
deliveryMode: Type.TOptional<Type.TUnion<[Type.TLiteral<"background">, Type.TLiteral<"foreground">]>>;
|
|
51
|
+
}>;
|
|
52
|
+
export declare function registerComputerUseTool(pi: ExtensionAPI): void;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isProjectTrusted } from "@amaster.ai/pi-shared/settings";
|
|
2
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { loadClassifierSettings } from "./config.js";
|
|
5
|
+
import { runComputerUse, DRIVER_TOOLS } from "./computer-use.js";
|
|
6
|
+
import { KEY_NAMES } from "./computer-use-state.js";
|
|
7
|
+
import { ClassifierError, isRecord, userError } from "./types.js";
|
|
8
|
+
const selector = Type.Object({ role: Type.String({ minLength: 1, maxLength: 128 }), label: Type.String({ minLength: 1, maxLength: 512 }) }, { additionalProperties: false });
|
|
9
|
+
const id = Type.String({ pattern: "^[a-zA-Z0-9_-]{1,64}$" });
|
|
10
|
+
const control = Type.Union([
|
|
11
|
+
Type.Object({ id, selector, operation: Type.Literal("click") }, { additionalProperties: false }),
|
|
12
|
+
Type.Object({ id, selector, operation: Type.Literal("fill"), valueKey: id }, { additionalProperties: false }),
|
|
13
|
+
Type.Object({ id, selector, operation: Type.Literal("key"), key: Type.Union(KEY_NAMES.map((k) => Type.Literal(k))) }, { additionalProperties: false }),
|
|
14
|
+
]);
|
|
15
|
+
const expectation = Type.Object({ element: Type.Object({
|
|
16
|
+
selector: Type.Object({ role: Type.String({ minLength: 1, maxLength: 128 }), label_contains: Type.String({ minLength: 1, maxLength: 512 }) }, { additionalProperties: false }),
|
|
17
|
+
exists: Type.Optional(Type.Literal(true)),
|
|
18
|
+
value_equals: Type.Optional(Type.String({ maxLength: 4096 })),
|
|
19
|
+
enabled: Type.Optional(Type.Boolean()), selected: Type.Optional(Type.Boolean()),
|
|
20
|
+
}, { additionalProperties: false }) }, { additionalProperties: false });
|
|
21
|
+
export const computerUseParameters = Type.Object({
|
|
22
|
+
goal: Type.String({ minLength: 1, maxLength: 4096 }),
|
|
23
|
+
target: Type.Object({ pid: Type.Integer({ minimum: 1 }), window_id: Type.Integer({ minimum: 0 }) }, { additionalProperties: false }),
|
|
24
|
+
controls: Type.Array(control, { minItems: 1, maxItems: 120, description: "Only controls authorized for this goal. Exact role and label; controls may span later screens. This is an allowlist, not an ordered script. Never include unapproved consequential or credential actions." }),
|
|
25
|
+
values: Type.Optional(Type.Record(id, Type.String({ maxLength: 4096 }), { maxProperties: 64, description: "Supplied nonsecret single-line text, never generated by Decisions. Missing values hand back. Do not include credentials." })),
|
|
26
|
+
expect: Type.Optional(Type.Array(expectation, { minItems: 1, maxItems: 8, description: "All predicates must cover the full desired outcome. Without them DONE hands back for supervisor verification." })),
|
|
27
|
+
context: Type.Optional(Type.String({ maxLength: 4096, description: "Supervisor note after a handoff or unsupported action; not evidence of completion or new authorization." })),
|
|
28
|
+
previousActions: Type.Optional(Type.Array(Type.String({ maxLength: 256 }), { maxItems: 20 })),
|
|
29
|
+
maxActions: Type.Optional(Type.Integer({ minimum: 1, maximum: 100 })),
|
|
30
|
+
timeoutMs: Type.Optional(Type.Integer({ minimum: 1000, maximum: 300000 })),
|
|
31
|
+
deliveryMode: Type.Optional(Type.Union([Type.Literal("background"), Type.Literal("foreground")], { description: "Default background. Choose foreground only after observed background failure, within the same authorized target and scope." })),
|
|
32
|
+
}, { additionalProperties: false });
|
|
33
|
+
export function registerComputerUseTool(pi) {
|
|
34
|
+
let running = false;
|
|
35
|
+
pi.registerTool(defineTool({
|
|
36
|
+
name: "classifier_computer_use",
|
|
37
|
+
label: "Computer use · Decisions",
|
|
38
|
+
description: "Run a bounded OpenAI Decisions observe/select/act/verify loop on one exact Cua Driver window. Uses configured pi-classifier.computerUse, never the generic classifier default. Supply permitted semantic controls and nonsecret typing values. Decisions chooses from grounded candidates; it cannot invent coordinates or text. Returns explicit handoff and continuation when supervisor help is needed. Completion requires fresh independent Driver predicates; otherwise needs_verification. Existing Driver permissions and Pi tool hooks remain active. Window text and optional screenshots are sent to the configured OpenAI endpoint.",
|
|
39
|
+
promptSnippet: "Delegate repeated permitted computer actions to OpenAI Decisions; handle explicit supervisor handoffs and independently verify outcomes.",
|
|
40
|
+
promptGuidelines: [
|
|
41
|
+
"Resolve the intended app/account/window and observe semantic control names before calling. Supply only controls and effects authorized by the user's goal; an allowlist is not user consent.",
|
|
42
|
+
"Keep the goal bounded and values nonsecret. Keep content generation, credentials, CAPTCHAs, unsupported surfaces and consequential approvals with the supervisor or user.",
|
|
43
|
+
"On handoff inspect the reason, attempted actions and current state. Supply missing values or perform the unsupported step, then call again using handoff.continuation with an updated context note. Never replay a possibly applied action blindly.",
|
|
44
|
+
"Do not call other tools that observe or mutate the same window concurrently. Every continuation reobserves; never pass element tokens, screenshots or pending actions between chunks.",
|
|
45
|
+
"Only report verified completion when expect covers the whole user goal. Without independent predicates, inspect fresh evidence after needs_verification. Do not lower confidence or change provider to force progress.",
|
|
46
|
+
],
|
|
47
|
+
parameters: computerUseParameters,
|
|
48
|
+
outputSchema: Type.Object({ status: Type.Union([Type.Literal("completed"), Type.Literal("handoff"), Type.Literal("stopped")]) }, { additionalProperties: true }),
|
|
49
|
+
reachesTools: () => Object.values(DRIVER_TOOLS),
|
|
50
|
+
annotations: { readOnlyHint: false, destructiveHint: true, openWorldHint: true },
|
|
51
|
+
async execute(_id, params, signal, onUpdate, ctx) {
|
|
52
|
+
if (running)
|
|
53
|
+
throw new Error("Another computer-use chunk is running in this extension. Serialize access to the desktop.");
|
|
54
|
+
running = true;
|
|
55
|
+
try {
|
|
56
|
+
const result = await runComputerUse(params, {
|
|
57
|
+
settings: loadClassifierSettings(ctx.cwd, isProjectTrusted(ctx)), cwd: ctx.cwd, ...(signal ? { signal } : {}),
|
|
58
|
+
callTool: async (name, args, nestedSignal) => {
|
|
59
|
+
const outcome = await ctx.executeTool(name, args, { signal: nestedSignal });
|
|
60
|
+
// Pi's MCP wrapper exposes the raw CallToolResult inside result.structuredContent.
|
|
61
|
+
const raw = outcome.result.structuredContent;
|
|
62
|
+
if (outcome.isError || !isRecord(raw))
|
|
63
|
+
return { isError: true };
|
|
64
|
+
return raw;
|
|
65
|
+
},
|
|
66
|
+
onProgress: (progress) => onUpdate?.({ content: [{ type: "text", text: `${progress.phase}: ${progress.actions} actions, ${progress.decisions} decisions` }], details: progress }),
|
|
67
|
+
});
|
|
68
|
+
return { content: [{ type: "text", text: JSON.stringify(result) }], details: result, structuredContent: JSON.parse(JSON.stringify(result)) };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new Error(error instanceof ClassifierError ? userError(error) : "Computer use could not start. Check computerUse settings and Driver availability.");
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
running = false;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ClassifierResult, ClassifierRoute } from "./types.js";
|
|
2
|
+
export interface ComputerUseSettings {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
route?: ClassifierRoute;
|
|
5
|
+
screenshots?: boolean;
|
|
6
|
+
minConfidence?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ComputerTarget {
|
|
9
|
+
pid: number;
|
|
10
|
+
window_id: number;
|
|
11
|
+
}
|
|
12
|
+
/** Exact semantic names are re-resolved on every observation, never cached tokens. */
|
|
13
|
+
export interface ControlSelector {
|
|
14
|
+
role: string;
|
|
15
|
+
label: string;
|
|
16
|
+
}
|
|
17
|
+
export type ComputerControl = {
|
|
18
|
+
id: string;
|
|
19
|
+
selector: ControlSelector;
|
|
20
|
+
} & ({
|
|
21
|
+
operation: "click";
|
|
22
|
+
} | {
|
|
23
|
+
operation: "fill";
|
|
24
|
+
valueKey: string;
|
|
25
|
+
} | {
|
|
26
|
+
operation: "key";
|
|
27
|
+
key: string;
|
|
28
|
+
});
|
|
29
|
+
export interface ComputerExpectation {
|
|
30
|
+
element: {
|
|
31
|
+
selector: {
|
|
32
|
+
role: string;
|
|
33
|
+
label_contains: string;
|
|
34
|
+
};
|
|
35
|
+
exists?: true;
|
|
36
|
+
value_equals?: string;
|
|
37
|
+
selected?: boolean;
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface ComputerUseParams {
|
|
42
|
+
goal: string;
|
|
43
|
+
target: ComputerTarget;
|
|
44
|
+
controls: ComputerControl[];
|
|
45
|
+
values?: Record<string, string>;
|
|
46
|
+
expect?: ComputerExpectation[];
|
|
47
|
+
/** Supervisor notes and prior actions are context, not success or authorization evidence. */
|
|
48
|
+
context?: string;
|
|
49
|
+
previousActions?: string[];
|
|
50
|
+
maxActions?: number;
|
|
51
|
+
timeoutMs?: number;
|
|
52
|
+
deliveryMode?: "background" | "foreground";
|
|
53
|
+
}
|
|
54
|
+
export interface DriverElement {
|
|
55
|
+
role: string;
|
|
56
|
+
label: string;
|
|
57
|
+
value?: string;
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
selected?: boolean;
|
|
60
|
+
actions: string[];
|
|
61
|
+
token?: string;
|
|
62
|
+
index: number;
|
|
63
|
+
}
|
|
64
|
+
export interface DriverObservation {
|
|
65
|
+
elements: DriverElement[];
|
|
66
|
+
truncated: boolean;
|
|
67
|
+
degraded: boolean;
|
|
68
|
+
title: string;
|
|
69
|
+
/** Bounded window-only passive text, because indexed elements omit static labels. */
|
|
70
|
+
text: string;
|
|
71
|
+
}
|
|
72
|
+
export interface ComputerCandidate {
|
|
73
|
+
id: string;
|
|
74
|
+
description: string;
|
|
75
|
+
control: ComputerControl;
|
|
76
|
+
element: DriverElement;
|
|
77
|
+
missingValue: boolean;
|
|
78
|
+
}
|
|
79
|
+
export interface ComputerUseMetrics {
|
|
80
|
+
elapsedMs: number;
|
|
81
|
+
observationMs: number;
|
|
82
|
+
decisionMs: number;
|
|
83
|
+
actionMs: number;
|
|
84
|
+
verificationMs: number;
|
|
85
|
+
decisions: number;
|
|
86
|
+
attemptedActions: number;
|
|
87
|
+
providerCalls: number;
|
|
88
|
+
usage?: ClassifierResult["usage"];
|
|
89
|
+
cost?: ClassifierResult["cost"];
|
|
90
|
+
/** A failed/cancelled request may have been billed without usable accounting. */
|
|
91
|
+
accountingIncomplete: boolean;
|
|
92
|
+
}
|
|
93
|
+
export interface ComputerUseResult {
|
|
94
|
+
status: "completed" | "handoff" | "stopped";
|
|
95
|
+
reason: string;
|
|
96
|
+
message: string;
|
|
97
|
+
provider: string;
|
|
98
|
+
model: string;
|
|
99
|
+
target: ComputerTarget;
|
|
100
|
+
actions: {
|
|
101
|
+
control: string;
|
|
102
|
+
operation: string;
|
|
103
|
+
effect: string;
|
|
104
|
+
}[];
|
|
105
|
+
metrics: ComputerUseMetrics;
|
|
106
|
+
decision?: {
|
|
107
|
+
choice: string;
|
|
108
|
+
confidence: number;
|
|
109
|
+
probability: number;
|
|
110
|
+
};
|
|
111
|
+
evidence?: {
|
|
112
|
+
status: string;
|
|
113
|
+
predicates: unknown[];
|
|
114
|
+
};
|
|
115
|
+
handoff?: {
|
|
116
|
+
next: string;
|
|
117
|
+
controls: {
|
|
118
|
+
id: string;
|
|
119
|
+
available: boolean;
|
|
120
|
+
missingValue: boolean;
|
|
121
|
+
}[];
|
|
122
|
+
/** Plain task context. Re-call after supervisor work; the next run always observes anew. */
|
|
123
|
+
continuation: ComputerUseParams;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** A raw MCP CallToolResult, already routed through the host's tool/approval pipeline. */
|
|
127
|
+
export interface DriverReply {
|
|
128
|
+
content?: unknown[];
|
|
129
|
+
structuredContent?: unknown;
|
|
130
|
+
isError?: boolean;
|
|
131
|
+
}
|
|
132
|
+
export type DriverCall = (name: string, args: Record<string, unknown>, signal: AbortSignal) => Promise<DriverReply>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ClassifierSettings } from "./types.js";
|
|
2
|
+
import type { ComputerUseParams, ComputerUseResult, DriverCall } from "./computer-use-types.js";
|
|
3
|
+
export declare const DRIVER_TOOLS: {
|
|
4
|
+
readonly observe: "mcp__cua_driver__get_window_state";
|
|
5
|
+
readonly verify: "mcp__cua_driver__verify_state";
|
|
6
|
+
readonly click: "mcp__cua_driver__click";
|
|
7
|
+
readonly fill: "mcp__cua_driver__set_value";
|
|
8
|
+
readonly key: "mcp__cua_driver__press_key";
|
|
9
|
+
};
|
|
10
|
+
export interface ComputerUseOptions {
|
|
11
|
+
settings: ClassifierSettings;
|
|
12
|
+
cwd: string;
|
|
13
|
+
callTool: DriverCall;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
onProgress?: (progress: {
|
|
16
|
+
phase: string;
|
|
17
|
+
actions: number;
|
|
18
|
+
decisions: number;
|
|
19
|
+
}) => void;
|
|
20
|
+
}
|
|
21
|
+
/** One bounded chunk. Handoff is explicit context, never a replayable queued action. */
|
|
22
|
+
export declare function runComputerUse(params: ComputerUseParams, options: ComputerUseOptions): Promise<ComputerUseResult>;
|