@kevinwang15/byok-ai 0.1.1 → 0.1.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/README.md CHANGED
@@ -197,10 +197,19 @@ An element has at most one of the last two. With neither, its description is all
197
197
 
198
198
  A read expression runs as page code, in the global scope and in strict mode, with two more names in scope:
199
199
 
200
- - `MCP(name, params)` calls one of the answer's MCP tools by its full name (`serverId__toolName`) with an object of arguments, through the same connection and headers as the model's calls. It returns a promise of the tool's structured content, or of its text when it has none, and rejects when the tool reports an error.
200
+ - `MCP(name, params)` calls one of the answer's MCP tools by its full name (`serverId__toolName`) with an object of arguments, through the same connection and headers as the model's calls. It returns a promise of the tool's structured content, or of its text when it has none (read as JSON when the text is JSON, as tools often return it), and rejects when the tool reports an error.
201
201
  - `element` is the element being read.
202
202
 
203
- A read may take 60 seconds, MCP calls included, and Stop stops the answer from waiting for it.
203
+ The value may be an object or array with promises in it, at any depth, and each is awaited, in parallel. A panel can then read as several calls at once, one per part it shows. A nested promise that fails becomes `{ "error": "…" }` in its place, so the other parts still arrive:
204
+
205
+ ```tsx
206
+ <section data-ai-context="Overview: every card" data-ai-context-read={`({
207
+ totals: MCP("stats__overview", ${JSON.stringify(params)}),
208
+ byModel: MCP("stats__breakdown", ${JSON.stringify({ ...params, dimension: 'model' })}),
209
+ })`}>…</section>
210
+ ```
211
+
212
+ Build expressions from your own values as JSON (`JSON.stringify` gives a JavaScript literal), never by pasting text from the page into code. A read may take 60 seconds, MCP calls included, and Stop stops the answer from waiting for it.
204
213
 
205
214
  The message tells the model which elements were added. For each one it gives the id, the description, and the descriptions of the described elements it is inside, recorded when it was picked:
206
215
 
@@ -2,5 +2,8 @@
2
2
  export declare const mcpText: (content: unknown) => string;
3
3
  /** The error an MCP result reports, or undefined when it reports none. */
4
4
  export declare function mcpError(result: unknown): string | undefined;
5
- /** An MCP result as a value: its structured content when it has some, else its text. Throws the error it reports. */
5
+ /**
6
+ * An MCP result as a value: its structured content when it has some, else its text, read as JSON
7
+ * when it is JSON (tools often return JSON as text). Throws the error it reports.
8
+ */
6
9
  export declare function mcpValue(result: unknown): unknown;
@@ -6,6 +6,13 @@ function n(n) {
6
6
  function r(r) {
7
7
  let i = n(r);
8
8
  if (i) throw Error(i);
9
- return e(r) ? r.structuredContent === void 0 ? t(r.content) : r.structuredContent : r;
9
+ if (!e(r)) return r;
10
+ if (r.structuredContent !== void 0) return r.structuredContent;
11
+ let a = t(r.content);
12
+ try {
13
+ return JSON.parse(a);
14
+ } catch {
15
+ return a;
16
+ }
10
17
  }
11
18
  export { n as mcpError, t as mcpText, r as mcpValue };
@@ -82,7 +82,24 @@ function D(e) {
82
82
  throw e instanceof EvalError ? Error(`This page's Content-Security-Policy doesn't allow ${a.read} (it needs 'unsafe-eval'), so only the element's description is known.`, { cause: e }) : Error(`The element's ${a.read} is not a JavaScript expression: ${E(e)}`, { cause: e });
83
83
  }
84
84
  }
85
- function O(e) {
85
+ async function O(e, t = 0) {
86
+ if (t > 8) return e;
87
+ if (e instanceof Promise) return O(await e, t + 1);
88
+ let n = async (e) => {
89
+ try {
90
+ return await O(e, t + 1);
91
+ } catch (e) {
92
+ return { error: E(e) };
93
+ }
94
+ };
95
+ if (Array.isArray(e)) return Promise.all(e.map(n));
96
+ if (typeof e == "object" && e && Object.getPrototypeOf(e) === Object.prototype) {
97
+ let t = await Promise.all(Object.entries(e).map(async ([e, t]) => [e, await n(t)]));
98
+ return Object.fromEntries(t);
99
+ }
100
+ return e;
101
+ }
102
+ function k(e) {
86
103
  if (e === void 0) throw Error(`The element's ${a.read} returned nothing.`);
87
104
  let t;
88
105
  try {
@@ -93,7 +110,7 @@ function O(e) {
93
110
  if (t === void 0) throw Error(`The element's ${a.read} returned a value that is not JSON.`);
94
111
  return JSON.parse(t);
95
112
  }
96
- async function k(e, t) {
113
+ async function A(e, t) {
97
114
  let n = h(e);
98
115
  if (n === void 0) throw Error("The element no longer has a description.");
99
116
  let r = g(e);
@@ -111,16 +128,16 @@ async function k(e, t) {
111
128
  d.catch(() => {});
112
129
  let f;
113
130
  try {
114
- f = await Promise.race([Promise.resolve().then(() => c(t.mcp, e)), d]);
131
+ f = await Promise.race([Promise.resolve().then(() => O(c(t.mcp, e))), d]);
115
132
  } catch (e) {
116
133
  throw u.aborted ? e : Error(`The element's ${a.read} failed: ${E(e)}`, { cause: e });
117
134
  }
118
135
  return {
119
136
  description: n,
120
- data: O(f)
137
+ data: k(f)
121
138
  };
122
139
  }
123
- function A(t) {
140
+ function j(t) {
124
141
  return async (n, r = {}) => {
125
142
  if (typeof n != "string" || !t.isMcp(n)) throw Error(`MCP(${JSON.stringify(n)}): there is no such MCP tool. Use its full name, "<server id>__<tool name>".`);
126
143
  if (typeof r != "object" || !r || Array.isArray(r)) throw Error(`MCP("${n}", params): params is an object.`);
@@ -129,7 +146,7 @@ function A(t) {
129
146
  return e(await i(r, t.callOptions));
130
147
  };
131
148
  }
132
- function j(e, t) {
149
+ function M(e, t) {
133
150
  let r = JSON.stringify(e.data) ?? "";
134
151
  return r.length <= o.resultChars ? e : {
135
152
  description: e.description,
@@ -138,13 +155,13 @@ function j(e, t) {
138
155
  note: t ? `The data is too large to give here. Save it with ${n}__save_tool_result (tool "${c}", the same arguments) and compute over the file.` : "The data is too large to give here; this is its start."
139
156
  };
140
157
  }
141
- function M(e) {
158
+ function N(e) {
142
159
  let n = async ({ id: t }, n) => {
143
160
  if (!e.elements.has(t)) throw Error(`No element with the id ${t} was added to this conversation.`);
144
161
  let r = e.registry.picked(t);
145
162
  if (!r) throw Error("This element is no longer on the page. The user can add it again.");
146
- return k(r, {
147
- mcp: A({
163
+ return A(r, {
164
+ mcp: j({
148
165
  ...e,
149
166
  callOptions: n
150
167
  }),
@@ -161,8 +178,8 @@ function M(e) {
161
178
  description: "The element's id, as listed in the message."
162
179
  } }
163
180
  }),
164
- execute: async (t, r) => j(await n(t, r), e.canSave)
181
+ execute: async (t, r) => M(await n(t, r), e.canSave)
165
182
  });
166
183
  return a[t] = (e, t) => n(e, t), { [c]: a };
167
184
  }
168
- export { a as PAGE_CONTEXT_ATTRIBUTES, s as PAGE_CONTEXT_ID, o as PAGE_CONTEXT_LIMITS, l as PAGE_ELEMENT_TYPE, c as READ_ELEMENT_TOOL, b as addableChain, x as addableElements, g as declarationProblem, h as descriptionOf, p as elementCount, d as insideOf, v as isAddable, u as isPageElementAttachment, m as isPageElementId, A as mcpCall, M as pageContextTools, C as pageElementRegistry, w as pageElementsIn, T as pageElementsNote, S as placeOf, k as readPageElement };
185
+ export { a as PAGE_CONTEXT_ATTRIBUTES, s as PAGE_CONTEXT_ID, o as PAGE_CONTEXT_LIMITS, l as PAGE_ELEMENT_TYPE, c as READ_ELEMENT_TOOL, b as addableChain, x as addableElements, g as declarationProblem, h as descriptionOf, p as elementCount, d as insideOf, v as isAddable, u as isPageElementAttachment, m as isPageElementId, j as mcpCall, N as pageContextTools, C as pageElementRegistry, w as pageElementsIn, T as pageElementsNote, S as placeOf, A as readPageElement };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevinwang15/byok-ai",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "An embeddable AI assistant for React apps where users bring their own API key: a browser-side agent loop with tools from your MCP servers and the page.",
5
5
  "license": "MIT",
6
6
  "author": "Kevin Wang",