@segmentstream/ontology 0.114.0 → 0.116.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/dist/agent-chat.d.ts +6 -0
- package/dist/agent-chat.js +28 -4
- package/dist/agent-review.d.ts +2 -2
- package/dist/analyze.d.ts +12 -1
- package/dist/analyze.js +51 -2
- package/dist/chat.d.ts +7 -0
- package/dist/diagnostics.d.ts +3 -3
- package/dist/diagnostics.js +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/manifest.d.ts +75 -1
- package/dist/manifest.js +17 -1
- package/dist/migrate/model.d.ts +32 -0
- package/dist/runtime/agents.d.ts +3 -3
- package/dist/runtime/agents.js +104 -1
- package/dist/service/agent/chat-archive.d.ts +5 -0
- package/dist/service/agent/chat-archive.js +2 -1
- package/dist/service/agent/protocol.d.ts +3 -0
- package/dist/service/agent/protocol.js +8 -0
- package/dist/service/agent/server.js +25 -1
- package/dist/service/chat/envelope.d.ts +2 -2
- package/dist/service/chat/reads.js +50 -0
- package/dist/service/chat/resolve.js +6 -1
- package/dist/service/mcp/tools.js +3 -1
- package/dist/service/runtime-contract.d.ts +28 -0
- package/dist/service/schemas.d.ts +8 -0
- package/dist/service/workflows.js +1 -0
- package/dist/storage/attachments.js +9 -4
- package/dist/webhook-token.d.ts +26 -0
- package/dist/webhook-token.js +47 -0
- package/dist/workflow.d.ts +8 -0
- package/dist/workflow.js +1 -0
- package/package.json +1 -1
|
@@ -151,6 +151,14 @@ export const ChatSeenRequest = Schema.Struct({
|
|
|
151
151
|
migratedFrom: Schema.optionalKey(ChatId.annotate({
|
|
152
152
|
description: "The chat's id before it became a supergroup: its record moves to externalId.",
|
|
153
153
|
})),
|
|
154
|
+
kind: Schema.optionalKey(Schema.Literal("private").annotate({
|
|
155
|
+
description: "A person's private chat, which the inbox registers when memberId holds one of its roles; " +
|
|
156
|
+
"absent for a group chat.",
|
|
157
|
+
})),
|
|
158
|
+
recipient: Schema.optionalKey(ChatId.annotate({
|
|
159
|
+
description: "Whom a reply in the private chat is sent to, where it differs from externalId.",
|
|
160
|
+
})),
|
|
161
|
+
threadId: Schema.optionalKey(ChatId.annotate({ description: "The thread a reply in the private chat stays in." })),
|
|
154
162
|
});
|
|
155
163
|
/**
|
|
156
164
|
* `POST /api/ontology/agent/reviews`: a reply the agent held for a member to review, for the member
|
|
@@ -208,6 +208,7 @@ function resolveChannelIdentity(store, agentTools) {
|
|
|
208
208
|
provisioned: resolution.provisioned,
|
|
209
209
|
notice_due: resolution.noticeDue,
|
|
210
210
|
on_new_user_pending: resolution.onNewUserPending,
|
|
211
|
+
...(resolution.inbox === true ? { inbox: true } : {}),
|
|
211
212
|
});
|
|
212
213
|
case "limited":
|
|
213
214
|
return json({
|
|
@@ -278,7 +279,30 @@ function seeChat(store) {
|
|
|
278
279
|
if (Result.isFailure(decoded)) {
|
|
279
280
|
return invalidBody(requestId, schemaIssues(decoded.failure));
|
|
280
281
|
}
|
|
281
|
-
const
|
|
282
|
+
const { kind, recipient, threadId, ...seen } = decoded.success;
|
|
283
|
+
if (kind === "private") {
|
|
284
|
+
// A person's private chat joins the inbox, which records nothing for anyone else.
|
|
285
|
+
const inboxed = seen.memberId === undefined ||
|
|
286
|
+
store.seeInboxChat === undefined ||
|
|
287
|
+
seen.leftMemberId !== undefined ||
|
|
288
|
+
seen.agentLeft !== undefined ||
|
|
289
|
+
seen.migratedFrom !== undefined
|
|
290
|
+
? undefined
|
|
291
|
+
: yield* store.seeInboxChat({
|
|
292
|
+
provider: seen.provider,
|
|
293
|
+
externalId: seen.externalId,
|
|
294
|
+
title: seen.title,
|
|
295
|
+
memberId: seen.memberId,
|
|
296
|
+
...(recipient === undefined ? {} : { recipient }),
|
|
297
|
+
...(threadId === undefined ? {} : { threadId }),
|
|
298
|
+
});
|
|
299
|
+
if (inboxed === undefined) {
|
|
300
|
+
return toServerResponse(notFound(requestId, "The chat is not one the inbox takes."));
|
|
301
|
+
}
|
|
302
|
+
yield* Effect.logInfo("ontology.agent.chat_reported").pipe(Effect.annotateLogs({ requestId, provider: seen.provider, chatId: seen.externalId }));
|
|
303
|
+
return json(inboxed);
|
|
304
|
+
}
|
|
305
|
+
const chat = yield* store.seeChat(seen);
|
|
282
306
|
yield* Effect.logInfo("ontology.agent.chat_reported").pipe(Effect.annotateLogs({
|
|
283
307
|
requestId,
|
|
284
308
|
provider: decoded.success.provider,
|
|
@@ -71,7 +71,7 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
|
|
|
71
71
|
readonly live_period?: number | undefined;
|
|
72
72
|
} | undefined;
|
|
73
73
|
} | {
|
|
74
|
-
readonly kind: "
|
|
74
|
+
readonly kind: "group" | "private";
|
|
75
75
|
readonly provider: "slack" | "whatsapp";
|
|
76
76
|
readonly chat_id: string;
|
|
77
77
|
readonly thread_id: string;
|
|
@@ -125,7 +125,7 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
|
|
|
125
125
|
readonly live_period?: number | undefined;
|
|
126
126
|
} | undefined;
|
|
127
127
|
} | {
|
|
128
|
-
readonly kind: "
|
|
128
|
+
readonly kind: "group" | "private";
|
|
129
129
|
readonly provider: "slack" | "whatsapp";
|
|
130
130
|
readonly chat_id: string;
|
|
131
131
|
readonly thread_id: string;
|
|
@@ -8,6 +8,32 @@ import { chatAttribution } from "./attribution.js";
|
|
|
8
8
|
import { chatResolver, groupChatKey, isChatMember } from "./resolve.js";
|
|
9
9
|
/** The kinds of channel whose archived chats are keyed by the chat a destination names. */
|
|
10
10
|
const HISTORY_KINDS = new Set(["telegram"]);
|
|
11
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
12
|
+
/**
|
|
13
|
+
* A private chat the inbox registered, by its record id, when the caller may read it: a member with
|
|
14
|
+
* `agent_chat:read` whose scope holds the record (`staff`), or the person in it. Its `externalId` is
|
|
15
|
+
* the chat's id in the archive on every channel. `undefined` for any other id.
|
|
16
|
+
*/
|
|
17
|
+
function registeredPrivateChat(backend, principal, id) {
|
|
18
|
+
return Effect.gen(function* () {
|
|
19
|
+
const [chat] = (yield* backend
|
|
20
|
+
.find({
|
|
21
|
+
entity: "agent_chat",
|
|
22
|
+
query: { where: { and: [{ id: { eq: id } }, { kind: { eq: "private" } }] }, limit: 1 },
|
|
23
|
+
})
|
|
24
|
+
.pipe(Effect.mapError((cause) => new ActionRejected({ message: "The chat directory could not be read.", cause })))).items;
|
|
25
|
+
// A group chat is read as before; only a private chat the inbox registered is read here.
|
|
26
|
+
if (chat === undefined || chat.kind !== "private") {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
const staff = isAllowed("agent_chat:read", principal) &&
|
|
30
|
+
(yield* backend.get({ entity: "agent_chat", id, reader: principal }).pipe(Effect.as(true), Effect.catchTag("NotFound", () => Effect.succeed(false))));
|
|
31
|
+
if (!staff && !(yield* isChatMember(backend, principal, id))) {
|
|
32
|
+
return yield* new PermissionDenied({ message: "This chat is not readable by the caller." });
|
|
33
|
+
}
|
|
34
|
+
return { provider: String(chat.provider), externalId: String(chat.external_id), staff };
|
|
35
|
+
});
|
|
36
|
+
}
|
|
11
37
|
export function chatReads(backend, options) {
|
|
12
38
|
const { providers, chatHistory, attribution } = options;
|
|
13
39
|
const kindOf = options.kindOf ?? ((channel) => channel);
|
|
@@ -53,6 +79,30 @@ export function chatReads(backend, options) {
|
|
|
53
79
|
if (principal.kind !== "member") {
|
|
54
80
|
return yield* new PermissionDenied({ message: "Only members may read chatHistory." });
|
|
55
81
|
}
|
|
82
|
+
const registered = UUID.test(input.chat)
|
|
83
|
+
? yield* registeredPrivateChat(backend, principal, input.chat)
|
|
84
|
+
: undefined;
|
|
85
|
+
if (registered !== undefined) {
|
|
86
|
+
if (!chatHistory) {
|
|
87
|
+
return yield* new ActionRejected({ message: "The chat archive is not configured." });
|
|
88
|
+
}
|
|
89
|
+
const key = `${registered.provider}:${registered.externalId}`;
|
|
90
|
+
// Staff read the chat's messages whoever wrote them; its person reads the messages they
|
|
91
|
+
// took part in, as in any private chat, so an account paired with someone else since
|
|
92
|
+
// shows the earlier person nothing newer.
|
|
93
|
+
return yield* chatHistory({
|
|
94
|
+
principal,
|
|
95
|
+
identityToken,
|
|
96
|
+
search: {
|
|
97
|
+
provider: registered.provider,
|
|
98
|
+
chatId: registered.externalId,
|
|
99
|
+
...(input.before === undefined ? {} : { before: input.before }),
|
|
100
|
+
...(input.limit === undefined ? {} : { limit: input.limit }),
|
|
101
|
+
},
|
|
102
|
+
memberGroups: [],
|
|
103
|
+
...(registered.staff ? { chats: [key] } : {}),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
56
106
|
// History reads the chats of Telegram channels only. Other channels archive a chat under
|
|
57
107
|
// their session's address with private coordinates, not under the chat a destination
|
|
58
108
|
// names, so their history would be incomplete until the archive keys them the same way.
|
|
@@ -40,7 +40,10 @@ export function memberGroupChats(backend, principal) {
|
|
|
40
40
|
if (principal.kind !== "member") {
|
|
41
41
|
return [];
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
// A private chat the inbox registered is no group, whose person reads it as a participant.
|
|
44
|
+
return (yield* records(backend, "agent_chat", {
|
|
45
|
+
where: { and: [memberOf(principal), { kind: { ne: "private" } }] },
|
|
46
|
+
})).map(groupChatKey);
|
|
44
47
|
});
|
|
45
48
|
}
|
|
46
49
|
/** Whether a member is in one group chat, which lets them read its history without agent_chat:read. */
|
|
@@ -87,6 +90,8 @@ export function chatResolver(backend, principal, providers, options = {}) {
|
|
|
87
90
|
const groupId = chat !== undefined && UUID.test(chat);
|
|
88
91
|
const filters = [
|
|
89
92
|
{ provider: { in: [...providers] } },
|
|
93
|
+
// A private chat the inbox registered is read by its record id, never a destination here.
|
|
94
|
+
{ kind: { ne: "private" } },
|
|
90
95
|
...(chat === undefined ? [] : [groupId ? { id: { eq: chat } } : { title: { eq: chat } }]),
|
|
91
96
|
...(canGroup ? [] : [memberOf(principal)]),
|
|
92
97
|
];
|
|
@@ -800,7 +800,9 @@ export function mcpTools(built, chatHistory = false, storage = {}, navigation =
|
|
|
800
800
|
name: "chat_history",
|
|
801
801
|
title: "Read chat history",
|
|
802
802
|
description: "Read messages for an exact chat ID/title, with their own pagination. " +
|
|
803
|
-
"Reads a group chat you are in (every group with agent_chat:read)
|
|
803
|
+
"Reads a group chat you are in (every group with agent_chat:read), your own private chat, " +
|
|
804
|
+
"or, by its agent_chat id, a person's private chat the inbox registered, with " +
|
|
805
|
+
"agent_chat:read within your scope.",
|
|
804
806
|
inputSchema: encodedJsonSchema(ChatHistory),
|
|
805
807
|
annotations: READ,
|
|
806
808
|
permissions: [],
|
|
@@ -518,6 +518,11 @@ export type AgentResolution = {
|
|
|
518
518
|
readonly provisioned: boolean;
|
|
519
519
|
readonly noticeDue: boolean;
|
|
520
520
|
readonly onNewUserPending: boolean;
|
|
521
|
+
/**
|
|
522
|
+
* The person holds a role of the ontology's `inbox` and wrote in a private chat: the agent
|
|
523
|
+
* reports the chat (`POST /api/ontology/agent/chats/seen`), which registers it.
|
|
524
|
+
*/
|
|
525
|
+
readonly inbox?: true;
|
|
521
526
|
} | {
|
|
522
527
|
readonly status: "pairing_required";
|
|
523
528
|
} | {
|
|
@@ -624,6 +629,14 @@ export interface AgentStore {
|
|
|
624
629
|
readonly agentLeft?: true;
|
|
625
630
|
readonly migratedFrom?: string;
|
|
626
631
|
}) => Effect.Effect<SeenChat>;
|
|
632
|
+
/**
|
|
633
|
+
* Records a person's private chat with the agent for the inbox: creates the `agent_chat` of kind
|
|
634
|
+
* `private`, with the person as its one member, or keeps its title, recipient and thread current,
|
|
635
|
+
* and records when it last had a message, as `seeChat` does. `undefined`, recording nothing,
|
|
636
|
+
* unless the person is an active member holding a role of the ontology's `inbox`. Absent on
|
|
637
|
+
* stores that predate the inbox.
|
|
638
|
+
*/
|
|
639
|
+
readonly seeInboxChat?: (input: InboxChatInput) => Effect.Effect<SeenChat | undefined>;
|
|
627
640
|
/**
|
|
628
641
|
* A reply the agent held for review becomes a pending `agent_review` for the member it answers,
|
|
629
642
|
* once per `key`: a repeated report answers the review it created. `undefined` when the member is
|
|
@@ -651,6 +664,21 @@ export interface HeldReplyInput {
|
|
|
651
664
|
readonly mode: "always" | "whenUnsure";
|
|
652
665
|
readonly confidence: number | null;
|
|
653
666
|
}
|
|
667
|
+
/** A person's private chat the agent saw, as `POST /api/ontology/agent/chats/seen` reports it. */
|
|
668
|
+
export interface InboxChatInput {
|
|
669
|
+
/** The channel the chat is on, by name. */
|
|
670
|
+
readonly provider: string;
|
|
671
|
+
/** The chat's id in the chat archive. */
|
|
672
|
+
readonly externalId: string;
|
|
673
|
+
/** The person's name on the channel. */
|
|
674
|
+
readonly title: string;
|
|
675
|
+
/** The person, a user id. */
|
|
676
|
+
readonly memberId: string;
|
|
677
|
+
/** Whom a reply is sent to, where it differs from `externalId`. */
|
|
678
|
+
readonly recipient?: string;
|
|
679
|
+
/** The thread a reply stays in. */
|
|
680
|
+
readonly threadId?: string;
|
|
681
|
+
}
|
|
654
682
|
/** The review a held reply became. */
|
|
655
683
|
export interface HeldReview {
|
|
656
684
|
readonly id: string;
|
|
@@ -545,6 +545,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
545
545
|
readonly separator: Schema.optionalKey<Schema.String>;
|
|
546
546
|
}>>;
|
|
547
547
|
}>>;
|
|
548
|
+
readonly inbox: Schema.optionalKey<Schema.Struct<{
|
|
549
|
+
readonly roles: Schema.$Array<Schema.String>;
|
|
550
|
+
}>>;
|
|
548
551
|
readonly metrics: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
549
552
|
readonly name: Schema.String;
|
|
550
553
|
readonly label: Schema.String;
|
|
@@ -586,6 +589,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
586
589
|
readonly verifyToken: Schema.String;
|
|
587
590
|
}>>;
|
|
588
591
|
readonly headers: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
592
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
589
593
|
}>, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>;
|
|
590
594
|
}>>>;
|
|
591
595
|
readonly toolsets: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
@@ -863,6 +867,9 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
863
867
|
readonly separator: Schema.optionalKey<Schema.String>;
|
|
864
868
|
}>>;
|
|
865
869
|
}>>>;
|
|
870
|
+
readonly inbox: Schema.toEncoded<Schema.optionalKey<Schema.Struct<{
|
|
871
|
+
readonly roles: Schema.$Array<Schema.String>;
|
|
872
|
+
}>>>;
|
|
866
873
|
readonly metrics: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
867
874
|
readonly name: Schema.String;
|
|
868
875
|
readonly label: Schema.String;
|
|
@@ -904,6 +911,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
904
911
|
readonly verifyToken: Schema.String;
|
|
905
912
|
}>>;
|
|
906
913
|
readonly headers: Schema.optionalKey<Schema.$Array<Schema.String>>;
|
|
914
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
907
915
|
}>, Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>>;
|
|
908
916
|
}>>>>;
|
|
909
917
|
readonly toolsets: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
@@ -107,7 +107,10 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
107
107
|
const binding = provider.binding;
|
|
108
108
|
const missing = (id) => new NotFound({ entity: "attachment", id, message: "Attachment not found." });
|
|
109
109
|
const unavailable = (message) => new Conflict({ entity: "attachment", message });
|
|
110
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* Group chats come from the registry, whose private chats the inbox registered stay private
|
|
112
|
+
* here. Neither a tool argument nor a caller's roles name a group.
|
|
113
|
+
*/
|
|
111
114
|
const accessScope = (caller) => Effect.gen(function* () {
|
|
112
115
|
const [member] = yield* sql.unsafe(`SELECT kind::text, active FROM "user" WHERE id = $1::uuid FOR SHARE`, [caller.principal.id]);
|
|
113
116
|
if (member?.kind !== "member" || !member.active) {
|
|
@@ -124,7 +127,7 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
124
127
|
}
|
|
125
128
|
const separator = caller.chat.indexOf(":");
|
|
126
129
|
const [chat] = yield* sql.unsafe(`SELECT id::text FROM "agent_chat"
|
|
127
|
-
WHERE provider = $1 AND external_id = $2 FOR SHARE`, [caller.chat.slice(0, separator), caller.chat.slice(separator + 1)]);
|
|
130
|
+
WHERE provider = $1 AND external_id = $2 AND kind::text = 'group' FOR SHARE`, [caller.chat.slice(0, separator), caller.chat.slice(separator + 1)]);
|
|
128
131
|
if (chat === undefined) {
|
|
129
132
|
return "private";
|
|
130
133
|
}
|
|
@@ -619,9 +622,11 @@ export function attachmentRuntime(ontology, sql, provider) {
|
|
|
619
622
|
const rows = yield* sql.unsafe(`${SELECT} WHERE a.branch_id = $1
|
|
620
623
|
AND ($2 OR EXISTS (
|
|
621
624
|
SELECT 1 FROM "agent_chat" c JOIN "agent_chat_members" m ON m.agent_chat_id = c.id
|
|
622
|
-
WHERE c.provider || ':' || c.external_id = a.conversation AND
|
|
625
|
+
WHERE c.provider || ':' || c.external_id = a.conversation AND c.kind::text = 'group'
|
|
626
|
+
AND m.user_id = $3::uuid
|
|
623
627
|
) OR (a.owner_id = $3::uuid AND NOT EXISTS (
|
|
624
|
-
SELECT 1 FROM "agent_chat" c
|
|
628
|
+
SELECT 1 FROM "agent_chat" c
|
|
629
|
+
WHERE c.provider || ':' || c.external_id = a.conversation AND c.kind::text = 'group'
|
|
625
630
|
)))
|
|
626
631
|
AND ($4::text IS NULL OR a.conversation = $4)
|
|
627
632
|
AND ($5::uuid IS NULL OR a.owner_id = $5::uuid)
|
package/dist/webhook-token.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { WebhookTrigger } from "./workflow.js";
|
|
1
2
|
/** What a token signs before the webhook's name, so the key derives nothing else. */
|
|
2
3
|
export declare const WEBHOOK_TOKEN_CONTEXT = "segmentstream.webhook.v1:";
|
|
3
4
|
/**
|
|
@@ -9,3 +10,28 @@ export declare const WEBHOOK_TOKEN_CONTEXT = "segmentstream.webhook.v1:";
|
|
|
9
10
|
export declare function webhookToken(secret: string, name: string): string;
|
|
10
11
|
/** Whether a received token is the webhook's, compared in constant time. */
|
|
11
12
|
export declare function webhookTokenMatches(secret: string, name: string, received: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The origin providers reach the workspace's webhooks at, the workspace's assigned domain. The
|
|
15
|
+
* platform sets it on production deployments that declare webhook triggers, beside
|
|
16
|
+
* `SEGMENTSTREAM_WEBHOOK_SECRET`; previews and local runs have neither.
|
|
17
|
+
*/
|
|
18
|
+
export declare const WEBHOOK_ORIGIN_VARIABLE = "SEGMENTSTREAM_WEBHOOK_ORIGIN";
|
|
19
|
+
/**
|
|
20
|
+
* Why a webhook's URL is not known here: outside a production deployment, which alone receives
|
|
21
|
+
* webhooks, or before the platform set the deployment's webhook variables.
|
|
22
|
+
*/
|
|
23
|
+
export declare class WebhookUrlUnavailable extends Error {
|
|
24
|
+
readonly code = "webhook_url_unavailable";
|
|
25
|
+
constructor(webhook: string);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The full URL of a webhook trigger of this deployment, as `segmentstream workflow webhooks` shows
|
|
29
|
+
* it: the workspace's domain and `/api/webhooks/<name>`, followed by the token for a webhook
|
|
30
|
+
* without a signature. Workspace code gives it to the provider through the webhook's connection,
|
|
31
|
+
* to register an endpoint or as a payment's callback URL. A token URL starts runs for anyone who
|
|
32
|
+
* has it: send it only to the provider, never to records, logs or people.
|
|
33
|
+
*
|
|
34
|
+
* Call it in a step or an action's `prepare`, not in a workflow function. It throws
|
|
35
|
+
* {@link WebhookUrlUnavailable} in previews and local runs, which receive no webhooks.
|
|
36
|
+
*/
|
|
37
|
+
export declare function webhookUrl(trigger: WebhookTrigger, environment?: Readonly<Record<string, string | undefined>>): string;
|
package/dist/webhook-token.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { nameToWire } from "./authoring-names.js";
|
|
3
|
+
import { webhookPath } from "./webhooks.js";
|
|
2
4
|
/** What a token signs before the webhook's name, so the key derives nothing else. */
|
|
3
5
|
export const WEBHOOK_TOKEN_CONTEXT = "segmentstream.webhook.v1:";
|
|
4
6
|
/**
|
|
@@ -16,3 +18,48 @@ export function webhookTokenMatches(secret, name, received) {
|
|
|
16
18
|
const actual = Buffer.from(received);
|
|
17
19
|
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
18
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The origin providers reach the workspace's webhooks at, the workspace's assigned domain. The
|
|
23
|
+
* platform sets it on production deployments that declare webhook triggers, beside
|
|
24
|
+
* `SEGMENTSTREAM_WEBHOOK_SECRET`; previews and local runs have neither.
|
|
25
|
+
*/
|
|
26
|
+
export const WEBHOOK_ORIGIN_VARIABLE = "SEGMENTSTREAM_WEBHOOK_ORIGIN";
|
|
27
|
+
/** The variable holding the workspace's webhook secret, as the ontology service reads it. */
|
|
28
|
+
const WEBHOOK_SECRET_VARIABLE = "SEGMENTSTREAM_WEBHOOK_SECRET";
|
|
29
|
+
/**
|
|
30
|
+
* Why a webhook's URL is not known here: outside a production deployment, which alone receives
|
|
31
|
+
* webhooks, or before the platform set the deployment's webhook variables.
|
|
32
|
+
*/
|
|
33
|
+
export class WebhookUrlUnavailable extends Error {
|
|
34
|
+
code = "webhook_url_unavailable";
|
|
35
|
+
constructor(webhook) {
|
|
36
|
+
super(`The URL of the webhook ${webhook} is known only in a production deployment that declares it.`);
|
|
37
|
+
this.name = "WebhookUrlUnavailable";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The full URL of a webhook trigger of this deployment, as `segmentstream workflow webhooks` shows
|
|
42
|
+
* it: the workspace's domain and `/api/webhooks/<name>`, followed by the token for a webhook
|
|
43
|
+
* without a signature. Workspace code gives it to the provider through the webhook's connection,
|
|
44
|
+
* to register an endpoint or as a payment's callback URL. A token URL starts runs for anyone who
|
|
45
|
+
* has it: send it only to the provider, never to records, logs or people.
|
|
46
|
+
*
|
|
47
|
+
* Call it in a step or an action's `prepare`, not in a workflow function. It throws
|
|
48
|
+
* {@link WebhookUrlUnavailable} in previews and local runs, which receive no webhooks.
|
|
49
|
+
*/
|
|
50
|
+
export function webhookUrl(trigger, environment = process.env) {
|
|
51
|
+
const wire = nameToWire(trigger.name);
|
|
52
|
+
const origin = environment[WEBHOOK_ORIGIN_VARIABLE];
|
|
53
|
+
if (!origin) {
|
|
54
|
+
throw new WebhookUrlUnavailable(wire);
|
|
55
|
+
}
|
|
56
|
+
const url = `${new URL(origin).origin}${webhookPath(wire)}`;
|
|
57
|
+
if (trigger.signature !== undefined) {
|
|
58
|
+
return url;
|
|
59
|
+
}
|
|
60
|
+
const secret = environment[WEBHOOK_SECRET_VARIABLE];
|
|
61
|
+
if (!secret) {
|
|
62
|
+
throw new WebhookUrlUnavailable(wire);
|
|
63
|
+
}
|
|
64
|
+
return `${url}/${webhookToken(secret, wire)}`;
|
|
65
|
+
}
|
package/dist/workflow.d.ts
CHANGED
|
@@ -129,6 +129,8 @@ export interface WebhookTrigger<Name extends string = string> {
|
|
|
129
129
|
readonly verification?: WebhookVerification | undefined;
|
|
130
130
|
/** Lowercase names of the request headers the delivery carries. */
|
|
131
131
|
readonly headers: ReadonlyArray<string>;
|
|
132
|
+
/** The connection whose provider sends to this webhook, when declared. */
|
|
133
|
+
readonly connection?: AnyConnectionHandle | undefined;
|
|
132
134
|
}
|
|
133
135
|
export interface WebhookOptions {
|
|
134
136
|
/** Required, like every declaration: what the provider sends and why. */
|
|
@@ -142,6 +144,12 @@ export interface WebhookOptions {
|
|
|
142
144
|
readonly verification?: WebhookVerification;
|
|
143
145
|
/** Lowercase request headers to pass to the workflow, at most ten: `["x-event-type"]`. */
|
|
144
146
|
readonly headers?: ReadonlyArray<string>;
|
|
147
|
+
/**
|
|
148
|
+
* The connection whose provider sends to this webhook, listed in `defineOntology({ connections })`.
|
|
149
|
+
* Workspace code registers the webhook's URL with the provider through that connection, and the
|
|
150
|
+
* console shows the webhook on the connection's page.
|
|
151
|
+
*/
|
|
152
|
+
readonly connection?: AnyConnectionHandle;
|
|
145
153
|
}
|
|
146
154
|
/**
|
|
147
155
|
* Declares a webhook trigger; list it in `workflow(name, { input: webhookDelivery, triggers })`.
|
package/dist/workflow.js
CHANGED
|
@@ -31,6 +31,7 @@ export function webhook(name, options) {
|
|
|
31
31
|
signature: options.signature,
|
|
32
32
|
...(options.verification === undefined ? {} : { verification: options.verification }),
|
|
33
33
|
headers: options.headers ?? [],
|
|
34
|
+
...(options.connection === undefined ? {} : { connection: options.connection }),
|
|
34
35
|
};
|
|
35
36
|
}
|
|
36
37
|
export function after(name, target, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.116.0",
|
|
4
4
|
"description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|