pika-shared 1.4.1 → 1.4.3

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.
@@ -35,6 +35,33 @@ declare function generateInstructionAssistanceContent(instructionAssistanceConfi
35
35
  * Apply instruction assistance to the base prompt using placeholder replacement
36
36
  */
37
37
  declare function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string;
38
+ /**
39
+ * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
40
+ *
41
+ * See header at top of file for important notes.
42
+ *
43
+ * Generate component-specific instruction content for direct component invocations
44
+ *
45
+ * @param tagDefinition - The tag definition with component invocation instructions
46
+ * @param componentAgentInstructionName - The name of the instruction set to use
47
+ * @param instructionAssistanceConfig - Optional instruction assistance config for placeholder replacement
48
+ * @param agentInstructionFeature - Optional agent instruction feature to control which placeholders are replaced
49
+ * @returns The component instruction content or undefined if not found
50
+ */
51
+ declare function generateComponentInstructionContent(tagDefinition: TagDefinition<TagDefinitionWidget>, componentAgentInstructionName: string, instructionAssistanceConfig?: InstructionAssistanceConfig, agentInstructionFeature?: AgentInstructionChatAppOverridableFeature): string | undefined;
52
+ /**
53
+ * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
54
+ *
55
+ * See header at top of file for important notes.
56
+ *
57
+ * Apply instruction assistance placeholder replacement to component instructions
58
+ *
59
+ * @param componentInstructions - The raw component instructions with placeholders
60
+ * @param instructionConfig - The instruction assistance configuration
61
+ * @param agentInstructionFeature - The agent instruction feature configuration
62
+ * @returns The component instructions with placeholders replaced
63
+ */
64
+ declare function applyComponentInstructionAssistance(componentInstructions: string, instructionConfig: InstructionAssistanceConfig, agentInstructionFeature: AgentInstructionChatAppOverridableFeature): string;
38
65
  /**
39
66
  * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
40
67
  *
@@ -47,4 +74,4 @@ declare function applyInstructionAssistance(basePrompt: string, instructionConte
47
74
  */
48
75
  declare function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig;
49
76
 
50
- export { applyInstructionAssistance, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
77
+ export { applyComponentInstructionAssistance, applyInstructionAssistance, generateComponentInstructionContent, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
@@ -35,6 +35,33 @@ declare function generateInstructionAssistanceContent(instructionAssistanceConfi
35
35
  * Apply instruction assistance to the base prompt using placeholder replacement
36
36
  */
37
37
  declare function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string;
38
+ /**
39
+ * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
40
+ *
41
+ * See header at top of file for important notes.
42
+ *
43
+ * Generate component-specific instruction content for direct component invocations
44
+ *
45
+ * @param tagDefinition - The tag definition with component invocation instructions
46
+ * @param componentAgentInstructionName - The name of the instruction set to use
47
+ * @param instructionAssistanceConfig - Optional instruction assistance config for placeholder replacement
48
+ * @param agentInstructionFeature - Optional agent instruction feature to control which placeholders are replaced
49
+ * @returns The component instruction content or undefined if not found
50
+ */
51
+ declare function generateComponentInstructionContent(tagDefinition: TagDefinition<TagDefinitionWidget>, componentAgentInstructionName: string, instructionAssistanceConfig?: InstructionAssistanceConfig, agentInstructionFeature?: AgentInstructionChatAppOverridableFeature): string | undefined;
52
+ /**
53
+ * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
54
+ *
55
+ * See header at top of file for important notes.
56
+ *
57
+ * Apply instruction assistance placeholder replacement to component instructions
58
+ *
59
+ * @param componentInstructions - The raw component instructions with placeholders
60
+ * @param instructionConfig - The instruction assistance configuration
61
+ * @param agentInstructionFeature - The agent instruction feature configuration
62
+ * @returns The component instructions with placeholders replaced
63
+ */
64
+ declare function applyComponentInstructionAssistance(componentInstructions: string, instructionConfig: InstructionAssistanceConfig, agentInstructionFeature: AgentInstructionChatAppOverridableFeature): string;
38
65
  /**
39
66
  * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!
40
67
  *
@@ -47,4 +74,4 @@ declare function applyInstructionAssistance(basePrompt: string, instructionConte
47
74
  */
48
75
  declare function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig;
49
76
 
50
- export { applyInstructionAssistance, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
77
+ export { applyComponentInstructionAssistance, applyInstructionAssistance, generateComponentInstructionContent, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
@@ -2,23 +2,18 @@
2
2
 
3
3
  // src/util/instruction-assistance-utils.ts
4
4
  function generateInstructionAssistanceContent(instructionAssistanceConfig, tags, agentInstructionFeature, tagDefinitions) {
5
- console.log("Generating instruction assistance content:", {
6
- enabled: agentInstructionFeature?.enabled,
7
- includeOutputFormattingRequirements: agentInstructionFeature?.includeOutputFormattingRequirements,
8
- includeInstructionsForTags: agentInstructionFeature?.includeInstructionsForTags,
9
- completeExampleEnabled: agentInstructionFeature?.completeExampleInstructionEnabled,
10
- jsonOnlyEnabled: agentInstructionFeature?.jsonOnlyImperativeInstructionEnabled
11
- });
12
5
  let outputFormattingRequirements = "";
13
6
  let tagInstructions = "";
14
7
  let completeExampleInstructionLine = "";
15
8
  let jsonOnlyImperativeInstructionLine = "";
9
+ let typescriptBackedOutputFormattingRequirements = "";
16
10
  if (!agentInstructionFeature?.enabled) {
17
11
  return {
18
12
  outputFormattingRequirements,
19
13
  tagInstructions,
20
14
  completeExampleInstructionLine,
21
- jsonOnlyImperativeInstructionLine
15
+ jsonOnlyImperativeInstructionLine,
16
+ typescriptBackedOutputFormattingRequirements
22
17
  };
23
18
  }
24
19
  if (agentInstructionFeature.includeOutputFormattingRequirements) {
@@ -26,18 +21,43 @@ function generateInstructionAssistanceContent(instructionAssistanceConfig, tags,
26
21
  - **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.
27
22
  - **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;
28
23
  }
29
- if (agentInstructionFeature.includeInstructionsForTags && tags && tags.tagsEnabled?.length > 0) {
30
- console.log("Fetching tag definitions for instruction generation:", tags.tagsEnabled);
24
+ if (agentInstructionFeature.includeInstructionsForTags && tags) {
31
25
  if (tagDefinitions.length > 0) {
32
- const tagDictionary = tagDefinitions.filter((tagDef) => tagDef.canBeGeneratedByLlm && !tagDef.disabled).map((tagDef) => ` - ${tagDef.tagTitle}: \`${tagDef.shortTagEx}\``).join("\n");
26
+ const enabledTagIds = /* @__PURE__ */ new Set();
27
+ if (tags.tagsEnabled) {
28
+ for (const tag of tags.tagsEnabled) {
29
+ const tagId = `${tag.scope}.${tag.tag}`;
30
+ enabledTagIds.add(tagId);
31
+ }
32
+ }
33
+ const disabledTagIds = /* @__PURE__ */ new Set();
34
+ if (tags.tagsDisabled) {
35
+ for (const tag of tags.tagsDisabled) {
36
+ const tagId = `${tag.scope}.${tag.tag}`;
37
+ disabledTagIds.add(tagId);
38
+ }
39
+ }
40
+ for (const tagDef of tagDefinitions) {
41
+ if (tagDef.usageMode === "global") {
42
+ const tagId = `${tagDef.scope}.${tagDef.tag}`;
43
+ if (!disabledTagIds.has(tagId)) {
44
+ enabledTagIds.add(tagId);
45
+ }
46
+ }
47
+ }
48
+ const enabledTagDefinitions = tagDefinitions.filter((tagDef) => {
49
+ const tagId = `${tagDef.scope}.${tagDef.tag}`;
50
+ return enabledTagIds.has(tagId) && tagDef.canBeGeneratedByLlm && tagDef.status === "enabled";
51
+ });
52
+ const tagDictionary = enabledTagDefinitions.map((tagDef) => ` - ${tagDef.tagTitle}: \`${tagDef.shortTagEx}\``).join("\n");
33
53
  let tagInstructionsContent = "";
34
54
  if (tagDictionary) {
35
55
  tagInstructionsContent += `- **Custom Tags Supported:**
36
56
  ${tagDictionary}
37
57
  `;
38
58
  }
39
- for (const tagDef of tagDefinitions) {
40
- if (tagDef.canBeGeneratedByLlm && !tagDef.disabled && tagDef.llmInstructionsMd) {
59
+ for (const tagDef of enabledTagDefinitions) {
60
+ if (tagDef.llmInstructionsMd) {
41
61
  const tagType = `${tagDef.scope}.${tagDef.tag}`;
42
62
  tagInstructionsContent += `- **${tagDef.tagTitle}:**
43
63
  <tag-instructions type="${tagType}">
@@ -57,17 +77,12 @@ ${tagDef.llmInstructionsMd}
57
77
  if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {
58
78
  jsonOnlyImperativeInstructionLine = agentInstructionFeature.jsonOnlyImperativeInstructionLine || instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine || "BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.";
59
79
  }
60
- console.log("Generated instruction assistance content:", {
61
- hasOutputFormatting: !!outputFormattingRequirements,
62
- hasTagInstructions: !!tagInstructions,
63
- hasCompleteExample: !!completeExampleInstructionLine,
64
- hasJsonValidation: !!jsonOnlyImperativeInstructionLine
65
- });
66
80
  return {
67
81
  outputFormattingRequirements,
68
82
  tagInstructions,
69
83
  completeExampleInstructionLine,
70
- jsonOnlyImperativeInstructionLine
84
+ jsonOnlyImperativeInstructionLine,
85
+ typescriptBackedOutputFormattingRequirements
71
86
  };
72
87
  }
73
88
  function applyInstructionAssistance(basePrompt, instructionContent) {
@@ -111,8 +126,48 @@ function applyInstructionAssistance(basePrompt, instructionContent) {
111
126
  }
112
127
  return enhancedPrompt;
113
128
  }
129
+ function generateComponentInstructionContent(tagDefinition, componentAgentInstructionName, instructionAssistanceConfig, agentInstructionFeature) {
130
+ console.log("Generating component instruction content:", {
131
+ scope: tagDefinition.scope,
132
+ tag: tagDefinition.tag,
133
+ componentAgentInstructionName,
134
+ hasDirectInvocationInstructions: !!tagDefinition.componentAgentInstructionsMd,
135
+ hasInstructionAssistanceConfig: !!instructionAssistanceConfig
136
+ });
137
+ if (!tagDefinition.componentAgentInstructionsMd) {
138
+ return void 0;
139
+ }
140
+ const instructions = tagDefinition.componentAgentInstructionsMd[componentAgentInstructionName];
141
+ if (!instructions) {
142
+ console.log(`No instructions found for componentAgentInstructionName: ${componentAgentInstructionName}`);
143
+ console.log("Available instruction names:", Object.keys(tagDefinition.componentAgentInstructionsMd));
144
+ return void 0;
145
+ }
146
+ console.log("Component instructions found:", {
147
+ instructionLength: instructions.length
148
+ });
149
+ if (instructionAssistanceConfig && agentInstructionFeature) {
150
+ return applyComponentInstructionAssistance(instructions, instructionAssistanceConfig, agentInstructionFeature);
151
+ }
152
+ return instructions;
153
+ }
154
+ function applyComponentInstructionAssistance(componentInstructions, instructionConfig, agentInstructionFeature) {
155
+ let enhancedInstructions = componentInstructions;
156
+ console.log("Applying component instruction assistance:", {
157
+ includeTypescriptBackedOutputFormattingRequirements: agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements,
158
+ hasPlaceholder: enhancedInstructions.includes("{{typescript-backed-output-formatting-requirements}}")
159
+ });
160
+ if (agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements && instructionConfig.typescriptBackedOutputFormattingRequirements) {
161
+ const placeholder = "{{typescript-backed-output-formatting-requirements}}";
162
+ if (enhancedInstructions.includes(placeholder)) {
163
+ console.log("Replacing typescript-backed-output-formatting-requirements placeholder");
164
+ enhancedInstructions = enhancedInstructions.replace(placeholder, instructionConfig.typescriptBackedOutputFormattingRequirements);
165
+ }
166
+ }
167
+ return enhancedInstructions;
168
+ }
114
169
  function getInstructionsAssistanceConfigFromRawSsmParams(params) {
115
- const expectedKeys = ["output-formatting-requirements", "default-complete-example-line", "default-json-validation-line"];
170
+ const expectedKeys = ["output-formatting-requirements", "default-complete-example-line", "default-json-validation-line", "typescript-backed-output-formatting-requirements"];
116
171
  const missingKeys = expectedKeys.filter((key) => !params[key]);
117
172
  if (missingKeys.length > 0) {
118
173
  throw new Error(
@@ -122,11 +177,14 @@ function getInstructionsAssistanceConfigFromRawSsmParams(params) {
122
177
  return {
123
178
  outputFormattingRequirements: params["output-formatting-requirements"],
124
179
  completeExampleInstructionLine: params["default-complete-example-line"],
125
- jsonOnlyImperativeInstructionLine: params["default-json-validation-line"]
180
+ jsonOnlyImperativeInstructionLine: params["default-json-validation-line"],
181
+ typescriptBackedOutputFormattingRequirements: params["typescript-backed-output-formatting-requirements"]
126
182
  };
127
183
  }
128
184
 
185
+ exports.applyComponentInstructionAssistance = applyComponentInstructionAssistance;
129
186
  exports.applyInstructionAssistance = applyInstructionAssistance;
187
+ exports.generateComponentInstructionContent = generateComponentInstructionContent;
130
188
  exports.generateInstructionAssistanceContent = generateInstructionAssistanceContent;
131
189
  exports.getInstructionsAssistanceConfigFromRawSsmParams = getInstructionsAssistanceConfigFromRawSsmParams;
132
190
  //# sourceMappingURL=instruction-assistance-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/util/instruction-assistance-utils.ts"],"names":[],"mappings":";;;AAgCO,SAAS,oCAAA,CACZ,2BAAA,EACA,IAAA,EACA,uBAAA,EACA,cAAA,EAC2B;AAC3B,EAAA,OAAA,CAAQ,IAAI,4CAAA,EAA8C;AAAA,IACtD,SAAS,uBAAA,EAAyB,OAAA;AAAA,IAClC,qCAAqC,uBAAA,EAAyB,mCAAA;AAAA,IAC9D,4BAA4B,uBAAA,EAAyB,0BAAA;AAAA,IACrD,wBAAwB,uBAAA,EAAyB,iCAAA;AAAA,IACjD,iBAAiB,uBAAA,EAAyB;AAAA,GAC7C,CAAA;AAED,EAAA,IAAI,4BAAA,GAA+B,EAAA;AACnC,EAAA,IAAI,eAAA,GAAkB,EAAA;AACtB,EAAA,IAAI,8BAAA,GAAiC,EAAA;AACrC,EAAA,IAAI,iCAAA,GAAoC,EAAA;AAExC,EAAA,IAAI,CAAC,yBAAyB,OAAA,EAAS;AACnC,IAAA,OAAO;AAAA,MACH,4BAAA;AAAA,MACA,eAAA;AAAA,MACA,8BAAA;AAAA,MACA;AAAA,KACJ;AAAA,EACJ;AAGA,EAAA,IAAI,wBAAwB,mCAAA,EAAqC;AAC7D,IAAA,4BAAA,GACI,6BAA6B,4BAAA,IAC7B,CAAA;AAAA;AAAA,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AgD,MAAA,EAA6D;AACzH,EAAA,MAAM,YAAA,GAAe,CAAC,gCAAA,EAAkC,+BAAA,EAAiC,8BAA8B,CAAA;AACvH,EAAA,MAAM,WAAA,GAAc,aAAa,MAAA,CAAO,CAAC,QAAQ,CAAC,MAAA,CAAO,GAAG,CAAC,CAAA;AAC7D,EAAA,IAAI,WAAA,CAAY,SAAS,CAAA,EAAG;AACxB,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,uDAAuD,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CACpE,OAAO,CAAC,GAAA,KAAQ,CAAC,YAAA,CAAa,SAAS,GAAG,CAAC,CAAA,CAC3C,IAAA,CAAK,IAAI,CAAC,CAAA;AAAA,KACnB;AAAA,EACJ;AAEA,EAAA,OAAO;AAAA,IACH,4BAAA,EAA8B,OAAO,gCAAgC,CAAA;AAAA,IACrE,8BAAA,EAAgC,OAAO,+BAA+B,CAAA;AAAA,IACtE,iCAAA,EAAmC,OAAO,8BAA8B;AAAA,GAC5E;AACJ","file":"instruction-assistance-utils.js","sourcesContent":["/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * The functions in this utility are used both by the front end svelte kit web client in the browser\n * and in the backend lambda converse functions. So, that means it needs to be able\n * to run in a browser. Do not add additional imports beyond anodine types.\n *\n * To be super clear: this is just logic to generate instruction assistance content given\n * the right inputs. It does not collect up those inputs, it is given them.\n *\n */\n\nimport type {\n AgentInstructionChatAppOverridableFeature,\n InstructionAssistanceConfig,\n TagDefinition,\n TagDefinitionWidget,\n TagsChatAppOverridableFeature\n} from '../types/chatbot/chatbot-types';\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate instruction assistance content based on enabled features\n *\n * @param instructionAssistanceConfig - The instruction assistance configuration from SSM\n * @param tags - The tags configuration from the chat app\n * @param agentInstructionFeature - The agent instruction feature configuration from the chat app\n * @returns The instruction assistance content\n */\nexport function generateInstructionAssistanceContent(\n instructionAssistanceConfig: InstructionAssistanceConfig | undefined,\n tags: TagsChatAppOverridableFeature | undefined,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature | undefined,\n tagDefinitions: TagDefinition<TagDefinitionWidget>[]\n): InstructionAssistanceConfig {\n console.log('Generating instruction assistance content:', {\n enabled: agentInstructionFeature?.enabled,\n includeOutputFormattingRequirements: agentInstructionFeature?.includeOutputFormattingRequirements,\n includeInstructionsForTags: agentInstructionFeature?.includeInstructionsForTags,\n completeExampleEnabled: agentInstructionFeature?.completeExampleInstructionEnabled,\n jsonOnlyEnabled: agentInstructionFeature?.jsonOnlyImperativeInstructionEnabled\n });\n\n let outputFormattingRequirements = '';\n let tagInstructions = '';\n let completeExampleInstructionLine = '';\n let jsonOnlyImperativeInstructionLine = '';\n\n if (!agentInstructionFeature?.enabled) {\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine\n };\n }\n\n // Generate output formatting requirements\n if (agentInstructionFeature.includeOutputFormattingRequirements) {\n outputFormattingRequirements =\n instructionAssistanceConfig?.outputFormattingRequirements ||\n `**Output Formatting Requirements:**\n- **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.\n- **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;\n }\n\n // Generate tag instructions\n if (agentInstructionFeature.includeInstructionsForTags && tags && tags.tagsEnabled?.length > 0) {\n console.log('Fetching tag definitions for instruction generation:', tags.tagsEnabled);\n\n if (tagDefinitions.length > 0) {\n // First create a dictionary listing all supported tags\n const tagDictionary = tagDefinitions\n .filter((tagDef) => tagDef.canBeGeneratedByLlm && !tagDef.disabled)\n .map((tagDef) => ` - ${tagDef.tagTitle}: \\`${tagDef.shortTagEx}\\``)\n .join('\\n');\n\n let tagInstructionsContent = '';\n if (tagDictionary) {\n tagInstructionsContent += `- **Custom Tags Supported:**\\n${tagDictionary}\\n`;\n }\n\n // Then add detailed instructions for each tag\n for (const tagDef of tagDefinitions) {\n if (tagDef.canBeGeneratedByLlm && !tagDef.disabled && tagDef.llmInstructionsMd) {\n const tagType = `${tagDef.scope}.${tagDef.tag}`;\n tagInstructionsContent += `- **${tagDef.tagTitle}:**\\n <tag-instructions type=\"${tagType}\">\\n${tagDef.llmInstructionsMd}\\n </tag-instructions>\\n`;\n }\n }\n\n if (tagInstructionsContent) {\n tagInstructions = tagInstructionsContent;\n }\n }\n }\n\n // Generate complete example instruction line\n if (agentInstructionFeature.completeExampleInstructionEnabled) {\n completeExampleInstructionLine =\n agentInstructionFeature.completeExampleInstructionLine ||\n instructionAssistanceConfig?.completeExampleInstructionLine ||\n '- **Complete Example Output:**\\n `<answer>##Example markdown\\nNormal text and an <image>http://some.url</image> and some **bold text**\\n<chart>(...)</chart></answer>`';\n }\n\n // Generate JSON validation instruction line\n if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {\n jsonOnlyImperativeInstructionLine =\n agentInstructionFeature.jsonOnlyImperativeInstructionLine ||\n instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine ||\n 'BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.';\n }\n\n console.log('Generated instruction assistance content:', {\n hasOutputFormatting: !!outputFormattingRequirements,\n hasTagInstructions: !!tagInstructions,\n hasCompleteExample: !!completeExampleInstructionLine,\n hasJsonValidation: !!jsonOnlyImperativeInstructionLine\n });\n\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine\n };\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance to the base prompt using placeholder replacement\n */\nexport function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string {\n let enhancedPrompt = basePrompt;\n\n // Check for primary placeholder first\n if (enhancedPrompt.includes('{{prompt-assistance}}')) {\n console.log('Found {{prompt-assistance}} placeholder');\n\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ]\n .filter((content) => content && content.trim().length > 0)\n .join('\\n\\n');\n\n enhancedPrompt = enhancedPrompt.replace('{{prompt-assistance}}', allContent);\n } else {\n // Look for fine-grained placeholders\n const placeholders = [\n { placeholder: '{{output-formatting-requirements}}', content: instructionContent.outputFormattingRequirements },\n { placeholder: '{{tag-instructions}}', content: instructionContent.tagInstructions },\n { placeholder: '{{complete-example-instruction-line}}', content: instructionContent.completeExampleInstructionLine },\n { placeholder: '{{json-only-imperative-instruction-line}}', content: instructionContent.jsonOnlyImperativeInstructionLine }\n ];\n\n let hasAnyPlaceholder = false;\n for (const { placeholder, content } of placeholders) {\n if (enhancedPrompt.includes(placeholder) && content) {\n console.log(`Found ${placeholder} placeholder`);\n hasAnyPlaceholder = true;\n enhancedPrompt = enhancedPrompt.replace(placeholder, content);\n }\n }\n\n // If no placeholders found, append to end\n if (!hasAnyPlaceholder) {\n console.log('No placeholders found, appending to end of prompt');\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ].filter((content) => content && content.trim().length > 0);\n\n if (allContent.length > 0) {\n enhancedPrompt = enhancedPrompt + '\\n\\n' + allContent.join('\\n\\n');\n }\n }\n }\n\n return enhancedPrompt;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Get the instruction assistance configuration from raw SSM parameters\n *\n * @param params - The raw SSM parameters\n * @returns The instruction assistance configuration\n */\nexport function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig {\n const expectedKeys = ['output-formatting-requirements', 'default-complete-example-line', 'default-json-validation-line'];\n const missingKeys = expectedKeys.filter((key) => !params[key]);\n if (missingKeys.length > 0) {\n throw new Error(\n `Missing required instruction assistance parameters: ${Object.keys(params)\n .filter((key) => !expectedKeys.includes(key))\n .join(', ')}`\n );\n }\n\n return {\n outputFormattingRequirements: params['output-formatting-requirements'],\n completeExampleInstructionLine: params['default-complete-example-line'],\n jsonOnlyImperativeInstructionLine: params['default-json-validation-line']\n };\n}\n"]}
1
+ {"version":3,"sources":["../../src/util/instruction-assistance-utils.ts"],"names":[],"mappings":";;;AAgCO,SAAS,oCAAA,CACZ,2BAAA,EACA,IAAA,EACA,uBAAA,EACA,cAAA,EAC2B;AAC3B,EAAA,IAAI,4BAAA,GAA+B,EAAA;AACnC,EAAA,IAAI,eAAA,GAAkB,EAAA;AACtB,EAAA,IAAI,8BAAA,GAAiC,EAAA;AACrC,EAAA,IAAI,iCAAA,GAAoC,EAAA;AACxC,EAAA,IAAI,4CAAA,GAA+C,EAAA;AAEnD,EAAA,IAAI,CAAC,yBAAyB,OAAA,EAAS;AACnC,IAAA,OAAO;AAAA,MACH,4BAAA;AAAA,MACA,eAAA;AAAA,MACA,8BAAA;AAAA,MACA,iCAAA;AAAA,MACA;AAAA,KACJ;AAAA,EACJ;AAGA,EAAA,IAAI,wBAAwB,mCAAA,EAAqC;AAC7D,IAAA,4BAAA,GACI,6BAA6B,4BAAA,IAC7B,CAAA;AAAA;AAAA,0FAAA,CAAA;AAAA,EAGR;AAGA,EAAA,IAAI,uBAAA,CAAwB,8BAA8B,IAAA,EAAM;AAC5D,IAAA,IAAI,cAAA,CAAe,SAAS,CAAA,EAAG;AAE3B,MAAA,MAAM,aAAA,uBAAoB,GAAA,EAAY;AAGtC,MAAA,IAAI,KAAK,WAAA,EAAa;AAClB,QAAA,KAAA,MAAW,GAAA,IAAO,KAAK,WAAA,EAAa;AAChC,UAAA,MAAM,QAAQ,CAAA,EAAG,GAAA,CAAI,KAAK,CAAA,CAAA,EAAI,IAAI,GAAG,CAAA,CAAA;AACrC,UAAA,aAAA,CAAc,IAAI,KAAK,CAAA;AAAA,QAC3B;AA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;AAAA,IACH,4BAAA,EAA8B,OAAO,gCAAgC,CAAA;AAAA,IACrE,8BAAA,EAAgC,OAAO,+BAA+B,CAAA;AAAA,IACtE,iCAAA,EAAmC,OAAO,8BAA8B,CAAA;AAAA,IACxE,4CAAA,EAA8C,OAAO,kDAAkD;AAAA,GAC3G;AACJ","file":"instruction-assistance-utils.js","sourcesContent":["/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * The functions in this utility are used both by the front end svelte kit web client in the browser\n * and in the backend lambda converse functions. So, that means it needs to be able\n * to run in a browser. Do not add additional imports beyond anodine types.\n *\n * To be super clear: this is just logic to generate instruction assistance content given\n * the right inputs. It does not collect up those inputs, it is given them.\n *\n */\n\nimport type {\n AgentInstructionChatAppOverridableFeature,\n InstructionAssistanceConfig,\n TagDefinition,\n TagDefinitionWidget,\n TagsChatAppOverridableFeature\n} from '../types/chatbot/chatbot-types';\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate instruction assistance content based on enabled features\n *\n * @param instructionAssistanceConfig - The instruction assistance configuration from SSM\n * @param tags - The tags configuration from the chat app\n * @param agentInstructionFeature - The agent instruction feature configuration from the chat app\n * @returns The instruction assistance content\n */\nexport function generateInstructionAssistanceContent(\n instructionAssistanceConfig: InstructionAssistanceConfig | undefined,\n tags: TagsChatAppOverridableFeature | undefined,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature | undefined,\n tagDefinitions: TagDefinition<TagDefinitionWidget>[]\n): InstructionAssistanceConfig {\n let outputFormattingRequirements = '';\n let tagInstructions = '';\n let completeExampleInstructionLine = '';\n let jsonOnlyImperativeInstructionLine = '';\n let typescriptBackedOutputFormattingRequirements = '';\n\n if (!agentInstructionFeature?.enabled) {\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine,\n typescriptBackedOutputFormattingRequirements\n };\n }\n\n // Generate output formatting requirements\n if (agentInstructionFeature.includeOutputFormattingRequirements) {\n outputFormattingRequirements =\n instructionAssistanceConfig?.outputFormattingRequirements ||\n `**Output Formatting Requirements:**\n- **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.\n- **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;\n }\n\n // Generate tag instructions\n if (agentInstructionFeature.includeInstructionsForTags && tags) {\n if (tagDefinitions.length > 0) {\n // Build a set of enabled tag identifiers\n const enabledTagIds = new Set<string>();\n\n // Add all explicitly enabled chat-app tags\n if (tags.tagsEnabled) {\n for (const tag of tags.tagsEnabled) {\n const tagId = `${tag.scope}.${tag.tag}`;\n enabledTagIds.add(tagId);\n }\n }\n\n // Add all global tags that aren't explicitly disabled\n const disabledTagIds = new Set<string>();\n if (tags.tagsDisabled) {\n for (const tag of tags.tagsDisabled) {\n const tagId = `${tag.scope}.${tag.tag}`;\n disabledTagIds.add(tagId);\n }\n }\n\n for (const tagDef of tagDefinitions) {\n if (tagDef.usageMode === 'global') {\n const tagId = `${tagDef.scope}.${tagDef.tag}`;\n if (!disabledTagIds.has(tagId)) {\n enabledTagIds.add(tagId);\n }\n }\n }\n\n // Filter to only include enabled tags\n const enabledTagDefinitions = tagDefinitions.filter((tagDef) => {\n const tagId = `${tagDef.scope}.${tagDef.tag}`;\n return enabledTagIds.has(tagId) && tagDef.canBeGeneratedByLlm && tagDef.status === 'enabled';\n });\n\n // First create a dictionary listing all supported tags\n const tagDictionary = enabledTagDefinitions.map((tagDef) => ` - ${tagDef.tagTitle}: \\`${tagDef.shortTagEx}\\``).join('\\n');\n\n let tagInstructionsContent = '';\n if (tagDictionary) {\n tagInstructionsContent += `- **Custom Tags Supported:**\\n${tagDictionary}\\n`;\n }\n\n // Then add detailed instructions for each tag\n for (const tagDef of enabledTagDefinitions) {\n if (tagDef.llmInstructionsMd) {\n const tagType = `${tagDef.scope}.${tagDef.tag}`;\n tagInstructionsContent += `- **${tagDef.tagTitle}:**\\n <tag-instructions type=\"${tagType}\">\\n${tagDef.llmInstructionsMd}\\n </tag-instructions>\\n`;\n }\n }\n\n if (tagInstructionsContent) {\n tagInstructions = tagInstructionsContent;\n }\n }\n }\n\n // Generate complete example instruction line\n if (agentInstructionFeature.completeExampleInstructionEnabled) {\n completeExampleInstructionLine =\n agentInstructionFeature.completeExampleInstructionLine ||\n instructionAssistanceConfig?.completeExampleInstructionLine ||\n '- **Complete Example Output:**\\n `<answer>##Example markdown\\nNormal text and an <image>http://some.url</image> and some **bold text**\\n<chart>(...)</chart></answer>`';\n }\n\n // Generate JSON validation instruction line\n if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {\n jsonOnlyImperativeInstructionLine =\n agentInstructionFeature.jsonOnlyImperativeInstructionLine ||\n instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine ||\n 'BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.';\n }\n\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine,\n typescriptBackedOutputFormattingRequirements\n };\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance to the base prompt using placeholder replacement\n */\nexport function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string {\n let enhancedPrompt = basePrompt;\n\n // Check for primary placeholder first\n if (enhancedPrompt.includes('{{prompt-assistance}}')) {\n console.log('Found {{prompt-assistance}} placeholder');\n\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ]\n .filter((content) => content && content.trim().length > 0)\n .join('\\n\\n');\n\n enhancedPrompt = enhancedPrompt.replace('{{prompt-assistance}}', allContent);\n } else {\n // Look for fine-grained placeholders\n const placeholders = [\n { placeholder: '{{output-formatting-requirements}}', content: instructionContent.outputFormattingRequirements },\n { placeholder: '{{tag-instructions}}', content: instructionContent.tagInstructions },\n { placeholder: '{{complete-example-instruction-line}}', content: instructionContent.completeExampleInstructionLine },\n { placeholder: '{{json-only-imperative-instruction-line}}', content: instructionContent.jsonOnlyImperativeInstructionLine }\n ];\n\n let hasAnyPlaceholder = false;\n for (const { placeholder, content } of placeholders) {\n if (enhancedPrompt.includes(placeholder) && content) {\n console.log(`Found ${placeholder} placeholder`);\n hasAnyPlaceholder = true;\n enhancedPrompt = enhancedPrompt.replace(placeholder, content);\n }\n }\n\n // If no placeholders found, append to end\n if (!hasAnyPlaceholder) {\n console.log('No placeholders found, appending to end of prompt');\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ].filter((content) => content && content.trim().length > 0);\n\n if (allContent.length > 0) {\n enhancedPrompt = enhancedPrompt + '\\n\\n' + allContent.join('\\n\\n');\n }\n }\n }\n\n return enhancedPrompt;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate component-specific instruction content for direct component invocations\n *\n * @param tagDefinition - The tag definition with component invocation instructions\n * @param componentAgentInstructionName - The name of the instruction set to use\n * @param instructionAssistanceConfig - Optional instruction assistance config for placeholder replacement\n * @param agentInstructionFeature - Optional agent instruction feature to control which placeholders are replaced\n * @returns The component instruction content or undefined if not found\n */\nexport function generateComponentInstructionContent(\n tagDefinition: TagDefinition<TagDefinitionWidget>,\n componentAgentInstructionName: string,\n instructionAssistanceConfig?: InstructionAssistanceConfig,\n agentInstructionFeature?: AgentInstructionChatAppOverridableFeature\n): string | undefined {\n console.log('Generating component instruction content:', {\n scope: tagDefinition.scope,\n tag: tagDefinition.tag,\n componentAgentInstructionName,\n hasDirectInvocationInstructions: !!tagDefinition.componentAgentInstructionsMd,\n hasInstructionAssistanceConfig: !!instructionAssistanceConfig\n });\n\n if (!tagDefinition.componentAgentInstructionsMd) {\n // console.log('No componentAgentInstructionsMd found on tag definition');\n return undefined;\n }\n\n const instructions = tagDefinition.componentAgentInstructionsMd[componentAgentInstructionName];\n\n if (!instructions) {\n console.log(`No instructions found for componentAgentInstructionName: ${componentAgentInstructionName}`);\n console.log('Available instruction names:', Object.keys(tagDefinition.componentAgentInstructionsMd));\n return undefined;\n }\n\n console.log('Component instructions found:', {\n instructionLength: instructions.length\n });\n\n // Apply placeholder replacement if instruction assistance config is provided\n if (instructionAssistanceConfig && agentInstructionFeature) {\n return applyComponentInstructionAssistance(instructions, instructionAssistanceConfig, agentInstructionFeature);\n }\n\n return instructions;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance placeholder replacement to component instructions\n *\n * @param componentInstructions - The raw component instructions with placeholders\n * @param instructionConfig - The instruction assistance configuration\n * @param agentInstructionFeature - The agent instruction feature configuration\n * @returns The component instructions with placeholders replaced\n */\nexport function applyComponentInstructionAssistance(\n componentInstructions: string,\n instructionConfig: InstructionAssistanceConfig,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature\n): string {\n let enhancedInstructions = componentInstructions;\n\n console.log('Applying component instruction assistance:', {\n includeTypescriptBackedOutputFormattingRequirements: agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements,\n hasPlaceholder: enhancedInstructions.includes('{{typescript-backed-output-formatting-requirements}}')\n });\n\n // Replace typescript-backed-output-formatting-requirements placeholder if enabled\n if (agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements && instructionConfig.typescriptBackedOutputFormattingRequirements) {\n const placeholder = '{{typescript-backed-output-formatting-requirements}}';\n if (enhancedInstructions.includes(placeholder)) {\n console.log('Replacing typescript-backed-output-formatting-requirements placeholder');\n enhancedInstructions = enhancedInstructions.replace(placeholder, instructionConfig.typescriptBackedOutputFormattingRequirements);\n }\n }\n\n return enhancedInstructions;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Get the instruction assistance configuration from raw SSM parameters\n *\n * @param params - The raw SSM parameters\n * @returns The instruction assistance configuration\n */\nexport function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig {\n const expectedKeys = ['output-formatting-requirements', 'default-complete-example-line', 'default-json-validation-line', 'typescript-backed-output-formatting-requirements'];\n const missingKeys = expectedKeys.filter((key) => !params[key]);\n if (missingKeys.length > 0) {\n throw new Error(\n `Missing required instruction assistance parameters: ${Object.keys(params)\n .filter((key) => !expectedKeys.includes(key))\n .join(', ')}`\n );\n }\n\n return {\n outputFormattingRequirements: params['output-formatting-requirements'],\n completeExampleInstructionLine: params['default-complete-example-line'],\n jsonOnlyImperativeInstructionLine: params['default-json-validation-line'],\n typescriptBackedOutputFormattingRequirements: params['typescript-backed-output-formatting-requirements']\n };\n}\n"]}
@@ -1,22 +1,17 @@
1
1
  // src/util/instruction-assistance-utils.ts
2
2
  function generateInstructionAssistanceContent(instructionAssistanceConfig, tags, agentInstructionFeature, tagDefinitions) {
3
- console.log("Generating instruction assistance content:", {
4
- enabled: agentInstructionFeature?.enabled,
5
- includeOutputFormattingRequirements: agentInstructionFeature?.includeOutputFormattingRequirements,
6
- includeInstructionsForTags: agentInstructionFeature?.includeInstructionsForTags,
7
- completeExampleEnabled: agentInstructionFeature?.completeExampleInstructionEnabled,
8
- jsonOnlyEnabled: agentInstructionFeature?.jsonOnlyImperativeInstructionEnabled
9
- });
10
3
  let outputFormattingRequirements = "";
11
4
  let tagInstructions = "";
12
5
  let completeExampleInstructionLine = "";
13
6
  let jsonOnlyImperativeInstructionLine = "";
7
+ let typescriptBackedOutputFormattingRequirements = "";
14
8
  if (!agentInstructionFeature?.enabled) {
15
9
  return {
16
10
  outputFormattingRequirements,
17
11
  tagInstructions,
18
12
  completeExampleInstructionLine,
19
- jsonOnlyImperativeInstructionLine
13
+ jsonOnlyImperativeInstructionLine,
14
+ typescriptBackedOutputFormattingRequirements
20
15
  };
21
16
  }
22
17
  if (agentInstructionFeature.includeOutputFormattingRequirements) {
@@ -24,18 +19,43 @@ function generateInstructionAssistanceContent(instructionAssistanceConfig, tags,
24
19
  - **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.
25
20
  - **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;
26
21
  }
27
- if (agentInstructionFeature.includeInstructionsForTags && tags && tags.tagsEnabled?.length > 0) {
28
- console.log("Fetching tag definitions for instruction generation:", tags.tagsEnabled);
22
+ if (agentInstructionFeature.includeInstructionsForTags && tags) {
29
23
  if (tagDefinitions.length > 0) {
30
- const tagDictionary = tagDefinitions.filter((tagDef) => tagDef.canBeGeneratedByLlm && !tagDef.disabled).map((tagDef) => ` - ${tagDef.tagTitle}: \`${tagDef.shortTagEx}\``).join("\n");
24
+ const enabledTagIds = /* @__PURE__ */ new Set();
25
+ if (tags.tagsEnabled) {
26
+ for (const tag of tags.tagsEnabled) {
27
+ const tagId = `${tag.scope}.${tag.tag}`;
28
+ enabledTagIds.add(tagId);
29
+ }
30
+ }
31
+ const disabledTagIds = /* @__PURE__ */ new Set();
32
+ if (tags.tagsDisabled) {
33
+ for (const tag of tags.tagsDisabled) {
34
+ const tagId = `${tag.scope}.${tag.tag}`;
35
+ disabledTagIds.add(tagId);
36
+ }
37
+ }
38
+ for (const tagDef of tagDefinitions) {
39
+ if (tagDef.usageMode === "global") {
40
+ const tagId = `${tagDef.scope}.${tagDef.tag}`;
41
+ if (!disabledTagIds.has(tagId)) {
42
+ enabledTagIds.add(tagId);
43
+ }
44
+ }
45
+ }
46
+ const enabledTagDefinitions = tagDefinitions.filter((tagDef) => {
47
+ const tagId = `${tagDef.scope}.${tagDef.tag}`;
48
+ return enabledTagIds.has(tagId) && tagDef.canBeGeneratedByLlm && tagDef.status === "enabled";
49
+ });
50
+ const tagDictionary = enabledTagDefinitions.map((tagDef) => ` - ${tagDef.tagTitle}: \`${tagDef.shortTagEx}\``).join("\n");
31
51
  let tagInstructionsContent = "";
32
52
  if (tagDictionary) {
33
53
  tagInstructionsContent += `- **Custom Tags Supported:**
34
54
  ${tagDictionary}
35
55
  `;
36
56
  }
37
- for (const tagDef of tagDefinitions) {
38
- if (tagDef.canBeGeneratedByLlm && !tagDef.disabled && tagDef.llmInstructionsMd) {
57
+ for (const tagDef of enabledTagDefinitions) {
58
+ if (tagDef.llmInstructionsMd) {
39
59
  const tagType = `${tagDef.scope}.${tagDef.tag}`;
40
60
  tagInstructionsContent += `- **${tagDef.tagTitle}:**
41
61
  <tag-instructions type="${tagType}">
@@ -55,17 +75,12 @@ ${tagDef.llmInstructionsMd}
55
75
  if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {
56
76
  jsonOnlyImperativeInstructionLine = agentInstructionFeature.jsonOnlyImperativeInstructionLine || instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine || "BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.";
57
77
  }
58
- console.log("Generated instruction assistance content:", {
59
- hasOutputFormatting: !!outputFormattingRequirements,
60
- hasTagInstructions: !!tagInstructions,
61
- hasCompleteExample: !!completeExampleInstructionLine,
62
- hasJsonValidation: !!jsonOnlyImperativeInstructionLine
63
- });
64
78
  return {
65
79
  outputFormattingRequirements,
66
80
  tagInstructions,
67
81
  completeExampleInstructionLine,
68
- jsonOnlyImperativeInstructionLine
82
+ jsonOnlyImperativeInstructionLine,
83
+ typescriptBackedOutputFormattingRequirements
69
84
  };
70
85
  }
71
86
  function applyInstructionAssistance(basePrompt, instructionContent) {
@@ -109,8 +124,48 @@ function applyInstructionAssistance(basePrompt, instructionContent) {
109
124
  }
110
125
  return enhancedPrompt;
111
126
  }
127
+ function generateComponentInstructionContent(tagDefinition, componentAgentInstructionName, instructionAssistanceConfig, agentInstructionFeature) {
128
+ console.log("Generating component instruction content:", {
129
+ scope: tagDefinition.scope,
130
+ tag: tagDefinition.tag,
131
+ componentAgentInstructionName,
132
+ hasDirectInvocationInstructions: !!tagDefinition.componentAgentInstructionsMd,
133
+ hasInstructionAssistanceConfig: !!instructionAssistanceConfig
134
+ });
135
+ if (!tagDefinition.componentAgentInstructionsMd) {
136
+ return void 0;
137
+ }
138
+ const instructions = tagDefinition.componentAgentInstructionsMd[componentAgentInstructionName];
139
+ if (!instructions) {
140
+ console.log(`No instructions found for componentAgentInstructionName: ${componentAgentInstructionName}`);
141
+ console.log("Available instruction names:", Object.keys(tagDefinition.componentAgentInstructionsMd));
142
+ return void 0;
143
+ }
144
+ console.log("Component instructions found:", {
145
+ instructionLength: instructions.length
146
+ });
147
+ if (instructionAssistanceConfig && agentInstructionFeature) {
148
+ return applyComponentInstructionAssistance(instructions, instructionAssistanceConfig, agentInstructionFeature);
149
+ }
150
+ return instructions;
151
+ }
152
+ function applyComponentInstructionAssistance(componentInstructions, instructionConfig, agentInstructionFeature) {
153
+ let enhancedInstructions = componentInstructions;
154
+ console.log("Applying component instruction assistance:", {
155
+ includeTypescriptBackedOutputFormattingRequirements: agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements,
156
+ hasPlaceholder: enhancedInstructions.includes("{{typescript-backed-output-formatting-requirements}}")
157
+ });
158
+ if (agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements && instructionConfig.typescriptBackedOutputFormattingRequirements) {
159
+ const placeholder = "{{typescript-backed-output-formatting-requirements}}";
160
+ if (enhancedInstructions.includes(placeholder)) {
161
+ console.log("Replacing typescript-backed-output-formatting-requirements placeholder");
162
+ enhancedInstructions = enhancedInstructions.replace(placeholder, instructionConfig.typescriptBackedOutputFormattingRequirements);
163
+ }
164
+ }
165
+ return enhancedInstructions;
166
+ }
112
167
  function getInstructionsAssistanceConfigFromRawSsmParams(params) {
113
- const expectedKeys = ["output-formatting-requirements", "default-complete-example-line", "default-json-validation-line"];
168
+ const expectedKeys = ["output-formatting-requirements", "default-complete-example-line", "default-json-validation-line", "typescript-backed-output-formatting-requirements"];
114
169
  const missingKeys = expectedKeys.filter((key) => !params[key]);
115
170
  if (missingKeys.length > 0) {
116
171
  throw new Error(
@@ -120,10 +175,11 @@ function getInstructionsAssistanceConfigFromRawSsmParams(params) {
120
175
  return {
121
176
  outputFormattingRequirements: params["output-formatting-requirements"],
122
177
  completeExampleInstructionLine: params["default-complete-example-line"],
123
- jsonOnlyImperativeInstructionLine: params["default-json-validation-line"]
178
+ jsonOnlyImperativeInstructionLine: params["default-json-validation-line"],
179
+ typescriptBackedOutputFormattingRequirements: params["typescript-backed-output-formatting-requirements"]
124
180
  };
125
181
  }
126
182
 
127
- export { applyInstructionAssistance, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
183
+ export { applyComponentInstructionAssistance, applyInstructionAssistance, generateComponentInstructionContent, generateInstructionAssistanceContent, getInstructionsAssistanceConfigFromRawSsmParams };
128
184
  //# sourceMappingURL=instruction-assistance-utils.mjs.map
129
185
  //# sourceMappingURL=instruction-assistance-utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/util/instruction-assistance-utils.ts"],"names":[],"mappings":";AAgCO,SAAS,oCAAA,CACZ,2BAAA,EACA,IAAA,EACA,uBAAA,EACA,cAAA,EAC2B;AAC3B,EAAA,OAAA,CAAQ,IAAI,4CAAA,EAA8C;AAAA,IACtD,SAAS,uBAAA,EAAyB,OAAA;AAAA,IAClC,qCAAqC,uBAAA,EAAyB,mCAAA;AAAA,IAC9D,4BAA4B,uBAAA,EAAyB,0BAAA;AAAA,IACrD,wBAAwB,uBAAA,EAAyB,iCAAA;AAAA,IACjD,iBAAiB,uBAAA,EAAyB;AAAA,GAC7C,CAAA;AAED,EAAA,IAAI,4BAAA,GAA+B,EAAA;AACnC,EAAA,IAAI,eAAA,GAAkB,EAAA;AACtB,EAAA,IAAI,8BAAA,GAAiC,EAAA;AACrC,EAAA,IAAI,iCAAA,GAAoC,EAAA;AAExC,EAAA,IAAI,CAAC,yBAAyB,OA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CAAA;AAE1D,MAAA,IAAI,UAAA,CAAW,SAAS,CAAA,EAAG;AACvB,QAAA,cAAA,GAAiB,cAAA,GAAiB,MAAA,GAAS,UAAA,CAAW,IAAA,CAAK,MAAM,CAAA;AAAA,MACrE;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,OAAO,cAAA;AACX;AAYO,SAAS,gDAAgD,MAAA,EAA6D;AACzH,EAAA,MAAM,YAAA,GAAe,CAAC,gCAAA,EAAkC,+BAAA,EAAiC,8BAA8B,CAAA;AACvH,EAAA,MAAM,WAAA,GAAc,aAAa,MAAA,CAAO,CAAC,QAAQ,CAAC,MAAA,CAAO,GAAG,CAAC,CAAA;AAC7D,EAAA,IAAI,WAAA,CAAY,SAAS,CAAA,EAAG;AACxB,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,uDAAuD,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CACpE,OAAO,CAAC,GAAA,KAAQ,CAAC,YAAA,CAAa,SAAS,GAAG,CAAC,CAAA,CAC3C,IAAA,CAAK,IAAI,CAAC,CAAA;AAAA,KACnB;AAAA,EACJ;AAEA,EAAA,OAAO;AAAA,IACH,4BAAA,EAA8B,OAAO,gCAAgC,CAAA;AAAA,IACrE,8BAAA,EAAgC,OAAO,+BAA+B,CAAA;AAAA,IACtE,iCAAA,EAAmC,OAAO,8BAA8B;AAAA,GAC5E;AACJ","file":"instruction-assistance-utils.mjs","sourcesContent":["/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * The functions in this utility are used both by the front end svelte kit web client in the browser\n * and in the backend lambda converse functions. So, that means it needs to be able\n * to run in a browser. Do not add additional imports beyond anodine types.\n *\n * To be super clear: this is just logic to generate instruction assistance content given\n * the right inputs. It does not collect up those inputs, it is given them.\n *\n */\n\nimport type {\n AgentInstructionChatAppOverridableFeature,\n InstructionAssistanceConfig,\n TagDefinition,\n TagDefinitionWidget,\n TagsChatAppOverridableFeature\n} from '../types/chatbot/chatbot-types';\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate instruction assistance content based on enabled features\n *\n * @param instructionAssistanceConfig - The instruction assistance configuration from SSM\n * @param tags - The tags configuration from the chat app\n * @param agentInstructionFeature - The agent instruction feature configuration from the chat app\n * @returns The instruction assistance content\n */\nexport function generateInstructionAssistanceContent(\n instructionAssistanceConfig: InstructionAssistanceConfig | undefined,\n tags: TagsChatAppOverridableFeature | undefined,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature | undefined,\n tagDefinitions: TagDefinition<TagDefinitionWidget>[]\n): InstructionAssistanceConfig {\n console.log('Generating instruction assistance content:', {\n enabled: agentInstructionFeature?.enabled,\n includeOutputFormattingRequirements: agentInstructionFeature?.includeOutputFormattingRequirements,\n includeInstructionsForTags: agentInstructionFeature?.includeInstructionsForTags,\n completeExampleEnabled: agentInstructionFeature?.completeExampleInstructionEnabled,\n jsonOnlyEnabled: agentInstructionFeature?.jsonOnlyImperativeInstructionEnabled\n });\n\n let outputFormattingRequirements = '';\n let tagInstructions = '';\n let completeExampleInstructionLine = '';\n let jsonOnlyImperativeInstructionLine = '';\n\n if (!agentInstructionFeature?.enabled) {\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine\n };\n }\n\n // Generate output formatting requirements\n if (agentInstructionFeature.includeOutputFormattingRequirements) {\n outputFormattingRequirements =\n instructionAssistanceConfig?.outputFormattingRequirements ||\n `**Output Formatting Requirements:**\n- **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.\n- **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;\n }\n\n // Generate tag instructions\n if (agentInstructionFeature.includeInstructionsForTags && tags && tags.tagsEnabled?.length > 0) {\n console.log('Fetching tag definitions for instruction generation:', tags.tagsEnabled);\n\n if (tagDefinitions.length > 0) {\n // First create a dictionary listing all supported tags\n const tagDictionary = tagDefinitions\n .filter((tagDef) => tagDef.canBeGeneratedByLlm && !tagDef.disabled)\n .map((tagDef) => ` - ${tagDef.tagTitle}: \\`${tagDef.shortTagEx}\\``)\n .join('\\n');\n\n let tagInstructionsContent = '';\n if (tagDictionary) {\n tagInstructionsContent += `- **Custom Tags Supported:**\\n${tagDictionary}\\n`;\n }\n\n // Then add detailed instructions for each tag\n for (const tagDef of tagDefinitions) {\n if (tagDef.canBeGeneratedByLlm && !tagDef.disabled && tagDef.llmInstructionsMd) {\n const tagType = `${tagDef.scope}.${tagDef.tag}`;\n tagInstructionsContent += `- **${tagDef.tagTitle}:**\\n <tag-instructions type=\"${tagType}\">\\n${tagDef.llmInstructionsMd}\\n </tag-instructions>\\n`;\n }\n }\n\n if (tagInstructionsContent) {\n tagInstructions = tagInstructionsContent;\n }\n }\n }\n\n // Generate complete example instruction line\n if (agentInstructionFeature.completeExampleInstructionEnabled) {\n completeExampleInstructionLine =\n agentInstructionFeature.completeExampleInstructionLine ||\n instructionAssistanceConfig?.completeExampleInstructionLine ||\n '- **Complete Example Output:**\\n `<answer>##Example markdown\\nNormal text and an <image>http://some.url</image> and some **bold text**\\n<chart>(...)</chart></answer>`';\n }\n\n // Generate JSON validation instruction line\n if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {\n jsonOnlyImperativeInstructionLine =\n agentInstructionFeature.jsonOnlyImperativeInstructionLine ||\n instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine ||\n 'BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.';\n }\n\n console.log('Generated instruction assistance content:', {\n hasOutputFormatting: !!outputFormattingRequirements,\n hasTagInstructions: !!tagInstructions,\n hasCompleteExample: !!completeExampleInstructionLine,\n hasJsonValidation: !!jsonOnlyImperativeInstructionLine\n });\n\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine\n };\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance to the base prompt using placeholder replacement\n */\nexport function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string {\n let enhancedPrompt = basePrompt;\n\n // Check for primary placeholder first\n if (enhancedPrompt.includes('{{prompt-assistance}}')) {\n console.log('Found {{prompt-assistance}} placeholder');\n\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ]\n .filter((content) => content && content.trim().length > 0)\n .join('\\n\\n');\n\n enhancedPrompt = enhancedPrompt.replace('{{prompt-assistance}}', allContent);\n } else {\n // Look for fine-grained placeholders\n const placeholders = [\n { placeholder: '{{output-formatting-requirements}}', content: instructionContent.outputFormattingRequirements },\n { placeholder: '{{tag-instructions}}', content: instructionContent.tagInstructions },\n { placeholder: '{{complete-example-instruction-line}}', content: instructionContent.completeExampleInstructionLine },\n { placeholder: '{{json-only-imperative-instruction-line}}', content: instructionContent.jsonOnlyImperativeInstructionLine }\n ];\n\n let hasAnyPlaceholder = false;\n for (const { placeholder, content } of placeholders) {\n if (enhancedPrompt.includes(placeholder) && content) {\n console.log(`Found ${placeholder} placeholder`);\n hasAnyPlaceholder = true;\n enhancedPrompt = enhancedPrompt.replace(placeholder, content);\n }\n }\n\n // If no placeholders found, append to end\n if (!hasAnyPlaceholder) {\n console.log('No placeholders found, appending to end of prompt');\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ].filter((content) => content && content.trim().length > 0);\n\n if (allContent.length > 0) {\n enhancedPrompt = enhancedPrompt + '\\n\\n' + allContent.join('\\n\\n');\n }\n }\n }\n\n return enhancedPrompt;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Get the instruction assistance configuration from raw SSM parameters\n *\n * @param params - The raw SSM parameters\n * @returns The instruction assistance configuration\n */\nexport function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig {\n const expectedKeys = ['output-formatting-requirements', 'default-complete-example-line', 'default-json-validation-line'];\n const missingKeys = expectedKeys.filter((key) => !params[key]);\n if (missingKeys.length > 0) {\n throw new Error(\n `Missing required instruction assistance parameters: ${Object.keys(params)\n .filter((key) => !expectedKeys.includes(key))\n .join(', ')}`\n );\n }\n\n return {\n outputFormattingRequirements: params['output-formatting-requirements'],\n completeExampleInstructionLine: params['default-complete-example-line'],\n jsonOnlyImperativeInstructionLine: params['default-json-validation-line']\n };\n}\n"]}
1
+ {"version":3,"sources":["../../src/util/instruction-assistance-utils.ts"],"names":[],"mappings":";AAgCO,SAAS,oCAAA,CACZ,2BAAA,EACA,IAAA,EACA,uBAAA,EACA,cAAA,EAC2B;AAC3B,EAAA,IAAI,4BAAA,GAA+B,EAAA;AACnC,EAAA,IAAI,eAAA,GAAkB,EAAA;AACtB,EAAA,IAAI,8BAAA,GAAiC,EAAA;AACrC,EAAA,IAAI,iCAAA,GAAoC,EAAA;AACxC,EAAA,IAAI,4CAAA,GAA+C,EAAA;AAEnD,EAAA,IAAI,CAAC,yBAAyB,OAAA,EAAS;AACnC,IAAA,OAAO;AAAA,MACH,4BAAA;AAAA,MACA,eAAA;AAAA,MACA,8BAAA;AAAA,MACA,iCAAA;AAAA,MACA;AAAA,KACJ;AAAA,EACJ;AAGA,EAAA,IAAI,wBAAwB,mCAAA,EAAqC;AAC7D,IAAA,4BAAA,GACI,6BAA6B,4BAAA,IAC7B,CAAA;AAAA;AAAA,0FAAA,CAAA;AAAA,EAGR;AAGA,EAAA,IAAI,uBAAA,CAAwB,8BAA8B,IAAA,EAAM;AAC5D,IAAA,IAAI,cAAA,CAAe,SAAS,CAAA,EAAG;AAE3B,MAAA,MAAM,aAAA,uBAAoB,GAAA,EAAY;AAGtC,MAAA,IAAI,KAAK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AM,IAAI,KAAA;AAAA,MACN,uDAAuD,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CACpE,OAAO,CAAC,GAAA,KAAQ,CAAC,YAAA,CAAa,SAAS,GAAG,CAAC,CAAA,CAC3C,IAAA,CAAK,IAAI,CAAC,CAAA;AAAA,KACnB;AAAA,EACJ;AAEA,EAAA,OAAO;AAAA,IACH,4BAAA,EAA8B,OAAO,gCAAgC,CAAA;AAAA,IACrE,8BAAA,EAAgC,OAAO,+BAA+B,CAAA;AAAA,IACtE,iCAAA,EAAmC,OAAO,8BAA8B,CAAA;AAAA,IACxE,4CAAA,EAA8C,OAAO,kDAAkD;AAAA,GAC3G;AACJ","file":"instruction-assistance-utils.mjs","sourcesContent":["/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * The functions in this utility are used both by the front end svelte kit web client in the browser\n * and in the backend lambda converse functions. So, that means it needs to be able\n * to run in a browser. Do not add additional imports beyond anodine types.\n *\n * To be super clear: this is just logic to generate instruction assistance content given\n * the right inputs. It does not collect up those inputs, it is given them.\n *\n */\n\nimport type {\n AgentInstructionChatAppOverridableFeature,\n InstructionAssistanceConfig,\n TagDefinition,\n TagDefinitionWidget,\n TagsChatAppOverridableFeature\n} from '../types/chatbot/chatbot-types';\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate instruction assistance content based on enabled features\n *\n * @param instructionAssistanceConfig - The instruction assistance configuration from SSM\n * @param tags - The tags configuration from the chat app\n * @param agentInstructionFeature - The agent instruction feature configuration from the chat app\n * @returns The instruction assistance content\n */\nexport function generateInstructionAssistanceContent(\n instructionAssistanceConfig: InstructionAssistanceConfig | undefined,\n tags: TagsChatAppOverridableFeature | undefined,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature | undefined,\n tagDefinitions: TagDefinition<TagDefinitionWidget>[]\n): InstructionAssistanceConfig {\n let outputFormattingRequirements = '';\n let tagInstructions = '';\n let completeExampleInstructionLine = '';\n let jsonOnlyImperativeInstructionLine = '';\n let typescriptBackedOutputFormattingRequirements = '';\n\n if (!agentInstructionFeature?.enabled) {\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine,\n typescriptBackedOutputFormattingRequirements\n };\n }\n\n // Generate output formatting requirements\n if (agentInstructionFeature.includeOutputFormattingRequirements) {\n outputFormattingRequirements =\n instructionAssistanceConfig?.outputFormattingRequirements ||\n `**Output Formatting Requirements:**\n- **Output Response Enclosure**: All response output MUST be completely enclosed within <answer></answer> tags, including supported custom tags.\n- **Output Content Format:** All responses MUST be in Markdown with supported custom tags.`;\n }\n\n // Generate tag instructions\n if (agentInstructionFeature.includeInstructionsForTags && tags) {\n if (tagDefinitions.length > 0) {\n // Build a set of enabled tag identifiers\n const enabledTagIds = new Set<string>();\n\n // Add all explicitly enabled chat-app tags\n if (tags.tagsEnabled) {\n for (const tag of tags.tagsEnabled) {\n const tagId = `${tag.scope}.${tag.tag}`;\n enabledTagIds.add(tagId);\n }\n }\n\n // Add all global tags that aren't explicitly disabled\n const disabledTagIds = new Set<string>();\n if (tags.tagsDisabled) {\n for (const tag of tags.tagsDisabled) {\n const tagId = `${tag.scope}.${tag.tag}`;\n disabledTagIds.add(tagId);\n }\n }\n\n for (const tagDef of tagDefinitions) {\n if (tagDef.usageMode === 'global') {\n const tagId = `${tagDef.scope}.${tagDef.tag}`;\n if (!disabledTagIds.has(tagId)) {\n enabledTagIds.add(tagId);\n }\n }\n }\n\n // Filter to only include enabled tags\n const enabledTagDefinitions = tagDefinitions.filter((tagDef) => {\n const tagId = `${tagDef.scope}.${tagDef.tag}`;\n return enabledTagIds.has(tagId) && tagDef.canBeGeneratedByLlm && tagDef.status === 'enabled';\n });\n\n // First create a dictionary listing all supported tags\n const tagDictionary = enabledTagDefinitions.map((tagDef) => ` - ${tagDef.tagTitle}: \\`${tagDef.shortTagEx}\\``).join('\\n');\n\n let tagInstructionsContent = '';\n if (tagDictionary) {\n tagInstructionsContent += `- **Custom Tags Supported:**\\n${tagDictionary}\\n`;\n }\n\n // Then add detailed instructions for each tag\n for (const tagDef of enabledTagDefinitions) {\n if (tagDef.llmInstructionsMd) {\n const tagType = `${tagDef.scope}.${tagDef.tag}`;\n tagInstructionsContent += `- **${tagDef.tagTitle}:**\\n <tag-instructions type=\"${tagType}\">\\n${tagDef.llmInstructionsMd}\\n </tag-instructions>\\n`;\n }\n }\n\n if (tagInstructionsContent) {\n tagInstructions = tagInstructionsContent;\n }\n }\n }\n\n // Generate complete example instruction line\n if (agentInstructionFeature.completeExampleInstructionEnabled) {\n completeExampleInstructionLine =\n agentInstructionFeature.completeExampleInstructionLine ||\n instructionAssistanceConfig?.completeExampleInstructionLine ||\n '- **Complete Example Output:**\\n `<answer>##Example markdown\\nNormal text and an <image>http://some.url</image> and some **bold text**\\n<chart>(...)</chart></answer>`';\n }\n\n // Generate JSON validation instruction line\n if (agentInstructionFeature.jsonOnlyImperativeInstructionEnabled) {\n jsonOnlyImperativeInstructionLine =\n agentInstructionFeature.jsonOnlyImperativeInstructionLine ||\n instructionAssistanceConfig?.jsonOnlyImperativeInstructionLine ||\n 'BE ABSOLUTELY CERTAIN ANY JSON INCLUDED IS 100% VALID (especially for charts). Invalid JSON will break the user experience.';\n }\n\n return {\n outputFormattingRequirements,\n tagInstructions,\n completeExampleInstructionLine,\n jsonOnlyImperativeInstructionLine,\n typescriptBackedOutputFormattingRequirements\n };\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance to the base prompt using placeholder replacement\n */\nexport function applyInstructionAssistance(basePrompt: string, instructionContent: InstructionAssistanceConfig): string {\n let enhancedPrompt = basePrompt;\n\n // Check for primary placeholder first\n if (enhancedPrompt.includes('{{prompt-assistance}}')) {\n console.log('Found {{prompt-assistance}} placeholder');\n\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ]\n .filter((content) => content && content.trim().length > 0)\n .join('\\n\\n');\n\n enhancedPrompt = enhancedPrompt.replace('{{prompt-assistance}}', allContent);\n } else {\n // Look for fine-grained placeholders\n const placeholders = [\n { placeholder: '{{output-formatting-requirements}}', content: instructionContent.outputFormattingRequirements },\n { placeholder: '{{tag-instructions}}', content: instructionContent.tagInstructions },\n { placeholder: '{{complete-example-instruction-line}}', content: instructionContent.completeExampleInstructionLine },\n { placeholder: '{{json-only-imperative-instruction-line}}', content: instructionContent.jsonOnlyImperativeInstructionLine }\n ];\n\n let hasAnyPlaceholder = false;\n for (const { placeholder, content } of placeholders) {\n if (enhancedPrompt.includes(placeholder) && content) {\n console.log(`Found ${placeholder} placeholder`);\n hasAnyPlaceholder = true;\n enhancedPrompt = enhancedPrompt.replace(placeholder, content);\n }\n }\n\n // If no placeholders found, append to end\n if (!hasAnyPlaceholder) {\n console.log('No placeholders found, appending to end of prompt');\n const allContent = [\n instructionContent.outputFormattingRequirements,\n instructionContent.tagInstructions,\n instructionContent.completeExampleInstructionLine,\n instructionContent.jsonOnlyImperativeInstructionLine\n ].filter((content) => content && content.trim().length > 0);\n\n if (allContent.length > 0) {\n enhancedPrompt = enhancedPrompt + '\\n\\n' + allContent.join('\\n\\n');\n }\n }\n }\n\n return enhancedPrompt;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Generate component-specific instruction content for direct component invocations\n *\n * @param tagDefinition - The tag definition with component invocation instructions\n * @param componentAgentInstructionName - The name of the instruction set to use\n * @param instructionAssistanceConfig - Optional instruction assistance config for placeholder replacement\n * @param agentInstructionFeature - Optional agent instruction feature to control which placeholders are replaced\n * @returns The component instruction content or undefined if not found\n */\nexport function generateComponentInstructionContent(\n tagDefinition: TagDefinition<TagDefinitionWidget>,\n componentAgentInstructionName: string,\n instructionAssistanceConfig?: InstructionAssistanceConfig,\n agentInstructionFeature?: AgentInstructionChatAppOverridableFeature\n): string | undefined {\n console.log('Generating component instruction content:', {\n scope: tagDefinition.scope,\n tag: tagDefinition.tag,\n componentAgentInstructionName,\n hasDirectInvocationInstructions: !!tagDefinition.componentAgentInstructionsMd,\n hasInstructionAssistanceConfig: !!instructionAssistanceConfig\n });\n\n if (!tagDefinition.componentAgentInstructionsMd) {\n // console.log('No componentAgentInstructionsMd found on tag definition');\n return undefined;\n }\n\n const instructions = tagDefinition.componentAgentInstructionsMd[componentAgentInstructionName];\n\n if (!instructions) {\n console.log(`No instructions found for componentAgentInstructionName: ${componentAgentInstructionName}`);\n console.log('Available instruction names:', Object.keys(tagDefinition.componentAgentInstructionsMd));\n return undefined;\n }\n\n console.log('Component instructions found:', {\n instructionLength: instructions.length\n });\n\n // Apply placeholder replacement if instruction assistance config is provided\n if (instructionAssistanceConfig && agentInstructionFeature) {\n return applyComponentInstructionAssistance(instructions, instructionAssistanceConfig, agentInstructionFeature);\n }\n\n return instructions;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Apply instruction assistance placeholder replacement to component instructions\n *\n * @param componentInstructions - The raw component instructions with placeholders\n * @param instructionConfig - The instruction assistance configuration\n * @param agentInstructionFeature - The agent instruction feature configuration\n * @returns The component instructions with placeholders replaced\n */\nexport function applyComponentInstructionAssistance(\n componentInstructions: string,\n instructionConfig: InstructionAssistanceConfig,\n agentInstructionFeature: AgentInstructionChatAppOverridableFeature\n): string {\n let enhancedInstructions = componentInstructions;\n\n console.log('Applying component instruction assistance:', {\n includeTypescriptBackedOutputFormattingRequirements: agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements,\n hasPlaceholder: enhancedInstructions.includes('{{typescript-backed-output-formatting-requirements}}')\n });\n\n // Replace typescript-backed-output-formatting-requirements placeholder if enabled\n if (agentInstructionFeature.includeTypescriptBackedOutputFormattingRequirements && instructionConfig.typescriptBackedOutputFormattingRequirements) {\n const placeholder = '{{typescript-backed-output-formatting-requirements}}';\n if (enhancedInstructions.includes(placeholder)) {\n console.log('Replacing typescript-backed-output-formatting-requirements placeholder');\n enhancedInstructions = enhancedInstructions.replace(placeholder, instructionConfig.typescriptBackedOutputFormattingRequirements);\n }\n }\n\n return enhancedInstructions;\n}\n\n/**\n * IMPORTANT!!!!!!!!!!!!!!!!!!!!!!\n *\n * See header at top of file for important notes.\n *\n * Get the instruction assistance configuration from raw SSM parameters\n *\n * @param params - The raw SSM parameters\n * @returns The instruction assistance configuration\n */\nexport function getInstructionsAssistanceConfigFromRawSsmParams(params: Record<string, string>): InstructionAssistanceConfig {\n const expectedKeys = ['output-formatting-requirements', 'default-complete-example-line', 'default-json-validation-line', 'typescript-backed-output-formatting-requirements'];\n const missingKeys = expectedKeys.filter((key) => !params[key]);\n if (missingKeys.length > 0) {\n throw new Error(\n `Missing required instruction assistance parameters: ${Object.keys(params)\n .filter((key) => !expectedKeys.includes(key))\n .join(', ')}`\n );\n }\n\n return {\n outputFormattingRequirements: params['output-formatting-requirements'],\n completeExampleInstructionLine: params['default-complete-example-line'],\n jsonOnlyImperativeInstructionLine: params['default-json-validation-line'],\n typescriptBackedOutputFormattingRequirements: params['typescript-backed-output-formatting-requirements']\n };\n}\n"]}
@@ -58,7 +58,8 @@ function getOverridableFeatures(siteFeatures, chatApp, user) {
58
58
  websiteEnabled: false
59
59
  },
60
60
  tags: {
61
- tagsEnabled: []
61
+ tagsEnabled: [],
62
+ tagsDisabled: []
62
63
  },
63
64
  agentInstructionAssistance: {
64
65
  enabled: false,
@@ -67,7 +68,9 @@ function getOverridableFeatures(siteFeatures, chatApp, user) {
67
68
  completeExampleInstructionEnabled: false,
68
69
  completeExampleInstructionLine: void 0,
69
70
  jsonOnlyImperativeInstructionEnabled: false,
70
- jsonOnlyImperativeInstructionLine: void 0
71
+ jsonOnlyImperativeInstructionLine: void 0,
72
+ includeTypescriptBackedOutputFormattingRequirements: false,
73
+ typescriptBackedOutputFormattingRequirements: void 0
71
74
  },
72
75
  instructionAugmentation: {
73
76
  enabled: false,
@@ -155,7 +158,8 @@ function getOverridableFeatures(siteFeatures, chatApp, user) {
155
158
  result.chatDisclaimerNotice = disclaimerResult.notice;
156
159
  const effectiveTagsFeature = chatApp.override?.features?.tags || chatApp.features?.tags;
157
160
  result.tags = handleSimpleFeature("tags", effectiveTagsFeature, siteFeatures?.tags, result.tags, (feature) => ({
158
- tagsEnabled: feature.tagsEnabled ?? []
161
+ tagsEnabled: feature.tagsEnabled ?? [],
162
+ tagsDisabled: feature.tagsDisabled ?? []
159
163
  }));
160
164
  const effectiveAgentInstructionAssistanceFeature = chatApp.override?.features?.agentInstructionAssistance || chatApp.features?.agentInstructionAssistance;
161
165
  result.agentInstructionAssistance = handleSimpleFeature(
@@ -170,7 +174,9 @@ function getOverridableFeatures(siteFeatures, chatApp, user) {
170
174
  completeExampleInstructionEnabled: feature.completeExampleInstructionLine?.enabled ?? false,
171
175
  completeExampleInstructionLine: feature.completeExampleInstructionLine?.mdLine ?? void 0,
172
176
  jsonOnlyImperativeInstructionEnabled: feature.jsonOnlyImperativeInstructionLine?.enabled ?? false,
173
- jsonOnlyImperativeInstructionLine: feature.jsonOnlyImperativeInstructionLine?.line ?? void 0
177
+ jsonOnlyImperativeInstructionLine: feature.jsonOnlyImperativeInstructionLine?.line ?? void 0,
178
+ includeTypescriptBackedOutputFormattingRequirements: feature.includeTypescriptBackedOutputFormattingRequirements?.enabled ?? false,
179
+ typescriptBackedOutputFormattingRequirements: feature.typescriptBackedOutputFormattingRequirements ?? void 0
174
180
  })
175
181
  );
176
182
  const effectiveInstructionAugmentationFeature = chatApp.override?.features?.instructionAugmentation || chatApp.features?.instructionAugmentation;