Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Quelle  monitor.bot-menu-handler.ts

  Sprache: JAVA
 

Spracherkennung für: .ts vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]

import type { ClawdbotConfig, HistoryEntry, RuntimeEnv } from "../runtime-api.js";
import { handleFeishuMessage, type FeishuMessageEvent } from "./bot.js";
import { maybeHandleFeishuQuickActionMenu } from "./card-ux-launcher.js";
import {
  claimUnprocessedFeishuMessage,
  recordProcessedFeishuMessage,
  releaseFeishuMessageProcessing,
} from "./dedup.js";
import { botNames, botOpenIds } from "./monitor.state.js";
import { isFeishuRetryableSyntheticEventError } from "./monitor.synthetic-error.js";

type FeishuBotMenuEvent = {
  event_key?: string;
  timestamp?: string | number;
  operator?: {
    operator_name?: string;
    operator_id?: { open_id?: string; user_id?: string; union_id?: string };
  };
};

function readString(value: unknown): string | undefined {
  return typeof value === "string" ? value : undefined;
}

function readStringOrNumber(value: unknown): string | number | undefined {
  return typeof value === "string" || typeof value === "number" ? value : undefined;
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function parseFeishuBotMenuEvent(value: unknown): FeishuBotMenuEvent | null {
  if (!isRecord(value)) {
    return null;
  }
  const operator = value.operator;
  if (operator !== undefined && !isRecord(operator)) {
    return null;
  }
  return {
    event_key: readString(value.event_key),
    timestamp: readStringOrNumber(value.timestamp),
    operator: operator
      ? {
          operator_name: readString(operator.operator_name),
          operator_id: isRecord(operator.operator_id)
            ? {
                open_id: readString(operator.operator_id.open_id),
                user_id: readString(operator.operator_id.user_id),
                union_id: readString(operator.operator_id.union_id),
              }
            : undefined,
        }
      : undefined,
  };
}

export function createFeishuBotMenuHandler(params: {
  cfg: ClawdbotConfig;
  accountId: string;
  runtime?: RuntimeEnv;
  chatHistories: Map<string, HistoryEntry[]>;
  fireAndForget?: boolean;
  getBotOpenId?: (accountId: string) => string | undefined;
  getBotName?: (accountId: string) => string | undefined;
}): (data: unknown) => Promise<void> {
  const { cfg, accountId, runtime, chatHistories, fireAndForget } = params;
  const log = runtime?.log ?? console.log;
  const error = runtime?.error ?? console.error;
  const getBotOpenId = params.getBotOpenId ?? ((id) => botOpenIds.get(id));
  const getBotName = params.getBotName ?? ((id) => botNames.get(id));

  return async (data) => {
    try {
      const event = parseFeishuBotMenuEvent(data);
      if (!event) {
        return;
      }
      const operatorOpenId = event.operator?.operator_id?.open_id?.trim();
      const eventKey = event.event_key?.trim();
      if (!operatorOpenId || !eventKey) {
        return;
      }
      const syntheticEvent: FeishuMessageEvent = {
        sender: {
          sender_id: {
            open_id: operatorOpenId,
            user_id: event.operator?.operator_id?.user_id,
            union_id: event.operator?.operator_id?.union_id,
          },
          sender_type: "user",
        },
        message: {
          message_id: `bot-menu:${eventKey}:${event.timestamp ?? Date.now()}`,
          chat_id: `p2p:${operatorOpenId}`,
          chat_type: "p2p",
          message_type: "text",
          content: JSON.stringify({
            text: `/menu ${eventKey}`,
          }),
        },
      };
      const syntheticMessageId = syntheticEvent.message.message_id;
      const claim = await claimUnprocessedFeishuMessage({
        messageId: syntheticMessageId,
        namespace: accountId,
        log,
      });
      if (claim === "duplicate") {
        log(`feishu[${accountId}]: dropping duplicate bot-menu event for ${syntheticMessageId}`);
        return;
      }
      if (claim === "inflight") {
        log(`feishu[${accountId}]: dropping in-flight bot-menu event for ${syntheticMessageId}`);
        return;
      }
      const handleLegacyMenu = () =>
        handleFeishuMessage({
          cfg,
          event: syntheticEvent,
          botOpenId: getBotOpenId(accountId),
          botName: getBotName(accountId),
          runtime,
          chatHistories,
          accountId,
          processingClaimHeld: true,
        });

      const promise = maybeHandleFeishuQuickActionMenu({
        cfg,
        eventKey,
        operatorOpenId,
        runtime,
        accountId,
      })
        .then(async (handledMenu) => {
          if (handledMenu) {
            await recordProcessedFeishuMessage(syntheticMessageId, accountId, log);
            releaseFeishuMessageProcessing(syntheticMessageId, accountId);
            return;
          }
          return await handleLegacyMenu();
        })
        .catch(async (err) => {
          if (isFeishuRetryableSyntheticEventError(err)) {
            releaseFeishuMessageProcessing(syntheticMessageId, accountId);
          } else {
            await recordProcessedFeishuMessage(syntheticMessageId, accountId, log);
          }
          throw err;
        });
      if (fireAndForget) {
        promise.catch((err) => {
          error(`feishu[${accountId}]: error handling bot menu event: ${String(err)}`);
        });
        return;
      }
      await promise;
    } catch (err) {
      error(`feishu[${accountId}]: error handling bot menu event: ${String(err)}`);
    }
  };
}

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet am  2026-04-27) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge