@primusclaw/personal-agent-teams 0.3.0 → 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.
@@ -0,0 +1,97 @@
1
+ // packages/teams/src/connector/hosts.ts
2
+ import { pyStrip, silentLogger } from "@primusclaw/personal-agent";
3
+
4
+ // packages/teams/src/connector/errors.ts
5
+ var UnaddressableError = class extends Error {
6
+ name = "UnaddressableError";
7
+ };
8
+
9
+ // packages/teams/src/connector/hosts.ts
10
+ var DEFAULT_HOSTS = ["smba.trafficmanager.net"];
11
+ var DEFAULT_SUFFIXES = [
12
+ ".botframework.com",
13
+ ".botframework.us",
14
+ ".teams.microsoft.com",
15
+ ".teams.microsoft.us"
16
+ ];
17
+ var LABEL = "[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?";
18
+ var NAME = new RegExp(`^${LABEL}(?:\\.${LABEL})+$`);
19
+ function ambiguous(text) {
20
+ for (const ch of text) {
21
+ const code = ch.codePointAt(0) ?? 0;
22
+ if (code <= 32 || code === 127 || code === 92) return true;
23
+ }
24
+ return false;
25
+ }
26
+ function parse(serviceUrl) {
27
+ if (typeof serviceUrl !== "string" || ambiguous(serviceUrl)) return void 0;
28
+ try {
29
+ return new URL(serviceUrl);
30
+ } catch {
31
+ return void 0;
32
+ }
33
+ }
34
+ var ConnectorHosts = class _ConnectorHosts {
35
+ /** `hosts`: exact host names. `suffixes`: ".example.com" allows any name under example.com, not example.com itself. */
36
+ constructor(hosts = DEFAULT_HOSTS, suffixes = DEFAULT_SUFFIXES) {
37
+ this.hosts = hosts;
38
+ this.suffixes = suffixes;
39
+ }
40
+ hosts;
41
+ suffixes;
42
+ /** The lower-cased host of `serviceUrl` when it is an https Bot Connector address on an allowed host; throws otherwise. */
43
+ host(serviceUrl) {
44
+ if (typeof serviceUrl !== "string") throw new UnaddressableError("the serviceUrl is not a string");
45
+ const url = parse(serviceUrl);
46
+ const host = url?.hostname.toLowerCase() ?? "";
47
+ const allowed = url !== void 0 && url.protocol === "https:" && url.username === "" && url.password === "" && (url.port === "" || url.port === "443") && (this.hosts.includes(host) || this.suffixes.some((suffix) => host.endsWith(suffix)));
48
+ if (!allowed) throw new UnaddressableError(`the serviceUrl host ${host || "?"} is not a Bot Connector host`);
49
+ return host;
50
+ }
51
+ check(serviceUrl) {
52
+ this.host(serviceUrl);
53
+ }
54
+ /**
55
+ * The allow-list from two ";"- or ","-separated settings. An unset setting (undefined) keeps its default; a set one
56
+ * replaces it, so an empty value allows nothing of that kind. Suffixes are written `.example.com` or `*.example.com`.
57
+ * An entry that is not a host name of at least two labels is ignored with an error in the log, so a typo narrows the
58
+ * list instead of widening it.
59
+ */
60
+ static fromSettings(hosts, suffixes, logger = silentLogger) {
61
+ return new _ConnectorHosts(
62
+ hosts === void 0 ? DEFAULT_HOSTS : entries(hosts, false, logger),
63
+ suffixes === void 0 ? DEFAULT_SUFFIXES : entries(suffixes, true, logger)
64
+ );
65
+ }
66
+ };
67
+ function entries(value, suffix, logger) {
68
+ const out = [];
69
+ for (const raw of value.split(/[;,]/)) {
70
+ const entry = asciiLower(pyStrip(raw));
71
+ if (!entry) continue;
72
+ const name = suffix ? removePrefix(removePrefix(entry, "*."), ".") : entry;
73
+ if (!NAME.test(name)) {
74
+ logger.error("ignoring Bot Connector entry: not a host name of at least two labels", {
75
+ kind: suffix ? "suffix" : "host",
76
+ entry
77
+ });
78
+ continue;
79
+ }
80
+ out.push(suffix ? `.${name}` : name);
81
+ }
82
+ return out;
83
+ }
84
+ function asciiLower(text) {
85
+ return text.replace(/[A-Z]/g, (ch) => ch.toLowerCase());
86
+ }
87
+ function removePrefix(text, prefix) {
88
+ return text.startsWith(prefix) ? text.slice(prefix.length) : text;
89
+ }
90
+ var DEFAULT_CONNECTOR_HOSTS = new ConnectorHosts();
91
+ export {
92
+ ConnectorHosts,
93
+ DEFAULT_CONNECTOR_HOSTS,
94
+ DEFAULT_HOSTS,
95
+ DEFAULT_SUFFIXES,
96
+ UnaddressableError
97
+ };
package/dist/index.js CHANGED
@@ -1804,7 +1804,8 @@ var TeamsConversation = class {
1804
1804
  return envelope("send", reference(this.ref), this.prepare(activityOf(message2, this.parts.render)));
1805
1805
  }
1806
1806
  async deliver(stored) {
1807
- await this.publish(asEnvelope(stored));
1807
+ const delivered = await this.publish(asEnvelope(stored));
1808
+ return delivered?.activityId || void 0;
1808
1809
  }
1809
1810
  async send(message2) {
1810
1811
  await this.deliver(this.outgoing(message2));
@@ -2282,6 +2283,7 @@ function questionCard(model) {
2282
2283
  header(t.questionTitle, { tone: "accent", subtitle: model.note, emoji: "question" }),
2283
2284
  ...model.questions.flatMap((q, i) => questionInputs(q, i, t))
2284
2285
  ];
2286
+ if (model.typedReply === true) body.push(note(model.group ? t.questionReplyGroup : t.questionReply));
2285
2287
  if (model.group) body.push(note(t.questionOnly({ who: model.ownerName || t.questionAsker })));
2286
2288
  return message(body, [answerButton(model.sessionId, model.toolUseId, model.questions)]);
2287
2289
  }
@@ -2302,12 +2304,21 @@ function approvalExpired(model) {
2302
2304
  return message([header(t.expired, { tone: "attention", emoji: "expired" }), text(line2, { subtle: true })]);
2303
2305
  }
2304
2306
  function questionAnswered(model) {
2307
+ if (model.replied !== void 0) return questionReplied(model, model.replied);
2305
2308
  const said = proactiveText.in("en").answered({ at: clockText(model.when, model.zone) });
2306
2309
  return message([
2307
2310
  header(said, { tone: "good", emoji: "good" }),
2308
2311
  ...model.answers.map(([, answer]) => text(clip(answer, ANSWER_SHOWN_MAX)))
2309
2312
  ]);
2310
2313
  }
2314
+ function questionReplied(model, lang) {
2315
+ const t = approvalText.in(lang);
2316
+ const answer = [...new Set(model.answers.map(([, a]) => a))].join(" / ");
2317
+ return message([
2318
+ header(t.questionReplied({ answer: clip(answer, TITLE_MAX) }), { tone: "good", emoji: "good" }),
2319
+ note(t.questionRepliedAt({ at: clockText(model.when, model.zone) }))
2320
+ ]);
2321
+ }
2311
2322
  function approvalAgain(model) {
2312
2323
  return noteCard(
2313
2324
  model.text,
@@ -2347,21 +2358,34 @@ ${model.page}`),
2347
2358
  [confirmButton()]
2348
2359
  );
2349
2360
  }
2361
+ var BINDING_WORDS = {
2362
+ en: {
2363
+ title: (bot) => `Connect ${bot}`,
2364
+ body: "Open the link, sign in to your Primus account and confirm once. After that I work as you on the Primus platform, with your own permissions.",
2365
+ note: "The link works once and runs out in 10 minutes. I use a token that lasts 5 minutes, never your password. You can remove the connection in SaFE under identity bindings.",
2366
+ open: "Open link",
2367
+ confirmed: "I confirmed"
2368
+ },
2369
+ zh: {
2370
+ title: (bot) => `\u8FDE\u63A5 ${bot}`,
2371
+ body: "\u6253\u5F00\u94FE\u63A5\uFF0C\u767B\u5F55\u4F60\u7684 Primus \u8D26\u53F7\u5E76\u786E\u8BA4\u4E00\u6B21\u3002\u4E4B\u540E\u6211\u5728 Primus \u5E73\u53F0\u4E0A\u4EE5\u4F60\u7684\u8EAB\u4EFD\u3001\u7528\u4F60\u81EA\u5DF1\u7684\u6743\u9650\u505A\u4E8B\u3002",
2372
+ note: "\u94FE\u63A5\u53EA\u80FD\u7528\u4E00\u6B21\uFF0C10 \u5206\u949F\u540E\u5931\u6548\u3002\u6211\u7528\u7684\u662F 5 \u5206\u949F\u6709\u6548\u7684\u4EE4\u724C\uFF0C\u4ECE\u4E0D\u7528\u4F60\u7684\u5BC6\u7801\u3002\u4F60\u53EF\u4EE5\u5728 SaFE \u7684\u8EAB\u4EFD\u7ED1\u5B9A\u91CC\u5220\u9664\u8FD9\u4E2A\u8FDE\u63A5\u3002",
2373
+ open: "\u6253\u5F00\u94FE\u63A5",
2374
+ confirmed: "\u6211\u5DF2\u786E\u8BA4"
2375
+ }
2376
+ };
2350
2377
  function bindingCard(model) {
2378
+ const words = BINDING_WORDS[model.lang === "zh" ? "zh" : "en"];
2351
2379
  return message(
2352
2380
  [
2353
- header(`Connect ${model.bot}`, { tone: "accent", subtitle: model.lead.trim(), emoji: "connect" }),
2354
- text(
2355
- "Open the link, sign in to your Primus account and confirm once. After that I work as you on the Primus platform, with your own permissions."
2356
- ),
2357
- note(
2358
- "The link works once and runs out in 10 minutes. I use a token that lasts 5 minutes, never your password. You can remove the connection in SaFE under identity bindings."
2359
- ),
2381
+ header(words.title(model.bot), { tone: "accent", subtitle: model.lead.trim(), emoji: "connect" }),
2382
+ text(words.body),
2383
+ note(words.note),
2360
2384
  note(model.notice)
2361
2385
  ],
2362
2386
  buttons([
2363
- openUrl("Open link", model.confirmUrl),
2364
- execute("I confirmed", "primusclaw.bind", { primusclaw: "bind", action: "confirm" })
2387
+ openUrl(words.open, model.confirmUrl),
2388
+ execute(words.confirmed, "primusclaw.bind", { primusclaw: "bind", action: "confirm" })
2365
2389
  ])
2366
2390
  );
2367
2391
  }
@@ -6327,7 +6351,9 @@ function proactiveParts(base2, style, assistant, errors, router) {
6327
6351
  const deps = {
6328
6352
  store,
6329
6353
  conversationFor: conversationFor(base2),
6330
- deliverStored: (envelope2) => surface.conversation(refOf(envelope2["reference"] ?? {})).deliver(envelope2),
6354
+ deliverStored: async (envelope2) => {
6355
+ await surface.conversation(refOf(envelope2["reference"] ?? {})).deliver(envelope2);
6356
+ },
6331
6357
  messageIds: surface.caps.messageIds,
6332
6358
  say: new SayPlanner({
6333
6359
  store,
@@ -6750,6 +6776,7 @@ function approvalClicks(base2, guard, hooks) {
6750
6776
  // packages/teams/src/assembly/features-w8.ts
6751
6777
  import { createHash as createHash3 } from "node:crypto";
6752
6778
  import {
6779
+ chatCredentialOf,
6753
6780
  DailyBrief,
6754
6781
  FeedbackFlow,
6755
6782
  FeedbackReporting,
@@ -6833,7 +6860,9 @@ function w8Parts(base2, options) {
6833
6860
  reporting,
6834
6861
  sessions: store.sessions,
6835
6862
  claw: { sessions: claw.sessions, assistant: claw.assistant },
6836
- credential,
6863
+ // A vote is cast as the person's chats run: someone bound but never linked has no key, and their turns run on the
6864
+ // exchanged token, so a key-only vote was never recorded for them.
6865
+ credential: chatCredentialOf(store.links, settings2.exchange.forChat ? base2.exchange : void 0),
6837
6866
  assistantOn: options.assistantOn,
6838
6867
  sha256Hex: (text4) => createHash3("sha256").update(text4, "utf8").digest("hex"),
6839
6868
  mask: mask2,
package/dist/main.js CHANGED
@@ -3033,7 +3033,8 @@ var TeamsConversation = class {
3033
3033
  return envelope("send", reference(this.ref), this.prepare(activityOf(message2, this.parts.render)));
3034
3034
  }
3035
3035
  async deliver(stored) {
3036
- await this.publish(asEnvelope(stored));
3036
+ const delivered = await this.publish(asEnvelope(stored));
3037
+ return delivered?.activityId || void 0;
3037
3038
  }
3038
3039
  async send(message2) {
3039
3040
  await this.deliver(this.outgoing(message2));
@@ -3751,6 +3752,7 @@ function questionCard(model) {
3751
3752
  header(t.questionTitle, { tone: "accent", subtitle: model.note, emoji: "question" }),
3752
3753
  ...model.questions.flatMap((q, i) => questionInputs(q, i, t))
3753
3754
  ];
3755
+ if (model.typedReply === true) body.push(note(model.group ? t.questionReplyGroup : t.questionReply));
3754
3756
  if (model.group) body.push(note(t.questionOnly({ who: model.ownerName || t.questionAsker })));
3755
3757
  return message(body, [answerButton(model.sessionId, model.toolUseId, model.questions)]);
3756
3758
  }
@@ -3771,12 +3773,21 @@ function approvalExpired(model) {
3771
3773
  return message([header(t.expired, { tone: "attention", emoji: "expired" }), text2(line2, { subtle: true })]);
3772
3774
  }
3773
3775
  function questionAnswered(model) {
3776
+ if (model.replied !== void 0) return questionReplied(model, model.replied);
3774
3777
  const said = proactiveText.in("en").answered({ at: clockText(model.when, model.zone) });
3775
3778
  return message([
3776
3779
  header(said, { tone: "good", emoji: "good" }),
3777
3780
  ...model.answers.map(([, answer]) => text2(clip(answer, ANSWER_SHOWN_MAX)))
3778
3781
  ]);
3779
3782
  }
3783
+ function questionReplied(model, lang) {
3784
+ const t = approvalText.in(lang);
3785
+ const answer = [...new Set(model.answers.map(([, a]) => a))].join(" / ");
3786
+ return message([
3787
+ header(t.questionReplied({ answer: clip(answer, TITLE_MAX) }), { tone: "good", emoji: "good" }),
3788
+ note(t.questionRepliedAt({ at: clockText(model.when, model.zone) }))
3789
+ ]);
3790
+ }
3780
3791
  function approvalAgain(model) {
3781
3792
  return noteCard(
3782
3793
  model.text,
@@ -3816,21 +3827,34 @@ ${model.page}`),
3816
3827
  [confirmButton()]
3817
3828
  );
3818
3829
  }
3830
+ var BINDING_WORDS = {
3831
+ en: {
3832
+ title: (bot) => `Connect ${bot}`,
3833
+ body: "Open the link, sign in to your Primus account and confirm once. After that I work as you on the Primus platform, with your own permissions.",
3834
+ note: "The link works once and runs out in 10 minutes. I use a token that lasts 5 minutes, never your password. You can remove the connection in SaFE under identity bindings.",
3835
+ open: "Open link",
3836
+ confirmed: "I confirmed"
3837
+ },
3838
+ zh: {
3839
+ title: (bot) => `\u8FDE\u63A5 ${bot}`,
3840
+ body: "\u6253\u5F00\u94FE\u63A5\uFF0C\u767B\u5F55\u4F60\u7684 Primus \u8D26\u53F7\u5E76\u786E\u8BA4\u4E00\u6B21\u3002\u4E4B\u540E\u6211\u5728 Primus \u5E73\u53F0\u4E0A\u4EE5\u4F60\u7684\u8EAB\u4EFD\u3001\u7528\u4F60\u81EA\u5DF1\u7684\u6743\u9650\u505A\u4E8B\u3002",
3841
+ note: "\u94FE\u63A5\u53EA\u80FD\u7528\u4E00\u6B21\uFF0C10 \u5206\u949F\u540E\u5931\u6548\u3002\u6211\u7528\u7684\u662F 5 \u5206\u949F\u6709\u6548\u7684\u4EE4\u724C\uFF0C\u4ECE\u4E0D\u7528\u4F60\u7684\u5BC6\u7801\u3002\u4F60\u53EF\u4EE5\u5728 SaFE \u7684\u8EAB\u4EFD\u7ED1\u5B9A\u91CC\u5220\u9664\u8FD9\u4E2A\u8FDE\u63A5\u3002",
3842
+ open: "\u6253\u5F00\u94FE\u63A5",
3843
+ confirmed: "\u6211\u5DF2\u786E\u8BA4"
3844
+ }
3845
+ };
3819
3846
  function bindingCard(model) {
3847
+ const words = BINDING_WORDS[model.lang === "zh" ? "zh" : "en"];
3820
3848
  return message(
3821
3849
  [
3822
- header(`Connect ${model.bot}`, { tone: "accent", subtitle: model.lead.trim(), emoji: "connect" }),
3823
- text2(
3824
- "Open the link, sign in to your Primus account and confirm once. After that I work as you on the Primus platform, with your own permissions."
3825
- ),
3826
- note(
3827
- "The link works once and runs out in 10 minutes. I use a token that lasts 5 minutes, never your password. You can remove the connection in SaFE under identity bindings."
3828
- ),
3850
+ header(words.title(model.bot), { tone: "accent", subtitle: model.lead.trim(), emoji: "connect" }),
3851
+ text2(words.body),
3852
+ note(words.note),
3829
3853
  note(model.notice)
3830
3854
  ],
3831
3855
  buttons([
3832
- openUrl("Open link", model.confirmUrl),
3833
- execute("I confirmed", "primusclaw.bind", { primusclaw: "bind", action: "confirm" })
3856
+ openUrl(words.open, model.confirmUrl),
3857
+ execute(words.confirmed, "primusclaw.bind", { primusclaw: "bind", action: "confirm" })
3834
3858
  ])
3835
3859
  );
3836
3860
  }
@@ -6295,7 +6319,9 @@ function proactiveParts(base2, style, assistant, errors, router) {
6295
6319
  const deps = {
6296
6320
  store,
6297
6321
  conversationFor: conversationFor(base2),
6298
- deliverStored: (envelope2) => surface.conversation(refOf(envelope2["reference"] ?? {})).deliver(envelope2),
6322
+ deliverStored: async (envelope2) => {
6323
+ await surface.conversation(refOf(envelope2["reference"] ?? {})).deliver(envelope2);
6324
+ },
6299
6325
  messageIds: surface.caps.messageIds,
6300
6326
  say: new SayPlanner({
6301
6327
  store,
@@ -6718,6 +6744,7 @@ function approvalClicks(base2, guard, hooks) {
6718
6744
  // packages/teams/src/assembly/features-w8.ts
6719
6745
  import { createHash as createHash3 } from "node:crypto";
6720
6746
  import {
6747
+ chatCredentialOf,
6721
6748
  DailyBrief,
6722
6749
  FeedbackFlow,
6723
6750
  FeedbackReporting,
@@ -6801,7 +6828,9 @@ function w8Parts(base2, options) {
6801
6828
  reporting,
6802
6829
  sessions: store.sessions,
6803
6830
  claw: { sessions: claw.sessions, assistant: claw.assistant },
6804
- credential,
6831
+ // A vote is cast as the person's chats run: someone bound but never linked has no key, and their turns run on the
6832
+ // exchanged token, so a key-only vote was never recorded for them.
6833
+ credential: chatCredentialOf(store.links, settings2.exchange.forChat ? base2.exchange : void 0),
6805
6834
  assistantOn: options.assistantOn,
6806
6835
  sha256Hex: (text4) => createHash3("sha256").update(text4, "utf8").digest("hex"),
6807
6836
  mask: mask2,
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @primusclaw/personal-agent-teams/connector-hosts: the Bot Connector host rule alone (where the bot's token may be
3
+ * sent), for the Teams ingress (@primusclaw/personal-agent-teams-ingress), which applies the worker's rule to the
4
+ * serviceUrl of every activity it accepts. An entry of its own so that the ingress loads this module and core, not the
5
+ * worker.
6
+ */
7
+ export { ConnectorHosts, DEFAULT_CONNECTOR_HOSTS, DEFAULT_HOSTS, DEFAULT_SUFFIXES } from "./connector/hosts.js";
8
+ export type { ServiceUrlPolicy } from "./connector/hosts.js";
9
+ export { UnaddressableError } from "./connector/errors.js";
@@ -42,7 +42,7 @@ export declare class TeamsConversation implements Conversation {
42
42
  private publish;
43
43
  typing(): Promise<void>;
44
44
  outgoing(message: OutMessage): Envelope;
45
- deliver(stored: Envelope): Promise<void>;
45
+ deliver(stored: Envelope): Promise<MessageId | undefined>;
46
46
  send(message: OutMessage): Promise<void>;
47
47
  /**
48
48
  * Send and return Teams' id of the new message; undefined if it never arrives. A direct send returns the id itself; a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primusclaw/personal-agent-teams",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "The personal agent on Microsoft Teams: activities, Adaptive Cards, streams, Bot Connector, inbound queue, process assembly.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,6 +17,10 @@
17
17
  ".": {
18
18
  "types": "./dist/types/index.d.ts",
19
19
  "default": "./dist/index.js"
20
+ },
21
+ "./connector-hosts": {
22
+ "types": "./dist/types/connector-hosts.d.ts",
23
+ "default": "./dist/connector-hosts.js"
20
24
  }
21
25
  },
22
26
  "files": [
@@ -31,8 +35,8 @@
31
35
  "dependencies": {
32
36
  "@azure/core-auth": "^1.11.0",
33
37
  "@azure/service-bus": "^7.10.0",
34
- "@primusclaw/personal-agent": "0.3.0",
35
- "@primusclaw/personal-agent-runtime": "0.3.0",
38
+ "@primusclaw/personal-agent": "0.5.0",
39
+ "@primusclaw/personal-agent-runtime": "0.5.0",
36
40
  "zod": "^4.6.7"
37
41
  },
38
42
  "devDependencies": {