@segmentstream/ontology 0.109.0 → 0.110.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 (57) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +21 -0
  3. package/dist/analyze.js +55 -3
  4. package/dist/index.d.ts +5 -5
  5. package/dist/index.js +2 -2
  6. package/dist/manifest.d.ts +94 -5
  7. package/dist/manifest.js +41 -6
  8. package/dist/migrate/apply.d.ts +4 -3
  9. package/dist/migrate/apply.js +8 -3
  10. package/dist/migrate/model.d.ts +46 -2
  11. package/dist/migrate/plan.js +1 -1
  12. package/dist/migrate/preview.js +2 -1
  13. package/dist/migrate/storage.d.ts +13 -1
  14. package/dist/migrate/storage.js +33 -1
  15. package/dist/runtime/agent-counters.d.ts +36 -0
  16. package/dist/runtime/agent-counters.js +84 -0
  17. package/dist/runtime/agent-web.d.ts +1 -1
  18. package/dist/runtime/agent-web.js +175 -3
  19. package/dist/runtime/batch.js +4 -6
  20. package/dist/runtime/history.d.ts +24 -0
  21. package/dist/runtime/history.js +35 -1
  22. package/dist/runtime/pipeline.js +3 -0
  23. package/dist/runtime/principals.d.ts +6 -0
  24. package/dist/runtime/principals.js +25 -6
  25. package/dist/runtime/reads.js +9 -2
  26. package/dist/runtime/sign-in-links.d.ts +10 -0
  27. package/dist/runtime/sign-in-links.js +26 -25
  28. package/dist/runtime/sql-runtime.d.ts +1 -1
  29. package/dist/runtime/sql-runtime.js +3 -2
  30. package/dist/runtime/writes.js +4 -6
  31. package/dist/runtime.d.ts +18 -0
  32. package/dist/service/agent/authentication.d.ts +3 -2
  33. package/dist/service/agent/authentication.js +2 -11
  34. package/dist/service/agent/bot-check.d.ts +42 -0
  35. package/dist/service/agent/bot-check.js +32 -0
  36. package/dist/service/agent/guest-sign-up.d.ts +140 -0
  37. package/dist/service/agent/guest-sign-up.js +545 -0
  38. package/dist/service/agent/protocol.d.ts +33 -1
  39. package/dist/service/agent/protocol.js +41 -1
  40. package/dist/service/agent/server.d.ts +17 -0
  41. package/dist/service/agent/server.js +42 -3
  42. package/dist/service/agent/web-tokens.d.ts +21 -3
  43. package/dist/service/agent/web-tokens.js +79 -6
  44. package/dist/service/authentication.d.ts +8 -1
  45. package/dist/service/authentication.js +26 -5
  46. package/dist/service/config.d.ts +10 -0
  47. package/dist/service/operations.d.ts +5 -0
  48. package/dist/service/runtime-contract.d.ts +124 -1
  49. package/dist/service/runtime-identity.d.ts +23 -0
  50. package/dist/service/runtime-identity.js +31 -0
  51. package/dist/service/schemas.d.ts +46 -2
  52. package/dist/service/service.js +27 -1
  53. package/dist/service/webhooks.d.ts +15 -0
  54. package/dist/service/webhooks.js +15 -4
  55. package/dist/sign-in.d.ts +29 -0
  56. package/dist/sign-in.js +8 -0
  57. package/package.json +1 -1
@@ -0,0 +1,140 @@
1
+ import { Effect } from "effect";
2
+ import { HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
3
+ import { type SessionExchange, type TokenVerifier } from "../authentication.js";
4
+ import { RequestId } from "../errors.js";
5
+ import type { AgentWebStore, OAuthStore, SignInIdentity } from "../runtime-contract.js";
6
+ /**
7
+ * A guest becomes a user by proving an email address, as anyone signs up by email in the app,
8
+ * through the app's own email sign-in. The website's chat asks for an address, and the ontology
9
+ * has the app's sign-in email it: the six-digit code to an address whose Neon Auth account
10
+ * receives codes, else a sign-in link, which also creates the account. Either way the answer is
11
+ * the same, so it reveals nothing about the address.
12
+ *
13
+ * - The code comes back from the website's chat; the ontology checks it with the app's sign-in and
14
+ * verifies the session it opens as it verifies any sign-in token.
15
+ * - The link signs in the browser that opens it on the app's origin, then lands on the ontology's
16
+ * `GET /api/ontology/agent/guests/link?nonce=…`, which reads that session, admits the user and
17
+ * returns to the guest's website with a single-use handoff in the fragment. Only the guest who
18
+ * asked for the link may take it, so a link opened in another browser signs the person up and
19
+ * hands nothing to that browser: someone who types another person's address into a website's
20
+ * chat never gets their account.
21
+ *
22
+ * Once the address is proven the guest becomes the user `signUp` created with it, or a new one,
23
+ * and their grant to the agent becomes that user's. Every refusal is a code alone: the website
24
+ * says it in its own words.
25
+ */
26
+ /** Sign-in emails one guest may ask for an hour, and one address may be sent from guests. */
27
+ export declare const GUEST_EMAIL_CODES_PER_HOUR = 5;
28
+ /** Codes and handoffs one guest may try an hour; each code also allows only a few tries. */
29
+ export declare const GUEST_SIGN_UP_ATTEMPTS_PER_HOUR = 10;
30
+ /** Where an emailed link lands once it signed its browser in on the app's origin. */
31
+ export declare const GUEST_LINK_PATH = "/api/ontology/agent/guests/link";
32
+ /** How long a link works: as long as the app's sign-in links. */
33
+ export declare const GUEST_LINK_TTL_MS: number;
34
+ /** How long the handoff of an opened link waits for the guest's browser. */
35
+ export declare const GUEST_HANDOFF_TTL_MS: number;
36
+ /** The fragment key that carries a handoff to the guest's website, and the one for a refusal. */
37
+ export declare const GUEST_HANDOFF_FRAGMENT = "segmentstream_guest";
38
+ export declare const GUEST_ERROR_FRAGMENT = "segmentstream_guest_error";
39
+ /**
40
+ * The app's email sign-in could not answer: `cause` is the failure or the step that failed, and
41
+ * `httpStatus` Neon Auth's answer when it gave one. Operators read them in the log under the
42
+ * request's id; the guest only reads `email_unavailable`.
43
+ */
44
+ export interface EmailUnavailable {
45
+ readonly status: "unavailable";
46
+ readonly cause: unknown;
47
+ readonly httpStatus?: number;
48
+ }
49
+ /** What asking for a sign-in email answered. */
50
+ export type EmailCodeSend = {
51
+ readonly status: "sent" | "invalid" | "limited";
52
+ } | EmailUnavailable;
53
+ /** What checking a code answered: the identity it proved, or why not. */
54
+ export type EmailCodeCheck = {
55
+ readonly status: "verified";
56
+ readonly identity: SignInIdentity;
57
+ } | {
58
+ readonly status: "invalid";
59
+ } | {
60
+ readonly status: "limited";
61
+ } | EmailUnavailable;
62
+ /**
63
+ * The app's email sign-in, from the ontology's side: send an address a code, or a sign-in link to
64
+ * `callbackPath` on the app's production origin, and check a code.
65
+ */
66
+ export interface EmailCodes {
67
+ readonly send: (email: string) => Promise<EmailCodeSend>;
68
+ readonly sendLink: (email: string, callbackPath: string) => Promise<EmailCodeSend>;
69
+ readonly verify: (email: string, code: string) => Promise<EmailCodeCheck>;
70
+ }
71
+ /**
72
+ * {@link EmailCodes} through the app's Neon Auth, server to server, as the app's sign-in page calls
73
+ * it, with the app's production origin, which Neon Auth trusts: the address gets the app's own
74
+ * sign-in email. A checked code opens a Neon Auth session, whose token the ontology verifies like
75
+ * any sign-in token, and the session is closed again: the browser holds none. A link is the app's
76
+ * sign-in link, which the platform's email webhook sends as it sends every one.
77
+ */
78
+ export declare function neonEmailCodes(config: {
79
+ readonly baseUrl: string;
80
+ readonly origin: string;
81
+ readonly verifyToken: TokenVerifier;
82
+ readonly fetch?: typeof fetch;
83
+ }): EmailCodes;
84
+ /**
85
+ * The page of a guest website a link returns to, without its fragment: an `https` address, or
86
+ * `http` on `localhost` and `127.0.0.1`, on one of the guests' websites. `undefined` for anything
87
+ * else.
88
+ */
89
+ export declare function guestReturnTo(value: string, origins: ReadonlyArray<string>): string | undefined;
90
+ export interface GuestSignUpOptions {
91
+ /** Whether `signUp` lists `email`, which a guest signs up on. */
92
+ readonly emailSignUp: boolean;
93
+ readonly web: AgentWebStore;
94
+ readonly oauth: OAuthStore;
95
+ /** The websites whose visitors are guests. */
96
+ readonly origins: ReadonlyArray<string>;
97
+ /** The app's email sign-in; `undefined` where the ontology cannot send its emails. */
98
+ readonly emailCodes: EmailCodes | undefined;
99
+ /** Turns the app's session cookie a link set into its token. */
100
+ readonly exchangeSession: SessionExchange | undefined;
101
+ readonly verifyToken: TokenVerifier;
102
+ }
103
+ type Answer = Effect.Effect<HttpServerResponse.HttpServerResponse, never, RequestId>;
104
+ /**
105
+ * The routes a website's guest uses to become a user. The first two take the guest's agent token
106
+ * as their bearer, from a declared website:
107
+ *
108
+ * - `POST /api/ontology/agent/guests/email-code` with `{ email, return_to? }` has the app's sign-in
109
+ * email the address a code or a link, and answers `{ sent: true }` either way. `return_to` is the
110
+ * page of a guest website the link returns to.
111
+ * - `POST /api/ontology/agent/guests/sign-up` with `{ email, code }`, or with the `{ handoff }` an
112
+ * opened link gave the browser, makes the guest that user and answers
113
+ * `{ token, expires_at, refresh_token, chat, user_id, created }`: the user's agent token and
114
+ * refresh token, of the grant that was the guest's, and the user's own chat.
115
+ * - `GET /api/ontology/agent/guests/link?nonce=…` is where the link lands, signed in on the app's
116
+ * origin: it admits the user and redirects to `return_to` with `#segmentstream_guest=<handoff>`,
117
+ * or `#segmentstream_guest_error=<code>`.
118
+ *
119
+ * Refusals: `email_sign_up_closed` when `signUp` does not list `email`, `member_address` for an
120
+ * address of a user sign-up did not create, `code_invalid`, `link_invalid`, `handoff_invalid`,
121
+ * `handoff_other_browser` for a handoff minted for another guest, `rate_limited` with `Retry-After`,
122
+ * `email_unavailable` when the app's email sign-in cannot be reached, and `forbidden` for a guest
123
+ * who is gone.
124
+ */
125
+ export declare function guestSignUpRoutes(options: GuestSignUpOptions): {
126
+ emailCode: (request: HttpServerRequest.HttpServerRequest) => Answer;
127
+ signUp: (request: HttpServerRequest.HttpServerRequest) => Answer;
128
+ link: (request: HttpServerRequest.HttpServerRequest) => Answer;
129
+ };
130
+ /**
131
+ * `OPTIONS` of the guests' routes: lets a browser on one of the guests' websites call them with
132
+ * the guest's agent token; any other origin gets no permission.
133
+ */
134
+ export declare function guestRoutesPreflight(origins: ReadonlyArray<string>): (request: HttpServerRequest.HttpServerRequest) => Effect.Effect<HttpServerResponse.HttpServerResponse, never, never>;
135
+ /**
136
+ * `POST /api/ontology/agent/guests/usage` (runtime): adds the model cost of a guest's turn to the
137
+ * guests' spend, once per `key`, and answers 204.
138
+ */
139
+ export declare function recordGuestUsage(web: AgentWebStore): (request: HttpServerRequest.HttpServerRequest) => Answer;
140
+ export {};