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

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 (265) hide show
  1. package/README.md +94 -3
  2. package/dist/canvas.cjs +0 -1
  3. package/dist/canvas.js +1 -2
  4. package/dist/{chunk-EL2YCROY.js → chunk-D64VLIRY.js} +0 -1
  5. package/dist/chunk-G7LQTOA2.js +1439 -0
  6. package/dist/{chunk-4EHUITGU.js → chunk-S5AHUAFG.js} +6 -1
  7. package/dist/{chunk-7O64ZI6Z.js → chunk-TJBGZM6D.js} +0 -1
  8. package/dist/{chunk-P2F2P3JC.js → chunk-UNIXGP4N.js} +6 -1
  9. package/dist/{chunk-A55Q377T.js → chunk-V33T6VX3.js} +6 -1
  10. package/dist/{chunk-TPK25QGK.js → chunk-VZCQXE3P.js} +6 -1
  11. package/dist/{chunk-GB7RZJMG.js → chunk-WEYQMUKY.js} +0 -1
  12. package/dist/{chunk-KZQB35LS.js → chunk-Y6QYUFSC.js} +0 -1
  13. package/dist/controller.cjs +1890 -0
  14. package/dist/controller.d.cts +253 -0
  15. package/dist/controller.d.ts +253 -0
  16. package/dist/controller.js +481 -0
  17. package/dist/{en-CcNUz2tN.d.ts → en-Dg2MtzcJ.d.cts} +6 -0
  18. package/dist/{en-CcNUz2tN.d.cts → en-Dg2MtzcJ.d.ts} +6 -0
  19. package/dist/index.cjs +133 -1
  20. package/dist/index.d.cts +82 -4
  21. package/dist/index.d.ts +82 -4
  22. package/dist/index.js +144 -1422
  23. package/dist/{labels-UuWI7wPo.d.ts → labels-CmiTBLWD.d.cts} +68 -100
  24. package/dist/{labels-D5BrHct4.d.cts → labels-DH1ESUtm.d.ts} +68 -100
  25. package/dist/locales/be.cjs +12 -1
  26. package/dist/locales/be.d.cts +4 -2
  27. package/dist/locales/be.d.ts +4 -2
  28. package/dist/locales/be.js +7 -2
  29. package/dist/locales/bg.cjs +12 -1
  30. package/dist/locales/bg.d.cts +4 -2
  31. package/dist/locales/bg.d.ts +4 -2
  32. package/dist/locales/bg.js +7 -2
  33. package/dist/locales/ca.cjs +12 -1
  34. package/dist/locales/ca.d.cts +4 -2
  35. package/dist/locales/ca.d.ts +4 -2
  36. package/dist/locales/ca.js +7 -2
  37. package/dist/locales/cs.cjs +12 -1
  38. package/dist/locales/cs.d.cts +4 -2
  39. package/dist/locales/cs.d.ts +4 -2
  40. package/dist/locales/cs.js +7 -2
  41. package/dist/locales/da.cjs +12 -1
  42. package/dist/locales/da.d.cts +4 -2
  43. package/dist/locales/da.d.ts +4 -2
  44. package/dist/locales/da.js +7 -2
  45. package/dist/locales/de.cjs +12 -1
  46. package/dist/locales/de.d.cts +4 -2
  47. package/dist/locales/de.d.ts +4 -2
  48. package/dist/locales/de.js +7 -2
  49. package/dist/locales/el.cjs +12 -1
  50. package/dist/locales/el.d.cts +4 -2
  51. package/dist/locales/el.d.ts +4 -2
  52. package/dist/locales/el.js +7 -2
  53. package/dist/locales/en.cjs +12 -1
  54. package/dist/locales/en.d.cts +4 -2
  55. package/dist/locales/en.d.ts +4 -2
  56. package/dist/locales/en.js +7 -2
  57. package/dist/locales/es-419.cjs +16 -2
  58. package/dist/locales/es-419.d.cts +4 -2
  59. package/dist/locales/es-419.d.ts +4 -2
  60. package/dist/locales/es-419.js +6 -4
  61. package/dist/locales/es.cjs +12 -1
  62. package/dist/locales/es.d.cts +4 -2
  63. package/dist/locales/es.d.ts +4 -2
  64. package/dist/locales/es.js +2 -3
  65. package/dist/locales/fi.cjs +12 -1
  66. package/dist/locales/fi.d.cts +4 -2
  67. package/dist/locales/fi.d.ts +4 -2
  68. package/dist/locales/fi.js +7 -2
  69. package/dist/locales/fr-CA.cjs +12 -1
  70. package/dist/locales/fr-CA.d.cts +4 -2
  71. package/dist/locales/fr-CA.d.ts +4 -2
  72. package/dist/locales/fr-CA.js +2 -3
  73. package/dist/locales/fr.cjs +12 -1
  74. package/dist/locales/fr.d.cts +4 -2
  75. package/dist/locales/fr.d.ts +4 -2
  76. package/dist/locales/fr.js +2 -3
  77. package/dist/locales/gn.cjs +12 -1
  78. package/dist/locales/gn.d.cts +4 -2
  79. package/dist/locales/gn.d.ts +4 -2
  80. package/dist/locales/gn.js +7 -2
  81. package/dist/locales/hr.cjs +12 -1
  82. package/dist/locales/hr.d.cts +4 -2
  83. package/dist/locales/hr.d.ts +4 -2
  84. package/dist/locales/hr.js +7 -2
  85. package/dist/locales/ht.cjs +12 -1
  86. package/dist/locales/ht.d.cts +4 -2
  87. package/dist/locales/ht.d.ts +4 -2
  88. package/dist/locales/ht.js +7 -2
  89. package/dist/locales/hu.cjs +12 -1
  90. package/dist/locales/hu.d.cts +4 -2
  91. package/dist/locales/hu.d.ts +4 -2
  92. package/dist/locales/hu.js +7 -2
  93. package/dist/locales/index.cjs +6 -1
  94. package/dist/locales/index.d.cts +6 -4
  95. package/dist/locales/index.d.ts +6 -4
  96. package/dist/locales/index.js +1 -2
  97. package/dist/locales/it.cjs +12 -1
  98. package/dist/locales/it.d.cts +4 -2
  99. package/dist/locales/it.d.ts +4 -2
  100. package/dist/locales/it.js +7 -2
  101. package/dist/locales/nb.cjs +12 -1
  102. package/dist/locales/nb.d.cts +4 -2
  103. package/dist/locales/nb.d.ts +4 -2
  104. package/dist/locales/nb.js +7 -2
  105. package/dist/locales/nl.cjs +12 -1
  106. package/dist/locales/nl.d.cts +4 -2
  107. package/dist/locales/nl.d.ts +4 -2
  108. package/dist/locales/nl.js +7 -2
  109. package/dist/locales/pl.cjs +12 -1
  110. package/dist/locales/pl.d.cts +4 -2
  111. package/dist/locales/pl.d.ts +4 -2
  112. package/dist/locales/pl.js +7 -2
  113. package/dist/locales/pt-BR.cjs +12 -1
  114. package/dist/locales/pt-BR.d.cts +4 -2
  115. package/dist/locales/pt-BR.d.ts +4 -2
  116. package/dist/locales/pt-BR.js +7 -2
  117. package/dist/locales/pt.cjs +12 -1
  118. package/dist/locales/pt.d.cts +4 -2
  119. package/dist/locales/pt.d.ts +4 -2
  120. package/dist/locales/pt.js +7 -2
  121. package/dist/locales/qu.cjs +12 -1
  122. package/dist/locales/qu.d.cts +4 -2
  123. package/dist/locales/qu.d.ts +4 -2
  124. package/dist/locales/qu.js +7 -2
  125. package/dist/locales/ro.cjs +12 -1
  126. package/dist/locales/ro.d.cts +4 -2
  127. package/dist/locales/ro.d.ts +4 -2
  128. package/dist/locales/ro.js +7 -2
  129. package/dist/locales/ru.cjs +12 -1
  130. package/dist/locales/ru.d.cts +4 -2
  131. package/dist/locales/ru.d.ts +4 -2
  132. package/dist/locales/ru.js +7 -2
  133. package/dist/locales/sk.cjs +12 -1
  134. package/dist/locales/sk.d.cts +4 -2
  135. package/dist/locales/sk.d.ts +4 -2
  136. package/dist/locales/sk.js +7 -2
  137. package/dist/locales/sq.cjs +12 -1
  138. package/dist/locales/sq.d.cts +4 -2
  139. package/dist/locales/sq.d.ts +4 -2
  140. package/dist/locales/sq.js +7 -2
  141. package/dist/locales/sr-Latn.cjs +12 -1
  142. package/dist/locales/sr-Latn.d.cts +4 -2
  143. package/dist/locales/sr-Latn.d.ts +4 -2
  144. package/dist/locales/sr-Latn.js +2 -3
  145. package/dist/locales/sr.cjs +12 -1
  146. package/dist/locales/sr.d.cts +4 -2
  147. package/dist/locales/sr.d.ts +4 -2
  148. package/dist/locales/sr.js +2 -3
  149. package/dist/locales/sv.cjs +12 -1
  150. package/dist/locales/sv.d.cts +4 -2
  151. package/dist/locales/sv.d.ts +4 -2
  152. package/dist/locales/sv.js +7 -2
  153. package/dist/locales/tr.cjs +12 -1
  154. package/dist/locales/tr.d.cts +4 -2
  155. package/dist/locales/tr.d.ts +4 -2
  156. package/dist/locales/tr.js +7 -2
  157. package/dist/locales/uk.cjs +12 -1
  158. package/dist/locales/uk.d.cts +4 -2
  159. package/dist/locales/uk.d.ts +4 -2
  160. package/dist/locales/uk.js +7 -2
  161. package/dist/markdown.cjs +0 -1
  162. package/dist/markdown.js +1 -2
  163. package/dist/mcp-app-sandbox.cjs +0 -1
  164. package/dist/mcp-app-sandbox.js +1 -2
  165. package/dist/mcp-app.cjs +0 -1
  166. package/dist/mcp-app.d.cts +491 -3
  167. package/dist/mcp-app.d.ts +491 -3
  168. package/dist/mcp-app.js +2 -3
  169. package/dist/pipeline.cjs +0 -1
  170. package/dist/pipeline.js +1 -2
  171. package/dist/questions-Dw--IEjT.d.cts +100 -0
  172. package/dist/questions-Dw--IEjT.d.ts +100 -0
  173. package/dist/{types-BmvgYUzF.d.cts → types-CQquuZpl.d.cts} +1 -1
  174. package/dist/{types-Cr5gmUts.d.ts → types-CXZfZVP5.d.ts} +1 -1
  175. package/dist/{mcp-app-entry-BA-Ca39z.d.ts → types-DnBCok3z.d.cts} +2 -488
  176. package/dist/{mcp-app-entry-tybABaHg.d.cts → types-DnBCok3z.d.ts} +2 -488
  177. package/package.json +13 -3
  178. package/dist/canvas.cjs.map +0 -1
  179. package/dist/canvas.js.map +0 -1
  180. package/dist/chunk-4EHUITGU.js.map +0 -1
  181. package/dist/chunk-7O64ZI6Z.js.map +0 -1
  182. package/dist/chunk-A55Q377T.js.map +0 -1
  183. package/dist/chunk-EL2YCROY.js.map +0 -1
  184. package/dist/chunk-GB7RZJMG.js.map +0 -1
  185. package/dist/chunk-KZQB35LS.js.map +0 -1
  186. package/dist/chunk-P2F2P3JC.js.map +0 -1
  187. package/dist/chunk-TPK25QGK.js.map +0 -1
  188. package/dist/index.cjs.map +0 -1
  189. package/dist/index.js.map +0 -1
  190. package/dist/locales/be.cjs.map +0 -1
  191. package/dist/locales/be.js.map +0 -1
  192. package/dist/locales/bg.cjs.map +0 -1
  193. package/dist/locales/bg.js.map +0 -1
  194. package/dist/locales/ca.cjs.map +0 -1
  195. package/dist/locales/ca.js.map +0 -1
  196. package/dist/locales/cs.cjs.map +0 -1
  197. package/dist/locales/cs.js.map +0 -1
  198. package/dist/locales/da.cjs.map +0 -1
  199. package/dist/locales/da.js.map +0 -1
  200. package/dist/locales/de.cjs.map +0 -1
  201. package/dist/locales/de.js.map +0 -1
  202. package/dist/locales/el.cjs.map +0 -1
  203. package/dist/locales/el.js.map +0 -1
  204. package/dist/locales/en.cjs.map +0 -1
  205. package/dist/locales/en.js.map +0 -1
  206. package/dist/locales/es-419.cjs.map +0 -1
  207. package/dist/locales/es-419.js.map +0 -1
  208. package/dist/locales/es.cjs.map +0 -1
  209. package/dist/locales/es.js.map +0 -1
  210. package/dist/locales/fi.cjs.map +0 -1
  211. package/dist/locales/fi.js.map +0 -1
  212. package/dist/locales/fr-CA.cjs.map +0 -1
  213. package/dist/locales/fr-CA.js.map +0 -1
  214. package/dist/locales/fr.cjs.map +0 -1
  215. package/dist/locales/fr.js.map +0 -1
  216. package/dist/locales/gn.cjs.map +0 -1
  217. package/dist/locales/gn.js.map +0 -1
  218. package/dist/locales/hr.cjs.map +0 -1
  219. package/dist/locales/hr.js.map +0 -1
  220. package/dist/locales/ht.cjs.map +0 -1
  221. package/dist/locales/ht.js.map +0 -1
  222. package/dist/locales/hu.cjs.map +0 -1
  223. package/dist/locales/hu.js.map +0 -1
  224. package/dist/locales/index.cjs.map +0 -1
  225. package/dist/locales/index.js.map +0 -1
  226. package/dist/locales/it.cjs.map +0 -1
  227. package/dist/locales/it.js.map +0 -1
  228. package/dist/locales/nb.cjs.map +0 -1
  229. package/dist/locales/nb.js.map +0 -1
  230. package/dist/locales/nl.cjs.map +0 -1
  231. package/dist/locales/nl.js.map +0 -1
  232. package/dist/locales/pl.cjs.map +0 -1
  233. package/dist/locales/pl.js.map +0 -1
  234. package/dist/locales/pt-BR.cjs.map +0 -1
  235. package/dist/locales/pt-BR.js.map +0 -1
  236. package/dist/locales/pt.cjs.map +0 -1
  237. package/dist/locales/pt.js.map +0 -1
  238. package/dist/locales/qu.cjs.map +0 -1
  239. package/dist/locales/qu.js.map +0 -1
  240. package/dist/locales/ro.cjs.map +0 -1
  241. package/dist/locales/ro.js.map +0 -1
  242. package/dist/locales/ru.cjs.map +0 -1
  243. package/dist/locales/ru.js.map +0 -1
  244. package/dist/locales/sk.cjs.map +0 -1
  245. package/dist/locales/sk.js.map +0 -1
  246. package/dist/locales/sq.cjs.map +0 -1
  247. package/dist/locales/sq.js.map +0 -1
  248. package/dist/locales/sr-Latn.cjs.map +0 -1
  249. package/dist/locales/sr-Latn.js.map +0 -1
  250. package/dist/locales/sr.cjs.map +0 -1
  251. package/dist/locales/sr.js.map +0 -1
  252. package/dist/locales/sv.cjs.map +0 -1
  253. package/dist/locales/sv.js.map +0 -1
  254. package/dist/locales/tr.cjs.map +0 -1
  255. package/dist/locales/tr.js.map +0 -1
  256. package/dist/locales/uk.cjs.map +0 -1
  257. package/dist/locales/uk.js.map +0 -1
  258. package/dist/markdown.cjs.map +0 -1
  259. package/dist/markdown.js.map +0 -1
  260. package/dist/mcp-app-sandbox.cjs.map +0 -1
  261. package/dist/mcp-app-sandbox.js.map +0 -1
  262. package/dist/mcp-app.cjs.map +0 -1
  263. package/dist/mcp-app.js.map +0 -1
  264. package/dist/pipeline.cjs.map +0 -1
  265. package/dist/pipeline.js.map +0 -1
@@ -0,0 +1,253 @@
1
+ import { RunFile, RunResult, AskUserAnswer, ToolApprovalMode, AppToolCallRequest, AppSamplingRequest, AppSamplingView, AppSamplingResult, CompactResult, IUsage, BrowserAgentConfig, AgentEventHandler, Agent } from '@dudko.dev/agent-web';
2
+ import { PendingQuestion } from './index.cjs';
3
+ import { ChatAttachment, ChatMessage, AgentUiState, ApprovalView } from './index.cjs';
4
+
5
+ /** How a controller behaves. `useAgent`'s options are these plus `deps`. */
6
+ interface AgentControllerOptions {
7
+ /** Invoked for every raw agent event, after the internal reducer folds it. */
8
+ onEvent?: AgentEventHandler;
9
+ /**
10
+ * Build the agent as soon as the controller is created (default `true`). With `false` it waits
11
+ * for `reload()`.
12
+ */
13
+ autoStart?: boolean;
14
+ /** Cap the retained raw event log length (default 200; 0 = unbounded). */
15
+ maxEvents?: number;
16
+ /**
17
+ * Where "Always allow" given to an MCP App's tool is kept (the core's `appGrantKey`: the
18
+ * server's identity on its connection, the tool, the app's resource): handed to the core's
19
+ * `approveAppToolCall`, so a new agent - an `update`, a new controller - keeps the grant. A new
20
+ * connection (the server removed and added again, reconnected, another server under the same
21
+ * name) starts with none, and a rule's 'deny' or 'read-only' still refuses. Default: one set for
22
+ * the page's life (this module's, shared by every controller of the page - and so by every
23
+ * `useAgent`); a reload forgets it, as sampling's "Always for this app" is forgotten. Pass your
24
+ * own set to scope it differently.
25
+ */
26
+ appToolGrants?: Set<string>;
27
+ /**
28
+ * Route tool-consent requests into the controller (`pendingApprovals`, `approve`, `deny`) when
29
+ * the config has no `toolApproval.onRequest` of its own (default true).
30
+ */
31
+ approvals?: boolean;
32
+ /**
33
+ * Route the model's questions (`ask_user`) into the controller (`pendingQuestions`,
34
+ * `answerQuestion`) when the config turns `askUser` on without an `onQuestion` of its own
35
+ * (`askUser: true`, or `{ timeoutMs, maxPerRun }`; default true). A host's own `onQuestion` wins;
36
+ * `askUser` off stays off.
37
+ */
38
+ questions?: boolean;
39
+ /**
40
+ * What the run never showed the agent — a message sent while the answer was being
41
+ * written — is sent as the next message, once the run has ended (default true).
42
+ * A run the user stopped never does this: those messages stay in `undelivered`
43
+ * for the UI to offer (`sendUndelivered` / `discardUndelivered`).
44
+ */
45
+ followUp?: boolean;
46
+ /**
47
+ * Follow a run of the current conversation that goes on in ANOTHER tab of the site (default
48
+ * true; needs a `memory` the tabs share - `IndexedDBStore` - and `sessionLock` not `'off'`): its
49
+ * steps and answer show here live (`remote`) and `isRunning` is true meanwhile. Writing to it or
50
+ * stopping it is explicit: `sendRemote` / `stopRemote`. Consent requests stay in that tab.
51
+ */
52
+ watch?: boolean;
53
+ }
54
+ interface ApproveOptions {
55
+ /** Also allow this tool for the rest of the agent's life ("always allow"). */
56
+ remember?: boolean;
57
+ }
58
+ interface RunGoalOptions {
59
+ /**
60
+ * What the transcript shows for this turn when it differs from what the
61
+ * agent receives — e.g. the typed text without the attached files' contents.
62
+ * `''` with `attachments`: a message of attachments only - the transcript shows them, no text.
63
+ */
64
+ label?: string;
65
+ /** Images sent to the model with the goal (vision models; see `agent.capabilities`). */
66
+ images?: RunFile[];
67
+ /** PDFs / files / URLs sent to the model with the goal. */
68
+ files?: RunFile[];
69
+ /** What the transcript shows as attached to this turn (thumbnails, chips). */
70
+ attachments?: ChatAttachment[];
71
+ }
72
+ /**
73
+ * A tool call an MCP App asks for, as `approveAppToolCall` takes it: the core's request, plus
74
+ * `frame` - the host's id of the frame that asked (`AppView.id`), put on the request's `app`.
75
+ */
76
+ type AppToolCall = AppToolCallRequest & {
77
+ frame?: string;
78
+ };
79
+ /** What `update` does besides taking the new config. */
80
+ interface AgentControllerUpdate {
81
+ /** New options (replace the old ones whole). Omitted: the options stay. */
82
+ options?: AgentControllerOptions;
83
+ /**
84
+ * Build a new agent from the config (default `true`): what a `deps` change does in `useAgent`.
85
+ * `false` only takes the config (and options) as the latest - read by the next build, the next
86
+ * run, `reset` - and applies what is live: a new `toolApproval.mode`, another `memory` /
87
+ * `sessionLock` / `learning.store` to follow other tabs through. A changed `autoStart` rebuilds
88
+ * either way.
89
+ */
90
+ rebuild?: boolean;
91
+ }
92
+ /** Everything a view renders, at one moment. Immutable: a new object on every change. */
93
+ interface AgentControllerSnapshot extends AgentUiState {
94
+ /** The memory session runs use (config.sessionId until switched). */
95
+ sessionId: string;
96
+ /** This tab's run, or the conversation's run in another tab followed here. */
97
+ isRunning: boolean;
98
+ /** The agent is built and nothing runs (here or, for this conversation, in another tab). */
99
+ isReady: boolean;
100
+ /** Tool calls waiting for the user's consent (another tab's are answered there, never here). */
101
+ pendingApprovals: ApprovalView[];
102
+ /**
103
+ * The model's questions waiting for the person in THIS tab (`ask_user`, when the controller
104
+ * answers them - see `AgentControllerOptions.questions`), oldest first, each with `expiresAt`
105
+ * when the core stops waiting. Another tab's questions are in `questions` (marked `remote`),
106
+ * never here.
107
+ */
108
+ pendingQuestions: PendingQuestion[];
109
+ /** The consent policy in effect. */
110
+ approvalMode: ToolApprovalMode;
111
+ }
112
+ /** What a view calls. Each keeps its identity for the controller's life. */
113
+ interface AgentControllerActions {
114
+ /**
115
+ * Run a goal. Resolves with the RunResult, or `undefined` if it couldn't start. When the
116
+ * conversation is running in another tab, the run waits for it (`sessionBusy`; the core's
117
+ * `sessionLock`) and then goes, with that run's turn in its memory.
118
+ */
119
+ run: (goal: string, options?: RunGoalOptions) => Promise<RunResult | undefined>;
120
+ /**
121
+ * Abort the in-flight run of THIS tab (observed between phases and mid-stream), going or waiting
122
+ * for the session. A run followed from another tab is stopped only by `stopRemote`.
123
+ */
124
+ stop: () => void;
125
+ /**
126
+ * Stop the conversation's run in another tab (`remote`), when that tab allows it (the core's
127
+ * `sessionRelay`). A no-op when none is followed.
128
+ */
129
+ stopRemote: () => void;
130
+ /**
131
+ * Say something to the conversation's run in another tab (`remote`): it reaches that run's
132
+ * model at its next boundary, marked as from another tab. `false` when no such run is followed
133
+ * or the text is empty. Until that tab answers, the text is in `queued` marked
134
+ * `sentTo: 'another-tab'` (shown "waiting for the other tab to pick it up") - with no time
135
+ * limit: a tab the browser froze answers when it wakes. Taken, that run's own entry takes its
136
+ * place; refused, or that run ended without it, it comes back in `undelivered`.
137
+ */
138
+ sendRemote: (text: string) => boolean;
139
+ /**
140
+ * Say something to the agent while it works. It is shown to the model at the next
141
+ * boundary (between tool calls, or when the step ends — where the replanner then
142
+ * re-checks the plan), never cutting a tool call short. `false` when no run is in
143
+ * flight or the text is empty — then use `run`. Watch `queued` / `notes`. Only this tab's run:
144
+ * for the conversation's run in another tab, `sendRemote`.
145
+ */
146
+ send: (text: string) => boolean;
147
+ /** Stop ONE running subagent (`SubagentView.id`); the run and its siblings go on. */
148
+ stopSubagent: (id: string) => boolean;
149
+ /**
150
+ * Delete one of the agent's own lessons (`learned`), e.g. one gone stale. Resolves false when
151
+ * there is no such lesson (or no agent yet). Needs `config.learning`.
152
+ */
153
+ forgetSkill: (name: string) => Promise<boolean>;
154
+ /** Send the `undelivered` messages (a stopped run's leftovers) as a new message. */
155
+ sendUndelivered: () => void;
156
+ /** Drop the `undelivered` messages. */
157
+ discardUndelivered: () => void;
158
+ /** Clear the whole conversation — transcript, current run, and the agent's memory. */
159
+ reset: () => void;
160
+ /** Rebuild the agent now (e.g. after the user stores a new API key). */
161
+ reload: () => void;
162
+ /** Let a pending call run (`remember` → never ask for this tool again). */
163
+ approve: (id: string, options?: ApproveOptions) => void;
164
+ /** Refuse a pending call; the model is told and works around it. */
165
+ deny: (id: string, reason?: string) => void;
166
+ /**
167
+ * Answer one of `pendingQuestions`: `{ choice: [label] }` (labels as offered; several only when
168
+ * `multiple`), `{ other: text }` (only when `allowOther`) or `{ declined: true }`. False when no
169
+ * such question is waiting (answered, expired, stopped). The answer goes to the core and is not
170
+ * kept here.
171
+ */
172
+ answerQuestion: (id: string, answer: AskUserAnswer) => boolean;
173
+ /** Switch the consent policy live — the "autopilot" toggle. */
174
+ setApprovalMode: (mode: ToolApprovalMode) => void;
175
+ /**
176
+ * Ask the agent's consent gate about a tool call an MCP App wants to make (the core's
177
+ * `Agent.approveAppToolCall`: the same rules and modes as the model's calls - 'autopilot' asks
178
+ * nothing -, "always allow" kept apart from the model's and for the page's life, not the
179
+ * agent's: `appToolGrants`). The request joins `pendingApprovals` with `app` set,
180
+ * and waits across runs - a run ending or `stop()` does not answer it; `signal` does (the
181
+ * app's frame going away). `call.frame` (the frame's `AppView.id`) is put on the request's
182
+ * `app`, so it is drawn with THAT frame; `call.serverId` (`McpAppTool.serverId`) scopes the
183
+ * grant to the server's connection. Resolves when the call may go; throws when it may not.
184
+ */
185
+ approveAppToolCall: (call: AppToolCall, signal?: AbortSignal) => Promise<void>;
186
+ /**
187
+ * Answer a completion an MCP App asks for (its `sampling/createMessage`) with the agent's model:
188
+ * the core's `Agent.sampleForApp` - checked, rate-limited per app, put to `approve` (shown
189
+ * exactly what the model gets), then sent with no tools and none of the conversation. The
190
+ * tokens count in `totalUsage` (a `usage` event of phase `'app'`, never the run's); the answer
191
+ * is returned to the caller and never reaches the transcript, the memory or the model's context.
192
+ * Throws the core's `AppSamplingError` (its `rpcCode` is what the app is told).
193
+ */
194
+ sampleForApp: (request: AppSamplingRequest, approve: (view: AppSamplingView) => Promise<boolean>, signal?: AbortSignal) => Promise<AppSamplingResult>;
195
+ /** Summarise the stored transcript now (needs `config.memory`). */
196
+ compact: () => Promise<CompactResult | undefined>;
197
+ /**
198
+ * Switch to another conversation: its id becomes the memory session of the
199
+ * next runs, and its saved transcript (chat history) is shown.
200
+ */
201
+ loadChat: (chat: {
202
+ sessionId: string;
203
+ messages?: ChatMessage[];
204
+ totalUsage?: IUsage;
205
+ }) => void;
206
+ }
207
+ /**
208
+ * The agent as a store: `getSnapshot` + `subscribe` (React's `useSyncExternalStore` contract),
209
+ * the methods a view calls, `update` and `dispose`.
210
+ */
211
+ interface AgentController extends AgentControllerActions {
212
+ /** The current snapshot: the same object until something changes, then a new one. */
213
+ getSnapshot: () => AgentControllerSnapshot;
214
+ /**
215
+ * Call `listener` after every change (never at once on subscribing). Returns the unsubscribe.
216
+ * After `dispose` nothing is called and subscribing does nothing.
217
+ */
218
+ subscribe: (listener: () => void) => () => void;
219
+ /** Take a new config (and options): see {@link AgentControllerUpdate}. */
220
+ update: (config: BrowserAgentConfig, how?: AgentControllerUpdate) => void;
221
+ /**
222
+ * Tear everything down: the in-flight run aborted, its consent requests and questions answered
223
+ * (denied / declined: "the view was closed"), the build in progress dropped, the other-tab
224
+ * channels closed, pending timers cleared, every listener dropped. The snapshot stays as it was;
225
+ * every method is then a no-op (`run` resolves `undefined`). Idempotent.
226
+ */
227
+ dispose: () => void;
228
+ /** The built agent, once ready (`undefined` while building, after a failure or `dispose`). */
229
+ readonly agent: Agent | undefined;
230
+ /** Whether `dispose` was called. */
231
+ readonly disposed: boolean;
232
+ }
233
+ /**
234
+ * What a controller's snapshot is before anything happened: what a view renders before it has a
235
+ * controller (React's first render, which must not create one; a server render).
236
+ */
237
+ declare const createInitialAgentSnapshot: (config: BrowserAgentConfig) => AgentControllerSnapshot;
238
+ /**
239
+ * Build the agent from `config` and keep its UI state: see {@link AgentController}. The agent is
240
+ * built at once (unless `autoStart: false` or the config has no `model` yet), and other tabs are
241
+ * followed from now on - so create it when the view appears and `dispose` it when the view goes.
242
+ *
243
+ * ```js
244
+ * const chat = createAgentController({ model, credentials, toolApproval: { mode: 'ask-writes' } })
245
+ * const off = chat.subscribe(() => render(chat.getSnapshot()))
246
+ * button.onclick = () => chat.run(input.value)
247
+ * // later: off(); chat.dispose()
248
+ * ```
249
+ */
250
+ declare const createAgentController: (initialConfig: BrowserAgentConfig, initialOptions?: AgentControllerOptions) => AgentController;
251
+
252
+ export { createAgentController, createInitialAgentSnapshot };
253
+ export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RunGoalOptions };
@@ -0,0 +1,253 @@
1
+ import { RunFile, RunResult, AskUserAnswer, ToolApprovalMode, AppToolCallRequest, AppSamplingRequest, AppSamplingView, AppSamplingResult, CompactResult, IUsage, BrowserAgentConfig, AgentEventHandler, Agent } from '@dudko.dev/agent-web';
2
+ import { PendingQuestion } from './index.js';
3
+ import { ChatAttachment, ChatMessage, AgentUiState, ApprovalView } from './index.js';
4
+
5
+ /** How a controller behaves. `useAgent`'s options are these plus `deps`. */
6
+ interface AgentControllerOptions {
7
+ /** Invoked for every raw agent event, after the internal reducer folds it. */
8
+ onEvent?: AgentEventHandler;
9
+ /**
10
+ * Build the agent as soon as the controller is created (default `true`). With `false` it waits
11
+ * for `reload()`.
12
+ */
13
+ autoStart?: boolean;
14
+ /** Cap the retained raw event log length (default 200; 0 = unbounded). */
15
+ maxEvents?: number;
16
+ /**
17
+ * Where "Always allow" given to an MCP App's tool is kept (the core's `appGrantKey`: the
18
+ * server's identity on its connection, the tool, the app's resource): handed to the core's
19
+ * `approveAppToolCall`, so a new agent - an `update`, a new controller - keeps the grant. A new
20
+ * connection (the server removed and added again, reconnected, another server under the same
21
+ * name) starts with none, and a rule's 'deny' or 'read-only' still refuses. Default: one set for
22
+ * the page's life (this module's, shared by every controller of the page - and so by every
23
+ * `useAgent`); a reload forgets it, as sampling's "Always for this app" is forgotten. Pass your
24
+ * own set to scope it differently.
25
+ */
26
+ appToolGrants?: Set<string>;
27
+ /**
28
+ * Route tool-consent requests into the controller (`pendingApprovals`, `approve`, `deny`) when
29
+ * the config has no `toolApproval.onRequest` of its own (default true).
30
+ */
31
+ approvals?: boolean;
32
+ /**
33
+ * Route the model's questions (`ask_user`) into the controller (`pendingQuestions`,
34
+ * `answerQuestion`) when the config turns `askUser` on without an `onQuestion` of its own
35
+ * (`askUser: true`, or `{ timeoutMs, maxPerRun }`; default true). A host's own `onQuestion` wins;
36
+ * `askUser` off stays off.
37
+ */
38
+ questions?: boolean;
39
+ /**
40
+ * What the run never showed the agent — a message sent while the answer was being
41
+ * written — is sent as the next message, once the run has ended (default true).
42
+ * A run the user stopped never does this: those messages stay in `undelivered`
43
+ * for the UI to offer (`sendUndelivered` / `discardUndelivered`).
44
+ */
45
+ followUp?: boolean;
46
+ /**
47
+ * Follow a run of the current conversation that goes on in ANOTHER tab of the site (default
48
+ * true; needs a `memory` the tabs share - `IndexedDBStore` - and `sessionLock` not `'off'`): its
49
+ * steps and answer show here live (`remote`) and `isRunning` is true meanwhile. Writing to it or
50
+ * stopping it is explicit: `sendRemote` / `stopRemote`. Consent requests stay in that tab.
51
+ */
52
+ watch?: boolean;
53
+ }
54
+ interface ApproveOptions {
55
+ /** Also allow this tool for the rest of the agent's life ("always allow"). */
56
+ remember?: boolean;
57
+ }
58
+ interface RunGoalOptions {
59
+ /**
60
+ * What the transcript shows for this turn when it differs from what the
61
+ * agent receives — e.g. the typed text without the attached files' contents.
62
+ * `''` with `attachments`: a message of attachments only - the transcript shows them, no text.
63
+ */
64
+ label?: string;
65
+ /** Images sent to the model with the goal (vision models; see `agent.capabilities`). */
66
+ images?: RunFile[];
67
+ /** PDFs / files / URLs sent to the model with the goal. */
68
+ files?: RunFile[];
69
+ /** What the transcript shows as attached to this turn (thumbnails, chips). */
70
+ attachments?: ChatAttachment[];
71
+ }
72
+ /**
73
+ * A tool call an MCP App asks for, as `approveAppToolCall` takes it: the core's request, plus
74
+ * `frame` - the host's id of the frame that asked (`AppView.id`), put on the request's `app`.
75
+ */
76
+ type AppToolCall = AppToolCallRequest & {
77
+ frame?: string;
78
+ };
79
+ /** What `update` does besides taking the new config. */
80
+ interface AgentControllerUpdate {
81
+ /** New options (replace the old ones whole). Omitted: the options stay. */
82
+ options?: AgentControllerOptions;
83
+ /**
84
+ * Build a new agent from the config (default `true`): what a `deps` change does in `useAgent`.
85
+ * `false` only takes the config (and options) as the latest - read by the next build, the next
86
+ * run, `reset` - and applies what is live: a new `toolApproval.mode`, another `memory` /
87
+ * `sessionLock` / `learning.store` to follow other tabs through. A changed `autoStart` rebuilds
88
+ * either way.
89
+ */
90
+ rebuild?: boolean;
91
+ }
92
+ /** Everything a view renders, at one moment. Immutable: a new object on every change. */
93
+ interface AgentControllerSnapshot extends AgentUiState {
94
+ /** The memory session runs use (config.sessionId until switched). */
95
+ sessionId: string;
96
+ /** This tab's run, or the conversation's run in another tab followed here. */
97
+ isRunning: boolean;
98
+ /** The agent is built and nothing runs (here or, for this conversation, in another tab). */
99
+ isReady: boolean;
100
+ /** Tool calls waiting for the user's consent (another tab's are answered there, never here). */
101
+ pendingApprovals: ApprovalView[];
102
+ /**
103
+ * The model's questions waiting for the person in THIS tab (`ask_user`, when the controller
104
+ * answers them - see `AgentControllerOptions.questions`), oldest first, each with `expiresAt`
105
+ * when the core stops waiting. Another tab's questions are in `questions` (marked `remote`),
106
+ * never here.
107
+ */
108
+ pendingQuestions: PendingQuestion[];
109
+ /** The consent policy in effect. */
110
+ approvalMode: ToolApprovalMode;
111
+ }
112
+ /** What a view calls. Each keeps its identity for the controller's life. */
113
+ interface AgentControllerActions {
114
+ /**
115
+ * Run a goal. Resolves with the RunResult, or `undefined` if it couldn't start. When the
116
+ * conversation is running in another tab, the run waits for it (`sessionBusy`; the core's
117
+ * `sessionLock`) and then goes, with that run's turn in its memory.
118
+ */
119
+ run: (goal: string, options?: RunGoalOptions) => Promise<RunResult | undefined>;
120
+ /**
121
+ * Abort the in-flight run of THIS tab (observed between phases and mid-stream), going or waiting
122
+ * for the session. A run followed from another tab is stopped only by `stopRemote`.
123
+ */
124
+ stop: () => void;
125
+ /**
126
+ * Stop the conversation's run in another tab (`remote`), when that tab allows it (the core's
127
+ * `sessionRelay`). A no-op when none is followed.
128
+ */
129
+ stopRemote: () => void;
130
+ /**
131
+ * Say something to the conversation's run in another tab (`remote`): it reaches that run's
132
+ * model at its next boundary, marked as from another tab. `false` when no such run is followed
133
+ * or the text is empty. Until that tab answers, the text is in `queued` marked
134
+ * `sentTo: 'another-tab'` (shown "waiting for the other tab to pick it up") - with no time
135
+ * limit: a tab the browser froze answers when it wakes. Taken, that run's own entry takes its
136
+ * place; refused, or that run ended without it, it comes back in `undelivered`.
137
+ */
138
+ sendRemote: (text: string) => boolean;
139
+ /**
140
+ * Say something to the agent while it works. It is shown to the model at the next
141
+ * boundary (between tool calls, or when the step ends — where the replanner then
142
+ * re-checks the plan), never cutting a tool call short. `false` when no run is in
143
+ * flight or the text is empty — then use `run`. Watch `queued` / `notes`. Only this tab's run:
144
+ * for the conversation's run in another tab, `sendRemote`.
145
+ */
146
+ send: (text: string) => boolean;
147
+ /** Stop ONE running subagent (`SubagentView.id`); the run and its siblings go on. */
148
+ stopSubagent: (id: string) => boolean;
149
+ /**
150
+ * Delete one of the agent's own lessons (`learned`), e.g. one gone stale. Resolves false when
151
+ * there is no such lesson (or no agent yet). Needs `config.learning`.
152
+ */
153
+ forgetSkill: (name: string) => Promise<boolean>;
154
+ /** Send the `undelivered` messages (a stopped run's leftovers) as a new message. */
155
+ sendUndelivered: () => void;
156
+ /** Drop the `undelivered` messages. */
157
+ discardUndelivered: () => void;
158
+ /** Clear the whole conversation — transcript, current run, and the agent's memory. */
159
+ reset: () => void;
160
+ /** Rebuild the agent now (e.g. after the user stores a new API key). */
161
+ reload: () => void;
162
+ /** Let a pending call run (`remember` → never ask for this tool again). */
163
+ approve: (id: string, options?: ApproveOptions) => void;
164
+ /** Refuse a pending call; the model is told and works around it. */
165
+ deny: (id: string, reason?: string) => void;
166
+ /**
167
+ * Answer one of `pendingQuestions`: `{ choice: [label] }` (labels as offered; several only when
168
+ * `multiple`), `{ other: text }` (only when `allowOther`) or `{ declined: true }`. False when no
169
+ * such question is waiting (answered, expired, stopped). The answer goes to the core and is not
170
+ * kept here.
171
+ */
172
+ answerQuestion: (id: string, answer: AskUserAnswer) => boolean;
173
+ /** Switch the consent policy live — the "autopilot" toggle. */
174
+ setApprovalMode: (mode: ToolApprovalMode) => void;
175
+ /**
176
+ * Ask the agent's consent gate about a tool call an MCP App wants to make (the core's
177
+ * `Agent.approveAppToolCall`: the same rules and modes as the model's calls - 'autopilot' asks
178
+ * nothing -, "always allow" kept apart from the model's and for the page's life, not the
179
+ * agent's: `appToolGrants`). The request joins `pendingApprovals` with `app` set,
180
+ * and waits across runs - a run ending or `stop()` does not answer it; `signal` does (the
181
+ * app's frame going away). `call.frame` (the frame's `AppView.id`) is put on the request's
182
+ * `app`, so it is drawn with THAT frame; `call.serverId` (`McpAppTool.serverId`) scopes the
183
+ * grant to the server's connection. Resolves when the call may go; throws when it may not.
184
+ */
185
+ approveAppToolCall: (call: AppToolCall, signal?: AbortSignal) => Promise<void>;
186
+ /**
187
+ * Answer a completion an MCP App asks for (its `sampling/createMessage`) with the agent's model:
188
+ * the core's `Agent.sampleForApp` - checked, rate-limited per app, put to `approve` (shown
189
+ * exactly what the model gets), then sent with no tools and none of the conversation. The
190
+ * tokens count in `totalUsage` (a `usage` event of phase `'app'`, never the run's); the answer
191
+ * is returned to the caller and never reaches the transcript, the memory or the model's context.
192
+ * Throws the core's `AppSamplingError` (its `rpcCode` is what the app is told).
193
+ */
194
+ sampleForApp: (request: AppSamplingRequest, approve: (view: AppSamplingView) => Promise<boolean>, signal?: AbortSignal) => Promise<AppSamplingResult>;
195
+ /** Summarise the stored transcript now (needs `config.memory`). */
196
+ compact: () => Promise<CompactResult | undefined>;
197
+ /**
198
+ * Switch to another conversation: its id becomes the memory session of the
199
+ * next runs, and its saved transcript (chat history) is shown.
200
+ */
201
+ loadChat: (chat: {
202
+ sessionId: string;
203
+ messages?: ChatMessage[];
204
+ totalUsage?: IUsage;
205
+ }) => void;
206
+ }
207
+ /**
208
+ * The agent as a store: `getSnapshot` + `subscribe` (React's `useSyncExternalStore` contract),
209
+ * the methods a view calls, `update` and `dispose`.
210
+ */
211
+ interface AgentController extends AgentControllerActions {
212
+ /** The current snapshot: the same object until something changes, then a new one. */
213
+ getSnapshot: () => AgentControllerSnapshot;
214
+ /**
215
+ * Call `listener` after every change (never at once on subscribing). Returns the unsubscribe.
216
+ * After `dispose` nothing is called and subscribing does nothing.
217
+ */
218
+ subscribe: (listener: () => void) => () => void;
219
+ /** Take a new config (and options): see {@link AgentControllerUpdate}. */
220
+ update: (config: BrowserAgentConfig, how?: AgentControllerUpdate) => void;
221
+ /**
222
+ * Tear everything down: the in-flight run aborted, its consent requests and questions answered
223
+ * (denied / declined: "the view was closed"), the build in progress dropped, the other-tab
224
+ * channels closed, pending timers cleared, every listener dropped. The snapshot stays as it was;
225
+ * every method is then a no-op (`run` resolves `undefined`). Idempotent.
226
+ */
227
+ dispose: () => void;
228
+ /** The built agent, once ready (`undefined` while building, after a failure or `dispose`). */
229
+ readonly agent: Agent | undefined;
230
+ /** Whether `dispose` was called. */
231
+ readonly disposed: boolean;
232
+ }
233
+ /**
234
+ * What a controller's snapshot is before anything happened: what a view renders before it has a
235
+ * controller (React's first render, which must not create one; a server render).
236
+ */
237
+ declare const createInitialAgentSnapshot: (config: BrowserAgentConfig) => AgentControllerSnapshot;
238
+ /**
239
+ * Build the agent from `config` and keep its UI state: see {@link AgentController}. The agent is
240
+ * built at once (unless `autoStart: false` or the config has no `model` yet), and other tabs are
241
+ * followed from now on - so create it when the view appears and `dispose` it when the view goes.
242
+ *
243
+ * ```js
244
+ * const chat = createAgentController({ model, credentials, toolApproval: { mode: 'ask-writes' } })
245
+ * const off = chat.subscribe(() => render(chat.getSnapshot()))
246
+ * button.onclick = () => chat.run(input.value)
247
+ * // later: off(); chat.dispose()
248
+ * ```
249
+ */
250
+ declare const createAgentController: (initialConfig: BrowserAgentConfig, initialOptions?: AgentControllerOptions) => AgentController;
251
+
252
+ export { createAgentController, createInitialAgentSnapshot };
253
+ export type { AgentController, AgentControllerActions, AgentControllerOptions, AgentControllerSnapshot, AgentControllerUpdate, AppToolCall, ApproveOptions, RunGoalOptions };