Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): 拆出 applyDeepSeekCredential 并合并 Pi/DeepSeek 协议映射 (#345)

fx-kurumia2b3577948ba
diff --git a/internal/runtime/agent_cli.go b/internal/runtime/agent_cli.go
index 4827f119..d93f322c 100644
--- a/internal/runtime/agent_cli.go
+++ b/internal/runtime/agent_cli.go
@@ -431,7 +431,7 @@ func ApplyAgentCLICredential(kind string, cred ProviderCredential, options Agent
env["KIMI_CODE_HOME"] = homeEnv + "/.kimi-code"
env["KIMI_DISABLE_TELEMETRY"] = "1"
case AgentCLIPi:
- api := map[string]string{AgentCLIProtocolChat: "openai-completions", AgentCLIProtocolResponses: "openai-responses", AgentCLIProtocolMessages: "anthropic-messages"}[protocol]
+ api := agentCLIPiAPI(protocol)
models, _ := json.MarshalIndent(map[string]any{"providers": map[string]any{"agentflow": map[string]any{
"baseUrl": baseURL, "api": api, "apiKey": "$" + agentCLIKeyEnv,
"models": []map[string]any{{"id": model, "reasoning": true}},
@@ -442,44 +442,7 @@ func ApplyAgentCLICredential(kind string, cred ProviderCredential, options Agent
env[agentCLIKeyEnv] = key
env["PI_CODING_AGENT_DIR"] = homeEnv + "/.pi/agent"
case AgentCLIDeepSeek:
- patch := []any{}
- if baseURL == "" {
- env["DEEPSEEK_API_KEY"] = key
- if model != "" {
- patch = append(patch, map[string]any{"id": "agent-default-model", "config": map[string]any{"provider": "deepseek-official", "model": model}})
- }
- } else {
- api := map[string]string{AgentCLIProtocolChat: "openai-completions", AgentCLIProtocolResponses: "openai-responses", AgentCLIProtocolMessages: "anthropic-messages"}[protocol]
- env[agentCLIKeyEnv] = key
- patch = []any{
- map[string]any{"id": "llm-pi-ai", "config": map[string]any{"providers": map[string]any{"agentflow": map[string]any{
- "displayName": "AgentFlow", "apiKeyEnv": agentCLIKeyEnv, "api": api, "baseURL": baseURL,
- "models": []map[string]any{{"id": model}},
- }}}},
- map[string]any{"id": "agent-default-model", "config": map[string]any{"provider": "agentflow", "model": model}},
- }
- }
- data, _ := json.MarshalIndent(patch, "", " ")
- if err := writeAgentCLIFile(home, ".dsh/agentflow-model.json", data); err != nil {
- return nil, nil, err
- }
- env["DSH_HOME"] = homeEnv + "/.dsh"
- // Launcher flags precede the profile's own arguments: dsh --profile <name> --patch … app-args.
- if len(argv) < 3 {
- return nil, nil, errors.New("invalid DeepSeek Harness invocation")
- }
- patches := []string{"--patch", homeEnv + "/.dsh/agentflow-model.json"}
- root, err := openAgentCLIHome(home)
- if err != nil {
- return nil, nil, err
- }
- _, err = root.Stat(filepath.Join(".dsh", "agentflow-mcp.json"))
- root.Close()
- if err == nil {
- patches = append(patches, "--patch", homeEnv+"/.dsh/agentflow-mcp.json")
- }
- head := append([]string{}, argv[:3]...)
- argv = append(append(head, patches...), argv[3:]...)
+ return applyDeepSeekCredential(home, homeEnv, key, baseURL, protocol, model, argv)
case AgentCLIZCode:
return applyZCodeCredential(home, homeEnv, key, baseURL, protocol, model, argv)
default:
@@ -488,6 +451,57 @@ func ApplyAgentCLICredential(kind string, cred ProviderCredential, options Agent
return argv, env, nil
}
+// agentCLIPiAPI maps a wire protocol to the pi-ai api name shared by Pi and
+// DeepSeek Harness provider configs.
+func agentCLIPiAPI(protocol string) string {
+ return map[string]string{AgentCLIProtocolChat: "openai-completions", AgentCLIProtocolResponses: "openai-responses", AgentCLIProtocolMessages: "anthropic-messages"}[protocol]
+}
+
+// applyDeepSeekCredential writes the DeepSeek Harness model patch and puts the
+// launcher --patch flags ahead of the profile's own arguments.
+func applyDeepSeekCredential(home AgentCLIHome, homeEnv, key, baseURL, protocol, model string, argv []string) ([]string, map[string]string, error) {
+ env := map[string]string{}
+ patch := []any{}
+ if baseURL == "" {
+ env["DEEPSEEK_API_KEY"] = key
+ if model != "" {
+ patch = append(patch, map[string]any{"id": "agent-default-model", "config": map[string]any{"provider": "deepseek-official", "model": model}})
+ }
+ } else {
+ api := agentCLIPiAPI(protocol)
+ env[agentCLIKeyEnv] = key
+ patch = []any{
+ map[string]any{"id": "llm-pi-ai", "config": map[string]any{"providers": map[string]any{"agentflow": map[string]any{
+ "displayName": "AgentFlow", "apiKeyEnv": agentCLIKeyEnv, "api": api, "baseURL": baseURL,
+ "models": []map[string]any{{"id": model}},
+ }}}},
+ map[string]any{"id": "agent-default-model", "config": map[string]any{"provider": "agentflow", "model": model}},
+ }
+ }
+ data, _ := json.MarshalIndent(patch, "", " ")
+ if err := writeAgentCLIFile(home, ".dsh/agentflow-model.json", data); err != nil {
+ return nil, nil, err
+ }
+ env["DSH_HOME"] = homeEnv + "/.dsh"
+ // Launcher flags precede the profile's own arguments: dsh --profile <name> --patch … app-args.
+ if len(argv) < 3 {
+ return nil, nil, errors.New("invalid DeepSeek Harness invocation")
+ }
+ patches := []string{"--patch", homeEnv + "/.dsh/agentflow-model.json"}
+ root, err := openAgentCLIHome(home)
+ if err != nil {
+ return nil, nil, err
+ }
+ _, err = root.Stat(filepath.Join(".dsh", "agentflow-mcp.json"))
+ root.Close()
+ if err == nil {
+ patches = append(patches, "--patch", homeEnv+"/.dsh/agentflow-mcp.json")
+ }
+ head := append([]string{}, argv[:3]...)
+ argv = append(append(head, patches...), argv[3:]...)
+ return argv, env, nil
+}
+
// insertBeforeFlag inserts values ahead of the last occurrence of flag, or
// appends them when flag is absent.
func insertBeforeFlag(argv []string, flag string, values ...string) []string {
diff --git a/internal/runtime/agent_cli_deepseek_test.go b/internal/runtime/agent_cli_deepseek_test.go
new file mode 100644
index 00000000..c119be6e
--- /dev/null
+++ b/internal/runtime/agent_cli_deepseek_test.go
@@ -0,0 +1,71 @@
+package runtime
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "slices"
+ "testing"
+)
+
+func TestAgentCLIPiAPI(t *testing.T) {
+ for protocol, want := range map[string]string{
+ AgentCLIProtocolChat: "openai-completions",
+ AgentCLIProtocolResponses: "openai-responses",
+ AgentCLIProtocolMessages: "anthropic-messages",
+ "unknown": "",
+ } {
+ if got := agentCLIPiAPI(protocol); got != want {
+ t.Fatalf("agentCLIPiAPI(%q) = %q, want %q", protocol, got, want)
+ }
+ }
+}
+
+func TestApplyDeepSeekCredentialCustomBaseURL(t *testing.T) {
+ home := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(home, ".dsh"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(home, ".dsh", "agentflow-mcp.json"), []byte("[]"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ argv, env, err := ApplyAgentCLICredential(AgentCLIDeepSeek,
+ ProviderCredential{APIKey: "k", BaseURL: "https://gw.example/", Protocol: AgentCLIProtocolMessages},
+ AgentCLIOptions{Model: "m"}, HostAgentCLIHome(home), "/h",
+ []string{"dsh", "--profile", "headless", "--json", "x"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ wantArgv := []string{"dsh", "--profile", "headless", "--patch", "/h/.dsh/agentflow-model.json", "--patch", "/h/.dsh/agentflow-mcp.json", "--json", "x"}
+ if !slices.Equal(argv, wantArgv) {
+ t.Fatalf("argv = %q, want %q", argv, wantArgv)
+ }
+ if env[agentCLIKeyEnv] != "k" || env["DSH_HOME"] != "/h/.dsh" || env["DEEPSEEK_API_KEY"] != "" {
+ t.Fatalf("env = %v", env)
+ }
+ data, err := os.ReadFile(filepath.Join(home, ".dsh", "agentflow-model.json"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ var patch []struct {
+ ID string `json:"id"`
+ Config map[string]any `json:"config"`
+ }
+ if err := json.Unmarshal(data, &patch); err != nil || len(patch) != 2 {
+ t.Fatalf("patch: %s (%v)", data, err)
+ }
+ provider := patch[0].Config["providers"].(map[string]any)["agentflow"].(map[string]any)
+ if patch[0].ID != "llm-pi-ai" || provider["api"] != "anthropic-messages" || provider["baseURL"] != "https://gw.example" {
+ t.Fatalf("provider patch: %s", data)
+ }
+ if patch[1].ID != "agent-default-model" || patch[1].Config["provider"] != "agentflow" || patch[1].Config["model"] != "m" {
+ t.Fatalf("default model patch: %s", data)
+ }
+}
+
+func TestApplyDeepSeekCredentialRejectsShortArgv(t *testing.T) {
+ _, _, err := ApplyAgentCLICredential(AgentCLIDeepSeek, ProviderCredential{APIKey: "k"}, AgentCLIOptions{Model: "m"}, HostAgentCLIHome(t.TempDir()), "/h", []string{"dsh"})
+ if err == nil || err.Error() != "invalid DeepSeek Harness invocation" {
+ t.Fatalf("err = %v", err)
+ }
+}