Repository navigation
Expand file tree
/
Copy pathget-users.ts
More file actions
153 lines (138 loc) · 5.02 KB
/
Copy pathget-users.ts
File metadata and controls
153 lines (138 loc) · 5.02 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
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
import {
isRestrictedWorkspaceUser,
type UserType,
type VisibleWorkspaceUser,
} from '@todoist/comms-sdk'
import { z } from 'zod'
import type { CommsTool } from '../comms-tool.js'
import { resolveUsersWithLog } from '../utils/degrade.js'
import { GetUsersOutputSchema } from '../utils/output-schemas.js'
import { ToolNames } from '../utils/tool-names.js'
const ArgsSchema = {
workspaceId: z.number().describe('The workspace ID to get users from.'),
userIds: z
.array(z.number())
.optional()
.describe(
'Optional array of specific user IDs to fetch. If not provided or empty array, fetches all workspace users.',
),
searchText: z
.string()
.optional()
.describe('Optional search text to filter users by name or email (case-insensitive).'),
}
type UserData = {
id: number
name: string
shortName: string
email?: string
userType?: UserType
removed: boolean
timezone?: string
restricted?: boolean
}
type GetUsersStructured = Record<string, unknown> & {
type: 'get_users'
workspaceId: number
users: UserData[]
totalUsers: number
filteredUsers: number
}
// A restricted profile carries no email, user type or timezone, so those stay
// absent rather than being filled with a placeholder.
function toUserData(user: VisibleWorkspaceUser): UserData {
const base = {
id: user.id,
name: user.fullName,
shortName: user.shortName,
removed: user.removed,
}
if (isRestrictedWorkspaceUser(user)) {
return { ...base, restricted: true }
}
return {
...base,
...(user.email && { email: user.email }),
userType: user.userType,
timezone: user.timezone,
}
}
const getUsers = {
name: ToolNames.GET_USERS,
description:
'Get users from a workspace. Retrieves all workspace users by default, or specific users if userIds array is provided. Supports optional case-insensitive search filtering by name or email.',
parameters: ArgsSchema,
outputSchema: GetUsersOutputSchema.shape,
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true },
async execute(args, client) {
const { workspaceId, userIds, searchText } = args
let users: VisibleWorkspaceUser[]
if (!userIds || userIds.length === 0) {
users = await client.workspaceUsers.getWorkspaceUsers({ workspaceId })
} else {
const byId = await resolveUsersWithLog(
client,
ToolNames.GET_USERS,
'failed to resolve user',
workspaceId,
userIds,
)
users = userIds.flatMap((id) => byId.get(id) ?? [])
}
const totalUsers = users.length
// Apply search filter if provided, before building output objects
let matchedUsers = users
if (searchText) {
const searchLower = searchText.toLowerCase()
matchedUsers = users.filter((user) => {
const nameMatch = user.fullName.toLowerCase().includes(searchLower)
const email = isRestrictedWorkspaceUser(user) ? undefined : user.email
const emailMatch = email?.toLowerCase().includes(searchLower) || false
return nameMatch || emailMatch
})
}
const filteredUsers = matchedUsers.map(toUserData)
// Build text content
const lines: string[] = ['# Workspace Users', '']
lines.push(`**Workspace ID:** ${workspaceId}`)
lines.push(
`**Total Users:** ${totalUsers}${searchText ? ` (${filteredUsers.length} matching search)` : ''}`,
)
lines.push('')
if (filteredUsers.length === 0) {
lines.push('No users found.')
} else {
for (const user of filteredUsers) {
lines.push(`## ${user.name}`)
lines.push(`**ID:** ${user.id}`)
if (user.email) {
lines.push(`**Email:** ${user.email}`)
}
if (user.userType) {
lines.push(`**User Type:** ${user.userType}`)
}
if (user.timezone) {
lines.push(`**Timezone:** ${user.timezone}`)
}
if (user.restricted) {
lines.push('**Profile:** Restricted, limited visibility of this user')
}
lines.push(`**Status:** ${user.removed ? 'Removed' : 'Active'}`)
lines.push('')
}
}
const textContent = lines.join('\n')
const structuredContent: GetUsersStructured = {
type: 'get_users',
workspaceId,
users: filteredUsers,
totalUsers,
filteredUsers: filteredUsers.length,
}
return {
textContent,
structuredContent,
}
},
} satisfies CommsTool<typeof ArgsSchema, typeof GetUsersOutputSchema.shape>
export { getUsers, type GetUsersStructured }