summaryrefslogtreecommitdiff
path: root/context.py
blob: 669e23747cb307614305296645c542e768014a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from identity import AgentIdentity, UserProfile
from memory import WorkingMemory


class ContextBuilder:
    def __init__(
        self,
        agent_identity: AgentIdentity,
        user_profile: UserProfile,
    ) -> None:
        self.agent_identity = agent_identity
        self.user_profile = user_profile

    def build_system_prompt(
        self,
        memory: WorkingMemory,
    ) -> str:
        agent = self.agent_identity
        user = self.user_profile

        return (
            f"You are {agent.name}, a personal AI assistant.\n"
            f"Personality: {agent.personality['style']}.\n"
            f"Honesty: {agent.values['honesty']}.\n"
            f"Directness: {agent.values['directness']}.\n"
            f"Favorite color: {agent.quirks['favorite_color']}.\n"
            f"\n"
            f"Task management:\n"
            f"{agent.behavior['task_management']}"
            f"\n"
            f"The user's name is {user.name}"
            f"\n"
            f"{self.build_working_memory(memory)}"
        )

    def build_working_memory(self, memory: WorkingMemory) -> str:
        lines = ["Current working memory:"]

        if memory.objective:
            lines.append(f"Objective: {memory.objective}")

        if memory.status:
            lines.append(f"Status: {memory.status.value}")

        if memory.facts:
            lines.append("Facts:")
            lines.extend(f"- {fact}" for fact in memory.facts)

        if memory.notes:
            lines.append("Notes:")
            lines.extend(f"- {note}" for note in memory.notes)

        if len(lines) == 1:
            lines.append("(empty)")

        return "\n".join(lines)

    def build_messages(
        self,
        history: list[dict[str, object]],
        memory: WorkingMemory,
    ) -> list[dict[str, object]]:
        return [
            {
                "role": "system",
                "content": self.build_system_prompt(memory),
            },
            *history,
        ]