@codetelemetry/mcp 0.3.8 → 0.3.12
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 +1 -1
- package/README.md +90 -9
- package/dist/src/client/agent.d.ts +221 -3
- package/dist/src/client/agent.js +174 -0
- package/dist/src/client/assistants.d.ts +4 -0
- package/dist/src/client/assistants.js +5 -0
- package/dist/src/client/bin.d.ts +2 -0
- package/dist/src/client/bin.js +3 -0
- package/dist/src/client/cli.d.ts +1 -0
- package/dist/src/client/cli.js +567 -0
- package/dist/src/client/clients.d.ts +34 -0
- package/dist/src/client/clients.js +74 -0
- package/dist/src/client/codexConfig.d.ts +1 -0
- package/dist/src/client/codexConfig.js +5 -0
- package/dist/src/client/config.d.ts +7 -0
- package/dist/src/client/config.js +7 -0
- package/dist/src/client/graderSeed.d.ts +50 -0
- package/dist/src/client/graderSeed.js +289 -0
- package/dist/src/client/hostMetrics.d.ts +54 -0
- package/dist/src/client/hostMetrics.js +425 -0
- package/dist/src/client/import/browser.d.ts +30 -0
- package/dist/src/client/import/browser.js +157 -0
- package/dist/src/client/import/convert.d.ts +20 -0
- package/dist/src/client/import/convert.js +165 -0
- package/dist/src/client/import/core.d.ts +143 -0
- package/dist/src/client/import/core.js +441 -0
- package/dist/src/client/import/ledger.d.ts +52 -0
- package/dist/src/client/import/ledger.js +161 -0
- package/dist/src/client/import/model.d.ts +70 -0
- package/dist/src/client/import/model.js +42 -0
- package/dist/src/client/import/options.d.ts +24 -0
- package/dist/src/client/import/options.js +71 -0
- package/dist/src/client/import/otlpImport.d.ts +7 -0
- package/dist/src/client/import/otlpImport.js +81 -0
- package/dist/src/client/import/protocol.d.ts +35 -0
- package/dist/src/client/import/protocol.js +51 -0
- package/dist/src/client/import/run.d.ts +30 -0
- package/dist/src/client/import/run.js +127 -0
- package/dist/src/client/import/sources/claudeCodeText.d.ts +19 -0
- package/dist/src/client/import/sources/claudeCodeText.js +214 -0
- package/dist/src/client/import/sources/codexText.d.ts +20 -0
- package/dist/src/client/import/sources/codexText.js +319 -0
- package/dist/src/client/import/sources/files.d.ts +24 -0
- package/dist/src/client/import/sources/files.js +129 -0
- package/dist/src/client/import/transport.d.ts +76 -0
- package/dist/src/client/import/transport.js +141 -0
- package/dist/src/client/index.d.ts +19 -0
- package/dist/src/client/index.js +19 -0
- package/dist/src/client/kiro/collector.d.ts +88 -0
- package/dist/src/client/kiro/collector.js +577 -0
- package/dist/src/client/kiro/command.d.ts +3 -0
- package/dist/src/client/kiro/command.js +109 -0
- package/dist/src/client/kiro/config.d.ts +39 -0
- package/dist/src/client/kiro/config.js +107 -0
- package/dist/src/client/kiro/records.d.ts +52 -0
- package/dist/src/client/kiro/records.js +206 -0
- package/dist/src/client/kiro/service.d.ts +21 -0
- package/dist/src/client/kiro/service.js +183 -0
- package/dist/src/client/kiro/state.d.ts +45 -0
- package/dist/src/client/kiro/state.js +205 -0
- package/dist/src/client/kiro/transport.d.ts +9 -0
- package/dist/src/client/kiro/transport.js +90 -0
- package/dist/src/client/onboard.d.ts +5 -1
- package/dist/src/client/onboard.js +28 -23
- package/dist/src/client/piConfig.d.ts +29 -0
- package/dist/src/client/piConfig.js +112 -0
- package/dist/src/client/plan.d.ts +1 -1
- package/dist/src/client/plan.js +2 -1
- package/dist/src/client/settings.d.ts +4 -0
- package/dist/src/client/settings.js +17 -0
- package/dist/src/client/yaml.d.ts +5 -0
- package/dist/src/client/yaml.js +72 -0
- package/dist/src/mcpServer.js +1 -1
- package/dist/src/tools.d.ts +9 -6
- package/dist/src/tools.js +478 -60
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +178 -9
- package/tool-manifest.json +497 -21
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { MAX_EVENTS_PER_SESSION, MAX_PROMPT_CHARS, MAX_TOOL_INPUT_CHARS, NO_CONTENT, capText } from "../model.js";
|
|
2
|
+
const textOfBlocks = (content) => {
|
|
3
|
+
if (typeof content === "string")
|
|
4
|
+
return content;
|
|
5
|
+
if (!Array.isArray(content))
|
|
6
|
+
return "";
|
|
7
|
+
return content
|
|
8
|
+
.filter((b) => b && b.type === "text" && typeof b.text === "string")
|
|
9
|
+
.map((b) => b.text)
|
|
10
|
+
.join("\n");
|
|
11
|
+
};
|
|
12
|
+
const sizeOf = (value) => {
|
|
13
|
+
if (typeof value === "string")
|
|
14
|
+
return value.length;
|
|
15
|
+
try {
|
|
16
|
+
return JSON.stringify(value)?.length ?? 0;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const count = (value) => Math.max(0, Math.trunc(Number(value) || 0));
|
|
23
|
+
const injectedPrompt = (text) => text.startsWith("<") || text.startsWith("[Request interrupted") || text.startsWith("Caveat: The messages below");
|
|
24
|
+
export class ClaudeSessionAccumulator {
|
|
25
|
+
#keep;
|
|
26
|
+
#prompts = [];
|
|
27
|
+
#tools = [];
|
|
28
|
+
#calls = new Map();
|
|
29
|
+
#pending = new Map();
|
|
30
|
+
#sessionId = "";
|
|
31
|
+
#cwd = null;
|
|
32
|
+
#skipped = 0;
|
|
33
|
+
#total = 0;
|
|
34
|
+
#overflow = false;
|
|
35
|
+
constructor(opts = {}) {
|
|
36
|
+
this.#keep = opts.keep ?? NO_CONTENT;
|
|
37
|
+
}
|
|
38
|
+
get overflowed() {
|
|
39
|
+
return this.#overflow;
|
|
40
|
+
}
|
|
41
|
+
#events() {
|
|
42
|
+
return this.#prompts.length + this.#tools.length + this.#calls.size;
|
|
43
|
+
}
|
|
44
|
+
line(raw) {
|
|
45
|
+
if (!raw.trim())
|
|
46
|
+
return;
|
|
47
|
+
this.#total++;
|
|
48
|
+
if (this.#overflow)
|
|
49
|
+
return;
|
|
50
|
+
let r;
|
|
51
|
+
try {
|
|
52
|
+
r = JSON.parse(raw);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
this.#skipped++;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!r || typeof r !== "object") {
|
|
59
|
+
this.#skipped++;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!this.#sessionId && typeof r.sessionId === "string")
|
|
63
|
+
this.#sessionId = r.sessionId;
|
|
64
|
+
if (!this.#cwd && typeof r.cwd === "string" && r.cwd)
|
|
65
|
+
this.#cwd = r.cwd;
|
|
66
|
+
const tsMs = typeof r.timestamp === "string" ? Date.parse(r.timestamp) : NaN;
|
|
67
|
+
if ((r.type === "user" || r.type === "assistant") && r.message && Number.isFinite(tsMs)) {
|
|
68
|
+
if (this.#events() >= MAX_EVENTS_PER_SESSION) {
|
|
69
|
+
this.#overflow = true;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (r.type === "user")
|
|
73
|
+
this.#user(r, tsMs);
|
|
74
|
+
else
|
|
75
|
+
this.#assistant(r, tsMs);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.#skipped++;
|
|
79
|
+
}
|
|
80
|
+
#user(r, tsMs) {
|
|
81
|
+
const content = r.message.content;
|
|
82
|
+
if (Array.isArray(content)) {
|
|
83
|
+
for (const block of content) {
|
|
84
|
+
if (!block || block.type !== "tool_result" || typeof block.tool_use_id !== "string")
|
|
85
|
+
continue;
|
|
86
|
+
const pending = this.#pending.get(block.tool_use_id);
|
|
87
|
+
if (!pending)
|
|
88
|
+
continue;
|
|
89
|
+
this.#pending.delete(block.tool_use_id);
|
|
90
|
+
this.#tools.push({
|
|
91
|
+
toolUseId: block.tool_use_id,
|
|
92
|
+
toolName: pending.name,
|
|
93
|
+
tsMs,
|
|
94
|
+
input: pending.input,
|
|
95
|
+
inputBytes: pending.inputBytes,
|
|
96
|
+
success: block.is_error === true ? false : true,
|
|
97
|
+
resultBytes: sizeOf(block.content),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const human = !r.isSidechain && !r.isMeta && !r.isCompactSummary && !r.isVisibleInTranscriptOnly &&
|
|
102
|
+
(!r.origin || r.origin.kind === "human") && r.message.role === "user";
|
|
103
|
+
if (!human)
|
|
104
|
+
return;
|
|
105
|
+
const text = textOfBlocks(content).trim();
|
|
106
|
+
if (!text || injectedPrompt(text))
|
|
107
|
+
return;
|
|
108
|
+
const promptId = typeof r.promptId === "string" && r.promptId ? r.promptId
|
|
109
|
+
: typeof r.uuid === "string" && r.uuid ? r.uuid : `${this.#sessionId || "session"}:p${this.#prompts.length}`;
|
|
110
|
+
this.#prompts.push({
|
|
111
|
+
promptId,
|
|
112
|
+
tsMs,
|
|
113
|
+
text: this.#keep.prompts ? capText(text, MAX_PROMPT_CHARS) : null,
|
|
114
|
+
length: text.length,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
#assistant(r, tsMs) {
|
|
118
|
+
const model = typeof r.message.model === "string" ? r.message.model : "";
|
|
119
|
+
const responseId = String(r.requestId ?? r.message.id ?? "");
|
|
120
|
+
const content = r.message.content;
|
|
121
|
+
if (Array.isArray(content)) {
|
|
122
|
+
for (const block of content) {
|
|
123
|
+
if (block && block.type === "tool_use" && typeof block.id === "string") {
|
|
124
|
+
let full = "";
|
|
125
|
+
try {
|
|
126
|
+
full = block.input === undefined ? "" : JSON.stringify(block.input) ?? "";
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
full = "";
|
|
130
|
+
}
|
|
131
|
+
this.#pending.set(block.id, {
|
|
132
|
+
name: String(block.name ?? "unknown"),
|
|
133
|
+
input: this.#keep.toolDetails && full ? capText(full, MAX_TOOL_INPUT_CHARS) : null,
|
|
134
|
+
inputBytes: full.length,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!responseId || model === "<synthetic>" || r.isApiErrorMessage)
|
|
140
|
+
return;
|
|
141
|
+
const usage = r.message.usage;
|
|
142
|
+
if (!usage || typeof usage !== "object")
|
|
143
|
+
return;
|
|
144
|
+
const prev = this.#calls.get(responseId);
|
|
145
|
+
this.#calls.set(responseId, {
|
|
146
|
+
responseId,
|
|
147
|
+
tsMs: prev?.tsMs ?? tsMs,
|
|
148
|
+
model: model || prev?.model || "",
|
|
149
|
+
inputTokens: count(usage.input_tokens),
|
|
150
|
+
outputTokens: count(usage.output_tokens),
|
|
151
|
+
cacheReadTokens: count(usage.cache_read_input_tokens),
|
|
152
|
+
cacheCreationTokens: count(usage.cache_creation_input_tokens),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
finish(meta) {
|
|
156
|
+
const modelCalls = [...this.#calls.values()].filter((c) => c.inputTokens + c.outputTokens + c.cacheReadTokens + c.cacheCreationTokens > 0);
|
|
157
|
+
const prompts = [...this.#prompts].sort((a, b) => a.tsMs - b.tsMs);
|
|
158
|
+
const toolCalls = [...this.#tools].sort((a, b) => a.tsMs - b.tsMs);
|
|
159
|
+
modelCalls.sort((a, b) => a.tsMs - b.tsMs);
|
|
160
|
+
if (prompts.length + modelCalls.length + toolCalls.length === 0)
|
|
161
|
+
return null;
|
|
162
|
+
let p = -1;
|
|
163
|
+
const attribute = (items) => {
|
|
164
|
+
p = -1;
|
|
165
|
+
for (const item of items) {
|
|
166
|
+
while (p + 1 < prompts.length && prompts[p + 1].tsMs <= item.tsMs)
|
|
167
|
+
p++;
|
|
168
|
+
if (p >= 0)
|
|
169
|
+
item.promptId = prompts[p].promptId;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
attribute(modelCalls);
|
|
173
|
+
attribute(toolCalls);
|
|
174
|
+
let startMs = Infinity;
|
|
175
|
+
let endMs = -Infinity;
|
|
176
|
+
for (const e of [...prompts, ...modelCalls, ...toolCalls]) {
|
|
177
|
+
if (e.tsMs < startMs)
|
|
178
|
+
startMs = e.tsMs;
|
|
179
|
+
if (e.tsMs > endMs)
|
|
180
|
+
endMs = e.tsMs;
|
|
181
|
+
}
|
|
182
|
+
const warnings = [];
|
|
183
|
+
if (this.#pending.size > 0)
|
|
184
|
+
warnings.push(`${this.#pending.size} tool call(s) without a recorded result (not imported)`);
|
|
185
|
+
return {
|
|
186
|
+
source: "claude-code",
|
|
187
|
+
sessionId: this.#sessionId || meta.fallbackSessionId || "claude-session",
|
|
188
|
+
files: meta.files,
|
|
189
|
+
fileSig: meta.fileSig,
|
|
190
|
+
cwd: this.#cwd,
|
|
191
|
+
startMs,
|
|
192
|
+
endMs,
|
|
193
|
+
prompts,
|
|
194
|
+
modelCalls,
|
|
195
|
+
toolCalls,
|
|
196
|
+
warnings,
|
|
197
|
+
skippedRecords: this.#skipped,
|
|
198
|
+
totalRecords: this.#total,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export function parseClaudeTranscriptText(raw, meta = {}, opts = {}) {
|
|
203
|
+
const acc = new ClaudeSessionAccumulator(opts);
|
|
204
|
+
for (const line of raw.split("\n"))
|
|
205
|
+
acc.line(line);
|
|
206
|
+
if (acc.overflowed)
|
|
207
|
+
return null;
|
|
208
|
+
const base = (meta.filePath ?? "").replace(/\\/g, "/").split("/").pop() ?? "";
|
|
209
|
+
return acc.finish({
|
|
210
|
+
files: meta.filePath ? [meta.filePath] : [],
|
|
211
|
+
fileSig: meta.fileSig ?? `${raw.length}:0`,
|
|
212
|
+
fallbackSessionId: base.replace(/\.jsonl$/, "") || undefined,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ContentPolicy, ImportedSession } from "../model.ts";
|
|
2
|
+
export type CodexParseOptions = {
|
|
3
|
+
keep?: ContentPolicy;
|
|
4
|
+
};
|
|
5
|
+
export declare class CodexSessionAccumulator {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(opts?: CodexParseOptions);
|
|
8
|
+
get overflowed(): boolean;
|
|
9
|
+
line(raw: string): void;
|
|
10
|
+
finish(meta: {
|
|
11
|
+
files: string[];
|
|
12
|
+
fileSig: string;
|
|
13
|
+
fallbackSessionId?: string;
|
|
14
|
+
}): ImportedSession | null;
|
|
15
|
+
}
|
|
16
|
+
export declare function codexSessionIdFromName(filePath: string): string | undefined;
|
|
17
|
+
export declare function parseCodexRolloutText(raw: string, meta?: {
|
|
18
|
+
filePath?: string;
|
|
19
|
+
fileSig?: string;
|
|
20
|
+
}, opts?: CodexParseOptions): ImportedSession | null;
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { MAX_EVENTS_PER_SESSION, MAX_PROMPT_CHARS, MAX_TOOL_INPUT_CHARS, NO_CONTENT, capText } from "../model.js";
|
|
2
|
+
const count = (value) => Math.max(0, Math.trunc(Number(value) || 0));
|
|
3
|
+
const usageOf = (u) => {
|
|
4
|
+
if (!u || typeof u !== "object")
|
|
5
|
+
return null;
|
|
6
|
+
const v = u;
|
|
7
|
+
return { input: count(v.input_tokens), cached: count(v.cached_input_tokens), output: count(v.output_tokens) };
|
|
8
|
+
};
|
|
9
|
+
const sizeOf = (value) => {
|
|
10
|
+
if (typeof value === "string")
|
|
11
|
+
return value.length;
|
|
12
|
+
try {
|
|
13
|
+
return JSON.stringify(value)?.length ?? 0;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const exitCodeSuccess = (output) => {
|
|
20
|
+
if (typeof output !== "string" || !output.startsWith("{"))
|
|
21
|
+
return undefined;
|
|
22
|
+
try {
|
|
23
|
+
const code = JSON.parse(output)?.metadata?.exit_code;
|
|
24
|
+
if (typeof code === "number")
|
|
25
|
+
return code === 0;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
};
|
|
31
|
+
const userMessageText = (item) => {
|
|
32
|
+
if (!Array.isArray(item.content))
|
|
33
|
+
return "";
|
|
34
|
+
return item.content
|
|
35
|
+
.filter((c) => c && typeof c.text === "string")
|
|
36
|
+
.map((c) => c.text)
|
|
37
|
+
.join("\n");
|
|
38
|
+
};
|
|
39
|
+
export class CodexSessionAccumulator {
|
|
40
|
+
#keep;
|
|
41
|
+
#prompts = [];
|
|
42
|
+
#records = [];
|
|
43
|
+
#counts = [];
|
|
44
|
+
#tools = [];
|
|
45
|
+
#pending = new Map();
|
|
46
|
+
#verdicts = new Map();
|
|
47
|
+
#warnings = new Set();
|
|
48
|
+
#sessionId = "";
|
|
49
|
+
#cwd = null;
|
|
50
|
+
#model = "";
|
|
51
|
+
#prevTotal = null;
|
|
52
|
+
#skipped = 0;
|
|
53
|
+
#total = 0;
|
|
54
|
+
#overflow = false;
|
|
55
|
+
constructor(opts = {}) {
|
|
56
|
+
this.#keep = opts.keep ?? NO_CONTENT;
|
|
57
|
+
}
|
|
58
|
+
get overflowed() {
|
|
59
|
+
return this.#overflow;
|
|
60
|
+
}
|
|
61
|
+
line(raw) {
|
|
62
|
+
if (!raw.trim())
|
|
63
|
+
return;
|
|
64
|
+
this.#total++;
|
|
65
|
+
if (this.#overflow)
|
|
66
|
+
return;
|
|
67
|
+
let r;
|
|
68
|
+
try {
|
|
69
|
+
r = JSON.parse(raw);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
this.#skipped++;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!r || typeof r !== "object") {
|
|
76
|
+
this.#skipped++;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const p = r.payload && typeof r.payload === "object" ? r.payload : {};
|
|
80
|
+
if (r.type === "session_meta") {
|
|
81
|
+
if (!this.#sessionId) {
|
|
82
|
+
if (typeof p.id === "string" && p.id)
|
|
83
|
+
this.#sessionId = p.id;
|
|
84
|
+
else if (typeof p.session_id === "string" && p.session_id)
|
|
85
|
+
this.#sessionId = p.session_id;
|
|
86
|
+
}
|
|
87
|
+
if (!this.#cwd && typeof p.cwd === "string" && p.cwd)
|
|
88
|
+
this.#cwd = p.cwd;
|
|
89
|
+
if (typeof p.model === "string" && p.model)
|
|
90
|
+
this.#model = p.model;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (r.type === "turn_context") {
|
|
94
|
+
if (typeof p.model === "string" && p.model)
|
|
95
|
+
this.#model = p.model;
|
|
96
|
+
if (!this.#cwd && typeof p.cwd === "string" && p.cwd)
|
|
97
|
+
this.#cwd = p.cwd;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const tsMs = typeof r.timestamp === "string" ? Date.parse(r.timestamp) : NaN;
|
|
101
|
+
if (!Number.isFinite(tsMs)) {
|
|
102
|
+
this.#skipped++;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (this.#prompts.length + this.#records.length + this.#counts.length + this.#tools.length >= MAX_EVENTS_PER_SESSION) {
|
|
106
|
+
this.#overflow = true;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (r.type === "token_usage_record") {
|
|
110
|
+
const u = usageOf(p.usage);
|
|
111
|
+
const responseId = typeof p.response_id === "string" ? p.response_id : "";
|
|
112
|
+
if (u && responseId)
|
|
113
|
+
this.#modelCall(this.#records, responseId, tsMs, u);
|
|
114
|
+
else
|
|
115
|
+
this.#skipped++;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (r.type === "event_msg") {
|
|
119
|
+
if (p.type === "user_message") {
|
|
120
|
+
const text = typeof p.message === "string" ? p.message.trim() : "";
|
|
121
|
+
const id = typeof p.client_id === "string" && p.client_id ? p.client_id : "";
|
|
122
|
+
this.#prompt(id, tsMs, text);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (p.type === "item_completed" && p.item && typeof p.item === "object") {
|
|
126
|
+
const item = p.item;
|
|
127
|
+
if (item.type === "UserMessage") {
|
|
128
|
+
this.#prompt(typeof item.id === "string" ? item.id : "", tsMs, userMessageText(item).trim());
|
|
129
|
+
}
|
|
130
|
+
else if (item.type === "CommandExecution" && typeof item.id === "string" && typeof item.exit_code === "number") {
|
|
131
|
+
this.#verdicts.set(item.id, item.exit_code === 0);
|
|
132
|
+
}
|
|
133
|
+
else if (item.type === "McpToolCall" && typeof item.id === "string" && typeof item.result?.isError === "boolean") {
|
|
134
|
+
this.#verdicts.set(item.id, !item.result.isError);
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (p.type === "token_count") {
|
|
139
|
+
this.#tokenCount(p.info, tsMs);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (r.type === "response_item") {
|
|
144
|
+
if (p.type === "function_call" || p.type === "custom_tool_call" || p.type === "local_shell_call") {
|
|
145
|
+
const callId = typeof p.call_id === "string" ? p.call_id : typeof p.id === "string" ? p.id : "";
|
|
146
|
+
if (!callId) {
|
|
147
|
+
this.#skipped++;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const rawInput = p.arguments ?? p.input ?? p.action;
|
|
151
|
+
let full = "";
|
|
152
|
+
if (typeof rawInput === "string")
|
|
153
|
+
full = rawInput;
|
|
154
|
+
else if (rawInput !== undefined) {
|
|
155
|
+
try {
|
|
156
|
+
full = JSON.stringify(rawInput) ?? "";
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
full = "";
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
this.#pending.set(callId, {
|
|
163
|
+
name: String(p.name ?? (p.type === "local_shell_call" ? "shell" : "unknown")),
|
|
164
|
+
input: this.#keep.toolDetails && full ? capText(full, MAX_TOOL_INPUT_CHARS) : null,
|
|
165
|
+
inputBytes: full.length,
|
|
166
|
+
});
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (p.type === "function_call_output" || p.type === "custom_tool_call_output" || p.type === "local_shell_call_output") {
|
|
170
|
+
const callId = typeof p.call_id === "string" ? p.call_id : "";
|
|
171
|
+
const pending = callId ? this.#pending.get(callId) : undefined;
|
|
172
|
+
if (!pending) {
|
|
173
|
+
this.#skipped++;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this.#pending.delete(callId);
|
|
177
|
+
const success = exitCodeSuccess(p.output);
|
|
178
|
+
this.#tools.push({
|
|
179
|
+
toolUseId: callId,
|
|
180
|
+
toolName: pending.name,
|
|
181
|
+
tsMs,
|
|
182
|
+
input: pending.input,
|
|
183
|
+
inputBytes: pending.inputBytes,
|
|
184
|
+
...(success !== undefined ? { success } : {}),
|
|
185
|
+
resultBytes: sizeOf(p.output),
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
this.#skipped++;
|
|
191
|
+
}
|
|
192
|
+
#prompt(id, tsMs, text) {
|
|
193
|
+
if (!text || text.startsWith("<"))
|
|
194
|
+
return;
|
|
195
|
+
this.#prompts.push({
|
|
196
|
+
promptId: id || `${this.#sessionId || "codex"}:p${this.#prompts.length}`,
|
|
197
|
+
tsMs,
|
|
198
|
+
text: this.#keep.prompts ? capText(text, MAX_PROMPT_CHARS) : null,
|
|
199
|
+
length: text.length,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
#modelCall(into, responseId, tsMs, u) {
|
|
203
|
+
if (u.input + u.output === 0)
|
|
204
|
+
return;
|
|
205
|
+
if (u.cached > u.input) {
|
|
206
|
+
this.#warnings.add("a token record reported more cached than total input tokens (not imported)");
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
into.push({
|
|
210
|
+
responseId,
|
|
211
|
+
tsMs,
|
|
212
|
+
model: this.#model,
|
|
213
|
+
inputTokens: u.input - u.cached,
|
|
214
|
+
outputTokens: u.output,
|
|
215
|
+
cacheReadTokens: u.cached,
|
|
216
|
+
cacheCreationTokens: 0,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
#tokenCount(info, tsMs) {
|
|
220
|
+
if (!info || typeof info !== "object")
|
|
221
|
+
return;
|
|
222
|
+
const i = info;
|
|
223
|
+
const total = usageOf(i.total_token_usage);
|
|
224
|
+
let turn = usageOf(i.last_token_usage);
|
|
225
|
+
if (total && this.#prevTotal &&
|
|
226
|
+
total.input === this.#prevTotal.input && total.cached === this.#prevTotal.cached && total.output === this.#prevTotal.output) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (!turn && total) {
|
|
230
|
+
if (this.#prevTotal) {
|
|
231
|
+
const d = {
|
|
232
|
+
input: total.input - this.#prevTotal.input,
|
|
233
|
+
cached: total.cached - this.#prevTotal.cached,
|
|
234
|
+
output: total.output - this.#prevTotal.output,
|
|
235
|
+
};
|
|
236
|
+
if (d.input < 0 || d.cached < 0 || d.output < 0) {
|
|
237
|
+
this.#warnings.add("token counter reset mid-session; that turn's usage was not imported");
|
|
238
|
+
turn = null;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
turn = d;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
turn = total;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (total)
|
|
249
|
+
this.#prevTotal = total;
|
|
250
|
+
if (turn)
|
|
251
|
+
this.#modelCall(this.#counts, `${this.#sessionId || "codex"}:t${this.#counts.length}`, tsMs, turn);
|
|
252
|
+
}
|
|
253
|
+
finish(meta) {
|
|
254
|
+
const modelCalls = (this.#records.length > 0 ? this.#records : this.#counts).sort((a, b) => a.tsMs - b.tsMs);
|
|
255
|
+
const prompts = [...this.#prompts].sort((a, b) => a.tsMs - b.tsMs);
|
|
256
|
+
const toolCalls = this.#tools
|
|
257
|
+
.map((t) => (t.success === undefined && this.#verdicts.has(t.toolUseId) ? { ...t, success: this.#verdicts.get(t.toolUseId) } : t))
|
|
258
|
+
.sort((a, b) => a.tsMs - b.tsMs);
|
|
259
|
+
if (prompts.length + modelCalls.length + toolCalls.length === 0)
|
|
260
|
+
return null;
|
|
261
|
+
let p = -1;
|
|
262
|
+
for (const c of modelCalls) {
|
|
263
|
+
while (p + 1 < prompts.length && prompts[p + 1].tsMs <= c.tsMs)
|
|
264
|
+
p++;
|
|
265
|
+
if (p >= 0)
|
|
266
|
+
c.promptId = prompts[p].promptId;
|
|
267
|
+
}
|
|
268
|
+
p = -1;
|
|
269
|
+
for (const t of toolCalls) {
|
|
270
|
+
while (p + 1 < prompts.length && prompts[p + 1].tsMs <= t.tsMs)
|
|
271
|
+
p++;
|
|
272
|
+
if (p >= 0)
|
|
273
|
+
t.promptId = prompts[p].promptId;
|
|
274
|
+
}
|
|
275
|
+
let startMs = Infinity;
|
|
276
|
+
let endMs = -Infinity;
|
|
277
|
+
for (const e of [...prompts, ...modelCalls, ...toolCalls]) {
|
|
278
|
+
if (e.tsMs < startMs)
|
|
279
|
+
startMs = e.tsMs;
|
|
280
|
+
if (e.tsMs > endMs)
|
|
281
|
+
endMs = e.tsMs;
|
|
282
|
+
}
|
|
283
|
+
const warnings = [...this.#warnings];
|
|
284
|
+
if (this.#pending.size > 0)
|
|
285
|
+
warnings.push(`${this.#pending.size} tool call(s) without a recorded output (not imported)`);
|
|
286
|
+
return {
|
|
287
|
+
source: "codex",
|
|
288
|
+
sessionId: this.#sessionId || meta.fallbackSessionId || "codex-session",
|
|
289
|
+
files: meta.files,
|
|
290
|
+
fileSig: meta.fileSig,
|
|
291
|
+
cwd: this.#cwd,
|
|
292
|
+
startMs,
|
|
293
|
+
endMs,
|
|
294
|
+
prompts,
|
|
295
|
+
modelCalls,
|
|
296
|
+
toolCalls,
|
|
297
|
+
warnings,
|
|
298
|
+
skippedRecords: this.#skipped,
|
|
299
|
+
totalRecords: this.#total,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
export function codexSessionIdFromName(filePath) {
|
|
304
|
+
const base = filePath.replace(/\\/g, "/").split("/").pop()?.replace(/\.jsonl$/, "") ?? "";
|
|
305
|
+
const m = base.match(/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i);
|
|
306
|
+
return m ? m[1] : undefined;
|
|
307
|
+
}
|
|
308
|
+
export function parseCodexRolloutText(raw, meta = {}, opts = {}) {
|
|
309
|
+
const acc = new CodexSessionAccumulator(opts);
|
|
310
|
+
for (const line of raw.split("\n"))
|
|
311
|
+
acc.line(line);
|
|
312
|
+
if (acc.overflowed)
|
|
313
|
+
return null;
|
|
314
|
+
return acc.finish({
|
|
315
|
+
files: meta.filePath ? [meta.filePath] : [],
|
|
316
|
+
fileSig: meta.fileSig ?? `${raw.length}:0`,
|
|
317
|
+
fallbackSessionId: meta.filePath ? codexSessionIdFromName(meta.filePath) : undefined,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ContentPolicy, ImportSource, ImportedSession } from "../model.ts";
|
|
2
|
+
export declare const MAX_SESSION_FILE_BYTES: number;
|
|
3
|
+
export declare const MAX_SUBAGENT_FILES = 2000;
|
|
4
|
+
export type SessionCandidate = {
|
|
5
|
+
source: ImportSource;
|
|
6
|
+
file: string;
|
|
7
|
+
extraFiles: string[];
|
|
8
|
+
mtimeMs: number;
|
|
9
|
+
bytes: number;
|
|
10
|
+
fileSig: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function defaultClaudeProjectsDir(home?: string): string;
|
|
13
|
+
export declare function defaultCodexSessionsDir(home?: string, env?: NodeJS.ProcessEnv): string;
|
|
14
|
+
export declare function scanClaudeSessions(projectsDir: string): SessionCandidate[];
|
|
15
|
+
export declare function scanCodexSessions(sessionsDir: string): SessionCandidate[];
|
|
16
|
+
export type ParseOutcome = {
|
|
17
|
+
ok: true;
|
|
18
|
+
session: ImportedSession | null;
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
reason: "too_large" | "too_many_events" | "unreadable";
|
|
22
|
+
detail: string;
|
|
23
|
+
};
|
|
24
|
+
export declare function parseCandidate(c: SessionCandidate, keep: ContentPolicy): Promise<ParseOutcome>;
|