@linxin666/dsh-pet 0.1.14 → 0.1.15
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/client.js +29 -16
- package/lib/client.js.map +1 -1
- package/lib/index.js +355 -251
- package/lib/invariant.js +1 -1
- package/lib/{state-P-wsVJ6O.js → state-CFyJv0sQ.js} +15 -1
- package/lib/types/affinity.d.ts +15 -0
- package/lib/types/affinity.d.ts.map +1 -1
- package/lib/types/affinity.js +14 -0
- package/lib/types/client/PluginSettingsCard.d.ts +26 -11
- package/lib/types/client/PluginSettingsCard.d.ts.map +1 -1
- package/lib/types/client/PluginSettingsCard.js +27 -7
- package/lib/types/client/index.d.ts +1 -1
- package/lib/types/client/index.js +3 -3
- package/lib/types/client/settings-form.d.ts +14 -5
- package/lib/types/client/settings-form.d.ts.map +1 -1
- package/lib/types/client/settings-form.js +38 -10
- package/lib/types/dsh-home.d.ts +17 -0
- package/lib/types/dsh-home.d.ts.map +1 -0
- package/lib/types/dsh-home.js +34 -0
- package/lib/types/event-projection.d.ts +36 -0
- package/lib/types/event-projection.d.ts.map +1 -0
- package/lib/types/event-projection.js +98 -0
- package/lib/types/ledger.d.ts +77 -0
- package/lib/types/ledger.d.ts.map +1 -0
- package/lib/types/ledger.js +139 -0
- package/lib/types/persist.d.ts +5 -1
- package/lib/types/persist.d.ts.map +1 -1
- package/lib/types/persist.js +7 -3
- package/lib/types/service.d.ts +15 -46
- package/lib/types/service.d.ts.map +1 -1
- package/lib/types/service.js +47 -204
- package/package.json +1 -1
- package/src/affinity.ts +33 -0
- package/src/client/PluginSettingsCard.tsx +83 -17
- package/src/client/index.ts +3 -3
- package/src/client/settings-card.module.css +6 -0
- package/src/client/settings-form.ts +41 -9
- package/src/dsh-home.test.ts +35 -0
- package/src/dsh-home.ts +36 -0
- package/src/event-projection.ts +128 -0
- package/src/ledger.test.ts +67 -0
- package/src/ledger.ts +183 -0
- package/src/persist.ts +7 -3
- package/src/service.ts +60 -274
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pet affinity economy (ledger) — composes the pure affinity and treats
|
|
3
|
+
* modules with the cooldown/dedup bookkeeping and emits updated persistence
|
|
4
|
+
* snapshots, marking dirty so the owning facade decides when to flush. Read
|
|
5
|
+
* paths (view) no longer settle the economy; settlements happen on explicit
|
|
6
|
+
* economic events: completed-turn rewards (official or legacy) and feeds.
|
|
7
|
+
* @module @linxin666/dsh-pet/ledger
|
|
8
|
+
*/
|
|
9
|
+
import { type AffinityConfig, type PetAffinityView, type PetInteraction } from './affinity.ts';
|
|
10
|
+
import { type TreatConfig } from './treats.ts';
|
|
11
|
+
import type { PetDisplayConfig, PetPersist } from './persist.ts';
|
|
12
|
+
/** Tuning overrides for the affinity economy. */
|
|
13
|
+
export interface LedgerConfig {
|
|
14
|
+
affinity?: Partial<AffinityConfig>;
|
|
15
|
+
treats?: Partial<TreatConfig>;
|
|
16
|
+
}
|
|
17
|
+
/** Result of one ledger interaction (the shape the pet RPC returns). */
|
|
18
|
+
export interface LedgerInteractionResult {
|
|
19
|
+
/** Reaction copy bubble. */
|
|
20
|
+
reaction: string;
|
|
21
|
+
/** Points gained (0 when inside the cooldown). */
|
|
22
|
+
delta: number;
|
|
23
|
+
/** Full affinity snapshot (same shape as the state view). */
|
|
24
|
+
affinity: PetAffinityView;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Holds the current persistence snapshot and all economy bookkeeping. Every
|
|
28
|
+
* mutating call flags takeDirty so the facade persists exactly once per
|
|
29
|
+
* batch of changes; read methods (snapshot, affinityView) never write.
|
|
30
|
+
*/
|
|
31
|
+
export declare class PetLedger {
|
|
32
|
+
private readonly affinityConfig;
|
|
33
|
+
private readonly treatConfig;
|
|
34
|
+
private current;
|
|
35
|
+
/** Completed turns already rewarded, per session (turn numbers are per-session). */
|
|
36
|
+
private rewardedTurns;
|
|
37
|
+
private lastLegacyTurnRewardAt;
|
|
38
|
+
private dirty;
|
|
39
|
+
constructor(persist: PetPersist, config?: LedgerConfig);
|
|
40
|
+
/** Affinity cooldown/rank tuning (read-only). */
|
|
41
|
+
get affinity(): AffinityConfig;
|
|
42
|
+
/** The current persistence snapshot (trade a copy when mutating). */
|
|
43
|
+
get snapshot(): PetPersist;
|
|
44
|
+
/** Stock cap reported to clients. */
|
|
45
|
+
get treatMax(): number;
|
|
46
|
+
/** Consume the pending-write flag if any mutation occurred. */
|
|
47
|
+
takeDirty(): boolean;
|
|
48
|
+
/** Replace the display block (clamping stays a caller concern). */
|
|
49
|
+
setDisplay(display: PetDisplayConfig): void;
|
|
50
|
+
/** Replace the pet display name (validation stays a caller concern). */
|
|
51
|
+
setName(name: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Settle the treat economy (work + time output since the last settlement).
|
|
54
|
+
* A zero-gain first settlement still starts the time clock (anchor write),
|
|
55
|
+
* which is how the 30-minute time output can ever accrue. Returns true when
|
|
56
|
+
* the in-memory ledger changed and should be persisted.
|
|
57
|
+
*/
|
|
58
|
+
settleTreats(nowMs: number): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Award the completed-turn reward once per session+turn (idempotent) and
|
|
61
|
+
* run the treat settlement that work output feeds. Returns true when the
|
|
62
|
+
* snapshot changed.
|
|
63
|
+
*/
|
|
64
|
+
rewardTurn(sessionId: string, turn: number, nowMs: number): boolean;
|
|
65
|
+
/** Preserve turn rewards for installations that only emit legacy activity. */
|
|
66
|
+
rewardLegacyTurn(nowMs: number): boolean;
|
|
67
|
+
private applyTurnReward;
|
|
68
|
+
/**
|
|
69
|
+
* Pet or feed the pet. Feeding settles first, then gates on the feed
|
|
70
|
+
* cooldown before spending stock — a feed inside the cooldown must not burn
|
|
71
|
+
* a treat for nothing.
|
|
72
|
+
*/
|
|
73
|
+
interact(kind: PetInteraction, nowMs: number): LedgerInteractionResult;
|
|
74
|
+
/** Current affinity view for the RPC snapshot. */
|
|
75
|
+
affinityView(nowMs: number): PetAffinityView;
|
|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IAMpB,mEAAmE;IACnE,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAK3C,wEAAwE;IACxE,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK3B;;;;;OAKG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAapC;;;;OAIG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IASnE,8EAA8E;IAC9E,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IASxC,OAAO,CAAC,eAAe;IASvB;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,GAAG,uBAAuB;IAyBtE,kDAAkD;IAClD,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe;CAG7C"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pet affinity economy (ledger) — composes the pure affinity and treats
|
|
3
|
+
* modules with the cooldown/dedup bookkeeping and emits updated persistence
|
|
4
|
+
* snapshots, marking dirty so the owning facade decides when to flush. Read
|
|
5
|
+
* paths (view) no longer settle the economy; settlements happen on explicit
|
|
6
|
+
* economic events: completed-turn rewards (official or legacy) and feeds.
|
|
7
|
+
* @module @linxin666/dsh-pet/ledger
|
|
8
|
+
*/
|
|
9
|
+
import { applyInteraction, affinityViewOf, applyTurnReward, defaultAffinityConfig, } from "./affinity.js";
|
|
10
|
+
import { consumeTreat, defaultTreatConfig, settleTreatGrants, } from "./treats.js";
|
|
11
|
+
/**
|
|
12
|
+
* Holds the current persistence snapshot and all economy bookkeeping. Every
|
|
13
|
+
* mutating call flags takeDirty so the facade persists exactly once per
|
|
14
|
+
* batch of changes; read methods (snapshot, affinityView) never write.
|
|
15
|
+
*/
|
|
16
|
+
export class PetLedger {
|
|
17
|
+
affinityConfig;
|
|
18
|
+
treatConfig;
|
|
19
|
+
current;
|
|
20
|
+
/** Completed turns already rewarded, per session (turn numbers are per-session). */
|
|
21
|
+
rewardedTurns = new Map();
|
|
22
|
+
lastLegacyTurnRewardAt = 0;
|
|
23
|
+
dirty = false;
|
|
24
|
+
constructor(persist, config = {}) {
|
|
25
|
+
this.affinityConfig = { ...defaultAffinityConfig, ...(config.affinity ?? {}) };
|
|
26
|
+
this.treatConfig = { ...defaultTreatConfig, ...(config.treats ?? {}) };
|
|
27
|
+
this.current = persist;
|
|
28
|
+
}
|
|
29
|
+
/** Affinity cooldown/rank tuning (read-only). */
|
|
30
|
+
get affinity() {
|
|
31
|
+
return this.affinityConfig;
|
|
32
|
+
}
|
|
33
|
+
/** The current persistence snapshot (trade a copy when mutating). */
|
|
34
|
+
get snapshot() {
|
|
35
|
+
return this.current;
|
|
36
|
+
}
|
|
37
|
+
/** Stock cap reported to clients. */
|
|
38
|
+
get treatMax() {
|
|
39
|
+
return this.treatConfig.maxTreats;
|
|
40
|
+
}
|
|
41
|
+
/** Consume the pending-write flag if any mutation occurred. */
|
|
42
|
+
takeDirty() {
|
|
43
|
+
const was = this.dirty;
|
|
44
|
+
this.dirty = false;
|
|
45
|
+
return was;
|
|
46
|
+
}
|
|
47
|
+
/** Replace the display block (clamping stays a caller concern). */
|
|
48
|
+
setDisplay(display) {
|
|
49
|
+
this.current = { ...this.current, display };
|
|
50
|
+
this.dirty = true;
|
|
51
|
+
}
|
|
52
|
+
/** Replace the pet display name (validation stays a caller concern). */
|
|
53
|
+
setName(name) {
|
|
54
|
+
this.current = { ...this.current, name };
|
|
55
|
+
this.dirty = true;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Settle the treat economy (work + time output since the last settlement).
|
|
59
|
+
* A zero-gain first settlement still starts the time clock (anchor write),
|
|
60
|
+
* which is how the 30-minute time output can ever accrue. Returns true when
|
|
61
|
+
* the in-memory ledger changed and should be persisted.
|
|
62
|
+
*/
|
|
63
|
+
settleTreats(nowMs) {
|
|
64
|
+
const settlement = settleTreatGrants(this.current.treats, this.current.affinity.turns, nowMs, this.treatConfig);
|
|
65
|
+
if (settlement.ledger === this.current.treats)
|
|
66
|
+
return false;
|
|
67
|
+
this.current = { ...this.current, treats: settlement.ledger };
|
|
68
|
+
this.dirty = true;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Award the completed-turn reward once per session+turn (idempotent) and
|
|
73
|
+
* run the treat settlement that work output feeds. Returns true when the
|
|
74
|
+
* snapshot changed.
|
|
75
|
+
*/
|
|
76
|
+
rewardTurn(sessionId, turn, nowMs) {
|
|
77
|
+
const last = this.rewardedTurns.get(sessionId) ?? 0;
|
|
78
|
+
if (turn <= last)
|
|
79
|
+
return false;
|
|
80
|
+
this.rewardedTurns.set(sessionId, turn);
|
|
81
|
+
let changed = this.applyTurnReward();
|
|
82
|
+
if (this.settleTreats(nowMs))
|
|
83
|
+
changed = true;
|
|
84
|
+
return changed;
|
|
85
|
+
}
|
|
86
|
+
/** Preserve turn rewards for installations that only emit legacy activity. */
|
|
87
|
+
rewardLegacyTurn(nowMs) {
|
|
88
|
+
// A legacy done snapshot may repeat during the celebration window.
|
|
89
|
+
if (nowMs - this.lastLegacyTurnRewardAt < 5_000)
|
|
90
|
+
return false;
|
|
91
|
+
this.lastLegacyTurnRewardAt = nowMs;
|
|
92
|
+
let changed = this.applyTurnReward();
|
|
93
|
+
if (this.settleTreats(nowMs))
|
|
94
|
+
changed = true;
|
|
95
|
+
return changed;
|
|
96
|
+
}
|
|
97
|
+
applyTurnReward() {
|
|
98
|
+
this.current = {
|
|
99
|
+
...this.current,
|
|
100
|
+
affinity: applyTurnReward(this.current.affinity, this.affinityConfig),
|
|
101
|
+
};
|
|
102
|
+
this.dirty = true;
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Pet or feed the pet. Feeding settles first, then gates on the feed
|
|
107
|
+
* cooldown before spending stock — a feed inside the cooldown must not burn
|
|
108
|
+
* a treat for nothing.
|
|
109
|
+
*/
|
|
110
|
+
interact(kind, nowMs) {
|
|
111
|
+
if (kind === 'feed')
|
|
112
|
+
this.settleTreats(nowMs);
|
|
113
|
+
const outcome = applyInteraction(this.current.affinity, kind, nowMs, this.affinityConfig);
|
|
114
|
+
if (kind === 'feed' && !outcome.accepted) {
|
|
115
|
+
return { reaction: outcome.reaction, delta: 0, affinity: this.affinityView(nowMs) };
|
|
116
|
+
}
|
|
117
|
+
if (kind === 'feed') {
|
|
118
|
+
const consume = consumeTreat(this.current.treats);
|
|
119
|
+
if (!consume.ok) {
|
|
120
|
+
return {
|
|
121
|
+
reaction: '没有小鱼干了,多陪鲸鱼娘工作一会儿吧~',
|
|
122
|
+
delta: 0,
|
|
123
|
+
affinity: this.affinityView(nowMs),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
this.current = { ...this.current, treats: consume.ledger };
|
|
127
|
+
this.dirty = true;
|
|
128
|
+
}
|
|
129
|
+
if (outcome.accepted) {
|
|
130
|
+
this.current = { ...this.current, affinity: outcome.affinity };
|
|
131
|
+
this.dirty = true;
|
|
132
|
+
}
|
|
133
|
+
return { reaction: outcome.reaction, delta: outcome.delta, affinity: this.affinityView(nowMs) };
|
|
134
|
+
}
|
|
135
|
+
/** Current affinity view for the RPC snapshot. */
|
|
136
|
+
affinityView(nowMs) {
|
|
137
|
+
return affinityViewOf(this.current.affinity, nowMs, this.affinityConfig);
|
|
138
|
+
}
|
|
139
|
+
}
|
package/lib/types/persist.d.ts
CHANGED
|
@@ -36,7 +36,11 @@ export declare const DEFAULT_PET_NAME = "\u9CB8\u9C7C\u5A18";
|
|
|
36
36
|
/** Name constraints. */
|
|
37
37
|
export declare const PET_NAME_MAX_LENGTH = 20;
|
|
38
38
|
export declare function emptyPersist(): PetPersist;
|
|
39
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Resolve the persistence directory ($DSH_HOME or ~/.dsh). Delegates to the
|
|
41
|
+
* shared {@link dshHome} resolution so the plugin family keeps one DSH_HOME
|
|
42
|
+
* definition (env override, ~ expansion, cwd-joined relative values).
|
|
43
|
+
*/
|
|
40
44
|
export declare function petHomeDir(): string;
|
|
41
45
|
/** Load persisted state; missing or corrupt files fall back to defaults. */
|
|
42
46
|
export declare function loadPetPersist(dir?: string): PetPersist;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.d.ts","sourceRoot":"","sources":["../../src/persist.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,EAA+B,KAAK,aAAa,EAAE,MAAM,eAAe,CAAA;AAC/E,OAAO,EAAwC,KAAK,WAAW,EAAE,MAAM,aAAa,CAAA;AAEpF,gDAAgD;AAChD,MAAM,WAAW,gBAAgB;IAC/B,qBAAqB;IACrB,OAAO,EAAE,OAAO,CAAA;IAChB,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAA;IACZ,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAA;IACb,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAA;CACf;AAED,eAAO,MAAM,oBAAoB,EAAE,gBAKlC,CAAA;AAED,2EAA2E;AAC3E,eAAO,MAAM,gBAAgB,KAAK,CAAA;AAClC,eAAO,MAAM,gBAAgB,MAAM,CAAA;AACnC,eAAO,MAAM,iBAAiB,QAAS,CAAA;AAEvC,wCAAwC;AACxC,MAAM,WAAW,UAAU;IACzB,0CAA0C;IAC1C,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,aAAa,CAAA;IACvB,gCAAgC;IAChC,MAAM,EAAE,WAAW,CAAA;IACnB,OAAO,EAAE,gBAAgB,CAAA;CAC1B;AAED,8DAA8D;AAC9D,eAAO,MAAM,gBAAgB,uBAAQ,CAAA;AAErC,wBAAwB;AACxB,eAAO,MAAM,mBAAmB,KAAK,CAAA;AAErC,wBAAgB,YAAY,IAAI,UAAU,CAOzC;AAED
|
|
1
|
+
{"version":3,"file":"persist.d.ts","sourceRoot":"","sources":["../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FileSync, renameSync, writeFileSync } from 'node:fs';
|
|
8
8
|
import { join } from 'node:path';
|
|
9
|
-
import {
|
|
9
|
+
import { dshHome } from "./dsh-home.js";
|
|
10
10
|
import { AFFINITY_MAX, emptyAffinity } from "./affinity.js";
|
|
11
11
|
import { defaultTreatConfig, emptyTreatLedger } from "./treats.js";
|
|
12
12
|
export const defaultDisplayConfig = {
|
|
@@ -31,9 +31,13 @@ export function emptyPersist() {
|
|
|
31
31
|
display: { ...defaultDisplayConfig },
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Resolve the persistence directory ($DSH_HOME or ~/.dsh). Delegates to the
|
|
36
|
+
* shared {@link dshHome} resolution so the plugin family keeps one DSH_HOME
|
|
37
|
+
* definition (env override, ~ expansion, cwd-joined relative values).
|
|
38
|
+
*/
|
|
35
39
|
export function petHomeDir() {
|
|
36
|
-
return
|
|
40
|
+
return dshHome();
|
|
37
41
|
}
|
|
38
42
|
/** Numeric field guard: finite numbers only, else the fallback. */
|
|
39
43
|
function finiteNum(value, fallback) {
|
package/lib/types/service.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pet host service — the `pet.*` RPC domain.
|
|
3
|
-
*
|
|
4
|
-
* affinity
|
|
5
|
-
*
|
|
6
|
-
* `pet.setVisible` / `pet.setConfig` for
|
|
2
|
+
* Pet host service — the `pet.*` RPC domain. A composition facade: it wires
|
|
3
|
+
* the pure event projection (`event-projection`) onto the state machine,
|
|
4
|
+
* delegates the affinity economy to the ledger (`ledger`), and routes
|
|
5
|
+
* persistence through `persist`. The API gateway maps these methods onto
|
|
6
|
+
* `pet.state` / `pet.interact` / `pet.setVisible` / `pet.setConfig` for
|
|
7
|
+
* browser consumers.
|
|
7
8
|
* @module @linxin666/dsh-pet/service
|
|
8
9
|
*/
|
|
9
10
|
import { Context, Service } from '@deepseek-ai/cordis';
|
|
10
|
-
import {
|
|
11
|
+
import type { AffinityConfig, PetAffinityView, PetInteraction } from './affinity.ts';
|
|
12
|
+
import type { TreatConfig } from './treats.ts';
|
|
13
|
+
import { type LedgerInteractionResult } from './ledger.ts';
|
|
11
14
|
import { type PetDisplayConfig } from './persist.ts';
|
|
12
|
-
import { type TreatConfig } from './treats.ts';
|
|
13
15
|
import { type PetStateConfig, type PetStateSnapshot } from './state.ts';
|
|
14
16
|
/** Plugin configuration. */
|
|
15
17
|
export interface PetConfig {
|
|
@@ -52,18 +54,7 @@ export interface PetStateView {
|
|
|
52
54
|
phase: PetStateSnapshot['phase'];
|
|
53
55
|
sessionActive: boolean;
|
|
54
56
|
/** Affinity ledger snapshot. */
|
|
55
|
-
affinity:
|
|
56
|
-
points: number;
|
|
57
|
-
rank: string;
|
|
58
|
-
rankEmoji: string;
|
|
59
|
-
pets: number;
|
|
60
|
-
feeds: number;
|
|
61
|
-
turns: number;
|
|
62
|
-
/** True while the pet interaction is inside its cooldown. */
|
|
63
|
-
petCooldown: boolean;
|
|
64
|
-
/** True while the feed is inside its cooldown. */
|
|
65
|
-
feedCooldown: boolean;
|
|
66
|
-
};
|
|
57
|
+
affinity: PetAffinityView;
|
|
67
58
|
/** Display configuration. */
|
|
68
59
|
display: PetDisplayConfig;
|
|
69
60
|
/** User-customizable pet display name. */
|
|
@@ -77,14 +68,7 @@ export interface PetStateView {
|
|
|
77
68
|
};
|
|
78
69
|
}
|
|
79
70
|
/** Result of `pet.interact`. */
|
|
80
|
-
export
|
|
81
|
-
/** Reaction copy bubble. */
|
|
82
|
-
reaction: string;
|
|
83
|
-
/** Points gained (0 when inside the cooldown). */
|
|
84
|
-
delta: number;
|
|
85
|
-
/** Full affinity snapshot (same shape as state view). */
|
|
86
|
-
affinity: PetStateView['affinity'];
|
|
87
|
-
}
|
|
71
|
+
export type PetInteractResult = LedgerInteractionResult;
|
|
88
72
|
declare module '@deepseek-ai/cordis' {
|
|
89
73
|
interface Context {
|
|
90
74
|
pet: PetService;
|
|
@@ -92,25 +76,20 @@ declare module '@deepseek-ai/cordis' {
|
|
|
92
76
|
}
|
|
93
77
|
/**
|
|
94
78
|
* Cordis service exposing the pet RPC domain. Lazy: nothing is scanned or
|
|
95
|
-
* written until
|
|
96
|
-
* in-memory state, and persistence happens on
|
|
97
|
-
*
|
|
79
|
+
* written until an economic event or interaction arrives; event listeners
|
|
80
|
+
* update only in-memory state, and persistence happens on economic changes
|
|
81
|
+
* (turn rewards, feeds, config/name changes) — never on a read.
|
|
98
82
|
*/
|
|
99
83
|
export declare class PetService extends Service {
|
|
100
84
|
static inject: string[];
|
|
101
85
|
private readonly machine;
|
|
102
|
-
private readonly
|
|
103
|
-
private readonly treatConfig;
|
|
86
|
+
private readonly ledger;
|
|
104
87
|
private readonly persistDir;
|
|
105
|
-
private persist;
|
|
106
|
-
/** Completed turns already rewarded, per session (turn numbers are per-session). */
|
|
107
|
-
private rewardedTurns;
|
|
108
88
|
private enabled;
|
|
109
89
|
private disposeActivity;
|
|
110
90
|
/** Session whose most recent meaningful event currently drives the global pet. */
|
|
111
91
|
private displaySession;
|
|
112
92
|
private readonly sessionActivity;
|
|
113
|
-
private lastLegacyTurnRewardAt;
|
|
114
93
|
constructor(ctx: Context, config?: PetConfig);
|
|
115
94
|
/** Whether the pet service consumes session activity while enabled. */
|
|
116
95
|
isEnabled(): boolean;
|
|
@@ -160,17 +139,7 @@ export declare class PetService extends Service {
|
|
|
160
139
|
private rewardTurn;
|
|
161
140
|
/** Preserve turn rewards for installations that only emit legacy activity. */
|
|
162
141
|
private rewardLegacyTurn;
|
|
163
|
-
/** Persist one accepted completed-turn reward. */
|
|
164
|
-
private applyTurnReward;
|
|
165
|
-
/**
|
|
166
|
-
* Settle the treat economy (work + time output since the last settlement)
|
|
167
|
-
* and persist whenever the ledger changed. A zero-gain first settlement
|
|
168
|
-
* still starts the time clock (anchor write), which is what lets the
|
|
169
|
-
* 30-minute time output ever accrue.
|
|
170
|
-
*/
|
|
171
|
-
private settleTreats;
|
|
172
142
|
private view;
|
|
173
|
-
private affinityView;
|
|
174
143
|
private flush;
|
|
175
144
|
}
|
|
176
145
|
//# sourceMappingURL=service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/service.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAEtD,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AACpF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAQ9C,OAAO,EAAgC,KAAK,uBAAuB,EAAE,MAAM,aAAa,CAAA;AACxF,OAAO,EAQL,KAAK,gBAAgB,EAEtB,MAAM,cAAc,CAAA;AACrB,OAAO,EAGL,KAAK,cAAc,EAEnB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAA;AAEnB,4BAA4B;AAC5B,MAAM,WAAW,SAAS;IACxB,uBAAuB;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;IAClC,4BAA4B;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;IAC/B,4BAA4B;IAC5B,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;IAC7B,8DAA8D;IAC9D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iEAAiE;IACjE,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,qBAAqB;IACrB,OAAO,EAAE,OAAO,CAAA;IAChB,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAA;IACZ,yDAAyD;IACzD,KAAK,EAAE,MAAM,CAAA;IACb,wDAAwD;IAC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