@vellumai/assistant 0.12.5-dev.202609260125.8a6a403 → 0.12.5-dev.202609261313.4d052a3
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/openapi.yaml +2 -0
- package/package.json +1 -1
- package/src/__tests__/conversation-agent-loop-overflow.test.ts +68 -81
- package/src/api/events/tool-result.ts +1 -0
- package/src/providers/__tests__/search-provider-catalog.test.ts +8 -0
- package/src/providers/search-provider-catalog.ts +10 -0
- package/src/tools/network/__tests__/web-search.test.ts +105 -0
- package/src/tools/network/web-search.ts +228 -4
package/openapi.yaml
CHANGED
|
@@ -21572,6 +21572,7 @@ paths:
|
|
|
21572
21572
|
- searxng
|
|
21573
21573
|
- tinyfish
|
|
21574
21574
|
- exa
|
|
21575
|
+
- serply
|
|
21575
21576
|
resultCount:
|
|
21576
21577
|
type: number
|
|
21577
21578
|
durationMs:
|
|
@@ -22191,6 +22192,7 @@ paths:
|
|
|
22191
22192
|
- searxng
|
|
22192
22193
|
- tinyfish
|
|
22193
22194
|
- exa
|
|
22195
|
+
- serply
|
|
22194
22196
|
resultCount:
|
|
22195
22197
|
type: number
|
|
22196
22198
|
durationMs:
|
package/package.json
CHANGED
|
@@ -770,96 +770,83 @@ describe("session-agent-loop overflow recovery (JARVIS-110)", () => {
|
|
|
770
770
|
});
|
|
771
771
|
|
|
772
772
|
// ── Test 1 ────────────────────────────────────────────────────────
|
|
773
|
-
//
|
|
774
|
-
// before hitting context_too_large
|
|
775
|
-
//
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
// Expected behavior (PR 2 fix): After progress + context_too_large,
|
|
780
|
-
// the system should still attempt compaction before surfacing error.
|
|
781
|
-
test.todo(
|
|
782
|
-
"context too large after progress triggers compaction retry instead of immediate failure",
|
|
783
|
-
async () => {
|
|
784
|
-
const events: AssistantEvent[] = [];
|
|
785
|
-
let reducerCalled = false;
|
|
773
|
+
// Verifies overflow recovery fires even when the agent made tool-use progress
|
|
774
|
+
// before hitting context_too_large. The convergence path must not skip
|
|
775
|
+
// compaction just because messages were appended earlier in the turn.
|
|
776
|
+
test("context too large after progress triggers compaction retry instead of immediate failure", async () => {
|
|
777
|
+
const events: AssistantEvent[] = [];
|
|
778
|
+
let reducerCalled = false;
|
|
786
779
|
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
780
|
+
mockReducerStepFn = (msgs: Message[]) => {
|
|
781
|
+
reducerCalled = true;
|
|
782
|
+
return {
|
|
783
|
+
messages: msgs,
|
|
784
|
+
tier: "forced_compaction",
|
|
785
|
+
state: {
|
|
786
|
+
appliedTiers: ["forced_compaction"],
|
|
787
|
+
injectionMode: "full",
|
|
788
|
+
exhausted: false,
|
|
789
|
+
},
|
|
790
|
+
estimatedTokens: 50_000,
|
|
791
|
+
compactionResult: {
|
|
792
|
+
compacted: true,
|
|
790
793
|
messages: msgs,
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
previousEstimatedInputTokens: 190_000,
|
|
804
|
-
estimatedInputTokens: 50_000,
|
|
805
|
-
maxInputTokens: 200_000,
|
|
806
|
-
thresholdTokens: 160_000,
|
|
807
|
-
compactedMessages: 10,
|
|
808
|
-
summaryCalls: 1,
|
|
809
|
-
summaryInputTokens: 500,
|
|
810
|
-
summaryOutputTokens: 200,
|
|
811
|
-
summaryModel: "mock-model",
|
|
812
|
-
},
|
|
813
|
-
};
|
|
794
|
+
compactedPersistedMessages: 5,
|
|
795
|
+
summaryText: "Summary",
|
|
796
|
+
previousEstimatedInputTokens: 190_000,
|
|
797
|
+
estimatedInputTokens: 50_000,
|
|
798
|
+
maxInputTokens: 200_000,
|
|
799
|
+
thresholdTokens: 160_000,
|
|
800
|
+
compactedMessages: 10,
|
|
801
|
+
summaryCalls: 1,
|
|
802
|
+
summaryInputTokens: 500,
|
|
803
|
+
summaryOutputTokens: 200,
|
|
804
|
+
summaryModel: "mock-model",
|
|
805
|
+
},
|
|
814
806
|
};
|
|
807
|
+
};
|
|
815
808
|
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
809
|
+
// Run 1 makes progress (a tool turn) then the following provider call
|
|
810
|
+
// rejects with a context_too_large error; after the convergence reducer
|
|
811
|
+
// compacts, the rerun recovers with plain text.
|
|
812
|
+
const { provider } = createMockProvider([
|
|
813
|
+
toolUseResponse("tu-progress", "bash", { command: "ls" }),
|
|
814
|
+
new Error("prompt is too long: 242201 tokens > 200000 maximum"),
|
|
815
|
+
textResponse("recovered after compaction"),
|
|
816
|
+
]);
|
|
824
817
|
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
},
|
|
818
|
+
const ctx = makeCtx({
|
|
819
|
+
loopProvider: provider,
|
|
820
|
+
loopTools: [
|
|
821
|
+
{
|
|
822
|
+
name: "bash",
|
|
823
|
+
description: "Run a shell command",
|
|
824
|
+
input_schema: {
|
|
825
|
+
type: "object",
|
|
826
|
+
properties: { command: { type: "string" } },
|
|
835
827
|
},
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
}
|
|
828
|
+
},
|
|
829
|
+
],
|
|
830
|
+
toolExecutor: async () => ({
|
|
831
|
+
content: "file1.ts\nfile2.ts",
|
|
832
|
+
isError: false,
|
|
833
|
+
}),
|
|
834
|
+
contextWindowManager: {
|
|
835
|
+
updateConfig: () => {},
|
|
836
|
+
shouldCompact: () => ({ needed: false, estimatedTokens: 0 }),
|
|
837
|
+
maybeCompact: async () => ({ compacted: false }),
|
|
838
|
+
} as unknown as Conversation["contextWindowManager"],
|
|
839
|
+
});
|
|
847
840
|
|
|
848
|
-
|
|
841
|
+
await runAgentLoopImpl(ctx, "hello", "msg-1", (msg) => events.push(msg));
|
|
849
842
|
|
|
850
|
-
|
|
851
|
-
// context_too_large. The error is surfaced immediately.
|
|
852
|
-
// After PR 2 fix, the reducer SHOULD be called to attempt compaction.
|
|
853
|
-
expect(reducerCalled).toBe(true);
|
|
843
|
+
expect(reducerCalled).toBe(true);
|
|
854
844
|
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
expect(conversationError).toBeUndefined();
|
|
861
|
-
},
|
|
862
|
-
);
|
|
845
|
+
const conversationError = events.find(
|
|
846
|
+
(e) => e.type === "conversation_error",
|
|
847
|
+
);
|
|
848
|
+
expect(conversationError).toBeUndefined();
|
|
849
|
+
});
|
|
863
850
|
|
|
864
851
|
// ── Test 2 ────────────────────────────────────────────────────────
|
|
865
852
|
// When estimation says we're within budget but the provider rejects, the
|
|
@@ -112,4 +112,12 @@ describe("search-provider-catalog", () => {
|
|
|
112
112
|
expect(exa?.supportsApiBase).toBeUndefined();
|
|
113
113
|
expect(exa?.envVar).toBe("EXA_API_KEY");
|
|
114
114
|
});
|
|
115
|
+
|
|
116
|
+
test("Serply is a keyed BYOK provider without a custom API base", () => {
|
|
117
|
+
const serply = getSearchProvider("serply");
|
|
118
|
+
expect(serply?.kind).toBe("byok");
|
|
119
|
+
expect(serply?.keyless).toBeUndefined();
|
|
120
|
+
expect(serply?.supportsApiBase).toBeUndefined();
|
|
121
|
+
expect(serply?.envVar).toBe("SERPLY_API_KEY");
|
|
122
|
+
});
|
|
115
123
|
});
|
|
@@ -197,6 +197,16 @@ export const SEARCH_PROVIDER_CATALOG: readonly SearchProviderCatalogEntry[] = [
|
|
|
197
197
|
fallbackOrder: 9,
|
|
198
198
|
privacyPolicyUrl: "https://exa.ai/privacy-policy",
|
|
199
199
|
},
|
|
200
|
+
{
|
|
201
|
+
id: "serply",
|
|
202
|
+
displayName: "Serply",
|
|
203
|
+
kind: "byok",
|
|
204
|
+
apiKeyPrefix: "Serply API key...",
|
|
205
|
+
envVar: "SERPLY_API_KEY",
|
|
206
|
+
secretKey: "serply",
|
|
207
|
+
fallbackOrder: 10,
|
|
208
|
+
privacyPolicyUrl: "https://serply.io/privacy",
|
|
209
|
+
},
|
|
200
210
|
];
|
|
201
211
|
|
|
202
212
|
/** Provider ids accepted by the web-search config schema. */
|
|
@@ -13,6 +13,7 @@ let mockFastcrwSecureKey: string | undefined;
|
|
|
13
13
|
let mockSearxngSecureKey: string | undefined;
|
|
14
14
|
let mockTinyfishSecureKey: string | undefined;
|
|
15
15
|
let mockExaSecureKey: string | undefined;
|
|
16
|
+
let mockSerplySecureKey: string | undefined;
|
|
16
17
|
let mockManagedSearchProxyResult: any;
|
|
17
18
|
let mockManagedSearchAvailable = true;
|
|
18
19
|
let mockManagedSearchProxyCalls: Array<{
|
|
@@ -69,6 +70,9 @@ mock.module("../../../security/secure-keys.js", () => ({
|
|
|
69
70
|
if (provider === "exa") {
|
|
70
71
|
return mockExaSecureKey;
|
|
71
72
|
}
|
|
73
|
+
if (provider === "serply") {
|
|
74
|
+
return mockSerplySecureKey;
|
|
75
|
+
}
|
|
72
76
|
return undefined;
|
|
73
77
|
},
|
|
74
78
|
}));
|
|
@@ -116,6 +120,7 @@ describe("web_search tool", () => {
|
|
|
116
120
|
mockSearxngSecureKey = undefined;
|
|
117
121
|
mockTinyfishSecureKey = undefined;
|
|
118
122
|
mockExaSecureKey = undefined;
|
|
123
|
+
mockSerplySecureKey = undefined;
|
|
119
124
|
mockManagedSearchProxyCalls = [];
|
|
120
125
|
mockManagedSearchAvailable = true;
|
|
121
126
|
mockManagedSearchProxyResult = {
|
|
@@ -1441,6 +1446,106 @@ describe("web_search tool", () => {
|
|
|
1441
1446
|
expect(result.activityMetadata?.webSearch?.provider).toBe("exa");
|
|
1442
1447
|
});
|
|
1443
1448
|
|
|
1449
|
+
// ---- Serply provider ---------------------------------------------------
|
|
1450
|
+
|
|
1451
|
+
test("Serply search sends the query form request and maps freshness", async () => {
|
|
1452
|
+
seedWebSearch("your-own", "serply");
|
|
1453
|
+
mockSerplySecureKey = "serply_test";
|
|
1454
|
+
let capturedUrl = "";
|
|
1455
|
+
let capturedInit: RequestInit | undefined;
|
|
1456
|
+
globalThis.fetch = (async (url: string, init?: RequestInit) => {
|
|
1457
|
+
capturedUrl = url;
|
|
1458
|
+
capturedInit = init;
|
|
1459
|
+
return new Response(
|
|
1460
|
+
JSON.stringify({
|
|
1461
|
+
query: "fresh tools",
|
|
1462
|
+
total: 1,
|
|
1463
|
+
results: [
|
|
1464
|
+
{
|
|
1465
|
+
title: "Serply Result",
|
|
1466
|
+
link: "https://example.com/serply",
|
|
1467
|
+
description: "Fresh from Serply",
|
|
1468
|
+
position: 1,
|
|
1469
|
+
},
|
|
1470
|
+
],
|
|
1471
|
+
}),
|
|
1472
|
+
{ status: 200, headers: { "content-type": "application/json" } },
|
|
1473
|
+
);
|
|
1474
|
+
}) as any;
|
|
1475
|
+
|
|
1476
|
+
const result = await execute({
|
|
1477
|
+
query: "fresh tools",
|
|
1478
|
+
freshness: "pw",
|
|
1479
|
+
count: 5,
|
|
1480
|
+
});
|
|
1481
|
+
|
|
1482
|
+
expect(result.isError).toBe(false);
|
|
1483
|
+
const parsed = new URL(capturedUrl);
|
|
1484
|
+
expect(parsed.origin + parsed.pathname).toBe(
|
|
1485
|
+
"https://api.serply.io/v1/search",
|
|
1486
|
+
);
|
|
1487
|
+
expect(parsed.searchParams.get("q")).toBe("fresh tools");
|
|
1488
|
+
expect(parsed.searchParams.get("num")).toBe("5");
|
|
1489
|
+
expect(parsed.searchParams.get("tbs")).toBe("qdr:w");
|
|
1490
|
+
expect(capturedInit?.method).toBe("GET");
|
|
1491
|
+
const headers = new Headers(capturedInit?.headers);
|
|
1492
|
+
expect(headers.get("x-api-key")).toBe("serply_test");
|
|
1493
|
+
expect(headers.get("user-agent")).toBe("vellum-assistant");
|
|
1494
|
+
expect(result.content).toContain("Serply Result");
|
|
1495
|
+
expect(result.content).toContain("Fresh from Serply");
|
|
1496
|
+
expect(result.activityMetadata?.webSearch?.provider).toBe("serply");
|
|
1497
|
+
});
|
|
1498
|
+
|
|
1499
|
+
test("Serply omits tbs without freshness, clamps num, and trims to count", async () => {
|
|
1500
|
+
seedWebSearch("your-own", "serply");
|
|
1501
|
+
mockSerplySecureKey = "serply_test";
|
|
1502
|
+
let capturedUrl = "";
|
|
1503
|
+
globalThis.fetch = (async (url: string) => {
|
|
1504
|
+
capturedUrl = url;
|
|
1505
|
+
return new Response(
|
|
1506
|
+
JSON.stringify({
|
|
1507
|
+
results: [
|
|
1508
|
+
{ title: "A", link: "https://a.example.com", description: "a" },
|
|
1509
|
+
{ title: "B", link: "https://b.example.com", description: "b" },
|
|
1510
|
+
{ title: "C", link: "https://c.example.com", description: "c" },
|
|
1511
|
+
],
|
|
1512
|
+
}),
|
|
1513
|
+
{ status: 200, headers: { "content-type": "application/json" } },
|
|
1514
|
+
);
|
|
1515
|
+
}) as any;
|
|
1516
|
+
|
|
1517
|
+
const result = await execute({ query: "test", count: 2 });
|
|
1518
|
+
|
|
1519
|
+
const parsed = new URL(capturedUrl);
|
|
1520
|
+
expect(parsed.searchParams.get("tbs")).toBeNull();
|
|
1521
|
+
expect(parsed.searchParams.get("num")).toBe("2");
|
|
1522
|
+
expect(result.content).toContain("A");
|
|
1523
|
+
expect(result.content).toContain("B");
|
|
1524
|
+
expect(result.content).not.toContain("https://c.example.com");
|
|
1525
|
+
expect(result.activityMetadata?.webSearch?.resultCount).toBe(2);
|
|
1526
|
+
|
|
1527
|
+
const clamped = await execute({ query: "test", count: 20 });
|
|
1528
|
+
expect(new URL(capturedUrl).searchParams.get("num")).toBe("10");
|
|
1529
|
+
expect(clamped.isError).toBe(false);
|
|
1530
|
+
});
|
|
1531
|
+
|
|
1532
|
+
test.each([401, 402, 403])(
|
|
1533
|
+
"Serply handles %d access error",
|
|
1534
|
+
async (status) => {
|
|
1535
|
+
seedWebSearch("your-own", "serply");
|
|
1536
|
+
mockSerplySecureKey = "serply_test";
|
|
1537
|
+
globalThis.fetch = (async () =>
|
|
1538
|
+
new Response(JSON.stringify({ detail: "Invalid API key" }), {
|
|
1539
|
+
status,
|
|
1540
|
+
})) as any;
|
|
1541
|
+
|
|
1542
|
+
const result = await execute({ query: "test" });
|
|
1543
|
+
|
|
1544
|
+
expect(result.isError).toBe(true);
|
|
1545
|
+
expect(result.activityMetadata?.webSearch?.provider).toBe("serply");
|
|
1546
|
+
},
|
|
1547
|
+
);
|
|
1548
|
+
|
|
1444
1549
|
// ---- Provider fallback --------------------------------------------------
|
|
1445
1550
|
|
|
1446
1551
|
test("falls back from perplexity to brave when perplexity has no key", async () => {
|
|
@@ -46,6 +46,10 @@ const FIRECRAWL_API_URL = "https://api.firecrawl.dev/v2/search";
|
|
|
46
46
|
const FASTCRW_SEARCH_PATH = "/v1/search";
|
|
47
47
|
const TINYFISH_DEFAULT_SEARCH_API_BASE = "https://api.search.tinyfish.ai";
|
|
48
48
|
const EXA_API_URL = "https://api.exa.ai/search";
|
|
49
|
+
const SERPLY_API_URL = "https://api.serply.io/v1/search";
|
|
50
|
+
// Serply returns at most 10 organic results per call; larger counts are
|
|
51
|
+
// clamped on request and trimmed on response.
|
|
52
|
+
const SERPLY_MAX_RESULTS = 10;
|
|
49
53
|
// Keenable is keyless by default: the public path needs no key (rate-limited);
|
|
50
54
|
// a key switches to the authenticated path and lifts the cap.
|
|
51
55
|
const KEENABLE_API_BASE_URL = "https://api.keenable.ai";
|
|
@@ -61,7 +65,8 @@ type WebSearchProvider =
|
|
|
61
65
|
| "fastcrw"
|
|
62
66
|
| "searxng"
|
|
63
67
|
| "tinyfish"
|
|
64
|
-
| "exa"
|
|
68
|
+
| "exa"
|
|
69
|
+
| "serply";
|
|
65
70
|
|
|
66
71
|
/**
|
|
67
72
|
* Arguments passed to every {@link WebSearchAdapter}. The full superset is
|
|
@@ -213,6 +218,19 @@ interface ExaSearchResponse {
|
|
|
213
218
|
results?: ExaSearchResult[];
|
|
214
219
|
}
|
|
215
220
|
|
|
221
|
+
interface SerplySearchResult {
|
|
222
|
+
title?: string | null;
|
|
223
|
+
link?: string;
|
|
224
|
+
description?: string | null;
|
|
225
|
+
position?: number;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
interface SerplySearchResponse {
|
|
229
|
+
query?: string;
|
|
230
|
+
total?: number;
|
|
231
|
+
results?: SerplySearchResult[];
|
|
232
|
+
}
|
|
233
|
+
|
|
216
234
|
const SEARXNG_MISSING_INSTANCE_MESSAGE =
|
|
217
235
|
"SearXNG needs an instance URL. Set API Base in Settings under Web Search.";
|
|
218
236
|
const SEARXNG_JSON_DISABLED_MESSAGE =
|
|
@@ -813,6 +831,81 @@ function exaStartPublishedDateForFreshness(
|
|
|
813
831
|
return new Date(now - days * 24 * 60 * 60 * 1000).toISOString();
|
|
814
832
|
}
|
|
815
833
|
|
|
834
|
+
function formatSerplyResults(
|
|
835
|
+
data: SerplySearchResponse,
|
|
836
|
+
query: string,
|
|
837
|
+
): string {
|
|
838
|
+
const results = data.results ?? [];
|
|
839
|
+
if (results.length === 0) {
|
|
840
|
+
return `No results found for "${query}".`;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
const lines: string[] = [`Web search results for "${query}":\n`];
|
|
844
|
+
for (let i = 0; i < results.length; i++) {
|
|
845
|
+
const result = results[i];
|
|
846
|
+
const title =
|
|
847
|
+
result.title?.trim() || result.link?.trim() || "Untitled result";
|
|
848
|
+
lines.push(`${i + 1}. ${title}`);
|
|
849
|
+
if (result.link) {
|
|
850
|
+
lines.push(` URL: ${result.link}`);
|
|
851
|
+
}
|
|
852
|
+
const snippet = result.description?.trim();
|
|
853
|
+
if (snippet) {
|
|
854
|
+
lines.push(` ${snippet}`);
|
|
855
|
+
}
|
|
856
|
+
lines.push("");
|
|
857
|
+
}
|
|
858
|
+
return lines.join("\n");
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function buildSerplyMetadata(
|
|
862
|
+
data: SerplySearchResponse,
|
|
863
|
+
query: string,
|
|
864
|
+
durationMs: number,
|
|
865
|
+
): WebSearchMetadata {
|
|
866
|
+
const results = data.results ?? [];
|
|
867
|
+
const items: WebSearchResultItem[] = results.map((result, index) => {
|
|
868
|
+
const url = result.link ?? "";
|
|
869
|
+
const domain = extractDomain(url);
|
|
870
|
+
return {
|
|
871
|
+
rank: index + 1,
|
|
872
|
+
title: result.title?.trim() || url.trim() || "Untitled result",
|
|
873
|
+
url,
|
|
874
|
+
domain,
|
|
875
|
+
faviconUrl: faviconUrlForDomain(domain),
|
|
876
|
+
snippet: result.description?.trim() || undefined,
|
|
877
|
+
};
|
|
878
|
+
});
|
|
879
|
+
return {
|
|
880
|
+
query,
|
|
881
|
+
provider: "serply",
|
|
882
|
+
resultCount: items.length,
|
|
883
|
+
durationMs,
|
|
884
|
+
results: items,
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Serply passes Google's `tbs` time filter through unchanged, so the tool's
|
|
890
|
+
* freshness buckets map onto the `qdr:` values Google understands.
|
|
891
|
+
*/
|
|
892
|
+
function serplyTbsForFreshness(
|
|
893
|
+
freshness: string | undefined,
|
|
894
|
+
): string | undefined {
|
|
895
|
+
switch (freshness) {
|
|
896
|
+
case "pd":
|
|
897
|
+
return "qdr:d";
|
|
898
|
+
case "pw":
|
|
899
|
+
return "qdr:w";
|
|
900
|
+
case "pm":
|
|
901
|
+
return "qdr:m";
|
|
902
|
+
case "py":
|
|
903
|
+
return "qdr:y";
|
|
904
|
+
default:
|
|
905
|
+
return undefined;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
816
909
|
function tinyfishRecencyMinutesForFreshness(
|
|
817
910
|
freshness: string | undefined,
|
|
818
911
|
): number | undefined {
|
|
@@ -2024,6 +2117,128 @@ async function executeExaSearch(
|
|
|
2024
2117
|
);
|
|
2025
2118
|
}
|
|
2026
2119
|
|
|
2120
|
+
async function executeSerplySearch(
|
|
2121
|
+
query: string,
|
|
2122
|
+
count: number,
|
|
2123
|
+
freshness: string | undefined,
|
|
2124
|
+
apiKey: string,
|
|
2125
|
+
signal?: AbortSignal,
|
|
2126
|
+
): Promise<ToolExecutionResult> {
|
|
2127
|
+
const startedAt = Date.now();
|
|
2128
|
+
const params = new URLSearchParams({
|
|
2129
|
+
q: query,
|
|
2130
|
+
num: String(Math.min(count, SERPLY_MAX_RESULTS)),
|
|
2131
|
+
});
|
|
2132
|
+
const tbs = serplyTbsForFreshness(freshness);
|
|
2133
|
+
if (tbs !== undefined) {
|
|
2134
|
+
params.set("tbs", tbs);
|
|
2135
|
+
}
|
|
2136
|
+
const url = `${SERPLY_API_URL}?${params.toString()}`;
|
|
2137
|
+
|
|
2138
|
+
const headers = {
|
|
2139
|
+
Accept: "application/json",
|
|
2140
|
+
"X-Api-Key": apiKey.trim(),
|
|
2141
|
+
"User-Agent": "vellum-assistant",
|
|
2142
|
+
};
|
|
2143
|
+
|
|
2144
|
+
for (let attempt = 0; attempt <= DEFAULT_MAX_RETRIES; attempt++) {
|
|
2145
|
+
let response: Response;
|
|
2146
|
+
try {
|
|
2147
|
+
response = await fetch(url, { method: "GET", headers, signal });
|
|
2148
|
+
} catch (err) {
|
|
2149
|
+
return networkFailureResult(query, "serply", startedAt, err, signal);
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
const bodyText = await response.text();
|
|
2153
|
+
if (response.ok) {
|
|
2154
|
+
let data: SerplySearchResponse;
|
|
2155
|
+
try {
|
|
2156
|
+
data = JSON.parse(bodyText) as SerplySearchResponse;
|
|
2157
|
+
} catch {
|
|
2158
|
+
return errorResult(
|
|
2159
|
+
query,
|
|
2160
|
+
"serply",
|
|
2161
|
+
startedAt,
|
|
2162
|
+
"Serply Search returned an invalid JSON payload.",
|
|
2163
|
+
);
|
|
2164
|
+
}
|
|
2165
|
+
if (data.results && data.results.length > count) {
|
|
2166
|
+
data.results = data.results.slice(0, count);
|
|
2167
|
+
}
|
|
2168
|
+
const durationMs = Date.now() - startedAt;
|
|
2169
|
+
return {
|
|
2170
|
+
content:
|
|
2171
|
+
wrapUntrustedContent(formatSerplyResults(data, query), {
|
|
2172
|
+
source: "search",
|
|
2173
|
+
sourceDetail: "serply",
|
|
2174
|
+
}) + CITATION_INSTRUCTION,
|
|
2175
|
+
isError: false,
|
|
2176
|
+
activityMetadata: {
|
|
2177
|
+
webSearch: buildSerplyMetadata(data, query, durationMs),
|
|
2178
|
+
},
|
|
2179
|
+
};
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
if (response.status === 401) {
|
|
2183
|
+
return errorResult(
|
|
2184
|
+
query,
|
|
2185
|
+
"serply",
|
|
2186
|
+
startedAt,
|
|
2187
|
+
"Invalid or expired Serply API key",
|
|
2188
|
+
);
|
|
2189
|
+
}
|
|
2190
|
+
if (response.status === 402) {
|
|
2191
|
+
return errorResult(
|
|
2192
|
+
query,
|
|
2193
|
+
"serply",
|
|
2194
|
+
startedAt,
|
|
2195
|
+
"Serply account has no remaining credits.",
|
|
2196
|
+
);
|
|
2197
|
+
}
|
|
2198
|
+
if (response.status === 403) {
|
|
2199
|
+
return errorResult(
|
|
2200
|
+
query,
|
|
2201
|
+
"serply",
|
|
2202
|
+
startedAt,
|
|
2203
|
+
"Serply Search request was forbidden by the upstream service.",
|
|
2204
|
+
);
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
if (response.status === 429 && attempt < DEFAULT_MAX_RETRIES) {
|
|
2208
|
+
const delayMs = getHttpRetryDelay(
|
|
2209
|
+
response,
|
|
2210
|
+
attempt,
|
|
2211
|
+
DEFAULT_BASE_DELAY_MS,
|
|
2212
|
+
);
|
|
2213
|
+
log.warn(
|
|
2214
|
+
{ attempt: attempt + 1, delayMs },
|
|
2215
|
+
"Serply Search rate limited, retrying",
|
|
2216
|
+
);
|
|
2217
|
+
await sleep(delayMs);
|
|
2218
|
+
continue;
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
log.warn({ status: response.status }, "Serply Search API error");
|
|
2222
|
+
return backendFailureResult(
|
|
2223
|
+
query,
|
|
2224
|
+
"serply",
|
|
2225
|
+
startedAt,
|
|
2226
|
+
{ statusCode: response.status, error: rawBodyDetail(bodyText) },
|
|
2227
|
+
response.status === 429
|
|
2228
|
+
? "Serply Search rate limit exceeded after retries. Try again shortly."
|
|
2229
|
+
: `Serply Search API returned status ${response.status}`,
|
|
2230
|
+
);
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
return backendFailureResult(
|
|
2234
|
+
query,
|
|
2235
|
+
"serply",
|
|
2236
|
+
startedAt,
|
|
2237
|
+
{ statusCode: 429 },
|
|
2238
|
+
"Serply Search rate limit exceeded after retries. Try again shortly.",
|
|
2239
|
+
);
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2027
2242
|
// ----------------------------------------------------------------------------
|
|
2028
2243
|
// Adapter registry
|
|
2029
2244
|
//
|
|
@@ -2115,6 +2330,14 @@ const exaSearchAdapter: WebSearchAdapter = {
|
|
|
2115
2330
|
executeExaSearch(query, count, freshness, apiKey, signal),
|
|
2116
2331
|
};
|
|
2117
2332
|
|
|
2333
|
+
const serplySearchAdapter: WebSearchAdapter = {
|
|
2334
|
+
id: "serply",
|
|
2335
|
+
providerKeyName: "serply",
|
|
2336
|
+
fallbackOrder: 10,
|
|
2337
|
+
execute: ({ query, count, freshness, apiKey, signal }) =>
|
|
2338
|
+
executeSerplySearch(query, count, freshness, apiKey, signal),
|
|
2339
|
+
};
|
|
2340
|
+
|
|
2118
2341
|
/**
|
|
2119
2342
|
* All built-in web-search adapters keyed by provider id. The
|
|
2120
2343
|
* `Record<WebSearchProvider, ...>` shape forces TypeScript to flag any
|
|
@@ -2130,6 +2353,7 @@ const WEB_SEARCH_ADAPTERS: Record<WebSearchProvider, WebSearchAdapter> = {
|
|
|
2130
2353
|
searxng: searxngSearchAdapter,
|
|
2131
2354
|
tinyfish: tinyfishSearchAdapter,
|
|
2132
2355
|
exa: exaSearchAdapter,
|
|
2356
|
+
serply: serplySearchAdapter,
|
|
2133
2357
|
};
|
|
2134
2358
|
|
|
2135
2359
|
/**
|
|
@@ -2161,7 +2385,7 @@ export const webSearchTool = {
|
|
|
2161
2385
|
count: {
|
|
2162
2386
|
type: "number",
|
|
2163
2387
|
description:
|
|
2164
|
-
"Number of results to return (1-20, default 10). Used with Brave, Tavily, Firecrawl, Keenable, fastCRW, SearXNG, TinyFish, and
|
|
2388
|
+
"Number of results to return (1-20, default 10). Used with Brave, Tavily, Firecrawl, Keenable, fastCRW, SearXNG, TinyFish, Exa, and Serply providers.",
|
|
2165
2389
|
},
|
|
2166
2390
|
offset: {
|
|
2167
2391
|
type: "number",
|
|
@@ -2171,7 +2395,7 @@ export const webSearchTool = {
|
|
|
2171
2395
|
freshness: {
|
|
2172
2396
|
type: "string",
|
|
2173
2397
|
description:
|
|
2174
|
-
'Filter by recency: "pd" (past day), "pw" (past week), "pm" (past month), "py" (past year). Used with Brave, Tavily, Firecrawl, Keenable, fastCRW, SearXNG, TinyFish, and
|
|
2398
|
+
'Filter by recency: "pd" (past day), "pw" (past week), "pm" (past month), "py" (past year). Used with Brave, Tavily, Firecrawl, Keenable, fastCRW, SearXNG, TinyFish, Exa, and Serply providers. SearXNG maps day/month/year and omits week.',
|
|
2175
2399
|
},
|
|
2176
2400
|
},
|
|
2177
2401
|
required: ["query"],
|
|
@@ -2289,7 +2513,7 @@ export const webSearchTool = {
|
|
|
2289
2513
|
query,
|
|
2290
2514
|
provider,
|
|
2291
2515
|
startedAt,
|
|
2292
|
-
"No web search API key configured. Set it via `keys set perplexity <key>`, `keys set brave <key>`, `keys set tavily <key>`, `keys set firecrawl <key>`, or `keys set fastcrw <key>`, or `keys set tinyfish <key>`, or `keys set exa <key>`, or configure it from the Settings page under API Keys. Or switch the web-search provider to Keenable, which works without a key, or SearXNG with your instance URL.",
|
|
2516
|
+
"No web search API key configured. Set it via `keys set perplexity <key>`, `keys set brave <key>`, `keys set tavily <key>`, `keys set firecrawl <key>`, or `keys set fastcrw <key>`, or `keys set tinyfish <key>`, or `keys set exa <key>`, or `keys set serply <key>`, or configure it from the Settings page under API Keys. Or switch the web-search provider to Keenable, which works without a key, or SearXNG with your instance URL.",
|
|
2293
2517
|
);
|
|
2294
2518
|
}
|
|
2295
2519
|
}
|