Repository navigation
Expand file tree
/
Copy pathcreate-thread.ts
More file actions
155 lines (141 loc) · 6.85 KB
/
Copy pathcreate-thread.ts
File metadata and controls
155 lines (141 loc) · 6.85 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
import { getFullCommsURL, NOTIFY_AUDIENCES } from '@todoist/comms-sdk'
import { z } from 'zod'
import type { CommsTool } from '../comms-tool.js'
import { type CreateThreadOutput, CreateThreadOutputSchema } from '../utils/output-schemas.js'
import { ToolNames } from '../utils/tool-names.js'
const ArgsSchema = {
channelId: z.string().describe('The ID of the channel to create the thread in.'),
title: z.string().min(1).describe('The title of the thread.'),
content: z
.string()
.min(1)
.describe(
'The content/body of the thread. 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. Mentioning inline does not notify anyone — also pass mentioned user IDs in recipients and mentioned group IDs in groups.',
),
recipients: z
.array(z.number())
.optional()
.describe(
"Optional array of individual user IDs to notify. To notify everyone in the channel, prefer notifyAudience: 'channel' instead of enumerating every member here. If omitted (with no groups and no notifyAudience), Comms applies the channel's defaults, which is everyone in the channel. Note: workspace users who have not joined this channel will not be notified — add their IDs explicitly if you want to reach them.",
),
displayInInbox: z
.boolean()
.optional()
.describe(
"If true, unarchives the thread after creation so it appears in the author's Inbox. Defaults to false. Can also be enabled for all calls by setting the COMMS_CREATE_THREAD_DISPLAY_IN_INBOX=true environment variable (local MCP only).",
),
groups: z
.array(z.string())
.optional()
.describe(
'Optional array of group IDs to notify. Use get-groups to discover group IDs before passing them here.',
),
notifyAudience: z
.enum(NOTIFY_AUDIENCES)
.optional()
.describe(
"Optional broader audience to notify in addition to recipients and groups. 'channel' tags the thread with \"Everyone in channel\" — use this to reach everyone instead of listing individual recipients. 'thread' (everyone who has interacted) has no effect when creating a thread, since a new thread has no interactions yet — use it on reply instead.",
),
}
const createThread = {
name: ToolNames.CREATE_THREAD,
description:
'Create a new thread in a workspace channel. Requires a channel ID, title, and content. Optionally notify specific users or groups.',
parameters: ArgsSchema,
outputSchema: CreateThreadOutputSchema.shape,
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false },
async execute(args, client) {
const { channelId, title, content, recipients, groups, notifyAudience, displayInInbox } =
args
const thread = await client.threads.createThread({
channelId,
title,
content,
recipients,
groups,
notifyAudience,
})
const wantsInboxDisplay =
displayInInbox === true ||
(displayInInbox === undefined &&
process.env.COMMS_CREATE_THREAD_DISPLAY_IN_INBOX === 'true')
let displayedInInbox = false
if (wantsInboxDisplay) {
// Unarchive so the thread appears in the author's Inbox.
// Failure here is non-fatal — the thread itself was created successfully.
try {
await client.inbox.unarchiveThread(thread.id)
displayedInInbox = true
} catch (error) {
console.error(`Error unarchiving thread ${thread.id} for Inbox display:`, {
threadId: thread.id,
error,
})
}
}
const postedValue = thread.posted
const created = postedValue
? typeof postedValue === 'string'
? new Date(postedValue)
: postedValue
: new Date()
const threadUrl =
thread.url ??
getFullCommsURL({
workspaceId: thread.workspaceId,
channelId: thread.channelId,
threadId: thread.id,
})
const inboxNote = displayedInInbox
? '> Thread is in your Inbox (auto-unarchived after creation).'
: '> Note: Threads you create do not appear in your own Inbox by default — only recipients see them there. Find the thread in the channel view or via its URL.'
// Only 'channel' takes effect at thread creation. 'thread' (everyone who
// has interacted) is discarded by the backend — a new thread has no
// interactions yet — so it never becomes an applied audience here. Report
// the applied audience, not the request, so machine consumers aren't told
// an audience was notified when it wasn't.
const appliedAudience = notifyAudience === 'channel' ? notifyAudience : undefined
const audienceNote =
notifyAudience === 'channel'
? 'Everyone in channel'
: notifyAudience === 'thread'
? 'Everyone who has interacted (no effect at thread creation)'
: undefined
const lines: string[] = [
`# Thread Created`,
'',
`**Title:** ${thread.title}`,
`**Thread ID:** ${thread.id}`,
`**Channel ID:** ${thread.channelId}`,
`**Created:** ${created.toISOString()}`,
`**URL:** ${threadUrl}`,
...(audienceNote ? [`**Notified:** ${audienceNote}`] : []),
'',
'## Content',
'',
thread.content,
'',
inboxNote,
]
const structuredContent: CreateThreadOutput = {
type: 'create_thread_result',
success: true,
threadId: thread.id,
title: thread.title,
channelId: thread.channelId,
workspaceId: thread.workspaceId,
content: thread.content,
creator: thread.creator,
created: created.toISOString(),
threadUrl,
...(recipients ? { recipients } : {}),
...(groups ? { groups } : {}),
...(appliedAudience ? { notifyAudience: appliedAudience } : {}),
}
return {
textContent: lines.join('\n'),
structuredContent,
}
},
} satisfies CommsTool<typeof ArgsSchema, typeof CreateThreadOutputSchema.shape>
export { createThread }