Skip to content

Commit 224f1c0

Browse files
committed
pass through thread id
1 parent dfe90c3 commit 224f1c0

File tree

8 files changed

+177
-218
lines changed

8 files changed

+177
-218
lines changed

src/Agents/Agent.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function __construct(
101101
protected array $middleware = [],
102102
) {
103103
$this->prompt = self::buildPromptTemplate($prompt, $strict, $initialPromptVariables);
104-
$this->memory = self::buildMemory($this->prompt, $this->memoryStore);
104+
$this->memory = self::buildMemory($this->prompt, $runtimeConfig?->threadId, $this->memoryStore);
105105
$this->middleware = [...$this->defaultMiddleware(), ...$this->middleware];
106106

107107
// Reset the prompt to only the message placeholders, since the initial
@@ -432,6 +432,7 @@ protected function invokePipeline(
432432
$messages = Utils::toMessageCollection($messages);
433433

434434
$this->memory
435+
->setThreadId($config->threadId)
435436
->setMessages($this->memory->getMessages()->merge($messages))
436437
->setVariables([
437438
...$this->initialPromptVariables,
@@ -502,9 +503,12 @@ protected static function buildPromptTemplate(
502503
/**
503504
* Build the memory instance for the agent.
504505
*/
505-
protected static function buildMemory(ChatPromptTemplate $prompt, ?Store $memoryStore = null): ChatMemoryContract
506-
{
507-
$memoryStore ??= new InMemoryStore(Str::uuid7()->toString());
506+
protected static function buildMemory(
507+
ChatPromptTemplate $prompt,
508+
?string $threadId = null,
509+
?Store $memoryStore = null,
510+
): ChatMemoryContract {
511+
$memoryStore ??= new InMemoryStore($threadId ?? Str::uuid7()->toString());
508512
$memoryStore->setMessages($prompt->messages->withoutPlaceholders());
509513

510514
return new ChatMemory($memoryStore);

src/Contracts/ChatMemory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public function getVariables(): array;
5050
* Delegates to the underlying store - the store is the source of truth for threadId.
5151
*/
5252
public function getThreadId(): string;
53+
54+
/**
55+
* Set the thread ID for this memory instance.
56+
*/
57+
public function setThreadId(string $threadId): static;
5358
}

src/Memory/ChatMemory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,11 @@ public function getThreadId(): string
9999
{
100100
return $this->store->getThreadId();
101101
}
102+
103+
public function setThreadId(string $threadId): static
104+
{
105+
$this->store->setThreadId($threadId);
106+
107+
return $this;
108+
}
102109
}

src/Memory/Contracts/Store.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public function reset(): void;
4040
* Get the thread ID for this store.
4141
*/
4242
public function getThreadId(): string;
43+
44+
/**
45+
* Set the thread ID for this store.
46+
*/
47+
public function setThreadId(string $threadId): void;
4348
}

src/Memory/Stores/CacheStore.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public function getThreadId(): string
7070
{
7171
return $this->threadId;
7272
}
73+
74+
public function setThreadId(string $threadId): void
75+
{
76+
$this->threadId = $threadId;
77+
}
7378
}

src/Memory/Stores/InMemoryStore.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public function getThreadId(): string
4444
{
4545
return $this->threadId;
4646
}
47+
48+
public function setThreadId(string $threadId): void
49+
{
50+
$this->threadId = $threadId;
51+
}
4752
}

tests/Unit/Agents/AgentOldTest.php

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

0 commit comments

Comments
 (0)