@okrlinkhub/agent-factory 3.0.3 → 3.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.
@@ -7,6 +7,7 @@ import {
7
7
  import type { Auth, HttpRouter } from "convex/server";
8
8
  import { v } from "convex/values";
9
9
  import type { ComponentApi } from "../component/_generated/component.js";
10
+ import { parseTelegramWebhookSecretToken } from "../component/identity.js";
10
11
  import {
11
12
  providerConfigValidator,
12
13
  scalingPolicyValidator,
@@ -488,6 +489,7 @@ export function exposeApi(
488
489
  agentKey: "default",
489
490
  version: "1.0.0",
490
491
  secretsRef: ["telegram.botToken"],
492
+ botIdentity: "telegram-bot-default",
491
493
  enabled: true,
492
494
  });
493
495
  },
@@ -599,6 +601,7 @@ export function exposeApi(
599
601
  args: {
600
602
  consumerUserId: v.string(),
601
603
  agentKey: v.string(),
604
+ botIdentity: v.optional(v.string()),
602
605
  source: v.optional(
603
606
  v.union(v.literal("manual"), v.literal("telegram_pairing"), v.literal("api")),
604
607
  ),
@@ -681,6 +684,7 @@ export function exposeApi(
681
684
  }),
682
685
  resolveAgentForTelegram: queryGeneric({
683
686
  args: {
687
+ botIdentity: v.optional(v.string()),
684
688
  telegramUserId: v.optional(v.string()),
685
689
  telegramChatId: v.optional(v.string()),
686
690
  },
@@ -714,6 +718,7 @@ export function exposeApi(
714
718
  consumePairingCode: mutationGeneric({
715
719
  args: {
716
720
  code: v.string(),
721
+ botIdentity: v.optional(v.string()),
717
722
  telegramUserId: v.string(),
718
723
  telegramChatId: v.string(),
719
724
  },
@@ -741,7 +746,7 @@ export function exposeApi(
741
746
  return await ctx.runQuery((component.lib as any).getUserAgentPairingStatus, args);
742
747
  },
743
748
  }),
744
- importTelegramTokenForAgent: mutationGeneric({
749
+ importTelegramTokenForAgent: actionGeneric({
745
750
  args: {
746
751
  consumerUserId: v.string(),
747
752
  agentKey: v.string(),
@@ -750,7 +755,17 @@ export function exposeApi(
750
755
  },
751
756
  handler: async (ctx, args) => {
752
757
  await options.auth(ctx, { type: "write", agentKey: args.agentKey });
753
- return await ctx.runMutation((component.lib as any).importTelegramTokenForAgent, args);
758
+ return await ctx.runAction((component.lib as any).importTelegramTokenForAgent, args);
759
+ },
760
+ }),
761
+ reconcileTelegramBotIdentityForAgent: actionGeneric({
762
+ args: {
763
+ agentKey: v.string(),
764
+ secretRef: v.optional(v.string()),
765
+ },
766
+ handler: async (ctx, args) => {
767
+ await options.auth(ctx, { type: "write", agentKey: args.agentKey });
768
+ return await ctx.runAction((component.lib as any).reconcileTelegramBotIdentityForAgent, args);
754
769
  },
755
770
  }),
756
771
  getUserAgentOnboardingState: queryGeneric({
@@ -803,12 +818,27 @@ export function exposeApi(
803
818
  args: {
804
819
  convexSiteUrl: v.string(),
805
820
  secretRef: v.optional(v.string()),
821
+ agentKey: v.optional(v.string()),
806
822
  },
807
823
  handler: async (ctx, args) => {
808
824
  await options.auth(ctx, { type: "read" });
809
825
  return await ctx.runAction(component.lib.configureTelegramWebhook, args);
810
826
  },
811
827
  }),
828
+ softResetTelegramBindingsMissingBotIdentity: mutationGeneric({
829
+ args: {
830
+ nowMs: v.optional(v.number()),
831
+ revokeActiveBindings: v.optional(v.boolean()),
832
+ expirePendingPairings: v.optional(v.boolean()),
833
+ },
834
+ handler: async (ctx, args) => {
835
+ await options.auth(ctx, { type: "write" });
836
+ return await ctx.runMutation(
837
+ (component.lib as any).softResetTelegramBindingsMissingBotIdentity,
838
+ args,
839
+ );
840
+ },
841
+ }),
812
842
  getWebhookReadiness: actionGeneric({
813
843
  args: {
814
844
  agentKey: v.string(),
@@ -1243,6 +1273,18 @@ export function registerRoutes(
1243
1273
  path: `${pathPrefix}/telegram/webhook`,
1244
1274
  method: "POST",
1245
1275
  handler: httpActionGeneric(async (ctx, request) => {
1276
+ const botIdentity = parseTelegramWebhookSecretToken(
1277
+ request.headers.get("X-Telegram-Bot-Api-Secret-Token"),
1278
+ );
1279
+ if (!botIdentity) {
1280
+ return new Response(
1281
+ JSON.stringify({ ok: false, error: "missing or invalid telegram webhook secret token" }),
1282
+ {
1283
+ status: 403,
1284
+ headers: { "Content-Type": "application/json" },
1285
+ },
1286
+ );
1287
+ }
1246
1288
  const update = (await request.json()) as {
1247
1289
  update_id?: number;
1248
1290
  message?: {
@@ -1296,6 +1338,7 @@ export function registerRoutes(
1296
1338
  try {
1297
1339
  const pairing = await ctx.runMutation(component.lib.consumePairingCode, {
1298
1340
  code: startCommandCode,
1341
+ botIdentity,
1299
1342
  telegramUserId,
1300
1343
  telegramChatId,
1301
1344
  });
@@ -1347,6 +1390,7 @@ export function registerRoutes(
1347
1390
 
1348
1391
  const mappedRaw = resolveAgentKeyFromBinding
1349
1392
  ? await ctx.runQuery(component.lib.resolveAgentForTelegram, {
1393
+ botIdentity,
1350
1394
  telegramUserId,
1351
1395
  telegramChatId,
1352
1396
  })
@@ -1376,6 +1420,7 @@ export function registerRoutes(
1376
1420
  });
1377
1421
  }
1378
1422
  const metadata: Record<string, string> = {
1423
+ telegramBotIdentity: botIdentity,
1379
1424
  telegramChatId,
1380
1425
  telegramUserId,
1381
1426
  };
@@ -1397,7 +1442,7 @@ export function registerRoutes(
1397
1442
  })
1398
1443
  : undefined;
1399
1444
  await ctx.runMutation(component.lib.enqueue, {
1400
- conversationId: mapped.conversationId ?? `telegram:${telegramChatId}`,
1445
+ conversationId: mapped.conversationId ?? buildTelegramIngressConversationId(botIdentity, telegramChatId),
1401
1446
  agentKey,
1402
1447
  payload: {
1403
1448
  provider: "telegram",
@@ -1423,6 +1468,10 @@ function parseStartCommandCode(messageText: string): string | null {
1423
1468
  return match?.[1] ?? null;
1424
1469
  }
1425
1470
 
1471
+ function buildTelegramIngressConversationId(botIdentity: string, telegramChatId: string) {
1472
+ return `telegram:${botIdentity}:${telegramChatId}`;
1473
+ }
1474
+
1426
1475
  type TelegramWebhookMessage = {
1427
1476
  text?: string;
1428
1477
  caption?: string;
@@ -66,6 +66,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
66
66
  "internal",
67
67
  {
68
68
  agentKey: string;
69
+ botIdentity?: string;
69
70
  consumerUserId: string;
70
71
  metadata?: Record<string, string>;
71
72
  nowMs?: number;
@@ -75,6 +76,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
75
76
  },
76
77
  {
77
78
  agentKey: string;
79
+ botIdentity: null | string;
78
80
  boundAt: number;
79
81
  consumerUserId: string;
80
82
  conversationId: string;
@@ -90,8 +92,9 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
90
92
  configureTelegramWebhook: FunctionReference<
91
93
  "action",
92
94
  "internal",
93
- { convexSiteUrl: string; secretRef?: string },
95
+ { agentKey?: string; convexSiteUrl: string; secretRef?: string },
94
96
  {
97
+ botIdentity: null | string;
95
98
  currentUrl: null | string;
96
99
  description: string;
97
100
  isReady: boolean;
@@ -99,6 +102,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
99
102
  lastErrorMessage: null | string;
100
103
  ok: boolean;
101
104
  pendingUpdateCount: number;
105
+ secretTokenConfigured: boolean;
102
106
  webhookUrl: string;
103
107
  },
104
108
  Name
@@ -107,6 +111,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
107
111
  "mutation",
108
112
  "internal",
109
113
  {
114
+ botIdentity?: string;
110
115
  code: string;
111
116
  nowMs?: number;
112
117
  telegramChatId: string;
@@ -114,6 +119,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
114
119
  },
115
120
  {
116
121
  agentKey: string;
122
+ botIdentity: null | string;
117
123
  code: string;
118
124
  consumerUserId: string;
119
125
  createdAt: number;
@@ -136,6 +142,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
136
142
  },
137
143
  {
138
144
  agentKey: string;
145
+ botIdentity: null | string;
139
146
  code: string;
140
147
  consumerUserId: string;
141
148
  createdAt: number;
@@ -160,6 +167,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
160
167
  deepLink: null | string;
161
168
  pairing: {
162
169
  agentKey: string;
170
+ botIdentity: null | string;
163
171
  code: string;
164
172
  consumerUserId: string;
165
173
  createdAt: number;
@@ -213,6 +221,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
213
221
  { code: string; nowMs?: number },
214
222
  null | {
215
223
  agentKey: string;
224
+ botIdentity: null | string;
216
225
  code: string;
217
226
  consumerUserId: string;
218
227
  createdAt: number;
@@ -283,6 +292,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
283
292
  { consumerUserId: string },
284
293
  null | {
285
294
  agentKey: string;
295
+ botIdentity: null | string;
286
296
  boundAt: number;
287
297
  consumerUserId: string;
288
298
  conversationId: string;
@@ -301,6 +311,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
301
311
  { agentKey: string; consumerUserId: string; nowMs?: number },
302
312
  {
303
313
  agentKey: string;
314
+ botIdentity: null | string;
304
315
  nextAction:
305
316
  | "import_token"
306
317
  | "configure_webhook"
@@ -323,6 +334,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
323
334
  { agentKey: string; consumerUserId: string; nowMs?: number },
324
335
  null | {
325
336
  agentKey: string;
337
+ botIdentity: null | string;
326
338
  code: string;
327
339
  consumerUserId: string;
328
340
  createdAt: number;
@@ -375,7 +387,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
375
387
  Name
376
388
  >;
377
389
  importTelegramTokenForAgent: FunctionReference<
378
- "mutation",
390
+ "action",
379
391
  "internal",
380
392
  {
381
393
  agentKey: string;
@@ -383,7 +395,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
383
395
  metadata?: Record<string, string>;
384
396
  plaintextValue: string;
385
397
  },
386
- { secretId: string; secretRef: string; version: number },
398
+ {
399
+ botIdentity: string;
400
+ secretId: string;
401
+ secretRef: string;
402
+ telegramUsername: null | string;
403
+ version: number;
404
+ },
387
405
  Name
388
406
  >;
389
407
  listUserAgents: FunctionReference<
@@ -407,10 +425,26 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
407
425
  }>,
408
426
  Name
409
427
  >;
428
+ reconcileTelegramBotIdentityForAgent: FunctionReference<
429
+ "action",
430
+ "internal",
431
+ { agentKey: string; secretRef?: string },
432
+ {
433
+ agentKey: string;
434
+ botIdentity: string;
435
+ secretRef: null | string;
436
+ telegramUsername: null | string;
437
+ },
438
+ Name
439
+ >;
410
440
  resolveAgentForTelegram: FunctionReference<
411
441
  "query",
412
442
  "internal",
413
- { telegramChatId?: string; telegramUserId?: string },
443
+ {
444
+ botIdentity?: string;
445
+ telegramChatId?: string;
446
+ telegramUserId?: string;
447
+ },
414
448
  {
415
449
  agentKey: null | string;
416
450
  consumerUserId: null | string;
@@ -432,6 +466,24 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
432
466
  { revoked: number },
433
467
  Name
434
468
  >;
469
+ softResetTelegramBindingsMissingBotIdentity: FunctionReference<
470
+ "mutation",
471
+ "internal",
472
+ {
473
+ expirePendingPairings?: boolean;
474
+ nowMs?: number;
475
+ revokeActiveBindings?: boolean;
476
+ },
477
+ {
478
+ annotatedBindings: number;
479
+ expiredPairings: number;
480
+ legacyBindingsMissingBotIdentity: number;
481
+ pendingPairingsMissingBotIdentity: number;
482
+ profilesMissingBotIdentity: number;
483
+ revokedBindings: number;
484
+ },
485
+ Name
486
+ >;
435
487
  };
436
488
  lib: {
437
489
  appendConversationMessages: FunctionReference<
@@ -462,6 +514,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
462
514
  "internal",
463
515
  {
464
516
  agentKey: string;
517
+ botIdentity?: string;
465
518
  consumerUserId: string;
466
519
  metadata?: Record<string, string>;
467
520
  nowMs?: number;
@@ -471,6 +524,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
471
524
  },
472
525
  {
473
526
  agentKey: string;
527
+ botIdentity: null | string;
474
528
  boundAt: number;
475
529
  consumerUserId: string;
476
530
  conversationId: string;
@@ -567,6 +621,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
567
621
  "internal",
568
622
  {
569
623
  agentKey: string;
624
+ botIdentity?: string;
570
625
  bridgeConfig?: {
571
626
  appBaseUrlMapJsonSecretRef?: string;
572
627
  appKey?: string;
@@ -585,8 +640,9 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
585
640
  configureTelegramWebhook: FunctionReference<
586
641
  "action",
587
642
  "internal",
588
- { convexSiteUrl: string; secretRef?: string },
643
+ { agentKey?: string; convexSiteUrl: string; secretRef?: string },
589
644
  {
645
+ botIdentity: null | string;
590
646
  currentUrl: null | string;
591
647
  description: string;
592
648
  isReady: boolean;
@@ -594,6 +650,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
594
650
  lastErrorMessage: null | string;
595
651
  ok: boolean;
596
652
  pendingUpdateCount: number;
653
+ secretTokenConfigured: boolean;
597
654
  webhookUrl: string;
598
655
  },
599
656
  Name
@@ -602,6 +659,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
602
659
  "mutation",
603
660
  "internal",
604
661
  {
662
+ botIdentity?: string;
605
663
  code: string;
606
664
  nowMs?: number;
607
665
  telegramChatId: string;
@@ -609,6 +667,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
609
667
  },
610
668
  {
611
669
  agentKey: string;
670
+ botIdentity: null | string;
612
671
  code: string;
613
672
  consumerUserId: string;
614
673
  createdAt: number;
@@ -645,6 +704,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
645
704
  },
646
705
  {
647
706
  agentKey: string;
707
+ botIdentity: null | string;
648
708
  code: string;
649
709
  consumerUserId: string;
650
710
  createdAt: number;
@@ -772,6 +832,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
772
832
  deepLink: null | string;
773
833
  pairing: {
774
834
  agentKey: string;
835
+ botIdentity: null | string;
775
836
  code: string;
776
837
  consumerUserId: string;
777
838
  createdAt: number;
@@ -1008,6 +1069,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1008
1069
  appBaseUrlMapJson: null | string;
1009
1070
  appKey: null | string;
1010
1071
  baseUrl: null | string;
1072
+ botIdentity: null | string;
1011
1073
  serviceId: null | string;
1012
1074
  serviceKey: null | string;
1013
1075
  serviceKeySecretRef: null | string;
@@ -1070,6 +1132,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1070
1132
  { code: string; nowMs?: number },
1071
1133
  null | {
1072
1134
  agentKey: string;
1135
+ botIdentity: null | string;
1073
1136
  code: string;
1074
1137
  consumerUserId: string;
1075
1138
  createdAt: number;
@@ -1147,6 +1210,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1147
1210
  { consumerUserId: string },
1148
1211
  null | {
1149
1212
  agentKey: string;
1213
+ botIdentity: null | string;
1150
1214
  boundAt: number;
1151
1215
  consumerUserId: string;
1152
1216
  conversationId: string;
@@ -1176,6 +1240,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1176
1240
  { agentKey: string; consumerUserId: string; nowMs?: number },
1177
1241
  {
1178
1242
  agentKey: string;
1243
+ botIdentity: null | string;
1179
1244
  nextAction:
1180
1245
  | "import_token"
1181
1246
  | "configure_webhook"
@@ -1198,6 +1263,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1198
1263
  { agentKey: string; consumerUserId: string; nowMs?: number },
1199
1264
  null | {
1200
1265
  agentKey: string;
1266
+ botIdentity: null | string;
1201
1267
  code: string;
1202
1268
  consumerUserId: string;
1203
1269
  createdAt: number;
@@ -1300,7 +1366,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1300
1366
  Name
1301
1367
  >;
1302
1368
  importTelegramTokenForAgent: FunctionReference<
1303
- "mutation",
1369
+ "action",
1304
1370
  "internal",
1305
1371
  {
1306
1372
  agentKey: string;
@@ -1308,7 +1374,13 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1308
1374
  metadata?: Record<string, string>;
1309
1375
  plaintextValue: string;
1310
1376
  },
1311
- { secretId: string; secretRef: string; version: number },
1377
+ {
1378
+ botIdentity: string;
1379
+ secretId: string;
1380
+ secretRef: string;
1381
+ telegramUsername: null | string;
1382
+ version: number;
1383
+ },
1312
1384
  Name
1313
1385
  >;
1314
1386
  listMessageTemplatesByCompany: FunctionReference<
@@ -1609,6 +1681,18 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1609
1681
  { deadLetter: number; processing: number; queuedReady: number },
1610
1682
  Name
1611
1683
  >;
1684
+ reconcileTelegramBotIdentityForAgent: FunctionReference<
1685
+ "action",
1686
+ "internal",
1687
+ { agentKey: string; secretRef?: string },
1688
+ {
1689
+ agentKey: string;
1690
+ botIdentity: string;
1691
+ secretRef: null | string;
1692
+ telegramUsername: null | string;
1693
+ },
1694
+ Name
1695
+ >;
1612
1696
  reconcileWorkers: FunctionReference<
1613
1697
  "action",
1614
1698
  "internal",
@@ -1648,7 +1732,11 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1648
1732
  resolveAgentForTelegram: FunctionReference<
1649
1733
  "query",
1650
1734
  "internal",
1651
- { telegramChatId?: string; telegramUserId?: string },
1735
+ {
1736
+ botIdentity?: string;
1737
+ telegramChatId?: string;
1738
+ telegramUserId?: string;
1739
+ },
1652
1740
  {
1653
1741
  agentKey: null | string;
1654
1742
  consumerUserId: null | string;
@@ -1829,6 +1917,24 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
1829
1917
  boolean,
1830
1918
  Name
1831
1919
  >;
1920
+ softResetTelegramBindingsMissingBotIdentity: FunctionReference<
1921
+ "mutation",
1922
+ "internal",
1923
+ {
1924
+ expirePendingPairings?: boolean;
1925
+ nowMs?: number;
1926
+ revokeActiveBindings?: boolean;
1927
+ },
1928
+ {
1929
+ annotatedBindings: number;
1930
+ expiredPairings: number;
1931
+ legacyBindingsMissingBotIdentity: number;
1932
+ pendingPairingsMissingBotIdentity: number;
1933
+ profilesMissingBotIdentity: number;
1934
+ revokedBindings: number;
1935
+ },
1936
+ Name
1937
+ >;
1832
1938
  triggerPushJobNow: FunctionReference<
1833
1939
  "mutation",
1834
1940
  "internal",
@@ -2738,6 +2844,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
2738
2844
  appBaseUrlMapJson: null | string;
2739
2845
  appKey: null | string;
2740
2846
  baseUrl: null | string;
2847
+ botIdentity: null | string;
2741
2848
  serviceId: null | string;
2742
2849
  serviceKey: null | string;
2743
2850
  serviceKeySecretRef: null | string;
@@ -3251,6 +3358,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
3251
3358
  "internal",
3252
3359
  {
3253
3360
  agentKey: string;
3361
+ botIdentity?: string;
3254
3362
  bridgeConfig?: {
3255
3363
  appBaseUrlMapJsonSecretRef?: string;
3256
3364
  appKey?: string;