Repository navigation
Expand file tree
/
Copy pathapply-opencode-session.sh
More file actions
125 lines (117 loc) · 3.33 KB
/
Copy pathapply-opencode-session.sh
File metadata and controls
125 lines (117 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
# DeepSeek Harness patch: send a per-conversation `x-opencode-session` header
# on every OpenCode Go request (OpenCode may reject headerless requests with
# 400 MissingSessionID since 2026-09-06).
#
# Run as a user who can write the target files — often root inside the DSH
# container:
# A. host machine: docker exec -u root <container> bash apply-opencode-session.sh
# B. root shell inside the container: bash apply-opencode-session.sh
# Options: --check (status only) | --revert (restore backups)
#
# Idempotent. Backs up each target next to itself as *.opencode-session-backup
# before the first patch; `node --check` failure auto-restores.
#
# Targets (hardlinked by default, deduped by inode — one write fixes both):
# - the DSH install tree
# - the web profile tree ($DSH_HOME/.dsh/profiles/...)
# Override with:
# DSH_HOME=/home/node DSH_TARGETS="/path/a/index.js /path/b/index.js" bash apply-opencode-session.sh
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PATCHER="$HERE/patch-dsh-llm-pi-ai.mjs"
MARKER="function isOpenCodeTarget(model) {"
DSH_HOME="${DSH_HOME:-/home/node}"
MODE="apply"
case "${1:-}" in
--revert) MODE="revert" ;;
--check) MODE="check" ;;
"") ;;
*) echo "unknown arg: $1 (use --revert / --check)" >&2; exit 1 ;;
esac
if [[ -z "${DSH_TARGETS:-}" ]]; then
DSH_TARGETS=(
"/usr/local/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/dsh-llm-pi-ai/lib/index.js"
"$DSH_HOME/.dsh/profiles/node_modules/@deepseek-ai/dsh-llm-pi-ai/lib/index.js"
)
else
# shellcheck disable=SC2206
DSH_TARGETS=($DSH_TARGETS)
fi
# Dedup by inode (hardlinks): patch each unique file once.
declare -A SEEN
UNIQUE=()
for f in "${DSH_TARGETS[@]}"; do
if [[ ! -f "$f" ]]; then
echo "skip (missing): $f"
continue
fi
inode="$(stat -c '%i' "$f" 2>/dev/null)" || continue
if [[ -n "${SEEN[$inode]:-}" ]]; then
continue
fi
SEEN[$inode]=1
UNIQUE+=("$f")
done
if [[ "$MODE" == "check" ]]; then
for f in "${DSH_TARGETS[@]}"; do
if [[ ! -f "$f" ]]; then
echo "missing : $f"
elif grep -q "$MARKER" "$f"; then
echo "patched : $f"
else
echo "unpatched: $f"
fi
done
exit 0
fi
if [[ "$MODE" == "revert" ]]; then
for f in "${DSH_TARGETS[@]}"; do
bak="${f}.opencode-session-backup"
if [[ -f "$bak" ]]; then
cp "$bak" "$f"
echo "reverted: $f"
else
echo "no backup, skip: $f"
fi
done
echo "done. restart the harness so the module reloads."
exit 0
fi
# apply
FAILED=0
for f in "${UNIQUE[@]}"; do
bak="${f}.opencode-session-backup"
if grep -q "$MARKER" "$f"; then
echo "already patched: $f"
continue
fi
if [[ ! -f "$bak" ]]; then
cp "$f" "$bak"
echo "backup: $bak"
fi
if node "$PATCHER" "$f"; then
if node --check "$f" 2>/dev/null; then
echo "syntax OK: $f"
else
echo "syntax check failed: $f" >&2
cp "$bak" "$f"
echo "restored from backup" >&2
FAILED=1
fi
else
echo "patcher failed on: $f (nothing written)" >&2
FAILED=1
fi
done
echo
if [[ "$FAILED" == "0" ]]; then
echo "next steps:"
echo " 1. restart the harness (docker restart <container> or your service manager);"
echo " the patched module only loads on process start"
echo " 2. optionally remove any static x-opencode-session from settings.yaml:"
echo " the patch's per-conversation id overrides it"
else
echo "some targets failed - see messages above" >&2
exit 1
fi