Repository navigation
Expand file tree
/
Copy pathplanner.ts
More file actions
223 lines (202 loc) · 6.95 KB
/
Copy pathplanner.ts
File metadata and controls
223 lines (202 loc) · 6.95 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import {
AD_RUN_LIMITS,
decodeAdRunPlan,
type AdBrief,
type AdRunPlan,
type PlannedConcept,
type Product,
} from "@/lib/ad-run";
import {
normalizeBrandColorHex,
type BrandColor,
} from "@/lib/brand-color";
import {
fetchBrand,
fetchProducts,
fetchStyleguide,
hasContextConfiguration,
scrapePage,
type BrandProfile,
type StyleguideProfile,
} from "@/lib/context";
import { normalizeFontFamily } from "@/lib/font-family";
import { normalizeDomain } from "@/lib/net";
import { parsePublicHttpUrl } from "@/lib/public-url";
import { deriveSummaryFromMarkdown, pickConceptsAndCraftCopy } from "./brief";
import { pickBrandColors } from "./colors";
import { gatewayTextModel, hasGatewayConfiguration } from "./gateway";
const DEFAULT_MOOD = "modern, confident, premium";
export type AdRunPlanningFailure =
| { code: "invalid-domain" }
| { code: "not-configured" }
| { code: "aborted" }
| { code: "domain-unreachable"; cause: unknown }
| { code: "brand-unavailable"; cause: unknown }
| { code: "products-unavailable"; cause: unknown }
| { code: "internal"; cause: unknown };
export type AdRunPlanningResult =
| { ok: true; value: AdRunPlan }
| { ok: false; error: AdRunPlanningFailure };
/**
* Behavior-level adapters used by the Ad Run planning module. External SDK
* objects and environment-variable names stay behind this seam.
*/
export type BrandResearchAdapter = {
isReady(): boolean;
fetchBrand(domain: string, signal?: AbortSignal): Promise<BrandProfile>;
scrapePage(url: string, signal?: AbortSignal): Promise<string>;
fetchStyleguide(
domain: string,
signal?: AbortSignal,
): Promise<StyleguideProfile>;
fetchProducts(domain: string, signal?: AbortSignal): Promise<Product[]>;
};
export type ConceptPlanningAdapter = {
isReady(): boolean;
planConcepts(
brief: AdBrief,
products: readonly Product[],
signal?: AbortSignal,
): Promise<readonly PlannedConcept[]>;
};
export type AdRunPlannerAdapters = {
research: BrandResearchAdapter;
concepts: ConceptPlanningAdapter;
};
export type AdRunPlanner = (
rawDomain: string,
signal?: AbortSignal,
) => Promise<AdRunPlanningResult>;
const bounded = (value: string | null | undefined, max: number): string =>
(value ?? "").trim().slice(0, max);
function boundedColors(colors: readonly BrandColor[]): BrandColor[] {
return colors
.flatMap((color) => {
const hex = normalizeBrandColorHex(color.hex);
if (!hex) return [];
const name = bounded(color.name, AD_RUN_LIMITS.colorName);
return [{ hex, name: name || null }];
})
.slice(0, AD_RUN_LIMITS.palette);
}
function boundedLogoUrl(value: string | null): string | null {
const candidate = (value ?? "").trim();
if (!candidate || candidate.length > AD_RUN_LIMITS.logoUrl) return null;
return parsePublicHttpUrl(candidate)?.href ?? null;
}
function buildBrief(
domain: string,
brand: BrandProfile,
markdown: string,
styleguide: StyleguideProfile,
): AdBrief {
const colors = boundedColors(styleguide.colors);
const { a: colorA, b: colorB } = pickBrandColors(colors);
const brandName = bounded(brand.name, AD_RUN_LIMITS.brandName) || domain;
const description =
bounded(brand.description, AD_RUN_LIMITS.description) ||
bounded(brand.slogan, AD_RUN_LIMITS.description);
return {
domain,
brandName,
description,
industry: bounded(brand.industry, AD_RUN_LIMITS.industry),
summary: deriveSummaryFromMarkdown(markdown),
mood: bounded(styleguide.mood, AD_RUN_LIMITS.mood) || DEFAULT_MOOD,
fontFamily: normalizeFontFamily(styleguide.fontFamily),
colorA: bounded(colorA, AD_RUN_LIMITS.promptColor),
colorB: bounded(colorB, AD_RUN_LIMITS.promptColor),
logoUrl: boundedLogoUrl(brand.logoUrl),
colors,
};
}
/**
* Domain -> Brief -> three Company concepts plus one to three Product concepts.
* The returned function is the whole test surface; partial-failure policy and
* contract validation stay local.
*/
export function createAdRunPlanner(adapters: AdRunPlannerAdapters): AdRunPlanner {
return async (rawDomain, signal) => {
const domain = normalizeDomain(rawDomain);
if (!domain) return { ok: false, error: { code: "invalid-domain" } };
if (signal?.aborted) return { ok: false, error: { code: "aborted" } };
try {
// Evaluate both checks before starting work: a planned Ad Run is useless
// unless both enrichment and generation are configured.
const contextReady = adapters.research.isReady();
const generationReady = adapters.concepts.isReady();
if (!contextReady || !generationReady) {
return { ok: false, error: { code: "not-configured" } };
}
const [brandResult, pageResult, styleguideResult, productsResult] =
await Promise.allSettled([
adapters.research.fetchBrand(domain, signal),
adapters.research.scrapePage(`https://${domain}`, signal),
adapters.research.fetchStyleguide(domain, signal),
adapters.research.fetchProducts(domain, signal),
]);
if (signal?.aborted) return { ok: false, error: { code: "aborted" } };
if (brandResult.status === "rejected" && pageResult.status === "rejected") {
return {
ok: false,
error: { code: "domain-unreachable", cause: brandResult.reason },
};
}
if (brandResult.status === "rejected") {
return {
ok: false,
error: { code: "brand-unavailable", cause: brandResult.reason },
};
}
if (productsResult.status === "rejected") {
return {
ok: false,
error: { code: "products-unavailable", cause: productsResult.reason },
};
}
const brief = buildBrief(
domain,
brandResult.value,
pageResult.status === "fulfilled" ? pageResult.value : "",
styleguideResult.status === "fulfilled"
? styleguideResult.value
: { mood: DEFAULT_MOOD, colors: [], fontFamily: null },
);
const concepts = await adapters.concepts.planConcepts(
brief,
productsResult.value,
signal,
);
if (signal?.aborted) return { ok: false, error: { code: "aborted" } };
return {
ok: true,
value: decodeAdRunPlan({ brief, concepts }),
};
} catch (cause) {
if (signal?.aborted) return { ok: false, error: { code: "aborted" } };
return { ok: false, error: { code: "internal", cause } };
}
};
}
const productionPlanner = createAdRunPlanner({
research: {
isReady: hasContextConfiguration,
fetchBrand,
scrapePage,
fetchStyleguide,
fetchProducts,
},
concepts: {
isReady: hasGatewayConfiguration,
planConcepts: (brief, products, signal) =>
pickConceptsAndCraftCopy(
gatewayTextModel("openai/gpt-5.4-mini"),
brief,
products,
signal,
),
},
});
/** Production adapter used by the Brief HTTP route. */
export const planAdRun: AdRunPlanner = (rawDomain, signal) =>
productionPlanner(rawDomain, signal);