dsh-deeptutor 0.1.11 → 0.2.1
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/.agents/skills/deeptutor/README.md +17 -36
- package/.agents/skills/deeptutor/SKILL.md +14 -14
- package/.agents/skills/html-doc/scripts/md-to-html.js +16 -7
- package/README.md +29 -22
- package/README.zh.md +13 -11
- package/lib/cli-exec.js +13 -2
- package/lib/config.d.ts +6 -6
- package/lib/config.js +16 -17
- package/lib/index.d.ts +8 -9
- package/lib/index.js +5 -111
- package/lib/turn.d.ts +2 -4
- package/lib/turn.js +4 -95
- package/lib/win-cmd.d.ts +27 -0
- package/lib/win-cmd.js +108 -0
- package/package.json +2 -2
- package/scripts/md-to-html.js +16 -7
- package/src/cli-exec.ts +18 -7
- package/src/config.ts +16 -17
- package/src/index.ts +13 -136
- package/src/turn.ts +5 -94
- package/src/win-cmd.ts +129 -0
- package/lib/http-api.d.ts +0 -10
- package/lib/http-api.js +0 -94
- package/scripts/sync-skills.mjs +0 -47
- package/src/http-api.ts +0 -98
package/lib/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
2
2
|
import { CAPABILITIES, configGuide, configHint, SSH_HOST, truncate } from "./config.js";
|
|
3
3
|
import { cliNoteLocal, cliNoteRemote, runCli } from "./cli-exec.js";
|
|
4
|
-
import { apiJson, ensureApi, killTunnel } from "./http-api.js";
|
|
5
4
|
import { renderHtml } from "./html-render.js";
|
|
6
|
-
import { cliRunTurn, fmtTurnResult
|
|
5
|
+
import { cliRunTurn, fmtTurnResult } from "./turn.js";
|
|
7
6
|
import { HTML_DOC_SCRIPT } from "./html-render.js";
|
|
8
7
|
export const name = "dsh-deeptutor";
|
|
9
8
|
export const inject = ["tools"];
|
|
@@ -16,10 +15,6 @@ function json(o) {
|
|
|
16
15
|
}
|
|
17
16
|
export function apply(ctx) {
|
|
18
17
|
console.log(`[dsh-deeptutor] plugin loaded (html script: ${HTML_DOC_SCRIPT})`);
|
|
19
|
-
// Kill the SSH tunnel process when the plugin unloads.
|
|
20
|
-
ctx.effect(() => {
|
|
21
|
-
return () => killTunnel();
|
|
22
|
-
});
|
|
23
18
|
// ---------- Tool 1: run a DeepTutor learning capability ----------
|
|
24
19
|
ctx.tools.register(defineTool({
|
|
25
20
|
name: "deeptutor_run",
|
|
@@ -94,27 +89,6 @@ export function apply(ctx) {
|
|
|
94
89
|
async execute(args, exec) {
|
|
95
90
|
const timeoutMs = (args.timeout ?? 600) * 1000;
|
|
96
91
|
const maxChars = args.max_chars ?? 30000;
|
|
97
|
-
try {
|
|
98
|
-
if (await ensureApi()) {
|
|
99
|
-
const r = await wsRunTurn(args, timeoutMs, exec.signal);
|
|
100
|
-
const htmlNote = await renderHtml(args, r.answer);
|
|
101
|
-
return json({
|
|
102
|
-
mode: "http",
|
|
103
|
-
sessionId: r.sessionId,
|
|
104
|
-
toolCalls: r.toolCalls,
|
|
105
|
-
errors: r.errors,
|
|
106
|
-
text: configHint() + fmtTurnResult(args.capability, r, maxChars) + htmlNote,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
if (exec.signal.aborted) {
|
|
112
|
-
// user cancelled mid-flight: don't start a fresh CLI run behind their back
|
|
113
|
-
throw new Error("[deeptutor_run cancelled]");
|
|
114
|
-
}
|
|
115
|
-
console.log(`[deeptutor] HTTP/WS failed, falling back to CLI: ${err?.message}`);
|
|
116
|
-
}
|
|
117
|
-
// fallback: local/SSH CLI
|
|
118
92
|
try {
|
|
119
93
|
const r = await cliRunTurn(args, timeoutMs, exec.signal);
|
|
120
94
|
const htmlNote = await renderHtml(args, r.answer);
|
|
@@ -132,7 +106,7 @@ export function apply(ctx) {
|
|
|
132
106
|
throw new Error("[deeptutor_run cancelled]");
|
|
133
107
|
}
|
|
134
108
|
throw new Error(configGuide() +
|
|
135
|
-
`[deeptutor_run failed
|
|
109
|
+
`[deeptutor_run failed] ${err?.message ?? String(err)}`);
|
|
136
110
|
}
|
|
137
111
|
},
|
|
138
112
|
}));
|
|
@@ -185,41 +159,7 @@ export function apply(ctx) {
|
|
|
185
159
|
},
|
|
186
160
|
async execute(args, exec) {
|
|
187
161
|
const maxChars = args.max_chars ?? 30000;
|
|
188
|
-
//
|
|
189
|
-
// list/info prefer HTTP, fall back to CLI
|
|
190
|
-
if (args.action !== "search") {
|
|
191
|
-
try {
|
|
192
|
-
if (await ensureApi()) {
|
|
193
|
-
if (args.action === "list") {
|
|
194
|
-
const arr = await apiJson("/api/v1/knowledge/list", undefined, {
|
|
195
|
-
signal: exec.signal,
|
|
196
|
-
});
|
|
197
|
-
const body = Array.isArray(arr)
|
|
198
|
-
? arr
|
|
199
|
-
.map((kb) => `- ${kb.name} (status=${kb.status ?? "?"}, ${kb.statistics ? JSON.stringify(kb.statistics) : "no stats"}${kb.is_default ? ", default" : ""})`)
|
|
200
|
-
.join("\n")
|
|
201
|
-
: JSON.stringify(arr);
|
|
202
|
-
return json({
|
|
203
|
-
action: "list",
|
|
204
|
-
mode: "http",
|
|
205
|
-
text: configHint() +
|
|
206
|
-
`Knowledge bases (${Array.isArray(arr) ? arr.length : "?"}):\n${body}`,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
const kb = await apiJson(`/api/v1/knowledge/${encodeURIComponent(args.kb ?? "")}`, undefined, { signal: exec.signal });
|
|
210
|
-
return json({
|
|
211
|
-
action: "info",
|
|
212
|
-
mode: "http",
|
|
213
|
-
kb: args.kb,
|
|
214
|
-
text: truncate(JSON.stringify(kb, null, 2), maxChars),
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
catch (err) {
|
|
219
|
-
console.log(`[deeptutor] kb ${args.action} HTTP failed, falling back to CLI: ${err?.message}`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
// CLI fallback (local or SSH). NOTE: `kb search` has no --top-k flag
|
|
162
|
+
// Local or SSH CLI. NOTE: `kb search` has no --top-k flag
|
|
223
163
|
// (verified on deeptutor CLI 1.5.x); top_k is applied locally below.
|
|
224
164
|
let argv;
|
|
225
165
|
if (args.action === "list") {
|
|
@@ -337,53 +277,7 @@ export function apply(ctx) {
|
|
|
337
277
|
async execute(args, exec) {
|
|
338
278
|
const recordType = (args.type ?? "chat");
|
|
339
279
|
const nbName = args.notebook.trim();
|
|
340
|
-
//
|
|
341
|
-
try {
|
|
342
|
-
if (await ensureApi()) {
|
|
343
|
-
const list = await apiJson("/api/v1/notebook/list", undefined, {
|
|
344
|
-
signal: exec.signal,
|
|
345
|
-
});
|
|
346
|
-
const notebooks = list?.notebooks ?? [];
|
|
347
|
-
let nb = notebooks.find((n) => n.name?.trim() === nbName);
|
|
348
|
-
if (!nb) {
|
|
349
|
-
const created = await apiJson("/api/v1/notebook/create", {
|
|
350
|
-
method: "POST",
|
|
351
|
-
headers: { "Content-Type": "application/json" },
|
|
352
|
-
body: JSON.stringify({
|
|
353
|
-
name: nbName,
|
|
354
|
-
description: `Archived by dsh agent (${new Date().toISOString().slice(0, 10)})`,
|
|
355
|
-
}),
|
|
356
|
-
}, { signal: exec.signal });
|
|
357
|
-
nb = created?.notebook ?? created;
|
|
358
|
-
}
|
|
359
|
-
const nbId = nb?.id ?? (typeof nb === "string" ? nb : undefined);
|
|
360
|
-
if (!nbId)
|
|
361
|
-
throw new Error("cannot resolve notebook id");
|
|
362
|
-
const added = await apiJson("/api/v1/notebook/add_record", {
|
|
363
|
-
method: "POST",
|
|
364
|
-
headers: { "Content-Type": "application/json" },
|
|
365
|
-
body: JSON.stringify({
|
|
366
|
-
notebook_ids: [nbId],
|
|
367
|
-
record_type: recordType,
|
|
368
|
-
title: args.title,
|
|
369
|
-
user_query: args.title,
|
|
370
|
-
output: args.content,
|
|
371
|
-
}),
|
|
372
|
-
}, { signal: exec.signal });
|
|
373
|
-
return json({
|
|
374
|
-
notebook: nbName,
|
|
375
|
-
nbId: String(nbId),
|
|
376
|
-
title: args.title,
|
|
377
|
-
mode: "http",
|
|
378
|
-
text: configHint() +
|
|
379
|
-
`[note archived (HTTP)] notebook=${nbName} (${nbId}), title=${args.title}, type=${recordType}\n${truncate(JSON.stringify(added ?? {}), 800)}`,
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
catch (err) {
|
|
384
|
-
console.log(`[deeptutor] note HTTP failed, falling back to CLI: ${err?.message}`);
|
|
385
|
-
}
|
|
386
|
-
// CLI fallback: remote deployment uses the remote PY_NOTE script; local deployment runs locally
|
|
280
|
+
// Remote deployment runs the PY_NOTE script on the server; local deployment runs locally
|
|
387
281
|
const { stdout, stderr, code } = SSH_HOST
|
|
388
282
|
? await cliNoteRemote(nbName, args.title, recordType, args.content, exec.signal)
|
|
389
283
|
: await cliNoteLocal(nbName, args.title, recordType, args.content, exec.signal);
|
|
@@ -396,7 +290,7 @@ export function apply(ctx) {
|
|
|
396
290
|
title: args.title,
|
|
397
291
|
mode: "cli",
|
|
398
292
|
text: configHint() +
|
|
399
|
-
`[note archived
|
|
293
|
+
`[note archived] notebook=${nbName}, title=${args.title}, type=${recordType}\n${truncate((stdout ?? "").trim(), 2000)}`,
|
|
400
294
|
});
|
|
401
295
|
},
|
|
402
296
|
}));
|
package/lib/turn.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface TurnResult {
|
|
|
6
6
|
sessionId?: string;
|
|
7
7
|
title?: string;
|
|
8
8
|
}
|
|
9
|
-
/** Fields
|
|
9
|
+
/** Fields cliRunTurn reads off the deeptutor_run tool params. */
|
|
10
10
|
export interface RunParams {
|
|
11
11
|
prompt: string;
|
|
12
12
|
capability: string;
|
|
@@ -16,10 +16,8 @@ export interface RunParams {
|
|
|
16
16
|
language?: string;
|
|
17
17
|
config?: Record<string, unknown>;
|
|
18
18
|
}
|
|
19
|
-
/** Fold one
|
|
19
|
+
/** Fold one CLI JSONL event into the running turn state. Exported for unit tests. */
|
|
20
20
|
export declare function collectEvent(state: TurnResult, e: any): void;
|
|
21
|
-
/** Execute one turn over WebSocket (streaming). */
|
|
22
|
-
export declare function wsRunTurn(params: RunParams, timeoutMs: number, signal?: AbortSignal): Promise<TurnResult>;
|
|
23
21
|
/**
|
|
24
22
|
* Build the `deeptutor run` argv for a capability turn (pure, unit-tested).
|
|
25
23
|
* Scalar config values go as repeatable --config k=v; object/array values are
|
package/lib/turn.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* DeepTutor bridge: one learning "turn" — running a capability
|
|
3
|
-
* or
|
|
2
|
+
* DeepTutor bridge: one learning "turn" — running a capability through the deeptutor CLI
|
|
3
|
+
* (local or over SSH), JSONL event parsing and result formatting.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { truncate } from "./config.js";
|
|
6
6
|
import { runCli } from "./cli-exec.js";
|
|
7
|
-
/** Fold one
|
|
7
|
+
/** Fold one CLI JSONL event into the running turn state. Exported for unit tests. */
|
|
8
8
|
export function collectEvent(state, e) {
|
|
9
9
|
if (e.type === "content" && typeof e.content === "string")
|
|
10
10
|
state.answer += e.content;
|
|
@@ -19,97 +19,6 @@ export function collectEvent(state, e) {
|
|
|
19
19
|
if (!state.sessionId && typeof e.session_id === "string")
|
|
20
20
|
state.sessionId = e.session_id;
|
|
21
21
|
}
|
|
22
|
-
/** Execute one turn over WebSocket (streaming). */
|
|
23
|
-
export function wsRunTurn(params, timeoutMs, signal) {
|
|
24
|
-
return new Promise((resolve, reject) => {
|
|
25
|
-
if (signal?.aborted)
|
|
26
|
-
return reject(new Error("Aborted"));
|
|
27
|
-
const WSImpl = globalThis.WebSocket;
|
|
28
|
-
if (!WSImpl)
|
|
29
|
-
return reject(new Error("WebSocket is not available in this environment"));
|
|
30
|
-
const ws = new WSImpl(`${API_BASE.replace(/^http/, "ws")}/api/v1/ws`);
|
|
31
|
-
let settled = false;
|
|
32
|
-
const state = { answer: "", errors: [], toolCalls: [] };
|
|
33
|
-
let timer;
|
|
34
|
-
const finish = (fn) => {
|
|
35
|
-
if (settled)
|
|
36
|
-
return;
|
|
37
|
-
settled = true;
|
|
38
|
-
if (timer)
|
|
39
|
-
clearTimeout(timer);
|
|
40
|
-
signal?.removeEventListener("abort", onAbort);
|
|
41
|
-
fn();
|
|
42
|
-
};
|
|
43
|
-
timer = setTimeout(() => {
|
|
44
|
-
finish(() => {
|
|
45
|
-
try {
|
|
46
|
-
ws.close();
|
|
47
|
-
}
|
|
48
|
-
catch { }
|
|
49
|
-
reject(new Error(`WebSocket timeout (${Math.round(timeoutMs / 1000)}s)`));
|
|
50
|
-
});
|
|
51
|
-
}, timeoutMs);
|
|
52
|
-
const onAbort = () => {
|
|
53
|
-
finish(() => {
|
|
54
|
-
try {
|
|
55
|
-
ws.close();
|
|
56
|
-
}
|
|
57
|
-
catch { }
|
|
58
|
-
reject(new Error("Aborted"));
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
62
|
-
ws.onopen = () => {
|
|
63
|
-
ws.send(JSON.stringify({
|
|
64
|
-
type: "start_turn",
|
|
65
|
-
content: params.prompt,
|
|
66
|
-
capability: params.capability,
|
|
67
|
-
session_id: params.session_id ?? null,
|
|
68
|
-
tools: params.tools ?? [],
|
|
69
|
-
knowledge_bases: params.kbs ?? [],
|
|
70
|
-
language: params.language ?? "en",
|
|
71
|
-
config: params.config ?? {},
|
|
72
|
-
notebook_references: [],
|
|
73
|
-
history_references: [],
|
|
74
|
-
attachments: [],
|
|
75
|
-
skills: [],
|
|
76
|
-
}));
|
|
77
|
-
};
|
|
78
|
-
ws.onmessage = (ev) => {
|
|
79
|
-
let e;
|
|
80
|
-
try {
|
|
81
|
-
e = JSON.parse(ev.data);
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
collectEvent(state, e);
|
|
87
|
-
if (e.type === "done") {
|
|
88
|
-
finish(() => {
|
|
89
|
-
try {
|
|
90
|
-
ws.close();
|
|
91
|
-
}
|
|
92
|
-
catch { }
|
|
93
|
-
resolve(state);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
ws.onerror = () => {
|
|
98
|
-
finish(() => {
|
|
99
|
-
try {
|
|
100
|
-
ws.close();
|
|
101
|
-
}
|
|
102
|
-
catch { }
|
|
103
|
-
reject(new Error("WebSocket connection error (server not running or tunnel down)"));
|
|
104
|
-
});
|
|
105
|
-
};
|
|
106
|
-
ws.onclose = () => {
|
|
107
|
-
finish(() => {
|
|
108
|
-
reject(new Error("WebSocket closed unexpectedly"));
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
22
|
/**
|
|
114
23
|
* Build the `deeptutor run` argv for a capability turn (pure, unit-tested).
|
|
115
24
|
* Scalar config values go as repeatable --config k=v; object/array values are
|
package/lib/win-cmd.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface LocalCommand {
|
|
2
|
+
file: string;
|
|
3
|
+
args: string[];
|
|
4
|
+
/** Pass the args to CreateProcess unchanged (needed for the cmd.exe command line). */
|
|
5
|
+
windowsVerbatimArguments?: boolean;
|
|
6
|
+
/** Set when the command cannot be run as requested; nothing should be spawned. */
|
|
7
|
+
error?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Quote one argument for a cmd.exe command line (cross-spawn's escapeArgument). */
|
|
10
|
+
export declare function escapeCmdArgument(arg: string, doubleEscapeMetaChars: boolean): string;
|
|
11
|
+
/** Escape the command path itself for a cmd.exe command line. */
|
|
12
|
+
export declare function escapeCmdCommand(command: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Build the `cmd.exe /d /s /c "..."` invocation for a batch file. Arguments are escaped
|
|
15
|
+
* twice because a batch file that forwards `%*` makes cmd.exe parse them a second time.
|
|
16
|
+
*/
|
|
17
|
+
export declare function buildCmdInvocation(batchFile: string, args: string[], comSpec?: string): LocalCommand;
|
|
18
|
+
/**
|
|
19
|
+
* If a .cmd/.bat wrapper does nothing but forward its arguments to an executable, return
|
|
20
|
+
* that executable's path. Accepts `@echo off`, `rem`/`::` comments and `setlocal`/`endlocal`
|
|
21
|
+
* around a single `["]<path>.exe["] %*` line; `%~dp0` is expanded to the wrapper's folder.
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseCmdWrapper(text: string, wrapperPath: string): string | undefined;
|
|
24
|
+
/** Find a bare command name on PATH, trying each PATHEXT extension (Windows search order). */
|
|
25
|
+
export declare function findOnPath(name: string, env?: NodeJS.ProcessEnv, exists?: (p: string) => boolean): string | undefined;
|
|
26
|
+
/** Turn a local CLI path plus argv into something execFile can start on this platform. */
|
|
27
|
+
export declare function resolveLocalCommand(file: string, args: string[], platform?: NodeJS.Platform, env?: NodeJS.ProcessEnv): LocalCommand;
|
package/lib/win-cmd.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeepTutor bridge: launching the local deeptutor CLI on Windows.
|
|
3
|
+
*
|
|
4
|
+
* Node's execFile cannot start a .cmd/.bat file directly on Windows (spawn EINVAL, see
|
|
5
|
+
* issue #8), and it does not search PATHEXT for a bare command name. resolveLocalCommand
|
|
6
|
+
* turns DEEPTUTOR_LOCAL_BIN into something execFile can run:
|
|
7
|
+
* 1. a bare name is looked up on PATH using PATHEXT;
|
|
8
|
+
* 2. a .cmd/.bat that only forwards its arguments to an .exe (`"C:\...\deeptutor.exe" %*`)
|
|
9
|
+
* is replaced by that .exe, so no shell is involved;
|
|
10
|
+
* 3. any other .cmd/.bat runs through `cmd.exe /d /s /c` with every argument quoted and
|
|
11
|
+
* its metacharacters escaped (the same scheme as cross-spawn).
|
|
12
|
+
* cmd.exe ends a command line at a newline, so case 3 cannot pass multi-line arguments;
|
|
13
|
+
* resolveLocalCommand reports that instead of silently truncating the prompt.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
+
import { extname, win32 } from "node:path";
|
|
17
|
+
const META_CHARS = /([()\][%!^"`<>&|;, *?])/g;
|
|
18
|
+
/** Quote one argument for a cmd.exe command line (cross-spawn's escapeArgument). */
|
|
19
|
+
export function escapeCmdArgument(arg, doubleEscapeMetaChars) {
|
|
20
|
+
let out = arg.replace(/(\\*)"/g, '$1$1\\"');
|
|
21
|
+
out = out.replace(/(\\*)$/, "$1$1");
|
|
22
|
+
out = `"${out}"`;
|
|
23
|
+
out = out.replace(META_CHARS, "^$1");
|
|
24
|
+
if (doubleEscapeMetaChars)
|
|
25
|
+
out = out.replace(META_CHARS, "^$1");
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** Escape the command path itself for a cmd.exe command line. */
|
|
29
|
+
export function escapeCmdCommand(command) {
|
|
30
|
+
return command.replace(META_CHARS, "^$1");
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build the `cmd.exe /d /s /c "..."` invocation for a batch file. Arguments are escaped
|
|
34
|
+
* twice because a batch file that forwards `%*` makes cmd.exe parse them a second time.
|
|
35
|
+
*/
|
|
36
|
+
export function buildCmdInvocation(batchFile, args, comSpec = "cmd.exe") {
|
|
37
|
+
const line = [escapeCmdCommand(batchFile), ...args.map((a) => escapeCmdArgument(a, true))].join(" ");
|
|
38
|
+
return { file: comSpec, args: ["/d", "/s", "/c", `"${line}"`], windowsVerbatimArguments: true };
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* If a .cmd/.bat wrapper does nothing but forward its arguments to an executable, return
|
|
42
|
+
* that executable's path. Accepts `@echo off`, `rem`/`::` comments and `setlocal`/`endlocal`
|
|
43
|
+
* around a single `["]<path>.exe["] %*` line; `%~dp0` is expanded to the wrapper's folder.
|
|
44
|
+
*/
|
|
45
|
+
export function parseCmdWrapper(text, wrapperPath) {
|
|
46
|
+
const lines = text
|
|
47
|
+
.split(/\r?\n/)
|
|
48
|
+
.map((l) => l.trim())
|
|
49
|
+
.filter((l) => l !== "" &&
|
|
50
|
+
!/^@?echo\s+off$/i.test(l) &&
|
|
51
|
+
!/^@?(rem(\s|$)|::)/i.test(l) &&
|
|
52
|
+
!/^@?(setlocal|endlocal)(\s|$)/i.test(l));
|
|
53
|
+
if (lines.length !== 1)
|
|
54
|
+
return undefined;
|
|
55
|
+
const m = /^@?(?:"([^"]+\.(?:exe|com))"|([^\s"]+\.(?:exe|com)))\s+%\*$/i.exec(lines[0]);
|
|
56
|
+
if (!m)
|
|
57
|
+
return undefined;
|
|
58
|
+
// Windows path rules regardless of the host, so this also behaves the same in Linux CI.
|
|
59
|
+
const dir = win32.dirname(wrapperPath);
|
|
60
|
+
const target = (m[1] ?? m[2]).replace(/%~dp0/gi, dir + "\\");
|
|
61
|
+
if (/%/.test(target))
|
|
62
|
+
return undefined; // other variables: let cmd.exe expand them
|
|
63
|
+
return win32.resolve(dir, target);
|
|
64
|
+
}
|
|
65
|
+
/** Find a bare command name on PATH, trying each PATHEXT extension (Windows search order). */
|
|
66
|
+
export function findOnPath(name, env = process.env, exists = existsSync) {
|
|
67
|
+
const exts = (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean);
|
|
68
|
+
// Windows path rules regardless of the host, so this also behaves the same in Linux CI.
|
|
69
|
+
const dirs = (env.PATH ?? env.Path ?? "").split(win32.delimiter).filter(Boolean);
|
|
70
|
+
for (const dir of dirs) {
|
|
71
|
+
for (const ext of exts) {
|
|
72
|
+
const candidate = win32.join(dir.replace(/^"|"$/g, ""), name + ext.toLowerCase());
|
|
73
|
+
if (exists(candidate))
|
|
74
|
+
return candidate;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
/** Turn a local CLI path plus argv into something execFile can start on this platform. */
|
|
80
|
+
export function resolveLocalCommand(file, args, platform = process.platform, env = process.env) {
|
|
81
|
+
if (platform !== "win32")
|
|
82
|
+
return { file, args };
|
|
83
|
+
let target = file;
|
|
84
|
+
const isBareName = !/[\\/]/.test(target) && extname(target) === "";
|
|
85
|
+
if (isBareName)
|
|
86
|
+
target = findOnPath(target, env) ?? target;
|
|
87
|
+
if (!/\.(cmd|bat)$/i.test(target))
|
|
88
|
+
return { file: target, args };
|
|
89
|
+
let wrapped;
|
|
90
|
+
try {
|
|
91
|
+
wrapped = parseCmdWrapper(readFileSync(target, "utf8"), target);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
wrapped = undefined;
|
|
95
|
+
}
|
|
96
|
+
if (wrapped && existsSync(wrapped))
|
|
97
|
+
return { file: wrapped, args };
|
|
98
|
+
if (args.some((a) => /[\r\n]/.test(a))) {
|
|
99
|
+
return {
|
|
100
|
+
file: target,
|
|
101
|
+
args,
|
|
102
|
+
error: `${target} is a batch file, and cmd.exe cannot pass arguments that contain line breaks. ` +
|
|
103
|
+
"Point DEEPTUTOR_LOCAL_BIN at deeptutor.exe instead (in a pip/venv install it is " +
|
|
104
|
+
"<venv>\\Scripts\\deeptutor.exe).",
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return buildCmdInvocation(target, args, env.ComSpec ?? env.COMSPEC ?? "cmd.exe");
|
|
108
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-deeptutor",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "Learning assistant extension for DeepSeek Harness (dsh): DeepTutor tutoring for your agent — deep explanations, self-test questions, learning paths, personal knowledge-base search (RAG), and note archiving (
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Learning assistant extension for DeepSeek Harness (dsh): DeepTutor tutoring for your agent — deep explanations, self-test questions, learning paths, personal knowledge-base search (RAG), and note archiving (drives the DeepTutor CLI locally or over SSH).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
7
7
|
"dsh-plugin",
|
package/scripts/md-to-html.js
CHANGED
|
@@ -360,13 +360,22 @@ function main() {
|
|
|
360
360
|
fail(`template.html not found (looked in: ${templateCandidates.join(", ")})`);
|
|
361
361
|
}
|
|
362
362
|
let template = fs.readFileSync(templatePath, "utf8");
|
|
363
|
-
template
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
363
|
+
// One pass over the template with a replacer function: the inserted text is
|
|
364
|
+
// used verbatim (a replacement *string* would expand `$$`, `$&`, `` $` ``,
|
|
365
|
+
// `$'`), and text already inserted (e.g. a literal `{{TOC}}` in the body)
|
|
366
|
+
// is never scanned for placeholders again.
|
|
367
|
+
const values = {
|
|
368
|
+
TITLE: esc(title),
|
|
369
|
+
BODY: html,
|
|
370
|
+
TOC: tocHtml,
|
|
371
|
+
THEME: opts.theme,
|
|
372
|
+
LANG: opts.lang,
|
|
373
|
+
GENERATED: generated,
|
|
374
|
+
};
|
|
375
|
+
template = template.replace(
|
|
376
|
+
/\{\{(TITLE|BODY|TOC|THEME|LANG|GENERATED)\}\}/g,
|
|
377
|
+
(_, key) => values[key],
|
|
378
|
+
);
|
|
370
379
|
|
|
371
380
|
if (opts.out) {
|
|
372
381
|
fs.writeFileSync(opts.out, template, "utf8");
|
package/src/cli-exec.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { rmSync, writeFileSync } from "node:fs";
|
|
|
7
7
|
import { tmpdir } from "node:os";
|
|
8
8
|
import { join } from "node:path";
|
|
9
9
|
import { DEEPTUTOR_BIN, DEEPTUTOR_HOME, LOCAL_BIN, SSH_HOST } from "./config.ts";
|
|
10
|
+
import { resolveLocalCommand } from "./win-cmd.ts";
|
|
10
11
|
|
|
11
12
|
export interface CliResult {
|
|
12
13
|
stdout: string;
|
|
@@ -63,7 +64,12 @@ const NOTE_STEP_TIMEOUT_MS = 30_000;
|
|
|
63
64
|
function execFileResolved(
|
|
64
65
|
file: string,
|
|
65
66
|
args: string[],
|
|
66
|
-
opts: {
|
|
67
|
+
opts: {
|
|
68
|
+
signal?: AbortSignal;
|
|
69
|
+
timeoutMs?: number;
|
|
70
|
+
env?: Record<string, string>;
|
|
71
|
+
windowsVerbatimArguments?: boolean;
|
|
72
|
+
} = {},
|
|
67
73
|
): Promise<CliResult> {
|
|
68
74
|
return new Promise((resolve) => {
|
|
69
75
|
const child = execFile(
|
|
@@ -72,6 +78,7 @@ function execFileResolved(
|
|
|
72
78
|
{
|
|
73
79
|
env: opts.env ? { ...process.env, ...opts.env } : process.env,
|
|
74
80
|
windowsHide: true,
|
|
81
|
+
windowsVerbatimArguments: opts.windowsVerbatimArguments,
|
|
75
82
|
timeout: opts.timeoutMs,
|
|
76
83
|
},
|
|
77
84
|
(err, stdout, stderr) => {
|
|
@@ -145,12 +152,16 @@ export async function runCli(
|
|
|
145
152
|
if (SSH_HOST) {
|
|
146
153
|
return sshExec([DEEPTUTOR_BIN, ...args], opts);
|
|
147
154
|
}
|
|
148
|
-
// Local mode: execute directly (force UTF-8 output to avoid Windows cp1252 console crashes)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
155
|
+
// Local mode: execute directly (force UTF-8 output to avoid Windows cp1252 console crashes).
|
|
156
|
+
// On Windows a .cmd/.bat LOCAL_BIN needs special handling (spawn EINVAL otherwise, issue #8).
|
|
157
|
+
const cmd = resolveLocalCommand(LOCAL_BIN, args);
|
|
158
|
+
if (cmd.error) return { stdout: "", stderr: cmd.error, code: 1, killed: false };
|
|
159
|
+
const res = await execFileResolved(cmd.file, cmd.args, {
|
|
160
|
+
signal: opts.signal,
|
|
161
|
+
timeoutMs: opts.timeoutMs,
|
|
162
|
+
env: { PYTHONUTF8: "1", PYTHONIOENCODING: "utf-8" },
|
|
163
|
+
windowsVerbatimArguments: cmd.windowsVerbatimArguments,
|
|
164
|
+
});
|
|
154
165
|
const out = res.stdout ?? "";
|
|
155
166
|
const [stdout, stderr] = out.split("__DT_STDERR__");
|
|
156
167
|
return {
|
package/src/config.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DeepTutor bridge: shared configuration (env vars) and small helpers.
|
|
3
3
|
*
|
|
4
|
+
* Every call goes through the deeptutor CLI. DeepTutor 1.7 moved its HTTP/WS routes off
|
|
5
|
+
* /api/v1 and put them behind a login, so the plugin no longer talks to `deeptutor serve`.
|
|
6
|
+
*
|
|
4
7
|
* Config (optional env vars, agent-agnostic):
|
|
5
8
|
* DEEPTUTOR_SSH_HOST SSH host alias (remote deployment; unset/empty = local deployment)
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* DEEPTUTOR_REMOTE_HOME remote DEEPTUTOR_HOME workspace (remote)
|
|
9
|
+
* DEEPTUTOR_LOCAL_BIN local deeptutor executable (default: deeptutor on PATH)
|
|
10
|
+
* DEEPTUTOR_REMOTE_BIN remote deeptutor binary path (remote deployment)
|
|
11
|
+
* DEEPTUTOR_REMOTE_HOME remote DEEPTUTOR_HOME workspace (remote deployment)
|
|
10
12
|
*/
|
|
11
13
|
export const SSH_HOST = process.env.DEEPTUTOR_SSH_HOST ?? "";
|
|
12
|
-
export const API_BASE = process.env.DEEPTUTOR_API_BASE ?? "http://127.0.0.1:8001";
|
|
13
|
-
export const TUNNEL_PORT = Number(new URL(API_BASE).port || 8001);
|
|
14
14
|
export const LOCAL_BIN = process.env.DEEPTUTOR_LOCAL_BIN ?? "deeptutor";
|
|
15
15
|
export const DEEPTUTOR_BIN =
|
|
16
16
|
process.env.DEEPTUTOR_REMOTE_BIN ?? "/home/ubuntu/my-deeptutor/.venv/bin/deeptutor";
|
|
@@ -19,7 +19,6 @@ export const DEEPTUTOR_HOME = process.env.DEEPTUTOR_REMOTE_HOME ?? "/home/ubuntu
|
|
|
19
19
|
// Whether the user explicitly configured anything (show a one-time hint when not)
|
|
20
20
|
const IS_CONFIGURED = Boolean(
|
|
21
21
|
process.env.DEEPTUTOR_SSH_HOST ||
|
|
22
|
-
process.env.DEEPTUTOR_API_BASE ||
|
|
23
22
|
process.env.DEEPTUTOR_LOCAL_BIN ||
|
|
24
23
|
process.env.DEEPTUTOR_REMOTE_BIN ||
|
|
25
24
|
process.env.DEEPTUTOR_REMOTE_HOME,
|
|
@@ -30,28 +29,28 @@ export function configHint(): string {
|
|
|
30
29
|
if (IS_CONFIGURED || configHintShown) return "";
|
|
31
30
|
configHintShown = true;
|
|
32
31
|
return (
|
|
33
|
-
"⚠️ No DEEPTUTOR_* env vars detected; running in local-deployment mode (
|
|
34
|
-
' Local deployment: export
|
|
35
|
-
' Remote deployment: export DEEPTUTOR_SSH_HOST="your-ssh-alias" plus
|
|
36
|
-
" (See the SKILL's Configuration section; changing server
|
|
32
|
+
"⚠️ No DEEPTUTOR_* env vars detected; running in local-deployment mode (deeptutor CLI on PATH).\n" +
|
|
33
|
+
' Local deployment: optionally export DEEPTUTOR_LOCAL_BIN="/path/to/deeptutor"\n' +
|
|
34
|
+
' Remote deployment: export DEEPTUTOR_SSH_HOST="your-ssh-alias" plus DEEPTUTOR_REMOTE_BIN / DEEPTUTOR_REMOTE_HOME\n' +
|
|
35
|
+
" (See the SKILL's Configuration section; changing server only requires env vars)\n"
|
|
37
36
|
);
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export function configGuide(): string {
|
|
41
40
|
return (
|
|
42
41
|
"\n--- Configuration troubleshooting ---\n" +
|
|
43
|
-
"Local deployment (deeptutor
|
|
44
|
-
' export DEEPTUTOR_API_BASE="http://127.0.0.1:8001" (serve port)\n' +
|
|
42
|
+
"Local deployment (deeptutor installed on this machine):\n" +
|
|
45
43
|
' export DEEPTUTOR_LOCAL_BIN="deeptutor" (local CLI path, optional)\n' +
|
|
46
44
|
" Note: do NOT set DEEPTUTOR_SSH_HOST (empty = local mode)\n" +
|
|
47
|
-
" Verify
|
|
48
|
-
"Remote deployment (deeptutor on a server):\n" +
|
|
45
|
+
" Verify the CLI works: deeptutor kb list --format json\n" +
|
|
46
|
+
"Remote deployment (deeptutor on a server, driven over SSH):\n" +
|
|
49
47
|
' export DEEPTUTOR_SSH_HOST="your-ssh-alias"\n' +
|
|
50
|
-
' export DEEPTUTOR_API_BASE="http://127.0.0.1:8001" (local tunnel address; port must match the server\'s serve)\n' +
|
|
51
48
|
' export DEEPTUTOR_REMOTE_BIN="remote-deeptutor-binary-path"\n' +
|
|
52
49
|
' export DEEPTUTOR_REMOTE_HOME="remote-DEEPTUTOR_HOME-workspace"\n' +
|
|
53
|
-
" Verify serve is running on the server (ssh to it: curl http://127.0.0.1:8001/api/v1/system/status)\n" +
|
|
54
50
|
" Verify passwordless SSH login works (ssh alias + BatchMode)\n" +
|
|
51
|
+
" Verify the remote CLI works: ssh <alias> <DEEPTUTOR_REMOTE_BIN> kb list --format json\n" +
|
|
52
|
+
"DeepTutor 1.7+: the kb / notebook commands and most capabilities need their modules\n" +
|
|
53
|
+
" installed and enabled (check with: deeptutor module list).\n" +
|
|
55
54
|
'PowerShell users: use $env:VAR="value" instead of export VAR=value.\n' +
|
|
56
55
|
"Restart the agent after configuring.\n"
|
|
57
56
|
);
|