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

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
@@ -108,7 +108,7 @@ the core's own by the tests. The rest goes through
108
108
  controller when it builds an agent (`createAgent`, the run's control, another tab's run), the
109
109
  chat history when it announces a write, `followOtherTabs` / `listenToOtherTabs` when there is a
110
110
  database to follow (pass `loadAgentCore` instead of the core's module: the follow starts once it
111
- resolves). `agentCoreIfLoaded()` answers without importing.
111
+ resolves). `agentCoreIfLoaded()` answers without importing; `AgentCoreModule` is the module's type.
112
112
 
113
113
  So a chat whose agent is on a server (`createRemoteAgentController`, the element's `backend`)
114
114
  never loads the core: the support chat of agent.dudko.dev went from 1.35 MB (399 KB gzip) to
@@ -228,6 +228,20 @@ const chat = createRemoteAgentController({
228
228
  })
229
229
  ```
230
230
 
231
+ The options (`RemoteAgentOptions`):
232
+
233
+ | Option | What |
234
+ | --- | --- |
235
+ | `baseUrl` | The gateway's base URL (required) |
236
+ | `sessionId` | A session to continue (below) |
237
+ | `headers` | Headers for every request: an object, or a function called before each one, async allowed (`RemoteAgentHeaders`) - a visitor token, a captcha token while there is no session yet |
238
+ | `credentials` | `fetch`'s `credentials` (default `'same-origin'`; `'include'` sends a cookie cross-site - the gateway's anonymous visitor cookie) |
239
+ | `fetch` | The `fetch` to use (default the global one) |
240
+ | `onSessionId` | Told every change of the session (started, forgotten, `reset()`) |
241
+ | `onEvent` | Every event folded, the swarm's `turn.started` / `turn.ended` markers as they came (a `tool.approval-resolved` that only says the turn parked is not told) |
242
+ | `maxEvents` | The raw event log kept in the snapshot (default 200; 0 = unbounded) |
243
+ | `reconnect` | `{ minMs, maxMs }`: the stream's reconnect backoff (default 1 s, doubled up to 30 s) |
244
+
231
245
  - `run(text)` - the first message `POST /sessions`, the next ones `POST /sessions/:id/messages`
232
246
  (each with an id: a retried POST is harmless); shown at once, its turn takes its place. Resolves
233
247
  with the turn's result, `undefined` when it failed (the error said, the words back in
@@ -251,8 +265,20 @@ const chat = createRemoteAgentController({
251
265
  `approveAppToolCall` / `sampleForApp` (throw), `setApprovalMode`, `stopRemote`, `update` (do
252
266
  nothing). `reset()` forgets the session; `loadChat({ sessionId })` opens another; `reload()` opens
253
267
  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.
268
+ - `onEvent(event, { live, id })` - every event as the reducer took it. `live` is true only for a
269
+ turn that a message sent through THIS controller started, as it happens: never for the replay of
270
+ a session's log (a reload, `loadChat`, a `sessionId` given) nor for a turn another tab or client
271
+ started on the same session - so a host can act on what its own message caused (open a page the
272
+ agent named, say) without acting again on every reload. `id` is the event's cursor on the
273
+ stream (`<turn>:<attempt>:<seq>`); the controller's own events (a request that failed) are live,
274
+ without an id.
275
+ - `createInitialRemoteSnapshot({ sessionId })` is its snapshot before one exists. `createSseParser`
276
+ (SSE text, fed chunk by chunk, to `RemoteFrame`s - `id`, `data` - and `retry:`), `parseRemoteCursor`
277
+ (`<turn>:<attempt>:<seq>`), `remoteEventScope` and `remoteEventOf` (a Node core event to the
278
+ shape the reducer folds) are its pure parts.
279
+ - **Nothing of the core is loaded** for it: the `./controller` entry takes the core only through
280
+ `loadAgentCore()`, which this controller never calls - see
281
+ [The core, loaded when needed](#the-core-loaded-when-needed).
256
282
 
257
283
  The React package's `useRemoteAgent` / `<RemoteAgentProvider>` and the element's `backend`
258
284
  attribute are this controller.
@@ -1592,6 +1592,9 @@ var createRemoteAgentController = (options) => {
1592
1592
  let turnIds = [];
1593
1593
  let turnFinal = false;
1594
1594
  let waiting = [];
1595
+ const sentHere = /* @__PURE__ */ new Set();
1596
+ let frameInfo;
1597
+ const turnIsOurs = () => turnIds.some((id) => sentHere.has(id));
1595
1598
  const runs = /* @__PURE__ */ new Map();
1596
1599
  const fold = (action, attempt = 0, scope) => {
1597
1600
  if (turn) {
@@ -1609,7 +1612,7 @@ var createRemoteAgentController = (options) => {
1609
1612
  };
1610
1613
  const tell = (e) => {
1611
1614
  try {
1612
- options.onEvent?.(e);
1615
+ options.onEvent?.(e, frameInfo ?? { live: true });
1613
1616
  } catch {
1614
1617
  }
1615
1618
  };
@@ -1665,13 +1668,26 @@ var createRemoteAgentController = (options) => {
1665
1668
  }
1666
1669
  const raw = parsed?.event;
1667
1670
  if (!raw || typeof raw !== "object" || typeof raw.type !== "string") return;
1671
+ try {
1672
+ foldFrame(frame, raw);
1673
+ } finally {
1674
+ frameInfo = void 0;
1675
+ }
1676
+ };
1677
+ const foldFrame = (frame, raw) => {
1668
1678
  const cursor2 = parseRemoteCursor(frame.id);
1679
+ const info = () => ({
1680
+ live: turnIsOurs(),
1681
+ ...frame.id !== void 0 ? { id: frame.id } : {}
1682
+ });
1683
+ frameInfo = info();
1669
1684
  const attempt = cursor2?.attempt ?? 0;
1670
1685
  const shown = remoteEventOf(raw);
1671
1686
  if (raw.type === "turn.started") {
1672
1687
  const ids = Array.isArray(raw.messageIds) ? raw.messageIds.filter((x) => typeof x === "string") : [];
1673
1688
  waiting = waiting.filter((w) => !ids.includes(w.id));
1674
1689
  turnIds = ids;
1690
+ frameInfo = info();
1675
1691
  turnFinal = false;
1676
1692
  apply({ type: "status", status: "running" });
1677
1693
  turn = { number: cursor2?.turn ?? 0, start: state, entries: [] };
@@ -1840,6 +1856,7 @@ var createRemoteAgentController = (options) => {
1840
1856
  apply({ type: "status", status: "running" });
1841
1857
  apply({ type: "submitted", goal: shown });
1842
1858
  waiting.push({ id, text });
1859
+ sentHere.add(id);
1843
1860
  const ended = new Promise((resolve) => runs.set(id, resolve));
1844
1861
  try {
1845
1862
  await sendMessage(id, text);
@@ -1882,6 +1899,7 @@ var createRemoteAgentController = (options) => {
1882
1899
  if (disposed || state.status !== "running" || !clean || !sessionId) return false;
1883
1900
  const id = newMessageId();
1884
1901
  waiting.push({ id, text: clean });
1902
+ sentHere.add(id);
1885
1903
  fold(event({ type: "message.queued", id, text: clean }));
1886
1904
  void post(`/sessions/${encodeURIComponent(sessionId)}/messages`, { text: clean, id }).catch(
1887
1905
  (err) => {
@@ -36,9 +36,9 @@ interface RemoteAgentOptions {
36
36
  * Every event, after the reducer folded it: the server agent's, a turn's start as the core's
37
37
  * `run.start` and its end as `final` / `stopped` / `error` (as a local run's), the swarm's
38
38
  * `turn.started` / `turn.ended` markers as they came. A run's `tool.approval-resolved` that only
39
- * says it parked is not told.
39
+ * says it parked is not told. `info` says whether it is LIVE - see {@link RemoteEventInfo}.
40
40
  */
41
- onEvent?: (event: AgentEvent) => void;
41
+ onEvent?: (event: AgentEvent, info: RemoteEventInfo) => void;
42
42
  /** Cap the retained raw event log (default 200; 0 = unbounded). */
43
43
  maxEvents?: number;
44
44
  /**
@@ -50,6 +50,18 @@ interface RemoteAgentOptions {
50
50
  maxMs?: number;
51
51
  };
52
52
  }
53
+ /**
54
+ * What `onEvent` is told with an event. `live`: the event belongs to a turn that a message sent
55
+ * THROUGH THIS CONTROLLER started (`run` / `send`), as it happens - not a turn replayed from the
56
+ * session's log (a reload, `loadChat`, a session given at start), nor one another tab or client
57
+ * started on the same session. An event of the controller's own (a request that failed) is live.
58
+ * `id`: the event's cursor on the gateway's stream (`<turn>:<attempt>:<seq>`), unique in the
59
+ * session; absent for the controller's own events.
60
+ */
61
+ interface RemoteEventInfo {
62
+ live: boolean;
63
+ id?: string;
64
+ }
53
65
  /** One frame of the gateway's stream: `id:` (the cursor) and the JSON of `data:`. */
54
66
  interface RemoteFrame {
55
67
  id?: string;
@@ -357,4 +369,4 @@ declare const createInitialAgentSnapshot: (config: BrowserAgentConfig) => AgentC
357
369
  declare const createAgentController: (initialConfig: BrowserAgentConfig, initialOptions?: AgentControllerOptions) => AgentController;
358
370
 
359
371
  export { createAgentController, createInitialAgentSnapshot, createInitialRemoteSnapshot, createRemoteAgentController, createSseParser, parseRemoteCursor, remoteEventOf, remoteEventScope };
360
- export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RemoteAgentHeaders, RemoteAgentOptions, RemoteFrame, RunGoalOptions };
372
+ export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RemoteAgentHeaders, RemoteAgentOptions, RemoteEventInfo, RemoteFrame, RunGoalOptions };
@@ -36,9 +36,9 @@ interface RemoteAgentOptions {
36
36
  * Every event, after the reducer folded it: the server agent's, a turn's start as the core's
37
37
  * `run.start` and its end as `final` / `stopped` / `error` (as a local run's), the swarm's
38
38
  * `turn.started` / `turn.ended` markers as they came. A run's `tool.approval-resolved` that only
39
- * says it parked is not told.
39
+ * says it parked is not told. `info` says whether it is LIVE - see {@link RemoteEventInfo}.
40
40
  */
41
- onEvent?: (event: AgentEvent) => void;
41
+ onEvent?: (event: AgentEvent, info: RemoteEventInfo) => void;
42
42
  /** Cap the retained raw event log (default 200; 0 = unbounded). */
43
43
  maxEvents?: number;
44
44
  /**
@@ -50,6 +50,18 @@ interface RemoteAgentOptions {
50
50
  maxMs?: number;
51
51
  };
52
52
  }
53
+ /**
54
+ * What `onEvent` is told with an event. `live`: the event belongs to a turn that a message sent
55
+ * THROUGH THIS CONTROLLER started (`run` / `send`), as it happens - not a turn replayed from the
56
+ * session's log (a reload, `loadChat`, a session given at start), nor one another tab or client
57
+ * started on the same session. An event of the controller's own (a request that failed) is live.
58
+ * `id`: the event's cursor on the gateway's stream (`<turn>:<attempt>:<seq>`), unique in the
59
+ * session; absent for the controller's own events.
60
+ */
61
+ interface RemoteEventInfo {
62
+ live: boolean;
63
+ id?: string;
64
+ }
53
65
  /** One frame of the gateway's stream: `id:` (the cursor) and the JSON of `data:`. */
54
66
  interface RemoteFrame {
55
67
  id?: string;
@@ -357,4 +369,4 @@ declare const createInitialAgentSnapshot: (config: BrowserAgentConfig) => AgentC
357
369
  declare const createAgentController: (initialConfig: BrowserAgentConfig, initialOptions?: AgentControllerOptions) => AgentController;
358
370
 
359
371
  export { createAgentController, createInitialAgentSnapshot, createInitialRemoteSnapshot, createRemoteAgentController, createSseParser, parseRemoteCursor, remoteEventOf, remoteEventScope };
360
- export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RemoteAgentHeaders, RemoteAgentOptions, RemoteFrame, RunGoalOptions };
372
+ export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RemoteAgentHeaders, RemoteAgentOptions, RemoteEventInfo, RemoteFrame, RunGoalOptions };
@@ -141,6 +141,9 @@ var createRemoteAgentController = (options) => {
141
141
  let turnIds = [];
142
142
  let turnFinal = false;
143
143
  let waiting = [];
144
+ const sentHere = /* @__PURE__ */ new Set();
145
+ let frameInfo;
146
+ const turnIsOurs = () => turnIds.some((id) => sentHere.has(id));
144
147
  const runs = /* @__PURE__ */ new Map();
145
148
  const fold = (action, attempt = 0, scope) => {
146
149
  if (turn) {
@@ -158,7 +161,7 @@ var createRemoteAgentController = (options) => {
158
161
  };
159
162
  const tell = (e) => {
160
163
  try {
161
- options.onEvent?.(e);
164
+ options.onEvent?.(e, frameInfo ?? { live: true });
162
165
  } catch {
163
166
  }
164
167
  };
@@ -214,13 +217,26 @@ var createRemoteAgentController = (options) => {
214
217
  }
215
218
  const raw = parsed?.event;
216
219
  if (!raw || typeof raw !== "object" || typeof raw.type !== "string") return;
220
+ try {
221
+ foldFrame(frame, raw);
222
+ } finally {
223
+ frameInfo = void 0;
224
+ }
225
+ };
226
+ const foldFrame = (frame, raw) => {
217
227
  const cursor2 = parseRemoteCursor(frame.id);
228
+ const info = () => ({
229
+ live: turnIsOurs(),
230
+ ...frame.id !== void 0 ? { id: frame.id } : {}
231
+ });
232
+ frameInfo = info();
218
233
  const attempt = cursor2?.attempt ?? 0;
219
234
  const shown = remoteEventOf(raw);
220
235
  if (raw.type === "turn.started") {
221
236
  const ids = Array.isArray(raw.messageIds) ? raw.messageIds.filter((x) => typeof x === "string") : [];
222
237
  waiting = waiting.filter((w) => !ids.includes(w.id));
223
238
  turnIds = ids;
239
+ frameInfo = info();
224
240
  turnFinal = false;
225
241
  apply({ type: "status", status: "running" });
226
242
  turn = { number: cursor2?.turn ?? 0, start: state, entries: [] };
@@ -389,6 +405,7 @@ var createRemoteAgentController = (options) => {
389
405
  apply({ type: "status", status: "running" });
390
406
  apply({ type: "submitted", goal: shown });
391
407
  waiting.push({ id, text });
408
+ sentHere.add(id);
392
409
  const ended = new Promise((resolve) => runs.set(id, resolve));
393
410
  try {
394
411
  await sendMessage(id, text);
@@ -431,6 +448,7 @@ var createRemoteAgentController = (options) => {
431
448
  if (disposed || state.status !== "running" || !clean || !sessionId) return false;
432
449
  const id = newMessageId();
433
450
  waiting.push({ id, text: clean });
451
+ sentHere.add(id);
434
452
  fold(event({ type: "message.queued", id, text: clean }));
435
453
  void post(`/sessions/${encodeURIComponent(sessionId)}/messages`, { text: clean, id }).catch(
436
454
  (err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudko.dev/agent-web-ui",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "private": false,
5
5
  "description": "The framework-free UI logic of @dudko.dev/agent-web, the in-browser LLM agent: the headless reducer that turns the agent's events into UI state, every UI string, the composer's and transcript's pure helpers, voice, saved chats, the sandboxed-frame and MCP Apps security cores, the MCP Apps sandbox page for a server, and the pipeline editor's logic - plus the stylesheets. No React: build a front end for any framework on it.",
6
6
  "type": "module",