Files
archipelago/sdk/connector/connector.go
T
sam 33890d3b52 fix(connector): declare operator settings in Describe
The contract has carried a settings list since the schema was added, but the
SDK never filled it, so a host received an empty declaration: every setting an
operator entered was refused as one the extension doesn't declare, and a
connector that needs any setting could not start at all.

Host, Telegram and Semaphore now declare the keys they already read.
2026-11-27 20:00:48 +11:00

123 lines
4.2 KiB
Go

// Package connector is what a Go connector extension builds on: the
// generated connector.v1 contract, the interfaces a connector implements and
// the Serve helper archie-core launches it with.
package connector
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json/jsontext"
"time"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// Contract is the connector contract version this SDK speaks.
const Contract = "v1"
// Config is the operator's settings and the host-resolved secrets.
type Config struct {
Settings map[string]string
Secrets map[string]string
}
// Description declares what a connector hears, does and answers.
type Description struct {
Events []*connectorv1.EventType
Actions []*connectorv1.ActionType
Lookups []*connectorv1.LookupType
Deliveries []*connectorv1.DeliveryType
// Settings are the operator values the connector reads, which the host
// renders as a form and refuses anything outside.
Settings []*connectorv1.Setting
}
// NoPrivilegedEffects declares a lookup or action that changes no privileges
// and accesses no secrets. Chat offers only tools that declare their effects,
// so a tool that never states them is unavailable there.
func NoPrivilegedEffects() *connectorv1.Effect {
return &connectorv1.Effect{EffectsDeclared: true}
}
// UndeclaredEffects names every action and lookup in d that doesn't state
// whether it changes privileges or accesses secrets.
func UndeclaredEffects(d Description) []string {
var names []string
for _, a := range d.Actions {
if !a.GetEffect().GetEffectsDeclared() {
names = append(names, "action "+a.GetName())
}
}
for _, l := range d.Lookups {
if !l.GetEffect().GetEffectsDeclared() {
names = append(names, "lookup "+l.GetName())
}
}
return names
}
// Event is one typed event parsed from a verified payload.
type Event struct {
Type string
SourceEventID string
Fields jsontext.Value
LinkKey string
OccurredAt time.Time
}
// Connector is the part every connector implements.
type Connector interface {
Describe() Description
}
// Parser turns a payload the host already verified into typed events.
type Parser interface {
Parse(ctx context.Context, body []byte, headers map[string]string) ([]Event, error)
}
// Actor resolves and runs actions. Invoke must honour the idempotency key for
// any action that isn't read-only, and returns unknown=true when it can't
// confirm what its effect did.
type Actor interface {
Resolve(ctx context.Context, action string, input jsontext.Value) (*connectorv1.ResolveResponse, error)
Invoke(ctx context.Context, action string, input jsontext.Value, idempotencyKey string) (output jsontext.Value, unknown bool, err error)
}
// Looker answers read-only lookups. found=false means nothing was found.
type Looker interface {
Lookup(ctx context.Context, name string, input jsontext.Value) (output jsontext.Value, found bool, err error)
}
// Deliverer sends deliveries: messages and records for people or a system of
// record. The host retries a failed delivery with the same idempotency key.
type Deliverer interface {
Deliver(ctx context.Context, delivery string, input jsontext.Value, idempotencyKey string) (externalID string, err error)
}
// Listener holds its own connection to a source and emits its events from
// cursor on. emit stores a batch and the cursor to resume after it; Listen
// returns when ctx ends or the connection can't continue, and the host calls
// it again with the last stored cursor.
type Listener interface {
Listen(ctx context.Context, cursor string, emit func(events []Event, cursor string) error) error
}
// Linker lets a person link their channel identity to an account. LinkURL
// returns the URL that delivers a link code to the connector, which then emits
// an account.link event carrying the code and the sender's identity. Its
// events name the sender in "by" as "<connector name>:<channel user id>".
type Linker interface {
LinkURL(ctx context.Context, code string) (string, error)
}
// Digest identifies one concrete action, the same way archie-core does: its
// name and its exact input.
func Digest(action string, input []byte) string {
h := sha256.New()
h.Write([]byte(action))
h.Write([]byte{0})
h.Write(input)
return hex.EncodeToString(h.Sum(nil))
}