Repository: koishijs/novelai-bot
File: src/index.ts (TypeScript)

Code before:
            const workflow = image ? workflowImage2Image : workflowText2Image
            logger.debug('workflow:', workflow)
            const prompt = JSON.parse(await readFile(workflow, 'utf8'))

            // have to upload image to the comfyui server first
            if (image) {
              const body = new FormData()
              const capture = /^data:([\w/.+-]+);base64,(.*)$/.exec(image.dataUrl)
              const [, mime,] = capture
              
              let name = Date.now().toString() 
              const ext = mime === 'image/jpeg' ? 'jpg' : mime === 'image/png' ? 'png' : ''
              if (ext) name += `.${ext}`
              const imageFile = new Blob([image.buffer], {type:mime})
              body.append("image", imageFile, name)
              const res = await ctx.http(trimSlash(config.endpoint) + "/upload/image", {
                method: 'POST',
                headers: {
                  ...config.headers,
                },
                data: body,
              })
              if (res.status === 200) {
                const data = res.data
                let imagePath = data.name
                if (data.subfolder) imagePath = data.subfolder + "/" + imagePath
      
                for (const nodeId in prompt) {
                  if (prompt[nodeId].class_type === 'LoadImage') {
                    prompt[nodeId].inputs.image = imagePath
                    break
                  }
                }
              } else {
                throw new SessionError('commands.novelai.messages.unknown-error')
              }
            }

            // only change the first node in the workflow
            for (const nodeId in prompt) {
              if (prompt[nodeId].class_type === 'KSampler') {
                prompt[nodeId].inputs.seed = parameters.seed
                prompt[nodeId].inputs.steps = parameters.steps
                prompt[nodeId].inputs.cfg = parameters.scale
                prompt[nodeId].inputs.sampler_name = options.sampler
                prompt[nodeId].inputs.denoise = options.strength ?? config.strength
                prompt[nodeId].inputs.scheduler = options.scheduler ?? config.scheduler
                const positiveNodeId = prompt[nodeId].inputs.positive[0]
                const negativeeNodeId = prompt[nodeId].inputs.negative[0]
                const latentImageNodeId = prompt[nodeId].inputs.latent_image[0]
                prompt[positiveNodeId].inputs.text = parameters.prompt

Review comment (suggestion):
```suggestion
                if (data.subfolder) imagePath = data.subfolder + '/' + imagePath
```

Code after:
            const workflow = image ? workflowImage2Image : workflowText2Image
            logger.debug('workflow:', workflow)
            const prompt = JSON.parse(await readFile(workflow, 'utf8'))

            // have to upload image to the comfyui server first
            if (image) {
              const body = new FormData()
              const capture = /^data:([\w/.+-]+);base64,(.*)$/.exec(image.dataUrl)
              const [, mime,] = capture
              
              let name = Date.now().toString() 
              const ext = mime === 'image/jpeg' ? 'jpg' : mime === 'image/png' ? 'png' : ''
              if (ext) name += `.${ext}`
              const imageFile = new Blob([image.buffer], {type:mime})
              body.append("image", imageFile, name)
              const res = await ctx.http(trimSlash(config.endpoint) + '/upload/image', {
                method: 'POST',
                headers: {
                  ...config.headers,
                },
                data: body,
              })
              if (res.status === 200) {
                const data = res.data
                let imagePath = data.name
                if (data.subfolder) imagePath = data.subfolder + '/' + imagePath
      
                for (const nodeId in prompt) {
                  if (prompt[nodeId].class_type === 'LoadImage') {
                    prompt[nodeId].inputs.image = imagePath
                    break
                  }
                }
              } else {
                throw new SessionError('commands.novelai.messages.unknown-error')
              }
            }

            // only change the first node in the workflow
            for (const nodeId in prompt) {
              if (prompt[nodeId].class_type === 'KSampler') {
                prompt[nodeId].inputs.seed = parameters.seed
                prompt[nodeId].inputs.steps = parameters.steps
                prompt[nodeId].inputs.cfg = parameters.scale
                prompt[nodeId].inputs.sampler_name = options.sampler
                prompt[nodeId].inputs.denoise = options.strength ?? config.strength
                prompt[nodeId].inputs.scheduler = options.scheduler ?? config.scheduler
                const positiveNodeId = prompt[nodeId].inputs.positive[0]
                const negativeeNodeId = prompt[nodeId].inputs.negative[0]
                const latentImageNodeId = prompt[nodeId].inputs.latent_image[0]
                prompt[positiveNodeId].inputs.text = parameters.prompt