Skip to content

Commit fce1423

Browse files
authored
v0.3.46: fix copilot stats updates
v0.3.46: fix copilot stats updates
2 parents 581929b + 3656d3d commit fce1423

File tree

5 files changed

+12
-125
lines changed

5 files changed

+12
-125
lines changed

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,11 @@ import { SIM_AGENT_API_URL_DEFAULT } from '@/lib/sim-agent'
1212

1313
const SIM_AGENT_API_URL = env.SIM_AGENT_API_URL || SIM_AGENT_API_URL_DEFAULT
1414

15-
const BodySchema = z
16-
.object({
17-
// Do NOT send id; messageId is the unique correlator
18-
userId: z.string().optional(),
19-
chatId: z.string().uuid().optional(),
20-
messageId: z.string().optional(),
21-
depth: z.number().int().nullable().optional(),
22-
maxEnabled: z.boolean().nullable().optional(),
23-
createdAt: z.union([z.string().datetime(), z.date()]).optional(),
24-
diffCreated: z.boolean().nullable().optional(),
25-
diffAccepted: z.boolean().nullable().optional(),
26-
duration: z.number().int().nullable().optional(),
27-
inputTokens: z.number().int().nullable().optional(),
28-
outputTokens: z.number().int().nullable().optional(),
29-
aborted: z.boolean().nullable().optional(),
30-
})
31-
.passthrough()
15+
const BodySchema = z.object({
16+
messageId: z.string(),
17+
diffCreated: z.boolean(),
18+
diffAccepted: z.boolean(),
19+
})
3220

3321
export async function POST(req: NextRequest) {
3422
const tracker = createRequestTracker()
@@ -43,15 +31,15 @@ export async function POST(req: NextRequest) {
4331
if (!parsed.success) {
4432
return createBadRequestResponse('Invalid request body for copilot stats')
4533
}
46-
const body = parsed.data as any
4734

48-
// Build outgoing payload for Sim Agent; do not include id
35+
const { messageId, diffCreated, diffAccepted } = parsed.data as any
36+
37+
// Build outgoing payload for Sim Agent with only required fields
4938
const payload: Record<string, any> = {
50-
...body,
51-
userId: body.userId || userId,
52-
createdAt: body.createdAt || new Date().toISOString(),
39+
messageId,
40+
diffCreated,
41+
diffAccepted,
5342
}
54-
payload.id = undefined
5543

5644
const agentRes = await fetch(`${SIM_AGENT_API_URL}/api/stats`, {
5745
method: 'POST',

apps/sim/lib/copilot/tools/client/workflow/build-workflow.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,6 @@ export class BuildWorkflowClientTool extends BaseClientTool {
9595
// Populate diff preview immediately (without marking complete yet)
9696
try {
9797
const diffStore = useWorkflowDiffStore.getState()
98-
// Send early stats upsert with the triggering user message id if available
99-
try {
100-
const { useCopilotStore } = await import('@/stores/copilot/store')
101-
const { currentChat, currentUserMessageId, agentDepth, agentPrefetch } =
102-
useCopilotStore.getState() as any
103-
if (currentChat?.id && currentUserMessageId) {
104-
fetch('/api/copilot/stats', {
105-
method: 'POST',
106-
headers: { 'Content-Type': 'application/json' },
107-
body: JSON.stringify({
108-
chatId: currentChat.id,
109-
messageId: currentUserMessageId,
110-
depth: agentDepth,
111-
maxEnabled: agentDepth >= 2 && !agentPrefetch,
112-
diffCreated: true,
113-
}),
114-
}).catch(() => {})
115-
}
116-
} catch {}
11798
await diffStore.setProposedChanges(result.yamlContent)
11899
logger.info('diff proposed changes set')
119100
} catch (e) {

apps/sim/lib/copilot/tools/client/workflow/edit-workflow.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,6 @@ export class EditWorkflowClientTool extends BaseClientTool {
151151
try {
152152
if (!this.hasAppliedDiff) {
153153
const diffStore = useWorkflowDiffStore.getState()
154-
// Send early stats upsert with the triggering user message id if available
155-
try {
156-
const { useCopilotStore } = await import('@/stores/copilot/store')
157-
const { currentChat, currentUserMessageId, agentDepth, agentPrefetch } =
158-
useCopilotStore.getState() as any
159-
if (currentChat?.id && currentUserMessageId) {
160-
fetch('/api/copilot/stats', {
161-
method: 'POST',
162-
headers: { 'Content-Type': 'application/json' },
163-
body: JSON.stringify({
164-
chatId: currentChat.id,
165-
messageId: currentUserMessageId,
166-
depth: agentDepth,
167-
maxEnabled: agentDepth >= 2 && !agentPrefetch,
168-
diffCreated: true,
169-
}),
170-
}).catch(() => {})
171-
}
172-
} catch {}
173154
await diffStore.setProposedChanges(result.yamlContent)
174155
logger.info('diff proposed changes set for edit_workflow')
175156
this.hasAppliedDiff = true

apps/sim/stores/copilot/store.ts

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,27 +1685,6 @@ export const useCopilotStore = create<CopilotStore>()(
16851685
}).catch(() => {})
16861686
} catch {}
16871687
}
1688-
1689-
// Optimistic stats: mark aborted for the in-flight user message
1690-
try {
1691-
const { currentChat: cc, currentUserMessageId, messageMetaById } = get() as any
1692-
if (cc?.id && currentUserMessageId) {
1693-
const meta = messageMetaById?.[currentUserMessageId] || null
1694-
const agentDepth = meta?.depth
1695-
const maxEnabled = meta?.maxEnabled
1696-
fetch('/api/copilot/stats', {
1697-
method: 'POST',
1698-
headers: { 'Content-Type': 'application/json' },
1699-
body: JSON.stringify({
1700-
chatId: cc.id,
1701-
messageId: currentUserMessageId,
1702-
...(typeof agentDepth === 'number' ? { depth: agentDepth } : {}),
1703-
...(typeof maxEnabled === 'boolean' ? { maxEnabled } : {}),
1704-
aborted: true,
1705-
}),
1706-
}).catch(() => {})
1707-
}
1708-
} catch {}
17091688
} catch {
17101689
set({ isSendingMessage: false, isAborting: false, abortController: null })
17111690
}
@@ -2113,47 +2092,7 @@ export const useCopilotStore = create<CopilotStore>()(
21132092

21142093
// Post copilot_stats record (input/output tokens can be null for now)
21152094
try {
2116-
const { messageMetaById } = get() as any
2117-
const meta =
2118-
(messageMetaById && (messageMetaById as any)[triggerUserMessageId || '']) || null
2119-
const agentDepth = meta?.depth ?? get().agentDepth
2120-
const maxEnabled = meta?.maxEnabled ?? (agentDepth >= 2 && !get().agentPrefetch)
2121-
const { useWorkflowDiffStore } = await import('@/stores/workflow-diff/store')
2122-
const diffState = useWorkflowDiffStore.getState() as any
2123-
const diffCreated = !!diffState?.isShowingDiff
2124-
const diffAccepted = false // acceptance may arrive earlier or later via diff store
2125-
const endMs = Date.now()
2126-
const duration = Math.max(0, endMs - startTimeMs)
2127-
const chatIdToUse = get().currentChat?.id || context.newChatId
2128-
// Prefer provided trigger user message id; fallback to last user message
2129-
let userMessageIdToUse = triggerUserMessageId
2130-
if (!userMessageIdToUse) {
2131-
const msgs = get().messages
2132-
for (let i = msgs.length - 1; i >= 0; i--) {
2133-
const m = msgs[i]
2134-
if (m.role === 'user') {
2135-
userMessageIdToUse = m.id
2136-
break
2137-
}
2138-
}
2139-
}
2140-
if (chatIdToUse) {
2141-
fetch('/api/copilot/stats', {
2142-
method: 'POST',
2143-
headers: { 'Content-Type': 'application/json' },
2144-
body: JSON.stringify({
2145-
chatId: chatIdToUse,
2146-
messageId: userMessageIdToUse || assistantMessageId,
2147-
depth: agentDepth,
2148-
maxEnabled,
2149-
diffCreated,
2150-
diffAccepted,
2151-
duration: duration ?? null,
2152-
inputTokens: null,
2153-
outputTokens: null,
2154-
}),
2155-
}).catch(() => {})
2156-
}
2095+
// Removed: stats sending now occurs only on accept/reject with minimal payload
21572096
} catch {}
21582097
} finally {
21592098
clearTimeout(timeoutId)

apps/sim/stores/workflow-diff/store.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ export const useWorkflowDiffStore = create<WorkflowDiffState & WorkflowDiffActio
303303
method: 'POST',
304304
headers: { 'Content-Type': 'application/json' },
305305
body: JSON.stringify({
306-
chatId: currentChat.id,
307306
messageId: triggerMessageId,
308307
diffCreated: true,
309308
diffAccepted: true,
@@ -441,7 +440,6 @@ export const useWorkflowDiffStore = create<WorkflowDiffState & WorkflowDiffActio
441440
method: 'POST',
442441
headers: { 'Content-Type': 'application/json' },
443442
body: JSON.stringify({
444-
chatId: currentChat.id,
445443
messageId: triggerMessageId,
446444
diffCreated: true,
447445
diffAccepted: false,

0 commit comments

Comments
 (0)