Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions bin/fm-kimi-turnend-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# serializes it: install adds or replaces one marker-delimited Firstmate region,
# and remove excises only that region. Missing, malformed, symlinked, partially
# marked, or otherwise surprising config is refused without a config write.
# Validation uses stdlib tomllib when the active python3 has it, otherwise the
# bundled bin/fm_toml reader, so stock macOS python3 3.9 can still install.
#
# The installed Stop hook always exits 0 and stays silent. It reads cwd from the
# hook payload, checks for a .fm-kimi-turnend pointer before registry work, and
Expand Down Expand Up @@ -34,33 +36,40 @@ if [ -z "${HOME:-}" ]; then
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
printf 'fm-kimi-turnend-hook: refused: python3 with tomllib is required to validate config.toml.\n' >&2
printf 'fm-kimi-turnend-hook: refused: python3 is required to validate config.toml.\n' >&2
exit 1
fi
if [ "$ACTION" = install ] && ! command -v jq >/dev/null 2>&1; then
printf 'fm-kimi-turnend-hook: refused: jq is required by the installed Kimi turn-end hook.\n' >&2
exit 1
fi

python3 - "$ACTION" "$HOME/.kimi-code" <<'PY'
HOOK_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) || exit 1
python3 - "$ACTION" "$HOME/.kimi-code" "$HOOK_DIR" <<'PY'
import os
import re
import shutil
import stat
import sys
import tempfile

ACTION = sys.argv[1]
CONFIG_DIR = sys.argv[2]
HOOK_DIR = sys.argv[3]
try:
import tomllib
except ImportError:
print(
"fm-kimi-turnend-hook: refused: python3 with tomllib is required to validate config.toml.",
file=sys.stderr,
)
raise SystemExit(1)

ACTION = sys.argv[1]
CONFIG_DIR = sys.argv[2]
if HOOK_DIR not in sys.path:
sys.path.insert(0, HOOK_DIR)
try:
import fm_toml as tomllib
except ImportError:
print(
"fm-kimi-turnend-hook: refused: python3 cannot import a TOML reader "
"(stdlib tomllib or bundled fm_toml).",
file=sys.stderr,
)
raise SystemExit(1)
CONFIG = os.path.join(CONFIG_DIR, "config.toml")
HOOK = os.path.join(CONFIG_DIR, "fm-turn-end.sh")
REGISTRY = os.path.join(CONFIG_DIR, "fm-turn-end.d")
Expand Down
11 changes: 11 additions & 0 deletions bin/fm_toml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Firstmate-vendored TOML reader for python3 older than 3.11, which has no
# stdlib tomllib. Source: tomli 2.2.1. Refresh the package together; do not
# edit the parser body except to bump that vendored version.
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
# Licensed to PSF under a Contributor Agreement.

__all__ = ("loads", "load", "TOMLDecodeError")
__version__ = "2.2.1" # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT

from ._parser import TOMLDecodeError, load, loads
Loading
Loading