2023-09-29 11:11:01 -07:00
|
|
|
import { Program, ProgramMemory, _executor, SourceRange } from '../lang/wasm'
|
2024-07-25 19:03:56 +10:00
|
|
|
import { EngineCommandManager } from 'lang/std/engineConnection'
|
2024-08-03 18:08:51 +10:00
|
|
|
import { EngineCommand } from 'lang/std/artifactGraph'
|
2023-09-20 18:27:08 -07:00
|
|
|
import { Models } from '@kittycad/lib'
|
2024-04-15 17:18:32 -07:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
import { DefaultPlanes } from 'wasm-lib/kcl/bindings/DefaultPlanes'
|
2024-06-24 11:45:40 -04:00
|
|
|
import { err } from 'lib/trap'
|
2023-09-20 18:27:08 -07:00
|
|
|
|
2024-04-09 11:51:41 -07:00
|
|
|
type WebSocketResponse = Models['WebSocketResponse_type']
|
2023-06-22 16:43:33 +10:00
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
const defaultPlanes: DefaultPlanes = {
|
|
|
|
xy: uuidv4(),
|
|
|
|
xz: uuidv4(),
|
|
|
|
yz: uuidv4(),
|
|
|
|
negXy: uuidv4(),
|
|
|
|
negXz: uuidv4(),
|
|
|
|
negYz: uuidv4(),
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:15:07 +10:00
|
|
|
class MockEngineCommandManager {
|
2023-11-01 07:39:31 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
2023-07-10 15:15:07 +10:00
|
|
|
constructor(mockParams: {
|
|
|
|
setIsStreamReady: (isReady: boolean) => void
|
|
|
|
setMediaStream: (stream: MediaStream) => void
|
|
|
|
}) {}
|
|
|
|
startNewSession() {}
|
|
|
|
waitForAllCommands() {}
|
|
|
|
waitForReady = new Promise<void>((resolve) => resolve())
|
2023-08-24 15:34:51 -07:00
|
|
|
sendModelingCommand({
|
|
|
|
id,
|
|
|
|
range,
|
|
|
|
command,
|
|
|
|
}: {
|
|
|
|
id: string
|
|
|
|
range: SourceRange
|
|
|
|
command: EngineCommand
|
|
|
|
}): Promise<any> {
|
2023-09-20 18:27:08 -07:00
|
|
|
const response: WebSocketResponse = {
|
2024-04-09 11:51:41 -07:00
|
|
|
success: true,
|
|
|
|
resp: {
|
|
|
|
type: 'modeling',
|
|
|
|
data: {
|
|
|
|
modeling_response: { type: 'empty' },
|
|
|
|
},
|
2023-09-20 18:27:08 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return Promise.resolve(JSON.stringify(response))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
2024-04-15 17:18:32 -07:00
|
|
|
async wasmGetDefaultPlanes(): Promise<string> {
|
|
|
|
return JSON.stringify(defaultPlanes)
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
sendModelingCommandFromWasm(
|
|
|
|
id: string,
|
|
|
|
rangeStr: string,
|
|
|
|
commandStr: string
|
|
|
|
): Promise<any> {
|
|
|
|
if (id === undefined) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error('id is undefined'))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
if (rangeStr === undefined) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error('rangeStr is undefined'))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
if (commandStr === undefined) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error('commandStr is undefined'))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
const command: EngineCommand = JSON.parse(commandStr)
|
|
|
|
const range: SourceRange = JSON.parse(rangeStr)
|
|
|
|
|
|
|
|
return this.sendModelingCommand({ id, range, command })
|
|
|
|
}
|
2023-07-10 15:15:07 +10:00
|
|
|
sendSceneCommand() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function enginelessExecutor(
|
2024-06-24 11:45:40 -04:00
|
|
|
ast: Program | Error,
|
2024-07-22 19:43:40 -04:00
|
|
|
pm: ProgramMemory | Error = ProgramMemory.empty()
|
2023-07-10 15:15:07 +10:00
|
|
|
): Promise<ProgramMemory> {
|
2024-06-24 11:45:40 -04:00
|
|
|
if (err(ast)) return Promise.reject(ast)
|
|
|
|
if (err(pm)) return Promise.reject(pm)
|
|
|
|
|
2023-07-10 15:15:07 +10:00
|
|
|
const mockEngineCommandManager = new MockEngineCommandManager({
|
|
|
|
setIsStreamReady: () => {},
|
|
|
|
setMediaStream: () => {},
|
|
|
|
}) as any as EngineCommandManager
|
|
|
|
await mockEngineCommandManager.waitForReady
|
|
|
|
mockEngineCommandManager.startNewSession()
|
2024-03-26 19:32:31 -07:00
|
|
|
const programMemory = await _executor(ast, pm, mockEngineCommandManager, true)
|
2023-07-10 15:15:07 +10:00
|
|
|
await mockEngineCommandManager.waitForAllCommands()
|
|
|
|
return programMemory
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
export async function executor(
|
|
|
|
ast: Program,
|
2024-07-22 19:43:40 -04:00
|
|
|
pm: ProgramMemory = ProgramMemory.empty()
|
2023-06-22 16:43:33 +10:00
|
|
|
): Promise<ProgramMemory> {
|
2023-09-25 19:49:53 -07:00
|
|
|
const engineCommandManager = new EngineCommandManager()
|
|
|
|
engineCommandManager.start({
|
2023-06-23 14:19:15 +10:00
|
|
|
setIsStreamReady: () => {},
|
|
|
|
setMediaStream: () => {},
|
2023-09-25 19:49:53 -07:00
|
|
|
width: 0,
|
|
|
|
height: 0,
|
2023-09-29 12:41:58 -07:00
|
|
|
executeCode: () => {},
|
2024-04-15 17:18:32 -07:00
|
|
|
makeDefaultPlanes: () => {
|
|
|
|
return new Promise((resolve) => resolve(defaultPlanes))
|
|
|
|
},
|
2024-06-30 19:21:24 -07:00
|
|
|
modifyGrid: (hidden: boolean) => {
|
|
|
|
return new Promise((resolve) => resolve())
|
|
|
|
},
|
2023-06-23 14:19:15 +10:00
|
|
|
})
|
2023-07-10 15:15:07 +10:00
|
|
|
await engineCommandManager.waitForReady
|
2023-06-22 16:43:33 +10:00
|
|
|
engineCommandManager.startNewSession()
|
2024-03-26 19:32:31 -07:00
|
|
|
const programMemory = await _executor(ast, pm, engineCommandManager, false)
|
2023-10-14 03:47:46 +11:00
|
|
|
await engineCommandManager.waitForAllCommands()
|
2023-06-22 16:43:33 +10:00
|
|
|
return programMemory
|
|
|
|
}
|