opencode-dux 1.0.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/LICENSE +21 -0
- package/README.md +452 -0
- package/dist/agents/descriptions.d.ts +6 -0
- package/dist/agents/designer.d.ts +2 -0
- package/dist/agents/explorer.d.ts +2 -0
- package/dist/agents/fixer.d.ts +2 -0
- package/dist/agents/index.d.ts +22 -0
- package/dist/agents/interpreter.d.ts +2 -0
- package/dist/agents/librarian.d.ts +2 -0
- package/dist/agents/oracle.d.ts +2 -0
- package/dist/agents/orchestrator.d.ts +27 -0
- package/dist/agents/overrides.d.ts +18 -0
- package/dist/agents/prompt-blocks.d.ts +97 -0
- package/dist/agents/steward.d.ts +3 -0
- package/dist/cli/config-io.d.ts +24 -0
- package/dist/cli/config-manager.d.ts +4 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +1006 -0
- package/dist/cli/install.d.ts +2 -0
- package/dist/cli/mcps.d.ts +13 -0
- package/dist/cli/model-key-normalization.d.ts +1 -0
- package/dist/cli/paths.d.ts +35 -0
- package/dist/cli/providers.d.ts +137 -0
- package/dist/cli/skills.d.ts +22 -0
- package/dist/cli/system.d.ts +5 -0
- package/dist/cli/types.d.ts +38 -0
- package/dist/config/constants.d.ts +12 -0
- package/dist/config/index.d.ts +4 -0
- package/dist/config/loader.d.ts +40 -0
- package/dist/config/runtime-preset.d.ts +12 -0
- package/dist/config/schema.d.ts +281 -0
- package/dist/config/utils.d.ts +10 -0
- package/dist/discovery/local/types.d.ts +79 -0
- package/dist/discovery/local.d.ts +73 -0
- package/dist/discovery/mcp-servers.d.ts +88 -0
- package/dist/discovery/skills.d.ts +94 -0
- package/dist/hooks/apply-patch/codec.d.ts +7 -0
- package/dist/hooks/apply-patch/errors.d.ts +25 -0
- package/dist/hooks/apply-patch/execution-context.d.ts +27 -0
- package/dist/hooks/apply-patch/index.d.ts +15 -0
- package/dist/hooks/apply-patch/matching.d.ts +26 -0
- package/dist/hooks/apply-patch/operations.d.ts +3 -0
- package/dist/hooks/apply-patch/patch.d.ts +2 -0
- package/dist/hooks/apply-patch/prepared-changes.d.ts +17 -0
- package/dist/hooks/apply-patch/resolution.d.ts +19 -0
- package/dist/hooks/apply-patch/rewrite.d.ts +7 -0
- package/dist/hooks/apply-patch/test-helpers.d.ts +6 -0
- package/dist/hooks/apply-patch/types.d.ts +80 -0
- package/dist/hooks/auto-update-checker/cache.d.ts +11 -0
- package/dist/hooks/auto-update-checker/checker.d.ts +32 -0
- package/dist/hooks/auto-update-checker/constants.d.ts +11 -0
- package/dist/hooks/auto-update-checker/index.d.ts +18 -0
- package/dist/hooks/auto-update-checker/types.d.ts +22 -0
- package/dist/hooks/chat-headers.d.ts +16 -0
- package/dist/hooks/context-pressure-reminder/index.d.ts +33 -0
- package/dist/hooks/delegate-task-retry/guidance.d.ts +2 -0
- package/dist/hooks/delegate-task-retry/hook.d.ts +8 -0
- package/dist/hooks/delegate-task-retry/index.d.ts +4 -0
- package/dist/hooks/delegate-task-retry/patterns.d.ts +11 -0
- package/dist/hooks/filter-available-skills/index.d.ts +32 -0
- package/dist/hooks/foreground-fallback/index.d.ts +72 -0
- package/dist/hooks/image-hook.d.ts +5 -0
- package/dist/hooks/index.d.ts +14 -0
- package/dist/hooks/json-error-recovery/hook.d.ts +18 -0
- package/dist/hooks/json-error-recovery/index.d.ts +1 -0
- package/dist/hooks/phase-reminder/index.d.ts +26 -0
- package/dist/hooks/post-file-tool-nudge/index.d.ts +19 -0
- package/dist/hooks/task-session-manager/index.d.ts +52 -0
- package/dist/hooks/todo-continuation/index.d.ts +53 -0
- package/dist/hooks/todo-continuation/todo-hygiene.d.ts +35 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +31782 -0
- package/dist/mcp/context7.d.ts +6 -0
- package/dist/mcp/grep-app.d.ts +6 -0
- package/dist/mcp/index.d.ts +13 -0
- package/dist/mcp/types.d.ts +12 -0
- package/dist/mcp/websearch.d.ts +9 -0
- package/dist/skills/registry.d.ts +29 -0
- package/dist/subscriptions/accounts-store.d.ts +57 -0
- package/dist/subscriptions/index.d.ts +13 -0
- package/dist/subscriptions/neuralwatt-scraper.d.ts +14 -0
- package/dist/subscriptions/opencode-go-scraper.d.ts +27 -0
- package/dist/subscriptions/types.d.ts +115 -0
- package/dist/subscriptions/usage-service.d.ts +74 -0
- package/dist/tools/ast-grep/cli.d.ts +15 -0
- package/dist/tools/ast-grep/constants.d.ts +25 -0
- package/dist/tools/ast-grep/downloader.d.ts +5 -0
- package/dist/tools/ast-grep/index.d.ts +10 -0
- package/dist/tools/ast-grep/tools.d.ts +3 -0
- package/dist/tools/ast-grep/types.d.ts +30 -0
- package/dist/tools/ast-grep/utils.d.ts +4 -0
- package/dist/tools/delegate.d.ts +14 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/preset-manager.d.ts +27 -0
- package/dist/tools/smartfetch/binary.d.ts +3 -0
- package/dist/tools/smartfetch/cache.d.ts +6 -0
- package/dist/tools/smartfetch/constants.d.ts +12 -0
- package/dist/tools/smartfetch/index.d.ts +3 -0
- package/dist/tools/smartfetch/network.d.ts +38 -0
- package/dist/tools/smartfetch/secondary-model.d.ts +28 -0
- package/dist/tools/smartfetch/tool.d.ts +3 -0
- package/dist/tools/smartfetch/types.d.ts +122 -0
- package/dist/tools/smartfetch/utils.d.ts +18 -0
- package/dist/tui-state.d.ts +168 -0
- package/dist/tui.d.ts +37 -0
- package/dist/tui.js +1896 -0
- package/dist/utils/agent-variant.d.ts +63 -0
- package/dist/utils/compat.d.ts +30 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/internal-initiator.d.ts +6 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/polling.d.ts +21 -0
- package/dist/utils/session-manager.d.ts +55 -0
- package/dist/utils/session.d.ts +90 -0
- package/dist/utils/subagent-depth.d.ts +35 -0
- package/dist/utils/system-collapse.d.ts +6 -0
- package/dist/utils/task.d.ts +4 -0
- package/dist/utils/zip-extractor.d.ts +1 -0
- package/index.ts +1 -0
- package/opencode-dux.schema.json +634 -0
- package/package.json +103 -0
- package/src/agents/descriptions.ts +55 -0
- package/src/agents/designer.test.ts +86 -0
- package/src/agents/designer.ts +154 -0
- package/src/agents/display-name.test.ts +186 -0
- package/src/agents/explorer.test.ts +79 -0
- package/src/agents/explorer.ts +144 -0
- package/src/agents/fixer.test.ts +79 -0
- package/src/agents/fixer.ts +145 -0
- package/src/agents/index.test.ts +472 -0
- package/src/agents/index.ts +248 -0
- package/src/agents/interpreter.ts +136 -0
- package/src/agents/librarian.test.ts +80 -0
- package/src/agents/librarian.ts +145 -0
- package/src/agents/oracle.test.ts +89 -0
- package/src/agents/oracle.ts +184 -0
- package/src/agents/orchestrator.test.ts +116 -0
- package/src/agents/orchestrator.ts +574 -0
- package/src/agents/overrides.ts +95 -0
- package/src/agents/prompt-blocks.test.ts +114 -0
- package/src/agents/prompt-blocks.ts +640 -0
- package/src/agents/steward.ts +146 -0
- package/src/cli/config-io.test.ts +536 -0
- package/src/cli/config-io.ts +473 -0
- package/src/cli/config-manager.test.ts +141 -0
- package/src/cli/config-manager.ts +4 -0
- package/src/cli/index.ts +88 -0
- package/src/cli/install.ts +282 -0
- package/src/cli/mcps.test.ts +62 -0
- package/src/cli/mcps.ts +39 -0
- package/src/cli/model-key-normalization.test.ts +21 -0
- package/src/cli/model-key-normalization.ts +60 -0
- package/src/cli/paths.test.ts +167 -0
- package/src/cli/paths.ts +144 -0
- package/src/cli/providers.test.ts +118 -0
- package/src/cli/providers.ts +141 -0
- package/src/cli/skills.test.ts +111 -0
- package/src/cli/skills.ts +103 -0
- package/src/cli/system.test.ts +91 -0
- package/src/cli/system.ts +180 -0
- package/src/cli/types.ts +43 -0
- package/src/config/constants.ts +58 -0
- package/src/config/index.ts +4 -0
- package/src/config/loader.test.ts +1194 -0
- package/src/config/loader.ts +269 -0
- package/src/config/model-resolution.test.ts +176 -0
- package/src/config/runtime-preset.test.ts +61 -0
- package/src/config/runtime-preset.ts +37 -0
- package/src/config/schema.ts +248 -0
- package/src/config/utils.test.ts +41 -0
- package/src/config/utils.ts +23 -0
- package/src/discovery/local/types.ts +85 -0
- package/src/discovery/local.ts +322 -0
- package/src/discovery/mcp-servers.ts +804 -0
- package/src/discovery/skills.ts +959 -0
- package/src/hooks/apply-patch/codec.test.ts +184 -0
- package/src/hooks/apply-patch/codec.ts +352 -0
- package/src/hooks/apply-patch/errors.ts +117 -0
- package/src/hooks/apply-patch/execution-context.ts +432 -0
- package/src/hooks/apply-patch/hook.test.ts +768 -0
- package/src/hooks/apply-patch/index.ts +126 -0
- package/src/hooks/apply-patch/matching.test.ts +215 -0
- package/src/hooks/apply-patch/matching.ts +586 -0
- package/src/hooks/apply-patch/operations.test.ts +1535 -0
- package/src/hooks/apply-patch/operations.ts +3 -0
- package/src/hooks/apply-patch/patch.ts +9 -0
- package/src/hooks/apply-patch/prepared-changes.ts +400 -0
- package/src/hooks/apply-patch/resolution.test.ts +420 -0
- package/src/hooks/apply-patch/resolution.ts +437 -0
- package/src/hooks/apply-patch/rewrite.ts +496 -0
- package/src/hooks/apply-patch/test-helpers.ts +52 -0
- package/src/hooks/apply-patch/types.ts +111 -0
- package/src/hooks/auto-update-checker/cache.test.ts +179 -0
- package/src/hooks/auto-update-checker/cache.ts +188 -0
- package/src/hooks/auto-update-checker/checker.test.ts +159 -0
- package/src/hooks/auto-update-checker/checker.ts +308 -0
- package/src/hooks/auto-update-checker/constants.ts +33 -0
- package/src/hooks/auto-update-checker/index.test.ts +282 -0
- package/src/hooks/auto-update-checker/index.ts +225 -0
- package/src/hooks/auto-update-checker/types.ts +26 -0
- package/src/hooks/chat-headers.test.ts +236 -0
- package/src/hooks/chat-headers.ts +97 -0
- package/src/hooks/context-pressure-reminder/index.test.ts +179 -0
- package/src/hooks/context-pressure-reminder/index.ts +137 -0
- package/src/hooks/delegate-task-retry/guidance.ts +41 -0
- package/src/hooks/delegate-task-retry/hook.ts +23 -0
- package/src/hooks/delegate-task-retry/index.test.ts +38 -0
- package/src/hooks/delegate-task-retry/index.ts +7 -0
- package/src/hooks/delegate-task-retry/patterns.ts +79 -0
- package/src/hooks/filter-available-skills/index.test.ts +297 -0
- package/src/hooks/filter-available-skills/index.ts +160 -0
- package/src/hooks/foreground-fallback/index.test.ts +624 -0
- package/src/hooks/foreground-fallback/index.ts +374 -0
- package/src/hooks/image-hook.ts +6 -0
- package/src/hooks/index.ts +17 -0
- package/src/hooks/json-error-recovery/hook.ts +73 -0
- package/src/hooks/json-error-recovery/index.test.ts +111 -0
- package/src/hooks/json-error-recovery/index.ts +6 -0
- package/src/hooks/phase-reminder/index.test.ts +74 -0
- package/src/hooks/phase-reminder/index.ts +85 -0
- package/src/hooks/post-file-tool-nudge/index.test.ts +94 -0
- package/src/hooks/post-file-tool-nudge/index.ts +63 -0
- package/src/hooks/task-session-manager/index.test.ts +833 -0
- package/src/hooks/task-session-manager/index.ts +434 -0
- package/src/hooks/todo-continuation/index.test.ts +3026 -0
- package/src/hooks/todo-continuation/index.ts +878 -0
- package/src/hooks/todo-continuation/todo-hygiene.test.ts +204 -0
- package/src/hooks/todo-continuation/todo-hygiene.ts +207 -0
- package/src/index.ts +1672 -0
- package/src/mcp/context7.ts +14 -0
- package/src/mcp/grep-app.ts +11 -0
- package/src/mcp/index.test.ts +96 -0
- package/src/mcp/index.ts +66 -0
- package/src/mcp/types.ts +16 -0
- package/src/mcp/websearch.ts +47 -0
- package/src/skills/codemap/README.md +60 -0
- package/src/skills/codemap/SKILL.md +174 -0
- package/src/skills/codemap/scripts/codemap.mjs +483 -0
- package/src/skills/codemap/scripts/codemap.test.ts +129 -0
- package/src/skills/registry.ts +218 -0
- package/src/skills/simplify/README.md +19 -0
- package/src/skills/simplify/SKILL.md +138 -0
- package/src/subscriptions/accounts-store.test.ts +236 -0
- package/src/subscriptions/accounts-store.ts +184 -0
- package/src/subscriptions/index.ts +30 -0
- package/src/subscriptions/neuralwatt-scraper.ts +108 -0
- package/src/subscriptions/opencode-go-scraper.ts +301 -0
- package/src/subscriptions/types.ts +145 -0
- package/src/subscriptions/usage-service.test.ts +202 -0
- package/src/subscriptions/usage-service.ts +651 -0
- package/src/tools/ast-grep/cli.ts +257 -0
- package/src/tools/ast-grep/constants.ts +214 -0
- package/src/tools/ast-grep/downloader.ts +131 -0
- package/src/tools/ast-grep/index.ts +24 -0
- package/src/tools/ast-grep/tools.ts +117 -0
- package/src/tools/ast-grep/types.ts +51 -0
- package/src/tools/ast-grep/utils.ts +126 -0
- package/src/tools/delegate-handoff.test.ts +18 -0
- package/src/tools/delegate.ts +508 -0
- package/src/tools/index.ts +8 -0
- package/src/tools/preset-manager.test.ts +795 -0
- package/src/tools/preset-manager.ts +332 -0
- package/src/tools/smartfetch/binary.ts +58 -0
- package/src/tools/smartfetch/cache.test.ts +34 -0
- package/src/tools/smartfetch/cache.ts +112 -0
- package/src/tools/smartfetch/constants.ts +29 -0
- package/src/tools/smartfetch/index.ts +8 -0
- package/src/tools/smartfetch/network.test.ts +178 -0
- package/src/tools/smartfetch/network.ts +614 -0
- package/src/tools/smartfetch/secondary-model.test.ts +85 -0
- package/src/tools/smartfetch/secondary-model.ts +276 -0
- package/src/tools/smartfetch/tool.test.ts +60 -0
- package/src/tools/smartfetch/tool.ts +832 -0
- package/src/tools/smartfetch/types.ts +135 -0
- package/src/tools/smartfetch/utils.test.ts +24 -0
- package/src/tools/smartfetch/utils.ts +456 -0
- package/src/tui-state.test.ts +867 -0
- package/src/tui-state.ts +1255 -0
- package/src/tui.test.ts +336 -0
- package/src/tui.ts +1539 -0
- package/src/utils/agent-variant.test.ts +244 -0
- package/src/utils/agent-variant.ts +187 -0
- package/src/utils/compat.ts +91 -0
- package/src/utils/env.ts +12 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/internal-initiator.ts +28 -0
- package/src/utils/logger.test.ts +220 -0
- package/src/utils/logger.ts +136 -0
- package/src/utils/polling.test.ts +191 -0
- package/src/utils/polling.ts +67 -0
- package/src/utils/session-manager.test.ts +173 -0
- package/src/utils/session-manager.ts +356 -0
- package/src/utils/session.test.ts +110 -0
- package/src/utils/session.ts +389 -0
- package/src/utils/subagent-depth.test.ts +170 -0
- package/src/utils/subagent-depth.ts +75 -0
- package/src/utils/system-collapse.test.ts +86 -0
- package/src/utils/system-collapse.ts +24 -0
- package/src/utils/task.test.ts +24 -0
- package/src/utils/task.ts +20 -0
- package/src/utils/zip-extractor.ts +102 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type { PluginInput } from '@opencode-ai/plugin';
|
|
3
|
+
import type { AgentName } from '../../config';
|
|
4
|
+
import {
|
|
5
|
+
type ContextFile,
|
|
6
|
+
deriveTaskSessionLabel,
|
|
7
|
+
parseTaskIdFromTaskOutput,
|
|
8
|
+
SessionManager,
|
|
9
|
+
SLIM_INTERNAL_INITIATOR_MARKER,
|
|
10
|
+
} from '../../utils';
|
|
11
|
+
|
|
12
|
+
interface TaskArgs {
|
|
13
|
+
description?: unknown;
|
|
14
|
+
prompt?: unknown;
|
|
15
|
+
subagent_type?: unknown;
|
|
16
|
+
task_id?: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PendingTaskCall {
|
|
20
|
+
callId: string;
|
|
21
|
+
parentSessionId: string;
|
|
22
|
+
agentType: AgentName;
|
|
23
|
+
label: string;
|
|
24
|
+
resumedTaskId?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const AGENT_NAME_SET = new Set<AgentName>([
|
|
28
|
+
'orchestrator',
|
|
29
|
+
'oracle',
|
|
30
|
+
'designer',
|
|
31
|
+
'explorer',
|
|
32
|
+
'librarian',
|
|
33
|
+
'fixer',
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
const MAX_PENDING_TASK_CALLS = 100;
|
|
37
|
+
|
|
38
|
+
interface PendingContextFile {
|
|
39
|
+
path: string;
|
|
40
|
+
lines: Set<number>;
|
|
41
|
+
lastReadAt: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ChatMessagePart {
|
|
45
|
+
type: string;
|
|
46
|
+
text?: string;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ChatMessage {
|
|
51
|
+
info: {
|
|
52
|
+
role: string;
|
|
53
|
+
agent?: string;
|
|
54
|
+
sessionID?: string;
|
|
55
|
+
};
|
|
56
|
+
parts: ChatMessagePart[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const RESUMABLE_SESSIONS_START = '<resumable_sessions>';
|
|
60
|
+
const RESUMABLE_SESSIONS_END = '</resumable_sessions>';
|
|
61
|
+
|
|
62
|
+
function isAgentName(value: unknown): value is AgentName {
|
|
63
|
+
return typeof value === 'string' && AGENT_NAME_SET.has(value as AgentName);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isObjectRecord(value: unknown): value is Record<string, unknown> {
|
|
67
|
+
return typeof value === 'object' && value !== null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function extractPath(output: string): string | undefined {
|
|
71
|
+
return /<path>([^<]+)<\/path>/.exec(output)?.[1];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function normalizePath(root: string, file: string): string {
|
|
75
|
+
const relative = path.relative(root, file);
|
|
76
|
+
let display: string;
|
|
77
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
|
|
78
|
+
display = file;
|
|
79
|
+
} else {
|
|
80
|
+
display = relative;
|
|
81
|
+
}
|
|
82
|
+
return display.split(path.sep).join('/');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function extractReadFiles(
|
|
86
|
+
root: string,
|
|
87
|
+
output: { output: unknown; metadata?: unknown },
|
|
88
|
+
): ContextFile[] {
|
|
89
|
+
if (typeof output.output !== 'string') return [];
|
|
90
|
+
|
|
91
|
+
const file = extractPath(output.output);
|
|
92
|
+
if (!file) return [];
|
|
93
|
+
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
path: normalizePath(root, file),
|
|
97
|
+
lineCount: countReadLines(output.output).length,
|
|
98
|
+
lineNumbers: countReadLines(output.output),
|
|
99
|
+
lastReadAt: Date.now(),
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function countReadLines(output: string): number[] {
|
|
105
|
+
const lines = new Set<number>();
|
|
106
|
+
for (const match of output.matchAll(/^([0-9]+):/gm)) {
|
|
107
|
+
lines.add(Number(match[1]));
|
|
108
|
+
}
|
|
109
|
+
return [...lines];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function createTaskSessionManagerHook(
|
|
113
|
+
_ctx: PluginInput,
|
|
114
|
+
options: {
|
|
115
|
+
maxSessionsPerAgent: number;
|
|
116
|
+
readContextMinLines?: number;
|
|
117
|
+
readContextMaxFiles?: number;
|
|
118
|
+
shouldManageSession: (sessionID: string) => boolean;
|
|
119
|
+
},
|
|
120
|
+
) {
|
|
121
|
+
const sessionManager = new SessionManager(options.maxSessionsPerAgent, {
|
|
122
|
+
readContextMinLines: options.readContextMinLines,
|
|
123
|
+
readContextMaxFiles: options.readContextMaxFiles,
|
|
124
|
+
});
|
|
125
|
+
const pendingCalls = new Map<string, PendingTaskCall>();
|
|
126
|
+
const pendingCallOrder: string[] = [];
|
|
127
|
+
const contextByTask = new Map<string, Map<string, PendingContextFile>>();
|
|
128
|
+
const pendingManagedTaskIds = new Set<string>();
|
|
129
|
+
let anonymousPendingCallId = 0;
|
|
130
|
+
|
|
131
|
+
function addTaskContext(taskId: string, files: ContextFile[]): void {
|
|
132
|
+
if (files.length === 0) return;
|
|
133
|
+
|
|
134
|
+
let context = contextByTask.get(taskId);
|
|
135
|
+
if (!context) {
|
|
136
|
+
context = new Map();
|
|
137
|
+
contextByTask.set(taskId, context);
|
|
138
|
+
}
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const pending = context.get(file.path) ?? {
|
|
141
|
+
path: file.path,
|
|
142
|
+
lines: new Set<number>(),
|
|
143
|
+
lastReadAt: file.lastReadAt,
|
|
144
|
+
};
|
|
145
|
+
for (const line of file.lineNumbers ?? []) {
|
|
146
|
+
pending.lines.add(line);
|
|
147
|
+
}
|
|
148
|
+
pending.lastReadAt = Math.max(pending.lastReadAt, file.lastReadAt);
|
|
149
|
+
context.set(file.path, pending);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
sessionManager.addContext(taskId, contextFilesForPrompt(context));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function contextFilesForPrompt(
|
|
156
|
+
context: Map<string, PendingContextFile> | undefined,
|
|
157
|
+
): ContextFile[] {
|
|
158
|
+
if (!context) return [];
|
|
159
|
+
return [...context.values()].map((file) => ({
|
|
160
|
+
path: file.path,
|
|
161
|
+
lineCount: file.lines.size,
|
|
162
|
+
lastReadAt: file.lastReadAt,
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function canTrackTaskContext(taskId: string): boolean {
|
|
167
|
+
return (
|
|
168
|
+
pendingManagedTaskIds.has(taskId) || sessionManager.taskIds().has(taskId)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function pruneContext(): void {
|
|
173
|
+
const remembered = sessionManager.taskIds();
|
|
174
|
+
for (const taskId of contextByTask.keys()) {
|
|
175
|
+
if (!pendingManagedTaskIds.has(taskId) && !remembered.has(taskId)) {
|
|
176
|
+
contextByTask.delete(taskId);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isMissingRememberedSessionError(output: string): boolean {
|
|
182
|
+
const firstLine = output.split(/\r?\n/, 1)[0]?.trim().toLowerCase() ?? '';
|
|
183
|
+
return (
|
|
184
|
+
firstLine.startsWith('[error]') &&
|
|
185
|
+
firstLine.includes('session') &&
|
|
186
|
+
(firstLine.includes('not found') || firstLine.includes('no session'))
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function pendingCallId(input: {
|
|
191
|
+
callID?: string;
|
|
192
|
+
sessionID?: string;
|
|
193
|
+
}): string {
|
|
194
|
+
return (
|
|
195
|
+
input.callID ??
|
|
196
|
+
`${input.sessionID ?? 'unknown'}:anonymous-${++anonymousPendingCallId}`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function rememberPendingCall(call: PendingTaskCall): void {
|
|
201
|
+
const existingIndex = pendingCallOrder.indexOf(call.callId);
|
|
202
|
+
if (existingIndex >= 0) {
|
|
203
|
+
pendingCallOrder.splice(existingIndex, 1);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
pendingCalls.set(call.callId, call);
|
|
207
|
+
pendingCallOrder.push(call.callId);
|
|
208
|
+
|
|
209
|
+
while (pendingCallOrder.length > MAX_PENDING_TASK_CALLS) {
|
|
210
|
+
const evictedCallId = pendingCallOrder.shift();
|
|
211
|
+
if (!evictedCallId) {
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
pendingCalls.delete(evictedCallId);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function takePendingCall(
|
|
219
|
+
callId?: string,
|
|
220
|
+
parentSessionId?: string,
|
|
221
|
+
): PendingTaskCall | undefined {
|
|
222
|
+
const resolvedCallId = callId ?? firstPendingCallForParent(parentSessionId);
|
|
223
|
+
if (!resolvedCallId) return undefined;
|
|
224
|
+
|
|
225
|
+
const pending = pendingCalls.get(resolvedCallId);
|
|
226
|
+
pendingCalls.delete(resolvedCallId);
|
|
227
|
+
|
|
228
|
+
const orderIndex = pendingCallOrder.indexOf(resolvedCallId);
|
|
229
|
+
if (orderIndex >= 0) {
|
|
230
|
+
pendingCallOrder.splice(orderIndex, 1);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return pending;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function firstPendingCallForParent(
|
|
237
|
+
parentSessionId?: string,
|
|
238
|
+
): string | undefined {
|
|
239
|
+
if (!parentSessionId) return undefined;
|
|
240
|
+
return pendingCallOrder.find(
|
|
241
|
+
(callId) => pendingCalls.get(callId)?.parentSessionId === parentSessionId,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
'tool.execute.before': async (
|
|
247
|
+
input: { tool: string; sessionID?: string; callID?: string },
|
|
248
|
+
output: { args?: unknown },
|
|
249
|
+
): Promise<void> => {
|
|
250
|
+
if (input.tool.toLowerCase() !== 'task') return;
|
|
251
|
+
if (!input.sessionID || !options.shouldManageSession(input.sessionID)) {
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (!isObjectRecord(output.args)) return;
|
|
255
|
+
|
|
256
|
+
const args = output.args as TaskArgs;
|
|
257
|
+
if (!isAgentName(args.subagent_type)) return;
|
|
258
|
+
|
|
259
|
+
const label = deriveTaskSessionLabel({
|
|
260
|
+
description:
|
|
261
|
+
typeof args.description === 'string' ? args.description : undefined,
|
|
262
|
+
prompt: typeof args.prompt === 'string' ? args.prompt : undefined,
|
|
263
|
+
agentType: args.subagent_type,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const pendingCall: PendingTaskCall = {
|
|
267
|
+
callId: pendingCallId({
|
|
268
|
+
callID: input.callID,
|
|
269
|
+
sessionID: input.sessionID,
|
|
270
|
+
}),
|
|
271
|
+
parentSessionId: input.sessionID,
|
|
272
|
+
agentType: args.subagent_type,
|
|
273
|
+
label,
|
|
274
|
+
};
|
|
275
|
+
rememberPendingCall(pendingCall);
|
|
276
|
+
|
|
277
|
+
if (typeof args.task_id !== 'string' || args.task_id.trim() === '') {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const requested = args.task_id.trim();
|
|
282
|
+
const remembered = sessionManager.resolve(
|
|
283
|
+
input.sessionID,
|
|
284
|
+
args.subagent_type,
|
|
285
|
+
requested,
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
if (!remembered) {
|
|
289
|
+
delete args.task_id;
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
args.task_id = remembered.taskId;
|
|
294
|
+
pendingManagedTaskIds.add(remembered.taskId);
|
|
295
|
+
sessionManager.markUsed(
|
|
296
|
+
input.sessionID,
|
|
297
|
+
args.subagent_type,
|
|
298
|
+
remembered.taskId,
|
|
299
|
+
);
|
|
300
|
+
pendingCall.resumedTaskId = remembered.taskId;
|
|
301
|
+
rememberPendingCall(pendingCall);
|
|
302
|
+
},
|
|
303
|
+
|
|
304
|
+
'tool.execute.after': async (
|
|
305
|
+
input: { tool: string; sessionID?: string; callID?: string },
|
|
306
|
+
output: { output: unknown; metadata?: unknown },
|
|
307
|
+
): Promise<void> => {
|
|
308
|
+
if (input.tool.toLowerCase() === 'read') {
|
|
309
|
+
if (input.sessionID && canTrackTaskContext(input.sessionID)) {
|
|
310
|
+
addTaskContext(
|
|
311
|
+
input.sessionID,
|
|
312
|
+
extractReadFiles(_ctx.directory, output),
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (input.tool.toLowerCase() !== 'task') return;
|
|
319
|
+
|
|
320
|
+
const pending = takePendingCall(input.callID, input.sessionID);
|
|
321
|
+
|
|
322
|
+
if (!pending || typeof output.output !== 'string') return;
|
|
323
|
+
const taskId = parseTaskIdFromTaskOutput(output.output);
|
|
324
|
+
if (!taskId) {
|
|
325
|
+
if (
|
|
326
|
+
pending.resumedTaskId &&
|
|
327
|
+
isMissingRememberedSessionError(output.output)
|
|
328
|
+
) {
|
|
329
|
+
sessionManager.drop(
|
|
330
|
+
pending.parentSessionId,
|
|
331
|
+
pending.agentType,
|
|
332
|
+
pending.resumedTaskId,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (pending.resumedTaskId && pending.resumedTaskId !== taskId) {
|
|
339
|
+
sessionManager.drop(
|
|
340
|
+
pending.parentSessionId,
|
|
341
|
+
pending.agentType,
|
|
342
|
+
pending.resumedTaskId,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
sessionManager.remember({
|
|
347
|
+
parentSessionId: pending.parentSessionId,
|
|
348
|
+
taskId,
|
|
349
|
+
agentType: pending.agentType,
|
|
350
|
+
label: pending.label,
|
|
351
|
+
});
|
|
352
|
+
pendingManagedTaskIds.delete(taskId);
|
|
353
|
+
const contextFiles = contextFilesForPrompt(contextByTask.get(taskId));
|
|
354
|
+
sessionManager.addContext(taskId, contextFiles);
|
|
355
|
+
pruneContext();
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
'experimental.chat.messages.transform': async (
|
|
359
|
+
_input: Record<string, never>,
|
|
360
|
+
output: { messages: ChatMessage[] },
|
|
361
|
+
): Promise<void> => {
|
|
362
|
+
for (let i = output.messages.length - 1; i >= 0; i -= 1) {
|
|
363
|
+
const message = output.messages[i];
|
|
364
|
+
if (message.info.role !== 'user') continue;
|
|
365
|
+
if (message.info.agent && message.info.agent !== 'orchestrator') return;
|
|
366
|
+
if (
|
|
367
|
+
!message.info.sessionID ||
|
|
368
|
+
!options.shouldManageSession(message.info.sessionID)
|
|
369
|
+
) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const reminder = sessionManager.formatForPrompt(message.info.sessionID);
|
|
374
|
+
if (!reminder) return;
|
|
375
|
+
|
|
376
|
+
const textPart = message.parts.find(
|
|
377
|
+
(part) => part.type === 'text' && typeof part.text === 'string',
|
|
378
|
+
);
|
|
379
|
+
if (!textPart) return;
|
|
380
|
+
if (textPart.text?.includes(SLIM_INTERNAL_INITIATOR_MARKER)) return;
|
|
381
|
+
if (textPart.text?.includes(RESUMABLE_SESSIONS_START)) return;
|
|
382
|
+
|
|
383
|
+
textPart.text = [
|
|
384
|
+
textPart.text ?? '',
|
|
385
|
+
'',
|
|
386
|
+
RESUMABLE_SESSIONS_START,
|
|
387
|
+
reminder,
|
|
388
|
+
RESUMABLE_SESSIONS_END,
|
|
389
|
+
].join('\n');
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
|
|
394
|
+
event: async (input: {
|
|
395
|
+
event: {
|
|
396
|
+
type: string;
|
|
397
|
+
properties?: {
|
|
398
|
+
info?: { id?: string; parentID?: string };
|
|
399
|
+
sessionID?: string;
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
}): Promise<void> => {
|
|
403
|
+
if (input.event.type === 'session.created') {
|
|
404
|
+
const info = input.event.properties?.info;
|
|
405
|
+
if (
|
|
406
|
+
info?.id &&
|
|
407
|
+
info.parentID &&
|
|
408
|
+
options.shouldManageSession(info.parentID)
|
|
409
|
+
) {
|
|
410
|
+
pendingManagedTaskIds.add(info.id);
|
|
411
|
+
}
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (input.event.type !== 'session.deleted') return;
|
|
416
|
+
const sessionId =
|
|
417
|
+
input.event.properties?.info?.id ?? input.event.properties?.sessionID;
|
|
418
|
+
if (!sessionId) return;
|
|
419
|
+
|
|
420
|
+
sessionManager.dropTask(sessionId);
|
|
421
|
+
sessionManager.clearParent(sessionId);
|
|
422
|
+
contextByTask.delete(sessionId);
|
|
423
|
+
pendingManagedTaskIds.delete(sessionId);
|
|
424
|
+
pruneContext();
|
|
425
|
+
|
|
426
|
+
for (const [callId, pending] of pendingCalls.entries()) {
|
|
427
|
+
if (pending.parentSessionId !== sessionId) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
takePendingCall(callId);
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
}
|