@njuptlzf/agent-chat-server 0.4.1 → 0.5.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/src/server.ts CHANGED
@@ -16,6 +16,7 @@ import { MCP_SPEC_ALLOWLIST_ENV, checkMcpDecl, mergedToolLabels, probeMcpProxy,
16
16
  import { baseUrlFromModelsJson, deleteProvider, keyEnvOf, modelToProbe, patchProvider, patchRouting, patchTiers, refreshProvider, testProvider, toModelsResponse, type ApiError } from "./models-api.ts";
17
17
  import { buildCommands, compileSystemPrompt, createProductSession, disposeProductSession, expandCommand, exportHistory, loadProductSkills, narrowMcpExposure, type UIPromptAnswer } from "./session.ts";
18
18
  import { auditPageWindow, toHistoryMessages, truncateLastTurn } from "./stores.ts";
19
+ import type { CostTable } from "./cost-model.ts";
19
20
  import type { AccountStateStore, AuditEvent, AuditRow, AuditStore, Authenticator, BillingOrder, BillingStore, HistoryStore, MembershipStore, MemberPoolStore, RunLogStore } from "./stores.ts";
20
21
  import type { Ledger } from "./ledger.ts";
21
22
  import type { Engine, EngineEvent, JobContext, JobRunResult } from "./orchestrator/engine.ts";
@@ -91,6 +92,11 @@ export interface ServerDeps {
91
92
  * 先占位再开跑,并发下不会超发(旧模型实测超发 140%)。
92
93
  */
93
94
  ledger: Ledger;
95
+ /**
96
+ * 成本表(统一成本模型)。omitted ⇒ settle 的 cu=0(CU 闸不启用,零行为变化);
97
+ * 主链 main.ts 由 loadCostTable 装配。按 run 起始 tier 模型折算,见 CostTable 注释。
98
+ */
99
+ costTable?: CostTable;
94
100
  /**
95
101
  * 编排引擎。omitted ⇒ 默认 `taskflow` 适配器(W1:单 phase 通路)。
96
102
  * 测试注入 `createFakeEngine()`,**不建 pi 会话、不调模型**——
@@ -1680,7 +1686,11 @@ export function createChatServer(deps: ServerDeps): Server {
1680
1686
  }, 15000);
1681
1687
 
1682
1688
  let session: Awaited<ReturnType<typeof createProductSession>> | undefined;
1683
- const mapper = createEventMapper();
1689
+ // costUsd 统一口径:装了 costTable 就按 run 起始 tier 模型折算 CU 写进 TokenUsage.costUsd;
1690
+ // 没装则传 undefined 让 mapper 回退 pi 的 cost.total(老部署零行为变化)。
1691
+ const mapper = createEventMapper(
1692
+ deps.costTable ? (u) => deps.costTable!.cuFor(tier.provider, tier.model, u) : undefined,
1693
+ );
1684
1694
  const accounting = createRunAccounting(limits.maxToolRounds);
1685
1695
  const toolCalls: Record<string, number> = {};
1686
1696
  let doneSent = false;
@@ -2130,7 +2140,9 @@ export function createChatServer(deps: ServerDeps): Server {
2130
2140
  const durationMs = Date.now() - startedAt.getTime();
2131
2141
  try {
2132
2142
  // 结算(I4):把占位换成实际消耗,多退少补。条件更新保证幂等(I6)。
2133
- await deps.ledger.settle({ operationId, userId, persona, tokens: usage.total });
2143
+ // CU 闸:按本次 run 的起始 tier 模型把 usage 折算成 CU(failover/多 phase 见 CostTable 边界)。
2144
+ const cu = deps.costTable?.cuFor(tier.provider, tier.model, usage) ?? 0;
2145
+ await deps.ledger.settle({ operationId, userId, persona, tokens: usage.total, cu });
2134
2146
  // Charge after the fact: the run already spent the tokens; the balance gate stops the next one.
2135
2147
  if (billingActive) {
2136
2148
  // 池优先拆账(结算=发放检查点之二):结算时**重读**池——
package/src/session.ts CHANGED
@@ -23,21 +23,27 @@ import {
23
23
  type Theme,
24
24
  type ToolDefinition,
25
25
  } from "@earendil-works/pi-coding-agent";
26
- import type { McpServerDecl } from "@njuptlzf/agent-chat-protocol";
26
+ import type { McpServerDecl, MemoryProviderSession } from "@njuptlzf/agent-chat-protocol";
27
27
  import { toProxySpawn, type McpConfigStore } from "./mcp-config.ts";
28
- import { scopedMemory } from "pi-scoped-memory";
28
+ import { loadMemoryProviderModule, resolveMemoryProvider } from "./memory-providers.ts";
29
29
  import type { LoadedProduct, ResolvedTier, ToolContext } from "./manifest.ts";
30
30
  import { agentFor, resolveTier } from "./manifest.ts";
31
31
  import { applyPersonaOverride, type PersonaConfigStore, type PersonaOverride } from "./persona-config.ts";
32
+ import { validateModelsCost } from "./cost-model.ts";
32
33
 
33
34
  /**
34
35
  * One shared ModelRuntime per process. Credentials live on the server only and
35
36
  * are injected from environment variables, never read from a user's home dir.
36
37
  */
37
38
  export async function createModelRuntime(dataDir: string, provider: string): Promise<ModelRuntime> {
39
+ // 成本模型校验:缺 costModel 声明放行(走全局倍率兜底),但声明了却非法的模型 fail fast。
40
+ // 2026-11-18 口径修订:ModelScope 这类按次计费模型不可能逐模型声明,统一 token×倍率兜底。
41
+ const modelsPath = `${dataDir}/models.json`;
42
+ const rawModels = JSON.parse(readFileSync(modelsPath, "utf8")) as unknown;
43
+ validateModelsCost(rawModels);
38
44
  const runtime = await ModelRuntime.create({
39
45
  authPath: `${dataDir}/auth.json`,
40
- modelsPath: `${dataDir}/models.json`,
46
+ modelsPath,
41
47
  });
42
48
  const envName = `${provider.toUpperCase().replace(/[^A-Z0-9]/g, "_")}_API_KEY`;
43
49
  const key = process.env[envName];
@@ -476,6 +482,15 @@ export function compileSystemPrompt(
476
482
  };
477
483
  }
478
484
 
485
+ /**
486
+ * Provider handle per live session.
487
+ *
488
+ * A WeakMap rather than a field on the session: the handle is host bookkeeping
489
+ * (store connection, lock file), and the session object belongs to pi. Keying by
490
+ * session also means a crashed session cannot leak a handle into the next one.
491
+ */
492
+ const memorySessionsByAgent = new WeakMap<AgentSession, MemoryProviderSession<ExtensionAPI>>();
493
+
479
494
  /**
480
495
  * Build a pi session locked down for multi-user serving:
481
496
  * - no built-in tools (no bash / read / write / edit), verified by test
@@ -583,17 +598,32 @@ export async function createProductSession(opts: CreateProductSessionOptions): P
583
598
  ]
584
599
  : [];
585
600
 
586
- // scopedMemory (plan B; npm 版本消费,正本在 github.com/njuptlzf/pi-scoped-memory):
587
- // one factory per session,
588
- // pointed at the tenant/scope directory the host computes. The plugin itself only
589
- // knows a directory — no userId/scope inside it, so it stays open-sourceable
590
- // without business knowledge. `qual || "default"` keeps single-track products
591
- // whole. `autoCapture` is host policy: every 10 turns one cheap completion
592
- // distils the tail of the transcript into a remembered point (secret-filtered,
593
- // silent on failure; the same opt-in stays false in the standalone CLI).
594
- const memoryExtensions = product.memoryEnabled
595
- ? [scopedMemory({ dir: path.join(opts.dataDir, "memory", opts.userId, opts.qual || "default"), autoCapture: true })]
596
- : [];
601
+ // Memory comes from the provider registry, not from a hardcoded plugin:
602
+ // one open() per session, pointed at the tenant/scope directory the host
603
+ // computes. The provider never learns userId/scope semantics beyond that root,
604
+ // so an implementation can stay generic (and open-sourceable). Swapping the
605
+ // engine is a manifest edit — `agent.memory.provider`.
606
+ //
607
+ // External modules load here, before resolution: the platform does not know
608
+ // `hermes-scoped` exists until the product declares the module that provides
609
+ // it. Loading is per-file memoised, so this is one dynamic import per process.
610
+ if (product.memoryEnabled) {
611
+ for (const entry of product.memoryProviderModules) {
612
+ await loadMemoryProviderModule(entry, product.dir);
613
+ }
614
+ }
615
+ // Two disjoint roots per session: the scope (user × product × qual) and the
616
+ // user's cross-scope profile. Siblings, not nested — erasing one qualification
617
+ // must not delete the user's identity, and the provider enforces that too.
618
+ const memorySession = product.memoryEnabled
619
+ ? await resolveMemoryProvider(product.memoryProviderId).open({
620
+ storageRoot: path.join(opts.dataDir, "memory", opts.userId, opts.qual || "default"),
621
+ sharedRoot: path.join(opts.dataDir, "memory", opts.userId, "profile"),
622
+ agentDir: opts.dataDir,
623
+ scope: { userId: opts.userId, productId: product.manifest.id, qual: opts.qual || "" },
624
+ })
625
+ : null;
626
+ const memoryExtensions = memorySession ? [memorySession.factory] : [];
597
627
 
598
628
  // pi prints the working directory into the system prompt, so use the per-product
599
629
  // data dir (empty of source code) rather than the product's source directory.
@@ -611,13 +641,19 @@ export async function createProductSession(opts: CreateProductSessionOptions): P
611
641
  // browser never has to remember to repeat it. `label` is display text the
612
642
  // operator wrote; ids alone would be meaningless to the model.
613
643
  const qualSection = qualSectionOf(product, opts.qual);
644
+ // A provider that does not inject on its own hands us the text; the host puts
645
+ // it where prompt sections belong. Asking for it here — before the session
646
+ // exists — is why `open()` and `mount()` are separate in the port.
647
+ const memorySection = memorySession && !memorySession.selfInjects
648
+ ? await memorySession.promptSection()
649
+ : "";
614
650
  const resourceLoader = new DefaultResourceLoader({
615
651
  cwd,
616
652
  agentDir: opts.dataDir,
617
653
  settingsManager,
618
654
  systemPrompt: effective.prompt,
619
655
  ...(() => {
620
- const extra = [qualSection, skillSupport.promptSection].filter((s) => s !== "");
656
+ const extra = [qualSection, memorySection, skillSupport.promptSection].filter((s) => s !== "");
621
657
  return extra.length > 0 ? { appendSystemPrompt: extra } : {};
622
658
  })(),
623
659
  // Manifest-declared extensions (operator-reviewed repo code). They load even
@@ -649,14 +685,18 @@ export async function createProductSession(opts: CreateProductSessionOptions): P
649
685
  sessionManager: SessionManager.inMemory(cwd, undefined, opts.history),
650
686
  settingsManager,
651
687
  });
652
- if (mcpExtensions.length > 0 || product.extensionPaths.length > 0) {
688
+ if (mcpExtensions.length > 0 || product.extensionPaths.length > 0 || memorySession) {
653
689
  // Emits session_start, which is when the MCP extension connects its servers
654
690
  // (in the background; the first prompt waits for "direct" servers) and when
655
691
  // manifest-declared extensions run their own startup handlers.
692
+ // A self-injecting memory provider needs this too: without the bind its
693
+ // before_agent_start hook never runs, and the failure mode is silence (tools
694
+ // work, the policy prompt never appears).
656
695
  // mode "rpc" is pi's own non-terminal contract: dialogs are data, not TUI
657
696
  // components — exactly what our SSE bridge below implements.
658
697
  await session.bindExtensions({ uiContext: createExtensionUIContext(opts), mode: "rpc" });
659
698
  }
699
+ if (memorySession) memorySessionsByAgent.set(session, memorySession);
660
700
  return session;
661
701
  }
662
702
 
@@ -675,6 +715,11 @@ export async function disposeProductSession(session: AgentSession): Promise<void
675
715
  }
676
716
  } finally {
677
717
  session.dispose();
718
+ // Provider handles go last and separately: the extension's shutdown handlers
719
+ // run inside a live session, while dispose() closes store/lock connections.
720
+ const memory = memorySessionsByAgent.get(session);
721
+ memorySessionsByAgent.delete(session);
722
+ if (memory) await memory.dispose();
678
723
  }
679
724
  }
680
725
 
@@ -3,7 +3,7 @@ import { DatabaseSync } from "node:sqlite";
3
3
  import type { FileEntry } from "@earendil-works/pi-coding-agent";
4
4
  import type { FeedbackEntry, FeedbackKind, FeedbackRating, FeedbackStatus, SessionSummary, TokenUsage } from "@njuptlzf/agent-chat-protocol";
5
5
  import { DEFAULT_PERSONA_ID } from "@njuptlzf/agent-chat-protocol";
6
- import { assertMembershipWrite, assertProductId, summarizeHistory, quotaBlockedReason } from "./stores.ts";
6
+ import { assertMembershipWrite, assertProductId, summarizeHistory, quotaBlockedCostReason, quotaBlockedReason } from "./stores.ts";
7
7
  import type { AccountStateStore, AuditEvent, AuditQuery, AuditRow, AuditStore, BillingAdjustOpts, BillingEntry, BillingOrder, BillingStore, NewOrderMeta, HistoryStore, MembershipStore, MemberPoolRow, MemberPoolStore, RunLogRecord, RunLogStore, RunUsageReport, RunUsageSummary } from "./stores.ts";
8
8
  import type { Ledger, ReserveInput, ReserveOutcome, SettleInput, SettleOutcome } from "./ledger.ts";
9
9
  import { RESERVATION_TTL_MS } from "./ledger.ts";
@@ -65,6 +65,8 @@ export function openDatabase(file: string, defaultScope = "", defaultPersona = D
65
65
  persona TEXT NOT NULL,
66
66
  day TEXT NOT NULL,
67
67
  tokens INTEGER NOT NULL DEFAULT 0,
68
+ -- 成本模型(CU 维度):独立额度闸,与 token 维度并列(用户拍板:不并入 T3)。
69
+ cost_units INTEGER NOT NULL DEFAULT 0,
68
70
  state TEXT NOT NULL,
69
71
  created_at INTEGER NOT NULL,
70
72
  expires_at INTEGER NOT NULL
@@ -315,6 +317,10 @@ function migrateScope(db: DatabaseSync, defaultScope: string, defaultPersona: st
315
317
  db.exec("ALTER TABLE feedback ADD COLUMN kind TEXT NOT NULL DEFAULT 'rating'");
316
318
  }
317
319
  if (!columns("feedback").includes("content")) db.exec("ALTER TABLE feedback ADD COLUMN content TEXT");
320
+ // 成本模型:预留账本加 CU 维度(独立额度闸)。老行 0 = 迁移前无成本计量,不回填。
321
+ if (!columns("quota_reservations").includes("cost_units")) {
322
+ db.exec("ALTER TABLE quota_reservations ADD COLUMN cost_units INTEGER NOT NULL DEFAULT 0");
323
+ }
318
324
  if (!columns("feedback").includes("status")) {
319
325
  db.exec("ALTER TABLE feedback ADD COLUMN status TEXT NOT NULL DEFAULT 'new'");
320
326
  }
@@ -565,6 +571,14 @@ export class SqliteLedger implements Ledger {
565
571
  return row?.tokens ?? 0;
566
572
  }
567
573
 
574
+ /** 今日已结算的 CU(成本模型独立闸)——结算后的预留仍留在表里,按 state='settled' 求和。 */
575
+ private cuUsed(userId: string, persona: string, day: string): number {
576
+ const row = this.db
577
+ .prepare("SELECT COALESCE(SUM(cost_units), 0) AS n FROM quota_reservations WHERE user_id = ? AND persona = ? AND day = ? AND state = 'settled'")
578
+ .get(userId, persona, day) as { n: number };
579
+ return row.n;
580
+ }
581
+
568
582
  async reserve(input: ReserveInput): Promise<ReserveOutcome> {
569
583
  const nowMs = this.now().getTime();
570
584
  const day = this.day();
@@ -614,13 +628,34 @@ export class SqliteLedger implements Ledger {
614
628
  if (cap !== undefined && cap > 0) reserveTokens = Math.min(cap, tokenCap - used - reserved.n);
615
629
  }
616
630
 
631
+ // 成本模型 CU 闸(独立于 token):同一 BEGIN IMMEDIATE 事务内判定,任一维触顶即拒。
632
+ let reserveCu = 0;
633
+ const dailyCostCap = input.caps?.dailyCostCap;
634
+ if (dailyCostCap !== undefined && dailyCostCap > 0) {
635
+ const usedCu = this.cuUsed(input.userId, input.persona, day);
636
+ const reservedCu = (
637
+ this.db
638
+ .prepare(
639
+ `SELECT COALESCE(SUM(cost_units), 0) AS n FROM quota_reservations
640
+ WHERE user_id = ? AND persona = ? AND day = ? AND state = 'reserved' AND expires_at > ?`,
641
+ )
642
+ .get(input.userId, input.persona, day, nowMs) as { n: number }
643
+ ).n;
644
+ if (usedCu + reservedCu >= dailyCostCap) {
645
+ this.db.exec("ROLLBACK");
646
+ return { ok: false, reason: quotaBlockedCostReason(input.caps ?? {}) };
647
+ }
648
+ const cap = input.caps?.perRunCostCap;
649
+ if (cap !== undefined && cap > 0) reserveCu = Math.min(cap, dailyCostCap - usedCu - reservedCu);
650
+ }
651
+
617
652
  const expiresAt = nowMs + (input.ttlMs ?? RESERVATION_TTL_MS);
618
653
  this.db
619
654
  .prepare(
620
- `INSERT INTO quota_reservations (operation_id, user_id, persona, day, tokens, state, created_at, expires_at)
621
- VALUES (?, ?, ?, ?, ?, 'reserved', ?, ?)`,
655
+ `INSERT INTO quota_reservations (operation_id, user_id, persona, day, tokens, cost_units, state, created_at, expires_at)
656
+ VALUES (?, ?, ?, ?, ?, ?, 'reserved', ?, ?)`,
622
657
  )
623
- .run(input.operationId, input.userId, input.persona, day, reserveTokens, nowMs, expiresAt);
658
+ .run(input.operationId, input.userId, input.persona, day, reserveTokens, reserveCu, nowMs, expiresAt);
624
659
  this.db.exec("COMMIT");
625
660
  return { ok: true, reservation: { operationId: input.operationId, expiresAt } };
626
661
  } catch (err) {
@@ -637,10 +672,10 @@ export class SqliteLedger implements Ledger {
637
672
  // 重复 settle 直接返回 already(不重复扣费)。
638
673
  const res = this.db
639
674
  .prepare(
640
- `UPDATE quota_reservations SET state = 'settled', tokens = ?
675
+ `UPDATE quota_reservations SET state = 'settled', tokens = ?, cost_units = ?
641
676
  WHERE operation_id = ? AND state = 'reserved'`,
642
677
  )
643
- .run(input.tokens, input.operationId) as { changes: number };
678
+ .run(input.tokens, input.cu ?? 0, input.operationId) as { changes: number };
644
679
  if (res.changes === 0) {
645
680
  this.db.exec("COMMIT");
646
681
  return "already";
package/src/stores.ts CHANGED
@@ -178,6 +178,18 @@ export interface QuotaCaps {
178
178
  * 当日总量判定。<=0/缺省 = 不启用(预留写 0,W3 现状)。
179
179
  */
180
180
  perRunTokenCap?: number;
181
+ /**
182
+ * 成本模型(CU 维度,独立于 token 的第三闸):每 run 的 CU 上界与每日 CU 总量。
183
+ * 独立于 T3 的 tokenCap 体系——按次计费换算不成 token,并入会编造假 token 值导致失真。
184
+ */
185
+ perRunCostCap?: number;
186
+ dailyCostCap?: number;
187
+ }
188
+
189
+ /** 成本额度(CU)撞闸文案——与 token/run 并列,明示这是成本维度而非 token 维度。 */
190
+ export function quotaBlockedCostReason(caps: { dailyCostCap?: number }): string {
191
+ const cap = caps.dailyCostCap;
192
+ return `今日成本额度已用完${cap !== undefined && cap > 0 ? `(上限 ${cap} CU)` : ""},次日重置;如需提额请联系管理员`;
181
193
  }
182
194
 
183
195
  /** 万位读法:5000000 → 「500 万」(撞闸文案与权益卡共用,别在 UI 里二次造轮子)。 */