@@ -20,6 +20,62 @@ import { Agent } from "@/agent/agent"
2020export namespace SessionSummary {
2121 const log = Log . create ( { service : "session.summary" } )
2222
23+ function unquoteGitPath ( input : string ) {
24+ if ( ! input . startsWith ( '"' ) ) return input
25+ if ( ! input . endsWith ( '"' ) ) return input
26+ const body = input . slice ( 1 , - 1 )
27+ const bytes : number [ ] = [ ]
28+
29+ for ( let i = 0 ; i < body . length ; i ++ ) {
30+ const char = body [ i ] !
31+ if ( char !== "\\" ) {
32+ bytes . push ( char . charCodeAt ( 0 ) )
33+ continue
34+ }
35+
36+ const next = body [ i + 1 ]
37+ if ( ! next ) {
38+ bytes . push ( "\\" . charCodeAt ( 0 ) )
39+ continue
40+ }
41+
42+ if ( next >= "0" && next <= "7" ) {
43+ const chunk = body . slice ( i + 1 , i + 4 )
44+ const match = chunk . match ( / ^ [ 0 - 7 ] { 1 , 3 } / )
45+ if ( ! match ) {
46+ bytes . push ( next . charCodeAt ( 0 ) )
47+ i ++
48+ continue
49+ }
50+ bytes . push ( parseInt ( match [ 0 ] , 8 ) )
51+ i += match [ 0 ] . length
52+ continue
53+ }
54+
55+ const escaped =
56+ next === "n"
57+ ? "\n"
58+ : next === "r"
59+ ? "\r"
60+ : next === "t"
61+ ? "\t"
62+ : next === "b"
63+ ? "\b"
64+ : next === "f"
65+ ? "\f"
66+ : next === "v"
67+ ? "\v"
68+ : next === "\\" || next === '"'
69+ ? next
70+ : undefined
71+
72+ bytes . push ( ( escaped ?? next ) . charCodeAt ( 0 ) )
73+ i ++
74+ }
75+
76+ return Buffer . from ( bytes ) . toString ( )
77+ }
78+
2379 export const summarize = fn (
2480 z . object ( {
2581 sessionID : z . string ( ) ,
@@ -116,7 +172,18 @@ export namespace SessionSummary {
116172 messageID : Identifier . schema ( "message" ) . optional ( ) ,
117173 } ) ,
118174 async ( input ) => {
119- return Storage . read < Snapshot . FileDiff [ ] > ( [ "session_diff" , input . sessionID ] ) . catch ( ( ) => [ ] )
175+ const diffs = await Storage . read < Snapshot . FileDiff [ ] > ( [ "session_diff" , input . sessionID ] ) . catch ( ( ) => [ ] )
176+ const next = diffs . map ( ( item ) => {
177+ const file = unquoteGitPath ( item . file )
178+ if ( file === item . file ) return item
179+ return {
180+ ...item ,
181+ file,
182+ }
183+ } )
184+ const changed = next . some ( ( item , i ) => item . file !== diffs [ i ] ?. file )
185+ if ( changed ) Storage . write ( [ "session_diff" , input . sessionID ] , next ) . catch ( ( ) => { } )
186+ return next
120187 } ,
121188 )
122189
0 commit comments