@vellumai/assistant 0.10.6-dev.202607070525.69af9c2 → 0.10.6-dev.202607070801.a5f625d
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/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/package.json +1 -0
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/client-metadata.ts +46 -0
- package/node_modules/@vellumai/ces-client/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/package.json +1 -0
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/client-metadata.ts +46 -0
- package/node_modules/@vellumai/gateway-client/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
- package/node_modules/@vellumai/service-contracts/package.json +1 -0
- package/node_modules/@vellumai/service-contracts/src/client-metadata.ts +46 -0
- package/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
- package/package.json +1 -1
- package/src/__tests__/client-os-metadata-persistence.test.ts +14 -0
- package/src/__tests__/http-user-message-parity.test.ts +154 -2
- package/src/config/feature-flag-registry.json +8 -0
- package/src/daemon/conversation-messaging.ts +18 -8
- package/src/persistence/conversation-crud.ts +7 -4
- package/src/runtime/routes/conversation-routes.ts +71 -15
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-metadata analytics headers.
|
|
3
|
+
*
|
|
4
|
+
* The web client attaches these to every daemon request via
|
|
5
|
+
* `getClientRegistrationHeaders()` (`clients/web/src/lib/telemetry/
|
|
6
|
+
* client-identity.ts`), the gateway allows them through webview CORS
|
|
7
|
+
* (`gateway/src/http/middleware/cors.ts`) and forwards them to the daemon
|
|
8
|
+
* by `x-vellum-*` prefix, and the daemon persists them under
|
|
9
|
+
* `metadata.client` on user messages (`handleSendMessage`), where
|
|
10
|
+
* `turn-events-store` projects them onto `TurnTelemetryEvent.client` for
|
|
11
|
+
* downstream analytics.
|
|
12
|
+
*
|
|
13
|
+
* Keys are the persisted `metadata.client` field names; values are the
|
|
14
|
+
* lowercase HTTP header names (HTTP header matching is case-insensitive).
|
|
15
|
+
*/
|
|
16
|
+
export const CLIENT_METADATA_HEADERS = {
|
|
17
|
+
browser_family: "x-vellum-browser-family",
|
|
18
|
+
browser_version: "x-vellum-browser-version",
|
|
19
|
+
os: "x-vellum-client-os",
|
|
20
|
+
interface_version: "x-vellum-interface-version",
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
export type ClientMetadataField = keyof typeof CLIENT_METADATA_HEADERS;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Bound on client-metadata header values: lowercase alphanumerics plus
|
|
27
|
+
* dot / underscore / dash, at most 64 chars. Enforced identically by the
|
|
28
|
+
* sender (web client) and the reader (daemon) so malformed or oversized
|
|
29
|
+
* values are dropped rather than transmitted or persisted.
|
|
30
|
+
*/
|
|
31
|
+
const CLIENT_METADATA_VALUE_RE = /^[a-z0-9._-]{1,64}$/;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Normalize a candidate client-metadata value (trim + lowercase) and
|
|
35
|
+
* validate it against {@link CLIENT_METADATA_VALUE_RE}. Returns `undefined`
|
|
36
|
+
* for empty or out-of-bounds values.
|
|
37
|
+
*/
|
|
38
|
+
export function sanitizeClientMetadataValue(
|
|
39
|
+
value: string | undefined | null,
|
|
40
|
+
): string | undefined {
|
|
41
|
+
const normalized = value?.trim().toLowerCase();
|
|
42
|
+
if (!normalized) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return CLIENT_METADATA_VALUE_RE.test(normalized) ? normalized : undefined;
|
|
46
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-metadata analytics headers.
|
|
3
|
+
*
|
|
4
|
+
* The web client attaches these to every daemon request via
|
|
5
|
+
* `getClientRegistrationHeaders()` (`clients/web/src/lib/telemetry/
|
|
6
|
+
* client-identity.ts`), the gateway allows them through webview CORS
|
|
7
|
+
* (`gateway/src/http/middleware/cors.ts`) and forwards them to the daemon
|
|
8
|
+
* by `x-vellum-*` prefix, and the daemon persists them under
|
|
9
|
+
* `metadata.client` on user messages (`handleSendMessage`), where
|
|
10
|
+
* `turn-events-store` projects them onto `TurnTelemetryEvent.client` for
|
|
11
|
+
* downstream analytics.
|
|
12
|
+
*
|
|
13
|
+
* Keys are the persisted `metadata.client` field names; values are the
|
|
14
|
+
* lowercase HTTP header names (HTTP header matching is case-insensitive).
|
|
15
|
+
*/
|
|
16
|
+
export const CLIENT_METADATA_HEADERS = {
|
|
17
|
+
browser_family: "x-vellum-browser-family",
|
|
18
|
+
browser_version: "x-vellum-browser-version",
|
|
19
|
+
os: "x-vellum-client-os",
|
|
20
|
+
interface_version: "x-vellum-interface-version",
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
export type ClientMetadataField = keyof typeof CLIENT_METADATA_HEADERS;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Bound on client-metadata header values: lowercase alphanumerics plus
|
|
27
|
+
* dot / underscore / dash, at most 64 chars. Enforced identically by the
|
|
28
|
+
* sender (web client) and the reader (daemon) so malformed or oversized
|
|
29
|
+
* values are dropped rather than transmitted or persisted.
|
|
30
|
+
*/
|
|
31
|
+
const CLIENT_METADATA_VALUE_RE = /^[a-z0-9._-]{1,64}$/;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Normalize a candidate client-metadata value (trim + lowercase) and
|
|
35
|
+
* validate it against {@link CLIENT_METADATA_VALUE_RE}. Returns `undefined`
|
|
36
|
+
* for empty or out-of-bounds values.
|
|
37
|
+
*/
|
|
38
|
+
export function sanitizeClientMetadataValue(
|
|
39
|
+
value: string | undefined | null,
|
|
40
|
+
): string | undefined {
|
|
41
|
+
const normalized = value?.trim().toLowerCase();
|
|
42
|
+
if (!normalized) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return CLIENT_METADATA_VALUE_RE.test(normalized) ? normalized : undefined;
|
|
46
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-metadata analytics headers.
|
|
3
|
+
*
|
|
4
|
+
* The web client attaches these to every daemon request via
|
|
5
|
+
* `getClientRegistrationHeaders()` (`clients/web/src/lib/telemetry/
|
|
6
|
+
* client-identity.ts`), the gateway allows them through webview CORS
|
|
7
|
+
* (`gateway/src/http/middleware/cors.ts`) and forwards them to the daemon
|
|
8
|
+
* by `x-vellum-*` prefix, and the daemon persists them under
|
|
9
|
+
* `metadata.client` on user messages (`handleSendMessage`), where
|
|
10
|
+
* `turn-events-store` projects them onto `TurnTelemetryEvent.client` for
|
|
11
|
+
* downstream analytics.
|
|
12
|
+
*
|
|
13
|
+
* Keys are the persisted `metadata.client` field names; values are the
|
|
14
|
+
* lowercase HTTP header names (HTTP header matching is case-insensitive).
|
|
15
|
+
*/
|
|
16
|
+
export const CLIENT_METADATA_HEADERS = {
|
|
17
|
+
browser_family: "x-vellum-browser-family",
|
|
18
|
+
browser_version: "x-vellum-browser-version",
|
|
19
|
+
os: "x-vellum-client-os",
|
|
20
|
+
interface_version: "x-vellum-interface-version",
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
export type ClientMetadataField = keyof typeof CLIENT_METADATA_HEADERS;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Bound on client-metadata header values: lowercase alphanumerics plus
|
|
27
|
+
* dot / underscore / dash, at most 64 chars. Enforced identically by the
|
|
28
|
+
* sender (web client) and the reader (daemon) so malformed or oversized
|
|
29
|
+
* values are dropped rather than transmitted or persisted.
|
|
30
|
+
*/
|
|
31
|
+
const CLIENT_METADATA_VALUE_RE = /^[a-z0-9._-]{1,64}$/;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Normalize a candidate client-metadata value (trim + lowercase) and
|
|
35
|
+
* validate it against {@link CLIENT_METADATA_VALUE_RE}. Returns `undefined`
|
|
36
|
+
* for empty or out-of-bounds values.
|
|
37
|
+
*/
|
|
38
|
+
export function sanitizeClientMetadataValue(
|
|
39
|
+
value: string | undefined | null,
|
|
40
|
+
): string | undefined {
|
|
41
|
+
const normalized = value?.trim().toLowerCase();
|
|
42
|
+
if (!normalized) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
return CLIENT_METADATA_VALUE_RE.test(normalized) ? normalized : undefined;
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -163,4 +163,18 @@ describe("client OS surface metadata persistence", () => {
|
|
|
163
163
|
interface_version: "1.2.3",
|
|
164
164
|
});
|
|
165
165
|
});
|
|
166
|
+
|
|
167
|
+
test("transport os fills in when the caller bag omits it", async () => {
|
|
168
|
+
const ctx = createWebTurnContext("ios");
|
|
169
|
+
await persistQueuedMessageBody(ctx, {
|
|
170
|
+
content: "hello",
|
|
171
|
+
requestId: "req-merge",
|
|
172
|
+
metadata: { client: { browser_family: "safari" } },
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(lastUserMetadata().client).toEqual({
|
|
176
|
+
os: "ios",
|
|
177
|
+
browser_family: "safari",
|
|
178
|
+
});
|
|
179
|
+
});
|
|
166
180
|
});
|
|
@@ -304,13 +304,18 @@ function makeConversation(overrides: Record<string, unknown> = {}) {
|
|
|
304
304
|
}
|
|
305
305
|
|
|
306
306
|
// ── Helper: create an HTTP request to POST /v1/messages ────────────────────
|
|
307
|
-
function makeRequest(
|
|
307
|
+
function makeRequest(
|
|
308
|
+
content: string,
|
|
309
|
+
extra: Record<string, unknown> = {},
|
|
310
|
+
headers: Record<string, string> = {},
|
|
311
|
+
) {
|
|
308
312
|
return new Request("http://localhost/v1/messages", {
|
|
309
313
|
method: "POST",
|
|
310
314
|
headers: {
|
|
311
315
|
"Content-Type": "application/json",
|
|
312
316
|
"x-vellum-actor-principal-id": "test-user",
|
|
313
317
|
"x-vellum-principal-type": "actor",
|
|
318
|
+
...headers,
|
|
314
319
|
},
|
|
315
320
|
body: JSON.stringify({
|
|
316
321
|
conversationKey: "parity-test-key",
|
|
@@ -332,6 +337,7 @@ async function sendMessage(
|
|
|
332
337
|
conversationId: string,
|
|
333
338
|
opts?: Record<string, unknown>,
|
|
334
339
|
) => void;
|
|
340
|
+
headers?: Record<string, string>;
|
|
335
341
|
} = {},
|
|
336
342
|
) {
|
|
337
343
|
return callHandler(
|
|
@@ -349,7 +355,7 @@ async function sendMessage(
|
|
|
349
355
|
resolveAttachments: () => [],
|
|
350
356
|
},
|
|
351
357
|
}),
|
|
352
|
-
makeRequest(content, extra),
|
|
358
|
+
makeRequest(content, extra, options.headers ?? {}),
|
|
353
359
|
undefined,
|
|
354
360
|
202,
|
|
355
361
|
);
|
|
@@ -526,6 +532,152 @@ describe("HTTP POST /v1/messages clientTimezone transport metadata", () => {
|
|
|
526
532
|
});
|
|
527
533
|
});
|
|
528
534
|
|
|
535
|
+
// ============================================================================
|
|
536
|
+
// CLIENT METADATA — sanitized x-vellum-* headers persisted under
|
|
537
|
+
// metadata.client for turn analytics
|
|
538
|
+
// ============================================================================
|
|
539
|
+
describe("HTTP POST /v1/messages client metadata headers", () => {
|
|
540
|
+
beforeEach(() => {
|
|
541
|
+
routeGuardianReplyMock.mockClear();
|
|
542
|
+
listPendingByDestinationMock.mockClear();
|
|
543
|
+
listCanonicalMock.mockClear();
|
|
544
|
+
addMessageMock.mockClear();
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
const clientMetadataHeaders = {
|
|
548
|
+
"x-vellum-browser-family": "safari",
|
|
549
|
+
"x-vellum-browser-version": "17",
|
|
550
|
+
"x-vellum-client-os": "ios",
|
|
551
|
+
"x-vellum-interface-version": "1.2.3",
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
test("persists client metadata on immediate user messages", async () => {
|
|
555
|
+
const persistUserMessage = mock(
|
|
556
|
+
async (_options: { metadata?: Record<string, unknown> }) => ({
|
|
557
|
+
id: "persisted-msg-id",
|
|
558
|
+
deduplicated: false,
|
|
559
|
+
}),
|
|
560
|
+
);
|
|
561
|
+
const runAgentLoop = mock(async () => undefined);
|
|
562
|
+
const conversation = makeConversation({ persistUserMessage, runAgentLoop });
|
|
563
|
+
|
|
564
|
+
const res = await sendMessage(
|
|
565
|
+
"hello",
|
|
566
|
+
conversation,
|
|
567
|
+
{},
|
|
568
|
+
{
|
|
569
|
+
headers: clientMetadataHeaders,
|
|
570
|
+
},
|
|
571
|
+
);
|
|
572
|
+
|
|
573
|
+
expect(res.status).toBe(202);
|
|
574
|
+
expect(persistUserMessage).toHaveBeenCalledTimes(1);
|
|
575
|
+
const persistCall = persistUserMessage.mock.calls[0];
|
|
576
|
+
expect(persistCall).toBeDefined();
|
|
577
|
+
const [persistOptions] = persistCall as unknown as [
|
|
578
|
+
{ metadata?: Record<string, unknown> },
|
|
579
|
+
];
|
|
580
|
+
expect(persistOptions.metadata).toEqual({
|
|
581
|
+
client: {
|
|
582
|
+
browser_family: "safari",
|
|
583
|
+
browser_version: "17",
|
|
584
|
+
os: "ios",
|
|
585
|
+
interface_version: "1.2.3",
|
|
586
|
+
},
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
test("persists client metadata on queued user messages", async () => {
|
|
591
|
+
const enqueueMessage = mock(
|
|
592
|
+
(_options: { metadata?: Record<string, unknown> }) => ({
|
|
593
|
+
queued: true,
|
|
594
|
+
requestId: "queued-id",
|
|
595
|
+
}),
|
|
596
|
+
);
|
|
597
|
+
const conversation = makeConversation({
|
|
598
|
+
isProcessing: () => true,
|
|
599
|
+
enqueueMessage,
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
const res = await sendMessage(
|
|
603
|
+
"hello",
|
|
604
|
+
conversation,
|
|
605
|
+
{},
|
|
606
|
+
{
|
|
607
|
+
headers: clientMetadataHeaders,
|
|
608
|
+
},
|
|
609
|
+
);
|
|
610
|
+
|
|
611
|
+
expect(res.status).toBe(202);
|
|
612
|
+
expect(enqueueMessage).toHaveBeenCalledTimes(1);
|
|
613
|
+
const enqueueCall = enqueueMessage.mock.calls[0];
|
|
614
|
+
expect(enqueueCall).toBeDefined();
|
|
615
|
+
const [enqueueOptions] = enqueueCall as unknown as [
|
|
616
|
+
{ metadata?: Record<string, unknown> },
|
|
617
|
+
];
|
|
618
|
+
expect(enqueueOptions.metadata).toMatchObject({
|
|
619
|
+
client: {
|
|
620
|
+
browser_family: "safari",
|
|
621
|
+
browser_version: "17",
|
|
622
|
+
os: "ios",
|
|
623
|
+
interface_version: "1.2.3",
|
|
624
|
+
},
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
test("malformed header values are dropped, valid ones kept", async () => {
|
|
629
|
+
const persistUserMessage = mock(
|
|
630
|
+
async (_options: { metadata?: Record<string, unknown> }) => ({
|
|
631
|
+
id: "persisted-msg-id",
|
|
632
|
+
deduplicated: false,
|
|
633
|
+
}),
|
|
634
|
+
);
|
|
635
|
+
const runAgentLoop = mock(async () => undefined);
|
|
636
|
+
const conversation = makeConversation({ persistUserMessage, runAgentLoop });
|
|
637
|
+
|
|
638
|
+
const res = await sendMessage(
|
|
639
|
+
"hello",
|
|
640
|
+
conversation,
|
|
641
|
+
{},
|
|
642
|
+
{
|
|
643
|
+
headers: {
|
|
644
|
+
// Uppercase + space + disallowed chars → normalized or dropped.
|
|
645
|
+
"x-vellum-browser-family": " SAFARI ",
|
|
646
|
+
"x-vellum-browser-version": "not allowed!",
|
|
647
|
+
"x-vellum-client-os": "a".repeat(65),
|
|
648
|
+
},
|
|
649
|
+
},
|
|
650
|
+
);
|
|
651
|
+
|
|
652
|
+
expect(res.status).toBe(202);
|
|
653
|
+
const [persistOptions] = persistUserMessage.mock.calls[0] as unknown as [
|
|
654
|
+
{ metadata?: Record<string, unknown> },
|
|
655
|
+
];
|
|
656
|
+
expect(persistOptions.metadata).toEqual({
|
|
657
|
+
client: { browser_family: "safari" },
|
|
658
|
+
});
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
test("no client metadata headers → metadata unchanged", async () => {
|
|
662
|
+
const persistUserMessage = mock(
|
|
663
|
+
async (_options: { metadata?: Record<string, unknown> }) => ({
|
|
664
|
+
id: "persisted-msg-id",
|
|
665
|
+
deduplicated: false,
|
|
666
|
+
}),
|
|
667
|
+
);
|
|
668
|
+
const runAgentLoop = mock(async () => undefined);
|
|
669
|
+
const conversation = makeConversation({ persistUserMessage, runAgentLoop });
|
|
670
|
+
|
|
671
|
+
const res = await sendMessage("hello", conversation);
|
|
672
|
+
|
|
673
|
+
expect(res.status).toBe(202);
|
|
674
|
+
const [persistOptions] = persistUserMessage.mock.calls[0] as unknown as [
|
|
675
|
+
{ metadata?: Record<string, unknown> },
|
|
676
|
+
];
|
|
677
|
+
expect(persistOptions.metadata).toBeUndefined();
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
|
|
529
681
|
// ============================================================================
|
|
530
682
|
// TRUST CONTEXT — derived from the gateway guardian binding
|
|
531
683
|
// ============================================================================
|
|
@@ -409,6 +409,14 @@
|
|
|
409
409
|
"label": "New-thread suggestions library",
|
|
410
410
|
"description": "Replace the empty-state conversation-starter chips with the scrollable, categorized suggestions library and detail drawer (currently mocked data).",
|
|
411
411
|
"defaultEnabled": false
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
"id": "account-mfa",
|
|
415
|
+
"scope": "client",
|
|
416
|
+
"key": "account-mfa",
|
|
417
|
+
"label": "Account MFA",
|
|
418
|
+
"description": "Surface the Security settings page where users enroll and manage TOTP two-factor authentication for their Vellum (WorkOS) account. Applies to email/password sign-in only.",
|
|
419
|
+
"defaultEnabled": false
|
|
412
420
|
}
|
|
413
421
|
]
|
|
414
422
|
}
|
|
@@ -522,16 +522,26 @@ export async function persistQueuedMessageBody(
|
|
|
522
522
|
turnChannel: turnCtx?.userMessageChannel,
|
|
523
523
|
});
|
|
524
524
|
|
|
525
|
-
//
|
|
526
|
-
//
|
|
527
|
-
//
|
|
528
|
-
//
|
|
529
|
-
//
|
|
525
|
+
// Client attribution for turn telemetry, stored under the `client`
|
|
526
|
+
// metadata bag which `turn-events-store` forwards onto
|
|
527
|
+
// `TurnTelemetryEvent.client`. The bag merges two sources per key:
|
|
528
|
+
// caller-supplied `client` metadata (e.g. the sanitized browser/OS/
|
|
529
|
+
// version headers read by `handleSendMessage`) wins, and the
|
|
530
|
+
// transport-reported OS (validated through `parseClientOs`) fills in
|
|
531
|
+
// `os` when the caller didn't supply one — so header-less paths (CLI,
|
|
532
|
+
// channel ingress) keep their OS attribution.
|
|
530
533
|
const clientOs = parseClientOs(ctx.clientOs);
|
|
534
|
+
const callerClient =
|
|
535
|
+
metadataWithoutSlackInbound.client != null &&
|
|
536
|
+
typeof metadataWithoutSlackInbound.client === "object"
|
|
537
|
+
? (metadataWithoutSlackInbound.client as Record<string, unknown>)
|
|
538
|
+
: null;
|
|
539
|
+
const clientEntries = {
|
|
540
|
+
...(clientOs ? { os: clientOs } : {}),
|
|
541
|
+
...(callerClient ?? {}),
|
|
542
|
+
};
|
|
531
543
|
const clientBag =
|
|
532
|
-
|
|
533
|
-
? { client: { os: clientOs } }
|
|
534
|
-
: {};
|
|
544
|
+
Object.keys(clientEntries).length > 0 ? { client: clientEntries } : {};
|
|
535
545
|
|
|
536
546
|
const mergedMetadata = {
|
|
537
547
|
...metadataWithoutSlackInbound,
|
|
@@ -169,10 +169,13 @@ export const messageMetadataSchema = z
|
|
|
169
169
|
* "macos" | "android") from the request body's `clientOs` field, stamped
|
|
170
170
|
* by `persistQueuedMessageBody` — the transport `userMessageInterface` is
|
|
171
171
|
* "web" for the web, iOS, and macOS apps alike, so this is the only
|
|
172
|
-
* per-platform attribution.
|
|
173
|
-
* `
|
|
174
|
-
*
|
|
175
|
-
*
|
|
172
|
+
* per-platform attribution. `browser_family` / `browser_version` /
|
|
173
|
+
* `interface_version` (and an `os` override) come from the sanitized
|
|
174
|
+
* `x-vellum-*` client-metadata headers read by `handleSendMessage`
|
|
175
|
+
* (see `@vellumai/service-contracts/client-metadata`). Forwarded
|
|
176
|
+
* verbatim onto `TurnTelemetryEvent.client` for downstream analytics.
|
|
177
|
+
* Kept as a permissive `record` so adding a new client field doesn't
|
|
178
|
+
* require a migration -- dbt can unpack later via JSON_VALUE.
|
|
176
179
|
*/
|
|
177
180
|
client: z.record(z.string(), z.unknown()).optional(),
|
|
178
181
|
subagentNotification: subagentNotificationSchema.optional(),
|
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
5
|
|
|
6
|
+
import {
|
|
7
|
+
CLIENT_METADATA_HEADERS,
|
|
8
|
+
type ClientMetadataField,
|
|
9
|
+
sanitizeClientMetadataValue,
|
|
10
|
+
} from "@vellumai/service-contracts/client-metadata";
|
|
6
11
|
import { z } from "zod";
|
|
7
12
|
|
|
8
13
|
import { enrichMessageWithSourcePaths } from "../../agent/attachments.js";
|
|
@@ -1289,6 +1294,51 @@ export function persistOnboardingArtifacts(onboarding: {
|
|
|
1289
1294
|
});
|
|
1290
1295
|
}
|
|
1291
1296
|
|
|
1297
|
+
type ClientMetadataBag = Partial<Record<ClientMetadataField, string>>;
|
|
1298
|
+
|
|
1299
|
+
/**
|
|
1300
|
+
* Read the sanitized client-metadata headers (browser family/version, OS
|
|
1301
|
+
* surface, build version) sent by web-bundle clients. Values are persisted
|
|
1302
|
+
* under `metadata.client` on the user message, which `turn-events-store`
|
|
1303
|
+
* projects onto `TurnTelemetryEvent.client` for analytics. Returns
|
|
1304
|
+
* `undefined` when no valid header is present so callers can omit the bag.
|
|
1305
|
+
*/
|
|
1306
|
+
function readClientMetadataHeaders(
|
|
1307
|
+
headers: Record<string, string> | undefined,
|
|
1308
|
+
): ClientMetadataBag | undefined {
|
|
1309
|
+
if (!headers) {
|
|
1310
|
+
return undefined;
|
|
1311
|
+
}
|
|
1312
|
+
const bag: ClientMetadataBag = {};
|
|
1313
|
+
for (const [field, headerName] of Object.entries(
|
|
1314
|
+
CLIENT_METADATA_HEADERS,
|
|
1315
|
+
) as Array<[ClientMetadataField, string]>) {
|
|
1316
|
+
const value = sanitizeClientMetadataValue(headers[headerName]);
|
|
1317
|
+
if (value) {
|
|
1318
|
+
bag[field] = value;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
return Object.keys(bag).length > 0 ? bag : undefined;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
/**
|
|
1325
|
+
* Attach the client-metadata bag to a persist-time metadata object under the
|
|
1326
|
+
* `client` key. Passes `metadata` through untouched (including `undefined`)
|
|
1327
|
+
* when there is no client metadata.
|
|
1328
|
+
*/
|
|
1329
|
+
function withClientMetadata(
|
|
1330
|
+
metadata: Record<string, unknown> | undefined,
|
|
1331
|
+
clientMetadata: ClientMetadataBag | undefined,
|
|
1332
|
+
): Record<string, unknown> | undefined {
|
|
1333
|
+
if (!clientMetadata) {
|
|
1334
|
+
return metadata;
|
|
1335
|
+
}
|
|
1336
|
+
return {
|
|
1337
|
+
...(metadata ?? {}),
|
|
1338
|
+
client: clientMetadata,
|
|
1339
|
+
};
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1292
1342
|
export async function handleSendMessage(
|
|
1293
1343
|
{ body: rawBody, headers }: RouteHandlerArgs,
|
|
1294
1344
|
deps: {
|
|
@@ -1342,6 +1392,7 @@ export async function handleSendMessage(
|
|
|
1342
1392
|
const actorPrincipalId = headers?.["x-vellum-actor-principal-id"];
|
|
1343
1393
|
const principalType = headers?.["x-vellum-principal-type"];
|
|
1344
1394
|
const originClientId = headers?.["x-vellum-client-id"]?.trim() || undefined;
|
|
1395
|
+
const clientMetadata = readClientMetadataHeaders(headers);
|
|
1345
1396
|
|
|
1346
1397
|
const { conversationKey, content, attachmentIds } = body;
|
|
1347
1398
|
const inboundConversationId =
|
|
@@ -1998,17 +2049,20 @@ export async function handleSendMessage(
|
|
|
1998
2049
|
attachments,
|
|
1999
2050
|
onEvent: broadcastMessage,
|
|
2000
2051
|
requestId,
|
|
2001
|
-
metadata:
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2052
|
+
metadata: withClientMetadata(
|
|
2053
|
+
{
|
|
2054
|
+
userMessageChannel: sourceChannel,
|
|
2055
|
+
assistantMessageChannel: sourceChannel,
|
|
2056
|
+
userMessageInterface: sourceInterface,
|
|
2057
|
+
assistantMessageInterface: sourceInterface,
|
|
2058
|
+
...(body.automated === true ? { automated: true } : {}),
|
|
2059
|
+
// Carry the transcript-suppression flag through the queue so a
|
|
2060
|
+
// hidden send that lands mid-turn stays hidden when drained —
|
|
2061
|
+
// the drain path persists this metadata and skips the echo.
|
|
2062
|
+
...(body.hidden === true ? { hidden: true } : {}),
|
|
2063
|
+
},
|
|
2064
|
+
clientMetadata,
|
|
2065
|
+
),
|
|
2012
2066
|
isInteractive,
|
|
2013
2067
|
sourceActorPrincipalId,
|
|
2014
2068
|
transport,
|
|
@@ -2144,7 +2198,7 @@ export async function handleSendMessage(
|
|
|
2144
2198
|
content: rawContent,
|
|
2145
2199
|
attachments,
|
|
2146
2200
|
requestId: crypto.randomUUID(),
|
|
2147
|
-
metadata: slashMeta,
|
|
2201
|
+
metadata: withClientMetadata(slashMeta, clientMetadata),
|
|
2148
2202
|
clientMessageId,
|
|
2149
2203
|
});
|
|
2150
2204
|
if (persisted.deduplicated) {
|
|
@@ -2243,7 +2297,7 @@ export async function handleSendMessage(
|
|
|
2243
2297
|
content: rawContent,
|
|
2244
2298
|
attachments,
|
|
2245
2299
|
requestId: crypto.randomUUID(),
|
|
2246
|
-
metadata: slashMeta,
|
|
2300
|
+
metadata: withClientMetadata(slashMeta, clientMetadata),
|
|
2247
2301
|
clientMessageId,
|
|
2248
2302
|
});
|
|
2249
2303
|
} catch (err) {
|
|
@@ -2356,7 +2410,7 @@ export async function handleSendMessage(
|
|
|
2356
2410
|
content: rawContent,
|
|
2357
2411
|
attachments,
|
|
2358
2412
|
requestId: crypto.randomUUID(),
|
|
2359
|
-
metadata: slashMeta,
|
|
2413
|
+
metadata: withClientMetadata(slashMeta, clientMetadata),
|
|
2360
2414
|
clientMessageId,
|
|
2361
2415
|
});
|
|
2362
2416
|
if (persisted.deduplicated) {
|
|
@@ -2439,13 +2493,15 @@ export async function handleSendMessage(
|
|
|
2439
2493
|
content: resolvedContent,
|
|
2440
2494
|
attachments,
|
|
2441
2495
|
requestId,
|
|
2442
|
-
metadata:
|
|
2496
|
+
metadata: withClientMetadata(
|
|
2443
2497
|
body.automated === true || body.hidden === true
|
|
2444
2498
|
? {
|
|
2445
2499
|
...(body.automated === true ? { automated: true } : {}),
|
|
2446
2500
|
...(body.hidden === true ? { hidden: true } : {}),
|
|
2447
2501
|
}
|
|
2448
2502
|
: undefined,
|
|
2503
|
+
clientMetadata,
|
|
2504
|
+
),
|
|
2449
2505
|
clientMessageId,
|
|
2450
2506
|
});
|
|
2451
2507
|
|