Skip to content

Commit 2354909

Browse files
committed
Lint
1 parent caccb61 commit 2354909

File tree

5 files changed

+77
-57
lines changed

5 files changed

+77
-57
lines changed

apps/sim/app/api/copilot/chat/route.ts

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { eq, and } from 'drizzle-orm'
1+
import { and, eq } from 'drizzle-orm'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { z } from 'zod'
4+
import { getSession } from '@/lib/auth'
45
import { createLogger } from '@/lib/logs/console-logger'
56
import { getRotatingApiKey } from '@/lib/utils'
6-
import { getSession } from '@/lib/auth'
77
import { db } from '@/db'
88
import { copilotChats } from '@/db/schema'
99
import { executeProviderRequest } from '@/providers'
1010
import type { Message } from '@/providers/types'
11-
import { executeTool } from '@/tools'
1211

1312
const logger = createLogger('CopilotChat')
1413

@@ -34,10 +33,11 @@ const CopilotChatSchema = z.object({
3433
async function generateChatTitle(userMessage: string): Promise<string> {
3534
try {
3635
const apiKey = getRotatingApiKey('anthropic')
37-
36+
3837
const response = await executeProviderRequest('anthropic', {
3938
model: 'claude-3-haiku-20240307',
40-
systemPrompt: 'You are a helpful assistant that generates concise, descriptive titles for chat conversations. Create a title that captures the main topic or question being discussed. Keep it under 50 characters and make it specific and clear.',
39+
systemPrompt:
40+
'You are a helpful assistant that generates concise, descriptive titles for chat conversations. Create a title that captures the main topic or question being discussed. Keep it under 50 characters and make it specific and clear.',
4141
context: `Generate a concise title for a conversation that starts with this user message: "${userMessage}"
4242
4343
Return only the title text, nothing else.`,
@@ -58,8 +58,6 @@ Return only the title text, nothing else.`,
5858
}
5959
}
6060

61-
62-
6361
/**
6462
* Generate chat response with tool calling support
6563
*/
@@ -84,8 +82,8 @@ function extractCitationsFromResponse(response: any): Array<{
8482
return []
8583
}
8684

87-
const docsSearchResult = response.toolResults.find((result: any) =>
88-
result.sources && Array.isArray(result.sources)
85+
const docsSearchResult = response.toolResults.find(
86+
(result: any) => result.sources && Array.isArray(result.sources)
8987
)
9088

9189
if (!docsSearchResult || !docsSearchResult.sources) {
@@ -109,15 +107,16 @@ async function generateChatResponse(
109107

110108
// Build conversation context
111109
const messages: Message[] = []
112-
110+
113111
// Add conversation history
114-
for (const msg of conversationHistory.slice(-10)) { // Keep last 10 messages
112+
for (const msg of conversationHistory.slice(-10)) {
113+
// Keep last 10 messages
115114
messages.push({
116115
role: msg.role as 'user' | 'assistant' | 'system',
117116
content: msg.content,
118117
})
119118
}
120-
119+
121120
// Add current user message
122121
messages.push({
123122
role: 'user',
@@ -170,7 +169,8 @@ MAKE SURE YOU FULLY ANSWER THE USER'S QUESTION.
170169
{
171170
id: 'docs_search_internal',
172171
name: 'Search Documentation',
173-
description: 'Search Sim Studio documentation for information about features, tools, workflows, and functionality',
172+
description:
173+
'Search Sim Studio documentation for information about features, tools, workflows, and functionality',
174174
params: {},
175175
parameters: {
176176
type: 'object',
@@ -204,43 +204,42 @@ MAKE SURE YOU FULLY ANSWER THE USER'S QUESTION.
204204
stream: false, // Always start with non-streaming to handle tool calls
205205
})
206206

207-
208-
209-
// If this is a streaming request and we got a regular response,
207+
// If this is a streaming request and we got a regular response,
210208
// we need to create a streaming response from the content
211209
if (stream && typeof response === 'object' && 'content' in response) {
212210
const content = response.content || 'Sorry, I could not generate a response.'
213-
211+
214212
// Extract citations from the provider response for later use
215213
const responseCitations = extractCitationsFromResponse(response)
216-
214+
217215
// Create a ReadableStream that emits the content in character chunks
218216
const streamResponse = new ReadableStream({
219217
start(controller) {
220218
// Use character-based streaming for more reliable transmission
221219
const chunkSize = 8 // Stream 8 characters at a time for smooth experience
222220
let index = 0
223-
221+
224222
const pushNext = () => {
225223
if (index < content.length) {
226224
const chunk = content.slice(index, index + chunkSize)
227225
controller.enqueue(new TextEncoder().encode(chunk))
228226
index += chunkSize
229-
227+
230228
// Add a small delay to simulate streaming
231229
setTimeout(pushNext, 25)
232230
} else {
233231
controller.close()
234232
}
235233
}
236-
234+
237235
pushNext()
238-
}
236+
},
239237
})
240-
238+
241239
// Store citations for later use in the main streaming handler
240+
242241
;(streamResponse as any)._citations = responseCitations
243-
242+
244243
return streamResponse
245244
}
246245

@@ -252,7 +251,9 @@ MAKE SURE YOU FULLY ANSWER THE USER'S QUESTION.
252251
return 'Sorry, I could not generate a response.'
253252
} catch (error) {
254253
logger.error('Failed to generate chat response:', error)
255-
throw new Error(`Failed to generate response: ${error instanceof Error ? error.message : 'Unknown error'}`)
254+
throw new Error(
255+
`Failed to generate response: ${error instanceof Error ? error.message : 'Unknown error'}`
256+
)
256257
}
257258
}
258259

@@ -268,7 +269,7 @@ export async function POST(req: NextRequest) {
268269
const { message, chatId, workflowId, createNewChat, stream } = CopilotChatSchema.parse(body)
269270

270271
const session = await getSession()
271-
272+
272273
logger.info(`[${requestId}] Copilot chat message: "${message}"`, {
273274
chatId,
274275
workflowId,
@@ -285,12 +286,7 @@ export async function POST(req: NextRequest) {
285286
const [existingChat] = await db
286287
.select()
287288
.from(copilotChats)
288-
.where(
289-
and(
290-
eq(copilotChats.id, chatId),
291-
eq(copilotChats.userId, session.user.id)
292-
)
293-
)
289+
.where(and(eq(copilotChats.id, chatId), eq(copilotChats.userId, session.user.id)))
294290
.limit(1)
295291

296292
if (existingChat) {
@@ -326,11 +322,11 @@ export async function POST(req: NextRequest) {
326322
const encoder = new TextEncoder()
327323
// Extract citations from the stream object if available
328324
const citations = (response as any)._citations || []
329-
330-
return new Response(
331-
new ReadableStream({
332-
async start(controller) {
333-
const reader = response.getReader()
325+
326+
return new Response(
327+
new ReadableStream({
328+
async start(controller) {
329+
const reader = response.getReader()
334330
let accumulatedResponse = ''
335331

336332
// Send initial metadata
@@ -352,7 +348,7 @@ export async function POST(req: NextRequest) {
352348

353349
const chunkText = new TextDecoder().decode(value)
354350
accumulatedResponse += chunkText
355-
351+
356352
const contentChunk = {
357353
type: 'content',
358354
content: chunkText,
@@ -430,14 +426,22 @@ export async function POST(req: NextRequest) {
430426
}
431427

432428
// Extract citations from response if available
433-
const citations = typeof response === 'object' && 'citations' in response ? response.citations :
434-
typeof response === 'object' && 'toolResults' in response ? extractCitationsFromResponse(response) : []
429+
const citations =
430+
typeof response === 'object' && 'citations' in response
431+
? response.citations
432+
: typeof response === 'object' && 'toolResults' in response
433+
? extractCitationsFromResponse(response)
434+
: []
435435

436436
const assistantMessage = {
437437
id: crypto.randomUUID(),
438438
role: 'assistant',
439-
content: typeof response === 'string' ? response :
440-
'content' in response ? response.content : '[Error generating response]',
439+
content:
440+
typeof response === 'string'
441+
? response
442+
: 'content' in response
443+
? response.content
444+
: '[Error generating response]',
441445
timestamp: new Date().toISOString(),
442446
citations: citations.length > 0 ? citations : undefined,
443447
}
@@ -466,8 +470,12 @@ export async function POST(req: NextRequest) {
466470

467471
return NextResponse.json({
468472
success: true,
469-
response: typeof response === 'string' ? response :
470-
'content' in response ? response.content : '[Error generating response]',
473+
response:
474+
typeof response === 'string'
475+
? response
476+
: 'content' in response
477+
? response.content
478+
: '[Error generating response]',
471479
chatId: currentChat?.id,
472480
metadata: {
473481
requestId,
@@ -485,4 +493,4 @@ export async function POST(req: NextRequest) {
485493
logger.error(`[${requestId}] Copilot chat error:`, error)
486494
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
487495
}
488-
}
496+
}

apps/sim/app/api/docs/search/route.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,18 @@ export async function POST(req: NextRequest) {
9393
// Step 3: Format the response with context and sources
9494
const context = chunks
9595
.map((chunk, index) => {
96-
const headerText = typeof chunk.headerText === 'string' ? chunk.headerText : String(chunk.headerText || 'Untitled Section')
97-
const sourceDocument = typeof chunk.sourceDocument === 'string' ? chunk.sourceDocument : String(chunk.sourceDocument || 'Unknown Document')
98-
const sourceLink = typeof chunk.sourceLink === 'string' ? chunk.sourceLink : String(chunk.sourceLink || '#')
99-
const chunkText = typeof chunk.chunkText === 'string' ? chunk.chunkText : String(chunk.chunkText || '')
96+
const headerText =
97+
typeof chunk.headerText === 'string'
98+
? chunk.headerText
99+
: String(chunk.headerText || 'Untitled Section')
100+
const sourceDocument =
101+
typeof chunk.sourceDocument === 'string'
102+
? chunk.sourceDocument
103+
: String(chunk.sourceDocument || 'Unknown Document')
104+
const sourceLink =
105+
typeof chunk.sourceLink === 'string' ? chunk.sourceLink : String(chunk.sourceLink || '#')
106+
const chunkText =
107+
typeof chunk.chunkText === 'string' ? chunk.chunkText : String(chunk.chunkText || '')
100108

101109
return `[${index + 1}] ${headerText}
102110
Document: ${sourceDocument}
@@ -138,4 +146,4 @@ Content: ${chunkText}`
138146
logger.error(`[${requestId}] Docs search error:`, error)
139147
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
140148
}
141-
}
149+
}

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/copilot.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const Copilot = forwardRef<CopilotRef, CopilotProps>(
208208
const decoder = new TextDecoder()
209209
let accumulatedContent = ''
210210
let newChatId: string | undefined
211-
let responseCitations: Array<{id: number, title: string, url: string}> = []
211+
let responseCitations: Array<{ id: number; title: string; url: string }> = []
212212

213213
while (true) {
214214
const { done, value } = await reader.read()
@@ -241,7 +241,8 @@ export const Copilot = forwardRef<CopilotRef, CopilotProps>(
241241
? {
242242
...msg,
243243
content: accumulatedContent,
244-
citations: responseCitations.length > 0 ? responseCitations : undefined,
244+
citations:
245+
responseCitations.length > 0 ? responseCitations : undefined,
245246
}
246247
: msg
247248
)
@@ -254,12 +255,13 @@ export const Copilot = forwardRef<CopilotRef, CopilotProps>(
254255
? {
255256
...msg,
256257
content: accumulatedContent,
257-
citations: responseCitations.length > 0 ? responseCitations : undefined,
258+
citations:
259+
responseCitations.length > 0 ? responseCitations : undefined,
258260
}
259261
: msg
260262
)
261263
)
262-
264+
263265
// Update current chat state with the chatId from response
264266
if (newChatId && !currentChat) {
265267
// For new chats, create a temporary chat object and reload the full chat list

apps/sim/tools/docs/search.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export const docsSearchTool: ToolConfig<DocsSearchParams, DocsSearchResponse> =
6868
},
6969

7070
transformError: (error) => {
71-
return error instanceof Error ? error.message : 'An error occurred while searching documentation'
71+
return error instanceof Error
72+
? error.message
73+
: 'An error occurred while searching documentation'
7274
},
73-
}
75+
}

apps/sim/tools/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { env } from '@/lib/env'
22
import { createLogger } from '@/lib/logs/console-logger'
33
import { useCustomToolsStore } from '@/stores/custom-tools/store'
44
import { useEnvironmentStore } from '@/stores/settings/environment/store'
5-
import { tools } from './registry'
65
import { docsSearchTool } from './docs/search'
6+
import { tools } from './registry'
77
import type { TableRow, ToolConfig, ToolResponse } from './types'
88

99
const logger = createLogger('ToolsUtils')

0 commit comments

Comments
 (0)