@dudko.dev/agent-web-ui 0.0.3 → 0.0.5

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 (88) hide show
  1. package/README.md +78 -6
  2. package/dist/{chunk-UNIXGP4N.js → chunk-627OXMFG.js} +2 -0
  3. package/dist/{chunk-G7LQTOA2.js → chunk-NHU6WQM5.js} +61 -6
  4. package/dist/controller.cjs +621 -20
  5. package/dist/controller.d.cts +110 -3
  6. package/dist/controller.d.ts +110 -3
  7. package/dist/controller.js +575 -17
  8. package/dist/index.cjs +81 -14
  9. package/dist/index.d.cts +28 -10
  10. package/dist/index.d.ts +28 -10
  11. package/dist/index.js +27 -8
  12. package/dist/{labels-DH1ESUtm.d.ts → labels-CCPL7t17.d.ts} +8 -1
  13. package/dist/{labels-CmiTBLWD.d.cts → labels-ipy-ydkp.d.cts} +8 -1
  14. package/dist/locales/be.cjs +2 -0
  15. package/dist/locales/be.js +1 -1
  16. package/dist/locales/bg.cjs +2 -0
  17. package/dist/locales/bg.js +1 -1
  18. package/dist/locales/ca.cjs +2 -0
  19. package/dist/locales/ca.js +1 -1
  20. package/dist/locales/cs.cjs +2 -0
  21. package/dist/locales/cs.js +1 -1
  22. package/dist/locales/da.cjs +2 -0
  23. package/dist/locales/da.js +1 -1
  24. package/dist/locales/de.cjs +2 -0
  25. package/dist/locales/de.js +1 -1
  26. package/dist/locales/el.cjs +2 -0
  27. package/dist/locales/el.js +1 -1
  28. package/dist/locales/en.cjs +2 -0
  29. package/dist/locales/en.js +1 -1
  30. package/dist/locales/es-419.cjs +2 -0
  31. package/dist/locales/es-419.js +1 -1
  32. package/dist/locales/es.cjs +2 -0
  33. package/dist/locales/es.js +1 -1
  34. package/dist/locales/fi.cjs +2 -0
  35. package/dist/locales/fi.js +1 -1
  36. package/dist/locales/fr-CA.cjs +2 -0
  37. package/dist/locales/fr-CA.js +1 -1
  38. package/dist/locales/fr.cjs +2 -0
  39. package/dist/locales/fr.js +1 -1
  40. package/dist/locales/gn.cjs +2 -0
  41. package/dist/locales/gn.js +1 -1
  42. package/dist/locales/hr.cjs +2 -0
  43. package/dist/locales/hr.js +1 -1
  44. package/dist/locales/ht.cjs +2 -0
  45. package/dist/locales/ht.js +1 -1
  46. package/dist/locales/hu.cjs +2 -0
  47. package/dist/locales/hu.js +1 -1
  48. package/dist/locales/index.cjs +2 -0
  49. package/dist/locales/index.js +1 -1
  50. package/dist/locales/it.cjs +2 -0
  51. package/dist/locales/it.js +1 -1
  52. package/dist/locales/nb.cjs +2 -0
  53. package/dist/locales/nb.js +1 -1
  54. package/dist/locales/nl.cjs +2 -0
  55. package/dist/locales/nl.js +1 -1
  56. package/dist/locales/pl.cjs +2 -0
  57. package/dist/locales/pl.js +1 -1
  58. package/dist/locales/pt-BR.cjs +2 -0
  59. package/dist/locales/pt-BR.js +1 -1
  60. package/dist/locales/pt.cjs +2 -0
  61. package/dist/locales/pt.js +1 -1
  62. package/dist/locales/qu.cjs +2 -0
  63. package/dist/locales/qu.js +1 -1
  64. package/dist/locales/ro.cjs +2 -0
  65. package/dist/locales/ro.js +1 -1
  66. package/dist/locales/ru.cjs +2 -0
  67. package/dist/locales/ru.js +1 -1
  68. package/dist/locales/sk.cjs +2 -0
  69. package/dist/locales/sk.js +1 -1
  70. package/dist/locales/sq.cjs +2 -0
  71. package/dist/locales/sq.js +1 -1
  72. package/dist/locales/sr-Latn.cjs +2 -0
  73. package/dist/locales/sr-Latn.js +1 -1
  74. package/dist/locales/sr.cjs +2 -0
  75. package/dist/locales/sr.js +1 -1
  76. package/dist/locales/sv.cjs +2 -0
  77. package/dist/locales/sv.js +1 -1
  78. package/dist/locales/tr.cjs +2 -0
  79. package/dist/locales/tr.js +1 -1
  80. package/dist/locales/uk.cjs +2 -0
  81. package/dist/locales/uk.js +1 -1
  82. package/dist/markdown.cjs +36 -0
  83. package/dist/markdown.d.cts +27 -1
  84. package/dist/markdown.d.ts +27 -1
  85. package/dist/markdown.js +33 -0
  86. package/dist/mcp-app.cjs +1 -4
  87. package/dist/mcp-app.js +1 -2
  88. package/package.json +1 -1
package/README.md CHANGED
@@ -63,21 +63,22 @@ on them.
63
63
  npm i @dudko.dev/agent-web-ui @dudko.dev/agent-web
64
64
  ```
65
65
 
66
- `@dudko.dev/agent-web` is a peer dependency (one core per page; the type imports and the few
67
- runtime helpers this package uses come from it). There is no other dependency. A React app does
66
+ `@dudko.dev/agent-web` is a peer dependency (one core per page; the type imports come from it, and
67
+ the core itself is imported only when something needs it - see [The core, loaded when needed](#the-core-loaded-when-needed)).
68
+ There is no other dependency. A React app does
68
69
  not install this package itself: `@dudko.dev/agent-web-react` depends on it.
69
70
 
70
71
  ## Entries
71
72
 
72
73
  | Import | What | Imports |
73
74
  | --- | --- | --- |
74
- | `@dudko.dev/agent-web-ui` | the reducer and its types, the labels, the pure helpers, voice, saved chats, the sandboxed frame | the core |
75
- | `@dudko.dev/agent-web-ui/mcp-app` | MCP Apps on the page's side: the frame's policy and shell, the bridge, the link to a sandbox page, an app's requests | the core |
75
+ | `@dudko.dev/agent-web-ui` | the reducer and its types, the labels, the pure helpers, voice, saved chats, the sandboxed frame | the core, dynamically (saved chats, other tabs) |
76
+ | `@dudko.dev/agent-web-ui/mcp-app` | MCP Apps on the page's side: the frame's policy and shell, the bridge, the link to a sandbox page, an app's requests | nothing |
76
77
  | `@dudko.dev/agent-web-ui/mcp-app-sandbox` | the MCP Apps sandbox page for a SERVER (a Worker, Node, Deno, Bun): the page, its headers, a `Request` → `Response` handler | nothing |
77
78
  | `@dudko.dev/agent-web-ui/pipeline` | the pipeline editor's logic: the reducer of a pipeline run, the editing helpers, the code generator, the editor's strings | the core |
78
79
  | `@dudko.dev/agent-web-ui/canvas` | the generic canvas's maths (viewport, curves, hit tests, layout order) and its strings | nothing |
79
- | `@dudko.dev/agent-web-ui/controller` | the agent as a store any front end wraps: `createAgentController` (see [Controller](#controller)) | the core (its `createAgent`) |
80
- | `@dudko.dev/agent-web-ui/markdown` | the answers' Markdown as a tree any front end draws: the parser, the parser of an answer still being written, `safeHref`, the renderer's helpers (see [Markdown](#markdown)) | nothing |
80
+ | `@dudko.dev/agent-web-ui/controller` | the agent as a store any front end wraps: `createAgentController`, and `createRemoteAgentController` for an agent on a server (see [Controller](#controller)) | the core, dynamically, when an agent is built in the page |
81
+ | `@dudko.dev/agent-web-ui/markdown` | the answers' Markdown as a tree any front end draws: the parser, the parser of an answer still being written, `safeHref`, the link allowlist, the renderer's helpers (see [Markdown](#markdown)) | nothing |
81
82
  | `@dudko.dev/agent-web-ui/locales` | the chat's labels in your language, loaded on demand: `loadAgentLabels(tag)`, the list of tags, `labelsFromCatalog` | nothing |
82
83
  | `@dudko.dev/agent-web-ui/locales/<tag>` | one language's labels (`labels`) and its catalog (`catalog`) | nothing |
83
84
  | `@dudko.dev/agent-web-ui/styles.css` | the chat's stylesheet | - |
@@ -96,6 +97,24 @@ Every entry is ESM and CommonJS with types. The main entry never mentions the co
96
97
  chunk it loads - so a host that uses none of them carries none of them (`tests/entries.test.ts`
97
98
  reads the built files).
98
99
 
100
+ ### The core, loaded when needed
101
+
102
+ No entry but `./pipeline` imports the core statically. A bundler that keeps one export of the core
103
+ keeps all of its top-level code - the AI SDK, zod, the providers -, so the few values the chat
104
+ needs of it (which media types are pictures, which text, the mark of a replayed write, the check of
105
+ an MCP App's origins; `fileKindOf` and `mediaFamilyOf` answer with them) are copies here, held to
106
+ the core's own by the tests. The rest goes through
107
+ `loadAgentCore()` - a dynamic `import('@dudko.dev/agent-web')`, the same module every time: the
108
+ controller when it builds an agent (`createAgent`, the run's control, another tab's run), the
109
+ chat history when it announces a write, `followOtherTabs` / `listenToOtherTabs` when there is a
110
+ database to follow (pass `loadAgentCore` instead of the core's module: the follow starts once it
111
+ resolves). `agentCoreIfLoaded()` answers without importing.
112
+
113
+ So a chat whose agent is on a server (`createRemoteAgentController`, the element's `backend`)
114
+ never loads the core: the support chat of agent.dudko.dev went from 1.35 MB (399 KB gzip) to
115
+ 0.62 MB (182 KB gzip) on its first open - React and the chat. A page that builds an agent imports
116
+ the core itself anyway (its model, its stores); your bundler puts the core in a chunk of its own.
117
+
99
118
  ## The reducer is the seam
100
119
 
101
120
  Everything a chat shows is a function of the events the agent emitted. Keep a state, fold every
@@ -191,6 +210,53 @@ allow.onclick = () => chat.approve(chat.getSnapshot().pendingApprovals[0].id)
191
210
  // when the view goes away: off(); chat.dispose()
192
211
  ```
193
212
 
213
+ ### An agent on a server
214
+
215
+ `createRemoteAgentController(options)` is the same store for an agent that runs on a SERVER - the
216
+ swarm's gateway (`examples/swarm*` in the repository: Temporal + Postgres, Cloudflare, Google
217
+ Cloud, AWS - one HTTP API). The same snapshot and the same methods, so any view of the controller
218
+ draws it unchanged; nothing of the agent is in the page (no model, no key, no tool).
219
+
220
+ ```js
221
+ import { createRemoteAgentController } from '@dudko.dev/agent-web-ui/controller'
222
+
223
+ const chat = createRemoteAgentController({
224
+ baseUrl: 'https://support.example.com/agent',
225
+ headers: async () => ({ authorization: `Bearer ${await visitorToken()}` }), // or credentials: 'include'
226
+ sessionId: sessionStorage.getItem('chat') ?? undefined,
227
+ onSessionId: (id) => (id ? sessionStorage.setItem('chat', id) : sessionStorage.removeItem('chat')),
228
+ })
229
+ ```
230
+
231
+ - `run(text)` - the first message `POST /sessions`, the next ones `POST /sessions/:id/messages`
232
+ (each with an id: a retried POST is harmless); shown at once, its turn takes its place. Resolves
233
+ with the turn's result, `undefined` when it failed (the error said, the words back in
234
+ `undelivered`). Text only: images or files are refused in words.
235
+ - The turn streams in from `GET /sessions/:id/events` (SSE read through `fetch`, so `headers` can
236
+ carry a token), folded by the same reducer - the server's agent emits the core's events, the
237
+ swarm's `turn.started` / `turn.ended` frame each turn. A dropped stream is opened again with
238
+ `Last-Event-ID` after `reconnect.minMs` (default 1 s, or the gateway's `retry:`), doubled up to
239
+ `reconnect.maxMs` (30 s); a frame resets it. A later attempt's copy of a step, the answer or the
240
+ plan replaces the earlier one (`remoteEventScope`): delivery is at-least-once.
241
+ - `sessionId` - a session to continue: its stream is replayed, the transcript comes back. 404:
242
+ forgotten; 409 `session_ended`: the next message starts a new one. `onSessionId` is told every
243
+ change.
244
+ - `pendingApprovals` - a call the server's policy asks about parks the turn; `approve(id)` /
245
+ `deny(id, reason)` post `{ approved }` to `POST /sessions/:id/answers/:requestId` (no
246
+ `remember`: every call is decided on its own on a server). `stop()` is
247
+ `POST /sessions/:id/stop`. `send(text)` while a turn runs posts a message that waits in `queued`
248
+ for its own turn.
249
+ - Not available, answered as before an agent exists: `agent` (`undefined`), `answerQuestion`,
250
+ `stopSubagent`, `sendRemote`, `forgetSkill` (`false`), `compact` (`undefined`),
251
+ `approveAppToolCall` / `sampleForApp` (throw), `setApprovalMode`, `stopRemote`, `update` (do
252
+ nothing). `reset()` forgets the session; `loadChat({ sessionId })` opens another; `reload()` opens
253
+ the stream again now. `dispose()` closes the stream and clears the reconnect timer.
254
+ - `createInitialRemoteSnapshot({ sessionId })` is its snapshot before one exists. `createSseParser`,
255
+ `parseRemoteCursor`, `remoteEventScope` and `remoteEventOf` are its pure parts.
256
+
257
+ The React package's `useRemoteAgent` / `<RemoteAgentProvider>` and the element's `backend`
258
+ attribute are this controller.
259
+
194
260
  ## Labels
195
261
 
196
262
  `defaultLabels` is every string the chat shows; counted phrases are functions so that languages
@@ -240,6 +306,12 @@ It is a security boundary for a host - the text is the model's:
240
306
  are refused; a whole code span that is one carries `path`. A renderer draws it as a control that
241
307
  opens the file in the page, never as `<a href>`. A link's words hold no link of their own.
242
308
  - **No picture** is loaded from an answer: `![a](…)` is its `!` and a link (or a file link).
309
+ - **A link allowlist**, for a chat that should link only to its own sites (opt-in, a renderer's
310
+ `linkAllowed`): `linkHostAllowed(href, hosts)` / `linkPolicyOf(hosts)` - each entry `host` or
311
+ `host/path`; `http(s)` only, the host exactly (no subdomain, no look-alike), no user, password or
312
+ port, a path entry a prefix by whole segments after the URL parser's normalisation.
313
+ `parseLinkHosts(attribute)` reads a list separated by blanks or commas; a malformed entry empties
314
+ it (no link kept - never every link).
243
315
  - **Linear on any text**: one pass for code spans, lookups for brackets and destinations, one
244
316
  opener stack per kind of emphasis, list nesting bounded - long runs of `*`, `_`, `[`, backticks,
245
317
  `>`, `|` or ever-deeper lists cost what plain text does (`tests/markdown.test.ts` times them).
@@ -55,6 +55,8 @@ var labelsFromCatalog = (tag, catalog) => {
55
55
  planned: t("planned"),
56
56
  replanned: (_mode, reason) => t("replanned", { reason }),
57
57
  placeholder: t("placeholder"),
58
+ // The catalogs' placeholder names no attachment: the same words for a text-only box.
59
+ placeholderTextOnly: t("placeholder"),
58
60
  placeholderBusy: t("placeholderBusy"),
59
61
  message: t("message"),
60
62
  commands: t("commands"),
@@ -119,10 +119,27 @@ var withoutOwnWords = (event) => {
119
119
  }
120
120
  };
121
121
 
122
+ // src/core-copies.ts
123
+ var REPLAY_NOTE_OF_CORE = "[not run again: this exact call already succeeded in an earlier attempt at this step, and its effect is in place. Its result then:]";
124
+ var isReplayedOutput = (output) => typeof output === "string" && output.startsWith(REPLAY_NOTE_OF_CORE);
125
+ var MEDIA_IMAGE_TYPES_OF_CORE = {
126
+ "image/png": "png",
127
+ "image/jpeg": "jpg",
128
+ "image/gif": "gif",
129
+ "image/webp": "webp"
130
+ };
131
+ var MEDIA_AUDIO_TYPES_OF_CORE = {
132
+ "audio/mpeg": "mp3",
133
+ "audio/wav": "wav",
134
+ "audio/ogg": "ogg",
135
+ "audio/flac": "flac",
136
+ "audio/aac": "aac",
137
+ "audio/mp4": "m4a",
138
+ "audio/webm": "webm"
139
+ };
140
+ var isTextMimeOfCore = (mime) => /^text\/|json|xml|yaml|javascript|typescript|svg/.test(mime);
141
+
122
142
  // src/state.ts
123
- import {
124
- isReplayedToolOutput
125
- } from "@dudko.dev/agent-web";
126
143
  var approxTokens = (value) => {
127
144
  if (value === void 0) return 0;
128
145
  let text;
@@ -858,7 +875,7 @@ var applyEvent = (state, event, ctx) => {
858
875
  ok: event.ok,
859
876
  status: "done",
860
877
  outputTokens: approxTokens(event.output),
861
- ...isReplayedToolOutput(event.output) ? { replayed: true } : {},
878
+ ...isReplayedOutput(event.output) ? { replayed: true } : {},
862
879
  ...ctx.at !== void 0 && c.startedAt !== void 0 ? { durationMs: Math.max(0, ctx.at - c.startedAt) } : {}
863
880
  };
864
881
  }
@@ -905,7 +922,7 @@ var applyEvent = (state, event, ctx) => {
905
922
  ...streamed?.durationMs !== void 0 ? { durationMs: streamed.durationMs } : {},
906
923
  // With tiers the list holds every attempt's calls, in the order they were made.
907
924
  ...streamed?.attempt !== void 0 ? { attempt: streamed.attempt } : {},
908
- ...isReplayedToolOutput(c.output) ? { replayed: true } : {},
925
+ ...isReplayedOutput(c.output) ? { replayed: true } : {},
909
926
  // The MCP App the call came with (only the stream told it).
910
927
  ...streamed?.app !== void 0 ? { app: streamed.app } : {}
911
928
  };
@@ -1383,6 +1400,21 @@ var createApprovalQueue = () => {
1383
1400
  };
1384
1401
  };
1385
1402
 
1403
+ // src/core-later.ts
1404
+ var loading;
1405
+ var loaded;
1406
+ var loadAgentCore = () => {
1407
+ if (!loading) {
1408
+ const next = import("@dudko.dev/agent-web").then((core) => loaded = core);
1409
+ loading = next;
1410
+ next.catch(() => {
1411
+ if (loading === next) loading = void 0;
1412
+ });
1413
+ }
1414
+ return loading;
1415
+ };
1416
+ var agentCoreIfLoaded = () => loaded;
1417
+
1386
1418
  // src/util.ts
1387
1419
  var errMessage = (err) => err instanceof Error ? err.message : String(err);
1388
1420
  var coalesce = (fn, ms = 100) => {
@@ -1401,7 +1433,25 @@ var coalesce = (fn, ms = 100) => {
1401
1433
  }
1402
1434
  };
1403
1435
  };
1404
- var listenToOtherTabs = (core, dbName, listener) => typeof core.onStorageChange === "function" && dbName ? core.onStorageChange(dbName, listener) : () => void 0;
1436
+ var listenToOtherTabs = (core, dbName, listener) => {
1437
+ if (!dbName) return () => void 0;
1438
+ if (typeof core === "function") {
1439
+ let closed = false;
1440
+ let off;
1441
+ core().then(
1442
+ (module) => {
1443
+ if (!closed) off = listenToOtherTabs(module, dbName, listener);
1444
+ },
1445
+ () => {
1446
+ }
1447
+ );
1448
+ return () => {
1449
+ closed = true;
1450
+ off?.();
1451
+ };
1452
+ }
1453
+ return typeof core.onStorageChange === "function" ? core.onStorageChange(dbName, listener) : () => void 0;
1454
+ };
1405
1455
  var followOtherTabs = (core, dbName, accept, settle, ms = 100) => {
1406
1456
  const later = coalesce(settle, ms);
1407
1457
  const off = listenToOtherTabs(core, dbName, (change) => {
@@ -1415,6 +1465,9 @@ var followOtherTabs = (core, dbName, accept, settle, ms = 100) => {
1415
1465
  var remoteAnswerOf = (ok, stillWatching) => ok ? "taken" : stillWatching ? "refused" : "unknown";
1416
1466
 
1417
1467
  export {
1468
+ MEDIA_IMAGE_TYPES_OF_CORE,
1469
+ MEDIA_AUDIO_TYPES_OF_CORE,
1470
+ isTextMimeOfCore,
1418
1471
  ASK_USER_TOOL_NAME,
1419
1472
  QUESTION_TIMEOUT_MS,
1420
1473
  questionTimeoutOf,
@@ -1431,6 +1484,8 @@ export {
1431
1484
  agentStatusOf,
1432
1485
  agentStateReducer,
1433
1486
  createApprovalQueue,
1487
+ loadAgentCore,
1488
+ agentCoreIfLoaded,
1434
1489
  errMessage,
1435
1490
  coalesce,
1436
1491
  listenToOtherTabs,