@doingdev/opencode-claude-manager-plugin 0.1.35 → 0.1.43
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/dist/claude/claude-agent-sdk-adapter.js +1 -0
- package/dist/manager/git-operations.d.ts +10 -1
- package/dist/manager/git-operations.js +18 -3
- package/dist/manager/persistent-manager.d.ts +19 -3
- package/dist/manager/persistent-manager.js +21 -9
- package/dist/manager/session-controller.d.ts +8 -5
- package/dist/manager/session-controller.js +25 -20
- package/dist/metadata/claude-metadata.service.d.ts +12 -0
- package/dist/metadata/claude-metadata.service.js +38 -0
- package/dist/metadata/repo-claude-config-reader.d.ts +7 -0
- package/dist/metadata/repo-claude-config-reader.js +154 -0
- package/dist/plugin/agent-hierarchy.d.ts +9 -9
- package/dist/plugin/agent-hierarchy.js +25 -25
- package/dist/plugin/claude-manager.plugin.js +83 -46
- package/dist/plugin/orchestrator.plugin.d.ts +2 -0
- package/dist/plugin/orchestrator.plugin.js +116 -0
- package/dist/plugin/service-factory.js +3 -8
- package/dist/prompts/registry.js +100 -103
- package/dist/providers/claude-code-wrapper.d.ts +13 -0
- package/dist/providers/claude-code-wrapper.js +13 -0
- package/dist/safety/bash-safety.d.ts +21 -0
- package/dist/safety/bash-safety.js +62 -0
- package/dist/src/claude/claude-agent-sdk-adapter.d.ts +27 -0
- package/dist/src/claude/claude-agent-sdk-adapter.js +517 -0
- package/dist/src/claude/claude-session.service.d.ts +10 -0
- package/dist/src/claude/claude-session.service.js +18 -0
- package/dist/src/claude/session-live-tailer.d.ts +51 -0
- package/dist/src/claude/session-live-tailer.js +269 -0
- package/dist/src/claude/tool-approval-manager.d.ts +27 -0
- package/dist/src/claude/tool-approval-manager.js +232 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +4 -0
- package/dist/src/manager/context-tracker.d.ts +33 -0
- package/dist/src/manager/context-tracker.js +106 -0
- package/dist/src/manager/git-operations.d.ts +12 -0
- package/dist/src/manager/git-operations.js +76 -0
- package/dist/src/manager/persistent-manager.d.ts +77 -0
- package/dist/src/manager/persistent-manager.js +170 -0
- package/dist/src/manager/session-controller.d.ts +44 -0
- package/dist/src/manager/session-controller.js +147 -0
- package/dist/src/plugin/agent-hierarchy.d.ts +60 -0
- package/dist/src/plugin/agent-hierarchy.js +157 -0
- package/dist/src/plugin/claude-manager.plugin.d.ts +2 -0
- package/dist/src/plugin/claude-manager.plugin.js +563 -0
- package/dist/src/plugin/service-factory.d.ts +12 -0
- package/dist/src/plugin/service-factory.js +38 -0
- package/dist/src/prompts/registry.d.ts +11 -0
- package/dist/src/prompts/registry.js +260 -0
- package/dist/src/state/file-run-state-store.d.ts +14 -0
- package/dist/src/state/file-run-state-store.js +85 -0
- package/dist/src/state/transcript-store.d.ts +15 -0
- package/dist/src/state/transcript-store.js +44 -0
- package/dist/src/types/contracts.d.ts +200 -0
- package/dist/src/types/contracts.js +1 -0
- package/dist/src/util/fs-helpers.d.ts +2 -0
- package/dist/src/util/fs-helpers.js +10 -0
- package/dist/src/util/project-context.d.ts +10 -0
- package/dist/src/util/project-context.js +105 -0
- package/dist/src/util/transcript-append.d.ts +7 -0
- package/dist/src/util/transcript-append.js +29 -0
- package/dist/test/claude-agent-sdk-adapter.test.d.ts +1 -0
- package/dist/test/claude-agent-sdk-adapter.test.js +459 -0
- package/dist/test/claude-manager.plugin.test.d.ts +1 -0
- package/dist/test/claude-manager.plugin.test.js +331 -0
- package/dist/test/context-tracker.test.d.ts +1 -0
- package/dist/test/context-tracker.test.js +138 -0
- package/dist/test/file-run-state-store.test.d.ts +1 -0
- package/dist/test/file-run-state-store.test.js +82 -0
- package/dist/test/git-operations.test.d.ts +1 -0
- package/dist/test/git-operations.test.js +90 -0
- package/dist/test/persistent-manager.test.d.ts +1 -0
- package/dist/test/persistent-manager.test.js +208 -0
- package/dist/test/project-context.test.d.ts +1 -0
- package/dist/test/project-context.test.js +92 -0
- package/dist/test/prompt-registry.test.d.ts +1 -0
- package/dist/test/prompt-registry.test.js +256 -0
- package/dist/test/session-controller.test.d.ts +1 -0
- package/dist/test/session-controller.test.js +149 -0
- package/dist/test/session-live-tailer.test.d.ts +1 -0
- package/dist/test/session-live-tailer.test.js +313 -0
- package/dist/test/tool-approval-manager.test.d.ts +1 -0
- package/dist/test/tool-approval-manager.test.js +264 -0
- package/dist/test/transcript-append.test.d.ts +1 -0
- package/dist/test/transcript-append.test.js +37 -0
- package/dist/test/transcript-store.test.d.ts +1 -0
- package/dist/test/transcript-store.test.js +50 -0
- package/dist/types/contracts.d.ts +3 -4
- package/dist/vitest.config.d.ts +2 -0
- package/dist/vitest.config.js +11 -0
- package/package.json +2 -2
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import { getSessionMessages, listSessions, query, } from '@anthropic-ai/claude-agent-sdk';
|
|
2
|
+
import { appendTranscriptEvents } from '../util/transcript-append.js';
|
|
3
|
+
const defaultFacade = {
|
|
4
|
+
query,
|
|
5
|
+
listSessions,
|
|
6
|
+
getSessionMessages,
|
|
7
|
+
};
|
|
8
|
+
const TOOL_INPUT_PREVIEW_MAX = 2000;
|
|
9
|
+
const USER_MESSAGE_MAX = 4000;
|
|
10
|
+
/**
|
|
11
|
+
* Ensures the Claude Agent SDK Skill tool is pre-approved per
|
|
12
|
+
* https://docs.anthropic.com/en/docs/claude-code/sdk/skills
|
|
13
|
+
* Subagents inherit the parent session tool surface unless given a narrower `tools` list.
|
|
14
|
+
*/
|
|
15
|
+
function mergeSkillIntoAllowedTools(allowedTools, disallowedTools) {
|
|
16
|
+
if (disallowedTools?.includes('Skill')) {
|
|
17
|
+
return allowedTools;
|
|
18
|
+
}
|
|
19
|
+
if (allowedTools === undefined) {
|
|
20
|
+
return ['Skill'];
|
|
21
|
+
}
|
|
22
|
+
return allowedTools.includes('Skill') ? allowedTools : [...allowedTools, 'Skill'];
|
|
23
|
+
}
|
|
24
|
+
export class ClaudeAgentSdkAdapter {
|
|
25
|
+
sdkFacade;
|
|
26
|
+
approvalManager;
|
|
27
|
+
constructor(sdkFacade = defaultFacade, approvalManager) {
|
|
28
|
+
this.sdkFacade = sdkFacade;
|
|
29
|
+
this.approvalManager = approvalManager;
|
|
30
|
+
}
|
|
31
|
+
async runSession(input, onEvent) {
|
|
32
|
+
const options = this.buildOptions(input);
|
|
33
|
+
const includePartials = options.includePartialMessages === true;
|
|
34
|
+
const abortController = new AbortController();
|
|
35
|
+
options.abortController = abortController;
|
|
36
|
+
const externalSignal = input.abortSignal;
|
|
37
|
+
let onAbort;
|
|
38
|
+
if (externalSignal) {
|
|
39
|
+
if (externalSignal.aborted) {
|
|
40
|
+
abortController.abort();
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
onAbort = () => abortController.abort();
|
|
44
|
+
externalSignal.addEventListener('abort', onAbort, { once: true });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const sessionQuery = this.sdkFacade.query({
|
|
48
|
+
prompt: input.prompt,
|
|
49
|
+
options,
|
|
50
|
+
});
|
|
51
|
+
let events = [];
|
|
52
|
+
let finalText = '';
|
|
53
|
+
let sessionId;
|
|
54
|
+
let turns;
|
|
55
|
+
let totalCostUsd;
|
|
56
|
+
let inputTokens;
|
|
57
|
+
let outputTokens;
|
|
58
|
+
let contextWindowSize;
|
|
59
|
+
try {
|
|
60
|
+
for await (const message of sessionQuery) {
|
|
61
|
+
const batch = normalizeSdkMessages(message, includePartials);
|
|
62
|
+
for (const event of batch) {
|
|
63
|
+
sessionId ??= event.sessionId;
|
|
64
|
+
if (event.type === 'result') {
|
|
65
|
+
finalText = event.text;
|
|
66
|
+
turns = event.turns;
|
|
67
|
+
totalCostUsd = event.totalCostUsd;
|
|
68
|
+
}
|
|
69
|
+
events = appendTranscriptEvents(events, [event]);
|
|
70
|
+
if (onEvent) {
|
|
71
|
+
await onEvent(event);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Extract usage data from result messages
|
|
75
|
+
if (message.type === 'result') {
|
|
76
|
+
const usage = extractUsageFromResult(message);
|
|
77
|
+
if (usage.inputTokens !== undefined) {
|
|
78
|
+
inputTokens = usage.inputTokens;
|
|
79
|
+
}
|
|
80
|
+
if (usage.outputTokens !== undefined) {
|
|
81
|
+
outputTokens = usage.outputTokens;
|
|
82
|
+
}
|
|
83
|
+
if (usage.contextWindowSize !== undefined) {
|
|
84
|
+
contextWindowSize = usage.contextWindowSize;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
if (onAbort && externalSignal) {
|
|
91
|
+
externalSignal.removeEventListener('abort', onAbort);
|
|
92
|
+
}
|
|
93
|
+
sessionQuery.close();
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
sessionId,
|
|
97
|
+
events,
|
|
98
|
+
finalText,
|
|
99
|
+
turns,
|
|
100
|
+
totalCostUsd,
|
|
101
|
+
inputTokens,
|
|
102
|
+
outputTokens,
|
|
103
|
+
contextWindowSize,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async listSavedSessions(cwd) {
|
|
107
|
+
const sessions = await this.sdkFacade.listSessions(cwd ? { dir: cwd } : undefined);
|
|
108
|
+
return sessions.map((session) => ({
|
|
109
|
+
sessionId: session.sessionId,
|
|
110
|
+
summary: session.summary,
|
|
111
|
+
cwd: session.cwd,
|
|
112
|
+
gitBranch: session.gitBranch,
|
|
113
|
+
createdAt: session.createdAt,
|
|
114
|
+
lastModified: session.lastModified,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
async getTranscript(sessionId, cwd) {
|
|
118
|
+
const messages = await this.sdkFacade.getSessionMessages(sessionId, cwd ? { dir: cwd } : undefined);
|
|
119
|
+
return messages.map((message) => ({
|
|
120
|
+
role: message.type,
|
|
121
|
+
sessionId: message.session_id,
|
|
122
|
+
messageId: message.uuid,
|
|
123
|
+
text: extractText(message.message),
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
async probeCapabilities(cwd, settingSources = ['project']) {
|
|
127
|
+
const sessionQuery = this.sdkFacade.query({
|
|
128
|
+
prompt: 'Reply with OK.',
|
|
129
|
+
options: {
|
|
130
|
+
cwd,
|
|
131
|
+
maxTurns: 1,
|
|
132
|
+
permissionMode: 'plan',
|
|
133
|
+
persistSession: false,
|
|
134
|
+
tools: [],
|
|
135
|
+
settingSources,
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
const [commands, agents, models] = await Promise.all([
|
|
140
|
+
sessionQuery.supportedCommands(),
|
|
141
|
+
sessionQuery.supportedAgents(),
|
|
142
|
+
sessionQuery.supportedModels(),
|
|
143
|
+
]);
|
|
144
|
+
return {
|
|
145
|
+
commands: commands.map(mapSlashCommand),
|
|
146
|
+
agents: agents.map(mapAgent),
|
|
147
|
+
models: models.map((model) => model.value),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
sessionQuery.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
buildOptions(input) {
|
|
155
|
+
const options = {
|
|
156
|
+
cwd: input.cwd,
|
|
157
|
+
tools: { type: 'preset', preset: 'claude_code' },
|
|
158
|
+
allowedTools: mergeSkillIntoAllowedTools(input.allowedTools, input.disallowedTools),
|
|
159
|
+
disallowedTools: input.disallowedTools,
|
|
160
|
+
continue: input.continueSession,
|
|
161
|
+
resume: input.resumeSessionId,
|
|
162
|
+
forkSession: input.forkSession,
|
|
163
|
+
persistSession: input.persistSession,
|
|
164
|
+
includePartialMessages: input.includePartialMessages,
|
|
165
|
+
settingSources: input.settingSources,
|
|
166
|
+
maxTurns: input.maxTurns,
|
|
167
|
+
model: input.model,
|
|
168
|
+
effort: input.effort,
|
|
169
|
+
permissionMode: input.permissionMode ?? 'acceptEdits',
|
|
170
|
+
systemPrompt: input.systemPrompt
|
|
171
|
+
? { type: 'preset', preset: 'claude_code', append: input.systemPrompt }
|
|
172
|
+
: { type: 'preset', preset: 'claude_code' },
|
|
173
|
+
env: {
|
|
174
|
+
...process.env,
|
|
175
|
+
CLAUDE_AGENT_SDK_CLIENT_APP: 'opencode-claude-manager-plugin/0.1.0',
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
if (!input.resumeSessionId) {
|
|
179
|
+
delete options.resume;
|
|
180
|
+
}
|
|
181
|
+
if (this.approvalManager) {
|
|
182
|
+
const manager = this.approvalManager;
|
|
183
|
+
options.canUseTool = async (toolName, toolInput, opts) => {
|
|
184
|
+
return manager.evaluate(toolName, toolInput, {
|
|
185
|
+
title: opts.title,
|
|
186
|
+
agentID: opts.agentID,
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return options;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function normalizeSdkMessages(message, includePartials) {
|
|
194
|
+
const sessionId = 'session_id' in message ? message.session_id : undefined;
|
|
195
|
+
if (message.type === 'assistant') {
|
|
196
|
+
return normalizeAssistantSdkMessage(message, sessionId);
|
|
197
|
+
}
|
|
198
|
+
if (message.type === 'stream_event') {
|
|
199
|
+
if (!includePartials) {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
const partialText = extractPartialEventText(message.event);
|
|
203
|
+
if (!partialText) {
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
return [
|
|
207
|
+
{
|
|
208
|
+
type: 'partial',
|
|
209
|
+
sessionId,
|
|
210
|
+
text: partialText,
|
|
211
|
+
rawType: message.type,
|
|
212
|
+
},
|
|
213
|
+
];
|
|
214
|
+
}
|
|
215
|
+
if (message.type === 'result') {
|
|
216
|
+
return [
|
|
217
|
+
{
|
|
218
|
+
type: message.is_error ? 'error' : 'result',
|
|
219
|
+
sessionId,
|
|
220
|
+
text: message.subtype === 'success'
|
|
221
|
+
? message.result
|
|
222
|
+
: message.errors.join('\n') || message.subtype,
|
|
223
|
+
turns: message.num_turns,
|
|
224
|
+
totalCostUsd: message.total_cost_usd,
|
|
225
|
+
rawType: `${message.type}:${message.subtype}`,
|
|
226
|
+
},
|
|
227
|
+
];
|
|
228
|
+
}
|
|
229
|
+
if (message.type === 'system') {
|
|
230
|
+
return [
|
|
231
|
+
{
|
|
232
|
+
type: message.subtype === 'init' ? 'init' : 'system',
|
|
233
|
+
sessionId,
|
|
234
|
+
text: message.subtype,
|
|
235
|
+
rawType: `${message.type}:${message.subtype}`,
|
|
236
|
+
},
|
|
237
|
+
];
|
|
238
|
+
}
|
|
239
|
+
if (message.type === 'auth_status') {
|
|
240
|
+
return [
|
|
241
|
+
{
|
|
242
|
+
type: 'system',
|
|
243
|
+
sessionId,
|
|
244
|
+
text: message.output.join('\n') || 'auth_status',
|
|
245
|
+
rawType: message.type,
|
|
246
|
+
},
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
if (message.type === 'prompt_suggestion') {
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
type: 'system',
|
|
253
|
+
sessionId,
|
|
254
|
+
text: message.suggestion,
|
|
255
|
+
rawType: message.type,
|
|
256
|
+
},
|
|
257
|
+
];
|
|
258
|
+
}
|
|
259
|
+
if (message.type === 'user') {
|
|
260
|
+
return normalizeUserSdkMessage(message, sessionId);
|
|
261
|
+
}
|
|
262
|
+
if (message.type === 'tool_progress') {
|
|
263
|
+
const toolName = 'tool_name' in message && typeof message.tool_name === 'string' ? message.tool_name : 'tool';
|
|
264
|
+
const elapsed = 'elapsed_time_seconds' in message && typeof message.elapsed_time_seconds === 'number'
|
|
265
|
+
? message.elapsed_time_seconds
|
|
266
|
+
: 0;
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
type: 'tool_progress',
|
|
270
|
+
sessionId,
|
|
271
|
+
text: JSON.stringify({ name: toolName, elapsed }),
|
|
272
|
+
rawType: message.type,
|
|
273
|
+
},
|
|
274
|
+
];
|
|
275
|
+
}
|
|
276
|
+
if (message.type === 'tool_use_summary') {
|
|
277
|
+
const summary = 'summary' in message && typeof message.summary === 'string' ? message.summary : '';
|
|
278
|
+
return [
|
|
279
|
+
{
|
|
280
|
+
type: 'tool_summary',
|
|
281
|
+
sessionId,
|
|
282
|
+
text: summary,
|
|
283
|
+
rawType: message.type,
|
|
284
|
+
},
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
return [
|
|
288
|
+
{
|
|
289
|
+
type: 'system',
|
|
290
|
+
sessionId,
|
|
291
|
+
text: message.type,
|
|
292
|
+
rawType: message.type,
|
|
293
|
+
},
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
function normalizeAssistantSdkMessage(message, sessionId) {
|
|
297
|
+
const raw = message.message;
|
|
298
|
+
if (!isRecord(raw) || !Array.isArray(raw.content)) {
|
|
299
|
+
const text = extractText(raw);
|
|
300
|
+
return text.trim()
|
|
301
|
+
? [
|
|
302
|
+
{
|
|
303
|
+
type: 'assistant',
|
|
304
|
+
sessionId,
|
|
305
|
+
text,
|
|
306
|
+
rawType: 'assistant',
|
|
307
|
+
},
|
|
308
|
+
]
|
|
309
|
+
: [];
|
|
310
|
+
}
|
|
311
|
+
const out = [];
|
|
312
|
+
let textBuffer = '';
|
|
313
|
+
const flushText = () => {
|
|
314
|
+
if (textBuffer.trim()) {
|
|
315
|
+
out.push({
|
|
316
|
+
type: 'assistant',
|
|
317
|
+
sessionId,
|
|
318
|
+
text: textBuffer,
|
|
319
|
+
rawType: 'assistant',
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
textBuffer = '';
|
|
323
|
+
};
|
|
324
|
+
for (const block of raw.content) {
|
|
325
|
+
if (!isRecord(block)) {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
329
|
+
textBuffer += block.text;
|
|
330
|
+
}
|
|
331
|
+
else if (block.type === 'tool_use' && typeof block.name === 'string') {
|
|
332
|
+
flushText();
|
|
333
|
+
const id = typeof block.id === 'string' ? block.id : '';
|
|
334
|
+
const preview = truncateJsonish(block.input, TOOL_INPUT_PREVIEW_MAX);
|
|
335
|
+
out.push({
|
|
336
|
+
type: 'tool_call',
|
|
337
|
+
sessionId,
|
|
338
|
+
text: JSON.stringify({
|
|
339
|
+
name: block.name,
|
|
340
|
+
id,
|
|
341
|
+
input: preview,
|
|
342
|
+
}),
|
|
343
|
+
rawType: 'assistant:tool_use',
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
flushText();
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
350
|
+
function normalizeUserSdkMessage(message, sessionId) {
|
|
351
|
+
let payload = serializeUserMessageContent(message.message);
|
|
352
|
+
if (message.tool_use_result !== undefined) {
|
|
353
|
+
const extra = truncateJsonish(message.tool_use_result, 1500);
|
|
354
|
+
if (extra) {
|
|
355
|
+
payload = payload ? `${payload}\n[tool_use_result] ${extra}` : `[tool_use_result] ${extra}`;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
payload = truncateString(payload, USER_MESSAGE_MAX);
|
|
359
|
+
if (!payload.trim()) {
|
|
360
|
+
return [];
|
|
361
|
+
}
|
|
362
|
+
return [
|
|
363
|
+
{
|
|
364
|
+
type: 'user',
|
|
365
|
+
sessionId,
|
|
366
|
+
text: payload,
|
|
367
|
+
rawType: 'user',
|
|
368
|
+
},
|
|
369
|
+
];
|
|
370
|
+
}
|
|
371
|
+
function serializeUserMessageContent(message) {
|
|
372
|
+
if (typeof message === 'string') {
|
|
373
|
+
return message;
|
|
374
|
+
}
|
|
375
|
+
if (!isRecord(message)) {
|
|
376
|
+
return '';
|
|
377
|
+
}
|
|
378
|
+
const content = message.content;
|
|
379
|
+
if (typeof content === 'string') {
|
|
380
|
+
return content;
|
|
381
|
+
}
|
|
382
|
+
if (!Array.isArray(content)) {
|
|
383
|
+
return truncateJsonish(message, USER_MESSAGE_MAX);
|
|
384
|
+
}
|
|
385
|
+
const lines = [];
|
|
386
|
+
for (const part of content) {
|
|
387
|
+
if (!isRecord(part)) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
if (part.type === 'text' && typeof part.text === 'string') {
|
|
391
|
+
lines.push(part.text);
|
|
392
|
+
}
|
|
393
|
+
else if (part.type === 'tool_result') {
|
|
394
|
+
const id = typeof part.tool_use_id === 'string' ? part.tool_use_id : '';
|
|
395
|
+
const body = truncateJsonish(part.content, TOOL_INPUT_PREVIEW_MAX);
|
|
396
|
+
lines.push(`[tool_result:${id}] ${body}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return lines.filter(Boolean).join('\n');
|
|
400
|
+
}
|
|
401
|
+
function truncateJsonish(value, max) {
|
|
402
|
+
if (value === undefined || value === null) {
|
|
403
|
+
return '';
|
|
404
|
+
}
|
|
405
|
+
if (typeof value === 'string') {
|
|
406
|
+
return truncateString(value, max);
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
return truncateString(JSON.stringify(value), max);
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
return truncateString(String(value), max);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function truncateString(s, max) {
|
|
416
|
+
if (s.length <= max) {
|
|
417
|
+
return s;
|
|
418
|
+
}
|
|
419
|
+
return s.slice(0, max);
|
|
420
|
+
}
|
|
421
|
+
function isRecord(value) {
|
|
422
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
423
|
+
}
|
|
424
|
+
function extractPartialEventText(event) {
|
|
425
|
+
if (!isRecord(event)) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
const delta = event.delta;
|
|
429
|
+
if (isRecord(delta)) {
|
|
430
|
+
if (typeof delta.text === 'string' && delta.text.length > 0) {
|
|
431
|
+
return delta.text;
|
|
432
|
+
}
|
|
433
|
+
if (typeof delta.partial_json === 'string' && delta.partial_json.length > 0) {
|
|
434
|
+
return delta.partial_json;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
function extractText(payload) {
|
|
440
|
+
if (typeof payload === 'string') {
|
|
441
|
+
return payload;
|
|
442
|
+
}
|
|
443
|
+
if (!isRecord(payload)) {
|
|
444
|
+
return '';
|
|
445
|
+
}
|
|
446
|
+
if (Array.isArray(payload.content)) {
|
|
447
|
+
const parts = payload.content
|
|
448
|
+
.map((contentPart) => {
|
|
449
|
+
if (!isRecord(contentPart)) {
|
|
450
|
+
return '';
|
|
451
|
+
}
|
|
452
|
+
if (typeof contentPart.text === 'string') {
|
|
453
|
+
return contentPart.text;
|
|
454
|
+
}
|
|
455
|
+
if (contentPart.type === 'tool_use' && typeof contentPart.name === 'string') {
|
|
456
|
+
return `[tool:${contentPart.name}]`;
|
|
457
|
+
}
|
|
458
|
+
return '';
|
|
459
|
+
})
|
|
460
|
+
.filter(Boolean);
|
|
461
|
+
if (parts.length > 0) {
|
|
462
|
+
return parts.join('\n');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return JSON.stringify(payload);
|
|
466
|
+
}
|
|
467
|
+
function mapSlashCommand(command) {
|
|
468
|
+
return {
|
|
469
|
+
name: command.name,
|
|
470
|
+
description: command.description,
|
|
471
|
+
argumentHint: command.argumentHint,
|
|
472
|
+
source: 'sdk',
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function mapAgent(agent) {
|
|
476
|
+
return {
|
|
477
|
+
name: agent.name,
|
|
478
|
+
description: agent.description,
|
|
479
|
+
model: agent.model,
|
|
480
|
+
source: 'sdk',
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
function extractUsageFromResult(message) {
|
|
484
|
+
if (message.type !== 'result') {
|
|
485
|
+
return {};
|
|
486
|
+
}
|
|
487
|
+
const result = {};
|
|
488
|
+
// Extract from usage field
|
|
489
|
+
const usage = message.usage;
|
|
490
|
+
if (isRecord(usage)) {
|
|
491
|
+
if (typeof usage.input_tokens === 'number') {
|
|
492
|
+
result.inputTokens = usage.input_tokens;
|
|
493
|
+
}
|
|
494
|
+
if (typeof usage.output_tokens === 'number') {
|
|
495
|
+
result.outputTokens = usage.output_tokens;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
// Extract contextWindow from modelUsage (camelCase) or model_usage (snake_case)
|
|
499
|
+
const modelUsage = message.modelUsage ??
|
|
500
|
+
message.model_usage;
|
|
501
|
+
if (isRecord(modelUsage)) {
|
|
502
|
+
for (const model of Object.values(modelUsage)) {
|
|
503
|
+
if (isRecord(model)) {
|
|
504
|
+
const cw = typeof model.contextWindow === 'number'
|
|
505
|
+
? model.contextWindow
|
|
506
|
+
: typeof model.context_window === 'number'
|
|
507
|
+
? model.context_window
|
|
508
|
+
: undefined;
|
|
509
|
+
if (cw !== undefined) {
|
|
510
|
+
result.contextWindowSize = cw;
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return result;
|
|
517
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ClaudeCapabilitySnapshot, ClaudeSessionRunResult, ClaudeSessionSummary, ClaudeSessionTranscriptMessage, RunClaudeSessionInput } from '../types/contracts.js';
|
|
2
|
+
import type { ClaudeAgentSdkAdapter, ClaudeSessionEventHandler } from './claude-agent-sdk-adapter.js';
|
|
3
|
+
export declare class ClaudeSessionService {
|
|
4
|
+
private readonly sdkAdapter;
|
|
5
|
+
constructor(sdkAdapter: ClaudeAgentSdkAdapter);
|
|
6
|
+
runTask(input: RunClaudeSessionInput, onEvent?: ClaudeSessionEventHandler): Promise<ClaudeSessionRunResult>;
|
|
7
|
+
listSessions(cwd?: string): Promise<ClaudeSessionSummary[]>;
|
|
8
|
+
getTranscript(sessionId: string, cwd?: string): Promise<ClaudeSessionTranscriptMessage[]>;
|
|
9
|
+
probeCapabilities(cwd: string): Promise<ClaudeCapabilitySnapshot>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class ClaudeSessionService {
|
|
2
|
+
sdkAdapter;
|
|
3
|
+
constructor(sdkAdapter) {
|
|
4
|
+
this.sdkAdapter = sdkAdapter;
|
|
5
|
+
}
|
|
6
|
+
runTask(input, onEvent) {
|
|
7
|
+
return this.sdkAdapter.runSession(input, onEvent);
|
|
8
|
+
}
|
|
9
|
+
listSessions(cwd) {
|
|
10
|
+
return this.sdkAdapter.listSavedSessions(cwd);
|
|
11
|
+
}
|
|
12
|
+
getTranscript(sessionId, cwd) {
|
|
13
|
+
return this.sdkAdapter.getTranscript(sessionId, cwd);
|
|
14
|
+
}
|
|
15
|
+
probeCapabilities(cwd) {
|
|
16
|
+
return this.sdkAdapter.probeCapabilities(cwd);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { LiveTailEvent, ToolOutputPreview } from '../types/contracts.js';
|
|
2
|
+
/**
|
|
3
|
+
* Tails Claude Code session JSONL files for live tool output.
|
|
4
|
+
*
|
|
5
|
+
* The SDK streams high-level events (assistant text, tool_call summaries, results)
|
|
6
|
+
* but does not expose the raw tool output that Claude Code writes to the JSONL
|
|
7
|
+
* transcript on disk. This service fills that gap by polling the file for new
|
|
8
|
+
* lines, parsing each one, and forwarding parsed events to a caller-supplied
|
|
9
|
+
* callback.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SessionLiveTailer {
|
|
12
|
+
private activeTails;
|
|
13
|
+
/**
|
|
14
|
+
* Locate the JSONL file for a session.
|
|
15
|
+
*
|
|
16
|
+
* Claude Code stores transcripts at:
|
|
17
|
+
* ~/.claude/projects/<sanitized-cwd>/sessions/<session-id>.jsonl
|
|
18
|
+
*
|
|
19
|
+
* The `<sanitized-cwd>` folder name is an internal implementation detail that
|
|
20
|
+
* can change across Claude Code versions, so we search all project directories
|
|
21
|
+
* for the session file rather than attempting to replicate the sanitisation.
|
|
22
|
+
*/
|
|
23
|
+
findSessionFile(sessionId: string, cwd?: string): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Check whether we can locate a JSONL file for the given session.
|
|
26
|
+
*/
|
|
27
|
+
sessionFileExists(sessionId: string, cwd?: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Poll a session's JSONL file for new lines and emit parsed events.
|
|
30
|
+
*
|
|
31
|
+
* Returns a stop function. The caller **must** invoke it when tailing is no
|
|
32
|
+
* longer needed (e.g. when the session completes or the tool is interrupted).
|
|
33
|
+
*/
|
|
34
|
+
startTailing(sessionId: string, cwd: string | undefined, onEvent: (event: LiveTailEvent) => void, pollIntervalMs?: number): () => void;
|
|
35
|
+
/**
|
|
36
|
+
* Stop tailing a specific session.
|
|
37
|
+
*/
|
|
38
|
+
stopTailing(sessionId: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* Stop all active tails (cleanup on shutdown).
|
|
41
|
+
*/
|
|
42
|
+
stopAll(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Read the last N lines from a session JSONL file.
|
|
45
|
+
*/
|
|
46
|
+
getLastLines(sessionId: string, cwd: string | undefined, lineCount?: number): Promise<string[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Extract a preview of recent tool output from the tail of a session file.
|
|
49
|
+
*/
|
|
50
|
+
getToolOutputPreview(sessionId: string, cwd: string | undefined, maxEntries?: number): Promise<ToolOutputPreview[]>;
|
|
51
|
+
}
|