managed-deepagents 0.10.7 → 0.10.8-dev.2
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/README.md +449 -89
- package/dist/agent-auth.d.ts +4 -2
- package/dist/agent-auth.d.ts.map +1 -1
- package/dist/agent-auth.js +3 -1
- package/dist/agent-auth.js.map +1 -1
- package/dist/channels/address.d.ts +13 -0
- package/dist/channels/address.d.ts.map +1 -0
- package/dist/channels/address.js +67 -0
- package/dist/channels/address.js.map +1 -0
- package/dist/channels/binding.d.ts +7 -0
- package/dist/channels/binding.d.ts.map +1 -1
- package/dist/channels/binding.js +28 -4
- package/dist/channels/binding.js.map +1 -1
- package/dist/channels/delivery.d.ts.map +1 -1
- package/dist/channels/delivery.js +13 -16
- package/dist/channels/delivery.js.map +1 -1
- package/dist/channels/github/app-setup.d.ts.map +1 -1
- package/dist/channels/github/app-setup.js +86 -3
- package/dist/channels/github/app-setup.js.map +1 -1
- package/dist/channels/github/index.d.ts +1 -6
- package/dist/channels/github/index.d.ts.map +1 -1
- package/dist/channels/github/index.js +191 -38
- package/dist/channels/github/index.js.map +1 -1
- package/dist/channels/github/schema.d.ts +9280 -1622
- package/dist/channels/github/schema.d.ts.map +1 -1
- package/dist/channels/github/schema.js +164 -0
- package/dist/channels/github/schema.js.map +1 -1
- package/dist/channels/github/types.d.ts +98 -36
- package/dist/channels/github/types.d.ts.map +1 -1
- package/dist/channels/http/endpoint.d.ts +33 -9
- package/dist/channels/http/endpoint.d.ts.map +1 -1
- package/dist/channels/http/endpoint.js +72 -18
- package/dist/channels/http/endpoint.js.map +1 -1
- package/dist/channels/http/http.d.ts +7 -0
- package/dist/channels/http/http.d.ts.map +1 -1
- package/dist/channels/http/http.js +273 -27
- package/dist/channels/http/http.js.map +1 -1
- package/dist/channels/http/index.d.ts.map +1 -1
- package/dist/channels/http/index.js +4 -5
- package/dist/channels/http/index.js.map +1 -1
- package/dist/channels/http/ingress.d.ts +44 -1
- package/dist/channels/http/ingress.d.ts.map +1 -1
- package/dist/channels/http/ingress.js +63 -0
- package/dist/channels/http/ingress.js.map +1 -1
- package/dist/channels/http/input-state.d.ts +7 -0
- package/dist/channels/http/input-state.d.ts.map +1 -0
- package/dist/channels/http/input-state.js +16 -0
- package/dist/channels/http/input-state.js.map +1 -0
- package/dist/channels/http/provider.d.ts +36 -7
- package/dist/channels/http/provider.d.ts.map +1 -1
- package/dist/channels/http/provider.js +46 -7
- package/dist/channels/http/provider.js.map +1 -1
- package/dist/channels/http/types.d.ts +13 -5
- package/dist/channels/http/types.d.ts.map +1 -1
- package/dist/channels/linear/index.d.ts +0 -5
- package/dist/channels/linear/index.d.ts.map +1 -1
- package/dist/channels/linear/index.js +94 -21
- package/dist/channels/linear/index.js.map +1 -1
- package/dist/channels/linear/schema.d.ts +20 -20
- package/dist/channels/linear/schema.d.ts.map +1 -1
- package/dist/channels/linear/schema.js +20 -1
- package/dist/channels/linear/schema.js.map +1 -1
- package/dist/channels/linear/types.d.ts +58 -49
- package/dist/channels/linear/types.d.ts.map +1 -1
- package/dist/channels/reply.d.ts +28 -0
- package/dist/channels/reply.d.ts.map +1 -0
- package/dist/channels/reply.js +37 -0
- package/dist/channels/reply.js.map +1 -0
- package/dist/channels/resolve-user.d.ts +3 -2
- package/dist/channels/resolve-user.d.ts.map +1 -1
- package/dist/channels/resolve-user.js +5 -4
- package/dist/channels/resolve-user.js.map +1 -1
- package/dist/channels/run-input.d.ts +120 -10
- package/dist/channels/run-input.d.ts.map +1 -1
- package/dist/channels/run-input.js +233 -16
- package/dist/channels/run-input.js.map +1 -1
- package/dist/channels/runtime.d.ts +10 -3
- package/dist/channels/runtime.d.ts.map +1 -1
- package/dist/channels/runtime.js +15 -1
- package/dist/channels/runtime.js.map +1 -1
- package/dist/channels/schedule-delivery.d.ts +2 -2
- package/dist/channels/schedule-delivery.d.ts.map +1 -1
- package/dist/channels/slack/events.d.ts +176 -63
- package/dist/channels/slack/events.d.ts.map +1 -1
- package/dist/channels/slack/events.js +153 -4
- package/dist/channels/slack/events.js.map +1 -1
- package/dist/channels/slack/files.d.ts +6 -3
- package/dist/channels/slack/files.d.ts.map +1 -1
- package/dist/channels/slack/files.js +4 -1
- package/dist/channels/slack/files.js.map +1 -1
- package/dist/channels/slack/index.d.ts +60 -29
- package/dist/channels/slack/index.d.ts.map +1 -1
- package/dist/channels/slack/index.js +134 -56
- package/dist/channels/slack/index.js.map +1 -1
- package/dist/channels/trigger/conversation.d.ts +8 -3
- package/dist/channels/trigger/conversation.d.ts.map +1 -1
- package/dist/channels/trigger/conversation.js +101 -12
- package/dist/channels/trigger/conversation.js.map +1 -1
- package/dist/channels/trigger/http.d.ts +48 -8
- package/dist/channels/trigger/http.d.ts.map +1 -1
- package/dist/channels/trigger/http.js +324 -47
- package/dist/channels/trigger/http.js.map +1 -1
- package/dist/channels/trigger/ingress.d.ts +68 -2
- package/dist/channels/trigger/ingress.d.ts.map +1 -1
- package/dist/channels/trigger/ingress.js +124 -1
- package/dist/channels/trigger/ingress.js.map +1 -1
- package/dist/channels/trigger/transport.d.ts +2 -0
- package/dist/channels/trigger/transport.d.ts.map +1 -1
- package/dist/channels/trigger/transport.js +5 -1
- package/dist/channels/trigger/transport.js.map +1 -1
- package/dist/channels/trigger/types.d.ts +49 -0
- package/dist/channels/trigger/types.d.ts.map +1 -1
- package/dist/channels/trigger/types.js +21 -0
- package/dist/channels/trigger/types.js.map +1 -1
- package/dist/channels/types.d.ts +205 -1
- package/dist/channels/types.d.ts.map +1 -1
- package/dist/channels/types.js +27 -0
- package/dist/channels/types.js.map +1 -1
- package/dist/channels/warnings.d.ts +8 -0
- package/dist/channels/warnings.d.ts.map +1 -0
- package/dist/channels/warnings.js +17 -0
- package/dist/channels/warnings.js.map +1 -0
- package/dist/connectors/mcp/index.d.ts.map +1 -1
- package/dist/connectors/mcp/index.js +12 -4
- package/dist/connectors/mcp/index.js.map +1 -1
- package/dist/connectors/mcp/instructions.d.ts +30 -0
- package/dist/connectors/mcp/instructions.d.ts.map +1 -0
- package/dist/connectors/mcp/instructions.js +150 -0
- package/dist/connectors/mcp/instructions.js.map +1 -0
- package/dist/connectors/mcp/schema.d.ts.map +1 -1
- package/dist/connectors/mcp/schema.js +2 -1
- package/dist/connectors/mcp/schema.js.map +1 -1
- package/dist/connectors/mcp/types.d.ts +10 -0
- package/dist/connectors/mcp/types.d.ts.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/ingress/runs.d.ts +3 -0
- package/dist/ingress/runs.d.ts.map +1 -1
- package/dist/ingress/runs.js +34 -16
- package/dist/ingress/runs.js.map +1 -1
- package/dist/memory/define-memory.d.ts +4 -2
- package/dist/memory/define-memory.d.ts.map +1 -1
- package/dist/memory/define-memory.js +4 -2
- package/dist/memory/define-memory.js.map +1 -1
- package/dist/namespaces.d.ts +2 -3
- package/dist/namespaces.d.ts.map +1 -1
- package/dist/namespaces.js +1 -1
- package/dist/namespaces.js.map +1 -1
- package/dist/run-context.d.ts +8 -0
- package/dist/run-context.d.ts.map +1 -1
- package/dist/run-context.js +5 -0
- package/dist/run-context.js.map +1 -1
- package/dist/run-ending-error.d.ts +16 -0
- package/dist/run-ending-error.d.ts.map +1 -0
- package/dist/run-ending-error.js +16 -0
- package/dist/run-ending-error.js.map +1 -0
- package/dist/runtime/auth.d.ts +1 -0
- package/dist/runtime/auth.d.ts.map +1 -1
- package/dist/runtime/auth.js +1 -1
- package/dist/runtime/auth.js.map +1 -1
- package/dist/runtime/backend.d.ts.map +1 -1
- package/dist/runtime/backend.js +7 -1
- package/dist/runtime/backend.js.map +1 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +4 -1
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/managed-thread.d.ts +6 -0
- package/dist/runtime/managed-thread.d.ts.map +1 -1
- package/dist/runtime/managed-thread.js +38 -2
- package/dist/runtime/managed-thread.js.map +1 -1
- package/dist/runtime/memory/policy.d.ts.map +1 -1
- package/dist/runtime/memory/policy.js +24 -5
- package/dist/runtime/memory/policy.js.map +1 -1
- package/dist/runtime/run-input.d.ts +1 -0
- package/dist/runtime/run-input.d.ts.map +1 -1
- package/dist/runtime/sandbox-manager.d.ts +27 -3
- package/dist/runtime/sandbox-manager.d.ts.map +1 -1
- package/dist/runtime/sandbox-manager.js +188 -22
- package/dist/runtime/sandbox-manager.js.map +1 -1
- package/dist/runtime/schedule-auth.d.ts +11 -5
- package/dist/runtime/schedule-auth.d.ts.map +1 -1
- package/dist/runtime/schedule-auth.js +37 -12
- package/dist/runtime/schedule-auth.js.map +1 -1
- package/dist/runtime/thread-handoff.d.ts +16 -0
- package/dist/runtime/thread-handoff.d.ts.map +1 -0
- package/dist/runtime/thread-handoff.js +24 -0
- package/dist/runtime/thread-handoff.js.map +1 -0
- package/dist/runtime/thread-record.d.ts +4 -0
- package/dist/runtime/thread-record.d.ts.map +1 -1
- package/dist/runtime/thread-record.js +4 -0
- package/dist/runtime/thread-record.js.map +1 -1
- package/dist/runtime-schedules.d.ts +2 -2
- package/dist/runtime-schedules.d.ts.map +1 -1
- package/dist/runtime-schedules.js +3 -1
- package/dist/runtime-schedules.js.map +1 -1
- package/dist/sandbox.d.ts +12 -0
- package/dist/sandbox.d.ts.map +1 -1
- package/dist/sandbox.js +5 -0
- package/dist/sandbox.js.map +1 -1
- package/dist/schedules.d.ts +33 -14
- package/dist/schedules.d.ts.map +1 -1
- package/dist/schedules.js +32 -1
- package/dist/schedules.js.map +1 -1
- package/dist/telemetry/run.d.ts +2 -1
- package/dist/telemetry/run.d.ts.map +1 -1
- package/dist/telemetry/run.js.map +1 -1
- package/dist/tool-errors.d.ts.map +1 -1
- package/dist/tool-errors.js +10 -5
- package/dist/tool-errors.js.map +1 -1
- package/package.json +7 -7
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { INTERRUPT } from "@langchain/langgraph";
|
|
2
2
|
import { bindingWithMessages } from "../binding.js";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { v5 as uuidv5 } from "uuid";
|
|
4
5
|
import { missingWorkspaceConnectionSlug, } from "../../agent-auth.js";
|
|
5
6
|
import { resolveExternalPrincipalId } from "../../connections.js";
|
|
6
7
|
import { DEFAULT_REACTION_EMOJI, resolveReactionEmoji, } from "../reactions.js";
|
|
7
8
|
import { setLogAttributes } from "../../telemetry/otel/index.js";
|
|
8
9
|
import { logger } from "../../runtime/log.js";
|
|
9
10
|
import { INTERRUPT_CORRELATION_KEY } from "../../run-context.js";
|
|
10
|
-
import { cancelIngressThreadRuns, conversationThreadId, ingressThreadExists, ingressResumeTarget, ingressThreadInterrupts, newerIngressRun, observeIngressAgent, resumeIngressAgent, startIngressAgent, } from "./ingress.js";
|
|
11
|
+
import { DELIVERS_METADATA, STARTED_FROM_METADATA, cancelHandlerThreadRuns, cancelIngressThreadRuns, conversationThreadId, deleteIngressThread, handlerThreadResumeTarget, handlerThreadsKey, ingressThreadExists, getIngressThreadState, ingressResumeTarget, ingressThreadInterrupts, newerIngressRun, observeIngressAgent, resumeIngressAgent, startIngressAgent, } from "./ingress.js";
|
|
11
12
|
import { scheduleIngressWork } from "../../ingress/index.js";
|
|
12
13
|
import { lookupStatus, rejectedAsBusy, } from "../../ingress/runs.js";
|
|
13
|
-
import { runStartOptions } from "../run-input.js";
|
|
14
|
+
import { runStartOptions, } from "../run-input.js";
|
|
14
15
|
import { createTriggerClient, LANGSMITH_MDA_TRIGGER_SERVER_URL_ENV, TriggerClientError, } from "./client.js";
|
|
15
16
|
import { IdentityContextUnavailableError, InvalidIdentityContextError, principalIdFromIdentityContext, } from "./identity-context.js";
|
|
16
17
|
import { resolveTracerSessionId } from "./tracing-project.js";
|
|
@@ -20,6 +21,14 @@ import { createTriggerTransport } from "./transport.js";
|
|
|
20
21
|
import { parseTriggerChannelEvent, triggerChannelBinding, TRIGGER_SERVICE_KEY_HEADER, normalizedUserPromptResponse, triggerEventActor, } from "./types.js";
|
|
21
22
|
import { isRecord, nonblankString } from "../../values.js";
|
|
22
23
|
export { TRIGGER_SERVICE_KEY_HEADER } from "./types.js";
|
|
24
|
+
/**
|
|
25
|
+
* What a message in a thread whose conversation moved to a new thread hears
|
|
26
|
+
* instead of a run (`post(message, { newThread: true, handOff: true })`).
|
|
27
|
+
*/
|
|
28
|
+
export const MOVED_AWAY = {
|
|
29
|
+
type: "content",
|
|
30
|
+
content: "This conversation moved to a new thread.",
|
|
31
|
+
};
|
|
23
32
|
export const ASSISTANT_ID_ENV = "MDA_ASSISTANT_ID";
|
|
24
33
|
// Raised when the thread cannot say which interrupt an answer belongs to. An
|
|
25
34
|
// app opts out of this by carrying a correlation id; see INTERRUPT_CORRELATION_KEY.
|
|
@@ -156,33 +165,33 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
156
165
|
});
|
|
157
166
|
/** `resolveUser`'s answer when it holds back the run the delivery would start. */
|
|
158
167
|
const withheld = user?.type === "principal" ? undefined : user;
|
|
168
|
+
/** Post a reply instead of the run, where the delivery came from. */
|
|
169
|
+
const replyTo = (message) => postReply(message, {
|
|
170
|
+
name: options.channelName,
|
|
171
|
+
provider: options.sourceProvider,
|
|
172
|
+
target: triggerTarget(event),
|
|
173
|
+
transport: createTriggerTransport(client, {
|
|
174
|
+
actionId: event.delivery_id,
|
|
175
|
+
}),
|
|
176
|
+
});
|
|
159
177
|
/** Post `resolveUser`'s reply, if any, instead of that run. */
|
|
160
178
|
const withhold = async () => {
|
|
161
179
|
if (withheld?.type === "reply")
|
|
162
|
-
await
|
|
163
|
-
name: options.channelName,
|
|
164
|
-
provider: options.sourceProvider,
|
|
165
|
-
target: triggerTarget(event),
|
|
166
|
-
transport: createTriggerTransport(client, {
|
|
167
|
-
actionId: event.delivery_id,
|
|
168
|
-
}),
|
|
169
|
-
});
|
|
180
|
+
await replyTo(withheld.message);
|
|
170
181
|
logNoRun(event, { type: "skipped" });
|
|
171
182
|
};
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
newerRun: options.newerRun,
|
|
185
|
-
});
|
|
183
|
+
/**
|
|
184
|
+
* Post a handler's reply instead of the run. When `resolveUser` held the
|
|
185
|
+
* delivery back, its answer stands instead, as for a run.
|
|
186
|
+
*/
|
|
187
|
+
const replyInstead = async (message) => {
|
|
188
|
+
if (withheld)
|
|
189
|
+
return withhold();
|
|
190
|
+
await replyTo(message);
|
|
191
|
+
logNoRun(event, { type: "skipped" });
|
|
192
|
+
};
|
|
193
|
+
/** Only a channel with handlers starts runs on threads they chose. */
|
|
194
|
+
const handlerThreads = options.decideMessage !== undefined;
|
|
186
195
|
let threadId;
|
|
187
196
|
try {
|
|
188
197
|
threadId = await (options.conversationThread ?? conversationThreadId)({
|
|
@@ -195,12 +204,50 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
195
204
|
logger.error(`channel thread could not be found (${logger.safeErrorLabel(error)})`);
|
|
196
205
|
return Response.json({ error: "channel run could not be started" }, { status: 500 });
|
|
197
206
|
}
|
|
198
|
-
const thread = {
|
|
207
|
+
const thread = {
|
|
208
|
+
id: threadId,
|
|
209
|
+
sourceThreadKey: event.source_thread_key,
|
|
210
|
+
getState: () => (options.getThreadState ?? getIngressThreadState)({
|
|
211
|
+
threadId,
|
|
212
|
+
sourceProvider: options.sourceProvider,
|
|
213
|
+
sourceThreadKey: event.source_thread_key,
|
|
214
|
+
userId,
|
|
215
|
+
}),
|
|
216
|
+
};
|
|
217
|
+
/** Whether the run was refused because this conversation handed its thread on. */
|
|
218
|
+
const movedAway = (error) => handedOff(error, options, {
|
|
219
|
+
threadId,
|
|
220
|
+
sourceProvider: options.sourceProvider,
|
|
221
|
+
sourceThreadKey: event.source_thread_key,
|
|
222
|
+
userId,
|
|
223
|
+
});
|
|
224
|
+
const observe = (started, as) => observeAcceptedRun({
|
|
225
|
+
event,
|
|
226
|
+
...(as.reportedFor && { reportedFor: as.reportedFor }),
|
|
227
|
+
started,
|
|
228
|
+
client,
|
|
229
|
+
observeAgent: options.observeAgent,
|
|
230
|
+
sourceProvider: options.sourceProvider,
|
|
231
|
+
sourceThreadKey: as.sourceThreadKey,
|
|
232
|
+
userId,
|
|
233
|
+
reactions: as.react === false
|
|
234
|
+
? false
|
|
235
|
+
: (options.reactions ?? DEFAULT_REACTION_EMOJI),
|
|
236
|
+
...(options.startedStatus && { startedStatus: options.startedStatus }),
|
|
237
|
+
...(options.showProgress === false && { showProgress: false }),
|
|
238
|
+
multitaskStrategy: as.multitaskStrategy,
|
|
239
|
+
newerRun: options.newerRun,
|
|
240
|
+
// Only the conversation's own thread can be handed on.
|
|
241
|
+
...(started.threadId === threadId &&
|
|
242
|
+
as.sourceThreadKey === event.source_thread_key && {
|
|
243
|
+
handedOff: movedAway,
|
|
244
|
+
}),
|
|
245
|
+
});
|
|
199
246
|
/**
|
|
200
247
|
* Whether the delivery starts a turn of its own, which finishes: a message,
|
|
201
248
|
* or a prompt response that answers no interrupt. Resumes don't.
|
|
202
249
|
*/
|
|
203
|
-
let startsTurn = event.type === "message";
|
|
250
|
+
let startsTurn = event.type === "message" || event.type === "provider_event";
|
|
204
251
|
/** Tell the provider a turn is done, once; a failure is logged. */
|
|
205
252
|
const finish = async (finished) => {
|
|
206
253
|
if (!startsTurn || !options.onFinish)
|
|
@@ -230,6 +277,90 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
230
277
|
});
|
|
231
278
|
};
|
|
232
279
|
let started;
|
|
280
|
+
let observedAs = {
|
|
281
|
+
sourceThreadKey: event.source_thread_key,
|
|
282
|
+
multitaskStrategy: options.multitaskStrategy,
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Start a run on a thread the handler chose, and see it through. Every such
|
|
286
|
+
* thread of the channel's is owned by one key, so Stop and the buttons in
|
|
287
|
+
* the conversation that started a run reach it there. Its status shows in
|
|
288
|
+
* that conversation; it answers there only with `autoPost`. An ephemeral
|
|
289
|
+
* thread is deleted once its run ends, unless the run waits on a decision.
|
|
290
|
+
*/
|
|
291
|
+
const runOnThread = async (run, index, react) => {
|
|
292
|
+
const owner = handlerThreadsKey(options.channelName);
|
|
293
|
+
const chosen = run.thread;
|
|
294
|
+
const threadId = chosen.mode === "persistent"
|
|
295
|
+
? chosen.id
|
|
296
|
+
: triggerEphemeralThreadId(options.channelName, event.delivery_id, index);
|
|
297
|
+
const strategy = run.multitaskStrategy ?? options.multitaskStrategy;
|
|
298
|
+
// A run that belongs to another Slack thread reports there, and Stop there stops it.
|
|
299
|
+
const reported = run.conversation
|
|
300
|
+
? inConversation(event, run.conversation)
|
|
301
|
+
: event;
|
|
302
|
+
const binding = {
|
|
303
|
+
...bindingWithMessages(triggerChannelBinding(reported, options.sourceProvider, options.channelName), run.messages),
|
|
304
|
+
reply: run.deliverToConversation ? "auto" : "none",
|
|
305
|
+
};
|
|
306
|
+
let threadRun;
|
|
307
|
+
try {
|
|
308
|
+
threadRun = await (options.startAgent ?? startIngressAgent)({
|
|
309
|
+
threadId,
|
|
310
|
+
assistantId,
|
|
311
|
+
sourceProvider: options.sourceProvider,
|
|
312
|
+
sourceThreadKey: owner,
|
|
313
|
+
userId,
|
|
314
|
+
...runStartOptions(run, {
|
|
315
|
+
[STARTED_FROM_METADATA]: reported.source_thread_key,
|
|
316
|
+
...(run.deliverToConversation && { [DELIVERS_METADATA]: true }),
|
|
317
|
+
}),
|
|
318
|
+
context: run.context ?? {},
|
|
319
|
+
binding,
|
|
320
|
+
...(strategy && { multitaskStrategy: strategy }),
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
if (rejectedAsBusy(strategy, error)) {
|
|
325
|
+
logger.info(`channel run rejected: its thread is busy (delivery_id=${logger.logIdentifier(event.delivery_id)})`);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
logger.error(`channel run could not be started (${logger.safeErrorLabel(error)})`);
|
|
329
|
+
}
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const finished = await observe(threadRun, {
|
|
333
|
+
sourceThreadKey: owner,
|
|
334
|
+
multitaskStrategy: strategy,
|
|
335
|
+
react,
|
|
336
|
+
reportedFor: reported,
|
|
337
|
+
});
|
|
338
|
+
if (chosen.mode === "persistent" ||
|
|
339
|
+
finished.type !== "run" ||
|
|
340
|
+
finished.interrupts.length)
|
|
341
|
+
return;
|
|
342
|
+
try {
|
|
343
|
+
await (options.deleteThread ?? deleteIngressThread)({
|
|
344
|
+
threadId,
|
|
345
|
+
sourceProvider: options.sourceProvider,
|
|
346
|
+
sourceThreadKey: owner,
|
|
347
|
+
userId,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
logger.error(`ephemeral channel thread could not be deleted (${logger.safeErrorLabel(error)})`);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
/** Start the handler's runs on its threads; the message's own reaction goes with its conversation's run, else the first. */
|
|
355
|
+
const startOnThreads = (decided) => {
|
|
356
|
+
(decided.runs ?? []).forEach((run, index) => {
|
|
357
|
+
const react = decided.input === undefined && index === 0;
|
|
358
|
+
scheduleIngressWork(() => runOnThread(run, index, react), {
|
|
359
|
+
label: "Channel run on a handler's thread",
|
|
360
|
+
schedule: options.schedule,
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
};
|
|
233
364
|
try {
|
|
234
365
|
const common = {
|
|
235
366
|
threadId: thread.id,
|
|
@@ -248,7 +379,11 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
248
379
|
const decideAndStart = async (route, undecided) => {
|
|
249
380
|
/** Start the run; `undefined` when `reject` left the thread to its run. */
|
|
250
381
|
const startRun = async (input) => {
|
|
251
|
-
const strategy = options.multitaskStrategy;
|
|
382
|
+
const strategy = input?.multitaskStrategy ?? options.multitaskStrategy;
|
|
383
|
+
observedAs = {
|
|
384
|
+
sourceThreadKey: event.source_thread_key,
|
|
385
|
+
multitaskStrategy: strategy,
|
|
386
|
+
};
|
|
252
387
|
try {
|
|
253
388
|
return await (options.startAgent ?? startIngressAgent)({
|
|
254
389
|
...common,
|
|
@@ -273,18 +408,29 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
273
408
|
scheduleIngressWork(async () => {
|
|
274
409
|
const decided = await route.decide();
|
|
275
410
|
if (decided.type !== "input") {
|
|
276
|
-
|
|
411
|
+
if (decided.type === "reply")
|
|
412
|
+
await replyInstead(decided.message);
|
|
413
|
+
else
|
|
414
|
+
logNoRun(event, decided);
|
|
277
415
|
return finish(skipped("ignored"));
|
|
278
416
|
}
|
|
279
417
|
if (withheld) {
|
|
280
418
|
await withhold();
|
|
281
419
|
return finish(skipped("ignored"));
|
|
282
420
|
}
|
|
421
|
+
startOnThreads(decided);
|
|
422
|
+
// The handler's runs all went to threads of its choosing.
|
|
423
|
+
if (!decided.input && decided.runs?.length)
|
|
424
|
+
return finish(skipped("ignored"));
|
|
283
425
|
let run;
|
|
284
426
|
try {
|
|
285
427
|
run = await startRun(decided.input);
|
|
286
428
|
}
|
|
287
429
|
catch (error) {
|
|
430
|
+
if (await movedAway(error)) {
|
|
431
|
+
await replyInstead(MOVED_AWAY);
|
|
432
|
+
return finish(skipped("ignored"));
|
|
433
|
+
}
|
|
288
434
|
logger.error(`channel run could not be started (${logger.safeErrorLabel(error)})`, error);
|
|
289
435
|
return finish({
|
|
290
436
|
type: "failed",
|
|
@@ -292,12 +438,15 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
292
438
|
interrupts: [],
|
|
293
439
|
});
|
|
294
440
|
}
|
|
295
|
-
await finish(run ? await observe(run) : skipped("rejected"));
|
|
441
|
+
await finish(run ? await observe(run, observedAs) : skipped("rejected"));
|
|
296
442
|
}, { label: "Channel event decision", schedule: options.schedule });
|
|
297
443
|
return acceptedResponse(event);
|
|
298
444
|
}
|
|
299
|
-
if (route?.type === "skipped") {
|
|
300
|
-
|
|
445
|
+
if (route?.type === "skipped" || route?.type === "reply") {
|
|
446
|
+
if (route.type === "reply")
|
|
447
|
+
await replyInstead(route.message);
|
|
448
|
+
else
|
|
449
|
+
logNoRun(event, route);
|
|
301
450
|
finishLater(skipped("ignored"));
|
|
302
451
|
return acceptedResponse(event);
|
|
303
452
|
}
|
|
@@ -309,6 +458,14 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
309
458
|
finishLater(skipped("ignored"));
|
|
310
459
|
return acceptedResponse(event);
|
|
311
460
|
}
|
|
461
|
+
if (route?.type === "input") {
|
|
462
|
+
startOnThreads(route);
|
|
463
|
+
if (!route.input && route.runs?.length) {
|
|
464
|
+
// The handler's runs all went to threads of its choosing.
|
|
465
|
+
finishLater(skipped("ignored"));
|
|
466
|
+
return acceptedResponse(event);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
312
469
|
const run = await startRun(route?.input);
|
|
313
470
|
if (!run) {
|
|
314
471
|
finishLater(skipped("rejected"));
|
|
@@ -330,21 +487,36 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
330
487
|
started = outcome;
|
|
331
488
|
break;
|
|
332
489
|
}
|
|
490
|
+
case "provider_event": {
|
|
491
|
+
// Only a handler starts a run for a provider event.
|
|
492
|
+
const route = await options.decideProviderEvent?.(event, thread, actor);
|
|
493
|
+
const outcome = await decideAndStart(route ?? { type: "skipped" }, () => []);
|
|
494
|
+
if (outcome instanceof Response)
|
|
495
|
+
return outcome;
|
|
496
|
+
started = outcome;
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
333
499
|
case "interrupt_resume": {
|
|
334
500
|
if (withheld) {
|
|
335
501
|
await withhold();
|
|
336
502
|
return acceptedResponse(event);
|
|
337
503
|
}
|
|
338
|
-
// The answer may belong to a paused async subagent
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
interruptIds: Object.keys(event.interaction.resume),
|
|
342
|
-
});
|
|
504
|
+
// The answer may belong to a paused async subagent, or to a run this
|
|
505
|
+
// conversation's message started on a thread its handler chose.
|
|
506
|
+
const answered = await answeredThread(common, Object.keys(event.interaction.resume), handlerThreads, options);
|
|
343
507
|
started = await (options.resumeAgent ?? resumeIngressAgent)({
|
|
344
508
|
...common,
|
|
345
|
-
...target,
|
|
509
|
+
...answered.target,
|
|
510
|
+
// The resumed run is the one Stop and the next answer look for.
|
|
511
|
+
...(answered.handler && {
|
|
512
|
+
sourceThreadKey: answered.handler.sourceThreadKey,
|
|
513
|
+
binding: { ...common.binding, reply: answered.handler.reply },
|
|
514
|
+
metadata: answered.handler.runMetadata,
|
|
515
|
+
}),
|
|
346
516
|
resume: event.interaction.resume,
|
|
347
517
|
});
|
|
518
|
+
if (answered.handler)
|
|
519
|
+
observedAs = { sourceThreadKey: answered.handler.sourceThreadKey };
|
|
348
520
|
break;
|
|
349
521
|
}
|
|
350
522
|
case "user_prompt_response": {
|
|
@@ -388,10 +560,15 @@ export async function handleTriggerChannelEventRequest(request, options) {
|
|
|
388
560
|
logger.warn(`channel user prompt response could not be matched to an interrupt (${error.message}, delivery_id=${logger.logIdentifier(event.delivery_id)}, event_id=${logger.logIdentifier(event.event_id)})`);
|
|
389
561
|
return acceptedResponse(event);
|
|
390
562
|
}
|
|
563
|
+
if (await movedAway(error)) {
|
|
564
|
+
await replyInstead(MOVED_AWAY);
|
|
565
|
+
finishLater(skipped("ignored"));
|
|
566
|
+
return acceptedResponse(event);
|
|
567
|
+
}
|
|
391
568
|
logger.error(`channel run could not be started (${logger.safeErrorLabel(error)})`, error);
|
|
392
569
|
return startFailedResponse();
|
|
393
570
|
}
|
|
394
|
-
scheduleIngressWork(async () => finish(await observe(started)), {
|
|
571
|
+
scheduleIngressWork(async () => finish(await observe(started, observedAs)), {
|
|
395
572
|
label: "Channel run observation",
|
|
396
573
|
schedule: options.schedule,
|
|
397
574
|
});
|
|
@@ -419,10 +596,64 @@ function logNoRun(event, decision) {
|
|
|
419
596
|
function decidedRun(input, binding) {
|
|
420
597
|
return {
|
|
421
598
|
...runStartOptions(input),
|
|
422
|
-
binding:
|
|
599
|
+
binding: {
|
|
600
|
+
...bindingWithMessages(binding, input.messages),
|
|
601
|
+
...(input.reply && { reply: input.reply }),
|
|
602
|
+
},
|
|
423
603
|
context: input.context ?? {},
|
|
424
604
|
};
|
|
425
605
|
}
|
|
606
|
+
function triggerEphemeralThreadId(channelName, deliveryId, index) {
|
|
607
|
+
return uuidv5(`mda-thread:v1:trigger-ephemeral:${JSON.stringify([channelName, deliveryId, index])}`, uuidv5.URL);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* The thread an answer resumes: the conversation's, one of its subagents', or
|
|
611
|
+
* else the handler thread a run this conversation's message started waits on.
|
|
612
|
+
* A failed search of the handler threads resumes the conversation, as before.
|
|
613
|
+
*/
|
|
614
|
+
async function answeredThread(common, interruptIds, handlerThreads, options) {
|
|
615
|
+
const target = await (options.resumeTarget ?? ingressResumeTarget)({
|
|
616
|
+
...common,
|
|
617
|
+
interruptIds,
|
|
618
|
+
});
|
|
619
|
+
if (!handlerThreads || target.threadId !== common.threadId)
|
|
620
|
+
return { target };
|
|
621
|
+
try {
|
|
622
|
+
const parked = await (options.threadInterrupts ?? ingressThreadInterrupts)(common);
|
|
623
|
+
if (parked.some(({ id }) => id !== undefined && interruptIds.includes(id)))
|
|
624
|
+
return { target };
|
|
625
|
+
const handler = await (options.handlerResumeTarget ?? handlerThreadResumeTarget)({
|
|
626
|
+
channelName: options.channelName,
|
|
627
|
+
sourceProvider: common.sourceProvider,
|
|
628
|
+
startedFrom: common.sourceThreadKey,
|
|
629
|
+
interruptIds,
|
|
630
|
+
userId: common.userId,
|
|
631
|
+
});
|
|
632
|
+
return handler ? { target: handler, handler } : { target };
|
|
633
|
+
}
|
|
634
|
+
catch (error) {
|
|
635
|
+
logger.warn(`channel handler threads could not be searched; the answer resumes the conversation (${logger.safeErrorLabel(error)})`);
|
|
636
|
+
return { target };
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Whether a refused run is a conversation that handed its thread to a new
|
|
641
|
+
* one. Ingress creates a conversation's thread before its first run, so its
|
|
642
|
+
* own thread refuses a run with a not-found or conflict only once another
|
|
643
|
+
* conversation owns it, and then this one can no longer see it. Any other
|
|
644
|
+
* failure, or a failed lookup, is not a move.
|
|
645
|
+
*/
|
|
646
|
+
async function handedOff(error, options, common) {
|
|
647
|
+
const status = lookupStatus(error);
|
|
648
|
+
if (status !== 404 && status !== 409)
|
|
649
|
+
return false;
|
|
650
|
+
try {
|
|
651
|
+
return !(await (options.threadExists ?? ingressThreadExists)(common));
|
|
652
|
+
}
|
|
653
|
+
catch {
|
|
654
|
+
return false;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
426
657
|
function startFailedResponse() {
|
|
427
658
|
return Response.json({ error: "channel run could not be started" }, { status: 500 });
|
|
428
659
|
}
|
|
@@ -430,7 +661,9 @@ function startFailedResponse() {
|
|
|
430
661
|
* Stop the source thread's runs; each run's observer reports `run.cancelled`.
|
|
431
662
|
*
|
|
432
663
|
* Runs are cancelled as the thread's channel principal, so the managed owner
|
|
433
|
-
* filter confines the cancel to this source conversation.
|
|
664
|
+
* filter confines the cancel to this source conversation. On threads the
|
|
665
|
+
* channel's handlers chose, only the runs this conversation's messages
|
|
666
|
+
* started are cancelled; another conversation's run there goes on.
|
|
434
667
|
*/
|
|
435
668
|
async function cancelEventRuns(event, userId, options) {
|
|
436
669
|
let cancelled;
|
|
@@ -450,6 +683,21 @@ async function cancelEventRuns(event, userId, options) {
|
|
|
450
683
|
logger.error(`channel runs could not be cancelled (${logger.safeErrorLabel(error)})`);
|
|
451
684
|
return Response.json({ error: "channel runs could not be cancelled" }, { status: 500 });
|
|
452
685
|
}
|
|
686
|
+
// Only a channel with handlers starts runs on threads they chose.
|
|
687
|
+
if (options.decideMessage) {
|
|
688
|
+
try {
|
|
689
|
+
cancelled += await (options.cancelHandlerRuns ?? cancelHandlerThreadRuns)({
|
|
690
|
+
channelName: options.channelName,
|
|
691
|
+
sourceProvider: options.sourceProvider,
|
|
692
|
+
startedFrom: event.source_thread_key,
|
|
693
|
+
userId,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
// The conversation's own runs are cancelled; only a handler thread's may go on.
|
|
698
|
+
logger.error(`channel runs on handler threads could not be cancelled (${logger.safeErrorLabel(error)})`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
453
701
|
logger.info(`Channel runs cancelled (count=${cancelled}, delivery_id=${logger.logIdentifier(event.delivery_id)})`);
|
|
454
702
|
return acceptedResponse(event);
|
|
455
703
|
}
|
|
@@ -519,6 +767,30 @@ function interruptCorrelationId(value) {
|
|
|
519
767
|
return undefined;
|
|
520
768
|
return nonblankString(value[INTERRUPT_CORRELATION_KEY]);
|
|
521
769
|
}
|
|
770
|
+
/**
|
|
771
|
+
* The event as the Slack thread a run belongs to sees it: that thread's
|
|
772
|
+
* address and key, in the event's Slack app and workspace. The event as it
|
|
773
|
+
* is when its key isn't a Slack thread's.
|
|
774
|
+
*/
|
|
775
|
+
function inConversation(event, conversation) {
|
|
776
|
+
const parts = event.source_thread_key.split(":");
|
|
777
|
+
if (parts.length !== 6 || parts[0] !== "slack" || parts[1] !== "v1") {
|
|
778
|
+
logger.error(`channel run conversation ignored: the event is not from a Slack thread (delivery_id=${logger.logIdentifier(event.delivery_id)})`);
|
|
779
|
+
return event;
|
|
780
|
+
}
|
|
781
|
+
return {
|
|
782
|
+
...event,
|
|
783
|
+
address: {
|
|
784
|
+
conversation_id: conversation.conversation_id,
|
|
785
|
+
thread_id: conversation.thread_id,
|
|
786
|
+
},
|
|
787
|
+
source_thread_key: [
|
|
788
|
+
...parts.slice(0, 4),
|
|
789
|
+
conversation.conversation_id,
|
|
790
|
+
conversation.thread_id,
|
|
791
|
+
].join(":"),
|
|
792
|
+
};
|
|
793
|
+
}
|
|
522
794
|
async function observeAcceptedRun(input) {
|
|
523
795
|
const { event, started, client } = input;
|
|
524
796
|
const run = {
|
|
@@ -532,12 +804,13 @@ async function observeAcceptedRun(input) {
|
|
|
532
804
|
const replaceable = input.multitaskStrategy === "interrupt" ||
|
|
533
805
|
input.multitaskStrategy === "rollback";
|
|
534
806
|
/**
|
|
535
|
-
* A newer message's run
|
|
536
|
-
*
|
|
537
|
-
*
|
|
807
|
+
* A newer message's run, or the conversation the run handed its thread to,
|
|
808
|
+
* took the thread over and reports to Slack, so this run says nothing that
|
|
809
|
+
* would stop or fail the thread's status. It ended `interrupted`, as the
|
|
810
|
+
* HTTP channel reports a replaced run.
|
|
538
811
|
*/
|
|
539
|
-
const replaced = () => {
|
|
540
|
-
logger.info(`channel run
|
|
812
|
+
const replaced = (why = "was replaced by a newer message") => {
|
|
813
|
+
logger.info(`channel run ${why} (delivery_id=${logger.logIdentifier(event.delivery_id)}, run_id=${logger.logIdentifier(started.runId)})`);
|
|
541
814
|
return {
|
|
542
815
|
type: "run",
|
|
543
816
|
run: { id: started.runId, status: "interrupted" },
|
|
@@ -547,7 +820,7 @@ async function observeAcceptedRun(input) {
|
|
|
547
820
|
const lifecycle = async (type, message, status) => {
|
|
548
821
|
logger.info(`Channel outgoing event (type=${type}, delivery_id=${logger.logIdentifier(event.delivery_id)}, channel_id=${logger.logIdentifier(event.channel_id)}, run_id=${logger.logIdentifier(started.runId)})`);
|
|
549
822
|
const request = {
|
|
550
|
-
...runtimeEvent(type, event, started, message, await resolveTracerSessionId()),
|
|
823
|
+
...runtimeEvent(type, input.reportedFor ?? event, started, message, await resolveTracerSessionId()),
|
|
551
824
|
...status,
|
|
552
825
|
...(input.showProgress === false && { show_progress: false }),
|
|
553
826
|
};
|
|
@@ -568,8 +841,12 @@ async function observeAcceptedRun(input) {
|
|
|
568
841
|
observed = await (input.observeAgent ?? observeIngressAgent)(run);
|
|
569
842
|
}
|
|
570
843
|
catch (error) {
|
|
844
|
+
const notFound = lookupStatus(error) === 404;
|
|
845
|
+
// The run handed its thread to a new conversation, which reports it now.
|
|
846
|
+
if (notFound && (await input.handedOff?.(error)))
|
|
847
|
+
return replaced("handed its thread to a new conversation");
|
|
571
848
|
// `rollback` cancels, then deletes, the run a newer message replaced.
|
|
572
|
-
if (replaceable &&
|
|
849
|
+
if (replaceable && notFound)
|
|
573
850
|
return replaced();
|
|
574
851
|
await lifecycle("run.failed", missingConnectionFailureMessage(error));
|
|
575
852
|
return { type: "failed", reason: "observe", interrupts: [] };
|