qlogicagent 0.5.2 → 0.5.3
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 +402 -402
- package/dist/agent.js +18 -0
- package/dist/cli.js +360 -0
- package/dist/contracts.js +1 -0
- package/dist/index.js +22 -0
- package/dist/orchestration.js +125 -0
- package/dist/types/agent/agent.d.ts +43 -0
- package/dist/types/agent/constants.d.ts +47 -0
- package/dist/types/agent/tool-loop.d.ts +96 -0
- package/dist/types/agent/types.d.ts +250 -0
- package/dist/types/cli/main.d.ts +11 -0
- package/dist/types/cli/stdio-server.d.ts +56 -0
- package/dist/types/cli/tool-bootstrap.d.ts +21 -0
- package/dist/types/config/config.d.ts +17 -0
- package/dist/types/contracts/hooks.d.ts +175 -0
- package/dist/types/contracts/index.d.ts +10 -0
- package/dist/types/contracts/planner.d.ts +35 -0
- package/dist/types/contracts/skill-candidate.d.ts +63 -0
- package/dist/types/contracts/todo.d.ts +14 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/llm/builtin-providers.d.ts +10 -0
- package/dist/types/llm/debug-transport.d.ts +12 -0
- package/dist/types/llm/index.d.ts +16 -0
- package/dist/types/llm/llm-client.d.ts +43 -0
- package/dist/types/llm/model-catalog.d.ts +53 -0
- package/dist/types/llm/provider-def.d.ts +59 -0
- package/dist/types/llm/provider-registry.d.ts +54 -0
- package/dist/types/llm/transport.d.ts +62 -0
- package/dist/types/llm/transports/anthropic-messages.d.ts +31 -0
- package/dist/types/llm/transports/openai-chat.d.ts +36 -0
- package/dist/types/orchestration/agent-registry.d.ts +46 -0
- package/dist/types/orchestration/context-collapse.d.ts +58 -0
- package/dist/types/orchestration/context-compression.d.ts +301 -0
- package/dist/types/orchestration/conversation-repair.d.ts +61 -0
- package/dist/types/orchestration/curator-scheduler.d.ts +119 -0
- package/dist/types/orchestration/error-classification.d.ts +12 -0
- package/dist/types/orchestration/failover-classification.d.ts +8 -0
- package/dist/types/orchestration/failover-error.d.ts +33 -0
- package/dist/types/orchestration/fork-subagent.d.ts +100 -0
- package/dist/types/orchestration/index.d.ts +113 -0
- package/dist/types/orchestration/memory-provider.d.ts +14 -0
- package/dist/types/orchestration/reactive-compact.d.ts +73 -0
- package/dist/types/orchestration/retry-loop.d.ts +69 -0
- package/dist/types/orchestration/skill-candidate.d.ts +52 -0
- package/dist/types/orchestration/skill-consolidation.d.ts +123 -0
- package/dist/types/orchestration/skill-improvement.d.ts +59 -0
- package/dist/types/orchestration/skill-similarity.d.ts +98 -0
- package/dist/types/orchestration/task-types.d.ts +142 -0
- package/dist/types/orchestration/team-orchestration.d.ts +195 -0
- package/dist/types/orchestration/team-tool-loop-wiring.d.ts +92 -0
- package/dist/types/orchestration/tool-choice-policy.d.ts +54 -0
- package/dist/types/orchestration/tool-loop-state.d.ts +50 -0
- package/dist/types/orchestration/tool-schema.d.ts +39 -0
- package/dist/types/orchestration/turn-loop-guard.d.ts +86 -0
- package/dist/types/runtime/execution/dream-agent.d.ts +199 -0
- package/dist/types/runtime/execution/forked-agent.d.ts +111 -0
- package/dist/types/runtime/execution/index.d.ts +6 -0
- package/dist/types/runtime/execution/progress-tracker.d.ts +78 -0
- package/dist/types/runtime/execution/remote-agent.d.ts +63 -0
- package/dist/types/runtime/execution/streaming-tool-executor.d.ts +100 -0
- package/dist/types/runtime/execution/tool-eligibility.d.ts +59 -0
- package/dist/types/runtime/execution/tool-result-storage.d.ts +87 -0
- package/dist/types/runtime/hooks/context-compression.d.ts +61 -0
- package/dist/types/runtime/hooks/hook-registry.d.ts +12 -0
- package/dist/types/runtime/hooks/index.d.ts +3 -0
- package/dist/types/runtime/hooks/memory-hooks.d.ts +49 -0
- package/dist/types/runtime/index.d.ts +5 -0
- package/dist/types/runtime/infra/cleanup-registry.d.ts +23 -0
- package/dist/types/runtime/infra/file-watcher.d.ts +72 -0
- package/dist/types/runtime/infra/index.d.ts +6 -0
- package/dist/types/runtime/infra/secure-storage.d.ts +81 -0
- package/dist/types/runtime/infra/task-runtime.d.ts +108 -0
- package/dist/types/runtime/infra/token-budget.d.ts +92 -0
- package/dist/types/runtime/infra/worktree-backend.d.ts +85 -0
- package/dist/types/runtime/prompt/environment-context.d.ts +23 -0
- package/dist/types/runtime/prompt/index.d.ts +3 -0
- package/dist/types/runtime/prompt/instruction-loader.d.ts +64 -0
- package/dist/types/runtime/prompt/system-prompt-sections.d.ts +63 -0
- package/dist/types/runtime/session/index.d.ts +2 -0
- package/dist/types/runtime/session/session-memory.d.ts +91 -0
- package/dist/types/runtime/session/session-persistence.d.ts +94 -0
- package/dist/types/runtime/session/session-state.d.ts +117 -0
- package/dist/types/skills/index.d.ts +125 -0
- package/dist/types/skills/mcp/index.d.ts +3 -0
- package/dist/types/skills/mcp/mcp-http-client.d.ts +66 -0
- package/dist/types/skills/mcp/mcp-manager.d.ts +84 -0
- package/dist/types/skills/mcp/mcp-stdio-client.d.ts +84 -0
- package/dist/types/skills/memory-extractor.d.ts +64 -0
- package/dist/types/skills/memory-query-tool.d.ts +43 -0
- package/dist/types/skills/memory-store.d.ts +66 -0
- package/dist/types/skills/memory-tool.d.ts +67 -0
- package/dist/types/skills/permissions/bash-classifier.d.ts +30 -0
- package/dist/types/skills/permissions/classifier-cache.d.ts +51 -0
- package/dist/types/skills/permissions/denial-tracking.d.ts +42 -0
- package/dist/types/skills/permissions/hook-runner.d.ts +85 -0
- package/dist/types/skills/permissions/index.d.ts +12 -0
- package/dist/types/skills/permissions/permission-classifier.d.ts +41 -0
- package/dist/types/skills/permissions/rule-engine.d.ts +41 -0
- package/dist/types/skills/permissions/settings-watcher.d.ts +46 -0
- package/dist/types/skills/permissions/types.d.ts +113 -0
- package/dist/types/skills/plugins/index.d.ts +2 -0
- package/dist/types/skills/plugins/plugin-api.d.ts +38 -0
- package/dist/types/skills/plugins/plugin-loader.d.ts +45 -0
- package/dist/types/skills/plugins/plugin-marketplace.d.ts +61 -0
- package/dist/types/skills/portable-tool.d.ts +71 -0
- package/dist/types/skills/qmemory-adapter.d.ts +42 -0
- package/dist/types/skills/skill-frontmatter.d.ts +19 -0
- package/dist/types/skills/skill-guard.d.ts +23 -0
- package/dist/types/skills/skill-loader.d.ts +16 -0
- package/dist/types/skills/skill-source.d.ts +119 -0
- package/dist/types/skills/skill-types.d.ts +199 -0
- package/dist/types/skills/think-tool.d.ts +16 -0
- package/dist/types/skills/todo-tool.d.ts +63 -0
- package/dist/types/skills/tool-registry.d.ts +29 -0
- package/dist/types/skills/tools/agent-tool.d.ts +91 -0
- package/dist/types/skills/tools/apply-patch-tool.d.ts +29 -0
- package/dist/types/skills/tools/ask-user-tool.d.ts +80 -0
- package/dist/types/skills/tools/brief-tool.d.ts +74 -0
- package/dist/types/skills/tools/browser-tool.d.ts +114 -0
- package/dist/types/skills/tools/checkpoint-tool.d.ts +66 -0
- package/dist/types/skills/tools/config-tool.d.ts +63 -0
- package/dist/types/skills/tools/cron-tool.d.ts +116 -0
- package/dist/types/skills/tools/edit-tool.d.ts +43 -0
- package/dist/types/skills/tools/exec-tool.d.ts +97 -0
- package/dist/types/skills/tools/image-generate-tool.d.ts +62 -0
- package/dist/types/skills/tools/instructions-tool.d.ts +103 -0
- package/dist/types/skills/tools/lsp-tool.d.ts +153 -0
- package/dist/types/skills/tools/mcp-client-types.d.ts +269 -0
- package/dist/types/skills/tools/mcp-resource-tools.d.ts +14 -0
- package/dist/types/skills/tools/mcp-tool.d.ts +249 -0
- package/dist/types/skills/tools/memory-tool.d.ts +74 -0
- package/dist/types/skills/tools/monitor-tool.d.ts +113 -0
- package/dist/types/skills/tools/music-generate-tool.d.ts +55 -0
- package/dist/types/skills/tools/notebook-edit-tool.d.ts +15 -0
- package/dist/types/skills/tools/notify-tool.d.ts +53 -0
- package/dist/types/skills/tools/patch-tool.d.ts +45 -0
- package/dist/types/skills/tools/pdf-tool.d.ts +66 -0
- package/dist/types/skills/tools/plan-mode-tool.d.ts +98 -0
- package/dist/types/skills/tools/read-tool.d.ts +51 -0
- package/dist/types/skills/tools/repl-tool.d.ts +70 -0
- package/dist/types/skills/tools/search-tool.d.ts +112 -0
- package/dist/types/skills/tools/send-message-tool.d.ts +51 -0
- package/dist/types/skills/tools/shell/bash-provider.d.ts +26 -0
- package/dist/types/skills/tools/shell/command-classification.d.ts +44 -0
- package/dist/types/skills/tools/shell/command-semantics.d.ts +14 -0
- package/dist/types/skills/tools/shell/destructive-command-warning.d.ts +10 -0
- package/dist/types/skills/tools/shell/exec-permissions.d.ts +52 -0
- package/dist/types/skills/tools/shell/index.d.ts +17 -0
- package/dist/types/skills/tools/shell/powershell-provider.d.ts +15 -0
- package/dist/types/skills/tools/shell/shell-command.d.ts +54 -0
- package/dist/types/skills/tools/shell/shell-exec.d.ts +33 -0
- package/dist/types/skills/tools/shell/shell-provider.d.ts +85 -0
- package/dist/types/skills/tools/shell/task-output.d.ts +45 -0
- package/dist/types/skills/tools/skill-invoke-tool.d.ts +46 -0
- package/dist/types/skills/tools/skill-list-tool.d.ts +33 -0
- package/dist/types/skills/tools/skill-manage-tool.d.ts +73 -0
- package/dist/types/skills/tools/skill-view-tool.d.ts +37 -0
- package/dist/types/skills/tools/sleep-tool.d.ts +49 -0
- package/dist/types/skills/tools/structured-output-tool.d.ts +116 -0
- package/dist/types/skills/tools/task-tool.d.ts +104 -0
- package/dist/types/skills/tools/team-tool.d.ts +89 -0
- package/dist/types/skills/tools/tool-search-tool.d.ts +51 -0
- package/dist/types/skills/tools/tts-tool.d.ts +38 -0
- package/dist/types/skills/tools/video-edit-tool.d.ts +69 -0
- package/dist/types/skills/tools/video-generate-tool.d.ts +62 -0
- package/dist/types/skills/tools/video-merge-tool.d.ts +105 -0
- package/dist/types/skills/tools/video-upscale-tool.d.ts +45 -0
- package/dist/types/skills/tools/web-fetch-tool.d.ts +78 -0
- package/dist/types/skills/tools/web-search-tool.d.ts +57 -0
- package/dist/types/skills/tools/workflow-tool.d.ts +44 -0
- package/dist/types/skills/tools/worktree-tool.d.ts +69 -0
- package/dist/types/skills/tools/write-tool.d.ts +45 -0
- package/package.json +3 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worktree Backend — git command implementation for WorktreeToolDeps.
|
|
3
|
+
*
|
|
4
|
+
* CC reference: claude-code-haha/src/utils/worktree.ts
|
|
5
|
+
*
|
|
6
|
+
* Implements the host-provided git worktree backend:
|
|
7
|
+
* - enterWorktree(): git worktree add -B <branch> <path> HEAD
|
|
8
|
+
* - exitWorktree(): git worktree remove / keep
|
|
9
|
+
* - listWorktrees(): git worktree list --porcelain
|
|
10
|
+
* - isInWorktree(): check if cwd is inside a worktree
|
|
11
|
+
* - currentWorktree(): get current worktree info
|
|
12
|
+
*
|
|
13
|
+
* State management:
|
|
14
|
+
* - Tracks current worktree session in memory
|
|
15
|
+
* - CWD switching via process.chdir()
|
|
16
|
+
* - Safety checks: uncommitted changes, unpushed commits
|
|
17
|
+
*
|
|
18
|
+
* Security: validateWorktreeSlug() prevents path traversal.
|
|
19
|
+
*/
|
|
20
|
+
import type { WorktreeToolDeps } from "../../skills/tools/worktree-tool.js";
|
|
21
|
+
interface WorktreeSession {
|
|
22
|
+
originalCwd: string;
|
|
23
|
+
worktreePath: string;
|
|
24
|
+
worktreeName: string;
|
|
25
|
+
worktreeBranch: string;
|
|
26
|
+
/** tmux session name (CC parity). */
|
|
27
|
+
tmuxSessionName?: string;
|
|
28
|
+
/** Whether worktree was created via a user hook (CC hookBased). */
|
|
29
|
+
hookBased?: boolean;
|
|
30
|
+
/** Creation duration in ms (unset on resume). */
|
|
31
|
+
creationDurationMs?: number;
|
|
32
|
+
/** True if sparse-checkout was applied. */
|
|
33
|
+
usedSparsePaths?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** Get the current worktree session (CC getCurrentWorktreeSession parity). */
|
|
36
|
+
export declare function getCurrentWorktreeSession(): WorktreeSession | null;
|
|
37
|
+
/** Restore session on resume (CC restoreWorktreeSession parity). */
|
|
38
|
+
export declare function restoreWorktreeSession(session: WorktreeSession | null): void;
|
|
39
|
+
/** Check whether tmux is available on the system. */
|
|
40
|
+
export declare function isTmuxAvailable(): Promise<boolean>;
|
|
41
|
+
/** Generate a tmux session name from repo path and branch (CC parity). */
|
|
42
|
+
export declare function generateTmuxSessionName(repoPath: string, branch: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Create a tmux session for a worktree (CC createTmuxSessionForWorktree parity).
|
|
45
|
+
* Returns the session name, or null if tmux is not available.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createTmuxSession(worktreePath: string, sessionName: string): Promise<string | null>;
|
|
48
|
+
/** Kill a tmux session (CC killTmuxSession parity). */
|
|
49
|
+
export declare function killTmuxSession(sessionName: string): Promise<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a lightweight worktree for a sub-agent without modifying the
|
|
52
|
+
* global session. CC reference: createAgentWorktree().
|
|
53
|
+
*/
|
|
54
|
+
export declare function createAgentWorktree(gitRoot: string, slug: string, log?: {
|
|
55
|
+
info(msg: string): void;
|
|
56
|
+
warn(msg: string): void;
|
|
57
|
+
}): Promise<{
|
|
58
|
+
worktreePath: string;
|
|
59
|
+
branch: string;
|
|
60
|
+
} | null>;
|
|
61
|
+
/** Remove an agent worktree (CC removeAgentWorktree parity). */
|
|
62
|
+
export declare function removeAgentWorktree(gitRoot: string, worktreePath: string, branch: string, log?: {
|
|
63
|
+
info(msg: string): void;
|
|
64
|
+
warn(msg: string): void;
|
|
65
|
+
}): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Clean up stale agent worktrees older than maxAgeDays (CC cleanupStaleAgentWorktrees parity).
|
|
68
|
+
*/
|
|
69
|
+
export declare function cleanupStaleAgentWorktrees(gitRoot: string, maxAgeDays?: number, log?: {
|
|
70
|
+
info(msg: string): void;
|
|
71
|
+
warn(msg: string): void;
|
|
72
|
+
}): Promise<number>;
|
|
73
|
+
export interface WorktreeBackendOptions {
|
|
74
|
+
/** Logger for worktree operations */
|
|
75
|
+
log: {
|
|
76
|
+
info(msg: string): void;
|
|
77
|
+
warn(msg: string): void;
|
|
78
|
+
};
|
|
79
|
+
/** Directories to symlink from main repo (CC parity: node_modules, etc.) */
|
|
80
|
+
symlinkDirs?: string[];
|
|
81
|
+
/** Sparse-checkout paths (CC settings.worktree.sparsePaths parity). */
|
|
82
|
+
sparsePaths?: string[];
|
|
83
|
+
}
|
|
84
|
+
export declare function createWorktreeBackend(opts: WorktreeBackendOptions): WorktreeToolDeps;
|
|
85
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment Context — CC environment-aware system prompt parity.
|
|
3
|
+
*
|
|
4
|
+
* Injects OS, shell, CWD, and platform info into the system prompt
|
|
5
|
+
* so the agent can generate platform-appropriate commands.
|
|
6
|
+
*
|
|
7
|
+
* CC reference: ShellSnapshot.ts (shell type detection),
|
|
8
|
+
* shellCompletion.ts (platform-aware command generation)
|
|
9
|
+
*
|
|
10
|
+
* This is a system prompt section that provides:
|
|
11
|
+
* - OS name + version
|
|
12
|
+
* - Shell type (bash/powershell/zsh)
|
|
13
|
+
* - Current working directory
|
|
14
|
+
* - Node.js version
|
|
15
|
+
* - Available package managers
|
|
16
|
+
*/
|
|
17
|
+
import { type SystemPromptSection } from "./system-prompt-sections.js";
|
|
18
|
+
export type ShellType = "bash" | "powershell" | "zsh" | "cmd" | "fish" | "unknown";
|
|
19
|
+
/**
|
|
20
|
+
* Create a system prompt section that provides environment context.
|
|
21
|
+
* Memoized — computed once per session (environment doesn't change mid-session).
|
|
22
|
+
*/
|
|
23
|
+
export declare function createEnvironmentContextSection(): SystemPromptSection;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { assembleSystemPrompt, clearSystemPromptSections, systemPromptSection, type SystemPromptSection, } from "./system-prompt-sections.js";
|
|
2
|
+
export { getInstructions, buildInstructionsPrompt, resetInstructionCache, } from "./instruction-loader.js";
|
|
3
|
+
export { createEnvironmentContextSection } from "./environment-context.js";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instruction Loader — CC claudemd.ts parity (adapted for qlogicagent).
|
|
3
|
+
*
|
|
4
|
+
* Discovers and loads instruction files following CC's load order:
|
|
5
|
+
* 1. User instructions (~/.openclaw/INSTRUCTIONS.md)
|
|
6
|
+
* 2. Project instructions (INSTRUCTIONS.md, .openclaw/INSTRUCTIONS.md,
|
|
7
|
+
* .openclaw/rules/*.md — walkup from cwd to git root)
|
|
8
|
+
* 3. Local instructions (INSTRUCTIONS.local.md — private, not checked in)
|
|
9
|
+
*
|
|
10
|
+
* Features ported from CC:
|
|
11
|
+
* - Walkup discovery (cwd → root)
|
|
12
|
+
* - @include directive support (text files only)
|
|
13
|
+
* - Frontmatter globs for conditional rules
|
|
14
|
+
* - HTML comment stripping
|
|
15
|
+
* - Memoized loading with cache invalidation
|
|
16
|
+
* - TEXT_FILE_EXTENSIONS whitelist
|
|
17
|
+
* - MAX_INCLUDE_DEPTH circular reference prevention
|
|
18
|
+
*
|
|
19
|
+
* Reference: claude-code src/utils/claudemd.ts
|
|
20
|
+
*/
|
|
21
|
+
import type { HookRegistry } from "../../contracts/hooks.js";
|
|
22
|
+
export type InstructionType = "User" | "Project" | "Local";
|
|
23
|
+
export interface InstructionFileInfo {
|
|
24
|
+
path: string;
|
|
25
|
+
type: InstructionType;
|
|
26
|
+
content: string;
|
|
27
|
+
/** Path of the file that @included this one */
|
|
28
|
+
parent?: string;
|
|
29
|
+
/** Glob patterns from frontmatter — file only applies when target path matches */
|
|
30
|
+
globs?: string[];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Load all instruction files following CC's discovery order.
|
|
34
|
+
*
|
|
35
|
+
* Discovery order (priority: last loaded = highest):
|
|
36
|
+
* 1. User: ~/.openclaw/INSTRUCTIONS.md, ~/.openclaw/rules/*.md
|
|
37
|
+
* 2. Project: Walk from git root → cwd, each dir:
|
|
38
|
+
* - INSTRUCTIONS.md
|
|
39
|
+
* - .openclaw/INSTRUCTIONS.md
|
|
40
|
+
* - .openclaw/rules/*.md (unconditional)
|
|
41
|
+
* 3. Local: INSTRUCTIONS.local.md (per dir, walkup)
|
|
42
|
+
*
|
|
43
|
+
* @param cwd - Starting directory for walkup discovery
|
|
44
|
+
* @param hooks - Optional hook registry for instructions.loaded event
|
|
45
|
+
*/
|
|
46
|
+
export declare function loadInstructions(cwd: string, hooks?: HookRegistry): Promise<InstructionFileInfo[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Build the instructions prompt string from loaded files.
|
|
49
|
+
* CC parity: getClaudeMds()
|
|
50
|
+
*/
|
|
51
|
+
export declare function buildInstructionsPrompt(files: InstructionFileInfo[]): string;
|
|
52
|
+
/**
|
|
53
|
+
* Get conditional rules that match a target file path.
|
|
54
|
+
* CC parity: getManagedAndUserConditionalRules() + getMemoryFilesForNestedDirectory()
|
|
55
|
+
*/
|
|
56
|
+
export declare function getConditionalRules(cwd: string, targetPath: string): Promise<InstructionFileInfo[]>;
|
|
57
|
+
/**
|
|
58
|
+
* Load instructions with memoization.
|
|
59
|
+
* CC parity: getMemoryFiles() with memoize.
|
|
60
|
+
* Returns cached result if cwd hasn't changed.
|
|
61
|
+
*/
|
|
62
|
+
export declare function getInstructions(cwd: string, hooks?: HookRegistry): Promise<InstructionFileInfo[]>;
|
|
63
|
+
/** Clear the instruction cache. Called after file changes or compaction. */
|
|
64
|
+
export declare function resetInstructionCache(): void;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System Prompt Sections — CC systemPromptSections.ts parity.
|
|
3
|
+
*
|
|
4
|
+
* Provides a section-based system prompt assembly mechanism:
|
|
5
|
+
* - Memoized sections (computed once, cached until clear)
|
|
6
|
+
* - Volatile sections (recompute every turn, cache-breaking)
|
|
7
|
+
* - User-provided appendSystemPrompt / customSystemPrompt
|
|
8
|
+
* - Clear on session reset / compact
|
|
9
|
+
*
|
|
10
|
+
* CC reference: src/constants/systemPromptSections.ts
|
|
11
|
+
*/
|
|
12
|
+
type ComputeFn = () => string | null | Promise<string | null>;
|
|
13
|
+
export interface SystemPromptSection {
|
|
14
|
+
name: string;
|
|
15
|
+
compute: ComputeFn;
|
|
16
|
+
/** If true, recompute every turn (breaks prompt cache). CC: cacheBreak. */
|
|
17
|
+
cacheBreak: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a memoized system prompt section.
|
|
21
|
+
* Computed once, cached until clearSystemPromptSections() is called.
|
|
22
|
+
*/
|
|
23
|
+
export declare function systemPromptSection(name: string, compute: ComputeFn): SystemPromptSection;
|
|
24
|
+
/**
|
|
25
|
+
* Create a volatile system prompt section that recomputes every turn.
|
|
26
|
+
* WARNING: This WILL break prompt cache when the value changes.
|
|
27
|
+
* Use sparingly — only for time-sensitive or frequently changing context.
|
|
28
|
+
*
|
|
29
|
+
* @param _reason - Why this section must be volatile (documentation only, CC parity).
|
|
30
|
+
*/
|
|
31
|
+
export declare function volatileSystemPromptSection(name: string, compute: ComputeFn, _reason?: string): SystemPromptSection;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve all system prompt sections, returning non-null prompt strings.
|
|
34
|
+
* Memoized sections are cached; volatile sections are always recomputed.
|
|
35
|
+
*/
|
|
36
|
+
export declare function resolveSystemPromptSections(sections: SystemPromptSection[]): Promise<string[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Clear all section caches. Called on session reset, /clear, or /compact.
|
|
39
|
+
*/
|
|
40
|
+
export declare function clearSystemPromptSections(): void;
|
|
41
|
+
export interface SystemPromptAssemblyOptions {
|
|
42
|
+
/** Base system prompt (from Gateway config) */
|
|
43
|
+
basePrompt?: string;
|
|
44
|
+
/** Prepended instruction block (from INSTRUCTIONS.md files) */
|
|
45
|
+
instructionBlock?: string;
|
|
46
|
+
/** User-provided custom system prompt (replaces base) */
|
|
47
|
+
customSystemPrompt?: string;
|
|
48
|
+
/** User-provided append text (appended after all sections) */
|
|
49
|
+
appendSystemPrompt?: string;
|
|
50
|
+
/** Dynamic sections to resolve */
|
|
51
|
+
sections?: SystemPromptSection[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Assemble the final system prompt from all sources.
|
|
55
|
+
*
|
|
56
|
+
* Order:
|
|
57
|
+
* 1. Instruction block (INSTRUCTIONS.md files)
|
|
58
|
+
* 2. Custom system prompt (replaces base) OR base prompt
|
|
59
|
+
* 3. Resolved sections
|
|
60
|
+
* 4. Append system prompt (user-provided suffix)
|
|
61
|
+
*/
|
|
62
|
+
export declare function assembleSystemPrompt(opts: SystemPromptAssemblyOptions): Promise<string>;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { SessionState, type SessionCostSnapshot } from "./session-state.js";
|
|
2
|
+
export { appendMessage, saveSessionState, loadSessionForResume, listSessions, deleteSession, pruneOldSessions, shouldGenerateTaskSummary, maybeGenerateTaskSummary, type SessionMetadata, type PersistedSession, type SessionListEntry, type TaskSummaryDeps, } from "./session-persistence.js";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Memory — CC SessionMemory background extraction parity.
|
|
3
|
+
*
|
|
4
|
+
* Automatically extracts key insights from the conversation and saves
|
|
5
|
+
* them to session memory files. Runs as a forked sub-agent with restricted
|
|
6
|
+
* tool access after each turn when extraction thresholds are met.
|
|
7
|
+
*
|
|
8
|
+
* CC reference:
|
|
9
|
+
* - src/services/SessionMemory/sessionMemory.ts
|
|
10
|
+
* - src/services/SessionMemory/extractMemories.ts
|
|
11
|
+
*
|
|
12
|
+
* Extraction process:
|
|
13
|
+
* 1. Check thresholds (tokens + tool calls since last extraction)
|
|
14
|
+
* 2. Fork a sub-agent with restricted tools (read-only + memory write)
|
|
15
|
+
* 3. Sub-agent reviews conversation, extracts key facts/decisions
|
|
16
|
+
* 4. Sub-agent writes to session-memory.md file
|
|
17
|
+
* 5. Reset threshold counters
|
|
18
|
+
*
|
|
19
|
+
* Tool restrictions for extractor:
|
|
20
|
+
* ✅ read, search, grep, glob (read-only fs)
|
|
21
|
+
* ✅ memory_write (to session-memory path only)
|
|
22
|
+
* ❌ Everything else (no file edits, no bash, no network)
|
|
23
|
+
*/
|
|
24
|
+
import type { ChatMessage, ToolDefinition, ToolInvoker, AgentLogger, HookRegistry } from "../../agent/types.js";
|
|
25
|
+
import type { LLMTransport } from "../../llm/transport.js";
|
|
26
|
+
import type { SessionState } from "./session-state.js";
|
|
27
|
+
/** Tool permission check function (mirrors execution/forked-agent CanUseToolFn). */
|
|
28
|
+
export type CanUseToolFn = (toolName: string, input: Record<string, unknown>) => {
|
|
29
|
+
allowed: true;
|
|
30
|
+
} | {
|
|
31
|
+
allowed: false;
|
|
32
|
+
reason: string;
|
|
33
|
+
};
|
|
34
|
+
/** Forked agent runner signature (injected by CLI layer). */
|
|
35
|
+
export type ForkedAgentRunner = (params: {
|
|
36
|
+
promptMessages: ChatMessage[];
|
|
37
|
+
systemPrompt?: string;
|
|
38
|
+
tools: ToolDefinition[];
|
|
39
|
+
canUseTool?: CanUseToolFn;
|
|
40
|
+
transport: LLMTransport;
|
|
41
|
+
toolInvoker: ToolInvoker;
|
|
42
|
+
apiKey: string;
|
|
43
|
+
model: string;
|
|
44
|
+
log: AgentLogger;
|
|
45
|
+
hooks?: HookRegistry;
|
|
46
|
+
forkLabel: string;
|
|
47
|
+
sidechainType?: "planner" | "research" | "plan-repair" | "code-repair" | "media-prep";
|
|
48
|
+
maxTurns?: number;
|
|
49
|
+
parentSignal?: AbortSignal;
|
|
50
|
+
skipTranscript?: boolean;
|
|
51
|
+
}) => Promise<{
|
|
52
|
+
ok: boolean;
|
|
53
|
+
content?: string;
|
|
54
|
+
error?: string;
|
|
55
|
+
durationMs: number;
|
|
56
|
+
totalUsage: {
|
|
57
|
+
prompt: number;
|
|
58
|
+
completion: number;
|
|
59
|
+
};
|
|
60
|
+
}>;
|
|
61
|
+
export interface SessionMemoryDeps {
|
|
62
|
+
/** Session state (for threshold tracking) */
|
|
63
|
+
sessionState: SessionState;
|
|
64
|
+
/** LLM transport for the extractor agent */
|
|
65
|
+
transport: LLMTransport;
|
|
66
|
+
/** Tool invoker */
|
|
67
|
+
toolInvoker: ToolInvoker;
|
|
68
|
+
/** Available tools (will be filtered for extraction) */
|
|
69
|
+
tools: ToolDefinition[];
|
|
70
|
+
/** API key */
|
|
71
|
+
apiKey: string;
|
|
72
|
+
/** Model for extraction (can use a cheaper/faster model) */
|
|
73
|
+
model: string;
|
|
74
|
+
/** Logger */
|
|
75
|
+
log: AgentLogger;
|
|
76
|
+
/** Hook registry */
|
|
77
|
+
hooks?: HookRegistry;
|
|
78
|
+
/** Path where session memory should be written */
|
|
79
|
+
memoryFilePath: string;
|
|
80
|
+
/** Parent abort signal */
|
|
81
|
+
parentSignal?: AbortSignal;
|
|
82
|
+
/** Forked agent runner (injected by CLI layer, decoupled from execution/) */
|
|
83
|
+
runForkedAgent: ForkedAgentRunner;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Check if memory extraction should run and execute if thresholds are met.
|
|
87
|
+
*
|
|
88
|
+
* Call this at the end of each turn (after turn.completed hook).
|
|
89
|
+
* It's a no-op if thresholds aren't met.
|
|
90
|
+
*/
|
|
91
|
+
export declare function maybeExtractSessionMemory(deps: SessionMemoryDeps, conversationMessages: ChatMessage[]): Promise<boolean>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Persistence — CC sessionRestore.ts + conversationRecovery.ts parity.
|
|
3
|
+
*
|
|
4
|
+
* Enables conversation resume by persisting:
|
|
5
|
+
* 1. Message transcript (JSONL-based, append-only)
|
|
6
|
+
* 2. Cost snapshot (atomic JSON)
|
|
7
|
+
* 3. Session metadata (model, cwd, agent mode)
|
|
8
|
+
*
|
|
9
|
+
* Storage layout:
|
|
10
|
+
* ~/.openclaw/sessions/<session-id>/
|
|
11
|
+
* transcript.jsonl — append-only message log
|
|
12
|
+
* state.json — cost snapshot + metadata
|
|
13
|
+
*
|
|
14
|
+
* Resume flow (CC processResumedConversation parity):
|
|
15
|
+
* 1. loadSessionForResume(sessionId) → messages + cost
|
|
16
|
+
* 2. Restore cost state into SessionState
|
|
17
|
+
* 3. Feed messages as initial context to agent
|
|
18
|
+
*
|
|
19
|
+
* Reference: claude-code src/utils/sessionRestore.ts
|
|
20
|
+
* claude-code src/utils/conversationRecovery.ts
|
|
21
|
+
*/
|
|
22
|
+
import type { ChatMessage } from "../../agent/types.js";
|
|
23
|
+
import type { SessionCostSnapshot } from "./session-state.js";
|
|
24
|
+
export interface SessionMetadata {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
createdAt: number;
|
|
27
|
+
lastActiveAt: number;
|
|
28
|
+
model?: string;
|
|
29
|
+
cwd?: string;
|
|
30
|
+
turnCount: number;
|
|
31
|
+
messageCount: number;
|
|
32
|
+
/** Title / description (auto-generated or user-set) */
|
|
33
|
+
title?: string;
|
|
34
|
+
/** Task summary generated by LLM (CC maybeGenerateTaskSummary parity) */
|
|
35
|
+
taskSummary?: string;
|
|
36
|
+
/** Timestamp of last task summary generation */
|
|
37
|
+
taskSummaryGeneratedAt?: number;
|
|
38
|
+
}
|
|
39
|
+
export interface PersistedSession {
|
|
40
|
+
metadata: SessionMetadata;
|
|
41
|
+
messages: ChatMessage[];
|
|
42
|
+
costSnapshot?: SessionCostSnapshot;
|
|
43
|
+
}
|
|
44
|
+
export interface SessionListEntry {
|
|
45
|
+
sessionId: string;
|
|
46
|
+
title?: string;
|
|
47
|
+
lastActiveAt: number;
|
|
48
|
+
messageCount: number;
|
|
49
|
+
model?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Append a message to the session transcript.
|
|
53
|
+
* Creates the session directory if it doesn't exist.
|
|
54
|
+
*/
|
|
55
|
+
export declare function appendMessage(sessionId: string, message: ChatMessage): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Save session state (cost + metadata) atomically.
|
|
58
|
+
*/
|
|
59
|
+
export declare function saveSessionState(sessionId: string, costSnapshot: SessionCostSnapshot, metadata: Partial<SessionMetadata>): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Load a persisted session for resume — CC loadConversationForResume parity.
|
|
62
|
+
*
|
|
63
|
+
* Returns null if the session doesn't exist or is corrupted.
|
|
64
|
+
*/
|
|
65
|
+
export declare function loadSessionForResume(sessionId: string): Promise<PersistedSession | null>;
|
|
66
|
+
/**
|
|
67
|
+
* List available sessions for resume — CC ResumeConversation picker.
|
|
68
|
+
* Returns most-recent-first, up to `limit` entries.
|
|
69
|
+
*/
|
|
70
|
+
export declare function listSessions(limit?: number): Promise<SessionListEntry[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Delete a session (cleanup).
|
|
73
|
+
*/
|
|
74
|
+
export declare function deleteSession(sessionId: string): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Prune old sessions beyond MAX_SESSIONS limit.
|
|
77
|
+
* Deletes oldest sessions first.
|
|
78
|
+
*/
|
|
79
|
+
export declare function pruneOldSessions(): Promise<number>;
|
|
80
|
+
export interface TaskSummaryDeps {
|
|
81
|
+
transport: import("../../llm/transport.js").LLMTransport;
|
|
82
|
+
apiKey: string;
|
|
83
|
+
model: string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Check if a task summary should be generated for this session.
|
|
87
|
+
* Triggers after TASK_SUMMARY_TURN_THRESHOLD turns, then every REGEN_INTERVAL turns.
|
|
88
|
+
*/
|
|
89
|
+
export declare function shouldGenerateTaskSummary(metadata: SessionMetadata): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Generate and persist a task summary for a long session.
|
|
92
|
+
* Fire-and-forget — errors are swallowed.
|
|
93
|
+
*/
|
|
94
|
+
export declare function maybeGenerateTaskSummary(sessionId: string, metadata: SessionMetadata, recentMessages: ChatMessage[], deps: TaskSummaryDeps): Promise<string | null>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session State — CC session state parity.
|
|
3
|
+
*
|
|
4
|
+
* Unified state container for a single agent session:
|
|
5
|
+
* - Token usage / cost accumulation across turns
|
|
6
|
+
* - Session trust acceptance (per-session, non-persistent)
|
|
7
|
+
* - Background memory extraction trigger thresholds
|
|
8
|
+
* - Session-level settings
|
|
9
|
+
*
|
|
10
|
+
* This module is the qlogicagent equivalent of CC's:
|
|
11
|
+
* - bootstrap/state.ts (session state fields)
|
|
12
|
+
* - cost-tracker.ts (cost accumulation + persistence)
|
|
13
|
+
* - sessionRestore.ts (cost restore guard)
|
|
14
|
+
*
|
|
15
|
+
* Reference: claude-code src/bootstrap/state.ts, src/cost-tracker.ts
|
|
16
|
+
*/
|
|
17
|
+
import type { TokenUsage } from "../../agent/types.js";
|
|
18
|
+
/** Per-model usage accumulation — CC modelUsage parity */
|
|
19
|
+
export interface ModelUsage {
|
|
20
|
+
inputTokens: number;
|
|
21
|
+
outputTokens: number;
|
|
22
|
+
cacheRead: number;
|
|
23
|
+
cacheCreation: number;
|
|
24
|
+
costUSD: number;
|
|
25
|
+
}
|
|
26
|
+
/** Snapshot of the session state for persistence / restore */
|
|
27
|
+
export interface SessionCostSnapshot {
|
|
28
|
+
sessionId: string;
|
|
29
|
+
totalCostUSD: number;
|
|
30
|
+
modelUsage: Record<string, ModelUsage>;
|
|
31
|
+
totalInputTokens: number;
|
|
32
|
+
totalOutputTokens: number;
|
|
33
|
+
turnCount: number;
|
|
34
|
+
lastSavedAt: number;
|
|
35
|
+
}
|
|
36
|
+
/** Thresholds for background memory extraction — CC sessionMemory.ts parity */
|
|
37
|
+
export interface MemoryExtractionThresholds {
|
|
38
|
+
/** Token threshold before FIRST extraction (default 10000) */
|
|
39
|
+
initialTokenThreshold: number;
|
|
40
|
+
/** Token threshold between UPDATES (default 5000) */
|
|
41
|
+
updateTokenThreshold: number;
|
|
42
|
+
/** Tool call threshold (default 3) */
|
|
43
|
+
toolCallThreshold: number;
|
|
44
|
+
}
|
|
45
|
+
export declare class SessionState {
|
|
46
|
+
readonly sessionId: string;
|
|
47
|
+
private _trustAccepted;
|
|
48
|
+
private _totalCostUSD;
|
|
49
|
+
private _modelUsage;
|
|
50
|
+
private _totalInputTokens;
|
|
51
|
+
private _totalOutputTokens;
|
|
52
|
+
private _turnCount;
|
|
53
|
+
private _toolCallCount;
|
|
54
|
+
private _tokensSinceLastExtraction;
|
|
55
|
+
private _toolCallsSinceLastExtraction;
|
|
56
|
+
private _hasExtractedOnce;
|
|
57
|
+
private _listeners;
|
|
58
|
+
/** Memory extraction thresholds — CC sessionMemory defaults */
|
|
59
|
+
readonly extractionThresholds: MemoryExtractionThresholds;
|
|
60
|
+
constructor(sessionId: string, thresholds?: Partial<MemoryExtractionThresholds>);
|
|
61
|
+
get trustAccepted(): boolean;
|
|
62
|
+
setTrustAccepted(accepted: boolean): void;
|
|
63
|
+
get totalCostUSD(): number;
|
|
64
|
+
get totalInputTokens(): number;
|
|
65
|
+
get totalOutputTokens(): number;
|
|
66
|
+
get turnCount(): number;
|
|
67
|
+
get toolCallCount(): number;
|
|
68
|
+
/** Get usage for a specific model */
|
|
69
|
+
getModelUsage(model: string): ModelUsage | undefined;
|
|
70
|
+
/** Get all model usages as a plain object */
|
|
71
|
+
getAllModelUsage(): Record<string, ModelUsage>;
|
|
72
|
+
/**
|
|
73
|
+
* Add usage from an API response — CC addToTotalSessionCost() parity.
|
|
74
|
+
*
|
|
75
|
+
* @param usage - Token counts from the API response
|
|
76
|
+
* @param model - Model identifier
|
|
77
|
+
* @param costUSD - Calculated cost (caller computes based on pricing)
|
|
78
|
+
*/
|
|
79
|
+
addUsage(usage: TokenUsage, model: string, costUSD?: number): void;
|
|
80
|
+
/** Record a tool call (for extraction threshold tracking) */
|
|
81
|
+
recordToolCall(): void;
|
|
82
|
+
/** Increment turn count */
|
|
83
|
+
recordTurnCompleted(): void;
|
|
84
|
+
/**
|
|
85
|
+
* Check whether background memory extraction should trigger.
|
|
86
|
+
* CC sessionMemory.ts shouldExtract() parity.
|
|
87
|
+
*
|
|
88
|
+
* Logic:
|
|
89
|
+
* - Token threshold is ALWAYS required
|
|
90
|
+
* - Tool call threshold is an additional gate
|
|
91
|
+
* - Token threshold: initialTokenThreshold before first extraction,
|
|
92
|
+
* updateTokenThreshold after
|
|
93
|
+
*/
|
|
94
|
+
shouldExtractMemory(): boolean;
|
|
95
|
+
/** Mark that extraction was performed — reset counters */
|
|
96
|
+
markExtractionDone(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Create a snapshot for persistence.
|
|
99
|
+
* CC saveCurrentSessionCosts() parity.
|
|
100
|
+
*/
|
|
101
|
+
createSnapshot(): SessionCostSnapshot;
|
|
102
|
+
/**
|
|
103
|
+
* Restore from a previous snapshot.
|
|
104
|
+
* CC setCostStateForRestore() parity.
|
|
105
|
+
*
|
|
106
|
+
* GUARD: Only restores if the snapshot's sessionId matches this session.
|
|
107
|
+
* Prevents cross-session cost leakage (CC's lastSessionId guard).
|
|
108
|
+
*/
|
|
109
|
+
restoreFromSnapshot(snapshot: SessionCostSnapshot): boolean;
|
|
110
|
+
/** Register a state change listener. Returns unsubscribe function. */
|
|
111
|
+
onStateChange(listener: () => void): () => void;
|
|
112
|
+
private notifyListeners;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create a new session state instance.
|
|
116
|
+
*/
|
|
117
|
+
export declare function createSessionState(sessionId: string, thresholds?: Partial<MemoryExtractionThresholds>): SessionState;
|