@vellumai/assistant 0.11.9-dev.202609042113.df14509 → 0.11.9-dev.202609042214.025e78d

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.11.9-dev.202609042113.df14509",
3
+ "version": "0.11.9-dev.202609042214.025e78d",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -10,6 +10,15 @@ import { UnprocessableEntityError } from "../errors.js";
10
10
  import type { RouteDefinition } from "../types.js";
11
11
 
12
12
  let storedApiKey: string | undefined = "assistant-key";
13
+ let platformBaseUrl = "https://platform.vellum.ai";
14
+
15
+ // Spread the real module: replacing it wholesale strips the other exports the
16
+ // route graph pulls in.
17
+ const actualEnv = await import("../../../config/env.js");
18
+ mock.module("../../../config/env.js", () => ({
19
+ ...actualEnv,
20
+ getPlatformBaseUrl: () => platformBaseUrl,
21
+ }));
13
22
 
14
23
  const actualSecureKeys = await import("../../../security/secure-keys.js");
15
24
  mock.module("../../../security/secure-keys.js", () => ({
@@ -83,6 +92,7 @@ const UPSTREAM_ITEM = {
83
92
 
84
93
  beforeEach(() => {
85
94
  storedApiKey = "assistant-key";
95
+ platformBaseUrl = "https://platform.vellum.ai";
86
96
  calls = [];
87
97
  process.env.VELLUM_MARKETING_URL = "https://marketing.test";
88
98
  process.env.VELLUM_WEB_URL = "https://web.test";
@@ -96,9 +106,8 @@ afterEach(() => {
96
106
  });
97
107
 
98
108
  describe("which deployment the roadmap calls reach", () => {
99
- test("production needs no configuration", async () => {
109
+ test("a production assistant needs no configuration", async () => {
100
110
  delete process.env.VELLUM_MARKETING_URL;
101
- process.env.VELLUM_ENVIRONMENT = "production";
102
111
  stubFetch({ items: [], total: 0 });
103
112
 
104
113
  await list({});
@@ -106,19 +115,101 @@ describe("which deployment the roadmap calls reach", () => {
106
115
  expect(calls[0].url).toBe("https://marketing.vellum.ai/v1/roadmap");
107
116
  });
108
117
 
109
- test("an unconfigured non-production assistant refuses rather than writing to the public roadmap", async () => {
118
+ // The deployment is judged by the platform that issued the key, never by
119
+ // VELLUM_ENVIRONMENT: unset, that variable means dev to getPlatformBaseUrl
120
+ // and local to every launcher, so trusting it would let exactly the assistant
121
+ // with no label post to the real roadmap.
122
+ test.each([
123
+ ["https://staging-platform.vellum.ai", "staging"],
124
+ ["https://dev-platform.vellum.ai", "dev"],
125
+ ["http://localhost:8000", "local"],
126
+ ])(
127
+ "an unconfigured %s assistant refuses to touch the public roadmap",
128
+ async (baseUrl) => {
129
+ platformBaseUrl = baseUrl;
130
+ delete process.env.VELLUM_MARKETING_URL;
131
+ delete process.env.VELLUM_ENVIRONMENT;
132
+ stubFetch({ items: [], total: 0 });
133
+
134
+ await expect(list({})).rejects.toThrow("The Vellum roadmap has no");
135
+ expect(calls).toHaveLength(0);
136
+ },
137
+ );
138
+
139
+ test("VELLUM_ENVIRONMENT=production does not by itself unlock the real roadmap", async () => {
140
+ platformBaseUrl = "https://dev-platform.vellum.ai";
110
141
  delete process.env.VELLUM_MARKETING_URL;
142
+ process.env.VELLUM_ENVIRONMENT = "production";
143
+ stubFetch({ items: [], total: 0 });
144
+
145
+ await expect(list({})).rejects.toThrow("The Vellum roadmap has no");
146
+ expect(calls).toHaveLength(0);
147
+ });
148
+
149
+ test("item links point at the deployment the item was read from", async () => {
150
+ // Labelled staging, but authenticating against production: the link has to
151
+ // follow the platform, or it points at a staging page for a real item.
111
152
  process.env.VELLUM_ENVIRONMENT = "staging";
153
+ delete process.env.VELLUM_WEB_URL;
154
+ delete process.env.VELLUM_MARKETING_URL;
155
+ stubFetch({ items: [UPSTREAM_ITEM], total: 1 });
156
+
157
+ const result = (await list({})) as { items: { url: string }[] };
158
+
159
+ expect(result.items[0].url).toBe("https://www.vellum.ai/roadmap/dark-mode");
160
+ });
161
+
162
+ // A self-hosted platform is not in the seed table, so nothing is known about
163
+ // it: naming its roadmap API says nothing about where its items are rendered.
164
+ test("a self-hosted deployment must name its web origin too", async () => {
165
+ platformBaseUrl = "https://platform.self-hosted.example";
166
+ delete process.env.VELLUM_WEB_URL;
112
167
  stubFetch({ items: [], total: 0 });
113
168
 
114
- await expect(list({})).rejects.toThrow(
115
- "The Vellum roadmap has no staging deployment",
116
- );
169
+ await expect(list({})).rejects.toThrow("Set VELLUM_WEB_URL");
117
170
  expect(calls).toHaveLength(0);
118
171
  });
119
172
 
120
- test("a named endpoint is honored in any environment", async () => {
121
- process.env.VELLUM_ENVIRONMENT = "dev";
173
+ test("a self-hosted deployment works once both endpoints are named", async () => {
174
+ platformBaseUrl = "https://platform.self-hosted.example";
175
+ stubFetch({ items: [UPSTREAM_ITEM], total: 1 });
176
+
177
+ const result = (await list({})) as { items: { url: string }[] };
178
+
179
+ expect(calls[0].url).toBe("https://marketing.test/v1/roadmap");
180
+ expect(result.items[0].url).toBe("https://web.test/roadmap/dark-mode");
181
+ });
182
+
183
+ test("a platform change mid-flight cannot fail a create that already published", async () => {
184
+ delete process.env.VELLUM_MARKETING_URL;
185
+ delete process.env.VELLUM_WEB_URL;
186
+ globalThis.fetch = (async (
187
+ _input: string | URL | Request,
188
+ _init?: RequestInit,
189
+ ): Promise<Response> => {
190
+ // The item is published. Only now is the assistant repointed at a
191
+ // platform with no roadmap deployment, as a concurrent
192
+ // `platform_base_url` write would do.
193
+ platformBaseUrl = "https://platform.self-hosted.example";
194
+ return new Response(
195
+ JSON.stringify({
196
+ slug: "dark-mode",
197
+ title: "Add dark mode",
198
+ status: "open",
199
+ }),
200
+ { status: 200, headers: { "Content-Type": "application/json" } },
201
+ );
202
+ }) as typeof fetch;
203
+
204
+ const created = (await create({
205
+ body: { title: "Add dark mode" },
206
+ })) as { url: string };
207
+
208
+ expect(created.url).toBe("https://www.vellum.ai/roadmap/dark-mode");
209
+ });
210
+
211
+ test("a named endpoint is honored on any deployment", async () => {
212
+ platformBaseUrl = "https://dev-platform.vellum.ai";
122
213
  stubFetch({ items: [], total: 0 });
123
214
 
124
215
  await list({});
@@ -19,9 +19,10 @@
19
19
  * DELETE /v1/roadmap/:slug/upvote : remove the upvote
20
20
  */
21
21
 
22
- import { SEEDS } from "@vellumai/environments";
22
+ import { type EnvironmentDefinition, SEEDS } from "@vellumai/environments";
23
23
  import { z } from "zod";
24
24
 
25
+ import { getPlatformBaseUrl } from "../../config/env.js";
25
26
  import { credentialKey } from "../../security/credential-key.js";
26
27
  import { getSecureKeyAsync } from "../../security/secure-keys.js";
27
28
  import { ACTOR_PRINCIPALS } from "../auth/route-policy.js";
@@ -58,44 +59,95 @@ function stripTrailingSlashes(url: string): string {
58
59
  return url.replace(/\/+$/, "");
59
60
  }
60
61
 
62
+ function hostnameOf(url: string): string | undefined {
63
+ try {
64
+ return new URL(url).hostname;
65
+ } catch {
66
+ return undefined;
67
+ }
68
+ }
69
+
61
70
  /**
62
- * Marketing service that serves the roadmap API. Reached directly rather than
63
- * through the `www.vellum.ai/api/marketing` proxy the user-facing CLI uses:
64
- * that proxy carries the owner's session cookie, which is exactly the identity
65
- * this surface exists to avoid.
71
+ * The deployment this assistant belongs to, judged by the platform it
72
+ * authenticates against.
73
+ *
74
+ * `VELLUM_ENVIRONMENT` cannot answer this. Unset, it means dev to
75
+ * `getPlatformBaseUrl` and local to every launcher, but it would have to mean
76
+ * production for a roadmap default to be safe, and it is blind to the config
77
+ * file and `VELLUM_PLATFORM_URL`, either of which can point a differently
78
+ * labelled assistant at production. The platform base URL is the one signal
79
+ * that already accounts for all three, and it names the deployment that issued
80
+ * the very key these calls are signed with.
66
81
  *
67
- * Only production has a default, and deliberately so. The roadmap is a single
68
- * public site, so an unconfigured staging or dev assistant that fell back to
69
- * it would file real items under its own name and hand a non-production key to
70
- * a production host. Outside production the endpoint has to be named.
82
+ * Undefined for a platform outside the seed table (a self-hosted one, say),
83
+ * which is not production and whose roadmap and web origins must be named.
71
84
  */
72
- function marketingBaseUrl(): string {
73
- const override = process.env.VELLUM_MARKETING_URL?.trim();
74
- if (override) {
75
- return stripTrailingSlashes(override);
85
+ function resolveDeployment(): EnvironmentDefinition | undefined {
86
+ const host = hostnameOf(getPlatformBaseUrl());
87
+ if (!host) {
88
+ return undefined;
76
89
  }
77
- const env = process.env.VELLUM_ENVIRONMENT?.trim();
78
- if (env && env !== "production") {
90
+ return Object.values(SEEDS).find(
91
+ (seed) => hostnameOf(seed.platformUrl) === host,
92
+ );
93
+ }
94
+
95
+ interface RoadmapEndpoints {
96
+ /**
97
+ * Marketing service that serves the roadmap API. Reached directly rather
98
+ * than through the `www.vellum.ai/api/marketing` proxy the user-facing CLI
99
+ * uses: that proxy carries the owner's session cookie, which is exactly the
100
+ * identity this surface exists to avoid.
101
+ */
102
+ api: string;
103
+ /** Site that renders roadmap items, for the human-facing item links. */
104
+ web: string;
105
+ }
106
+
107
+ /**
108
+ * Both endpoints this surface talks to, resolved together from one deployment
109
+ * so a link can never name a different deployment than the call that fetched
110
+ * it.
111
+ *
112
+ * Only production has defaults, and deliberately so. The roadmap is a single
113
+ * public site, so a non-production assistant that fell back to it would file
114
+ * real items under its own name and hand a key the production service never
115
+ * issued to a production host. Every other deployment, including a self-hosted
116
+ * platform the seed table does not know, has to name both.
117
+ *
118
+ * Each handler resolves this once and carries the result through both the
119
+ * request and the response mapping. Resolving again while rendering the reply
120
+ * would reintroduce the failure this ordering exists to prevent: a
121
+ * `platform_base_url` write landing mid-flight could make a create throw after
122
+ * the item was already published, or stamp a link from a deployment other than
123
+ * the one that answered.
124
+ */
125
+ function resolveEndpoints(): RoadmapEndpoints {
126
+ const deployment = resolveDeployment();
127
+ const apiOverride = process.env.VELLUM_MARKETING_URL?.trim();
128
+ const webOverride = process.env.VELLUM_WEB_URL?.trim();
129
+
130
+ if (!apiOverride && deployment?.name !== "production") {
79
131
  throw new UnprocessableEntityError(
80
- `The Vellum roadmap has no ${env} deployment. Set VELLUM_MARKETING_URL to the roadmap service for this environment, or run the assistant in production.`,
132
+ `The Vellum roadmap has no deployment for a ${getPlatformBaseUrl()} assistant. Set VELLUM_MARKETING_URL to the roadmap service for this environment.`,
81
133
  );
82
134
  }
83
- return DEFAULT_MARKETING_URL;
84
- }
85
135
 
86
- /** Site that renders roadmap items, for the human-facing item links. */
87
- function webBaseUrl(): string {
88
- const override = process.env.VELLUM_WEB_URL?.trim();
89
- if (override) {
90
- return stripTrailingSlashes(override);
136
+ const web = webOverride || deployment?.webUrl;
137
+ if (!web) {
138
+ throw new UnprocessableEntityError(
139
+ `No web origin is known for a ${getPlatformBaseUrl()} assistant, so roadmap item links cannot be built. Set VELLUM_WEB_URL to the site that renders this environment's roadmap.`,
140
+ );
91
141
  }
92
- const env = process.env.VELLUM_ENVIRONMENT?.trim();
93
- const seed = (env ? SEEDS[env] : undefined) ?? SEEDS.production;
94
- return stripTrailingSlashes(seed.webUrl);
142
+
143
+ return {
144
+ api: stripTrailingSlashes(apiOverride || DEFAULT_MARKETING_URL),
145
+ web: stripTrailingSlashes(web),
146
+ };
95
147
  }
96
148
 
97
- function itemUrl(slug: string): string {
98
- return `${webBaseUrl()}/roadmap/${slug}`;
149
+ function itemUrl(web: string, slug: string): string {
150
+ return `${web}/roadmap/${slug}`;
99
151
  }
100
152
 
101
153
  // ---------------------------------------------------------------------------
@@ -137,6 +189,7 @@ async function requireAssistantApiKey(): Promise<string> {
137
189
  * anonymous.
138
190
  */
139
191
  async function roadmapFetch(
192
+ endpoints: RoadmapEndpoints,
140
193
  path: string,
141
194
  opts: {
142
195
  method?: string;
@@ -151,7 +204,7 @@ async function roadmapFetch(
151
204
  (entry): entry is [string, string] => entry[1] !== undefined,
152
205
  ),
153
206
  ).toString();
154
- const url = `${marketingBaseUrl()}/v1/roadmap${path}${query ? `?${query}` : ""}`;
207
+ const url = `${endpoints.api}/v1/roadmap${path}${query ? `?${query}` : ""}`;
155
208
 
156
209
  const headers: Record<string, string> = {};
157
210
  if (opts.key) {
@@ -342,12 +395,12 @@ type RoadmapItem = z.infer<typeof itemSchema>;
342
395
  type RoadmapItemDetail = z.infer<typeof itemDetailSchema>;
343
396
  type MutatedRoadmapItem = z.infer<typeof mutatedItemSchema>;
344
397
 
345
- function toItem(raw: UpstreamItem): RoadmapItem {
398
+ function toItem(web: string, raw: UpstreamItem): RoadmapItem {
346
399
  return {
347
400
  slug: raw.slug,
348
401
  title: raw.title,
349
402
  status: raw.status,
350
- url: itemUrl(raw.slug),
403
+ url: itemUrl(web, raw.slug),
351
404
  upvoteCount: raw.upvote_count,
352
405
  commentCount: raw.comment_count,
353
406
  tags: raw.tags ?? [],
@@ -355,18 +408,21 @@ function toItem(raw: UpstreamItem): RoadmapItem {
355
408
  };
356
409
  }
357
410
 
358
- function toMutatedItem(raw: UpstreamMutatedItem): MutatedRoadmapItem {
411
+ function toMutatedItem(
412
+ web: string,
413
+ raw: UpstreamMutatedItem,
414
+ ): MutatedRoadmapItem {
359
415
  return {
360
416
  slug: raw.slug,
361
417
  title: raw.title,
362
418
  status: raw.status,
363
- url: itemUrl(raw.slug),
419
+ url: itemUrl(web, raw.slug),
364
420
  };
365
421
  }
366
422
 
367
- function toItemDetail(raw: UpstreamItemDetail): RoadmapItemDetail {
423
+ function toItemDetail(web: string, raw: UpstreamItemDetail): RoadmapItemDetail {
368
424
  return {
369
- ...toItem(raw),
425
+ ...toItem(web, raw),
370
426
  description: raw.description ?? "",
371
427
  creatorUsername: raw.creator_username,
372
428
  creatorKind: raw.creator_kind ?? null,
@@ -419,7 +475,8 @@ async function handleList({
419
475
  items: RoadmapItem[];
420
476
  total: number;
421
477
  }> {
422
- const response = await roadmapFetch("", {
478
+ const endpoints = resolveEndpoints();
479
+ const response = await roadmapFetch(endpoints, "", {
423
480
  key: await assistantApiKey(),
424
481
  signal: abortSignal,
425
482
  params: {
@@ -435,17 +492,23 @@ async function handleList({
435
492
  response,
436
493
  "list roadmap items",
437
494
  );
438
- const items = (data.items ?? []).map(toItem);
495
+ const items = (data.items ?? []).map((raw) => toItem(endpoints.web, raw));
439
496
  return { items, total: data.total ?? items.length };
440
497
  }
441
498
 
442
499
  async function handleGet(args: RouteHandlerArgs): Promise<RoadmapItemDetail> {
443
500
  const slug = requireSlug(args);
444
- const response = await roadmapFetch(`/${encodeURIComponent(slug)}`, {
445
- key: await assistantApiKey(),
446
- signal: args.abortSignal,
447
- });
501
+ const endpoints = resolveEndpoints();
502
+ const response = await roadmapFetch(
503
+ endpoints,
504
+ `/${encodeURIComponent(slug)}`,
505
+ {
506
+ key: await assistantApiKey(),
507
+ signal: args.abortSignal,
508
+ },
509
+ );
448
510
  return toItemDetail(
511
+ endpoints.web,
449
512
  await roadmapJson<UpstreamItemDetail>(response, "get roadmap item"),
450
513
  );
451
514
  }
@@ -454,13 +517,15 @@ async function handleCreate(
454
517
  args: RouteHandlerArgs,
455
518
  ): Promise<MutatedRoadmapItem> {
456
519
  const item = parseBody(createRequestSchema, args.body);
457
- const response = await roadmapFetch("", {
520
+ const endpoints = resolveEndpoints();
521
+ const response = await roadmapFetch(endpoints, "", {
458
522
  method: "POST",
459
523
  key: await requireAssistantApiKey(),
460
524
  body: { ...item, title: item.title.trim() },
461
525
  signal: args.abortSignal,
462
526
  });
463
527
  return toMutatedItem(
528
+ endpoints.web,
464
529
  await roadmapJson<UpstreamMutatedItem>(response, "create roadmap item"),
465
530
  );
466
531
  }
@@ -470,13 +535,19 @@ async function handleUpdate(
470
535
  ): Promise<MutatedRoadmapItem> {
471
536
  const slug = requireSlug(args);
472
537
  const patch = parseBody(updateRequestSchema, args.body);
473
- const response = await roadmapFetch(`/${encodeURIComponent(slug)}`, {
474
- method: "PATCH",
475
- key: await requireAssistantApiKey(),
476
- body: patch,
477
- signal: args.abortSignal,
478
- });
538
+ const endpoints = resolveEndpoints();
539
+ const response = await roadmapFetch(
540
+ endpoints,
541
+ `/${encodeURIComponent(slug)}`,
542
+ {
543
+ method: "PATCH",
544
+ key: await requireAssistantApiKey(),
545
+ body: patch,
546
+ signal: args.abortSignal,
547
+ },
548
+ );
479
549
  return toMutatedItem(
550
+ endpoints.web,
480
551
  await roadmapJson<UpstreamMutatedItem>(response, "update roadmap item"),
481
552
  );
482
553
  }
@@ -485,11 +556,15 @@ async function handleDelete(
485
556
  args: RouteHandlerArgs,
486
557
  ): Promise<{ slug: string; deleted: true }> {
487
558
  const slug = requireSlug(args);
488
- const response = await roadmapFetch(`/${encodeURIComponent(slug)}`, {
489
- method: "DELETE",
490
- key: await requireAssistantApiKey(),
491
- signal: args.abortSignal,
492
- });
559
+ const response = await roadmapFetch(
560
+ resolveEndpoints(),
561
+ `/${encodeURIComponent(slug)}`,
562
+ {
563
+ method: "DELETE",
564
+ key: await requireAssistantApiKey(),
565
+ signal: args.abortSignal,
566
+ },
567
+ );
493
568
  if (!response.ok) {
494
569
  await throwRoadmapError(response, "delete roadmap item");
495
570
  }
@@ -502,11 +577,15 @@ async function vote(
502
577
  action: string,
503
578
  ): Promise<{ slug: string; upvoteCount: number }> {
504
579
  const slug = requireSlug(args);
505
- const response = await roadmapFetch(`/${encodeURIComponent(slug)}/upvote`, {
506
- method,
507
- key: await requireAssistantApiKey(),
508
- signal: args.abortSignal,
509
- });
580
+ const response = await roadmapFetch(
581
+ resolveEndpoints(),
582
+ `/${encodeURIComponent(slug)}/upvote`,
583
+ {
584
+ method,
585
+ key: await requireAssistantApiKey(),
586
+ signal: args.abortSignal,
587
+ },
588
+ );
510
589
  const data = await roadmapJson<{ slug?: string; upvote_count?: number }>(
511
590
  response,
512
591
  action,