Skip to content

Commit b725ca6

Browse files
authored
Merge pull request microsoft#250065 from microsoft/legomushroom/prompts/non-quoted-strings-in-frontmatter-arrays
[prompts]: allow for non-quoted string values inside front matter arrays
2 parents 1727f10 + c9cccf0 commit b725ca6

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

src/vs/editor/common/codecs/frontMatterCodec/parsers/frontMatterArray.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { VALID_INTER_RECORD_SPACING_TOKENS } from '../constants.js';
76
import { assert } from '../../../../../base/common/assert.js';
87
import { PartialFrontMatterValue } from './frontMatterValue.js';
98
import { FrontMatterArray } from '../tokens/frontMatterArray.js';
109
import { assertDefined } from '../../../../../base/common/types.js';
10+
import { VALID_INTER_RECORD_SPACING_TOKENS } from '../constants.js';
1111
import { FrontMatterValueToken } from '../tokens/frontMatterToken.js';
12+
import { FrontMatterSequence } from '../tokens/frontMatterSequence.js';
1213
import { TSimpleDecoderToken } from '../../simpleCodec/simpleDecoder.js';
1314
import { Comma, LeftBracket, RightBracket } from '../../simpleCodec/tokens/index.js';
1415
import { assertNotConsumed, ParserBase, TAcceptTokenResult } from '../../simpleCodec/parserBase.js';
15-
import { FrontMatterSequence } from '../tokens/frontMatterSequence.js';
1616

1717
/**
1818
* List of tokens that can go in-between array items
@@ -162,7 +162,7 @@ export class PartialFrontMatterArray extends ParserBase<TSimpleDecoderToken, Par
162162

163163
assertDefined(
164164
endToken,
165-
`No tokens found.`,
165+
'No tokens found.',
166166
);
167167

168168
assert(

src/vs/editor/common/codecs/frontMatterCodec/parsers/frontMatterValue.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { BaseToken } from '../../baseToken.js';
77
import { PartialFrontMatterArray } from './frontMatterArray.js';
88
import { PartialFrontMatterString } from './frontMatterString.js';
9-
import { FrontMatterBoolean } from '../tokens/frontMatterBoolean.js';
9+
import { asBoolean, FrontMatterBoolean } from '../tokens/frontMatterBoolean.js';
1010
import { FrontMatterValueToken } from '../tokens/frontMatterToken.js';
1111
import { PartialFrontMatterSequence } from './frontMatterSequence.js';
1212
import { FrontMatterSequence } from '../tokens/frontMatterSequence.js';
@@ -154,10 +154,8 @@ export class PartialFrontMatterValue extends ParserBase<TSimpleDecoderToken, Par
154154
}
155155
}
156156

157-
if (token instanceof Word) {
158-
return ['true', 'false'].includes(
159-
token.text.toLowerCase(),
160-
);
157+
if ((token instanceof Word) && (asBoolean(token) !== null)) {
158+
return true;
161159
}
162160

163161
return false;

src/vs/editor/common/codecs/frontMatterCodec/tokens/frontMatterBoolean.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export class FrontMatterBoolean extends FrontMatterValueToken<'boolean', readonl
7373
/**
7474
* Try to convert a {@link Word} token to a `boolean` value.
7575
*/
76-
const asBoolean = (
76+
export function asBoolean(
7777
token: Word,
78-
): boolean | null => {
78+
): boolean | null {
7979
if (token.text.toLowerCase() === 'true') {
8080
return true;
8181
}
@@ -85,4 +85,4 @@ const asBoolean = (
8585
}
8686

8787
return null;
88-
};
88+
}

src/vs/editor/test/common/codecs/frontMatterDecoder/frontMatterDecoder.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ suite('FrontMatterDecoder', () => {
358358
]);
359359
});
360360

361-
362361
test('• redundant commas', async () => {
363362
const test = disposables.add(new TestFrontMatterDecoder());
364363

src/vs/workbench/contrib/chat/common/promptSyntax/parsers/promptHeader/metadata/tools.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { PromptMetadataRecord } from './base/record.js';
77
import { localize } from '../../../../../../../../nls.js';
88
import { PromptMetadataDiagnostic, PromptMetadataError, PromptMetadataWarning } from '../diagnostics.js';
9+
import { FrontMatterSequence } from '../../../../../../../../editor/common/codecs/frontMatterCodec/tokens/frontMatterSequence.js';
910
import { FrontMatterArray, FrontMatterRecord, FrontMatterString, FrontMatterToken, FrontMatterValueToken } from '../../../../../../../../editor/common/codecs/frontMatterCodec/tokens/index.js';
1011

1112
/**
@@ -99,8 +100,11 @@ export class PromptToolsMetadata extends PromptMetadataRecord {
99100
): readonly PromptMetadataDiagnostic[] {
100101
const issues: PromptMetadataDiagnostic[] = [];
101102

102-
// tool name must be a string
103-
if ((valueToken instanceof FrontMatterString) === false) {
103+
// tool name must be a quoted or an unquoted 'string'
104+
if (
105+
(valueToken instanceof FrontMatterString) === false &&
106+
(valueToken instanceof FrontMatterSequence) === false
107+
) {
104108
issues.push(
105109
new PromptMetadataWarning(
106110
valueToken.range,

src/vs/workbench/contrib/chat/test/common/promptSyntax/parsers/textModelPromptParser.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,38 @@ suite('TextModelPromptParser', () => {
556556
]);
557557
});
558558

559+
suite('• tools metadata', () => {
560+
test('• tool names can be quoted and non-quoted string', async () => {
561+
const test = createTest(
562+
URI.file('/absolute/folder/and/a/my.prompt.md'),
563+
[
564+
/* 01 */"---",
565+
/* 02 */"tools: [tool1, 'tool2', \"tool3\", tool-4]",
566+
/* 03 */"---",
567+
/* 04 */"The cactus on my desk has a thriving Instagram account.",
568+
],
569+
PROMPT_LANGUAGE_ID,
570+
);
571+
572+
await test.allSettled();
573+
574+
const { header, metadata } = test.parser;
575+
assertDefined(
576+
header,
577+
'Prompt header must be defined.',
578+
);
579+
580+
const { tools } = metadata;
581+
assert.deepStrictEqual(
582+
tools,
583+
['tool1', 'tool2', 'tool3', 'tool-4'],
584+
'Mode metadata must have correct value.',
585+
);
586+
587+
await test.validateHeaderDiagnostics([]);
588+
});
589+
});
590+
559591
suite('• applyTo metadata', () => {
560592
suite('• language', () => {
561593
test('• prompt', async () => {

0 commit comments

Comments
 (0)