# Upsert Vector

> 在向量数据库中插入或更新记录。

Source: https://docs.sommatic.ai/zh/nodes/ai.vector.upsert/

- 标识: `ai.vector.upsert`
- 类别: AI
- 版本: 1.0.0
- 状态: 实验性. 该节点调用的后端向量写入接口尚未上线，因此不会写入任何数据。
- 执行方式: async

在向量数据库中插入或更新记录。

用于数据摄取阶段。它接收向量（及其元数据/文本），并存入索引以供后续检索。

**适用场景：**
- 为新文档建立索引。
- 更新用户画像的嵌入。

## 配置

- `index` (string, 必填)

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `success` (默认): Success
- `error`: Error

## 输入契约

- `vectors` (array<object>, 必填): 要插入的记录列表。

## 输出契约

- `count` (number, 可选): 执行 UPSERT 的记录数。

## 在表达式中

后续节点可通过 `$("Upsert Vector").item.json.<路径>` 读取此节点输出中的内容（请把 `Upsert Vector` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `success` 返回的路径，依据“端口”一节所示的输出。

- `$("Upsert Vector").item.json.count` (number) = `2`: 执行 UPSERT 的记录数。
- `$("Upsert Vector").item.json.__port` (string) = `"success"` (编辑器不提示)


## 工作原理

实验性。将 `index` 和 `vectors` 发送到后端尚不存在的向量写入端点，因此目前不会存储任何内容；调用失败后本次运行也随之失败。

1. 解析节点配置：配置键中的表达式会基于本次运行求值；传入条目按原样使用。
2. 将传入条目与配置合并，并要求提供 `index` 和非空的 `vectors` 数组；缺少任一项都会抛出异常。
3. 向后端 POST `{ index, vectors }`。404 会以 `Upsert service not available on Backend` 抛出。
4. 如果后端成功响应，worker 会返回不含 `port` 的 `{ __port: "success", count }`；引擎会路由到此节点未声明的 `out`，因此 `success` 连线不会执行。

## 在工作流中

### 节点

```json
{
  "node_id": "node-vector-upsert",
  "label": "Store embeddings",
  "operator_slug": "ai.vector.upsert",
  "operator_version": "1.0.0",
  "config": {
    "index": "handbook"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "__port": "success",
    "count": 2
  }
}
```

## 输出端口 (代码中的行为)

### `success`

永远不会触发：即使调用成功也会路由到未声明的 `out`。代码将返回的结构如下：

```json
{
  "__port": "success",
  "count": 2
}
```

### `error`

从不以信封形式返回：所有失败都会抛出异常并使运行失败。

## 代码中的行为

- `vectors`: 从传入条目读取（或从配置中的 `vectors` 键读取，可为表达式）。每条记录为 `{ id, values, metadata }`；按原样发送，不做校验。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Missing 'index' config. Set a target index in the node configuration.` | `error` | 以异常抛出，而不是作为信封返回：引擎会将本次运行标记为失败，除非节点位于 `flow.try` 作用域内，此时错误会进入其 catch 分支。 |
| `Missing or empty 'vectors' input. Provide at least one vector record.` | `error` | 以异常抛出，而不是作为信封返回：引擎会将本次运行标记为失败，除非节点位于 `flow.try` 作用域内，此时错误会进入其 catch 分支。 |
| `Upsert service not available on Backend` | `error` | 后端尚未提供向量写入端点（HTTP 404）。以异常抛出：除非被 `flow.try` 作用域捕获，否则运行失败。 |

## 说明与限制

- 后端尚未实现：没有可写入的向量存储。请勿在生产工作流中使用此节点。
- 后端请求未设置超时。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "ai.vector.upsert",
  "name": "Upsert Vector",
  "category": "ai",
  "version": "1.0.0",
  "description": "Inserts or Updates records in a Vector Database.\n\n  Used during the ingestion phase. It takes vectors (and their metadata/text) and stores them in the index for future retrieval.\n\n  **Use Cases:**\n  - Indexing a new document.\n  - Updating user profile embeddings.",
  "capabilities": [
    "vector",
    "database",
    "write",
    "upsert"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "index"
    ],
    "properties": {
      "index": {
        "type": "string",
        "title": "Target Index"
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "vectors": {
        "type": "array",
        "description": "List of records to insert.",
        "items": {
          "type": "object",
          "properties": {
            "id": {
              "type": "string"
            },
            "values": {
              "type": "array",
              "items": {
                "type": "number"
              }
            },
            "metadata": {
              "type": "object"
            }
          }
        }
      }
    },
    "required": [
      "vectors"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "count": {
        "type": "number",
        "description": "Number of records UPSERTED."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Success",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "async",
  "tags": [
    "vector",
    "write",
    "database"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "index"
  ],
  "properties": {
    "index": {
      "type": "string",
      "title": "Target Index"
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "vectors": {
      "type": "array",
      "description": "List of records to insert.",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "values": {
            "type": "array",
            "items": {
              "type": "number"
            }
          },
          "metadata": {
            "type": "object"
          }
        }
      }
    }
  },
  "required": [
    "vectors"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "count": {
      "type": "number",
      "description": "Number of records UPSERTED."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Success",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
