7
7
app = typer .Typer ()
8
8
9
9
10
+ # debug constants
11
+ POST_ID = "1234"
12
+ USER_POST_ID = "5678"
13
+ USER = {"id" : "1234" , "display_name" : "John Doe" , "auth_method" : "local" }
14
+
15
+
16
+ def _render_message (message : dict ) -> str :
17
+ """Render a message to the user."""
18
+ if message ["is_assistant" ]:
19
+ return f"Assistant: { message ['text' ]} "
20
+ return f"User: { message ['text' ]} "
21
+
22
+
10
23
@app .command ()
11
24
def main (backend_url : str , api_key : str ):
12
25
"""Simple REPL frontend."""
@@ -17,7 +30,7 @@ def _post(path: str, json: dict) -> dict:
17
30
return response .json ()
18
31
19
32
typer .echo ("Requesting work..." )
20
- tasks = [_post ("/api/v1/tasks/" , {"type" : "generic " })]
33
+ tasks = [_post ("/api/v1/tasks/" , {"type" : "random " })]
21
34
while tasks :
22
35
task = tasks .pop (0 )
23
36
match (task ["type" ]):
@@ -26,7 +39,7 @@ def _post(path: str, json: dict) -> dict:
26
39
typer .echo (task ["story" ])
27
40
28
41
# acknowledge task
29
- _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : "1234" })
42
+ _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : POST_ID })
30
43
31
44
summary = typer .prompt ("Enter your summary" )
32
45
@@ -35,10 +48,10 @@ def _post(path: str, json: dict) -> dict:
35
48
"/api/v1/tasks/interaction" ,
36
49
{
37
50
"type" : "text_reply_to_post" ,
38
- "post_id" : "1234" ,
39
- "user_post_id" : "5678" ,
51
+ "post_id" : POST_ID ,
52
+ "user_post_id" : USER_POST_ID ,
40
53
"text" : summary ,
41
- "user" : { "id" : "1234" , "name" : "John Doe" } ,
54
+ "user" : USER ,
42
55
},
43
56
)
44
57
tasks .append (new_task )
@@ -50,17 +63,17 @@ def _post(path: str, json: dict) -> dict:
50
63
typer .echo (f"Rating scale: { task ['scale' ]['min' ]} - { task ['scale' ]['max' ]} " )
51
64
52
65
# acknowledge task
53
- _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "rating_created" , "post_id" : "1234" })
66
+ _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "rating_created" , "post_id" : POST_ID })
54
67
55
68
rating = typer .prompt ("Enter your rating" , type = int )
56
69
# send interaction
57
70
new_task = _post (
58
71
"/api/v1/tasks/interaction" ,
59
72
{
60
73
"type" : "post_rating" ,
61
- "post_id" : "1234" ,
74
+ "post_id" : POST_ID ,
62
75
"rating" : rating ,
63
- "user" : { "id" : "1234" , "name" : "John Doe" } ,
76
+ "user" : USER ,
64
77
},
65
78
)
66
79
tasks .append (new_task )
@@ -69,23 +82,70 @@ def _post(path: str, json: dict) -> dict:
69
82
if task ["hint" ]:
70
83
typer .echo (f"Hint: { task ['hint' ]} " )
71
84
# acknowledge task
72
- _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : "1234" })
85
+ _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : POST_ID })
73
86
prompt = typer .prompt ("Enter your prompt" )
74
87
# send interaction
75
88
new_task = _post (
76
89
"/api/v1/tasks/interaction" ,
77
90
{
78
91
"type" : "text_reply_to_post" ,
79
- "post_id" : "1234" ,
80
- "user_post_id" : "5678" ,
92
+ "post_id" : POST_ID ,
93
+ "user_post_id" : USER_POST_ID ,
81
94
"text" : prompt ,
82
- "user" : {"id" : "1234" , "name" : "John Doe" },
95
+ "user" : USER ,
96
+ },
97
+ )
98
+ tasks .append (new_task )
99
+
100
+ case "user_reply" :
101
+ typer .echo ("Please provide a reply to the assistant." )
102
+ typer .echo ("Here is the conversation so far:" )
103
+ for message in task ["conversation" ]["messages" ]:
104
+ typer .echo (_render_message (message ))
105
+ if task ["hint" ]:
106
+ typer .echo (f"Hint: { task ['hint' ]} " )
107
+ # acknowledge task
108
+ _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : POST_ID })
109
+ reply = typer .prompt ("Enter your reply" )
110
+ # send interaction
111
+ new_task = _post (
112
+ "/api/v1/tasks/interaction" ,
113
+ {
114
+ "type" : "text_reply_to_post" ,
115
+ "post_id" : POST_ID ,
116
+ "user_post_id" : USER_POST_ID ,
117
+ "text" : reply ,
118
+ "user" : USER ,
119
+ },
120
+ )
121
+ tasks .append (new_task )
122
+
123
+ case "assistant_reply" :
124
+ typer .echo ("Act as the assistant and reply to the user." )
125
+ typer .echo ("Here is the conversation so far:" )
126
+ for message in task ["conversation" ]["messages" ]:
127
+ typer .echo (_render_message (message ))
128
+ # acknowledge task
129
+ _post (f"/api/v1/tasks/{ task ['id' ]} /ack" , {"type" : "post_created" , "post_id" : POST_ID })
130
+ reply = typer .prompt ("Enter your reply" )
131
+ # send interaction
132
+ new_task = _post (
133
+ "/api/v1/tasks/interaction" ,
134
+ {
135
+ "type" : "text_reply_to_post" ,
136
+ "post_id" : POST_ID ,
137
+ "user_post_id" : USER_POST_ID ,
138
+ "text" : reply ,
139
+ "user" : USER ,
83
140
},
84
141
)
85
142
tasks .append (new_task )
86
143
87
144
case "task_done" :
88
- typer .echo ("Task done!" )
145
+ if addressed_user := task ["addressed_user" ]:
146
+ typer .echo (f"Hey, { addressed_user ['display_name' ]} ! Thank you!" )
147
+ else :
148
+ typer .echo ("Task done!" )
89
149
case _:
90
150
typer .echo (f"Unknown task type { task ['type' ]} " )
91
151
0 commit comments