Compare commits

..

4 Commits

Author SHA1 Message Date
e33df3e8d1 Merge remote-tracking branch 'origin/master' into feat/web-search
# Conflicts:
#	bun.lock
2026-06-13 00:39:28 +01:00
b024a4ba4d feat: add exa web search source 2026-06-13 00:34:09 +01:00
877b955493 feat: add mcp source primitive (#123) 2026-06-12 22:50:42 +01:00
6b1db0b3d3 chore: rename aelis to freya (#122) 2026-06-12 17:35:26 +01:00
22 changed files with 2488 additions and 36 deletions

View File

@@ -66,6 +66,14 @@ export function SourceConfigPanel({ source, onUpdate }: SourceConfigPanelProps)
return creds
}
function buildReplaceBody(enabledValue: boolean): Parameters<typeof replaceSource>[1] {
const body: Parameters<typeof replaceSource>[1] = { enabled: enabledValue }
if (Object.keys(source.fields).length > 0) {
body.config = getUserConfig()
}
return body
}
function invalidate() {
queryClient.invalidateQueries({ queryKey: ["sourceConfig", source.id] })
queryClient.invalidateQueries({ queryKey: ["configs"] })
@@ -79,10 +87,7 @@ export function SourceConfigPanel({ source, onUpdate }: SourceConfigPanelProps)
(v) => typeof v === "string" && v.length > 0,
)
const body: Parameters<typeof replaceSource>[1] = {
enabled,
config: getUserConfig(),
}
const body = buildReplaceBody(enabled)
if (hasCredentials && source.perUserCredentials) {
body.credentials = credentialFields
}
@@ -104,8 +109,7 @@ export function SourceConfigPanel({ source, onUpdate }: SourceConfigPanelProps)
})
const toggleMutation = useMutation({
mutationFn: (checked: boolean) =>
replaceSource(source.id, { enabled: checked, config: getUserConfig() }),
mutationFn: (checked: boolean) => replaceSource(source.id, buildReplaceBody(checked)),
onSuccess(_data, checked) {
invalidate()
toast.success(`Source ${checked ? "enabled" : "disabled"}`)
@@ -116,7 +120,7 @@ export function SourceConfigPanel({ source, onUpdate }: SourceConfigPanelProps)
})
const deleteMutation = useMutation({
mutationFn: () => replaceSource(source.id, { enabled: false, config: {} }),
mutationFn: () => replaceSource(source.id, buildReplaceBody(false)),
onSuccess() {
setDirty({})
invalidate()

View File

@@ -151,6 +151,12 @@ const sourceDefinitions: SourceDefinition[] = [
},
},
},
{
id: "freya.web-search",
name: "Web Search",
description: "Exa web search action. Requires EXA_API_KEY on the backend.",
fields: {},
},
]
export function fetchSources(): Promise<SourceDefinition[]> {
@@ -174,7 +180,7 @@ export async function fetchConfigs(): Promise<SourceConfig[]> {
export async function replaceSource(
sourceId: string,
body: { enabled: boolean; config: unknown; credentials?: Record<string, unknown> },
body: { enabled: boolean; config?: unknown; credentials?: Record<string, unknown> },
): Promise<void> {
const res = await fetch(`${serverBase()}/sources/${sourceId}`, {
method: "PUT",

View File

@@ -21,6 +21,7 @@
"@freya/source-location": "workspace:*",
"@freya/source-tfl": "workspace:*",
"@freya/source-weatherkit": "workspace:*",
"@freya/source-web-search": "workspace:*",
"@openrouter/sdk": "^0.9.11",
"arktype": "^2.1.29",
"better-auth": "^1",

View File

@@ -18,6 +18,7 @@ import { UserSessionManager } from "./session/index.ts"
import { registerSourcesHttpHandlers } from "./sources/http.ts"
import { TflSourceProvider } from "./tfl/provider.ts"
import { WeatherSourceProvider } from "./weather/provider.ts"
import { WebSearchSourceProvider } from "./web-search/provider.ts"
function main() {
const { db, close: closeDb } = createDatabase(process.env.DATABASE_URL!)
@@ -60,6 +61,7 @@ function main() {
},
}),
new TflSourceProvider({ apiKey: process.env.TFL_API_KEY! }),
new WebSearchSourceProvider({ apiKey: process.env.EXA_API_KEY }),
],
feedEnhancer,
credentialEncryptor,

View File

@@ -186,6 +186,18 @@ function put(app: Hono, sourceId: string, body: unknown) {
})
}
function listActions(app: Hono, sourceId: string) {
return app.request(`/api/sources/${sourceId}/actions`, { method: "GET" })
}
function executeAction(app: Hono, sourceId: string, actionId: string, body: unknown) {
return app.request(`/api/sources/${sourceId}/actions/${actionId}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
@@ -781,6 +793,168 @@ describe("PUT /api/sources/:sourceId", () => {
})
})
describe("GET /api/sources/:sourceId/actions", () => {
test("returns 401 without auth", async () => {
activeStore = createInMemoryStore()
const { app } = createApp([createStubProvider("freya.location")])
const res = await listActions(app, "freya.location")
expect(res.status).toBe(401)
})
test("returns 404 for source that is not enabled in the user session", async () => {
activeStore = createInMemoryStore()
const { app } = createApp([createStubProvider("freya.location")], MOCK_USER_ID)
const res = await listActions(app, "freya.location")
expect(res.status).toBe(404)
})
test("returns serializable action definitions", async () => {
activeStore = createInMemoryStore()
activeStore.seed(MOCK_USER_ID, "test.actions")
const provider: FeedSourceProvider = {
sourceId: "test.actions",
async feedSourceForUser() {
return {
id: "test.actions",
async listActions() {
return {
search: {
id: "search",
description: "Search something",
input: tflConfig,
},
}
},
async executeAction() {
return undefined
},
async fetchContext() {
return null
},
}
},
}
const { app } = createApp([provider], MOCK_USER_ID)
const res = await listActions(app, "test.actions")
expect(res.status).toBe(200)
const body = (await res.json()) as {
actions: Record<string, { id: string; description?: string; input?: unknown }>
}
expect(body.actions.search).toEqual({
id: "search",
description: "Search something",
})
})
})
describe("POST /api/sources/:sourceId/actions/:actionId", () => {
test("returns 401 without auth", async () => {
activeStore = createInMemoryStore()
const { app } = createApp([createStubProvider("freya.location")])
const res = await executeAction(app, "freya.location", "update-location", {})
expect(res.status).toBe(401)
})
test("executes source action with request body as params", async () => {
activeStore = createInMemoryStore()
activeStore.seed(MOCK_USER_ID, "test.actions")
let receivedParams: unknown
const provider: FeedSourceProvider = {
sourceId: "test.actions",
async feedSourceForUser() {
return {
id: "test.actions",
async listActions() {
return {
search: { id: "search", description: "Search something" },
}
},
async executeAction(_actionId: string, params: unknown) {
receivedParams = params
return { ok: true, count: 2 }
},
async fetchContext() {
return null
},
}
},
}
const { app } = createApp([provider], MOCK_USER_ID)
const res = await executeAction(app, "test.actions", "search", { query: "exa" })
expect(res.status).toBe(200)
expect(receivedParams).toEqual({ query: "exa" })
const body = (await res.json()) as { result: unknown }
expect(body.result).toEqual({ ok: true, count: 2 })
})
test("returns 404 for unknown action", async () => {
activeStore = createInMemoryStore()
activeStore.seed(MOCK_USER_ID, "freya.location")
const { app } = createApp([createStubProvider("freya.location")], MOCK_USER_ID)
const res = await executeAction(app, "freya.location", "missing", {})
expect(res.status).toBe(404)
})
test("returns 400 for invalid JSON", async () => {
activeStore = createInMemoryStore()
activeStore.seed(MOCK_USER_ID, "freya.location")
const { app } = createApp([createStubProvider("freya.location")], MOCK_USER_ID)
const res = await app.request("/api/sources/freya.location/actions/search", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "not-json",
})
expect(res.status).toBe(400)
const body = (await res.json()) as { error: string }
expect(body.error).toBe("Invalid JSON")
})
test("returns 400 when source rejects params", async () => {
activeStore = createInMemoryStore()
activeStore.seed(MOCK_USER_ID, "test.actions")
const provider: FeedSourceProvider = {
sourceId: "test.actions",
async feedSourceForUser() {
return {
id: "test.actions",
async listActions() {
return {
search: { id: "search" },
}
},
async executeAction() {
throw new Error("query must not be empty")
},
async fetchContext() {
return null
},
}
},
}
const { app } = createApp([provider], MOCK_USER_ID)
const res = await executeAction(app, "test.actions", "search", { query: "" })
expect(res.status).toBe(400)
const body = (await res.json()) as { error: string }
expect(body.error).toBe("query must not be empty")
})
})
describe("PUT /api/sources/:sourceId/credentials", () => {
test("returns 401 without auth", async () => {
activeStore = createInMemoryStore()

View File

@@ -1,3 +1,4 @@
import type { ActionDefinition } from "@freya/core"
import type { Context, Hono } from "hono"
import { type } from "arktype"
@@ -55,6 +56,13 @@ export function registerSourcesHttpHandlers(
app.get("/api/sources/:sourceId", inject, authSessionMiddleware, handleGetSource)
app.patch("/api/sources/:sourceId", inject, authSessionMiddleware, handleUpdateSource)
app.put("/api/sources/:sourceId", inject, authSessionMiddleware, handleReplaceSource)
app.get("/api/sources/:sourceId/actions", inject, authSessionMiddleware, handleListActions)
app.post(
"/api/sources/:sourceId/actions/:actionId",
inject,
authSessionMiddleware,
handleExecuteAction,
)
app.put(
"/api/sources/:sourceId/credentials",
inject,
@@ -189,6 +197,71 @@ async function handleReplaceSource(c: Context<Env>) {
return c.body(null, 204)
}
async function handleListActions(c: Context<Env>) {
const sourceId = c.req.param("sourceId")
if (!sourceId) {
return c.body(null, 404)
}
const user = c.get("user")!
const sessionManager = c.get("sessionManager")
let session
try {
session = await sessionManager.getOrCreate(user.id)
} catch (err) {
console.error("[handleListActions] Failed to create session:", err)
return c.json({ error: "Service unavailable" }, 503)
}
try {
const actions = await session.engine.listActions(sourceId)
return c.json({ actions: serializeActions(actions) })
} catch (err) {
if (isActionNotFoundError(err)) {
return c.json({ error: err.message }, 404)
}
console.error(`[handleListActions] Failed to list actions for "${sourceId}":`, err)
return c.json({ error: "Failed to list actions" }, 500)
}
}
async function handleExecuteAction(c: Context<Env>) {
const sourceId = c.req.param("sourceId")
const actionId = c.req.param("actionId")
if (!sourceId || !actionId) {
return c.body(null, 404)
}
let params: unknown
try {
params = await c.req.json()
} catch {
return c.json({ error: "Invalid JSON" }, 400)
}
const user = c.get("user")!
const sessionManager = c.get("sessionManager")
let session
try {
session = await sessionManager.getOrCreate(user.id)
} catch (err) {
console.error("[handleExecuteAction] Failed to create session:", err)
return c.json({ error: "Service unavailable" }, 503)
}
try {
const result = await session.engine.executeAction(sourceId, actionId, params)
return c.json({ result })
} catch (err) {
if (isActionNotFoundError(err)) {
return c.json({ error: err.message }, 404)
}
return c.json({ error: err instanceof Error ? err.message : String(err) }, 400)
}
}
async function handleUpdateCredentials(c: Context<Env>) {
const sourceId = c.req.param("sourceId")
if (!sourceId) {
@@ -228,3 +301,21 @@ async function handleUpdateCredentials(c: Context<Env>) {
return c.body(null, 204)
}
function serializeActions(actions: Record<string, ActionDefinition>) {
const serialized: Record<string, { id: string; description?: string }> = {}
for (const [key, action] of Object.entries(actions)) {
serialized[key] = {
id: action.id,
...(action.description ? { description: action.description } : {}),
}
}
return serialized
}
function isActionNotFoundError(err: unknown): err is Error {
if (!(err instanceof Error)) {
return false
}
return err.message.startsWith("Source not found:") || err.message.startsWith("Action ")
}

View File

@@ -0,0 +1,30 @@
import { WebSearchSource, type WebSearchClient } from "@freya/source-web-search"
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
export type WebSearchSourceProviderOptions =
| { apiKey: string | undefined; client?: never }
| { apiKey?: never; client: WebSearchClient }
export class WebSearchSourceProvider implements FeedSourceProvider {
readonly sourceId = "freya.web-search"
private readonly apiKey: string | undefined
private readonly client: WebSearchClient | undefined
constructor(options: WebSearchSourceProviderOptions) {
this.apiKey = "apiKey" in options ? options.apiKey : undefined
this.client = "client" in options ? options.client : undefined
}
async feedSourceForUser(
_userId: string,
_config: unknown,
_credentials: unknown,
): Promise<WebSearchSource> {
return new WebSearchSource({
apiKey: this.apiKey,
client: this.client,
})
}
}

View File

@@ -56,6 +56,7 @@
"@freya/source-location": "workspace:*",
"@freya/source-tfl": "workspace:*",
"@freya/source-weatherkit": "workspace:*",
"@freya/source-web-search": "workspace:*",
"@openrouter/sdk": "^0.9.11",
"arktype": "^2.1.29",
"better-auth": "^1",
@@ -200,6 +201,14 @@
"arktype": "^2.1.0",
},
},
"packages/freya-source-mcp": {
"name": "@freya/source-mcp",
"version": "0.0.0",
"dependencies": {
"@freya/core": "workspace:*",
"@modelcontextprotocol/sdk": "^1.27.1",
},
},
"packages/freya-source-tfl": {
"name": "@freya/source-tfl",
"version": "0.0.0",
@@ -223,28 +232,18 @@
"arktype": "^2.1.0",
},
},
"packages/freya-source-web-search": {
"name": "@freya/source-web-search",
"version": "0.0.0",
"dependencies": {
"@freya/core": "workspace:*",
"arktype": "^2.1.0",
},
},
},
"packages": {
"@0no-co/graphql.web": ["@0no-co/graphql.web@1.2.0", "", { "peerDependencies": { "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0" }, "optionalPeers": ["graphql"] }, "sha512-/1iHy9TTr63gE1YcR5idjx8UREz1s0kFhydf3bBLCXyqjhkIc6igAzTOx3zPifCwFR87tsh/4Pa9cNts6d2otw=="],
"@freya/backend": ["@freya/backend@workspace:apps/freya-backend"],
"@freya/components": ["@freya/components@workspace:packages/freya-components"],
"@freya/core": ["@freya/core@workspace:packages/freya-core"],
"@freya/feed-enhancers": ["@freya/feed-enhancers@workspace:packages/freya-feed-enhancers"],
"@freya/source-caldav": ["@freya/source-caldav@workspace:packages/freya-source-caldav"],
"@freya/source-google-calendar": ["@freya/source-google-calendar@workspace:packages/freya-source-google-calendar"],
"@freya/source-location": ["@freya/source-location@workspace:packages/freya-source-location"],
"@freya/source-tfl": ["@freya/source-tfl@workspace:packages/freya-source-tfl"],
"@freya/source-weatherkit": ["@freya/source-weatherkit@workspace:packages/freya-source-weatherkit"],
"@alloc/quick-lru": ["@alloc/quick-lru@5.2.0", "", {}, "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="],
"@ark/schema": ["@ark/schema@0.56.0", "", { "dependencies": { "@ark/util": "0.56.0" } }, "sha512-ECg3hox/6Z/nLajxXqNhgPtNdHWC9zNsDyskwO28WinoFEnWow4IsERNz9AnXRhTZJnYIlAJ4uGn3nlLk65vZA=="],
@@ -661,6 +660,26 @@
"@formatjs/intl-localematcher": ["@formatjs/intl-localematcher@0.6.2", "", { "dependencies": { "tslib": "^2.8.0" } }, "sha512-XOMO2Hupl0wdd172Y06h6kLpBz6Dv+J4okPLl4LPtzbr8f66WbIoy4ev98EBuZ6ZK4h5ydTN6XneT4QVpD7cdA=="],
"@freya/backend": ["@freya/backend@workspace:apps/freya-backend"],
"@freya/components": ["@freya/components@workspace:packages/freya-components"],
"@freya/core": ["@freya/core@workspace:packages/freya-core"],
"@freya/feed-enhancers": ["@freya/feed-enhancers@workspace:packages/freya-feed-enhancers"],
"@freya/source-caldav": ["@freya/source-caldav@workspace:packages/freya-source-caldav"],
"@freya/source-google-calendar": ["@freya/source-google-calendar@workspace:packages/freya-source-google-calendar"],
"@freya/source-location": ["@freya/source-location@workspace:packages/freya-source-location"],
"@freya/source-tfl": ["@freya/source-tfl@workspace:packages/freya-source-tfl"],
"@freya/source-weatherkit": ["@freya/source-weatherkit@workspace:packages/freya-source-weatherkit"],
"@freya/source-web-search": ["@freya/source-web-search@workspace:packages/freya-source-web-search"],
"@hapi/hoek": ["@hapi/hoek@9.3.0", "", {}, "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="],
"@hapi/topo": ["@hapi/topo@5.1.0", "", { "dependencies": { "@hapi/hoek": "^9.0.0" } }, "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg=="],
@@ -1529,8 +1548,6 @@
"admin-dashboard": ["admin-dashboard@workspace:apps/admin-dashboard"],
"freya-client": ["freya-client@workspace:apps/freya-client"],
"agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],
"ajv": ["ajv@8.11.0", "", { "dependencies": { "fast-deep-equal": "^3.1.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2", "uri-js": "^4.2.2" } }, "sha512-wGgprdCvMalC0BztXvitD2hC04YffAvtsUn93JbGXYLAtCUO4xd17mCCZQxUOItiBwZvJScWo8NIvQMQ71rdpg=="],
@@ -2167,6 +2184,8 @@
"fresh": ["fresh@0.5.2", "", {}, "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q=="],
"freya-client": ["freya-client@workspace:apps/freya-client"],
"fs-extra": ["fs-extra@11.3.4", "", { "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" } }, "sha512-CTXd6rk/M3/ULNQj8FBqBWHYBVYybQ3VPBw0xGKFe3tuH7ytT6ACnvzpIQ3UZtB8yvUKC2cXn1a+x+5EVQLovA=="],
"fs.realpath": ["fs.realpath@1.0.0", "", {}, "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw=="],
@@ -3921,12 +3940,6 @@
"accepts/negotiator": ["negotiator@0.6.3", "", {}, "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg=="],
"freya-client/@types/react": ["@types/react@19.1.17", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-Qec1E3mhALmaspIrhWt9jkQMNdw6bReVu64mjvhbhq2NFPftLPVr+l1SZgmw/66WwBNpDh7ao5AT6gF5v41PFA=="],
"freya-client/react": ["react@19.1.0", "", {}, "sha512-FS+XFBNvn3GTAWq26joslQgWNoFu08F4kl0J4CgdNKADkdSGXQyTCnKteIAJy96Br6YbpEU1LSzV5dYtjMkMDg=="],
"freya-client/react-dom": ["react-dom@19.1.0", "", { "dependencies": { "scheduler": "^0.26.0" }, "peerDependencies": { "react": "^19.1.0" } }, "sha512-Xs1hdnE+DyKgeHJeJznQmYMIBG3TKIHJJT95Q58nHLSrElKlGQqDTR2HQ9fx5CN/Gk6Vh/kupBTDLU11/nDk/g=="],
"ansi-escapes/type-fest": ["type-fest@0.21.3", "", {}, "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w=="],
"anymatch/picomatch": ["picomatch@2.3.1", "", {}, "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="],
@@ -4069,6 +4082,12 @@
"framer-motion/tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
"freya-client/@types/react": ["@types/react@19.1.17", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-Qec1E3mhALmaspIrhWt9jkQMNdw6bReVu64mjvhbhq2NFPftLPVr+l1SZgmw/66WwBNpDh7ao5AT6gF5v41PFA=="],
"freya-client/react": ["react@19.1.0", "", {}, "sha512-FS+XFBNvn3GTAWq26joslQgWNoFu08F4kl0J4CgdNKADkdSGXQyTCnKteIAJy96Br6YbpEU1LSzV5dYtjMkMDg=="],
"freya-client/react-dom": ["react-dom@19.1.0", "", { "dependencies": { "scheduler": "^0.26.0" }, "peerDependencies": { "react": "^19.1.0" } }, "sha512-Xs1hdnE+DyKgeHJeJznQmYMIBG3TKIHJJT95Q58nHLSrElKlGQqDTR2HQ9fx5CN/Gk6Vh/kupBTDLU11/nDk/g=="],
"giget/pathe": ["pathe@2.0.3", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="],
"glob/minimatch": ["minimatch@3.1.5", "", { "dependencies": { "brace-expansion": "^1.1.7" } }, "sha512-VgjWUsnnT6n+NUk6eZq77zeFdpW2LWDzP6zFGrCbHXiYNul5Dzqk2HHQ5uFH2DNW5Xbp8+jVzaeNt94ssEEl4w=="],
@@ -4559,8 +4578,6 @@
"@typescript-eslint/typescript-estree/minimatch/brace-expansion": ["brace-expansion@5.0.4", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-h+DEnpVvxmfVefa4jFbCf5HdH5YMDXRsmKflpf1pILZWRFlTbJpxeU55nJl4Smt5HQaGzg1o6RHFPJaOqnmBDg=="],
"freya-client/react-dom/scheduler": ["scheduler@0.26.0", "", {}, "sha512-NlHwttCI/l5gCPR3D1nNXtWABUmBwvZpEQiD4IXSbIDq8BzLIK/7Ir5gTFSGZDUu37K5cMNp0hFtzO38sC7gWA=="],
"better-opn/open/define-lazy-prop": ["define-lazy-prop@2.0.0", "", {}, "sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og=="],
"better-opn/open/is-docker": ["is-docker@2.2.1", "", { "bin": { "is-docker": "cli.js" } }, "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ=="],
@@ -4695,6 +4712,8 @@
"finalhandler/debug/ms": ["ms@2.0.0", "", {}, "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="],
"freya-client/react-dom/scheduler": ["scheduler@0.26.0", "", {}, "sha512-NlHwttCI/l5gCPR3D1nNXtWABUmBwvZpEQiD4IXSbIDq8BzLIK/7Ir5gTFSGZDUu37K5cMNp0hFtzO38sC7gWA=="],
"glob/minimatch/brace-expansion": ["brace-expansion@1.1.12", "", { "dependencies": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, "sha512-9T9UjW3r0UW5c1Q7GTwllptXwhvYmEzFhzMfZ9H7FQWt+uZePjZPjBP/W1ZEyZ1twGWom5/56TF4lPcqjnDHcg=="],
"globby/fast-glob/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="],

View File

@@ -0,0 +1,14 @@
{
"name": "@freya/source-mcp",
"version": "0.0.0",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"test": "bun test src/"
},
"dependencies": {
"@freya/core": "workspace:*",
"@modelcontextprotocol/sdk": "^1.27.1"
}
}

View File

@@ -0,0 +1,26 @@
export {
McpSource,
type McpActionMapping,
type McpContextResource,
type McpContextTool,
type McpFeedItem,
type McpFeedItemMapping,
type McpSourceOptions,
} from "./mcp-source"
export {
StreamableHttpMcpClient,
type McpCallToolParams,
type McpCallToolResult,
type McpClient,
type McpHttpHeaders,
type McpListToolsParams,
type McpListToolsResult,
type McpReadResourceParams,
type McpReadResourceResult,
type McpResourceContent,
type McpRequestOptions,
type McpTool,
type McpToolContent,
type StreamableHttpMcpClientOptions,
} from "./mcp-client"

View File

@@ -0,0 +1,275 @@
import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js"
import { describe, expect, test } from "bun:test"
import { StreamableHttpMcpClient, type StreamableHttpMcpClientOptions } from "./mcp-client"
type JsonRpcId = string | number
type FetchLike = NonNullable<
NonNullable<StreamableHttpMcpClientOptions["transportOptions"]>["fetch"]
>
describe("StreamableHttpMcpClient", () => {
test("retries connection after initial connection failure", async () => {
const methods: string[] = []
let initializeAttempts = 0
const fetch: FetchLike = async (_url, init) => {
const method = requestMethod(init)
if (init?.method === "GET") {
return new Response(null, { status: 405, statusText: "Method Not Allowed" })
}
methods.push(method)
switch (method) {
case "initialize":
initializeAttempts += 1
if (initializeAttempts === 1) {
throw new Error("Transient connection failure")
}
return jsonRpcResponse(requestId(init), {
protocolVersion: "2025-06-18",
capabilities: {
tools: {},
},
serverInfo: {
name: "test-mcp",
version: "1.0.0",
},
})
case "notifications/initialized":
return new Response(null, { status: 202, statusText: "Accepted" })
case "tools/list":
return jsonRpcResponse(requestId(init), {
tools: [],
})
default:
throw new Error(`Unexpected MCP method: ${method}`)
}
}
const client = new StreamableHttpMcpClient({
url: "https://example.test/mcp",
transportOptions: { fetch },
})
await expectRejectedMessage(client.listTools(), "Transient connection failure")
const result = await client.listTools()
await client.close()
expect(result.tools).toEqual([])
expect(initializeAttempts).toBe(2)
expect(methods).toEqual(["initialize", "initialize", "notifications/initialized", "tools/list"])
})
test("applies timeout to initial connection request", async () => {
const methods: string[] = []
let initializeAttempts = 0
const fetch: FetchLike = async (_url, init) => {
if (init?.method === "GET") {
return new Response(null, { status: 405, statusText: "Method Not Allowed" })
}
const method = requestMethod(init)
methods.push(method)
switch (method) {
case "initialize":
initializeAttempts += 1
if (initializeAttempts === 1) {
return new Promise<Response>(() => {})
}
return jsonRpcResponse(requestId(init), {
protocolVersion: "2025-06-18",
capabilities: {
tools: {},
},
serverInfo: {
name: "test-mcp",
version: "1.0.0",
},
})
case "notifications/cancelled":
case "notifications/initialized":
return new Response(null, { status: 202, statusText: "Accepted" })
case "tools/list":
return jsonRpcResponse(requestId(init), {
tools: [],
})
default:
throw new Error(`Unexpected MCP method: ${method}`)
}
}
const client = new StreamableHttpMcpClient({
url: "https://example.test/mcp",
timeoutMs: 1,
transportOptions: { fetch },
})
await expectMcpErrorCode(client.listTools(), ErrorCode.RequestTimeout)
const result = await client.listTools()
await client.close()
expect(result.tools).toEqual([])
expect(initializeAttempts).toBe(2)
expect(methods).toEqual([
"initialize",
"notifications/cancelled",
"initialize",
"notifications/initialized",
"tools/list",
])
})
test("applies caller signal to initial connection request", async () => {
const methods: string[] = []
const fetch = createSuccessfulFetch(methods)
const controller = new AbortController()
controller.abort(new Error("Caller aborted"))
const client = new StreamableHttpMcpClient({
url: "https://example.test/mcp",
transportOptions: { fetch },
})
await expectRejectedMessageContaining(
client.listTools(undefined, { signal: controller.signal }),
"Caller aborted",
)
expect(methods).toEqual([])
const result = await client.listTools()
await client.close()
expect(result.tools).toEqual([])
expect(methods).toEqual(["initialize", "notifications/initialized", "tools/list"])
})
})
function createSuccessfulFetch(methods: string[]): FetchLike {
return async (_url, init) => {
if (init?.method === "GET") {
return new Response(null, { status: 405, statusText: "Method Not Allowed" })
}
const method = requestMethod(init)
methods.push(method)
switch (method) {
case "initialize":
return jsonRpcResponse(requestId(init), {
protocolVersion: "2025-06-18",
capabilities: {
tools: {},
},
serverInfo: {
name: "test-mcp",
version: "1.0.0",
},
})
case "notifications/initialized":
return new Response(null, { status: 202, statusText: "Accepted" })
case "tools/list":
return jsonRpcResponse(requestId(init), {
tools: [],
})
default:
throw new Error(`Unexpected MCP method: ${method}`)
}
}
}
function jsonRpcResponse(id: JsonRpcId, result: Record<string, unknown>): Response {
return Response.json({
jsonrpc: "2.0",
id,
result,
})
}
function requestMethod(init: RequestInit | undefined): string {
const request = requestBody(init)
const method = request.method
if (typeof method !== "string") {
throw new Error("Expected JSON-RPC request method")
}
return method
}
function requestId(init: RequestInit | undefined): JsonRpcId {
const request = requestBody(init)
const id = request.id
if (typeof id !== "string" && typeof id !== "number") {
throw new Error("Expected JSON-RPC request id")
}
return id
}
function requestBody(init: RequestInit | undefined): Record<string, unknown> {
const body = init?.body
if (typeof body !== "string") {
throw new Error("Expected string request body")
}
const value: unknown = JSON.parse(body)
if (!isRecord(value)) {
throw new Error("Expected object request body")
}
return value
}
async function expectRejectedMessage(promise: Promise<unknown>, message: string): Promise<void> {
try {
await promise
} catch (error) {
expect(error).toBeInstanceOf(Error)
if (error instanceof Error) {
expect(error.message).toBe(message)
return
}
throw new Error("Expected promise to reject with an Error")
}
throw new Error(`Expected promise to reject with message: ${message}`)
}
async function expectMcpErrorCode(promise: Promise<unknown>, code: ErrorCode): Promise<void> {
try {
await promise
} catch (error) {
expect(error).toBeInstanceOf(McpError)
if (error instanceof McpError) {
expect(error.code).toBe(code)
return
}
throw new Error("Expected promise to reject with an McpError")
}
throw new Error(`Expected promise to reject with MCP error code: ${code}`)
}
async function expectRejectedMessageContaining(
promise: Promise<unknown>,
message: string,
): Promise<void> {
try {
await promise
} catch (error) {
expect(error).toBeInstanceOf(Error)
if (error instanceof Error) {
expect(error.message).toContain(message)
return
}
throw new Error("Expected promise to reject with an Error")
}
throw new Error(`Expected promise to reject with message containing: ${message}`)
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value)
}

View File

@@ -0,0 +1,276 @@
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import {
StreamableHTTPClientTransport,
type StreamableHTTPClientTransportOptions,
} from "@modelcontextprotocol/sdk/client/streamableHttp.js"
export interface McpRequestOptions {
readonly signal?: AbortSignal
readonly timeout?: number
}
export interface McpListToolsParams {
readonly cursor?: string
}
export interface McpReadResourceParams {
readonly uri: string
}
export interface McpCallToolParams {
readonly name: string
readonly arguments?: Record<string, unknown>
}
export interface McpTool {
readonly name: string
readonly title?: string
readonly description?: string
readonly inputSchema?: {
readonly type: "object"
readonly properties?: Record<string, object>
readonly required?: string[]
readonly [key: string]: unknown
}
readonly outputSchema?: {
readonly type: "object"
readonly properties?: Record<string, object>
readonly required?: string[]
readonly [key: string]: unknown
}
readonly annotations?: {
readonly title?: string
readonly readOnlyHint?: boolean
readonly destructiveHint?: boolean
readonly idempotentHint?: boolean
readonly openWorldHint?: boolean
}
readonly _meta?: Record<string, unknown>
}
export interface McpListToolsResult {
readonly tools: readonly McpTool[]
readonly nextCursor?: string
}
export type McpResourceContent = McpTextResourceContent | McpBlobResourceContent
export interface McpTextResourceContent {
readonly uri: string
readonly mimeType?: string
readonly text: string
readonly _meta?: Record<string, unknown>
}
export interface McpBlobResourceContent {
readonly uri: string
readonly mimeType?: string
readonly blob: string
readonly _meta?: Record<string, unknown>
}
export interface McpReadResourceResult {
readonly contents: readonly McpResourceContent[]
readonly _meta?: Record<string, unknown>
}
export type McpToolContent =
| McpToolTextContent
| McpToolImageContent
| McpToolAudioContent
| McpToolResourceContent
| McpToolResourceLinkContent
export interface McpToolTextContent {
readonly type: "text"
readonly text: string
readonly _meta?: Record<string, unknown>
}
export interface McpToolImageContent {
readonly type: "image"
readonly data: string
readonly mimeType: string
readonly _meta?: Record<string, unknown>
}
export interface McpToolAudioContent {
readonly type: "audio"
readonly data: string
readonly mimeType: string
readonly _meta?: Record<string, unknown>
}
export interface McpToolResourceContent {
readonly type: "resource"
readonly resource: McpResourceContent
readonly _meta?: Record<string, unknown>
}
export interface McpToolResourceLinkContent {
readonly type: "resource_link"
readonly uri: string
readonly name: string
readonly title?: string
readonly description?: string
readonly mimeType?: string
readonly _meta?: Record<string, unknown>
}
export interface McpCallToolResult {
readonly content?: readonly McpToolContent[]
readonly structuredContent?: Record<string, unknown>
readonly toolResult?: unknown
readonly isError?: boolean
readonly _meta?: Record<string, unknown>
readonly [key: string]: unknown
}
export interface McpClient {
listTools(params?: McpListToolsParams, options?: McpRequestOptions): Promise<McpListToolsResult>
readResource(
params: McpReadResourceParams,
options?: McpRequestOptions,
): Promise<McpReadResourceResult>
callTool(params: McpCallToolParams, options?: McpRequestOptions): Promise<McpCallToolResult>
close?(): Promise<void>
}
export type McpHttpHeaders =
| Headers
| Record<string, string>
| readonly (readonly [string, string])[]
export interface StreamableHttpMcpClientOptions {
readonly url: string | URL
readonly name?: string
readonly version?: string
readonly timeoutMs?: number
readonly headers?: McpHttpHeaders | (() => Promise<McpHttpHeaders>)
readonly requestInit?: RequestInit
readonly transportOptions?: Omit<StreamableHTTPClientTransportOptions, "requestInit">
}
export class StreamableHttpMcpClient implements McpClient {
private clientPromise: Promise<Client> | null = null
constructor(private readonly options: StreamableHttpMcpClientOptions) {}
async listTools(
params?: McpListToolsParams,
options?: McpRequestOptions,
): Promise<McpListToolsResult> {
const request = requestOptions(this.options.timeoutMs, options)
const client = await this.client(request)
return client.listTools(params, request)
}
async readResource(
params: McpReadResourceParams,
options?: McpRequestOptions,
): Promise<McpReadResourceResult> {
const request = requestOptions(this.options.timeoutMs, options)
const client = await this.client(request)
return client.readResource(params, request)
}
async callTool(
params: McpCallToolParams,
options?: McpRequestOptions,
): Promise<McpCallToolResult> {
const request = requestOptions(this.options.timeoutMs, options)
const client = await this.client(request)
return client.callTool(params, undefined, request)
}
async close(): Promise<void> {
if (!this.clientPromise) return
const client = await this.clientPromise
this.clientPromise = null
await client.close()
}
private client(options?: McpRequestOptions): Promise<Client> {
if (!this.clientPromise) {
const promise = this.connect(options)
this.clientPromise = promise
void promise.catch(() => {
if (this.clientPromise === promise) {
this.clientPromise = null
}
})
}
return this.clientPromise
}
private async connect(options?: McpRequestOptions): Promise<Client> {
const client = new Client({
name: this.options.name ?? "freya-source-mcp",
version: this.options.version ?? "0.0.0",
})
const transport = new StreamableHTTPClientTransport(toUrl(this.options.url), {
...this.options.transportOptions,
requestInit: await mergeRequestInit(this.options.requestInit, this.options.headers),
})
await client.connect(transport, options)
return client
}
}
function requestOptions(
defaultTimeoutMs: number | undefined,
options: McpRequestOptions | undefined,
): McpRequestOptions | undefined {
if (defaultTimeoutMs === undefined && options === undefined) {
return undefined
}
return {
...(defaultTimeoutMs === undefined ? {} : { timeout: defaultTimeoutMs }),
...options,
}
}
function toUrl(value: string | URL): URL {
if (value instanceof URL) return value
return new URL(value)
}
async function mergeRequestInit(
requestInit: RequestInit | undefined,
headers: McpHttpHeaders | (() => Promise<McpHttpHeaders>) | undefined,
): Promise<RequestInit | undefined> {
if (!requestInit && !headers) return undefined
const mergedHeaders = new Headers(requestInit?.headers)
const extraHeaders = typeof headers === "function" ? await headers() : headers
if (extraHeaders) {
applyHeaders(mergedHeaders, extraHeaders)
}
return {
...requestInit,
headers: mergedHeaders,
}
}
function applyHeaders(target: Headers, headers: McpHttpHeaders): void {
if (headers instanceof Headers) {
headers.forEach((value, key) => {
target.set(key, value)
})
return
}
if (Array.isArray(headers)) {
for (const [key, value] of headers) {
target.set(key, value)
}
return
}
for (const [key, value] of Object.entries(headers)) {
target.set(key, value)
}
}

View File

@@ -0,0 +1,355 @@
import { Context, UnknownActionError, contextKey, type ActionDefinition } from "@freya/core"
import { describe, expect, test } from "bun:test"
import type {
McpCallToolParams,
McpCallToolResult,
McpClient,
McpListToolsParams,
McpListToolsResult,
McpReadResourceParams,
McpReadResourceResult,
McpTool,
} from "./mcp-client"
import { McpSource } from "./mcp-source"
class FakeMcpClient implements McpClient {
tools: readonly McpTool[] = []
readonly resources = new Map<string, McpReadResourceResult>()
readonly toolResults = new Map<string, McpCallToolResult>()
readonly listToolParams: Array<McpListToolsParams | undefined> = []
readonly readResourceParams: McpReadResourceParams[] = []
readonly callToolParams: McpCallToolParams[] = []
async listTools(params?: McpListToolsParams): Promise<McpListToolsResult> {
this.listToolParams.push(params)
return { tools: this.tools }
}
async readResource(params: McpReadResourceParams): Promise<McpReadResourceResult> {
this.readResourceParams.push(params)
const result = this.resources.get(params.uri)
if (!result) {
throw new Error(`Missing resource: ${params.uri}`)
}
return result
}
async callTool(params: McpCallToolParams): Promise<McpCallToolResult> {
this.callToolParams.push(params)
const result = this.toolResults.get(params.name)
if (!result) {
throw new Error(`Missing tool result: ${params.name}`)
}
return result
}
}
describe("McpSource", () => {
test("reads configured MCP resources into context", async () => {
const NotificationsKey = contextKey<{ unread: number }>("com.example.mcp", "notifications")
const client = new FakeMcpClient()
client.resources.set("mcp://notifications", {
contents: [
{
uri: "mcp://notifications",
mimeType: "application/json",
text: JSON.stringify({ unread: 3 }),
},
],
})
const source = new McpSource({
id: "com.example.mcp",
client,
resources: [
{
uri: "mcp://notifications",
contextKey: NotificationsKey,
},
],
})
const context = new Context()
const entries = await source.fetchContext(context)
context.set(entries ?? [])
expect(context.get(NotificationsKey)).toEqual({ unread: 3 })
expect(client.readResourceParams).toEqual([{ uri: "mcp://notifications" }])
})
test("calls configured MCP tools into context", async () => {
const ViewerKey = contextKey<{ name: string }>("com.example.mcp", "viewer")
const client = new FakeMcpClient()
client.toolResults.set("viewer", {
structuredContent: { name: "Kenneth" },
})
const source = new McpSource({
id: "com.example.mcp",
client,
contextTools: [
{
tool: "viewer",
contextKey: ViewerKey,
},
],
})
const context = new Context()
const entries = await source.fetchContext(context)
context.set(entries ?? [])
expect(context.get(ViewerKey)).toEqual({ name: "Kenneth" })
expect(client.callToolParams).toEqual([{ name: "viewer", arguments: {} }])
})
test("projects configured MCP resources into feed items", async () => {
const client = new FakeMcpClient()
client.resources.set("mcp://alerts", {
contents: [
{
uri: "mcp://alerts",
text: JSON.stringify([{ title: "Build failed" }]),
},
],
})
const source = new McpSource({
id: "com.example.mcp",
client,
feedItems: [
{
kind: "resource",
uri: "mcp://alerts",
type: "mcp-alerts",
},
],
})
const context = new Context(new Date("2026-01-01T00:00:00.000Z"))
const items = await source.fetchItems(context)
expect(items).toHaveLength(1)
expect(items[0]).toMatchObject({
sourceId: "com.example.mcp",
type: "mcp-alerts",
timestamp: context.time,
data: {
kind: "mcp-resource",
uri: "mcp://alerts",
value: [{ title: "Build failed" }],
},
})
})
test("lists allowlisted MCP tools as Freya actions", async () => {
const client = new FakeMcpClient()
client.tools = [
{
name: "github.create_issue",
description: "Create a GitHub issue",
inputSchema: { type: "object" },
},
{
name: "github.delete_repo",
description: "Delete a repository",
inputSchema: { type: "object" },
},
]
const source = new McpSource({
id: "com.example.github",
client,
actions: {
"create-issue": {
tool: "github.create_issue",
},
},
})
const actions = await source.listActions()
expect(Object.keys(actions)).toEqual(["create-issue"])
expect(actions["create-issue"]).toMatchObject({
id: "create-issue",
description: "Create a GitHub issue",
})
})
test("executes allowlisted MCP tools as Freya actions", async () => {
const client = new FakeMcpClient()
client.toolResults.set("github.create_issue", {
structuredContent: { issueNumber: 42 },
})
const source = new McpSource({
id: "com.example.github",
client,
actions: {
"create-issue": {
tool: "github.create_issue",
},
},
})
const result = await source.executeAction("create-issue", { title: "Bug" })
expect(result).toEqual({ issueNumber: 42 })
expect(client.callToolParams).toEqual([
{
name: "github.create_issue",
arguments: { title: "Bug" },
},
])
})
test("validates mapped action input before calling MCP tools", async () => {
const client = new FakeMcpClient()
client.toolResults.set("github.create_issue", {
structuredContent: { issueNumber: 42 },
})
const source = new McpSource({
id: "com.example.github",
client,
actions: {
"create-issue": {
tool: "github.create_issue",
input: createIssueInputSchema(),
},
},
})
await expectRejectedMessage(
source.executeAction("create-issue", { title: 42 }),
'Invalid MCP action "create-issue" params: title: Expected string',
)
expect(client.callToolParams).toEqual([])
})
test("rejects MCP tools that are not allowlisted as actions", async () => {
const client = new FakeMcpClient()
client.tools = [
{
name: "github.create_issue",
description: "Create a GitHub issue",
inputSchema: { type: "object" },
},
{
name: "github.delete_repo",
description: "Delete a repository",
inputSchema: { type: "object" },
},
]
client.toolResults.set("github.delete_repo", {
structuredContent: { deleted: true },
})
const source = new McpSource({
id: "com.example.github",
client,
actions: {
"create-issue": {
tool: "github.create_issue",
},
},
})
const actions = await source.listActions()
expect(Object.keys(actions)).toEqual(["create-issue"])
await expectUnknownActionError(source.executeAction("github.delete_repo", {}))
expect(client.callToolParams).toEqual([])
})
test("rejects unknown actions", async () => {
const source = new McpSource({
id: "com.example.mcp",
client: new FakeMcpClient(),
actions: {
"known-action": {
tool: "known_tool",
},
},
})
await expectUnknownActionError(source.executeAction("unknown-action", {}))
})
test("requires object params for default action argument mapping", async () => {
const source = new McpSource({
id: "com.example.mcp",
client: new FakeMcpClient(),
actions: {
"known-action": {
tool: "known_tool",
},
},
})
await expectRejectedMessage(
source.executeAction("known-action", "bad params"),
'MCP action "known-action" requires object params',
)
})
})
async function expectUnknownActionError(promise: Promise<unknown>): Promise<void> {
try {
await promise
} catch (error) {
expect(error).toBeInstanceOf(UnknownActionError)
return
}
throw new Error("Expected promise to reject with UnknownActionError")
}
async function expectRejectedMessage(promise: Promise<unknown>, message: string): Promise<void> {
try {
await promise
} catch (error) {
expect(error).toBeInstanceOf(Error)
if (error instanceof Error) {
expect(error.message).toBe(message)
return
}
throw new Error("Expected promise to reject with an Error")
}
throw new Error(`Expected promise to reject with message: ${message}`)
}
function createIssueInputSchema(): NonNullable<ActionDefinition["input"]> {
return {
"~standard": {
version: 1,
vendor: "freya-test",
validate(value: unknown) {
if (!isRecord(value)) {
return {
issues: [{ message: "Expected object" }],
}
}
if (typeof value.title !== "string") {
return {
issues: [{ message: "Expected string", path: ["title"] }],
}
}
return {
value: {
title: value.title.trim(),
},
}
},
},
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value)
}

View File

@@ -0,0 +1,624 @@
import type {
ActionDefinition,
ContextEntry,
ContextKey,
FeedItem,
FeedItemSignals,
FeedSource,
Slot,
} from "@freya/core"
import { Context, UnknownActionError } from "@freya/core"
import {
StreamableHttpMcpClient,
type McpCallToolResult,
type McpClient,
type McpHttpHeaders,
type McpResourceContent,
type McpTool,
type McpToolContent,
type StreamableHttpMcpClientOptions,
} from "./mcp-client"
export type McpFeedItem = FeedItem<string, Record<string, unknown>>
/**
* Configuration for an MCP-backed `FeedSource`.
*
* The source is intentionally projection-based: remote MCP resources/tools are
* only exposed to Freya when listed here as context entries, feed items, or
* allowlisted actions.
*/
export interface McpSourceOptions {
/** Stable Freya source identifier, for example `freya.github` or `freya.discord`. */
readonly id: string
/** Streamable HTTP MCP endpoint. Required unless `client` or `clientFactory` is provided. */
readonly url?: string | URL
/** Client name advertised during MCP initialization. */
readonly clientName?: string
/** Client version advertised during MCP initialization. */
readonly clientVersion?: string
/** Default timeout, in milliseconds, for MCP connection and request calls. */
readonly timeoutMs?: number
/** Static or lazily-resolved HTTP headers for the MCP transport. */
readonly headers?: McpHttpHeaders | (() => Promise<McpHttpHeaders>)
/** Additional `fetch` options merged into the MCP transport request init. */
readonly requestInit?: RequestInit
/** Additional transport options forwarded to the MCP SDK streamable HTTP transport. */
readonly transportOptions?: StreamableHttpMcpClientOptions["transportOptions"]
/** Preconfigured MCP client, primarily useful for tests or custom transports. */
readonly client?: McpClient
/** Lazy MCP client factory, useful when client construction depends on runtime state. */
readonly clientFactory?: () => McpClient | Promise<McpClient>
/** Freya source dependencies used by the context graph scheduler. */
readonly dependencies?: readonly string[]
/** MCP resources to read and write into Freya context keys. */
readonly resources?: readonly McpContextResource[]
/** MCP tools to call and write into Freya context keys. */
readonly contextTools?: readonly McpContextTool[]
/** MCP resources or tools to project into feed items. */
readonly feedItems?: readonly McpFeedItemMapping[]
/** Freya action IDs mapped to explicit, allowlisted MCP tools. */
readonly actions?: Record<string, McpActionMapping>
}
export interface McpContextResource<T = unknown> {
readonly uri: string
readonly contextKey: ContextKey<T>
readonly map?: (contents: readonly McpResourceContent[], context: Context) => T | null
}
export type McpToolArguments =
| Record<string, unknown>
| ((context: Context) => Record<string, unknown>)
export interface McpContextTool<T = unknown> {
readonly tool: string
readonly arguments?: McpToolArguments
readonly contextKey: ContextKey<T>
readonly map?: (result: McpCallToolResult, context: Context) => T | null
}
/**
* Mapping from a Freya action ID to an MCP tool call.
*
* Only actions declared in `McpSourceOptions.actions` can be executed through
* the source. The map is keyed by Freya action ID, while `tool` names the
* remote MCP tool to call.
*/
export interface McpActionMapping {
/** Remote MCP tool name to call when the Freya action is executed. */
readonly tool: string
/** Optional action description; falls back to the MCP tool description/title when omitted. */
readonly description?: string
/** Optional Standard Schema input validator exposed on the Freya action and checked locally. */
readonly input?: ActionDefinition["input"]
/** Static MCP arguments or a mapper from validated Freya action params to MCP arguments. */
readonly arguments?: Record<string, unknown> | ((params: unknown) => Record<string, unknown>)
/** Optional mapper from raw MCP tool result to the Freya action return value. */
readonly mapResult?: (result: McpCallToolResult) => unknown
}
export type McpFeedItemMapping = McpResourceFeedItemMapping | McpToolFeedItemMapping
export type McpFeedPayload = McpResourceFeedPayload | McpToolFeedPayload
export interface McpFeedItemBaseMapping {
readonly type: string
readonly id?: string | ((payload: McpFeedPayload, context: Context) => string)
readonly mapData?: (payload: McpFeedPayload, context: Context) => Record<string, unknown> | null
readonly signals?:
| FeedItemSignals
| ((payload: McpFeedPayload, context: Context) => FeedItemSignals | undefined)
readonly slots?:
| Record<string, Slot>
| ((payload: McpFeedPayload, context: Context) => Record<string, Slot>)
}
export interface McpResourceFeedItemMapping extends McpFeedItemBaseMapping {
readonly kind: "resource"
readonly uri: string
}
export interface McpToolFeedItemMapping extends McpFeedItemBaseMapping {
readonly kind: "tool"
readonly tool: string
readonly arguments?: McpToolArguments
}
export interface McpResourceFeedPayload {
readonly kind: "resource"
readonly uri: string
readonly contents: readonly McpResourceContent[]
readonly value: unknown
}
export interface McpToolFeedPayload {
readonly kind: "tool"
readonly tool: string
readonly result: McpCallToolResult
readonly value: unknown
}
/**
* FeedSource backed by a remote MCP server.
*
* The source intentionally uses explicit projections. A remote MCP server can
* expose many resources and tools, but only configured resources/tools enter the
* Freya context graph or action surface.
*/
export class McpSource implements FeedSource<McpFeedItem> {
readonly id: string
readonly dependencies: readonly string[] | undefined
private clientPromise: Promise<McpClient> | null = null
constructor(private readonly options: McpSourceOptions) {
this.id = options.id
this.dependencies = options.dependencies
if (!options.client && !options.clientFactory && !options.url) {
throw new Error("McpSource requires either a client, clientFactory, or remote url")
}
}
async listActions(): Promise<Record<string, ActionDefinition>> {
const actionMappings = this.options.actions
if (!actionMappings) {
return {}
}
const tools = await this.toolsByName()
const actions: Record<string, ActionDefinition> = {}
for (const [actionId, mapping] of Object.entries(actionMappings)) {
const tool = tools.get(mapping.tool)
if (!tool) {
throw new Error(
`Configured MCP action "${actionId}" maps to missing tool "${mapping.tool}"`,
)
}
const description = mapping.description ?? tool.description ?? tool.title
actions[actionId] = {
id: actionId,
...(description ? { description } : {}),
...(mapping.input ? { input: mapping.input } : {}),
}
}
return actions
}
async executeAction(actionId: string, params: unknown): Promise<unknown> {
const mapping = this.options.actions?.[actionId]
if (!mapping) {
throw new UnknownActionError(actionId)
}
const validatedParams = await validateActionInput(actionId, params, mapping)
const client = await this.client()
const result = await client.callTool(
{
name: mapping.tool,
arguments: resolveActionArguments(actionId, validatedParams, mapping),
},
this.requestOptions(),
)
if (result.isError) {
throw new Error(`MCP tool "${mapping.tool}" returned an error: ${toolResultText(result)}`)
}
return mapping.mapResult ? mapping.mapResult(result) : toolResultValue(result)
}
async fetchContext(context: Context): Promise<readonly ContextEntry[] | null> {
const resources = this.options.resources ?? []
const contextTools = this.options.contextTools ?? []
if (resources.length === 0 && contextTools.length === 0) {
return null
}
const entries: ContextEntry[] = []
const client = await this.client()
for (const resource of resources) {
const result = await client.readResource({ uri: resource.uri }, this.requestOptions())
const value = resource.map
? resource.map(result.contents, context)
: resourceContentsValue(result.contents)
if (value !== null) {
entries.push([resource.contextKey, value])
}
}
for (const tool of contextTools) {
const result = await client.callTool(
{
name: tool.tool,
arguments: resolveToolArguments(tool.arguments, context),
},
this.requestOptions(),
)
if (result.isError) {
throw new Error(`MCP tool "${tool.tool}" returned an error: ${toolResultText(result)}`)
}
const value = tool.map ? tool.map(result, context) : toolResultValue(result)
if (value !== null) {
entries.push([tool.contextKey, value])
}
}
return entries.length > 0 ? entries : null
}
async fetchItems(context: Context): Promise<McpFeedItem[]> {
const mappings = this.options.feedItems ?? []
if (mappings.length === 0) {
return []
}
const client = await this.client()
const items: McpFeedItem[] = []
for (const mapping of mappings) {
const payload = await this.fetchFeedPayload(client, mapping, context)
const data = mapping.mapData
? mapping.mapData(payload, context)
: defaultFeedItemData(payload)
if (data === null) {
continue
}
items.push({
id: resolveFeedItemId(this.id, mapping, payload, context),
sourceId: this.id,
type: mapping.type,
timestamp: context.time,
data,
...resolveSignals(mapping, payload, context),
...resolveSlots(mapping, payload, context),
})
}
return items
}
async close(): Promise<void> {
if (!this.clientPromise) return
const client = await this.clientPromise
this.clientPromise = null
await client.close?.()
}
private async fetchFeedPayload(
client: McpClient,
mapping: McpFeedItemMapping,
context: Context,
): Promise<McpFeedPayload> {
switch (mapping.kind) {
case "resource": {
const result = await client.readResource({ uri: mapping.uri }, this.requestOptions())
return {
kind: "resource",
uri: mapping.uri,
contents: result.contents,
value: resourceContentsValue(result.contents),
}
}
case "tool": {
const result = await client.callTool(
{
name: mapping.tool,
arguments: resolveToolArguments(mapping.arguments, context),
},
this.requestOptions(),
)
if (result.isError) {
throw new Error(`MCP tool "${mapping.tool}" returned an error: ${toolResultText(result)}`)
}
return {
kind: "tool",
tool: mapping.tool,
result,
value: toolResultValue(result),
}
}
}
}
private async toolsByName(): Promise<Map<string, McpTool>> {
const client = await this.client()
const tools = new Map<string, McpTool>()
let cursor: string | undefined
do {
const result = await client.listTools(cursor ? { cursor } : undefined, this.requestOptions())
for (const tool of result.tools) {
tools.set(tool.name, tool)
}
cursor = result.nextCursor
} while (cursor)
return tools
}
private client(): Promise<McpClient> {
if (!this.clientPromise) {
this.clientPromise = this.createClient()
}
return this.clientPromise
}
private async createClient(): Promise<McpClient> {
if (this.options.client) {
return this.options.client
}
if (this.options.clientFactory) {
return this.options.clientFactory()
}
return new StreamableHttpMcpClient({
url: this.options.url!,
name: this.options.clientName,
version: this.options.clientVersion,
timeoutMs: this.options.timeoutMs,
headers: this.options.headers,
requestInit: this.options.requestInit,
transportOptions: this.options.transportOptions,
})
}
private requestOptions(): { timeout?: number } | undefined {
if (this.options.timeoutMs === undefined) {
return undefined
}
return { timeout: this.options.timeoutMs }
}
}
async function validateActionInput(
actionId: string,
params: unknown,
mapping: McpActionMapping,
): Promise<unknown> {
if (!mapping.input) {
return params
}
const result = await mapping.input["~standard"].validate(params)
if (result.issues) {
throw new Error(
`Invalid MCP action "${actionId}" params: ${formatStandardSchemaIssues(result.issues)}`,
)
}
return result.value
}
function resolveToolArguments(
args: McpToolArguments | undefined,
context: Context,
): Record<string, unknown> {
if (!args) return {}
if (typeof args === "function") {
return args(context)
}
return args
}
function resolveActionArguments(
actionId: string,
params: unknown,
mapping: McpActionMapping,
): Record<string, unknown> {
if (mapping.arguments) {
if (typeof mapping.arguments === "function") {
return mapping.arguments(params)
}
return mapping.arguments
}
if (params === undefined || params === null) {
return {}
}
if (!isRecord(params)) {
throw new Error(`MCP action "${actionId}" requires object params`)
}
return params
}
function resolveFeedItemId(
sourceId: string,
mapping: McpFeedItemMapping,
payload: McpFeedPayload,
context: Context,
): string {
if (typeof mapping.id === "function") {
return mapping.id(payload, context)
}
if (mapping.id) {
return mapping.id
}
const identifier = payload.kind === "resource" ? payload.uri : payload.tool
return `${sourceId}-${mapping.type}-${slug(identifier)}`
}
function resolveSignals(
mapping: McpFeedItemMapping,
payload: McpFeedPayload,
context: Context,
): { signals?: FeedItemSignals } {
if (!mapping.signals) return {}
const signals =
typeof mapping.signals === "function" ? mapping.signals(payload, context) : mapping.signals
return signals ? { signals } : {}
}
function resolveSlots(
mapping: McpFeedItemMapping,
payload: McpFeedPayload,
context: Context,
): { slots?: Record<string, Slot> } {
if (!mapping.slots) return {}
const slots =
typeof mapping.slots === "function" ? mapping.slots(payload, context) : mapping.slots
return { slots }
}
function defaultFeedItemData(payload: McpFeedPayload): Record<string, unknown> {
switch (payload.kind) {
case "resource":
return {
kind: "mcp-resource",
uri: payload.uri,
value: payload.value,
}
case "tool":
return {
kind: "mcp-tool",
tool: payload.tool,
value: payload.value,
}
}
}
function resourceContentsValue(contents: readonly McpResourceContent[]): unknown {
const values = contents.map(resourceContentValue)
if (values.length === 1) {
return values[0]
}
return values
}
function resourceContentValue(content: McpResourceContent): unknown {
if ("text" in content) {
return parseTextValue(content.text, content.mimeType)
}
return {
uri: content.uri,
...(content.mimeType ? { mimeType: content.mimeType } : {}),
blob: content.blob,
}
}
function toolResultValue(result: McpCallToolResult): unknown {
if (result.structuredContent) {
return result.structuredContent
}
if ("toolResult" in result) {
return result.toolResult
}
if (result.content) {
const values = result.content.map(toolContentValue)
if (values.length === 1) {
return values[0]
}
return values
}
return result
}
function toolContentValue(content: McpToolContent): unknown {
switch (content.type) {
case "text":
return parseTextValue(content.text)
case "resource":
return resourceContentValue(content.resource)
case "resource_link":
return {
type: content.type,
uri: content.uri,
name: content.name,
...(content.title ? { title: content.title } : {}),
...(content.description ? { description: content.description } : {}),
...(content.mimeType ? { mimeType: content.mimeType } : {}),
}
case "image":
case "audio":
return {
type: content.type,
data: content.data,
mimeType: content.mimeType,
}
}
}
function toolResultText(result: McpCallToolResult): string {
const value = toolResultValue(result)
if (typeof value === "string") {
return value
}
return JSON.stringify(value)
}
function parseTextValue(text: string, mimeType?: string): unknown {
if (shouldParseJson(text, mimeType)) {
try {
return JSON.parse(text)
} catch {
return text
}
}
return text
}
function shouldParseJson(text: string, mimeType?: string): boolean {
if (mimeType?.includes("json")) {
return true
}
const trimmed = text.trim()
return trimmed.startsWith("{") || trimmed.startsWith("[")
}
function slug(value: string): string {
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "")
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value)
}
function formatStandardSchemaIssues(
issues: readonly {
readonly message: string
readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[]
}[],
): string {
return issues.map(formatStandardSchemaIssue).join("; ")
}
function formatStandardSchemaIssue(issue: {
readonly message: string
readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[]
}): string {
const path = issue.path?.map(formatStandardSchemaPathSegment).join(".")
return path ? `${path}: ${issue.message}` : issue.message
}
function formatStandardSchemaPathSegment(
segment: PropertyKey | { readonly key: PropertyKey },
): string {
if (typeof segment === "object" && segment !== null && "key" in segment) {
return String(segment.key)
}
return String(segment)
}

View File

@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src"]
}

View File

@@ -0,0 +1,14 @@
{
"name": "@freya/source-web-search",
"version": "0.0.0",
"type": "module",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"test": "bun test ."
},
"dependencies": {
"@freya/core": "workspace:*",
"arktype": "^2.1.0"
}
}

View File

@@ -0,0 +1,97 @@
import { describe, expect, test } from "bun:test"
import { ExaSearchClient } from "./exa-client.ts"
describe("ExaSearchClient", () => {
test("maps request and response", async () => {
const originalFetch = globalThis.fetch
let requestUrl = ""
let requestHeaders: Headers
let requestBody: unknown
globalThis.fetch = (async (
input: Parameters<typeof fetch>[0],
init?: Parameters<typeof fetch>[1],
) => {
requestUrl = String(input)
requestHeaders = new Headers(init?.headers)
requestBody = JSON.parse(String(init?.body))
return new Response(
JSON.stringify({
requestId: "exa-request-1",
results: [
{
id: "result-1",
url: "https://example.com",
title: "Example",
publishedDate: "2026-01-01T00:00:00.000Z",
author: "Author",
image: "https://example.com/image.png",
favicon: "https://example.com/favicon.ico",
highlights: ["A useful passage"],
highlightScores: [0.7],
summary: "Summary",
},
],
}),
{ status: 200 },
)
}) as unknown as typeof fetch
try {
const client = new ExaSearchClient("api-key", "https://api.example.test")
const result = await client.search({
query: "test query",
numResults: 3,
includeDomains: ["example.com"],
highlights: false,
})
expect(requestUrl).toBe("https://api.example.test/search")
expect(requestHeaders!.get("x-api-key")).toBe("api-key")
expect(requestBody).toEqual({
query: "test query",
numResults: 3,
includeDomains: ["example.com"],
contents: { highlights: false },
})
expect(result).toEqual({
query: "test query",
requestId: "exa-request-1",
results: [
{
id: "result-1",
url: "https://example.com",
title: "Example",
publishedDate: "2026-01-01T00:00:00.000Z",
author: "Author",
image: "https://example.com/image.png",
favicon: "https://example.com/favicon.ico",
text: null,
highlights: ["A useful passage"],
highlightScores: [0.7],
summary: "Summary",
},
],
})
} finally {
globalThis.fetch = originalFetch
}
})
test("throws on non-ok response", async () => {
const originalFetch = globalThis.fetch
globalThis.fetch = (async () =>
new Response("nope", { status: 401, statusText: "Unauthorized" })) as unknown as typeof fetch
try {
const client = new ExaSearchClient("bad-key")
await expect(client.search({ query: "test" })).rejects.toThrow(
"Exa API error: 401 Unauthorized",
)
} finally {
globalThis.fetch = originalFetch
}
})
})

View File

@@ -0,0 +1,124 @@
import { type } from "arktype"
import type {
WebSearchClient,
WebSearchRequest,
WebSearchResponse,
WebSearchResult,
} from "./types.ts"
const EXA_API_BASE = "https://api.exa.ai"
const DEFAULT_NUM_RESULTS = 10
const ExaSearchResult = type({
id: "string",
url: "string",
"title?": "string | null",
"publishedDate?": "string | null",
"author?": "string | null",
"image?": "string | null",
"favicon?": "string | null",
"text?": "string | null",
"highlights?": "string[]",
"highlightScores?": "number[]",
"summary?": "string | null",
})
const ExaSearchResponse = type({
results: ExaSearchResult.array(),
"requestId?": "string",
})
interface ExaSearchBody {
query: string
numResults?: number
includeDomains?: string[]
excludeDomains?: string[]
startCrawlDate?: string
endCrawlDate?: string
startPublishedDate?: string
endPublishedDate?: string
type?: WebSearchRequest["type"]
category?: string
userLocation?: string
moderation?: boolean
contents: {
highlights: boolean
}
}
export class ExaSearchClient implements WebSearchClient {
private readonly apiKey: string
private readonly baseUrl: string
constructor(apiKey: string, baseUrl = EXA_API_BASE) {
this.apiKey = apiKey
this.baseUrl = baseUrl
}
async search(request: WebSearchRequest): Promise<WebSearchResponse> {
const response = await fetch(new URL("/search", this.baseUrl), {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey,
},
body: JSON.stringify(toExaSearchBody(request)),
})
if (!response.ok) {
throw new Error(`Exa API error: ${response.status} ${response.statusText}`)
}
const data = await response.json()
const parsed = ExaSearchResponse(data)
if (parsed instanceof type.errors) {
throw new Error(`Invalid Exa API response: ${parsed.summary}`)
}
return {
query: request.query,
requestId: parsed.requestId ?? null,
results: parsed.results.map(toWebSearchResult),
}
}
}
function toExaSearchBody(request: WebSearchRequest): ExaSearchBody {
const body: ExaSearchBody = {
query: request.query,
numResults: request.numResults ?? DEFAULT_NUM_RESULTS,
contents: {
highlights: request.highlights ?? true,
},
}
if (request.includeDomains) body.includeDomains = request.includeDomains
if (request.excludeDomains) body.excludeDomains = request.excludeDomains
if (request.startCrawlDate) body.startCrawlDate = request.startCrawlDate
if (request.endCrawlDate) body.endCrawlDate = request.endCrawlDate
if (request.startPublishedDate) body.startPublishedDate = request.startPublishedDate
if (request.endPublishedDate) body.endPublishedDate = request.endPublishedDate
if (request.type) body.type = request.type
if (request.category) body.category = request.category
if (request.userLocation) body.userLocation = request.userLocation
if (request.moderation !== undefined) body.moderation = request.moderation
return body
}
function toWebSearchResult(result: typeof ExaSearchResult.infer): WebSearchResult {
return {
id: result.id,
url: result.url,
title: result.title ?? null,
publishedDate: result.publishedDate ?? null,
author: result.author ?? null,
image: result.image ?? null,
favicon: result.favicon ?? null,
text: result.text ?? null,
highlights: result.highlights ?? [],
highlightScores: result.highlightScores ?? [],
summary: result.summary ?? null,
}
}

View File

@@ -0,0 +1,11 @@
export { ExaSearchClient } from "./exa-client.ts"
export { WebSearchSource } from "./web-search-source.ts"
export {
WebSearchAction,
WebSearchType,
type WebSearchClient,
type WebSearchRequest,
type WebSearchResponse,
type WebSearchResult,
type WebSearchSourceOptions,
} from "./types.ts"

View File

@@ -0,0 +1,61 @@
export const WebSearchAction = {
Search: "search",
} as const
export type WebSearchAction = (typeof WebSearchAction)[keyof typeof WebSearchAction]
export const WebSearchType = {
Instant: "instant",
Fast: "fast",
Auto: "auto",
DeepLite: "deep-lite",
Deep: "deep",
DeepReasoning: "deep-reasoning",
} as const
export type WebSearchType = (typeof WebSearchType)[keyof typeof WebSearchType]
export interface WebSearchRequest {
query: string
numResults?: number
includeDomains?: string[]
excludeDomains?: string[]
startCrawlDate?: string
endCrawlDate?: string
startPublishedDate?: string
endPublishedDate?: string
type?: WebSearchType
category?: string
userLocation?: string
moderation?: boolean
highlights?: boolean
}
export interface WebSearchResult extends Record<string, unknown> {
id: string
url: string
title: string | null
publishedDate: string | null
author: string | null
image: string | null
favicon: string | null
text: string | null
highlights: string[]
highlightScores: number[]
summary: string | null
}
export interface WebSearchResponse extends Record<string, unknown> {
query: string
requestId: string | null
results: WebSearchResult[]
}
export interface WebSearchClient {
search(request: WebSearchRequest): Promise<WebSearchResponse>
}
export interface WebSearchSourceOptions {
apiKey?: string
client?: WebSearchClient
}

View File

@@ -0,0 +1,123 @@
import { Context } from "@freya/core"
import { describe, expect, test } from "bun:test"
import type { WebSearchClient, WebSearchRequest, WebSearchResponse } from "./types.ts"
import { WebSearchAction } from "./types.ts"
import { WebSearchSource } from "./web-search-source.ts"
class RecordingSearchClient implements WebSearchClient {
requests: WebSearchRequest[] = []
async search(request: WebSearchRequest): Promise<WebSearchResponse> {
this.requests.push(request)
return {
query: request.query,
requestId: "request-1",
results: [
{
id: "https://example.com/a",
url: "https://example.com/a",
title: "Example result",
publishedDate: "2026-01-01T00:00:00.000Z",
author: "Example Author",
image: null,
favicon: "https://example.com/favicon.ico",
text: null,
highlights: ["Relevant excerpt"],
highlightScores: [0.8],
summary: null,
},
],
}
}
}
describe("WebSearchSource", () => {
test("has correct id", () => {
const source = new WebSearchSource({ client: new RecordingSearchClient() })
expect(source.id).toBe("freya.web-search")
})
test("does not provide context or feed items", async () => {
const source = new WebSearchSource({ client: new RecordingSearchClient() })
expect("fetchItems" in source).toBe(false)
expect(await source.fetchContext(new Context())).toBeNull()
})
test("lists search action", async () => {
const source = new WebSearchSource({ client: new RecordingSearchClient() })
const actions = await source.listActions()
expect(actions[WebSearchAction.Search]).toBeDefined()
expect(actions[WebSearchAction.Search]!.id).toBe(WebSearchAction.Search)
expect(actions[WebSearchAction.Search]!.input).toBeDefined()
})
test("executes search action with normalized params", async () => {
const client = new RecordingSearchClient()
const source = new WebSearchSource({ client })
const result = await source.executeAction(WebSearchAction.Search, {
query: " latest personal assistant research ",
includeDomains: ["exa.ai"],
type: "fast",
userLocation: "gb",
moderation: true,
})
expect(result.requestId).toBe("request-1")
expect(result.results).toHaveLength(1)
expect(client.requests).toEqual([
{
query: "latest personal assistant research",
numResults: 10,
includeDomains: ["exa.ai"],
type: "fast",
userLocation: "GB",
moderation: true,
},
])
})
test("allows per-call numResults override", async () => {
const client = new RecordingSearchClient()
const source = new WebSearchSource({ client })
await source.executeAction(WebSearchAction.Search, {
query: "freya",
numResults: 2,
})
expect(client.requests[0]!.numResults).toBe(2)
})
test("throws for invalid action", async () => {
const source = new WebSearchSource({ client: new RecordingSearchClient() })
await expect(source.executeAction("missing", {})).rejects.toThrow("Unknown action")
})
test("throws for invalid search params", async () => {
const source = new WebSearchSource({ client: new RecordingSearchClient() })
await expect(
source.executeAction(WebSearchAction.Search, {
query: "",
}),
).rejects.toThrow("query must not be empty")
await expect(
source.executeAction(WebSearchAction.Search, {
query: "x",
numResults: 101,
}),
).rejects.toThrow("numResults must be an integer")
})
test("throws if neither client nor apiKey is provided", () => {
expect(() => new WebSearchSource({})).toThrow("Either client or apiKey must be provided")
})
})

View File

@@ -0,0 +1,121 @@
import type { ActionDefinition, Context, ContextEntry, FeedSource } from "@freya/core"
import { UnknownActionError } from "@freya/core"
import { type } from "arktype"
import type {
WebSearchClient,
WebSearchRequest,
WebSearchResponse,
WebSearchSourceOptions,
} from "./types.ts"
import { ExaSearchClient } from "./exa-client.ts"
import { WebSearchAction, WebSearchType } from "./types.ts"
const DEFAULT_NUM_RESULTS = 10
const MIN_NUM_RESULTS = 1
const MAX_NUM_RESULTS = 100
const SearchInput = type({
"+": "reject",
query: "string",
"numResults?": "number",
"includeDomains?": "string[]",
"excludeDomains?": "string[]",
"startCrawlDate?": "string.date.iso",
"endCrawlDate?": "string.date.iso",
"startPublishedDate?": "string.date.iso",
"endPublishedDate?": "string.date.iso",
"type?": "'instant' | 'fast' | 'auto' | 'deep-lite' | 'deep' | 'deep-reasoning'",
"category?": "string",
"userLocation?": "string",
"moderation?": "boolean",
"highlights?": "boolean",
})
/**
* Action-only FeedSource for web search through Exa.
*
* It intentionally does not produce feed items. Consumers call the `search`
* action and receive structured web results.
*/
export class WebSearchSource implements FeedSource {
readonly id = "freya.web-search"
private readonly client: WebSearchClient
constructor(options: WebSearchSourceOptions) {
if (!options.client && !options.apiKey) {
throw new Error("Either client or apiKey must be provided")
}
this.client = options.client ?? new ExaSearchClient(options.apiKey!)
}
async listActions(): Promise<Record<string, ActionDefinition>> {
return {
[WebSearchAction.Search]: {
id: WebSearchAction.Search,
description: "Search the web and return structured results",
input: SearchInput,
},
}
}
async executeAction(actionId: string, params: unknown): Promise<WebSearchResponse> {
switch (actionId) {
case WebSearchAction.Search:
return this.client.search(this.parseSearchInput(params))
default:
throw new UnknownActionError(actionId)
}
}
async fetchContext(_context: Context): Promise<readonly ContextEntry[] | null> {
return null
}
private parseSearchInput(params: unknown): WebSearchRequest {
const parsed = SearchInput(params)
if (parsed instanceof type.errors) {
throw new Error(parsed.summary)
}
const query = parsed.query.trim()
if (!query) {
throw new Error("query must not be empty")
}
const numResults = parsed.numResults ?? DEFAULT_NUM_RESULTS
if (
!Number.isInteger(numResults) ||
numResults < MIN_NUM_RESULTS ||
numResults > MAX_NUM_RESULTS
) {
throw new Error(`numResults must be an integer from ${MIN_NUM_RESULTS} to ${MAX_NUM_RESULTS}`)
}
if (parsed.userLocation && !/^[A-Za-z]{2}$/.test(parsed.userLocation)) {
throw new Error("userLocation must be a two-letter ISO country code")
}
const request: WebSearchRequest = {
query,
numResults,
}
if (parsed.includeDomains) request.includeDomains = parsed.includeDomains
if (parsed.excludeDomains) request.excludeDomains = parsed.excludeDomains
if (parsed.startCrawlDate) request.startCrawlDate = parsed.startCrawlDate
if (parsed.endCrawlDate) request.endCrawlDate = parsed.endCrawlDate
if (parsed.startPublishedDate) request.startPublishedDate = parsed.startPublishedDate
if (parsed.endPublishedDate) request.endPublishedDate = parsed.endPublishedDate
if (parsed.type) request.type = parsed.type as WebSearchType
if (parsed.category) request.category = parsed.category
if (parsed.userLocation) request.userLocation = parsed.userLocation.toUpperCase()
if (parsed.moderation !== undefined) request.moderation = parsed.moderation
if (parsed.highlights !== undefined) request.highlights = parsed.highlights
return request
}
}