tnega 0.1.0 → 0.3.0
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/README.md +115 -189
- package/dist/agent.js +2907 -0
- package/dist/agent.js.map +7 -0
- package/dist/bin.js +32512 -4419
- package/dist/bin.js.map +4 -4
- package/dist/cli-runtime.js +33404 -0
- package/dist/cli-runtime.js.map +7 -0
- package/dist/coding-agent.js +19088 -0
- package/dist/coding-agent.js.map +7 -0
- package/dist/core.js +1398 -0
- package/dist/core.js.map +7 -0
- package/dist/eval.js +23905 -0
- package/dist/eval.js.map +7 -0
- package/dist/events.js +1215 -0
- package/dist/events.js.map +7 -0
- package/dist/evolve.js +18303 -0
- package/dist/evolve.js.map +7 -0
- package/dist/index.js +33087 -4450
- package/dist/index.js.map +4 -4
- package/dist/llm.js +1016 -0
- package/dist/llm.js.map +7 -0
- package/dist/services.js +1142 -0
- package/dist/services.js.map +7 -0
- package/dist/session.js +1045 -0
- package/dist/session.js.map +7 -0
- package/dist/tools.js +2577 -0
- package/dist/tools.js.map +7 -0
- package/dist/types/packages/agent/src/definition.d.ts +17 -0
- package/dist/types/packages/agent/src/inbox-durable.d.ts +54 -0
- package/dist/types/packages/agent/src/index.d.ts +7 -0
- package/dist/types/packages/agent/src/live.d.ts +115 -0
- package/dist/types/packages/agent/src/llm-service.d.ts +32 -0
- package/dist/types/packages/agent/src/prompt.d.ts +55 -0
- package/dist/types/packages/agent/src/service.d.ts +68 -0
- package/dist/types/packages/agent/src/types.d.ts +215 -0
- package/dist/types/packages/benchmark/src/bigcodebench.d.ts +6 -0
- package/dist/types/packages/benchmark/src/humaneval.d.ts +11 -0
- package/dist/types/packages/benchmark/src/index.d.ts +9 -0
- package/dist/types/packages/benchmark/src/manifest.d.ts +10 -0
- package/dist/types/packages/benchmark/src/mbpp.d.ts +10 -0
- package/dist/types/packages/benchmark/src/parquet.d.ts +2 -0
- package/dist/types/packages/benchmark/src/stdlib.d.ts +3 -0
- package/dist/types/packages/benchmark/src/swebench.d.ts +2 -0
- package/dist/types/packages/benchmark/src/types.d.ts +58 -0
- package/dist/types/packages/cli/src/bin.d.ts +2 -0
- package/dist/types/packages/cli/src/commands.d.ts +146 -0
- package/dist/types/packages/cli/src/config.d.ts +28 -0
- package/dist/types/packages/cli/src/index.d.ts +9 -0
- package/dist/types/packages/cli/src/profile-file.d.ts +26 -0
- package/dist/types/packages/cli/src/profile.d.ts +9 -0
- package/dist/types/packages/cli/src/server.d.ts +14 -0
- package/dist/types/packages/cli/src/store.d.ts +60 -0
- package/dist/types/packages/cli/src/yaml.d.ts +1 -0
- package/dist/types/packages/coding-agent/src/codingAgent.d.ts +25 -0
- package/dist/types/packages/coding-agent/src/index.d.ts +6 -0
- package/dist/types/packages/coding-agent/src/mcp.d.ts +24 -0
- package/dist/types/packages/coding-agent/src/plan.d.ts +13 -0
- package/dist/types/packages/coding-agent/src/skills.d.ts +12 -0
- package/dist/types/packages/coding-agent/src/slash.d.ts +28 -0
- package/dist/types/packages/coding-agent/src/types.d.ts +41 -0
- package/dist/types/packages/core/src/context.d.ts +28 -0
- package/dist/types/packages/core/src/events.d.ts +57 -0
- package/dist/types/packages/core/src/fiber.d.ts +79 -0
- package/dist/types/packages/core/src/index.d.ts +10 -0
- package/dist/types/packages/core/src/invariant.d.ts +45 -0
- package/dist/types/packages/core/src/logger.d.ts +48 -0
- package/dist/types/packages/core/src/reflect.d.ts +42 -0
- package/dist/types/packages/core/src/registry.d.ts +54 -0
- package/dist/types/packages/core/src/service.d.ts +19 -0
- package/dist/types/packages/core/src/utils.d.ts +54 -0
- package/dist/types/packages/eval/src/codingRuntime.d.ts +18 -0
- package/dist/types/packages/eval/src/index.d.ts +5 -0
- package/dist/types/packages/eval/src/policy.d.ts +8 -0
- package/dist/types/packages/eval/src/runner.d.ts +31 -0
- package/dist/types/packages/eval/src/service.d.ts +23 -0
- package/dist/types/packages/eval/src/strategies.d.ts +58 -0
- package/dist/types/packages/eval/src/trace.d.ts +7 -0
- package/dist/types/packages/eval/src/types.d.ts +281 -0
- package/dist/types/packages/eval/src/utils.d.ts +5 -0
- package/dist/types/packages/eval/src/workspace.d.ts +17 -0
- package/dist/types/packages/evolve/src/diagnose.d.ts +9 -0
- package/dist/types/packages/evolve/src/index.d.ts +7 -0
- package/dist/types/packages/evolve/src/llm.d.ts +39 -0
- package/dist/types/packages/evolve/src/log.d.ts +26 -0
- package/dist/types/packages/evolve/src/select.d.ts +3 -0
- package/dist/types/packages/evolve/src/service.d.ts +29 -0
- package/dist/types/packages/evolve/src/types.d.ts +176 -0
- package/dist/types/packages/llm/src/anthropic-messages.d.ts +4 -0
- package/dist/types/packages/llm/src/errors.d.ts +6 -0
- package/dist/types/packages/llm/src/index.d.ts +7 -0
- package/dist/types/packages/llm/src/models.d.ts +29 -0
- package/dist/types/packages/llm/src/openai.d.ts +4 -0
- package/dist/types/packages/llm/src/provider.d.ts +3 -0
- package/dist/types/packages/llm/src/shared.d.ts +15 -0
- package/dist/types/packages/llm/src/types.d.ts +13 -0
- package/dist/types/packages/session/src/index.d.ts +500 -0
- package/dist/types/packages/session/src/invariant.d.ts +33 -0
- package/dist/types/packages/tools/src/builtins.d.ts +25 -0
- package/dist/types/packages/tools/src/calc.d.ts +4 -0
- package/dist/types/packages/tools/src/execution.d.ts +31 -0
- package/dist/types/packages/tools/src/index.d.ts +107 -0
- package/dist/types/packages/tools/src/path.d.ts +4 -0
- package/dist/types/packages/tools/src/policy.d.ts +14 -0
- package/dist/types/src/events.d.ts +1 -0
- package/dist/types/src/index.d.ts +15 -0
- package/dist/types/src/services.d.ts +4 -0
- package/dist/web/assets/index-45pokiBH.js +270 -0
- package/dist/web/assets/index-DCLL69Kz.css +1 -0
- package/dist/web/index.html +13 -0
- package/package.json +92 -35
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Plugin } from "../../core/src/index.js";
|
|
2
|
+
import type { AgentRuntimeOptions } from './commands.js';
|
|
3
|
+
import type { AgentProfile } from './profile.js';
|
|
4
|
+
import { bootAgentRuntime, generalAgentProfile } from './profile.js';
|
|
5
|
+
/** A serializable reference to a bundle: a built-in name, or an explicit bundler module. */
|
|
6
|
+
export type ProfileBundleRef = {
|
|
7
|
+
plugin?: undefined;
|
|
8
|
+
} | {
|
|
9
|
+
plugin: Plugin;
|
|
10
|
+
} | {
|
|
11
|
+
module: string;
|
|
12
|
+
} | {
|
|
13
|
+
name: string;
|
|
14
|
+
};
|
|
15
|
+
export interface LoadableAgentProfile {
|
|
16
|
+
name: string;
|
|
17
|
+
bundles: readonly ProfileBundleRef[];
|
|
18
|
+
options?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
export declare function profileDir(): string;
|
|
21
|
+
export declare function profileFile(name: string): string;
|
|
22
|
+
export declare function resolveProfileFile(nameOrFile: string): string;
|
|
23
|
+
export declare function readAgentProfile(nameOrFile: string): Promise<AgentProfile>;
|
|
24
|
+
export declare function ensureProfileDir(): Promise<string>;
|
|
25
|
+
export declare function bootAgentRuntimeFromFile(base: Parameters<typeof bootAgentRuntime>[0], nameOrFile: string, overlay?: Parameters<typeof bootAgentRuntime>[2]): Promise<AgentRuntimeOptions>;
|
|
26
|
+
export { generalAgentProfile };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Plugin } from "../../core/src/index.js";
|
|
2
|
+
import type { AgentRuntimeOptions } from './commands.js';
|
|
3
|
+
export interface AgentProfile {
|
|
4
|
+
name: string;
|
|
5
|
+
bundles: readonly Plugin[];
|
|
6
|
+
options?: Omit<AgentRuntimeOptions, 'cwd' | 'sessionFile' | 'plugins'>;
|
|
7
|
+
}
|
|
8
|
+
export declare const generalAgentProfile: AgentProfile;
|
|
9
|
+
export declare function bootAgentRuntime(base: Pick<AgentRuntimeOptions, 'cwd' | 'sessionFile'>, profile?: AgentProfile, overlay?: Omit<AgentRuntimeOptions, 'cwd' | 'sessionFile'>): AgentRuntimeOptions;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface WebServerOptions {
|
|
2
|
+
port?: number;
|
|
3
|
+
host?: string;
|
|
4
|
+
webRoot?: string;
|
|
5
|
+
configFile?: string;
|
|
6
|
+
/** Run sessions through resident durable-inbox agents (auto mode). */
|
|
7
|
+
resident?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface WebServer {
|
|
10
|
+
url: string;
|
|
11
|
+
port: number;
|
|
12
|
+
close: () => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare function startWebServer(options?: WebServerOptions): Promise<WebServer>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type ContextUsage, type AgentType, type ModelMessage, type SessionMode } from "../../session/src/index.js";
|
|
2
|
+
export interface SessionMetaPayload {
|
|
3
|
+
title: string;
|
|
4
|
+
workspace: string;
|
|
5
|
+
createdAt: number;
|
|
6
|
+
parentSessionId?: string;
|
|
7
|
+
forkedAtMessageId?: string;
|
|
8
|
+
agentType?: AgentType;
|
|
9
|
+
mode?: SessionMode;
|
|
10
|
+
}
|
|
11
|
+
export interface SessionSummary extends SessionMetaPayload {
|
|
12
|
+
id: string;
|
|
13
|
+
updatedAt: number;
|
|
14
|
+
eventCount: number;
|
|
15
|
+
}
|
|
16
|
+
export interface CreateSessionOptions {
|
|
17
|
+
title?: string;
|
|
18
|
+
createdAt?: number;
|
|
19
|
+
parentSessionId?: string;
|
|
20
|
+
forkedAtMessageId?: string;
|
|
21
|
+
agentType?: AgentType;
|
|
22
|
+
mode?: SessionMode;
|
|
23
|
+
}
|
|
24
|
+
export interface SessionMetaPatch {
|
|
25
|
+
title?: string;
|
|
26
|
+
agentType?: AgentType;
|
|
27
|
+
mode?: SessionMode;
|
|
28
|
+
}
|
|
29
|
+
export declare function sessionDir(workspace: string): string;
|
|
30
|
+
export declare function sessionFile(workspace: string, id: string): string;
|
|
31
|
+
export declare function isSessionId(value: string): boolean;
|
|
32
|
+
export declare function ensureSessionDir(workspace: string): Promise<string>;
|
|
33
|
+
export declare function createSession(workspace: string, options?: CreateSessionOptions): Promise<SessionSummary>;
|
|
34
|
+
export declare function listSessions(workspace: string): Promise<SessionSummary[]>;
|
|
35
|
+
export declare function readSessionSummary(workspace: string, id: string): Promise<SessionSummary>;
|
|
36
|
+
export declare function setSessionTitle(workspace: string, id: string, title: string): Promise<SessionSummary>;
|
|
37
|
+
export declare function patchSessionMeta(workspace: string, id: string, patch: SessionMetaPatch): Promise<SessionSummary>;
|
|
38
|
+
export declare function forkSession(workspace: string, id: string, options?: {
|
|
39
|
+
title?: string;
|
|
40
|
+
messageId?: string;
|
|
41
|
+
}): Promise<SessionSummary>;
|
|
42
|
+
export declare function truncateSessionAt(workspace: string, id: string, messageId: string): Promise<SessionSummary>;
|
|
43
|
+
export declare function readSessionMessages(workspace: string, id: string): Promise<ModelMessage[]>;
|
|
44
|
+
export declare function estimateContextUsage(workspace: string, id: string): Promise<ContextUsage>;
|
|
45
|
+
export interface CompactSessionOptions {
|
|
46
|
+
keep?: number;
|
|
47
|
+
keepTokens?: number;
|
|
48
|
+
checkpointMessages?: readonly ModelMessage[];
|
|
49
|
+
summary?: string;
|
|
50
|
+
tokensBefore?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface SessionCompactPreparation {
|
|
53
|
+
prefixMessages: ModelMessage[];
|
|
54
|
+
previousSummary: string | undefined;
|
|
55
|
+
tokensBefore: number;
|
|
56
|
+
}
|
|
57
|
+
export declare function prepareSessionCompact(workspace: string, id: string, keepTokens: number): Promise<SessionCompactPreparation>;
|
|
58
|
+
export declare function compactSession(workspace: string, id: string, options?: CompactSessionOptions): Promise<SessionSummary>;
|
|
59
|
+
export declare function deleteSession(workspace: string, id: string): Promise<void>;
|
|
60
|
+
export declare function ensureWorkspace(value: string): Promise<string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseYaml(text: string): Record<string, unknown>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Plugin } from "../../core/src/index.js";
|
|
2
|
+
import { type LLMAdapter } from "../../agent/src/index.js";
|
|
3
|
+
import type { ModelMessage, SessionMode } from "../../session/src/index.js";
|
|
4
|
+
import { type SlashCommandResult } from './slash.js';
|
|
5
|
+
import type { CodingSurvey, Plan, SlashCommand, SlashSuggestion } from './types.js';
|
|
6
|
+
export interface CodingAgentOptions {
|
|
7
|
+
cwd: string;
|
|
8
|
+
mode?: SessionMode;
|
|
9
|
+
setMode?: (mode: SessionMode) => void | Promise<void>;
|
|
10
|
+
skills?: boolean;
|
|
11
|
+
mcp?: boolean;
|
|
12
|
+
planTools?: boolean;
|
|
13
|
+
registerAgent?: boolean;
|
|
14
|
+
systemPrompt?: string;
|
|
15
|
+
planPrompt?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface CodingService {
|
|
18
|
+
generatePlan(adapter: LLMAdapter, messages: readonly ModelMessage[], signal?: AbortSignal): Promise<Plan>;
|
|
19
|
+
commands(): SlashCommand[];
|
|
20
|
+
runCommand(name: string, args: string[]): Promise<SlashCommandResult>;
|
|
21
|
+
suggestCommand(name: string): Promise<SlashSuggestion[]>;
|
|
22
|
+
survey(): CodingSurvey;
|
|
23
|
+
}
|
|
24
|
+
export declare const CODING_SYSTEM_PROMPT = "You are Tnega, a coding agent running in a workspace session.\n\nYou work iteratively in the user's repository:\n- Inspect the workspace before editing when the task is unclear.\n- Make small, reviewable changes and verify them with tests or commands when appropriate.\n- Report exact file paths and command output in your final answer.\n- Keep the user's existing code conventions and do not rewrite unrelated code.\n\nWhen a plan is present, follow it and keep every item's status updated with the plan tools.";
|
|
25
|
+
export declare function createCodingAgentPlugin(options: CodingAgentOptions): Plugin;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ToolDefinition } from "../../tools/src/index.js";
|
|
2
|
+
export interface McpServerConfig {
|
|
3
|
+
command: string;
|
|
4
|
+
args?: string[];
|
|
5
|
+
env?: Record<string, string>;
|
|
6
|
+
cwd?: string;
|
|
7
|
+
stderr?: 'inherit' | 'pipe' | 'ignore';
|
|
8
|
+
}
|
|
9
|
+
export interface McpConfig {
|
|
10
|
+
mcpServers?: Record<string, McpServerConfig>;
|
|
11
|
+
}
|
|
12
|
+
export interface McpSurvey {
|
|
13
|
+
name: string;
|
|
14
|
+
status: 'connected' | 'failed';
|
|
15
|
+
toolCount: number;
|
|
16
|
+
error?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface McpRuntime {
|
|
19
|
+
surveys: McpSurvey[];
|
|
20
|
+
tools: ToolDefinition[];
|
|
21
|
+
dispose(): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export declare function loadMcpConfig(cwd: string): Promise<McpConfig>;
|
|
24
|
+
export declare function connectMcpServers(cwd: string, onTool?: (server: string, tool: ToolDefinition) => void): Promise<McpRuntime>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModelMessage } from "../../session/src/index.js";
|
|
2
|
+
import type { LLMAdapter } from "../../agent/src/index.js";
|
|
3
|
+
import type { Plan, PlanStatus } from './types.js';
|
|
4
|
+
export declare class PlanGenerationError extends Error {
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const PLAN_GENERATION_MAX_RETRIES = 2;
|
|
8
|
+
export declare const PLAN_GENERATION_PROMPT = "You are a planning assistant for a coding agent.\nAnalyze the user request and produce a concise, ordered implementation plan.\n\nRespond with ONLY a JSON object in this exact shape:\n{\n \"summary\": \"one or two sentence overview of the goal\",\n \"items\": [\n {\n \"title\": \"short actionable step\",\n \"detail\": \"optional one-line detail, can be omitted\"\n }\n ]\n}\n\nRules:\n- Split the work into 2-8 concrete steps with clear checkable outcomes.\n- Keep titles short and imperative.\n- Do not include markdown fences, prose, or anything outside the JSON object.";
|
|
9
|
+
export declare function planCorrectionPrompt(previous: string): string;
|
|
10
|
+
export declare function parsePlanResponse(raw: string): Plan;
|
|
11
|
+
export declare function planToContext(plan: Plan): string;
|
|
12
|
+
export declare function isValidPlanStatus(value: unknown): value is PlanStatus;
|
|
13
|
+
export declare function generatePlan(adapter: LLMAdapter, messages: readonly ModelMessage[], signal?: AbortSignal, prompt?: string): Promise<Plan>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ToolDefinition } from "../../tools/src/index.js";
|
|
2
|
+
export interface SkillEntry {
|
|
3
|
+
name: string;
|
|
4
|
+
path: string;
|
|
5
|
+
description: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function skillsDir(cwd: string): string;
|
|
8
|
+
export declare function ensureSkillsDir(cwd: string): Promise<string>;
|
|
9
|
+
export declare function listSkills(cwd: string): Promise<SkillEntry[]>;
|
|
10
|
+
export declare function readSkill(cwd: string, name: string): Promise<string>;
|
|
11
|
+
export declare function skillTool(cwd: string): Promise<ToolDefinition>;
|
|
12
|
+
export declare function skillReadTool(cwd: string): Promise<ToolDefinition>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ToolDefinition } from "../../tools/src/index.js";
|
|
2
|
+
import type { SessionMode } from "../../session/src/index.js";
|
|
3
|
+
import { type SkillEntry } from './skills.js';
|
|
4
|
+
import type { McpSurvey } from './mcp.js';
|
|
5
|
+
import type { SlashCommand, SlashCommandResult, SlashSuggestion } from './types.js';
|
|
6
|
+
export type { SlashCommandResult, SlashSuggestion } from './types.js';
|
|
7
|
+
export interface SlashContext {
|
|
8
|
+
tools: readonly ToolDefinition[];
|
|
9
|
+
cwd: string;
|
|
10
|
+
mode?: SessionMode;
|
|
11
|
+
setMode?: (mode: SessionMode) => void | Promise<void>;
|
|
12
|
+
skills?: SkillEntry[];
|
|
13
|
+
mcp?: {
|
|
14
|
+
surveys: McpSurvey[];
|
|
15
|
+
tools: readonly ToolDefinition[];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export type SlashHandler = (args: string[], context: SlashContext) => SlashCommandResult | Promise<SlashCommandResult>;
|
|
19
|
+
export type SlashSuggester = (context: SlashContext) => SlashSuggestion[] | Promise<SlashSuggestion[]>;
|
|
20
|
+
export declare class SlashRegistry {
|
|
21
|
+
private _commands;
|
|
22
|
+
register(name: string, description: string, handler: SlashHandler, suggest?: SlashSuggester): void;
|
|
23
|
+
list(): SlashCommand[];
|
|
24
|
+
run(name: string, args: string[], context: SlashContext): Promise<SlashCommandResult>;
|
|
25
|
+
suggest(name: string, context: SlashContext): Promise<SlashSuggestion[]>;
|
|
26
|
+
has(name: string): boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare function createSlashRegistry(): SlashRegistry;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AgentType, SessionMode } from "../../session/src/index.js";
|
|
2
|
+
export type PlanStatus = 'pending' | 'done' | 'failed';
|
|
3
|
+
export interface PlanItem {
|
|
4
|
+
id: string;
|
|
5
|
+
title: string;
|
|
6
|
+
status: PlanStatus;
|
|
7
|
+
detail?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Plan {
|
|
10
|
+
summary?: string;
|
|
11
|
+
items: PlanItem[];
|
|
12
|
+
status: 'pending' | 'running' | 'done' | 'failed';
|
|
13
|
+
}
|
|
14
|
+
export interface SlashCommand {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
}
|
|
18
|
+
export interface SlashSuggestion {
|
|
19
|
+
command: string;
|
|
20
|
+
args: string[];
|
|
21
|
+
label: string;
|
|
22
|
+
detail?: string;
|
|
23
|
+
}
|
|
24
|
+
export type SlashCommandResult = {
|
|
25
|
+
kind: 'text';
|
|
26
|
+
text: string;
|
|
27
|
+
} | {
|
|
28
|
+
kind: 'json';
|
|
29
|
+
value: unknown;
|
|
30
|
+
};
|
|
31
|
+
export interface CodingSurvey {
|
|
32
|
+
agentType: AgentType;
|
|
33
|
+
mode: SessionMode;
|
|
34
|
+
planTools: number;
|
|
35
|
+
skillsEnabled: boolean;
|
|
36
|
+
skills: number;
|
|
37
|
+
mcpEnabled: boolean;
|
|
38
|
+
mcpServers: number;
|
|
39
|
+
mcpTools: number;
|
|
40
|
+
}
|
|
41
|
+
export { type AgentType, type SessionMode };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { EventsService } from './events.js';
|
|
2
|
+
import './fiber.js';
|
|
3
|
+
import { Logger, LoggerService } from './logger.js';
|
|
4
|
+
import { ReflectService } from './reflect.js';
|
|
5
|
+
import { RegistryService } from './registry.js';
|
|
6
|
+
import { symbols, type Any, type Dict } from './utils.js';
|
|
7
|
+
export interface Context {
|
|
8
|
+
root: this;
|
|
9
|
+
baseUrl?: string | undefined;
|
|
10
|
+
events: EventsService;
|
|
11
|
+
logger: LoggerService & ((name?: string) => Logger);
|
|
12
|
+
reflect: ReflectService;
|
|
13
|
+
registry: RegistryService;
|
|
14
|
+
}
|
|
15
|
+
export declare class Context {
|
|
16
|
+
[symbols.isolate]: Dict<symbol | undefined>;
|
|
17
|
+
[symbols.intercept]: Dict<any>;
|
|
18
|
+
static readonly effect: symbol;
|
|
19
|
+
static readonly filter: symbol;
|
|
20
|
+
static readonly isolate: symbol;
|
|
21
|
+
static readonly intercept: symbol;
|
|
22
|
+
static readonly marker: unique symbol;
|
|
23
|
+
static is(value: Any): value is Context;
|
|
24
|
+
constructor();
|
|
25
|
+
extend(meta?: Dict): this;
|
|
26
|
+
isolate(name: string, label?: symbol): this;
|
|
27
|
+
intercept(name: string, config: Any): this;
|
|
28
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Context } from './context.js';
|
|
2
|
+
import { type Any, type AnyFunction } from './utils.js';
|
|
3
|
+
declare module './context.js' {
|
|
4
|
+
interface Context {
|
|
5
|
+
on(name: string, listener: AnyFunction, options?: boolean | EventOptions): () => Any;
|
|
6
|
+
once(name: string, listener: AnyFunction, options?: boolean | EventOptions): () => Any;
|
|
7
|
+
emit(name: string, ...args: Any[]): void;
|
|
8
|
+
emit(thisArg: object, name: string, ...args: Any[]): void;
|
|
9
|
+
parallel(name: string, ...args: Any[]): Promise<void>;
|
|
10
|
+
parallel(thisArg: object, name: string, ...args: Any[]): Promise<void>;
|
|
11
|
+
serial(name: string, ...args: Any[]): Promise<void>;
|
|
12
|
+
serial(thisArg: object, name: string, ...args: Any[]): Promise<void>;
|
|
13
|
+
bail(name: string, ...args: Any[]): Any;
|
|
14
|
+
bail(thisArg: object, name: string, ...args: Any[]): Any;
|
|
15
|
+
waterfall(name: string, ...args: Any[]): Any;
|
|
16
|
+
waterfall(thisArg: object, name: string, ...args: Any[]): Any;
|
|
17
|
+
waterfallAsync(name: string, ...args: Any[]): Promise<Any>;
|
|
18
|
+
waterfallAsync(thisArg: object, name: string, ...args: Any[]): Promise<Any>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export declare function isBailed(value: Any): boolean;
|
|
22
|
+
export type DispatchMode = 'emit' | 'parallel' | 'serial' | 'bail' | 'waterfall' | 'waterfallAsync';
|
|
23
|
+
export interface EventOptions {
|
|
24
|
+
prepend?: boolean;
|
|
25
|
+
global?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface Hook extends EventOptions {
|
|
28
|
+
ctx: Context;
|
|
29
|
+
callback: AnyFunction;
|
|
30
|
+
}
|
|
31
|
+
export declare class EventsService {
|
|
32
|
+
private ctx;
|
|
33
|
+
_hooks: Record<string, Hook[]>;
|
|
34
|
+
constructor(ctx: Context);
|
|
35
|
+
private _resolve;
|
|
36
|
+
dispatch(type: string, args: Any[]): AnyFunction[];
|
|
37
|
+
parallel(...args: Any[]): Promise<void>;
|
|
38
|
+
emit(...args: Any[]): void;
|
|
39
|
+
serial(...args: Any[]): Promise<any>;
|
|
40
|
+
bail(...args: Any[]): any;
|
|
41
|
+
waterfall(...args: Any[]): any;
|
|
42
|
+
waterfallAsync(...args: Any[]): Promise<any>;
|
|
43
|
+
register(label: string, hooks: Hook[], callback: AnyFunction, options: EventOptions): Any;
|
|
44
|
+
unregister(hooks: Hook[], callback: AnyFunction): true | undefined;
|
|
45
|
+
on(name: string, listener: AnyFunction, options?: boolean | EventOptions): Any;
|
|
46
|
+
once(name: string, listener: AnyFunction, options?: boolean | EventOptions): any;
|
|
47
|
+
}
|
|
48
|
+
export interface Events {
|
|
49
|
+
'internal/plugin'(this: Any): void;
|
|
50
|
+
'internal/status'(this: Any, oldValue: Any): void;
|
|
51
|
+
'internal/service'(this: Context, name: string, value: Any): void;
|
|
52
|
+
'internal/update'(this: Any, config: Any, noSave: boolean, next: AnyFunction): void;
|
|
53
|
+
'internal/get'(this: Context, name: string, error: Error, next: AnyFunction): Any;
|
|
54
|
+
'internal/set'(this: Context, name: string, value: Any, error: Error, next: AnyFunction): boolean;
|
|
55
|
+
'internal/listener'(this: Context, name: string, listener: AnyFunction, options: EventOptions): Any;
|
|
56
|
+
'internal/dispatch'(mode: DispatchMode, name: string, args: Any[], thisArg: Any): void;
|
|
57
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Context } from './context.js';
|
|
2
|
+
import type { PluginRuntime } from './registry.js';
|
|
3
|
+
import type { Impl } from './reflect.js';
|
|
4
|
+
import { DisposableList, type Any, type AnyFunction, type Dict } from './utils.js';
|
|
5
|
+
declare module './context.js' {
|
|
6
|
+
interface Context extends Pick<Fiber, 'effect'> {
|
|
7
|
+
fiber: Fiber;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare class ValidationError extends TypeError {
|
|
11
|
+
name: string;
|
|
12
|
+
constructor(issues: readonly Any[]);
|
|
13
|
+
}
|
|
14
|
+
export declare function resolveConfig(runtime: PluginRuntime, config: Any): any;
|
|
15
|
+
export type Disposable = () => Any;
|
|
16
|
+
export type SyncEffect = Disposable | Iterable<Disposable> | null | undefined;
|
|
17
|
+
export type AsyncEffect = Promise<Disposable> | AsyncIterable<Disposable> | null | undefined;
|
|
18
|
+
export type Effect = SyncEffect | AsyncEffect;
|
|
19
|
+
interface AsyncDisposable extends PromiseLike<() => Promise<void>> {
|
|
20
|
+
(): Any;
|
|
21
|
+
}
|
|
22
|
+
export interface EffectMeta {
|
|
23
|
+
label: string;
|
|
24
|
+
children: EffectMeta[];
|
|
25
|
+
}
|
|
26
|
+
export declare const enum FiberState {
|
|
27
|
+
PENDING = 0,
|
|
28
|
+
LOADING = 1,
|
|
29
|
+
ACTIVE = 2,
|
|
30
|
+
FAILED = 3,
|
|
31
|
+
DISPOSED = 4,
|
|
32
|
+
UNLOADING = 5
|
|
33
|
+
}
|
|
34
|
+
export declare const CordisErrorCode: {
|
|
35
|
+
readonly INACTIVE_EFFECT: "cannot create effect on inactive context";
|
|
36
|
+
};
|
|
37
|
+
export type CordisErrorCode = keyof typeof CordisErrorCode;
|
|
38
|
+
export declare class CordisError extends Error {
|
|
39
|
+
code: CordisErrorCode;
|
|
40
|
+
constructor(code: CordisErrorCode, message?: string);
|
|
41
|
+
}
|
|
42
|
+
export declare class Fiber {
|
|
43
|
+
parent: Context;
|
|
44
|
+
inject: Dict<Any>;
|
|
45
|
+
runtime: PluginRuntime | null;
|
|
46
|
+
uid: number | null;
|
|
47
|
+
readonly ctx: Context;
|
|
48
|
+
config: Any;
|
|
49
|
+
state: FiberState;
|
|
50
|
+
readonly dispose: () => Promise<void>;
|
|
51
|
+
store: Dict<Impl> | undefined;
|
|
52
|
+
inertia: Promise<void> | undefined;
|
|
53
|
+
readonly _hooks: Dict<DisposableList<AnyFunction>>;
|
|
54
|
+
readonly _disposables: DisposableList<Disposable>;
|
|
55
|
+
protected context: Context;
|
|
56
|
+
private _error;
|
|
57
|
+
private _runner;
|
|
58
|
+
private _store;
|
|
59
|
+
private _restarting;
|
|
60
|
+
constructor(parent: Context, config: Any, inject: Dict<Any>, runtime: PluginRuntime | null, getOuterStack: () => string[]);
|
|
61
|
+
get name(): string;
|
|
62
|
+
assertActive(): void;
|
|
63
|
+
private _execute;
|
|
64
|
+
effect(execute: () => SyncEffect, label?: string): Disposable;
|
|
65
|
+
effect(execute: () => Effect, label?: string): AsyncDisposable;
|
|
66
|
+
getEffects(): EffectMeta[];
|
|
67
|
+
private _getState;
|
|
68
|
+
private _updateState;
|
|
69
|
+
_checkImpl(name: string): boolean | undefined;
|
|
70
|
+
_refresh(): void;
|
|
71
|
+
private _setEpoch;
|
|
72
|
+
private _reload;
|
|
73
|
+
private _unload;
|
|
74
|
+
await(): Promise<this>;
|
|
75
|
+
restart(): Promise<undefined>;
|
|
76
|
+
private _runRestart;
|
|
77
|
+
update(config: Any, noSave?: boolean): void;
|
|
78
|
+
}
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './context.js';
|
|
2
|
+
export * from './events.js';
|
|
3
|
+
export * from './fiber.js';
|
|
4
|
+
export * from './invariant.js';
|
|
5
|
+
export * from './logger.js';
|
|
6
|
+
export * from './reflect.js';
|
|
7
|
+
export * from './registry.js';
|
|
8
|
+
export * from './service.js';
|
|
9
|
+
export * from './utils.js';
|
|
10
|
+
export declare const name = "index.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invariant companion support.
|
|
3
|
+
*
|
|
4
|
+
* Layered defense, mirroring the DSH "each package ships a `./invariant`"
|
|
5
|
+
* discipline: every package that owns durable or live relationships exports a
|
|
6
|
+
* set of checks that continuously assert cross-event / cross-data invariants
|
|
7
|
+
* (open/close pairing, monotonic sequence, ownership). The checks are pure
|
|
8
|
+
* over the data they observe, so they can run at any time: on load, on each
|
|
9
|
+
* commit, or on demand during tests and debugging.
|
|
10
|
+
*/
|
|
11
|
+
export interface InvariantContext {
|
|
12
|
+
/**
|
|
13
|
+
* The durable events observed so far. A check runs over whatever slice the
|
|
14
|
+
* caller supplies (the full log, a single appended event, …).
|
|
15
|
+
*/
|
|
16
|
+
events: readonly unknown[];
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
/** A check returns a list of violated descriptions; an empty list means pass. */
|
|
20
|
+
export type InvariantCheck = (context: InvariantContext) => readonly string[] | Promise<readonly string[]>;
|
|
21
|
+
export interface InvariantDefinition {
|
|
22
|
+
/** Stable, package-scoped name, e.g. `session/open-lifecycle`. */
|
|
23
|
+
name: string;
|
|
24
|
+
/** When this check does not apply, explain why in one line. */
|
|
25
|
+
describe(): string;
|
|
26
|
+
check: InvariantCheck;
|
|
27
|
+
}
|
|
28
|
+
export declare class InvariantRegistry {
|
|
29
|
+
private _checks;
|
|
30
|
+
register(definition: InvariantDefinition): () => void;
|
|
31
|
+
has(name: string): boolean;
|
|
32
|
+
list(): readonly InvariantDefinition[];
|
|
33
|
+
runAll(context: InvariantContext): Promise<readonly string[]>;
|
|
34
|
+
}
|
|
35
|
+
export declare class InvariantViolationError extends Error {
|
|
36
|
+
readonly violations: readonly string[];
|
|
37
|
+
name: string;
|
|
38
|
+
constructor(violations: readonly string[]);
|
|
39
|
+
}
|
|
40
|
+
/** Fail-fast helper: throws when any registered invariant reports a violation. */
|
|
41
|
+
export declare function assertInvariants(violations: readonly string[]): void;
|
|
42
|
+
export declare const invariant: {
|
|
43
|
+
name: string;
|
|
44
|
+
apply: (ctx: import("./context.js").Context) => () => void;
|
|
45
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Context } from './context.js';
|
|
2
|
+
import { symbols, type Any } from './utils.js';
|
|
3
|
+
export type LoggerType = 'error' | 'info' | 'warn' | 'debug';
|
|
4
|
+
export type LoggerMethod = (format: Any, ...param: Any[]) => void;
|
|
5
|
+
export interface Message {
|
|
6
|
+
sn: number;
|
|
7
|
+
ts: number;
|
|
8
|
+
name: string;
|
|
9
|
+
type: LoggerType;
|
|
10
|
+
args: Any[];
|
|
11
|
+
}
|
|
12
|
+
export interface Exporter {
|
|
13
|
+
level?: number;
|
|
14
|
+
export(message: Message): void;
|
|
15
|
+
}
|
|
16
|
+
export interface LoggerOptions {
|
|
17
|
+
name: string;
|
|
18
|
+
level?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface LoggerService {
|
|
21
|
+
(name?: string): Logger;
|
|
22
|
+
}
|
|
23
|
+
export declare class Logger {
|
|
24
|
+
private service;
|
|
25
|
+
name: string;
|
|
26
|
+
level: number | undefined;
|
|
27
|
+
error: LoggerMethod;
|
|
28
|
+
info: LoggerMethod;
|
|
29
|
+
warn: LoggerMethod;
|
|
30
|
+
debug: LoggerMethod;
|
|
31
|
+
constructor(options: LoggerOptions, service: LoggerService);
|
|
32
|
+
private _method;
|
|
33
|
+
}
|
|
34
|
+
export declare class LoggerService {
|
|
35
|
+
[symbols.invoke]: (name?: string) => Logger;
|
|
36
|
+
buffer: Message[];
|
|
37
|
+
ctx: Context;
|
|
38
|
+
_snMessage: number;
|
|
39
|
+
_snExporter: number;
|
|
40
|
+
exporters: Map<number, Exporter>;
|
|
41
|
+
constructor(ctx: Context);
|
|
42
|
+
exporter(exporter: Exporter): import("./fiber.js").Disposable;
|
|
43
|
+
_log(message: Omit<Message, 'sn' | 'ts'>): void;
|
|
44
|
+
error(...args: Any[]): any;
|
|
45
|
+
info(...args: Any[]): any;
|
|
46
|
+
warn(...args: Any[]): any;
|
|
47
|
+
debug(...args: Any[]): any;
|
|
48
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Context } from './context.js';
|
|
2
|
+
import { type Any, type AnyFunction, type Dict } from './utils.js';
|
|
3
|
+
declare module './context.js' {
|
|
4
|
+
interface Context {
|
|
5
|
+
get(name: string, strict?: boolean): Any;
|
|
6
|
+
set(name: string, value: Any): void;
|
|
7
|
+
provide(name: string, value?: Any, check?: () => boolean): () => Any;
|
|
8
|
+
accessor(name: string, options: Omit<AccessorProperty, 'type'>): () => Any;
|
|
9
|
+
mixin(source: Any, mixins: string[] | Dict<string>): void;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export interface ServiceProperty {
|
|
13
|
+
type: 'service';
|
|
14
|
+
}
|
|
15
|
+
export interface AccessorProperty {
|
|
16
|
+
type: 'accessor';
|
|
17
|
+
get: (this: Context, receiver: Any, error: Error) => Any;
|
|
18
|
+
set?: (this: Context, value: Any, receiver: Any, error: Error) => boolean;
|
|
19
|
+
}
|
|
20
|
+
export type ReflectProperty = ServiceProperty | AccessorProperty;
|
|
21
|
+
export interface Impl {
|
|
22
|
+
name: string;
|
|
23
|
+
fiber: Any;
|
|
24
|
+
value?: Any;
|
|
25
|
+
check?: () => boolean;
|
|
26
|
+
}
|
|
27
|
+
export declare class ReflectService {
|
|
28
|
+
ctx: Context;
|
|
29
|
+
static handler: ProxyHandler<Context>;
|
|
30
|
+
store: Dict<Impl, symbol>;
|
|
31
|
+
props: Dict<ReflectProperty>;
|
|
32
|
+
constructor(ctx: Context);
|
|
33
|
+
get(name: string, strict?: boolean): any;
|
|
34
|
+
_getImpl(name: string, strict?: boolean): any;
|
|
35
|
+
set(name: string, value: Any): boolean;
|
|
36
|
+
provide(name: string, value?: Any, check?: () => boolean): import("./fiber.js").Disposable;
|
|
37
|
+
notify(names: string[], filter?: (ctx: Context, name: string) => boolean): any[];
|
|
38
|
+
accessor(name: string, options: Omit<AccessorProperty, 'type'>): import("./fiber.js").Disposable;
|
|
39
|
+
mixin(source: Any, mixins: string[] | Dict<string>): import("./fiber.js").Disposable;
|
|
40
|
+
trace<T>(value: T): T;
|
|
41
|
+
bind<T extends AnyFunction>(callback: T): T;
|
|
42
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Context } from './context.js';
|
|
2
|
+
import { Fiber } from './fiber.js';
|
|
3
|
+
import { DisposableList, type Any, type AnyFunction, type Dict } from './utils.js';
|
|
4
|
+
declare module './context.js' {
|
|
5
|
+
interface Context {
|
|
6
|
+
inject(deps: Inject, callback: FunctionPlugin): Fiber & PromiseLike<Fiber>;
|
|
7
|
+
plugin<P extends Plugin>(plugin: P, ...args: Any[]): Fiber & PromiseLike<Fiber>;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export type Inject = string[] | Dict<Any>;
|
|
11
|
+
export type InjectKey = string & keyof Context;
|
|
12
|
+
export declare function Inject(name: string, config?: Any): (value: Any, decorator: Any) => void;
|
|
13
|
+
export declare function resolveInject(inject: Inject | null | undefined, result?: Dict): Dict;
|
|
14
|
+
export type Plugin = FunctionPlugin | ConstructorPlugin | ObjectPlugin;
|
|
15
|
+
export interface PluginBase {
|
|
16
|
+
name?: string;
|
|
17
|
+
Config?: Any;
|
|
18
|
+
inject?: Inject;
|
|
19
|
+
provide?: string | string[];
|
|
20
|
+
intercept?: Dict<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export interface FunctionPlugin extends PluginBase {
|
|
23
|
+
(ctx: Context, config: Any): Any;
|
|
24
|
+
}
|
|
25
|
+
export interface ConstructorPlugin extends PluginBase {
|
|
26
|
+
new (ctx: Context, config: Any): Any;
|
|
27
|
+
}
|
|
28
|
+
export interface ObjectPlugin extends PluginBase {
|
|
29
|
+
apply(ctx: Context, config: Any): Any;
|
|
30
|
+
}
|
|
31
|
+
export interface PluginRuntime {
|
|
32
|
+
name?: string;
|
|
33
|
+
fibers: DisposableList<Fiber>;
|
|
34
|
+
callback: Any;
|
|
35
|
+
Config?: Any;
|
|
36
|
+
}
|
|
37
|
+
export declare class RegistryService {
|
|
38
|
+
ctx: Context;
|
|
39
|
+
private _counter;
|
|
40
|
+
private _internal;
|
|
41
|
+
constructor(ctx: Context);
|
|
42
|
+
get counter(): number;
|
|
43
|
+
get size(): number;
|
|
44
|
+
resolve(plugin: Plugin): AnyFunction | undefined;
|
|
45
|
+
get(plugin: Plugin): PluginRuntime | undefined;
|
|
46
|
+
has(plugin: Plugin): boolean;
|
|
47
|
+
delete(plugin: Plugin): PluginRuntime | undefined;
|
|
48
|
+
keys(): MapIterator<AnyFunction>;
|
|
49
|
+
values(): MapIterator<PluginRuntime>;
|
|
50
|
+
entries(): MapIterator<[AnyFunction, PluginRuntime]>;
|
|
51
|
+
forEach(callback: (value: PluginRuntime, key: AnyFunction) => void): void;
|
|
52
|
+
inject(inject: Inject, callback: FunctionPlugin): Fiber & PromiseLike<Fiber>;
|
|
53
|
+
plugin(plugin: Plugin, config?: Any, getOuterStack?: () => string[]): Fiber & PromiseLike<Fiber>;
|
|
54
|
+
}
|