src/vs/platform/agentHost/common/meta/agentCompletionAttachmentMeta.ts

219 LOC · 219 covered · 0 uncovered · 38 ranges · 1605 concepts · 19 introducers · 782 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentCompletionAttachmentMeta.ts ×7
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import type { SimpleMessageAttachment } from '../state/protocol/state.js';
7 >
8 > /**
9 > * Well-known typed views over a `SimpleMessageAttachment`'s `_meta` bag as
10 > * produced by the `completions` command, populated by the slash-command and
11 > * skill completion providers and read by the session handler. Read the bag
12 > * through {@link readCompletionAttachmentMeta} rather than indexing `_meta`
13 > * directly. Two variants are distinguished by which discriminating key is
14 > * present: a slash command (`command`) or a skill (`uri`).
15 > */
16 >
17 > /**
18 > * A client-side side effect a command completion carries. When present, the
19 > * workbench interprets it on accept (see the shared agent-host completion action
20 > * handler) rather than treating the item as a plain text/reference insertion.
21 > *
22 > * Used by Copilot agent-host permission/mode toggles (e.g. `/yolo`,
23 > * `/autopilot on`): the item applies a well-known session-config change. Whether
24 > * the item leaves text behind is expressed by its `insertText` (empty for a pure
25 > * toggle; `/command ` for an item that keeps the text so an argument can be
26 > * typed, with {@link ICommandCompletionAttachmentMeta.argumentHint} as ghost text).
27 > */
28 > export interface IAgentHostCompletionAction {
29 > /**
30 > * A partial agent-host session-config change to apply when the completion is
31 > * accepted, keyed by well-known session-config property (e.g. `autoApprove`,
32 > * `mode`) to the string-enum value. Applied via the active session's provider
33 > * so the corresponding picker updates reactively.
34 > */
35 > readonly applyConfig?: Readonly<Record<string, string>>;
36 > }
37 >
38 > /**
39 > * The `_meta` shape attached to a `completions` result that resolves to a slash
40 > * command.
41 > */
42 > export interface ICommandCompletionAttachmentMeta {
43 > /** The slash command name (without the leading `/`). */
44 > readonly command: string;
45 > /** Optional human-readable description of the command. */
46 > readonly description?: string;
47 > /**
48 > * Optional hint describing the argument the command expects. Rendered as
49 > * inline placeholder (ghost text) after an accepted command completion.
50 > */
51 > readonly argumentHint?: string;
52 > /**
53 > * Optional client-side action to run when the completion is accepted (e.g. a
54 > * permission/mode session-config toggle). See {@link IAgentHostCompletionAction}.
55 > */
56 > readonly action?: IAgentHostCompletionAction;
57 > }
58 >
59 > /**
60 > * The `_meta` shape attached to a `completions` result that resolves to a skill.
61 > */
62 > export interface ISkillCompletionAttachmentMeta {
63 > /** The skill resource URI as a string. */
64 > readonly uri: string;
65 > /** Optional internal name of the skill. */
66 > readonly name?: string;
67 > /** Optional human-readable display name (e.g. the slash-command name). */
68 > readonly displayName?: string;
69 > /** Optional human-readable description of the skill. */
70 > readonly description?: string;
71 > }
72 >
73 > /**
74 > * A typed, discriminated view over the well-known `completions` attachment
75 > * `_meta` variants. The `kind` discriminant is computed by
76 > * {@link readCompletionAttachmentMeta} from which key is present on the wire; it
77 > * is not itself carried in `_meta`.
78 > */
79 > export type CompletionAttachmentMeta =
80 > | ({ readonly kind: 'command' } & ICommandCompletionAttachmentMeta)
81 > | ({ readonly kind: 'skill' } & ISkillCompletionAttachmentMeta);
82 >
83 > /**
84 > * Reads the well-known `completions` attachment `_meta` keys, classifying the
85 > * bag into a {@link CompletionAttachmentMeta} variant by its discriminating key
86 > * (`command` for a slash command, `uri` for a skill). Returns `undefined` when
87 > * the bag is absent or matches neither variant; wrong-typed keys are dropped.
88 > */
89 > export function readCompletionAttachmentMeta(attachment: SimpleMessageAttachment): CompletionAttachmentMeta | undefined {
90 > const meta = attachment._meta; agentCompletionAttachmentMeta.ts ×2
91 > if (!meta || typeof meta !== 'object' || Array.isArray(meta)) {
92 > return undefined; agentCompletionAttachmentMeta.ts ×2
93 > }
94 > if (typeof meta['command'] === 'string') { agentCompletionAttachmentMeta.ts ×2
95 > const action = readCompletionActionMeta(meta['action']); agentCompletionAttachmentMeta.ts ×1
96 > return {
97 > kind: 'command',
98 > command: meta['command'],
99 > ...(typeof meta['description'] === 'string' ? { description: meta['description'] } : {}),
100 > ...(typeof meta['argumentHint'] === 'string' ? { argumentHint: meta['argumentHint'] } : {}),
101 > ...(action ? { action } : {}),
102 > };
103 > }
104 > if (typeof meta['uri'] === 'string') { agentCompletionAttachmentMeta.ts ×1
106 > kind: 'skill',
107 > uri: meta['uri'],
108 > ...(typeof meta['name'] === 'string' ? { name: meta['name'] } : {}),
109 > ...(typeof meta['displayName'] === 'string' ? { displayName: meta['displayName'] } : {}),
110 > ...(typeof meta['description'] === 'string' ? { description: meta['description'] } : {}),
111 > };
112 > }
113 > return undefined; agentCompletionAttachmentMeta.ts ×2
114 > }
116 > /**
117 > * Serializes a typed {@link ICommandCompletionAttachmentMeta} into the `_meta`
118 > * record, dropping `undefined` entries. Build a slash-command completion's
119 > * `_meta` through this so producers stay in lock-step with
120 > * {@link readCompletionAttachmentMeta}.
121 > */
122 > export function toCommandCompletionAttachmentMeta(meta: ICommandCompletionAttachmentMeta): Record<string, unknown> {
123 > const result: Record<string, unknown> = { command: meta.command }; agentCompletionAttachmentMeta.ts ×5
124 > if (meta.description !== undefined) {
125 > result['description'] = meta.description; agentCompletionAttachmentMeta.ts ×1
126 > }
127 > if (meta.argumentHint !== undefined) { agentCompletionAttachmentMeta.ts ×5
128 > result['argumentHint'] = meta.argumentHint; agentCompletionAttachmentMeta.ts ×1
129 > }
130 > const action = toCompletionActionMeta(meta.action); agentCompletionAttachmentMeta.ts ×5
131 > if (action !== undefined) {
132 > result['action'] = action; agentCompletionAttachmentMeta.ts ×2
133 > }
135 > }
137 > /**
138 > * Reads the optional {@link IAgentHostCompletionAction} carried on a command
139 > * completion's raw `_meta` bag (under the `action` key). Kept as the single
140 > * seam consumers use to obtain the action, mirroring {@link getCommandArgumentHint}.
141 > * Returns `undefined` when absent or malformed; wrong-typed sub-fields are dropped.
142 > */
143 > export function getCompletionAction(meta: Record<string, unknown> | undefined): IAgentHostCompletionAction | undefined {
144 > if (!meta || typeof meta !== 'object' || Array.isArray(meta)) { agentCompletionAttachmentMeta.ts ×2
145 > return undefined; agentCompletionAttachmentMeta.ts ×2
146 > }
147 > return readCompletionActionMeta(meta['action']); agentCompletionAttachmentMeta.ts ×2
148 > }
150 > /**
151 > * Parses an unknown value into an {@link IAgentHostCompletionAction}. Accepts an
152 > * object with a string-map `applyConfig`; returns `undefined` when no valid
153 > * `applyConfig` is present.
154 > */
155 > function readCompletionActionMeta(value: unknown): IAgentHostCompletionAction | undefined { agentCompletionAttachmentMeta.ts ×2
156 > if (!value || typeof value !== 'object' || Array.isArray(value)) {
157 > return undefined; agentCompletionAttachmentMeta.ts ×1
158 > }
159 > const raw = value as Record<string, unknown>; agentCompletionAttachmentMeta.ts ×3
160 > let applyConfig: Record<string, string> | undefined;
161 > const rawApplyConfig = raw['applyConfig'];
162 > if (rawApplyConfig && typeof rawApplyConfig === 'object' && !Array.isArray(rawApplyConfig)) { agentCompletionAttachmentMeta.ts ×2
163 > for (const [key, entry] of Object.entries(rawApplyConfig as Record<string, unknown>)) { agentCompletionAttachmentMeta.ts ×3
164 > if (typeof entry === 'string') {
165 > applyConfig ??= {};
166 > applyConfig[key] = entry;
167 > }
168 > }
169 > }
170 > if (applyConfig === undefined) {
171 > return undefined; agentCompletionAttachmentMeta.ts ×2
172 > }
173 > return { applyConfig }; agentCompletionAttachmentMeta.ts ×3
174 > }
176 > /**
177 > * Serializes an {@link IAgentHostCompletionAction} into a plain record for the
178 > * `_meta` bag, dropping empty entries. Returns `undefined` when the action
179 > * carries nothing meaningful.
180 > */
181 > function toCompletionActionMeta(action: IAgentHostCompletionAction | undefined): Record<string, unknown> | undefined { agentCompletionAttachmentMeta.ts ×5
182 > if (!action?.applyConfig || Object.keys(action.applyConfig).length === 0) {
183 > return undefined; agentCompletionAttachmentMeta.ts ×1
184 > }
185 > return { applyConfig: { ...action.applyConfig } }; agentCompletionAttachmentMeta.ts ×2
186 > }
188 > /**
189 > * Reads the well-known `argumentHint` from a raw completion attachment `_meta`
190 > * bag. Kept as the single seam that consumers use to obtain the hint, so a
191 > * future promotion of `argumentHint` to a first-class attachment field only
192 > * needs to change this reader. Returns `undefined` when absent or wrong-typed.
193 > */
194 > export function getCommandArgumentHint(meta: Record<string, unknown> | undefined): string | undefined {
195 > if (!meta || typeof meta !== 'object' || Array.isArray(meta)) { agentCompletionAttachmentMeta.ts ×1
196 > return undefined;
197 > }
198 > return typeof meta['argumentHint'] === 'string' ? meta['argumentHint'] : undefined;
199 > }
201 > /**
202 > * Serializes a typed {@link ISkillCompletionAttachmentMeta} into the `_meta`
203 > * record, dropping `undefined` entries. Build a skill completion's `_meta`
204 > * through this so producers stay in lock-step with
205 > * {@link readCompletionAttachmentMeta}.
206 > */
207 > export function toSkillCompletionAttachmentMeta(meta: ISkillCompletionAttachmentMeta): Record<string, unknown> {
208 > const result: Record<string, unknown> = { uri: meta.uri }; agentCompletionAttachmentMeta.ts ×2
209 > if (meta.name !== undefined) {
210 > result['name'] = meta.name;
211 > }
212 > if (meta.displayName !== undefined) {
213 > result['displayName'] = meta.displayName;
214 > }
215 > if (meta.description !== undefined) {
216 > result['description'] = meta.description; agentCompletionAttachmentMeta.ts ×1
217 > }
219 > }