Skip to content
Open
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
19 changes: 12 additions & 7 deletions packages/api/src/routes/mcp/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,18 @@ export const TOOLS: ToolDefinition[] = [
zodSchema: storeConversationSchema,
inputSchema: jsonSchema(storeConversationSchema),
handler: async (c, args: z.infer<typeof storeConversationSchema>) => {
const result = await conversationsService.createConversation(c.get("db"), {
projectId: c.get("projectId"),
externalId: args.external_id ?? null,
title: args.title ?? null,
metadata: args.metadata ?? null,
inputMessages: args.messages,
});
const result = await conversationsService.createConversation(
c.get("db"),
{
projectId: c.get("projectId"),
externalId: args.external_id ?? null,
title: args.title ?? null,
metadata: args.metadata ?? null,
inputMessages: args.messages,
},
c.executionCtx,
c.env.AUTH_CACHE,
);
if (result.error) throw new ToolError(result.error.code, result.error.message);
return {
id: result.conversationId,
Expand Down
12 changes: 12 additions & 0 deletions packages/api/src/services/mcp-conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

import { and, asc, desc, eq, gt, lt, sql } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
// Hono's type, not the workers-types v5 global — see services/conversations.ts.
import type { ExecutionContext } from "hono";
import { conversations, conversationTags, messages } from "../db/schema";
import { invalidateAnalyticsCache } from "../lib/analytics-cache";
import { CACHE_COUNT_TTL_S } from "../lib/config";
import { generateId } from "../lib/id";
import { serializeMetadata } from "../lib/serialization";
Expand Down Expand Up @@ -118,6 +121,8 @@ export function validateCursor(
export async function createConversation(
db: DrizzleD1Database,
input: CreateConversationInput,
executionCtx: ExecutionContext,
cache?: KVNamespace,
): Promise<CreateConversationResult> {
const { projectId, externalId, title, metadata, inputMessages } = input;
const now = Date.now();
Expand Down Expand Up @@ -188,6 +193,8 @@ export async function createConversation(
await db.insert(messages).values(rows);
}

invalidateAnalyticsCache(cache, executionCtx, projectId);

return {
conversationId,
projectId,
Expand Down Expand Up @@ -340,11 +347,16 @@ export async function updateConversation(
export async function deleteConversation(
db: DrizzleD1Database,
conversationId: string,
projectId: string,
executionCtx: ExecutionContext,
cache?: KVNamespace,
): Promise<void> {
// Batch delete: tags → messages → conversation (order respects FK constraints)
await db.batch([
db.delete(conversationTags).where(eq(conversationTags.conversationId, conversationId)),
db.delete(messages).where(eq(messages.conversationId, conversationId)),
db.delete(conversations).where(eq(conversations.id, conversationId)),
]);

invalidateAnalyticsCache(cache, executionCtx, projectId);
}
50 changes: 50 additions & 0 deletions packages/api/test/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,56 @@ describe("Analytics", () => {
expect(recentIds).toContain(created.id);
});

it("counts match after MCP store_conversation, with no stale cache (issue #372)", async () => {
const baselineRes = await fetchAnalytics();
const baseline = await baselineRes.json<AnalyticsResponse>();
const baseConvs = baseline.summary.total_conversations;
const baseMsgs = baseline.summary.total_messages;
const baseTokens = baseline.summary.total_tokens;
const mcpMessages = [
{ role: "user", content: "Hello", token_count: 4 },
{ role: "assistant", content: "Hi", token_count: 6 },
];

const store = await SELF.fetch("http://localhost/api/mcp", {
method: "POST",
headers: {
...authHeaders(),
Accept: "application/json, text/event-stream",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "store_conversation",
arguments: {
title: "MCP Analytics Cache Test",
messages: mcpMessages,
},
},
}),
});
expect(store.status).toBe(200);
const storeJson = (await store.json()) as {
result?: { isError?: boolean; content: Array<{ text: string }> };
};
expect(storeJson.result?.isError).toBeUndefined();
const created = JSON.parse(storeJson.result?.content[0].text ?? "{}") as { id?: string };
expect(created.id).toBeTruthy();

const afterRes = await fetchAnalytics();
const after = await afterRes.json<AnalyticsResponse>();

expect(after.summary.total_conversations).toBe(baseConvs + 1);
expect(after.summary.total_messages).toBe(baseMsgs + mcpMessages.length);
const expectedTokenDelta = mcpMessages.reduce(
(sum, message) => sum + (message.token_count ?? 0),
0,
);
expect(after.summary.total_tokens).toBe(baseTokens + expectedTokenDelta);
});

it("reflects deleted conversations immediately, with no stale cache (issue #352)", async () => {
const createRes = await createConversation({
title: "To Be Deleted",
Expand Down