-
Notifications
You must be signed in to change notification settings - Fork 37
feat: Implement agent graph definitions #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jsonbailey
wants to merge
6
commits into
feat/ai-sdk-next-release
Choose a base branch
from
jb/aic-2202/agent-graph-definitions
base: feat/ai-sdk-next-release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b57ba2
feat: implement agent graph definitions (AIGRAPH + AIGRAPHTRACK)
jsonbailey 0846d45
refactor: align LDGraphTrackerImpl constructor with LDAIConfigTracker…
jsonbailey e7d1c30
fix: prettier formatting for graph tracker constructor call sites
jsonbailey 00e3497
fix: detect cycles in agentGraph and replace BFS longest-path with to…
jsonbailey 5191c48
fix: support cyclic graphs — rewrite reverseTraverse as upward BFS fr…
jsonbailey 5cba2c1
fix(server-ai): check _ldMeta.enabled before returning enabled graph
jsonbailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
423 changes: 423 additions & 0 deletions
423
packages/sdk/server-ai/__tests__/AgentGraphDefinition.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
516 changes: 214 additions & 302 deletions
516
packages/sdk/server-ai/__tests__/LDGraphTrackerImpl.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { LDContext } from '@launchdarkly/js-server-sdk-common'; | ||
|
|
||
| import { AgentGraphDefinition } from '../src/api/graph/AgentGraphDefinition'; | ||
| import { LDAIClientImpl } from '../src/LDAIClientImpl'; | ||
| import { LDClientMin } from '../src/LDClientMin'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Mocks | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const mockTrack = jest.fn(); | ||
| const mockVariation = jest.fn(); | ||
| const mockDebug = jest.fn(); | ||
|
|
||
| const mockLdClient: LDClientMin = { | ||
| track: mockTrack, | ||
| variation: mockVariation, | ||
| logger: { | ||
| debug: mockDebug, | ||
| info: jest.fn(), | ||
| warn: jest.fn(), | ||
| error: jest.fn(), | ||
| }, | ||
| }; | ||
|
|
||
| const testContext: LDContext = { kind: 'user', key: 'test-user' }; | ||
|
|
||
| const makeClient = () => new LDAIClientImpl(mockLdClient); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| function makeGraphFlagValue( | ||
| root: string, | ||
| edges: Record<string, Array<{ key: string }>> = {}, | ||
| variationKey = 'v1', | ||
| version = 1, | ||
| ) { | ||
| return { _ldMeta: { variationKey, version }, root, edges }; | ||
| } | ||
|
|
||
| function makeAgentFlagValue(key: string, enabled = true) { | ||
| return { | ||
| _ldMeta: { variationKey: `${key}-v1`, enabled, version: 1, mode: 'agent' }, | ||
| instructions: `Instructions for ${key}`, | ||
| }; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // agentGraph – disabled / validation failures | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| it('returns { enabled: false } when graph flag has no root', async () => { | ||
| const client = makeClient(); | ||
| mockVariation.mockResolvedValueOnce({ root: '' }); // no root | ||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(false); | ||
| }); | ||
|
|
||
| it('logs debug when graph has no root', async () => { | ||
| const client = makeClient(); | ||
| mockVariation.mockResolvedValueOnce({ root: '' }); | ||
| await client.agentGraph('my-graph', testContext); | ||
| expect(mockDebug).toHaveBeenCalledWith(expect.stringContaining('not fetchable')); | ||
| }); | ||
|
|
||
| it('returns { enabled: false } when a node is unconnected (not reachable from root)', async () => { | ||
| const client = makeClient(); | ||
| // Graph says root → child, but "orphan" appears in edges with no path from root | ||
| const graphValue = makeGraphFlagValue('root', { | ||
| root: [{ key: 'child' }], | ||
| orphan: [{ key: 'other' }], // orphan is not reachable from root | ||
| }); | ||
| mockVariation.mockResolvedValueOnce(graphValue); | ||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(false); | ||
| expect(mockDebug).toHaveBeenCalledWith(expect.stringContaining('unconnected node')); | ||
| }); | ||
|
|
||
| it('returns { enabled: false } when the graph contains a cycle', async () => { | ||
| const client = makeClient(); | ||
| // A → B → A forms a cycle | ||
| const graphValue = makeGraphFlagValue('a', { | ||
| a: [{ key: 'b' }], | ||
| b: [{ key: 'a' }], | ||
| }); | ||
| mockVariation.mockResolvedValueOnce(graphValue); | ||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(false); | ||
| expect(mockDebug).toHaveBeenCalledWith(expect.stringContaining('cycle')); | ||
| }); | ||
|
|
||
| it('returns { enabled: false } when a child agent config is disabled', async () => { | ||
| const client = makeClient(); | ||
| const graphValue = makeGraphFlagValue('root', { root: [{ key: 'child' }] }); | ||
| mockVariation | ||
| .mockResolvedValueOnce(graphValue) // graph flag | ||
| .mockResolvedValueOnce(makeAgentFlagValue('root', true)) // root agent config | ||
| .mockResolvedValueOnce(makeAgentFlagValue('child', false)); // child is disabled | ||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(false); | ||
| expect(mockDebug).toHaveBeenCalledWith(expect.stringContaining('not enabled')); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // agentGraph – success path | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| it('returns { enabled: true, graph } for a valid graph with a single node', async () => { | ||
| const client = makeClient(); | ||
| const graphValue = makeGraphFlagValue('solo-agent'); | ||
| mockVariation | ||
| .mockResolvedValueOnce(graphValue) | ||
| .mockResolvedValueOnce(makeAgentFlagValue('solo-agent')); | ||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(true); | ||
| if (result.enabled) { | ||
| expect(result.graph).toBeInstanceOf(AgentGraphDefinition); | ||
| expect(result.graph.rootNode().getKey()).toBe('solo-agent'); | ||
| } | ||
| }); | ||
|
|
||
| it('returns a valid AgentGraphDefinition with correct nodes for multi-node graph', async () => { | ||
| const client = makeClient(); | ||
| const graphValue = makeGraphFlagValue('root', { | ||
| root: [{ key: 'child-a' }, { key: 'child-b' }], | ||
| 'child-a': [{ key: 'leaf' }], | ||
| }); | ||
| // variation is called for: graph flag + root + child-a + child-b + leaf (order may vary) | ||
| mockVariation | ||
| .mockResolvedValueOnce(graphValue) | ||
| .mockResolvedValue(makeAgentFlagValue('agent', true)); // all agent configs succeed | ||
|
|
||
| const result = await client.agentGraph('my-graph', testContext); | ||
| expect(result.enabled).toBe(true); | ||
| if (result.enabled) { | ||
| const { graph } = result; | ||
| expect(graph.rootNode().getKey()).toBe('root'); | ||
| expect( | ||
| graph | ||
| .getChildNodes('root') | ||
| .map((n) => n.getKey()) | ||
| .sort(), | ||
| ).toEqual(['child-a', 'child-b']); | ||
| expect( | ||
| graph | ||
| .terminalNodes() | ||
| .map((n) => n.getKey()) | ||
| .sort(), | ||
| ).toEqual(['child-b', 'leaf']); | ||
| } | ||
| }); | ||
|
|
||
| it('tracks usage event when agentGraph is called', async () => { | ||
| const client = makeClient(); | ||
| mockVariation.mockResolvedValue({ root: '' }); | ||
| await client.agentGraph('my-graph', testContext); | ||
| expect(mockTrack).toHaveBeenCalledWith('$ld:ai:usage:agent-graph', testContext, 'my-graph', 1); | ||
| }); | ||
|
|
||
| it('createTracker on returned graph produces a tracker with correct graphKey', async () => { | ||
| const client = makeClient(); | ||
| const graphValue = makeGraphFlagValue('root', {}, 'varKey', 3); | ||
| mockVariation | ||
| .mockResolvedValueOnce(graphValue) | ||
| .mockResolvedValueOnce(makeAgentFlagValue('root', true)); | ||
|
|
||
| const result = await client.agentGraph('graph-key', testContext); | ||
| expect(result.enabled).toBe(true); | ||
| if (result.enabled) { | ||
| const tracker = result.graph.createTracker(); | ||
| expect(tracker.getTrackData().graphKey).toBe('graph-key'); | ||
| expect(tracker.getTrackData().version).toBe(3); | ||
| expect(tracker.getTrackData().variationKey).toBe('varKey'); | ||
| } | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // createGraphTracker | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| it('createGraphTracker reconstructs a tracker from a resumption token', async () => { | ||
| const client = makeClient(); | ||
| const graphValue = makeGraphFlagValue('root', {}, 'v99', 7); | ||
| mockVariation | ||
| .mockResolvedValueOnce(graphValue) | ||
| .mockResolvedValueOnce(makeAgentFlagValue('root', true)); | ||
|
|
||
| const result = await client.agentGraph('g-key', testContext); | ||
| expect(result.enabled).toBe(true); | ||
| if (result.enabled) { | ||
| const originalTracker = result.graph.createTracker(); | ||
| const token = originalTracker.resumptionToken; | ||
|
|
||
| const reconstructed = client.createGraphTracker(token, testContext); | ||
| expect(reconstructed.getTrackData().graphKey).toBe('g-key'); | ||
| expect(reconstructed.getTrackData().version).toBe(7); | ||
| expect(reconstructed.getTrackData().variationKey).toBe('v99'); | ||
| expect(reconstructed.getTrackData().runId).toBe(originalTracker.getTrackData().runId); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.