@wu529778790/open-im 1.11.2-beta.2 → 1.11.2-beta.3
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/index.js +3 -10
- package/dist/logger.d.ts +0 -3
- package/dist/logger.js +5 -36
- package/dist/shared/ai-task.js +3 -49
- package/dist/shared/task-cleanup.js +1 -11
- package/package.json +1 -1
- package/dist/telemetry/hash-user.d.ts +0 -2
- package/dist/telemetry/hash-user.js +0 -5
- package/dist/telemetry/telemetry-sanitize.d.ts +0 -2
- package/dist/telemetry/telemetry-sanitize.js +0 -30
package/dist/index.js
CHANGED
|
@@ -32,7 +32,7 @@ import { initAdapters, cleanupAdapters } from "./adapters/registry.js";
|
|
|
32
32
|
import { SessionManager } from "./session/session-manager.js";
|
|
33
33
|
import { loadActiveChats, getActiveChatId, flushActiveChats, } from "./shared/active-chats.js";
|
|
34
34
|
import { destroyAllLiveChildren } from "./shared/process-kill.js";
|
|
35
|
-
import { initLogger, createLogger, closeLogger,
|
|
35
|
+
import { initLogger, createLogger, closeLogger, } from "./logger.js";
|
|
36
36
|
import { APP_HOME, SHUTDOWN_PORT } from "./constants.js";
|
|
37
37
|
import { createRequire } from "node:module";
|
|
38
38
|
import { escapePathForMarkdown, getAIToolDisplayName } from "./shared/utils.js";
|
|
@@ -267,10 +267,6 @@ export async function main() {
|
|
|
267
267
|
}
|
|
268
268
|
log.info("Service is running. Press Ctrl+C to stop.");
|
|
269
269
|
log.info(`Successfully initialized platforms: ${successfulPlatforms.join(", ")}`);
|
|
270
|
-
emitStructuredEvent("Main", "service.platform.init", {
|
|
271
|
-
platforms: successfulPlatforms,
|
|
272
|
-
version: APP_VERSION,
|
|
273
|
-
});
|
|
274
270
|
// Send notification only to successfully initialized platforms
|
|
275
271
|
for (const platform of successfulPlatforms) {
|
|
276
272
|
const startupMsg = buildStartupMessage(platform, APP_VERSION, resolvePlatformAiCommand(config, platform), startupCwd, sessionManager);
|
|
@@ -328,7 +324,6 @@ export async function main() {
|
|
|
328
324
|
sessionManager.destroy();
|
|
329
325
|
cleanupAdapters();
|
|
330
326
|
flushActiveChats();
|
|
331
|
-
await shutdownLoggerTelemetry();
|
|
332
327
|
await flushSentry();
|
|
333
328
|
await closeLogger();
|
|
334
329
|
process.exit(0);
|
|
@@ -381,8 +376,7 @@ export async function main() {
|
|
|
381
376
|
}
|
|
382
377
|
}
|
|
383
378
|
}
|
|
384
|
-
void
|
|
385
|
-
.then(() => closeLogger())
|
|
379
|
+
void closeLogger()
|
|
386
380
|
.finally(() => process.exit(1));
|
|
387
381
|
});
|
|
388
382
|
}
|
|
@@ -391,8 +385,7 @@ const isEntry = process.argv[1]?.replace(/\\/g, "/").endsWith("/index.js") ||
|
|
|
391
385
|
if (isEntry) {
|
|
392
386
|
main().catch((err) => {
|
|
393
387
|
log.error("Fatal error:", err);
|
|
394
|
-
void
|
|
395
|
-
.then(() => closeLogger())
|
|
388
|
+
void closeLogger()
|
|
396
389
|
.finally(() => process.exit(1));
|
|
397
390
|
});
|
|
398
391
|
}
|
package/dist/logger.d.ts
CHANGED
|
@@ -21,14 +21,11 @@ export declare function createLogger(tag: string): {
|
|
|
21
21
|
warn: (msg: string, ...args: unknown[]) => void;
|
|
22
22
|
error: (msg: string, ...args: unknown[]) => void;
|
|
23
23
|
debug: (msg: string, ...args: unknown[]) => void;
|
|
24
|
-
infoEvent: (event: string, data?: Record<string, unknown>, msg?: string) => void;
|
|
25
24
|
};
|
|
26
25
|
/**
|
|
27
26
|
* Audit log — records user interactions for debugging and compliance.
|
|
28
27
|
* Always enabled, writes to audit.log.
|
|
29
28
|
*/
|
|
30
29
|
export declare function auditLog(platform: string, userId: string, action: string, detail?: Record<string, unknown>): void;
|
|
31
|
-
export declare function emitStructuredEvent(tag: string, event: string, data?: Record<string, unknown>, level?: LogLevel, msg?: string): void;
|
|
32
|
-
export declare function shutdownLoggerTelemetry(): Promise<void>;
|
|
33
30
|
export declare function closeLogger(): Promise<void>;
|
|
34
31
|
export {};
|
package/dist/logger.js
CHANGED
|
@@ -3,16 +3,13 @@ import { join } from 'node:path';
|
|
|
3
3
|
import { finished } from 'node:stream/promises';
|
|
4
4
|
import { sanitize } from './sanitize.js';
|
|
5
5
|
import { APP_HOME } from './constants.js';
|
|
6
|
-
import { sanitizeTelemetryData } from './telemetry/telemetry-sanitize.js';
|
|
7
6
|
const DEFAULT_LOG_DIR = join(APP_HOME, 'logs');
|
|
8
7
|
const MAX_LOG_FILES = 10;
|
|
9
8
|
const LOG_LEVELS = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3 };
|
|
10
9
|
let logDir = DEFAULT_LOG_DIR;
|
|
11
10
|
let minLevel = LOG_LEVELS.DEBUG;
|
|
12
11
|
let logStream;
|
|
13
|
-
let eventsStream;
|
|
14
12
|
let auditStream;
|
|
15
|
-
let telemetryEnabled = false;
|
|
16
13
|
function pad(n) {
|
|
17
14
|
return String(n).padStart(2, '0');
|
|
18
15
|
}
|
|
@@ -80,15 +77,6 @@ export function initLogger(dirOrOpts, level, telemetry) {
|
|
|
80
77
|
auditStream = undefined;
|
|
81
78
|
}
|
|
82
79
|
auditStream = createWriteStream(join(logDir, 'audit.log'), { flags: 'a' });
|
|
83
|
-
telemetryEnabled = !!tel?.enabled;
|
|
84
|
-
if (eventsStream) {
|
|
85
|
-
eventsStream.end();
|
|
86
|
-
eventsStream = undefined;
|
|
87
|
-
}
|
|
88
|
-
if (telemetryEnabled) {
|
|
89
|
-
rotateOldJsonl();
|
|
90
|
-
eventsStream = createWriteStream(join(logDir, getEventsFileName()), { flags: 'a' });
|
|
91
|
-
}
|
|
92
80
|
}
|
|
93
81
|
function write(level, tag, msg, ...args) {
|
|
94
82
|
if (LOG_LEVELS[level] < minLevel)
|
|
@@ -109,7 +97,6 @@ export function createLogger(tag) {
|
|
|
109
97
|
warn: (msg, ...args) => write('WARN', tag, msg, ...args),
|
|
110
98
|
error: (msg, ...args) => write('ERROR', tag, msg, ...args),
|
|
111
99
|
debug: (msg, ...args) => write('DEBUG', tag, msg, ...args),
|
|
112
|
-
infoEvent: (event, data, msg) => emitStructuredEvent(tag, event, data, 'INFO', msg),
|
|
113
100
|
};
|
|
114
101
|
}
|
|
115
102
|
/**
|
|
@@ -126,31 +113,13 @@ export function auditLog(platform, userId, action, detail) {
|
|
|
126
113
|
};
|
|
127
114
|
auditStream?.write(JSON.stringify(entry) + '\n');
|
|
128
115
|
}
|
|
129
|
-
export function emitStructuredEvent(tag, event, data, level = 'INFO', msg = '') {
|
|
130
|
-
if (!telemetryEnabled)
|
|
131
|
-
return;
|
|
132
|
-
const payload = {
|
|
133
|
-
v: 1,
|
|
134
|
-
ts: new Date().toISOString(),
|
|
135
|
-
level,
|
|
136
|
-
tag,
|
|
137
|
-
event,
|
|
138
|
-
msg,
|
|
139
|
-
data: sanitizeTelemetryData(data),
|
|
140
|
-
};
|
|
141
|
-
const line = `${JSON.stringify(payload)}\n`;
|
|
142
|
-
eventsStream?.write(line);
|
|
143
|
-
}
|
|
144
|
-
export async function shutdownLoggerTelemetry() {
|
|
145
|
-
// Local event logging only — no upload to clean up
|
|
146
|
-
}
|
|
147
116
|
export async function closeLogger() {
|
|
148
|
-
if (
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
117
|
+
if (auditStream) {
|
|
118
|
+
const as = auditStream;
|
|
119
|
+
auditStream = undefined;
|
|
120
|
+
as.end();
|
|
152
121
|
try {
|
|
153
|
-
await finished(
|
|
122
|
+
await finished(as);
|
|
154
123
|
}
|
|
155
124
|
catch {
|
|
156
125
|
/* ignore */
|
package/dist/shared/ai-task.js
CHANGED
|
@@ -4,9 +4,7 @@
|
|
|
4
4
|
import { resolvePlatformAiCommand } from '../config.js';
|
|
5
5
|
import { captureError } from './sentry.js';
|
|
6
6
|
import { formatToolStats, formatToolCallNotification, getContextWarning, getAIToolDisplayName, toReplyPlainText, } from './utils.js';
|
|
7
|
-
import { createLogger
|
|
8
|
-
import { hashUserId } from '../telemetry/hash-user.js';
|
|
9
|
-
import { sanitize } from '../sanitize.js';
|
|
7
|
+
import { createLogger } from '../logger.js';
|
|
10
8
|
const log = createLogger('AITask');
|
|
11
9
|
function isUsageLimitError(error) {
|
|
12
10
|
return /usage limit/i.test(error) || /try again at\s+\d{1,2}:\d{2}\s*(AM|PM)/i.test(error);
|
|
@@ -154,12 +152,6 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
154
152
|
const aiCommand = resolvePlatformAiCommand(config, ctx.platform);
|
|
155
153
|
const startRun = () => {
|
|
156
154
|
log.info(`[AITask] Starting: userId=${ctx.userId}, initialSessionId=${currentSessionId ?? 'new'}, prompt="${prompt.slice(0, 50)}..."`);
|
|
157
|
-
emitStructuredEvent('AITask', 'ai.task.start', {
|
|
158
|
-
platform: ctx.platform,
|
|
159
|
-
taskKey: hashUserId(ctx.taskKey),
|
|
160
|
-
userKey: hashUserId(ctx.userId),
|
|
161
|
-
toolId: aiCommand,
|
|
162
|
-
});
|
|
163
155
|
activeHandle = toolAdapter.run(prompt, currentSessionId, ctx.workDir, {
|
|
164
156
|
onSessionId: (id) => {
|
|
165
157
|
log.info(`[AITask] SessionId callback: old=${currentSessionId ?? 'none'}, new=${id}, aiCommand=${aiCommand}, userId=${ctx.userId}`);
|
|
@@ -218,17 +210,6 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
218
210
|
log.info(`[AITask] onComplete fired: settled=${settled}, success=${result.success}, platform=${ctx.platform}, taskKey=${ctx.taskKey}`);
|
|
219
211
|
if (settled)
|
|
220
212
|
return;
|
|
221
|
-
emitStructuredEvent('AITask', 'ai.task.complete', {
|
|
222
|
-
platform: ctx.platform,
|
|
223
|
-
taskKey: hashUserId(ctx.taskKey),
|
|
224
|
-
userKey: hashUserId(ctx.userId),
|
|
225
|
-
toolId: aiCommand,
|
|
226
|
-
durationMs: result.durationMs,
|
|
227
|
-
success: result.success,
|
|
228
|
-
numTurns: result.numTurns,
|
|
229
|
-
model: result.model,
|
|
230
|
-
toolStats: result.toolStats,
|
|
231
|
-
});
|
|
232
213
|
settled = true;
|
|
233
214
|
if (pendingUpdate) {
|
|
234
215
|
clearTimeout(pendingUpdate);
|
|
@@ -282,15 +263,6 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
282
263
|
}
|
|
283
264
|
settled = true;
|
|
284
265
|
log.error(`Task error for user ${ctx.userId}: ${error}`);
|
|
285
|
-
emitStructuredEvent('AITask', 'ai.task.error', {
|
|
286
|
-
platform: ctx.platform,
|
|
287
|
-
taskKey: hashUserId(ctx.taskKey),
|
|
288
|
-
userKey: hashUserId(ctx.userId),
|
|
289
|
-
toolId: aiCommand,
|
|
290
|
-
durationMs: Date.now() - taskState.startedAt,
|
|
291
|
-
errorSnippet: sanitize(String(error).slice(0, 400)),
|
|
292
|
-
errorType: classifyErrorType(String(error)),
|
|
293
|
-
});
|
|
294
266
|
if (isUsageLimitError(error)) {
|
|
295
267
|
// Usage limit errors: keep session for all tools (user can retry later)
|
|
296
268
|
log.warn(`Keeping ${aiCommand} session for user ${ctx.userId} after usage limit error`);
|
|
@@ -330,15 +302,6 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
330
302
|
handle: {
|
|
331
303
|
abort: () => {
|
|
332
304
|
if (!settled) {
|
|
333
|
-
emitStructuredEvent('AITask', 'ai.task.error', {
|
|
334
|
-
platform: ctx.platform,
|
|
335
|
-
taskKey: hashUserId(ctx.taskKey),
|
|
336
|
-
userKey: hashUserId(ctx.userId),
|
|
337
|
-
toolId: aiCommand,
|
|
338
|
-
durationMs: Date.now() - taskState.startedAt,
|
|
339
|
-
errorSnippet: 'aborted',
|
|
340
|
-
errorType: 'aborted',
|
|
341
|
-
});
|
|
342
305
|
// 用户取消(/new、/resume、队列超时、stale 清理):把「思考中…」占位卡片编辑为终态,
|
|
343
306
|
// 避免卡片卡在转圈。停按钮路径会先 settle() 再 abort,settled=true 时此处跳过,不会双发。
|
|
344
307
|
void platformAdapter.sendError('⏹️ 已取消').catch(() => {
|
|
@@ -354,9 +317,9 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
354
317
|
settle,
|
|
355
318
|
startedAt: Date.now(),
|
|
356
319
|
toolId: aiCommand,
|
|
357
|
-
taskKey:
|
|
320
|
+
taskKey: ctx.taskKey,
|
|
358
321
|
platform: ctx.platform,
|
|
359
|
-
userKey:
|
|
322
|
+
userKey: ctx.userId,
|
|
360
323
|
};
|
|
361
324
|
try {
|
|
362
325
|
startRun();
|
|
@@ -371,15 +334,6 @@ export function runAITask(deps, ctx, prompt, toolAdapter, platformAdapter) {
|
|
|
371
334
|
userId: ctx.userId,
|
|
372
335
|
aiCommand,
|
|
373
336
|
});
|
|
374
|
-
emitStructuredEvent('AITask', 'ai.task.error', {
|
|
375
|
-
platform: ctx.platform,
|
|
376
|
-
taskKey: hashUserId(ctx.taskKey),
|
|
377
|
-
userKey: hashUserId(ctx.userId),
|
|
378
|
-
toolId: aiCommand,
|
|
379
|
-
durationMs: 0,
|
|
380
|
-
errorSnippet: sanitize(String(err).slice(0, 400)),
|
|
381
|
-
errorType: classifyErrorType(String(err)),
|
|
382
|
-
});
|
|
383
337
|
platformAdapter
|
|
384
338
|
.sendError(`内部错误:${err instanceof Error ? err.message : String(err)}`)
|
|
385
339
|
.catch(() => {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Tasks older than 30 minutes are aborted and removed from the running-tasks
|
|
5
5
|
* map so they never accumulate indefinitely.
|
|
6
6
|
*/
|
|
7
|
-
import { createLogger
|
|
7
|
+
import { createLogger } from '../logger.js';
|
|
8
8
|
const log = createLogger('TaskCleanup');
|
|
9
9
|
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
|
|
10
10
|
const STALE_THRESHOLD_MS = 30 * 60 * 1000; // 30 minutes
|
|
@@ -46,17 +46,7 @@ export function startTaskCleanup(runningTasks) {
|
|
|
46
46
|
export function emitInterruptedTerminals(runningTasks) {
|
|
47
47
|
if (runningTasks.size === 0)
|
|
48
48
|
return;
|
|
49
|
-
const now = Date.now();
|
|
50
49
|
for (const state of runningTasks.values()) {
|
|
51
|
-
emitStructuredEvent('AITask', 'ai.task.error', {
|
|
52
|
-
platform: state.platform,
|
|
53
|
-
taskKey: state.taskKey,
|
|
54
|
-
userKey: state.userKey,
|
|
55
|
-
toolId: state.toolId,
|
|
56
|
-
durationMs: now - state.startedAt,
|
|
57
|
-
errorSnippet: 'interrupted',
|
|
58
|
-
errorType: 'interrupted',
|
|
59
|
-
});
|
|
60
50
|
// 标记已结算,使随后 shutdown 的 abort() 跳过重复的 aborted 事件
|
|
61
51
|
state.settle();
|
|
62
52
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wu529778790/open-im",
|
|
3
|
-
"version": "1.11.2-beta.
|
|
3
|
+
"version": "1.11.2-beta.3",
|
|
4
4
|
"description": "Your AI coding assistant, in every chat app. Multi-platform IM bridge for Claude Code, Codex, and CodeBuddy.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { sanitize } from '../sanitize.js';
|
|
2
|
-
const MAX_STRING = 512;
|
|
3
|
-
function sanitizeValue(v) {
|
|
4
|
-
if (v === null || typeof v === 'boolean' || typeof v === 'number')
|
|
5
|
-
return v;
|
|
6
|
-
if (typeof v === 'string') {
|
|
7
|
-
const s = sanitize(v.length > MAX_STRING ? `${v.slice(0, MAX_STRING)}…` : v);
|
|
8
|
-
return s;
|
|
9
|
-
}
|
|
10
|
-
if (Array.isArray(v))
|
|
11
|
-
return v.slice(0, 32).map((x) => sanitizeValue(x));
|
|
12
|
-
if (typeof v === 'object') {
|
|
13
|
-
const o = v;
|
|
14
|
-
const out = {};
|
|
15
|
-
let i = 0;
|
|
16
|
-
for (const [k, val] of Object.entries(o)) {
|
|
17
|
-
if (i++ >= 32)
|
|
18
|
-
break;
|
|
19
|
-
out[k] = sanitizeValue(val);
|
|
20
|
-
}
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
/** 结构化遥测 data 字段:截断长串、走 sanitize。 */
|
|
26
|
-
export function sanitizeTelemetryData(data) {
|
|
27
|
-
if (!data)
|
|
28
|
-
return {};
|
|
29
|
-
return sanitizeValue(data);
|
|
30
|
-
}
|