WeKnora/internal/application/service/chat_pipeline
draix 7adf88766a fix(graph): recover JSON from malformed/truncated fences in extract_entity
Closes #1113

The graph extraction pipeline relies on a strict markdown fence regex to
pull JSON out of LLM responses. In production the regex misses ~84% of
real-world responses, in three ways:

  1. The LLM hits max_tokens mid-output and produces no closing fence.
  2. The opening fence is malformed or surrounded by prose, so the
     non-greedy regex fails to anchor a match.
  3. The model returns raw JSON with stray backticks but no real fence.

In every case extractContent fell through to returning the raw text,
which then failed json.Unmarshal with errors like
"invalid character '` + "`" + "' looking for beginning of value".

This change keeps the existing happy path untouched and adds a
conservative recovery step in the default branch of extractContent:

  - If an opening ``` is present, take everything after it, drop a
    likely language tag on the first line, cut at any trailing closing
    fence, and trim stray backticks/whitespace.
  - Otherwise, look for an outermost JSON object/array in the text using
    a small bracket-balanced scanner that respects string literals, so
    embedded {} or [] inside JSON strings don't confuse it.
  - Only fall back to the original raw-text behavior when neither
    strategy yields anything plausible.

The recovery helpers (stripFencesAndExtract, extractJSONLike,
isLikelyLanguageTag) are package-private and have table-driven tests in
extract_entity_test.go covering the three failure patterns described in
the issue, plus the previously-working fenced and bare-JSON shapes to
guard against regressions.
2026-05-07 15:40:19 +08:00
..
chat_completion.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
chat_completion_stream.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
chat_pipeline.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
chat_pipeline_test.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
common.go fix(chat): include history in model fallback 2026-05-04 23:24:21 +08:00
data_analysis.go feat: correct data analysis errors using fileService. 2026-04-27 13:18:08 +08:00
extract_entity.go fix(graph): recover JSON from malformed/truncated fences in extract_entity 2026-05-07 15:40:19 +08:00
extract_entity_test.go fix(graph): recover JSON from malformed/truncated fences in extract_entity 2026-05-07 15:40:19 +08:00
filter_top_k.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
into_chat_message.go feat(prompt): enhance question rewriting and intent classification in prompt templates 2026-04-16 23:15:02 +08:00
load_history.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
memory.go fix(memory): dedupe episode write on duplicate streaming Done 2026-05-04 23:49:29 +08:00
merge.go feat(logger): implement LLM debug logging functionality 2026-04-16 23:15:02 +08:00
merge_expand.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
merge_faq.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
merge_history.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
merge_overlap.go refactor(postgres): optimize vector retrieval and HNSW settings 2026-04-17 12:11:03 +08:00
plugin_test.go feat(prompt): enhance context and intent classification in templates 2026-03-26 18:57:12 +08:00
query_expansion.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
query_understand.go feat(prompt): enhance question rewriting and intent classification in prompt templates 2026-04-16 23:15:02 +08:00
rerank.go refactor(chat_pipeline): improve regex patterns for table parsing 2026-03-25 22:08:29 +08:00
rerank_clean_test.go feat(prompts): update assistant descriptions and add intent-specific templates 2026-03-25 22:08:29 +08:00
search.go fix(web-search): normalize default tenant web search config at runtime 2026-04-30 15:28:35 +08:00
search_entity.go feat(logger): implement LLM debug logging functionality 2026-04-16 23:15:02 +08:00
search_parallel.go feat(web-search): refactor web search provider 2026-03-31 20:22:34 +08:00
web_fetch.go feat(web-search): refactor web search provider 2026-03-31 20:22:34 +08:00
wiki_boost.go feat: Enhance wiki API and repository with slug encoding and improved search functionality 2026-04-22 21:18:09 +08:00