openai
Version:
The official TypeScript library for the OpenAI API
782 lines • 37.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComputerToolset = void 0;
const error_1 = require("../error.js");
const ResponseInputItems_1 = require("../lib/responses/ResponseInputItems.js");
const runner_diagnostics_1 = require("../internal/runner-diagnostics.js");
const runner_lifecycle_1 = require("../internal/runner-lifecycle.js");
const native_response_message_1 = require("../internal/native-response-message.js");
const values_1 = require("../internal/utils/values.js");
// Modern actions calls may omit the checklist. Legacy single actions still require it.
function validateComputerSafetyChecks(call) {
const checks = (0, runner_lifecycle_1.configuredOption)(call, 'pending_safety_checks');
if (!(0, runner_lifecycle_1.configuredOption)(call, 'actions') || checks !== undefined) {
if (!Array.isArray(checks)) {
throw new error_1.OpenAIError('ComputerToolset requires valid pending safety checks before execution.');
}
// oxlint-disable-next-line typescript/prefer-for-of -- Validate each JSON slot, including holes that must become invalid nulls.
for (let index = 0; index < checks.length; index += 1) {
const check = (0, values_1.hasOwn)(checks, index) ? checks[index] : undefined;
const code = check ? (0, runner_lifecycle_1.configuredOption)(check, 'code') : undefined;
const message = check ? (0, runner_lifecycle_1.configuredOption)(check, 'message') : undefined;
if (!check ||
!(0, values_1.hasOwn)(check, 'id') ||
typeof check.id !== 'string' ||
(code !== undefined && code !== null && typeof code !== 'string') ||
(message !== undefined && message !== null && typeof message !== 'string')) {
throw new error_1.OpenAIError('ComputerToolset requires valid pending safety checks before execution.');
}
}
}
}
function validComputerPoint(point) {
return ((0, values_1.isObj)(point) &&
(0, values_1.hasOwn)(point, 'x') &&
(0, values_1.hasOwn)(point, 'y') &&
Number.isInteger(point.x) &&
Number.isInteger(point.y));
}
function isDenseArray(values) {
if (!Array.isArray(values)) {
return false;
}
// oxlint-disable-next-line typescript/prefer-for-of -- Iteration alone cannot distinguish an inherited entry from an owned slot.
for (let index = 0; index < values.length; index += 1) {
if (!(0, values_1.hasOwn)(values, index)) {
return false;
}
}
return true;
}
function validComputerKeys(keys) {
return isDenseArray(keys) && keys.every((key) => typeof key === 'string');
}
function validComputerAction(action) {
if (!action || !(0, values_1.hasOwn)(action, 'type')) {
return false;
}
const keys = 'keys' in action ? (0, runner_lifecycle_1.configuredOption)(action, 'keys') : undefined;
if (keys !== null && keys !== undefined && !validComputerKeys(keys)) {
return false;
}
switch (action.type) {
case 'click': {
const button = (0, runner_lifecycle_1.configuredOption)(action, 'button');
return (validComputerPoint(action) &&
['left', 'right', 'wheel', 'back', 'forward'].some((value) => value === button));
}
case 'double_click':
case 'move': {
return validComputerPoint(action);
}
case 'scroll': {
return (validComputerPoint(action) &&
Number.isInteger((0, runner_lifecycle_1.configuredOption)(action, 'scroll_x')) &&
Number.isInteger((0, runner_lifecycle_1.configuredOption)(action, 'scroll_y')));
}
case 'drag': {
const path = (0, runner_lifecycle_1.configuredOption)(action, 'path');
return isDenseArray(path) && path.every(validComputerPoint);
}
case 'keypress': {
return validComputerKeys((0, runner_lifecycle_1.configuredOption)(action, 'keys'));
}
case 'type': {
return typeof (0, runner_lifecycle_1.configuredOption)(action, 'text') === 'string';
}
case 'screenshot':
case 'wait': {
return true;
}
default: {
return false;
}
}
}
function computerActions(call) {
const singleAction = (0, runner_lifecycle_1.configuredOption)(call, 'action');
const batch = (0, runner_lifecycle_1.configuredOption)(call, 'actions');
if (!(0, values_1.hasOwn)(call, 'type') ||
!(0, values_1.hasOwn)(call, 'status') ||
!(0, values_1.hasOwn)(call, 'id') ||
!(0, values_1.hasOwn)(call, 'call_id') ||
call.type !== 'computer_call' ||
call.status !== 'completed' ||
typeof call.id !== 'string' ||
typeof call.call_id !== 'string' ||
singleAction === null ||
batch === null ||
(singleAction !== undefined) === (batch !== undefined)) {
throw new error_1.OpenAIError('ComputerToolset requires a completed call with exactly one of action or actions.');
}
validateComputerSafetyChecks(call);
// SAFETY: The guard above established exactly one of action/actions is present.
const actions = batch ?? [singleAction];
if (!Array.isArray(actions) || actions.length === 0) {
throw new error_1.OpenAIError('ComputerToolset requires at least one computer action.');
}
if (!isDenseArray(actions)) {
throw new error_1.OpenAIError('ComputerToolset received an invalid computer action.');
}
for (const action of actions) {
if (Array.isArray(action) || !validComputerAction(action)) {
throw new error_1.OpenAIError('ComputerToolset received an invalid computer action.');
}
}
return actions;
}
function snapshotComputerPolicy(options) {
return {
signal: (0, runner_lifecycle_1.configuredOption)(options, 'signal'),
approveSafetyChecks: (0, runner_lifecycle_1.configuredOption)(options, 'approveSafetyChecks')?.bind(options),
beforeAction: (0, runner_lifecycle_1.configuredOption)(options, 'beforeAction')?.bind(options),
onDiagnostic: (0, runner_lifecycle_1.configuredOption)(options, 'onDiagnostic')?.bind(options),
};
}
function computerRunTools(params, maxSteps) {
if (!Number.isSafeInteger(maxSteps) || maxSteps < 1) {
throw new TypeError('ComputerToolset maxSteps must be a positive safe integer.');
}
const suppliedTools = (0, runner_lifecycle_1.configuredOption)(params, 'tools');
const tools = suppliedTools === undefined ? [{ type: 'computer' }] : suppliedTools;
const conversation = (0, runner_lifecycle_1.configuredOption)(params, 'conversation');
const previousResponse = (0, runner_lifecycle_1.configuredOption)(params, 'previous_response_id');
if ((0, runner_lifecycle_1.configuredOption)(params, 'stream') ||
(0, runner_lifecycle_1.configuredOption)(params, 'background') ||
(conversation !== undefined && conversation !== null) ||
(previousResponse !== undefined && previousResponse !== null) ||
!Array.isArray(tools) ||
tools.length !== 1 ||
tools.some((tool) => {
const type = tool && (0, runner_lifecycle_1.configuredOption)(tool, 'type');
return type !== 'computer' && type !== 'computer_use_preview';
})) {
throw new TypeError('ComputerToolset.run requires non-streaming, foreground, one computer tool and explicit input history.');
}
return tools;
}
function releaseForcedComputerChoice(choice) {
if (choice === 'required' ||
(typeof choice === 'object' &&
(0, values_1.hasOwn)(choice, 'type') &&
['computer', 'computer_use_preview', 'computer_use'].includes(choice.type))) {
return 'auto';
}
if (typeof choice === 'object' &&
(0, values_1.hasOwn)(choice, 'type') &&
choice.type === 'allowed_tools' &&
(0, values_1.hasOwn)(choice, 'mode') &&
choice.mode === 'required') {
return { ...choice, mode: 'auto' };
}
return choice;
}
function validateComputerChecklistOwner(call) {
// Match configured options' owner boundary without invoking a checklist getter.
let owner = call;
while (owner !== null && !(0, runner_lifecycle_1.isObjectPrototype)(owner)) {
const descriptor = Object.getOwnPropertyDescriptor(owner, 'pending_safety_checks');
if (descriptor) {
if (owner !== call || !descriptor.enumerable || !('value' in descriptor)) {
throw new error_1.OpenAIError('ComputerToolset requires own enumerable data for safety checks.');
}
break;
}
// SAFETY: Object.getPrototypeOf returns the object's prototype or null.
owner = Object.getPrototypeOf(owner);
}
}
function snapshotComputerCall(call) {
if (Array.isArray(call)) {
throw new error_1.OpenAIError('ComputerToolset requires a call object.');
}
validateComputerChecklistOwner(call);
const snapshot = structuredClone(call);
for (const action of computerActions(snapshot)) {
if (action.type === 'double_click' && (0, runner_lifecycle_1.configuredOption)(action, 'keys') === undefined) {
action.keys = null;
}
}
return snapshot;
}
function actionApprovalCall(call) {
const checks = (0, runner_lifecycle_1.configuredOption)(call, 'pending_safety_checks') ?? [];
const actions = (0, runner_lifecycle_1.configuredOption)(call, 'actions');
if (!Array.isArray(actions)) {
const view = structuredClone({ ...call, pending_safety_checks: checks });
(0, runner_lifecycle_1.protectRunnerJSON)(view);
return view;
}
const sources = { actions, pending_safety_checks: checks };
const view = structuredClone({
...call,
actions: [],
pending_safety_checks: [],
});
// Protect the base before installing accessors so unused batch views stay lazy.
(0, runner_lifecycle_1.protectRunnerJSON)(view);
for (const key of ['actions', 'pending_safety_checks']) {
const source = sources[key];
let materialized = false;
let value;
const save = (replacement) => {
value = replacement;
materialized = true;
if (Object.getOwnPropertyDescriptor(view, key)?.configurable) {
Object.defineProperty(view, key, {
value,
writable: true,
enumerable: true,
configurable: true,
});
}
return value;
};
Object.defineProperty(view, key, {
configurable: true,
enumerable: true,
get() {
if (materialized) {
return value;
}
const captured = structuredClone(source);
(0, runner_lifecycle_1.protectRunnerJSON)(captured);
return save(captured);
},
set(replacement) {
if (Object.isFrozen(view)) {
throw new TypeError('Cannot assign to frozen computer approval call.');
}
save(replacement);
},
});
}
return view;
}
// oxlint-disable-next-line complexity -- Validate every supported native output before any computer effect.
function computerCalls(response, earlierIdentities, earlierCallIds) {
if ((0, runner_lifecycle_1.configuredOption)(response, 'status') !== 'completed') {
throw new error_1.OpenAIError('ComputerToolset.run received a response that is not completed.');
}
// Preflight the entire response before any effects, even when unsupported items follow a valid call.
const calls = [];
const output = [];
const source = (0, runner_lifecycle_1.configuredOption)(response, 'output');
if (!Array.isArray(source)) {
throw new error_1.OpenAIError('ComputerToolset.run received an invalid output array.');
}
const { length } = source;
if (!Number.isSafeInteger(length) || length < 0) {
throw new error_1.OpenAIError('ComputerToolset.run received an invalid output array.');
}
let hasFinalMessage = false;
const identities = new Set();
const callIds = new Set();
for (let index = 0; index < length; index += 1) {
const raw = (0, values_1.hasOwn)(source, index) ? source[index] : undefined;
if (!raw) {
throw new error_1.OpenAIError('ComputerToolset.run received an absent output item.');
}
const descriptors = Object.getOwnPropertyDescriptors(raw);
for (const [name, descriptor] of Object.entries(descriptors)) {
if (descriptor.get) {
if (name === 'type') {
descriptors.type = {
value: descriptor.get.call(raw),
enumerable: Boolean(descriptor.enumerable),
configurable: Boolean(descriptor.configurable),
};
}
else {
descriptor.get = descriptor.get.bind(raw);
}
}
}
// SAFETY: This shadow has exactly the provider's own descriptors and prototype, with type captured once.
const captured = Object.create(Object.getPrototypeOf(raw), descriptors);
const item = (0, values_1.hasOwn)(captured, 'type') && captured.type === 'computer_call'
? snapshotComputerCall(captured)
: structuredClone(captured);
if (!(0, values_1.hasOwn)(item, 'type') || !(0, values_1.hasOwn)(item, 'id') || typeof item.id !== 'string') {
throw new error_1.OpenAIError('ComputerToolset.run requires emitted output type and id fields.');
}
if (item.type === 'computer_call') {
if (callIds.has(item.call_id) || earlierCallIds.has(item.call_id)) {
throw new error_1.OpenAIError('ComputerToolset.run received a duplicate computer call identity.');
}
callIds.add(item.call_id);
calls.push(item);
}
else if (item.type === 'message') {
if (!(0, native_response_message_1.isNativeResponseMessage)(item)) {
throw new error_1.OpenAIError('ComputerToolset.run requires native assistant content before replay.');
}
hasFinalMessage || (hasFinalMessage = item.status === 'completed' && (!(0, values_1.hasOwn)(item, 'phase') || item.phase !== 'commentary'));
}
else if (item.type === 'reasoning') {
if (!(0, native_response_message_1.isNativeResponseReasoning)(item)) {
throw new error_1.OpenAIError('ComputerToolset.run requires native reasoning content before replay.');
}
}
else if (item.type === 'compaction') {
if (!(0, values_1.hasOwn)(item, 'encrypted_content') || typeof item.encrypted_content !== 'string') {
throw new error_1.OpenAIError('ComputerToolset.run requires native compaction content before replay.');
}
}
else {
throw new error_1.OpenAIError('ComputerToolset.run cannot handle a non-computer output item.');
}
output.push(item);
if (identities.has(item.id) || (item.id !== '' && earlierIdentities.has(item.id))) {
throw new error_1.OpenAIError('ComputerToolset.run received a duplicate output item identity.');
}
identities.add(item.id);
}
if (calls.length === 0 && !hasFinalMessage) {
throw new error_1.OpenAIError('ComputerToolset.run received no completed final message.');
}
for (const id of identities) {
if (id !== '') {
earlierIdentities.add(id);
}
}
for (const id of callIds) {
earlierCallIds.add(id);
}
return { calls, output };
}
function computerDriverMethod(driver, name) {
const method = (0, runner_lifecycle_1.configuredOption)(driver, name);
if (typeof method !== 'function') {
throw new error_1.OpenAIError('ComputerToolset requires callable computer driver methods.');
}
return method;
}
/**
* Dispatches existing computer actions for a caller-owned browser or computer.
* run() handles computer-only conversations; execute() supports caller-managed loops.
*/
class ComputerToolset {
/** The environment remains owned by the caller and is never opened or closed by the helper. */
constructor(driver) {
this.lifecycle = new runner_lifecycle_1.RunnerLifecycle();
const methods = {
click: computerDriverMethod(driver, 'click'),
double_click: computerDriverMethod(driver, 'double_click'),
drag: computerDriverMethod(driver, 'drag'),
keypress: computerDriverMethod(driver, 'keypress'),
move: computerDriverMethod(driver, 'move'),
screenshot: computerDriverMethod(driver, 'screenshot'),
scroll: computerDriverMethod(driver, 'scroll'),
type: computerDriverMethod(driver, 'type'),
wait: computerDriverMethod(driver, 'wait'),
};
this.driver = {
click: methods.click.bind(driver),
double_click: methods.double_click.bind(driver),
drag: methods.drag.bind(driver),
keypress: methods.keypress.bind(driver),
move: methods.move.bind(driver),
screenshot: methods.screenshot.bind(driver),
scroll: methods.scroll.bind(driver),
type: methods.type.bind(driver),
wait: methods.wait.bind(driver),
};
}
/** Stop new work. If draining, await the current operation before closing your driver. */
close() {
return this.lifecycle.close();
}
/** Snapshot and check a complete call before a runner dispatches any effects from its response.
* @internal
*/
static validate(call) {
return snapshotComputerCall(call);
}
/**
* Run until a completed response contains no computer calls.
* Defaults to the computer tool when tools is omitted. Other tools, streams,
* background work, conversation, and previous_response_id are rejected.
* Replays history (including encrypted reasoning for store:false).
*
* @throws {OpenAIError} If a response is incomplete, unsupported, declined, or exceeds maxSteps.
*/
async run(client, params, options = {}) {
this.lifecycle.enter();
try {
return await this.runOwned(client, params, options);
}
finally {
this.lifecycle.leave();
}
}
// oxlint-disable-next-line complexity -- Ordered requests share cancellation and continuation policy.
async runOwned(client, params, options) {
const suppliedMaxSteps = (0, runner_lifecycle_1.configuredOption)(options, 'maxSteps');
const maxSteps = suppliedMaxSteps === undefined ? 20 : suppliedMaxSteps;
// Match the ordinary SDK JSON encoder once, then validate and reuse the resulting body.
const encoded = JSON.stringify(params);
const decoded = JSON.parse(encoded);
if (!(0, values_1.isObj)(decoded)) {
throw new TypeError('ComputerToolset request must be an object.');
}
// SAFETY: computerRunTools validates the serialized modes/tools before the request can reach the API.
const request = decoded;
const tools = computerRunTools(request, maxSteps);
const suppliedInput = (0, runner_lifecycle_1.configuredOption)(request, 'input');
const input = typeof suppliedInput === 'string'
? [{ role: 'user', content: suppliedInput }]
: [...(suppliedInput === undefined ? [] : suppliedInput)];
const include = [...((0, runner_lifecycle_1.configuredOption)(request, 'include') ?? [])];
if (!include.includes('reasoning.encrypted_content')) {
include.push('reasoning.encrypted_content');
}
const body = { ...request, tools, include, input };
(0, runner_lifecycle_1.protectRunnerJSON)(body);
const policy = snapshotComputerPolicy(options);
let supplied = (0, runner_lifecycle_1.configuredOption)(options, 'requestOptions');
const ownSignal = supplied && Object.getOwnPropertyDescriptor(supplied, 'signal');
const signal = policy.signal ?? (supplied && (0, runner_lifecycle_1.configuredOption)(supplied, 'signal')) ?? undefined;
if (policy.signal || (ownSignal && !('value' in ownSignal)) || (signal && !ownSignal?.enumerable)) {
// Capture accessors once and expose hidden signals to the native request spread.
supplied = (0, runner_lifecycle_1.overlayRequestOptions)(supplied, { signal });
}
const executionOptions = { ...policy, signal };
const earlierIdentities = new Set();
const earlierCallIds = new Set();
for (const item of input) {
if (!(0, values_1.isObj)(item)) {
continue;
}
const id = (0, values_1.hasOwn)(item, 'id') ? item['id'] : undefined;
const callId = (0, values_1.hasOwn)(item, 'call_id') ? item['call_id'] : undefined;
if (typeof id === 'string' && id !== '') {
earlierIdentities.add(id);
}
if (typeof callId === 'string') {
earlierCallIds.add(callId);
}
}
for (let step = 0;; step += 1) {
signal?.throwIfAborted();
const nextOptions = step === 0 ? supplied : (0, runner_lifecycle_1.continuationRequestOptions)(client, supplied, signal);
if (nextOptions && Object.getOwnPropertyDescriptor(nextOptions, 'body')?.enumerable) {
throw new TypeError('ComputerToolset.run requestOptions.body is not supported.');
}
let response;
try {
// Each response depends on observations from the preceding one.
// oxlint-disable-next-line no-await-in-loop
response = await client.responses.create(body, nextOptions);
}
catch (error) {
signal?.throwIfAborted();
throw error;
}
signal?.throwIfAborted();
const { calls, output } = computerCalls(response, earlierIdentities, earlierCallIds);
if (calls.length === 0) {
return response;
}
if (step >= maxSteps - 1) {
throw new error_1.OpenAIError('ComputerToolset.run reached maxSteps before executing another computer call.');
}
// Capture the native replay as it will be sent, before an action can change the environment.
const projected = (0, ResponseInputItems_1.toResponseInputItems)(output);
(0, runner_lifecycle_1.protectRunnerJSON)(projected);
// SAFETY: computerCalls validated every supported item; toResponseInputItems strips output-only fields.
// oxlint-disable-next-line unicorn/prefer-structured-clone -- Replay must satisfy the native JSON encoder, not just be cloneable.
const replay = JSON.parse(JSON.stringify(projected));
(0, runner_lifecycle_1.protectRunnerJSON)(replay);
for (const item of replay) {
body.input.push(item);
}
for (const call of calls) {
// Calls share one environment and must run in server order.
// oxlint-disable-next-line no-await-in-loop
const observation = await this.executeOwned(call, executionOptions);
body.input.push(observation);
}
const choice = (0, runner_lifecycle_1.configuredOption)(body, 'tool_choice');
if (choice !== undefined && choice !== null) {
body.tool_choice = releaseForcedComputerChoice(choice);
if (typeof body.tool_choice === 'object') {
(0, runner_lifecycle_1.protectRunnerJSON)(body.tool_choice);
}
}
}
}
/**
* Execute one completed call for a caller-managed Responses loop.
* Returns exactly one observation captured after all actions in the call.
* Screenshot actions still pass through beforeAction; intermediate captures
* are omitted because Responses accepts only the final observation.
* Approval, policy rejection, abort, and driver failures stop immediately.
*/
async execute(call, options = {}) {
this.lifecycle.enter();
try {
return await this.executeOwned(call, snapshotComputerPolicy(options));
}
finally {
this.lifecycle.leave();
}
}
// oxlint-disable-next-line eslint/complexity -- Keep the execution snapshot shared by safety approval, action approval and dispatch.
async executeOwned(call, policy) {
const snapshot = ComputerToolset.validate(call);
// Use the same native projection as run(), before any callback or effect.
// SAFETY: Validation permits modern calls to omit only the legacy checklist required by the output type.
const projected = (0, ResponseInputItems_1.toResponseInputItems)([snapshot]);
(0, runner_lifecycle_1.protectRunnerJSON)(projected);
// SAFETY: The validated single computer call retains its native fields after projection.
// oxlint-disable-next-line unicorn/prefer-structured-clone -- Direct replay must satisfy the native JSON encoder too.
const replay = JSON.parse(JSON.stringify(projected));
const actions = computerActions(snapshot);
for (const action of actions) {
if ((action.type === 'click' ||
action.type === 'drag' ||
action.type === 'move' ||
action.type === 'scroll') &&
(0, runner_lifecycle_1.configuredOption)(action, 'keys') === undefined) {
// Shadow omitted modifiers only in execution copies; wire snapshots must remain JSON values.
Object.defineProperty(action, 'keys', {
value: undefined,
enumerable: true,
writable: true,
configurable: true,
});
}
}
// The final capture still requires approval when a driver mutates its action.
// The SDK compiles against ES2020, which does not include Array.at.
// oxlint-disable-next-line prefer-at
const needsScreenshot = actions[actions.length - 1]?.type !== 'screenshot';
policy.signal?.throwIfAborted();
const checks = (0, runner_lifecycle_1.configuredOption)(snapshot, 'pending_safety_checks') ?? [];
const nativeChecks = checks.map((check) => ({
id: check.id,
code: (0, values_1.hasOwn)(check, 'code') ? check.code : undefined,
message: (0, values_1.hasOwn)(check, 'message') ? check.message : undefined,
}));
// Native fields in the captured replay must still describe the same checks.
const captured = replay[0]?.pending_safety_checks ?? [];
if (!Array.isArray(captured) ||
captured.length !== nativeChecks.length ||
!captured.every((value, index) => (0, values_1.isObj)(value) &&
(0, values_1.hasOwn)(value, 'id') &&
value['id'] === nativeChecks[index]?.id &&
((0, values_1.hasOwn)(value, 'code') ? value['code'] : undefined) === nativeChecks[index]?.code &&
((0, values_1.hasOwn)(value, 'message') ? value['message'] : undefined) === nativeChecks[index]?.message)) {
throw new error_1.OpenAIError('ComputerToolset serialization changed the pending safety checks.');
}
// SAFETY: Every captured native field matches its already validated check; optional extensions are JSON values.
const acknowledged = captured;
if (checks.length > 0) {
// Preserve omissions in replay/acknowledgement; callbacks own absent fields in execution copies.
for (const check of checks) {
for (const key of ['code', 'message']) {
if (!(0, values_1.hasOwn)(check, key)) {
Object.defineProperty(check, key, {
value: undefined,
enumerable: true,
writable: true,
configurable: true,
});
}
}
}
const started = performance.now();
try {
const view = structuredClone({ ...snapshot, pending_safety_checks: checks });
(0, runner_lifecycle_1.protectRunnerJSON)(view);
const approved = await policy.approveSafetyChecks?.(view, { signal: policy.signal });
policy.signal?.throwIfAborted();
if (approved !== true) {
throw new error_1.OpenAIError('ComputerToolset requires explicit approval of pending safety checks.');
}
(0, runner_diagnostics_1.reportDiagnostic)(policy.onDiagnostic, { api: 'computer', stage: 'approval', reason: 'completed', call_id: snapshot.call_id }, started);
}
catch (error) {
(0, runner_diagnostics_1.reportDiagnostic)(policy.onDiagnostic, {
api: 'computer',
stage: 'approval',
reason: policy.signal?.aborted ? 'cancelled' : 'policy_failed',
call_id: snapshot.call_id,
}, started);
policy.signal?.throwIfAborted();
throw error;
}
}
for (const action of actions) {
// Computer effects are ordered and must never execute concurrently.
// oxlint-disable-next-line no-await-in-loop
await this.perform(action, snapshot, policy);
}
if (needsScreenshot) {
await this.perform({ type: 'screenshot' }, snapshot, policy);
}
policy.signal?.throwIfAborted();
const output = await this.captureScreenshot(snapshot, policy);
const observation = {
type: 'computer_call_output',
call_id: snapshot.call_id,
output,
...(checks.length > 0 ? { acknowledged_safety_checks: acknowledged } : {}),
};
(0, runner_lifecycle_1.protectRunnerJSON)(observation);
return observation;
}
async captureScreenshot(snapshot, policy) {
const started = performance.now();
let output;
try {
const supplied = await this.driver.screenshot({ signal: policy.signal });
policy.signal?.throwIfAborted();
if (!(0, values_1.isObj)(supplied)) {
throw new error_1.OpenAIError('ComputerToolset screenshot must return a computer_screenshot with image_url or file_id.');
}
const type = (0, runner_lifecycle_1.configuredOption)(supplied, 'type');
const image_url = (0, runner_lifecycle_1.configuredOption)(supplied, 'image_url');
const file_id = (0, runner_lifecycle_1.configuredOption)(supplied, 'file_id');
if (type !== 'computer_screenshot' ||
(image_url !== undefined && (typeof image_url !== 'string' || !image_url.trim())) ||
(file_id !== undefined && (typeof file_id !== 'string' || !file_id.trim())) ||
(image_url === undefined && file_id === undefined)) {
throw new error_1.OpenAIError('ComputerToolset screenshot must return a computer_screenshot with image_url or file_id.');
}
output = {
type,
...(image_url === undefined ? {} : { image_url }),
...(file_id === undefined ? {} : { file_id }),
};
(0, runner_diagnostics_1.reportDiagnostic)(policy.onDiagnostic, {
api: 'computer',
stage: 'action',
reason: 'completed',
call_id: snapshot.call_id,
member: 'screenshot',
}, started);
}
catch (error) {
(0, runner_diagnostics_1.reportDiagnostic)(policy.onDiagnostic, {
api: 'computer',
stage: 'action',
reason: policy.signal?.aborted ? 'cancelled' : 'driver_failed',
call_id: snapshot.call_id,
member: 'screenshot',
}, started);
policy.signal?.throwIfAborted();
throw error;
}
policy.signal?.throwIfAborted();
return output;
}
async perform(action, call, options) {
options.signal?.throwIfAborted();
if (options.beforeAction) {
await ComputerToolset.approveAction(action, call, options.beforeAction, options);
}
options.signal?.throwIfAborted();
const started = performance.now();
try {
const dispatched = structuredClone(action);
switch (dispatched.type) {
case 'click': {
await this.driver.click(dispatched, { signal: options.signal });
break;
}
case 'double_click': {
await this.driver.double_click(dispatched, { signal: options.signal });
break;
}
case 'drag': {
await this.driver.drag(dispatched, { signal: options.signal });
break;
}
case 'keypress': {
await this.driver.keypress(dispatched, { signal: options.signal });
break;
}
case 'move': {
await this.driver.move(dispatched, { signal: options.signal });
break;
}
case 'screenshot': {
break;
}
case 'scroll': {
await this.driver.scroll(dispatched, { signal: options.signal });
break;
}
case 'type': {
await this.driver.type(dispatched, { signal: options.signal });
break;
}
case 'wait': {
await this.driver.wait(dispatched, { signal: options.signal });
break;
}
default: {
throw new error_1.OpenAIError('ComputerToolset received an unsupported computer action.');
}
}
options.signal?.throwIfAborted();
if (action.type !== 'screenshot') {
(0, runner_diagnostics_1.reportDiagnostic)(options.onDiagnostic, {
api: 'computer',
stage: 'action',
reason: 'completed',
call_id: call.call_id,
member: action.type,
}, started);
}
}
catch (error) {
(0, runner_diagnostics_1.reportDiagnostic)(options.onDiagnostic, {
api: 'computer',
stage: 'action',
reason: options.signal?.aborted ? 'cancelled' : 'driver_failed',
call_id: call.call_id,
member: action.type,
}, started);
options.signal?.throwIfAborted();
throw error;
}
options.signal?.throwIfAborted();
}
static async approveAction(action, call, beforeAction, options) {
const started = performance.now();
try {
const view = structuredClone(action);
(0, runner_lifecycle_1.protectRunnerJSON)(view);
const approved = await beforeAction(view, actionApprovalCall(call), {
signal: options.signal,
});
options.signal?.throwIfAborted();
if (approved !== true) {
throw new error_1.OpenAIError('ComputerToolset action declined by beforeAction.');
}
(0, runner_diagnostics_1.reportDiagnostic)(options.onDiagnostic, {
api: 'computer',
stage: 'approval',
reason: 'completed',
call_id: call.call_id,
member: action.type,
}, started);
}
catch (error) {
(0, runner_diagnostics_1.reportDiagnostic)(options.onDiagnostic, {
api: 'computer',
stage: 'approval',
reason: options.signal?.aborted ? 'cancelled' : 'policy_failed',
call_id: call.call_id,
member: action.type,
}, started);
options.signal?.throwIfAborted();
throw error;
}
}
}
exports.ComputerToolset = ComputerToolset;
//# sourceMappingURL=computer.js.map