@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,481 @@
1
+ import {
2
+ agentStateReducer,
3
+ agentStatusOf,
4
+ askUserWith,
5
+ createApprovalQueue,
6
+ createInitialAgentState,
7
+ createQuestionQueue,
8
+ errMessage,
9
+ followOtherTabs,
10
+ lessonsOf,
11
+ pendingApprovalsOf,
12
+ questionTimeoutOf,
13
+ remoteAnswerOf
14
+ } from "./chunk-G7LQTOA2.js";
15
+
16
+ // src/controller.ts
17
+ import * as core from "@dudko.dev/agent-web";
18
+ import {
19
+ createAgent,
20
+ createRunControl
21
+ } from "@dudko.dev/agent-web";
22
+ var dbNameOf = (store) => {
23
+ const name = store?.dbName;
24
+ return typeof name === "string" && name ? name : void 0;
25
+ };
26
+ var modeOf = (config) => config.toolApproval?.mode ?? "autopilot";
27
+ var pageAppToolGrants = /* @__PURE__ */ new Set();
28
+ var snapshotOf = (state, extra) => {
29
+ const remoteRunning = state.remote?.running === true;
30
+ return Object.freeze({
31
+ ...state,
32
+ // `running` over ready / idle only: a build in progress or an error stays visible.
33
+ status: agentStatusOf(state),
34
+ isRunning: state.status === "running" || remoteRunning,
35
+ isReady: state.status === "ready" && !remoteRunning,
36
+ // Another tab's consent requests are answered there, never here.
37
+ pendingApprovals: pendingApprovalsOf(state),
38
+ pendingQuestions: extra.pendingQuestions,
39
+ approvalMode: extra.approvalMode,
40
+ sessionId: extra.sessionId
41
+ });
42
+ };
43
+ var createInitialAgentSnapshot = (config) => snapshotOf(createInitialAgentState(), {
44
+ sessionId: config.sessionId ?? "default",
45
+ pendingQuestions: [],
46
+ approvalMode: modeOf(config)
47
+ });
48
+ var createAgentController = (initialConfig, initialOptions = {}) => {
49
+ let config = initialConfig;
50
+ let options = initialOptions;
51
+ let disposed = false;
52
+ let state = createInitialAgentState();
53
+ let pendingQuestions = [];
54
+ let sessionId = initialConfig.sessionId ?? "default";
55
+ let approvalMode = modeOf(initialConfig);
56
+ let snapshot;
57
+ const listeners = /* @__PURE__ */ new Set();
58
+ const changed = () => {
59
+ snapshot = void 0;
60
+ for (const listener of [...listeners]) {
61
+ try {
62
+ listener();
63
+ } catch (err) {
64
+ queueMicrotask(() => {
65
+ throw err;
66
+ });
67
+ }
68
+ }
69
+ };
70
+ const dispatch = (action) => {
71
+ if (disposed) return;
72
+ const next = agentStateReducer(state, action);
73
+ if (next === state) return;
74
+ state = next;
75
+ changed();
76
+ };
77
+ let agent;
78
+ let abort;
79
+ let control;
80
+ let generation = 0;
81
+ const approvals = createApprovalQueue();
82
+ const appApprovals = createApprovalQueue();
83
+ const questionQueue = createQuestionQueue({
84
+ timeoutMs: () => questionTimeoutOf(config.askUser),
85
+ onChange: (pending) => {
86
+ if (disposed) return;
87
+ pendingQuestions = pending;
88
+ changed();
89
+ }
90
+ });
91
+ const uiOnRequest = (req, ctx) => (req.app ? appApprovals : approvals).handler(req, ctx);
92
+ const settleAll = (decision) => approvals.settleAll(decision);
93
+ let mode = approvalMode;
94
+ let configMode = approvalMode;
95
+ const setApprovalMode = (next) => {
96
+ if (disposed) return;
97
+ mode = next;
98
+ agent?.setToolApprovalMode(next);
99
+ if (approvalMode === next) return;
100
+ approvalMode = next;
101
+ changed();
102
+ };
103
+ let buildStatus = { status: "idle" };
104
+ const setBuildStatus = (next) => {
105
+ buildStatus = next;
106
+ if (!abort) dispatch({ type: "status", ...next });
107
+ };
108
+ let cancelBuild;
109
+ const build = () => {
110
+ cancelBuild?.();
111
+ cancelBuild = void 0;
112
+ agent = void 0;
113
+ const cfg = config;
114
+ const shouldBuild = (options.autoStart !== false || generation > 0) && Boolean(cfg.model);
115
+ if (!shouldBuild) {
116
+ setBuildStatus({ status: "idle" });
117
+ return;
118
+ }
119
+ const routeApprovals = options.approvals !== false && !cfg.toolApproval?.onRequest;
120
+ const askUser = options.questions !== false ? askUserWith(cfg.askUser, questionQueue.handler) : cfg.askUser;
121
+ const built = {
122
+ ...cfg,
123
+ ...routeApprovals ? { toolApproval: { ...cfg.toolApproval, mode, onRequest: uiOnRequest } } : {},
124
+ ...askUser !== cfg.askUser ? { askUser } : {}
125
+ };
126
+ let cancelled = false;
127
+ cancelBuild = () => {
128
+ cancelled = true;
129
+ };
130
+ setBuildStatus({ status: "initializing" });
131
+ createAgent(built).then((next) => {
132
+ if (cancelled) return;
133
+ next.setToolApprovalMode(mode);
134
+ agent = next;
135
+ dispatch({ type: "notebook", lessons: lessonsOf(next.listLearnedSkills()) });
136
+ setBuildStatus({ status: "ready" });
137
+ }).catch((err) => {
138
+ if (cancelled) return;
139
+ setBuildStatus({ status: "error", error: errMessage(err) });
140
+ });
141
+ };
142
+ let watch;
143
+ let watched;
144
+ let started = false;
145
+ const closeWatch = () => {
146
+ watch?.close();
147
+ watch = void 0;
148
+ };
149
+ const syncWatch = () => {
150
+ const db = options.watch !== false && config.sessionLock !== "off" ? dbNameOf(config.memory) : void 0;
151
+ if (watched && watched.db === db && watched.sessionId === sessionId) return;
152
+ closeWatch();
153
+ watched = { db, sessionId };
154
+ const watchSession2 = core.watchSession;
155
+ if (!db || typeof watchSession2 !== "function") return;
156
+ watch = watchSession2({
157
+ dbName: db,
158
+ sessionId,
159
+ onEvent: (event) => {
160
+ if (started) return;
161
+ dispatch({
162
+ type: "event",
163
+ event,
164
+ remote: true,
165
+ maxEvents: options.maxEvents,
166
+ at: Date.now()
167
+ });
168
+ },
169
+ onState: (s) => {
170
+ if (started && s.running) return;
171
+ dispatch({
172
+ type: "remote",
173
+ remote: {
174
+ running: s.running,
175
+ ...s.runId ? { runId: s.runId } : {},
176
+ ...s.ended ? { ended: s.ended } : {}
177
+ }
178
+ });
179
+ }
180
+ });
181
+ };
182
+ let skillsDb;
183
+ let unfollowSkills;
184
+ const syncSkills = () => {
185
+ const db = dbNameOf(typeof config.learning === "object" ? config.learning.store : void 0);
186
+ if (skillsDb && skillsDb.db === db) return;
187
+ unfollowSkills?.();
188
+ skillsDb = { db };
189
+ unfollowSkills = followOtherTabs(
190
+ core,
191
+ db,
192
+ (change) => change.kind === "skills",
193
+ () => {
194
+ const current = agent;
195
+ if (disposed || !current || typeof current.reloadLearnedSkills !== "function") return;
196
+ void current.reloadLearnedSkills().then((skills) => dispatch({ type: "notebook", lessons: lessonsOf(skills) })).catch(() => {
197
+ });
198
+ }
199
+ );
200
+ };
201
+ const run = async (goal, runOptions) => {
202
+ const current = agent;
203
+ if (disposed || !current || abort) return void 0;
204
+ const controller = new AbortController();
205
+ abort = controller;
206
+ const runControl = createRunControl();
207
+ control = runControl;
208
+ let left = [];
209
+ let stopped = false;
210
+ let began = false;
211
+ const shownGoal = runOptions?.label ?? goal;
212
+ dispatch({ type: "status", status: "running" });
213
+ const shownAtAll = Boolean(shownGoal.trim() || runOptions?.attachments?.length);
214
+ if (shownAtAll) {
215
+ dispatch({
216
+ type: "submitted",
217
+ goal: shownGoal,
218
+ ...runOptions?.attachments?.length ? { attachments: runOptions.attachments } : {}
219
+ });
220
+ }
221
+ try {
222
+ const result = await current.run(goal, {
223
+ signal: controller.signal,
224
+ control: runControl,
225
+ sessionId,
226
+ ...runOptions?.images?.length ? { images: runOptions.images } : {},
227
+ ...runOptions?.files?.length ? { files: runOptions.files } : {},
228
+ onEvent: (event) => {
229
+ const start = event.type === "run.start";
230
+ if (start) {
231
+ started = true;
232
+ began = true;
233
+ }
234
+ const shown = start && runOptions?.label !== void 0 ? { ...event, goal: runOptions.label } : event;
235
+ dispatch({
236
+ type: "event",
237
+ event: shown,
238
+ maxEvents: options.maxEvents,
239
+ at: Date.now(),
240
+ // What the user sent waits with the run: shown as queued meanwhile.
241
+ ...event.type === "session.busy" && shownAtAll ? {
242
+ goal: shownGoal.trim() || (runOptions?.attachments ?? []).map((a) => a.name).join(", ")
243
+ } : {},
244
+ ...start && runOptions?.attachments?.length ? { attachments: runOptions.attachments } : {}
245
+ });
246
+ try {
247
+ options.onEvent?.(event);
248
+ } catch {
249
+ }
250
+ }
251
+ });
252
+ left = result.undelivered ?? [];
253
+ stopped = result.stopped;
254
+ return result;
255
+ } catch (err) {
256
+ left = runControl.pending();
257
+ dispatch({
258
+ type: "event",
259
+ event: { type: "error", phase: "run", error: errMessage(err) },
260
+ maxEvents: options.maxEvents
261
+ });
262
+ return void 0;
263
+ } finally {
264
+ abort = void 0;
265
+ started = false;
266
+ control = void 0;
267
+ settleAll({ approved: false, reason: "the run ended" });
268
+ questionQueue.cancel();
269
+ dispatch({ type: "status", ...buildStatus });
270
+ if (stopped && !began && shownGoal.trim()) left = [shownGoal, ...left];
271
+ if (left.length > 0) {
272
+ if (!controller.signal.aborted && !stopped && options.followUp !== false) {
273
+ dispatch({ type: "undelivered", texts: [] });
274
+ void run(left.join("\n\n"));
275
+ } else {
276
+ dispatch({ type: "undelivered", texts: left });
277
+ }
278
+ } else {
279
+ dispatch({ type: "undelivered", texts: [] });
280
+ }
281
+ }
282
+ };
283
+ const send = (text) => control?.send(text) ?? false;
284
+ let remoteSends = 0;
285
+ const sendRemote = (text) => {
286
+ const current = watch;
287
+ const clean = typeof text === "string" ? text.trim() : "";
288
+ if (!current?.state.running || !clean) return false;
289
+ const id = `to-other-tab-${remoteSends += 1}`;
290
+ dispatch({ type: "remote-send", id, text: clean });
291
+ void current.send(clean).then((ok) => {
292
+ const answer = remoteAnswerOf(ok, watch === current);
293
+ dispatch({ type: "remote-answer", id, text: clean, answer });
294
+ });
295
+ return true;
296
+ };
297
+ const stopRemote = () => {
298
+ if (watch?.state.running) void watch.stop();
299
+ };
300
+ const stopSubagent = (id) => control?.stopSubagent(id) ?? false;
301
+ const sendUndelivered = () => {
302
+ const texts = state.undelivered;
303
+ if (disposed || texts.length === 0) return;
304
+ dispatch({ type: "undelivered", texts: [] });
305
+ void run(texts.join("\n\n"));
306
+ };
307
+ const discardUndelivered = () => dispatch({ type: "undelivered", texts: [] });
308
+ const stop = () => {
309
+ abort?.abort();
310
+ settleAll({ approved: false, reason: "stopped by the user" });
311
+ questionQueue.cancel();
312
+ };
313
+ const answerQuestion = (id, answer) => questionQueue.answer(id, answer);
314
+ const reset = () => {
315
+ if (disposed) return;
316
+ dispatch({ type: "reset" });
317
+ void config.memory?.clear(sessionId).catch(() => {
318
+ });
319
+ };
320
+ const reload = () => {
321
+ if (disposed) return;
322
+ generation += 1;
323
+ build();
324
+ };
325
+ const loadChat = (chat) => {
326
+ if (disposed) return;
327
+ abort?.abort();
328
+ const switched = chat.sessionId !== sessionId;
329
+ sessionId = chat.sessionId;
330
+ if (switched) snapshot = void 0;
331
+ const before = state;
332
+ dispatch({ type: "load", messages: chat.messages ?? [], totalUsage: chat.totalUsage });
333
+ if (switched && state === before) changed();
334
+ syncWatch();
335
+ };
336
+ const forgetSkill = async (name) => {
337
+ const current = agent;
338
+ if (disposed || !current) return false;
339
+ return current.forgetSkill(
340
+ name,
341
+ (event) => dispatch({ type: "event", event, maxEvents: options.maxEvents, at: Date.now() })
342
+ );
343
+ };
344
+ const approve = (id, opts) => {
345
+ if (!approvals.approve(id, { remember: opts?.remember })) {
346
+ appApprovals.approve(id, { remember: opts?.remember });
347
+ }
348
+ };
349
+ const deny = (id, reason) => {
350
+ if (!approvals.deny(id, reason)) appApprovals.deny(id, reason);
351
+ };
352
+ const approveAppToolCall = async ({ frame, ...call }, signal) => {
353
+ const current = agent;
354
+ if (!current) throw new Error("The agent is not ready");
355
+ if (typeof current.approveAppToolCall !== "function") {
356
+ throw new Error("This version of @dudko.dev/agent-web cannot ask about an app’s calls");
357
+ }
358
+ await current.approveAppToolCall(
359
+ call,
360
+ (event) => {
361
+ if (event.type === "tool.approval-resolved") appApprovals.deny(event.id, "no longer asked");
362
+ dispatch({
363
+ type: "event",
364
+ // The frame that asked rides on the request's `app`: drawn with that frame.
365
+ event: frame !== void 0 && event.type === "tool.approval-requested" && event.app ? { ...event, app: { ...event.app, frame } } : event,
366
+ maxEvents: options.maxEvents,
367
+ at: Date.now()
368
+ });
369
+ },
370
+ signal,
371
+ { remembered: options.appToolGrants ?? pageAppToolGrants }
372
+ );
373
+ };
374
+ const sampleForApp = async (request, approveView, signal) => {
375
+ const current = agent;
376
+ if (!current) throw new Error("The agent is not ready");
377
+ if (typeof current.sampleForApp !== "function") {
378
+ throw new Error("This version of @dudko.dev/agent-web cannot answer an app’s sampling");
379
+ }
380
+ return current.sampleForApp(request, {
381
+ approve: approveView,
382
+ ...signal ? { signal } : {},
383
+ sessionId,
384
+ // The only event: its tokens (phase 'app'). The answer is never dispatched.
385
+ onEvent: (event) => dispatch({ type: "event", event, maxEvents: options.maxEvents, at: Date.now() })
386
+ });
387
+ };
388
+ const compact = async () => {
389
+ const current = agent;
390
+ if (disposed || !current) return void 0;
391
+ const result = await current.compact({ sessionId });
392
+ if (result.compacted) {
393
+ dispatch({
394
+ type: "event",
395
+ event: {
396
+ type: "context.compacted",
397
+ scope: "history",
398
+ beforeTokens: result.beforeTokens,
399
+ afterTokens: result.afterTokens
400
+ },
401
+ maxEvents: options.maxEvents,
402
+ at: Date.now()
403
+ });
404
+ }
405
+ return result;
406
+ };
407
+ const update = (next, how = {}) => {
408
+ if (disposed) return;
409
+ const autoStartBefore = options.autoStart;
410
+ config = next;
411
+ if (how.options) options = how.options;
412
+ const nextMode = modeOf(next);
413
+ if (nextMode !== configMode) {
414
+ configMode = nextMode;
415
+ if (nextMode !== mode) setApprovalMode(nextMode);
416
+ }
417
+ if (how.rebuild !== false || !Object.is(options.autoStart, autoStartBefore)) build();
418
+ syncWatch();
419
+ syncSkills();
420
+ };
421
+ const dispose = () => {
422
+ if (disposed) return;
423
+ abort?.abort();
424
+ settleAll({ approved: false, reason: "the view was closed" });
425
+ appApprovals.settleAll({ approved: false, reason: "the view was closed" });
426
+ questionQueue.dispose();
427
+ disposed = true;
428
+ cancelBuild?.();
429
+ cancelBuild = void 0;
430
+ agent = void 0;
431
+ closeWatch();
432
+ unfollowSkills?.();
433
+ unfollowSkills = void 0;
434
+ listeners.clear();
435
+ };
436
+ build();
437
+ syncWatch();
438
+ syncSkills();
439
+ return {
440
+ getSnapshot: () => snapshot ??= snapshotOf(state, { sessionId, pendingQuestions, approvalMode }),
441
+ subscribe: (listener) => {
442
+ if (disposed) return () => {
443
+ };
444
+ listeners.add(listener);
445
+ return () => {
446
+ listeners.delete(listener);
447
+ };
448
+ },
449
+ update,
450
+ dispose,
451
+ get agent() {
452
+ return agent;
453
+ },
454
+ get disposed() {
455
+ return disposed;
456
+ },
457
+ run,
458
+ stop,
459
+ stopRemote,
460
+ send,
461
+ sendRemote,
462
+ stopSubagent,
463
+ forgetSkill,
464
+ sendUndelivered,
465
+ discardUndelivered,
466
+ reset,
467
+ reload,
468
+ approve,
469
+ deny,
470
+ answerQuestion,
471
+ setApprovalMode,
472
+ approveAppToolCall,
473
+ sampleForApp,
474
+ compact,
475
+ loadChat
476
+ };
477
+ };
478
+ export {
479
+ createAgentController,
480
+ createInitialAgentSnapshot
481
+ };
@@ -273,6 +273,12 @@ declare const catalog: {
273
273
  tooLarge: string;
274
274
  tooManyPixels: string;
275
275
  noAttachmentsWhileBusy: string;
276
+ sendWhenReady: string;
277
+ sendWhenReadyButton: string;
278
+ cancelSendWhenReady: string;
279
+ notSentFailed: string;
280
+ notSentUnavailable: string;
281
+ notSentDisabled: string;
276
282
  mediaImage: string;
277
283
  mediaReferences: string;
278
284
  mediaReferencesUnused: string;
@@ -273,6 +273,12 @@ declare const catalog: {
273
273
  tooLarge: string;
274
274
  tooManyPixels: string;
275
275
  noAttachmentsWhileBusy: string;
276
+ sendWhenReady: string;
277
+ sendWhenReadyButton: string;
278
+ cancelSendWhenReady: string;
279
+ notSentFailed: string;
280
+ notSentUnavailable: string;
281
+ notSentDisabled: string;
276
282
  mediaImage: string;
277
283
  mediaReferences: string;
278
284
  mediaReferencesUnused: string;