Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

16.5 KB
Raw Blame
/**
 * feishu/streamingText.ts
 * ---------------------------------------------------------------------------
 * Throttled streaming-text card with `xdt-image://` / `xdt-file://` rewrite.
 *
 * Lifecycle:
 *   start(openId, initial)        → mints v2 markdown card, returns handle
 *   handle.append(delta)          → throttled v2 markdown patch (every 1.5s).
 *                                   xdt-image/file references are stripped to
 *                                   placeholder text in intermediate frames
 *                                   (feishu rejects raw xdt-* URLs in cards).
 *   handle.finalize(finalText)    → 1) parse xdt-image URLs, parallel-upload
 *                                      to feishu → image_keys
 *                                   2) parse xdt-file URLs (dedup), send each
 *                                      as a separate file message
 *                                   3) strip xdt-file from card text
 *                                   4) update the progress card with final
 *                                      text/images
 *   handle.close()                → cancel pending throttle, no further patches
 *
 * Design parity: matches the legacy feishuBot/replyClient.ts behaviour
 * (xdt-image: `![alt](xdt-image://...)`, xdt-file:
 * `[name](xdt-file:///abs/path)`).
 */

import {
  sendCardRaw,
  patchCardRaw,
  uploadImage,
  sendFile,
  sendText,
  claimPatchableOpener,
  recallMessage,
  getAccountEpoch,
  rearmAnchorToTrigger,
} from './outbound.js';
import { buildMarkdownCardV2, buildMixedMarkdownCardV2 } from './cards.js';
import { resolveFeishuMediaUrl } from './mediaCache.js';
import { bindScope, getHost, getLog, getScope } from './moduleScope.js';
import { messages as transportMessages } from './messages.js';
import type { StreamingTextHandle } from '../types.js';
// xdt-* 引用解析抽到渠道无关模块(slack streamingText 共用同一套语义)
import {
  stripXdtForStreaming,
  classifyXdtOnly,
  stripXdtFileLinks,
  collectXdtFileLinks,
  collectXdtImageUrls,
} from '../xdtRefs.js';

const PATCH_THROTTLE_MS = 1500;
/** Feishu caps serialized card request bodies at 30 KB. */
export const FEISHU_CARD_REQUEST_MAX_BYTES = 30 * 1024;

function cardRequestBytes(card: unknown): number {
  return Buffer.byteLength(JSON.stringify({ content: JSON.stringify(card) }), 'utf8');
}

function fitCardToLimit(
  text: string,
  fullCard: unknown,
  buildCard: (visibleText: string) => unknown,
): unknown {
  if (cardRequestBytes(fullCard) <= FEISHU_CARD_REQUEST_MAX_BYTES) return fullCard;

  const chars = Array.from(text);
  const suffix = transportMessages.streaming.replyTruncated;
  let low = 0;
  let high = chars.length;
  let fitted = buildCard(suffix);
  if (cardRequestBytes(fitted) > FEISHU_CARD_REQUEST_MAX_BYTES) {
    return buildMarkdownCardV2(transportMessages.streaming.deliveryFailed);
  }
  while (low <= high) {
    const middle = Math.floor((low + high) / 2);
    const candidate = buildCard(`${chars.slice(0, middle).join('')}${suffix}`);
    if (cardRequestBytes(candidate) <= FEISHU_CARD_REQUEST_MAX_BYTES) {
      fitted = candidate;
      low = middle + 1;
    } else {
      high = middle - 1;
    }
  }
  return fitted;
}

async function uploadExtraImageKeys(absPaths: readonly string[]): Promise<string[]> {
  const results = await Promise.all(
    absPaths.map(async (absPath) => {
      try {
        return await uploadImage(absPath);
      } catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        getLog().warn(`[feishu/streamingText] uploadImage extra ${absPath} failed: ${msg}`);
        return null;
      }
    }),
  );
  const keys: string[] = [];
  for (const key of results) {
    if (key) keys.push(key);
  }
  return keys;
}

class FeishuStreamingTextHandle implements StreamingTextHandle {
  private messageIdValue: string;
  get messageId(): string {
    return this.messageIdValue;
  }
  private readonly openId: string;
  private buffer: string;
  private flushed: string;
  private pending: NodeJS.Timeout | null = null;
  private inFlight: Promise<void> | null = null;
  private finalized = false;
  /**
   * 工具结果(tool_result_full event)带过来的图片 absPath, finalize 时跟文本里
   * xdt-image markdown 链接一起 upload + 拼到 card 末尾。host 主进程负责把
   * xdt-image:// URL 用 imageCacheStore.resolveSafe 解成 absPath 再投递, 这里
   * 不参与 namespace 路由(@cindy/im 包对 lizi-art / 其它 host 命名空间不感知)。
   */
  private extraImageAbsPaths: string[] = [];

  constructor(messageId: string, openId: string, initial: string, private readonly opts?: { threadTs?: string }, private readonly accountEpoch = getAccountEpoch(), private readonly claimedOpener = false) {
    this.messageIdValue = messageId;
    this.openId = openId;
    // `initial` is what's currently DISPLAYED in feishu (e.g. "🧠 思考中...").
    // `buffer` is what we've ACCUMULATED to display — starts empty so the
    // first real append() *replaces* the placeholder rather than appending
    // to it (otherwise the placeholder permanently prefixes the message).
    this.buffer = '';
    this.flushed = initial;
    // The handle outlives FeishuIM.startStreamingText's ALS call. Agent events
    // may arrive from outside that scope (or another bot's), so bind both I/O
    // paths to the connection that created the card, including timer callbacks,
    // media uploads, empty-reply recalls and delivery-failure fallbacks.
    const scope = getScope();
    this.flushIntermediate = bindScope(scope, this.flushIntermediate.bind(this));
    this.doFinalize = bindScope(scope, this.doFinalize.bind(this));
  }

  append(delta: string): void {
    if (this.finalized) return;
    this.buffer += delta;
    this.scheduleFlush();
  }

  replace(fullText: string): void {
    if (this.finalized) return;
    this.buffer = fullText;
    this.scheduleFlush();
  }

  private scheduleFlush(): void {
    if (this.pending) return;
    this.pending = setTimeout(() => {
      this.pending = null;
      void this.flushIntermediate();
    }, PATCH_THROTTLE_MS);
  }

  async finalize(finalText: string): Promise<void> {
    if (this.finalized) return;
    this.finalized = true;
    if (this.pending) {
      clearTimeout(this.pending);
      this.pending = null;
    }
    if (this.inFlight) {
      try {
        await this.inFlight;
      } catch {
        /* swallow */
      }
    }
    this.buffer = finalText;
    await this.doFinalize();
  }

  close(): void {
    this.finalized = true;
    if (this.pending) {
      clearTimeout(this.pending);
      this.pending = null;
    }
  }

  addExtraImageAbsPath(absPath: string): void {
    if (this.finalized) return;
    if (!absPath) return;
    // dedupe by absPath — model 偶尔在同一 turn 多条 tool_result 重复同一张图
    if (this.extraImageAbsPaths.includes(absPath)) return;
    this.extraImageAbsPaths.push(absPath);
  }

  // ── intermediate (throttled) patch ────────────────────────────────────────

  private async flushIntermediate(): Promise<void> {
    if (this.inFlight) {
      try {
        await this.inFlight;
      } catch {
        /* swallow */
      }
    }
    if (this.finalized || getAccountEpoch() !== this.accountEpoch || this.buffer === this.flushed) return;
    // Friendlier placeholder when buffer is xdt-only (model sent images/files
    // but no narration yet) — `classifyXdtOnly` returns the kind so we can
    // show a hint instead of stripped placeholders.
    const klass = classifyXdtOnly(this.buffer);
    let text: string;
    if (klass === 'image-only') text = transportMessages.streaming.preparingImage;
    else if (klass === 'file-only') text = transportMessages.streaming.preparingFile;
    else text = stripXdtForStreaming(this.buffer);
    const log = getLog();
    this.inFlight = (async () => {
      try {
        await patchCardRaw(
          this.messageId,
          fitCardToLimit(text, buildMarkdownCardV2(text), buildMarkdownCardV2),
        );
        this.flushed = this.buffer;
      } catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        log.warn(
          `[feishu/streamingText] intermediate patch failed (will retry): ${msg}`,
        );
      } finally {
        this.inFlight = null;
      }
    })();
    await this.inFlight;
  }

  // ── finalize: upload media, replace progress with the final ───────────────

  private async doFinalize(): Promise<void> {
    const log = getLog();
    const text = this.buffer;
    if (getAccountEpoch() !== this.accountEpoch) return;
    if (!text.trim() && this.extraImageAbsPaths.length === 0) {
      // A claimed opener is also the topic's reply anchor. Only its owning
      // account/round may restore that anchor after a successful recall.
      await recallMessage(this.messageId);
      if (this.claimedOpener && getAccountEpoch() === this.accountEpoch) {
        rearmAnchorToTrigger(this.openId, this.messageId);
      }
      return;
    }

    // 1. Upload all xdt-image URLs in parallel; missing entries get a "图片
    //    加载失败" placeholder in the final card.
    const imageUrls = collectXdtImageUrls(text);
    const imageMap = new Map<string, string>();
    // 正文图 absPath 集合:1b 用它对 extras 求差——同一张图既被模型 markdown
    // 内联进正文、又经 tool_result 账本 sidechannel 送来时(ghost 读文档
    // xdt_media_inline 内联场景),只保留正文内联位,不在卡片尾部再挂一份。
    const bodyImageAbsPaths = new Set<string>();
    if (imageUrls.length > 0) {
      log.debug(`[feishu/streamingText] uploading ${imageUrls.length} xdt-image(s)`);
      const results = await Promise.all(
        imageUrls.map(async (url) => {
          // mediaCache.resolveFeishuMediaUrl decoupled to outbound layer:
          // uploadImage takes an absPath, so we resolve here.
          // cindy-media(媒体总仓新地址)优先走 host 注入的解析回调;老
          // xdt-image 仍走 feishu 专属目录解析。
          let absPath: string;
          try {
            const hostResolved = getHost().media?.resolveMediaUrl(url) ?? null;
            absPath =
              hostResolved ??
              resolveFeishuMediaUrl(url, getHost().paths.feishuMediaDir).absPath;
          } catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            log.warn(
              `[feishu/streamingText] resolve managed image failed for ${url}: ${msg}`,
            );
            return null;
          }
          bodyImageAbsPaths.add(absPath);
          try {
            const key = await uploadImage(absPath);
            return key ? ([url, key] as const) : null;
          } catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            log.warn(`[feishu/streamingText] uploadImage ${absPath} failed: ${msg}`);
            return null;
          }
        }),
      );
      for (const r of results) {
        if (r) imageMap.set(r[0], r[1]);
      }
    }

    // 1b. Upload extras (来自 tool_result event 的图片). 跟文本里 xdt-image 走
    //     同一条 uploadImage 通道, 但 path 由 host 主进程已经解好直接传 absPath,
    //     这里不再过 resolveFeishuMediaUrl (@cindy/im 包对其它 host namespace 不感知)。
    //     正文里已内联的同图(按 absPath)跳过,防"正文一张 + 尾部一张"双份。
    const extrasToUpload = this.extraImageAbsPaths.filter((p) => !bodyImageAbsPaths.has(p));
    if (extrasToUpload.length > 0) {
      log.debug(
        `[feishu/streamingText] uploading ${extrasToUpload.length} extra image(s) from tool_result`,
      );
    }
    if (getAccountEpoch() !== this.accountEpoch) return;
    const extraImageKeys = await uploadExtraImageKeys(extrasToUpload);
    if (getAccountEpoch() !== this.accountEpoch) return;

    // 2. Send xdt-file links as separate file messages.
    const fileLinks = collectXdtFileLinks(text);
    if (fileLinks.length > 0) {
      log.debug(`[feishu/streamingText] sending ${fileLinks.length} xdt-file(s)`);
      await Promise.all(
        fileLinks.map(async (link) => {
          try {
            await sendFile(this.openId, link.absPath, link.alt || undefined, this.opts);
          } catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            log.warn(
              `[feishu/streamingText] sendFile ${link.absPath} failed: ${msg}`,
            );
          }
        }),
      );
    }

    if (getAccountEpoch() !== this.accountEpoch) return;

    // 3. Strip xdt-file from card text (delivered separately, no placeholder).
    const cardText = stripXdtFileLinks(text);
    const cardTextTrimmed = cardText.trim();

    // 4. Update the original card without a recall notice. Terminal shapes:
    //    a) text only → plain markdown card v2
    //    b) text + images (markdown 内联 OR tool_result 追加) → mixed elements card
    //    c) empty text + only files → "🎉 N 个文件已送达" placeholder
    //    d) extra images all failed → delivery failure (truly empty finals returned above)
    const hasAnyImage = imageUrls.length > 0 || extraImageKeys.length > 0;
    const fileOnly = cardTextTrimmed.length === 0 && !hasAnyImage && fileLinks.length > 0;
    let card: unknown;
    try {
      if (fileOnly) {
        card = buildMarkdownCardV2(transportMessages.streaming.fileSentDone(fileLinks.length));
      } else if (hasAnyImage) {
        // 文本空但有图(画完图没说话) — 不要在图上面塞 "(空回复)" 占位, 让图自己说话。
        // 反过来如果文本也空, buildMixedMarkdownCardV2 内部 elements.length===0
        // 兜底分支会塞 "(空回复)" — 但 hasAnyImage 时一定不为空, 所以不会触发。
        card = buildMixedMarkdownCardV2(cardText, imageMap, extraImageKeys);
      } else {
        card = buildMarkdownCardV2(cardTextTrimmed.length > 0 ? cardText : transportMessages.streaming.deliveryFailed);
      }
      card = fitCardToLimit(cardText, card, (visibleText) =>
        hasAnyImage
          ? buildMixedMarkdownCardV2(visibleText, imageMap, extraImageKeys)
          : buildMarkdownCardV2(visibleText),
      );
      await patchCardRaw(this.messageId, card);
      this.flushed = this.buffer;
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      log.warn(`[feishu/streamingText] finalize patch failed: ${msg}`);
      if (getAccountEpoch() !== this.accountEpoch) return;
      const notice = transportMessages.streaming.deliveryFailed;
      try {
        // Preserve the full answer if the original card can no longer be edited.
        // Stay on this round's target and never recall the user's progress card.
        const result = await sendCardRaw(this.openId, card ?? buildMarkdownCardV2(notice), {
          ...this.opts, advanceRound: false,
        });
        this.messageIdValue = result.messageId;
        this.flushed = this.buffer;
      } catch (fallbackErr) {
        const fallbackMsg =
          fallbackErr instanceof Error ? fallbackErr.message : String(fallbackErr);
        log.warn(`[feishu/streamingText] fallback send failed: ${fallbackMsg}`);
        if (getAccountEpoch() !== this.accountEpoch) return;
        try {
          await sendText(this.openId, notice, this.opts);
        } catch (textErr) {
          const textMsg = textErr instanceof Error ? textErr.message : String(textErr);
          log.error(`[feishu/streamingText] fallback text failed: ${textMsg}`);
        }
      }
    }
  }
}

export async function start(
  openId: string,
  initial: string = transportMessages.streaming.randomThinking(),
  opts?: { threadTs?: string },
): Promise<StreamingTextHandle> {
  // Preserve explicit opener callers; ordinary inbound messages never create topics.
  const epoch = getAccountEpoch();
  const claimed = opts?.threadTs ? null : claimPatchableOpener(openId);
  if (claimed) {
    return new FeishuStreamingTextHandle(claimed, openId, initial, opts, epoch, true);
  }
  const { messageId } = await sendCardRaw(openId, buildMarkdownCardV2(initial), opts);
  return new FeishuStreamingTextHandle(messageId, openId, initial, opts, epoch);
}

/**
 * 一次性把已有 card patch 成 v2 markdown 内容 (不流式)。/ctr 接管路径用这个
 * 把 picker card 直接转成"已接管 + 总结"视图, 替代发两张独立消息。
 */
export async function patchMarkdown(messageId: string, markdown: string): Promise<void> {
  if (!markdown.trim()) {
    await recallMessage(messageId);
    return;
  }
  await patchCardRaw(messageId, buildMarkdownCardV2(markdown));
}