@frontmcp/sdk 1.10.14 → 1.10.16
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/agent/flows/call-agent.flow.d.ts +1 -2
- package/agent/flows/call-agent.flow.d.ts.map +1 -1
- package/agent/hooks/agent.hooks.d.ts +4 -4
- package/agent/hooks/agent.hooks.d.ts.map +1 -1
- package/browser/agent/flows/call-agent.flow.mjs +2 -1
- package/browser/common/decorators/front-mcp.decorator.mjs +7 -0
- package/browser/common/decorators/hook.decorator.mjs +9 -1
- package/browser/common/decorators/index.mjs +1 -1
- package/browser/common/decorators/provider.decorator.mjs +1 -1
- package/browser/common/entries/tool.entry.mjs +52 -24
- package/browser/common/index.mjs +2 -2
- package/browser/common/interfaces/flow.interface.mjs +36 -0
- package/browser/common/interfaces/internal/flow.utils.mjs +3 -0
- package/browser/common/metadata/index.mjs +1 -1
- package/browser/common/metadata/output-policy.mjs +57 -11
- package/browser/common/metadata/provider.metadata.mjs +1 -1
- package/browser/common/schemas/annotated-class.schema.mjs +1 -1
- package/browser/common/types/options/skills-http/schema.mjs +2 -1
- package/browser/elicitation/helpers/extend-output-schema.mjs +3 -1
- package/browser/errors/index.mjs +9 -1
- package/browser/errors/transport.errors.mjs +31 -0
- package/browser/flows/flow.instance.mjs +103 -14
- package/browser/flows/flow.stages.mjs +23 -1
- package/browser/front-mcp/front-mcp.mjs +29 -30
- package/browser/front-mcp/shutdown-signals.mjs +2 -0
- package/browser/hooks/entry-class-hooks.mjs +10 -0
- package/browser/hooks/hook.registry.mjs +8 -8
- package/browser/index.cjs +1484 -1060
- package/browser/index.mjs +2 -2
- package/browser/job/flows/execute-job.flow.mjs +2 -1
- package/browser/prompt/flows/get-prompt.flow.mjs +6 -1
- package/browser/provider/provider.utils.mjs +5 -2
- package/browser/resource/flows/read-resource.flow.mjs +6 -1
- package/browser/scope/scope.instance.mjs +2 -0
- package/browser/server/security/resolve-allowed-hosts.mjs +32 -0
- package/browser/server/security/security-audit.mjs +3 -3
- package/browser/skill/sep-2640/sep-2640.per-skill.mjs +13 -11
- package/browser/skill/skill-instructions.helper.mjs +23 -8
- package/browser/skill/skill-scope.helper.mjs +2 -1
- package/browser/tool/flows/call-tool.flow.mjs +33 -2
- package/browser/tool/flows/tools-list.flow.mjs +9 -10
- package/browser/tool/tool-output-policy.mjs +24 -0
- package/browser/tool/tool.instance.mjs +4 -4
- package/browser/tool/tool.utils.mjs +49 -8
- package/common/decorators/front-mcp.decorator.d.ts.map +1 -1
- package/common/decorators/hook.decorator.d.ts +21 -10
- package/common/decorators/hook.decorator.d.ts.map +1 -1
- package/common/decorators/provider.decorator.d.ts +1 -1
- package/common/decorators/provider.decorator.d.ts.map +1 -1
- package/common/decorators/tool.decorator.d.ts +1 -1
- package/common/decorators/tool.decorator.d.ts.map +1 -1
- package/common/entries/tool.entry.d.ts +24 -11
- package/common/entries/tool.entry.d.ts.map +1 -1
- package/common/interfaces/flow.interface.d.ts +30 -1
- package/common/interfaces/flow.interface.d.ts.map +1 -1
- package/common/interfaces/internal/flow.utils.d.ts.map +1 -1
- package/common/metadata/agent.metadata.d.ts +3 -3
- package/common/metadata/app.metadata.d.ts +12 -6
- package/common/metadata/app.metadata.d.ts.map +1 -1
- package/common/metadata/front-mcp.metadata.d.ts +141 -70
- package/common/metadata/front-mcp.metadata.d.ts.map +1 -1
- package/common/metadata/hook.metadata.d.ts +3 -1
- package/common/metadata/hook.metadata.d.ts.map +1 -1
- package/common/metadata/output-policy.d.ts +32 -0
- package/common/metadata/output-policy.d.ts.map +1 -1
- package/common/metadata/plugin.metadata.d.ts +4 -4
- package/common/metadata/provider.metadata.d.ts +1 -1
- package/common/metadata/tool.metadata.d.ts +10 -3
- package/common/metadata/tool.metadata.d.ts.map +1 -1
- package/common/metadata/ui-serving-mode.d.ts +1 -1
- package/common/schemas/annotated-class.schema.d.ts +5 -5
- package/common/types/options/http/interfaces.d.ts +6 -2
- package/common/types/options/http/interfaces.d.ts.map +1 -1
- package/common/types/options/skills-http/index.d.ts +1 -1
- package/common/types/options/skills-http/index.d.ts.map +1 -1
- package/common/types/options/skills-http/interfaces.d.ts +27 -5
- package/common/types/options/skills-http/interfaces.d.ts.map +1 -1
- package/common/types/options/skills-http/schema.d.ts +8 -2
- package/common/types/options/skills-http/schema.d.ts.map +1 -1
- package/elicitation/helpers/extend-output-schema.d.ts.map +1 -1
- package/elicitation/hooks/elicitation.hooks.d.ts +10 -8
- package/elicitation/hooks/elicitation.hooks.d.ts.map +1 -1
- package/errors/index.d.ts +1 -1
- package/errors/index.d.ts.map +1 -1
- package/errors/transport.errors.d.ts +19 -0
- package/errors/transport.errors.d.ts.map +1 -1
- package/esm/index.mjs +31 -24
- package/flows/flow.instance.d.ts +10 -0
- package/flows/flow.instance.d.ts.map +1 -1
- package/flows/flow.stages.d.ts +4 -0
- package/flows/flow.stages.d.ts.map +1 -1
- package/front-mcp/front-mcp.d.ts +4 -0
- package/front-mcp/front-mcp.d.ts.map +1 -1
- package/front-mcp/shutdown-signals.d.ts +2 -0
- package/front-mcp/shutdown-signals.d.ts.map +1 -1
- package/hooks/entry-class-hooks.d.ts +4 -0
- package/hooks/entry-class-hooks.d.ts.map +1 -1
- package/hooks/hook.registry.d.ts +7 -7
- package/hooks/hook.registry.d.ts.map +1 -1
- package/index.d.ts +85 -68
- package/index.d.ts.map +1 -1
- package/index.js +37 -30
- package/job/flows/execute-job.flow.d.ts +1 -2
- package/job/flows/execute-job.flow.d.ts.map +1 -1
- package/package.json +10 -10
- package/prompt/flows/get-prompt.flow.d.ts +3 -2
- package/prompt/flows/get-prompt.flow.d.ts.map +1 -1
- package/provider/provider.utils.d.ts.map +1 -1
- package/resource/flows/read-resource.flow.d.ts +3 -2
- package/resource/flows/read-resource.flow.d.ts.map +1 -1
- package/scope/scope.instance.d.ts.map +1 -1
- package/server/adapters/express.host.adapter.d.ts +2 -2
- package/server/adapters/express.host.adapter.d.ts.map +1 -1
- package/server/security/resolve-allowed-hosts.d.ts +16 -0
- package/server/security/resolve-allowed-hosts.d.ts.map +1 -1
- package/server/security/security-audit.d.ts.map +1 -1
- package/server/security/unix-socket-guard.d.ts +12 -0
- package/server/security/unix-socket-guard.d.ts.map +1 -0
- package/skill/sep-2640/sep-2640.per-skill.d.ts +7 -5
- package/skill/sep-2640/sep-2640.per-skill.d.ts.map +1 -1
- package/skill/skill-instructions.helper.d.ts +15 -11
- package/skill/skill-instructions.helper.d.ts.map +1 -1
- package/skill/skill-scope.helper.d.ts.map +1 -1
- package/tool/flows/call-tool.flow.d.ts +7 -2
- package/tool/flows/call-tool.flow.d.ts.map +1 -1
- package/tool/flows/tools-list.flow.d.ts.map +1 -1
- package/tool/tool-output-policy.d.ts +33 -0
- package/tool/tool-output-policy.d.ts.map +1 -0
- package/tool/tool.instance.d.ts +3 -3
- package/tool/tool.instance.d.ts.map +1 -1
- package/tool/tool.utils.d.ts +2 -2
- package/tool/tool.utils.d.ts.map +1 -1
- package/transport/mcp-handlers/index.d.ts +549 -549
- package/transport/mcp-handlers/index.d.ts.map +1 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// libs/sdk/src/flows/flow.instance.ts
|
|
2
2
|
import "reflect-metadata";
|
|
3
|
+
import { tokenName } from "@frontmcp/di";
|
|
4
|
+
import { isDebug, isDevelopment } from "@frontmcp/utils";
|
|
3
5
|
import {
|
|
4
6
|
FlowControl,
|
|
5
7
|
FlowEntry,
|
|
@@ -9,6 +11,7 @@ import { FRONTMCP_CONTEXT, FrontMcpContextStorage } from "../context/index.mjs";
|
|
|
9
11
|
import { runAsFlow } from "../context/running-flow.mjs";
|
|
10
12
|
import { InternalMcpError, PublicMcpError, RequestContextNotAvailableError } from "../errors/index.mjs";
|
|
11
13
|
import { findMisconfiguration, misconfigurationBody } from "../errors/misconfiguration.mjs";
|
|
14
|
+
import { entryContextStageOf } from "../hooks/entry-class-hooks.mjs";
|
|
12
15
|
import { bindContextHookTargets, staticHooksFor } from "../hooks/hooks.utils.mjs";
|
|
13
16
|
import { writeHttpResponse } from "../server/server.validation.mjs";
|
|
14
17
|
import { flowErrorToHttpOutput } from "../transport/flow-error-output.mjs";
|
|
@@ -16,6 +19,7 @@ import { matchMountedPath } from "./flow.http-path.mjs";
|
|
|
16
19
|
import {
|
|
17
20
|
cloneStageMap,
|
|
18
21
|
collectFlowHookMap,
|
|
22
|
+
describeHookOrder,
|
|
19
23
|
mergeHookMetasIntoStageMap
|
|
20
24
|
} from "./flow.stages.mjs";
|
|
21
25
|
var HOOK_OBSERVER_KEY = /* @__PURE__ */ Symbol.for("frontmcp:hook-observer");
|
|
@@ -25,7 +29,12 @@ var stageKeysByName = /* @__PURE__ */ new Map();
|
|
|
25
29
|
function stageKeysOf(stage) {
|
|
26
30
|
let keys = stageKeysByName.get(stage);
|
|
27
31
|
if (!keys) {
|
|
28
|
-
keys = {
|
|
32
|
+
keys = {
|
|
33
|
+
will: `will${CAP(stage)}`,
|
|
34
|
+
did: `did${CAP(stage)}`,
|
|
35
|
+
around: `around${CAP(stage)}`,
|
|
36
|
+
finally: `finally${CAP(stage)}`
|
|
37
|
+
};
|
|
29
38
|
stageKeysByName.set(stage, keys);
|
|
30
39
|
}
|
|
31
40
|
return keys;
|
|
@@ -36,8 +45,22 @@ function parseTypedKey(k) {
|
|
|
36
45
|
if (lc.startsWith("did") && k.length > 3) return { base: k.slice(3, 4).toLowerCase() + k.slice(4), type: "did" };
|
|
37
46
|
if (lc.startsWith("around") && k.length > 6)
|
|
38
47
|
return { base: k.slice(6, 7).toLowerCase() + k.slice(7), type: "around" };
|
|
48
|
+
if (lc.startsWith("finally") && k.length > 7)
|
|
49
|
+
return { base: k.slice(7, 8).toLowerCase() + k.slice(8), type: "finally" };
|
|
39
50
|
return { base: k };
|
|
40
51
|
}
|
|
52
|
+
function describeValue(value) {
|
|
53
|
+
try {
|
|
54
|
+
return String(value);
|
|
55
|
+
} catch {
|
|
56
|
+
return Object.prototype.toString.call(value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function failureOf(control) {
|
|
60
|
+
const originalError = control.originalError;
|
|
61
|
+
if (originalError !== void 0) return originalError;
|
|
62
|
+
return control.type === "abort" ? new InternalMcpError(`Flow aborted: ${String(control.output)}`) : control;
|
|
63
|
+
}
|
|
41
64
|
var stageBasesByGroup = /* @__PURE__ */ new WeakMap();
|
|
42
65
|
function stageBasesOf(group) {
|
|
43
66
|
let bases = stageBasesByGroup.get(group);
|
|
@@ -46,14 +69,24 @@ function stageBasesOf(group) {
|
|
|
46
69
|
bases = [];
|
|
47
70
|
for (const rawKey of group) {
|
|
48
71
|
const { base, type } = parseTypedKey(rawKey);
|
|
49
|
-
|
|
50
|
-
seen.
|
|
51
|
-
|
|
72
|
+
const stage = type === "finally" && !group.includes(base) ? rawKey : base;
|
|
73
|
+
if (type && stage === base || seen.has(stage)) continue;
|
|
74
|
+
seen.add(stage);
|
|
75
|
+
bases.push(stage);
|
|
52
76
|
}
|
|
53
77
|
stageBasesByGroup.set(group, bases);
|
|
54
78
|
}
|
|
55
79
|
return bases;
|
|
56
80
|
}
|
|
81
|
+
var FLOW_PHASES = ["pre", "execute", "post", "error", "finalize"];
|
|
82
|
+
function listJoinedAfterStep(list, present, step) {
|
|
83
|
+
const stepIndex = list.indexOf(step);
|
|
84
|
+
if (stepIndex === -1) return list;
|
|
85
|
+
const joinedBefore = list.slice(0, stepIndex).filter((entry) => !present.includes(entry));
|
|
86
|
+
const rest = list.filter((entry) => !joinedBefore.includes(entry));
|
|
87
|
+
rest.splice(rest.indexOf(step) + 1, 0, ...joinedBefore);
|
|
88
|
+
return rest;
|
|
89
|
+
}
|
|
57
90
|
function nextStageOrder(stages) {
|
|
58
91
|
return Math.max(0, ...Object.values(stages).flatMap((list) => list.map((e) => e._order ?? 0))) + 1;
|
|
59
92
|
}
|
|
@@ -66,6 +99,8 @@ var FlowInstance = class extends FlowEntry {
|
|
|
66
99
|
hooks;
|
|
67
100
|
logger;
|
|
68
101
|
baseStageOrderEnd;
|
|
102
|
+
/** In dev mode or with DEBUG only: per hook owner, the entry classes (`undefined`: none) whose hook order is logged. */
|
|
103
|
+
loggedHookOrders = isDevelopment() || isDebug() ? /* @__PURE__ */ new Map() : void 0;
|
|
69
104
|
constructor(scope, record, deps, globalProviders) {
|
|
70
105
|
super(scope, record);
|
|
71
106
|
this.deps = [...deps];
|
|
@@ -317,6 +352,7 @@ var FlowInstance = class extends FlowEntry {
|
|
|
317
352
|
await materializeAndMerge(initialInjectedHooks, { orderStart: -1e6 });
|
|
318
353
|
if (ownsStages) orderBase = nextStageOrder(stages);
|
|
319
354
|
await materializeAndMerge(entryStaticHooks);
|
|
355
|
+
if (this.loggedHookOrders) this.logHookOrderOnce(this.loggedHookOrders, stages, hookOwnerId, entryClass);
|
|
320
356
|
let responded;
|
|
321
357
|
const toStageResult = (e) => {
|
|
322
358
|
if (e instanceof FlowControl) {
|
|
@@ -330,6 +366,20 @@ var FlowInstance = class extends FlowEntry {
|
|
|
330
366
|
return { outcome: "unknown_error", control: e };
|
|
331
367
|
};
|
|
332
368
|
const hasEntries = (key) => (stages[key]?.length ?? 0) > 0;
|
|
369
|
+
const ignoreFinallyThrow = (key, thrown) => {
|
|
370
|
+
const description = thrown instanceof FlowControl ? `FlowControl '${thrown.type}'` : describeValue(thrown);
|
|
371
|
+
this.logger.warn(`Ignored a throw from a Finally hook of '${key}': it cannot change the stage's outcome`, {
|
|
372
|
+
flow: name,
|
|
373
|
+
thrown: description
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
const restoreState = (before) => {
|
|
377
|
+
const state = context.state;
|
|
378
|
+
for (const key of Object.keys(context.state.snapshot())) {
|
|
379
|
+
if (!(key in before)) delete state[key];
|
|
380
|
+
}
|
|
381
|
+
context.state.set(before);
|
|
382
|
+
};
|
|
333
383
|
const runList = async (key, opts) => {
|
|
334
384
|
const seen = /* @__PURE__ */ new Set();
|
|
335
385
|
while (true) {
|
|
@@ -343,9 +393,15 @@ var FlowInstance = class extends FlowEntry {
|
|
|
343
393
|
}
|
|
344
394
|
if (!item) break;
|
|
345
395
|
seen.add(item);
|
|
396
|
+
const stateBefore = opts?.ignoreThrows ? context.state.snapshot() : void 0;
|
|
346
397
|
try {
|
|
347
398
|
await item.method(context);
|
|
348
399
|
} catch (e) {
|
|
400
|
+
if (opts?.ignoreThrows) {
|
|
401
|
+
if (stateBefore) restoreState(stateBefore);
|
|
402
|
+
ignoreFinallyThrow(key, e);
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
349
405
|
if (e instanceof FlowControl && e.type === "respond" && opts?.ignoreRespond) continue;
|
|
350
406
|
return toStageResult(e);
|
|
351
407
|
}
|
|
@@ -367,7 +423,13 @@ var FlowInstance = class extends FlowEntry {
|
|
|
367
423
|
}
|
|
368
424
|
return FAILED_OUTCOMES.has(innerResult.outcome) ? { outcome: "ok" } : innerResult;
|
|
369
425
|
};
|
|
370
|
-
const
|
|
426
|
+
const recordFailure = (control) => {
|
|
427
|
+
if (context.state.get("flowError")) return;
|
|
428
|
+
const failure = control instanceof FlowControl ? failureOf(control) : control;
|
|
429
|
+
const flowError = failure instanceof Error ? failure : new InternalMcpError(`Flow failed with a non-Error value: ${describeValue(failure)}`);
|
|
430
|
+
context.state.set("flowError", flowError);
|
|
431
|
+
};
|
|
432
|
+
const runStageSteps = async (stageName, stopOnRespond) => {
|
|
371
433
|
const keys = stageKeysOf(stageName);
|
|
372
434
|
if (hasEntries(keys.will)) {
|
|
373
435
|
const res = await runList(keys.will);
|
|
@@ -385,6 +447,13 @@ var FlowInstance = class extends FlowEntry {
|
|
|
385
447
|
if (bodyOutcome.outcome === "respond") return bodyOutcome;
|
|
386
448
|
return { outcome: "ok" };
|
|
387
449
|
};
|
|
450
|
+
const runOneStage = async (stageName, stopOnRespond) => {
|
|
451
|
+
const result = await runStageSteps(stageName, stopOnRespond);
|
|
452
|
+
if (FAILED_OUTCOMES.has(result.outcome)) recordFailure(result.control);
|
|
453
|
+
const finallyKey = stageKeysOf(stageName).finally;
|
|
454
|
+
if (hasEntries(finallyKey)) await runList(finallyKey, { ignoreThrows: true });
|
|
455
|
+
return result;
|
|
456
|
+
};
|
|
388
457
|
const runStageGroup = async (group, stopOnRespond, opts) => {
|
|
389
458
|
if (!group || group.length === 0) return { outcome: "ok" };
|
|
390
459
|
for (const base of stageBasesOf(group)) {
|
|
@@ -397,10 +466,6 @@ var FlowInstance = class extends FlowEntry {
|
|
|
397
466
|
}
|
|
398
467
|
return { outcome: "ok" };
|
|
399
468
|
};
|
|
400
|
-
const recordFailure = (control) => {
|
|
401
|
-
const flowError = control instanceof Error ? control : new InternalMcpError(`Flow failed with a non-Error value: ${String(control)}`);
|
|
402
|
-
context.state.set("flowError", flowError);
|
|
403
|
-
};
|
|
404
469
|
const runErrorStage = async () => {
|
|
405
470
|
await runStageGroup(plan.error, false, { ignoreRespond: true });
|
|
406
471
|
};
|
|
@@ -425,7 +490,6 @@ var FlowInstance = class extends FlowEntry {
|
|
|
425
490
|
if (pre.outcome === "respond") {
|
|
426
491
|
const post = await runStageGroup(plan.post, false);
|
|
427
492
|
if (post.outcome === "unknown_error" || post.outcome === "fail") {
|
|
428
|
-
recordFailure(post.control);
|
|
429
493
|
try {
|
|
430
494
|
await runErrorStage();
|
|
431
495
|
} finally {
|
|
@@ -444,7 +508,6 @@ var FlowInstance = class extends FlowEntry {
|
|
|
444
508
|
return responded;
|
|
445
509
|
}
|
|
446
510
|
if (pre.outcome === "unknown_error" || pre.outcome === "fail") {
|
|
447
|
-
recordFailure(pre.control);
|
|
448
511
|
try {
|
|
449
512
|
await runErrorStage();
|
|
450
513
|
} finally {
|
|
@@ -465,7 +528,6 @@ var FlowInstance = class extends FlowEntry {
|
|
|
465
528
|
this.logger.verbose(`run: EXECUTE completed, outcome=${exec.outcome}`);
|
|
466
529
|
if (exec.outcome === "respond") {
|
|
467
530
|
} else if (exec.outcome === "unknown_error" || exec.outcome === "fail") {
|
|
468
|
-
recordFailure(exec.control);
|
|
469
531
|
try {
|
|
470
532
|
await runErrorStage();
|
|
471
533
|
} finally {
|
|
@@ -484,7 +546,6 @@ var FlowInstance = class extends FlowEntry {
|
|
|
484
546
|
const post = await runStageGroup(plan.post, false);
|
|
485
547
|
this.logger.verbose(`run: POST completed, outcome=${post.outcome}`);
|
|
486
548
|
if (post.outcome === "unknown_error" || post.outcome === "fail") {
|
|
487
|
-
recordFailure(post.control);
|
|
488
549
|
try {
|
|
489
550
|
await runErrorStage();
|
|
490
551
|
} finally {
|
|
@@ -503,7 +564,35 @@ var FlowInstance = class extends FlowEntry {
|
|
|
503
564
|
await runFinalizeStage();
|
|
504
565
|
return responded;
|
|
505
566
|
}
|
|
567
|
+
/** Logs the order the run's hooks resolve to, on the first run for its hook owner and its entry class's hooks. */
|
|
568
|
+
logHookOrderOnce(logged, stages, ownerId, entryClass) {
|
|
569
|
+
const entryHooks = entryClass ? this.hooks.getClsHooks(entryClass).filter((hook) => hook.metadata.flow === this.name) : [];
|
|
570
|
+
const entryKey = entryHooks.length > 0 ? entryClass : void 0;
|
|
571
|
+
const loggedEntries = logged.get(ownerId) ?? /* @__PURE__ */ new Set();
|
|
572
|
+
if (loggedEntries.has(entryKey)) return;
|
|
573
|
+
logged.set(ownerId, loggedEntries.add(entryKey));
|
|
574
|
+
const resolved = cloneStageMap(stages);
|
|
575
|
+
const instanceHooks = entryHooks.filter((hook) => !hook.metadata.static).map((hook) => hook.metadata);
|
|
576
|
+
mergeHookMetasIntoStageMap(this.FlowClass, resolved, instanceHooks, nextStageOrder(stages));
|
|
577
|
+
const contextStage = entryContextStageOf(this.name);
|
|
578
|
+
const contextStep = contextStage ? this.stages[contextStage]?.[0] : void 0;
|
|
579
|
+
if (contextStage && contextStep && resolved[contextStage]) {
|
|
580
|
+
resolved[contextStage] = listJoinedAfterStep(resolved[contextStage], stages[contextStage] ?? [], contextStep);
|
|
581
|
+
}
|
|
582
|
+
const stageNames = FLOW_PHASES.flatMap((phase) => {
|
|
583
|
+
const group = this.plan[phase];
|
|
584
|
+
return group ? stageBasesOf(group) : [];
|
|
585
|
+
});
|
|
586
|
+
const lines = describeHookOrder(stageNames, this.stages, resolved, this.FlowClass.name);
|
|
587
|
+
if (lines.length === 0) return;
|
|
588
|
+
const owner = ownerId === void 0 ? "" : ` for '${ownerId}'`;
|
|
589
|
+
const entry = entryKey === void 0 ? "" : ` and ${tokenName(entryKey)}`;
|
|
590
|
+
const header = `Hook order of '${this.name}'${owner}${entry}, lowest priority first (an around hook wraps what its brackets hold):`;
|
|
591
|
+
this.logger.info([header, ...lines].join("\n"));
|
|
592
|
+
}
|
|
506
593
|
};
|
|
507
594
|
export {
|
|
508
|
-
FlowInstance
|
|
595
|
+
FlowInstance,
|
|
596
|
+
parseTypedKey,
|
|
597
|
+
stageBasesOf
|
|
509
598
|
};
|
|
@@ -15,7 +15,7 @@ function cloneStageMap(table) {
|
|
|
15
15
|
return out;
|
|
16
16
|
}
|
|
17
17
|
function resolveStageKey(type, stage) {
|
|
18
|
-
return type === "
|
|
18
|
+
return type === "stage" ? stage : `${type}${cap(stage)}`;
|
|
19
19
|
}
|
|
20
20
|
function collectFlowHookMap(FlowClass) {
|
|
21
21
|
const cached = CACHE.get(FlowClass);
|
|
@@ -31,6 +31,7 @@ function collectFlowHookMap(FlowClass) {
|
|
|
31
31
|
for (const m of sorted) {
|
|
32
32
|
const resolved = resolveStageKey(m.type, m.stage);
|
|
33
33
|
const entry = {
|
|
34
|
+
hook: m,
|
|
34
35
|
_priority: m.priority ?? 0,
|
|
35
36
|
_order: order++,
|
|
36
37
|
method: async (ctx, next = NO_NEXT) => {
|
|
@@ -58,6 +59,7 @@ function mergeHookMetasIntoStageMap(FlowClass, table, metas, orderStart = 1e5, r
|
|
|
58
59
|
for (const m of sorted) {
|
|
59
60
|
const resolved = resolveStageKey(m.type, m.stage);
|
|
60
61
|
const entry = {
|
|
62
|
+
hook: m,
|
|
61
63
|
_priority: m.priority ?? 0,
|
|
62
64
|
_order: order++,
|
|
63
65
|
method: async (ctx, next = NO_NEXT) => {
|
|
@@ -80,9 +82,29 @@ function mergeHookMetasIntoStageMap(FlowClass, table, metas, orderStart = 1e5, r
|
|
|
80
82
|
}
|
|
81
83
|
sortStageMap(table);
|
|
82
84
|
}
|
|
85
|
+
var HOOK_TYPES_IN_RUN_ORDER = ["will", "around", "stage", "did", "finally"];
|
|
86
|
+
function describeStep(hook, flowClassName) {
|
|
87
|
+
const target = hook.target;
|
|
88
|
+
const owner = typeof target === "function" ? target.name : target && typeof target === "object" ? target.constructor.name : "";
|
|
89
|
+
return `${hook.type} ${owner || flowClassName}.${hook.method} (${hook.priority ?? 0})`;
|
|
90
|
+
}
|
|
91
|
+
function describeHookOrder(stages, base, resolved, flowClassName) {
|
|
92
|
+
const lines = [];
|
|
93
|
+
for (const stage of stages) {
|
|
94
|
+
const keys = HOOK_TYPES_IN_RUN_ORDER.map((type) => resolveStageKey(type, stage));
|
|
95
|
+
if (!keys.some((key) => (resolved[key]?.length ?? 0) > (base[key]?.length ?? 0))) continue;
|
|
96
|
+
const [will, around, body, did, after] = keys.map(
|
|
97
|
+
(key) => (resolved[key] ?? []).map((entry) => describeStep(entry.hook, flowClassName))
|
|
98
|
+
);
|
|
99
|
+
const wrapped = around.reduceRight((inner, outer) => `${outer} [ ${inner} ]`, body.join(" \u2192 "));
|
|
100
|
+
lines.push(` ${stage}: ${[...will, wrapped, ...did, ...after].filter(Boolean).join(" \u2192 ")}`);
|
|
101
|
+
}
|
|
102
|
+
return lines;
|
|
103
|
+
}
|
|
83
104
|
export {
|
|
84
105
|
cloneStageMap,
|
|
85
106
|
collectFlowHookMap,
|
|
107
|
+
describeHookOrder,
|
|
86
108
|
mergeHookMetasIntoStageMap,
|
|
87
109
|
resolveStageKey,
|
|
88
110
|
sortStageMap
|
|
@@ -28,7 +28,7 @@ import { composeCallerInstructions } from "../skill/skill-instructions.helper.mj
|
|
|
28
28
|
import { computeTaskCapabilities } from "../task/task-scope.helper.mjs";
|
|
29
29
|
import { markDirectOnly } from "./direct-only.flag.mjs";
|
|
30
30
|
import { createMcpGlobalProviders } from "./front-mcp.providers.mjs";
|
|
31
|
-
import { DEFAULT_SHUTDOWN_DEADLINE_MS, exitOnShutdownSignals } from "./shutdown-signals.mjs";
|
|
31
|
+
import { DEFAULT_SHUTDOWN_DEADLINE_MS, exitOnShutdownSignals, STDIO_SHUTDOWN_DEADLINE_MS } from "./shutdown-signals.mjs";
|
|
32
32
|
var stdioServing = false;
|
|
33
33
|
function resolveConfigInput(optionsOrClass) {
|
|
34
34
|
if (typeof optionsOrClass === "function") {
|
|
@@ -57,21 +57,23 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
57
57
|
}
|
|
58
58
|
/** Shut down what a failed start built, for at most the signal path's deadline, then fail with its error. */
|
|
59
59
|
async releaseFailedStart(error) {
|
|
60
|
+
await this.releaseWithin(DEFAULT_SHUTDOWN_DEADLINE_MS);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
/** Shut down what a failed start built, waiting at most `deadlineMs` for it. */
|
|
64
|
+
async releaseWithin(deadlineMs) {
|
|
60
65
|
let deadline;
|
|
61
66
|
const outlasted = new Promise((resolve) => {
|
|
62
|
-
deadline = setTimeout(() => resolve(true),
|
|
67
|
+
deadline = setTimeout(() => resolve(true), deadlineMs);
|
|
63
68
|
});
|
|
64
69
|
const released = this.shutdown().then(
|
|
65
70
|
() => false,
|
|
66
71
|
() => false
|
|
67
72
|
);
|
|
68
73
|
if (await Promise.race([released, outlasted])) {
|
|
69
|
-
this.log?.error(
|
|
70
|
-
`Releasing a server that failed to start did not finish within ${DEFAULT_SHUTDOWN_DEADLINE_MS} ms`
|
|
71
|
-
);
|
|
74
|
+
this.log?.error(`Releasing a server that failed to start did not finish within ${deadlineMs} ms`);
|
|
72
75
|
}
|
|
73
76
|
clearTimeout(deadline);
|
|
74
|
-
throw error;
|
|
75
77
|
}
|
|
76
78
|
async initialize() {
|
|
77
79
|
this.providers = new ProviderRegistry([...createMcpGlobalProviders(this.config)]);
|
|
@@ -103,6 +105,14 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
103
105
|
`);
|
|
104
106
|
}
|
|
105
107
|
}
|
|
108
|
+
/** Start, or shut down what was built when the start fails (a refused socket, a port in use), then rethrow. */
|
|
109
|
+
async startOrRelease() {
|
|
110
|
+
try {
|
|
111
|
+
await this.start();
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return this.releaseFailedStart(error);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
106
116
|
/** Where this server serves MCP, as reported to the `frontmcp dev --stdio` bridge. */
|
|
107
117
|
devListenerInfo(server) {
|
|
108
118
|
const http = server.config;
|
|
@@ -260,7 +270,7 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
260
270
|
}
|
|
261
271
|
const frontMcp = new _FrontMcpInstance(parsedConfig);
|
|
262
272
|
await frontMcp.ready;
|
|
263
|
-
await frontMcp.
|
|
273
|
+
await frontMcp.startOrRelease();
|
|
264
274
|
exitOnShutdownSignals(() => frontMcp.shutdown(), { logger: frontMcp.log });
|
|
265
275
|
frontMcp.log?.info("FrontMCP bootstrap complete");
|
|
266
276
|
return frontMcp;
|
|
@@ -502,7 +512,7 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
502
512
|
});
|
|
503
513
|
const frontMcp = new _FrontMcpInstance(parsedConfig);
|
|
504
514
|
await frontMcp.ready;
|
|
505
|
-
await frontMcp.
|
|
515
|
+
await frontMcp.startOrRelease();
|
|
506
516
|
frontMcp.log?.info(`MCP server listening on unix://${socketPath}`);
|
|
507
517
|
const shutdown = () => frontMcp.shutdown();
|
|
508
518
|
const removeSignalHandlers = exitOnShutdownSignals(shutdown, { logger: frontMcp.log });
|
|
@@ -597,11 +607,7 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
597
607
|
}
|
|
598
608
|
const frontMcp = new _FrontMcpInstance(parsedConfig);
|
|
599
609
|
await frontMcp.ready;
|
|
600
|
-
startupCleanups.push(
|
|
601
|
-
for (const s of frontMcp.getScopes()) {
|
|
602
|
-
await s.shutdown();
|
|
603
|
-
}
|
|
604
|
-
});
|
|
610
|
+
startupCleanups.push(() => frontMcp.releaseWithin(STDIO_SHUTDOWN_DEADLINE_MS));
|
|
605
611
|
const primaryScope = frontMcp.getPrimaryScope();
|
|
606
612
|
if (!primaryScope) {
|
|
607
613
|
throw new InternalMcpError("No scopes initialized. Ensure at least one app is configured.");
|
|
@@ -663,24 +669,17 @@ var FrontMcpInstance = class _FrontMcpInstance {
|
|
|
663
669
|
const channelNames = scope.channels.getChannelInstances().map((ch) => ch.name);
|
|
664
670
|
scope.notifications.subscribeAllChannels(sessionId, channelNames);
|
|
665
671
|
}
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
if (shuttingDown) return;
|
|
669
|
-
shuttingDown = true;
|
|
670
|
-
const deadline = setTimeout(() => process.exit(1), 5e3);
|
|
671
|
-
try {
|
|
672
|
+
exitOnShutdownSignals(
|
|
673
|
+
async () => {
|
|
672
674
|
scope.notifications.unregisterServer(sessionId);
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
};
|
|
682
|
-
process.on("SIGINT", shutdownHandler);
|
|
683
|
-
process.on("SIGTERM", shutdownHandler);
|
|
675
|
+
try {
|
|
676
|
+
await frontMcp.shutdown();
|
|
677
|
+
} finally {
|
|
678
|
+
await mcpServer.close();
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
{ logger: frontMcp.log, deadlineMs: STDIO_SHUTDOWN_DEADLINE_MS }
|
|
682
|
+
);
|
|
684
683
|
} finally {
|
|
685
684
|
if (!startupSucceeded) {
|
|
686
685
|
for (const cleanup of startupCleanups.reverse()) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// libs/sdk/src/front-mcp/shutdown-signals.ts
|
|
2
2
|
var SHUTDOWN_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
3
3
|
var DEFAULT_SHUTDOWN_DEADLINE_MS = 1e4;
|
|
4
|
+
var STDIO_SHUTDOWN_DEADLINE_MS = 5e3;
|
|
4
5
|
function exitOnShutdownSignals(shutdown, options = {}) {
|
|
5
6
|
const target = options.target ?? process;
|
|
6
7
|
const deadlineMs = options.deadlineMs ?? DEFAULT_SHUTDOWN_DEADLINE_MS;
|
|
@@ -28,5 +29,6 @@ function exitOnShutdownSignals(shutdown, options = {}) {
|
|
|
28
29
|
}
|
|
29
30
|
export {
|
|
30
31
|
DEFAULT_SHUTDOWN_DEADLINE_MS,
|
|
32
|
+
STDIO_SHUTDOWN_DEADLINE_MS,
|
|
31
33
|
exitOnShutdownSignals
|
|
32
34
|
};
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
// libs/sdk/src/hooks/entry-class-hooks.ts
|
|
2
|
+
var contextStageByFlow = /* @__PURE__ */ new Map();
|
|
3
|
+
function entryClassHooksJoin(join) {
|
|
4
|
+
contextStageByFlow.set(join.flow, join.contextStage);
|
|
5
|
+
return join;
|
|
6
|
+
}
|
|
7
|
+
function entryContextStageOf(flow) {
|
|
8
|
+
return contextStageByFlow.get(flow);
|
|
9
|
+
}
|
|
2
10
|
function stagesInOrder(plan) {
|
|
3
11
|
return [
|
|
4
12
|
...plan.pre ?? [],
|
|
@@ -35,5 +43,7 @@ function unreachableHooksMessage(kind, className, problems) {
|
|
|
35
43
|
}
|
|
36
44
|
export {
|
|
37
45
|
describeUnreachableEntryClassHooks,
|
|
46
|
+
entryClassHooksJoin,
|
|
47
|
+
entryContextStageOf,
|
|
38
48
|
unreachableHooksMessage
|
|
39
49
|
};
|
|
@@ -30,7 +30,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
30
30
|
scope;
|
|
31
31
|
/** Historical records by class (kept if you still want access to raw records) */
|
|
32
32
|
recordsByCls = /* @__PURE__ */ new Map();
|
|
33
|
-
/** Fast O(1) indexes of *instances*,
|
|
33
|
+
/** Fast O(1) indexes of *instances*, in the order a stage runs them: lowest priority first, then registration order */
|
|
34
34
|
entriesByCls = /* @__PURE__ */ new Map();
|
|
35
35
|
hooksByFlow = /* @__PURE__ */ new Map();
|
|
36
36
|
hooksByFlowStage = /* @__PURE__ */ new Map();
|
|
@@ -54,7 +54,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
54
54
|
getPriority(entry) {
|
|
55
55
|
return entry.metadata?.priority ?? 0;
|
|
56
56
|
}
|
|
57
|
-
/** Binary insert
|
|
57
|
+
/** Binary insert, lowest priority first, after the items of an equal priority. */
|
|
58
58
|
insertSorted(arr, item) {
|
|
59
59
|
const p = this.getPriority(item);
|
|
60
60
|
let lo = 0;
|
|
@@ -62,7 +62,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
62
62
|
while (lo < hi) {
|
|
63
63
|
const mid = lo + hi >>> 1;
|
|
64
64
|
const mp = this.getPriority(arr[mid]);
|
|
65
|
-
if (mp
|
|
65
|
+
if (mp > p) {
|
|
66
66
|
hi = mid;
|
|
67
67
|
} else {
|
|
68
68
|
lo = mid + 1;
|
|
@@ -131,7 +131,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
131
131
|
inheritFrom(parent, ownerId) {
|
|
132
132
|
this.inherited = { registry: parent, ownerId };
|
|
133
133
|
}
|
|
134
|
-
/** Hooks for a given *flow*, filtered by owner if provided,
|
|
134
|
+
/** Hooks for a given *flow*, filtered by owner if provided, in run order. */
|
|
135
135
|
getFlowHooksForOwner(flow, ownerId) {
|
|
136
136
|
const own = this.getOwnFlowHooksForOwner(flow, ownerId);
|
|
137
137
|
if (!this.inherited) return own;
|
|
@@ -141,7 +141,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
141
141
|
for (const hook of inherited) this.insertSorted(merged, hook);
|
|
142
142
|
return merged;
|
|
143
143
|
}
|
|
144
|
-
/** This registry's own hooks for a given *flow*, filtered by owner if provided,
|
|
144
|
+
/** This registry's own hooks for a given *flow*, filtered by owner if provided, in run order. */
|
|
145
145
|
getOwnFlowHooksForOwner(flow, ownerId) {
|
|
146
146
|
const allHooks = this.getFlowHooks(flow);
|
|
147
147
|
if (!ownerId) {
|
|
@@ -154,7 +154,7 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
154
154
|
(hook) => own.has(hook) || hook.metadata.appliesTo === "uncovered-apps" && !ownHooks.some((ownHook) => isSameHook(ownHook, hook))
|
|
155
155
|
);
|
|
156
156
|
}
|
|
157
|
-
/** Hooks defined on a given *class* (metadata.target),
|
|
157
|
+
/** Hooks defined on a given *class* (metadata.target), in run order. */
|
|
158
158
|
getClsHooks(token) {
|
|
159
159
|
return this.entriesByCls.get(token) ?? [];
|
|
160
160
|
}
|
|
@@ -162,11 +162,11 @@ var HookRegistry = class extends RegistryAbstract {
|
|
|
162
162
|
getHooks() {
|
|
163
163
|
return [...this.instances.values()];
|
|
164
164
|
}
|
|
165
|
-
/** Hooks for a given *flow*,
|
|
165
|
+
/** Hooks for a given *flow*, in run order. */
|
|
166
166
|
getFlowHooks(flow) {
|
|
167
167
|
return this.hooksByFlow.get(flow) ?? [];
|
|
168
168
|
}
|
|
169
|
-
/** Hooks for a specific *flow + stage*,
|
|
169
|
+
/** Hooks for a specific *flow + stage*, in run order. */
|
|
170
170
|
getFlowStageHooks(flow, stage) {
|
|
171
171
|
const byStage = this.hooksByFlowStage.get(flow);
|
|
172
172
|
return byStage?.get(String(stage)) ?? [];
|