mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
* chore: Prune redundant comments and normalize lint directives Removes comments that restate the following line and stale TODOs, drops the passive event listener feature check now that all supported browsers support it, and renames eslint directives to oxlint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * fix: Evaluate restricted team domains at validation time The restricted email provider list was captured when the model loaded, so it could not follow the hosting environment and the tests that covered it never reached the model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * chore: Replace type suppressions with accurate types Adds a Concrete helper type for registries of abstract classes, declares missing Sequelize and refractor typings, uses public ProseMirror APIs, a typed mock context helper in middleware tests, and a StaticLink component for sidebar rows being edited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH --------- Co-authored-by: Claude <noreply@anthropic.com>
148 lines
4.4 KiB
TypeScript
148 lines
4.4 KiB
TypeScript
import { snakeCase } from "es-toolkit";
|
|
import * as React from "react";
|
|
import { TeamPreference } from "@shared/types";
|
|
import { toError } from "@shared/utils/error";
|
|
import { performAction, resolve } from "~/actions";
|
|
import type {
|
|
ActionContext,
|
|
ActionGroup,
|
|
ActionSeparator,
|
|
ActionVariant,
|
|
ActionWithChildren,
|
|
} from "~/types";
|
|
import Analytics from "~/utils/Analytics";
|
|
import {
|
|
isModelContextSupported,
|
|
registerModelContextTool,
|
|
} from "~/utils/ModelContext";
|
|
import useActionContext from "./useActionContext";
|
|
import { useLocationSelector } from "./useLocationSelector";
|
|
import useStores from "./useStores";
|
|
|
|
type PerformableAction = Exclude<ActionVariant, ActionWithChildren>;
|
|
|
|
/**
|
|
* Hook to expose actions as WebMCP tools while the hook is inside a mounted
|
|
* component. Tools are unregistered automatically on unmount or navigation.
|
|
*
|
|
* @param actions actions to make available as tools.
|
|
*/
|
|
export default function useWebMCPActions(
|
|
actions: ActionVariant[],
|
|
additionalDeps: React.DependencyList = []
|
|
) {
|
|
const pathname = useLocationSelector((location) => location.pathname);
|
|
const { auth } = useStores();
|
|
const context = useActionContext({ isMCP: true });
|
|
|
|
const enabled =
|
|
isModelContextSupported() && !!auth.team?.getPreference(TeamPreference.MCP);
|
|
|
|
const performable = enabled ? flattenActions(actions, context) : [];
|
|
const toolNames = performable.map((action) =>
|
|
snakeCase(action.analyticsName ?? "")
|
|
);
|
|
|
|
// Effect events keep `execute` callbacks working against the latest context
|
|
// and action list without re-registering tools on every render.
|
|
const getContext = React.useEffectEvent(() => context);
|
|
const getPerformable = React.useEffectEvent(() => performable);
|
|
|
|
React.useEffect(() => {
|
|
const performableActions = getPerformable();
|
|
if (!enabled || performableActions.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
|
|
for (const action of performableActions) {
|
|
const ctx = getContext();
|
|
const name = snakeCase(action.analyticsName ?? "");
|
|
const title = resolve<React.ReactNode>(action.name, ctx);
|
|
const description =
|
|
resolve<string | undefined>(action.description, ctx) ??
|
|
(typeof title === "string" ? title : action.analyticsName);
|
|
|
|
if (!name || !description) {
|
|
continue;
|
|
}
|
|
|
|
registerModelContextTool(
|
|
{
|
|
name,
|
|
description,
|
|
inputSchema: action.mcp?.inputSchema,
|
|
execute: async (args) => {
|
|
try {
|
|
const result = await performAction(action, {
|
|
...getContext(),
|
|
mcpArgs: args,
|
|
});
|
|
return {
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text:
|
|
typeof result === "string"
|
|
? result
|
|
: result === undefined
|
|
? "Done"
|
|
: JSON.stringify(result),
|
|
},
|
|
],
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
isError: true,
|
|
content: [{ type: "text", text: toError(err).message }],
|
|
};
|
|
}
|
|
},
|
|
},
|
|
controller.signal
|
|
);
|
|
}
|
|
|
|
Analytics.track("webmcp", "register");
|
|
|
|
return () => controller.abort();
|
|
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [enabled, toolNames.join(","), pathname, ...additionalDeps]);
|
|
}
|
|
|
|
function flattenActions(
|
|
actions: (ActionVariant | ActionGroup | ActionSeparator)[],
|
|
context: ActionContext
|
|
): PerformableAction[] {
|
|
const result: PerformableAction[] = [];
|
|
|
|
for (const action of actions) {
|
|
if (action.type === "action_separator") {
|
|
continue;
|
|
}
|
|
if (action.type === "action_group") {
|
|
result.push(...flattenActions(action.actions, context));
|
|
continue;
|
|
}
|
|
if (resolve<boolean>(action.visible, context) === false) {
|
|
continue;
|
|
}
|
|
if (action.variant === "action_with_children") {
|
|
const children = resolve<
|
|
(ActionVariant | ActionGroup | ActionSeparator)[]
|
|
>(action.children, context);
|
|
result.push(...flattenActions(children, context));
|
|
continue;
|
|
}
|
|
if (action.variant === "action" && action.dangerous) {
|
|
continue;
|
|
}
|
|
if (action.analyticsName) {
|
|
result.push(action);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|