Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.0 KB
Raw Blame
package agent

import (
	"fmt"
	"io"
	"os"
	"strings"
	"time"
)

// Env contract between the OpenCode stdin tests and the helper process they
// re-execute. The helper stands in for a natively-installed `opencode.exe`
// (the Chocolatey shim in #6538 is a real PE binary, not a .cmd wrapper), so
// the backend's argv reaches CreateProcess unmediated.
const (
	opencodeStdinHelperEnv       = "MULTICA_OPENCODE_STDIN_HELPER"
	opencodeStdinHelperArgvFile  = "MULTICA_OPENCODE_STDIN_HELPER_ARGV_FILE"
	opencodeStdinHelperInFile    = "MULTICA_OPENCODE_STDIN_HELPER_STDIN_FILE"
	opencodeStdinHelperUsageOnly = "MULTICA_OPENCODE_STDIN_HELPER_USAGE_ONLY"
)

const (
	codeartsModelHelperEnv         = "CODEARTS_MODEL_TEST_HELPER"
	codeartsModelHelperArgvFile    = "CODEARTS_MODEL_TEST_HELPER_ARGV_FILE"
	codeartsModelHelperHangVerbose = "CODEARTS_MODEL_TEST_HELPER_HANG_VERBOSE"
)

func runFakeCodeArtsModelHelper() {
	args := os.Args[1:]
	if path := os.Getenv(codeartsModelHelperArgvFile); path != "" {
		if err := os.WriteFile(path, []byte(strings.Join(args, "\n")), 0o644); err != nil {
			fmt.Fprintf(os.Stderr, "helper: write CodeArts model argv: %v\n", err)
			os.Exit(1)
		}
	}
	for _, arg := range args {
		if arg == "--verbose" && os.Getenv(codeartsModelHelperHangVerbose) == "1" {
			time.Sleep(time.Minute)
		}
		if arg == "--version" {
			fmt.Println("CodeArts Agent 1.2.3")
			return
		}
	}
	if len(args) > 0 && strings.HasPrefix(args[0], "profile-") {
		fmt.Println("test/" + args[0])
		return
	}
	fmt.Println("huaweicloud-maas/deepseek-v3.2")
}

// runFakeOpencodeStdinHelper records the argv and stdin the backend handed this
// process, then emits a successful OpenCode event stream. It is dispatched from
// TestMain before the testing flag parser runs, which is what lets the process
// be invoked with OpenCode's own argv (`run --format json ...`) instead of
// -test.* flags.
func runFakeOpencodeStdinHelper() {
	argv := strings.Join(os.Args[1:], "\n")
	if err := os.WriteFile(os.Getenv(opencodeStdinHelperArgvFile), []byte(argv), 0o644); err != nil {
		fmt.Fprintf(os.Stderr, "helper: write argv: %v\n", err)
		os.Exit(1)
	}
	stdin, err := io.ReadAll(os.Stdin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "helper: read stdin: %v\n", err)
		os.Exit(1)
	}
	if err := os.WriteFile(os.Getenv(opencodeStdinHelperInFile), stdin, 0o644); err != nil {
		fmt.Fprintf(os.Stderr, "helper: write stdin: %v\n", err)
		os.Exit(1)
	}
	if len(os.Args) > 1 && os.Args[1] == "models" {
		fmt.Println("huaweicloud-maas/deepseek-v3.2")
		return
	}

	fmt.Println(`{"type":"step_start","timestamp":1,"sessionID":"ses_fake","part":{"type":"step-start"}}`)
	if os.Getenv(opencodeStdinHelperUsageOnly) == "1" {
		fmt.Println(`{"type":"step_finish","timestamp":2,"sessionID":"ses_fake","part":{"type":"step-finish","reason":"stop","tokens":{"input":0,"output":0,"reasoning":20}}}`)
		return
	}
	fmt.Println(`{"type":"text","timestamp":2,"sessionID":"ses_fake","part":{"type":"text","text":"ok"}}`)
	fmt.Println(`{"type":"step_finish","timestamp":3,"sessionID":"ses_fake","part":{"type":"step-finish"}}`)
}