上下文

一个复合组件系统,用于显示 AI 模型上下文窗口使用情况、token 消耗和成本估算。

Context 组件通过复合组件系统提供 AI 模型使用情况的全面视图。它在交互式悬停卡片界面中显示上下文窗口利用率、token 消耗明细(输入、输出、推理、缓存)和成本估算。

Install using CLI

AI Elements Vue
shadcn-vue CLI
npx ai-elements-vue@latest add context

Install Manually

Copy and paste the following files into the same folder.

Context.vue
ContextIcon.vue
ContextTrigger.vue
ContextContent.vue
ContextContentHeader.vue
ContextContentBody.vue
ContextContentFooter.vue
ContextInputUsage.vue
ContextOutputUsage.vue
ContextReasoningUsage.vue
ContextCacheUsage.vue
TokensWithCost.vue
context.ts
index.ts
<script setup lang="ts">
import type { LanguageModelUsage } from 'ai'
import type { ModelId } from './context'
import { HoverCard } from '@repo/shadcn-vue/components/ui/hover-card'
import { computed, provide } from 'vue'
import { ContextKey } from './context'

interface Props {
  usedTokens: number
  maxTokens: number
  usage?: LanguageModelUsage
  modelId?: ModelId
}

const props = defineProps<Props>()

provide(ContextKey, {
  usedTokens: computed(() => props.usedTokens),
  maxTokens: computed(() => props.maxTokens),
  usage: computed(() => props.usage),
  modelId: computed(() => props.modelId),
})
</script>

<template>
  <HoverCard :close-delay="0" :open-delay="0" v-bind="{ ...$attrs, ...props }">
    <slot />
  </HoverCard>
</template>

组件架构

Context 组件使用复合组件模式,通过 Vue 的 provide/inject API 进行数据共享:

  1. <Context> - 根提供者组件,保存所有上下文数据
  2. <ContextTrigger> - 交互式触发器元素(默认:带百分比的按钮)
  3. <ContextContent> - 悬停卡片内容容器
  4. <ContextContentHeader> - 带有进度可视化的标题部分
  5. <ContextContentBody> - 用于使用明细的主体部分
  6. <ContextContentFooter> - 用于总成本的页脚部分
  7. 使用组件 - 单独的 token 使用显示(输入、输出、推理、缓存)

Token 格式化

组件使用 Intl.NumberFormat 和紧凑表示法进行自动格式化:

  • 1,000 以下:显示确切计数(例如,"842")
  • 1,000+:显示带 K 后缀(例如,"32K")
  • 1,000,000+:显示带 M 后缀(例如,"1.5M")
  • 1,000,000,000+:显示带 B 后缀(例如,"2.1B")

成本计算

当提供 modelId 时,组件使用 tokenlens 库自动计算成本:

  • 输入 tokens:基于模型的输入定价
  • 输出 tokens:基于模型的输出定价
  • 推理 tokens:支持推理模型的特殊定价
  • 缓存 tokens:缓存输入 tokens 的降低定价
  • 总成本:所有 token 类型成本的总和

成本使用 Intl.NumberFormat 和 USD 货币进行格式化。

样式

组件使用 Tailwind CSS 类并遵循你的设计系统:

  • 进度指示器使用 currentColor 进行主题适配
  • 悬停卡片具有可自定义的宽度和内边距
  • 页脚具有次要背景以进行视觉分离
  • 所有文本大小使用 text-xs 类以保持一致性
  • 次要信息使用静音前景色

特性

  • 复合组件架构:灵活的上下文显示元素组合
  • 可视化进度指示器:显示上下文使用百分比的圆形 SVG 进度环
  • Token 明细:输入、输出、推理和缓存 tokens 的详细视图
  • 成本估算:使用 tokenlens 库进行实时成本计算
  • 智能格式化:自动 token 计数格式化(K、M、B 后缀)
  • 交互式悬停卡片:悬停时显示详细信息
  • 上下文提供者模式:通过 Vue 的 provide/inject API 进行清晰的数据流
  • TypeScript 支持:所有组件的完整类型定义
  • 可访问设计:适当的 ARIA 标签和语义 HTML
  • 主题集成:使用 currentColor 进行自动主题适配

Props

<Context />

maxTokensnumber
上下文窗口的总大小(以 tokens 为单位)。
usedTokensnumber
当前使用的 tokens 数量。
usageLanguageModelUsage
来自 AI SDK 的详细 token 使用明细(输入、输出、推理、缓存 tokens)。
modelIdstring
用于成本计算的模型标识符(例如,"openai:gpt-4"、"anthropic:claude-3-opus")。

<ContextContent />

classstring
应用于组件的额外 CSS 类。

<ContextContentHeader />

classstring
应用于组件的额外 CSS 类。

<ContextContentBody />

classstring
应用于组件的额外 CSS 类。

<ContextContentFooter />

classstring
应用于组件的额外 CSS 类。

使用组件

所有使用组件(ContextInputUsageContextOutputUsageContextReasoningUsageContextCacheUsage)共享相同的 props:

classstring
应用于组件的额外 CSS 类。