跳到主要内容

集成

集成

集成把外部收发行为绑定到集合。每个集成是模型旁的一个文件,自动被发现。

定义

把集成声明为 +integrations.ts 中的具名绑定——每个外部系统一个绑定,各自拥有独立的 receive 与 send 行为:

import { defineConnection, definePull } from '@norbital-ai/bolt/authoring';
import { Schema } from 'effect';
import type { Integrations } from './$types.js';

const accounting = defineConnection({
  baseUrl: 'https://erp.internal.example/api/v1',
  authentication: { type: 'bearer', token: { env: 'ACCOUNTING_TOKEN' } }
});

export default {
  accounting: {
    policies: ['sites_read', 'accounting_integration'],
    connection: accounting,
    receive: {
      invoices_changed: definePull({
        pull: { schedule: '15 * * * *', method: 'GET', path: '/invoices/changed' },
        records: { field: 'invoices' },
        input: Schema.Struct({ external_code: Schema.String, amount: Schema.Number }),
        identity: { column: 'external_code', value: (row) => row.external_code },
        map: (row) => ({ external_code: row.external_code, amount: row.amount })
      })
    },
    send: {
      confirm: {
        on: { update: ({ previous, record }) => previous.status !== record.status },
        send: { method: 'POST', path: '/sites/confirmed' },
        body: ({ record }) => ({ reference: record.name })
      }
    }
  }
} satisfies Integrations;

连接留在宿主

连接与密钥值仍然是宿主设施;租户源码只声明需求——凭据永远不会到达工作区代码。参见 设施 。

选择最窄的角色

配套角色刻意重叠——选择最合适的一个:

  • 写入契约 ——调用方可提交什么、变更不变量与写入引发的通知( 写入契约)
  • 流水线 ——可复用的批量摄取与产物约定( 流水线)
  • 集成 ——复用流水线的可靠外部投递