К основному содержанию
скилл CX Codex

Hedera Hook Creation — скилл для Claude Code: обзор и запуск

Скилл помогает создавать ненаблюдаемые и неизменяющие поток выполнения хуки для Hedera Agent Kit. Пригодится при настройке аудита вызовов, сборе метрик или сохранении состояния сессии.

Установить в Claude Code · Через Skills CLI (Vercel) Все способы и платформы ↓
npx skills add hedera-dev/hedera-skills --skill hedera-hook-creation -a claude-code
npx skills add hedera-dev/hedera-skills --skill agent-kit-hook -a claude-code
Открыть на GitHub

Что делает Hedera Hook Creation

Скилл обучает агента генерировать хуки, наследующие класс AbstractHook из библиотеки @hashgraph/hedera-agent-kit. Они подключаются к четырём фазам жизненного цикла BaseTool.execute(): до старта инструмента, после приведения параметров к нормальному виду, после основного действия и на итоговом этапе. Хуки решают фоновые задачи: формируют журнал аудита наподобие HcsAuditTrailHook, собирают счётчики вызовов и обогащают сессионный объект Context.

Хуки не блокируют выполнение операций. Для остановки нежелательных вызовов и проверки правил во фреймворке применяются политики. Если кастомный инструмент реализует интерфейс Tool напрямую в обход BaseTool, система хуков его проигнорирует.

Любая ошибка внутри хука без try/catch прерывает вызов инструмента целиком, поэтому внешние запросы и запись в HCS нужно изолировать. Также в первой строчке каждого метода важно фильтровать вызовы через список relevantTools, иначе хук будет запускаться при каждом действии агента.

Какие задачи решает

  • разработать класс аудита на базе AbstractHook для Hedera Agent Kit
  • перехватить параметры инструмента до и после выполнения фаз жизненного цикла
  • сохранять счётчики вызовов и промежуточное состояние сессии в Context
  • настроить фильтрацию запускаемых методов через массив relevantTools

Примеры запросов агенту

  • Помоги написать хук на TypeScript для логирования аргументов вызова transfer_hbar в Hedera Agent Kit
  • Создай аудит-хук с сохранением счётчиков вызовов инструментов в контексте агента
  • Как отслеживать результаты вызова BaseTool через postToolExecutionHook?

Как установить Hedera Hook Creation

1Через Skills CLI (Vercel)

npx skills add hedera-dev/hedera-skills --skill hedera-hook-creation -a claude-code
npx skills add hedera-dev/hedera-skills --skill agent-kit-hook -a claude-code

2Вручную: положить файлы

~/.claude/skills/agent-kit-hook/SKILL.md

Папка скилла целиком; для одного проекта — .claude/skills/agent-kit-hook/

По шагам, с командами для каждой платформы: Как установить скилл в Claude Code: пять способов

Вопросы о Hedera Hook Creation

Как установить Hedera Hook Creation в Claude Code?

Выполните команду npx skills add hedera-dev/hedera-skills --skill hedera-hook-creation -a claude-code. Агент получит правила проектирования хуков и типовые сигнатуры Hedera Agent Kit.

Что делает этот скилл?

Он объясняет агенту структуру класса AbstractHook и правила внедрения в жизненный цикл BaseTool. Это позволяет корректно отслеживать вызовы, вести аудит и работать с объектом Context.

Почему хук не реагирует на запуск инструмента?

Хуки срабатывают только для инструментов, унаследованных от BaseTool. Если инструмент напрямую реализует интерфейс Tool, механизм хуков не запускается.

Исходный текст

SKILL.md · Apache-2.0
---
name: Hedera Hook Creation
description: This skill should be used when the user asks to "create a hedera hook", "write an agent kit hook", "extend AbstractHook", "add a tool lifecycle hook", "log tool executions", "audit trail hook", "track tool calls", or needs guidance on building observation/logging/metrics extensions for Hedera Agent Kit tools. Hooks are non-blocking — for blocking validation rules use the policy skill instead.
version: 1.0.0
---

# Creating Hedera Agent Kit Hooks

Hooks are non-blocking extensions that run around the `BaseTool` lifecycle. They observe and can enrich execution: log calls, write audit trails, track metrics, persist state on `Context`. They **do not block** tool execution — that's the job of policies (see `agent-kit-policy` skill).

> Hooks fire **only** for tools that extend `BaseTool`. A tool that implements the `Tool` interface directly without extending `BaseTool` bypasses the hook system entirely.

## When to write a hook vs. a policy

| If you want to… | Use |
|---|---|
| Log/audit/track tool calls | **Hook** |
| Enrich `Context` (counters, session state) | **Hook** |
| Stop a call from happening when a condition holds | **Policy** (see `agent-kit-policy`) |
| Reject specific tool methods | **Policy** |

Built-in hooks include `HcsAuditTrailHook` and `HolAuditTrailHook` (HCS-backed audit trails) — both live in `@hashgraph/hedera-agent-kit/hooks`.

## The 7-stage lifecycle

`BaseTool.execute()` runs:

```text
[1] preToolExecutionHook        ← hook fires
[2] normalizeParams             (tool internal)
[3] postParamsNormalizationHook ← hook fires
[4] coreAction                  (tool internal)
[5] postCoreActionHook          ← hook fires
[6] secondaryAction             (tool internal)
[7] postToolExecutionHook       ← hook fires
```

Hooks tap into stages 1, 3, 5, 7. They never fire at stages 2, 4, 6.

For full param shapes at each stage see `references/lifecycle.md`.

## `AbstractHook` contract

```typescript
import { AbstractHook } from '@hashgraph/hedera-agent-kit';

export abstract class AbstractHook {
  abstract name: string;             // human-readable identifier
  abstract description?: string;     // what the hook does
  abstract relevantTools: string[];  // tool methods this hook cares about

  // Override only the stages you need; defaults are no-ops.
  preToolExecutionHook(params, method): Promise<any>;
  postParamsNormalizationHook(params, method): Promise<any>;
  postCoreActionHook(params, method): Promise<any>;
  postToolExecutionHook(params, method): Promise<any>;
}
```

**Critical:** the method signature is `(params, method)` — `params` is one of four typed objects, and `context` lives **inside** it (`params.context`). It is not a separate first argument.

| Stage | Params type | Notable fields |
|---|---|---|
| 1 | `PreToolExecutionParams` | `context`, `rawParams`, `client` |
| 3 | `PostParamsNormalizationParams` | `+ normalisedParams` |
| 5 | `PostCoreActionParams` | `+ coreActionResult` |
| 7 | `PostSecondaryActionParams` | `+ toolResult` |

## Skeleton

```typescript
import {
  AbstractHook,
  Context,
  PreToolExecutionParams,
  PostSecondaryActionParams,
} from '@hashgraph/hedera-agent-kit';

export class MyHook extends AbstractHook {
  name = 'My Hook';
  description = 'What this hook does, in one sentence';
  relevantTools = ['transfer_hbar', 'create_token'];

  async preToolExecutionHook(params: PreToolExecutionParams, method: string) {
    if (!this.relevantTools.includes(method)) return;
    // ... your pre-execution logic ...
    // params.context, params.rawParams, params.client
  }

  async postToolExecutionHook(params: PostSecondaryActionParams, method: string) {
    if (!this.relevantTools.includes(method)) return;
    // params.toolResult is the final tool output
  }
}
```

The first line of every hook method should filter by `relevantTools`. The kit dispatches **all** hooks for **all** tool calls — `relevantTools` is informational unless you act on it.

## Registering hooks

Hooks go on `Context.hooks`. The toolkit threads that context through to `BaseTool.execute()`, where the lifecycle dispatches them.

```typescript
import { AgentMode } from '@hashgraph/hedera-agent-kit';
import { allCorePlugins } from '@hashgraph/hedera-agent-kit/plugins';
import { HederaLangchainToolkit } from '@hashgraph/hedera-agent-kit-langchain';
import { MyHook } from './my-hook';

const toolkit = new HederaLangchainToolkit({
  client,
  configuration: {
    plugins: allCorePlugins,
    context: {
      mode: AgentMode.AUTONOMOUS,
      hooks: [new MyHook()],
    },
  },
});
```

Multiple hooks can coexist — they run in array order at each stage. Policies (see `agent-kit-policy`) are also `AbstractHook` instances and go in the same array.

## Multi-tool typing patterns

Hook params are generic (`any`) — the kit can't infer per-tool types because one hook can target many tools with different schemas. Three patterns:

### 1. Universal logic

When the hook is parameter-agnostic (counting calls, logging the whole payload as JSON):

```typescript
async postToolExecutionHook(params: PostSecondaryActionParams, method: string) {
  console.log(`[${method}]`, JSON.stringify(params.rawParams));
}
```

### 2. Type guards (small known set)

Switch on `method`, narrow with a cast or guard:

```typescript
async postParamsNormalizationHook(
  params: PostParamsNormalizationParams,
  method: string,
) {
  switch (method) {
    case 'transfer_hbar':
    case 'transfer_hbar_with_allowance': {
      const p = params.normalisedParams as { transfers: Array<{ amount: number }> };
      const total = p.transfers.reduce((s, t) => s + t.amount, 0);
      console.log(`[${method}] total HBAR: ${total}`);
      break;
    }
  }
}
```

### 3. Strategy map (pluggable, third-party tools)

Accept a `Record<string, (params: any) => SomeResult>` in the constructor. Validate each `additionalTool` has a strategy, throw at construction time if missing. This is the pattern used by `MaxRecipientsPolicy`.

## State on `Context`

`params.context` is the same `Context` instance for the whole agent run. Use it as a per-session scratchpad:

```typescript
async preToolExecutionHook(params: PreToolExecutionParams, method: string) {
  if (!this.relevantTools.includes(method)) return;
  const counts = ((params.context as any).callCounts ??= {} as Record<string, number>);
  counts[method] = (counts[method] ?? 0) + 1;
}
```

(`Context` is structurally typed; cast to attach ad-hoc state. If you publish the hook, document the keys you write.)

## Best practices

1. **Filter by `relevantTools`** at the top of every method — cheap and idiomatic
2. **Wrap in `try/catch`** if the hook does I/O (HCS write, HTTP) — a thrown hook aborts the whole tool call
3. **Keep hooks lightweight** — they sit on the hot path
4. **Don't block** — if you need to stop execution, write a policy
5. **Don't mutate `rawParams` or `normalisedParams`** unless you really mean to; subsequent hooks see your changes
6. **Use `description` to document side-effects** (HCS topic writes, external API calls) so policy authors / agent operators know what the hook does
7. **Tag log lines** with `[<hook-name>]` for easy grep

## Resources

- **`references/hook-interface.md`** — full param-type definitions, all four hook stages
- **`references/lifecycle.md`** — what fires when, around `BaseTool`'s `normalizeParams`/`coreAction`/`secondaryAction`
- **`examples/tx-timing-hook/`** — minimal hook that times tool execution and logs duration

## Common imports

```typescript
import {
  AbstractHook,
  Context,
  PreToolExecutionParams,
  PostParamsNormalizationParams,
  PostCoreActionParams,
  PostSecondaryActionParams,
} from '@hashgraph/hedera-agent-kit';

// Built-in implementations live in the /hooks subpath
import { HcsAuditTrailHook, HolAuditTrailHook } from '@hashgraph/hedera-agent-kit/hooks';
```

© hedera-dev · Apache-2.0 · hedera-dev/hedera-skills

Похожие скиллы

Все скиллы по теме «Настройка агента: память, контекст, субагенты» →
setup-matt-pocock-skills Matt Pocock Скилл настраивает репозиторий для инженерных скиллов Matt Pocock: задаёт трекер задач, метки триажа и расположение доменной документации. Запустите его перед первым использованием остальных скиллов. 1 052 851установок ask-matt Matt Pocock Помогает выбрать подходящий скилл или рабочий поток из набора Matt Pocock. Пригодится, когда непонятно, с чего начать задачу: от идеи до разбора бага. 766 587установок to-tickets Matt Pocock Разбивает план, спецификацию или переписку на небольшие сквозные задачи и отмечает, какие из них блокируют остальные. Пригодится, когда нужно согласовать порядок работы до публикации задач в трекере. 711 230установок writing-plans Jesse Vincent Скилл превращает требования к многошаговой задаче в подробный план реализации до начала работы с кодом. Пригодится, когда исполнителю нужно зафиксировать файлы, интерфейсы и проверки. CX Codex 282 859установок caveman-review JuliusBrussee Пишет замечания к коду одной строкой: место, проблема, исправление. Пригодится, когда ревью диффа или PR надо пройти быстро и без воды. CX Codex +1 411 800установок caveman-compress JuliusBrussee Сжимает CLAUDE.md, списки задач и другие текстовые файлы в краткий формат, сохраняя читаемую резервную копию. CX Codex +1 411 500установок

Из того же набора24

Session Management Ведёт журнал работ и техдолга между сессиями агента, чтобы после перезапуска было видно, что сделано и что осталось. CX Codex +1 538установок Quality Gates Скилл настраивает автоматические проверки кода: линт и типы после каждой правки, локальный CI перед пушем, чек-лист перед деплоем. CX Codex +1 536установок Hedera Policy Creation Помогает создавать блокирующие правила валидации для Hedera Agent Kit. Пригодится при настройке лимитов сумм, списков разрешённых аккаунтов и ограничений частоты вызовов. CX Codex +1 480установок Hedera Agent Kit v4 Migration Ведёт проект с [email protected] на v4: меняет пакеты, импорты и переписывает кастомные инструменты на BaseTool. CX Codex +1 484установок hts-system-contract Скилл помогает создавать и настраивать нативные токены Hedera через смарт-контракты Solidity с вызовом прекомпайла 0x167. Пригодится при выпуске NFT, настройке комиссий и правил KYC. CX Codex +1 463установок agent-kit-plugin Скилл помогает создавать плагины и инструменты для Hedera Agent Kit. Он пригодится при добавлении операций с токенами, аккаунтами и другими сервисами Hedera. CX Codex +1 463установок hss-system-contract Помогает проектировать отложенные транзакции и вызовы смарт-контрактов в сети Hedera через системный контракт 0x16b. Пригодится при автоматизации DeFi-протоколов и DAO. CX Codex +1 461установок create-harness-spec Превращает идею фичи Hedera в готовый рецепт для hedera-harness: PRD, spec.yaml, валидаторы. Пригодится, когда нужно перейти от замысла к запускаемой спецификации. CX Codex +1 465установок

Спонсоры проекта

Компании, которые помогают держать аналитику открытой