@segmentstream/ontology 0.106.3 → 0.108.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/README.md +45 -11
  3. package/dist/analyze.d.ts +7 -1
  4. package/dist/analyze.js +271 -16
  5. package/dist/authoring.js +6 -0
  6. package/dist/diagnostics.d.ts +3 -3
  7. package/dist/diagnostics.js +1 -0
  8. package/dist/emails.d.ts +36 -5
  9. package/dist/emails.js +139 -2
  10. package/dist/index.d.ts +7 -7
  11. package/dist/index.js +5 -3
  12. package/dist/manifest.d.ts +262 -1
  13. package/dist/manifest.js +73 -4
  14. package/dist/migrate/apply.js +8 -1
  15. package/dist/migrate/model.d.ts +154 -0
  16. package/dist/migrate/storage.d.ts +2 -0
  17. package/dist/migrate/storage.js +8 -0
  18. package/dist/names.d.ts +5 -0
  19. package/dist/names.js +5 -0
  20. package/dist/ontology.d.ts +4 -1
  21. package/dist/permissions.d.ts +30 -2
  22. package/dist/permissions.js +51 -6
  23. package/dist/runtime/agent-counters.d.ts +5 -1
  24. package/dist/runtime/agent-counters.js +21 -6
  25. package/dist/runtime/agent-web.d.ts +9 -6
  26. package/dist/runtime/agent-web.js +101 -5
  27. package/dist/runtime/generated-actions.js +2 -3
  28. package/dist/runtime/guests.d.ts +32 -0
  29. package/dist/runtime/guests.js +64 -0
  30. package/dist/runtime/new-user-hook.js +2 -1
  31. package/dist/runtime/oauth.js +7 -1
  32. package/dist/runtime/pipeline.js +3 -2
  33. package/dist/runtime/principals.js +6 -4
  34. package/dist/runtime/sql-runtime.js +2 -1
  35. package/dist/runtime.d.ts +10 -2
  36. package/dist/service/agent/authentication.d.ts +6 -4
  37. package/dist/service/agent/authentication.js +20 -5
  38. package/dist/service/agent/protocol.d.ts +28 -2
  39. package/dist/service/agent/protocol.js +23 -10
  40. package/dist/service/agent/server.d.ts +9 -2
  41. package/dist/service/agent/server.js +12 -12
  42. package/dist/service/agent/tasks.d.ts +3 -0
  43. package/dist/service/agent/tasks.js +5 -3
  44. package/dist/service/agent/web-tokens.d.ts +28 -13
  45. package/dist/service/agent/web-tokens.js +91 -38
  46. package/dist/service/authentication.d.ts +6 -0
  47. package/dist/service/authentication.js +11 -2
  48. package/dist/service/chat/review-delivery.js +4 -0
  49. package/dist/service/mcp/server.js +3 -3
  50. package/dist/service/mcp/tools.d.ts +4 -2
  51. package/dist/service/mcp/tools.js +14 -2
  52. package/dist/service/oauth/protocol.js +8 -2
  53. package/dist/service/oauth/server.js +5 -1
  54. package/dist/service/runtime-contract.d.ts +58 -6
  55. package/dist/service/schemas.d.ts +40 -0
  56. package/dist/service/service.js +15 -4
  57. package/dist/sign-in.d.ts +66 -9
  58. package/dist/sign-in.js +87 -9
  59. package/package.json +1 -1
@@ -127,7 +127,11 @@ export type DevicePoll = {
127
127
  export interface OAuthGrant {
128
128
  readonly id: string;
129
129
  readonly clientId: string;
130
- readonly userId: string;
130
+ /**
131
+ * The user the grant acts for; `null` for a guest's grant to the agent (`agent-web`), whose id is
132
+ * the guest's, since a guest is no user.
133
+ */
134
+ readonly userId: string | null;
131
135
  readonly scope: string;
132
136
  readonly resource: string;
133
137
  /** Present on access-token lookup, for absolute session expiry. */
@@ -191,8 +195,10 @@ export interface OAuthStore {
191
195
  readonly findRequest: (id: string) => Effect.Effect<OAuthRequest | undefined>;
192
196
  /** Removes a request that has not expired and returns it: a request is decided once. */
193
197
  readonly takeRequest: (id: string) => Effect.Effect<OAuthRequest | undefined>;
194
- /** Records a grant, with its PKCE code when using browser authorization. */
195
- readonly saveGrant: (grant: OAuthGrant, code?: OAuthToken) => Effect.Effect<void>;
198
+ /** Records a member's grant, with its PKCE code when using browser authorization. */
199
+ readonly saveGrant: (grant: OAuthGrant & {
200
+ readonly userId: string;
201
+ }, code?: OAuthToken) => Effect.Effect<void>;
196
202
  /** Uses a code or refresh token, once, before it expires and while its grant stands. */
197
203
  readonly useToken: (hash: string, kind: "code" | "refresh") => Effect.Effect<UsedOAuthToken>;
198
204
  /** Stores new tokens, and forgets the expired ones. */
@@ -204,6 +210,11 @@ export interface OAuthStore {
204
210
  readonly revokeGrant: (id: string) => Effect.Effect<void>;
205
211
  /** The member a grant acts for, with the roles they hold now; undefined once they may not sign in. */
206
212
  readonly memberPrincipal: (userId: string) => Effect.Effect<Principal | undefined>;
213
+ /**
214
+ * The guest a grant without a user stands for: a guest's grant to the agent (`agent-web`) while
215
+ * the ontology admits guests; `undefined` for any other grant. Absent on a store without guests.
216
+ */
217
+ readonly guestPrincipal?: (grant: OAuthGrant) => Effect.Effect<Principal | undefined>;
207
218
  }
208
219
  /** A channel account waiting, for ten minutes, for a member to claim it with its pairing code. */
209
220
  export interface AgentPairing {
@@ -258,9 +269,30 @@ export interface AgentWebStore {
258
269
  * Counts one turn a member starts on the web against the agent's limits when `signUp` created
259
270
  * them, as their turns on a messenger are counted, on the same counters; invited and paired
260
271
  * members are never counted. Their `onSignUp`, if it is still pending, runs again first. The
261
- * limit the turn exceeds, or `undefined` when it may run.
272
+ * limit the turn exceeds, or `undefined` when it may run. `limits`, the agent's own, are kept as
273
+ * a messenger's turn keeps them, before counting.
274
+ */
275
+ readonly countTurn: (userId: string, limits?: ResolveIdentityInput["limits"]) => Effect.Effect<WebTurnLimit | undefined>;
276
+ /**
277
+ * The guest an `agent-web` grant without a user stands for, while it stands and the ontology
278
+ * admits guests; `undefined` otherwise. The grant's id is the guest's.
279
+ */
280
+ readonly grantGuest?: (grantId: string) => Effect.Effect<Principal | undefined>;
281
+ /**
282
+ * Counts one turn a guest starts against the agent's guest limits, per guest and for every guest
283
+ * together, apart from the counters of people who signed up. `limits` are kept first, as for
284
+ * {@link countTurn}. The limit the turn exceeds, or `undefined` when it may run.
285
+ */
286
+ readonly countGuestTurn?: (guestId: string, limits?: ResolveIdentityInput["limits"]) => Effect.Effect<WebTurnLimit | undefined>;
287
+ /**
288
+ * A new guest of a declared website: a grant to the agent at `resource` that names no user, with
289
+ * its access and refresh tokens. No user or identity is created. `client` keys how many guests one
290
+ * network address may start an hour; it counts toward no limit of new users.
262
291
  */
263
- readonly countTurn: (userId: string) => Effect.Effect<WebTurnLimit | undefined>;
292
+ readonly guest?: (input: {
293
+ readonly resource: string;
294
+ readonly client: string;
295
+ }) => Effect.Effect<GuestIssue>;
264
296
  /**
265
297
  * Signs up a new visitor of a website on `web`: the user `signUp` creates for them, and the access
266
298
  * and refresh tokens of their grant to the agent at `resource`. `client` keys how many visitors one
@@ -285,6 +317,20 @@ export type VisitorSignUp = {
285
317
  } | {
286
318
  readonly status: "refused";
287
319
  };
320
+ /** A guest's credential, as {@link AgentWebStore.guest} answers it. */
321
+ export type GuestIssue = {
322
+ readonly status: "issued";
323
+ /** The guest's id: the id of their grant, which names no user. */
324
+ readonly guestId: string;
325
+ readonly token: string;
326
+ readonly refreshToken: string;
327
+ readonly expiresAt: Date;
328
+ } | {
329
+ readonly status: "limited";
330
+ readonly retryAfterSeconds: number;
331
+ } | {
332
+ readonly status: "refused";
333
+ };
288
334
  /** The limit a member's turn on the web exceeds, and when the next one may run. */
289
335
  export interface WebTurnLimit {
290
336
  readonly scope: AgentLimitScope;
@@ -303,8 +349,13 @@ export interface AgentLimits {
303
349
  };
304
350
  readonly newUsersPerHour: number;
305
351
  readonly workspaceTurnsPerDay: number;
352
+ /** Guests' turns, apart from those of people who signed up: per guest and for all together. */
353
+ readonly guest: {
354
+ readonly turnsPerDay: number;
355
+ readonly workspaceTurnsPerDay: number;
356
+ };
306
357
  }
307
- export type AgentLimitScope = "turnsPerMinute" | "turnsPerDay" | "newUsersPerHour" | "workspaceTurnsPerDay";
358
+ export type AgentLimitScope = "turnsPerMinute" | "turnsPerDay" | "newUsersPerHour" | "workspaceTurnsPerDay" | "guestTurnsPerDay" | "guestWorkspaceTurnsPerDay" | "newGuestsPerHour";
308
359
  export interface ResolveIdentityInput {
309
360
  readonly provider: string;
310
361
  readonly externalId: string;
@@ -323,6 +374,7 @@ export interface ResolveIdentityInput {
323
374
  readonly perSender?: Partial<AgentLimits["perSender"]>;
324
375
  readonly newUsersPerHour?: number;
325
376
  readonly workspaceTurnsPerDay?: number;
377
+ readonly guest?: Partial<AgentLimits["guest"]>;
326
378
  };
327
379
  }
328
380
  /** Verified provider facts only; channel-app callers cannot submit policy or bot-turn fields. */
@@ -428,11 +428,17 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
428
428
  readonly origins: Schema.optionalKey<Schema.$Array<Schema.String>>;
429
429
  }>>;
430
430
  readonly onSignUp: Schema.optionalKey<Schema.String>;
431
+ readonly guest: Schema.optionalKey<Schema.Struct<{
432
+ readonly channels: Schema.$Array<Schema.String>;
433
+ readonly origins: Schema.$Array<Schema.String>;
434
+ }>>;
435
+ readonly invitationEmail: Schema.optionalKey<Schema.String>;
431
436
  }>, {
432
437
  readonly onNewUser: "on_new_user";
433
438
  readonly codeLink: "code_link";
434
439
  readonly signUp: "sign_up";
435
440
  readonly onSignUp: "on_sign_up";
441
+ readonly invitationEmail: "invitation_email";
436
442
  }>>;
437
443
  readonly emails: Schema.optionalKey<Schema.$Record<Schema.String, Schema.encodeKeys<Schema.Struct<{
438
444
  readonly subject: Schema.String;
@@ -445,6 +451,20 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
445
451
  readonly note: Schema.optionalKey<Schema.String>;
446
452
  readonly paragraphs: Schema.optionalKey<Schema.$Array<Schema.String>>;
447
453
  readonly action: Schema.String;
454
+ readonly sections: Schema.optionalKey<Schema.$Array<Schema.Struct<{
455
+ readonly title: Schema.String;
456
+ readonly text: Schema.optionalKey<Schema.String>;
457
+ readonly copy: Schema.optionalKey<Schema.Struct<{
458
+ readonly label: Schema.optionalKey<Schema.String>;
459
+ readonly value: Schema.String;
460
+ }>>;
461
+ readonly items: Schema.optionalKey<Schema.$Array<Schema.Struct<{
462
+ readonly label: Schema.String;
463
+ readonly text: Schema.optionalKey<Schema.String>;
464
+ readonly code: Schema.optionalKey<Schema.String>;
465
+ }>>>;
466
+ readonly note: Schema.optionalKey<Schema.String>;
467
+ }>>>;
448
468
  readonly footer: Schema.optionalKey<Schema.String>;
449
469
  readonly replyTo: Schema.optionalKey<Schema.String>;
450
470
  }>, {
@@ -704,11 +724,17 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
704
724
  readonly origins: Schema.optionalKey<Schema.$Array<Schema.String>>;
705
725
  }>>;
706
726
  readonly onSignUp: Schema.optionalKey<Schema.String>;
727
+ readonly guest: Schema.optionalKey<Schema.Struct<{
728
+ readonly channels: Schema.$Array<Schema.String>;
729
+ readonly origins: Schema.$Array<Schema.String>;
730
+ }>>;
731
+ readonly invitationEmail: Schema.optionalKey<Schema.String>;
707
732
  }>, {
708
733
  readonly onNewUser: "on_new_user";
709
734
  readonly codeLink: "code_link";
710
735
  readonly signUp: "sign_up";
711
736
  readonly onSignUp: "on_sign_up";
737
+ readonly invitationEmail: "invitation_email";
712
738
  }>>>;
713
739
  readonly emails: Schema.toEncoded<Schema.optionalKey<Schema.$Record<Schema.String, Schema.encodeKeys<Schema.Struct<{
714
740
  readonly subject: Schema.String;
@@ -721,6 +747,20 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
721
747
  readonly note: Schema.optionalKey<Schema.String>;
722
748
  readonly paragraphs: Schema.optionalKey<Schema.$Array<Schema.String>>;
723
749
  readonly action: Schema.String;
750
+ readonly sections: Schema.optionalKey<Schema.$Array<Schema.Struct<{
751
+ readonly title: Schema.String;
752
+ readonly text: Schema.optionalKey<Schema.String>;
753
+ readonly copy: Schema.optionalKey<Schema.Struct<{
754
+ readonly label: Schema.optionalKey<Schema.String>;
755
+ readonly value: Schema.String;
756
+ }>>;
757
+ readonly items: Schema.optionalKey<Schema.$Array<Schema.Struct<{
758
+ readonly label: Schema.String;
759
+ readonly text: Schema.optionalKey<Schema.String>;
760
+ readonly code: Schema.optionalKey<Schema.String>;
761
+ }>>>;
762
+ readonly note: Schema.optionalKey<Schema.String>;
763
+ }>>>;
724
764
  readonly footer: Schema.optionalKey<Schema.String>;
725
765
  readonly replyTo: Schema.optionalKey<Schema.String>;
726
766
  }>, {
@@ -9,8 +9,9 @@ import { RuntimeDeployment } from "../runtime/deployment.js";
9
9
  import { nameToWire } from "../authoring-names.js";
10
10
  import { bundledNavigationCatalog } from "../navigation-artifact.js";
11
11
  import { parsePageCatalog } from "../navigation-catalog.js";
12
- import { signUpOriginsOf } from "../manifest.js";
12
+ import { guestOf, signUpOriginsOf } from "../manifest.js";
13
13
  import { isAllowed } from "../permissions.js";
14
+ import { agentToolsFor } from "../runtime/guests.js";
14
15
  import { sqlServiceRuntime } from "../runtime/service-binding.js";
15
16
  import { neonSignInLinks, segmentstreamSignInLinks } from "../runtime/sign-in-links.js";
16
17
  import { memberFileRoutes } from "./member-file-routes.js";
@@ -350,13 +351,19 @@ export function createOntologyService(ontology, config) {
350
351
  ErrorResponsesLive,
351
352
  storageMaintenanceAuthenticationLayer(config.storageMaintenanceKey),
352
353
  ]));
353
- const visitorOrigins = signUpOriginsOf(ontology.manifest);
354
+ // The websites whose visitors are guests, and those whose visitors still sign up on web.
355
+ // @legacy ontology.sign-up-web
356
+ const visitorWebsites = {
357
+ guests: guestOf(ontology.manifest)?.origins ?? [],
358
+ signUp: signUpOriginsOf(ontology.manifest),
359
+ };
360
+ const visitors = visitorWebsites.guests.length > 0 || visitorWebsites.signUp.length > 0;
354
361
  const oauth = preview
355
362
  ? Layer.empty
356
363
  : oauthLayer({ verifyToken, refreshReuseGraceMs: config.oauthRefreshReuseGraceMs });
357
364
  const mcp = config.mcp === false
358
365
  ? // A website's visitors renew their agent tokens at the OAuth token endpoint.
359
- visitorOrigins.length > 0
366
+ visitors
360
367
  ? oauth
361
368
  : Layer.empty
362
369
  : Layer.mergeAll(mcpLayer({
@@ -375,13 +382,16 @@ export function createOntologyService(ontology, config) {
375
382
  workspaceId: config.workspaceId,
376
383
  decide: config.decisions,
377
384
  }), oauth);
385
+ // Every answer that tells the agent whom it acts for names the agent's own tools they hold.
386
+ const agentTools = (principal) => agentToolsFor(ontology, principal);
378
387
  const agent = preview
379
388
  ? Layer.empty
380
389
  : agentLayer({
381
390
  verifyToken,
382
391
  verifyRuntime,
383
392
  exchangeSession,
384
- visitorOrigins,
393
+ visitorWebsites,
394
+ agentTools,
385
395
  });
386
396
  const agentTasks = preview
387
397
  ? Layer.empty
@@ -390,6 +400,7 @@ export function createOntologyService(ontology, config) {
390
400
  verifyRuntime,
391
401
  workspaceId: config.workspaceId,
392
402
  engine: config.workflowEngine ?? vercelWorkflowEngine,
403
+ agentTools,
393
404
  });
394
405
  const attachments = preview
395
406
  ? Layer.empty
package/dist/sign-in.d.ts CHANGED
@@ -12,8 +12,9 @@ export declare const CHANNEL_PROVIDERS: readonly ["telegram"];
12
12
  export type ChannelProvider = (typeof CHANNEL_PROVIDERS)[number];
13
13
  /**
14
14
  * The channels a stranger may sign up through. `email` is sign-in by email code in the app; `web`
15
- * is a visitor who writes to the agent from one of the websites `signUp.origins` names; the others
16
- * are a first private message to the agent, or opening the Telegram Mini App.
15
+ * is a visitor who writes to the agent from one of the websites `signUp.origins` names, which
16
+ * `guest` supersedes; the others are a first private message to the agent, or opening the
17
+ * Telegram Mini App.
17
18
  */
18
19
  export declare const SIGN_UP_CHANNELS: readonly ["email", "telegram", "whatsapp", "web"];
19
20
  export type SignUpChannel = (typeof SIGN_UP_CHANNELS)[number];
@@ -32,9 +33,9 @@ export interface SignUpOptions<R = unknown> {
32
33
  /** Roles for a listed channel that differs from `roles`. */
33
34
  readonly byChannel?: Readonly<Partial<Record<SignUpChannel, ReadonlyArray<R>>>>;
34
35
  /**
35
- * The websites whose visitors may sign up on `web`, by origin, such as
36
- * `https://www.example.com`: the agent's sign-up answers a browser on one of them. Required with
37
- * `web`; `http` only for `localhost` and `127.0.0.1`.
36
+ * The websites whose visitors may sign up on `web`, as {@link websiteOrigin} accepts them, such
37
+ * as `https://www.example.com` or `https://*.example.com`: the agent's sign-up answers a browser
38
+ * on one of them. Required with `web`.
38
39
  */
39
40
  readonly origins?: ReadonlyArray<string>;
40
41
  }
@@ -44,12 +45,58 @@ export interface ResolvedSignUp {
44
45
  /** The websites whose visitors may sign up on `web`, by origin. */
45
46
  readonly origins?: ReadonlyArray<string>;
46
47
  }
48
+ /** The channels a guest talks to the agent through: only `web`, a website's chat, so far. */
49
+ export declare const GUEST_CHANNELS: readonly ["web"];
50
+ export type GuestChannel = (typeof GUEST_CHANNELS)[number];
51
+ export declare function isGuestChannel(value: unknown): value is GuestChannel;
47
52
  /**
48
- * The origin a website's address declares, when it is one `signUp.origins` accepts: the exact
49
- * origin of an `https` address, or of an `http` address on `localhost` or `127.0.0.1`, written as
50
- * the origin alone, without a path.
53
+ * Who may talk to the agent without signing in or signing up: a guest, the counterpart of the
54
+ * built-in `member`. A guest is a session, not a user: nobody proved who they are, so no user or
55
+ * identity record is created for them, and they hold the built-in role `guest` alone. Without
56
+ * `guest`, nobody chats without signing in or signing up.
51
57
  */
52
- export declare function signUpOrigin(value: unknown): string | undefined;
58
+ export interface GuestOptions<P = unknown> {
59
+ /** Where guests talk to the agent: `["web"]`, a declared website's chat. */
60
+ readonly channels: ReadonlyArray<GuestChannel>;
61
+ /**
62
+ * The websites whose visitors are guests, as {@link websiteOrigin} accepts them, such as
63
+ * `https://www.example.com` or `https://*.example.com`.
64
+ */
65
+ readonly origins: ReadonlyArray<string>;
66
+ /**
67
+ * What every guest may do, as role permissions: the permissions of the built-in role `guest`.
68
+ * `check` holds them to the rules of sign-up roles, and a guest has no user record, so a scope
69
+ * cannot use `me`. `[]` lets guests talk to the agent and nothing more.
70
+ */
71
+ readonly permissions: ReadonlyArray<P>;
72
+ }
73
+ /** Guests as the manifest carries them; the built-in role `guest` holds their permissions. */
74
+ export interface ResolvedGuest {
75
+ readonly channels: ReadonlyArray<GuestChannel>;
76
+ readonly origins: ReadonlyArray<string>;
77
+ }
78
+ /**
79
+ * A website's origin as `guest.origins` and `signUp.origins` accept it, written as an origin alone,
80
+ * without a path, and lowercase:
81
+ *
82
+ * - the exact origin of an `https` address, such as `https://www.example.com`;
83
+ * - the exact origin of an `http` address on `localhost` or `127.0.0.1`, for development;
84
+ * - a pattern whose leftmost label is `*`, under a domain of at least two labels that is not a
85
+ * shared second-level domain such as `co.uk`: `https://*.example.com` or
86
+ * `https://*.vercel.app`. `*` stands for exactly one whole label: the pattern matches
87
+ * `https://www.example.com` and `https://pr-12.example.com`, not `https://example.com` or
88
+ * `https://a.b.example.com`. A pattern under a domain others share, such as `vercel.app`,
89
+ * admits their websites too.
90
+ */
91
+ export declare function websiteOrigin(value: unknown): string | undefined;
92
+ /** @deprecated Use {@link websiteOrigin}, which `signUp.origins` and `guest.origins` share. */
93
+ export declare const signUpOrigin: typeof websiteOrigin;
94
+ /**
95
+ * Whether a browser's `Origin` is one of the declared websites, each an origin
96
+ * {@link websiteOrigin} accepted: equal to an exact one, or an `https` origin on the default port
97
+ * whose host is one label and a pattern's domain.
98
+ */
99
+ export declare function matchesWebsite(declared: ReadonlyArray<string>, origin: unknown): boolean;
53
100
  /** How people become members of the app. Email and channel admission are independent. */
54
101
  export interface SignInOptions {
55
102
  readonly policy: SignInPolicy;
@@ -64,10 +111,20 @@ export interface SignInOptions {
64
111
  * person to confirm. The link signs in whichever browser opens it.
65
112
  */
66
113
  readonly codeLink?: boolean;
114
+ /**
115
+ * One of the ontology's `emails`, by its camelCase name, that the platform sends instead of its
116
+ * own invitation: when a member is invited from the CLI or the console, and when
117
+ * `User.sendSignInLink` names no template for a member who has not signed in yet. It may use only
118
+ * the built-in values, such as `{{app.mcpUrl}}`, since nothing fills others. The platform's own
119
+ * invitation when omitted. A checked ontology holds the email's wire name.
120
+ */
121
+ readonly invitationEmail?: string;
67
122
  }
68
123
  /** The sign-in options of a checked ontology, with the sign-up its definition declares. */
69
124
  export interface ResolvedSignInOptions extends SignInOptions {
70
125
  readonly signUp?: ResolvedSignUp;
126
+ /** Where guests talk to the agent, when the definition declares `guest`. */
127
+ readonly guest?: ResolvedGuest;
71
128
  /** Wire name of the idempotent, input-free action run as each user created by sign-up. */
72
129
  readonly onSignUp?: string;
73
130
  }
package/dist/sign-in.js CHANGED
@@ -11,8 +11,9 @@ export const DEFAULT_SIGN_IN_POLICY = "invited";
11
11
  export const CHANNEL_PROVIDERS = ["telegram"];
12
12
  /**
13
13
  * The channels a stranger may sign up through. `email` is sign-in by email code in the app; `web`
14
- * is a visitor who writes to the agent from one of the websites `signUp.origins` names; the others
15
- * are a first private message to the agent, or opening the Telegram Mini App.
14
+ * is a visitor who writes to the agent from one of the websites `signUp.origins` names, which
15
+ * `guest` supersedes; the others are a first private message to the agent, or opening the
16
+ * Telegram Mini App.
16
17
  */
17
18
  export const SIGN_UP_CHANNELS = ["email", "telegram", "whatsapp", "web"];
18
19
  /** The sign-up channels a person reaches through a chat, which need a matching `chat` adapter. */
@@ -20,25 +21,102 @@ export const SIGN_UP_CHAT_CHANNELS = ["telegram", "whatsapp"];
20
21
  export function isSignUpChannel(value) {
21
22
  return SIGN_UP_CHANNELS.includes(value);
22
23
  }
24
+ /** The channels a guest talks to the agent through: only `web`, a website's chat, so far. */
25
+ export const GUEST_CHANNELS = ["web"];
26
+ export function isGuestChannel(value) {
27
+ return GUEST_CHANNELS.includes(value);
28
+ }
29
+ /** A DNS label, lowercase, as a browser serializes it in an origin. */
30
+ const DNS_LABEL = /^(?!-)[a-z0-9-]{1,63}(?<!-)$/;
31
+ /** A top-level domain: letters, or an internationalized name in its ASCII form. */
32
+ const TOP_LEVEL_LABEL = /^(?:[a-z]{2,63}|xn--[a-z0-9-]{1,59})$/;
33
+ /**
34
+ * Second-level labels that registries share under a country's domain, such as `co.uk`: a pattern
35
+ * directly under one would admit every company's website there.
36
+ */
37
+ const SHARED_SECOND_LEVEL = new Set([
38
+ "ac",
39
+ "co",
40
+ "com",
41
+ "edu",
42
+ "go",
43
+ "gov",
44
+ "ne",
45
+ "net",
46
+ "or",
47
+ "org",
48
+ ]);
49
+ /** The domain under the `*` of an origin pattern, when the pattern is one websites may declare. */
50
+ function patternSuffix(value) {
51
+ const match = /^https:\/\/\*\.([a-z0-9.-]+)$/.exec(value);
52
+ const labels = match?.[1]?.split(".");
53
+ if (labels === undefined ||
54
+ labels.length < 2 ||
55
+ !labels.every((label) => DNS_LABEL.test(label)) ||
56
+ !TOP_LEVEL_LABEL.test(labels.at(-1)) ||
57
+ (labels.length === 2 && labels[1].length === 2 && SHARED_SECOND_LEVEL.has(labels[0]))) {
58
+ return undefined;
59
+ }
60
+ return labels.join(".");
61
+ }
23
62
  /**
24
- * The origin a website's address declares, when it is one `signUp.origins` accepts: the exact
25
- * origin of an `https` address, or of an `http` address on `localhost` or `127.0.0.1`, written as
26
- * the origin alone, without a path.
63
+ * A website's origin as `guest.origins` and `signUp.origins` accept it, written as an origin alone,
64
+ * without a path, and lowercase:
65
+ *
66
+ * - the exact origin of an `https` address, such as `https://www.example.com`;
67
+ * - the exact origin of an `http` address on `localhost` or `127.0.0.1`, for development;
68
+ * - a pattern whose leftmost label is `*`, under a domain of at least two labels that is not a
69
+ * shared second-level domain such as `co.uk`: `https://*.example.com` or
70
+ * `https://*.vercel.app`. `*` stands for exactly one whole label: the pattern matches
71
+ * `https://www.example.com` and `https://pr-12.example.com`, not `https://example.com` or
72
+ * `https://a.b.example.com`. A pattern under a domain others share, such as `vercel.app`,
73
+ * admits their websites too.
27
74
  */
28
- export function signUpOrigin(value) {
29
- if (typeof value !== "string" || !URL.canParse(value)) {
75
+ export function websiteOrigin(value) {
76
+ if (typeof value !== "string") {
77
+ return undefined;
78
+ }
79
+ if (value.startsWith("https://*.")) {
80
+ return patternSuffix(value) === undefined ? undefined : value;
81
+ }
82
+ if (!URL.canParse(value)) {
30
83
  return undefined;
31
84
  }
32
85
  const url = new URL(value);
33
86
  const local = url.hostname === "localhost" || url.hostname === "127.0.0.1";
34
- // The service compares a browser's origin exactly, so a pattern such as `*.example.com` would
35
- // match nothing.
36
87
  return (url.protocol === "https:" || (url.protocol === "http:" && local)) &&
37
88
  !url.hostname.includes("*") &&
38
89
  url.origin === value
39
90
  ? url.origin
40
91
  : undefined;
41
92
  }
93
+ // @legacy ontology.sign-up-origin-name
94
+ /** @deprecated Use {@link websiteOrigin}, which `signUp.origins` and `guest.origins` share. */
95
+ export const signUpOrigin = websiteOrigin;
96
+ /**
97
+ * Whether a browser's `Origin` is one of the declared websites, each an origin
98
+ * {@link websiteOrigin} accepted: equal to an exact one, or an `https` origin on the default port
99
+ * whose host is one label and a pattern's domain.
100
+ */
101
+ export function matchesWebsite(declared, origin) {
102
+ if (typeof origin !== "string" || origin === "") {
103
+ return false;
104
+ }
105
+ return declared.some((candidate) => {
106
+ if (candidate === origin) {
107
+ return true;
108
+ }
109
+ const suffix = candidate.startsWith("https://*.") ? patternSuffix(candidate) : undefined;
110
+ if (suffix === undefined || !origin.startsWith("https://")) {
111
+ return false;
112
+ }
113
+ const host = origin.slice("https://".length);
114
+ const label = host.endsWith(`.${suffix}`)
115
+ ? host.slice(0, host.length - suffix.length - 1)
116
+ : undefined;
117
+ return label !== undefined && DNS_LABEL.test(label);
118
+ });
119
+ }
42
120
  /**
43
121
  * How a person signs in with an address. `code`: a six-digit code, for a member who has signed in
44
122
  * with Neon Auth before, so their Neon Auth account exists. `link`: an emailed sign-in link, for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.106.3",
3
+ "version": "0.108.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",