mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 16:06:16 +00:00
refactor: dedupe core account record helpers
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { stripUrlUserInfo } from "../shared/net/url-userinfo.js";
|
||||
import { isRecord } from "../utils.js";
|
||||
import type { ChannelAccountSnapshot } from "./plugins/types.core.js";
|
||||
|
||||
// Read-only status commands project a safe subset of account fields into snapshots
|
||||
@@ -15,13 +16,6 @@ const CREDENTIAL_STATUS_KEYS = [
|
||||
|
||||
type CredentialStatusKey = (typeof CREDENTIAL_STATUS_KEYS)[number];
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function readTrimmedString(record: Record<string, unknown>, key: string): string | undefined {
|
||||
const value = record[key];
|
||||
if (typeof value !== "string") {
|
||||
@@ -60,7 +54,7 @@ function readCredentialStatus(record: Record<string, unknown>, key: CredentialSt
|
||||
}
|
||||
|
||||
export function resolveConfiguredFromCredentialStatuses(account: unknown): boolean | undefined {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -82,7 +76,7 @@ export function resolveConfiguredFromRequiredCredentialStatuses(
|
||||
account: unknown,
|
||||
requiredKeys: CredentialStatusKey[],
|
||||
): boolean | undefined {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -101,7 +95,7 @@ export function resolveConfiguredFromRequiredCredentialStatuses(
|
||||
}
|
||||
|
||||
export function hasConfiguredUnavailableCredentialStatus(account: unknown): boolean {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return false;
|
||||
}
|
||||
@@ -111,7 +105,7 @@ export function hasConfiguredUnavailableCredentialStatus(account: unknown): bool
|
||||
}
|
||||
|
||||
export function hasResolvedCredentialValue(account: unknown): boolean {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return false;
|
||||
}
|
||||
@@ -137,7 +131,7 @@ export function projectCredentialSnapshotFields(
|
||||
| "signingSecretStatus"
|
||||
| "userTokenStatus"
|
||||
> {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return {};
|
||||
}
|
||||
@@ -176,7 +170,7 @@ export function projectCredentialSnapshotFields(
|
||||
export function projectSafeChannelAccountSnapshotFields(
|
||||
account: unknown,
|
||||
): Partial<ChannelAccountSnapshot> {
|
||||
const record = asRecord(account);
|
||||
const record = isRecord(account) ? account : null;
|
||||
if (!record) {
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ChannelPlugin } from "../channels/plugins/types.js";
|
||||
import { inspectReadOnlyChannelAccount } from "../channels/read-only-account-inspect.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { formatErrorMessage } from "../infra/errors.js";
|
||||
import { isRecord } from "../utils.js";
|
||||
|
||||
export type ChannelDefaultAccountContext = {
|
||||
accountIds: string[];
|
||||
@@ -20,15 +21,8 @@ export type ChannelDefaultAccountContext = {
|
||||
|
||||
export type ChannelAccountContextMode = "strict" | "read_only";
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function getBooleanField(value: unknown, key: string): boolean | undefined {
|
||||
const record = asRecord(value);
|
||||
const record = isRecord(value) ? value : null;
|
||||
if (!record) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user