Files
gstack/scripts/resolvers/types.ts
T
Garry Tan 4d76cbb4ac feat(resolvers): T2 — ResolverEntry + appliesTo gate infrastructure
Adds the conditional-resolver-injection plumbing from the v2_PLAN A.1
step. Resolvers can now be either a bare ResolverFn (always fires, current
behavior) or a ResolverEntry { resolve, appliesTo? } (gated; appliesTo
returning false skips the resolver, substitutes empty string).

Why infrastructure-only: the audit during T0a confirmed most resolvers
don't need gating. The {{NAME}} placeholder system is already conditional
at the template level — a resolver only fires for skills that reference it.
The gate is for future use when a placeholder's audience needs a structural
guardrail beyond social convention, or when a sub-resolver inside a larger
composed resolver (e.g. preamble) needs per-skill skip.

scripts/gen-skill-docs.ts:444 now uses unwrapResolver() to handle both
shapes. RESOLVERS map signature widens from Record<string, ResolverFn>
to Record<string, ResolverValue>. All existing resolvers stay bare
functions and work unchanged.

Test plan:
- bun test test/resolver-entry.test.ts: 6 pass (gate plumbing + registry)
- bun test test/gen-skill-docs.test.ts: 389 pass (no regression)
- bun run gen:skill-docs --dry-run: all SKILL.md files FRESH (no diff)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 20:30:36 -07:00

102 lines
3.5 KiB
TypeScript

import { ALL_HOST_CONFIGS } from '../../hosts/index';
/**
* Host type — derived from host configs in hosts/*.ts.
* Adding a new host: create hosts/myhost.ts + add to hosts/index.ts.
* Do NOT hardcode host names here.
*/
export type Host = (typeof ALL_HOST_CONFIGS)[number]['name'];
export interface HostPaths {
skillRoot: string;
localSkillRoot: string;
binDir: string;
browseDir: string;
designDir: string;
makePdfDir: string;
}
/**
* HOST_PATHS — derived from host configs.
* Each config's globalRoot/localSkillRoot determines the path structure.
* Non-Claude hosts use $GSTACK_ROOT env vars (set by preamble).
*/
function buildHostPaths(): Record<string, HostPaths> {
const paths: Record<string, HostPaths> = {};
for (const config of ALL_HOST_CONFIGS) {
if (config.usesEnvVars) {
paths[config.name] = {
skillRoot: '$GSTACK_ROOT',
localSkillRoot: config.localSkillRoot,
binDir: '$GSTACK_BIN',
browseDir: '$GSTACK_BROWSE',
designDir: '$GSTACK_DESIGN',
makePdfDir: '$GSTACK_MAKE_PDF',
};
} else {
const root = `~/${config.globalRoot}`;
paths[config.name] = {
skillRoot: root,
localSkillRoot: config.localSkillRoot,
binDir: `${root}/bin`,
browseDir: `${root}/browse/dist`,
designDir: `${root}/design/dist`,
makePdfDir: `${root}/make-pdf/dist`,
};
}
}
return paths;
}
export const HOST_PATHS: Record<string, HostPaths> = buildHostPaths();
import type { Model } from '../models';
export type { Model } from '../models';
export interface TemplateContext {
skillName: string;
tmplPath: string;
benefitsFrom?: string[];
host: Host;
paths: HostPaths;
preambleTier?: number; // 1-4, controls which preamble sections are included
model?: Model; // model family for behavioral overlay. Omitted/undefined → no overlay.
interactive?: boolean; // true → emit plan-mode handshake in preamble. Generator-only, not written to SKILL.md.
}
/** Resolver function signature. args is populated for parameterized placeholders like {{INVOKE_SKILL:name}}. */
export type ResolverFn = (ctx: TemplateContext, args?: string[]) => string;
/**
* Optional gated resolver. When the gate returns false, the resolver is
* skipped (substituted with empty string) — same effect as the placeholder
* not being referenced. Use when a resolver's output is only meaningful for
* a known subset of skills, so future template authors get a structural
* guardrail instead of relying on social knowledge.
*
* Most resolvers don't need this — the {{NAME}} placeholder system is
* already conditional at the template level. Use only when a resolver
* lives inside another resolver (e.g. via preamble composition) AND must
* be conditionalized, or when a top-level resolver has a small, well-defined
* audience.
*/
export interface ResolverEntry {
resolve: ResolverFn;
appliesTo?: (ctx: TemplateContext) => boolean;
}
/** Anything the RESOLVERS map accepts — either a bare function or a gated entry. */
export type ResolverValue = ResolverFn | ResolverEntry;
/**
* Type-narrowing helper for the gen-skill-docs lookup.
* Returns (resolverFn, gate) so callers can do gate?.(ctx) before invoking.
*/
export function unwrapResolver(entry: ResolverValue): {
resolve: ResolverFn;
appliesTo?: (ctx: TemplateContext) => boolean;
} {
if (typeof entry === 'function') return { resolve: entry };
return { resolve: entry.resolve, appliesTo: entry.appliesTo };
}