Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,298 changes: 0 additions & 2,298 deletions package-lock.json

This file was deleted.

45 changes: 9 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
{
"name": "@leadbay/openclaw-leadclaw",
"version": "0.1.0",
"description": "OpenClaw plugin for Leadbay — AI lead discovery, qualification, and enrichment",
"name": "leadclaw-workspace",
"version": "0.0.0",
"private": true,
"description": "Leadbay packages monorepo — @leadbay/core shared tools, @leadbay/leadclaw OpenClaw adapter, @leadbay/mcp MCP server",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/",
"openclaw.plugin.json",
"README.md",
"LICENSE",
"logo.png"
],
"openclaw": {
"extensions": [
"./dist/index.js"
],
"install": {
"npmSpec": "@leadbay/openclaw-leadclaw"
},
"compat": {
"pluginApi": ">=2026.3.24-beta.2",
"minGatewayVersion": "2026.3.24-beta.2"
},
"build": {
"openclawVersion": "2026.3.24-beta.2",
"pluginSdkVersion": "2026.3.24-beta.2"
}
},
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:smoke": "vitest run --config vitest.smoke.config.ts",
"posttest": "echo 'Tip: run npm run test:smoke with LEADBAY_TEST_TOKEN to test the live API.'",
"prepublishOnly": "npm run build && npm test"
"build": "pnpm -r build",
"typecheck": "pnpm -r typecheck",
"test": "pnpm -r test",
"test:smoke": "pnpm --filter @leadbay/core --filter @leadbay/mcp test:smoke"
},
"devDependencies": {
"@types/node": "^25.6.0",
"@vitest/coverage-v8": "^2.1.0",
"tsup": "^8.3.5",
"typescript": "^5.5",
"vitest": "^2.1.0"
},
Expand Down
21 changes: 21 additions & 0 deletions packages/core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Leadbay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@leadbay/core",
"version": "0.1.0",
"private": true,
"description": "Leadbay shared core: HTTP client and protocol-agnostic tool definitions. Consumed by @leadbay/leadclaw and @leadbay/mcp.",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:smoke": "vitest run --config vitest.smoke.config.ts"
},
"license": "MIT"
}
41 changes: 35 additions & 6 deletions src/client.ts → packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const LENS_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
const TASTE_CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
const MAX_CONCURRENT = 5;

const REGIONS: Record<string, string> = {
us: "https://api-us.leadbay.app",
fr: "https://api-fr.leadbay.app",
};

interface HttpResult {
status: number;
body: string;
Expand Down Expand Up @@ -62,6 +67,23 @@ export interface TasteProfileResult {
qualificationQuestions: AiAgentQuestionPayload[];
}

export interface CreateClientConfig {
token?: string;
region?: "us" | "fr";
baseUrl?: string;
}

export function createClient(config: CreateClientConfig = {}): LeadbayClient {
const region = config.region ?? "us";
const baseUrl = config.baseUrl ?? REGIONS[region];
if (!baseUrl) {
throw new Error(
`Leadbay: unknown region "${region}". Supported: ${Object.keys(REGIONS).join(", ")}. Or pass an explicit baseUrl.`
);
}
return new LeadbayClient(baseUrl, config.token);
}

export class LeadbayClient {
private token: string | null;
readonly baseUrl: string;
Expand All @@ -88,6 +110,11 @@ export class LeadbayClient {
return this.token !== null;
}

// Test-only getter for concurrency assertions
get _semaphoreState(): { active: number; queued: number } {
return { active: this.activeRequests, queued: this.waitQueue.length };
}

private async acquireSemaphore(): Promise<void> {
if (this.activeRequests < MAX_CONCURRENT) {
this.activeRequests++;
Expand All @@ -112,7 +139,7 @@ export class LeadbayClient {
throw this.makeError(
"NOT_AUTHENTICATED",
"Not logged in to Leadbay",
"Call leadbay_login with your Leadbay email and password first"
"Set LEADBAY_TOKEN env var (obtain token at https://app.leadbay.ai/settings/api-tokens), or use the OpenClaw leadbay_login tool"
);
}
await this.acquireSemaphore();
Expand Down Expand Up @@ -151,7 +178,7 @@ export class LeadbayClient {
throw this.makeError(
"NOT_AUTHENTICATED",
"Not logged in to Leadbay",
"Call leadbay_login with your Leadbay email and password first"
"Set LEADBAY_TOKEN env var (obtain token at https://app.leadbay.ai/settings/api-tokens), or use the OpenClaw leadbay_login tool"
);
}
await this.acquireSemaphore();
Expand Down Expand Up @@ -191,14 +218,14 @@ export class LeadbayClient {
return this.makeError(
"AUTH_EXPIRED",
"Authentication token expired or invalid",
"Call leadbay_login to re-authenticate"
"Your LEADBAY_TOKEN is no longer valid. Regenerate at https://app.leadbay.ai/settings/api-tokens and restart."
);
}
if (status === 402 || parsed?.error === "quota_exceeded") {
return this.makeError(
"QUOTA_EXCEEDED",
"No enrichment credits remaining",
"Purchase more credits at app.leadbay.ai"
"Your Leadbay account is out of credits. Purchase more at https://app.leadbay.ai"
);
}
if (status === 403) {
Expand All @@ -210,13 +237,13 @@ export class LeadbayClient {
return this.makeError(
"BILLING_SUSPENDED",
"Account billing is suspended",
"Check billing at app.leadbay.ai"
"Your Leadbay account billing is suspended. Update at https://app.leadbay.ai"
);
}
return this.makeError(
"FORBIDDEN",
"Insufficient permissions",
"Check your account permissions"
"Your token does not have access to this resource. Check account permissions at https://app.leadbay.ai"
);
}
if (status === 404) {
Expand Down Expand Up @@ -327,3 +354,5 @@ export class LeadbayClient {
return { error: true, code, message, hint };
}
}

export { REGIONS };
68 changes: 68 additions & 0 deletions packages/core/src/composite/find-prospects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { LeadbayClient } from "../client.js";
import type { Tool, ToolContext } from "../types.js";
import { listLenses } from "../tools/list-lenses.js";
import { discoverLeads } from "../tools/discover-leads.js";

interface FindProspectsParams {
lensName?: string;
lensId?: number;
count?: number;
}

export const findProspects: Tool<FindProspectsParams> = {
name: "leadbay_find_prospects",
description:
"Find B2B prospects matching the user's Ideal Buyer Profile. Uses the active Leadbay lens by default. Returns scored lead summaries with AI qualification, recommended contact titles, and next-step suggestions. Start here for most lead-gen tasks.",
inputSchema: {
type: "object",
properties: {
lensName: {
type: "string",
description:
"Lens name to search (optional). If omitted, uses the active lens. The lens defines the target market segment.",
},
lensId: {
type: "number",
description:
"Lens ID (optional, takes precedence over lensName). Auto-resolves to the active lens if both omitted.",
},
count: {
type: "number",
description: "Number of prospects to return, max 50 (default: 20)",
},
},
},
execute: async (
client: LeadbayClient,
params: FindProspectsParams,
ctx?: ToolContext
) => {
let lensId = params.lensId;

if (!lensId && params.lensName) {
// Resolve lensName → lensId
const lensesResult = await listLenses.execute(client, {}, ctx);
const match = (lensesResult.lenses as Array<{ id: number; name: string }>).find(
(l) => l.name.toLowerCase() === params.lensName!.toLowerCase()
);
if (!match) {
throw client.makeError(
"LENS_NOT_FOUND",
`No lens named "${params.lensName}" found`,
"Call leadbay_list_lenses to see available lens names, or omit lensName to use the active lens."
);
}
lensId = match.id;
}

return discoverLeads.execute(
client,
{
lensId,
count: params.count,
page: 0,
},
ctx
);
},
};
111 changes: 111 additions & 0 deletions packages/core/src/composite/prepare-outreach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import type { LeadbayClient } from "../client.js";
import type { Tool, ToolContext } from "../types.js";
import { getLeadProfile } from "../tools/get-lead-profile.js";
import { getContacts } from "../tools/get-contacts.js";
import { enrichContacts } from "../tools/enrich-contacts.js";

interface PrepareOutreachParams {
leadId: string;
enrich?: boolean;
}

export const prepareOutreach: Tool<PrepareOutreachParams> = {
name: "leadbay_prepare_outreach",
description:
"Prepare an outreach package for a lead: returns the recommended contact (best match by job title), their enriched email/phone (if available), and the lead's AI summary. If enrich=true and credits are available, will trigger enrichment on the recommended contact and return the ID to poll later. Write action — requires user-level permission.",
optional: true,
inputSchema: {
type: "object",
properties: {
leadId: {
type: "string",
description: "Lead UUID (required)",
},
enrich: {
type: "boolean",
description:
"If true and credits available, trigger enrichment on the recommended contact (default: false). Enrichment is async — poll leadbay_get_contacts after ~60s.",
},
},
required: ["leadId"],
},
execute: async (
client: LeadbayClient,
params: PrepareOutreachParams,
ctx?: ToolContext
) => {
const contactsResult = await getContacts.execute(
client,
{ leadId: params.leadId },
ctx
);
const contacts = contactsResult.contacts as Array<{
id: string;
first_name: string | null;
last_name: string | null;
email: string | null;
phone_number: string | null;
linkedin_page: string | null;
job_title: string | null;
recommended: boolean;
source: "org" | "paid";
}>;

const recommended = contacts.find((c) => c.recommended) ?? contacts[0];

let enrichmentTriggered = false;
let enrichmentError: string | null = null;
if (params.enrich && recommended) {
try {
await enrichContacts.execute(
client,
{ leadId: params.leadId, contactId: recommended.id },
ctx
);
enrichmentTriggered = true;
} catch (e: any) {
enrichmentError = e?.message ?? String(e);
}
}

// Also fetch a short profile for AI summary context
let leadSummary: { name: string; ai_summary: string | null; website: string | null } | null =
null;
try {
const profile = await getLeadProfile.execute(
client,
{ leadId: params.leadId },
ctx
);
leadSummary = {
name: (profile.lead as any).name,
ai_summary: (profile.lead as any).ai_summary,
website: (profile.lead as any).website,
};
} catch {}

return {
lead: leadSummary,
recommended_contact: recommended
? {
id: recommended.id,
name: [recommended.first_name, recommended.last_name]
.filter(Boolean)
.join(" "),
job_title: recommended.job_title,
email: recommended.email,
phone_number: recommended.phone_number,
linkedin_page: recommended.linkedin_page,
}
: null,
other_contacts_count: Math.max(0, contacts.length - 1),
enrichment: {
triggered: enrichmentTriggered,
error: enrichmentError,
hint: enrichmentTriggered
? "Enrichment started. Poll leadbay_get_contacts with the same leadId in ~60 seconds."
: null,
},
};
},
};
Loading