Skip to content

Commit 3173b18

Browse files
committed
Init project
0 parents  commit 3173b18

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
/vendor
3+
/.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) llm-agents-php <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# J

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "llm-agents/json-schema-mapper",
3+
"description": "JSON Schema to PHP Class Mapper for LLM Agents",
4+
"license": "MIT",
5+
"require": {
6+
"php": "^8.3",
7+
"spiral/json-schema-generator": "^1.0",
8+
"llm-agents/agents": "^1.0",
9+
"spiral/boot": "^3.13"
10+
},
11+
"require-dev": {
12+
"phpunit/phpunit": "^11.3"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"LLM\\Agents\\JsonSchema\\Mapper\\": "src"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"LLM\\Tests\\": "tests/src"
22+
}
23+
},
24+
"config": {
25+
"sort-packages": true
26+
},
27+
"minimum-stability": "dev",
28+
"prefer-stable": true
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\JsonSchema\Mapper\Bootloader;
6+
7+
use CuyZ\Valinor\Cache\FileSystemCache;
8+
use CuyZ\Valinor\Mapper\TreeMapper;
9+
use LLM\Agents\JsonSchema\Mapper\MapperBuilder;
10+
use LLM\Agents\JsonSchema\Mapper\SchemaMapper;
11+
use LLM\Agents\Tool\SchemaMapperInterface;
12+
use Spiral\Boot\Bootloader\Bootloader;
13+
use Spiral\Boot\DirectoriesInterface;
14+
use Spiral\Boot\Environment\AppEnvironment;
15+
16+
final class SchemaMapperBootloader extends Bootloader
17+
{
18+
public function defineSingletons(): array
19+
{
20+
return [
21+
SchemaMapperInterface::class => SchemaMapper::class,
22+
23+
TreeMapper::class => static fn(
24+
MapperBuilder $builder,
25+
) => $builder->build(),
26+
MapperBuilder::class => static fn(
27+
DirectoriesInterface $dirs,
28+
AppEnvironment $env,
29+
) => new MapperBuilder(
30+
cache: match ($env) {
31+
AppEnvironment::Production => new FileSystemCache(
32+
cacheDir: $dirs->get('runtime') . 'cache/valinor',
33+
),
34+
default => null,
35+
},
36+
),
37+
];
38+
}
39+
}

src/MapperBuilder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\JsonSchema\Mapper;
6+
7+
use CuyZ\Valinor\Mapper\TreeMapper;
8+
use CuyZ\Valinor\MapperBuilder as BaseMapperBuilder;
9+
use Psr\SimpleCache\CacheInterface;
10+
11+
final readonly class MapperBuilder
12+
{
13+
public function __construct(
14+
private ?CacheInterface $cache = null,
15+
) {}
16+
17+
public function build(): TreeMapper
18+
{
19+
$builder = (new BaseMapperBuilder())
20+
->enableFlexibleCasting()
21+
->allowPermissiveTypes();
22+
23+
if ($this->cache) {
24+
$builder = $builder->withCache($this->cache);
25+
}
26+
27+
return $builder->mapper();
28+
}
29+
}

src/SchemaMapper.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\JsonSchema\Mapper;
6+
7+
use CuyZ\Valinor\Mapper\TreeMapper;
8+
use LLM\Agents\Tool\SchemaMapperInterface;
9+
use Spiral\JsonSchemaGenerator\Generator as JsonSchemaGenerator;
10+
11+
final readonly class SchemaMapper implements SchemaMapperInterface
12+
{
13+
public function __construct(
14+
private JsonSchemaGenerator $generator,
15+
private TreeMapper $mapper,
16+
) {}
17+
18+
public function toJsonSchema(string $class): array
19+
{
20+
if (\json_validate($class)) {
21+
return \json_decode($class, associative: true);
22+
}
23+
24+
if (\class_exists($class)) {
25+
return $this->generator->generate($class)->jsonSerialize();
26+
}
27+
28+
throw new \InvalidArgumentException(\sprintf('Invalid class or JSON schema provided: %s', $class));
29+
}
30+
31+
/**
32+
* @template T of object
33+
* @param class-string<T>|string $class
34+
* @return T
35+
*/
36+
public function toObject(string $json, ?string $class = null): object
37+
{
38+
if ($class === null) {
39+
return \json_decode($json, associative: false);
40+
}
41+
42+
return $this->mapper->map($class, \json_decode($json, associative: true));
43+
}
44+
}

0 commit comments

Comments
 (0)