Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cli-rules-plain-wording.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reflag/cli": patch
---

Rewrite the coding-agent rules from `reflag rules` in plain language, cover React Native, and note that `fallbackFlags` is replaced by read defaults.
40 changes: 20 additions & 20 deletions packages/cli/services/rules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function getCursorRules() {
return `
---
description: Guidelines for Reflag SDK v2 direct-value flags
description: Guidelines for reading Reflag flags with SDK v2
globs: "**/*.ts, **/*.tsx, **/*.js, **/*.jsx, **/*.vue"
---

Expand All @@ -16,22 +16,22 @@ export function getCopilotInstructions() {
const rules = /* markdown */ `
# Reflag SDK v2

These instructions apply to Node, Browser, React, and Vue SDK v2. Native and OpenFeature integrations may still use SDK v1; do not mix their declarations or bootstrap formats with v2.
These instructions apply to the Node.js, Browser, React, Vue and React Native SDKs, version 2. The OpenFeature providers still use SDK v1; don't mix their code or generated types with v2.

## Direct-value reads
## Reading flags

- Singular reads return the selected JSON value, not a flag wrapper. Do not use flag .isEnabled, .config.payload, .value, .track(), or feedback methods. Vue's .value is the normal ref API, not a flag wrapper.
- A read without a default requests Boolean with implicit false, even when generated types register a JSON flag. Generated types do not change runtime read categories.
- For other values, pass a JSON default immediately after the key. It selects the top-level category and supplies missing/mismatched-value fallback behavior.
- Non-null defaults must match the registered value schema, if types are generated. Without registration, return types are inferred from the default. Literal values widen, and readonly defaults produce mutable results.
- Explicit null requests arbitrary JSON and returns FlagValue; narrow or validate it before using object properties. It is not a request for a null-only value.
- Preserve authored false, zero, empty strings, empty arrays, and null. Do not substitute defaults with truthiness checks.
- Defaults must be ordinary JSON: no functions, Dates, undefined, or cycles. Objects and arrays returned by the SDK are independent mutable copies.
- Runtime category checks are not nested schema validation. Generated declarations are a snapshot inferred from authored variants; regenerate when flags change and validate externally when stronger runtime guarantees are needed.
- A flag read returns the flag's value. There's no flag object: don't use .isEnabled, .config, .payload, .track() or feedback methods on the result. In Vue, .value is the normal ref API.
- Without a default, a read expects a Boolean and returns false when the flag is missing or not a Boolean.
- For other values, pass a default after the key, such as useFlag("huddle-settings", { tokens: 128, model: "quick" }). The default sets the expected JSON type and is returned while flags load, when the flag has no value, or when the value has a different JSON type.
- Pass null as the default to accept any JSON value, then check its type before using it.
- Use the value as returned. Don't replace false, 0, empty strings or null with your default using truthiness checks such as value || fallback.
- Defaults must be plain JSON: no functions, Dates, undefined or circular references.
- The SDK checks only the top-level JSON type. It doesn't check object fields or fill in missing fields from your default, so validate nested values yourself when it matters.
- There's no fallbackFlags option. To choose the value used when flags can't be fetched, pass it as the read's default.
Comment thread
roncohen marked this conversation as resolved.
Resolved

## Generated declarations
## Generated types

Use reflag flags types after configuring the project. This CLI targets SDK v2 and loads the current production-environment variants through the management flag API. It does not generate legacy remote-config wrappers.
After configuring the project, run reflag flags types to generate declarations from your flags. Regenerate them when flags change. Declarations are checked at compile time only.

Example generated shape:

Expand All @@ -47,7 +47,7 @@ declare module "@reflag/react-sdk" {

## React

Configure ReflagProvider with the publishable key and appropriate user/company context. Never expose a secret key in browser code.
Wrap the app in ReflagProvider with the publishable key and the user and company context. Never put a secret key in browser code.

\`\`\`tsx
import { useFlag } from "@reflag/react-sdk";
Expand All @@ -60,11 +60,11 @@ function HuddleButton() {
}
\`\`\`

useFlag is non-suspending. Use useFlagSuspense explicitly inside a React Suspense boundary when initialization must complete before reading. Use separate useTrack and feedback hooks rather than methods on flag results.
useFlag never suspends. Use useFlagSuspense inside a Suspense boundary to wait for flags. Use useTrack and useSendFeedback for tracking and feedback.

## Node
## Node.js

Initialize the client, then supply context directly or bind a context-specific client. Defaults are the third argument to an unbound getFlag and the second argument to bound or remote getters.
Initialize the client, then pass the context with each read or bind a client to it. The default is the third argument to client.getFlag and the second argument to a bound client's getFlag.

\`\`\`typescript
import { ReflagClient } from "@reflag/node-sdk";
Expand All @@ -77,15 +77,15 @@ const bound = client.bindClient(context);
const settings = bound.getFlag("huddle-settings", { tokens: 128, model: "quick" });
\`\`\`

Use getFlagRemote for explicit remote evaluation. getFlags returns a record of values that may be undefined; reading a complete record is not a replacement for choosing a singular read's category/default.
Use getFlagRemote to evaluate on Reflag's servers instead. getFlags returns every flag's value, and missing flags are undefined, so prefer getFlag with a default for individual flags.

## Vue and Browser

Vue useFlag has the same default rules and returns a ref; access settings.value.model for an object-valued ref. Browser client.getFlag(key, defaultValue) returns a direct value, not a ref or wrapper.
Vue's useFlag takes the same defaults and returns a ref; read settings.value.model for an object flag. The Browser SDK's client.getFlag(key, defaultValue) returns the value itself.

## Further resources

- [Documentation](https://docs.reflag.com)
- [Repository](https://github.com/reflagcom/javascript)
- Prefer the installed package README and declarations for the exact SDK version in use.
- Prefer the installed package's README and type declarations for the exact SDK version in use.
`.trim();
5 changes: 3 additions & 2 deletions packages/cli/test/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { getCopilotInstructions, getCursorRules } from "../services/rules.js";
import { flagUrl } from "../utils/urls.js";

describe("v2 workflow helpers", () => {
it("ships matching direct-value guidance for supported coding-agent formats", () => {
it("ships matching flag-read guidance for supported coding-agent formats", () => {
const rules = getCopilotInstructions();
expect(getCursorRules()).toContain(rules);
expect(rules).toContain(
'useFlag("huddle-settings", { tokens: 128, model: "quick" })',
);
expect(rules).toContain('client.getFlag(context, "huddles")');
expect(rules).toContain("reflag flags types");
expect(rules).toContain("not nested schema validation");
expect(rules).toContain("doesn't check object fields");
expect(rules).not.toMatch(/direct-value|fallbackFlags:/i);
expect(rules).not.toMatch(
/client\.(isEnabled|getConfig|setContext|trackEvent)\(/,
);
Expand Down
Loading