{"id":53543,"date":"2024-12-03T11:29:25","date_gmt":"2024-12-03T03:29:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53543\/"},"modified":"2024-12-03T11:29:25","modified_gmt":"2024-12-03T03:29:25","slug":"%e6%90%9c%e7%b4%a2%e5%92%8c%ef%bc%88%e6%b3%95%e5%ad%a6%e7%a1%95%e5%a3%ab%ef%bc%89%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53543\/","title":{"rendered":"\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733055687674c54c722100.jpg\" class=\"aligncenter\" title=\"\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362\u63d2\u56fe\" alt=\"\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362\u63d2\u56fe\" \/><\/p>\n<p>\u672c\u6587\u5c55\u793a\u4e86\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u201c\u641c\u7d22\u548c\u66ff\u6362\u201d\u529f\u80fd\u7684\u6f14\u53d8\uff0c<br \/> \u5176\u4e2d\u201c\u66ff\u6362\u201d\u6b65\u9aa4\u5df2\u88ab llm \u8f6c\u6362\u66ff\u6362\u3002<br \/> \u8be5\u793a\u4f8b\u4f7f\u7528 genaiscript\u3002<\/p>\n<p>\u6279\u91cf\u5e94\u7528\u4f7f\u7528<br \/>\u4e0d\u5bb9\u6613\u5b8c\u6210\u7684\u6587\u672c\u8f6c\u6362\u53ef\u80fd\u5f88\u6709\u7528 \u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5728<br \/>\u4e2d\u6dfb\u52a0\u4f7f\u7528\u5b57\u7b26\u4e32\u547d\u4ee4\u7684\u529f\u80fd\u65f6 \u5bf9\u4e8e exec \u547d\u4ee4\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4f7f\u7528\u53c2\u6570\u6570\u7ec4\u7684\u6240\u6709\u8c03\u7528\u8f6c\u6362\u4e3a\u4ee5\u4e0b\u65b0\u8bed\u6cd5\uff1a<\/p>\n<pre>host.exec(\"cmd\", [\"arg0\", \"arg1\", \"arg2\"])\n<\/pre>\n<p>\u5230<\/p>\n<pre>host.exec(`cmd arg0 arg1 arg2`)`\n<\/pre>\n<p>\u867d\u7136\u53ef\u4ee5\u5c06\u6b64\u51fd\u6570\u8c03\u7528\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d<\/p>\n<pre>host\\.exec\\s*\\([^,]+,\\s*\\[[^\\]]+\\]\\s*\\)\n<\/pre>\n<p>\u5236\u5b9a\u66ff\u6362\u5b57\u7b26\u4e32\u5e76\u4e0d\u5bb9\u6613&#8230;&#8230;\u9664\u975e\u4f60\u80fd\u7528\u81ea\u7136\u8bed\u8a00\u63cf\u8ff0\u5b83\uff1a<\/p>\n<pre>convert the call to a single string command shell in typescript\n<\/pre>\n<p>\u4ee5\u4e0b\u662f llm \u6b63\u786e\u5904\u7406\u53d8\u91cf\u7684\u4e00\u4e9b\u8f6c\u6362\u793a\u4f8b\u3002<\/p>\n<ul>\n<li> \u5c06\u51fd\u6570\u8c03\u7528\u7684\u53c2\u6570\u8fde\u63a5\u6210\u5355\u4e2a\u5b57\u7b26\u4e32 <\/li>\n<\/ul>\n<pre>- const { stdout } = await host.exec(\"git\", [\"diff\"])\n+ const { stdout } = await host.exec(`git diff`)\n<\/pre>\n<ul>\n<li> \u8fde\u63a5\u53c2\u6570\u5e76\u4f7f\u7528 ${} \u8bed\u6cd5\u6765\u63d2\u5165\u53d8\u91cf <\/li>\n<\/ul>\n<pre>- const { stdout: commits } = await host.exec(\"git\", [\n-     \"log\",\n-     \"--author\",\n-     author,\n-     \"--until\",\n-     until,\n-     \"--format=oneline\",\n- ])\n+ const { stdout: commits } = \n+   await host.exec(`git log --author ${author} --until ${until} --format=oneline`)\n<\/pre>\n<h2> \u641c\u7d22 <\/h2>\n<p>\u641c\u7d22\u6b65\u9aa4\u662f\u901a\u8fc7workspace.grep<br \/>\u5b8c\u6210\u7684 \u5141\u8bb8\u6709\u6548\u5730\u641c\u7d22\u6587\u4ef6\u4e2d\u7684\u6a21\u5f0f\uff08\u8fd9\u662f\u540c\u4e00\u4e2a\u641c\u7d22\u5f15\u64ce<br \/> \u4e3a visual studio code \u641c\u7d22\u63d0\u4f9b\u652f\u6301\uff09\u3002<\/p>\n<pre>const { pattern, glob } = env.vars\nconst patternrx = new regexp(pattern, \"g\")\nconst { files } = await workspace.grep(patternrx, glob)\n<\/pre>\n<h2> \u8ba1\u7b97\u53d8\u6362 <\/h2>\n<p>\u7b2c\u4e8c\u6b65\u662f\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528\u5230\u6587\u4ef6\u5185\u5bb9<br \/> \u5e76\u4f7f\u7528\u5185\u8054\u63d0\u793a\u9884\u5148\u8ba1\u7b97\u6bcf\u573a\u6bd4\u8d5b\u7684 llm \u8f6c\u6362\u3002<\/p>\n<pre>const { transform } = env.vars\n...\nconst patches = {} \/\/ map of match -&gt; transformed\nfor (const file of files) {\n    const { content } = await workspace.readtext(file.filename)\n    for (const match of content.matchall(patternrx)) {\n        const res = await runprompt(\n            (ctx) =&gt; {\n                ctx.$`\n            ## task\n\n            your task is to transform the match with the following transform.\n            return the transformed text.\n            - do not add enclosing quotes.\n\n            ## context\n            `\n                ctx.def(\"matched\", match[0])\n                ctx.def(\"transform\", transform)\n            },\n            { label: match[0], system: [], cache: \"search-and-transform\" }\n        )\n        ...\n<\/pre>\n<p>\u7531\u4e8e llm \u6709\u65f6\u51b3\u5b9a\u5c06\u7b54\u6848\u7528\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u5176\u5220\u9664\u3002<\/p>\n<pre>    ...\n    const transformed = res.fences?.[0].content ?? res.text\n    patches[match[0]] = transformed\n<\/pre>\n<h2> \u8f6c\u6362 <\/h2>\n<p>\u6700\u540e\uff0c\u901a\u8fc7\u9884\u5148\u8ba1\u7b97\u7684\u8f6c\u6362\uff0c\u6211\u4eec\u5c06\u6700\u7ec8\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5e94\u7528\u4e8e<br \/> \u4f7f\u7528\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u4fee\u8865\u65e7\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<pre>    const newcontent = content.replace(\n        patternrx,\n        (match) =&gt; patches[match] ?? match\n    )\n    await workspace.writetext(file.filename, newcontent)\n}\n<\/pre>\n<h2> \u53c2\u6570 <\/h2>\n<p>\u8be5\u811a\u672c\u91c7\u7528\u4e09\u4e2a\u53c2\u6570\uff1a\u6587\u4ef6 glob\u3001\u8981\u641c\u7d22\u7684\u6a21\u5f0f\u4ee5\u53ca\u8981\u5e94\u7528\u7684 llm \u8f6c\u6362\u3002<br \/> \u6211\u4eec\u5728\u811a\u672c\u5143\u6570\u636e\u4e2d\u58f0\u660e\u8fd9\u4e9b\u53c2\u6570\uff0c\u5e76\u4ece env.vars \u5bf9\u8c61\u4e2d\u63d0\u53d6\u5b83\u4eec\u3002<\/p>\n<pre>script({ ...,\n    parameters: {\n        glob: {\n            type: \"string\",\n            description: \"the glob pattern to filter files\",\n            default: \"*\",\n        },\n        pattern: {\n            type: \"string\",\n            description: \"the text pattern (regular expression) to search for\",\n        },\n        transform: {\n            type: \"string\",\n            description: \"the llm transformation to apply to the match\",\n        },\n    },\n})\nconst { pattern, glob, transform } = env.vars\n<\/pre>\n<h2> \u8dd1\u6b65 <\/h2>\n<p>\u8981\u8fd0\u884c\u6b64\u811a\u672c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 &#8211;vars \u9009\u9879\u6765\u4f20\u9012\u6a21\u5f0f\u548c\u8f6c\u6362\u3002<\/p>\n<pre>genaiscript run st --vars 'pattern=host\\.exec\\s*\\([^,]+,\\s*\\[[^\\]]+\\]\\s*\\)' 'transform=Convert the call to a single string command shell in TypeScript'\n<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   WGCLOUD\u5982\u4f55\u76d1\u6d4b\u670d\u52a1\u5668\u4e1a\u52a1\u5e94\u7528\u8fd0\u884c\u72b6\u6001\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362 \u6536\u85cf \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u641c\u7d22\u548c\uff08\u6cd5\u5b66\u7855\u58eb\uff09\u8f6c\u6362\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u672c\u6587\u5c55\u793a\u4e86\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u201c\u641c\u7d22\u548c\u66ff\u6362\u201d\u529f\u80fd\u7684\u6f14\u53d8\uff0c \u5176\u4e2d\u201c\u66ff\u6362\u201d\u6b65\u9aa4\u5df2\u88ab llm \u8f6c\u6362\u66ff\u6362\u3002 \u8be5\u793a\u4f8b\u4f7f\u7528 genaiscript\u3002 \u6279\u91cf\u5e94\u7528\u4f7f\u7528\u4e0d\u5bb9\u6613\u5b8c\u6210\u7684\u6587\u672c\u8f6c\u6362\u53ef\u80fd\u5f88\u6709\u7528 \u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5728\u4e2d\u6dfb\u52a0\u4f7f\u7528\u5b57\u7b26\u4e32\u547d\u4ee4\u7684\u529f\u80fd\u65f6 \u5bf9\u4e8e exec \u547d\u4ee4\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4f7f\u7528\u53c2\u6570\u6570\u7ec4\u7684\u6240\u6709\u8c03\u7528\u8f6c\u6362\u4e3a\u4ee5\u4e0b\u65b0\u8bed\u6cd5\uff1a host.exec(&#8220;cmd&#8221;, [&#8220;arg0&#8221;, &#8220;arg1&#8221;, &#8220;arg2&#8221;]) \u5230 host.exec(`cmd arg0 arg1 arg2`)` \u867d\u7136\u53ef\u4ee5\u5c06\u6b64\u51fd\u6570\u8c03\u7528\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d host\\.exec\\s*\\([^,]+,\\s*\\[[^\\]]+\\]\\s*\\) \u5236\u5b9a\u66ff\u6362\u5b57\u7b26\u4e32\u5e76\u4e0d\u5bb9\u6613&#8230;&#8230;\u9664\u975e\u4f60\u80fd\u7528\u81ea\u7136\u8bed\u8a00\u63cf\u8ff0\u5b83\uff1a convert the call to a single string command shell in typescript \u4ee5\u4e0b\u662f llm \u6b63\u786e\u5904\u7406\u53d8\u91cf\u7684\u4e00\u4e9b\u8f6c\u6362\u793a\u4f8b\u3002 \u5c06\u51fd\u6570\u8c03\u7528\u7684\u53c2\u6570\u8fde\u63a5\u6210\u5355\u4e2a\u5b57\u7b26\u4e32 &#8211; const { stdout } = await host.exec(&#8220;git&#8221;, [&#8220;diff&#8221;]) + const { stdout [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53543","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53543"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53543\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}