Skip to content

Commit c92271e

Browse files
committed
adjust return types and graph tracker creation
1 parent 5cba2c1 commit c92271e

5 files changed

Lines changed: 32 additions & 62 deletions

File tree

packages/sdk/server-ai/src/LDAIClientImpl.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -392,23 +392,26 @@ export class LDAIClientImpl implements LDAIClient {
392392
return LDAIConfigTrackerImpl.fromResumptionToken(token, this._ldClient, context);
393393
}
394394

395-
async agentGraph(
396-
graphKey: string,
397-
context: LDContext,
398-
): Promise<{ enabled: false } | { enabled: true; graph: AgentGraphDefinition }> {
395+
async agentGraph(graphKey: string, context: LDContext): Promise<AgentGraphDefinition> {
399396
this._ldClient.track(TRACK_USAGE_AGENT_GRAPH, context, graphKey, 1);
400397

401-
const disabled = { enabled: false as const };
402-
403-
// Step 1: Fetch the graph flag value
404398
const defaultGraphValue: LDAgentGraphFlagValue = { root: '' };
405399
const graphFlagValue = (await this._ldClient.variation(
406400
graphKey,
407401
context,
408402
defaultGraphValue,
409403
)) as LDAgentGraphFlagValue;
410404

411-
// Step 2: Validate - graph must be enabled and have a root
405+
// eslint-disable-next-line no-underscore-dangle
406+
const variationKey = graphFlagValue._ldMeta?.variationKey;
407+
// eslint-disable-next-line no-underscore-dangle
408+
const version = graphFlagValue._ldMeta?.version ?? 1;
409+
const ldClient = this._ldClient;
410+
const trackerFactory = () =>
411+
new LDGraphTrackerImpl(ldClient, randomUUID(), graphKey, variationKey, version, context);
412+
413+
const disabled = new AgentGraphDefinition(graphFlagValue, {}, false, trackerFactory);
414+
412415
// eslint-disable-next-line no-underscore-dangle
413416
if (graphFlagValue._ldMeta?.enabled === false) {
414417
this._logger?.debug(`agentGraph: graph "${graphKey}" is disabled.`);
@@ -420,7 +423,6 @@ export class LDAIClientImpl implements LDAIClient {
420423
return disabled;
421424
}
422425

423-
// Step 3: Validate - collect all node keys and check connectivity from root
424426
const allKeys = AgentGraphDefinition.collectAllKeys(graphFlagValue);
425427
const reachableKeys = this._collectReachableKeys(graphFlagValue);
426428

@@ -432,7 +434,6 @@ export class LDAIClientImpl implements LDAIClient {
432434
return disabled;
433435
}
434436

435-
// Step 4: Validate - fetch all child agent configs
436437
const agentConfigs: Record<string, LDAIAgentConfig> = {};
437438
const fetchResults = await Promise.all(
438439
[...allKeys].map(async (key) => {
@@ -452,23 +453,8 @@ export class LDAIClientImpl implements LDAIClient {
452453
agentConfigs[key] = config;
453454
});
454455

455-
// Build the node map
456456
const nodes = AgentGraphDefinition.buildNodes(graphFlagValue, agentConfigs);
457-
458-
// eslint-disable-next-line no-underscore-dangle
459-
const variationKey = graphFlagValue._ldMeta?.variationKey;
460-
// eslint-disable-next-line no-underscore-dangle
461-
const version = graphFlagValue._ldMeta?.version ?? 1;
462-
const ldClient = this._ldClient;
463-
464-
const graph = new AgentGraphDefinition(
465-
graphFlagValue,
466-
nodes,
467-
() =>
468-
new LDGraphTrackerImpl(ldClient, randomUUID(), graphKey, variationKey, version, context),
469-
);
470-
471-
return { enabled: true, graph };
457+
return new AgentGraphDefinition(graphFlagValue, nodes, true, trackerFactory);
472458
}
473459

474460
createGraphTracker(token: string, context: LDContext): LDGraphTracker {

packages/sdk/server-ai/src/LDGraphTrackerImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import type { LDClientMin } from './LDClientMin';
99
/**
1010
* Concrete implementation of {@link LDGraphTracker}.
1111
*
12-
* Instantiate via {@link AgentGraphDefinition.createTracker} or reconstruct from
13-
* a resumption token via {@link LDGraphTrackerImpl.fromResumptionToken}.
12+
* Construct directly or reconstruct from a resumption token via
13+
* {@link LDGraphTrackerImpl.fromResumptionToken}.
1414
*/
1515
export class LDGraphTrackerImpl implements LDGraphTracker {
1616
private _summary: LDGraphMetricSummary = {};

packages/sdk/server-ai/src/api/LDAIClient.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,39 +341,36 @@ export interface LDAIClient {
341341

342342
/**
343343
* Fetches an agent graph configuration from LaunchDarkly and returns an
344-
* {@link AgentGraphDefinition} that can be traversed and tracked.
344+
* {@link AgentGraphDefinition}.
345345
*
346-
* The method validates that:
346+
* When the graph is enabled the method validates that:
347347
* - The graph flag can be evaluated.
348348
* - A single root node is present.
349349
* - All nodes in the graph are reachable from the root (no disconnected nodes).
350350
* - Every referenced agent config can be fetched and is enabled.
351351
*
352-
* If any validation check fails, `{ enabled: false }` is returned and, when the
353-
* logger level is DEBUG, a warning is emitted describing the failure.
352+
* If any validation check fails, the returned definition has
353+
* {@link AgentGraphDefinition.enabled | enabled} set to `false` with an empty
354+
* node collection. When the logger level is DEBUG, a message describing the
355+
* failure is emitted.
354356
*
355357
* @param graphKey The LaunchDarkly flag key for the agent graph configuration.
356358
* @param context The LaunchDarkly context used for flag evaluation and tracking.
357359
*
358-
* @returns A promise that resolves to `{ enabled: false }` when the graph is
359-
* invalid or disabled, or `{ enabled: true, graph: AgentGraphDefinition }` otherwise.
360+
* @returns A promise that resolves to an {@link AgentGraphDefinition}. Check
361+
* {@link AgentGraphDefinition.enabled | enabled} before traversing.
360362
*
361363
* @example
362364
* ```typescript
363-
* const result = await aiClient.agentGraph('my-agent-graph', context);
364-
* if (result.enabled) {
365-
* const tracker = result.graph.createTracker();
366-
* result.graph.traverse((node, ctx) => {
365+
* const graph = await aiClient.agentGraph('my-agent-graph', context);
366+
* if (graph.enabled) {
367+
* graph.traverse((node, ctx) => {
367368
* // build your provider-specific node here
368369
* });
369-
* tracker.trackInvocationSuccess();
370370
* }
371371
* ```
372372
*/
373-
agentGraph(
374-
graphKey: string,
375-
context: LDContext,
376-
): Promise<{ enabled: false } | { enabled: true; graph: AgentGraphDefinition }>;
373+
agentGraph(graphKey: string, context: LDContext): Promise<AgentGraphDefinition>;
377374

378375
/**
379376
* Reconstructs an {@link LDGraphTracker} from a resumption token, preserving

packages/sdk/server-ai/src/api/graph/AgentGraphDefinition.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ export type TraversalFn = (
1515
* Encapsulates an agent graph configuration and its pre-built node collection.
1616
*
1717
* Provides graph-level orchestration including relationship queries (parent/child),
18-
* breadth-first traversal in both forward and reverse directions, and tracker creation.
18+
* breadth-first traversal in both forward and reverse directions, and graph tracker creation.
1919
*
20-
* Obtain an instance via {@link LDAIClient.agentGraph}.
20+
* Obtain an instance via {@link LDAIClient.agentGraph}. When the graph is disabled
21+
* or invalid, the returned instance has {@link enabled} set to `false` and an
22+
* empty node collection.
2123
*/
2224
export class AgentGraphDefinition {
2325
constructor(
2426
private readonly _agentGraph: LDAgentGraphFlagValue,
2527
private readonly _nodes: Record<string, AgentGraphNode>,
28+
readonly enabled: boolean,
2629
private readonly _createTracker: () => LDGraphTracker,
2730
) {}
2831

@@ -52,22 +55,6 @@ export class AgentGraphDefinition {
5255
return nodes;
5356
}
5457

55-
/**
56-
* Whether the graph is enabled. Always `true` for a successfully constructed definition
57-
* (disabled graphs are not surfaced as AgentGraphDefinition instances).
58-
*/
59-
get enabled(): boolean {
60-
// eslint-disable-next-line no-underscore-dangle
61-
return this._agentGraph._ldMeta?.enabled ?? true;
62-
}
63-
64-
/**
65-
* Returns whether the graph is enabled.
66-
*/
67-
isEnabled(): boolean {
68-
return this.enabled;
69-
}
70-
7158
/**
7259
* Returns the children of the node identified by `nodeKey`.
7360
*

packages/sdk/server-ai/src/api/graph/LDGraphTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { LDGraphMetricSummary, LDGraphTrackData } from './types';
1212
*
1313
* @example
1414
* ```typescript
15-
* const tracker = graph.createTracker();
15+
* const tracker = graphDefinition.createTracker();
1616
* try {
1717
* // ... execute graph ...
1818
* tracker.trackInvocationSuccess();

0 commit comments

Comments
 (0)