refactor: dedupe channel helper readers

This commit is contained in:
Peter Steinberger
2026-04-07 10:00:27 +01:00
parent 255abc57b9
commit cb29ecc100
6 changed files with 25 additions and 50 deletions

View File

@@ -25,7 +25,7 @@ import { logVerbose, shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { normalizeAllowFrom } from "./bot-access.js";
import { resolveLineGroupConfigEntry } from "./group-keys.js";
import type { LineGroupConfig, ResolvedLineAccount } from "./types.js";
import type { ResolvedLineAccount } from "./types.js";
type EventSource = webhook.Source | undefined;
type MessageEvent = webhook.MessageEvent;
@@ -283,17 +283,6 @@ function resolveLineAddresses(params: {
return { fromAddress, toAddress, originatingTo };
}
function resolveLineGroupSystemPrompt(
groups: Record<string, LineGroupConfig | undefined> | undefined,
source: LineSourceInfoWithPeerId,
): string | undefined {
const entry = resolveLineGroupConfigEntry(groups, {
groupId: source.groupId,
roomId: source.roomId,
});
return normalizeOptionalString(entry?.systemPrompt);
}
async function finalizeLineInboundContext(params: {
cfg: OpenClawConfig;
account: ResolvedLineAccount;
@@ -380,7 +369,12 @@ async function finalizeLineInboundContext(params: {
OriginatingChannel: "line" as const,
OriginatingTo: originatingTo,
GroupSystemPrompt: params.source.isGroup
? resolveLineGroupSystemPrompt(params.account.config.groups, params.source)
? normalizeOptionalString(
resolveLineGroupConfigEntry(params.account.config.groups, {
groupId: params.source.groupId,
roomId: params.source.roomId,
})?.systemPrompt,
)
: undefined,
InboundHistory: params.inboundHistory,
});

View File

@@ -1,4 +1,3 @@
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { fetchWithSsrFGuard, type SsrFPolicy } from "../../runtime-api.js";
import { getMSTeamsRuntime } from "../runtime.js";
import { ensureUserAgentHeader } from "../user-agent.js";
@@ -8,7 +7,7 @@ import {
applyAuthorizationHeaderForUrl,
GRAPH_ROOT,
inferPlaceholder,
isRecord,
readNestedString,
isUrlAllowed,
type MSTeamsAttachmentFetchPolicy,
normalizeContentType,
@@ -39,17 +38,6 @@ type GraphAttachment = {
content?: unknown;
};
function readNestedString(value: unknown, keys: Array<string | number>): string | undefined {
let current: unknown = value;
for (const key of keys) {
if (!isRecord(current)) {
return undefined;
}
current = current[key as keyof typeof current];
}
return normalizeOptionalString(current);
}
export function buildMSTeamsGraphMessageUrls(params: {
conversationType?: string | null;
conversationId?: string | null;

View File

@@ -7,7 +7,7 @@ import {
normalizeHostnameSuffixAllowlist,
type SsrFPolicy,
} from "openclaw/plugin-sdk/ssrf-policy";
import { isRecord } from "openclaw/plugin-sdk/text-runtime";
import { isRecord, normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import type { MSTeamsAttachmentLike } from "./types.js";
type InlineImageCandidate =
@@ -78,6 +78,17 @@ export const DEFAULT_MEDIA_AUTH_HOST_ALLOWLIST = [
export const GRAPH_ROOT = "https://graph.microsoft.com/v1.0";
export { isRecord };
export function readNestedString(value: unknown, keys: Array<string | number>): string | undefined {
let current: unknown = value;
for (const key of keys) {
if (!isRecord(current)) {
return undefined;
}
current = current[key as keyof typeof current];
}
return normalizeOptionalString(current);
}
export function resolveRequestUrl(input: RequestInfo | URL): string {
if (typeof input === "string") {
return input;

View File

@@ -1,6 +1,5 @@
import crypto from "node:crypto";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { isRecord } from "./attachments/shared.js";
import { isRecord, readNestedString } from "./attachments/shared.js";
import { resolveMSTeamsStorePath } from "./storage.js";
import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
@@ -88,11 +87,6 @@ function readNestedValue(value: unknown, keys: Array<string | number>): unknown
return current;
}
function readNestedString(value: unknown, keys: Array<string | number>): string | undefined {
const found = readNestedValue(value, keys);
return normalizeOptionalString(found);
}
export function extractMSTeamsPollVote(
activity: { value?: unknown } | undefined,
): MSTeamsPollVote | null {

View File

@@ -67,13 +67,6 @@ function extractScopes(payload: unknown): string[] {
return normalizeScopes(scopes);
}
function readError(payload: unknown): string | undefined {
if (!isRecord(payload)) {
return undefined;
}
return normalizeOptionalString(payload.error);
}
async function callSlack(
client: WebClient,
method: SlackScopesSource,
@@ -103,7 +96,7 @@ export async function fetchSlackScopes(
if (scopes.length > 0) {
return { ok: true, scopes, source: method };
}
const error = readError(result);
const error = isRecord(result) ? normalizeOptionalString(result.error) : undefined;
if (error) {
errors.push(`${method}: ${error}`);
}

View File

@@ -41,13 +41,6 @@ export function findAgentEntryIndex(list: AgentEntry[], agentId: string): number
return list.findIndex((entry) => normalizeAgentId(entry.id) === id);
}
function resolveAgentName(cfg: OpenClawConfig, agentId: string) {
const entry = listAgentEntries(cfg).find(
(agent) => normalizeAgentId(agent.id) === normalizeAgentId(agentId),
);
return normalizeOptionalString(entry?.name);
}
function resolveAgentModel(cfg: OpenClawConfig, agentId: string) {
const entry = listAgentEntries(cfg).find(
(agent) => normalizeAgentId(agent.id) === normalizeAgentId(agentId),
@@ -101,7 +94,9 @@ export function buildAgentSummaries(cfg: OpenClawConfig): AgentSummary[] {
: undefined;
return {
id,
name: resolveAgentName(cfg, id),
name: normalizeOptionalString(
configuredAgents.find((agent) => normalizeAgentId(agent.id) === id)?.name,
),
identityName,
identityEmoji,
identitySource,