@stigmer/react-core 0.4.0 → 0.4.2

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 (70) hide show
  1. package/agent/appUser.d.ts +18 -0
  2. package/agent/appUser.d.ts.map +1 -0
  3. package/agent/appUser.js +26 -0
  4. package/agent/appUser.js.map +1 -0
  5. package/index.d.ts +5 -1
  6. package/index.d.ts.map +1 -1
  7. package/index.js +3 -1
  8. package/index.js.map +1 -1
  9. package/internal/useKeyedState.d.ts +6 -0
  10. package/internal/useKeyedState.d.ts.map +1 -0
  11. package/internal/useKeyedState.js +56 -0
  12. package/internal/useKeyedState.js.map +1 -0
  13. package/internal.d.ts +6 -0
  14. package/internal.d.ts.map +1 -1
  15. package/internal.js +5 -0
  16. package/internal.js.map +1 -1
  17. package/models/registry.d.ts +12 -0
  18. package/models/registry.d.ts.map +1 -1
  19. package/models/registry.js +17 -1
  20. package/models/registry.js.map +1 -1
  21. package/package.json +3 -3
  22. package/run/useAnswerCustomToolUse.d.ts +3 -1
  23. package/run/useAnswerCustomToolUse.d.ts.map +1 -1
  24. package/run/useAnswerCustomToolUse.js +9 -4
  25. package/run/useAnswerCustomToolUse.js.map +1 -1
  26. package/run/useAnswerToolUse.d.ts +16 -1
  27. package/run/useAnswerToolUse.d.ts.map +1 -1
  28. package/run/useAnswerToolUse.js +29 -9
  29. package/run/useAnswerToolUse.js.map +1 -1
  30. package/run/useCreateRun.d.ts +3 -1
  31. package/run/useCreateRun.d.ts.map +1 -1
  32. package/run/useCreateRun.js +11 -7
  33. package/run/useCreateRun.js.map +1 -1
  34. package/run/useReviewFileChanges.d.ts +18 -2
  35. package/run/useReviewFileChanges.d.ts.map +1 -1
  36. package/run/useReviewFileChanges.js +21 -7
  37. package/run/useReviewFileChanges.js.map +1 -1
  38. package/run/useRunActions.d.ts +10 -3
  39. package/run/useRunActions.d.ts.map +1 -1
  40. package/run/useRunActions.js +22 -18
  41. package/run/useRunActions.js.map +1 -1
  42. package/session/useSessionConversation.d.ts.map +1 -1
  43. package/session/useSessionConversation.js +10 -6
  44. package/session/useSessionConversation.js.map +1 -1
  45. package/src/agent/appUser.ts +26 -0
  46. package/src/index.ts +5 -0
  47. package/src/internal/useKeyedState.ts +70 -0
  48. package/src/internal.ts +6 -0
  49. package/src/models/registry.ts +19 -1
  50. package/src/run/useAnswerCustomToolUse.ts +9 -4
  51. package/src/run/useAnswerToolUse.ts +33 -9
  52. package/src/run/useCreateRun.ts +11 -7
  53. package/src/run/useReviewFileChanges.ts +29 -8
  54. package/src/run/useRunActions.ts +43 -25
  55. package/src/session/useSessionConversation.ts +10 -6
  56. package/src/tool-tokens/ToolTokensProvider.tsx +176 -0
  57. package/src/tool-tokens/tool-tokens.ts +114 -0
  58. package/src/vault/address.ts +54 -0
  59. package/tool-tokens/ToolTokensProvider.d.ts +44 -0
  60. package/tool-tokens/ToolTokensProvider.d.ts.map +1 -0
  61. package/tool-tokens/ToolTokensProvider.js +135 -0
  62. package/tool-tokens/ToolTokensProvider.js.map +1 -0
  63. package/tool-tokens/tool-tokens.d.ts +68 -0
  64. package/tool-tokens/tool-tokens.d.ts.map +1 -0
  65. package/tool-tokens/tool-tokens.js +73 -0
  66. package/tool-tokens/tool-tokens.js.map +1 -0
  67. package/vault/address.d.ts +24 -0
  68. package/vault/address.d.ts.map +1 -0
  69. package/vault/address.js +57 -0
  70. package/vault/address.js.map +1 -0
@@ -0,0 +1,135 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ // The provider behind `AgentChat`'s `toolTokens`, and the same seam for a
4
+ // chat built on react-core's hooks alone: it binds the host's token
5
+ // functions to the organization the conversation runs in and gives the
6
+ // hooks below one `refresh` (./tool-tokens.ts says why a write, and why
7
+ // before each request).
8
+ //
9
+ // What it decides once, not on every send:
10
+ // - which keys are addresses: each key is normalized by the server's rule
11
+ // when the keys change; a key the server would refuse is named in the
12
+ // console and never written;
13
+ // - whether the signed-in account is an app user, kept once it is: only an
14
+ // app user's own turns read these logins, so for anyone else (a person
15
+ // signed in to Stigmer itself, testing the page) the console says so once
16
+ // and nothing is written to their own vault. A "no" is asked again at the
17
+ // next send, since a failed read answers "no" too, and one failure must
18
+ // never stop the writes for the rest of the page's life.
19
+ //
20
+ // What it does on every send: calls every function (the latest the host
21
+ // rendered), then writes every token. Nothing is cached in the page: a
22
+ // login removed elsewhere (settings, another tab) is written again by the
23
+ // next send rather than silently skipped.
24
+ import { useCallback, useContext, useMemo, useRef } from "react";
25
+ import { isAppUser } from "../agent/appUser.js";
26
+ import { StigmerContext } from "../context.js";
27
+ import { normalizeAddress } from "../vault/address.js";
28
+ import { ToolTokensContext, ToolTokenUnavailableError, } from "./tool-tokens.js";
29
+ /**
30
+ * Hands the signed-in user's own token for each of your tool servers to
31
+ * every conversation below, before every message and every answer that
32
+ * resumes a turn. `AgentChat` mounts it for its `toolTokens`; mount it
33
+ * yourself above a chat you build on react-core's hooks.
34
+ *
35
+ * Only an app user's turns read these tokens (a user your backend signs in
36
+ * to Stigmer), each only for the tool server at its address and only for
37
+ * their own conversations. Each token is kept sealed in that user's own My
38
+ * vault in `org`, replaced by the next send and never renewed. A function
39
+ * that throws or gives no token fails the send with
40
+ * {@link ToolTokenUnavailableError}, and nothing is sent.
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * import type { ReactNode } from "react";
45
+ *
46
+ * declare const auth: { getAccessToken(): Promise<string> };
47
+ *
48
+ * // Above your own chat, built on useCreateRun and the answer hooks.
49
+ * function SupportChat({ children }: { children: ReactNode }) {
50
+ * return (
51
+ * <ToolTokensProvider org="acme" tokens={{ "https://api.acme.com/mcp": () => auth.getAccessToken() }}>
52
+ * {children}
53
+ * </ToolTokensProvider>
54
+ * );
55
+ * }
56
+ * ```
57
+ */
58
+ export function ToolTokensProvider({ org, tokens, children }) {
59
+ // Nullable: a chat drawn outside a client provider (a gallery, a test)
60
+ // still renders; with no client nothing can be sent, so nothing is written.
61
+ const stigmer = useContext(StigmerContext);
62
+ // The functions the host rendered last: a host passes a new object (and
63
+ // new functions) on every render, and a send calls the current ones.
64
+ const tokensRef = useRef(tokens);
65
+ tokensRef.current = tokens;
66
+ // Keyed by the keys alone, so a new object with the same servers keeps
67
+ // the same addresses (and the setup that reads them does not run again).
68
+ const keysKey = Object.keys(tokens).join("\n");
69
+ const entries = useMemo(() => entriesOf(keysKey === "" ? [] : keysKey.split("\n")), [keysKey]);
70
+ const addresses = useMemo(() => new Set(entries.map((entry) => entry.address)), [entries]);
71
+ // The client that answered "app user"; anything else is asked again.
72
+ const appUserRef = useRef(null);
73
+ const toldNotAppUserRef = useRef(false);
74
+ const refresh = useCallback(async () => {
75
+ if (entries.length === 0 || stigmer === null)
76
+ return;
77
+ if (appUserRef.current !== stigmer) {
78
+ if (await isAppUser(stigmer))
79
+ appUserRef.current = stigmer;
80
+ }
81
+ if (appUserRef.current !== stigmer) {
82
+ if (!toldNotAppUserRef.current) {
83
+ toldNotAppUserRef.current = true;
84
+ console.warn("Stigmer: toolTokens is set, but the signed-in account is not an app user (a user your backend signs in to Stigmer). " +
85
+ "Only an app user's own turns read these tokens, so none is written.");
86
+ }
87
+ return;
88
+ }
89
+ const current = tokensRef.current;
90
+ // Every token first, so a function that fails sends nothing at all.
91
+ const read = await Promise.all(entries.map(async (entry) => ({ address: entry.address, token: await tokenOf(entry.key, current[entry.key]) })));
92
+ await Promise.all(read.map(({ address, token }) => stigmer.vault.setConnection({ vault: { org, vault: { case: "mine", value: true } }, address, token })));
93
+ }, [stigmer, org, entries]);
94
+ const value = useMemo(() => ({ addresses, refresh }), [addresses, refresh]);
95
+ return _jsx(ToolTokensContext.Provider, { value: value, children: children });
96
+ }
97
+ /**
98
+ * The host's keys the address rule accepts, each once. A key the server
99
+ * would refuse, or a second key for an address already keyed, is named in
100
+ * the console and left out.
101
+ */
102
+ function entriesOf(keys) {
103
+ const byAddress = new Map();
104
+ for (const key of keys) {
105
+ const address = normalizeAddress(key);
106
+ if (address === null || !address.includes("://")) {
107
+ console.error(`Stigmer: toolTokens key "${key}" is not a tool server's address (its http or https URL, as your agent dials it); its token is never written.`);
108
+ continue;
109
+ }
110
+ const first = byAddress.get(address);
111
+ if (first !== undefined) {
112
+ console.error(`Stigmer: toolTokens keys "${first.key}" and "${key}" are one address (${address}); only "${first.key}"'s token is written.`);
113
+ continue;
114
+ }
115
+ byAddress.set(address, { key, address });
116
+ }
117
+ return [...byAddress.values()];
118
+ }
119
+ /** The token one host function gives, or {@link ToolTokenUnavailableError}. */
120
+ async function tokenOf(key, getter) {
121
+ let token;
122
+ try {
123
+ token = await getter?.();
124
+ }
125
+ catch (error) {
126
+ console.error(`Stigmer: the toolTokens function for ${key} threw, so nothing was sent.`, error);
127
+ throw new ToolTokenUnavailableError(key, error);
128
+ }
129
+ if (typeof token !== "string" || token.trim() === "") {
130
+ console.error(`Stigmer: the toolTokens function for ${key} gave no token, so nothing was sent.`);
131
+ throw new ToolTokenUnavailableError(key);
132
+ }
133
+ return token;
134
+ }
135
+ //# sourceMappingURL=ToolTokensProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ToolTokensProvider.js","sourceRoot":"","sources":["../src/tool-tokens/ToolTokensProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,0EAA0E;AAC1E,oEAAoE;AACpE,uEAAuE;AACvE,wEAAwE;AACxE,wBAAwB;AACxB,EAAE;AACF,2CAA2C;AAC3C,0EAA0E;AAC1E,wEAAwE;AACxE,+BAA+B;AAC/B,2EAA2E;AAC3E,yEAAyE;AACzE,4EAA4E;AAC5E,4EAA4E;AAC5E,0EAA0E;AAC1E,2DAA2D;AAC3D,EAAE;AACF,wEAAwE;AACxE,uEAAuE;AACvE,0EAA0E;AAC1E,0CAA0C;AAE1C,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAEjF,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AA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CAAC,wCAAwC,GAAG,8BAA8B,EAAE,KAAK,CAAC,CAAC;QAChG,MAAM,IAAI,yBAAyB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,OAAO,CAAC,KAAK,CAAC,wCAAwC,GAAG,sCAAsC,CAAC,CAAC;QACjG,MAAM,IAAI,yBAAyB,CAAC,GAAG,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Returns the signed-in user's current token for one of your tool
3
+ * servers: a string, or a promise of one. Called before every message the
4
+ * user sends and every answer that resumes a turn, so return the token as
5
+ * it is now (your auth library's `getAccessToken()`, or a fetch to your own
6
+ * backend with the user's session).
7
+ */
8
+ export type ToolTokenGetter = () => string | Promise<string>;
9
+ /**
10
+ * Your tool servers' token functions, keyed by the address your agent
11
+ * dials each server at (`"https://api.acme.com/mcp"`). An address matches
12
+ * as a saved login does: scheme and host in any case, a default port, a
13
+ * trailing slash, a query or a fragment make no difference; the path does.
14
+ */
15
+ export type ToolTokens = Readonly<Record<string, ToolTokenGetter>>;
16
+ /**
17
+ * A send that did not happen because one of the host's token functions
18
+ * threw or gave no token. Nothing was sent: not the message, not the
19
+ * answer, not any token. `message` is written for the person in front of
20
+ * the chat, who reads it where the send failed; `address` names the tool
21
+ * server whose function failed, and `cause` is what it threw, if it threw.
22
+ * The token itself is never part of it.
23
+ */
24
+ export declare class ToolTokenUnavailableError extends Error {
25
+ /** The tool server whose token function failed, as the host keyed it. */
26
+ readonly address: string;
27
+ constructor(address: string, cause?: unknown);
28
+ }
29
+ /**
30
+ * What {@link ToolTokensContext} holds: the addresses the host fills,
31
+ * normalized, and the one function that writes their current tokens.
32
+ *
33
+ * @internal The React kit's and react-core's own plumbing.
34
+ */
35
+ export interface ToolTokensValue {
36
+ /** The normalized addresses of the host's tool servers. */
37
+ readonly addresses: ReadonlySet<string>;
38
+ /**
39
+ * Calls each token function, then writes each token as the signed-in
40
+ * app user's own login at its address. Resolves at once for an account
41
+ * that is not an app user. Rejects with {@link ToolTokenUnavailableError}
42
+ * before any write when a function fails, or with the server's error when
43
+ * a write is refused.
44
+ */
45
+ readonly refresh: () => Promise<void>;
46
+ }
47
+ /**
48
+ * The host's tool tokens for the conversation below, or `null` where none
49
+ * were given.
50
+ *
51
+ * @internal Provided by `ToolTokensProvider`.
52
+ */
53
+ export declare const ToolTokensContext: import("react").Context<ToolTokensValue | null>;
54
+ /**
55
+ * The host's tool tokens from the nearest `ToolTokensProvider`, or `null`.
56
+ *
57
+ * @internal
58
+ */
59
+ export declare function useToolTokens(): ToolTokensValue | null;
60
+ /**
61
+ * Runs `send` after the host's tool tokens are written, or at once where
62
+ * none were given: the one order every hook that starts work on a turn
63
+ * keeps.
64
+ *
65
+ * @internal
66
+ */
67
+ export declare function withToolTokens<T>(tokens: ToolTokensValue | null, send: () => Promise<T>): Promise<T>;
68
+ //# sourceMappingURL=tool-tokens.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-tokens.d.ts","sourceRoot":"","sources":["../src/tool-tokens/tool-tokens.ts"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAE7D;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC;AAEnE;;;;;;;GAOG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,yEAAyE;IACzE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEb,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAO7C;AAED;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,2DAA2D;IAC3D,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACxC;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,iDAA8C,CAAC;AAE7E;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,eAAe,GAAG,IAAI,CAEtD;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAG1G"}
@@ -0,0 +1,73 @@
1
+ "use client";
2
+ // A host's tool tokens: the signed-in user's own token for each of the
3
+ // host's tool servers, written as that user's own login before every
4
+ // request that starts work on a turn.
5
+ //
6
+ // Why a write, and why before each request: a turn's values are read by
7
+ // the runner when each piece of the turn's work starts, from the vaults as
8
+ // they are at that moment (the server's `domain/vault/values.ts`), and the
9
+ // server keeps no copy between. An app user's own turn reads the logins in
10
+ // their own My vault last, each only for the tool at the login's address
11
+ // (the server's `domain/vault/resolve.ts`, its fifth source). So the
12
+ // user's current token, saved there just before a message is sent or an
13
+ // approval answered, is what the tool receives for the work that request
14
+ // starts, and no other person's turn ever reads it. A token handed with the
15
+ // message instead would have to wait on the server until the runner asked,
16
+ // which can be hours across an approval.
17
+ //
18
+ // Each write replaces the last one in place; nothing renews a token written
19
+ // this way (only a sign-in's login is renewed), so an expired token reaches
20
+ // the tool server as it is and the server's refusal is the turn's answer.
21
+ //
22
+ // The context is read by the four hooks that start work on a turn
23
+ // (`useCreateRun`, `useAnswerToolUse`, `useAnswerCustomToolUse`,
24
+ // `useRunActions`' resume and recover) and by the React kit's setup, which
25
+ // offers no sign-in for an address the host fills.
26
+ import { createContext, useContext } from "react";
27
+ /**
28
+ * A send that did not happen because one of the host's token functions
29
+ * threw or gave no token. Nothing was sent: not the message, not the
30
+ * answer, not any token. `message` is written for the person in front of
31
+ * the chat, who reads it where the send failed; `address` names the tool
32
+ * server whose function failed, and `cause` is what it threw, if it threw.
33
+ * The token itself is never part of it.
34
+ */
35
+ export class ToolTokenUnavailableError extends Error {
36
+ /** The tool server whose token function failed, as the host keyed it. */
37
+ address;
38
+ constructor(address, cause) {
39
+ super("Your sign-in to this app's tools could not be read, so nothing was sent. Try again, or sign in to the app again.", {
40
+ cause,
41
+ });
42
+ this.name = "ToolTokenUnavailableError";
43
+ this.address = address;
44
+ }
45
+ }
46
+ /**
47
+ * The host's tool tokens for the conversation below, or `null` where none
48
+ * were given.
49
+ *
50
+ * @internal Provided by `ToolTokensProvider`.
51
+ */
52
+ export const ToolTokensContext = createContext(null);
53
+ /**
54
+ * The host's tool tokens from the nearest `ToolTokensProvider`, or `null`.
55
+ *
56
+ * @internal
57
+ */
58
+ export function useToolTokens() {
59
+ return useContext(ToolTokensContext);
60
+ }
61
+ /**
62
+ * Runs `send` after the host's tool tokens are written, or at once where
63
+ * none were given: the one order every hook that starts work on a turn
64
+ * keeps.
65
+ *
66
+ * @internal
67
+ */
68
+ export async function withToolTokens(tokens, send) {
69
+ if (tokens !== null)
70
+ await tokens.refresh();
71
+ return send();
72
+ }
73
+ //# sourceMappingURL=tool-tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-tokens.js","sourceRoot":"","sources":["../src/tool-tokens/tool-tokens.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,uEAAuE;AACvE,qEAAqE;AACrE,sCAAsC;AACtC,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,2EAA2E;AAC3E,2EAA2E;AAC3E,yEAAyE;AACzE,qEAAqE;AACrE,wEAAwE;AACxE,yEAAyE;AACzE,4EAA4E;AAC5E,2EAA2E;AAC3E,yCAAyC;AACzC,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,0EAA0E;AAC1E,EAAE;AACF,kEAAkE;AAClE,iEAAiE;AACjE,2EAA2E;AAC3E,mDAAmD;AAEnD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAmBlD;;;;;;;GAOG;AACH,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD,yEAAyE;IAChE,OAAO,CAAS;IAEzB,YAAY,OAAe,EAAE,KAAe;QAC1C,KAAK,CAAC,kHAAkH,EAAE;YACxH,KAAK;SACN,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;QACxC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAqBD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAAyB,IAAI,CAAC,CAAC;AAE7E;;;;GAIG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,UAAU,CAAC,iBAAiB,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAI,MAA8B,EAAE,IAAsB;IAC5F,IAAI,MAAM,KAAK,IAAI;QAAE,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;IAC5C,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The address a vault connection is saved at, by the server's one rule
3
+ * (`domain/vault/address.ts` in the server): a tool's address is its full
4
+ * URL, with scheme and host lowercased, a default port dropped, one trailing
5
+ * slash trimmed, query and fragment dropped; only an http or https URL
6
+ * without credentials in it is one. A Git host's address is its bare host
7
+ * name, lowercased, its default port dropped (`github.com`).
8
+ *
9
+ * The server normalizes every saved address itself. This mirror lives here,
10
+ * below the React kit, because two layers match an address before anything
11
+ * is saved: `ToolTokensProvider` refuses a host's key the server would
12
+ * refuse, and the kit's setup matches a tool against the logins that fill
13
+ * it. The kit's `vault/address.ts` builds its tool and Git-host rules on it.
14
+ *
15
+ * Pinned by __tests__/address.test.ts.
16
+ */
17
+ /**
18
+ * Normalizes an address in either shape: a URL (`https://mcp.linear.app/mcp/`)
19
+ * becomes a tool address, anything else a bare host. Returns `null` for input
20
+ * the server would refuse (an unparseable URL, a scheme other than http or
21
+ * https, credentials in the URL, a placeholder, a blank).
22
+ */
23
+ export declare function normalizeAddress(input: string): string | null;
24
+ //# sourceMappingURL=address.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/vault/address.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAUH;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAgB7D"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * The address a vault connection is saved at, by the server's one rule
3
+ * (`domain/vault/address.ts` in the server): a tool's address is its full
4
+ * URL, with scheme and host lowercased, a default port dropped, one trailing
5
+ * slash trimmed, query and fragment dropped; only an http or https URL
6
+ * without credentials in it is one. A Git host's address is its bare host
7
+ * name, lowercased, its default port dropped (`github.com`).
8
+ *
9
+ * The server normalizes every saved address itself. This mirror lives here,
10
+ * below the React kit, because two layers match an address before anything
11
+ * is saved: `ToolTokensProvider` refuses a host's key the server would
12
+ * refuse, and the kit's setup matches a tool against the logins that fill
13
+ * it. The kit's `vault/address.ts` builds its tool and Git-host rules on it.
14
+ *
15
+ * Pinned by __tests__/address.test.ts.
16
+ */
17
+ const DEFAULT_PORTS = {
18
+ "http:": "80",
19
+ "https:": "443",
20
+ };
21
+ /** A bare host: labels of letters, digits and hyphens, optionally with a port. */
22
+ const BARE_HOST = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*(:[0-9]{1,5})?$/;
23
+ /**
24
+ * Normalizes an address in either shape: a URL (`https://mcp.linear.app/mcp/`)
25
+ * becomes a tool address, anything else a bare host. Returns `null` for input
26
+ * the server would refuse (an unparseable URL, a scheme other than http or
27
+ * https, credentials in the URL, a placeholder, a blank).
28
+ */
29
+ export function normalizeAddress(input) {
30
+ const trimmed = input.trim();
31
+ if (trimmed === "" || trimmed.includes("${"))
32
+ return null;
33
+ if (!trimmed.includes("://"))
34
+ return normalizeHost(trimmed);
35
+ let url;
36
+ try {
37
+ url = new URL(trimmed);
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ if (url.protocol !== "https:" && url.protocol !== "http:")
43
+ return null;
44
+ if (url.username !== "" || url.password !== "")
45
+ return null;
46
+ const port = url.port !== "" && DEFAULT_PORTS[url.protocol] !== url.port ? `:${url.port}` : "";
47
+ let path = url.pathname;
48
+ if (path.endsWith("/"))
49
+ path = path.slice(0, -1);
50
+ return `${url.protocol}//${url.hostname.toLowerCase()}${port}${path}`;
51
+ }
52
+ /** A bare host name, lowercased, one trailing slash and the HTTPS port dropped; `null` when it is not one. */
53
+ function normalizeHost(input) {
54
+ const host = input.trim().toLowerCase().replace(/\/$/, "");
55
+ return BARE_HOST.test(host) ? host.replace(/:443$/, "") : null;
56
+ }
57
+ //# sourceMappingURL=address.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address.js","sourceRoot":"","sources":["../src/vault/address.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,aAAa,GAAqC;IACtD,OAAO,EAAE,IAAI;IACb,QAAQ,EAAE,KAAK;CAChB,CAAC;AAEF,kFAAkF;AAClF,MAAM,SAAS,GAAG,iFAAiF,CAAC;AAEpG;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,OAAO,KAAK,EAAE,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1D,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,aAAa,CAAC,OA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