Files
wonder 922e1c3be2 Initial commit: pi-lens session observability for pi
- collector, classify, prompt-parser, server (TypeScript)
- web panel (app.js, index.html, style.css)
- smoke and load-test scripts
- DESIGN.md and README.md
2026-10-04 19:54:34 +08:00

194 lines
4.8 KiB
TypeScript

/**
* pi-lens data model — shared between collector and server.
* Pure TypeScript, no pi imports (keeps this module testable standalone).
*/
export type ContextKind =
| "preamble"
| "tool-rules"
| "docs"
| "context-file"
| "skill-list"
| "skill-loaded"
| "user-append"
| "custom-section"
| "forced"
| "compaction"
| "user"
| "assistant"
| "tool"
| "other";
export interface PromptSection {
/** Section name: "preamble", "rules", "project_context:AGENTS.md", "skills:pdf-tools", ... */
name: string;
kind: ContextKind;
/** Origin label shown in the UI (file path, tool name, "pi", ...). */
source: string;
chars: number;
tokensEstimate: number;
/** Short first-line preview for list rendering. */
preview: string;
/** Id for /api/content/:id — full text is fetched on demand only. */
contentId: string;
/** Diff against the previous PromptBuild. */
status?: "added" | "changed" | "removed" | "same";
}
export interface PromptBuild {
id: string;
turn: number;
at: number;
trigger: string;
forced: boolean;
totalChars: number;
totalTokensEstimate: number;
sections: PromptSection[];
selectedTools: string[];
skillCount: number;
contextFileCount: number;
}
export type SkillStepKind = "trigger" | "load" | "tool-call" | "reference" | "output";
export interface SkillStep {
kind: SkillStepKind;
at: number;
label: string;
detail?: Record<string, unknown>;
toolCallId?: string;
status?: "running" | "ok" | "error";
chars?: number;
/** Full step content (e.g. the injected skill body) — fetched on demand only. */
contentId?: string;
/** Nested tool calls hang under their parent. */
parentToolCallId?: string;
}
export type SkillSpanStatus = "open" | "loaded" | "done" | "unloaded" | "error";
export interface SkillSpan {
id: string;
skill: string;
location: string;
trigger: "explicit" | "model";
triggerBy: string;
args?: string;
startedAt: number;
loadedAt?: number;
endedAt?: number;
status: SkillSpanStatus;
steps: SkillStep[];
injectedChars: number;
}
export interface SkillMeta {
name: string;
description: string;
filePath: string;
baseDir: string;
source: string;
disableModelInvocation: boolean;
invokedCount: number;
lastInvokedAt?: number;
injectedChars: number;
}
export interface TraceEvent {
seq: number;
t: number;
/** Session this event belongs to — the client shows only the current session. */
session?: string;
/** Event type string: input, prompt_build, tool_call, tool_result, turn_start, ... */
type: string;
/** Color class in the timeline. */
kind: ContextKind;
/** One-line label. */
label: string;
detail?: Record<string, unknown>;
contentId?: string;
toolCallId?: string;
spanId?: string;
turn?: number;
}
export interface StateSnapshot {
startedAt: number;
updatedAt: number;
sessionId?: string;
sessionFile?: string;
sessionName?: string;
cwd?: string;
mode?: string;
hasUI?: boolean;
model?: { id?: string; provider?: string; name?: string };
thinkingLevel?: string;
contextUsage?: { tokens: number | null; contextWindow: number; percent: number | null };
entryCount?: number;
branchCount?: number;
activeTools?: string[];
tools?: Array<{ name: string; exposure?: string; namespace?: string }>;
mcpServers?: Array<{ name: string }>;
skills?: SkillMeta[];
totals: {
turns: number;
toolCalls: number;
toolErrors: number;
promptBuilds: number;
skillInvocations: number;
compactions: number;
inputChars: number;
outputChars: number;
errors: number;
};
}
export interface ToolCallLike {
toolCallId: string;
parentToolCallId?: string;
toolName: string;
input: Record<string, unknown>;
}
export interface ToolResultLike {
toolCallId: string;
parentToolCallId?: string;
toolName: string;
isError: boolean;
content?: unknown[];
usage?: unknown;
}
/** Structural view of pi's NormalizedBuildSystemPromptOptions (see pi dist/core/system-prompt.d.ts). */
export interface SkillInfo {
name: string;
description: string;
filePath: string;
baseDir: string;
disableModelInvocation: boolean;
sourceInfo?: { source?: string };
}
export interface PromptSourceOptions {
customPrompt?: string;
forceSystemPrompt?: string;
selectedTools: string[];
toolSnippets: Record<string, string>;
toolGuidelines: Record<string, string[]>;
promptGuidelines: string[];
appendSystemPrompt: string;
sections: Record<string, string>;
cwd: string;
contextFiles: Array<{ path: string; content: string }>;
skills: SkillInfo[];
}
export function estimateTokens(chars: number): number {
return Math.round(chars / 4);
}
export function previewOf(text: string, max = 120): string {
const firstLine = text.replace(/\s+/g, " ").trim();
return firstLine.length > max ? firstLine.slice(0, max) + "…" : firstLine;
}