claude

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package claude provides Claude Code integration templates and utilities.

It embeds hook scripts, slash command definitions, and configuration types for integrating ctx with Claude Code's settings.local.json. The embedded assets are installed to project directories via "ctx init --claude".

Embedded assets:

  • auto-save-session.sh: Saves session transcripts on session end
  • block-non-path-ctx.sh: Prevents non-PATH ctx invocations
  • tpl/commands/*.md: Slash command definitions for Claude Code

Example usage:

script, err := claude.GetAutoSaveScript()
if err != nil {
    return err
}
os.WriteFile(".claude/hooks/auto-save-session.sh", script, 0755)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDefaultPermissions added in v0.1.2

func CreateDefaultPermissions() []string

CreateDefaultPermissions returns the default permissions for ctx commands.

These permissions allow Claude Code to run ctx CLI commands without prompting for approval. All ctx subcommands are pre-approved.

Returns:

  • []string: List of permission patterns for ctx commands

func GetAutoSaveScript

func GetAutoSaveScript() ([]byte, error)

GetAutoSaveScript returns the auto-save session script content.

The script automatically saves Claude Code session transcripts when a session ends. It is installed to .claude/hooks/ during ctx init --claude.

Returns:

  • []byte: Raw bytes of the auto-save-session.sh script
  • error: Non-nil if the embedded file cannot be read

func GetBlockNonPathCtxScript

func GetBlockNonPathCtxScript() ([]byte, error)

GetBlockNonPathCtxScript returns the script that blocks non-PATH ctx invocations.

The script prevents Claude from running ctx via relative paths (./ctx, ./dist/ctx) or "go run", ensuring only the installed PATH version is used. It is installed to .claude/hooks/ during ctx init --claude.

Returns:

  • []byte: Raw bytes of the block-non-path-ctx.sh script
  • error: Non-nil if the embedded file cannot be read

func GetCommand

func GetCommand(name string) ([]byte, error)

GetCommand returns the content of a command file by name.

Parameters:

  • name: Filename as returned by ListCommands (e.g., "ctx-status.md")

Returns:

  • []byte: Raw bytes of the command definition file
  • error: Non-nil if the command file does not exist or cannot be read

func ListCommands

func ListCommands() ([]string, error)

ListCommands returns the list of embedded command file names.

These are Claude Code slash command definitions (e.g., "ctx-status.md", "ctx-reflect.md") from internal/templates/claude/commands/. They can be installed to .claude/commands/ via "ctx init".

Returns:

  • []string: Filenames of available command definitions
  • error: Non-nil if the commands directory cannot be read

Types

type Hook

type Hook struct {
	Type    string `json:"type"`
	Command string `json:"command"`
}

Hook represents a single hook command to execute.

Fields:

  • Type: Hook type, typically "command"
  • Command: Shell command or script path to execute

type HookConfig

type HookConfig struct {
	PreToolUse []HookMatcher `json:"PreToolUse,omitempty"`
	SessionEnd []HookMatcher `json:"SessionEnd,omitempty"`
}

HookConfig represents the hooks section of Claude Code's settings.local.json.

Hooks are shell commands that Claude Code executes at specific lifecycle events. See https://docs.anthropic.com/en/docs/claude-code/hooks for details.

Fields:

  • PreToolUse: Matchers that run before each tool invocation
  • SessionEnd: Matchers that run when a session ends

func CreateDefaultHooks

func CreateDefaultHooks(projectDir string) HookConfig

CreateDefaultHooks returns the default ctx hooks configuration for Claude Code.

The returned hooks configure PreToolUse to block non-PATH ctx invocations and auto-load context on every tool use, and SessionEnd to run auto-save-session.sh for persisting session transcripts.

Parameters:

  • projectDir: Project root directory for absolute hook paths; if empty, paths are relative (e.g., ".claude/hooks/")

Returns:

  • HookConfig: Configured hooks for PreToolUse and SessionEnd events

type HookMatcher

type HookMatcher struct {
	Matcher string `json:"matcher,omitempty"`
	Hooks   []Hook `json:"hooks"`
}

HookMatcher associates a regex pattern with hooks to execute.

For PreToolUse hooks, the Matcher pattern matches against the tool name (e.g., "Bash", "Read"). Use ".*" to match all tools.

Fields:

  • Matcher: Regex pattern to match; empty string matches all
  • Hooks: Commands to execute when the pattern matches

type PermissionsConfig added in v0.1.2

type PermissionsConfig struct {
	Allow []string `json:"allow,omitempty"`
}

PermissionsConfig represents the permissions section of Claude Code's settings.local.json.

Fields:

  • Allow: List of tool patterns that are pre-approved (e.g., "Bash(ctx status:*)")

type Settings

type Settings struct {
	Hooks       HookConfig        `json:"hooks,omitempty"`
	Permissions PermissionsConfig `json:"permissions,omitempty"`
}

Settings represents the full Claude Code settings.local.json structure.

This is used when reading or writing project-level Claude Code configuration.

Fields:

  • Hooks: Hook configuration for lifecycle events
  • Permissions: Tool permission configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL