@akanjs/devkit 3.1.0-rc.12 → 3.1.0-rc.13

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.
@@ -25,6 +25,7 @@ import {
25
25
  } from "akanjs/common";
26
26
  import type { Workspace } from "../../commandDecorators";
27
27
  import { AkanCodePlugins } from "../tools/AkanCodePlugins";
28
+ import { DeepSeekWeb } from "../tools/DeepSeekWeb";
28
29
  import { AkanCodeCompaction } from "./AkanCodeCompaction";
29
30
  import { AkanCodeServices } from "./AkanCodeServices";
30
31
  import {
@@ -101,7 +102,8 @@ export class CodeAgent {
101
102
  const agent = new CodeAgent(options.profile);
102
103
  const autoCompactAt = AkanCodeCompaction.normalize(options.autoCompactAt);
103
104
  const settingsManager = AkanCodeServices.settings();
104
- const modelRuntime = await AkanCodeServices.runtime(workspaceRoot, CodeAgentProxy.fromEnv(options.profile));
105
+ const proxy = CodeAgentProxy.fromEnv(options.profile);
106
+ const modelRuntime = await AkanCodeServices.runtime(workspaceRoot, proxy);
105
107
  const model = await akanCodeModel(modelRuntime, options.model);
106
108
  const compaction = settingsManager.getCompactionSettings(model);
107
109
  const modelWarnings = akanCodeModelWarnings(model, compaction.reserveTokens + compaction.keepRecentTokens);
@@ -112,6 +114,7 @@ export class CodeAgent {
112
114
  profile: options.profile,
113
115
  apps: options.apps ?? [],
114
116
  canSeeImages: akanCodeModelSupportsImages(model),
117
+ deepseek: await DeepSeekWeb.fromRuntime(modelRuntime, !!proxy),
115
118
  depth: options.depth ?? 0,
116
119
  ...(options.subagentSpend ? { subagentSpend: options.subagentSpend } : {}),
117
120
  currentSessionId: () => agent.sessionId,
@@ -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) {
@@ -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";
@@ -652,6 +653,128 @@ describe("WebToolPack", () => {
652
653
  server.stop(true);
653
654
  }
654
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
+ });
655
778
  });
656
779
 
657
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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/devkit",
3
- "version": "3.1.0-rc.12",
3
+ "version": "3.1.0-rc.13",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -48,7 +48,7 @@
48
48
  "@earendil-works/pi-tui": "1.3.0",
49
49
  "@inquirer/prompts": "^8.7.3",
50
50
  "@tailwindcss/node": "^4.3.3",
51
- "akanjs": "3.1.0-rc.12",
51
+ "akanjs": "3.1.0-rc.13",
52
52
  "chalk": "^6.0.1",
53
53
  "commander": "^15.0.0",
54
54
  "dayjs": "^1.11.23",