@mondaydotcomorg/atp-runtime
Version:
Runtime SDK injected into sandbox for Agent Tool Protocol
95 lines • 4.01 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
/**
* Approval API - Clean refactored version with decorators and extracted modules
*
* Benefits:
* - No duplication between implementation and metadata
* - Types auto-detected from TypeScript signatures
* - Clean separation of concerns (handler, API)
*/
import { RuntimeAPI, RuntimeMethod } from '../metadata/decorators.js';
import { getApprovalHandler } from './handler.js';
import { pauseForCallback, CallbackType, ApprovalOperation } from '../pause/index.js';
import { nextSequenceNumber, getCachedResult, shouldPauseForClient } from '../llm/replay.js';
export { initializeApproval } from './handler.js';
/**
* Approval Runtime API
*
* Allows agents to request explicit human approval before proceeding with sensitive operations.
* This integrates with MCP's elicitation feature to request structured input from users.
*/
let ApprovalAPI = class ApprovalAPI {
/**
* Request approval from a human
*/
async request(message, context) {
const currentSequence = nextSequenceNumber();
const cachedResult = getCachedResult(currentSequence);
if (cachedResult !== undefined) {
return cachedResult;
}
const shouldPause = shouldPauseForClient();
if (shouldPause) {
pauseForCallback(CallbackType.APPROVAL, ApprovalOperation.REQUEST, {
message,
context,
sequenceNumber: currentSequence,
});
}
const handler = getApprovalHandler();
if (!handler) {
throw new Error('Approval handler not configured. Human approval is required but no handler is set.');
}
const approvalRequest = {
message,
context,
timeout: 300000,
};
let timeoutId = null;
const timeoutPromise = new Promise((_, reject) => {
timeoutId = setTimeout(() => reject(new Error('Approval request timed out')), approvalRequest.timeout);
});
try {
const response = await Promise.race([handler(approvalRequest), timeoutPromise]);
if (timeoutId)
clearTimeout(timeoutId);
return {
...response,
timestamp: Date.now(),
};
}
catch (error) {
if (timeoutId)
clearTimeout(timeoutId);
throw new Error(`Approval request failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
};
__decorate([
RuntimeMethod('Request approval from a human', {
message: {
description: 'The message to display to the user',
},
context: {
description: 'Optional context information about what needs approval',
optional: true,
type: 'Record<string, unknown>',
},
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", Promise)
], ApprovalAPI.prototype, "request", null);
ApprovalAPI = __decorate([
RuntimeAPI('approval', 'Approval API - Request explicit human approval for sensitive operations')
], ApprovalAPI);
export const approval = new ApprovalAPI();
//# sourceMappingURL=index.js.map