11import InteractionAffordance from './interaction-affordance.js' ;
22import ValidationError from './validation-error.js' ;
3+ import { randomUUID } from 'node:crypto' ;
34
45/** @import {DataSchema, PartialActionDescription, ActionDescription, Form,
56 * ActionStatus} from "./types.js"
67 */
78
9+ /** @typedef {{status: ActionStatus, controller: AbortController} } ActionRequest */
10+
811/**
912 * Action Affordance
1013 *
@@ -37,6 +40,16 @@ class ActionAffordance extends InteractionAffordance {
3740 */
3841 synchronous ;
3942
43+ /**
44+ * @type {((value: any, signal?: AbortSignal) => Promise<any>)|undefined }
45+ */
46+ invokeHandler ;
47+
48+ /**
49+ * @type {Map<string, ActionRequest> }
50+ */
51+ #actionRequests = new Map ( ) ;
52+
4053 /**
4154 * Create a new Action.
4255 *
@@ -198,7 +211,7 @@ class ActionAffordance extends InteractionAffordance {
198211 /**
199212 * Set invoke handler function.
200213 *
201- * @param {(value: any) => Promise<void > } handler An asynchronous function to action invocations.
214+ * @param {(value: any, signal?: AbortSignal ) => Promise<any > } handler An asynchronous function to handle action invocations.
202215 */
203216 setInvokeHandler ( handler ) {
204217 this . invokeHandler = handler ;
@@ -212,7 +225,6 @@ class ActionAffordance extends InteractionAffordance {
212225 * @returns {Promise<any> } A Promise which resolves with the output of the
213226 * action (if any), conforming to the output data schema.
214227 *
215- * Note: All actions are currently treated as synchronous.
216228 */
217229 async invoke ( input ) {
218230 // TODO: Validate input against data schema
@@ -223,6 +235,96 @@ class ActionAffordance extends InteractionAffordance {
223235 return this . invokeHandler ( input ) ;
224236 }
225237
238+ /**
239+ * Start an asynchronous invocation and return its initial status.
240+ *
241+ * @param {any } input The action input.
242+ * @returns {ActionStatus & {href: string} } The initial status of the action request.
243+ */
244+ invokeAsync ( input ) {
245+ if ( ! this . invokeHandler ) {
246+ console . error ( `No invoke handler set for action ${ this . name } ` ) ;
247+ throw new Error ( 'InternalError' ) ;
248+ }
249+
250+ const actionID = randomUUID ( ) ;
251+ const status = /** @type {ActionStatus & {href: string} } */ ( {
252+ status : 'pending' ,
253+ href : `/actions/${ encodeURIComponent ( this . name ) } /${ actionID } ` ,
254+ timeRequested : new Date ( ) . toISOString ( ) ,
255+ } ) ;
256+ const request = {
257+ status,
258+ controller : new AbortController ( ) ,
259+ } ;
260+ this . #actionRequests. set ( actionID , request ) ;
261+
262+ void Promise . resolve ( ) . then ( async ( ) => {
263+ if ( this . #actionRequests. get ( actionID ) !== request ) {
264+ return ;
265+ }
266+ request . status . status = 'running' ;
267+ try {
268+ const output = await this . invokeHandler ?. (
269+ input ,
270+ request . controller . signal ,
271+ ) ;
272+ if ( this . #actionRequests. get ( actionID ) !== request ) {
273+ return ;
274+ }
275+ request . status . status = 'completed' ;
276+ if ( output !== undefined ) {
277+ request . status . output = output ;
278+ }
279+ } catch ( error ) {
280+ if ( this . #actionRequests. get ( actionID ) !== request ) {
281+ return ;
282+ }
283+ request . status . status = 'failed' ;
284+ request . status . error = {
285+ title : 'Action failed' ,
286+ ...( error instanceof Error ? { detail : error . message } : { } ) ,
287+ } ;
288+ }
289+ request . status . timeEnded = new Date ( ) . toISOString ( ) ;
290+ } ) ;
291+
292+ return { ...status } ;
293+ }
294+
295+ /**
296+ * Get the current status of an action request.
297+ *
298+ * @param {string } actionID The ID of the action request.
299+ * @returns {ActionStatus|undefined } The status, if the request exists.
300+ */
301+ getActionStatus ( actionID ) {
302+ const request = this . #actionRequests. get ( actionID ) ;
303+ return request ? { ...request . status } : undefined ;
304+ }
305+
306+ /**
307+ * Cancel an ongoing action request.
308+ *
309+ * @param {string } actionID The ID of the action request.
310+ * @returns {'cancelled'|'not-found'|'not-cancellable' } The cancellation result.
311+ */
312+ cancelAction ( actionID ) {
313+ const request = this . #actionRequests. get ( actionID ) ;
314+ if ( ! request ) {
315+ return 'not-found' ;
316+ }
317+ if (
318+ request . status . status === 'completed' ||
319+ request . status . status === 'failed'
320+ ) {
321+ return 'not-cancellable' ;
322+ }
323+ request . controller . abort ( ) ;
324+ this . #actionRequests. delete ( actionID ) ;
325+ return 'cancelled' ;
326+ }
327+
226328 /**
227329 * @returns {ActionDescription }
228330 */
0 commit comments