Skip to content

Commit

Permalink
Use JSON library to handle marshalling consistent with REST APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 26, 2024
1 parent f26d938 commit ff8b68c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/php/com/openai/tools/Calls.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Throwable as Any;
use lang\reflection\TargetException;
use lang\{Type, Throwable, IllegalArgumentException};
use text\json\Json;
use util\data\Marshalling;

/**
Expand Down Expand Up @@ -109,9 +110,8 @@ public function call($name, $arguments, $context= []) {
try {
list($instance, $method)= $this->functions->target($name);

$named= json_decode($arguments, null, 512, JSON_OBJECT_AS_ARRAY | JSON_THROW_ON_ERROR);
$pass= [];
foreach ($this->pass($method, $named, $context) as $type => $value) {
foreach ($this->pass($method, Json::read($arguments), $context) as $type => $value) {
$pass[]= $this->marshalling->unmarshal($value, $type);
}

Expand All @@ -122,6 +122,6 @@ public function call($name, $arguments, $context= []) {
$result= $this->error(Throwable::wrap($e));
}

return json_encode($result);
return Json::of($result);
}
}
2 changes: 1 addition & 1 deletion src/test/php/com/openai/unittest/CallsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function call_successfully() {
#[Test]
public function call_invalid_json() {
Assert::equals(
'{"error":"lang.Error","message":"Control character error, possibly incorrectly encoded"}',
'{"error":"lang.FormatException","message":"Unclosed string "}',
(new Calls($this->functions))->call('testing_greet', '{"unclosed')
);
}
Expand Down

0 comments on commit ff8b68c

Please sign in to comment.