-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(agent): route a Slack event to the run that owns the channel #193
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
github-actions
wants to merge
21
commits into
main
Choose a base branch
from
feat/slack-event-resume
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
21 commits
Select commit
Hold shift + click to select a range
204f250
spike(agent): resume a parked run from an outside event
ripgrim 15abf2f
feat(agent): route a Slack event to the run that owns the channel
ripgrim c5ad91f
feat(agent): take Slack events in, and route them to a run
ripgrim abe13b9
feat(api): accept Slack events, verify them, and hand them to the agent
ripgrim 075dccc
feat(agent): drain the Slack inbox into a parked run
ripgrim 0e7b727
feat(agent): let a run open its own Slack channel and watch it
ripgrim ee6ac0d
feat(agent): invite people to the channel a run opened
ripgrim c810748
feat(dev): give the Slack tunnel a hostname that survives a restart
ripgrim c04499d
feat(agent): teach the agent how to onboard a customer into Slack
ripgrim acce304
feat(agent): let a deployed run open a Slack channel and invite the c…
ripgrim b467676
feat(agent): add the deal owner to the channel a run opens
ripgrim 0ddfbc5
fix(auth): request app_mentions:read, without which mentions never ar…
ripgrim 8387329
fix(agent): keep Slack's invite so a run can prove it was sent
ripgrim a12b23a
fix(agent): store parsed action results as Prisma JSON
ripgrim 9c3a6c5
fix(agent): name Slack request bodies so lint:slop can pass
ripgrim d1549da
fix(validation): sort SlackEvent imports so biome can lint
ripgrim 70da103
fix(validation): export agentActionResult under one name
ripgrim 42b85a0
fix(agent): ask Slack for the Connect invite url
ripgrim 966c958
fix: close Slack resume holes so a drain cannot lose or double-handle…
ripgrim bc1a4b3
fix(api): declare Express so the Slack raw-body cap can load
ripgrim 40d08e8
copy: apply Cal's CRM-13 Slack strings
ripgrim 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
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
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
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
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,130 @@ | ||
| import { db } from "@crm/db"; | ||
| import type { SendFn } from "eve/channels"; | ||
| import { APP_AUTH } from "./app-auth"; | ||
| import { runToken } from "./custom-agent-dispatch"; | ||
|
|
||
| const LIVE_STATUSES = ["QUEUED", "RUNNING", "WAITING_FOR_APPROVAL"] as const; | ||
|
|
||
| export type ResumeOutcome = | ||
| | { kind: "resumed"; runId: string; sessionId: string } | ||
| | { kind: "ignored"; runId: string; reason: string }; | ||
|
|
||
| export type ResumeInput = { | ||
| runId: string; | ||
| message: string; | ||
| source: string; | ||
| attributes?: Readonly<Record<string, string>>; | ||
| }; | ||
|
|
||
| export async function resumeAgentRun( | ||
| input: ResumeInput, | ||
| send: SendFn, | ||
| ): Promise<ResumeOutcome> { | ||
| const { runId, message, source } = input; | ||
|
|
||
| const run = await db.agentRun.findUnique({ | ||
| where: { id: runId }, | ||
| select: { | ||
| id: true, | ||
| status: true, | ||
| sessionId: true, | ||
| agentId: true, | ||
| versionId: true, | ||
| agent: { select: { status: true, name: true } }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!run) return { kind: "ignored", runId, reason: "no such run" }; | ||
|
|
||
| if (!(LIVE_STATUSES as readonly string[]).includes(run.status)) { | ||
| return { | ||
| kind: "ignored", | ||
| runId, | ||
| reason: `the run is ${run.status.toLowerCase()}`, | ||
| }; | ||
| } | ||
|
|
||
| if (run.agent.status !== "LIVE") { | ||
| return { | ||
| kind: "ignored", | ||
| runId, | ||
| reason: `the agent is ${run.agent.status.toLowerCase()}`, | ||
| }; | ||
| } | ||
|
|
||
| if (!run.sessionId) { | ||
| return { | ||
| kind: "ignored", | ||
| runId, | ||
| reason: "the run has not started a session yet", | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| const session = await send(message, { | ||
| auth: { | ||
| authenticator: APP_AUTH.authenticator, | ||
| principalType: APP_AUTH.principalType, | ||
| principalId: APP_AUTH.principalId, | ||
| attributes: { | ||
| purpose: "team-agent", | ||
| runId: run.id, | ||
| agentId: run.agentId, | ||
| versionId: run.versionId, | ||
| resumeSource: source, | ||
| ...input.attributes, | ||
| }, | ||
| }, | ||
| continuationToken: runToken(run.id), | ||
| title: `${run.agent.name} run`, | ||
| mode: "task", | ||
| }); | ||
|
|
||
| return { kind: "resumed", runId, sessionId: session.id }; | ||
| } catch (error) { | ||
| return { | ||
| kind: "ignored", | ||
| runId, | ||
| reason: error instanceof Error ? error.message : String(error), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export async function runOnSlackChannel( | ||
| channelId: string, | ||
| ): Promise<string | null> { | ||
| const trimmed = channelId.trim(); | ||
| if (!trimmed) return null; | ||
|
|
||
| const run = await db.agentRun.findFirst({ | ||
| where: { | ||
| slackChannelId: trimmed, | ||
| status: { in: [...LIVE_STATUSES] }, | ||
| }, | ||
| orderBy: { createdAt: "desc" }, | ||
| select: { id: true }, | ||
| }); | ||
|
|
||
| return run?.id ?? null; | ||
| } | ||
|
|
||
| export async function channelOfRun(runId: string): Promise<string | null> { | ||
| const run = await db.agentRun.findUnique({ | ||
| where: { id: runId }, | ||
| select: { slackChannelId: true }, | ||
| }); | ||
|
|
||
| return run?.slackChannelId ?? null; | ||
| } | ||
|
|
||
| export async function claimSlackChannel( | ||
| runId: string, | ||
| channelId: string, | ||
| ): Promise<string | null> { | ||
| await db.agentRun.updateMany({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When Prompt for AI agents |
||
| where: { id: runId, slackChannelId: null }, | ||
| data: { slackChannelId: channelId.trim() }, | ||
| }); | ||
|
|
||
| return channelOfRun(runId); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: When a run declares
slack.channel.openorslack.channel.invitebut never calls it, the required-action failure is persisted as a CRM action. Classify all Slack action types asprovider: "slack"inrequiredActionFailureso the action ledger retains the correct provider.Prompt for AI agents