@bridge_gpt/mcp-server 0.1.16 → 0.2.0
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/README.md +333 -162
- package/build/agent-capabilities/cli.js +152 -0
- package/build/agent-capabilities/default-deps.js +45 -0
- package/build/agent-capabilities/probe-context.js +111 -0
- package/build/agent-capabilities/probes.js +278 -0
- package/build/agent-capabilities/reporter.js +50 -0
- package/build/agent-capabilities/runner.js +56 -0
- package/build/agent-capabilities/types.js +10 -0
- package/build/agent-launchers/claude.js +85 -0
- package/build/agent-launchers/index.js +17 -0
- package/build/agent-launchers/types.js +1 -0
- package/build/agents.generated.js +1 -1
- package/build/brainstorm-files.js +89 -0
- package/build/bridge-config.js +404 -0
- package/build/chain-orchestrator.js +1364 -0
- package/build/chain-utils.js +68 -0
- package/build/commands.generated.js +5 -3
- package/build/credential-materialization.js +128 -0
- package/build/credential-store.js +232 -0
- package/build/decision-page-schema.js +39 -6
- package/build/decision-page-template.js +54 -18
- package/build/doctor.js +18 -2
- package/build/fetch-stub.js +139 -0
- package/build/git-ignore-utils.js +63 -0
- package/build/index.js +1623 -546
- package/build/mcp-invoke.js +417 -0
- package/build/mcp-provisioning.js +249 -0
- package/build/mcp-registration-doctor.js +96 -0
- package/build/pipeline-orchestrator.js +66 -1
- package/build/pipeline-utils.js +33 -0
- package/build/pipelines.generated.js +165 -5
- package/build/schedule-run.js +951 -0
- package/build/schedule-store.js +132 -0
- package/build/scheduler-backends/at-fallback.js +144 -0
- package/build/scheduler-backends/escaping.js +113 -0
- package/build/scheduler-backends/index.js +72 -0
- package/build/scheduler-backends/launchd.js +216 -0
- package/build/scheduler-backends/systemd-user.js +237 -0
- package/build/scheduler-backends/task-scheduler.js +219 -0
- package/build/scheduler-backends/types.js +23 -0
- package/build/start-tickets-prereqs.js +90 -1
- package/build/start-tickets.js +222 -70
- package/build/third-party-mcp-targets.js +75 -0
- package/build/version.generated.js +1 -1
- package/package.json +8 -8
- package/pipelines/full-automation.json +49 -0
- package/pipelines/idea-to-ticket.json +71 -0
- package/pipelines/implement-ticket.json +28 -2
- package/smoke-test/SMOKE-TEST.md +511 -0
- package/smoke-test/smoke-test-mcp.md +23 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Non-throwing probe runner. For each selected agent it builds a probe context,
|
|
3
|
+
* filters probes by `--only` and per-agent applicability, applies the skip-gate
|
|
4
|
+
* (binary absent, or an `env`-auth agent whose credential is unset → skip BEFORE
|
|
5
|
+
* spawning), runs each probe catching any throw as `fail`, and cleans up temp dirs.
|
|
6
|
+
*/
|
|
7
|
+
import { resolveAgentSpec } from "../agent-registry.js";
|
|
8
|
+
import { createProbeContext } from "./probe-context.js";
|
|
9
|
+
import { ALL_PROBES } from "./probes.js";
|
|
10
|
+
import { AGENT_AUTH, } from "./types.js";
|
|
11
|
+
/** Apply the skip-gate, then run the probe (catching throws as `fail`). */
|
|
12
|
+
async function runOneProbe(deps, agentName, probe, ctx) {
|
|
13
|
+
if (probe.spawnsAgent) {
|
|
14
|
+
if (ctx.resolvedBinary === null) {
|
|
15
|
+
return { status: "skip", detail: `${ctx.agent.command} not on PATH — see binary-resolves` };
|
|
16
|
+
}
|
|
17
|
+
const auth = AGENT_AUTH[agentName];
|
|
18
|
+
if (auth.kind === "env") {
|
|
19
|
+
const value = deps.env[auth.varName];
|
|
20
|
+
if (!value || value.length === 0) {
|
|
21
|
+
return { status: "skip", detail: `${auth.varName} not set — set it to run this probe` };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// interactive auth (claude) cannot be verified from env — run and report the outcome.
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return await probe.run(ctx);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
return { status: "fail", detail: err instanceof Error ? err.message : String(err) };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export async function collectCapabilityResults(deps, options) {
|
|
34
|
+
const records = [];
|
|
35
|
+
for (const agentName of options.agents) {
|
|
36
|
+
const agent = resolveAgentSpec(agentName);
|
|
37
|
+
if (!agent)
|
|
38
|
+
continue; // defensive: agent names are validated by the CLI parser
|
|
39
|
+
const applicable = ALL_PROBES.filter((p) => p.appliesTo.includes(agentName) && (!options.only || options.only.includes(p.id)));
|
|
40
|
+
const { ctx, cleanup } = await createProbeContext(deps, agent, options.timeoutMs);
|
|
41
|
+
try {
|
|
42
|
+
for (const probe of applicable) {
|
|
43
|
+
const result = await runOneProbe(deps, agentName, probe, ctx);
|
|
44
|
+
records.push({ agent: agentName, probeId: probe.id, title: probe.title, result });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
await cleanup();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { records };
|
|
52
|
+
}
|
|
53
|
+
/** True when any probe failed or hung — drives the nonzero CLI exit code. */
|
|
54
|
+
export function hasFailureOrHang(collection) {
|
|
55
|
+
return collection.records.some((r) => r.result.status === "fail" || r.result.status === "hang");
|
|
56
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Per-agent auth requirement used by the runner's skip-gate and the reporter. */
|
|
2
|
+
export const AGENT_AUTH = {
|
|
3
|
+
claude: {
|
|
4
|
+
kind: "interactive",
|
|
5
|
+
note: "Claude Code authenticates via keychain/OAuth — cannot be verified from the environment.",
|
|
6
|
+
},
|
|
7
|
+
"cursor-agent": { kind: "env", varName: "CURSOR_API_KEY" },
|
|
8
|
+
};
|
|
9
|
+
/** Default hard timeout per headless probe run (ms). The cursor `-p` hang bug is version-sensitive. */
|
|
10
|
+
export const DEFAULT_PROBE_TIMEOUT_MS = 90_000;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The v1 `claude` agent launcher (BAPI-327).
|
|
3
|
+
*
|
|
4
|
+
* Resolves the `claude` binary against the *baked schedule-time PATH* (not the
|
|
5
|
+
* ambient process default), builds the locked
|
|
6
|
+
* `/full-automation --scheduled-at <T> --idea-file <abs> [--auto]`
|
|
7
|
+
* prompt, and emits `{ exe, args: ["-p", prompt] }`. Claude Code has no
|
|
8
|
+
* working-directory flag, so the cwd is always set by the scheduler unit — this
|
|
9
|
+
* adapter must never add a cwd argument to the invocation.
|
|
10
|
+
*/
|
|
11
|
+
import { pathApiForPlatform } from "../scheduler-backends/types.js";
|
|
12
|
+
import { posixShellQuote, windowsCmdQuote } from "../scheduler-backends/escaping.js";
|
|
13
|
+
/**
|
|
14
|
+
* Resolve a bare command to an absolute path using the platform PATH-probe,
|
|
15
|
+
* forcing the baked PATH so the schedule resolves the same binary the user had
|
|
16
|
+
* at creation time. On Windows both `PATH` and `Path` are overridden (Node and
|
|
17
|
+
* `where.exe` disagree on casing). Returns the first non-empty *absolute* path
|
|
18
|
+
* from stdout, or `null` when the probe fails or yields only relative paths.
|
|
19
|
+
*/
|
|
20
|
+
export async function resolveCommandOnPath(command, envPath, deps) {
|
|
21
|
+
const pathApi = pathApiForPlatform(deps.platform);
|
|
22
|
+
const probe = deps.platform === "win32" ? "where.exe" : "which";
|
|
23
|
+
const env = { ...deps.env, PATH: envPath };
|
|
24
|
+
if (deps.platform === "win32") {
|
|
25
|
+
// Windows env var lookup is case-insensitive but Node preserves the literal
|
|
26
|
+
// key; override both casings so neither the probe nor child sees a stale one.
|
|
27
|
+
env.Path = envPath;
|
|
28
|
+
}
|
|
29
|
+
const result = await deps.runCommand(probe, [command], { env });
|
|
30
|
+
if (result.exitCode !== 0)
|
|
31
|
+
return null;
|
|
32
|
+
const candidate = result.stdout
|
|
33
|
+
.split(/\r?\n/)
|
|
34
|
+
.map((line) => line.trim())
|
|
35
|
+
.find((line) => line.length > 0);
|
|
36
|
+
if (!candidate)
|
|
37
|
+
return null;
|
|
38
|
+
if (!pathApi.isAbsolute(candidate))
|
|
39
|
+
return null;
|
|
40
|
+
return pathApi.normalize(candidate);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Quote the idea-file path so it survives as a single `/full-automation`
|
|
44
|
+
* argument even when it contains spaces or markup-significant characters
|
|
45
|
+
* (`&`, `<`, `>`, …) that would otherwise be split or mangled when the slash
|
|
46
|
+
* command is parsed. Only an embedded double quote needs escaping; the same
|
|
47
|
+
* absolute path is additionally baked into `BRIDGE_GPT_IDEA_FILE` by every
|
|
48
|
+
* backend as a robust environment fallback.
|
|
49
|
+
*/
|
|
50
|
+
export function quoteIdeaFileForPrompt(ideaFile) {
|
|
51
|
+
return `"${ideaFile.replace(/"/g, '\\"')}"`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Build the exact full-automation prompt. `--auto` is appended by
|
|
55
|
+
* default; it is omitted only when the caller selected `--no-auto`.
|
|
56
|
+
*/
|
|
57
|
+
export function buildClaudePrompt(input) {
|
|
58
|
+
const base = `/full-automation --scheduled-at ${input.runAtIso} ` +
|
|
59
|
+
`--idea-file ${quoteIdeaFileForPrompt(input.ideaFile)}`;
|
|
60
|
+
return input.autoApprove ? `${base} --auto` : base;
|
|
61
|
+
}
|
|
62
|
+
const CLAUDE_CAPABILITY = {
|
|
63
|
+
name: "claude",
|
|
64
|
+
command: "claude",
|
|
65
|
+
supportsCwdFlag: false,
|
|
66
|
+
promptFlag: "-p",
|
|
67
|
+
};
|
|
68
|
+
/** Create the v1 Claude agent launcher. */
|
|
69
|
+
export function createClaudeLauncher() {
|
|
70
|
+
return {
|
|
71
|
+
capability: CLAUDE_CAPABILITY,
|
|
72
|
+
resolveBinary(envPath, deps) {
|
|
73
|
+
return resolveCommandOnPath("claude", envPath, deps);
|
|
74
|
+
},
|
|
75
|
+
buildInvocation(exe, input) {
|
|
76
|
+
const prompt = buildClaudePrompt(input);
|
|
77
|
+
// No working-directory flag: cwd is owned by the scheduler unit.
|
|
78
|
+
return { exe, args: [CLAUDE_CAPABILITY.promptFlag, prompt], prompt };
|
|
79
|
+
},
|
|
80
|
+
formatInvocationLine(invocation, platform) {
|
|
81
|
+
const quote = platform === "win32" ? windowsCmdQuote : posixShellQuote;
|
|
82
|
+
return [invocation.exe, ...invocation.args].map((part) => quote(part)).join(" ");
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-launcher registry (BAPI-327). v1 ships only `claude`; the lookup returns
|
|
3
|
+
* `null` for every other name so the CLI can reject unsupported agents with a
|
|
4
|
+
* clear message rather than silently falling back.
|
|
5
|
+
*/
|
|
6
|
+
import { createClaudeLauncher } from "./claude.js";
|
|
7
|
+
export { createClaudeLauncher } from "./claude.js";
|
|
8
|
+
const CLAUDE_LAUNCHER = createClaudeLauncher();
|
|
9
|
+
/** Return the launcher for a name, or `null` when unsupported in v1. */
|
|
10
|
+
export function getAgentLauncher(name) {
|
|
11
|
+
return name === "claude" ? CLAUDE_LAUNCHER : null;
|
|
12
|
+
}
|
|
13
|
+
/** Comma-separated list of valid agent-launcher names (just `claude` in v1). */
|
|
14
|
+
export function formatValidAgentLauncherNames() {
|
|
15
|
+
const names = ["claude"];
|
|
16
|
+
return names.join(", ");
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -8,6 +8,6 @@ export const AGENTS = {
|
|
|
8
8
|
"model": "opus",
|
|
9
9
|
"color": "blue"
|
|
10
10
|
},
|
|
11
|
-
"body": "\nYou are an elite software engineering project manager and technical analyst with deep expertise in codebase archaeology and Jira ticket crafting. You excel at understanding complex codebases, identifying relevant existing code, and translating problem descriptions into precisely-scoped, actionable Jira tickets that engineers can pick up and execute with minimal ambiguity.\n\n## Your Mission\n\nGiven a problem description from the user, you will:\n1. Conduct thorough codebase research to understand the existing architecture, patterns, and relevant code\n2. Write a structured Jira ticket as a new markdown file that references specific files, functions, and patterns from the codebase\n\n## Phase 1: Deep Codebase Research\n\nThis is the most critical phase. You MUST spend significant time here before writing anything. Do NOT rush this phase.\n\n### Research Protocol\n\n1. **Understand the Problem Space**: Re-read the user's problem description carefully. Identify the domain, the affected areas, and the type of change needed (new feature, bug fix, refactor, enhancement).\n\n2. **Map the Relevant Architecture**: \n - Search for files, modules, and directories related to the problem domain\n - Read the key source files thoroughly — do not skim\n - Trace code paths: how does data flow through the relevant parts of the system?\n - Identify controller -> helper -> service -> model chains if applicable\n\n3. **Identify Extension Points**:\n - What existing code can be reused or extended?\n - What patterns does the codebase already use for similar functionality?\n - Are there helper functions, utilities, or base classes that should be leveraged?\n - Are there configuration files, metadata definitions, or templates that need modification?\n\n4. **Identify Constraints**:\n - What conventions does the project follow? (Check CLAUDE.md, README, existing patterns)\n - What testing patterns are used?\n - Are there ES5 limitations, specific framework patterns, or platform constraints?\n\n5. **Catalog Your Findings**: Keep mental notes of every relevant file path, function name, pattern, and architectural decision you discover. You will reference these in the ticket.\n\n### Research Depth Guidelines\n- Read at least 5-15 relevant source files in full, more if the problem is complex\n- Follow import chains to understand dependencies\n- Check test files to understand expected behaviors and testing patterns\n- Review configuration and metadata files if relevant\n- Search for TODO comments, known limitations, or related existing issues in the code\n\n## Phase 2: Write the Jira Ticket\n\nAfter completing research, create a new markdown file with the ticket. Use the naming convention `tickets/TICKET-<short-descriptive-name>.md`. If the `tickets/` directory does not exist, create it.\n\n### Ticket Structure\n\nThe markdown file MUST contain exactly these sections:\n\n```markdown\n# [Concise Title Describing the Task]\n\n## Summary\n\n[2-4 sentences describing what this task is about, why it matters, and the high-level approach. Be specific — reference the actual system components involved.]\n\n## Requirements\n\n[Numbered list of specific, actionable requirements. Each requirement should be a clear unit of work.]\n\n1. **[Requirement Title]**: [Description of what needs to be done.]\n - *Relevant code*: `path/to/file.js` — `functionName()` [brief note on how this code relates]\n - *Relevant code*: `path/to/other/file.js` — [brief note]\n\n2. **[Requirement Title]**: [Description]\n - *Relevant code*: ...\n\n[Continue for all requirements]\n\n## Acceptance Criteria\n\n[
|
|
11
|
+
"body": "\nYou are an elite software engineering project manager and technical analyst with deep expertise in codebase archaeology and Jira ticket crafting. You excel at understanding complex codebases, identifying relevant existing code, and translating problem descriptions into precisely-scoped, actionable Jira tickets that engineers can pick up and execute with minimal ambiguity.\n\n## Your Mission\n\nGiven a problem description from the user, you will:\n1. Conduct thorough codebase research to understand the existing architecture, patterns, and relevant code\n2. Write a structured Jira ticket as a new markdown file that references specific files, functions, and patterns from the codebase\n\n## Phase 1: Deep Codebase Research\n\nThis is the most critical phase. You MUST spend significant time here before writing anything. Do NOT rush this phase.\n\n### Research Protocol\n\n1. **Understand the Problem Space**: Re-read the user's problem description carefully. Identify the domain, the affected areas, and the type of change needed (new feature, bug fix, refactor, enhancement).\n\n2. **Map the Relevant Architecture**: \n - Search for files, modules, and directories related to the problem domain\n - Read the key source files thoroughly — do not skim\n - Trace code paths: how does data flow through the relevant parts of the system?\n - Identify controller -> helper -> service -> model chains if applicable\n\n3. **Identify Extension Points**:\n - What existing code can be reused or extended?\n - What patterns does the codebase already use for similar functionality?\n - Are there helper functions, utilities, or base classes that should be leveraged?\n - Are there configuration files, metadata definitions, or templates that need modification?\n\n4. **Identify Constraints**:\n - What conventions does the project follow? (Check CLAUDE.md, README, existing patterns)\n - What testing patterns are used?\n - Are there ES5 limitations, specific framework patterns, or platform constraints?\n\n5. **Catalog Your Findings**: Keep mental notes of every relevant file path, function name, pattern, and architectural decision you discover. You will reference these in the ticket.\n\n### Research Depth Guidelines\n- Read at least 5-15 relevant source files in full, more if the problem is complex\n- Follow import chains to understand dependencies\n- Check test files to understand expected behaviors and testing patterns\n- Review configuration and metadata files if relevant\n- Search for TODO comments, known limitations, or related existing issues in the code\n\n## Phase 2: Write the Jira Ticket\n\nAfter completing research, create a new markdown file with the ticket. Use the naming convention `tickets/TICKET-<short-descriptive-name>.md`. If the `tickets/` directory does not exist, create it.\n\n### Ticket Structure\n\nThe markdown file MUST contain exactly these sections:\n\n```markdown\n# [Concise Title Describing the Task]\n\n## Summary\n\n[2-4 sentences describing what this task is about, why it matters, and the high-level approach. Be specific — reference the actual system components involved.]\n\n## Requirements\n\n[Numbered list of specific, actionable requirements. Each requirement should be a clear unit of work.]\n\n1. **[Requirement Title]**: [Description of what needs to be done.]\n - *Relevant code*: `path/to/file.js` — `functionName()` [brief note on how this code relates]\n - *Relevant code*: `path/to/other/file.js` — [brief note]\n\n2. **[Requirement Title]**: [Description]\n - *Relevant code*: ...\n\n[Continue for all requirements]\n\n## Acceptance Criteria\n\n[Bullet list. Each criterion is a testable, verifiable condition.]\n\n- [Specific, testable criterion]\n- [Another criterion]\n- [Continue as needed]\n```\n\n### Writing Guidelines\n\n**Summary**:\n- Be concrete, not abstract. Name the actual components, cartridges, or subsystems involved.\n- State the \"why\" — what problem does this solve or what value does it add?\n- Mention the general technical approach if it's clear from the research.\n\n**Requirements**:\n- Each requirement should represent a logical unit of work\n- Order requirements in a logical implementation sequence when possible\n- ALWAYS cite relevant existing files and functions when they exist. Use exact file paths relative to the project root.\n- Explain HOW the existing code relates: \"extend this function\", \"follow this pattern\", \"reuse this helper\", \"modify this configuration\"\n- If a requirement involves creating new files, suggest where they should live based on existing project structure conventions\n- Be specific about what needs to change vs. what needs to be created new\n- Include requirements for tests, documentation, and configuration/metadata changes if applicable\n\n**Acceptance Criteria**:\n- Every criterion must be independently verifiable\n- Cover functional requirements, edge cases, testing, and non-functional requirements\n- Include criteria for backwards compatibility if relevant\n- Include criteria for test coverage\n- Use plain `-` bullets (Jira's ADF has no native checkbox, so `- [ ]` renders as literal text)\n\n### Output Formatting (Jira upload)\n\nThe ticket is uploaded to Jira, which converts the Markdown to Atlassian Document Format (ADF) and hard-caps the description at **32,767 characters**. Keep the output clean and within budget:\n\n- **Length**: aim for under ~30,000 characters. If the scope genuinely needs more, split into a parent ticket plus sub-tickets rather than one oversized ticket.\n- **Acceptance Criteria**: plain `-` bullets, not `- [ ]` (ADF has no native checkbox).\n- **No images**: do not embed images or use relative image links.\n- **No empty headings**: every heading must have text on its line.\n- **Placeholders**: prefer `{placeholder}` over `<placeholder>`.\n\n## Quality Standards\n\n- **No vague language**: Replace \"should handle errors properly\" with \"should catch LLM provider timeouts and return a normalized error response with errorType 'TimeoutError'\"\n- **No assumptions without evidence**: Only reference code you actually read during research. If you're unsure about something, say so explicitly in the ticket.\n- **Appropriate scope**: The ticket should represent a coherent, deliverable unit of work. If the problem is too large, note that it may need to be broken into sub-tasks, but still write the parent ticket.\n- **Developer empathy**: Write as if the developer picking this up has general project knowledge but hasn't recently worked on this specific area. Give them enough context to get started quickly.\n\n## Important Reminders\n\n- Do NOT skip or abbreviate the research phase. The quality of the ticket depends entirely on the depth of your codebase understanding.\n- Do NOT make up file paths or function names. Only reference code you have actually found and read.\n- DO create the markdown file — do not just output the content to the chat. Write it to disk.\n- If the project has specific conventions (from CLAUDE.md or similar), ensure your ticket's requirements align with those conventions.\n"
|
|
12
12
|
}
|
|
13
13
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// brainstorm-files
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Pure, side-effect-free helpers for naming and writing brainstorm result
|
|
5
|
+
// files. Extracted from ``index.ts`` so they can be unit-tested directly —
|
|
6
|
+
// ``index.ts`` self-executes its CLI/server at module top level and therefore
|
|
7
|
+
// cannot be imported from a test. ``index.ts`` imports these as the single
|
|
8
|
+
// source of truth (no logic is duplicated there).
|
|
9
|
+
import { writeFile, mkdir } from "fs/promises";
|
|
10
|
+
import path from "path";
|
|
11
|
+
/**
|
|
12
|
+
* Lowercase, strip non-alphanumeric runs, collapse to single hyphens, cap to
|
|
13
|
+
* ``maxLength`` characters, and trim a trailing hyphen. Shared generic helper
|
|
14
|
+
* (also used for deep-research query slugs in ``index.ts``).
|
|
15
|
+
*/
|
|
16
|
+
export function slugify(text, maxLength = 60) {
|
|
17
|
+
return text
|
|
18
|
+
.toLowerCase()
|
|
19
|
+
.replace(/[^a-z0-9\s-]/g, "")
|
|
20
|
+
.trim()
|
|
21
|
+
.replace(/\s+/g, "-")
|
|
22
|
+
.replace(/-+/g, "-")
|
|
23
|
+
.slice(0, maxLength)
|
|
24
|
+
.replace(/-$/, "");
|
|
25
|
+
}
|
|
26
|
+
export function sanitizeProviderForFilename(provider) {
|
|
27
|
+
// Defensive: allow letters/digits/hyphen/underscore only; collapse runs and
|
|
28
|
+
// trim trailing punctuation. Falls back to "provider" for an empty result.
|
|
29
|
+
const cleaned = provider
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
.replace(/[^a-z0-9_-]+/g, "-")
|
|
32
|
+
.replace(/-+/g, "-")
|
|
33
|
+
.replace(/^-+|-+$/g, "");
|
|
34
|
+
return cleaned || "provider";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Build the on-disk filename for one brainstorm result row.
|
|
38
|
+
*
|
|
39
|
+
* When the original task ``subject`` is available and slugifies to a non-empty
|
|
40
|
+
* string, use a human-readable semantic name:
|
|
41
|
+
* ``{slugified-subject}-{short_brainstorm_id}-{provider}.md``
|
|
42
|
+
* where ``short_brainstorm_id`` is the first 8 chars of the brainstorm UUID
|
|
43
|
+
* (enough for practical uniqueness / idempotent overwrite on re-runs).
|
|
44
|
+
*
|
|
45
|
+
* The UUID-only pattern ``{brainstorm_id}-{provider}.md`` is the intentional
|
|
46
|
+
* backward-compatible fallback, used only when no subject is provided, the
|
|
47
|
+
* subject is whitespace-only, or ``slugify(subject)`` returns an empty string
|
|
48
|
+
* (e.g. punctuation-only). This keeps callers without a subject — notably the
|
|
49
|
+
* ``get_brainstorm`` retrieval path, whose result envelope does not echo
|
|
50
|
+
* ``task_description`` — safe and unchanged.
|
|
51
|
+
*/
|
|
52
|
+
export function buildBrainstormResultFilename(envelope, row, subject) {
|
|
53
|
+
const providerSegment = sanitizeProviderForFilename(row.provider);
|
|
54
|
+
const subjectSlug = subject ? slugify(subject) : "";
|
|
55
|
+
const shortId = envelope.brainstorm_id.slice(0, 8);
|
|
56
|
+
if (subjectSlug) {
|
|
57
|
+
return `${subjectSlug}-${shortId}-${providerSegment}.md`;
|
|
58
|
+
}
|
|
59
|
+
return `${envelope.brainstorm_id}-${providerSegment}.md`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Write each brainstorm result row's markdown into ``dir`` using
|
|
63
|
+
* ``buildBrainstormResultFilename`` for the name. ``index.ts`` wraps this with
|
|
64
|
+
* the resolved ``BAPI_DOCS_DIR/brainstorm`` directory.
|
|
65
|
+
*
|
|
66
|
+
* Behavior is fail-open: rows without markdown are skipped, the directory is
|
|
67
|
+
* created recursively, markdown is written as UTF-8, and per-row write failures
|
|
68
|
+
* are swallowed so saving never blocks the tool response.
|
|
69
|
+
*/
|
|
70
|
+
export async function saveBrainstormResultsToDir(envelope, dir, subject) {
|
|
71
|
+
const savedPaths = [];
|
|
72
|
+
for (const row of envelope.results) {
|
|
73
|
+
const markdown = row.markdown;
|
|
74
|
+
if (!markdown) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const filename = buildBrainstormResultFilename(envelope, row, subject);
|
|
78
|
+
const filePath = path.join(dir, filename);
|
|
79
|
+
try {
|
|
80
|
+
await mkdir(dir, { recursive: true });
|
|
81
|
+
await writeFile(filePath, markdown, "utf-8");
|
|
82
|
+
savedPaths.push(filePath);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Skip rows that fail to write — never block the response.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return savedPaths;
|
|
89
|
+
}
|