UNPKG

@langgraph-js/sdk

Version:

The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces

61 lines (60 loc) 1.94 kB
/** * @zh SpendTime 类用于计算和记录操作的耗时。 * @en The SpendTime class is used to calculate and record the time spent on operations. */ export class SpendTime { constructor() { this.timeCounter = new Map(); } /** * @zh 开始计时。 * @en Starts timing. */ start(key) { this.timeCounter.set(key, [new Date()]); } /** * @zh 结束计时。 * @en Ends timing. */ end(key) { var _a; this.timeCounter.set(key, [((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[0]) || new Date(), new Date()]); } /** * @zh 设置或更新指定键的耗时记录。如果键已存在,则更新结束时间;否则,开始新的计时。 * @en Sets or updates the time spent record for the specified key. If the key already exists, updates the end time; otherwise, starts a new timing. */ setSpendTime(key) { if (this.timeCounter.has(key)) { this.end(key); } else { this.start(key); } } /** * @zh 获取指定键的开始时间。 * @en Gets the start time for the specified key. */ getStartTime(key) { var _a; return ((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[0]) || new Date(); } /** * @zh 获取指定键的结束时间。 * @en Gets the end time for the specified key. */ getEndTime(key) { var _a; return ((_a = this.timeCounter.get(key)) === null || _a === void 0 ? void 0 : _a[1]) || new Date(); } /** * @zh 获取指定键的耗时(毫秒)。 * @en Gets the time spent (in milliseconds) for the specified key. */ getSpendTime(key) { const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()]; return end.getTime() - start.getTime(); } }