@crewhaus/target-cf-worker-workflow 0.1.1 → 0.1.3
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/package.json +9 -13
- package/src/index.test.ts +46 -0
- package/src/index.ts +30 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/target-cf-worker-workflow",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Codegen backend for the workflow target emitted as a Cloudflare Worker. Sibling of target-cf-worker-cli; reads the same IrWorkflowV0 variant, emits a self-contained Worker bundle that runs the steps sequentially.",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -12,15 +12,16 @@
|
|
|
12
12
|
"test": "bun test src"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@crewhaus/errors": "0.1.
|
|
16
|
-
"@crewhaus/infra-utils": "0.1.
|
|
17
|
-
"@crewhaus/ir": "0.1.
|
|
15
|
+
"@crewhaus/errors": "0.1.3",
|
|
16
|
+
"@crewhaus/infra-utils": "0.1.3",
|
|
17
|
+
"@crewhaus/ir": "0.1.3",
|
|
18
|
+
"@crewhaus/model-router": "0.1.3"
|
|
18
19
|
},
|
|
19
20
|
"license": "Apache-2.0",
|
|
20
21
|
"author": {
|
|
21
22
|
"name": "Max Meier",
|
|
22
|
-
"email": "max@
|
|
23
|
-
"url": "https://
|
|
23
|
+
"email": "max@crewhaus.ai",
|
|
24
|
+
"url": "https://crewhaus.ai"
|
|
24
25
|
},
|
|
25
26
|
"repository": {
|
|
26
27
|
"type": "git",
|
|
@@ -32,12 +33,7 @@
|
|
|
32
33
|
"url": "https://github.com/crewhaus/factory/issues"
|
|
33
34
|
},
|
|
34
35
|
"publishConfig": {
|
|
35
|
-
"access": "
|
|
36
|
+
"access": "public"
|
|
36
37
|
},
|
|
37
|
-
"files": [
|
|
38
|
-
"src",
|
|
39
|
-
"README.md",
|
|
40
|
-
"LICENSE",
|
|
41
|
-
"NOTICE"
|
|
42
|
-
]
|
|
38
|
+
"files": ["src", "README.md", "LICENSE", "NOTICE"]
|
|
43
39
|
}
|
package/src/index.test.ts
CHANGED
|
@@ -117,3 +117,49 @@ describe("emitCfWorkerWorkflow", () => {
|
|
|
117
117
|
expect(() => parseJs(worker?.content ?? "")).not.toThrow();
|
|
118
118
|
});
|
|
119
119
|
});
|
|
120
|
+
|
|
121
|
+
describe("emitCfWorkerWorkflow — package.json name injection (#148)", () => {
|
|
122
|
+
test("package.json sanitizes the spec name and resists JSON injection", () => {
|
|
123
|
+
const ir: IrWorkflowV0 = {
|
|
124
|
+
...baseIr,
|
|
125
|
+
name: '", "dependencies": { "evil-typosquat": "1.0.0" }, "x": "',
|
|
126
|
+
};
|
|
127
|
+
const pkg = emitCfWorkerWorkflow(ir).files.find((f) => f.path === "package.json");
|
|
128
|
+
expect(pkg).toBeDefined();
|
|
129
|
+
const parsed = JSON.parse(pkg?.content ?? "") as {
|
|
130
|
+
name: string;
|
|
131
|
+
dependencies?: Record<string, string>;
|
|
132
|
+
};
|
|
133
|
+
expect(parsed.name).not.toContain('"'); // sanitized to [a-z0-9-]
|
|
134
|
+
expect(parsed.dependencies).toBeUndefined(); // injection did not break out
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("emitCfWorkerWorkflow — provider gate", () => {
|
|
139
|
+
test("a workflow with one openai/ step fails at compile time, naming the step", () => {
|
|
140
|
+
const ir: IrWorkflowV0 = {
|
|
141
|
+
...baseIr,
|
|
142
|
+
steps: [step({ name: "research" }), step({ name: "draft", model: "openai/gpt-4o-mini" })],
|
|
143
|
+
};
|
|
144
|
+
expect(() => emitCfWorkerWorkflow(ir)).toThrow(TargetEmitError);
|
|
145
|
+
expect(() => emitCfWorkerWorkflow(ir)).toThrow(
|
|
146
|
+
/cf-worker targets currently support claude-\* models only — use the cli target for other providers/,
|
|
147
|
+
);
|
|
148
|
+
expect(() => emitCfWorkerWorkflow(ir)).toThrow(/step "draft"/);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("gemini/, bedrock/, and local/ step models are all rejected", () => {
|
|
152
|
+
for (const model of [
|
|
153
|
+
"gemini/gemini-2.5-flash",
|
|
154
|
+
"bedrock/us.anthropic.claude-sonnet-4-5-20250929-v1:0",
|
|
155
|
+
"local/llama3.2@http://localhost:11434/v1",
|
|
156
|
+
]) {
|
|
157
|
+
const ir: IrWorkflowV0 = { ...baseIr, steps: [step({ model })] };
|
|
158
|
+
expect(() => emitCfWorkerWorkflow(ir)).toThrow(TargetEmitError);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("all-claude steps still emit cleanly", () => {
|
|
163
|
+
expect(emitCfWorkerWorkflow(baseIr).files.length).toBe(3);
|
|
164
|
+
});
|
|
165
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CrewhausError } from "@crewhaus/errors";
|
|
2
2
|
import { escapeJsonString } from "@crewhaus/infra-utils";
|
|
3
3
|
import type { Bundle, IrWorkflowV0 } from "@crewhaus/ir";
|
|
4
|
+
import { parseModelString } from "@crewhaus/model-router";
|
|
4
5
|
|
|
5
6
|
export type EmitOptions = {
|
|
6
7
|
/**
|
|
@@ -12,7 +13,7 @@ export type EmitOptions = {
|
|
|
12
13
|
readonly allowedOrigins?: readonly string[];
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
const DEFAULT_ALLOWED_ORIGINS = ["https://studio.crewhaus.
|
|
16
|
+
const DEFAULT_ALLOWED_ORIGINS = ["https://studio.crewhaus.ai", "http://localhost:4322"];
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Emit a Cloudflare Worker bundle for a WORKFLOW-target IR. Workflow sibling
|
|
@@ -43,6 +44,10 @@ export function emitCfWorkerWorkflow(ir: IrWorkflowV0, opts: EmitOptions = {}):
|
|
|
43
44
|
);
|
|
44
45
|
}
|
|
45
46
|
for (const step of ir.steps) {
|
|
47
|
+
// Provider gate — the emitted worker inlines an api.anthropic.com client
|
|
48
|
+
// and POSTs each step's model verbatim, so a non-Anthropic model string
|
|
49
|
+
// would compile cleanly and 502 at runtime. Fail at compile time instead.
|
|
50
|
+
assertAnthropicModel(step.model, `step "${step.name}"`);
|
|
46
51
|
if (step.tools.length > 0) {
|
|
47
52
|
throw new TargetEmitError(
|
|
48
53
|
`cf-worker-workflow does not yet support tools (step "${step.name}"); use the local workflow target`,
|
|
@@ -69,6 +74,25 @@ export class TargetEmitError extends CrewhausError {
|
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Reject any model string the router would dispatch to a non-Anthropic
|
|
79
|
+
* provider (openai/, gemini/, bedrock/, local/…) — the inlined worker
|
|
80
|
+
* client only speaks the Anthropic Messages API.
|
|
81
|
+
*/
|
|
82
|
+
function assertAnthropicModel(model: string, where: string): void {
|
|
83
|
+
let providerId: string;
|
|
84
|
+
try {
|
|
85
|
+
providerId = parseModelString(model).providerId;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
throw new TargetEmitError(`${where} model "${model}": ${(err as Error).message}`, err);
|
|
88
|
+
}
|
|
89
|
+
if (providerId !== "anthropic") {
|
|
90
|
+
throw new TargetEmitError(
|
|
91
|
+
`${where} model "${model}" routes to provider "${providerId}" — cf-worker targets currently support claude-* models only — use the cli target for other providers`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
72
96
|
function renderWorker(ir: IrWorkflowV0, allowedOrigins: readonly string[]): string {
|
|
73
97
|
// escapeJsonString === JSON.stringify: each of these is a COMPLETE,
|
|
74
98
|
// quoted JS/JSON string literal already. Do NOT wrap them in extra quotes
|
|
@@ -339,8 +363,12 @@ enabled = true
|
|
|
339
363
|
}
|
|
340
364
|
|
|
341
365
|
function renderPackageJson(ir: IrWorkflowV0): string {
|
|
366
|
+
// Sanitize like the wrangler name. A raw ir.name breaks out of the JSON
|
|
367
|
+
// string (this manifest is reachable from the hosted compiler-worker HTTP
|
|
368
|
+
// endpoint), allowing dependency/script injection into package.json (#148).
|
|
369
|
+
const safeName = ir.name.replace(/[^a-z0-9-]/gi, "-").toLowerCase();
|
|
342
370
|
return `{
|
|
343
|
-
"name": "${
|
|
371
|
+
"name": "${safeName}",
|
|
344
372
|
"version": "0.0.0",
|
|
345
373
|
"private": true,
|
|
346
374
|
"type": "module",
|