--- Response Parsing --- parseEnhancedResponse parses the enhanced extraction prompt response.
(response string)
| 574 | |
| 575 | // parseEnhancedResponse parses the enhanced extraction prompt response. |
| 576 | func parseEnhancedResponse(response string) (daily, longTerm string, profile map[string]string, topics map[string]string, projects map[string]string) { |
| 577 | profile = make(map[string]string) |
| 578 | projects = make(map[string]string) |
| 579 | topics = make(map[string]string) |
| 580 | |
| 581 | upper := strings.ToUpper(response) |
| 582 | |
| 583 | // Find all section positions |
| 584 | type section struct { |
| 585 | name string |
| 586 | idx int |
| 587 | } |
| 588 | sections := []section{ |
| 589 | {"DAILY", findSection(upper, "DAILY")}, |
| 590 | {"LONGTERM", findSection(upper, "LONGTERM")}, |
| 591 | {"PROFILE_UPDATE", findSection(upper, "PROFILE_UPDATE")}, |
| 592 | {"PROFILE", findSection(upper, "PROFILE")}, |
| 593 | {"TOPICS", findSection(upper, "TOPICS")}, |
| 594 | {"PROJECTS", findSection(upper, "PROJECTS")}, |
| 595 | } |
| 596 | |
| 597 | // Filter found sections and sort by position (stable: PROFILE_UPDATE and |
| 598 | // PROFILE can match at the same index; declaration order must win). |
| 599 | var found []section |
| 600 | for _, s := range sections { |
| 601 | if s.idx >= 0 { |
| 602 | found = append(found, s) |
| 603 | } |
| 604 | } |
| 605 | sort.SliceStable(found, func(i, j int) bool { return found[i].idx < found[j].idx }) |
| 606 | |
| 607 | // Extract content between sections |
| 608 | extractContent := func(startIdx int, nextIdx int) string { |
| 609 | // Find end of header line |
| 610 | nlIdx := strings.Index(response[startIdx:], "\n") |
| 611 | if nlIdx < 0 { |
| 612 | return "" |
| 613 | } |
| 614 | contentStart := startIdx + nlIdx + 1 |
| 615 | contentEnd := len(response) |
| 616 | if nextIdx > 0 { |
| 617 | contentEnd = nextIdx |
| 618 | } |
| 619 | if contentStart >= contentEnd { |
| 620 | return "" |
| 621 | } |
| 622 | return strings.TrimSpace(response[contentStart:contentEnd]) |
| 623 | } |
| 624 | |
| 625 | for i, sec := range found { |
| 626 | nextIdx := -1 |
| 627 | if i+1 < len(found) { |
| 628 | nextIdx = found[i+1].idx |
| 629 | } |
| 630 | content := extractContent(sec.idx, nextIdx) |
| 631 | |
| 632 | if isNothingNew(content) { |
| 633 | continue |