@@ -4,7 +4,7 @@ import log from '@apify/log';
44
55import type { ApifyClient } from '../../apify_client.js' ;
66import { DATASET_SIZE_HINT_BYTES , HELPER_TOOLS , NARROW_OUTPUT_HINT } from '../../const.js' ;
7- import { getWidgetConfig , WIDGET_URIS } from '../../resources/widgets.js' ;
7+ import { buildActorRunWidgetMeta } from '../../resources/widgets.js' ;
88import type { ConsoleLinkContext } from '../../types.js' ;
99import {
1010 buildConsoleDatasetUrl ,
@@ -702,24 +702,11 @@ async function waitForRunWithProgress(opts: {
702702// -----------------------------------------------------------------------------
703703
704704/**
705- * Build a RunResponse from an already-started ActorRun without waiting.
706- * Used when waitSecs=0 (default and apps modes) and by widget variants that return immediately.
707- * Storage metadata contains IDs only; pollers/widgets fetch updates via get-actor-run.
708- *
709- * Pass `widget: true` for widget-rendered responses: nextStep is replaced with a no-poll
710- * message and widget _meta is included so the UI renders automatically.
711- *
712- * Invariant: `widget: true` is only valid from `*-widget` tools. Non-widget tools (call-actor,
713- * direct actor tools) must omit it or pass `false`.
705+ * Shared construction for the immediate start response, used by both the base and widget
706+ * builders below. Returns the full `RunResponse` with the computed (non-widget) `nextStep`;
707+ * `buildStartRunWidgetResponse` overrides `nextStep` on its own copy.
714708 */
715- export function buildStartRunResponse ( params : {
716- actorName : string ;
717- actorRun : ActorRun ;
718- widget ?: boolean ;
719- linkContext ?: ConsoleLinkContext ;
720- } ) : ToolResponse {
721- const { actorName, actorRun, widget, linkContext } = params ;
722-
709+ function buildStartRunSharedContent ( actorName : string , actorRun : ActorRun ) : RunResponse {
723710 // Start path returns before any metadata fetch, so every entry — default and aliases — is id-only.
724711 const datasetIds = buildStorageAliasIds ( actorRun . storageIds ?. datasets , actorRun . defaultDatasetId ?? undefined ) ;
725712 const kvIds = buildStorageAliasIds (
@@ -733,15 +720,13 @@ export function buildStartRunResponse(params: {
733720 Object . fromEntries ( Object . entries ( kvIds ) . map ( ( [ alias , id ] ) => [ alias , { id } ] ) ) ,
734721 ) ;
735722
736- const { summary, nextStep : computedNextStep } = buildStatusSummaryNextStep ( {
723+ const { summary, nextStep } = buildStatusSummaryNextStep ( {
737724 run : actorRun ,
738725 dataset : datasets ?. default ,
739726 keyValueStore : keyValueStores ?. default ,
740727 } ) ;
741728
742- const nextStep = widget ? WIDGET_NO_POLL_NEXT_STEP : computedNextStep ;
743-
744- const structuredContent : RunResponse = {
729+ return {
745730 runId : actorRun . id ,
746731 actorId : actorRun . actId ,
747732 actorName,
@@ -754,19 +739,50 @@ export function buildStartRunResponse(params: {
754739 summary,
755740 nextStep,
756741 } ;
742+ }
743+
744+ /**
745+ * Build a RunResponse from an already-started ActorRun without waiting.
746+ * Used when waitSecs=0 (default and apps modes).
747+ * Storage metadata contains IDs only; pollers fetch updates via get-actor-run.
748+ */
749+ export function buildStartRunResponse ( params : {
750+ actorName : string ;
751+ actorRun : ActorRun ;
752+ linkContext ?: ConsoleLinkContext ;
753+ } ) : ToolResponse {
754+ const { actorName, actorRun, linkContext } = params ;
755+
756+ const structuredContent = buildStartRunSharedContent ( actorName , actorRun ) ;
757757 const consoleLinks = applyConsoleLinks ( structuredContent , linkContext ) ;
758758
759- const widgetMeta = widget
760- ? {
761- ...( getWidgetConfig ( WIDGET_URIS . ACTOR_RUN ) ?. meta ?? { } ) ,
762- 'openai/widgetDescription' : `Actor run progress for ${ actorName } ` ,
763- }
764- : undefined ;
759+ return respondOk (
760+ [
761+ JSON . stringify ( structuredContent ) ,
762+ `${ structuredContent . summary } \n${ structuredContent . nextStep } ${ consoleLinks } ` ,
763+ ] ,
764+ { structuredContent } ,
765+ ) ;
766+ }
767+
768+ /**
769+ * Build a RunResponse from an already-started ActorRun for widget-rendered responses:
770+ * nextStep is replaced with a no-poll message and widget _meta is included so the UI renders
771+ * automatically. Used only by `*-widget` tools.
772+ */
773+ export function buildStartRunWidgetResponse ( params : { actorName : string ; actorRun : ActorRun } ) : ToolResponse {
774+ const { actorName, actorRun } = params ;
775+
776+ const base = buildStartRunSharedContent ( actorName , actorRun ) ;
777+ const structuredContent = { ...base , nextStep : WIDGET_NO_POLL_NEXT_STEP } ;
765778
766- return respondOk ( [ JSON . stringify ( structuredContent ) , `${ summary } \n${ nextStep } ${ consoleLinks } ` ] , {
767- structuredContent,
768- meta : widgetMeta ,
769- } ) ;
779+ return respondOk (
780+ [ JSON . stringify ( structuredContent ) , `${ structuredContent . summary } \n${ structuredContent . nextStep } ` ] ,
781+ {
782+ structuredContent,
783+ meta : buildActorRunWidgetMeta ( actorName ) ,
784+ } ,
785+ ) ;
770786}
771787
772788// -----------------------------------------------------------------------------
0 commit comments