{
  "openapi": "3.1.0",
  "info": {
    "title": "MemoryPlugin",
    "version": "1.0.0",
    "description": "API for managing and querying user memories"
  },
  "servers": [
    {
      "url": "https://www.memoryplugin.com"
    }
  ],
  "paths": {
    "/api/chat-history/inject": {
      "post": {
        "summary": "Fetch context from users chat history",
        "operationId": "ChatHistoryInject",
        "x-openai-isConsequential": false,
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatHistoryInjectRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Injection payload with context and metadata",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChatHistoryInjectResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      }
    },
    "/api/memory": {
      "post": {
        "summary": "Store a new memory",
        "operationId": "AddNewMemory",
        "x-openai-isConsequential": false,
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MemoryInput"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Memory stored successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SuccessResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      },
      "get": {
        "summary": "Query or retrieve memories.",
        "operationId": "GetMemories",
        "parameters": [
          {
            "name": "query",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Search query for memories. You will receive the 5 most relevant memories."
          },
          {
            "name": "all",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Fetch all memories"
          },
          {
            "name": "latest",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Fetch latest memories"
          },
          {
            "name": "count",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 10
            },
            "description": "Number of memories to retrieve, use with latest=true to limit the number of memories retrieved"
          },
          {
            "name": "v",
            "in": "query",
            "schema": {
              "type": "number",
              "default": 2
            },
            "description": "API version - current is 2"
          },
          {
            "name": "source",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Client source identifier (e.g., 'chatgpt' for ChatGPT requests)"
          },
          {
            "name": "skip",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0,
              "minimum": 0
            },
            "description": "Number of memories to skip for pagination. Zero-indexed: skip=0 starts from first memory, skip=10 skips first 10 memories and starts from 11th. Used primarily with token limiting to load additional memories beyond the initial response."
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Memory"
                      }
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Memory"
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      }
    },
    "/api/buckets": {
      "get": {
        "summary": "List all memory buckets",
        "operationId": "ListBuckets",
        "parameters": [
          {
            "name": "v",
            "in": "query",
            "schema": {
              "type": "number",
              "default": 2
            },
            "description": "API version - current is 2"
          },
          {
            "name": "source",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Client source identifier (e.g., 'chatgpt' for ChatGPT requests)"
          }
        ],
        "responses": {
          "200": {
            "description": "List of memory buckets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Bucket"
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      },
      "post": {
        "summary": "Create a new bucket",
        "operationId": "CreateBucket",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "description": "Name of the bucket to create"
                  },
                  "source": {
                    "type": "string",
                    "description": "Client source identifier (e.g., 'chatgpt' for ChatGPT requests)"
                  }
                },
                "required": ["name"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Bucket created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string",
                      "enum": ["Bucket created successfully"]
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      }
    },
    "/api/v2/memory": {
      "get": {
        "summary": "Get memories and buckets in a single request",
        "operationId": "GetMemoriesAndBuckets",
        "parameters": [
          {
            "name": "bucketId",
            "in": "query",
            "schema": {
              "type": "integer"
            },
            "description": "Optional bucket ID to filter memories"
          },
          {
            "name": "count",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 10
            },
            "description": "Number of memories to retrieve"
          },
          {
            "name": "all",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Fetch all memories"
          },
          {
            "name": "latest",
            "in": "query",
            "schema": {
              "type": "boolean"
            },
            "description": "Fetch latest memories"
          },
          {
            "name": "query",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Search query for memories"
          },
          {
            "name": "source",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Client source identifier (e.g., 'chatgpt' for ChatGPT requests)"
          },
          {
            "name": "skip",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 0,
              "minimum": 0
            },
            "description": "Number of memories to skip for pagination. Zero-indexed: skip=0 starts from first memory, skip=10 skips first 10 memories and starts from 11th. Used primarily with token limiting to load additional memories beyond the initial response."
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response with memories and buckets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "memories": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "text": {
                            "type": "string"
                          },
                          "metadata": {
                            "type": "object",
                            "properties": {
                              "text": {
                                "type": "string"
                              },
                              "createdAt": {
                                "type": "string",
                                "format": "date-time"
                              },
                              "bucketId": {
                                "type": "integer"
                              },
                              "bucketName": {
                                "type": "string"
                              }
                            }
                          }
                        }
                      }
                    },
                    "buckets": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Bucket"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      }
    }
  },
  "components": {
    "schemas": {
      "ChatHistoryInjectRequest": {
        "type": "object",
        "properties": {
          "query": {
            "type": "string",
            "description": "The user's current input used to search chat history for relevant context"
          },
          "conversationContext": {
            "type": "string",
            "description": "Optional short context string from the current conversation to help retrieval"
          },
          "maxTokens": {
            "type": "integer",
            "minimum": 1,
            "maximum": 2000,
            "default": 600,
            "description": "Maximum tokens for injected content (hard cap 2000)"
          }
        },
        "required": ["query"]
      },
      "ChatHistoryInjectionSource": {
        "type": "object",
        "properties": {
          "conversationId": { "type": "string" },
          "conversationTitle": { "type": "string" },
          "messageId": { "type": "string" },
          "date": { "type": "string" },
          "relevanceScore": { "type": "number" }
        }
      },
      "ChatHistoryInjectResponse": {
        "type": "object",
        "properties": {
          "content": { "type": "string" },
          "sources": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ChatHistoryInjectionSource" }
          },
          "tokenCount": { "type": "integer" },
          "message": { "type": "string" },
          "processingTime": { "type": "string", "description": "Human-readable processing time, e.g. '123ms'" },
          "model": { "type": "string" },
          "timingBreakdown": { "type": "object", "additionalProperties": true },
          "chunking": { "type": ["string", "null"], "enum": ["cross", "per-message", null] }
        }
      },
      "MemoryInput": {
        "type": "object",
        "properties": {
          "text": {
            "type": "string",
            "description": "The memory to be added. Always prepend with current date in YYYY-MM-DD format in the users timezone if you know it, UTC otherwise."
          },
          "bucketId": {
            "type": "integer",
            "description": "The id of the bucket to add the memory to. If not provided, the memory will be added to the default bucket."
          },
          "source": {
            "type": "string",
            "description": "Client source identifier (e.g., 'chatgpt' for ChatGPT requests)"
          }
        },
        "required": ["text"]
      },
      "Memory": {
        "type": "object",
        "properties": {
          "text": {
            "type": "string"
          },
          "score": {
            "type": "number"
          },
          "metadata": {
            "type": "object",
            "properties": {
              "bucketId": {
                "type": "integer",
                "description": "Numeric ID of the bucket"
              }
            }
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "text": {
            "type": "string"
          },
          "score": {
            "type": "number"
          }
        }
      },
      "SuccessResponse": {
        "type": "object",
        "properties": {
          "done": {
            "type": "string",
            "enum": ["ok"]
          }
        }
      },
      "Bucket": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "description": "Unique identifier for the bucket"
          },
          "name": {
            "type": "string",
            "description": "Display name of the bucket"
          },
          "description": {
            "type": "string",
            "description": "Optional description of the bucket"
          },
          "memoryCount": {
            "type": "integer",
            "description": "Number of memories in this bucket"
          }
        },
        "required": ["id", "name", "memoryCount"]
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    }
  }
}
