@primusclaw/personal-agent-teams 0.10.3 → 0.10.4

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 CHANGED
@@ -813,14 +813,29 @@ var TeamsStream = class {
813
813
  }
814
814
  finalOnce(activity) {
815
815
  return this.lock.run(async () => {
816
- await this.send(streamFinal(activity, this.parts.replyTo, this.streamId), true);
817
- this.closed = true;
818
- if (this.parts.conversation && this.openedAt !== void 0) {
819
- this.parts.busy.closed(this.parts.conversation, this.openedAt);
816
+ try {
817
+ await this.send(streamFinal(activity, this.parts.replyTo, this.streamId), true);
818
+ } catch (error) {
819
+ if (refusedForGood(error)) {
820
+ this.logger.info("Teams refused the stream's final for good; the conversation can stream again");
821
+ this.close();
822
+ }
823
+ throw error;
820
824
  }
825
+ this.close();
821
826
  });
822
827
  }
828
+ /** The stream is done with: it takes nothing more, and the conversation is free for the next one. */
829
+ close() {
830
+ this.closed = true;
831
+ if (this.parts.conversation && this.openedAt !== void 0) {
832
+ this.parts.busy.closed(this.parts.conversation, this.openedAt);
833
+ }
834
+ }
823
835
  };
836
+ function refusedForGood(error) {
837
+ return error instanceof StreamRefusedError && error.status === 403 && error.code === "ContentStreamNotAllowed";
838
+ }
824
839
 
825
840
  // packages/teams/src/stream/live-stream.ts
826
841
  function refused(reason, status = 0, code = "") {
@@ -3281,8 +3296,8 @@ var MODE_WHAT = {
3281
3296
  "Approved and merged when nothing needs fixing and every check passes; a change the reviewing model judges high risk (security, credentials, CI/release, permissions, data migrations, ...) or one the repository asks to confirm waits for your own click on a card."
3282
3297
  ],
3283
3298
  fully_auto: [
3284
- "\u5BA1\u5B8C\u6CA1\u6709\u95EE\u9898\u3001\u68C0\u67E5\u5168\u90E8\u901A\u8FC7\u65F6\u76F4\u63A5\u6279\u51C6\u5E76\u5408\u5165\uFF0C\u4E0D\u518D\u8BF7\u4F60\u786E\u8BA4\uFF1B\u53EA\u6709\u4ED3\u5E93\u81EA\u5DF1\u8981\u6C42\u786E\u8BA4\u7684\u53D8\u66F4\uFF08.github/claw-review.yml \u7684 confirm_merge: always \u7B49\uFF09\u4ECD\u4F1A\u5148\u95EE\u4F60\u3002",
3285
- "Approved and merged when nothing needs fixing and every check passes, without asking you; only a change the repository itself requires a person for (confirm_merge: always in its review config, and the like) still waits for your click."
3299
+ "\u5BA1\u5B8C\u6CA1\u6709\u95EE\u9898\u3001\u68C0\u67E5\u5168\u90E8\u901A\u8FC7\u65F6\u76F4\u63A5\u6279\u51C6\u5E76\u5408\u5165\uFF0C\u4E0D\u518D\u8BF7\u4F60\u786E\u8BA4\uFF0C\u4F60\u5728\u804A\u5929\u91CC\u8BA9\u6211\u5BA1\u5E76\u5408\u5165\u7684 PR \u4E5F\u4E00\u6837\uFF1B\u4ED3\u5E93\u81EA\u8EAB\u8981\u6C42\u7684\u68C0\u67E5\u4ECD\u7136\u751F\u6548\uFF1ACI \u6CA1\u8FC7\u6216\u8FD8\u6709\u6CA1\u89E3\u51B3\u7684\u8BC4\u5BA1\u8BA8\u8BBA\u4E0D\u4F1A\u5408\u5165\uFF0C\u4ED3\u5E93\u81EA\u5DF1\u8981\u6C42\u786E\u8BA4\u7684\u53D8\u66F4\uFF08.github/claw-review.yml \u7684 confirm_merge: always \u7B49\uFF09\u4ECD\u4F1A\u5148\u95EE\u4F60\u3002",
3300
+ "Approved and merged when nothing needs fixing and every check passes, without asking you, a pull request you ask me in the chat to review and merge included; the repository's own requirements still apply: nothing merges with CI failing or review threads open, and a change the repository itself requires a person for (confirm_merge: always in its review config, and the like) still waits for your click."
3286
3301
  ]
3287
3302
  };
3288
3303
  var MODE_BUTTON = { high_risk_confirm: "\u9AD8\u98CE\u9669\u518D\u95EE\u6211", fully_auto: "\u5168\u81EA\u52A8\u5408\u5165" };
@@ -6185,6 +6200,49 @@ function queueLagMs(message2, nowEpochMs, handleMs = 0) {
6185
6200
  if (!message2.enqueuedAt) return void 0;
6186
6201
  return Math.max(Math.trunc(nowEpochMs - message2.enqueuedAt.getTime()) - handleMs, 0);
6187
6202
  }
6203
+ var MAX_CLIENT_LAG_MS = 24 * 60 * 6e4;
6204
+ var ISO_TIME = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
6205
+ var CLIENT_ID = /^[A-Za-z0-9._:-]{1,64}$/;
6206
+ var CLIENT_ID_KEYS = ["clientActivityID", "clientActivityId", "clientmessageid", "clientMessageId"];
6207
+ var EPOCH_MS_ID = /^\d{13}$/;
6208
+ function isoEpochMs(value) {
6209
+ if (typeof value !== "string") return void 0;
6210
+ const match = ISO_TIME.exec(value);
6211
+ if (!match) return void 0;
6212
+ const [, base2, fraction = "", zone] = match;
6213
+ const ms = Date.parse(`${base2}.${fraction.padEnd(3, "0").slice(0, 3)}${zone}`);
6214
+ return Number.isFinite(ms) ? ms : void 0;
6215
+ }
6216
+ function clientTiming(activity) {
6217
+ const timing = {};
6218
+ const local = activity["localTimestamp"];
6219
+ const clientMs = isoEpochMs(local);
6220
+ if (clientMs !== void 0) timing.client_ts = local;
6221
+ const channelData = activity["channelData"];
6222
+ if (isObject(channelData)) {
6223
+ for (const key of CLIENT_ID_KEYS) {
6224
+ const id2 = channelData[key];
6225
+ if (typeof id2 === "string" && CLIENT_ID.test(id2)) {
6226
+ timing.client_msg_id = id2;
6227
+ break;
6228
+ }
6229
+ }
6230
+ }
6231
+ const stamp = activity["timestamp"];
6232
+ const id = activity["id"];
6233
+ let serverMs = isoEpochMs(stamp);
6234
+ if (serverMs !== void 0) timing.server_ts = stamp;
6235
+ else if (typeof id === "string" && EPOCH_MS_ID.test(id)) {
6236
+ serverMs = Number(id);
6237
+ timing.server_ts = new Date(serverMs).toISOString();
6238
+ }
6239
+ if (clientMs !== void 0 && serverMs !== void 0) {
6240
+ const lag = serverMs - clientMs;
6241
+ timing.client_lag_raw_ms = lag;
6242
+ if (lag >= 0 && lag <= MAX_CLIENT_LAG_MS) timing.client_lag_ms = lag;
6243
+ }
6244
+ return timing;
6245
+ }
6188
6246
  function decode(body) {
6189
6247
  const parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
6190
6248
  if (!isObject(parsed)) throw new SyntaxError("body is not a JSON object");
@@ -6250,32 +6308,34 @@ var InboundConsumer = class {
6250
6308
  await receiver.deadLetter(message2, "malformed", Array.from(why).slice(0, 200).join(""));
6251
6309
  return;
6252
6310
  }
6311
+ const timing = clientTiming(body);
6253
6312
  const started = this.parts.clock.monotonicMs();
6254
6313
  try {
6255
6314
  await this.parts.handle(body);
6256
6315
  } catch (error) {
6257
6316
  this.logger.error("handler failed; abandoning for redelivery", { error: errorName5(error) });
6258
- this.record(message2, started, false);
6317
+ this.record(message2, timing, started, false);
6259
6318
  await receiver.abandon(message2);
6260
6319
  return;
6261
6320
  }
6262
- this.record(message2, started, true);
6321
+ this.record(message2, timing, started, true);
6263
6322
  await receiver.complete(message2);
6264
6323
  }
6265
- record(message2, started, ok5) {
6324
+ record(message2, timing, started, ok5) {
6266
6325
  const handleMs = Math.trunc(this.parts.clock.monotonicMs() - started);
6267
6326
  const lag = queueLagMs(message2, this.parts.clock.epochMs(), handleMs);
6268
- this.logLine(message2, ok5 ? "completed" : "abandoned", lag, handleMs);
6327
+ this.logLine(message2, ok5 ? "completed" : "abandoned", lag, handleMs, timing);
6269
6328
  this.parts.stats?.processed(lag, handleMs, ok5);
6270
6329
  }
6271
6330
  /** The one line for a message taken: ids, outcome and times only. */
6272
- logLine(message2, outcome, lagMs, handleMs) {
6331
+ logLine(message2, outcome, lagMs, handleMs, timing = {}) {
6273
6332
  this.logger.info("inbound message", {
6274
6333
  id: message2.messageId,
6275
6334
  delivery: message2.deliveryCount,
6276
6335
  outcome,
6277
6336
  lag_ms: lagMs,
6278
- ms: handleMs
6337
+ ms: handleMs,
6338
+ ...timing
6279
6339
  });
6280
6340
  }
6281
6341
  };
package/dist/main.js CHANGED
@@ -2547,6 +2547,49 @@ function queueLagMs(message2, nowEpochMs, handleMs = 0) {
2547
2547
  if (!message2.enqueuedAt) return void 0;
2548
2548
  return Math.max(Math.trunc(nowEpochMs - message2.enqueuedAt.getTime()) - handleMs, 0);
2549
2549
  }
2550
+ var MAX_CLIENT_LAG_MS = 24 * 60 * 6e4;
2551
+ var ISO_TIME = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
2552
+ var CLIENT_ID = /^[A-Za-z0-9._:-]{1,64}$/;
2553
+ var CLIENT_ID_KEYS = ["clientActivityID", "clientActivityId", "clientmessageid", "clientMessageId"];
2554
+ var EPOCH_MS_ID = /^\d{13}$/;
2555
+ function isoEpochMs(value) {
2556
+ if (typeof value !== "string") return void 0;
2557
+ const match = ISO_TIME.exec(value);
2558
+ if (!match) return void 0;
2559
+ const [, base2, fraction = "", zone] = match;
2560
+ const ms = Date.parse(`${base2}.${fraction.padEnd(3, "0").slice(0, 3)}${zone}`);
2561
+ return Number.isFinite(ms) ? ms : void 0;
2562
+ }
2563
+ function clientTiming(activity) {
2564
+ const timing = {};
2565
+ const local = activity["localTimestamp"];
2566
+ const clientMs = isoEpochMs(local);
2567
+ if (clientMs !== void 0) timing.client_ts = local;
2568
+ const channelData = activity["channelData"];
2569
+ if (isObject(channelData)) {
2570
+ for (const key of CLIENT_ID_KEYS) {
2571
+ const id2 = channelData[key];
2572
+ if (typeof id2 === "string" && CLIENT_ID.test(id2)) {
2573
+ timing.client_msg_id = id2;
2574
+ break;
2575
+ }
2576
+ }
2577
+ }
2578
+ const stamp = activity["timestamp"];
2579
+ const id = activity["id"];
2580
+ let serverMs = isoEpochMs(stamp);
2581
+ if (serverMs !== void 0) timing.server_ts = stamp;
2582
+ else if (typeof id === "string" && EPOCH_MS_ID.test(id)) {
2583
+ serverMs = Number(id);
2584
+ timing.server_ts = new Date(serverMs).toISOString();
2585
+ }
2586
+ if (clientMs !== void 0 && serverMs !== void 0) {
2587
+ const lag = serverMs - clientMs;
2588
+ timing.client_lag_raw_ms = lag;
2589
+ if (lag >= 0 && lag <= MAX_CLIENT_LAG_MS) timing.client_lag_ms = lag;
2590
+ }
2591
+ return timing;
2592
+ }
2550
2593
  function decode(body) {
2551
2594
  const parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
2552
2595
  if (!isObject(parsed)) throw new SyntaxError("body is not a JSON object");
@@ -2612,32 +2655,34 @@ var InboundConsumer = class {
2612
2655
  await receiver.deadLetter(message2, "malformed", Array.from(why).slice(0, 200).join(""));
2613
2656
  return;
2614
2657
  }
2658
+ const timing = clientTiming(body);
2615
2659
  const started = this.parts.clock.monotonicMs();
2616
2660
  try {
2617
2661
  await this.parts.handle(body);
2618
2662
  } catch (error) {
2619
2663
  this.logger.error("handler failed; abandoning for redelivery", { error: errorName3(error) });
2620
- this.record(message2, started, false);
2664
+ this.record(message2, timing, started, false);
2621
2665
  await receiver.abandon(message2);
2622
2666
  return;
2623
2667
  }
2624
- this.record(message2, started, true);
2668
+ this.record(message2, timing, started, true);
2625
2669
  await receiver.complete(message2);
2626
2670
  }
2627
- record(message2, started, ok5) {
2671
+ record(message2, timing, started, ok5) {
2628
2672
  const handleMs = Math.trunc(this.parts.clock.monotonicMs() - started);
2629
2673
  const lag = queueLagMs(message2, this.parts.clock.epochMs(), handleMs);
2630
- this.logLine(message2, ok5 ? "completed" : "abandoned", lag, handleMs);
2674
+ this.logLine(message2, ok5 ? "completed" : "abandoned", lag, handleMs, timing);
2631
2675
  this.parts.stats?.processed(lag, handleMs, ok5);
2632
2676
  }
2633
2677
  /** The one line for a message taken: ids, outcome and times only. */
2634
- logLine(message2, outcome, lagMs, handleMs) {
2678
+ logLine(message2, outcome, lagMs, handleMs, timing = {}) {
2635
2679
  this.logger.info("inbound message", {
2636
2680
  id: message2.messageId,
2637
2681
  delivery: message2.deliveryCount,
2638
2682
  outcome,
2639
2683
  lag_ms: lagMs,
2640
- ms: handleMs
2684
+ ms: handleMs,
2685
+ ...timing
2641
2686
  });
2642
2687
  }
2643
2688
  };
@@ -3084,14 +3129,29 @@ var TeamsStream = class {
3084
3129
  }
3085
3130
  finalOnce(activity) {
3086
3131
  return this.lock.run(async () => {
3087
- await this.send(streamFinal(activity, this.parts.replyTo, this.streamId), true);
3088
- this.closed = true;
3089
- if (this.parts.conversation && this.openedAt !== void 0) {
3090
- this.parts.busy.closed(this.parts.conversation, this.openedAt);
3132
+ try {
3133
+ await this.send(streamFinal(activity, this.parts.replyTo, this.streamId), true);
3134
+ } catch (error) {
3135
+ if (refusedForGood(error)) {
3136
+ this.logger.info("Teams refused the stream's final for good; the conversation can stream again");
3137
+ this.close();
3138
+ }
3139
+ throw error;
3091
3140
  }
3141
+ this.close();
3092
3142
  });
3093
3143
  }
3144
+ /** The stream is done with: it takes nothing more, and the conversation is free for the next one. */
3145
+ close() {
3146
+ this.closed = true;
3147
+ if (this.parts.conversation && this.openedAt !== void 0) {
3148
+ this.parts.busy.closed(this.parts.conversation, this.openedAt);
3149
+ }
3150
+ }
3094
3151
  };
3152
+ function refusedForGood(error) {
3153
+ return error instanceof StreamRefusedError && error.status === 403 && error.code === "ContentStreamNotAllowed";
3154
+ }
3095
3155
 
3096
3156
  // packages/teams/src/stream/live-stream.ts
3097
3157
  function refused(reason, status = 0, code = "") {
@@ -5792,8 +5852,8 @@ var MODE_WHAT = {
5792
5852
  "Approved and merged when nothing needs fixing and every check passes; a change the reviewing model judges high risk (security, credentials, CI/release, permissions, data migrations, ...) or one the repository asks to confirm waits for your own click on a card."
5793
5853
  ],
5794
5854
  fully_auto: [
5795
- "\u5BA1\u5B8C\u6CA1\u6709\u95EE\u9898\u3001\u68C0\u67E5\u5168\u90E8\u901A\u8FC7\u65F6\u76F4\u63A5\u6279\u51C6\u5E76\u5408\u5165\uFF0C\u4E0D\u518D\u8BF7\u4F60\u786E\u8BA4\uFF1B\u53EA\u6709\u4ED3\u5E93\u81EA\u5DF1\u8981\u6C42\u786E\u8BA4\u7684\u53D8\u66F4\uFF08.github/claw-review.yml \u7684 confirm_merge: always \u7B49\uFF09\u4ECD\u4F1A\u5148\u95EE\u4F60\u3002",
5796
- "Approved and merged when nothing needs fixing and every check passes, without asking you; only a change the repository itself requires a person for (confirm_merge: always in its review config, and the like) still waits for your click."
5855
+ "\u5BA1\u5B8C\u6CA1\u6709\u95EE\u9898\u3001\u68C0\u67E5\u5168\u90E8\u901A\u8FC7\u65F6\u76F4\u63A5\u6279\u51C6\u5E76\u5408\u5165\uFF0C\u4E0D\u518D\u8BF7\u4F60\u786E\u8BA4\uFF0C\u4F60\u5728\u804A\u5929\u91CC\u8BA9\u6211\u5BA1\u5E76\u5408\u5165\u7684 PR \u4E5F\u4E00\u6837\uFF1B\u4ED3\u5E93\u81EA\u8EAB\u8981\u6C42\u7684\u68C0\u67E5\u4ECD\u7136\u751F\u6548\uFF1ACI \u6CA1\u8FC7\u6216\u8FD8\u6709\u6CA1\u89E3\u51B3\u7684\u8BC4\u5BA1\u8BA8\u8BBA\u4E0D\u4F1A\u5408\u5165\uFF0C\u4ED3\u5E93\u81EA\u5DF1\u8981\u6C42\u786E\u8BA4\u7684\u53D8\u66F4\uFF08.github/claw-review.yml \u7684 confirm_merge: always \u7B49\uFF09\u4ECD\u4F1A\u5148\u95EE\u4F60\u3002",
5856
+ "Approved and merged when nothing needs fixing and every check passes, without asking you, a pull request you ask me in the chat to review and merge included; the repository's own requirements still apply: nothing merges with CI failing or review threads open, and a change the repository itself requires a person for (confirm_merge: always in its review config, and the like) still waits for your click."
5797
5857
  ]
5798
5858
  };
5799
5859
  var MODE_BUTTON = { high_risk_confirm: "\u9AD8\u98CE\u9669\u518D\u95EE\u6211", fully_auto: "\u5168\u81EA\u52A8\u5408\u5165" };
@@ -4,7 +4,8 @@
4
4
  * succeed); a handler failure abandons the message, so the broker redelivers it and, after its maximum delivery count,
5
5
  * dead-letters it. Either way the session moves on: one poison message never blocks a conversation. Every message taken
6
6
  * gets one line (`inbound message`): its id, what came of it (completed, abandoned, dead_lettered), how long it waited in
7
- * the queue and how long it took; never its body.
7
+ * the queue and how long it took; never its body. For an activity that decodes, the line also says when the client sent it
8
+ * (`clientTiming`), so a message that reached Teams late can be told apart from one the person sent late.
8
9
  */
9
10
  import type { Clock, Logger } from "@primusclaw/personal-agent";
10
11
  import { type JsonObject } from "../cards/json.js";
@@ -26,6 +27,28 @@ export declare function isIdleTimeout(error: unknown): boolean;
26
27
  * handling (now, less `handleMs`). undefined when the broker gave no enqueue time. A growing lag is the queue backing up.
27
28
  */
28
29
  export declare function queueLagMs(message: Received, nowEpochMs: number, handleMs?: number): number | undefined;
30
+ /** A client lag beyond this is a wrong clock, not a held message: it is logged raw and not as `client_lag_ms`. */
31
+ export declare const MAX_CLIENT_LAG_MS: number;
32
+ /** When the client sent an activity and when the service took it, as the activity says; every field may be absent. */
33
+ export interface ClientTiming {
34
+ /** The activity's `localTimestamp`: the client's own clock when it sent the message, with its zone offset. */
35
+ readonly client_ts?: string;
36
+ /** The client's id of the message from `channelData`, when the client gave one. */
37
+ readonly client_msg_id?: string;
38
+ /** When the service took the message: the activity's `timestamp`, else the epoch milliseconds of a 1:1 message id. */
39
+ readonly server_ts?: string;
40
+ /** `server_ts` less `client_ts`, signed, as measured: negative when the client's clock runs ahead. */
41
+ readonly client_lag_raw_ms?: number;
42
+ /** The same lag when it can be one (0 to MAX_CLIENT_LAG_MS); absent when the clocks disagree beyond that. */
43
+ readonly client_lag_ms?: number;
44
+ }
45
+ /**
46
+ * When `activity` was sent by the client and taken by the service, for the `inbound message` line. Teams sets
47
+ * `timestamp` (the service's clock) and `localTimestamp` (the client's clock) on every message; the difference is how long
48
+ * the client held the message, plus the skew between the two clocks. A difference below zero or above
49
+ * MAX_CLIENT_LAG_MS is skew, so it is kept only as `client_lag_raw_ms`. Never reads the text or who sent it.
50
+ */
51
+ export declare function clientTiming(activity: JsonObject): ClientTiming;
29
52
  export interface ConsumerParts {
30
53
  readonly client: BusClient;
31
54
  readonly queue: string;
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Teams allows one open stream per conversation. This remembers, per conversation, until when this process does not open
3
- * another one there: one is open, or one whose final was refused may still be open on Teams' side until it times out.
3
+ * another one there: one is open, or one whose final did not get through may still be open on Teams' side until it times
4
+ * out. A final Teams refuses because the stream is already over (403 ContentStreamNotAllowed) frees the conversation at once.
4
5
  */
5
6
  import type { Clock } from "@primusclaw/personal-agent";
6
7
  /** Teams closes a stream 10 minutes after its first request. */
@@ -60,4 +60,6 @@ export declare class TeamsStream {
60
60
  */
61
61
  final(activity: JsonObject): Promise<void>;
62
62
  private finalOnce;
63
+ /** The stream is done with: it takes nothing more, and the conversation is free for the next one. */
64
+ private close;
63
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primusclaw/personal-agent-teams",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "description": "The personal agent on Microsoft Teams: activities, Adaptive Cards, streams, Bot Connector, inbound queue, and the worker's Teams surface.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,8 +35,8 @@
35
35
  "dependencies": {
36
36
  "@azure/core-auth": "^1.11.0",
37
37
  "@azure/service-bus": "^7.10.0",
38
- "@primusclaw/personal-agent": "0.10.3",
39
- "@primusclaw/personal-agent-runtime": "0.10.3",
38
+ "@primusclaw/personal-agent": "0.10.4",
39
+ "@primusclaw/personal-agent-runtime": "0.10.4",
40
40
  "zod": "^4.6.7"
41
41
  },
42
42
  "devDependencies": {