@ashx-j/lunr 0.2.27 → 0.2.28
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 +10 -1
- package/dist/builtin-extensions/narumiruna-pi-goal/src/accounting.d.ts.map +1 -1
- package/dist/builtin-extensions/narumiruna-pi-goal/src/accounting.js +5 -2
- package/dist/builtin-extensions/narumiruna-pi-goal/src/accounting.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/chain-root-attachment.d.ts +4 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/chain-root-attachment.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/chain-root-attachment.js +2 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/background/chain-root-attachment.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/notify.d.ts +5 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/background/notify.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/notify.js +2 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/background/notify.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.d.ts +5 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.js +68 -40
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.d.ts +0 -3
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.js +28 -4
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/subagent-executor.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/subagent-executor.js +1 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/subagent-executor.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/shared/session-tokens.d.ts +7 -1
- package/dist/builtin-extensions/pi-subagents/src/shared/session-tokens.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/shared/session-tokens.js +70 -38
- package/dist/builtin-extensions/pi-subagents/src/shared/session-tokens.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/shared/types.d.ts +9 -0
- package/dist/builtin-extensions/pi-subagents/src/shared/types.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/shared/types.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/slash/slash-commands.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/slash/slash-commands.js +13 -62
- package/dist/builtin-extensions/pi-subagents/src/slash/slash-commands.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/render.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/render.js +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/render.js.map +1 -1
- package/dist/builtin-extensions/pi-tps.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-tps.js +10 -2
- package/dist/builtin-extensions/pi-tps.js.map +1 -1
- package/dist/catalog/models.json +1 -1
- package/dist/catalog/providers/google-vertex.json +1 -1
- package/dist/catalog/providers/google.json +1 -1
- package/dist/catalog/publication.json +3 -3
- package/dist/core/agent-session.d.ts +6 -15
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +35 -50
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/cache-stats.d.ts +1 -1
- package/dist/core/cache-stats.d.ts.map +1 -1
- package/dist/core/cache-stats.js +1 -1
- package/dist/core/cache-stats.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +2 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +1 -0
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +5 -11
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +38 -92
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/context-breakdown.d.ts +2 -2
- package/dist/core/context-breakdown.d.ts.map +1 -1
- package/dist/core/context-breakdown.js +22 -20
- package/dist/core/context-breakdown.js.map +1 -1
- package/dist/core/extensions/types.d.ts +3 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/session-manager.d.ts +5 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +16 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/usage-accounting.d.ts +47 -0
- package/dist/core/usage-accounting.d.ts.map +1 -0
- package/dist/core/usage-accounting.js +205 -0
- package/dist/core/usage-accounting.js.map +1 -0
- package/dist/core/usage-history.d.ts +1 -18
- package/dist/core/usage-history.d.ts.map +1 -1
- package/dist/core/usage-history.js +46 -114
- package/dist/core/usage-history.js.map +1 -1
- package/dist/gateway/mobile-commands.d.ts.map +1 -1
- package/dist/gateway/mobile-commands.js +13 -1
- package/dist/gateway/mobile-commands.js.map +1 -1
- package/dist/modes/interactive/components/context-view.d.ts.map +1 -1
- package/dist/modes/interactive/components/context-view.js +2 -2
- package/dist/modes/interactive/components/context-view.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +8 -12
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/usage-view.d.ts +5 -0
- package/dist/modes/interactive/components/usage-view.d.ts.map +1 -1
- package/dist/modes/interactive/components/usage-view.js +21 -6
- package/dist/modes/interactive/components/usage-view.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -5
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +55 -22
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/node-runtime/{agent-session-services-KDMSPQDY.js → agent-session-services-RTUGP3XR.js} +5 -4
- package/dist/node-runtime/{builtin-extensions-OX6KELNX.js → builtin-extensions-N4EUD6SB.js} +1 -1
- package/dist/node-runtime/{chunk-A2WO6WIN.js → chunk-4QRUZ64T.js} +11 -5
- package/dist/node-runtime/{chunk-ZDJUR45D.js → chunk-BMRXLQWQ.js} +41 -38
- package/dist/node-runtime/{chunk-MQPKUHRN.js → chunk-DFMPRAQQ.js} +8 -8
- package/dist/node-runtime/{chunk-C3P6Q3B2.js → chunk-E5DQGNJI.js} +10 -10
- package/dist/node-runtime/{chunk-FIRA54AC.js → chunk-ICKGAKWX.js} +22 -55
- package/dist/node-runtime/{chunk-JWEVT363.js → chunk-JEK35V2M.js} +14 -8
- package/dist/node-runtime/{chunk-EPR2F3GU.js → chunk-PWHTSQC4.js} +15 -1
- package/dist/node-runtime/{chunk-AYHH7YSD.js → chunk-QT3F6UEV.js} +61 -30
- package/dist/node-runtime/{chunk-JQYNHAKX.js → chunk-RY3FJ6WI.js} +29 -27
- package/dist/node-runtime/{chunk-I2H5QMWW.js → chunk-STMX7W5I.js} +1 -1
- package/dist/node-runtime/chunk-SWWHZ2RI.js +158 -0
- package/dist/node-runtime/{chunk-QNVMKNNN.js → chunk-U3YVZ6UC.js} +4 -4
- package/dist/node-runtime/{command-3HMSY6W2.js → command-YRT7I2BW.js} +1 -1
- package/dist/node-runtime/{commands-OCSSEEWJ.js → commands-2ETOWWRA.js} +5 -5
- package/dist/node-runtime/{export-html-BRWF5AHI.js → export-html-DBQRRMAY.js} +1 -1
- package/dist/node-runtime/{gateway-6623C2U3.js → gateway-V6K2CDWN.js} +9 -9
- package/dist/node-runtime/{goal-AIHEDUYA.js → goal-J7TYMOBK.js} +9 -3
- package/dist/node-runtime/index.js +9 -8
- package/dist/node-runtime/{interactive-mode-FM5DNUSW.js → interactive-mode-ASZ7APYP.js} +7 -6
- package/dist/node-runtime/{lunr-cron-LKVA5CZZ.js → lunr-cron-WD3YH4CR.js} +4 -4
- package/dist/node-runtime/main.js +7 -6
- package/dist/node-runtime/{mobile-commands-XT3NQXLD.js → mobile-commands-ICUXFW5V.js} +2 -2
- package/dist/node-runtime/{pi-subagents-LT3CTO4E.js → pi-subagents-QPU3OXE4.js} +17 -62
- package/dist/node-runtime/rpc-entry.js +7 -6
- package/dist/node-runtime/{session-handoff-LNSPDTKY.js → session-handoff-LYH53ESV.js} +2 -2
- package/dist/node-runtime/{session-manager-YIFIAVGC.js → session-manager-ZJ3N5Q74.js} +1 -1
- package/dist/node-runtime/{subagent-executor-Q5F7DBVM.js → subagent-executor-UI6SHG3X.js} +294 -231
- package/docs/features.md +10 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/install-lock/package-lock.json +24 -24
- package/install-lock/package.json +2 -2
- package/npm-shrinkwrap.json +21 -21
- package/package.json +7 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { collectChildUsage, collectUsageRequests, totalChildUsage, totalRequestUsage, } from "./usage-accounting.js";
|
|
1
2
|
/**
|
|
2
3
|
* AgentSession - Core abstraction for agent lifecycle and session management.
|
|
3
4
|
*
|
|
@@ -14,7 +15,7 @@
|
|
|
14
15
|
*/
|
|
15
16
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
16
17
|
import { basename, dirname, join, resolve } from "node:path";
|
|
17
|
-
import { clampThinkingLevel, cleanupSessionResources, getSupportedThinkingLevels, isContextOverflow, isRetryableAssistantError, modelsAreEqual, resetApiProviders, streamSimple, } from "@ashx-j/lunr-ai/compat";
|
|
18
|
+
import { clampThinkingLevel, cleanupSessionResources, estimateContextTokens as estimateRequestContextTokens, getSupportedThinkingLevels, isContextOverflow, isRetryableAssistantError, modelsAreEqual, resetApiProviders, streamSimple, } from "@ashx-j/lunr-ai/compat";
|
|
18
19
|
import { getAgentDir } from "../config.js";
|
|
19
20
|
import { getThemeByName, theme } from "../modes/interactive/theme/theme.js";
|
|
20
21
|
import { stripFrontmatter } from "../utils/frontmatter.js";
|
|
@@ -27,6 +28,7 @@ import { DEFAULT_THINKING_LEVEL } from "./defaults.js";
|
|
|
27
28
|
import { ExtensionRunner, wrapRegisteredTools, } from "./extensions/index.js";
|
|
28
29
|
import { emitSessionShutdownEvent } from "./extensions/runner.js";
|
|
29
30
|
import { MEMORY_TOOL_NAMES } from "./memory-cap.js";
|
|
31
|
+
import { convertToLlm } from "./messages.js";
|
|
30
32
|
import { isUserInstructionsPath, loadSelectedUserInstructions } from "./model-instructions.js";
|
|
31
33
|
import { ModelRegistry } from "./model-registry.js";
|
|
32
34
|
import { AUTO_MODE_ADDENDUM, deletePermissionContext, gateToolCall, getPermissionMode, initializePermissionContext, setPermissionMode, } from "./permissions.js";
|
|
@@ -355,12 +357,17 @@ export class AgentSession {
|
|
|
355
357
|
: undefined);
|
|
356
358
|
this.agent.prepareNextTurnWithContext = async (turn, signal) => {
|
|
357
359
|
const previousSnapshot = await previousPrepareNextTurnWithContext?.(turn, signal);
|
|
358
|
-
let previousContext =
|
|
360
|
+
let previousContext = {
|
|
361
|
+
...(previousSnapshot?.context ?? turn.context),
|
|
362
|
+
systemPrompt: this._withSystemPromptAppend(this._systemPromptOverride ?? this._baseSystemPrompt),
|
|
363
|
+
tools: this.agent.state.tools.slice(),
|
|
364
|
+
};
|
|
359
365
|
const model = this.model;
|
|
360
366
|
const settings = this.settingsManager.getCompactionSettings();
|
|
361
367
|
if (model?.provider === "openai-codex" &&
|
|
362
368
|
settings.enabled &&
|
|
363
|
-
shouldCompact(
|
|
369
|
+
shouldCompact(estimateRequestContextTokens({ ...previousContext, messages: convertToLlm(previousContext.messages) })
|
|
370
|
+
.tokens, model.contextWindow, settings)) {
|
|
364
371
|
const previousCompactionId = getLatestCompactionEntry(this.sessionManager.getBranch())?.id;
|
|
365
372
|
await this._runAutoCompaction("threshold", true);
|
|
366
373
|
const currentCompaction = getLatestCompactionEntry(this.sessionManager.getBranch());
|
|
@@ -1981,7 +1988,7 @@ export class AgentSession {
|
|
|
1981
1988
|
}
|
|
1982
1989
|
else {
|
|
1983
1990
|
// Generate compaction result
|
|
1984
|
-
const result = await compact(preparation, this.model, apiKey, headers, customInstructions, this._compactionAbortController.signal, this.thinkingLevel, this.agent.streamFn, env);
|
|
1991
|
+
const result = await compact(preparation, this.model, apiKey, headers, customInstructions, this._compactionAbortController.signal, this.thinkingLevel, this.agent.streamFn, env, (response) => this.sessionManager.appendRequestUsage("compaction", response));
|
|
1985
1992
|
summary = result.summary;
|
|
1986
1993
|
firstKeptEntryId = result.firstKeptEntryId;
|
|
1987
1994
|
tokensBefore = result.tokensBefore;
|
|
@@ -2215,7 +2222,7 @@ export class AgentSession {
|
|
|
2215
2222
|
}
|
|
2216
2223
|
else {
|
|
2217
2224
|
// Generate compaction result
|
|
2218
|
-
const compactResult = await compact(preparation, this.model, apiKey, headers, undefined, this._autoCompactionAbortController.signal, this.thinkingLevel, this.agent.streamFn, env);
|
|
2225
|
+
const compactResult = await compact(preparation, this.model, apiKey, headers, undefined, this._autoCompactionAbortController.signal, this.thinkingLevel, this.agent.streamFn, env, (response) => this.sessionManager.appendRequestUsage("compaction", response));
|
|
2219
2226
|
summary = compactResult.summary;
|
|
2220
2227
|
firstKeptEntryId = compactResult.firstKeptEntryId;
|
|
2221
2228
|
tokensBefore = compactResult.tokensBefore;
|
|
@@ -3038,6 +3045,7 @@ export class AgentSession {
|
|
|
3038
3045
|
replaceInstructions,
|
|
3039
3046
|
reserveTokens: branchSummarySettings.reserveTokens,
|
|
3040
3047
|
streamFn: this.agent.streamFn,
|
|
3048
|
+
onUsage: (response) => this.sessionManager.appendRequestUsage("branch-summary", response),
|
|
3041
3049
|
});
|
|
3042
3050
|
if (result.aborted) {
|
|
3043
3051
|
return { cancelled: true, aborted: true };
|
|
@@ -3162,7 +3170,7 @@ export class AgentSession {
|
|
|
3162
3170
|
/**
|
|
3163
3171
|
* Get session statistics. Aggregates over ALL session entries (including
|
|
3164
3172
|
* history that was compacted away), so token/cost totals reflect what was
|
|
3165
|
-
*
|
|
3173
|
+
* reported or estimated across requests incurred in this session.
|
|
3166
3174
|
*/
|
|
3167
3175
|
getSessionStats() {
|
|
3168
3176
|
let userMessages = 0;
|
|
@@ -3170,11 +3178,12 @@ export class AgentSession {
|
|
|
3170
3178
|
let toolResults = 0;
|
|
3171
3179
|
let totalMessages = 0;
|
|
3172
3180
|
let toolCalls = 0;
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3181
|
+
const entries = this.sessionManager.getEntries();
|
|
3182
|
+
const parentUsage = totalRequestUsage(collectUsageRequests(entries));
|
|
3183
|
+
const childUsage = totalChildUsage(collectChildUsage(entries));
|
|
3184
|
+
const combinedUsage = { ...parentUsage };
|
|
3185
|
+
for (const key of Object.keys(combinedUsage))
|
|
3186
|
+
combinedUsage[key] += childUsage[key];
|
|
3178
3187
|
for (const entry of this.sessionManager.getEntries()) {
|
|
3179
3188
|
if (entry.type !== "message")
|
|
3180
3189
|
continue;
|
|
@@ -3192,12 +3201,6 @@ export class AgentSession {
|
|
|
3192
3201
|
if (Array.isArray(assistantMsg.content)) {
|
|
3193
3202
|
toolCalls += assistantMsg.content.filter((c) => c.type === "toolCall").length;
|
|
3194
3203
|
}
|
|
3195
|
-
const usage = assistantMsg.usage;
|
|
3196
|
-
totalInput += usage.input;
|
|
3197
|
-
totalOutput += usage.output;
|
|
3198
|
-
totalCacheRead += usage.cacheRead;
|
|
3199
|
-
totalCacheWrite += usage.cacheWrite;
|
|
3200
|
-
totalCost += usage.cost.total;
|
|
3201
3204
|
}
|
|
3202
3205
|
}
|
|
3203
3206
|
return {
|
|
@@ -3209,13 +3212,16 @@ export class AgentSession {
|
|
|
3209
3212
|
toolResults,
|
|
3210
3213
|
totalMessages,
|
|
3211
3214
|
tokens: {
|
|
3212
|
-
input:
|
|
3213
|
-
output:
|
|
3214
|
-
cacheRead:
|
|
3215
|
-
cacheWrite:
|
|
3216
|
-
total:
|
|
3215
|
+
input: parentUsage.input,
|
|
3216
|
+
output: parentUsage.output,
|
|
3217
|
+
cacheRead: parentUsage.cacheRead,
|
|
3218
|
+
cacheWrite: parentUsage.cacheWrite,
|
|
3219
|
+
total: parentUsage.total,
|
|
3217
3220
|
},
|
|
3218
|
-
cost:
|
|
3221
|
+
cost: parentUsage.cost,
|
|
3222
|
+
parentUsage,
|
|
3223
|
+
childUsage,
|
|
3224
|
+
combinedUsage,
|
|
3219
3225
|
contextUsage: this.getContextUsage(),
|
|
3220
3226
|
};
|
|
3221
3227
|
}
|
|
@@ -3226,38 +3232,17 @@ export class AgentSession {
|
|
|
3226
3232
|
const contextWindow = model.contextWindow ?? 0;
|
|
3227
3233
|
if (contextWindow <= 0)
|
|
3228
3234
|
return undefined;
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
if (latestCompaction) {
|
|
3235
|
-
// Check if there's a valid assistant usage after the compaction boundary
|
|
3236
|
-
const compactionIndex = branchEntries.lastIndexOf(latestCompaction);
|
|
3237
|
-
let hasPostCompactionUsage = false;
|
|
3238
|
-
for (let i = branchEntries.length - 1; i > compactionIndex; i--) {
|
|
3239
|
-
const entry = branchEntries[i];
|
|
3240
|
-
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
3241
|
-
const assistant = entry.message;
|
|
3242
|
-
if (assistant.stopReason !== "aborted" && assistant.stopReason !== "error") {
|
|
3243
|
-
const contextTokens = calculateContextTokens(assistant.usage);
|
|
3244
|
-
if (contextTokens > 0) {
|
|
3245
|
-
hasPostCompactionUsage = true;
|
|
3246
|
-
break;
|
|
3247
|
-
}
|
|
3248
|
-
}
|
|
3249
|
-
}
|
|
3250
|
-
}
|
|
3251
|
-
if (!hasPostCompactionUsage) {
|
|
3252
|
-
return { tokens: null, contextWindow, percent: null };
|
|
3253
|
-
}
|
|
3254
|
-
}
|
|
3255
|
-
const estimate = estimateContextTokens(this.messages);
|
|
3235
|
+
const estimate = estimateRequestContextTokens({
|
|
3236
|
+
systemPrompt: this.systemPrompt,
|
|
3237
|
+
tools: this.agent.state.tools,
|
|
3238
|
+
messages: convertToLlm(this.messages),
|
|
3239
|
+
});
|
|
3256
3240
|
const percent = (estimate.tokens / contextWindow) * 100;
|
|
3257
3241
|
return {
|
|
3258
3242
|
tokens: estimate.tokens,
|
|
3259
3243
|
contextWindow,
|
|
3260
3244
|
percent,
|
|
3245
|
+
estimated: estimate.lastUsageIndex === null || estimate.trailingTokens > 0,
|
|
3261
3246
|
};
|
|
3262
3247
|
}
|
|
3263
3248
|
/**
|