@@ -44,6 +44,7 @@ import {
4444 type SDKRateLimitInfo ,
4545 type SDKResultMessage ,
4646 type SDKUserMessage ,
47+ type UserDialogRequest ,
4748} from "@anthropic-ai/claude-agent-sdk" ;
4849import type {
4950 AskUserQuestionInput ,
@@ -56,7 +57,7 @@ import { parseCliArgs } from "@t3tools/shared/cliArgs";
5657import { fromLenientJson } from "@t3tools/shared/schemaJson" ;
5758import { AgentScope } from "@t3tools/shared/AgentScope" ;
5859import * as HostProcess from "@t3tools/shared/HostProcess" ;
59- import { applyClaudePromptEffortPrefix } from "@t3tools/shared/model" ;
60+ import { applyClaudePromptEffortPrefix , formatModelSlugName } from "@t3tools/shared/model" ;
6061import {
6162 CLAUDE_RESUME_COMPACTION_NEVER_ANSWER ,
6263 formatClaudeResumeCompactionQuestion ,
@@ -9053,12 +9054,68 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90539054 const canUseTool : CanUseTool = ( toolName , toolInput , callbackOptions ) =>
90549055 runPromise ( canUseToolEffect ( toolName , toolInput , callbackOptions ) ) ;
90559056
9057+ const askDialogQuestion = (
9058+ request : UserDialogRequest ,
9059+ callbackOptions : { readonly signal : AbortSignal ; readonly requestId : string } ,
9060+ prompt : {
9061+ readonly header : string ;
9062+ readonly question : string ;
9063+ readonly options : ReadonlyArray < {
9064+ readonly label : string ;
9065+ readonly description : string ;
9066+ } > ;
9067+ } ,
9068+ ) =>
9069+ canUseToolEffect (
9070+ "AskUserQuestion" ,
9071+ { questions : [ { ...prompt , multiSelect : false } ] } ,
9072+ {
9073+ signal : callbackOptions . signal ,
9074+ requestId : callbackOptions . requestId ,
9075+ toolUseID : request . toolUseID ?? callbackOptions . requestId ,
9076+ } ,
9077+ ) . pipe (
9078+ Effect . map ( ( result ) => {
9079+ if ( result . behavior !== "allow" ) return Option . none ( ) ;
9080+ const answers =
9081+ result . updatedInput === undefined
9082+ ? undefined
9083+ : Reflect . get ( result . updatedInput , "answers" ) ;
9084+ return Option . some (
9085+ typeof answers === "object" && answers !== null
9086+ ? Reflect . get ( answers , prompt . question )
9087+ : undefined ,
9088+ ) ;
9089+ } ) ,
9090+ ) ;
9091+
90569092 const onUserDialog : NonNullable < ClaudeQueryOptions [ "onUserDialog" ] > = (
90579093 request ,
90589094 callbackOptions ,
90599095 ) =>
90609096 runPromise (
90619097 Effect . gen ( function * ( ) {
9098+ if ( request . dialogKind === "refusal_fallback_prompt" ) {
9099+ const { originalModel, fallbackModel } = request . payload ;
9100+ if ( typeof originalModel !== "string" || typeof fallbackModel !== "string" ) {
9101+ return { behavior : "cancelled" as const } ;
9102+ }
9103+ const retryLabel = `Retry with ${ formatModelSlugName ( fallbackModel ) } ` ;
9104+ const selection = yield * askDialogQuestion ( request , callbackOptions , {
9105+ header : "Message flagged" ,
9106+ question : `${ formatModelSlugName ( originalModel ) } 's safeguards flagged this message.` ,
9107+ options : [
9108+ {
9109+ label : retryLabel ,
9110+ description : "Send the same message again on the fallback model." ,
9111+ } ,
9112+ { label : "Stop" , description : "End the turn without a response." } ,
9113+ ] ,
9114+ } ) ;
9115+ return Option . isSome ( selection ) && selection . value === retryLabel
9116+ ? { behavior : "completed" as const , result : "retry_fallback" as const }
9117+ : { behavior : "cancelled" as const } ;
9118+ }
90629119 if ( request . dialogKind !== "resume_return" ) {
90639120 return { behavior : "cancelled" as const } ;
90649121 }
@@ -9087,52 +9144,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90879144 ageMinutes,
90889145 estimatedTokens,
90899146 } ) ;
9090- const result = yield * canUseToolEffect (
9091- "AskUserQuestion" ,
9092- {
9093- questions : [
9094- {
9095- header : "Resume session" ,
9096- question,
9097- options : [
9098- {
9099- label : "Compact and continue" ,
9100- description : "Resume with a summary and use fewer tokens." ,
9101- } ,
9102- {
9103- label : "Keep full history" ,
9104- description : "Resume without changing the conversation." ,
9105- } ,
9106- {
9107- label : CLAUDE_RESUME_COMPACTION_NEVER_ANSWER ,
9108- description : "Keep full history and skip future resume prompts." ,
9109- } ,
9110- ] ,
9111- multiSelect : false ,
9112- } ,
9113- ] ,
9114- } ,
9115- {
9116- signal : callbackOptions . signal ,
9117- requestId : callbackOptions . requestId ,
9118- toolUseID : request . toolUseID ?? callbackOptions . requestId ,
9119- } ,
9120- ) ;
9121- if ( result . behavior !== "allow" ) return { behavior : "cancelled" as const } ;
9122- const answers =
9123- result . updatedInput === undefined
9124- ? undefined
9125- : Reflect . get ( result . updatedInput , "answers" ) ;
9126- const selection =
9127- typeof answers === "object" && answers !== null
9128- ? Reflect . get ( answers , question )
9129- : undefined ;
9147+ const selection = yield * askDialogQuestion ( request , callbackOptions , {
9148+ header : "Resume session" ,
9149+ question,
9150+ options : [
9151+ {
9152+ label : "Compact and continue" ,
9153+ description : "Resume with a summary and use fewer tokens." ,
9154+ } ,
9155+ {
9156+ label : "Keep full history" ,
9157+ description : "Resume without changing the conversation." ,
9158+ } ,
9159+ {
9160+ label : CLAUDE_RESUME_COMPACTION_NEVER_ANSWER ,
9161+ description : "Keep full history and skip future resume prompts." ,
9162+ } ,
9163+ ] ,
9164+ } ) ;
9165+ if ( Option . isNone ( selection ) ) return { behavior : "cancelled" as const } ;
91309166 return {
91319167 behavior : "completed" as const ,
91329168 result :
9133- selection === "Compact and continue"
9169+ selection . value === "Compact and continue"
91349170 ? ( "compact" as const )
9135- : selection === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER
9171+ : selection . value === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER
91369172 ? ( "never" as const )
91379173 : ( "continue" as const ) ,
91389174 } ;
@@ -9315,7 +9351,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93159351 } ) ,
93169352 canUseTool,
93179353 onUserDialog,
9318- supportedDialogKinds : [ "resume_return" ] ,
9354+ supportedDialogKinds : [ "resume_return" , "refusal_fallback_prompt" ] ,
93199355 ...( appendSystemPromptFileText === undefined ? { } : { appendSystemPromptFileText } ) ,
93209356 } ) ;
93219357 const querySession = yield * queryRunner
0 commit comments