Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

remove useless workflow

Hugo Richarde9800a6fe0f1
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 711c5a8..e177560 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -21,6 +21,6 @@ jobs:
run: bun install
- name: 🛠️ Build
- run: bun run build
+ run: bun run prepare
env:
BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }}
diff --git a/apps/chat/server/api/stats/compute.post.ts b/apps/chat/server/api/stats/compute.post.ts
deleted file mode 100644
index fdd5aad..0000000
--- a/apps/chat/server/api/stats/compute.post.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { z } from 'zod'
-import { start } from 'workflow/api'
-import { computeStats } from '../../workflows/compute-stats'
-
-/**
- * POST /api/stats/compute
- * Trigger stats computation workflow (admin only)
- */
-export default defineEventHandler(async (event) => {
- await requireAdmin(event)
-
- // Body is optional - parse empty body as empty object
- const rawBody = await readBody(event) ?? {}
- const body = z.object({
- date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(),
- }).parse(rawBody)
-
- // Use provided date or yesterday (since today's data may be incomplete)
- const targetDate = body.date ?? (() => {
- const yesterday = new Date()
- yesterday.setDate(yesterday.getDate() - 1)
- return yesterday.toISOString().split('T')[0]!
- })()
-
- await start(computeStats, [{ date: targetDate }])
-
- return {
- status: 'started',
- message: `Stats computation started for ${targetDate}`,
- date: targetDate,
- }
-})
diff --git a/apps/chat/server/workflows/compute-stats/index.ts b/apps/chat/server/workflows/compute-stats/index.ts
deleted file mode 100644
index 32b2969..0000000
--- a/apps/chat/server/workflows/compute-stats/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export { computeStats } from './workflow'
-export type { ComputeStatsConfig, ComputeStatsResult, AggregatedStats } from './types'
diff --git a/apps/chat/server/workflows/compute-stats/steps/aggregate.ts b/apps/chat/server/workflows/compute-stats/steps/aggregate.ts
deleted file mode 100644
index d677449..0000000
--- a/apps/chat/server/workflows/compute-stats/steps/aggregate.ts
+++ /dev/null
@@ -1,180 +0,0 @@
-/**
- * Step: Aggregate Stats
- *
- * Aggregates message statistics for a specific date into the usage_stats table.
- */
-
-import { getStepMetadata, RetryableError } from 'workflow'
-import { log } from 'evlog'
-import { db, schema } from '@nuxthub/db'
-import { and, gte, lt, isNotNull, isNull, eq } from 'drizzle-orm'
-import type { AggregatedStats, ComputeStatsResult } from '../types'
-
-export async function stepAggregateStats(date: string): Promise<ComputeStatsResult> {
- 'use step'
-
- const { stepId, attempt } = getStepMetadata()
- log.info('stats', `[${stepId}] Computing stats for ${date} (attempt ${attempt})`)
-
- try {
- const startDate = new Date(`${date}T00:00:00.000Z`)
- const endDate = new Date(`${date}T23:59:59.999Z`)
-
- const messagesWithStats = await db
- .select({
- chatId: schema.messages.chatId,
- model: schema.messages.model,
- inputTokens: schema.messages.inputTokens,
- outputTokens: schema.messages.outputTokens,
- durationMs: schema.messages.durationMs,
- })
- .from(schema.messages)
- .where(
- and(
- isNotNull(schema.messages.model),
- gte(schema.messages.createdAt, startDate),
- lt(schema.messages.createdAt, endDate),
- ),
- )
-
- if (messagesWithStats.length === 0) {
- log.info('stats', `[${stepId}] No messages found for ${date}`)
- return {
- success: true,
- date,
- statsCreated: 0,
- userStats: 0,
- globalStats: 0,
- }
- }
-
- log.info('stats', `[${stepId}] Found ${messagesWithStats.length} messages to aggregate`)
-
- const chatIds = [...new Set(messagesWithStats.map(m => m.chatId))]
- const chats = await db
- .select({ id: schema.chats.id, userId: schema.chats.userId })
- .from(schema.chats)
- .where(
- chatIds.length === 1
- ? eq(schema.chats.id, chatIds[0]!)
- : undefined, // Will filter in JS for multiple
- )
-
- const chatUserMap = new Map<string, string>()
- for (const chat of chats) {
- if (chatIds.includes(chat.id)) {
- chatUserMap.set(chat.id, chat.userId)
- }
- }
-
- const userModelMap = new Map<string, AggregatedStats>()
- const globalModelMap = new Map<string, AggregatedStats>()
-
- for (const msg of messagesWithStats) {
- if (!msg.model)
- continue
-
- const userId = chatUserMap.get(msg.chatId) ?? null
- const inputTokens = msg.inputTokens ?? 0
- const outputTokens = msg.outputTokens ?? 0
- const durationMs = msg.durationMs ?? 0
-
- if (userId) {
- const userKey = `${userId}:${msg.model}`
- const userStats = userModelMap.get(userKey) ?? {
- userId,
- model: msg.model,
- messageCount: 0,
- totalInputTokens: 0,
- totalOutputTokens: 0,
- totalDurationMs: 0,
- }
- userStats.messageCount++
- userStats.totalInputTokens += inputTokens
- userStats.totalOutputTokens += outputTokens
- userStats.totalDurationMs += durationMs
- userModelMap.set(userKey, userStats)
- }
-
- // Global stats (userId = null)
- const globalKey = msg.model
- const globalStats = globalModelMap.get(globalKey) ?? {
- userId: null,
- model: msg.model,
- messageCount: 0,
- totalInputTokens: 0,
- totalOutputTokens: 0,
- totalDurationMs: 0,
- }
- globalStats.messageCount++
- globalStats.totalInputTokens += inputTokens
- globalStats.totalOutputTokens += outputTokens
- globalStats.totalDurationMs += durationMs
- globalModelMap.set(globalKey, globalStats)
- }
-
- const allStats = [...userModelMap.values(), ...globalModelMap.values()]
-
- for (const stats of allStats) {
- const existing = await db
- .select()
- .from(schema.usageStats)
- .where(
- and(
- eq(schema.usageStats.date, date),
- stats.userId
- ? eq(schema.usageStats.userId, stats.userId)
- : isNull(schema.usageStats.userId),
- eq(schema.usageStats.model, stats.model),
- ),
- )
- .limit(1)
-
- if (existing.length > 0) {
- await db
- .update(schema.usageStats)
- .set({
- messageCount: stats.messageCount,
- totalInputTokens: stats.totalInputTokens,
- totalOutputTokens: stats.totalOutputTokens,
- totalDurationMs: stats.totalDurationMs,
- })
- .where(eq(schema.usageStats.id, existing[0]!.id))
- } else {
- await db.insert(schema.usageStats).values({
- date,
- userId: stats.userId,
- model: stats.model,
- messageCount: stats.messageCount,
- totalInputTokens: stats.totalInputTokens,
- totalOutputTokens: stats.totalOutputTokens,
- totalDurationMs: stats.totalDurationMs,
- })
- }
- }
-
- const userStatsCount = userModelMap.size
- const globalStatsCount = globalModelMap.size
-
- log.info('stats', `[${stepId}] Created ${allStats.length} stats records (${userStatsCount} user, ${globalStatsCount} global)`)
-
- return {
- success: true,
- date,
- statsCreated: allStats.length,
- userStats: userStatsCount,
- globalStats: globalStatsCount,
- }
- } catch (error) {
- // Retry on database connection errors
- if (error instanceof Error && (error.message.includes('SQLITE_BUSY') || error.message.includes('ECONNRESET'))) {
- throw new RetryableError(`Database error: ${error.message}`, {
- retryAfter: attempt * 1000, // Exponential backoff
- })
- }
- throw error
- }
-}
-
-// Allow retries for database operations
-stepAggregateStats.maxRetries = 3
diff --git a/apps/chat/server/workflows/compute-stats/steps/index.ts b/apps/chat/server/workflows/compute-stats/steps/index.ts
deleted file mode 100644
index 91680be..0000000
--- a/apps/chat/server/workflows/compute-stats/steps/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export { stepAggregateStats } from './aggregate'
diff --git a/apps/chat/server/workflows/compute-stats/types.ts b/apps/chat/server/workflows/compute-stats/types.ts
deleted file mode 100644
index 5a1ecd3..0000000
--- a/apps/chat/server/workflows/compute-stats/types.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * Types for compute-stats workflow
- */
-
-export interface ComputeStatsConfig {
- /**
- * Date to compute stats for (YYYY-MM-DD format)
- * Defaults to yesterday
- */
- date: string
-}
-
-export interface AggregatedStats {
- userId: string | null // null = global stats
- model: string
- messageCount: number
- totalInputTokens: number
- totalOutputTokens: number
- totalDurationMs: number
-}
-
-export interface ComputeStatsResult {
- success: boolean
- date: string
- statsCreated: number
- userStats: number
- globalStats: number
-}
diff --git a/apps/chat/server/workflows/compute-stats/workflow.ts b/apps/chat/server/workflows/compute-stats/workflow.ts
deleted file mode 100644
index 8107928..0000000
--- a/apps/chat/server/workflows/compute-stats/workflow.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-/**
- * Compute Stats Workflow
- *
- * Aggregates daily usage statistics from messages into the usage_stats table.
- * Can be triggered manually via API or scheduled via cron.
- */
-
-import { FatalError } from 'workflow'
-import { log } from 'evlog'
-import type { ComputeStatsConfig, ComputeStatsResult } from './types'
-import { stepAggregateStats } from './steps'
-
-export async function computeStats(config: ComputeStatsConfig): Promise<ComputeStatsResult> {
- 'use workflow'
-
- // Validate date format
- if (!config.date || !/^\d{4}-\d{2}-\d{2}$/.test(config.date)) {
- throw new FatalError(`Invalid date format: ${config.date}. Expected YYYY-MM-DD`)
- }
-
- log.info('stats', `Starting stats computation for ${config.date}`)
-
- // Let errors propagate for retry semantics
- const result = await stepAggregateStats(config.date)
-
- log.info('stats', `✓ Stats computed: ${result.statsCreated} records for ${config.date}`)
-
- return result
-}