@patchbay-nebius/agent 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +66 -2
- package/dist/chunk-JVXVCXNZ.js +2356 -0
- package/dist/chunk-JVXVCXNZ.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +391 -0
- package/dist/cli.js.map +1 -0
- package/dist/fixtures/typeform-pipedrive/attempt-1/README.md +41 -0
- package/dist/fixtures/typeform-pipedrive/attempt-1/src/connector.test.ts +111 -0
- package/dist/fixtures/typeform-pipedrive/attempt-1/src/connector.ts +93 -0
- package/dist/fixtures/typeform-pipedrive/attempt-1/src/fixtures.ts +173 -0
- package/dist/fixtures/typeform-pipedrive/final/README.md +41 -0
- package/dist/fixtures/typeform-pipedrive/final/src/connector.test.ts +111 -0
- package/dist/fixtures/typeform-pipedrive/final/src/connector.ts +94 -0
- package/dist/fixtures/typeform-pipedrive/final/src/fixtures.ts +173 -0
- package/dist/fixtures/typeform-pipedrive/meta.json +192 -0
- package/dist/fixtures/typeform-pipedrive/run.json +444 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/scanner-IPBNTPNM.js +247 -0
- package/dist/scanner-IPBNTPNM.js.map +1 -0
- package/package.json +61 -4
|
@@ -0,0 +1,2356 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
function loadConfig(env = process.env) {
|
|
3
|
+
const provider = parseProvider(env.PATCHBAY_LLM_PROVIDER);
|
|
4
|
+
const apiKey = env.PATCHBAY_LLM_API_KEY || env.NEBIUS_API_KEY || env.OPENAI_API_KEY || "";
|
|
5
|
+
const baseUrl = env.PATCHBAY_LLM_BASE_URL || env.NEBIUS_BASE_URL || (provider === "openai" ? "https://api.openai.com/v1" : "https://api.tokenfactory.nebius.com/v1");
|
|
6
|
+
const defaultModels = provider === "openai" ? { fast: "gpt-4o-mini", mid: "gpt-4o-mini", strong: "gpt-4o" } : { fast: "nvidia/Nemotron-3_5-Lightning", mid: "nvidia/nemotron-3-super-120b-a12b", strong: "nvidia/Nemotron-3-Ultra-550b-a55b" };
|
|
7
|
+
return {
|
|
8
|
+
mock: env.PATCHBAY_MOCK !== "false",
|
|
9
|
+
runner: env.PATCHBAY_RUNNER === "local" ? "local" : "nebius",
|
|
10
|
+
nebiusApiKey: apiKey,
|
|
11
|
+
nebiusProject: env.NEBIUS_AI_PROJECT ?? "",
|
|
12
|
+
nebiusBaseUrl: baseUrl,
|
|
13
|
+
sandboxUrl: env.NEBIUS_SANDBOX_URL || "https://api.tokenfactory.nebius.com/sandboxes/v1",
|
|
14
|
+
tavilyApiKey: env.TAVILY_API_KEY ?? "",
|
|
15
|
+
baseImage: env.PATCHBAY_BASE_IMAGE || void 0,
|
|
16
|
+
maxAttempts: Number(env.PATCHBAY_MAX_ATTEMPTS || 4),
|
|
17
|
+
parallelRepairs: clamp(Number(env.PATCHBAY_PARALLEL_REPAIRS || 2), 1, 4),
|
|
18
|
+
llmProvider: provider,
|
|
19
|
+
models: {
|
|
20
|
+
fast: { id: env.PATCHBAY_MODEL_FAST || defaultModels.fast, inputPrice: 0.06, outputPrice: 0.24 },
|
|
21
|
+
mid: { id: env.PATCHBAY_MODEL_MID || defaultModels.mid, inputPrice: 0.3, outputPrice: 0.9 },
|
|
22
|
+
strong: { id: env.PATCHBAY_MODEL_STRONG || defaultModels.strong, inputPrice: 1, outputPrice: 3 }
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function parseProvider(raw) {
|
|
27
|
+
if (raw === "openai" || raw === "generic") return raw;
|
|
28
|
+
return "nebius";
|
|
29
|
+
}
|
|
30
|
+
function clamp(n, min, max) {
|
|
31
|
+
return Number.isFinite(n) ? Math.min(max, Math.max(min, Math.round(n))) : min;
|
|
32
|
+
}
|
|
33
|
+
function missingLiveKeys(config) {
|
|
34
|
+
const missing = [];
|
|
35
|
+
if (!config.nebiusApiKey) missing.push("NEBIUS_API_KEY");
|
|
36
|
+
if (config.runner === "nebius" && !config.nebiusProject) missing.push("NEBIUS_AI_PROJECT");
|
|
37
|
+
if (!config.tavilyApiKey) missing.push("TAVILY_API_KEY");
|
|
38
|
+
return missing;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/parse.ts
|
|
42
|
+
function stripReasoning(text) {
|
|
43
|
+
return text.replace(/<think>[\s\S]*?<\/think>/gi, "").replace(/^[\s\S]*?<\/think>/i, "").trim();
|
|
44
|
+
}
|
|
45
|
+
function extractJson(text) {
|
|
46
|
+
const clean = stripReasoning(text);
|
|
47
|
+
const fenced = clean.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
48
|
+
const candidate = fenced?.[1] ?? clean;
|
|
49
|
+
const start = candidate.indexOf("{");
|
|
50
|
+
const end = candidate.lastIndexOf("}");
|
|
51
|
+
if (start === -1 || end <= start) throw new Error("model answer contained no JSON object");
|
|
52
|
+
return JSON.parse(candidate.slice(start, end + 1));
|
|
53
|
+
}
|
|
54
|
+
function parseFileBlocks(text) {
|
|
55
|
+
const clean = stripReasoning(text);
|
|
56
|
+
const files = [];
|
|
57
|
+
const pattern = /<file\s+path="([^"]+)">\s*\n?([\s\S]*?)\n?<\/file>/g;
|
|
58
|
+
for (const match of clean.matchAll(pattern)) {
|
|
59
|
+
const path = normalizePath(match[1] ?? "");
|
|
60
|
+
const content = stripFence(match[2] ?? "");
|
|
61
|
+
if (path) files.push({ path, content: content.endsWith("\n") ? content : `${content}
|
|
62
|
+
` });
|
|
63
|
+
}
|
|
64
|
+
return files;
|
|
65
|
+
}
|
|
66
|
+
function renderFileBlocks(files) {
|
|
67
|
+
return files.map((f) => `<file path="${f.path}">
|
|
68
|
+
${f.content.trimEnd()}
|
|
69
|
+
</file>`).join("\n\n");
|
|
70
|
+
}
|
|
71
|
+
function stripFence(content) {
|
|
72
|
+
const fenced = content.match(/^\s*```[a-zA-Z]*\n([\s\S]*?)\n```\s*$/);
|
|
73
|
+
return fenced?.[1] ?? content;
|
|
74
|
+
}
|
|
75
|
+
function normalizePath(path) {
|
|
76
|
+
const slashed = path.trim().replace(/\\/g, "/");
|
|
77
|
+
if (slashed.startsWith("/") || /^[a-zA-Z]:/.test(slashed)) return "";
|
|
78
|
+
const clean = slashed.replace(/^\.\//, "");
|
|
79
|
+
if (clean.split("/").includes("..")) return "";
|
|
80
|
+
return clean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/llm.ts
|
|
84
|
+
function createNebiusClient(opts) {
|
|
85
|
+
const doFetch = opts.fetch ?? fetch;
|
|
86
|
+
const retries = opts.retries ?? 2;
|
|
87
|
+
const nemotron = opts.supportsNemotronThinking ?? true;
|
|
88
|
+
const ledger = /* @__PURE__ */ new Map();
|
|
89
|
+
async function chat(model, messages, options = {}) {
|
|
90
|
+
const body = {
|
|
91
|
+
model: model.id,
|
|
92
|
+
messages,
|
|
93
|
+
temperature: options.temperature ?? 0.2,
|
|
94
|
+
max_tokens: options.maxTokens ?? 4096,
|
|
95
|
+
...nemotron && options.thinking === false ? { chat_template_kwargs: { enable_thinking: false } } : {}
|
|
96
|
+
};
|
|
97
|
+
let lastError;
|
|
98
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
99
|
+
try {
|
|
100
|
+
const res = await doFetch(`${opts.baseUrl}/chat/completions`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: { Authorization: `Bearer ${opts.apiKey}`, "Content-Type": "application/json" },
|
|
103
|
+
body: JSON.stringify(body)
|
|
104
|
+
});
|
|
105
|
+
const data = await res.json().catch(() => ({}));
|
|
106
|
+
if (!res.ok) {
|
|
107
|
+
const message = data.error?.message ?? `HTTP ${res.status}`;
|
|
108
|
+
if (res.status === 429 || res.status >= 500) throw new RetryableError(message);
|
|
109
|
+
throw new Error(`Nebius ${model.id}: ${message}`);
|
|
110
|
+
}
|
|
111
|
+
record(model, data.usage);
|
|
112
|
+
const content = data.choices?.[0]?.message?.content ?? "";
|
|
113
|
+
return stripReasoning(content);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
lastError = error;
|
|
116
|
+
if (!(error instanceof RetryableError) && !isTransientFetchError(error)) break;
|
|
117
|
+
if (attempt === retries) break;
|
|
118
|
+
await sleep(800 * 2 ** attempt);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
122
|
+
}
|
|
123
|
+
function record(model, usage) {
|
|
124
|
+
const current = ledger.get(model.id) ?? { model: model.id, promptTokens: 0, completionTokens: 0, costUsd: 0 };
|
|
125
|
+
current.promptTokens += usage?.prompt_tokens ?? 0;
|
|
126
|
+
current.completionTokens += usage?.completion_tokens ?? 0;
|
|
127
|
+
current.costUsd = current.promptTokens / 1e6 * model.inputPrice + current.completionTokens / 1e6 * model.outputPrice;
|
|
128
|
+
ledger.set(model.id, current);
|
|
129
|
+
}
|
|
130
|
+
return { chat, usage: () => [...ledger.values()] };
|
|
131
|
+
}
|
|
132
|
+
var RetryableError = class extends Error {
|
|
133
|
+
};
|
|
134
|
+
function isTransientFetchError(error) {
|
|
135
|
+
if (!(error instanceof Error)) return false;
|
|
136
|
+
const cause = error.cause;
|
|
137
|
+
const code = cause?.code ?? "";
|
|
138
|
+
const name = cause?.name ?? "";
|
|
139
|
+
return error.message === "fetch failed" || name === "SocketError" || ["UND_ERR_SOCKET", "ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EAI_AGAIN", "ENOTFOUND", "EPIPE"].includes(code);
|
|
140
|
+
}
|
|
141
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
142
|
+
|
|
143
|
+
// src/local-runner.ts
|
|
144
|
+
import { execFile } from "child_process";
|
|
145
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs";
|
|
146
|
+
import { dirname, join } from "path";
|
|
147
|
+
|
|
148
|
+
// src/report.ts
|
|
149
|
+
var TSC_MARKER = "@@PATCHBAY_TSC@@";
|
|
150
|
+
var VITEST_MARKER = "@@PATCHBAY_VITEST@@";
|
|
151
|
+
var TEST_COMMAND = [
|
|
152
|
+
"cd /work",
|
|
153
|
+
"npx tsc --noEmit -p tsconfig.json > /tmp/tsc.txt 2>&1; echo $? > /tmp/tsc.code",
|
|
154
|
+
"npx vitest run --reporter=json --outputFile=/tmp/vitest.json > /tmp/vitest.log 2>&1",
|
|
155
|
+
`echo "${TSC_MARKER}"`,
|
|
156
|
+
"cat /tmp/tsc.code",
|
|
157
|
+
"head -c 6000 /tmp/tsc.txt",
|
|
158
|
+
`echo "${VITEST_MARKER}"`,
|
|
159
|
+
"cat /tmp/vitest.json 2>/dev/null || tail -c 6000 /tmp/vitest.log"
|
|
160
|
+
].join("; ");
|
|
161
|
+
function parseTestOutput(stdout, ranOn) {
|
|
162
|
+
const tscStart = stdout.indexOf(TSC_MARKER);
|
|
163
|
+
const vitestStart = stdout.indexOf(VITEST_MARKER);
|
|
164
|
+
const tscSection = tscStart >= 0 ? stdout.slice(tscStart + TSC_MARKER.length, vitestStart >= 0 ? vitestStart : void 0).trim() : "";
|
|
165
|
+
const vitestSection = vitestStart >= 0 ? stdout.slice(vitestStart + VITEST_MARKER.length).trim() : stdout.trim();
|
|
166
|
+
const [codeLine = "1", ...tscLines] = tscSection.split("\n");
|
|
167
|
+
const typecheckOk = codeLine.trim() === "0";
|
|
168
|
+
const typecheckOutput = tscLines.join("\n").trim();
|
|
169
|
+
let json = null;
|
|
170
|
+
try {
|
|
171
|
+
const start = vitestSection.indexOf("{");
|
|
172
|
+
json = start >= 0 ? JSON.parse(vitestSection.slice(start)) : null;
|
|
173
|
+
} catch {
|
|
174
|
+
json = null;
|
|
175
|
+
}
|
|
176
|
+
if (!json) {
|
|
177
|
+
return {
|
|
178
|
+
typecheckOk,
|
|
179
|
+
typecheckOutput,
|
|
180
|
+
passed: 0,
|
|
181
|
+
failed: 1,
|
|
182
|
+
total: 1,
|
|
183
|
+
failures: [{ name: "vitest", message: truncate(vitestSection || "vitest produced no output", 4e3) }],
|
|
184
|
+
durationMs: 0,
|
|
185
|
+
ranOn
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const failures = [];
|
|
189
|
+
const passedNames = [];
|
|
190
|
+
let endTime = json.startTime ?? 0;
|
|
191
|
+
for (const file of json.testResults ?? []) {
|
|
192
|
+
endTime = Math.max(endTime, file.endTime ?? 0);
|
|
193
|
+
if ((file.assertionResults ?? []).length === 0 && file.message) {
|
|
194
|
+
failures.push({ name: "test file", message: truncate(stripAnsi(file.message), 2e3) });
|
|
195
|
+
}
|
|
196
|
+
for (const assertion of file.assertionResults ?? []) {
|
|
197
|
+
if (assertion.status === "passed") passedNames.push(assertion.fullName || assertion.title || "");
|
|
198
|
+
if (assertion.status === "failed") {
|
|
199
|
+
failures.push({
|
|
200
|
+
name: assertion.fullName || assertion.title || "unnamed test",
|
|
201
|
+
message: truncate(stripAnsi((assertion.failureMessages ?? []).join("\n")), 2e3)
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const total = json.numTotalTests ?? 0;
|
|
207
|
+
const failed = Math.max(json.numFailedTests ?? 0, failures.length);
|
|
208
|
+
return {
|
|
209
|
+
typecheckOk,
|
|
210
|
+
typecheckOutput,
|
|
211
|
+
passed: json.numPassedTests ?? Math.max(0, total - failed),
|
|
212
|
+
failed,
|
|
213
|
+
total: Math.max(total, failed),
|
|
214
|
+
failures,
|
|
215
|
+
durationMs: json.startTime && endTime ? Math.max(0, endTime - json.startTime) : 0,
|
|
216
|
+
ranOn,
|
|
217
|
+
passedNames
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function isGreen(report) {
|
|
221
|
+
const contractOk = report.contract === void 0 || report.contract === "passed";
|
|
222
|
+
return report.typecheckOk && report.failed === 0 && report.total > 0 && contractOk;
|
|
223
|
+
}
|
|
224
|
+
var stripAnsi = (s) => s.replace(/\u001b\[[0-9;]*m/g, "");
|
|
225
|
+
var truncate = (s, max) => s.length > max ? `${s.slice(0, max)}
|
|
226
|
+
\u2026` : s;
|
|
227
|
+
|
|
228
|
+
// src/local-runner.ts
|
|
229
|
+
var TSCONFIG = {
|
|
230
|
+
compilerOptions: {
|
|
231
|
+
target: "ES2022",
|
|
232
|
+
module: "ESNext",
|
|
233
|
+
moduleResolution: "Bundler",
|
|
234
|
+
strict: true,
|
|
235
|
+
skipLibCheck: true,
|
|
236
|
+
types: ["node"],
|
|
237
|
+
lib: ["ES2022", "DOM"]
|
|
238
|
+
},
|
|
239
|
+
include: ["src/**/*.ts"]
|
|
240
|
+
};
|
|
241
|
+
function createLocalRunner(opts = {}) {
|
|
242
|
+
const root = opts.root ?? findRepoRoot(process.cwd());
|
|
243
|
+
const tscEntry = join(root, "node_modules", "typescript", "bin", "tsc");
|
|
244
|
+
const vitestEntry = join(root, "node_modules", "vitest", "vitest.mjs");
|
|
245
|
+
const timeout = opts.timeoutMs ?? 12e4;
|
|
246
|
+
return {
|
|
247
|
+
async prepare(log) {
|
|
248
|
+
log("Local runner: typecheck and tests run on this machine, not in an isolated sandbox");
|
|
249
|
+
return "local";
|
|
250
|
+
},
|
|
251
|
+
async runTests(_base, files, log) {
|
|
252
|
+
const workRoot = join(root, ".fixture-tmp");
|
|
253
|
+
mkdirSync(workRoot, { recursive: true });
|
|
254
|
+
const work = mkdtempSync(join(workRoot, "run-"));
|
|
255
|
+
try {
|
|
256
|
+
for (const file of files.filter((f) => f.path.endsWith(".ts"))) write(work, file);
|
|
257
|
+
writeFileSync(join(work, "tsconfig.json"), JSON.stringify(TSCONFIG));
|
|
258
|
+
writeFileSync(join(work, "vitest.config.mjs"), 'export default { test: { include: ["src/**/*.test.ts"] } }\n');
|
|
259
|
+
log(`Running tsc and vitest in ${work.replace(root, ".")}`);
|
|
260
|
+
const tsc = await run(tscEntry, ["--noEmit", "-p", "tsconfig.json"], work, timeout);
|
|
261
|
+
await run(vitestEntry, ["run", "--reporter=json", "--outputFile=result.json"], work, timeout);
|
|
262
|
+
const json = existsSync(join(work, "result.json")) ? await readText(join(work, "result.json")) : "";
|
|
263
|
+
const stdout = `${TSC_MARKER}
|
|
264
|
+
${tsc.code}
|
|
265
|
+
${tsc.output}
|
|
266
|
+
${VITEST_MARKER}
|
|
267
|
+
${json}`;
|
|
268
|
+
return parseTestOutput(stdout, "local");
|
|
269
|
+
} finally {
|
|
270
|
+
rmSync(work, { recursive: true, force: true });
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function write(dir, file) {
|
|
276
|
+
const path = join(dir, file.path);
|
|
277
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
278
|
+
writeFileSync(path, file.content);
|
|
279
|
+
}
|
|
280
|
+
function run(entry, args, cwd, timeout) {
|
|
281
|
+
return new Promise((resolve) => {
|
|
282
|
+
execFile(process.execPath, [entry, ...args], { cwd, timeout, maxBuffer: 1e7 }, (error, stdout, stderr) => {
|
|
283
|
+
const code = error ? typeof error.code === "number" ? error.code : 1 : 0;
|
|
284
|
+
resolve({ code, output: `${stdout}${stderr}`.slice(0, 6e3) });
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async function readText(path) {
|
|
289
|
+
const { readFile } = await import("fs/promises");
|
|
290
|
+
return readFile(path, "utf8");
|
|
291
|
+
}
|
|
292
|
+
function findRepoRoot(start) {
|
|
293
|
+
let dir = start;
|
|
294
|
+
for (; ; ) {
|
|
295
|
+
if (existsSync(join(dir, "node_modules", "vitest"))) return dir;
|
|
296
|
+
const parent = dirname(dir);
|
|
297
|
+
if (parent === dir) return start;
|
|
298
|
+
dir = parent;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/fixtures/typeform-pipedrive/run.json
|
|
303
|
+
var run_default = {
|
|
304
|
+
prompt: "When someone submits our Typeform contact form, create the lead in Pipedrive as organization, person and deal.",
|
|
305
|
+
origin: "fixture",
|
|
306
|
+
events: [
|
|
307
|
+
{
|
|
308
|
+
type: "mode",
|
|
309
|
+
mode: "live"
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
type: "step",
|
|
313
|
+
step: "plan",
|
|
314
|
+
status: "start"
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
type: "plan",
|
|
318
|
+
plan: {
|
|
319
|
+
summary: "On every Typeform form_response webhook, create a Pipedrive organization (if a company was given), a person linked to it, and a deal for that person.",
|
|
320
|
+
source: {
|
|
321
|
+
app: "Typeform",
|
|
322
|
+
trigger: "form_response webhook",
|
|
323
|
+
docsQuery: "Typeform webhooks form_response payload answers field ref"
|
|
324
|
+
},
|
|
325
|
+
target: {
|
|
326
|
+
app: "Pipedrive",
|
|
327
|
+
action: "create organization, person and deal",
|
|
328
|
+
docsQuery: "Pipedrive API v1 add person add deal add organization request body api token"
|
|
329
|
+
},
|
|
330
|
+
fieldMapping: [
|
|
331
|
+
{
|
|
332
|
+
from: "answer with field ref lead_name (text)",
|
|
333
|
+
to: "person.name"
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
from: "answer with field ref lead_email (email)",
|
|
337
|
+
to: "person.email[0].value"
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
from: "answer with field ref lead_phone (phone_number)",
|
|
341
|
+
to: "person.phone[0].value"
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
from: "answer with field ref lead_company (text)",
|
|
345
|
+
to: "organization.name, deal title"
|
|
346
|
+
}
|
|
347
|
+
],
|
|
348
|
+
assumptions: [
|
|
349
|
+
"Field refs are configurable because every Typeform form uses different refs.",
|
|
350
|
+
"Webhook signature verification happens before the connector is called.",
|
|
351
|
+
"Every submission is a new lead; no deduplication against existing persons."
|
|
352
|
+
]
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
type: "step",
|
|
357
|
+
step: "plan",
|
|
358
|
+
status: "done",
|
|
359
|
+
detail: "On every Typeform form_response webhook, create a Pipedrive organization (if a company was given), a person linked to it, and a deal for that person."
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
type: "step",
|
|
363
|
+
step: "research",
|
|
364
|
+
status: "start"
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
type: "log",
|
|
368
|
+
step: "research",
|
|
369
|
+
message: 'Searched "Typeform webhooks form_response payload answers field ref": 5 results'
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
type: "log",
|
|
373
|
+
step: "research",
|
|
374
|
+
message: 'Searched "Pipedrive API v1 add person add deal add organization request body api token": 5 results'
|
|
375
|
+
},
|
|
376
|
+
{
|
|
377
|
+
type: "log",
|
|
378
|
+
step: "research",
|
|
379
|
+
message: "Extracted 4 documentation pages"
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
type: "sources",
|
|
383
|
+
sources: [
|
|
384
|
+
{
|
|
385
|
+
title: "Webhooks payload | Typeform Developer Platform",
|
|
386
|
+
url: "https://www.typeform.com/developers/webhooks/example-payload/",
|
|
387
|
+
official: true
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
title: "Webhooks | Typeform Developer Platform",
|
|
391
|
+
url: "https://www.typeform.com/developers/webhooks/",
|
|
392
|
+
official: true
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
title: "Persons - Pipedrive API Reference",
|
|
396
|
+
url: "https://developers.pipedrive.com/docs/api/v1/Persons",
|
|
397
|
+
official: true
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
title: "Deals - Pipedrive API Reference",
|
|
401
|
+
url: "https://developers.pipedrive.com/docs/api/v1/Deals",
|
|
402
|
+
official: true
|
|
403
|
+
}
|
|
404
|
+
]
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
type: "spec",
|
|
408
|
+
spec: {
|
|
409
|
+
source: {
|
|
410
|
+
app: "Typeform",
|
|
411
|
+
role: "source",
|
|
412
|
+
method: "POST",
|
|
413
|
+
url: "(your webhook endpoint)",
|
|
414
|
+
auth: {
|
|
415
|
+
type: "none",
|
|
416
|
+
envVar: "TYPEFORM_WEBHOOK_SECRET"
|
|
417
|
+
},
|
|
418
|
+
example: {
|
|
419
|
+
event_id: "01H...",
|
|
420
|
+
event_type: "form_response",
|
|
421
|
+
form_response: {
|
|
422
|
+
form_id: "lT4Z3j",
|
|
423
|
+
token: "a3a12ec67a1365927098a606107fac15",
|
|
424
|
+
submitted_at: "2026-10-01T09:00:00Z",
|
|
425
|
+
answers: [
|
|
426
|
+
{
|
|
427
|
+
type: "text",
|
|
428
|
+
field: {
|
|
429
|
+
id: "f1",
|
|
430
|
+
ref: "lead_name",
|
|
431
|
+
type: "short_text"
|
|
432
|
+
},
|
|
433
|
+
text: "Ada Lovelace"
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
type: "email",
|
|
437
|
+
field: {
|
|
438
|
+
id: "f2",
|
|
439
|
+
ref: "lead_email",
|
|
440
|
+
type: "email"
|
|
441
|
+
},
|
|
442
|
+
email: "ada@example.com"
|
|
443
|
+
}
|
|
444
|
+
]
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
docsUrl: "https://www.typeform.com/developers/webhooks/example-payload/",
|
|
448
|
+
notes: [
|
|
449
|
+
"Each answer carries its value under a key named after its type (text, email, phone_number, ...).",
|
|
450
|
+
"Answers are matched by field.ref, which the form owner can set; field ids are generated."
|
|
451
|
+
]
|
|
452
|
+
},
|
|
453
|
+
target: [
|
|
454
|
+
{
|
|
455
|
+
app: "Pipedrive",
|
|
456
|
+
role: "target",
|
|
457
|
+
method: "POST",
|
|
458
|
+
url: "https://api.pipedrive.com/v1/organizations",
|
|
459
|
+
auth: {
|
|
460
|
+
type: "header",
|
|
461
|
+
name: "x-api-token",
|
|
462
|
+
envVar: "PIPEDRIVE_API_TOKEN"
|
|
463
|
+
},
|
|
464
|
+
example: {
|
|
465
|
+
name: "Analytical Engines Ltd"
|
|
466
|
+
},
|
|
467
|
+
responseExample: {
|
|
468
|
+
success: true,
|
|
469
|
+
data: {
|
|
470
|
+
id: 11
|
|
471
|
+
}
|
|
472
|
+
},
|
|
473
|
+
docsUrl: "https://developers.pipedrive.com/docs/api/v1/Organizations",
|
|
474
|
+
notes: [
|
|
475
|
+
"Only call when a company was answered."
|
|
476
|
+
]
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
app: "Pipedrive",
|
|
480
|
+
role: "target",
|
|
481
|
+
method: "POST",
|
|
482
|
+
url: "https://api.pipedrive.com/v1/persons",
|
|
483
|
+
auth: {
|
|
484
|
+
type: "header",
|
|
485
|
+
name: "x-api-token",
|
|
486
|
+
envVar: "PIPEDRIVE_API_TOKEN"
|
|
487
|
+
},
|
|
488
|
+
example: {
|
|
489
|
+
name: "Ada Lovelace",
|
|
490
|
+
email: [
|
|
491
|
+
{
|
|
492
|
+
value: "ada@example.com",
|
|
493
|
+
primary: true,
|
|
494
|
+
label: "work"
|
|
495
|
+
}
|
|
496
|
+
],
|
|
497
|
+
org_id: 11
|
|
498
|
+
},
|
|
499
|
+
responseExample: {
|
|
500
|
+
success: true,
|
|
501
|
+
data: {
|
|
502
|
+
id: 22
|
|
503
|
+
}
|
|
504
|
+
},
|
|
505
|
+
docsUrl: "https://developers.pipedrive.com/docs/api/v1/Persons",
|
|
506
|
+
notes: [
|
|
507
|
+
"email and phone are arrays of { value, primary, label }.",
|
|
508
|
+
"The API token can be sent as the x-api-token header instead of the api_token query parameter."
|
|
509
|
+
]
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
app: "Pipedrive",
|
|
513
|
+
role: "target",
|
|
514
|
+
method: "POST",
|
|
515
|
+
url: "https://api.pipedrive.com/v1/deals",
|
|
516
|
+
auth: {
|
|
517
|
+
type: "header",
|
|
518
|
+
name: "x-api-token",
|
|
519
|
+
envVar: "PIPEDRIVE_API_TOKEN"
|
|
520
|
+
},
|
|
521
|
+
example: {
|
|
522
|
+
title: "Website lead: Analytical Engines Ltd",
|
|
523
|
+
person_id: 22,
|
|
524
|
+
org_id: 11
|
|
525
|
+
},
|
|
526
|
+
responseExample: {
|
|
527
|
+
success: true,
|
|
528
|
+
data: {
|
|
529
|
+
id: 33
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
docsUrl: "https://developers.pipedrive.com/docs/api/v1/Deals",
|
|
533
|
+
notes: [
|
|
534
|
+
"title is required. Link both person_id and org_id so the deal shows up on the organization."
|
|
535
|
+
]
|
|
536
|
+
}
|
|
537
|
+
]
|
|
538
|
+
}
|
|
539
|
+
},
|
|
540
|
+
{
|
|
541
|
+
type: "step",
|
|
542
|
+
step: "research",
|
|
543
|
+
status: "done",
|
|
544
|
+
detail: "4 endpoints from 4 pages"
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
type: "step",
|
|
548
|
+
step: "generate",
|
|
549
|
+
status: "start"
|
|
550
|
+
},
|
|
551
|
+
{
|
|
552
|
+
type: "files",
|
|
553
|
+
attempt: 1,
|
|
554
|
+
files: [
|
|
555
|
+
{
|
|
556
|
+
path: "src/connector.ts",
|
|
557
|
+
content: "export interface ConnectorConfig {\n /** Pipedrive personal API token (env: PIPEDRIVE_API_TOKEN). */\n pipedriveApiToken: string\n /** Defaults to https://api.pipedrive.com/v1 */\n pipedriveBaseUrl?: string\n /** Typeform field refs that hold the lead's data. */\n fieldRefs: { name: string; email: string; company?: string; phone?: string }\n /** Prefix for the deal title, e.g. \"Website lead\". */\n dealTitlePrefix?: string\n fetch?: typeof fetch\n}\n\nexport interface TypeformAnswer {\n type: string\n field: { id: string; ref: string; type: string }\n text?: string\n email?: string\n phone_number?: string\n}\n\nexport interface TypeformWebhook {\n event_id: string\n event_type: 'form_response'\n form_response: {\n form_id: string\n token: string\n submitted_at: string\n answers: TypeformAnswer[]\n }\n}\n\nexport interface HandleResult {\n personId: number\n organizationId: number | null\n dealId: number\n}\n\ninterface PipedriveResponse {\n success: boolean\n data?: { id: number }\n error?: string\n}\n\nexport async function handle(payload: TypeformWebhook, config: ConnectorConfig): Promise<HandleResult> {\n if (payload?.event_type !== 'form_response') {\n throw new Error(`Unsupported Typeform event: ${String(payload?.event_type)}`)\n }\n const answers = payload.form_response.answers ?? []\n const name = answerValue(answers, config.fieldRefs.name)\n const email = answerValue(answers, config.fieldRefs.email)\n if (!name) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.name}\" (name)`)\n if (!email) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.email}\" (email)`)\n const company = config.fieldRefs.company ? answerValue(answers, config.fieldRefs.company) : undefined\n const phone = config.fieldRefs.phone ? answerValue(answers, config.fieldRefs.phone) : undefined\n\n const organizationId = company ? (await post(config, '/organizations', { name: company })).id : null\n\n const person = await post(config, '/persons', {\n name,\n email: [{ value: email, primary: true, label: 'work' }],\n ...(phone ? { phone: [{ value: phone, primary: true, label: 'work' }] } : {}),\n ...(organizationId ? { org_id: organizationId } : {}),\n })\n\n const deal = await post(config, '/deals', {\n title: `${config.dealTitlePrefix ?? 'Typeform lead'}: ${company ?? name}`,\n person_id: person.id,\n })\n\n return { personId: person.id, organizationId, dealId: deal.id }\n}\n\nfunction answerValue(answers: TypeformAnswer[], ref: string): string | undefined {\n const answer = answers.find((a) => a.field?.ref === ref)\n if (!answer) return undefined\n const value = answer.email ?? answer.phone_number ?? answer.text\n return value?.trim() || undefined\n}\n\nasync function post(config: ConnectorConfig, path: string, body: Record<string, unknown>): Promise<{ id: number }> {\n const doFetch = config.fetch ?? fetch\n const url = `${config.pipedriveBaseUrl ?? 'https://api.pipedrive.com/v1'}${path}`\n const res = await doFetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${config.pipedriveApiToken}` },\n body: JSON.stringify(body),\n })\n const data = (await res.json().catch(() => ({}))) as PipedriveResponse\n if (!res.ok || !data.success || !data.data) {\n throw new Error(`Pipedrive POST ${path} failed with ${res.status}: ${data.error ?? 'no error message'}`)\n }\n return data.data\n}\n"
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
path: "src/connector.test.ts",
|
|
561
|
+
content: "import { describe, it, expect, vi } from 'vitest'\nimport { handle, type ConnectorConfig, type TypeformWebhook } from './connector'\nimport { SOURCE_EXAMPLE, contractFetch } from './fixtures'\n\nfunction fakeFetch(responses: Record<string, { status?: number; body: unknown }>) {\n return vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {\n const url = String(input)\n const path = Object.keys(responses).find((p) => url.endsWith(p))\n const response = path ? responses[path] : undefined\n if (!response) throw new Error(`unexpected request to ${url} ${init?.method}`)\n return new Response(JSON.stringify(response.body), { status: response.status ?? 201 })\n })\n}\n\nconst ok = (id: number) => ({ body: { success: true, data: { id } } })\n\nfunction payload(overrides: Partial<Record<'name' | 'email' | 'company' | 'phone', string | null>> = {}): TypeformWebhook {\n const values = { name: 'Ada Lovelace', email: 'ada@example.com', company: 'Analytical Engines Ltd', phone: '+44 20 7946 0000', ...overrides }\n const answers = [\n values.name !== null && { type: 'text', field: { id: 'f1', ref: 'lead_name', type: 'short_text' }, text: values.name },\n values.email !== null && { type: 'email', field: { id: 'f2', ref: 'lead_email', type: 'email' }, email: values.email },\n values.company !== null && { type: 'text', field: { id: 'f3', ref: 'lead_company', type: 'short_text' }, text: values.company },\n values.phone !== null && { type: 'phone_number', field: { id: 'f4', ref: 'lead_phone', type: 'phone_number' }, phone_number: values.phone },\n ].filter(Boolean) as TypeformWebhook['form_response']['answers']\n return {\n event_id: 'evt_1',\n event_type: 'form_response',\n form_response: { form_id: 'frm_1', token: 'tok_1', submitted_at: '2026-10-01T09:00:00Z', answers },\n }\n}\n\nfunction config(fetch: typeof globalThis.fetch): ConnectorConfig {\n return {\n pipedriveApiToken: 'test-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n fetch,\n }\n}\n\ndescribe('handle', () => {\n it('creates organization, person and deal with the mapped fields', async () => {\n const fetch = fakeFetch({ '/organizations': ok(11), '/persons': ok(22), '/deals': ok(33) })\n const result = await handle(payload(), config(fetch))\n\n expect(result).toEqual({ personId: 22, organizationId: 11, dealId: 33 })\n const bodies = fetch.mock.calls.map(([url, init]) => [String(url), JSON.parse(String(init?.body))])\n expect(bodies).toEqual([\n ['https://api.pipedrive.com/v1/organizations', { name: 'Analytical Engines Ltd' }],\n ['https://api.pipedrive.com/v1/persons', {\n name: 'Ada Lovelace',\n email: [{ value: 'ada@example.com', primary: true, label: 'work' }],\n phone: [{ value: '+44 20 7946 0000', primary: true, label: 'work' }],\n org_id: 11,\n }],\n ['https://api.pipedrive.com/v1/deals', { title: 'Website lead: Analytical Engines Ltd', person_id: 22, org_id: 11 }],\n ])\n })\n\n it('authenticates every call with the x-api-token header', async () => {\n const fetch = fakeFetch({ '/organizations': ok(1), '/persons': ok(2), '/deals': ok(3) })\n await handle(payload(), config(fetch))\n for (const [, init] of fetch.mock.calls) {\n expect(new Headers(init?.headers).get('x-api-token')).toBe('test-token')\n expect(init?.method).toBe('POST')\n }\n })\n\n it('skips the organization when no company was answered', async () => {\n const fetch = fakeFetch({ '/persons': ok(2), '/deals': ok(3) })\n const result = await handle(payload({ company: null }), config(fetch))\n expect(result.organizationId).toBeNull()\n expect(fetch).toHaveBeenCalledTimes(2)\n const deal = JSON.parse(String(fetch.mock.calls[1]?.[1]?.body))\n expect(deal).toEqual({ title: 'Website lead: Ada Lovelace', person_id: 2 })\n })\n\n it('rejects a response without an email answer', async () => {\n const fetch = fakeFetch({})\n await expect(handle(payload({ email: null }), config(fetch))).rejects.toThrow(/lead_email/)\n expect(fetch).not.toHaveBeenCalled()\n })\n\n it('surfaces Pipedrive errors with status and endpoint', async () => {\n const fetch = fakeFetch({\n '/organizations': ok(1),\n '/persons': { status: 401, body: { success: false, error: 'unauthorized access' } },\n })\n await expect(handle(payload(), config(fetch))).rejects.toThrow('Pipedrive POST /persons failed with 401: unauthorized access')\n })\n\n it('ignores events that are not form responses', async () => {\n const fetch = fakeFetch({})\n const event = { ...payload(), event_type: 'form_deleted' } as unknown as TypeformWebhook\n await expect(handle(event, config(fetch))).rejects.toThrow(/Unsupported Typeform event/)\n })\n})\n\ndescribe('contract', () => {\n it('handles the documented Typeform payload against the documented Pipedrive responses', async () => {\n const { fetch, calls } = contractFetch()\n const result = await handle(SOURCE_EXAMPLE as unknown as TypeformWebhook, {\n pipedriveApiToken: 'contract-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email' },\n fetch,\n })\n expect(result.personId).toBeGreaterThan(0)\n expect(result.dealId).toBeGreaterThan(0)\n expect(calls.map((c) => c.url)).toEqual(['https://api.pipedrive.com/v1/persons', 'https://api.pipedrive.com/v1/deals'])\n })\n})\n"
|
|
562
|
+
},
|
|
563
|
+
{
|
|
564
|
+
path: "README.md",
|
|
565
|
+
content: "# Typeform \u2192 Pipedrive connector\n\nTurns a Typeform `form_response` webhook into a Pipedrive organization, person and deal.\n\n## Configuration\n\n| Env var | Used as | Notes |\n| --- | --- | --- |\n| `PIPEDRIVE_API_TOKEN` | `pipedriveApiToken` | Personal API token, sent as the `x-api-token` header |\n\n`fieldRefs` maps your Typeform field refs to the lead fields. `name` and `email` are required; `company` and `phone` are optional.\n\n## Usage\n\n```ts\nimport { handle } from './src/connector'\n\nexport async function POST(request: Request) {\n const payload = await request.json()\n const result = await handle(payload, {\n pipedriveApiToken: process.env.PIPEDRIVE_API_TOKEN!,\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n })\n return Response.json(result)\n}\n```\n\n## Behaviour\n\n1. Reads the answers by field ref, rejects the event when name or email is missing.\n2. Creates an organization when a company was answered (`POST /v1/organizations`).\n3. Creates the person with email and phone, linked to the organization (`POST /v1/persons`).\n4. Creates a deal for the person (`POST /v1/deals`).\n\nAny non-2xx or `success: false` response throws with the status code and endpoint, so the webhook caller can retry.\n\n## Assumptions\n\n- Typeform webhook signature verification happens before `handle` is called.\n- Every submission is a new lead; the connector does not search for existing persons.\n"
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
path: "src/fixtures.ts",
|
|
569
|
+
content: `// Written by patchbay from the API documentation. Regenerated on every attempt, do not edit.
|
|
570
|
+
// Source: https://www.typeform.com/developers/webhooks/example-payload/
|
|
571
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Organizations
|
|
572
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Persons
|
|
573
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Deals
|
|
574
|
+
|
|
575
|
+
/** Example payload of the source event, as documented. */
|
|
576
|
+
export const SOURCE_EXAMPLE = {
|
|
577
|
+
"event_id": "01H...",
|
|
578
|
+
"event_type": "form_response",
|
|
579
|
+
"form_response": {
|
|
580
|
+
"form_id": "lT4Z3j",
|
|
581
|
+
"token": "a3a12ec67a1365927098a606107fac15",
|
|
582
|
+
"submitted_at": "2026-10-01T09:00:00Z",
|
|
583
|
+
"answers": [
|
|
584
|
+
{
|
|
585
|
+
"type": "text",
|
|
586
|
+
"field": {
|
|
587
|
+
"id": "f1",
|
|
588
|
+
"ref": "lead_name",
|
|
589
|
+
"type": "short_text"
|
|
590
|
+
},
|
|
591
|
+
"text": "Ada Lovelace"
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
"type": "email",
|
|
595
|
+
"field": {
|
|
596
|
+
"id": "f2",
|
|
597
|
+
"ref": "lead_email",
|
|
598
|
+
"type": "email"
|
|
599
|
+
},
|
|
600
|
+
"email": "ada@example.com"
|
|
601
|
+
}
|
|
602
|
+
]
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export interface DocumentedEndpoint {
|
|
607
|
+
method: string
|
|
608
|
+
/** As documented; may contain placeholders such as {id}, :id, <id> or a region host like usX. */
|
|
609
|
+
url: string
|
|
610
|
+
response: unknown
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** Target endpoints the connector may call, in the documented order, with their documented responses. */
|
|
614
|
+
export const TARGET_ENDPOINTS: DocumentedEndpoint[] = [
|
|
615
|
+
{
|
|
616
|
+
"method": "POST",
|
|
617
|
+
"url": "https://api.pipedrive.com/v1/organizations",
|
|
618
|
+
"response": {
|
|
619
|
+
"success": true,
|
|
620
|
+
"data": {
|
|
621
|
+
"id": 11
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
{
|
|
626
|
+
"method": "POST",
|
|
627
|
+
"url": "https://api.pipedrive.com/v1/persons",
|
|
628
|
+
"response": {
|
|
629
|
+
"success": true,
|
|
630
|
+
"data": {
|
|
631
|
+
"id": 22
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
{
|
|
636
|
+
"method": "POST",
|
|
637
|
+
"url": "https://api.pipedrive.com/v1/deals",
|
|
638
|
+
"response": {
|
|
639
|
+
"success": true,
|
|
640
|
+
"data": {
|
|
641
|
+
"id": 33
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
]
|
|
646
|
+
|
|
647
|
+
export interface RecordedCall {
|
|
648
|
+
method: string
|
|
649
|
+
url: string
|
|
650
|
+
/** The request body exactly as sent (usually a JSON string). */
|
|
651
|
+
body: string | undefined
|
|
652
|
+
/** The body parsed as JSON, or undefined when it is not JSON. */
|
|
653
|
+
json: unknown
|
|
654
|
+
headers: Headers
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** The documented endpoint a request matches, placeholders and region subdomains included. */
|
|
658
|
+
export function findEndpoint(method: string, url: string): DocumentedEndpoint | undefined {
|
|
659
|
+
return TARGET_ENDPOINTS.find((endpoint) => endpoint.method === method.toUpperCase() && urlMatches(endpoint.url, url))
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* A fake fetch that answers every documented endpoint with its documented response and
|
|
664
|
+
* rejects anything else. Use it in the contract test:
|
|
665
|
+
*
|
|
666
|
+
* const { fetch, calls } = contractFetch()
|
|
667
|
+
* await handle(SOURCE_EXAMPLE as MyInput, { ...config, fetch })
|
|
668
|
+
* expect(calls.length).toBeGreaterThan(0)
|
|
669
|
+
*/
|
|
670
|
+
export function contractFetch(): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
671
|
+
return fakeFetch((request) => {
|
|
672
|
+
const endpoint = findEndpoint(request.method, request.url)
|
|
673
|
+
if (!endpoint) throw new Error(\`contract: \${request.method} \${request.url} is not a documented endpoint\`)
|
|
674
|
+
return jsonResponse(endpoint.response)
|
|
675
|
+
})
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* A correctly typed fake fetch for every other test. The handler receives the recorded
|
|
680
|
+
* request: { method, url, body (string as sent), json (parsed body), headers (Headers) }.
|
|
681
|
+
*
|
|
682
|
+
* const { fetch, calls } = fakeFetch((req) => req.url.endsWith('/deals') ? jsonResponse({ id: 1 }, 201) : jsonResponse({}, 404))
|
|
683
|
+
*/
|
|
684
|
+
export function fakeFetch(
|
|
685
|
+
handler: (request: RecordedCall) => Response | Promise<Response>,
|
|
686
|
+
): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
687
|
+
const calls: RecordedCall[] = []
|
|
688
|
+
const fake = async (input: string | URL | Request, init: RequestInit = {}): Promise<Response> => {
|
|
689
|
+
const url = input instanceof Request ? input.url : String(input)
|
|
690
|
+
const method = (init.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase()
|
|
691
|
+
const body = typeof init.body === 'string' ? init.body : init.body == null ? undefined : String(init.body)
|
|
692
|
+
const call: RecordedCall = { method, url, body, json: parseJson(body), headers: new Headers(init.headers) }
|
|
693
|
+
calls.push(call)
|
|
694
|
+
return handler(call)
|
|
695
|
+
}
|
|
696
|
+
return { fetch: fake as typeof fetch, calls }
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const STATUS_TEXT: Record<number, string> = {
|
|
700
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',
|
|
701
|
+
400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 409: 'Conflict',
|
|
702
|
+
422: 'Unprocessable Entity', 429: 'Too Many Requests',
|
|
703
|
+
500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** A JSON Response with the standard status text, e.g. jsonResponse({ error: 'nope' }, 401) -> "401 Unauthorized". */
|
|
707
|
+
export function jsonResponse(body: unknown, status = 200): Response {
|
|
708
|
+
const init = { status, statusText: STATUS_TEXT[status] ?? '', headers: { 'Content-Type': 'application/json' } }
|
|
709
|
+
return new Response(status === 204 ? null : JSON.stringify(body), init)
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function parseJson(raw: string | undefined): unknown {
|
|
713
|
+
if (raw === undefined) return undefined
|
|
714
|
+
try {
|
|
715
|
+
return JSON.parse(raw)
|
|
716
|
+
} catch {
|
|
717
|
+
return undefined
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function urlMatches(template: string, actual: string): boolean {
|
|
722
|
+
const t = splitUrl(template)
|
|
723
|
+
const a = splitUrl(actual)
|
|
724
|
+
if (!t || !a) return false
|
|
725
|
+
const tHost = t.host.split('.')
|
|
726
|
+
const aHost = a.host.split('.')
|
|
727
|
+
if (tHost.slice(-2).join('.') !== aHost.slice(-2).join('.')) return false
|
|
728
|
+
const tPath = t.path.replace(/\\/+$/, '').split('/')
|
|
729
|
+
const aPath = a.path.replace(/\\/+$/, '').split('/')
|
|
730
|
+
if (tPath.length !== aPath.length) return false
|
|
731
|
+
return tPath.every((segment, i) => isPlaceholder(segment) || segment === aPath[i])
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
function splitUrl(url: string): { host: string; path: string } | null {
|
|
735
|
+
const match = url.match(/^https?:\\/\\/([^/?#]+)([^?#]*)/i)
|
|
736
|
+
return match ? { host: (match[1] ?? '').toLowerCase(), path: match[2] || '/' } : null
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
function isPlaceholder(segment: string): boolean {
|
|
740
|
+
return /^\\{[^}]+\\}$|^:[A-Za-z_]\\w*$|^<[^>]+>$/.test(segment)
|
|
741
|
+
}
|
|
742
|
+
`
|
|
743
|
+
}
|
|
744
|
+
]
|
|
745
|
+
},
|
|
746
|
+
{
|
|
747
|
+
type: "step",
|
|
748
|
+
step: "generate",
|
|
749
|
+
status: "done",
|
|
750
|
+
detail: "src/connector.ts, src/connector.test.ts, README.md"
|
|
751
|
+
},
|
|
752
|
+
{
|
|
753
|
+
type: "log",
|
|
754
|
+
step: "generate",
|
|
755
|
+
message: "Wrote src/fixtures.ts from the documented example payload and responses"
|
|
756
|
+
},
|
|
757
|
+
{
|
|
758
|
+
type: "step",
|
|
759
|
+
step: "verify",
|
|
760
|
+
status: "start"
|
|
761
|
+
},
|
|
762
|
+
{
|
|
763
|
+
type: "log",
|
|
764
|
+
step: "verify",
|
|
765
|
+
message: "Reusing prepared sandbox image (node 22, typescript, vitest)"
|
|
766
|
+
},
|
|
767
|
+
{
|
|
768
|
+
type: "log",
|
|
769
|
+
step: "verify",
|
|
770
|
+
message: "Forking checkpoint with 4 files, network off"
|
|
771
|
+
},
|
|
772
|
+
{
|
|
773
|
+
type: "tests",
|
|
774
|
+
attempt: 1,
|
|
775
|
+
report: {
|
|
776
|
+
typecheckOk: true,
|
|
777
|
+
typecheckOutput: "",
|
|
778
|
+
passed: 5,
|
|
779
|
+
failed: 2,
|
|
780
|
+
total: 7,
|
|
781
|
+
failures: [
|
|
782
|
+
{
|
|
783
|
+
name: "handle creates organization, person and deal with the mapped fields",
|
|
784
|
+
message: "AssertionError: expected [ [ \u2026(2) ], [ \u2026(2) ], [ \u2026(2) ] ] to deeply equal [ [ \u2026(2) ], [ \u2026(2) ], [ \u2026(2) ] ]\n at /work/src/connector.test.ts:48:20\n at processTicksAndRejections (node:internal/process/task_queues:104:5)\n at file:///C:/Users/vogel/Desktop/patchbay/node_modules/@vitest/runner/dist/chunk-artifact.js:1903:20"
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
name: "handle authenticates every call with the x-api-token header",
|
|
788
|
+
message: "AssertionError: expected null to be 'test-token' // Object.is equality\n at /work/src/connector.test.ts:64:61\n at processTicksAndRejections (node:internal/process/task_queues:104:5)\n at file:///C:/Users/vogel/Desktop/patchbay/node_modules/@vitest/runner/dist/chunk-artifact.js:1903:20"
|
|
789
|
+
}
|
|
790
|
+
],
|
|
791
|
+
durationMs: 199.913818359375,
|
|
792
|
+
ranOn: "replay",
|
|
793
|
+
passedNames: [
|
|
794
|
+
"handle skips the organization when no company was answered",
|
|
795
|
+
"handle rejects a response without an email answer",
|
|
796
|
+
"handle surfaces Pipedrive errors with status and endpoint",
|
|
797
|
+
"handle ignores events that are not form responses",
|
|
798
|
+
"contract handles the documented Typeform payload against the documented Pipedrive responses"
|
|
799
|
+
],
|
|
800
|
+
contract: "passed"
|
|
801
|
+
}
|
|
802
|
+
},
|
|
803
|
+
{
|
|
804
|
+
type: "log",
|
|
805
|
+
step: "verify",
|
|
806
|
+
message: "Attempt 1 failed (2 tests, typecheck ok), repairing"
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
type: "files",
|
|
810
|
+
attempt: 2,
|
|
811
|
+
files: [
|
|
812
|
+
{
|
|
813
|
+
path: "src/connector.ts",
|
|
814
|
+
content: "export interface ConnectorConfig {\n /** Pipedrive personal API token (env: PIPEDRIVE_API_TOKEN). */\n pipedriveApiToken: string\n /** Defaults to https://api.pipedrive.com/v1 */\n pipedriveBaseUrl?: string\n /** Typeform field refs that hold the lead's data. */\n fieldRefs: { name: string; email: string; company?: string; phone?: string }\n /** Prefix for the deal title, e.g. \"Website lead\". */\n dealTitlePrefix?: string\n fetch?: typeof fetch\n}\n\nexport interface TypeformAnswer {\n type: string\n field: { id: string; ref: string; type: string }\n text?: string\n email?: string\n phone_number?: string\n}\n\nexport interface TypeformWebhook {\n event_id: string\n event_type: 'form_response'\n form_response: {\n form_id: string\n token: string\n submitted_at: string\n answers: TypeformAnswer[]\n }\n}\n\nexport interface HandleResult {\n personId: number\n organizationId: number | null\n dealId: number\n}\n\ninterface PipedriveResponse {\n success: boolean\n data?: { id: number }\n error?: string\n}\n\nexport async function handle(payload: TypeformWebhook, config: ConnectorConfig): Promise<HandleResult> {\n if (payload?.event_type !== 'form_response') {\n throw new Error(`Unsupported Typeform event: ${String(payload?.event_type)}`)\n }\n const answers = payload.form_response.answers ?? []\n const name = answerValue(answers, config.fieldRefs.name)\n const email = answerValue(answers, config.fieldRefs.email)\n if (!name) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.name}\" (name)`)\n if (!email) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.email}\" (email)`)\n const company = config.fieldRefs.company ? answerValue(answers, config.fieldRefs.company) : undefined\n const phone = config.fieldRefs.phone ? answerValue(answers, config.fieldRefs.phone) : undefined\n\n const organizationId = company ? (await post(config, '/organizations', { name: company })).id : null\n\n const person = await post(config, '/persons', {\n name,\n email: [{ value: email, primary: true, label: 'work' }],\n ...(phone ? { phone: [{ value: phone, primary: true, label: 'work' }] } : {}),\n ...(organizationId ? { org_id: organizationId } : {}),\n })\n\n const deal = await post(config, '/deals', {\n title: `${config.dealTitlePrefix ?? 'Typeform lead'}: ${company ?? name}`,\n person_id: person.id,\n ...(organizationId ? { org_id: organizationId } : {}),\n })\n\n return { personId: person.id, organizationId, dealId: deal.id }\n}\n\nfunction answerValue(answers: TypeformAnswer[], ref: string): string | undefined {\n const answer = answers.find((a) => a.field?.ref === ref)\n if (!answer) return undefined\n const value = answer.email ?? answer.phone_number ?? answer.text\n return value?.trim() || undefined\n}\n\nasync function post(config: ConnectorConfig, path: string, body: Record<string, unknown>): Promise<{ id: number }> {\n const doFetch = config.fetch ?? fetch\n const url = `${config.pipedriveBaseUrl ?? 'https://api.pipedrive.com/v1'}${path}`\n const res = await doFetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', 'x-api-token': config.pipedriveApiToken },\n body: JSON.stringify(body),\n })\n const data = (await res.json().catch(() => ({}))) as PipedriveResponse\n if (!res.ok || !data.success || !data.data) {\n throw new Error(`Pipedrive POST ${path} failed with ${res.status}: ${data.error ?? 'no error message'}`)\n }\n return data.data\n}\n"
|
|
815
|
+
},
|
|
816
|
+
{
|
|
817
|
+
path: "src/connector.test.ts",
|
|
818
|
+
content: "import { describe, it, expect, vi } from 'vitest'\nimport { handle, type ConnectorConfig, type TypeformWebhook } from './connector'\nimport { SOURCE_EXAMPLE, contractFetch } from './fixtures'\n\nfunction fakeFetch(responses: Record<string, { status?: number; body: unknown }>) {\n return vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {\n const url = String(input)\n const path = Object.keys(responses).find((p) => url.endsWith(p))\n const response = path ? responses[path] : undefined\n if (!response) throw new Error(`unexpected request to ${url} ${init?.method}`)\n return new Response(JSON.stringify(response.body), { status: response.status ?? 201 })\n })\n}\n\nconst ok = (id: number) => ({ body: { success: true, data: { id } } })\n\nfunction payload(overrides: Partial<Record<'name' | 'email' | 'company' | 'phone', string | null>> = {}): TypeformWebhook {\n const values = { name: 'Ada Lovelace', email: 'ada@example.com', company: 'Analytical Engines Ltd', phone: '+44 20 7946 0000', ...overrides }\n const answers = [\n values.name !== null && { type: 'text', field: { id: 'f1', ref: 'lead_name', type: 'short_text' }, text: values.name },\n values.email !== null && { type: 'email', field: { id: 'f2', ref: 'lead_email', type: 'email' }, email: values.email },\n values.company !== null && { type: 'text', field: { id: 'f3', ref: 'lead_company', type: 'short_text' }, text: values.company },\n values.phone !== null && { type: 'phone_number', field: { id: 'f4', ref: 'lead_phone', type: 'phone_number' }, phone_number: values.phone },\n ].filter(Boolean) as TypeformWebhook['form_response']['answers']\n return {\n event_id: 'evt_1',\n event_type: 'form_response',\n form_response: { form_id: 'frm_1', token: 'tok_1', submitted_at: '2026-10-01T09:00:00Z', answers },\n }\n}\n\nfunction config(fetch: typeof globalThis.fetch): ConnectorConfig {\n return {\n pipedriveApiToken: 'test-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n fetch,\n }\n}\n\ndescribe('handle', () => {\n it('creates organization, person and deal with the mapped fields', async () => {\n const fetch = fakeFetch({ '/organizations': ok(11), '/persons': ok(22), '/deals': ok(33) })\n const result = await handle(payload(), config(fetch))\n\n expect(result).toEqual({ personId: 22, organizationId: 11, dealId: 33 })\n const bodies = fetch.mock.calls.map(([url, init]) => [String(url), JSON.parse(String(init?.body))])\n expect(bodies).toEqual([\n ['https://api.pipedrive.com/v1/organizations', { name: 'Analytical Engines Ltd' }],\n ['https://api.pipedrive.com/v1/persons', {\n name: 'Ada Lovelace',\n email: [{ value: 'ada@example.com', primary: true, label: 'work' }],\n phone: [{ value: '+44 20 7946 0000', primary: true, label: 'work' }],\n org_id: 11,\n }],\n ['https://api.pipedrive.com/v1/deals', { title: 'Website lead: Analytical Engines Ltd', person_id: 22, org_id: 11 }],\n ])\n })\n\n it('authenticates every call with the x-api-token header', async () => {\n const fetch = fakeFetch({ '/organizations': ok(1), '/persons': ok(2), '/deals': ok(3) })\n await handle(payload(), config(fetch))\n for (const [, init] of fetch.mock.calls) {\n expect(new Headers(init?.headers).get('x-api-token')).toBe('test-token')\n expect(init?.method).toBe('POST')\n }\n })\n\n it('skips the organization when no company was answered', async () => {\n const fetch = fakeFetch({ '/persons': ok(2), '/deals': ok(3) })\n const result = await handle(payload({ company: null }), config(fetch))\n expect(result.organizationId).toBeNull()\n expect(fetch).toHaveBeenCalledTimes(2)\n const deal = JSON.parse(String(fetch.mock.calls[1]?.[1]?.body))\n expect(deal).toEqual({ title: 'Website lead: Ada Lovelace', person_id: 2 })\n })\n\n it('rejects a response without an email answer', async () => {\n const fetch = fakeFetch({})\n await expect(handle(payload({ email: null }), config(fetch))).rejects.toThrow(/lead_email/)\n expect(fetch).not.toHaveBeenCalled()\n })\n\n it('surfaces Pipedrive errors with status and endpoint', async () => {\n const fetch = fakeFetch({\n '/organizations': ok(1),\n '/persons': { status: 401, body: { success: false, error: 'unauthorized access' } },\n })\n await expect(handle(payload(), config(fetch))).rejects.toThrow('Pipedrive POST /persons failed with 401: unauthorized access')\n })\n\n it('ignores events that are not form responses', async () => {\n const fetch = fakeFetch({})\n const event = { ...payload(), event_type: 'form_deleted' } as unknown as TypeformWebhook\n await expect(handle(event, config(fetch))).rejects.toThrow(/Unsupported Typeform event/)\n })\n})\n\ndescribe('contract', () => {\n it('handles the documented Typeform payload against the documented Pipedrive responses', async () => {\n const { fetch, calls } = contractFetch()\n const result = await handle(SOURCE_EXAMPLE as unknown as TypeformWebhook, {\n pipedriveApiToken: 'contract-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email' },\n fetch,\n })\n expect(result.personId).toBeGreaterThan(0)\n expect(result.dealId).toBeGreaterThan(0)\n expect(calls.map((c) => c.url)).toEqual(['https://api.pipedrive.com/v1/persons', 'https://api.pipedrive.com/v1/deals'])\n })\n})\n"
|
|
819
|
+
},
|
|
820
|
+
{
|
|
821
|
+
path: "README.md",
|
|
822
|
+
content: "# Typeform \u2192 Pipedrive connector\n\nTurns a Typeform `form_response` webhook into a Pipedrive organization, person and deal.\n\n## Configuration\n\n| Env var | Used as | Notes |\n| --- | --- | --- |\n| `PIPEDRIVE_API_TOKEN` | `pipedriveApiToken` | Personal API token, sent as the `x-api-token` header |\n\n`fieldRefs` maps your Typeform field refs to the lead fields. `name` and `email` are required; `company` and `phone` are optional.\n\n## Usage\n\n```ts\nimport { handle } from './src/connector'\n\nexport async function POST(request: Request) {\n const payload = await request.json()\n const result = await handle(payload, {\n pipedriveApiToken: process.env.PIPEDRIVE_API_TOKEN!,\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n })\n return Response.json(result)\n}\n```\n\n## Behaviour\n\n1. Reads the answers by field ref, rejects the event when name or email is missing.\n2. Creates an organization when a company was answered (`POST /v1/organizations`).\n3. Creates the person with email and phone, linked to the organization (`POST /v1/persons`).\n4. Creates a deal for the person (`POST /v1/deals`).\n\nAny non-2xx or `success: false` response throws with the status code and endpoint, so the webhook caller can retry.\n\n## Assumptions\n\n- Typeform webhook signature verification happens before `handle` is called.\n- Every submission is a new lead; the connector does not search for existing persons.\n"
|
|
823
|
+
},
|
|
824
|
+
{
|
|
825
|
+
path: "src/fixtures.ts",
|
|
826
|
+
content: `// Written by patchbay from the API documentation. Regenerated on every attempt, do not edit.
|
|
827
|
+
// Source: https://www.typeform.com/developers/webhooks/example-payload/
|
|
828
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Organizations
|
|
829
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Persons
|
|
830
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Deals
|
|
831
|
+
|
|
832
|
+
/** Example payload of the source event, as documented. */
|
|
833
|
+
export const SOURCE_EXAMPLE = {
|
|
834
|
+
"event_id": "01H...",
|
|
835
|
+
"event_type": "form_response",
|
|
836
|
+
"form_response": {
|
|
837
|
+
"form_id": "lT4Z3j",
|
|
838
|
+
"token": "a3a12ec67a1365927098a606107fac15",
|
|
839
|
+
"submitted_at": "2026-10-01T09:00:00Z",
|
|
840
|
+
"answers": [
|
|
841
|
+
{
|
|
842
|
+
"type": "text",
|
|
843
|
+
"field": {
|
|
844
|
+
"id": "f1",
|
|
845
|
+
"ref": "lead_name",
|
|
846
|
+
"type": "short_text"
|
|
847
|
+
},
|
|
848
|
+
"text": "Ada Lovelace"
|
|
849
|
+
},
|
|
850
|
+
{
|
|
851
|
+
"type": "email",
|
|
852
|
+
"field": {
|
|
853
|
+
"id": "f2",
|
|
854
|
+
"ref": "lead_email",
|
|
855
|
+
"type": "email"
|
|
856
|
+
},
|
|
857
|
+
"email": "ada@example.com"
|
|
858
|
+
}
|
|
859
|
+
]
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export interface DocumentedEndpoint {
|
|
864
|
+
method: string
|
|
865
|
+
/** As documented; may contain placeholders such as {id}, :id, <id> or a region host like usX. */
|
|
866
|
+
url: string
|
|
867
|
+
response: unknown
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Target endpoints the connector may call, in the documented order, with their documented responses. */
|
|
871
|
+
export const TARGET_ENDPOINTS: DocumentedEndpoint[] = [
|
|
872
|
+
{
|
|
873
|
+
"method": "POST",
|
|
874
|
+
"url": "https://api.pipedrive.com/v1/organizations",
|
|
875
|
+
"response": {
|
|
876
|
+
"success": true,
|
|
877
|
+
"data": {
|
|
878
|
+
"id": 11
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
"method": "POST",
|
|
884
|
+
"url": "https://api.pipedrive.com/v1/persons",
|
|
885
|
+
"response": {
|
|
886
|
+
"success": true,
|
|
887
|
+
"data": {
|
|
888
|
+
"id": 22
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
},
|
|
892
|
+
{
|
|
893
|
+
"method": "POST",
|
|
894
|
+
"url": "https://api.pipedrive.com/v1/deals",
|
|
895
|
+
"response": {
|
|
896
|
+
"success": true,
|
|
897
|
+
"data": {
|
|
898
|
+
"id": 33
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
]
|
|
903
|
+
|
|
904
|
+
export interface RecordedCall {
|
|
905
|
+
method: string
|
|
906
|
+
url: string
|
|
907
|
+
/** The request body exactly as sent (usually a JSON string). */
|
|
908
|
+
body: string | undefined
|
|
909
|
+
/** The body parsed as JSON, or undefined when it is not JSON. */
|
|
910
|
+
json: unknown
|
|
911
|
+
headers: Headers
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/** The documented endpoint a request matches, placeholders and region subdomains included. */
|
|
915
|
+
export function findEndpoint(method: string, url: string): DocumentedEndpoint | undefined {
|
|
916
|
+
return TARGET_ENDPOINTS.find((endpoint) => endpoint.method === method.toUpperCase() && urlMatches(endpoint.url, url))
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* A fake fetch that answers every documented endpoint with its documented response and
|
|
921
|
+
* rejects anything else. Use it in the contract test:
|
|
922
|
+
*
|
|
923
|
+
* const { fetch, calls } = contractFetch()
|
|
924
|
+
* await handle(SOURCE_EXAMPLE as MyInput, { ...config, fetch })
|
|
925
|
+
* expect(calls.length).toBeGreaterThan(0)
|
|
926
|
+
*/
|
|
927
|
+
export function contractFetch(): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
928
|
+
return fakeFetch((request) => {
|
|
929
|
+
const endpoint = findEndpoint(request.method, request.url)
|
|
930
|
+
if (!endpoint) throw new Error(\`contract: \${request.method} \${request.url} is not a documented endpoint\`)
|
|
931
|
+
return jsonResponse(endpoint.response)
|
|
932
|
+
})
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* A correctly typed fake fetch for every other test. The handler receives the recorded
|
|
937
|
+
* request: { method, url, body (string as sent), json (parsed body), headers (Headers) }.
|
|
938
|
+
*
|
|
939
|
+
* const { fetch, calls } = fakeFetch((req) => req.url.endsWith('/deals') ? jsonResponse({ id: 1 }, 201) : jsonResponse({}, 404))
|
|
940
|
+
*/
|
|
941
|
+
export function fakeFetch(
|
|
942
|
+
handler: (request: RecordedCall) => Response | Promise<Response>,
|
|
943
|
+
): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
944
|
+
const calls: RecordedCall[] = []
|
|
945
|
+
const fake = async (input: string | URL | Request, init: RequestInit = {}): Promise<Response> => {
|
|
946
|
+
const url = input instanceof Request ? input.url : String(input)
|
|
947
|
+
const method = (init.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase()
|
|
948
|
+
const body = typeof init.body === 'string' ? init.body : init.body == null ? undefined : String(init.body)
|
|
949
|
+
const call: RecordedCall = { method, url, body, json: parseJson(body), headers: new Headers(init.headers) }
|
|
950
|
+
calls.push(call)
|
|
951
|
+
return handler(call)
|
|
952
|
+
}
|
|
953
|
+
return { fetch: fake as typeof fetch, calls }
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const STATUS_TEXT: Record<number, string> = {
|
|
957
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',
|
|
958
|
+
400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 409: 'Conflict',
|
|
959
|
+
422: 'Unprocessable Entity', 429: 'Too Many Requests',
|
|
960
|
+
500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/** A JSON Response with the standard status text, e.g. jsonResponse({ error: 'nope' }, 401) -> "401 Unauthorized". */
|
|
964
|
+
export function jsonResponse(body: unknown, status = 200): Response {
|
|
965
|
+
const init = { status, statusText: STATUS_TEXT[status] ?? '', headers: { 'Content-Type': 'application/json' } }
|
|
966
|
+
return new Response(status === 204 ? null : JSON.stringify(body), init)
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
function parseJson(raw: string | undefined): unknown {
|
|
970
|
+
if (raw === undefined) return undefined
|
|
971
|
+
try {
|
|
972
|
+
return JSON.parse(raw)
|
|
973
|
+
} catch {
|
|
974
|
+
return undefined
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
function urlMatches(template: string, actual: string): boolean {
|
|
979
|
+
const t = splitUrl(template)
|
|
980
|
+
const a = splitUrl(actual)
|
|
981
|
+
if (!t || !a) return false
|
|
982
|
+
const tHost = t.host.split('.')
|
|
983
|
+
const aHost = a.host.split('.')
|
|
984
|
+
if (tHost.slice(-2).join('.') !== aHost.slice(-2).join('.')) return false
|
|
985
|
+
const tPath = t.path.replace(/\\/+$/, '').split('/')
|
|
986
|
+
const aPath = a.path.replace(/\\/+$/, '').split('/')
|
|
987
|
+
if (tPath.length !== aPath.length) return false
|
|
988
|
+
return tPath.every((segment, i) => isPlaceholder(segment) || segment === aPath[i])
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
function splitUrl(url: string): { host: string; path: string } | null {
|
|
992
|
+
const match = url.match(/^https?:\\/\\/([^/?#]+)([^?#]*)/i)
|
|
993
|
+
return match ? { host: (match[1] ?? '').toLowerCase(), path: match[2] || '/' } : null
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
function isPlaceholder(segment: string): boolean {
|
|
997
|
+
return /^\\{[^}]+\\}$|^:[A-Za-z_]\\w*$|^<[^>]+>$/.test(segment)
|
|
998
|
+
}
|
|
999
|
+
`
|
|
1000
|
+
}
|
|
1001
|
+
]
|
|
1002
|
+
},
|
|
1003
|
+
{
|
|
1004
|
+
type: "log",
|
|
1005
|
+
step: "verify",
|
|
1006
|
+
message: "Forking checkpoint with 4 files, network off"
|
|
1007
|
+
},
|
|
1008
|
+
{
|
|
1009
|
+
type: "tests",
|
|
1010
|
+
attempt: 2,
|
|
1011
|
+
report: {
|
|
1012
|
+
typecheckOk: true,
|
|
1013
|
+
typecheckOutput: "",
|
|
1014
|
+
passed: 7,
|
|
1015
|
+
failed: 0,
|
|
1016
|
+
total: 7,
|
|
1017
|
+
failures: [],
|
|
1018
|
+
durationMs: 202.9775390625,
|
|
1019
|
+
ranOn: "replay",
|
|
1020
|
+
passedNames: [
|
|
1021
|
+
"handle creates organization, person and deal with the mapped fields",
|
|
1022
|
+
"handle authenticates every call with the x-api-token header",
|
|
1023
|
+
"handle skips the organization when no company was answered",
|
|
1024
|
+
"handle rejects a response without an email answer",
|
|
1025
|
+
"handle surfaces Pipedrive errors with status and endpoint",
|
|
1026
|
+
"handle ignores events that are not form responses",
|
|
1027
|
+
"contract handles the documented Typeform payload against the documented Pipedrive responses"
|
|
1028
|
+
],
|
|
1029
|
+
contract: "passed"
|
|
1030
|
+
}
|
|
1031
|
+
},
|
|
1032
|
+
{
|
|
1033
|
+
type: "step",
|
|
1034
|
+
step: "verify",
|
|
1035
|
+
status: "done",
|
|
1036
|
+
detail: "7/7 tests after 2 attempts"
|
|
1037
|
+
},
|
|
1038
|
+
{
|
|
1039
|
+
type: "step",
|
|
1040
|
+
step: "package",
|
|
1041
|
+
status: "start"
|
|
1042
|
+
},
|
|
1043
|
+
{
|
|
1044
|
+
type: "result",
|
|
1045
|
+
ok: true,
|
|
1046
|
+
attempts: 2,
|
|
1047
|
+
files: [
|
|
1048
|
+
{
|
|
1049
|
+
path: "src/connector.ts",
|
|
1050
|
+
content: "export interface ConnectorConfig {\n /** Pipedrive personal API token (env: PIPEDRIVE_API_TOKEN). */\n pipedriveApiToken: string\n /** Defaults to https://api.pipedrive.com/v1 */\n pipedriveBaseUrl?: string\n /** Typeform field refs that hold the lead's data. */\n fieldRefs: { name: string; email: string; company?: string; phone?: string }\n /** Prefix for the deal title, e.g. \"Website lead\". */\n dealTitlePrefix?: string\n fetch?: typeof fetch\n}\n\nexport interface TypeformAnswer {\n type: string\n field: { id: string; ref: string; type: string }\n text?: string\n email?: string\n phone_number?: string\n}\n\nexport interface TypeformWebhook {\n event_id: string\n event_type: 'form_response'\n form_response: {\n form_id: string\n token: string\n submitted_at: string\n answers: TypeformAnswer[]\n }\n}\n\nexport interface HandleResult {\n personId: number\n organizationId: number | null\n dealId: number\n}\n\ninterface PipedriveResponse {\n success: boolean\n data?: { id: number }\n error?: string\n}\n\nexport async function handle(payload: TypeformWebhook, config: ConnectorConfig): Promise<HandleResult> {\n if (payload?.event_type !== 'form_response') {\n throw new Error(`Unsupported Typeform event: ${String(payload?.event_type)}`)\n }\n const answers = payload.form_response.answers ?? []\n const name = answerValue(answers, config.fieldRefs.name)\n const email = answerValue(answers, config.fieldRefs.email)\n if (!name) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.name}\" (name)`)\n if (!email) throw new Error(`Missing required answer for field ref \"${config.fieldRefs.email}\" (email)`)\n const company = config.fieldRefs.company ? answerValue(answers, config.fieldRefs.company) : undefined\n const phone = config.fieldRefs.phone ? answerValue(answers, config.fieldRefs.phone) : undefined\n\n const organizationId = company ? (await post(config, '/organizations', { name: company })).id : null\n\n const person = await post(config, '/persons', {\n name,\n email: [{ value: email, primary: true, label: 'work' }],\n ...(phone ? { phone: [{ value: phone, primary: true, label: 'work' }] } : {}),\n ...(organizationId ? { org_id: organizationId } : {}),\n })\n\n const deal = await post(config, '/deals', {\n title: `${config.dealTitlePrefix ?? 'Typeform lead'}: ${company ?? name}`,\n person_id: person.id,\n ...(organizationId ? { org_id: organizationId } : {}),\n })\n\n return { personId: person.id, organizationId, dealId: deal.id }\n}\n\nfunction answerValue(answers: TypeformAnswer[], ref: string): string | undefined {\n const answer = answers.find((a) => a.field?.ref === ref)\n if (!answer) return undefined\n const value = answer.email ?? answer.phone_number ?? answer.text\n return value?.trim() || undefined\n}\n\nasync function post(config: ConnectorConfig, path: string, body: Record<string, unknown>): Promise<{ id: number }> {\n const doFetch = config.fetch ?? fetch\n const url = `${config.pipedriveBaseUrl ?? 'https://api.pipedrive.com/v1'}${path}`\n const res = await doFetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', 'x-api-token': config.pipedriveApiToken },\n body: JSON.stringify(body),\n })\n const data = (await res.json().catch(() => ({}))) as PipedriveResponse\n if (!res.ok || !data.success || !data.data) {\n throw new Error(`Pipedrive POST ${path} failed with ${res.status}: ${data.error ?? 'no error message'}`)\n }\n return data.data\n}\n"
|
|
1051
|
+
},
|
|
1052
|
+
{
|
|
1053
|
+
path: "src/connector.test.ts",
|
|
1054
|
+
content: "import { describe, it, expect, vi } from 'vitest'\nimport { handle, type ConnectorConfig, type TypeformWebhook } from './connector'\nimport { SOURCE_EXAMPLE, contractFetch } from './fixtures'\n\nfunction fakeFetch(responses: Record<string, { status?: number; body: unknown }>) {\n return vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {\n const url = String(input)\n const path = Object.keys(responses).find((p) => url.endsWith(p))\n const response = path ? responses[path] : undefined\n if (!response) throw new Error(`unexpected request to ${url} ${init?.method}`)\n return new Response(JSON.stringify(response.body), { status: response.status ?? 201 })\n })\n}\n\nconst ok = (id: number) => ({ body: { success: true, data: { id } } })\n\nfunction payload(overrides: Partial<Record<'name' | 'email' | 'company' | 'phone', string | null>> = {}): TypeformWebhook {\n const values = { name: 'Ada Lovelace', email: 'ada@example.com', company: 'Analytical Engines Ltd', phone: '+44 20 7946 0000', ...overrides }\n const answers = [\n values.name !== null && { type: 'text', field: { id: 'f1', ref: 'lead_name', type: 'short_text' }, text: values.name },\n values.email !== null && { type: 'email', field: { id: 'f2', ref: 'lead_email', type: 'email' }, email: values.email },\n values.company !== null && { type: 'text', field: { id: 'f3', ref: 'lead_company', type: 'short_text' }, text: values.company },\n values.phone !== null && { type: 'phone_number', field: { id: 'f4', ref: 'lead_phone', type: 'phone_number' }, phone_number: values.phone },\n ].filter(Boolean) as TypeformWebhook['form_response']['answers']\n return {\n event_id: 'evt_1',\n event_type: 'form_response',\n form_response: { form_id: 'frm_1', token: 'tok_1', submitted_at: '2026-10-01T09:00:00Z', answers },\n }\n}\n\nfunction config(fetch: typeof globalThis.fetch): ConnectorConfig {\n return {\n pipedriveApiToken: 'test-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n fetch,\n }\n}\n\ndescribe('handle', () => {\n it('creates organization, person and deal with the mapped fields', async () => {\n const fetch = fakeFetch({ '/organizations': ok(11), '/persons': ok(22), '/deals': ok(33) })\n const result = await handle(payload(), config(fetch))\n\n expect(result).toEqual({ personId: 22, organizationId: 11, dealId: 33 })\n const bodies = fetch.mock.calls.map(([url, init]) => [String(url), JSON.parse(String(init?.body))])\n expect(bodies).toEqual([\n ['https://api.pipedrive.com/v1/organizations', { name: 'Analytical Engines Ltd' }],\n ['https://api.pipedrive.com/v1/persons', {\n name: 'Ada Lovelace',\n email: [{ value: 'ada@example.com', primary: true, label: 'work' }],\n phone: [{ value: '+44 20 7946 0000', primary: true, label: 'work' }],\n org_id: 11,\n }],\n ['https://api.pipedrive.com/v1/deals', { title: 'Website lead: Analytical Engines Ltd', person_id: 22, org_id: 11 }],\n ])\n })\n\n it('authenticates every call with the x-api-token header', async () => {\n const fetch = fakeFetch({ '/organizations': ok(1), '/persons': ok(2), '/deals': ok(3) })\n await handle(payload(), config(fetch))\n for (const [, init] of fetch.mock.calls) {\n expect(new Headers(init?.headers).get('x-api-token')).toBe('test-token')\n expect(init?.method).toBe('POST')\n }\n })\n\n it('skips the organization when no company was answered', async () => {\n const fetch = fakeFetch({ '/persons': ok(2), '/deals': ok(3) })\n const result = await handle(payload({ company: null }), config(fetch))\n expect(result.organizationId).toBeNull()\n expect(fetch).toHaveBeenCalledTimes(2)\n const deal = JSON.parse(String(fetch.mock.calls[1]?.[1]?.body))\n expect(deal).toEqual({ title: 'Website lead: Ada Lovelace', person_id: 2 })\n })\n\n it('rejects a response without an email answer', async () => {\n const fetch = fakeFetch({})\n await expect(handle(payload({ email: null }), config(fetch))).rejects.toThrow(/lead_email/)\n expect(fetch).not.toHaveBeenCalled()\n })\n\n it('surfaces Pipedrive errors with status and endpoint', async () => {\n const fetch = fakeFetch({\n '/organizations': ok(1),\n '/persons': { status: 401, body: { success: false, error: 'unauthorized access' } },\n })\n await expect(handle(payload(), config(fetch))).rejects.toThrow('Pipedrive POST /persons failed with 401: unauthorized access')\n })\n\n it('ignores events that are not form responses', async () => {\n const fetch = fakeFetch({})\n const event = { ...payload(), event_type: 'form_deleted' } as unknown as TypeformWebhook\n await expect(handle(event, config(fetch))).rejects.toThrow(/Unsupported Typeform event/)\n })\n})\n\ndescribe('contract', () => {\n it('handles the documented Typeform payload against the documented Pipedrive responses', async () => {\n const { fetch, calls } = contractFetch()\n const result = await handle(SOURCE_EXAMPLE as unknown as TypeformWebhook, {\n pipedriveApiToken: 'contract-token',\n fieldRefs: { name: 'lead_name', email: 'lead_email' },\n fetch,\n })\n expect(result.personId).toBeGreaterThan(0)\n expect(result.dealId).toBeGreaterThan(0)\n expect(calls.map((c) => c.url)).toEqual(['https://api.pipedrive.com/v1/persons', 'https://api.pipedrive.com/v1/deals'])\n })\n})\n"
|
|
1055
|
+
},
|
|
1056
|
+
{
|
|
1057
|
+
path: "README.md",
|
|
1058
|
+
content: "# Typeform \u2192 Pipedrive connector\n\nTurns a Typeform `form_response` webhook into a Pipedrive organization, person and deal.\n\n## Configuration\n\n| Env var | Used as | Notes |\n| --- | --- | --- |\n| `PIPEDRIVE_API_TOKEN` | `pipedriveApiToken` | Personal API token, sent as the `x-api-token` header |\n\n`fieldRefs` maps your Typeform field refs to the lead fields. `name` and `email` are required; `company` and `phone` are optional.\n\n## Usage\n\n```ts\nimport { handle } from './src/connector'\n\nexport async function POST(request: Request) {\n const payload = await request.json()\n const result = await handle(payload, {\n pipedriveApiToken: process.env.PIPEDRIVE_API_TOKEN!,\n fieldRefs: { name: 'lead_name', email: 'lead_email', company: 'lead_company', phone: 'lead_phone' },\n dealTitlePrefix: 'Website lead',\n })\n return Response.json(result)\n}\n```\n\n## Behaviour\n\n1. Reads the answers by field ref, rejects the event when name or email is missing.\n2. Creates an organization when a company was answered (`POST /v1/organizations`).\n3. Creates the person with email and phone, linked to the organization (`POST /v1/persons`).\n4. Creates a deal for the person (`POST /v1/deals`).\n\nAny non-2xx or `success: false` response throws with the status code and endpoint, so the webhook caller can retry.\n\n## Assumptions\n\n- Typeform webhook signature verification happens before `handle` is called.\n- Every submission is a new lead; the connector does not search for existing persons.\n"
|
|
1059
|
+
},
|
|
1060
|
+
{
|
|
1061
|
+
path: "src/fixtures.ts",
|
|
1062
|
+
content: `// Written by patchbay from the API documentation. Regenerated on every attempt, do not edit.
|
|
1063
|
+
// Source: https://www.typeform.com/developers/webhooks/example-payload/
|
|
1064
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Organizations
|
|
1065
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Persons
|
|
1066
|
+
// Source: https://developers.pipedrive.com/docs/api/v1/Deals
|
|
1067
|
+
|
|
1068
|
+
/** Example payload of the source event, as documented. */
|
|
1069
|
+
export const SOURCE_EXAMPLE = {
|
|
1070
|
+
"event_id": "01H...",
|
|
1071
|
+
"event_type": "form_response",
|
|
1072
|
+
"form_response": {
|
|
1073
|
+
"form_id": "lT4Z3j",
|
|
1074
|
+
"token": "a3a12ec67a1365927098a606107fac15",
|
|
1075
|
+
"submitted_at": "2026-10-01T09:00:00Z",
|
|
1076
|
+
"answers": [
|
|
1077
|
+
{
|
|
1078
|
+
"type": "text",
|
|
1079
|
+
"field": {
|
|
1080
|
+
"id": "f1",
|
|
1081
|
+
"ref": "lead_name",
|
|
1082
|
+
"type": "short_text"
|
|
1083
|
+
},
|
|
1084
|
+
"text": "Ada Lovelace"
|
|
1085
|
+
},
|
|
1086
|
+
{
|
|
1087
|
+
"type": "email",
|
|
1088
|
+
"field": {
|
|
1089
|
+
"id": "f2",
|
|
1090
|
+
"ref": "lead_email",
|
|
1091
|
+
"type": "email"
|
|
1092
|
+
},
|
|
1093
|
+
"email": "ada@example.com"
|
|
1094
|
+
}
|
|
1095
|
+
]
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
export interface DocumentedEndpoint {
|
|
1100
|
+
method: string
|
|
1101
|
+
/** As documented; may contain placeholders such as {id}, :id, <id> or a region host like usX. */
|
|
1102
|
+
url: string
|
|
1103
|
+
response: unknown
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/** Target endpoints the connector may call, in the documented order, with their documented responses. */
|
|
1107
|
+
export const TARGET_ENDPOINTS: DocumentedEndpoint[] = [
|
|
1108
|
+
{
|
|
1109
|
+
"method": "POST",
|
|
1110
|
+
"url": "https://api.pipedrive.com/v1/organizations",
|
|
1111
|
+
"response": {
|
|
1112
|
+
"success": true,
|
|
1113
|
+
"data": {
|
|
1114
|
+
"id": 11
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
"method": "POST",
|
|
1120
|
+
"url": "https://api.pipedrive.com/v1/persons",
|
|
1121
|
+
"response": {
|
|
1122
|
+
"success": true,
|
|
1123
|
+
"data": {
|
|
1124
|
+
"id": 22
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
{
|
|
1129
|
+
"method": "POST",
|
|
1130
|
+
"url": "https://api.pipedrive.com/v1/deals",
|
|
1131
|
+
"response": {
|
|
1132
|
+
"success": true,
|
|
1133
|
+
"data": {
|
|
1134
|
+
"id": 33
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
]
|
|
1139
|
+
|
|
1140
|
+
export interface RecordedCall {
|
|
1141
|
+
method: string
|
|
1142
|
+
url: string
|
|
1143
|
+
/** The request body exactly as sent (usually a JSON string). */
|
|
1144
|
+
body: string | undefined
|
|
1145
|
+
/** The body parsed as JSON, or undefined when it is not JSON. */
|
|
1146
|
+
json: unknown
|
|
1147
|
+
headers: Headers
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/** The documented endpoint a request matches, placeholders and region subdomains included. */
|
|
1151
|
+
export function findEndpoint(method: string, url: string): DocumentedEndpoint | undefined {
|
|
1152
|
+
return TARGET_ENDPOINTS.find((endpoint) => endpoint.method === method.toUpperCase() && urlMatches(endpoint.url, url))
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* A fake fetch that answers every documented endpoint with its documented response and
|
|
1157
|
+
* rejects anything else. Use it in the contract test:
|
|
1158
|
+
*
|
|
1159
|
+
* const { fetch, calls } = contractFetch()
|
|
1160
|
+
* await handle(SOURCE_EXAMPLE as MyInput, { ...config, fetch })
|
|
1161
|
+
* expect(calls.length).toBeGreaterThan(0)
|
|
1162
|
+
*/
|
|
1163
|
+
export function contractFetch(): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
1164
|
+
return fakeFetch((request) => {
|
|
1165
|
+
const endpoint = findEndpoint(request.method, request.url)
|
|
1166
|
+
if (!endpoint) throw new Error(\`contract: \${request.method} \${request.url} is not a documented endpoint\`)
|
|
1167
|
+
return jsonResponse(endpoint.response)
|
|
1168
|
+
})
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* A correctly typed fake fetch for every other test. The handler receives the recorded
|
|
1173
|
+
* request: { method, url, body (string as sent), json (parsed body), headers (Headers) }.
|
|
1174
|
+
*
|
|
1175
|
+
* const { fetch, calls } = fakeFetch((req) => req.url.endsWith('/deals') ? jsonResponse({ id: 1 }, 201) : jsonResponse({}, 404))
|
|
1176
|
+
*/
|
|
1177
|
+
export function fakeFetch(
|
|
1178
|
+
handler: (request: RecordedCall) => Response | Promise<Response>,
|
|
1179
|
+
): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
1180
|
+
const calls: RecordedCall[] = []
|
|
1181
|
+
const fake = async (input: string | URL | Request, init: RequestInit = {}): Promise<Response> => {
|
|
1182
|
+
const url = input instanceof Request ? input.url : String(input)
|
|
1183
|
+
const method = (init.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase()
|
|
1184
|
+
const body = typeof init.body === 'string' ? init.body : init.body == null ? undefined : String(init.body)
|
|
1185
|
+
const call: RecordedCall = { method, url, body, json: parseJson(body), headers: new Headers(init.headers) }
|
|
1186
|
+
calls.push(call)
|
|
1187
|
+
return handler(call)
|
|
1188
|
+
}
|
|
1189
|
+
return { fetch: fake as typeof fetch, calls }
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
const STATUS_TEXT: Record<number, string> = {
|
|
1193
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',
|
|
1194
|
+
400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 409: 'Conflict',
|
|
1195
|
+
422: 'Unprocessable Entity', 429: 'Too Many Requests',
|
|
1196
|
+
500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/** A JSON Response with the standard status text, e.g. jsonResponse({ error: 'nope' }, 401) -> "401 Unauthorized". */
|
|
1200
|
+
export function jsonResponse(body: unknown, status = 200): Response {
|
|
1201
|
+
const init = { status, statusText: STATUS_TEXT[status] ?? '', headers: { 'Content-Type': 'application/json' } }
|
|
1202
|
+
return new Response(status === 204 ? null : JSON.stringify(body), init)
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
function parseJson(raw: string | undefined): unknown {
|
|
1206
|
+
if (raw === undefined) return undefined
|
|
1207
|
+
try {
|
|
1208
|
+
return JSON.parse(raw)
|
|
1209
|
+
} catch {
|
|
1210
|
+
return undefined
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
function urlMatches(template: string, actual: string): boolean {
|
|
1215
|
+
const t = splitUrl(template)
|
|
1216
|
+
const a = splitUrl(actual)
|
|
1217
|
+
if (!t || !a) return false
|
|
1218
|
+
const tHost = t.host.split('.')
|
|
1219
|
+
const aHost = a.host.split('.')
|
|
1220
|
+
if (tHost.slice(-2).join('.') !== aHost.slice(-2).join('.')) return false
|
|
1221
|
+
const tPath = t.path.replace(/\\/+$/, '').split('/')
|
|
1222
|
+
const aPath = a.path.replace(/\\/+$/, '').split('/')
|
|
1223
|
+
if (tPath.length !== aPath.length) return false
|
|
1224
|
+
return tPath.every((segment, i) => isPlaceholder(segment) || segment === aPath[i])
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
function splitUrl(url: string): { host: string; path: string } | null {
|
|
1228
|
+
const match = url.match(/^https?:\\/\\/([^/?#]+)([^?#]*)/i)
|
|
1229
|
+
return match ? { host: (match[1] ?? '').toLowerCase(), path: match[2] || '/' } : null
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
function isPlaceholder(segment: string): boolean {
|
|
1233
|
+
return /^\\{[^}]+\\}$|^:[A-Za-z_]\\w*$|^<[^>]+>$/.test(segment)
|
|
1234
|
+
}
|
|
1235
|
+
`
|
|
1236
|
+
}
|
|
1237
|
+
],
|
|
1238
|
+
report: {
|
|
1239
|
+
typecheckOk: true,
|
|
1240
|
+
typecheckOutput: "",
|
|
1241
|
+
passed: 7,
|
|
1242
|
+
failed: 0,
|
|
1243
|
+
total: 7,
|
|
1244
|
+
failures: [],
|
|
1245
|
+
durationMs: 202.9775390625,
|
|
1246
|
+
ranOn: "replay",
|
|
1247
|
+
passedNames: [
|
|
1248
|
+
"handle creates organization, person and deal with the mapped fields",
|
|
1249
|
+
"handle authenticates every call with the x-api-token header",
|
|
1250
|
+
"handle skips the organization when no company was answered",
|
|
1251
|
+
"handle rejects a response without an email answer",
|
|
1252
|
+
"handle surfaces Pipedrive errors with status and endpoint",
|
|
1253
|
+
"handle ignores events that are not form responses",
|
|
1254
|
+
"contract handles the documented Typeform payload against the documented Pipedrive responses"
|
|
1255
|
+
],
|
|
1256
|
+
contract: "passed"
|
|
1257
|
+
}
|
|
1258
|
+
},
|
|
1259
|
+
{
|
|
1260
|
+
type: "step",
|
|
1261
|
+
step: "package",
|
|
1262
|
+
status: "done"
|
|
1263
|
+
}
|
|
1264
|
+
]
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1267
|
+
// src/replay.ts
|
|
1268
|
+
var REPLAY_RUN = run_default;
|
|
1269
|
+
async function* replayRun(run2 = REPLAY_RUN, pace = 1) {
|
|
1270
|
+
yield { type: "mode", mode: "replay" };
|
|
1271
|
+
for (const event of run2.events) {
|
|
1272
|
+
if (event.type === "mode") continue;
|
|
1273
|
+
await sleep2(delayFor(event) * pace);
|
|
1274
|
+
yield event;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
function delayFor(event) {
|
|
1278
|
+
switch (event.type) {
|
|
1279
|
+
case "step":
|
|
1280
|
+
return event.status === "start" ? 250 : 700;
|
|
1281
|
+
case "files":
|
|
1282
|
+
return 1200;
|
|
1283
|
+
case "tests":
|
|
1284
|
+
return 1500;
|
|
1285
|
+
default:
|
|
1286
|
+
return 180;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
var sleep2 = (ms) => ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
|
|
1290
|
+
|
|
1291
|
+
// src/contract.ts
|
|
1292
|
+
var FIXTURES_PATH = "src/fixtures.ts";
|
|
1293
|
+
function renderFixtures(spec) {
|
|
1294
|
+
const endpoints = spec.target.map((target) => ({
|
|
1295
|
+
method: target.method.toUpperCase(),
|
|
1296
|
+
url: target.url,
|
|
1297
|
+
response: target.responseExample ?? {}
|
|
1298
|
+
}));
|
|
1299
|
+
const docs = [spec.source.docsUrl, ...spec.target.map((t) => t.docsUrl)].filter(Boolean);
|
|
1300
|
+
const content = `// Written by patchbay from the API documentation. Regenerated on every attempt, do not edit.
|
|
1301
|
+
${[...new Set(docs)].map((url) => `// Source: ${url}`).join("\n")}
|
|
1302
|
+
|
|
1303
|
+
/** Example payload of the source event, as documented. */
|
|
1304
|
+
export const SOURCE_EXAMPLE = ${JSON.stringify(spec.source.example ?? {}, null, 2)}
|
|
1305
|
+
|
|
1306
|
+
export interface DocumentedEndpoint {
|
|
1307
|
+
method: string
|
|
1308
|
+
/** As documented; may contain placeholders such as {id}, :id, <id> or a region host like usX. */
|
|
1309
|
+
url: string
|
|
1310
|
+
response: unknown
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
/** Target endpoints the connector may call, in the documented order, with their documented responses. */
|
|
1314
|
+
export const TARGET_ENDPOINTS: DocumentedEndpoint[] = ${JSON.stringify(endpoints, null, 2)}
|
|
1315
|
+
|
|
1316
|
+
export interface RecordedCall {
|
|
1317
|
+
method: string
|
|
1318
|
+
url: string
|
|
1319
|
+
/** The request body exactly as sent (usually a JSON string). */
|
|
1320
|
+
body: string | undefined
|
|
1321
|
+
/** The body parsed as JSON, or undefined when it is not JSON. */
|
|
1322
|
+
json: unknown
|
|
1323
|
+
headers: Headers
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
/** The documented endpoint a request matches, placeholders and region subdomains included. */
|
|
1327
|
+
export function findEndpoint(method: string, url: string): DocumentedEndpoint | undefined {
|
|
1328
|
+
return TARGET_ENDPOINTS.find((endpoint) => endpoint.method === method.toUpperCase() && urlMatches(endpoint.url, url))
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* A fake fetch that answers every documented endpoint with its documented response and
|
|
1333
|
+
* rejects anything else. Use it in the contract test:
|
|
1334
|
+
*
|
|
1335
|
+
* const { fetch, calls } = contractFetch()
|
|
1336
|
+
* await handle(SOURCE_EXAMPLE as MyInput, { ...config, fetch })
|
|
1337
|
+
* expect(calls.length).toBeGreaterThan(0)
|
|
1338
|
+
*/
|
|
1339
|
+
export function contractFetch(): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
1340
|
+
return fakeFetch((request) => {
|
|
1341
|
+
const endpoint = findEndpoint(request.method, request.url)
|
|
1342
|
+
if (!endpoint) throw new Error(\`contract: \${request.method} \${request.url} is not a documented endpoint\`)
|
|
1343
|
+
return jsonResponse(endpoint.response)
|
|
1344
|
+
})
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* A correctly typed fake fetch for every other test. The handler receives the recorded
|
|
1349
|
+
* request: { method, url, body (string as sent), json (parsed body), headers (Headers) }.
|
|
1350
|
+
*
|
|
1351
|
+
* const { fetch, calls } = fakeFetch((req) => req.url.endsWith('/deals') ? jsonResponse({ id: 1 }, 201) : jsonResponse({}, 404))
|
|
1352
|
+
*/
|
|
1353
|
+
export function fakeFetch(
|
|
1354
|
+
handler: (request: RecordedCall) => Response | Promise<Response>,
|
|
1355
|
+
): { fetch: typeof fetch; calls: RecordedCall[] } {
|
|
1356
|
+
const calls: RecordedCall[] = []
|
|
1357
|
+
const fake = async (input: string | URL | Request, init: RequestInit = {}): Promise<Response> => {
|
|
1358
|
+
const url = input instanceof Request ? input.url : String(input)
|
|
1359
|
+
const method = (init.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase()
|
|
1360
|
+
const body = typeof init.body === 'string' ? init.body : init.body == null ? undefined : String(init.body)
|
|
1361
|
+
const call: RecordedCall = { method, url, body, json: parseJson(body), headers: new Headers(init.headers) }
|
|
1362
|
+
calls.push(call)
|
|
1363
|
+
return handler(call)
|
|
1364
|
+
}
|
|
1365
|
+
return { fetch: fake as typeof fetch, calls }
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
const STATUS_TEXT: Record<number, string> = {
|
|
1369
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',
|
|
1370
|
+
400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 409: 'Conflict',
|
|
1371
|
+
422: 'Unprocessable Entity', 429: 'Too Many Requests',
|
|
1372
|
+
500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
/** A JSON Response with the standard status text, e.g. jsonResponse({ error: 'nope' }, 401) -> "401 Unauthorized". */
|
|
1376
|
+
export function jsonResponse(body: unknown, status = 200): Response {
|
|
1377
|
+
const init = { status, statusText: STATUS_TEXT[status] ?? '', headers: { 'Content-Type': 'application/json' } }
|
|
1378
|
+
return new Response(status === 204 ? null : JSON.stringify(body), init)
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
function parseJson(raw: string | undefined): unknown {
|
|
1382
|
+
if (raw === undefined) return undefined
|
|
1383
|
+
try {
|
|
1384
|
+
return JSON.parse(raw)
|
|
1385
|
+
} catch {
|
|
1386
|
+
return undefined
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
function urlMatches(template: string, actual: string): boolean {
|
|
1391
|
+
const t = splitUrl(template)
|
|
1392
|
+
const a = splitUrl(actual)
|
|
1393
|
+
if (!t || !a) return false
|
|
1394
|
+
const tHost = t.host.split('.')
|
|
1395
|
+
const aHost = a.host.split('.')
|
|
1396
|
+
if (tHost.slice(-2).join('.') !== aHost.slice(-2).join('.')) return false
|
|
1397
|
+
const tPath = t.path.replace(/\\/+$/, '').split('/')
|
|
1398
|
+
const aPath = a.path.replace(/\\/+$/, '').split('/')
|
|
1399
|
+
if (tPath.length !== aPath.length) return false
|
|
1400
|
+
return tPath.every((segment, i) => isPlaceholder(segment) || segment === aPath[i])
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
function splitUrl(url: string): { host: string; path: string } | null {
|
|
1404
|
+
const match = url.match(/^https?:\\/\\/([^/?#]+)([^?#]*)/i)
|
|
1405
|
+
return match ? { host: (match[1] ?? '').toLowerCase(), path: match[2] || '/' } : null
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
function isPlaceholder(segment: string): boolean {
|
|
1409
|
+
return /^\\{[^}]+\\}$|^:[A-Za-z_]\\w*$|^<[^>]+>$/.test(segment)
|
|
1410
|
+
}
|
|
1411
|
+
`;
|
|
1412
|
+
return { path: FIXTURES_PATH, content };
|
|
1413
|
+
}
|
|
1414
|
+
function contractMissing(files) {
|
|
1415
|
+
const tests = files.find((f) => f.path === "src/connector.test.ts")?.content ?? "";
|
|
1416
|
+
if (!/from\s+['"]\.\/fixtures['"]/.test(tests)) {
|
|
1417
|
+
return "src/connector.test.ts does not import SOURCE_EXAMPLE and contractFetch from ./fixtures";
|
|
1418
|
+
}
|
|
1419
|
+
if (!/describe\(\s*['"`]contract/.test(tests)) {
|
|
1420
|
+
return 'src/connector.test.ts has no describe("contract", ...) block running handle on SOURCE_EXAMPLE';
|
|
1421
|
+
}
|
|
1422
|
+
if (!tests.includes("SOURCE_EXAMPLE")) {
|
|
1423
|
+
return "the contract tests never pass SOURCE_EXAMPLE to handle";
|
|
1424
|
+
}
|
|
1425
|
+
return null;
|
|
1426
|
+
}
|
|
1427
|
+
function withContract(report, files) {
|
|
1428
|
+
const missing = contractMissing(files);
|
|
1429
|
+
if (missing) {
|
|
1430
|
+
return {
|
|
1431
|
+
...report,
|
|
1432
|
+
contract: "missing",
|
|
1433
|
+
failed: report.failed + 1,
|
|
1434
|
+
total: report.total + 1,
|
|
1435
|
+
failures: [...report.failures, { name: "contract (static check)", message: missing }]
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
const failed = report.failures.some((f) => /\bcontract\b/i.test(f.name));
|
|
1439
|
+
if (failed) return { ...report, contract: "failed" };
|
|
1440
|
+
const ran = (report.passedNames ?? []).some((name) => /\bcontract\b/i.test(name));
|
|
1441
|
+
if (!ran) {
|
|
1442
|
+
return {
|
|
1443
|
+
...report,
|
|
1444
|
+
contract: "failed",
|
|
1445
|
+
failed: report.failed + 1,
|
|
1446
|
+
total: report.total + 1,
|
|
1447
|
+
failures: [
|
|
1448
|
+
...report.failures,
|
|
1449
|
+
{ name: "contract (did not run)", message: "The contract test never passed; the test file probably failed to load." }
|
|
1450
|
+
]
|
|
1451
|
+
};
|
|
1452
|
+
}
|
|
1453
|
+
return { ...report, contract: "passed" };
|
|
1454
|
+
}
|
|
1455
|
+
function withFixtures(files, fixtures) {
|
|
1456
|
+
return [...files.filter((f) => f.path !== FIXTURES_PATH), fixtures];
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
// src/syntax-check.ts
|
|
1460
|
+
import { transform } from "esbuild";
|
|
1461
|
+
async function preflightSyntax(files) {
|
|
1462
|
+
const tsFiles = files.filter((f) => f.path.endsWith(".ts"));
|
|
1463
|
+
const failures = [];
|
|
1464
|
+
await Promise.all(
|
|
1465
|
+
tsFiles.map(async (f) => {
|
|
1466
|
+
try {
|
|
1467
|
+
await transform(f.content, {
|
|
1468
|
+
loader: "ts",
|
|
1469
|
+
sourcefile: f.path,
|
|
1470
|
+
sourcemap: false,
|
|
1471
|
+
target: "es2022"
|
|
1472
|
+
});
|
|
1473
|
+
} catch (error) {
|
|
1474
|
+
const errs = error.errors ?? [];
|
|
1475
|
+
for (const e of errs) {
|
|
1476
|
+
failures.push({
|
|
1477
|
+
file: f.path,
|
|
1478
|
+
line: e.location?.line,
|
|
1479
|
+
message: e.text ? `${e.text}${e.location ? ` at ${f.path}:${e.location.line}:${e.location.column ?? 0}` : ""}` : String(error)
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
if (!errs.length) failures.push({ file: f.path, message: error instanceof Error ? error.message : String(error) });
|
|
1483
|
+
}
|
|
1484
|
+
})
|
|
1485
|
+
);
|
|
1486
|
+
if (failures.length === 0) return null;
|
|
1487
|
+
return {
|
|
1488
|
+
typecheckOk: false,
|
|
1489
|
+
typecheckOutput: failures.map((f) => `${f.file}: ${f.message}`).join("\n"),
|
|
1490
|
+
passed: 0,
|
|
1491
|
+
failed: failures.length,
|
|
1492
|
+
total: failures.length,
|
|
1493
|
+
failures: failures.map((f) => ({
|
|
1494
|
+
name: `syntax (${f.file}${f.line ? `:${f.line}` : ""})`,
|
|
1495
|
+
message: f.message
|
|
1496
|
+
})),
|
|
1497
|
+
durationMs: 0,
|
|
1498
|
+
ranOn: "preflight:esbuild",
|
|
1499
|
+
passedNames: []
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
// src/prompts.ts
|
|
1504
|
+
var PLAN_SYSTEM = `You are the planning step of patchbay, an agent that builds API integrations.
|
|
1505
|
+
Turn the user's request into a precise integration plan. Answer with a single JSON object and nothing else:
|
|
1506
|
+
{
|
|
1507
|
+
"summary": string, // one sentence describing this specific integration
|
|
1508
|
+
"source": { "app": string, "trigger": string, "docsQuery": string },
|
|
1509
|
+
"target": { "app": string, "action": string, "docsQuery": string },
|
|
1510
|
+
"additionalTargets"?: [ { "app": string, "action": string, "docsQuery": string } ],
|
|
1511
|
+
"fieldMapping": [ { "from": string, "to": string, "note"?: string } ],
|
|
1512
|
+
"assumptions": [ string ] // concrete assumptions you made about this request
|
|
1513
|
+
}
|
|
1514
|
+
The source is where data comes from (usually a webhook payload), the "target" is the PRIMARY API the connector calls.
|
|
1515
|
+
Use "additionalTargets" when the request calls two or more distinct external services in sequence \u2014 for example
|
|
1516
|
+
"refresh an OAuth token at Google then write to Google Sheets" (additionalTargets: Google OAuth), or
|
|
1517
|
+
"open a GitHub issue then post a comment back on the Zendesk ticket" (additionalTargets: Zendesk comment),
|
|
1518
|
+
or "find a HubSpot engagement, delete it, log to Google Sheets" (additionalTargets: Google Sheets). List them
|
|
1519
|
+
in the order they are called. Do NOT split one app into multiple entries \u2014 a single app with several endpoints
|
|
1520
|
+
stays under its primary target or additionalTarget entry and the endpoints come out of research.
|
|
1521
|
+
docsQuery values are web search queries that find the official API reference for exactly that endpoint.
|
|
1522
|
+
List every field the user's request implies in fieldMapping, using the real field names of both APIs.
|
|
1523
|
+
|
|
1524
|
+
Example of the shape (for a different request, do not copy its content):
|
|
1525
|
+
{"summary":"When a Shopify order is paid, append a row with the order number, customer email and total to an Airtable table.",
|
|
1526
|
+
"source":{"app":"Shopify","trigger":"orders/paid webhook","docsQuery":"Shopify orders/paid webhook payload order fields"},
|
|
1527
|
+
"target":{"app":"Airtable","action":"create record","docsQuery":"Airtable Web API create records request body personal access token"},
|
|
1528
|
+
"fieldMapping":[{"from":"name","to":"fields['Order']"},{"from":"email","to":"fields['Email']"},{"from":"total_price","to":"fields['Total']","note":"string in the payload, number in Airtable"}],
|
|
1529
|
+
"assumptions":["The Airtable table already has the columns Order, Email and Total."]}
|
|
1530
|
+
|
|
1531
|
+
Chained-flow example (two distinct target services):
|
|
1532
|
+
{"summary":"When a Zendesk ticket is created, open a GitHub issue and post the issue URL back as a Zendesk ticket comment.",
|
|
1533
|
+
"source":{"app":"Zendesk","trigger":"ticket.created webhook","docsQuery":"Zendesk ticket webhook payload fields"},
|
|
1534
|
+
"target":{"app":"GitHub","action":"create issue","docsQuery":"GitHub REST API create an issue repo owner title body"},
|
|
1535
|
+
"additionalTargets":[{"app":"Zendesk","action":"add ticket comment","docsQuery":"Zendesk API add comment to ticket request body"}],
|
|
1536
|
+
"fieldMapping":[{"from":"ticket.subject","to":"issue.title"},{"from":"ticket.description","to":"issue.body"},{"from":"issue.html_url","to":"comment.body","note":"after the GitHub issue is created, write its URL into a Zendesk ticket comment"}],
|
|
1537
|
+
"assumptions":["The Zendesk account has a webhook configured for ticket.created."]}`;
|
|
1538
|
+
var SPEC_SYSTEM = `You are the research step of patchbay. You receive a plan and raw text of API documentation pages.
|
|
1539
|
+
Extract the concrete endpoint facts the connector needs. Only state what the docs support; if something is not in the docs, add a note instead of guessing.
|
|
1540
|
+
When plan.additionalTargets is set, the connector talks to MORE THAN ONE target service in sequence \u2014
|
|
1541
|
+
list every call in spec.target[] in call order (primary target first, then additionalTargets in the order
|
|
1542
|
+
given), each with its own "app" field. The contract test runs the connector against the SOURCE_EXAMPLE and
|
|
1543
|
+
every documented endpoint must either be called or clearly optional, so do not include an endpoint the
|
|
1544
|
+
connector will not actually hit.
|
|
1545
|
+
Answer with a single JSON object:
|
|
1546
|
+
{
|
|
1547
|
+
"source": {
|
|
1548
|
+
"app": "...", "role": "source", "method": "POST", "url": "(where the webhook is delivered, or the polling URL)",
|
|
1549
|
+
"auth": { "type": "none|header|bearer|query|basic", "name": "header or query name if any", "envVar": "SCREAMING_SNAKE env var name" },
|
|
1550
|
+
"example": { "a realistic example payload with the fields the plan maps" },
|
|
1551
|
+
"docsUrl": "https://...", "notes": ["pitfalls, required fields, formats"]
|
|
1552
|
+
},
|
|
1553
|
+
"target": [
|
|
1554
|
+
{ "app": "...", "role": "target", "method": "POST", "url": "https://full endpoint url",
|
|
1555
|
+
"auth": { "type": "...", "name": "...", "envVar": "..." },
|
|
1556
|
+
"example": { "minimal valid request body" }, "responseExample": { "the part of the response the connector needs" },
|
|
1557
|
+
"docsUrl": "https://...", "notes": ["..."] }
|
|
1558
|
+
]
|
|
1559
|
+
}
|
|
1560
|
+
List target endpoints in the order the connector must call them.
|
|
1561
|
+
|
|
1562
|
+
When any endpoint is documented as paginated (cursor, offset, page, has_more, next_url,
|
|
1563
|
+
next_page_token, "endCursor"), capture the pagination mechanism in notes[] on that endpoint
|
|
1564
|
+
so the connector follows it correctly, e.g. "paginate with ?after=cursor; stop when
|
|
1565
|
+
response.pagination.next is null".
|
|
1566
|
+
|
|
1567
|
+
URL path templates: when the docs describe the endpoint with placeholders \u2014 numeric ids,
|
|
1568
|
+
UUIDs, slugs, external-id upserts, composite keys \u2014 write the full documented template as
|
|
1569
|
+
the "url" value, keeping the placeholder syntax from the docs. Examples:
|
|
1570
|
+
|
|
1571
|
+
"https://api.hubspot.com/crm/v3/objects/contacts/{contactId}"
|
|
1572
|
+
"https://na1.salesforce.com/services/data/v60.0/sobjects/Account/Website__c/{website}"
|
|
1573
|
+
"https://api.notion.com/v1/pages/{page_id}"
|
|
1574
|
+
"https://api.linear.app/graphql" (single POST, variables carry the id)
|
|
1575
|
+
|
|
1576
|
+
Do NOT substitute a sample value into the url (so no acme.com in the Salesforce example,
|
|
1577
|
+
no abc123 in the HubSpot example). The connector constructs the final URL from the input
|
|
1578
|
+
payload at runtime; the spec URL stays the template so the contract check accepts both
|
|
1579
|
+
the documented endpoint and the generated call.
|
|
1580
|
+
|
|
1581
|
+
For upsert / idempotent-create endpoints (Salesforce external-id upsert via PATCH,
|
|
1582
|
+
Stripe Idempotency-Key, HubSpot updateOrCreate), add a note describing which header or
|
|
1583
|
+
URL segment carries the idempotency token so the connector sends it correctly.`;
|
|
1584
|
+
var GENERATE_SYSTEM = `You are the code-generation step of patchbay. Write a production-quality TypeScript connector and its tests.
|
|
1585
|
+
|
|
1586
|
+
Hard rules:
|
|
1587
|
+
- Exactly three files: src/connector.ts, src/connector.test.ts, README.md. Output each as
|
|
1588
|
+
<file path="src/connector.ts">
|
|
1589
|
+
...code...
|
|
1590
|
+
</file>
|
|
1591
|
+
with no markdown fences and no prose outside the file blocks.
|
|
1592
|
+
- No runtime dependencies. Use the global fetch, but ALWAYS accept an injected \`fetch\` in the config so tests can pass a fake.
|
|
1593
|
+
- Secrets come from the config object, never hard-coded. Document the env var names from the spec in the README.
|
|
1594
|
+
- src/connector.ts exports: a \`ConnectorConfig\` interface, a typed input type for the source payload, and one async
|
|
1595
|
+
entry function named \`handle\` that takes (payload, config) and returns a typed result with the created ids.
|
|
1596
|
+
- Validate the input: throw a descriptive Error when a required field is missing.
|
|
1597
|
+
- Non-2xx responses from the target must throw an Error that includes the status code and the endpoint.
|
|
1598
|
+
- Follow the spec exactly for URLs, HTTP methods, auth header/query names and body shapes.
|
|
1599
|
+
- src/connector.test.ts uses vitest (\`import { describe, it, expect, vi } from 'vitest'\`) and a fake fetch that records calls.
|
|
1600
|
+
Cover at least: happy path with the exact request bodies, the auth mechanism, the field mapping, a missing required field,
|
|
1601
|
+
and a non-2xx target response. Tests must be hermetic: no network, no timers, no real env vars.
|
|
1602
|
+
- src/fixtures.ts is provided by patchbay, read-only, do not output it. It exports SOURCE_EXAMPLE (the documented
|
|
1603
|
+
source payload), TARGET_ENDPOINTS and contractFetch(), a fake fetch that answers documented endpoints (URL
|
|
1604
|
+
placeholders like {id} or region hosts are handled) and throws on anything else.
|
|
1605
|
+
src/connector.test.ts MUST contain this block, adapted to your types and config. Put it at
|
|
1606
|
+
the TOP LEVEL of the test file \u2014 not nested inside another describe \u2014 and keep the title
|
|
1607
|
+
exactly 'contract' so patchbay can detect it:
|
|
1608
|
+
describe('contract', () => {
|
|
1609
|
+
it('handles the documented example payload', async () => {
|
|
1610
|
+
const { fetch, calls } = contractFetch()
|
|
1611
|
+
const result = await handle(SOURCE_EXAMPLE as unknown as <your input type>, { <a valid config object>, fetch })
|
|
1612
|
+
expect(calls.length).toBeGreaterThan(0)
|
|
1613
|
+
})
|
|
1614
|
+
})
|
|
1615
|
+
Configure the connector in that test so the documented example is valid input. If the example lacks a field the
|
|
1616
|
+
connector needs, make the connector handle that case (skip, fallback or clear error) rather than editing the example.
|
|
1617
|
+
- Do NOT reject SOURCE_EXAMPLE based on wall-clock time. If the connector guards against
|
|
1618
|
+
stale webhooks (timestamp age, signature expiry, nonce window), either freeze time in the
|
|
1619
|
+
contract test with vi.useFakeTimers() + vi.setSystemTime(new Date(<a date compatible with
|
|
1620
|
+
SOURCE_EXAMPLE's timestamps>)), or make the guard window large enough that the documented
|
|
1621
|
+
example always validates. The contract test must never fail with "timestamp too old".
|
|
1622
|
+
- RETRY SEMANTICS. "Retry up to N times" means at most N retry attempts after the initial
|
|
1623
|
+
request, so total HTTP calls are at most N+1. On a 429 (or other retryable status), compute
|
|
1624
|
+
the next delay as delay = Math.max(parseRetryAfter(res), baseMs * 2 ** retryIndex) where
|
|
1625
|
+
retryIndex starts at 0 for the first retry. Retry-After may be an integer in seconds OR an
|
|
1626
|
+
HTTP-date string \u2014 handle both and fall back to pure exponential backoff when the header is
|
|
1627
|
+
absent, malformed, or parses as NaN. Never call setTimeout(NaN). Tests will assert the mock
|
|
1628
|
+
fetch is called exactly (successfulAttempt + 1) times on success and exactly (N + 1) times
|
|
1629
|
+
on exhaustion; the loop must match this count exactly.
|
|
1630
|
+
- CURSOR PAGINATION. Terminate pagination loops on the API's explicit end signal \u2014 for
|
|
1631
|
+
GraphQL (Linear, Shopify GraphQL, GitHub v4) check pageInfo.hasNextPage === false, for REST
|
|
1632
|
+
check the absence or null of the cursor field (next_cursor, after, starting_after, page_info).
|
|
1633
|
+
Never loop while "cursor is truthy" alone \u2014 many APIs return an empty-string or placeholder
|
|
1634
|
+
cursor on the final page. Pass the cursor under the exact field name the vendor documents
|
|
1635
|
+
(after for Linear, starting_after for Intercom and Stripe, page_info for Shopify REST).
|
|
1636
|
+
Hard-cap every pagination loop at 50 iterations as a safety net against runaway loops.
|
|
1637
|
+
- For every other test use fakeFetch and jsonResponse from './fixtures' instead of writing your own fetch mock:
|
|
1638
|
+
const { fetch, calls } = fakeFetch((req) => req.url.endsWith('/deals') ? jsonResponse({ id: 2 }, 201) : jsonResponse({ error: 'x' }, 500))
|
|
1639
|
+
Each recorded call is { method, url, body (the string as sent), json (the parsed body), headers (a Headers object,
|
|
1640
|
+
use headers.get('authorization')) }. Assert on calls[i].json, do not JSON.parse it again. jsonResponse(body, status)
|
|
1641
|
+
sets the standard status text (401 -> 'Unauthorized'). Never declare a fetch type yourself.
|
|
1642
|
+
- TypeScript strict mode must pass. Import from './connector' and './fixtures' without an extension.
|
|
1643
|
+
- README.md: what it does, config/env vars, a usage example, and the assumptions from the plan.`;
|
|
1644
|
+
function planUser(prompt) {
|
|
1645
|
+
return `Request: ${prompt}`;
|
|
1646
|
+
}
|
|
1647
|
+
function specUser(plan, docs) {
|
|
1648
|
+
const pages = docs.map((d) => `=== ${d.url} ===
|
|
1649
|
+
${d.content.slice(0, 14e3)}`).join("\n\n");
|
|
1650
|
+
return `Plan:
|
|
1651
|
+
${JSON.stringify(plan, null, 2)}
|
|
1652
|
+
|
|
1653
|
+
Documentation pages:
|
|
1654
|
+
${pages}`;
|
|
1655
|
+
}
|
|
1656
|
+
function generateUser(plan, spec, fixtures) {
|
|
1657
|
+
return `Plan:
|
|
1658
|
+
${JSON.stringify(plan, null, 2)}
|
|
1659
|
+
|
|
1660
|
+
API spec:
|
|
1661
|
+
${JSON.stringify(spec, null, 2)}
|
|
1662
|
+
|
|
1663
|
+
src/fixtures.ts (provided by patchbay, read-only):
|
|
1664
|
+
${fixtures}
|
|
1665
|
+
|
|
1666
|
+
Write the three files now.`;
|
|
1667
|
+
}
|
|
1668
|
+
function repairUser(filesBlock, report, context) {
|
|
1669
|
+
const tsc = report.typecheckOk ? "Typecheck passed." : `Typecheck failed:
|
|
1670
|
+
${report.typecheckOutput.slice(0, 4e3)}`;
|
|
1671
|
+
const failures = report.failures.map((f) => `- ${f.name}
|
|
1672
|
+
${f.message}`).join("\n\n");
|
|
1673
|
+
const header = context ? `This is repair attempt ${context.attempt} of ${context.maxAttempts}. Produce something materially different from the earlier attempts${context.previousAttempts?.length ? " listed below" : ""}.` : "";
|
|
1674
|
+
const history = context?.previousAttempts?.length ? `
|
|
1675
|
+
Previous attempts:
|
|
1676
|
+
${context.previousAttempts.map((a) => ` attempt ${a.attempt}: ${a.summary}`).join("\n")}
|
|
1677
|
+
` : "";
|
|
1678
|
+
return `These files were run in an isolated sandbox (tsc --noEmit, then vitest).
|
|
1679
|
+
${header ? `
|
|
1680
|
+
${header}
|
|
1681
|
+
` : ""}
|
|
1682
|
+
${tsc}
|
|
1683
|
+
|
|
1684
|
+
Tests: ${report.passed} passed, ${report.failed} failed of ${report.total}.
|
|
1685
|
+
${failures ? `Failures:
|
|
1686
|
+
${failures}` : ""}
|
|
1687
|
+
${history}
|
|
1688
|
+
Current files:
|
|
1689
|
+
${filesBlock}
|
|
1690
|
+
|
|
1691
|
+
Fix the code so everything passes. Fix the connector when it contradicts the spec; fix a test only when the test itself is wrong.
|
|
1692
|
+
Return all three files again in full, using the same <file path="..."> format.`;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
// src/sources.ts
|
|
1696
|
+
var THIRD_PARTY = [
|
|
1697
|
+
"medium.com",
|
|
1698
|
+
"dev.to",
|
|
1699
|
+
"stackoverflow.com",
|
|
1700
|
+
"reddit.com",
|
|
1701
|
+
"youtube.com",
|
|
1702
|
+
"github.com",
|
|
1703
|
+
"endgrate.com",
|
|
1704
|
+
"cotera.co",
|
|
1705
|
+
"zapier.com",
|
|
1706
|
+
"make.com",
|
|
1707
|
+
"n8n.io",
|
|
1708
|
+
"pipedream.com",
|
|
1709
|
+
"rollout.com",
|
|
1710
|
+
"apidog.com",
|
|
1711
|
+
"postman.com"
|
|
1712
|
+
];
|
|
1713
|
+
var DOC_PATH = /\/(docs?|developers?|reference|api)(\/|$|-)/i;
|
|
1714
|
+
function rankOfficial(hits, app) {
|
|
1715
|
+
const slug = app.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
1716
|
+
const score = (hit) => {
|
|
1717
|
+
let host = "";
|
|
1718
|
+
let path = "";
|
|
1719
|
+
try {
|
|
1720
|
+
const url = new URL(hit.url);
|
|
1721
|
+
host = url.hostname.toLowerCase();
|
|
1722
|
+
path = url.pathname;
|
|
1723
|
+
} catch {
|
|
1724
|
+
return hit.score - 1;
|
|
1725
|
+
}
|
|
1726
|
+
const official = slug.length > 2 && host.replace(/[^a-z0-9.]/g, "").replace(/\./g, "").includes(slug);
|
|
1727
|
+
const thirdParty = THIRD_PARTY.some((domain) => host === domain || host.endsWith(`.${domain}`));
|
|
1728
|
+
return hit.score + (official ? 1 : 0) + (official && DOC_PATH.test(path) ? 0.5 : 0) - (thirdParty ? 1 : 0);
|
|
1729
|
+
};
|
|
1730
|
+
return [...hits].sort((a, b) => score(b) - score(a));
|
|
1731
|
+
}
|
|
1732
|
+
function isOfficial(url, app) {
|
|
1733
|
+
const slug = app.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
1734
|
+
try {
|
|
1735
|
+
return new URL(url).hostname.toLowerCase().replace(/[^a-z0-9]/g, "").includes(slug);
|
|
1736
|
+
} catch {
|
|
1737
|
+
return false;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
// src/run.ts
|
|
1742
|
+
var REQUIRED_FILES = ["src/connector.ts", "src/connector.test.ts", "README.md"];
|
|
1743
|
+
async function* runAgent(prompt, deps) {
|
|
1744
|
+
const { config, llm, search, sandbox } = deps;
|
|
1745
|
+
const queue = [];
|
|
1746
|
+
const log = (step) => (message) => queue.push({ type: "log", step, message });
|
|
1747
|
+
const drain = function* () {
|
|
1748
|
+
while (queue.length) yield queue.shift();
|
|
1749
|
+
};
|
|
1750
|
+
yield { type: "mode", mode: "live" };
|
|
1751
|
+
let current = "plan";
|
|
1752
|
+
try {
|
|
1753
|
+
yield { type: "step", step: "plan", status: "start" };
|
|
1754
|
+
const plan = await makePlan(prompt, llm, config);
|
|
1755
|
+
yield { type: "plan", plan };
|
|
1756
|
+
yield { type: "step", step: "plan", status: "done", detail: plan.summary };
|
|
1757
|
+
current = "research";
|
|
1758
|
+
yield { type: "step", step: "research", status: "start" };
|
|
1759
|
+
const prepared = sandbox.prepare(log("verify")).catch((error) => error);
|
|
1760
|
+
const { spec, sources } = await research(plan, search, llm, config, log("research"));
|
|
1761
|
+
yield* drain();
|
|
1762
|
+
yield { type: "sources", sources };
|
|
1763
|
+
yield { type: "spec", spec };
|
|
1764
|
+
yield { type: "step", step: "research", status: "done", detail: `${spec.target.length + 1} endpoints from ${sources.length} pages` };
|
|
1765
|
+
current = "generate";
|
|
1766
|
+
yield { type: "step", step: "generate", status: "start" };
|
|
1767
|
+
const fixtures = renderFixtures(spec);
|
|
1768
|
+
let files = withFixtures(await generate(plan, spec, fixtures.content, llm, config), fixtures);
|
|
1769
|
+
yield { type: "files", attempt: 1, files };
|
|
1770
|
+
yield { type: "step", step: "generate", status: "done", detail: files.map((f) => f.path).join(", ") };
|
|
1771
|
+
current = "verify";
|
|
1772
|
+
yield { type: "step", step: "verify", status: "start" };
|
|
1773
|
+
const baseImage = await prepared;
|
|
1774
|
+
yield* drain();
|
|
1775
|
+
if (baseImage instanceof Error) throw baseImage;
|
|
1776
|
+
let report = null;
|
|
1777
|
+
let bestFiles = files;
|
|
1778
|
+
let bestReport = null;
|
|
1779
|
+
let attempt = 1;
|
|
1780
|
+
let skipInitialTest = false;
|
|
1781
|
+
const history = [];
|
|
1782
|
+
for (; ; ) {
|
|
1783
|
+
if (!skipInitialTest) {
|
|
1784
|
+
const preflight = await preflightSyntax(files);
|
|
1785
|
+
if (preflight) {
|
|
1786
|
+
report = withContract(preflight, files);
|
|
1787
|
+
queue.push({ type: "log", step: "verify", message: `Preflight parse failed (${preflight.failed} file${preflight.failed > 1 ? "s" : ""}), skipping sandbox` });
|
|
1788
|
+
} else {
|
|
1789
|
+
report = withContract(await sandbox.runTests(baseImage, files, log("verify")), files);
|
|
1790
|
+
}
|
|
1791
|
+
yield* drain();
|
|
1792
|
+
yield { type: "tests", attempt, report };
|
|
1793
|
+
}
|
|
1794
|
+
skipInitialTest = false;
|
|
1795
|
+
({ files, report, bestFiles, bestReport } = keepBest({ attempt, files, report, bestFiles, bestReport, log: (m) => queue.push({ type: "log", step: "verify", message: m }) }));
|
|
1796
|
+
yield* drain();
|
|
1797
|
+
if (isGreen(report) || attempt >= config.maxAttempts) break;
|
|
1798
|
+
history.push({ attempt, summary: summariseReport(report) });
|
|
1799
|
+
attempt++;
|
|
1800
|
+
yield { type: "log", step: "verify", message: `Attempt ${attempt - 1} failed (${report.failed} tests, typecheck ${report.typecheckOk ? "ok" : "failed"}), repairing` };
|
|
1801
|
+
const repairCtx = { attempt, maxAttempts: config.maxAttempts, previousAttempts: history.slice() };
|
|
1802
|
+
if (config.parallelRepairs > 1) {
|
|
1803
|
+
const r = await repairParallel(files, report, {
|
|
1804
|
+
llm,
|
|
1805
|
+
sandbox,
|
|
1806
|
+
config,
|
|
1807
|
+
baseImage,
|
|
1808
|
+
withFixtures: (next) => withFixtures(next, fixtures),
|
|
1809
|
+
context: repairCtx
|
|
1810
|
+
}, log("verify"));
|
|
1811
|
+
yield* drain();
|
|
1812
|
+
files = r.files;
|
|
1813
|
+
report = r.report;
|
|
1814
|
+
yield { type: "files", attempt, files };
|
|
1815
|
+
yield { type: "tests", attempt, report, branches: r.branches };
|
|
1816
|
+
skipInitialTest = true;
|
|
1817
|
+
} else {
|
|
1818
|
+
files = withFixtures(await repair(files, report, llm, config, { context: repairCtx }), fixtures);
|
|
1819
|
+
yield { type: "files", attempt, files };
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
files = bestFiles;
|
|
1823
|
+
report = bestReport ?? report;
|
|
1824
|
+
const ok = report !== null && isGreen(report);
|
|
1825
|
+
yield { type: "step", step: "verify", status: ok ? "done" : "error", detail: `${report.passed}/${report.total} tests after ${attempt} attempt${attempt > 1 ? "s" : ""}` };
|
|
1826
|
+
current = "package";
|
|
1827
|
+
yield { type: "step", step: "package", status: "start" };
|
|
1828
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1829
|
+
yield { type: "result", ok, attempts: attempt, files, report };
|
|
1830
|
+
yield { type: "step", step: "package", status: "done" };
|
|
1831
|
+
} catch (error) {
|
|
1832
|
+
yield* drain();
|
|
1833
|
+
yield { type: "step", step: current, status: "error", detail: messageOf(error) };
|
|
1834
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1835
|
+
yield { type: "error", message: messageOf(error) };
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
async function* researchAgent(prompt, deps) {
|
|
1839
|
+
const { config, llm, search } = deps;
|
|
1840
|
+
yield { type: "mode", mode: "live" };
|
|
1841
|
+
let current = "plan";
|
|
1842
|
+
try {
|
|
1843
|
+
yield { type: "step", step: "plan", status: "start" };
|
|
1844
|
+
const plan = await makePlan(prompt, llm, config);
|
|
1845
|
+
yield { type: "plan", plan };
|
|
1846
|
+
yield { type: "step", step: "plan", status: "done", detail: plan.summary };
|
|
1847
|
+
current = "research";
|
|
1848
|
+
yield { type: "step", step: "research", status: "start" };
|
|
1849
|
+
const queue = [];
|
|
1850
|
+
const { spec, sources } = await research(
|
|
1851
|
+
plan,
|
|
1852
|
+
search,
|
|
1853
|
+
llm,
|
|
1854
|
+
config,
|
|
1855
|
+
(m) => queue.push({ type: "log", step: "research", message: m })
|
|
1856
|
+
);
|
|
1857
|
+
while (queue.length) yield queue.shift();
|
|
1858
|
+
yield { type: "sources", sources };
|
|
1859
|
+
yield { type: "spec", spec };
|
|
1860
|
+
yield { type: "step", step: "research", status: "done", detail: `${spec.target.length + 1} endpoints from ${sources.length} pages` };
|
|
1861
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1862
|
+
} catch (error) {
|
|
1863
|
+
yield { type: "step", step: current, status: "error", detail: messageOf(error) };
|
|
1864
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1865
|
+
yield { type: "error", message: messageOf(error) };
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
async function* verifyAgent(files, deps) {
|
|
1869
|
+
const { config, llm, sandbox } = deps;
|
|
1870
|
+
yield { type: "mode", mode: "live" };
|
|
1871
|
+
const queue = [];
|
|
1872
|
+
const log = (message) => queue.push({ type: "log", step: "verify", message });
|
|
1873
|
+
const drain = function* () {
|
|
1874
|
+
while (queue.length) yield queue.shift();
|
|
1875
|
+
};
|
|
1876
|
+
try {
|
|
1877
|
+
yield { type: "step", step: "verify", status: "start" };
|
|
1878
|
+
const baseImage = await sandbox.prepare(log);
|
|
1879
|
+
yield* drain();
|
|
1880
|
+
let current = files;
|
|
1881
|
+
let report = null;
|
|
1882
|
+
let bestFiles = current;
|
|
1883
|
+
let bestReport = null;
|
|
1884
|
+
let attempt = 1;
|
|
1885
|
+
let skipInitialTest = false;
|
|
1886
|
+
const history = [];
|
|
1887
|
+
for (; ; ) {
|
|
1888
|
+
if (!skipInitialTest) {
|
|
1889
|
+
const preflight = await preflightSyntax(current);
|
|
1890
|
+
if (preflight) {
|
|
1891
|
+
report = preflight;
|
|
1892
|
+
queue.push({ type: "log", step: "verify", message: `Preflight parse failed (${preflight.failed} file${preflight.failed > 1 ? "s" : ""}), skipping sandbox` });
|
|
1893
|
+
} else {
|
|
1894
|
+
report = await sandbox.runTests(baseImage, current, log);
|
|
1895
|
+
}
|
|
1896
|
+
yield* drain();
|
|
1897
|
+
yield { type: "tests", attempt, report };
|
|
1898
|
+
}
|
|
1899
|
+
skipInitialTest = false;
|
|
1900
|
+
({ files: current, report, bestFiles, bestReport } = keepBest({ attempt, files: current, report, bestFiles, bestReport, log: (m) => queue.push({ type: "log", step: "verify", message: m }) }));
|
|
1901
|
+
yield* drain();
|
|
1902
|
+
if (isGreen(report) || attempt >= config.maxAttempts) break;
|
|
1903
|
+
history.push({ attempt, summary: summariseReport(report) });
|
|
1904
|
+
attempt++;
|
|
1905
|
+
yield { type: "log", step: "verify", message: `Attempt ${attempt - 1} failed (${report.failed} tests, typecheck ${report.typecheckOk ? "ok" : "failed"}), repairing` };
|
|
1906
|
+
const repairCtx = { attempt, maxAttempts: config.maxAttempts, previousAttempts: history.slice() };
|
|
1907
|
+
if (config.parallelRepairs > 1) {
|
|
1908
|
+
const r = await repairParallel(current, report, {
|
|
1909
|
+
llm,
|
|
1910
|
+
sandbox,
|
|
1911
|
+
config,
|
|
1912
|
+
baseImage,
|
|
1913
|
+
withFixtures: (next) => next,
|
|
1914
|
+
context: repairCtx
|
|
1915
|
+
}, log);
|
|
1916
|
+
yield* drain();
|
|
1917
|
+
current = r.files;
|
|
1918
|
+
report = r.report;
|
|
1919
|
+
yield { type: "files", attempt, files: current };
|
|
1920
|
+
yield { type: "tests", attempt, report, branches: r.branches };
|
|
1921
|
+
skipInitialTest = true;
|
|
1922
|
+
} else {
|
|
1923
|
+
current = await repair(current, report, llm, config, { context: repairCtx });
|
|
1924
|
+
yield { type: "files", attempt, files: current };
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
current = bestFiles;
|
|
1928
|
+
report = bestReport ?? report;
|
|
1929
|
+
const ok = report !== null && isGreen(report);
|
|
1930
|
+
yield { type: "step", step: "verify", status: ok ? "done" : "error", detail: `${report.passed}/${report.total} tests after ${attempt} attempt${attempt > 1 ? "s" : ""}` };
|
|
1931
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1932
|
+
yield { type: "result", ok, attempts: attempt, files: current, report };
|
|
1933
|
+
} catch (error) {
|
|
1934
|
+
yield* drain();
|
|
1935
|
+
yield { type: "step", step: "verify", status: "error", detail: messageOf(error) };
|
|
1936
|
+
yield { type: "usage", usage: llm.usage() };
|
|
1937
|
+
yield { type: "error", message: messageOf(error) };
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
async function makePlan(prompt, llm, config) {
|
|
1941
|
+
const answer = await llm.chat(config.models.fast, [
|
|
1942
|
+
{ role: "system", content: PLAN_SYSTEM },
|
|
1943
|
+
{ role: "user", content: planUser(prompt) }
|
|
1944
|
+
], { thinking: false, maxTokens: 2e3 });
|
|
1945
|
+
const plan = await parseJsonOrAsk(answer, llm, config.models.fast, PLAN_SYSTEM);
|
|
1946
|
+
if (!plan.source?.app || !plan.target?.app) throw new Error("Could not identify a source and a target app in the request");
|
|
1947
|
+
plan.fieldMapping ??= [];
|
|
1948
|
+
plan.assumptions ??= [];
|
|
1949
|
+
return plan;
|
|
1950
|
+
}
|
|
1951
|
+
async function research(plan, search, llm, config, log) {
|
|
1952
|
+
const sides = [plan.source, plan.target, ...plan.additionalTargets ?? []];
|
|
1953
|
+
const hits = await Promise.all(
|
|
1954
|
+
sides.map(async (side) => {
|
|
1955
|
+
let list = await search.search(side.docsQuery, { maxResults: 8 });
|
|
1956
|
+
if (!list.some((h) => isOfficial(h.url, side.app))) {
|
|
1957
|
+
const domain = guessDomain(side.app);
|
|
1958
|
+
const focused = await search.search(side.docsQuery, { maxResults: 5, includeDomains: [domain] });
|
|
1959
|
+
log(`No ${side.app} page in the results, searched ${domain} directly: ${focused.length} results`);
|
|
1960
|
+
list = [...focused, ...list];
|
|
1961
|
+
}
|
|
1962
|
+
const ranked = rankOfficial(list, side.app).slice(0, 2);
|
|
1963
|
+
const official = ranked.filter((h) => isOfficial(h.url, side.app)).length;
|
|
1964
|
+
log(`Searched "${side.docsQuery}": ${official} of 2 picked from ${side.app}'s own docs`);
|
|
1965
|
+
return ranked.map((h) => ({ ...h, app: side.app }));
|
|
1966
|
+
})
|
|
1967
|
+
);
|
|
1968
|
+
const picked = hits.flat();
|
|
1969
|
+
const urls = [...new Set(picked.map((h) => h.url))];
|
|
1970
|
+
const pages = await search.extract(urls);
|
|
1971
|
+
log(`Extracted ${pages.length} documentation pages`);
|
|
1972
|
+
const docs = pages.length ? pages : picked.map((h) => ({ url: h.url, content: h.content }));
|
|
1973
|
+
const answer = await llm.chat(config.models.mid, [
|
|
1974
|
+
{ role: "system", content: SPEC_SYSTEM },
|
|
1975
|
+
{ role: "user", content: specUser(plan, docs) }
|
|
1976
|
+
], { thinking: false, maxTokens: 6e3 });
|
|
1977
|
+
const spec = await parseJsonOrAsk(answer, llm, config.models.mid, SPEC_SYSTEM);
|
|
1978
|
+
if (!spec.source || !Array.isArray(spec.target) || spec.target.length === 0) {
|
|
1979
|
+
throw new Error("Research did not produce a usable API spec");
|
|
1980
|
+
}
|
|
1981
|
+
return { spec, sources: picked.map((h) => ({ title: h.title, url: h.url, official: isOfficial(h.url, h.app) })) };
|
|
1982
|
+
}
|
|
1983
|
+
async function generate(plan, spec, fixtures, llm, config) {
|
|
1984
|
+
const answer = await llm.chat(config.models.strong, [
|
|
1985
|
+
{ role: "system", content: GENERATE_SYSTEM },
|
|
1986
|
+
{ role: "user", content: generateUser(plan, spec, fixtures) }
|
|
1987
|
+
], { maxTokens: 32e3, temperature: 0.1 });
|
|
1988
|
+
return requireFiles(parseFileBlocks(answer));
|
|
1989
|
+
}
|
|
1990
|
+
async function repair(files, report, llm, config, options = {}) {
|
|
1991
|
+
const answer = await llm.chat(config.models.strong, [
|
|
1992
|
+
{ role: "system", content: GENERATE_SYSTEM },
|
|
1993
|
+
{ role: "user", content: repairUser(renderFileBlocks(files), report, options.context) }
|
|
1994
|
+
], { maxTokens: 32e3, temperature: options.temperature ?? 0.1 });
|
|
1995
|
+
const repaired = parseFileBlocks(answer);
|
|
1996
|
+
const merged = new Map(files.map((f) => [f.path, f]));
|
|
1997
|
+
for (const file of repaired) merged.set(file.path, file);
|
|
1998
|
+
return requireFiles([...merged.values()]);
|
|
1999
|
+
}
|
|
2000
|
+
var REPAIR_TEMPERATURES = [0.1, 0.4, 0.7, 0.9];
|
|
2001
|
+
async function repairParallel(files, report, deps, log) {
|
|
2002
|
+
const n = Math.max(1, Math.min(REPAIR_TEMPERATURES.length, deps.config.parallelRepairs));
|
|
2003
|
+
const temperatures = REPAIR_TEMPERATURES.slice(0, n);
|
|
2004
|
+
log(`Repairing with ${n} parallel branch${n > 1 ? "es" : ""} (temperatures ${temperatures.join(", ")})`);
|
|
2005
|
+
const candidates = await Promise.all(
|
|
2006
|
+
temperatures.map(async (temperature, i) => {
|
|
2007
|
+
const generated = await repair(files, report, deps.llm, deps.config, { temperature, context: deps.context });
|
|
2008
|
+
return { branch: i + 1, files: deps.withFixtures(generated) };
|
|
2009
|
+
})
|
|
2010
|
+
);
|
|
2011
|
+
const results = await Promise.all(
|
|
2012
|
+
candidates.map(async (c) => {
|
|
2013
|
+
const preflight = await preflightSyntax(c.files);
|
|
2014
|
+
if (preflight) {
|
|
2015
|
+
log(`[branch ${c.branch}] preflight parse failed (${preflight.failed}), skipping sandbox`);
|
|
2016
|
+
return { ...c, report: withContract(preflight, c.files) };
|
|
2017
|
+
}
|
|
2018
|
+
const branchReport = withContract(
|
|
2019
|
+
await deps.sandbox.runTests(deps.baseImage, c.files, (m) => log(`[branch ${c.branch}] ${m}`)),
|
|
2020
|
+
c.files
|
|
2021
|
+
);
|
|
2022
|
+
return { ...c, report: branchReport };
|
|
2023
|
+
})
|
|
2024
|
+
);
|
|
2025
|
+
for (const r of results) {
|
|
2026
|
+
log(
|
|
2027
|
+
`branch ${r.branch}: ${r.report.passed}/${r.report.total}, typecheck ${r.report.typecheckOk ? "ok" : "failed"}, contract ${r.report.contract}`
|
|
2028
|
+
);
|
|
2029
|
+
}
|
|
2030
|
+
const green = results.find((r) => isGreen(r.report));
|
|
2031
|
+
const winner = green ?? results.reduce((best, r) => isBetter(r.report, best.report) ? r : best);
|
|
2032
|
+
log(`Chose branch ${winner.branch}${green ? " (first green)" : " (best overall)"}`);
|
|
2033
|
+
return {
|
|
2034
|
+
files: winner.files,
|
|
2035
|
+
report: winner.report,
|
|
2036
|
+
branches: results.map((r) => ({
|
|
2037
|
+
branch: r.branch,
|
|
2038
|
+
passed: r.report.passed,
|
|
2039
|
+
total: r.report.total,
|
|
2040
|
+
typecheckOk: r.report.typecheckOk,
|
|
2041
|
+
green: isGreen(r.report)
|
|
2042
|
+
}))
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
function requireFiles(files) {
|
|
2046
|
+
const missing = REQUIRED_FILES.filter((path) => !files.some((f) => f.path === path));
|
|
2047
|
+
if (missing.length) throw new Error(`Model output is missing ${missing.join(", ")}`);
|
|
2048
|
+
return REQUIRED_FILES.map((path) => files.find((f) => f.path === path));
|
|
2049
|
+
}
|
|
2050
|
+
async function parseJsonOrAsk(answer, llm, model, system) {
|
|
2051
|
+
try {
|
|
2052
|
+
return extractJson(answer);
|
|
2053
|
+
} catch (error) {
|
|
2054
|
+
const fixed = await llm.chat(model, [
|
|
2055
|
+
{ role: "system", content: system },
|
|
2056
|
+
{ role: "user", content: `This answer is not valid JSON (${messageOf(error)}). Return the same content as strict JSON: double quotes, no comments, no trailing commas.
|
|
2057
|
+
|
|
2058
|
+
${answer.slice(0, 12e3)}` }
|
|
2059
|
+
], { thinking: false, maxTokens: 6e3 });
|
|
2060
|
+
return extractJson(fixed);
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
function guessDomain(app) {
|
|
2064
|
+
return `${app.toLowerCase().replace(/[^a-z0-9]/g, "")}.com`;
|
|
2065
|
+
}
|
|
2066
|
+
function keepBest(args) {
|
|
2067
|
+
const { attempt, report, files, bestFiles, bestReport, log } = args;
|
|
2068
|
+
if (bestReport === null || isBetter(report, bestReport)) {
|
|
2069
|
+
return { files, report, bestFiles: files, bestReport: report };
|
|
2070
|
+
}
|
|
2071
|
+
if (attempt > 1) {
|
|
2072
|
+
const strictlyWorse = report.passed - report.failed < bestReport.passed - bestReport.failed;
|
|
2073
|
+
const verb = strictlyWorse ? "regressed" : "did not improve";
|
|
2074
|
+
log(`Attempt ${attempt} ${verb} (${report.passed}/${report.total} vs best ${bestReport.passed}/${bestReport.total}), rolling back for repair`);
|
|
2075
|
+
}
|
|
2076
|
+
return { files: bestFiles, report: bestReport, bestFiles, bestReport };
|
|
2077
|
+
}
|
|
2078
|
+
function isBetter(a, b) {
|
|
2079
|
+
const greenA = isGreen(a), greenB = isGreen(b);
|
|
2080
|
+
if (greenA !== greenB) return greenA;
|
|
2081
|
+
const diff = a.passed - a.failed - (b.passed - b.failed);
|
|
2082
|
+
if (diff !== 0) return diff > 0;
|
|
2083
|
+
if (a.typecheckOk !== b.typecheckOk) return a.typecheckOk;
|
|
2084
|
+
return false;
|
|
2085
|
+
}
|
|
2086
|
+
var messageOf = (error) => {
|
|
2087
|
+
if (!(error instanceof Error)) return String(error);
|
|
2088
|
+
const cause = error.cause;
|
|
2089
|
+
if (cause instanceof Error) return `${error.message} \u2014 cause: ${cause.name}${cause.message ? `: ${cause.message}` : ""}${cause.code ? ` (${cause.code})` : ""}`;
|
|
2090
|
+
if (cause && typeof cause === "object") {
|
|
2091
|
+
const c = cause;
|
|
2092
|
+
return `${error.message} \u2014 cause: ${c.code ?? ""}${c.message ? ` ${c.message}` : ""}`.trim();
|
|
2093
|
+
}
|
|
2094
|
+
return error.message;
|
|
2095
|
+
};
|
|
2096
|
+
function summariseReport(report) {
|
|
2097
|
+
const bits = [];
|
|
2098
|
+
bits.push(`${report.passed}/${report.total} tests`);
|
|
2099
|
+
bits.push(report.typecheckOk ? "typecheck ok" : "typecheck failed");
|
|
2100
|
+
if (report.contract) bits.push(`contract ${report.contract}`);
|
|
2101
|
+
const firstFailure = report.failures[0];
|
|
2102
|
+
if (firstFailure) bits.push(`e.g. "${firstFailure.name}: ${firstFailure.message.slice(0, 160).replace(/\s+/g, " ")}"`);
|
|
2103
|
+
return bits.join(", ");
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
// src/sandbox.ts
|
|
2107
|
+
var NODE_IMAGE_TAG = "patchbay-node:22";
|
|
2108
|
+
var NODE_IMAGE_REGISTRY = "docker://docker.io/library/node:22-slim";
|
|
2109
|
+
var PREPARE_COMMAND = [
|
|
2110
|
+
"mkdir -p /work/src",
|
|
2111
|
+
"cd /work",
|
|
2112
|
+
`printf '%s' '{"name":"connector","private":true,"type":"module"}' > package.json`,
|
|
2113
|
+
"npm install --no-audit --no-fund --loglevel=error -D vitest@3.2.4 typescript@5.6.3 @types/node@22"
|
|
2114
|
+
].join(" && ");
|
|
2115
|
+
var TSCONFIG2 = JSON.stringify(
|
|
2116
|
+
{
|
|
2117
|
+
compilerOptions: {
|
|
2118
|
+
target: "ES2022",
|
|
2119
|
+
module: "ESNext",
|
|
2120
|
+
moduleResolution: "Bundler",
|
|
2121
|
+
strict: true,
|
|
2122
|
+
skipLibCheck: true,
|
|
2123
|
+
types: ["node"],
|
|
2124
|
+
lib: ["ES2022", "DOM"]
|
|
2125
|
+
},
|
|
2126
|
+
include: ["src/**/*.ts"]
|
|
2127
|
+
},
|
|
2128
|
+
null,
|
|
2129
|
+
2
|
|
2130
|
+
);
|
|
2131
|
+
function createNebiusSandbox(opts) {
|
|
2132
|
+
const doFetch = opts.fetch ?? fetch;
|
|
2133
|
+
const pollMs = opts.pollIntervalMs ?? 1500;
|
|
2134
|
+
let cachedImage = opts.preparedImage;
|
|
2135
|
+
const headers = (extra = {}) => ({
|
|
2136
|
+
Authorization: `Bearer ${opts.apiKey}`,
|
|
2137
|
+
Project: opts.project,
|
|
2138
|
+
...extra
|
|
2139
|
+
});
|
|
2140
|
+
async function request(path, init = {}) {
|
|
2141
|
+
const method = (init.method ?? "GET").toUpperCase();
|
|
2142
|
+
const maxTries = 3;
|
|
2143
|
+
let lastError;
|
|
2144
|
+
for (let attempt = 0; attempt < maxTries; attempt++) {
|
|
2145
|
+
try {
|
|
2146
|
+
const res = await doFetch(`${opts.baseUrl}${path}`, { ...init, headers: headers(init.headers) });
|
|
2147
|
+
const text = await res.text();
|
|
2148
|
+
if (res.status >= 500 && res.status < 600 && attempt < maxTries - 1) {
|
|
2149
|
+
lastError = new Error(`HTTP ${res.status} ${text.slice(0, 120)}`);
|
|
2150
|
+
await sleep3(500 * Math.pow(2, attempt));
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
if (!res.ok) throw new Error(`Sandbox ${method} ${path}: HTTP ${res.status} ${text.slice(0, 300)}`);
|
|
2154
|
+
return { body: text ? JSON.parse(text) : {}, location: res.headers.get("location") };
|
|
2155
|
+
} catch (error) {
|
|
2156
|
+
lastError = error;
|
|
2157
|
+
if (attempt >= maxTries - 1) break;
|
|
2158
|
+
await sleep3(500 * Math.pow(2, attempt));
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
throw lastError instanceof Error ? lastError : new Error(`Sandbox ${method} ${path}: ${String(lastError)}`);
|
|
2162
|
+
}
|
|
2163
|
+
async function uploadFile(content) {
|
|
2164
|
+
const { body } = await request("/files", {
|
|
2165
|
+
method: "POST",
|
|
2166
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
2167
|
+
body: content
|
|
2168
|
+
});
|
|
2169
|
+
return body.uuid;
|
|
2170
|
+
}
|
|
2171
|
+
async function waitFor(operationId, timeoutMs) {
|
|
2172
|
+
const deadline = Date.now() + timeoutMs;
|
|
2173
|
+
for (; ; ) {
|
|
2174
|
+
const { body } = await request(`/operations/${operationId}`);
|
|
2175
|
+
if (body.status === "SUCCESS" || body.status === "FAILED" || body.status === "CANCELLED") return body;
|
|
2176
|
+
if (Date.now() > deadline) throw new Error(`Sandbox operation ${operationId} did not finish in ${timeoutMs / 1e3}s`);
|
|
2177
|
+
await sleep3(pollMs);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
function operationIdFrom(location, body) {
|
|
2181
|
+
const fromLocation = location?.split("/").filter(Boolean).pop();
|
|
2182
|
+
const id = fromLocation ?? body.uuid;
|
|
2183
|
+
if (!id) throw new Error("Sandbox did not return an operation id");
|
|
2184
|
+
return id;
|
|
2185
|
+
}
|
|
2186
|
+
async function ensureNodeImage(log) {
|
|
2187
|
+
const { body } = await request("/images?tagged=1&limit=200");
|
|
2188
|
+
const existing = (body.images ?? []).find((image2) => image2.tag === NODE_IMAGE_TAG);
|
|
2189
|
+
if (existing) return existing.uuid;
|
|
2190
|
+
log("Importing node:22-slim into the sandbox registry (one-time)");
|
|
2191
|
+
const started = await request("/images/import", {
|
|
2192
|
+
method: "POST",
|
|
2193
|
+
headers: { "Content-Type": "application/json" },
|
|
2194
|
+
body: JSON.stringify({ registry: { url: NODE_IMAGE_REGISTRY }, tag: NODE_IMAGE_TAG, timeout: 600 })
|
|
2195
|
+
});
|
|
2196
|
+
const op = await waitFor(operationIdFrom(started.location, started.body), 66e4);
|
|
2197
|
+
const image = op.result?.image ?? op.result_image_uuid;
|
|
2198
|
+
if (op.status !== "SUCCESS" || !image) throw new Error(`Image import failed: ${op.error ?? op.status}`);
|
|
2199
|
+
return image;
|
|
2200
|
+
}
|
|
2201
|
+
return {
|
|
2202
|
+
async prepare(log) {
|
|
2203
|
+
if (cachedImage) {
|
|
2204
|
+
log(`Reusing prepared sandbox image ${cachedImage.slice(0, 8)}`);
|
|
2205
|
+
return cachedImage;
|
|
2206
|
+
}
|
|
2207
|
+
const nodeImage = await ensureNodeImage(log);
|
|
2208
|
+
log("Installing typescript + vitest in a sandbox checkpoint");
|
|
2209
|
+
const started = await request("/instances", {
|
|
2210
|
+
method: "POST",
|
|
2211
|
+
headers: { "Content-Type": "application/json" },
|
|
2212
|
+
body: JSON.stringify({ image: nodeImage, command: PREPARE_COMMAND, shell: true, timeout: 300, networking: { enabled: true } })
|
|
2213
|
+
});
|
|
2214
|
+
const op = await waitFor(operationIdFrom(started.location, started.body), 33e4);
|
|
2215
|
+
const exit = op.metadata?.result?.state?.exit_code;
|
|
2216
|
+
if (op.status !== "SUCCESS" || !op.result_image_uuid || exit !== 0) {
|
|
2217
|
+
throw new Error(`Sandbox preparation failed (exit ${exit ?? "?"}): ${decode(op.metadata?.result?.stderr).slice(0, 500)}`);
|
|
2218
|
+
}
|
|
2219
|
+
cachedImage = op.result_image_uuid;
|
|
2220
|
+
log(`Checkpoint ready: ${cachedImage.slice(0, 8)}`);
|
|
2221
|
+
return cachedImage;
|
|
2222
|
+
},
|
|
2223
|
+
async runTests(baseImage, files, log) {
|
|
2224
|
+
const all = [{ path: "tsconfig.json", content: TSCONFIG2 }, ...files.filter((f) => f.path.endsWith(".ts"))];
|
|
2225
|
+
const mapped = {};
|
|
2226
|
+
for (const file of all) {
|
|
2227
|
+
mapped[`/work/${file.path}`] = { uuid: await uploadFile(file.content), mode: "0644" };
|
|
2228
|
+
}
|
|
2229
|
+
log(`Forking checkpoint ${baseImage.slice(0, 8)} with ${all.length} files, network off`);
|
|
2230
|
+
const started = await request("/instances", {
|
|
2231
|
+
method: "POST",
|
|
2232
|
+
headers: { "Content-Type": "application/json" },
|
|
2233
|
+
body: JSON.stringify({
|
|
2234
|
+
image: baseImage,
|
|
2235
|
+
command: TEST_COMMAND,
|
|
2236
|
+
shell: true,
|
|
2237
|
+
disposable: true,
|
|
2238
|
+
timeout: 120,
|
|
2239
|
+
files: mapped,
|
|
2240
|
+
networking: { enabled: false }
|
|
2241
|
+
})
|
|
2242
|
+
});
|
|
2243
|
+
const operationId = operationIdFrom(started.location, started.body);
|
|
2244
|
+
const op = await waitFor(operationId, 18e4);
|
|
2245
|
+
if (op.status !== "SUCCESS" && !op.metadata?.result) {
|
|
2246
|
+
throw new Error(`Sandbox test run ${op.status}: ${op.error ?? "no result"}`);
|
|
2247
|
+
}
|
|
2248
|
+
return parseTestOutput(decode(op.metadata?.result?.stdout), `nebius-sandbox:${operationId}`);
|
|
2249
|
+
}
|
|
2250
|
+
};
|
|
2251
|
+
}
|
|
2252
|
+
function decode(stream) {
|
|
2253
|
+
if (!stream?.value) return "";
|
|
2254
|
+
return stream.encoding === "base64" ? Buffer.from(stream.value, "base64").toString("utf8") : stream.value;
|
|
2255
|
+
}
|
|
2256
|
+
var sleep3 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
2257
|
+
|
|
2258
|
+
// src/tavily.ts
|
|
2259
|
+
function createTavilyClient(opts) {
|
|
2260
|
+
const doFetch = opts.fetch ?? fetch;
|
|
2261
|
+
async function post(path, body) {
|
|
2262
|
+
const res = await doFetch(`https://api.tavily.com${path}`, {
|
|
2263
|
+
method: "POST",
|
|
2264
|
+
headers: { Authorization: `Bearer ${opts.apiKey}`, "Content-Type": "application/json" },
|
|
2265
|
+
body: JSON.stringify(body)
|
|
2266
|
+
});
|
|
2267
|
+
if (!res.ok) throw new Error(`Tavily ${path}: HTTP ${res.status} ${await res.text().catch(() => "")}`);
|
|
2268
|
+
return await res.json();
|
|
2269
|
+
}
|
|
2270
|
+
return {
|
|
2271
|
+
async search(query, options = {}) {
|
|
2272
|
+
const data = await post("/search", {
|
|
2273
|
+
query,
|
|
2274
|
+
search_depth: "advanced",
|
|
2275
|
+
max_results: options.maxResults ?? 5,
|
|
2276
|
+
include_answer: false,
|
|
2277
|
+
...options.includeDomains?.length ? { include_domains: options.includeDomains } : {}
|
|
2278
|
+
});
|
|
2279
|
+
return (data.results ?? []).map((r) => ({ title: r.title, url: r.url, content: r.content, score: r.score }));
|
|
2280
|
+
},
|
|
2281
|
+
async extract(urls) {
|
|
2282
|
+
if (urls.length === 0) return [];
|
|
2283
|
+
const data = await post("/extract", {
|
|
2284
|
+
urls,
|
|
2285
|
+
extract_depth: "advanced"
|
|
2286
|
+
});
|
|
2287
|
+
return (data.results ?? []).map((r) => ({ url: r.url, content: r.raw_content ?? "" }));
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
|
|
2292
|
+
// src/index.ts
|
|
2293
|
+
function startRun(prompt, config = loadConfig()) {
|
|
2294
|
+
if (config.mock || missingLiveKeys(config).length > 0) return replayRun();
|
|
2295
|
+
return runAgent(prompt, {
|
|
2296
|
+
config,
|
|
2297
|
+
llm: createNebiusClient({ apiKey: config.nebiusApiKey, baseUrl: config.nebiusBaseUrl, supportsNemotronThinking: config.llmProvider === "nebius" }),
|
|
2298
|
+
search: createTavilyClient({ apiKey: config.tavilyApiKey }),
|
|
2299
|
+
sandbox: config.runner === "local" ? createLocalRunner() : createNebiusSandbox({
|
|
2300
|
+
apiKey: config.nebiusApiKey,
|
|
2301
|
+
project: config.nebiusProject,
|
|
2302
|
+
baseUrl: config.sandboxUrl,
|
|
2303
|
+
preparedImage: config.baseImage
|
|
2304
|
+
})
|
|
2305
|
+
});
|
|
2306
|
+
}
|
|
2307
|
+
function startResearch(prompt, config = loadConfig()) {
|
|
2308
|
+
if (config.mock || missingLiveKeys(config).length > 0) return truncateAt(replayRun(), "spec");
|
|
2309
|
+
return researchAgent(prompt, {
|
|
2310
|
+
config,
|
|
2311
|
+
llm: createNebiusClient({ apiKey: config.nebiusApiKey, baseUrl: config.nebiusBaseUrl, supportsNemotronThinking: config.llmProvider === "nebius" }),
|
|
2312
|
+
search: createTavilyClient({ apiKey: config.tavilyApiKey })
|
|
2313
|
+
});
|
|
2314
|
+
}
|
|
2315
|
+
function startVerify(files, config = loadConfig()) {
|
|
2316
|
+
if (config.mock || missingLiveKeys(config).length > 0) {
|
|
2317
|
+
throw new Error("Verify needs live keys (NEBIUS_API_KEY, NEBIUS_AI_PROJECT). Set PATCHBAY_MOCK=false and provide credentials.");
|
|
2318
|
+
}
|
|
2319
|
+
return verifyAgent(files, {
|
|
2320
|
+
config,
|
|
2321
|
+
llm: createNebiusClient({ apiKey: config.nebiusApiKey, baseUrl: config.nebiusBaseUrl, supportsNemotronThinking: config.llmProvider === "nebius" }),
|
|
2322
|
+
sandbox: config.runner === "local" ? createLocalRunner() : createNebiusSandbox({
|
|
2323
|
+
apiKey: config.nebiusApiKey,
|
|
2324
|
+
project: config.nebiusProject,
|
|
2325
|
+
baseUrl: config.sandboxUrl,
|
|
2326
|
+
preparedImage: config.baseImage
|
|
2327
|
+
})
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2330
|
+
async function* truncateAt(stream, lastType) {
|
|
2331
|
+
for await (const event of stream) {
|
|
2332
|
+
yield event;
|
|
2333
|
+
if (event.type === lastType) return;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
export {
|
|
2338
|
+
loadConfig,
|
|
2339
|
+
missingLiveKeys,
|
|
2340
|
+
parseTestOutput,
|
|
2341
|
+
isGreen,
|
|
2342
|
+
REPLAY_RUN,
|
|
2343
|
+
replayRun,
|
|
2344
|
+
runAgent,
|
|
2345
|
+
researchAgent,
|
|
2346
|
+
verifyAgent,
|
|
2347
|
+
makePlan,
|
|
2348
|
+
research,
|
|
2349
|
+
generate,
|
|
2350
|
+
repair,
|
|
2351
|
+
repairParallel,
|
|
2352
|
+
startRun,
|
|
2353
|
+
startResearch,
|
|
2354
|
+
startVerify
|
|
2355
|
+
};
|
|
2356
|
+
//# sourceMappingURL=chunk-JVXVCXNZ.js.map
|