11import { createLogger } from '@sim/logger'
2- import type { QueryResult , Session } from 'neo4j-driver'
2+ import type { Record as Neo4jRecord , Result , ResultSummary , Session } from 'neo4j-driver'
33import type {
44 Neo4jCreateRequest ,
55 Neo4jDeleteRequest ,
@@ -11,6 +11,7 @@ import type {
1111} from '@/lib/api/contracts/tools/databases/neo4j'
1212import { createNeo4jDriver } from '@/lib/internal/neo4j/client'
1313import { convertNeo4jValue } from '@/lib/internal/neo4j/values'
14+ import { createResponseRowsBudget } from '@/lib/internal/tool-operations/response-limits'
1415import type { Neo4jNodeSchema , Neo4jRelationshipSchema } from '@/tools/neo4j/types'
1516
1617const logger = createLogger ( 'Neo4jOperations' )
@@ -43,21 +44,44 @@ function bindSessionAbort(session: Session, signal?: AbortSignal): () => void {
4344 return ( ) => signal . removeEventListener ( 'abort' , abort )
4445}
4546
46- function projectRecords ( result : QueryResult ) : Array < Record < string , unknown > > {
47- return result . records . map ( ( record ) => {
48- const projected : Record < string , unknown > = { }
49- for ( const key of record . keys ) {
50- if ( typeof key === 'string' ) projected [ key ] = convertNeo4jValue ( record . get ( key ) )
47+ function projectRecord ( record : Neo4jRecord ) : Record < string , unknown > {
48+ const projected : Record < string , unknown > = { }
49+ for ( const key of record . keys ) {
50+ if ( typeof key === 'string' ) projected [ key ] = convertNeo4jValue ( record . get ( key ) )
51+ }
52+ return projected
53+ }
54+
55+ /**
56+ * Streams the records, failing with the tool response size error as soon as they cannot fit the
57+ * response. Returning the iterator early tells the server to discard the rest, so an oversized
58+ * result is never buffered in full. `delete` returns no records, so it only waits for the summary.
59+ */
60+ async function readResult ( result : Result , kind : StatementKind ) {
61+ if ( kind === 'delete' ) return { records : [ ] , summary : await result . summary ( ) }
62+ const budget = createResponseRowsBudget ( `neo4j_${ kind } ` )
63+ const records : Array < Record < string , unknown > > = [ ]
64+ const iterator = result [ Symbol . asyncIterator ] ( )
65+ try {
66+ let next = await iterator . next ( )
67+ while ( ! next . done ) {
68+ const projected = projectRecord ( next . value )
69+ budget . admit ( projected )
70+ records . push ( projected )
71+ next = await iterator . next ( )
5172 }
52- return projected
53- } )
73+ return { records, summary : next . value }
74+ } catch ( error ) {
75+ await iterator . return ?.( ) . catch ( ( ) => undefined )
76+ throw error
77+ }
5478}
5579
56- function projectSummary ( result : QueryResult ) {
57- const updates = result . summary . counters . updates ( )
80+ function projectSummary ( summary : ResultSummary ) {
81+ const updates = summary . counters . updates ( )
5882 return {
59- resultAvailableAfter : result . summary . resultAvailableAfter . toNumber ( ) ,
60- resultConsumedAfter : result . summary . resultConsumedAfter . toNumber ( ) ,
83+ resultAvailableAfter : summary . resultAvailableAfter . toNumber ( ) ,
84+ resultConsumedAfter : summary . resultConsumedAfter . toNumber ( ) ,
6185 counters : {
6286 nodesCreated : updates . nodesCreated ,
6387 nodesDeleted : updates . nodesDeleted ,
@@ -74,9 +98,11 @@ function projectSummary(result: QueryResult) {
7498 }
7599}
76100
77- function statementResponse ( kind : StatementKind , result : QueryResult ) {
78- const records = projectRecords ( result )
79- const summary = projectSummary ( result )
101+ function statementResponse (
102+ kind : StatementKind ,
103+ { records, summary : resultSummary } : Awaited < ReturnType < typeof readResult > >
104+ ) {
105+ const summary = projectSummary ( resultSummary )
80106 switch ( kind ) {
81107 case 'query' :
82108 return {
@@ -128,7 +154,7 @@ async function executeStatement(input: StatementInput, kind: StatementKind, sign
128154 const session = driver . session ( { database : input . database } )
129155 const unbindAbort = bindSessionAbort ( session , signal )
130156 try {
131- const result = await session . run ( input . cypherQuery , input . parameters ?? { } )
157+ const result = await readResult ( session . run ( input . cypherQuery , input . parameters ?? { } ) , kind )
132158 signal ?. throwIfAborted ( )
133159 return statementResponse ( kind , result )
134160 } finally {
0 commit comments