Skip to content

Commit 4ecbd41

Browse files
committed
fix: relocate and fix counter when branch
1 parent 474edc0 commit 4ecbd41

5 files changed

Lines changed: 9 additions & 11 deletions

File tree

apps/backend-convex/convex/_counters.ts

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

apps/backend-convex/convex/_generated/api.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* @module
99
*/
1010

11-
import type * as _counters from "../_counters.js";
1211
import type * as authInfo from "../authInfo.js";
1312
import type * as crons from "../crons.js";
1413
import type * as http_ai from "../http/ai.js";
@@ -32,7 +31,6 @@ import type {
3231
* ```
3332
*/
3433
declare const fullApi: ApiFromModules<{
35-
_counters: typeof _counters;
3634
authInfo: typeof authInfo;
3735
crons: typeof crons;
3836
"http/ai": typeof http_ai;

apps/backend-convex/convex/messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { objectPick } from '@namesmt/utils'
22
import { ConvexError, v } from 'convex/values'
3-
import { messagesInThreadCounter } from './_counters'
3+
import { singleShardCounter } from '../utils/counters'
44
import { internalMutation, internalQuery, query } from './_generated/server'
55
import { assertThreadAccess } from './threads'
66

@@ -37,7 +37,7 @@ export const countByThread = query({
3737

3838
await assertThreadAccess(ctx, { thread, lockerKey: args.lockerKey })
3939

40-
return await messagesInThreadCounter.count(ctx, args.threadId)
40+
return await singleShardCounter.count(ctx, `messages-in-thread_${args.threadId}`)
4141
},
4242
})
4343

@@ -77,7 +77,7 @@ export const internalAdd = internalMutation({
7777
lockerKey: v.optional(v.string()),
7878
},
7979
handler: async (ctx, args) => {
80-
await messagesInThreadCounter.inc(ctx, args.threadId)
80+
await singleShardCounter.inc(ctx, `messages-in-thread_${args.threadId}`)
8181

8282
return await ctx.db.insert('messages', {
8383
...objectPick(args, ['threadId', 'role', 'content', 'context', 'isStreaming', 'streamId', 'provider', 'model']),

apps/backend-convex/convex/threads.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { clearUndefined } from '@namesmt/utils'
66
import { openrouter } from '@openrouter/ai-sdk-provider'
77
import { generateText } from 'ai'
88
import { ConvexError, v } from 'convex/values'
9-
import { messagesInThreadCounter } from './_counters'
9+
import { singleShardCounter } from '../utils/counters'
1010
import { api, internal } from './_generated/api'
1111
import { action, internalMutation, mutation, query } from './_generated/server'
1212

@@ -139,7 +139,7 @@ export const branchThreadFromMessage = mutation({
139139
})
140140
}))
141141

142-
await messagesInThreadCounter.add(ctx, thread._id, messages.length)
142+
await singleShardCounter.add(ctx, `messages-in-thread_${newThreadId}`, messages.length)
143143

144144
return newThreadId
145145
},
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { ShardedCounter } from '@convex-dev/sharded-counter'
2+
import { components } from '../convex/_generated/api'
3+
4+
export const singleShardCounter = new ShardedCounter(components.shardedCounter, { defaultShards: 1 })

0 commit comments

Comments
 (0)