Files
openclaw/extensions/mattermost/src/setup-surface.ts
2026-03-15 17:06:42 -07:00

194 lines
6.5 KiB
TypeScript

import {
applyAccountNameToChannelSection,
applySetupAccountConfigPatch,
DEFAULT_ACCOUNT_ID,
hasConfiguredSecretInput,
migrateBaseNameToDefaultAccount,
normalizeAccountId,
type OpenClawConfig,
} from "openclaw/plugin-sdk/mattermost";
import { type ChannelSetupWizard } from "../../../src/channels/plugins/setup-wizard.js";
import type { ChannelSetupAdapter } from "../../../src/channels/plugins/types.adapters.js";
import { formatDocsLink } from "../../../src/terminal/links.js";
import {
listMattermostAccountIds,
resolveMattermostAccount,
type ResolvedMattermostAccount,
} from "./mattermost/accounts.js";
import { normalizeMattermostBaseUrl } from "./mattermost/client.js";
const channel = "mattermost" as const;
function isMattermostConfigured(account: ResolvedMattermostAccount): boolean {
const tokenConfigured =
Boolean(account.botToken?.trim()) || hasConfiguredSecretInput(account.config.botToken);
return tokenConfigured && Boolean(account.baseUrl);
}
function resolveMattermostAccountWithSecrets(cfg: OpenClawConfig, accountId: string) {
return resolveMattermostAccount({
cfg,
accountId,
allowUnresolvedSecretRef: true,
});
}
export const mattermostSetupAdapter: ChannelSetupAdapter = {
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
applyAccountName: ({ cfg, accountId, name }) =>
applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name,
}),
validateInput: ({ accountId, input }) => {
const token = input.botToken ?? input.token;
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
return "Mattermost env vars can only be used for the default account.";
}
if (!input.useEnv && (!token || !baseUrl)) {
return "Mattermost requires --bot-token and --http-url (or --use-env).";
}
if (input.httpUrl && !baseUrl) {
return "Mattermost --http-url must include a valid base URL.";
}
return null;
},
applyAccountConfig: ({ cfg, accountId, input }) => {
const token = input.botToken ?? input.token;
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
const namedConfig = applyAccountNameToChannelSection({
cfg,
channelKey: channel,
accountId,
name: input.name,
});
const next =
accountId !== DEFAULT_ACCOUNT_ID
? migrateBaseNameToDefaultAccount({
cfg: namedConfig,
channelKey: channel,
})
: namedConfig;
return applySetupAccountConfigPatch({
cfg: next,
channelKey: channel,
accountId,
patch: input.useEnv
? {}
: {
...(token ? { botToken: token } : {}),
...(baseUrl ? { baseUrl } : {}),
},
});
},
};
export const mattermostSetupWizard: ChannelSetupWizard = {
channel,
status: {
configuredLabel: "configured",
unconfiguredLabel: "needs token + url",
configuredHint: "configured",
unconfiguredHint: "needs setup",
configuredScore: 2,
unconfiguredScore: 1,
resolveConfigured: ({ cfg }) =>
listMattermostAccountIds(cfg).some((accountId) =>
isMattermostConfigured(resolveMattermostAccountWithSecrets(cfg, accountId)),
),
},
introNote: {
title: "Mattermost bot token",
lines: [
"1) Mattermost System Console -> Integrations -> Bot Accounts",
"2) Create a bot + copy its token",
"3) Use your server base URL (e.g., https://chat.example.com)",
"Tip: the bot must be a member of any channel you want it to monitor.",
`Docs: ${formatDocsLink("/mattermost", "mattermost")}`,
],
shouldShow: ({ cfg, accountId }) =>
!isMattermostConfigured(resolveMattermostAccountWithSecrets(cfg, accountId)),
},
envShortcut: {
prompt: "MATTERMOST_BOT_TOKEN + MATTERMOST_URL detected. Use env vars?",
preferredEnvVar: "MATTERMOST_BOT_TOKEN",
isAvailable: ({ cfg, accountId }) => {
if (accountId !== DEFAULT_ACCOUNT_ID) {
return false;
}
const resolvedAccount = resolveMattermostAccountWithSecrets(cfg, accountId);
const hasConfigValues =
hasConfiguredSecretInput(resolvedAccount.config.botToken) ||
Boolean(resolvedAccount.config.baseUrl?.trim());
return Boolean(
process.env.MATTERMOST_BOT_TOKEN?.trim() &&
process.env.MATTERMOST_URL?.trim() &&
!hasConfigValues,
);
},
apply: ({ cfg, accountId }) =>
applySetupAccountConfigPatch({
cfg,
channelKey: channel,
accountId,
patch: {},
}),
},
credentials: [
{
inputKey: "botToken",
providerHint: channel,
credentialLabel: "bot token",
preferredEnvVar: "MATTERMOST_BOT_TOKEN",
envPrompt: "MATTERMOST_BOT_TOKEN + MATTERMOST_URL detected. Use env vars?",
keepPrompt: "Mattermost bot token already configured. Keep it?",
inputPrompt: "Enter Mattermost bot token",
inspect: ({ cfg, accountId }) => {
const resolvedAccount = resolveMattermostAccountWithSecrets(cfg, accountId);
return {
accountConfigured: isMattermostConfigured(resolvedAccount),
hasConfiguredValue: hasConfiguredSecretInput(resolvedAccount.config.botToken),
};
},
},
],
textInputs: [
{
inputKey: "httpUrl",
message: "Enter Mattermost base URL",
confirmCurrentValue: false,
currentValue: ({ cfg, accountId }) =>
resolveMattermostAccountWithSecrets(cfg, accountId).baseUrl ??
process.env.MATTERMOST_URL?.trim(),
initialValue: ({ cfg, accountId }) =>
resolveMattermostAccountWithSecrets(cfg, accountId).baseUrl ??
process.env.MATTERMOST_URL?.trim(),
shouldPrompt: ({ cfg, accountId, credentialValues, currentValue }) => {
const resolvedAccount = resolveMattermostAccountWithSecrets(cfg, accountId);
const tokenConfigured =
Boolean(resolvedAccount.botToken?.trim()) ||
hasConfiguredSecretInput(resolvedAccount.config.botToken);
return Boolean(credentialValues.botToken) || !tokenConfigured || !currentValue;
},
validate: ({ value }) =>
normalizeMattermostBaseUrl(value)
? undefined
: "Mattermost base URL must include a valid base URL.",
normalizeValue: ({ value }) => normalizeMattermostBaseUrl(value) ?? value.trim(),
},
],
disable: (cfg: OpenClawConfig) => ({
...cfg,
channels: {
...cfg.channels,
mattermost: {
...cfg.channels?.mattermost,
enabled: false,
},
},
}),
};