Handle native structured-format incompatibility and preserve failure evidence
This commit is contained in:
@@ -36,11 +36,14 @@ func messageText(data []byte) (string, error) {
|
||||
} `json:"info"`
|
||||
Parts []struct{ Type, Text string } `json:"parts"`
|
||||
}
|
||||
if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" || message.Info.Time.Completed == 0 {
|
||||
if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" {
|
||||
return "", fmt.Errorf("OpenCode returned no completed assistant result")
|
||||
}
|
||||
if len(message.Info.Error) > 0 && string(message.Info.Error) != "null" {
|
||||
return "", fmt.Errorf("OpenCode agent reported an execution error")
|
||||
return "", ExecutionError(message.Info.Error)
|
||||
}
|
||||
if message.Info.Time.Completed == 0 {
|
||||
return "", fmt.Errorf("OpenCode returned no completed assistant result")
|
||||
}
|
||||
if len(message.Info.Structured) > 0 && string(message.Info.Structured) != "null" && (message.Info.Finish == "stop" || message.Info.Finish == "tool-calls") {
|
||||
return string(message.Info.Structured), nil
|
||||
@@ -159,7 +162,17 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
}
|
||||
payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}}
|
||||
if len(request.Format) > 0 {
|
||||
payload["format"] = request.Format
|
||||
supported, err := c.supportsFormat(ctx, request.Format)
|
||||
if err != nil {
|
||||
var evidence []byte
|
||||
if c.FormatProbe != nil {
|
||||
evidence = []byte(c.FormatProbe.Response)
|
||||
}
|
||||
return evidence, fmt.Errorf("native structured-format compatibility check: %w", err)
|
||||
}
|
||||
if supported {
|
||||
payload["format"] = request.Format
|
||||
}
|
||||
}
|
||||
response, err := c.Native(ctx, "post", "/session/"+id+"/message", payload)
|
||||
if err != nil {
|
||||
@@ -176,11 +189,14 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
return json.Marshal(map[string]any{"data": map[string]string{"text": result}})
|
||||
case "wait":
|
||||
_, err := messageText(c.responses[id])
|
||||
return nil, err
|
||||
case "message":
|
||||
data, err := c.Native(ctx, "get", "/session/"+id+"/message", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return c.responses[id], err
|
||||
}
|
||||
return nil, nil
|
||||
case "message":
|
||||
data, err := c.Messages(ctx, id)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
var messages []struct {
|
||||
Info json.RawMessage `json:"info"`
|
||||
|
||||
Reference in New Issue
Block a user