67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
/*
|
|
* Copyright 2025 coze-dev Authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package impl
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/coze-dev/coze-studio/backend/infra/coderunner"
|
|
"github.com/coze-dev/coze-studio/backend/infra/coderunner/impl/direct"
|
|
"github.com/coze-dev/coze-studio/backend/infra/coderunner/impl/sandbox"
|
|
"github.com/coze-dev/coze-studio/backend/types/consts"
|
|
)
|
|
|
|
type Runner = coderunner.Runner
|
|
|
|
func New() Runner {
|
|
switch typ := os.Getenv(consts.CodeRunnerType); typ {
|
|
case "sandbox":
|
|
getAndSplit := func(key string) []string {
|
|
v := os.Getenv(key)
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
return strings.Split(v, ",")
|
|
}
|
|
config := &sandbox.Config{
|
|
AllowEnv: getAndSplit(consts.CodeRunnerAllowEnv),
|
|
AllowRead: getAndSplit(consts.CodeRunnerAllowRead),
|
|
AllowWrite: getAndSplit(consts.CodeRunnerAllowWrite),
|
|
AllowNet: getAndSplit(consts.CodeRunnerAllowNet),
|
|
AllowRun: getAndSplit(consts.CodeRunnerAllowRun),
|
|
AllowFFI: getAndSplit(consts.CodeRunnerAllowFFI),
|
|
NodeModulesDir: os.Getenv(consts.CodeRunnerNodeModulesDir),
|
|
TimeoutSeconds: 0,
|
|
MemoryLimitMB: 0,
|
|
}
|
|
if f, err := strconv.ParseFloat(os.Getenv(consts.CodeRunnerTimeoutSeconds), 64); err == nil {
|
|
config.TimeoutSeconds = f
|
|
} else {
|
|
config.TimeoutSeconds = 60.0
|
|
}
|
|
if mem, err := strconv.ParseInt(os.Getenv(consts.CodeRunnerMemoryLimitMB), 10, 64); err == nil {
|
|
config.MemoryLimitMB = mem
|
|
} else {
|
|
config.MemoryLimitMB = 100
|
|
}
|
|
return sandbox.NewRunner(config)
|
|
default:
|
|
return direct.NewRunner()
|
|
}
|
|
}
|