Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | 21x 21x 21x 21x 21x 42x 21x 21x 21x 21x 21x 21x 164x 21x 49x 21x 39x 18x 18x 5296x 5296x 18x 21x 52x 52x 51x 51x 21x 6x 5x 5x 21x 15x 15x 14x 5x 5x 5x 21x 9x 9x 9x 8x 8x 9x 9x 7x 5x 8x 4x 2x 8x | import { gql } from 'graphql-tag';
import { graphqlSupports } from '../instance-version';
import { extractSimpleId } from '../../utils/idConversion';
import { requestDuo, type PageInfo } from './queries';
import type { DuoProject } from './access';
export interface Consumer {
id: string;
enabled: boolean | null;
project?: { id: string } | null;
serviceAccount?: { id: string } | null;
parentItemConsumer?: { serviceAccount?: { id: string } | null } | null;
userPermissions: { executeAiCatalogItem?: boolean } | null;
item: {
id: string;
name: string;
description: string;
itemType: string;
foundational?: boolean;
foundationalFlowReference?: string | null;
} | null;
}
interface Agent {
id: string;
name: string;
description: string;
referenceWithVersion: string | null;
}
export interface Enablement {
enabled: boolean;
runnerAvailable?: boolean | null;
createDuoWorkflowForCiAllowed?: boolean;
remoteFlowsEnabled?: boolean;
enabledFoundationalFlows?: string[] | null;
checks?: Array<{ name: string; value: boolean; message: string | null }>;
}
const CONSUMER_FIELDS = `
id enabled project { id }
serviceAccount @optional { id }
parentItemConsumer @optional { serviceAccount @optional { id } }
userPermissions { executeAiCatalogItem @optional }
item { id name description itemType foundational @optional foundationalFlowReference @optional }
`;
const catalogQuery = (foundational: boolean) => gql`
query DuoCatalog($projectId: ProjectID!, $first: Int!, $after: String) {
aiCatalogConfiguredItems(projectId: $projectId, first: $first, after: $after ${foundational ? ', includeFoundationalConsumers: true' : ''}) {
nodes { ${CONSUMER_FIELDS} } pageInfo { hasNextPage endCursor }
}
}
`;
const CATALOG = catalogQuery(false);
const CATALOG_FOUNDATIONAL = catalogQuery(true);
const CONSUMER = gql`
query DuoConfiguredConsumer($id: AiCatalogItemConsumerID!) {
aiCatalogItemConsumer(id: $id) { ${CONSUMER_FIELDS} }
}
`;
const AGENTS = gql`
query DuoFoundationalAgents($projectId: ProjectID!) {
aiFoundationalChatAgents(projectId: $projectId) {
nodes {
id
name
description
referenceWithVersion
}
}
}
`;
const ENABLEMENT = gql`
query DuoProjectEnablement($path: ID!) {
project(fullPath: $path) {
duoWorkflowRunnerAvailable @optional
duoWorkflowStatusCheck @optional {
enabled
createDuoWorkflowForCiAllowed @optional
remoteFlowsEnabled @optional
enabledFoundationalFlows @optional
checks {
name
value
message
}
}
}
}
`;
export const isDuoChat = (reference: string | null | undefined): boolean =>
reference === 'chat' || reference === 'agentic_chat/v1';
export function executionUnavailable(checks: Enablement | null): boolean {
return (
checks?.enabled === false ||
checks?.createDuoWorkflowForCiAllowed === false ||
checks?.runnerAvailable === false
);
}
export function preview(
value: string | null | undefined,
limit: number,
): { text: string | null; truncated: boolean } {
if (value === undefined || value === null) return { text: null, truncated: false };
let end = 0;
for (let characters = 0; characters < limit && end < value.length; characters++) {
// The loop condition keeps the offset within the string.
const codePoint = value.codePointAt(end) as number;
end += codePoint > 0xffff ? 2 : 1;
}
return { text: value.slice(0, end), truncated: end < value.length };
}
export async function projectEnablement(project: DuoProject): Promise<Enablement | null> {
const result = await requestDuo<{
project: {
duoWorkflowStatusCheck?: Enablement | null;
duoWorkflowRunnerAvailable?: boolean | null;
} | null;
}>(ENABLEMENT, { path: project.path });
if (!result.project) throw new Error('Duo project is unavailable');
const checks = result.project.duoWorkflowStatusCheck;
return checks
? { ...checks, runnerAvailable: result.project.duoWorkflowRunnerAvailable ?? null }
: null;
}
export async function foundationalAgents(project: DuoProject): Promise<Agent[]> {
if (!graphqlSupports('Query', 'aiFoundationalChatAgents')) return [];
const result = await requestDuo<{ aiFoundationalChatAgents: { nodes: Agent[] } | null }>(AGENTS, {
projectId: `gid://gitlab/Project/${project.id}`,
});
return result.aiFoundationalChatAgents?.nodes ?? [];
}
export async function readConsumer(id: string): Promise<Consumer> {
const result = await requestDuo<{ aiCatalogItemConsumer: Consumer | null }>(CONSUMER, {
id: `gid://gitlab/Ai::Catalog::ItemConsumer/${id}`,
});
if (!result.aiCatalogItemConsumer) throw new Error('Configured Duo consumer is unavailable');
return result.aiCatalogItemConsumer;
}
function catalogItem(
consumer: Consumer,
item: NonNullable<Consumer['item']>,
enablement: Enablement | null,
): Record<string, unknown> {
const permitted = consumer.userPermissions?.executeAiCatalogItem;
const refused =
item.itemType !== 'FLOW' || permitted === false || executionUnavailable(enablement);
// Execution permission alone does not prove runner, billing and composite-identity readiness.
return {
kind: item.itemType.toLowerCase(),
catalog_item_id: extractSimpleId(item.id),
consumer_id: extractSimpleId(consumer.id),
name: item.name,
description: preview(item.description, 200),
flow: item.foundationalFlowReference ?? null,
can_start_session: refused ? false : null,
can_execute_catalog_item: permitted ?? null,
service_account_configured: !!(
consumer.serviceAccount ?? consumer.parentItemConsumer?.serviceAccount
),
};
}
export async function discoverDuo(
project: DuoProject,
first: number,
after: string | undefined,
itemType?: string,
): Promise<unknown> {
const document = graphqlSupports(
'Query',
'aiCatalogConfiguredItems',
'includeFoundationalConsumers',
)
? CATALOG_FOUNDATIONAL
: CATALOG;
const result = await requestDuo<{
aiCatalogConfiguredItems: { nodes: Consumer[]; pageInfo: PageInfo };
}>(document, {
projectId: `gid://gitlab/Project/${project.id}`,
first,
after,
});
const enablement = await projectEnablement(project);
const items: Record<string, unknown>[] = [];
for (const consumer of result.aiCatalogConfiguredItems.nodes) {
const item = consumer.item;
if (!consumer.enabled || !item || (item.itemType === 'AGENT' && item.foundational)) continue;
if (itemType !== undefined && item.itemType.toLowerCase() !== itemType) continue;
items.push(catalogItem(consumer, item, enablement));
}
if (after === undefined && (itemType === undefined || itemType === 'agent')) {
for (const agent of await foundationalAgents(project)) {
items.push({
kind: 'foundational_agent',
agent: agent.referenceWithVersion,
name: agent.name,
description: preview(agent.description, 200),
// Visibility alone does not establish the agent's execution or billing eligibility.
can_start_session:
isDuoChat(agent.referenceWithVersion) || executionUnavailable(enablement) ? false : null,
});
}
}
return {
project_id: project.id,
items,
pageInfo: result.aiCatalogConfiguredItems.pageInfo,
enablement,
};
}
|