diff options
Diffstat (limited to 'hook_inject_context.sh')
| -rwxr-xr-x | hook_inject_context.sh | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/hook_inject_context.sh b/hook_inject_context.sh new file mode 100755 index 0000000..6937a14 --- /dev/null +++ b/hook_inject_context.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# UserPromptSubmit hook: 从 broker 拉取长消息注入 context +# 每次 dispatcher 提交 prompt 时触发,检查有没有待注入的长消息 + +export PATH="/usr/bin:/bin:$PATH" +cd "$(dirname "$0")" +source .env 2>/dev/null + +# 拉取 pending context +RESP=$(curl -sf -H "Authorization: Bearer $API_SECRET" \ + "${BROKER_URL:-http://127.0.0.1:8000}/context/pending" 2>/dev/null) || exit 0 + +# 解析消息 +CONTEXT=$(echo "$RESP" | /usr/bin/python3 -c " +import sys, json +d = json.load(sys.stdin) +msgs = d.get('messages', []) +if not msgs: + sys.exit(1) +print('\n---\n'.join(msgs)) +" 2>/dev/null) || exit 0 + +# 有消息则注入 additionalContext +if [ -n "$CONTEXT" ]; then + /usr/bin/python3 -c " +import json, sys +ctx = sys.stdin.read() +print(json.dumps({ + 'hookSpecificOutput': { + 'hookEventName': 'UserPromptSubmit', + 'additionalContext': ctx + } +})) +" <<< "$CONTEXT" +fi + +exit 0 |
