Skip to content

Commit e01502b

Browse files
committed
wip
1 parent 1689aab commit e01502b

File tree

11 files changed

+234
-87
lines changed

11 files changed

+234
-87
lines changed

resources/js/actions/Cortex/Http/Controllers/AgentsController.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { queryParams, type RouteQueryOptions, type RouteDefinition, applyUrlDefaults } from './../../../../wayfinder'
22
/**
33
* @see \Cortex\Http\Controllers\AgentsController::invoke
4-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
4+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
55
* @route '/api/agents/{agent}/invoke'
66
*/
77
export const invoke = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -16,7 +16,7 @@ invoke.definition = {
1616

1717
/**
1818
* @see \Cortex\Http\Controllers\AgentsController::invoke
19-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
19+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
2020
* @route '/api/agents/{agent}/invoke'
2121
*/
2222
invoke.url = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions) => {
@@ -49,7 +49,7 @@ invoke.url = (args: { agent: string | number | { string: string | number } } | [
4949

5050
/**
5151
* @see \Cortex\Http\Controllers\AgentsController::invoke
52-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
52+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
5353
* @route '/api/agents/{agent}/invoke'
5454
*/
5555
invoke.get = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -59,7 +59,7 @@ invoke.get = (args: { agent: string | number | { string: string | number } } | [
5959

6060
/**
6161
* @see \Cortex\Http\Controllers\AgentsController::invoke
62-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
62+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
6363
* @route '/api/agents/{agent}/invoke'
6464
*/
6565
invoke.post = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'post'> => ({
@@ -69,7 +69,7 @@ invoke.post = (args: { agent: string | number | { string: string | number } } |
6969

7070
/**
7171
* @see \Cortex\Http\Controllers\AgentsController::invoke
72-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
72+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
7373
* @route '/api/agents/{agent}/invoke'
7474
*/
7575
invoke.head = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'head'> => ({
@@ -79,7 +79,7 @@ invoke.head = (args: { agent: string | number | { string: string | number } } |
7979

8080
/**
8181
* @see \Cortex\Http\Controllers\AgentsController::stream
82-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
82+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
8383
* @route '/api/agents/{agent}/stream'
8484
*/
8585
export const stream = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -94,7 +94,7 @@ stream.definition = {
9494

9595
/**
9696
* @see \Cortex\Http\Controllers\AgentsController::stream
97-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
97+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
9898
* @route '/api/agents/{agent}/stream'
9999
*/
100100
stream.url = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions) => {
@@ -127,7 +127,7 @@ stream.url = (args: { agent: string | number | { string: string | number } } | [
127127

128128
/**
129129
* @see \Cortex\Http\Controllers\AgentsController::stream
130-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
130+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
131131
* @route '/api/agents/{agent}/stream'
132132
*/
133133
stream.get = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -137,7 +137,7 @@ stream.get = (args: { agent: string | number | { string: string | number } } | [
137137

138138
/**
139139
* @see \Cortex\Http\Controllers\AgentsController::stream
140-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
140+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
141141
* @route '/api/agents/{agent}/stream'
142142
*/
143143
stream.post = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'post'> => ({
@@ -147,7 +147,7 @@ stream.post = (args: { agent: string | number | { string: string | number } } |
147147

148148
/**
149149
* @see \Cortex\Http\Controllers\AgentsController::stream
150-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
150+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
151151
* @route '/api/agents/{agent}/stream'
152152
*/
153153
stream.head = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'head'> => ({

resources/js/components/app-logo-icon.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import { SVGAttributes } from "react";
22

33
export default function AppLogoIcon(props: SVGAttributes<SVGElement>) {
44
return (
5-
<svg {...props} viewBox="0 0 40 42" xmlns="http://www.w3.org/2000/svg">
6-
<path
7-
fillRule="evenodd"
8-
clipRule="evenodd"
9-
d="M17.2 5.63325L8.6 0.855469L0 5.63325V32.1434L16.2 41.1434L32.4 32.1434V23.699L40 19.4767V9.85547L31.4 5.07769L22.8 9.85547V18.2999L17.2 21.411V5.63325ZM38 18.2999L32.4 21.411V15.2545L38 12.1434V18.2999ZM36.9409 10.4439L31.4 13.5221L25.8591 10.4439L31.4 7.36561L36.9409 10.4439ZM24.8 18.2999V12.1434L30.4 15.2545V21.411L24.8 18.2999ZM23.8 20.0323L29.3409 23.1105L16.2 30.411L10.6591 27.3328L23.8 20.0323ZM7.6 27.9212L15.2 32.1434V38.2999L2 30.9666V7.92116L7.6 11.0323V27.9212ZM8.6 9.29991L3.05913 6.22165L8.6 3.14339L14.1409 6.22165L8.6 9.29991ZM30.4 24.8101L17.2 32.1434V38.2999L30.4 30.9666V24.8101ZM9.6 11.0323L15.2 7.92117V22.5221L9.6 25.6333V11.0323Z"
10-
/>
5+
<svg {...props} viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
6+
<path d="M13 4c-1.91 0-3.457 1.387-3.844 3.188C6.824 7.604 5 9.55 5 12c0 .246.027.465.063.688C4.418 13.636 4 14.753 4 16c0 1.797.895 3.309 2.156 4.406-.09.356-.156.711-.156 1.094 0 2.473 2.027 4.5 4.5 4.5h.063c.582 1.129 1.59 2 2.937 2a3.49 3.49 0 0 0 2.5-1.063A3.49 3.49 0 0 0 18.5 28c1.348 0 2.355-.871 2.938-2h.062c2.348 0 4.273-1.828 4.469-4.125H26c0-.063-.027-.125-.031-.188.004-.062.031-.125.031-.187 0-.371-.133-.688-.219-1.031C27.078 19.367 28 17.829 28 16c0-1.246-.418-2.363-1.063-3.313.036-.222.063-.44.063-.687 0-2.45-1.824-4.395-4.156-4.813C22.457 5.388 20.91 4 19 4c-1.2 0-2.266.559-3 1.406C15.266 4.56 14.2 4 13 4Zm0 2c1.117 0 2 .883 2 2v2c0 1.668-1.332 3-3 3v2c1.133 0 2.16-.39 3-1.031V24.5c0 .84-.66 1.5-1.5 1.5a1.47 1.47 0 0 1-1.469-1.219l-.187-.968-.969.156A2.487 2.487 0 0 1 8 21.5c0-.309.05-.598.156-.875.008-.023.024-.04.031-.063.31-.769.981-1.332 1.813-1.5l-.406-1.968A4.452 4.452 0 0 0 7 18.656 3.958 3.958 0 0 1 6 16a3.9 3.9 0 0 1 .875-2.469l.313-.375-.094-.5A2.858 2.858 0 0 1 7 12c0-1.668 1.332-3 3-3h1V8c0-1.117.883-2 2-2Zm6 0c1.117 0 2 .883 2 2v1h1c1.668 0 3 1.332 3 3 0 .215-.04.422-.094.656l-.093.5.312.375A3.9 3.9 0 0 1 26 16c0 1.2-.512 2.27-1.344 3-.683-.605-1.554-1-2.531-1-.945 0-1.797.336-2.469.906l1.282 1.532A1.806 1.806 0 0 1 22.125 20c.965 0 1.734.723 1.844 1.656C23.887 22.973 22.836 24 21.5 24c-.09 0-.2-.004-.375-.031l-.969-.157-.187.97A1.47 1.47 0 0 1 18.5 26c-.84 0-1.5-.66-1.5-1.5V13.969A4.915 4.915 0 0 0 20 15v-2c-1.668 0-3-1.332-3-3V8c0-1.117.883-2 2-2Z"/>
117
</svg>
128
);
139
}

resources/js/components/app-logo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default function AppLogo() {
44
return (
55
<>
66
<div className="flex aspect-square size-8 items-center justify-center rounded-md bg-sidebar-primary text-sidebar-primary-foreground">
7-
<AppLogoIcon className="size-5 fill-current text-white dark:text-black" />
7+
<AppLogoIcon className="size-7 fill-current text-white" />
88
</div>
99
<div className="ml-1 grid flex-1 text-left text-lg">
1010
<span className="mb-0.5 truncate leading-tight">

resources/js/routes/cortex/api/agents/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { queryParams, type RouteQueryOptions, type RouteDefinition, applyUrlDefaults } from './../../../../wayfinder'
22
/**
33
* @see \Cortex\Http\Controllers\AgentsController::invoke
4-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
4+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
55
* @route '/api/agents/{agent}/invoke'
66
*/
77
export const invoke = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -16,7 +16,7 @@ invoke.definition = {
1616

1717
/**
1818
* @see \Cortex\Http\Controllers\AgentsController::invoke
19-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
19+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
2020
* @route '/api/agents/{agent}/invoke'
2121
*/
2222
invoke.url = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions) => {
@@ -49,7 +49,7 @@ invoke.url = (args: { agent: string | number | { string: string | number } } | [
4949

5050
/**
5151
* @see \Cortex\Http\Controllers\AgentsController::invoke
52-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
52+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
5353
* @route '/api/agents/{agent}/invoke'
5454
*/
5555
invoke.get = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -59,7 +59,7 @@ invoke.get = (args: { agent: string | number | { string: string | number } } | [
5959

6060
/**
6161
* @see \Cortex\Http\Controllers\AgentsController::invoke
62-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
62+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
6363
* @route '/api/agents/{agent}/invoke'
6464
*/
6565
invoke.post = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'post'> => ({
@@ -69,7 +69,7 @@ invoke.post = (args: { agent: string | number | { string: string | number } } |
6969

7070
/**
7171
* @see \Cortex\Http\Controllers\AgentsController::invoke
72-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:22
72+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:27
7373
* @route '/api/agents/{agent}/invoke'
7474
*/
7575
invoke.head = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'head'> => ({
@@ -79,7 +79,7 @@ invoke.head = (args: { agent: string | number | { string: string | number } } |
7979

8080
/**
8181
* @see \Cortex\Http\Controllers\AgentsController::stream
82-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
82+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
8383
* @route '/api/agents/{agent}/stream'
8484
*/
8585
export const stream = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -94,7 +94,7 @@ stream.definition = {
9494

9595
/**
9696
* @see \Cortex\Http\Controllers\AgentsController::stream
97-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
97+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
9898
* @route '/api/agents/{agent}/stream'
9999
*/
100100
stream.url = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions) => {
@@ -127,7 +127,7 @@ stream.url = (args: { agent: string | number | { string: string | number } } | [
127127

128128
/**
129129
* @see \Cortex\Http\Controllers\AgentsController::stream
130-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
130+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
131131
* @route '/api/agents/{agent}/stream'
132132
*/
133133
stream.get = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
@@ -137,7 +137,7 @@ stream.get = (args: { agent: string | number | { string: string | number } } | [
137137

138138
/**
139139
* @see \Cortex\Http\Controllers\AgentsController::stream
140-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
140+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
141141
* @route '/api/agents/{agent}/stream'
142142
*/
143143
stream.post = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'post'> => ({
@@ -147,7 +147,7 @@ stream.post = (args: { agent: string | number | { string: string | number } } |
147147

148148
/**
149149
* @see \Cortex\Http\Controllers\AgentsController::stream
150-
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:62
150+
* @see Users/sean/Code/cortexphp/cortex/src/Http/Controllers/AgentsController.php:67
151151
* @route '/api/agents/{agent}/stream'
152152
*/
153153
stream.head = (args: { agent: string | number | { string: string | number } } | [agent: string | number | { string: string | number } ] | string | number | { string: string | number }, options?: RouteQueryOptions): RouteDefinition<'head'> => ({

src/Http/Controllers/AgentsController.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Cortex\Http\Controllers;
66

7-
use Exception;
87
use Throwable;
98
use Cortex\Cortex;
109
use Illuminate\Support\Arr;
1110
use Illuminate\Http\Request;
1211
use Cortex\Pipeline\Metadata;
1312
use Cortex\Tools\FrontendTool;
13+
use Cortex\LLM\Contracts\Content;
1414
use Illuminate\Http\JsonResponse;
1515
use Cortex\Pipeline\RuntimeConfig;
1616
use Illuminate\Routing\Controller;
@@ -65,32 +65,32 @@ public function invoke(string $agent, Request $request): JsonResponse
6565

6666
public function stream(string $agent, Request $request): StreamedResponse
6767
{
68-
$agent = Cortex::agent($agent);
68+
try {
69+
$agent = Cortex::agent($agent);
6970

70-
if ($request->isMethod('post')) {
71-
$messages = $this->getMessages($request);
72-
} else {
73-
$messages = $request->has('message') ? [
74-
new UserMessage($request->input('message')),
75-
] : [];
76-
}
71+
if ($request->isMethod('post')) {
72+
$messages = $this->getMessages($request);
73+
} else {
74+
$messages = $request->has('message') ? [
75+
new UserMessage($request->input('message')),
76+
] : [];
77+
}
7778

78-
/** @var array<string, array<string, mixed>> $toolsInput */
79-
$toolsInput = $request->input('tools', []);
80-
$tools = collect($toolsInput)
81-
->filter(function (array $tool): bool {
82-
return $tool !== [];
83-
})
84-
->map(function (array $tool, string $toolName): FrontendTool {
85-
return new FrontendTool(
86-
$toolName,
87-
$tool['description'] ?? null,
88-
$tool['parameters'] ?? [],
89-
);
90-
})
91-
->values();
79+
/** @var array<string, array<string, mixed>> $toolsInput */
80+
$toolsInput = $request->input('tools', []);
81+
$tools = collect($toolsInput)
82+
->filter(function (array $tool): bool {
83+
return $tool !== [];
84+
})
85+
->map(function (array $tool, string $toolName): FrontendTool {
86+
return new FrontendTool(
87+
$toolName,
88+
$tool['description'] ?? null,
89+
$tool['parameters'] ?? [],
90+
);
91+
})
92+
->values();
9293

93-
try {
9494
$result = $agent->stream(
9595
messages: $messages,
9696
input: $request->input('input', []),
@@ -105,7 +105,7 @@ public function stream(string $agent, Request $request): StreamedResponse
105105
$request->enum('protocol', StreamingProtocol::class, config('cortex.default_streaming_protocol')),
106106
);
107107
} catch (Throwable $e) {
108-
dd($e->getMessage());
108+
dd($e);
109109
}
110110

111111
// dd([
@@ -125,14 +125,18 @@ protected function getMessages(Request $request): array
125125
->map(function (array $message): UserMessage {
126126
/** @var array<int, array<string, mixed>> $parts */
127127
$parts = $message['parts'];
128-
$content = collect($parts)->map(function (array $part): TextContent|ImageContent|FileContent {
129-
return match (true) {
130-
$part['type'] === 'text' => new TextContent($part['text']),
131-
$part['type'] === 'file' && str_starts_with((string) $part['mediaType'], 'image/') => new ImageContent($part['url'], $part['mediaType'] ?? null),
132-
$part['type'] === 'file' => new FileContent($part['url'], $part['mediaType'] ?? null, $part['filename'] ?? null),
133-
default => throw new Exception('Invalid message type: ' . $part['type']),
134-
};
135-
})->all();
128+
$content = collect($parts)
129+
->map(function (array $part): ?Content {
130+
return match (true) {
131+
$part['type'] === 'text' => new TextContent($part['text']),
132+
$part['type'] === 'file' && str_starts_with((string) $part['mediaType'], 'image/') => new ImageContent($part['url'], $part['mediaType'] ?? null),
133+
$part['type'] === 'file' => new FileContent($part['url'], $part['mediaType'] ?? null, $part['filename'] ?? null),
134+
default => null,
135+
};
136+
})
137+
->filter()
138+
->values()
139+
->all();
136140

137141
return new UserMessage($content, id: $message['id'] ?? null);
138142
})

0 commit comments

Comments
 (0)