| 88 | } |
| 89 | |
| 90 | function applyUsage( |
| 91 | db: DatabaseService, |
| 92 | sessionID: (typeof SessionV1.Event.MessageUpdated.Type)["data"]["sessionID"], |
| 93 | value: Usage, |
| 94 | sign = 1, |
| 95 | ) { |
| 96 | return db |
| 97 | .update(SessionTable) |
| 98 | .set({ |
| 99 | cost: sql`${SessionTable.cost} + ${value.cost * sign}`, |
| 100 | tokens_input: sql`${SessionTable.tokens_input} + ${value.tokens.input * sign}`, |
| 101 | tokens_output: sql`${SessionTable.tokens_output} + ${value.tokens.output * sign}`, |
| 102 | tokens_reasoning: sql`${SessionTable.tokens_reasoning} + ${value.tokens.reasoning * sign}`, |
| 103 | tokens_cache_read: sql`${SessionTable.tokens_cache_read} + ${value.tokens.cache.read * sign}`, |
| 104 | tokens_cache_write: sql`${SessionTable.tokens_cache_write} + ${value.tokens.cache.write * sign}`, |
| 105 | time_updated: sql`${SessionTable.time_updated}`, |
| 106 | }) |
| 107 | .where(eq(SessionTable.id, sessionID)) |
| 108 | .run() |
| 109 | .pipe(Effect.orDie) |
| 110 | } |
| 111 | |
| 112 | function run(db: DatabaseService, event: SessionEvent.Event) { |
| 113 | return Effect.gen(function* () { |