chirli 0.0.0-stage → 0.2.0

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.
Files changed (97) hide show
  1. package/ARCHITECTURE.md +103 -0
  2. package/CONTRIBUTING.md +56 -0
  3. package/LICENSE +22 -0
  4. package/README.md +281 -2
  5. package/SECURITY.md +55 -0
  6. package/THIRD_PARTY_NOTICES.md +51 -0
  7. package/dist/allowlist.d.ts +3 -0
  8. package/dist/allowlist.js +9 -0
  9. package/dist/announcements.d.ts +25 -0
  10. package/dist/announcements.js +142 -0
  11. package/dist/attachments.d.ts +43 -0
  12. package/dist/attachments.js +340 -0
  13. package/dist/blossom.d.ts +30 -0
  14. package/dist/blossom.js +233 -0
  15. package/dist/bot-handlers.d.ts +70 -0
  16. package/dist/bot-handlers.js +317 -0
  17. package/dist/bot.d.ts +63 -0
  18. package/dist/bot.js +244 -0
  19. package/dist/client.d.ts +221 -0
  20. package/dist/client.js +1197 -0
  21. package/dist/command-discovery.d.ts +4 -0
  22. package/dist/command-discovery.js +33 -0
  23. package/dist/command-schema.d.ts +4 -0
  24. package/dist/command-schema.js +101 -0
  25. package/dist/commands.d.ts +32 -0
  26. package/dist/commands.js +253 -0
  27. package/dist/defaults.d.ts +9 -0
  28. package/dist/defaults.js +22 -0
  29. package/dist/errors.d.ts +55 -0
  30. package/dist/errors.js +126 -0
  31. package/dist/inbox.d.ts +55 -0
  32. package/dist/inbox.js +376 -0
  33. package/dist/index.d.ts +25 -0
  34. package/dist/index.js +11 -0
  35. package/dist/key-sync.d.ts +33 -0
  36. package/dist/key-sync.js +293 -0
  37. package/dist/keys.d.ts +66 -0
  38. package/dist/keys.js +418 -0
  39. package/dist/message-storage-sqlite.d.ts +6 -0
  40. package/dist/message-storage-sqlite.js +90 -0
  41. package/dist/message-storage.d.ts +32 -0
  42. package/dist/message-storage.js +1 -0
  43. package/dist/messaging-metadata.d.ts +48 -0
  44. package/dist/messaging-metadata.js +280 -0
  45. package/dist/metadata.d.ts +7 -0
  46. package/dist/metadata.js +63 -0
  47. package/dist/modes.d.ts +26 -0
  48. package/dist/modes.js +108 -0
  49. package/dist/nip4e-compat.d.ts +4 -0
  50. package/dist/nip4e-compat.js +14 -0
  51. package/dist/nipcd.d.ts +109 -0
  52. package/dist/nipcd.js +555 -0
  53. package/dist/protocol.d.ts +68 -0
  54. package/dist/protocol.js +302 -0
  55. package/dist/public-inbox.d.ts +66 -0
  56. package/dist/public-inbox.js +295 -0
  57. package/dist/public-thread.d.ts +62 -0
  58. package/dist/public-thread.js +436 -0
  59. package/dist/relay-pool.d.ts +43 -0
  60. package/dist/relay-pool.js +429 -0
  61. package/dist/relay-roles.d.ts +18 -0
  62. package/dist/relay-roles.js +55 -0
  63. package/dist/relay.d.ts +52 -0
  64. package/dist/relay.js +493 -0
  65. package/dist/request-timing.d.ts +12 -0
  66. package/dist/request-timing.js +37 -0
  67. package/dist/session.d.ts +16 -0
  68. package/dist/session.js +51 -0
  69. package/dist/signer.d.ts +13 -0
  70. package/dist/signer.js +1 -0
  71. package/dist/signers/bunker-relays.d.ts +15 -0
  72. package/dist/signers/bunker-relays.js +87 -0
  73. package/dist/signers/bunker.d.ts +7 -0
  74. package/dist/signers/bunker.js +194 -0
  75. package/dist/signers/nsec.d.ts +3 -0
  76. package/dist/signers/nsec.js +62 -0
  77. package/dist/signers.d.ts +18 -0
  78. package/dist/signers.js +59 -0
  79. package/dist/storage-sqlite.d.ts +29 -0
  80. package/dist/storage-sqlite.js +221 -0
  81. package/dist/storage.d.ts +85 -0
  82. package/dist/storage.js +1 -0
  83. package/dist/store.d.ts +96 -0
  84. package/dist/store.js +404 -0
  85. package/docs/ACCESS.md +28 -0
  86. package/docs/API.md +382 -0
  87. package/docs/INTEROPERABILITY.md +32 -0
  88. package/docs/KEYS.md +75 -0
  89. package/docs/NIP-CD.md +112 -0
  90. package/docs/PUBLIC.md +88 -0
  91. package/docs/RECOVERY.md +59 -0
  92. package/docs/RELAYS.md +145 -0
  93. package/docs/SESSIONS.md +108 -0
  94. package/docs/SIGNERS.md +59 -0
  95. package/docs/STORAGE.md +71 -0
  96. package/docs/TUTORIAL.md +178 -0
  97. package/package.json +47 -4
@@ -0,0 +1,178 @@
1
+ # Build your first bot
2
+
3
+ Run the included bot, add a command and keep state between messages. Then use the same SDK in your own application.
4
+
5
+ ## 1. Run the existing bot
6
+
7
+ Use Node.js 24+ and run these commands from the GitHub checkout.
8
+
9
+ ```sh
10
+ npm ci --ignore-scripts
11
+ npm run build
12
+ npm run bot:commands
13
+ ```
14
+
15
+ Wait for the ready banner. Send `/ping` to its printed npub from another Nostr account. Press Ctrl+C to stop.
16
+
17
+ On first launch the runner creates credentials beside `.chirli/bot.sqlite`. Later launches reuse them and the database. Local storage is plaintext unless a storage key is configured. The live setup guide covers existing nsec keys, bunker and custom relays. [Live setup](https://github.com/CodyTseng/chirli/blob/main/examples/live/README.md)
18
+
19
+ ## 2. Add a command
20
+
21
+ Add this import at the top of `examples/live/commands.ts`:
22
+
23
+ ```ts
24
+ import { npubEncode } from "nostr-tools/nip19";
25
+ ```
26
+
27
+ Register the command inside the existing `runLive` callback:
28
+
29
+ ```ts
30
+ bot.command({ command: "hello", scopes: ["kind:14"] }, async (ctx) => {
31
+ await ctx.reply(`Hello, nostr:${npubEncode(ctx.sender)}!`);
32
+ });
33
+ ```
34
+
35
+ Rebuild and restart:
36
+
37
+ ```sh
38
+ npm run build
39
+ npm run bot:commands
40
+ ```
41
+
42
+ Send `/hello` to get a greeting with a `nostr:npub1…` mention. Clients that support inline mentions can display it as the sender's profile name. See [NIP-27](https://github.com/nostr-protocol/nips/blob/master/27.md).
43
+
44
+ The `runLive` helper selects a signer, constructs and starts a Bot, prints readiness and closes application-owned resources on signals. [Example runtime](https://github.com/CodyTseng/chirli/blob/main/examples/live/runtime.ts)
45
+
46
+ ### Validate command parameters with Zod
47
+
48
+ The existing live commands example already imports z from zod; keep that import once. Add only these imports:
49
+
50
+ ```ts
51
+ import { InvalidArgsError } from "chirli";
52
+ import { randomInt } from "node:crypto";
53
+ ```
54
+
55
+ Inside the existing runLive callback, register shared error handling before the command:
56
+
57
+ ```ts
58
+ bot.use(async (ctx, next) => {
59
+ try {
60
+ await next();
61
+ } catch (error) {
62
+ if (!(error instanceof InvalidArgsError)) throw error;
63
+ await ctx.reply("Usage: /roll <1..20>");
64
+ }
65
+ });
66
+
67
+ bot.command(
68
+ {
69
+ command: "roll",
70
+ scopes: ["kind:14"],
71
+ args: z.object({
72
+ sides: z.bigint().min(1n).max(20n).meta({ label: "Sides" }),
73
+ }),
74
+ },
75
+ async (ctx, args) => {
76
+ // Both entry points contain the same validated output.
77
+ const rolled = randomInt(1, Number(ctx.args.sides) + 1);
78
+ await ctx.reply(`Rolled ${rolled} on a ${args.sides}-sided die.`);
79
+ },
80
+ );
81
+ ```
82
+
83
+ Rebuild and restart, then send `/roll 6`: the reply is `Rolled N on a 6-sided die.`, with N in 1..6. `/roll`, `/roll invalid` and `/roll 21` each produce `Usage: /roll <1..20>` from the shared middleware, never the command handler. Arguments are decoded by NIP-CD before Zod runs; number tokens produce bigint. No response or retry is added when no error handler is configured. Plain fallback does not receive invalid arguments for a recognized, correctly targeted/scoped command.
84
+
85
+ Only command handlers have typed ctx.args; middleware uses the ordinary context before validation and sees no args after next returns. Use `.meta({nipcdType:'text'})` for a final free-text string; plain strings are words. See [schema support](API.md#zod-command-arguments) for optional/address fields and unsupported transforms. To run these valid/invalid examples without any external connection, build and run `npm run example:commands`; its /roll handler uses the same schema and middleware.
86
+
87
+ For commands with args-dependent authorize, invalid input is silently terminated: an invalid request cannot authorize a usage reply. A validator exception before authorization bypasses reply-capable middleware/catch observers and reaches operational `onError` without a second processing attempt.
88
+
89
+ ## 3. Persist conversation state
90
+
91
+ Use the conversation’s key-value storage through `ctx.session`. The handler supplies the default when a key is missing.
92
+
93
+ ```ts
94
+ const bot = new Bot<{ visits: number }>({
95
+ signer,
96
+ database: "./chirli.sqlite",
97
+ });
98
+
99
+ bot.command({ command: "visit", scopes: ["kind:14"] }, async (ctx) => {
100
+ const visits = ((await ctx.session.getItem("visits")) ?? 0) + 1;
101
+ ctx.session.setItem("visits", visits);
102
+ await ctx.reply(`Visit ${visits}`);
103
+ });
104
+
105
+ await bot.start();
106
+ ```
107
+
108
+ Send `/visit` twice, restart with the same account and database, then send it again. The count continues because the default session backend uses SQLite.
109
+
110
+ Call `ctx.session.removeItem(key)` to delete one value or `ctx.session.clear()` to reset the current conversation. Writes and deletions are saved after successful processing. A failed handler discards its changes. See [conversation sessions](SESSIONS.md) for a complete custom `sessionStore` example.
111
+
112
+ ## 4. Choose durable work and error handling
113
+
114
+ Register a handler for messages with no matching command:
115
+
116
+ ```ts
117
+ bot.handle(async (ctx) => {
118
+ await ctx.reply("Message received");
119
+ });
120
+ ```
121
+
122
+ Use `bot.use()` for shared middleware before routing. Use `observe()` for independent receipt monitoring. Observers do not determine processing success.
123
+
124
+ ```ts
125
+ bot.catch((error, ctx) => {
126
+ // Observe the failure using your application's sanitized diagnostics.
127
+ });
128
+ ```
129
+
130
+ `Bot.catch()` observes session, middleware and business errors. No callback is retried by the SDK; the input ID stays claimed. Framework `InvalidArgsError` never reaches the business handler or plain fallback and is silent unless middleware or `bot.catch` handles it. For business work that must be retried, persist your own job using the input ID. For an already queued outgoing message, use `bot.flushOutbox()` to retry delivery of the same signed event.
131
+
132
+ See the [event processing flow](API.md#event-processing-flow) for what stops before routing and what can be retried. Handlers are serial per conversation. Calling `idle()`, `stop()` or `close()` from a handler would wait on itself and is rejected. Use `requestStop()` to request shutdown and drain from outside the handler.
133
+
134
+ ## 5. Understand public command synchronization
135
+
136
+ All commands are public on configured write relays. Their definitions are synchronized before Bot startup finishes. Register the complete command set before starting; definitions absent locally are withdrawn.
137
+
138
+ - To remove a command, omit it from the complete registry and restart with the same account/relays; startup publishes the withdrawal. Runtime additions remain supported.
139
+ - Declare a typed argument schema in `command()` for typed arguments. Registration accepts an `authorize` callback.
140
+ - Register `bot.use(allowlist(pubkeys))` first to authorize live senders before later middleware and handlers.
141
+ - Use `discoverCommands()` to inspect metadata; discovery never installs handlers.
142
+
143
+ Commands are declared automatically as kind `31992`, with `d` derived from the command name. Withdrawals use kind `5`; user-installed kind `10992` lists are untouched. See [commands](NIP-CD.md) for the complete contract.
144
+
145
+ ## 6. Move into your own application
146
+
147
+ Create a verified release tarball from this checkout.
148
+
149
+ ```sh
150
+ npm run pack:release
151
+ ```
152
+
153
+ Install the resulting tarball in your ESM application, for example `npm install /absolute/path/to/chirli/chirli-0.1.0.tgz`. Import from `chirli`, without internal source paths.
154
+
155
+ Construct the Bot, register handlers and await startup:
156
+
157
+ ```ts
158
+ import { Bot, nsecSigner } from "chirli";
159
+
160
+ const signer = nsecSigner(nsecFromYourSecretManager);
161
+ const bot = new Bot({
162
+ signer,
163
+ database: "./chirli.sqlite",
164
+ });
165
+
166
+ bot.command({ command: "ping", scopes: ["kind:14"] }, async (ctx) => {
167
+ await ctx.reply("pong", { replyTo: ctx.input.id });
168
+ });
169
+
170
+ await bot.start();
171
+ console.log(bot.pubkey);
172
+ ```
173
+
174
+ Use `bunkerSigner(uri, { clientSecret })` through the same interface when signing remotely. Keep the NIP-46 client key for subsequent runs. Register handlers, then await `bot.start()` before displaying the public identity.
175
+
176
+ On shutdown, stop application timers and await their work, close the Bot, then close injected resources. Bot closes only its internally created transport/storage; signers and other injected resources remain yours.
177
+
178
+ The live runner shows the complete application lifecycle. The offline walkthroughs illustrate individual APIs. [Live runner](https://github.com/CodyTseng/chirli/blob/main/examples/live/runtime.ts) · [Examples](https://github.com/CodyTseng/chirli/blob/main/examples/README.md)
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "chirli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "engines": {
7
+ "node": ">=24"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "!dist/examples",
18
+ "docs",
19
+ "README.md",
20
+ "ARCHITECTURE.md",
21
+ "THIRD_PARTY_NOTICES.md",
22
+ "LICENSE",
23
+ "SECURITY.md",
24
+ "CONTRIBUTING.md"
25
+ ],
26
+ "dependencies": {
27
+ "nostr-tools": "^2.26.0",
28
+ "ws": "^8.18.0",
29
+ "zod": "^4.6.7"
30
+ },
31
+ "description": "A developer-controlled Nostr bot SDK for private messages and explicitly scoped kind 1/1111 public commands.",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/CodyTseng/chirli.git"
35
+ },
36
+ "homepage": "https://github.com/CodyTseng/chirli#readme",
37
+ "bugs": {
38
+ "url": "https://github.com/CodyTseng/chirli/issues"
39
+ },
40
+ "main": "./dist/index.js",
41
+ "types": "./dist/index.d.ts",
42
+ "keywords": [
43
+ "nostr",
44
+ "nip17",
45
+ "nip44",
46
+ "bot",
47
+ "typescript"
48
+ ]
49
+ }