Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ CHANNEL_WEBCHAT_O := $(if $(filter 1,$(GATEWAY)),src/channels/webchat.o,)
STATIC_O := $(if $(filter 1,$(GATEWAY)),src/gateway/static.o,)
HTTP_O := $(if $(filter 1,$(GATEWAY)),src/gateway/http.o,)
HTTP_LWS_O := $(if $(filter 1,$(GATEWAY)),src/gateway/http_lws.o,)
ROUTES_WEBCHAT_O := $(if $(filter 1,$(GATEWAY)),src/gateway/routes_webchat.o,)
ROUTES_O := $(if $(filter 1,$(GATEWAY)),src/gateway/routes.o,)
ROUTES_HARDWARE_O := $(if $(filter 1,$(GATEWAY)),src/gateway/routes_hardware.o,)
WS_O := $(if $(filter 1,$(GATEWAY)),src/gateway/ws.o,)
Expand Down Expand Up @@ -173,7 +174,7 @@ OBJS := $(CORE_OBJS) $(VENDOR_OBJS)
PROVIDER_OBJS := $(PROVIDER_COMMON_O) $(STUB_O) $(ROUTER_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O)
CHANNEL_OBJS := $(CHANNEL_COMMON_O) $(CHANNEL_CLI_O) $(CHANNEL_TG_O) $(CHANNEL_DISCORD_O) $(DISCORD_HELPERS_O) $(CHANNEL_HEARTBEAT_O) $(CHANNEL_WEBCHAT_O)
ASAP_HTTP_BODY_O := $(if $(filter 1,$(GATEWAY)),src/gateway/asap_http_body.o,)
GATEWAY_OBJS := $(AUTH_O) $(STATIC_O) $(HTTP_O) $(HTTP_LWS_O) $(ASAP_HTTP_BODY_O) $(ROUTES_O) $(ROUTES_HARDWARE_O) $(WS_O) $(RATE_LIMIT_O)
GATEWAY_OBJS := $(AUTH_O) $(STATIC_O) $(HTTP_O) $(HTTP_LWS_O) $(ASAP_HTTP_BODY_O) $(ROUTES_O) $(ROUTES_HARDWARE_O) $(ROUTES_WEBCHAT_O) $(WS_O) $(RATE_LIMIT_O)
GATEWAY_OBJS += $(if $(filter 1,$(GATEWAY)),$(WS_RX_O))
TOOL_OBJS := $(SHELL_O) $(WEBSEARCH_O) $(FILE_O) $(REGISTRY_O) $(CONTEXT_O) $(CONTEXT_CACHE_O) $(CONTEXT_HTTP_O) $(CONTEXT_GEO_O) $(CRYPTO_LINK) $(HARDWARE_STUB_O) $(HARDWARE_INIT_O) $(HARDWARE_GPIO_SNAPSHOT_O) $(HARDWARE_TEGRASTATS_O) $(HARDWARE_TOOLS_O) $(BOARD_DETECT_O) $(HARDWARE_I2C_O) $(HARDWARE_CAMERA_O) $(HARDWARE_LIBGPIOD_O) $(CRON_O) $(ASAP_INVOKE_O)
ASAP_OBJS := $(MANIFEST_O) $(MANIFEST_PROFILES_O) $(MANIFEST_BUILD_O) $(MANIFEST_SIGN_O) $(MANIFEST_KEYS_O) $(JCS_O) $(ENVELOPE_O) $(ULID_O) $(CLIENT_O) $(ASAP_REGISTRY_O) $(SERVER_O) $(ASAP_LOG_O)
Expand Down Expand Up @@ -227,7 +228,7 @@ $(RELOAD_CHANNEL_STUB_O): tests/stubs/reload_channel_stub.c src/channels/channel
$(HTTP_RELOAD_STUB_O): tests/stubs/http_reload_stub.c src/gateway/http.h src/core/config.h
$(CC) $(CFLAGS) $(INC) -c -o $@ tests/stubs/http_reload_stub.c

$(DISPATCH_O): src/core/dispatch.c src/core/dispatch.h src/core/agent.h src/core/bootstrap.h src/core/memory.h src/channels/channel.h src/channels/heartbeat.h src/tools/cron.h
$(DISPATCH_O): src/core/dispatch.c src/channels/webchat.h src/core/dispatch.h src/core/agent.h src/core/bootstrap.h src/core/memory.h src/channels/channel.h src/channels/heartbeat.h src/tools/cron.h
$(CC) $(CFLAGS) $(INC) -c -o $@ src/core/dispatch.c

$(TOML_O): vendor/tomlc99/toml.c vendor/tomlc99/toml.h
Expand Down Expand Up @@ -309,7 +310,7 @@ $(HEARTBEAT_TEST_O): src/channels/heartbeat.c src/channels/heartbeat.h src/chann
@mkdir -p $(BINDIR)
$(CC) $(CFLAGS) $(INC) -DSHELLCLAW_TEST -c -o $@ src/channels/heartbeat.c

$(CHANNEL_WEBCHAT_O): src/channels/webchat.c src/channels/channel.h src/channels/webchat.h src/core/config.h
$(CHANNEL_WEBCHAT_O): src/channels/webchat.c src/channels/channel.h src/channels/webchat.h src/core/config.h src/core/memory.h src/crypto/crypto.h src/gateway/ws.h
$(CC) $(CFLAGS) $(INC) $(GATEWAY_CFLAGS) -c -o $@ src/channels/webchat.c

$(AUTH_O): src/gateway/auth.c src/gateway/auth.h src/core/config.h src/crypto/crypto.h
Expand Down Expand Up @@ -373,13 +374,16 @@ $(ASAP_REGISTRY_TEST_O): src/asap/registry.c src/asap/registry.h src/asap/client
$(RATE_LIMIT_O): src/gateway/rate_limit.c src/gateway/rate_limit.h
$(CC) $(CFLAGS) $(INC) -pthread -c -o $@ src/gateway/rate_limit.c

$(HTTP_O): src/gateway/http.c src/gateway/http.h src/gateway/http_lws.h src/gateway/ws.h src/gateway/ws_rx.h src/providers/provider.h
$(HTTP_O): src/gateway/http.c src/core/memory.h src/gateway/http.h src/gateway/http_lws.h src/gateway/ws.h src/gateway/ws_rx.h src/providers/provider.h
$(CC) $(CFLAGS) $(INC) $(GATEWAY_CFLAGS) -pthread -c -o $@ src/gateway/http.c

$(HTTP_LWS_O): src/gateway/http_lws.c src/gateway/http_lws.h src/gateway/asap_http_body.h src/gateway/routes.h src/gateway/auth.h src/gateway/static.h src/gateway/ws.h src/gateway/ws_rx.h
$(HTTP_LWS_O): src/gateway/http_lws.c src/gateway/routes_webchat.h src/gateway/http_lws.h src/gateway/asap_http_body.h src/gateway/routes.h src/gateway/auth.h src/gateway/static.h src/gateway/ws.h src/gateway/ws_rx.h
$(CC) $(CFLAGS) $(INC) $(GATEWAY_CFLAGS) -pthread -c -o $@ src/gateway/http_lws.c

$(ROUTES_O): src/gateway/routes.c src/gateway/routes.h src/gateway/routes_hardware.h src/gateway/http.h src/gateway/http_lws.h src/gateway/auth.h src/gateway/rate_limit.h src/tools/context.h src/asap/manifest.h src/asap/envelope.h src/asap/server.h src/asap/log.h src/core/bootstrap.h src/core/agent.h src/core/config.h src/core/config_patch.h src/core/dispatch.h src/core/reload.h src/core/memory.h src/core/skill.h src/providers/provider.h src/channels/channel.h src/tools/cron.h src/tools/tool.h
$(ROUTES_WEBCHAT_O): src/gateway/routes_webchat.c src/gateway/routes_webchat.h src/gateway/routes.h src/channels/webchat.h src/core/memory.h src/vendor/tweetnacl/tweetnacl.h
$(CC) $(CFLAGS) $(INC) $(GATEWAY_CFLAGS) -c -o $@ $<

$(ROUTES_O): src/gateway/routes.c src/gateway/routes_webchat.h src/gateway/routes.h src/gateway/routes_hardware.h src/gateway/http.h src/gateway/http_lws.h src/gateway/auth.h src/gateway/rate_limit.h src/tools/context.h src/asap/manifest.h src/asap/envelope.h src/asap/server.h src/asap/log.h src/core/bootstrap.h src/core/agent.h src/core/config.h src/core/config_patch.h src/core/dispatch.h src/core/reload.h src/core/memory.h src/core/skill.h src/providers/provider.h src/channels/channel.h src/tools/cron.h src/tools/tool.h
$(CC) $(CFLAGS) $(INC) $(GATEWAY_CFLAGS) -pthread -c -o $@ src/gateway/routes.c

$(ROUTES_HARDWARE_O): src/gateway/routes_hardware.c src/gateway/routes_hardware.h src/gateway/routes.h src/gateway/http_lws.h src/gateway/uri_match.h src/hardware/hardware.h src/hardware/hardware_gpio_snapshot.h src/hardware/hardware_tegrastats.h src/hardware/board_detect.h src/core/config.h
Expand Down Expand Up @@ -748,13 +752,13 @@ test_asap_invoke: tests/test_asap_invoke.c $(ASAP_INVOKE_TEST_O) $(ASAP_REGISTRY
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -DSHELLCLAW_ASAP_INVOKE_TEST -DSHELLCLAW_REGISTRY_TEST -o $(BINDIR)/$@ tests/test_asap_invoke.c $(ASAP_INVOKE_TEST_O) $(ASAP_REGISTRY_TEST_O) $(CLIENT_O) $(ENVELOPE_O) $(ULID_O) $(CRYPTO_LINK) $(CJSON_O) $(PROVIDER_COMMON_O) $(CONFIG_O) $(TOML_O) $(LDLIBS)
$(DSYM_SCRIPT)

test_gateway_http: shellclaw tests/test_gateway_http.c $(AUTH_O) $(CONFIG_O) $(TOML_O) $(CJSON_O)
test_gateway_http: shellclaw tests/test_gateway_http.c $(AUTH_O) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(SQLITE3_O)
@if [ "$(GATEWAY)" != "1" ]; then \
if [ "$(CI)" = "true" ]; then echo "test_gateway_http: GATEWAY=0 in CI — install libwebsockets-dev"; exit 1; fi; \
echo "test_gateway_http: skipped (GATEWAY=0)"; exit 0; \
fi
@mkdir -p $(BINDIR)
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -DSHELLCLAW_GATEWAY -pthread -o $(BINDIR)/$@ tests/test_gateway_http.c $(AUTH_O) $(CRYPTO_LINK) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(LDLIBS) -pthread
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -DSHELLCLAW_GATEWAY -pthread -o $(BINDIR)/$@ tests/test_gateway_http.c $(AUTH_O) $(CRYPTO_LINK) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(SQLITE3_O) $(LDLIBS) -pthread
$(DSYM_SCRIPT)

GATEWAY_DB_LOCK_OBJS := $(filter-out src/core/main.o,$(SHELLCLAW_OBJS))
Expand Down Expand Up @@ -945,7 +949,7 @@ clean-root-dsym:
@rm -f shellclaw test_agent test_anthropic test_channel test_cli test_config test_file test_memory test_local_provider test_openai test_provider test_router test_shell test_skill test_telegram test_web_search test_ws

clean: clean-root-dsym
rm -f $(OBJS) $(PROVIDER_COMMON_O) $(STUB_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O) $(ROUTER_O) $(CJSON_O) $(TWEETNACL_O) $(ANTHROPIC_TEST_O) $(OPENAI_TEST_O) $(LOCAL_TEST_O) $(CONTEXT_TEST_OBJS) $(HEARTBEAT_TEST_O) $(CHANNEL_TG_TEST_O) $(CHANNEL_COMMON_O) $(CHANNEL_STUB_O) $(CHANNEL_CLI_O) $(CHANNEL_TG_O) $(CHANNEL_DISCORD_O) $(DISCORD_HELPERS_O) $(CHANNEL_HEARTBEAT_O) $(CHANNEL_WEBCHAT_O) $(AUTH_O) $(STATIC_O) $(HTTP_O) $(HTTP_LWS_O) $(ASAP_HTTP_BODY_O) $(ROUTES_O) $(ROUTES_HARDWARE_O) $(WS_O) $(MANIFEST_O) $(MANIFEST_PROFILES_O) $(MANIFEST_BUILD_O) $(MANIFEST_SIGN_O) $(MANIFEST_KEYS_O) $(ENVELOPE_O) $(ULID_O) $(CLIENT_O) $(ASAP_REGISTRY_O) $(SERVER_O) $(ASAP_LOG_O) $(RATE_LIMIT_O) $(SHELL_O) $(WEBSEARCH_O) $(FILE_O) $(REGISTRY_O) $(CONTEXT_O) $(CONTEXT_CACHE_O) $(CONTEXT_HTTP_O) $(CONTEXT_GEO_O) $(CRYPTO_O) $(JCS_O) $(HARDWARE_STUB_O) $(HARDWARE_INIT_O) $(HARDWARE_GPIO_SNAPSHOT_O) $(HARDWARE_TEGRASTATS_O) $(HARDWARE_TOOLS_O) $(BOARD_DETECT_O) src/hardware/hardware_libgpiod.o $(HARDWARE_I2C_O) $(HARDWARE_CAMERA_O) $(CRON_O) $(ASAP_INVOKE_O) $(SANDBOX_CORE_O) $(SANDBOX_LANDLOCK_O) $(SANDBOX_ENV_O) $(ALLOWLIST_SCAN_O) $(ALLOWLIST_PATH_O)
rm -f $(OBJS) $(PROVIDER_COMMON_O) $(STUB_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O) $(ROUTER_O) $(CJSON_O) $(TWEETNACL_O) $(ANTHROPIC_TEST_O) $(OPENAI_TEST_O) $(LOCAL_TEST_O) $(CONTEXT_TEST_OBJS) $(HEARTBEAT_TEST_O) $(CHANNEL_TG_TEST_O) $(CHANNEL_COMMON_O) $(CHANNEL_STUB_O) $(CHANNEL_CLI_O) $(CHANNEL_TG_O) $(CHANNEL_DISCORD_O) $(DISCORD_HELPERS_O) $(CHANNEL_HEARTBEAT_O) $(CHANNEL_WEBCHAT_O) $(AUTH_O) $(STATIC_O) $(HTTP_O) $(HTTP_LWS_O) $(ASAP_HTTP_BODY_O) $(ROUTES_O) $(ROUTES_HARDWARE_O) $(ROUTES_WEBCHAT_O) $(WS_O) $(MANIFEST_O) $(MANIFEST_PROFILES_O) $(MANIFEST_BUILD_O) $(MANIFEST_SIGN_O) $(MANIFEST_KEYS_O) $(ENVELOPE_O) $(ULID_O) $(CLIENT_O) $(ASAP_REGISTRY_O) $(SERVER_O) $(ASAP_LOG_O) $(RATE_LIMIT_O) $(SHELL_O) $(WEBSEARCH_O) $(FILE_O) $(REGISTRY_O) $(CONTEXT_O) $(CONTEXT_CACHE_O) $(CONTEXT_HTTP_O) $(CONTEXT_GEO_O) $(CRYPTO_O) $(JCS_O) $(HARDWARE_STUB_O) $(HARDWARE_INIT_O) $(HARDWARE_GPIO_SNAPSHOT_O) $(HARDWARE_TEGRASTATS_O) $(HARDWARE_TOOLS_O) $(BOARD_DETECT_O) src/hardware/hardware_libgpiod.o $(HARDWARE_I2C_O) $(HARDWARE_CAMERA_O) $(CRON_O) $(ASAP_INVOKE_O) $(SANDBOX_CORE_O) $(SANDBOX_LANDLOCK_O) $(SANDBOX_ENV_O) $(ALLOWLIST_SCAN_O) $(ALLOWLIST_PATH_O)
rm -f src/gateway/ui_assets.h $(WS_RX_O) $(BINDIR)/test_ws_rx
find . -name '*.gcno' -o -name '*.gcda' -o -name '*.gcov' | xargs rm -f 2>/dev/null || true
rm -f $(WS_TEST_O) $(SANDBOX_TEST_O) $(BINDIR)/asap_registry_test.o $(BINDIR)/asap_invoke_test.o $(CONTEXT_TEST_OBJS) $(HEARTBEAT_TEST_O) $(BINDIR)/shellclaw $(BINDIR)/test_tweetnacl_smoke $(BINDIR)/test_config $(BINDIR)/test_config_patch $(BINDIR)/test_memory $(BINDIR)/test_skill $(BINDIR)/test_provider $(BINDIR)/test_anthropic $(BINDIR)/test_openai $(BINDIR)/test_local_provider $(BINDIR)/test_router $(BINDIR)/test_heartbeat $(BINDIR)/test_crypto $(BINDIR)/test_hardware_stub $(BINDIR)/test_board_detect $(BINDIR)/test_hardware_libgpiod $(BINDIR)/test_hardware_i2c $(BINDIR)/test_hardware_camera $(BINDIR)/test_pin_tables $(BINDIR)/test_hardware_init $(BINDIR)/test_hardware_tools $(BINDIR)/test_registry $(BINDIR)/test_ws $(BINDIR)/test_agent $(BINDIR)/test_channel $(BINDIR)/test_cli $(BINDIR)/test_shell $(BINDIR)/test_file $(BINDIR)/test_telegram $(BINDIR)/test_discord_helpers $(BINDIR)/test_web_search $(BINDIR)/test_cron $(BINDIR)/test_context $(BINDIR)/test_manifest_build $(BINDIR)/test_manifest_keys $(BINDIR)/test_jcs $(BINDIR)/test_asap_envelope $(BINDIR)/test_asap_ulid $(BINDIR)/test_asap_client $(BINDIR)/test_asap_registry $(BINDIR)/test_asap_server $(BINDIR)/test_asap_invoke $(BINDIR)/test_asap_log $(BINDIR)/test_auth $(BINDIR)/test_gateway_http $(BINDIR)/test_gateway_db_lock $(BINDIR)/test_static $(BINDIR)/test_sandbox $(BINDIR)/test_allowlist $(BINDIR)/test_rate_limit
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for PR workflow, coding standards, and th

**Jetson install (v1.0):** `./scripts/install.sh`, `./scripts/build_llama_jetson.sh`, `./scripts/download_model.sh phi3` — details in [`docs/HARDWARE_JETSON.md`](docs/HARDWARE_JETSON.md) and [`docs/LOCAL_INFERENCE.md`](docs/LOCAL_INFERENCE.md).

**Pair another device.** On the gateway host, confirm that the new device needs access.
Send `SIGUSR1` to the running gateway with `kill -USR1 <pid>`.
For the systemd user service, run `systemctl --user kill --kill-whom=main --signal=SIGUSR1 shellclaw.service`.
Read the new code in the gateway console or service journal.
Enter the code on the Pair page within five minutes. The code expires after one use or a gateway restart.
The gateway keeps existing devices paired and accepts up to 16 devices.

**WebSocket authentication.** Pair with the gateway before opening `/ws`. For a browser client, offer both `ws` and `bearer.<pairing-token>` as subprotocols. The gateway selects `ws` and reads the token from the other entry. For a native client, offer `ws` and send the token in `Authorization: Bearer <pairing-token>`.

**Debug (macOS):** Symbols in `tests-dSYM/`. Use `lldb build/test_agent` then `settings set target.debug-file-search-path tests-dSYM`. Old `.dSYM` in repo root? Run `make clean-root-dsym`.
Expand Down
4 changes: 4 additions & 0 deletions src/channels/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define SHELLCLAW_CHANNEL_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -32,6 +33,7 @@ typedef struct channel_incoming_msg {
char *text; /**< Message text (may be empty for photo-only) */
channel_attachment_t *attachments;
size_t attachments_count;
int64_t turn_seq; /**< Persistent turn sequence, or 0 if absent. */
} channel_incoming_msg_t;

/** Clear and free incoming message fields. Safe to call repeatedly or on zeroed. */
Expand Down Expand Up @@ -122,6 +124,8 @@ typedef struct channel {
const channel_attachment_t *attachments, size_t att_count);
/** Release channel resources. */
void (*cleanup)(void);
/** Optional. Reply to a specific incoming message. Return 0 on success. */
int (*reply)(const channel_incoming_msg_t *msg, const char *text);
} channel_t;

#define MAX_REGISTERED_CHANNELS 8
Expand Down
1 change: 1 addition & 0 deletions src/channels/channel_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void channel_incoming_msg_clear(channel_incoming_msg_t *msg)
msg->user_id = NULL;
free(msg->text);
msg->text = NULL;
msg->turn_seq = 0;
if (msg->attachments) {
for (size_t i = 0; i < msg->attachments_count; i++)
free(msg->attachments[i].path_or_base64);
Expand Down
88 changes: 81 additions & 7 deletions src/channels/webchat.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
/**
* @file webchat.c
* @brief WebChat channel: poll from WS queue, send to WS connection.
* @brief Process WebChat turns and messages from native WebSocket clients.
*/
#define _POSIX_C_SOURCE 200809L

#include "channels/channel.h"
#include "channels/webchat.h"
#include "gateway/ws.h"
#include "core/config.h"
#include "core/memory.h"
#include "crypto/crypto.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WEBCHAT_USER_ID "webchat"
#define NATIVE_SESSION_PREFIX "webchat:socket:"
#define WS_SESSION_PREFIX "webchat:"

static char g_native_prefix[sizeof(NATIVE_SESSION_PREFIX) + 17];
static int g_prefer_socket;
static int64_t g_pending_seq;
static char g_pending_reply[WS_TEXT_BUF_SIZE];

static int webchat_flush_reply(void)
{
if (!g_pending_seq) return 0;
int rc = webchat_store_finish(g_pending_seq, g_pending_reply);
if (rc >= 0) {
g_pending_seq = 0;
g_pending_reply[0] = '\0';
}
return rc;
}

static int webchat_complete_turn(int64_t seq, const char *reply)
{
size_t size = strlen(reply) + 1;
if (g_pending_seq || size > sizeof(g_pending_reply)) return -1;
g_pending_seq = seq;
memcpy(g_pending_reply, reply, size);
int rc = webchat_flush_reply();
if (rc < 0)
fprintf(stderr, "Cannot save the WebChat reply. The gateway will retry.\n");
return rc;
}

static int webchat_init(const config_t *cfg)
{
(void)cfg;
uint64_t id;
if (crypto_read_urandom(&id, sizeof(id)) != 0) return -1;
snprintf(g_native_prefix, sizeof(g_native_prefix), NATIVE_SESSION_PREFIX "%016" PRIx64 ":", id);
g_prefer_socket = 0;
return 0;
}

Expand All @@ -25,18 +62,36 @@ static int webchat_poll(channel_incoming_msg_t *out, int timeout_ms)
if (!out) return -1;
memset(out, 0, sizeof(*out));
char session_id[128];
char socket_session[32];
char text[WS_TEXT_BUF_SIZE];
int r = ws_pop_incoming(session_id, sizeof(session_id), text, sizeof(text), timeout_ms);
if (r != 1) return r;
int64_t seq = 0;
int r = 0;
if (g_prefer_socket)
r = ws_pop_incoming(socket_session, sizeof(socket_session), text, sizeof(text), 0);
/* Hold at most one calculated reply while its write is pending. */
if (r == 0 && webchat_flush_reply() >= 0)
r = webchat_store_claim(&seq, session_id, sizeof(session_id), text, sizeof(text));
if (r != 1) {
if (r < 0) fprintf(stderr, "Cannot read the WebChat queue.\n");
r = ws_pop_incoming(socket_session, sizeof(socket_session), text, sizeof(text), timeout_ms);
if (r != 1) return r;
}
if (!seq) {
/* Connection numbers can repeat after restart. Give each run its own prefix. */
snprintf(session_id, sizeof(session_id), "%s%s", g_native_prefix,
socket_session + sizeof(WS_SESSION_PREFIX) - 1);
}
g_prefer_socket = seq != 0;
out->session_id = strdup(session_id);
out->user_id = strdup(WEBCHAT_USER_ID);
out->text = strdup(text);
out->attachments = NULL;
out->attachments_count = 0;
out->turn_seq = seq;
if (!out->session_id || !out->user_id || !out->text) {
free(out->session_id);
free(out->user_id);
free(out->text);
if (seq)
webchat_complete_turn(seq, "Cannot process the message.");
channel_incoming_msg_clear(out);
return -1;
}
return 1;
Expand All @@ -48,11 +103,29 @@ static int webchat_send(const char *recipient, const char *text,
(void)attachments;
(void)att_count;
if (!recipient || !text) return -1;
return ws_send_to(recipient, text);
size_t prefix_size = strlen(g_native_prefix);
if (!prefix_size || strncmp(recipient, g_native_prefix, prefix_size) != 0)
return -1;
char socket_session[32];
int n = snprintf(socket_session, sizeof(socket_session), WS_SESSION_PREFIX "%s",
recipient + prefix_size);
if (n < 0 || (size_t)n >= sizeof(socket_session)) return -1;
return ws_send_to(socket_session, text);
}

static int webchat_reply(const channel_incoming_msg_t *msg, const char *text)
{
if (msg->turn_seq)
return webchat_complete_turn(msg->turn_seq, text);
return webchat_send(msg->session_id, text, NULL, 0);
}

static void webchat_cleanup(void)
{
if (webchat_flush_reply() < 0)
fprintf(stderr, "Cannot save the WebChat reply before shutdown.\n");
g_pending_seq = 0;
memset(g_pending_reply, 0, sizeof(g_pending_reply));
}

static const channel_t webchat_channel = {
Expand All @@ -61,6 +134,7 @@ static const channel_t webchat_channel = {
.poll = webchat_poll,
.send = webchat_send,
.cleanup = webchat_cleanup,
.reply = webchat_reply,
};

const channel_t *channel_webchat_get(void)
Expand Down
6 changes: 4 additions & 2 deletions src/channels/webchat.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file webchat.h
* @brief WebChat channel: WebSocket-based channel for agent loop.
* @brief WebChat channel for paired devices and native WebSocket clients.
*/

#ifndef SHELLCLAW_CHANNEL_WEBCHAT_H
Expand All @@ -12,7 +12,9 @@
extern "C" {
#endif

/** WebChat channel: poll returns from WS queue, send pushes to WS. */
#define WEBCHAT_DEVICE_PREFIX "webchat:device:"

/** Return the channel for paired devices and native WebSocket clients. */
const channel_t *channel_webchat_get(void);

#ifdef __cplusplus
Expand Down
Loading
Loading