review: streamed tool calls keep their index
A reviewed rule buffers the worker answer (the review path needs a non-stream
body) and turns the completed message back into one SSE delta. A non-streaming
message has no index on its tool_calls; a streaming client keys parallel calls by
it. Splinter put every index-less call into the same slot, so four calls arrived
as one `mcp_tasksd__task_scopes` whose arguments were concatenated JSON objects:
{}{"state":"pending","limit":50}{"state":"running","limit":50}{...}
Every call object in the replayed delta now carries "index":N, so a client sees
four calls with their own ids, names and arguments. The tail copy is bounded by
the message length -- strlen() ran past the message into the rest of the JSON and
duplicated finish_reason in the delta.
tests/review.sh: the tool stub now emits three calls; a streaming request through
a review rule checks that each call has its index and keeps its own name (the
non-stream tool test still passes). tests/run.sh: 539 OK, PASS.
This commit is contained in:
1 parent
479503eae9
commit
11edb7b798
4 files changed
+122
-9
No files matched your search
+59
-1
@@ -538,6 +538,64 @@ static void finish_reason(const char *choice, const char *cend, char *out, size_
|
||||
snprintf(out, cap, "stop");
|
||||
}
|
||||
|
||||
/* Copy a message, adding "index":N to every tool call object. A non-streaming
|
||||
* message has no index; a streaming client keys parallel calls by it. Splinter,
|
||||
* for one, puts index-less calls into the same slot, so several calls arrived as
|
||||
* one whose arguments were concatenated JSON objects ("{}{...}{...}"). */
|
||||
static int
|
||||
append_message_indexed(j_buf *b, const char *msg, size_t len)
|
||||
{
|
||||
const char *end = msg + len;
|
||||
const char *tc = find_top_value(msg, end, "tool_calls");
|
||||
const char *arr, *arr_end, *p;
|
||||
int n = 0;
|
||||
|
||||
if (tc == NULL)
|
||||
return j_buf_append(b, msg, len);
|
||||
arr = skip_ws(tc, end);
|
||||
if (arr >= end || *arr != '[')
|
||||
return j_buf_append(b, msg, len);
|
||||
arr_end = skip_value(arr, end);
|
||||
if (arr_end == NULL || arr_end[-1] != ']')
|
||||
return j_buf_append(b, msg, len);
|
||||
if (j_buf_append(b, msg, (size_t)(arr - msg)) != 0 ||
|
||||
j_buf_append(b, "[", 1) != 0)
|
||||
return -1;
|
||||
p = skip_ws(arr + 1, arr_end);
|
||||
while (p < arr_end - 1) {
|
||||
const char *v_end = skip_value(p, arr_end);
|
||||
char idx[32];
|
||||
int il;
|
||||
|
||||
if (v_end == NULL)
|
||||
return -1;
|
||||
if (*p == '{') {
|
||||
il = snprintf(idx, sizeof idx, "\"index\":%d,", n);
|
||||
if (il < 0 || (size_t)il >= sizeof idx)
|
||||
return -1;
|
||||
if (j_buf_append(b, "{", 1) != 0 ||
|
||||
j_buf_append(b, idx, (size_t)il) != 0 ||
|
||||
j_buf_append(b, p + 1, (size_t)(v_end - p - 1)) != 0)
|
||||
return -1;
|
||||
} else if (j_buf_append(b, p, (size_t)(v_end - p)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
n++;
|
||||
p = skip_ws(v_end, arr_end);
|
||||
if (p < arr_end - 1 && *p == ',') {
|
||||
if (j_buf_append(b, ",", 1) != 0)
|
||||
return -1;
|
||||
p = skip_ws(p + 1, arr_end);
|
||||
} else if (p < arr_end - 1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (j_buf_append(b, "]", 1) != 0 ||
|
||||
j_buf_append(b, arr_end, (size_t)(end - arr_end)) != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int j_review_sse(const char *json, const char *model, const char *meta,
|
||||
char **out, size_t *outlen)
|
||||
{
|
||||
@@ -566,7 +624,7 @@ int j_review_sse(const char *json, const char *model, const char *meta,
|
||||
app_json_string(&b, model && model[0] ? model : "junction") != 0 ||
|
||||
j_buf_append(&b, ",\"choices\":[{\"index\":0,\"delta\":",
|
||||
strlen(",\"choices\":[{\"index\":0,\"delta\":")) != 0 ||
|
||||
j_buf_append(&b, msg, (size_t)(mend - msg)) != 0 ||
|
||||
append_message_indexed(&b, msg, (size_t)(mend - msg)) != 0 ||
|
||||
j_buf_append(&b, ",\"finish_reason\":null}]}\n\n",
|
||||
strlen(",\"finish_reason\":null}]}\n\n")) != 0) {
|
||||
j_buf_free(&b);
|
||||
|
||||
+2
-2
@@ -33,8 +33,8 @@ int j_review_extract(const char *json, char **answer, int *tool_calls,
|
||||
const char **reason);
|
||||
|
||||
/* Turn a completed OpenAI JSON response into the SSE frames the client expects:
|
||||
* one chunk carrying the raw message (text, reasoning, tool_calls), a stop chunk,
|
||||
* and [DONE]. `model` is what the client asked for. */
|
||||
* one chunk carrying the message (text, reasoning, tool_calls with an index added
|
||||
* to each call), a stop chunk, and [DONE]. `model` is what the client asked for. */
|
||||
int j_review_sse(const char *json, const char *model, const char *meta,
|
||||
char **out, size_t *outlen);
|
||||
|
||||
|
||||
+41
-1
@@ -28,7 +28,7 @@ RNTJ=$((PORT+296))
|
||||
STUB_STATE="$TMP/review.state" STUB_ANSWERS="first answer|second answer|third answer" \
|
||||
tests/stub $RW "REVIEWER" > "$TMP/review_stub.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
STUB_STATE="$TMP/review_tools.state" STUB_ANSWERS="tool answer" STUB_TOOLS=1 \
|
||||
STUB_STATE="$TMP/review_tools.state" STUB_ANSWERS="tool answer" STUB_TOOLS=3 \
|
||||
tests/stub $RT "TOOLER" > "$TMP/review_tools_stub.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
STUB_STATE="$TMP/review_bad.state" STUB_ANSWERS="unused" tests/stub $RB "BADREQUEST" --status 400 \
|
||||
@@ -150,6 +150,46 @@ TCALLS=$(tr -d '\n' < "$TMP/review_tools.state")
|
||||
[ "$TCALLS" = "1" ] && ok "no review call was made for the tool-call answer" \
|
||||
|| bad "the tool-call answer caused '$TCALLS' model calls"
|
||||
|
||||
# Ein Streaming-Client bekommt die Calls mit ihren Indizes zurueck. Ein
|
||||
# nicht-streamendes tool_calls-Array hat keine; schiebt ein Client sie zusammen,
|
||||
# sieht er einen Call, dessen Argument mehrere JSON-Objekte sind
|
||||
# ("{}{...}{...}") -- genau das liess splinter an vm-tools scheitern.
|
||||
curl -s -N -m 20 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"vm-tools","stream":true,"messages":[{"role":"user","content":"do it"}],"max_tokens":8,"tools":[{"type":"function","function":{"name":"doit","parameters":{"type":"object"}}}]}' \
|
||||
"http://127.0.0.1:$RJT/v1/chat/completions" > "$TMP/review_tools_sse.txt"
|
||||
if python3 - "$TMP/review_tools_sse.txt" <<'PY'
|
||||
import json, sys
|
||||
calls = {}
|
||||
done = False
|
||||
for line in open(sys.argv[1], errors="replace"):
|
||||
line = line.strip()
|
||||
if not line.startswith("data: "):
|
||||
continue
|
||||
payload = line[6:]
|
||||
if payload == "[DONE]":
|
||||
done = True
|
||||
continue
|
||||
try:
|
||||
event = json.loads(payload)
|
||||
except Exception:
|
||||
continue
|
||||
for choice in event.get("choices", []):
|
||||
for call in (choice.get("delta") or {}).get("tool_calls") or []:
|
||||
index = call.get("index")
|
||||
if index is None:
|
||||
raise SystemExit("tool call without index: %r" % (call,))
|
||||
fn = call.get("function") or {}
|
||||
calls[index] = (fn.get("name"), fn.get("arguments"))
|
||||
want = {0: ("doit1", "{}"), 1: ("doit2", "{}"), 2: ("doit3", "{}")}
|
||||
if not done:
|
||||
raise SystemExit("stream did not end with [DONE]")
|
||||
if calls != want:
|
||||
raise SystemExit("tool calls merged or renamed: %r" % (calls,))
|
||||
PY
|
||||
then ok "streamed tool calls keep their index and their own names"
|
||||
else bad "streamed tool calls were merged or lost their index"
|
||||
fi
|
||||
|
||||
# /v1/models und /status kennen das virtuelle Modell und die Rundenzahl.
|
||||
M=$(curl -s -m 10 "http://127.0.0.1:$RJ/v1/models")
|
||||
contains "$M" '"id":"vm-reviewed"' "the reviewed virtual model is listed"
|
||||
|
||||
+20
-5
@@ -459,15 +459,30 @@ int main(int argc, char **argv)
|
||||
"Connection: close\r\n\r\n%s", strlen(b), name, body_len, accept, b);
|
||||
} else {
|
||||
char b[4096];
|
||||
if (tools_env && *tools_env)
|
||||
if (tools_env && *tools_env) {
|
||||
long ntools = strtol(tools_env, NULL, 10);
|
||||
char calls[2048];
|
||||
size_t co = 0;
|
||||
|
||||
if (ntools < 1) ntools = 1;
|
||||
if (ntools > 8) ntools = 8;
|
||||
calls[0] = '\0';
|
||||
for (long t = 0; t < ntools; t++) {
|
||||
int w = snprintf(calls + co, sizeof calls - co,
|
||||
"%s{\"id\":\"call_%ld\",\"type\":\"function\","
|
||||
"\"function\":{\"name\":\"doit%ld\","
|
||||
"\"arguments\":\"{}\"}}",
|
||||
t ? "," : "", t + 1, t + 1);
|
||||
if (w < 0 || (size_t)w >= sizeof calls - co) break;
|
||||
co += (size_t)w;
|
||||
}
|
||||
snprintf(b, sizeof b,
|
||||
"{\"id\":\"stub\",\"model\":\"%s\",\"choices\":[{\"index\":0,"
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":null,"
|
||||
"\"tool_calls\":[{\"id\":\"call_1\",\"type\":\"function\","
|
||||
"\"function\":{\"name\":\"doit\",\"arguments\":\"{}\"}}]},"
|
||||
"\"tool_calls\":[%s]},"
|
||||
"\"finish_reason\":\"tool_calls\"}]}",
|
||||
name);
|
||||
else {
|
||||
name, calls);
|
||||
} else {
|
||||
char default_answer[256];
|
||||
snprintf(default_answer, sizeof default_answer, "served by %s", name);
|
||||
const char *ans = selected_answer[0] ? selected_answer : default_answer;
|
||||
|
||||
Reference in new issue
Block a user