@alexpeng/dsh-custom-plugin 0.1.3 → 0.1.5
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/CHANGELOG.md +27 -0
- package/README.i18n.yaml +2 -2
- package/README.md +9 -6
- package/README.zh.md +9 -6
- package/lib/client.js +157 -57
- package/lib/client.js.map +1 -1
- package/lib/index.js +338 -121
- package/lib/types/client/api.d.ts +5 -2
- package/lib/types/host-service.d.ts +29 -13
- package/lib/types/pricing.d.ts +38 -0
- package/lib/types/protocol.d.ts +14 -3
- package/lib/types/system-credentials.d.ts +25 -0
- package/lib/types/usage.d.ts +12 -4
- package/package.json +16 -15
|
@@ -6,8 +6,18 @@
|
|
|
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
|
|
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
|
+
day: string;
|
|
15
|
+
usageToday: unknown;
|
|
16
|
+
scannedSessions: number;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
11
21
|
/** Options the loader entry supplies to the host service. */
|
|
12
22
|
export interface CustomPluginHostOptions {
|
|
13
23
|
sessionQuery: SessionQueryEngine;
|
|
@@ -27,6 +37,8 @@ export interface CustomPluginHostOptions {
|
|
|
27
37
|
localMermaidPath?: () => string | null;
|
|
28
38
|
/** Test seam: read the DeepSeek credential from DSH's credentials file. */
|
|
29
39
|
readCredential?: () => Promise<string>;
|
|
40
|
+
/** Test seam and optional platform adapter for OS credential storage. */
|
|
41
|
+
credentialStore?: CredentialStore;
|
|
30
42
|
}
|
|
31
43
|
/** Host capabilities behind the routes and the agent tool. */
|
|
32
44
|
export declare class CustomPluginHost {
|
|
@@ -36,7 +48,11 @@ export declare class CustomPluginHost {
|
|
|
36
48
|
private readonly persistNow;
|
|
37
49
|
private readonly diagReports;
|
|
38
50
|
private readonly attachments;
|
|
51
|
+
private readonly credentialStore;
|
|
39
52
|
private readonly sessionModel;
|
|
53
|
+
private usageScanPromise;
|
|
54
|
+
private usageRevision;
|
|
55
|
+
private lastUsagePruneDay;
|
|
40
56
|
private mermaidBytesValue;
|
|
41
57
|
private mermaidJs;
|
|
42
58
|
private mermaidSource;
|
|
@@ -53,8 +69,8 @@ export declare class CustomPluginHost {
|
|
|
53
69
|
/** Fold one assistant/message usage record into the ledger. The event time
|
|
54
70
|
* decides both the day bucket and the peak/off-peak portion. */
|
|
55
71
|
foldUsage(sessionId: string, usage: UsageRecord, time?: number): void;
|
|
56
|
-
/** Snapshot of the
|
|
57
|
-
stateView():
|
|
72
|
+
/** Snapshot of browser-safe state; the API key value never crosses this boundary. */
|
|
73
|
+
stateView(): Promise<CustomPluginPublicState>;
|
|
58
74
|
/** Apply a browser-side state edit (config, folders, prompts, stars, api key). */
|
|
59
75
|
applyEdit(edit: {
|
|
60
76
|
cfg?: Partial<CustomPluginState['cfg']>;
|
|
@@ -62,7 +78,9 @@ export declare class CustomPluginHost {
|
|
|
62
78
|
prompts?: CustomPluginState['prompts'];
|
|
63
79
|
stars?: CustomPluginState['stars'];
|
|
64
80
|
apiKey?: string;
|
|
65
|
-
}): void
|
|
81
|
+
}): Promise<void>;
|
|
82
|
+
/** Move a legacy plaintext state key into the OS store when available. */
|
|
83
|
+
migrateLegacyApiKey(): Promise<boolean>;
|
|
66
84
|
/** Timeline nodes for one session, capped to the 400-node tail. The tail cap
|
|
67
85
|
* bounds the payload (each item carries up to 4k of preview text); 400 keeps
|
|
68
86
|
* dots available when the GUI deep-loads history. */
|
|
@@ -84,8 +102,11 @@ export declare class CustomPluginHost {
|
|
|
84
102
|
ok: false;
|
|
85
103
|
error: string;
|
|
86
104
|
}>;
|
|
87
|
-
/** Resolve the
|
|
105
|
+
/** Resolve the key and its source without returning either value to the browser. */
|
|
106
|
+
private resolveApiKeyWithSource;
|
|
88
107
|
private resolveApiKey;
|
|
108
|
+
/** Browser-safe credential status used by state and save responses. */
|
|
109
|
+
credentialStatus(): Promise<Pick<CustomPluginPublicState, 'apiKeyConfigured' | 'credentialStorage'>>;
|
|
89
110
|
/** Query the DeepSeek balance endpoint (15s timeout). */
|
|
90
111
|
balanceGet(): Promise<{
|
|
91
112
|
ok: boolean;
|
|
@@ -99,14 +120,8 @@ export declare class CustomPluginHost {
|
|
|
99
120
|
* is replayed but only usage events whose own timestamp falls on today are
|
|
100
121
|
* folded, so sessions created before midnight keep contributing the usage
|
|
101
122
|
* they produced today. */
|
|
102
|
-
usageScan(): Promise<
|
|
103
|
-
|
|
104
|
-
usageToday: unknown;
|
|
105
|
-
scannedSessions: number;
|
|
106
|
-
} | {
|
|
107
|
-
ok: false;
|
|
108
|
-
error: string;
|
|
109
|
-
}>;
|
|
123
|
+
usageScan(): Promise<UsageScanResult>;
|
|
124
|
+
private usageScanImpl;
|
|
110
125
|
/** Resolve the Mermaid engine: the bundled `mermaid` dependency on disk
|
|
111
126
|
* first (no runtime network), CDN mirrors only as a fallback (cached for
|
|
112
127
|
* the host lifetime). */
|
|
@@ -143,3 +158,4 @@ export declare class CustomPluginHost {
|
|
|
143
158
|
* tree (works for `link:` checkouts and registry installs alike); null when
|
|
144
159
|
* the dependency is absent, which falls mermaidFetch back to the CDN. */
|
|
145
160
|
export declare function defaultLocalMermaidPath(): string | null;
|
|
161
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
/** False for pre-split rows whose tariff allocation is not recoverable. */
|
|
22
|
+
exact: boolean;
|
|
23
|
+
peakTokens: number;
|
|
24
|
+
offPeakTokens: number;
|
|
25
|
+
peakCostCny: number;
|
|
26
|
+
offPeakCostCny: number;
|
|
27
|
+
totalCostCny: number;
|
|
28
|
+
}
|
|
29
|
+
/** Resolve a model's peak price, falling back to Flash for unknown/legacy rows. */
|
|
30
|
+
export declare function priceOf(model: string): DeepSeekUnitPrice;
|
|
31
|
+
/**
|
|
32
|
+
* Split one row into peak/off-peak token volume and CNY cost. Rows without
|
|
33
|
+
* peak counters retain the old all-off-peak numeric fallback for compatibility,
|
|
34
|
+
* but `exact` is false until a rescan rebuilds the row with split counters.
|
|
35
|
+
*/
|
|
36
|
+
export declare function usageCostBreakdown(row: UsageRow, model: string): UsageCostBreakdown;
|
|
37
|
+
/** Estimate one row's total CNY cost. */
|
|
38
|
+
export declare function estimateUsageCostCny(row: UsageRow, model: string): number;
|
package/lib/types/protocol.d.ts
CHANGED
|
@@ -66,16 +66,22 @@ 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;
|
|
70
|
-
* off-peak. Optional because rows persisted
|
|
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;
|
|
75
75
|
peakOut?: number;
|
|
76
|
+
/** True when the complete row was rebuilt or recorded with an exact
|
|
77
|
+
* peak/off-peak split. Omitted/false marks legacy rows whose historical
|
|
78
|
+
* portion cannot be assigned to either tariff from persisted counters. */
|
|
79
|
+
peakSplitKnown?: boolean;
|
|
76
80
|
}
|
|
77
81
|
/** Day -> model -> counters. */
|
|
78
82
|
export type UsageMap = Record<string, Record<string, UsageRow>>;
|
|
83
|
+
/** Where the balance API key was resolved from, without exposing its value. */
|
|
84
|
+
export type CredentialStorage = 'system' | 'legacy-state' | 'environment' | 'dsh' | 'none';
|
|
79
85
|
/** The complete persisted state file shape. */
|
|
80
86
|
export interface CustomPluginState {
|
|
81
87
|
cfg: CustomPluginConfig;
|
|
@@ -86,6 +92,11 @@ export interface CustomPluginState {
|
|
|
86
92
|
apiKey: string;
|
|
87
93
|
usage: UsageMap;
|
|
88
94
|
}
|
|
95
|
+
/** Browser-safe state projection; the API key itself never crosses this boundary. */
|
|
96
|
+
export type CustomPluginPublicState = Omit<CustomPluginState, 'apiKey'> & {
|
|
97
|
+
apiKeyConfigured: boolean;
|
|
98
|
+
credentialStorage: CredentialStorage;
|
|
99
|
+
};
|
|
89
100
|
/** One timeline node: one direct user message in a session. */
|
|
90
101
|
export interface TimelineItem {
|
|
91
102
|
seq: number;
|
|
@@ -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
|
+
}
|
package/lib/types/usage.d.ts
CHANGED
|
@@ -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,20 @@ 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;
|
|
17
|
+
/** Create a row whose peak/off-peak split is exact from its first event. */
|
|
18
|
+
export declare function createUsageRow(): UsageRow;
|
|
19
|
+
/** Whether a row has enough persisted information for exact tariff math. */
|
|
20
|
+
export declare function hasKnownPeakSplit(row: UsageRow): boolean;
|
|
15
21
|
/** Beijing-time (UTC+8) day key `YYYY-MM-DD`. */
|
|
16
22
|
export declare function dayKey(time?: number): string;
|
|
17
|
-
/** DeepSeek peak billing window: Beijing 09:00-12:00 /
|
|
18
|
-
*
|
|
19
|
-
* matches the published schedule on any host timezone. */
|
|
23
|
+
/** DeepSeek peak billing window: Beijing Monday-Friday 09:00-12:00 /
|
|
24
|
+
* 14:00-18:00; all weekend hours are off-peak. Evaluated on China Standard
|
|
25
|
+
* Time so the split matches the published schedule on any host timezone. */
|
|
20
26
|
export declare function isPeakHour(time: number): boolean;
|
|
27
|
+
/** Remove malformed or expired day buckets from the persisted usage ledger. */
|
|
28
|
+
export declare function pruneUsage(usage: UsageMap, now?: number, retentionDays?: number): number;
|
|
21
29
|
/** Fold one usage record into a ledger row, tracking the peak-hour portion. */
|
|
22
30
|
export declare function foldUsageRecord(row: UsageRow, usage: UsageRecord, peak: boolean): void;
|
|
23
31
|
/** Merge one aggregated row into a target row (scan folds per-session rows). */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexpeng/dsh-custom-plugin",
|
|
3
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
|
+
"version": "0.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/types/index.d.ts",
|
|
@@ -40,18 +40,6 @@
|
|
|
40
40
|
"engines": {
|
|
41
41
|
"node": ">=22"
|
|
42
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
|
-
"check:readme": "node scripts/check-readme-i18n.mjs",
|
|
51
|
-
"smoke": "node scripts/smoke.mjs",
|
|
52
|
-
"prepare": "pnpm build",
|
|
53
|
-
"prepublishOnly": "pnpm typecheck && pnpm test"
|
|
54
|
-
},
|
|
55
43
|
"peerDependencies": {
|
|
56
44
|
"@deepseek-ai/cordis": "^4.0.1",
|
|
57
45
|
"@deepseek-ai/dsh-tools": "^0.1.1-rc.1"
|
|
@@ -88,5 +76,18 @@
|
|
|
88
76
|
"README.zh.md",
|
|
89
77
|
"README.i18n.yaml"
|
|
90
78
|
],
|
|
91
|
-
"license": "Apache-2.0"
|
|
92
|
-
|
|
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
|
+
}
|