@@ -11,6 +11,7 @@ import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
1111import { env } from '@/lib/core/config/env'
1212import { getCostMultiplier , isBillingEnabled } from '@/lib/core/config/feature-flags'
1313import { generateRequestId } from '@/lib/core/utils/request'
14+ import { enrichTableSchema } from '@/lib/table/llm/wand'
1415import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1516import { getModelPricing } from '@/providers/utils'
1617
@@ -60,6 +61,7 @@ interface RequestBody {
6061 history ?: ChatMessage [ ]
6162 workflowId ?: string
6263 generationType ?: string
64+ wandContext ?: Record < string , unknown >
6365}
6466
6567function safeStringify ( value : unknown ) : string {
@@ -70,6 +72,38 @@ function safeStringify(value: unknown): string {
7072 }
7173}
7274
75+ /**
76+ * Wand enricher function type.
77+ * Enrichers add context to the system prompt based on generationType.
78+ */
79+ type WandEnricher = (
80+ workspaceId : string | null ,
81+ context : Record < string , unknown >
82+ ) => Promise < string | null >
83+
84+ /**
85+ * Registry of wand enrichers by generationType.
86+ * Each enricher returns additional context to append to the system prompt.
87+ */
88+ const wandEnrichers : Partial < Record < string , WandEnricher > > = {
89+ timestamp : async ( ) => {
90+ const now = new Date ( )
91+ return `Current date and time context for reference:
92+ - Current UTC timestamp: ${ now . toISOString ( ) }
93+ - Current Unix timestamp (seconds): ${ Math . floor ( now . getTime ( ) / 1000 ) }
94+ - Current Unix timestamp (milliseconds): ${ now . getTime ( ) }
95+ - Current date (UTC): ${ now . toISOString ( ) . split ( 'T' ) [ 0 ] }
96+ - Current year: ${ now . getUTCFullYear ( ) }
97+ - Current month: ${ now . getUTCMonth ( ) + 1 }
98+ - Current day of month: ${ now . getUTCDate ( ) }
99+ - Current day of week: ${ [ 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ] [ now . getUTCDay ( ) ] }
100+
101+ Use this context to calculate relative dates like "yesterday", "last week", "beginning of this month", etc.`
102+ } ,
103+
104+ 'table-schema' : enrichTableSchema ,
105+ }
106+
73107async function updateUserStatsForWand (
74108 userId : string ,
75109 usage : {
@@ -159,7 +193,15 @@ export async function POST(req: NextRequest) {
159193 try {
160194 const body = ( await req . json ( ) ) as RequestBody
161195
162- const { prompt, systemPrompt, stream = false , history = [ ] , workflowId, generationType } = body
196+ const {
197+ prompt,
198+ systemPrompt,
199+ stream = false ,
200+ history = [ ] ,
201+ workflowId,
202+ generationType,
203+ wandContext = { } ,
204+ } = body
163205
164206 if ( ! prompt ) {
165207 logger . warn ( `[${ requestId } ] Invalid request: Missing prompt.` )
@@ -227,20 +269,15 @@ export async function POST(req: NextRequest) {
227269 systemPrompt ||
228270 'You are a helpful AI assistant. Generate content exactly as requested by the user.'
229271
230- if ( generationType === 'timestamp' ) {
231- const now = new Date ( )
232- const currentTimeContext = `\n\nCurrent date and time context for reference:
233- - Current UTC timestamp: ${ now . toISOString ( ) }
234- - Current Unix timestamp (seconds): ${ Math . floor ( now . getTime ( ) / 1000 ) }
235- - Current Unix timestamp (milliseconds): ${ now . getTime ( ) }
236- - Current date (UTC): ${ now . toISOString ( ) . split ( 'T' ) [ 0 ] }
237- - Current year: ${ now . getUTCFullYear ( ) }
238- - Current month: ${ now . getUTCMonth ( ) + 1 }
239- - Current day of month: ${ now . getUTCDate ( ) }
240- - Current day of week: ${ [ 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ] [ now . getUTCDay ( ) ] }
241-
242- Use this context to calculate relative dates like "yesterday", "last week", "beginning of this month", etc.`
243- finalSystemPrompt += currentTimeContext
272+ // Apply enricher if one exists for this generationType
273+ if ( generationType ) {
274+ const enricher = wandEnrichers [ generationType ]
275+ if ( enricher ) {
276+ const enrichment = await enricher ( workspaceId , wandContext )
277+ if ( enrichment ) {
278+ finalSystemPrompt += `\n\n${ enrichment } `
279+ }
280+ }
244281 }
245282
246283 if ( generationType === 'json-object' ) {
0 commit comments