@flowe-ai/cli 0.1.8 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/assets/menubar-icon.png +0 -0
  2. package/assets/notification-icon.png +0 -0
  3. package/audio/body_scan_60.ellie.cues.json +21 -21
  4. package/audio/body_scan_60.ellie.mp3 +0 -0
  5. package/audio/box_breath_45.ellie.cues.json +25 -25
  6. package/audio/box_breath_45.ellie.mp3 +0 -0
  7. package/audio/box_breath_60.ellie.cues.json +33 -33
  8. package/audio/box_breath_60.ellie.mp3 +0 -0
  9. package/audio/extended_box_90.ellie.cues.json +49 -49
  10. package/audio/extended_box_90.ellie.mp3 +0 -0
  11. package/audio/eye_rotation_30.ellie.cues.json +11 -11
  12. package/audio/eye_rotation_30.ellie.mp3 +0 -0
  13. package/audio/full_sensory_90.ellie.cues.json +17 -17
  14. package/audio/full_sensory_90.ellie.mp3 +0 -0
  15. package/audio/grounded_feet_45.ellie.cues.json +11 -11
  16. package/audio/grounded_feet_45.ellie.mp3 +0 -0
  17. package/audio/neck_rotation_45.ellie.cues.json +15 -15
  18. package/audio/neck_rotation_45.ellie.mp3 +0 -0
  19. package/audio/palming_60.ellie.cues.json +17 -17
  20. package/audio/palming_60.ellie.mp3 +0 -0
  21. package/audio/physiological_sigh_30.ellie.cues.json +13 -13
  22. package/audio/physiological_sigh_30.ellie.mp3 +0 -0
  23. package/audio/progressive_palming_90.ellie.cues.json +19 -19
  24. package/audio/progressive_palming_90.ellie.mp3 +0 -0
  25. package/audio/quick_palming_30.ellie.cues.json +11 -11
  26. package/audio/quick_palming_30.ellie.mp3 +0 -0
  27. package/bin/flowe.js +0 -0
  28. package/bin/menubar/flowe.1s.sh +96 -0
  29. package/dist/ambient-SIMIQWQM.js +1 -0
  30. package/dist/chunk-AX3TLBEV.js +2 -0
  31. package/dist/{chunk-CGFAXS4S.js → chunk-C6NO4B4N.js} +2 -2
  32. package/dist/cli.js +62 -23
  33. package/dist/mcp-server.js +1 -1
  34. package/package.json +6 -2
  35. package/skills/flowe/SKILL.md +69 -6
@@ -1,41 +1,41 @@
1
1
  {
2
2
  "protocol_id": "eye_rotation_30",
3
3
  "voice": "ellie",
4
- "duration_ms": 18640,
4
+ "duration_ms": 19080,
5
5
  "cues": [
6
6
  {
7
7
  "label": "clockwise",
8
8
  "mode": "rotation",
9
- "start_ms": 1640,
10
- "end_ms": 7420,
9
+ "start_ms": 1780,
10
+ "end_ms": 7640,
11
11
  "round": 1
12
12
  },
13
13
  {
14
14
  "label": "clockwise",
15
15
  "mode": "rotation",
16
- "start_ms": 7420,
17
- "end_ms": 9600,
16
+ "start_ms": 7640,
17
+ "end_ms": 9540,
18
18
  "round": 2
19
19
  },
20
20
  {
21
21
  "label": "counter clockwise",
22
22
  "mode": "rotation",
23
- "start_ms": 9600,
24
- "end_ms": 13160,
23
+ "start_ms": 9540,
24
+ "end_ms": 12800,
25
25
  "round": 1
26
26
  },
27
27
  {
28
28
  "label": "counter clockwise",
29
29
  "mode": "rotation",
30
- "start_ms": 13160,
31
- "end_ms": 14000,
30
+ "start_ms": 12800,
31
+ "end_ms": 14180,
32
32
  "round": 2
33
33
  },
34
34
  {
35
35
  "label": "soft focus",
36
36
  "mode": "rotation",
37
- "start_ms": 14000,
38
- "end_ms": 17960
37
+ "start_ms": 14180,
38
+ "end_ms": 18400
39
39
  }
40
40
  ]
41
41
  }
Binary file
@@ -1,60 +1,60 @@
1
1
  {
2
2
  "protocol_id": "full_sensory_90",
3
3
  "voice": "ellie",
4
- "duration_ms": 43460,
4
+ "duration_ms": 45000,
5
5
  "cues": [
6
6
  {
7
7
  "label": "5 see",
8
8
  "mode": "grounding",
9
- "start_ms": 4280,
10
- "end_ms": 13540,
9
+ "start_ms": 4520,
10
+ "end_ms": 14080,
11
11
  "round": 5
12
12
  },
13
13
  {
14
14
  "label": "4 hear",
15
15
  "mode": "grounding",
16
- "start_ms": 13540,
17
- "end_ms": 20500,
16
+ "start_ms": 14080,
17
+ "end_ms": 21040,
18
18
  "round": 4
19
19
  },
20
20
  {
21
21
  "label": "3 touch",
22
22
  "mode": "grounding",
23
- "start_ms": 20500,
24
- "end_ms": 26520,
23
+ "start_ms": 21040,
24
+ "end_ms": 28240,
25
25
  "round": 3
26
26
  },
27
27
  {
28
28
  "label": "2 smell",
29
29
  "mode": "grounding",
30
- "start_ms": 26520,
31
- "end_ms": 33800,
30
+ "start_ms": 28240,
31
+ "end_ms": 35800,
32
32
  "round": 2
33
33
  },
34
34
  {
35
35
  "label": "1 taste",
36
36
  "mode": "grounding",
37
- "start_ms": 33800,
38
- "end_ms": 38140,
37
+ "start_ms": 35800,
38
+ "end_ms": 39440,
39
39
  "round": 1
40
40
  },
41
41
  {
42
42
  "label": "breath one",
43
43
  "mode": "grounding",
44
- "start_ms": 38140,
45
- "end_ms": 39680
44
+ "start_ms": 39440,
45
+ "end_ms": 40900
46
46
  },
47
47
  {
48
48
  "label": "breath two",
49
49
  "mode": "grounding",
50
- "start_ms": 39680,
51
- "end_ms": 40700
50
+ "start_ms": 40900,
51
+ "end_ms": 41180
52
52
  },
53
53
  {
54
54
  "label": "breath three",
55
55
  "mode": "grounding",
56
- "start_ms": 40700,
57
- "end_ms": 41580
56
+ "start_ms": 41180,
57
+ "end_ms": 42040
58
58
  }
59
59
  ]
60
60
  }
Binary file
@@ -1,39 +1,39 @@
1
1
  {
2
2
  "protocol_id": "grounded_feet_45",
3
3
  "voice": "ellie",
4
- "duration_ms": 22840,
4
+ "duration_ms": 23380,
5
5
  "cues": [
6
6
  {
7
7
  "label": "feet flat",
8
8
  "mode": "grounding",
9
- "start_ms": 2600,
10
- "end_ms": 7660
9
+ "start_ms": 2400,
10
+ "end_ms": 8020
11
11
  },
12
12
  {
13
13
  "label": "feel the weight",
14
14
  "mode": "grounding",
15
- "start_ms": 7660,
16
- "end_ms": 10720
15
+ "start_ms": 8020,
16
+ "end_ms": 11920
17
17
  },
18
18
  {
19
19
  "label": "breath one",
20
20
  "mode": "grounding",
21
- "start_ms": 10720,
22
- "end_ms": 16160,
21
+ "start_ms": 11920,
22
+ "end_ms": 16440,
23
23
  "round": 1
24
24
  },
25
25
  {
26
26
  "label": "breath two",
27
27
  "mode": "grounding",
28
- "start_ms": 16160,
29
- "end_ms": 17520,
28
+ "start_ms": 16440,
29
+ "end_ms": 18340,
30
30
  "round": 2
31
31
  },
32
32
  {
33
33
  "label": "breath three",
34
34
  "mode": "grounding",
35
- "start_ms": 17520,
36
- "end_ms": 19360,
35
+ "start_ms": 18340,
36
+ "end_ms": 19980,
37
37
  "round": 3
38
38
  }
39
39
  ]
Binary file
@@ -1,55 +1,55 @@
1
1
  {
2
2
  "protocol_id": "neck_rotation_45",
3
3
  "voice": "ellie",
4
- "duration_ms": 25160,
4
+ "duration_ms": 23240,
5
5
  "cues": [
6
6
  {
7
7
  "label": "shoulder roll back",
8
8
  "mode": "rotation",
9
- "start_ms": 3520,
10
- "end_ms": 6460,
9
+ "start_ms": 3420,
10
+ "end_ms": 6060,
11
11
  "round": 1
12
12
  },
13
13
  {
14
14
  "label": "shoulder roll back",
15
15
  "mode": "rotation",
16
- "start_ms": 6460,
17
- "end_ms": 9180,
16
+ "start_ms": 6060,
17
+ "end_ms": 7980,
18
18
  "round": 2
19
19
  },
20
20
  {
21
21
  "label": "shoulder roll forward",
22
22
  "mode": "rotation",
23
- "start_ms": 9180,
24
- "end_ms": 11140,
23
+ "start_ms": 7980,
24
+ "end_ms": 9920,
25
25
  "round": 1
26
26
  },
27
27
  {
28
28
  "label": "shoulder roll forward",
29
29
  "mode": "rotation",
30
- "start_ms": 11140,
31
- "end_ms": 11680,
30
+ "start_ms": 9920,
31
+ "end_ms": 10440,
32
32
  "round": 2
33
33
  },
34
34
  {
35
35
  "label": "neck roll right",
36
36
  "mode": "rotation",
37
- "start_ms": 11680,
38
- "end_ms": 18340,
37
+ "start_ms": 10440,
38
+ "end_ms": 17300,
39
39
  "round": 1
40
40
  },
41
41
  {
42
42
  "label": "neck roll left",
43
43
  "mode": "rotation",
44
- "start_ms": 18340,
45
- "end_ms": 21580,
44
+ "start_ms": 17300,
45
+ "end_ms": 19540,
46
46
  "round": 1
47
47
  },
48
48
  {
49
49
  "label": "lift and breathe",
50
50
  "mode": "rotation",
51
- "start_ms": 21580,
52
- "end_ms": 25160
51
+ "start_ms": 19540,
52
+ "end_ms": 23240
53
53
  }
54
54
  ]
55
55
  }
Binary file
@@ -1,60 +1,60 @@
1
1
  {
2
2
  "protocol_id": "palming_60",
3
3
  "voice": "ellie",
4
- "duration_ms": 29260,
4
+ "duration_ms": 29600,
5
5
  "cues": [
6
6
  {
7
7
  "label": "warm palms",
8
8
  "mode": "palming",
9
- "start_ms": 1440,
10
- "end_ms": 5460
9
+ "start_ms": 1960,
10
+ "end_ms": 6380
11
11
  },
12
12
  {
13
13
  "label": "cup over eyes",
14
14
  "mode": "palming",
15
- "start_ms": 5460,
16
- "end_ms": 12940
15
+ "start_ms": 6380,
16
+ "end_ms": 13420
17
17
  },
18
18
  {
19
19
  "label": "breath one",
20
20
  "mode": "palming",
21
- "start_ms": 12940,
22
- "end_ms": 14780,
21
+ "start_ms": 13420,
22
+ "end_ms": 15460,
23
23
  "round": 1
24
24
  },
25
25
  {
26
26
  "label": "breath two",
27
27
  "mode": "palming",
28
- "start_ms": 14780,
29
- "end_ms": 16720,
28
+ "start_ms": 15460,
29
+ "end_ms": 17940,
30
30
  "round": 2
31
31
  },
32
32
  {
33
33
  "label": "breath three",
34
34
  "mode": "palming",
35
- "start_ms": 16720,
36
- "end_ms": 18580,
35
+ "start_ms": 17940,
36
+ "end_ms": 19560,
37
37
  "round": 3
38
38
  },
39
39
  {
40
40
  "label": "breath four",
41
41
  "mode": "palming",
42
- "start_ms": 18580,
43
- "end_ms": 20280,
42
+ "start_ms": 19560,
43
+ "end_ms": 21160,
44
44
  "round": 4
45
45
  },
46
46
  {
47
47
  "label": "breath five",
48
48
  "mode": "palming",
49
- "start_ms": 20280,
50
- "end_ms": 22180,
49
+ "start_ms": 21160,
50
+ "end_ms": 23220,
51
51
  "round": 5
52
52
  },
53
53
  {
54
54
  "label": "breath six",
55
55
  "mode": "palming",
56
- "start_ms": 22180,
57
- "end_ms": 24080,
56
+ "start_ms": 23220,
57
+ "end_ms": 24880,
58
58
  "round": 6
59
59
  }
60
60
  ]
Binary file
@@ -1,53 +1,53 @@
1
1
  {
2
2
  "protocol_id": "physiological_sigh_30",
3
3
  "voice": "ellie",
4
- "duration_ms": 23080,
4
+ "duration_ms": 22080,
5
5
  "cues": [
6
6
  {
7
7
  "label": "double inhale",
8
8
  "mode": "breath",
9
- "start_ms": 3520,
10
- "end_ms": 7180,
9
+ "start_ms": 3620,
10
+ "end_ms": 7400,
11
11
  "phase": "inhale",
12
12
  "round": 1
13
13
  },
14
14
  {
15
15
  "label": "long exhale",
16
16
  "mode": "breath",
17
- "start_ms": 7180,
18
- "end_ms": 9640,
17
+ "start_ms": 7400,
18
+ "end_ms": 9820,
19
19
  "phase": "exhale",
20
20
  "round": 1
21
21
  },
22
22
  {
23
23
  "label": "double inhale",
24
24
  "mode": "breath",
25
- "start_ms": 9640,
26
- "end_ms": 14940,
25
+ "start_ms": 9820,
26
+ "end_ms": 14400,
27
27
  "phase": "inhale",
28
28
  "round": 2
29
29
  },
30
30
  {
31
31
  "label": "long exhale",
32
32
  "mode": "breath",
33
- "start_ms": 14940,
34
- "end_ms": 16720,
33
+ "start_ms": 14400,
34
+ "end_ms": 16060,
35
35
  "phase": "exhale",
36
36
  "round": 2
37
37
  },
38
38
  {
39
39
  "label": "double inhale",
40
40
  "mode": "breath",
41
- "start_ms": 16720,
42
- "end_ms": 19560,
41
+ "start_ms": 16060,
42
+ "end_ms": 18720,
43
43
  "phase": "inhale",
44
44
  "round": 3
45
45
  },
46
46
  {
47
47
  "label": "long exhale",
48
48
  "mode": "breath",
49
- "start_ms": 19560,
50
- "end_ms": 22440,
49
+ "start_ms": 18720,
50
+ "end_ms": 21360,
51
51
  "phase": "exhale",
52
52
  "round": 3
53
53
  }
@@ -1,61 +1,61 @@
1
1
  {
2
2
  "protocol_id": "progressive_palming_90",
3
3
  "voice": "ellie",
4
- "duration_ms": 36500,
4
+ "duration_ms": 36900,
5
5
  "cues": [
6
6
  {
7
7
  "label": "warm palms",
8
8
  "mode": "palming",
9
- "start_ms": 3240,
10
- "end_ms": 7120
9
+ "start_ms": 3080,
10
+ "end_ms": 6700
11
11
  },
12
12
  {
13
13
  "label": "cup over eyes",
14
14
  "mode": "palming",
15
- "start_ms": 7120,
16
- "end_ms": 10940
15
+ "start_ms": 6700,
16
+ "end_ms": 10680
17
17
  },
18
18
  {
19
19
  "label": "eye rest",
20
20
  "mode": "palming",
21
- "start_ms": 10940,
22
- "end_ms": 16220
21
+ "start_ms": 10680,
22
+ "end_ms": 15580
23
23
  },
24
24
  {
25
25
  "label": "slow breaths",
26
26
  "mode": "palming",
27
- "start_ms": 16220,
28
- "end_ms": 18500
27
+ "start_ms": 15580,
28
+ "end_ms": 18980
29
29
  },
30
30
  {
31
31
  "label": "soften forehead",
32
32
  "mode": "palming",
33
- "start_ms": 18500,
34
- "end_ms": 19600
33
+ "start_ms": 18980,
34
+ "end_ms": 19980
35
35
  },
36
36
  {
37
37
  "label": "soften jaw",
38
38
  "mode": "palming",
39
- "start_ms": 19600,
40
- "end_ms": 21240
39
+ "start_ms": 19980,
40
+ "end_ms": 21680
41
41
  },
42
42
  {
43
43
  "label": "drop shoulders",
44
44
  "mode": "palming",
45
- "start_ms": 21240,
46
- "end_ms": 24860
45
+ "start_ms": 21680,
46
+ "end_ms": 25240
47
47
  },
48
48
  {
49
49
  "label": "slow breath",
50
50
  "mode": "palming",
51
- "start_ms": 24860,
52
- "end_ms": 26580
51
+ "start_ms": 25240,
52
+ "end_ms": 26840
53
53
  },
54
54
  {
55
55
  "label": "eyes warm dark",
56
56
  "mode": "palming",
57
- "start_ms": 26580,
58
- "end_ms": 31480
57
+ "start_ms": 26840,
58
+ "end_ms": 31800
59
59
  }
60
60
  ]
61
61
  }
@@ -1,39 +1,39 @@
1
1
  {
2
2
  "protocol_id": "quick_palming_30",
3
3
  "voice": "ellie",
4
- "duration_ms": 17700,
4
+ "duration_ms": 19000,
5
5
  "cues": [
6
6
  {
7
7
  "label": "warm palms",
8
8
  "mode": "palming",
9
- "start_ms": 1480,
10
- "end_ms": 4260
9
+ "start_ms": 1540,
10
+ "end_ms": 4320
11
11
  },
12
12
  {
13
13
  "label": "cup over eyes",
14
14
  "mode": "palming",
15
- "start_ms": 4260,
16
- "end_ms": 9100
15
+ "start_ms": 4320,
16
+ "end_ms": 9800
17
17
  },
18
18
  {
19
19
  "label": "breath one",
20
20
  "mode": "palming",
21
- "start_ms": 9100,
22
- "end_ms": 11060,
21
+ "start_ms": 9800,
22
+ "end_ms": 11880,
23
23
  "round": 1
24
24
  },
25
25
  {
26
26
  "label": "breath two",
27
27
  "mode": "palming",
28
- "start_ms": 11060,
29
- "end_ms": 13040,
28
+ "start_ms": 11880,
29
+ "end_ms": 13680,
30
30
  "round": 2
31
31
  },
32
32
  {
33
33
  "label": "breath three",
34
34
  "mode": "palming",
35
- "start_ms": 13040,
36
- "end_ms": 15320,
35
+ "start_ms": 13680,
36
+ "end_ms": 15880,
37
37
  "round": 3
38
38
  }
39
39
  ]
Binary file
package/bin/flowe.js CHANGED
File without changes
@@ -0,0 +1,96 @@
1
+ #!/usr/bin/env bash
2
+ # flowe SwiftBar plugin — refresh every 1s (filename suffix .1s.sh).
3
+ #
4
+ # Reads the active focus session's state.json and renders a menubar
5
+ # title plus a dropdown of controls (pause / resume / skip / quit).
6
+ # When no daemon is running, shows "Start pomodoro / dev / ultradian"
7
+ # entries that open Terminal so the picker chain can run.
8
+ #
9
+ # Auto-installed via `flowe install-menubar`, which writes this file
10
+ # into SwiftBar's plugin dir with NODE_BIN + FLOWE_BIN hardcoded to
11
+ # absolute paths. SwiftBar runs under a minimal launchd PATH, so a
12
+ # `#!/usr/bin/env node` shebang resolves to whatever launchd happens
13
+ # to find — often nothing, or an old node. Baking the absolute paths
14
+ # bypasses that entirely.
15
+ #
16
+ # Manual install: copy this file into SwiftBar's plugin dir and either
17
+ # set NODE_BIN + FLOWE_BIN below, or rely on `command -v` resolution.
18
+ #
19
+ # <bitbar.title>flowe focus</bitbar.title>
20
+ # <bitbar.author>flowe.ai</bitbar.author>
21
+ # <bitbar.desc>Active focus cycle status + controls</bitbar.desc>
22
+ # <bitbar.dependencies>flowe</bitbar.dependencies>
23
+ # <swiftbar.hideAbout>true</swiftbar.hideAbout>
24
+ # <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
25
+ # <swiftbar.hideLastUpdated>true</swiftbar.hideLastUpdated>
26
+ # <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
27
+ # <swiftbar.hideSwiftBar>true</swiftbar.hideSwiftBar>
28
+
29
+ set -e
30
+
31
+ # install-menubar replaces these lines with hardcoded absolute paths.
32
+ # When run manually, fall back to PATH resolution.
33
+ NODE_BIN="${NODE_BIN:-}"
34
+ FLOWE_BIN="${FLOWE_BIN:-}"
35
+
36
+ if [ -z "$FLOWE_BIN" ]; then
37
+ if command -v flowe >/dev/null 2>&1; then
38
+ FLOWE_BIN="$(command -v flowe)"
39
+ else
40
+ # Common Homebrew + npm-global fallbacks. SwiftBar inherits a
41
+ # minimal launchd env, so PATH may not include these by default.
42
+ for candidate in \
43
+ /opt/homebrew/bin/flowe \
44
+ /usr/local/bin/flowe \
45
+ "$HOME/.npm-global/bin/flowe" \
46
+ "$HOME/.bun/bin/flowe"
47
+ do
48
+ if [ -x "$candidate" ]; then
49
+ FLOWE_BIN="$candidate"
50
+ break
51
+ fi
52
+ done
53
+ fi
54
+ fi
55
+
56
+ if [ -z "$FLOWE_BIN" ] || [ ! -x "$FLOWE_BIN" ]; then
57
+ echo "flowe ⚠"
58
+ echo "---"
59
+ echo "could not locate the flowe binary"
60
+ echo "---"
61
+ echo "Run \`flowe install-menubar\` from a shell that has flowe on PATH."
62
+ exit 0
63
+ fi
64
+
65
+ if [ -z "$NODE_BIN" ]; then
66
+ if command -v node >/dev/null 2>&1; then
67
+ NODE_BIN="$(command -v node)"
68
+ else
69
+ for candidate in \
70
+ /opt/homebrew/bin/node \
71
+ /usr/local/bin/node \
72
+ "$HOME/.nvm/versions/node/$(ls -1 "$HOME/.nvm/versions/node" 2>/dev/null | tail -1)/bin/node"
73
+ do
74
+ if [ -x "$candidate" ]; then
75
+ NODE_BIN="$candidate"
76
+ break
77
+ fi
78
+ done
79
+ fi
80
+ fi
81
+
82
+ # When we have an absolute node, invoke it directly so we bypass the
83
+ # `#!/usr/bin/env node` shebang inside flowe.js. Without this, SwiftBar's
84
+ # launchd PATH may resolve to an older / missing node and crash on bare
85
+ # `import "stream"` style imports inside the bundled CLI.
86
+ #
87
+ # We also pass --node-bin so the menubar formatter can emit `bash=$NODE_BIN`
88
+ # for every dropdown action (Pause/Skip/Quit/etc). Without that, clicking
89
+ # an action would re-trigger the broken shebang resolution.
90
+ if [ -n "$NODE_BIN" ] && [ -x "$NODE_BIN" ]; then
91
+ exec "$NODE_BIN" "$FLOWE_BIN" focus menubar \
92
+ --flowe-bin "$FLOWE_BIN" \
93
+ --node-bin "$NODE_BIN"
94
+ fi
95
+
96
+ exec "$FLOWE_BIN" focus menubar --flowe-bin "$FLOWE_BIN"
@@ -0,0 +1 @@
1
+ import{a,b,c,d,e,f,g,h,i,j,k,l,m}from"./chunk-AX3TLBEV.js";export{a as DEFAULT_MANIFEST_URL,c as applyAmbientAssetBaseUrl,l as buildAmbientEntries,d as cachePathFor,b as defaultManifestUrl,j as downloadAmbient,i as fetchAmbientManifest,h as isCached,e as loadCustomAmbientManifest,f as mergeAmbientManifests,g as saveCustomAmbientSound,m as selectableIndices,k as startAmbientFile};
@@ -0,0 +1,2 @@
1
+ import{spawn as y}from"child_process";import{createHash as A}from"crypto";import{createWriteStream as v,existsSync as h,mkdirSync as T,readFileSync as E,statSync as k,unlinkSync as M,writeFileSync as P}from"fs";import{homedir as C,platform as L}from"os";import{dirname as O,join as g}from"path";import{Readable as I}from"stream";import{pipeline as R}from"stream/promises";var F="https://www.flowe.ai/cli/ambient/manifest.json",N=5e3,U=3e4,D=29e3,$=900;function B(){return process.env.FLOWE_AMBIENT_MANIFEST_URL||F}function j(){return process.env.FLOWE_AMBIENT_ASSET_BASE_URL||null}function J(t){try{return new URL(t).pathname.split("/").filter(Boolean).pop()||t}catch{return t.split("/").filter(Boolean).pop()||t}}function W(t,e=j()){if(!e)return t;let n=e.replace(/\/+$/,"");return{...t,sounds:t.sounds.map(o=>({...o,url:`${n}/${encodeURIComponent(J(o.url))}`}))}}function w(){let t=C(),e=L()==="win32"?g(t,"AppData","Roaming","flowe"):g(t,".config","flowe");return g(e,"cache","ambient")}function x(){return g(w(),"custom-manifest.json")}function S(t){return g(w(),`${t.sha256}.mp3`)}function H(){let t=x();if(!h(t))return null;try{let e=JSON.parse(E(t,"utf-8"));return e.version!==1||!Array.isArray(e.categories)||!Array.isArray(e.sounds)?null:e}catch{return null}}function nt(t,e){if(!t&&!e)return null;if(!t)return e;if(!e||e.sounds.length===0)return t;let n=new Set(t.categories.map(r=>r.id)),o=[...t.categories];for(let r of e.categories)n.has(r.id)||(n.add(r.id),o.push(r));let i=new Set(t.sounds.map(r=>r.id)),s=[...t.sounds];for(let r of e.sounds)i.has(r.id)||(i.add(r.id),s.push(r));return{version:1,generated_at:t.generated_at,categories:o,sounds:s}}function rt(t){let e=Buffer.from(t.bytes),n=A("sha256").update(e).digest("hex"),o=`custom_${n.slice(0,12)}`,i=(t.name.trim()||"custom ambience").slice(0,48),s={id:o,category:"custom",name:i,blurb:"generated ambience",url:`flowe-local://ambient/custom/${o}.mp3`,sha256:n,bytes:e.byteLength};T(w(),{recursive:!0}),P(S(s),e);let r=H(),c=[s,...r?.sounds.filter(m=>m.id!==o)??[]],d={version:1,generated_at:(t.now??(()=>new Date))().toISOString(),categories:[{id:"custom",label:"custom"}],sounds:c};return P(x(),JSON.stringify(d,null,2)+`
2
+ `,"utf-8"),s}function q(t){let e=S(t);if(!h(e))return!1;try{let n=E(e);return A("sha256").update(n).digest("hex")===t.sha256}catch{return!1}}async function ot(t){let e=t?.url??B(),n=t?.timeoutMs??N,o=new AbortController,i=setTimeout(()=>o.abort(),n);t?.signal&&(t.signal.aborted?o.abort():t.signal.addEventListener("abort",()=>o.abort()));try{let s=await fetch(e,{signal:o.signal});if(!s.ok)throw new Error(`manifest fetch failed: ${s.status}`);let r=await s.json();if(!r.sounds||!r.categories)throw new Error("manifest shape invalid");return W(r)}finally{clearTimeout(i)}}async function it(t,e){let n=S(t);if(q(t))return e?.onProgress?.({downloaded:t.bytes,total:t.bytes}),n;T(O(n),{recursive:!0});let o=e?.timeoutMs??U,i=new AbortController,s=setTimeout(()=>i.abort(),o);e?.signal&&(e.signal.aborted?i.abort():e.signal.addEventListener("abort",()=>i.abort()));let r=`${n}.part`;try{let c=await fetch(t.url,{signal:i.signal});if(!c.ok||!c.body)throw new Error(`download failed: ${c.status}`);let d=t.bytes,m=0,f=A("sha256"),p=c.body.getReader(),b=new I({read(){p.read().then(({value:u,done:_})=>{if(_){this.push(null);return}m+=u.byteLength,f.update(u),e?.onProgress?.({downloaded:m,total:d}),this.push(Buffer.from(u))}).catch(u=>this.destroy(u))}});if(await R(b,v(r)),f.digest("hex")!==t.sha256){try{M(r)}catch{}throw new Error("sha256 mismatch")}let{renameSync:l}=await import("fs");return l(r,n),n}catch(c){try{h(r)&&M(r)}catch{}throw c}finally{clearTimeout(s)}}function z(t){return process.platform==="darwin"?y("afplay",[t],{stdio:"ignore"}):process.platform==="win32"?y("powershell",["-NoProfile","-Command",`(New-Object Media.SoundPlayer '${t}').PlaySync()`],{stdio:"ignore"}):y("mpg123",["-q",t],{stdio:"ignore"})}function st(t,e={}){if(!t||!h(t))return{stop:()=>{}};try{if(k(t).size<=0)return{stop:()=>{}}}catch{return{stop:()=>{}}}let n=!1,o=null,i=null,s=new Set,r=new Set,c=e.loopRestartMs??D,d=e.overlapMs??$,m=e.spawnPlayer??z,f=()=>{i&&(clearTimeout(i),i=null);for(let a of s)clearTimeout(a);s.clear()},p=a=>{if(a&&!a.killed)try{a.kill("SIGTERM")}catch{}},b=()=>{if(n)return;let a=o,l;try{l=m(t)}catch{n=!0;return}if(o=l,r.add(l),a&&a!==l){let u=setTimeout(()=>{s.delete(u),p(a)},d);s.add(u)}i=setTimeout(()=>{i=null,b()},c),l.on("exit",()=>{r.delete(l),!n&&o===l&&(i&&(clearTimeout(i),i=null),b())}),l.on("error",()=>{n=!0,f()})};return b(),{stop:()=>{n=!0,f(),o=null;for(let a of r)p(a);r.clear()}}}function at(t){let e=[{kind:"none"}];if(!t)return e;for(let n of t.categories){let o=t.sounds.filter(i=>i.category===n.id);if(o.length!==0){e.push({kind:"category",categoryId:n.id,label:n.label});for(let i of o)e.push({kind:"sound",sound:i})}}return e}function ct(t){let e=[];for(let n=0;n<t.length;n++)t[n].kind!=="category"&&e.push(n);return e}export{F as a,B as b,W as c,S as d,H as e,nt as f,rt as g,q as h,ot as i,it as j,st as k,at as l,ct as m};