Skip to content

Commit 17978de

Browse files
committed
Add a could-not-parse-json command error instead of raising
1 parent f6eced7 commit 17978de

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.7] - 2025-05-30
2+
3+
- Add a could-not-parse-json command error instead of raising
4+
15
## [0.0.6] - 2025-05-21
26

37
- Give an error if no ai api services have been provided

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-llm-backed-command (0.0.6)
4+
foobara-llm-backed-command (0.0.7)
55
foobara (~> 0.0.92)
66
foobara-ai (~> 0.0.1)
77
foobara-json-schema-generator (~> 0.0.1)
@@ -12,7 +12,7 @@ GEM
1212
addressable (2.8.7)
1313
public_suffix (>= 2.0.2, < 7.0)
1414
ast (2.4.3)
15-
base64 (0.2.0)
15+
base64 (0.3.0)
1616
bigdecimal (3.2.0)
1717
byebug (12.0.0)
1818
coderay (1.1.3)
@@ -58,7 +58,7 @@ GEM
5858
foobara-type-generator (~> 0.0.1)
5959
foobara-typescript-react-command-form-generator (~> 0.0.1)
6060
foobara-typescript-remote-command-generator (~> 0.0.1)
61-
foobara (0.0.127)
61+
foobara (0.0.129)
6262
bigdecimal
6363
foobara-lru-cache (~> 0.0.2)
6464
foobara-util (~> 0.0.11)
@@ -196,7 +196,7 @@ GEM
196196
public_suffix (6.0.2)
197197
racc (1.8.1)
198198
rainbow (3.1.1)
199-
rake (13.2.1)
199+
rake (13.3.0)
200200
rb-fsevent (0.11.2)
201201
rb-inotify (0.11.1)
202202
ffi (~> 1.0)

src/llm_backed_execute_method.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ module LlmBackedExecuteMethod
1111

1212
on_include do
1313
depends_on Ai::AnswerBot::Ask
14+
possible_error :could_not_parse_result_json,
15+
message: "Could not parse answer",
16+
context: {
17+
raw_answer: :string,
18+
stripped_answer: :string
19+
}
1420
end
1521

1622
def execute
@@ -77,24 +83,32 @@ def llm_instructions
7783
def parse_answer
7884
stripped_answer = answer.gsub(/<THINK>.*?<\/THINK>/mi, "")
7985
fencepostless_answer = stripped_answer.gsub(/^\s*```\w*\n(.*)```\s*\z/m, "\\1")
86+
8087
# TODO: should we verify against json-schema or no?
8188
self.parsed_answer = begin
8289
JSON.parse(fencepostless_answer)
83-
rescue => e
90+
rescue JSON::ParserError
8491
# see if we can extract the last fence-posts content just in case
8592
last_fence_post_regex = /```\w*\s*\n((?:(?!```).)+)\n```(?:(?!```).)*\z/m
93+
8694
begin
8795
match = last_fence_post_regex.match(stripped_answer)
96+
8897
if match
89-
JSON.parse(match[1])
98+
fencepostless_answer = match[1]
99+
JSON.parse(fencepostless_answer)
90100
else
91101
# :nocov:
92-
raise e
102+
raise
93103
# :nocov:
94104
end
95-
rescue
105+
rescue JSON::ParserError => e
106+
# TODO: figure out how to test this code path
96107
# :nocov:
97-
raise e
108+
add_runtime_error :could_not_parse_result_json,
109+
"Could not parse result JSON: #{e.message}",
110+
raw_answer: answer,
111+
stripped_answer: fencepostless_answer
98112
# :nocov:
99113
end
100114
end

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Foobara
22
module LlmBackedCommandVersion
3-
VERSION = "0.0.6".freeze
3+
VERSION = "0.0.7".freeze
44

55
local_ruby_version = File.read("#{__dir__}/.ruby-version").chomp
66
local_ruby_version_minor = local_ruby_version[/\A(\d+\.\d+)\.\d+\z/, 1]

0 commit comments

Comments
 (0)