mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-02 19:55:18 +02:00
chore: ran linting
This commit is contained in:
parent
4afdfb580d
commit
e059ee4193
2 changed files with 59 additions and 39 deletions
|
|
@ -257,10 +257,12 @@ def _parse_sections(content: str) -> list[dict[str, str]]:
|
||||||
if is_section_heading:
|
if is_section_heading:
|
||||||
# Save previous section
|
# Save previous section
|
||||||
if current_heading or current_body_lines:
|
if current_heading or current_body_lines:
|
||||||
sections.append({
|
sections.append(
|
||||||
"heading": current_heading,
|
{
|
||||||
"body": "\n".join(current_body_lines).strip(),
|
"heading": current_heading,
|
||||||
})
|
"body": "\n".join(current_body_lines).strip(),
|
||||||
|
}
|
||||||
|
)
|
||||||
current_heading = line.strip()
|
current_heading = line.strip()
|
||||||
current_body_lines = []
|
current_body_lines = []
|
||||||
else:
|
else:
|
||||||
|
|
@ -268,10 +270,12 @@ def _parse_sections(content: str) -> list[dict[str, str]]:
|
||||||
|
|
||||||
# Save last section
|
# Save last section
|
||||||
if current_heading or current_body_lines:
|
if current_heading or current_body_lines:
|
||||||
sections.append({
|
sections.append(
|
||||||
"heading": current_heading,
|
{
|
||||||
"body": "\n".join(current_body_lines).strip(),
|
"heading": current_heading,
|
||||||
})
|
"body": "\n".join(current_body_lines).strip(),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return sections
|
return sections
|
||||||
|
|
||||||
|
|
@ -370,11 +374,17 @@ async def _revise_with_sections(
|
||||||
# Emit plan summary
|
# Emit plan summary
|
||||||
parts = []
|
parts = []
|
||||||
if modify_indices:
|
if modify_indices:
|
||||||
parts.append(f"modifying {len(modify_indices)} section{'s' if len(modify_indices) > 1 else ''}")
|
parts.append(
|
||||||
|
f"modifying {len(modify_indices)} section{'s' if len(modify_indices) > 1 else ''}"
|
||||||
|
)
|
||||||
if add_sections:
|
if add_sections:
|
||||||
parts.append(f"adding {len(add_sections)} new section{'s' if len(add_sections) > 1 else ''}")
|
parts.append(
|
||||||
|
f"adding {len(add_sections)} new section{'s' if len(add_sections) > 1 else ''}"
|
||||||
|
)
|
||||||
if remove_indices:
|
if remove_indices:
|
||||||
parts.append(f"removing {len(remove_indices)} section{'s' if len(remove_indices) > 1 else ''}")
|
parts.append(
|
||||||
|
f"removing {len(remove_indices)} section{'s' if len(remove_indices) > 1 else ''}"
|
||||||
|
)
|
||||||
plan_summary = ", ".join(parts) if parts else "no changes needed"
|
plan_summary = ", ".join(parts) if parts else "no changes needed"
|
||||||
|
|
||||||
dispatch_custom_event(
|
dispatch_custom_event(
|
||||||
|
|
@ -400,7 +410,11 @@ async def _revise_with_sections(
|
||||||
sec = sections[idx]
|
sec = sections[idx]
|
||||||
|
|
||||||
# Extract plain section name (strip markdown heading markers)
|
# Extract plain section name (strip markdown heading markers)
|
||||||
section_name = re.sub(r"^#+\s*", "", sec["heading"]).strip() if sec["heading"] else "Preamble"
|
section_name = (
|
||||||
|
re.sub(r"^#+\s*", "", sec["heading"]).strip()
|
||||||
|
if sec["heading"]
|
||||||
|
else "Preamble"
|
||||||
|
)
|
||||||
dispatch_custom_event(
|
dispatch_custom_event(
|
||||||
"report_progress",
|
"report_progress",
|
||||||
{
|
{
|
||||||
|
|
@ -417,20 +431,18 @@ async def _revise_with_sections(
|
||||||
context_parts = []
|
context_parts = []
|
||||||
if idx > 0:
|
if idx > 0:
|
||||||
prev = sections[idx - 1]
|
prev = sections[idx - 1]
|
||||||
prev_preview = prev["body"][:300] + ("..." if len(prev["body"]) > 300 else "")
|
prev_preview = prev["body"][:300] + (
|
||||||
|
"..." if len(prev["body"]) > 300 else ""
|
||||||
|
)
|
||||||
context_parts.append(
|
context_parts.append(
|
||||||
f"**Previous section:** {prev['heading']}\n{prev_preview}"
|
f"**Previous section:** {prev['heading']}\n{prev_preview}"
|
||||||
)
|
)
|
||||||
if idx < len(sections) - 1:
|
if idx < len(sections) - 1:
|
||||||
nxt = sections[idx + 1]
|
nxt = sections[idx + 1]
|
||||||
nxt_preview = nxt["body"][:300] + ("..." if len(nxt["body"]) > 300 else "")
|
nxt_preview = nxt["body"][:300] + ("..." if len(nxt["body"]) > 300 else "")
|
||||||
context_parts.append(
|
context_parts.append(f"**Next section:** {nxt['heading']}\n{nxt_preview}")
|
||||||
f"**Next section:** {nxt['heading']}\n{nxt_preview}"
|
|
||||||
)
|
|
||||||
context = (
|
context = (
|
||||||
"\n\n".join(context_parts)
|
"\n\n".join(context_parts) if context_parts else "(No surrounding sections)"
|
||||||
if context_parts
|
|
||||||
else "(No surrounding sections)"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
revise_prompt = _REVISE_SECTION_PROMPT.format(
|
revise_prompt = _REVISE_SECTION_PROMPT.format(
|
||||||
|
|
@ -455,9 +467,7 @@ async def _revise_with_sections(
|
||||||
"body": revised_text,
|
"body": revised_text,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
logger.info(f"[generate_report] Revised section [{idx}]: {sec['heading']}")
|
||||||
f"[generate_report] Revised section [{idx}]: {sec['heading']}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Step 3: Handle new section additions (insert in reverse order to preserve indices)
|
# Step 3: Handle new section additions (insert in reverse order to preserve indices)
|
||||||
for add_info in sorted(
|
for add_info in sorted(
|
||||||
|
|
@ -513,10 +523,13 @@ async def _revise_with_sections(
|
||||||
if new_parsed:
|
if new_parsed:
|
||||||
revised_sections.insert(insert_idx, new_parsed[0])
|
revised_sections.insert(insert_idx, new_parsed[0])
|
||||||
else:
|
else:
|
||||||
revised_sections.insert(insert_idx, {
|
revised_sections.insert(
|
||||||
"heading": heading,
|
insert_idx,
|
||||||
"body": new_content,
|
{
|
||||||
})
|
"heading": heading,
|
||||||
|
"body": new_content,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"[generate_report] Added new section after [{after_idx}]: {heading}"
|
f"[generate_report] Added new section after [{after_idx}]: {heading}"
|
||||||
|
|
@ -859,7 +872,10 @@ def create_generate_report_tool(
|
||||||
else:
|
else:
|
||||||
dispatch_custom_event(
|
dispatch_custom_event(
|
||||||
"report_progress",
|
"report_progress",
|
||||||
{"phase": "kb_search_done", "message": "No results found in knowledge base"},
|
{
|
||||||
|
"phase": "kb_search_done",
|
||||||
|
"message": "No results found in knowledge base",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
logger.info("[generate_report] KB search returned no results")
|
logger.info("[generate_report] KB search returned no results")
|
||||||
|
|
||||||
|
|
@ -893,7 +909,10 @@ def create_generate_report_tool(
|
||||||
# all sections need changes.
|
# all sections need changes.
|
||||||
dispatch_custom_event(
|
dispatch_custom_event(
|
||||||
"report_progress",
|
"report_progress",
|
||||||
{"phase": "revision_start", "message": "Analyzing sections to modify..."},
|
{
|
||||||
|
"phase": "revision_start",
|
||||||
|
"message": "Analyzing sections to modify...",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
logger.info(
|
logger.info(
|
||||||
"[generate_report] Revision mode — attempting section-level revision"
|
"[generate_report] Revision mode — attempting section-level revision"
|
||||||
|
|
@ -901,7 +920,8 @@ def create_generate_report_tool(
|
||||||
report_content = await _revise_with_sections(
|
report_content = await _revise_with_sections(
|
||||||
llm=llm,
|
llm=llm,
|
||||||
parent_content=parent_report_content,
|
parent_content=parent_report_content,
|
||||||
user_instructions=user_instructions or "Improve and refine the report.",
|
user_instructions=user_instructions
|
||||||
|
or "Improve and refine the report.",
|
||||||
source_content=capped_source,
|
source_content=capped_source,
|
||||||
topic=topic,
|
topic=topic,
|
||||||
report_style=report_style,
|
report_style=report_style,
|
||||||
|
|
|
||||||
|
|
@ -388,8 +388,7 @@ async def _stream_agent_events(
|
||||||
else "Report"
|
else "Report"
|
||||||
)
|
)
|
||||||
is_revision = bool(
|
is_revision = bool(
|
||||||
isinstance(tool_input, dict)
|
isinstance(tool_input, dict) and tool_input.get("parent_report_id")
|
||||||
and tool_input.get("parent_report_id")
|
|
||||||
)
|
)
|
||||||
step_title = "Revising report" if is_revision else "Generating report"
|
step_title = "Revising report" if is_revision else "Generating report"
|
||||||
last_active_step_title = step_title
|
last_active_step_title = step_title
|
||||||
|
|
@ -824,21 +823,22 @@ async def _stream_agent_events(
|
||||||
phase = data.get("phase", "")
|
phase = data.get("phase", "")
|
||||||
# Always keep the "Topic: ..." line
|
# Always keep the "Topic: ..." line
|
||||||
topic_items = [
|
topic_items = [
|
||||||
item for item in last_active_step_items
|
item for item in last_active_step_items if item.startswith("Topic:")
|
||||||
if item.startswith("Topic:")
|
|
||||||
]
|
]
|
||||||
|
|
||||||
if phase in ("revising_section", "adding_section"):
|
if phase in ("revising_section", "adding_section"):
|
||||||
# During section-level ops: keep plan summary + show current op
|
# During section-level ops: keep plan summary + show current op
|
||||||
plan_items = [
|
plan_items = [
|
||||||
item for item in last_active_step_items
|
item
|
||||||
if item.startswith("Topic:") or item.startswith("Modifying ")
|
for item in last_active_step_items
|
||||||
or item.startswith("Adding ") or item.startswith("Removing ")
|
if item.startswith("Topic:")
|
||||||
|
or item.startswith("Modifying ")
|
||||||
|
or item.startswith("Adding ")
|
||||||
|
or item.startswith("Removing ")
|
||||||
]
|
]
|
||||||
# Only keep plan_items that don't end with "..." (not progress lines)
|
# Only keep plan_items that don't end with "..." (not progress lines)
|
||||||
plan_items = [
|
plan_items = [
|
||||||
item for item in plan_items
|
item for item in plan_items if not item.endswith("...")
|
||||||
if not item.endswith("...")
|
|
||||||
]
|
]
|
||||||
last_active_step_items = [*plan_items, message]
|
last_active_step_items = [*plan_items, message]
|
||||||
else:
|
else:
|
||||||
|
|
@ -1384,4 +1384,4 @@ async def stream_resume_chat(
|
||||||
yield streaming_service.format_done()
|
yield streaming_service.format_done()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
await clear_ai_responding(session, chat_id)
|
await clear_ai_responding(session, chat_id)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue