Skip to content

Commit 86c5e1b

Browse files
committed
updates
1 parent cca1772 commit 86c5e1b

File tree

4 files changed

+60
-103
lines changed

4 files changed

+60
-103
lines changed

apps/sim/app/api/wand/route.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
1111
import { env } from '@/lib/core/config/env'
1212
import { getCostMultiplier, isBillingEnabled } from '@/lib/core/config/feature-flags'
1313
import { generateRequestId } from '@/lib/core/utils/request'
14+
import { enrichTableSchema } from '@/lib/table/llm/wand'
1415
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1516
import { 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

6567
function 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+
73107
async 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') {

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-wand.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useCallback, useRef, useState } from 'react'
22
import { createLogger } from '@sim/logger'
33
import { useQueryClient } from '@tanstack/react-query'
4-
import { fetchTableSchemaContext } from '@/lib/table/wand'
54
import type { GenerationType } from '@/blocks/types'
65
import { subscriptionKeys } from '@/hooks/queries/subscription'
76
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
@@ -16,19 +15,16 @@ interface ChatMessage {
1615
interface BuildWandContextInfoOptions {
1716
currentValue?: string
1817
generationType?: string
19-
tableId?: string | null
20-
workspaceId?: string | null
2118
}
2219

2320
/**
2421
* Builds rich context information based on current content and generation type.
22+
* Note: Table schema context is now fetched server-side in /api/wand for simplicity.
2523
*/
26-
async function buildWandContextInfo({
24+
function buildWandContextInfo({
2725
currentValue,
2826
generationType,
29-
tableId,
30-
workspaceId,
31-
}: BuildWandContextInfoOptions): Promise<string> {
27+
}: BuildWandContextInfoOptions): string {
3228
const hasContent = Boolean(currentValue && currentValue.trim() !== '')
3329
const contentLength = currentValue?.length ?? 0
3430
const lineCount = currentValue ? currentValue.split('\n').length : 0
@@ -62,13 +58,6 @@ async function buildWandContextInfo({
6258
}
6359
}
6460

65-
if (generationType === 'table-schema') {
66-
const tableSchemaContext = await fetchTableSchemaContext({ tableId, workspaceId })
67-
if (tableSchemaContext) {
68-
contextInfo += `\n\n${tableSchemaContext}`
69-
}
70-
}
71-
7261
return contextInfo
7362
}
7463

@@ -102,7 +91,7 @@ export function useWand({
10291
onGenerationComplete,
10392
}: UseWandProps) {
10493
const queryClient = useQueryClient()
105-
const workspaceId = useWorkflowRegistry((state) => state.hydration.workspaceId)
94+
const workflowId = useWorkflowRegistry((state) => state.hydration.workflowId)
10695
const [isLoading, setIsLoading] = useState(false)
10796
const [isPromptVisible, setIsPromptVisible] = useState(false)
10897
const [promptInputValue, setPromptInputValue] = useState('')
@@ -173,11 +162,9 @@ export function useWand({
173162
}
174163

175164
try {
176-
const contextInfo = await buildWandContextInfo({
165+
const contextInfo = buildWandContextInfo({
177166
currentValue,
178167
generationType: wandConfig?.generationType,
179-
tableId: contextParams?.tableId,
180-
workspaceId,
181168
})
182169

183170
let systemPrompt = wandConfig?.prompt || ''
@@ -201,6 +188,8 @@ export function useWand({
201188
stream: true,
202189
history: wandConfig?.maintainHistory ? conversationHistory : [],
203190
generationType: wandConfig?.generationType,
191+
workflowId,
192+
wandContext: contextParams?.tableId ? { tableId: contextParams.tableId } : undefined,
204193
}),
205194
signal: abortControllerRef.current.signal,
206195
cache: 'no-store',
@@ -308,7 +297,7 @@ export function useWand({
308297
onGenerationComplete,
309298
queryClient,
310299
contextParams?.tableId,
311-
workspaceId,
300+
workflowId,
312301
]
313302
)
314303

apps/sim/lib/table/llm/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
/**
2-
* LLM enrichment utilities for table operations.
3-
*/
4-
51
export * from './enrichment'

apps/sim/lib/table/wand.ts

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

0 commit comments

Comments
 (0)