-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add broadcasts tool #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
connorlindsey
wants to merge
2
commits into
main
Choose a base branch
from
connor-kno-9028-add-broadcast-tool-to-agent-toolkit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@knocklabs/agent-toolkit": minor | ||
--- | ||
|
||
feat: add broadcast tools |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
import { WorkflowStep } from "@knocklabs/mgmt/resources/index.js"; | ||
import { z } from "zod"; | ||
|
||
import { KnockTool } from "../knock-tool.js"; | ||
|
||
/** | ||
* A slimmed down version of the Broadcast resource that is easier to work with in the LLM. | ||
*/ | ||
export type SerializedBroadcast = { | ||
key: string; | ||
name: string; | ||
status: string; | ||
description: string | undefined; | ||
categories: string[] | undefined; | ||
}; | ||
|
||
export function serializeBroadcastResponse( | ||
broadcast: any | ||
): SerializedBroadcast { | ||
return { | ||
key: broadcast.key, | ||
name: broadcast.name, | ||
status: broadcast.status, | ||
description: broadcast.description, | ||
categories: broadcast.categories, | ||
}; | ||
} | ||
|
||
export function serializeFullBroadcastResponse( | ||
broadcast: any | ||
): SerializedBroadcast & { steps: WorkflowStep[] } { | ||
return { | ||
key: broadcast.key, | ||
name: broadcast.name, | ||
status: broadcast.status, | ||
description: broadcast.description, | ||
categories: broadcast.categories, | ||
steps: broadcast.steps, | ||
}; | ||
} | ||
|
||
const listBroadcasts = KnockTool({ | ||
method: "list_broadcasts", | ||
name: "List broadcasts", | ||
description: ` | ||
List all broadcasts available for the given environment. Returns structural information about the broadcasts, including the key, name, description, categories, and status. | ||
|
||
Use this tool when you need to understand which broadcasts are available to be managed. | ||
`, | ||
parameters: z.object({ | ||
environment: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The environment to list broadcasts for. Defaults to `development`." | ||
), | ||
}), | ||
execute: (knockClient, config) => async (params) => { | ||
const allBroadcasts: SerializedBroadcast[] = []; | ||
const listParams = { | ||
environment: params.environment ?? config.environment ?? "development", | ||
}; | ||
|
||
for await (const broadcast of knockClient.broadcasts.list(listParams)) { | ||
allBroadcasts.push(serializeBroadcastResponse(broadcast)); | ||
} | ||
|
||
return allBroadcasts; | ||
}, | ||
}); | ||
|
||
const getBroadcast = KnockTool({ | ||
method: "get_broadcast", | ||
name: "Get broadcast", | ||
description: ` | ||
Get a broadcast by key. Returns complete information about the broadcast, including the key, name, description, categories, and status. | ||
`, | ||
parameters: z.object({ | ||
environment: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The environment to get the broadcast for. Defaults to `development`." | ||
), | ||
broadcastKey: z | ||
.string() | ||
.describe("(string): The key of the broadcast to get."), | ||
}), | ||
execute: (knockClient, config) => async (params) => { | ||
const broadcast = await knockClient.broadcasts.retrieve( | ||
params.broadcastKey, | ||
{ | ||
environment: params.environment ?? config.environment ?? "development", | ||
} | ||
); | ||
|
||
return serializeFullBroadcastResponse(broadcast); | ||
}, | ||
}); | ||
|
||
const upsertBroadcast = KnockTool({ | ||
method: "upsert_broadcast", | ||
name: "Upsert broadcast", | ||
description: ` | ||
Create or update a broadcast. Use this tool when you need to create a new broadcast or modify an existing one. | ||
|
||
Broadcasts are used to send one-time messages to a target audience. They support channel, branch, and delay steps. | ||
`, | ||
parameters: z.object({ | ||
environment: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The environment to create/update the broadcast in. Defaults to `development`." | ||
), | ||
broadcastKey: z | ||
.string() | ||
.describe( | ||
"(string): The key of the broadcast to create/update. Only use a kebab-case string with no spaces or special characters." | ||
), | ||
name: z.string().describe("(string): The name of the broadcast."), | ||
description: z | ||
.string() | ||
.optional() | ||
.describe("(string): The description of the broadcast."), | ||
categories: z | ||
.array(z.string()) | ||
.optional() | ||
.describe("(array): The categories to add to the broadcast."), | ||
targetAudienceKey: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The key of the audience to target for this broadcast." | ||
), | ||
steps: z | ||
.array(z.record(z.any())) | ||
.describe( | ||
"(array): The steps in the broadcast. Broadcasts only support channel, branch, and delay steps." | ||
), | ||
settings: z | ||
.object({ | ||
overridePreferences: z.boolean().optional(), | ||
isCommercial: z.boolean().optional(), | ||
}) | ||
.optional() | ||
.describe("(object): Broadcast settings."), | ||
}), | ||
execute: (knockClient, config) => async (params) => { | ||
const result = await knockClient.broadcasts.upsert(params.broadcastKey, { | ||
environment: params.environment ?? config.environment ?? "development", | ||
broadcast: { | ||
name: params.name, | ||
description: params.description, | ||
categories: params.categories ?? [], | ||
target_audience_key: params.targetAudienceKey, | ||
steps: params.steps ?? [], | ||
settings: params.settings, | ||
}, | ||
}); | ||
|
||
return serializeFullBroadcastResponse(result.broadcast); | ||
}, | ||
}); | ||
|
||
const sendBroadcast = KnockTool({ | ||
method: "send_broadcast", | ||
name: "Send broadcast", | ||
description: ` | ||
Send a broadcast immediately or schedule it to send at a future time. Use this tool when you need to send a broadcast to its target audience. | ||
|
||
If sendAt is provided, the broadcast will be scheduled to send at that time. If not provided, the broadcast will be sent immediately. | ||
`, | ||
parameters: z.object({ | ||
environment: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The environment to send the broadcast in. Defaults to `development`." | ||
), | ||
broadcastKey: z | ||
.string() | ||
.describe("(string): The key of the broadcast to send."), | ||
sendAt: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): When to send the broadcast. Must be in ISO 8601 UTC format. If not provided, the broadcast will be sent immediately." | ||
), | ||
}), | ||
execute: (knockClient, config) => async (params) => { | ||
const result = await knockClient.broadcasts.send(params.broadcastKey, { | ||
environment: params.environment ?? config.environment ?? "development", | ||
send_at: params.sendAt, | ||
}); | ||
|
||
return serializeFullBroadcastResponse(result.broadcast); | ||
}, | ||
}); | ||
|
||
const cancelBroadcast = KnockTool({ | ||
method: "cancel_broadcast", | ||
name: "Cancel broadcast", | ||
description: ` | ||
Cancel a scheduled broadcast. The broadcast will return to draft status. Use this tool when you need to cancel a broadcast that has been scheduled but not yet sent. | ||
`, | ||
parameters: z.object({ | ||
environment: z | ||
.string() | ||
.optional() | ||
.describe( | ||
"(string): The environment to cancel the broadcast in. Defaults to `development`." | ||
), | ||
broadcastKey: z | ||
.string() | ||
.describe("(string): The key of the broadcast to cancel."), | ||
}), | ||
execute: (knockClient, config) => async (params) => { | ||
const result = await knockClient.broadcasts.cancel(params.broadcastKey, { | ||
environment: params.environment ?? config.environment ?? "development", | ||
}); | ||
|
||
return serializeFullBroadcastResponse(result.broadcast); | ||
}, | ||
}); | ||
|
||
export const broadcasts = { | ||
listBroadcasts, | ||
getBroadcast, | ||
upsertBroadcast, | ||
sendBroadcast, | ||
cancelBroadcast, | ||
}; | ||
|
||
export const permissions = { | ||
read: ["listBroadcasts", "getBroadcast"], | ||
manage: ["upsertBroadcast", "sendBroadcast", "cancelBroadcast"], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can’t do an array of any here - it will cause issues with certain models. We need to specify the types.
it would be nice to have a way here to share the types from the workflow to manage this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, I'll update the type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow tool doesn't type steps at all since we're using the workflow steps tool. Do you think broadcasts should do something similar having a tool per channel / step type or should I just pull out and share the types?