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
 
 
 
 
 
 
 
 
 
 
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""MCP Tool definitions for HR Onboarding/Offboarding environment.

Each tool is a callable that takes parameters and operates on the WorldState.
Tools are designed to mirror realistic enterprise HR/IT APIs.
"""

import json
from typing import Any, Callable
try:
    from .world import WorldState
except ImportError:
    from world import WorldState


TOOL_DEFINITIONS = [
    {
        "name": "hr_create_employee",
        "description": "Create a new employee record in the HR system. Requires name, department, level, and role. Returns the created employee record with generated emp_id.",
        "parameters": {
            "type": "object",
            "properties": {
                "name": {"type": "string", "description": "Full name of the employee"},
                "department": {"type": "string", "description": "Department name (Engineering, Product, Marketing, Sales, Finance, HR, Data Science, Security)"},
                "level": {"type": "string", "description": "Employee level (L1-L6)"},
                "role": {"type": "string", "description": "Job title/role"},
                "manager_id": {"type": "string", "description": "Employee ID of the manager"},
                "is_contractor": {"type": "boolean", "description": "Whether this is a contractor (default: false)"},
                "location": {"type": "string", "description": "Office location"},
                "date_of_joining": {"type": "string", "description": "Start date (YYYY-MM-DD)"},
            },
            "required": ["name", "department", "level", "role"],
        },
    },
    {
        "name": "hr_read_employee",
        "description": "Look up an employee by their employee ID or email address. Returns full employee record.",
        "parameters": {
            "type": "object",
            "properties": {
                "emp_id": {"type": "string", "description": "Employee ID (e.g. emp_0001)"},
                "email": {"type": "string", "description": "Employee email address"},
            },
        },
    },
    {
        "name": "hr_update_employee",
        "description": "Update fields on an existing employee record. Cannot modify emp_id.",
        "parameters": {
            "type": "object",
            "properties": {
                "emp_id": {"type": "string", "description": "Employee ID to update"},
                "updates": {"type": "object", "description": "Dictionary of fields to update (e.g. {status: 'active', department: 'Engineering'})"},
            },
            "required": ["emp_id", "updates"],
        },
    },
    {
        "name": "hr_search_employees",
        "description": "Search employees by criteria. Filter by department, level, status, location, or role.",
        "parameters": {
            "type": "object",
            "properties": {
                "department": {"type": "string", "description": "Filter by department"},
                "level": {"type": "string", "description": "Filter by level (L1-L6)"},
                "status": {"type": "string", "description": "Filter by status (active, pending, offboarded)"},
                "location": {"type": "string", "description": "Filter by location"},
                "role": {"type": "string", "description": "Filter by role/title"},
                "name": {"type": "string", "description": "Filter by name (exact match)"},
            },
        },
    },
    {
        "name": "hr_get_org_chart",
        "description": "Get the organizational hierarchy/reporting structure for a department. Shows manager-report relationships.",
        "parameters": {
            "type": "object",
            "properties": {
                "department": {"type": "string", "description": "Department name"},
            },
            "required": ["department"],
        },
    },
    {
        "name": "onboarding_create_request",
        "description": "Initiate an onboarding request for a new hire. The employee must exist in the HR system with 'pending' status. Creates a checklist of onboarding steps based on department.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID of the new hire"},
            },
            "required": ["employee_id"],
        },
    },
    {
        "name": "onboarding_get_status",
        "description": "Check the status of an onboarding request. Can look up by request ID or employee ID.",
        "parameters": {
            "type": "object",
            "properties": {
                "request_id": {"type": "string", "description": "Onboarding request ID"},
                "employee_id": {"type": "string", "description": "Employee ID"},
            },
        },
    },
    {
        "name": "onboarding_complete_step",
        "description": "Mark an onboarding step as completed. Steps must be valid for the request.",
        "parameters": {
            "type": "object",
            "properties": {
                "request_id": {"type": "string", "description": "Onboarding request ID"},
                "step": {"type": "string", "description": "Step name to mark as completed"},
            },
            "required": ["request_id", "step"],
        },
    },
    {
        "name": "offboarding_create_request",
        "description": "Initiate an offboarding request for a departing employee. Specify reason (resignation, termination, transfer) and optional exit date.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
                "reason": {"type": "string", "description": "Reason for offboarding (resignation, termination, transfer)"},
                "exit_date": {"type": "string", "description": "Last working date (YYYY-MM-DD)"},
            },
            "required": ["employee_id"],
        },
    },
    {
        "name": "offboarding_get_status",
        "description": "Check the status of an offboarding request. Can look up by request ID or employee ID.",
        "parameters": {
            "type": "object",
            "properties": {
                "request_id": {"type": "string", "description": "Offboarding request ID"},
                "employee_id": {"type": "string", "description": "Employee ID"},
            },
        },
    },
    {
        "name": "offboarding_complete_step",
        "description": "Mark an offboarding step as completed.",
        "parameters": {
            "type": "object",
            "properties": {
                "request_id": {"type": "string", "description": "Offboarding request ID"},
                "step": {"type": "string", "description": "Step name to mark as completed"},
            },
            "required": ["request_id", "step"],
        },
    },
    {
        "name": "it_assign_asset",
        "description": "Assign an IT asset (laptop, monitor, phone, headset) to an employee. Asset must be available.",
        "parameters": {
            "type": "object",
            "properties": {
                "asset_id": {"type": "string", "description": "Asset ID to assign"},
                "employee_id": {"type": "string", "description": "Employee ID to assign to"},
            },
            "required": ["asset_id", "employee_id"],
        },
    },
    {
        "name": "it_get_available_assets",
        "description": "List available (unassigned) IT assets. Optionally filter by type.",
        "parameters": {
            "type": "object",
            "properties": {
                "asset_type": {"type": "string", "description": "Filter by asset type (laptop, monitor, phone, headset)"},
            },
        },
    },
    {
        "name": "it_create_account",
        "description": "Create IT accounts (email, Slack, VPN, etc.) for an employee.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
                "account_types": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Types of accounts to create (email, slack, vpn, github, jira, etc.)",
                },
            },
            "required": ["employee_id"],
        },
    },
    {
        "name": "it_revoke_access",
        "description": "Revoke all IT system access for an employee (used during offboarding).",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
            },
            "required": ["employee_id"],
        },
    },
    {
        "name": "it_get_software_licenses",
        "description": "Check software license availability. Optionally filter by software name.",
        "parameters": {
            "type": "object",
            "properties": {
                "software_name": {"type": "string", "description": "Filter by software name"},
            },
        },
    },
    {
        "name": "access_assign_role",
        "description": "Assign an access role to an employee. Checks level requirements and department restrictions.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
                "role_id": {"type": "string", "description": "Access role ID to assign"},
            },
            "required": ["employee_id", "role_id"],
        },
    },
    {
        "name": "access_create_badge",
        "description": "Create a physical access badge for an employee. Server room access requires L4+ security approval.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
                "access_zones": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "List of access zones (main_entrance, floor_X, server_room, parking)",
                },
            },
            "required": ["employee_id"],
        },
    },
    {
        "name": "access_revoke_role",
        "description": "Revoke a specific access role from an employee.",
        "parameters": {
            "type": "object",
            "properties": {
                "employee_id": {"type": "string", "description": "Employee ID"},
                "role_id": {"type": "string", "description": "Access role ID to revoke"},
            },
            "required": ["employee_id", "role_id"],
        },
    },
    {
        "name": "access_get_security_groups",
        "description": "List all security groups. Optionally see members and resources.",
        "parameters": {
            "type": "object",
            "properties": {},
        },
    },
    {
        "name": "email_send",
        "description": "Send an email. Used for welcome emails, notifications, farewell messages, etc.",
        "parameters": {
            "type": "object",
            "properties": {
                "from_address": {"type": "string", "description": "Sender email address"},
                "to_address": {"type": "string", "description": "Recipient email address"},
                "subject": {"type": "string", "description": "Email subject line"},
                "body": {"type": "string", "description": "Email body text"},
            },
            "required": ["from_address", "to_address", "subject", "body"],
        },
    },
    {
        "name": "slack_send_message",
        "description": "Post a message in a Slack channel or send a DM.",
        "parameters": {
            "type": "object",
            "properties": {
                "channel": {"type": "string", "description": "Slack channel (e.g. #engineering, #general, @username)"},
                "sender": {"type": "string", "description": "Sender username or bot name"},
                "text": {"type": "string", "description": "Message text"},
            },
            "required": ["channel", "sender", "text"],
        },
    },
    {
        "name": "meeting_schedule",
        "description": "Schedule a meeting (orientation, 1-on-1, exit interview, etc.).",
        "parameters": {
            "type": "object",
            "properties": {
                "title": {"type": "string", "description": "Meeting title"},
                "attendees": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "List of attendee employee IDs or emails",
                },
                "datetime": {"type": "string", "description": "Meeting date and time (ISO format)"},
                "meeting_type": {"type": "string", "description": "Type of meeting (orientation, one_on_one, exit_interview, team_intro)"},
            },
            "required": ["title", "attendees", "datetime"],
        },
    },
    {
        "name": "policy_lookup",
        "description": "Look up company policies by topic or department. Returns policy content and key rules.",
        "parameters": {
            "type": "object",
            "properties": {
                "topic": {"type": "string", "description": "Policy topic to search for"},
                "department": {"type": "string", "description": "Department-specific policies"},
                "policy_id": {"type": "string", "description": "Specific policy ID"},
            },
        },
    },
    {
        "name": "approval_request",
        "description": "Submit an approval request (manager approval, IT approval, security approval). Approver must meet level requirements.",
        "parameters": {
            "type": "object",
            "properties": {
                "request_id": {"type": "string", "description": "The onboarding/offboarding request ID this approval is for"},
                "approver_id": {"type": "string", "description": "Employee ID of the approver"},
                "approval_type": {"type": "string", "description": "Type of approval (manager_approval, it_approval, security_approval, legal_approval)"},
            },
            "required": ["request_id", "approver_id", "approval_type"],
        },
    },
]


class ToolRegistry:
    """Registry of all available tools, mapping names to executors."""

    def __init__(self, world: WorldState):
        self.world = world
        self._tools: dict[str, Callable] = self._register_tools()

    def _register_tools(self) -> dict[str, Callable]:
        return {
            "hr_create_employee": self._hr_create_employee,
            "hr_read_employee": self._hr_read_employee,
            "hr_update_employee": self._hr_update_employee,
            "hr_search_employees": self._hr_search_employees,
            "hr_get_org_chart": self._hr_get_org_chart,
            "onboarding_create_request": self._onboarding_create_request,
            "onboarding_get_status": self._onboarding_get_status,
            "onboarding_complete_step": self._onboarding_complete_step,
            "offboarding_create_request": self._offboarding_create_request,
            "offboarding_get_status": self._offboarding_get_status,
            "offboarding_complete_step": self._offboarding_complete_step,
            "it_assign_asset": self._it_assign_asset,
            "it_get_available_assets": self._it_get_available_assets,
            "it_create_account": self._it_create_account,
            "it_revoke_access": self._it_revoke_access,
            "it_get_software_licenses": self._it_get_software_licenses,
            "access_assign_role": self._access_assign_role,
            "access_create_badge": self._access_create_badge,
            "access_revoke_role": self._access_revoke_role,
            "access_get_security_groups": self._access_get_security_groups,
            "email_send": self._email_send,
            "slack_send_message": self._slack_send_message,
            "meeting_schedule": self._meeting_schedule,
            "policy_lookup": self._policy_lookup,
            "approval_request": self._approval_request,
        }

    @property
    def tool_definitions(self) -> list[dict]:
        return TOOL_DEFINITIONS

    @property
    def tool_names(self) -> list[str]:
        return list(self._tools.keys())

    def execute(self, tool_name: str, params: dict) -> dict:
        """Execute a tool by name with given parameters. Returns result dict."""
        if tool_name not in self._tools:
            return {"success": False, "error": f"Unknown tool: {tool_name}. Available tools: {self.tool_names}"}
        try:
            result = self._tools[tool_name](params)
            self.world.log_action(tool_name, params, result)
            return result
        except Exception as e:
            error_result = {"success": False, "error": f"Tool execution error: {str(e)}"}
            self.world.log_action(tool_name, params, error_result)
            return error_result

    # ---- Tool Implementations ----

    def _hr_create_employee(self, params: dict) -> dict:
        return self.world.create_employee(params)

    def _hr_read_employee(self, params: dict) -> dict:
        emp = None
        if "emp_id" in params:
            emp = self.world.get_employee(params["emp_id"])
        elif "email" in params:
            emp = self.world.get_employee_by_email(params["email"])
        if emp:
            return {"success": True, "employee": emp}
        return {"success": False, "error": "Employee not found"}

    def _hr_update_employee(self, params: dict) -> dict:
        return self.world.update_employee(params["emp_id"], params["updates"])

    def _hr_search_employees(self, params: dict) -> dict:
        results = self.world.search_employees(**params)
        return {"success": True, "count": len(results), "employees": results}

    def _hr_get_org_chart(self, params: dict) -> dict:
        chart = self.world.get_org_chart(params["department"])
        if chart:
            return {"success": True, "org_chart": chart}
        return {"success": False, "error": f"No employees found in department '{params['department']}'"}

    def _onboarding_create_request(self, params: dict) -> dict:
        return self.world.create_onboarding_request(params["employee_id"])

    def _onboarding_get_status(self, params: dict) -> dict:
        status = self.world.get_onboarding_status(
            request_id=params.get("request_id"),
            emp_id=params.get("employee_id"),
        )
        if status:
            return {"success": True, "onboarding_status": status}
        return {"success": False, "error": "Onboarding request not found"}

    def _onboarding_complete_step(self, params: dict) -> dict:
        return self.world.complete_onboarding_step(params["request_id"], params["step"])

    def _offboarding_create_request(self, params: dict) -> dict:
        return self.world.create_offboarding_request(
            params["employee_id"],
            reason=params.get("reason", "resignation"),
            exit_date=params.get("exit_date"),
        )

    def _offboarding_get_status(self, params: dict) -> dict:
        status = self.world.get_offboarding_status(
            request_id=params.get("request_id"),
            emp_id=params.get("employee_id"),
        )
        if status:
            return {"success": True, "offboarding_status": status}
        return {"success": False, "error": "Offboarding request not found"}

    def _offboarding_complete_step(self, params: dict) -> dict:
        return self.world.complete_offboarding_step(params["request_id"], params["step"])

    def _it_assign_asset(self, params: dict) -> dict:
        return self.world.assign_asset(params["asset_id"], params["employee_id"])

    def _it_get_available_assets(self, params: dict) -> dict:
        assets = self.world.get_available_assets(params.get("asset_type"))
        return {"success": True, "count": len(assets), "assets": assets}

    def _it_create_account(self, params: dict) -> dict:
        return self.world.create_it_account(
            params["employee_id"],
            account_types=params.get("account_types"),
        )

    def _it_revoke_access(self, params: dict) -> dict:
        return self.world.revoke_it_access(params["employee_id"])

    def _it_get_software_licenses(self, params: dict) -> dict:
        licenses = self.world.get_software_licenses(params.get("software_name"))
        return {"success": True, "licenses": licenses}

    def _access_assign_role(self, params: dict) -> dict:
        return self.world.assign_role(params["employee_id"], params["role_id"])

    def _access_create_badge(self, params: dict) -> dict:
        return self.world.create_badge(
            params["employee_id"],
            access_zones=params.get("access_zones"),
        )

    def _access_revoke_role(self, params: dict) -> dict:
        return self.world.revoke_role(params["employee_id"], params["role_id"])

    def _access_get_security_groups(self, params: dict) -> dict:
        return {"success": True, "security_groups": self.world.state["security_groups"]}

    def _email_send(self, params: dict) -> dict:
        return self.world.send_email(
            params["from_address"], params["to_address"],
            params["subject"], params["body"],
        )

    def _slack_send_message(self, params: dict) -> dict:
        return self.world.send_slack_message(
            params["channel"], params["sender"], params["text"],
        )

    def _meeting_schedule(self, params: dict) -> dict:
        return self.world.schedule_meeting(
            params["title"], params["attendees"],
            params["datetime"], params.get("meeting_type", "general"),
        )

    def _policy_lookup(self, params: dict) -> dict:
        policies = self.world.lookup_policy(
            topic=params.get("topic"),
            department=params.get("department"),
            policy_id=params.get("policy_id"),
        )
        return {"success": True, "count": len(policies), "policies": policies}

    def _approval_request(self, params: dict) -> dict:
        return self.world.create_approval(
            params["request_id"], params["approver_id"], params["approval_type"],
        )