@rkarim08/sia 1.0.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/.claude-plugin/marketplace.json +35 -0
- package/.claude-plugin/plugin.json +27 -0
- package/.mcp.json +13 -0
- package/CLAUDE.md +226 -0
- package/LICENSE +202 -0
- package/PLUGIN_README.md +253 -0
- package/README.md +1013 -0
- package/agents/sia-changelog-writer.md +89 -0
- package/agents/sia-code-reviewer.md +86 -0
- package/agents/sia-conflict-resolver.md +100 -0
- package/agents/sia-convention-enforcer.md +69 -0
- package/agents/sia-debug.md +106 -0
- package/agents/sia-decision-reviewer.md +101 -0
- package/agents/sia-dependency-tracker.md +80 -0
- package/agents/sia-explain.md +126 -0
- package/agents/sia-feature.md +116 -0
- package/agents/sia-knowledge-capture.md +117 -0
- package/agents/sia-lead-architecture-advisor.md +93 -0
- package/agents/sia-lead-team-health.md +107 -0
- package/agents/sia-migration.md +100 -0
- package/agents/sia-onboarding.md +115 -0
- package/agents/sia-orientation.md +99 -0
- package/agents/sia-pm-briefing.md +106 -0
- package/agents/sia-pm-risk-advisor.md +82 -0
- package/agents/sia-qa-analyst.md +116 -0
- package/agents/sia-qa-regression-map.md +94 -0
- package/agents/sia-refactor.md +115 -0
- package/agents/sia-regression.md +112 -0
- package/agents/sia-security-audit.md +125 -0
- package/agents/sia-test-advisor.md +91 -0
- package/hooks/hooks.json +98 -0
- package/migrations/bridge/001_initial.sql +34 -0
- package/migrations/episodic/001_initial.sql +35 -0
- package/migrations/meta/001_initial.sql +68 -0
- package/migrations/semantic/001_initial.sql +292 -0
- package/migrations/semantic/002_ontology.sql +89 -0
- package/migrations/semantic/003_freshness.sql +63 -0
- package/migrations/semantic/004_v5_unified_schema.sql +194 -0
- package/migrations/semantic/005_backfill_event_kinds.sql +8 -0
- package/migrations/semantic/006_tree_sitter.sql +6 -0
- package/migrations/semantic/007_branch_snapshots.sql +22 -0
- package/package.json +110 -0
- package/scripts/branch-switch.sh +13 -0
- package/scripts/build-wasm-grammars.sh +81 -0
- package/scripts/post-compact.sh +8 -0
- package/scripts/post-tool-use.sh +10 -0
- package/scripts/pre-compact.sh +8 -0
- package/scripts/session-end.sh +8 -0
- package/scripts/session-start.sh +8 -0
- package/scripts/start-mcp.ts +45 -0
- package/scripts/stop-hook.sh +8 -0
- package/scripts/user-prompt-submit.sh +8 -0
- package/scripts/viz-server.ts +152 -0
- package/skills/sia-brainstorm/SKILL.md +156 -0
- package/skills/sia-brainstorm/scripts/frame-template.html +214 -0
- package/skills/sia-brainstorm/scripts/helper.js +95 -0
- package/skills/sia-brainstorm/scripts/server.cjs +338 -0
- package/skills/sia-brainstorm/scripts/start-server.sh +153 -0
- package/skills/sia-brainstorm/scripts/stop-server.sh +55 -0
- package/skills/sia-brainstorm/spec-document-reviewer-prompt.md +49 -0
- package/skills/sia-brainstorm/visual-companion.md +286 -0
- package/skills/sia-capture/SKILL.md +64 -0
- package/skills/sia-compare/SKILL.md +33 -0
- package/skills/sia-conflicts/SKILL.md +38 -0
- package/skills/sia-debug-workflow/SKILL.md +120 -0
- package/skills/sia-debug-workflow/root-cause-tracing.md +70 -0
- package/skills/sia-debug-workflow/scripts/find-polluter.sh +64 -0
- package/skills/sia-debug-workflow/temporal-investigation.md +72 -0
- package/skills/sia-digest/SKILL.md +23 -0
- package/skills/sia-dispatch/SKILL.md +69 -0
- package/skills/sia-dispatch/agent-task-template.md +99 -0
- package/skills/sia-doctor/SKILL.md +39 -0
- package/skills/sia-execute/SKILL.md +70 -0
- package/skills/sia-execute-plan/SKILL.md +85 -0
- package/skills/sia-export-import/SKILL.md +49 -0
- package/skills/sia-export-knowledge/SKILL.md +46 -0
- package/skills/sia-finish/SKILL.md +100 -0
- package/skills/sia-finish/pr-summary-template.md +54 -0
- package/skills/sia-freshness/SKILL.md +38 -0
- package/skills/sia-history/SKILL.md +42 -0
- package/skills/sia-impact/SKILL.md +70 -0
- package/skills/sia-index/SKILL.md +54 -0
- package/skills/sia-install/SKILL.md +39 -0
- package/skills/sia-lead-compliance/SKILL.md +16 -0
- package/skills/sia-lead-drift-report/SKILL.md +16 -0
- package/skills/sia-lead-knowledge-map/SKILL.md +16 -0
- package/skills/sia-learn/SKILL.md +58 -0
- package/skills/sia-plan/SKILL.md +68 -0
- package/skills/sia-plan/plan-reviewer-prompt.md +63 -0
- package/skills/sia-playbooks/SKILL.md +29 -0
- package/skills/sia-playbooks/reference-feature.md +100 -0
- package/skills/sia-playbooks/reference-flagging.md +50 -0
- package/skills/sia-playbooks/reference-orientation.md +92 -0
- package/skills/sia-playbooks/reference-regression.md +115 -0
- package/skills/sia-playbooks/reference-review.md +64 -0
- package/skills/sia-playbooks/reference-tools.md +239 -0
- package/skills/sia-pm-decision-log/SKILL.md +28 -0
- package/skills/sia-pm-risk-dashboard/SKILL.md +24 -0
- package/skills/sia-pm-sprint-summary/SKILL.md +27 -0
- package/skills/sia-prune/SKILL.md +45 -0
- package/skills/sia-qa-coverage/SKILL.md +28 -0
- package/skills/sia-qa-flaky/SKILL.md +20 -0
- package/skills/sia-qa-report/SKILL.md +26 -0
- package/skills/sia-reindex/SKILL.md +30 -0
- package/skills/sia-review-respond/SKILL.md +88 -0
- package/skills/sia-review-respond/pushback-patterns.md +90 -0
- package/skills/sia-search/SKILL.md +47 -0
- package/skills/sia-setup/SKILL.md +82 -0
- package/skills/sia-setup/setup-checklist.md +97 -0
- package/skills/sia-stats/SKILL.md +36 -0
- package/skills/sia-status/SKILL.md +44 -0
- package/skills/sia-sync/SKILL.md +46 -0
- package/skills/sia-team/SKILL.md +64 -0
- package/skills/sia-test/SKILL.md +92 -0
- package/skills/sia-test/testing-anti-patterns.md +104 -0
- package/skills/sia-tour/SKILL.md +29 -0
- package/skills/sia-upgrade/SKILL.md +43 -0
- package/skills/sia-verify/SKILL.md +81 -0
- package/skills/sia-visualize/SKILL.md +28 -0
- package/skills/sia-visualize-live/SKILL.md +55 -0
- package/skills/sia-visualize-live/scripts/graph-template.html +389 -0
- package/skills/sia-visualize-live/scripts/start-visualizer.sh +161 -0
- package/skills/sia-visualize-live/scripts/stop-visualizer.sh +55 -0
- package/skills/sia-visualize-live/scripts/visualizer-server.cjs +264 -0
- package/skills/sia-workspace/SKILL.md +57 -0
- package/src/agent/claude-md-template-flagging.md +219 -0
- package/src/agent/claude-md-template.md +213 -0
- package/src/agent/modules/sia-feature.md +100 -0
- package/src/agent/modules/sia-flagging.md +50 -0
- package/src/agent/modules/sia-orientation.md +92 -0
- package/src/agent/modules/sia-regression.md +115 -0
- package/src/agent/modules/sia-review.md +64 -0
- package/src/agent/modules/sia-tools.md +239 -0
- package/src/ast/extractors/c-include.ts +189 -0
- package/src/ast/extractors/csharp-project.ts +260 -0
- package/src/ast/extractors/prisma-schema.ts +44 -0
- package/src/ast/extractors/project-manifest.ts +111 -0
- package/src/ast/extractors/sql-schema.ts +67 -0
- package/src/ast/extractors/tier-a.ts +423 -0
- package/src/ast/extractors/tier-b.ts +289 -0
- package/src/ast/extractors/tier-dispatch.ts +247 -0
- package/src/ast/index-worker.ts +108 -0
- package/src/ast/indexer.ts +484 -0
- package/src/ast/languages.ts +408 -0
- package/src/ast/pagerank-builder.ts +125 -0
- package/src/ast/path-utils.ts +137 -0
- package/src/ast/tree-sitter/backends/native.ts +57 -0
- package/src/ast/tree-sitter/backends/wasm.ts +39 -0
- package/src/ast/tree-sitter/call-walker.ts +44 -0
- package/src/ast/tree-sitter/edit-computer.ts +55 -0
- package/src/ast/tree-sitter/query-runner.ts +46 -0
- package/src/ast/tree-sitter/service.ts +174 -0
- package/src/ast/tree-sitter/tree-cache.ts +39 -0
- package/src/ast/tree-sitter/types.ts +79 -0
- package/src/ast/watcher.ts +322 -0
- package/src/capture/chunker.ts +169 -0
- package/src/capture/consolidate.ts +127 -0
- package/src/capture/edge-inferrer.ts +161 -0
- package/src/capture/embedder.ts +166 -0
- package/src/capture/embedding-cache.ts +73 -0
- package/src/capture/flag-processor.ts +64 -0
- package/src/capture/hook.ts +67 -0
- package/src/capture/pipeline.ts +450 -0
- package/src/capture/prompts/consolidate.ts +25 -0
- package/src/capture/prompts/edge-infer.ts +29 -0
- package/src/capture/prompts/extract-flagged.ts +36 -0
- package/src/capture/prompts/extract.ts +42 -0
- package/src/capture/tokenizer.ts +147 -0
- package/src/capture/track-a-ast.ts +93 -0
- package/src/capture/track-b-llm.ts +149 -0
- package/src/capture/types.ts +64 -0
- package/src/cli/commands/community.ts +137 -0
- package/src/cli/commands/compare.ts +123 -0
- package/src/cli/commands/conflicts.ts +41 -0
- package/src/cli/commands/digest.ts +197 -0
- package/src/cli/commands/disable-flagging.ts +34 -0
- package/src/cli/commands/doctor.ts +240 -0
- package/src/cli/commands/download-model.ts +161 -0
- package/src/cli/commands/enable-flagging.ts +34 -0
- package/src/cli/commands/export-knowledge.ts +208 -0
- package/src/cli/commands/export.ts +85 -0
- package/src/cli/commands/freshness.ts +164 -0
- package/src/cli/commands/graph.ts +51 -0
- package/src/cli/commands/history.ts +139 -0
- package/src/cli/commands/import.ts +335 -0
- package/src/cli/commands/install.ts +156 -0
- package/src/cli/commands/lead-report.ts +241 -0
- package/src/cli/commands/learn.ts +321 -0
- package/src/cli/commands/pm-report.ts +413 -0
- package/src/cli/commands/prune.ts +75 -0
- package/src/cli/commands/qa-report.ts +278 -0
- package/src/cli/commands/reindex.ts +104 -0
- package/src/cli/commands/rollback.ts +70 -0
- package/src/cli/commands/search.ts +103 -0
- package/src/cli/commands/server.ts +91 -0
- package/src/cli/commands/share.ts +33 -0
- package/src/cli/commands/stats.ts +79 -0
- package/src/cli/commands/status.ts +176 -0
- package/src/cli/commands/sync.ts +96 -0
- package/src/cli/commands/team.ts +118 -0
- package/src/cli/commands/tour.ts +157 -0
- package/src/cli/commands/visualize-live.ts +162 -0
- package/src/cli/commands/workspace.ts +117 -0
- package/src/cli/index.ts +424 -0
- package/src/cli/learn-progress.ts +87 -0
- package/src/community/detection-bridge.ts +344 -0
- package/src/community/leiden.ts +462 -0
- package/src/community/raptor.ts +210 -0
- package/src/community/scheduler.ts +74 -0
- package/src/community/summarize.ts +115 -0
- package/src/decay/archiver.ts +73 -0
- package/src/decay/bridge-orphan-cleanup.ts +212 -0
- package/src/decay/consolidation-sweep.ts +112 -0
- package/src/decay/decay.ts +116 -0
- package/src/decay/deep-validator.ts +62 -0
- package/src/decay/episodic-promoter.ts +132 -0
- package/src/decay/maintenance-scheduler.ts +326 -0
- package/src/decay/scheduler.ts +6 -0
- package/src/decay/session-sweeper.ts +79 -0
- package/src/decay/types.ts +17 -0
- package/src/freshness/confidence-decay.ts +122 -0
- package/src/freshness/cuckoo-filter.ts +176 -0
- package/src/freshness/deep-validation.ts +345 -0
- package/src/freshness/dirty-tracker.ts +237 -0
- package/src/freshness/file-watcher-layer.ts +119 -0
- package/src/freshness/firewall.ts +64 -0
- package/src/freshness/git-reconcile-layer.ts +161 -0
- package/src/freshness/inverted-index.ts +158 -0
- package/src/freshness/stale-read-layer.ts +222 -0
- package/src/graph/audit.ts +69 -0
- package/src/graph/bridge-db.ts +141 -0
- package/src/graph/communities.ts +195 -0
- package/src/graph/db-interface.ts +259 -0
- package/src/graph/edges.ts +163 -0
- package/src/graph/entities.ts +327 -0
- package/src/graph/episodic-db.ts +113 -0
- package/src/graph/flags.ts +31 -0
- package/src/graph/meta-db.ts +200 -0
- package/src/graph/semantic-db.ts +101 -0
- package/src/graph/session-resume.ts +56 -0
- package/src/graph/snapshots.ts +342 -0
- package/src/graph/staging.ts +151 -0
- package/src/graph/types.ts +128 -0
- package/src/hooks/adapters/claude-code.ts +21 -0
- package/src/hooks/adapters/cline.ts +43 -0
- package/src/hooks/adapters/cursor.ts +65 -0
- package/src/hooks/adapters/generic.ts +12 -0
- package/src/hooks/agent-detect.ts +34 -0
- package/src/hooks/claude-md-directives.ts +32 -0
- package/src/hooks/event-router.ts +182 -0
- package/src/hooks/extractors/pattern-detector.ts +111 -0
- package/src/hooks/handlers/post-compact.ts +30 -0
- package/src/hooks/handlers/post-tool-use.ts +403 -0
- package/src/hooks/handlers/pre-compact.ts +100 -0
- package/src/hooks/handlers/session-end.ts +47 -0
- package/src/hooks/handlers/session-start.ts +154 -0
- package/src/hooks/handlers/stop.ts +128 -0
- package/src/hooks/handlers/user-prompt-submit.ts +68 -0
- package/src/hooks/plugin-branch-switch.ts +68 -0
- package/src/hooks/plugin-common.ts +47 -0
- package/src/hooks/plugin-post-compact.ts +28 -0
- package/src/hooks/plugin-post-tool-use.ts +38 -0
- package/src/hooks/plugin-pre-compact.ts +37 -0
- package/src/hooks/plugin-session-end.ts +37 -0
- package/src/hooks/plugin-session-start.ts +75 -0
- package/src/hooks/plugin-stop.ts +61 -0
- package/src/hooks/plugin-user-prompt-submit.ts +47 -0
- package/src/hooks/types.ts +43 -0
- package/src/knowledge/discovery.ts +238 -0
- package/src/knowledge/external-refs.ts +98 -0
- package/src/knowledge/freshness.ts +221 -0
- package/src/knowledge/ingest.ts +330 -0
- package/src/knowledge/markdown-export.ts +229 -0
- package/src/knowledge/markdown-import.ts +359 -0
- package/src/knowledge/patterns.ts +74 -0
- package/src/knowledge/templates.ts +307 -0
- package/src/llm/ai-sdk-adapter.ts +46 -0
- package/src/llm/config.ts +88 -0
- package/src/llm/cost-tracker.ts +110 -0
- package/src/llm/prompts/extraction.ts +55 -0
- package/src/llm/prompts/summarization.ts +36 -0
- package/src/llm/prompts/validation.ts +37 -0
- package/src/llm/provider-registry.ts +68 -0
- package/src/llm/reliability.ts +179 -0
- package/src/llm/schemas.ts +52 -0
- package/src/mcp/freshness-annotator.ts +69 -0
- package/src/mcp/server.ts +949 -0
- package/src/mcp/tools/sia-ast-query.ts +225 -0
- package/src/mcp/tools/sia-at-time.ts +151 -0
- package/src/mcp/tools/sia-backlinks.ts +87 -0
- package/src/mcp/tools/sia-batch-execute.ts +169 -0
- package/src/mcp/tools/sia-by-file.ts +89 -0
- package/src/mcp/tools/sia-community.ts +113 -0
- package/src/mcp/tools/sia-doctor.ts +73 -0
- package/src/mcp/tools/sia-execute-file.ts +122 -0
- package/src/mcp/tools/sia-execute.ts +104 -0
- package/src/mcp/tools/sia-expand.ts +158 -0
- package/src/mcp/tools/sia-fetch-and-index.ts +241 -0
- package/src/mcp/tools/sia-flag.ts +65 -0
- package/src/mcp/tools/sia-index.ts +111 -0
- package/src/mcp/tools/sia-note.ts +134 -0
- package/src/mcp/tools/sia-search.ts +105 -0
- package/src/mcp/tools/sia-stats.ts +63 -0
- package/src/mcp/tools/sia-sync-status.ts +44 -0
- package/src/mcp/tools/sia-upgrade.ts +247 -0
- package/src/mcp/truncate.ts +231 -0
- package/src/native/bridge.ts +167 -0
- package/src/native/fallback-ast-diff.ts +144 -0
- package/src/native/fallback-graph.ts +325 -0
- package/src/ontology/constraints.ts +56 -0
- package/src/ontology/errors.ts +8 -0
- package/src/ontology/middleware.ts +266 -0
- package/src/retrieval/bm25-search.ts +151 -0
- package/src/retrieval/context-assembly.ts +76 -0
- package/src/retrieval/graph-traversal.ts +168 -0
- package/src/retrieval/pagerank.ts +40 -0
- package/src/retrieval/query-classifier.ts +106 -0
- package/src/retrieval/reranker.ts +156 -0
- package/src/retrieval/search.ts +236 -0
- package/src/retrieval/throttle.ts +102 -0
- package/src/retrieval/vector-search.ts +203 -0
- package/src/retrieval/workspace-search.ts +130 -0
- package/src/sandbox/context-mode.ts +285 -0
- package/src/sandbox/credential-pass.ts +55 -0
- package/src/sandbox/executor.ts +235 -0
- package/src/security/pattern-detector.ts +127 -0
- package/src/security/rule-of-two.ts +50 -0
- package/src/security/sanitize.ts +46 -0
- package/src/security/semantic-consistency.ts +93 -0
- package/src/security/staging-promoter.ts +154 -0
- package/src/shared/config.ts +302 -0
- package/src/shared/diagnostics.ts +210 -0
- package/src/shared/errors.ts +48 -0
- package/src/shared/git-utils.ts +143 -0
- package/src/shared/llm-client.ts +120 -0
- package/src/shared/logger.ts +99 -0
- package/src/shared/types.ts +79 -0
- package/src/sync/client.ts +43 -0
- package/src/sync/conflict.ts +106 -0
- package/src/sync/dedup.ts +183 -0
- package/src/sync/hlc.ts +117 -0
- package/src/sync/keychain.ts +144 -0
- package/src/sync/pull.ts +232 -0
- package/src/sync/push.ts +131 -0
- package/src/types/chokidar.d.ts +23 -0
- package/src/visualization/graph-renderer.ts +312 -0
- package/src/visualization/subgraph-extract.ts +208 -0
- package/src/visualization/views/community-clusters.ts +246 -0
- package/src/visualization/views/dependency-map.ts +189 -0
- package/src/visualization/views/graph-explorer.ts +364 -0
- package/src/visualization/views/timeline.ts +247 -0
- package/src/workspace/api-contracts.ts +226 -0
- package/src/workspace/cross-repo.ts +61 -0
- package/src/workspace/detector.ts +190 -0
- package/src/workspace/manifest.ts +141 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>SIA Brainstorming</title>
|
|
6
|
+
<style>
|
|
7
|
+
/*
|
|
8
|
+
* BRAINSTORM COMPANION FRAME TEMPLATE
|
|
9
|
+
*
|
|
10
|
+
* This template provides a consistent frame with:
|
|
11
|
+
* - OS-aware light/dark theming
|
|
12
|
+
* - Fixed header and selection indicator bar
|
|
13
|
+
* - Scrollable main content area
|
|
14
|
+
* - CSS helpers for common UI patterns
|
|
15
|
+
*
|
|
16
|
+
* Content is injected via placeholder comment in #claude-content.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
20
|
+
html, body { height: 100%; overflow: hidden; }
|
|
21
|
+
|
|
22
|
+
/* ===== THEME VARIABLES ===== */
|
|
23
|
+
:root {
|
|
24
|
+
--bg-primary: #f5f5f7;
|
|
25
|
+
--bg-secondary: #ffffff;
|
|
26
|
+
--bg-tertiary: #e5e5e7;
|
|
27
|
+
--border: #d1d1d6;
|
|
28
|
+
--text-primary: #1d1d1f;
|
|
29
|
+
--text-secondary: #86868b;
|
|
30
|
+
--text-tertiary: #aeaeb2;
|
|
31
|
+
--accent: #0071e3;
|
|
32
|
+
--accent-hover: #0077ed;
|
|
33
|
+
--success: #34c759;
|
|
34
|
+
--warning: #ff9f0a;
|
|
35
|
+
--error: #ff3b30;
|
|
36
|
+
--selected-bg: #e8f4fd;
|
|
37
|
+
--selected-border: #0071e3;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@media (prefers-color-scheme: dark) {
|
|
41
|
+
:root {
|
|
42
|
+
--bg-primary: #1d1d1f;
|
|
43
|
+
--bg-secondary: #2d2d2f;
|
|
44
|
+
--bg-tertiary: #3d3d3f;
|
|
45
|
+
--border: #424245;
|
|
46
|
+
--text-primary: #f5f5f7;
|
|
47
|
+
--text-secondary: #86868b;
|
|
48
|
+
--text-tertiary: #636366;
|
|
49
|
+
--accent: #0a84ff;
|
|
50
|
+
--accent-hover: #409cff;
|
|
51
|
+
--selected-bg: rgba(10, 132, 255, 0.15);
|
|
52
|
+
--selected-border: #0a84ff;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
body {
|
|
57
|
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
|
58
|
+
background: var(--bg-primary);
|
|
59
|
+
color: var(--text-primary);
|
|
60
|
+
display: flex;
|
|
61
|
+
flex-direction: column;
|
|
62
|
+
line-height: 1.5;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* ===== FRAME STRUCTURE ===== */
|
|
66
|
+
.header {
|
|
67
|
+
background: var(--bg-secondary);
|
|
68
|
+
padding: 0.5rem 1.5rem;
|
|
69
|
+
display: flex;
|
|
70
|
+
justify-content: space-between;
|
|
71
|
+
align-items: center;
|
|
72
|
+
border-bottom: 1px solid var(--border);
|
|
73
|
+
flex-shrink: 0;
|
|
74
|
+
}
|
|
75
|
+
.header h1 { font-size: 0.85rem; font-weight: 500; color: var(--text-secondary); }
|
|
76
|
+
.header .status { font-size: 0.7rem; color: var(--success); display: flex; align-items: center; gap: 0.4rem; }
|
|
77
|
+
.header .status::before { content: ''; width: 6px; height: 6px; background: var(--success); border-radius: 50%; }
|
|
78
|
+
|
|
79
|
+
.main { flex: 1; overflow-y: auto; }
|
|
80
|
+
#claude-content { padding: 2rem; min-height: 100%; }
|
|
81
|
+
|
|
82
|
+
.indicator-bar {
|
|
83
|
+
background: var(--bg-secondary);
|
|
84
|
+
border-top: 1px solid var(--border);
|
|
85
|
+
padding: 0.5rem 1.5rem;
|
|
86
|
+
flex-shrink: 0;
|
|
87
|
+
text-align: center;
|
|
88
|
+
}
|
|
89
|
+
.indicator-bar span {
|
|
90
|
+
font-size: 0.75rem;
|
|
91
|
+
color: var(--text-secondary);
|
|
92
|
+
}
|
|
93
|
+
.indicator-bar .selected-text {
|
|
94
|
+
color: var(--accent);
|
|
95
|
+
font-weight: 500;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* ===== TYPOGRAPHY ===== */
|
|
99
|
+
h2 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.5rem; }
|
|
100
|
+
h3 { font-size: 1.1rem; font-weight: 600; margin-bottom: 0.25rem; }
|
|
101
|
+
.subtitle { color: var(--text-secondary); margin-bottom: 1.5rem; }
|
|
102
|
+
.section { margin-bottom: 2rem; }
|
|
103
|
+
.label { font-size: 0.7rem; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.5rem; }
|
|
104
|
+
|
|
105
|
+
/* ===== OPTIONS (for A/B/C choices) ===== */
|
|
106
|
+
.options { display: flex; flex-direction: column; gap: 0.75rem; }
|
|
107
|
+
.option {
|
|
108
|
+
background: var(--bg-secondary);
|
|
109
|
+
border: 2px solid var(--border);
|
|
110
|
+
border-radius: 12px;
|
|
111
|
+
padding: 1rem 1.25rem;
|
|
112
|
+
cursor: pointer;
|
|
113
|
+
transition: all 0.15s ease;
|
|
114
|
+
display: flex;
|
|
115
|
+
align-items: flex-start;
|
|
116
|
+
gap: 1rem;
|
|
117
|
+
}
|
|
118
|
+
.option:hover { border-color: var(--accent); }
|
|
119
|
+
.option.selected { background: var(--selected-bg); border-color: var(--selected-border); }
|
|
120
|
+
.option .letter {
|
|
121
|
+
background: var(--bg-tertiary);
|
|
122
|
+
color: var(--text-secondary);
|
|
123
|
+
width: 1.75rem; height: 1.75rem;
|
|
124
|
+
border-radius: 6px;
|
|
125
|
+
display: flex; align-items: center; justify-content: center;
|
|
126
|
+
font-weight: 600; font-size: 0.85rem; flex-shrink: 0;
|
|
127
|
+
}
|
|
128
|
+
.option.selected .letter { background: var(--accent); color: white; }
|
|
129
|
+
.option .content { flex: 1; }
|
|
130
|
+
.option .content h3 { font-size: 0.95rem; margin-bottom: 0.15rem; }
|
|
131
|
+
.option .content p { color: var(--text-secondary); font-size: 0.85rem; margin: 0; }
|
|
132
|
+
|
|
133
|
+
/* ===== CARDS (for showing designs/mockups) ===== */
|
|
134
|
+
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }
|
|
135
|
+
.card {
|
|
136
|
+
background: var(--bg-secondary);
|
|
137
|
+
border: 1px solid var(--border);
|
|
138
|
+
border-radius: 12px;
|
|
139
|
+
overflow: hidden;
|
|
140
|
+
cursor: pointer;
|
|
141
|
+
transition: all 0.15s ease;
|
|
142
|
+
}
|
|
143
|
+
.card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
|
|
144
|
+
.card.selected { border-color: var(--selected-border); border-width: 2px; }
|
|
145
|
+
.card-image { background: var(--bg-tertiary); aspect-ratio: 16/10; display: flex; align-items: center; justify-content: center; }
|
|
146
|
+
.card-body { padding: 1rem; }
|
|
147
|
+
.card-body h3 { margin-bottom: 0.25rem; }
|
|
148
|
+
.card-body p { color: var(--text-secondary); font-size: 0.85rem; }
|
|
149
|
+
|
|
150
|
+
/* ===== MOCKUP CONTAINER ===== */
|
|
151
|
+
.mockup {
|
|
152
|
+
background: var(--bg-secondary);
|
|
153
|
+
border: 1px solid var(--border);
|
|
154
|
+
border-radius: 12px;
|
|
155
|
+
overflow: hidden;
|
|
156
|
+
margin-bottom: 1.5rem;
|
|
157
|
+
}
|
|
158
|
+
.mockup-header {
|
|
159
|
+
background: var(--bg-tertiary);
|
|
160
|
+
padding: 0.5rem 1rem;
|
|
161
|
+
font-size: 0.75rem;
|
|
162
|
+
color: var(--text-secondary);
|
|
163
|
+
border-bottom: 1px solid var(--border);
|
|
164
|
+
}
|
|
165
|
+
.mockup-body { padding: 1.5rem; }
|
|
166
|
+
|
|
167
|
+
/* ===== SPLIT VIEW (side-by-side comparison) ===== */
|
|
168
|
+
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
|
|
169
|
+
@media (max-width: 700px) { .split { grid-template-columns: 1fr; } }
|
|
170
|
+
|
|
171
|
+
/* ===== PROS/CONS ===== */
|
|
172
|
+
.pros-cons { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin: 1rem 0; }
|
|
173
|
+
.pros, .cons { background: var(--bg-secondary); border-radius: 8px; padding: 1rem; }
|
|
174
|
+
.pros h4 { color: var(--success); font-size: 0.85rem; margin-bottom: 0.5rem; }
|
|
175
|
+
.cons h4 { color: var(--error); font-size: 0.85rem; margin-bottom: 0.5rem; }
|
|
176
|
+
.pros ul, .cons ul { margin-left: 1.25rem; font-size: 0.85rem; color: var(--text-secondary); }
|
|
177
|
+
.pros li, .cons li { margin-bottom: 0.25rem; }
|
|
178
|
+
|
|
179
|
+
/* ===== PLACEHOLDER (for mockup areas) ===== */
|
|
180
|
+
.placeholder {
|
|
181
|
+
background: var(--bg-tertiary);
|
|
182
|
+
border: 2px dashed var(--border);
|
|
183
|
+
border-radius: 8px;
|
|
184
|
+
padding: 2rem;
|
|
185
|
+
text-align: center;
|
|
186
|
+
color: var(--text-tertiary);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* ===== INLINE MOCKUP ELEMENTS ===== */
|
|
190
|
+
.mock-nav { background: var(--accent); color: white; padding: 0.75rem 1rem; display: flex; gap: 1.5rem; font-size: 0.9rem; }
|
|
191
|
+
.mock-sidebar { background: var(--bg-tertiary); padding: 1rem; min-width: 180px; }
|
|
192
|
+
.mock-content { padding: 1.5rem; flex: 1; }
|
|
193
|
+
.mock-button { background: var(--accent); color: white; border: none; padding: 0.5rem 1rem; border-radius: 6px; font-size: 0.85rem; }
|
|
194
|
+
.mock-input { background: var(--bg-primary); border: 1px solid var(--border); border-radius: 6px; padding: 0.5rem; width: 100%; }
|
|
195
|
+
</style>
|
|
196
|
+
</head>
|
|
197
|
+
<body>
|
|
198
|
+
<div class="header">
|
|
199
|
+
<h1>SIA Brainstorming</h1>
|
|
200
|
+
<div class="status">Connected</div>
|
|
201
|
+
</div>
|
|
202
|
+
|
|
203
|
+
<div class="main">
|
|
204
|
+
<div id="claude-content">
|
|
205
|
+
<!-- CONTENT -->
|
|
206
|
+
</div>
|
|
207
|
+
</div>
|
|
208
|
+
|
|
209
|
+
<div class="indicator-bar">
|
|
210
|
+
<span id="indicator-text">Click an option above, then return to the terminal</span>
|
|
211
|
+
</div>
|
|
212
|
+
|
|
213
|
+
</body>
|
|
214
|
+
</html>
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// SIA Brainstorm Companion - Client-side helper
|
|
2
|
+
// Manages WebSocket connection and user interaction tracking.
|
|
3
|
+
// Note: innerHTML usage below is safe — content is agent-generated,
|
|
4
|
+
// not external user input. This runs on a local-only dev server.
|
|
5
|
+
|
|
6
|
+
(function() {
|
|
7
|
+
const WS_URL = 'ws://' + window.location.host;
|
|
8
|
+
let ws = null;
|
|
9
|
+
let eventQueue = [];
|
|
10
|
+
|
|
11
|
+
function connect() {
|
|
12
|
+
ws = new WebSocket(WS_URL);
|
|
13
|
+
|
|
14
|
+
ws.onopen = () => {
|
|
15
|
+
eventQueue.forEach(e => ws.send(JSON.stringify(e)));
|
|
16
|
+
eventQueue = [];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
ws.onmessage = (msg) => {
|
|
20
|
+
const data = JSON.parse(msg.data);
|
|
21
|
+
if (data.type === 'reload') {
|
|
22
|
+
window.location.reload();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
ws.onclose = () => {
|
|
27
|
+
setTimeout(connect, 1000);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function sendEvent(event) {
|
|
32
|
+
event.timestamp = Date.now();
|
|
33
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
34
|
+
ws.send(JSON.stringify(event));
|
|
35
|
+
} else {
|
|
36
|
+
eventQueue.push(event);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Capture clicks on choice elements
|
|
41
|
+
document.addEventListener('click', (e) => {
|
|
42
|
+
const target = e.target.closest('[data-choice]');
|
|
43
|
+
if (!target) return;
|
|
44
|
+
|
|
45
|
+
sendEvent({
|
|
46
|
+
type: 'click',
|
|
47
|
+
text: target.textContent.trim(),
|
|
48
|
+
choice: target.dataset.choice,
|
|
49
|
+
id: target.id || null
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Update indicator bar (defer so toggleSelect runs first)
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
const indicator = document.getElementById('indicator-text');
|
|
55
|
+
if (!indicator) return;
|
|
56
|
+
const container = target.closest('.options') || target.closest('.cards');
|
|
57
|
+
const selected = container ? container.querySelectorAll('.selected') : [];
|
|
58
|
+
if (selected.length === 0) {
|
|
59
|
+
indicator.textContent = 'Click an option above, then return to the terminal';
|
|
60
|
+
} else if (selected.length === 1) {
|
|
61
|
+
const label = selected[0].querySelector('h3, .content h3, .card-body h3')?.textContent?.trim() || selected[0].dataset.choice;
|
|
62
|
+
// Safe: label is extracted from agent-generated DOM content, not external input
|
|
63
|
+
indicator.innerHTML = '<span class="selected-text">' + label + ' selected</span> \u2014 return to terminal to continue';
|
|
64
|
+
} else {
|
|
65
|
+
// Safe: selected.length is a number, not user input
|
|
66
|
+
indicator.innerHTML = '<span class="selected-text">' + selected.length + ' selected</span> \u2014 return to terminal to continue';
|
|
67
|
+
}
|
|
68
|
+
}, 0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Frame UI: selection tracking
|
|
72
|
+
window.selectedChoice = null;
|
|
73
|
+
|
|
74
|
+
window.toggleSelect = function(el) {
|
|
75
|
+
const container = el.closest('.options') || el.closest('.cards');
|
|
76
|
+
const multi = container && container.dataset.multiselect !== undefined;
|
|
77
|
+
if (container && !multi) {
|
|
78
|
+
container.querySelectorAll('.option, .card').forEach(o => o.classList.remove('selected'));
|
|
79
|
+
}
|
|
80
|
+
if (multi) {
|
|
81
|
+
el.classList.toggle('selected');
|
|
82
|
+
} else {
|
|
83
|
+
el.classList.add('selected');
|
|
84
|
+
}
|
|
85
|
+
window.selectedChoice = el.dataset.choice;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Expose API for explicit use
|
|
89
|
+
window.brainstorm = {
|
|
90
|
+
send: sendEvent,
|
|
91
|
+
choice: (value, metadata = {}) => sendEvent({ type: 'choice', value, ...metadata })
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
connect();
|
|
95
|
+
})();
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
const http = require('http');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
// ========== WebSocket Protocol (RFC 6455) ==========
|
|
7
|
+
|
|
8
|
+
const OPCODES = { TEXT: 0x01, CLOSE: 0x08, PING: 0x09, PONG: 0x0A };
|
|
9
|
+
const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
10
|
+
|
|
11
|
+
function computeAcceptKey(clientKey) {
|
|
12
|
+
return crypto.createHash('sha1').update(clientKey + WS_MAGIC).digest('base64');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function encodeFrame(opcode, payload) {
|
|
16
|
+
const fin = 0x80;
|
|
17
|
+
const len = payload.length;
|
|
18
|
+
let header;
|
|
19
|
+
|
|
20
|
+
if (len < 126) {
|
|
21
|
+
header = Buffer.alloc(2);
|
|
22
|
+
header[0] = fin | opcode;
|
|
23
|
+
header[1] = len;
|
|
24
|
+
} else if (len < 65536) {
|
|
25
|
+
header = Buffer.alloc(4);
|
|
26
|
+
header[0] = fin | opcode;
|
|
27
|
+
header[1] = 126;
|
|
28
|
+
header.writeUInt16BE(len, 2);
|
|
29
|
+
} else {
|
|
30
|
+
header = Buffer.alloc(10);
|
|
31
|
+
header[0] = fin | opcode;
|
|
32
|
+
header[1] = 127;
|
|
33
|
+
header.writeBigUInt64BE(BigInt(len), 2);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return Buffer.concat([header, payload]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function decodeFrame(buffer) {
|
|
40
|
+
if (buffer.length < 2) return null;
|
|
41
|
+
|
|
42
|
+
const secondByte = buffer[1];
|
|
43
|
+
const opcode = buffer[0] & 0x0F;
|
|
44
|
+
const masked = (secondByte & 0x80) !== 0;
|
|
45
|
+
let payloadLen = secondByte & 0x7F;
|
|
46
|
+
let offset = 2;
|
|
47
|
+
|
|
48
|
+
if (!masked) throw new Error('Client frames must be masked');
|
|
49
|
+
|
|
50
|
+
if (payloadLen === 126) {
|
|
51
|
+
if (buffer.length < 4) return null;
|
|
52
|
+
payloadLen = buffer.readUInt16BE(2);
|
|
53
|
+
offset = 4;
|
|
54
|
+
} else if (payloadLen === 127) {
|
|
55
|
+
if (buffer.length < 10) return null;
|
|
56
|
+
payloadLen = Number(buffer.readBigUInt64BE(2));
|
|
57
|
+
offset = 10;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const maskOffset = offset;
|
|
61
|
+
const dataOffset = offset + 4;
|
|
62
|
+
const totalLen = dataOffset + payloadLen;
|
|
63
|
+
if (buffer.length < totalLen) return null;
|
|
64
|
+
|
|
65
|
+
const mask = buffer.slice(maskOffset, dataOffset);
|
|
66
|
+
const data = Buffer.alloc(payloadLen);
|
|
67
|
+
for (let i = 0; i < payloadLen; i++) {
|
|
68
|
+
data[i] = buffer[dataOffset + i] ^ mask[i % 4];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return { opcode, payload: data, bytesConsumed: totalLen };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ========== Configuration ==========
|
|
75
|
+
|
|
76
|
+
const PORT = process.env.BRAINSTORM_PORT || (49152 + Math.floor(Math.random() * 16383));
|
|
77
|
+
const HOST = process.env.BRAINSTORM_HOST || '127.0.0.1';
|
|
78
|
+
const URL_HOST = process.env.BRAINSTORM_URL_HOST || (HOST === '127.0.0.1' ? 'localhost' : HOST);
|
|
79
|
+
const SCREEN_DIR = process.env.BRAINSTORM_DIR || '/tmp/brainstorm';
|
|
80
|
+
const OWNER_PID = process.env.BRAINSTORM_OWNER_PID ? Number(process.env.BRAINSTORM_OWNER_PID) : null;
|
|
81
|
+
|
|
82
|
+
const MIME_TYPES = {
|
|
83
|
+
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
|
|
84
|
+
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
|
|
85
|
+
'.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml'
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// ========== Templates and Constants ==========
|
|
89
|
+
|
|
90
|
+
const WAITING_PAGE = `<!DOCTYPE html>
|
|
91
|
+
<html>
|
|
92
|
+
<head><meta charset="utf-8"><title>SIA Brainstorm Companion</title>
|
|
93
|
+
<style>body { font-family: system-ui, sans-serif; padding: 2rem; max-width: 800px; margin: 0 auto; }
|
|
94
|
+
h1 { color: #333; } p { color: #666; }</style>
|
|
95
|
+
</head>
|
|
96
|
+
<body><h1>SIA Brainstorm Companion</h1>
|
|
97
|
+
<p>Waiting for the agent to push a screen...</p></body></html>`;
|
|
98
|
+
|
|
99
|
+
const frameTemplate = fs.readFileSync(path.join(__dirname, 'frame-template.html'), 'utf-8');
|
|
100
|
+
const helperScript = fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8');
|
|
101
|
+
const helperInjection = '<script>\n' + helperScript + '\n</script>';
|
|
102
|
+
|
|
103
|
+
// ========== Helper Functions ==========
|
|
104
|
+
|
|
105
|
+
function isFullDocument(html) {
|
|
106
|
+
const trimmed = html.trimStart().toLowerCase();
|
|
107
|
+
return trimmed.startsWith('<!doctype') || trimmed.startsWith('<html');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function wrapInFrame(content) {
|
|
111
|
+
return frameTemplate.replace('<!-- CONTENT -->', content);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function getNewestScreen() {
|
|
115
|
+
const files = fs.readdirSync(SCREEN_DIR)
|
|
116
|
+
.filter(f => f.endsWith('.html'))
|
|
117
|
+
.map(f => {
|
|
118
|
+
const fp = path.join(SCREEN_DIR, f);
|
|
119
|
+
return { path: fp, mtime: fs.statSync(fp).mtime.getTime() };
|
|
120
|
+
})
|
|
121
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
122
|
+
return files.length > 0 ? files[0].path : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ========== HTTP Request Handler ==========
|
|
126
|
+
|
|
127
|
+
function handleRequest(req, res) {
|
|
128
|
+
touchActivity();
|
|
129
|
+
if (req.method === 'GET' && req.url === '/') {
|
|
130
|
+
const screenFile = getNewestScreen();
|
|
131
|
+
let html = screenFile
|
|
132
|
+
? (raw => isFullDocument(raw) ? raw : wrapInFrame(raw))(fs.readFileSync(screenFile, 'utf-8'))
|
|
133
|
+
: WAITING_PAGE;
|
|
134
|
+
|
|
135
|
+
if (html.includes('</body>')) {
|
|
136
|
+
html = html.replace('</body>', helperInjection + '\n</body>');
|
|
137
|
+
} else {
|
|
138
|
+
html += helperInjection;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
142
|
+
res.end(html);
|
|
143
|
+
} else if (req.method === 'GET' && req.url.startsWith('/files/')) {
|
|
144
|
+
const fileName = req.url.slice(7);
|
|
145
|
+
const filePath = path.join(SCREEN_DIR, path.basename(fileName));
|
|
146
|
+
if (!fs.existsSync(filePath)) {
|
|
147
|
+
res.writeHead(404);
|
|
148
|
+
res.end('Not found');
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
152
|
+
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
153
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
154
|
+
res.end(fs.readFileSync(filePath));
|
|
155
|
+
} else {
|
|
156
|
+
res.writeHead(404);
|
|
157
|
+
res.end('Not found');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ========== WebSocket Connection Handling ==========
|
|
162
|
+
|
|
163
|
+
const clients = new Set();
|
|
164
|
+
|
|
165
|
+
function handleUpgrade(req, socket) {
|
|
166
|
+
const key = req.headers['sec-websocket-key'];
|
|
167
|
+
if (!key) { socket.destroy(); return; }
|
|
168
|
+
|
|
169
|
+
const accept = computeAcceptKey(key);
|
|
170
|
+
socket.write(
|
|
171
|
+
'HTTP/1.1 101 Switching Protocols\r\n' +
|
|
172
|
+
'Upgrade: websocket\r\n' +
|
|
173
|
+
'Connection: Upgrade\r\n' +
|
|
174
|
+
'Sec-WebSocket-Accept: ' + accept + '\r\n\r\n'
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
let buffer = Buffer.alloc(0);
|
|
178
|
+
clients.add(socket);
|
|
179
|
+
|
|
180
|
+
socket.on('data', (chunk) => {
|
|
181
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
182
|
+
while (buffer.length > 0) {
|
|
183
|
+
let result;
|
|
184
|
+
try {
|
|
185
|
+
result = decodeFrame(buffer);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
|
|
188
|
+
clients.delete(socket);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (!result) break;
|
|
192
|
+
buffer = buffer.slice(result.bytesConsumed);
|
|
193
|
+
|
|
194
|
+
switch (result.opcode) {
|
|
195
|
+
case OPCODES.TEXT:
|
|
196
|
+
handleMessage(result.payload.toString());
|
|
197
|
+
break;
|
|
198
|
+
case OPCODES.CLOSE:
|
|
199
|
+
socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
|
|
200
|
+
clients.delete(socket);
|
|
201
|
+
return;
|
|
202
|
+
case OPCODES.PING:
|
|
203
|
+
socket.write(encodeFrame(OPCODES.PONG, result.payload));
|
|
204
|
+
break;
|
|
205
|
+
case OPCODES.PONG:
|
|
206
|
+
break;
|
|
207
|
+
default: {
|
|
208
|
+
const closeBuf = Buffer.alloc(2);
|
|
209
|
+
closeBuf.writeUInt16BE(1003);
|
|
210
|
+
socket.end(encodeFrame(OPCODES.CLOSE, closeBuf));
|
|
211
|
+
clients.delete(socket);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
socket.on('close', () => clients.delete(socket));
|
|
219
|
+
socket.on('error', () => clients.delete(socket));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function handleMessage(text) {
|
|
223
|
+
let event;
|
|
224
|
+
try {
|
|
225
|
+
event = JSON.parse(text);
|
|
226
|
+
} catch (e) {
|
|
227
|
+
console.error('Failed to parse WebSocket message:', e.message);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
touchActivity();
|
|
231
|
+
console.log(JSON.stringify({ source: 'user-event', ...event }));
|
|
232
|
+
if (event.choice) {
|
|
233
|
+
const eventsFile = path.join(SCREEN_DIR, '.events');
|
|
234
|
+
fs.appendFileSync(eventsFile, JSON.stringify(event) + '\n');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function broadcast(msg) {
|
|
239
|
+
const frame = encodeFrame(OPCODES.TEXT, Buffer.from(JSON.stringify(msg)));
|
|
240
|
+
for (const socket of clients) {
|
|
241
|
+
try { socket.write(frame); } catch (e) { clients.delete(socket); }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ========== Activity Tracking ==========
|
|
246
|
+
|
|
247
|
+
const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
|
|
248
|
+
let lastActivity = Date.now();
|
|
249
|
+
|
|
250
|
+
function touchActivity() {
|
|
251
|
+
lastActivity = Date.now();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ========== File Watching ==========
|
|
255
|
+
|
|
256
|
+
const debounceTimers = new Map();
|
|
257
|
+
|
|
258
|
+
// ========== Server Startup ==========
|
|
259
|
+
|
|
260
|
+
function startServer() {
|
|
261
|
+
if (!fs.existsSync(SCREEN_DIR)) fs.mkdirSync(SCREEN_DIR, { recursive: true });
|
|
262
|
+
|
|
263
|
+
// Track known files to distinguish new screens from updates.
|
|
264
|
+
// macOS fs.watch reports 'rename' for both new files and overwrites,
|
|
265
|
+
// so we can't rely on eventType alone.
|
|
266
|
+
const knownFiles = new Set(
|
|
267
|
+
fs.readdirSync(SCREEN_DIR).filter(f => f.endsWith('.html'))
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
const server = http.createServer(handleRequest);
|
|
271
|
+
server.on('upgrade', handleUpgrade);
|
|
272
|
+
|
|
273
|
+
const watcher = fs.watch(SCREEN_DIR, (eventType, filename) => {
|
|
274
|
+
if (!filename || !filename.endsWith('.html')) return;
|
|
275
|
+
|
|
276
|
+
if (debounceTimers.has(filename)) clearTimeout(debounceTimers.get(filename));
|
|
277
|
+
debounceTimers.set(filename, setTimeout(() => {
|
|
278
|
+
debounceTimers.delete(filename);
|
|
279
|
+
const filePath = path.join(SCREEN_DIR, filename);
|
|
280
|
+
|
|
281
|
+
if (!fs.existsSync(filePath)) return; // file was deleted
|
|
282
|
+
touchActivity();
|
|
283
|
+
|
|
284
|
+
if (!knownFiles.has(filename)) {
|
|
285
|
+
knownFiles.add(filename);
|
|
286
|
+
const eventsFile = path.join(SCREEN_DIR, '.events');
|
|
287
|
+
if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
|
|
288
|
+
console.log(JSON.stringify({ type: 'screen-added', file: filePath }));
|
|
289
|
+
} else {
|
|
290
|
+
console.log(JSON.stringify({ type: 'screen-updated', file: filePath }));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
broadcast({ type: 'reload' });
|
|
294
|
+
}, 100));
|
|
295
|
+
});
|
|
296
|
+
watcher.on('error', (err) => console.error('fs.watch error:', err.message));
|
|
297
|
+
|
|
298
|
+
function shutdown(reason) {
|
|
299
|
+
console.log(JSON.stringify({ type: 'server-stopped', reason }));
|
|
300
|
+
const infoFile = path.join(SCREEN_DIR, '.server-info');
|
|
301
|
+
if (fs.existsSync(infoFile)) fs.unlinkSync(infoFile);
|
|
302
|
+
fs.writeFileSync(
|
|
303
|
+
path.join(SCREEN_DIR, '.server-stopped'),
|
|
304
|
+
JSON.stringify({ reason, timestamp: Date.now() }) + '\n'
|
|
305
|
+
);
|
|
306
|
+
watcher.close();
|
|
307
|
+
clearInterval(lifecycleCheck);
|
|
308
|
+
server.close(() => process.exit(0));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function ownerAlive() {
|
|
312
|
+
if (!OWNER_PID) return true;
|
|
313
|
+
try { process.kill(OWNER_PID, 0); return true; } catch (e) { return false; }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Check every 60s: exit if owner process died or idle for 30 minutes
|
|
317
|
+
const lifecycleCheck = setInterval(() => {
|
|
318
|
+
if (!ownerAlive()) shutdown('owner process exited');
|
|
319
|
+
else if (Date.now() - lastActivity > IDLE_TIMEOUT_MS) shutdown('idle timeout');
|
|
320
|
+
}, 60 * 1000);
|
|
321
|
+
lifecycleCheck.unref();
|
|
322
|
+
|
|
323
|
+
server.listen(PORT, HOST, () => {
|
|
324
|
+
const info = JSON.stringify({
|
|
325
|
+
type: 'server-started', port: Number(PORT), host: HOST,
|
|
326
|
+
url_host: URL_HOST, url: 'http://' + URL_HOST + ':' + PORT,
|
|
327
|
+
screen_dir: SCREEN_DIR
|
|
328
|
+
});
|
|
329
|
+
console.log(info);
|
|
330
|
+
fs.writeFileSync(path.join(SCREEN_DIR, '.server-info'), info + '\n');
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (require.main === module) {
|
|
335
|
+
startServer();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
module.exports = { computeAcceptKey, encodeFrame, decodeFrame, OPCODES };
|