Repository navigation
Expand file tree
/
Copy pathreact.ts
More file actions
122 lines (110 loc) · 4.14 KB
/
Copy pathreact.ts
File metadata and controls
122 lines (110 loc) · 4.14 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
import { getFullCommsURL } from '@todoist/comms-sdk'
import { z } from 'zod'
import type { CommsTool } from '../comms-tool.js'
import { ReactOutputSchema } from '../utils/output-schemas.js'
import { type ReactionTargetType, ReactionTargetTypeSchema } from '../utils/target-types.js'
import { ToolNames } from '../utils/tool-names.js'
const ArgsSchema = {
targetType: ReactionTargetTypeSchema.describe(
'The type of object to react to: thread, comment, or message.',
),
targetId: z.string().describe('The ID of the thread, comment, or message to react to.'),
emoji: z.string().min(1).describe('The emoji to react with (e.g., "👍", "❤️", "🎉").'),
operation: z
.enum(['add', 'remove'])
.default('add')
.describe('Whether to add or remove the reaction.'),
}
type ReactStructured = {
type: 'reaction_result'
success: boolean
operation: 'add' | 'remove'
targetType: ReactionTargetType
targetId: string
emoji: string
targetUrl: string
}
const react = {
name: ToolNames.REACT,
description:
'Add or remove an emoji reaction on a thread, comment, or conversation message. Use targetType to specify the type of object (thread, comment, or message) and targetId for the ID.',
parameters: ArgsSchema,
outputSchema: ReactOutputSchema.shape,
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false },
async execute(args, client) {
const { targetType, targetId, emoji, operation } = args
let targetUrl: string
// Fetch target metadata to get URL
if (targetType === 'thread') {
const thread = await client.threads.getThread(targetId)
targetUrl =
thread.url ??
getFullCommsURL({
workspaceId: thread.workspaceId,
channelId: thread.channelId,
threadId: thread.id,
})
} else if (targetType === 'comment') {
const comment = await client.comments.getComment(targetId)
targetUrl =
comment.url ??
getFullCommsURL({
workspaceId: comment.workspaceId,
channelId: comment.channelId,
threadId: comment.threadId,
commentId: comment.id,
})
} else {
// message
const message = await client.conversationMessages.getMessage(targetId)
targetUrl =
message.url ??
getFullCommsURL({
workspaceId: message.workspaceId,
conversationId: message.conversationId,
messageId: message.id,
})
}
// Map targetType to the appropriate API parameter
const apiParams: {
threadId?: string
commentId?: string
messageId?: string
reaction: string
} = { reaction: emoji }
if (targetType === 'thread') {
apiParams.threadId = targetId
} else if (targetType === 'comment') {
apiParams.commentId = targetId
} else {
apiParams.messageId = targetId
}
// Perform the reaction operation
if (operation === 'add') {
await client.reactions.add({ ...apiParams, reaction: emoji })
} else {
await client.reactions.remove({ ...apiParams, reaction: emoji })
}
const lines: string[] = [
`# Reaction ${operation === 'add' ? 'Added' : 'Removed'}`,
'',
`**Target:** ${targetType} ${targetId}`,
`**Emoji:** ${emoji}`,
`**Operation:** ${operation}`,
]
const structuredContent: ReactStructured = {
type: 'reaction_result',
success: true,
operation,
targetType,
targetId,
emoji,
targetUrl,
}
return {
textContent: lines.join('\n'),
structuredContent,
}
},
} satisfies CommsTool<typeof ArgsSchema, typeof ReactOutputSchema.shape>
export { react, type ReactStructured }