11import fs from "fs" ;
22import path from "path" ;
3+ import readline from "readline/promises" ;
34import { Authzor } from "./authz" ;
45import chalk from "chalk" ;
56import dotenv from "dotenv" ;
67import * as Filter from "./trackFilter" ;
78import {
89 createLanguageModel ,
910 createProgramTranslator ,
10- processRequests ,
1111 Program ,
1212 createModuleTextFromProgram ,
1313 evaluateJsonProgram ,
@@ -39,6 +39,7 @@ import {
3939 shuffle ,
4040 getAlbumTracks ,
4141 getQueue ,
42+ getRecent ,
4243} from "./endpoints" ;
4344import { listAvailableDevices , printStatus , selectDevice } from "./playback" ;
4445import { SpotifyService , User } from "./service" ;
@@ -75,7 +76,7 @@ async function printTrackNames(
7576 let count = 1 ;
7677 for ( const track of tracks ) {
7778 let prefix = "" ;
78- if ( context && ( tracks . length > 1 ) ) {
79+ if ( context && tracks . length > 1 ) {
7980 prefix = `T${ count } : ` ;
8081 }
8182 console . log ( chalk . cyanBright ( `${ prefix } ${ track . name } ` ) ) ;
@@ -236,14 +237,20 @@ async function handleCall(
236237 const currentQueue = await getQueue ( clientContext . service ) ;
237238 if ( currentQueue ) {
238239 // not yet supporting episidoes
239- const filtered = currentQueue . queue . filter ( ( item ) => item . type === "track" ) as SpotifyApi . TrackObjectFull [ ] ;
240+ const filtered = currentQueue . queue . filter (
241+ ( item ) => item . type === "track"
242+ ) as SpotifyApi . TrackObjectFull [ ] ;
240243 console . log ( chalk . magentaBright ( "Current Queue:" ) ) ;
241244 console . log (
242- chalk . cyanBright ( `--------------------------------------------` )
245+ chalk . cyanBright (
246+ `--------------------------------------------`
247+ )
243248 ) ;
244249 await printTrackNames ( filtered , clientContext ) ;
245250 console . log (
246- chalk . cyanBright ( `--------------------------------------------` )
251+ chalk . cyanBright (
252+ `--------------------------------------------`
253+ )
247254 ) ;
248255 await printStatus ( clientContext ) ;
249256 }
@@ -311,7 +318,10 @@ async function handleCall(
311318 break ;
312319 }
313320 case "setVolume" : {
314- const newVolumeLevel = args [ 0 ] as number ;
321+ let newVolumeLevel = args [ 0 ] as number ;
322+ if ( newVolumeLevel > 50 ) {
323+ newVolumeLevel = 50 ;
324+ }
315325 console . log (
316326 chalk . yellowBright ( `setting volume to ${ newVolumeLevel } ...` )
317327 ) ;
@@ -326,8 +336,8 @@ async function handleCall(
326336 let nv = Math . floor (
327337 ( 1.0 + volumeChangeAmount / 100.0 ) * volpct
328338 ) ;
329- if ( nv > 100 ) {
330- nv = 100 ;
339+ if ( nv > 50 ) {
340+ nv = 50 ;
331341 }
332342 console . log ( chalk . yellowBright ( `setting volume to ${ nv } ...` ) ) ;
333343 await setVolume ( clientContext . service , nv ) ;
@@ -495,10 +505,7 @@ async function handleCall(
495505 const playlistCollection = args [ 0 ] as PlaylistTrackCollection ;
496506 if ( playlistCollection ) {
497507 const playlist = playlistCollection . getPlaylist ( ) ;
498- await deletePlaylist (
499- clientContext . service ,
500- playlist . id
501- ) ;
508+ await deletePlaylist ( clientContext . service , playlist . id ) ;
502509 console . log (
503510 chalk . magentaBright ( `playlist ${ playlist . name } deleted` )
504511 ) ;
@@ -527,7 +534,34 @@ async function handleCall(
527534// set this to false to just look at llm generation without Spotify connection
528535const spotifyConnect = true ;
529536
530- // Process requests interactively or from the input file specified on the command line
537+ export async function index ( context : IClientContext ) {
538+ let playHistory = await getRecent (
539+ context . service ,
540+ Date . parse ( "2018-01-01T00:00:00.00Z" )
541+ ) ;
542+ if ( playHistory ) {
543+ console . log ( playHistory ?. length ) ;
544+ let trackNames = '' ;
545+ playHistory . map ( ( item ) => {
546+ trackNames += item . track . name + '\n' ;
547+ } ) ;
548+ fs . writeFileSync ( "bigFetch.txt" , trackNames ) ;
549+ }
550+ }
551+
552+ function checkAck ( input : string , program : Program ) : Program | undefined {
553+ const linput = input . toLocaleLowerCase ( ) ;
554+ if ( [ "y" , "yes" , "ok" ] . includes ( linput ) ) {
555+ return program ;
556+ } else {
557+ return undefined ;
558+ }
559+ }
560+
561+ // whether to confirm each action with the user
562+ const confirmMode = true ;
563+
564+ // Process requests interactively (no batch mode for now)
531565async function musicApp ( ) {
532566 const authz = new Authzor ( ) ;
533567 authz . authorize ( spotifyConnect , async ( token ) => {
@@ -541,7 +575,15 @@ async function musicApp() {
541575 )
542576 ) ;
543577 }
544- processRequests ( "🎵> " , process . argv [ 2 ] , async ( request ) => {
578+ const musicPrompt = "🎵> " ;
579+ const confirmPrompt = "👍👎 (answer y/n)> " ;
580+ const stdio = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
581+ while ( true ) {
582+ const request = await stdio . question ( musicPrompt ) ;
583+ if ( request . toLowerCase ( ) === "quit" || request . toLowerCase ( ) === "exit" ) {
584+ stdio . close ( ) ;
585+ return ;
586+ }
545587 const localResult = localParser ( request ) ;
546588 let program : Program | undefined = undefined ;
547589 if ( localResult ) {
@@ -550,13 +592,21 @@ async function musicApp() {
550592 const response = await translator . translate ( request ) ;
551593 if ( ! response . success ) {
552594 console . log ( response . message ) ;
553- return ;
595+ continue ;
554596 }
555597 program = response . data ;
556598 }
557599 if ( program !== undefined ) {
558600 chalkPlan ( program ) ;
559601 console . log ( getData ( createModuleTextFromProgram ( program ) ) ) ;
602+ if ( confirmMode && ( ! localResult ) ) {
603+ const input = await stdio . question ( confirmPrompt ) ;
604+ program = checkAck ( input , program ) ;
605+ if ( program === undefined ) {
606+ console . log ( "Thanks for the feedback. Canceling execution..." )
607+ continue ;
608+ }
609+ }
560610 if ( context !== undefined ) {
561611 const result = await evaluateJsonProgram (
562612 program ,
@@ -576,7 +626,7 @@ async function musicApp() {
576626 }
577627 }
578628 }
579- } ) ;
629+ }
580630 } ) ;
581631}
582632
0 commit comments