Skip to content

Commit aa0fc00

Browse files
committed
wip
1 parent 27747d2 commit aa0fc00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+558
-278
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^8.3",
2020
"adhocore/json-fixer": "^1.0",
21-
"cortexphp/json-schema": "^0.6",
21+
"cortexphp/json-schema": "dev-main",
2222
"cortexphp/model-info": "^0.3",
2323
"illuminate/collections": "^12.0",
2424
"mozex/anthropic-php": "^1.1",

config/cortex.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249

250250
'cache' => [
251251
'enabled' => env('CORTEX_PROMPT_CACHE_ENABLED', false),
252-
'store' => env('CORTEX_PROMPT_CACHE_STORE', null),
252+
'store' => env('CORTEX_PROMPT_CACHE_STORE'),
253253
'ttl' => env('CORTEX_PROMPT_CACHE_TTL', 3600),
254254
],
255255
],
@@ -365,15 +365,15 @@
365365
* Defaults to app_path('Agents').
366366
* Agents will be discovered from this directory and its subdirectories.
367367
*/
368-
'path' => env('CORTEX_AGENTS_PATH', null),
368+
'path' => env('CORTEX_AGENTS_PATH'),
369369
],
370370

371371
/*
372372
* Configure the cache settings.
373373
*/
374374
'cache' => [
375375
'enabled' => env('CORTEX_CACHE_ENABLED', false),
376-
'store' => env('CORTEX_CACHE_STORE', null),
376+
'store' => env('CORTEX_CACHE_STORE'),
377377
'ttl' => env('CORTEX_CACHE_TTL', 3600),
378378
],
379379
];

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
typeDeclarations: true,
2727
instanceOf: true,
2828
earlyReturn: true,
29-
strictBooleans: true,
3029
)
3130
->withFluentCallNewLine()
3231
->withSkip([

scratchpad.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Cortex\Cortex;
66
use Cortex\Agents\Agent;
77
use Cortex\Prompts\Prompt;
8-
use Cortex\JsonSchema\SchemaFactory;
8+
use Cortex\JsonSchema\Schema;
99
use Cortex\Tools\Prebuilt\WeatherTool;
1010
use Cortex\Agents\Prebuilt\WeatherAgent;
1111
use Cortex\LLM\Data\Messages\UserMessage;
@@ -25,8 +25,8 @@
2525
->metadata(
2626
provider: 'anthropic',
2727
model: 'claude-3-5-sonnet-20240620',
28-
structuredOutput: SchemaFactory::object()->properties(
29-
SchemaFactory::string('capital'),
28+
structuredOutput: Schema::object()->properties(
29+
Schema::string('capital'),
3030
),
3131
)
3232
->llm()
@@ -67,8 +67,8 @@
6767
new UserMessage('What is the capital of France?'),
6868
]);
6969

70-
$schema = SchemaFactory::object()->properties(
71-
SchemaFactory::string('capital'),
70+
$schema = Schema::object()->properties(
71+
Schema::string('capital'),
7272
);
7373

7474
$result = Cortex::llm()->withStructuredOutput($schema)->invoke([
@@ -111,9 +111,9 @@
111111
->withTools([
112112
WeatherTool::class,
113113
])
114-
->withOutput(SchemaFactory::object()->properties(
115-
SchemaFactory::string('location')->required(),
116-
SchemaFactory::string('summary')->required(),
114+
->withOutput(Schema::object()->properties(
115+
Schema::string('location')->required(),
116+
Schema::string('summary')->required(),
117117
))
118118
->withMaxSteps(3)
119119
->withStrict(true);

src/Agents/Agent.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Cortex\Prompts\Prompt;
1212
use Illuminate\Support\Str;
1313
use Cortex\Contracts\ToolKit;
14+
use Cortex\JsonSchema\Schema;
1415
use Cortex\Memory\ChatMemory;
1516
use UnexpectedValueException;
1617
use Cortex\Contracts\Pipeable;
@@ -19,15 +20,15 @@
1920
use Cortex\LLM\Contracts\Message;
2021
use Cortex\Memory\Contracts\Store;
2122
use Cortex\Support\Traits\CanPipe;
22-
use Cortex\JsonSchema\SchemaFactory;
23+
use Cortex\Pipeline\RuntimeContext;
2324
use Cortex\Agents\Stages\AppendUsage;
2425
use Cortex\LLM\Data\ChatStreamResult;
2526
use Cortex\Exceptions\GenericException;
26-
use Cortex\JsonSchema\Contracts\Schema;
2727
use Cortex\Memory\Stores\InMemoryStore;
2828
use Cortex\Agents\Stages\HandleToolCalls;
2929
use Cortex\JsonSchema\Types\ObjectSchema;
3030
use Cortex\LLM\Enums\StructuredOutputMode;
31+
use Cortex\JsonSchema\Contracts\JsonSchema;
3132
use Cortex\LLM\Data\Messages\SystemMessage;
3233
use Illuminate\Contracts\Support\Arrayable;
3334
use Cortex\Agents\Stages\AddMessageToMemory;
@@ -59,7 +60,7 @@ class Agent implements Pipeable
5960
/**
6061
* @param class-string|\Cortex\JsonSchema\Types\ObjectSchema $output
6162
* @param array<int, \Cortex\LLM\Contracts\Tool|\Closure|string>|\Cortex\Contracts\ToolKit $tools
62-
* @param class-string|class-string<\BackedEnum>|\Cortex\JsonSchema\Types\ObjectSchema|array<array-key, \Cortex\JsonSchema\Contracts\Schema>|null $output
63+
* @param class-string|class-string<\BackedEnum>|\Cortex\JsonSchema\Types\ObjectSchema|array<array-key, \Cortex\JsonSchema\Contracts\JsonSchema>|null $llm
6364
* @param array<string, mixed> $initialPromptVariables
6465
*/
6566
public function __construct(
@@ -176,7 +177,7 @@ public function stream(array $messages = [], array $input = []): ChatStreamResul
176177
return $result;
177178
}
178179

179-
public function handlePipeable(mixed $payload, Closure $next): mixed
180+
public function handlePipeable(mixed $payload, RuntimeContext $context, Closure $next): mixed
180181
{
181182
$messages = match (true) {
182183
$payload instanceof MessageCollection => $payload->all(),
@@ -192,7 +193,7 @@ public function handlePipeable(mixed $payload, Closure $next): mixed
192193
default => [],
193194
};
194195

195-
return $next($this->invoke($messages, $input));
196+
return $next($this->invoke($messages, $input), $context);
196197
}
197198

198199
public function getName(): string
@@ -319,20 +320,20 @@ protected static function buildLLM(
319320
/**
320321
* Build the output schema for the agent.
321322
*
322-
* @param class-string|class-string<\BackedEnum>|\Cortex\JsonSchema\Types\ObjectSchema|array<array-key, \Cortex\JsonSchema\Contracts\Schema>|null $output
323+
* @param class-string|class-string<\BackedEnum>|\Cortex\JsonSchema\Types\ObjectSchema|array<array-key, \Cortex\JsonSchema\Contracts\JsonSchema>|null $output
323324
*
324325
* @throws \Cortex\Exceptions\GenericException
325326
*/
326327
protected static function buildOutput(ObjectSchema|array|string|null $output): ObjectSchema|string|null
327328
{
328329
if (is_array($output)) {
329330
try {
330-
collect($output)->ensure(Schema::class);
331+
collect($output)->ensure(JsonSchema::class);
331332
} catch (UnexpectedValueException $e) {
332333
throw new GenericException('Invalid output schema: ' . $e->getMessage(), previous: $e);
333334
}
334335

335-
return SchemaFactory::object()->properties(...$output);
336+
return Schema::object()->properties(...$output);
336337
}
337338

338339
return $output;

src/Agents/Prebuilt/WeatherAgent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Override;
88
use Cortex\Cortex;
99
use Cortex\Contracts\ToolKit;
10+
use Cortex\JsonSchema\Schema;
1011
use Cortex\LLM\Contracts\LLM;
11-
use Cortex\JsonSchema\SchemaFactory;
1212
use Cortex\Tools\Prebuilt\WeatherTool;
1313
use Cortex\Agents\AbstractAgentBuilder;
1414
use Cortex\JsonSchema\Types\ObjectSchema;
@@ -47,9 +47,9 @@ public function tools(): array|ToolKit
4747

4848
public function output(): ObjectSchema|string|null
4949
{
50-
return SchemaFactory::object()->properties(
51-
SchemaFactory::string('location')->required(),
52-
SchemaFactory::string('summary')->required(),
50+
return Schema::object()->properties(
51+
Schema::string('location')->required(),
52+
Schema::string('summary')->required(),
5353
);
5454
}
5555
}

src/Agents/Stages/AddMessageToMemory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cortex\Contracts\ChatMemory;
1111
use Cortex\Support\Traits\CanPipe;
1212
use Cortex\LLM\Data\ChatGeneration;
13+
use Cortex\Pipeline\RuntimeContext;
1314
use Cortex\LLM\Data\ChatGenerationChunk;
1415

1516
class AddMessageToMemory implements Pipeable
@@ -20,7 +21,7 @@ public function __construct(
2021
protected ChatMemory $memory,
2122
) {}
2223

23-
public function handlePipeable(mixed $payload, Closure $next): mixed
24+
public function handlePipeable(mixed $payload, RuntimeContext $context, Closure $next): mixed
2425
{
2526
$message = match (true) {
2627
$payload instanceof ChatGeneration => $payload->message,
@@ -33,6 +34,6 @@ public function handlePipeable(mixed $payload, Closure $next): mixed
3334
$this->memory->addMessage($message);
3435
}
3536

36-
return $next($payload);
37+
return $next($payload, $context);
3738
}
3839
}

src/Agents/Stages/AppendUsage.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cortex\Contracts\Pipeable;
1010
use Cortex\LLM\Data\ChatResult;
1111
use Cortex\Support\Traits\CanPipe;
12+
use Cortex\Pipeline\RuntimeContext;
1213
use Cortex\LLM\Data\ChatGenerationChunk;
1314

1415
class AppendUsage implements Pipeable
@@ -19,7 +20,7 @@ public function __construct(
1920
protected Usage $usage,
2021
) {}
2122

22-
public function handlePipeable(mixed $payload, Closure $next): mixed
23+
public function handlePipeable(mixed $payload, RuntimeContext $context, Closure $next): mixed
2324
{
2425
$usage = match (true) {
2526
$payload instanceof ChatResult, $payload instanceof ChatGenerationChunk && $payload->isFinal => $payload->usage,
@@ -30,6 +31,6 @@ public function handlePipeable(mixed $payload, Closure $next): mixed
3031
$this->usage->add($usage);
3132
}
3233

33-
return $next($payload);
34+
return $next($payload, $context);
3435
}
3536
}

src/Agents/Stages/HandleToolCalls.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cortex\Support\Traits\CanPipe;
1313
use Illuminate\Support\Collection;
1414
use Cortex\LLM\Data\ChatGeneration;
15+
use Cortex\Pipeline\RuntimeContext;
1516
use Cortex\LLM\Data\ChatGenerationChunk;
1617
use Cortex\LLM\Data\Messages\ToolMessage;
1718

@@ -31,7 +32,7 @@ public function __construct(
3132
protected int $maxSteps,
3233
) {}
3334

34-
public function handlePipeable(mixed $payload, Closure $next): mixed
35+
public function handlePipeable(mixed $payload, RuntimeContext $context, Closure $next): mixed
3536
{
3637
$generation = $this->getGeneration($payload);
3738

@@ -49,14 +50,14 @@ public function handlePipeable(mixed $payload, Closure $next): mixed
4950
$payload = $this->executionPipeline->invoke([
5051
'messages' => $this->memory->getMessages(),
5152
...$this->memory->getVariables(),
52-
]);
53+
], $context);
5354

5455
// Update the generation so that the loop can check the new generation for tool calls.
5556
$generation = $this->getGeneration($payload);
5657
}
5758
}
5859

59-
return $next($payload);
60+
return $next($payload, $context);
6061
}
6162

6263
/**

src/Contracts/Pipeable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
use Closure;
88
use Cortex\Pipeline;
9+
use Cortex\Pipeline\RuntimeContext;
910

1011
interface Pipeable
1112
{
1213
/**
1314
* Handle the pipeline processing.
1415
*
1516
* @param mixed $payload The input to process
17+
* @param RuntimeContext $context The runtime context containing settings
1618
* @param Closure $next The next stage in the pipeline
1719
*
1820
* @return mixed The processed result
1921
*/
20-
public function handlePipeable(mixed $payload, Closure $next): mixed;
22+
public function handlePipeable(mixed $payload, RuntimeContext $context, Closure $next): mixed;
2123

2224
/**
2325
* Pipe the pipeable into another pipeable.

0 commit comments

Comments
 (0)