Skip to content

Commit 05e1006

Browse files
Sync public snapshot from freebuff-private
1 parent df23db0 commit 05e1006

2 files changed

Lines changed: 145 additions & 13 deletions

File tree

‎common/src/ads/sponsored-skill-layer2.test.ts‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
compileLayer2Skill,
1212
isLayer2Skill,
1313
layer2PackageKey,
14+
layer2StoredProduct,
1415
LAYER2_MAX_VERIFY,
1516
LAYER2_SKILL_CHANGELOG,
1617
type SponsoredSkillLayer2,
@@ -442,12 +443,66 @@ describe('the agent step', () => {
442443
).toBe(false)
443444
})
444445

445-
test('with no keys there is no env line', () => {
446+
test('with no keys there is no env line, and the tracked signup link comes before the docs link', () => {
446447
const integrate = compileLayer2Skill(base, '1.0.0').steps[0]!
447448
expect(integrate.instructions).not.toContain('environment')
448-
expect(integrate.instructions).toEndWith(
449-
'In an empty folder, scaffold a minimal project first.',
449+
expect(integrate.instructions).toBe(
450+
`${base.goal}\nAccount: ${base.signupUrl}\nDocs: ${base.docsUrl}\nIn an empty folder, scaffold a minimal project first.`,
451+
)
452+
const rendered = renderSponsoredSkillProcedure(
453+
compileLayer2Skill(base, '1.0.0'),
454+
)
455+
const first = rendered.match(/https?:\/\/\S+/)?.[0] ?? ''
456+
expect(first).toContain('bfcid={bfcid}')
457+
expect(rendered.indexOf(SIGNUP)).toBeLessThan(
458+
rendered.indexOf(base.docsUrl),
459+
)
460+
expect(
461+
sponsoredStepNeedsUser(`${integrate.title} ${integrate.instructions}`),
462+
).toBe(false)
463+
})
464+
465+
test('a product name replaces the advertiser in the headings, and a single-product skill stays the advertiser', () => {
466+
const serpie = compileLayer2Skill(
467+
{
468+
...base,
469+
id: 'serpie-ai-search-seo',
470+
advertiser: 'griffin',
471+
product: 'Serpie',
472+
packages: [],
473+
},
474+
'1.0.0',
475+
)
476+
expect(serpie.advertiser).toBe('griffin')
477+
expect(serpie.steps.map((step) => step.title)).toEqual([
478+
'Build the Serpie integration in this project',
479+
'Ask the user to sign up for Serpie',
480+
])
481+
expect(serpie.steps[1]!.checks[0]).toMatchObject({
482+
label: 'the user finished signing up for Serpie',
483+
})
484+
const keyed = compileLayer2Skill(
485+
{
486+
...base,
487+
id: 'serpie-ai-search-seo',
488+
advertiser: 'griffin',
489+
product: 'Serpie',
490+
packages: [],
491+
envKeys: ['SERPIE_API_KEY'],
492+
},
493+
'1.0.0',
494+
)
495+
expect(keyed.steps[1]!.title).toBe(
496+
'Ask the user to create a Serpie API key and paste it',
497+
)
498+
expect(keyed.steps[1]!.credential?.label).toBe('Serpie API key')
499+
// The credential get_url is already first, so the docs step is unchanged.
500+
expect(keyed.steps[0]!.instructions).not.toContain('Account:')
501+
expect(compileLayer2Skill(base, '1.0.0').steps[0]!.title).toBe(
502+
'Build the Posthog integration in this project',
450503
)
504+
expect(layer2StoredProduct('Griffin', 'griffin')).toBeUndefined()
505+
expect(layer2StoredProduct('Serpie', 'griffin')).toBe('Serpie')
451506
})
452507

453508
test('two keys share one line', () => {

‎common/src/ads/sponsored-skill-layer2.ts‎

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,25 @@ export type SponsoredSkillLayer2Verify = z.infer<typeof layer2Verify>
130130

131131
export const sponsoredSkillLayer2Schema = z
132132
.object({
133-
/** Stable slug, `<advertiser>-<goal>`; one id has many versions. */
133+
/**
134+
* Stable slug, `<product>-<goal>` (or `<advertiser>-<goal>` when the
135+
* campaign is for the advertiser's own product). One id has many versions.
136+
*/
134137
id: slug,
135138
advertiser: slug,
139+
/**
140+
* The product this campaign sells, when it is not the advertiser.
141+
* Omitted for a single-product advertiser, which keeps the advertiser
142+
* label. Step headings and the skill id use it; `advertiser` stays the
143+
* company.
144+
*/
145+
product: z
146+
.string()
147+
.trim()
148+
.min(1)
149+
.max(40)
150+
.refine((value) => !/[\r\n]/.test(value), 'Use a single line.')
151+
.optional(),
136152
/** One sentence: what the agent builds in the user's project. */
137153
goal: z.string().trim().min(10).max(300),
138154
docsUrl: httpsUrl,
@@ -189,18 +205,69 @@ export type Layer2BuildResult =
189205
}
190206
| { ok: false; errors: SponsoredSkillParseError[] }
191207

208+
/** `PostHog, Inc.` → `posthog-inc`. Shared with the draft so ids match headings. */
209+
export function layer2Slug(text: string): string {
210+
return text
211+
.normalize('NFKD')
212+
.replace(/[̀-ͯ]/g, '')
213+
.toLowerCase()
214+
.replace(/[^a-z0-9]+/g, '-')
215+
.replace(/^-+|-+$/g, '')
216+
.slice(0, 40)
217+
.replace(/-+$/g, '')
218+
}
219+
192220
/** `posthog` → `Posthog`, `open-router` → `Open Router`: a label, never parsed. */
193-
function advertiserLabel(advertiser: string): string {
221+
export function layer2AdvertiserLabel(advertiser: string): string {
194222
return advertiser
195223
.split('-')
196224
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
197225
.join(' ')
198226
}
199227

228+
/**
229+
* The product field as stored. Blank, or the advertiser's own label, means
230+
* there is no distinct product, so a save does not rename a single-product skill.
231+
*/
232+
export function layer2StoredProduct(
233+
value: string,
234+
advertiser: string,
235+
): string | undefined {
236+
const trimmed = value.trim()
237+
if (!trimmed || trimmed === layer2AdvertiserLabel(advertiser))
238+
return undefined
239+
return trimmed
240+
}
241+
242+
/** The name step headings use: the product, else the advertiser label. */
243+
function brandLabel(
244+
source: Pick<SponsoredSkillLayer2, 'advertiser' | 'product'>,
245+
): string {
246+
return source.product?.trim() || layer2AdvertiserLabel(source.advertiser)
247+
}
248+
249+
/** Product slug first, then the advertiser, so `SERPIE_API_KEY` and `GRIFFIN_KEY` both strip. */
250+
function brandPrefixes(
251+
source: Pick<SponsoredSkillLayer2, 'advertiser' | 'product'>,
252+
): string[] {
253+
const prefixes: string[] = []
254+
for (const value of [source.product, source.advertiser]) {
255+
const slug = value ? layer2Slug(value) : ''
256+
if (slug && !prefixes.includes(slug)) prefixes.push(slug)
257+
}
258+
return prefixes
259+
}
260+
200261
/** `POSTHOG_PROJECT_TOKEN` → `project token`, `EXA_API_KEY` → `API key`. */
201-
function keyLabel(env: string, advertiser: string): string {
202-
const prefix = `${advertiser.replace(/-/g, '_').toUpperCase()}_`
203-
const rest = env.startsWith(prefix) ? env.slice(prefix.length) : env
262+
function keyLabel(env: string, prefixes: readonly string[]): string {
263+
let rest = env
264+
for (const prefix of prefixes) {
265+
const needle = `${prefix.replace(/-/g, '_').toUpperCase()}_`
266+
if (rest.startsWith(needle)) {
267+
rest = rest.slice(needle.length)
268+
break
269+
}
270+
}
204271
return rest
205272
.split('_')
206273
.map((word) => (word === 'API' ? word : word.toLowerCase()))
@@ -234,7 +301,8 @@ export function compileLayer2Skill(
234301
const npm = source.packages.filter((pkg) => pkg.ecosystem === 'npm')
235302
const pypi = source.packages.filter((pkg) => pkg.ecosystem === 'pypi')
236303
const envKeys = source.envKeys ?? []
237-
const label = advertiserLabel(source.advertiser)
304+
const label = brandLabel(source)
305+
const prefixes = brandPrefixes(source)
238306

239307
// Format 1 has no fit block, so `frameworks` must be non-empty even for a
240308
// Python-only skill; it is informational there, like the archil skill's.
@@ -302,13 +370,21 @@ export function compileLayer2Skill(
302370
envKeys.length === 1 ? 'it' : 'each'
303371
} empty in the example env file.`
304372
: ''
373+
const docsLine = `Docs: ${source.docsUrl}`
374+
const scaffold = 'In an empty folder, scaffold a minimal project first.'
375+
// A credential's get_url is already the first link in the rendered skill,
376+
// so that step text stays as it was. A signup-only skill has to lead with
377+
// the tracked URL: otherwise the docs link is first and review flags it.
378+
const instructions = envKeys.length
379+
? `${source.goal}\n${docsLine}\n${scaffold}${envLine}`
380+
: `${source.goal}\nAccount: ${source.signupUrl}\n${docsLine}\n${scaffold}`
305381
const integrate: SponsoredSkillStep = {
306382
id: 'integrate',
307383
kind: 'agent',
308384
title: `Build the ${label} integration in this project`,
309385
// Layer 1 already says fit the project, fewest changes, read the docs;
310386
// the renderer says a step is done when its checks pass.
311-
instructions: `${source.goal}\nDocs: ${source.docsUrl}\nIn an empty folder, scaffold a minimal project first.${envLine}`,
387+
instructions,
312388
checks: [
313389
...verifyChecks,
314390
...packageChecks,
@@ -324,21 +400,22 @@ export function compileLayer2Skill(
324400

325401
const userSteps: SponsoredSkillStep[] = envKeys.length
326402
? envKeys.map((env, index) => {
403+
const key = keyLabel(env, prefixes)
327404
const credential: SponsoredRunCredential = {
328405
env,
329-
label: `${label} ${keyLabel(env, source.advertiser)}`.slice(0, 60),
406+
label: `${label} ${key}`.slice(0, 60),
330407
getUrl: source.signupUrl,
331408
}
332409
return {
333410
id: `connect-${index + 1}`,
334411
kind: 'user',
335-
title: `Ask the user to create ${withArticle(`${label} ${keyLabel(env, source.advertiser)}`)} and paste it`,
412+
title: `Ask the user to create ${withArticle(`${label} ${key}`)} and paste it`,
336413
instructions: `The harness saves the value; never read, write or print it or an env file. Once ${env} is set, run the feature once and report whether ${label} answered.`,
337414
checks: [
338415
{
339416
id: `credential-${index + 1}`,
340417
kind: 'credential_provided',
341-
label: `the ${label} ${keyLabel(env, source.advertiser)} was provided`,
418+
label: `the ${label} ${key} was provided`,
342419
envKey: env,
343420
},
344421
],

0 commit comments

Comments
 (0)