Skip to content

Commit d7a06fc

Browse files
committed
add logger
1 parent 5286c54 commit d7a06fc

Some content is hidden

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

46 files changed

+639
-163
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
/README.md export-ignore
99
/testbench.yaml export-ignore
1010
/ecs.php export-ignore
11+
/rector.php export-ignore
1112
/tests export-ignore
1213
/phpstan.neon export-ignore
1314
/workbench export-ignore
15+
/scratchpad.php export-ignore
16+
/phpstan.dist.neon export-ignore
17+
/package.json export-ignore
18+
/vite.config.ts export-ignore
19+
/tsconfig.json export-ignore
20+
/eslint.config.js export-ignore

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"laravel/wayfinder": "^0.1.13",
4141
"league/event": "^3.0",
4242
"mockery/mockery": "^1.6",
43+
"monolog/monolog": "^3.10",
4344
"orchestra/testbench": "^10.6",
4445
"pestphp/pest": "^4.0",
4546
"pestphp/pest-plugin-type-coverage": "^4.0",

resources/js/components/assistant.tsx

Lines changed: 8 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import {
2-
AssistantRuntimeProvider,
3-
makeAssistantTool,
4-
} from "@assistant-ui/react";
5-
import {
6-
useChatRuntime,
7-
AssistantChatTransport,
8-
} from "@assistant-ui/react-ai-sdk";
1+
import { AssistantRuntimeProvider } from "@assistant-ui/react";
2+
import { useChatRuntime, AssistantChatTransport } from "@assistant-ui/react-ai-sdk";
93
import { Thread } from "@/components/assistant-ui/thread";
104
import { ThreadList } from "@/components/assistant-ui/thread-list";
11-
import { WeatherCard } from "@/components/assistant-ui/weather-card";
5+
import { WeatherToolUI } from "@/components/tools/weather-tool";
126
import api from "@/routes/cortex/api";
13-
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
7+
// import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
148

159
export const Assistant = ({ agent }: { agent: string }) => {
1610
const runtime = useChatRuntime({
@@ -23,107 +17,18 @@ export const Assistant = ({ agent }: { agent: string }) => {
2317
}),
2418
});
2519

26-
const WeatherToolUI = makeAssistantTool({
27-
toolName: "get_weather",
28-
render: ({ args, result, status }) => {
29-
if (status.type === "running") {
30-
return (
31-
<div className="flex items-center gap-2">
32-
<span>
33-
Checking weather in {args.location as string}...
34-
</span>
35-
</div>
36-
);
37-
}
38-
39-
if (status.type === "incomplete" && status.reason === "error") {
40-
return (
41-
<div className="text-red-500">
42-
Failed to get weather for {args.location as string}
43-
</div>
44-
);
45-
}
46-
47-
let weatherResult: Record<string, unknown> | undefined = undefined;
48-
49-
if (typeof result === "string") {
50-
weatherResult = JSON.parse(result) as Record<string, unknown>;
51-
}
52-
53-
return (
54-
<WeatherCard
55-
temperature={
56-
typeof weatherResult?.temperature === "number"
57-
? weatherResult?.temperature
58-
: undefined
59-
}
60-
feelsLike={
61-
typeof weatherResult?.feels_like === "number"
62-
? weatherResult?.feels_like
63-
: undefined
64-
}
65-
humidity={
66-
typeof weatherResult?.humidity === "number"
67-
? weatherResult?.humidity
68-
: undefined
69-
}
70-
windSpeed={
71-
typeof weatherResult?.wind_speed === "number"
72-
? weatherResult?.wind_speed
73-
: undefined
74-
}
75-
windGusts={
76-
typeof weatherResult?.wind_gusts === "number"
77-
? weatherResult?.wind_gusts
78-
: undefined
79-
}
80-
conditions={
81-
typeof weatherResult?.conditions === "string"
82-
? weatherResult?.conditions
83-
: undefined
84-
}
85-
location={
86-
typeof weatherResult?.location === "string"
87-
? weatherResult?.location
88-
: undefined
89-
}
90-
temperatureUnit={
91-
typeof weatherResult?.temperature_unit === "string"
92-
? weatherResult?.temperature_unit
93-
: undefined
94-
}
95-
windSpeedUnit={
96-
typeof weatherResult?.wind_speed_unit === "string"
97-
? weatherResult?.wind_speed_unit
98-
: undefined
99-
}
100-
conditionsCode={
101-
typeof weatherResult?.conditions_code === "number"
102-
? weatherResult?.conditions_code
103-
: undefined
104-
}
105-
time={
106-
typeof weatherResult?.time === "string"
107-
? weatherResult?.time
108-
: undefined
109-
}
110-
/>
111-
);
112-
},
113-
});
114-
11520
return (
11621
<AssistantRuntimeProvider runtime={runtime}>
11722
<WeatherToolUI />
11823
<div className="flex h-svh flex-row justify-items-start">
119-
{/* <div className="w-64 m-4">
24+
{/* <div className="w-96 m-4">
12025
<ThreadList />
12126
</div> */}
122-
<div className="flex h-full w-full flex-col overflow-hidden">
27+
<div className="flex h-[calc(100vh-64px)] w-full flex-col overflow-hidden">
12328
<Thread />
12429
</div>
125-
{/* <div className="w-64 m-4">
126-
<Card className="h-full">
30+
{/* <div className="w-96 m-4">
31+
<Card className="h-full bg-muted border-none">
12732
<CardHeader>
12833
<CardTitle>Agent Metadata</CardTitle>
12934
</CardHeader>
File renamed without changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { makeAssistantTool } from "@assistant-ui/react";
2+
import { WeatherCard } from "./weather-card";
3+
4+
export const WeatherToolUI = makeAssistantTool({
5+
toolName: "get_weather",
6+
render: ({ args, result, status }) => {
7+
if (status.type === "running") {
8+
return (
9+
<div className="flex items-center gap-2">
10+
<span>
11+
Checking weather in {args.location as string}...
12+
</span>
13+
</div>
14+
);
15+
}
16+
17+
if (status.type === "incomplete" && status.reason === "error") {
18+
return (
19+
<div className="text-red-500">
20+
Failed to get weather for {args.location as string}
21+
</div>
22+
);
23+
}
24+
25+
let weatherResult: Record<string, unknown> | undefined = undefined;
26+
27+
if (typeof result === "string") {
28+
weatherResult = JSON.parse(result) as Record<string, unknown>;
29+
}
30+
31+
return (
32+
<WeatherCard
33+
temperature={
34+
typeof weatherResult?.temperature === "number"
35+
? weatherResult?.temperature
36+
: undefined
37+
}
38+
feelsLike={
39+
typeof weatherResult?.feels_like === "number"
40+
? weatherResult?.feels_like
41+
: undefined
42+
}
43+
humidity={
44+
typeof weatherResult?.humidity === "number"
45+
? weatherResult?.humidity
46+
: undefined
47+
}
48+
windSpeed={
49+
typeof weatherResult?.wind_speed === "number"
50+
? weatherResult?.wind_speed
51+
: undefined
52+
}
53+
windGusts={
54+
typeof weatherResult?.wind_gusts === "number"
55+
? weatherResult?.wind_gusts
56+
: undefined
57+
}
58+
conditions={
59+
typeof weatherResult?.conditions === "string"
60+
? weatherResult?.conditions
61+
: undefined
62+
}
63+
location={
64+
typeof weatherResult?.location === "string"
65+
? weatherResult?.location
66+
: undefined
67+
}
68+
temperatureUnit={
69+
typeof weatherResult?.temperature_unit === "string"
70+
? weatherResult?.temperature_unit
71+
: undefined
72+
}
73+
windSpeedUnit={
74+
typeof weatherResult?.wind_speed_unit === "string"
75+
? weatherResult?.wind_speed_unit
76+
: undefined
77+
}
78+
conditionsCode={
79+
typeof weatherResult?.conditions_code === "number"
80+
? weatherResult?.conditions_code
81+
: undefined
82+
}
83+
time={
84+
typeof weatherResult?.time === "string"
85+
? weatherResult?.time
86+
: undefined
87+
}
88+
/>
89+
);
90+
},
91+
});

src/Agents/Agent.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use Cortex\Memory\Stores\InMemoryStore;
4040
use Cortex\LLM\Data\ChatGenerationChunk;
4141
use Cortex\Agents\Stages\HandleToolCalls;
42-
use Cortex\Agents\Stages\TrackAgentStart;
4342
use Cortex\JsonSchema\Types\ObjectSchema;
4443
use Cortex\LLM\Data\Messages\UserMessage;
4544
use Cortex\LLM\Enums\StructuredOutputMode;
@@ -416,15 +415,10 @@ public function hasPromptVariables(): bool
416415

417416
protected function buildPipeline(): Pipeline
418417
{
419-
$tools = Utils::toToolCollection($this->getTools());
420418
$executionStages = $this->executionStages();
419+
$tools = Utils::toToolCollection($this->getTools());
421420

422-
$pipeline = new Pipeline(
423-
new TrackAgentStart($this),
424-
...$executionStages,
425-
);
426-
427-
return $pipeline->when(
421+
return new Pipeline(...$executionStages)->when(
428422
$tools->isNotEmpty(),
429423
fn(Pipeline $pipeline): Pipeline => $pipeline->pipe(
430424
new HandleToolCalls($tools, $this->memory, $executionStages, $this->maxSteps),
@@ -509,19 +503,42 @@ protected function invokePipeline(
509503
$result = $this->pipeline
510504
->enableStreaming($streaming)
511505
->onStart(function (PipelineStart $event): void {
506+
$event->config->pushChunkWhenStreaming(
507+
new ChatGenerationChunk(
508+
ChunkType::RunStart,
509+
metadata: [
510+
'run_id' => $event->config->runId,
511+
'thread_id' => $event->config->threadId,
512+
],
513+
),
514+
fn() => $this->dispatchEvent(new AgentStart($this, $event->config)),
515+
);
512516
$this->withRuntimeConfig($event->config);
513517
})
514518
->onEnd(function (PipelineEnd $event): void {
515519
$this->withRuntimeConfig($event->config);
516520
$event->config->pushChunkWhenStreaming(
517-
new ChatGenerationChunk(ChunkType::RunEnd),
521+
new ChatGenerationChunk(
522+
ChunkType::RunEnd,
523+
metadata: [
524+
'run_id' => $event->config->runId,
525+
'thread_id' => $event->config->threadId,
526+
],
527+
),
518528
fn() => $this->dispatchEvent(new AgentEnd($this, $event->config)),
519529
);
520530
})
521531
->onError(function (PipelineError $event): void {
522532
$this->withRuntimeConfig($event->config);
523533
$event->config->pushChunkWhenStreaming(
524-
new ChatGenerationChunk(ChunkType::Error, exception: $event->exception),
534+
new ChatGenerationChunk(
535+
ChunkType::Error,
536+
exception: $event->exception,
537+
metadata: [
538+
'run_id' => $event->config->runId,
539+
'thread_id' => $event->config->threadId,
540+
],
541+
),
525542
fn() => $this->dispatchEvent(new AgentStepError($this, $event->exception, $event->config)),
526543
);
527544
})

src/Agents/Stages/TrackAgentStart.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)