@mastra/code-sdk 0.0.0-agent-tags-alpha-20261125185303
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +32 -0
- package/README.md +284 -0
- package/dist/acp/agent.d.ts +48 -0
- package/dist/acp/agent.js +414 -0
- package/dist/acp/errors.d.ts +3 -0
- package/dist/acp/errors.js +11 -0
- package/dist/acp/event-mapper.d.ts +28 -0
- package/dist/acp/event-mapper.js +274 -0
- package/dist/acp/index.d.ts +9 -0
- package/dist/acp/index.js +21 -0
- package/dist/acp/runtime.d.ts +5 -0
- package/dist/acp/runtime.js +91 -0
- package/dist/acp/server.d.ts +4 -0
- package/dist/acp/server.js +35 -0
- package/dist/acp/skills.d.ts +6 -0
- package/dist/acp/skills.js +30 -0
- package/dist/acp.d.ts +2 -0
- package/dist/acp.js +1 -0
- package/dist/agent-connections/messaging-processor.d.ts +17 -0
- package/dist/agent-connections/messaging-processor.js +131 -0
- package/dist/agent-connections/ownership.d.ts +24 -0
- package/dist/agent-connections/ownership.js +96 -0
- package/dist/agent-connections/registry.d.ts +20 -0
- package/dist/agent-connections/registry.js +167 -0
- package/dist/agent-connections/session-advertisement.d.ts +27 -0
- package/dist/agent-connections/session-advertisement.js +86 -0
- package/dist/agent-connections/signal-provider.d.ts +141 -0
- package/dist/agent-connections/signal-provider.js +35 -0
- package/dist/agent-connections/state-processor.d.ts +23 -0
- package/dist/agent-connections/state-processor.js +270 -0
- package/dist/agent-connections/thread-state.d.ts +41 -0
- package/dist/agent-connections/thread-state.js +157 -0
- package/dist/agent-connections/tools.d.ts +133 -0
- package/dist/agent-connections/tools.js +616 -0
- package/dist/agent-connections/types.d.ts +99 -0
- package/dist/agent-connections/types.js +4 -0
- package/dist/agent-connections/untrusted-text.d.ts +5 -0
- package/dist/agent-connections/untrusted-text.js +19 -0
- package/dist/agents/background-completion-events.d.ts +16 -0
- package/dist/agents/background-completion-events.js +16 -0
- package/dist/agents/background-completion.d.ts +22 -0
- package/dist/agents/background-completion.js +73 -0
- package/dist/agents/context-audit.d.ts +84 -0
- package/dist/agents/context-audit.js +115 -0
- package/dist/agents/credential-resolver.d.ts +74 -0
- package/dist/agents/credential-resolver.js +142 -0
- package/dist/agents/custom-provider-source.d.ts +35 -0
- package/dist/agents/custom-provider-source.js +22 -0
- package/dist/agents/fastembed-subprocess.d.ts +52 -0
- package/dist/agents/fastembed-subprocess.js +282 -0
- package/dist/agents/instructions.d.ts +42 -0
- package/dist/agents/instructions.js +69 -0
- package/dist/agents/mastracode-gateway.d.ts +85 -0
- package/dist/agents/mastracode-gateway.js +500 -0
- package/dist/agents/memory.d.ts +36 -0
- package/dist/agents/memory.js +248 -0
- package/dist/agents/model.d.ts +158 -0
- package/dist/agents/model.js +382 -0
- package/dist/agents/modes/build.d.ts +12 -0
- package/dist/agents/modes/build.js +33 -0
- package/dist/agents/modes/explore.d.ts +10 -0
- package/dist/agents/modes/explore.js +25 -0
- package/dist/agents/modes/plan.d.ts +8 -0
- package/dist/agents/modes/plan.js +43 -0
- package/dist/agents/prompts/agent-instructions.d.ts +122 -0
- package/dist/agents/prompts/agent-instructions.js +617 -0
- package/dist/agents/prompts/build.d.ts +12 -0
- package/dist/agents/prompts/build.js +73 -0
- package/dist/agents/prompts/fast.d.ts +5 -0
- package/dist/agents/prompts/fast.js +23 -0
- package/dist/agents/prompts/index.d.ts +61 -0
- package/dist/agents/prompts/index.js +141 -0
- package/dist/agents/prompts/model.d.ts +5 -0
- package/dist/agents/prompts/model.js +20 -0
- package/dist/agents/prompts/plan.d.ts +11 -0
- package/dist/agents/prompts/plan.js +132 -0
- package/dist/agents/prompts/tool-guidance.d.ts +18 -0
- package/dist/agents/prompts/tool-guidance.js +158 -0
- package/dist/agents/sandbox-filesystem.d.ts +232 -0
- package/dist/agents/sandbox-filesystem.js +851 -0
- package/dist/agents/subagents/execute.d.ts +3 -0
- package/dist/agents/subagents/execute.js +24 -0
- package/dist/agents/subagents/explore.d.ts +3 -0
- package/dist/agents/subagents/explore.js +15 -0
- package/dist/agents/subagents/index.d.ts +6 -0
- package/dist/agents/subagents/index.js +15 -0
- package/dist/agents/subagents/plan.d.ts +3 -0
- package/dist/agents/subagents/plan.js +15 -0
- package/dist/agents/thread-caveman-state.d.ts +17 -0
- package/dist/agents/thread-caveman-state.js +104 -0
- package/dist/agents/tool-availability.d.ts +17 -0
- package/dist/agents/tool-availability.js +134 -0
- package/dist/agents/tools.d.ts +37 -0
- package/dist/agents/tools.js +154 -0
- package/dist/agents/workflow-builder-agent.d.ts +2 -0
- package/dist/agents/workflow-builder-agent.js +79 -0
- package/dist/agents/workspace.d.ts +35 -0
- package/dist/agents/workspace.js +207 -0
- package/dist/analytics.d.ts +18 -0
- package/dist/analytics.js +159 -0
- package/dist/auth/account-rotation-processor.d.ts +210 -0
- package/dist/auth/account-rotation-processor.js +662 -0
- package/dist/auth/account-routing-context.d.ts +17 -0
- package/dist/auth/account-routing-context.js +38 -0
- package/dist/auth/authorization-input.d.ts +16 -0
- package/dist/auth/authorization-input.js +39 -0
- package/dist/auth/device-code.d.ts +93 -0
- package/dist/auth/device-code.js +126 -0
- package/dist/auth/index.d.ts +13 -0
- package/dist/auth/index.js +9 -0
- package/dist/auth/pkce.d.ts +13 -0
- package/dist/auth/pkce.js +29 -0
- package/dist/auth/provider-auth-error.d.ts +24 -0
- package/dist/auth/provider-auth-error.js +44 -0
- package/dist/auth/providers/anthropic.d.ts +40 -0
- package/dist/auth/providers/anthropic.js +117 -0
- package/dist/auth/providers/github-copilot.d.ts +154 -0
- package/dist/auth/providers/github-copilot.js +359 -0
- package/dist/auth/providers/kimi-coding.d.ts +38 -0
- package/dist/auth/providers/kimi-coding.js +225 -0
- package/dist/auth/providers/openai-codex.d.ts +141 -0
- package/dist/auth/providers/openai-codex.js +531 -0
- package/dist/auth/providers/xai.d.ts +64 -0
- package/dist/auth/providers/xai.js +223 -0
- package/dist/auth/storage.d.ts +222 -0
- package/dist/auth/storage.js +805 -0
- package/dist/auth/types.d.ts +164 -0
- package/dist/auth/types.js +1 -0
- package/dist/clipboard/index.d.ts +26 -0
- package/dist/clipboard/index.js +224 -0
- package/dist/constants.d.ts +29 -0
- package/dist/constants.js +34 -0
- package/dist/error-classification.d.ts +10 -0
- package/dist/error-classification.js +19 -0
- package/dist/evals/context-builder.d.ts +41 -0
- package/dist/evals/context-builder.js +156 -0
- package/dist/evals/index.d.ts +4 -0
- package/dist/evals/index.js +5 -0
- package/dist/evals/scorers/classify-command.d.ts +30 -0
- package/dist/evals/scorers/classify-command.js +61 -0
- package/dist/evals/scorers/efficiency.d.ts +44 -0
- package/dist/evals/scorers/efficiency.js +256 -0
- package/dist/evals/scorers/extract-tools.d.ts +25 -0
- package/dist/evals/scorers/extract-tools.js +57 -0
- package/dist/evals/scorers/index.d.ts +3 -0
- package/dist/evals/scorers/index.js +3 -0
- package/dist/evals/scorers/outcome.d.ts +49 -0
- package/dist/evals/scorers/outcome.js +360 -0
- package/dist/experimental-agent.d.ts +19 -0
- package/dist/experimental-agent.js +45 -0
- package/dist/factory/client.d.ts +44 -0
- package/dist/factory/client.js +110 -0
- package/dist/factory/remote-controller.d.ts +34 -0
- package/dist/factory/remote-controller.js +40 -0
- package/dist/factory/remote-session.d.ts +210 -0
- package/dist/factory/remote-session.js +415 -0
- package/dist/goal-manager.d.ts +114 -0
- package/dist/goal-manager.js +351 -0
- package/dist/goal-signal.d.ts +14 -0
- package/dist/goal-signal.js +14 -0
- package/dist/headless/cli.d.ts +55 -0
- package/dist/headless/cli.js +261 -0
- package/dist/headless/flags.d.ts +33 -0
- package/dist/headless/flags.js +207 -0
- package/dist/headless/format.d.ts +41 -0
- package/dist/headless/format.js +94 -0
- package/dist/headless/index.d.ts +25 -0
- package/dist/headless/index.js +6 -0
- package/dist/headless/policy.d.ts +17 -0
- package/dist/headless/policy.js +39 -0
- package/dist/headless/run-mc.d.ts +7 -0
- package/dist/headless/run-mc.js +438 -0
- package/dist/headless/types.d.ts +175 -0
- package/dist/headless/types.js +16 -0
- package/dist/hooks/config.d.ts +11 -0
- package/dist/hooks/config.js +77 -0
- package/dist/hooks/executor.d.ts +13 -0
- package/dist/hooks/executor.js +128 -0
- package/dist/hooks/index.d.ts +6 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/manager.d.ts +75 -0
- package/dist/hooks/manager.js +289 -0
- package/dist/hooks/types.d.ts +180 -0
- package/dist/hooks/types.js +5 -0
- package/dist/index.d.ts +537 -0
- package/dist/index.js +1399 -0
- package/dist/ipc/ipc-reporter.d.ts +14 -0
- package/dist/ipc/ipc-reporter.js +18 -0
- package/dist/knowledge-inspector.d.ts +139 -0
- package/dist/knowledge-inspector.js +533 -0
- package/dist/knowledge-scope-id.d.ts +7 -0
- package/dist/knowledge-scope-id.js +10 -0
- package/dist/knowledge-scope.d.ts +41 -0
- package/dist/knowledge-scope.js +85 -0
- package/dist/lsp/client.d.ts +56 -0
- package/dist/lsp/client.js +248 -0
- package/dist/lsp/index.d.ts +4 -0
- package/dist/lsp/index.js +4 -0
- package/dist/lsp/language.d.ts +9 -0
- package/dist/lsp/language.js +41 -0
- package/dist/lsp/manager.d.ts +34 -0
- package/dist/lsp/manager.js +79 -0
- package/dist/lsp/server.d.ts +27 -0
- package/dist/lsp/server.js +158 -0
- package/dist/lsp/workspace.d.ts +9 -0
- package/dist/lsp/workspace.js +52 -0
- package/dist/mcp/config.d.ts +77 -0
- package/dist/mcp/config.js +313 -0
- package/dist/mcp/index.d.ts +6 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/manager.d.ts +104 -0
- package/dist/mcp/manager.js +693 -0
- package/dist/mcp/state.d.ts +35 -0
- package/dist/mcp/state.js +94 -0
- package/dist/mcp/types.d.ts +139 -0
- package/dist/mcp/types.js +1 -0
- package/dist/onboarding/custom-packs.d.ts +7 -0
- package/dist/onboarding/custom-packs.js +17 -0
- package/dist/onboarding/custom-providers.d.ts +4 -0
- package/dist/onboarding/custom-providers.js +27 -0
- package/dist/onboarding/index.d.ts +5 -0
- package/dist/onboarding/index.js +3 -0
- package/dist/onboarding/om-settings.d.ts +47 -0
- package/dist/onboarding/om-settings.js +80 -0
- package/dist/onboarding/pack-activation.d.ts +43 -0
- package/dist/onboarding/pack-activation.js +105 -0
- package/dist/onboarding/packs.d.ts +106 -0
- package/dist/onboarding/packs.js +283 -0
- package/dist/onboarding/settings.d.ts +563 -0
- package/dist/onboarding/settings.js +1110 -0
- package/dist/permissions.d.ts +55 -0
- package/dist/permissions.js +139 -0
- package/dist/platform/index.d.ts +74 -0
- package/dist/platform/index.js +199 -0
- package/dist/plugin.d.ts +156 -0
- package/dist/plugin.js +27 -0
- package/dist/plugins/dependencies.d.ts +9 -0
- package/dist/plugins/dependencies.js +84 -0
- package/dist/plugins/install.d.ts +21 -0
- package/dist/plugins/install.js +187 -0
- package/dist/plugins/loader.d.ts +25 -0
- package/dist/plugins/loader.js +264 -0
- package/dist/plugins/manager.d.ts +115 -0
- package/dist/plugins/manager.js +507 -0
- package/dist/plugins/manifest.d.ts +14 -0
- package/dist/plugins/manifest.js +47 -0
- package/dist/plugins/package-link.d.ts +3 -0
- package/dist/plugins/package-link.js +58 -0
- package/dist/plugins/paths.d.ts +10 -0
- package/dist/plugins/paths.js +22 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +109 -0
- package/dist/plugins/scaffold.d.ts +10 -0
- package/dist/plugins/scaffold.js +115 -0
- package/dist/plugins/signal-lane.d.ts +59 -0
- package/dist/plugins/signal-lane.js +163 -0
- package/dist/plugins/types.d.ts +95 -0
- package/dist/plugins/types.js +1 -0
- package/dist/process-memory-diagnostics.d.ts +136 -0
- package/dist/process-memory-diagnostics.js +497 -0
- package/dist/processors/plan-rejection-abort.d.ts +16 -0
- package/dist/processors/plan-rejection-abort.js +62 -0
- package/dist/providers/amazon-bedrock-gateway.d.ts +47 -0
- package/dist/providers/amazon-bedrock-gateway.js +142 -0
- package/dist/providers/amazon-bedrock.d.ts +28 -0
- package/dist/providers/amazon-bedrock.js +99 -0
- package/dist/providers/anthropic-prompt-cache.d.ts +19 -0
- package/dist/providers/anthropic-prompt-cache.js +8 -0
- package/dist/providers/anthropic-thinking.d.ts +5 -0
- package/dist/providers/anthropic-thinking.js +22 -0
- package/dist/providers/claude-max.d.ts +67 -0
- package/dist/providers/claude-max.js +232 -0
- package/dist/providers/github-copilot-gateway.d.ts +35 -0
- package/dist/providers/github-copilot-gateway.js +58 -0
- package/dist/providers/github-copilot.d.ts +60 -0
- package/dist/providers/github-copilot.js +320 -0
- package/dist/providers/google-thinking.d.ts +31 -0
- package/dist/providers/google-thinking.js +64 -0
- package/dist/providers/google-vertex-gateway.d.ts +23 -0
- package/dist/providers/google-vertex-gateway.js +49 -0
- package/dist/providers/google-vertex.d.ts +6 -0
- package/dist/providers/google-vertex.js +51 -0
- package/dist/providers/kimi-coding.d.ts +14 -0
- package/dist/providers/kimi-coding.js +80 -0
- package/dist/providers/model-ids.d.ts +53 -0
- package/dist/providers/model-ids.js +132 -0
- package/dist/providers/openai-codex.d.ts +114 -0
- package/dist/providers/openai-codex.js +427 -0
- package/dist/providers/provider-thinking.d.ts +23 -0
- package/dist/providers/provider-thinking.js +61 -0
- package/dist/providers/thinking-request.d.ts +14 -0
- package/dist/providers/thinking-request.js +87 -0
- package/dist/providers/xai.d.ts +33 -0
- package/dist/providers/xai.js +72 -0
- package/dist/schedules/index.d.ts +10 -0
- package/dist/schedules/index.js +6 -0
- package/dist/schedules/interval.d.ts +49 -0
- package/dist/schedules/interval.js +212 -0
- package/dist/schedules/prompt.d.ts +36 -0
- package/dist/schedules/prompt.js +36 -0
- package/dist/schedules/run-script.d.ts +8 -0
- package/dist/schedules/run-script.js +80 -0
- package/dist/schedules/scheduler.d.ts +101 -0
- package/dist/schedules/scheduler.js +167 -0
- package/dist/schedules/tools.d.ts +57 -0
- package/dist/schedules/tools.js +217 -0
- package/dist/schema.d.ts +499 -0
- package/dist/schema.js +131 -0
- package/dist/theme-palette.d.ts +17 -0
- package/dist/theme-palette.js +18 -0
- package/dist/thinking-catalog.d.ts +4 -0
- package/dist/thinking-catalog.js +13 -0
- package/dist/thinking.d.ts +37 -0
- package/dist/thinking.js +134 -0
- package/dist/tool-names.d.ts +77 -0
- package/dist/tool-names.js +53 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/request-sandbox-access.d.ts +6 -0
- package/dist/tools/request-sandbox-access.js +80 -0
- package/dist/tools/utils.d.ts +20 -0
- package/dist/tools/utils.js +31 -0
- package/dist/tools/web-search.d.ts +116 -0
- package/dist/tools/web-search.js +158 -0
- package/dist/tools/workflows/create-workflow.d.ts +7 -0
- package/dist/tools/workflows/create-workflow.js +82 -0
- package/dist/tools/workflows/delete-workflow.d.ts +7 -0
- package/dist/tools/workflows/delete-workflow.js +18 -0
- package/dist/tools/workflows/ephemeral-memory.d.ts +28 -0
- package/dist/tools/workflows/ephemeral-memory.js +41 -0
- package/dist/tools/workflows/extract-json-schema.d.ts +12 -0
- package/dist/tools/workflows/extract-json-schema.js +19 -0
- package/dist/tools/workflows/get-workflow.d.ts +11 -0
- package/dist/tools/workflows/get-workflow.js +31 -0
- package/dist/tools/workflows/list-available-agents.d.ts +8 -0
- package/dist/tools/workflows/list-available-agents.js +29 -0
- package/dist/tools/workflows/list-available-classifiers.d.ts +19 -0
- package/dist/tools/workflows/list-available-classifiers.js +53 -0
- package/dist/tools/workflows/list-available-tools.d.ts +9 -0
- package/dist/tools/workflows/list-available-tools.js +37 -0
- package/dist/tools/workflows/list-available-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-available-workflows.js +29 -0
- package/dist/tools/workflows/list-workflows.d.ts +9 -0
- package/dist/tools/workflows/list-workflows.js +30 -0
- package/dist/tools/workflows/run-workflow.d.ts +9 -0
- package/dist/tools/workflows/run-workflow.js +49 -0
- package/dist/tools/workflows/save-workflow.d.ts +1125 -0
- package/dist/tools/workflows/save-workflow.js +25 -0
- package/dist/tools/workflows/tool-ids.d.ts +16 -0
- package/dist/tools/workflows/tool-ids.js +17 -0
- package/dist/utils/binaries.d.ts +7 -0
- package/dist/utils/binaries.js +97 -0
- package/dist/utils/debug-log.d.ts +12 -0
- package/dist/utils/debug-log.js +56 -0
- package/dist/utils/discarding-scores-storage.d.ts +33 -0
- package/dist/utils/discarding-scores-storage.js +51 -0
- package/dist/utils/errors.d.ts +36 -0
- package/dist/utils/errors.js +215 -0
- package/dist/utils/gateway-sync.d.ts +26 -0
- package/dist/utils/gateway-sync.js +58 -0
- package/dist/utils/local-observability.d.ts +19 -0
- package/dist/utils/local-observability.js +54 -0
- package/dist/utils/maintenance-lock.d.ts +42 -0
- package/dist/utils/maintenance-lock.js +196 -0
- package/dist/utils/notification-dispatch.d.ts +74 -0
- package/dist/utils/notification-dispatch.js +128 -0
- package/dist/utils/open-url.d.ts +10 -0
- package/dist/utils/open-url.js +27 -0
- package/dist/utils/path-security.d.ts +2 -0
- package/dist/utils/path-security.js +7 -0
- package/dist/utils/plan-diff.d.ts +25 -0
- package/dist/utils/plan-diff.js +59 -0
- package/dist/utils/plans.d.ts +61 -0
- package/dist/utils/plans.js +120 -0
- package/dist/utils/project.d.ts +160 -0
- package/dist/utils/project.js +361 -0
- package/dist/utils/prune-cli.d.ts +24 -0
- package/dist/utils/prune-cli.js +124 -0
- package/dist/utils/signals-pubsub.d.ts +103 -0
- package/dist/utils/signals-pubsub.js +409 -0
- package/dist/utils/slash-command-loader.d.ts +52 -0
- package/dist/utils/slash-command-loader.js +139 -0
- package/dist/utils/slash-command-processor.d.ts +14 -0
- package/dist/utils/slash-command-processor.js +106 -0
- package/dist/utils/stdin-pipe.d.ts +33 -0
- package/dist/utils/stdin-pipe.js +93 -0
- package/dist/utils/storage-factory.d.ts +50 -0
- package/dist/utils/storage-factory.js +118 -0
- package/dist/utils/storage-maintenance.d.ts +121 -0
- package/dist/utils/storage-maintenance.js +352 -0
- package/dist/utils/thread-lock.d.ts +26 -0
- package/dist/utils/thread-lock.js +109 -0
- package/dist/utils/token-estimator.d.ts +3 -0
- package/dist/utils/token-estimator.js +19 -0
- package/dist/utils/update-check.d.ts +151 -0
- package/dist/utils/update-check.js +430 -0
- package/dist/utils/wake-session.d.ts +44 -0
- package/dist/utils/wake-session.js +83 -0
- package/dist/voice/stt-registry.d.ts +67 -0
- package/dist/voice/stt-registry.js +144 -0
- package/dist/voice/transcribe.d.ts +10 -0
- package/dist/voice/transcribe.js +75 -0
- package/dist/workflows/register-primitives.d.ts +52 -0
- package/dist/workflows/register-primitives.js +40 -0
- package/dist/workflows/service.d.ts +64 -0
- package/dist/workflows/service.js +50 -0
- package/package.json +117 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { DEFAULT_CONFIG_DIR } from "../constants.js";
|
|
2
|
+
import * as path$1 from "path";
|
|
3
|
+
import * as fs$1 from "fs";
|
|
4
|
+
import * as os$1 from "os";
|
|
5
|
+
// src/hooks/config.ts
|
|
6
|
+
/**
|
|
7
|
+
* Hook configuration loading from filesystem.
|
|
8
|
+
* Loads from global (~/.mastracode/hooks.json) and project (.mastracode/hooks.json).
|
|
9
|
+
* Global hooks run first, project hooks append.
|
|
10
|
+
*/
|
|
11
|
+
const VALID_EVENTS = [
|
|
12
|
+
"PreToolUse",
|
|
13
|
+
"PostToolUse",
|
|
14
|
+
"Stop",
|
|
15
|
+
"UserPromptSubmit",
|
|
16
|
+
"SessionStart",
|
|
17
|
+
"SessionEnd",
|
|
18
|
+
"Notification",
|
|
19
|
+
"AgentStart",
|
|
20
|
+
"AgentEnd",
|
|
21
|
+
"PermissionRequest",
|
|
22
|
+
"PermissionResult",
|
|
23
|
+
"Interrupt",
|
|
24
|
+
"SubagentStart",
|
|
25
|
+
"SubagentEnd"
|
|
26
|
+
];
|
|
27
|
+
function loadHooksConfig(projectDir, configDirName = DEFAULT_CONFIG_DIR, homeDir) {
|
|
28
|
+
const globalPath = getGlobalHooksPath(configDirName, homeDir);
|
|
29
|
+
const projectPath = getProjectHooksPath(projectDir, configDirName);
|
|
30
|
+
return mergeConfigs(loadSingleConfig(globalPath), isSameFile(globalPath, projectPath) ? {} : loadSingleConfig(projectPath));
|
|
31
|
+
}
|
|
32
|
+
function isSameFile(a, b) {
|
|
33
|
+
try {
|
|
34
|
+
return fs$1.realpathSync(a) === fs$1.realpathSync(b);
|
|
35
|
+
} catch {
|
|
36
|
+
return path$1.resolve(a) === path$1.resolve(b);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function getProjectHooksPath(projectDir, configDirName = DEFAULT_CONFIG_DIR) {
|
|
40
|
+
return path$1.join(projectDir, configDirName, "hooks.json");
|
|
41
|
+
}
|
|
42
|
+
function getGlobalHooksPath(configDirName = DEFAULT_CONFIG_DIR, homeDir = os$1.homedir()) {
|
|
43
|
+
return path$1.join(homeDir, configDirName, "hooks.json");
|
|
44
|
+
}
|
|
45
|
+
function loadSingleConfig(filePath) {
|
|
46
|
+
try {
|
|
47
|
+
if (!fs$1.existsSync(filePath)) return {};
|
|
48
|
+
const raw = fs$1.readFileSync(filePath, "utf-8");
|
|
49
|
+
return validateConfig(JSON.parse(raw));
|
|
50
|
+
} catch {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function validateConfig(raw) {
|
|
55
|
+
if (!raw || typeof raw !== "object") return {};
|
|
56
|
+
const config = {};
|
|
57
|
+
const obj = raw;
|
|
58
|
+
for (const event of VALID_EVENTS) if (Array.isArray(obj[event])) {
|
|
59
|
+
const hooks = obj[event].filter(isValidHook);
|
|
60
|
+
if (hooks.length > 0) config[event] = hooks;
|
|
61
|
+
}
|
|
62
|
+
return config;
|
|
63
|
+
}
|
|
64
|
+
function isValidHook(raw) {
|
|
65
|
+
if (!raw || typeof raw !== "object") return false;
|
|
66
|
+
const obj = raw;
|
|
67
|
+
return obj.type === "command" && typeof obj.command === "string";
|
|
68
|
+
}
|
|
69
|
+
function mergeConfigs(global, project) {
|
|
70
|
+
const merged = {};
|
|
71
|
+
for (const event of VALID_EVENTS) {
|
|
72
|
+
const combined = [...global[event] ?? [], ...project[event] ?? []];
|
|
73
|
+
if (combined.length > 0) merged[event] = combined;
|
|
74
|
+
}
|
|
75
|
+
return merged;
|
|
76
|
+
}
|
|
77
|
+
export { VALID_EVENTS, getGlobalHooksPath, getProjectHooksPath, loadHooksConfig };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook process execution.
|
|
3
|
+
* Spawns shell commands, handles stdin/stdout/exit-code protocol.
|
|
4
|
+
*/
|
|
5
|
+
import type { HookDefinition, HookStdin, HookResult, HookEventResult } from './types.js';
|
|
6
|
+
export declare function executeHook(hook: HookDefinition, stdinPayload: HookStdin): Promise<HookResult>;
|
|
7
|
+
export declare function matchesHook(hook: HookDefinition, context: {
|
|
8
|
+
tool_name?: string;
|
|
9
|
+
}): boolean;
|
|
10
|
+
export declare function runHooksForEvent(hooks: HookDefinition[], stdinPayload: HookStdin, matchContext?: {
|
|
11
|
+
tool_name?: string;
|
|
12
|
+
}): Promise<HookEventResult>;
|
|
13
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { isBlockingEvent } from "./types.js";
|
|
2
|
+
import { spawn } from "child_process";
|
|
3
|
+
// src/hooks/executor.ts
|
|
4
|
+
/**
|
|
5
|
+
* Hook process execution.
|
|
6
|
+
* Spawns shell commands, handles stdin/stdout/exit-code protocol.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_TIMEOUT = 1e4;
|
|
9
|
+
async function executeHook(hook, stdinPayload) {
|
|
10
|
+
const timeout = hook.timeout ?? DEFAULT_TIMEOUT;
|
|
11
|
+
const startTime = Date.now();
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
const isWindows = process.platform === "win32";
|
|
14
|
+
const shell = isWindows ? "cmd" : "/bin/sh";
|
|
15
|
+
const shellArgs = isWindows ? ["/c", hook.command] : ["-c", hook.command];
|
|
16
|
+
const child = spawn(shell, shellArgs, {
|
|
17
|
+
stdio: [
|
|
18
|
+
"pipe",
|
|
19
|
+
"pipe",
|
|
20
|
+
"pipe"
|
|
21
|
+
],
|
|
22
|
+
cwd: stdinPayload.cwd,
|
|
23
|
+
...isWindows ? { windowsVerbatimArguments: true } : {},
|
|
24
|
+
env: {
|
|
25
|
+
...process.env,
|
|
26
|
+
MASTRA_HOOK_EVENT: stdinPayload.hook_event_name
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
let stdout = "";
|
|
30
|
+
let stderr = "";
|
|
31
|
+
let timedOut = false;
|
|
32
|
+
let resolved = false;
|
|
33
|
+
const timer = setTimeout(() => {
|
|
34
|
+
timedOut = true;
|
|
35
|
+
child.kill("SIGKILL");
|
|
36
|
+
}, timeout);
|
|
37
|
+
child.stdout?.on("data", (data) => {
|
|
38
|
+
stdout += data.toString();
|
|
39
|
+
});
|
|
40
|
+
child.stderr?.on("data", (data) => {
|
|
41
|
+
stderr += data.toString();
|
|
42
|
+
});
|
|
43
|
+
child.on("close", (exitCode) => {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
if (resolved) return;
|
|
46
|
+
resolved = true;
|
|
47
|
+
let parsedStdout;
|
|
48
|
+
if (stdout.trim()) try {
|
|
49
|
+
parsedStdout = JSON.parse(stdout.trim());
|
|
50
|
+
} catch {}
|
|
51
|
+
resolve({
|
|
52
|
+
hook,
|
|
53
|
+
exitCode: exitCode ?? 1,
|
|
54
|
+
stdout: parsedStdout,
|
|
55
|
+
stderr: stderr.trim() || void 0,
|
|
56
|
+
timedOut,
|
|
57
|
+
durationMs: Date.now() - startTime
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
child.on("error", (error) => {
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
if (resolved) return;
|
|
63
|
+
resolved = true;
|
|
64
|
+
resolve({
|
|
65
|
+
hook,
|
|
66
|
+
exitCode: 1,
|
|
67
|
+
stderr: error.message,
|
|
68
|
+
timedOut: false,
|
|
69
|
+
durationMs: Date.now() - startTime
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
child.stdin?.on("error", () => {});
|
|
73
|
+
try {
|
|
74
|
+
child.stdin?.write(JSON.stringify(stdinPayload));
|
|
75
|
+
child.stdin?.end();
|
|
76
|
+
} catch {}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function matchesHook(hook, context) {
|
|
80
|
+
if (!hook.matcher) return true;
|
|
81
|
+
if (hook.matcher.tool_name) {
|
|
82
|
+
if (!context.tool_name) return false;
|
|
83
|
+
try {
|
|
84
|
+
return new RegExp(hook.matcher.tool_name).test(context.tool_name);
|
|
85
|
+
} catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
async function runHooksForEvent(hooks, stdinPayload, matchContext = {}) {
|
|
92
|
+
const results = [];
|
|
93
|
+
const warnings = [];
|
|
94
|
+
let additionalContext;
|
|
95
|
+
const applicable = hooks.filter((h) => matchesHook(h, matchContext));
|
|
96
|
+
if (applicable.length === 0) return {
|
|
97
|
+
allowed: true,
|
|
98
|
+
results: [],
|
|
99
|
+
warnings: []
|
|
100
|
+
};
|
|
101
|
+
const blocking = isBlockingEvent(stdinPayload.hook_event_name);
|
|
102
|
+
for (const hook of applicable) {
|
|
103
|
+
const result = await executeHook(hook, stdinPayload);
|
|
104
|
+
results.push(result);
|
|
105
|
+
if (result.stdout?.additionalContext) additionalContext = additionalContext ? `${additionalContext}\n${result.stdout.additionalContext}` : result.stdout.additionalContext;
|
|
106
|
+
if (result.timedOut) {
|
|
107
|
+
warnings.push(`Hook timed out after ${hook.timeout ?? DEFAULT_TIMEOUT}ms: ${hook.command}`);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (result.exitCode === 2 && blocking) return {
|
|
111
|
+
allowed: false,
|
|
112
|
+
blockReason: result.stdout?.reason || result.stderr || `Blocked by hook: ${hook.description || hook.command}`,
|
|
113
|
+
additionalContext,
|
|
114
|
+
results,
|
|
115
|
+
warnings
|
|
116
|
+
};
|
|
117
|
+
if (result.exitCode === 0) continue;
|
|
118
|
+
const warnMsg = result.stderr || `Hook exited with code ${result.exitCode}`;
|
|
119
|
+
warnings.push(`${hook.description || hook.command}: ${warnMsg}`);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
allowed: true,
|
|
123
|
+
additionalContext,
|
|
124
|
+
results,
|
|
125
|
+
warnings
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export { executeHook, matchesHook, runHooksForEvent };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { HookManager } from './manager.js';
|
|
2
|
+
export { loadHooksConfig, getProjectHooksPath, getGlobalHooksPath, VALID_EVENTS } from './config.js';
|
|
3
|
+
export { executeHook, runHooksForEvent, matchesHook } from './executor.js';
|
|
4
|
+
export { isBlockingEvent } from './types.js';
|
|
5
|
+
export type { HookEventName, HookDefinition, HookMatcher, HooksConfig, HookStdin, HookStdinBase, HookStdinToolEvent, HookStdinUserPrompt, HookStdinStop, HookStdinSession, SessionStartSource, HookStdinNotification, HookStdinAgentStart, HookStdinAgentEnd, HookStdinPermissionRequest, HookStdinPermissionResult, HookStdinInterrupt, HookStdinSubagentStart, HookStdinSubagentEnd, HookStdout, HookResult, HookEventResult, BlockingHookEvent, LifecycleHookEvent, PermissionKind, PermissionDecision, InterruptReason, } from './types.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { HookManager } from "./manager.js";
|
|
2
|
+
import { VALID_EVENTS, getGlobalHooksPath, getProjectHooksPath, loadHooksConfig } from "./config.js";
|
|
3
|
+
import { executeHook, matchesHook, runHooksForEvent } from "./executor.js";
|
|
4
|
+
import { isBlockingEvent } from "./types.js";
|
|
5
|
+
export { HookManager, VALID_EVENTS, executeHook, getGlobalHooksPath, getProjectHooksPath, isBlockingEvent, loadHooksConfig, matchesHook, runHooksForEvent };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { HooksConfig, HookEventResult, SessionStartSource, PermissionKind, PermissionDecision, InterruptReason, SessionWorktreeInfo } from './types.js';
|
|
2
|
+
export declare class HookManager {
|
|
3
|
+
private config;
|
|
4
|
+
private projectDir;
|
|
5
|
+
private sessionId;
|
|
6
|
+
private configDirName;
|
|
7
|
+
private homeDir?;
|
|
8
|
+
private runId?;
|
|
9
|
+
/**
|
|
10
|
+
* Run id each pending permission prompt was raised under. A suspended run can
|
|
11
|
+
* end (clearing runId) while its prompt stays open, so the result must not
|
|
12
|
+
* read the current run id.
|
|
13
|
+
*/
|
|
14
|
+
private permissionRunIds;
|
|
15
|
+
private worktree?;
|
|
16
|
+
/** True once SessionStart has fired, so later thread binds announce themselves. */
|
|
17
|
+
private sessionStarted;
|
|
18
|
+
/**
|
|
19
|
+
* The latest queued SessionStart, settled or not. SessionStart runs one after another,
|
|
20
|
+
* and other events wait for it, so hooks see each session in order and before its events.
|
|
21
|
+
*/
|
|
22
|
+
private pendingSessionStart?;
|
|
23
|
+
constructor(projectDir: string, sessionId: string, configDirName?: string, homeDir?: string, worktree?: SessionWorktreeInfo);
|
|
24
|
+
reload(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Bind hooks to a thread. After SessionStart has fired, binding a different thread
|
|
27
|
+
* (the first message creating one, `/new`, a resumed thread) fires SessionStart again
|
|
28
|
+
* with `source: 'thread_change'`, so a hook that tracks sessions learns the new id
|
|
29
|
+
* before any event of that thread reaches it.
|
|
30
|
+
*/
|
|
31
|
+
setSessionId(sessionId: string): void;
|
|
32
|
+
/** Dispatch an event's hooks once any queued SessionStart has finished. */
|
|
33
|
+
private dispatch;
|
|
34
|
+
/** Set the active agent run id. Included as `run_id` in all hook stdin while a run is active. */
|
|
35
|
+
setRunId(runId: string): void;
|
|
36
|
+
/** Clear the active run id. Call only after end-of-run hooks (AgentEnd, Stop) have been dispatched. */
|
|
37
|
+
clearRunId(): void;
|
|
38
|
+
getRunId(): string | undefined;
|
|
39
|
+
hasHooks(): boolean;
|
|
40
|
+
getConfig(): HooksConfig;
|
|
41
|
+
getConfigPaths(): {
|
|
42
|
+
project: string;
|
|
43
|
+
global: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Build the common stdin base fields. `run_id` is included when an active run
|
|
47
|
+
* is set, so every event dispatched during a run carries the same identifier.
|
|
48
|
+
*/
|
|
49
|
+
private baseStdinFields;
|
|
50
|
+
/**
|
|
51
|
+
* Session stdin, carrying worktree identity when there is one. Emitted on both
|
|
52
|
+
* session events so a hook that provisions on start can tear down on end using
|
|
53
|
+
* the same fields.
|
|
54
|
+
*/
|
|
55
|
+
private sessionStdin;
|
|
56
|
+
runPreToolUse(toolName: string, toolInput: unknown): Promise<HookEventResult>;
|
|
57
|
+
runPostToolUse(toolName: string, toolInput: unknown, toolOutput: unknown, toolError: boolean): Promise<HookEventResult>;
|
|
58
|
+
runUserPromptSubmit(userMessage: string): Promise<HookEventResult>;
|
|
59
|
+
runStop(assistantMessage: string | undefined, stopReason: 'complete' | 'aborted' | 'error'): Promise<HookEventResult>;
|
|
60
|
+
runSessionStart(source?: SessionStartSource): Promise<HookEventResult>;
|
|
61
|
+
runSessionEnd(): Promise<HookEventResult>;
|
|
62
|
+
/**
|
|
63
|
+
* Fire notification hooks (non-blocking, fire-and-forget).
|
|
64
|
+
* Called when the TUI is waiting for user input.
|
|
65
|
+
*/
|
|
66
|
+
runNotification(reason: string, message?: string): void;
|
|
67
|
+
runAgentStart(): Promise<HookEventResult>;
|
|
68
|
+
runAgentEnd(stopReason: 'complete' | 'aborted' | 'error' | 'suspended'): Promise<HookEventResult>;
|
|
69
|
+
runPermissionRequest(permissionKind: PermissionKind, toolCallId: string, toolName: string, toolInput?: unknown): Promise<HookEventResult>;
|
|
70
|
+
runPermissionResult(permissionKind: PermissionKind, toolCallId: string, toolName: string, decision: PermissionDecision, toolInput?: unknown): Promise<HookEventResult>;
|
|
71
|
+
runInterrupt(reason: InterruptReason): Promise<HookEventResult>;
|
|
72
|
+
runSubagentStart(toolCallId: string, agentType: string, task: string, modelId?: string, forked?: boolean): Promise<HookEventResult>;
|
|
73
|
+
runSubagentEnd(toolCallId: string, agentType: string, result: unknown, isError: boolean, durationMs: number): Promise<HookEventResult>;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { DEFAULT_CONFIG_DIR } from "../constants.js";
|
|
2
|
+
import { getGlobalHooksPath, getProjectHooksPath, loadHooksConfig } from "./config.js";
|
|
3
|
+
import { runHooksForEvent } from "./executor.js";
|
|
4
|
+
// src/hooks/manager.ts
|
|
5
|
+
/**
|
|
6
|
+
* HookManager — high-level orchestration for the hooks system.
|
|
7
|
+
* Created once at startup, provides methods for each lifecycle event.
|
|
8
|
+
*/
|
|
9
|
+
const emptyResult = () => ({
|
|
10
|
+
allowed: true,
|
|
11
|
+
results: [],
|
|
12
|
+
warnings: []
|
|
13
|
+
});
|
|
14
|
+
var HookManager = class {
|
|
15
|
+
config;
|
|
16
|
+
projectDir;
|
|
17
|
+
sessionId;
|
|
18
|
+
configDirName;
|
|
19
|
+
homeDir;
|
|
20
|
+
runId;
|
|
21
|
+
/**
|
|
22
|
+
* Run id each pending permission prompt was raised under. A suspended run can
|
|
23
|
+
* end (clearing runId) while its prompt stays open, so the result must not
|
|
24
|
+
* read the current run id.
|
|
25
|
+
*/
|
|
26
|
+
permissionRunIds = /* @__PURE__ */ new Map();
|
|
27
|
+
worktree;
|
|
28
|
+
/** True once SessionStart has fired, so later thread binds announce themselves. */
|
|
29
|
+
sessionStarted = false;
|
|
30
|
+
/**
|
|
31
|
+
* The latest queued SessionStart, settled or not. SessionStart runs one after another,
|
|
32
|
+
* and other events wait for it, so hooks see each session in order and before its events.
|
|
33
|
+
*/
|
|
34
|
+
pendingSessionStart;
|
|
35
|
+
constructor(projectDir, sessionId, configDirName = DEFAULT_CONFIG_DIR, homeDir, worktree) {
|
|
36
|
+
this.worktree = worktree;
|
|
37
|
+
this.projectDir = projectDir;
|
|
38
|
+
this.sessionId = sessionId;
|
|
39
|
+
this.configDirName = configDirName;
|
|
40
|
+
this.homeDir = homeDir;
|
|
41
|
+
this.config = loadHooksConfig(projectDir, configDirName, homeDir);
|
|
42
|
+
}
|
|
43
|
+
reload() {
|
|
44
|
+
this.config = loadHooksConfig(this.projectDir, this.configDirName, this.homeDir);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Bind hooks to a thread. After SessionStart has fired, binding a different thread
|
|
48
|
+
* (the first message creating one, `/new`, a resumed thread) fires SessionStart again
|
|
49
|
+
* with `source: 'thread_change'`, so a hook that tracks sessions learns the new id
|
|
50
|
+
* before any event of that thread reaches it.
|
|
51
|
+
*/
|
|
52
|
+
setSessionId(sessionId) {
|
|
53
|
+
if (sessionId === this.sessionId) return;
|
|
54
|
+
this.sessionId = sessionId;
|
|
55
|
+
if (this.sessionStarted) this.runSessionStart("thread_change").catch(() => {});
|
|
56
|
+
}
|
|
57
|
+
/** Dispatch an event's hooks once any queued SessionStart has finished. */
|
|
58
|
+
async dispatch(...args) {
|
|
59
|
+
if (this.pendingSessionStart) await this.pendingSessionStart;
|
|
60
|
+
return runHooksForEvent(...args);
|
|
61
|
+
}
|
|
62
|
+
/** Set the active agent run id. Included as `run_id` in all hook stdin while a run is active. */
|
|
63
|
+
setRunId(runId) {
|
|
64
|
+
this.runId = runId;
|
|
65
|
+
}
|
|
66
|
+
/** Clear the active run id. Call only after end-of-run hooks (AgentEnd, Stop) have been dispatched. */
|
|
67
|
+
clearRunId() {
|
|
68
|
+
this.runId = void 0;
|
|
69
|
+
}
|
|
70
|
+
getRunId() {
|
|
71
|
+
return this.runId;
|
|
72
|
+
}
|
|
73
|
+
hasHooks() {
|
|
74
|
+
return Object.keys(this.config).length > 0;
|
|
75
|
+
}
|
|
76
|
+
getConfig() {
|
|
77
|
+
return this.config;
|
|
78
|
+
}
|
|
79
|
+
getConfigPaths() {
|
|
80
|
+
return {
|
|
81
|
+
project: getProjectHooksPath(this.projectDir, this.configDirName),
|
|
82
|
+
global: getGlobalHooksPath(this.configDirName, this.homeDir)
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Build the common stdin base fields. `run_id` is included when an active run
|
|
87
|
+
* is set, so every event dispatched during a run carries the same identifier.
|
|
88
|
+
*/
|
|
89
|
+
baseStdinFields(hook_event_name) {
|
|
90
|
+
const base = {
|
|
91
|
+
session_id: this.sessionId,
|
|
92
|
+
cwd: this.projectDir,
|
|
93
|
+
hook_event_name
|
|
94
|
+
};
|
|
95
|
+
if (this.runId) base.run_id = this.runId;
|
|
96
|
+
return base;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Session stdin, carrying worktree identity when there is one. Emitted on both
|
|
100
|
+
* session events so a hook that provisions on start can tear down on end using
|
|
101
|
+
* the same fields.
|
|
102
|
+
*/
|
|
103
|
+
sessionStdin(hook_event_name, source) {
|
|
104
|
+
const stdin = this.baseStdinFields(hook_event_name);
|
|
105
|
+
if (source) stdin.source = source;
|
|
106
|
+
if (this.worktree) {
|
|
107
|
+
stdin.worktree_path = this.worktree.path;
|
|
108
|
+
if (this.worktree.branch) stdin.branch = this.worktree.branch;
|
|
109
|
+
if (this.worktree.mainRepoPath) stdin.main_repo_path = this.worktree.mainRepoPath;
|
|
110
|
+
}
|
|
111
|
+
return stdin;
|
|
112
|
+
}
|
|
113
|
+
async runPreToolUse(toolName, toolInput) {
|
|
114
|
+
const hooks = this.config.PreToolUse;
|
|
115
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
116
|
+
const stdin = {
|
|
117
|
+
...this.baseStdinFields("PreToolUse"),
|
|
118
|
+
tool_name: toolName,
|
|
119
|
+
tool_input: toolInput
|
|
120
|
+
};
|
|
121
|
+
return this.dispatch(hooks, stdin, { tool_name: toolName });
|
|
122
|
+
}
|
|
123
|
+
async runPostToolUse(toolName, toolInput, toolOutput, toolError) {
|
|
124
|
+
const hooks = this.config.PostToolUse;
|
|
125
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
126
|
+
const stdin = {
|
|
127
|
+
...this.baseStdinFields("PostToolUse"),
|
|
128
|
+
tool_name: toolName,
|
|
129
|
+
tool_input: toolInput,
|
|
130
|
+
tool_output: toolOutput,
|
|
131
|
+
tool_error: toolError
|
|
132
|
+
};
|
|
133
|
+
return this.dispatch(hooks, stdin, { tool_name: toolName });
|
|
134
|
+
}
|
|
135
|
+
async runUserPromptSubmit(userMessage) {
|
|
136
|
+
const hooks = this.config.UserPromptSubmit;
|
|
137
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
138
|
+
const stdin = {
|
|
139
|
+
...this.baseStdinFields("UserPromptSubmit"),
|
|
140
|
+
user_message: userMessage
|
|
141
|
+
};
|
|
142
|
+
return this.dispatch(hooks, stdin);
|
|
143
|
+
}
|
|
144
|
+
async runStop(assistantMessage, stopReason) {
|
|
145
|
+
const hooks = this.config.Stop;
|
|
146
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
147
|
+
const stdin = {
|
|
148
|
+
...this.baseStdinFields("Stop"),
|
|
149
|
+
assistant_message: assistantMessage,
|
|
150
|
+
stop_reason: stopReason
|
|
151
|
+
};
|
|
152
|
+
return this.dispatch(hooks, stdin);
|
|
153
|
+
}
|
|
154
|
+
async runSessionStart(source = "startup") {
|
|
155
|
+
this.sessionStarted = true;
|
|
156
|
+
const hooks = this.config.SessionStart;
|
|
157
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
158
|
+
const stdin = this.sessionStdin("SessionStart", source);
|
|
159
|
+
const previous = this.pendingSessionStart;
|
|
160
|
+
const run = previous ? previous.then(() => runHooksForEvent(hooks, stdin)) : runHooksForEvent(hooks, stdin);
|
|
161
|
+
const settled = run.then(() => {}, () => {});
|
|
162
|
+
this.pendingSessionStart = settled;
|
|
163
|
+
settled.then(() => {
|
|
164
|
+
if (this.pendingSessionStart === settled) this.pendingSessionStart = void 0;
|
|
165
|
+
});
|
|
166
|
+
return run;
|
|
167
|
+
}
|
|
168
|
+
async runSessionEnd() {
|
|
169
|
+
const hooks = this.config.SessionEnd;
|
|
170
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
171
|
+
const stdin = this.sessionStdin("SessionEnd");
|
|
172
|
+
return this.dispatch(hooks, stdin);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Fire notification hooks (non-blocking, fire-and-forget).
|
|
176
|
+
* Called when the TUI is waiting for user input.
|
|
177
|
+
*/
|
|
178
|
+
runNotification(reason, message) {
|
|
179
|
+
const hooks = this.config.Notification;
|
|
180
|
+
if (!hooks || hooks.length === 0) return;
|
|
181
|
+
const stdin = {
|
|
182
|
+
...this.baseStdinFields("Notification"),
|
|
183
|
+
reason,
|
|
184
|
+
message
|
|
185
|
+
};
|
|
186
|
+
this.dispatch(hooks, stdin).catch(() => {});
|
|
187
|
+
}
|
|
188
|
+
async runAgentStart() {
|
|
189
|
+
const hooks = this.config.AgentStart;
|
|
190
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
191
|
+
const runId = this.runId;
|
|
192
|
+
if (!runId) return emptyResult();
|
|
193
|
+
const stdin = {
|
|
194
|
+
...this.baseStdinFields("AgentStart"),
|
|
195
|
+
run_id: runId
|
|
196
|
+
};
|
|
197
|
+
return this.dispatch(hooks, stdin);
|
|
198
|
+
}
|
|
199
|
+
async runAgentEnd(stopReason) {
|
|
200
|
+
const hooks = this.config.AgentEnd;
|
|
201
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
202
|
+
const runId = this.runId;
|
|
203
|
+
if (!runId) return emptyResult();
|
|
204
|
+
const stdin = {
|
|
205
|
+
...this.baseStdinFields("AgentEnd"),
|
|
206
|
+
run_id: runId,
|
|
207
|
+
stop_reason: stopReason
|
|
208
|
+
};
|
|
209
|
+
return this.dispatch(hooks, stdin);
|
|
210
|
+
}
|
|
211
|
+
async runPermissionRequest(permissionKind, toolCallId, toolName, toolInput) {
|
|
212
|
+
const runId = this.runId;
|
|
213
|
+
if (runId) this.permissionRunIds.set(toolCallId, runId);
|
|
214
|
+
const hooks = this.config.PermissionRequest;
|
|
215
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
216
|
+
if (!runId) return emptyResult();
|
|
217
|
+
const stdin = {
|
|
218
|
+
...this.baseStdinFields("PermissionRequest"),
|
|
219
|
+
run_id: runId,
|
|
220
|
+
permission_kind: permissionKind,
|
|
221
|
+
tool_call_id: toolCallId,
|
|
222
|
+
tool_name: toolName,
|
|
223
|
+
...toolInput !== void 0 ? { tool_input: toolInput } : {}
|
|
224
|
+
};
|
|
225
|
+
return this.dispatch(hooks, stdin);
|
|
226
|
+
}
|
|
227
|
+
async runPermissionResult(permissionKind, toolCallId, toolName, decision, toolInput) {
|
|
228
|
+
const runId = this.permissionRunIds.get(toolCallId) ?? this.runId;
|
|
229
|
+
this.permissionRunIds.delete(toolCallId);
|
|
230
|
+
const hooks = this.config.PermissionResult;
|
|
231
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
232
|
+
if (!runId) return emptyResult();
|
|
233
|
+
const stdin = {
|
|
234
|
+
...this.baseStdinFields("PermissionResult"),
|
|
235
|
+
run_id: runId,
|
|
236
|
+
permission_kind: permissionKind,
|
|
237
|
+
tool_call_id: toolCallId,
|
|
238
|
+
tool_name: toolName,
|
|
239
|
+
decision,
|
|
240
|
+
...toolInput !== void 0 ? { tool_input: toolInput } : {}
|
|
241
|
+
};
|
|
242
|
+
return this.dispatch(hooks, stdin);
|
|
243
|
+
}
|
|
244
|
+
async runInterrupt(reason) {
|
|
245
|
+
const hooks = this.config.Interrupt;
|
|
246
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
247
|
+
const runId = this.runId;
|
|
248
|
+
if (!runId) return emptyResult();
|
|
249
|
+
const stdin = {
|
|
250
|
+
...this.baseStdinFields("Interrupt"),
|
|
251
|
+
run_id: runId,
|
|
252
|
+
reason
|
|
253
|
+
};
|
|
254
|
+
return this.dispatch(hooks, stdin);
|
|
255
|
+
}
|
|
256
|
+
async runSubagentStart(toolCallId, agentType, task, modelId, forked) {
|
|
257
|
+
const hooks = this.config.SubagentStart;
|
|
258
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
259
|
+
const runId = this.runId;
|
|
260
|
+
if (!runId) return emptyResult();
|
|
261
|
+
const stdin = {
|
|
262
|
+
...this.baseStdinFields("SubagentStart"),
|
|
263
|
+
run_id: runId,
|
|
264
|
+
tool_call_id: toolCallId,
|
|
265
|
+
agent_type: agentType,
|
|
266
|
+
task,
|
|
267
|
+
...modelId !== void 0 ? { model_id: modelId } : {},
|
|
268
|
+
...forked !== void 0 ? { forked } : {}
|
|
269
|
+
};
|
|
270
|
+
return this.dispatch(hooks, stdin);
|
|
271
|
+
}
|
|
272
|
+
async runSubagentEnd(toolCallId, agentType, result, isError, durationMs) {
|
|
273
|
+
const hooks = this.config.SubagentEnd;
|
|
274
|
+
if (!hooks || hooks.length === 0) return emptyResult();
|
|
275
|
+
const runId = this.runId;
|
|
276
|
+
if (!runId) return emptyResult();
|
|
277
|
+
const stdin = {
|
|
278
|
+
...this.baseStdinFields("SubagentEnd"),
|
|
279
|
+
run_id: runId,
|
|
280
|
+
tool_call_id: toolCallId,
|
|
281
|
+
agent_type: agentType,
|
|
282
|
+
result,
|
|
283
|
+
is_error: isError,
|
|
284
|
+
duration_ms: durationMs
|
|
285
|
+
};
|
|
286
|
+
return this.dispatch(hooks, stdin);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
export { HookManager };
|