( body: string, rawArgs: string, context: SkillExpandContext, )
| 174 | * shell/CLI conventions rather than any specific product. |
| 175 | */ |
| 176 | export function expandSkillParameters( |
| 177 | body: string, |
| 178 | rawArgs: string, |
| 179 | context: SkillExpandContext, |
| 180 | ): string { |
| 181 | const tokens = tokenizeArgs(rawArgs); |
| 182 | let content = body; |
| 183 | |
| 184 | for (let index = 0; index < (context.argumentNames?.length ?? 0); index++) { |
| 185 | const name = context.argumentNames?.[index]; |
| 186 | if (name === undefined) continue; |
| 187 | const escaped = regexpEscape(name); |
| 188 | content = content.replaceAll( |
| 189 | new RegExp(`\\$${escaped}(?![\\[\\w])`, 'g'), |
| 190 | escapeXmlTags(tokens[index] ?? ''), |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | content = content |
| 195 | .replaceAll(/\$ARGUMENTS\[(\d+)\]/g, (_match, indexText: string) => { |
| 196 | const index = Number.parseInt(indexText, 10); |
| 197 | return escapeXmlTags(tokens[index] ?? ''); |
| 198 | }) |
| 199 | .replaceAll(/\$(\d+)(?!\w)/g, (_match, indexText: string) => { |
| 200 | const index = Number.parseInt(indexText, 10); |
| 201 | return escapeXmlTags(tokens[index] ?? ''); |
| 202 | }) |
| 203 | .replaceAll('$ARGUMENTS', escapeXmlTags(rawArgs)); |
| 204 | |
| 205 | const hasArgumentPlaceholder = content !== body; |
| 206 | content = content |
| 207 | .replaceAll('${KIMI_SKILL_DIR}', context.skillDir) |
| 208 | .replaceAll('${KIMI_SESSION_ID}', context.sessionId ?? ''); |
| 209 | |
| 210 | if (!hasArgumentPlaceholder && rawArgs.length > 0) { |
| 211 | return `${content}\n\nARGUMENTS: ${escapeXmlTags(rawArgs)}`; |
| 212 | } |
| 213 | return content; |
| 214 | } |
| 215 | |
| 216 | export function skillArgumentNames(metadata: SkillMetadata): readonly string[] { |
| 217 | const value = metadata.arguments; |
no test coverage detected