Open History to see the last commit
1#!/usr/bin/env bash
2
3set -uo pipefail
4
5PORT="${PORT:-8005}"
6SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
7DEFAULT_OPENAI_MODEL="gpt-4o"
8DEFAULT_ANTHROPIC_MODEL="claude-sonnet-4-6"
9
10json_event() {
11 jq -nc "$@"
12}
13
14write_event() {
15 printf 'data: %s\n\n' "$1"
16}
17
18write_done() {
19 printf 'data: [DONE]\n\n'
20}
21
22write_error_response() {
23 local status="$1"
24 local message="$2"
25 printf 'HTTP/1.1 %s\r\n' "$status"
26 printf 'Content-Type: text/plain; charset=utf-8\r\n'
27 printf 'Access-Control-Allow-Origin: *\r\n'
28 printf 'Connection: close\r\n'
29 printf '\r\n'
30 printf '%s' "$message"
31}
32
33normalize_messages() {
34 jq -c '
35 def message_text:
36 if (.parts? | type) == "array" then
37 [.parts[]? |
38 select(
39 type == "object" and
40 .type == "text" and
41 (.content | type) == "string"
42 ) |
43 .content
44 ] | join("")
45 elif (.content? | type) == "string" then
46 .content
47 elif (.content? | type) == "array" then
48 [.content[]? |
49 select(
50 type == "object" and
51 .type == "text" and
52 (.text | type) == "string"
53 ) |
54 .text
55 ] | join("")
56 else
57 ""
58 end;
59
60 [.messages[]? |
61 select(type == "object") |
62 (if .role == "developer" then "system" else (.role // "") end) as $role |
63 message_text as $content |
64 select(
65 ($role == "system" or $role == "user" or $role == "assistant") and
66 ($content | length) > 0
67 ) |
68 { role: $role, content: $content }
69 ]
70 '
71}
72
73process_openai_stream() {
74 local message_id="$1"
75 local line data event
76
77 while IFS= read -r line || [[ -n "$line" ]]; do
78 line="${line%$'\r'}"
79 [[ "$line" == "data: "* ]] || continue
80 data="${line#data: }"
81 [[ "$data" == "[DONE]" ]] && continue
82
83 event="$(
84 jq -c --arg messageId "$message_id" '
85 .choices[0].delta.content? as $delta |
86 select(($delta | type) == "string" and ($delta | length) > 0) |
87 {
88 type: "TEXT_MESSAGE_CONTENT",
89 messageId: $messageId,
90 delta: $delta
91 }
92 ' <<<"$data" 2>/dev/null
93 )" || continue
94
95 [[ -n "$event" ]] && write_event "$event"
96 done
97}
98
99process_anthropic_stream() {
100 local message_id="$1"
101 local line data event
102
103 while IFS= read -r line || [[ -n "$line" ]]; do
104 line="${line%$'\r'}"
105 [[ "$line" == "data: "* ]] || continue
106 data="${line#data: }"
107
108 event="$(
109 jq -c --arg messageId "$message_id" '
110 select(
111 .type == "content_block_delta" and
112 .delta.type == "text_delta" and
113 (.delta.text | type) == "string" and
114 (.delta.text | length) > 0
115 ) |
116 {
117 type: "TEXT_MESSAGE_CONTENT",
118 messageId: $messageId,
119 delta: .delta.text
120 }
121 ' <<<"$data" 2>/dev/null
122 )" || continue
123
124 [[ -n "$event" ]] && write_event "$event"
125 done
126}
127
128stream_openai() {
129 local payload="$1"
130 local message_id="$2"
131
132 curl --silent --show-error --no-buffer --fail \
133 https://api.openai.com/v1/chat/completions \
134 -H "Authorization: Bearer ${OPENAI_API_KEY}" \
135 -H 'Content-Type: application/json' \
136 --data-binary "$payload" |
137 process_openai_stream "$message_id"
138
139 return "${PIPESTATUS[0]}"
140}
141
142stream_anthropic() {
143 local payload="$1"
144 local message_id="$2"
145
146 curl --silent --show-error --no-buffer --fail \
147 https://api.anthropic.com/v1/messages \
148 -H "x-api-key: ${ANTHROPIC_API_KEY}" \
149 -H 'anthropic-version: 2023-06-01' \
150 -H 'Content-Type: application/json' \
151 --data-binary "$payload" |
152 process_anthropic_stream "$message_id"
153
154 return "${PIPESTATUS[0]}"
155}
156
157handle_chat() {
158 local body="$1"
159 local thread_id run_id provider model normalized system chat message_id payload
160 local stream_status
161
162 thread_id="$(jq -r 'if (.threadId? | type) == "string" then .threadId else "" end' <<<"$body")"
163 run_id="$(jq -r 'if (.runId? | type) == "string" then .runId else "" end' <<<"$body")"
164
165 if [[ -z "$thread_id" || -z "$run_id" ]]; then
166 write_error_response '400 Bad Request' 'threadId and runId are required'
167 return
168 fi
169
170 provider="$(
171 jq -r '
172 (.forwardedProps // .data // {}) as $props |
173 if ($props.provider? | type) == "string" and ($props.provider | length) > 0
174 then $props.provider
175 else "openai"
176 end
177 ' <<<"$body"
178 )"
179 model="$(
180 jq -r '
181 (.forwardedProps // .data // {}) as $props |
182 if ($props.model? | type) == "string" then $props.model else "" end
183 ' <<<"$body"
184 )"
185
186 normalized="$(normalize_messages <<<"$body")"
187 system="$(
188 jq -r '[.[] | select(.role == "system") | .content] | join("\n\n")' \
189 <<<"$normalized"
190 )"
191 chat="$(jq -c '[.[] | select(.role == "user" or .role == "assistant")]' <<<"$normalized")"
192
193 printf 'HTTP/1.1 200 OK\r\n'
194 printf 'Content-Type: text/event-stream\r\n'
195 printf 'Cache-Control: no-cache\r\n'
196 printf 'Connection: close\r\n'
197 printf 'Access-Control-Allow-Origin: *\r\n'
198 printf '\r\n'
199
200 message_id="msg-${run_id}"
201 write_event "$(
202 json_event \
203 --arg threadId "$thread_id" \
204 --arg runId "$run_id" \
205 '{ type: "RUN_STARTED", threadId: $threadId, runId: $runId }'
206 )"
207
208 if [[ "$(jq 'length' <<<"$chat")" -eq 0 ]]; then
209 write_event "$(
210 json_event \
211 --arg threadId "$thread_id" \
212 --arg runId "$run_id" \
213 --arg message 'no user or assistant messages to send' \
214 '{
215 type: "RUN_ERROR",
216 threadId: $threadId,
217 runId: $runId,
218 error: { message: $message }
219 }'
220 )"
221 write_done
222 return
223 fi
224
225 write_event "$(
226 json_event \
227 --arg messageId "$message_id" \
228 '{ type: "TEXT_MESSAGE_START", messageId: $messageId, role: "assistant" }'
229 )"
230
231 case "$provider" in
232 openai)
233 if [[ -z "${OPENAI_API_KEY:-}" ]]; then
234 stream_status=1
235 else
236 [[ -n "$model" ]] || model="$DEFAULT_OPENAI_MODEL"
237 payload="$(
238 jq -nc \
239 --arg model "$model" \
240 --arg system "$system" \
241 --argjson messages "$chat" '
242 {
243 model: $model,
244 messages:
245 (if ($system | length) > 0
246 then [{ role: "system", content: $system }] + $messages
247 else $messages
248 end),
249 stream: true
250 }
251 '
252 )"
253 stream_openai "$payload" "$message_id"
254 stream_status=$?
255 fi
256 ;;
257 anthropic)
258 if [[ -z "${ANTHROPIC_API_KEY:-}" ]]; then
259 stream_status=1
260 else
261 [[ -n "$model" ]] || model="$DEFAULT_ANTHROPIC_MODEL"
262 payload="$(
263 jq -nc \
264 --arg model "$model" \
265 --arg system "$system" \
266 --argjson messages "$chat" '
267 {
268 model: $model,
269 max_tokens: 4096,
270 messages: $messages,
271 stream: true
272 } +
273 (if ($system | length) > 0 then { system: $system } else {} end)
274 '
275 )"
276 stream_anthropic "$payload" "$message_id"
277 stream_status=$?
278 fi
279 ;;
280 *)
281 stream_status=1
282 ;;
283 esac
284
285 if [[ "$stream_status" -ne 0 ]]; then
286 local error_message
287 case "$provider" in
288 openai)
289 error_message="${OPENAI_API_KEY:+OpenAI request failed}"
290 error_message="${error_message:-OPENAI_API_KEY is not set}"
291 ;;
292 anthropic)
293 error_message="${ANTHROPIC_API_KEY:+Anthropic request failed}"
294 error_message="${error_message:-ANTHROPIC_API_KEY is not set}"
295 ;;
296 *)
297 error_message="unsupported provider \"${provider}\" (expected openai or anthropic)"
298 ;;
299 esac
300
301 write_event "$(
302 json_event \
303 --arg threadId "$thread_id" \
304 --arg runId "$run_id" \
305 --arg message "$error_message" \
306 '{
307 type: "RUN_ERROR",
308 threadId: $threadId,
309 runId: $runId,
310 error: { message: $message }
311 }'
312 )"
313 write_done
314 return
315 fi
316
317 write_event "$(
318 json_event \
319 --arg messageId "$message_id" \
320 '{ type: "TEXT_MESSAGE_END", messageId: $messageId }'
321 )"
322 write_event "$(
323 json_event \
324 --arg threadId "$thread_id" \
325 --arg runId "$run_id" \
326 '{
327 type: "RUN_FINISHED",
328 threadId: $threadId,
329 runId: $runId,
330 finishReason: "stop"
331 }'
332 )"
333 write_done
334}
335
336handle_connection() {
337 local request_line method target version line header_name header_value
338 local content_length=0 body=''
339
340 IFS= read -r request_line || return
341 request_line="${request_line%$'\r'}"
342 read -r method target version <<<"$request_line"
343
344 while IFS= read -r line; do
345 line="${line%$'\r'}"
346 [[ -z "$line" ]] && break
347
348 header_name="${line%%:*}"
349 header_value="${line#*:}"
350 header_value="${header_value#"${header_value%%[![:space:]]*}"}"
351 if [[ "${header_name,,}" == 'content-length' ]]; then
352 content_length="$header_value"
353 fi
354 done
355
356 if ! [[ "$content_length" =~ ^[0-9]+$ ]]; then
357 write_error_response '400 Bad Request' 'invalid Content-Length'
358 return
359 fi
360
361 if [[ "$content_length" -gt 0 ]]; then
362 body="$(dd bs=1 count="$content_length" 2>/dev/null)"
363 fi
364
365 target="${target%%\?*}"
366
367 if [[ "$method" == 'OPTIONS' ]]; then
368 printf 'HTTP/1.1 204 No Content\r\n'
369 printf 'Access-Control-Allow-Origin: *\r\n'
370 printf 'Access-Control-Allow-Methods: POST, OPTIONS\r\n'
371 printf 'Access-Control-Allow-Headers: Content-Type\r\n'
372 printf 'Connection: close\r\n'
373 printf '\r\n'
374 return
375 fi
376
377 if [[ "$method" == 'GET' && "$target" == '/health' ]]; then
378 printf 'HTTP/1.1 200 OK\r\n'
379 printf 'Content-Type: text/plain; charset=utf-8\r\n'
380 printf 'Content-Length: 2\r\n'
381 printf 'Connection: close\r\n'
382 printf '\r\n'
383 printf 'ok'
384 return
385 fi
386
387 if [[ "$method" != 'POST' ]]; then
388 write_error_response '405 Method Not Allowed' 'method not allowed'
389 return
390 fi
391
392 if [[ "$target" != '/' ]]; then
393 write_error_response '404 Not Found' 'not found'
394 return
395 fi
396
397 if ! jq -e 'type == "object"' >/dev/null 2>&1 <<<"$body"; then
398 write_error_response '400 Bad Request' 'invalid JSON body'
399 return
400 fi
401
402 handle_chat "$body"
403}
404
405check_dependencies() {
406 local dependency
407 for dependency in bash curl jq socat; do
408 if ! command -v "$dependency" >/dev/null 2>&1; then
409 printf '[bash] missing dependency: %s\n' "$dependency" >&2
410 return 1
411 fi
412 done
413}
414
415if [[ "${1:-}" == '--handle' ]]; then
416 handle_connection
417 exit 0
418fi
419
420check_dependencies || exit 1
421printf '[bash] listening on http://127.0.0.1:%s\n' "$PORT"
422exec socat \
423 "TCP-LISTEN:${PORT},bind=127.0.0.1,reuseaddr,fork" \
424 "SYSTEM:exec bash '${SCRIPT_PATH}' --handle"