Repository navigation
Expand file tree
/
Copy pathupdate-object.ts
More file actions
208 lines (179 loc) · 7.49 KB
/
Copy pathupdate-object.ts
File metadata and controls
208 lines (179 loc) · 7.49 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
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
import { getFullCommsURL, type CommsApi } from '@todoist/comms-sdk'
import { z } from 'zod'
import type { CommsTool } from '../comms-tool.js'
import {
type UpdateCommentOutput,
type UpdateMessageOutput,
UpdateObjectOutputSchema,
type UpdateObjectStructured,
type UpdateThreadOutput,
} from '../utils/output-schemas.js'
import { UpdateTargetTypeSchema } from '../utils/target-types.js'
import { ToolNames } from '../utils/tool-names.js'
const ArgsSchema = {
targetType: UpdateTargetTypeSchema.describe(
'The type of object to update: thread, comment, or message.',
),
targetId: z
.string()
.describe('The ID of the thread, comment, or conversation message to update.'),
content: z
.string()
.min(1)
.optional()
.describe(
'The new content/body. Required for comments and messages; for threads, optional if title is provided. Markdown. Mention people with the link syntax [Name](comms-mention://USER_ID) — e.g. [Afzal](comms-mention://29367677) — never @Name or [[Name|id]], which post as literal text. Do not put "@" in the label; the client adds it. Groups use [Name](comms-group-mention://GROUP_ID), channels [#name](comms-channel://CHANNEL_ID), threads [Title](comms-thread://THREAD_ID). Resolve IDs with get-users/get-groups/list-channels first. Preserve any existing mention links you are not deliberately changing — rewriting them as plain text drops the mention. Editing sends no notifications: this tool has no recipients/groups/notifyAudience, so a mention added here is rendering only.',
),
title: z
.string()
.min(1)
.optional()
.describe('The new title. Only valid when targetType is "thread".'),
}
type Args = z.infer<z.ZodObject<typeof ArgsSchema>>
type Branch = { textContent: string; structuredContent: UpdateObjectStructured }
async function updateThreadBranch(args: Args, client: CommsApi): Promise<Branch> {
const { targetId, title, content } = args
if (title === undefined && content === undefined) {
throw new Error('At least one of `title` or `content` must be provided.')
}
const thread = await client.threads.updateThread({ id: targetId, title, content })
const threadUrl =
thread.url ??
getFullCommsURL({
workspaceId: thread.workspaceId,
channelId: thread.channelId,
threadId: thread.id,
})
const lastEdited = thread.lastEdited ? thread.lastEdited.toISOString() : undefined
const lines: string[] = [
`# Thread Updated`,
'',
`**Title:** ${thread.title}`,
`**Thread ID:** ${thread.id}`,
`**Channel ID:** ${thread.channelId}`,
...(lastEdited ? [`**Last Edited:** ${lastEdited}`] : []),
`**URL:** ${threadUrl}`,
'',
'## Content',
'',
thread.content,
]
const structuredContent: UpdateThreadOutput = {
type: 'update_thread_result',
success: true,
threadId: thread.id,
title: thread.title,
channelId: thread.channelId,
workspaceId: thread.workspaceId,
content: thread.content,
threadUrl,
lastEdited,
}
return { textContent: lines.join('\n'), structuredContent }
}
async function updateCommentBranch(args: Args, client: CommsApi): Promise<Branch> {
const { targetId, content } = args
if (content === undefined) {
throw new Error('`content` is required when targetType is "comment".')
}
const comment = await client.comments.updateComment({ id: targetId, content })
const commentUrl =
comment.url ??
getFullCommsURL({
workspaceId: comment.workspaceId,
channelId: comment.channelId,
threadId: comment.threadId,
commentId: comment.id,
})
const lastEdited = comment.lastEdited ? comment.lastEdited.toISOString() : undefined
const lines: string[] = [
`# Comment Updated`,
'',
`**Comment ID:** ${comment.id}`,
`**Thread ID:** ${comment.threadId}`,
`**Channel ID:** ${comment.channelId}`,
...(lastEdited ? [`**Last Edited:** ${lastEdited}`] : []),
`**URL:** ${commentUrl}`,
'',
'## Content',
'',
comment.content,
]
const structuredContent: UpdateCommentOutput = {
type: 'update_comment_result',
success: true,
commentId: comment.id,
threadId: comment.threadId,
channelId: comment.channelId,
workspaceId: comment.workspaceId,
content: comment.content,
commentUrl,
lastEdited,
}
return { textContent: lines.join('\n'), structuredContent }
}
async function updateMessageBranch(args: Args, client: CommsApi): Promise<Branch> {
const { targetId, content } = args
if (content === undefined) {
throw new Error('`content` is required when targetType is "message".')
}
const message = await client.conversationMessages.updateMessage({ id: targetId, content })
const messageUrl =
message.url ??
getFullCommsURL({
workspaceId: message.workspaceId,
conversationId: message.conversationId,
messageId: message.id,
})
const lastEdited = message.lastEdited ? message.lastEdited.toISOString() : undefined
const lines: string[] = [
`# Message Updated`,
'',
`**Message ID:** ${message.id}`,
`**Conversation ID:** ${message.conversationId}`,
`**Workspace ID:** ${message.workspaceId}`,
...(lastEdited ? [`**Last Edited:** ${lastEdited}`] : []),
`**URL:** ${messageUrl}`,
'',
'## Content',
'',
message.content,
]
const structuredContent: UpdateMessageOutput = {
type: 'update_message_result',
success: true,
messageId: message.id,
conversationId: message.conversationId,
workspaceId: message.workspaceId,
content: message.content,
messageUrl,
lastEdited,
}
return { textContent: lines.join('\n'), structuredContent }
}
const updateObject = {
name: ToolNames.UPDATE_OBJECT,
description:
'Update an existing Comms object. `targetType: "thread"` updates a thread\'s title and/or body; `"comment"` updates a thread comment\'s body; `"message"` updates a direct/group conversation message\'s body. Always pass `targetId`. `content` is required for `"comment"` and `"message"`; for `"thread"` it is optional as long as `title` is provided (i.e. a thread can be renamed without re-sending the body). `title` is only valid when `targetType` is `"thread"`.',
parameters: ArgsSchema,
outputSchema: UpdateObjectOutputSchema.shape,
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true },
async execute(args, client) {
const { targetType, title } = args
if (targetType !== 'thread' && title !== undefined) {
throw new Error('`title` is only valid when targetType is "thread".')
}
const branch =
targetType === 'thread'
? await updateThreadBranch(args, client)
: targetType === 'comment'
? await updateCommentBranch(args, client)
: await updateMessageBranch(args, client)
return {
textContent: branch.textContent,
structuredContent: branch.structuredContent,
}
},
} satisfies CommsTool<typeof ArgsSchema, typeof UpdateObjectOutputSchema.shape>
export { updateObject }