@actagent/nostr 2026.6.2
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/README.md +142 -0
- package/actagent.plugin.json +17 -0
- package/api.ts +11 -0
- package/channel-plugin-api.ts +2 -0
- package/doctor-contract-api.test.ts +105 -0
- package/doctor-contract-api.ts +297 -0
- package/index.ts +96 -0
- package/npm-shrinkwrap.json +137 -0
- package/package.json +67 -0
- package/runtime-api.ts +6 -0
- package/setup-api.ts +2 -0
- package/setup-entry.ts +10 -0
- package/setup-plugin-api.ts +3 -0
- package/src/channel-api.ts +12 -0
- package/src/channel.inbound.test.ts +203 -0
- package/src/channel.lifecycle.test.ts +97 -0
- package/src/channel.outbound.test.ts +175 -0
- package/src/channel.setup.ts +161 -0
- package/src/channel.test.ts +527 -0
- package/src/channel.ts +215 -0
- package/src/config-schema.ts +99 -0
- package/src/default-relays.ts +2 -0
- package/src/gateway.ts +338 -0
- package/src/inbound-direct-dm-runtime.ts +2 -0
- package/src/metrics.ts +454 -0
- package/src/nostr-bus.fuzz.test.ts +383 -0
- package/src/nostr-bus.inbound.test.ts +598 -0
- package/src/nostr-bus.integration.test.ts +491 -0
- package/src/nostr-bus.test.ts +256 -0
- package/src/nostr-bus.ts +799 -0
- package/src/nostr-key-utils.ts +93 -0
- package/src/nostr-profile-core.ts +135 -0
- package/src/nostr-profile-http-runtime.ts +7 -0
- package/src/nostr-profile-http.test.ts +632 -0
- package/src/nostr-profile-http.ts +583 -0
- package/src/nostr-profile-import.test.ts +196 -0
- package/src/nostr-profile-import.ts +273 -0
- package/src/nostr-profile-url-safety.ts +22 -0
- package/src/nostr-profile.fuzz.test.ts +431 -0
- package/src/nostr-profile.test.ts +416 -0
- package/src/nostr-profile.ts +144 -0
- package/src/nostr-state-store.test.ts +172 -0
- package/src/nostr-state-store.ts +132 -0
- package/src/runtime.ts +10 -0
- package/src/seen-tracker.ts +291 -0
- package/src/session-route.ts +26 -0
- package/src/setup-adapter.ts +86 -0
- package/src/setup-surface.ts +204 -0
- package/src/test-fixtures.ts +46 -0
- package/src/types.ts +118 -0
- package/test/setup.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Nostr tests cover nostr state store plugin behavior.
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { OpenKeyedStoreOptions } from "actagent/plugin-sdk/plugin-state-runtime";
|
|
6
|
+
import {
|
|
7
|
+
createPluginStateKeyedStoreForTests,
|
|
8
|
+
resetPluginStateStoreForTests,
|
|
9
|
+
} from "actagent/plugin-sdk/plugin-state-test-runtime";
|
|
10
|
+
import { describe, expect, it } from "vitest";
|
|
11
|
+
import type { PluginRuntime } from "../runtime-api.js";
|
|
12
|
+
import {
|
|
13
|
+
readNostrBusState,
|
|
14
|
+
readNostrProfileState,
|
|
15
|
+
writeNostrBusState,
|
|
16
|
+
writeNostrProfileState,
|
|
17
|
+
computeSinceTimestamp,
|
|
18
|
+
} from "./nostr-state-store.js";
|
|
19
|
+
import { setNostrRuntime } from "./runtime.js";
|
|
20
|
+
|
|
21
|
+
async function withTempStateDir<T>(fn: (dir: string) => Promise<T>) {
|
|
22
|
+
const previous = process.env.ACTAGENT_STATE_DIR;
|
|
23
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "actagent-nostr-"));
|
|
24
|
+
process.env.ACTAGENT_STATE_DIR = dir;
|
|
25
|
+
resetPluginStateStoreForTests();
|
|
26
|
+
setNostrRuntime({
|
|
27
|
+
state: {
|
|
28
|
+
openKeyedStore: (options: OpenKeyedStoreOptions) =>
|
|
29
|
+
createPluginStateKeyedStoreForTests("nostr", {
|
|
30
|
+
...options,
|
|
31
|
+
env: { ...process.env, ACTAGENT_STATE_DIR: dir },
|
|
32
|
+
}),
|
|
33
|
+
resolveStateDir: (env, homedir) => {
|
|
34
|
+
const stateEnv = env ?? process.env;
|
|
35
|
+
const override = stateEnv.ACTAGENT_STATE_DIR?.trim();
|
|
36
|
+
if (override) {
|
|
37
|
+
return override;
|
|
38
|
+
}
|
|
39
|
+
const resolveHome = homedir ?? os.homedir;
|
|
40
|
+
return path.join(resolveHome(), ".actagent");
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
} as PluginRuntime);
|
|
44
|
+
try {
|
|
45
|
+
return await fn(dir);
|
|
46
|
+
} finally {
|
|
47
|
+
if (previous === undefined) {
|
|
48
|
+
delete process.env.ACTAGENT_STATE_DIR;
|
|
49
|
+
} else {
|
|
50
|
+
process.env.ACTAGENT_STATE_DIR = previous;
|
|
51
|
+
}
|
|
52
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("nostr bus state store", () => {
|
|
57
|
+
it("persists and reloads state across restarts", async () => {
|
|
58
|
+
await withTempStateDir(async () => {
|
|
59
|
+
// Fresh start - no state
|
|
60
|
+
expect(await readNostrBusState({ accountId: "test-bot" })).toBeNull();
|
|
61
|
+
|
|
62
|
+
// Write state
|
|
63
|
+
await writeNostrBusState({
|
|
64
|
+
accountId: "test-bot",
|
|
65
|
+
lastProcessedAt: 1700000000,
|
|
66
|
+
gatewayStartedAt: 1700000100,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Read it back
|
|
70
|
+
const state = await readNostrBusState({ accountId: "test-bot" });
|
|
71
|
+
expect(state).toEqual({
|
|
72
|
+
version: 2,
|
|
73
|
+
lastProcessedAt: 1700000000,
|
|
74
|
+
gatewayStartedAt: 1700000100,
|
|
75
|
+
recentEventIds: [],
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("isolates state by accountId", async () => {
|
|
81
|
+
await withTempStateDir(async () => {
|
|
82
|
+
await writeNostrBusState({
|
|
83
|
+
accountId: "bot-a",
|
|
84
|
+
lastProcessedAt: 1000,
|
|
85
|
+
gatewayStartedAt: 1000,
|
|
86
|
+
});
|
|
87
|
+
await writeNostrBusState({
|
|
88
|
+
accountId: "bot-b",
|
|
89
|
+
lastProcessedAt: 2000,
|
|
90
|
+
gatewayStartedAt: 2000,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const stateA = await readNostrBusState({ accountId: "bot-a" });
|
|
94
|
+
const stateB = await readNostrBusState({ accountId: "bot-b" });
|
|
95
|
+
|
|
96
|
+
expect(stateA?.lastProcessedAt).toBe(1000);
|
|
97
|
+
expect(stateB?.lastProcessedAt).toBe(2000);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("nostr profile state store", () => {
|
|
103
|
+
it("persists and reloads profile publish state", async () => {
|
|
104
|
+
await withTempStateDir(async () => {
|
|
105
|
+
await writeNostrProfileState({
|
|
106
|
+
accountId: "test-bot",
|
|
107
|
+
lastPublishedAt: 1700000000,
|
|
108
|
+
lastPublishedEventId: "evt-1",
|
|
109
|
+
lastPublishResults: {
|
|
110
|
+
"wss://relay.example": "ok",
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const state = await readNostrProfileState({ accountId: "test-bot" });
|
|
115
|
+
expect(state).toEqual({
|
|
116
|
+
version: 1,
|
|
117
|
+
lastPublishedAt: 1700000000,
|
|
118
|
+
lastPublishedEventId: "evt-1",
|
|
119
|
+
lastPublishResults: {
|
|
120
|
+
"wss://relay.example": "ok",
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("computeSinceTimestamp", () => {
|
|
128
|
+
it("returns now for null state (fresh start)", () => {
|
|
129
|
+
const now = 1700000000;
|
|
130
|
+
expect(computeSinceTimestamp(null, now)).toBe(now);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("uses lastProcessedAt when available", () => {
|
|
134
|
+
const state: Parameters<typeof computeSinceTimestamp>[0] = {
|
|
135
|
+
version: 2,
|
|
136
|
+
lastProcessedAt: 1699999000,
|
|
137
|
+
gatewayStartedAt: null,
|
|
138
|
+
recentEventIds: [],
|
|
139
|
+
};
|
|
140
|
+
expect(computeSinceTimestamp(state, 1700000000)).toBe(1699999000);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("uses gatewayStartedAt when lastProcessedAt is null", () => {
|
|
144
|
+
const state: Parameters<typeof computeSinceTimestamp>[0] = {
|
|
145
|
+
version: 2,
|
|
146
|
+
lastProcessedAt: null,
|
|
147
|
+
gatewayStartedAt: 1699998000,
|
|
148
|
+
recentEventIds: [],
|
|
149
|
+
};
|
|
150
|
+
expect(computeSinceTimestamp(state, 1700000000)).toBe(1699998000);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("uses the max of both timestamps", () => {
|
|
154
|
+
const state: Parameters<typeof computeSinceTimestamp>[0] = {
|
|
155
|
+
version: 2,
|
|
156
|
+
lastProcessedAt: 1699999000,
|
|
157
|
+
gatewayStartedAt: 1699998000,
|
|
158
|
+
recentEventIds: [],
|
|
159
|
+
};
|
|
160
|
+
expect(computeSinceTimestamp(state, 1700000000)).toBe(1699999000);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("falls back to now if both are null", () => {
|
|
164
|
+
const state: Parameters<typeof computeSinceTimestamp>[0] = {
|
|
165
|
+
version: 2,
|
|
166
|
+
lastProcessedAt: null,
|
|
167
|
+
gatewayStartedAt: null,
|
|
168
|
+
recentEventIds: [],
|
|
169
|
+
};
|
|
170
|
+
expect(computeSinceTimestamp(state, 1700000000)).toBe(1700000000);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Nostr plugin module implements nostr state store behavior.
|
|
2
|
+
import { getNostrRuntime } from "./runtime.js";
|
|
3
|
+
|
|
4
|
+
const STORE_VERSION = 2;
|
|
5
|
+
const PROFILE_STATE_VERSION = 1;
|
|
6
|
+
|
|
7
|
+
type NostrBusState = {
|
|
8
|
+
version: 2;
|
|
9
|
+
/** Unix timestamp (seconds) of the last processed event */
|
|
10
|
+
lastProcessedAt: number | null;
|
|
11
|
+
/** Gateway startup timestamp (seconds) - events before this are old */
|
|
12
|
+
gatewayStartedAt: number | null;
|
|
13
|
+
/** Recent processed event IDs for overlap dedupe across restarts */
|
|
14
|
+
recentEventIds: string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Profile publish state (separate from bus state) */
|
|
18
|
+
type NostrProfileState = {
|
|
19
|
+
version: 1;
|
|
20
|
+
/** Unix timestamp (seconds) of last successful profile publish */
|
|
21
|
+
lastPublishedAt: number | null;
|
|
22
|
+
/** Event ID of the last published profile */
|
|
23
|
+
lastPublishedEventId: string | null;
|
|
24
|
+
/** Per-relay publish results from last attempt */
|
|
25
|
+
lastPublishResults: Record<string, "ok" | "failed" | "timeout"> | null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function normalizeAccountId(accountId?: string): string {
|
|
29
|
+
const trimmed = accountId?.trim();
|
|
30
|
+
if (!trimmed) {
|
|
31
|
+
return "default";
|
|
32
|
+
}
|
|
33
|
+
return trimmed.replace(/[^a-z0-9._-]+/gi, "_");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function openNostrBusStateStore(env?: NodeJS.ProcessEnv) {
|
|
37
|
+
return getNostrRuntime().state.openKeyedStore<NostrBusState>({
|
|
38
|
+
namespace: "bus-state",
|
|
39
|
+
maxEntries: 256,
|
|
40
|
+
...(env ? { env } : {}),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function openNostrProfileStateStore(env?: NodeJS.ProcessEnv) {
|
|
45
|
+
return getNostrRuntime().state.openKeyedStore<NostrProfileState>({
|
|
46
|
+
namespace: "profile-state",
|
|
47
|
+
maxEntries: 256,
|
|
48
|
+
...(env ? { env } : {}),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function readNostrBusState(params: {
|
|
53
|
+
accountId?: string;
|
|
54
|
+
env?: NodeJS.ProcessEnv;
|
|
55
|
+
}): Promise<NostrBusState | null> {
|
|
56
|
+
return (
|
|
57
|
+
(await openNostrBusStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ?? null
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function writeNostrBusState(params: {
|
|
62
|
+
accountId?: string;
|
|
63
|
+
lastProcessedAt: number;
|
|
64
|
+
gatewayStartedAt: number;
|
|
65
|
+
recentEventIds?: string[];
|
|
66
|
+
env?: NodeJS.ProcessEnv;
|
|
67
|
+
}): Promise<void> {
|
|
68
|
+
const payload: NostrBusState = {
|
|
69
|
+
version: STORE_VERSION,
|
|
70
|
+
lastProcessedAt: params.lastProcessedAt,
|
|
71
|
+
gatewayStartedAt: params.gatewayStartedAt,
|
|
72
|
+
recentEventIds: (params.recentEventIds ?? []).filter((x): x is string => typeof x === "string"),
|
|
73
|
+
};
|
|
74
|
+
await openNostrBusStateStore(params.env).register(normalizeAccountId(params.accountId), payload);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Determine the `since` timestamp for subscription.
|
|
79
|
+
* Returns the later of: lastProcessedAt or gatewayStartedAt (both from state),
|
|
80
|
+
* falling back to `now` for fresh starts.
|
|
81
|
+
*/
|
|
82
|
+
export function computeSinceTimestamp(
|
|
83
|
+
state: NostrBusState | null,
|
|
84
|
+
nowSec: number = Math.floor(Date.now() / 1000),
|
|
85
|
+
): number {
|
|
86
|
+
if (!state) {
|
|
87
|
+
return nowSec;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Use the most recent timestamp we have
|
|
91
|
+
const candidates = [state.lastProcessedAt, state.gatewayStartedAt].filter(
|
|
92
|
+
(t): t is number => t !== null && t > 0,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
if (candidates.length === 0) {
|
|
96
|
+
return nowSec;
|
|
97
|
+
}
|
|
98
|
+
return Math.max(...candidates);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ============================================================================
|
|
102
|
+
// Profile State Management
|
|
103
|
+
// ============================================================================
|
|
104
|
+
|
|
105
|
+
export async function readNostrProfileState(params: {
|
|
106
|
+
accountId?: string;
|
|
107
|
+
env?: NodeJS.ProcessEnv;
|
|
108
|
+
}): Promise<NostrProfileState | null> {
|
|
109
|
+
return (
|
|
110
|
+
(await openNostrProfileStateStore(params.env).lookup(normalizeAccountId(params.accountId))) ??
|
|
111
|
+
null
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function writeNostrProfileState(params: {
|
|
116
|
+
accountId?: string;
|
|
117
|
+
lastPublishedAt: number;
|
|
118
|
+
lastPublishedEventId: string;
|
|
119
|
+
lastPublishResults: Record<string, "ok" | "failed" | "timeout">;
|
|
120
|
+
env?: NodeJS.ProcessEnv;
|
|
121
|
+
}): Promise<void> {
|
|
122
|
+
const payload: NostrProfileState = {
|
|
123
|
+
version: PROFILE_STATE_VERSION,
|
|
124
|
+
lastPublishedAt: params.lastPublishedAt,
|
|
125
|
+
lastPublishedEventId: params.lastPublishedEventId,
|
|
126
|
+
lastPublishResults: params.lastPublishResults,
|
|
127
|
+
};
|
|
128
|
+
await openNostrProfileStateStore(params.env).register(
|
|
129
|
+
normalizeAccountId(params.accountId),
|
|
130
|
+
payload,
|
|
131
|
+
);
|
|
132
|
+
}
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Nostr plugin module implements runtime behavior.
|
|
2
|
+
import type { PluginRuntime } from "actagent/plugin-sdk/core";
|
|
3
|
+
import { createPluginRuntimeStore } from "actagent/plugin-sdk/runtime-store";
|
|
4
|
+
|
|
5
|
+
const { setRuntime: setNostrRuntime, getRuntime: getNostrRuntime } =
|
|
6
|
+
createPluginRuntimeStore<PluginRuntime>({
|
|
7
|
+
pluginId: "nostr",
|
|
8
|
+
errorMessage: "Nostr runtime not initialized",
|
|
9
|
+
});
|
|
10
|
+
export { getNostrRuntime, setNostrRuntime };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LRU-based seen event tracker with TTL support.
|
|
3
|
+
* Prevents unbounded memory growth under high load or abuse.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { resolveIntegerOption } from "actagent/plugin-sdk/number-runtime";
|
|
7
|
+
|
|
8
|
+
interface SeenTrackerOptions {
|
|
9
|
+
/** Maximum number of entries to track (default: 100,000) */
|
|
10
|
+
maxEntries?: number;
|
|
11
|
+
/** TTL in milliseconds (default: 1 hour) */
|
|
12
|
+
ttlMs?: number;
|
|
13
|
+
/** Prune interval in milliseconds (default: 10 minutes) */
|
|
14
|
+
pruneIntervalMs?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SeenTracker {
|
|
18
|
+
/** Check if an ID has been seen (also marks it as seen if not) */
|
|
19
|
+
has: (id: string) => boolean;
|
|
20
|
+
/** Mark an ID as seen */
|
|
21
|
+
add: (id: string) => void;
|
|
22
|
+
/** Check if ID exists without marking */
|
|
23
|
+
peek: (id: string) => boolean;
|
|
24
|
+
/** Delete an ID */
|
|
25
|
+
delete: (id: string) => void;
|
|
26
|
+
/** Clear all entries */
|
|
27
|
+
clear: () => void;
|
|
28
|
+
/** Get current size */
|
|
29
|
+
size: () => number;
|
|
30
|
+
/** Stop the pruning timer */
|
|
31
|
+
stop: () => void;
|
|
32
|
+
/** Pre-seed with IDs (useful for restart recovery) */
|
|
33
|
+
seed: (ids: string[]) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Entry {
|
|
37
|
+
seenAt: number;
|
|
38
|
+
// For LRU: track order via doubly-linked list
|
|
39
|
+
prev: string | null;
|
|
40
|
+
next: string | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a new seen tracker with LRU eviction and TTL expiration.
|
|
45
|
+
*/
|
|
46
|
+
export function createSeenTracker(options?: SeenTrackerOptions): SeenTracker {
|
|
47
|
+
const maxEntries = resolveIntegerOption(options?.maxEntries, 100_000, { min: 1 });
|
|
48
|
+
const ttlMs = options?.ttlMs ?? 60 * 60 * 1000; // 1 hour
|
|
49
|
+
const pruneIntervalMs = options?.pruneIntervalMs ?? 10 * 60 * 1000; // 10 minutes
|
|
50
|
+
|
|
51
|
+
// Main storage
|
|
52
|
+
const entries = new Map<string, Entry>();
|
|
53
|
+
|
|
54
|
+
// LRU tracking: head = most recent, tail = least recent
|
|
55
|
+
let head: string | null = null;
|
|
56
|
+
let tail: string | null = null;
|
|
57
|
+
|
|
58
|
+
// Move an entry to the front (most recently used)
|
|
59
|
+
function moveToFront(id: string): void {
|
|
60
|
+
const entry = entries.get(id);
|
|
61
|
+
if (!entry) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Already at front
|
|
66
|
+
if (head === id) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Remove from current position
|
|
71
|
+
if (entry.prev) {
|
|
72
|
+
const prevEntry = entries.get(entry.prev);
|
|
73
|
+
if (prevEntry) {
|
|
74
|
+
prevEntry.next = entry.next;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (entry.next) {
|
|
78
|
+
const nextEntry = entries.get(entry.next);
|
|
79
|
+
if (nextEntry) {
|
|
80
|
+
nextEntry.prev = entry.prev;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Update tail if this was the tail
|
|
85
|
+
if (tail === id) {
|
|
86
|
+
tail = entry.prev;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Move to front
|
|
90
|
+
entry.prev = null;
|
|
91
|
+
entry.next = head;
|
|
92
|
+
if (head) {
|
|
93
|
+
const headEntry = entries.get(head);
|
|
94
|
+
if (headEntry) {
|
|
95
|
+
headEntry.prev = id;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
head = id;
|
|
99
|
+
|
|
100
|
+
// If no tail, this is also the tail
|
|
101
|
+
if (!tail) {
|
|
102
|
+
tail = id;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Remove an entry from the linked list
|
|
107
|
+
function removeFromList(id: string): void {
|
|
108
|
+
const entry = entries.get(id);
|
|
109
|
+
if (!entry) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (entry.prev) {
|
|
114
|
+
const prevEntry = entries.get(entry.prev);
|
|
115
|
+
if (prevEntry) {
|
|
116
|
+
prevEntry.next = entry.next;
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
head = entry.next;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (entry.next) {
|
|
123
|
+
const nextEntry = entries.get(entry.next);
|
|
124
|
+
if (nextEntry) {
|
|
125
|
+
nextEntry.prev = entry.prev;
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
tail = entry.prev;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Evict the least recently used entry
|
|
133
|
+
function evictLRU(): void {
|
|
134
|
+
if (!tail) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const idToEvict = tail;
|
|
138
|
+
removeFromList(idToEvict);
|
|
139
|
+
entries.delete(idToEvict);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function insertAtFront(id: string, seenAt: number): void {
|
|
143
|
+
const newEntry: Entry = {
|
|
144
|
+
seenAt,
|
|
145
|
+
prev: null,
|
|
146
|
+
next: head,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
if (head) {
|
|
150
|
+
const headEntry = entries.get(head);
|
|
151
|
+
if (headEntry) {
|
|
152
|
+
headEntry.prev = id;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
entries.set(id, newEntry);
|
|
157
|
+
head = id;
|
|
158
|
+
if (!tail) {
|
|
159
|
+
tail = id;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Prune expired entries
|
|
164
|
+
function pruneExpired(): void {
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
const toDelete: string[] = [];
|
|
167
|
+
|
|
168
|
+
for (const [id, entry] of entries) {
|
|
169
|
+
if (now - entry.seenAt > ttlMs) {
|
|
170
|
+
toDelete.push(id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
for (const id of toDelete) {
|
|
175
|
+
removeFromList(id);
|
|
176
|
+
entries.delete(id);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Start pruning timer
|
|
181
|
+
let pruneTimer: ReturnType<typeof setInterval> | undefined;
|
|
182
|
+
if (pruneIntervalMs > 0) {
|
|
183
|
+
pruneTimer = setInterval(pruneExpired, pruneIntervalMs);
|
|
184
|
+
// Don't keep process alive just for pruning
|
|
185
|
+
if (pruneTimer.unref) {
|
|
186
|
+
pruneTimer.unref();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function add(id: string): void {
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
|
|
193
|
+
// If already exists, update and move to front
|
|
194
|
+
const existing = entries.get(id);
|
|
195
|
+
if (existing) {
|
|
196
|
+
existing.seenAt = now;
|
|
197
|
+
moveToFront(id);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Evict if at capacity
|
|
202
|
+
while (entries.size >= maxEntries) {
|
|
203
|
+
evictLRU();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
insertAtFront(id, now);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function has(id: string): boolean {
|
|
210
|
+
const entry = entries.get(id);
|
|
211
|
+
if (!entry) {
|
|
212
|
+
add(id);
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Check if expired
|
|
217
|
+
if (Date.now() - entry.seenAt > ttlMs) {
|
|
218
|
+
removeFromList(id);
|
|
219
|
+
entries.delete(id);
|
|
220
|
+
add(id);
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Mark as recently used
|
|
225
|
+
entry.seenAt = Date.now();
|
|
226
|
+
moveToFront(id);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function peek(id: string): boolean {
|
|
231
|
+
const entry = entries.get(id);
|
|
232
|
+
if (!entry) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Check if expired
|
|
237
|
+
if (Date.now() - entry.seenAt > ttlMs) {
|
|
238
|
+
removeFromList(id);
|
|
239
|
+
entries.delete(id);
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function deleteEntry(id: string): void {
|
|
247
|
+
if (entries.has(id)) {
|
|
248
|
+
removeFromList(id);
|
|
249
|
+
entries.delete(id);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function clear(): void {
|
|
254
|
+
entries.clear();
|
|
255
|
+
head = null;
|
|
256
|
+
tail = null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function size(): number {
|
|
260
|
+
return entries.size;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function stop(): void {
|
|
264
|
+
if (pruneTimer) {
|
|
265
|
+
clearInterval(pruneTimer);
|
|
266
|
+
pruneTimer = undefined;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function seed(ids: string[]): void {
|
|
271
|
+
const now = Date.now();
|
|
272
|
+
// Seed in reverse order so first IDs end up at front
|
|
273
|
+
for (let i = ids.length - 1; i >= 0; i--) {
|
|
274
|
+
const id = ids[i];
|
|
275
|
+
if (!entries.has(id) && entries.size < maxEntries) {
|
|
276
|
+
insertAtFront(id, now);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
has,
|
|
283
|
+
add,
|
|
284
|
+
peek,
|
|
285
|
+
delete: deleteEntry,
|
|
286
|
+
clear,
|
|
287
|
+
size,
|
|
288
|
+
stop,
|
|
289
|
+
seed,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Nostr plugin module implements session route behavior.
|
|
2
|
+
import {
|
|
3
|
+
buildChannelOutboundSessionRoute,
|
|
4
|
+
stripChannelTargetPrefix,
|
|
5
|
+
type ChannelOutboundSessionRouteParams,
|
|
6
|
+
} from "actagent/plugin-sdk/core";
|
|
7
|
+
|
|
8
|
+
export function resolveNostrOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
|
|
9
|
+
const target = stripChannelTargetPrefix(params.target, "nostr");
|
|
10
|
+
if (!target) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return buildChannelOutboundSessionRoute({
|
|
14
|
+
cfg: params.cfg,
|
|
15
|
+
agentId: params.agentId,
|
|
16
|
+
channel: "nostr",
|
|
17
|
+
accountId: params.accountId,
|
|
18
|
+
peer: {
|
|
19
|
+
kind: "direct",
|
|
20
|
+
id: target,
|
|
21
|
+
},
|
|
22
|
+
chatType: "direct",
|
|
23
|
+
from: `nostr:${target}`,
|
|
24
|
+
to: `nostr:${target}`,
|
|
25
|
+
});
|
|
26
|
+
}
|