@aalis/plugin-prompt-budget 0.1.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.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @aalis/plugin-prompt-budget —— 暴露 Prompt 预算自检工具
3
+ *
4
+ * 设计动机:plugin-agent-default 已 emit `token:usage` 事件(含 12 桶 breakdown),
5
+ * WebUI 通过 plugin-webui-server 订阅并渲染面板。但 AI 自己跑在工具循环里时,
6
+ * 没有看 WebUI 的机会——需要一个**主动 query** 路径,让模型怀疑"是不是 prompt 太大了"
7
+ * 时能立即查到当前 session 的最新预算消耗。
8
+ *
9
+ * 实现:监听 `token:usage` 事件维护 sessionId → 最新 usage 缓存,注册
10
+ * `prompt_budget_info` 工具供 AI 查询。零业务逻辑,纯只读 introspection。
11
+ */
12
+ import type { Context } from '@aalis/core';
13
+ import '@aalis/plugin-tools-api';
14
+ export declare const name = "@aalis/plugin-prompt-budget";
15
+ export declare const displayName = "Prompt \u9884\u7B97\u81EA\u68C0";
16
+ export declare const subsystem = "agent";
17
+ export declare function apply(ctx: Context): void;
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,yBAAyB,CAAC;AAEjC,eAAO,MAAM,IAAI,gCAAgC,CAAC;AAClD,eAAO,MAAM,WAAW,oCAAgB,CAAC;AACzC,eAAO,MAAM,SAAS,UAAU,CAAC;AAQjC,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CA6ExC"}
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @aalis/plugin-prompt-budget —— 暴露 Prompt 预算自检工具
3
+ *
4
+ * 设计动机:plugin-agent-default 已 emit `token:usage` 事件(含 12 桶 breakdown),
5
+ * WebUI 通过 plugin-webui-server 订阅并渲染面板。但 AI 自己跑在工具循环里时,
6
+ * 没有看 WebUI 的机会——需要一个**主动 query** 路径,让模型怀疑"是不是 prompt 太大了"
7
+ * 时能立即查到当前 session 的最新预算消耗。
8
+ *
9
+ * 实现:监听 `token:usage` 事件维护 sessionId → 最新 usage 缓存,注册
10
+ * `prompt_budget_info` 工具供 AI 查询。零业务逻辑,纯只读 introspection。
11
+ */
12
+ import { useToolService } from '@aalis/plugin-tools-api';
13
+ import '@aalis/plugin-tools-api';
14
+ export const name = '@aalis/plugin-prompt-budget';
15
+ export const displayName = 'Prompt 预算自检';
16
+ export const subsystem = 'agent';
17
+ export function apply(ctx) {
18
+ const tools = useToolService(ctx);
19
+ /** sessionId → 最新一次的 token:usage 快照(仅保留最近一次即可) */
20
+ const cache = new Map();
21
+ ctx.on('token:usage', u => {
22
+ if (!u || typeof u.sessionId !== 'string')
23
+ return;
24
+ cache.set(u.sessionId, { ...u, observedAt: Date.now() });
25
+ });
26
+ tools.register({
27
+ definition: {
28
+ type: 'function',
29
+ function: {
30
+ name: 'prompt_budget_info',
31
+ description: '查询当前 session 最近一次 LLM 调用的 prompt token 预算消耗。' +
32
+ '当你怀疑"system prompt 过大""上下文快满了"或想验证压缩 / 工具裁剪是否生效时使用。' +
33
+ '返回 12 桶 breakdown(system / persona / memorySummary / memoryVector / skills / ' +
34
+ 'platform / subtask / systemOther / history / toolResults / toolDefs / reservedForReply)' +
35
+ '及 used / contextWindow / usageRatio。' +
36
+ '注意:数据由上一次 LLM 调用 emit,刚启动或未调用时返回 noData。',
37
+ parameters: {
38
+ type: 'object',
39
+ properties: {
40
+ sessionId: {
41
+ type: 'string',
42
+ description: '可选。默认使用当前调用 session(callCtx.sessionId);传入可查询其他会话。',
43
+ },
44
+ },
45
+ required: [],
46
+ additionalProperties: false,
47
+ },
48
+ },
49
+ },
50
+ handler: async (args, callCtx) => {
51
+ const sessionId = args.sessionId || callCtx.sessionId;
52
+ const u = cache.get(sessionId);
53
+ if (!u) {
54
+ return JSON.stringify({
55
+ noData: true,
56
+ sessionId,
57
+ advice: '本 session 尚未产生 token:usage 事件(可能是首轮调用前或本 session 从未调过 LLM)。' +
58
+ '在 LLM 至少调用一次后再查询本工具即可。',
59
+ });
60
+ }
61
+ const ageMs = Date.now() - u.observedAt;
62
+ const tag = u.usageRatio >= 0.85 ? 'CRITICAL' : u.usageRatio >= 0.7 ? 'WARN' : u.usageRatio >= 0.5 ? 'INFO' : 'OK';
63
+ // top3 提示哪几个桶最大
64
+ const buckets = Object.entries(u.breakdown).sort((a, b) => b[1] - a[1]);
65
+ const top3 = buckets.slice(0, 3).map(([k, v]) => ({ name: k, tokens: v }));
66
+ const advice = u.usageRatio >= 0.85
67
+ ? '上下文几乎用尽。建议:调用 memory.compress / 清理 toolResults / 缩减 system prompt。'
68
+ : u.usageRatio >= 0.7
69
+ ? '上下文压力较高。可考虑主动压缩历史或减少后续工具调用的输出体量。'
70
+ : '预算健康,无需干预。';
71
+ return JSON.stringify({
72
+ sessionId,
73
+ ageMs,
74
+ observedAt: new Date(u.observedAt).toISOString(),
75
+ tag,
76
+ used: u.used,
77
+ contextWindow: u.contextWindow,
78
+ usageRatio: Number(u.usageRatio.toFixed(4)),
79
+ maxTokens: u.maxTokens,
80
+ tokenBudget: u.tokenBudget,
81
+ breakdown: u.breakdown,
82
+ top3,
83
+ advice,
84
+ });
85
+ },
86
+ });
87
+ ctx.logger.info('prompt_budget_info 工具已注册');
88
+ }
89
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,yBAAyB,CAAC;AAEjC,MAAM,CAAC,MAAM,IAAI,GAAG,6BAA6B,CAAC;AAClD,MAAM,CAAC,MAAM,WAAW,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AI,EAAE,CAAC,CAAC,IAAI;gBACZ,aAAa,EAAE,CAAC,CAAC,aAAa;gBAC9B,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;gBAC3C,SAAS,EAAE,CAAC,CAAC,SAAS;gBACtB,WAAW,EAAE,CAAC,CAAC,WAAW;gBAC1B,SAAS,EAAE,CAAC,CAAC,SAAS;gBACtB,IAAI;gBACJ,MAAM;aACP,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;IAEH,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;AAC9C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@aalis/plugin-prompt-budget",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "keywords": [
6
+ "aalis-plugin"
7
+ ],
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "dependencies": {
14
+ "@aalis/plugin-tools-api": "^0.1.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^22.0.0",
18
+ "typescript": "^5.7.0",
19
+ "@aalis/core": "0.1.0",
20
+ "@aalis/plugin-agent-api": "0.1.0"
21
+ },
22
+ "peerDependencies": {
23
+ "@aalis/core": "^0.1.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "dev": "tsc --watch"
28
+ }
29
+ }