null
feat: deploy BotPsy local WebGPU static Space
1cc8d34
Raw History Blame Contribute Delete
3.49 kB
import type {
CheckpointId,
PrefixHash,
ProfileId,
ProposalId,
RequestId,
SessionEpoch,
} from './ids.ts';
export interface EngineIdentity {
modelHash: string;
tokenizerHash: string;
templateHash: string;
numericalProfile: ProfileId;
stateSchema: number;
}
export interface PrefixState {
processedLength: number;
prefixHash: PrefixHash;
pendingToken?: number;
samplerPhase: 'beforeDraw' | 'afterDraw';
}
export interface CheckpointRef {
id: CheckpointId;
identity: EngineIdentity;
prefix: PrefixState;
canResampleNext: boolean;
}
export interface GenerateRequest {
epoch: SessionEpoch;
requestId: RequestId;
samplerProfile: ProfileId;
seed: bigint;
maxNewTokens: number;
stopPolicy: string;
}
export type StopReason = 'eos' | 'length' | 'cancelled' | 'tool' | 'grammar' | 'fault' | 'window';
export type EngineEvent =
| { type: 'preparation'; requestId: RequestId; stage: PrepareStage; completed: number; total: number }
| { type: 'prefill'; requestId: RequestId; processed: number; total: number }
| { type: 'token'; requestId: RequestId; tokenId: number; index: number }
| {
type: 'channel';
requestId: RequestId;
channel: 'reasoning' | 'answer';
text: string;
tokenStart: number;
tokenEnd: number;
}
| { type: 'toolProposal'; requestId: RequestId; proposal: ToolProposal }
| { type: 'completion'; requestId: RequestId; reason: StopReason; processed: number; emitted: number }
| { type: 'cancelled'; requestId: RequestId; boundary: PrefixState }
| { type: 'error'; requestId: RequestId | null; recoverable: boolean; code: string; message: string }
| { type: 'checkpoint'; requestId: RequestId; checkpoint: CheckpointRef };
export type PrepareStage =
| 'manifest'
| 'download'
| 'verify'
| 'upload'
| 'compile'
| 'calibrate'
| 'ready';
export interface ToolCall {
name: string;
arguments: Record<string, unknown>;
}
export interface ToolProposal {
proposalId: ProposalId;
call: ToolCall;
rawText: string;
complete: boolean;
parseError?: string;
}
export interface InferenceEngine {
load(manifestUrl: string): Promise<EngineIdentity>;
prefill(tokens: Uint32Array): Promise<PrefixState>;
generate(req: GenerateRequest): AsyncIterable<EngineEvent>;
checkpoint(): Promise<CheckpointRef>;
restore(ref: CheckpointRef): Promise<PrefixState>;
cancel(requestId: number): Promise<PrefixState>;
dispose(): Promise<void>;
}
export interface DeviceCapabilities {
webgpu: boolean;
secureContext: boolean;
crossOriginIsolated: boolean;
adapter: AdapterReport | null;
features: string[];
wgslLanguageFeatures: string[];
limits: Record<string, number>;
timestampQuery: boolean;
subgroups: boolean;
subgroupSizeControl: boolean;
immediateAddressSpace: boolean;
bufferView: boolean;
subgroupUniformity: boolean;
subgroupId: boolean;
shaderF16: boolean;
opfs: boolean;
indexedDb: boolean;
sharedArrayBuffer: boolean;
webLocks: boolean;
broadcastChannel: boolean;
serviceWorker: boolean;
screenWakeLock: boolean;
deviceMemoryGiB: number | null;
storageQuotaBytes: number | null;
storageUsageBytes: number | null;
}
export interface AdapterReport {
vendor: string;
architecture: string;
device: string;
description: string;
subgroupMinSize: number;
subgroupMaxSize: number;
isFallbackAdapter: boolean;
}
export interface CapabilityFailure {
missing: string[];
detail: string;
}