Skip to content

Commit cb09e44

Browse files
committed
fix(core): append before update status
1 parent f3ded19 commit cb09e44

8 files changed

Lines changed: 38 additions & 15 deletions

File tree

src/server/core/acontext_core/llm/agent/task.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ async def build_task_ctx(
7373
project_id=project_id,
7474
session_id=session_id,
7575
task_ids_index=[t.id for t in current_tasks],
76+
task_index=current_tasks,
7677
message_ids_index=[m.message_id for m in messages],
7778
)
7879
return use_ctx

src/server/core/acontext_core/llm/prompt/task.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,13 @@ def system_prompt(cls) -> str:
6565
6666
## Report your Thinking
6767
Use extremely brief wordings to report:
68-
1. Any new user requirement or planning? What are the tasks/steps?
69-
2. For each task, is it a task modification or creation situation?
70-
3. How existing tasks are related to current conversation? Do the existing tasks need to be updated?
68+
1. Any user requirement or planning?
69+
2. How existing tasks are related to current conversation?
70+
3. Any new task is created?
7171
4. Messages are contributed to which task?
72-
5. Do New/Existing tasks' status need to be updated?
73-
6. Describe your actions.
74-
7. Confirm your will call every necessary tools in this response.
75-
8. Confirm your will call `finish` tool once every tools are called
72+
5. Which task's status/description need to be updated?
73+
6. Describe your tool-call actions to correctly manage the tasks.
74+
7. Confirm your will call `finish` tool after every tools are called
7675
"""
7776

7877
@classmethod

src/server/core/acontext_core/llm/tool/task_lib/append.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
from ....schema.result import Result
77
from ....schema.orm import Task
88
from ....service.data import task as TD
9-
from ....env import LOG
9+
from ....schema.session.task import TaskStatus
1010
from .ctx import TaskCtx
1111

1212

1313
async def _append_messages_to_task_handler(
1414
ctx: TaskCtx,
1515
llm_arguments: dict,
1616
) -> Result[str]:
17-
task_order = llm_arguments.get("task_order", None)
17+
task_order: int = llm_arguments.get("task_order", None)
1818
message_order_indexes = llm_arguments.get("message_ids", [])
1919
if not task_order:
2020
return Result.resolve(
@@ -25,6 +25,7 @@ async def _append_messages_to_task_handler(
2525
f"Task order {task_order} is out of range, appending failed."
2626
)
2727
actually_task_id = ctx.task_ids_index[task_order - 1]
28+
actually_task = ctx.task_index[task_order - 1]
2829
actually_message_ids = [
2930
ctx.message_ids_index[i]
3031
for i in message_order_indexes
@@ -34,6 +35,10 @@ async def _append_messages_to_task_handler(
3435
return Result.resolve(
3536
f"No message ids to append, skip: {message_order_indexes}"
3637
)
38+
if actually_task.task_status in (TaskStatus.SUCCESS, TaskStatus.FAILED):
39+
return Result.resolve(
40+
f"Task {task_order} is already {actually_task.task_status}, appending failed."
41+
)
3742
r = await TD.append_messages_to_task(
3843
ctx.db_session,
3944
actually_message_ids,
@@ -56,6 +61,7 @@ async def _append_messages_to_task_handler(
5661
"name": "append_messages_to_task",
5762
"description": """Link current message ids to a task for tracking progress and context.
5863
Use this to associate conversation messages with relevant tasks.
64+
Make sure you append messages first(if any), then update the task status.
5965
If the task is marked as 'success' or 'failed', don't append messages to it.""",
6066
"parameters": {
6167
"type": "object",

src/server/core/acontext_core/llm/tool/task_lib/ctx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass
22
from ....infra.db import AsyncSession
33
from ....schema.utils import asUUID
4+
from ....schema.session.task import TaskSchema
45

56

67
@dataclass
@@ -9,4 +10,5 @@ class TaskCtx:
910
project_id: asUUID
1011
session_id: asUUID
1112
task_ids_index: list[asUUID]
13+
task_index: list[TaskSchema]
1214
message_ids_index: list[asUUID]

src/server/core/acontext_core/schema/session/message.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from pydantic import BaseModel
23
from typing import List, Optional
34
from ..orm import Part, ToolCallMeta
@@ -21,7 +22,13 @@ def pack_part_line(role: str, part: Part) -> str:
2122
return f"<{role}> {part.text}"
2223
elif part.type == "tool-call":
2324
tool_call_meta = ToolCallMeta(**part.meta)
24-
return f"<{role}> USE TOOL {tool_call_meta.tool_name}, WITH PARAMS {tool_call_meta.arguments}"
25+
tool_data = json.dumps(
26+
{
27+
"tool_name": tool_call_meta.tool_name,
28+
"arguments": tool_call_meta.arguments,
29+
}
30+
)
31+
return f"<{role}> {tool_data}"
2532
else:
2633
raise TypeError(f"Unknown message part type: {part.type}")
2734

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import session_message
2+
from . import space_task

src/server/core/acontext_core/service/controller/space_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
async def process_space_task(
1414
project_config: ProjectConfig, space_id: asUUID, task: TaskSchema
1515
):
16+
print("SPACE!!!!", space_id, task)
1617
if task.task_status != TaskStatus.SUCCESS:
1718
LOG.info(f"Task {task.id} is not success, skipping")
1819
return
@@ -28,7 +29,7 @@ async def process_space_task(
2829
MessageBlob(message_id=m.id, role=m.role, parts=m.parts, task_id=m.task_id)
2930
for m in messages
3031
]
31-
print(messages_data)
32+
print("\n".join([m.to_string() for m in messages_data]))
3233
# 2. call agent to digest raw messages to SOP
3334
...
3435

src/server/core/acontext_core/service/data/task.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ async def fetch_planning_task(
3333
task_description="",
3434
task_data=planning.task_data,
3535
space_digested=planning.space_digested,
36-
raw_message_ids=[msg.id for msg in planning.messages],
36+
raw_message_ids=[
37+
msg.id for msg in sorted(planning.messages, key=lambda m: m.created_at)
38+
],
3739
)
3840
)
3941

@@ -53,7 +55,9 @@ async def fetch_task(db_session: AsyncSession, task_id: asUUID) -> Result[TaskSc
5355
task_description=task.task_data.get("task_description", ""),
5456
task_data=task.task_data,
5557
space_digested=task.space_digested,
56-
raw_message_ids=[msg.id for msg in task.messages],
58+
raw_message_ids=[
59+
msg.id for msg in sorted(task.messages, key=lambda m: m.created_at)
60+
],
5761
)
5862
)
5963

@@ -81,11 +85,13 @@ async def fetch_current_tasks(
8185
task_description=t.task_data.get("task_description", ""),
8286
task_data=t.task_data,
8387
space_digested=t.space_digested,
84-
raw_message_ids=[msg.id for msg in t.messages],
88+
raw_message_ids=[
89+
msg.id for msg in sorted(t.messages, key=lambda m: m.created_at)
90+
],
8591
)
8692
for t in tasks
8793
]
88-
return Result.resolve(tasks_d) # Fixed: return tasks_d instead of tasks
94+
return Result.resolve(tasks_d)
8995

9096

9197
async def update_task(

0 commit comments

Comments
 (0)