Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/global-memory-title-generation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/core": patch
---

Fix conversation title generation when memory is supplied globally to VoltAgent.
2 changes: 1 addition & 1 deletion packages/core/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8199,7 +8199,7 @@ export class Agent {
if (this.memoryConfigured || this.memory === false) {
return;
}
this.memoryManager.setMemory(memory);
this.memoryManager.setMemory(memory, this.createConversationTitleGenerator(memory));
}

/**
Expand Down
31 changes: 23 additions & 8 deletions packages/core/src/memory/manager/memory-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,26 @@ export class MemoryManager {
context.input ?? messageWithMetadata,
"Conversation",
);
await this.conversationMemory?.createConversation({
id: conversationId,
userId: userId,
resourceId: this.resourceId,
title,
metadata: {},
});
try {
await this.conversationMemory?.createConversation({
id: conversationId,
userId: userId,
resourceId: this.resourceId,
title,
metadata: {},
});
} catch (createError) {
if (this.isConversationAlreadyExistsError(createError)) {
context.logger.debug(
"[Memory] Conversation already exists (race condition handled)",
{
conversationId,
},
);
} else {
throw createError;
}
}
}

// Add message to conversation using Memory V2's saveMessageWithContext
Expand Down Expand Up @@ -775,14 +788,16 @@ export class MemoryManager {
/**
* Replace the Memory instance used for this manager.
*/
setMemory(memory: Memory | false): void {
setMemory(memory: Memory | false, titleGenerator?: ConversationTitleGenerator): void {
if (memory === false) {
this.conversationMemory = undefined;
this.titleGenerator = undefined;
return;
}

if (memory instanceof Memory) {
this.conversationMemory = memory;
this.titleGenerator = titleGenerator;
}
}

Expand Down
61 changes: 61 additions & 0 deletions packages/core/src/voltagent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MockLanguageModelV3 } from "ai/test";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { z } from "zod";
import { Agent } from "./agent/agent";
Expand Down Expand Up @@ -59,6 +60,66 @@ describe("VoltAgent defaults", () => {
expect(agent.getMemory()).toBe(agentMemory);
});

it("applies title generation when default memory is assigned to a preconstructed agent", async () => {
const agentMemory = new Memory({
storage: new InMemoryStorageAdapter(),
generateTitle: true,
});
const model = new MockLanguageModelV3({
modelId: "title-model",
doGenerate: async () => ({
content: [{ type: "text", text: "Global Memory Title" }],
finishReason: "stop",
usage: {
inputTokens: 1,
outputTokens: 1,
totalTokens: 2,
inputTokenDetails: {
noCacheTokens: 1,
cacheReadTokens: 0,
cacheWriteTokens: 0,
},
outputTokenDetails: {
textTokens: 1,
reasoningTokens: 0,
},
},
warnings: [],
}),
});
const agent = new Agent({
name: "assistant",
instructions: "Be helpful.",
model: model as any,
});

const voltAgent = new VoltAgent({
agents: { assistant: agent },
memory: agentMemory,
checkDependencies: false,
});

const operationContext = (agent as any).createOperationContext("Plan a weekend trip to Rome.", {
userId: "user-1",
conversationId: "conversation-1",
});
await (agent as any).memoryManager.saveMessage(
operationContext,
{
id: "message-1",
role: "assistant",
parts: [{ type: "text", text: "Sure, let's plan it." }],
},
"user-1",
"conversation-1",
);

const conversation = await agentMemory.getConversation("conversation-1");
expect(conversation?.title).toBe("Global Memory Title");

await voltAgent.shutdown();
});

it("applies workspace to preconstructed registered agents without explicit workspace", async () => {
const workspace = new Workspace({ id: "global-workspace" });
const agent = new Agent({
Expand Down
Loading