octocode-shared 1.0.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/dist/credentials/index.d.ts +6 -0
- package/dist/credentials/index.d.ts.map +1 -0
- package/dist/credentials/index.js +42 -0
- package/dist/credentials/keychain.d.ts +52 -0
- package/dist/credentials/keychain.d.ts.map +1 -0
- package/dist/credentials/storage.d.ts +367 -0
- package/dist/credentials/storage.d.ts.map +1 -0
- package/dist/credentials/types.d.ts +61 -0
- package/dist/credentials/types.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -0
- package/dist/platform/index.d.ts +5 -0
- package/dist/platform/index.d.ts.map +1 -0
- package/dist/platform/index.js +11 -0
- package/dist/platform/platform.d.ts +30 -0
- package/dist/platform/platform.d.ts.map +1 -0
- package/dist/platform-1V_81nPi.js +37 -0
- package/dist/session/index.d.ts +12 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +18 -0
- package/dist/session/storage.d.ts +85 -0
- package/dist/session/storage.d.ts.map +1 -0
- package/dist/session/types.d.ts +44 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/storage-D-QEqQEn.js +244 -0
- package/dist/storage-DuH3rTiu.js +737 -0
- package/package.json +82 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform Detection & Utilities
|
|
3
|
+
*
|
|
4
|
+
* Cross-platform utilities for octocode packages.
|
|
5
|
+
*/
|
|
6
|
+
/** Check if running on Windows */
|
|
7
|
+
export declare const isWindows: boolean;
|
|
8
|
+
/** Check if running on macOS */
|
|
9
|
+
export declare const isMac: boolean;
|
|
10
|
+
/** Check if running on Linux */
|
|
11
|
+
export declare const isLinux: boolean;
|
|
12
|
+
/** User's home directory */
|
|
13
|
+
export declare const HOME: string;
|
|
14
|
+
/**
|
|
15
|
+
* Get the AppData path on Windows, HOME on other platforms
|
|
16
|
+
*/
|
|
17
|
+
export declare function getAppDataPath(): string;
|
|
18
|
+
/**
|
|
19
|
+
* Get the local AppData path on Windows, HOME on other platforms
|
|
20
|
+
*/
|
|
21
|
+
export declare function getLocalAppDataPath(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Get platform name for display
|
|
24
|
+
*/
|
|
25
|
+
export declare function getPlatformName(): string;
|
|
26
|
+
/**
|
|
27
|
+
* Get architecture for display
|
|
28
|
+
*/
|
|
29
|
+
export declare function getArchitecture(): string;
|
|
30
|
+
//# sourceMappingURL=platform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../../src/platform/platform.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,kCAAkC;AAClC,eAAO,MAAM,SAAS,EAAE,OAAmC,CAAC;AAE5D,gCAAgC;AAChC,eAAO,MAAM,KAAK,EAAE,OAAoC,CAAC;AAEzD,gCAAgC;AAChC,eAAO,MAAM,OAAO,EAAE,OAAmC,CAAC;AAE1D,4BAA4B;AAC5B,eAAO,MAAM,IAAI,EAAE,MAAqB,CAAC;AAEzC;;GAEG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAKvC;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,CAK5C;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAKxC;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
const isWindows = os.platform() === "win32";
|
|
4
|
+
const isMac = os.platform() === "darwin";
|
|
5
|
+
const isLinux = os.platform() === "linux";
|
|
6
|
+
const HOME = os.homedir();
|
|
7
|
+
function getAppDataPath() {
|
|
8
|
+
if (isWindows) {
|
|
9
|
+
return process.env.APPDATA || path.join(HOME, "AppData", "Roaming");
|
|
10
|
+
}
|
|
11
|
+
return HOME;
|
|
12
|
+
}
|
|
13
|
+
function getLocalAppDataPath() {
|
|
14
|
+
if (isWindows) {
|
|
15
|
+
return process.env.LOCALAPPDATA || path.join(HOME, "AppData", "Local");
|
|
16
|
+
}
|
|
17
|
+
return HOME;
|
|
18
|
+
}
|
|
19
|
+
function getPlatformName() {
|
|
20
|
+
if (isMac) return "macOS";
|
|
21
|
+
if (isWindows) return "Windows";
|
|
22
|
+
if (isLinux) return "Linux";
|
|
23
|
+
return os.platform();
|
|
24
|
+
}
|
|
25
|
+
function getArchitecture() {
|
|
26
|
+
return os.arch();
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
HOME as H,
|
|
30
|
+
isMac as a,
|
|
31
|
+
isLinux as b,
|
|
32
|
+
getLocalAppDataPath as c,
|
|
33
|
+
getPlatformName as d,
|
|
34
|
+
getArchitecture as e,
|
|
35
|
+
getAppDataPath as g,
|
|
36
|
+
isWindows as i
|
|
37
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Module
|
|
3
|
+
*
|
|
4
|
+
* Persistent session management for Octocode packages.
|
|
5
|
+
* Stores session data in ~/.octocode/session.json with cross-platform support.
|
|
6
|
+
*/
|
|
7
|
+
export type { PersistedSession, SessionStats, SessionUpdateResult, SessionOptions, } from './types.js';
|
|
8
|
+
export { SESSION_FILE } from './storage.js';
|
|
9
|
+
export { readSession, writeSession, getOrCreateSession, getSessionId, deleteSession, flushSession, flushSessionSync, } from './storage.js';
|
|
10
|
+
export { updateSessionStats, incrementToolCalls, incrementPromptCalls, incrementErrors, incrementRateLimits, resetSessionStats, } from './storage.js';
|
|
11
|
+
export { _resetSessionState } from './storage.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,YAAY,EACV,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,cAAc,GACf,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG5C,OAAO,EACL,WAAW,EACX,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,gBAAgB,GACjB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,eAAe,EACf,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { S, _, d, f, b, g, a, e, c, h, i, r, j, u, w } from "../storage-D-QEqQEn.js";
|
|
2
|
+
export {
|
|
3
|
+
S as SESSION_FILE,
|
|
4
|
+
_ as _resetSessionState,
|
|
5
|
+
d as deleteSession,
|
|
6
|
+
f as flushSession,
|
|
7
|
+
b as flushSessionSync,
|
|
8
|
+
g as getOrCreateSession,
|
|
9
|
+
a as getSessionId,
|
|
10
|
+
e as incrementErrors,
|
|
11
|
+
c as incrementPromptCalls,
|
|
12
|
+
h as incrementRateLimits,
|
|
13
|
+
i as incrementToolCalls,
|
|
14
|
+
r as readSession,
|
|
15
|
+
j as resetSessionStats,
|
|
16
|
+
u as updateSessionStats,
|
|
17
|
+
w as writeSession
|
|
18
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Storage
|
|
3
|
+
*
|
|
4
|
+
* Persistent session storage in ~/.octocode/session.json
|
|
5
|
+
* Cross-platform support for Windows, Linux, and macOS.
|
|
6
|
+
*
|
|
7
|
+
* Uses batch saving to reduce disk I/O - changes are buffered in memory
|
|
8
|
+
* and flushed to disk every 60 seconds or on process exit.
|
|
9
|
+
*/
|
|
10
|
+
import type { PersistedSession, SessionStats, SessionUpdateResult, SessionOptions } from './types.js';
|
|
11
|
+
export declare const SESSION_FILE: string;
|
|
12
|
+
/**
|
|
13
|
+
* Read session (from cache or disk)
|
|
14
|
+
* @returns The persisted session or null if not found/invalid
|
|
15
|
+
*/
|
|
16
|
+
export declare function readSession(): PersistedSession | null;
|
|
17
|
+
/**
|
|
18
|
+
* Write session (to cache, batched to disk)
|
|
19
|
+
* Changes are buffered and flushed every 60 seconds or on process exit.
|
|
20
|
+
*/
|
|
21
|
+
export declare function writeSession(session: PersistedSession): void;
|
|
22
|
+
/**
|
|
23
|
+
* Flush session to disk immediately
|
|
24
|
+
* Use this when you need to ensure data is persisted (e.g., before critical operations)
|
|
25
|
+
*/
|
|
26
|
+
export declare function flushSession(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Flush session to disk synchronously (for exit handlers)
|
|
29
|
+
*/
|
|
30
|
+
export declare function flushSessionSync(): void;
|
|
31
|
+
/**
|
|
32
|
+
* Get or create a session
|
|
33
|
+
* - If session exists and is valid, update lastActiveAt and return it
|
|
34
|
+
* - If session doesn't exist or is invalid, create a new one
|
|
35
|
+
*
|
|
36
|
+
* @param options - Session options (forceNew to create fresh session)
|
|
37
|
+
* @returns The persisted session
|
|
38
|
+
*/
|
|
39
|
+
export declare function getOrCreateSession(options?: SessionOptions): PersistedSession;
|
|
40
|
+
/**
|
|
41
|
+
* Get the current session ID without modifying the session
|
|
42
|
+
* @returns The session ID or null if no session exists
|
|
43
|
+
*/
|
|
44
|
+
export declare function getSessionId(): string | null;
|
|
45
|
+
/**
|
|
46
|
+
* Update session statistics
|
|
47
|
+
* Increments the specified stat counters (batched to disk)
|
|
48
|
+
*
|
|
49
|
+
* @param updates - Partial stats to increment
|
|
50
|
+
* @returns Result with success status and updated session
|
|
51
|
+
*/
|
|
52
|
+
export declare function updateSessionStats(updates: Partial<SessionStats>): SessionUpdateResult;
|
|
53
|
+
/**
|
|
54
|
+
* Increment tool call counter (batched)
|
|
55
|
+
*/
|
|
56
|
+
export declare function incrementToolCalls(count?: number): SessionUpdateResult;
|
|
57
|
+
/**
|
|
58
|
+
* Increment prompt call counter (batched)
|
|
59
|
+
*/
|
|
60
|
+
export declare function incrementPromptCalls(count?: number): SessionUpdateResult;
|
|
61
|
+
/**
|
|
62
|
+
* Increment error counter (batched)
|
|
63
|
+
*/
|
|
64
|
+
export declare function incrementErrors(count?: number): SessionUpdateResult;
|
|
65
|
+
/**
|
|
66
|
+
* Increment rate limit counter (batched)
|
|
67
|
+
*/
|
|
68
|
+
export declare function incrementRateLimits(count?: number): SessionUpdateResult;
|
|
69
|
+
/**
|
|
70
|
+
* Reset session statistics to zero
|
|
71
|
+
*/
|
|
72
|
+
export declare function resetSessionStats(): SessionUpdateResult;
|
|
73
|
+
/**
|
|
74
|
+
* Delete the current session (for testing or cleanup)
|
|
75
|
+
* Also cleans up exit handlers to avoid listener warnings in tests
|
|
76
|
+
* @returns true if session was deleted, false if it didn't exist
|
|
77
|
+
*/
|
|
78
|
+
export declare function deleteSession(): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Reset internal state (for testing)
|
|
81
|
+
* This properly cleans up all listeners to avoid MaxListenersExceededWarning
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
export declare function _resetSessionState(): void;
|
|
85
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/session/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAYH,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EACZ,mBAAmB,EACnB,cAAc,EACf,MAAM,YAAY,CAAC;AAGpB,eAAO,MAAM,YAAY,QAAqC,CAAC;AAmL/D;;;GAGG;AACH,wBAAgB,WAAW,IAAI,gBAAgB,GAAG,IAAI,CAYrD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAO5D;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,IAAI,CAKnC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CASvC;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,gBAAgB,CA+B7E;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,MAAM,GAAG,IAAI,CAG5C;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,GAC7B,mBAAmB,CAwBrB;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,GAAE,MAAU,GAAG,mBAAmB,CAEzE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,GAAE,MAAU,GAAG,mBAAmB,CAE3E;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,GAAE,MAAU,GAAG,mBAAmB,CAEtE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,GAAE,MAAU,GAAG,mBAAmB,CAE1E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,mBAAmB,CAevD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAmBvC;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAKzC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Types
|
|
3
|
+
*
|
|
4
|
+
* Types for session persistence across octocode packages.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Session statistics tracking
|
|
8
|
+
*/
|
|
9
|
+
export interface SessionStats {
|
|
10
|
+
toolCalls: number;
|
|
11
|
+
promptCalls: number;
|
|
12
|
+
errors: number;
|
|
13
|
+
rateLimits: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Persisted session data stored in ~/.octocode/session.json
|
|
17
|
+
*/
|
|
18
|
+
export interface PersistedSession {
|
|
19
|
+
/** Schema version for future migrations */
|
|
20
|
+
version: 1;
|
|
21
|
+
/** Unique session identifier (UUID) */
|
|
22
|
+
sessionId: string;
|
|
23
|
+
/** When the session was first created */
|
|
24
|
+
createdAt: string;
|
|
25
|
+
/** Last time the session was active (updated on init) */
|
|
26
|
+
lastActiveAt: string;
|
|
27
|
+
/** Cumulative session statistics */
|
|
28
|
+
stats: SessionStats;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Result from updating session stats
|
|
32
|
+
*/
|
|
33
|
+
export interface SessionUpdateResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
session: PersistedSession | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Options for creating/loading a session
|
|
39
|
+
*/
|
|
40
|
+
export interface SessionOptions {
|
|
41
|
+
/** Force create a new session even if one exists */
|
|
42
|
+
forceNew?: boolean;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/session/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,2CAA2C;IAC3C,OAAO,EAAE,CAAC,CAAC;IACX,uCAAuC;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,yDAAyD;IACzD,YAAY,EAAE,MAAM,CAAC;IACrB,oCAAoC;IACpC,KAAK,EAAE,YAAY,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,oDAAoD;IACpD,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { existsSync, unlinkSync, readFileSync, writeFileSync, renameSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { O as OCTOCODE_DIR, A as ensureOctocodeDir } from "./storage-DuH3rTiu.js";
|
|
5
|
+
const SESSION_FILE = join(OCTOCODE_DIR, "session.json");
|
|
6
|
+
const CURRENT_VERSION = 1;
|
|
7
|
+
const FLUSH_INTERVAL_MS = 6e4;
|
|
8
|
+
let cachedSession = null;
|
|
9
|
+
let isDirty = false;
|
|
10
|
+
let flushTimer = null;
|
|
11
|
+
let exitHandlersRegistered = false;
|
|
12
|
+
let exitListener = null;
|
|
13
|
+
let sigintListener = null;
|
|
14
|
+
let sigtermListener = null;
|
|
15
|
+
function registerExitHandlers() {
|
|
16
|
+
if (exitHandlersRegistered) return;
|
|
17
|
+
exitHandlersRegistered = true;
|
|
18
|
+
exitListener = () => {
|
|
19
|
+
flushSessionSync();
|
|
20
|
+
};
|
|
21
|
+
sigintListener = () => {
|
|
22
|
+
flushSessionSync();
|
|
23
|
+
};
|
|
24
|
+
sigtermListener = () => {
|
|
25
|
+
flushSessionSync();
|
|
26
|
+
};
|
|
27
|
+
process.on("exit", exitListener);
|
|
28
|
+
process.once("SIGINT", sigintListener);
|
|
29
|
+
process.once("SIGTERM", sigtermListener);
|
|
30
|
+
}
|
|
31
|
+
function unregisterExitHandlers() {
|
|
32
|
+
if (exitListener) {
|
|
33
|
+
process.removeListener("exit", exitListener);
|
|
34
|
+
exitListener = null;
|
|
35
|
+
}
|
|
36
|
+
if (sigintListener) {
|
|
37
|
+
process.removeListener("SIGINT", sigintListener);
|
|
38
|
+
sigintListener = null;
|
|
39
|
+
}
|
|
40
|
+
if (sigtermListener) {
|
|
41
|
+
process.removeListener("SIGTERM", sigtermListener);
|
|
42
|
+
sigtermListener = null;
|
|
43
|
+
}
|
|
44
|
+
exitHandlersRegistered = false;
|
|
45
|
+
}
|
|
46
|
+
function startFlushTimer() {
|
|
47
|
+
if (flushTimer) return;
|
|
48
|
+
flushTimer = setInterval(() => {
|
|
49
|
+
if (isDirty && cachedSession) {
|
|
50
|
+
writeSessionToDisk(cachedSession);
|
|
51
|
+
isDirty = false;
|
|
52
|
+
}
|
|
53
|
+
}, FLUSH_INTERVAL_MS);
|
|
54
|
+
flushTimer.unref();
|
|
55
|
+
}
|
|
56
|
+
function stopFlushTimer() {
|
|
57
|
+
if (flushTimer) {
|
|
58
|
+
clearInterval(flushTimer);
|
|
59
|
+
flushTimer = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function writeSessionToDisk(session) {
|
|
63
|
+
ensureOctocodeDir();
|
|
64
|
+
const tempFile = `${SESSION_FILE}.tmp`;
|
|
65
|
+
writeFileSync(tempFile, JSON.stringify(session, null, 2), {
|
|
66
|
+
mode: 384
|
|
67
|
+
});
|
|
68
|
+
renameSync(tempFile, SESSION_FILE);
|
|
69
|
+
}
|
|
70
|
+
function readSessionFromDisk() {
|
|
71
|
+
if (!existsSync(SESSION_FILE)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const content = readFileSync(SESSION_FILE, "utf8");
|
|
76
|
+
const session = JSON.parse(content);
|
|
77
|
+
if (session.version !== CURRENT_VERSION) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (!session.sessionId || !session.createdAt) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return session;
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createDefaultStats() {
|
|
89
|
+
return {
|
|
90
|
+
toolCalls: 0,
|
|
91
|
+
promptCalls: 0,
|
|
92
|
+
errors: 0,
|
|
93
|
+
rateLimits: 0
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function createNewSession() {
|
|
97
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
98
|
+
return {
|
|
99
|
+
version: CURRENT_VERSION,
|
|
100
|
+
sessionId: randomUUID(),
|
|
101
|
+
createdAt: now,
|
|
102
|
+
lastActiveAt: now,
|
|
103
|
+
stats: createDefaultStats()
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function readSession() {
|
|
107
|
+
if (cachedSession) {
|
|
108
|
+
return cachedSession;
|
|
109
|
+
}
|
|
110
|
+
const session = readSessionFromDisk();
|
|
111
|
+
if (session) {
|
|
112
|
+
cachedSession = session;
|
|
113
|
+
}
|
|
114
|
+
return session;
|
|
115
|
+
}
|
|
116
|
+
function writeSession(session) {
|
|
117
|
+
cachedSession = session;
|
|
118
|
+
isDirty = true;
|
|
119
|
+
registerExitHandlers();
|
|
120
|
+
startFlushTimer();
|
|
121
|
+
}
|
|
122
|
+
function flushSession() {
|
|
123
|
+
if (isDirty && cachedSession) {
|
|
124
|
+
writeSessionToDisk(cachedSession);
|
|
125
|
+
isDirty = false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function flushSessionSync() {
|
|
129
|
+
if (isDirty && cachedSession) {
|
|
130
|
+
try {
|
|
131
|
+
writeSessionToDisk(cachedSession);
|
|
132
|
+
isDirty = false;
|
|
133
|
+
} catch {
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function getOrCreateSession(options) {
|
|
138
|
+
if (options?.forceNew) {
|
|
139
|
+
const newSession2 = createNewSession();
|
|
140
|
+
writeSession(newSession2);
|
|
141
|
+
flushSession();
|
|
142
|
+
return newSession2;
|
|
143
|
+
}
|
|
144
|
+
const existingSession = readSession();
|
|
145
|
+
if (existingSession) {
|
|
146
|
+
const updatedSession = {
|
|
147
|
+
...existingSession,
|
|
148
|
+
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
149
|
+
};
|
|
150
|
+
writeSession(updatedSession);
|
|
151
|
+
flushSession();
|
|
152
|
+
return updatedSession;
|
|
153
|
+
}
|
|
154
|
+
const newSession = createNewSession();
|
|
155
|
+
writeSession(newSession);
|
|
156
|
+
flushSession();
|
|
157
|
+
return newSession;
|
|
158
|
+
}
|
|
159
|
+
function getSessionId() {
|
|
160
|
+
const session = readSession();
|
|
161
|
+
return session?.sessionId ?? null;
|
|
162
|
+
}
|
|
163
|
+
function updateSessionStats(updates) {
|
|
164
|
+
const session = readSession();
|
|
165
|
+
if (!session) {
|
|
166
|
+
return { success: false, session: null };
|
|
167
|
+
}
|
|
168
|
+
const updatedStats = {
|
|
169
|
+
toolCalls: session.stats.toolCalls + (updates.toolCalls ?? 0),
|
|
170
|
+
promptCalls: session.stats.promptCalls + (updates.promptCalls ?? 0),
|
|
171
|
+
errors: session.stats.errors + (updates.errors ?? 0),
|
|
172
|
+
rateLimits: session.stats.rateLimits + (updates.rateLimits ?? 0)
|
|
173
|
+
};
|
|
174
|
+
const updatedSession = {
|
|
175
|
+
...session,
|
|
176
|
+
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
177
|
+
stats: updatedStats
|
|
178
|
+
};
|
|
179
|
+
writeSession(updatedSession);
|
|
180
|
+
return { success: true, session: updatedSession };
|
|
181
|
+
}
|
|
182
|
+
function incrementToolCalls(count = 1) {
|
|
183
|
+
return updateSessionStats({ toolCalls: count });
|
|
184
|
+
}
|
|
185
|
+
function incrementPromptCalls(count = 1) {
|
|
186
|
+
return updateSessionStats({ promptCalls: count });
|
|
187
|
+
}
|
|
188
|
+
function incrementErrors(count = 1) {
|
|
189
|
+
return updateSessionStats({ errors: count });
|
|
190
|
+
}
|
|
191
|
+
function incrementRateLimits(count = 1) {
|
|
192
|
+
return updateSessionStats({ rateLimits: count });
|
|
193
|
+
}
|
|
194
|
+
function resetSessionStats() {
|
|
195
|
+
const session = readSession();
|
|
196
|
+
if (!session) {
|
|
197
|
+
return { success: false, session: null };
|
|
198
|
+
}
|
|
199
|
+
const updatedSession = {
|
|
200
|
+
...session,
|
|
201
|
+
lastActiveAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
202
|
+
stats: createDefaultStats()
|
|
203
|
+
};
|
|
204
|
+
writeSession(updatedSession);
|
|
205
|
+
return { success: true, session: updatedSession };
|
|
206
|
+
}
|
|
207
|
+
function deleteSession() {
|
|
208
|
+
cachedSession = null;
|
|
209
|
+
isDirty = false;
|
|
210
|
+
stopFlushTimer();
|
|
211
|
+
unregisterExitHandlers();
|
|
212
|
+
if (!existsSync(SESSION_FILE)) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
unlinkSync(SESSION_FILE);
|
|
217
|
+
return true;
|
|
218
|
+
} catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function _resetSessionState() {
|
|
223
|
+
cachedSession = null;
|
|
224
|
+
isDirty = false;
|
|
225
|
+
stopFlushTimer();
|
|
226
|
+
unregisterExitHandlers();
|
|
227
|
+
}
|
|
228
|
+
export {
|
|
229
|
+
SESSION_FILE as S,
|
|
230
|
+
_resetSessionState as _,
|
|
231
|
+
getSessionId as a,
|
|
232
|
+
flushSessionSync as b,
|
|
233
|
+
incrementPromptCalls as c,
|
|
234
|
+
deleteSession as d,
|
|
235
|
+
incrementErrors as e,
|
|
236
|
+
flushSession as f,
|
|
237
|
+
getOrCreateSession as g,
|
|
238
|
+
incrementRateLimits as h,
|
|
239
|
+
incrementToolCalls as i,
|
|
240
|
+
resetSessionStats as j,
|
|
241
|
+
readSession as r,
|
|
242
|
+
updateSessionStats as u,
|
|
243
|
+
writeSession as w
|
|
244
|
+
};
|