| 1 |
package actwire |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"regexp" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/clnkr-ai/clnkr" |
| 13 |
) |
| 14 |
|
| 15 |
type wireEnvelope struct { |
| 16 |
Turn wireTurn `json:"turn"` |
| 17 |
} |
| 18 |
|
| 19 |
type wireTurn struct { |
| 20 |
Type string `json:"type"` |
| 21 |
Bash *wireBash `json:"bash"` |
| 22 |
Question any `json:"question"` |
| 23 |
Summary any `json:"summary"` |
| 24 |
Verification *wireVerification `json:"verification,omitempty"` |
| 25 |
KnownRisks any `json:"known_risks,omitempty"` |
| 26 |
Reasoning any `json:"reasoning"` |
| 27 |
} |
| 28 |
|
| 29 |
type wireBash struct { |
| 30 |
Commands []wireCommand `json:"commands"` |
| 31 |
} |
| 32 |
|
| 33 |
type wireCommand struct { |
| 34 |
Command string `json:"command"` |
| 35 |
Workdir *string `json:"workdir"` |
| 36 |
} |
| 37 |
|
| 38 |
type wireVerification struct { |
| 39 |
Status string `json:"status"` |
| 40 |
Checks []wireCheck `json:"checks"` |
| 41 |
} |
| 42 |
|
| 43 |
type wireCheck struct { |
| 44 |
Command string `json:"command"` |
| 45 |
Outcome string `json:"outcome"` |
| 46 |
Evidence string `json:"evidence"` |
| 47 |
} |
| 48 |
|
| 49 |
func RequestSchema() map[string]any { |
| 50 |
return structuredOutputSchema(true) |
| 51 |
} |
| 52 |
|
| 53 |
// UnattendedRequestSchema accepts act and done turns, but not clarify. |
| 54 |
func UnattendedRequestSchema() map[string]any { |
| 55 |
return structuredOutputSchema(false) |
| 56 |
} |
| 57 |
|
| 58 |
func FinalTurnSchema() map[string]any { |
| 59 |
return finalTurnSchema(false) |
| 60 |
} |
| 61 |
|
| 62 |
func DoneOnlySchema() map[string]any { |
| 63 |
return finalTurnSchema(true) |
| 64 |
} |
| 65 |
|
| 66 |
func structuredOutputSchema(allowClarify bool) map[string]any { |
| 67 |
choices := []any{actTurnSchema(), doneTurnSchema()} |
| 68 |
if allowClarify { |
| 69 |
choices = []any{actTurnSchema(), clarifyTurnSchema(), doneTurnSchema()} |
| 70 |
} |
| 71 |
return map[string]any{ |
| 72 |
"type": "object", |
| 73 |
"additionalProperties": false, |
| 74 |
"properties": map[string]any{ |
| 75 |
"turn": map[string]any{ |
| 76 |
"anyOf": choices, |
| 77 |
}, |
| 78 |
}, |
| 79 |
"required": []string{"turn"}, |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func finalTurnSchema(doneOnly bool) map[string]any { |
| 84 |
choices := []any{doneTurnSchema()} |
| 85 |
if !doneOnly { |
| 86 |
choices = append([]any{clarifyTurnSchema()}, choices...) |
| 87 |
} |
| 88 |
return map[string]any{ |
| 89 |
"type": "object", |
| 90 |
"additionalProperties": false, |
| 91 |
"properties": map[string]any{ |
| 92 |
"turn": map[string]any{ |
| 93 |
"anyOf": choices, |
| 94 |
}, |
| 95 |
}, |
| 96 |
"required": []string{"turn"}, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func actTurnSchema() map[string]any { |
| 101 |
commands := map[string]any{ |
| 102 |
"type": "array", |
| 103 |
"minItems": 1, |
| 104 |
"items": map[string]any{ |
| 105 |
"type": "object", |
| 106 |
"additionalProperties": false, |
| 107 |
"properties": map[string]any{ |
| 108 |
"command": map[string]any{"type": "string"}, |
| 109 |
"workdir": nullableStringSchema(), |
| 110 |
}, |
| 111 |
"required": []string{"command", "workdir"}, |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
return map[string]any{ |
| 116 |
"type": "object", |
| 117 |
"additionalProperties": false, |
| 118 |
"properties": map[string]any{ |
| 119 |
"type": map[string]any{ |
| 120 |
"type": "string", |
| 121 |
"const": "act", |
| 122 |
}, |
| 123 |
"bash": map[string]any{ |
| 124 |
"type": "object", |
| 125 |
"additionalProperties": false, |
| 126 |
"properties": map[string]any{ |
| 127 |
"commands": commands, |
| 128 |
}, |
| 129 |
"required": []string{"commands"}, |
| 130 |
}, |
| 131 |
"question": map[string]any{"type": "null"}, |
| 132 |
"summary": map[string]any{"type": "null"}, |
| 133 |
"reasoning": nullableStringSchema(), |
| 134 |
}, |
| 135 |
"required": []string{"type", "bash", "question", "summary", "reasoning"}, |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func clarifyTurnSchema() map[string]any { |
| 140 |
return map[string]any{ |
| 141 |
"type": "object", |
| 142 |
"additionalProperties": false, |
| 143 |
"properties": map[string]any{ |
| 144 |
"type": map[string]any{ |
| 145 |
"type": "string", |
| 146 |
"const": "clarify", |
| 147 |
}, |
| 148 |
"bash": map[string]any{"type": "null"}, |
| 149 |
"question": map[string]any{"type": "string"}, |
| 150 |
"summary": map[string]any{"type": "null"}, |
| 151 |
"reasoning": nullableStringSchema(), |
| 152 |
}, |
| 153 |
"required": []string{"type", "bash", "question", "summary", "reasoning"}, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func doneTurnSchema() map[string]any { |
| 158 |
checkSchema := map[string]any{ |
| 159 |
"type": "object", |
| 160 |
"additionalProperties": false, |
| 161 |
"properties": map[string]any{ |
| 162 |
"command": map[string]any{"type": "string"}, |
| 163 |
"outcome": map[string]any{"type": "string"}, |
| 164 |
"evidence": map[string]any{"type": "string"}, |
| 165 |
}, |
| 166 |
"required": []string{"command", "outcome", "evidence"}, |
| 167 |
} |
| 168 |
return map[string]any{ |
| 169 |
"type": "object", |
| 170 |
"additionalProperties": false, |
| 171 |
"properties": map[string]any{ |
| 172 |
"type": map[string]any{ |
| 173 |
"type": "string", |
| 174 |
"const": "done", |
| 175 |
}, |
| 176 |
"bash": map[string]any{"type": "null"}, |
| 177 |
"question": map[string]any{"type": "null"}, |
| 178 |
"summary": map[string]any{"type": "string"}, |
| 179 |
"verification": map[string]any{ |
| 180 |
"type": "object", |
| 181 |
"additionalProperties": false, |
| 182 |
"properties": map[string]any{ |
| 183 |
"status": map[string]any{ |
| 184 |
"type": "string", |
| 185 |
"enum": []string{"verified", "partially_verified", "not_verified"}, |
| 186 |
}, |
| 187 |
"checks": map[string]any{ |
| 188 |
"type": "array", |
| 189 |
"items": checkSchema, |
| 190 |
}, |
| 191 |
}, |
| 192 |
"required": []string{"status", "checks"}, |
| 193 |
}, |
| 194 |
"known_risks": map[string]any{ |
| 195 |
"type": "array", |
| 196 |
"items": map[string]any{"type": "string"}, |
| 197 |
}, |
| 198 |
"reasoning": nullableStringSchema(), |
| 199 |
}, |
| 200 |
"required": []string{ |
| 201 |
"type", |
| 202 |
"bash", |
| 203 |
"question", |
| 204 |
"summary", |
| 205 |
"verification", |
| 206 |
"known_risks", |
| 207 |
"reasoning", |
| 208 |
}, |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func nullableStringSchema() map[string]any { |
| 213 |
return map[string]any{ |
| 214 |
"anyOf": []any{ |
| 215 |
map[string]any{"type": "string"}, |
| 216 |
map[string]any{"type": "null"}, |
| 217 |
}, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func NormalizeMessagesForProvider(messages []clnkr.Message) []clnkr.Message { |
| 222 |
normalized := make([]clnkr.Message, len(messages)) |
| 223 |
for i, msg := range messages { |
| 224 |
normalized[i] = msg |
| 225 |
if msg.Role != "assistant" { |
| 226 |
continue |
| 227 |
} |
| 228 |
if turn, err := ParseProviderTurn(msg.Content); err == nil { |
| 229 |
if wrapped, err := providerJSON(turn); err == nil { |
| 230 |
normalized[i].Content = wrapped |
| 231 |
} |
| 232 |
continue |
| 233 |
} |
| 234 |
if turn, err := clnkr.ParseTurn(msg.Content); err == nil { |
| 235 |
if wrapped, err := providerJSON(turn); err == nil { |
| 236 |
normalized[i].Content = wrapped |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return normalized |
| 241 |
} |
| 242 |
|
| 243 |
func ParseProviderTurn(raw string) (clnkr.Turn, error) { |
| 244 |
innerRaw, err := extractWrappedTurn(raw) |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
if err := validateWrappedTurnShape(innerRaw); err != nil { |
| 249 |
return nil, err |
| 250 |
} |
| 251 |
|
| 252 |
var payload wireTurnPayload |
| 253 |
dec := json.NewDecoder(strings.NewReader(innerRaw)) |
| 254 |
dec.DisallowUnknownFields() |
| 255 |
if err := dec.Decode(&payload); err != nil { |
| 256 |
return nil, fmt.Errorf("%w: %v", clnkr.ErrInvalidJSON, err) |
| 257 |
} |
| 258 |
if err := ensureSingleJSONObject(dec); err != nil { |
| 259 |
return nil, fmt.Errorf("%w: %v", clnkr.ErrInvalidJSON, err) |
| 260 |
} |
| 261 |
payload.Question = normalizeHumanText(payload.Question) |
| 262 |
payload.Summary = normalizeHumanText(payload.Summary) |
| 263 |
payload.Reasoning = normalizeHumanText(payload.Reasoning) |
| 264 |
return turnFromProviderPayload(payload) |
| 265 |
} |
| 266 |
|
| 267 |
type wireTurnPayload struct { |
| 268 |
Type string `json:"type"` |
| 269 |
Bash *wireBash `json:"bash"` |
| 270 |
Question string `json:"question"` |
| 271 |
Summary string `json:"summary"` |
| 272 |
Verification *wireVerification `json:"verification"` |
| 273 |
KnownRisks []string `json:"known_risks"` |
| 274 |
Reasoning string `json:"reasoning"` |
| 275 |
} |
| 276 |
|
| 277 |
func extractWrappedTurn(raw string) (string, error) { |
| 278 |
trimmed := strings.TrimSpace(raw) |
| 279 |
if trimmed == "" { |
| 280 |
return "", fmt.Errorf("%w: empty response", clnkr.ErrInvalidJSON) |
| 281 |
} |
| 282 |
|
| 283 |
var envFields map[string]json.RawMessage |
| 284 |
dec := json.NewDecoder(strings.NewReader(trimmed)) |
| 285 |
if err := dec.Decode(&envFields); err != nil { |
| 286 |
return "", fmt.Errorf("%w: %v", clnkr.ErrInvalidJSON, err) |
| 287 |
} |
| 288 |
if err := ensureSingleJSONObject(dec); err != nil { |
| 289 |
return "", fmt.Errorf("%w: %v", clnkr.ErrInvalidJSON, err) |
| 290 |
} |
| 291 |
|
| 292 |
turnRaw, ok := envFields["turn"] |
| 293 |
if !ok { |
| 294 |
return "", fmt.Errorf( |
| 295 |
"%w: missing required structured output field %q", |
| 296 |
clnkr.ErrInvalidJSON, |
| 297 |
"turn", |
| 298 |
) |
| 299 |
} |
| 300 |
if len(envFields) != 1 { |
| 301 |
for field := range envFields { |
| 302 |
if field != "turn" { |
| 303 |
return "", fmt.Errorf( |
| 304 |
"%w: unknown structured output field %q", |
| 305 |
clnkr.ErrInvalidJSON, |
| 306 |
field, |
| 307 |
) |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
var turnFields map[string]json.RawMessage |
| 313 |
if err := json.Unmarshal(turnRaw, &turnFields); err != nil { |
| 314 |
return "", fmt.Errorf( |
| 315 |
"%w: structured output field %q must be object", |
| 316 |
clnkr.ErrInvalidJSON, |
| 317 |
"turn", |
| 318 |
) |
| 319 |
} |
| 320 |
if turnFields == nil { |
| 321 |
return "", fmt.Errorf( |
| 322 |
"%w: structured output field %q must be object", |
| 323 |
clnkr.ErrInvalidJSON, |
| 324 |
"turn", |
| 325 |
) |
| 326 |
} |
| 327 |
return string(turnRaw), nil |
| 328 |
} |
| 329 |
|
| 330 |
func validateWrappedTurnShape(raw string) error { |
| 331 |
var fields map[string]json.RawMessage |
| 332 |
if err := json.Unmarshal([]byte(raw), &fields); err != nil { |
| 333 |
return fmt.Errorf("%w: %v", clnkr.ErrInvalidJSON, err) |
| 334 |
} |
| 335 |
turnType, err := stringField(fields, "type", "type") |
| 336 |
if err != nil { |
| 337 |
return err |
| 338 |
} |
| 339 |
|
| 340 |
switch turnType { |
| 341 |
case "act": |
| 342 |
return validateActShape(fields) |
| 343 |
case "clarify": |
| 344 |
if err := rejectNonNullField( |
| 345 |
fields, |
| 346 |
"bash", |
| 347 |
"clarify turn only allows bash when it is null or omitted", |
| 348 |
); err != nil { |
| 349 |
return err |
| 350 |
} |
| 351 |
if err := rejectNonNullField( |
| 352 |
fields, |
| 353 |
"summary", |
| 354 |
"clarify turn only allows summary when it is null or omitted", |
| 355 |
); err != nil { |
| 356 |
return err |
| 357 |
} |
| 358 |
case "done": |
| 359 |
return validateDoneShape(fields) |
| 360 |
} |
| 361 |
return nil |
| 362 |
} |
| 363 |
|
| 364 |
func validateActShape(fields map[string]json.RawMessage) error { |
| 365 |
bashFields, err := objectField(fields, "bash", "bash") |
| 366 |
if err != nil { |
| 367 |
return err |
| 368 |
} |
| 369 |
commands, err := rawArrayField(bashFields, "commands", "bash.commands") |
| 370 |
if err != nil { |
| 371 |
return err |
| 372 |
} |
| 373 |
if len(commands) == 0 { |
| 374 |
return fmt.Errorf( |
| 375 |
"%w: structured output field %q must contain at least 1 item", |
| 376 |
clnkr.ErrInvalidJSON, |
| 377 |
"bash.commands", |
| 378 |
) |
| 379 |
} |
| 380 |
for i, rawCommand := range commands { |
| 381 |
if err := validateCommandShape(rawCommand, i); err != nil { |
| 382 |
return err |
| 383 |
} |
| 384 |
} |
| 385 |
if err := rejectNonNullField( |
| 386 |
fields, |
| 387 |
"question", |
| 388 |
"act turn only allows question when it is null or omitted", |
| 389 |
); err != nil { |
| 390 |
return err |
| 391 |
} |
| 392 |
return rejectNonNullField( |
| 393 |
fields, |
| 394 |
"summary", |
| 395 |
"act turn only allows summary when it is null or omitted", |
| 396 |
) |
| 397 |
} |
| 398 |
|
| 399 |
func validateCommandShape(raw json.RawMessage, index int) error { |
| 400 |
field := fmt.Sprintf("bash.commands[%d]", index) |
| 401 |
commandFields, err := objectValue(raw, field) |
| 402 |
if err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
for _, name := range []string{"command", "workdir"} { |
| 406 |
if _, err := requiredField( |
| 407 |
commandFields, |
| 408 |
name, |
| 409 |
fmt.Sprintf("%s.%s", field, name), |
| 410 |
); err != nil { |
| 411 |
return err |
| 412 |
} |
| 413 |
} |
| 414 |
if _, err := stringField(commandFields, "command", field+".command"); err != nil { |
| 415 |
return err |
| 416 |
} |
| 417 |
return nullableStringField(commandFields, "workdir", field+".workdir") |
| 418 |
} |
| 419 |
|
| 420 |
func validateDoneShape(fields map[string]json.RawMessage) error { |
| 421 |
if err := rejectNonNullField( |
| 422 |
fields, |
| 423 |
"bash", |
| 424 |
"done turn only allows bash when it is null or omitted", |
| 425 |
); err != nil { |
| 426 |
return err |
| 427 |
} |
| 428 |
if err := rejectNonNullField( |
| 429 |
fields, |
| 430 |
"question", |
| 431 |
"done turn only allows question when it is null or omitted", |
| 432 |
); err != nil { |
| 433 |
return err |
| 434 |
} |
| 435 |
for _, field := range []string{"summary", "verification", "known_risks"} { |
| 436 |
if _, err := requiredField(fields, field, field); err != nil { |
| 437 |
return err |
| 438 |
} |
| 439 |
} |
| 440 |
if err := stringArrayField(fields, "known_risks", "known_risks"); err != nil { |
| 441 |
return err |
| 442 |
} |
| 443 |
verificationFields, err := objectField(fields, "verification", "verification") |
| 444 |
if err != nil { |
| 445 |
return err |
| 446 |
} |
| 447 |
rawChecks, err := requiredField(verificationFields, "checks", "verification.checks") |
| 448 |
if err != nil { |
| 449 |
return err |
| 450 |
} |
| 451 |
if string(rawChecks) == "null" { |
| 452 |
return fmt.Errorf( |
| 453 |
"%w: structured output field %q must be array", |
| 454 |
clnkr.ErrInvalidJSON, |
| 455 |
"verification.checks", |
| 456 |
) |
| 457 |
} |
| 458 |
return nil |
| 459 |
} |
| 460 |
|
| 461 |
func requiredField(fields map[string]json.RawMessage, key, label string) (json.RawMessage, error) { |
| 462 |
raw, ok := fields[key] |
| 463 |
if !ok { |
| 464 |
return nil, fmt.Errorf( |
| 465 |
"%w: missing required structured output field %q", |
| 466 |
clnkr.ErrInvalidJSON, |
| 467 |
label, |
| 468 |
) |
| 469 |
} |
| 470 |
return raw, nil |
| 471 |
} |
| 472 |
|
| 473 |
func stringField(fields map[string]json.RawMessage, key, label string) (string, error) { |
| 474 |
raw, err := requiredField(fields, key, label) |
| 475 |
if err != nil { |
| 476 |
return "", err |
| 477 |
} |
| 478 |
var value string |
| 479 |
if err := json.Unmarshal(raw, &value); err != nil { |
| 480 |
return "", fmt.Errorf( |
| 481 |
"%w: structured output field %q must be string", |
| 482 |
clnkr.ErrInvalidJSON, |
| 483 |
label, |
| 484 |
) |
| 485 |
} |
| 486 |
return value, nil |
| 487 |
} |
| 488 |
|
| 489 |
func nullableStringField(fields map[string]json.RawMessage, key, label string) error { |
| 490 |
raw, err := requiredField(fields, key, label) |
| 491 |
if err != nil { |
| 492 |
return err |
| 493 |
} |
| 494 |
if string(raw) == "null" { |
| 495 |
return nil |
| 496 |
} |
| 497 |
var value string |
| 498 |
if err := json.Unmarshal(raw, &value); err != nil { |
| 499 |
return fmt.Errorf( |
| 500 |
"%w: structured output field %q must be string or null", |
| 501 |
clnkr.ErrInvalidJSON, |
| 502 |
label, |
| 503 |
) |
| 504 |
} |
| 505 |
return nil |
| 506 |
} |
| 507 |
|
| 508 |
func objectField( |
| 509 |
fields map[string]json.RawMessage, |
| 510 |
key, label string, |
| 511 |
) (map[string]json.RawMessage, error) { |
| 512 |
raw, err := requiredField(fields, key, label) |
| 513 |
if err != nil { |
| 514 |
return nil, err |
| 515 |
} |
| 516 |
return objectValue(raw, label) |
| 517 |
} |
| 518 |
|
| 519 |
func objectValue(raw json.RawMessage, field string) (map[string]json.RawMessage, error) { |
| 520 |
var fields map[string]json.RawMessage |
| 521 |
if err := json.Unmarshal(raw, &fields); err != nil { |
| 522 |
return nil, fmt.Errorf( |
| 523 |
"%w: structured output field %q must be object", |
| 524 |
clnkr.ErrInvalidJSON, |
| 525 |
field, |
| 526 |
) |
| 527 |
} |
| 528 |
if fields == nil { |
| 529 |
return nil, fmt.Errorf( |
| 530 |
"%w: structured output field %q must be object", |
| 531 |
clnkr.ErrInvalidJSON, |
| 532 |
field, |
| 533 |
) |
| 534 |
} |
| 535 |
return fields, nil |
| 536 |
} |
| 537 |
|
| 538 |
func rawArrayField( |
| 539 |
fields map[string]json.RawMessage, |
| 540 |
key, label string, |
| 541 |
) ([]json.RawMessage, error) { |
| 542 |
raw, err := requiredField(fields, key, label) |
| 543 |
if err != nil { |
| 544 |
return nil, err |
| 545 |
} |
| 546 |
var values []json.RawMessage |
| 547 |
if err := json.Unmarshal(raw, &values); err != nil { |
| 548 |
return nil, fmt.Errorf( |
| 549 |
"%w: structured output field %q must be array", |
| 550 |
clnkr.ErrInvalidJSON, |
| 551 |
label, |
| 552 |
) |
| 553 |
} |
| 554 |
return values, nil |
| 555 |
} |
| 556 |
|
| 557 |
func stringArrayField(fields map[string]json.RawMessage, key, label string) error { |
| 558 |
raw, err := requiredField(fields, key, label) |
| 559 |
if err != nil { |
| 560 |
return err |
| 561 |
} |
| 562 |
if string(raw) == "null" { |
| 563 |
return fmt.Errorf( |
| 564 |
"%w: structured output field %q must be array of strings", |
| 565 |
clnkr.ErrInvalidJSON, |
| 566 |
label, |
| 567 |
) |
| 568 |
} |
| 569 |
var values []string |
| 570 |
if err := json.Unmarshal(raw, &values); err != nil { |
| 571 |
return fmt.Errorf( |
| 572 |
"%w: structured output field %q must be array of strings", |
| 573 |
clnkr.ErrInvalidJSON, |
| 574 |
label, |
| 575 |
) |
| 576 |
} |
| 577 |
return nil |
| 578 |
} |
| 579 |
|
| 580 |
func rejectNonNullField(fields map[string]json.RawMessage, field, detail string) error { |
| 581 |
raw, ok := fields[field] |
| 582 |
if ok && string(raw) != "null" { |
| 583 |
return fmt.Errorf("%w: %s", clnkr.ErrInvalidJSON, detail) |
| 584 |
} |
| 585 |
return nil |
| 586 |
} |
| 587 |
|
| 588 |
func turnFromProviderPayload(payload wireTurnPayload) (clnkr.Turn, error) { |
| 589 |
switch payload.Type { |
| 590 |
case "act": |
| 591 |
if payload.Bash == nil || len(payload.Bash.Commands) == 0 { |
| 592 |
return nil, clnkr.ErrMissingCommand |
| 593 |
} |
| 594 |
actions := make([]clnkr.BashAction, 0, len(payload.Bash.Commands)) |
| 595 |
for _, command := range payload.Bash.Commands { |
| 596 |
if strings.TrimSpace(command.Command) == "" { |
| 597 |
return nil, clnkr.ErrMissingCommand |
| 598 |
} |
| 599 |
action := clnkr.BashAction{Command: command.Command} |
| 600 |
if command.Workdir != nil { |
| 601 |
action.Workdir = *command.Workdir |
| 602 |
} |
| 603 |
actions = append(actions, action) |
| 604 |
} |
| 605 |
return &clnkr.ActTurn{ |
| 606 |
Bash: clnkr.BashBatch{Commands: actions}, |
| 607 |
Reasoning: payload.Reasoning, |
| 608 |
}, nil |
| 609 |
case "clarify": |
| 610 |
if payload.Question == "" { |
| 611 |
return nil, clnkr.ErrEmptyClarify |
| 612 |
} |
| 613 |
return &clnkr.ClarifyTurn{Question: payload.Question, Reasoning: payload.Reasoning}, nil |
| 614 |
case "done": |
| 615 |
if payload.Summary == "" { |
| 616 |
return nil, clnkr.ErrEmptySummary |
| 617 |
} |
| 618 |
verification, err := providerCompletionVerification( |
| 619 |
payload.Verification, |
| 620 |
payload.KnownRisks, |
| 621 |
) |
| 622 |
if err != nil { |
| 623 |
return nil, err |
| 624 |
} |
| 625 |
return &clnkr.DoneTurn{ |
| 626 |
Summary: payload.Summary, |
| 627 |
Verification: verification, |
| 628 |
KnownRisks: cloneStrings(payload.KnownRisks), |
| 629 |
Reasoning: payload.Reasoning, |
| 630 |
}, nil |
| 631 |
case "": |
| 632 |
return nil, fmt.Errorf("%w: missing type field", clnkr.ErrUnknownTurnType) |
| 633 |
default: |
| 634 |
return nil, fmt.Errorf("%w: %q", clnkr.ErrUnknownTurnType, payload.Type) |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
func providerCompletionVerification( |
| 639 |
raw *wireVerification, |
| 640 |
knownRisks []string, |
| 641 |
) (clnkr.CompletionVerification, error) { |
| 642 |
if raw == nil { |
| 643 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 644 |
"%w: missing required field %q", |
| 645 |
clnkr.ErrInvalidJSON, |
| 646 |
"verification", |
| 647 |
) |
| 648 |
} |
| 649 |
status := clnkr.VerificationStatus(strings.TrimSpace(raw.Status)) |
| 650 |
switch status { |
| 651 |
case clnkr.VerificationVerified, |
| 652 |
clnkr.VerificationPartiallyVerified, |
| 653 |
clnkr.VerificationNotVerified: |
| 654 |
default: |
| 655 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 656 |
"%w: invalid verification status %q", |
| 657 |
clnkr.ErrInvalidJSON, |
| 658 |
raw.Status, |
| 659 |
) |
| 660 |
} |
| 661 |
checks := make([]clnkr.VerificationCheck, 0, len(raw.Checks)) |
| 662 |
for i, check := range raw.Checks { |
| 663 |
if strings.TrimSpace(check.Command) == "" { |
| 664 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 665 |
"%w: verification.checks[%d].command must be non-empty", |
| 666 |
clnkr.ErrInvalidJSON, |
| 667 |
i, |
| 668 |
) |
| 669 |
} |
| 670 |
if strings.TrimSpace(check.Outcome) == "" { |
| 671 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 672 |
"%w: verification.checks[%d].outcome must be non-empty", |
| 673 |
clnkr.ErrInvalidJSON, |
| 674 |
i, |
| 675 |
) |
| 676 |
} |
| 677 |
if strings.TrimSpace(check.Evidence) == "" { |
| 678 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 679 |
"%w: verification.checks[%d].evidence must be non-empty", |
| 680 |
clnkr.ErrInvalidJSON, |
| 681 |
i, |
| 682 |
) |
| 683 |
} |
| 684 |
checks = append(checks, clnkr.VerificationCheck{ |
| 685 |
Command: check.Command, |
| 686 |
Outcome: check.Outcome, |
| 687 |
Evidence: check.Evidence, |
| 688 |
}) |
| 689 |
} |
| 690 |
if status == clnkr.VerificationVerified && len(checks) == 0 { |
| 691 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 692 |
"%w: verified done requires at least one verification check", |
| 693 |
clnkr.ErrInvalidJSON, |
| 694 |
) |
| 695 |
} |
| 696 |
if status == clnkr.VerificationPartiallyVerified && len(knownRisks) == 0 { |
| 697 |
return clnkr.CompletionVerification{}, fmt.Errorf( |
| 698 |
"%w: partially verified done requires at least one known risk", |
| 699 |
clnkr.ErrInvalidJSON, |
| 700 |
) |
| 701 |
} |
| 702 |
return clnkr.CompletionVerification{Status: status, Checks: checks}, nil |
| 703 |
} |
| 704 |
|
| 705 |
func providerJSON(turn clnkr.Turn) (string, error) { |
| 706 |
env, err := wrappedEnvelope(turn) |
| 707 |
if err != nil { |
| 708 |
return "", err |
| 709 |
} |
| 710 |
body, err := json.Marshal(env) |
| 711 |
if err != nil { |
| 712 |
return "", fmt.Errorf("provider turn json: %w", err) |
| 713 |
} |
| 714 |
return string(body), nil |
| 715 |
} |
| 716 |
|
| 717 |
func wrappedEnvelope(turn clnkr.Turn) (wireEnvelope, error) { |
| 718 |
switch v := turn.(type) { |
| 719 |
case clnkr.ActTurn: |
| 720 |
return wireEnvelope{ |
| 721 |
Turn: wireTurn{ |
| 722 |
Type: "act", |
| 723 |
Bash: &wireBash{Commands: wireCommands(v.Bash.Commands)}, |
| 724 |
Question: nil, |
| 725 |
Summary: nil, |
| 726 |
Reasoning: nullableString(v.Reasoning), |
| 727 |
}, |
| 728 |
}, nil |
| 729 |
case *clnkr.ActTurn: |
| 730 |
if v == nil { |
| 731 |
return wireEnvelope{}, fmt.Errorf( |
| 732 |
"provider turn json: %w: nil *ActTurn", |
| 733 |
errUnsupportedTurn, |
| 734 |
) |
| 735 |
} |
| 736 |
return wrappedEnvelope(*v) |
| 737 |
case clnkr.ClarifyTurn: |
| 738 |
return wireEnvelope{ |
| 739 |
Turn: wireTurn{ |
| 740 |
Type: "clarify", |
| 741 |
Bash: nil, |
| 742 |
Question: v.Question, |
| 743 |
Summary: nil, |
| 744 |
Reasoning: nullableString(v.Reasoning), |
| 745 |
}, |
| 746 |
}, nil |
| 747 |
case *clnkr.ClarifyTurn: |
| 748 |
if v == nil { |
| 749 |
return wireEnvelope{}, fmt.Errorf( |
| 750 |
"provider turn json: %w: nil *ClarifyTurn", |
| 751 |
errUnsupportedTurn, |
| 752 |
) |
| 753 |
} |
| 754 |
return wrappedEnvelope(*v) |
| 755 |
case clnkr.DoneTurn: |
| 756 |
return wireEnvelope{ |
| 757 |
Turn: wireTurn{ |
| 758 |
Type: "done", |
| 759 |
Bash: nil, |
| 760 |
Question: nil, |
| 761 |
Summary: v.Summary, |
| 762 |
Verification: wireCompletionVerification(v.Verification), |
| 763 |
KnownRisks: cloneStrings(v.KnownRisks), |
| 764 |
Reasoning: nullableString(v.Reasoning), |
| 765 |
}, |
| 766 |
}, nil |
| 767 |
case *clnkr.DoneTurn: |
| 768 |
if v == nil { |
| 769 |
return wireEnvelope{}, fmt.Errorf( |
| 770 |
"provider turn json: %w: nil *DoneTurn", |
| 771 |
errUnsupportedTurn, |
| 772 |
) |
| 773 |
} |
| 774 |
return wrappedEnvelope(*v) |
| 775 |
case nil: |
| 776 |
return wireEnvelope{}, fmt.Errorf("provider turn json: %w: nil turn", errUnsupportedTurn) |
| 777 |
default: |
| 778 |
return wireEnvelope{}, fmt.Errorf("provider turn json: %w: %T", errUnsupportedTurn, turn) |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
func cloneStrings(values []string) []string { |
| 783 |
if values == nil { |
| 784 |
return nil |
| 785 |
} |
| 786 |
return append([]string{}, values...) |
| 787 |
} |
| 788 |
|
| 789 |
func wireCompletionVerification(verification clnkr.CompletionVerification) *wireVerification { |
| 790 |
checks := make([]wireCheck, 0, len(verification.Checks)) |
| 791 |
for _, check := range verification.Checks { |
| 792 |
checks = append(checks, wireCheck{ |
| 793 |
Command: check.Command, |
| 794 |
Outcome: check.Outcome, |
| 795 |
Evidence: check.Evidence, |
| 796 |
}) |
| 797 |
} |
| 798 |
return &wireVerification{ |
| 799 |
Status: string(verification.Status), |
| 800 |
Checks: checks, |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
func wireCommands(commands []clnkr.BashAction) []wireCommand { |
| 805 |
wire := make([]wireCommand, 0, len(commands)) |
| 806 |
for _, command := range commands { |
| 807 |
wire = append(wire, wireCommand{ |
| 808 |
Command: command.Command, |
| 809 |
Workdir: workdirPtr(command.Workdir), |
| 810 |
}) |
| 811 |
} |
| 812 |
return wire |
| 813 |
} |
| 814 |
|
| 815 |
func nullableString(value string) any { |
| 816 |
if value == "" { |
| 817 |
return nil |
| 818 |
} |
| 819 |
return value |
| 820 |
} |
| 821 |
|
| 822 |
var errUnsupportedTurn = errors.New("unsupported turn") |
| 823 |
|
| 824 |
func ensureSingleJSONObject(dec *json.Decoder) error { |
| 825 |
var extra any |
| 826 |
if err := dec.Decode(&extra); err != io.EOF { |
| 827 |
if err == nil { |
| 828 |
return fmt.Errorf("unexpected trailing JSON value") |
| 829 |
} |
| 830 |
return err |
| 831 |
} |
| 832 |
return nil |
| 833 |
} |
| 834 |
|
| 835 |
var escapedListMarker = regexp.MustCompile(`(?m)^\\([*-])\s`) |
| 836 |
var inlineEscapedListMarker = regexp.MustCompile(`\\([*-])\s`) |
| 837 |
|
| 838 |
func normalizeHumanText(value string) string { |
| 839 |
if value == "" || !strings.Contains(value, `\`) { |
| 840 |
return value |
| 841 |
} |
| 842 |
|
| 843 |
normalized := value |
| 844 |
for range 2 { |
| 845 |
decoded, ok := decodeEscapedHumanText(normalized) |
| 846 |
if !ok || decoded == normalized { |
| 847 |
break |
| 848 |
} |
| 849 |
normalized = decoded |
| 850 |
} |
| 851 |
|
| 852 |
normalized = escapedListMarker.ReplaceAllString(normalized, `$1 `) |
| 853 |
if strings.Contains(normalized, "\n- ") || |
| 854 |
strings.Contains(normalized, "\n* ") || |
| 855 |
strings.HasPrefix(normalized, "- ") || |
| 856 |
strings.HasPrefix(normalized, "* ") { |
| 857 |
normalized = inlineEscapedListMarker.ReplaceAllString(normalized, "\n$1 ") |
| 858 |
} |
| 859 |
return normalized |
| 860 |
} |
| 861 |
|
| 862 |
func decodeEscapedHumanText(value string) (string, bool) { |
| 863 |
if !strings.Contains(value, `\n`) && |
| 864 |
!strings.Contains(value, `\r`) && |
| 865 |
!strings.Contains(value, `\\-`) && |
| 866 |
!strings.Contains(value, `\\*`) && |
| 867 |
!strings.Contains(value, `\"`) { |
| 868 |
return "", false |
| 869 |
} |
| 870 |
|
| 871 |
quoted := `"` + strings.ReplaceAll(value, `"`, `\"`) + `"` |
| 872 |
decoded, err := strconv.Unquote(quoted) |
| 873 |
if err != nil { |
| 874 |
return "", false |
| 875 |
} |
| 876 |
return decoded, true |
| 877 |
} |
| 878 |
|
| 879 |
func workdirPtr(workdir string) *string { |
| 880 |
if workdir == "" { |
| 881 |
return nil |
| 882 |
} |
| 883 |
return &workdir |
| 884 |
} |