详细分析 ▾
运行时依赖
版本
- js-literals-protocol技能首次发布。 - 引入新协议,允许LLM使用模板字面量(标签模板)语法调用本地JavaScript函数。 - 为开发者与LLM提供实现指南与使用示例。 - 强调自然JS集成、类型安全与可读性等优势。 - 记录已知限制,包括对JavaScript运行时的依赖。
安装命令
点击复制技能文档
# JavaScript Literals Protocol Tool Skill ## 概述 该技能为 LLM 建立了一种新的协议,使其能够使用 JavaScript 模板字面量语法调用工具。通过 tag function 机制,LLM 提示与本地 JavaScript 函数之间可实现无缝集成。 ## 协议定义 ### 核心概念 该协议借助 JavaScript 模板字面量与 tag function,让 LLM 能够调用本地函数。这为 LLM 与工具交互提供了一种自然、原生 JavaScript 的方式。 ### 语法格式 ``javascript // Define variables const var1 = value1; const var2 = value2; // Define tag function (tool) function toolTag(strings, ...expressions) { // Process the strings and expressions // Return the result } // LLM invokes the tool using template literal syntax const result = toolTagTemplate ${var1} with ${var2} expressions; ` ## 使用示例 ### 基础函数调用 `javascript // Define variables const person = "Mike"; const age = 28; // Define a simple tool function function describePerson(strings, personExp, ageExp) { const str0 = strings[0]; // "That " const str1 = strings[1]; // " is a " const str2 = strings[2]; // "." const ageStr = ageExp > 99 ? "centenarian" : "youngster"; return ${str0}${personExp}${str1}${ageStr}${str2}; } // LLM invokes the tool const output = describePersonThat ${person} is a ${age}.; console.log(output); // Output: That Mike is a youngster. ` ### 多参数工具调用 `javascript // Define a calculation tool function calculate(strings, ...nums) { const operation = strings.join(' ').trim(); let result; if (operation.includes('add')) { result = nums.reduce((a, b) => a + b, 0); } else if (operation.includes('multiply')) { result = nums.reduce((a, b) => a * b, 1); } return ${operation} ${nums.join(', ')} = ${result}; } // LLM invokes with multiple parameters const sum = calculateadd ${5} ${3} ${7}; const product = calculatemultiply ${2} ${4} ${6}; console.log(sum); // Output: add 5, 3, 7 = 15 console.log(product); // Output: multiply 2, 4, 6 = 48 ` ## 实现指南 ### 供开发者创建工具 1. 定义 tag function,首个参数接受 strings,其余参数接受可变表达式 2. 恰当处理模板字符串与表达式 3. 返回能为 LLM 提供有意义反馈的结果 ### 供 LLM 使用协议 1. 使用反引号包裹的模板字面量语法 2. 在 ${}` 占位符中插入变量与表达式 3. 将对应 tag function 作为模板字面量的前缀调用 ## 优势 1. JavaScript 原生:使用开发者熟悉的现有 JavaScript 语法 2. 自然集成:与 JavaScript 代码无缝融合 3. 类型安全:利用 JavaScript 的类型系统 4. 灵活:支持多参数与复杂逻辑 5. 可读:提供清晰、自解释的工具调用 ## 局限 1. 需要 JavaScript 运行时环境 2. 模板字面量语法在某些 LLM 提示中可能需要特殊处理 3. 受限于 JavaScript 函数能力 ## 示例工作流 1. 开发者使用 tag function 模式定义本地工具函数 2. 指示 LLM 使用 JavaScript 字面量协议 3. LLM 生成对已定义工具的模板字面量调用 4. 系统执行工具调用并将结果返回给 LLM 5. LLM 根据结果继续交互