No tsNetDeleteSync function how to delete ? (Solved with shell)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

No tsNetDeleteSync function how to delete ? (Solved with shell)

Post by mrcoollion » Mon Jan 20, 2025 5:53 pm

Hello LC friends and specialists,

I have the following problem with tsNet function.
There does not seem to be a tsNetDeleteSync function :shock: .
I need to send a delete file message with tsNet.... to an url , how do i do this?
Below is the curl code.

Code: Select all

curl -X 'DELETE' \
  'http://localhost:3001/api/v1/system/remove-documents' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer *******-*******-*******-*******' \
  -H 'Content-Type: application/json' \
  -d '{
  "names": [
    "custom-documents/file.txt-fc4beeeb-e436-454d-8bb4-e5b8979cb48f.json"
  ]
}'
Last edited by mrcoollion on Wed Jan 22, 2025 9:22 am, edited 2 times in total.

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: No tsNetDeleteSync function how to delete ?

Post by mrcoollion » Tue Jan 21, 2025 11:40 am

No answers yet from any of the LiveCode experts ? Here is some additional information and a piece of script.
I get the following debug information, The server does not seem to be able to cope with the additional information I send in the tSettings variable. But tsNetCustomSync needs to have those setting in array format, also there is no other way as I see it to hand over the document name for removal
Below is the script an below that the debug information.

Code: Select all

 put "http://localhost:3001/api/v1/system/remove-documents" into tURLString
         ------------------------------------------------
         put tDocumentName into aSettings["names"]
         put "DELETE" into tRequest
         ------------------------------------------------
         -- Prepare headers, in Headers do not use crlf but use lf
         put "Content-Type: application/json" & lf & \  
               "accept: application/json" & lf & \
               "Authorization: Bearer " & tApiKey into tHeaders
         ------------------------------------------------------------------
         put tHeaders into tPrePostHeaders
         ------------------------------------------------------------------
         # Make the API call using TSNet
         put "" into  field "BotInfoWindow" // empty field first
         tsNetSetDebugCallback("updateDebugField")
         tsNetInit
         put tsNetCustomSync(tURLString, tRequest, tHeaders, tRecvHeaders, tResult, tBytes,aSettings) into tData
         // tsNetCustomSync(<pURL>, <pRequest>, <xHeaders>, <rOutHeaders>, <rResult>, <rBytes>, [<pSettings>])
         tsNetClose
The debug information from tsNetSetDebugCallback("updateDebugField"):
http://localhost:3001/api/v1/system/remove-documents: Trying ::1:3001...
http://localhost:3001/api/v1/system/remove-documents: Trying 127.0.0.1:3001...
http://localhost:3001/api/v1/system/remove-documents: Connected to localhost (127.0.0.1) port 3001 (#0)
http://localhost:3001/api/v1/system/remove-documents: DELETE /api/v1/system/remove-documents HTTP/1.1
Host: localhost:3001
Content-Type: application/json
accept: application/json
Authorization: Bearer NHNV7Q7-ERT4TRX-KEZEJ2S-TKEDR65

http://localhost:3001/api/v1/system/remove-documents: Mark bundle as not supporting multiuse
http://localhost:3001/api/v1/system/remove-documents: HTTP/1.1 500 Internal Server Error
http://localhost:3001/api/v1/system/remove-documents: X-Powered-By: Express
http://localhost:3001/api/v1/system/remove-documents: Vary: Origin
http://localhost:3001/api/v1/system/remove-documents: Content-Type: text/plain; charset=utf-8
http://localhost:3001/api/v1/system/remove-documents: Content-Length: 21
http://localhost:3001/api/v1/system/remove-documents: ETag: W/"15-/6VXivhc2MKdLfIkLcUE47K6aH0"
http://localhost:3001/api/v1/system/remove-documents: Date: Tue, 21 Jan 2025 10:10:38 GMT
http://localhost:3001/api/v1/system/remove-documents: Connection: keep-alive
http://localhost:3001/api/v1/system/remove-documents: Keep-Alive: timeout=5
http://localhost:3001/api/v1/system/remove-documents:
http://localhost:3001/api/v1/system/remove-documents: Connection #0 to host localhost left intact

mrcoollion
Posts: 738
Joined: Thu Sep 11, 2014 1:49 pm

Re: No tsNetDeleteSync function how to delete ? Solved

Post by mrcoollion » Wed Jan 22, 2025 9:15 am

For those who are interested.

I got it solved an running but without tsNet functionality.
What I did is make use of the shell functionality in livecode to run a PowerShell script.
This makes it so much easier because I can run the script as a curl script (which Windows, Linux and Mac support with slight differences).
I made the following code for this which you can easaly change for running any powershell script.
The script I stored in a field (fld "CurlDeleteScript") to make sure it can be used as is (no quotes issues).
- tScripLines is the script (template) I want to run
- tScriptOutputField is used for the field where the messages need to go.
TIP: Use AI to make the script for you! :D

Code: Select all

Global gaStoredData
on mouseUp
   put fld "DocumentID" into tDocumentID
   put fld "DocumentName" into tDocumentName
   set itemdelimiter to "/"
   put item 2 of tDocumentName into tOnlyDocument
   set itemdelimiter to comma
   --
   if tDocumentID = "" or tDocumentName = ""
   then
      answer "Missing Document ID or Document Name. Quitting!"
      exit MouseUp
   end if
   --
   put fld "CurlDeleteScript" into ScripLines
   put "C:\tmp" into tAppPath // Is where temporary file is stored.
   --
   AI_AnythingLLM_FileRemove_shell_PS ScripLines, tAppPath, tOuttScriptOutputVar, tOutMessage
   --
   put tOutMessage into field "responseField"
   --
end mouseUp

//======================================================================
// DETETE from AnythingLLM :Makescript file and let powershell run it
//======================================================================
command AI_AnythingLLM_FileRemove_shell_PS ScripLines, tAppPath, @tScriptOutputVar @tOutMessage
   if ScripLines is empty then 
      put "Yes"into tErrorYN
      put "No scriptlines ???" into tOutMessage
      exit AI_AnythingLLM_FileRemove_shell_PS
   end if
   if tAppPath is empty then 
      put "Yes"into tErrorYN
      put "No Application Path given where the temp script must be placed ???" into tOutMessage
      exit AI_AnythingLLM_FileRemove_shell_PS
   else
      put tAppPath into theAppPath
   end if 
   -- Extracting necessary data
   put gaStoredData["APIKeyAnythingLLM"] into tAPIKeyAnythingLLM
   put gaStoredData["APIChatUrlAnythingLLM"] into tAPIChatUrlAnythingLLM
   put gaStoredData["APIUploadUrlAnythingLLM"] into tAPIUploadUrlAnythingLLM
   put gaStoredData["APIAddDeleteUrlAnythingLLM"] into tAPIAddDeleteUrlAnythingLLM
   put gaStoredData["APIRemoveFileUrlAnythingLLM"] into tAPIRemoveFileUrlAnythingLLM
   put gaStoredData["AnythingLLMWorkspace"] into tAnythingLLMWorkspace
   -- REPLACE placeholders in the script
   put fld "DocumentName" into tDocumentID
   // <URL>  <APIKEY>    <DOCID>
   replace "{workspace}" with tAnythingLLMWorkspace in tAPIAddDeleteUrlAnythingLLM
   replace "<URL>" with  tAPIRemoveFileUrlAnythingLLM in ScripLines 
   replace "<APIKEY>" with  tAPIKeyAnythingLLM in ScripLines 
   replace "<DOCID>" with  tDocumentID in ScripLines 
   -------- From here the script is agnostic ---------
   put  the Seconds into tScriptFileName // Is a temp file excl of .ps1 extension
   put   tScriptFileName &".ps1" into tScriptFileName
   if the last character of theAppPath is not "/" or the last character of theAppPath is not "\"  then  put "/" after theAppPath
   put theAppPath & tScriptFileName into thePathwithFile
   //replace "\" with "/" in thePathwithFile // Make all the same
   replace "/" with "\" in thePathwithFile // Make all the same
   put "file:" & thePathwithFile into tScriptFileNameGen
   put empty into theresult
   //answer tScriptFileNameGen
   put ScripLines into URL tScriptFileNameGen // First make the script .ps1 file that needs to be run with the powershell command!
   put the result into theresult
   if theresult is not empty then
      put "Error generating temp scriptfile: "&theresult into tOutMessage
      Put "Yes" into tErrorYN
      exit AI_AnythingLLM_FileRemove_shell_PS
   end if
   put tScriptFileNameGen into tScriptFileName
   set the hideconsolewindows to true // Disable the window console
   put "powershell.exe " & "powershell.exe -ExecutionPolicy ByPass -File " & thePathwithFile into  tCommandstring 
   -----
   put shell(tCommandstring)  into tScriptOutputVar 
   put tScriptOutputVar into tOutMessage
   set the hideconsolewindows to false // Enable the window console
   if tOutMessage contains "success=True" then put "" else put tOutMessage into tResultMessage
   delete file thePathwithFile 
   put the result into theresult
   if theresult is not empty or tResultMessage is not empty then
      put  "Error : "&tScriptFileNameGen&" error message: "&theresult &lf & tResultMessage into tOutMessage
      Put "Yes"into tErrorYN
   end if
end AI_AnythingLLM_FileRemove_shell_PS

Post Reply

Return to “Talking LiveCode”