@nan0web/communication.app 0.0.0-stage → 0.1.1
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/LICENSE +15 -0
- package/bin/nan0com.js +16 -0
- package/package.json +34 -3
- package/src/domain/ChatRepoModel.js +157 -0
- package/src/domain/CommunicationApp.js +527 -0
- package/src/domain/CrystallizeModel.js +58 -0
- package/src/domain/FractalContactBookModel.js +147 -0
- package/src/domain/PeerGitSyncModel.js +105 -0
- package/src/index.js +13 -0
- package/src/infra/GitTransport.js +155 -0
- package/src/infra/Notifier.js +61 -0
- package/types/domain/ChatRepoModel.d.ts +66 -0
- package/types/domain/CommunicationApp.d.ts +296 -0
- package/types/domain/CrystallizeModel.d.ts +26 -0
- package/types/domain/DomainModel.d.ts +19 -0
- package/types/domain/FractalContactBookModel.d.ts +73 -0
- package/types/domain/PeerGitSyncModel.d.ts +64 -0
- package/types/index.d.ts +5 -0
- package/types/infra/GitTransport.d.ts +53 -0
- package/types/infra/Notifier.d.ts +23 -0
- package/README.md +0 -3
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import { ModelAsApp, show, result, progress } from '@nan0web/ui'
|
|
2
|
+
import { FractalContactBookModel } from './FractalContactBookModel.js'
|
|
3
|
+
import { ChatRepoModel, parseMessageMarkdown, resolveChatShardUri } from './ChatRepoModel.js'
|
|
4
|
+
import { CrystallizeModel } from './CrystallizeModel.js'
|
|
5
|
+
import { PeerGitSyncModel } from './PeerGitSyncModel.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolves target repository URI from explicit repo or chatId prefix sharding.
|
|
9
|
+
* @param {string} repo
|
|
10
|
+
* @param {string} chatId
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
function resolveTargetRepo(repo, chatId) {
|
|
14
|
+
if (repo) return repo
|
|
15
|
+
if (chatId) return resolveChatShardUri(chatId)
|
|
16
|
+
return resolveChatShardUri('default')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Subcommand: Register a contact in a 7iOS we/ circle.
|
|
21
|
+
*/
|
|
22
|
+
export class InitCommand extends ModelAsApp {
|
|
23
|
+
static alias = 'init'
|
|
24
|
+
static UI = {
|
|
25
|
+
initialized: 'Initialized contact {soulId} in circle {circle}',
|
|
26
|
+
bootstrapped: 'Day 1 tree: {count} files ({mode})',
|
|
27
|
+
}
|
|
28
|
+
static author = { help: 'Contact soulId', default: '' }
|
|
29
|
+
static circle = {
|
|
30
|
+
help: '7iOS circle (7i, kinship, partner, law, community, service, inbox)',
|
|
31
|
+
default: '7i',
|
|
32
|
+
}
|
|
33
|
+
static bootstrap = { help: 'Bootstrap Day 1 tree from tour/01', default: false }
|
|
34
|
+
static tourDir = { help: 'Tour template directory', default: 'docs/uk/tour/01' }
|
|
35
|
+
static dryRun = { help: 'Simulate initialization without writing files', default: false }
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {Partial<InitCommand>} [data]
|
|
39
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
40
|
+
*/
|
|
41
|
+
constructor(data = {}, options = {}) {
|
|
42
|
+
super(data, options)
|
|
43
|
+
/** @type {string} Contact soulId */ this.author
|
|
44
|
+
/** @type {string} 7iOS circle */ this.circle
|
|
45
|
+
/** @type {boolean} Bootstrap Day 1 tree */ this.bootstrap
|
|
46
|
+
/** @type {string} Tour template directory */ this.tourDir
|
|
47
|
+
/** @type {boolean} Dry run mode */ this.dryRun
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async *run() {
|
|
51
|
+
const book = new FractalContactBookModel({}, { db: this.$db })
|
|
52
|
+
if (this.bootstrap || !this.author) {
|
|
53
|
+
const summary = await book.bootstrapDayOneTree({
|
|
54
|
+
tourDir: this.tourDir,
|
|
55
|
+
dryRun: Boolean(this.dryRun),
|
|
56
|
+
})
|
|
57
|
+
const mode = summary.dryRun ? 'dry-run' : 'created'
|
|
58
|
+
yield show(this._.t(InitCommand.UI.bootstrapped, { count: summary.created.length, mode }), 'success')
|
|
59
|
+
return result({ ok: true, ...summary })
|
|
60
|
+
}
|
|
61
|
+
const contact = await book.addContact({
|
|
62
|
+
soulId: this.author,
|
|
63
|
+
circle: this.circle,
|
|
64
|
+
name: this.author,
|
|
65
|
+
dryRun: Boolean(this.dryRun),
|
|
66
|
+
})
|
|
67
|
+
yield show(this._.t(InitCommand.UI.initialized, contact), 'success')
|
|
68
|
+
return result({ ok: true, dryRun: Boolean(this.dryRun), contact })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Subcommand: Send a message to a sharded chat repository.
|
|
74
|
+
*/
|
|
75
|
+
export class SendCommand extends ModelAsApp {
|
|
76
|
+
static alias = 'send'
|
|
77
|
+
static UI = { sent: 'Message saved: {uri}' }
|
|
78
|
+
static repo = { help: 'Explicit repository URI', default: '' }
|
|
79
|
+
static chatId = { help: 'Chat ID for prefix sharding', default: '' }
|
|
80
|
+
static author = { help: 'Author soulId', default: '' }
|
|
81
|
+
static text = { help: 'Message text', default: '' }
|
|
82
|
+
static timestamp = { help: 'ISO timestamp', default: '' }
|
|
83
|
+
static push = { help: 'Commit and push via Git', default: true }
|
|
84
|
+
static skipIndex = { help: 'Skip updating README.md index', default: true }
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {Partial<SendCommand>} [data]
|
|
88
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* @param {Partial<ViewCommand>} [data]
|
|
92
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
93
|
+
*/
|
|
94
|
+
/**
|
|
95
|
+
* @param {Partial<SyncCommand>} [data]
|
|
96
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
97
|
+
*/
|
|
98
|
+
constructor(data = {}, options = {}) {
|
|
99
|
+
super(data, options)
|
|
100
|
+
/** @type {string} Explicit repository URI */ this.repo
|
|
101
|
+
/** @type {string} Chat ID for prefix sharding */ this.chatId
|
|
102
|
+
/** @type {string} Author soulId */ this.author
|
|
103
|
+
/** @type {string} Message text */ this.text
|
|
104
|
+
/** @type {string} ISO timestamp */ this.timestamp
|
|
105
|
+
/** @type {boolean} Commit and push via Git */ this.push
|
|
106
|
+
/** @type {boolean} Skip updating README.md index */ this.skipIndex
|
|
107
|
+
/** @type {any} Optional Git transport port */ this.$git = options.git || null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async *run() {
|
|
111
|
+
const targetRepo = resolveTargetRepo(this.repo, this.chatId)
|
|
112
|
+
const opts = { db: this.$db, git: this.$git }
|
|
113
|
+
const chat = new ChatRepoModel({ repo: targetRepo }, opts)
|
|
114
|
+
const msg = await chat.sendMessage({
|
|
115
|
+
author: this.author,
|
|
116
|
+
text: this.text,
|
|
117
|
+
timestamp: this.timestamp || new Date().toISOString(),
|
|
118
|
+
skipIndex: Boolean(this.skipIndex),
|
|
119
|
+
})
|
|
120
|
+
const sync = new PeerGitSyncModel({ repo: targetRepo }, opts)
|
|
121
|
+
await sync.recordPeerCommit({ soulId: this.author, messageUri: msg.uri })
|
|
122
|
+
if (this.push && this.$git) {
|
|
123
|
+
await sync.pushMessage({ uri: msg.uri, author: this.author })
|
|
124
|
+
}
|
|
125
|
+
yield show(this._.t(SendCommand.UI.sent, { uri: msg.uri }), 'success')
|
|
126
|
+
return result({ ok: true, message: msg })
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Subcommand: View messages for a given date.
|
|
132
|
+
*/
|
|
133
|
+
export class ViewCommand extends ModelAsApp {
|
|
134
|
+
static alias = 'view'
|
|
135
|
+
static repo = { help: 'Explicit repository URI', default: '' }
|
|
136
|
+
static chatId = { help: 'Chat ID for prefix sharding', default: '' }
|
|
137
|
+
static date = { help: 'Date YYYY-MM-DD', default: '' }
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {Partial<ViewCommand>} [data]
|
|
141
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
142
|
+
*/
|
|
143
|
+
constructor(data = {}, options = {}) {
|
|
144
|
+
super(data, options)
|
|
145
|
+
/** @type {string} Explicit repository URI */ this.repo
|
|
146
|
+
/** @type {string} Chat ID for prefix sharding */ this.chatId
|
|
147
|
+
/** @type {string} Date YYYY-MM-DD */ this.date
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async *run() {
|
|
151
|
+
const targetRepo = resolveTargetRepo(this.repo, this.chatId)
|
|
152
|
+
const chat = new ChatRepoModel({ repo: targetRepo }, { db: this.$db })
|
|
153
|
+
const dateStr = this.date || new Date().toISOString().slice(0, 10)
|
|
154
|
+
const messages = await chat.listDay(dateStr)
|
|
155
|
+
if (messages.length === 0) {
|
|
156
|
+
yield show(`No messages for ${dateStr}`, 'info')
|
|
157
|
+
} else {
|
|
158
|
+
for (const msg of messages) {
|
|
159
|
+
yield show(`[${msg.timestamp}] ${msg.author}: ${msg.text}`, 'info')
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return result({ ok: true, date: dateStr, messages })
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Subcommand: Crystallize a chat message into a document or todo.md.
|
|
168
|
+
*/
|
|
169
|
+
export class CrystallizeCommand extends ModelAsApp {
|
|
170
|
+
static alias = 'crystallize'
|
|
171
|
+
static UI = { crystallized: 'Crystallized into {targetDocUri}' }
|
|
172
|
+
static msgUri = { help: 'Source message URI', default: '' }
|
|
173
|
+
static targetDocUri = { help: 'Target document URI', default: '' }
|
|
174
|
+
static kind = { help: 'Entry kind: todo | note', default: 'todo' }
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @param {Partial<CrystallizeCommand>} [data]
|
|
178
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
179
|
+
*/
|
|
180
|
+
constructor(data = {}, options = {}) {
|
|
181
|
+
super(data, options)
|
|
182
|
+
/** @type {string} Source message URI */ this.msgUri
|
|
183
|
+
/** @type {string} Target document URI */ this.targetDocUri
|
|
184
|
+
/** @type {string} Entry kind */ this.kind
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async *run() {
|
|
188
|
+
const crystallizer = new CrystallizeModel({}, { db: this.$db })
|
|
189
|
+
const entry = await crystallizer.crystallize({
|
|
190
|
+
msgUri: this.msgUri,
|
|
191
|
+
targetDocUri: this.targetDocUri,
|
|
192
|
+
kind: this.kind === 'todo' ? 'todo' : 'note',
|
|
193
|
+
})
|
|
194
|
+
yield show(this._.t(CrystallizeCommand.UI.crystallized, { targetDocUri: this.targetDocUri }), 'success')
|
|
195
|
+
return result({ ok: true, entry })
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Subcommand: Inspect peer branch sync state.
|
|
201
|
+
*/
|
|
202
|
+
export class SyncCommand extends ModelAsApp {
|
|
203
|
+
static alias = 'sync'
|
|
204
|
+
static UI = { synced: 'Sync state: {count} peer branches' }
|
|
205
|
+
static repo = { help: 'Explicit repository URI', default: '' }
|
|
206
|
+
static chatId = { help: 'Chat ID for prefix sharding', default: '' }
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param {Partial<SendCommand>} [data]
|
|
210
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
211
|
+
*/
|
|
212
|
+
/**
|
|
213
|
+
* @param {Partial<ViewCommand>} [data]
|
|
214
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
215
|
+
*/
|
|
216
|
+
/**
|
|
217
|
+
* @param {Partial<SyncCommand>} [data]
|
|
218
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
219
|
+
*/
|
|
220
|
+
constructor(data = {}, options = {}) {
|
|
221
|
+
super(data, options)
|
|
222
|
+
/** @type {string} Explicit repository URI */ this.repo
|
|
223
|
+
/** @type {string} Chat ID for prefix sharding */ this.chatId
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async *run() {
|
|
227
|
+
const targetRepo = resolveTargetRepo(this.repo, this.chatId)
|
|
228
|
+
const sync = new PeerGitSyncModel({ repo: targetRepo }, { db: this.$db })
|
|
229
|
+
const state = await sync.getSyncState()
|
|
230
|
+
const count = Object.keys(state.branches).length
|
|
231
|
+
yield show(this._.t(SyncCommand.UI.synced, { count }), 'info')
|
|
232
|
+
return result({ ok: true, state })
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Subcommand: Poll remote Git repository for new messages and notify.
|
|
238
|
+
*/
|
|
239
|
+
export class WatchCommand extends ModelAsApp {
|
|
240
|
+
static alias = 'watch'
|
|
241
|
+
static UI = {
|
|
242
|
+
watching: 'Watching {repo} every {interval}s...',
|
|
243
|
+
fetching: 'Fetching {repo}...',
|
|
244
|
+
waiting: 'Waiting {interval}s (last check {time})...',
|
|
245
|
+
upToDate: 'Up to date ({time})',
|
|
246
|
+
received: '[{time}] {author}: {text}',
|
|
247
|
+
syncWarning: 'Sync warning: {message}',
|
|
248
|
+
}
|
|
249
|
+
static repo = { help: 'Explicit repository URI', default: '.' }
|
|
250
|
+
static chatId = { help: 'Chat ID for prefix sharding', default: '' }
|
|
251
|
+
static interval = { help: 'Polling interval in seconds (10-15 recommended)', default: 12 }
|
|
252
|
+
static once = { help: 'Run a single poll tick and exit', default: false }
|
|
253
|
+
static maxTicks = { help: 'Maximum polling ticks (0 = unlimited)', default: 0 }
|
|
254
|
+
static limit = { help: 'Initial history messages count', default: 10 }
|
|
255
|
+
static author = { help: 'Current user author name for highlighting own messages', default: '' }
|
|
256
|
+
static color = { help: 'Colorize author names in terminal', default: true }
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @param {Partial<WatchCommand>} [data]
|
|
260
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions> & { git?: any, notify?: any }} [options]
|
|
261
|
+
*/
|
|
262
|
+
/**
|
|
263
|
+
* @param {Partial<SendCommand>} [data]
|
|
264
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
265
|
+
*/
|
|
266
|
+
/**
|
|
267
|
+
* @param {Partial<ViewCommand>} [data]
|
|
268
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
269
|
+
*/
|
|
270
|
+
/**
|
|
271
|
+
* @param {Partial<SyncCommand>} [data]
|
|
272
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
273
|
+
*/
|
|
274
|
+
constructor(data = {}, options = {}) {
|
|
275
|
+
super(data, options)
|
|
276
|
+
/** @type {string} Explicit repository URI */ this.repo
|
|
277
|
+
/** @type {string} Chat ID for prefix sharding */ this.chatId
|
|
278
|
+
/** @type {number} Polling interval in seconds */ this.interval
|
|
279
|
+
/** @type {boolean} Run a single poll tick and exit */ this.once
|
|
280
|
+
/** @type {number} Maximum polling ticks */ this.maxTicks
|
|
281
|
+
/** @type {number} Initial history messages count */ this.limit
|
|
282
|
+
/** @type {string} Current user author name */ this.author
|
|
283
|
+
/** @type {boolean} Colorize author names in terminal */ this.color
|
|
284
|
+
/** @type {any} Optional Git transport port */ this.$git = options.git || null
|
|
285
|
+
/** @type {any} Optional Notifier port */ this.$notify = options.notify || null
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Formats a sync error into a concise, actionable warning message.
|
|
290
|
+
* @param {any} err
|
|
291
|
+
* @returns {string}
|
|
292
|
+
*/
|
|
293
|
+
#formatSyncWarning(err) {
|
|
294
|
+
const msg = String(err?.message || 'Sync failed').split('\n')[0]
|
|
295
|
+
if (err?.hint && !msg.includes(err.hint)) {
|
|
296
|
+
return `${msg} (${err.hint})`
|
|
297
|
+
}
|
|
298
|
+
return msg
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Formats ISO timestamp into HH:MM.
|
|
303
|
+
* @param {string} [timestamp]
|
|
304
|
+
* @returns {string}
|
|
305
|
+
*/
|
|
306
|
+
#formatTime(timestamp) {
|
|
307
|
+
const match = String(timestamp || '').match(/T(\d{2}:\d{2})/)
|
|
308
|
+
if (match) return match[1]
|
|
309
|
+
return new Date().toTimeString().slice(0, 5)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Applies a deterministic ANSI color to an author name.
|
|
314
|
+
* @param {string} author
|
|
315
|
+
* @returns {string}
|
|
316
|
+
*/
|
|
317
|
+
#colorizeAuthor(author) {
|
|
318
|
+
if (!this.color) return author
|
|
319
|
+
const palette = ['\x1b[36m', '\x1b[32m', '\x1b[33m', '\x1b[35m', '\x1b[34m', '\x1b[96m']
|
|
320
|
+
let hash = 0
|
|
321
|
+
for (let i = 0; i < author.length; i += 1) {
|
|
322
|
+
hash = (hash * 31 + author.charCodeAt(i)) >>> 0
|
|
323
|
+
}
|
|
324
|
+
const code = palette[hash % palette.length]
|
|
325
|
+
return `${code}${author}\x1b[0m`
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Formats a message line with visual separation for own vs peer messages.
|
|
330
|
+
* @param {{ time: string, author: string, text: string }} msg
|
|
331
|
+
* @returns {{ line: string, level: 'info' | 'success' }}
|
|
332
|
+
*/
|
|
333
|
+
#formatMessageLine({ time, author, text }) {
|
|
334
|
+
const isOwn = Boolean(this.author && author === this.author)
|
|
335
|
+
const prefix = isOwn ? ' → ' : '◀ '
|
|
336
|
+
const displayAuthor = this.#colorizeAuthor(author)
|
|
337
|
+
const body = this._.t(WatchCommand.UI.received, { time, author: displayAuthor, text })
|
|
338
|
+
return {
|
|
339
|
+
line: `\n${prefix}${body}`,
|
|
340
|
+
level: isOwn ? 'info' : 'success',
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Yields initial history messages for today without triggering OS notifications.
|
|
346
|
+
* @param {ChatRepoModel} chat
|
|
347
|
+
* @param {Set<string>} seen
|
|
348
|
+
*/
|
|
349
|
+
async *#showInitialHistory(chat, seen) {
|
|
350
|
+
const today = new Date().toISOString().slice(0, 10)
|
|
351
|
+
const messages = await chat.listDay(today)
|
|
352
|
+
for (const msg of messages) {
|
|
353
|
+
seen.add(msg.uri)
|
|
354
|
+
}
|
|
355
|
+
const maxHistory = Math.max(0, Number(this.limit ?? 10))
|
|
356
|
+
const slice = maxHistory > 0 ? messages.slice(-maxHistory) : []
|
|
357
|
+
for (const msg of slice) {
|
|
358
|
+
const { line, level } = this.#formatMessageLine({
|
|
359
|
+
time: this.#formatTime(msg.timestamp),
|
|
360
|
+
author: msg.author || 'peer',
|
|
361
|
+
text: msg.text || msg.uri,
|
|
362
|
+
})
|
|
363
|
+
yield show(line, level)
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Loads, yields, and notifies newly pulled messages.
|
|
369
|
+
* @param {string[]} newFiles
|
|
370
|
+
* @param {any[]} received
|
|
371
|
+
* @param {Set<string>} seen
|
|
372
|
+
*/
|
|
373
|
+
async *#deliverMessages(newFiles, received, seen) {
|
|
374
|
+
for (const uri of newFiles) {
|
|
375
|
+
if (seen.has(uri)) continue
|
|
376
|
+
seen.add(uri)
|
|
377
|
+
const raw = await this.$db.loadDocument(uri, '')
|
|
378
|
+
const parsed = parseMessageMarkdown(String(raw))
|
|
379
|
+
received.push({ uri, ...parsed })
|
|
380
|
+
const { line, level } = this.#formatMessageLine({
|
|
381
|
+
time: this.#formatTime(parsed.timestamp),
|
|
382
|
+
author: parsed.author || 'peer',
|
|
383
|
+
text: parsed.text || uri,
|
|
384
|
+
})
|
|
385
|
+
yield show(line, level)
|
|
386
|
+
if (this.$notify) {
|
|
387
|
+
await this.$notify.notify({
|
|
388
|
+
title: parsed.author || 'New message',
|
|
389
|
+
body: parsed.text || uri,
|
|
390
|
+
})
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Yields any new local messages not yet seen in the stream.
|
|
397
|
+
* @param {ChatRepoModel} chat
|
|
398
|
+
* @param {any[]} received
|
|
399
|
+
* @param {Set<string>} seen
|
|
400
|
+
*/
|
|
401
|
+
async *#deliverLocalMessages(chat, received, seen) {
|
|
402
|
+
const today = new Date().toISOString().slice(0, 10)
|
|
403
|
+
const messages = await chat.listDay(today)
|
|
404
|
+
for (const msg of messages) {
|
|
405
|
+
if (seen.has(msg.uri)) continue
|
|
406
|
+
seen.add(msg.uri)
|
|
407
|
+
received.push(msg)
|
|
408
|
+
const { line, level } = this.#formatMessageLine({
|
|
409
|
+
time: this.#formatTime(msg.timestamp),
|
|
410
|
+
author: msg.author || 'me',
|
|
411
|
+
text: msg.text || msg.uri,
|
|
412
|
+
})
|
|
413
|
+
yield show(line, level)
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Waits between poll ticks while updating spinner countdown every second.
|
|
419
|
+
* @param {string} now
|
|
420
|
+
*/
|
|
421
|
+
async *#waitWithCountdown(now) {
|
|
422
|
+
let remainingMs = Math.max(0, Number(this.interval || 12) * 1000)
|
|
423
|
+
while (remainingMs > 0) {
|
|
424
|
+
const sec = Math.ceil(remainingMs / 1000)
|
|
425
|
+
yield progress(this._.t(WatchCommand.UI.waiting, { interval: sec, time: now }), 0, {
|
|
426
|
+
type: 'spinner',
|
|
427
|
+
id: 'watch',
|
|
428
|
+
})
|
|
429
|
+
const step = Math.min(1000, remainingMs)
|
|
430
|
+
await new Promise((r) => setTimeout(r, step))
|
|
431
|
+
remainingMs -= step
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Polls remote Git repository and yields received messages.
|
|
437
|
+
* @returns {AsyncGenerator<import('@nan0web/ui').Intent, any, any>}
|
|
438
|
+
*/
|
|
439
|
+
async *run() {
|
|
440
|
+
const targetRepo = resolveTargetRepo(this.repo, this.chatId)
|
|
441
|
+
const chat = new ChatRepoModel({ repo: targetRepo }, { db: this.$db })
|
|
442
|
+
const sync = new PeerGitSyncModel({ repo: targetRepo }, { db: this.$db, git: this.$git })
|
|
443
|
+
yield show(this._.t(WatchCommand.UI.watching, { repo: targetRepo, interval: this.interval }), 'info')
|
|
444
|
+
|
|
445
|
+
const seen = new Set()
|
|
446
|
+
yield* this.#showInitialHistory(chat, seen)
|
|
447
|
+
yield show('', 'info')
|
|
448
|
+
|
|
449
|
+
const received = []
|
|
450
|
+
const limit = this.once ? 1 : Number(this.maxTicks || 0)
|
|
451
|
+
let ticks = 0
|
|
452
|
+
/** @type {any} */
|
|
453
|
+
let lastError = null
|
|
454
|
+
|
|
455
|
+
while (limit === 0 || ticks < limit) {
|
|
456
|
+
ticks += 1
|
|
457
|
+
const now = new Date().toTimeString().slice(0, 8)
|
|
458
|
+
yield progress(this._.t(WatchCommand.UI.fetching, { repo: targetRepo }), 0, { type: 'spinner', id: 'watch' })
|
|
459
|
+
try {
|
|
460
|
+
const newFiles = await sync.pullUpdates()
|
|
461
|
+
lastError = null
|
|
462
|
+
if (newFiles.length > 0) {
|
|
463
|
+
yield progress('', 0, { type: 'spinner', stop: 'success', id: 'watch' })
|
|
464
|
+
yield* this.#deliverMessages(newFiles, received, seen)
|
|
465
|
+
yield show('', 'info')
|
|
466
|
+
}
|
|
467
|
+
yield* this.#deliverLocalMessages(chat, received, seen)
|
|
468
|
+
if (newFiles.length === 0 && limit > 0 && ticks >= limit) {
|
|
469
|
+
yield progress(this._.t(WatchCommand.UI.upToDate, { time: now }), 0, { type: 'spinner', stop: 'success', id: 'watch' })
|
|
470
|
+
}
|
|
471
|
+
} catch (err) {
|
|
472
|
+
lastError = err
|
|
473
|
+
yield progress('', 0, { type: 'spinner', stop: true, id: 'watch' })
|
|
474
|
+
yield show(this._.t(WatchCommand.UI.syncWarning, { message: this.#formatSyncWarning(err) }), 'warn')
|
|
475
|
+
}
|
|
476
|
+
if (limit === 0 || ticks < limit) {
|
|
477
|
+
yield* this.#waitWithCountdown(now)
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (this.once && lastError) {
|
|
482
|
+
return result({
|
|
483
|
+
ok: false,
|
|
484
|
+
error: String(lastError?.message || 'Sync failed'),
|
|
485
|
+
code: lastError?.code || 'SYNC_ERROR',
|
|
486
|
+
hint: lastError?.hint || '',
|
|
487
|
+
received,
|
|
488
|
+
})
|
|
489
|
+
}
|
|
490
|
+
return result({ ok: true, received })
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Main polymorphic OLMUI application controller for @nan0web/communication.app.
|
|
496
|
+
*/
|
|
497
|
+
export class CommunicationApp extends ModelAsApp {
|
|
498
|
+
static alias = 'nan0com'
|
|
499
|
+
static UI = { title: '7iOS.CoCreation — Sovereign Git Messenger' }
|
|
500
|
+
|
|
501
|
+
static command = {
|
|
502
|
+
help: 'Command to execute',
|
|
503
|
+
options: [InitCommand, SendCommand, ViewCommand, CrystallizeCommand, SyncCommand, WatchCommand],
|
|
504
|
+
positional: true,
|
|
505
|
+
default: ViewCommand,
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* @param {Partial<CommunicationApp>} [data]
|
|
510
|
+
* @param {Partial<import('@nan0web/ui').ModelAsAppOptions>} [options]
|
|
511
|
+
*/
|
|
512
|
+
constructor(data = {}, options = {}) {
|
|
513
|
+
super(data, options)
|
|
514
|
+
/** @type {ModelAsApp} Active subcommand instance */ this.command
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Delegates execution polymorphically to the active subcommand model.
|
|
519
|
+
* @returns {AsyncGenerator<import('@nan0web/ui').Intent, any, any>}
|
|
520
|
+
*/
|
|
521
|
+
async *run() {
|
|
522
|
+
if (this.help || !this.command || typeof this.command.run !== 'function') {
|
|
523
|
+
return yield* super.run()
|
|
524
|
+
}
|
|
525
|
+
return yield* this.command.run()
|
|
526
|
+
}
|
|
527
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ModelError } from '@nan0web/types'
|
|
2
|
+
import { ModelAsApp } from '@nan0web/ui'
|
|
3
|
+
import { parseMessageMarkdown } from './ChatRepoModel.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Formats a crystallized entry for todo.md or docs/*.md with $ref and authorship.
|
|
7
|
+
* @param {{ text: string, author: string, msgUri: string, kind: string }} data
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
function formatCrystallizedEntry({ text, author, msgUri, kind }) {
|
|
11
|
+
if (kind === 'todo') {
|
|
12
|
+
return `- [ ] ${text} [author:${author}] [$ref:${msgUri}]`
|
|
13
|
+
}
|
|
14
|
+
return `> ${text}\n>\n> — **${author}** ([$ref](${msgUri}))`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Crystallizes ephemeral chat messages into structured project documents or todo.md.
|
|
19
|
+
*/
|
|
20
|
+
export class CrystallizeModel extends ModelAsApp {
|
|
21
|
+
static UI = {
|
|
22
|
+
errorNoDb: 'Database instance ($db) is required',
|
|
23
|
+
errorMsgNotFound: 'Source message not found',
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Moves/projects a chat message into targetDocUri preserving authorship and $ref.
|
|
28
|
+
* @param {{ msgUri: string, targetDocUri: string, kind?: 'todo' | 'note' }} input
|
|
29
|
+
* @returns {Promise<{ author: string, $ref: string, targetDocUri: string, kind: string, text: string }>}
|
|
30
|
+
*/
|
|
31
|
+
async crystallize(input) {
|
|
32
|
+
const { msgUri, targetDocUri, kind = 'note' } = input || {}
|
|
33
|
+
const stat = await this.$db.stat(msgUri)
|
|
34
|
+
if (!stat || !stat.exists) {
|
|
35
|
+
throw new ModelError({ msgUri: `${CrystallizeModel.UI.errorMsgNotFound}: ${msgUri}` })
|
|
36
|
+
}
|
|
37
|
+
const rawMsg = await this.$db.loadDocument(msgUri, '')
|
|
38
|
+
const parsed = parseMessageMarkdown(String(rawMsg))
|
|
39
|
+
const entryText = formatCrystallizedEntry({
|
|
40
|
+
text: parsed.text,
|
|
41
|
+
author: parsed.author,
|
|
42
|
+
msgUri,
|
|
43
|
+
kind,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const existingDoc = await this.$db.loadDocument(targetDocUri, '')
|
|
47
|
+
const prefix = existingDoc ? `${String(existingDoc).trimEnd()}\n` : ''
|
|
48
|
+
await this.$db.saveDocument(targetDocUri, `${prefix}${entryText}\n`)
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
author: parsed.author,
|
|
52
|
+
$ref: msgUri,
|
|
53
|
+
targetDocUri,
|
|
54
|
+
kind,
|
|
55
|
+
text: parsed.text,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|