File size: 14,039 Bytes
102dd4f
 
 
 
cf6d08c
102dd4f
 
cf6d08c
102dd4f
c925062
 
102dd4f
 
 
 
 
 
cf6d08c
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a802c
6ab175e
 
102dd4f
 
 
 
 
 
6ab175e
102dd4f
 
 
6ab175e
102dd4f
 
 
6ab175e
102dd4f
 
 
6ab175e
102dd4f
 
 
 
6ab175e
102dd4f
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
b5a802c
 
 
 
102dd4f
 
6ab175e
102dd4f
 
cc10684
102dd4f
 
 
 
 
edb038d
102dd4f
 
edb038d
102dd4f
edb038d
 
6ab175e
102dd4f
 
 
 
6ab175e
102dd4f
 
 
0467de1
102dd4f
 
 
 
 
 
0467de1
102dd4f
 
 
 
 
 
 
 
 
cf6d08c
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
fcf2812
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ab175e
 
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
96f3b64
 
 
 
 
102dd4f
 
 
6ab175e
102dd4f
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb9b63f
 
 
c925062
 
 
 
bb9b63f
 
102dd4f
 
 
 
 
 
 
dc8cb49
 
 
 
fcf2812
dc8cb49
 
 
 
 
 
 
 
 
 
6ab175e
dc8cb49
 
 
 
6ab175e
dc8cb49
 
 
 
 
 
 
 
 
 
 
 
7629a0e
dc8cb49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7629a0e
dc8cb49
 
 
7629a0e
dc8cb49
 
 
 
 
 
 
 
102dd4f
 
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
6ab175e
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6d08c
102dd4f
edb038d
102dd4f
 
 
 
 
dc8cb49
 
102dd4f
 
 
 
 
 
cf6d08c
102dd4f
 
 
 
 
cf6d08c
102dd4f
 
 
 
dc8cb49
 
102dd4f
 
 
cf6d08c
102dd4f
edb038d
102dd4f
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from typing import Any

from app.models.domain import UserMode

_ABOUT_FALZH = {
    "type": "function",
    "function": {
        "name": "about_falzh",
        "description": (
            "Retrieve official FALZH company, FAQ, policy, pricing, or feature information. "
            "Use before answering any FALZH capability/request you are unsure about."
        ),
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The customer's question about FALZH.",
                },
                "language": {
                    "type": "string",
                    "enum": ["ar", "en"],
                    "description": "Customer language for the result.",
                },
            },
            "required": ["query", "language"],
            "additionalProperties": False,
        },
    },
}

_SEARCH_TRIPS = {
    "type": "function",
    "function": {
        "name": "search_trips",
        "description": (
            "Search active trips by departure/destination/driver name (any can be omitted). "
            "Results sent as WhatsApp cards automatically. "
            "Times Asia/Aden. Buckets: صباح before 12, ظهر 12-17:59, ليل 18+."
        ),
        "parameters": {
            "type": "object",
            "properties": {
                "departure": {
                    "type": "string",
                    "description": "Departure city/area in Arabic. Required if destination omitted.",
                },
                "destination": {
                    "type": "string",
                    "description": "Destination city/area in Arabic. Required if departure omitted.",
                },
                "travel_datetime": {
                    "type": "string",
                    "description": "Optional ISO date+time in Asia/Aden; normalized to the matching time bucket.",
                },
                "travel_date": {
                    "type": "string",
                    "description": "Optional trip date YYYY-MM-DD (Asia/Aden).",
                },
                "travel_time": {
                    "type": "string",
                    "enum": ["صباح", "ظهر", "ليل"],
                    "description": "Optional time bucket (Arabic).",
                },
                "travel_time_exact": {
                    "type": "string",
                    "description": "Optional exact time HH:MM (Asia/Aden). Also set the corresponding travel_time bucket.",
                },
                "seats": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Optional number of seats requested.",
                },
                "vehicle_type": {
                    "type": "string",
                    "description": "Optional car type in Arabic, for example سيارة or باص.",
                },
                "driver_name": {
                    "type": "string",
                    "description": "Optional driver name to filter trips by driver.",
                },
                "vector_query_text": {
                    "type": "string",
                    "description": "Optional semantic text; auto-built from other fields.",
                },
            },
            "required": [],
            "additionalProperties": False,
        },
    },
}

_SELECT_TRIP = {
    "type": "function",
    "function": {
        "name": "select_trip",
        "description": (
            "Record customer interest in a trip (default 1 seat), notify driver, "
            "and return driver phone number so they can coordinate directly. "
            "Call on trip-card reply. Does not reserve or confirm."
        ),
        "parameters": {
            "type": "object",
            "properties": {
                "trip_id": {"type": "string", "description": "Trip ID. Optional if replying to a trip card."},
                "requested_seats": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Number of seats requested. Defaults to 1 if not specified.",
                },
                "notes": {
                    "type": "string",
                    "description": "Optional customer notes or pickup details.",
                },
            },
            "required": [],
            "additionalProperties": False,
        },
    },
}

_CREATE_DRIVER_ACCOUNT = {
    "type": "function",
    "function": {
        "name": "create_driver_account",
        "description": "Register this WhatsApp sender as a FALZH driver. Phone from chat session.",
        "parameters": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Driver full legal name.",
                },
            },
            "required": ["name"],
            "additionalProperties": False,
        },
    },
}

_CHECK_DRIVER_INFO = {
    "type": "function",
    "function": {
        "name": "check_driver_info",
        "description": "Get driver account, vehicles & active trip summary.",
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
            "additionalProperties": False,
        },
    },
}

_CHECK_DRIVER_TRIPS = {
    "type": "function",
    "function": {
        "name": "check_driver_trips",
        "description": "List upcoming active trips. Sends trip cards directly.",
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
            "additionalProperties": False,
        },
    },
}

_ADD_DRIVER_CAR = {
    "type": "function",
    "function": {
        "name": "add_driver_car",
        "description": "Register vehicle for current driver. Only name required.",
        "parameters": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Vehicle name or type in Arabic.",
                },
                "plate_number": {
                    "type": "string",
                    "description": "Optional vehicle plate number.",
                },
                "seat_count": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Optional number of seats in the vehicle.",
                },
            },
            "required": ["name"],
            "additionalProperties": False,
        },
    },
}

_ADD_TRIP_BY_DRIVER = {
    "type": "function",
    "function": {
        "name": "add_trip_by_driver",
        "description": (
            "Create a trip for the registered driver. Phone from chat session. "
            "Optional car/seats/price default from latest trip or sole registered vehicle."
        ),
        "parameters": {
            "type": "object",
            "properties": {
                "departure": {
                    "type": "string",
                    "description": "Departure city or area in Arabic.",
                },
                "destination": {
                    "type": "string",
                    "description": "Destination city or area in Arabic.",
                },
                "departure_date": {
                    "type": "string",
                    "description": (
                        "Trip date as YYYY-MM-DD in Asia/Aden. "
                        "Convert natural expressions (اليوم/بكرة/غدا/يوم الخميس etc.) "
                        "to YYYY-MM-DD using the current date from system context."
                    ),
                },
                "departure_time": {
                    "type": "string",
                    "description": "Time bucket: morning/noon/night or صباح/ظهر/ليل.",
                },
                "vehicle_type": {
                    "type": "string",
                    "description": "Optional vehicle name/type in Arabic (e.g. سيارة/باص).",
                },
                "available_seats": {
                    "type": "integer",
                    "minimum": 0,
                    "description": "Optional seats available for booking.",
                },
                "total_seats": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Optional total vehicle seats for this trip.",
                },
                "price": {
                    "type": "number",
                    "minimum": 0,
                    "description": "Optional trip price.",
                },
                "driver_message": {
                    "type": "string",
                    "description": (
                        "Optional cleaned version of the driver's original trip ad when the "
                        "driver wrote it in a style likely meant for passengers. Remove phone "
                        "numbers, personal info, and driver name. Keep route info, dates, "
                        "times, emojis, and decorations."
                    ),
                },
            },
            "required": ["departure", "destination", "departure_date", "departure_time"],
            "additionalProperties": False,
        },
    },
}

_INITIATE_TRIP_ACTION = {
    "type": "function",
    "function": {
        "name": "initiate_trip_action",
        "description": "Start delete/modify flow. Sends trip cards — driver replies to a card to pick the trip.",
        "parameters": {
            "type": "object",
            "properties": {
                "action_type": {
                    "type": "string",
                    "enum": ["DELETE", "MODIFY"],
                    "description": "Whether the driver wants to cancel or edit a trip.",
                },
                "travel_date": {
                    "type": "string",
                    "description": "Optional trip date filter YYYY-MM-DD (Asia/Aden).",
                },
                "travel_time": {
                    "type": "string",
                    "enum": ["صباح", "ظهر", "ليل"],
                    "description": "Departure time bucket in Arabic.",
                },
            },
            "required": ["action_type"],
            "additionalProperties": False,
        },
    },
}

_UPDATE_TRIP_FIELD = {
    "type": "function",
    "function": {
        "name": "update_trip_field",
        "description": "Update one field on the trip the driver is editing. trip_id from active session. Note: departure_date cannot be changed once the trip is created.",
        "parameters": {
            "type": "object",
            "properties": {
                "field": {
                    "type": "string",
                    "enum": [
                        "departure",
                        "destination",
                        "departure_time",
                        "pickup_time",
                        "vehicle_type",
                        "available_seats",
                        "total_seats",
                        "price",
                    ],
                    "description": "Trip field to update. departure_date is not allowed.",
                },
                "value": {
                    "type": "string",
                    "description": "New value. time HH:MM, routes Arabic, seats/price digits. Do NOT include date — departure_date cannot be changed.",
                },
            },
            "required": ["field", "value"],
            "additionalProperties": False,
        },
    },
}

_SWITCH_TO_DRIVER = {
    "type": "function",
    "function": {
        "name": "switch_to_driver",
        "description": "Switch sender to driver mode. Requires existing driver account.",
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
            "additionalProperties": False,
        },
    },
}

_SWITCH_TO_PASSENGER = {
    "type": "function",
    "function": {
        "name": "switch_to_passenger",
        "description": "Switch sender to passenger mode to search/book trips.",
        "parameters": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Optional passenger display name.",
                },
            },
            "required": [],
            "additionalProperties": False,
        },
    },
}

_TOOL_SCHEMAS: dict[str, dict[str, Any]] = {
    "about_falzh": _ABOUT_FALZH,
    "search_trips": _SEARCH_TRIPS,
    "select_trip": _SELECT_TRIP,
    "create_driver_account": _CREATE_DRIVER_ACCOUNT,
    "check_driver_info": _CHECK_DRIVER_INFO,
    "check_driver_trips": _CHECK_DRIVER_TRIPS,
    "add_driver_car": _ADD_DRIVER_CAR,
    "add_trip_by_driver": _ADD_TRIP_BY_DRIVER,
    "initiate_trip_action": _INITIATE_TRIP_ACTION,
    "update_trip_field": _UPDATE_TRIP_FIELD,
    "switch_to_driver": _SWITCH_TO_DRIVER,
    "switch_to_passenger": _SWITCH_TO_PASSENGER,
}

_TOOLS_BY_MODE: dict[UserMode, list[str]] = {
    "new_user": [
        "about_falzh",
        "create_driver_account",
        "switch_to_driver",
        "switch_to_passenger",
    ],
    "driver": [
        "about_falzh",
        "check_driver_info",
        "check_driver_trips",
        "add_driver_car",
        "add_trip_by_driver",
        "initiate_trip_action",
        "update_trip_field",
        "switch_to_passenger",
    ],
    "passenger": [
        "about_falzh",
        "search_trips",
        "select_trip",
        "create_driver_account",
        "switch_to_driver",
    ],
}


def get_tool_schemas(user_mode: UserMode = "new_user") -> list[dict[str, Any]]:
    return [_TOOL_SCHEMAS[name] for name in _TOOLS_BY_MODE[user_mode]]


def get_all_tool_schemas() -> list[dict[str, Any]]:
    return list(_TOOL_SCHEMAS.values())