@@ -964,9 +964,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
964964 const same = ag . model && model . providerID === ag . model . providerID && model . modelID === ag . model . modelID
965965 const full =
966966 ! input . variant && ag . variant && same
967- ? yield * provider
968- . getModel ( model . providerID , model . modelID )
969- . pipe ( Effect . catch ( ( ) => Effect . succeed ( undefined ) ) )
967+ ? yield * provider . getModel ( model . providerID , model . modelID ) . pipe ( Effect . catchDefect ( ( ) => Effect . void ) )
970968 : undefined
971969 const variant = input . variant ?? ( ag . variant && full ?. variants ?. [ ag . variant ] ? ag . variant : undefined )
972970
@@ -986,9 +984,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
986984 format : input . format ,
987985 }
988986
989- yield * Effect . addFinalizer ( ( ) =>
990- InstanceState . withALS ( ( ) => instruction . clear ( info . id ) ) . pipe ( Effect . flatMap ( ( x ) => x ) ) ,
991- )
987+ yield * Effect . addFinalizer ( ( ) => instruction . clear ( info . id ) )
992988
993989 type Draft < T > = T extends MessageV2 . Part ? Omit < T , "id" > & { id ?: string } : never
994990 const assign = ( part : Draft < MessageV2 . Part > ) : MessageV2 . Part => ( {
@@ -1459,110 +1455,104 @@ NOTE: At any point in time through this workflow you should feel free to ask the
14591455 model,
14601456 } )
14611457
1462- const outcome : "break" | "continue" = yield * Effect . onExit (
1463- Effect . gen ( function * ( ) {
1464- const lastUserMsg = msgs . findLast ( ( m ) => m . info . role === "user" )
1465- const bypassAgentCheck = lastUserMsg ?. parts . some ( ( p ) => p . type === "agent" ) ?? false
1466-
1467- const tools = yield * resolveTools ( {
1468- agent,
1469- session,
1470- model,
1471- tools : lastUser . tools ,
1472- processor : handle ,
1473- bypassAgentCheck,
1474- messages : msgs ,
1475- } )
1458+ const outcome : "break" | "continue" = yield * Effect . gen ( function * ( ) {
1459+ const lastUserMsg = msgs . findLast ( ( m ) => m . info . role === "user" )
1460+ const bypassAgentCheck = lastUserMsg ?. parts . some ( ( p ) => p . type === "agent" ) ?? false
14761461
1477- if ( lastUser . format ?. type === "json_schema" ) {
1478- tools [ "StructuredOutput" ] = createStructuredOutputTool ( {
1479- schema : lastUser . format . schema ,
1480- onSuccess ( output ) {
1481- structured = output
1482- } ,
1483- } )
1484- }
1462+ const tools = yield * resolveTools ( {
1463+ agent,
1464+ session,
1465+ model,
1466+ tools : lastUser . tools ,
1467+ processor : handle ,
1468+ bypassAgentCheck,
1469+ messages : msgs ,
1470+ } )
14851471
1486- if ( step === 1 ) SessionSummary . summarize ( { sessionID, messageID : lastUser . id } )
1487-
1488- if ( step > 1 && lastFinished ) {
1489- for ( const m of msgs ) {
1490- if ( m . info . role !== "user" || m . info . id <= lastFinished . id ) continue
1491- for ( const p of m . parts ) {
1492- if ( p . type !== "text" || p . ignored || p . synthetic ) continue
1493- if ( ! p . text . trim ( ) ) continue
1494- p . text = [
1495- "<system-reminder>" ,
1496- "The user sent the following message:" ,
1497- p . text ,
1498- "" ,
1499- "Please address this message and continue with your tasks." ,
1500- "</system-reminder>" ,
1501- ] . join ( "\n" )
1502- }
1472+ if ( lastUser . format ?. type === "json_schema" ) {
1473+ tools [ "StructuredOutput" ] = createStructuredOutputTool ( {
1474+ schema : lastUser . format . schema ,
1475+ onSuccess ( output ) {
1476+ structured = output
1477+ } ,
1478+ } )
1479+ }
1480+
1481+ if ( step === 1 ) SessionSummary . summarize ( { sessionID, messageID : lastUser . id } )
1482+
1483+ if ( step > 1 && lastFinished ) {
1484+ for ( const m of msgs ) {
1485+ if ( m . info . role !== "user" || m . info . id <= lastFinished . id ) continue
1486+ for ( const p of m . parts ) {
1487+ if ( p . type !== "text" || p . ignored || p . synthetic ) continue
1488+ if ( ! p . text . trim ( ) ) continue
1489+ p . text = [
1490+ "<system-reminder>" ,
1491+ "The user sent the following message:" ,
1492+ p . text ,
1493+ "" ,
1494+ "Please address this message and continue with your tasks." ,
1495+ "</system-reminder>" ,
1496+ ] . join ( "\n" )
15031497 }
15041498 }
1499+ }
15051500
1506- yield * plugin . trigger ( "experimental.chat.messages.transform" , { } , { messages : msgs } )
1507-
1508- const [ skills , env , instructions , modelMsgs ] = yield * Effect . all ( [
1509- Effect . promise ( ( ) => SystemPrompt . skills ( agent ) ) ,
1510- Effect . promise ( ( ) => SystemPrompt . environment ( model ) ) ,
1511- instruction . system ( ) . pipe ( Effect . orDie ) ,
1512- Effect . promise ( ( ) => MessageV2 . toModelMessages ( msgs , model ) ) ,
1513- ] )
1514- const system = [ ...env , ...( skills ? [ skills ] : [ ] ) , ...instructions ]
1515- const format = lastUser . format ?? { type : "text" as const }
1516- if ( format . type === "json_schema" ) system . push ( STRUCTURED_OUTPUT_SYSTEM_PROMPT )
1517- const result = yield * handle . process ( {
1518- user : lastUser ,
1519- agent,
1520- permission : session . permission ,
1521- sessionID,
1522- parentSessionID : session . parentID ,
1523- system,
1524- messages : [ ...modelMsgs , ...( isLastStep ? [ { role : "assistant" as const , content : MAX_STEPS } ] : [ ] ) ] ,
1525- tools,
1526- model,
1527- toolChoice : format . type === "json_schema" ? "required" : undefined ,
1528- } )
1501+ yield * plugin . trigger ( "experimental.chat.messages.transform" , { } , { messages : msgs } )
1502+
1503+ const [ skills , env , instructions , modelMsgs ] = yield * Effect . all ( [
1504+ Effect . promise ( ( ) => SystemPrompt . skills ( agent ) ) ,
1505+ Effect . promise ( ( ) => SystemPrompt . environment ( model ) ) ,
1506+ instruction . system ( ) . pipe ( Effect . orDie ) ,
1507+ Effect . promise ( ( ) => MessageV2 . toModelMessages ( msgs , model ) ) ,
1508+ ] )
1509+ const system = [ ...env , ...( skills ? [ skills ] : [ ] ) , ...instructions ]
1510+ const format = lastUser . format ?? { type : "text" as const }
1511+ if ( format . type === "json_schema" ) system . push ( STRUCTURED_OUTPUT_SYSTEM_PROMPT )
1512+ const result = yield * handle . process ( {
1513+ user : lastUser ,
1514+ agent,
1515+ permission : session . permission ,
1516+ sessionID,
1517+ parentSessionID : session . parentID ,
1518+ system,
1519+ messages : [ ...modelMsgs , ...( isLastStep ? [ { role : "assistant" as const , content : MAX_STEPS } ] : [ ] ) ] ,
1520+ tools,
1521+ model,
1522+ toolChoice : format . type === "json_schema" ? "required" : undefined ,
1523+ } )
1524+
1525+ if ( structured !== undefined ) {
1526+ handle . message . structured = structured
1527+ handle . message . finish = handle . message . finish ?? "stop"
1528+ yield * sessions . updateMessage ( handle . message )
1529+ return "break" as const
1530+ }
15291531
1530- if ( structured !== undefined ) {
1531- handle . message . structured = structured
1532- handle . message . finish = handle . message . finish ?? "stop"
1532+ const finished = handle . message . finish && ! [ "tool-calls" , "unknown" ] . includes ( handle . message . finish )
1533+ if ( finished && ! handle . message . error ) {
1534+ if ( format . type === "json_schema" ) {
1535+ handle . message . error = new MessageV2 . StructuredOutputError ( {
1536+ message : "Model did not produce structured output" ,
1537+ retries : 0 ,
1538+ } ) . toObject ( )
15331539 yield * sessions . updateMessage ( handle . message )
15341540 return "break" as const
15351541 }
1542+ }
15361543
1537- const finished = handle . message . finish && ! [ "tool-calls" , "unknown" ] . includes ( handle . message . finish )
1538- if ( finished && ! handle . message . error ) {
1539- if ( format . type === "json_schema" ) {
1540- handle . message . error = new MessageV2 . StructuredOutputError ( {
1541- message : "Model did not produce structured output" ,
1542- retries : 0 ,
1543- } ) . toObject ( )
1544- yield * sessions . updateMessage ( handle . message )
1545- return "break" as const
1546- }
1547- }
1548-
1549- if ( result === "stop" ) return "break" as const
1550- if ( result === "compact" ) {
1551- yield * compaction . create ( {
1552- sessionID,
1553- agent : lastUser . agent ,
1554- model : lastUser . model ,
1555- auto : true ,
1556- overflow : ! handle . message . finish ,
1557- } )
1558- }
1559- return "continue" as const
1560- } ) ,
1561- Effect . fnUntraced ( function * ( exit ) {
1562- if ( Exit . isFailure ( exit ) && Cause . hasInterruptsOnly ( exit . cause ) ) yield * handle . abort ( )
1563- yield * InstanceState . withALS ( ( ) => instruction . clear ( handle . message . id ) ) . pipe ( Effect . flatMap ( ( x ) => x ) )
1564- } ) ,
1565- )
1544+ if ( result === "stop" ) return "break" as const
1545+ if ( result === "compact" ) {
1546+ yield * compaction . create ( {
1547+ sessionID,
1548+ agent : lastUser . agent ,
1549+ model : lastUser . model ,
1550+ auto : true ,
1551+ overflow : ! handle . message . finish ,
1552+ } )
1553+ }
1554+ return "continue" as const
1555+ } ) . pipe ( Effect . ensuring ( instruction . clear ( handle . message . id ) ) )
15661556 if ( outcome === "break" ) break
15671557 continue
15681558 }
0 commit comments