Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up code
  • Loading branch information
Sg312 committed Feb 10, 2026
commit b5b0c396002a57c1573260fe30091b7a4d89c896
50 changes: 47 additions & 3 deletions apps/sim/app/api/copilot/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSession } from '@/lib/auth'
import { buildConversationHistory } from '@/lib/copilot/chat-context'
import { resolveOrCreateChat } from '@/lib/copilot/chat-lifecycle'
import { buildCopilotRequestPayload } from '@/lib/copilot/chat-payload'
import { generateChatTitle } from '@/lib/copilot/chat-title'
import { SIM_AGENT_API_URL } from '@/lib/copilot/constants'
import { COPILOT_REQUEST_MODES } from '@/lib/copilot/models'
import { orchestrateCopilotStream } from '@/lib/copilot/orchestrator'
import {
Expand All @@ -23,10 +23,54 @@ import {
createRequestTracker,
createUnauthorizedResponse,
} from '@/lib/copilot/request-helpers'
import { env } from '@/lib/core/config/env'
import { resolveWorkflowIdForUser } from '@/lib/workflows/utils'

const logger = createLogger('CopilotChatAPI')

async function requestChatTitleFromCopilot(params: {
message: string
model: string
provider?: string
}): Promise<string | null> {
const { message, model, provider } = params
if (!message || !model) return null

const headers: Record<string, string> = {
'Content-Type': 'application/json',
}
if (env.COPILOT_API_KEY) {
headers['x-api-key'] = env.COPILOT_API_KEY
}

try {
const response = await fetch(`${SIM_AGENT_API_URL}/api/generate-chat-title`, {
method: 'POST',
headers,
body: JSON.stringify({
message,
model,
...(provider ? { provider } : {}),
}),
})

const payload = await response.json().catch(() => ({}))
if (!response.ok) {
logger.warn('Failed to generate chat title via copilot backend', {
status: response.status,
error: payload,
})
return null
}

const title = typeof payload?.title === 'string' ? payload.title.trim() : ''
return title || null
} catch (error) {
logger.error('Error generating chat title:', error)
return null
}
}

const FileAttachmentSchema = z.object({
id: z.string(),
key: z.string(),
Expand Down Expand Up @@ -280,7 +324,7 @@ export async function POST(req: NextRequest) {
}

if (actualChatId && !currentChat?.title && conversationHistory.length === 0) {
generateChatTitle({ message, model: selectedModel, provider })
requestChatTitleFromCopilot({ message, model: selectedModel, provider })
.then(async (title) => {
if (title) {
await db
Expand Down Expand Up @@ -407,7 +451,7 @@ export async function POST(req: NextRequest) {
// Start title generation in parallel if this is first message (non-streaming)
if (actualChatId && !currentChat.title && conversationHistory.length === 0) {
logger.info(`[${tracker.requestId}] Starting title generation for non-streaming response`)
generateChatTitle({ message, model: selectedModel, provider })
requestChatTitleFromCopilot({ message, model: selectedModel, provider })
.then(async (title) => {
if (title) {
await db
Expand Down
4 changes: 2 additions & 2 deletions apps/sim/app/api/mcp/copilot/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ async function handleBuildToolCall(
): Promise<CallToolResult> {
try {
const requestText = (args.request as string) || JSON.stringify(args)
const { model } = getCopilotModel('chat')
const { model } = getCopilotModel()
const workflowId = args.workflowId as string | undefined

const resolved = workflowId ? { workflowId } : await resolveWorkflowIdForUser(userId)
Expand Down Expand Up @@ -721,7 +721,7 @@ async function handleSubagentToolCall(
context.plan = args.plan
}

const { model } = getCopilotModel('chat')
const { model } = getCopilotModel()

const result = await orchestrateSubagentStream(
toolDef.agentId,
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/app/api/v1/copilot/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function POST(req: NextRequest) {
try {
const body = await req.json()
const parsed = RequestSchema.parse(body)
const defaults = getCopilotModel('chat')
const defaults = getCopilotModel()
const selectedModel = parsed.model || defaults.model

// Resolve workflow ID
Expand Down
59 changes: 0 additions & 59 deletions apps/sim/lib/copilot/chat-title.ts

This file was deleted.

Loading