Skip to content

Domain models#50

Merged
Dax233 merged 14 commits intodevfrom
Domain-Models
Aug 12, 2025
Merged

Domain models#50
Dax233 merged 14 commits intodevfrom
Domain-Models

Conversation

@Dax233
Copy link
Member

@Dax233 Dax233 commented Aug 12, 2025

Sourcery 总结

通过定义新的数据库模型和服务,将 GoalManager 连接到核心 AI 状态和决策工作流中,并在提示和模式中暴露目标控制,从而引入短期目标管理功能。

新功能:

  • 添加“目标”域,包含 GoalDocument 模型、用于持久化的 GoalStorageService 和用于内存中目标生命周期管理的 GoalManager

改进:

  • 将目标管理集成到 AIStateManager 初始化和决策分发器中,以处理添加/删除命令
  • 在 LLM 模式中暴露 "manage_goals" 控制,并在系统提示中包含格式化的当前目标
  • 在 CoreDBCollections 中注册新的“目标”集合,并带有适当的索引

杂项任务:

  • goal_storage_service 添加到服务容器初始化中
  • 清理 ThoughtGenerator 中的系统提示日志记录
Original summary in English

Summary by Sourcery

Introduce a short-term goal management feature by defining new database models and services, connecting a GoalManager into the core AI state and decision workflows, and exposing goal controls in prompts and schemas

New Features:

  • Add "Goal" domain with GoalDocument model, GoalStorageService for persistence, and GoalManager for in-memory goal lifecycle management

Enhancements:

  • Integrate goal management into AIStateManager initialization and into the decision dispatcher for handling add/remove commands
  • Expose "manage_goals" control in the LLM schema and include formatted current goals in system prompts
  • Register a new "Goals" collection with appropriate indexes in CoreDBCollections

Chores:

  • Add goal_storage_service to service container initialization
  • Sanitize system prompt logging in ThoughtGenerator

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 12, 2025

审阅者指南

此PR引入了一个新AI目标域模型和持久层,将GoalManager集成到核心逻辑管道中,扩展了意识控制模式,增加了“manage_goals”操作,并在提示中显示当前目标,以及少量日志清理。

文件级变更

变更 详情 文件
引入目标持久化和领域模型
  • 在数据库模型中定义GoalDocument
  • 在CoreDBCollections中添加GOALS集合和索引
  • 实现带有加载/添加/更新方法的GoalStorageService
src/database/models.py
src/database/services/goal_storage_service.py
添加GoalManager并集成到核心逻辑
  • 实现带有initialize/add/remove/get_formatted_goals方法的GoalManager类
  • 更新AIStateManager以接受goal_storage_service并调用initialize
  • 在decision_dispatcher中处理manage_goals并移除已处理的payload
  • 在bootstrap中实例化带有goal_storage_service的state_manager并调用initialize
  • 通过GoalManager在prompt_builder中注入current_goals_block
src/core_logic/goal_manager.py
src/core_logic/state_manager.py
src/core_logic/decision_dispatcher.py
src/bootstrap/builder.py
src/core_logic/prompt_builder.py
通过目标管理扩展意识控制
  • 为manage_goals操作(添加/移除)定义JSON Schema
  • 在get_level_consciousness_controls_definitions中包含manage_goals
src/platform_builders/core_builder.py
在提示模板中公开当前目标
  • 在注入的current_goals_block周围添加<current_goals>块
src/prompt_templates/prompt_templates.py
优化日志记录并移除冗余代码
  • 在ThoughtGenerator日志记录中清理并缩短system_prompt
  • 修复image_utils中的缩进/文档字符串并移除未使用的datetime导入
src/core_logic/thought_generator.py
src/common/image_utils.py
src/core_logic/state_manager.py

提示和命令

与Sourcery交互

  • 触发新评论: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复Sourcery的评论。
  • 从评论生成GitHub Issue: 回复评论,要求Sourcery从评论创建Issue。您也可以回复评论并加上 @sourcery-ai issue 来创建Issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai 即可随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 来随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任意位置写入 @sourcery-ai summary 即可随时在您想要的位置生成PR摘要。您也可以在拉取请求上评论 @sourcery-ai summary 来随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求上评论 @sourcery-ai guide 即可随时(重新)生成审阅者指南。
  • 解决所有Sourcery评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有Sourcery评论。如果您已经处理了所有评论并且不想再看到它们,这将很有用。
  • 关闭所有Sourcery评论: 在拉取请求上评论 @sourcery-ai dismiss 以关闭所有现有的Sourcery评论。如果您想重新开始新的评论,这尤其有用——别忘了评论 @sourcery-ai review 以触发新的评论!

自定义您的体验

访问您的仪表盘以:

  • 启用或禁用评论功能,例如Sourcery生成的拉取请求摘要、审阅者指南等。
  • 更改评论语言。
  • 添加、移除或编辑自定义评论说明。
  • 调整其他评论设置。

获取帮助

Original review guide in English

Reviewer's Guide

This PR introduces a new domain model and persistence layer for AI goals, integrates a GoalManager into the core logic pipeline, expands the consciousness-control schema with a “manage_goals” action, and surfaces current goals in prompts, alongside minor logging cleanups.

File-Level Changes

Change Details Files
Introduce goal persistence and domain model
  • Define GoalDocument in database models
  • Add GOALS collection and index in CoreDBCollections
  • Implement GoalStorageService with load/add/update methods
src/database/models.py
src/database/services/goal_storage_service.py
Add GoalManager and integrate into core logic
  • Implement GoalManager class with initialize/add/remove/get_formatted_goals
  • Update AIStateManager to accept goal_storage_service and invoke initialize
  • Handle manage_goals in decision_dispatcher and remove processed payload
  • Instantiate state_manager with goal_storage_service and call initialize in bootstrap
  • Inject current_goals_block via GoalManager in prompt_builder
src/core_logic/goal_manager.py
src/core_logic/state_manager.py
src/core_logic/decision_dispatcher.py
src/bootstrap/builder.py
src/core_logic/prompt_builder.py
Expand consciousness controls with goal management
  • Define JSON schema for manage_goals action (add/remove)
  • Include manage_goals in get_level_consciousness_controls_definitions
src/platform_builders/core_builder.py
Expose current goals in prompt templates
  • Add <current_goals> block around injected current_goals_block
src/prompt_templates/prompt_templates.py
Refine logging and remove small cruft
  • Sanitize and shorten system_prompt in ThoughtGenerator logging
  • Fix indentation/docstring in image_utils and remove unused datetime import
src/core_logic/thought_generator.py
src/common/image_utils.py
src/core_logic/state_manager.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @Dax233, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new "Goal Management" capability for the AI system, allowing it to dynamically set, track, and manage its own short-term objectives. This enhancement aims to improve the AI's ability to maintain focus and pursue specific tasks by integrating goal awareness directly into its decision-making and prompt generation processes, backed by persistent storage.

Highlights

  • Core Goal Management Logic: A new GoalManager component has been implemented to handle the creation, retrieval, and completion of AI-defined goals.
  • Persistent Storage for Goals: Dedicated data models (GoalDocument) and a GoalStorageService have been added to ensure that AI goals are persistently stored and retrieved from the database.
  • Integrated AI State Awareness: The AIStateManager now incorporates the GoalManager, and the system prompt template has been updated to expose current goals to the AI, enabling goal-oriented reasoning.
  • AI-Driven Goal Manipulation: The decision_dispatcher now supports new manage_goals commands, allowing the AI to programmatically add or remove its own goals based on its internal logic and evolving needs.
  • Refactored State Management: The AIStateManager has been streamlined by removing the get_current_state_for_prompt method, centralizing state initialization and management around the new initialize method and GoalManager.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


AI, with purpose, takes its flight, Goals it sets, both day and night. From code's deep core, new thoughts ignite, To guide its path, and make things right.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Dax233 - 我已审阅了你的更改,发现了一些需要解决的问题。

  • _handle_goal_management 中,你调用了异步的 add_goals/remove_goals 方法而没有 await 它们——请添加 await 以确保这些操作在继续之前完成。
  • 由于 add_goalsremove_goals 返回已处理的 ID 列表,请考虑使用这些返回的 ID 进行更精确的日志记录或下游逻辑,而不是仅仅记录输入长度。
供AI代理使用的提示
请解决此代码审查中的评论:
## 总体评论
-`_handle_goal_management` 中,你调用了异步的 `add_goals`/`remove_goals` 方法而没有 `await` 它们——请添加 `await` 以确保这些操作在继续之前完成。
- 由于 `add_goals``remove_goals` 返回已处理的 ID 列表,请考虑使用这些返回的 ID 进行更精确的日志记录或下游逻辑,而不是仅仅记录输入长度。

## 单独评论

### 评论 1
<location> `src/core_logic/goal_manager.py:60` </location>
<code_context>
+            f"下一个ID将是 G{self._next_id}。"
+        )
+
+    def _generate_next_id(self) -> str:
+        """生成一个格式化的、唯一的、递增的目标ID."""
+        goal_id = f"G{self._next_id}"
</code_context>

<issue_to_address>
`_next_id` 可能存在并发问题。

并发的 `add_goals` 调用可能由于非原子递增而导致重复的 ID。请使用原子计数器或数据库生成的 ID 来防止这种情况。

建议的实现:

```python
        self._next_id = max_id_num + 1
        self._id_lock = threading.Lock()
        logger.info(
            f"GoalManager 初始化完成,加载了 {len(self._goals)} 个活动目标。"
            f"下一个ID将是 G{self._next_id}"
        )

```

```python
    def _generate_next_id(self) -> str:
        """生成一个格式化的、唯一的、递增的目标ID."""
        with self._id_lock:
            goal_id = f"G{self._next_id}"
            self._next_id += 1
        return goal_id

```

如果文件中尚未包含 `import threading`,则需要将其添加到文件顶部:

```python
import threading
```

如果您的环境是异步的,并且您期望来自异步协程(而不是线程)的并发访问,请考虑使用 `asyncio.Lock` 而不是 `threading.Lock`。请相应地调整锁定机制。
</issue_to_address>

Sourcery 对开源项目免费 - 如果您喜欢我们的评论,请考虑分享它们 ✨
帮助我变得更有用!请点击每个评论旁边的 👍 或 👎,我将根据您的反馈改进您的评论。
Original comment in English

Hey @Dax233 - I've reviewed your changes and found some issues that need to be addressed.

  • In _handle_goal_management you’re calling the async add_goals/remove_goals methods without awaiting them—add awaits to ensure those operations complete before moving on.
  • Since add_goals and remove_goals return lists of processed IDs, consider using those returned IDs for more precise logging or downstream logic instead of just logging the input length.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In _handle_goal_management you’re calling the async add_goals/remove_goals methods without awaiting them—add awaits to ensure those operations complete before moving on.
- Since add_goals and remove_goals return lists of processed IDs, consider using those returned IDs for more precise logging or downstream logic instead of just logging the input length.

## Individual Comments

### Comment 1
<location> `src/core_logic/goal_manager.py:60` </location>
<code_context>
+            f"下一个ID将是 G{self._next_id}。"
+        )
+
+    def _generate_next_id(self) -> str:
+        """生成一个格式化的、唯一的、递增的目标ID."""
+        goal_id = f"G{self._next_id}"
</code_context>

<issue_to_address>
Potential concurrency issue with _next_id.

Concurrent add_goals calls could result in duplicate IDs due to non-atomic increments. Use an atomic counter or database-generated IDs to prevent this.

Suggested implementation:

```python
        self._next_id = max_id_num + 1
        self._id_lock = threading.Lock()
        logger.info(
            f"GoalManager 初始化完成,加载了 {len(self._goals)} 个活动目标。"
            f"下一个ID将是 G{self._next_id}"
        )

```

```python
    def _generate_next_id(self) -> str:
        """生成一个格式化的、唯一的、递增的目标ID."""
        with self._id_lock:
            goal_id = f"G{self._next_id}"
            self._next_id += 1
        return goal_id

```

You need to add `import threading` at the top of the file if it is not already present:

```python
import threading
```

If your environment is async and you expect concurrent access from async coroutines (not threads), consider using `asyncio.Lock` instead of `threading.Lock`. Adjust the locking mechanism accordingly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次代码审查主要关注新引入的目标管理功能。整体实现结构清晰,将目标管理逻辑有效地分离到了 GoalManagerGoalStorageService 中,并与现有的状态管理和决策流程进行了整合。审查中发现一个严重的功能性 bug,即在 decision_dispatcher.py 中调用异步函数时缺少 await,这将导致目标管理功能无法正常工作。此外,我还提出了一些关于性能优化和降低代码耦合度的建议,以提高代码的健壮性和可维护性。修复关键问题后,此拉取请求将是一次出色的功能增强。

@Dax233 Dax233 merged commit 4f2e436 into dev Aug 12, 2025
1 check passed
@Dax233 Dax233 deleted the Domain-Models branch August 12, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants