@selesai/code 0.1.19 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/architect.md +4 -3
- package/dist/agents/builder.md +5 -4
- package/dist/agents/commentator.md +3 -2
- package/dist/agents/explorer.md +4 -4
- package/dist/agents/recapper.md +4 -3
- package/dist/config.d.ts +6 -7
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +7 -9
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +3 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +18 -24
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +2 -0
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/types.d.ts +8 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +25 -11
- package/dist/core/skills.js.map +1 -1
- package/dist/defaults/models.json +143 -39
- package/dist/defaults/settings.json +2 -29
- package/dist/extensions/package.json +5 -4
- package/dist/extensions/pi-intercom/index.ts +12 -5
- package/dist/extensions/pi-powerline-footer/index.ts +4 -1
- package/dist/extensions/pi-powerline-footer/tps.ts +286 -0
- package/dist/extensions/pi-powerline-footer/welcome.ts +47 -46
- package/dist/extensions/pi-subagents/README.md +12 -5
- package/dist/extensions/pi-subagents/agents/context-builder.md +2 -2
- package/dist/extensions/pi-subagents/agents/researcher.md +4 -5
- package/dist/extensions/pi-subagents/package.json +8 -5
- package/dist/extensions/pi-subagents/prompts/parallel-context-build.md +1 -1
- package/dist/extensions/pi-subagents/prompts/parallel-handoff-plan.md +1 -1
- package/dist/extensions/pi-subagents/src/agents/agent-management.ts +1 -1
- package/dist/extensions/pi-subagents/src/agents/agents.ts +3 -3
- package/dist/extensions/pi-subagents/src/extension/control-notices.ts +1 -1
- package/dist/extensions/pi-subagents/src/extension/doctor.ts +3 -3
- package/dist/extensions/pi-subagents/src/extension/fanout-child.ts +1 -1
- package/dist/extensions/pi-subagents/src/extension/index.ts +29 -2
- package/dist/extensions/pi-subagents/src/profiles/profiles.ts +2 -2
- package/dist/extensions/pi-subagents/src/runs/background/async-execution.ts +72 -13
- package/dist/extensions/pi-subagents/src/runs/background/async-job-tracker.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/background/notify.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/background/subagent-runner.ts +85 -41
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-clarify.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-execution.ts +1 -1
- package/dist/extensions/pi-subagents/src/runs/foreground/execution.ts +46 -3
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +2 -2
- package/dist/extensions/pi-subagents/src/runs/shared/completion-guard.ts +1 -3
- package/dist/extensions/pi-subagents/src/runs/shared/nested-events.ts +2 -2
- package/dist/extensions/pi-subagents/src/runs/shared/pi-args.ts +18 -18
- package/dist/extensions/pi-subagents/src/runs/shared/pi-spawn.ts +24 -24
- package/dist/extensions/pi-subagents/src/runs/shared/spawn-timing.ts +46 -0
- package/dist/extensions/pi-subagents/src/runs/shared/structured-output.ts +2 -2
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-prompt-runtime.ts +4 -4
- package/dist/extensions/pi-subagents/src/shared/fork-context.ts +1 -1
- package/dist/extensions/pi-subagents/src/shared/types.ts +6 -6
- package/dist/extensions/pi-subagents/src/shared/utils.ts +3 -3
- package/dist/extensions/pi-subagents/src/slash/slash-bridge.ts +1 -1
- package/dist/extensions/pi-subagents/src/slash/slash-commands.ts +1 -1
- package/dist/extensions/pi-subagents/src/tui/render-helpers.ts +1 -1
- package/dist/extensions/pi-subagents/src/tui/render.ts +1 -1
- package/dist/extensions/pi-subagents/test/unit/spawn-timing.test.ts +45 -0
- package/dist/extensions/pi-subagents/tsconfig.runner.json +17 -0
- package/dist/extensions/pi-web-agent/package.json +31 -0
- package/dist/extensions/pi-web-agent/src/backends/config.ts +205 -0
- package/dist/extensions/pi-web-agent/src/backends/doctor.ts +136 -0
- package/dist/extensions/pi-web-agent/src/backends/factory.ts +152 -0
- package/dist/extensions/pi-web-agent/src/backends/settings-reader.ts +25 -0
- package/dist/extensions/pi-web-agent/src/cache/ttl-cache.ts +28 -0
- package/dist/extensions/pi-web-agent/src/changelog-notice.ts +133 -0
- package/dist/extensions/pi-web-agent/src/commands/web-agent-config.ts +946 -0
- package/dist/extensions/pi-web-agent/src/extension.ts +126 -0
- package/dist/extensions/pi-web-agent/src/extract/readability.ts +118 -0
- package/dist/extensions/pi-web-agent/src/fetch/browser-resolution.ts +199 -0
- package/dist/extensions/pi-web-agent/src/fetch/firecrawl-fetch.ts +100 -0
- package/dist/extensions/pi-web-agent/src/fetch/headless-fetch.ts +117 -0
- package/dist/extensions/pi-web-agent/src/fetch/http-fetch.ts +67 -0
- package/dist/extensions/pi-web-agent/src/orchestration/answer-synthesizer.ts +60 -0
- package/dist/extensions/pi-web-agent/src/orchestration/candidate-selector.ts +50 -0
- package/dist/extensions/pi-web-agent/src/orchestration/direct-url.ts +52 -0
- package/dist/extensions/pi-web-agent/src/orchestration/evidence-quality.ts +105 -0
- package/dist/extensions/pi-web-agent/src/orchestration/evidence-ranker.ts +45 -0
- package/dist/extensions/pi-web-agent/src/orchestration/index.ts +28 -0
- package/dist/extensions/pi-web-agent/src/orchestration/query-planner.ts +47 -0
- package/dist/extensions/pi-web-agent/src/orchestration/research-orchestrator.ts +376 -0
- package/dist/extensions/pi-web-agent/src/orchestration/research-types.ts +64 -0
- package/dist/extensions/pi-web-agent/src/orchestration/research-worker.ts +181 -0
- package/dist/extensions/pi-web-agent/src/orchestration/source-profile.ts +101 -0
- package/dist/extensions/pi-web-agent/src/orchestration/stop-decider.ts +81 -0
- package/dist/extensions/pi-web-agent/src/presentation/config-store.ts +208 -0
- package/dist/extensions/pi-web-agent/src/presentation/config.ts +75 -0
- package/dist/extensions/pi-web-agent/src/presentation/explore-presentation.ts +61 -0
- package/dist/extensions/pi-web-agent/src/presentation/fetch-presentation.ts +54 -0
- package/dist/extensions/pi-web-agent/src/presentation/search-presentation.ts +41 -0
- package/dist/extensions/pi-web-agent/src/presentation/select-view.ts +20 -0
- package/dist/extensions/pi-web-agent/src/presentation/types.ts +63 -0
- package/dist/extensions/pi-web-agent/src/search/brave.ts +114 -0
- package/dist/extensions/pi-web-agent/src/search/duckduckgo.ts +72 -0
- package/dist/extensions/pi-web-agent/src/search/searxng.ts +96 -0
- package/dist/extensions/pi-web-agent/src/tools/web-explore.ts +71 -0
- package/dist/extensions/pi-web-agent/src/tools/web-fetch-headless.ts +31 -0
- package/dist/extensions/pi-web-agent/src/tools/web-fetch.ts +31 -0
- package/dist/extensions/pi-web-agent/src/tools/web-search.ts +157 -0
- package/dist/extensions/pi-web-agent/src/types.ts +88 -0
- package/dist/extensions/ponytail/index.js +189 -0
- package/dist/extensions/ponytail/package.json +8 -0
- package/dist/extensions/ponytail/ponytail-config.cjs +122 -0
- package/dist/extensions/ponytail/ponytail-instructions.cjs +94 -0
- package/dist/extensions/ponytail/test/extension.test.js +167 -0
- package/dist/extensions/ponytail/test/helpers.test.js +92 -0
- package/dist/extensions/question/constants.ts +38 -0
- package/dist/extensions/question/dialog-adapter.ts +74 -0
- package/dist/extensions/question/helpers.ts +102 -0
- package/dist/extensions/question/index.ts +671 -0
- package/dist/extensions/question/navigation.ts +14 -0
- package/dist/extensions/question/package.json +18 -0
- package/dist/extensions/question/question-list.ts +498 -0
- package/dist/extensions/question/row-layout.ts +162 -0
- package/dist/extensions/question/schemas.ts +69 -0
- package/dist/extensions/question/selection-mode.ts +49 -0
- package/dist/extensions/question/shortcuts.ts +53 -0
- package/dist/extensions/question/tests/helpers.test.ts +178 -0
- package/dist/extensions/question/tests/question-list.test.ts +230 -0
- package/dist/extensions/question/tests/row-layout.test.ts +136 -0
- package/dist/extensions/question/tests/shortcuts.test.ts +85 -0
- package/dist/extensions/question/tests/ui-protocol.test.ts +143 -0
- package/dist/extensions/question/tui-adapter.ts +33 -0
- package/dist/extensions/question/types.ts +102 -0
- package/dist/extensions/question/ui-protocol.ts +46 -0
- package/dist/extensions/tokenin-onboarding.ts +265 -2
- package/dist/extensions/web-agent-onboarding.ts +222 -0
- package/dist/extensions/workflow/adapter.ts +588 -0
- package/dist/extensions/workflow/extension.ts +24 -0
- package/dist/extensions/workflow/modes/prototype.ts +156 -0
- package/dist/extensions/workflow/modes/quick.ts +138 -0
- package/dist/extensions/workflow/package.json +17 -0
- package/dist/extensions/workflow/state-machine.ts +386 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +2 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +57 -13
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/utils/mime.d.ts.map +1 -1
- package/dist/utils/mime.js +24 -1
- package/dist/utils/mime.js.map +1 -1
- package/docs/docs.json +4 -0
- package/docs/workflows.md +206 -0
- package/package.json +5 -1
- package/README.md +0 -198
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const PRESENTATION_MODES = ['compact', 'preview', 'verbose'] as const;
|
|
2
|
+
export type PresentationMode = (typeof PRESENTATION_MODES)[number];
|
|
3
|
+
|
|
4
|
+
export type PresentationViews = {
|
|
5
|
+
compact: string;
|
|
6
|
+
preview?: string;
|
|
7
|
+
verbose?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type PresentationMetrics = {
|
|
11
|
+
durationMs?: number;
|
|
12
|
+
resultCount?: number;
|
|
13
|
+
sourceCount?: number;
|
|
14
|
+
wordCount?: number;
|
|
15
|
+
statusCode?: number;
|
|
16
|
+
cacheHit?: boolean;
|
|
17
|
+
truncated?: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type PresentationSource = {
|
|
21
|
+
title: string;
|
|
22
|
+
url: string;
|
|
23
|
+
domain?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type PresentationEnvelope = {
|
|
27
|
+
mode: PresentationMode;
|
|
28
|
+
views: PresentationViews;
|
|
29
|
+
metrics?: PresentationMetrics;
|
|
30
|
+
sources?: PresentationSource[];
|
|
31
|
+
debug?: Record<string, unknown>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type PresentationToolName =
|
|
35
|
+
| 'web_search'
|
|
36
|
+
| 'web_fetch'
|
|
37
|
+
| 'web_fetch_headless'
|
|
38
|
+
| 'web_explore';
|
|
39
|
+
|
|
40
|
+
export type PresentationScope = 'global' | 'project';
|
|
41
|
+
|
|
42
|
+
export type PresentationToolOverrideMode = PresentationMode;
|
|
43
|
+
|
|
44
|
+
export type PresentationToolConfig = {
|
|
45
|
+
mode: PresentationToolOverrideMode;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type PresentationConfig = {
|
|
49
|
+
defaultMode: PresentationMode;
|
|
50
|
+
tools: Partial<Record<PresentationToolName, PresentationToolConfig>>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type PresentationConfigOverride = {
|
|
54
|
+
defaultMode?: PresentationMode;
|
|
55
|
+
tools: Partial<Record<PresentationToolName, PresentationToolConfig>>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type PresentationConfigFile = {
|
|
59
|
+
presentation?: {
|
|
60
|
+
defaultMode?: unknown;
|
|
61
|
+
tools?: Partial<Record<PresentationToolName, { mode?: unknown }>>;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { buildSearchPresentation } from '../presentation/search-presentation.js';
|
|
2
|
+
import type { SearchResult, WebSearchResponse } from '../types.js';
|
|
3
|
+
|
|
4
|
+
type BraveWebResult = {
|
|
5
|
+
title?: unknown;
|
|
6
|
+
url?: unknown;
|
|
7
|
+
description?: unknown;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type BraveSearchResponse = {
|
|
11
|
+
web?: {
|
|
12
|
+
results?: BraveWebResult[];
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const BRAVE_WEB_SEARCH_URL = 'https://api.search.brave.com/res/v1/web/search';
|
|
17
|
+
|
|
18
|
+
function resultWithPresentation(result: WebSearchResponse): WebSearchResponse {
|
|
19
|
+
return { ...result, presentation: buildSearchPresentation(result) };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function normalizeResults(response: BraveSearchResponse): SearchResult[] {
|
|
23
|
+
return (response.web?.results ?? []).flatMap((item) => {
|
|
24
|
+
if (typeof item.title !== 'string' || typeof item.url !== 'string') {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
title: item.title,
|
|
31
|
+
url: item.url,
|
|
32
|
+
snippet: typeof item.description === 'string' ? item.description : ''
|
|
33
|
+
}
|
|
34
|
+
];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function buildBraveUrl(query: string) {
|
|
39
|
+
const url = new URL(BRAVE_WEB_SEARCH_URL);
|
|
40
|
+
url.searchParams.set('q', query);
|
|
41
|
+
return url.toString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createBraveSearchTool({
|
|
45
|
+
apiKey,
|
|
46
|
+
fetchImpl = fetch
|
|
47
|
+
}: {
|
|
48
|
+
apiKey?: string;
|
|
49
|
+
fetchImpl?: typeof fetch;
|
|
50
|
+
}) {
|
|
51
|
+
return async function braveSearch({ query }: { query: string }): Promise<WebSearchResponse> {
|
|
52
|
+
const normalizedQuery = query.trim();
|
|
53
|
+
|
|
54
|
+
if (!normalizedQuery) {
|
|
55
|
+
return resultWithPresentation({
|
|
56
|
+
status: 'error',
|
|
57
|
+
results: [],
|
|
58
|
+
metadata: { backend: 'brave', cacheHit: false },
|
|
59
|
+
error: { code: 'INVALID_QUERY', message: 'Query must not be empty.' }
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!apiKey?.trim()) {
|
|
64
|
+
return resultWithPresentation({
|
|
65
|
+
status: 'error',
|
|
66
|
+
results: [],
|
|
67
|
+
metadata: { backend: 'brave', cacheHit: false },
|
|
68
|
+
error: {
|
|
69
|
+
code: 'BACKEND_CONFIG_INVALID',
|
|
70
|
+
message: 'Brave search requires PI_WEB_AGENT_BRAVE_API_KEY.'
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetchImpl(buildBraveUrl(normalizedQuery), {
|
|
77
|
+
headers: {
|
|
78
|
+
Accept: 'application/json',
|
|
79
|
+
'X-Subscription-Token': apiKey
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
throw new Error(`HTTP ${response.status}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const parsed = (await response.json()) as BraveSearchResponse;
|
|
88
|
+
const results = normalizeResults(parsed);
|
|
89
|
+
|
|
90
|
+
if (results.length === 0) {
|
|
91
|
+
return resultWithPresentation({
|
|
92
|
+
status: 'error',
|
|
93
|
+
results: [],
|
|
94
|
+
metadata: { backend: 'brave', cacheHit: false },
|
|
95
|
+
error: { code: 'NO_RESULTS', message: 'Brave returned no usable results for this query.' }
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return resultWithPresentation({
|
|
100
|
+
status: 'ok',
|
|
101
|
+
results,
|
|
102
|
+
metadata: { backend: 'brave', cacheHit: false }
|
|
103
|
+
});
|
|
104
|
+
} catch (error) {
|
|
105
|
+
const rawMessage = error instanceof Error ? error.message : String(error);
|
|
106
|
+
return resultWithPresentation({
|
|
107
|
+
status: 'error',
|
|
108
|
+
results: [],
|
|
109
|
+
metadata: { backend: 'brave', cacheHit: false },
|
|
110
|
+
error: { code: 'FETCH_FAILED', message: `Brave search request failed: ${rawMessage}` }
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as cheerio from 'cheerio';
|
|
2
|
+
import type { SearchResult } from '../types.js';
|
|
3
|
+
|
|
4
|
+
export type ParsedDuckDuckGoResults = {
|
|
5
|
+
results: SearchResult[];
|
|
6
|
+
noResults: boolean;
|
|
7
|
+
hasResultContainers: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function normalizeDuckDuckGoUrl(rawUrl: string): string {
|
|
11
|
+
try {
|
|
12
|
+
const absolute = rawUrl.startsWith('//') ? `https:${rawUrl}` : rawUrl;
|
|
13
|
+
const parsed = new URL(absolute);
|
|
14
|
+
const isDuckDuckGoRedirect = parsed.hostname === 'duckduckgo.com' && parsed.pathname === '/l/';
|
|
15
|
+
|
|
16
|
+
if (!isDuckDuckGoRedirect) {
|
|
17
|
+
return rawUrl;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const target = parsed.searchParams.get('uddg');
|
|
21
|
+
if (!target) {
|
|
22
|
+
return rawUrl;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return decodeURIComponent(target);
|
|
26
|
+
} catch {
|
|
27
|
+
return rawUrl;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function buildSearchUrl(query: string): string {
|
|
32
|
+
const params = new URLSearchParams({ q: query });
|
|
33
|
+
return `https://html.duckduckgo.com/html/?${params.toString()}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function fetchDuckDuckGoHtml(query: string): Promise<string> {
|
|
37
|
+
const response = await fetch(buildSearchUrl(query));
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
throw new Error(`DuckDuckGo request failed with ${response.status}`);
|
|
40
|
+
}
|
|
41
|
+
return response.text();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function parseDuckDuckGoResults(html: string): ParsedDuckDuckGoResults {
|
|
45
|
+
const $ = cheerio.load(html);
|
|
46
|
+
const resultContainers = $('.result');
|
|
47
|
+
|
|
48
|
+
const results = resultContainers
|
|
49
|
+
.map((_, element) => {
|
|
50
|
+
const title = $(element).find('.result__a').first().text().trim();
|
|
51
|
+
const url = normalizeDuckDuckGoUrl(
|
|
52
|
+
$(element).find('.result__a').first().attr('href')?.trim() ?? ''
|
|
53
|
+
);
|
|
54
|
+
const snippet = $(element).find('.result__snippet').first().text().trim();
|
|
55
|
+
|
|
56
|
+
return title && url ? { title, url, snippet } : null;
|
|
57
|
+
})
|
|
58
|
+
.get()
|
|
59
|
+
.filter((value): value is SearchResult => value !== null);
|
|
60
|
+
|
|
61
|
+
const text = $.text().toLowerCase();
|
|
62
|
+
const noResults =
|
|
63
|
+
text.includes('no results found') ||
|
|
64
|
+
text.includes('no more results') ||
|
|
65
|
+
text.includes('did not match any documents');
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
results,
|
|
69
|
+
noResults,
|
|
70
|
+
hasResultContainers: resultContainers.length > 0
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { SearxngOptions } from '../backends/config.js';
|
|
2
|
+
import { buildSearchPresentation } from '../presentation/search-presentation.js';
|
|
3
|
+
import type { SearchResult, WebSearchResponse } from '../types.js';
|
|
4
|
+
|
|
5
|
+
type SearxngResult = {
|
|
6
|
+
title?: unknown;
|
|
7
|
+
url?: unknown;
|
|
8
|
+
content?: unknown;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type SearxngResponse = {
|
|
12
|
+
results?: SearxngResult[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function buildSearchUrl(baseUrl: string, query: string, options: SearxngOptions = {}) {
|
|
16
|
+
const url = new URL('/search', baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`);
|
|
17
|
+
url.searchParams.set('q', query);
|
|
18
|
+
url.searchParams.set('format', 'json');
|
|
19
|
+
if (options.categories?.length) url.searchParams.set('categories', options.categories.join(','));
|
|
20
|
+
if (options.language) url.searchParams.set('language', options.language);
|
|
21
|
+
if (options.safesearch !== undefined) url.searchParams.set('safesearch', String(options.safesearch));
|
|
22
|
+
return url.toString();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function normalizeResults(response: SearxngResponse): SearchResult[] {
|
|
26
|
+
return (response.results ?? []).flatMap((result) => {
|
|
27
|
+
if (typeof result.title !== 'string' || typeof result.url !== 'string') {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [
|
|
32
|
+
{
|
|
33
|
+
title: result.title,
|
|
34
|
+
url: result.url,
|
|
35
|
+
snippet: typeof result.content === 'string' ? result.content : ''
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createSearxngSearchTool({
|
|
42
|
+
baseUrl,
|
|
43
|
+
options,
|
|
44
|
+
fetchImpl = fetch
|
|
45
|
+
}: {
|
|
46
|
+
baseUrl: string;
|
|
47
|
+
options?: SearxngOptions;
|
|
48
|
+
fetchImpl?: typeof fetch;
|
|
49
|
+
}) {
|
|
50
|
+
return async function searxngSearch({ query }: { query: string }): Promise<WebSearchResponse> {
|
|
51
|
+
const normalizedQuery = query.trim();
|
|
52
|
+
|
|
53
|
+
if (!normalizedQuery) {
|
|
54
|
+
const result: WebSearchResponse = {
|
|
55
|
+
status: 'error',
|
|
56
|
+
results: [],
|
|
57
|
+
metadata: { backend: 'searxng', cacheHit: false },
|
|
58
|
+
error: { code: 'INVALID_QUERY', message: 'Query must not be empty.' }
|
|
59
|
+
};
|
|
60
|
+
return { ...result, presentation: buildSearchPresentation(result) };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const response = await fetchImpl(buildSearchUrl(baseUrl, normalizedQuery, options));
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
throw new Error(`HTTP ${response.status}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const parsed = (await response.json()) as SearxngResponse;
|
|
70
|
+
const results = normalizeResults(parsed);
|
|
71
|
+
const result: WebSearchResponse = results.length > 0
|
|
72
|
+
? {
|
|
73
|
+
status: 'ok',
|
|
74
|
+
results,
|
|
75
|
+
metadata: { backend: 'searxng', cacheHit: false }
|
|
76
|
+
}
|
|
77
|
+
: {
|
|
78
|
+
status: 'error',
|
|
79
|
+
results: [],
|
|
80
|
+
metadata: { backend: 'searxng', cacheHit: false },
|
|
81
|
+
error: { code: 'NO_RESULTS', message: 'SearXNG returned no usable results for this query.' }
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return { ...result, presentation: buildSearchPresentation(result) };
|
|
85
|
+
} catch (error) {
|
|
86
|
+
const rawMessage = error instanceof Error ? error.message : String(error);
|
|
87
|
+
const result: WebSearchResponse = {
|
|
88
|
+
status: 'error',
|
|
89
|
+
results: [],
|
|
90
|
+
metadata: { backend: 'searxng', cacheHit: false },
|
|
91
|
+
error: { code: 'FETCH_FAILED', message: `SearXNG search request failed: ${rawMessage}` }
|
|
92
|
+
};
|
|
93
|
+
return { ...result, presentation: buildSearchPresentation(result) };
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { createResearchWorkflow } from '../orchestration/index.js';
|
|
2
|
+
import { synthesizeAnswer } from '../orchestration/answer-synthesizer.js';
|
|
3
|
+
import type { ResearchEvidence } from '../orchestration/research-types.js';
|
|
4
|
+
import { buildExplorePresentation } from '../presentation/explore-presentation.js';
|
|
5
|
+
import type { EvidenceCaveatReason } from '../orchestration/evidence-quality.js';
|
|
6
|
+
import type { WebExploreResponse } from '../types.js';
|
|
7
|
+
|
|
8
|
+
export function createWebExploreTool({
|
|
9
|
+
explore = createResearchWorkflow()
|
|
10
|
+
}: {
|
|
11
|
+
explore?:
|
|
12
|
+
| {
|
|
13
|
+
run: (input: { query: string }) => Promise<{
|
|
14
|
+
decision: { action: 'answer' | 'research-again' | 'escalate-headless' };
|
|
15
|
+
evidence: ResearchEvidence[];
|
|
16
|
+
workerPass: unknown;
|
|
17
|
+
metadata?: WebExploreResponse['metadata'];
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
| ((input: { query: string }) => Promise<{
|
|
21
|
+
decision: { action: 'answer' | 'research-again' | 'escalate-headless' };
|
|
22
|
+
evidence: ResearchEvidence[];
|
|
23
|
+
workerPass: unknown;
|
|
24
|
+
metadata?: WebExploreResponse['metadata'];
|
|
25
|
+
}>);
|
|
26
|
+
} = {}) {
|
|
27
|
+
const runExplore = typeof explore === 'function' ? explore : explore.run.bind(explore);
|
|
28
|
+
|
|
29
|
+
return async function webExplore({ query }: { query: string }) {
|
|
30
|
+
const normalizedQuery = query.trim();
|
|
31
|
+
|
|
32
|
+
if (!normalizedQuery) {
|
|
33
|
+
const result: WebExploreResponse = {
|
|
34
|
+
status: 'error',
|
|
35
|
+
findings: [],
|
|
36
|
+
sources: [],
|
|
37
|
+
error: { code: 'INVALID_QUERY', message: 'Query must not be empty.' }
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
...result,
|
|
42
|
+
presentation: buildExplorePresentation(result)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const result = await runExplore({ query: normalizedQuery });
|
|
47
|
+
const sources = result.evidence.slice(0, 4).map((item) => ({
|
|
48
|
+
title: item.title,
|
|
49
|
+
url: item.url,
|
|
50
|
+
method: item.method
|
|
51
|
+
}));
|
|
52
|
+
const synthesized = synthesizeAnswer({
|
|
53
|
+
evidence: result.evidence,
|
|
54
|
+
partial: result.decision.action !== 'answer',
|
|
55
|
+
caveatReasons: result.metadata?.caveatReasons as EvidenceCaveatReason[] | undefined
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const shaped: WebExploreResponse = {
|
|
59
|
+
status: 'ok',
|
|
60
|
+
findings: synthesized.findings,
|
|
61
|
+
sources,
|
|
62
|
+
caveat: synthesized.caveat,
|
|
63
|
+
metadata: result.metadata
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
...shaped,
|
|
68
|
+
presentation: buildExplorePresentation(shaped)
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { headlessFetch } from '../fetch/headless-fetch.js';
|
|
2
|
+
import { buildFetchPresentation } from '../presentation/fetch-presentation.js';
|
|
3
|
+
import type { WebFetchHeadlessResponse } from '../types.js';
|
|
4
|
+
|
|
5
|
+
export function createWebFetchHeadlessTool({
|
|
6
|
+
fetchPage = headlessFetch
|
|
7
|
+
}: {
|
|
8
|
+
fetchPage?: (url: string) => Promise<WebFetchHeadlessResponse>;
|
|
9
|
+
} = {}) {
|
|
10
|
+
return async function webFetchHeadless({ url }: { url: string }): Promise<WebFetchHeadlessResponse> {
|
|
11
|
+
if (!/^https?:\/\//.test(url)) {
|
|
12
|
+
const result: WebFetchHeadlessResponse = {
|
|
13
|
+
status: 'unsupported',
|
|
14
|
+
url,
|
|
15
|
+
metadata: { method: 'headless', cacheHit: false },
|
|
16
|
+
error: { code: 'UNSUPPORTED_URL', message: 'Only http and https URLs are supported.' }
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
...result,
|
|
21
|
+
presentation: buildFetchPresentation(result)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = await fetchPage(url);
|
|
26
|
+
return {
|
|
27
|
+
...result,
|
|
28
|
+
presentation: buildFetchPresentation(result)
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createHttpFetcher } from '../fetch/http-fetch.js';
|
|
2
|
+
import { buildFetchPresentation } from '../presentation/fetch-presentation.js';
|
|
3
|
+
import type { WebFetchResponse } from '../types.js';
|
|
4
|
+
|
|
5
|
+
export function createWebFetchTool({
|
|
6
|
+
fetchPage = createHttpFetcher()
|
|
7
|
+
}: {
|
|
8
|
+
fetchPage?: (url: string) => Promise<WebFetchResponse>;
|
|
9
|
+
} = {}) {
|
|
10
|
+
return async function webFetch({ url }: { url: string }): Promise<WebFetchResponse> {
|
|
11
|
+
if (!/^https?:\/\//.test(url)) {
|
|
12
|
+
const result: WebFetchResponse = {
|
|
13
|
+
status: 'unsupported',
|
|
14
|
+
url,
|
|
15
|
+
metadata: { method: 'http', cacheHit: false },
|
|
16
|
+
error: { code: 'UNSUPPORTED_URL', message: 'Only http and https URLs are supported.' }
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
...result,
|
|
21
|
+
presentation: buildFetchPresentation(result)
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = await fetchPage(url);
|
|
26
|
+
return {
|
|
27
|
+
...result,
|
|
28
|
+
presentation: buildFetchPresentation(result)
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { createCacheKey, createTtlCache } from '../cache/ttl-cache.js';
|
|
2
|
+
import { buildSearchPresentation } from '../presentation/search-presentation.js';
|
|
3
|
+
import { fetchDuckDuckGoHtml, parseDuckDuckGoResults } from '../search/duckduckgo.js';
|
|
4
|
+
import type { WebSearchResponse } from '../types.js';
|
|
5
|
+
|
|
6
|
+
function classifySearchFailure(error: unknown) {
|
|
7
|
+
const rawMessage = error instanceof Error ? error.message : 'Unknown search failure.';
|
|
8
|
+
const normalized = rawMessage.toLowerCase();
|
|
9
|
+
|
|
10
|
+
if (
|
|
11
|
+
normalized.includes('blocked') ||
|
|
12
|
+
normalized.includes('rate limit') ||
|
|
13
|
+
normalized.includes('rate-limit') ||
|
|
14
|
+
normalized.includes('403') ||
|
|
15
|
+
normalized.includes('429') ||
|
|
16
|
+
normalized.includes('captcha') ||
|
|
17
|
+
normalized.includes('challenge')
|
|
18
|
+
) {
|
|
19
|
+
return {
|
|
20
|
+
code: 'BLOCKED',
|
|
21
|
+
message: 'DuckDuckGo search appears to be blocked or rate limited.'
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
code: 'FETCH_FAILED',
|
|
27
|
+
message: `DuckDuckGo search request failed: ${rawMessage}`
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function htmlLooksBlocked(html: string) {
|
|
32
|
+
const normalized = html.toLowerCase();
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
normalized.includes('captcha') ||
|
|
36
|
+
normalized.includes('challenge') ||
|
|
37
|
+
normalized.includes('verify you are human') ||
|
|
38
|
+
normalized.includes('are you a robot') ||
|
|
39
|
+
normalized.includes('unusual traffic')
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function createWebSearchTool({
|
|
44
|
+
searchHtml = fetchDuckDuckGoHtml,
|
|
45
|
+
cache = createTtlCache<WebSearchResponse>({ ttlMs: 30_000 })
|
|
46
|
+
}: {
|
|
47
|
+
searchHtml?: (query: string) => Promise<string>;
|
|
48
|
+
cache?: {
|
|
49
|
+
get(key: string): WebSearchResponse | undefined;
|
|
50
|
+
set(key: string, value: WebSearchResponse): void;
|
|
51
|
+
};
|
|
52
|
+
} = {}) {
|
|
53
|
+
return async function webSearch({ query }: { query: string }): Promise<WebSearchResponse> {
|
|
54
|
+
const normalizedQuery = query.trim();
|
|
55
|
+
|
|
56
|
+
if (!normalizedQuery) {
|
|
57
|
+
const result: WebSearchResponse = {
|
|
58
|
+
status: 'error',
|
|
59
|
+
results: [],
|
|
60
|
+
metadata: { backend: 'duckduckgo', cacheHit: false },
|
|
61
|
+
error: { code: 'INVALID_QUERY', message: 'Query must not be empty.' }
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
...result,
|
|
65
|
+
presentation: buildSearchPresentation(result)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const cacheKey = createCacheKey(['web_search', normalizedQuery]);
|
|
70
|
+
const cached = cache.get(cacheKey);
|
|
71
|
+
if (cached) {
|
|
72
|
+
const result: WebSearchResponse = {
|
|
73
|
+
...cached,
|
|
74
|
+
metadata: { ...cached.metadata, cacheHit: true }
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
...result,
|
|
78
|
+
presentation: buildSearchPresentation(result)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const html = await searchHtml(normalizedQuery);
|
|
84
|
+
const parsed = parseDuckDuckGoResults(html);
|
|
85
|
+
|
|
86
|
+
if (parsed.results.length > 0) {
|
|
87
|
+
const result: WebSearchResponse = {
|
|
88
|
+
status: 'ok',
|
|
89
|
+
results: parsed.results,
|
|
90
|
+
metadata: { backend: 'duckduckgo', cacheHit: false }
|
|
91
|
+
};
|
|
92
|
+
cache.set(cacheKey, result);
|
|
93
|
+
return {
|
|
94
|
+
...result,
|
|
95
|
+
presentation: buildSearchPresentation(result)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (parsed.noResults) {
|
|
100
|
+
const result: WebSearchResponse = {
|
|
101
|
+
status: 'error',
|
|
102
|
+
results: [],
|
|
103
|
+
metadata: { backend: 'duckduckgo', cacheHit: false },
|
|
104
|
+
error: {
|
|
105
|
+
code: 'NO_RESULTS',
|
|
106
|
+
message: 'DuckDuckGo returned no usable results for this query.'
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
...result,
|
|
111
|
+
presentation: buildSearchPresentation(result)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (htmlLooksBlocked(html)) {
|
|
116
|
+
const result: WebSearchResponse = {
|
|
117
|
+
status: 'error',
|
|
118
|
+
results: [],
|
|
119
|
+
metadata: { backend: 'duckduckgo', cacheHit: false },
|
|
120
|
+
error: {
|
|
121
|
+
code: 'BLOCKED',
|
|
122
|
+
message: 'DuckDuckGo search appears to be blocked or rate limited.'
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
...result,
|
|
127
|
+
presentation: buildSearchPresentation(result)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const result: WebSearchResponse = {
|
|
132
|
+
status: 'error',
|
|
133
|
+
results: [],
|
|
134
|
+
metadata: { backend: 'duckduckgo', cacheHit: false },
|
|
135
|
+
error: {
|
|
136
|
+
code: 'PARSE_FAILED',
|
|
137
|
+
message: 'DuckDuckGo returned a page, but it did not match the expected results format.'
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
...result,
|
|
142
|
+
presentation: buildSearchPresentation(result)
|
|
143
|
+
};
|
|
144
|
+
} catch (error) {
|
|
145
|
+
const result: WebSearchResponse = {
|
|
146
|
+
status: 'error',
|
|
147
|
+
results: [],
|
|
148
|
+
metadata: { backend: 'duckduckgo', cacheHit: false },
|
|
149
|
+
error: classifySearchFailure(error)
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
...result,
|
|
153
|
+
presentation: buildSearchPresentation(result)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|