Files
sam 125457a391
publish / publish (push) Successful in 1m21s
feat(connector): declare privileged effects on every lookup and action (#2071)
Regenerate the connector SDK from the host contract, which adds the
effects_declared, privilege_change and secret_access fields. Every lookup and
action in this repository now states them, and a Semaphore template states
them in its effect settings or is unavailable in chat.
2026-11-21 21:29:49 +11:00

112 lines
3.7 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
}
// 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
}
// 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))
}