@stigmer/react-core 0.4.1 → 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.
- package/agent/appUser.d.ts +18 -0
- package/agent/appUser.d.ts.map +1 -0
- package/agent/appUser.js +26 -0
- package/agent/appUser.js.map +1 -0
- package/index.d.ts +5 -1
- package/index.d.ts.map +1 -1
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/internal/useKeyedState.d.ts +6 -0
- package/internal/useKeyedState.d.ts.map +1 -0
- package/internal/useKeyedState.js +56 -0
- package/internal/useKeyedState.js.map +1 -0
- package/internal.d.ts +5 -0
- package/internal.d.ts.map +1 -1
- package/internal.js +4 -0
- package/internal.js.map +1 -1
- package/models/registry.d.ts +12 -0
- package/models/registry.d.ts.map +1 -1
- package/models/registry.js +17 -1
- package/models/registry.js.map +1 -1
- package/package.json +3 -3
- package/run/useAnswerCustomToolUse.d.ts +3 -1
- package/run/useAnswerCustomToolUse.d.ts.map +1 -1
- package/run/useAnswerCustomToolUse.js +9 -4
- package/run/useAnswerCustomToolUse.js.map +1 -1
- package/run/useAnswerToolUse.d.ts +16 -1
- package/run/useAnswerToolUse.d.ts.map +1 -1
- package/run/useAnswerToolUse.js +29 -9
- package/run/useAnswerToolUse.js.map +1 -1
- package/run/useCreateRun.d.ts +3 -1
- package/run/useCreateRun.d.ts.map +1 -1
- package/run/useCreateRun.js +11 -7
- package/run/useCreateRun.js.map +1 -1
- package/run/useReviewFileChanges.d.ts +18 -2
- package/run/useReviewFileChanges.d.ts.map +1 -1
- package/run/useReviewFileChanges.js +21 -7
- package/run/useReviewFileChanges.js.map +1 -1
- package/run/useRunActions.d.ts +10 -3
- package/run/useRunActions.d.ts.map +1 -1
- package/run/useRunActions.js +22 -18
- package/run/useRunActions.js.map +1 -1
- package/session/useSessionConversation.d.ts.map +1 -1
- package/session/useSessionConversation.js +10 -6
- package/session/useSessionConversation.js.map +1 -1
- package/src/agent/appUser.ts +26 -0
- package/src/index.ts +5 -0
- package/src/internal/useKeyedState.ts +70 -0
- package/src/internal.ts +5 -0
- package/src/models/registry.ts +19 -1
- package/src/run/useAnswerCustomToolUse.ts +9 -4
- package/src/run/useAnswerToolUse.ts +33 -9
- package/src/run/useCreateRun.ts +11 -7
- package/src/run/useReviewFileChanges.ts +29 -8
- package/src/run/useRunActions.ts +43 -25
- package/src/session/useSessionConversation.ts +10 -6
- package/src/tool-tokens/ToolTokensProvider.tsx +176 -0
- package/src/tool-tokens/tool-tokens.ts +114 -0
- package/src/vault/address.ts +54 -0
- package/tool-tokens/ToolTokensProvider.d.ts +44 -0
- package/tool-tokens/ToolTokensProvider.d.ts.map +1 -0
- package/tool-tokens/ToolTokensProvider.js +135 -0
- package/tool-tokens/ToolTokensProvider.js.map +1 -0
- package/tool-tokens/tool-tokens.d.ts +68 -0
- package/tool-tokens/tool-tokens.d.ts.map +1 -0
- package/tool-tokens/tool-tokens.js +73 -0
- package/tool-tokens/tool-tokens.js.map +1 -0
- package/vault/address.d.ts +24 -0
- package/vault/address.d.ts.map +1 -0
- package/vault/address.js +57 -0
- package/vault/address.js.map +1 -0
|
@@ -30,6 +30,7 @@ import type { ThinkingModeOption } from "../models/thinking-mode.js";
|
|
|
30
30
|
import { useStigmer } from "../hooks.js";
|
|
31
31
|
import { toError } from "../internal/toError.js";
|
|
32
32
|
import { useFetch } from "../internal/useFetch.js";
|
|
33
|
+
import { useKeyedState } from "../internal/useKeyedState.js";
|
|
33
34
|
import { useConversationStoreRef } from "../internal/store/conversation-store.js";
|
|
34
35
|
// Type-only import so the SharedRunFields doc links below resolve;
|
|
35
36
|
// the strict tsdoc gate (tsdoc:check) guards it against removal.
|
|
@@ -669,7 +670,7 @@ export function useSessionConversation(
|
|
|
669
670
|
errorsByToolUseId,
|
|
670
671
|
error: answerError,
|
|
671
672
|
clearError: clearAnswerError,
|
|
672
|
-
} = useAnswerToolUse();
|
|
673
|
+
} = useAnswerToolUse(sessionId);
|
|
673
674
|
const {
|
|
674
675
|
answerCustomToolUse: rawAnswerCustomToolUse,
|
|
675
676
|
submittingIds: answeringCustomToolUseIds,
|
|
@@ -687,7 +688,7 @@ export function useSessionConversation(
|
|
|
687
688
|
clearDecisionError: clearFileDecisionError,
|
|
688
689
|
error: fileReviewError,
|
|
689
690
|
clearError: clearFileReviewError,
|
|
690
|
-
} = useReviewFileChanges();
|
|
691
|
+
} = useReviewFileChanges(sessionId);
|
|
691
692
|
|
|
692
693
|
// Local run: attach the session's runner worker while it is open and
|
|
693
694
|
// detach it on close. No-op for cloud sessions or when no adapter is set.
|
|
@@ -832,10 +833,13 @@ export function useSessionConversation(
|
|
|
832
833
|
liveTurn !== null && activePhase !== null && STOPPABLE_PHASES.has(activePhase);
|
|
833
834
|
|
|
834
835
|
// Stop: the session's interrupt first; a repeat press for the same run,
|
|
835
|
-
// while it still winds down, terminates it.
|
|
836
|
+
// while it still winds down, terminates it. The interrupt's flag and
|
|
837
|
+
// error belong to this conversation, as the run actions' belong to its
|
|
838
|
+
// run and the answers' to it: the mounted viewer moving to another
|
|
839
|
+
// conversation shows none of them there (stigmer/stigmer#3226).
|
|
836
840
|
const runActions = useRunActions(activeRunId, { onSuccess: refetch });
|
|
837
|
-
const [isInterrupting, setIsInterrupting] =
|
|
838
|
-
const [interruptError, setInterruptError] =
|
|
841
|
+
const [isInterrupting, setIsInterrupting] = useKeyedState(sessionId ?? "", false);
|
|
842
|
+
const [interruptError, setInterruptError] = useKeyedState<Error | null>(sessionId ?? "", null);
|
|
839
843
|
const interruptedRunRef = useRef<string | null>(null);
|
|
840
844
|
const terminate = runActions.terminate;
|
|
841
845
|
const stop = useCallback(
|
|
@@ -857,7 +861,7 @@ export function useSessionConversation(
|
|
|
857
861
|
setIsInterrupting(false);
|
|
858
862
|
}
|
|
859
863
|
},
|
|
860
|
-
[isStoppable, sessionId, activeRunId, terminate, stigmer],
|
|
864
|
+
[isStoppable, sessionId, activeRunId, terminate, stigmer, setIsInterrupting, setInterruptError],
|
|
861
865
|
);
|
|
862
866
|
|
|
863
867
|
// A message is sent while a turn works too: it waits in the session's
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
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
|
+
|
|
25
|
+
import { useCallback, useContext, useMemo, useRef, type ReactNode } from "react";
|
|
26
|
+
import type { Stigmer } from "@stigmer/client";
|
|
27
|
+
import { isAppUser } from "../agent/appUser.js";
|
|
28
|
+
import { StigmerContext } from "../context.js";
|
|
29
|
+
import { normalizeAddress } from "../vault/address.js";
|
|
30
|
+
import {
|
|
31
|
+
ToolTokensContext,
|
|
32
|
+
ToolTokenUnavailableError,
|
|
33
|
+
type ToolTokenGetter,
|
|
34
|
+
type ToolTokens,
|
|
35
|
+
type ToolTokensValue,
|
|
36
|
+
} from "./tool-tokens.js";
|
|
37
|
+
|
|
38
|
+
/** Props for {@link ToolTokensProvider}. */
|
|
39
|
+
export interface ToolTokensProviderProps {
|
|
40
|
+
/**
|
|
41
|
+
* The organization the conversations below run in, by slug or id: the
|
|
42
|
+
* one whose My vault the signed-in user's turns read.
|
|
43
|
+
*/
|
|
44
|
+
readonly org: string;
|
|
45
|
+
/** Your tool servers' token functions, keyed by address (see {@link ToolTokens}). */
|
|
46
|
+
readonly tokens: ToolTokens;
|
|
47
|
+
readonly children?: ReactNode;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** One host key the server's address rule accepts. */
|
|
51
|
+
interface ToolTokenEntry {
|
|
52
|
+
/** The key as the host wrote it, which every console line names. */
|
|
53
|
+
readonly key: string;
|
|
54
|
+
/** The address the login is saved at. */
|
|
55
|
+
readonly address: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Hands the signed-in user's own token for each of your tool servers to
|
|
60
|
+
* every conversation below, before every message and every answer that
|
|
61
|
+
* resumes a turn. `AgentChat` mounts it for its `toolTokens`; mount it
|
|
62
|
+
* yourself above a chat you build on react-core's hooks.
|
|
63
|
+
*
|
|
64
|
+
* Only an app user's turns read these tokens (a user your backend signs in
|
|
65
|
+
* to Stigmer), each only for the tool server at its address and only for
|
|
66
|
+
* their own conversations. Each token is kept sealed in that user's own My
|
|
67
|
+
* vault in `org`, replaced by the next send and never renewed. A function
|
|
68
|
+
* that throws or gives no token fails the send with
|
|
69
|
+
* {@link ToolTokenUnavailableError}, and nothing is sent.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```tsx
|
|
73
|
+
* import type { ReactNode } from "react";
|
|
74
|
+
*
|
|
75
|
+
* declare const auth: { getAccessToken(): Promise<string> };
|
|
76
|
+
*
|
|
77
|
+
* // Above your own chat, built on useCreateRun and the answer hooks.
|
|
78
|
+
* function SupportChat({ children }: { children: ReactNode }) {
|
|
79
|
+
* return (
|
|
80
|
+
* <ToolTokensProvider org="acme" tokens={{ "https://api.acme.com/mcp": () => auth.getAccessToken() }}>
|
|
81
|
+
* {children}
|
|
82
|
+
* </ToolTokensProvider>
|
|
83
|
+
* );
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function ToolTokensProvider({ org, tokens, children }: ToolTokensProviderProps) {
|
|
88
|
+
// Nullable: a chat drawn outside a client provider (a gallery, a test)
|
|
89
|
+
// still renders; with no client nothing can be sent, so nothing is written.
|
|
90
|
+
const stigmer = useContext(StigmerContext);
|
|
91
|
+
// The functions the host rendered last: a host passes a new object (and
|
|
92
|
+
// new functions) on every render, and a send calls the current ones.
|
|
93
|
+
const tokensRef = useRef(tokens);
|
|
94
|
+
tokensRef.current = tokens;
|
|
95
|
+
|
|
96
|
+
// Keyed by the keys alone, so a new object with the same servers keeps
|
|
97
|
+
// the same addresses (and the setup that reads them does not run again).
|
|
98
|
+
const keysKey = Object.keys(tokens).join("\n");
|
|
99
|
+
const entries = useMemo(() => entriesOf(keysKey === "" ? [] : keysKey.split("\n")), [keysKey]);
|
|
100
|
+
const addresses = useMemo(() => new Set(entries.map((entry) => entry.address)), [entries]);
|
|
101
|
+
|
|
102
|
+
// The client that answered "app user"; anything else is asked again.
|
|
103
|
+
const appUserRef = useRef<Stigmer | null>(null);
|
|
104
|
+
const toldNotAppUserRef = useRef(false);
|
|
105
|
+
|
|
106
|
+
const refresh = useCallback(async (): Promise<void> => {
|
|
107
|
+
if (entries.length === 0 || stigmer === null) return;
|
|
108
|
+
if (appUserRef.current !== stigmer) {
|
|
109
|
+
if (await isAppUser(stigmer)) appUserRef.current = stigmer;
|
|
110
|
+
}
|
|
111
|
+
if (appUserRef.current !== stigmer) {
|
|
112
|
+
if (!toldNotAppUserRef.current) {
|
|
113
|
+
toldNotAppUserRef.current = true;
|
|
114
|
+
console.warn(
|
|
115
|
+
"Stigmer: toolTokens is set, but the signed-in account is not an app user (a user your backend signs in to Stigmer). " +
|
|
116
|
+
"Only an app user's own turns read these tokens, so none is written.",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const current = tokensRef.current;
|
|
122
|
+
// Every token first, so a function that fails sends nothing at all.
|
|
123
|
+
const read = await Promise.all(
|
|
124
|
+
entries.map(async (entry) => ({ address: entry.address, token: await tokenOf(entry.key, current[entry.key]) })),
|
|
125
|
+
);
|
|
126
|
+
await Promise.all(
|
|
127
|
+
read.map(({ address, token }) => stigmer.vault.setConnection({ vault: { org, vault: { case: "mine", value: true } }, address, token })),
|
|
128
|
+
);
|
|
129
|
+
}, [stigmer, org, entries]);
|
|
130
|
+
|
|
131
|
+
const value = useMemo<ToolTokensValue>(() => ({ addresses, refresh }), [addresses, refresh]);
|
|
132
|
+
return <ToolTokensContext.Provider value={value}>{children}</ToolTokensContext.Provider>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The host's keys the address rule accepts, each once. A key the server
|
|
137
|
+
* would refuse, or a second key for an address already keyed, is named in
|
|
138
|
+
* the console and left out.
|
|
139
|
+
*/
|
|
140
|
+
function entriesOf(keys: readonly string[]): readonly ToolTokenEntry[] {
|
|
141
|
+
const byAddress = new Map<string, ToolTokenEntry>();
|
|
142
|
+
for (const key of keys) {
|
|
143
|
+
const address = normalizeAddress(key);
|
|
144
|
+
if (address === null || !address.includes("://")) {
|
|
145
|
+
console.error(
|
|
146
|
+
`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.`,
|
|
147
|
+
);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const first = byAddress.get(address);
|
|
151
|
+
if (first !== undefined) {
|
|
152
|
+
console.error(
|
|
153
|
+
`Stigmer: toolTokens keys "${first.key}" and "${key}" are one address (${address}); only "${first.key}"'s token is written.`,
|
|
154
|
+
);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
byAddress.set(address, { key, address });
|
|
158
|
+
}
|
|
159
|
+
return [...byAddress.values()];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The token one host function gives, or {@link ToolTokenUnavailableError}. */
|
|
163
|
+
async function tokenOf(key: string, getter: ToolTokenGetter | undefined): Promise<string> {
|
|
164
|
+
let token: unknown;
|
|
165
|
+
try {
|
|
166
|
+
token = await getter?.();
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error(`Stigmer: the toolTokens function for ${key} threw, so nothing was sent.`, error);
|
|
169
|
+
throw new ToolTokenUnavailableError(key, error);
|
|
170
|
+
}
|
|
171
|
+
if (typeof token !== "string" || token.trim() === "") {
|
|
172
|
+
console.error(`Stigmer: the toolTokens function for ${key} gave no token, so nothing was sent.`);
|
|
173
|
+
throw new ToolTokenUnavailableError(key);
|
|
174
|
+
}
|
|
175
|
+
return token;
|
|
176
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// A host's tool tokens: the signed-in user's own token for each of the
|
|
4
|
+
// host's tool servers, written as that user's own login before every
|
|
5
|
+
// request that starts work on a turn.
|
|
6
|
+
//
|
|
7
|
+
// Why a write, and why before each request: a turn's values are read by
|
|
8
|
+
// the runner when each piece of the turn's work starts, from the vaults as
|
|
9
|
+
// they are at that moment (the server's `domain/vault/values.ts`), and the
|
|
10
|
+
// server keeps no copy between. An app user's own turn reads the logins in
|
|
11
|
+
// their own My vault last, each only for the tool at the login's address
|
|
12
|
+
// (the server's `domain/vault/resolve.ts`, its fifth source). So the
|
|
13
|
+
// user's current token, saved there just before a message is sent or an
|
|
14
|
+
// approval answered, is what the tool receives for the work that request
|
|
15
|
+
// starts, and no other person's turn ever reads it. A token handed with the
|
|
16
|
+
// message instead would have to wait on the server until the runner asked,
|
|
17
|
+
// which can be hours across an approval.
|
|
18
|
+
//
|
|
19
|
+
// Each write replaces the last one in place; nothing renews a token written
|
|
20
|
+
// this way (only a sign-in's login is renewed), so an expired token reaches
|
|
21
|
+
// the tool server as it is and the server's refusal is the turn's answer.
|
|
22
|
+
//
|
|
23
|
+
// The context is read by the four hooks that start work on a turn
|
|
24
|
+
// (`useCreateRun`, `useAnswerToolUse`, `useAnswerCustomToolUse`,
|
|
25
|
+
// `useRunActions`' resume and recover) and by the React kit's setup, which
|
|
26
|
+
// offers no sign-in for an address the host fills.
|
|
27
|
+
|
|
28
|
+
import { createContext, useContext } from "react";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the signed-in user's current token for one of your tool
|
|
32
|
+
* servers: a string, or a promise of one. Called before every message the
|
|
33
|
+
* user sends and every answer that resumes a turn, so return the token as
|
|
34
|
+
* it is now (your auth library's `getAccessToken()`, or a fetch to your own
|
|
35
|
+
* backend with the user's session).
|
|
36
|
+
*/
|
|
37
|
+
export type ToolTokenGetter = () => string | Promise<string>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Your tool servers' token functions, keyed by the address your agent
|
|
41
|
+
* dials each server at (`"https://api.acme.com/mcp"`). An address matches
|
|
42
|
+
* as a saved login does: scheme and host in any case, a default port, a
|
|
43
|
+
* trailing slash, a query or a fragment make no difference; the path does.
|
|
44
|
+
*/
|
|
45
|
+
export type ToolTokens = Readonly<Record<string, ToolTokenGetter>>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A send that did not happen because one of the host's token functions
|
|
49
|
+
* threw or gave no token. Nothing was sent: not the message, not the
|
|
50
|
+
* answer, not any token. `message` is written for the person in front of
|
|
51
|
+
* the chat, who reads it where the send failed; `address` names the tool
|
|
52
|
+
* server whose function failed, and `cause` is what it threw, if it threw.
|
|
53
|
+
* The token itself is never part of it.
|
|
54
|
+
*/
|
|
55
|
+
export class ToolTokenUnavailableError extends Error {
|
|
56
|
+
/** The tool server whose token function failed, as the host keyed it. */
|
|
57
|
+
readonly address: string;
|
|
58
|
+
|
|
59
|
+
constructor(address: string, cause?: unknown) {
|
|
60
|
+
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.", {
|
|
61
|
+
cause,
|
|
62
|
+
});
|
|
63
|
+
this.name = "ToolTokenUnavailableError";
|
|
64
|
+
this.address = address;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* What {@link ToolTokensContext} holds: the addresses the host fills,
|
|
70
|
+
* normalized, and the one function that writes their current tokens.
|
|
71
|
+
*
|
|
72
|
+
* @internal The React kit's and react-core's own plumbing.
|
|
73
|
+
*/
|
|
74
|
+
export interface ToolTokensValue {
|
|
75
|
+
/** The normalized addresses of the host's tool servers. */
|
|
76
|
+
readonly addresses: ReadonlySet<string>;
|
|
77
|
+
/**
|
|
78
|
+
* Calls each token function, then writes each token as the signed-in
|
|
79
|
+
* app user's own login at its address. Resolves at once for an account
|
|
80
|
+
* that is not an app user. Rejects with {@link ToolTokenUnavailableError}
|
|
81
|
+
* before any write when a function fails, or with the server's error when
|
|
82
|
+
* a write is refused.
|
|
83
|
+
*/
|
|
84
|
+
readonly refresh: () => Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The host's tool tokens for the conversation below, or `null` where none
|
|
89
|
+
* were given.
|
|
90
|
+
*
|
|
91
|
+
* @internal Provided by `ToolTokensProvider`.
|
|
92
|
+
*/
|
|
93
|
+
export const ToolTokensContext = createContext<ToolTokensValue | null>(null);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The host's tool tokens from the nearest `ToolTokensProvider`, or `null`.
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export function useToolTokens(): ToolTokensValue | null {
|
|
101
|
+
return useContext(ToolTokensContext);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Runs `send` after the host's tool tokens are written, or at once where
|
|
106
|
+
* none were given: the one order every hook that starts work on a turn
|
|
107
|
+
* keeps.
|
|
108
|
+
*
|
|
109
|
+
* @internal
|
|
110
|
+
*/
|
|
111
|
+
export async function withToolTokens<T>(tokens: ToolTokensValue | null, send: () => Promise<T>): Promise<T> {
|
|
112
|
+
if (tokens !== null) await tokens.refresh();
|
|
113
|
+
return send();
|
|
114
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
const DEFAULT_PORTS: Readonly<Record<string, string>> = {
|
|
19
|
+
"http:": "80",
|
|
20
|
+
"https:": "443",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** A bare host: labels of letters, digits and hyphens, optionally with a port. */
|
|
24
|
+
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})?$/;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Normalizes an address in either shape: a URL (`https://mcp.linear.app/mcp/`)
|
|
28
|
+
* becomes a tool address, anything else a bare host. Returns `null` for input
|
|
29
|
+
* the server would refuse (an unparseable URL, a scheme other than http or
|
|
30
|
+
* https, credentials in the URL, a placeholder, a blank).
|
|
31
|
+
*/
|
|
32
|
+
export function normalizeAddress(input: string): string | null {
|
|
33
|
+
const trimmed = input.trim();
|
|
34
|
+
if (trimmed === "" || trimmed.includes("${")) return null;
|
|
35
|
+
if (!trimmed.includes("://")) return normalizeHost(trimmed);
|
|
36
|
+
let url: URL;
|
|
37
|
+
try {
|
|
38
|
+
url = new URL(trimmed);
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
43
|
+
if (url.username !== "" || url.password !== "") return null;
|
|
44
|
+
const port = url.port !== "" && DEFAULT_PORTS[url.protocol] !== url.port ? `:${url.port}` : "";
|
|
45
|
+
let path = url.pathname;
|
|
46
|
+
if (path.endsWith("/")) path = path.slice(0, -1);
|
|
47
|
+
return `${url.protocol}//${url.hostname.toLowerCase()}${port}${path}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A bare host name, lowercased, one trailing slash and the HTTPS port dropped; `null` when it is not one. */
|
|
51
|
+
function normalizeHost(input: string): string | null {
|
|
52
|
+
const host = input.trim().toLowerCase().replace(/\/$/, "");
|
|
53
|
+
return BARE_HOST.test(host) ? host.replace(/:443$/, "") : null;
|
|
54
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { type ToolTokens } from "./tool-tokens.js";
|
|
3
|
+
/** Props for {@link ToolTokensProvider}. */
|
|
4
|
+
export interface ToolTokensProviderProps {
|
|
5
|
+
/**
|
|
6
|
+
* The organization the conversations below run in, by slug or id: the
|
|
7
|
+
* one whose My vault the signed-in user's turns read.
|
|
8
|
+
*/
|
|
9
|
+
readonly org: string;
|
|
10
|
+
/** Your tool servers' token functions, keyed by address (see {@link ToolTokens}). */
|
|
11
|
+
readonly tokens: ToolTokens;
|
|
12
|
+
readonly children?: ReactNode;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Hands the signed-in user's own token for each of your tool servers to
|
|
16
|
+
* every conversation below, before every message and every answer that
|
|
17
|
+
* resumes a turn. `AgentChat` mounts it for its `toolTokens`; mount it
|
|
18
|
+
* yourself above a chat you build on react-core's hooks.
|
|
19
|
+
*
|
|
20
|
+
* Only an app user's turns read these tokens (a user your backend signs in
|
|
21
|
+
* to Stigmer), each only for the tool server at its address and only for
|
|
22
|
+
* their own conversations. Each token is kept sealed in that user's own My
|
|
23
|
+
* vault in `org`, replaced by the next send and never renewed. A function
|
|
24
|
+
* that throws or gives no token fails the send with
|
|
25
|
+
* {@link ToolTokenUnavailableError}, and nothing is sent.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* import type { ReactNode } from "react";
|
|
30
|
+
*
|
|
31
|
+
* declare const auth: { getAccessToken(): Promise<string> };
|
|
32
|
+
*
|
|
33
|
+
* // Above your own chat, built on useCreateRun and the answer hooks.
|
|
34
|
+
* function SupportChat({ children }: { children: ReactNode }) {
|
|
35
|
+
* return (
|
|
36
|
+
* <ToolTokensProvider org="acme" tokens={{ "https://api.acme.com/mcp": () => auth.getAccessToken() }}>
|
|
37
|
+
* {children}
|
|
38
|
+
* </ToolTokensProvider>
|
|
39
|
+
* );
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function ToolTokensProvider({ org, tokens, children }: ToolTokensProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
44
|
+
//# sourceMappingURL=ToolTokensProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolTokensProvider.d.ts","sourceRoot":"","sources":["../src/tool-tokens/ToolTokensProvider.tsx"],"names":[],"mappings":"AAwBA,OAAO,EAA4C,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAKjF,OAAO,EAIL,KAAK,UAAU,EAEhB,MAAM,kBAAkB,CAAC;AAE1B,4CAA4C;AAC5C,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;CAC/B;AAUD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,kBAAkB,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,uBAAuB,2CA8CpF"}
|
|
@@ -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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F,SAAS;QACX,CAAC;QACD,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;AACjC,CAAC;AAED,+EAA+E;AAC/E,KAAK,UAAU,OAAO,CAAC,GAAW,EAAE,MAAmC;IACrE,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,MAAM,MAAM,EAAE,EAAE,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,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"}
|