(
paused: PausedExecution,
options?: { readonly deadline?: PausedExecutionDeadline },
)
| 172 | }; |
| 173 | |
| 174 | export const formatPausedExecution = ( |
| 175 | paused: PausedExecution, |
| 176 | options?: { readonly deadline?: PausedExecutionDeadline }, |
| 177 | ): { |
| 178 | text: string; |
| 179 | structured: Record<string, unknown>; |
| 180 | } => { |
| 181 | const req = paused.elicitationContext.request; |
| 182 | const lines: string[] = [`Execution paused: ${req.message}`]; |
| 183 | const deadline = options?.deadline; |
| 184 | const isUrlElicitation = Predicate.isTagged(req, "UrlElicitation"); |
| 185 | const isFormElicitation = Predicate.isTagged(req, "FormElicitation"); |
| 186 | const requestedSchema = isFormElicitation ? req.requestedSchema : undefined; |
| 187 | const hasRequestedSchema = |
| 188 | requestedSchema !== undefined && Object.keys(requestedSchema).length > 0; |
| 189 | const baseInstructions = isUrlElicitation |
| 190 | ? `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".` |
| 191 | : hasRequestedSchema |
| 192 | ? `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".` |
| 193 | : `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".`; |
| 194 | const deadlineInstructions = deadline |
| 195 | ? ` Resume before ${deadline.expiresAt}; this approval window lasts ${formatTtlDuration(deadline.ttlMs)}.` |
| 196 | : ""; |
| 197 | const instructions = `${baseInstructions}${deadlineInstructions}`; |
| 198 | |
| 199 | if (isUrlElicitation) { |
| 200 | lines.push(`\nOpen this URL in a browser:\n${req.url}`); |
| 201 | lines.push('\nAfter the browser flow, call the resume tool with action "accept".'); |
| 202 | } else if (hasRequestedSchema) { |
| 203 | lines.push( |
| 204 | "\nAsk the user for a response matching the requested schema, then call the resume tool.", |
| 205 | ); |
| 206 | lines.push(`\nRequested schema:\n${JSON.stringify(requestedSchema, null, 2)}`); |
| 207 | } else { |
| 208 | lines.push( |
| 209 | '\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".', |
| 210 | ); |
| 211 | } |
| 212 | |
| 213 | lines.push(`\nexecutionId: ${paused.id}`); |
| 214 | if (deadline) { |
| 215 | lines.push( |
| 216 | `\nresumeDeadline: ${deadline.expiresAt} (${formatTtlDuration(deadline.ttlMs)} approval window)`, |
| 217 | ); |
| 218 | } |
| 219 | lines.push(`\ninstructions: ${instructions}`); |
| 220 | |
| 221 | return { |
| 222 | text: lines.join("\n"), |
| 223 | structured: { |
| 224 | status: "waiting_for_interaction", |
| 225 | executionId: paused.id, |
| 226 | ...(deadline ? { expiresAt: deadline.expiresAt, ttlMs: deadline.ttlMs } : {}), |
| 227 | interaction: { |
| 228 | kind: isUrlElicitation ? "url" : "form", |
| 229 | message: req.message, |
| 230 | instructions, |
| 231 | address: String(paused.elicitationContext.address), |
no test coverage detected