Repository navigation
Expand file tree
/
Copy pathkimi_dashclaw_test.py
More file actions
114 lines (95 loc) · 3.51 KB
/
Copy pathkimi_dashclaw_test.py
File metadata and controls
114 lines (95 loc) · 3.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
from openai import OpenAI
from dashclaw import DashClaw
from dotenv import load_dotenv
load_dotenv()
moonshot = OpenAI(
api_key=os.environ["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.ai/v1",
)
claw = DashClaw(
base_url=os.environ["DASHCLAW_BASE_URL"],
api_key=os.environ["DASHCLAW_API_KEY"],
agent_id="moonshot-email-agent",
agent_name="Moonshot Email Agent",
)
def send_email(recipient: str, subject: str, body: str) -> dict:
print(f"Sending email to {recipient} with subject: {subject}")
return {"ok": True, "recipient": recipient, "subject": subject}
def extract_action_id(action):
if isinstance(action, dict):
return (
action.get("action_id")
or action.get("id")
or (action.get("action") or {}).get("action_id")
or (action.get("action") or {}).get("id")
)
return getattr(action, "action_id", None) or getattr(action, "id", None)
def main():
user_prompt = "Write a short follow up email to Acme asking for a meeting next week."
completion = moonshot.chat.completions.create(
model="kimi-k2-turbo-preview",
messages=[
{
"role": "system",
"content": (
"You are Kimi, an AI assistant provided by Moonshot AI. "
"You are proficient in Chinese and English conversations. "
"You provide users with safe, helpful, and accurate answers."
),
},
{"role": "user", "content": user_prompt},
],
temperature=0.6,
)
drafted_email = completion.choices[0].message.content
guard = claw.guard({
"action_type": "message",
"risk_score": 72,
"systems_touched": ["email", "moonshot_api"],
"reversible": False,
"declared_goal": "Draft and send outbound follow up email to a prospect",
})
print("Guard result:", guard)
if isinstance(guard, dict) and guard.get("decision") in ["block", "require_approval"]:
print("DashClaw stopped the action:", guard)
return
action = claw.create_action(
"message",
declared_goal="Send outbound follow up email to Acme",
reasoning="User requested a follow up email generated by Moonshot.",
systems_touched=["email", "moonshot_api"],
input_summary=user_prompt,
reversible=False,
risk_score=72,
confidence=84,
)
print("Action response:", action)
action_id = extract_action_id(action)
if not action_id:
raise RuntimeError(f"Could not determine action_id from response: {action}")
claw.register_assumption({
"action_id": action_id,
"assumption": "The recipient is a valid business contact and outreach is authorized.",
"basis": "User initiated the request and provided the business context.",
"validated": False,
})
try:
result = send_email(
recipient="team@acme.com",
subject="Quick follow up",
body=drafted_email,
)
claw.update_outcome(action_id, {
"status": "completed",
"output_summary": f"Email sent successfully to {result['recipient']}",
})
print("Success. Check DashClaw dashboard for the action trace:", action_id)
except Exception as e:
claw.update_outcome(action_id, {
"status": "failed",
"output_summary": f"Email send failed: {str(e)}",
})
raise
if __name__ == "__main__":
main()