Purpose
Add OpenAI functions (extended): Undo and Redo buttons

This extension builds on the previous example, adding user-friendly controls to manage AI-generated text interactions.

Solution

  • Before invoking the AI, store the current text input.
  • After receiving the AI response, save the updated text.
  • Add Undo and Redo buttons to the interface.
  • When clicked, these buttons will restore the appropriate saved text to the input field—allowing users to easily revert or reapply changes.

Video

Show video in full screen


GuiXT Script:

GuiXT
// Customer complaint ?
if Q[Transaction=QM01] or Q[Transaction=QM02]
  
  // notification type Q1 ?
  Set V[qm_notification_type] "&F[RIWO00-QMARTE]" 
  if V[qm_notification_type=Q1]
    
    // new transaction? then reset all undo/redo variables
    if not V[qm_transactionid=&V[_transactionid]]
      Set V[qm_transactionid] "&V[_transactionid]"
      Set V[qm_undo_level] 0
      Set V[qm_undo_max] 0
      Clear text[qm_undo_*]
    endif
    
    // we need some space for our new buttons 
    pos G[Reference object]   (32,1) 
    BoxSize G[Subject]    (11,82)
    
    // add button to improve text via OpenAI
    PushButton X[Text]+(6,0) _
      "@IY\QImprove the text via OpenAI@" _
      process="qm_improve_text.txt" size=(1,2)
    
    // add "undo" button
    if V[qm_undo_level=0]
      Set V[qm_undo_option] "-disabled"
    else
      Set V[qm_undo_option] ""
    endif
    
    PushButton X[Text]+(6,4)  "@CF\QUndo@"  process="qm_undo_text.txt" _ 
      size=(1,2) &V[qm_undo_option] 

    
    // add "redo" button
    if V[qm_undo_level=&V[qm_undo_max]]
      Set V[qm_redo_option] "-disabled"
    else
      Set V[qm_redo_option] ""
    endif
    
    PushButton X[Text]+(6,8)  "@CG\QRedo@" process="qm_redo_text.txt" _ 
      size=(1,2)  &V[qm_redo_option] 
    
  endif  
endif



InputScript "qm_improve_text.txt":

GuiXT
CopyText fromScreen=X[Text] toText="qm_longtext"
CallVB result = openai.openaihelper.askGPT _ 
  "Can you please improve the following notification text 
      and only return the new text? &text[qm_longtext]"

if V[result]
  // save text into undo/redo butffer
  copyText fromText="qm_longtext" toText="qm_undo_text_&V[qm_undo_level]" 
   
  // increase undo level
  Set V[qm_undo_level] &V[qm_undo_level] + 1

  // set max level for redo
  if V[qm_undo_max<&V[qm_undo_level]]
    Set V[qm_undo_max] &V[qm_undo_level]
  endif

   // save text into undo/redo butffer
  copyText fromString="result" toText="qm_undo_text_&V[qm_undo_level]" 

  // to screen 
 CopyText fromText="qm_undo_text_&V[qm_undo_level]" toScreen=X[Text] 

endif

Return


InputScript "qm_undo_text.txt":

GuiXT
// undo buffer available? 
if V[qm_undo_level<1]
  Return
endif

//decrease undo level
Set V[qm_undo_level] &V[qm_undo_level] - 1

// set text
CopyText fromText="qm_undo_text_&V[qm_undo_level]" toScreen=X[Text] 

Return


InputScript "qm_redo_text.txt":

GuiXT
// redo possible ? 
if not V[qm_undo_max>&V[qm_undo_level]]
  Return
endif

//increase undo level
Set V[qm_undo_level] &V[qm_undo_level] + 1

// set text
CopyText fromText="qm_undo_text_&V[qm_undo_level]" toScreen=X[Text] 

Return

Components InputAssistant + Controls