panel-breaker-stateful
它能做什么
这项技能指导智能体构建能妥善处理 API 故障并保留界面状态的仪表板面板。适合希望面板具有韧性、并能记住上次状态的开发者。
适用场景
- ✓当面板需要在 API 宕机期间使用缓存数据继续工作的时候。
- ✓当折叠或调整过尺寸等面板状态需要在两次访问之间保留的时候。
- ✓当你希望显示详细的错误信息,并提供重试方式的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-breaker-stateful/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请构建一个仪表板面板:即使 API 宕机也能继续工作,记住我是否折叠了它,并在出错时显示清楚的信息和重试按钮。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体创建一个带日志功能的熔断器工具,然后创建一个 Panel 基类,用于处理加载、错误状态和重试。每个面板都有自己的服务,其界面状态保存在 localStorage 中。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Resilient Dashboard Panel Pattern
This skill merges the best of data-service-merged and panel-component-enhanced to create panels that are resilient to API failures while offering enhanced UI features. Each panel manages its own data fetching with built-in circuit breakers and retry logic, and persists its state to localStorage.
Architecture Overview
- Service Module: Each panel has a dedicated service module for data fetching, using circuit breakers for resilience.
- Panel Class: Extends a base
Panelclass with built-in loading/error states, retry logic, and state persistence. - Data Flow: Panels automatically fetch data, handle errors with retries, and persist UI state.
Implementation
1. Circuit Breaker Utility with Enhanced Logging
Create src/utils/circuit-breaker.ts:
interface CircuitState {
failures: number;
cooldownUntil: number;
}
interface CacheEntry<T> {
data: T;
timestamp: number;
}
export interface CircuitBreakerOptions {
name: string;
maxFailures?: number;
cooldownMs?: number;
cacheTtlMs?: number;
}
export class CircuitBreaker<T> {
private state: CircuitState = { failures: 0, cooldownUntil: 0 };
private cache: CacheEntry<T> | null = null;
private name: string;
private maxFailures: number;
private cooldownMs: number;
private cacheTtlMs: number;
constructor(options: CircuitBreakerOptions) {
this.name = options.name;
this.maxFailures = options.maxFailures ?? 2;
this.cooldownMs = options.cooldownMs ?? 5 * 60 * 1000; // 5 minutes
this.cacheTtlMs = options.cacheTtlMs ?? 10 * 60 * 1000; // 10 minutes
}
isOnCooldown(): boolean {
if (Date.now() < this.state.cooldownUntil) return true;
if (this.state.cooldownUntil > 0) {
this.state = { failures: 0, cooldownUntil: 0 };
}
return false;
}
getCached(): T | null {
if (this.cache && Date.now() - this.cache.timestamp < this.cacheTtlMs) {
return this.cache.data;
}
return null;
}
recordSuccess(data: T): void {
this.state = { failures: 0, cooldownUntil: 0 };
this.cache = { data, timestamp: Date.now() };
console.log(`[${this.name}] Successfully fetched data`);
}
recordFailure(error?: string): void {
this.state.failures++;
if (this.state.failures >= this.maxFailures) {
this.state.cooldownUntil = Date.now() + this.cooldownMs;
console.warn(`[${this.name}] Cooldown for ${this.cooldownMs / 1000}s`);
}
console.error(`[${this.name}] Failure ${this.state.failures}/${this.maxFailures}:`, error);
}
async execute<R extends T>(fn: () => Promise<R>, defaultValue: R): Promise<R> {
if (this.isOnCooldown()) {
const cached = this.getCached();
return (cached as R) ?? defaultValue;
}
const cached = this.getCached();
if (cached !== null) return cached as R;
try {
const result = await fn();
this.recordSuccess(result);
return result;
} catch (e) {
this.recordFailure(String(e));
return defaultValue;
}
}
}
export function createCircuitBreaker<T>(options: CircuitBreakerOptions): CircuitBreaker<T> {
return new CircuitBreaker<T>(options);
}
2. Base Panel Class with Retry Logic and State Persistence
Create src/components/Panel.ts:
export interface PanelOptions {
id: string;
title: string;
showCount?: boolean;
className?: string;
}
export class Panel {
protected element: HTMLElement;
protected content: HTMLElement;
protected header: HTMLElement;
protected countEl: HTMLElement | null = null;
protected panelId: string;
private _fetching = false;
private retryAttempts = 0;
private maxRetries = 3;
private retryDelay = 1000;
constructor(options: PanelOptions) {
this.panelId = options.id;
this.element = document.createElement('div');
this.element.className = `panel ${options.className || ''}`;
this.element.dataset.panel = options.id;
// Header
this.header = document.createElement('div');
this.header.className = 'panel-header';
const headerLeft = document.createElement('div');
headerLeft.className = 'panel-header-left';
const title = document.createElement('span');
title.className = 'panel-title';
title.textContent = options.title;
headerLeft.appendChild(title);
this.header.appendChild(headerLeft);
if (options.showCount) {
this.countEl = document.createElement('span');
this.countEl.className = 'pane
常见问题
熔断器会记录什么?
技能提到熔断器中增强的日志功能,使失败和冷却情况都可以被看到。
状态保存在哪里?
面板将折叠或调整尺寸等界面状态保存到 localStorage。
宕机期间会发生什么?
面板在冷却期间显示缓存数据,而不是空白屏幕。
详情
作者的原始描述
Unified dashboard panel pattern combining circuit breaker resilience with enhanced UI features like retry logic, state persistence, and detailed error handling.
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13064
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。