@vellumai/assistant 0.9.0-dev.202606171903.06eea08 → 0.9.0-dev.202606172051.0afee75
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/package.json +1 -1
- package/src/__tests__/access-request-card-view.test.ts +98 -0
- package/src/__tests__/access-request-seed-content-blocks.test.ts +2 -4
- package/src/__tests__/slack-notification-approval-card.test.ts +170 -0
- package/src/__tests__/tool-approval-seed-content-blocks.test.ts +1 -1
- package/src/notifications/access-request-copy.ts +72 -59
- package/src/notifications/adapters/slack.ts +38 -68
- package/src/notifications/approval-card-data.ts +297 -0
- package/src/notifications/copy-composer.ts +3 -3
- package/src/notifications/decision-engine.ts +4 -2
- package/src/notifications/tool-approval-copy.ts +0 -142
package/package.json
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildAccessRequestCardView,
|
|
5
|
+
parseAccessRequestPayload,
|
|
6
|
+
} from "../notifications/access-request-copy.js";
|
|
7
|
+
|
|
8
|
+
function view(raw: Record<string, unknown>) {
|
|
9
|
+
return buildAccessRequestCardView(parseAccessRequestPayload(raw));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const TAB = String.fromCharCode(9);
|
|
13
|
+
|
|
14
|
+
describe("buildAccessRequestCardView", () => {
|
|
15
|
+
test("prefers actorDisplayName, falls back to senderIdentifier then 'Someone'", () => {
|
|
16
|
+
expect(
|
|
17
|
+
view({ actorDisplayName: "Alice", senderIdentifier: "U999" }).displayName,
|
|
18
|
+
).toBe("Alice");
|
|
19
|
+
expect(view({ senderIdentifier: "U999" }).displayName).toBe("U999");
|
|
20
|
+
expect(view({}).displayName).toBe("Someone");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("sanitizes identity fields (strips control characters)", () => {
|
|
24
|
+
const v = view({
|
|
25
|
+
actorDisplayName: `Al${TAB}ice`,
|
|
26
|
+
actorUsername: `a${TAB}lice`,
|
|
27
|
+
actorExternalId: `U9${TAB}99`,
|
|
28
|
+
});
|
|
29
|
+
expect(v.displayName).toBe("Al ice");
|
|
30
|
+
expect(v.username).toBe("a lice");
|
|
31
|
+
expect(v.externalId).toBe("U9 99");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("username and externalId are undefined when absent", () => {
|
|
35
|
+
const v = view({ actorDisplayName: "Alice" });
|
|
36
|
+
expect(v.username).toBeUndefined();
|
|
37
|
+
expect(v.externalId).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("detects Slack DM conversations", () => {
|
|
41
|
+
expect(
|
|
42
|
+
view({ sourceChannel: "slack", conversationExternalId: "D01XYZ" })
|
|
43
|
+
.isSlackDm,
|
|
44
|
+
).toBe(true);
|
|
45
|
+
expect(
|
|
46
|
+
view({ sourceChannel: "slack", conversationExternalId: "C01ABC" })
|
|
47
|
+
.isSlackDm,
|
|
48
|
+
).toBe(false);
|
|
49
|
+
expect(
|
|
50
|
+
view({ sourceChannel: "telegram", conversationExternalId: "D01XYZ" })
|
|
51
|
+
.isSlackDm,
|
|
52
|
+
).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("builds a Slack permalink only with slack source + conversation + ts", () => {
|
|
56
|
+
expect(
|
|
57
|
+
view({
|
|
58
|
+
sourceChannel: "slack",
|
|
59
|
+
conversationExternalId: "C01ABC",
|
|
60
|
+
messageTs: "1700000000.000100",
|
|
61
|
+
}).messagePermalink,
|
|
62
|
+
).toBe("https://slack.com/archives/C01ABC/p1700000000000100");
|
|
63
|
+
expect(
|
|
64
|
+
view({ sourceChannel: "slack", conversationExternalId: "C01ABC" })
|
|
65
|
+
.messagePermalink,
|
|
66
|
+
).toBeUndefined();
|
|
67
|
+
expect(
|
|
68
|
+
view({
|
|
69
|
+
sourceChannel: "telegram",
|
|
70
|
+
conversationExternalId: "C01ABC",
|
|
71
|
+
messageTs: "1.2",
|
|
72
|
+
}).messagePermalink,
|
|
73
|
+
).toBeUndefined();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("sanitizes message preview and yields undefined when blank after sanitizing", () => {
|
|
77
|
+
expect(view({ messagePreview: " hello " }).messagePreview).toBe("hello");
|
|
78
|
+
// Blank / control-character-only previews sanitize to empty → undefined
|
|
79
|
+
// (no empty quote block is rendered downstream).
|
|
80
|
+
expect(view({ messagePreview: "" }).messagePreview).toBeUndefined();
|
|
81
|
+
expect(view({ messagePreview: " " }).messagePreview).toBeUndefined();
|
|
82
|
+
expect(view({}).messagePreview).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("collects trust/security warnings", () => {
|
|
86
|
+
const v = view({
|
|
87
|
+
isStranger: true,
|
|
88
|
+
isRestricted: true,
|
|
89
|
+
previousMemberStatus: "revoked",
|
|
90
|
+
});
|
|
91
|
+
expect(v.warnings).toEqual([
|
|
92
|
+
"This user was previously revoked.",
|
|
93
|
+
"External Slack user (not in this workspace).",
|
|
94
|
+
"Guest / restricted account.",
|
|
95
|
+
]);
|
|
96
|
+
expect(view({}).warnings).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
parseAccessRequestPayload,
|
|
6
|
-
} from "../notifications/access-request-copy.js";
|
|
3
|
+
import { parseAccessRequestPayload } from "../notifications/access-request-copy.js";
|
|
4
|
+
import { buildAccessRequestSeedContentBlocks } from "../notifications/approval-card-data.js";
|
|
7
5
|
|
|
8
6
|
describe("buildAccessRequestSeedContentBlocks", () => {
|
|
9
7
|
const basePayload: Record<string, unknown> = {
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { parseAccessRequestPayload } from "../notifications/access-request-copy.js";
|
|
4
|
+
import { buildApprovalNotificationBlocks } from "../notifications/adapters/slack.js";
|
|
5
|
+
import type { ChannelDeliveryPayload } from "../notifications/types.js";
|
|
6
|
+
import type { ApprovalUIMetadata } from "../runtime/channel-approval-types.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Pins the Slack approval-card block contract: the title, identity subtitle,
|
|
10
|
+
* quoted-preview body, action callback ids, source/permalink and requester-id
|
|
11
|
+
* context blocks, warning subtext, and guardian verification note that
|
|
12
|
+
* `buildApprovalNotificationBlocks` emits for an access request.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const APPROVAL: ApprovalUIMetadata = {
|
|
16
|
+
requestId: "req-123",
|
|
17
|
+
actions: [
|
|
18
|
+
{ id: "approve_once", label: "Approve once" },
|
|
19
|
+
{ id: "reject", label: "Reject" },
|
|
20
|
+
],
|
|
21
|
+
plainTextFallback: 'Reply "ABC123 approve" or "ABC123 reject"',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function buildPayload(
|
|
25
|
+
raw: Record<string, unknown>,
|
|
26
|
+
approval: ApprovalUIMetadata = APPROVAL,
|
|
27
|
+
): ChannelDeliveryPayload {
|
|
28
|
+
return {
|
|
29
|
+
sourceEventName: "ingress.access_request",
|
|
30
|
+
copy: { title: "Access Request", body: "Someone is requesting access" },
|
|
31
|
+
urgency: "high",
|
|
32
|
+
approvalContext: approval,
|
|
33
|
+
accessRequestContext: parseAccessRequestPayload(raw),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type Block = Record<string, unknown>;
|
|
38
|
+
|
|
39
|
+
function card(blocks: unknown[]): Block {
|
|
40
|
+
const c = (blocks as Block[]).find((b) => b.type === "card");
|
|
41
|
+
if (!c) throw new Error("no card block");
|
|
42
|
+
return c;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function contextTexts(blocks: unknown[]): string[] {
|
|
46
|
+
return (blocks as Block[])
|
|
47
|
+
.filter((b) => b.type === "context")
|
|
48
|
+
.map((b) => {
|
|
49
|
+
const elements = b.elements as Array<{ text: string }>;
|
|
50
|
+
return elements.map((e) => e.text).join("");
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function text(node: unknown): string {
|
|
55
|
+
return (node as { text: string }).text;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const BASE: Record<string, unknown> = {
|
|
59
|
+
requestId: "req-123",
|
|
60
|
+
requestCode: "ABC123",
|
|
61
|
+
sourceChannel: "slack",
|
|
62
|
+
conversationExternalId: "C01ABC",
|
|
63
|
+
actorExternalId: "U999",
|
|
64
|
+
actorDisplayName: "Alice",
|
|
65
|
+
actorUsername: "alice",
|
|
66
|
+
senderIdentifier: "U999",
|
|
67
|
+
messagePreview: "Hello, I need help with something",
|
|
68
|
+
messageTs: "1700000000.000100",
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
describe("Slack access-request card blocks", () => {
|
|
72
|
+
test("card carries title, identity subtitle, and quoted preview body", () => {
|
|
73
|
+
const c = card(buildApprovalNotificationBlocks(buildPayload(BASE), "msg"));
|
|
74
|
+
expect(text(c.title)).toBe("Access Request");
|
|
75
|
+
expect(text(c.subtitle)).toBe("Alice (@alice) via slack");
|
|
76
|
+
expect(text(c.body)).toBe('> _"Hello, I need help with something"_');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("card actions encode the apr:<requestId>:<action> callback ids", () => {
|
|
80
|
+
const c = card(buildApprovalNotificationBlocks(buildPayload(BASE), "msg"));
|
|
81
|
+
const actions = c.actions as Array<Record<string, unknown>>;
|
|
82
|
+
expect(actions).toHaveLength(2);
|
|
83
|
+
expect(actions[0].action_id).toBe("apr:req-123:approve_once");
|
|
84
|
+
expect(actions[0].style).toBe("primary");
|
|
85
|
+
expect(actions[1].action_id).toBe("apr:req-123:reject");
|
|
86
|
+
expect(actions[1].style).toBe("danger");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("source context renders a channel mention with permalink", () => {
|
|
90
|
+
const texts = contextTexts(
|
|
91
|
+
buildApprovalNotificationBlocks(buildPayload(BASE), "msg"),
|
|
92
|
+
);
|
|
93
|
+
expect(texts).toContain(
|
|
94
|
+
"Source: Slack — <#C01ABC> · <https://slack.com/archives/C01ABC/p1700000000000100|View message>",
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("DM source renders as Direct message", () => {
|
|
99
|
+
const blocks = buildApprovalNotificationBlocks(
|
|
100
|
+
buildPayload({ ...BASE, conversationExternalId: "D01XYZ" }),
|
|
101
|
+
"msg",
|
|
102
|
+
);
|
|
103
|
+
expect(contextTexts(blocks)).toContain(
|
|
104
|
+
"Source: Slack — Direct message · <https://slack.com/archives/D01XYZ/p1700000000000100|View message>",
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("requester id block appears when external id adds info", () => {
|
|
109
|
+
const texts = contextTexts(
|
|
110
|
+
buildApprovalNotificationBlocks(buildPayload(BASE), "msg"),
|
|
111
|
+
);
|
|
112
|
+
expect(texts).toContain("ID: U999");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("invite directive context block is always present", () => {
|
|
116
|
+
const texts = contextTexts(
|
|
117
|
+
buildApprovalNotificationBlocks(buildPayload(BASE), "msg"),
|
|
118
|
+
);
|
|
119
|
+
expect(texts).toContain(
|
|
120
|
+
'Reply "open invite flow" to start Trusted Contacts invite flow.',
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("warnings render in the card subtext", () => {
|
|
125
|
+
const c = card(
|
|
126
|
+
buildApprovalNotificationBlocks(
|
|
127
|
+
buildPayload({
|
|
128
|
+
...BASE,
|
|
129
|
+
isStranger: true,
|
|
130
|
+
isRestricted: true,
|
|
131
|
+
previousMemberStatus: "revoked",
|
|
132
|
+
}),
|
|
133
|
+
"msg",
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
const subtext = text(c.subtext);
|
|
137
|
+
expect(subtext).toContain(":warning: This user was previously revoked.");
|
|
138
|
+
expect(subtext).toContain(
|
|
139
|
+
":warning: External Slack user (not in this workspace).",
|
|
140
|
+
);
|
|
141
|
+
expect(subtext).toContain(":warning: Guest / restricted account.");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("no subtext when there are no warnings", () => {
|
|
145
|
+
const c = card(buildApprovalNotificationBlocks(buildPayload(BASE), "msg"));
|
|
146
|
+
expect(c.subtext).toBeUndefined();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("body falls back to a default label when no preview", () => {
|
|
150
|
+
const c = card(
|
|
151
|
+
buildApprovalNotificationBlocks(
|
|
152
|
+
buildPayload({ ...BASE, messagePreview: undefined }),
|
|
153
|
+
"msg",
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
expect(text(c.body)).toBe("Requesting access to the assistant");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("guardian verification note appears for fallback guardian resolution", () => {
|
|
160
|
+
const texts = contextTexts(
|
|
161
|
+
buildApprovalNotificationBlocks(
|
|
162
|
+
buildPayload({ ...BASE, guardianResolutionSource: "vellum-anchor" }),
|
|
163
|
+
"msg",
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
expect(
|
|
167
|
+
texts.some((t) => t.includes("haven't verified your identity on slack")),
|
|
168
|
+
).toBe(true);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
2
|
|
|
3
|
-
import { buildToolApprovalSeedContentBlocks } from "../notifications/
|
|
3
|
+
import { buildToolApprovalSeedContentBlocks } from "../notifications/approval-card-data.js";
|
|
4
4
|
|
|
5
5
|
describe("buildToolApprovalSeedContentBlocks", () => {
|
|
6
6
|
const toolApprovalPayload: Record<string, unknown> = {
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
|
|
10
|
-
import { buildApprovalCardBlocks } from "./approval-card-builder.js";
|
|
11
10
|
import {
|
|
12
11
|
nonEmpty,
|
|
13
12
|
sanitizeIdentityField,
|
|
@@ -312,74 +311,88 @@ export function buildAccessRequestContractText(
|
|
|
312
311
|
return lines.join("\n");
|
|
313
312
|
}
|
|
314
313
|
|
|
315
|
-
// ──
|
|
314
|
+
// ── Card view model ─────────────────────────────────────────────────────────
|
|
316
315
|
|
|
317
316
|
/**
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
317
|
+
* Display-ready projection of an access request, shared by every renderer
|
|
318
|
+
* (the Vellum Surface card and the Slack Card block). It carries the
|
|
319
|
+
* sanitized, pre-computed facts each renderer needs — identity sanitizing,
|
|
320
|
+
* warnings, permalink, DM detection, preview sanitizing — so that projection
|
|
321
|
+
* lives in exactly one place. Renderers lay these facts out in their
|
|
322
|
+
* channel-native shape without re-deriving them.
|
|
323
323
|
*/
|
|
324
|
-
export
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
324
|
+
export interface AccessRequestCardView {
|
|
325
|
+
/** Sanitized display name (actorDisplayName ?? senderIdentifier, else "Someone"). */
|
|
326
|
+
displayName: string;
|
|
327
|
+
/** Sanitized username, without the leading `@`. */
|
|
328
|
+
username: string | undefined;
|
|
329
|
+
/** Sanitized external ID. */
|
|
330
|
+
externalId: string | undefined;
|
|
331
|
+
sourceChannel: string | undefined;
|
|
332
|
+
conversationExternalId: string | undefined;
|
|
333
|
+
/** Whether the source Slack conversation is a DM. */
|
|
334
|
+
isSlackDm: boolean;
|
|
335
|
+
/** Slack permalink — present only for a slack source with conversation + ts. */
|
|
336
|
+
messagePermalink: string | undefined;
|
|
337
|
+
/** Sanitized message preview, or undefined when blank after sanitizing. */
|
|
338
|
+
messagePreview: string | undefined;
|
|
339
|
+
/** Human-readable trust/security warnings. */
|
|
340
|
+
warnings: string[];
|
|
341
|
+
guardianResolutionSource: string | undefined;
|
|
342
|
+
requestId: string | undefined;
|
|
343
|
+
}
|
|
328
344
|
|
|
345
|
+
/**
|
|
346
|
+
* Project a parsed access-request payload into display-ready card facts.
|
|
347
|
+
*
|
|
348
|
+
* The payload is parsed once upstream — the broadcaster resolves
|
|
349
|
+
* `accessRequestContext`, and the Surface seed path parses the raw payload —
|
|
350
|
+
* so this takes the parsed payload rather than re-parsing it.
|
|
351
|
+
*/
|
|
352
|
+
export function buildAccessRequestCardView(
|
|
353
|
+
p: ParsedAccessRequestPayload,
|
|
354
|
+
): AccessRequestCardView {
|
|
329
355
|
const rawName = nonEmpty(p.actorDisplayName) ?? nonEmpty(p.senderIdentifier);
|
|
330
356
|
const displayName = rawName ? sanitizeIdentityField(rawName) : "Someone";
|
|
331
357
|
|
|
332
|
-
const
|
|
358
|
+
const rawUsername = nonEmpty(p.actorUsername);
|
|
359
|
+
const username = rawUsername ? sanitizeIdentityField(rawUsername) : undefined;
|
|
333
360
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
});
|
|
339
|
-
}
|
|
361
|
+
const rawExternalId = nonEmpty(p.actorExternalId);
|
|
362
|
+
const externalId = rawExternalId
|
|
363
|
+
? sanitizeIdentityField(rawExternalId)
|
|
364
|
+
: undefined;
|
|
340
365
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
label: "Source",
|
|
345
|
-
value: isDm
|
|
346
|
-
? "Slack — Direct message"
|
|
347
|
-
: `Slack — #${p.conversationExternalId}`,
|
|
348
|
-
});
|
|
349
|
-
} else if (p.sourceChannel) {
|
|
350
|
-
metadata.push({ label: "Source", value: p.sourceChannel });
|
|
351
|
-
}
|
|
366
|
+
const sourceChannel = nonEmpty(p.sourceChannel);
|
|
367
|
+
const conversationExternalId = nonEmpty(p.conversationExternalId);
|
|
368
|
+
const messageTs = nonEmpty(p.messageTs);
|
|
352
369
|
|
|
353
|
-
const
|
|
354
|
-
|
|
370
|
+
const isSlackDm =
|
|
371
|
+
sourceChannel === "slack" && conversationExternalId != null
|
|
372
|
+
? isSlackDmConversation(conversationExternalId)
|
|
373
|
+
: false;
|
|
355
374
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
p.messageTs,
|
|
366
|
-
);
|
|
367
|
-
bodyParts.push(`[View message](${permalink})`);
|
|
368
|
-
}
|
|
375
|
+
const messagePermalink =
|
|
376
|
+
sourceChannel === "slack" && conversationExternalId && messageTs
|
|
377
|
+
? buildSlackMessagePermalink(conversationExternalId, messageTs)
|
|
378
|
+
: undefined;
|
|
379
|
+
|
|
380
|
+
const rawPreview = nonEmpty(p.messagePreview);
|
|
381
|
+
const messagePreview = rawPreview
|
|
382
|
+
? sanitizeMessagePreview(rawPreview) || undefined
|
|
383
|
+
: undefined;
|
|
369
384
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
fallbackText: buildAccessRequestContractText(payload),
|
|
384
|
-
});
|
|
385
|
+
return {
|
|
386
|
+
displayName,
|
|
387
|
+
username,
|
|
388
|
+
externalId,
|
|
389
|
+
sourceChannel,
|
|
390
|
+
conversationExternalId,
|
|
391
|
+
isSlackDm,
|
|
392
|
+
messagePermalink,
|
|
393
|
+
messagePreview,
|
|
394
|
+
warnings: buildAccessRequestWarnings(p),
|
|
395
|
+
guardianResolutionSource: nonEmpty(p.guardianResolutionSource),
|
|
396
|
+
requestId: nonEmpty(p.requestId),
|
|
397
|
+
};
|
|
385
398
|
}
|
|
@@ -16,18 +16,11 @@ import { sendSlackReply } from "../../messaging/providers/slack/send.js";
|
|
|
16
16
|
import type { ApprovalUIMetadata } from "../../runtime/channel-approval-types.js";
|
|
17
17
|
import { getLogger } from "../../util/logger.js";
|
|
18
18
|
import {
|
|
19
|
+
type AccessRequestCardView,
|
|
20
|
+
buildAccessRequestCardView,
|
|
19
21
|
buildAccessRequestInviteDirective,
|
|
20
|
-
buildAccessRequestWarnings,
|
|
21
|
-
buildSlackMessagePermalink,
|
|
22
|
-
isSlackDmConversation,
|
|
23
|
-
type ParsedAccessRequestPayload,
|
|
24
22
|
} from "../access-request-copy.js";
|
|
25
|
-
import {
|
|
26
|
-
nonEmpty,
|
|
27
|
-
sanitizeIdentityField,
|
|
28
|
-
sanitizeMessagePreview,
|
|
29
|
-
truncate,
|
|
30
|
-
} from "../notification-utils.js";
|
|
23
|
+
import { truncate } from "../notification-utils.js";
|
|
31
24
|
import type {
|
|
32
25
|
ChannelAdapter,
|
|
33
26
|
ChannelDeliveryPayload,
|
|
@@ -61,57 +54,48 @@ function buildCardActions(approval: ApprovalUIMetadata): unknown[] {
|
|
|
61
54
|
// ---------------------------------------------------------------------------
|
|
62
55
|
|
|
63
56
|
/** Concise requester identity for the card subtitle (≤150 chars). */
|
|
64
|
-
function buildAccessRequestSubtitle(
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const username = nonEmpty(p.actorUsername);
|
|
70
|
-
if (username) {
|
|
71
|
-
const safe = sanitizeIdentityField(username);
|
|
72
|
-
if (safe !== displayName) parts.push(`(@${safe})`);
|
|
57
|
+
function buildAccessRequestSubtitle(view: AccessRequestCardView): string {
|
|
58
|
+
const parts = [view.displayName];
|
|
59
|
+
|
|
60
|
+
if (view.username && view.username !== view.displayName) {
|
|
61
|
+
parts.push(`(@${view.username})`);
|
|
73
62
|
}
|
|
74
63
|
|
|
75
|
-
if (
|
|
64
|
+
if (view.sourceChannel) parts.push(`via ${view.sourceChannel}`);
|
|
76
65
|
|
|
77
66
|
return truncate(parts.join(" "), 150);
|
|
78
67
|
}
|
|
79
68
|
|
|
80
69
|
/** Card body: message preview when available, otherwise a default label. */
|
|
81
|
-
function buildAccessRequestBody(
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const trimmed = truncate(sanitized, 200 - 6);
|
|
88
|
-
return `> _"${trimmed}"_`;
|
|
89
|
-
}
|
|
70
|
+
function buildAccessRequestBody(view: AccessRequestCardView): string {
|
|
71
|
+
if (view.messagePreview) {
|
|
72
|
+
// Truncate content before wrapping so formatting chars stay balanced.
|
|
73
|
+
// Wrapper `> _"..."_` is 6 chars; reserve space for them.
|
|
74
|
+
const trimmed = truncate(view.messagePreview, 200 - 6);
|
|
75
|
+
return `> _"${trimmed}"_`;
|
|
90
76
|
}
|
|
91
77
|
return "Requesting access to the assistant";
|
|
92
78
|
}
|
|
93
79
|
|
|
94
80
|
/** Source-channel context block with Slack permalink when available. */
|
|
95
81
|
function buildSourceContextBlock(
|
|
96
|
-
|
|
82
|
+
view: AccessRequestCardView,
|
|
97
83
|
): unknown | undefined {
|
|
98
|
-
if (
|
|
84
|
+
if (view.sourceChannel !== "slack" || !view.conversationExternalId) {
|
|
99
85
|
return undefined;
|
|
100
86
|
}
|
|
101
87
|
|
|
102
|
-
const permalink =
|
|
103
|
-
? buildSlackMessagePermalink(p.conversationExternalId, p.messageTs)
|
|
104
|
-
: undefined;
|
|
88
|
+
const permalink = view.messagePermalink;
|
|
105
89
|
|
|
106
90
|
let sourceText: string;
|
|
107
|
-
if (
|
|
91
|
+
if (view.isSlackDm) {
|
|
108
92
|
sourceText = permalink
|
|
109
93
|
? `Source: Slack — Direct message · <${permalink}|View message>`
|
|
110
94
|
: "Source: Slack — Direct message";
|
|
111
95
|
} else {
|
|
112
96
|
sourceText = permalink
|
|
113
|
-
? `Source: Slack — <#${
|
|
114
|
-
: `Source: Slack — <#${
|
|
97
|
+
? `Source: Slack — <#${view.conversationExternalId}> · <${permalink}|View message>`
|
|
98
|
+
: `Source: Slack — <#${view.conversationExternalId}>`;
|
|
115
99
|
}
|
|
116
100
|
|
|
117
101
|
return {
|
|
@@ -122,27 +106,12 @@ function buildSourceContextBlock(
|
|
|
122
106
|
|
|
123
107
|
/** Stable requester identifier context block (external ID when it adds info). */
|
|
124
108
|
function buildRequesterIdBlock(
|
|
125
|
-
|
|
109
|
+
view: AccessRequestCardView,
|
|
126
110
|
): unknown | undefined {
|
|
127
|
-
const safeExternalId =
|
|
128
|
-
p.actorExternalId ? sanitizeIdentityField(p.actorExternalId) : undefined,
|
|
129
|
-
);
|
|
111
|
+
const safeExternalId = view.externalId;
|
|
130
112
|
if (!safeExternalId) return undefined;
|
|
131
113
|
|
|
132
|
-
|
|
133
|
-
p.actorDisplayName
|
|
134
|
-
? sanitizeIdentityField(p.actorDisplayName)
|
|
135
|
-
: p.senderIdentifier
|
|
136
|
-
? sanitizeIdentityField(p.senderIdentifier)
|
|
137
|
-
: undefined,
|
|
138
|
-
);
|
|
139
|
-
const displayedUsername = nonEmpty(
|
|
140
|
-
p.actorUsername ? sanitizeIdentityField(p.actorUsername) : undefined,
|
|
141
|
-
);
|
|
142
|
-
if (
|
|
143
|
-
safeExternalId === displayedName ||
|
|
144
|
-
safeExternalId === displayedUsername
|
|
145
|
-
) {
|
|
114
|
+
if (safeExternalId === view.displayName || safeExternalId === view.username) {
|
|
146
115
|
return undefined;
|
|
147
116
|
}
|
|
148
117
|
|
|
@@ -166,16 +135,15 @@ function buildAccessRequestCardBlocks(
|
|
|
166
135
|
payload: ChannelDeliveryPayload,
|
|
167
136
|
): unknown[] {
|
|
168
137
|
const approval = payload.approvalContext!;
|
|
169
|
-
const
|
|
138
|
+
const view = buildAccessRequestCardView(payload.accessRequestContext!);
|
|
170
139
|
const blocks: unknown[] = [];
|
|
171
140
|
|
|
172
|
-
const subtitle = buildAccessRequestSubtitle(
|
|
173
|
-
const body = buildAccessRequestBody(
|
|
141
|
+
const subtitle = buildAccessRequestSubtitle(view);
|
|
142
|
+
const body = buildAccessRequestBody(view);
|
|
174
143
|
|
|
175
|
-
const warnings = buildAccessRequestWarnings(p);
|
|
176
144
|
const subtext =
|
|
177
|
-
warnings.length > 0
|
|
178
|
-
? truncate(warnings.map((w) => `:warning: ${w}`).join(" · "), 200)
|
|
145
|
+
view.warnings.length > 0
|
|
146
|
+
? truncate(view.warnings.map((w) => `:warning: ${w}`).join(" · "), 200)
|
|
179
147
|
: undefined;
|
|
180
148
|
|
|
181
149
|
const card: Record<string, unknown> = {
|
|
@@ -188,10 +156,10 @@ function buildAccessRequestCardBlocks(
|
|
|
188
156
|
if (subtext) card.subtext = { type: "mrkdwn", text: subtext };
|
|
189
157
|
blocks.push(card);
|
|
190
158
|
|
|
191
|
-
const sourceContext = buildSourceContextBlock(
|
|
159
|
+
const sourceContext = buildSourceContextBlock(view);
|
|
192
160
|
if (sourceContext) blocks.push(sourceContext);
|
|
193
161
|
|
|
194
|
-
const idBlock = buildRequesterIdBlock(
|
|
162
|
+
const idBlock = buildRequesterIdBlock(view);
|
|
195
163
|
if (idBlock) blocks.push(idBlock);
|
|
196
164
|
|
|
197
165
|
blocks.push({
|
|
@@ -200,16 +168,16 @@ function buildAccessRequestCardBlocks(
|
|
|
200
168
|
});
|
|
201
169
|
|
|
202
170
|
if (
|
|
203
|
-
(
|
|
204
|
-
|
|
205
|
-
|
|
171
|
+
(view.guardianResolutionSource === "vellum-anchor" ||
|
|
172
|
+
view.guardianResolutionSource === "none") &&
|
|
173
|
+
view.sourceChannel
|
|
206
174
|
) {
|
|
207
175
|
blocks.push({
|
|
208
176
|
type: "context",
|
|
209
177
|
elements: [
|
|
210
178
|
{
|
|
211
179
|
type: "mrkdwn",
|
|
212
|
-
text: `_You haven't verified your identity on ${
|
|
180
|
+
text: `_You haven't verified your identity on ${view.sourceChannel} yet. If this was you trying to message your assistant, say "help me verify as guardian on ${view.sourceChannel}" to set up direct access._`,
|
|
213
181
|
},
|
|
214
182
|
],
|
|
215
183
|
});
|
|
@@ -280,8 +248,10 @@ function buildToolApprovalCardBlocks(
|
|
|
280
248
|
/**
|
|
281
249
|
* Build Slack blocks for any notification carrying approval context.
|
|
282
250
|
* Dispatches to the appropriate card builder based on source event type.
|
|
251
|
+
*
|
|
252
|
+
* Exported for characterization tests of the approval-card block output.
|
|
283
253
|
*/
|
|
284
|
-
function buildApprovalNotificationBlocks(
|
|
254
|
+
export function buildApprovalNotificationBlocks(
|
|
285
255
|
payload: ChannelDeliveryPayload,
|
|
286
256
|
messageText: string,
|
|
287
257
|
): unknown[] {
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified resolver + renderer for guardian approval card seed content.
|
|
3
|
+
*
|
|
4
|
+
* `contextPayload` resolves into a typed {@link ApprovalCardData} discriminated
|
|
5
|
+
* union, then renders into Surface `[ui_surface, text]` blocks via the shared
|
|
6
|
+
* {@link buildApprovalCardBlocks}. Access-request and tool-approval
|
|
7
|
+
* notifications share this single entry point, which parses and shapes the raw
|
|
8
|
+
* payload into card data.
|
|
9
|
+
*
|
|
10
|
+
* The Surface card data shape matches `CardSurfaceData` from
|
|
11
|
+
* `daemon/message-types/surfaces.ts`. Actions use the canonical
|
|
12
|
+
* `apr:<requestId>:<action>` callback format consumed by
|
|
13
|
+
* `surface-action-routes.ts` → `processGuardianDecision`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
buildAccessRequestCardView,
|
|
18
|
+
buildAccessRequestContractText,
|
|
19
|
+
parseAccessRequestPayload,
|
|
20
|
+
} from "./access-request-copy.js";
|
|
21
|
+
import {
|
|
22
|
+
type ApprovalCardParams,
|
|
23
|
+
buildApprovalCardBlocks,
|
|
24
|
+
} from "./approval-card-builder.js";
|
|
25
|
+
import {
|
|
26
|
+
buildGuardianRequestCodeInstruction,
|
|
27
|
+
type GuardianQuestionPayload,
|
|
28
|
+
type LenientToolApprovalPayload,
|
|
29
|
+
LenientToolApprovalPayloadSchema,
|
|
30
|
+
parseGuardianQuestionPayload,
|
|
31
|
+
resolveGuardianInstructionModeFromFields,
|
|
32
|
+
resolveGuardianInstructionModeFromPayload,
|
|
33
|
+
} from "./guardian-question-mode.js";
|
|
34
|
+
import { nonEmpty, sanitizeIdentityField } from "./notification-utils.js";
|
|
35
|
+
|
|
36
|
+
// ── Typed card data ─────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** Resolved card data for an access-request notification. */
|
|
39
|
+
export interface AccessRequestCardData {
|
|
40
|
+
kind: "access_request";
|
|
41
|
+
card: ApprovalCardParams;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Resolved card data for a tool-approval / tool-grant notification. */
|
|
45
|
+
export interface ToolApprovalCardData {
|
|
46
|
+
kind: "tool_approval";
|
|
47
|
+
card: ApprovalCardParams;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Channel-agnostic approval card content, resolved once from `contextPayload`.
|
|
52
|
+
* The discriminant `kind` lets consumers branch on the approval type without
|
|
53
|
+
* re-parsing the payload.
|
|
54
|
+
*/
|
|
55
|
+
export type ApprovalCardData = AccessRequestCardData | ToolApprovalCardData;
|
|
56
|
+
|
|
57
|
+
// ── Access-request resolution ────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
/** Shape the parsed access-request payload into card params via the view model. */
|
|
60
|
+
function resolveAccessRequestCard(
|
|
61
|
+
payload: Record<string, unknown>,
|
|
62
|
+
): ApprovalCardParams {
|
|
63
|
+
const view = buildAccessRequestCardView(parseAccessRequestPayload(payload));
|
|
64
|
+
|
|
65
|
+
const metadata: Array<{ label: string; value: string }> = [];
|
|
66
|
+
|
|
67
|
+
if (view.username) {
|
|
68
|
+
metadata.push({
|
|
69
|
+
label: "Username",
|
|
70
|
+
value: `@${view.username}`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (view.sourceChannel === "slack" && view.conversationExternalId) {
|
|
75
|
+
metadata.push({
|
|
76
|
+
label: "Source",
|
|
77
|
+
value: view.isSlackDm
|
|
78
|
+
? "Slack — Direct message"
|
|
79
|
+
: `Slack — #${view.conversationExternalId}`,
|
|
80
|
+
});
|
|
81
|
+
} else if (view.sourceChannel) {
|
|
82
|
+
metadata.push({ label: "Source", value: view.sourceChannel });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const bodyParts: string[] = [];
|
|
86
|
+
|
|
87
|
+
if (view.messagePreview) {
|
|
88
|
+
bodyParts.push(`> "${view.messagePreview}"`);
|
|
89
|
+
}
|
|
90
|
+
for (const w of view.warnings) {
|
|
91
|
+
bodyParts.push(`⚠️ ${w}`);
|
|
92
|
+
}
|
|
93
|
+
if (view.messagePermalink) {
|
|
94
|
+
bodyParts.push(`[View message](${view.messagePermalink})`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const body =
|
|
98
|
+
bodyParts.length > 0
|
|
99
|
+
? bodyParts.join("\n\n")
|
|
100
|
+
: "No additional context available.";
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
surfaceIdPrefix: "access-request",
|
|
104
|
+
cardTitle: "Access Request",
|
|
105
|
+
requesterName: view.displayName,
|
|
106
|
+
subtitle: "Requesting access to the assistant",
|
|
107
|
+
body,
|
|
108
|
+
metadata,
|
|
109
|
+
requestId: view.requestId,
|
|
110
|
+
fallbackText: buildAccessRequestContractText(payload),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── Tool-approval resolution ─────────────────────────────────────────────────
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Determine whether a typed guardian.question payload represents a tool
|
|
118
|
+
* approval (as opposed to a free-text answer). Uses the canonical mode
|
|
119
|
+
* resolver in `guardian-question-mode.ts` — the single source of truth
|
|
120
|
+
* for the requestKind → instructionMode mapping.
|
|
121
|
+
*/
|
|
122
|
+
function isToolApprovalPayload(payload: GuardianQuestionPayload): boolean {
|
|
123
|
+
const { mode } = resolveGuardianInstructionModeFromPayload(payload);
|
|
124
|
+
return mode === "approval" && payload.requestKind !== "access_request";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Lenient approval detection for partially-constructed payloads that don't
|
|
129
|
+
* satisfy the strict discriminated union schema.
|
|
130
|
+
*/
|
|
131
|
+
function isLenientToolApproval(payload: LenientToolApprovalPayload): boolean {
|
|
132
|
+
const modeResolution = resolveGuardianInstructionModeFromFields(
|
|
133
|
+
payload.requestKind,
|
|
134
|
+
payload.toolName,
|
|
135
|
+
);
|
|
136
|
+
if (!modeResolution) return false;
|
|
137
|
+
return (
|
|
138
|
+
modeResolution.mode === "approval" &&
|
|
139
|
+
payload.requestKind !== "access_request"
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Shape a tool-approval/grant payload (strict or lenient) into card params. */
|
|
144
|
+
function extractToolApprovalCard(
|
|
145
|
+
p: GuardianQuestionPayload | LenientToolApprovalPayload,
|
|
146
|
+
): ApprovalCardParams {
|
|
147
|
+
const toolName =
|
|
148
|
+
("toolName" in p ? nonEmpty(p.toolName) : undefined) ?? "unknown tool";
|
|
149
|
+
const rawRequester = nonEmpty(p.requesterIdentifier);
|
|
150
|
+
const requester = rawRequester
|
|
151
|
+
? sanitizeIdentityField(rawRequester)
|
|
152
|
+
: "Someone";
|
|
153
|
+
|
|
154
|
+
const isGrant = p.requestKind === "tool_grant_request";
|
|
155
|
+
|
|
156
|
+
const metadata: Array<{ label: string; value: string }> = [];
|
|
157
|
+
metadata.push({ label: "Tool", value: toolName });
|
|
158
|
+
const sourceChannel = nonEmpty(p.sourceChannel);
|
|
159
|
+
if (sourceChannel) {
|
|
160
|
+
metadata.push({ label: "Source", value: sourceChannel });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const body = p.questionText
|
|
164
|
+
? `> ${p.questionText}`
|
|
165
|
+
: "No additional context available.";
|
|
166
|
+
|
|
167
|
+
// Fallback text with request-code instructions for older clients.
|
|
168
|
+
const baseFallback =
|
|
169
|
+
p.questionText ?? `${requester} is requesting approval to use ${toolName}`;
|
|
170
|
+
let fallbackText = baseFallback;
|
|
171
|
+
const requestCode = nonEmpty(p.requestCode);
|
|
172
|
+
if (requestCode) {
|
|
173
|
+
const modeResolution = resolveGuardianInstructionModeFromFields(
|
|
174
|
+
p.requestKind,
|
|
175
|
+
"toolName" in p ? (p.toolName ?? undefined) : undefined,
|
|
176
|
+
);
|
|
177
|
+
const mode = modeResolution?.mode ?? "approval";
|
|
178
|
+
const instruction = buildGuardianRequestCodeInstruction(
|
|
179
|
+
requestCode.trim().toUpperCase(),
|
|
180
|
+
mode,
|
|
181
|
+
);
|
|
182
|
+
fallbackText = `${baseFallback}\n\n${instruction}`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
surfaceIdPrefix: "tool-approval",
|
|
187
|
+
cardTitle: isGrant ? "Tool Grant Request" : "Tool Approval",
|
|
188
|
+
requesterName: requester,
|
|
189
|
+
subtitle: isGrant
|
|
190
|
+
? "Requesting permission to use this tool"
|
|
191
|
+
: "Requesting approval to run this tool",
|
|
192
|
+
body,
|
|
193
|
+
metadata,
|
|
194
|
+
requestId: nonEmpty(p.requestId),
|
|
195
|
+
fallbackText,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Resolve a guardian.question payload into tool-approval card params, or
|
|
201
|
+
* `null` when it does not represent a tool approval.
|
|
202
|
+
*
|
|
203
|
+
* Tries strict Zod parsing first (full discriminated union), then falls back
|
|
204
|
+
* to lenient field extraction so cards still render when optional fields are
|
|
205
|
+
* absent.
|
|
206
|
+
*/
|
|
207
|
+
function resolveToolApprovalCard(
|
|
208
|
+
payload: Record<string, unknown>,
|
|
209
|
+
): ApprovalCardParams | null {
|
|
210
|
+
const strict = parseGuardianQuestionPayload(payload);
|
|
211
|
+
if (strict) {
|
|
212
|
+
if (!isToolApprovalPayload(strict)) return null;
|
|
213
|
+
return extractToolApprovalCard(strict);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const lenient = LenientToolApprovalPayloadSchema.safeParse(payload);
|
|
217
|
+
if (!lenient.success) return null;
|
|
218
|
+
if (!isLenientToolApproval(lenient.data)) return null;
|
|
219
|
+
return extractToolApprovalCard(lenient.data);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ── Unified dispatcher + renderer ────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Resolve a notification's `contextPayload` into typed {@link ApprovalCardData}
|
|
226
|
+
* based on its source event, or `null` when the signal does not carry a
|
|
227
|
+
* renderable approval card. Single entry point so each consumer (copy
|
|
228
|
+
* composition, decision engine) shapes the payload identically.
|
|
229
|
+
*/
|
|
230
|
+
export function resolveApprovalCardData(
|
|
231
|
+
sourceEventName: string,
|
|
232
|
+
contextPayload: Record<string, unknown> | undefined,
|
|
233
|
+
): ApprovalCardData | null {
|
|
234
|
+
if (!contextPayload) return null;
|
|
235
|
+
|
|
236
|
+
if (sourceEventName === "ingress.access_request") {
|
|
237
|
+
return {
|
|
238
|
+
kind: "access_request",
|
|
239
|
+
card: resolveAccessRequestCard(contextPayload),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (sourceEventName === "guardian.question") {
|
|
244
|
+
const card = resolveToolApprovalCard(contextPayload);
|
|
245
|
+
return card ? { kind: "tool_approval", card } : null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Render resolved approval card data into Surface `[ui_surface, text]` blocks. */
|
|
252
|
+
export function renderApprovalCardData(data: ApprovalCardData): unknown[] {
|
|
253
|
+
return buildApprovalCardBlocks(data.card);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ── Public seed-content builders ─────────────────────────────────────────────
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Build structured content blocks for an access-request notification seed
|
|
260
|
+
* message. Produces a `ui_surface` card block that the web/macOS/iOS apps
|
|
261
|
+
* render as an interactive card via `SurfaceRouter → CardSurface`, plus a
|
|
262
|
+
* plain-text fallback block for search, CLI display, and backward-compatible
|
|
263
|
+
* clients that don't support surfaces.
|
|
264
|
+
*/
|
|
265
|
+
export function buildAccessRequestSeedContentBlocks(
|
|
266
|
+
payload: Record<string, unknown>,
|
|
267
|
+
): unknown[] {
|
|
268
|
+
return renderApprovalCardData({
|
|
269
|
+
kind: "access_request",
|
|
270
|
+
card: resolveAccessRequestCard(payload),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Build structured content blocks for a tool approval/grant notification seed
|
|
276
|
+
* message. Returns `null` when the payload does not represent a tool approval.
|
|
277
|
+
*
|
|
278
|
+
* Accepts both strict `GuardianQuestionPayload` (Zod-validated) and raw
|
|
279
|
+
* `Record<string, unknown>` payloads. For raw payloads, attempts strict
|
|
280
|
+
* parsing first, then falls back to lenient field extraction so cards
|
|
281
|
+
* still render when optional fields are absent.
|
|
282
|
+
*/
|
|
283
|
+
export function buildToolApprovalSeedContentBlocks(
|
|
284
|
+
payload: GuardianQuestionPayload,
|
|
285
|
+
): unknown[] | null;
|
|
286
|
+
export function buildToolApprovalSeedContentBlocks(
|
|
287
|
+
payload: Record<string, unknown>,
|
|
288
|
+
): unknown[] | null;
|
|
289
|
+
export function buildToolApprovalSeedContentBlocks(
|
|
290
|
+
payload: GuardianQuestionPayload | Record<string, unknown>,
|
|
291
|
+
): unknown[] | null {
|
|
292
|
+
const data = resolveApprovalCardData(
|
|
293
|
+
"guardian.question",
|
|
294
|
+
payload as Record<string, unknown>,
|
|
295
|
+
);
|
|
296
|
+
return data ? renderApprovalCardData(data) : null;
|
|
297
|
+
}
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
* values from the context payload.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import { buildAccessRequestContractText } from "./access-request-copy.js";
|
|
12
13
|
import {
|
|
13
|
-
buildAccessRequestContractText,
|
|
14
14
|
buildAccessRequestSeedContentBlocks,
|
|
15
|
-
|
|
15
|
+
buildToolApprovalSeedContentBlocks,
|
|
16
|
+
} from "./approval-card-data.js";
|
|
16
17
|
import {
|
|
17
18
|
buildGuardianRequestCodeInstruction,
|
|
18
19
|
parseGuardianQuestionPayload,
|
|
@@ -28,7 +29,6 @@ import type {
|
|
|
28
29
|
NotificationSignal,
|
|
29
30
|
NotificationSourceEventName,
|
|
30
31
|
} from "./signal.js";
|
|
31
|
-
import { buildToolApprovalSeedContentBlocks } from "./tool-approval-copy.js";
|
|
32
32
|
import type { NotificationChannel, RenderedChannelCopy } from "./types.js";
|
|
33
33
|
|
|
34
34
|
type CopyTemplate = (payload: Record<string, unknown>) => RenderedChannelCopy;
|
|
@@ -26,10 +26,13 @@ import { truncate } from "../util/truncate.js";
|
|
|
26
26
|
import {
|
|
27
27
|
buildAccessRequestContractText,
|
|
28
28
|
buildAccessRequestInviteDirective,
|
|
29
|
-
buildAccessRequestSeedContentBlocks,
|
|
30
29
|
hasAccessRequestInstructions,
|
|
31
30
|
hasInviteFlowDirective,
|
|
32
31
|
} from "./access-request-copy.js";
|
|
32
|
+
import {
|
|
33
|
+
buildAccessRequestSeedContentBlocks,
|
|
34
|
+
buildToolApprovalSeedContentBlocks,
|
|
35
|
+
} from "./approval-card-data.js";
|
|
33
36
|
import {
|
|
34
37
|
buildConversationCandidates,
|
|
35
38
|
type ConversationCandidateSet,
|
|
@@ -46,7 +49,6 @@ import {
|
|
|
46
49
|
import { nonEmpty, readPayloadString } from "./notification-utils.js";
|
|
47
50
|
import { getPreferenceSummary } from "./preference-summary.js";
|
|
48
51
|
import type { NotificationSignal, RoutingIntent } from "./signal.js";
|
|
49
|
-
import { buildToolApprovalSeedContentBlocks } from "./tool-approval-copy.js";
|
|
50
52
|
import type {
|
|
51
53
|
ConversationAction,
|
|
52
54
|
NotificationChannel,
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Deterministic helpers for building guardian-facing tool-approval copy.
|
|
3
|
-
*
|
|
4
|
-
* Produces Surface card seed content blocks for `tool_approval`,
|
|
5
|
-
* `tool_grant_request`, and voice/call `pending_question` (with `toolName`)
|
|
6
|
-
* guardian questions, enabling Approve/Reject buttons in the Vellum in-app
|
|
7
|
-
* channel (web/macOS/iOS).
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { buildApprovalCardBlocks } from "./approval-card-builder.js";
|
|
11
|
-
import {
|
|
12
|
-
buildGuardianRequestCodeInstruction,
|
|
13
|
-
type GuardianQuestionPayload,
|
|
14
|
-
type LenientToolApprovalPayload,
|
|
15
|
-
LenientToolApprovalPayloadSchema,
|
|
16
|
-
parseGuardianQuestionPayload,
|
|
17
|
-
resolveGuardianInstructionModeFromFields,
|
|
18
|
-
resolveGuardianInstructionModeFromPayload,
|
|
19
|
-
} from "./guardian-question-mode.js";
|
|
20
|
-
import { nonEmpty, sanitizeIdentityField } from "./notification-utils.js";
|
|
21
|
-
|
|
22
|
-
// ── Approval detection ──────────────────────────────────────────────────────
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Determine whether a typed guardian.question payload represents a tool
|
|
26
|
-
* approval (as opposed to a free-text answer). Uses the canonical mode
|
|
27
|
-
* resolver in `guardian-question-mode.ts` — the single source of truth
|
|
28
|
-
* for the requestKind → instructionMode mapping.
|
|
29
|
-
*/
|
|
30
|
-
function isToolApprovalPayload(payload: GuardianQuestionPayload): boolean {
|
|
31
|
-
const { mode } = resolveGuardianInstructionModeFromPayload(payload);
|
|
32
|
-
return mode === "approval" && payload.requestKind !== "access_request";
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Lenient approval detection for partially-constructed payloads that don't
|
|
37
|
-
* satisfy the strict discriminated union schema.
|
|
38
|
-
*/
|
|
39
|
-
function isLenientToolApproval(payload: LenientToolApprovalPayload): boolean {
|
|
40
|
-
const modeResolution = resolveGuardianInstructionModeFromFields(
|
|
41
|
-
payload.requestKind,
|
|
42
|
-
payload.toolName,
|
|
43
|
-
);
|
|
44
|
-
if (!modeResolution) return false;
|
|
45
|
-
return (
|
|
46
|
-
modeResolution.mode === "approval" &&
|
|
47
|
-
payload.requestKind !== "access_request"
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// ── Seed content blocks (Surface-based rendering) ───────────────────────────
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Build structured content blocks for a tool approval/grant notification seed
|
|
55
|
-
* message. Returns `null` when the payload does not represent a tool approval.
|
|
56
|
-
*
|
|
57
|
-
* Accepts both strict `GuardianQuestionPayload` (Zod-validated) and raw
|
|
58
|
-
* `Record<string, unknown>` payloads. For raw payloads, attempts strict
|
|
59
|
-
* parsing first, then falls back to lenient field extraction so cards
|
|
60
|
-
* still render when optional fields are absent.
|
|
61
|
-
*/
|
|
62
|
-
export function buildToolApprovalSeedContentBlocks(
|
|
63
|
-
payload: GuardianQuestionPayload,
|
|
64
|
-
): unknown[] | null;
|
|
65
|
-
export function buildToolApprovalSeedContentBlocks(
|
|
66
|
-
payload: Record<string, unknown>,
|
|
67
|
-
): unknown[] | null;
|
|
68
|
-
export function buildToolApprovalSeedContentBlocks(
|
|
69
|
-
payload: GuardianQuestionPayload | Record<string, unknown>,
|
|
70
|
-
): unknown[] | null {
|
|
71
|
-
// Try strict Zod parsing first (full discriminated union).
|
|
72
|
-
const strict = parseGuardianQuestionPayload(
|
|
73
|
-
payload as Record<string, unknown>,
|
|
74
|
-
);
|
|
75
|
-
if (strict) {
|
|
76
|
-
if (!isToolApprovalPayload(strict)) return null;
|
|
77
|
-
return buildCardFromFields(strict);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Fall back to lenient parsing — requires only `requestKind`.
|
|
81
|
-
const lenient = LenientToolApprovalPayloadSchema.safeParse(payload);
|
|
82
|
-
if (!lenient.success) return null;
|
|
83
|
-
if (!isLenientToolApproval(lenient.data)) return null;
|
|
84
|
-
return buildCardFromFields(lenient.data);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// ── Card construction (shared between strict and lenient paths) ─────────────
|
|
88
|
-
|
|
89
|
-
function buildCardFromFields(
|
|
90
|
-
p: GuardianQuestionPayload | LenientToolApprovalPayload,
|
|
91
|
-
): unknown[] {
|
|
92
|
-
const toolName =
|
|
93
|
-
("toolName" in p ? nonEmpty(p.toolName) : undefined) ?? "unknown tool";
|
|
94
|
-
const rawRequester = nonEmpty(p.requesterIdentifier);
|
|
95
|
-
const requester = rawRequester
|
|
96
|
-
? sanitizeIdentityField(rawRequester)
|
|
97
|
-
: "Someone";
|
|
98
|
-
|
|
99
|
-
const isGrant = p.requestKind === "tool_grant_request";
|
|
100
|
-
|
|
101
|
-
const metadata: Array<{ label: string; value: string }> = [];
|
|
102
|
-
metadata.push({ label: "Tool", value: toolName });
|
|
103
|
-
const sourceChannel = nonEmpty(p.sourceChannel);
|
|
104
|
-
if (sourceChannel) {
|
|
105
|
-
metadata.push({ label: "Source", value: sourceChannel });
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const body = p.questionText
|
|
109
|
-
? `> ${p.questionText}`
|
|
110
|
-
: "No additional context available.";
|
|
111
|
-
|
|
112
|
-
// Fallback text with request-code instructions for older clients.
|
|
113
|
-
const baseFallback =
|
|
114
|
-
p.questionText ?? `${requester} is requesting approval to use ${toolName}`;
|
|
115
|
-
let fallbackText = baseFallback;
|
|
116
|
-
const requestCode = nonEmpty(p.requestCode);
|
|
117
|
-
if (requestCode) {
|
|
118
|
-
const modeResolution = resolveGuardianInstructionModeFromFields(
|
|
119
|
-
p.requestKind,
|
|
120
|
-
"toolName" in p ? (p.toolName ?? undefined) : undefined,
|
|
121
|
-
);
|
|
122
|
-
const mode = modeResolution?.mode ?? "approval";
|
|
123
|
-
const instruction = buildGuardianRequestCodeInstruction(
|
|
124
|
-
requestCode.trim().toUpperCase(),
|
|
125
|
-
mode,
|
|
126
|
-
);
|
|
127
|
-
fallbackText = `${baseFallback}\n\n${instruction}`;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return buildApprovalCardBlocks({
|
|
131
|
-
surfaceIdPrefix: "tool-approval",
|
|
132
|
-
cardTitle: isGrant ? "Tool Grant Request" : "Tool Approval",
|
|
133
|
-
requesterName: requester,
|
|
134
|
-
subtitle: isGrant
|
|
135
|
-
? "Requesting permission to use this tool"
|
|
136
|
-
: "Requesting approval to run this tool",
|
|
137
|
-
body,
|
|
138
|
-
metadata,
|
|
139
|
-
requestId: nonEmpty(p.requestId),
|
|
140
|
-
fallbackText,
|
|
141
|
-
});
|
|
142
|
-
}
|