(
paused: PausedExecution,
options?: { readonly deadline?: PausedExecutionDeadline },
)
| 131 | }; |
| 132 | |
| 133 | export const formatPausedExecution = ( |
| 134 | paused: PausedExecution, |
| 135 | options?: { readonly deadline?: PausedExecutionDeadline }, |
| 136 | ): { |
| 137 | text: string; |
| 138 | structured: Record<string, unknown>; |
| 139 | } => { |
| 140 | const req = paused.elicitationContext.request; |
| 141 | const lines: string[] = [`Execution paused: ${req.message}`]; |
| 142 | const deadline = options?.deadline; |
| 143 | const isUrlElicitation = Predicate.isTagged(req, "UrlElicitation"); |
| 144 | const isFormElicitation = Predicate.isTagged(req, "FormElicitation"); |
| 145 | const requestedSchema = isFormElicitation ? req.requestedSchema : undefined; |
| 146 | const hasRequestedSchema = |
| 147 | requestedSchema !== undefined && Object.keys(requestedSchema).length > 0; |
| 148 | const baseInstructions = isUrlElicitation |
| 149 | ? `The user needs to open this URL in a browser and complete the flow. After the user finishes, call the resume tool with executionId "${paused.id}" and action "accept".` |
| 150 | : hasRequestedSchema |
| 151 | ? `Ask the user for values matching requestedSchema. Then call the resume tool with executionId "${paused.id}", action "accept", and content matching requestedSchema. If the user declines, call resume with action "decline" or "cancel".` |
| 152 | : `This is a model-side confirmation gate; there is no browser form to open. Ask the user whether to approve the paused tool call. If the user approves, call the resume tool with executionId "${paused.id}" and action "accept". If the user declines, call resume with action "decline" or "cancel".`; |
| 153 | const deadlineInstructions = deadline |
| 154 | ? ` Resume before ${deadline.expiresAt}; this approval window lasts ${formatTtlDuration(deadline.ttlMs)}.` |
| 155 | : ""; |
| 156 | const instructions = `${baseInstructions}${deadlineInstructions}`; |
| 157 | |
| 158 | if (isUrlElicitation) { |
| 159 | lines.push(`\nOpen this URL in a browser:\n${req.url}`); |
| 160 | lines.push('\nAfter the browser flow, call the resume tool with action "accept".'); |
| 161 | } else if (hasRequestedSchema) { |
| 162 | lines.push( |
| 163 | "\nAsk the user for a response matching the requested schema, then call the resume tool.", |
| 164 | ); |
| 165 | lines.push(`\nRequested schema:\n${JSON.stringify(requestedSchema, null, 2)}`); |
| 166 | } else { |
| 167 | lines.push( |
| 168 | '\nThis is a model-side confirmation gate; no browser form is waiting. Ask the user whether to approve, then call the resume tool with action "accept", "decline", or "cancel".', |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | lines.push(`\nexecutionId: ${paused.id}`); |
| 173 | if (deadline) { |
| 174 | lines.push( |
| 175 | `\nresumeDeadline: ${deadline.expiresAt} (${formatTtlDuration(deadline.ttlMs)} approval window)`, |
| 176 | ); |
| 177 | } |
| 178 | lines.push(`\ninstructions: ${instructions}`); |
| 179 | |
| 180 | return { |
| 181 | text: lines.join("\n"), |
| 182 | structured: { |
| 183 | status: "waiting_for_interaction", |
| 184 | executionId: paused.id, |
| 185 | ...(deadline ? { expiresAt: deadline.expiresAt, ttlMs: deadline.ttlMs } : {}), |
| 186 | interaction: { |
| 187 | kind: isUrlElicitation ? "url" : "form", |
| 188 | message: req.message, |
| 189 | instructions, |
| 190 | address: String(paused.elicitationContext.address), |
no test coverage detected