@nordbyte/nordrelay 0.8.1 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +9 -0
- package/README.md +84 -1205
- package/dist/{access-control.js → access/access-control.js} +1 -1
- package/dist/{audit-log.js → access/audit-log.js} +32 -15
- package/dist/{session-locks.js → access/session-locks.js} +1 -1
- package/dist/{user-management.js → access/user-management.js} +1 -1
- package/dist/{claude-code-cli.js → agents/claude-code/claude-code-cli.js} +2 -2
- package/dist/{claude-code-session.js → agents/claude-code/claude-code-session.js} +1 -1
- package/dist/{codex-cli.js → agents/codex/codex-cli.js} +14 -5
- package/dist/{codex-session.js → agents/codex/codex-session.js} +2 -4
- package/dist/{hermes-cli.js → agents/hermes/hermes-cli.js} +2 -2
- package/dist/{hermes-launch.js → agents/hermes/hermes-launch.js} +1 -1
- package/dist/{hermes-session.js → agents/hermes/hermes-session.js} +1 -1
- package/dist/{openclaw-cli.js → agents/openclaw/openclaw-cli.js} +2 -2
- package/dist/{openclaw-launch.js → agents/openclaw/openclaw-launch.js} +1 -1
- package/dist/{openclaw-session.js → agents/openclaw/openclaw-session.js} +1 -1
- package/dist/{pi-cli.js → agents/pi/pi-cli.js} +2 -2
- package/dist/{pi-launch.js → agents/pi/pi-launch.js} +1 -1
- package/dist/{pi-session.js → agents/pi/pi-session.js} +1 -1
- package/dist/{adapter-conformance.js → agents/shared/adapter-conformance.js} +2 -2
- package/dist/{agent-activity.js → agents/shared/agent-activity.js} +5 -5
- package/dist/agents/shared/agent-auth-commands.js +30 -0
- package/dist/{agent-factory.js → agents/shared/agent-factory.js} +5 -5
- package/dist/{agent-feature-matrix.js → agents/shared/agent-feature-matrix.js} +2 -2
- package/dist/{agent-updates.js → agents/shared/agent-updates.js} +7 -7
- package/dist/{discord-artifacts.js → channels/discord/discord-artifacts.js} +4 -4
- package/dist/{discord-bot.js → channels/discord/discord-bot.js} +176 -451
- package/dist/{discord-channel-runtime.js → channels/discord/discord-channel-runtime.js} +2 -2
- package/dist/{discord-command-surface.js → channels/discord/discord-command-surface.js} +3 -3
- package/dist/{bot-rendering.js → channels/shared/bot-rendering.js} +6 -6
- package/dist/{channel-actions.js → channels/shared/channel-actions.js} +4 -4
- package/dist/channels/shared/channel-bridge-controller.js +69 -0
- package/dist/channels/shared/channel-cli-artifacts.js +51 -0
- package/dist/{channel-command-service.js → channels/shared/channel-command-service.js} +51 -28
- package/dist/channels/shared/channel-external-mirror-controller.js +193 -0
- package/dist/channels/shared/channel-external-monitor.js +52 -0
- package/dist/{channel-mirror-registry.js → channels/shared/channel-mirror-registry.js} +14 -6
- package/dist/{channel-peer-prompt.js → channels/shared/channel-peer-prompt.js} +3 -3
- package/dist/channels/shared/channel-prompt-queue.js +37 -0
- package/dist/{channel-turn-service.js → channels/shared/channel-turn-service.js} +25 -11
- package/dist/{context-key.js → channels/shared/context-key.js} +1 -1
- package/dist/{session-format.js → channels/shared/session-format.js} +2 -2
- package/dist/{slack-artifacts.js → channels/slack/slack-artifacts.js} +4 -4
- package/dist/{slack-bot.js → channels/slack/slack-bot.js} +171 -309
- package/dist/{slack-channel-runtime.js → channels/slack/slack-channel-runtime.js} +2 -2
- package/dist/{slack-command-surface.js → channels/slack/slack-command-surface.js} +2 -2
- package/dist/{slack-diagnostics.js → channels/slack/slack-diagnostics.js} +2 -2
- package/dist/{bot-ui.js → channels/telegram/bot-ui.js} +1 -1
- package/dist/{bot.js → channels/telegram/bot.js} +195 -430
- package/dist/{telegram-access-commands.js → channels/telegram/telegram-access-commands.js} +3 -3
- package/dist/{telegram-access-middleware.js → channels/telegram/telegram-access-middleware.js} +4 -4
- package/dist/{telegram-agent-commands.js → channels/telegram/telegram-agent-commands.js} +9 -9
- package/dist/{telegram-artifact-commands.js → channels/telegram/telegram-artifact-commands.js} +4 -4
- package/dist/{telegram-channel-runtime.js → channels/telegram/telegram-channel-runtime.js} +2 -2
- package/dist/{telegram-command-menu.js → channels/telegram/telegram-command-menu.js} +1 -1
- package/dist/{telegram-diagnostics-command.js → channels/telegram/telegram-diagnostics-command.js} +7 -7
- package/dist/{telegram-general-commands.js → channels/telegram/telegram-general-commands.js} +4 -4
- package/dist/{telegram-operational-commands.js → channels/telegram/telegram-operational-commands.js} +5 -5
- package/dist/{telegram-output.js → channels/telegram/telegram-output.js} +2 -2
- package/dist/{telegram-preference-commands.js → channels/telegram/telegram-preference-commands.js} +3 -3
- package/dist/{telegram-queue-commands.js → channels/telegram/telegram-queue-commands.js} +6 -6
- package/dist/{telegram-support-command.js → channels/telegram/telegram-support-command.js} +4 -4
- package/dist/{telegram-update-commands.js → channels/telegram/telegram-update-commands.js} +5 -5
- package/dist/{config-metadata.js → core/config-metadata.js} +8 -0
- package/dist/{config.js → core/config.js} +11 -3
- package/dist/core/pagination.js +22 -0
- package/dist/index.js +27 -23
- package/dist/peers/peer-discovery-jobs.js +206 -0
- package/dist/peers/peer-discovery.js +223 -0
- package/dist/peers/peer-health-monitor.js +49 -0
- package/dist/{peer-identity.js → peers/peer-identity.js} +50 -1
- package/dist/{peer-runtime-service.js → peers/peer-runtime-service.js} +29 -7
- package/dist/{peer-server.js → peers/peer-server.js} +3 -2
- package/dist/{peer-store.js → peers/peer-store.js} +96 -9
- package/dist/{peer-types.js → peers/peer-types.js} +28 -0
- package/dist/peers/peer-web-proxy-contract.js +129 -0
- package/dist/{metrics.js → runtime/metrics.js} +5 -3
- package/dist/{relay-artifact-service.js → runtime/relay-artifact-service.js} +1 -1
- package/dist/runtime/relay-auth-service.js +63 -0
- package/dist/runtime/relay-dashboard-service.js +139 -0
- package/dist/{relay-external-activity-monitor.js → runtime/relay-external-activity-monitor.js} +155 -53
- package/dist/{relay-queue-service.js → runtime/relay-queue-service.js} +1 -0
- package/dist/runtime/relay-runtime-active-sessions.js +387 -0
- package/dist/runtime/relay-runtime-dashboard.js +204 -0
- package/dist/{relay-runtime-helpers.js → runtime/relay-runtime-helpers.js} +3 -0
- package/dist/runtime/relay-runtime-prompt-queue-artifacts.js +311 -0
- package/dist/runtime/relay-runtime-sessions.js +631 -0
- package/dist/runtime/relay-runtime-trace.js +92 -0
- package/dist/runtime/relay-runtime-types.js +1 -0
- package/dist/runtime/relay-runtime-updates-jobs.js +366 -0
- package/dist/runtime/relay-runtime.js +461 -0
- package/dist/runtime/runtime-cache.js +117 -0
- package/dist/{prompt-store.js → state/prompt-store.js} +13 -1
- package/dist/{session-registry.js → state/session-registry.js} +3 -3
- package/dist/{operations.js → support/operations.js} +7 -7
- package/dist/{support-bundle.js → support/support-bundle.js} +1 -1
- package/dist/{web-api-contract.js → web/web-api-contract.js} +19 -3
- package/dist/web/web-api-types.js +1 -0
- package/dist/{web-dashboard-access-routes.js → web/web-dashboard-access-routes.js} +17 -14
- package/dist/{web-dashboard-artifact-routes.js → web/web-dashboard-artifact-routes.js} +6 -2
- package/dist/{web-dashboard-assets.js → web/web-dashboard-assets.js} +25 -2
- package/dist/{web-dashboard-http.js → web/web-dashboard-http.js} +41 -5
- package/dist/{web-dashboard-pages.js → web/web-dashboard-pages.js} +95 -30
- package/dist/{web-dashboard-peer-routes.js → web/web-dashboard-peer-routes.js} +121 -7
- package/dist/{web-dashboard-runtime-routes.js → web/web-dashboard-runtime-routes.js} +8 -1
- package/dist/web/web-dashboard-security.js +14 -0
- package/dist/{web-dashboard-session-routes.js → web/web-dashboard-session-routes.js} +29 -13
- package/dist/web/web-dashboard-ui.js +56 -0
- package/dist/{web-dashboard.js → web/web-dashboard.js} +132 -48
- package/dist/web/web-performance.js +62 -0
- package/dist/web/web-rate-limit.js +19 -0
- package/dist/{web-state.js → web/web-state.js} +107 -9
- package/dist/webui-assets/dashboard.css +398 -49
- package/dist/webui-assets/dashboard.js +1239 -103
- package/dist/webui-assets/favicon.ico +0 -0
- package/dist/webui-assets/favicon.png +0 -0
- package/dist/webui-assets/logo.png +0 -0
- package/package.json +6 -3
- package/plugins/nordrelay/scripts/nordrelay.mjs +346 -12
- package/plugins/nordrelay/scripts/service-installer.mjs +183 -0
- package/{launchd/start.sh → scripts/launchd-start.sh} +1 -1
- package/scripts/postinstall.mjs +122 -0
- package/dist/relay-runtime.js +0 -1916
- package/dist/runtime-cache.js +0 -57
- package/dist/web-dashboard-ui.js +0 -20
- /package/dist/{user-management-crypto.js → access/user-management-crypto.js} +0 -0
- /package/dist/{user-management-normalize.js → access/user-management-normalize.js} +0 -0
- /package/dist/{user-management-types.js → access/user-management-types.js} +0 -0
- /package/dist/{claude-code-auth.js → agents/claude-code/claude-code-auth.js} +0 -0
- /package/dist/{claude-code-launch.js → agents/claude-code/claude-code-launch.js} +0 -0
- /package/dist/{claude-code-state.js → agents/claude-code/claude-code-state.js} +0 -0
- /package/dist/{codex-auth.js → agents/codex/codex-auth.js} +0 -0
- /package/dist/{codex-config.js → agents/codex/codex-config.js} +0 -0
- /package/dist/{codex-launch.js → agents/codex/codex-launch.js} +0 -0
- /package/dist/{codex-state.js → agents/codex/codex-state.js} +0 -0
- /package/dist/{hermes-api.js → agents/hermes/hermes-api.js} +0 -0
- /package/dist/{hermes-auth.js → agents/hermes/hermes-auth.js} +0 -0
- /package/dist/{hermes-state.js → agents/hermes/hermes-state.js} +0 -0
- /package/dist/{openclaw-auth.js → agents/openclaw/openclaw-auth.js} +0 -0
- /package/dist/{openclaw-gateway.js → agents/openclaw/openclaw-gateway.js} +0 -0
- /package/dist/{openclaw-state.js → agents/openclaw/openclaw-state.js} +0 -0
- /package/dist/{pi-auth.js → agents/pi/pi-auth.js} +0 -0
- /package/dist/{pi-rpc.js → agents/pi/pi-rpc.js} +0 -0
- /package/dist/{pi-state.js → agents/pi/pi-state.js} +0 -0
- /package/dist/{agent-adapter.js → agents/shared/agent-adapter.js} +0 -0
- /package/dist/{agent.js → agents/shared/agent.js} +0 -0
- /package/dist/{artifacts.js → artifacts/artifacts.js} +0 -0
- /package/dist/{attachments.js → artifacts/attachments.js} +0 -0
- /package/dist/{voice.js → artifacts/voice.js} +0 -0
- /package/dist/{discord-rate-limit.js → channels/discord/discord-rate-limit.js} +0 -0
- /package/dist/{channel-adapter.js → channels/shared/channel-adapter.js} +0 -0
- /package/dist/{relay-runtime-types.js → channels/shared/channel-bridge-state.js} +0 -0
- /package/dist/{channel-command-catalog.js → channels/shared/channel-command-catalog.js} +0 -0
- /package/dist/{channel-command-core.js → channels/shared/channel-command-core.js} +0 -0
- /package/dist/{channel-prompt-engine.js → channels/shared/channel-prompt-engine.js} +0 -0
- /package/dist/{channel-runtime.js → channels/shared/channel-runtime.js} +0 -0
- /package/dist/{channel-turn-lifecycle.js → channels/shared/channel-turn-lifecycle.js} +0 -0
- /package/dist/{slack-rate-limit.js → channels/slack/slack-rate-limit.js} +0 -0
- /package/dist/{telegram-command-types.js → channels/telegram/telegram-command-types.js} +0 -0
- /package/dist/{telegram-rate-limit.js → channels/telegram/telegram-rate-limit.js} +0 -0
- /package/dist/{activity-events.js → core/activity-events.js} +0 -0
- /package/dist/{error-messages.js → core/error-messages.js} +0 -0
- /package/dist/{format.js → core/format.js} +0 -0
- /package/dist/{logger.js → core/logger.js} +0 -0
- /package/dist/{redaction.js → core/redaction.js} +0 -0
- /package/dist/{settings-service.js → core/settings-service.js} +0 -0
- /package/dist/{settings-wizard-test.js → core/settings-wizard-test.js} +0 -0
- /package/dist/{workspace-policy.js → core/workspace-policy.js} +0 -0
- /package/dist/{peer-auth.js → peers/peer-auth.js} +0 -0
- /package/dist/{peer-client.js → peers/peer-client.js} +0 -0
- /package/dist/{peer-context.js → peers/peer-context.js} +0 -0
- /package/dist/{peer-readiness.js → peers/peer-readiness.js} +0 -0
- /package/dist/{web-api-types.js → runtime/relay-runtime-delegate.js} +0 -0
- /package/dist/{remote-prompt.js → runtime/remote-prompt.js} +0 -0
- /package/dist/{bot-preferences.js → state/bot-preferences.js} +0 -0
- /package/dist/{job-store.js → state/job-store.js} +0 -0
- /package/dist/{persistence.js → state/persistence.js} +0 -0
- /package/dist/{state-backend.js → state/state-backend.js} +0 -0
- /package/dist/{zip-writer.js → support/zip-writer.js} +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const recent = [];
|
|
2
|
+
const routeMetrics = new Map();
|
|
3
|
+
const MAX_RECENT = 200;
|
|
4
|
+
export function recordWebApiMetric(sample) {
|
|
5
|
+
const next = {
|
|
6
|
+
...sample,
|
|
7
|
+
durationMs: Math.max(0, Math.round(sample.durationMs)),
|
|
8
|
+
at: sample.at ?? new Date().toISOString(),
|
|
9
|
+
};
|
|
10
|
+
recent.push(next);
|
|
11
|
+
if (recent.length > MAX_RECENT) {
|
|
12
|
+
recent.splice(0, recent.length - MAX_RECENT);
|
|
13
|
+
}
|
|
14
|
+
const key = `${next.method} ${routeKey(next.path)}`;
|
|
15
|
+
const existing = routeMetrics.get(key);
|
|
16
|
+
if (!existing) {
|
|
17
|
+
routeMetrics.set(key, {
|
|
18
|
+
method: next.method,
|
|
19
|
+
path: routeKey(next.path),
|
|
20
|
+
count: 1,
|
|
21
|
+
averageMs: next.durationMs,
|
|
22
|
+
maxMs: next.durationMs,
|
|
23
|
+
lastMs: next.durationMs,
|
|
24
|
+
lastStatusCode: next.statusCode,
|
|
25
|
+
lastAt: next.at,
|
|
26
|
+
totalMs: next.durationMs,
|
|
27
|
+
});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
existing.count += 1;
|
|
31
|
+
existing.totalMs += next.durationMs;
|
|
32
|
+
existing.averageMs = Math.round(existing.totalMs / existing.count);
|
|
33
|
+
existing.maxMs = Math.max(existing.maxMs, next.durationMs);
|
|
34
|
+
existing.lastMs = next.durationMs;
|
|
35
|
+
existing.lastStatusCode = next.statusCode;
|
|
36
|
+
existing.lastAt = next.at;
|
|
37
|
+
}
|
|
38
|
+
export function getWebApiPerformanceMetrics() {
|
|
39
|
+
return {
|
|
40
|
+
recent: [...recent].reverse().slice(0, 25),
|
|
41
|
+
slowest: [...recent].sort((left, right) => right.durationMs - left.durationMs).slice(0, 10),
|
|
42
|
+
routes: [...routeMetrics.values()]
|
|
43
|
+
.map(({ totalMs: _totalMs, ...metric }) => ({ ...metric }))
|
|
44
|
+
.sort((left, right) => right.averageMs - left.averageMs)
|
|
45
|
+
.slice(0, 25),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function routeKey(path) {
|
|
49
|
+
return path
|
|
50
|
+
.replace(/\/api\/peers\/[^/]+\/proxy$/, "/api/peers/:id/proxy")
|
|
51
|
+
.replace(/\/api\/peers\/[^/]+\/events$/, "/api/peers/:id/events")
|
|
52
|
+
.replace(/\/api\/peers\/[^/]+\/health$/, "/api/peers/:id/health")
|
|
53
|
+
.replace(/\/api\/peers\/[^/]+\/repin$/, "/api/peers/:id/repin")
|
|
54
|
+
.replace(/\/api\/peers\/[^/]+\/rotate$/, "/api/peers/:id/rotate")
|
|
55
|
+
.replace(/\/api\/agent-update\/[^/]+\/(log|input|cancel)$/, "/api/agent-update/:id/$1")
|
|
56
|
+
.replace(/\/api\/jobs\/[^/]+\/(log|action)$/, "/api/jobs/:id/$1")
|
|
57
|
+
.replace(/\/api\/trace$/, "/api/trace")
|
|
58
|
+
.replace(/\/api\/users\/[^/]+\/sessions\/[^/]+$/, "/api/users/:id/sessions/:sessionId")
|
|
59
|
+
.replace(/\/api\/users\/[^/]+\/(password|telegram|discord|slack|sessions)$/, "/api/users/:id/$1")
|
|
60
|
+
.replace(/\/api\/peers\/discovery-jobs\/[^/]+\/(cancel|log)$/, "/api/peers/discovery-jobs/:id/$1")
|
|
61
|
+
.replace(/\/api\/peers\/discovery-jobs\/[^/]+$/, "/api/peers/discovery-jobs/:id");
|
|
62
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function consumeRateLimit(buckets, key, maxAttempts, windowMs, blockMs, now = Date.now()) {
|
|
2
|
+
const existing = buckets.get(key);
|
|
3
|
+
if (existing?.blockedUntil && existing.blockedUntil > now) {
|
|
4
|
+
return { limited: true, retryAfterMs: existing.blockedUntil - now };
|
|
5
|
+
}
|
|
6
|
+
if (!existing || existing.resetAt <= now) {
|
|
7
|
+
buckets.set(key, { count: 1, resetAt: now + windowMs });
|
|
8
|
+
return { limited: false };
|
|
9
|
+
}
|
|
10
|
+
existing.count += 1;
|
|
11
|
+
if (existing.count > maxAttempts) {
|
|
12
|
+
existing.blockedUntil = now + blockMs;
|
|
13
|
+
return { limited: true, retryAfterMs: blockMs };
|
|
14
|
+
}
|
|
15
|
+
return { limited: false };
|
|
16
|
+
}
|
|
17
|
+
export function resetRateLimit(buckets, key) {
|
|
18
|
+
buckets.delete(key);
|
|
19
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { activityActorLabel, activityCategoryForType, } from "
|
|
3
|
-
import {
|
|
2
|
+
import { activityActorLabel, activityCategoryForType, } from "../core/activity-events.js";
|
|
3
|
+
import { cursorPage, normalizeCursorLimit } from "../core/pagination.js";
|
|
4
|
+
import { createDocumentStore } from "../state/state-backend.js";
|
|
4
5
|
const DEFAULT_CHAT_LIMIT = 300;
|
|
5
6
|
const DEFAULT_ACTIVITY_LIMIT = 1000;
|
|
6
7
|
export class WebChatStore {
|
|
@@ -16,9 +17,16 @@ export class WebChatStore {
|
|
|
16
17
|
this.maxMessages = maxMessages;
|
|
17
18
|
}
|
|
18
19
|
append(input) {
|
|
20
|
+
return this.appendWithResult(input).message;
|
|
21
|
+
}
|
|
22
|
+
appendWithResult(input) {
|
|
19
23
|
const payload = this.readPayload();
|
|
20
24
|
const threadId = input.threadId || "pending";
|
|
21
25
|
const messages = payload.messagesByThread[threadId] ?? [];
|
|
26
|
+
const duplicate = findDuplicateWebChatMessage(messages, { ...input, threadId });
|
|
27
|
+
if (duplicate) {
|
|
28
|
+
return { message: duplicate, inserted: false };
|
|
29
|
+
}
|
|
22
30
|
const message = {
|
|
23
31
|
id: randomId(),
|
|
24
32
|
timestamp: input.timestamp ?? new Date().toISOString(),
|
|
@@ -31,12 +39,54 @@ export class WebChatStore {
|
|
|
31
39
|
}
|
|
32
40
|
payload.messagesByThread[threadId] = messages;
|
|
33
41
|
this.store.write(payload);
|
|
34
|
-
return message;
|
|
42
|
+
return { message, inserted: true };
|
|
43
|
+
}
|
|
44
|
+
upsertByKey(input) {
|
|
45
|
+
const payload = this.readPayload();
|
|
46
|
+
const threadId = input.threadId || "pending";
|
|
47
|
+
const messages = payload.messagesByThread[threadId] ?? [];
|
|
48
|
+
const now = new Date().toISOString();
|
|
49
|
+
const existing = messages.find((message) => message.key === input.key);
|
|
50
|
+
if (existing) {
|
|
51
|
+
existing.role = input.role;
|
|
52
|
+
existing.text = input.text;
|
|
53
|
+
existing.source = input.source;
|
|
54
|
+
existing.correlationId = input.correlationId;
|
|
55
|
+
existing.turnId = input.turnId;
|
|
56
|
+
existing.timestamp = input.timestamp ?? now;
|
|
57
|
+
existing.key = input.key;
|
|
58
|
+
this.store.write(payload);
|
|
59
|
+
return { message: existing, inserted: false, updated: true };
|
|
60
|
+
}
|
|
61
|
+
const message = {
|
|
62
|
+
id: randomId(),
|
|
63
|
+
timestamp: input.timestamp ?? now,
|
|
64
|
+
...input,
|
|
65
|
+
threadId,
|
|
66
|
+
};
|
|
67
|
+
messages.push(message);
|
|
68
|
+
if (messages.length > this.maxMessages) {
|
|
69
|
+
messages.splice(0, messages.length - this.maxMessages);
|
|
70
|
+
}
|
|
71
|
+
payload.messagesByThread[threadId] = messages;
|
|
72
|
+
this.store.write(payload);
|
|
73
|
+
return { message, inserted: true, updated: false };
|
|
35
74
|
}
|
|
36
75
|
list(threadId, limit = 200) {
|
|
37
76
|
const messages = this.readPayload().messagesByThread[threadId || "pending"] ?? [];
|
|
38
77
|
return messages.slice(-Math.max(1, Math.min(this.maxMessages, limit)));
|
|
39
78
|
}
|
|
79
|
+
findByCorrelationId(correlationId, limit = 100) {
|
|
80
|
+
const needle = correlationId.trim();
|
|
81
|
+
if (!needle) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
return Object.values(this.readPayload().messagesByThread)
|
|
85
|
+
.flat()
|
|
86
|
+
.filter((message) => message.correlationId === needle)
|
|
87
|
+
.sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp))
|
|
88
|
+
.slice(-Math.max(1, Math.min(this.maxMessages, limit)));
|
|
89
|
+
}
|
|
40
90
|
clear(threadId) {
|
|
41
91
|
const payload = this.readPayload();
|
|
42
92
|
const key = threadId || "pending";
|
|
@@ -53,7 +103,7 @@ export class WebChatStore {
|
|
|
53
103
|
const messagesByThread = {};
|
|
54
104
|
for (const [threadId, messages] of Object.entries(payload.messagesByThread)) {
|
|
55
105
|
if (Array.isArray(messages)) {
|
|
56
|
-
messagesByThread[threadId] = messages.filter(isWebChatMessage).slice(-this.maxMessages);
|
|
106
|
+
messagesByThread[threadId] = dedupeWebChatMessages(messages.filter(isWebChatMessage)).slice(-this.maxMessages);
|
|
57
107
|
}
|
|
58
108
|
}
|
|
59
109
|
return { version: 1, messagesByThread };
|
|
@@ -87,7 +137,25 @@ export class WebActivityStore {
|
|
|
87
137
|
return event;
|
|
88
138
|
}
|
|
89
139
|
list(options = {}) {
|
|
90
|
-
|
|
140
|
+
return this.listPage(options).items;
|
|
141
|
+
}
|
|
142
|
+
listPage(options = {}) {
|
|
143
|
+
const limit = normalizeCursorLimit(options.limit, 100, 500);
|
|
144
|
+
const events = this.filteredEvents(options)
|
|
145
|
+
.sort((left, right) => Date.parse(right.timestamp) - Date.parse(left.timestamp));
|
|
146
|
+
return cursorPage(events, options.cursor, limit, (event) => event.id);
|
|
147
|
+
}
|
|
148
|
+
findByCorrelationId(correlationId, limit = 100) {
|
|
149
|
+
const needle = correlationId.trim();
|
|
150
|
+
if (!needle) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
return this.readPayload().events
|
|
154
|
+
.filter((event) => event.correlationId === needle)
|
|
155
|
+
.sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp))
|
|
156
|
+
.slice(-Math.max(1, Math.min(500, limit)));
|
|
157
|
+
}
|
|
158
|
+
filteredEvents(options) {
|
|
91
159
|
const since = normalizeSince(options.since);
|
|
92
160
|
return this.readPayload().events
|
|
93
161
|
.filter((event) => !options.source || options.source === "all" || event.source === options.source)
|
|
@@ -98,9 +166,7 @@ export class WebActivityStore {
|
|
|
98
166
|
.filter((event) => !options.workspace || event.workspace === options.workspace)
|
|
99
167
|
.filter((event) => !options.type || event.type.toLowerCase().includes(options.type.toLowerCase()))
|
|
100
168
|
.filter((event) => !options.actor || activityActorMatches(event.actor, options.actor))
|
|
101
|
-
.filter((event) => !since || Date.parse(event.timestamp) >= since)
|
|
102
|
-
.slice(-limit)
|
|
103
|
-
.reverse();
|
|
169
|
+
.filter((event) => !since || Date.parse(event.timestamp) >= since);
|
|
104
170
|
}
|
|
105
171
|
readPayload() {
|
|
106
172
|
const payload = this.store.read();
|
|
@@ -122,6 +188,7 @@ function isWebChatMessage(value) {
|
|
|
122
188
|
typeof candidate.threadId === "string" &&
|
|
123
189
|
typeof candidate.text === "string" &&
|
|
124
190
|
typeof candidate.timestamp === "string" &&
|
|
191
|
+
(candidate.key === undefined || typeof candidate.key === "string") &&
|
|
125
192
|
["user", "agent", "system", "tool"].includes(candidate.role) &&
|
|
126
193
|
["web", "telegram", "discord", "slack", "cli"].includes(candidate.source);
|
|
127
194
|
}
|
|
@@ -159,4 +226,35 @@ function activityActorMatches(actor, query) {
|
|
|
159
226
|
function randomId() {
|
|
160
227
|
return randomUUID().replace(/-/g, "").slice(0, 12);
|
|
161
228
|
}
|
|
162
|
-
|
|
229
|
+
function dedupeWebChatMessages(messages) {
|
|
230
|
+
const seen = new Set();
|
|
231
|
+
return messages.filter((message) => {
|
|
232
|
+
const key = webChatDedupKey(message);
|
|
233
|
+
if (!key) {
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
if (seen.has(key)) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
seen.add(key);
|
|
240
|
+
return true;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
function findDuplicateWebChatMessage(messages, input) {
|
|
244
|
+
const key = webChatDedupKey(input);
|
|
245
|
+
return key ? messages.find((message) => webChatDedupKey(message) === key) : undefined;
|
|
246
|
+
}
|
|
247
|
+
function webChatDedupKey(message) {
|
|
248
|
+
const threadId = message.threadId || "pending";
|
|
249
|
+
if (message.key) {
|
|
250
|
+
return [threadId, message.key].join("\0");
|
|
251
|
+
}
|
|
252
|
+
if (message.turnId) {
|
|
253
|
+
return [threadId, message.role, message.source, message.turnId, message.text].join("\0");
|
|
254
|
+
}
|
|
255
|
+
if (message.timestamp) {
|
|
256
|
+
return [threadId, message.role, message.source, message.timestamp, message.text].join("\0");
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
export { activityActorLabel, activityCategoryForType, auditCategoryForAction, } from "../core/activity-events.js";
|