@klarkxy/dsh-self-improvement 0.1.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.
@@ -0,0 +1,103 @@
1
+ import { AiFeatureScope, AiServices, AuxiliaryRequest, AuxiliaryResult, EvidenceRef, KnowledgeKind, KnowledgeScope, MemoryMutationOptions, MemoryQuery, MemoryRecord, MemoryService, NewMemoryRecord, ProducerMessageSource, PurposeSpec, RpcResult, UsageReceipt, projectIdFromCwd as projectIdFromCwd$1 } from "@klarkxy/dsh-ai-services/contracts";
2
+ //#region src/contracts.d.ts
3
+ declare const CHAT_EVENTS_SLOT = "dsh-editor.chat.events";
4
+ declare const projectIdFromCwd: typeof projectIdFromCwd$1;
5
+ declare const SELF_IMPROVEMENT_RPC_CHANNEL = "/dsh-self-improvement";
6
+ declare const SELF_IMPROVEMENT_PLUGIN = "@klarkxy/dsh-self-improvement";
7
+ declare const SELF_IMPROVEMENT_SOURCE_KIND: "plugin:@klarkxy/dsh-self-improvement";
8
+ /** Production activate identity. Core owns the scoped pluginName regex. */
9
+ declare const SELF_IMPROVEMENT_ACTIVATE_ID = "@klarkxy/dsh-self-improvement";
10
+ declare const EXTRACT_PURPOSE = "self-improvement.extract";
11
+ declare const LESSON_INJECTION_SECTION = "dsh-self-improvement:lessons";
12
+ declare const LESSON_SCHEMA_TAG = "lesson-schema:1";
13
+ declare const LESSON_SCHEMA_VERSION = 1;
14
+ declare const MAX_INJECTED_LESSONS = 5;
15
+ declare const MAX_INJECTION_TOKENS = 800;
16
+ declare const MAX_PROJECT_ID_CHARS = 32768;
17
+ declare const MAX_RECALL_QUERY_CHARS = 200;
18
+ type SkillStatus = 'preview' | 'accepted' | 'rejected' | 'revoked';
19
+ type SkillExportState = 'none' | 'recorded' | 'revoked';
20
+ interface SkillAuditEntry {
21
+ at: number;
22
+ action: string;
23
+ detail?: string;
24
+ }
25
+ interface SkillSourceRef {
26
+ id: string;
27
+ revision: number;
28
+ status: MemoryRecord['status'];
29
+ scope: KnowledgeScope;
30
+ expiresAt?: number;
31
+ }
32
+ interface SkillRecord {
33
+ id: string;
34
+ revision: number;
35
+ status: SkillStatus;
36
+ lessonIds: string[];
37
+ sources: SkillSourceRef[];
38
+ title: string;
39
+ markdown: string;
40
+ createdAt: number;
41
+ updatedAt: number;
42
+ exportState: SkillExportState;
43
+ exportedAt?: number;
44
+ exportRevokedAt?: number;
45
+ exportFilename?: string;
46
+ audit: SkillAuditEntry[];
47
+ }
48
+ interface SessionWatermark {
49
+ sessionId: string;
50
+ seq: number;
51
+ projectId?: string;
52
+ updatedAt: number;
53
+ }
54
+ type LessonTriggerKind = 'human-correction' | 'verified-tool-fix' | 'manual';
55
+ interface LessonTrigger {
56
+ kind: LessonTriggerKind;
57
+ evidence: EvidenceRef[];
58
+ titleHint: string;
59
+ contentHint: string;
60
+ toolName?: string;
61
+ }
62
+ interface LessonInjectPayload {
63
+ source: ProducerMessageSource & {
64
+ kind: typeof SELF_IMPROVEMENT_SOURCE_KIND;
65
+ plugin: typeof SELF_IMPROVEMENT_PLUGIN;
66
+ form: 'snapshot';
67
+ sections: Array<{
68
+ name: string;
69
+ text: string;
70
+ }>;
71
+ };
72
+ content: Array<{
73
+ type: 'text';
74
+ text: string;
75
+ }>;
76
+ }
77
+ interface PreStepEnter {
78
+ kind: 'enter';
79
+ messages: unknown[];
80
+ startsRequestSeries?: true;
81
+ }
82
+ type PreStepDecision = {
83
+ kind: 'reject';
84
+ } | PreStepEnter;
85
+ interface ReviewSnapshot {
86
+ memoryAvailable: boolean;
87
+ memoryMessage?: string;
88
+ projectId?: string;
89
+ generation: number;
90
+ storageFailed: boolean;
91
+ lessons: MemoryRecord[];
92
+ skills: SkillRecord[];
93
+ /** True while an extract for this session (or any session, if none requested) is queued or in flight. */
94
+ extracting?: boolean;
95
+ }
96
+ declare function evidenceWatermark(refs: readonly EvidenceRef[]): string;
97
+ declare function sessionCwd(session: unknown): string | undefined;
98
+ declare function fail(code: string, message: string): RpcResult<never>;
99
+ declare const MEMORY_UNAVAILABLE_MESSAGE = "记忆服务不可用。请先单独启用「记忆」插件;启用自我改进不会自动打开记忆。";
100
+ declare const MEMORY_UNAVAILABLE_MESSAGE_EN = "Memory is unavailable. Enable the Memory plugin separately; turning on self-improvement does not enable Memory.";
101
+ //#endregion
102
+ export { type AiFeatureScope, type AiServices, type AuxiliaryRequest, type AuxiliaryResult, CHAT_EVENTS_SLOT, EXTRACT_PURPOSE, type EvidenceRef, type KnowledgeKind, type KnowledgeScope, LESSON_INJECTION_SECTION, LESSON_SCHEMA_TAG, LESSON_SCHEMA_VERSION, LessonInjectPayload, LessonTrigger, LessonTriggerKind, MAX_INJECTED_LESSONS, MAX_INJECTION_TOKENS, MAX_PROJECT_ID_CHARS, MAX_RECALL_QUERY_CHARS, MEMORY_UNAVAILABLE_MESSAGE, MEMORY_UNAVAILABLE_MESSAGE_EN, type MemoryMutationOptions, type MemoryQuery, type MemoryRecord, type MemoryService, type NewMemoryRecord, PreStepDecision, PreStepEnter, type ProducerMessageSource, type PurposeSpec, ReviewSnapshot, type RpcResult, SELF_IMPROVEMENT_ACTIVATE_ID, SELF_IMPROVEMENT_PLUGIN, SELF_IMPROVEMENT_RPC_CHANNEL, SELF_IMPROVEMENT_SOURCE_KIND, SessionWatermark, SkillAuditEntry, SkillExportState, SkillRecord, SkillSourceRef, SkillStatus, type UsageReceipt, evidenceWatermark, fail, projectIdFromCwd, sessionCwd };
103
+ //# sourceMappingURL=contracts.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { CHAT_EVENTS_SLOT as CHAT_EVENTS_SLOT$1, projectIdFromCwd as projectIdFromCwd$1 } from "@klarkxy/dsh-ai-services/contracts";
2
+ //#region src/contracts.ts
3
+ /** Own records plus reexported frozen AI/Memory interfaces. */
4
+ const CHAT_EVENTS_SLOT = CHAT_EVENTS_SLOT$1;
5
+ const projectIdFromCwd = projectIdFromCwd$1;
6
+ const SELF_IMPROVEMENT_RPC_CHANNEL = "/dsh-self-improvement";
7
+ const SELF_IMPROVEMENT_PLUGIN = "@klarkxy/dsh-self-improvement";
8
+ const SELF_IMPROVEMENT_SOURCE_KIND = "plugin:@klarkxy/dsh-self-improvement";
9
+ /** Production activate identity. Core owns the scoped pluginName regex. */
10
+ const SELF_IMPROVEMENT_ACTIVATE_ID = SELF_IMPROVEMENT_PLUGIN;
11
+ const EXTRACT_PURPOSE = "self-improvement.extract";
12
+ const LESSON_INJECTION_SECTION = "dsh-self-improvement:lessons";
13
+ const LESSON_SCHEMA_TAG = "lesson-schema:1";
14
+ const LESSON_SCHEMA_VERSION = 1;
15
+ const MAX_INJECTED_LESSONS = 5;
16
+ const MAX_INJECTION_TOKENS = 800;
17
+ const MAX_PROJECT_ID_CHARS = 32768;
18
+ const MAX_RECALL_QUERY_CHARS = 200;
19
+ function evidenceWatermark(refs) {
20
+ return [...refs].map((ref) => `${ref.sessionId}:${ref.seq}:${ref.kind}`).sort().join("|");
21
+ }
22
+ function sessionCwd(session) {
23
+ if (!session || typeof session !== "object") return void 0;
24
+ const row = session;
25
+ const cwd = row.meta?.cwd ?? row.header?.cwd;
26
+ return typeof cwd === "string" ? cwd : void 0;
27
+ }
28
+ function fail(code, message) {
29
+ return {
30
+ ok: false,
31
+ error: {
32
+ code,
33
+ message
34
+ }
35
+ };
36
+ }
37
+ const MEMORY_UNAVAILABLE_MESSAGE = "记忆服务不可用。请先单独启用「记忆」插件;启用自我改进不会自动打开记忆。";
38
+ const MEMORY_UNAVAILABLE_MESSAGE_EN = "Memory is unavailable. Enable the Memory plugin separately; turning on self-improvement does not enable Memory.";
39
+ //#endregion
40
+ export { CHAT_EVENTS_SLOT, EXTRACT_PURPOSE, LESSON_INJECTION_SECTION, LESSON_SCHEMA_TAG, LESSON_SCHEMA_VERSION, MAX_INJECTED_LESSONS, MAX_INJECTION_TOKENS, MAX_PROJECT_ID_CHARS, MAX_RECALL_QUERY_CHARS, MEMORY_UNAVAILABLE_MESSAGE, MEMORY_UNAVAILABLE_MESSAGE_EN, SELF_IMPROVEMENT_ACTIVATE_ID, SELF_IMPROVEMENT_PLUGIN, SELF_IMPROVEMENT_RPC_CHANNEL, SELF_IMPROVEMENT_SOURCE_KIND, evidenceWatermark, fail, projectIdFromCwd, sessionCwd };
41
+
42
+ //# sourceMappingURL=contracts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contracts.js","names":["FROZEN_CHAT_EVENTS_SLOT","sharedProjectIdFromCwd"],"sources":["../src/contracts.ts"],"sourcesContent":["/** Own records plus reexported frozen AI/Memory interfaces. */\nimport {\n CHAT_EVENTS_SLOT as FROZEN_CHAT_EVENTS_SLOT,\n projectIdFromCwd as sharedProjectIdFromCwd,\n} from '@klarkxy/dsh-ai-services/contracts'\nimport type {\n AiFeatureScope,\n AiServices,\n AuxiliaryRequest,\n AuxiliaryResult,\n EvidenceRef,\n KnowledgeKind,\n KnowledgeScope,\n MemoryMutationOptions,\n MemoryQuery,\n MemoryRecord,\n MemoryService,\n NewMemoryRecord,\n PurposeSpec,\n ProducerMessageSource,\n RpcResult,\n UsageReceipt,\n} from '@klarkxy/dsh-ai-services/contracts'\n\nexport type {\n AiFeatureScope,\n AiServices,\n AuxiliaryRequest,\n AuxiliaryResult,\n EvidenceRef,\n KnowledgeKind,\n KnowledgeScope,\n MemoryMutationOptions,\n MemoryQuery,\n MemoryRecord,\n MemoryService,\n NewMemoryRecord,\n PurposeSpec,\n ProducerMessageSource,\n RpcResult,\n UsageReceipt,\n}\n\nexport const CHAT_EVENTS_SLOT = FROZEN_CHAT_EVENTS_SLOT\nexport const projectIdFromCwd = sharedProjectIdFromCwd\nexport const SELF_IMPROVEMENT_RPC_CHANNEL = '/dsh-self-improvement'\nexport const SELF_IMPROVEMENT_PLUGIN = '@klarkxy/dsh-self-improvement'\nexport const SELF_IMPROVEMENT_SOURCE_KIND = 'plugin:@klarkxy/dsh-self-improvement' as const\n/** Production activate identity. Core owns the scoped pluginName regex. */\nexport const SELF_IMPROVEMENT_ACTIVATE_ID = SELF_IMPROVEMENT_PLUGIN\nexport const EXTRACT_PURPOSE = 'self-improvement.extract'\nexport const LESSON_INJECTION_SECTION = 'dsh-self-improvement:lessons'\nexport const LESSON_SCHEMA_TAG = 'lesson-schema:1'\nexport const LESSON_SCHEMA_VERSION = 1\nexport const MAX_INJECTED_LESSONS = 5\nexport const MAX_INJECTION_TOKENS = 800\nexport const MAX_PROJECT_ID_CHARS = 32_768\nexport const MAX_RECALL_QUERY_CHARS = 200\n\nexport type SkillStatus = 'preview' | 'accepted' | 'rejected' | 'revoked'\nexport type SkillExportState = 'none' | 'recorded' | 'revoked'\nexport interface SkillAuditEntry { at: number; action: string; detail?: string }\nexport interface SkillSourceRef {\n id: string\n revision: number\n status: MemoryRecord['status']\n scope: KnowledgeScope\n expiresAt?: number\n}\nexport interface SkillRecord {\n id: string\n revision: number\n status: SkillStatus\n lessonIds: string[]\n sources: SkillSourceRef[]\n title: string\n markdown: string\n createdAt: number\n updatedAt: number\n exportState: SkillExportState\n exportedAt?: number\n exportRevokedAt?: number\n exportFilename?: string\n audit: SkillAuditEntry[]\n}\nexport interface SessionWatermark {\n sessionId: string\n seq: number\n projectId?: string\n updatedAt: number\n}\n\nexport type LessonTriggerKind = 'human-correction' | 'verified-tool-fix' | 'manual'\nexport interface LessonTrigger {\n kind: LessonTriggerKind\n evidence: EvidenceRef[]\n titleHint: string\n contentHint: string\n toolName?: string\n}\n\nexport interface LessonInjectPayload {\n source: ProducerMessageSource & {\n kind: typeof SELF_IMPROVEMENT_SOURCE_KIND\n plugin: typeof SELF_IMPROVEMENT_PLUGIN\n form: 'snapshot'\n sections: Array<{ name: string; text: string }>\n }\n content: Array<{ type: 'text'; text: string }>\n}\n\nexport interface PreStepEnter { kind: 'enter'; messages: unknown[]; startsRequestSeries?: true }\nexport type PreStepDecision = { kind: 'reject' } | PreStepEnter\n\nexport interface ReviewSnapshot {\n memoryAvailable: boolean\n memoryMessage?: string\n projectId?: string\n generation: number\n storageFailed: boolean\n lessons: MemoryRecord[]\n skills: SkillRecord[]\n /** True while an extract for this session (or any session, if none requested) is queued or in flight. */\n extracting?: boolean\n}\n\nexport function evidenceWatermark(refs: readonly EvidenceRef[]): string {\n return [...refs].map(ref => `${ref.sessionId}:${ref.seq}:${ref.kind}`).sort().join('|')\n}\n\nexport function sessionCwd(session: unknown): string | undefined {\n if (!session || typeof session !== 'object') return undefined\n const row = session as { meta?: { cwd?: unknown }; header?: { cwd?: unknown } }\n const cwd = row.meta?.cwd ?? row.header?.cwd\n return typeof cwd === 'string' ? cwd : undefined\n}\n\nexport function fail(code: string, message: string): RpcResult<never> {\n return { ok: false, error: { code, message } }\n}\n\nexport const MEMORY_UNAVAILABLE_MESSAGE = '记忆服务不可用。请先单独启用「记忆」插件;启用自我改进不会自动打开记忆。'\nexport const MEMORY_UNAVAILABLE_MESSAGE_EN = 'Memory is unavailable. Enable the Memory plugin separately; turning on self-improvement does not enable Memory.'\n"],"mappings":";;;AA2CA,MAAa,mBAAmBA;AAChC,MAAa,mBAAmBC;AAChC,MAAa,+BAA+B;AAC5C,MAAa,0BAA0B;AACvC,MAAa,+BAA+B;;AAE5C,MAAa,+BAA+B;AAC5C,MAAa,kBAAkB;AAC/B,MAAa,2BAA2B;AACxC,MAAa,oBAAoB;AACjC,MAAa,wBAAwB;AACrC,MAAa,uBAAuB;AACpC,MAAa,uBAAuB;AACpC,MAAa,uBAAuB;AACpC,MAAa,yBAAyB;AAqEtC,SAAgB,kBAAkB,MAAsC;CACtE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,KAAI,QAAO,GAAG,IAAI,UAAU,GAAG,IAAI,IAAI,GAAG,IAAI,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG;AACxF;AAEA,SAAgB,WAAW,SAAsC;CAC/D,IAAI,CAAC,WAAW,OAAO,YAAY,UAAU,OAAO,KAAA;CACpD,MAAM,MAAM;CACZ,MAAM,MAAM,IAAI,MAAM,OAAO,IAAI,QAAQ;CACzC,OAAO,OAAO,QAAQ,WAAW,MAAM,KAAA;AACzC;AAEA,SAAgB,KAAK,MAAc,SAAmC;CACpE,OAAO;EAAE,IAAI;EAAO,OAAO;GAAE;GAAM;EAAQ;CAAE;AAC/C;AAEA,MAAa,6BAA6B;AAC1C,MAAa,gCAAgC"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,118 @@
1
+ import { AiServices, CHAT_EVENTS_SLOT, MemoryRecord, MemoryService, PreStepDecision, ReviewSnapshot, RpcResult, SELF_IMPROVEMENT_RPC_CHANNEL, SessionWatermark, SkillRecord } from "./contracts.js";
2
+ import { Context } from "@deepseek-ai/cordis";
3
+ //#region src/detect.d.ts
4
+ interface SessionEventLike {
5
+ type: string;
6
+ seq: number;
7
+ data: unknown;
8
+ }
9
+ //#endregion
10
+ //#region src/engine.d.ts
11
+ interface KvTableLike<V> {
12
+ get(key: string): V | undefined;
13
+ entries(): IterableIterator<[string, V]>;
14
+ put(key: string, value: V): Promise<void>;
15
+ }
16
+ interface SessionLike {
17
+ id?: unknown;
18
+ header?: {
19
+ cwd?: string;
20
+ };
21
+ meta?: {
22
+ cwd?: string;
23
+ };
24
+ snapshotEvents(): readonly SessionEventLike[];
25
+ }
26
+ interface EngineOptions {
27
+ memory: () => MemoryService | undefined;
28
+ ai: () => Pick<AiServices, 'activate'> | undefined;
29
+ sessionOf: (sessionId: string) => SessionLike | undefined;
30
+ skills: KvTableLike<SkillRecord>;
31
+ watermarks: KvTableLike<SessionWatermark>;
32
+ now?: () => number;
33
+ }
34
+ declare class SelfImprovementEngine {
35
+ private readonly options;
36
+ private generation;
37
+ private active;
38
+ private lifetime;
39
+ private storageFailed;
40
+ private chain;
41
+ private readonly extractJobs;
42
+ private aiScope;
43
+ private seenProjects;
44
+ private readonly now;
45
+ constructor(options: EngineOptions);
46
+ getGeneration(): number;
47
+ isActive(): boolean;
48
+ dispose(): void;
49
+ private memoryOrError;
50
+ private asMemory;
51
+ private requireActive;
52
+ private persist;
53
+ private currentSignal;
54
+ private rememberProject;
55
+ private knownProjectIds;
56
+ private sameMemory;
57
+ private beginWork;
58
+ private isWork;
59
+ /** Linearize disable/replace against captured generation + Memory. Never start a new write after this fails. */
60
+ private revalidate;
61
+ private failed;
62
+ private mutationGuards;
63
+ private isGuardRejection;
64
+ /** Invoke a Memory/Skill write only while still current. Queued Memory work receives captured generation/identity + lifetime abort. An already-started write is not rolled back; the RPC still fails if we are no longer current after it. */
65
+ private mutateWhileCurrent;
66
+ private rejectionOrThrow;
67
+ listAllLessons(projectId?: string, expectedMemory?: MemoryService): Promise<MemoryRecord[]>;
68
+ currentRequestText(session: SessionLike, decision?: PreStepDecision): string;
69
+ recordsForInjection(projectId: string | undefined, requestText: string, expectedMemory?: MemoryService): Promise<MemoryRecord[]>;
70
+ applyPreStep(session: SessionLike, decision: PreStepDecision, signal: AbortSignal): Promise<PreStepDecision>;
71
+ private writeWatermark;
72
+ private ensureAi;
73
+ private draftTrigger;
74
+ private noteExtractStart;
75
+ private noteExtractEnd;
76
+ private isExtracting;
77
+ private enqueue;
78
+ /** Queue auto and manual extract on the existing chain. Counts rise synchronously at request entry. */
79
+ private enqueueExtract;
80
+ extractFromSession(session: SessionLike, signal: AbortSignal, mode?: 'auto' | 'manual'): Promise<RpcResult<{
81
+ created: MemoryRecord[];
82
+ skipped: number;
83
+ }>>;
84
+ considerSession(session: SessionLike, signal: AbortSignal): Promise<void>;
85
+ private runExtractFromSession;
86
+ private requireLesson;
87
+ acceptLesson(id: string, expectedRevision: number, scope: 'project' | 'global', currentProjectId?: string): Promise<RpcResult<MemoryRecord>>;
88
+ setLessonStatus(id: string, expectedRevision: number, status: 'rejected' | 'revoked', extraProjectId?: string): Promise<RpcResult<MemoryRecord>>;
89
+ private skills;
90
+ private liveSkillLessons;
91
+ previewSkill(lessonIds: string[]): Promise<RpcResult<SkillRecord>>;
92
+ private skillOrError;
93
+ private requireCurrentSkillSources;
94
+ patchSkill(id: string, expectedRevision: number, patch: (current: SkillRecord) => SkillRecord): Promise<RpcResult<SkillRecord>>;
95
+ acceptSkill(id: string, expectedRevision: number): Promise<RpcResult<SkillRecord>>;
96
+ prepareSkillExport(id: string, expectedRevision: number): Promise<RpcResult<{
97
+ filename: string;
98
+ markdown: string;
99
+ skill: SkillRecord;
100
+ }>>;
101
+ recordSkillExport(id: string, expectedRevision: number, filename: string): Promise<RpcResult<SkillRecord>>;
102
+ snapshot(sessionId?: string): Promise<RpcResult<ReviewSnapshot>>;
103
+ call(endpoint: string, payload: unknown, signal: AbortSignal): Promise<RpcResult>;
104
+ }
105
+ //#endregion
106
+ //#region src/index.d.ts
107
+ declare const name = "@klarkxy/dsh-self-improvement";
108
+ declare const inject: readonly ["storageDomain", "connection", "webServer"];
109
+ declare module '@deepseek-ai/cordis' {
110
+ interface Context {
111
+ selfImprovement: SelfImprovementEngine;
112
+ }
113
+ }
114
+ declare function listen(ctx: Context, name: string, handler: (...args: never[]) => unknown): (() => void) | undefined;
115
+ declare function apply(ctx: Context): Promise<void>;
116
+ //#endregion
117
+ export { CHAT_EVENTS_SLOT, SELF_IMPROVEMENT_RPC_CHANNEL, SelfImprovementEngine, apply, inject, listen, name };
118
+ //# sourceMappingURL=index.d.ts.map