@lmzhen/dsh-evolution-state-domain 0.3.16 → 0.3.18
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/lib/index.js +31 -12
- package/lib/types/index.d.ts +3 -3
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { DomainError, defineDomain, domainTable } from "@deepseek-ai/dsh-storage-domain";
|
|
3
|
+
import { CLAIM_EXPIRY_MS, canClaimPending, canResolvePending, releasedStatus } from "@lmzhen/dsh-evolution-state-storage";
|
|
3
4
|
//#region lib/types/index.js
|
|
4
5
|
/**
|
|
5
6
|
* storage-domain provider for evolution state.
|
|
@@ -30,7 +31,6 @@ const pendingSchema = z.object({
|
|
|
30
31
|
kind: z.union([
|
|
31
32
|
z.literal("memory"),
|
|
32
33
|
z.literal("skill"),
|
|
33
|
-
z.literal("skill_batch"),
|
|
34
34
|
z.literal("capability")
|
|
35
35
|
]),
|
|
36
36
|
summary: z.string(),
|
|
@@ -38,6 +38,7 @@ const pendingSchema = z.object({
|
|
|
38
38
|
createdAt: z.string(),
|
|
39
39
|
status: z.union([
|
|
40
40
|
z.literal("pending"),
|
|
41
|
+
z.literal("executing"),
|
|
41
42
|
z.literal("approved"),
|
|
42
43
|
z.literal("rejected")
|
|
43
44
|
]),
|
|
@@ -103,6 +104,20 @@ function apply(ctx) {
|
|
|
103
104
|
async saveCuratorState(record) {
|
|
104
105
|
await (await ensure()).table("curator_state").put("primary", record);
|
|
105
106
|
},
|
|
107
|
+
async transactCuratorState(task) {
|
|
108
|
+
const table = (await ensure()).table("curator_state");
|
|
109
|
+
try {
|
|
110
|
+
await table.update("primary", (current) => task(current) ?? current);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof DomainError && error.code === "missing-key") {
|
|
113
|
+
const next = task(null);
|
|
114
|
+
if (next !== null) await table.put("primary", next);
|
|
115
|
+
else await table.delete("primary");
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
},
|
|
106
121
|
async listPending(status = "pending") {
|
|
107
122
|
return [...(await ensure()).table("pending").entries()].map(([, value]) => value).filter((record) => record.status === status);
|
|
108
123
|
},
|
|
@@ -115,11 +130,12 @@ function apply(ctx) {
|
|
|
115
130
|
const slot = { record: null };
|
|
116
131
|
const now = Date.now();
|
|
117
132
|
await table.update(id, (current) => {
|
|
118
|
-
if (current.status
|
|
133
|
+
if (!canClaimPending(current.status)) return current;
|
|
119
134
|
const claimedAt = typeof current.claimedAt === "string" ? Date.parse(current.claimedAt) : 0;
|
|
120
|
-
if (current.claimedBy !== void 0 && Number.isFinite(claimedAt) && now - claimedAt <
|
|
135
|
+
if (current.claimedBy !== void 0 && Number.isFinite(claimedAt) && now - claimedAt < CLAIM_EXPIRY_MS) return current;
|
|
121
136
|
slot.record = {
|
|
122
137
|
...current,
|
|
138
|
+
status: "executing",
|
|
123
139
|
claimedBy: claimId,
|
|
124
140
|
claimedAt: new Date(now).toISOString()
|
|
125
141
|
};
|
|
@@ -133,13 +149,15 @@ function apply(ctx) {
|
|
|
133
149
|
},
|
|
134
150
|
async releasePendingClaim(id, claimId) {
|
|
135
151
|
await (await ensure()).table("pending").update(id, (current) => {
|
|
136
|
-
if (current.status
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
152
|
+
if (current.status !== "pending" && current.status !== "executing") return current;
|
|
153
|
+
if (current.claimedBy !== claimId) return current;
|
|
154
|
+
const released = {
|
|
155
|
+
...current,
|
|
156
|
+
status: releasedStatus(current.status)
|
|
157
|
+
};
|
|
158
|
+
delete released.claimedBy;
|
|
159
|
+
delete released.claimedAt;
|
|
160
|
+
return released;
|
|
143
161
|
});
|
|
144
162
|
},
|
|
145
163
|
async tryResolvePending(id, status) {
|
|
@@ -147,7 +165,7 @@ function apply(ctx) {
|
|
|
147
165
|
try {
|
|
148
166
|
const resolved = { record: null };
|
|
149
167
|
const record = await table.update(id, (current) => {
|
|
150
|
-
if (current.status
|
|
168
|
+
if (!canResolvePending(current.status)) return current;
|
|
151
169
|
resolved.record = {
|
|
152
170
|
...current,
|
|
153
171
|
status,
|
|
@@ -156,7 +174,7 @@ function apply(ctx) {
|
|
|
156
174
|
return resolved.record;
|
|
157
175
|
});
|
|
158
176
|
if (resolved.record === null) return {
|
|
159
|
-
record
|
|
177
|
+
record,
|
|
160
178
|
applied: false
|
|
161
179
|
};
|
|
162
180
|
return {
|
|
@@ -176,6 +194,7 @@ function apply(ctx) {
|
|
|
176
194
|
const dispose = ctx.evolutionStateStorage.registerProvider(provider);
|
|
177
195
|
return async () => {
|
|
178
196
|
dispose();
|
|
197
|
+
if (opening) await opening.catch(() => null);
|
|
179
198
|
if (domain) await domain.close();
|
|
180
199
|
domain = null;
|
|
181
200
|
opening = null;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type { Context } from '@deepseek-ai/cordis';
|
|
11
11
|
import { z } from 'zod';
|
|
12
|
-
import type
|
|
12
|
+
import { type CuratorStateRecord, type PendingRecord, type ReviewStateRecord } from '@deepseek-ai/dsh-evolution-state-storage';
|
|
13
13
|
export declare const name = "evolution-state-domain";
|
|
14
14
|
export declare const inject: string[];
|
|
15
15
|
export declare const reviewStateSchema: z.ZodObject<{
|
|
@@ -26,11 +26,11 @@ export declare const curatorStateSchema: z.ZodObject<{
|
|
|
26
26
|
}, z.core.$strip>;
|
|
27
27
|
export declare const pendingSchema: z.ZodObject<{
|
|
28
28
|
id: z.ZodString;
|
|
29
|
-
kind: z.ZodUnion<readonly [z.ZodLiteral<"memory">, z.ZodLiteral<"skill">, z.ZodLiteral<"
|
|
29
|
+
kind: z.ZodUnion<readonly [z.ZodLiteral<"memory">, z.ZodLiteral<"skill">, z.ZodLiteral<"capability">]>;
|
|
30
30
|
summary: z.ZodString;
|
|
31
31
|
args: z.ZodUnknown;
|
|
32
32
|
createdAt: z.ZodString;
|
|
33
|
-
status: z.ZodUnion<readonly [z.ZodLiteral<"pending">, z.ZodLiteral<"approved">, z.ZodLiteral<"rejected">]>;
|
|
33
|
+
status: z.ZodUnion<readonly [z.ZodLiteral<"pending">, z.ZodLiteral<"executing">, z.ZodLiteral<"approved">, z.ZodLiteral<"rejected">]>;
|
|
34
34
|
resolvedAt: z.ZodOptional<z.ZodString>;
|
|
35
35
|
claimedBy: z.ZodOptional<z.ZodString>;
|
|
36
36
|
claimedAt: z.ZodOptional<z.ZodString>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lmzhen/dsh-evolution-state-domain",
|
|
3
3
|
"description": "storage-domain provider for evolution state (community build)",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.18",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"@deepseek-ai/dsh-invariants": "^0.1.1-rc.2",
|
|
39
39
|
"@deepseek-ai/cordis": "^4.0.1",
|
|
40
40
|
"@deepseek-ai/dsh-storage-domain": "^0.1.1-rc.2",
|
|
41
|
-
"@lmzhen/dsh-evolution-state-storage": "^0.3.
|
|
41
|
+
"@lmzhen/dsh-evolution-state-storage": "^0.3.18"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@deepseek-ai/dsh-invariants": "^0.1.1-rc.2",
|
|
45
45
|
"@deepseek-ai/dsh-storage-domain": "^0.1.1-rc.2",
|
|
46
|
-
"@lmzhen/dsh-evolution-state-storage": "^0.3.
|
|
46
|
+
"@lmzhen/dsh-evolution-state-storage": "^0.3.18"
|
|
47
47
|
}
|
|
48
48
|
}
|