@alexpeng/dsh-custom-plugin 0.1.2 → 0.1.4

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.
@@ -6,8 +6,17 @@
6
6
  */
7
7
  import type { AttachmentStore } from '@deepseek-ai/dsh-attachment';
8
8
  import type { SessionQueryEngine } from '@deepseek-ai/dsh-session-query';
9
- import type { BalanceInfo, CustomPluginState, TimelineItem } from './protocol.ts';
9
+ import { type CredentialStore } from './system-credentials.ts';
10
+ import type { BalanceInfo, CustomPluginPublicState, CustomPluginState, TimelineItem } from './protocol.ts';
10
11
  import { type UsageRecord } from './usage.ts';
12
+ type UsageScanResult = {
13
+ ok: true;
14
+ usageToday: unknown;
15
+ scannedSessions: number;
16
+ } | {
17
+ ok: false;
18
+ error: string;
19
+ };
11
20
  /** Options the loader entry supplies to the host service. */
12
21
  export interface CustomPluginHostOptions {
13
22
  sessionQuery: SessionQueryEngine;
@@ -25,6 +34,10 @@ export interface CustomPluginHostOptions {
25
34
  attachments?: AttachmentStore | undefined;
26
35
  /** Test seam: return the local mermaid engine file path (or null). */
27
36
  localMermaidPath?: () => string | null;
37
+ /** Test seam: read the DeepSeek credential from DSH's credentials file. */
38
+ readCredential?: () => Promise<string>;
39
+ /** Test seam and optional platform adapter for OS credential storage. */
40
+ credentialStore?: CredentialStore;
28
41
  }
29
42
  /** Host capabilities behind the routes and the agent tool. */
30
43
  export declare class CustomPluginHost {
@@ -34,11 +47,15 @@ export declare class CustomPluginHost {
34
47
  private readonly persistNow;
35
48
  private readonly diagReports;
36
49
  private readonly attachments;
50
+ private readonly credentialStore;
37
51
  private readonly sessionModel;
52
+ private usageScanPromise;
53
+ private lastUsagePruneDay;
38
54
  private mermaidBytesValue;
39
55
  private mermaidJs;
40
56
  private mermaidSource;
41
57
  private localMermaidPath;
58
+ private readCredential;
42
59
  constructor(options: CustomPluginHostOptions);
43
60
  /** Persist the state document (debounced by the loader entry). */
44
61
  persist(): Promise<void>;
@@ -50,8 +67,8 @@ export declare class CustomPluginHost {
50
67
  /** Fold one assistant/message usage record into the ledger. The event time
51
68
  * decides both the day bucket and the peak/off-peak portion. */
52
69
  foldUsage(sessionId: string, usage: UsageRecord, time?: number): void;
53
- /** Snapshot of the whole state document for the browser. */
54
- stateView(): Pick<CustomPluginState, 'cfg' | 'folders' | 'prompts' | 'stars' | 'apiKey' | 'usage'>;
70
+ /** Snapshot of browser-safe state; the API key value never crosses this boundary. */
71
+ stateView(): Promise<CustomPluginPublicState>;
55
72
  /** Apply a browser-side state edit (config, folders, prompts, stars, api key). */
56
73
  applyEdit(edit: {
57
74
  cfg?: Partial<CustomPluginState['cfg']>;
@@ -59,7 +76,9 @@ export declare class CustomPluginHost {
59
76
  prompts?: CustomPluginState['prompts'];
60
77
  stars?: CustomPluginState['stars'];
61
78
  apiKey?: string;
62
- }): void;
79
+ }): Promise<void>;
80
+ /** Move a legacy plaintext state key into the OS store when available. */
81
+ migrateLegacyApiKey(): Promise<boolean>;
63
82
  /** Timeline nodes for one session, capped to the 400-node tail. The tail cap
64
83
  * bounds the payload (each item carries up to 4k of preview text); 400 keeps
65
84
  * dots available when the GUI deep-loads history. */
@@ -81,8 +100,11 @@ export declare class CustomPluginHost {
81
100
  ok: false;
82
101
  error: string;
83
102
  }>;
84
- /** Resolve the DeepSeek balance API key: state value first, then environment, then DSH credential refs. */
103
+ /** Resolve the key and its source without returning either value to the browser. */
104
+ private resolveApiKeyWithSource;
85
105
  private resolveApiKey;
106
+ /** Browser-safe credential status used by state and save responses. */
107
+ credentialStatus(): Promise<Pick<CustomPluginPublicState, 'apiKeyConfigured' | 'credentialStorage'>>;
86
108
  /** Query the DeepSeek balance endpoint (15s timeout). */
87
109
  balanceGet(): Promise<{
88
110
  ok: boolean;
@@ -96,14 +118,8 @@ export declare class CustomPluginHost {
96
118
  * is replayed but only usage events whose own timestamp falls on today are
97
119
  * folded, so sessions created before midnight keep contributing the usage
98
120
  * they produced today. */
99
- usageScan(): Promise<{
100
- ok: true;
101
- usageToday: unknown;
102
- scannedSessions: number;
103
- } | {
104
- ok: false;
105
- error: string;
106
- }>;
121
+ usageScan(): Promise<UsageScanResult>;
122
+ private usageScanImpl;
107
123
  /** Resolve the Mermaid engine: the bundled `mermaid` dependency on disk
108
124
  * first (no runtime network), CDN mirrors only as a fallback (cached for
109
125
  * the host lifetime). */
@@ -140,3 +156,4 @@ export declare class CustomPluginHost {
140
156
  * tree (works for `link:` checkouts and registry installs alike); null when
141
157
  * the dependency is absent, which falls mermaidFetch back to the CDN. */
142
158
  export declare function defaultLocalMermaidPath(): string | null;
159
+ export {};
@@ -0,0 +1,37 @@
1
+ /**
2
+ * DeepSeek cost-estimation helpers shared by the host-facing usage model and
3
+ * the browser panel. Prices are CNY per 1M tokens at peak rate; off-peak is
4
+ * applied to the portion without a peak counter.
5
+ * @module @alexpeng/dsh-custom-plugin/pricing
6
+ */
7
+ import type { UsageRow } from './protocol.ts';
8
+ /** Official source used for the built-in estimate and shown in the panel. */
9
+ export declare const DEEPSEEK_PRICING_SOURCE_URL = "https://api-docs.deepseek.com/zh-cn/quick_start/pricing/";
10
+ /** Date on which the built-in rule was checked against the official page. */
11
+ export declare const DEEPSEEK_PRICING_CHECKED_ON = "2026-08-26";
12
+ export interface DeepSeekUnitPrice {
13
+ /** Uncached input / cache-miss input price. */
14
+ input: number;
15
+ /** Cache-hit input price. */
16
+ cacheHit: number;
17
+ /** Output price; reasoning is an output subdivision in DSH usage. */
18
+ output: number;
19
+ }
20
+ export interface UsageCostBreakdown {
21
+ peakTokens: number;
22
+ offPeakTokens: number;
23
+ peakCostCny: number;
24
+ offPeakCostCny: number;
25
+ totalCostCny: number;
26
+ }
27
+ /** Resolve a model's peak price, falling back to Flash for unknown/legacy rows. */
28
+ export declare function priceOf(model: string): DeepSeekUnitPrice;
29
+ /**
30
+ * Split one row into peak/off-peak token volume and CNY cost. Rows without
31
+ * peak counters are treated as
32
+ * entirely off-peak for compatibility with state written before peak
33
+ * tracking existed; a rescan can rebuild the row with exact split counters.
34
+ */
35
+ export declare function usageCostBreakdown(row: UsageRow, model: string): UsageCostBreakdown;
36
+ /** Estimate one row's total CNY cost. */
37
+ export declare function estimateUsageCostCny(row: UsageRow, model: string): number;
@@ -66,9 +66,9 @@ export interface UsageRow {
66
66
  cacheW: number;
67
67
  reason: number;
68
68
  calls: number;
69
- /** Peak-hour (Beijing 09-12 / 14-18) portion of each counter; the rest is
70
- * off-peak. Optional because rows persisted before the peak/off-peak split
71
- * predate these fields. */
69
+ /** Peak-hour (Beijing Monday-Friday 09-12 / 14-18) portion of each counter;
70
+ * the rest, including weekends, is off-peak. Optional because rows persisted
71
+ * before the peak/off-peak split predate these fields. */
72
72
  peakIn?: number;
73
73
  peakCacheIn?: number;
74
74
  peakCacheW?: number;
@@ -76,6 +76,8 @@ export interface UsageRow {
76
76
  }
77
77
  /** Day -> model -> counters. */
78
78
  export type UsageMap = Record<string, Record<string, UsageRow>>;
79
+ /** Where the balance API key was resolved from, without exposing its value. */
80
+ export type CredentialStorage = 'system' | 'legacy-state' | 'environment' | 'dsh' | 'none';
79
81
  /** The complete persisted state file shape. */
80
82
  export interface CustomPluginState {
81
83
  cfg: CustomPluginConfig;
@@ -86,6 +88,11 @@ export interface CustomPluginState {
86
88
  apiKey: string;
87
89
  usage: UsageMap;
88
90
  }
91
+ /** Browser-safe state projection; the API key itself never crosses this boundary. */
92
+ export type CustomPluginPublicState = Omit<CustomPluginState, 'apiKey'> & {
93
+ apiKeyConfigured: boolean;
94
+ credentialStorage: CredentialStorage;
95
+ };
89
96
  /** One timeline node: one direct user message in a session. */
90
97
  export interface TimelineItem {
91
98
  seq: number;
@@ -4,7 +4,9 @@
4
4
  * One JSON document at `$DSH_HOME/custom-plugin-state.json` holds the
5
5
  * appearance configuration, folder tree, prompt library, starred timeline
6
6
  * nodes, the balance API key, and the per-day token usage ledger. Writes are
7
- * atomic (temp file + rename) so a crash never truncates the document.
7
+ * atomic (temp file + rename) and serialized (one in-flight write per path),
8
+ * so a crash never truncates the document and concurrent saves never race
9
+ * on the temp file.
8
10
  * @module @alexpeng/dsh-custom-plugin/state
9
11
  */
10
12
  import { type CustomPluginConfig, type CustomPluginState } from './protocol.ts';
@@ -18,5 +20,7 @@ export declare function normalizeCfg(raw: unknown): CustomPluginConfig;
18
20
  export declare function mergeState(target: CustomPluginState, raw: unknown): void;
19
21
  /** Load and merge the persisted document into `state`. */
20
22
  export declare function loadStateFile(state: CustomPluginState, home?: string): Promise<string>;
21
- /** Atomically persist the state document. */
23
+ /** Atomically persist the state document. Saves to the same path are
24
+ * serialized: a caller awaits its own turn's completion, a failed turn never
25
+ * poisons the queue behind it. */
22
26
  export declare function saveStateFile(state: CustomPluginState, home?: string): Promise<void>;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Optional OS credential-store adapter.
3
+ *
4
+ * `keytar` is optional so the plugin still works in minimal/headless
5
+ * installations. When present, it delegates storage to the platform keyring
6
+ * (Windows Credential Manager, macOS Keychain, or the Linux Secret Service
7
+ * backend exposed by keytar). The caller owns the legacy state-file fallback.
8
+ * @module @alexpeng/dsh-custom-plugin/system-credentials
9
+ */
10
+ export declare const CREDENTIAL_SERVICE = "@alexpeng/dsh-custom-plugin";
11
+ export declare const CREDENTIAL_ACCOUNT = "deepseek-api-key";
12
+ export interface CredentialStore {
13
+ readonly available: boolean;
14
+ get(): Promise<string>;
15
+ set(value: string): Promise<boolean>;
16
+ clear(): Promise<boolean>;
17
+ }
18
+ /** Safe wrapper around the optional keytar dependency. */
19
+ export declare class SystemCredentialStore implements CredentialStore {
20
+ private readonly keytar;
21
+ get available(): boolean;
22
+ get(): Promise<string>;
23
+ set(value: string): Promise<boolean>;
24
+ clear(): Promise<boolean>;
25
+ }
@@ -3,7 +3,7 @@
3
3
  * @module @alexpeng/dsh-custom-plugin/usage
4
4
  */
5
5
  import type { SessionEvent } from '@deepseek-ai/dsh-session';
6
- import type { UsageRow } from './protocol.ts';
6
+ import type { UsageMap, UsageRow } from './protocol.ts';
7
7
  /** One adapter-reported token accounting record (counters are disjoint). */
8
8
  export interface UsageRecord {
9
9
  inputTokens?: number;
@@ -12,12 +12,16 @@ export interface UsageRecord {
12
12
  cacheWriteTokens?: number;
13
13
  reasoningTokens?: number;
14
14
  }
15
+ /** Number of Beijing calendar days retained in the local usage ledger. */
16
+ export declare const USAGE_RETENTION_DAYS = 90;
15
17
  /** Beijing-time (UTC+8) day key `YYYY-MM-DD`. */
16
18
  export declare function dayKey(time?: number): string;
17
- /** DeepSeek peak billing window: Beijing 09:00-12:00 / 14:00-18:00, off-peak is
18
- * the remainder at half price. Evaluated on China Standard Time so the split
19
- * matches the published schedule on any host timezone. */
19
+ /** DeepSeek peak billing window: Beijing Monday-Friday 09:00-12:00 /
20
+ * 14:00-18:00; all weekend hours are off-peak. Evaluated on China Standard
21
+ * Time so the split matches the published schedule on any host timezone. */
20
22
  export declare function isPeakHour(time: number): boolean;
23
+ /** Remove malformed or expired day buckets from the persisted usage ledger. */
24
+ export declare function pruneUsage(usage: UsageMap, now?: number, retentionDays?: number): number;
21
25
  /** Fold one usage record into a ledger row, tracking the peak-hour portion. */
22
26
  export declare function foldUsageRecord(row: UsageRow, usage: UsageRecord, peak: boolean): void;
23
27
  /** Merge one aggregated row into a target row (scan folds per-session rows). */
package/package.json CHANGED
@@ -1,88 +1,93 @@
1
- {
2
- "name": "@alexpeng/dsh-custom-plugin",
3
- "description": "Custom convenience suite for the dsh web GUI: background colors with weather FX, liquid glass, a per-user-message timeline rail with stars and branching, multi-level project folders, prompt library, conversation export (JSON / Markdown / PDF-with-images), Mermaid rendering, quote reply, DeepSeek balance and daily token usage; dual-face plugin mounted without dsh source changes.",
4
- "version": "0.1.2",
5
- "type": "module",
6
- "main": "lib/index.js",
7
- "types": "lib/types/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./lib/types/index.d.ts",
11
- "default": "./lib/index.js"
12
- },
13
- "./client": {
14
- "types": "./lib/types/client/index.d.ts",
15
- "default": "./lib/client.js"
16
- },
17
- "./package.json": "./package.json"
18
- },
19
- "dsh": {
20
- "bundle": {
21
- "patch": "./cordis.patch.yml"
22
- },
23
- "client": {
24
- "inject": [
25
- "@deepseek-ai/dsh-client-runtime"
26
- ],
27
- "platform": "web"
28
- }
29
- },
30
- "publishConfig": {
31
- "access": "public"
32
- },
33
- "repository": {
34
- "type": "git",
35
- "url": "git+https://github.com/AlexPeng07/dsh-custom-plugin.git"
36
- },
37
- "author": "AlexPeng <alexpeng07@outlook.com>",
38
- "bugs": "https://github.com/AlexPeng07/dsh-custom-plugin/issues",
39
- "homepage": "https://github.com/AlexPeng07/dsh-custom-plugin#readme",
40
- "engines": {
41
- "node": ">=22"
42
- },
43
- "scripts": {
44
- "build": "tsc -p tsconfig.build.json && tsdown",
45
- "bundle": "tsdown",
46
- "watch": "tsdown --watch",
47
- "typecheck": "tsc --noEmit",
48
- "test": "vitest run",
49
- "test:watch": "vitest",
50
- "prepare": "pnpm build",
51
- "prepublishOnly": "pnpm typecheck && pnpm test"
52
- },
53
- "peerDependencies": {
54
- "@deepseek-ai/cordis": "^4.0.1",
55
- "@deepseek-ai/dsh-tools": "^0.1.1-rc.1"
56
- },
57
- "dependencies": {
58
- "mermaid": "^11.6.0"
59
- },
60
- "devDependencies": {
61
- "@deepseek-ai/cordis": "^4.0.1",
62
- "@deepseek-ai/dsh-attachment": "^0.1.1-rc.1",
63
- "@deepseek-ai/dsh-client-runtime": "^0.1.1-rc.1",
64
- "@deepseek-ai/dsh-client-ui-slots": "^0.1.1-rc.1",
65
- "@deepseek-ai/dsh-client-ui-theme": "^0.1.1-rc.1",
66
- "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.1",
67
- "@deepseek-ai/dsh-llm": "^0.1.1-rc.1",
68
- "@deepseek-ai/dsh-session": "^0.1.1-rc.1",
69
- "@deepseek-ai/dsh-session-query": "^0.1.1-rc.1",
70
- "@deepseek-ai/dsh-tools": "^0.1.1-rc.1",
71
- "@types/node": "^22.20.0",
72
- "@types/react": "~18.3.1",
73
- "@types/react-dom": "^18.3.5",
74
- "react": "^18.3.1",
75
- "react-dom": "^18.3.1",
76
- "tsdown": "0.22.2",
77
- "typescript": "~5.7.2",
78
- "vitest": "^3.0.0"
79
- },
80
- "files": [
81
- "lib",
82
- "cordis.patch.yml",
83
- "README.md",
84
- "README.zh.md",
85
- "README.i18n.yaml"
86
- ],
87
- "license": "Apache-2.0"
88
- }
1
+ {
2
+ "name": "@alexpeng/dsh-custom-plugin",
3
+ "description": "Custom convenience suite for the dsh web GUI: background colors with weather FX, liquid glass, a per-user-message timeline rail with stars and branching, multi-level project folders, prompt library, conversation export (JSON / Markdown / PDF-with-images), Mermaid rendering, quote reply, DeepSeek balance and daily token usage; dual-face plugin mounted without dsh source changes.",
4
+ "version": "0.1.4",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "types": "lib/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/types/index.d.ts",
11
+ "default": "./lib/index.js"
12
+ },
13
+ "./client": {
14
+ "types": "./lib/types/client/index.d.ts",
15
+ "default": "./lib/client.js"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "dsh": {
20
+ "bundle": {
21
+ "patch": "./cordis.patch.yml"
22
+ },
23
+ "client": {
24
+ "inject": [
25
+ "@deepseek-ai/dsh-client-runtime"
26
+ ],
27
+ "platform": "web"
28
+ }
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/AlexPeng07/dsh-custom-plugin.git"
36
+ },
37
+ "author": "AlexPeng <alexpeng07@outlook.com>",
38
+ "bugs": "https://github.com/AlexPeng07/dsh-custom-plugin/issues",
39
+ "homepage": "https://github.com/AlexPeng07/dsh-custom-plugin#readme",
40
+ "engines": {
41
+ "node": ">=22"
42
+ },
43
+ "peerDependencies": {
44
+ "@deepseek-ai/cordis": "^4.0.1",
45
+ "@deepseek-ai/dsh-tools": "^0.1.1-rc.1"
46
+ },
47
+ "dependencies": {
48
+ "mermaid": "^11.6.0"
49
+ },
50
+ "devDependencies": {
51
+ "@deepseek-ai/cordis": "^4.0.1",
52
+ "@deepseek-ai/dsh-attachment": "^0.1.1-rc.1",
53
+ "@deepseek-ai/dsh-client-runtime": "^0.1.1-rc.1",
54
+ "@deepseek-ai/dsh-client-ui-slots": "^0.1.1-rc.1",
55
+ "@deepseek-ai/dsh-client-ui-theme": "^0.1.1-rc.1",
56
+ "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.1",
57
+ "@deepseek-ai/dsh-llm": "^0.1.1-rc.1",
58
+ "@deepseek-ai/dsh-session": "^0.1.1-rc.1",
59
+ "@deepseek-ai/dsh-session-query": "^0.1.1-rc.1",
60
+ "@deepseek-ai/dsh-tools": "^0.1.1-rc.1",
61
+ "@types/node": "^22.20.0",
62
+ "@types/react": "~18.3.1",
63
+ "@types/react-dom": "^18.3.5",
64
+ "puppeteer-core": "^25.8.0",
65
+ "react": "^18.3.1",
66
+ "react-dom": "^18.3.1",
67
+ "tsdown": "0.22.2",
68
+ "typescript": "~5.7.2",
69
+ "vitest": "^3.0.0"
70
+ },
71
+ "files": [
72
+ "lib",
73
+ "cordis.patch.yml",
74
+ "CHANGELOG.md",
75
+ "README.md",
76
+ "README.zh.md",
77
+ "README.i18n.yaml"
78
+ ],
79
+ "license": "Apache-2.0",
80
+ "optionalDependencies": {
81
+ "keytar": "7.9.0"
82
+ },
83
+ "scripts": {
84
+ "build": "tsc -p tsconfig.build.json && tsdown",
85
+ "bundle": "tsdown",
86
+ "watch": "tsdown --watch",
87
+ "typecheck": "tsc --noEmit",
88
+ "test": "vitest run",
89
+ "test:watch": "vitest",
90
+ "check:readme": "node scripts/check-readme-i18n.mjs",
91
+ "smoke": "node scripts/smoke.mjs"
92
+ }
93
+ }