|
| 1 | +package dun |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const HarnessPingPrompt = `Reply with JSON on one line: {"ok":true,"model":"<model name>"}. If unknown, use "unknown".` |
| 12 | + |
| 13 | +const defaultHarnessPingTimeout = 30 * time.Second |
| 14 | + |
| 15 | +// HarnessPingResult captures the outcome of a harness liveness ping. |
| 16 | +type HarnessPingResult struct { |
| 17 | + Live bool |
| 18 | + Model string |
| 19 | + Detail string |
| 20 | + Response string |
| 21 | +} |
| 22 | + |
| 23 | +// PingHarness sends a short liveness prompt to the harness and parses the response. |
| 24 | +func PingHarness(ctx context.Context, name string, config HarnessConfig) (HarnessPingResult, error) { |
| 25 | + if config.AutomationMode == "" { |
| 26 | + config.AutomationMode = AutomationAuto |
| 27 | + } |
| 28 | + if config.Timeout == 0 { |
| 29 | + config.Timeout = defaultHarnessPingTimeout |
| 30 | + } |
| 31 | + if config.Timeout > 0 { |
| 32 | + var cancel context.CancelFunc |
| 33 | + ctx, cancel = context.WithTimeout(ctx, config.Timeout) |
| 34 | + defer cancel() |
| 35 | + } |
| 36 | + |
| 37 | + harness, err := DefaultRegistry.Get(name, config) |
| 38 | + if err != nil { |
| 39 | + return HarnessPingResult{Live: false, Detail: err.Error()}, err |
| 40 | + } |
| 41 | + if !harness.SupportsAutomation(config.AutomationMode) { |
| 42 | + err := fmt.Errorf("harness %s does not support automation mode %s", name, config.AutomationMode) |
| 43 | + return HarnessPingResult{Live: false, Detail: err.Error()}, err |
| 44 | + } |
| 45 | + |
| 46 | + response, err := harness.Execute(ctx, HarnessPingPrompt) |
| 47 | + if err != nil { |
| 48 | + return HarnessPingResult{Live: false, Detail: err.Error()}, err |
| 49 | + } |
| 50 | + |
| 51 | + model, detail := parseHarnessPingResponse(response) |
| 52 | + return HarnessPingResult{Live: true, Model: model, Detail: detail, Response: response}, nil |
| 53 | +} |
| 54 | + |
| 55 | +func parseHarnessPingResponse(response string) (string, string) { |
| 56 | + candidate := extractJSON(response) |
| 57 | + if candidate != "" { |
| 58 | + var ping struct { |
| 59 | + OK bool `json:"ok"` |
| 60 | + Model string `json:"model"` |
| 61 | + } |
| 62 | + if err := json.Unmarshal([]byte(candidate), &ping); err == nil { |
| 63 | + model := strings.TrimSpace(ping.Model) |
| 64 | + if model == "" { |
| 65 | + model = "unknown" |
| 66 | + } |
| 67 | + return model, "" |
| 68 | + } |
| 69 | + } |
| 70 | + model := extractModelHint(response) |
| 71 | + if model != "" { |
| 72 | + return model, "non-json response" |
| 73 | + } |
| 74 | + return "", "unexpected response" |
| 75 | +} |
| 76 | + |
| 77 | +func extractJSON(response string) string { |
| 78 | + start := strings.Index(response, "{") |
| 79 | + end := strings.LastIndex(response, "}") |
| 80 | + if start == -1 || end == -1 || end <= start { |
| 81 | + return "" |
| 82 | + } |
| 83 | + return response[start : end+1] |
| 84 | +} |
| 85 | + |
| 86 | +func extractModelHint(response string) string { |
| 87 | + lower := strings.ToLower(response) |
| 88 | + for _, key := range []string{"model:", "model="} { |
| 89 | + if idx := strings.Index(lower, key); idx != -1 { |
| 90 | + fragment := response[idx+len(key):] |
| 91 | + fragment = strings.TrimSpace(fragment) |
| 92 | + if fragment == "" { |
| 93 | + return "" |
| 94 | + } |
| 95 | + fields := strings.Fields(fragment) |
| 96 | + if len(fields) == 0 { |
| 97 | + return "" |
| 98 | + } |
| 99 | + return strings.Trim(fields[0], "\"',.") |
| 100 | + } |
| 101 | + } |
| 102 | + return "" |
| 103 | +} |
0 commit comments