Skip to content

fix(bot):user memory message role changed to 'user',session add token use#733

Merged
qin-ctx merged 1 commit intomainfrom
fix_chat_system_message
Mar 18, 2026
Merged

fix(bot):user memory message role changed to 'user',session add token use#733
qin-ctx merged 1 commit intomainfrom
fix_chat_system_message

Conversation

@yeshion23333
Copy link
Copy Markdown
Collaborator

@yeshion23333 yeshion23333 commented Mar 18, 2026

Description

  1. user memory message role changed to 'user';
  2. session file add token use info;

Related Issue

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Additional Notes

2. fix memory role
@github-actions
Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Consecutive User Messages

Two consecutive "user" role messages are added to the prompt, which may cause issues with LLM APIs that require alternating user/assistant roles.

# User
user_info = await self._build_user_memory(session_key, current_message, history)
messages.append({"role": "user", "content": user_info})

# Current message (with optional image attachments)
user_content = self._build_user_content(current_message, media)
messages.append({"role": "user", "content": user_content})
Outdated Docstring

The docstring for _run_agent_loop is outdated; it still states the return value is a tuple of (final_content, tools_used), but now it returns three elements including token_usage.

    tuple of (final_content, tools_used)
"""

@yeshion23333 yeshion23333 linked an issue Mar 18, 2026 that may be closed by this pull request
@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix syntax error in OutboundMessage

Fix the syntax error in the OutboundMessage call. The time_cost keyword argument's
value is incorrectly split across two lines, which will cause a Python syntax error.
Combine the expression onto a single line or use parentheses for multi-line clarity.

bot/vikingbot/agent/loop.py [517-518]

-time_cost=time_cost
-or {},  # Pass through for channel-specific needs (e.g. Slack thread_ts)
+time_cost=time_cost or {},  # Pass through for channel-specific needs (e.g. Slack thread_ts)
Suggestion importance[1-10]: 9

__

Why: The existing code has a Python syntax error due to incorrectly splitting the time_cost keyword argument across lines without proper parentheses. The fix resolves this critical syntax issue by combining the expression into a single line.

High
Handle token_usage in _process_system_message

Ensure the _process_system_message method handles the new token_usage return value
from _run_agent_loop. Update the OutboundMessage (and session saving if applicable)
to include the token usage, and remove any references to the deleted
self._token_usage.

bot/vikingbot/agent/loop.py [544-549]

 # Run agent loop (no events published)
 final_content, tools_used, token_usage = await self._run_agent_loop(
     messages=messages,
     session_key=msg.session_key,
     publish_events=False,
 )
+# (Add code here to use token_usage in OutboundMessage/session)
Suggestion importance[1-10]: 3

__

Why: The suggestion correctly identifies that the new token_usage return value from _run_agent_loop should be handled in _process_system_message, but the provided improved code is identical to the existing code and only adds a comment, offering no concrete fix.

Low

Copy link
Copy Markdown
Contributor

@qin-ptr qin-ptr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查报告

Summary

这个 PR 实现了两个主要改进:1)将 user memory message 的 role 从 'system' 改为 'user';2)在 session 中记录 token 使用信息。整体方向正确,将 token_usage 从实例变量改为局部变量避免了状态污染,增强了异常处理。但存在一个严重的 bug 会导致 LLM API 调用失败,以及几处需要修复的代码问题。


Must Fix

  • [Bug] bot/vikingbot/agent/context.py:275-281连续的 user role 消息会导致 LLM API 失败

    当前代码产生两个连续的 'user' role 消息:

    # User memory (role: user)
    messages.append({"role": "user", "content": user_info})
    # Current message (role: user)
    messages.append({"role": "user", "content": user_content})

    大多数 LLM API(OpenAI、Anthropic Claude 等)要求消息必须在 user 和 assistant 之间交替,连续的 user 消息会导致 API 调用报错。

    建议方案 1(推荐):将 user memory 信息合并到 current user message 中

    # User memory + Current message combined
    user_info = await self._build_user_memory(session_key, current_message, history)
    user_content = self._build_user_content(current_message, media)
    
    # Combine into single user message
    if isinstance(user_content, str):
        combined_content = f"{user_info}\n\n---\n\n{user_content}"
    else:  # list of dicts (with images)
        combined_content = [{"type": "text", "text": user_info}] + user_content
    
    messages.append({"role": "user", "content": combined_content})

    建议方案 2:将 user memory 保留为 system role(恢复原设计),或在 system prompt 中处理。

  • [Bug] bot/vikingbot/agent/loop.py:517-518time_cost 参数处理错误

    代码写成:

    time_cost=time_cost
    or {},  # Pass through for channel-specific needs

    但 line 511 的 time_cost = round(time.time() - start_time, 2) 总是返回浮点数(不可能是 None),所以 or {} 没有意义且会导致类型不一致(应该是 float,却可能变成 dict)。而且代码格式混乱,将一个参数分成两行。

    修复

    time_cost=time_cost,
  • [Bug] bot/vikingbot/agent/loop.py:229文档字符串与实际返回值不匹配

    文档说返回 tuple of (final_content, tools_used),但实际返回三个值:tuple[str | None, list[dict], dict[str, int]]

    修复

    Returns:
        tuple of (final_content, tools_used, token_usage)

Suggestions

  • [Suggestion] bot/vikingbot/agent/loop.py:556-558_process_system_message 中应使用 token_usage

    虽然在 line 545 接收了 token_usage 返回值,但 line 556-558 的 session.add_message 没有传递它。为了一致性和数据完整性,建议添加:

    session.add_message(
        "assistant", final_content, tools_used=tools_used if tools_used else None,
        token_usage=token_usage
    )
  • [Suggestion] bot/vikingbot/session/manager.py:32类型提示可以更精确

    token_usage: dict[str, Any] 可以改为 token_usage: dict[str, int] | None,因为从代码来看 token_usage 的值都是整数(prompt_tokens, completion_tokens, total_tokens)。


Verdict

REQUEST_CHANGES

存在一个严重的 bug(连续 user 消息)会导致 LLM API 调用失败,必须修复才能合并。另外两个 bug(time_cost 处理错误、文档过时)也应该修复以保证代码质量和类型一致性。修复后这个 PR 的改进方向是正确的。


🤖 I am a bot owned by @qin-ctx, powered by Claude.

@qin-ctx qin-ctx merged commit a88927b into main Mar 18, 2026
5 of 6 checks passed
@qin-ctx qin-ctx deleted the fix_chat_system_message branch March 18, 2026 08:01
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: 使用ov chat后提示报错

4 participants