@ironbee-ai/cli 0.13.0 → 0.14.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/CHANGELOG.md +6 -0
- package/README.md +9 -0
- package/dist/clients/claude/hooks/activity-start.d.ts.map +1 -1
- package/dist/clients/claude/hooks/activity-start.js +4 -0
- package/dist/clients/claude/hooks/activity-start.js.map +1 -1
- package/dist/clients/claude/hooks/session-start.d.ts.map +1 -1
- package/dist/clients/claude/hooks/session-start.js +4 -0
- package/dist/clients/claude/hooks/session-start.js.map +1 -1
- package/dist/clients/claude/index.d.ts +10 -0
- package/dist/clients/claude/index.d.ts.map +1 -1
- package/dist/clients/claude/index.js +105 -0
- package/dist/clients/claude/index.js.map +1 -1
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +1 -0
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/otel.d.ts +20 -0
- package/dist/commands/otel.d.ts.map +1 -0
- package/dist/commands/otel.js +132 -0
- package/dist/commands/otel.js.map +1 -0
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/config.d.ts +47 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +57 -3
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/event.d.ts +112 -0
- package/dist/lib/event.d.ts.map +1 -1
- package/dist/lib/event.js +1 -0
- package/dist/lib/event.js.map +1 -1
- package/dist/lib/gitignore.d.ts.map +1 -1
- package/dist/lib/gitignore.js +6 -0
- package/dist/lib/gitignore.js.map +1 -1
- package/dist/lib/version.d.ts +5 -0
- package/dist/lib/version.d.ts.map +1 -1
- package/dist/lib/version.js +5 -0
- package/dist/lib/version.js.map +1 -1
- package/dist/otel/collector.d.ts +66 -0
- package/dist/otel/collector.d.ts.map +1 -0
- package/dist/otel/collector.js +317 -0
- package/dist/otel/collector.js.map +1 -0
- package/dist/otel/context/build.d.ts +37 -0
- package/dist/otel/context/build.d.ts.map +1 -0
- package/dist/otel/context/build.js +103 -0
- package/dist/otel/context/build.js.map +1 -0
- package/dist/otel/context/classify.d.ts +40 -0
- package/dist/otel/context/classify.d.ts.map +1 -0
- package/dist/otel/context/classify.js +228 -0
- package/dist/otel/context/classify.js.map +1 -0
- package/dist/otel/context/extract.d.ts +39 -0
- package/dist/otel/context/extract.d.ts.map +1 -0
- package/dist/otel/context/extract.js +76 -0
- package/dist/otel/context/extract.js.map +1 -0
- package/dist/otel/context/markers.d.ts +37 -0
- package/dist/otel/context/markers.d.ts.map +1 -0
- package/dist/otel/context/markers.js +179 -0
- package/dist/otel/context/markers.js.map +1 -0
- package/dist/otel/context/util.d.ts +15 -0
- package/dist/otel/context/util.d.ts.map +1 -0
- package/dist/otel/context/util.js +33 -0
- package/dist/otel/context/util.js.map +1 -0
- package/dist/otel/daemon/ensure.d.ts +52 -0
- package/dist/otel/daemon/ensure.d.ts.map +1 -0
- package/dist/otel/daemon/ensure.js +226 -0
- package/dist/otel/daemon/ensure.js.map +1 -0
- package/dist/otel/daemon/forward.d.ts +47 -0
- package/dist/otel/daemon/forward.d.ts.map +1 -0
- package/dist/otel/daemon/forward.js +0 -0
- package/dist/otel/daemon/forward.js.map +1 -0
- package/dist/otel/daemon/paths.d.ts +24 -0
- package/dist/otel/daemon/paths.d.ts.map +1 -0
- package/dist/otel/daemon/paths.js +47 -0
- package/dist/otel/daemon/paths.js.map +1 -0
- package/dist/otel/daemon/process.d.ts +21 -0
- package/dist/otel/daemon/process.d.ts.map +1 -0
- package/dist/otel/daemon/process.js +149 -0
- package/dist/otel/daemon/process.js.map +1 -0
- package/dist/otel/daemon/reprocess.d.ts +27 -0
- package/dist/otel/daemon/reprocess.d.ts.map +1 -0
- package/dist/otel/daemon/reprocess.js +112 -0
- package/dist/otel/daemon/reprocess.js.map +1 -0
- package/dist/otel/log-handler.d.ts +37 -0
- package/dist/otel/log-handler.d.ts.map +1 -0
- package/dist/otel/log-handler.js +332 -0
- package/dist/otel/log-handler.js.map +1 -0
- package/dist/otel/metric-handler.d.ts +12 -0
- package/dist/otel/metric-handler.d.ts.map +1 -0
- package/dist/otel/metric-handler.js +18 -0
- package/dist/otel/metric-handler.js.map +1 -0
- package/dist/otel/trace-handler.d.ts +12 -0
- package/dist/otel/trace-handler.d.ts.map +1 -0
- package/dist/otel/trace-handler.js +18 -0
- package/dist/otel/trace-handler.js.map +1 -0
- package/dist/otel/types.d.ts +105 -0
- package/dist/otel/types.d.ts.map +1 -0
- package/dist/otel/types.js +15 -0
- package/dist/otel/types.js.map +1 -0
- package/dist/tui/config/schema.d.ts.map +1 -1
- package/dist/tui/config/schema.js +44 -0
- package/dist/tui/config/schema.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../../../src/otel/context/build.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AAmCH,wDAKC;AA6CD,4DAmCC;AAtHD,6CAAyC;AACzC,qDAA0D;AAC1D,2CAAoH;AAqBpH,SAAS,eAAe,CAAC,KAAa;IAClC,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG;UACrE,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;AAC1D,CAAC;AAED;;;;GAIG;AACH,SAAgB,sBAAsB,CAAC,SAAiB,EAAE,WAAmB;IACzE,MAAM,GAAG,GAAW,IAAA,wBAAU,EAAC,QAAQ,CAAC;SACnC,MAAM,CAAC,mBAAmB,SAAS,IAAI,WAAW,EAAE,CAAC;SACrD,MAAM,CAAC,KAAK,CAAC,CAAC;IACnB,OAAO,eAAe,CAAC,GAAG,CAA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B,CAAC;YACnG,aAAa,EAAE,QAAQ,CAAC,cAAc,CAAC,aAAa,EAAE,cAAc,CAAC,iBAAiB,CAAC;YACvF,cAAc,EAAE,QAAQ,CAAC,cAAc,CAAC,cAAc,EAAE,cAAc,CAAC,iBAAiB,CAAC;YACzF,oBAAoB,EAAE,QAAQ,CAAC,cAAc,CAAC,oBAAoB,EAAE,cAAc,CAAC,iBAAiB,CAAC;SACxG;QACD,WAAW,EAAE,cAAc,CAAC,WAAW;QACvC,cAAc,EAAE,cAAc,CAAC,cAAc;KAChD,CAAC;IACF,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QAChC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IACvC,CAAC;IACD,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QAChC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IACvC,CAAC;IACD,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;QAChC,KAAK,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IACvC,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IronBee — OTEL context classifier (pure).
|
|
3
|
+
*
|
|
4
|
+
* Walks a parsed Claude Code Messages API **request** body and attributes its
|
|
5
|
+
* UTF-8 bytes to context categories. Structural / wrapper bytes that aren't
|
|
6
|
+
* attributed to a specific category land in `others` (computed as the
|
|
7
|
+
* remainder), so the breakdown sums EXACTLY to `total_input_bytes`.
|
|
8
|
+
*
|
|
9
|
+
* Defensive by design: `system` / `content` may be a string or an array;
|
|
10
|
+
* unrecognized roles / block types fall through to `others`; nothing throws on
|
|
11
|
+
* an unexpected shape.
|
|
12
|
+
*/
|
|
13
|
+
/** The 11 breakdown categories (mirrors `SessionContextBreakdown` keys). */
|
|
14
|
+
export type BreakdownCategory = "system_prompt" | "builtin_tools" | "mcp_tools" | "rules" | "memory" | "skills" | "system_messages" | "user_messages" | "assistant_messages" | "tool_result" | "others";
|
|
15
|
+
export interface ContextClassification {
|
|
16
|
+
total_input_bytes: number;
|
|
17
|
+
bytes: Record<BreakdownCategory, number>;
|
|
18
|
+
/** `mcp_tools` split by MCP server name; sums to `bytes.mcp_tools`. */
|
|
19
|
+
mcp_tools_by_server: Record<string, number>;
|
|
20
|
+
/** `rules` split by source file basename; sums to `bytes.rules`. */
|
|
21
|
+
rules_by_name: Record<string, number>;
|
|
22
|
+
/** `skills` split by skill name; sums to `bytes.skills`. */
|
|
23
|
+
skills_by_name: Record<string, number>;
|
|
24
|
+
/** `tool_result` split by tool (`<server>:<tool>` / bare built-in); sums to `bytes.tool_result`. */
|
|
25
|
+
tool_results_by_name: Record<string, number>;
|
|
26
|
+
tool_counts: {
|
|
27
|
+
builtin: number;
|
|
28
|
+
mcp: number;
|
|
29
|
+
total: number;
|
|
30
|
+
};
|
|
31
|
+
message_counts: {
|
|
32
|
+
user: number;
|
|
33
|
+
assistant: number;
|
|
34
|
+
tool_result: number;
|
|
35
|
+
system: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Classify a parsed request body into the per-category byte breakdown. */
|
|
39
|
+
export declare function classifyRequestBody(body: unknown): ContextClassification;
|
|
40
|
+
//# sourceMappingURL=classify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../../../src/otel/context/classify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,4EAA4E;AAC5E,MAAM,MAAM,iBAAiB,GACvB,eAAe,GACf,eAAe,GACf,WAAW,GACX,OAAO,GACP,QAAQ,GACR,QAAQ,GACR,iBAAiB,GACjB,eAAe,GACf,oBAAoB,GACpB,aAAa,GACb,QAAQ,CAAC;AAEf,MAAM,WAAW,qBAAqB;IAClC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;IACzC,uEAAuE;IACvE,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC5C,oEAAoE;IACpE,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,4DAA4D;IAC5D,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,oGAAoG;IACpG,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7C,WAAW,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7D,cAAc,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;CAC5F;AA2ID,2EAA2E;AAC3E,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,OAAO,GAAG,qBAAqB,CA6FxE"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* IronBee — OTEL context classifier (pure).
|
|
4
|
+
*
|
|
5
|
+
* Walks a parsed Claude Code Messages API **request** body and attributes its
|
|
6
|
+
* UTF-8 bytes to context categories. Structural / wrapper bytes that aren't
|
|
7
|
+
* attributed to a specific category land in `others` (computed as the
|
|
8
|
+
* remainder), so the breakdown sums EXACTLY to `total_input_bytes`.
|
|
9
|
+
*
|
|
10
|
+
* Defensive by design: `system` / `content` may be a string or an array;
|
|
11
|
+
* unrecognized roles / block types fall through to `others`; nothing throws on
|
|
12
|
+
* an unexpected shape.
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.classifyRequestBody = classifyRequestBody;
|
|
16
|
+
const util_1 = require("./util");
|
|
17
|
+
const markers_1 = require("./markers");
|
|
18
|
+
function zeroBytes() {
|
|
19
|
+
return {
|
|
20
|
+
system_prompt: 0,
|
|
21
|
+
builtin_tools: 0,
|
|
22
|
+
mcp_tools: 0,
|
|
23
|
+
rules: 0,
|
|
24
|
+
memory: 0,
|
|
25
|
+
skills: 0,
|
|
26
|
+
system_messages: 0,
|
|
27
|
+
user_messages: 0,
|
|
28
|
+
assistant_messages: 0,
|
|
29
|
+
tool_result: 0,
|
|
30
|
+
others: 0,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function isObject(v) {
|
|
34
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
35
|
+
}
|
|
36
|
+
/** A message's content as a single string for marker scanning. */
|
|
37
|
+
function asText(content) {
|
|
38
|
+
if (typeof content === "string") {
|
|
39
|
+
return content;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
return JSON.stringify(content) ?? "";
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Add every key of `src` into `dst` (numeric accumulate). */
|
|
49
|
+
function mergeNames(dst, src) {
|
|
50
|
+
for (const key of Object.keys(src)) {
|
|
51
|
+
dst[key] = (dst[key] ?? 0) + src[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function addInjected(acc, injected) {
|
|
55
|
+
acc.bytes.rules += injected.bytes.rules;
|
|
56
|
+
acc.bytes.memory += injected.bytes.memory;
|
|
57
|
+
acc.bytes.skills += injected.bytes.skills;
|
|
58
|
+
acc.bytes.system_messages += injected.bytes.system_messages;
|
|
59
|
+
mergeNames(acc.rules_by_name, injected.rules_by_name);
|
|
60
|
+
mergeNames(acc.skills_by_name, injected.skills_by_name);
|
|
61
|
+
}
|
|
62
|
+
/** A `role:"user"` text blob: injected reminder → markers; else a real prompt. */
|
|
63
|
+
function classifyUserText(acc, text) {
|
|
64
|
+
if (text.includes("<system-reminder>")) {
|
|
65
|
+
addInjected(acc, (0, markers_1.attributeInjectedText)(text));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
acc.bytes.user_messages += (0, util_1.byteLen)(text);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function classifyUserContent(acc, content) {
|
|
72
|
+
if (typeof content === "string") {
|
|
73
|
+
classifyUserText(acc, content);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!Array.isArray(content)) {
|
|
77
|
+
return; // unknown shape → bytes fall into `others`
|
|
78
|
+
}
|
|
79
|
+
for (const block of content) {
|
|
80
|
+
if (!isObject(block)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const type = block.type;
|
|
84
|
+
if (type === "tool_result") {
|
|
85
|
+
acc.msgCounts.tool_result += 1;
|
|
86
|
+
const b = (0, util_1.jsonBytes)(block);
|
|
87
|
+
acc.bytes.tool_result += b;
|
|
88
|
+
// Attribute to the tool that produced it (id → name, joined within this
|
|
89
|
+
// body). Orphan (tool_use compacted away) → OTHER_NAME, so the map
|
|
90
|
+
// still sums EXACTLY to bytes.tool_result.
|
|
91
|
+
const useId = block.tool_use_id;
|
|
92
|
+
const raw = typeof useId === "string" ? acc.toolUseNames[useId] : undefined;
|
|
93
|
+
const name = raw !== undefined ? normalizeToolName(raw) : markers_1.OTHER_NAME;
|
|
94
|
+
acc.tool_results_by_name[name] = (acc.tool_results_by_name[name] ?? 0) + b;
|
|
95
|
+
}
|
|
96
|
+
else if (type === "text" && typeof block.text === "string") {
|
|
97
|
+
classifyUserText(acc, block.text);
|
|
98
|
+
}
|
|
99
|
+
// other block types (image, …) → `others` remainder
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** `mcp__<server>__<tool>` → `<server>`; malformed name → `OTHER_NAME`. */
|
|
103
|
+
function mcpServerName(toolName) {
|
|
104
|
+
const rest = toolName.slice("mcp__".length);
|
|
105
|
+
const sep = rest.indexOf("__");
|
|
106
|
+
return sep > 0 ? rest.slice(0, sep) : markers_1.OTHER_NAME;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Normalize a raw tool name to our own wire format (drops the `mcp__` host noise):
|
|
110
|
+
* `mcp__<server>__<tool>` → `<server>:<tool>`; built-in tools keep their bare name
|
|
111
|
+
* (`Bash`, `Read`). A malformed `mcp__` name falls back to its stripped remainder.
|
|
112
|
+
*/
|
|
113
|
+
function normalizeToolName(raw) {
|
|
114
|
+
if (!raw.startsWith("mcp__")) {
|
|
115
|
+
return raw;
|
|
116
|
+
}
|
|
117
|
+
const rest = raw.slice("mcp__".length);
|
|
118
|
+
const sep = rest.indexOf("__");
|
|
119
|
+
return sep > 0 ? `${rest.slice(0, sep)}:${rest.slice(sep + 2)}` : rest;
|
|
120
|
+
}
|
|
121
|
+
/** First pass: `tool_use_id → tool_name` from every assistant `tool_use` block in the body. */
|
|
122
|
+
function collectToolUseNames(messages) {
|
|
123
|
+
const out = {};
|
|
124
|
+
for (const msg of messages) {
|
|
125
|
+
if (!isObject(msg) || msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
for (const block of msg.content) {
|
|
129
|
+
if (isObject(block) && block.type === "tool_use" && typeof block.id === "string" && typeof block.name === "string") {
|
|
130
|
+
out[block.id] = block.name;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
/** Classify a parsed request body into the per-category byte breakdown. */
|
|
137
|
+
function classifyRequestBody(body) {
|
|
138
|
+
const acc = {
|
|
139
|
+
bytes: zeroBytes(),
|
|
140
|
+
rules_by_name: {},
|
|
141
|
+
skills_by_name: {},
|
|
142
|
+
mcp_tools_by_server: {},
|
|
143
|
+
tool_results_by_name: {},
|
|
144
|
+
toolUseNames: {},
|
|
145
|
+
msgCounts: { user: 0, assistant: 0, tool_result: 0, system: 0 },
|
|
146
|
+
};
|
|
147
|
+
const bytes = acc.bytes;
|
|
148
|
+
const toolCounts = { builtin: 0, mcp: 0, total: 0 };
|
|
149
|
+
const total = (0, util_1.jsonBytes)(body);
|
|
150
|
+
const result = () => ({
|
|
151
|
+
total_input_bytes: total,
|
|
152
|
+
bytes,
|
|
153
|
+
mcp_tools_by_server: acc.mcp_tools_by_server,
|
|
154
|
+
rules_by_name: acc.rules_by_name,
|
|
155
|
+
skills_by_name: acc.skills_by_name,
|
|
156
|
+
tool_results_by_name: acc.tool_results_by_name,
|
|
157
|
+
tool_counts: toolCounts,
|
|
158
|
+
message_counts: acc.msgCounts,
|
|
159
|
+
});
|
|
160
|
+
if (!isObject(body)) {
|
|
161
|
+
bytes.others = total;
|
|
162
|
+
return result();
|
|
163
|
+
}
|
|
164
|
+
// system[] — base prompt (string or array of blocks)
|
|
165
|
+
const system = body.system;
|
|
166
|
+
if (Array.isArray(system)) {
|
|
167
|
+
for (const block of system) {
|
|
168
|
+
bytes.system_prompt += (0, util_1.jsonBytes)(block);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (typeof system === "string") {
|
|
172
|
+
bytes.system_prompt += (0, util_1.byteLen)(system);
|
|
173
|
+
}
|
|
174
|
+
// tools[] — builtin vs mcp by name prefix; mcp also split per server
|
|
175
|
+
const tools = body.tools;
|
|
176
|
+
if (Array.isArray(tools)) {
|
|
177
|
+
for (const tool of tools) {
|
|
178
|
+
toolCounts.total += 1;
|
|
179
|
+
const name = isObject(tool) ? tool.name : undefined;
|
|
180
|
+
if (typeof name === "string" && name.startsWith("mcp__")) {
|
|
181
|
+
const b = (0, util_1.jsonBytes)(tool);
|
|
182
|
+
bytes.mcp_tools += b;
|
|
183
|
+
const server = mcpServerName(name);
|
|
184
|
+
acc.mcp_tools_by_server[server] = (acc.mcp_tools_by_server[server] ?? 0) + b;
|
|
185
|
+
toolCounts.mcp += 1;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
bytes.builtin_tools += (0, util_1.jsonBytes)(tool);
|
|
189
|
+
toolCounts.builtin += 1;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// messages[] — by role + content markers
|
|
194
|
+
const messages = body.messages;
|
|
195
|
+
if (Array.isArray(messages)) {
|
|
196
|
+
// First pass: tool_use_id → name, so tool_result bytes attribute per tool.
|
|
197
|
+
acc.toolUseNames = collectToolUseNames(messages);
|
|
198
|
+
for (const msg of messages) {
|
|
199
|
+
if (!isObject(msg)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const role = msg.role;
|
|
203
|
+
if (role === "assistant") {
|
|
204
|
+
acc.msgCounts.assistant += 1;
|
|
205
|
+
bytes.assistant_messages += (0, util_1.jsonBytes)(msg);
|
|
206
|
+
}
|
|
207
|
+
else if (role === "system") {
|
|
208
|
+
acc.msgCounts.system += 1;
|
|
209
|
+
addInjected(acc, (0, markers_1.attributeInjectedText)(asText(msg.content)));
|
|
210
|
+
}
|
|
211
|
+
else if (role === "user") {
|
|
212
|
+
acc.msgCounts.user += 1;
|
|
213
|
+
classifyUserContent(acc, msg.content);
|
|
214
|
+
}
|
|
215
|
+
// unknown role → bytes fall into `others` remainder
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// others = remainder (scaffolding + JSON structural bytes) → sums exactly.
|
|
219
|
+
let attributed = 0;
|
|
220
|
+
for (const key of Object.keys(bytes)) {
|
|
221
|
+
if (key !== "others") {
|
|
222
|
+
attributed += bytes[key];
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
bytes.others = Math.max(0, total - attributed);
|
|
226
|
+
return result();
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=classify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.js","sourceRoot":"","sources":["../../../src/otel/context/classify.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;AA4KH,kDA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from the OTLP
|
|
3
|
+
* `api_request_body` log record (+ its resource attributes). Pure: reads the
|
|
4
|
+
* event-only fields the body file lacks (`event.sequence`, `query_source`,
|
|
5
|
+
* clean `session.id`) plus the `body_ref` pointer.
|
|
6
|
+
*/
|
|
7
|
+
import { OTLPLogRecord, OTLPKeyValue } from "../types";
|
|
8
|
+
/** Per-request metadata pulled from the announcing OTLP event. */
|
|
9
|
+
export interface ApiRequestBodyMeta {
|
|
10
|
+
/** Absolute path to the `<uuid>.request.json` raw body file. */
|
|
11
|
+
body_ref: string;
|
|
12
|
+
/** OTEL `event.sequence`; `null` when absent. */
|
|
13
|
+
sequence_number: number | null;
|
|
14
|
+
/** Model id from the event attributes (`""` when absent). */
|
|
15
|
+
model: string;
|
|
16
|
+
/** `query_source` (`repl_main_thread` / `compact` / …); `null` when absent. */
|
|
17
|
+
query_source: string | null;
|
|
18
|
+
/** Untruncated body byte length (for the partial-write guard); `null` when absent. */
|
|
19
|
+
body_length: number | null;
|
|
20
|
+
/** `session.id` (record attr → resource attr); `null` when absent (body fallback applies later). */
|
|
21
|
+
session_id: string | null;
|
|
22
|
+
/** `ironbee.project_name` resource attr; `null` when absent. */
|
|
23
|
+
project_name: string | null;
|
|
24
|
+
/** Request time in epoch ms (event.timestamp → record.timeUnixNano → now). */
|
|
25
|
+
timestamp: number;
|
|
26
|
+
/**
|
|
27
|
+
* `true` only for the catch-up / reprocess path (a body file with no
|
|
28
|
+
* announcing event). The emit throttle (`otel.emitMinIntervalSeconds`) is
|
|
29
|
+
* applied to the LIVE path only — catch-up resubmits genuinely-undelivered
|
|
30
|
+
* files, so throttling them would silently drop data. Absent ⇒ live.
|
|
31
|
+
*/
|
|
32
|
+
from_catchup?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Extract metadata from an `api_request_body` record. Returns `null` when there
|
|
36
|
+
* is no `body_ref` (nothing to process — e.g. inline mode `OTEL_LOG_RAW_API_BODIES=1`).
|
|
37
|
+
*/
|
|
38
|
+
export declare function extractApiRequestBodyMeta(record: OTLPLogRecord, resourceAttrs: OTLPKeyValue[]): ApiRequestBodyMeta | null;
|
|
39
|
+
//# sourceMappingURL=extract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../../../src/otel/context/extract.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,aAAa,EAAE,YAAY,EAAgB,MAAM,UAAU,CAAC;AAErE,kEAAkE;AAClE,MAAM,WAAW,kBAAkB;IAC/B,gEAAgE;IAChE,QAAQ,EAAE,MAAM,CAAC;IACjB,iDAAiD;IACjD,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,+EAA+E;IAC/E,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,sFAAsF;IACtF,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,oGAAoG;IACpG,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,gEAAgE;IAChE,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,8EAA8E;IAC9E,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CAC1B;AAkDD;;;GAGG;AACH,wBAAgB,yBAAyB,CACrC,MAAM,EAAE,aAAa,EACrB,aAAa,EAAE,YAAY,EAAE,GAC9B,kBAAkB,GAAG,IAAI,CAgB3B"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* IronBee — extract `session_context` per-request metadata from the OTLP
|
|
4
|
+
* `api_request_body` log record (+ its resource attributes). Pure: reads the
|
|
5
|
+
* event-only fields the body file lacks (`event.sequence`, `query_source`,
|
|
6
|
+
* clean `session.id`) plus the `body_ref` pointer.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.extractApiRequestBodyMeta = extractApiRequestBodyMeta;
|
|
10
|
+
function findAttr(attrs, key) {
|
|
11
|
+
for (const kv of attrs) {
|
|
12
|
+
if (kv && kv.key === key) {
|
|
13
|
+
return kv.value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
function readStr(attrs, key) {
|
|
19
|
+
const v = findAttr(attrs, key);
|
|
20
|
+
return v && typeof v.stringValue === "string" ? v.stringValue : undefined;
|
|
21
|
+
}
|
|
22
|
+
function readNum(attrs, key) {
|
|
23
|
+
const v = findAttr(attrs, key);
|
|
24
|
+
if (!v) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
if (v.intValue !== undefined) {
|
|
28
|
+
const n = Number(v.intValue);
|
|
29
|
+
return Number.isFinite(n) ? n : undefined;
|
|
30
|
+
}
|
|
31
|
+
if (typeof v.doubleValue === "number") {
|
|
32
|
+
return v.doubleValue;
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
function resolveTimestamp(record, attrs) {
|
|
37
|
+
const iso = readStr(attrs, "event.timestamp");
|
|
38
|
+
if (iso) {
|
|
39
|
+
const ms = Date.parse(iso);
|
|
40
|
+
if (Number.isFinite(ms)) {
|
|
41
|
+
return ms;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const nano = record.timeUnixNano ?? record.observedTimeUnixNano;
|
|
45
|
+
if (nano) {
|
|
46
|
+
try {
|
|
47
|
+
return Number(BigInt(nano) / 1000000n);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// fall through
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return Date.now();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Extract metadata from an `api_request_body` record. Returns `null` when there
|
|
57
|
+
* is no `body_ref` (nothing to process — e.g. inline mode `OTEL_LOG_RAW_API_BODIES=1`).
|
|
58
|
+
*/
|
|
59
|
+
function extractApiRequestBodyMeta(record, resourceAttrs) {
|
|
60
|
+
const attrs = record.attributes ?? [];
|
|
61
|
+
const bodyRef = readStr(attrs, "body_ref");
|
|
62
|
+
if (!bodyRef) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
body_ref: bodyRef,
|
|
67
|
+
sequence_number: readNum(attrs, "event.sequence") ?? null,
|
|
68
|
+
model: readStr(attrs, "model") ?? "",
|
|
69
|
+
query_source: readStr(attrs, "query_source") ?? null,
|
|
70
|
+
body_length: readNum(attrs, "body_length") ?? null,
|
|
71
|
+
session_id: readStr(attrs, "session.id") ?? readStr(resourceAttrs, "session.id") ?? null,
|
|
72
|
+
project_name: readStr(resourceAttrs, "ironbee.project_name") ?? null,
|
|
73
|
+
timestamp: resolveTimestamp(record, attrs),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=extract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract.js","sourceRoot":"","sources":["../../../src/otel/context/extract.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAmFH,8DAmBC;AAvED,SAAS,QAAQ,CAAC,KAAqB,EAAE,GAAW;IAChD,KAAK,MAAM,EAAE,IAAI,KAAK,EAAE,CAAC;QACrB,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;YACvB,OAAO,EAAE,CAAC,KAAK,CAAC;QACpB,CAAC;IACL,CAAC;IACD,OAAO,SAAS,CAAC;AACrB,CAAC;AAED,SAAS,OAAO,CAAC,KAAqB,EAAE,GAAW;IAC/C,MAAM,CAAC,GAA6B,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IACzD,OAAO,CAAC,IAAI,OAAO,CAAC,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAA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;;;GAGG;AACH,SAAgB,yBAAyB,CACrC,MAAqB,EACrB,aAA6B;IAE7B,MAAM,KAAK,GAAmB,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC;IACtD,MAAM,OAAO,GAAuB,OAAO,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IAC/D,IAAI,CAAC,OAAO,EAAE,CAAC;QACX,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO;QACH,QAAQ,EAAE,OAAO;QACjB,eAAe,EAAE,OAAO,CAAC,KAAK,EAAE,gBAAgB,CAAC,IAAI,IAAI;QACzD,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE;QACpC,YAAY,EAAE,OAAO,CAAC,KAAK,EAAE,cAAc,CAAC,IAAI,IAAI;QACpD,WAAW,EAAE,OAAO,CAAC,KAAK,EAAE,aAAa,CAAC,IAAI,IAAI;QAClD,UAAU,EAAE,OAAO,CAAC,KAAK,EAAE,YAAY,CAAC,IAAI,OAAO,CAAC,aAAa,EAAE,YAAY,CAAC,IAAI,IAAI;QACxF,YAAY,EAAE,OAAO,CAAC,aAAa,EAAE,sBAAsB,CAAC,IAAI,IAAI;QACpE,SAAS,EAAE,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC;KAC7C,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IronBee — OTEL context classifier: injected-content markers.
|
|
3
|
+
*
|
|
4
|
+
* Host-injected context (CLAUDE.md / rules, auto-memory, the available-skills
|
|
5
|
+
* listing) rides inside `role:"user"` `<system-reminder>` text blocks AND
|
|
6
|
+
* inside `role:"system"` messages. This module splits such a text blob into
|
|
7
|
+
* its sections by marker position and attributes each section's bytes to a
|
|
8
|
+
* category.
|
|
9
|
+
*
|
|
10
|
+
* The marker set is the part most likely to drift with Claude Code versions —
|
|
11
|
+
* keep it in this one table. Matching is case-insensitive "contains". Anything
|
|
12
|
+
* before the first recognized marker (the reminder preamble) is attributed to
|
|
13
|
+
* `system_messages`.
|
|
14
|
+
*/
|
|
15
|
+
/** Category a slice of injected text maps to. */
|
|
16
|
+
export type InjectedCategory = "rules" | "memory" | "skills" | "system_messages";
|
|
17
|
+
/** Synthetic per-name key absorbing bytes that can't be attributed to a name. */
|
|
18
|
+
export declare const OTHER_NAME: string;
|
|
19
|
+
/**
|
|
20
|
+
* Per-category byte totals plus per-NAME sub-breakdowns for `rules` (by source
|
|
21
|
+
* file basename) and `skills` (by skill name). Each sub-map sums EXACTLY to its
|
|
22
|
+
* category total in `bytes` — unattributable bytes land in `OTHER_NAME`.
|
|
23
|
+
*/
|
|
24
|
+
export interface InjectedAttribution {
|
|
25
|
+
bytes: Record<InjectedCategory, number>;
|
|
26
|
+
rules_by_name: Record<string, number>;
|
|
27
|
+
skills_by_name: Record<string, number>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Split `text` into sections by marker position and return the UTF-8 bytes
|
|
31
|
+
* attributed to each {@link InjectedCategory}, plus per-name sub-breakdowns for
|
|
32
|
+
* `rules` (by file basename) and `skills` (by skill name). The preamble before
|
|
33
|
+
* the first marker → `system_messages`; each `[marker, nextMarker)` span → that
|
|
34
|
+
* marker's category. When no marker matches, the whole text → `system_messages`.
|
|
35
|
+
*/
|
|
36
|
+
export declare function attributeInjectedText(text: string): InjectedAttribution;
|
|
37
|
+
//# sourceMappingURL=markers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markers.d.ts","sourceRoot":"","sources":["../../../src/otel/context/markers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH,iDAAiD;AACjD,MAAM,MAAM,gBAAgB,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,iBAAiB,CAAC;AAEjF,iFAAiF;AACjF,eAAO,MAAM,UAAU,EAAE,MAAkB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAChC,KAAK,EAAE,MAAM,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;IACxC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC1C;AA4ID;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAoCvE"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* IronBee — OTEL context classifier: injected-content markers.
|
|
4
|
+
*
|
|
5
|
+
* Host-injected context (CLAUDE.md / rules, auto-memory, the available-skills
|
|
6
|
+
* listing) rides inside `role:"user"` `<system-reminder>` text blocks AND
|
|
7
|
+
* inside `role:"system"` messages. This module splits such a text blob into
|
|
8
|
+
* its sections by marker position and attributes each section's bytes to a
|
|
9
|
+
* category.
|
|
10
|
+
*
|
|
11
|
+
* The marker set is the part most likely to drift with Claude Code versions —
|
|
12
|
+
* keep it in this one table. Matching is case-insensitive "contains". Anything
|
|
13
|
+
* before the first recognized marker (the reminder preamble) is attributed to
|
|
14
|
+
* `system_messages`.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.OTHER_NAME = void 0;
|
|
18
|
+
exports.attributeInjectedText = attributeInjectedText;
|
|
19
|
+
const util_1 = require("./util");
|
|
20
|
+
/** Synthetic per-name key absorbing bytes that can't be attributed to a name. */
|
|
21
|
+
exports.OTHER_NAME = "[other]";
|
|
22
|
+
/** Add every key of `src` into `dst` (numeric accumulate). */
|
|
23
|
+
function mergeInto(dst, src) {
|
|
24
|
+
for (const key of Object.keys(src)) {
|
|
25
|
+
dst[key] = (dst[key] ?? 0) + src[key];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Maintained marker table (calibrated against real bodies; verified 2.1.150). */
|
|
29
|
+
const MARKERS = [
|
|
30
|
+
{ category: "rules", needles: ["# claudemd", "project instructions", "/.claude/rules/", "/claude.md"] },
|
|
31
|
+
{ category: "memory", needles: ["# memory", "memory.md", "auto-memory"] },
|
|
32
|
+
{ category: "skills", needles: ["following skills are available", "skill.md"] },
|
|
33
|
+
];
|
|
34
|
+
/** Earliest char index of any of `needles` in `lower`, or -1. */
|
|
35
|
+
function earliestNeedle(lower, needles) {
|
|
36
|
+
let best = -1;
|
|
37
|
+
for (const needle of needles) {
|
|
38
|
+
const idx = lower.indexOf(needle);
|
|
39
|
+
if (idx !== -1 && (best === -1 || idx < best)) {
|
|
40
|
+
best = idx;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return best;
|
|
44
|
+
}
|
|
45
|
+
const CONTENTS_OF = "contents of ";
|
|
46
|
+
/**
|
|
47
|
+
* `"Contents of /a/b/foo.md (project instructions…)"` → `"foo.md"` (basename,
|
|
48
|
+
* separator-agnostic). `null` when no path can be parsed (folds into the
|
|
49
|
+
* remainder). The leading `"Contents of "` is fixed-length regardless of case.
|
|
50
|
+
*/
|
|
51
|
+
function parseRuleName(segment) {
|
|
52
|
+
const after = segment.slice(CONTENTS_OF.length);
|
|
53
|
+
const parenIdx = after.indexOf(" (");
|
|
54
|
+
const pathPart = (parenIdx !== -1 ? after.slice(0, parenIdx) : after.split("\n")[0]).trim();
|
|
55
|
+
if (pathPart.length === 0) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const parts = pathPart.split(/[/\\]/);
|
|
59
|
+
const base = parts[parts.length - 1].trim();
|
|
60
|
+
return base.length > 0 ? base : null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Is the `"Contents of "` occurrence at `idx` a genuine section header rather
|
|
64
|
+
* than a prose mention inside rule body text? Real headers are always at the
|
|
65
|
+
* START of a line and carry a `" ("` (the `(project instructions…)` suffix) on
|
|
66
|
+
* that same line. This rejects mid-sentence mentions like ``a `Contents of
|
|
67
|
+
* foo.md` reference`` that would otherwise split off a phantom file.
|
|
68
|
+
*/
|
|
69
|
+
function isRuleHeaderAt(span, idx) {
|
|
70
|
+
if (idx !== 0 && span[idx - 1] !== "\n") {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const nl = span.indexOf("\n", idx);
|
|
74
|
+
const line = span.slice(idx, nl === -1 ? span.length : nl);
|
|
75
|
+
return line.includes(" (");
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Split a `rules` span by `"Contents of <path> (…)"` headers and attribute each
|
|
79
|
+
* `[header, nextHeader)` segment's bytes to that file's basename. Only genuine
|
|
80
|
+
* line-start headers split (see {@link isRuleHeaderAt}); bytes outside any
|
|
81
|
+
* header (the `# claudeMd` preamble, blank lines, prose) land in `OTHER_NAME`,
|
|
82
|
+
* so the map sums EXACTLY to `byteLen(span)`.
|
|
83
|
+
*/
|
|
84
|
+
function rulesByName(span) {
|
|
85
|
+
const out = {};
|
|
86
|
+
const lower = span.toLowerCase();
|
|
87
|
+
const starts = [];
|
|
88
|
+
let idx = lower.indexOf(CONTENTS_OF);
|
|
89
|
+
while (idx !== -1) {
|
|
90
|
+
if (isRuleHeaderAt(span, idx)) {
|
|
91
|
+
starts.push(idx);
|
|
92
|
+
}
|
|
93
|
+
idx = lower.indexOf(CONTENTS_OF, idx + CONTENTS_OF.length);
|
|
94
|
+
}
|
|
95
|
+
let named = 0;
|
|
96
|
+
for (let i = 0; i < starts.length; i++) {
|
|
97
|
+
const segEnd = i + 1 < starts.length ? starts[i + 1] : span.length;
|
|
98
|
+
const segment = span.slice(starts[i], segEnd);
|
|
99
|
+
const name = parseRuleName(segment);
|
|
100
|
+
if (name) {
|
|
101
|
+
const b = (0, util_1.byteLen)(segment);
|
|
102
|
+
out[name] = (out[name] ?? 0) + b;
|
|
103
|
+
named += b;
|
|
104
|
+
}
|
|
105
|
+
// unparseable header segment → absorbed by the remainder below
|
|
106
|
+
}
|
|
107
|
+
const other = (0, util_1.byteLen)(span) - named;
|
|
108
|
+
if (other > 0) {
|
|
109
|
+
out[exports.OTHER_NAME] = (out[exports.OTHER_NAME] ?? 0) + other;
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
const SKILL_LINE = /^\s*-\s+([^:]+):/;
|
|
114
|
+
/**
|
|
115
|
+
* Attribute a `skills` span's bytes to each skill listed as `- <name>: <desc>`.
|
|
116
|
+
* The header line + blank lines + dropped newline bytes land in `OTHER_NAME`, so
|
|
117
|
+
* the map sums EXACTLY to `byteLen(span)`.
|
|
118
|
+
*/
|
|
119
|
+
function skillsByName(span) {
|
|
120
|
+
const out = {};
|
|
121
|
+
let named = 0;
|
|
122
|
+
for (const line of span.split("\n")) {
|
|
123
|
+
const m = line.match(SKILL_LINE);
|
|
124
|
+
if (m) {
|
|
125
|
+
const name = m[1].trim();
|
|
126
|
+
if (name.length > 0) {
|
|
127
|
+
const b = (0, util_1.byteLen)(line);
|
|
128
|
+
out[name] = (out[name] ?? 0) + b;
|
|
129
|
+
named += b;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const other = (0, util_1.byteLen)(span) - named;
|
|
134
|
+
if (other > 0) {
|
|
135
|
+
out[exports.OTHER_NAME] = (out[exports.OTHER_NAME] ?? 0) + other;
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Split `text` into sections by marker position and return the UTF-8 bytes
|
|
141
|
+
* attributed to each {@link InjectedCategory}, plus per-name sub-breakdowns for
|
|
142
|
+
* `rules` (by file basename) and `skills` (by skill name). The preamble before
|
|
143
|
+
* the first marker → `system_messages`; each `[marker, nextMarker)` span → that
|
|
144
|
+
* marker's category. When no marker matches, the whole text → `system_messages`.
|
|
145
|
+
*/
|
|
146
|
+
function attributeInjectedText(text) {
|
|
147
|
+
const bytes = { rules: 0, memory: 0, skills: 0, system_messages: 0 };
|
|
148
|
+
const out = { bytes, rules_by_name: {}, skills_by_name: {} };
|
|
149
|
+
const lower = text.toLowerCase();
|
|
150
|
+
const hits = [];
|
|
151
|
+
for (const marker of MARKERS) {
|
|
152
|
+
const start = earliestNeedle(lower, marker.needles);
|
|
153
|
+
if (start !== -1) {
|
|
154
|
+
hits.push({ category: marker.category, start });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (hits.length === 0) {
|
|
158
|
+
bytes.system_messages += (0, util_1.byteLen)(text);
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
hits.sort((a, b) => a.start - b.start);
|
|
162
|
+
// Preamble before the first marker (reminder wrapper) → system_messages.
|
|
163
|
+
bytes.system_messages += (0, util_1.byteLen)(text.slice(0, hits[0].start));
|
|
164
|
+
for (let i = 0; i < hits.length; i++) {
|
|
165
|
+
const start = hits[i].start;
|
|
166
|
+
const end = i + 1 < hits.length ? hits[i + 1].start : text.length;
|
|
167
|
+
const span = text.slice(start, end);
|
|
168
|
+
const category = hits[i].category;
|
|
169
|
+
bytes[category] += (0, util_1.byteLen)(span);
|
|
170
|
+
if (category === "rules") {
|
|
171
|
+
mergeInto(out.rules_by_name, rulesByName(span));
|
|
172
|
+
}
|
|
173
|
+
else if (category === "skills") {
|
|
174
|
+
mergeInto(out.skills_by_name, skillsByName(span));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=markers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markers.js","sourceRoot":"","sources":["../../../src/otel/context/markers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AAsKH,sDAoCC;AAxMD,iCAAiC;AAKjC,iFAAiF;AACpE,QAAA,UAAU,GAAW,SAAS,CAAC;AAa5C,8DAA8D;AAC9D,SAAS,SAAS,CAAC,GAA2B,EAAE,GAA2B;IACvE,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,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