negotium 0.2.14 → 0.2.16

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.
Files changed (34) hide show
  1. package/dist/agent-helpers.js +178 -26
  2. package/dist/agent-helpers.js.map +8 -8
  3. package/dist/{chunk-5g5a63vv.js → chunk-5cpdbbeh.js} +56 -6
  4. package/dist/{chunk-5g5a63vv.js.map → chunk-5cpdbbeh.js.map} +4 -4
  5. package/dist/hosted-agent.js +118 -9
  6. package/dist/hosted-agent.js.map +6 -6
  7. package/dist/main.js +389 -50
  8. package/dist/main.js.map +15 -15
  9. package/dist/mcp-factories.js +247 -36
  10. package/dist/mcp-factories.js.map +9 -9
  11. package/dist/registry.js +3 -3
  12. package/dist/registry.js.map +2 -2
  13. package/dist/rollout.js +1 -1
  14. package/dist/runtime/src/agents/codex-provider.ts +53 -2
  15. package/dist/runtime/src/agents/rollout/codex.ts +62 -0
  16. package/dist/runtime/src/mcp/factories/token-stats.ts +33 -11
  17. package/dist/runtime/src/mcp/runtime-spec.ts +58 -1
  18. package/dist/runtime/src/node-host.ts +1 -0
  19. package/dist/runtime/src/runtime/public-helpers.ts +8 -0
  20. package/dist/runtime/src/runtime/turn-event-stream.ts +17 -2
  21. package/dist/runtime/src/storage/token-stats.ts +200 -19
  22. package/dist/runtime/src/version.ts +1 -1
  23. package/dist/runtime-helpers.js +443 -30
  24. package/dist/runtime-helpers.js.map +9 -3
  25. package/dist/storage.js +112 -11
  26. package/dist/storage.js.map +3 -3
  27. package/dist/types/packages/core/src/agents/codex-provider.d.ts +13 -0
  28. package/dist/types/packages/core/src/agents/rollout/codex.d.ts +9 -0
  29. package/dist/types/packages/core/src/mcp/factories/token-stats.d.ts +4 -2
  30. package/dist/types/packages/core/src/runtime/bashrs-completions.d.ts +50 -0
  31. package/dist/types/packages/core/src/runtime/public-helpers.d.ts +2 -0
  32. package/dist/types/packages/core/src/storage/token-stats.d.ts +35 -6
  33. package/dist/types/packages/core/src/version.d.ts +1 -1
  34. package/package.json +1 -1
@@ -38,6 +38,12 @@ export interface CodexContextUsage {
38
38
  contextTokens: number;
39
39
  contextWindow: number;
40
40
  }
41
+ export interface CodexTokenTotals {
42
+ inputTokens: number;
43
+ outputTokens: number;
44
+ cachedInputTokens: number;
45
+ cacheWriteInputTokens: number;
46
+ }
41
47
  export interface CodexPatchChangePreview {
42
48
  path: string;
43
49
  before?: string;
@@ -64,8 +70,11 @@ export declare function readCodexPatchCallIds(threadId: string): string[];
64
70
  export declare function readLatestCodexPatchPreview(threadId: string, expectedPaths: string[], consumedCallIds?: ReadonlySet<string>, expectedCallId?: string): CodexPatchPreview | undefined;
65
71
  /** Read the latest per-request context measurement from a Codex rollout. */
66
72
  export declare function extractLatestCodexContextUsage(jsonl: string): CodexContextUsage | undefined;
73
+ /** Latest cumulative token counter persisted in a native Codex rollout. */
74
+ export declare function extractLatestCodexTokenTotals(jsonl: string): CodexTokenTotals | undefined;
67
75
  /** Resolve a thread's current rollout and return its latest context measurement. */
68
76
  export declare function readLatestCodexContextUsage(threadId: string): CodexContextUsage | undefined;
77
+ export declare function readLatestCodexTokenTotals(threadId: string): CodexTokenTotals | undefined;
69
78
  /**
70
79
  * Old and metadata-free Codex rollouts resume as native multi-agent v1 even
71
80
  * when the feature flag is false. Rewrite their runtime metadata in place so
@@ -1,5 +1,5 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import { type Bucket } from "../../storage/token-stats";
2
+ import { type Bucket, type CurrentSessionUsage } from "../../storage/token-stats";
3
3
  export interface TokenStatsMcpContext {
4
4
  userId: string;
5
5
  }
@@ -7,11 +7,13 @@ export interface TokenStatsSnapshot {
7
7
  total: Bucket;
8
8
  byHour: Record<string, Bucket>;
9
9
  bySession: Record<string, Bucket>;
10
+ currentSessions?: CurrentSessionUsage[];
11
+ ignoredLegacyRecords?: number;
10
12
  estimatedCostUsd: number;
11
13
  }
12
14
  export interface TokenStatsMcpHost {
13
15
  getStats(userId: string, from?: string, to?: string): TokenStatsSnapshot;
14
- calcCost(bucket: Pick<Bucket, "inputTokens" | "outputTokens" | "cacheCreationInputTokens" | "cacheReadInputTokens">): number;
16
+ calcCost(bucket: Pick<Bucket, "inputTokens" | "outputTokens" | "cacheCreationInputTokens" | "cacheReadInputTokens" | "estimatedCostUsd">): number;
15
17
  extraSummaryLines?(userId: string): readonly string[];
16
18
  }
17
19
  export declare const defaultTokenStatsMcpHost: TokenStatsMcpHost;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * bash-rs completion watcher — turns `result.json` files written by the Rust
3
+ * `bash-rs` background-bash daemon into session-inbox `tell`s. bash-rs is the
4
+ * only backend; the retired TypeScript server called `injectMessage()` itself.
5
+ *
6
+ * Why a watcher instead of bash-rs calling back into negotium: bash-rs is a
7
+ * standalone project (github.com/maestrojeong/bash-rs-mcp, sibling of
8
+ * browser-rs-mcp) that knows nothing about topics/session-inbox/turns — it
9
+ * only ever appends `{bash_id}/meta.json` (on spawn) and `{bash_id}/result.json`
10
+ * (on completion) under `BASHRS_SPILL_ROOT`, and never reads or deletes
11
+ * either. Any consumer, negotium or otherwise, watches that directory the
12
+ * same way `runtime/inbox.ts` already watches the session-inbox itself:
13
+ * `fs.watch` + a fallback poll, at-least-once, dedup by a stable id.
14
+ *
15
+ * `owner` on the wire is `${userId}\0${topicId}` — see
16
+ * `platform/mcp-config.ts::backgroundBashTransport` for what negotium sends
17
+ * as `X-Background-Bash-User`/`X-Background-Bash-Topic`, and bash-rs's own
18
+ * `security.rs::resolve_identity` for the matching NUL-join on the other end.
19
+ */
20
+ /**
21
+ * Where a finished background job's turn is delivered.
22
+ *
23
+ * The default appends to negotium's own session inbox. An embedding host that
24
+ * owns a different inbox installs its own sink here — otium, for one, resolves
25
+ * `RUN_DIR` to its own state dir and runs its own inbox worker, so writing to
26
+ * negotium's path would drop the turn on the floor rather than deliver it.
27
+ * Mirrors the `setFileHooks()` seam in `runtime/file-hooks.ts`.
28
+ *
29
+ * A sink must throw to signal failure: the caller leaves `result.json` in
30
+ * place so the next sweep retries, which is what keeps delivery at-least-once.
31
+ */
32
+ export interface BashrsCompletion {
33
+ userId: string;
34
+ topicId: string;
35
+ /** Stable per-job id; use it to collapse a retried delivery into one turn. */
36
+ bashId: string;
37
+ message: string;
38
+ }
39
+ export type BashrsCompletionSink = (completion: BashrsCompletion) => void;
40
+ /** Install a host's delivery sink, or pass null to restore the default. */
41
+ export declare function setBashrsCompletionSink(sink: BashrsCompletionSink | null): void;
42
+ /** One sweep: deliver any new `result.json`, then reap old delivered jobs. */
43
+ export declare function flushBashrsCompletions(): Promise<void>;
44
+ /**
45
+ * Join the bashrs-completions worker election — same one-leader-per-role
46
+ * lease as `startSessionInboxWorker`, so exactly one runtime process watches
47
+ * `BASHRS_SPILL_ROOT` regardless of how many are up. Returns a cleanup
48
+ * function for graceful shutdown.
49
+ */
50
+ export declare function startBashrsCompletionsWorker(): () => void;
@@ -5,6 +5,8 @@ export { delay } from "../platform/delay";
5
5
  export { errMsg } from "../platform/error";
6
6
  export { appendJsonlEntry, appendJsonlLine, parseJsonlText, readJsonFile, readJsonlLines, writeJsonFileAtomic, writeJsonlFile, } from "../platform/jsonl";
7
7
  export { createLifecycleManager, type LifecycleLogger, type LifecycleManager, type LifecycleManagerOptions, type LifecycleProcessHost, onShutdown, runShutdown, type ShutdownHandler, type SignalReason, } from "../platform/lifecycle";
8
+ export type { BashrsCompletion, BashrsCompletionSink, } from "./bashrs-completions";
9
+ export { setBashrsCompletionSink, startBashrsCompletionsWorker, } from "./bashrs-completions";
8
10
  export { escapeRegExp, mentionsAi } from "./channel-context";
9
11
  export { buildContextWarningText, type ClaudeRequestTokenUsage, type ContextOccupancy, type ContextWarningOptions, type ContextWarningState, type ContextWarningTextOptions, claudeRequestContextTokens, clearContextWarning, contextUsageRatio, createContextWarningState, DEFAULT_CONTEXT_WARNING_RATIO, formatContextTokenCount, nextContextWarning, shouldWarnForContext, } from "./context-warning";
10
12
  export { renderTaskPanel, taskPanelMessageId } from "./task-format";
@@ -1,10 +1,20 @@
1
+ import type { AgentKind, TokenUsage } from "../types";
1
2
  export interface QueryRecord {
3
+ schemaVersion: 2;
2
4
  timestamp: string;
3
5
  session: string;
6
+ topicId: string;
7
+ providerSessionId?: string;
8
+ agent: AgentKind;
9
+ model: string;
10
+ /** Cache-miss input only. */
4
11
  inputTokens: number;
5
12
  outputTokens: number;
6
13
  cacheCreationInputTokens: number;
7
14
  cacheReadInputTokens: number;
15
+ contextTokens?: number;
16
+ contextWindow?: number;
17
+ estimatedCostUsd: number;
8
18
  }
9
19
  export interface Bucket {
10
20
  inputTokens: number;
@@ -12,19 +22,38 @@ export interface Bucket {
12
22
  cacheCreationInputTokens: number;
13
23
  cacheReadInputTokens: number;
14
24
  queries: number;
25
+ estimatedCostUsd: number;
26
+ }
27
+ export interface CurrentSessionUsage {
28
+ timestamp: string;
29
+ topicId: string;
30
+ topicTitle: string;
31
+ providerSessionId?: string;
32
+ agent: AgentKind;
33
+ model: string;
34
+ contextTokens: number;
35
+ contextWindow: number;
36
+ }
37
+ export interface TopicUsageSummary extends Bucket {
38
+ topicId: string;
39
+ currentSession?: CurrentSessionUsage;
15
40
  }
16
41
  export declare function tokenStatsFileId(userId: number | string): string;
17
- export declare function calcCost(b: Pick<Bucket, "inputTokens" | "outputTokens" | "cacheCreationInputTokens" | "cacheReadInputTokens">): number;
42
+ export declare function calcCost(b: Pick<Bucket, "inputTokens" | "outputTokens" | "cacheCreationInputTokens" | "cacheReadInputTokens" | "estimatedCostUsd">): number;
18
43
  /** 쿼리 완료 시 호출 — JSONL에 한 줄 추가 */
19
- export declare function recordUsage(userId: number | string, session: string, usage: {
20
- inputTokens: number;
21
- outputTokens: number;
22
- cacheCreationInputTokens?: number;
23
- cacheReadInputTokens?: number;
44
+ export declare function recordUsage(userId: number | string, session: string, usage: TokenUsage, context: {
45
+ topicId: string;
46
+ providerSessionId?: string;
47
+ agent: AgentKind;
48
+ model: string;
24
49
  }): void;
25
50
  export declare function getStats(userId: number | string, from?: string, to?: string): {
26
51
  total: Bucket;
27
52
  byHour: Record<string, Bucket>;
28
53
  bySession: Record<string, Bucket>;
54
+ currentSessions: CurrentSessionUsage[];
55
+ ignoredLegacyRecords: number;
29
56
  estimatedCostUsd: number;
30
57
  };
58
+ /** Exact all-time usage for one topic, independent of transcript pagination. */
59
+ export declare function getTopicStats(userId: number | string, topicId: string): TopicUsageSummary;
@@ -1 +1 @@
1
- export declare const NEGOTIUM_VERSION = "0.2.14";
1
+ export declare const NEGOTIUM_VERSION = "0.2.16";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "negotium",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "type": "module",
5
5
  "description": "Install the Negotium multi-agent runtime and CLI with one package",
6
6
  "license": "Apache-2.0",