blob: 99c0b28d78ef8c0bae36a368169d490b4fc36ff7 (
plain)
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
|
#!/usr/bin/env bash
# Copyright 2026 Hee-Suk Kim <[email protected]>
# SPDX-License-Identifier: GPL-3.0-only
set -euo pipefail
. lib.bash
usage() {
cat <<EOF
usage: ${NAME} [-nrch] [-p <chain-ish>] [-t <name>] [-e <endpoint>] [-m <model>]
-e API <endpoint> (PLM_ENDPOINT)
-m Model name (PLM_MODEL)
-p Parent <chain-ish> to chain on (default HEAD)
-t Save the current HEAD as <name> before asking
-n Start new conversation
-r Do not accept or chain input (use with -p)
-c Chain user turn only, no API call done
-h Show this help
EOF
}
parent=HEAD parent_arg= resend= ref= only_chain= noderef=
while getopts he:m:p:t:nrc o; do
case $o in
h) usage; exit 0 ;;
e) export PLM_ENDPOINT="$OPTARG" ;;
m) export PLM_MODEL="$OPTARG" ;;
p) parent="$OPTARG" ;;
t) ref="$OPTARG" ;;
n) parent= ;;
r) resend=1 ;;
c) only_chain=1 ;;
*) usage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
[[ -n $ref ]] && sib save -- "$ref"
# An unborn HEAD has no parent. The first update should detach HEAD
sib rev-parse HEAD &>/dev/null || {
noderef=--no-deref
[[ $parent == HEAD ]] && parent=
}
if [[ -z $parent ]]; then
# -n is set, should detach HEAD to not taint existing ref
noderef=--no-deref
else
tmp=$(sib rev-parse "$parent") || die bad parent: $parent
parent=$tmp
fi
if [[ -z $resend ]]; then
[[ -t 0 ]] && echo '(stdin, Ctrl+D to send)' >&2
# rev-parse prints empty means there is no ref pointed to by
# $parent. In this case, start a new chain and update HEAD.
parent_arg="${parent:+-p $parent}"
parent=$(plm-wrap -f text -t plm | sib chain-trace-json $parent_arg)
fi
[[ -n $parent ]] || die 'refuse to do worthless job with -nr'
reflog=$(sib show -c $parent | tr -s '[:space:]' ' ' | cut -c1-72)
[[ -n $only_chain ]] && {
sib update-ref -m "ask (user): ${reflog:-$parent}" $noderef HEAD $parent
exit
}
{
res=$(sib rev-jsonl $parent \
| plm-wrap -f plm -t request \
| plm-send) || die 'fail to recieve API call'
[[ -n $res ]] || die 'fail to recieve API call'
[[ -z "${SIB_DEBUG:-}" ]] || echo "sib-ask: res: $res" >&2
} 3>&1
new=$(plm-wrap -f response -t plm -r assistant <<<"$res" | sib chain-trace-json -p $parent) ||
die something wrong
[[ -n $new ]] || die "sib chain-trace-json -p $parent gives empty output"
sib update-ref -m "ask: ${reflog:-$new}" $noderef HEAD $new
|