@@ -59,15 +59,18 @@ func (c *PastedContent) UnmarshalJSON(data []byte) error {
5959 if err := json .Unmarshal (data , & fields ); err != nil {
6060 return err
6161 }
62- * c = PastedContent {
63- ID : historyIntJSONField ( fields , historyIDFieldNames () ... ),
64- Type : canonicalPastedContentType ( historyStringJSONField ( fields , "type" , "kind" , "pastedType" , "pasted_type" )),
65- Content : historyStringJSONField ( fields , "content" , "value" , "data" , "base64" ),
66- MediaType : historyStringJSONField ( fields , "mediaType" , "media_type" , "mimeType" , "mime_type" , "contentType" , "content_type" ),
67- Filename : historyStringJSONField ( fields , "filename" , "fileName" , "file_name" , "name" ),
68- Dimensions : historyDimensionsJSONField ( fields , "dimensions" , "imageDimensions" , "image_dimensions" ),
69- SourcePath : historyStringJSONField ( fields , "sourcePath" , "source_path" , "filePath" , "file_path" , "path" ),
62+ if nested , ok := historyWrappedPayloadJSON ( fields , historyPastedContentWrapperFieldNames (), historyPastedContentValueFieldNames (), nil ); ok {
63+ var content PastedContent
64+ if err := json . Unmarshal ( nested , & content ); err != nil {
65+ return err
66+ }
67+ historyApplyPastedContentFields ( & content , fields , false )
68+ * c = content
69+ return nil
7070 }
71+ var content PastedContent
72+ historyApplyPastedContentFields (& content , fields , true )
73+ * c = content
7174 return nil
7275}
7376
@@ -76,19 +79,72 @@ func (c *StoredPastedContent) UnmarshalJSON(data []byte) error {
7679 if err := json .Unmarshal (data , & fields ); err != nil {
7780 return err
7881 }
79- * c = StoredPastedContent {
80- ID : historyIntJSONField (fields , historyIDFieldNames ()... ),
81- Type : canonicalPastedContentType (historyStringJSONField (fields , "type" , "kind" , "pastedType" , "pasted_type" )),
82- Content : historyStringJSONField (fields , "content" , "value" , "data" , "base64" ),
83- ContentHash : historyStringJSONField (fields , "contentHash" , "content_hash" , "hash" , "contentDigest" , "content_digest" ),
84- MediaType : historyStringJSONField (fields , "mediaType" , "media_type" , "mimeType" , "mime_type" , "contentType" , "content_type" ),
85- Filename : historyStringJSONField (fields , "filename" , "fileName" , "file_name" , "name" ),
86- Dimensions : historyDimensionsJSONField (fields , "dimensions" , "imageDimensions" , "image_dimensions" ),
87- SourcePath : historyStringJSONField (fields , "sourcePath" , "source_path" , "filePath" , "file_path" , "path" ),
82+ if nested , ok := historyWrappedPayloadJSON (fields , historyPastedContentWrapperFieldNames (), historyPastedContentValueFieldNames (), nil ); ok {
83+ var content StoredPastedContent
84+ if err := json .Unmarshal (nested , & content ); err != nil {
85+ return err
86+ }
87+ historyApplyStoredPastedContentFields (& content , fields , false )
88+ * c = content
89+ return nil
8890 }
91+ var content StoredPastedContent
92+ historyApplyStoredPastedContentFields (& content , fields , true )
93+ * c = content
8994 return nil
9095}
9196
97+ func historyApplyPastedContentFields (content * PastedContent , fields map [string ]json.RawMessage , overwrite bool ) {
98+ if value := historyIntJSONField (fields , historyIDFieldNames ()... ); value > 0 && (overwrite || content .ID == 0 ) {
99+ content .ID = value
100+ }
101+ if value := canonicalPastedContentType (historyStringJSONField (fields , "type" , "kind" , "pastedType" , "pasted_type" )); value != "" && (overwrite || content .Type == "" ) {
102+ content .Type = value
103+ }
104+ if value := historyStringJSONField (fields , "content" , "value" , "data" , "base64" ); value != "" && (overwrite || content .Content == "" ) {
105+ content .Content = value
106+ }
107+ if value := historyStringJSONField (fields , "mediaType" , "media_type" , "mimeType" , "mime_type" , "contentType" , "content_type" ); value != "" && (overwrite || content .MediaType == "" ) {
108+ content .MediaType = value
109+ }
110+ if value := historyStringJSONField (fields , "filename" , "fileName" , "file_name" , "name" ); value != "" && (overwrite || content .Filename == "" ) {
111+ content .Filename = value
112+ }
113+ if value := historyDimensionsJSONField (fields , "dimensions" , "imageDimensions" , "image_dimensions" ); value != nil && (overwrite || content .Dimensions == nil ) {
114+ content .Dimensions = value
115+ }
116+ if value := historyStringJSONField (fields , "sourcePath" , "source_path" , "filePath" , "file_path" , "path" ); value != "" && (overwrite || content .SourcePath == "" ) {
117+ content .SourcePath = value
118+ }
119+ }
120+
121+ func historyApplyStoredPastedContentFields (content * StoredPastedContent , fields map [string ]json.RawMessage , overwrite bool ) {
122+ if value := historyIntJSONField (fields , historyIDFieldNames ()... ); value > 0 && (overwrite || content .ID == 0 ) {
123+ content .ID = value
124+ }
125+ if value := canonicalPastedContentType (historyStringJSONField (fields , "type" , "kind" , "pastedType" , "pasted_type" )); value != "" && (overwrite || content .Type == "" ) {
126+ content .Type = value
127+ }
128+ if value := historyStringJSONField (fields , "content" , "value" , "data" , "base64" ); value != "" && (overwrite || content .Content == "" ) {
129+ content .Content = value
130+ }
131+ if value := historyStringJSONField (fields , "contentHash" , "content_hash" , "hash" , "contentDigest" , "content_digest" ); value != "" && (overwrite || content .ContentHash == "" ) {
132+ content .ContentHash = value
133+ }
134+ if value := historyStringJSONField (fields , "mediaType" , "media_type" , "mimeType" , "mime_type" , "contentType" , "content_type" ); value != "" && (overwrite || content .MediaType == "" ) {
135+ content .MediaType = value
136+ }
137+ if value := historyStringJSONField (fields , "filename" , "fileName" , "file_name" , "name" ); value != "" && (overwrite || content .Filename == "" ) {
138+ content .Filename = value
139+ }
140+ if value := historyDimensionsJSONField (fields , "dimensions" , "imageDimensions" , "image_dimensions" ); value != nil && (overwrite || content .Dimensions == nil ) {
141+ content .Dimensions = value
142+ }
143+ if value := historyStringJSONField (fields , "sourcePath" , "source_path" , "filePath" , "file_path" , "path" ); value != "" && (overwrite || content .SourcePath == "" ) {
144+ content .SourcePath = value
145+ }
146+ }
147+
92148func canonicalPastedContentType (value string ) string {
93149 trimmed := strings .TrimSpace (value )
94150 normalized := strings .ToLower (trimmed )
@@ -338,10 +394,18 @@ func (e *HistoryEntry) UnmarshalJSON(data []byte) error {
338394 if err := json .Unmarshal (data , & fields ); err != nil {
339395 return err
340396 }
341- * e = HistoryEntry {
342- Display : historyStringJSONField (fields , "display" , "prompt" , "text" , "input" , "content" , "value" ),
343- PastedContents : historyPastedContentsJSONField (fields , historyPastedContentContainerFieldNames ()... ),
397+ if nested , ok := historyWrappedPayloadJSON (fields , historyEntryWrapperFieldNames (), historyEntryValueFieldNames (), historyPastedContentContainerFieldNames ()); ok {
398+ var entry HistoryEntry
399+ if err := json .Unmarshal (nested , & entry ); err != nil {
400+ return err
401+ }
402+ historyApplyEntryFields (& entry , fields , false )
403+ * e = entry
404+ return nil
344405 }
406+ var entry HistoryEntry
407+ historyApplyEntryFields (& entry , fields , true )
408+ * e = entry
345409 return nil
346410}
347411
@@ -350,24 +414,120 @@ func (e *LogEntry) UnmarshalJSON(data []byte) error {
350414 if err := json .Unmarshal (data , & fields ); err != nil {
351415 return err
352416 }
353- * e = LogEntry {
354- Display : historyStringJSONField (fields , "display" , "prompt" , "text" , "input" , "content" , "value" ),
355- PastedContents : historyStoredPastedContentsJSONField (fields , historyPastedContentContainerFieldNames ()... ),
356- Timestamp : historyTimestampJSONField (fields , "timestamp" , "createdAt" , "created_at" , "time" , "unixTimestamp" , "unix_timestamp" ),
357- Project : historyStringJSONField (fields , "project" , "projectPath" , "project_path" , "cwd" , "cwdPath" , "cwd_path" , "workingDirectory" , "working_directory" , "workspacePath" , "workspace_path" , "workspace" ),
358- SessionID : firstHistoryID (
359- historyIDJSONField (fields , "sessionId" ),
360- historyIDJSONField (fields , "sessionID" ),
361- historyIDJSONField (fields , "session_id" ),
362- historyIDJSONField (fields , "session" ),
363- historyIDJSONField (fields , "sessionUuid" ),
364- historyIDJSONField (fields , "sessionUUID" ),
365- historyIDJSONField (fields , "session_uuid" ),
366- ),
417+ if nested , ok := historyWrappedPayloadJSON (fields , historyEntryWrapperFieldNames (), historyEntryValueFieldNames (), historyPastedContentContainerFieldNames ()); ok {
418+ var entry LogEntry
419+ if err := json .Unmarshal (nested , & entry ); err != nil {
420+ return err
421+ }
422+ historyApplyLogEntryFields (& entry , fields , false )
423+ * e = entry
424+ return nil
367425 }
426+ var entry LogEntry
427+ historyApplyLogEntryFields (& entry , fields , true )
428+ * e = entry
368429 return nil
369430}
370431
432+ func historyApplyEntryFields (entry * HistoryEntry , fields map [string ]json.RawMessage , overwrite bool ) {
433+ if value := historyStringJSONField (fields , "display" , "prompt" , "text" , "input" , "content" , "value" ); value != "" && (overwrite || entry .Display == "" ) {
434+ entry .Display = value
435+ }
436+ if value := historyPastedContentsJSONField (fields , historyPastedContentContainerFieldNames ()... ); value != nil && (overwrite || len (entry .PastedContents ) == 0 ) {
437+ entry .PastedContents = value
438+ }
439+ }
440+
441+ func historyApplyLogEntryFields (entry * LogEntry , fields map [string ]json.RawMessage , overwrite bool ) {
442+ if value := historyStringJSONField (fields , "display" , "prompt" , "text" , "input" , "content" , "value" ); value != "" && (overwrite || entry .Display == "" ) {
443+ entry .Display = value
444+ }
445+ if value := historyStoredPastedContentsJSONField (fields , historyPastedContentContainerFieldNames ()... ); value != nil && (overwrite || len (entry .PastedContents ) == 0 ) {
446+ entry .PastedContents = value
447+ }
448+ if value := historyTimestampJSONField (fields , "timestamp" , "createdAt" , "created_at" , "time" , "unixTimestamp" , "unix_timestamp" ); value != 0 && (overwrite || entry .Timestamp == 0 ) {
449+ entry .Timestamp = value
450+ }
451+ if value := historyStringJSONField (fields , "project" , "projectPath" , "project_path" , "cwd" , "cwdPath" , "cwd_path" , "workingDirectory" , "working_directory" , "workspacePath" , "workspace_path" , "workspace" ); value != "" && (overwrite || entry .Project == "" ) {
452+ entry .Project = value
453+ }
454+ if value := firstHistoryID (
455+ historyIDJSONField (fields , "sessionId" ),
456+ historyIDJSONField (fields , "sessionID" ),
457+ historyIDJSONField (fields , "session_id" ),
458+ historyIDJSONField (fields , "session" ),
459+ historyIDJSONField (fields , "sessionUuid" ),
460+ historyIDJSONField (fields , "sessionUUID" ),
461+ historyIDJSONField (fields , "session_uuid" ),
462+ ); value != "" && (overwrite || entry .SessionID == "" ) {
463+ entry .SessionID = value
464+ }
465+ }
466+
467+ func historyEntryWrapperFieldNames () []string {
468+ return []string {"entry" , "record" , "item" , "payload" , "body" , "data" , "result" , "value" , "historyEntry" , "history_entry" , "logEntry" , "log_entry" }
469+ }
470+
471+ func historyEntryValueFieldNames () []string {
472+ return []string {"display" , "prompt" , "text" , "input" , "content" , "value" }
473+ }
474+
475+ func historyPastedContentWrapperFieldNames () []string {
476+ return []string {"pastedContent" , "pasted_content" , "storedPastedContent" , "stored_pasted_content" , "attachment" , "paste" , "entry" , "record" , "item" , "payload" , "body" , "data" , "result" , "value" }
477+ }
478+
479+ func historyPastedContentValueFieldNames () []string {
480+ return []string {"content" , "value" , "data" , "base64" }
481+ }
482+
483+ func historyWrappedPayloadJSON (fields map [string ]json.RawMessage , wrappers []string , scalarDirect []string , containerDirect []string ) (json.RawMessage , bool ) {
484+ if historyHasScalarPayload (fields , scalarDirect ) || historyHasContainerPayload (fields , containerDirect ) {
485+ return nil , false
486+ }
487+ for _ , name := range wrappers {
488+ raw , ok := fields [name ]
489+ if ! ok {
490+ continue
491+ }
492+ trimmed := bytes .TrimSpace (raw )
493+ if len (trimmed ) > 0 && trimmed [0 ] == '{' {
494+ return raw , true
495+ }
496+ }
497+ return nil , false
498+ }
499+
500+ func historyHasScalarPayload (fields map [string ]json.RawMessage , names []string ) bool {
501+ for _ , name := range names {
502+ raw , ok := fields [name ]
503+ if ! ok {
504+ continue
505+ }
506+ trimmed := bytes .TrimSpace (raw )
507+ if len (trimmed ) == 0 || bytes .Equal (trimmed , []byte ("null" )) {
508+ continue
509+ }
510+ if trimmed [0 ] != '{' {
511+ return true
512+ }
513+ }
514+ return false
515+ }
516+
517+ func historyHasContainerPayload (fields map [string ]json.RawMessage , names []string ) bool {
518+ for _ , name := range names {
519+ raw , ok := fields [name ]
520+ if ! ok {
521+ continue
522+ }
523+ trimmed := bytes .TrimSpace (raw )
524+ if len (trimmed ) > 0 && ! bytes .Equal (trimmed , []byte ("null" )) {
525+ return true
526+ }
527+ }
528+ return false
529+ }
530+
371531func firstHistoryID (values ... contracts.ID ) contracts.ID {
372532 for _ , value := range values {
373533 if value != "" {
0 commit comments