@robota-sdk/agent-session-analytics 3.0.0-beta.76

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Robota Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @robota-sdk/agent-session-analytics
2
+
3
+ Session-log timing analysis and reporting for the Robota SDK.
4
+
5
+ Given persisted session records, it computes per-turn timing intervals (LLM wait vs. tool
6
+ execution), aggregates across sessions, and renders text reports. Pure functions — no file I/O, no
7
+ `process.*`, no CLI concerns; callers load records and write output.
8
+
9
+ ## API
10
+
11
+ | Export | Description |
12
+ | --------------------------- | ---------------------------------------------------- |
13
+ | `analyzeSession(record)` | Compute the timing report for one session record |
14
+ | `aggregateReports(reports)` | Aggregate multiple single-session reports |
15
+ | `computeTimingIntervals(h)` | Classify a history array into timing intervals |
16
+ | `gapMs(from, to)` | Millisecond gap between two timestamps (string/Date) |
17
+ | `formatSingleSession(r)` | Render a single-session report as text |
18
+ | `formatAggregateReport(a)` | Render an aggregate report as text |
19
+
20
+ Operates on the canonical `IInteractiveSessionRecord` projection and `IHistoryEntry` — owns no
21
+ duplicate record/history types.
22
+
23
+ See [docs/SPEC.md](./docs/SPEC.md) for the full contract.
@@ -0,0 +1,3 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e,t){return new Date(t).getTime()-new Date(e).getTime()}function t(e){return new Date(e).toISOString()}function n(n,r,i,a,o,s){return{kind:n,fromType:r,toType:i,fromTimestamp:t(a),toTimestamp:t(o),durationMs:e(a,o),turnIndex:s}}function r(e){let t=[],r=0,i=0;for(;i<e.length;){let a=e[i];if(!a){i++;continue}if(a.category===`chat`&&a.type===`user`){r++;let o=a.timestamp,s=!1,c=null,l=i+1;for(;l<e.length;){let i=e[l];if(!i){l++;continue}if(i.category===`chat`&&i.type===`user`)break;if(i.category===`event`&&i.type===`tool-start`){s?c!==null&&t.push(n(`llm_between_tools`,`tool-end`,`tool-start`,c,i.timestamp,r)):(t.push(n(`user_to_first_tool`,`user`,`tool-start`,o,i.timestamp,r)),s=!0),c=null,l++;continue}if(i.category===`event`&&i.type===`tool-end`){let a=e[l-1];a&&a.category===`event`&&a.type===`tool-start`&&t.push(n(`tool_exec`,`tool-start`,`tool-end`,a.timestamp,i.timestamp,r)),c=i.timestamp,l++;continue}if(i.category===`event`&&i.type===`tool-summary`){c!==null&&t.push(n(`llm_final_response`,`tool-end`,`tool-summary`,c,i.timestamp,r)),l++;continue}if(i.category===`chat`&&i.type===`assistant`){s?c!==null&&t.push(n(`llm_final_response`,`tool-end`,`assistant`,c,i.timestamp,r)):t.push(n(`user_to_assistant`,`user`,`assistant`,o,i.timestamp,r)),l++;break}l++}i=l}else i++}return t}function i(e){let t=e.filter(e=>e.kind===`user_to_first_tool`||e.kind===`user_to_assistant`||e.kind===`llm_between_tools`||e.kind===`llm_final_response`),n=e.filter(e=>e.kind===`tool_exec`),r=e.filter(e=>e.kind===`user_to_assistant`),i=e=>e.length?Math.round(e.reduce((e,t)=>e+t.durationMs,0)/e.length):0,a=e=>e.length?Math.max(...e.map(e=>e.durationMs)):0;return{llmWaitMs:{avg:i(t),max:a(t),total:t.reduce((e,t)=>e+t.durationMs,0),count:t.length},toolExecMs:{avg:i(n),max:a(n),median:(e=>{if(!e.length)return 0;let t=[...e].sort((e,t)=>e.durationMs-t.durationMs),n=Math.floor(t.length/2);return t.length%2==0?Math.round(((t[n-1]?.durationMs??0)+(t[n]?.durationMs??0))/2):t[n]?.durationMs??0})(n),count:n.length},userToAssistantMs:{avg:i(r),max:a(r),count:r.length}}}function a(e){let t=r(e.history??[]),n=t.filter(e=>e.durationMs>=1e4);return{sessionId:e.id,cwd:e.cwd,createdAt:e.createdAt,totalIntervals:t.length,intervals:t,slowIntervals:n,stats:i(t)}}function o(e){let t=e.flatMap(e=>e.intervals.filter(e=>e.kind===`user_to_first_tool`||e.kind===`user_to_assistant`||e.kind===`llm_between_tools`||e.kind===`llm_final_response`)),n=e.flatMap(e=>e.intervals.filter(e=>e.kind===`tool_exec`)),r=t.length?Math.round(t.reduce((e,t)=>e+t.durationMs,0)/t.length):0,i=n.length?Math.round(n.reduce((e,t)=>e+t.durationMs,0)/n.length):0,a=0,o=``,s=0,c=``;for(let t of e)for(let e of t.intervals)e.durationMs>a&&(a=e.durationMs,o=t.sessionId,s=e.turnIndex,c=e.kind);let l=e.map(e=>e.createdAt).sort();return{sessionCount:e.length,fromDate:l[0]??``,toDate:l[l.length-1]??``,avgLlmResponseMs:r,avgToolExecMs:i,maxSingleDelayMs:a,maxSingleDelaySession:o,maxSingleDelayTurn:s,maxSingleDelayKind:c}}function s(e){return e>=1e3?`${(e/1e3).toFixed(1)}s`:`${e}ms`}function c(e){return new Date(e).toLocaleString(`sv-SE`,{timeZoneName:`short`}).slice(0,16)}function l(e){return{user_to_first_tool:`user → tool-start`,user_to_assistant:`user → assistant`,tool_exec:`tool-start → tool-end`,llm_between_tools:`tool-end → tool-start`,llm_final_response:`tool-end → assistant/summary`}[e.kind]??e.kind}function u(e){let{stats:t}=e,n=t.llmWaitMs.total+t.toolExecMs.avg*t.toolExecMs.count;if(n===0)return`Verdict: No timing data available.`;let r=Math.round(t.llmWaitMs.total/n*100);return`Verdict: LLM API wait ${r}% | Code processing ${100-r}%`}function d(e){let t=[],{stats:n}=e;if(t.push(`Session: ${e.sessionId}`),t.push(`Created: ${c(e.createdAt)} | cwd: ${e.cwd}`),t.push(``),t.push(`Timing Summary:`),n.llmWaitMs.count>0&&t.push(` LLM API wait ${s(n.llmWaitMs.avg).padStart(8)} avg | ${s(n.llmWaitMs.max).padStart(8)} max`),n.toolExecMs.count>0&&t.push(` Tool execution ${s(n.toolExecMs.avg).padStart(8)} avg | ${s(n.toolExecMs.median).padStart(8)} median`),n.userToAssistantMs.count>0&&t.push(` user → assistant ${s(n.userToAssistantMs.avg).padStart(8)} avg`),n.llmWaitMs.count===0&&n.toolExecMs.count===0&&t.push(` (no intervals computed — session may have no history)`),e.slowIntervals.length>0){t.push(``),t.push(`Slow intervals (>10s):`);for(let n of e.slowIntervals)t.push(` turn ${n.turnIndex} ${l(n).padEnd(32)} ${s(n.durationMs).padStart(8)}`)}return t.push(``),t.push(u(e)),t.join(`
2
+ `)}function f(e){let t=[];return t.push(`Analyzed ${e.sessionCount} sessions (${c(e.fromDate)} ~ ${c(e.toDate)})`),t.push(` Avg LLM response: ${s(e.avgLlmResponseMs)}`),t.push(` Avg tool exec: ${s(e.avgToolExecMs)}`),e.maxSingleDelayMs>0&&t.push(` Max single delay: ${s(e.maxSingleDelayMs)} (${e.maxSingleDelaySession}, turn ${e.maxSingleDelayTurn}, ${e.maxSingleDelayKind})`),t.join(`
3
+ `)}exports.aggregateReports=o,exports.analyzeSession=a,exports.computeTimingIntervals=r,exports.formatAggregateReport=f,exports.formatSingleSession=d,exports.gapMs=e;
@@ -0,0 +1,77 @@
1
+ import { IInteractiveSessionRecord } from "@robota-sdk/agent-interface-transport";
2
+ import { IHistoryEntry } from "@robota-sdk/agent-core";
3
+
4
+ //#region src/types.d.ts
5
+ /** Minimal session-record projection the analyzer reads — SSOT-derived, not a duplicate. */
6
+ type ISessionAnalysisInput = Pick<IInteractiveSessionRecord, 'id' | 'cwd' | 'createdAt' | 'history'>;
7
+ type TIntervalKind = 'user_to_first_tool' | 'user_to_assistant' | 'tool_exec' | 'llm_between_tools' | 'llm_final_response';
8
+ interface ITimingInterval {
9
+ kind: TIntervalKind;
10
+ fromType: string;
11
+ toType: string;
12
+ fromTimestamp: string;
13
+ toTimestamp: string;
14
+ durationMs: number;
15
+ turnIndex: number;
16
+ }
17
+ interface ITimingStats {
18
+ llmWaitMs: {
19
+ avg: number;
20
+ max: number;
21
+ total: number;
22
+ count: number;
23
+ };
24
+ toolExecMs: {
25
+ avg: number;
26
+ max: number;
27
+ median: number;
28
+ count: number;
29
+ };
30
+ userToAssistantMs: {
31
+ avg: number;
32
+ max: number;
33
+ count: number;
34
+ };
35
+ }
36
+ interface ISessionTimingReport {
37
+ sessionId: string;
38
+ cwd: string;
39
+ createdAt: string;
40
+ totalIntervals: number;
41
+ intervals: ITimingInterval[];
42
+ slowIntervals: ITimingInterval[];
43
+ stats: ITimingStats;
44
+ }
45
+ interface IAggregateReport {
46
+ sessionCount: number;
47
+ fromDate: string;
48
+ toDate: string;
49
+ avgLlmResponseMs: number;
50
+ avgToolExecMs: number;
51
+ maxSingleDelayMs: number;
52
+ maxSingleDelaySession: string;
53
+ maxSingleDelayTurn: number;
54
+ maxSingleDelayKind: string;
55
+ }
56
+ //#endregion
57
+ //#region src/analyze.d.ts
58
+ /** Gap in milliseconds between two timestamps (ISO string or Date). */
59
+ declare function gapMs(from: string | Date, to: string | Date): number;
60
+ /**
61
+ * Compute timing intervals from a history entry array.
62
+ *
63
+ * Strategy: scan the timeline turn by turn. A "turn" starts with a user message. After user, we
64
+ * expect zero or more tool-start → tool-end cycles, optionally a tool-summary, then an assistant
65
+ * message.
66
+ */
67
+ declare function computeTimingIntervals(history: readonly IHistoryEntry[]): ITimingInterval[];
68
+ /** Compute the timing report for a single session record. */
69
+ declare function analyzeSession(record: ISessionAnalysisInput): ISessionTimingReport;
70
+ /** Aggregate multiple single-session reports into a fleet-level summary. */
71
+ declare function aggregateReports(reports: readonly ISessionTimingReport[]): IAggregateReport;
72
+ //#endregion
73
+ //#region src/report.d.ts
74
+ declare function formatSingleSession(report: ISessionTimingReport): string;
75
+ declare function formatAggregateReport(aggregate: IAggregateReport): string;
76
+ //#endregion
77
+ export { type IAggregateReport, type ISessionAnalysisInput, type ISessionTimingReport, type ITimingInterval, type ITimingStats, type TIntervalKind, aggregateReports, analyzeSession, computeTimingIntervals, formatAggregateReport, formatSingleSession, gapMs };
@@ -0,0 +1,3 @@
1
+ function e(e,t){return new Date(t).getTime()-new Date(e).getTime()}function t(e){return new Date(e).toISOString()}function n(n,r,i,a,o,s){return{kind:n,fromType:r,toType:i,fromTimestamp:t(a),toTimestamp:t(o),durationMs:e(a,o),turnIndex:s}}function r(e){let t=[],r=0,i=0;for(;i<e.length;){let a=e[i];if(!a){i++;continue}if(a.category===`chat`&&a.type===`user`){r++;let o=a.timestamp,s=!1,c=null,l=i+1;for(;l<e.length;){let i=e[l];if(!i){l++;continue}if(i.category===`chat`&&i.type===`user`)break;if(i.category===`event`&&i.type===`tool-start`){s?c!==null&&t.push(n(`llm_between_tools`,`tool-end`,`tool-start`,c,i.timestamp,r)):(t.push(n(`user_to_first_tool`,`user`,`tool-start`,o,i.timestamp,r)),s=!0),c=null,l++;continue}if(i.category===`event`&&i.type===`tool-end`){let a=e[l-1];a&&a.category===`event`&&a.type===`tool-start`&&t.push(n(`tool_exec`,`tool-start`,`tool-end`,a.timestamp,i.timestamp,r)),c=i.timestamp,l++;continue}if(i.category===`event`&&i.type===`tool-summary`){c!==null&&t.push(n(`llm_final_response`,`tool-end`,`tool-summary`,c,i.timestamp,r)),l++;continue}if(i.category===`chat`&&i.type===`assistant`){s?c!==null&&t.push(n(`llm_final_response`,`tool-end`,`assistant`,c,i.timestamp,r)):t.push(n(`user_to_assistant`,`user`,`assistant`,o,i.timestamp,r)),l++;break}l++}i=l}else i++}return t}function i(e){let t=e.filter(e=>e.kind===`user_to_first_tool`||e.kind===`user_to_assistant`||e.kind===`llm_between_tools`||e.kind===`llm_final_response`),n=e.filter(e=>e.kind===`tool_exec`),r=e.filter(e=>e.kind===`user_to_assistant`),i=e=>e.length?Math.round(e.reduce((e,t)=>e+t.durationMs,0)/e.length):0,a=e=>e.length?Math.max(...e.map(e=>e.durationMs)):0;return{llmWaitMs:{avg:i(t),max:a(t),total:t.reduce((e,t)=>e+t.durationMs,0),count:t.length},toolExecMs:{avg:i(n),max:a(n),median:(e=>{if(!e.length)return 0;let t=[...e].sort((e,t)=>e.durationMs-t.durationMs),n=Math.floor(t.length/2);return t.length%2==0?Math.round(((t[n-1]?.durationMs??0)+(t[n]?.durationMs??0))/2):t[n]?.durationMs??0})(n),count:n.length},userToAssistantMs:{avg:i(r),max:a(r),count:r.length}}}function a(e){let t=r(e.history??[]),n=t.filter(e=>e.durationMs>=1e4);return{sessionId:e.id,cwd:e.cwd,createdAt:e.createdAt,totalIntervals:t.length,intervals:t,slowIntervals:n,stats:i(t)}}function o(e){let t=e.flatMap(e=>e.intervals.filter(e=>e.kind===`user_to_first_tool`||e.kind===`user_to_assistant`||e.kind===`llm_between_tools`||e.kind===`llm_final_response`)),n=e.flatMap(e=>e.intervals.filter(e=>e.kind===`tool_exec`)),r=t.length?Math.round(t.reduce((e,t)=>e+t.durationMs,0)/t.length):0,i=n.length?Math.round(n.reduce((e,t)=>e+t.durationMs,0)/n.length):0,a=0,o=``,s=0,c=``;for(let t of e)for(let e of t.intervals)e.durationMs>a&&(a=e.durationMs,o=t.sessionId,s=e.turnIndex,c=e.kind);let l=e.map(e=>e.createdAt).sort();return{sessionCount:e.length,fromDate:l[0]??``,toDate:l[l.length-1]??``,avgLlmResponseMs:r,avgToolExecMs:i,maxSingleDelayMs:a,maxSingleDelaySession:o,maxSingleDelayTurn:s,maxSingleDelayKind:c}}function s(e){return e>=1e3?`${(e/1e3).toFixed(1)}s`:`${e}ms`}function c(e){return new Date(e).toLocaleString(`sv-SE`,{timeZoneName:`short`}).slice(0,16)}function l(e){return{user_to_first_tool:`user → tool-start`,user_to_assistant:`user → assistant`,tool_exec:`tool-start → tool-end`,llm_between_tools:`tool-end → tool-start`,llm_final_response:`tool-end → assistant/summary`}[e.kind]??e.kind}function u(e){let{stats:t}=e,n=t.llmWaitMs.total+t.toolExecMs.avg*t.toolExecMs.count;if(n===0)return`Verdict: No timing data available.`;let r=Math.round(t.llmWaitMs.total/n*100);return`Verdict: LLM API wait ${r}% | Code processing ${100-r}%`}function d(e){let t=[],{stats:n}=e;if(t.push(`Session: ${e.sessionId}`),t.push(`Created: ${c(e.createdAt)} | cwd: ${e.cwd}`),t.push(``),t.push(`Timing Summary:`),n.llmWaitMs.count>0&&t.push(` LLM API wait ${s(n.llmWaitMs.avg).padStart(8)} avg | ${s(n.llmWaitMs.max).padStart(8)} max`),n.toolExecMs.count>0&&t.push(` Tool execution ${s(n.toolExecMs.avg).padStart(8)} avg | ${s(n.toolExecMs.median).padStart(8)} median`),n.userToAssistantMs.count>0&&t.push(` user → assistant ${s(n.userToAssistantMs.avg).padStart(8)} avg`),n.llmWaitMs.count===0&&n.toolExecMs.count===0&&t.push(` (no intervals computed — session may have no history)`),e.slowIntervals.length>0){t.push(``),t.push(`Slow intervals (>10s):`);for(let n of e.slowIntervals)t.push(` turn ${n.turnIndex} ${l(n).padEnd(32)} ${s(n.durationMs).padStart(8)}`)}return t.push(``),t.push(u(e)),t.join(`
2
+ `)}function f(e){let t=[];return t.push(`Analyzed ${e.sessionCount} sessions (${c(e.fromDate)} ~ ${c(e.toDate)})`),t.push(` Avg LLM response: ${s(e.avgLlmResponseMs)}`),t.push(` Avg tool exec: ${s(e.avgToolExecMs)}`),e.maxSingleDelayMs>0&&t.push(` Max single delay: ${s(e.maxSingleDelayMs)} (${e.maxSingleDelaySession}, turn ${e.maxSingleDelayTurn}, ${e.maxSingleDelayKind})`),t.join(`
3
+ `)}export{o as aggregateReports,a as analyzeSession,r as computeTimingIntervals,f as formatAggregateReport,d as formatSingleSession,e as gapMs};
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@robota-sdk/agent-session-analytics",
3
+ "version": "3.0.0-beta.76",
4
+ "description": "Session-log timing analysis and reporting for the Robota SDK (analyzeSession, aggregateReports, report formatters)",
5
+ "type": "module",
6
+ "main": "dist/node/index.js",
7
+ "types": "dist/node/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/node/index.d.ts",
11
+ "source": "./src/index.ts",
12
+ "node": {
13
+ "import": "./dist/node/index.js",
14
+ "require": "./dist/node/index.cjs"
15
+ },
16
+ "default": {
17
+ "import": "./dist/node/index.js",
18
+ "require": "./dist/node/index.cjs"
19
+ }
20
+ }
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/woojubb/robota.git"
25
+ },
26
+ "homepage": "https://robota.io/",
27
+ "bugs": {
28
+ "url": "https://github.com/woojubb/robota/issues"
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "dependencies": {
34
+ "@robota-sdk/agent-core": "3.0.0-beta.76",
35
+ "@robota-sdk/agent-interface-transport": "3.0.0-beta.76"
36
+ },
37
+ "devDependencies": {
38
+ "rimraf": "^5.0.10",
39
+ "tsdown": "^0.22.2",
40
+ "typescript": "^5.9.3",
41
+ "vitest": "^3.2.6"
42
+ },
43
+ "license": "MIT",
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "scripts": {
48
+ "build": "tsdown",
49
+ "build:js": "tsdown --no-dts",
50
+ "build:types": "tsdown --dts",
51
+ "test": "vitest run --passWithNoTests",
52
+ "test:coverage": "vitest run --coverage --passWithNoTests",
53
+ "typecheck": "tsc --noEmit",
54
+ "lint": "eslint src/ --ext .ts",
55
+ "clean": "rimraf dist"
56
+ }
57
+ }