dsh-task-time-cost 0.0.0-stage → 0.2.8-beta.2
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 +29 -0
- package/COMPATIBILITY.md +26 -0
- package/CONTRIBUTING.md +11 -0
- package/LICENSE +21 -0
- package/README.en.md +60 -0
- package/README.md +59 -2
- package/THIRD_PARTY_NOTICES.md +113 -0
- package/cordis.patch.yml +4 -0
- package/data/baselines.json +12 -0
- package/data/defaults.json +21 -0
- package/data/models-dev-prices.json +1 -0
- package/data/prices.json +7 -0
- package/data/sources.json +15 -0
- package/docs/public/DETAIL.md +1 -0
- package/docs/public/DETAIL.zh.md +1 -0
- package/docs/public/acceptance.md +26 -0
- package/docs/public/guide.en.md +33 -0
- package/docs/public/guide.zh.md +35 -0
- package/docs/public/preview.svg +1 -0
- package/docs/public/privacy.md +13 -0
- package/docs/public/publishing.md +9 -0
- package/icon.svg +1 -0
- package/lib/client.js +175 -0
- package/lib/index.js +22948 -0
- package/lib/index.js.map +7 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +122 -4
- package/scripts/import-public-prices.mjs +35 -0
- package/src/client/INTEGRATION.md +61 -0
- package/src/client/config.ts +24 -0
- package/src/client/details.tsx +126 -0
- package/src/client/format.ts +218 -0
- package/src/client/index.tsx +225 -0
- package/src/client/locale.ts +353 -0
- package/src/client/management.tsx +246 -0
- package/src/client/placement.ts +26 -0
- package/src/client/remote.ts +59 -0
- package/src/client/store.ts +210 -0
- package/src/client/styles.ts +94 -0
- package/src/client/use-panel-placement.ts +54 -0
- package/src/client-entry.ts +12 -0
- package/src/host/config-entry.ts +47 -0
- package/src/host/config.ts +85 -0
- package/src/host/engine.ts +580 -0
- package/src/host/facts.ts +170 -0
- package/src/host/forecast-cost.ts +16 -0
- package/src/host/index.ts +269 -0
- package/src/host/record-schema.ts +175 -0
- package/src/host/repository.ts +167 -0
- package/src/host/runtime.ts +99 -0
- package/src/shared/ai-plan.ts +45 -0
- package/src/shared/baseline-selection.ts +43 -0
- package/src/shared/baselines.ts +135 -0
- package/src/shared/call-size.ts +32 -0
- package/src/shared/config.ts +65 -0
- package/src/shared/forecast.ts +91 -0
- package/src/shared/index.ts +12 -0
- package/src/shared/ledger.ts +38 -0
- package/src/shared/money.ts +46 -0
- package/src/shared/performance.ts +216 -0
- package/src/shared/pricing.ts +293 -0
- package/src/shared/public-prices.ts +57 -0
- package/src/shared/remote-contract.ts +72 -0
- package/src/shared/retained-time.ts +14 -0
- package/src/shared/time.ts +207 -0
- package/src/shared/types.ts +66 -0
- package/src/shared/usage.ts +232 -0
- package/src/shared/wire.ts +134 -0
- package/src/shared/workload.ts +265 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { placeStatPanel } from './placement.ts';
|
|
3
|
+
|
|
4
|
+
type ElementRef = { readonly current: HTMLElement | null };
|
|
5
|
+
|
|
6
|
+
/** Measure only owned elements; viewport events schedule at most one layout frame. */
|
|
7
|
+
export function usePanelPlacement(open: boolean, triggerRef: ElementRef, panelRef: ElementRef, firstActionRef: ElementRef): void {
|
|
8
|
+
React.useLayoutEffect(() => {
|
|
9
|
+
const trigger = triggerRef.current;
|
|
10
|
+
const panel = panelRef.current;
|
|
11
|
+
if (!open || !trigger || !panel) return;
|
|
12
|
+
let frame = 0;
|
|
13
|
+
let disposed = false;
|
|
14
|
+
const write = (key: 'minWidth' | 'maxWidth' | 'maxHeight' | 'left' | 'top', value: number) => {
|
|
15
|
+
const pixels = `${value}px`;
|
|
16
|
+
if (panel.style[key] !== pixels) panel.style[key] = pixels;
|
|
17
|
+
};
|
|
18
|
+
const arrange = () => {
|
|
19
|
+
if (disposed || !trigger.isConnected || !panel.isConnected) return;
|
|
20
|
+
const ratio = panel.getBoundingClientRect().width / panel.offsetWidth;
|
|
21
|
+
const scale = Number.isFinite(ratio) && ratio > 0 ? ratio : 1;
|
|
22
|
+
const availableWidth = Math.max(0, window.innerWidth / scale - 24);
|
|
23
|
+
write('minWidth', Math.min(300, availableWidth));
|
|
24
|
+
write('maxWidth', Math.min(440, availableWidth));
|
|
25
|
+
const overlay = Number.parseFloat(getComputedStyle(panel).getPropertyValue('--dsh-frame-overlay-top'));
|
|
26
|
+
const safeTop = Math.max(12, Number.isFinite(overlay) ? overlay : 12) * scale;
|
|
27
|
+
const bounds = panel.getBoundingClientRect();
|
|
28
|
+
const border = panel.offsetHeight - panel.clientHeight;
|
|
29
|
+
const placed = placeStatPanel(trigger.getBoundingClientRect(),
|
|
30
|
+
{ width: bounds.width, height: (panel.scrollHeight + border) * scale },
|
|
31
|
+
{ width: window.innerWidth, height: window.innerHeight, topInset: safeTop, inset: 12 * scale, gap: 8 * scale });
|
|
32
|
+
write('maxHeight', placed.maxHeight / scale);
|
|
33
|
+
write('left', placed.left / scale);
|
|
34
|
+
write('top', placed.top / scale);
|
|
35
|
+
};
|
|
36
|
+
const schedule = () => {
|
|
37
|
+
if (!disposed && !frame) frame = requestAnimationFrame(() => { frame = 0; arrange(); });
|
|
38
|
+
};
|
|
39
|
+
arrange();
|
|
40
|
+
firstActionRef.current?.focus({ preventScroll: true });
|
|
41
|
+
const observer = new ResizeObserver(schedule);
|
|
42
|
+
observer.observe(panel);
|
|
43
|
+
observer.observe(trigger);
|
|
44
|
+
window.addEventListener('resize', schedule);
|
|
45
|
+
document.addEventListener('scroll', schedule, true);
|
|
46
|
+
return () => {
|
|
47
|
+
disposed = true;
|
|
48
|
+
observer.disconnect();
|
|
49
|
+
window.removeEventListener('resize', schedule);
|
|
50
|
+
document.removeEventListener('scroll', schedule, true);
|
|
51
|
+
if (frame) cancelAnimationFrame(frame);
|
|
52
|
+
};
|
|
53
|
+
}, [open, triggerRef, panelRef, firstActionRef]);
|
|
54
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { TypertClientRemote } from '@deepseek-ai/dsh-typert-protocol';
|
|
3
|
+
import { remoteContribution } from './shared/remote-contract.ts';
|
|
4
|
+
import { applyClient } from './client/index.tsx';
|
|
5
|
+
export const inject = ['slots', 'locale', 'remote'];
|
|
6
|
+
export async function apply(ctx: Context): Promise<void> {
|
|
7
|
+
const remote = ctx.get('remote') as TypertClientRemote | undefined;
|
|
8
|
+
if (!remote) throw new Error('Estimator requires the DSH Remote service');
|
|
9
|
+
// $mount owns its disposer in the caller's fiber and resolves after the namespace is ready.
|
|
10
|
+
await remote.$mount(remoteContribution);
|
|
11
|
+
applyClient(ctx);
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { PACKAGE_NAME } from '../shared/wire.ts';
|
|
2
|
+
|
|
3
|
+
/** Minimal Loader entry surface needed to persist plugin configuration. */
|
|
4
|
+
export interface ConfigEntryLike {
|
|
5
|
+
options: { id: string; name?: string; config?: unknown };
|
|
6
|
+
fiber?: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Minimal ConfigEditor surface used by the estimator management action. */
|
|
10
|
+
export interface ConfigEditorLike {
|
|
11
|
+
entries(): ConfigEntryLike[];
|
|
12
|
+
edit(entry: unknown, change: (current: Record<string, unknown>, inherited: Record<string, unknown>) => Record<string, unknown>): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A configuration save that must reach the user as a failure, not a success. */
|
|
16
|
+
export class ConfigSaveError extends Error {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The entry that owns this package. The ambient fiber entry is only a hint: it can
|
|
20
|
+
* address a different plugin, so identity is always re-established by package name.
|
|
21
|
+
*/
|
|
22
|
+
export function ownerEntry(editor: ConfigEditorLike, ambient?: ConfigEntryLike | null): ConfigEntryLike {
|
|
23
|
+
const owned = [ambient, ...editor.entries()].filter((entry): entry is ConfigEntryLike => !!entry && entry.options?.name === PACKAGE_NAME);
|
|
24
|
+
const active = owned.find(entry => entry.fiber !== undefined) ?? owned[0];
|
|
25
|
+
if (!active) throw new ConfigSaveError('The estimator Loader entry is unavailable, so configuration was not saved.');
|
|
26
|
+
return active;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Persist the full config through DSH and require the active entry to adopt exactly
|
|
31
|
+
* that config. A file write that the running entry did not adopt is reported as a failure.
|
|
32
|
+
*/
|
|
33
|
+
export async function persistConfig(
|
|
34
|
+
editor: ConfigEditorLike,
|
|
35
|
+
ambient: ConfigEntryLike | null | undefined,
|
|
36
|
+
config: Record<string, unknown>,
|
|
37
|
+
validate: (value: unknown) => Record<string, unknown>,
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
const owner = ownerEntry(editor, ambient);
|
|
40
|
+
await editor.edit(owner, () => ({ ...config }));
|
|
41
|
+
const active = editor.entries().find(entry => entry.options?.name === PACKAGE_NAME);
|
|
42
|
+
let applied: Record<string, unknown> | undefined;
|
|
43
|
+
try { applied = validate(active?.options?.config); } catch { applied = undefined; }
|
|
44
|
+
if (!applied || JSON.stringify(applied) !== JSON.stringify(config)) {
|
|
45
|
+
throw new ConfigSaveError('The configuration file was written, but the active entry did not adopt it. Reopen DSH to apply the saved file.');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import Schema from '@deepseek-ai/schemastery';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { DEFAULT_CONFIG, validateConfig } from '../shared/config.ts';
|
|
4
|
+
import type { EstimatorConfig } from '../shared/config.ts';
|
|
5
|
+
import { validatePrices } from '../shared/pricing.ts';
|
|
6
|
+
import type { PriceEntry, SameAs } from '../shared/pricing.ts';
|
|
7
|
+
import type { Span } from '../shared/types.ts';
|
|
8
|
+
import { DEFAULT_CLASSIFICATION_RULES, DEFAULT_TEMPLATES, validateTemplates } from '../shared/workload.ts';
|
|
9
|
+
import type { ClassificationRule, WorkloadTemplate } from '../shared/workload.ts';
|
|
10
|
+
|
|
11
|
+
export interface RouteOverride {
|
|
12
|
+
/** A user-chosen opaque endpoint identity; never a credential or URL. */
|
|
13
|
+
endpointFingerprint?: string;
|
|
14
|
+
serviceTier: string;
|
|
15
|
+
outputIncludesReasoning: boolean | 'unknown';
|
|
16
|
+
cacheFieldsOmittedAreZero: boolean;
|
|
17
|
+
cacheWriteTtl: '5m' | '1h' | null;
|
|
18
|
+
forecastCacheReadRatio: Span | null;
|
|
19
|
+
forecastCacheWrite5mTokens: Span | null;
|
|
20
|
+
forecastCacheWrite1hTokens: Span | null;
|
|
21
|
+
forecastReasoningTokens: Span | null;
|
|
22
|
+
}
|
|
23
|
+
export interface PluginConfig extends EstimatorConfig {
|
|
24
|
+
prices: PriceEntry[];
|
|
25
|
+
usePublicPriceCatalog: boolean;
|
|
26
|
+
sameAs: SameAs[];
|
|
27
|
+
routeOverrides: Record<string, RouteOverride>;
|
|
28
|
+
workloadTemplates: WorkloadTemplate[];
|
|
29
|
+
classificationRules: ClassificationRule[];
|
|
30
|
+
}
|
|
31
|
+
const rangeSchema = z.object({ low: z.number().finite().nonnegative(), high: z.number().finite().nonnegative() })
|
|
32
|
+
.strict().refine(value => value.low <= value.high);
|
|
33
|
+
const routeSchema = z.object({
|
|
34
|
+
endpointFingerprint: z.string().min(1).max(128).regex(/^[\p{L}\p{N}_.:-]+$/u).optional(),
|
|
35
|
+
serviceTier: z.string().min(1).max(80).default('unknown'),
|
|
36
|
+
outputIncludesReasoning: z.union([z.boolean(), z.literal('unknown')]).default('unknown'),
|
|
37
|
+
cacheFieldsOmittedAreZero: z.boolean().default(false),
|
|
38
|
+
cacheWriteTtl: z.enum(['5m', '1h']).nullable().default(null),
|
|
39
|
+
forecastCacheReadRatio: rangeSchema.nullable().default(null),
|
|
40
|
+
forecastCacheWrite5mTokens: rangeSchema.nullable().default(null),
|
|
41
|
+
forecastCacheWrite1hTokens: rangeSchema.nullable().default(null),
|
|
42
|
+
forecastReasoningTokens: rangeSchema.nullable().default(null),
|
|
43
|
+
}).strict();
|
|
44
|
+
|
|
45
|
+
export function validatePluginConfig(raw: unknown): PluginConfig {
|
|
46
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new TypeError('Configuration must be an object');
|
|
47
|
+
const input = raw as Record<string, unknown>;
|
|
48
|
+
const { prices = [], usePublicPriceCatalog = true, sameAs = [], routeOverrides = {}, workloadTemplates = [...DEFAULT_TEMPLATES], classificationRules = [...DEFAULT_CLASSIFICATION_RULES], ...engine } = input;
|
|
49
|
+
if (typeof usePublicPriceCatalog !== 'boolean') throw new TypeError('usePublicPriceCatalog must be a boolean');
|
|
50
|
+
const validated = validateConfig(engine as Partial<EstimatorConfig>);
|
|
51
|
+
if (validated.saveRawContent || validated.magpieIntegration) throw new TypeError('This release supports local DSH statistics only; saveRawContent and magpieIntegration must stay false');
|
|
52
|
+
if (validated.predictionRefreshMs < 1000 || validated.predictionRefreshMs > 60000 || validated.timerRefreshMs < 250 || validated.timerRefreshMs > 60000) throw new TypeError('Refresh intervals must stay within supported bounds');
|
|
53
|
+
if (validated.maxSamplesPerBucket > 2000 || validated.historyRetentionDays > 3650) throw new TypeError('Retention limits exceed the supported local data size');
|
|
54
|
+
if (!Array.isArray(prices) || !Array.isArray(sameAs)) throw new TypeError('prices and sameAs must be arrays');
|
|
55
|
+
validatePrices(prices as PriceEntry[], sameAs as SameAs[]);
|
|
56
|
+
const routes = z.record(z.string().min(1).max(256), routeSchema).parse(routeOverrides);
|
|
57
|
+
for (const route of Object.values(routes)) if (route.forecastCacheReadRatio && route.forecastCacheReadRatio.high > 1) throw new TypeError('Cache ratio must be between 0 and 1');
|
|
58
|
+
if (!Array.isArray(workloadTemplates) || !Array.isArray(classificationRules)) throw new TypeError('Templates and classification rules must be arrays');
|
|
59
|
+
validateTemplates(workloadTemplates as WorkloadTemplate[]);
|
|
60
|
+
const ruleIds = new Set<string>();
|
|
61
|
+
for (const rule of classificationRules as ClassificationRule[]) {
|
|
62
|
+
if (!rule.id || ruleIds.has(rule.id) || !DEFAULT_TEMPLATES.some(template => template.kind === rule.kind) || !Array.isArray(rule.patterns) || !rule.patterns.length) throw new TypeError('Invalid classification rule');
|
|
63
|
+
ruleIds.add(rule.id);
|
|
64
|
+
for (const pattern of [...rule.patterns, ...(rule.excludePatterns ?? [])]) { if (typeof pattern !== 'string' || pattern.length > 1000) throw new TypeError('Invalid classification pattern'); new RegExp(pattern, 'iu'); }
|
|
65
|
+
}
|
|
66
|
+
return { ...validated, prices: structuredClone(prices) as PriceEntry[], usePublicPriceCatalog, sameAs: structuredClone(sameAs) as SameAs[], routeOverrides: routes,
|
|
67
|
+
workloadTemplates: structuredClone(workloadTemplates) as WorkloadTemplate[], classificationRules: structuredClone(classificationRules) as ClassificationRule[] };
|
|
68
|
+
}
|
|
69
|
+
export function defaultPluginConfig(): PluginConfig {
|
|
70
|
+
return validatePluginConfig({ ...DEFAULT_CONFIG, prices: [], sameAs: [], routeOverrides: {} });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const configFields: Record<string, Schema> = {};
|
|
74
|
+
for (const [key, value] of Object.entries(DEFAULT_CONFIG)) {
|
|
75
|
+
if (typeof value === 'number') configFields[key] = Schema.number().min(0).default(value);
|
|
76
|
+
else if (typeof value === 'boolean') configFields[key] = Schema.boolean().default(value);
|
|
77
|
+
else configFields[key] = Schema.object({ low: Schema.number().min(0), high: Schema.number().min(0) }).default(value);
|
|
78
|
+
}
|
|
79
|
+
configFields.usePublicPriceCatalog = Schema.boolean().default(true).description('Automatically match the bundled models.dev list-price reference; never a provider invoice.');
|
|
80
|
+
configFields.prices = Schema.array(Schema.any()).default([]).description('Per-request prices with currency, provenance and version. Unknown prices remain unpriced.');
|
|
81
|
+
configFields.sameAs = Schema.array(Schema.any()).default([]).description('Explicit price aliases; cycles are refused.');
|
|
82
|
+
configFields.routeOverrides = Schema.dict(Schema.any()).default({}).description('Opaque endpoint identities, service tiers and adapter-specific usage semantics.');
|
|
83
|
+
configFields.workloadTemplates = Schema.array(Schema.any()).default([...DEFAULT_TEMPLATES]).description('Versioned workload assumptions for each task type.');
|
|
84
|
+
configFields.classificationRules = Schema.array(Schema.any()).default([...DEFAULT_CLASSIFICATION_RULES]).description('Ordered local task classification rules.');
|
|
85
|
+
export const Config = Schema.object(configFields).description('Local task time and cost estimates. No extra model calls or remote data uploads.');
|