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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,105 @@ describe('ConversationStore', () => {
expect(update).not.toHaveBeenCalled();
});

it('renameConversationId updates a conversation id and current id', async () => {
const { store, update, conversations } = createStore([
{
id: 'conversation-1',
title: 'Conversation',
messages: [{ role: 'user' as const, content: 'first', timestamp: 1 }],
createdAt: 1,
updatedAt: 1,
},
]);
store.setCurrentConversationId('conversation-1');

await expect(
store.renameConversationId('conversation-1', 'session-1'),
).resolves.toBe(true);

expect(conversations[0]?.id).toBe('session-1');
expect(store.getCurrentConversationId()).toBe('session-1');
expect(update).toHaveBeenCalledWith('conversations', conversations);
});

it('renameConversationId returns false when the target id already exists', async () => {
const { store, update, conversations } = createStore([
{
id: 'conversation-1',
title: 'Conversation',
messages: [],
createdAt: 1,
updatedAt: 1,
},
{
id: 'session-1',
title: 'Existing Session',
messages: [],
createdAt: 2,
updatedAt: 2,
},
]);

await expect(
store.renameConversationId('conversation-1', 'session-1'),
).resolves.toBe(false);

expect(conversations.map((conversation) => conversation.id)).toEqual([
'conversation-1',
'session-1',
]);
expect(update).not.toHaveBeenCalled();
});

it('upsertConversation inserts a missing conversation with cloned messages', async () => {
const messages = [
{ role: 'user' as const, content: 'first', timestamp: 1 },
];
const { store, update, conversations } = createStore([]);

await store.upsertConversation({
id: 'session-1',
title: 'Session',
messages,
createdAt: 1,
updatedAt: 1,
});

expect(conversations).toHaveLength(1);
expect(conversations[0]?.id).toBe('session-1');
expect(conversations[0]?.messages).toEqual(messages);
expect(conversations[0]?.messages[0]).not.toBe(messages[0]);
expect(store.getCurrentConversationId()).toBe('session-1');
expect(update).toHaveBeenCalledWith('conversations', conversations);
});

it('upsertConversation replaces an existing conversation', async () => {
const { store, update, conversations } = createStore([
{
id: 'session-1',
title: 'Old',
messages: [{ role: 'user' as const, content: 'old', timestamp: 1 }],
createdAt: 1,
updatedAt: 1,
},
]);

await store.upsertConversation({
id: 'session-1',
title: 'New',
messages: [{ role: 'assistant' as const, content: 'new', timestamp: 2 }],
createdAt: 1,
updatedAt: 2,
});

expect(conversations).toHaveLength(1);
expect(conversations[0]?.title).toBe('New');
expect(conversations[0]?.messages).toEqual([
{ role: 'assistant', content: 'new', timestamp: 2 },
]);
expect(update).toHaveBeenCalledWith('conversations', conversations);
});

it('truncateFromUserTurn truncates from the matching user turn', async () => {
const { store, update, conversations } = createStore([
{
Expand Down
69 changes: 69 additions & 0 deletions packages/vscode-ide-companion/src/services/conversationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,75 @@ export class ConversationStore {
return true;
}

async renameConversationId(
fromConversationId: string,
toConversationId: string,
): Promise<boolean> {
if (fromConversationId === toConversationId) {
return true;
}

const conversations = await this.getAllConversations();
const sourceIndex = conversations.findIndex(
(c) => c.id === fromConversationId,
);

if (sourceIndex < 0) {
console.warn(
'[ConversationStore] renameConversationId: source conversation not found:',
fromConversationId,
);
return false;
}

if (conversations.some((c) => c.id === toConversationId)) {
console.warn(
'[ConversationStore] renameConversationId: target conversation already exists:',
toConversationId,
);
return false;
}

const source = conversations[sourceIndex];
if (!source) {
return false;
}

conversations[sourceIndex] = {
...source,
id: toConversationId,
updatedAt: Date.now(),
};

await this.context.globalState.update('conversations', conversations);

if (this.currentConversationId === fromConversationId) {
this.currentConversationId = toConversationId;
}

return true;
}

async upsertConversation(conversation: Conversation): Promise<void> {
const conversations = await this.getAllConversations();
const storedConversation: Conversation = {
...conversation,
messages: conversation.messages.map((message) => ({ ...message })),
};
const existingIndex = conversations.findIndex(
(c) => c.id === conversation.id,
);

if (existingIndex >= 0) {
conversations[existingIndex] = storedConversation;
} else {
conversations.push(storedConversation);
}

await this.context.globalState.update('conversations', conversations);
this.currentConversationId = conversation.id;
}

async truncateFromUserTurn(
conversationId: string,
targetTurnIndex: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export abstract class BaseMessageHandler implements IMessageHandler {
protected conversationStore: ConversationStore,
protected currentConversationId: string | null,
protected sendToWebView: (message: unknown) => void,
private readonly syncCurrentConversationId?: (id: string | null) => void,
) {}

abstract handle(message: { type: string; data?: unknown }): Promise<void>;
Expand All @@ -49,6 +50,18 @@ export abstract class BaseMessageHandler implements IMessageHandler {
this.currentConversationId = id;
}

/**
* Update current conversation ID through the owning router when available.
*/
protected updateCurrentConversationId(id: string | null): void {
if (this.syncCurrentConversationId) {
this.syncCurrentConversationId(id);
return;
}

this.currentConversationId = id;
}

/**
* Get current conversation ID
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class MessageRouter {
conversationStore,
currentConversationId,
sendToWebView,
(id) => this.setCurrentConversationId(id),
);

this.fileHandler = new FileMessageHandler(
Expand Down
Loading
Loading