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