I cannot work out when the message is finished

New to this and used ChatGPT and got the following code working. (TMS Webcore for delphi). However I tried to use the ‘has_more’ value ( presuming the value would be true on the first poll result ) The value is always false. What am I not understanding ???

unit Unit1;

interface

uses
  System.SysUtils, System.Classes, JS, Web, WEBLib.Graphics, WEBLib.Controls,
  WEBLib.Forms, WEBLib.Dialogs, WEBLib.REST, Vcl.Controls, Vcl.StdCtrls,
  WEBLib.StdCtrls,WEBLib.JSON, WEBLib.ExtCtrls,
   WEBLib.Utils, WEBLib.ComCtrls;

type
  TForm1 = class(TWebForm)
    WebHttpRequest1: TWebHttpRequest;
    WebButton1: TWebButton;
    WebHttpRequest2: TWebHttpRequest;
    WebMemo1: TWebMemo;
    WebMemo2: TWebMemo;
    WebTimer1: TWebTimer;
    WebTimer2: TWebTimer;
    WebHttpRequest3: TWebHttpRequest;
    WebButton2: TWebButton;
    WebHttpRequest4: TWebHttpRequest;
    WebButton3: TWebButton;
    editQuestion: TWebEdit;
    WebHTMLContainer1: TWebHTMLContainer;
    WebEdit1: TWebEdit;
    WebButton4: TWebButton;
    WebPageControl1: TWebPageControl;
    WebPageControl1Sheet1: TWebTabSheet;
    WebPageControl1Sheet2: TWebTabSheet;
    WebPageControl1Sheet3: TWebTabSheet;
    procedure WebButton1Click(Sender: TObject);


    procedure WebTimer2Timer(Sender: TObject);

    procedure WebButton2Click(Sender: TObject);
    procedure WebButton3Click(Sender: TObject);
    procedure MakeOpenAIRequest(question : String);
    procedure WebTimer1Timer(Sender: TObject);
    procedure pollThread(ThreadID: String);
    procedure OnThreadPollReceived(Sender: TObject; AResponse: string);
    procedure WebButton4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
var

  ThreadID, RunID: string; // To store the thread ID
  JsonResponseAnswer : TJSONObject;

{$R *.dfm}



procedure TForm1.MakeOpenAIRequest(question : String);
var
  Http: TJSXMLHttpRequest;
   jsonRequest: String;
  JsonResponse: TJSONObject;
begin
  Http := TJSXMLHttpRequest.New;


  Http.OnReadyStateChange :=
    procedure
    begin
      if Http.ReadyState = 4 then
      begin
        if Http.Status = 200 then
        begin
          // Parse the response JSON to extract the ThreadID

          JsonResponse := TJSONObject.ParseJSONValue(Http.ResponseText) as TJSONObject;

          if Assigned(JsonResponse) then
          begin

            ThreadID := JsonResponse.Values['thread_id'].Value;
            RunID := JsonResponse.Values['id'].Value;

          end;

          // Set the content of WebMemo2 with the API response
          WebMemo2.Text := Http.ResponseText;
          WebTimer1.Enabled := true;  // start the timer to call pollThread
          WebTimer2.Enabled := true;  // start the timer to show second elapsed
        end
        else
        begin
          ShowMessage('Request failed with status code ' + IntToStr(Http.Status));
          ShowMessage('Response: ' + Http.ResponseText); // Display the response content on error
        end;
      end;
    end;

  Http.Open('POST', 'https://api.openai.com/v1/threads/runs', True);
  Http.SetRequestHeader('OpenAI-Beta', 'assistants=v1');
  Http.SetRequestHeader('Authorization', 'Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■F7vfrPfn');
  Http.SetRequestHeader('Content-Type', 'application/json');
  //Http.SetRequestHeader('OpenAI-Beta', 'assistants=v1');

  // Create a JSON request with your question
  jsonRequest := '{"assistant_id": "asst_9m7jUNMhEcbYdOCWaZO5q1jY","thread": {"messages": [{"role": "user", "content": "'+question+'"}]}}';

  Http.Send(jsonRequest);
end;


// this is called from Timer1



procedure TForm1.pollThread(ThreadID: string);
begin
  // Define the URL to get messages from a specific thread
  WebHttpRequest1.URL := 'https://api.openai.com/v1/threads/' + ThreadID + '/messages?order=asc';
  WebHttpRequest1.Command := httpGET;
  WebHttpRequest1.Headers.Values['Content-Type'] := 'application/json';
  WebHttpRequest1.Headers.Values['Authorization'] := 'Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■NbHCHag4';
  WebHttpRequest1.Headers.Values['OpenAI-Beta'] := 'assistants=v1';
  // Define the response event handler
  WebHttpRequest1.OnResponse := OnThreadPollReceived;

  // Execute the HTTP request
  WebHttpRequest1.Execute;
end;

procedure TForm1.OnThreadPollReceived(Sender: TObject; AResponse: string);
var

  Messages: TJSONArray;
  Answer: string;
  jsonRequest: String;
  JsonResponse, DataObject, ContentObject, TextObject: TJSONObject;
  DataArray, ContentArray: TJSONArray;
  i, j: Integer;
  valueText: String;
  hasMoreValue : Boolean;
begin
  WebMemo1.Clear;
  WebMemo2.Lines.Insert(0,'OnThreadPollReceived ' + AResponse);
  JsonResponse := TJSONObject.ParseJSONValue(AResponse) as TJSONObject;
  if Assigned(JsonResponse) then
  begin

         // JsonResponse := TJSONObject.ParseJSONValue(AResponse) as TJSONObject;

            hasMoreValue := (JsonResponse.Values['has_more'] as TJSONBool).AsBoolean;
            webMemo2.Lines.Insert(0,' has more value ' + hasMoreValue.ToString());
            if not hasMoreValue then
            begin
            //WebTimer1.Enabled := false;
            //WebTimer2.Enabled := false;
            end;
            //WebMemo2.Lines.Insert(0,'in loop looking for data ' + AResponse);
            DataArray := JsonResponse.Values['data'] as TJSONArray;
            for i := 0 to DataArray.Count - 1 do
            begin
              DataObject := DataArray.Items[i] as TJSONObject;
              if (DataObject <> nil) and (DataObject.Values['content'] <> nil) then
              begin
                ContentArray := DataObject.Values['content'] as TJSONArray;
                for j := 0 to ContentArray.Count - 1 do
                begin
                  ContentObject := ContentArray.Items[j] as TJSONObject;
                  if (ContentObject <> nil) and (ContentObject.Values['text'] <> nil) then
                  begin
                    TextObject := ContentObject.Values['text'] as TJSONObject;
                    if (TextObject <> nil) and (TextObject.Values['value'] <> nil) then
                    begin

                    end;
                  end;
                end;
              end;
            end;
            valueText := TextObject.Values['value'].Value;
                      //webEdit1.Text := '';
            //WebMemo1.text := WebMemo1.Text + valueText; // This will display the 'value'
            WebMemo1.Lines.Insert(0,valueText + WebMemo1.Text)  ;

  end
  else
  begin
    // Handle the case where the JSON response could not be parsed
    WebMemo2.Lines.Add('Error parsing JSON response');
  end;
end;





procedure TForm1.WebButton1Click(Sender: TObject);

begin

end;

procedure TForm1.WebButton2Click(Sender: TObject);
var

  question : String;
begin
    question := editQuestion.Text;
    MakeOpenAIRequest(question);

end;

procedure TForm1.WebButton3Click(Sender: TObject);

begin

  WebTimer1.Enabled := false;
  WebTimer2.Enabled := false;
end;


procedure TForm1.WebButton4Click(Sender: TObject);


begin

end;

procedure TForm1.WebTimer1Timer(Sender: TObject);
begin

      pollThread(ThreadID);
end;

procedure TForm1.WebTimer2Timer(Sender: TObject);
var
  SecondsElapsed: Integer;
begin
  SecondsElapsed := WebTimer2.Tag + 10; // Increment by 5 seconds
  WebTimer2.Tag := SecondsElapsed; // Update the tag for the next interval
  Webedit1.text := 'Seconds Elapsed: ' + SecondsElapsed.ToString + ' seconds';
  if SecondsElapsed > 60 then
    begin
      WebTimer1.Enabled := false;
      WebTimer2.Enabled := false;
    end;

end;

end.

I was doing the procedure wrong. I should poll the run and check the status. If completed get the thread/message