dsh-agent-enhancer 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +28 -21
- package/README.md +28 -21
- package/THIRD-PARTY-NOTICES.md +11 -0
- package/lib/client/context/right-tab.d.ts.map +1 -1
- package/lib/client/dsh/overlay.d.ts.map +1 -1
- package/lib/client/dsh/settings-about.d.ts +9 -0
- package/lib/client/dsh/settings-about.d.ts.map +1 -0
- package/lib/client/dsh/settings-catalog.d.ts +29 -0
- package/lib/client/dsh/settings-catalog.d.ts.map +1 -0
- package/lib/client/dsh/settings-features.d.ts +12 -0
- package/lib/client/dsh/settings-features.d.ts.map +1 -0
- package/lib/client/dsh/settings-model-pill.d.ts +19 -0
- package/lib/client/dsh/settings-model-pill.d.ts.map +1 -0
- package/lib/client/dsh/settings-models.d.ts +18 -0
- package/lib/client/dsh/settings-models.d.ts.map +1 -0
- package/lib/client/dsh/settings-update.d.ts +9 -0
- package/lib/client/dsh/settings-update.d.ts.map +1 -0
- package/lib/client/dsh/settings.d.ts.map +1 -1
- package/lib/client/extra-folders/guide-icon.d.ts +4 -0
- package/lib/client/extra-folders/guide-icon.d.ts.map +1 -0
- package/lib/client/extra-folders/kind.d.ts +6 -0
- package/lib/client/extra-folders/kind.d.ts.map +1 -0
- package/lib/client/extra-folders/remote.d.ts +17 -0
- package/lib/client/extra-folders/remote.d.ts.map +1 -0
- package/lib/client/extra-folders/tab.d.ts +16 -0
- package/lib/client/extra-folders/tab.d.ts.map +1 -0
- package/lib/client/file-edit/action.d.ts +12 -0
- package/lib/client/file-edit/action.d.ts.map +1 -0
- package/lib/client/file-edit/address.d.ts +8 -0
- package/lib/client/file-edit/address.d.ts.map +1 -0
- package/lib/client/file-edit/editor.d.ts +16 -0
- package/lib/client/file-edit/editor.d.ts.map +1 -0
- package/lib/client/file-edit/kind.d.ts +7 -0
- package/lib/client/file-edit/kind.d.ts.map +1 -0
- package/lib/client/file-edit/language.d.ts +9 -0
- package/lib/client/file-edit/language.d.ts.map +1 -0
- package/lib/client/file-edit/monaco-loader.d.ts +76 -0
- package/lib/client/file-edit/monaco-loader.d.ts.map +1 -0
- package/lib/client/file-edit/open-tab.d.ts +32 -0
- package/lib/client/file-edit/open-tab.d.ts.map +1 -0
- package/lib/client/file-edit/remote.d.ts +8 -0
- package/lib/client/file-edit/remote.d.ts.map +1 -0
- package/lib/client/file-edit/slot-types.d.ts +17 -0
- package/lib/client/file-edit/slot-types.d.ts.map +1 -0
- package/lib/client/file-edit/tab.d.ts +16 -0
- package/lib/client/file-edit/tab.d.ts.map +1 -0
- package/lib/client/index.d.ts +1 -0
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/locales.d.ts +147 -1
- package/lib/client/locales.d.ts.map +1 -1
- package/lib/client/quote/dom-host.d.ts +10 -0
- package/lib/client/quote/dom-host.d.ts.map +1 -0
- package/lib/client/quote/dom-source.d.ts +4 -0
- package/lib/client/quote/dom-source.d.ts.map +1 -0
- package/lib/client/quote/external-hosts.d.ts +7 -0
- package/lib/client/quote/external-hosts.d.ts.map +1 -0
- package/lib/client/quote/floating-button.d.ts +11 -0
- package/lib/client/quote/floating-button.d.ts.map +1 -0
- package/lib/client/quote/insert.d.ts +7 -0
- package/lib/client/quote/insert.d.ts.map +1 -0
- package/lib/client/quote/interaction.d.ts +6 -0
- package/lib/client/quote/interaction.d.ts.map +1 -0
- package/lib/client/quote/portal-layer.d.ts +9 -0
- package/lib/client/quote/portal-layer.d.ts.map +1 -0
- package/lib/client/quote/position.d.ts +12 -0
- package/lib/client/quote/position.d.ts.map +1 -0
- package/lib/client/quote/session.d.ts +6 -0
- package/lib/client/quote/session.d.ts.map +1 -0
- package/lib/client/quote/surface.d.ts +10 -0
- package/lib/client/quote/surface.d.ts.map +1 -0
- package/lib/client/quote/terminal-host.d.ts +10 -0
- package/lib/client/quote/terminal-host.d.ts.map +1 -0
- package/lib/client/quote/terminal-probe.d.ts +12 -0
- package/lib/client/quote/terminal-probe.d.ts.map +1 -0
- package/lib/client/remote.d.ts.map +1 -1
- package/lib/client/session-delete/menu.d.ts +10 -0
- package/lib/client/session-delete/menu.d.ts.map +1 -0
- package/lib/client/session-delete/overlay.d.ts +12 -0
- package/lib/client/session-delete/overlay.d.ts.map +1 -0
- package/lib/client/session-delete/remote.d.ts +6 -0
- package/lib/client/session-delete/remote.d.ts.map +1 -0
- package/lib/client/session-delete/store.d.ts +11 -0
- package/lib/client/session-delete/store.d.ts.map +1 -0
- package/lib/client.js +3797 -850
- package/lib/client.js.map +1 -1
- package/lib/config.d.ts +1 -1
- package/lib/config.d.ts.map +1 -1
- package/lib/errors.d.ts +2 -2
- package/lib/errors.d.ts.map +1 -1
- package/lib/host/auto-route/session-hook.d.ts.map +1 -1
- package/lib/host/context-compact.d.ts +27 -8
- package/lib/host/context-compact.d.ts.map +1 -1
- package/lib/host/extra-folders/io.d.ts +37 -0
- package/lib/host/extra-folders/io.d.ts.map +1 -0
- package/lib/host/extra-folders/paths.d.ts +8 -0
- package/lib/host/extra-folders/paths.d.ts.map +1 -0
- package/lib/host/extra-folders/pick.d.ts +7 -0
- package/lib/host/extra-folders/pick.d.ts.map +1 -0
- package/lib/host/extra-folders/store.d.ts +8 -0
- package/lib/host/extra-folders/store.d.ts.map +1 -0
- package/lib/host/extra-folders/types.d.ts +65 -0
- package/lib/host/extra-folders/types.d.ts.map +1 -0
- package/lib/host/file-edit/io.d.ts +19 -0
- package/lib/host/file-edit/io.d.ts.map +1 -0
- package/lib/host/file-edit/monaco.d.ts +19 -0
- package/lib/host/file-edit/monaco.d.ts.map +1 -0
- package/lib/host/file-edit/types.d.ts +30 -0
- package/lib/host/file-edit/types.d.ts.map +1 -0
- package/lib/host/index.d.ts +31 -7
- package/lib/host/index.d.ts.map +1 -1
- package/lib/host/rpc.d.ts +95 -0
- package/lib/host/rpc.d.ts.map +1 -1
- package/lib/host/session-delete/cleanup.d.ts +26 -0
- package/lib/host/session-delete/cleanup.d.ts.map +1 -0
- package/lib/host/session-delete/delete.d.ts +32 -0
- package/lib/host/session-delete/delete.d.ts.map +1 -0
- package/lib/host/session-delete/plugin-purge.d.ts +15 -0
- package/lib/host/session-delete/plugin-purge.d.ts.map +1 -0
- package/lib/identity.d.ts +1 -1
- package/lib/index.js +1980 -272
- package/lib/index.js.map +4 -4
- package/lib/shared/monaco-route.d.ts +3 -0
- package/lib/shared/monaco-route.d.ts.map +1 -0
- package/package.json +7 -5
- package/vendor/monaco/vs/base/browser/ui/codicons/codicon/codicon.ttf +0 -0
- package/vendor/monaco/vs/base/worker/workerMain.js +31 -0
- package/vendor/monaco/vs/basic-languages/abap/abap.js +10 -0
- package/vendor/monaco/vs/basic-languages/apex/apex.js +10 -0
- package/vendor/monaco/vs/basic-languages/azcli/azcli.js +10 -0
- package/vendor/monaco/vs/basic-languages/bat/bat.js +10 -0
- package/vendor/monaco/vs/basic-languages/bicep/bicep.js +11 -0
- package/vendor/monaco/vs/basic-languages/cameligo/cameligo.js +10 -0
- package/vendor/monaco/vs/basic-languages/clojure/clojure.js +10 -0
- package/vendor/monaco/vs/basic-languages/coffee/coffee.js +10 -0
- package/vendor/monaco/vs/basic-languages/cpp/cpp.js +10 -0
- package/vendor/monaco/vs/basic-languages/csharp/csharp.js +10 -0
- package/vendor/monaco/vs/basic-languages/csp/csp.js +10 -0
- package/vendor/monaco/vs/basic-languages/css/css.js +12 -0
- package/vendor/monaco/vs/basic-languages/cypher/cypher.js +10 -0
- package/vendor/monaco/vs/basic-languages/dart/dart.js +10 -0
- package/vendor/monaco/vs/basic-languages/dockerfile/dockerfile.js +10 -0
- package/vendor/monaco/vs/basic-languages/ecl/ecl.js +10 -0
- package/vendor/monaco/vs/basic-languages/elixir/elixir.js +10 -0
- package/vendor/monaco/vs/basic-languages/flow9/flow9.js +10 -0
- package/vendor/monaco/vs/basic-languages/freemarker2/freemarker2.js +12 -0
- package/vendor/monaco/vs/basic-languages/fsharp/fsharp.js +10 -0
- package/vendor/monaco/vs/basic-languages/go/go.js +10 -0
- package/vendor/monaco/vs/basic-languages/graphql/graphql.js +10 -0
- package/vendor/monaco/vs/basic-languages/handlebars/handlebars.js +10 -0
- package/vendor/monaco/vs/basic-languages/hcl/hcl.js +10 -0
- package/vendor/monaco/vs/basic-languages/html/html.js +10 -0
- package/vendor/monaco/vs/basic-languages/ini/ini.js +10 -0
- package/vendor/monaco/vs/basic-languages/java/java.js +10 -0
- package/vendor/monaco/vs/basic-languages/javascript/javascript.js +10 -0
- package/vendor/monaco/vs/basic-languages/julia/julia.js +10 -0
- package/vendor/monaco/vs/basic-languages/kotlin/kotlin.js +10 -0
- package/vendor/monaco/vs/basic-languages/less/less.js +11 -0
- package/vendor/monaco/vs/basic-languages/lexon/lexon.js +10 -0
- package/vendor/monaco/vs/basic-languages/liquid/liquid.js +10 -0
- package/vendor/monaco/vs/basic-languages/lua/lua.js +10 -0
- package/vendor/monaco/vs/basic-languages/m3/m3.js +10 -0
- package/vendor/monaco/vs/basic-languages/markdown/markdown.js +10 -0
- package/vendor/monaco/vs/basic-languages/mdx/mdx.js +10 -0
- package/vendor/monaco/vs/basic-languages/mips/mips.js +10 -0
- package/vendor/monaco/vs/basic-languages/msdax/msdax.js +10 -0
- package/vendor/monaco/vs/basic-languages/mysql/mysql.js +10 -0
- package/vendor/monaco/vs/basic-languages/objective-c/objective-c.js +10 -0
- package/vendor/monaco/vs/basic-languages/pascal/pascal.js +10 -0
- package/vendor/monaco/vs/basic-languages/pascaligo/pascaligo.js +10 -0
- package/vendor/monaco/vs/basic-languages/perl/perl.js +10 -0
- package/vendor/monaco/vs/basic-languages/pgsql/pgsql.js +10 -0
- package/vendor/monaco/vs/basic-languages/php/php.js +10 -0
- package/vendor/monaco/vs/basic-languages/pla/pla.js +10 -0
- package/vendor/monaco/vs/basic-languages/postiats/postiats.js +10 -0
- package/vendor/monaco/vs/basic-languages/powerquery/powerquery.js +10 -0
- package/vendor/monaco/vs/basic-languages/powershell/powershell.js +10 -0
- package/vendor/monaco/vs/basic-languages/protobuf/protobuf.js +11 -0
- package/vendor/monaco/vs/basic-languages/pug/pug.js +10 -0
- package/vendor/monaco/vs/basic-languages/python/python.js +10 -0
- package/vendor/monaco/vs/basic-languages/qsharp/qsharp.js +10 -0
- package/vendor/monaco/vs/basic-languages/r/r.js +10 -0
- package/vendor/monaco/vs/basic-languages/razor/razor.js +10 -0
- package/vendor/monaco/vs/basic-languages/redis/redis.js +10 -0
- package/vendor/monaco/vs/basic-languages/redshift/redshift.js +10 -0
- package/vendor/monaco/vs/basic-languages/restructuredtext/restructuredtext.js +10 -0
- package/vendor/monaco/vs/basic-languages/ruby/ruby.js +10 -0
- package/vendor/monaco/vs/basic-languages/rust/rust.js +10 -0
- package/vendor/monaco/vs/basic-languages/sb/sb.js +10 -0
- package/vendor/monaco/vs/basic-languages/scala/scala.js +10 -0
- package/vendor/monaco/vs/basic-languages/scheme/scheme.js +10 -0
- package/vendor/monaco/vs/basic-languages/scss/scss.js +12 -0
- package/vendor/monaco/vs/basic-languages/shell/shell.js +10 -0
- package/vendor/monaco/vs/basic-languages/solidity/solidity.js +10 -0
- package/vendor/monaco/vs/basic-languages/sophia/sophia.js +10 -0
- package/vendor/monaco/vs/basic-languages/sparql/sparql.js +10 -0
- package/vendor/monaco/vs/basic-languages/sql/sql.js +10 -0
- package/vendor/monaco/vs/basic-languages/st/st.js +10 -0
- package/vendor/monaco/vs/basic-languages/swift/swift.js +13 -0
- package/vendor/monaco/vs/basic-languages/systemverilog/systemverilog.js +10 -0
- package/vendor/monaco/vs/basic-languages/tcl/tcl.js +10 -0
- package/vendor/monaco/vs/basic-languages/twig/twig.js +10 -0
- package/vendor/monaco/vs/basic-languages/typescript/typescript.js +10 -0
- package/vendor/monaco/vs/basic-languages/typespec/typespec.js +10 -0
- package/vendor/monaco/vs/basic-languages/vb/vb.js +10 -0
- package/vendor/monaco/vs/basic-languages/wgsl/wgsl.js +307 -0
- package/vendor/monaco/vs/basic-languages/xml/xml.js +10 -0
- package/vendor/monaco/vs/basic-languages/yaml/yaml.js +10 -0
- package/vendor/monaco/vs/editor/editor.main.css +8 -0
- package/vendor/monaco/vs/editor/editor.main.js +798 -0
- package/vendor/monaco/vs/language/css/cssMode.js +13 -0
- package/vendor/monaco/vs/language/css/cssWorker.js +77 -0
- package/vendor/monaco/vs/language/html/htmlMode.js +13 -0
- package/vendor/monaco/vs/language/html/htmlWorker.js +454 -0
- package/vendor/monaco/vs/language/json/jsonMode.js +19 -0
- package/vendor/monaco/vs/language/json/jsonWorker.js +42 -0
- package/vendor/monaco/vs/language/typescript/tsMode.js +20 -0
- package/vendor/monaco/vs/language/typescript/tsWorker.js +51328 -0
- package/vendor/monaco/vs/loader.js +11 -0
- package/vendor/monaco/vs/nls.messages.de.js +21 -0
- package/vendor/monaco/vs/nls.messages.es.js +21 -0
- package/vendor/monaco/vs/nls.messages.fr.js +19 -0
- package/vendor/monaco/vs/nls.messages.it.js +19 -0
- package/vendor/monaco/vs/nls.messages.ja.js +21 -0
- package/vendor/monaco/vs/nls.messages.ko.js +19 -0
- package/vendor/monaco/vs/nls.messages.ru.js +21 -0
- package/vendor/monaco/vs/nls.messages.zh-cn.js +21 -0
- package/vendor/monaco/vs/nls.messages.zh-tw.js +19 -0
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/host/index.ts
|
|
2
|
-
import { existsSync, mkdirSync, readdirSync, renameSync, rmdirSync } from "node:fs";
|
|
3
|
-
import { homedir as
|
|
4
|
-
import { join as
|
|
2
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync2, readdirSync, renameSync as renameSync2, rmdirSync } from "node:fs";
|
|
3
|
+
import { homedir as homedir3 } from "node:os";
|
|
4
|
+
import { join as join14 } from "node:path";
|
|
5
5
|
import z5 from "@deepseek-ai/schemastery";
|
|
6
6
|
import { RemoteError, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
7
7
|
|
|
@@ -19,8 +19,12 @@ var DEFAULT_SETTINGS = {
|
|
|
19
19
|
historyEnabled: true,
|
|
20
20
|
optimizeEnabled: true,
|
|
21
21
|
predictEnabled: true,
|
|
22
|
+
quoteEnabled: true,
|
|
22
23
|
rollbackEnabled: true,
|
|
23
24
|
contextInspectEnabled: true,
|
|
25
|
+
usageEnabled: true,
|
|
26
|
+
sessionDeleteEnabled: true,
|
|
27
|
+
fileEditEnabled: true,
|
|
24
28
|
optimizeProvider: "",
|
|
25
29
|
optimizeModel: "",
|
|
26
30
|
optimizeReasoningEffort: "",
|
|
@@ -52,8 +56,12 @@ function resolveSettings(value) {
|
|
|
52
56
|
historyEnabled: value?.historyEnabled ?? true,
|
|
53
57
|
optimizeEnabled: value?.optimizeEnabled ?? true,
|
|
54
58
|
predictEnabled: value?.predictEnabled ?? true,
|
|
59
|
+
quoteEnabled: value?.quoteEnabled ?? true,
|
|
55
60
|
rollbackEnabled: value?.rollbackEnabled ?? true,
|
|
56
61
|
contextInspectEnabled: value?.contextInspectEnabled ?? true,
|
|
62
|
+
usageEnabled: value?.usageEnabled ?? true,
|
|
63
|
+
sessionDeleteEnabled: value?.sessionDeleteEnabled ?? true,
|
|
64
|
+
fileEditEnabled: value?.fileEditEnabled ?? true,
|
|
57
65
|
optimizeProvider: text(value?.optimizeProvider),
|
|
58
66
|
optimizeModel: text(value?.optimizeModel),
|
|
59
67
|
optimizeReasoningEffort: text(value?.optimizeReasoningEffort),
|
|
@@ -146,11 +154,18 @@ var MESSAGES = {
|
|
|
146
154
|
COMPACT_BUSY: "Compaction is unavailable while the agent is busy",
|
|
147
155
|
COMPACT_FAILED: "Compaction did not finish; the conversation is unchanged or only partially updated",
|
|
148
156
|
COMPACT_SUMMARY: "Compaction could not produce a useful summary; the conversation is unchanged",
|
|
149
|
-
COMPACT_CHANGED: "History changed during compaction; the conversation is unchanged"
|
|
157
|
+
COMPACT_CHANGED: "History changed during compaction; the conversation is unchanged",
|
|
158
|
+
SESSION_BUSY: "The session is running and cannot be deleted yet",
|
|
159
|
+
SESSION_DELETE_FAILED: "Session delete did not finish cleanly",
|
|
160
|
+
FILE_NOT_FOUND: "The file was not found",
|
|
161
|
+
FILE_NOT_TEXT: "The file is not editable UTF-8 text",
|
|
162
|
+
FILE_TOO_LARGE: "The file is too large to edit in Desktop",
|
|
163
|
+
FILE_DENIED: "Permission denied for this file path",
|
|
164
|
+
FILE_EDIT_FAILED: "The file could not be read or saved"
|
|
150
165
|
};
|
|
151
166
|
var AuxiliaryError = class extends Error {
|
|
152
|
-
constructor(code) {
|
|
153
|
-
super(MESSAGES[code]);
|
|
167
|
+
constructor(code, message) {
|
|
168
|
+
super(message ?? MESSAGES[code]);
|
|
154
169
|
this.code = code;
|
|
155
170
|
this.name = "AuxiliaryError";
|
|
156
171
|
}
|
|
@@ -160,7 +175,7 @@ var AuxiliaryError = class extends Error {
|
|
|
160
175
|
// src/identity.ts
|
|
161
176
|
var PACKAGE_NAME = "dsh-agent-enhancer";
|
|
162
177
|
var PLUGIN_ID = "agent-enhancer";
|
|
163
|
-
var PACKAGE_VERSION = "0.3.
|
|
178
|
+
var PACKAGE_VERSION = "0.3.4";
|
|
164
179
|
var LEGACY_PACKAGE_NAME = "dsh-composer-enhancer";
|
|
165
180
|
var ROLLBACK_PREFIX = `${PACKAGE_NAME}:rollback:`;
|
|
166
181
|
var UNDO_PREFIX = `${PACKAGE_NAME}:undo:`;
|
|
@@ -2761,6 +2776,9 @@ var CheckpointStore = class {
|
|
|
2761
2776
|
}
|
|
2762
2777
|
};
|
|
2763
2778
|
|
|
2779
|
+
// src/host/context-compact.ts
|
|
2780
|
+
import { ManualCompactionError } from "@deepseek-ai/dsh-compaction";
|
|
2781
|
+
|
|
2764
2782
|
// src/host/auto-route/classifier.ts
|
|
2765
2783
|
import { BlockAssembler } from "@deepseek-ai/dsh-llm";
|
|
2766
2784
|
|
|
@@ -2848,7 +2866,7 @@ function createClassifier(llm, route) {
|
|
|
2848
2866
|
}
|
|
2849
2867
|
return async (text3, signal) => withUsageSource("autoClassifier", async () => {
|
|
2850
2868
|
const timeout = new AbortController();
|
|
2851
|
-
const
|
|
2869
|
+
const unlink6 = signal === void 0 ? void 0 : linkAbortSignal(signal, timeout);
|
|
2852
2870
|
const run = (async () => {
|
|
2853
2871
|
timeout.signal.throwIfAborted();
|
|
2854
2872
|
const call = await llm.prepareCall(
|
|
@@ -2892,7 +2910,7 @@ function createClassifier(llm, route) {
|
|
|
2892
2910
|
]);
|
|
2893
2911
|
} finally {
|
|
2894
2912
|
if (timer !== void 0) clearTimeout(timer);
|
|
2895
|
-
|
|
2913
|
+
unlink6?.();
|
|
2896
2914
|
}
|
|
2897
2915
|
});
|
|
2898
2916
|
}
|
|
@@ -3143,88 +3161,35 @@ async function resolveAutoRoute(options) {
|
|
|
3143
3161
|
}
|
|
3144
3162
|
|
|
3145
3163
|
// src/host/context-compact.ts
|
|
3146
|
-
function
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
}
|
|
3164
|
-
const fallback = ctx.agentDefaultModel.currentSelection();
|
|
3165
|
-
if (isAutoModel(fallback) || !modelExists(fallback.provider, fallback.model)) return void 0;
|
|
3166
|
-
return tierRouteFromSelection(fallback, override.reasoningEffort);
|
|
3167
|
-
}
|
|
3168
|
-
async function resolveAutoRouteForCompact(ctx, settings, session, feature, signal) {
|
|
3169
|
-
const override = auxiliaryModelOf(settings, "compact");
|
|
3170
|
-
const map = autoRouteMapOf(settings);
|
|
3171
|
-
const classify = settings.autoClassifierEnabled ? createClassifier(ctx.llm, map.classifier) : void 0;
|
|
3172
|
-
const hint = sessionTurnHint(session);
|
|
3173
|
-
const mapped = await resolveAutoRoute({
|
|
3174
|
-
text: hint?.text ?? "",
|
|
3175
|
-
feature,
|
|
3176
|
-
toolHeavy: hint?.toolHeavy,
|
|
3177
|
-
map,
|
|
3178
|
-
classifierEnabled: settings.autoClassifierEnabled,
|
|
3179
|
-
classify,
|
|
3180
|
-
isAvailable: modelExists,
|
|
3181
|
-
fallback: fallbackFromSession(ctx, session, override),
|
|
3182
|
-
signal
|
|
3183
|
-
});
|
|
3184
|
-
if (feature === "session") {
|
|
3185
|
-
setSessionResolvedRouteForSession(session, {
|
|
3186
|
-
provider: mapped.provider,
|
|
3187
|
-
model: mapped.model,
|
|
3188
|
-
reasoningEffort: mapped.reasoningEffort
|
|
3189
|
-
});
|
|
3190
|
-
}
|
|
3191
|
-
return { provider: mapped.provider, model: mapped.model };
|
|
3192
|
-
}
|
|
3193
|
-
async function resolveCompactRouteOverride(ctx, settings, session, signal) {
|
|
3194
|
-
signal.throwIfAborted();
|
|
3195
|
-
const override = auxiliaryModelOf(settings, "compact");
|
|
3196
|
-
if (isAutoModel(override)) {
|
|
3197
|
-
return resolveAutoRouteForCompact(ctx, settings, session, "compact", signal);
|
|
3198
|
-
}
|
|
3199
|
-
if (!isFollowSessionModel(override)) {
|
|
3200
|
-
return { provider: override.provider, model: override.model };
|
|
3201
|
-
}
|
|
3202
|
-
const selection = sessionModelSelection(ctx, session);
|
|
3203
|
-
if (isAutoModel(selection)) {
|
|
3204
|
-
const cached = getSessionResolvedRouteForSession(session);
|
|
3205
|
-
if (cached !== void 0) {
|
|
3206
|
-
return { provider: cached.provider, model: cached.model };
|
|
3164
|
+
function mapManualCompactionCode(code) {
|
|
3165
|
+
switch (code) {
|
|
3166
|
+
case "busy":
|
|
3167
|
+
return "COMPACT_BUSY";
|
|
3168
|
+
case "cancelled":
|
|
3169
|
+
return "COMPACT_FAILED";
|
|
3170
|
+
case "changed":
|
|
3171
|
+
return "COMPACT_CHANGED";
|
|
3172
|
+
case "summary":
|
|
3173
|
+
return "COMPACT_SUMMARY";
|
|
3174
|
+
case "commit":
|
|
3175
|
+
return "COMPACT_FAILED";
|
|
3176
|
+
case "persistence":
|
|
3177
|
+
return "COMPACT_FAILED";
|
|
3178
|
+
default: {
|
|
3179
|
+
const _exhaustive = code;
|
|
3180
|
+
return _exhaustive;
|
|
3207
3181
|
}
|
|
3208
|
-
return resolveAutoRouteForCompact(ctx, settings, session, "session", signal);
|
|
3209
3182
|
}
|
|
3210
|
-
return void 0;
|
|
3211
3183
|
}
|
|
3212
|
-
function
|
|
3213
|
-
if (
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
options.provider = route.provider;
|
|
3218
|
-
options.model = route.model;
|
|
3219
|
-
return () => {
|
|
3220
|
-
;
|
|
3221
|
-
options.provider = previous.provider;
|
|
3222
|
-
options.model = previous.model;
|
|
3223
|
-
};
|
|
3184
|
+
function isManualCompactionError(error) {
|
|
3185
|
+
if (error instanceof ManualCompactionError) return true;
|
|
3186
|
+
if (typeof error !== "object" || error === null) return false;
|
|
3187
|
+
const candidate = error;
|
|
3188
|
+
return candidate.name === "ManualCompactionError" && typeof candidate.code === "string";
|
|
3224
3189
|
}
|
|
3225
3190
|
function mapCompactErrorText(text3) {
|
|
3226
3191
|
const lower = text3.toLowerCase();
|
|
3227
|
-
if (lower.includes("busy") || lower.includes("active") || text3.includes("\u5FD9\u788C")) {
|
|
3192
|
+
if (lower.includes("busy") || lower.includes("active") || text3.includes("\u5FD9\u788C") || text3.includes("\u4E0D\u53EF\u7528\u56E0\u4E3A")) {
|
|
3228
3193
|
return "COMPACT_BUSY";
|
|
3229
3194
|
}
|
|
3230
3195
|
if (lower.includes("summary") || text3.includes("\u6458\u8981")) {
|
|
@@ -3236,6 +3201,9 @@ function mapCompactErrorText(text3) {
|
|
|
3236
3201
|
if (lower.includes("not available") || text3.includes("\u672A\u63D0\u4F9B") || text3.includes("\u4E0D\u53EF\u7528")) {
|
|
3237
3202
|
return "COMPACT_UNAVAILABLE";
|
|
3238
3203
|
}
|
|
3204
|
+
if (lower.includes("cancelled") || text3.includes("\u5DF2\u53D6\u6D88")) {
|
|
3205
|
+
return "COMPACT_FAILED";
|
|
3206
|
+
}
|
|
3239
3207
|
return "COMPACT_FAILED";
|
|
3240
3208
|
}
|
|
3241
3209
|
function parseCompactSuccess(sessionId, text3) {
|
|
@@ -3257,20 +3225,48 @@ function parseCompactSuccess(sessionId, text3) {
|
|
|
3257
3225
|
shadowedTokens: tokenMatch ? Number(tokenMatch[1]) : 0
|
|
3258
3226
|
};
|
|
3259
3227
|
}
|
|
3228
|
+
function resultFromEngine(sessionId, result) {
|
|
3229
|
+
if (result === null) {
|
|
3230
|
+
return {
|
|
3231
|
+
sessionId,
|
|
3232
|
+
status: "noop",
|
|
3233
|
+
shadowedCount: 0,
|
|
3234
|
+
shadowedTokens: 0
|
|
3235
|
+
};
|
|
3236
|
+
}
|
|
3237
|
+
return {
|
|
3238
|
+
sessionId,
|
|
3239
|
+
status: "compacted",
|
|
3240
|
+
shadowedCount: result.shadowedSeqs.length,
|
|
3241
|
+
shadowedTokens: result.shadowedTokenCount
|
|
3242
|
+
};
|
|
3243
|
+
}
|
|
3260
3244
|
async function compactContextNow(options) {
|
|
3261
3245
|
options.signal.throwIfAborted();
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
}
|
|
3265
|
-
if (options.agent === null || typeof options.agent !== "object") {
|
|
3246
|
+
const agent = options.agent;
|
|
3247
|
+
if (agent === null || typeof agent !== "object") {
|
|
3266
3248
|
throw new AuxiliaryError("SESSION_CHANGED");
|
|
3267
3249
|
}
|
|
3268
|
-
const agent = options.agent;
|
|
3269
3250
|
if (agent.status !== void 0 && agent.status !== "idle") {
|
|
3270
3251
|
throw new AuxiliaryError("COMPACT_BUSY");
|
|
3271
3252
|
}
|
|
3272
|
-
const
|
|
3253
|
+
const canCompactNow = options.compaction !== void 0 && typeof options.compaction.compactNow === "function" && typeof agent.runMaintenance === "function";
|
|
3273
3254
|
try {
|
|
3255
|
+
if (canCompactNow && options.compaction !== void 0) {
|
|
3256
|
+
try {
|
|
3257
|
+
const result = await options.compaction.compactNow(agent, options.signal);
|
|
3258
|
+
return resultFromEngine(options.sessionId, result);
|
|
3259
|
+
} catch (error) {
|
|
3260
|
+
if (options.signal.aborted) throw error;
|
|
3261
|
+
if (isManualCompactionError(error)) {
|
|
3262
|
+
throw new AuxiliaryError(mapManualCompactionCode(error.code));
|
|
3263
|
+
}
|
|
3264
|
+
throw error;
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
if (options.commands === void 0 || typeof options.commands.execute !== "function") {
|
|
3268
|
+
throw new AuxiliaryError("COMPACT_UNAVAILABLE");
|
|
3269
|
+
}
|
|
3274
3270
|
const execution = await options.commands.execute(agent, "/compact", [], options.signal);
|
|
3275
3271
|
if (execution === void 0) throw new AuxiliaryError("COMPACT_UNAVAILABLE");
|
|
3276
3272
|
if (execution.result.kind === "error") {
|
|
@@ -3280,147 +3276,1391 @@ async function compactContextNow(options) {
|
|
|
3280
3276
|
} catch (error) {
|
|
3281
3277
|
if (options.signal.aborted) throw error;
|
|
3282
3278
|
if (error instanceof AuxiliaryError) throw error;
|
|
3279
|
+
if (isManualCompactionError(error)) {
|
|
3280
|
+
throw new AuxiliaryError(mapManualCompactionCode(error.code));
|
|
3281
|
+
}
|
|
3283
3282
|
throw new AuxiliaryError("COMPACT_FAILED");
|
|
3284
|
-
} finally {
|
|
3285
|
-
restore?.();
|
|
3286
3283
|
}
|
|
3287
3284
|
}
|
|
3288
3285
|
|
|
3289
|
-
// src/host/
|
|
3290
|
-
var
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3286
|
+
// src/host/session-delete/cleanup.ts
|
|
3287
|
+
var SessionCleanupRegistry = class {
|
|
3288
|
+
cleaners = /* @__PURE__ */ new Map();
|
|
3289
|
+
register(cleaner) {
|
|
3290
|
+
if (cleaner.id.trim() === "") throw new Error("SessionCleaner.id must be non-empty");
|
|
3291
|
+
this.cleaners.set(cleaner.id, cleaner);
|
|
3292
|
+
return () => {
|
|
3293
|
+
const current = this.cleaners.get(cleaner.id);
|
|
3294
|
+
if (current === cleaner) this.cleaners.delete(cleaner.id);
|
|
3295
|
+
};
|
|
3296
|
+
}
|
|
3297
|
+
list() {
|
|
3298
|
+
return [...this.cleaners.values()];
|
|
3299
|
+
}
|
|
3300
|
+
async purgeAll(sessionId, signal) {
|
|
3301
|
+
const reports = [];
|
|
3302
|
+
for (const cleaner of this.cleaners.values()) {
|
|
3303
|
+
signal.throwIfAborted();
|
|
3304
|
+
try {
|
|
3305
|
+
await cleaner.purge(sessionId, signal);
|
|
3306
|
+
reports.push({ id: cleaner.id, label: cleaner.label, ok: true });
|
|
3307
|
+
} catch (error) {
|
|
3308
|
+
reports.push({
|
|
3309
|
+
id: cleaner.id,
|
|
3310
|
+
label: cleaner.label,
|
|
3311
|
+
ok: false,
|
|
3312
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
3313
|
+
});
|
|
3314
|
+
}
|
|
3301
3315
|
}
|
|
3316
|
+
return reports;
|
|
3302
3317
|
}
|
|
3303
|
-
|
|
3318
|
+
};
|
|
3319
|
+
|
|
3320
|
+
// src/host/session-delete/plugin-purge.ts
|
|
3321
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
3322
|
+
import { existsSync } from "node:fs";
|
|
3323
|
+
import { readdir as readdir3, readFile as readFile4, rm, unlink as unlink4 } from "node:fs/promises";
|
|
3324
|
+
import { join as join6 } from "node:path";
|
|
3325
|
+
function digest4(value) {
|
|
3326
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
3304
3327
|
}
|
|
3305
|
-
function
|
|
3306
|
-
return
|
|
3328
|
+
function shortDigest(value, length = 16) {
|
|
3329
|
+
return digest4(value).slice(0, length);
|
|
3307
3330
|
}
|
|
3308
|
-
function
|
|
3309
|
-
if (
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
return
|
|
3331
|
+
async function removeIfPresent4(path) {
|
|
3332
|
+
if (!existsSync(path)) return false;
|
|
3333
|
+
await rm(path, { recursive: true, force: true });
|
|
3334
|
+
return true;
|
|
3335
|
+
}
|
|
3336
|
+
async function unlinkIfPresent(path) {
|
|
3337
|
+
try {
|
|
3338
|
+
await unlink4(path);
|
|
3339
|
+
return true;
|
|
3340
|
+
} catch (error) {
|
|
3341
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
3342
|
+
return false;
|
|
3343
|
+
}
|
|
3344
|
+
throw error;
|
|
3317
3345
|
}
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3346
|
+
}
|
|
3347
|
+
function createCheckpointCleaner(options) {
|
|
3348
|
+
return {
|
|
3349
|
+
id: "checkpoint",
|
|
3350
|
+
label: "Checkpoint / cut / shadow data",
|
|
3351
|
+
async purge(sessionId, signal) {
|
|
3352
|
+
signal.throwIfAborted();
|
|
3353
|
+
await options.store.replace(sessionId, []);
|
|
3354
|
+
signal.throwIfAborted();
|
|
3355
|
+
await options.cutStore.set(sessionId, null);
|
|
3356
|
+
const key = shortDigest(sessionId);
|
|
3357
|
+
await removeIfPresent4(join6(options.storageRoot, "shadow", key));
|
|
3358
|
+
await removeIfPresent4(join6(options.storageRoot, "shadow-git", key));
|
|
3359
|
+
const journalDir = join6(options.storageRoot, "journals");
|
|
3360
|
+
try {
|
|
3361
|
+
const names = await readdir3(journalDir);
|
|
3362
|
+
for (const name2 of names) {
|
|
3363
|
+
signal.throwIfAborted();
|
|
3364
|
+
if (!name2.endsWith(".json")) continue;
|
|
3365
|
+
const path = join6(journalDir, name2);
|
|
3366
|
+
try {
|
|
3367
|
+
const raw = JSON.parse(await readFile4(path, "utf8"));
|
|
3368
|
+
if (raw.sessionId === sessionId) await unlinkIfPresent(path);
|
|
3369
|
+
} catch {
|
|
3370
|
+
}
|
|
3371
|
+
}
|
|
3372
|
+
} catch (error) {
|
|
3373
|
+
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")) {
|
|
3374
|
+
throw error;
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
}
|
|
3378
|
+
};
|
|
3379
|
+
}
|
|
3380
|
+
function createContextExcludeCleaner(pluginDataRoot) {
|
|
3381
|
+
return {
|
|
3382
|
+
id: "context-exclude",
|
|
3383
|
+
label: "Context exclude rules",
|
|
3384
|
+
async purge(sessionId, signal) {
|
|
3385
|
+
signal.throwIfAborted();
|
|
3386
|
+
const dir = join6(pluginDataRoot, "context-exclude", "sessions");
|
|
3387
|
+
const hashed = join6(dir, `${digest4(sessionId)}.json`);
|
|
3388
|
+
const raw = join6(dir, `${sessionId}.json`);
|
|
3389
|
+
await unlinkIfPresent(hashed);
|
|
3390
|
+
await unlinkIfPresent(raw);
|
|
3391
|
+
}
|
|
3392
|
+
};
|
|
3393
|
+
}
|
|
3394
|
+
|
|
3395
|
+
// src/host/session-delete/delete.ts
|
|
3396
|
+
import { createHash as createHash5 } from "node:crypto";
|
|
3397
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
3398
|
+
import { appendFile, mkdir as mkdir4, readdir as readdir4, rm as rm2 } from "node:fs/promises";
|
|
3399
|
+
import { homedir, tmpdir } from "node:os";
|
|
3400
|
+
import { join as join7 } from "node:path";
|
|
3401
|
+
var SESSION_ID_RE = /^session-[0-9a-fA-F-]{36}$/;
|
|
3402
|
+
var SPILL_ROOT_RE = /^dsh-spill-[A-Za-z0-9_-]{6}$/;
|
|
3403
|
+
var SessionDeleteError = class extends Error {
|
|
3404
|
+
constructor(code, message) {
|
|
3405
|
+
super(message);
|
|
3406
|
+
this.code = code;
|
|
3407
|
+
this.name = "SessionDeleteError";
|
|
3326
3408
|
}
|
|
3327
|
-
|
|
3409
|
+
code;
|
|
3410
|
+
};
|
|
3411
|
+
function dshHome() {
|
|
3412
|
+
return process.env.DSH_HOME ?? join7(homedir(), ".dsh");
|
|
3328
3413
|
}
|
|
3329
|
-
function
|
|
3330
|
-
|
|
3331
|
-
if (firstLine !== "" && firstLine.length <= 48) return firstLine;
|
|
3332
|
-
if (total <= 1) return "System";
|
|
3333
|
-
return `System \xB7 ${index + 1}`;
|
|
3414
|
+
function sessionsRoot() {
|
|
3415
|
+
return join7(dshHome(), "sessions");
|
|
3334
3416
|
}
|
|
3335
|
-
function
|
|
3336
|
-
|
|
3337
|
-
if (compact.length <= limit) return compact;
|
|
3338
|
-
return `${compact.slice(0, Math.max(0, limit - 1))}\u2026`;
|
|
3417
|
+
function projcacheSessionsDir() {
|
|
3418
|
+
return join7(dshHome(), "storages", "session_projcache", "sessions");
|
|
3339
3419
|
}
|
|
3340
|
-
function
|
|
3341
|
-
|
|
3342
|
-
if (trimmed.length <= limit) return trimmed;
|
|
3343
|
-
return `${trimmed.slice(0, Math.max(0, limit - 1))}\u2026`;
|
|
3420
|
+
function spillBase() {
|
|
3421
|
+
return process.env.DSH_SESSION_DELETE_TMP ?? tmpdir();
|
|
3344
3422
|
}
|
|
3345
|
-
function
|
|
3346
|
-
|
|
3347
|
-
return typeof op === "object" && op !== null && op.op === "replace";
|
|
3423
|
+
function spillSessionDirName(sessionId) {
|
|
3424
|
+
return `session-${createHash5("sha256").update(sessionId).digest("hex").slice(0, 12)}`;
|
|
3348
3425
|
}
|
|
3349
|
-
function
|
|
3350
|
-
|
|
3351
|
-
if (typeof op !== "object" || op === null || op.op !== "replace") return 0;
|
|
3352
|
-
return Math.max(0, Number(op.endSeq) - Number(op.startSeq) + 1);
|
|
3426
|
+
function uuidOf(sessionId) {
|
|
3427
|
+
return sessionId.slice("session-".length);
|
|
3353
3428
|
}
|
|
3354
|
-
function
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
if (event.type === "tool/result" && shadowed >= 2) return "prune";
|
|
3361
|
-
return "replace";
|
|
3429
|
+
function serviceOf(ctx, key) {
|
|
3430
|
+
try {
|
|
3431
|
+
return ctx.get(key);
|
|
3432
|
+
} catch {
|
|
3433
|
+
return void 0;
|
|
3434
|
+
}
|
|
3362
3435
|
}
|
|
3363
|
-
function
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3436
|
+
async function noteFailure(ctx, message) {
|
|
3437
|
+
try {
|
|
3438
|
+
ctx.logger.warn(`agent-enhancer deleteSession: ${message}`);
|
|
3439
|
+
} catch {
|
|
3440
|
+
}
|
|
3441
|
+
try {
|
|
3442
|
+
const dir = join7(dshHome(), "profiles", "desktop", "plugin-data", "dsh-agent-enhancer");
|
|
3443
|
+
await mkdir4(dir, { recursive: true });
|
|
3444
|
+
await appendFile(
|
|
3445
|
+
join7(dir, "session-delete-last-error.log"),
|
|
3446
|
+
`${(/* @__PURE__ */ new Date()).toISOString()} ${message}
|
|
3447
|
+
`,
|
|
3448
|
+
"utf8"
|
|
3449
|
+
);
|
|
3450
|
+
} catch {
|
|
3376
3451
|
}
|
|
3377
|
-
if (marks.length <= MAX_EVENT_MARKS) return marks;
|
|
3378
|
-
return marks.slice(marks.length - MAX_EVENT_MARKS);
|
|
3379
3452
|
}
|
|
3380
|
-
function
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
return
|
|
3453
|
+
function probeBusy(ctx, sessionId) {
|
|
3454
|
+
const agents = serviceOf(ctx, "agents");
|
|
3455
|
+
if (agents === void 0 || typeof agents.get !== "function") {
|
|
3456
|
+
return { busy: false, detail: "agents-unavailable" };
|
|
3384
3457
|
}
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3458
|
+
let agent;
|
|
3459
|
+
try {
|
|
3460
|
+
agent = agents.get(sessionId);
|
|
3461
|
+
} catch {
|
|
3462
|
+
agent = void 0;
|
|
3388
3463
|
}
|
|
3389
|
-
|
|
3464
|
+
if (agent === void 0 || agent === null) return { busy: false, detail: "no-live-agent" };
|
|
3465
|
+
const face = agent;
|
|
3466
|
+
if (face.status === "running") return { busy: true, detail: "running" };
|
|
3467
|
+
const snapshot = face.session?.getSnapshot?.() ?? face.getSnapshot?.();
|
|
3468
|
+
if (snapshot?.running === true) return { busy: true, detail: "running" };
|
|
3469
|
+
if (snapshot?.pendingInteraction !== void 0 && snapshot.pendingInteraction !== null) {
|
|
3470
|
+
return { busy: true, detail: "pending-interaction" };
|
|
3471
|
+
}
|
|
3472
|
+
return { busy: false, detail: "agent-idle" };
|
|
3390
3473
|
}
|
|
3391
|
-
function
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3474
|
+
function isResident(ctx, sessionId) {
|
|
3475
|
+
const sessions = serviceOf(ctx, "sessions");
|
|
3476
|
+
if (sessions === void 0 || typeof sessions.get !== "function") return false;
|
|
3477
|
+
try {
|
|
3478
|
+
return sessions.get(sessionId) !== void 0;
|
|
3479
|
+
} catch {
|
|
3480
|
+
return false;
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
async function unloadLiveSession(ctx, sessionId) {
|
|
3484
|
+
const sessions = serviceOf(ctx, "sessions");
|
|
3485
|
+
if (sessions === void 0 || typeof sessions.get !== "function") {
|
|
3486
|
+
return { unloaded: false, detail: "sessions-unavailable" };
|
|
3487
|
+
}
|
|
3488
|
+
let session;
|
|
3489
|
+
try {
|
|
3490
|
+
session = sessions.get(sessionId);
|
|
3491
|
+
} catch (error) {
|
|
3492
|
+
return { unloaded: false, detail: `get-failed: ${error instanceof Error ? error.message : String(error)}` };
|
|
3493
|
+
}
|
|
3494
|
+
if (session === void 0 || session === null) return { unloaded: false, detail: "not-resident" };
|
|
3495
|
+
if (typeof sessions.liveEntryFor !== "function") {
|
|
3496
|
+
return { unloaded: false, detail: "no-liveEntryFor" };
|
|
3497
|
+
}
|
|
3498
|
+
try {
|
|
3499
|
+
const entry = sessions.liveEntryFor(session);
|
|
3500
|
+
if (entry === void 0 || entry === null) return { unloaded: false, detail: "no-entry" };
|
|
3501
|
+
if (typeof entry.detach === "function") entry.detach();
|
|
3502
|
+
else if (typeof sessions.detachEntered === "function") sessions.detachEntered(entry);
|
|
3503
|
+
else return { unloaded: false, detail: "no-detach" };
|
|
3504
|
+
} catch (error) {
|
|
3505
|
+
return { unloaded: false, detail: `unload-failed: ${error instanceof Error ? error.message : String(error)}` };
|
|
3506
|
+
}
|
|
3507
|
+
return isResident(ctx, sessionId) ? { unloaded: false, detail: "still-resident" } : { unloaded: true, detail: "detached" };
|
|
3508
|
+
}
|
|
3509
|
+
function workspaceRegistryOf(ctx) {
|
|
3510
|
+
const registry = serviceOf(ctx, "workspaceRegistry");
|
|
3511
|
+
if (registry === void 0) return void 0;
|
|
3512
|
+
if (typeof registry.list !== "function") return void 0;
|
|
3513
|
+
if (typeof registry.archiveSession !== "function") return void 0;
|
|
3514
|
+
if (typeof registry.unarchiveSession !== "function") return void 0;
|
|
3515
|
+
if (typeof registry.unpinSession !== "function") return void 0;
|
|
3516
|
+
return registry;
|
|
3517
|
+
}
|
|
3518
|
+
async function hideLiveRemnant(ctx, sessionId) {
|
|
3519
|
+
const registry = workspaceRegistryOf(ctx);
|
|
3520
|
+
if (registry !== void 0) {
|
|
3521
|
+
if (registry.archivedSessionIds.includes(sessionId)) return false;
|
|
3522
|
+
try {
|
|
3523
|
+
await registry.archiveSession(sessionId, { stopActivity: true });
|
|
3524
|
+
return true;
|
|
3525
|
+
} catch (error) {
|
|
3526
|
+
await noteFailure(ctx, `hide via archiveSession failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
3527
|
+
return false;
|
|
3401
3528
|
}
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3529
|
+
}
|
|
3530
|
+
const facility = serviceOf(ctx, "storageDomain");
|
|
3531
|
+
const domain = facility?.get?.("workspace");
|
|
3532
|
+
if (domain?.global === void 0) return false;
|
|
3533
|
+
try {
|
|
3534
|
+
const state = { ...domain.global.get() };
|
|
3535
|
+
const archived = Array.isArray(state.archivedSessionIds) ? state.archivedSessionIds : [];
|
|
3536
|
+
if (archived.includes(sessionId)) return false;
|
|
3537
|
+
state.archivedSessionIds = [sessionId, ...archived];
|
|
3538
|
+
await domain.global.set(state);
|
|
3539
|
+
return true;
|
|
3540
|
+
} catch (error) {
|
|
3541
|
+
await noteFailure(ctx, `hide via storageDomain failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
3542
|
+
return false;
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
3545
|
+
async function forgetInRegistry(ctx, sessionId, options) {
|
|
3546
|
+
const removed = { workspaces: [], archived: false, pinned: false };
|
|
3547
|
+
const registry = workspaceRegistryOf(ctx);
|
|
3548
|
+
if (registry !== void 0) {
|
|
3549
|
+
for (const workspace of registry.list()) {
|
|
3550
|
+
if (!workspace.sessionIds.includes(sessionId)) continue;
|
|
3551
|
+
try {
|
|
3552
|
+
await workspace.detachSession(sessionId);
|
|
3553
|
+
removed.workspaces.push(workspace.title || workspace.id);
|
|
3554
|
+
} catch (error) {
|
|
3555
|
+
throw new SessionDeleteError(
|
|
3556
|
+
"REGISTRY",
|
|
3557
|
+
`detach from workspace failed: ${error instanceof Error ? error.message : String(error)}`
|
|
3558
|
+
);
|
|
3559
|
+
}
|
|
3560
|
+
}
|
|
3561
|
+
if (registry.pinnedSessionIds.includes(sessionId)) {
|
|
3562
|
+
try {
|
|
3563
|
+
await registry.unpinSession(sessionId);
|
|
3564
|
+
removed.pinned = true;
|
|
3565
|
+
} catch (error) {
|
|
3566
|
+
throw new SessionDeleteError(
|
|
3567
|
+
"REGISTRY",
|
|
3568
|
+
`unpin failed: ${error instanceof Error ? error.message : String(error)}`
|
|
3569
|
+
);
|
|
3570
|
+
}
|
|
3571
|
+
}
|
|
3572
|
+
if (!options.keepArchiveHide && registry.archivedSessionIds.includes(sessionId)) {
|
|
3573
|
+
try {
|
|
3574
|
+
await registry.unarchiveSession(sessionId);
|
|
3575
|
+
removed.archived = true;
|
|
3576
|
+
} catch (error) {
|
|
3577
|
+
throw new SessionDeleteError(
|
|
3578
|
+
"REGISTRY",
|
|
3579
|
+
`unarchive failed: ${error instanceof Error ? error.message : String(error)}`
|
|
3580
|
+
);
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
return removed;
|
|
3584
|
+
}
|
|
3585
|
+
const facility = serviceOf(ctx, "storageDomain");
|
|
3586
|
+
const domain = facility?.get?.("workspace");
|
|
3587
|
+
if (domain === void 0 || typeof domain.table !== "function" || domain.global === void 0) {
|
|
3588
|
+
throw new SessionDeleteError("REGISTRY", "workspace registry is unavailable");
|
|
3589
|
+
}
|
|
3590
|
+
const globalState = { ...domain.global.get() };
|
|
3591
|
+
const archiveSet = Array.isArray(globalState.archivedSessionIds) ? globalState.archivedSessionIds : [];
|
|
3592
|
+
const pinSet = Array.isArray(globalState.pinnedSessionIds) ? globalState.pinnedSessionIds : [];
|
|
3593
|
+
let globalDirty = false;
|
|
3594
|
+
if (!options.keepArchiveHide && archiveSet.includes(sessionId)) {
|
|
3595
|
+
removed.archived = true;
|
|
3596
|
+
globalState.archivedSessionIds = archiveSet.filter((id) => id !== sessionId);
|
|
3597
|
+
globalDirty = true;
|
|
3598
|
+
}
|
|
3599
|
+
if (pinSet.includes(sessionId)) {
|
|
3600
|
+
removed.pinned = true;
|
|
3601
|
+
globalState.pinnedSessionIds = pinSet.filter((id) => id !== sessionId);
|
|
3602
|
+
globalDirty = true;
|
|
3603
|
+
}
|
|
3604
|
+
if (globalDirty) await domain.global.set(globalState);
|
|
3605
|
+
const table = domain.table("workspaces");
|
|
3606
|
+
for (const [workspaceId, record2] of table.entries()) {
|
|
3607
|
+
const owned = Array.isArray(record2?.sessionIds) && record2.sessionIds.includes(sessionId);
|
|
3608
|
+
if (!owned) continue;
|
|
3609
|
+
await table.update(workspaceId, (current) => {
|
|
3610
|
+
const row = current;
|
|
3406
3611
|
return {
|
|
3407
|
-
|
|
3408
|
-
|
|
3612
|
+
...row,
|
|
3613
|
+
sessionIds: (row.sessionIds ?? []).filter((id) => id !== sessionId),
|
|
3614
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3409
3615
|
};
|
|
3616
|
+
});
|
|
3617
|
+
removed.workspaces.push(record2.title || workspaceId);
|
|
3618
|
+
}
|
|
3619
|
+
return removed;
|
|
3620
|
+
}
|
|
3621
|
+
async function removePathBestEffort(path) {
|
|
3622
|
+
if (!existsSync2(path)) return false;
|
|
3623
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
3624
|
+
try {
|
|
3625
|
+
await rm2(path, { recursive: true, force: true });
|
|
3626
|
+
return true;
|
|
3627
|
+
} catch {
|
|
3628
|
+
if (attempt === 2) return false;
|
|
3629
|
+
await new Promise((resolve6) => setTimeout(resolve6, 120 * (attempt + 1)));
|
|
3410
3630
|
}
|
|
3411
|
-
default:
|
|
3412
|
-
return { kind: "developer", title: event.type };
|
|
3413
3631
|
}
|
|
3632
|
+
return false;
|
|
3414
3633
|
}
|
|
3415
|
-
function
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3634
|
+
async function eraseSessionLog(sessionId) {
|
|
3635
|
+
const removed = [];
|
|
3636
|
+
const root = sessionsRoot();
|
|
3637
|
+
if (!existsSync2(root)) return removed;
|
|
3638
|
+
for (const workspaceDir of await readdir4(root)) {
|
|
3639
|
+
const target = join7(root, workspaceDir, sessionId);
|
|
3640
|
+
if (!existsSync2(target)) continue;
|
|
3641
|
+
const normalizedRoot = root.toLowerCase();
|
|
3642
|
+
if (!target.toLowerCase().startsWith(normalizedRoot)) continue;
|
|
3643
|
+
if (await removePathBestEffort(target)) removed.push(target);
|
|
3644
|
+
}
|
|
3645
|
+
return removed;
|
|
3646
|
+
}
|
|
3647
|
+
async function eraseSpill(sessionId) {
|
|
3648
|
+
const removed = [];
|
|
3649
|
+
let entries;
|
|
3650
|
+
try {
|
|
3651
|
+
entries = await readdir4(spillBase());
|
|
3652
|
+
} catch {
|
|
3653
|
+
return removed;
|
|
3654
|
+
}
|
|
3655
|
+
const name2 = spillSessionDirName(sessionId);
|
|
3656
|
+
for (const entry of entries) {
|
|
3657
|
+
if (!SPILL_ROOT_RE.test(entry)) continue;
|
|
3658
|
+
const target = join7(spillBase(), entry, name2);
|
|
3659
|
+
if (!existsSync2(target)) continue;
|
|
3660
|
+
if (await removePathBestEffort(target)) removed.push(target);
|
|
3661
|
+
}
|
|
3662
|
+
return removed;
|
|
3663
|
+
}
|
|
3664
|
+
async function eraseProjcache(ctx, sessionId) {
|
|
3665
|
+
const removed = [];
|
|
3666
|
+
const uuid = uuidOf(sessionId);
|
|
3667
|
+
try {
|
|
3668
|
+
const facility = serviceOf(ctx, "storageDomain");
|
|
3669
|
+
const cacheDomain = facility?.get?.("session_projcache");
|
|
3670
|
+
if (cacheDomain && typeof cacheDomain.table === "function") {
|
|
3671
|
+
const table = cacheDomain.table("sessions");
|
|
3672
|
+
if (table && typeof table.delete === "function") {
|
|
3673
|
+
for (const key of /* @__PURE__ */ new Set([sessionId, uuid])) {
|
|
3674
|
+
try {
|
|
3675
|
+
if (await table.delete(key)) removed.push(`session_projcache#sessions:${key}`);
|
|
3676
|
+
} catch {
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3681
|
+
} catch {
|
|
3682
|
+
}
|
|
3683
|
+
for (const cacheFile of [
|
|
3684
|
+
join7(projcacheSessionsDir(), `${sessionId}.json`),
|
|
3685
|
+
join7(projcacheSessionsDir(), `${uuid}.json`)
|
|
3686
|
+
]) {
|
|
3687
|
+
if (!existsSync2(cacheFile)) continue;
|
|
3688
|
+
if (await removePathBestEffort(cacheFile)) removed.push(cacheFile);
|
|
3689
|
+
}
|
|
3690
|
+
return removed;
|
|
3691
|
+
}
|
|
3692
|
+
function scheduleResidualPurge(ctx, sessionId) {
|
|
3693
|
+
const timer = setTimeout(() => {
|
|
3694
|
+
void eraseProjcache(ctx, sessionId).catch(() => void 0);
|
|
3695
|
+
void eraseSessionLog(sessionId).catch(() => void 0);
|
|
3696
|
+
}, 8e3);
|
|
3697
|
+
timer.unref?.();
|
|
3698
|
+
}
|
|
3699
|
+
async function deleteSessionNow(options) {
|
|
3700
|
+
options.signal.throwIfAborted();
|
|
3701
|
+
const sessionId = options.request.sessionId;
|
|
3702
|
+
if (!SESSION_ID_RE.test(sessionId)) {
|
|
3703
|
+
throw new SessionDeleteError("INVALID_ID", "session id is not a valid session-<uuid>");
|
|
3704
|
+
}
|
|
3705
|
+
const busy = probeBusy(options.ctx, sessionId);
|
|
3706
|
+
if (busy.busy) {
|
|
3707
|
+
throw new SessionDeleteError("BUSY", `session is busy (${busy.detail})`);
|
|
3708
|
+
}
|
|
3709
|
+
const wasResident = isResident(options.ctx, sessionId);
|
|
3710
|
+
let unloaded = false;
|
|
3711
|
+
let hidden = false;
|
|
3712
|
+
let unloadDetail = "not-resident";
|
|
3713
|
+
if (wasResident) {
|
|
3714
|
+
const unload = await unloadLiveSession(options.ctx, sessionId);
|
|
3715
|
+
unloaded = unload.unloaded;
|
|
3716
|
+
unloadDetail = unload.detail;
|
|
3717
|
+
if (!unloaded) {
|
|
3718
|
+
hidden = await hideLiveRemnant(options.ctx, sessionId);
|
|
3719
|
+
}
|
|
3720
|
+
}
|
|
3721
|
+
options.signal.throwIfAborted();
|
|
3722
|
+
let registry;
|
|
3723
|
+
try {
|
|
3724
|
+
registry = await forgetInRegistry(options.ctx, sessionId, {
|
|
3725
|
+
keepArchiveHide: wasResident && !unloaded
|
|
3726
|
+
});
|
|
3727
|
+
} catch (error) {
|
|
3728
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3729
|
+
await noteFailure(options.ctx, `registry: ${message}`);
|
|
3730
|
+
if (error instanceof SessionDeleteError) throw error;
|
|
3731
|
+
throw new SessionDeleteError("REGISTRY", message);
|
|
3732
|
+
}
|
|
3733
|
+
options.signal.throwIfAborted();
|
|
3734
|
+
const removedPaths = [
|
|
3735
|
+
...await eraseSessionLog(sessionId),
|
|
3736
|
+
...await eraseProjcache(options.ctx, sessionId),
|
|
3737
|
+
...await eraseSpill(sessionId)
|
|
3738
|
+
];
|
|
3739
|
+
if (wasResident && !unloaded) scheduleResidualPurge(options.ctx, sessionId);
|
|
3740
|
+
else if (removedPaths.length === 0) scheduleResidualPurge(options.ctx, sessionId);
|
|
3741
|
+
options.signal.throwIfAborted();
|
|
3742
|
+
const pluginCleaners = await options.cleanup.purgeAll(sessionId, options.signal);
|
|
3743
|
+
const live = isResident(options.ctx, sessionId);
|
|
3744
|
+
return {
|
|
3745
|
+
ok: true,
|
|
3746
|
+
sessionId,
|
|
3747
|
+
unloaded,
|
|
3748
|
+
hidden,
|
|
3749
|
+
live,
|
|
3750
|
+
removedPaths,
|
|
3751
|
+
registry,
|
|
3752
|
+
pluginCleaners,
|
|
3753
|
+
detail: unloaded ? "detached" : hidden ? `hidden-remnant:${unloadDetail}` : unloadDetail
|
|
3754
|
+
};
|
|
3755
|
+
}
|
|
3756
|
+
|
|
3757
|
+
// src/host/extra-folders/io.ts
|
|
3758
|
+
import { promises as fsp } from "node:fs";
|
|
3759
|
+
import { basename as basename4, join as join8, resolve as resolve3, sep as sep4 } from "node:path";
|
|
3760
|
+
|
|
3761
|
+
// src/host/extra-folders/paths.ts
|
|
3762
|
+
import { createHash as createHash6 } from "node:crypto";
|
|
3763
|
+
import { isAbsolute as isAbsolute3, relative as relative2, resolve as resolve2, sep as sep3 } from "node:path";
|
|
3764
|
+
function cwdStorageKey(primaryCwd) {
|
|
3765
|
+
const normalized = resolve2(primaryCwd);
|
|
3766
|
+
return createHash6("sha256").update(normalized).digest("hex").slice(0, 32);
|
|
3767
|
+
}
|
|
3768
|
+
function isInsideRoot(root, absolutePath2) {
|
|
3769
|
+
const base = resolve2(root);
|
|
3770
|
+
const target = resolve2(absolutePath2);
|
|
3771
|
+
const rel = relative2(base, target);
|
|
3772
|
+
return rel === "" || !rel.startsWith(`..${sep3}`) && rel !== ".." && !isAbsolute3(rel);
|
|
3773
|
+
}
|
|
3774
|
+
function assertInsideAllowedRoots(primaryCwd, extraRoots, absolutePath2) {
|
|
3775
|
+
if (isInsideRoot(primaryCwd, absolutePath2)) return;
|
|
3776
|
+
for (const root of extraRoots) {
|
|
3777
|
+
if (isInsideRoot(root, absolutePath2)) return;
|
|
3778
|
+
}
|
|
3779
|
+
throw new Error("Path is outside the session workspace and attached folders");
|
|
3780
|
+
}
|
|
3781
|
+
function normalizeRootPath(path) {
|
|
3782
|
+
const trimmed = path.trim();
|
|
3783
|
+
if (trimmed === "") throw new Error("Path is empty");
|
|
3784
|
+
return resolve2(trimmed);
|
|
3785
|
+
}
|
|
3786
|
+
|
|
3787
|
+
// src/host/extra-folders/pick.ts
|
|
3788
|
+
import { execFile } from "node:child_process";
|
|
3789
|
+
import { createRequire } from "node:module";
|
|
3790
|
+
import { promisify } from "node:util";
|
|
3791
|
+
|
|
3792
|
+
// src/host/extra-folders/types.ts
|
|
3793
|
+
var EXTRA_FOLDERS_VERSION = 1;
|
|
3794
|
+
var EXTRA_FOLDERS_MAX_ROOTS = 16;
|
|
3795
|
+
var ExtraFoldersError = class extends Error {
|
|
3796
|
+
constructor(code, message) {
|
|
3797
|
+
super(message ?? code);
|
|
3798
|
+
this.code = code;
|
|
3799
|
+
this.name = "ExtraFoldersError";
|
|
3800
|
+
}
|
|
3801
|
+
code;
|
|
3802
|
+
};
|
|
3803
|
+
|
|
3804
|
+
// src/host/extra-folders/pick.ts
|
|
3805
|
+
var execFileAsync = promisify(execFile);
|
|
3806
|
+
var nodeRequire = createRequire(import.meta.url);
|
|
3807
|
+
async function pickViaElectron() {
|
|
3808
|
+
try {
|
|
3809
|
+
const electron = nodeRequire("electron");
|
|
3810
|
+
if (electron.dialog === void 0 || typeof electron.dialog.showOpenDialog !== "function") {
|
|
3811
|
+
return void 0;
|
|
3812
|
+
}
|
|
3813
|
+
const result = await electron.dialog.showOpenDialog({
|
|
3814
|
+
properties: ["openDirectory"],
|
|
3815
|
+
title: "Select folder"
|
|
3816
|
+
});
|
|
3817
|
+
if (result.canceled || result.filePaths.length === 0) return null;
|
|
3818
|
+
const path = result.filePaths[0];
|
|
3819
|
+
return typeof path === "string" && path.trim() !== "" ? path : null;
|
|
3820
|
+
} catch {
|
|
3821
|
+
return void 0;
|
|
3822
|
+
}
|
|
3823
|
+
}
|
|
3824
|
+
async function pickViaWindowsForms() {
|
|
3825
|
+
if (process.platform !== "win32") return void 0;
|
|
3826
|
+
const script = [
|
|
3827
|
+
"Add-Type -AssemblyName System.Windows.Forms | Out-Null",
|
|
3828
|
+
"$dialog = New-Object System.Windows.Forms.FolderBrowserDialog",
|
|
3829
|
+
"$dialog.Description = 'Select folder'",
|
|
3830
|
+
"$dialog.ShowNewFolderButton = $true",
|
|
3831
|
+
"$result = $dialog.ShowDialog()",
|
|
3832
|
+
"if ($result -eq [System.Windows.Forms.DialogResult]::OK) {",
|
|
3833
|
+
" [Console]::Out.Write($dialog.SelectedPath)",
|
|
3834
|
+
"}"
|
|
3835
|
+
].join("; ");
|
|
3836
|
+
try {
|
|
3837
|
+
const { stdout } = await execFileAsync(
|
|
3838
|
+
"powershell.exe",
|
|
3839
|
+
["-NoProfile", "-STA", "-Command", script],
|
|
3840
|
+
{ windowsHide: true, encoding: "utf8", timeout: 12e4 }
|
|
3841
|
+
);
|
|
3842
|
+
const path = stdout.trim();
|
|
3843
|
+
return path === "" ? null : path;
|
|
3844
|
+
} catch {
|
|
3845
|
+
return void 0;
|
|
3846
|
+
}
|
|
3847
|
+
}
|
|
3848
|
+
async function pickViaOsascript() {
|
|
3849
|
+
if (process.platform !== "darwin") return void 0;
|
|
3850
|
+
try {
|
|
3851
|
+
const { stdout } = await execFileAsync(
|
|
3852
|
+
"osascript",
|
|
3853
|
+
["-e", 'POSIX path of (choose folder with prompt "Select folder")'],
|
|
3854
|
+
{ encoding: "utf8", timeout: 12e4 }
|
|
3855
|
+
);
|
|
3856
|
+
const path = stdout.trim().replace(/\/$/, "");
|
|
3857
|
+
return path === "" ? null : path;
|
|
3858
|
+
} catch (error) {
|
|
3859
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
3860
|
+
if (/User canceled|(-128)/i.test(message)) return null;
|
|
3861
|
+
return void 0;
|
|
3862
|
+
}
|
|
3863
|
+
}
|
|
3864
|
+
async function pickViaZenity() {
|
|
3865
|
+
if (process.platform === "win32" || process.platform === "darwin") return void 0;
|
|
3866
|
+
try {
|
|
3867
|
+
const { stdout } = await execFileAsync(
|
|
3868
|
+
"zenity",
|
|
3869
|
+
["--file-selection", "--directory", "--title=Select folder"],
|
|
3870
|
+
{ encoding: "utf8", timeout: 12e4 }
|
|
3871
|
+
);
|
|
3872
|
+
const path = stdout.trim();
|
|
3873
|
+
return path === "" ? null : path;
|
|
3874
|
+
} catch (error) {
|
|
3875
|
+
const code = error !== null && typeof error === "object" ? Reflect.get(error, "code") : void 0;
|
|
3876
|
+
if (code === 1) return null;
|
|
3877
|
+
return void 0;
|
|
3878
|
+
}
|
|
3879
|
+
}
|
|
3880
|
+
async function pickDirectoryNow(signal) {
|
|
3881
|
+
signal.throwIfAborted();
|
|
3882
|
+
const attempts = [
|
|
3883
|
+
pickViaElectron,
|
|
3884
|
+
pickViaWindowsForms,
|
|
3885
|
+
pickViaOsascript,
|
|
3886
|
+
pickViaZenity
|
|
3887
|
+
];
|
|
3888
|
+
for (const attempt of attempts) {
|
|
3889
|
+
signal.throwIfAborted();
|
|
3890
|
+
const path = await attempt();
|
|
3891
|
+
if (path !== void 0) return { path };
|
|
3892
|
+
}
|
|
3893
|
+
throw new ExtraFoldersError(
|
|
3894
|
+
"FILE_EDIT_FAILED",
|
|
3895
|
+
"No directory picker is available in this environment"
|
|
3896
|
+
);
|
|
3897
|
+
}
|
|
3898
|
+
|
|
3899
|
+
// src/host/extra-folders/io.ts
|
|
3900
|
+
function sessionCwd(session) {
|
|
3901
|
+
const header = session?.header;
|
|
3902
|
+
if (typeof header?.cwd === "string" && header.cwd.trim() !== "") return resolve3(header.cwd);
|
|
3903
|
+
try {
|
|
3904
|
+
const requested = session?.requestHeader?.();
|
|
3905
|
+
if (typeof requested?.cwd === "string" && requested.cwd.trim() !== "") {
|
|
3906
|
+
return resolve3(requested.cwd);
|
|
3907
|
+
}
|
|
3908
|
+
} catch {
|
|
3909
|
+
}
|
|
3910
|
+
throw new ExtraFoldersError("SESSION_CHANGED", "Session workspace is unavailable");
|
|
3911
|
+
}
|
|
3912
|
+
function agentOf(ctx, sessionId) {
|
|
3913
|
+
const agent = ctx.agents.get(sessionId);
|
|
3914
|
+
if (agent === void 0) {
|
|
3915
|
+
throw new ExtraFoldersError("SESSION_CHANGED", "Session is not available");
|
|
3916
|
+
}
|
|
3917
|
+
return agent;
|
|
3918
|
+
}
|
|
3919
|
+
function titleOf(path, explicit) {
|
|
3920
|
+
if (typeof explicit === "string" && explicit.trim() !== "") return explicit.trim();
|
|
3921
|
+
const name2 = basename4(path);
|
|
3922
|
+
return name2 === "" ? path : name2;
|
|
3923
|
+
}
|
|
3924
|
+
function samePath(a, b) {
|
|
3925
|
+
return resolve3(a).toLowerCase() === resolve3(b).toLowerCase();
|
|
3926
|
+
}
|
|
3927
|
+
async function assertDirectory(absolutePath2) {
|
|
3928
|
+
try {
|
|
3929
|
+
const stat = await fsp.stat(absolutePath2);
|
|
3930
|
+
if (!stat.isDirectory()) {
|
|
3931
|
+
throw new ExtraFoldersError("FILE_DENIED", "Path is not a directory");
|
|
3932
|
+
}
|
|
3933
|
+
} catch (error) {
|
|
3934
|
+
if (error instanceof ExtraFoldersError) throw error;
|
|
3935
|
+
const code = error !== null && typeof error === "object" ? String(Reflect.get(error, "code") ?? "") : "";
|
|
3936
|
+
if (code === "ENOENT") {
|
|
3937
|
+
throw new ExtraFoldersError("FILE_NOT_FOUND", "Directory not found");
|
|
3938
|
+
}
|
|
3939
|
+
throw new ExtraFoldersError(
|
|
3940
|
+
"FILE_EDIT_FAILED",
|
|
3941
|
+
error instanceof Error ? error.message : "Cannot access directory"
|
|
3942
|
+
);
|
|
3943
|
+
}
|
|
3944
|
+
}
|
|
3945
|
+
async function listExtraFoldersNow(options) {
|
|
3946
|
+
options.signal.throwIfAborted();
|
|
3947
|
+
const agent = agentOf(options.ctx, options.request.sessionId);
|
|
3948
|
+
const primaryCwd = sessionCwd(agent.session);
|
|
3949
|
+
const doc = options.store.load(primaryCwd);
|
|
3950
|
+
return {
|
|
3951
|
+
primaryCwd,
|
|
3952
|
+
primaryTitle: titleOf(primaryCwd),
|
|
3953
|
+
roots: doc.roots
|
|
3954
|
+
};
|
|
3955
|
+
}
|
|
3956
|
+
async function attachRoot(options) {
|
|
3957
|
+
const absolute = options.absolute;
|
|
3958
|
+
await assertDirectory(absolute);
|
|
3959
|
+
if (samePath(absolute, options.primaryCwd) || isInsideRoot(options.primaryCwd, absolute)) {
|
|
3960
|
+
throw new ExtraFoldersError(
|
|
3961
|
+
"FILE_DENIED",
|
|
3962
|
+
"Folder is already inside the session workspace root"
|
|
3963
|
+
);
|
|
3964
|
+
}
|
|
3965
|
+
const doc = options.store.load(options.primaryCwd);
|
|
3966
|
+
if (doc.roots.some((root) => samePath(root.path, absolute))) {
|
|
3967
|
+
throw new ExtraFoldersError("FILE_DENIED", "Folder is already attached");
|
|
3968
|
+
}
|
|
3969
|
+
if (doc.roots.length >= EXTRA_FOLDERS_MAX_ROOTS) {
|
|
3970
|
+
throw new ExtraFoldersError(
|
|
3971
|
+
"FILE_DENIED",
|
|
3972
|
+
`At most ${EXTRA_FOLDERS_MAX_ROOTS} extra folders are allowed`
|
|
3973
|
+
);
|
|
3974
|
+
}
|
|
3975
|
+
const next = [
|
|
3976
|
+
...doc.roots,
|
|
3977
|
+
{ path: absolute, title: titleOf(absolute, options.title) }
|
|
3978
|
+
];
|
|
3979
|
+
const saved = options.store.save(options.primaryCwd, next);
|
|
3980
|
+
return {
|
|
3981
|
+
primaryCwd: options.primaryCwd,
|
|
3982
|
+
primaryTitle: titleOf(options.primaryCwd),
|
|
3983
|
+
roots: saved.roots
|
|
3984
|
+
};
|
|
3985
|
+
}
|
|
3986
|
+
async function addExtraFolderNow(options) {
|
|
3987
|
+
options.signal.throwIfAborted();
|
|
3988
|
+
const agent = agentOf(options.ctx, options.request.sessionId);
|
|
3989
|
+
const primaryCwd = sessionCwd(agent.session);
|
|
3990
|
+
let absolute;
|
|
3991
|
+
try {
|
|
3992
|
+
absolute = normalizeRootPath(options.request.path);
|
|
3993
|
+
} catch (error) {
|
|
3994
|
+
throw new ExtraFoldersError(
|
|
3995
|
+
"FILE_DENIED",
|
|
3996
|
+
error instanceof Error ? error.message : "Invalid path"
|
|
3997
|
+
);
|
|
3998
|
+
}
|
|
3999
|
+
return attachRoot({
|
|
4000
|
+
store: options.store,
|
|
4001
|
+
primaryCwd,
|
|
4002
|
+
absolute,
|
|
4003
|
+
title: options.request.title
|
|
4004
|
+
});
|
|
4005
|
+
}
|
|
4006
|
+
async function pickExtraFolderNow(options) {
|
|
4007
|
+
options.signal.throwIfAborted();
|
|
4008
|
+
const agent = agentOf(options.ctx, options.request.sessionId);
|
|
4009
|
+
const primaryCwd = sessionCwd(agent.session);
|
|
4010
|
+
const current = options.store.load(primaryCwd);
|
|
4011
|
+
const picked = await pickDirectoryNow(options.signal);
|
|
4012
|
+
if (picked.path === null) {
|
|
4013
|
+
return {
|
|
4014
|
+
path: null,
|
|
4015
|
+
primaryCwd,
|
|
4016
|
+
primaryTitle: titleOf(primaryCwd),
|
|
4017
|
+
roots: current.roots,
|
|
4018
|
+
added: false
|
|
4019
|
+
};
|
|
4020
|
+
}
|
|
4021
|
+
const absolute = normalizeRootPath(picked.path);
|
|
4022
|
+
const saved = await attachRoot({
|
|
4023
|
+
store: options.store,
|
|
4024
|
+
primaryCwd,
|
|
4025
|
+
absolute
|
|
4026
|
+
});
|
|
4027
|
+
return {
|
|
4028
|
+
path: absolute,
|
|
4029
|
+
primaryCwd: saved.primaryCwd,
|
|
4030
|
+
primaryTitle: saved.primaryTitle,
|
|
4031
|
+
roots: saved.roots,
|
|
4032
|
+
added: true
|
|
4033
|
+
};
|
|
4034
|
+
}
|
|
4035
|
+
async function removeExtraFolderNow(options) {
|
|
4036
|
+
options.signal.throwIfAborted();
|
|
4037
|
+
const agent = agentOf(options.ctx, options.request.sessionId);
|
|
4038
|
+
const primaryCwd = sessionCwd(agent.session);
|
|
4039
|
+
let absolute;
|
|
4040
|
+
try {
|
|
4041
|
+
absolute = normalizeRootPath(options.request.path);
|
|
4042
|
+
} catch (error) {
|
|
4043
|
+
throw new ExtraFoldersError(
|
|
4044
|
+
"FILE_DENIED",
|
|
4045
|
+
error instanceof Error ? error.message : "Invalid path"
|
|
4046
|
+
);
|
|
4047
|
+
}
|
|
4048
|
+
const doc = options.store.load(primaryCwd);
|
|
4049
|
+
const next = doc.roots.filter((root) => !samePath(root.path, absolute));
|
|
4050
|
+
if (next.length === doc.roots.length) {
|
|
4051
|
+
throw new ExtraFoldersError("FILE_NOT_FOUND", "Attached folder not found");
|
|
4052
|
+
}
|
|
4053
|
+
const saved = options.store.save(primaryCwd, next);
|
|
4054
|
+
return {
|
|
4055
|
+
primaryCwd,
|
|
4056
|
+
primaryTitle: titleOf(primaryCwd),
|
|
4057
|
+
roots: saved.roots
|
|
4058
|
+
};
|
|
4059
|
+
}
|
|
4060
|
+
function relativeUnderRoot(rootPath, relativePath) {
|
|
4061
|
+
const raw = (relativePath ?? "").replace(/\\/g, "/").replace(/^\/+/, "").trim();
|
|
4062
|
+
if (raw === "" || raw === ".") return "";
|
|
4063
|
+
const parts = raw.split("/").filter((part) => part !== "" && part !== ".");
|
|
4064
|
+
if (parts.some((part) => part === "..")) {
|
|
4065
|
+
throw new ExtraFoldersError("FILE_DENIED", "Path escapes the folder root");
|
|
4066
|
+
}
|
|
4067
|
+
return parts.join(sep4);
|
|
4068
|
+
}
|
|
4069
|
+
async function listExtraFolderEntriesNow(options) {
|
|
4070
|
+
options.signal.throwIfAborted();
|
|
4071
|
+
const agent = agentOf(options.ctx, options.request.sessionId);
|
|
4072
|
+
const primaryCwd = sessionCwd(agent.session);
|
|
4073
|
+
const doc = options.store.load(primaryCwd);
|
|
4074
|
+
const extraPaths = doc.roots.map((root) => root.path);
|
|
4075
|
+
let rootPath;
|
|
4076
|
+
try {
|
|
4077
|
+
rootPath = normalizeRootPath(options.request.rootPath);
|
|
4078
|
+
} catch (error) {
|
|
4079
|
+
throw new ExtraFoldersError(
|
|
4080
|
+
"FILE_DENIED",
|
|
4081
|
+
error instanceof Error ? error.message : "Invalid root path"
|
|
4082
|
+
);
|
|
4083
|
+
}
|
|
4084
|
+
const allowed = samePath(rootPath, primaryCwd) || extraPaths.some((path) => samePath(path, rootPath));
|
|
4085
|
+
if (!allowed) {
|
|
4086
|
+
throw new ExtraFoldersError("FILE_DENIED", "Root is not the workspace or an attached folder");
|
|
4087
|
+
}
|
|
4088
|
+
const relativePath = relativeUnderRoot(rootPath, options.request.relativePath);
|
|
4089
|
+
const absoluteDir = relativePath === "" ? rootPath : join8(rootPath, relativePath);
|
|
4090
|
+
assertInsideAllowedRoots(primaryCwd, extraPaths, absoluteDir);
|
|
4091
|
+
await assertDirectory(absoluteDir);
|
|
4092
|
+
let names;
|
|
4093
|
+
try {
|
|
4094
|
+
names = await fsp.readdir(absoluteDir);
|
|
4095
|
+
} catch (error) {
|
|
4096
|
+
throw new ExtraFoldersError(
|
|
4097
|
+
"FILE_EDIT_FAILED",
|
|
4098
|
+
error instanceof Error ? error.message : "Cannot list directory"
|
|
4099
|
+
);
|
|
4100
|
+
}
|
|
4101
|
+
const entries = [];
|
|
4102
|
+
for (const name2 of names) {
|
|
4103
|
+
if (name2 === "." || name2 === "..") continue;
|
|
4104
|
+
const absolutePath2 = join8(absoluteDir, name2);
|
|
4105
|
+
try {
|
|
4106
|
+
assertInsideAllowedRoots(primaryCwd, extraPaths, absolutePath2);
|
|
4107
|
+
const stat = await fsp.lstat(absolutePath2);
|
|
4108
|
+
if (stat.isSymbolicLink()) continue;
|
|
4109
|
+
const kind = stat.isDirectory() ? "directory" : stat.isFile() ? "file" : null;
|
|
4110
|
+
if (kind === null) continue;
|
|
4111
|
+
const path = relativePath === "" ? name2 : join8(relativePath, name2);
|
|
4112
|
+
entries.push({ name: name2, path: path.replace(/\\/g, "/"), absolutePath: absolutePath2, kind });
|
|
4113
|
+
} catch {
|
|
4114
|
+
}
|
|
4115
|
+
}
|
|
4116
|
+
entries.sort((a, b) => {
|
|
4117
|
+
if (a.kind !== b.kind) return a.kind === "directory" ? -1 : 1;
|
|
4118
|
+
return a.name.localeCompare(b.name, void 0, { sensitivity: "base" });
|
|
4119
|
+
});
|
|
4120
|
+
return {
|
|
4121
|
+
rootPath,
|
|
4122
|
+
relativePath: relativePath.replace(/\\/g, "/"),
|
|
4123
|
+
entries
|
|
4124
|
+
};
|
|
4125
|
+
}
|
|
4126
|
+
function loadExtraRootPaths(store, primaryCwd) {
|
|
4127
|
+
return store.load(resolve3(primaryCwd)).roots.map((root) => root.path);
|
|
4128
|
+
}
|
|
4129
|
+
|
|
4130
|
+
// src/host/extra-folders/store.ts
|
|
4131
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
4132
|
+
import { dirname, join as join9 } from "node:path";
|
|
4133
|
+
function emptyDoc(primaryCwd) {
|
|
4134
|
+
return {
|
|
4135
|
+
version: EXTRA_FOLDERS_VERSION,
|
|
4136
|
+
primaryCwd,
|
|
4137
|
+
roots: []
|
|
4138
|
+
};
|
|
4139
|
+
}
|
|
4140
|
+
function parseDoc(raw, primaryCwd) {
|
|
4141
|
+
try {
|
|
4142
|
+
const parsed = JSON.parse(raw);
|
|
4143
|
+
if (parsed.version !== EXTRA_FOLDERS_VERSION || !Array.isArray(parsed.roots)) {
|
|
4144
|
+
return emptyDoc(primaryCwd);
|
|
4145
|
+
}
|
|
4146
|
+
const roots = [];
|
|
4147
|
+
for (const entry of parsed.roots) {
|
|
4148
|
+
if (entry === null || typeof entry !== "object" || typeof entry.path !== "string" || entry.path.trim() === "") {
|
|
4149
|
+
continue;
|
|
4150
|
+
}
|
|
4151
|
+
const title = typeof entry.title === "string" && entry.title.trim() !== "" ? entry.title.trim() : entry.path;
|
|
4152
|
+
roots.push({ path: entry.path, title });
|
|
4153
|
+
}
|
|
4154
|
+
return {
|
|
4155
|
+
version: EXTRA_FOLDERS_VERSION,
|
|
4156
|
+
primaryCwd,
|
|
4157
|
+
roots
|
|
4158
|
+
};
|
|
4159
|
+
} catch {
|
|
4160
|
+
return emptyDoc(primaryCwd);
|
|
4161
|
+
}
|
|
4162
|
+
}
|
|
4163
|
+
function createExtraFoldersStore(rootDir) {
|
|
4164
|
+
mkdirSync(rootDir, { recursive: true });
|
|
4165
|
+
const fileOf = (primaryCwd) => join9(rootDir, `${cwdStorageKey(primaryCwd)}.json`);
|
|
4166
|
+
return {
|
|
4167
|
+
rootDir,
|
|
4168
|
+
load(primaryCwd) {
|
|
4169
|
+
const file = fileOf(primaryCwd);
|
|
4170
|
+
try {
|
|
4171
|
+
return parseDoc(readFileSync(file, "utf8"), primaryCwd);
|
|
4172
|
+
} catch (error) {
|
|
4173
|
+
const code = error !== null && typeof error === "object" ? String(Reflect.get(error, "code") ?? "") : "";
|
|
4174
|
+
if (code === "ENOENT") return emptyDoc(primaryCwd);
|
|
4175
|
+
return emptyDoc(primaryCwd);
|
|
4176
|
+
}
|
|
4177
|
+
},
|
|
4178
|
+
save(primaryCwd, roots) {
|
|
4179
|
+
const doc = {
|
|
4180
|
+
version: EXTRA_FOLDERS_VERSION,
|
|
4181
|
+
primaryCwd,
|
|
4182
|
+
roots: [...roots]
|
|
4183
|
+
};
|
|
4184
|
+
const file = fileOf(primaryCwd);
|
|
4185
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
4186
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
4187
|
+
writeFileSync(tmp, `${JSON.stringify(doc, null, 2)}
|
|
4188
|
+
`, "utf8");
|
|
4189
|
+
renameSync(tmp, file);
|
|
4190
|
+
return doc;
|
|
4191
|
+
}
|
|
4192
|
+
};
|
|
4193
|
+
}
|
|
4194
|
+
|
|
4195
|
+
// src/host/file-edit/io.ts
|
|
4196
|
+
import { isAbsolute as isAbsolute4, resolve as resolve4 } from "node:path";
|
|
4197
|
+
|
|
4198
|
+
// src/host/file-edit/types.ts
|
|
4199
|
+
var FILE_EDIT_MAX_BYTES = 2 * 1024 * 1024;
|
|
4200
|
+
var FileEditError = class extends Error {
|
|
4201
|
+
constructor(code, message) {
|
|
4202
|
+
super(message ?? code);
|
|
4203
|
+
this.code = code;
|
|
4204
|
+
this.name = "FileEditError";
|
|
4205
|
+
}
|
|
4206
|
+
code;
|
|
4207
|
+
};
|
|
4208
|
+
|
|
4209
|
+
// src/host/file-edit/io.ts
|
|
4210
|
+
function softGet(holder, name2, method) {
|
|
4211
|
+
if (holder === void 0 || typeof holder.get !== "function") return void 0;
|
|
4212
|
+
try {
|
|
4213
|
+
const value = holder.get(name2);
|
|
4214
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
4215
|
+
if (typeof Reflect.get(value, method) !== "function") return void 0;
|
|
4216
|
+
return value;
|
|
4217
|
+
} catch {
|
|
4218
|
+
return void 0;
|
|
4219
|
+
}
|
|
4220
|
+
}
|
|
4221
|
+
function sessionCwd2(session) {
|
|
4222
|
+
const header = session?.header;
|
|
4223
|
+
if (typeof header?.cwd === "string" && header.cwd.trim() !== "") return header.cwd;
|
|
4224
|
+
try {
|
|
4225
|
+
const requested = session?.requestHeader?.();
|
|
4226
|
+
if (typeof requested?.cwd === "string" && requested.cwd.trim() !== "") return requested.cwd;
|
|
4227
|
+
} catch {
|
|
4228
|
+
}
|
|
4229
|
+
throw new FileEditError("SESSION_CHANGED", "Session workspace is unavailable");
|
|
4230
|
+
}
|
|
4231
|
+
function processPathOf(target, fallback) {
|
|
4232
|
+
const key = typeof target.key === "string" && target.key.trim() !== "" ? target.key : fallback;
|
|
4233
|
+
return key;
|
|
4234
|
+
}
|
|
4235
|
+
function mapFsError(error) {
|
|
4236
|
+
const code = error !== null && typeof error === "object" ? String(Reflect.get(error, "code") ?? "") : "";
|
|
4237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4238
|
+
switch (code) {
|
|
4239
|
+
case "FS_NOT_FOUND":
|
|
4240
|
+
throw new FileEditError("FILE_NOT_FOUND", message || "File not found");
|
|
4241
|
+
case "FS_NOT_TEXT":
|
|
4242
|
+
throw new FileEditError("FILE_NOT_TEXT", message || "File is not UTF-8 text");
|
|
4243
|
+
case "FS_TOO_LARGE":
|
|
4244
|
+
throw new FileEditError("FILE_TOO_LARGE", message || "File is too large to edit here");
|
|
4245
|
+
case "FS_SANDBOX_DENIED":
|
|
4246
|
+
case "FS_PERMISSION_DENIED":
|
|
4247
|
+
throw new FileEditError("FILE_DENIED", message || "Permission denied");
|
|
4248
|
+
default:
|
|
4249
|
+
throw new FileEditError("FILE_EDIT_FAILED", message || "File operation failed");
|
|
4250
|
+
}
|
|
4251
|
+
}
|
|
4252
|
+
function pickFs(agent, ctx) {
|
|
4253
|
+
const fromAgent = softGet(agent?.ctx, "fs", "writeText");
|
|
4254
|
+
if (fromAgent !== void 0) return fromAgent;
|
|
4255
|
+
const fromCtx = softGet(ctx, "fs", "writeText");
|
|
4256
|
+
if (fromCtx !== void 0) return fromCtx;
|
|
4257
|
+
try {
|
|
4258
|
+
const direct = Reflect.get(ctx, "fs");
|
|
4259
|
+
if (direct !== void 0 && typeof direct.writeText === "function") return direct;
|
|
4260
|
+
} catch {
|
|
4261
|
+
}
|
|
4262
|
+
throw new FileEditError("FILE_EDIT_FAILED", "Filesystem service is unavailable");
|
|
4263
|
+
}
|
|
4264
|
+
function pickPolicy(agent, ctx) {
|
|
4265
|
+
const sandbox = softGet(agent?.ctx, "sandboxPolicy", "resolve") ?? softGet(ctx, "sandboxPolicy", "resolve");
|
|
4266
|
+
if (sandbox === void 0) return void 0;
|
|
4267
|
+
try {
|
|
4268
|
+
return sandbox.resolve({ session: agent?.session });
|
|
4269
|
+
} catch {
|
|
4270
|
+
return void 0;
|
|
4271
|
+
}
|
|
4272
|
+
}
|
|
4273
|
+
async function resolveTarget(fs, workspaceRoot, extraRoots, path) {
|
|
4274
|
+
const trimmed = path.trim();
|
|
4275
|
+
if (trimmed === "") throw new FileEditError("FILE_NOT_FOUND", "Path is empty");
|
|
4276
|
+
const target = await fs.resolve(trimmed, { cwd: workspaceRoot });
|
|
4277
|
+
const absolutePath2 = processPathOf(target, isAbsolute4(trimmed) ? trimmed : resolve4(workspaceRoot, trimmed));
|
|
4278
|
+
try {
|
|
4279
|
+
assertInsideAllowedRoots(workspaceRoot, extraRoots, absolutePath2);
|
|
4280
|
+
} catch (error) {
|
|
4281
|
+
throw new FileEditError(
|
|
4282
|
+
"FILE_DENIED",
|
|
4283
|
+
error instanceof Error ? error.message : "Path is outside the session workspace"
|
|
4284
|
+
);
|
|
4285
|
+
}
|
|
4286
|
+
return { target, absolutePath: absolutePath2 };
|
|
4287
|
+
}
|
|
4288
|
+
async function readWorkspaceFileNow(options) {
|
|
4289
|
+
options.signal.throwIfAborted();
|
|
4290
|
+
const agent = options.ctx.agents.get(options.request.sessionId);
|
|
4291
|
+
if (agent === void 0) {
|
|
4292
|
+
throw new FileEditError("SESSION_CHANGED", "Session is not available");
|
|
4293
|
+
}
|
|
4294
|
+
const workspaceRoot = sessionCwd2(agent.session);
|
|
4295
|
+
const extraRoots = options.extraRoots ?? [];
|
|
4296
|
+
const fs = pickFs(agent, options.ctx);
|
|
4297
|
+
try {
|
|
4298
|
+
const { target, absolutePath: absolutePath2 } = await resolveTarget(
|
|
4299
|
+
fs,
|
|
4300
|
+
workspaceRoot,
|
|
4301
|
+
extraRoots,
|
|
4302
|
+
options.request.path
|
|
4303
|
+
);
|
|
4304
|
+
if (typeof fs.stat === "function") {
|
|
4305
|
+
const info = await fs.stat(target, options.signal);
|
|
4306
|
+
if (typeof info.bytes === "number" && info.bytes > FILE_EDIT_MAX_BYTES) {
|
|
4307
|
+
throw new FileEditError(
|
|
4308
|
+
"FILE_TOO_LARGE",
|
|
4309
|
+
`File exceeds the ${FILE_EDIT_MAX_BYTES} byte in-app edit limit`
|
|
4310
|
+
);
|
|
4311
|
+
}
|
|
4312
|
+
}
|
|
4313
|
+
const text3 = await fs.readText(target, options.signal);
|
|
4314
|
+
const bytes = Buffer.byteLength(text3, "utf8");
|
|
4315
|
+
if (bytes > FILE_EDIT_MAX_BYTES) {
|
|
4316
|
+
throw new FileEditError(
|
|
4317
|
+
"FILE_TOO_LARGE",
|
|
4318
|
+
`File exceeds the ${FILE_EDIT_MAX_BYTES} byte in-app edit limit`
|
|
4319
|
+
);
|
|
4320
|
+
}
|
|
4321
|
+
return {
|
|
4322
|
+
path: options.request.path,
|
|
4323
|
+
absolutePath: absolutePath2,
|
|
4324
|
+
text: text3,
|
|
4325
|
+
bytes,
|
|
4326
|
+
truncated: false
|
|
4327
|
+
};
|
|
4328
|
+
} catch (error) {
|
|
4329
|
+
if (error instanceof FileEditError) throw error;
|
|
4330
|
+
if (options.signal.aborted) throw error;
|
|
4331
|
+
mapFsError(error);
|
|
4332
|
+
}
|
|
4333
|
+
}
|
|
4334
|
+
async function writeWorkspaceFileNow(options) {
|
|
4335
|
+
options.signal.throwIfAborted();
|
|
4336
|
+
const agent = options.ctx.agents.get(options.request.sessionId);
|
|
4337
|
+
if (agent === void 0) {
|
|
4338
|
+
throw new FileEditError("SESSION_CHANGED", "Session is not available");
|
|
4339
|
+
}
|
|
4340
|
+
const content = options.request.content;
|
|
4341
|
+
const bytes = Buffer.byteLength(content, "utf8");
|
|
4342
|
+
if (bytes > FILE_EDIT_MAX_BYTES) {
|
|
4343
|
+
throw new FileEditError(
|
|
4344
|
+
"FILE_TOO_LARGE",
|
|
4345
|
+
`Content exceeds the ${FILE_EDIT_MAX_BYTES} byte in-app edit limit`
|
|
4346
|
+
);
|
|
4347
|
+
}
|
|
4348
|
+
const workspaceRoot = sessionCwd2(agent.session);
|
|
4349
|
+
const extraRoots = options.extraRoots ?? [];
|
|
4350
|
+
const fs = pickFs(agent, options.ctx);
|
|
4351
|
+
try {
|
|
4352
|
+
const { target, absolutePath: absolutePath2 } = await resolveTarget(
|
|
4353
|
+
fs,
|
|
4354
|
+
workspaceRoot,
|
|
4355
|
+
extraRoots,
|
|
4356
|
+
options.request.path
|
|
4357
|
+
);
|
|
4358
|
+
const policy = isInsideRoot(workspaceRoot, absolutePath2) ? pickPolicy(agent, options.ctx) : void 0;
|
|
4359
|
+
await fs.writeText(target, content, void 0, options.signal, policy);
|
|
4360
|
+
return {
|
|
4361
|
+
path: options.request.path,
|
|
4362
|
+
absolutePath: absolutePath2,
|
|
4363
|
+
bytes
|
|
4364
|
+
};
|
|
4365
|
+
} catch (error) {
|
|
4366
|
+
if (error instanceof FileEditError) throw error;
|
|
4367
|
+
if (options.signal.aborted) throw error;
|
|
4368
|
+
mapFsError(error);
|
|
4369
|
+
}
|
|
4370
|
+
}
|
|
4371
|
+
|
|
4372
|
+
// src/host/file-edit/monaco.ts
|
|
4373
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
4374
|
+
import { promises as fsp2 } from "node:fs";
|
|
4375
|
+
import { dirname as dirname2, extname, join as join10, resolve as resolve5, sep as sep5 } from "node:path";
|
|
4376
|
+
import { fileURLToPath } from "node:url";
|
|
4377
|
+
|
|
4378
|
+
// src/shared/monaco-route.ts
|
|
4379
|
+
var MONACO_ROUTE = "/api/agent-enhancer/monaco";
|
|
4380
|
+
|
|
4381
|
+
// src/host/file-edit/monaco.ts
|
|
4382
|
+
var FILE_PATTERN = /^(?:vs\/)?(?:[A-Za-z0-9._-]+\/)*[A-Za-z0-9._-]+$/;
|
|
4383
|
+
function packageRootCandidates() {
|
|
4384
|
+
const here = dirname2(fileURLToPath(import.meta.url));
|
|
4385
|
+
return [
|
|
4386
|
+
resolve5(here, "..", "vendor", "monaco", "vs"),
|
|
4387
|
+
resolve5(here, "..", "..", "vendor", "monaco", "vs"),
|
|
4388
|
+
resolve5(here, "..", "..", "..", "vendor", "monaco", "vs")
|
|
4389
|
+
];
|
|
4390
|
+
}
|
|
4391
|
+
function resolveMonacoVsDir() {
|
|
4392
|
+
for (const candidate of packageRootCandidates()) {
|
|
4393
|
+
if (existsSync3(join10(candidate, "loader.js"))) return candidate;
|
|
4394
|
+
}
|
|
4395
|
+
return packageRootCandidates()[0];
|
|
4396
|
+
}
|
|
4397
|
+
function normalizeMonacoVsRelative(file) {
|
|
4398
|
+
return file.startsWith("vs/") ? file.slice(3) : file;
|
|
4399
|
+
}
|
|
4400
|
+
function monacoFileError(file) {
|
|
4401
|
+
if (file === null || file.length === 0) return "missing file";
|
|
4402
|
+
if (file.includes("\0") || file.includes("\\") || file.includes("?") || file.includes("#")) {
|
|
4403
|
+
return "invalid file";
|
|
4404
|
+
}
|
|
4405
|
+
if (file.startsWith("/") || /^[A-Za-z]:/.test(file)) return "invalid file";
|
|
4406
|
+
if (!FILE_PATTERN.test(file)) return "invalid file";
|
|
4407
|
+
const relative3 = normalizeMonacoVsRelative(file);
|
|
4408
|
+
if (relative3 === "" || relative3.split("/").some((part) => part === "" || part === "." || part === "..")) {
|
|
4409
|
+
return "invalid file";
|
|
4410
|
+
}
|
|
4411
|
+
return null;
|
|
4412
|
+
}
|
|
4413
|
+
function contentTypeFor(filePath) {
|
|
4414
|
+
switch (extname(filePath).toLowerCase()) {
|
|
4415
|
+
case ".js":
|
|
4416
|
+
case ".mjs":
|
|
4417
|
+
return "text/javascript; charset=utf-8";
|
|
4418
|
+
case ".css":
|
|
4419
|
+
return "text/css; charset=utf-8";
|
|
4420
|
+
case ".ttf":
|
|
4421
|
+
return "font/ttf";
|
|
4422
|
+
case ".json":
|
|
4423
|
+
return "application/json; charset=utf-8";
|
|
4424
|
+
default:
|
|
4425
|
+
return "application/octet-stream";
|
|
4426
|
+
}
|
|
4427
|
+
}
|
|
4428
|
+
function plain(status, message) {
|
|
4429
|
+
return new Response(message, {
|
|
4430
|
+
status,
|
|
4431
|
+
headers: {
|
|
4432
|
+
"content-type": "text/plain; charset=utf-8",
|
|
4433
|
+
"x-content-type-options": "nosniff",
|
|
4434
|
+
"cache-control": "no-store"
|
|
4435
|
+
}
|
|
4436
|
+
});
|
|
4437
|
+
}
|
|
4438
|
+
function isInside(root, candidate) {
|
|
4439
|
+
const normalizedRoot = resolve5(root);
|
|
4440
|
+
const normalized = resolve5(candidate);
|
|
4441
|
+
if (normalized === normalizedRoot) return true;
|
|
4442
|
+
return normalized.startsWith(normalizedRoot + sep5);
|
|
4443
|
+
}
|
|
4444
|
+
async function locateFile(vsDir, relativeUnderVs) {
|
|
4445
|
+
const rootReal = await fsp2.realpath(vsDir);
|
|
4446
|
+
const parts = relativeUnderVs.split("/");
|
|
4447
|
+
let cursor = rootReal;
|
|
4448
|
+
for (const part of parts) {
|
|
4449
|
+
cursor = join10(cursor, part);
|
|
4450
|
+
if (!isInside(rootReal, cursor)) return null;
|
|
4451
|
+
}
|
|
4452
|
+
if (!isInside(rootReal, cursor)) return null;
|
|
4453
|
+
try {
|
|
4454
|
+
const real = await fsp2.realpath(cursor);
|
|
4455
|
+
if (!isInside(rootReal, real)) return null;
|
|
4456
|
+
const stat = await fsp2.stat(real);
|
|
4457
|
+
if (!stat.isFile()) return null;
|
|
4458
|
+
return { path: real };
|
|
4459
|
+
} catch {
|
|
4460
|
+
return null;
|
|
4461
|
+
}
|
|
4462
|
+
}
|
|
4463
|
+
async function handleMonacoRequest(request) {
|
|
4464
|
+
const url = new URL(request.url);
|
|
4465
|
+
const requested = url.searchParams.get("file");
|
|
4466
|
+
const reason = monacoFileError(requested);
|
|
4467
|
+
if (reason !== null || requested === null) return plain(400, reason ?? "missing file");
|
|
4468
|
+
let relative3 = normalizeMonacoVsRelative(requested);
|
|
4469
|
+
const vsDir = resolveMonacoVsDir();
|
|
4470
|
+
let located = await locateFile(vsDir, relative3);
|
|
4471
|
+
if (located === null && !relative3.endsWith(".js")) {
|
|
4472
|
+
const withJs = `${relative3}.js`;
|
|
4473
|
+
if (monacoFileError(withJs) === null) {
|
|
4474
|
+
located = await locateFile(vsDir, withJs);
|
|
4475
|
+
if (located !== null) relative3 = withJs;
|
|
4476
|
+
}
|
|
4477
|
+
}
|
|
4478
|
+
if (located === null) return plain(404, "not found");
|
|
4479
|
+
if (request.method === "HEAD") {
|
|
4480
|
+
return new Response(null, {
|
|
4481
|
+
status: 200,
|
|
4482
|
+
headers: {
|
|
4483
|
+
"content-type": contentTypeFor(relative3),
|
|
4484
|
+
"x-content-type-options": "nosniff",
|
|
4485
|
+
"cache-control": "public, max-age=31536000, immutable"
|
|
4486
|
+
}
|
|
4487
|
+
});
|
|
4488
|
+
}
|
|
4489
|
+
const body = await fsp2.readFile(located.path);
|
|
4490
|
+
return new Response(body, {
|
|
4491
|
+
status: 200,
|
|
4492
|
+
headers: {
|
|
4493
|
+
"content-type": contentTypeFor(relative3),
|
|
4494
|
+
"x-content-type-options": "nosniff",
|
|
4495
|
+
"cache-control": "public, max-age=31536000, immutable"
|
|
4496
|
+
}
|
|
4497
|
+
});
|
|
4498
|
+
}
|
|
4499
|
+
function installMonacoRoute(ctx) {
|
|
4500
|
+
let connection;
|
|
4501
|
+
if (typeof ctx.get === "function") {
|
|
4502
|
+
try {
|
|
4503
|
+
const value = ctx.get("connection");
|
|
4504
|
+
if (value !== null && typeof value === "object") {
|
|
4505
|
+
connection = value;
|
|
4506
|
+
}
|
|
4507
|
+
} catch {
|
|
4508
|
+
connection = void 0;
|
|
4509
|
+
}
|
|
4510
|
+
}
|
|
4511
|
+
const register = connection?.fetch?.register;
|
|
4512
|
+
if (typeof register !== "function" || connection?.fetch === void 0) {
|
|
4513
|
+
ctx.logger.warn("agent-enhancer: Monaco route skipped (connection.fetch unavailable)");
|
|
4514
|
+
return;
|
|
4515
|
+
}
|
|
4516
|
+
try {
|
|
4517
|
+
register.call(connection.fetch, {
|
|
4518
|
+
path: MONACO_ROUTE,
|
|
4519
|
+
methods: ["GET", "HEAD"],
|
|
4520
|
+
requestBody: "buffered",
|
|
4521
|
+
fetch: (request) => handleMonacoRequest(request)
|
|
4522
|
+
});
|
|
4523
|
+
} catch (error) {
|
|
4524
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
4525
|
+
ctx.logger.warn(`agent-enhancer: Monaco route registration failed: ${detail}`);
|
|
4526
|
+
}
|
|
4527
|
+
}
|
|
4528
|
+
|
|
4529
|
+
// src/host/context-inspect.ts
|
|
4530
|
+
var CHARS_PER_TOKEN = 4;
|
|
4531
|
+
var BLOCK_OVERHEAD = 4;
|
|
4532
|
+
var PREVIEW_LIMIT = 160;
|
|
4533
|
+
var BODY_LIMIT = 12e3;
|
|
4534
|
+
var MAX_ITEMS = 80;
|
|
4535
|
+
var MAX_EVENT_MARKS = 12;
|
|
4536
|
+
function textBlocks(blocks) {
|
|
4537
|
+
const parts = [];
|
|
4538
|
+
for (const block of blocks) {
|
|
4539
|
+
if (block.type === "text" && typeof block.text === "string" && block.text.trim() !== "") {
|
|
4540
|
+
parts.push(block.text.trim());
|
|
4541
|
+
}
|
|
4542
|
+
}
|
|
4543
|
+
return parts;
|
|
4544
|
+
}
|
|
4545
|
+
function textFromBlocks(blocks) {
|
|
4546
|
+
return textBlocks(blocks).join("\n\n");
|
|
4547
|
+
}
|
|
4548
|
+
function allocateTokens(total, weights) {
|
|
4549
|
+
if (weights.length === 0) return [];
|
|
4550
|
+
if (weights.length === 1) return [total];
|
|
4551
|
+
const sum = weights.reduce((acc, weight) => acc + weight, 0);
|
|
4552
|
+
if (sum <= 0) {
|
|
4553
|
+
const even = Math.floor(total / weights.length);
|
|
4554
|
+
const parts = weights.map(() => even);
|
|
4555
|
+
parts[parts.length - 1] = total - even * (weights.length - 1);
|
|
4556
|
+
return parts;
|
|
4557
|
+
}
|
|
4558
|
+
const raw = weights.map((weight) => total * weight / sum);
|
|
4559
|
+
const floored = raw.map((value) => Math.floor(value));
|
|
4560
|
+
let remain = total - floored.reduce((acc, value) => acc + value, 0);
|
|
4561
|
+
const order = raw.map((value, index) => ({ index, frac: value - Math.floor(value) })).sort((a, b) => b.frac - a.frac);
|
|
4562
|
+
for (const entry of order) {
|
|
4563
|
+
if (remain <= 0) break;
|
|
4564
|
+
floored[entry.index] = (floored[entry.index] ?? 0) + 1;
|
|
4565
|
+
remain -= 1;
|
|
4566
|
+
}
|
|
4567
|
+
return floored;
|
|
4568
|
+
}
|
|
4569
|
+
function systemTitle(text3, index, total) {
|
|
4570
|
+
const firstLine = text3.split(/\r?\n/, 1)[0]?.trim() ?? "";
|
|
4571
|
+
if (firstLine !== "" && firstLine.length <= 48) return firstLine;
|
|
4572
|
+
if (total <= 1) return "System";
|
|
4573
|
+
return `System \xB7 ${index + 1}`;
|
|
4574
|
+
}
|
|
4575
|
+
function clip(text3, limit) {
|
|
4576
|
+
const compact = text3.replace(/\s+/g, " ").trim();
|
|
4577
|
+
if (compact.length <= limit) return compact;
|
|
4578
|
+
return `${compact.slice(0, Math.max(0, limit - 1))}\u2026`;
|
|
4579
|
+
}
|
|
4580
|
+
function preserveBody(text3, limit = BODY_LIMIT) {
|
|
4581
|
+
const trimmed = text3.trim();
|
|
4582
|
+
if (trimmed.length <= limit) return trimmed;
|
|
4583
|
+
return `${trimmed.slice(0, Math.max(0, limit - 1))}\u2026`;
|
|
4584
|
+
}
|
|
4585
|
+
function isReplaced(event) {
|
|
4586
|
+
const op = event.surfaceOp;
|
|
4587
|
+
return typeof op === "object" && op !== null && op.op === "replace";
|
|
4588
|
+
}
|
|
4589
|
+
function shadowedCount(event) {
|
|
4590
|
+
const op = event.surfaceOp;
|
|
4591
|
+
if (typeof op !== "object" || op === null || op.op !== "replace") return 0;
|
|
4592
|
+
return Math.max(0, Number(op.endSeq) - Number(op.startSeq) + 1);
|
|
4593
|
+
}
|
|
4594
|
+
function classifyReplace(event) {
|
|
4595
|
+
const type = String(event.type);
|
|
4596
|
+
if (type.includes("prune")) return "prune";
|
|
4597
|
+
if (type.includes("compact")) return "compact";
|
|
4598
|
+
const shadowed = shadowedCount(event);
|
|
4599
|
+
if (shadowed >= 3) return "compact";
|
|
4600
|
+
if (event.type === "tool/result" && shadowed >= 2) return "prune";
|
|
4601
|
+
return "replace";
|
|
4602
|
+
}
|
|
4603
|
+
function collectEventMarks(events) {
|
|
4604
|
+
const marks = [];
|
|
4605
|
+
for (const event of events) {
|
|
4606
|
+
if (!isReplaced(event)) continue;
|
|
4607
|
+
const kind = classifyReplace(event);
|
|
4608
|
+
const shadowed = shadowedCount(event);
|
|
4609
|
+
marks.push({
|
|
4610
|
+
id: `evt:${event.seq}`,
|
|
4611
|
+
kind,
|
|
4612
|
+
seq: Number(event.seq),
|
|
4613
|
+
shadowed,
|
|
4614
|
+
title: `${kind} \xB7 seq ${event.seq}`
|
|
4615
|
+
});
|
|
4616
|
+
}
|
|
4617
|
+
if (marks.length <= MAX_EVENT_MARKS) return marks;
|
|
4618
|
+
return marks.slice(marks.length - MAX_EVENT_MARKS);
|
|
4619
|
+
}
|
|
4620
|
+
function sourceKind(event) {
|
|
4621
|
+
if (event.type === "user/message") {
|
|
4622
|
+
const source = event.data.source;
|
|
4623
|
+
return typeof source?.kind === "string" ? source.kind : void 0;
|
|
4624
|
+
}
|
|
4625
|
+
if (event.type === "assistant/message" || event.type === "tool/result" || event.type === "system/message" || event.type === "developer/message") {
|
|
4626
|
+
const source = event.data.message?.source;
|
|
4627
|
+
return typeof source?.kind === "string" ? source.kind : void 0;
|
|
4628
|
+
}
|
|
4629
|
+
return void 0;
|
|
4630
|
+
}
|
|
4631
|
+
function messageMeta(event) {
|
|
4632
|
+
switch (event.type) {
|
|
4633
|
+
case "system/message":
|
|
4634
|
+
return { kind: "system", title: "System" };
|
|
4635
|
+
case "developer/message":
|
|
4636
|
+
return { kind: "developer", title: "Developer" };
|
|
4637
|
+
case "user/message": {
|
|
4638
|
+
const kind = sourceKind(event);
|
|
4639
|
+
if (kind === void 0 || kind === "user") return { kind: "user", title: "User" };
|
|
4640
|
+
return { kind: "inject", title: `Inject \xB7 ${kind}` };
|
|
4641
|
+
}
|
|
4642
|
+
case "assistant/message":
|
|
4643
|
+
return { kind: "assistant", title: "Assistant" };
|
|
4644
|
+
case "tool/result": {
|
|
4645
|
+
const message = event.data.message;
|
|
4646
|
+
return {
|
|
4647
|
+
kind: "tool",
|
|
4648
|
+
title: `Tool \xB7 ${message.name ?? message.id ?? "unknown"}`
|
|
4649
|
+
};
|
|
4650
|
+
}
|
|
4651
|
+
default:
|
|
4652
|
+
return { kind: "developer", title: event.type };
|
|
4653
|
+
}
|
|
4654
|
+
}
|
|
4655
|
+
function blocksOf(event) {
|
|
4656
|
+
switch (event.type) {
|
|
4657
|
+
case "user/message":
|
|
4658
|
+
return event.data.content;
|
|
4659
|
+
case "system/message":
|
|
4660
|
+
case "developer/message":
|
|
4661
|
+
case "assistant/message":
|
|
4662
|
+
case "tool/result":
|
|
4663
|
+
return event.data.message.content;
|
|
3424
4664
|
default:
|
|
3425
4665
|
return [];
|
|
3426
4666
|
}
|
|
@@ -3726,43 +4966,77 @@ function installAutoSessionRoute(ctx, options) {
|
|
|
3726
4966
|
});
|
|
3727
4967
|
ctx.effect(() => {
|
|
3728
4968
|
const llm = ctx.llm;
|
|
3729
|
-
|
|
3730
|
-
|
|
4969
|
+
if (typeof llm.prepareCall !== "function") return () => {
|
|
4970
|
+
};
|
|
4971
|
+
const innerPrepare = llm.prepareCall.bind(llm);
|
|
4972
|
+
const innerStream = typeof llm.stream === "function" ? llm.stream.bind(llm) : void 0;
|
|
4973
|
+
const remapAuto = async (config, signal, agent, feature = "session") => {
|
|
4974
|
+
const settings = options.settings();
|
|
4975
|
+
const map = autoRouteMapOf(settings);
|
|
4976
|
+
const note = (agent === void 0 ? void 0 : notes.get(agent)) ?? loggedTurn(asAgent(agent));
|
|
4977
|
+
const classify = settings.autoClassifierEnabled ? createClassifier({ prepareCall: innerPrepare }, map.classifier) : void 0;
|
|
4978
|
+
const mapped = await resolveAutoRoute({
|
|
4979
|
+
text: note?.text ?? "",
|
|
4980
|
+
feature,
|
|
4981
|
+
toolHeavy: note?.toolHeavy ?? false,
|
|
4982
|
+
map,
|
|
4983
|
+
classifierEnabled: settings.autoClassifierEnabled,
|
|
4984
|
+
classify,
|
|
4985
|
+
isAvailable: (provider, model) => provider !== "" && model !== "" && !isAutoModel({ provider, model }),
|
|
4986
|
+
signal,
|
|
4987
|
+
fallback: fallbackRoute(ctx, asAgent(agent))
|
|
4988
|
+
});
|
|
4989
|
+
if (agent !== void 0 && feature === "session") setSessionResolvedRoute(agent, mapped);
|
|
4990
|
+
return mapped;
|
|
4991
|
+
};
|
|
4992
|
+
const wrappedPrepare = async (config, signal, configure) => {
|
|
3731
4993
|
const agent = activeAgent(ctx);
|
|
3732
4994
|
const keyProbe = agent ?? config;
|
|
3733
4995
|
const depth = resolving.get(keyProbe) ?? 0;
|
|
3734
4996
|
if (depth > 0 || !shouldRemapAuto(config, asAgent(agent))) {
|
|
3735
|
-
return
|
|
4997
|
+
return innerPrepare(config, signal, configure);
|
|
3736
4998
|
}
|
|
3737
4999
|
const key = agent ?? config;
|
|
3738
5000
|
resolving.set(key, (resolving.get(key) ?? 0) + 1);
|
|
3739
5001
|
try {
|
|
3740
|
-
const
|
|
3741
|
-
|
|
3742
|
-
const note = (agent === void 0 ? void 0 : notes.get(agent)) ?? loggedTurn(asAgent(agent));
|
|
3743
|
-
const classify = settings.autoClassifierEnabled ? createClassifier({ prepareCall: inner }, map.classifier) : void 0;
|
|
3744
|
-
const mapped = await resolveAutoRoute({
|
|
3745
|
-
text: note?.text ?? "",
|
|
3746
|
-
feature: "session",
|
|
3747
|
-
toolHeavy: note?.toolHeavy ?? false,
|
|
3748
|
-
map,
|
|
3749
|
-
classifierEnabled: settings.autoClassifierEnabled,
|
|
3750
|
-
classify,
|
|
3751
|
-
isAvailable: (provider, model) => provider !== "" && model !== "" && !isAutoModel({ provider, model }),
|
|
3752
|
-
signal,
|
|
3753
|
-
fallback: fallbackRoute(ctx, asAgent(agent))
|
|
3754
|
-
});
|
|
3755
|
-
if (agent !== void 0) setSessionResolvedRoute(agent, mapped);
|
|
3756
|
-
return await inner(routeConfig(config, mapped), signal, configure);
|
|
5002
|
+
const mapped = await remapAuto(config, signal, agent);
|
|
5003
|
+
return await innerPrepare(routeConfig(config, mapped), signal, configure);
|
|
3757
5004
|
} finally {
|
|
3758
5005
|
const nextDepth = (resolving.get(key) ?? 1) - 1;
|
|
3759
5006
|
if (nextDepth <= 0) resolving.delete(key);
|
|
3760
5007
|
else resolving.set(key, nextDepth);
|
|
3761
5008
|
}
|
|
3762
5009
|
};
|
|
3763
|
-
llm.prepareCall =
|
|
5010
|
+
llm.prepareCall = wrappedPrepare;
|
|
5011
|
+
let wrappedStream;
|
|
5012
|
+
if (innerStream !== void 0) {
|
|
5013
|
+
wrappedStream = (options2) => {
|
|
5014
|
+
const agent = activeAgent(ctx);
|
|
5015
|
+
if (!shouldRemapAuto(options2, asAgent(agent))) return innerStream(options2);
|
|
5016
|
+
const keyProbe = agent ?? options2;
|
|
5017
|
+
const depth = resolving.get(keyProbe) ?? 0;
|
|
5018
|
+
if (depth > 0) return innerStream(options2);
|
|
5019
|
+
return (async function* remappedAutoStream() {
|
|
5020
|
+
const key = agent ?? options2;
|
|
5021
|
+
resolving.set(key, (resolving.get(key) ?? 0) + 1);
|
|
5022
|
+
try {
|
|
5023
|
+
const mapped = await remapAuto(options2, options2.signal, agent, "session");
|
|
5024
|
+
const routed = routeConfig(options2, mapped);
|
|
5025
|
+
yield* innerStream({ ...options2, ...routed });
|
|
5026
|
+
} finally {
|
|
5027
|
+
const nextDepth = (resolving.get(key) ?? 1) - 1;
|
|
5028
|
+
if (nextDepth <= 0) resolving.delete(key);
|
|
5029
|
+
else resolving.set(key, nextDepth);
|
|
5030
|
+
}
|
|
5031
|
+
})();
|
|
5032
|
+
};
|
|
5033
|
+
llm.stream = wrappedStream;
|
|
5034
|
+
}
|
|
3764
5035
|
return () => {
|
|
3765
|
-
if (llm.prepareCall ===
|
|
5036
|
+
if (llm.prepareCall === wrappedPrepare) llm.prepareCall = innerPrepare;
|
|
5037
|
+
if (wrappedStream !== void 0 && llm.stream === wrappedStream && innerStream !== void 0) {
|
|
5038
|
+
llm.stream = innerStream;
|
|
5039
|
+
}
|
|
3766
5040
|
};
|
|
3767
5041
|
});
|
|
3768
5042
|
}
|
|
@@ -4930,8 +6204,8 @@ function mergeUsageCalls(stored, history) {
|
|
|
4930
6204
|
|
|
4931
6205
|
// src/host/usage/ledger.ts
|
|
4932
6206
|
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
4933
|
-
import { chmod as chmod4, mkdir as
|
|
4934
|
-
import { basename as
|
|
6207
|
+
import { chmod as chmod4, mkdir as mkdir5, readFile as readFile5, rename as rename4, unlink as unlink5, writeFile as writeFile4 } from "node:fs/promises";
|
|
6208
|
+
import { basename as basename5, join as join11 } from "node:path";
|
|
4935
6209
|
import { z as z3 } from "zod";
|
|
4936
6210
|
var callSchema = z3.object({
|
|
4937
6211
|
at: z3.number().int().nonnegative(),
|
|
@@ -4953,21 +6227,21 @@ var fileSchema3 = z3.object({
|
|
|
4953
6227
|
function isMissing4(error) {
|
|
4954
6228
|
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
4955
6229
|
}
|
|
4956
|
-
async function
|
|
6230
|
+
async function removeIfPresent5(path) {
|
|
4957
6231
|
try {
|
|
4958
|
-
await
|
|
6232
|
+
await unlink5(path);
|
|
4959
6233
|
} catch (error) {
|
|
4960
6234
|
if (!isMissing4(error)) throw error;
|
|
4961
6235
|
}
|
|
4962
6236
|
}
|
|
4963
6237
|
async function writeAtomic4(path, value) {
|
|
4964
|
-
const temporary =
|
|
6238
|
+
const temporary = join11(join11(path, ".."), `.${basename5(path)}.${randomUUID6()}.tmp`);
|
|
4965
6239
|
try {
|
|
4966
6240
|
await writeFile4(temporary, JSON.stringify(value), { encoding: "utf8", mode: 384, flag: "wx" });
|
|
4967
6241
|
await rename4(temporary, path);
|
|
4968
6242
|
await chmod4(path, 384);
|
|
4969
6243
|
} finally {
|
|
4970
|
-
await
|
|
6244
|
+
await removeIfPresent5(temporary);
|
|
4971
6245
|
}
|
|
4972
6246
|
}
|
|
4973
6247
|
var UsageLedger = class {
|
|
@@ -4975,7 +6249,7 @@ var UsageLedger = class {
|
|
|
4975
6249
|
}) {
|
|
4976
6250
|
this.root = root;
|
|
4977
6251
|
this.warn = warn;
|
|
4978
|
-
this.file =
|
|
6252
|
+
this.file = join11(root, "ledger.json");
|
|
4979
6253
|
}
|
|
4980
6254
|
root;
|
|
4981
6255
|
warn;
|
|
@@ -5016,7 +6290,7 @@ var UsageLedger = class {
|
|
|
5016
6290
|
async read() {
|
|
5017
6291
|
let text3;
|
|
5018
6292
|
try {
|
|
5019
|
-
text3 = await
|
|
6293
|
+
text3 = await readFile5(this.file, "utf8");
|
|
5020
6294
|
} catch (error) {
|
|
5021
6295
|
if (isMissing4(error)) return [];
|
|
5022
6296
|
throw error;
|
|
@@ -5042,7 +6316,7 @@ var UsageLedger = class {
|
|
|
5042
6316
|
await this.persist();
|
|
5043
6317
|
}
|
|
5044
6318
|
async persist() {
|
|
5045
|
-
await
|
|
6319
|
+
await mkdir5(this.root, { recursive: true, mode: 448 });
|
|
5046
6320
|
await writeAtomic4(this.file, { schemaVersion: 1, calls: this.calls });
|
|
5047
6321
|
}
|
|
5048
6322
|
};
|
|
@@ -5234,6 +6508,147 @@ var contextCompactResultSchema = z4.strictObject({
|
|
|
5234
6508
|
});
|
|
5235
6509
|
var contextCompactRequestCodec = codec("dsh-agent-enhancer#ContextCompactRequest", contextCompactRequestSchema);
|
|
5236
6510
|
var contextCompactResultCodec = codec("dsh-agent-enhancer#ContextCompactResult", contextCompactResultSchema);
|
|
6511
|
+
var deleteSessionRequestSchema = z4.strictObject({
|
|
6512
|
+
sessionId: sessionIdSchema3
|
|
6513
|
+
});
|
|
6514
|
+
var deleteSessionCleanerReportSchema = z4.strictObject({
|
|
6515
|
+
id: z4.string(),
|
|
6516
|
+
label: z4.string(),
|
|
6517
|
+
ok: z4.boolean(),
|
|
6518
|
+
detail: z4.string().optional()
|
|
6519
|
+
});
|
|
6520
|
+
var deleteSessionResultSchema = z4.strictObject({
|
|
6521
|
+
ok: z4.literal(true),
|
|
6522
|
+
sessionId: sessionIdSchema3,
|
|
6523
|
+
unloaded: z4.boolean(),
|
|
6524
|
+
hidden: z4.boolean(),
|
|
6525
|
+
live: z4.boolean(),
|
|
6526
|
+
removedPaths: z4.array(z4.string()),
|
|
6527
|
+
registry: z4.strictObject({
|
|
6528
|
+
workspaces: z4.array(z4.string()),
|
|
6529
|
+
archived: z4.boolean(),
|
|
6530
|
+
pinned: z4.boolean()
|
|
6531
|
+
}),
|
|
6532
|
+
pluginCleaners: z4.array(deleteSessionCleanerReportSchema),
|
|
6533
|
+
detail: z4.string()
|
|
6534
|
+
});
|
|
6535
|
+
var deleteSessionRequestCodec = codec("dsh-agent-enhancer#DeleteSessionRequest", deleteSessionRequestSchema);
|
|
6536
|
+
var deleteSessionResultCodec = codec("dsh-agent-enhancer#DeleteSessionResult", deleteSessionResultSchema);
|
|
6537
|
+
var readWorkspaceFileRequestSchema = z4.strictObject({
|
|
6538
|
+
sessionId: sessionIdSchema3,
|
|
6539
|
+
path: z4.string().min(1)
|
|
6540
|
+
});
|
|
6541
|
+
var readWorkspaceFileResultSchema = z4.strictObject({
|
|
6542
|
+
path: z4.string(),
|
|
6543
|
+
absolutePath: z4.string(),
|
|
6544
|
+
text: z4.string(),
|
|
6545
|
+
bytes: z4.number().int().nonnegative(),
|
|
6546
|
+
truncated: z4.boolean()
|
|
6547
|
+
});
|
|
6548
|
+
var writeWorkspaceFileRequestSchema = z4.strictObject({
|
|
6549
|
+
sessionId: sessionIdSchema3,
|
|
6550
|
+
path: z4.string().min(1),
|
|
6551
|
+
content: z4.string()
|
|
6552
|
+
});
|
|
6553
|
+
var writeWorkspaceFileResultSchema = z4.strictObject({
|
|
6554
|
+
path: z4.string(),
|
|
6555
|
+
absolutePath: z4.string(),
|
|
6556
|
+
bytes: z4.number().int().nonnegative()
|
|
6557
|
+
});
|
|
6558
|
+
var readWorkspaceFileRequestCodec = codec(
|
|
6559
|
+
"dsh-agent-enhancer#ReadWorkspaceFileRequest",
|
|
6560
|
+
readWorkspaceFileRequestSchema
|
|
6561
|
+
);
|
|
6562
|
+
var readWorkspaceFileResultCodec = codec(
|
|
6563
|
+
"dsh-agent-enhancer#ReadWorkspaceFileResult",
|
|
6564
|
+
readWorkspaceFileResultSchema
|
|
6565
|
+
);
|
|
6566
|
+
var writeWorkspaceFileRequestCodec = codec(
|
|
6567
|
+
"dsh-agent-enhancer#WriteWorkspaceFileRequest",
|
|
6568
|
+
writeWorkspaceFileRequestSchema
|
|
6569
|
+
);
|
|
6570
|
+
var writeWorkspaceFileResultCodec = codec(
|
|
6571
|
+
"dsh-agent-enhancer#WriteWorkspaceFileResult",
|
|
6572
|
+
writeWorkspaceFileResultSchema
|
|
6573
|
+
);
|
|
6574
|
+
var extraFolderRootSchema = z4.strictObject({
|
|
6575
|
+
path: z4.string().min(1),
|
|
6576
|
+
title: z4.string().min(1)
|
|
6577
|
+
});
|
|
6578
|
+
var listExtraFoldersRequestSchema = z4.strictObject({
|
|
6579
|
+
sessionId: sessionIdSchema3
|
|
6580
|
+
});
|
|
6581
|
+
var listExtraFoldersResultSchema = z4.strictObject({
|
|
6582
|
+
primaryCwd: z4.string().min(1),
|
|
6583
|
+
primaryTitle: z4.string().min(1),
|
|
6584
|
+
roots: z4.array(extraFolderRootSchema)
|
|
6585
|
+
});
|
|
6586
|
+
var addExtraFolderRequestSchema = z4.strictObject({
|
|
6587
|
+
sessionId: sessionIdSchema3,
|
|
6588
|
+
path: z4.string().min(1),
|
|
6589
|
+
title: z4.string().min(1).optional()
|
|
6590
|
+
});
|
|
6591
|
+
var pickExtraFolderRequestSchema = z4.strictObject({
|
|
6592
|
+
sessionId: sessionIdSchema3
|
|
6593
|
+
});
|
|
6594
|
+
var pickExtraFolderResultSchema = z4.strictObject({
|
|
6595
|
+
path: z4.string().min(1).nullable(),
|
|
6596
|
+
primaryCwd: z4.string().min(1),
|
|
6597
|
+
primaryTitle: z4.string().min(1),
|
|
6598
|
+
roots: z4.array(extraFolderRootSchema),
|
|
6599
|
+
added: z4.boolean()
|
|
6600
|
+
});
|
|
6601
|
+
var removeExtraFolderRequestSchema = z4.strictObject({
|
|
6602
|
+
sessionId: sessionIdSchema3,
|
|
6603
|
+
path: z4.string().min(1)
|
|
6604
|
+
});
|
|
6605
|
+
var listExtraFolderEntriesRequestSchema = z4.strictObject({
|
|
6606
|
+
sessionId: sessionIdSchema3,
|
|
6607
|
+
rootPath: z4.string().min(1),
|
|
6608
|
+
relativePath: z4.string().optional()
|
|
6609
|
+
});
|
|
6610
|
+
var listExtraFolderEntriesResultSchema = z4.strictObject({
|
|
6611
|
+
rootPath: z4.string().min(1),
|
|
6612
|
+
relativePath: z4.string(),
|
|
6613
|
+
entries: z4.array(z4.strictObject({
|
|
6614
|
+
name: z4.string().min(1),
|
|
6615
|
+
path: z4.string(),
|
|
6616
|
+
absolutePath: z4.string().min(1),
|
|
6617
|
+
kind: z4.enum(["directory", "file"])
|
|
6618
|
+
}))
|
|
6619
|
+
});
|
|
6620
|
+
var listExtraFoldersRequestCodec = codec(
|
|
6621
|
+
"dsh-agent-enhancer#ListExtraFoldersRequest",
|
|
6622
|
+
listExtraFoldersRequestSchema
|
|
6623
|
+
);
|
|
6624
|
+
var listExtraFoldersResultCodec = codec(
|
|
6625
|
+
"dsh-agent-enhancer#ListExtraFoldersResult",
|
|
6626
|
+
listExtraFoldersResultSchema
|
|
6627
|
+
);
|
|
6628
|
+
var addExtraFolderRequestCodec = codec(
|
|
6629
|
+
"dsh-agent-enhancer#AddExtraFolderRequest",
|
|
6630
|
+
addExtraFolderRequestSchema
|
|
6631
|
+
);
|
|
6632
|
+
var pickExtraFolderRequestCodec = codec(
|
|
6633
|
+
"dsh-agent-enhancer#PickExtraFolderRequest",
|
|
6634
|
+
pickExtraFolderRequestSchema
|
|
6635
|
+
);
|
|
6636
|
+
var pickExtraFolderResultCodec = codec(
|
|
6637
|
+
"dsh-agent-enhancer#PickExtraFolderResult",
|
|
6638
|
+
pickExtraFolderResultSchema
|
|
6639
|
+
);
|
|
6640
|
+
var removeExtraFolderRequestCodec = codec(
|
|
6641
|
+
"dsh-agent-enhancer#RemoveExtraFolderRequest",
|
|
6642
|
+
removeExtraFolderRequestSchema
|
|
6643
|
+
);
|
|
6644
|
+
var listExtraFolderEntriesRequestCodec = codec(
|
|
6645
|
+
"dsh-agent-enhancer#ListExtraFolderEntriesRequest",
|
|
6646
|
+
listExtraFolderEntriesRequestSchema
|
|
6647
|
+
);
|
|
6648
|
+
var listExtraFolderEntriesResultCodec = codec(
|
|
6649
|
+
"dsh-agent-enhancer#ListExtraFolderEntriesResult",
|
|
6650
|
+
listExtraFolderEntriesResultSchema
|
|
6651
|
+
);
|
|
5237
6652
|
var updateCheckResultSchema = z4.strictObject({
|
|
5238
6653
|
current: z4.string(),
|
|
5239
6654
|
latest: z4.string().nullable(),
|
|
@@ -5387,6 +6802,110 @@ var remoteInfo = createTypertRemoteInfo({
|
|
|
5387
6802
|
cancellation: { parameter: "signal" },
|
|
5388
6803
|
result: contextCompactResultCodec
|
|
5389
6804
|
},
|
|
6805
|
+
"agentEnhancer/deleteSession": {
|
|
6806
|
+
service: "agentEnhancer",
|
|
6807
|
+
implementation: "deleteSession",
|
|
6808
|
+
invocation: { kind: "direct" },
|
|
6809
|
+
parameters: [{
|
|
6810
|
+
name: "request",
|
|
6811
|
+
source: "json",
|
|
6812
|
+
codec: deleteSessionRequestCodec,
|
|
6813
|
+
strictCodec: deleteSessionRequestCodec
|
|
6814
|
+
}],
|
|
6815
|
+
cancellation: { parameter: "signal" },
|
|
6816
|
+
result: deleteSessionResultCodec
|
|
6817
|
+
},
|
|
6818
|
+
"agentEnhancer/readWorkspaceFile": {
|
|
6819
|
+
service: "agentEnhancer",
|
|
6820
|
+
implementation: "readWorkspaceFile",
|
|
6821
|
+
invocation: { kind: "direct" },
|
|
6822
|
+
parameters: [{
|
|
6823
|
+
name: "request",
|
|
6824
|
+
source: "json",
|
|
6825
|
+
codec: readWorkspaceFileRequestCodec,
|
|
6826
|
+
strictCodec: readWorkspaceFileRequestCodec
|
|
6827
|
+
}],
|
|
6828
|
+
cancellation: { parameter: "signal" },
|
|
6829
|
+
result: readWorkspaceFileResultCodec
|
|
6830
|
+
},
|
|
6831
|
+
"agentEnhancer/writeWorkspaceFile": {
|
|
6832
|
+
service: "agentEnhancer",
|
|
6833
|
+
implementation: "writeWorkspaceFile",
|
|
6834
|
+
invocation: { kind: "direct" },
|
|
6835
|
+
parameters: [{
|
|
6836
|
+
name: "request",
|
|
6837
|
+
source: "json",
|
|
6838
|
+
codec: writeWorkspaceFileRequestCodec,
|
|
6839
|
+
strictCodec: writeWorkspaceFileRequestCodec
|
|
6840
|
+
}],
|
|
6841
|
+
cancellation: { parameter: "signal" },
|
|
6842
|
+
result: writeWorkspaceFileResultCodec
|
|
6843
|
+
},
|
|
6844
|
+
"agentEnhancer/listExtraFolders": {
|
|
6845
|
+
service: "agentEnhancer",
|
|
6846
|
+
implementation: "listExtraFolders",
|
|
6847
|
+
invocation: { kind: "direct" },
|
|
6848
|
+
parameters: [{
|
|
6849
|
+
name: "request",
|
|
6850
|
+
source: "json",
|
|
6851
|
+
codec: listExtraFoldersRequestCodec,
|
|
6852
|
+
strictCodec: listExtraFoldersRequestCodec
|
|
6853
|
+
}],
|
|
6854
|
+
cancellation: { parameter: "signal" },
|
|
6855
|
+
result: listExtraFoldersResultCodec
|
|
6856
|
+
},
|
|
6857
|
+
"agentEnhancer/addExtraFolder": {
|
|
6858
|
+
service: "agentEnhancer",
|
|
6859
|
+
implementation: "addExtraFolder",
|
|
6860
|
+
invocation: { kind: "direct" },
|
|
6861
|
+
parameters: [{
|
|
6862
|
+
name: "request",
|
|
6863
|
+
source: "json",
|
|
6864
|
+
codec: addExtraFolderRequestCodec,
|
|
6865
|
+
strictCodec: addExtraFolderRequestCodec
|
|
6866
|
+
}],
|
|
6867
|
+
cancellation: { parameter: "signal" },
|
|
6868
|
+
result: listExtraFoldersResultCodec
|
|
6869
|
+
},
|
|
6870
|
+
"agentEnhancer/pickExtraFolder": {
|
|
6871
|
+
service: "agentEnhancer",
|
|
6872
|
+
implementation: "pickExtraFolder",
|
|
6873
|
+
invocation: { kind: "direct" },
|
|
6874
|
+
parameters: [{
|
|
6875
|
+
name: "request",
|
|
6876
|
+
source: "json",
|
|
6877
|
+
codec: pickExtraFolderRequestCodec,
|
|
6878
|
+
strictCodec: pickExtraFolderRequestCodec
|
|
6879
|
+
}],
|
|
6880
|
+
cancellation: { parameter: "signal" },
|
|
6881
|
+
result: pickExtraFolderResultCodec
|
|
6882
|
+
},
|
|
6883
|
+
"agentEnhancer/removeExtraFolder": {
|
|
6884
|
+
service: "agentEnhancer",
|
|
6885
|
+
implementation: "removeExtraFolder",
|
|
6886
|
+
invocation: { kind: "direct" },
|
|
6887
|
+
parameters: [{
|
|
6888
|
+
name: "request",
|
|
6889
|
+
source: "json",
|
|
6890
|
+
codec: removeExtraFolderRequestCodec,
|
|
6891
|
+
strictCodec: removeExtraFolderRequestCodec
|
|
6892
|
+
}],
|
|
6893
|
+
cancellation: { parameter: "signal" },
|
|
6894
|
+
result: listExtraFoldersResultCodec
|
|
6895
|
+
},
|
|
6896
|
+
"agentEnhancer/listExtraFolderEntries": {
|
|
6897
|
+
service: "agentEnhancer",
|
|
6898
|
+
implementation: "listExtraFolderEntries",
|
|
6899
|
+
invocation: { kind: "direct" },
|
|
6900
|
+
parameters: [{
|
|
6901
|
+
name: "request",
|
|
6902
|
+
source: "json",
|
|
6903
|
+
codec: listExtraFolderEntriesRequestCodec,
|
|
6904
|
+
strictCodec: listExtraFolderEntriesRequestCodec
|
|
6905
|
+
}],
|
|
6906
|
+
cancellation: { parameter: "signal" },
|
|
6907
|
+
result: listExtraFolderEntriesResultCodec
|
|
6908
|
+
},
|
|
5390
6909
|
"agentEnhancer/checkUpdate": {
|
|
5391
6910
|
service: "agentEnhancer",
|
|
5392
6911
|
implementation: "checkUpdate",
|
|
@@ -5606,8 +7125,8 @@ var checkpointRemoteInfo = createTypertRemoteInfo({
|
|
|
5606
7125
|
}, z4);
|
|
5607
7126
|
|
|
5608
7127
|
// src/host/update/check.ts
|
|
5609
|
-
import { homedir } from "node:os";
|
|
5610
|
-
import { join as
|
|
7128
|
+
import { homedir as homedir2 } from "node:os";
|
|
7129
|
+
import { join as join13 } from "node:path";
|
|
5611
7130
|
|
|
5612
7131
|
// src/host/update/cli.ts
|
|
5613
7132
|
import { spawn as nodeSpawn } from "node:child_process";
|
|
@@ -5634,14 +7153,14 @@ function resolveCanAutoApply(input) {
|
|
|
5634
7153
|
return input.installKind === "npm" && input.dshAvailable;
|
|
5635
7154
|
}
|
|
5636
7155
|
function waitForExit(child, timeoutMs) {
|
|
5637
|
-
return new Promise((
|
|
7156
|
+
return new Promise((resolve6) => {
|
|
5638
7157
|
let settled = false;
|
|
5639
7158
|
const chunks = [];
|
|
5640
7159
|
const finish = (ok, detail) => {
|
|
5641
7160
|
if (settled) return;
|
|
5642
7161
|
settled = true;
|
|
5643
7162
|
clearTimeout(timer);
|
|
5644
|
-
|
|
7163
|
+
resolve6({ ok, detail });
|
|
5645
7164
|
};
|
|
5646
7165
|
const timer = setTimeout(() => {
|
|
5647
7166
|
child.kill();
|
|
@@ -5678,9 +7197,9 @@ async function runPluginAdd(latest, spawnFn = defaultSpawn) {
|
|
|
5678
7197
|
|
|
5679
7198
|
// src/host/update/install-kind.ts
|
|
5680
7199
|
import { lstatSync } from "node:fs";
|
|
5681
|
-
import { join as
|
|
7200
|
+
import { join as join12 } from "node:path";
|
|
5682
7201
|
function detectInstallKind(profileDir) {
|
|
5683
|
-
const pkgPath =
|
|
7202
|
+
const pkgPath = join12(profileDir, "node_modules", PACKAGE_NAME);
|
|
5684
7203
|
try {
|
|
5685
7204
|
const stat = lstatSync(pkgPath);
|
|
5686
7205
|
if (stat.isSymbolicLink()) return "link";
|
|
@@ -5810,9 +7329,9 @@ function isNewerVersion(latest, current) {
|
|
|
5810
7329
|
}
|
|
5811
7330
|
|
|
5812
7331
|
// src/host/update/check.ts
|
|
5813
|
-
function resolveDesktopProfileDir(env = process.env, home =
|
|
5814
|
-
const
|
|
5815
|
-
return
|
|
7332
|
+
function resolveDesktopProfileDir(env = process.env, home = homedir2()) {
|
|
7333
|
+
const dshHome2 = env.DSH_HOME ?? join13(home, ".dsh");
|
|
7334
|
+
return join13(dshHome2, "profiles", "desktop");
|
|
5816
7335
|
}
|
|
5817
7336
|
async function checkPluginUpdate(deps) {
|
|
5818
7337
|
const latest = await deps.fetchLatest();
|
|
@@ -5881,15 +7400,22 @@ var inject = [
|
|
|
5881
7400
|
"tokenMeter",
|
|
5882
7401
|
"typert",
|
|
5883
7402
|
"typertGateway",
|
|
5884
|
-
//
|
|
5885
|
-
"commands"
|
|
7403
|
+
// Chat `/compact` → compaction.compactNow; commands kept as fallback.
|
|
7404
|
+
"commands",
|
|
7405
|
+
// Session delete: registry detach + durable workspace/global domain writes.
|
|
7406
|
+
"workspaceRegistry",
|
|
7407
|
+
"storageDomain"
|
|
5886
7408
|
];
|
|
5887
7409
|
var Config = z5.object({
|
|
5888
7410
|
historyEnabled: z5.boolean().default(true).volatile(),
|
|
5889
7411
|
optimizeEnabled: z5.boolean().default(true).volatile(),
|
|
5890
7412
|
predictEnabled: z5.boolean().default(true).volatile(),
|
|
7413
|
+
quoteEnabled: z5.boolean().default(true).volatile(),
|
|
5891
7414
|
rollbackEnabled: z5.boolean().default(true).volatile(),
|
|
5892
7415
|
contextInspectEnabled: z5.boolean().default(true).volatile(),
|
|
7416
|
+
usageEnabled: z5.boolean().default(true).volatile(),
|
|
7417
|
+
sessionDeleteEnabled: z5.boolean().default(true).volatile(),
|
|
7418
|
+
fileEditEnabled: z5.boolean().default(true).volatile(),
|
|
5893
7419
|
// Empty provider/model = follow the current Desktop session model.
|
|
5894
7420
|
optimizeProvider: z5.string().default("").volatile(),
|
|
5895
7421
|
optimizeModel: z5.string().default("").volatile(),
|
|
@@ -5922,7 +7448,7 @@ var hostContribution = {
|
|
|
5922
7448
|
invocations: [...remoteInfo.descriptors, ...checkpointRemoteInfo.descriptors]
|
|
5923
7449
|
};
|
|
5924
7450
|
var AgentEnhancerService = class extends TypertRemoteService {
|
|
5925
|
-
constructor(ctx, models,
|
|
7451
|
+
constructor(ctx, models, usage = {
|
|
5926
7452
|
async summarize(range) {
|
|
5927
7453
|
return summarizeUsage([], range, Date.now());
|
|
5928
7454
|
}
|
|
@@ -5933,17 +7459,31 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
5933
7459
|
apiKey: { source: "api-key", status: "no-key" }
|
|
5934
7460
|
};
|
|
5935
7461
|
}
|
|
5936
|
-
})
|
|
7462
|
+
}, sessionCleanup = new SessionCleanupRegistry(), extraFolders = createExtraFoldersStore(
|
|
7463
|
+
join14(homedir3(), ".dsh", "profiles", "desktop", "plugin-data", PACKAGE_NAME, "extra-folders")
|
|
7464
|
+
)) {
|
|
5937
7465
|
super(ctx, "agentEnhancer");
|
|
5938
7466
|
this.models = models;
|
|
5939
|
-
this.compactRoute = compactRoute;
|
|
5940
7467
|
this.usage = usage;
|
|
5941
7468
|
this.balances = balances;
|
|
7469
|
+
this.sessionCleanup = sessionCleanup;
|
|
7470
|
+
this.extraFolders = extraFolders;
|
|
5942
7471
|
}
|
|
5943
7472
|
models;
|
|
5944
|
-
compactRoute;
|
|
5945
7473
|
usage;
|
|
5946
7474
|
balances;
|
|
7475
|
+
sessionCleanup;
|
|
7476
|
+
extraFolders;
|
|
7477
|
+
extraRootsForSession(sessionId) {
|
|
7478
|
+
const agent = this.ctx.agents.get(sessionId);
|
|
7479
|
+
const cwd = agent?.session?.header?.cwd;
|
|
7480
|
+
if (typeof cwd !== "string" || cwd.trim() === "") return [];
|
|
7481
|
+
try {
|
|
7482
|
+
return loadExtraRootPaths(this.extraFolders, cwd);
|
|
7483
|
+
} catch {
|
|
7484
|
+
return [];
|
|
7485
|
+
}
|
|
7486
|
+
}
|
|
5947
7487
|
assertOwner() {
|
|
5948
7488
|
if (this.ctx.invocation?.peer.principal.kind !== "owner") {
|
|
5949
7489
|
throw new RemoteError("agent-enhancer/forbidden", "Only the desktop owner may use Agent Enhance", {});
|
|
@@ -5991,7 +7531,140 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
5991
7531
|
throw new RemoteError("agent-enhancer/failed", "Context inspect failed", { code: "MODEL_FAILED" });
|
|
5992
7532
|
}
|
|
5993
7533
|
}
|
|
5994
|
-
/**
|
|
7534
|
+
/** Permanently delete one sidebar session (official logs + plugin persistence). */
|
|
7535
|
+
async deleteSession(request, signal) {
|
|
7536
|
+
this.assertOwner();
|
|
7537
|
+
signal.throwIfAborted();
|
|
7538
|
+
try {
|
|
7539
|
+
return await deleteSessionNow({
|
|
7540
|
+
ctx: this.ctx,
|
|
7541
|
+
request,
|
|
7542
|
+
cleanup: this.sessionCleanup,
|
|
7543
|
+
signal
|
|
7544
|
+
});
|
|
7545
|
+
} catch (error) {
|
|
7546
|
+
if (signal.aborted) throw error;
|
|
7547
|
+
if (error instanceof SessionDeleteError) {
|
|
7548
|
+
this.ctx.logger.warn(`agent-enhancer deleteSession failed: ${error.code} ${error.message}`);
|
|
7549
|
+
throw new RemoteError("agent-enhancer/failed", error.message, {
|
|
7550
|
+
code: error.code === "BUSY" ? "SESSION_BUSY" : "SESSION_DELETE_FAILED"
|
|
7551
|
+
});
|
|
7552
|
+
}
|
|
7553
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7554
|
+
this.ctx.logger.warn(`agent-enhancer deleteSession failed: ${detail}`);
|
|
7555
|
+
throw new RemoteError("agent-enhancer/failed", detail || "Session delete failed", {
|
|
7556
|
+
code: "SESSION_DELETE_FAILED"
|
|
7557
|
+
});
|
|
7558
|
+
}
|
|
7559
|
+
}
|
|
7560
|
+
/** Read a workspace UTF-8 text file for the in-app Monaco editor. */
|
|
7561
|
+
async readWorkspaceFile(request, signal) {
|
|
7562
|
+
this.assertOwner();
|
|
7563
|
+
signal.throwIfAborted();
|
|
7564
|
+
try {
|
|
7565
|
+
return await readWorkspaceFileNow({
|
|
7566
|
+
ctx: this.ctx,
|
|
7567
|
+
request,
|
|
7568
|
+
signal,
|
|
7569
|
+
extraRoots: this.extraRootsForSession(request.sessionId)
|
|
7570
|
+
});
|
|
7571
|
+
} catch (error) {
|
|
7572
|
+
if (signal.aborted) throw error;
|
|
7573
|
+
throw fileEditRemoteError(error);
|
|
7574
|
+
}
|
|
7575
|
+
}
|
|
7576
|
+
/** Write a workspace UTF-8 text file from the in-app Monaco editor. */
|
|
7577
|
+
async writeWorkspaceFile(request, signal) {
|
|
7578
|
+
this.assertOwner();
|
|
7579
|
+
signal.throwIfAborted();
|
|
7580
|
+
try {
|
|
7581
|
+
return await writeWorkspaceFileNow({
|
|
7582
|
+
ctx: this.ctx,
|
|
7583
|
+
request,
|
|
7584
|
+
signal,
|
|
7585
|
+
extraRoots: this.extraRootsForSession(request.sessionId)
|
|
7586
|
+
});
|
|
7587
|
+
} catch (error) {
|
|
7588
|
+
if (signal.aborted) throw error;
|
|
7589
|
+
throw fileEditRemoteError(error);
|
|
7590
|
+
}
|
|
7591
|
+
}
|
|
7592
|
+
async listExtraFolders(request, signal) {
|
|
7593
|
+
this.assertOwner();
|
|
7594
|
+
signal.throwIfAborted();
|
|
7595
|
+
try {
|
|
7596
|
+
return await listExtraFoldersNow({
|
|
7597
|
+
ctx: this.ctx,
|
|
7598
|
+
store: this.extraFolders,
|
|
7599
|
+
request,
|
|
7600
|
+
signal
|
|
7601
|
+
});
|
|
7602
|
+
} catch (error) {
|
|
7603
|
+
if (signal.aborted) throw error;
|
|
7604
|
+
throw extraFoldersRemoteError(error);
|
|
7605
|
+
}
|
|
7606
|
+
}
|
|
7607
|
+
async addExtraFolder(request, signal) {
|
|
7608
|
+
this.assertOwner();
|
|
7609
|
+
signal.throwIfAborted();
|
|
7610
|
+
try {
|
|
7611
|
+
return await addExtraFolderNow({
|
|
7612
|
+
ctx: this.ctx,
|
|
7613
|
+
store: this.extraFolders,
|
|
7614
|
+
request,
|
|
7615
|
+
signal
|
|
7616
|
+
});
|
|
7617
|
+
} catch (error) {
|
|
7618
|
+
if (signal.aborted) throw error;
|
|
7619
|
+
throw extraFoldersRemoteError(error);
|
|
7620
|
+
}
|
|
7621
|
+
}
|
|
7622
|
+
async pickExtraFolder(request, signal) {
|
|
7623
|
+
this.assertOwner();
|
|
7624
|
+
signal.throwIfAborted();
|
|
7625
|
+
try {
|
|
7626
|
+
return await pickExtraFolderNow({
|
|
7627
|
+
ctx: this.ctx,
|
|
7628
|
+
store: this.extraFolders,
|
|
7629
|
+
request,
|
|
7630
|
+
signal
|
|
7631
|
+
});
|
|
7632
|
+
} catch (error) {
|
|
7633
|
+
if (signal.aborted) throw error;
|
|
7634
|
+
throw extraFoldersRemoteError(error);
|
|
7635
|
+
}
|
|
7636
|
+
}
|
|
7637
|
+
async removeExtraFolder(request, signal) {
|
|
7638
|
+
this.assertOwner();
|
|
7639
|
+
signal.throwIfAborted();
|
|
7640
|
+
try {
|
|
7641
|
+
return await removeExtraFolderNow({
|
|
7642
|
+
ctx: this.ctx,
|
|
7643
|
+
store: this.extraFolders,
|
|
7644
|
+
request,
|
|
7645
|
+
signal
|
|
7646
|
+
});
|
|
7647
|
+
} catch (error) {
|
|
7648
|
+
if (signal.aborted) throw error;
|
|
7649
|
+
throw extraFoldersRemoteError(error);
|
|
7650
|
+
}
|
|
7651
|
+
}
|
|
7652
|
+
async listExtraFolderEntries(request, signal) {
|
|
7653
|
+
this.assertOwner();
|
|
7654
|
+
signal.throwIfAborted();
|
|
7655
|
+
try {
|
|
7656
|
+
return await listExtraFolderEntriesNow({
|
|
7657
|
+
ctx: this.ctx,
|
|
7658
|
+
store: this.extraFolders,
|
|
7659
|
+
request,
|
|
7660
|
+
signal
|
|
7661
|
+
});
|
|
7662
|
+
} catch (error) {
|
|
7663
|
+
if (signal.aborted) throw error;
|
|
7664
|
+
throw extraFoldersRemoteError(error);
|
|
7665
|
+
}
|
|
7666
|
+
}
|
|
7667
|
+
/** Trigger official idle compaction (same seam as chat `/compact`). */
|
|
5995
7668
|
async compactContext(request, signal) {
|
|
5996
7669
|
this.assertOwner();
|
|
5997
7670
|
signal.throwIfAborted();
|
|
@@ -6003,16 +7676,18 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
6003
7676
|
}
|
|
6004
7677
|
try {
|
|
6005
7678
|
return await withUsageSource("compact", async () => compactContextNow({
|
|
6006
|
-
|
|
7679
|
+
compaction: softService2(this.ctx, "compaction", "compactNow"),
|
|
7680
|
+
commands: softService2(this.ctx, "commands", "execute") ?? this.ctx.commands,
|
|
6007
7681
|
agent,
|
|
6008
7682
|
sessionId: request.sessionId,
|
|
6009
|
-
signal
|
|
6010
|
-
route: await this.compactRoute(agent.session, signal)
|
|
7683
|
+
signal
|
|
6011
7684
|
}));
|
|
6012
7685
|
} catch (error) {
|
|
6013
7686
|
if (signal.aborted) throw error;
|
|
6014
7687
|
const code = error instanceof AuxiliaryError ? error.code : "COMPACT_FAILED";
|
|
6015
|
-
|
|
7688
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7689
|
+
this.ctx.logger.warn(`agent-enhancer compact failed (${code}): ${detail}`);
|
|
7690
|
+
throw new RemoteError("agent-enhancer/failed", detail || "Context compaction failed", { code });
|
|
6016
7691
|
}
|
|
6017
7692
|
}
|
|
6018
7693
|
/** Read-only version probe — same gate policy as `listCatalog` (settings-safe). */
|
|
@@ -6039,22 +7714,40 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
6039
7714
|
});
|
|
6040
7715
|
}
|
|
6041
7716
|
};
|
|
6042
|
-
function resolveCheckpointRoot(
|
|
6043
|
-
const base =
|
|
6044
|
-
const nextPkg =
|
|
6045
|
-
const next =
|
|
6046
|
-
const prev =
|
|
6047
|
-
if (!
|
|
6048
|
-
|
|
6049
|
-
|
|
6050
|
-
const prevPkg =
|
|
7717
|
+
function resolveCheckpointRoot(dshHome2) {
|
|
7718
|
+
const base = join14(dshHome2, "profiles", "desktop", "plugin-data");
|
|
7719
|
+
const nextPkg = join14(base, PACKAGE_NAME);
|
|
7720
|
+
const next = join14(nextPkg, "checkpoint");
|
|
7721
|
+
const prev = join14(base, LEGACY_PACKAGE_NAME, "checkpoint");
|
|
7722
|
+
if (!existsSync4(next) && existsSync4(prev)) {
|
|
7723
|
+
mkdirSync2(nextPkg, { recursive: true });
|
|
7724
|
+
renameSync2(prev, next);
|
|
7725
|
+
const prevPkg = join14(base, LEGACY_PACKAGE_NAME);
|
|
6051
7726
|
try {
|
|
6052
|
-
if (
|
|
7727
|
+
if (existsSync4(prevPkg) && readdirSync(prevPkg).length === 0) rmdirSync(prevPkg);
|
|
6053
7728
|
} catch {
|
|
6054
7729
|
}
|
|
6055
7730
|
}
|
|
6056
7731
|
return next;
|
|
6057
7732
|
}
|
|
7733
|
+
function fileEditRemoteError(error) {
|
|
7734
|
+
if (error instanceof FileEditError) {
|
|
7735
|
+
return new RemoteError("agent-enhancer/failed", error.message, { code: error.code });
|
|
7736
|
+
}
|
|
7737
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7738
|
+
return new RemoteError("agent-enhancer/failed", detail || "File edit failed", {
|
|
7739
|
+
code: "FILE_EDIT_FAILED"
|
|
7740
|
+
});
|
|
7741
|
+
}
|
|
7742
|
+
function extraFoldersRemoteError(error) {
|
|
7743
|
+
if (error instanceof ExtraFoldersError) {
|
|
7744
|
+
return new RemoteError("agent-enhancer/failed", error.message, { code: error.code });
|
|
7745
|
+
}
|
|
7746
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7747
|
+
return new RemoteError("agent-enhancer/failed", detail || "Extra folders failed", {
|
|
7748
|
+
code: "FILE_EDIT_FAILED"
|
|
7749
|
+
});
|
|
7750
|
+
}
|
|
6058
7751
|
function softService2(ctx, name2, method) {
|
|
6059
7752
|
try {
|
|
6060
7753
|
const value = ctx.get(name2);
|
|
@@ -6083,8 +7776,12 @@ function readSettings(config) {
|
|
|
6083
7776
|
historyEnabled: config.historyEnabled.get(),
|
|
6084
7777
|
optimizeEnabled: config.optimizeEnabled.get(),
|
|
6085
7778
|
predictEnabled: config.predictEnabled.get(),
|
|
7779
|
+
quoteEnabled: config.quoteEnabled.get(),
|
|
6086
7780
|
rollbackEnabled: config.rollbackEnabled.get(),
|
|
6087
7781
|
contextInspectEnabled: config.contextInspectEnabled.get(),
|
|
7782
|
+
usageEnabled: config.usageEnabled.get(),
|
|
7783
|
+
sessionDeleteEnabled: config.sessionDeleteEnabled.get(),
|
|
7784
|
+
fileEditEnabled: config.fileEditEnabled.get(),
|
|
6088
7785
|
optimizeProvider: config.optimizeProvider.get(),
|
|
6089
7786
|
optimizeModel: config.optimizeModel.get(),
|
|
6090
7787
|
optimizeReasoningEffort: config.optimizeReasoningEffort.get(),
|
|
@@ -6113,8 +7810,8 @@ function apply(ctx, config) {
|
|
|
6113
7810
|
const settings = () => readSettings(config);
|
|
6114
7811
|
const models = new AuxiliaryModels(ctx, settings);
|
|
6115
7812
|
installAutoSessionRoute(ctx, { settings });
|
|
6116
|
-
const storageRoot = resolveCheckpointRoot(process.env.DSH_HOME ??
|
|
6117
|
-
const usage = new UsageLedger(
|
|
7813
|
+
const storageRoot = resolveCheckpointRoot(process.env.DSH_HOME ?? join14(homedir3(), ".dsh"));
|
|
7814
|
+
const usage = new UsageLedger(join14(storageRoot, "..", "usage"), (message) => {
|
|
6118
7815
|
ctx.logger.warn(message);
|
|
6119
7816
|
});
|
|
6120
7817
|
const balancesFacade = (() => {
|
|
@@ -6143,10 +7840,16 @@ function apply(ctx, config) {
|
|
|
6143
7840
|
};
|
|
6144
7841
|
})();
|
|
6145
7842
|
installUsageObserver(ctx, usage);
|
|
7843
|
+
const store = new CheckpointStore(storageRoot);
|
|
7844
|
+
const cutStore = new SessionCutStore(join14(storageRoot, "cuts"));
|
|
7845
|
+
const cutCache = /* @__PURE__ */ new Map();
|
|
7846
|
+
const sessionCleanup = new SessionCleanupRegistry();
|
|
7847
|
+
sessionCleanup.register(createCheckpointCleaner({ store, cutStore, storageRoot }));
|
|
7848
|
+
sessionCleanup.register(createContextExcludeCleaner(join14(storageRoot, "..")));
|
|
7849
|
+
const extraFoldersStore = createExtraFoldersStore(join14(storageRoot, "..", "extra-folders"));
|
|
6146
7850
|
new AgentEnhancerService(
|
|
6147
7851
|
ctx,
|
|
6148
7852
|
models,
|
|
6149
|
-
(session, signal) => resolveCompactRouteOverride(ctx, settings(), session, signal),
|
|
6150
7853
|
{
|
|
6151
7854
|
async summarize(range) {
|
|
6152
7855
|
const stored = await usage.listCalls();
|
|
@@ -6160,12 +7863,17 @@ function apply(ctx, config) {
|
|
|
6160
7863
|
return summarizeUsage(mergeUsageCalls(stored, history), range, Date.now());
|
|
6161
7864
|
}
|
|
6162
7865
|
},
|
|
6163
|
-
balancesFacade
|
|
7866
|
+
balancesFacade,
|
|
7867
|
+
sessionCleanup,
|
|
7868
|
+
extraFoldersStore
|
|
6164
7869
|
);
|
|
6165
7870
|
ctx.effect(() => ctx.typert.register(hostContribution));
|
|
6166
|
-
|
|
6167
|
-
|
|
6168
|
-
|
|
7871
|
+
try {
|
|
7872
|
+
installMonacoRoute(ctx);
|
|
7873
|
+
} catch (error) {
|
|
7874
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7875
|
+
ctx.logger.warn(`agent-enhancer: Monaco route install failed: ${detail}`);
|
|
7876
|
+
}
|
|
6169
7877
|
const sessions = new DshCheckpointSessions(ctx);
|
|
6170
7878
|
let rollbackEnabled = config.rollbackEnabled.get();
|
|
6171
7879
|
const captures = new DshCheckpointCaptureSwitch(ctx, { store, pluginVersion: PACKAGE_VERSION });
|