Skip to content

Commit bddebd0

Browse files
authored
add console logs for reasoning for debugging (#8042)
* add console logs for reasoning for debugging Signed-off-by: Jessie Frazelle <[email protected]> * updates Signed-off-by: Jessie Frazelle <[email protected]> * updates Signed-off-by: Jessie Frazelle <[email protected]> --------- Signed-off-by: Jessie Frazelle <[email protected]>
1 parent 66ee258 commit bddebd0

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/lib/crossPlatformFetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import isomorphicFetch from 'isomorphic-fetch'
2-
import { isDesktop } from '@src/lib/isDesktop'
31
import env from '@src/env'
2+
import { isDesktop } from '@src/lib/isDesktop'
3+
import isomorphicFetch from 'isomorphic-fetch'
44

55
const headers = (token?: string): HeadersInit => ({
66
'Content-Type': 'application/json',

src/lib/promptToEdit.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type { File as KittyCadLibFile } from '@kittycad/lib/dist/types/src/model
2929
import type { FileMeta } from '@src/lib/types'
3030
import type { RequestedKCLFile } from '@src/machines/systemIO/utils'
3131
import { withAPIBaseURL, withSiteBaseURL } from '@src/lib/withBaseURL'
32+
import { connectReasoningStream } from '@src/lib/reasoningWs'
3233

3334
type KclFileMetaMap = {
3435
[execStateFileNamesIndex: number]: Extract<FileMeta, { type: 'kcl' }>
@@ -98,6 +99,8 @@ async function submitTextToCadRequest(
9899
return new Error(errorData.message || 'Unknown error')
99100
}
100101

102+
connectReasoningStream(token, data.id)
103+
101104
return data as TextToCadMultiFileIteration_type
102105
}
103106

src/lib/reasoningWs.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// wsReasoning.ts
2+
// Logs every frame with the async-op `id`, closes on {"type":"end_of_stream"}
3+
4+
import { withWebSocketURL } from '@src/lib/withBaseURL'
5+
6+
export function connectReasoningStream(token: string, id: string): void {
7+
const url = withWebSocketURL('').replace(
8+
'/ws/modeling/commands',
9+
`/ws/ml/reasoning/${id}`
10+
)
11+
const ws = new WebSocket(url)
12+
const authMessage = {
13+
type: 'headers',
14+
headers: {
15+
Authorization: `Bearer ${token}`,
16+
},
17+
}
18+
19+
ws.addEventListener('open', () => {
20+
console.log(`[${id}] open ${url}`)
21+
ws.send(JSON.stringify(authMessage)) // 🔸 send immediately
22+
console.log(`[${id}] →`, authMessage)
23+
})
24+
25+
ws.addEventListener('message', (ev) => {
26+
console.log(`[${id}] raw`, ev.data)
27+
28+
let msg: unknown
29+
try {
30+
msg = JSON.parse(ev.data as string)
31+
} catch {
32+
console.error(`[${id}] JSON parse error`, ev.data)
33+
return // non-JSON frame
34+
}
35+
36+
if ('error' in (msg as any)) {
37+
ws.send(JSON.stringify(authMessage)) // 🔸 send immediately
38+
console.log(`[${id}] →`, authMessage)
39+
}
40+
41+
if ('end_of_stream' in (msg as any)) {
42+
console.log(`[${id}] end_of_stream → closing`)
43+
ws.close(1000, 'done')
44+
}
45+
})
46+
47+
ws.addEventListener('close', (e) =>
48+
console.log(`[${id}] close`, e.code, e.reason)
49+
)
50+
ws.addEventListener('error', console.error)
51+
}

src/lib/textToCad.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import type { Models } from '@kittycad/lib'
2-
import toast from 'react-hot-toast'
3-
import type { NavigateFunction } from 'react-router-dom'
42
import {
53
ToastTextToCadError,
64
ToastTextToCadSuccess,
@@ -9,16 +7,19 @@ import { PROJECT_ENTRYPOINT } from '@src/lib/constants'
97
import crossPlatformFetch from '@src/lib/crossPlatformFetch'
108
import { getUniqueProjectName } from '@src/lib/desktopFS'
119
import { isDesktop } from '@src/lib/isDesktop'
10+
import { joinOSPaths } from '@src/lib/paths'
11+
import { connectReasoningStream } from '@src/lib/reasoningWs'
1212
import { kclManager, systemIOActor } from '@src/lib/singletons'
13+
import { err, reportRejection } from '@src/lib/trap'
14+
import { toSync } from '@src/lib/utils'
15+
import { withAPIBaseURL } from '@src/lib/withBaseURL'
16+
import { getAllSubDirectoriesAtProjectRoot } from '@src/machines/systemIO/snapshotContext'
1317
import {
1418
SystemIOMachineEvents,
1519
waitForIdleState,
1620
} from '@src/machines/systemIO/utils'
17-
import { err, reportRejection } from '@src/lib/trap'
18-
import { toSync } from '@src/lib/utils'
19-
import { getAllSubDirectoriesAtProjectRoot } from '@src/machines/systemIO/snapshotContext'
20-
import { joinOSPaths } from '@src/lib/paths'
21-
import { withAPIBaseURL } from '@src/lib/withBaseURL'
21+
import toast from 'react-hot-toast'
22+
import type { NavigateFunction } from 'react-router-dom'
2223

2324
export async function submitTextToCadPrompt(
2425
prompt: string,
@@ -51,6 +52,10 @@ export async function submitTextToCadPrompt(
5152
return new Error('No id returned from Text-to-CAD API')
5253
}
5354

55+
if (token) {
56+
connectReasoningStream(token, data.id)
57+
}
58+
5459
return data
5560
}
5661

0 commit comments

Comments
 (0)