Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Built-in cmd #397

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions backend-golang/cmd_interactive_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,49 @@ import (
"bufio"
"io"
"os/exec"
"strconv"
"time"

wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
)

func (a *App) CmdInteractive(args []string, eventId string) error {
func (a *App) CmdInteractive(args []string, taskName string) error {
currentTime := time.Now().UnixMilli()
threadID := taskName + "_" + strconv.FormatInt(currentTime, 10)

wruntime.EventsEmit(a.ctx, "cmd_event")

cmd := exec.Command(args[0], args[1:]...)

stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}

cmd.Stderr = cmd.Stdout

err = cmd.Start()

if err != nil {
return err
}
cmds[eventId] = args
cmdProcesses[eventId] = cmd.Process

cmds[threadID] = args
cmdProcesses[threadID] = cmd.Process
reader := bufio.NewReader(stdout)

for {
line, _, err := reader.ReadLine()
if err != nil {
delete(cmds, eventId)
delete(cmdProcesses, eventId)
delete(cmds, threadID)
delete(cmdProcesses, threadID)
if err == io.EOF {
wruntime.EventsEmit(a.ctx, eventId+"-finish")
wruntime.EventsEmit(a.ctx, "cmd_event", threadID)
return nil
}
return err
}
strLine := string(line)
wruntime.EventsEmit(a.ctx, eventId+"-output", strLine)
wruntime.EventsEmit(a.ctx, "cmd_event", threadID, strLine)
}
}
27 changes: 19 additions & 8 deletions backend-golang/cmd_interactive_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,49 @@ import (
"bytes"
"io"
"os/exec"
"strconv"
"syscall"
"time"

wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

func (a *App) CmdInteractive(args []string, eventId string) error {
func (a *App) CmdInteractive(args []string, taskName string) error {
currentTime := time.Now().UnixMilli()
threadID := taskName + "_" + strconv.FormatInt(currentTime, 10)

wruntime.EventsEmit(a.ctx, "cmd_event")

cmd := exec.Command(args[0], args[1:]...)

stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
cmd.Stderr = cmd.Stdout

cmd.Stderr = cmd.Stdout
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.HideWindow = true

err = cmd.Start()

if err != nil {
return err
}
cmds[eventId] = args
cmdProcesses[eventId] = cmd.Process

cmds[taskName] = args
cmdProcesses[taskName] = cmd.Process
reader := bufio.NewReader(stdout)

for {
line, _, err := reader.ReadLine()
if err != nil {
delete(cmds, eventId)
delete(cmdProcesses, eventId)
delete(cmds, taskName)
delete(cmdProcesses, taskName)
if err == io.EOF {
wruntime.EventsEmit(a.ctx, eventId+"-finish")
wruntime.EventsEmit(a.ctx, "cmd_event", threadID)
return nil
}
return err
Expand All @@ -49,6 +60,6 @@ func (a *App) CmdInteractive(args []string, eventId string) error {
line = line2
}
strLine := string(line)
wruntime.EventsEmit(a.ctx, eventId+"-output", strLine)
wruntime.EventsEmit(a.ctx, "cmd_event", threadID, strLine)
}
}
25 changes: 13 additions & 12 deletions backend-golang/rwkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a *App) StartServer(python string, port int, host string, webui bool, rwkv
args = append(args, "--webgpu")
}
args = append(args, "--port", strconv.Itoa(port), "--host", host)
return Cmd(args...)
return "", a.CmdInteractive(args, "StartServer")
}

func (a *App) StartWebGPUServer(port int, host string) (string, error) {
Expand All @@ -55,7 +55,7 @@ func (a *App) StartWebGPUServer(port int, host string) (string, error) {
}
args := []string{execFile}
args = append(args, "--port", strconv.Itoa(port), "--ip", host)
return Cmd(args...)
return "", a.CmdInteractive(args, "StartWebGPUServer")
}

func (a *App) ConvertModel(python string, modelPath string, strategy string, outPath string) (string, error) {
Expand All @@ -70,7 +70,8 @@ func (a *App) ConvertModel(python string, modelPath string, strategy string, out
if err != nil {
return "", err
}
return Cmd(python, execFile, "--in", modelPath, "--out", outPath, "--strategy", strategy)
args := []string{python, execFile, "--in", modelPath, "--out", outPath, "--strategy", strategy}
return "", a.CmdInteractive(args, "ConvertModel")
}

func (a *App) ConvertSafetensors(modelPath string, outPath string) (string, error) {
Expand All @@ -88,7 +89,7 @@ func (a *App) ConvertSafetensors(modelPath string, outPath string) (string, erro
}
args := []string{execFile}
args = append(args, "--input", modelPath, "--output", outPath)
return Cmd(args...)
return "", a.CmdInteractive(args, "ConvertSafetensors")
}

func (a *App) ConvertSafetensorsWithPython(python string, modelPath string, outPath string) (string, error) {
Expand All @@ -103,7 +104,7 @@ func (a *App) ConvertSafetensorsWithPython(python string, modelPath string, outP
if err != nil {
return "", err
}
return Cmd(python, execFile, "--input", modelPath, "--output", outPath)
return "", a.CmdInteractive([]string{python, execFile, "--input", modelPath, "--output", outPath}, "ConvertSafetensorsWithPython")
}

func (a *App) ConvertGGML(python string, modelPath string, outPath string, Q51 bool) (string, error) {
Expand All @@ -122,7 +123,7 @@ func (a *App) ConvertGGML(python string, modelPath string, outPath string, Q51 b
if Q51 {
dataType = "Q5_1"
}
return Cmd(python, execFile, modelPath, outPath, dataType)
return "", a.CmdInteractive([]string{python, execFile, modelPath, outPath, dataType}, "ConvertGGML")
}

func (a *App) ConvertData(python string, input string, outputPrefix string, vocab string) (string, error) {
Expand Down Expand Up @@ -174,8 +175,8 @@ func (a *App) ConvertData(python string, input string, outputPrefix string, voca
return "", err
}

return Cmd(python, execFile, "--input", input, "--output-prefix", outputPrefix, "--vocab", vocab,
"--tokenizer-type", tokenizerType, "--dataset-impl", "mmap", "--append-eod")
return "", a.CmdInteractive([]string{python, execFile, "--input", input, "--output-prefix", outputPrefix, "--vocab", vocab,
"--tokenizer-type", tokenizerType, "--dataset-impl", "mmap", "--append-eod"}, "ConvertData")
}

func (a *App) MergeLora(python string, useGpu bool, loraAlpha int, baseModel string, loraPath string, outputPath string) (string, error) {
Expand All @@ -195,7 +196,7 @@ func (a *App) MergeLora(python string, useGpu bool, loraAlpha int, baseModel str
args = append(args, "--use-gpu")
}
args = append(args, strconv.Itoa(loraAlpha), baseModel, loraPath, outputPath)
return Cmd(args...)
return "", a.CmdInteractive(args, "MergeLora")
}

func (a *App) DepCheck(python string) error {
Expand Down Expand Up @@ -242,13 +243,13 @@ func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) {
if err != nil {
return "", err
}
return Cmd("install-py-dep.bat")
return "", a.CmdInteractive([]string{"./install-py-dep.bat"}, "InstallPyDep")
}

if cnMirror {
return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt", "-i", "https://mirrors.aliyun.com/pypi/simple")
return "", a.CmdInteractive([]string{python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt", "-i", "https://mirrors.aliyun.com/pypi/simple"}, "InstallPyDep")
} else {
return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt")
return "", a.CmdInteractive([]string{python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt"}, "InstallPyDep")
}
}

Expand Down
16 changes: 15 additions & 1 deletion backend-golang/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ import (
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
"syscall"
)

func IsDebugMode() bool {
info, ok := debug.ReadBuildInfo()
// In Makefile, "-ldflags '-s -w'" is added in wails build phase.
containLinkerFlags := strings.Contains(info.String(), "-ldflags")
isDebugMode := ok && !containLinkerFlags
return isDebugMode
}

func CmdHelper(hideWindow bool, args ...string) (*exec.Cmd, error) {
if runtime.GOOS != "windows" {
return nil, errors.New("unsupported OS")
Expand Down Expand Up @@ -70,7 +79,12 @@ func Cmd(args ...string) (string, error) {
if err != nil {
return "", err
}
exDir := filepath.Dir(ex) + "/../../../"
exDir := filepath.Dir(ex)
if IsDebugMode() {
exDir += "/../../../../../"
} else {
exDir += "/../../../"
}
cmd := exec.Command("osascript", "-e", `tell application "Terminal" to do script "`+"cd "+exDir+" && "+strings.Join(args, " ")+`"`)
err = cmd.Start()
if err != nil {
Expand Down
35 changes: 20 additions & 15 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import {
webDarkTheme,
webLightTheme,
} from '@fluentui/react-components'
import classNames from 'classnames'
import { observer } from 'mobx-react-lite'
import { useTranslation } from 'react-i18next'
import { Route, Routes, useLocation, useNavigate } from 'react-router'
import { useMediaQuery } from 'usehooks-ts'
import { BottomLogger } from './components/BottomLogger'
import { CustomToastContainer } from './components/CustomToastContainer'
import { DebugModeIndicator } from './components/DebugModeIndicator'
import { LazyImportComponent } from './components/LazyImportComponent'
Expand Down Expand Up @@ -127,21 +129,24 @@ const App: FC = observer(() => {
{bottomTabList}
</div>
)}
<div
className={
'box-border h-full w-full overflow-y-hidden py-2 pr-2' +
(useMobileStyle ? ' pl-2' : '')
}
>
<Routes>
{pages.map(({ path, element }, index) => (
<Route
key={`${path}-${index}`}
path={path}
element={<LazyImportComponent lazyChildren={element} />}
/>
))}
</Routes>
<div className={classNames('flex w-full flex-col')}>
<div
className={
'box-border h-full w-full overflow-y-hidden py-2 pr-2' +
(useMobileStyle ? ' pl-2' : '')
}
>
<Routes>
{pages.map(({ path, element }, index) => (
<Route
key={`${path}-${index}`}
path={path}
element={<LazyImportComponent lazyChildren={element} />}
/>
))}
</Routes>
</div>
{!isWeb && <BottomLogger />}
</div>
</div>
<CustomToastContainer />
Expand Down
Loading