-
Notifications
You must be signed in to change notification settings - Fork 0
Convert json.RawMessage to JSON strings instead of byte arrays (#41) #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moshloop
wants to merge
34
commits into
main
Choose a base branch
from
pr/fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
32fb1d3
feat: proper rendering of api.Text and Textable in all formatters
moshloop 4687661
feat: refactor formatters to use FieldValue.Text and PrettyData.Pretty()
moshloop a8be24d
chore: misc
moshloop c777998
chore: update examples
moshloop eb6a679
fix: restore table rendering temporarily, fix ANSI test checks
moshloop 4d8ab4c
fix: wrap string values in Text for TextList compatibility
moshloop 24a2c20
fix: add missing types and methods for compilation
moshloop e60bcff
refactor: remove Formatted(), Plain(), Markdown(), HTML() from FieldV…
moshloop be7a811
fix: update test to handle Textable interface properly
moshloop ab7ea01
wip
moshloop 74aeefc
misc
moshloop d6800db
fix: improve HTML tree rendering with embedded assets
moshloop 7cba29c
refactor: extract inline CSS/JS to go:embed files and use Iconify che…
moshloop 5e1402b
fix: align tree chevrons to top
moshloop b6123b8
fix: use interface assertion in NewTypedValue for nested trees
moshloop be5dc1a
feat: implement nested table rendering in HTML with compact support
moshloop b6ab2be
feat(ai): add LLM agent adapter with structured output support
moshloop 222dab6
fix(task): prevent hanging and terminal corruption in task manager
moshloop 44c4a63
fix(task): resolve race conditions in batch.go
moshloop a77b8aa
fix(task): resolve data races in Task and Manager
moshloop 90b7b9c
fix: formatting improvements
moshloop d9d468c
fix: args handling in clicky.AddCommand
moshloop 80289dd
chore: ai refactorings
moshloop 3868624
fix: batch race conditions
moshloop 684686d
fix: parse max-w unit suffixes (ch, px, rem, em)
moshloop 5471a3c
feat: default to truncate-suffix when constraints specified
moshloop a4ee1da
fix: prevent truncate class from overwriting explicit max-w
moshloop b6e0935
feat: migrate terminal tables to lipgloss (hybrid approach)
moshloop 5571de3
feat: migrate tree rendering to lipgloss
moshloop 7a85328
chore: switch tree/table printing back to lipgloss
moshloop 937606c
chore: fix nested html table output
moshloop a1ac86c
chore: fix nested html tree output
moshloop 3c8c682
chore: html fixes
moshloop c680026
chore: review fixes
moshloop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,3 +71,6 @@ clicky | |
| formatters/.playwright-mcp/ | ||
| __debug* | ||
|
|
||
| ginkgo.report | ||
| *.bak | ||
| examples/uber_demo/uber_demo | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| // Match markdown code blocks with optional language specifier | ||
| codeBlockRegex = regexp.MustCompile("(?s)```(?:json)?\\s*(.+?)```") | ||
|
|
||
| // Match JSON objects or arrays | ||
| jsonObjectRegex = regexp.MustCompile(`(?s)\{.*\}`) | ||
| jsonArrayRegex = regexp.MustCompile(`(?s)\[.*\]`) | ||
| ) | ||
|
|
||
| // CleanupJSONResponse attempts to extract and clean JSON from LLM responses | ||
| // that may contain markdown formatting, explanatory text, or other noise. | ||
| // | ||
| // It tries the following strategies in order: | ||
| // 1. Extract JSON from markdown code blocks (```json or ```) | ||
| // 2. Extract the first JSON object {...} | ||
| // 3. Extract the first JSON array [...] | ||
| // 4. Return the trimmed original string | ||
| // | ||
| // After extraction, it validates that the result is valid JSON. | ||
| func CleanupJSONResponse(response string) string { | ||
| // Trim whitespace | ||
| response = strings.TrimSpace(response) | ||
|
|
||
| // If it's already valid JSON, return as-is | ||
| if isValidJSON(response) { | ||
| return response | ||
| } | ||
|
|
||
| // Strategy 1: Extract from markdown code blocks | ||
| if matches := codeBlockRegex.FindStringSubmatch(response); len(matches) > 1 { | ||
| extracted := strings.TrimSpace(matches[1]) | ||
| if isValidJSON(extracted) { | ||
| return extracted | ||
| } | ||
| } | ||
|
|
||
| // Strategy 2: Extract first JSON object | ||
| if match := jsonObjectRegex.FindString(response); match != "" { | ||
| if isValidJSON(match) { | ||
| return match | ||
| } | ||
| } | ||
|
|
||
| // Strategy 3: Extract first JSON array | ||
| if match := jsonArrayRegex.FindString(response); match != "" { | ||
| if isValidJSON(match) { | ||
| return match | ||
| } | ||
| } | ||
|
|
||
| // Strategy 4: Return original trimmed | ||
| return response | ||
| } | ||
|
|
||
| // isValidJSON checks if a string is valid JSON | ||
| func isValidJSON(s string) bool { | ||
| var js interface{} | ||
| return json.Unmarshal([]byte(s), &js) == nil | ||
| } | ||
|
|
||
| // UnmarshalWithCleanup attempts to unmarshal JSON with automatic cleanup | ||
| func UnmarshalWithCleanup(data string, v interface{}) error { | ||
| // First try direct unmarshal | ||
| if err := json.Unmarshal([]byte(data), v); err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // If that fails, try with cleanup | ||
| cleaned := CleanupJSONResponse(data) | ||
| return json.Unmarshal([]byte(cleaned), v) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Fix missing
fmtimport and consider explicit handling of structured data typesTwo points here:
Compile-time issue (must fix)
The error-path branches use
fmt.Sprintf:but
fmtis not imported in this file. Add it to the import block:Without this, the package won’t compile.
StructuredData conversion edge cases
The generic conversion logic:
works well when
StructuredDatais a*T, aT, or a JSON-serializable map/slice. IfStructuredDatacan ever be ajson.RawMessage,[]byte, or a raw JSON string,json.Marshalwill either base64-encode the bytes or double-encode the JSON string, which could cause unexpected unmarshal failures.If those types are possible here, consider special‑casing them, e.g.:
json.RawMessage/[]byte: use the bytes directly asjsonData.string: treat as raw JSON and[]byte(stringVal).Otherwise, this path is fine as-is.
🏁 Script executed:
Length of output: 124
🏁 Script executed:
Length of output: 2892
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 200
🏁 Script executed:
Length of output: 5395
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
Fix missing
fmtimportThe code uses
fmt.Sprintfat lines 192 and 196, butfmtis not in the import block. This will cause a compile error. Add it:StructuredData conversion edge cases (optional)
The conversion logic handles
*T,T, and JSON-serializable types viajson.Marshal/Unmarshal. While special-casing forjson.RawMessage,[]byte, or raw JSON strings would be defensive (to avoid base64-encoding or double-encoding), no evidence of these types currently being assigned toStructuredDatawas found in the codebase. If such types are introduced in future, consider explicit handling.🤖 Prompt for AI Agents