Splinter can now talk to a real endpoint: --endpoint plus --model shapes the whole conversation into one chat/completions request and parses the reply back. The seam changed from a raw string to the conversation, because a provider adapter cannot invent history and the loop must not depend on vendor JSON: - sp_conv: owned messages (role, name, call_id, content, raw tool_calls), geometric growth, trim from the front. Copy on push, so a compaction step must produce a new array - which is what makes it reviewable. - sp_openai_request(): conversation + tool table -> JSON body. - sp_openai_reply(): choices[0].message -> content + calls, and it keeps the provider's raw tool_calls array to hand back verbatim next round. - provider error objects, empty choice lists, non-JSON bodies and empty bodies each produce a distinct, quoted error instead of a silent empty answer. - transport_openai: curl, body through a temp file, status from curl's -w, key from the environment only. - libmcp's JSON parser is vendored and instantiated exactly once (src/json.c); two instantiations would be a link error rather than a silent duplicate. Proven end to end against a local stub speaking the real wire format: request 1 (1 message, 2 tools, 448 bytes) -> tool_call for echo; splinter ran the tool, request 2 (3 messages, 682 bytes) -> final text; the session log holds user, observation, assistant. Nine test binaries green, including test_openai (request shape, tool_call parsing, raw array kept, four failing reply shapes) and test_conv.
89 lines
2.1 KiB
C
89 lines
2.1 KiB
C
/* splinter -- conversation tests. */
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_conv c;
|
|
struct sp_msg m;
|
|
|
|
sp_conv_init(&c);
|
|
CHECK(c.n == 0);
|
|
CHECK(sp_conv_bytes(&c) == 0);
|
|
|
|
CHECK(sp_conv_push(&c, "user", NULL, "hallo") == 0);
|
|
CHECK(sp_conv_push(&c, "assistant", NULL, "hi") == 0);
|
|
CHECK(c.n == 2);
|
|
CHECK(strcmp(c.v[0].role, "user") == 0);
|
|
CHECK(strcmp(c.v[0].content, "hallo") == 0);
|
|
CHECK(c.v[0].name == NULL);
|
|
CHECK(c.v[0].call_id == NULL);
|
|
|
|
/* a tool message carries name and call id */
|
|
memset(&m, 0, sizeof(m));
|
|
m.role = "tool";
|
|
m.name = "echo";
|
|
m.call_id = "call-7";
|
|
m.content = "pong";
|
|
CHECK(sp_conv_add(&c, &m) == 0);
|
|
CHECK(c.n == 3);
|
|
CHECK(strcmp(c.v[2].name, "echo") == 0);
|
|
CHECK(strcmp(c.v[2].call_id, "call-7") == 0);
|
|
|
|
/* an assistant turn that asked for tools keeps the provider's raw array */
|
|
memset(&m, 0, sizeof(m));
|
|
m.role = "assistant";
|
|
m.content = "";
|
|
m.calls = "[{\"id\":\"call-7\",\"type\":\"function\",\"function\":"
|
|
"{\"name\":\"echo\",\"arguments\":\"x\"}}]";
|
|
CHECK(sp_conv_add(&c, &m) == 0);
|
|
CHECK(c.v[3].calls != NULL);
|
|
|
|
/* the copy is a copy: changing the source must not change the record */
|
|
{
|
|
char buf[16];
|
|
|
|
snprintf(buf, sizeof(buf), "hallo");
|
|
buf[0] = 'H';
|
|
CHECK(strcmp(c.v[0].content, "hallo") == 0);
|
|
}
|
|
|
|
/* a message without a role is refused */
|
|
memset(&m, 0, sizeof(m));
|
|
m.content = "x";
|
|
CHECK(sp_conv_add(&c, &m) == -1);
|
|
CHECK(c.n == 4);
|
|
|
|
/* trimming drops from the front and keeps the tail */
|
|
CHECK(sp_conv_trim(&c, 2) == 0);
|
|
CHECK(c.n == 2);
|
|
/* the last two of user, assistant, tool, assistant-with-calls: tool first */
|
|
CHECK(strcmp(c.v[0].role, "tool") == 0);
|
|
CHECK(strcmp(c.v[1].role, "assistant") == 0);
|
|
CHECK(sp_conv_trim(&c, 10) == 0);
|
|
CHECK(c.n == 2);
|
|
|
|
/* bytes grow with the content */
|
|
CHECK(sp_conv_bytes(&c) > 0);
|
|
|
|
sp_conv_free(&c);
|
|
CHECK(c.n == 0);
|
|
CHECK(c.v == NULL);
|
|
|
|
if (failures == 0)
|
|
printf("test_conv: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|