@@ -21,6 +21,7 @@ import type {
2121 OrchestrationV2ExecutionNode ,
2222 OrchestrationV2PlanArtifact ,
2323 OrchestrationV2PendingRuntimeRequestSummary ,
24+ OrchestrationV2RuntimeRequest ,
2425 PlanId ,
2526 OrchestrationV2ConversationMessage ,
2627 OrchestrationV2DomainEvent ,
@@ -67,6 +68,7 @@ import {
6768 TurnItemId ,
6869 NodeId ,
6970 ProviderInstanceId ,
71+ ProviderRequestKind ,
7072} from "@t3tools/contracts" ;
7173import {
7274 createOrchestrationV2TurnItemVisibility ,
@@ -297,11 +299,22 @@ export type ProjectionRuntimeRecoveryState = Pick<
297299 | "turnItems"
298300> ;
299301
300- export type ProjectionPendingUserInputs = Pick <
302+ export type ProjectionPendingNativeRequests = Pick <
301303 OrchestrationV2ThreadProjection ,
302304 "runtimeRequests" | "nodes" | "turnItems"
303305> ;
304306
307+ const isApprovalRequestKind = Schema . is ( ProviderRequestKind ) ;
308+
309+ function isPendingNativeRequest (
310+ request : OrchestrationV2RuntimeRequest ,
311+ includeApprovals : boolean ,
312+ ) : boolean {
313+ return request . kind === "user_input"
314+ ? request . responseCapability . type !== "message"
315+ : includeApprovals && isApprovalRequestKind ( request . kind ) ;
316+ }
317+
305318export type ProjectionThreadProviderContext = Pick <
306319 OrchestrationV2ThreadProjection ,
307320 "thread" | "providerSessions" | "providerThreads"
@@ -486,10 +499,11 @@ export interface ProjectionStoreV2Shape {
486499 readonly getRuntimeRecoveryProjection : (
487500 threadId : ThreadId ,
488501 ) => Effect . Effect < ProjectionRuntimeRecoveryState , ProjectionStoreV2Error > ;
489- readonly getPendingNativeUserInputs : (
502+ readonly getPendingNativeRequests : (
490503 threadId : ThreadId ,
491504 providerTurnId : ProviderTurnId ,
492- ) => Effect . Effect < ProjectionPendingUserInputs , ProjectionStoreV2Error > ;
505+ options : { readonly includeApprovals : boolean } ,
506+ ) => Effect . Effect < ProjectionPendingNativeRequests , ProjectionStoreV2Error > ;
493507 readonly getPlan : (
494508 threadId : ThreadId ,
495509 planId : PlanId ,
@@ -4498,22 +4512,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
44984512 ? cause
44994513 : new ProjectionStoreReadError ( { threadId, cause } ) ;
45004514
4501- const getPendingNativeUserInputs : ProjectionStoreV2Shape [ "getPendingNativeUserInputs " ] = (
4515+ const getPendingNativeRequests : ProjectionStoreV2Shape [ "getPendingNativeRequests " ] = (
45024516 threadId ,
45034517 providerTurnId ,
4518+ { includeApprovals } ,
45044519 ) =>
45054520 sql
45064521 . withTransaction (
45074522 Effect . gen ( function * ( ) {
45084523 const rows = yield * sql < PayloadRow > `
45094524 SELECT payload_json FROM orchestration_v2_projection_runtime_requests
45104525 WHERE thread_id = ${ threadId } AND provider_turn_id = ${ providerTurnId }
4511- AND kind = 'user_input' AND status = 'pending'
4526+ AND status = 'pending'
45124527 ` ;
45134528 const runtimeRequests = ( yield * decodeRows (
45144529 decodeRuntimeRequestPayload ,
45154530 threadId ,
4516- ) ( rows ) ) . filter ( ( request ) => request . responseCapability . type !== "message" ) ;
4531+ ) ( rows ) ) . filter ( ( request ) => isPendingNativeRequest ( request , includeApprovals ) ) ;
45174532 if ( runtimeRequests . length === 0 ) return { runtimeRequests, nodes : [ ] , turnItems : [ ] } ;
45184533 const nodeRows = yield * sql < PayloadRow > `
45194534 SELECT payload_json FROM orchestration_v2_projection_nodes
@@ -4525,14 +4540,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
45254540 const itemRows = yield * sql < PayloadRow > `
45264541 SELECT payload_json FROM orchestration_v2_projection_turn_items
45274542 WHERE thread_id = ${ threadId } AND provider_turn_id = ${ providerTurnId }
4528- AND type = 'user_input_request'
4543+ AND type IN ( 'user_input_request', 'approval_request')
45294544 ` ;
45304545 const requestIds = new Set ( runtimeRequests . map ( ( request ) => request . id ) ) ;
45314546 return {
45324547 runtimeRequests,
45334548 nodes : yield * decodeRows ( decodeNodePayload , threadId ) ( nodeRows ) ,
45344549 turnItems : ( yield * decodeRows ( decodeTurnItemPayload , threadId ) ( itemRows ) ) . filter (
4535- ( item ) => item . type === "user_input_request" && requestIds . has ( item . requestId ) ,
4550+ ( item ) =>
4551+ ( item . type === "user_input_request" || item . type === "approval_request" ) &&
4552+ requestIds . has ( item . requestId ) ,
45364553 ) ,
45374554 } ;
45384555 } ) ,
@@ -6316,7 +6333,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
63166333 getCheckpointRollbackContext,
63176334 getRunMessage,
63186335 canStartQueuedRun,
6319- getPendingNativeUserInputs ,
6336+ getPendingNativeRequests ,
63206337 hasUnpairedRunInterruptRequest,
63216338 isNodeStored,
63226339 getMessageCount,
@@ -6556,28 +6573,31 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
65566573 . map ( ( projection ) => projection . thread . id ) ;
65576574 } ) ,
65586575 getUnreadableThreadIds : ( ) => Effect . succeed ( [ ] ) ,
6559- getPendingNativeUserInputs : Effect . fn ( "ProjectionStore.getPendingNativeUserInputs" ) (
6560- function * ( threadId , providerTurnId ) {
6561- const projection = ( yield * Ref . get ( replayState ) ) . projections . get ( threadId ) ;
6562- if ( projection === undefined ) return { runtimeRequests : [ ] , nodes : [ ] , turnItems : [ ] } ;
6563- const runtimeRequests = projection . runtimeRequests . filter (
6564- ( request ) =>
6565- request . providerTurnId === providerTurnId &&
6566- request . kind === "user_input" &&
6567- request . status === "pending" &&
6568- request . responseCapability . type !== "message" ,
6569- ) ;
6570- const requestIds = new Set ( runtimeRequests . map ( ( request ) => request . id ) ) ;
6571- const nodeIds = new Set ( runtimeRequests . map ( ( request ) => request . nodeId ) ) ;
6572- return {
6573- runtimeRequests,
6574- nodes : projection . nodes . filter ( ( node ) => nodeIds . has ( node . id ) ) ,
6575- turnItems : projection . turnItems . filter (
6576- ( item ) => item . type === "user_input_request" && requestIds . has ( item . requestId ) ,
6577- ) ,
6578- } ;
6579- } ,
6580- ) ,
6576+ getPendingNativeRequests : Effect . fn ( "ProjectionStore.getPendingNativeRequests" ) ( function * (
6577+ threadId ,
6578+ providerTurnId ,
6579+ { includeApprovals } ,
6580+ ) {
6581+ const projection = ( yield * Ref . get ( replayState ) ) . projections . get ( threadId ) ;
6582+ if ( projection === undefined ) return { runtimeRequests : [ ] , nodes : [ ] , turnItems : [ ] } ;
6583+ const runtimeRequests = projection . runtimeRequests . filter (
6584+ ( request ) =>
6585+ request . providerTurnId === providerTurnId &&
6586+ request . status === "pending" &&
6587+ isPendingNativeRequest ( request , includeApprovals ) ,
6588+ ) ;
6589+ const requestIds = new Set ( runtimeRequests . map ( ( request ) => request . id ) ) ;
6590+ const nodeIds = new Set ( runtimeRequests . map ( ( request ) => request . nodeId ) ) ;
6591+ return {
6592+ runtimeRequests,
6593+ nodes : projection . nodes . filter ( ( node ) => nodeIds . has ( node . id ) ) ,
6594+ turnItems : projection . turnItems . filter (
6595+ ( item ) =>
6596+ ( item . type === "user_input_request" || item . type === "approval_request" ) &&
6597+ requestIds . has ( item . requestId ) ,
6598+ ) ,
6599+ } ;
6600+ } ) ,
65816601 getPlan : Effect . fn ( "ProjectionStore.getPlan" ) ( function * ( threadId , planId ) {
65826602 const projection = ( yield * Ref . get ( replayState ) ) . projections . get ( threadId ) ;
65836603 if ( projection === undefined )
0 commit comments