@kevinwang15/byok-ai 0.1.0 → 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.
Files changed (48) hide show
  1. package/README.md +21 -2
  2. package/dist/lib/AskAI.js +383 -0
  3. package/dist/lib/AskAIHistory.js +120 -0
  4. package/dist/lib/AskAIProvider.js +255 -0
  5. package/dist/lib/Markdown.js +111 -0
  6. package/dist/lib/PageElements.js +127 -0
  7. package/dist/lib/agent.js +223 -0
  8. package/dist/lib/answer-components.js +49 -0
  9. package/dist/lib/connection.d.ts +15 -0
  10. package/dist/lib/connection.js +32 -0
  11. package/dist/lib/context.js +9 -0
  12. package/dist/lib/conversation-files.js +164 -0
  13. package/dist/lib/entries/agent.d.ts +9 -0
  14. package/dist/lib/entries/agent.js +5 -0
  15. package/dist/lib/entries/connection.d.ts +2 -0
  16. package/dist/lib/entries/connection.js +1 -0
  17. package/dist/lib/entries/script-engine.d.ts +3 -0
  18. package/dist/lib/entries/script-engine.js +5 -0
  19. package/dist/lib/history.js +16 -0
  20. package/dist/lib/images.js +79 -0
  21. package/dist/lib/index.d.ts +6 -2
  22. package/dist/lib/index.js +18 -2326
  23. package/dist/lib/local-storage.d.ts +8 -0
  24. package/dist/lib/local-storage.js +23 -0
  25. package/dist/lib/mcp-result.d.ts +4 -1
  26. package/dist/lib/mcp-result.js +18 -0
  27. package/dist/lib/mount.js +9 -0
  28. package/dist/lib/node_modules/@jitl/quickjs-ffi-types/dist/index.js +46 -0
  29. package/dist/lib/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/emscripten-module.browser.js +330 -0
  30. package/dist/lib/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/ffi.js +76 -0
  31. package/dist/lib/node_modules/@jitl/quickjs-wasmfile-release-sync/dist/index.js +7 -0
  32. package/dist/lib/node_modules/quickjs-emscripten-core/dist/chunk-V2S4ZYJR.js +1142 -0
  33. package/dist/lib/node_modules/quickjs-emscripten-core/dist/index.js +21 -0
  34. package/dist/lib/node_modules/quickjs-emscripten-core/dist/module-ES6BEMUI.js +1 -0
  35. package/dist/lib/page-context.js +185 -0
  36. package/dist/lib/page-picker.js +119 -0
  37. package/dist/lib/providers.js +84 -0
  38. package/dist/lib/rendering-context.js +4 -0
  39. package/dist/lib/sandbox.js +102 -0
  40. package/dist/lib/script-runner.js +26 -0
  41. package/dist/lib/script.js +9 -0
  42. package/dist/lib/storage.d.ts +0 -13
  43. package/dist/lib/storage.js +67 -0
  44. package/dist/lib/useAskAIImage.js +26 -0
  45. package/dist/lib/usePanelLayout.js +168 -0
  46. package/dist/lib/workspace-tools.js +182 -0
  47. package/package.json +14 -1
  48. /package/dist/lib/{sandbox-worker-BdFlhTIS.js → sandbox-worker.js} +0 -0
@@ -0,0 +1,26 @@
1
+ import { useAskAI as e } from "./context.js";
2
+ import { useEffect as t, useState as n } from "react";
3
+ import { useAuiState as r } from "@assistant-ui/react";
4
+ // frontend/byok/useAskAIImage.ts
5
+ function i() {
6
+ let { loadImage: i } = e(), a = r((e) => e.attachment), [o, s] = n({ missing: !1 }), c = a.status.type === "complete" ? void 0 : a.file;
7
+ return t(() => {
8
+ let e, t = !0, n = (n) => {
9
+ t && (e = n && URL.createObjectURL(n), s({
10
+ src: e,
11
+ missing: !n
12
+ }));
13
+ };
14
+ return c ? n(c) : i(a.id).then(n, () => n(void 0)), () => {
15
+ t = !1, e && URL.revokeObjectURL(e);
16
+ };
17
+ }, [
18
+ c,
19
+ a.id,
20
+ i
21
+ ]), {
22
+ ...o,
23
+ name: a.name
24
+ };
25
+ }
26
+ export { i as useAskAIImage };
@@ -0,0 +1,168 @@
1
+ import { useEffect as e, useRef as t, useState as n } from "react";
2
+ // frontend/byok/usePanelLayout.ts
3
+ var r = () => window.innerWidth <= 640 ? 12 : 16, i = (e) => Math.max(0, Math.min(1, e));
4
+ function a(e) {
5
+ try {
6
+ let t = JSON.parse(localStorage.getItem(e) ?? "null"), n = t?.size, r = t?.position;
7
+ return {
8
+ size: n && [n.width, n.height].every((e) => typeof e == "number" && Number.isFinite(e) && e > 0 && e <= 1e4) ? n : void 0,
9
+ position: r && [r.x, r.y].every((e) => typeof e == "number" && Number.isFinite(e)) ? {
10
+ x: i(r.x),
11
+ y: i(r.y)
12
+ } : void 0,
13
+ maximized: t?.maximized === !0
14
+ };
15
+ } catch {
16
+ return { maximized: !1 };
17
+ }
18
+ }
19
+ function o({ storageKey: o, margin: s = r, minSize: c = 320 }) {
20
+ let [l, u] = n(() => a(o)), [d, f] = n(!1), [p, m] = n(!1), h = t(null), g = t(null);
21
+ e(() => {
22
+ if (!(d || p)) try {
23
+ localStorage.setItem(o, JSON.stringify(l));
24
+ } catch {}
25
+ }, [
26
+ l,
27
+ d,
28
+ p,
29
+ o
30
+ ]);
31
+ let _ = (e) => (e.closest("[data-byok-panel]") ?? e).getBoundingClientRect(), v = (e, t, n, r) => {
32
+ let a = s();
33
+ return {
34
+ x: i((e - a) / Math.max(1, window.innerWidth - n - a * 2)),
35
+ y: i((t - a) / Math.max(1, window.innerHeight - r - a * 2))
36
+ };
37
+ }, y = (e, t, n, r) => {
38
+ let i = s(), { left: a, top: o, right: l, bottom: d } = e, f = (e, t) => Math.min(c, t - e);
39
+ t.includes("w") && (a = Math.min(Math.max(i, e.left + n), l - f(i, l))), t.includes("e") && (l = Math.max(Math.min(window.innerWidth - i, e.right + n), a + f(a, window.innerWidth - i))), t.includes("n") && (o = Math.min(Math.max(i, e.top + r), d - f(i, d))), t.includes("s") && (d = Math.max(Math.min(window.innerHeight - i, e.bottom + r), o + f(o, window.innerHeight - i)));
40
+ let p = {
41
+ width: l - a,
42
+ height: d - o
43
+ }, m = t.includes("e") && l !== e.right || t.includes("s") && d !== e.bottom;
44
+ u((e) => ({
45
+ ...e,
46
+ size: p,
47
+ position: e.position || m ? v(a, o, p.width, p.height) : void 0
48
+ }));
49
+ }, b = () => {
50
+ h.current = null, f(!1);
51
+ }, x = () => {
52
+ let e = g.current;
53
+ e && (u((t) => ({
54
+ ...t,
55
+ position: e.original
56
+ })), g.current = null, m(!1));
57
+ }, S = (e) => {
58
+ let t = _(e);
59
+ return {
60
+ left: t.left,
61
+ top: t.top,
62
+ right: t.right,
63
+ bottom: t.bottom
64
+ };
65
+ }, C = (e) => ({
66
+ "data-byok-resize": e,
67
+ onPointerDown(t) {
68
+ t.button === 0 && t.isPrimary && (t.preventDefault(), t.currentTarget.focus(), t.currentTarget.setPointerCapture(t.pointerId), h.current = {
69
+ x: t.clientX,
70
+ y: t.clientY,
71
+ rect: S(t.currentTarget),
72
+ edge: e
73
+ }, f(!0));
74
+ },
75
+ onPointerMove(e) {
76
+ let t = h.current;
77
+ t && e.currentTarget.hasPointerCapture(e.pointerId) && y(t.rect, t.edge, e.clientX - t.x, e.clientY - t.y);
78
+ },
79
+ onPointerUp(e) {
80
+ e.currentTarget.hasPointerCapture(e.pointerId) && e.currentTarget.releasePointerCapture(e.pointerId), b();
81
+ },
82
+ onPointerCancel: b,
83
+ onLostPointerCapture: b,
84
+ onKeyDown(t) {
85
+ let n = /[ew]/.test(e) && ["ArrowLeft", "ArrowRight"].includes(t.key), r = /[ns]/.test(e) && ["ArrowUp", "ArrowDown"].includes(t.key);
86
+ if (!n && !r) return;
87
+ t.preventDefault();
88
+ let i = t.shiftKey ? 64 : 24;
89
+ y(S(t.currentTarget), e, n ? t.key === "ArrowLeft" ? -i : i : 0, r ? t.key === "ArrowUp" ? -i : i : 0);
90
+ }
91
+ });
92
+ return {
93
+ maximized: l.maximized,
94
+ positioned: !!l.position,
95
+ resizing: d,
96
+ moving: p,
97
+ toggleMaximized: () => {
98
+ b(), u((e) => ({
99
+ ...e,
100
+ maximized: !e.maximized
101
+ }));
102
+ },
103
+ style: {
104
+ ...l.size ? {
105
+ "--byok-panel-width": `${l.size.width}px`,
106
+ "--byok-panel-height": `${l.size.height}px`
107
+ } : {},
108
+ ...l.position ? {
109
+ "--byok-panel-x": l.position.x,
110
+ "--byok-panel-y": l.position.y
111
+ } : {}
112
+ },
113
+ panelProps: { "data-byok-panel": "" },
114
+ handleProps: C,
115
+ headerProps: {
116
+ tabIndex: l.maximized ? -1 : 0,
117
+ onPointerDown(e) {
118
+ if (l.maximized || e.button !== 0 || !e.isPrimary || e.target.closest("button, a, input, select, textarea, [role=\"button\"]")) return;
119
+ let t = _(e.currentTarget);
120
+ e.preventDefault(), e.currentTarget.focus(), e.currentTarget.setPointerCapture(e.pointerId), g.current = {
121
+ pointerId: e.pointerId,
122
+ x: e.clientX,
123
+ y: e.clientY,
124
+ left: t.left,
125
+ top: t.top,
126
+ width: t.width,
127
+ height: t.height,
128
+ original: l.position,
129
+ moved: !1
130
+ };
131
+ },
132
+ onPointerMove(e) {
133
+ let t = g.current;
134
+ if (!t || t.pointerId !== e.pointerId) return;
135
+ let n = e.clientX - t.x, r = e.clientY - t.y;
136
+ !t.moved && Math.hypot(n, r) < 4 || (t.moved = !0, m(!0), u((e) => ({
137
+ ...e,
138
+ position: v(t.left + n, t.top + r, t.width, t.height)
139
+ })));
140
+ },
141
+ onPointerUp(e) {
142
+ g.current?.pointerId === e.pointerId && (g.current = null, m(!1), e.currentTarget.hasPointerCapture(e.pointerId) && e.currentTarget.releasePointerCapture(e.pointerId));
143
+ },
144
+ onPointerCancel: x,
145
+ onLostPointerCapture: x,
146
+ onKeyDown(e) {
147
+ if (e.target !== e.currentTarget || l.maximized) return;
148
+ if (e.key === "Escape" && g.current) {
149
+ x(), e.preventDefault(), e.stopPropagation();
150
+ return;
151
+ }
152
+ if (![
153
+ "ArrowLeft",
154
+ "ArrowRight",
155
+ "ArrowUp",
156
+ "ArrowDown"
157
+ ].includes(e.key)) return;
158
+ e.preventDefault();
159
+ let t = _(e.currentTarget), n = e.shiftKey ? 64 : 24;
160
+ u((r) => ({
161
+ ...r,
162
+ position: v(t.left + (e.key === "ArrowLeft" ? -n : e.key === "ArrowRight" ? n : 0), t.top + (e.key === "ArrowUp" ? -n : e.key === "ArrowDown" ? n : 0), t.width, t.height)
163
+ }));
164
+ }
165
+ }
166
+ };
167
+ }
168
+ export { o as usePanelLayout };
@@ -0,0 +1,182 @@
1
+ import { mcpError as e, mcpText as t } from "./mcp-result.js";
2
+ import { SCRIPT_LIMITS as n } from "./script.js";
3
+ import { jsonSchema as r, tool as i } from "ai";
4
+ // frontend/byok/workspace-tools.ts
5
+ var a = "workspace", o = Symbol("byok-ai saved result"), s = 4e3, c = 2e4;
6
+ function l(n, r) {
7
+ if (r && typeof n == "object" && n) {
8
+ let r = e(n);
9
+ if (r) throw Error(r);
10
+ let { content: i, structuredContent: a } = n;
11
+ if (a !== void 0) return JSON.stringify(a, null, 2);
12
+ if (Array.isArray(i)) return t(i);
13
+ }
14
+ return typeof n == "string" ? n : JSON.stringify(n, null, 2) ?? "null";
15
+ }
16
+ function u(e) {
17
+ return {
18
+ size: new TextEncoder().encode(e).length,
19
+ characters: e.length,
20
+ start: e.slice(0, 600)
21
+ };
22
+ }
23
+ function d(e) {
24
+ let t = Promise.resolve(), d = (e) => {
25
+ let n = t.then(e, e);
26
+ return t = n.catch(() => {}), n;
27
+ }, f = () => e.workspace();
28
+ return {
29
+ workspace__list_files: i({
30
+ description: "List the files in this conversation's workspace, with their sizes in bytes.",
31
+ inputSchema: r({
32
+ type: "object",
33
+ properties: {},
34
+ additionalProperties: !1
35
+ }),
36
+ execute: () => d(async () => ({ files: (await f().list()).map(({ name: e, size: t }) => ({
37
+ name: e,
38
+ size: t
39
+ })) }))
40
+ }),
41
+ workspace__read_file: i({
42
+ description: `Read part of a workspace file as text: up to ${c} characters from offset (default ${s}). To compute over a large file, use ${a}__run_javascript instead of reading it all.`,
43
+ inputSchema: r({
44
+ type: "object",
45
+ additionalProperties: !1,
46
+ required: ["name"],
47
+ properties: {
48
+ name: { type: "string" },
49
+ offset: {
50
+ type: "integer",
51
+ minimum: 0,
52
+ description: "Character offset to start at. Default 0."
53
+ },
54
+ length: {
55
+ type: "integer",
56
+ minimum: 1,
57
+ maximum: c,
58
+ description: `Characters to read. Default ${s}.`
59
+ }
60
+ }
61
+ }),
62
+ execute: ({ name: e, offset: t = 0, length: n = s }) => d(async () => {
63
+ let r = await f().read(e);
64
+ if (r === void 0) throw Error(`There is no file named ${e}.`);
65
+ let i = Math.max(0, Math.floor(t)), a = Math.min(r.length, i + Math.min(c, Math.max(1, Math.floor(n))));
66
+ return {
67
+ name: e,
68
+ characters: r.length,
69
+ offset: i,
70
+ text: r.slice(i, a),
71
+ ...a < r.length ? { nextOffset: a } : {}
72
+ };
73
+ })
74
+ }),
75
+ workspace__write_file: i({
76
+ description: "Create or replace a workspace file with the given text. Names are letters, digits, \".\", \"-\" and \"_\", like data.csv.",
77
+ inputSchema: r({
78
+ type: "object",
79
+ additionalProperties: !1,
80
+ required: ["name", "text"],
81
+ properties: {
82
+ name: { type: "string" },
83
+ text: { type: "string" }
84
+ }
85
+ }),
86
+ execute: ({ name: e, text: t }) => d(async () => (await f().write(e, t), {
87
+ name: e,
88
+ size: new TextEncoder().encode(t).length
89
+ }))
90
+ }),
91
+ workspace__delete_file: i({
92
+ description: "Delete a workspace file.",
93
+ inputSchema: r({
94
+ type: "object",
95
+ additionalProperties: !1,
96
+ required: ["name"],
97
+ properties: { name: { type: "string" } }
98
+ }),
99
+ execute: ({ name: e }) => d(async () => ({
100
+ name: e,
101
+ deleted: await f().remove(e)
102
+ }))
103
+ }),
104
+ workspace__save_tool_result: i({
105
+ description: `Call another of your tools and save its result to a workspace file instead of returning it here. Use it for results too large to read, then compute over the file with ${a}__run_javascript. Returns the file's size and its first characters.`,
106
+ inputSchema: r({
107
+ type: "object",
108
+ additionalProperties: !1,
109
+ required: ["tool", "file"],
110
+ properties: {
111
+ tool: {
112
+ type: "string",
113
+ description: "The tool's full name, as you would call it."
114
+ },
115
+ arguments: {
116
+ type: "object",
117
+ description: "The tool's arguments."
118
+ },
119
+ file: {
120
+ type: "string",
121
+ description: "The file to save the result to, like orders.json."
122
+ }
123
+ }
124
+ }),
125
+ execute: async ({ tool: t, arguments: n = {}, file: r }, i) => {
126
+ if (t.startsWith("workspace__")) throw Error("Workspace tools can't be saved: call them directly.");
127
+ let a = e.tools()[t], s = a?.[o] ?? a?.execute;
128
+ if (!s) throw Error(`There is no tool named ${t}.`);
129
+ let c = l(await s(n, i), e.isMcp(t));
130
+ return d(async () => (await f().write(r, c), {
131
+ file: r,
132
+ ...u(c)
133
+ }));
134
+ }
135
+ }),
136
+ workspace__run_javascript: i({
137
+ description: [
138
+ "Run JavaScript over the workspace files, for calculation and analysis, and get back what the code returns.",
139
+ "The code is the body of an async function: `return` a JSON value (a summary, not raw data;",
140
+ `at most ${n.resultChars} characters as JSON). Available: files.list() → [{name, size}], files.read(name) → text or undefined,`,
141
+ "files.readJSON(name), files.write(name, textOrValue) (values are saved as JSON), files.delete(name), console.log(...).",
142
+ "Standard JavaScript only: no fetch, DOM, timers or imports.",
143
+ `It stops after ${n.timeMs / 1e3} seconds. Writes are saved only if the code finishes without an error.`
144
+ ].join(" "),
145
+ inputSchema: r({
146
+ type: "object",
147
+ additionalProperties: !1,
148
+ required: ["code"],
149
+ properties: { code: {
150
+ type: "string",
151
+ description: "For example: const rows = files.readJSON(\"orders.json\"); return rows.length"
152
+ } }
153
+ }),
154
+ execute: ({ code: t }, { abortSignal: n }) => d(async () => {
155
+ let r = f(), i = await r.list(), a = {};
156
+ for (let e of i) a[e.name] = await r.read(e.name) ?? "";
157
+ let o = await e.run({
158
+ code: t,
159
+ files: a
160
+ }, n), s = o.logs ? { logs: o.logs + (o.logsTruncated ? "\n…(more output was cut)" : "") } : {};
161
+ if (!o.ok) return {
162
+ ok: !1,
163
+ error: o.error,
164
+ ...s
165
+ };
166
+ for (let e of o.deleted) await r.remove(e);
167
+ for (let [e, t] of Object.entries(o.written)) await r.write(e, t);
168
+ let c = [...Object.keys(o.written), ...o.deleted];
169
+ return {
170
+ ok: !0,
171
+ result: o.result,
172
+ ...s,
173
+ ...c.length ? {
174
+ written: Object.keys(o.written),
175
+ deleted: o.deleted
176
+ } : {}
177
+ };
178
+ })
179
+ })
180
+ };
181
+ }
182
+ export { o as SAVED_RESULT, a as WORKSPACE_ID, l as toolResultText, d as workspaceTools };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevinwang15/byok-ai",
3
- "version": "0.1.0",
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",
@@ -28,11 +28,24 @@
28
28
  ".": {
29
29
  "types": "./dist/lib/index.d.ts",
30
30
  "import": "./dist/lib/index.js"
31
+ },
32
+ "./agent": {
33
+ "types": "./dist/lib/entries/agent.d.ts",
34
+ "import": "./dist/lib/entries/agent.js"
35
+ },
36
+ "./connection": {
37
+ "types": "./dist/lib/entries/connection.d.ts",
38
+ "import": "./dist/lib/entries/connection.js"
39
+ },
40
+ "./script-engine": {
41
+ "types": "./dist/lib/entries/script-engine.d.ts",
42
+ "import": "./dist/lib/entries/script-engine.js"
31
43
  }
32
44
  },
33
45
  "files": [
34
46
  "dist/lib"
35
47
  ],
48
+ "sideEffects": false,
36
49
  "scripts": {
37
50
  "dev": "concurrently --kill-others --names api,web \"npm:dev:server\" \"npm:dev:client\"",
38
51
  "dev:client": "vite",