experiment(confidence-engine): add openai provider apparatus

This commit is contained in:
2026-09-06 07:50:47 +01:00
parent 653934559c
commit 860ee6fc5b
3 changed files with 133 additions and 1 deletions
+61
View File
@@ -12,6 +12,14 @@ export function getProvider() {
return new OllamaLlmProvider();
}
/**
* Experiment-only construction seam. Production provider selection remains Ollama.
* @param {{ apiKey?: string, fetchImpl?: typeof fetch }} [options]
*/
export function createOpenAIReconstructionProvider(options = {}) {
return new OpenAIReconstructionProvider(options);
}
function recoverJson(raw) {
if (typeof raw !== "string") return raw;
const trimmed = raw.trim();
@@ -294,3 +302,56 @@ class OllamaLlmProvider {
}
}
}
class OpenAIReconstructionProvider {
constructor({ apiKey = process.env.OPENAI_API_KEY, fetchImpl = fetch } = {}) {
this.apiKey = apiKey;
this.fetchImpl = fetchImpl;
}
async generateReconstruction(prompt, modelName = "gpt-5.6-terra") {
if (!this.apiKey) throw new Error("OPENAI_API_KEY is not set");
const response = await this.fetchImpl("https://api.openai.com/v1/responses", {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: modelName,
input: prompt,
text: {
format: {
type: "json_schema",
name: "reconstruction",
strict: true,
schema: reconstructionJsonSchema,
},
},
}),
});
if (!response.ok) {
const body = await response.text();
const error = new Error(
`OpenAI Responses API returned ${response.status}: ${body}`,
);
error.providerApiPath = "/v1/responses";
throw error;
}
const data = await response.json();
const outputText = data.output_text;
if (typeof outputText !== "string") {
const error = new Error("OpenAI Responses API returned no output_text");
error.providerApiPath = "/v1/responses";
throw error;
}
return {
response: recoverJson(outputText),
providerApiPath: "/v1/responses",
};
}
}