Source: site.view [edit]
Function name: cCmd
Arguments: input
Description: Handle all the console commands
Page type: webl
Render function:  
Module: graffiti

Page source:

var saveData = fun(gData)

  var funcinfo = Wub_GetFunctionInfo("graffiti.gData");
  funcinfo.exec := ToString(gData);
  Wub_SaveFunctionInfo(funcinfo)

end;

var callLLM = fun(req)
      var llm = WubCall("llm", [req]);
      var res = Wub_ReplaceAll(llm.response ? "", `\n`, "\n");
      res = Wub_ReplaceAll(res, `\"`, `"`);
      var obj = WubCall("jsonToWebL", [res]) ? res;
      return obj;
end;


var suggestItinerary = fun(gData)
      var itinerary = [. .];
      var where = gData.query.where;
      var duration = gData.query.duration;
      var req;
      var res;
      if where == "" then
         where = gData.profile.livesIn;
      end;
      if duration == "" then
         duration = 7
      end;
      itinerary["duration"] := duration;

      var purpose = gData.query.purpose;
      if purpose == "" then
         purpose = Str_Trim(gData.query.when + " " + where)
      end;

      var title = callLLM("make a travel concept title for this goal: " + purpose);
      itinerary["title"] := title.title ? title;

      var geo = WubCall("googlePlace", [where]);

      if geo.type == "locality" then
         
         itinerary["locations"] := [[. city= where, duration=duration .]];

      else
         req = "Suggest three pairs of recommended cities and titles of travel concepts to that city that fits this purpose " + gData.query.purpose
            + ` Use the format { "recommendations": [ {"city": <first city>", "title": <first title>}, {"city": <second city>, "title": <second title> }, {...} ] }`;
         res = callLLM(req);
         itinerary.locations := res

      end;

      gData.itineraries := gData.itineraries + [itinerary];

      saveData(gData);

      return ToString(itinerary);
end;


var purpose = fun(gData, args)
   if args == "" then
      gData.query.purpose := "";
      gData.query.where := "";
      gData.query.when := "";
      gData.query.duration := "";
      gData.query.who := "";
      saveData(gData);
      return "Query cleared."
   else
      var req = "Given the phrase '" + Str_Trim(args) + "' extract out 'where' 'when', 'duration' (in days) and 'who' arguments and return them as a JSON object. Use 'empty' if args not specified.";
      var obj = callLLM(req);
   
      if Objectp(obj) then
         gData.query.purpose := args;

         every fld in ["where", "when", "duration", "who"] do
            var val = obj[fld] ? "";
            gData.query[fld] := val;
         end;

         saveData(gData)
      end;
      return ToString(gData.query);
   end
end;


var handleCommand = fun(cmd, args, gData)

   if cmd == "help" then
      return `
   COMMANDS
   ========
   itineraries: lists current itineraries
   purpose <input>: parse purpose into possible where, when, who
   suggest-itinerary: suggest new itinerary given current context and profile
   profile: show user profile
   ========
`;

   elsif cmd == "purpose" then
      if Size(args) == 1 then
         return purpose(gData, Str_Trim(args[0]))
      else
         return ToString(gData.query);
      end

  elsif cmd member ["who", "where", "when", "context"] then
      if Size(args) == 1 then
         gData.query[cmd] := Str_Trim(args[0]);
         saveData(gData)
      end;
      return ToString(gData.query)

   elsif cmd == "suggest-itinerary" then
      return suggestItinerary(gData);

   elsif cmd == "itineraries" then
      return ToString(gData.itineraries);

   end;

  return "unknown command"

end;


var main = fun()       
              
   var spt = Str_Split(input, ":");
   var cmd = spt[0];
   var args = [];
   if Size(spt) > 1 then
      args = Str_Split(spt[1], ",")
   end;

   var gData = WubCall("graffiti.gData", []);

   handleCommand(cmd, args, gData);

end;
     
               
NewPage(main(), "text/html");