@structured-world/gitlab-mcp 13.1.0 → 13.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +18 -6
  2. package/README.md.in +16 -4
  3. package/dist/src/channel-gateway/forwarding.js +5 -0
  4. package/dist/src/channel-gateway/forwarding.js.map +1 -1
  5. package/dist/src/cli/inject-tool-refs.d.ts +6 -18
  6. package/dist/src/cli/inject-tool-refs.js +8 -71
  7. package/dist/src/cli/inject-tool-refs.js.map +1 -1
  8. package/dist/src/cli/install/types.js +23 -3
  9. package/dist/src/cli/install/types.js.map +1 -1
  10. package/dist/src/cli/list-tools.js +3 -56
  11. package/dist/src/cli/list-tools.js.map +1 -1
  12. package/dist/src/cli/tool-catalog.d.ts +35 -0
  13. package/dist/src/cli/tool-catalog.js +112 -0
  14. package/dist/src/cli/tool-catalog.js.map +1 -0
  15. package/dist/src/config.d.ts +1 -0
  16. package/dist/src/config.js +3 -2
  17. package/dist/src/config.js.map +1 -1
  18. package/dist/src/configuration/groups.js +1 -0
  19. package/dist/src/configuration/groups.js.map +1 -1
  20. package/dist/src/configuration/namespace-capabilities.d.ts +10 -0
  21. package/dist/src/configuration/namespace-capabilities.js +105 -0
  22. package/dist/src/configuration/namespace-capabilities.js.map +1 -0
  23. package/dist/src/configuration/scoped-execution.js +53 -39
  24. package/dist/src/configuration/scoped-execution.js.map +1 -1
  25. package/dist/src/entities/context/whoami.js +2 -1
  26. package/dist/src/entities/context/whoami.js.map +1 -1
  27. package/dist/src/entities/duo/access.d.ts +14 -0
  28. package/dist/src/entities/duo/access.js +70 -0
  29. package/dist/src/entities/duo/access.js.map +1 -0
  30. package/dist/src/entities/duo/commands.d.ts +8 -0
  31. package/dist/src/entities/duo/commands.js +159 -0
  32. package/dist/src/entities/duo/commands.js.map +1 -0
  33. package/dist/src/entities/duo/discovery.d.ts +56 -0
  34. package/dist/src/entities/duo/discovery.js +159 -0
  35. package/dist/src/entities/duo/discovery.js.map +1 -0
  36. package/dist/src/entities/duo/legacy-goal-context.d.ts +9 -0
  37. package/dist/src/entities/duo/legacy-goal-context.js +134 -0
  38. package/dist/src/entities/duo/legacy-goal-context.js.map +1 -0
  39. package/dist/src/entities/duo/queries.d.ts +40 -0
  40. package/dist/src/entities/duo/queries.js +114 -0
  41. package/dist/src/entities/duo/queries.js.map +1 -0
  42. package/dist/src/entities/duo/registry.d.ts +3 -0
  43. package/dist/src/entities/duo/registry.js +70 -0
  44. package/dist/src/entities/duo/registry.js.map +1 -0
  45. package/dist/src/entities/duo/schema.d.ts +35 -0
  46. package/dist/src/entities/duo/schema.js +108 -0
  47. package/dist/src/entities/duo/schema.js.map +1 -0
  48. package/dist/src/entities/duo/sessions.d.ts +10 -0
  49. package/dist/src/entities/duo/sessions.js +205 -0
  50. package/dist/src/entities/duo/sessions.js.map +1 -0
  51. package/dist/src/entities/instance-version.d.ts +1 -1
  52. package/dist/src/entities/pipelines/schema-readonly.d.ts +3 -3
  53. package/dist/src/entities/workitems/registry.js +6 -2
  54. package/dist/src/entities/workitems/registry.js.map +1 -1
  55. package/dist/src/graphql/client.d.ts +1 -1
  56. package/dist/src/graphql/client.js +5 -2
  57. package/dist/src/graphql/client.js.map +1 -1
  58. package/dist/src/handlers.js +41 -6
  59. package/dist/src/handlers.js.map +1 -1
  60. package/dist/src/oauth/types.d.ts +1 -2
  61. package/dist/src/plugin/panel.d.ts +2 -1
  62. package/dist/src/plugin/panel.js +3 -2
  63. package/dist/src/plugin/panel.js.map +1 -1
  64. package/dist/src/plugin/server.js +108 -24
  65. package/dist/src/plugin/server.js.map +1 -1
  66. package/dist/src/profiles/applicator.js +1 -0
  67. package/dist/src/profiles/applicator.js.map +1 -1
  68. package/dist/src/profiles/builtin/admin.yaml +1 -0
  69. package/dist/src/profiles/builtin/ci.yaml +1 -0
  70. package/dist/src/profiles/builtin/code-reviewer.yaml +1 -0
  71. package/dist/src/profiles/builtin/developer.yaml +1 -0
  72. package/dist/src/profiles/builtin/devops.yaml +1 -0
  73. package/dist/src/profiles/builtin/full-access.yaml +1 -0
  74. package/dist/src/profiles/builtin/gitlab-com.yaml +1 -0
  75. package/dist/src/profiles/builtin/junior-dev.yaml +1 -0
  76. package/dist/src/profiles/builtin/pm.yaml +1 -0
  77. package/dist/src/profiles/builtin/readonly.yaml +1 -0
  78. package/dist/src/profiles/builtin/security.yaml +1 -0
  79. package/dist/src/profiles/builtin/senior-dev.yaml +1 -0
  80. package/dist/src/profiles/builtin/team-lead.yaml +1 -0
  81. package/dist/src/profiles/types.d.ts +6 -0
  82. package/dist/src/profiles/types.js +1 -0
  83. package/dist/src/profiles/types.js.map +1 -1
  84. package/dist/src/registry-manager.d.ts +8 -2
  85. package/dist/src/registry-manager.js +88 -56
  86. package/dist/src/registry-manager.js.map +1 -1
  87. package/dist/src/services/ConnectionManager.d.ts +7 -3
  88. package/dist/src/services/ConnectionManager.js +16 -2
  89. package/dist/src/services/ConnectionManager.js.map +1 -1
  90. package/dist/src/services/GitLabVersionDetector.d.ts +1 -1
  91. package/dist/src/services/GitLabVersionDetector.js +2 -2
  92. package/dist/src/services/GitLabVersionDetector.js.map +1 -1
  93. package/dist/src/services/InstanceCapabilities.d.ts +3 -2
  94. package/dist/src/services/InstanceCapabilities.js +11 -7
  95. package/dist/src/services/InstanceCapabilities.js.map +1 -1
  96. package/dist/src/services/NamespaceTierDetector.d.ts +3 -4
  97. package/dist/src/services/NamespaceTierDetector.js +146 -209
  98. package/dist/src/services/NamespaceTierDetector.js.map +1 -1
  99. package/dist/src/services/TokenScopeDetector.js +2 -0
  100. package/dist/src/services/TokenScopeDetector.js.map +1 -1
  101. package/dist/src/services/WidgetAvailability.d.ts +1 -1
  102. package/dist/src/services/WidgetAvailability.js +6 -11
  103. package/dist/src/services/WidgetAvailability.js.map +1 -1
  104. package/dist/src/services/namespace-tier-context.d.ts +9 -0
  105. package/dist/src/services/namespace-tier-context.js +19 -0
  106. package/dist/src/services/namespace-tier-context.js.map +1 -0
  107. package/dist/src/types.d.ts +2 -1
  108. package/dist/src/types.js.map +1 -1
  109. package/dist/src/utils/bounded-json.d.ts +6 -0
  110. package/dist/src/utils/bounded-json.js +39 -0
  111. package/dist/src/utils/bounded-json.js.map +1 -0
  112. package/dist/src/utils/error-handler.js +16 -5
  113. package/dist/src/utils/error-handler.js.map +1 -1
  114. package/dist/src/utils/gitlab-api.d.ts +2 -0
  115. package/dist/src/utils/gitlab-api.js +9 -3
  116. package/dist/src/utils/gitlab-api.js.map +1 -1
  117. package/dist/tsconfig.build.tsbuildinfo +1 -1
  118. package/package.json +33 -18
  119. package/skills/gitlab-duo/SKILL.md +41 -0
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startDuoSession = startDuoSession;
4
+ exports.sendDuoInput = sendDuoInput;
5
+ const graphql_tag_1 = require("graphql-tag");
6
+ const gitlab_api_1 = require("../../utils/gitlab-api");
7
+ const gitlab_base_url_1 = require("../../utils/gitlab-base-url");
8
+ const projectIdentifier_1 = require("../../utils/projectIdentifier");
9
+ const api_path_1 = require("../../utils/api-path");
10
+ const idConversion_1 = require("../../utils/idConversion");
11
+ const instance_version_1 = require("../instance-version");
12
+ const access_1 = require("./access");
13
+ const discovery_1 = require("./discovery");
14
+ const queries_1 = require("./queries");
15
+ const sessions_1 = require("./sessions");
16
+ const legacy_goal_context_1 = require("./legacy-goal-context");
17
+ const INPUT_STATE = (0, graphql_tag_1.gql) `
18
+ query DuoResumeState($id: AiDuoWorkflowsWorkflowID!) {
19
+ duoWorkflowWorkflows(workflowId: $id, first: 1) {
20
+ nodes {
21
+ id
22
+ projectId
23
+ humanStatus
24
+ createdAt
25
+ statusName @optional
26
+ status @optional
27
+ workflowDefinition @optional
28
+ aiCatalogItemVersionId @optional
29
+ project @optional {
30
+ id
31
+ fullPath
32
+ }
33
+ userPermissions {
34
+ resumeDuoWorkflow
35
+ }
36
+ }
37
+ }
38
+ }
39
+ `;
40
+ async function mutate(path, body, workflowId) {
41
+ try {
42
+ const result = await gitlab_api_1.gitlab.post(path, {
43
+ body,
44
+ contentType: 'json',
45
+ rawResponse: true,
46
+ responseByteLimit: queries_1.DUO_RESPONSE_BYTES,
47
+ redirect: 'error',
48
+ });
49
+ const id = result?.id;
50
+ if ((typeof id !== 'string' && typeof id !== 'number') ||
51
+ (typeof id === 'number' && !Number.isSafeInteger(id)) ||
52
+ !/^[1-9]\d*$/.test(String(id)) ||
53
+ typeof result.status !== 'string')
54
+ throw new Error('GitLab did not return a usable Duo session identity');
55
+ return {
56
+ workflow_id: String(result.id),
57
+ status: result.status,
58
+ web_url: result.web_url ?? null,
59
+ workload_id: result.workload?.id ?? null,
60
+ poll_after_seconds: 120,
61
+ };
62
+ }
63
+ catch (error) {
64
+ const guidance = workflowId === undefined
65
+ ? 'Inspect browse_duo_sessions list before trying to start another session.'
66
+ : `Inspect browse_duo_sessions get for workflow ${workflowId} before trying again.`;
67
+ if (error instanceof gitlab_api_1.GitLabApiError) {
68
+ error.message += ` ${guidance} Do not automatically replay this command.`;
69
+ throw error;
70
+ }
71
+ throw new Error(`Duo mutation outcome is unknown. ${guidance} Do not automatically replay this command.`, { cause: error });
72
+ }
73
+ }
74
+ async function goalContext(project, flow, goal) {
75
+ if (flow !== 'fix_pipeline/v1' && flow !== 'code_review/v1')
76
+ return { goal };
77
+ const target = new URL(goal);
78
+ const base = new URL((0, gitlab_base_url_1.getGitLabBaseUrl)());
79
+ if (target.origin !== base.origin || target.username || target.password)
80
+ throw new Error('Duo target URL must belong to the selected GitLab instance');
81
+ const kind = flow === 'fix_pipeline/v1' ? 'pipelines' : 'merge_requests';
82
+ const prefix = `${base.pathname.replace(/\/$/, '')}/${project.path}/-/${kind}/`;
83
+ const pathname = decodeURIComponent(target.pathname);
84
+ if (!pathname.startsWith(prefix))
85
+ throw new Error('Duo target URL must belong to the selected project');
86
+ const id = pathname.slice(prefix.length);
87
+ if (!/^[1-9]\d*$/.test(id))
88
+ throw new Error('Duo target URL must identify one pipeline or merge request');
89
+ const object = await gitlab_api_1.gitlab.get(`projects/${(0, projectIdentifier_1.normalizeProjectId)(project.id)}/${(0, api_path_1.encodeApiSegment)(kind)}/${(0, api_path_1.encodeApiSegment)(id)}`, { responseByteLimit: queries_1.DUO_RESPONSE_BYTES, rawResponse: true });
90
+ if (String(object.project_id) !== project.id)
91
+ throw new Error('Duo goal target belongs to another project');
92
+ if (!(0, instance_version_1.instanceAtLeast)('19.5'))
93
+ return (0, legacy_goal_context_1.legacyGoalContext)(project, flow, object, target.href, id);
94
+ return { goal: target.href };
95
+ }
96
+ async function checkedConsumer(id, project) {
97
+ const consumer = await (0, discovery_1.readConsumer)(id);
98
+ if (!consumer.project || (0, idConversion_1.extractSimpleId)(consumer.project.id) !== project.id)
99
+ throw new Error('Configured Duo consumer does not belong to the selected project');
100
+ if (!consumer.enabled || consumer.item?.itemType !== 'FLOW')
101
+ throw new Error('Configured Duo consumer is not an enabled flow');
102
+ if (consumer.userPermissions?.executeAiCatalogItem === false)
103
+ throw new Error('GitLab does not permit executing this Duo flow');
104
+ return consumer.item;
105
+ }
106
+ async function startSelection(input, project, enabledFlows) {
107
+ const body = {};
108
+ let flow = input.flow;
109
+ if (input.consumer_id !== undefined) {
110
+ const item = await checkedConsumer(input.consumer_id, project);
111
+ flow = item.foundationalFlowReference;
112
+ body.ai_catalog_item_consumer_id = input.consumer_id;
113
+ }
114
+ else if (input.agent !== undefined) {
115
+ if (!(0, instance_version_1.instanceAtLeast)('19.5'))
116
+ throw new Error('Foundational agent CI sessions require GitLab 19.5');
117
+ const available = (await (0, discovery_1.foundationalAgents)(project)).some((entry) => entry.referenceWithVersion === input.agent);
118
+ if (!available || (0, discovery_1.isDuoChat)(input.agent))
119
+ throw new Error('Select a startable foundational agent from project discovery');
120
+ body.workflow_definition = input.agent;
121
+ }
122
+ else {
123
+ if (input.flow === undefined || !enabledFlows?.includes(input.flow))
124
+ throw new Error('The foundational flow is not enabled in this project; select a configured consumer from discovery');
125
+ body.workflow_definition = input.flow;
126
+ }
127
+ return { body, flow };
128
+ }
129
+ async function startDuoSession(input, policy) {
130
+ const project = await (0, access_1.readDuoProject)(input.project_id);
131
+ (0, access_1.assertDuoProject)(project, policy);
132
+ const enablement = await (0, discovery_1.projectEnablement)(project);
133
+ if ((0, discovery_1.executionUnavailable)(enablement))
134
+ throw new Error('GitLab Duo CI execution is unavailable; inspect agents_and_flows prerequisites');
135
+ const { body, flow } = await startSelection(input, project, enablement?.enabledFoundationalFlows);
136
+ Object.assign(body, { project_id: project.id, start_workflow: true, environment: 'ambient' });
137
+ Object.assign(body, await goalContext(project, flow, input.goal));
138
+ return mutate('ai/duo_workflows/workflows', body);
139
+ }
140
+ async function sendDuoInput(input, policy) {
141
+ const before = await (0, queries_1.readWorkflow)(input.workflow_id);
142
+ await (0, access_1.assertWorkflowProject)(before, policy);
143
+ const result = await (0, queries_1.requestDuo)(INPUT_STATE, { id: (0, queries_1.workflowGid)(input.workflow_id) });
144
+ const row = result.duoWorkflowWorkflows.nodes.find((entry) => (0, idConversion_1.extractSimpleId)(entry.id) === (0, idConversion_1.extractSimpleId)(input.workflow_id));
145
+ if (row?.projectId !== before.projectId)
146
+ throw new Error('Duo session project changed or is unavailable');
147
+ const status = (0, sessions_1.statusCode)(row);
148
+ if (status === null || !sessions_1.WAITING.has(status))
149
+ throw new Error('Duo session is not in a supported waiting state');
150
+ if (row.userPermissions?.resumeDuoWorkflow !== true)
151
+ throw new Error('GitLab does not currently allow resuming: the preceding CI job may still be running, or access is restricted');
152
+ if (!(0, instance_version_1.instanceAtLeast)('19.5') && !row.aiCatalogItemVersionId)
153
+ throw new Error('This GitLab version can resume catalog flows only; foundational agents require GitLab 19.5');
154
+ const body = { human_approval: input.human_approval };
155
+ if (input.human_message !== undefined)
156
+ body.human_message = input.human_message;
157
+ return mutate(`ai/duo_workflows/workflows/${(0, idConversion_1.extractSimpleId)(input.workflow_id)}/resume`, body, (0, idConversion_1.extractSimpleId)(input.workflow_id));
158
+ }
159
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.js","sourceRoot":"","sources":["../../../../src/entities/duo/commands.ts"],"names":[],"mappings":";;AAwLA,0CAeC;AAED,oCAgCC;AAzOD,6CAAkC;AAElC,uDAAgE;AAChE,iEAA+D;AAC/D,qEAAmE;AACnE,mDAAwD;AACxD,2DAA2D;AAC3D,0DAAsD;AACtD,qCAAoG;AACpG,2CAOqB;AACrB,uCAMmB;AACnB,yCAAiD;AAEjD,+DAA0D;AAE1D,MAAM,WAAW,GAAG,IAAA,iBAAG,EAAA;;;;;;;;;;;;;;;;;;;;;;CAsBtB,CAAC;AAUF,KAAK,UAAU,MAAM,CACnB,IAAY,EACZ,IAA6B,EAC7B,UAAmB;IAEnB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,mBAAM,CAAC,IAAI,CAAiB,IAAI,EAAE;YACrD,IAAI;YACJ,WAAW,EAAE,MAAM;YACnB,WAAW,EAAE,IAAI;YACjB,iBAAiB,EAAE,4BAAkB;YACrC,QAAQ,EAAE,OAAO;SAClB,CAAC,CAAC;QACH,MAAM,EAAE,GAAG,MAAM,EAAE,EAAE,CAAC;QACtB,IACE,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,OAAO,EAAE,KAAK,QAAQ,CAAC;YAClD,CAAC,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;YACrD,CAAC,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YAC9B,OAAO,MAAM,CAAC,MAAM,KAAK,QAAQ;YAEjC,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;QACzE,OAAO;YACL,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;YAC9B,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,MAAM,CAAC,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WAAW,CAAC,SAAS,EACzE,IAAI,EACJ,IAAA,8BAAe,EAAC,KAAK,CAAC,WAAW,CAAC,CACnC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,56 @@
1
+ import type { DuoProject } from './access';
2
+ export interface Consumer {
3
+ id: string;
4
+ enabled: boolean | null;
5
+ project?: {
6
+ id: string;
7
+ } | null;
8
+ serviceAccount?: {
9
+ id: string;
10
+ } | null;
11
+ parentItemConsumer?: {
12
+ serviceAccount?: {
13
+ id: string;
14
+ } | null;
15
+ } | null;
16
+ userPermissions: {
17
+ executeAiCatalogItem?: boolean;
18
+ } | null;
19
+ item: {
20
+ id: string;
21
+ name: string;
22
+ description: string;
23
+ itemType: string;
24
+ foundational?: boolean;
25
+ foundationalFlowReference?: string | null;
26
+ } | null;
27
+ }
28
+ interface Agent {
29
+ id: string;
30
+ name: string;
31
+ description: string;
32
+ referenceWithVersion: string | null;
33
+ }
34
+ export interface Enablement {
35
+ enabled: boolean;
36
+ runnerAvailable?: boolean | null;
37
+ createDuoWorkflowForCiAllowed?: boolean;
38
+ remoteFlowsEnabled?: boolean;
39
+ enabledFoundationalFlows?: string[] | null;
40
+ checks?: Array<{
41
+ name: string;
42
+ value: boolean;
43
+ message: string | null;
44
+ }>;
45
+ }
46
+ export declare const isDuoChat: (reference: string | null | undefined) => boolean;
47
+ export declare function executionUnavailable(checks: Enablement | null): boolean;
48
+ export declare function preview(value: string | null | undefined, limit: number): {
49
+ text: string | null;
50
+ truncated: boolean;
51
+ };
52
+ export declare function projectEnablement(project: DuoProject): Promise<Enablement | null>;
53
+ export declare function foundationalAgents(project: DuoProject): Promise<Agent[]>;
54
+ export declare function readConsumer(id: string): Promise<Consumer>;
55
+ export declare function discoverDuo(project: DuoProject, first: number, after: string | undefined, itemType?: string): Promise<unknown>;
56
+ export {};
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isDuoChat = void 0;
4
+ exports.executionUnavailable = executionUnavailable;
5
+ exports.preview = preview;
6
+ exports.projectEnablement = projectEnablement;
7
+ exports.foundationalAgents = foundationalAgents;
8
+ exports.readConsumer = readConsumer;
9
+ exports.discoverDuo = discoverDuo;
10
+ const graphql_tag_1 = require("graphql-tag");
11
+ const instance_version_1 = require("../instance-version");
12
+ const idConversion_1 = require("../../utils/idConversion");
13
+ const queries_1 = require("./queries");
14
+ const CONSUMER_FIELDS = `
15
+ id enabled project { id }
16
+ serviceAccount @optional { id }
17
+ parentItemConsumer @optional { serviceAccount @optional { id } }
18
+ userPermissions { executeAiCatalogItem @optional }
19
+ item { id name description itemType foundational @optional foundationalFlowReference @optional }
20
+ `;
21
+ const catalogQuery = (foundational) => (0, graphql_tag_1.gql) `
22
+ query DuoCatalog($projectId: ProjectID!, $first: Int!, $after: String) {
23
+ aiCatalogConfiguredItems(projectId: $projectId, first: $first, after: $after ${foundational ? ', includeFoundationalConsumers: true' : ''}) {
24
+ nodes { ${CONSUMER_FIELDS} } pageInfo { hasNextPage endCursor }
25
+ }
26
+ }
27
+ `;
28
+ const CATALOG = catalogQuery(false);
29
+ const CATALOG_FOUNDATIONAL = catalogQuery(true);
30
+ const CONSUMER = (0, graphql_tag_1.gql) `
31
+ query DuoConfiguredConsumer($id: AiCatalogItemConsumerID!) {
32
+ aiCatalogItemConsumer(id: $id) { ${CONSUMER_FIELDS} }
33
+ }
34
+ `;
35
+ const AGENTS = (0, graphql_tag_1.gql) `
36
+ query DuoFoundationalAgents($projectId: ProjectID!) {
37
+ aiFoundationalChatAgents(projectId: $projectId) {
38
+ nodes {
39
+ id
40
+ name
41
+ description
42
+ referenceWithVersion
43
+ }
44
+ }
45
+ }
46
+ `;
47
+ const ENABLEMENT = (0, graphql_tag_1.gql) `
48
+ query DuoProjectEnablement($path: ID!) {
49
+ project(fullPath: $path) {
50
+ duoWorkflowRunnerAvailable @optional
51
+ duoWorkflowStatusCheck @optional {
52
+ enabled
53
+ createDuoWorkflowForCiAllowed @optional
54
+ remoteFlowsEnabled @optional
55
+ enabledFoundationalFlows @optional
56
+ checks {
57
+ name
58
+ value
59
+ message
60
+ }
61
+ }
62
+ }
63
+ }
64
+ `;
65
+ const isDuoChat = (reference) => reference === 'chat' || reference === 'agentic_chat/v1';
66
+ exports.isDuoChat = isDuoChat;
67
+ function executionUnavailable(checks) {
68
+ return (checks?.enabled === false ||
69
+ checks?.createDuoWorkflowForCiAllowed === false ||
70
+ checks?.runnerAvailable === false);
71
+ }
72
+ function preview(value, limit) {
73
+ if (value === undefined || value === null)
74
+ return { text: null, truncated: false };
75
+ let end = 0;
76
+ for (let characters = 0; characters < limit && end < value.length; characters++) {
77
+ const codePoint = value.codePointAt(end);
78
+ end += codePoint > 0xffff ? 2 : 1;
79
+ }
80
+ return { text: value.slice(0, end), truncated: end < value.length };
81
+ }
82
+ async function projectEnablement(project) {
83
+ const result = await (0, queries_1.requestDuo)(ENABLEMENT, { path: project.path });
84
+ if (!result.project)
85
+ throw new Error('Duo project is unavailable');
86
+ const checks = result.project.duoWorkflowStatusCheck;
87
+ return checks
88
+ ? { ...checks, runnerAvailable: result.project.duoWorkflowRunnerAvailable ?? null }
89
+ : null;
90
+ }
91
+ async function foundationalAgents(project) {
92
+ if (!(0, instance_version_1.graphqlSupports)('Query', 'aiFoundationalChatAgents'))
93
+ return [];
94
+ const result = await (0, queries_1.requestDuo)(AGENTS, {
95
+ projectId: `gid://gitlab/Project/${project.id}`,
96
+ });
97
+ return result.aiFoundationalChatAgents?.nodes ?? [];
98
+ }
99
+ async function readConsumer(id) {
100
+ const result = await (0, queries_1.requestDuo)(CONSUMER, {
101
+ id: `gid://gitlab/Ai::Catalog::ItemConsumer/${id}`,
102
+ });
103
+ if (!result.aiCatalogItemConsumer)
104
+ throw new Error('Configured Duo consumer is unavailable');
105
+ return result.aiCatalogItemConsumer;
106
+ }
107
+ function catalogItem(consumer, item, enablement) {
108
+ const permitted = consumer.userPermissions?.executeAiCatalogItem;
109
+ const refused = item.itemType !== 'FLOW' || permitted === false || executionUnavailable(enablement);
110
+ return {
111
+ kind: item.itemType.toLowerCase(),
112
+ catalog_item_id: (0, idConversion_1.extractSimpleId)(item.id),
113
+ consumer_id: (0, idConversion_1.extractSimpleId)(consumer.id),
114
+ name: item.name,
115
+ description: preview(item.description, 200),
116
+ flow: item.foundationalFlowReference ?? null,
117
+ can_start_session: refused ? false : null,
118
+ can_execute_catalog_item: permitted ?? null,
119
+ service_account_configured: !!(consumer.serviceAccount ?? consumer.parentItemConsumer?.serviceAccount),
120
+ };
121
+ }
122
+ async function discoverDuo(project, first, after, itemType) {
123
+ const document = (0, instance_version_1.graphqlSupports)('Query', 'aiCatalogConfiguredItems', 'includeFoundationalConsumers')
124
+ ? CATALOG_FOUNDATIONAL
125
+ : CATALOG;
126
+ const result = await (0, queries_1.requestDuo)(document, {
127
+ projectId: `gid://gitlab/Project/${project.id}`,
128
+ first,
129
+ after,
130
+ });
131
+ const enablement = await projectEnablement(project);
132
+ const items = [];
133
+ for (const consumer of result.aiCatalogConfiguredItems.nodes) {
134
+ const item = consumer.item;
135
+ if (!consumer.enabled || !item || (item.itemType === 'AGENT' && item.foundational))
136
+ continue;
137
+ if (itemType !== undefined && item.itemType.toLowerCase() !== itemType)
138
+ continue;
139
+ items.push(catalogItem(consumer, item, enablement));
140
+ }
141
+ if (after === undefined && (itemType === undefined || itemType === 'agent')) {
142
+ for (const agent of await foundationalAgents(project)) {
143
+ items.push({
144
+ kind: 'foundational_agent',
145
+ agent: agent.referenceWithVersion,
146
+ name: agent.name,
147
+ description: preview(agent.description, 200),
148
+ can_start_session: (0, exports.isDuoChat)(agent.referenceWithVersion) || executionUnavailable(enablement) ? false : null,
149
+ });
150
+ }
151
+ }
152
+ return {
153
+ project_id: project.id,
154
+ items,
155
+ pageInfo: result.aiCatalogConfiguredItems.pageInfo,
156
+ enablement,
157
+ };
158
+ }
159
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.js","sourceRoot":"","sources":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AAG,CAAC;gBAE5C,iBAAiB,EACf,IAAA,iBAAS,EAAC,KAAK,CAAC,oBAAoB,CAAC,IAAI,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;aAC3F,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO;QACL,UAAU,EAAE,OAAO,CAAC,EAAE;QACtB,KAAK;QACL,QAAQ,EAAE,MAAM,CAAC,wBAAwB,CAAC,QAAQ;QAClD,UAAU;KACX,CAAC;AACJ,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { DuoProject } from './access';
2
+ interface Target {
3
+ ref?: string;
4
+ source_branch?: string;
5
+ source?: string;
6
+ sha?: string;
7
+ }
8
+ export declare function legacyGoalContext(project: DuoProject, flow: string, target: Target, goal: string, id: string): Promise<Record<string, unknown>>;
9
+ export {};
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.legacyGoalContext = legacyGoalContext;
4
+ const graphql_tag_1 = require("graphql-tag");
5
+ const gitlab_api_1 = require("../../utils/gitlab-api");
6
+ const projectIdentifier_1 = require("../../utils/projectIdentifier");
7
+ const api_path_1 = require("../../utils/api-path");
8
+ const idConversion_1 = require("../../utils/idConversion");
9
+ const queries_1 = require("./queries");
10
+ const PIPELINE = (0, graphql_tag_1.gql) `
11
+ query DuoPipelineContext($path: ID!, $id: CiPipelineID!) {
12
+ project(fullPath: $path) {
13
+ id
14
+ pipeline(id: $id) {
15
+ id
16
+ mergeRequest {
17
+ sourceBranch
18
+ state
19
+ webUrl
20
+ author {
21
+ id
22
+ }
23
+ }
24
+ }
25
+ }
26
+ }
27
+ `;
28
+ function contextReader() {
29
+ let requests = 0;
30
+ return async function read(path, query) {
31
+ const items = [];
32
+ async function page(number) {
33
+ if (requests === 10)
34
+ throw new Error('Pipeline context lookup limit reached; no session was started');
35
+ requests++;
36
+ const rows = await gitlab_api_1.gitlab.get(path, {
37
+ query: { ...query, per_page: 100, page: number },
38
+ rawResponse: true,
39
+ responseByteLimit: queries_1.DUO_RESPONSE_BYTES,
40
+ });
41
+ if (!Array.isArray(rows) || rows.length > 100)
42
+ throw new Error('Invalid pipeline context page');
43
+ items.push(...rows);
44
+ if (rows.length === 100)
45
+ await page(number + 1);
46
+ }
47
+ await page(1);
48
+ return items;
49
+ };
50
+ }
51
+ async function associatedMergeRequest(project, branch, sha) {
52
+ const read = contextReader();
53
+ const rows = await read('merge_requests', {
54
+ scope: 'all',
55
+ state: 'opened',
56
+ source_project_id: project.id,
57
+ source_branch: branch,
58
+ });
59
+ const candidates = rows.filter((mr) => String(mr.source_project_id) === project.id &&
60
+ mr.source_branch === branch &&
61
+ mr.state === 'opened');
62
+ if (candidates.some((mr) => !Number.isSafeInteger(mr.id)))
63
+ throw new Error('Invalid associated merge request identity');
64
+ candidates.sort((left, right) => right.id - left.id);
65
+ async function matching(index) {
66
+ if (index === candidates.length)
67
+ return null;
68
+ const mr = candidates[index];
69
+ const commits = await read(`projects/${(0, projectIdentifier_1.normalizeProjectId)(String(mr.project_id))}/merge_requests/${(0, api_path_1.encodeApiSegment)(mr.iid)}/commits`, {});
70
+ if (commits.some((commit) => commit.id === sha))
71
+ return mr;
72
+ return matching(index + 1);
73
+ }
74
+ return matching(0);
75
+ }
76
+ function branchValue(value) {
77
+ if (!value)
78
+ throw new Error('GitLab did not expose the target source branch; no session was started');
79
+ return value;
80
+ }
81
+ async function attachedMergeRequest(project, id) {
82
+ const result = await (0, queries_1.requestDuo)(PIPELINE, {
83
+ path: project.path,
84
+ id: `gid://gitlab/Ci::Pipeline/${id}`,
85
+ });
86
+ const pipeline = result.project?.pipeline;
87
+ const mr = pipeline?.mergeRequest;
88
+ if (!result.project ||
89
+ (0, idConversion_1.extractSimpleId)(result.project.id) !== project.id ||
90
+ !pipeline ||
91
+ (0, idConversion_1.extractSimpleId)(pipeline.id) !== id ||
92
+ !mr)
93
+ throw new Error('GitLab did not expose the pipeline source merge request; no session was started');
94
+ return mr;
95
+ }
96
+ async function legacyGoalContext(project, flow, target, goal, id) {
97
+ if (flow === 'code_review/v1')
98
+ return { goal, source_branch: branchValue(target.source_branch) };
99
+ let branch;
100
+ let mergeRequest = null;
101
+ if (target.source === 'merge_request_event') {
102
+ const mr = await attachedMergeRequest(project, id);
103
+ branch = branchValue(mr.sourceBranch);
104
+ if (mr.state === 'opened')
105
+ mergeRequest = { url: mr.webUrl, author_id: (0, idConversion_1.extractSimpleId)(mr.author.id) };
106
+ }
107
+ else {
108
+ branch = branchValue(target.ref);
109
+ if (!target.sha)
110
+ throw new Error('GitLab did not expose the pipeline commit; no session was started');
111
+ const mr = await associatedMergeRequest(project, branch, target.sha);
112
+ if (mr)
113
+ mergeRequest = { url: mr.web_url, author_id: String(mr.author.id) };
114
+ }
115
+ if (!target.source)
116
+ throw new Error('GitLab did not expose the pipeline source; no session was started');
117
+ return {
118
+ goal,
119
+ source_branch: branch,
120
+ additional_context: [
121
+ {
122
+ Category: 'merge_request',
123
+ Content: JSON.stringify(mergeRequest ?? { url: '' }),
124
+ metadata: { version: '1.0.0' },
125
+ },
126
+ {
127
+ Category: 'pipeline',
128
+ Content: JSON.stringify({ source_branch: branch, source: target.source }),
129
+ metadata: { version: '1.0.0' },
130
+ },
131
+ ],
132
+ };
133
+ }
134
+ //# sourceMappingURL=legacy-goal-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacy-goal-context.js","sourceRoot":"","sources":["../../../../src/entities/duo/legacy-goal-context.ts"],"names":[],"mappings":";;AAiJA,8CA0CC;AA3LD,6CAAkC;AAClC,uDAAgD;AAChD,qEAAmE;AACnE,mDAAwD;AACxD,2DAA2D;AAC3D,uCAA2D;AAyB3D,MAAM,QAAQ,GAAG,IAAA,iBAAG,EAAA;;;;;;;;;;;;;;;;;CAiBnB,CAAC;AAGF,SAAS,aAAa;IACpB,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,OAAO,KAAK,UAAU,IAAI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QACN,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,CAAC,GAAG;YACb,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;QACvF,MAAM,EAAE,GAAG,MAAM,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QACrE,IAAI,EAAE;YAAE,YAAY,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;IAC9E,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,MAAM;QAChB,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IAGvF,OAAO;QACL,IAAI;QACJ,aAAa,EAAE,MAAM;QACrB,kBAAkB,EAAE;YAClB;gBACE,QAAQ,EAAE,eAAe;gBACzB,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;gBACpD,QAAQ,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE;aAC/B;YACD;gBACE,QAAQ,EAAE,UAAU;gBACpB,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;gBACzE,QAAQ,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE;aAC/B;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,40 @@
1
+ import type { DocumentNode } from 'graphql';
2
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
3
+ export declare const DUO_RESPONSE_BYTES: number;
4
+ export interface PageInfo {
5
+ hasNextPage: boolean;
6
+ endCursor: string | null;
7
+ }
8
+ export interface Workflow {
9
+ id: string;
10
+ projectId: string | null;
11
+ humanStatus: string;
12
+ statusName?: string | null;
13
+ status?: string | null;
14
+ webUrl?: string | null;
15
+ createdAt: string;
16
+ workflowDefinition?: string | null;
17
+ project?: {
18
+ id: string;
19
+ fullPath: string;
20
+ } | null;
21
+ summary?: string | null;
22
+ userPermissions?: {
23
+ resumeDuoWorkflow?: boolean;
24
+ } | null;
25
+ }
26
+ export interface WorkflowPage {
27
+ nodes: Workflow[];
28
+ pageInfo: PageInfo;
29
+ }
30
+ export declare function requestDuo<T>(document: TypedDocumentNode<T, Record<string, unknown>>, variables?: Record<string, unknown>): Promise<T>;
31
+ export declare const workflowGid: (id: string) => string;
32
+ export declare function workflowPage(first: number, after?: string): Promise<WorkflowPage>;
33
+ export declare function readWorkflow(id: string): Promise<Workflow>;
34
+ export interface Message {
35
+ content: string | null;
36
+ role: string | null;
37
+ messageType: string | null;
38
+ }
39
+ export declare const DETAILS: DocumentNode;
40
+ export declare const HISTORY: DocumentNode;