rvlib-shipkit 2.12.0 → 2.14.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.
- package/CHANGELOG.md +75 -21
- package/README.md +1 -1
- package/package.json +4 -2
- package/src/agent-time/store.ts +2 -2
- package/src/assets/run-assets.ts +2 -2
- package/src/assets/server/studio-server.ts +27 -23
- package/src/check.ts +4 -4
- package/src/checks/SK001-package-json-shape.check.ts +8 -4
- package/src/checks/SK002-contracts-fulfillness.check.ts +2 -2
- package/src/checks/SK003-claude-md-shape.check.ts +20 -13
- package/src/checks/SK004-gitignore-policy.check.ts +45 -6
- package/src/checks/SK005-rvlib-versions.check.ts +2 -2
- package/src/checks/SK006-npm-publish-safety.check.ts +3 -3
- package/src/checks/SK007-github-visibility.check.ts +2 -2
- package/src/checks/SK008-reviewer-agent.check.ts +3 -3
- package/src/checks/SK010-undeclared-unit.check.ts +2 -2
- package/src/checks/SK011-sibling-unit-ranges.check.ts +2 -2
- package/src/checks/SK012-config-barrel-import.check.ts +2 -2
- package/src/checks/SK013-one-way-in-policy.check.ts +2 -2
- package/src/checks/SK015-nested-repo.check.ts +3 -3
- package/src/checks/SK016-legacy-check-shapes.check.ts +2 -2
- package/src/checks/SK017-vscode-hub-task.check.ts +2 -2
- package/src/checks/SK018-test-files-run.check.ts +2 -2
- package/src/checks/SK019-tests-run-in-units.check.ts +2 -2
- package/src/checks/SK020-self-spawning-check.check.ts +2 -2
- package/src/checks/SK022-machine-runs-the-pin.check.ts +2 -2
- package/src/checks/SK024-identity-colors.check.ts +1 -1
- package/src/checks/SK025-release-drift.check.ts +2 -2
- package/src/checks/SK026-asks-autoclose.check.ts +2 -2
- package/src/checks/SK027-agent-wiki-links.check.ts +8 -2
- package/src/checks/SK028-readme.check.ts +2 -2
- package/src/checks/SK029-explicit-check-files.check.ts +3 -3
- package/src/checks/SK030-cache-audit.check.ts +7 -5
- package/src/checks/SK031-trace-audit.check.ts +7 -7
- package/src/checks/SK032-ignored-inputs.check.ts +3 -3
- package/src/checks/SK033-global-contribution.check.ts +11 -4
- package/src/checks/SK034-commit-gates-armed.check.ts +2 -2
- package/src/checks/SK035-public-clean.check.ts +46 -0
- package/src/checks/SK036-draft-media-size.check.ts +28 -0
- package/src/checks/SK037-keyword-additions.check.ts +43 -0
- package/src/cli/cmd_Backup.ts +152 -0
- package/src/cli/cmd_Ci.ts +8 -11
- package/src/cli/cmd_Commands.ts +4 -4
- package/src/cli/cmd_ConfigKeys.ts +2 -2
- package/src/cli/cmd_Drill.ts +145 -0
- package/src/cli/cmd_Home.ts +50 -3
- package/src/cli/cmd_Init.ts +1 -1
- package/src/cli/cmd_Media.ts +10 -8
- package/src/cli/cmd_Mv.ts +14 -7
- package/src/cli/cmd_Review.ts +6 -4
- package/src/cli/cmd_Shot.ts +5 -2
- package/src/cli/cmd_Smoke.ts +5 -2
- package/src/cli/cmd_Social.ts +226 -0
- package/src/cli/cmd_Sound.ts +1 -1
- package/src/cli/cmd_Start.ts +1 -1
- package/src/cli/cmd_Time.ts +1 -1
- package/src/cli/cmd_TraceAudit.ts +6 -6
- package/src/cli/cmd_Trailer.ts +7 -4
- package/src/cli/cmd_Veille.ts +173 -0
- package/src/cli/cmd_When.ts +2 -2
- package/src/cli/family-pages.ts +19 -16
- package/src/cli/registry.ts +13 -2
- package/src/cli.ts +5 -2
- package/src/collections/actions.ts +1 -49
- package/src/collections/checks.ts +2 -1
- package/src/collections/collection-types.ts +12 -3
- package/src/collections/providers.ts +4 -4
- package/src/collections/review.ts +3 -2
- package/src/collections/tests.ts +19 -8
- package/src/comfy/comfy-panel.ts +2 -2
- package/src/contracts/apply-contracts.ts +1 -3
- package/src/contracts/contract-manifests.ts +2 -3
- package/src/contracts/contract-types.ts +0 -1
- package/src/contracts/contracts-engine.ts +0 -3
- package/src/contracts/manifests/discord.contract.ts +1 -1
- package/src/contracts/manifests/identity.contract.ts +1 -1
- package/src/contracts/manifests/itch.contract.ts +1 -1
- package/src/contracts/manifests/steam.contract.ts +1 -1
- package/src/core/agent-dir-at.ts +12 -0
- package/src/core/agent-dir.ts +16 -0
- package/src/core/app-icon.ts +9 -3
- package/src/core/budget.ts +1 -1
- package/src/core/bun-builtins.ts +2 -2
- package/src/core/canonical-scripts.ts +2 -2
- package/src/core/discover-checks.ts +38 -6
- package/src/core/fix-scope.ts +4 -2
- package/src/core/floor-resolve.ts +68 -48
- package/src/core/floor.ts +9 -0
- package/src/core/git-root.ts +24 -0
- package/src/core/global-contribution.ts +4 -1
- package/src/core/ignored-block.ts +1 -1
- package/src/core/ignored-inputs.ts +3 -2
- package/src/core/last-good.ts +3 -1
- package/src/core/layout.ts +8 -0
- package/src/core/log-stamp.ts +36 -0
- package/src/core/loop-watch.ts +93 -0
- package/src/core/machine-link.ts +134 -0
- package/src/core/memory-watch.ts +41 -0
- package/src/core/motherhome.ts +2 -2
- package/src/core/one-way-in-policy.ts +34 -26
- package/src/core/run-ci.ts +9 -11
- package/src/core/run-fixers.ts +9 -9
- package/src/core/runs-matrix.ts +1 -1
- package/src/core/slots.ts +30 -0
- package/src/core/trace-audit.ts +50 -9
- package/src/core/tree-rev.ts +60 -0
- package/src/core/unit-files.ts +42 -0
- package/src/drill/render.ts +140 -0
- package/src/drill/run.ts +269 -0
- package/src/fleet/scan-fleet.ts +6 -5
- package/src/home/daemon-jobs.ts +201 -0
- package/src/home/global-context.ts +44 -8
- package/src/home/gui-app/ShipkitGui.swift +91 -2
- package/src/home/home-routes.ts +22 -15
- package/src/home/hubs.ts +10 -3
- package/src/home/inbox-shape.ts +54 -5
- package/src/home/inbox.ts +17 -2
- package/src/home/lib-watch.ts +10 -3
- package/src/home/machine-stores.ts +15 -29
- package/src/home/pm-config.ts +4 -1
- package/src/home/pm-inputs.ts +2 -0
- package/src/home/resources.ts +7 -2
- package/src/home/run-daemon.ts +15 -18
- package/src/home/run-pm.ts +3 -1
- package/src/home/serve-daemon.ts +4 -3
- package/src/home/serve-web.ts +104 -1
- package/src/hooks/commit-message.ts +29 -20
- package/src/hooks/hook-gates.ts +1 -1
- package/src/hooks/leak-guard.ts +69 -9
- package/src/hooks/run-hooks.ts +18 -22
- package/src/identities/identities.ts +334 -0
- package/src/identities/machine-route.ts +126 -0
- package/src/identities/machine.ts +62 -0
- package/src/init-repo.ts +3 -3
- package/src/ledger/check-stats.ts +10 -9
- package/src/ledger/gate-checks.ts +32 -1
- package/src/ledger/record-kept.ts +112 -0
- package/src/ledger/repo-view.ts +10 -0
- package/src/ledger/run-log.ts +17 -12
- package/src/ledger/run-timeline.ts +8 -4
- package/src/ledger/store.ts +7 -0
- package/src/ledger/suite-log.ts +17 -13
- package/src/media-kinds.ts +3 -2
- package/src/media-store/backup/daemon.ts +73 -0
- package/src/media-store/backup/keys.ts +25 -0
- package/src/media-store/backup/manifest.ts +193 -0
- package/src/media-store/backup/restore.ts +192 -0
- package/src/media-store/backup/run.ts +278 -0
- package/src/media-store/backup/secret.ts +86 -0
- package/src/media-store/backup/snapshot.ts +64 -0
- package/src/media-store/backup/sources.ts +199 -0
- package/src/media-store/content.ts +1 -1
- package/src/media-store/daemon.ts +56 -70
- package/src/media-store/db.ts +1 -1
- package/src/media-store/fact.ts +1 -1
- package/src/media-store/hand.ts +1 -1
- package/src/media-store/kind.ts +1 -1
- package/src/media-store/purge.ts +1 -1
- package/src/media-store/put.ts +5 -3
- package/src/media-store/remote.ts +1 -1
- package/src/media-store/space.ts +24 -5
- package/src/mv/mv-md.ts +63 -22
- package/src/pages/forward.ts +35 -0
- package/src/pages/frame-client.ts +118 -0
- package/src/pages/routes.ts +29 -0
- package/src/pages/unit.ts +92 -0
- package/src/plan/planner.ts +12 -0
- package/src/plan/repo-source.ts +19 -5
- package/src/reflect/transcript.ts +35 -10
- package/src/release/public-clean.ts +319 -0
- package/src/release/published-here.ts +22 -0
- package/src/release/run-bump.ts +3 -2
- package/src/release/run-release.ts +7 -4
- package/src/repo-config.ts +71 -13
- package/src/review/anim.ts +1 -1
- package/src/review/auto.ts +1 -1
- package/src/review/cells.ts +15 -1
- package/src/review/format.ts +2 -2
- package/src/review/http.ts +16 -2
- package/src/review/marks.ts +1 -1
- package/src/review/media-kind.ts +1 -1
- package/src/review/media-read.ts +1 -1
- package/src/review/model.ts +27 -6
- package/src/review/notes.ts +125 -0
- package/src/review/previews.ts +1 -1
- package/src/review/project.ts +7 -39
- package/src/review/role.ts +1 -1
- package/src/review/routes.ts +18 -14
- package/src/review/store.ts +42 -2
- package/src/review/threads.ts +1 -1
- package/src/review/trailer-desk.ts +3 -3
- package/src/review/trailer-media.ts +8 -5
- package/src/review/trailer-model.ts +1 -1
- package/src/review/trailer-routes.ts +1 -1
- package/src/review/trailer-shots.ts +22 -5
- package/src/review/unit.ts +2 -2
- package/src/scaffold/files/agent-handoff.md +3 -0
- package/src/scaffold/files/agent-index.md +4 -12
- package/src/scaffold/files/tsconfig.json +1 -1
- package/src/scaffold/project-scaffold.ts +6 -5
- package/src/serve/embed.ts +2 -2
- package/src/serve/hub-app/check-words.ts +1 -1
- package/src/serve/hub-app/col-card.tsx +3 -17
- package/src/serve/hub-app/col-tables-view.tsx +2 -1
- package/src/serve/hub-app/cols-svc.ts +0 -13
- package/src/serve/hub-app/dock-st.ts +4 -2
- package/src/serve/hub-app/dock-view.tsx +2 -0
- package/src/serve/hub-app/frames-st.ts +35 -0
- package/src/serve/hub-app/hash-route.ts +64 -1
- package/src/serve/hub-app/home-st.ts +32 -8
- package/src/serve/hub-app/home-strip.css +9 -1
- package/src/serve/hub-app/home-strip.tsx +7 -6
- package/src/serve/hub-app/home.html +5 -0
- package/src/serve/hub-app/hub-app.tsx +51 -7
- package/src/serve/hub-app/hub.css +29 -11
- package/src/serve/hub-app/icons.tsx +16 -1
- package/src/serve/hub-app/identities-st.ts +210 -0
- package/src/serve/hub-app/identities-view.tsx +353 -0
- package/src/serve/hub-app/identities.css +48 -0
- package/src/serve/hub-app/identity-address.ts +52 -0
- package/src/serve/hub-app/inbox-st.ts +60 -7
- package/src/serve/hub-app/inbox-view.tsx +49 -7
- package/src/serve/hub-app/inbox.css +12 -1
- package/src/serve/hub-app/index.html +3 -0
- package/src/serve/hub-app/kept-cols.tsx +30 -0
- package/src/serve/hub-app/keys.ts +10 -2
- package/src/serve/hub-app/list-walk.ts +89 -0
- package/src/serve/hub-app/machine-st.ts +12 -0
- package/src/serve/hub-app/media-svc.ts +1 -1
- package/src/serve/hub-app/media-view.tsx +2 -1
- package/src/serve/hub-app/pages-frames.tsx +134 -0
- package/src/serve/hub-app/pages-svc.ts +111 -0
- package/src/serve/hub-app/palette-st.ts +18 -3
- package/src/serve/hub-app/palette-view.tsx +9 -2
- package/src/serve/hub-app/planned-view.tsx +2 -1
- package/src/serve/hub-app/project-list.css +18 -14
- package/src/serve/hub-app/project-list.tsx +88 -49
- package/src/serve/hub-app/projects-svc.ts +33 -3
- package/src/serve/hub-app/repo-sections.ts +19 -2
- package/src/serve/hub-app/repo-st.ts +76 -20
- package/src/serve/hub-app/review/review-board.tsx +1 -1
- package/src/serve/hub-app/review/review-cells.tsx +6 -5
- package/src/serve/hub-app/review/review-kit.ts +30 -7
- package/src/serve/hub-app/review/review-page.tsx +6 -5
- package/src/serve/hub-app/review/review-st.ts +34 -15
- package/src/serve/hub-app/review/review-thread.tsx +63 -14
- package/src/serve/hub-app/review/review-views.tsx +133 -51
- package/src/serve/hub-app/review/thread-st.ts +80 -37
- package/src/serve/hub-app/review/trailer-page.tsx +7 -5
- package/src/serve/hub-app/review/trailer-st.ts +1 -1
- package/src/serve/hub-app/review.css +26 -9
- package/src/serve/hub-app/site/site-part.tsx +173 -0
- package/src/serve/hub-app/site/site-st.ts +157 -0
- package/src/serve/hub-app/site.css +35 -0
- package/src/serve/hub-app/social/calendar.tsx +182 -0
- package/src/serve/hub-app/social/comms-page.tsx +330 -0
- package/src/serve/hub-app/social/kit.tsx +244 -0
- package/src/serve/hub-app/social/post-card.tsx +456 -0
- package/src/serve/hub-app/social/problems.tsx +60 -0
- package/src/serve/hub-app/social/social-model.ts +154 -0
- package/src/serve/hub-app/social/social-page.tsx +681 -0
- package/src/serve/hub-app/social/social-st.ts +1014 -0
- package/src/serve/hub-app/social.css +289 -0
- package/src/serve/hub-app/stores/stores-page.tsx +312 -0
- package/src/serve/hub-app/stores/stores-st.ts +286 -0
- package/src/serve/hub-app/stores.css +54 -0
- package/src/serve/hub-app/timeline-geometry.ts +1 -1
- package/src/serve/hub-app/timeline-view.tsx +3 -2
- package/src/serve/hub-app/tree-build.ts +6 -0
- package/src/serve/hub-app/tree-row.tsx +27 -4
- package/src/serve/hub-app/tree-svc.ts +18 -49
- package/src/serve/hub-app/veille/veille-page.tsx +515 -0
- package/src/serve/hub-app/veille/veille-st.ts +443 -0
- package/src/serve/hub-app/veille.css +84 -0
- package/src/serve/hub-runtime.ts +20 -11
- package/src/serve/project-gutter-route.ts +44 -12
- package/src/serve/run-lanes.ts +7 -4
- package/src/serve/same-origin.ts +1 -1
- package/src/serve/serve-hub.ts +20 -12
- package/src/serve/tracked-watch.ts +24 -3
- package/src/serve/warm-cell.ts +5 -1
- package/src/shipkit-files.ts +15 -4
- package/src/shipkit.ts +0 -1
- package/src/shot/box.ts +160 -0
- package/src/shot/browser-slot.ts +74 -0
- package/src/shot/client.ts +42 -7
- package/src/shot/clip-encoder.ts +108 -24
- package/src/shot/film-box.ts +292 -0
- package/src/shot/shooter.ts +71 -17
- package/src/shot/shot-ask.ts +10 -2
- package/src/shot/stepped-clip.ts +5 -4
- package/src/site/desk.ts +242 -0
- package/src/site/routes.ts +66 -0
- package/src/site/unit.ts +62 -0
- package/src/social/brands.ts +22 -0
- package/src/social/card-drafts.ts +24 -0
- package/src/social/compose.ts +97 -0
- package/src/social/desk-index.ts +123 -0
- package/src/social/desk-wire.ts +64 -0
- package/src/social/drafts.ts +183 -0
- package/src/social/house.ts +24 -0
- package/src/social/load.ts +332 -0
- package/src/social/md.ts +70 -0
- package/src/social/media-facts.ts +53 -0
- package/src/social/media-kind.ts +189 -0
- package/src/social/media-limit.ts +42 -0
- package/src/social/media-of.ts +30 -0
- package/src/social/migrate.ts +123 -0
- package/src/social/open.ts +17 -0
- package/src/social/post-url.ts +158 -0
- package/src/social/presence.ts +332 -0
- package/src/social/public-read.ts +53 -0
- package/src/social/publish/bluesky.ts +168 -0
- package/src/social/publish/discord.ts +76 -0
- package/src/social/publish/index.ts +24 -0
- package/src/social/publish/secret.ts +13 -0
- package/src/social/push.ts +37 -0
- package/src/social/richtext.ts +37 -0
- package/src/social/routes.ts +186 -0
- package/src/social/stats.ts +134 -0
- package/src/social/text.ts +267 -0
- package/src/social/tracker-route.ts +39 -0
- package/src/social/tracker.ts +255 -0
- package/src/social/unit.ts +53 -0
- package/src/social/writes.ts +130 -0
- package/src/start-repo.ts +14 -9
- package/src/stores/collection.ts +27 -0
- package/src/stores/desk.ts +336 -0
- package/src/stores/model.ts +187 -0
- package/src/stores/routes.ts +126 -0
- package/src/stores/unit.ts +57 -0
- package/src/suite/calibrate.ts +127 -0
- package/src/suite/core-counters.ts +91 -0
- package/src/suite/judge-suites.ts +95 -35
- package/src/suite/suite-cli.ts +44 -10
- package/src/suite/suite-cpu-preload.ts +34 -9
- package/src/trace/tracer.ts +12 -5
- package/src/trailer/adapter.ts +5 -1
- package/src/trailer/build.ts +45 -12
- package/src/trailer/ffmpeg.ts +11 -0
- package/src/trailer/footage.ts +30 -13
- package/src/trailer/grammar.ts +18 -2
- package/src/trailer/graph.ts +28 -6
- package/src/trailer/holes.ts +1 -1
- package/src/trailer/passage.ts +18 -6
- package/src/trailer/plan.ts +74 -3
- package/src/trailer/prune.ts +1 -1
- package/src/trailer/states.ts +1 -1
- package/src/trailer/stitch.ts +67 -26
- package/src/trailer/url.ts +1 -1
- package/src/trailer/versions.ts +24 -0
- package/src/trailer/words.ts +1 -1
- package/src/ts/gate-typecheck.ts +32 -0
- package/src/ts/typecheck-cli.ts +9 -11
- package/src/tui/run-tui.ts +1 -1
- package/src/tui/tui-app.tsx +0 -1
- package/src/tui/tui-store.ts +1 -1
- package/src/veille/asks.ts +155 -0
- package/src/veille/auto.ts +31 -0
- package/src/veille/batch.ts +27 -0
- package/src/veille/chrome.ts +127 -0
- package/src/veille/collection.ts +28 -0
- package/src/veille/desk.ts +242 -0
- package/src/veille/identity.ts +80 -0
- package/src/veille/import.ts +68 -0
- package/src/veille/log.ts +183 -0
- package/src/veille/platforms.ts +118 -0
- package/src/veille/reads.ts +66 -0
- package/src/veille/replied.ts +110 -0
- package/src/veille/routes.ts +185 -0
- package/src/veille/service.ts +81 -0
- package/src/veille/watch.ts +116 -0
- package/src/veille/worth.ts +46 -0
- package/src/wiki/scan-md.ts +18 -0
- package/src/collections/posts.ts +0 -142
- package/src/contracts/manifests/social.contract.ts +0 -20
- package/src/scaffold/files/agent-coding.md +0 -35
- package/src/ts/typecheck-cache.ts +0 -11
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { handleKey } from './identity.ts'
|
|
4
|
+
import { fileKey, VeilleLog } from './log.ts'
|
|
5
|
+
import { type FeedRow, type Interaction, keyOf, type MediaPick, type Platform, PLATFORMS, type Replied, type Reply } from './platforms.ts'
|
|
6
|
+
import { kept, matches, type Watch, watch } from './watch.ts'
|
|
7
|
+
import { groupOf, parts, worthOf } from './worth.ts'
|
|
8
|
+
|
|
9
|
+
// one identity's desk (wiki tools/shipkit/veille.md): the log ranked, what he handled, the reply drafts and the asks for
|
|
10
|
+
// one. What `sk veille feed` prints and the hub's Veille page shows
|
|
11
|
+
/** `all`: the « vus ou expirés » too; else only the active groups */
|
|
12
|
+
type FeedQuery = { now?: number; platform?: Platform; sinceMs?: number; top?: number; all?: boolean; ours?: ReadonlySet<string>; watch?: Watch; asks?: Record<string, Interaction> }
|
|
13
|
+
|
|
14
|
+
/** `<platform>:<author>` the way our own accounts are written, to leave them out */
|
|
15
|
+
export const ourKey = handleKey
|
|
16
|
+
|
|
17
|
+
export class Desk {
|
|
18
|
+
readonly log: VeilleLog
|
|
19
|
+
/** the marks, drafts and replies live beside the log; `watchFile` the identity's watch list */
|
|
20
|
+
constructor(
|
|
21
|
+
readonly dir: string,
|
|
22
|
+
readonly watchFile: string,
|
|
23
|
+
) {
|
|
24
|
+
this.log = new VeilleLog(dir)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get repliesDir(): string {
|
|
28
|
+
return join(this.dir, 'replies')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
handled(): Record<string, number> {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(readFileSync(join(this.dir, 'handled.json'), 'utf8')) as Record<string, number>
|
|
34
|
+
} catch {
|
|
35
|
+
return {}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setHandled(key: string, on: boolean, now = Date.now()): void {
|
|
40
|
+
const h = this.handled()
|
|
41
|
+
if (on) h[key] = now
|
|
42
|
+
else delete h[key]
|
|
43
|
+
mkdirSync(this.dir, { recursive: true })
|
|
44
|
+
writeFileSync(join(this.dir, 'handled.json'), JSON.stringify(h))
|
|
45
|
+
this.log.stamp++
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** our replies detected, by the key of the post they answer */
|
|
49
|
+
replied(): Record<string, Replied> {
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(readFileSync(join(this.dir, 'replied.json'), 'utf8')) as Record<string, Replied>
|
|
52
|
+
} catch {
|
|
53
|
+
return {}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** a reply detected: kept with its url, the post marked handled; false when that post already had one */
|
|
58
|
+
setReplied(key: string, r: Replied, now = Date.now()): boolean {
|
|
59
|
+
const all = this.replied()
|
|
60
|
+
if (all[key] != null) return false
|
|
61
|
+
all[key] = r
|
|
62
|
+
mkdirSync(this.dir, { recursive: true })
|
|
63
|
+
writeFileSync(join(this.dir, 'replied.json'), JSON.stringify(all))
|
|
64
|
+
if (this.handled()[key] == null) this.setHandled(key, true, now)
|
|
65
|
+
this.log.stamp++
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
reply(key: string): Reply | null {
|
|
70
|
+
try {
|
|
71
|
+
return JSON.parse(readFileSync(join(this.repliesDir, `${fileKey(key)}.json`), 'utf8')) as Reply
|
|
72
|
+
} catch {
|
|
73
|
+
return null
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
writeReply(key: string, r: Reply): void {
|
|
78
|
+
mkdirSync(this.repliesDir, { recursive: true })
|
|
79
|
+
writeFileSync(join(this.repliesDir, `${fileKey(key)}.json`), JSON.stringify(r, null, 2))
|
|
80
|
+
this.log.stamp++
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** every draft, by key: one listing of the folder */
|
|
84
|
+
drafts(): Record<string, Reply> {
|
|
85
|
+
const out: Record<string, Reply> = {}
|
|
86
|
+
if (!existsSync(this.repliesDir)) return out
|
|
87
|
+
for (const f of readdirSync(this.repliesDir)) {
|
|
88
|
+
const m = /^([a-z]+)-(.+)\.json$/.exec(f)
|
|
89
|
+
const r = m ? this.reply(`${m[1]}:${m[2]}`) : null
|
|
90
|
+
if (m && r) out[`${m[1]}:${m[2]}`] = r
|
|
91
|
+
}
|
|
92
|
+
return out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** the platforms he switched off in the rail: their columns hidden, out of the review and its rule, their log kept */
|
|
96
|
+
off(): Platform[] {
|
|
97
|
+
try {
|
|
98
|
+
const o = (JSON.parse(readFileSync(join(this.dir, 'platforms.json'), 'utf8')) as { off?: unknown }).off
|
|
99
|
+
return PLATFORMS.filter((p) => Array.isArray(o) && o.includes(p))
|
|
100
|
+
} catch {
|
|
101
|
+
return []
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
setOn(platform: Platform, on: boolean): void {
|
|
106
|
+
const off = this.off().filter((p) => p !== platform)
|
|
107
|
+
if (!on) off.push(platform)
|
|
108
|
+
mkdirSync(this.dir, { recursive: true })
|
|
109
|
+
writeFileSync(join(this.dir, 'platforms.json'), `${JSON.stringify({ off: PLATFORMS.filter((p) => off.includes(p)) })}\n`)
|
|
110
|
+
this.log.stamp++
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** the posts a review passed, with its one line reason */
|
|
114
|
+
passed(): Record<string, { why: string; at: number }> {
|
|
115
|
+
try {
|
|
116
|
+
return JSON.parse(readFileSync(join(this.dir, 'passed.json'), 'utf8')) as Record<string, { why: string; at: number }>
|
|
117
|
+
} catch {
|
|
118
|
+
return {}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
pass(key: string, why: string, now = Date.now()): void {
|
|
123
|
+
const p = this.passed()
|
|
124
|
+
p[key] = { why, at: now }
|
|
125
|
+
mkdirSync(this.dir, { recursive: true })
|
|
126
|
+
writeFileSync(join(this.dir, 'passed.json'), JSON.stringify(p))
|
|
127
|
+
this.log.stamp++
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
watch(): Watch {
|
|
131
|
+
return watch(this.watchFile)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** the posts ranked by worth, best first: in the window, on the platform, kept by the watch list as it stands
|
|
135
|
+
* (the identity's, unless the query names one), our own accounts left out */
|
|
136
|
+
feed(q: FeedQuery = {}): FeedRow[] {
|
|
137
|
+
const now = q.now ?? Date.now()
|
|
138
|
+
const w = q.watch ?? this.watch()
|
|
139
|
+
const rules = w.rules
|
|
140
|
+
const handled = this.handled()
|
|
141
|
+
const drafts = this.drafts()
|
|
142
|
+
const passed = this.passed()
|
|
143
|
+
const replied = this.replied()
|
|
144
|
+
const authors = this.log.authors()
|
|
145
|
+
const rows: FeedRow[] = []
|
|
146
|
+
for (const p of this.log.posts().values()) {
|
|
147
|
+
if (q.platform && p.platform !== q.platform) continue
|
|
148
|
+
if (q.sinceMs != null && now - p.last > q.sinceMs) continue
|
|
149
|
+
if (q.ours?.has(ourKey(p.platform, p.author))) continue
|
|
150
|
+
const key = keyOf(p)
|
|
151
|
+
const followers = authors[`${p.platform}:${p.author.toLowerCase()}`]?.followers ?? p.followers
|
|
152
|
+
const post = { ...p, place: p.place ?? '', followers }
|
|
153
|
+
if (!kept(post, w)) continue
|
|
154
|
+
const tags = matches(post, w)
|
|
155
|
+
const x = parts(post, tags.length, now, rules)
|
|
156
|
+
const worth = worthOf(x)
|
|
157
|
+
const group = groupOf(worth, handled[key] != null || passed[key] != null, rules)
|
|
158
|
+
if (!q.all && group === 'done') continue
|
|
159
|
+
rows.push({ ...post, key, worth, parts: x, group, handled: handled[key] != null, passed: passed[key]?.why ?? null, reply: drafts[key] ?? null, replied: replied[key] ?? null, asked: q.asks?.[key] ?? null, tags })
|
|
160
|
+
}
|
|
161
|
+
rows.sort((a, b) => b.worth - a.worth || b.last - a.last)
|
|
162
|
+
return q.top != null ? rows.slice(0, q.top) : rows
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 1234 → 1.2k */
|
|
167
|
+
export const short = (n: number | null): string => (n == null ? '-' : n >= 1e6 ? `${(n / 1e6).toFixed(1)}M` : n >= 1e4 ? `${Math.round(n / 1e3)}k` : n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : String(n))
|
|
168
|
+
|
|
169
|
+
export const age = (ms: number): string => (ms < 3600_000 ? `${Math.max(1, Math.round(ms / 60_000))}m` : ms < 48 * 3600_000 ? `${Math.round(ms / 3600_000)}h` : `${Math.round(ms / 86_400_000)}d`)
|
|
170
|
+
|
|
171
|
+
/** one compact line a post, what an agent reads instead of the log */
|
|
172
|
+
export function feedLine(r: FeedRow, now = Date.now()): string {
|
|
173
|
+
const text = r.text.length > 100 ? `${r.text.slice(0, 99)}…` : r.text
|
|
174
|
+
const marks = `${r.reply ? ' [draft]' : r.asked ? ` [asked ${r.asked.id}]` : ''}${r.handled ? ' [handled]' : r.passed ? ` [passed: ${r.passed}]` : r.group === 'done' ? ' [expired]' : ''}`
|
|
175
|
+
return `${r.platform.padEnd(9)} @${r.author} (${short(r.followers)}) ${age(now - (r.at ?? r.first))} ♥${short(r.likes)} ↻${short(r.reposts)} 💬${short(r.replies)}${r.views != null ? ` 👁${short(r.views)}` : ''} w${r.worth}${r.place ? ` ${r.place}` : ''} « ${text} » ${r.key}${marks}`
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** a draft of the project as the media picker reads it: its slug, and each message's text, title and media file */
|
|
179
|
+
export type DraftMedia = { slug: string; posts: readonly { text: string; title: string; media: { path: string } | null }[] }
|
|
180
|
+
|
|
181
|
+
/** the media our drafts could lend a reply, repo relative: the drafts' files, the ones sharing the most words with the
|
|
182
|
+
* post first */
|
|
183
|
+
export function mediaFor(text: string, drafts: readonly DraftMedia[], root: string, top = 6): MediaPick[] {
|
|
184
|
+
const words = (s: string): Set<string> => new Set(s.toLowerCase().normalize('NFD').replace(/\p{M}/gu, '').split(/[^\p{L}\p{N}]+/u).filter((w) => w.length > 3))
|
|
185
|
+
const mine = words(text)
|
|
186
|
+
const seen = new Set<string>()
|
|
187
|
+
const out: MediaPick[] = []
|
|
188
|
+
for (const d of drafts)
|
|
189
|
+
for (const p of d.posts) {
|
|
190
|
+
if (p.media == null || seen.has(p.media.path)) continue
|
|
191
|
+
seen.add(p.media.path)
|
|
192
|
+
const theirs = words(`${p.title} ${p.text}`)
|
|
193
|
+
out.push({ path: p.media.path.startsWith(`${root}/`) ? p.media.path.slice(root.length + 1) : p.media.path, draft: d.slug, words: [...mine].filter((w) => theirs.has(w)).length })
|
|
194
|
+
}
|
|
195
|
+
// a draft's slug starts with its date: the newest first among equals
|
|
196
|
+
return out.sort((a, b) => b.words - a.words || b.draft.localeCompare(a.draft)).slice(0, top)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** the identity's voice as a brief names it: its pages and skill, and nothing else */
|
|
200
|
+
const voiceLine = (voice: readonly string[], as: string): string =>
|
|
201
|
+
voice.length > 0 ? `Write in the voice of ${as}: read ${voice.join(', ')}. Nothing else.` : `Write in the voice of ${as}: its watch list names no ## Voice page, so keep it short, specific and warm.`
|
|
202
|
+
|
|
203
|
+
/** the brief for one reply: the post, the pages to read, the media to lend, where to write. Nothing else is loaded */
|
|
204
|
+
export function brief(r: FeedRow, media: readonly MediaPick[], voice: { as: string; pages: readonly string[] }, now = Date.now()): string {
|
|
205
|
+
return [
|
|
206
|
+
`# A reply to ${r.key}`,
|
|
207
|
+
'',
|
|
208
|
+
`${feedLine(r, now)}`,
|
|
209
|
+
`${r.url}`,
|
|
210
|
+
'',
|
|
211
|
+
'> ' + r.text.replaceAll('\n', '\n> '),
|
|
212
|
+
'',
|
|
213
|
+
voiceLine(voice.pages, voice.as),
|
|
214
|
+
'No reply is better than a thin one: pass it when there is nothing specific, warm and true to say.',
|
|
215
|
+
'',
|
|
216
|
+
media.length > 0 ? 'Media our drafts could lend (the path, the draft, the words shared):' : 'No media from our drafts.',
|
|
217
|
+
...media.map((m) => `- ${m.path} (${m.draft}, ${m.words})`),
|
|
218
|
+
'',
|
|
219
|
+
`Write it: sk veille reply ${r.key} --as ${voice.as} --text "<the reply>" [--media <path>]`,
|
|
220
|
+
`Or pass it: sk veille pass ${r.key} --as ${voice.as} --why "<one line>"`,
|
|
221
|
+
'Then close this item: sk review fixed <its id> --note "<the draft, or why not>"',
|
|
222
|
+
'',
|
|
223
|
+
].join('\n')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** the brief for one review of every column: each platform's active posts as compact lines under its heading, the
|
|
227
|
+
* platform with the best post first, best first in each, and how to answer */
|
|
228
|
+
export function reviewBrief(columns: readonly { label: string; rows: readonly FeedRow[] }[], voice: { as: string; pages: readonly string[] }, now = Date.now()): string {
|
|
229
|
+
const shown = columns.filter((c) => c.rows.length > 0).sort((a, b) => b.rows[0]!.worth - a.rows[0]!.worth)
|
|
230
|
+
const n = shown.reduce((t, c) => t + c.rows.length, 0)
|
|
231
|
+
return [
|
|
232
|
+
`# One review of the Veille: ${n} active post${n > 1 ? 's' : ''} on ${shown.length} platform${shown.length > 1 ? 's' : ''}, best first`,
|
|
233
|
+
'',
|
|
234
|
+
...shown.flatMap((c) => [`## ${c.label}: ${c.rows.length} post${c.rows.length > 1 ? 's' : ''}`, '', ...c.rows.map((r) => feedLine(r, now)), '']),
|
|
235
|
+
voiceLine(voice.pages, voice.as),
|
|
236
|
+
'Read every line first, then pick the few posts truly worth a reply (often two or three, sometimes none).',
|
|
237
|
+
`Each one picked: sk veille reply <key> --as ${voice.as} for its brief, then sk veille reply <key> --as ${voice.as} --text "<the reply>".`,
|
|
238
|
+
`Every other line: sk veille pass <key> --as ${voice.as} --why "<the real reason, one line>".`,
|
|
239
|
+
'Then close this item: sk review fixed <its id> --note "<n drafts, m passed>"',
|
|
240
|
+
'',
|
|
241
|
+
].join('\n')
|
|
242
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { LAYOUT } from '../core/layout.ts'
|
|
4
|
+
import { houseRootOf } from '../home/home-start.ts'
|
|
5
|
+
import { homeDir } from '../home/home-paths.ts'
|
|
6
|
+
import { type Identity, speakerOf } from '../identities/identities.ts'
|
|
7
|
+
import { identityCache } from '../identities/machine.ts'
|
|
8
|
+
|
|
9
|
+
// who scrolls (wiki tools/shipkit/veille.md, Identities): what the Veille needs of an identity of the identity model
|
|
10
|
+
// (identities/identities.ts), its Chrome profile's email, its own accounts' handles, its watch list beside its files
|
|
11
|
+
// and its log in shipkit's home
|
|
12
|
+
|
|
13
|
+
export type VeilleIdentity = {
|
|
14
|
+
id: string
|
|
15
|
+
name: string
|
|
16
|
+
/** the email its Chrome profile is signed in with: the profile the extension says it runs in, where a page opens */
|
|
17
|
+
email: string
|
|
18
|
+
/** its own accounts, their posts never in its feed */
|
|
19
|
+
accounts: { platform: string; handle: string }[]
|
|
20
|
+
/** its watch list, absent when it has no Veille */
|
|
21
|
+
watchFile: string
|
|
22
|
+
/** its log, on this machine only */
|
|
23
|
+
logDir: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** the identities folder: `SHIPKIT_IDENTITIES_DIR`, else the house repo's, found from `cwd` */
|
|
27
|
+
export const identitiesRoot = (cwd = process.cwd()): string | null => {
|
|
28
|
+
const env = process.env.SHIPKIT_IDENTITIES_DIR
|
|
29
|
+
if (env) return env
|
|
30
|
+
const house = houseRootOf(cwd)
|
|
31
|
+
return house == null ? null : join(house, LAYOUT.identities)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** an identity's log folder in shipkit's home */
|
|
35
|
+
export const veilleLogDir = (id: string, home = homeDir()): string => join(home, 'veille', id)
|
|
36
|
+
|
|
37
|
+
/** `<platform>:<handle>` the way an author is compared: no `@`, no instance, lowercase */
|
|
38
|
+
export const handleKey = (platform: string, handle: string): string => `${platform}:${handle.replace(/^@/, '').split('@')[0]!.toLowerCase()}`
|
|
39
|
+
|
|
40
|
+
const veilleOfIdentity = (x: Identity): VeilleIdentity => ({
|
|
41
|
+
id: x.id,
|
|
42
|
+
name: x.name,
|
|
43
|
+
email: x.rows.find((r) => r.field === 'email')?.value ?? '',
|
|
44
|
+
accounts: x.accounts.filter((a) => a.handle !== '').map((a) => ({ platform: a.id, handle: a.handle })),
|
|
45
|
+
watchFile: join(x.dir, 'veille.md'),
|
|
46
|
+
logDir: veilleLogDir(x.id),
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** the identities, read again once an identity.md moved: every hub's tree asks on each refresh, and a read measures
|
|
50
|
+
* every picture */
|
|
51
|
+
const identitiesOf = identityCache()
|
|
52
|
+
|
|
53
|
+
/** every identity, the model's order */
|
|
54
|
+
export const identities = (root = identitiesRoot()): VeilleIdentity[] => identitiesOf(root).identities.map(veilleOfIdentity)
|
|
55
|
+
|
|
56
|
+
export const identityOf = (id: string, root = identitiesRoot()): VeilleIdentity | null => identities(root).find((x) => x.id === id) ?? null
|
|
57
|
+
|
|
58
|
+
export const hasVeille = (x: VeilleIdentity): boolean => existsSync(x.watchFile)
|
|
59
|
+
|
|
60
|
+
/** the identity a Chrome profile is signed in as, by its email; null when none is */
|
|
61
|
+
export const identityOfProfile = (email: string, root = identitiesRoot()): VeilleIdentity | null =>
|
|
62
|
+
email === '' ? null : (identities(root).find((x) => x.email.toLowerCase() === email.toLowerCase()) ?? null)
|
|
63
|
+
|
|
64
|
+
/** the `asks:` line of a watch list, read without parsing the rest */
|
|
65
|
+
function asksOf(file: string): string | null {
|
|
66
|
+
try {
|
|
67
|
+
return /^asks:\s*(\S+)\s*$/m.exec(/^---\n([\s\S]*?)\n---/.exec(readFileSync(file, 'utf8'))?.[1] ?? '')?.[1] ?? null
|
|
68
|
+
} catch {
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** the Veille a repo shows: the identity it speaks as (its `speaksAs`, else the default one), else the one whose watch
|
|
74
|
+
* list names it in `asks:` */
|
|
75
|
+
export function veilleOfRepo(repo: string, speaksAs: string | null, root = identitiesRoot()): VeilleIdentity | null {
|
|
76
|
+
const ids = identitiesOf(root)
|
|
77
|
+
const own = speakerOf(speaksAs ?? undefined, ids)
|
|
78
|
+
if (own != null && hasVeille(veilleOfIdentity(own))) return veilleOfIdentity(own)
|
|
79
|
+
return ids.identities.map(veilleOfIdentity).find((x) => hasVeille(x) && asksOf(x.watchFile) === repo) ?? null
|
|
80
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { VeilleLog } from './log.ts'
|
|
4
|
+
import type { Kept } from './platforms.ts'
|
|
5
|
+
|
|
6
|
+
// an older log brought into an identity's (wiki tools/shipkit/veille.md, The log): the day files merged line by line in
|
|
7
|
+
// the order they were seen, every mark kept, the target's own answer winning where both hold one. The posts are counted
|
|
8
|
+
// before and after: a post of either side missing after is a failure, never a silence
|
|
9
|
+
|
|
10
|
+
const DAY_FILE = /^\d{4}-\d{2}-\d{2}\.jsonl$/
|
|
11
|
+
const MAPS = ['handled.json', 'passed.json', 'replied.json'] as const
|
|
12
|
+
|
|
13
|
+
export type ImportReport = { source: number; targetBefore: number; targetAfter: number; days: number; marks: Record<string, number>; replies: number; missing: string[] }
|
|
14
|
+
|
|
15
|
+
const readJson = <T>(f: string, empty: T): T => {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(readFileSync(f, 'utf8')) as T
|
|
18
|
+
} catch {
|
|
19
|
+
return empty
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const lastOf = (line: string): number => {
|
|
24
|
+
try {
|
|
25
|
+
return (JSON.parse(line) as Kept).last ?? 0
|
|
26
|
+
} catch {
|
|
27
|
+
return 0
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function importLog(from: string, to: string): ImportReport {
|
|
32
|
+
if (!existsSync(from)) throw new Error(`no log at ${from}`)
|
|
33
|
+
const source = new VeilleLog(from).posts()
|
|
34
|
+
const before = new VeilleLog(to).posts()
|
|
35
|
+
const keys = new Set([...source.keys(), ...before.keys()])
|
|
36
|
+
mkdirSync(to, { recursive: true })
|
|
37
|
+
const days = readdirSync(from).filter((f) => DAY_FILE.test(f))
|
|
38
|
+
for (const f of days) {
|
|
39
|
+
const theirs = readFileSync(join(from, f), 'utf8').split('\n').filter(Boolean)
|
|
40
|
+
const ours = existsSync(join(to, f)) ? readFileSync(join(to, f), 'utf8').split('\n').filter(Boolean) : []
|
|
41
|
+
// one day of both: every line once, in the order it was seen, so the last line of a post stays its latest state
|
|
42
|
+
const merged = [...new Set([...theirs, ...ours])].map((line, i) => ({ line, i, last: lastOf(line) })).sort((a, b) => a.last - b.last || a.i - b.i)
|
|
43
|
+
writeFileSync(join(to, f), `${merged.map((x) => x.line).join('\n')}\n`)
|
|
44
|
+
}
|
|
45
|
+
const marks: Record<string, number> = {}
|
|
46
|
+
for (const m of MAPS) {
|
|
47
|
+
const all = { ...readJson<Record<string, unknown>>(join(from, m), {}), ...readJson<Record<string, unknown>>(join(to, m), {}) }
|
|
48
|
+
marks[m] = Object.keys(all).length
|
|
49
|
+
if (marks[m] > 0) writeFileSync(join(to, m), JSON.stringify(all))
|
|
50
|
+
}
|
|
51
|
+
type Author = { followers: number; at: number }
|
|
52
|
+
const authors = readJson<Record<string, Author>>(join(from, 'authors.json'), {})
|
|
53
|
+
for (const [k, a] of Object.entries(readJson<Record<string, Author>>(join(to, 'authors.json'), {}))) if ((authors[k]?.at ?? 0) <= a.at) authors[k] = a
|
|
54
|
+
if (Object.keys(authors).length > 0) writeFileSync(join(to, 'authors.json'), JSON.stringify(authors))
|
|
55
|
+
const off = [...new Set([...readJson<{ off?: string[] }>(join(from, 'platforms.json'), {}).off ?? [], ...readJson<{ off?: string[] }>(join(to, 'platforms.json'), {}).off ?? []])]
|
|
56
|
+
if (off.length > 0) writeFileSync(join(to, 'platforms.json'), `${JSON.stringify({ off })}\n`)
|
|
57
|
+
if (existsSync(join(from, 'reads.json')) && !existsSync(join(to, 'reads.json'))) copyFileSync(join(from, 'reads.json'), join(to, 'reads.json'))
|
|
58
|
+
let replies = 0
|
|
59
|
+
if (existsSync(join(from, 'replies'))) {
|
|
60
|
+
mkdirSync(join(to, 'replies'), { recursive: true })
|
|
61
|
+
for (const f of readdirSync(join(from, 'replies'))) {
|
|
62
|
+
if (!existsSync(join(to, 'replies', f))) copyFileSync(join(from, 'replies', f), join(to, 'replies', f))
|
|
63
|
+
replies++
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const after = new VeilleLog(to).posts()
|
|
67
|
+
return { source: source.size, targetBefore: before.size, targetAfter: after.size, days: days.length, marks, replies, missing: [...keys].filter((k) => !after.has(k)) }
|
|
68
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { isPlatform, keyOf, type Kept, type Platform, type Read } from './platforms.ts'
|
|
4
|
+
|
|
5
|
+
// the Veille's log (wiki tools/shipkit/veille.md, The log): one identity's posts the extension read, on this machine
|
|
6
|
+
// only. One JSONL file a day (the day in Paris), each line a post's whole state, the last line of a post wins; capped in
|
|
7
|
+
// bytes, the oldest days dropped first
|
|
8
|
+
|
|
9
|
+
const CAP_BYTES = 20 * 1024 * 1024
|
|
10
|
+
/** a post seen again with the same counts gets a new line this long after its last one */
|
|
11
|
+
const REWRITE_MS = 15 * 60_000
|
|
12
|
+
const AUTHORS_KEPT = 5000
|
|
13
|
+
|
|
14
|
+
const DAY = new Intl.DateTimeFormat('en-CA', { timeZone: 'Europe/Paris', year: 'numeric', month: '2-digit', day: '2-digit' })
|
|
15
|
+
/** a moment's day, `YYYY-MM-DD`, in Paris */
|
|
16
|
+
export const dayOf = (ms: number): string => DAY.format(ms)
|
|
17
|
+
|
|
18
|
+
/** `page`: the path and query of the page read, empty when the extension did not say */
|
|
19
|
+
export type Batch = { platform: Platform; posts: Read[]; authors: { author: string; followers: number }[]; page: string }
|
|
20
|
+
|
|
21
|
+
/** a post's key as a file name */
|
|
22
|
+
export const fileKey = (key: string): string => key.replace(':', '-')
|
|
23
|
+
|
|
24
|
+
const ID = /^[A-Za-z0-9_-]{1,64}$/
|
|
25
|
+
const num = (v: unknown): number | null => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.round(v) : null)
|
|
26
|
+
const str = (v: unknown, max: number): string => (typeof v === 'string' ? v.slice(0, max) : '')
|
|
27
|
+
|
|
28
|
+
/** a batch from the extension, every field checked and capped; a string says what is wrong */
|
|
29
|
+
export function parseBatch(body: unknown): Batch | string {
|
|
30
|
+
if (typeof body !== 'object' || body == null) return 'a JSON object is expected'
|
|
31
|
+
const b = body as { platform?: unknown; posts?: unknown; authors?: unknown; page?: unknown }
|
|
32
|
+
if (!isPlatform(b.platform)) return `platform ${String(b.platform)}?`
|
|
33
|
+
const platform = b.platform
|
|
34
|
+
const posts: Read[] = []
|
|
35
|
+
for (const raw of Array.isArray(b.posts) ? b.posts.slice(0, 100) : []) {
|
|
36
|
+
const p = raw as Record<string, unknown>
|
|
37
|
+
const id = str(p.id, 64)
|
|
38
|
+
const url = str(p.url, 300)
|
|
39
|
+
if (p.platform !== platform || !ID.test(id) || !/^https:\/\/\S+$/.test(url)) continue
|
|
40
|
+
posts.push({
|
|
41
|
+
platform,
|
|
42
|
+
id,
|
|
43
|
+
url,
|
|
44
|
+
author: str(p.author, 80),
|
|
45
|
+
name: str(p.name, 80),
|
|
46
|
+
text: str(p.text, 500),
|
|
47
|
+
place: str(p.place, 80),
|
|
48
|
+
at: num(p.at),
|
|
49
|
+
likes: num(p.likes),
|
|
50
|
+
reposts: num(p.reposts),
|
|
51
|
+
replies: num(p.replies),
|
|
52
|
+
views: num(p.views),
|
|
53
|
+
followers: num(p.followers),
|
|
54
|
+
media: p.media === 'image' || p.media === 'video' ? p.media : null,
|
|
55
|
+
...(typeof p.replyTo === 'string' && p.replyTo !== '' ? { replyTo: p.replyTo.slice(0, 80) } : {}),
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
const authors = (Array.isArray(b.authors) ? b.authors.slice(0, 50) : []).flatMap((raw) => {
|
|
59
|
+
const a = raw as Record<string, unknown>
|
|
60
|
+
const followers = num(a.followers)
|
|
61
|
+
return typeof a.author === 'string' && a.author !== '' && followers != null ? [{ author: a.author.slice(0, 80), followers }] : []
|
|
62
|
+
})
|
|
63
|
+
return { platform, posts, authors, page: str(b.page, 300) }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const DAY_FILE = /^\d{4}-\d{2}-\d{2}\.jsonl$/
|
|
67
|
+
const counts = (p: Read): string => [p.likes, p.reposts, p.replies, p.views, p.followers].join(',')
|
|
68
|
+
|
|
69
|
+
export class VeilleLog {
|
|
70
|
+
private index: Map<string, Kept> | null = null
|
|
71
|
+
/** the day files as last read, names and sizes: another process that wrote a line moves it */
|
|
72
|
+
private shape = ''
|
|
73
|
+
private written = new Map<string, number>()
|
|
74
|
+
private authorsMap: Record<string, { followers: number; at: number }> | null = null
|
|
75
|
+
/** moves at every write: the page fetches the feed again */
|
|
76
|
+
stamp = 0
|
|
77
|
+
|
|
78
|
+
constructor(
|
|
79
|
+
readonly dir: string,
|
|
80
|
+
readonly cap = CAP_BYTES,
|
|
81
|
+
) {}
|
|
82
|
+
|
|
83
|
+
/** the day files, oldest first */
|
|
84
|
+
days(): string[] {
|
|
85
|
+
return existsSync(this.dir) ? readdirSync(this.dir).filter((f) => DAY_FILE.test(f)).sort() : []
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
bytes(): number {
|
|
89
|
+
return this.days().reduce((n, f) => n + statSync(join(this.dir, f)).size, 0)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private filesShape(): string {
|
|
93
|
+
return this.days()
|
|
94
|
+
.map((f) => `${f}:${statSync(join(this.dir, f)).size}`)
|
|
95
|
+
.join(',')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** every post, the last line of each; read again when another process wrote to the log */
|
|
99
|
+
posts(): Map<string, Kept> {
|
|
100
|
+
const shape = this.filesShape()
|
|
101
|
+
if (this.index && shape === this.shape) return this.index
|
|
102
|
+
this.written.clear()
|
|
103
|
+
this.shape = shape
|
|
104
|
+
const index = new Map<string, Kept>()
|
|
105
|
+
for (const f of this.days())
|
|
106
|
+
for (const line of readFileSync(join(this.dir, f), 'utf8').split('\n')) {
|
|
107
|
+
if (line === '') continue
|
|
108
|
+
try {
|
|
109
|
+
const p = JSON.parse(line) as Kept
|
|
110
|
+
index.set(keyOf(p), p)
|
|
111
|
+
this.written.set(keyOf(p), p.last)
|
|
112
|
+
} catch {
|
|
113
|
+
// a line cut by a crash: the next one of that post wins anyway
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
this.index = index
|
|
117
|
+
return index
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** the followers last read for each author, by `<platform>:<author>` */
|
|
121
|
+
authors(): Record<string, { followers: number; at: number }> {
|
|
122
|
+
if (this.authorsMap) return this.authorsMap
|
|
123
|
+
try {
|
|
124
|
+
this.authorsMap = JSON.parse(readFileSync(join(this.dir, 'authors.json'), 'utf8')) as Record<string, { followers: number; at: number }>
|
|
125
|
+
} catch {
|
|
126
|
+
this.authorsMap = {}
|
|
127
|
+
}
|
|
128
|
+
return this.authorsMap
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** one batch merged: a post new, its counts moved, or its last line old gets a line in today's file */
|
|
132
|
+
add(batch: Batch, now = Date.now()): { lines: number } {
|
|
133
|
+
const index = this.posts()
|
|
134
|
+
const authors = this.authors()
|
|
135
|
+
if (batch.authors.length > 0) {
|
|
136
|
+
for (const a of batch.authors) authors[`${batch.platform}:${a.author.toLowerCase()}`] = { followers: a.followers, at: now }
|
|
137
|
+
const kept = Object.entries(authors).sort((x, y) => y[1].at - x[1].at).slice(0, AUTHORS_KEPT)
|
|
138
|
+
this.authorsMap = Object.fromEntries(kept)
|
|
139
|
+
mkdirSync(this.dir, { recursive: true })
|
|
140
|
+
writeFileSync(join(this.dir, 'authors.json'), JSON.stringify(this.authorsMap))
|
|
141
|
+
}
|
|
142
|
+
const lines: string[] = []
|
|
143
|
+
for (const read of batch.posts) {
|
|
144
|
+
const key = keyOf(read)
|
|
145
|
+
const was = index.get(key)
|
|
146
|
+
const followers = read.followers ?? this.authorsMap?.[`${read.platform}:${read.author.toLowerCase()}`]?.followers ?? was?.followers ?? null
|
|
147
|
+
const via = read.via || was?.via
|
|
148
|
+
const next: Kept = { ...read, followers, at: read.at ?? was?.at ?? null, first: was?.first ?? now, last: now, seen: (was?.seen ?? 0) + 1, ...(via ? { via } : {}) }
|
|
149
|
+
index.set(key, next)
|
|
150
|
+
if (was == null || counts(was) !== counts(next) || next.via !== was.via || now - (this.written.get(key) ?? 0) >= REWRITE_MS) {
|
|
151
|
+
lines.push(JSON.stringify(next))
|
|
152
|
+
this.written.set(key, now)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (lines.length > 0) {
|
|
156
|
+
mkdirSync(this.dir, { recursive: true })
|
|
157
|
+
appendFileSync(join(this.dir, `${dayOf(now)}.jsonl`), `${lines.join('\n')}\n`)
|
|
158
|
+
this.rotate(now)
|
|
159
|
+
if (this.index) this.shape = this.filesShape()
|
|
160
|
+
}
|
|
161
|
+
if (lines.length > 0 || batch.authors.length > 0) this.stamp++
|
|
162
|
+
return { lines: lines.length }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** past the cap, the oldest days deleted, today's never; the posts then read again from the days kept */
|
|
166
|
+
rotate(now = Date.now()): string[] {
|
|
167
|
+
const today = `${dayOf(now)}.jsonl`
|
|
168
|
+
const files = this.days().map((f) => ({ f, size: statSync(join(this.dir, f)).size }))
|
|
169
|
+
let total = files.reduce((n, x) => n + x.size, 0)
|
|
170
|
+
const dropped: string[] = []
|
|
171
|
+
for (const x of files) {
|
|
172
|
+
if (total <= this.cap || x.f === today) break
|
|
173
|
+
rmSync(join(this.dir, x.f))
|
|
174
|
+
total -= x.size
|
|
175
|
+
dropped.push(x.f)
|
|
176
|
+
}
|
|
177
|
+
if (dropped.length > 0) {
|
|
178
|
+
this.index = null
|
|
179
|
+
this.written.clear()
|
|
180
|
+
}
|
|
181
|
+
return dropped
|
|
182
|
+
}
|
|
183
|
+
}
|