skyloom 1.5.3 → 1.7.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.
Files changed (41) hide show
  1. package/dist/core/agent.d.ts +2 -0
  2. package/dist/core/agent.d.ts.map +1 -1
  3. package/dist/core/agent.js +33 -6
  4. package/dist/core/agent.js.map +1 -1
  5. package/dist/core/arbitrate.d.ts +32 -0
  6. package/dist/core/arbitrate.d.ts.map +1 -0
  7. package/dist/core/arbitrate.js +136 -0
  8. package/dist/core/arbitrate.js.map +1 -0
  9. package/dist/core/estimate.d.ts +30 -0
  10. package/dist/core/estimate.d.ts.map +1 -0
  11. package/dist/core/estimate.js +94 -0
  12. package/dist/core/estimate.js.map +1 -0
  13. package/dist/core/filter.d.ts +16 -0
  14. package/dist/core/filter.d.ts.map +1 -0
  15. package/dist/core/filter.js +91 -0
  16. package/dist/core/filter.js.map +1 -0
  17. package/dist/core/index.d.ts +7 -1
  18. package/dist/core/index.d.ts.map +1 -1
  19. package/dist/core/index.js +13 -2
  20. package/dist/core/index.js.map +1 -1
  21. package/dist/core/learn.d.ts +30 -0
  22. package/dist/core/learn.d.ts.map +1 -0
  23. package/dist/core/learn.js +156 -0
  24. package/dist/core/learn.js.map +1 -0
  25. package/dist/core/longdoc.d.ts +41 -0
  26. package/dist/core/longdoc.d.ts.map +1 -0
  27. package/dist/core/longdoc.js +128 -0
  28. package/dist/core/longdoc.js.map +1 -0
  29. package/dist/core/security.d.ts +73 -0
  30. package/dist/core/security.d.ts.map +1 -0
  31. package/dist/core/security.js +220 -0
  32. package/dist/core/security.js.map +1 -0
  33. package/package.json +1 -1
  34. package/src/core/agent.ts +18 -6
  35. package/src/core/arbitrate.ts +162 -0
  36. package/src/core/estimate.ts +104 -0
  37. package/src/core/filter.ts +103 -0
  38. package/src/core/index.ts +8 -2
  39. package/src/core/learn.ts +146 -0
  40. package/src/core/longdoc.ts +155 -0
  41. package/src/core/security.ts +243 -0
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ /**
3
+ * 资源估算模块 — Token & time budget estimation for task planning.
4
+ *
5
+ * Helps Snow and other planning agents estimate the cost of
6
+ * proposed sub-tasks before committing to execution.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.estimateTokens = estimateTokens;
10
+ exports.estimateTaskCost = estimateTaskCost;
11
+ exports.estimateTaskPlan = estimateTaskPlan;
12
+ exports.formatPlanEstimate = formatPlanEstimate;
13
+ /* ═══════════════════════════════════════
14
+ Token estimation
15
+ ═══════════════════════════════════════ */
16
+ const CJK_REGEX = /[一-鿿぀-ゟ가-힯㐀-䶿]/g;
17
+ /** Estimate tokens for a given text (CJK ~2 each, ASCII ~4 chars each). */
18
+ function estimateTokens(text) {
19
+ const cjk = (text.match(CJK_REGEX) || []).length;
20
+ return cjk * 2 + Math.ceil((text.length - cjk) / 4);
21
+ }
22
+ /* ═══════════════════════════════════════
23
+ Per-task-type cost estimates
24
+ ═══════════════════════════════════════ */
25
+ const TASK_TYPE_PATTERNS = [
26
+ // [pattern, estimated tokens, estimated tools]
27
+ [/read|read_file|grep|search|查|搜索|list/i, 2000, 2],
28
+ [/write|write_file|生成|写|create|implement/i, 4000, 5],
29
+ [/edit|edit_file|改|修改|fix|修复/i, 3000, 3],
30
+ [/delete|delete_file|删|rm/i, 1500, 2],
31
+ [/deploy|部署|publish|发布|release/i, 8000, 8],
32
+ [/review|审查|audit|审计|scan|扫描/i, 5000, 4],
33
+ [/test|测试|run_test|coverage/i, 3000, 3],
34
+ [/research|研究|调研|analyze|分析/i, 6000, 4],
35
+ [/orchestrate|编排|multi-step|多步/i, 12000, 10],
36
+ ];
37
+ /** Estimate cost for a single task description. */
38
+ function estimateTaskCost(description) {
39
+ let tokens = 2000; // base
40
+ let tools = 2; // base
41
+ for (const [pattern, t, tc] of TASK_TYPE_PATTERNS) {
42
+ if (pattern.test(description)) {
43
+ tokens = Math.max(tokens, t);
44
+ tools = Math.max(tools, tc);
45
+ }
46
+ }
47
+ // Time estimate: ~0.5s per tool call + 2s per 1k tokens
48
+ const timeSeconds = (tokens / 1000) * 2 + tools * 0.5 + 2;
49
+ return { tokens, tools, timeSeconds };
50
+ }
51
+ function estimateTaskPlan(tasks) {
52
+ const perTask = [];
53
+ let totalTokens = 500; // system prompt overhead
54
+ let totalTools = 0;
55
+ let totalTime = 5; // init overhead
56
+ const warnings = [];
57
+ for (const t of tasks) {
58
+ const est = estimateTaskCost(t.description);
59
+ perTask.push({ id: t.id, tokens: est.tokens, tools: est.tools, time: est.timeSeconds });
60
+ totalTokens += est.tokens;
61
+ totalTools += est.tools;
62
+ totalTime += est.timeSeconds;
63
+ if (est.timeSeconds > 60)
64
+ warnings.push(`Task ${t.id} may take >${Math.round(est.timeSeconds)}s`);
65
+ if (est.tools > 10)
66
+ warnings.push(`Task ${t.id} uses many tool calls (${est.tools})`);
67
+ }
68
+ if (totalTokens > 64000)
69
+ warnings.push(`Total token estimate (${totalTokens}) exceeds typical context window`);
70
+ if (totalTime > 120)
71
+ warnings.push(`Estimated total time (${Math.round(totalTime)}s) is significant`);
72
+ if (tasks.length > 6)
73
+ warnings.push(`Large number of sub-tasks (${tasks.length}) — consider merging simpler ones`);
74
+ return { totalTokens, totalTools, totalTimeSeconds: Math.round(totalTime), perTask, warnings };
75
+ }
76
+ /* ═══════════════════════════════════════
77
+ Format estimate for display
78
+ ═══════════════════════════════════════ */
79
+ function formatPlanEstimate(est) {
80
+ const lines = [
81
+ `## Plan Estimate`,
82
+ `| Task | Tokens | Tools | Time |`,
83
+ `|------|--------|-------|------|`,
84
+ ...est.perTask.map(t => `| ${t.id} | ${t.tokens} | ${t.tools} | ${t.time.toFixed(0)}s |`),
85
+ `| **Total** | **${est.totalTokens}** | **${est.totalTools}** | **${est.totalTimeSeconds}s** |`,
86
+ ];
87
+ if (est.warnings.length > 0) {
88
+ lines.push("", "### Warnings");
89
+ for (const w of est.warnings)
90
+ lines.push(`- ⚠ ${w}`);
91
+ }
92
+ return lines.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,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5B,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,cAAc,CAAC,CAAC;QAC/B,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,QAAQ;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 输出过滤模块 — sensitive information sanitization.
3
+ *
4
+ * Before agent responses reach the user (or are persisted),
5
+ * scan for and redact sensitive patterns like API keys,
6
+ * tokens, passwords, PII, and internal paths.
7
+ */
8
+ export interface FilterResult {
9
+ clean: string;
10
+ redacted: boolean;
11
+ count: number;
12
+ details: string[];
13
+ }
14
+ export declare function filterOutput(text: string): FilterResult;
15
+ export declare function needsFiltering(text: string): boolean;
16
+ //# sourceMappingURL=filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.d.ts","sourceRoot":"","sources":["../../src/core/filter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA2CH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAgCvD;AAKD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CASpD"}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ /**
3
+ * 输出过滤模块 — sensitive information sanitization.
4
+ *
5
+ * Before agent responses reach the user (or are persisted),
6
+ * scan for and redact sensitive patterns like API keys,
7
+ * tokens, passwords, PII, and internal paths.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.filterOutput = filterOutput;
11
+ exports.needsFiltering = needsFiltering;
12
+ /* ═══════════════════════════════════════
13
+ Detection patterns — compiled once at module load
14
+ ═══════════════════════════════════════ */
15
+ const SENSITIVE_PATTERNS = [
16
+ // API keys & tokens
17
+ [/sk-[a-zA-Z0-9]{32,}/g, "[REDACTED:API_KEY]"],
18
+ [/(?:api_key|apikey|secret_key|access_token|auth_token)\s*[:=]\s*["']?[^\s"']{8,}["']?/gi, "$1: [REDACTED]"],
19
+ [/ghp_[a-zA-Z0-9]{36}/g, "[REDACTED:GITHUB_TOKEN]"],
20
+ [/gho_[a-zA-Z0-9]{36}/g, "[REDACTED:GITHUB_TOKEN]"],
21
+ // AWS credentials
22
+ [/AKIA[0-9A-Z]{16}/g, "[REDACTED:AWS_KEY]"],
23
+ [/(?:aws_access_key_id|aws_secret_access_key)\s*[:=]\s*["']?[^\s"']+/gi, "$1: [REDACTED]"],
24
+ // Passwords
25
+ [/(?:password|passwd|pwd)\s*[:=]\s*["']?[^\s"']{4,}["']?/gi, "$1: [REDACTED]"],
26
+ [/(?:密码|口令)\s*[:=]\s*["']?[^\s"']{2,}["']?/g, "$1: [已脱敏]"],
27
+ // Connection strings
28
+ [/(?:mongodb|postgres|mysql|redis):\/\/[^\s]+/g, "[REDACTED:DB_URI]"],
29
+ [/(?:jdbc|odbc):[^\s]+/g, "[REDACTED:DB_URI]"],
30
+ // Private keys
31
+ [/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END .*?PRIVATE KEY-----/g, "[REDACTED:PRIVATE_KEY]"],
32
+ // IP addresses (local only)
33
+ [/192\.168\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
34
+ [/10\.\d{1,3}\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
35
+ [/172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}/g, "[REDACTED:LAN_IP]"],
36
+ // File paths
37
+ [/(?:\/etc\/(?:passwd|shadow|hosts|sudoers))/g, "[REDACTED:SYSTEM_PATH]"],
38
+ ];
39
+ /* Email masking (function-based, handled separately) */
40
+ const EMAIL_RE = /([a-zA-Z0-9._%+-]{3,})@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/g;
41
+ function filterOutput(text) {
42
+ if (!text)
43
+ return { clean: "", redacted: false, count: 0, details: [] };
44
+ let clean = text;
45
+ let count = 0;
46
+ const details = [];
47
+ // Email masking (function-based replacement)
48
+ let emailCount = 0;
49
+ clean = clean.replace(EMAIL_RE, (full, user, domain) => {
50
+ emailCount++;
51
+ return user.slice(0, 2) + "***@" + domain;
52
+ });
53
+ if (emailCount > 0) {
54
+ count += emailCount;
55
+ details.push(`Masked ${emailCount}x email addresses`);
56
+ }
57
+ for (const [pattern, replacement] of SENSITIVE_PATTERNS) {
58
+ const matches = clean.match(pattern);
59
+ if (matches) {
60
+ count += matches.length;
61
+ if (typeof replacement === "string") {
62
+ details.push(`Redacted ${matches.length}x ${pattern.source.slice(0, 30)}`);
63
+ }
64
+ else {
65
+ details.push(`Masked ${matches.length}x email addresses`);
66
+ }
67
+ clean = clean.replace(pattern, replacement);
68
+ }
69
+ }
70
+ return { clean, redacted: count > 0, count, details };
71
+ }
72
+ /* ═══════════════════════════════════════
73
+ Quick check — is filtering needed?
74
+ ═══════════════════════════════════════ */
75
+ function needsFiltering(text) {
76
+ if (!text)
77
+ return false;
78
+ // Quick scan with the most common patterns
79
+ if (/sk-[a-zA-Z0-9]{32,}/.test(text))
80
+ return true;
81
+ if (/api_key.*[:=]/.test(text))
82
+ return true;
83
+ if (/password.*[:=]/.test(text))
84
+ return true;
85
+ if (/-----BEGIN.*PRIVATE KEY-----/.test(text))
86
+ return true;
87
+ if (EMAIL_RE.test(text))
88
+ return true;
89
+ return false;
90
+ }
91
+ //# sourceMappingURL=filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filter.js","sourceRoot":"","sources":["../../src/core/filter.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG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;QAAE,OAAO,IAAI,CAAC;IAClD,IAAI,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5C,IAAI,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC7C,IAAI,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3D,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACrC,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -25,5 +25,11 @@ export * from './skill';
25
25
  export * from './router';
26
26
  export * from './agent';
27
27
  export * from './factory';
28
- export declare const VERSION = "1.4.0";
28
+ export * from './security';
29
+ export * from './learn';
30
+ export * from './longdoc';
31
+ export * from './filter';
32
+ export * from './estimate';
33
+ export * from './arbitrate';
34
+ export declare const VERSION: any;
29
35
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,mBAAmB,CAAC;AAClC,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,aAAa,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,WAAW,EAAE,eAAe,EAAE,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AACzL,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;A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gG,CAAC"}
@@ -49,6 +49,17 @@ __exportStar(require("./skill"), exports);
49
49
  __exportStar(require("./router"), exports);
50
50
  __exportStar(require("./agent"), exports);
51
51
  __exportStar(require("./factory"), exports);
52
- // Version
53
- exports.VERSION = '1.4.0';
52
+ __exportStar(require("./security"), exports);
53
+ __exportStar(require("./learn"), exports);
54
+ __exportStar(require("./longdoc"), exports);
55
+ __exportStar(require("./filter"), exports);
56
+ __exportStar(require("./estimate"), exports);
57
+ __exportStar(require("./arbitrate"), exports);
58
+ // Version — read from package.json
59
+ exports.VERSION = (() => { try {
60
+ return require('../../package.json').version;
61
+ }
62
+ catch {
63
+ return '1.6.0';
64
+ } })();
54
65
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;AAEH,8CAA4B;AAC5B,4CAA0B;AAC1B,2CAAyB;AACzB,2CAAyB;AACzB,yCAAuB;AACvB,oDAAkC;AAClC,wCAAsB;AACtB,0CAAwB;AACxB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,wCAAsB;AACtB,yCAAyL;AAAhL,0GAAA,aAAa,OAAA;AAAE,mHAAA,sBAAsB,OAAA;AAAE,0GAAA,aAAa,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,4GAAA,eAAe,OAAA;AACjI,6CAA2B;AAC3B,0CAAwB;AACxB,+CAA6B;AAC7B,8CAA4B;AAC5B,4CAA0B;AAC1B,gDAA8B;AAC9B,kDAAgC;AAChC,0CAAwB;AACxB,2CAAyB;AACzB,0CAAwB;AACxB,4CAA0B;AAE1B,UAAU;AACG,QAAA,OAAO,GAAG,OAAO,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;AAEH,8CAA4B;AAC5B,4CAA0B;AAC1B,2CAAyB;AACzB,2CAAyB;AACzB,yCAAuB;AACvB,oDAAkC;AAClC,wCAAsB;AACtB,0CAAwB;AACxB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,wCAAsB;AACtB,yCAAyL;AAAhL,0GAAA,aAAa,OAAA;AAAE,mHAAA,sBAAsB,OAAA;AAAE,0GAAA,aAAa,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AAAE,8GAAA,iBAAiB,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,4GAAA,eAAe,OAAA;AACjI,6CAA2B;AAC3B,0CAAwB;AACxB,+CAA6B;AAC7B,8CAA4B;AAC5B,4CAA0B;AAC1B,gDAA8B;AAC9B,kDAAgC;AAChC,0CAAwB;AACxB,2CAAyB;AACzB,0CAAwB;AACxB,4CAA0B;AAC1B,6CAA2B;AAC3B,0CAAwB;AACxB,4CAA0B;AAC1B,2CAAyB;AACzB,6CAA2B;AAC3B,8CAA4B;AAE5B,mCAAmC;AACtB,QAAA,OAAO,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;IAAC,OAAO,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAAC;AAAC,CAAC;AAAC,MAAM,CAAC;IAAC,OAAO,OAAO,CAAC;AAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 持续学习模块 — post-task review + experience recording.
3
+ *
4
+ * After each task, the agent writes a structured review.
5
+ * Failed attempts are indexed for similarity search to avoid repetition.
6
+ */
7
+ export interface TaskReview {
8
+ ts: string;
9
+ agent: string;
10
+ goal: string;
11
+ success: boolean;
12
+ durationMs: number;
13
+ toolCalls: string[];
14
+ errorMsg?: string;
15
+ rootCause?: string;
16
+ improvement?: string;
17
+ }
18
+ export interface ExperienceEntry {
19
+ id: string;
20
+ pattern: string;
21
+ solution: string;
22
+ frequency: number;
23
+ lastSeen: string;
24
+ }
25
+ export declare function recordReview(review: TaskReview): void;
26
+ export declare function recordExperience(errorPattern: string, rootCause: string, solution: string): void;
27
+ export declare function queryExperiences(problem: string, limit?: number): ExperienceEntry[];
28
+ export declare function formatExperiencesForPrompt(problem: string): string;
29
+ export declare function generateReview(agent: string, goal: string, success: boolean, durationMs: number, toolCalls: string[], errorMsg?: string): TaskReview;
30
+ //# sourceMappingURL=learn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"learn.d.ts","sourceRoot":"","sources":["../../src/core/learn.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAYD,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAWrD;AAiBD,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAwBhG;AAKD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,GAAE,MAAU,GAAG,eAAe,EAAE,CAatF;AAKD,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CASlE;AAKD,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EACjE,SAAS,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,EAAE,MAAM,GACrC,UAAU,CAQZ"}
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ /**
3
+ * 持续学习模块 — post-task review + experience recording.
4
+ *
5
+ * After each task, the agent writes a structured review.
6
+ * Failed attempts are indexed for similarity search to avoid repetition.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.recordReview = recordReview;
43
+ exports.recordExperience = recordExperience;
44
+ exports.queryExperiences = queryExperiences;
45
+ exports.formatExperiencesForPrompt = formatExperiencesForPrompt;
46
+ exports.generateReview = generateReview;
47
+ const fs = __importStar(require("fs"));
48
+ const path = __importStar(require("path"));
49
+ const config_1 = require("./config");
50
+ const logger_1 = require("./logger");
51
+ const log = (0, logger_1.getLogger)("learn");
52
+ /* ── Persistence ── */
53
+ const reviewDir = path.join(config_1.USER_CONFIG_DIR, "reviews");
54
+ const expFile = path.join(config_1.USER_CONFIG_DIR, "experiences.json");
55
+ const reviewDir_ = reviewDir; // for closure
56
+ function ensureDir() { if (!fs.existsSync(reviewDir_))
57
+ fs.mkdirSync(reviewDir_, { recursive: true }); }
58
+ /* ═══════════════════════════════════════
59
+ Task Review Recording
60
+ ═══════════════════════════════════════ */
61
+ function recordReview(review) {
62
+ ensureDir();
63
+ const file = path.join(reviewDir_, `${review.ts.slice(0, 10)}_${review.agent}.jsonl`);
64
+ const line = JSON.stringify(review);
65
+ fs.appendFileSync(file, line + "\n");
66
+ log.debug("review_recorded", { agent: review.agent, success: review.success });
67
+ // If failed, also record as experience
68
+ if (!review.success && review.errorMsg) {
69
+ recordExperience(review.errorMsg, review.rootCause || "unknown", review.improvement || "no improvement noted");
70
+ }
71
+ }
72
+ /* ═══════════════════════════════════════
73
+ Experience Recording (for failure patterns)
74
+ ═══════════════════════════════════════ */
75
+ function loadExperiences() {
76
+ try {
77
+ if (fs.existsSync(expFile))
78
+ return JSON.parse(fs.readFileSync(expFile, "utf-8"));
79
+ }
80
+ catch { /* ignore */ }
81
+ return [];
82
+ }
83
+ function saveExperiences(entries) {
84
+ ensureDir();
85
+ fs.writeFileSync(expFile, JSON.stringify(entries, null, 2), "utf-8");
86
+ }
87
+ function recordExperience(errorPattern, rootCause, solution) {
88
+ const entries = loadExperiences();
89
+ const normalized = errorPattern.toLowerCase().slice(0, 200);
90
+ // Check for existing similar pattern (simple substring match)
91
+ const existing = entries.find(e => e.pattern.toLowerCase().includes(normalized.slice(0, 50)) || normalized.includes(e.pattern.toLowerCase().slice(0, 50)));
92
+ if (existing) {
93
+ existing.frequency++;
94
+ existing.lastSeen = new Date().toISOString();
95
+ if (solution && solution !== "no improvement noted")
96
+ existing.solution = solution;
97
+ }
98
+ else {
99
+ entries.push({
100
+ id: Math.random().toString(36).slice(2, 10),
101
+ pattern: errorPattern.slice(0, 200),
102
+ solution,
103
+ frequency: 1,
104
+ lastSeen: new Date().toISOString(),
105
+ });
106
+ }
107
+ // Keep top 100 experiences, sorted by frequency
108
+ entries.sort((a, b) => b.frequency - a.frequency);
109
+ if (entries.length > 100)
110
+ entries.splice(100);
111
+ saveExperiences(entries);
112
+ }
113
+ /* ═══════════════════════════════════════
114
+ Query experiences
115
+ ═══════════════════════════════════════ */
116
+ function queryExperiences(problem, limit = 3) {
117
+ const entries = loadExperiences();
118
+ const lower = problem.toLowerCase();
119
+ return entries
120
+ .filter(e => {
121
+ const plow = e.pattern.toLowerCase();
122
+ // Simple token overlap scoring
123
+ const tokens = lower.split(/\s+/).filter(t => t.length > 2);
124
+ const matches = tokens.filter(t => plow.includes(t));
125
+ return matches.length >= 2;
126
+ })
127
+ .sort((a, b) => b.frequency - a.frequency)
128
+ .slice(0, limit);
129
+ }
130
+ /* ═══════════════════════════════════════
131
+ Format experiences for system prompt injection
132
+ ═══════════════════════════════════════ */
133
+ function formatExperiencesForPrompt(problem) {
134
+ const exps = queryExperiences(problem);
135
+ if (!exps.length)
136
+ return "";
137
+ const lines = ["## 历史教训(从经验库检索)", "以下是与当前任务相关的过往失败案例,请避免重复:"];
138
+ for (const e of exps) {
139
+ lines.push(`- **模式**: ${e.pattern.slice(0, 120)}`);
140
+ lines.push(` **解决**: ${e.solution.slice(0, 200)} (出现 ${e.frequency} 次)`);
141
+ }
142
+ return lines.join("\n");
143
+ }
144
+ /* ═══════════════════════════════════════
145
+ Generate a structured review after task completion
146
+ ═══════════════════════════════════════ */
147
+ function generateReview(agent, goal, success, durationMs, toolCalls, errorMsg) {
148
+ return {
149
+ ts: new Date().toISOString(),
150
+ agent, goal, success, durationMs, toolCalls,
151
+ errorMsg,
152
+ rootCause: errorMsg ? "auto-detected failure" : undefined,
153
+ improvement: errorMsg ? "review error and adjust approach" : undefined,
154
+ };
155
+ }
156
+ //# sourceMappingURL=learn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"learn.js","sourceRoot":"","sources":["../../src/core/learn.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCH,oCAWC;AAiBD,4CAwBC;AAKD,4CAaC;AAKD,gEASC;AAKD,wCAWC;AA1ID,uCAAyB;AACzB,2CAA6B;AAC7B,qCAA2C;AAC3C,qCAAqC;AAErC,MAAM,GAAG,GAAG,IAAA,kBAAS,EAAC,OAAO,CAAC,CAAC;AAuB/B,uBAAuB;AACvB,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,wBAAe,EAAE,SAAS,CAAC,CAAC;AACxD,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,wBAAe,EAAE,kBAAkB,CAAC,CAAC;AAC/D,MAAM,UAAU,GAAG,SAAS,CAAC,CAAC,cAAc;AAE5C,SAAS,SAAS,KAAK,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;IAAE,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;AAEvG;;6CAE6C;AAC7C,SAAgB,YAAY,CAAC,MAAkB;IAC7C,SAAS,EAAE,CAAC;IACZ,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,KAAK,QAAQ,CAAC,CAAC;IACtF,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAC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,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;6CAE6C;AAC7C,SAAgB,cAAc,CAC5B,KAAa,EAAE,IAAY,EAAE,OAAgB,EAAE,UAAkB,EACjE,SAAmB,EAAE,QAAiB;IAEtC,OAAO;QACL,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QAC5B,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,SAAS;QAC3C,QAAQ;QACR,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC,SAAS;QACzD,WAAW,EAAE,QAAQ,CAAC,CAAC,CAAC,kCAAkC,CAAC,CAAC,CAAC,SAAS;KACvE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 长文档处理策略 — sliding window + summary chain.
3
+ *
4
+ * When an input exceeds the agent's effective context window,
5
+ * split into overlapping chunks, summarize each, then chain
6
+ * summaries into a final digest.
7
+ *
8
+ * Architecture:
9
+ * Input → Chunk(sliding window) → Per-chunk Summary → Chain → Final Digest
10
+ *
11
+ * All summaries are generated by the calling agent's LLM, so quality
12
+ * depends on the model in use. The chunker is pure text processing
13
+ * and works without any LLM call.
14
+ */
15
+ import type { BaseAgent } from "./agent";
16
+ export interface ChunkOptions {
17
+ /** Target chunk size in characters (default 6000) */
18
+ chunkSize?: number;
19
+ /** Overlap between consecutive chunks in characters (default 800) */
20
+ overlap?: number;
21
+ /** Minimum chunk size before we stop splitting (default 500) */
22
+ minChunk?: number;
23
+ }
24
+ export declare function chunkText(text: string, opts?: ChunkOptions): string[];
25
+ export interface SummaryOptions {
26
+ /** Max total chars for the final digest (default 3000) */
27
+ maxDigestChars?: number;
28
+ /** Custom summarization prompt for each chunk */
29
+ chunkPrompt?: string;
30
+ /** Custom chain prompt for combining summaries */
31
+ chainPrompt?: string;
32
+ }
33
+ export declare function summarizeLongDoc(agent: BaseAgent, text: string, opts?: SummaryOptions): Promise<string>;
34
+ export declare function parseStructuredInput(input: string): {
35
+ hasTable: boolean;
36
+ hasJSON: boolean;
37
+ hasCSV: boolean;
38
+ extractedJSON: string | null;
39
+ extractedTable: string[][] | null;
40
+ };
41
+ //# sourceMappingURL=longdoc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"longdoc.d.ts","sourceRoot":"","sources":["../../src/core/longdoc.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAKzC,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,MAAM,EAAE,CAkCrE;AAKD,MAAM,WAAW,cAAc;IAC7B,0DAA0D;IAC1D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,iDAAiD;IACjD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kDAAkD;IAClD,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAWD,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,SAAS,EAChB,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,cAAc,GACpB,OAAO,CAAC,MAAM,CAAC,CAgCjB;AAKD,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG;IACnD,QAAQ,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC;IACrD,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,cAAc,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,CAAC;CACjE,CAwBA"}
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ /**
3
+ * 长文档处理策略 — sliding window + summary chain.
4
+ *
5
+ * When an input exceeds the agent's effective context window,
6
+ * split into overlapping chunks, summarize each, then chain
7
+ * summaries into a final digest.
8
+ *
9
+ * Architecture:
10
+ * Input → Chunk(sliding window) → Per-chunk Summary → Chain → Final Digest
11
+ *
12
+ * All summaries are generated by the calling agent's LLM, so quality
13
+ * depends on the model in use. The chunker is pure text processing
14
+ * and works without any LLM call.
15
+ */
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.chunkText = chunkText;
18
+ exports.summarizeLongDoc = summarizeLongDoc;
19
+ exports.parseStructuredInput = parseStructuredInput;
20
+ function chunkText(text, opts) {
21
+ const cs = opts?.chunkSize ?? 6000;
22
+ const ol = opts?.overlap ?? 800;
23
+ const min = opts?.minChunk ?? 500;
24
+ const chunks = [];
25
+ if (text.length <= cs + min) {
26
+ chunks.push(text);
27
+ return chunks;
28
+ }
29
+ let start = 0;
30
+ while (start < text.length) {
31
+ let end = start + cs;
32
+ if (end >= text.length) {
33
+ end = text.length;
34
+ }
35
+ else {
36
+ // Try to break at paragraph boundary
37
+ const searchEnd = Math.min(end + 400, text.length);
38
+ const paraBreak = text.lastIndexOf("\n\n", searchEnd);
39
+ if (paraBreak > start + min)
40
+ end = paraBreak;
41
+ else {
42
+ const lineBreak = text.lastIndexOf("\n", searchEnd);
43
+ if (lineBreak > start + min)
44
+ end = lineBreak;
45
+ else {
46
+ const space = text.lastIndexOf(" ", searchEnd);
47
+ if (space > start + min)
48
+ end = space;
49
+ }
50
+ }
51
+ }
52
+ chunks.push(text.slice(start, end).trim());
53
+ if (end >= text.length)
54
+ break;
55
+ start = end - ol;
56
+ if (start < 0)
57
+ start = 0;
58
+ }
59
+ return chunks;
60
+ }
61
+ const DEFAULT_CHUNK_PROMPT = `Summarize the following text concisely. Keep all key facts, names, numbers, and code snippets. Output the summary directly without preamble. Limit to 300 words.
62
+
63
+ Text:
64
+ {text}`;
65
+ const DEFAULT_CHAIN_PROMPT = `Combine the following section summaries into a single coherent digest. Preserve all key facts, remove redundancy. Output directly without preamble.
66
+
67
+ {summaries}`;
68
+ async function summarizeLongDoc(agent, text, opts) {
69
+ const maxDigest = opts?.maxDigestChars ?? 3000;
70
+ const chunks = chunkText(text);
71
+ // Single chunk — no summarization needed
72
+ if (chunks.length <= 1) {
73
+ if (text.length <= maxDigest)
74
+ return text;
75
+ const prompt = (opts?.chunkPrompt || DEFAULT_CHUNK_PROMPT).replace("{text}", text);
76
+ return agent.chatOneshot(prompt, { maxTokens: maxDigest });
77
+ }
78
+ // Multi-chunk: summarize each, then chain
79
+ const summaries = [];
80
+ for (let i = 0; i < chunks.length; i++) {
81
+ const prompt = (opts?.chunkPrompt || DEFAULT_CHUNK_PROMPT).replace("{text}", chunks[i]);
82
+ try {
83
+ const s = await agent.chatOneshot(prompt, { maxTokens: 600 });
84
+ summaries.push(s);
85
+ }
86
+ catch {
87
+ summaries.push(chunks[i].slice(0, 400) + "...");
88
+ }
89
+ }
90
+ // Chain summaries
91
+ if (summaries.length === 1)
92
+ return summaries[0].slice(0, maxDigest);
93
+ const joined = summaries.map((s, i) => `## Section ${i + 1}\n${s}`).join("\n\n");
94
+ if (joined.length <= maxDigest)
95
+ return joined;
96
+ const chainPrompt = (opts?.chainPrompt || DEFAULT_CHAIN_PROMPT).replace("{summaries}", joined);
97
+ const final = await agent.chatOneshot(chainPrompt, { maxTokens: maxDigest });
98
+ return final.slice(0, maxDigest);
99
+ }
100
+ /* ═══════════════════════════════════════
101
+ Structured data parsing helpers
102
+ ═══════════════════════════════════════ */
103
+ function parseStructuredInput(input) {
104
+ const result = { hasTable: false, hasJSON: false, hasCSV: false, extractedJSON: null, extractedTable: null };
105
+ // Detect JSON
106
+ const jsonMatch = input.match(/\{[\s\S]*\}|\[[\s\S]*\]/);
107
+ if (jsonMatch) {
108
+ try {
109
+ JSON.parse(jsonMatch[0]);
110
+ result.hasJSON = true;
111
+ result.extractedJSON = jsonMatch[0];
112
+ }
113
+ catch { /* not valid JSON */ }
114
+ }
115
+ // Detect markdown table
116
+ const tableMatch = input.match(/\|[\s\S]*?\|/);
117
+ if (tableMatch) {
118
+ result.hasTable = true;
119
+ const lines = input.split("\n").filter(l => l.includes("|") && !l.startsWith("|---") && !l.startsWith("| --"));
120
+ result.extractedTable = lines.map(l => l.split("|").filter(c => c.trim()).map(c => c.trim()));
121
+ }
122
+ // Detect CSV
123
+ if (input.includes(",") && input.split("\n").filter(l => l.includes(",")).length >= 2) {
124
+ result.hasCSV = true;
125
+ }
126
+ return result;
127
+ }
128
+ //# sourceMappingURL=longdoc.js.map