Repository: toeverything/AFFiNE
File: packages/frontend/core/src/blocksuite/presets/ai/chat-panel/components/doc-chip.ts (TypeScript)

Code before:
    }
  };

  private readonly processDocChip = async () => {
    try {
      const doc = this.docDisplayConfig.getDoc(this.chip.docId);
      if (!doc) {
        throw new Error('Document not found');
      }
      if (!doc.ready) {
        doc.load();
      }
      const value = await extractMarkdownFromDoc(doc, this.host.std.provider);
      const tokenCount = estimateTokenCount(value);
      if (this.checkTokenLimit(this.chip, tokenCount)) {
        const markdown = this.chip.markdown ?? new Signal<string>('');
        markdown.value = value;
        this.updateChip(this.chip, {
          state: 'success',
          markdown,
          tokenCount,
        });
      } else {
        this.updateChip(this.chip, {
          state: 'failed',
          tooltip: 'Content touched the token limit',
        });
      }
    } catch (e) {
      this.updateChip(this.chip, {
        state: 'failed',
        tooltip: e instanceof Error ? e.message : 'Failed to process document',
      });
    }
  };

  override render() {
    const { state, docId } = this.chip;
    const isLoading = state === 'processing';
    const getIcon = this.docDisplayConfig.getIcon(docId);
    const docIcon = typeof getIcon === 'function' ? getIcon() : getIcon;
    const icon = getChipIcon(state, docIcon);
    const tooltip = getChipTooltip(
      state,
      this.chipName.value,
      this.chip.tooltip
    );

    return html`<chat-panel-chip
      .state=${state}
      .name=${this.chipName.value}

Review comment (suggestion):
rephrase to `Doc content exceeds the maximum token limit` instead

Code after:
    }
  };

  private readonly processDocChip = async () => {
    try {
      const doc = this.docDisplayConfig.getDoc(this.chip.docId);
      if (!doc) {
        throw new Error('Document not found');
      }
      if (!doc.ready) {
        doc.load();
      }
      const value = await extractMarkdownFromDoc(doc, this.host.std.provider);
      const tokenCount = estimateTokenCount(value);
      if (this.checkTokenLimit(this.chip, tokenCount)) {
        const markdown = this.chip.markdown ?? new Signal<string>('');
        markdown.value = value;
        this.updateChip(this.chip, {
          state: 'success',
          markdown,
          tokenCount,
        });
      } else {
        this.updateChip(this.chip, {
          state: 'failed',
          tooltip: 'Content exceeds token limit',
        });
      }
    } catch (e) {
      this.updateChip(this.chip, {
        state: 'failed',
        tooltip: e instanceof Error ? e.message : 'Failed to process document',
      });
    }
  };

  override render() {
    const { state, docId } = this.chip;
    const isLoading = state === 'processing';
    const getIcon = this.docDisplayConfig.getIcon(docId);
    const docIcon = typeof getIcon === 'function' ? getIcon() : getIcon;
    const icon = getChipIcon(state, docIcon);
    const tooltip = getChipTooltip(
      state,
      this.chipName.value,
      this.chip.tooltip
    );

    return html`<chat-panel-chip
      .state=${state}
      .name=${this.chipName.value}