-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefine_hook.ts
More file actions
85 lines (73 loc) · 2.69 KB
/
Copy pathrefine_hook.ts
File metadata and controls
85 lines (73 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function run() {
const input = JSON.parse(fs.readFileSync(0, "utf-8"));
const prompt = input.prompt;
const answers = input.answers;
// Connect to the local MCP Server via STDIO using absolute path
const serverPath = "C:/repo/Promptimprover/universal-refiner/dist/src/index.js";
const transport = new StdioClientTransport({
command: "node",
args: [serverPath],
});
const client = new Client({ name: "refine-hook", version: "1.0.0" }, { capabilities: {} });
await client.connect(transport);
if (!answers) {
// PASS 1: Lint the prompt
const lintResult = await client.request(
{ method: "tools/call", params: { name: "lint_prompt", arguments: { prompt } } },
CallToolResultSchema
);
const firstContent = lintResult.content[0];
if (firstContent.type !== "text") throw new Error("Expected text content");
const { gaps } = JSON.parse(firstContent.text);
if (gaps && gaps.length > 0) {
// Create questions
const questionResult = await client.request(
{ method: "tools/call", params: { name: "create_questions", arguments: { gaps } } },
CallToolResultSchema
);
const firstQContent = questionResult.content[0];
if (firstQContent.type !== "text") throw new Error("Expected text content");
const questions = JSON.parse(firstQContent.text);
console.log(JSON.stringify({
decision: "ask_user",
questions: questions
}));
process.exit(0);
}
} else {
// PASS 2: We have answers, finalize the prompt
const finalResult = await client.request(
{
method: "tools/call",
params: {
name: "finalize_prompt",
arguments: { original_prompt: prompt, answers }
}
},
CallToolResultSchema
);
const firstFinalContent = finalResult.content[0];
if (firstFinalContent.type !== "text") throw new Error("Expected text content");
const refinedPrompt = firstFinalContent.text;
console.log(JSON.stringify({
prompt: refinedPrompt
}));
process.exit(0);
}
// No refinement needed or error
console.log(JSON.stringify({ decision: "allow" }));
process.exit(0);
}
run().catch((err) => {
console.error(err);
console.log(JSON.stringify({ decision: "allow" })); // Fail-open
process.exit(0);
});