@akanjs/devkit 3.1.0-rc.2 → 3.1.0-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/abstractDoc.ts +1 -1
  2. package/agentsIndex.test.ts +4 -1
  3. package/akanConfig/akanConfig.ts +2 -2
  4. package/akanContext.ts +4 -4
  5. package/akanMcpContract.ts +1 -1
  6. package/applicationBuildRunner.test.ts +24 -1
  7. package/applicationBuildRunner.ts +11 -1
  8. package/artifact/implicitRootLayout.test.ts +2 -0
  9. package/artifact/implicitRootLayout.ts +1 -0
  10. package/biome.base.json +3 -12
  11. package/biomeBase.ts +1 -1
  12. package/cloud/configLock.ts +2 -1
  13. package/cloud/globalConfig.ts +3 -2
  14. package/codeAgent/agent/AkanCodeCompaction.ts +49 -0
  15. package/codeAgent/agent/AkanCodeServices.ts +2 -3
  16. package/codeAgent/agent/CodeAgent.ts +46 -2
  17. package/codeAgent/agent/CodeAgentProxy.ts +2 -2
  18. package/codeAgent/agent/CodeAgentRpcHost.ts +3 -0
  19. package/codeAgent/agent/akanCodeCompaction.test.ts +36 -0
  20. package/codeAgent/agent/akanCodeModel.ts +1 -1
  21. package/codeAgent/agent/akanContextFiles.test.ts +3 -1
  22. package/codeAgent/agent/codeAgent.test.ts +61 -3
  23. package/codeAgent/agent/codeAgentContract.test.ts +1 -0
  24. package/codeAgent/index.ts +1 -0
  25. package/codeAgent/skills/akan-code/SKILL.md +4 -0
  26. package/codeAgent/skills/akan-store/SKILL.md +2 -1
  27. package/codeAgent/tools/AkanCodePlugins.ts +3 -1
  28. package/codeAgent/tools/DeepSeekWeb.ts +127 -0
  29. package/codeAgent/tools/WebToolPack.ts +128 -29
  30. package/codeAgent/tools/akanCodePlugins.test.ts +143 -16
  31. package/codeAgent/tools/deepSeekWeb.test.ts +113 -0
  32. package/commandDecorators/argMeta.ts +1 -0
  33. package/commandDecorators/command.ts +52 -14
  34. package/commandDecorators/commandBuilder.ts +6 -3
  35. package/commandDecorators/commandDecorators.test.ts +130 -6
  36. package/commandDecorators/helpFormatter.ts +2 -1
  37. package/commandDecorators/targetMeta.ts +10 -0
  38. package/createTunnel.test.ts +14 -0
  39. package/createTunnel.ts +3 -1
  40. package/dependencyScanner.ts +1 -1
  41. package/devkitUtils.test.ts +1 -1
  42. package/executors.test.ts +108 -3
  43. package/executors.ts +30 -13
  44. package/frontendBuild/autoImportSync.test.ts +3 -1
  45. package/frontendBuild/clientEntryDiscovery.ts +1 -1
  46. package/frontendBuild/csrArtifactBuilder.test.ts +30 -0
  47. package/frontendBuild/csrArtifactBuilder.ts +5 -3
  48. package/frontendBuild/csrDevPatcher.test.ts +4 -1
  49. package/frontendBuild/cssBlockParser.test.ts +123 -0
  50. package/frontendBuild/cssBlockParser.ts +132 -0
  51. package/frontendBuild/cssCandidateCache.test.ts +16 -0
  52. package/frontendBuild/cssCandidateCache.ts +8 -6
  53. package/frontendBuild/cssCompiler.test.ts +53 -0
  54. package/frontendBuild/cssCompiler.ts +8 -4
  55. package/frontendBuild/fontOptimizer.ts +2 -1
  56. package/frontendBuild/frontendBuild.test.ts +14 -0
  57. package/frontendBuild/hmrWatcher.ts +1 -1
  58. package/frontendBuild/pagesBundleBuilder.test.ts +98 -0
  59. package/frontendBuild/pagesBundleBuilder.ts +64 -9
  60. package/frontendBuild/routeClientBuilder.ts +1 -1
  61. package/frontendBuild/sourceMtimeIndex.test.ts +1 -1
  62. package/frontendBuild/themeValidator.test.ts +83 -2
  63. package/frontendBuild/themeValidator.ts +37 -15
  64. package/getRelatedCnsts.ts +2 -1
  65. package/integration/devStabilityHarness.ts +5 -3
  66. package/lint/__fixtures__/no-deep-internal-import/bad.tsx +1 -0
  67. package/lint/__fixtures__/no-deep-internal-import/good.tsx +1 -0
  68. package/lint/__fixtures__/no-import-client-functions/bad.tsx +1 -0
  69. package/lint/__fixtures__/no-import-client-functions/good.tsx +1 -0
  70. package/lint/__fixtures__/no-import-client-in-server/bad.tsx +1 -0
  71. package/lint/__fixtures__/no-import-client-in-server/good.tsx +1 -0
  72. package/lint/__fixtures__/no-import-external-library/bad.tsx +1 -0
  73. package/lint/__fixtures__/no-import-external-library/good.tsx +1 -0
  74. package/lint/__fixtures__/no-import-external-library-in-test/bad.tsx +1 -0
  75. package/lint/__fixtures__/no-import-external-library-in-test/good.tsx +1 -0
  76. package/lint/__fixtures__/no-import-server-in-client/bad.tsx +1 -0
  77. package/lint/__fixtures__/no-import-server-in-client/good.tsx +1 -0
  78. package/lint/__fixtures__/no-init-fetch-in-client/store-file/bad.ts +1 -1
  79. package/lint/__fixtures__/no-static-in-object-light-model/bad.ts +2 -2
  80. package/mobile/mobileTarget.ts +1 -2
  81. package/mobile/nativeApp.test.ts +1 -1
  82. package/mobile/nativeConfig.test.ts +3 -0
  83. package/mobile/nativeConfig.ts +3 -2
  84. package/mobile/nativeWebDir.ts +3 -2
  85. package/mobile/updateUploader.test.ts +1 -0
  86. package/mobile/updateUploader.ts +3 -3
  87. package/package.json +20 -20
  88. package/piDependencyPins.test.ts +18 -19
  89. package/platformTest/LinuxDockerTarget.ts +2 -1
  90. package/platformTest/PlatformTestTarget.ts +7 -5
  91. package/platformTest/WindowsSshTarget.ts +2 -1
  92. package/qualityScanner.ts +1 -1
  93. package/recipeScanner.test.ts +9 -3
  94. package/scanInfo.ts +37 -4
  95. package/subspaceClone.ts +5 -4
  96. package/testHelpers.ts +2 -0
  97. package/transforms/barrelImportsPlugin.ts +1 -1
  98. package/transforms/browserDirnamePlugin.test.ts +88 -0
  99. package/transforms/browserDirnamePlugin.ts +25 -0
  100. package/transforms/rscUseClientTransform.ts +2 -2
  101. package/transforms/transforms.test.ts +18 -2
  102. package/transforms/tsconfigPackageResolver.test.ts +9 -0
  103. package/types.ts +1 -0
  104. package/workflow/artifacts.ts +1 -1
  105. package/workflow/rolloutGate.test.ts +5 -1
  106. package/workflow/source.ts +2 -2
@@ -10,6 +10,7 @@ import { TurnFeedback, type TurnFeedbackSource } from "../feedback/TurnFeedback"
10
10
  import { VerifyFeedback } from "../feedback/VerifyFeedback";
11
11
  import { AkanToolPack } from "./AkanToolPack";
12
12
  import { AskToolPack } from "./AskToolPack";
13
+ import type { DeepSeekWeb } from "./DeepSeekWeb";
13
14
  import { MailToolPack } from "./MailToolPack";
14
15
  import { McpToolPack } from "./McpToolPack";
15
16
  import { SessionToolPack } from "./SessionToolPack";
@@ -22,6 +23,7 @@ export interface AkanCodePluginsOptions {
22
23
  /** Apps whose dev server should be watched, and whose page should be probed. */
23
24
  apps: string[];
24
25
  canSeeImages: boolean;
26
+ deepseek?: DeepSeekWeb | undefined;
25
27
  /** How many `task` levels deep the owning agent is. */
26
28
  depth: number;
27
29
  /** The tally of the sub-agent tree the owning agent belongs to, when a `task` opened it. */
@@ -56,7 +58,7 @@ export class AkanCodePlugins {
56
58
  });
57
59
  const dispose = () => mcp?.close();
58
60
  const mcpStatus = mcp?.status ?? [];
59
- const web = new WebToolPack({ profile: options.profile });
61
+ const web = new WebToolPack({ profile: options.profile, deepseek: options.deepseek });
60
62
  const sessions = new SessionToolPack({
61
63
  workspaceRoot: options.workspace.workspaceRoot,
62
64
  cwd: options.cwd,
@@ -0,0 +1,127 @@
1
+ import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
2
+ import { codeAgentClip } from "akanjs/common";
3
+
4
+ interface DeepSeekSearchResult {
5
+ title: string;
6
+ url: string;
7
+ snippet?: string;
8
+ summary?: string;
9
+ date?: string | null;
10
+ }
11
+
12
+ export interface DeepSeekPage {
13
+ title: string;
14
+ content: string;
15
+ }
16
+
17
+ export class DeepSeekWeb {
18
+ static readonly baseUrl = "https://api.deepseek.com/v1/tools";
19
+ static readonly maxQueryChars = 128;
20
+ static readonly summaryChars = 600;
21
+ // Only a successful call is billed, so a retry costs nothing. Search has no fallback and waits out every transient
22
+ // status; fetch waits only on its quota and otherwise falls back to a direct read, which beats DeepSeek's 504.
23
+ static readonly #retryable: { [tool in "search" | "fetch"]: number[] } = {
24
+ search: [408, 429, 500, 502, 503, 504],
25
+ fetch: [429],
26
+ };
27
+ static readonly #attempts = 3;
28
+
29
+ readonly #key: string;
30
+ readonly #baseUrl: string;
31
+
32
+ constructor(key: string, baseUrl = DeepSeekWeb.baseUrl) {
33
+ this.#key = key;
34
+ this.#baseUrl = baseUrl;
35
+ }
36
+
37
+ /** Undefined behind a proxy, where the runtime holds only a placeholder and the real key stays with the host. */
38
+ static async fromRuntime(runtime: ModelRuntime, proxied: boolean) {
39
+ if (proxied) return undefined;
40
+ const key = (await runtime.getAuth("deepseek").catch(() => undefined))?.auth.apiKey;
41
+ return key ? new DeepSeekWeb(key) : undefined;
42
+ }
43
+
44
+ async search(query: string, count: number, signal?: AbortSignal) {
45
+ if ([...query].length > DeepSeekWeb.maxQueryChars)
46
+ throw new Error(`DeepSeek search takes at most ${DeepSeekWeb.maxQueryChars} characters; shorten the query.`);
47
+ const body = await this.#call<{ results?: DeepSeekSearchResult[] }>("search", { query, count }, 20_000, signal);
48
+ const byUrl = new Map<string, DeepSeekSearchResult>();
49
+ for (const result of body.results ?? []) {
50
+ const url = DeepSeekWeb.#cleanUrl(result.url);
51
+ if (!byUrl.has(url)) byUrl.set(url, { ...result, url });
52
+ }
53
+ return [...byUrl.values()]
54
+ .map((result) => {
55
+ const date = result.date ? ` · ${result.date.slice(0, 10)}` : "";
56
+ const summary = DeepSeekWeb.unescape(result.summary || result.snippet || "")
57
+ .replace(/\s+/g, " ")
58
+ .trim();
59
+ return `- ${result.title}\n ${result.url}${date}\n ${codeAgentClip(summary, DeepSeekWeb.summaryChars)}`;
60
+ })
61
+ .join("\n");
62
+ }
63
+
64
+ async fetch(url: string, signal?: AbortSignal): Promise<DeepSeekPage> {
65
+ const page = await this.#call<{ title?: string; content?: string }>("fetch", { url }, 30_000, signal);
66
+ return { title: page.title ?? "", content: DeepSeekWeb.unescape(page.content ?? "") };
67
+ }
68
+
69
+ // Fetch escapes every ASCII punctuation mark, structure included (`\#\# Title`, `foo\(\)`), which costs tokens
70
+ // and garbles code. Undone only when nearly all punctuation is escaped, so a page already clean keeps its `\.`.
71
+ static unescape(markdown: string) {
72
+ const escaped = markdown.match(/\\[!-/:-@[-`{-~]/g)?.length ?? 0;
73
+ const punctuation = markdown.match(/[!-/:-@[-`{-~]/g)?.length ?? 0;
74
+ if (!escaped || escaped < (punctuation - escaped) / 2) return markdown;
75
+ return markdown.replace(/\\([!-/:-@[-`{-~])/g, "$1");
76
+ }
77
+
78
+ async #call<T>(tool: "search" | "fetch", body: object, timeoutMs: number, signal?: AbortSignal): Promise<T> {
79
+ for (let attempt = 1; ; attempt++) {
80
+ const response = await fetch(`${this.#baseUrl}/${tool}`, {
81
+ method: "POST",
82
+ headers: { authorization: `Bearer ${this.#key}`, "content-type": "application/json" },
83
+ body: JSON.stringify(body),
84
+ signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs),
85
+ });
86
+ if (response.ok) return (await response.json()) as T;
87
+ if (!DeepSeekWeb.#retryable[tool].includes(response.status) || attempt >= DeepSeekWeb.#attempts) {
88
+ const error = (await response.json().catch(() => undefined)) as { error?: { message?: string } } | undefined;
89
+ throw new Error(
90
+ `DeepSeek ${tool} answered ${response.status}: ${error?.error?.message ?? response.statusText}`,
91
+ );
92
+ }
93
+ await DeepSeekWeb.#wait(DeepSeekWeb.#delayOf(response, attempt), signal);
94
+ }
95
+ }
96
+
97
+ static #delayOf(response: Response, attempt: number) {
98
+ const retryAfter = Number(response.headers.get("retry-after"));
99
+ return Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter, 5) * 1000 : 1000 * 2 ** (attempt - 1);
100
+ }
101
+
102
+ static #wait(ms: number, signal?: AbortSignal) {
103
+ return new Promise<void>((resolve) => {
104
+ const timer = setTimeout(resolve, ms);
105
+ signal?.addEventListener(
106
+ "abort",
107
+ () => {
108
+ clearTimeout(timer);
109
+ resolve();
110
+ },
111
+ { once: true },
112
+ );
113
+ });
114
+ }
115
+
116
+ /** Drops the tracking parameters and the bare `#1` chunk marker DeepSeek appends to result URLs. */
117
+ static #cleanUrl(url: string) {
118
+ try {
119
+ const parsed = new URL(url);
120
+ for (const name of [...parsed.searchParams.keys()]) if (name.startsWith("utm_")) parsed.searchParams.delete(name);
121
+ if (/^#\d+$/.test(parsed.hash)) parsed.hash = "";
122
+ return parsed.toString();
123
+ } catch {
124
+ return url;
125
+ }
126
+ }
127
+ }
@@ -1,9 +1,13 @@
1
+ import { isIP } from "node:net";
1
2
  import type { ExtensionAPI, InlineExtension } from "@earendil-works/pi-coding-agent";
2
- import { type CodeAgentProfile, codeAgentClip, codeAgentOutputChars } from "akanjs/common";
3
+ import { type CodeAgentProfile, codeAgentClip } from "akanjs/common";
3
4
  import { Type } from "typebox";
5
+ import { DeepSeekWeb } from "./DeepSeekWeb";
4
6
 
5
7
  export interface WebToolPackOptions {
6
8
  profile: CodeAgentProfile;
9
+ /** Serves search after any key-named provider, and reads public pages to Markdown ahead of a direct fetch. */
10
+ deepseek?: DeepSeekWeb | undefined;
7
11
  }
8
12
 
9
13
  interface SearchProvider {
@@ -12,9 +16,27 @@ interface SearchProvider {
12
16
  search(key: string, query: string, count: number): Promise<string>;
13
17
  }
14
18
 
19
+ interface WebSearcher {
20
+ name: string;
21
+ search(query: string, count: number, signal?: AbortSignal): Promise<string>;
22
+ }
23
+
24
+ interface WebPage {
25
+ status: number;
26
+ ok: boolean;
27
+ title: string;
28
+ text: string;
29
+ }
30
+
15
31
  // web_search only with a provider key set: a tool that can never work costs prompt tokens and invites retries.
16
32
  export class WebToolPack {
33
+ static readonly pageChars = 10_000;
34
+ static readonly maxResults = 10;
35
+ static readonly #rememberedPages = 16;
17
36
  readonly #options: WebToolPackOptions;
37
+ // A continuation reads the snapshot its first page came from; a fresh read always refetches, since a local dev
38
+ // page changes between edits.
39
+ readonly #pages = new Map<string, WebPage>();
18
40
 
19
41
  constructor(options: WebToolPackOptions) {
20
42
  this.#options = options;
@@ -52,7 +74,7 @@ export class WebToolPack {
52
74
  names() {
53
75
  const names: string[] = [];
54
76
  if (this.#options.profile.tools.web.fetch) names.push("web_fetch");
55
- if (this.#options.profile.tools.web.search && WebToolPack.#provider()) names.push("web_search");
77
+ if (this.#options.profile.tools.web.search && this.#searcher()) names.push("web_search");
56
78
  return names;
57
79
  }
58
80
 
@@ -63,8 +85,8 @@ export class WebToolPack {
63
85
 
64
86
  #register(pi: ExtensionAPI) {
65
87
  if (this.#options.profile.tools.web.fetch) this.#registerFetch(pi);
66
- const provider = WebToolPack.#provider();
67
- if (this.#options.profile.tools.web.search && provider) this.#registerSearch(pi, provider);
88
+ const searcher = this.#searcher();
89
+ if (this.#options.profile.tools.web.search && searcher) this.#registerSearch(pi, searcher);
68
90
  }
69
91
 
70
92
  #registerFetch(pi: ExtensionAPI) {
@@ -72,48 +94,125 @@ export class WebToolPack {
72
94
  pi.registerTool({
73
95
  name: "web_fetch",
74
96
  label: "Fetch",
75
- description: "Fetch a URL and return its readable text. HTML is stripped to text; the result is truncated.",
97
+ description: `Fetch a URL and return its readable text, ${WebToolPack.pageChars} characters at a time. A longer page ends with the offset that continues it.`,
76
98
  promptSnippet: "web_fetch: fetch a URL and return its readable text",
77
- parameters: Type.Object({ url: Type.String({ description: "Absolute http(s) URL." }) }),
78
- execute: async (_id, params) => {
99
+ parameters: Type.Object({
100
+ url: Type.String({ description: "Absolute http(s) URL." }),
101
+ offset: Type.Optional(Type.Number({ description: "Character offset a previous call named, to read on." })),
102
+ }),
103
+ execute: async (_id, params, signal) => {
79
104
  const refusal = WebToolPack.#refuseHost(params.url, allowHosts);
80
105
  if (refusal) return { content: [{ type: "text", text: refusal }], details: undefined, isError: true };
81
- const response = await fetch(params.url, { signal: AbortSignal.timeout(20_000), redirect: "follow" });
82
- const text = WebToolPack.#readable(await response.text());
106
+ const offset = Math.max(0, Math.floor(params.offset ?? 0));
107
+ const cached = offset ? this.#pages.get(params.url) : undefined;
108
+ const page = cached ?? (await this.#read(params.url, signal));
109
+ if (page.ok) this.#remember(params.url, page);
83
110
  return {
84
- content: [
85
- { type: "text", text: `${response.status} ${params.url}\n\n${codeAgentClip(text, codeAgentOutputChars)}` },
86
- ],
111
+ content: [{ type: "text", text: WebToolPack.#window(params.url, page, offset) }],
87
112
  details: undefined,
88
- isError: !response.ok,
113
+ isError: !page.ok,
89
114
  };
90
115
  },
91
116
  });
92
117
  }
93
118
 
94
- #registerSearch(pi: ExtensionAPI, provider: SearchProvider) {
119
+ #registerSearch(pi: ExtensionAPI, searcher: WebSearcher) {
120
+ const hint =
121
+ searcher.name === "deepseek"
122
+ ? ` Keep the query within ${DeepSeekWeb.maxQueryChars} characters; site:, -site: and quoted phrases work.`
123
+ : "";
95
124
  pi.registerTool({
96
125
  name: "web_search",
97
126
  label: "Search",
98
- description: `Search the web (${provider.name}) and return the top results as title, url and snippet.`,
127
+ description: `Search the web (${searcher.name}) and return the top results as title, url and summary.${hint}`,
99
128
  promptSnippet: "web_search: search the web and return titles, urls and snippets",
100
129
  parameters: Type.Object({
101
130
  query: Type.String(),
102
- count: Type.Optional(Type.Number({ description: "How many results, default 5." })),
131
+ count: Type.Optional(
132
+ Type.Number({ description: `How many results, default 5, at most ${WebToolPack.maxResults}.` }),
133
+ ),
103
134
  }),
104
- execute: async (_id, params) => {
105
- const key = process.env[provider.envKey] ?? "";
106
- const results = await provider.search(key, params.query, params.count ?? 5);
107
- return {
108
- content: [{ type: "text", text: codeAgentClip(results || "(no results)", codeAgentOutputChars) }],
109
- details: undefined,
110
- };
135
+ execute: async (_id, params, signal) => {
136
+ const count = Math.min(Math.max(Math.round(params.count ?? 5), 1), WebToolPack.maxResults);
137
+ try {
138
+ const results = await searcher.search(params.query, count, signal);
139
+ return {
140
+ content: [{ type: "text", text: codeAgentClip(results || "(no results)", WebToolPack.pageChars) }],
141
+ details: undefined,
142
+ };
143
+ } catch (error: unknown) {
144
+ const message = error instanceof Error ? error.message : String(error);
145
+ return { content: [{ type: "text", text: message }], details: undefined, isError: true };
146
+ }
111
147
  },
112
148
  });
113
149
  }
114
150
 
115
- static #provider() {
116
- return WebToolPack.providers.find((provider) => !!process.env[provider.envKey]);
151
+ #searcher(): WebSearcher | undefined {
152
+ const provider = WebToolPack.providers.find((entry) => !!process.env[entry.envKey]);
153
+ if (provider)
154
+ return {
155
+ name: provider.name,
156
+ search: (query, count) => provider.search(process.env[provider.envKey] ?? "", query, count),
157
+ };
158
+ const deepseek = this.#options.deepseek;
159
+ return deepseek
160
+ ? { name: "deepseek", search: (query, count, signal) => deepseek.search(query, count, signal) }
161
+ : undefined;
162
+ }
163
+
164
+ async #read(url: string, signal?: AbortSignal): Promise<WebPage> {
165
+ const deepseek = this.#options.deepseek;
166
+ if (deepseek && !WebToolPack.#isLocal(new URL(url).hostname)) {
167
+ try {
168
+ const page = await deepseek.fetch(url, signal);
169
+ return { status: 200, ok: true, title: page.title, text: page.content };
170
+ } catch (error: unknown) {
171
+ if (signal?.aborted) throw error;
172
+ }
173
+ }
174
+ const response = await fetch(url, {
175
+ signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(20_000)]) : AbortSignal.timeout(20_000),
176
+ redirect: "follow",
177
+ });
178
+ return { status: response.status, ok: response.ok, title: "", text: WebToolPack.#readable(await response.text()) };
179
+ }
180
+
181
+ #remember(url: string, page: WebPage) {
182
+ this.#pages.delete(url);
183
+ this.#pages.set(url, page);
184
+ const oldest = this.#pages.keys().next().value;
185
+ if (this.#pages.size > WebToolPack.#rememberedPages && oldest) this.#pages.delete(oldest);
186
+ }
187
+
188
+ static #window(url: string, page: WebPage, offset: number) {
189
+ const head = [`${page.status} ${url}`, page.title].filter(Boolean).join("\n");
190
+ const total = page.text.length;
191
+ if (offset && offset >= total)
192
+ return `${head}\n\nOffset ${offset} is past the end; the page has ${total} characters.`;
193
+ const end = Math.min(offset + WebToolPack.pageChars, total);
194
+ const rest = end < total ? `\n\n[characters ${offset}–${end} of ${total}; offset ${end} reads on]` : "";
195
+ return `${head}\n\n${page.text.slice(offset, end)}${rest}`;
196
+ }
197
+
198
+ // DeepSeek's servers cannot reach these, and an intranet name has no business leaving the machine.
199
+ static #isLocal(hostname: string) {
200
+ const host = hostname.replace(/^\[|\]$/g, "").toLowerCase();
201
+ const family = isIP(host);
202
+ if (family === 6) return host === "::1" || /^(f[cd]|fe[89ab])/.test(host);
203
+ if (family === 4) {
204
+ const [a = 0, b = 0] = host.split(".").map(Number);
205
+ return (
206
+ a === 0 ||
207
+ a === 10 ||
208
+ a === 127 ||
209
+ (a === 100 && b >= 64 && b <= 127) ||
210
+ (a === 169 && b === 254) ||
211
+ (a === 172 && b >= 16 && b <= 31) ||
212
+ (a === 192 && b === 168)
213
+ );
214
+ }
215
+ return !host.includes(".") || /\.(localhost|local|internal|lan|home\.arpa)$/.test(host);
117
216
  }
118
217
 
119
218
  static #refuseHost(url: string, allowHosts: string[] | undefined) {
@@ -132,14 +231,14 @@ export class WebToolPack {
132
231
  /** Enough to read a documentation page; a full HTML parser is not worth a dependency for that. */
133
232
  static #readable(html: string) {
134
233
  return html
135
- .replace(/<script[\s\S]*?<\/script>/gi, " ")
136
- .replace(/<style[\s\S]*?<\/style>/gi, " ")
137
- .replace(/<[^>]+>/g, " ")
234
+ .replace(/<script[\s\S]*?<\/script[^<>]*>/gi, " ")
235
+ .replace(/<style[\s\S]*?<\/style[^<>]*>/gi, " ")
236
+ .replace(/<[^<>]+>/g, " ")
138
237
  .replace(/&nbsp;/g, " ")
139
- .replace(/&amp;/g, "&")
140
238
  .replace(/&lt;/g, "<")
141
239
  .replace(/&gt;/g, ">")
142
240
  .replace(/&quot;/g, '"')
241
+ .replace(/&amp;/g, "&")
143
242
  .replace(/[ \t]+/g, " ")
144
243
  .replace(/\n{3,}/g, "\n\n")
145
244
  .trim();
@@ -11,6 +11,7 @@ import { AkanCodePlugins } from "./AkanCodePlugins";
11
11
  import { AkanEditScope } from "./AkanEditScope";
12
12
  import { AkanEnvKeys } from "./AkanEnvKeys";
13
13
  import { AkanVerifier } from "./AkanVerifier";
14
+ import { DeepSeekWeb } from "./DeepSeekWeb";
14
15
  import { McpClient } from "./McpClient";
15
16
  import { McpToolPack } from "./McpToolPack";
16
17
  import { SessionToolPack } from "./SessionToolPack";
@@ -247,22 +248,26 @@ describe("PreviewView", () => {
247
248
  expect(typeof PreviewView.available).toBe("boolean");
248
249
  });
249
250
 
250
- test.skipIf(!PreviewView.available)("finds an empty body and a console error on a real page", async () => {
251
- const server = Bun.serve({
252
- port: 0,
253
- fetch: () =>
254
- new Response('<!doctype html><html><body></body><script>console.error("boom")</script></html>', {
255
- headers: { "content-type": "text/html" },
256
- }),
257
- });
258
- try {
259
- const probe = await PreviewView.probe(`http://localhost:${server.port}/`);
260
- expect(probe.pageErrors.join(" ")).toContain("rendered empty");
261
- expect(probe.consoleErrors.join(" ")).toContain("boom");
262
- } finally {
263
- server.stop(true);
264
- }
265
- });
251
+ test.skipIf(!PreviewView.available)(
252
+ "finds an empty body and a console error on a real page",
253
+ async () => {
254
+ const server = Bun.serve({
255
+ port: 0,
256
+ fetch: () =>
257
+ new Response('<!doctype html><html><body></body><script>console.error("boom")</script></html>', {
258
+ headers: { "content-type": "text/html" },
259
+ }),
260
+ });
261
+ try {
262
+ const probe = await PreviewView.probe(`http://localhost:${server.port}/`);
263
+ expect(probe.pageErrors.join(" ")).toContain("rendered empty");
264
+ expect(probe.consoleErrors.join(" ")).toContain("boom");
265
+ } finally {
266
+ server.stop(true);
267
+ }
268
+ },
269
+ 30_000,
270
+ );
266
271
  });
267
272
 
268
273
  describe("SubagentPool", () => {
@@ -648,6 +653,128 @@ describe("WebToolPack", () => {
648
653
  server.stop(true);
649
654
  }
650
655
  });
656
+
657
+ test("reads a long page a window at a time, continuing from the snapshot it first read", async () => {
658
+ let hits = 0;
659
+ const server = Bun.serve({
660
+ port: 0,
661
+ fetch: () => {
662
+ hits += 1;
663
+ return new Response(`${"a".repeat(10_000)}${"b".repeat(10_000)}${"c".repeat(500)}`);
664
+ },
665
+ });
666
+ try {
667
+ const fetchTool = toolsOf(new WebToolPack({ profile: codeAgentPresets.local(root) }).extension()).get(
668
+ "web_fetch",
669
+ );
670
+ const url = `http://localhost:${server.port}/long`;
671
+ const read = async (offset?: number) =>
672
+ (await fetchTool?.("1", { url, ...(offset === undefined ? {} : { offset }) } as never))?.content[0]?.text ?? "";
673
+
674
+ const first = await read();
675
+ expect(first).toContain(`${"a".repeat(10_000)}\n\n[characters 0–10000 of 20500; offset 10000 reads on]`);
676
+ const second = await read(10_000);
677
+ expect(second).toContain(`\n\n${"b".repeat(10_000)}\n\n[characters 10000–20000 of 20500; offset 20000 reads on]`);
678
+ expect(await read(20_000)).toEndWith(`\n\n${"c".repeat(500)}`);
679
+ expect(await read(30_000)).toContain("Offset 30000 is past the end; the page has 20500 characters.");
680
+ expect(hits).toBe(1);
681
+ await read();
682
+ expect(hits).toBe(2);
683
+ } finally {
684
+ server.stop(true);
685
+ }
686
+ });
687
+
688
+ test("reads a public page through DeepSeek, a local one directly, and a refused one directly too", async () => {
689
+ const asked: string[] = [];
690
+ const deepseekServer = Bun.serve({
691
+ port: 0,
692
+ fetch: async (request) => {
693
+ const { url } = (await request.json()) as { url: string };
694
+ asked.push(url);
695
+ if (url.endsWith("/refused"))
696
+ return Response.json({ error: { message: "rejected by security policy" } }, { status: 403 });
697
+ return Response.json({ title: "Install", content: "\\#\\# Install\n\nRun \\`bun add akanjs\\`\\." });
698
+ },
699
+ });
700
+ const page = Bun.serve({ port: 0, fetch: () => new Response("<h1>local dev page</h1>") });
701
+ const realFetch = globalThis.fetch;
702
+ const direct = spyOn(globalThis, "fetch").mockImplementation((async (
703
+ input: RequestInfo | URL,
704
+ init?: RequestInit,
705
+ ) => {
706
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
707
+ if (new URL(url).hostname === "docs.example.com") return new Response("<p>read directly</p>");
708
+ return await realFetch(input, init);
709
+ }) as typeof fetch);
710
+ try {
711
+ const deepseek = new DeepSeekWeb("sk-test", `http://127.0.0.1:${deepseekServer.port}/v1/tools`);
712
+ const fetchTool = toolsOf(new WebToolPack({ profile: codeAgentPresets.local(root), deepseek }).extension()).get(
713
+ "web_fetch",
714
+ );
715
+ const read = async (url: string) => (await fetchTool?.("1", { url } as never))?.content[0]?.text ?? "";
716
+
717
+ expect(await read("https://docs.example.com/install")).toBe(
718
+ "200 https://docs.example.com/install\nInstall\n\n## Install\n\nRun `bun add akanjs`.",
719
+ );
720
+ expect(await read(`http://localhost:${page.port}/`)).toContain("local dev page");
721
+ expect(asked).toEqual(["https://docs.example.com/install"]);
722
+ expect(await read("https://docs.example.com/refused")).toContain("read directly");
723
+ expect(asked.at(-1)).toBe("https://docs.example.com/refused");
724
+ } finally {
725
+ direct.mockRestore();
726
+ deepseekServer.stop(true);
727
+ page.stop(true);
728
+ }
729
+ });
730
+
731
+ test("searches through DeepSeek when no key-named provider is set, and a key-named one wins", async () => {
732
+ const had = { brave: process.env.BRAVE_API_KEY, tavily: process.env.TAVILY_API_KEY };
733
+ const counts: number[] = [];
734
+ const deepseekServer = Bun.serve({
735
+ port: 0,
736
+ fetch: async (request) => {
737
+ counts.push(((await request.json()) as { count: number }).count);
738
+ return Response.json({
739
+ results: [{ title: "Akan.js", url: "https://akanjs.com/?utm_source=x", summary: "s" }],
740
+ });
741
+ },
742
+ });
743
+ const descriptionsOf = (pack: WebToolPack) => {
744
+ const descriptions = new Map<string, string>();
745
+ (pack.extension() as { factory: (pi: unknown) => void }).factory({
746
+ registerTool: (tool: { name: string; description: string }) => descriptions.set(tool.name, tool.description),
747
+ });
748
+ return descriptions;
749
+ };
750
+ try {
751
+ delete process.env.BRAVE_API_KEY;
752
+ delete process.env.TAVILY_API_KEY;
753
+ const deepseek = new DeepSeekWeb("sk-test", `http://127.0.0.1:${deepseekServer.port}/v1/tools`);
754
+ const pack = new WebToolPack({ profile: codeAgentPresets.local(root), deepseek });
755
+ expect(new WebToolPack({ profile: codeAgentPresets.review(root), deepseek }).names()).toEqual([]);
756
+ expect(descriptionsOf(pack).get("web_search")).toContain("(deepseek)");
757
+
758
+ const search = toolsOf(pack.extension()).get("web_search");
759
+ const found = await search?.("1", { query: "akan", count: 50 } as never);
760
+ expect(found?.content[0]?.text).toBe("- Akan.js\n https://akanjs.com/\n s");
761
+ expect(counts).toEqual([WebToolPack.maxResults]);
762
+ const tooLong = await search?.("1", { query: "x".repeat(DeepSeekWeb.maxQueryChars + 1) } as never);
763
+ expect(tooLong?.isError).toBe(true);
764
+ expect(counts).toHaveLength(1);
765
+
766
+ process.env.BRAVE_API_KEY = "test";
767
+ expect(descriptionsOf(pack).get("web_search")).toContain("(brave)");
768
+ } finally {
769
+ for (const [name, value] of [
770
+ ["BRAVE_API_KEY", had.brave],
771
+ ["TAVILY_API_KEY", had.tavily],
772
+ ] as const)
773
+ if (value === undefined) delete process.env[name];
774
+ else process.env[name] = value;
775
+ deepseekServer.stop(true);
776
+ }
777
+ });
651
778
  });
652
779
 
653
780
  describe("SessionToolPack", () => {
@@ -0,0 +1,113 @@
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { DeepSeekWeb } from "./DeepSeekWeb";
3
+
4
+ interface Received {
5
+ path: string;
6
+ body: { query?: string; count?: number; url?: string };
7
+ auth: string | null;
8
+ }
9
+
10
+ const received: Received[] = [];
11
+ let answers: Response[] = [];
12
+ let server: ReturnType<typeof Bun.serve>;
13
+ beforeAll(() => {
14
+ server = Bun.serve({
15
+ port: 0,
16
+ fetch: async (request) => {
17
+ received.push({
18
+ path: new URL(request.url).pathname,
19
+ body: (await request.json()) as Received["body"],
20
+ auth: request.headers.get("authorization"),
21
+ });
22
+ return answers.shift() ?? Response.json({ results: [] });
23
+ },
24
+ });
25
+ });
26
+ afterAll(() => server.stop(true));
27
+
28
+ const client = () => new DeepSeekWeb("sk-test", `http://127.0.0.1:${server.port}/v1/tools`);
29
+ const rateLimited = () =>
30
+ Response.json({ error: { message: "QPS exceeded" } }, { status: 429, headers: { "retry-after": "0.01" } });
31
+
32
+ describe("DeepSeekWeb", () => {
33
+ test("search lists each page once, without tracking parameters, with its date and a clipped summary", async () => {
34
+ answers = [
35
+ Response.json({
36
+ results: [
37
+ {
38
+ title: "Bun v1.3.13",
39
+ url: "https://bun.com/blog/bun-v1.3.13?utm_source=x&utm_medium=y&ref=keep",
40
+ summary: "word ".repeat(400),
41
+ date: "2026-04-19T16:00:00+00:00",
42
+ },
43
+ { title: "Isolate issue", url: "https://github.com/oven-sh/bun/issues/31771#1", snippet: "only a snippet" },
44
+ { title: "Same issue again", url: "https://github.com/oven-sh/bun/issues/31771", summary: "dup" },
45
+ ],
46
+ }),
47
+ ];
48
+ const text = await client().search("bun isolate", 5);
49
+ expect(received.at(-1)).toEqual({
50
+ path: "/v1/tools/search",
51
+ body: { query: "bun isolate", count: 5 },
52
+ auth: "Bearer sk-test",
53
+ });
54
+ expect(text).toContain("- Bun v1.3.13\n https://bun.com/blog/bun-v1.3.13?ref=keep · 2026-04-19\n word word");
55
+ expect(text).toContain("- Isolate issue\n https://github.com/oven-sh/bun/issues/31771\n only a snippet");
56
+ expect(text).not.toContain("Same issue again");
57
+ expect(text.split("\n")[2]?.trim().length).toBe(DeepSeekWeb.summaryChars);
58
+ });
59
+
60
+ test("a query longer than DeepSeek takes is refused before any request is spent", async () => {
61
+ const before = received.length;
62
+ await expect(client().search("x".repeat(DeepSeekWeb.maxQueryChars + 1), 5)).rejects.toThrow("shorten the query");
63
+ expect(received.length).toBe(before);
64
+ });
65
+
66
+ test("search waits out a rate limit, and gives up with DeepSeek's own message after the last attempt", async () => {
67
+ answers = [rateLimited(), Response.json({ results: [{ title: "t", url: "https://a.com/", summary: "s" }] })];
68
+ expect(await client().search("q", 1)).toContain("https://a.com/");
69
+
70
+ answers = [rateLimited(), rateLimited(), rateLimited()];
71
+ await expect(client().search("q", 1)).rejects.toThrow("429: QPS exceeded");
72
+ });
73
+
74
+ test("fetch waits only on its quota and hands any other failure straight back", async () => {
75
+ answers = [rateLimited(), Response.json({ title: "Page", content: "body" })];
76
+ expect(await client().fetch("https://a.com/")).toEqual({ title: "Page", content: "body" });
77
+
78
+ const before = received.length;
79
+ answers = [Response.json({ error: { message: "downstream timeout" } }, { status: 504 })];
80
+ await expect(client().fetch("https://a.com/")).rejects.toThrow("504: downstream timeout");
81
+ expect(received.length).toBe(before + 1);
82
+ });
83
+
84
+ test("fetch hands back Markdown with its blanket escaping undone", async () => {
85
+ answers = [
86
+ Response.json({
87
+ title: "Example",
88
+ content: "\\#\\# Usage\n\nCall \\`foo\\(1\\)\\` \\- it returns 2\\.\n\nconst re \\= \\/\\\\s\\+\\/\\;",
89
+ }),
90
+ ];
91
+ const page = await client().fetch("https://example.com/usage");
92
+ expect(received.at(-1)?.body).toEqual({ url: "https://example.com/usage" });
93
+ expect(page).toEqual({
94
+ title: "Example",
95
+ content: "## Usage\n\nCall `foo(1)` - it returns 2.\n\nconst re = /\\s+/;",
96
+ });
97
+ });
98
+
99
+ test("a page that was never escaped keeps the backslashes it has", () => {
100
+ const clean = "Match a dot with `a\\.b`. See (the docs), [a link](x) and {a: 1}; done!";
101
+ expect(DeepSeekWeb.unescape(clean)).toBe(clean);
102
+ });
103
+
104
+ test("the key comes from the model runtime, and never from behind a proxy", async () => {
105
+ const runtime = (apiKey?: string) =>
106
+ ({ getAuth: async () => (apiKey ? { auth: { apiKey } } : undefined) }) as unknown as Parameters<
107
+ typeof DeepSeekWeb.fromRuntime
108
+ >[0];
109
+ expect(await DeepSeekWeb.fromRuntime(runtime("sk"), false)).toBeInstanceOf(DeepSeekWeb);
110
+ expect(await DeepSeekWeb.fromRuntime(runtime("sk"), true)).toBeUndefined();
111
+ expect(await DeepSeekWeb.fromRuntime(runtime(), false)).toBeUndefined();
112
+ });
113
+ });