Repository navigation
feat(cli): call the Flags REST API directly for flags create, toggle and target - #148
Conversation
8bf7de8 to
628587e
Compare
32069dd to
fd357b5
Compare
628587e to
374282c
Compare
374282c to
f29ee10
Compare
…and target flags create, toggle and target --add now call the regional Flags and IAM APIs with the user's token instead of the MCP server, on the shared REST resource client. Output is JSON or a table instead of MCP text, and dry-run prints the exact request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f29ee10 to
38ec6e0
Compare
| const body = { | ||
| schema: { schema: { enabled: { boolSchema: {} } } }, | ||
| variants: | ||
| variants.length > 0 | ||
| ? variants.map((variant) => ({ name: variantResourceName(key, variant), value: {} })) | ||
| : [ | ||
| { name: variantResourceName(key, 'disabled'), value: { enabled: false } }, | ||
| { name: variantResourceName(key, 'enabled'), value: { enabled: true } }, | ||
| ], | ||
| ...(description ? { description } : {}), | ||
| }; | ||
| const create = createOperation(FLAGS, body, { id: key }); | ||
| if (client === undefined) return create; | ||
|
|
||
| const result = await createFlag(token, flagKey, { | ||
| description, | ||
| variants: variants.length > 0 ? variants : undefined, | ||
| client, | ||
| return { | ||
| ...create, | ||
| local: { client }, | ||
| run: async (token) => { | ||
| const resolved = await resolveClient(token, client); | ||
| if (!resolved.ok) return resolved; | ||
| return createOperation(FLAGS, { ...body, clients: [resolved.data] }, { id: key }).run( | ||
| token, |
There was a problem hiding this comment.
I wonder why we decided to inline the network call builders in the command handler. It isn't blocking just seems weird that some of the commands now inline it and some don't 😄
(I'd prefer to extract all the networking from handlers but that's not mandatory)
There was a problem hiding this comment.
Good point, it was inconsistent. Fixed in 5854f5e: create, toggle and target now build their requests in features/flags/api.ts (createFlagOperation, toggleFlagOperation, addRuleOperation, getFlagOperation), next to list/get/update/archive. The handlers only parse input and print output now.
| const BUCKET_COUNT = 10_000; | ||
| const DEFAULT_TARGETING_KEY = 'user_id'; |
There was a problem hiding this comment.
Do we want to make this configurable btw?
There was a problem hiding this comment.
I'd keep it fixed. The bucket count only sets how finely percentages are rounded: 10,000 buckets is 0.01% steps, the same as the MCP used. Users give percentages, so a different count wouldn't change what they can express in practice, and it's one more thing to get wrong. If someone needs finer splits we can add it later.
…ommand handlers Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bespoyasov
left a comment
There was a problem hiding this comment.
Nice!
P.S. Let's also update the code structure skill to ensure the agents keep network builders separated from the handlers later on?
Why
Moves the last
flagscommands off the MCP layer onto the Flags REST API (CGT-69), as part of the CLI's move to the Confidence REST APIs for CLI 2.0 (CGT-76).flags create,toggleandtarget --addwere the last flag commands on the MCP. Withflags resolveremoved in #146, everyflagscommand now calls REST.Builds on the shared REST client from #147, which is merged. The warehouse (#149) and recordings (#150) commands are stacked on this PR and move off the MCP there.
What
All three commands go through the shared REST client with the user's token. Routes and edge exposure were checked against
epx-flags-admin,konfidens-mcpandedge-config.flags create:POST /v1/flags?flagId=….enabledschema withdisabledandenabledvariants, unless--variantis given.--clienttakes a display name, which is resolved through IAMGET /v1/clients(newfeatures/iam, shared with recordings in feat(cli): call the Flags REST API directly for recordings targeting-key commands #150). A resource name (clients/<id>) is used as is.flags toggle --on|--off: resolves the client, reads the flag, then sendsPATCH …?updateMask=clientswith the client added or removed. If the flag is already in that state, nothing is sent.flags target --add/--from-file: reads the flag, checks every variant exists, then sendsPOST /v1/segments(allocated at proportion 1.0) followed byPOST /v1/flags/{f}/rules.--targeting-keydefaults touser_id.flags target <key>) prints the same output, now read through the same REST client as the writes.flagsandiam. Routes are registered at runtime, so they take priority over the existing default flags handlers, and requests to these hosts are still recorded.Breaking
The output changes from MCP text to JSON or a table, and creating a flag that already exists is now an error. This ships with the rest of the migration in CLI 2.0 (the release PR #143 is on hold).
Testing
pnpm qapasses. I broke each guard on purpose to confirm a test catches it: the no-op toggle, the missing-variant check, the fallthrough remainder, the client lookup, and reporting the leftover segment.target --add enabled:80,disabled:20, which produced buckets 0–8000 and 8000–10000; viewing the rules; and an unknown variant.🤖 Generated with Claude Code