cc-ding 0.4.0 → 1.0.0
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.
- package/dist/bin/cc-ding.js +5 -5
- package/dist/src/biz/api-key-manager.js +3 -3
- package/dist/src/biz/cc-ding-cli.js +65 -66
- package/dist/src/biz/claude-process.js +33 -32
- package/dist/src/biz/claude-sdk.js +3 -2
- package/dist/src/biz/command-route.js +1 -1
- package/dist/src/biz/commands.js +6 -6
- package/dist/src/biz/cron.js +6 -6
- package/dist/src/biz/doctor.js +2 -2
- package/dist/src/biz/image.js +8 -8
- package/dist/src/biz/lock.js +1 -1
- package/dist/src/biz/menu.js +2 -2
- package/dist/src/biz/messaging.js +5 -5
- package/dist/src/biz/notify.js +1 -1
- package/dist/src/biz/platform.js +2 -2
- package/dist/src/biz/quote.js +5 -5
- package/dist/src/biz/recorder.js +9 -9
- package/dist/src/biz/secrets.js +1 -1
- package/dist/src/biz/session.js +13 -13
- package/dist/src/biz/task.js +23 -22
- package/dist/src/biz/todo.js +6 -6
- package/dist/src/common.js +1 -1
- package/dist/src/constants.js +1 -1
- package/package.json +4 -5
package/dist/src/biz/task.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
${
|
|
3
|
-
`)}`)}else
|
|
4
|
-
\u65E0`);if(e.length>0){const
|
|
5
|
-
${
|
|
6
|
-
`)}`)}else
|
|
7
|
-
\u65E0`);const
|
|
8
|
-
${
|
|
9
|
-
`)}`)}else
|
|
10
|
-
\u65E0`);if(
|
|
11
|
-
${
|
|
12
|
-
`)}`)}return
|
|
1
|
+
"use strict";var X=exports&&exports.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.notifyTaskQueue=Y,exports.formatTaskInfo=lt,exports.countTodoTask=ft,exports.getOneTodoTask=z,exports.finishTask=Z,exports.cancelTask=dt,exports.parseTaskCancelCommand=pt,exports.handleTask=tt,exports.runTaskHandlerLoop=gt,exports.saveTask=$t;const p=require("utils-ok"),f=X(require("fs")),M=X(require("path")),ot=require("../common"),E=require("./messaging"),B=require("./claude-process"),it=require("./claude-sdk"),s=require("./session"),y=require("./api-key-manager"),L=require("./platform"),q=3,rt=1e4,at=6e4,b=[];function Y(){for(;b.length>0;)b.shift()()}function ct(t){return new Promise(e=>{const i=()=>{clearTimeout(o),e()},o=setTimeout(()=>{const c=b.indexOf(i);c!==-1&&b.splice(c,1),e()},t);o.unref?.(),b.push(i)})}function lt(t){const e=[],i=[],o=[],c=[];for(const r of t.config.conversations){const a=(0,s.getTasksDir)(t,r.conversationId);if(!f.default.existsSync(a))continue;const m=p.fileUtil.dirWalker(a);for(const d of m){const l=M.default.basename(d);try{const S=p.fileUtil.getJSON(d);if(!S)continue;l==="task.json"?e.push(S):l==="task-doing.json"?i.push(S):l==="task-done.json"?o.push(S):l==="task-failed.json"&&c.push(S)}catch{}}}e.sort((r,a)=>r.startTime-a.startTime),i.sort((r,a)=>r.startTime-a.startTime),o.sort((r,a)=>a.startTime-r.startTime),c.sort((r,a)=>a.startTime-r.startTime);const u=[];if(i.length>0){const r=i.map(a=>`#${a.startTime} ${a.senderNickName||a.senderStaffId}: ${F(a)}`);u.push(`**\u23F3 \u5904\u7406\u4E2D (${i.length})**
|
|
2
|
+
${r.join(`
|
|
3
|
+
`)}`)}else u.push(`**\u23F3 \u5904\u7406\u4E2D**
|
|
4
|
+
\u65E0`);if(e.length>0){const r=e.map(a=>`#${a.startTime} ${a.senderNickName||a.senderStaffId}: ${F(a)}`);u.push(`**\u{1F4CB} \u5F85\u529E\u961F\u5217 (${e.length})**
|
|
5
|
+
${r.join(`
|
|
6
|
+
`)}`)}else u.push(`**\u{1F4CB} \u5F85\u529E\u961F\u5217**
|
|
7
|
+
\u65E0`);const n=o.slice(0,5);if(n.length>0){const r=n.map(a=>`#${a.startTime} ${a.senderNickName||a.senderStaffId}: ${F(a)}`);u.push(`**\u2705 \u6700\u8FD1\u5B8C\u6210 (top ${n.length}/${o.length})**
|
|
8
|
+
${r.join(`
|
|
9
|
+
`)}`)}else u.push(`**\u2705 \u6700\u8FD1\u5B8C\u6210**
|
|
10
|
+
\u65E0`);if(c.length>0){const r=c.map(a=>`#${a.startTime} ${a.senderNickName||a.senderStaffId}: ${F(a)}${a.retryCount?` (\u91CD\u8BD5${a.retryCount}\u6B21)`:""}`);u.push(`**\u274C \u5931\u8D25 (${c.length})**
|
|
11
|
+
${r.join(`
|
|
12
|
+
`)}`)}return u.join(`
|
|
13
13
|
|
|
14
|
-
`)}function
|
|
15
|
-
`),
|
|
16
|
-
\u539F\u56E0: ${
|
|
17
|
-
`),
|
|
18
|
-
...\u5171\u53D6\u6D88 ${
|
|
19
|
-
${
|
|
20
|
-
`);return
|
|
21
|
-
`);return
|
|
22
|
-
`);return
|
|
23
|
-
`);
|
|
14
|
+
`)}function ut(t,e=80){const i=t.replace(/\n/g," ").trim();return i.length>e?i.slice(0,e)+"...":i}function F(t){return t.title?t.title:ut(t.prompt)}async function mt(t,e,i){const o=(0,s.getConversationDir)(t,e),c=(0,B.resolveClaudeSettingsPath)(t,o),u=["\u8BF7\u5BF9\u4EE5\u4E0B\u4EFB\u52A1\u9700\u6C42\u8FDB\u884C\u9884\u5904\u7406\uFF0C\u751F\u6210\u7B80\u77ED\u6807\u9898\u548C\u4F18\u5316\u540E\u7684\u9700\u6C42\u63CF\u8FF0\u3002","\u76F4\u63A5\u8FD4\u56DEJSON\uFF0C\u4E0D\u8981markdown\u4EE3\u7801\u5757\u6216\u5176\u4ED6\u5185\u5BB9\u3002",'\u683C\u5F0F: {"title":"\u7B80\u77ED\u6807\u9898(15\u5B57\u4EE5\u5185)","promptSimply":"\u4F18\u5316\u540E\u7684\u9700\u6C42\u63CF\u8FF0\uFF0C\u4FDD\u7559\u539F\u59CB\u9700\u6C42\u7684\u6240\u6709\u5173\u952E\u4FE1\u606F\uFF0C\u4F7F\u63CF\u8FF0\u66F4\u6E05\u6670\u3001\u7ED3\u6784\u5316"}',"",`\u539F\u59CB\u9700\u6C42: ${i}`].join(`
|
|
15
|
+
`),n=await(0,it.runOneShotPrompt)(u,{cwd:o,settingsPath:c,timeoutMs:3e4});if(!n.ok||!n.text.trim()){const r=n.timedOut?"\u8D85\u65F6":n.errorOutput.trim().substring(0,100)||"\u65E0\u8F93\u51FA";return console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406\u5931\u8D25(${r})\uFF0C\u4F7F\u7528\u539F\u59CBprompt`),null}try{let r=n.text.trim();const a=r.match(/\{[\s\S]*\}/);a&&(r=a[0]);const m=JSON.parse(r);return m.title&&m.promptSimply?(console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406\u5B8C\u6210: title="${m.title}"`),{title:String(m.title),promptSimply:String(m.promptSimply)}):(console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406\u8FD4\u56DE\u683C\u5F0F\u4E0D\u6B63\u786E\uFF0C\u4F7F\u7528\u539F\u59CBprompt`),null)}catch(r){return console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406JSON\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBprompt: ${r}`),null}}function ft(t){let e=0;for(const i of t.config.conversations){const o=(0,s.getTasksDir)(t,i.conversationId);if(f.default.existsSync(o)){const u=p.fileUtil.dirWalker(o).filter(n=>M.default.basename(n)==="task.json");e+=u.length}}return e}async function z(t){for(const e of t.config.conversations){const i=(0,s.getTasksDir)(t,e.conversationId);if(!f.default.existsSync(i))continue;const c=p.fileUtil.dirWalker(i).filter(n=>M.default.basename(n)==="task.json");if(c.length===0)continue;const u=c.map(n=>{try{const r=p.fileUtil.getJSON(n);return r?{file:n,task:r}:null}catch{return console.error(`\u4EFB\u52A1\u6587\u4EF6\u89E3\u6790\u5931\u8D25\uFF0C\u8DF3\u8FC7: ${n}`),null}}).filter(n=>n!==null).sort((n,r)=>n.task.startTime-r.task.startTime);if(u.length>0){const{file:n,task:r}=u[0];return ot.utils.addSuffixToFile(n,"-doing"),r}}}async function Z(t,e){const i=`${e}/task-doing.json`;let o;try{o=p.fileUtil.getJSON(i)}catch(r){console.error(`\u8BFB\u53D6\u4EFB\u52A1\u6587\u4EF6\u5931\u8D25: ${i}`,r);return}const c=`${e}/result.md`,u=f.default.existsSync(c)?p.fileUtil.getFileStr(c):"\u62B1\u6B49,\u4EFB\u52A1\u5904\u7406\u5F02\u5E38...";await(0,E.sendDingMessage)(t,{conversationId:o.conversationId,sessionWebhook:o.sessionWebhook,atUserId:o.senderStaffId,content:u,msgType:"markdown"});const n=i.replace("-doing.json","-done.json");p.fileUtil.rename(i,n)}async function x(t,e,i){const o=`${e}/task-doing.json`;let c;try{c=p.fileUtil.getJSON(o)}catch(n){console.error(`\u8BFB\u53D6\u4EFB\u52A1\u6587\u4EF6\u5931\u8D25: ${o}`,n);return}await(0,E.sendDingMessage)(t,{conversationId:c.conversationId,sessionWebhook:c.sessionWebhook,atUserId:c.senderStaffId,content:`\u274C \u4EFB\u52A1\u5931\u8D25: ${F(c)}
|
|
16
|
+
\u539F\u56E0: ${i}`,msgType:"markdown"});const u=o.replace("-doing.json","-failed.json");p.fileUtil.rename(o,u)}async function H(t,e,i){const o=`${e}/task-doing.json`,c=`${e}/task.json`;if(!f.default.existsSync(o))return!1;try{const n=p.fileUtil.getJSON(o),r=(n.retryCount||0)+1;if(r>=q)return console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u91CD\u8BD5\u6B21\u6570\u5DF2\u8FBE\u4E0A\u9650(${r}/${q})\uFF0C\u6807\u8BB0\u4E3A\u5931\u8D25: ${n.title||n.prompt}`),n.retryCount=r,f.default.writeFileSync(o,JSON.stringify(n,null,2),"utf-8"),await x(t,e,`${i} (\u5DF2\u91CD\u8BD5${r}\u6B21)`),!1;n.retryCount=r,f.default.writeFileSync(o,JSON.stringify(n,null,2),"utf-8"),console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u91CD\u7F6E\u4E3A\u5F85\u529E (\u91CD\u8BD5${r}/${q}): ${n.title||n.prompt}`)}catch(n){console.error(`[${(0,s.timestamp)()}] \u66F4\u65B0\u4EFB\u52A1\u91CD\u8BD5\u8BA1\u6570\u5931\u8D25:`,n)}return f.default.existsSync(o)&&p.fileUtil.rename(o,c),setTimeout(()=>Y(),rt).unref?.(),!0}function dt(t,e,i){const o=[],c=e.trim(),u=i?t.config.conversations.filter(m=>m.conversationId===i):t.config.conversations;for(const m of u){const d=(0,s.getTasksDir)(t,m.conversationId);if(!f.default.existsSync(d))continue;const S=p.fileUtil.dirWalker(d).filter($=>M.default.basename($)==="task.json");for(const $ of S)try{const T=p.fileUtil.getJSON($);if(!T)continue;if(String(T.startTime)===c){o.push({file:$,task:T});continue}if(T.title&&T.title.includes(c)){o.push({file:$,task:T});continue}}catch{}}if(o.length===0)return`\u672A\u627E\u5230\u5339\u914D\u7684\u5F85\u529E\u4EFB\u52A1: ${c}`;let n=0;for(const{file:m,task:d}of o)try{f.default.unlinkSync(m),n++,console.log(`[${(0,s.timestamp)()}] \u5DF2\u53D6\u6D88\u4EFB\u52A1: #${d.startTime} ${d.title||d.prompt}`)}catch(l){console.error(`[${(0,s.timestamp)()}] \u53D6\u6D88\u4EFB\u52A1\u5931\u8D25: ${m}`,l)}const r=o.slice(0,5).map(m=>`#${m.task.startTime} ${F(m.task)}`).join(`
|
|
17
|
+
`),a=n>5?`
|
|
18
|
+
...\u5171\u53D6\u6D88 ${n} \u4E2A\u4EFB\u52A1`:"";return`\u5DF2\u53D6\u6D88 ${n} \u4E2A\u5F85\u529E\u4EFB\u52A1:
|
|
19
|
+
${r}${a}`}function pt(t){const i=t.trim().match(/^\/task\s+cancel\s+(.+)$/i);return i?i[1].trim():null}function R(t,e){const i=t.indexOf("--settings");i!==-1&&t.splice(i,2),e&&t.push("--settings",e)}async function tt(t){const e=await z(t);if(!e)return(0,s.debugLog)(t,"\u672A\u53D1\u73B0\u5F85\u529E\u4EFB\u52A1..."),!1;const i=(0,s.getConversationConfig)(t,e.conversationId),o=(0,s.getConversationDir)(t,e.conversationId),c=`${(0,s.getTasksDir)(t,e.conversationId)}/${e.startTimeStr}`;if(!f.default.existsSync(c))return console.error(`\u4EFB\u52A1\u76EE\u5F55\u4E0D\u5B58\u5728: ${c}`),!0;e.type!=="cron"&&await(0,E.sendDingMessage)(t,{conversationId:e.conversationId,sessionWebhook:e.sessionWebhook,atUserId:e.senderStaffId,content:`\u{1F504} \u5F00\u59CB\u5904\u7406\u4EFB\u52A1: ${F(e)}`});const u=`${c}/result.md`,n=`${c}/task.log`;console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u5904\u7406\u4E2D: ${e.title||e.prompt}`);const r=e.senderNickName&&e.senderStaffId?`${e.senderNickName}(${e.senderStaffId}), `:"unknown",a=t.config.apiKeyCfg,m=(0,y.getForceEnabledSettingsPath)(o);let d=!1,l=null;if(m)console.log(`[${(0,s.timestamp)()}] \u68C0\u6D4B\u5230 FORCE_ENABLE\uFF0C\u5F3A\u5236\u4F7F\u7528 settings-ding.json`);else if(a){if(l=(0,y.pickValidApiKey)(t),!l)return console.log(`[${(0,s.timestamp)()}] \u65E0\u53EF\u7528\u914D\u989D\uFF0C\u4EFB\u52A1\u91CD\u7F6E\u4E3A\u5F85\u529E`),await H(t,c,"\u65E0\u53EF\u7528\u914D\u989D"),!1;d=!0,console.log(`[${(0,s.timestamp)()}] \u5207\u6362\u5230 API Key \u6A21\u5F0F`)}const S=i?.taskCfg?.skill,$=i?.agent,T=e.promptSimply||e.prompt,O=`${S?`/${S}`:""} \u7528\u6237: ${r}, \u9700\u6C42: ${T}; \u6700\u540E\u5C06\u56DE\u590D\u5185\u5BB9\u4FDD\u5B58\u81F3: ${u}`.trim(),g=["--permission-mode",i?.permissionMode||"acceptEdits","--print",O];if(m)g.push("--settings",m);else if(d&&l){const k=(0,y.ensureSettingsWithApiKey)(o,l);g.push("--settings",k)}else if(!a){const k=M.default.join(o,".claude","settings.json");f.default.existsSync(k)&&(g.push("--settings",k),console.log(`[${(0,s.timestamp)()}] \u81EA\u52A8\u68C0\u6D4B\u5230 settings.json: ${k}`))}$&&g.push("--agent",$),(0,s.debugLog)(t,`\u6267\u884C\u547D\u4EE4: claude ${g.join(" ")}`);const A=20,P=1e4,Q=1e4,K=300*1e3,et=60*1e3,_="claude";if(!(0,L.commandExists)(_)){const k=(0,L.formatClaudeCommandMissingMessage)(_),h=[`[${(0,s.timestamp)()}] \u6267\u884C\u547D\u4EE4: ${_} ${g.join(" ")}`,`[${(0,s.timestamp)()}] \u9000\u51FA\u7801: 127`,k].join(`
|
|
20
|
+
`);return f.default.writeFileSync(n,h),await x(t,c,k),!0}const nt=k=>{const h=Date.now();return new Promise(w=>{const D=[];let J=!1,V=Date.now();const U=(0,L.spawnCommand)(_,k,{cwd:o,stdio:["ignore","pipe","pipe"]}),W=setInterval(()=>{if(J){clearInterval(W);return}Date.now()-V>=K&&(console.warn(`[${(0,s.timestamp)()}] \u4EFB\u52A1 Watchdog: ${K/1e3}s \u65E0\u65E5\u5FD7\u8F93\u51FA\uFF0C\u901A\u77E5\u7528\u6237`),clearInterval(W),(0,E.sendDingMessage)(t,{conversationId:e.conversationId,sessionWebhook:e.sessionWebhook,atUserId:e.senderStaffId,content:`\u23F0 \u4EFB\u52A1\u8D85\u8FC7 ${K/1e3}s \u65E0\u54CD\u5E94\uFF0C\u4ECD\u5728\u6267\u884C\u4E2D\uFF0C\u8BF7\u7A0D\u5019`}).catch(N=>console.error("\u53D1\u9001\u4EFB\u52A1 Watchdog \u901A\u77E5\u5931\u8D25:",N)))},et),G=()=>{V=Date.now()};U.stdout.on("data",v=>{const N=v.toString();process.stdout.write(N),D.push(N),G()}),U.stderr.on("data",v=>{const N=v.toString();process.stderr.write(N),D.push(N),G()}),U.on("close",v=>{J=!0,clearInterval(W),w({exitCode:v??1,output:D.join(""),elapsed:Date.now()-h})}),U.on("error",v=>{J=!0,clearInterval(W),console.error("\u8FDB\u7A0B\u6267\u884C\u9519\u8BEF:",v),D.push(`\u8FDB\u7A0B\u6267\u884C\u9519\u8BEF: ${v.message}`),w({exitCode:1,output:D.join(""),elapsed:Date.now()-h})})})};let I,C,j=0;for(;;){const k=await nt(g);if(I=k.exitCode,C=k.output,I!==0){if((0,y.isQuotaExhaustedError)(C)&&a){if(d){if(l){const w=(0,y.rotateApiKey)(t,l.apiKey);if(w){l=w,j=0;const D=(0,y.ensureSettingsWithApiKey)(o,l);R(g,D),console.log(`[${(0,s.timestamp)()}] API Key \u914D\u989D\u8017\u5C3D(429)\uFF0C\u5207\u6362\u5230\u65B0 Key: ${(0,y.settingLabel)(w)}`);continue}}}else if(l=(0,y.pickValidApiKey)(t),l){d=!0,j=0;const w=(0,y.ensureSettingsWithApiKey)(o,l);R(g,w),console.log(`[${(0,s.timestamp)()}] \u5207\u6362\u5230 API Key \u6A21\u5F0F`);continue}console.log(`[${(0,s.timestamp)()}] \u65E0\u53EF\u7528\u914D\u989D\uFF0C\u4EFB\u52A1\u91CD\u7F6E\u4E3A\u5F85\u529E`);const h=[`[${(0,s.timestamp)()}] \u6267\u884C\u547D\u4EE4: ${_} ${g.join(" ")}`,`[${(0,s.timestamp)()}] \u9000\u51FA\u7801: ${I}`,C].join(`
|
|
21
|
+
`);return f.default.writeFileSync(n,h),await H(t,c,"\u65E0\u53EF\u7528\u914D\u989D(429)"),!1}if((0,y.isAuthenticationError)(C)){console.log(`[${(0,s.timestamp)()}] \u68C0\u6D4B\u5230\u8BA4\u8BC1\u9519\u8BEF(401)\uFF0C\u6807\u8BB0\u4EFB\u52A1\u5931\u8D25`);const h=[`[${(0,s.timestamp)()}] \u6267\u884C\u547D\u4EE4: ${_} ${g.join(" ")}`,`[${(0,s.timestamp)()}] \u9000\u51FA\u7801: ${I}`,C].join(`
|
|
22
|
+
`);return f.default.writeFileSync(n,h),await x(t,c,"\u8BA4\u8BC1\u5931\u8D25(401)\uFF0CAPI Key \u65E0\u6548\u6216\u670D\u52A1\u672A\u6388\u6743"),!0}if((0,B.isRetryableApiError)(C)){if(k.elapsed<Q&&d&&l&&a){if(j++,j>=A){const h=(0,y.rotateApiKey)(t,l.apiKey);if(h){l=h,j=0;const w=(0,y.ensureSettingsWithApiKey)(o,l);R(g,w),console.log(`[${(0,s.timestamp)()}] TPM \u9650\u6D41\u8FDE\u7EED\u5FEB\u901F\u5931\u8D25 ${A} \u6B21\uFF0C\u5207\u6362\u5230\u65B0 Key: ${(0,y.settingLabel)(h)}`),await p.asyncUtil.sleep(P);continue}}}else if(k.elapsed<Q){if(j++,j>=A){console.log(`[${(0,s.timestamp)()}] TPM \u9650\u6D41\u5FEB\u901F\u5931\u8D25\u8FDE\u7EED${A}\u6B21\uFF0C\u91CD\u7F6E\u4EFB\u52A1\u4E3A\u5F85\u529E`);const h=[`[${(0,s.timestamp)()}] \u6267\u884C\u547D\u4EE4: ${_} ${g.join(" ")}`,`[${(0,s.timestamp)()}] \u9000\u51FA\u7801: ${I}`,C].join(`
|
|
23
|
+
`);return f.default.writeFileSync(n,h),await H(t,c,"TPM\u9650\u6D41\u5FEB\u901F\u5931\u8D25\u6B21\u6570\u8FC7\u591A"),!1}console.log(`[${(0,s.timestamp)()}] \u68C0\u6D4B\u5230 TPM \u9650\u6D41(\u5FEB\u901F\u5931\u8D25)\uFF0C${P/1e3}s \u540E\u91CD\u8BD5 (${j}/${A})`)}else j=0,console.log(`[${(0,s.timestamp)()}] \u68C0\u6D4B\u5230 TPM \u9650\u6D41(\u8FDB\u7A0B\u5DF2\u8FD0\u884C\u4E00\u6BB5\u65F6\u95F4)\uFF0C\u91CD\u7F6E\u5FEB\u901F\u5931\u8D25\u8BA1\u6570\uFF0C${P/1e3}s \u540E\u91CD\u8BD5`);await p.asyncUtil.sleep(P);continue}}break}const st=[`[${(0,s.timestamp)()}] \u6267\u884C\u547D\u4EE4: ${_} ${g.join(" ")}`,`[${(0,s.timestamp)()}] \u9000\u51FA\u7801: ${I}`,C].join(`
|
|
24
|
+
`);if(f.default.writeFileSync(n,st),I!==0)return console.error(`\u547D\u4EE4\u6267\u884C\u5931\u8D25, \u9000\u51FA\u7801: ${I}`),await x(t,c,`\u6267\u884C\u5931\u8D25(\u9000\u51FA\u7801: ${I})`),!0;if(f.default.existsSync(u))console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u5904\u7406\u5B8C\u6210`);else return console.error("\u4EFB\u52A1\u672A\u6309\u9884\u671F\u5904\u7406"),await x(t,c,"\u672A\u751F\u6210\u7ED3\u679C\u6587\u4EF6"),!0;return await Z(t,c),!0}async function gt(t){for(;;)await tt(t).catch(i=>(console.error(i),!1))||await ct(at)}async function $t(t,e){const{conversationId:i,prompt:o,senderStaffId:c,senderNickName:u,sessionWebhook:n,type:r="normal"}=e,a=Date.now(),m=p.dateUtil.mm(a).format("YYYY-MM-DD-HH-mm-ss"),d=`${(0,s.getTasksDir)(t,i)}/${m}`,l=`${d}/task.json`,S={conversationId:i,startTime:a,startTimeStr:m,prompt:o,senderStaffId:c,senderNickName:u||"",sessionWebhook:n,type:r};await p.fileUtil.saveFileStr(JSON.stringify(S,null,2),l),(0,s.debugLog)(t,`\u4EFB\u52A1\u5DF2\u4FDD\u5B58: ${l}`),Y(),mt(t,i,o).then($=>{if($)try{const T=`${d}/task-doing.json`,O=f.default.existsSync(T)?T:f.default.existsSync(l)?l:null;if(!O){console.log(`[${(0,s.timestamp)()}] \u9884\u5904\u7406\u5B8C\u6210\u4F46\u4EFB\u52A1\u6587\u4EF6\u5DF2\u4E0D\u5B58\u5728: ${d}`);return}const g=p.fileUtil.getJSON(O);g.title=$.title,g.promptSimply=$.promptSimply,f.default.writeFileSync(O,JSON.stringify(g,null,2),"utf-8"),console.log(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406\u7ED3\u679C\u5DF2\u66F4\u65B0: ${$.title}`)}catch(T){console.error(`[${(0,s.timestamp)()}] \u66F4\u65B0\u4EFB\u52A1\u9884\u5904\u7406\u7ED3\u679C\u5931\u8D25:`,T)}}).catch($=>{console.error(`[${(0,s.timestamp)()}] \u4EFB\u52A1\u9884\u5904\u7406\u5F02\u5E38:`,$)})}
|
package/dist/src/biz/todo.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var k=exports&&exports.__importDefault||function(n){return n&&n.__esModule?n:{default:n}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.TodoEngine=void 0,exports.loadTodoData=m,exports.getSortedTodoItems=C,exports.addTodoItem=j,exports.doneTodoItem=b,exports.deleteTodoItem=F,exports.clearAllTodoItems=H,exports.getReminderHour=x,exports.setReminderHour=O,exports.getIdMode=R,exports.setIdMode=E,exports.formatDate=p,exports.getDefaultDeadline=q,exports.parseDeadline=U,exports.formatDeadlineDisplay=y,exports.formatTodoList=L,exports.formatTodoItemCreated=W;const M=k(require("fs")),N=require("utils-ok"),h=require("./session"),A=require("./messaging");function v(n){return`${(0,h.getClientDir)(n)}/todo.json`}function m(n){const e=v(n);try{const o=M.default.readFileSync(e,"utf-8"),t=JSON.parse(o);return{conversations:t.conversations&&typeof t.conversations=="object"?t.conversations:{},reminders:t.reminders&&typeof t.reminders=="object"?t.reminders:{},idModes:t.idModes&&typeof t.idModes=="object"?t.idModes:{}}}catch{return{conversations:{},reminders:{},idModes:{}}}}function $(n,e){const o=v(n);M.default.writeFileSync(o,JSON.stringify(e,null,2),"utf-8")}function S(n){const e=n.filter(t=>!t.completed).sort(Y),o=n.filter(t=>t.completed).sort((t,s)=>(s.completedAt||"").localeCompare(t.completedAt||""));return[...e,...o]}function C(n,e){const o=m(n);return S(o.conversations[e]||[])}function Y(n,e){return!n.deadline&&!e.deadline?0:n.deadline?e.deadline?n.deadline.localeCompare(e.deadline):-1:1}function j(n,e,o){const t=m(n);t.conversations[e]||(t.conversations[e]=[]);const s={content:o.content,assigneeStaffId:o.assigneeStaffId,assigneeNick:o.assigneeNick,deadline:o.deadline,createdAt:N.dateUtil.mm(Date.now()).format("YYYY-MM-DD HH:mm:ss"),completed:!1,assigneeIdType:o.assigneeIdType||"staffId"};return t.conversations[e].push(s),$(n,t),console.log(`[${(0,h.timestamp)()}] [todo] \u6DFB\u52A0\u5F85\u529E in ${e}: ${o.content}`),s}function b(n,e,o){const t=m(n),s=t.conversations[e]||[],r=S(s);if(r.length===0)return{success:!1,error:"\u5F53\u524D\u65E0\u5F85\u529E\u4E8B\u9879"};if(o<1||o>r.length)return{success:!1,error:`\u5E8F\u53F7\u65E0\u6548\uFF0C\u8303\u56F4 1-${r.length}`};const i=r[o-1];if(i.completed)return{success:!1,error:`#${o} \u5DF2\u5B8C\u6210`};const a=s.findIndex(l=>l.content===i.content&&l.createdAt===i.createdAt&&l.assigneeStaffId===i.assigneeStaffId);return a===-1?{success:!1,error:"\u672A\u627E\u5230\u5F85\u529E\u9879"}:(s[a].completed=!0,s[a].completedAt=(0,h.timestamp)(),$(n,t),console.log(`[${(0,h.timestamp)()}] [todo] \u5B8C\u6210\u5F85\u529E in ${e}: ${i.content}`),{success:!0,item:i})}function F(n,e,o){const t=m(n),s=t.conversations[e]||[],r=S(s);if(r.length===0)return{success:!1,error:"\u5F53\u524D\u65E0\u5F85\u529E\u4E8B\u9879"};if(o<1||o>r.length)return{success:!1,error:`\u5E8F\u53F7\u65E0\u6548\uFF0C\u8303\u56F4 1-${r.length}`};const i=r[o-1],a=s.findIndex(u=>u.content===i.content&&u.createdAt===i.createdAt&&u.assigneeStaffId===i.assigneeStaffId);if(a===-1)return{success:!1,error:"\u672A\u627E\u5230\u5F85\u529E\u9879"};const[l]=s.splice(a,1);return s.length===0&&delete t.conversations[e],$(n,t),console.log(`[${(0,h.timestamp)()}] [todo] \u5220\u9664\u5F85\u529E in ${e}: ${l.content}`),{success:!0,item:l}}function H(n,e){const o=m(n),t=o.conversations[e]||[];if(t.length===0)return{success:!1,count:0,error:"\u5F53\u524D\u65E0\u5F85\u529E\u4E8B\u9879"};const s=t.length;return delete o.conversations[e],$(n,o),console.log(`[${(0,h.timestamp)()}] [todo] \u6E05\u7A7A\u5F85\u529E in ${e}: ${s} \u6761`),{success:!0,count:s}}function x(n,e){const t=m(n).reminders?.[e];return t===!1?null:typeof t=="number"?t:10}function O(n,e,o){const t=m(n);t.reminders||(t.reminders={}),o===null?t.reminders[e]=!1:t.reminders[e]=o,$(n,t)}function R(n,e){return m(n).idModes?.[e]||"staffId"}function E(n,e,o){const t=m(n);t.idModes||(t.idModes={}),t.idModes[e]=o,$(n,t)}function I(n,e){const o=new Date(n);return o.setDate(o.getDate()+e),o}function p(n){const e=n.getFullYear(),o=String(n.getMonth()+1).padStart(2,"0"),t=String(n.getDate()).padStart(2,"0");return`${e}-${o}-${t}`}function w(n,e,o){const t=n.getDay()===0?7:n.getDay();let s=e-t;return o?s<=0&&(s+=7):(s<0&&(s+=7),s===0&&(s=7)),p(I(n,s))}function q(){return p(I(new Date,7))}function U(n){const e=new Date,o=new Date(e.getFullYear(),e.getMonth(),e.getDate()),t=n.trim(),s=t.match(/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/);if(s)return`${s[1]}-${s[2].padStart(2,"0")}-${s[3].padStart(2,"0")}`;const r=t.match(/^(\d{1,2})[-/](\d{1,2})$/);if(r){const c=parseInt(r[1],10),f=parseInt(r[2],10);let g=new Date(e.getFullYear(),c-1,f);return g<o&&(g=new Date(e.getFullYear()+1,c-1,f)),p(g)}const i=t.match(/^(\d{2})(\d{2})$/);if(i){const c=parseInt(i[1],10),f=parseInt(i[2],10);if(c>=1&&c<=12&&f>=1&&f<=31){let g=new Date(e.getFullYear(),c-1,f);return g<o&&(g=new Date(e.getFullYear()+1,c-1,f)),p(g)}}const a={\u4E00:1,\u4E8C:2,\u4E09:3,\u56DB:4,\u4E94:5,\u516D:6,\u65E5:7,\u5929:7};if(t==="\u4ECA\u5929")return p(o);if(t==="\u660E\u5929")return p(I(o,1));if(t==="\u540E\u5929")return p(I(o,2));if(t==="\u5927\u540E\u5929")return p(I(o,3));const l=t.match(/^(这|下)周([一二三四五六日天])$/);if(l){const c=a[l[2]];if(c!==void 0)return w(o,c,l[1]==="\u4E0B")}const u=t.match(/^(?:周|星期)([一二三四五六日天])$/);if(u){const c=a[u[1]];if(c!==void 0)return w(o,c,!1)}return""}function _(n){const e=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()),o=new Date(n+"T00:00:00");return Math.floor((o.getTime()-e.getTime())/(1440*60*1e3))}function y(n){if(!n)return"";const e=_(n);return e<0?`${n} \u{1F534}\u5DF2\u903E\u671F${Math.abs(e)}\u5929`:e===0?`${n} \u26A1\u4ECA\u5929`:e===1?`${n} \u{1F7E1}\u660E\u5929`:e<=3?`${n} \u{1F7E1}${e}\u5929\u540E`:e<=7?`${n} ${e}\u5929\u540E`:n}function L(n,e){if(n.length===0)return`\u{1F4ED} \u6682\u65E0\u5F85\u529E\u4E8B\u9879${e===null?"":`
|
|
2
2
|
\u23F0 \u6BCF\u65E5\u63D0\u9192: ${e}:00`}
|
|
3
3
|
|
|
4
|
-
\u{1F4A1} \`/todo <\u5185\u5BB9> ddl \u660E\u5929\` \u6DFB\u52A0\uFF08\u9ED8\u8BA47\u5929\u5230\u671F\uFF09`;const o=n.filter(r=>!r.completed),t=n.filter(r=>r.completed),s=[];if(o.length>0){s.push("### \u{1F4CB} \u8FDB\u884C\u4E2D","");for(let r=0;r<o.length;r++){const i=o[r],a=i.deadline?` \u{1F4C5}${
|
|
5
|
-
`)}function
|
|
6
|
-
- **\u622A\u6B62:** ${
|
|
7
|
-
`)}const
|
|
8
|
-
`),msgType:"markdown",atUserId:
|
|
4
|
+
\u{1F4A1} \`/todo <\u5185\u5BB9> ddl \u660E\u5929\` \u6DFB\u52A0\uFF08\u9ED8\u8BA47\u5929\u5230\u671F\uFF09`;const o=n.filter(r=>!r.completed),t=n.filter(r=>r.completed),s=[];if(o.length>0){s.push("### \u{1F4CB} \u8FDB\u884C\u4E2D","");for(let r=0;r<o.length;r++){const i=o[r],a=i.deadline?` \u{1F4C5}${y(i.deadline)}`:"";s.push(`${r+1}. ${i.content} _@${i.assigneeNick}_${a}`)}}if(t.length>0){o.length>0&&s.push(""),s.push("### \u2705 \u5DF2\u5B8C\u6210","");for(let r=0;r<t.length;r++){const i=t[r],a=o.length+r+1;s.push(`${a}. ~~${i.content}~~ _@${i.assigneeNick}_`)}}if(s.push("","---","\u{1F4A1} `/todo done <\u5E8F\u53F7>` \u5B8C\u6210 | `/todo rm <\u5E8F\u53F7>` \u5220\u9664 | `/todo <\u5185\u5BB9> ddl \u660E\u5929`"),e!==void 0){const r=e===null?"\u23F0 \u6BCF\u65E5\u63D0\u9192: \u5DF2\u5173\u95ED":`\u23F0 \u6BCF\u65E5\u63D0\u9192: ${e}:00`;s.push(r)}return s.join(`
|
|
5
|
+
`)}function W(n,e){const o=n.deadline?`
|
|
6
|
+
- **\u622A\u6B62:** ${y(n.deadline)}`:"";return[`\u2705 \u5DF2\u6DFB\u52A0\u5F85\u529E #${e}`,"",`- **\u5185\u5BB9:** ${n.content}`,`- **\u8D1F\u8D23\u4EBA:** @${n.assigneeNick}${o}`].join(`
|
|
7
|
+
`)}const J=1;class z{constructor(e){this.timer=null,this.dc=e}start(){this.scheduleNext(),console.log(`[${(0,h.timestamp)()}] Todo\u5F15\u64CE\u5DF2\u542F\u52A8`)}destroy(){this.timer&&(clearTimeout(this.timer),this.timer=null)}scheduleNext(){const e=new Date,o=this.findNextReminderHour(e),t=new Date(e.getFullYear(),e.getMonth(),e.getDate(),o,0,0,0);t.getTime()<=e.getTime()&&t.setDate(t.getDate()+1);const s=t.getTime()-e.getTime();this.timer=setTimeout(()=>{console.log(`[${(0,h.timestamp)()}] [todo] \u5B9A\u65F6\u89E6\u53D1 (${o}:00)`),this.cleanExpiredItems(),this.sendReminders(o),this.scheduleNext()},s),console.log(`[${(0,h.timestamp)()}] [todo] \u4E0B\u6B21\u89E6\u53D1: ${t.toISOString()}`)}findNextReminderHour(e){const o=e.getHours(),t=m(this.dc),s=new Set;for(const i of Object.keys(t.reminders)){const a=t.reminders[i];if(a!==!1){if(typeof a=="number"){s.add(a);continue}s.add(10)}}s.size===0&&s.add(10);const r=Array.from(s).sort((i,a)=>i-a);for(const i of r)if(i>o)return i;return r[0]}cleanExpiredItems(){const e=m(this.dc),o=Date.now();let t=0;for(const s of Object.keys(e.conversations)){const r=e.conversations[s],i=r.length,a=r.filter(l=>{if(!l.completed||!l.completedAt)return!0;const u=new Date(l.completedAt).getTime();return isNaN(u)?!0:(o-u)/(1440*60*1e3)<J});a.length<i&&(e.conversations[s]=a,t+=i-a.length,a.length===0&&delete e.conversations[s])}t>0&&($(this.dc,e),console.log(`[${(0,h.timestamp)()}] [todo] \u81EA\u52A8\u6E05\u7406 ${t} \u6761\u5DF2\u5B8C\u6210\u5F85\u529E`))}async sendReminders(e){const o=m(this.dc),t=Object.keys(o.conversations);for(const s of t){const r=o.reminders?.[s];if((r===!1?null:typeof r=="number"?r:10)!==e)continue;const a=(o.conversations[s]||[]).filter(d=>!d.completed);if(a.length===0)continue;const l=this.dc.getConversationConfig(s);if(!l||!(l.dingToken||this.dc.config.defaultDingToken||this.dc.config.ownerConversationId))continue;const u=[],c=[];for(const d of a){if(!d.deadline)continue;const D=_(d.deadline);D<0?u.push(d):D<=3&&c.push(d)}if(u.length===0&&c.length===0)continue;const f=["### \u23F0 \u6BCF\u65E5\u5F85\u529E\u63D0\u9192",""],g=[];let T=!1;if(u.length>0){f.push("\u{1F534} **\u5DF2\u903E\u671F:**");for(const d of u)f.push(`- ${d.content} _@${d.assigneeNick}_ \u{1F4C5}${y(d.deadline)}`),d.assigneeIdType==="dingtalkId"?T=!0:g.push(d.assigneeStaffId);f.push("")}if(c.length>0){f.push("\u{1F7E1} **\u5373\u5C06\u5230\u671F (3\u5929\u5185):**");for(const d of c)f.push(`- ${d.content} _@${d.assigneeNick}_ \u{1F4C5}${y(d.deadline)}`),d.assigneeIdType==="dingtalkId"?T=!0:g.push(d.assigneeStaffId);f.push("")}if(T){const d=u.concat(c).filter(D=>D.assigneeIdType==="dingtalkId").map(D=>`@${D.assigneeStaffId}`);f.push(d.join(" ")),f.push("")}f.push("---"),f.push("\u{1F4A1} `/todo done <\u5E8F\u53F7>` \u5B8C\u6210 | `/todo` \u67E5\u770B\u5168\u90E8");try{await(0,A.sendDingMessage)(this.dc,{conversationId:s,sessionWebhook:"",content:f.join(`
|
|
8
|
+
`),msgType:"markdown",atUserId:g.length>0?g[0]:""})}catch(d){console.error(`[${(0,h.timestamp)()}] [todo] \u53D1\u9001\u63D0\u9192\u5931\u8D25 ${s}:`,d)}}}}exports.TodoEngine=z;
|
package/dist/src/common.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var n=exports&&exports.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.utils=exports.helper=exports.projUtil=void 0,exports.loadEnv=d;const u=require("utils-ok"),t=n(require("path")),c=n(require("assert")),r=n(require("fs")),f=n(require("os"));function d(){const e=t.default.join(f.default.homedir(),".cc-ding",".env");r.default.existsSync(e)&&require("dotenv").config({path:e}),require("dotenv").config()}exports.projUtil=(()=>{let e;return()=>(e||(e=new u.ProjUtil(t.default.resolve(__dirname,"../"))),e)})(),exports.helper={getCookiesByFile(e){return(0,c.default)(r.default.existsSync(e),`cookie\u7F13\u5B58\u6587\u4EF6\u4E0D\u5B58\u5728: ${e}`),u.fileUtil.getJSON(e).cookies}},exports.utils={addSuffixToFile(e,i){const s=t.default.dirname(e),o=t.default.extname(e),a=`${t.default.basename(e,o)}${i}${o}`,l=t.default.join(s,a);r.default.renameSync(e,l)}};
|
package/dist/src/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DEV=exports.DEBUG=void 0;const
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DEV=exports.DEBUG=void 0;const e=require("utils-ok"),r=require("os").platform(),t=r==="darwin";exports.DEBUG=e.strUtil.safeBoolean(process.env.DEBUG||""),exports.DEV=t;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-ding",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "本地Claude对接钉钉机器人工具套件",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
],
|
|
10
10
|
"main": "dist/index",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@anthropic-ai/claude-agent-sdk": "^0.3.173",
|
|
13
12
|
"commander": "^7.2.0",
|
|
14
13
|
"dotenv": "^16.6.1",
|
|
15
14
|
"shelljs": "^0.10.0",
|
|
@@ -43,9 +42,9 @@
|
|
|
43
42
|
"scripts": {
|
|
44
43
|
"lint": "eslint . && tsc && npm run blacklint",
|
|
45
44
|
"lint:fix": "eslint . --fix",
|
|
46
|
-
"blacklint": "utils-ok blacklint --blacklintDir
|
|
47
|
-
"test": "mocha
|
|
48
|
-
"build": "npm run lint && npm run test &&
|
|
45
|
+
"blacklint": "utils-ok blacklint --blacklintDir \"src bin\"",
|
|
46
|
+
"test": "mocha \"test/**/*.test.ts\"",
|
|
47
|
+
"build": "npm run lint && npm run test && node scripts/build.mjs",
|
|
49
48
|
"prepublishOnly": "npm run build"
|
|
50
49
|
},
|
|
51
50
|
"files": [
|