Skip to content

Commit 025fb3a

Browse files
committed
fix(core): use tool-result id
1 parent e050b3e commit 025fb3a

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def pack_previous_messages_section(
3131
) -> str:
3232
task_ids = [m.task_id for m in messages]
3333
mappings = {t.id: t for t in tasks}
34+
tool_mappings = {}
3435
task_descs = []
3536
for ti in task_ids:
3637
if ti is None:
@@ -45,15 +46,19 @@ def pack_previous_messages_section(
4546
task_descs.append("(no task linked)")
4647
return "\n---\n".join(
4748
[
48-
f"{td}\n{m.to_string(truncate_chars=256)}"
49+
f"{td}\n{m.to_string(tool_mappings, truncate_chars=256)}"
4950
for td, m in zip(task_descs, messages)
5051
]
5152
)
5253

5354

5455
def pack_current_message_with_ids(messages: list[MessageBlob]) -> str:
56+
tool_mappings = {}
5557
return "\n".join(
56-
[f"<message id={i}> {m.to_string()} </message>" for i, m in enumerate(messages)]
58+
[
59+
f"<message id={i}> {m.to_string(tool_mappings, truncate_chars=1024)} </message>"
60+
for i, m in enumerate(messages)
61+
]
5762
)
5863

5964

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
def pack_task_data(
1616
task: TaskSchema, message_blobs: list[MessageBlob]
1717
) -> tuple[str, str, str]:
18+
tool_mappings = {}
1819
return (
1920
task.data.task_description,
2021
"\n".join([f"- {p}" for p in (task.data.user_preferences or [])]),
21-
"\n".join([m.to_string(truncate_chars=1024) for m in message_blobs]),
22+
"\n".join(
23+
[m.to_string(tool_mappings, truncate_chars=1024) for m in message_blobs]
24+
),
2225
)
2326

2427

src/server/core/acontext_core/schema/orm/block.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import uuid
21
from dataclasses import dataclass, field
32
from sqlalchemy import (
43
String,
@@ -9,7 +8,7 @@
98
Boolean,
109
BigInteger,
1110
)
12-
from sqlalchemy.orm import relationship, foreign, remote
11+
from sqlalchemy.orm import relationship
1312
from sqlalchemy.dialects.postgresql import JSONB, UUID
1413
from typing import TYPE_CHECKING, Optional, List, Dict, Any
1514
from .base import ORM_BASE, CommonMixin

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class Asset(BaseModel):
2727
class ToolCallMeta(BaseModel):
2828
name: str
2929
arguments: dict | str
30+
id: Optional[str] = None
3031

3132

3233
class ToolResultMeta(BaseModel):
33-
name: str
34-
result: str
34+
tool_call_id: str
3535

3636

3737
class Part(BaseModel):

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
ROLE_REPLACE_NAME = {"assistant": "agent"}
1111

1212

13-
def pack_part_line(role: str, part: Part, truncate_chars: int = None) -> str:
13+
def pack_part_line(
14+
role: str,
15+
part: Part,
16+
tool_mapping: dict[str, ToolCallMeta],
17+
truncate_chars: int = None,
18+
) -> str:
1419
role = ROLE_REPLACE_NAME.get(role, role)
1520
header = f"<{role}>({part.type})"
1621
if part.type not in STRING_TYPES:
@@ -25,15 +30,25 @@ def pack_part_line(role: str, part: Part, truncate_chars: int = None) -> str:
2530
"arguments": tool_call_meta.arguments,
2631
}
2732
)
33+
if tool_call_meta.id is not None:
34+
tool_mapping[tool_call_meta.id] = tool_call_meta
2835
r = f"{header} {tool_data}"
2936
elif part.type == "tool-result":
3037
tool_result_meta = ToolResultMeta(**part.meta)
31-
tool_data = json.dumps(
32-
{
33-
"tool_name": tool_result_meta.name,
34-
"result": tool_result_meta.result,
35-
}
36-
)
38+
if tool_result_meta.tool_call_id not in tool_mapping:
39+
tool_data = json.dumps(
40+
{
41+
"result": part.text,
42+
}
43+
)
44+
else:
45+
tool_data = tool_mapping[tool_result_meta.tool_call_id]
46+
tool_data = json.dumps(
47+
{
48+
"tool_name": tool_data.name,
49+
"result": part.text,
50+
}
51+
)
3752
r = f"{header} {tool_data}"
3853
else:
3954
LOG.warning(f"Unknown message part type: {part.type}")
@@ -49,9 +64,16 @@ class MessageBlob(BaseModel):
4964
parts: List[Part]
5065
task_id: Optional[asUUID] = None
5166

52-
def to_string(self, truncate_chars: int = None, **kwargs) -> str:
67+
def to_string(
68+
self,
69+
tool_mapping: dict[str, ToolCallMeta],
70+
truncate_chars: int = None,
71+
**kwargs,
72+
) -> str:
5373
lines = [
54-
pack_part_line(self.role, p, truncate_chars=truncate_chars, **kwargs)
74+
pack_part_line(
75+
self.role, p, tool_mapping, truncate_chars=truncate_chars, **kwargs
76+
)
5577
for p in self.parts
5678
]
5779
return "\n".join(lines)

0 commit comments

Comments
 (0)