@executioncontrolprotocol/extension-openai 0.10.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/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +81 -0
- package/dist/index.js.map +1 -0
- package/dist/resolve-api-key.d.ts +3 -0
- package/dist/resolve-api-key.d.ts.map +1 -0
- package/dist/resolve-api-key.js +11 -0
- package/dist/resolve-api-key.js.map +1 -0
- package/package.json +33 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Registry } from "@executioncontrolprotocol/core";
|
|
2
|
+
/** @executioncontrolprotocol/openai extension. @category Extensions */
|
|
3
|
+
export declare const openaiExtension: import("@executioncontrolprotocol/core").ExtensionDefinition;
|
|
4
|
+
/** Register @executioncontrolprotocol/openai. */
|
|
5
|
+
export declare function registerOpenaiExtension(registry?: Registry): Promise<void>;
|
|
6
|
+
export default openaiExtension;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoE,KAAK,QAAQ,EAAE,MAAM,gCAAgC,CAAA;AAqChI,uEAAuE;AACvE,eAAO,MAAM,eAAe,8DA+ClB,CAAA;AAIV,iDAAiD;AACjD,wBAAsB,uBAAuB,CAAC,QAAQ,GAAE,QAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAIhG;AAED,eAAe,eAAe,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { defineExtension, capabilityFor, globalRegistry, catalogExtension } from "@executioncontrolprotocol/core";
|
|
2
|
+
import { modelGenerateInputSchema, modelGenerateOutputSchema } from "@executioncontrolprotocol/types";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { resolveOpenaiApiKey } from "./resolve-api-key.js";
|
|
5
|
+
async function chatComplete(apiKey, model, prompt, system, context) {
|
|
6
|
+
const body = {
|
|
7
|
+
model,
|
|
8
|
+
messages: [
|
|
9
|
+
...(system ? [{ role: "system", content: system }] : []),
|
|
10
|
+
...(context
|
|
11
|
+
? [{ role: "system", content: JSON.stringify(context) }]
|
|
12
|
+
: []),
|
|
13
|
+
{ role: "user", content: prompt },
|
|
14
|
+
],
|
|
15
|
+
};
|
|
16
|
+
const res = await fetch("https://api.openai.com/v1/chat/completions", {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: {
|
|
19
|
+
Authorization: `Bearer ${apiKey}`,
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
},
|
|
22
|
+
body: JSON.stringify(body),
|
|
23
|
+
});
|
|
24
|
+
if (!res.ok)
|
|
25
|
+
throw new Error(`OpenAI API error: ${res.status}`);
|
|
26
|
+
const data = (await res.json());
|
|
27
|
+
return data.choices[0]?.message?.content ?? "";
|
|
28
|
+
}
|
|
29
|
+
/** @executioncontrolprotocol/openai extension. @category Extensions */
|
|
30
|
+
export const openaiExtension = defineExtension("@executioncontrolprotocol", "openai")
|
|
31
|
+
.withConfig({
|
|
32
|
+
apiKey: z.string().optional(),
|
|
33
|
+
defaultModel: z.string().optional(),
|
|
34
|
+
})
|
|
35
|
+
.withCapabilities([
|
|
36
|
+
capabilityFor("@executioncontrolprotocol/openai", "generate")
|
|
37
|
+
.withInput(modelGenerateInputSchema)
|
|
38
|
+
.withOutput(modelGenerateOutputSchema)
|
|
39
|
+
.withHandler(async (input, ctx) => {
|
|
40
|
+
const parsed = modelGenerateInputSchema.parse(input);
|
|
41
|
+
const cfg = ctx.extensionConfig ?? {};
|
|
42
|
+
const apiKey = resolveOpenaiApiKey(cfg);
|
|
43
|
+
if (!apiKey)
|
|
44
|
+
throw new Error("OpenAI API key required");
|
|
45
|
+
const model = parsed.model ?? cfg.defaultModel ?? "gpt-4o-mini";
|
|
46
|
+
ctx.usage.increment({ modelCalls: 1 });
|
|
47
|
+
const text = await chatComplete(apiKey, model, parsed.prompt, parsed.system, parsed.context);
|
|
48
|
+
return { text };
|
|
49
|
+
}),
|
|
50
|
+
capabilityFor("@executioncontrolprotocol/openai", "evaluate")
|
|
51
|
+
.withInput(z.object({
|
|
52
|
+
artifact: z.unknown(),
|
|
53
|
+
criteria: z.unknown().optional(),
|
|
54
|
+
goal: z.string().optional(),
|
|
55
|
+
}))
|
|
56
|
+
.withOutput(z.object({ approved: z.boolean(), feedback: z.string().optional() }))
|
|
57
|
+
.withHandler(async (input, ctx) => {
|
|
58
|
+
const prompt = `Evaluate: ${input.goal ?? "quality check"}. Reply JSON {approved:boolean,feedback:string}`;
|
|
59
|
+
const cfg = ctx.extensionConfig ?? {};
|
|
60
|
+
const apiKey = resolveOpenaiApiKey(cfg);
|
|
61
|
+
if (!apiKey)
|
|
62
|
+
return { approved: true, feedback: "skipped (no API key)" };
|
|
63
|
+
const content = await chatComplete(apiKey, "gpt-4o-mini", prompt, undefined, input);
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(content);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return { approved: true, feedback: content };
|
|
69
|
+
}
|
|
70
|
+
}),
|
|
71
|
+
])
|
|
72
|
+
.build();
|
|
73
|
+
catalogExtension(openaiExtension);
|
|
74
|
+
/** Register @executioncontrolprotocol/openai. */
|
|
75
|
+
export async function registerOpenaiExtension(registry = globalRegistry) {
|
|
76
|
+
if (!registry.getExtension("@executioncontrolprotocol/openai")) {
|
|
77
|
+
await registry.registerExtension(openaiExtension);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export default openaiExtension;
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAiB,MAA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e,eAAe,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-api-key.d.ts","sourceRoot":"","sources":["../src/resolve-api-key.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAOxE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Resolve OpenAI API key from extension config, optionally process.env on Node. */
|
|
2
|
+
export function resolveOpenaiApiKey(cfg) {
|
|
3
|
+
const fromConfig = cfg.apiKey;
|
|
4
|
+
if (fromConfig)
|
|
5
|
+
return fromConfig;
|
|
6
|
+
if (typeof process !== "undefined" && process.env?.OPENAI_API_KEY) {
|
|
7
|
+
return process.env.OPENAI_API_KEY;
|
|
8
|
+
}
|
|
9
|
+
return "";
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=resolve-api-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-api-key.js","sourceRoot":"","sources":["../src/resolve-api-key.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,UAAU,mBAAmB,CAAC,GAA4B;IAC9D,MAAM,UAAU,GAAG,GAAG,CAAC,MAA4B,CAAA;IACnD,IAAI,UAAU;QAAE,OAAO,UAAU,CAAA;IACjC,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,CAAC,GAAG,EAAE,cAAc,EAAE,CAAC;QAClE,OAAO,OAAO,CAAC,GAAG,CAAC,cAAc,CAAA;IACnC,CAAC;IACD,OAAO,EAAE,CAAA;AACX,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@executioncontrolprotocol/extension-openai",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "OpenAI model provider extension for ECP",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc -b"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@executioncontrolprotocol/core": "^0.10.0",
|
|
24
|
+
"@executioncontrolprotocol/types": "^0.10.0",
|
|
25
|
+
"zod": "^3.24.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@executioncontrolprotocol/core": "0.10.0",
|
|
29
|
+
"@executioncontrolprotocol/types": "0.10.0",
|
|
30
|
+
"typescript": "^5.7.0",
|
|
31
|
+
"zod": "^3.24.0"
|
|
32
|
+
}
|
|
33
|
+
}
|