@clawtopia/werewolf-evolution 0.1.4 → 0.1.5
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.d.ts +23 -5
- package/dist/index.js +134 -35
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -116,6 +116,7 @@ interface MessageReceivedLike {
|
|
|
116
116
|
from: string;
|
|
117
117
|
content: string;
|
|
118
118
|
messageId?: string;
|
|
119
|
+
sessionKey?: string;
|
|
119
120
|
runId?: string;
|
|
120
121
|
}
|
|
121
122
|
interface MessageContextLike {
|
|
@@ -144,12 +145,14 @@ interface AgentRunContextLike {
|
|
|
144
145
|
}
|
|
145
146
|
declare class EvolutionContextStore {
|
|
146
147
|
private readonly inboundByRun;
|
|
148
|
+
private readonly inboundBySession;
|
|
147
149
|
private readonly boundByRun;
|
|
148
150
|
private readonly hashByDebriefId;
|
|
149
151
|
private readonly toolCalls;
|
|
150
152
|
capture(event: MessageReceivedLike, ctx: MessageContextLike, now?: number): CapturedInbound | undefined;
|
|
151
|
-
getPending(runId?: string, sessionKey?: string, now?: number): CapturedInbound | undefined;
|
|
152
|
-
|
|
153
|
+
getPending(runId?: string, sessionKey?: string, prompt?: string, now?: number): CapturedInbound | undefined;
|
|
154
|
+
private storeInbound;
|
|
155
|
+
bindAgentRun(event: AgentRunEventLike, ctx: AgentRunContextLike, binding: AgentAccountBinding | undefined, now?: number, explicitOwnerAuthorized?: boolean, pendingInbound?: CapturedInbound): BoundRun | undefined;
|
|
153
156
|
getBound(runId?: string, sessionKey?: string, now?: number): BoundRun | undefined;
|
|
154
157
|
consume(runId: string, sessionKey: string, now?: number): BoundRun;
|
|
155
158
|
authorizeToolCall(runId: string | undefined, sessionKey: string | undefined, toolCallId: string | undefined, now?: number): BoundRun;
|
|
@@ -171,18 +174,33 @@ interface SuggestionDecisionInput {
|
|
|
171
174
|
}
|
|
172
175
|
interface PromptContextLike extends AgentRunContextLike {
|
|
173
176
|
}
|
|
177
|
+
interface PromptBuildEventLike {
|
|
178
|
+
prompt: string;
|
|
179
|
+
}
|
|
180
|
+
interface OrdinaryDirectMessage {
|
|
181
|
+
channelId: string;
|
|
182
|
+
accountId: string;
|
|
183
|
+
from: string;
|
|
184
|
+
sessionKey: string;
|
|
185
|
+
content: string;
|
|
186
|
+
capturedAt: number;
|
|
187
|
+
}
|
|
174
188
|
declare class WerewolfEvolutionService {
|
|
175
189
|
readonly contexts: EvolutionContextStore;
|
|
176
190
|
private readonly version;
|
|
177
191
|
private readonly ordinaryDirectMessages;
|
|
192
|
+
private readonly ordinaryDirectMessagesBySession;
|
|
178
193
|
private readonly ownerDirectRuns;
|
|
179
194
|
private readonly observedRuns;
|
|
180
195
|
constructor(version?: string);
|
|
196
|
+
private cleanupOrdinaryDirectMessages;
|
|
181
197
|
captureMessage(event: MessageReceivedLike, ctx: MessageContextLike): void;
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
private pendingOrdinaryDirectMessage;
|
|
199
|
+
private consumeOrdinaryDirectMessage;
|
|
200
|
+
bindAgentRun(event: AgentRunEventLike, ctx: AgentRunContextLike, binding: AgentAccountBinding | undefined, explicitOwnerAuthorized?: boolean, pendingInbound?: CapturedDebrief | CapturedSuggestionCommand, pendingOrdinary?: OrdinaryDirectMessage): void;
|
|
201
|
+
bindPromptRun(event: PromptBuildEventLike, ctx: AgentRunContextLike, config: unknown): void;
|
|
184
202
|
buildPromptContext(ctx: PromptContextLike): {
|
|
185
|
-
|
|
203
|
+
prependContext: string;
|
|
186
204
|
} | undefined;
|
|
187
205
|
beforeToolCall(event: {
|
|
188
206
|
toolName: string;
|
package/dist/index.js
CHANGED
|
@@ -324,7 +324,7 @@ function isUnknownEligibilityValue(value) {
|
|
|
324
324
|
var CAPTURE_TTL_MS = 10 * 60 * 1e3;
|
|
325
325
|
var MAX_CAPTURES = 256;
|
|
326
326
|
var SUGGESTION_COMMAND = /^\s*(采纳|忽略|拒绝)\s+(S-\d{3,6})\s*[。.!!]?\s*$/i;
|
|
327
|
-
var
|
|
327
|
+
var LEGACY_DIRECT_CONVERSATION = /^private:(?:HUMAN|human):([0-9]+)$/;
|
|
328
328
|
function isExplicitConfiguredOwner(config, channelId, senderId) {
|
|
329
329
|
if (channelId !== "clawtopia" && channelId !== "coolclaw" || !senderId) return false;
|
|
330
330
|
const owners = record(record(config)?.commands)?.ownerAllowFrom;
|
|
@@ -344,8 +344,11 @@ function sameHumanSender(left, right) {
|
|
|
344
344
|
}
|
|
345
345
|
function isHumanDirectMessage(event, ctx) {
|
|
346
346
|
if (ctx.channelId !== "clawtopia" && ctx.channelId !== "coolclaw") return false;
|
|
347
|
-
if (!ctx.conversationId
|
|
348
|
-
|
|
347
|
+
if (!ctx.conversationId) return false;
|
|
348
|
+
const legacy = LEGACY_DIRECT_CONVERSATION.exec(ctx.conversationId);
|
|
349
|
+
const canonical = new RegExp(`^${ctx.channelId}:human:([0-9]+)$`).exec(ctx.conversationId);
|
|
350
|
+
const humanId = legacy?.[1] ?? canonical?.[1];
|
|
351
|
+
return humanId !== void 0 && humanId !== "0" && event.from === `${ctx.channelId}:human:${humanId}`;
|
|
349
352
|
}
|
|
350
353
|
function normalizedAccountId(value) {
|
|
351
354
|
return value?.trim() || "default";
|
|
@@ -365,13 +368,15 @@ function resolveAgentAccountBinding(config, channelId, accountId) {
|
|
|
365
368
|
}
|
|
366
369
|
var EvolutionContextStore = class {
|
|
367
370
|
inboundByRun = /* @__PURE__ */ new Map();
|
|
371
|
+
inboundBySession = /* @__PURE__ */ new Map();
|
|
368
372
|
boundByRun = /* @__PURE__ */ new Map();
|
|
369
373
|
hashByDebriefId = /* @__PURE__ */ new Map();
|
|
370
374
|
toolCalls = /* @__PURE__ */ new Map();
|
|
371
375
|
capture(event, ctx, now = Date.now()) {
|
|
372
376
|
this.cleanup(now);
|
|
373
377
|
const runId = event.runId ?? ctx.runId;
|
|
374
|
-
|
|
378
|
+
const sessionKey = event.sessionKey ?? ctx.sessionKey;
|
|
379
|
+
if (!sessionKey) return void 0;
|
|
375
380
|
if (isTrustedDebriefOrigin(ctx.channelId, event.from)) {
|
|
376
381
|
try {
|
|
377
382
|
const { envelope, bodyHash } = parseDebriefBody(event.content);
|
|
@@ -381,7 +386,7 @@ var EvolutionContextStore = class {
|
|
|
381
386
|
channelId: ctx.channelId,
|
|
382
387
|
accountId: normalizedAccountId(ctx.accountId),
|
|
383
388
|
from: event.from,
|
|
384
|
-
sessionKey
|
|
389
|
+
sessionKey,
|
|
385
390
|
runId,
|
|
386
391
|
conversationId: ctx.conversationId,
|
|
387
392
|
messageId: event.messageId,
|
|
@@ -392,7 +397,7 @@ var EvolutionContextStore = class {
|
|
|
392
397
|
...prior && prior.hash !== bodyHash ? { conflict: true } : {}
|
|
393
398
|
};
|
|
394
399
|
this.hashByDebriefId.set(envelope.debriefId, prior ?? { hash: bodyHash, capturedAt: now });
|
|
395
|
-
this.
|
|
400
|
+
this.storeInbound(captured);
|
|
396
401
|
return captured;
|
|
397
402
|
} catch {
|
|
398
403
|
return void 0;
|
|
@@ -405,7 +410,7 @@ var EvolutionContextStore = class {
|
|
|
405
410
|
channelId: ctx.channelId,
|
|
406
411
|
accountId: normalizedAccountId(ctx.accountId),
|
|
407
412
|
from: event.from,
|
|
408
|
-
sessionKey
|
|
413
|
+
sessionKey,
|
|
409
414
|
runId,
|
|
410
415
|
conversationId: ctx.conversationId,
|
|
411
416
|
messageId: event.messageId,
|
|
@@ -414,21 +419,38 @@ var EvolutionContextStore = class {
|
|
|
414
419
|
decision: match[1] === "\u91C7\u7EB3" ? "accept" : "reject",
|
|
415
420
|
capturedAt: now
|
|
416
421
|
};
|
|
417
|
-
this.
|
|
422
|
+
this.storeInbound(command);
|
|
418
423
|
return command;
|
|
419
424
|
}
|
|
420
|
-
getPending(runId, sessionKey, now = Date.now()) {
|
|
425
|
+
getPending(runId, sessionKey, prompt, now = Date.now()) {
|
|
421
426
|
this.cleanup(now);
|
|
422
427
|
if (!runId || !sessionKey) return void 0;
|
|
423
428
|
const inbound = this.inboundByRun.get(runId);
|
|
424
|
-
|
|
429
|
+
if (inbound) {
|
|
430
|
+
return inbound.sessionKey === sessionKey && now - inbound.capturedAt <= CAPTURE_TTL_MS ? inbound : void 0;
|
|
431
|
+
}
|
|
432
|
+
if (!prompt) return void 0;
|
|
433
|
+
const pending = this.inboundBySession.get(sessionKey);
|
|
434
|
+
if (!pending) return void 0;
|
|
435
|
+
return pending.find((candidate) => prompt.includes(candidate.content));
|
|
436
|
+
}
|
|
437
|
+
storeInbound(inbound) {
|
|
438
|
+
if (inbound.runId) {
|
|
439
|
+
this.inboundByRun.set(inbound.runId, inbound);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
const pending = this.inboundBySession.get(inbound.sessionKey) ?? [];
|
|
443
|
+
pending.push(inbound);
|
|
444
|
+
this.inboundBySession.set(inbound.sessionKey, pending);
|
|
425
445
|
}
|
|
426
|
-
bindAgentRun(event, ctx, binding, now = Date.now(), explicitOwnerAuthorized = false) {
|
|
446
|
+
bindAgentRun(event, ctx, binding, now = Date.now(), explicitOwnerAuthorized = false, pendingInbound) {
|
|
427
447
|
this.cleanup(now);
|
|
428
448
|
if (!ctx.runId || !ctx.sessionKey || !ctx.agentId || !ctx.workspaceDir) return void 0;
|
|
429
|
-
const
|
|
449
|
+
const runInbound = this.inboundByRun.get(ctx.runId);
|
|
450
|
+
const sessionPending = this.inboundBySession.get(ctx.sessionKey);
|
|
451
|
+
const pendingIndex = pendingInbound ? sessionPending?.indexOf(pendingInbound) ?? -1 : -1;
|
|
452
|
+
const inbound = runInbound ?? (pendingIndex >= 0 ? pendingInbound : void 0);
|
|
430
453
|
if (!inbound || inbound.sessionKey !== ctx.sessionKey || now - inbound.capturedAt > CAPTURE_TTL_MS) return void 0;
|
|
431
|
-
this.inboundByRun.delete(ctx.runId);
|
|
432
454
|
const eventAccountId = normalizedAccountId(event.accountId);
|
|
433
455
|
if (event.channelId !== inbound.channelId || eventAccountId !== inbound.accountId) return void 0;
|
|
434
456
|
if (!binding || binding.channelId !== inbound.channelId || binding.accountId !== inbound.accountId) return void 0;
|
|
@@ -437,6 +459,12 @@ var EvolutionContextStore = class {
|
|
|
437
459
|
} else {
|
|
438
460
|
if (!explicitOwnerAuthorized || event.senderIsOwner !== true || !event.senderId || normalizedSender(event.senderId) !== normalizedSender(inbound.from)) return void 0;
|
|
439
461
|
}
|
|
462
|
+
if (runInbound) {
|
|
463
|
+
this.inboundByRun.delete(ctx.runId);
|
|
464
|
+
} else {
|
|
465
|
+
sessionPending.splice(pendingIndex, 1);
|
|
466
|
+
if (sessionPending.length === 0) this.inboundBySession.delete(ctx.sessionKey);
|
|
467
|
+
}
|
|
440
468
|
const bound = {
|
|
441
469
|
runId: ctx.runId,
|
|
442
470
|
sessionKey: ctx.sessionKey,
|
|
@@ -488,6 +516,11 @@ var EvolutionContextStore = class {
|
|
|
488
516
|
for (const [key, value] of this.inboundByRun) {
|
|
489
517
|
if (now - value.capturedAt > CAPTURE_TTL_MS) this.inboundByRun.delete(key);
|
|
490
518
|
}
|
|
519
|
+
for (const [key, values] of this.inboundBySession) {
|
|
520
|
+
const current = values.filter((value) => now - value.capturedAt <= CAPTURE_TTL_MS);
|
|
521
|
+
if (current.length === 0) this.inboundBySession.delete(key);
|
|
522
|
+
else if (current.length !== values.length) this.inboundBySession.set(key, current);
|
|
523
|
+
}
|
|
491
524
|
for (const [key, value] of this.boundByRun) {
|
|
492
525
|
if (value.expiresAt < now) this.boundByRun.delete(key);
|
|
493
526
|
}
|
|
@@ -498,6 +531,13 @@ var EvolutionContextStore = class {
|
|
|
498
531
|
if (now - value.capturedAt > CAPTURE_TTL_MS) this.hashByDebriefId.delete(key);
|
|
499
532
|
}
|
|
500
533
|
while (this.inboundByRun.size > MAX_CAPTURES) this.inboundByRun.delete(this.inboundByRun.keys().next().value);
|
|
534
|
+
while ([...this.inboundBySession.values()].reduce((total, values) => total + values.length, 0) > MAX_CAPTURES) {
|
|
535
|
+
const firstKey = this.inboundBySession.keys().next().value;
|
|
536
|
+
if (!firstKey) break;
|
|
537
|
+
const values = this.inboundBySession.get(firstKey);
|
|
538
|
+
values.shift();
|
|
539
|
+
if (values.length === 0) this.inboundBySession.delete(firstKey);
|
|
540
|
+
}
|
|
501
541
|
while (this.boundByRun.size > MAX_CAPTURES) this.boundByRun.delete(this.boundByRun.keys().next().value);
|
|
502
542
|
while (this.toolCalls.size > MAX_CAPTURES) this.toolCalls.delete(this.toolCalls.keys().next().value);
|
|
503
543
|
while (this.hashByDebriefId.size > MAX_CAPTURES) this.hashByDebriefId.delete(this.hashByDebriefId.keys().next().value);
|
|
@@ -2266,6 +2306,8 @@ function getWerewolfEvolutionVersion() {
|
|
|
2266
2306
|
}
|
|
2267
2307
|
|
|
2268
2308
|
// src/service.ts
|
|
2309
|
+
var DIRECT_MESSAGE_TTL_MS = 10 * 60 * 1e3;
|
|
2310
|
+
var MAX_DIRECT_MESSAGES = 256;
|
|
2269
2311
|
function clone(value) {
|
|
2270
2312
|
return structuredClone(value);
|
|
2271
2313
|
}
|
|
@@ -2369,35 +2411,83 @@ var WerewolfEvolutionService = class {
|
|
|
2369
2411
|
contexts = new EvolutionContextStore();
|
|
2370
2412
|
version;
|
|
2371
2413
|
ordinaryDirectMessages = /* @__PURE__ */ new Map();
|
|
2414
|
+
ordinaryDirectMessagesBySession = /* @__PURE__ */ new Map();
|
|
2372
2415
|
ownerDirectRuns = /* @__PURE__ */ new Map();
|
|
2373
2416
|
observedRuns = /* @__PURE__ */ new Map();
|
|
2374
2417
|
constructor(version = getWerewolfEvolutionVersion()) {
|
|
2375
2418
|
this.version = version;
|
|
2376
2419
|
}
|
|
2420
|
+
cleanupOrdinaryDirectMessages(now) {
|
|
2421
|
+
for (const [runId, inbound] of this.ordinaryDirectMessages) {
|
|
2422
|
+
if (now - inbound.capturedAt > DIRECT_MESSAGE_TTL_MS) this.ordinaryDirectMessages.delete(runId);
|
|
2423
|
+
}
|
|
2424
|
+
for (const [sessionKey, inbounds] of this.ordinaryDirectMessagesBySession) {
|
|
2425
|
+
const current = inbounds.filter((inbound) => now - inbound.capturedAt <= DIRECT_MESSAGE_TTL_MS);
|
|
2426
|
+
if (current.length === 0) this.ordinaryDirectMessagesBySession.delete(sessionKey);
|
|
2427
|
+
else if (current.length !== inbounds.length) this.ordinaryDirectMessagesBySession.set(sessionKey, current);
|
|
2428
|
+
}
|
|
2429
|
+
while (this.ordinaryDirectMessages.size > MAX_DIRECT_MESSAGES) {
|
|
2430
|
+
this.ordinaryDirectMessages.delete(this.ordinaryDirectMessages.keys().next().value);
|
|
2431
|
+
}
|
|
2432
|
+
while ([...this.ordinaryDirectMessagesBySession.values()].reduce((total, inbounds) => total + inbounds.length, 0) > MAX_DIRECT_MESSAGES) {
|
|
2433
|
+
const firstKey = this.ordinaryDirectMessagesBySession.keys().next().value;
|
|
2434
|
+
if (!firstKey) break;
|
|
2435
|
+
const inbounds = this.ordinaryDirectMessagesBySession.get(firstKey);
|
|
2436
|
+
inbounds.shift();
|
|
2437
|
+
if (inbounds.length === 0) this.ordinaryDirectMessagesBySession.delete(firstKey);
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2377
2440
|
captureMessage(event, ctx) {
|
|
2441
|
+
const now = Date.now();
|
|
2442
|
+
this.cleanupOrdinaryDirectMessages(now);
|
|
2378
2443
|
const captured = this.contexts.capture(event, ctx);
|
|
2379
2444
|
const runId = event.runId ?? ctx.runId;
|
|
2380
|
-
|
|
2381
|
-
|
|
2445
|
+
const sessionKey = event.sessionKey ?? ctx.sessionKey;
|
|
2446
|
+
if (!captured && sessionKey && isHumanDirectMessage(event, ctx)) {
|
|
2447
|
+
const direct = {
|
|
2382
2448
|
channelId: ctx.channelId,
|
|
2383
2449
|
accountId: normalizedAccountId(ctx.accountId),
|
|
2384
2450
|
from: event.from,
|
|
2385
|
-
sessionKey
|
|
2386
|
-
|
|
2387
|
-
|
|
2451
|
+
sessionKey,
|
|
2452
|
+
content: event.content,
|
|
2453
|
+
capturedAt: now
|
|
2454
|
+
};
|
|
2455
|
+
if (runId) {
|
|
2456
|
+
this.ordinaryDirectMessages.set(runId, direct);
|
|
2457
|
+
} else {
|
|
2458
|
+
const pending = this.ordinaryDirectMessagesBySession.get(sessionKey) ?? [];
|
|
2459
|
+
pending.push(direct);
|
|
2460
|
+
this.ordinaryDirectMessagesBySession.set(sessionKey, pending);
|
|
2461
|
+
}
|
|
2462
|
+
this.cleanupOrdinaryDirectMessages(now);
|
|
2388
2463
|
}
|
|
2389
2464
|
}
|
|
2390
|
-
|
|
2465
|
+
pendingOrdinaryDirectMessage(runId, sessionKey, prompt, now) {
|
|
2466
|
+
if (!runId || !sessionKey) return void 0;
|
|
2467
|
+
const byRun = this.ordinaryDirectMessages.get(runId);
|
|
2468
|
+
if (byRun) return byRun.sessionKey === sessionKey ? byRun : void 0;
|
|
2469
|
+
return this.ordinaryDirectMessagesBySession.get(sessionKey)?.find((candidate) => now - candidate.capturedAt <= DIRECT_MESSAGE_TTL_MS && prompt.includes(candidate.content));
|
|
2470
|
+
}
|
|
2471
|
+
consumeOrdinaryDirectMessage(runId, direct) {
|
|
2472
|
+
if (this.ordinaryDirectMessages.get(runId) === direct) {
|
|
2473
|
+
this.ordinaryDirectMessages.delete(runId);
|
|
2474
|
+
return;
|
|
2475
|
+
}
|
|
2476
|
+
const pending = this.ordinaryDirectMessagesBySession.get(direct.sessionKey);
|
|
2477
|
+
const index = pending?.indexOf(direct) ?? -1;
|
|
2478
|
+
if (index < 0) return;
|
|
2479
|
+
pending.splice(index, 1);
|
|
2480
|
+
if (pending.length === 0) this.ordinaryDirectMessagesBySession.delete(direct.sessionKey);
|
|
2481
|
+
}
|
|
2482
|
+
bindAgentRun(event, ctx, binding, explicitOwnerAuthorized = false, pendingInbound, pendingOrdinary) {
|
|
2391
2483
|
const now = Date.now();
|
|
2484
|
+
this.cleanupOrdinaryDirectMessages(now);
|
|
2392
2485
|
for (const [runId, observed] of this.observedRuns) {
|
|
2393
2486
|
if (observed.expiresAt < now) this.observedRuns.delete(runId);
|
|
2394
2487
|
}
|
|
2395
2488
|
for (const [runId, direct] of this.ownerDirectRuns) {
|
|
2396
2489
|
if (direct.expiresAt < now) this.ownerDirectRuns.delete(runId);
|
|
2397
2490
|
}
|
|
2398
|
-
for (const [runId, inbound] of this.ordinaryDirectMessages) {
|
|
2399
|
-
if (now - inbound.capturedAt > 10 * 60 * 1e3) this.ordinaryDirectMessages.delete(runId);
|
|
2400
|
-
}
|
|
2401
2491
|
if (ctx.runId && ctx.sessionKey && ctx.agentId && ctx.workspaceDir) {
|
|
2402
2492
|
this.observedRuns.set(ctx.runId, {
|
|
2403
2493
|
sessionKey: ctx.sessionKey,
|
|
@@ -2406,22 +2496,29 @@ var WerewolfEvolutionService = class {
|
|
|
2406
2496
|
expiresAt: now + 10 * 60 * 1e3
|
|
2407
2497
|
});
|
|
2408
2498
|
}
|
|
2409
|
-
const bound = this.contexts.bindAgentRun(
|
|
2410
|
-
|
|
2411
|
-
|
|
2499
|
+
const bound = this.contexts.bindAgentRun(
|
|
2500
|
+
event,
|
|
2501
|
+
ctx,
|
|
2502
|
+
binding,
|
|
2503
|
+
Date.now(),
|
|
2504
|
+
explicitOwnerAuthorized,
|
|
2505
|
+
pendingInbound
|
|
2506
|
+
);
|
|
2507
|
+
const ordinary = (ctx.runId ? this.ordinaryDirectMessages.get(ctx.runId) : void 0) ?? pendingOrdinary;
|
|
2412
2508
|
if (!bound && ordinary && ctx.runId && ctx.sessionKey && ctx.agentId && ctx.workspaceDir && ordinary.sessionKey === ctx.sessionKey && ordinary.channelId === event.channelId && ordinary.accountId === normalizedAccountId(event.accountId) && explicitOwnerAuthorized && event.senderIsOwner === true && event.senderId && sameHumanSender(event.senderId, ordinary.from)) {
|
|
2413
2509
|
this.ownerDirectRuns.set(ctx.runId, {
|
|
2414
2510
|
sessionKey: ctx.sessionKey,
|
|
2415
2511
|
agentId: ctx.agentId,
|
|
2416
2512
|
workspaceDir: ctx.workspaceDir,
|
|
2417
2513
|
senderId: event.senderId,
|
|
2418
|
-
expiresAt: now +
|
|
2514
|
+
expiresAt: now + DIRECT_MESSAGE_TTL_MS
|
|
2419
2515
|
});
|
|
2516
|
+
this.consumeOrdinaryDirectMessage(ctx.runId, ordinary);
|
|
2420
2517
|
}
|
|
2421
2518
|
}
|
|
2422
|
-
bindPromptRun(ctx, config) {
|
|
2423
|
-
const pending = this.contexts.getPending(ctx.runId, ctx.sessionKey);
|
|
2424
|
-
const ordinary = ctx.runId
|
|
2519
|
+
bindPromptRun(event, ctx, config) {
|
|
2520
|
+
const pending = this.contexts.getPending(ctx.runId, ctx.sessionKey, event.prompt);
|
|
2521
|
+
const ordinary = this.pendingOrdinaryDirectMessage(ctx.runId, ctx.sessionKey, event.prompt, Date.now());
|
|
2425
2522
|
const inbound = pending ?? ordinary;
|
|
2426
2523
|
const channelId = inbound?.channelId;
|
|
2427
2524
|
const accountId = inbound?.accountId;
|
|
@@ -2448,7 +2545,9 @@ var WerewolfEvolutionService = class {
|
|
|
2448
2545
|
},
|
|
2449
2546
|
ctx,
|
|
2450
2547
|
resolveAgentAccountBinding(config, channelId, accountId),
|
|
2451
|
-
workspaceAuthorized
|
|
2548
|
+
workspaceAuthorized,
|
|
2549
|
+
pending,
|
|
2550
|
+
ordinary
|
|
2452
2551
|
);
|
|
2453
2552
|
}
|
|
2454
2553
|
buildPromptContext(ctx) {
|
|
@@ -2463,7 +2562,7 @@ var WerewolfEvolutionService = class {
|
|
|
2463
2562
|
const reminder = claimPendingSuggestionReminder(paths, this.version);
|
|
2464
2563
|
if (!reminder) return void 0;
|
|
2465
2564
|
return {
|
|
2466
|
-
|
|
2565
|
+
prependContext: [
|
|
2467
2566
|
"[werewolf-evolution pending suggestion reminder]",
|
|
2468
2567
|
"This is a trusted ordinary direct message from the Agent owner.",
|
|
2469
2568
|
`Remind the owner once about ${reminder.id}; do not call werewolf_suggestion_decide unless the original message is an exact decision command.`,
|
|
@@ -2489,7 +2588,7 @@ var WerewolfEvolutionService = class {
|
|
|
2489
2588
|
return void 0;
|
|
2490
2589
|
}
|
|
2491
2590
|
return {
|
|
2492
|
-
|
|
2591
|
+
prependContext: `[werewolf-evolution] Trusted owner direct-message command captured for ${bound.inbound.suggestionId}. Call werewolf_suggestion_decide exactly once with decision=${bound.inbound.decision}. Do not mention or process this command in a game room or group.`
|
|
2493
2592
|
};
|
|
2494
2593
|
}
|
|
2495
2594
|
const captured = bound.inbound;
|
|
@@ -2521,7 +2620,7 @@ var WerewolfEvolutionService = class {
|
|
|
2521
2620
|
summary: fact.summary
|
|
2522
2621
|
}));
|
|
2523
2622
|
return {
|
|
2524
|
-
|
|
2623
|
+
prependContext: [
|
|
2525
2624
|
"[werewolf-evolution trusted context]",
|
|
2526
2625
|
`debriefId=${captured.envelope.debriefId}`,
|
|
2527
2626
|
`eligibility=${JSON.stringify(captured.envelope.game)}`,
|
|
@@ -2760,8 +2859,8 @@ function registerWerewolfEvolution(api, service = new WerewolfEvolutionService()
|
|
|
2760
2859
|
api.on("message_received", (event, ctx) => {
|
|
2761
2860
|
service.captureMessage(event, ctx);
|
|
2762
2861
|
});
|
|
2763
|
-
api.on("before_prompt_build", (
|
|
2764
|
-
service.bindPromptRun(ctx, api.config);
|
|
2862
|
+
api.on("before_prompt_build", (event, ctx) => {
|
|
2863
|
+
service.bindPromptRun(event, ctx, api.config);
|
|
2765
2864
|
return service.buildPromptContext(ctx);
|
|
2766
2865
|
});
|
|
2767
2866
|
api.on("before_tool_call", (event, ctx) => service.beforeToolCall(event, ctx));
|