@meetopenbot/slack 1.5.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +11 -0
- package/dist/invoke.js +72 -12
- package/dist/needs-input.js +24 -0
- package/dist/reactions.js +32 -0
- package/dist/webhook.js +3 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { definePlugin } from "@meetopenbot/plugin-sdk";
|
|
2
2
|
import { pluginConfigSchema, readSlackConfig, SLACK_BOT_SCOPES, slackCredentials, } from "./config.js";
|
|
3
3
|
import { handleSlackInvoke } from "./invoke.js";
|
|
4
|
+
import { addSlackReaction } from "./reactions.js";
|
|
4
5
|
import { handleSlackWebhook } from "./webhook.js";
|
|
5
6
|
export const plugin = definePlugin({
|
|
6
7
|
name: "Slack",
|
|
@@ -32,6 +33,16 @@ export const plugin = definePlugin({
|
|
|
32
33
|
return !!state && typeof state === "object" && Object.keys(state).length > 0;
|
|
33
34
|
},
|
|
34
35
|
storage: context.storage,
|
|
36
|
+
acknowledge: (message) => {
|
|
37
|
+
const credentials = slackCredentials(context);
|
|
38
|
+
if (!credentials)
|
|
39
|
+
return;
|
|
40
|
+
void addSlackReaction({
|
|
41
|
+
token: credentials.botToken,
|
|
42
|
+
channel: message.channel,
|
|
43
|
+
timestamp: message.ts,
|
|
44
|
+
});
|
|
45
|
+
},
|
|
35
46
|
});
|
|
36
47
|
},
|
|
37
48
|
async *run({ event, handlerCtx, context }) {
|
package/dist/invoke.js
CHANGED
|
@@ -6,15 +6,24 @@ import { resolveSlackModel, ROUTING_AGENT_ID, SLACK_USER_DISPLAY_NAME, } from ".
|
|
|
6
6
|
import { buildPromptWithSlackContext } from "./context.js";
|
|
7
7
|
import { fetchSlackUserFlags, postSlackMessage } from "./post.js";
|
|
8
8
|
import { ambiguousSpaceMessage, boundMessage, canRouteSlackChannel, findSpace, missingSpaceMessage, parseUseSpaceCommand, ROUTING_FORBIDDEN, ROUTING_HINT, ROUTING_UNNAMED_CHANNEL, withChannelBinding, withHintedChannel, } from "./routing.js";
|
|
9
|
+
import { slackNeedsInputNotice, SLACK_THREAD_STATE_KEY, } from "./needs-input.js";
|
|
9
10
|
import { getSlackThreadMeta, isSlackWebhookIngress } from "./webhook.js";
|
|
10
11
|
/**
|
|
11
12
|
* The host connects Slack before `run`. A run without credentials means the host is too
|
|
12
13
|
* old to know `oauthApp` connections.
|
|
13
14
|
*/
|
|
14
15
|
export const SLACK_NEEDS_NEWER_RUNTIME = "Slack needs a newer OpenBot runtime to connect. Update the runtime and try again.";
|
|
16
|
+
/** Behalf's own answer, as opposed to what a specialist said inside it. */
|
|
17
|
+
function isCoordinatorReply(event) {
|
|
18
|
+
return event.type === "agent:output" && !event.meta?.parentToolCallId;
|
|
19
|
+
}
|
|
15
20
|
async function* bridgeSystemAgentRun(args) {
|
|
16
21
|
const runId = `slack_${randomUUID()}`;
|
|
17
22
|
const slackMeta = getSlackThreadMeta(args.meta);
|
|
23
|
+
// Only a run that answers a Slack message is marked as Slack's; a reply typed in OpenBot is not.
|
|
24
|
+
const slackOrigin = isSlackWebhookIngress(args.meta)
|
|
25
|
+
? { source: "slack", slack: slackMeta }
|
|
26
|
+
: {};
|
|
18
27
|
const eventQueue = [];
|
|
19
28
|
let resolveNext = null;
|
|
20
29
|
let isFinished = false;
|
|
@@ -34,23 +43,29 @@ async function* bridgeSystemAgentRun(args) {
|
|
|
34
43
|
meta: {
|
|
35
44
|
channelId: args.channelId,
|
|
36
45
|
threadId: args.threadId,
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
// The person's message is already in the thread; don't store it a second time.
|
|
47
|
+
alreadyStored: true,
|
|
48
|
+
...slackOrigin,
|
|
39
49
|
userName: SLACK_USER_DISPLAY_NAME,
|
|
40
50
|
...(typeof args.meta?.userId === "string" ? { userId: args.meta.userId } : {}),
|
|
41
51
|
},
|
|
42
52
|
},
|
|
43
53
|
onEvent: async (outEvent) => {
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
// The run isn't stored here; the Slack run that wraps it stores what it yields. So pass
|
|
55
|
+
// on everything a normal Behalf run would show: replies, tool calls, and the cards a
|
|
56
|
+
// specialist (Linear, GitHub…) raises inside it. Without those cards a run waiting on
|
|
57
|
+
// one would sit on "Working…" with nothing to answer. Only this run's own start/end
|
|
58
|
+
// are left out; the Slack run has its own.
|
|
59
|
+
const isOwnLifecycle = (outEvent.type === "agent:run:start" || outEvent.type === "agent:run:end") &&
|
|
60
|
+
!outEvent.meta?.parentToolCallId;
|
|
61
|
+
if (!isOwnLifecycle) {
|
|
62
|
+
const isReply = isCoordinatorReply(outEvent);
|
|
46
63
|
eventQueue.push({
|
|
47
|
-
...
|
|
64
|
+
...outEvent,
|
|
48
65
|
meta: {
|
|
49
|
-
...
|
|
50
|
-
agentId: ROUTING_AGENT_ID,
|
|
66
|
+
...outEvent.meta,
|
|
51
67
|
threadId: args.threadId,
|
|
52
|
-
|
|
53
|
-
slack: slackMeta,
|
|
68
|
+
...(isReply ? { agentId: ROUTING_AGENT_ID, ...slackOrigin } : {}),
|
|
54
69
|
},
|
|
55
70
|
});
|
|
56
71
|
}
|
|
@@ -68,8 +83,7 @@ async function* bridgeSystemAgentRun(args) {
|
|
|
68
83
|
meta: {
|
|
69
84
|
agentId: ROUTING_AGENT_ID,
|
|
70
85
|
threadId: args.threadId,
|
|
71
|
-
|
|
72
|
-
slack: slackMeta,
|
|
86
|
+
...slackOrigin,
|
|
73
87
|
},
|
|
74
88
|
});
|
|
75
89
|
})
|
|
@@ -249,6 +263,14 @@ export async function* handleSlackInvoke(args) {
|
|
|
249
263
|
// Fall back to the raw mention if Slack history is unavailable.
|
|
250
264
|
}
|
|
251
265
|
}
|
|
266
|
+
// Remember the thread came from Slack, so replies typed in OpenBot still go to Behalf.
|
|
267
|
+
await args.storage
|
|
268
|
+
?.patchThreadState({
|
|
269
|
+
channelId,
|
|
270
|
+
threadId,
|
|
271
|
+
state: { [SLACK_THREAD_STATE_KEY]: { channel: slackChannelId, threadTs: threadId } },
|
|
272
|
+
})
|
|
273
|
+
.catch(() => undefined);
|
|
252
274
|
try {
|
|
253
275
|
for await (const output of bridgeSystemAgentRun({
|
|
254
276
|
host: args.host,
|
|
@@ -259,7 +281,13 @@ export async function* handleSlackInvoke(args) {
|
|
|
259
281
|
publicBaseUrl: args.publicBaseUrl,
|
|
260
282
|
})) {
|
|
261
283
|
yield output;
|
|
262
|
-
|
|
284
|
+
if (isCoordinatorReply(output))
|
|
285
|
+
await reply(output);
|
|
286
|
+
else {
|
|
287
|
+
const notice = slackNeedsInputNotice(output);
|
|
288
|
+
if (notice)
|
|
289
|
+
await postToSlack(notice);
|
|
290
|
+
}
|
|
263
291
|
}
|
|
264
292
|
}
|
|
265
293
|
catch (error) {
|
|
@@ -302,6 +330,38 @@ export async function* handleSlackInvoke(args) {
|
|
|
302
330
|
});
|
|
303
331
|
return;
|
|
304
332
|
}
|
|
333
|
+
// A reply typed in OpenBot into a thread that started in Slack is for Behalf, not for the
|
|
334
|
+
// Slack specialist, which only has Slack tools.
|
|
335
|
+
const metaChannelId = args.event.meta?.channelId;
|
|
336
|
+
const channelId = typeof metaChannelId === "string" ? metaChannelId : undefined;
|
|
337
|
+
if (args.host?.runAgent && channelId && args.storage) {
|
|
338
|
+
const details = await args.storage.getThreadDetails({ channelId, threadId }).catch(() => null);
|
|
339
|
+
const state = details?.state;
|
|
340
|
+
if (state?.[SLACK_THREAD_STATE_KEY]) {
|
|
341
|
+
try {
|
|
342
|
+
for await (const output of bridgeSystemAgentRun({
|
|
343
|
+
host: args.host,
|
|
344
|
+
channelId,
|
|
345
|
+
threadId,
|
|
346
|
+
userMessage,
|
|
347
|
+
meta: args.event.meta,
|
|
348
|
+
publicBaseUrl: args.publicBaseUrl,
|
|
349
|
+
})) {
|
|
350
|
+
yield output;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
catch (error) {
|
|
354
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
355
|
+
yield agentOutput({
|
|
356
|
+
agentId: ROUTING_AGENT_ID,
|
|
357
|
+
content: `Agent error: ${message}`,
|
|
358
|
+
threadId,
|
|
359
|
+
meta: args.event.meta,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
305
365
|
const model = resolveSlackModel(args.config);
|
|
306
366
|
let provider;
|
|
307
367
|
try {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Thread-state key marking an OpenBot thread that started from a Slack message. */
|
|
2
|
+
export const SLACK_THREAD_STATE_KEY = "slackThread";
|
|
3
|
+
/** Events from the system agent's run that the Slack thread's own run must pass on. */
|
|
4
|
+
export function isInteractionEvent(event) {
|
|
5
|
+
return event.type === "client:ui:widget" || event.type === "action:set_thread_status:result";
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* What to tell the Slack thread when Behalf stops to ask for something (approval, a question,
|
|
9
|
+
* a connection to set up). The card itself only exists in OpenBot, so without this the Slack
|
|
10
|
+
* thread would go quiet. Undefined for anything that isn't a new open card.
|
|
11
|
+
*/
|
|
12
|
+
export function slackNeedsInputNotice(event) {
|
|
13
|
+
if (event.type !== "client:ui:widget")
|
|
14
|
+
return undefined;
|
|
15
|
+
const widget = event.data;
|
|
16
|
+
if (widget.state && widget.state !== "open")
|
|
17
|
+
return undefined;
|
|
18
|
+
const hasInput = (Array.isArray(widget.actions) && widget.actions.length > 0) ||
|
|
19
|
+
("fields" in widget && Array.isArray(widget.fields) && widget.fields.length > 0);
|
|
20
|
+
if (!hasInput)
|
|
21
|
+
return undefined;
|
|
22
|
+
const title = widget.title?.trim();
|
|
23
|
+
return `I need your input to continue${title ? `: ${title}` : ""}. Open this conversation in OpenBot to answer, and I'll carry on from there.`;
|
|
24
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** The reaction that tells a Slack user the bot saw their message and is working on it. */
|
|
2
|
+
export const SLACK_WORKING_REACTION = "eyes";
|
|
3
|
+
/** Slack errors that mean the reaction is already where we want it, or can't matter. */
|
|
4
|
+
const HARMLESS_REACTION_ERRORS = new Set(["already_reacted", "message_not_found"]);
|
|
5
|
+
/**
|
|
6
|
+
* Adds a reaction to a message. Never throws: a missing reaction must not hold up or fail
|
|
7
|
+
* the answer, so failures are logged and dropped.
|
|
8
|
+
*/
|
|
9
|
+
export async function addSlackReaction(args) {
|
|
10
|
+
const doFetch = args.fetchImpl ?? fetch;
|
|
11
|
+
try {
|
|
12
|
+
const res = await doFetch("https://slack.com/api/reactions.add", {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: {
|
|
15
|
+
Authorization: `Bearer ${args.token}`,
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
},
|
|
18
|
+
body: JSON.stringify({
|
|
19
|
+
channel: args.channel,
|
|
20
|
+
timestamp: args.timestamp,
|
|
21
|
+
name: args.name ?? SLACK_WORKING_REACTION,
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
const body = (await res.json());
|
|
25
|
+
if (!body.ok && !HARMLESS_REACTION_ERRORS.has(body.error ?? "")) {
|
|
26
|
+
console.warn("[slack] could not add reaction", body.error ?? res.status);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.warn("[slack] could not add reaction", error);
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/webhook.js
CHANGED
|
@@ -95,6 +95,9 @@ export async function* handleSlackWebhook(args) {
|
|
|
95
95
|
yield webhookHttpResponse({ status: 200, body: {} });
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
|
+
if (slackEvent.channel && slackEvent.ts) {
|
|
99
|
+
args.acknowledge?.({ channel: slackEvent.channel, ts: slackEvent.ts });
|
|
100
|
+
}
|
|
98
101
|
args.ctx.state.threadId = threadId;
|
|
99
102
|
const slackMeta = {
|
|
100
103
|
teamId: payload.team_id,
|