code: def upsert_workflow(
      workflow: AgentWorkFlowConfig, dbmanager: DBManager
  ) -> List[Dict[str, Any]]:
      """
      Insert or update a workflow for a specific user in the database.
  
      If the workflow with the given ID already exists, it will be updated with the new data.
      Otherwise, a new workflow will be created.
  
      :param workflow: The AgentWorkFlowConfig object containing workflow data
      :param dbmanager: The DBManager instance to interact with the database
      :return: A list of dictionaries, each representing a workflow after insertion or update
      """
      existing_workflow = get_item_by_field(
          "workflows", "id", workflow.id, dbmanager
      )
  
      if existing_workflow:
          updated_data = {
              "user_id": workflow.user_id,
              "timestamp": workflow.timestamp,
              "sender": json.dumps(workflow.sender.dict()),
              "receiver": json.dumps(
                  [receiver.dict() for receiver in workflow.receiver]
                  if isinstance(workflow.receiver, list)
                  else workflow.receiver.dict()
              ),
              "type": workflow.type,
              "name": workflow.name,
              "description": workflow.description,
              "summary_method": workflow.summary_method,
          }
          update_item("workflows", workflow.id, updated_data, dbmanager)
      else:
          query = "INSERT INTO workflows (id, user_id, timestamp, sender, receiver, type, name, description, summary_method) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)"
          args = (
              workflow.id,
              workflow.user_id,
              workflow.timestamp,
              json.dumps(workflow.sender.dict()),
              json.dumps(
                  [receiver.dict() for receiver in workflow.receiver]
                  if isinstance(workflow.receiver, list)
                  else workflow.receiver.dict()
              ),
              workflow.type,
              workflow.name,
              workflow.description,
              workflow.summary_method,
          )
          dbmanager.query(query=query, args=args)
  
      return get_workflows(
          user_id=workflow.user_id, dbmanager=dbmanager
      )
  
imports: import json
  import logging
  import sqlite3
  import threading
  import os
  from typing import Any, List, Dict, Optional, Tuple
  from ..datamodel import AgentFlowSpec, AgentWorkFlowConfig, Gallery, Message, Session, Skill
label_desc: Use image node_id 18 for calling a global function with example usage: upsert_workflow(workflow, dbmanager) and returns: get_workflows