Skip to content

Commit 81969f1

Browse files
authored
feat(copilot): copilot tool refactor (#1030)
* get user workflow * get env vars and oauth creds * metadtata * Search docs * More tools * Build edit workflow * Read workflwo console * v1 * UPdates * Fixes * Updates * Skip tools * Lint * Fix tests * Remove logs * Remove more logs * UPdate
1 parent 9aa1fe8 commit 81969f1

35 files changed

+2604
-1209
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ describe('Copilot Chat API Route', () => {
107107
COPILOT_API_KEY: 'test-sim-agent-key',
108108
BETTER_AUTH_URL: 'http://localhost:3000',
109109
},
110+
isTruthy: (value: string | boolean | number | undefined) =>
111+
typeof value === 'string'
112+
? value.toLowerCase() === 'true' || value === '1'
113+
: Boolean(value),
110114
}))
111115

112116
global.fetch = vi.fn()

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@/lib/copilot/auth'
1313
import { getCopilotModel } from '@/lib/copilot/config'
1414
import { TITLE_GENERATION_SYSTEM_PROMPT, TITLE_GENERATION_USER_PROMPT } from '@/lib/copilot/prompts'
15+
import { getBlocksAndToolsTool } from '@/lib/copilot/tools/server-tools/blocks/get-blocks-and-tools'
1516
import { env } from '@/lib/env'
1617
import { createLogger } from '@/lib/logs/console/logger'
1718
import { SIM_AGENT_API_URL_DEFAULT } from '@/lib/sim-agent'
@@ -409,6 +410,26 @@ export async function POST(req: NextRequest) {
409410
[...messages].reverse().find((m) => m?.role === 'user') || messages[messages.length - 1]
410411
const messagesForAgent = effectiveConversationId ? [latestUserMessage] : messages
411412

413+
// Prepare optional prefetch results
414+
let prefetchResults: Record<string, any> | undefined
415+
if (effectivePrefetch === true) {
416+
try {
417+
const prefetchResp = await getBlocksAndToolsTool.execute({})
418+
if (prefetchResp.success) {
419+
prefetchResults = { get_blocks_and_tools: prefetchResp.data }
420+
logger.info(`[${tracker.requestId}] Prepared prefetchResults for streaming payload`, {
421+
hasBlocksAndTools: !!prefetchResp.data,
422+
})
423+
} else {
424+
logger.warn(`[${tracker.requestId}] Failed to prefetch get_blocks_and_tools`, {
425+
error: prefetchResp.error,
426+
})
427+
}
428+
} catch (e) {
429+
logger.error(`[${tracker.requestId}] Error while preparing prefetchResults`, e)
430+
}
431+
}
432+
412433
const requestPayload = {
413434
messages: messagesForAgent,
414435
workflowId,
@@ -422,6 +443,7 @@ export async function POST(req: NextRequest) {
422443
...(typeof effectivePrefetch === 'boolean' ? { prefetch: effectivePrefetch } : {}),
423444
...(session?.user?.name && { userName: session.user.name }),
424445
...(requestOrigin ? { origin: requestOrigin } : {}),
446+
...(prefetchResults ? { prefetchResults } : {}),
425447
}
426448

427449
// Log the payload being sent to the streaming endpoint
@@ -438,10 +460,6 @@ export async function POST(req: NextRequest) {
438460
messagesCount: requestPayload.messages.length,
439461
...(requestOrigin ? { origin: requestOrigin } : {}),
440462
})
441-
// Full payload as JSON string
442-
logger.info(
443-
`[${tracker.requestId}] Full streaming payload: ${JSON.stringify(requestPayload)}`
444-
)
445463
} catch (e) {
446464
logger.warn(`[${tracker.requestId}] Failed to log payload preview for streaming endpoint`, e)
447465
}
@@ -622,6 +640,15 @@ export async function POST(req: NextRequest) {
622640
if (event.data?.id) {
623641
announcedToolCallIds.add(event.data.id)
624642
}
643+
if (event.data?.name === 'get_user_workflow') {
644+
logger.info(
645+
`[${tracker.requestId}] get_user_workflow tool call received in stream; client will execute locally and post to /api/copilot/methods`,
646+
{
647+
toolCallId: event.data?.id,
648+
hasArgs: !!event.data?.arguments,
649+
}
650+
)
651+
}
625652
}
626653
break
627654

0 commit comments

Comments
 (0)