dsh-external-toolbox 0.0.0-stage → 0.2.0

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/icon.png ADDED
Binary file
package/lib/client.js ADDED
@@ -0,0 +1,264 @@
1
+ /**
2
+ * dsh-external-toolbox — browser half.
3
+ *
4
+ * The floating card (and the translation) live in the PowerShell helper, so the
5
+ * DSH window never has to be open. The browser-side job is the "提问" hand-off:
6
+ * when the card reports an ask intent, open a NEW session and quote the captured
7
+ * text into that session's composer.
8
+ *
9
+ * Hand-written module: no build step, no imports beyond the runtime's own
10
+ * allowlist (react), and the loader id must equal the package name.
11
+ */
12
+ window.__ModuleLoader__.load({
13
+ id: "dsh-external-toolbox",
14
+ factory: (require) => {
15
+ var module = { exports: {} };
16
+ var exports = module.exports;
17
+
18
+ const react = require("react");
19
+
20
+ const LATEST_ROUTE = "/toolbox/latest";
21
+ const BEAT_ROUTE = "/toolbox/beat";
22
+ const POLL_MS = 800;
23
+ /** Give the freshly started session a moment to mount its composer. */
24
+ const DRAFT_DELAY_MS = 350;
25
+
26
+ /** Quote every line so the composer renders it as a block quote. */
27
+ function toQuote(text) {
28
+ return String(text)
29
+ .split("\n")
30
+ .map((line) => (line === "" ? ">" : "> " + line))
31
+ .join("\n");
32
+ }
33
+
34
+ /** Fire-and-forget diagnostics: the host logs these lines. */
35
+ function beat(kind, id) {
36
+ try {
37
+ const url = BEAT_ROUTE + "?kind=" + encodeURIComponent(kind) + "&id=" + encodeURIComponent(id || "");
38
+ fetch(url, { cache: "no-store" }).catch(() => {});
39
+ } catch {
40
+ /* diagnostics only */
41
+ }
42
+ }
43
+
44
+ /**
45
+ * One poller per document: the overlay seat can re-mount the component, and a
46
+ * fresh interval on every mount would multiply the fetch rate.
47
+ */
48
+ const bridge = {
49
+ started: false,
50
+ handled: "",
51
+ /** Quote waiting to be written into the composer of the new session. */
52
+ pending: "",
53
+ /** Client navigation service, captured in apply(). */
54
+ navigation: null,
55
+ };
56
+
57
+ function startNewSession() {
58
+ const navigation = bridge.navigation;
59
+ if (navigation !== null && navigation !== undefined && typeof navigation.startSession === "function") {
60
+ navigation.startSession();
61
+ return true;
62
+ }
63
+ // Fallback: the app routes sessions; going back to the root view is the
64
+ // same landing page the sidebar's "new session" opens.
65
+ try {
66
+ if (/^\/session/.test(location.pathname)) {
67
+ history.pushState({}, "", "/");
68
+ window.dispatchEvent(new PopStateEvent("popstate"));
69
+ return true;
70
+ }
71
+ } catch {
72
+ /* history is unavailable; fall through */
73
+ }
74
+ return false;
75
+ }
76
+
77
+ function writeDraft(actions, text) {
78
+ if (actions === null || actions === undefined) return false;
79
+ if (typeof actions.setDraft !== "function") return false;
80
+ actions.setDraft(text + "\n\n");
81
+ return true;
82
+ }
83
+
84
+ /**
85
+ * Invisible session-scoped seat: the composer overlay is the only place that
86
+ * receives `inputActions`, which is what writing a draft requires.
87
+ */
88
+ function ToolboxBridge({ inputActions, sessionId }) {
89
+ const actionsRef = react.useRef(inputActions);
90
+ actionsRef.current = inputActions;
91
+
92
+ // A quote that was waiting for "some session" is written into whichever
93
+ // session this seat is now mounted for — after an ask the component
94
+ // re-mounts for the freshly started session.
95
+ react.useEffect(() => {
96
+ if (bridge.pending === "") return undefined;
97
+ const timer = setTimeout(() => {
98
+ const text = bridge.pending;
99
+ bridge.pending = "";
100
+ writeDraft(actionsRef.current, text);
101
+ }, DRAFT_DELAY_MS);
102
+ return () => clearTimeout(timer);
103
+ }, [sessionId]);
104
+
105
+ react.useEffect(() => {
106
+ let stopped = false;
107
+ let inFlight = false;
108
+
109
+ const tick = async () => {
110
+ if (stopped || inFlight) return;
111
+ inFlight = true;
112
+ try {
113
+ const response = await fetch(LATEST_ROUTE, {
114
+ headers: { accept: "application/json" },
115
+ cache: "no-store",
116
+ });
117
+ if (!response.ok) return;
118
+ const data = await response.json();
119
+ if (stopped || data === null || typeof data !== "object") return;
120
+ if (data.intent !== "ask" || typeof data.id !== "string" || data.id === "") return;
121
+ if (data.id === bridge.handled) return;
122
+ bridge.handled = data.id;
123
+ const text = typeof data.text === "string" ? data.text.trim() : "";
124
+ if (text === "") return;
125
+ const quote = toQuote(text);
126
+ beat("ask", data.id);
127
+ // Prefer a brand-new session; fall back to the current composer when
128
+ // the navigation service is unavailable.
129
+ bridge.pending = quote;
130
+ if (startNewSession()) return;
131
+ bridge.pending = "";
132
+ writeDraft(actionsRef.current, quote);
133
+ } catch {
134
+ /* the host may be restarting; the next tick retries */
135
+ } finally {
136
+ inFlight = false;
137
+ }
138
+ };
139
+
140
+ if (bridge.started) {
141
+ return undefined;
142
+ }
143
+ bridge.started = true;
144
+ beat("mount", "");
145
+ const timer = setInterval(tick, POLL_MS);
146
+ return () => {
147
+ stopped = true;
148
+ bridge.started = false;
149
+ clearInterval(timer);
150
+ };
151
+ }, []);
152
+
153
+ return null;
154
+ }
155
+
156
+ // Only the slot service is declared: an unavailable name in this list keeps
157
+ // the whole browser module from activating. Navigation is looked up
158
+ // opportunistically in apply() instead.
159
+ const inject = ["slots"];
160
+
161
+ // Candidate service names for "open a new session"; the probe in apply()
162
+ // reports which ones exist so the real name can be pinned down in one pass.
163
+ var CANDIDATES = [
164
+ "uiWorkspace",
165
+ "navigation",
166
+ "navigator",
167
+ "router",
168
+ "shell",
169
+ "layout",
170
+ "workspace",
171
+ "sessions",
172
+ "sessionActions",
173
+ "sessionNavigation",
174
+ "uiNavigation",
175
+ "app",
176
+ ];
177
+
178
+ var METHODS = ["startSession", "createSession", "newSession", "openSession", "start"];
179
+
180
+ function probeServices(ctx) {
181
+ var parts = [];
182
+ try {
183
+ parts.push("path=" + location.pathname + location.search);
184
+ } catch (error) {
185
+ parts.push("path=?");
186
+ }
187
+ for (var i = 0; i < CANDIDATES.length; i += 1) {
188
+ var name = CANDIDATES[i];
189
+ var service;
190
+ try {
191
+ service = ctx.get(name);
192
+ } catch (error) {
193
+ parts.push(name + "=threw");
194
+ continue;
195
+ }
196
+ if (service === undefined || service === null) {
197
+ parts.push(name + "=-");
198
+ continue;
199
+ }
200
+ var found = [];
201
+ for (var j = 0; j < METHODS.length; j += 1) {
202
+ if (typeof service[METHODS[j]] === "function") {
203
+ found.push(METHODS[j]);
204
+ }
205
+ }
206
+ parts.push(name + "=" + (found.length === 0 ? "obj" : found.join("+")));
207
+ // @deepseek-ai/dsh-client-ui-workspace registers itself as `uiWorkspace`
208
+ // and owns startSession(); older/other builds may expose `navigation`.
209
+ if (
210
+ (name === "uiWorkspace" || name === "navigation") &&
211
+ typeof service.startSession === "function" &&
212
+ bridge.navigation === null
213
+ ) {
214
+ bridge.navigation = service;
215
+ }
216
+ }
217
+ // Best effort: a Context may expose its registry; listing it would settle
218
+ // the service name question in one restart.
219
+ try {
220
+ var root = ctx.root || ctx;
221
+ var registry = root.registry || root.registries;
222
+ if (registry && typeof registry === "object") {
223
+ var keys = Object.keys(registry);
224
+ parts.push("reg=" + keys.slice(0, 40).join(","));
225
+ } else {
226
+ parts.push("reg=none");
227
+ }
228
+ } catch (error) {
229
+ parts.push("reg=threw");
230
+ }
231
+ return parts.join(" ");
232
+ }
233
+
234
+ function apply(ctx) {
235
+ try {
236
+ var probe = "none";
237
+ try {
238
+ probe = probeServices(ctx);
239
+ } catch (error) {
240
+ probe = "probe threw:" + String(error && error.message);
241
+ }
242
+ beat("apply", probe);
243
+ ctx.slots.inject("conversation.input.overlay", () =>
244
+ ctx.slots.register(
245
+ {
246
+ name: "conversation.input.overlay",
247
+ id: "external-toolbox-bridge",
248
+ order: 5,
249
+ },
250
+ ToolboxBridge,
251
+ ),
252
+ );
253
+ } catch (error) {
254
+ console.error("dsh-external-toolbox: activation failed", error);
255
+ throw error;
256
+ }
257
+ }
258
+
259
+ exports.apply = apply;
260
+ exports.inject = inject;
261
+
262
+ return module.exports;
263
+ },
264
+ });