repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import { encode } from 'gpt-tokenizer'; |
| #2 | |
| #3 | import { LobeAgent } from '../schema/agentMeta'; |
| #4 | |
| #5 | /** |
| #6 | * 计算 Agent 的 token 使用量 |
| #7 | * 计算 systemRole、openingMessage、openingQuestions、examples 等内容的 token 数量 |
| #8 | * @param agent Agent 对象 |
| #9 | * @returns token 使用量 |
| #10 | */ |
| #11 | export function calculateTokenUsage(agent: LobeAgent): number { |
| #12 | let totalTokens = 0; |
| #13 | |
| #14 | // 计算 systemRole 的 tokens |
| #15 | if (agent.config.systemRole) { |
| #16 | totalTokens += encode(agent.config.systemRole).length; |
| #17 | } |
| #18 | |
| #19 | return totalTokens; |
| #20 | } |
| #21 | |
| #22 | /** |
| #23 | * 更新 Agent 的 tokenUsage 字段 |
| #24 | * @param agent Agent 对象 |
| #25 | * @param force 是否强制重新计算,默认为 false |
| #26 | * @returns 更新后的 Agent 对象 |
| #27 | */ |
| #28 | export function updateAgentWithTokenUsage(agent: LobeAgent, force: boolean = false): LobeAgent { |
| #29 | // 如果已经有 tokenUsage 且不强制重新计算,则跳过 |
| #30 | if (!force && agent.tokenUsage && agent.tokenUsage > 0) { |
| #31 | return agent; |
| #32 | } |
| #33 | |
| #34 | const tokenUsage = calculateTokenUsage(agent); |
| #35 | return { |
| #36 | ...agent, |
| #37 | tokenUsage, |
| #38 | }; |
| #39 | } |
| #40 | |
| #41 | |
| #42 |