teleton 0.7.2 → 0.7.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.
@@ -1,6 +1,10 @@
1
+ import {
2
+ getClaudeCodeApiKey,
3
+ refreshClaudeCodeApiKey
4
+ } from "./chunk-JQDLW7IE.js";
1
5
  import {
2
6
  getProviderMetadata
3
- } from "./chunk-LRCPA7SC.js";
7
+ } from "./chunk-RMLQS3X6.js";
4
8
  import {
5
9
  appendToTranscript,
6
10
  readTranscript
@@ -92,12 +96,13 @@ function sanitizeToolsForGemini(tools) {
92
96
  // src/agent/client.ts
93
97
  var log = createLogger("LLM");
94
98
  function isOAuthToken(apiKey, provider) {
95
- if (provider && provider !== "anthropic") return false;
99
+ if (provider && provider !== "anthropic" && provider !== "claude-code") return false;
96
100
  return apiKey.startsWith("sk-ant-oat01-");
97
101
  }
98
102
  function getEffectiveApiKey(provider, rawKey) {
99
103
  if (provider === "local") return "local";
100
104
  if (provider === "cocoon") return "";
105
+ if (provider === "claude-code") return getClaudeCodeApiKey(rawKey);
101
106
  return rawKey;
102
107
  }
103
108
  var modelCache = /* @__PURE__ */ new Map();
@@ -307,7 +312,15 @@ async function chatWithContext(config, options) {
307
312
  const { stripCocoonPayload } = await import("./tool-adapter-Y3TCEQOC.js");
308
313
  completeOptions.onPayload = stripCocoonPayload;
309
314
  }
310
- const response = await complete(model, context, completeOptions);
315
+ let response = await complete(model, context, completeOptions);
316
+ if (provider === "claude-code" && response.stopReason === "error" && response.errorMessage && (response.errorMessage.includes("401") || response.errorMessage.toLowerCase().includes("unauthorized"))) {
317
+ log.warn("Claude Code token rejected (401), refreshing credentials and retrying...");
318
+ const refreshedKey = refreshClaudeCodeApiKey();
319
+ if (refreshedKey) {
320
+ completeOptions.apiKey = refreshedKey;
321
+ response = await complete(model, context, completeOptions);
322
+ }
323
+ }
311
324
  if (isCocoon) {
312
325
  const textBlock = response.content.find((b) => b.type === "text");
313
326
  if (textBlock?.type === "text" && textBlock.text.includes("<tool_call>")) {
@@ -1,14 +1,13 @@
1
1
  import {
2
2
  ConfigSchema,
3
+ getCachedTonClient,
3
4
  getKeyPair,
4
5
  getTonPrice,
5
6
  getWalletAddress,
6
7
  getWalletBalance,
8
+ invalidateTonClientCache,
7
9
  loadWallet
8
- } from "./chunk-NERLQY2H.js";
9
- import {
10
- getCachedHttpEndpoint
11
- } from "./chunk-QUAPFI2N.js";
10
+ } from "./chunk-DAMCNMYL.js";
12
11
  import {
13
12
  require_BigInteger
14
13
  } from "./chunk-TSKJCWQQ.js";
@@ -17,7 +16,7 @@ import {
17
16
  } from "./chunk-XBE4JB7C.js";
18
17
  import {
19
18
  getProviderMetadata
20
- } from "./chunk-LRCPA7SC.js";
19
+ } from "./chunk-RMLQS3X6.js";
21
20
  import {
22
21
  createDbWrapper,
23
22
  migrateFromMainDb,
@@ -92,7 +91,7 @@ Run 'teleton setup' to create one.`);
92
91
  }
93
92
  const config = result.data;
94
93
  const provider = config.agent.provider;
95
- if (provider !== "anthropic" && !raw.agent?.model) {
94
+ if (provider !== "anthropic" && provider !== "claude-code" && !raw.agent?.model) {
96
95
  const meta = getProviderMetadata(provider);
97
96
  config.agent.model = meta.defaultModel;
98
97
  }
@@ -657,7 +656,7 @@ var PluginSDKError = class extends Error {
657
656
  var SDK_VERSION = "1.0.0";
658
657
 
659
658
  // src/ton/transfer.ts
660
- import { WalletContractV5R1, TonClient, toNano, internal } from "@ton/ton";
659
+ import { WalletContractV5R1, toNano, internal } from "@ton/ton";
661
660
  import { Address, SendMode } from "@ton/core";
662
661
 
663
662
  // src/ton/tx-lock.ts
@@ -699,8 +698,7 @@ async function sendTon(params) {
699
698
  workchain: 0,
700
699
  publicKey: keyPair.publicKey
701
700
  });
702
- const endpoint = await getCachedHttpEndpoint();
703
- const client = new TonClient({ endpoint });
701
+ const client = await getCachedTonClient();
704
702
  const contract = client.open(wallet);
705
703
  const seqno = await contract.getSeqno();
706
704
  await contract.sendTransfer({
@@ -720,8 +718,11 @@ async function sendTon(params) {
720
718
  log4.info(`Sent ${amount} TON to ${toAddress.slice(0, 8)}... - seqno: ${seqno}`);
721
719
  return pseudoHash;
722
720
  } catch (error) {
721
+ if (error?.status >= 500 || error?.response?.status >= 500) {
722
+ invalidateTonClientCache();
723
+ }
723
724
  log4.error({ err: error }, "Error sending TON");
724
- return null;
725
+ throw error;
725
726
  }
726
727
  });
727
728
  }
@@ -775,11 +776,181 @@ async function withBlockchainRetry(fn, operation = "blockchain operation") {
775
776
  }
776
777
 
777
778
  // src/sdk/ton.ts
778
- import { toNano as tonToNano, fromNano as tonFromNano } from "@ton/ton";
779
- import { Address as TonAddress } from "@ton/core";
779
+ import {
780
+ toNano as tonToNano,
781
+ fromNano as tonFromNano,
782
+ WalletContractV5R1 as WalletContractV5R12,
783
+ internal as internal2
784
+ } from "@ton/ton";
785
+ import { Address as TonAddress, beginCell, SendMode as SendMode2 } from "@ton/core";
786
+
787
+ // src/ton/format-transactions.ts
788
+ import { fromNano } from "@ton/ton";
789
+ var OP_CODES = {
790
+ COMMENT: 0,
791
+ JETTON_TRANSFER: 260734629,
792
+ JETTON_TRANSFER_NOTIFICATION: 1935855772,
793
+ JETTON_INTERNAL_TRANSFER: 395134233,
794
+ JETTON_BURN: 1499400124,
795
+ NFT_TRANSFER: 1607220500,
796
+ NFT_OWNERSHIP_ASSIGNED: 85167505,
797
+ EXCESSES: 3576854235,
798
+ BOUNCE: 4294967295
799
+ };
800
+ function parseMessageBody(body) {
801
+ if (!body) return null;
802
+ try {
803
+ const slice = body.beginParse();
804
+ if (slice.remainingBits < 32) return null;
805
+ const op = slice.loadUint(32);
806
+ if (op === OP_CODES.COMMENT && slice.remainingBits > 0) {
807
+ return { op, comment: slice.loadStringTail() };
808
+ }
809
+ if (op === OP_CODES.JETTON_TRANSFER_NOTIFICATION) {
810
+ const _queryId = slice.loadUint(64);
811
+ const amount = slice.loadCoins();
812
+ const _sender = slice.loadAddress();
813
+ return { op, jettonAmount: amount.toString() };
814
+ }
815
+ if (op === OP_CODES.JETTON_TRANSFER) {
816
+ const _queryId = slice.loadUint(64);
817
+ const amount = slice.loadCoins();
818
+ const _destination = slice.loadAddress();
819
+ return { op, jettonAmount: amount.toString() };
820
+ }
821
+ if (op === OP_CODES.NFT_OWNERSHIP_ASSIGNED) {
822
+ const _queryId = slice.loadUint(64);
823
+ const _prevOwner = slice.loadAddress();
824
+ return { op };
825
+ }
826
+ if (op === OP_CODES.NFT_TRANSFER) {
827
+ const _queryId = slice.loadUint(64);
828
+ const newOwner = slice.loadAddress();
829
+ return { op, nftAddress: newOwner?.toString() };
830
+ }
831
+ return { op };
832
+ } catch {
833
+ return null;
834
+ }
835
+ }
836
+ function formatTransactions(transactions) {
837
+ return transactions.map((tx) => {
838
+ const inMsg = tx.inMessage;
839
+ const outMsgArray = [...tx.outMessages.values()];
840
+ const hash = tx.hash().toString("hex");
841
+ const explorer = `https://tonviewer.com/transaction/${hash}`;
842
+ const txTimeMs = tx.now * 1e3;
843
+ const date = new Date(txTimeMs).toISOString();
844
+ const secondsAgo = Math.max(0, Math.floor((Date.now() - txTimeMs) / 1e3));
845
+ if (inMsg?.info.type === "internal") {
846
+ const tonAmount = fromNano(inMsg.info.value.coins);
847
+ const from = inMsg.info.src?.toString() || "unknown";
848
+ const parsed = parseMessageBody(inMsg.body);
849
+ if (parsed?.op === OP_CODES.EXCESSES) {
850
+ return {
851
+ type: "gas_refund",
852
+ hash,
853
+ amount: `${tonAmount} TON`,
854
+ from,
855
+ date,
856
+ secondsAgo,
857
+ explorer
858
+ };
859
+ }
860
+ if (parsed?.op === OP_CODES.JETTON_TRANSFER_NOTIFICATION) {
861
+ return {
862
+ type: "jetton_received",
863
+ hash,
864
+ jettonAmount: parsed.jettonAmount,
865
+ jettonWallet: from,
866
+ date,
867
+ secondsAgo,
868
+ explorer
869
+ };
870
+ }
871
+ if (parsed?.op === OP_CODES.NFT_OWNERSHIP_ASSIGNED) {
872
+ return { type: "nft_received", hash, nftAddress: from, date, secondsAgo, explorer };
873
+ }
874
+ if (inMsg.info.bounced || parsed?.op === OP_CODES.BOUNCE) {
875
+ return {
876
+ type: "bounce",
877
+ hash,
878
+ amount: `${tonAmount} TON`,
879
+ from,
880
+ date,
881
+ secondsAgo,
882
+ explorer
883
+ };
884
+ }
885
+ return {
886
+ type: "ton_received",
887
+ hash,
888
+ amount: `${tonAmount} TON`,
889
+ from,
890
+ comment: parsed?.comment || null,
891
+ date,
892
+ secondsAgo,
893
+ explorer
894
+ };
895
+ }
896
+ if (outMsgArray.length > 0) {
897
+ const results = [];
898
+ for (const outMsg of outMsgArray) {
899
+ if (outMsg.info.type !== "internal") continue;
900
+ const info = outMsg.info;
901
+ const to = info.dest?.toString() || "unknown";
902
+ const tonAmount = fromNano(info.value.coins);
903
+ const parsed = parseMessageBody(outMsg.body);
904
+ if (parsed?.op === OP_CODES.JETTON_TRANSFER) {
905
+ results.push({
906
+ type: "jetton_sent",
907
+ hash,
908
+ jettonAmount: parsed.jettonAmount,
909
+ jettonWallet: to,
910
+ date,
911
+ secondsAgo,
912
+ explorer
913
+ });
914
+ continue;
915
+ }
916
+ if (parsed?.op === OP_CODES.NFT_TRANSFER) {
917
+ results.push({ type: "nft_sent", hash, nftAddress: to, date, secondsAgo, explorer });
918
+ continue;
919
+ }
920
+ results.push({
921
+ type: "ton_sent",
922
+ hash,
923
+ amount: `${tonAmount} TON`,
924
+ to,
925
+ comment: parsed?.comment || null,
926
+ date,
927
+ secondsAgo,
928
+ explorer
929
+ });
930
+ }
931
+ if (results.length === 1) return results[0];
932
+ if (results.length > 1) {
933
+ return { type: "multi_send", hash, transfers: results, date, secondsAgo, explorer };
934
+ }
935
+ }
936
+ return { type: "contract_call", hash, date, secondsAgo, explorer };
937
+ });
938
+ }
939
+
940
+ // src/sdk/ton.ts
780
941
  var DEFAULT_MAX_AGE_MINUTES = 10;
781
942
  var DEFAULT_TX_RETENTION_DAYS = 30;
782
943
  var CLEANUP_PROBABILITY = 0.1;
944
+ function findJettonBalance(balances, jettonAddress) {
945
+ return balances.find((b) => {
946
+ if (b.jetton.address.toLowerCase() === jettonAddress.toLowerCase()) return true;
947
+ try {
948
+ return TonAddress.parse(b.jetton.address).toString() === TonAddress.parse(jettonAddress).toString();
949
+ } catch {
950
+ return false;
951
+ }
952
+ });
953
+ }
783
954
  function cleanupOldTransactions(db, retentionDays, log7) {
784
955
  if (Math.random() > CLEANUP_PROBABILITY) return;
785
956
  try {
@@ -829,8 +1000,7 @@ function createTonSDK(log7, db) {
829
1000
  throw new PluginSDKError("Amount must be a positive number", "OPERATION_FAILED");
830
1001
  }
831
1002
  try {
832
- const { Address: Address2 } = await import("@ton/core");
833
- Address2.parse(to);
1003
+ TonAddress.parse(to);
834
1004
  } catch {
835
1005
  throw new PluginSDKError("Invalid TON address format", "INVALID_ADDRESS");
836
1006
  }
@@ -858,13 +1028,8 @@ function createTonSDK(log7, db) {
858
1028
  },
859
1029
  async getTransactions(address, limit) {
860
1030
  try {
861
- const { TonClient: TonClient2 } = await import("@ton/ton");
862
- const { Address: Address2 } = await import("@ton/core");
863
- const { getCachedHttpEndpoint: getCachedHttpEndpoint2 } = await import("./endpoint-FLYNEZ2F.js");
864
- const { formatTransactions } = await import("./format-transactions-FD74HI5N.js");
865
- const addressObj = Address2.parse(address);
866
- const endpoint = await getCachedHttpEndpoint2();
867
- const client = new TonClient2({ endpoint });
1031
+ const addressObj = TonAddress.parse(address);
1032
+ const client = await getCachedTonClient();
868
1033
  const transactions = await withBlockchainRetry(
869
1034
  () => client.getTransactions(addressObj, {
870
1035
  limit: Math.min(limit ?? 10, 50)
@@ -935,7 +1100,7 @@ function createTonSDK(log7, db) {
935
1100
  try {
936
1101
  const addr = ownerAddress ?? getWalletAddress();
937
1102
  if (!addr) return [];
938
- const response = await tonapiFetch(`/accounts/${addr}/jettons`);
1103
+ const response = await tonapiFetch(`/accounts/${encodeURIComponent(addr)}/jettons`);
939
1104
  if (!response.ok) {
940
1105
  log7.error(`ton.getJettonBalances() TonAPI error: ${response.status}`);
941
1106
  return [];
@@ -971,7 +1136,7 @@ function createTonSDK(log7, db) {
971
1136
  },
972
1137
  async getJettonInfo(jettonAddress) {
973
1138
  try {
974
- const response = await tonapiFetch(`/jettons/${jettonAddress}`);
1139
+ const response = await tonapiFetch(`/jettons/${encodeURIComponent(jettonAddress)}`);
975
1140
  if (response.status === 404) return null;
976
1141
  if (!response.ok) {
977
1142
  log7.error(`ton.getJettonInfo() TonAPI error: ${response.status}`);
@@ -997,9 +1162,6 @@ function createTonSDK(log7, db) {
997
1162
  }
998
1163
  },
999
1164
  async sendJetton(jettonAddress, to, amount, opts) {
1000
- const { Address: Address2, beginCell, SendMode: SendMode2 } = await import("@ton/core");
1001
- const { WalletContractV5R1: WalletContractV5R12, TonClient: TonClient2, toNano: toNano2, internal: internal2 } = await import("@ton/ton");
1002
- const { getCachedHttpEndpoint: getCachedHttpEndpoint2 } = await import("./endpoint-FLYNEZ2F.js");
1003
1165
  const walletData = loadWallet();
1004
1166
  if (!walletData) {
1005
1167
  throw new PluginSDKError("Wallet not initialized", "WALLET_NOT_INITIALIZED");
@@ -1008,12 +1170,14 @@ function createTonSDK(log7, db) {
1008
1170
  throw new PluginSDKError("Amount must be a positive number", "OPERATION_FAILED");
1009
1171
  }
1010
1172
  try {
1011
- Address2.parse(to);
1173
+ TonAddress.parse(to);
1012
1174
  } catch {
1013
1175
  throw new PluginSDKError("Invalid recipient address", "INVALID_ADDRESS");
1014
1176
  }
1015
1177
  try {
1016
- const jettonsResponse = await tonapiFetch(`/accounts/${walletData.address}/jettons`);
1178
+ const jettonsResponse = await tonapiFetch(
1179
+ `/accounts/${encodeURIComponent(walletData.address)}/jettons`
1180
+ );
1017
1181
  if (!jettonsResponse.ok) {
1018
1182
  throw new PluginSDKError(
1019
1183
  `Failed to fetch jetton balances: ${jettonsResponse.status}`,
@@ -1021,9 +1185,7 @@ function createTonSDK(log7, db) {
1021
1185
  );
1022
1186
  }
1023
1187
  const jettonsData = await jettonsResponse.json();
1024
- const jettonBalance = jettonsData.balances?.find(
1025
- (b) => b.jetton.address.toLowerCase() === jettonAddress.toLowerCase() || Address2.parse(b.jetton.address).toString() === Address2.parse(jettonAddress).toString()
1026
- );
1188
+ const jettonBalance = findJettonBalance(jettonsData.balances ?? [], jettonAddress);
1027
1189
  if (!jettonBalance) {
1028
1190
  throw new PluginSDKError(
1029
1191
  `You don't own any of this jetton: ${jettonAddress}`,
@@ -1048,7 +1210,7 @@ function createTonSDK(log7, db) {
1048
1210
  forwardPayload = beginCell().storeUint(0, 32).storeStringTail(comment).endCell();
1049
1211
  }
1050
1212
  const JETTON_TRANSFER_OP = 260734629;
1051
- const messageBody = beginCell().storeUint(JETTON_TRANSFER_OP, 32).storeUint(0, 64).storeCoins(amountInUnits).storeAddress(Address2.parse(to)).storeAddress(Address2.parse(walletData.address)).storeBit(false).storeCoins(comment ? toNano2("0.01") : BigInt(1)).storeBit(comment ? true : false).storeMaybeRef(comment ? forwardPayload : null).endCell();
1213
+ const messageBody = beginCell().storeUint(JETTON_TRANSFER_OP, 32).storeUint(0, 64).storeCoins(amountInUnits).storeAddress(TonAddress.parse(to)).storeAddress(TonAddress.parse(walletData.address)).storeBit(false).storeCoins(comment ? tonToNano("0.01") : BigInt(1)).storeBit(comment ? 1 : 0).storeRef(comment ? forwardPayload : beginCell().endCell()).endCell();
1052
1214
  const keyPair = await getKeyPair();
1053
1215
  if (!keyPair) {
1054
1216
  throw new PluginSDKError("Wallet key derivation failed", "OPERATION_FAILED");
@@ -1058,8 +1220,7 @@ function createTonSDK(log7, db) {
1058
1220
  workchain: 0,
1059
1221
  publicKey: keyPair.publicKey
1060
1222
  });
1061
- const endpoint = await getCachedHttpEndpoint2();
1062
- const client = new TonClient2({ endpoint });
1223
+ const client = await getCachedTonClient();
1063
1224
  const walletContract = client.open(wallet);
1064
1225
  const seq = await walletContract.getSeqno();
1065
1226
  await walletContract.sendTransfer({
@@ -1068,8 +1229,8 @@ function createTonSDK(log7, db) {
1068
1229
  sendMode: SendMode2.PAY_GAS_SEPARATELY,
1069
1230
  messages: [
1070
1231
  internal2({
1071
- to: Address2.parse(senderJettonWallet),
1072
- value: toNano2("0.05"),
1232
+ to: TonAddress.parse(senderJettonWallet),
1233
+ value: tonToNano("0.05"),
1073
1234
  body: messageBody,
1074
1235
  bounce: true
1075
1236
  })
@@ -1088,16 +1249,13 @@ function createTonSDK(log7, db) {
1088
1249
  },
1089
1250
  async getJettonWalletAddress(ownerAddress, jettonAddress) {
1090
1251
  try {
1091
- const response = await tonapiFetch(`/accounts/${ownerAddress}/jettons`);
1252
+ const response = await tonapiFetch(`/accounts/${encodeURIComponent(ownerAddress)}/jettons`);
1092
1253
  if (!response.ok) {
1093
1254
  log7.error(`ton.getJettonWalletAddress() TonAPI error: ${response.status}`);
1094
1255
  return null;
1095
1256
  }
1096
- const { Address: Address2 } = await import("@ton/core");
1097
1257
  const data = await response.json();
1098
- const match = (data.balances || []).find(
1099
- (b) => b.jetton.address.toLowerCase() === jettonAddress.toLowerCase() || Address2.parse(b.jetton.address).toString() === Address2.parse(jettonAddress).toString()
1100
- );
1258
+ const match = findJettonBalance(data.balances ?? [], jettonAddress);
1101
1259
  return match ? match.wallet_address.address : null;
1102
1260
  } catch (err) {
1103
1261
  log7.error("ton.getJettonWalletAddress() failed:", err);
@@ -1126,7 +1284,7 @@ function createTonSDK(log7, db) {
1126
1284
  },
1127
1285
  async getNftInfo(nftAddress) {
1128
1286
  try {
1129
- const response = await tonapiFetch(`/nfts/${nftAddress}`);
1287
+ const response = await tonapiFetch(`/nfts/${encodeURIComponent(nftAddress)}`);
1130
1288
  if (response.status === 404) return null;
1131
1289
  if (!response.ok) {
1132
1290
  log7.error(`ton.getNftInfo() TonAPI error: ${response.status}`);
@@ -2910,6 +3068,7 @@ var CONFIGURABLE_KEYS = {
2910
3068
  sensitive: false,
2911
3069
  options: [
2912
3070
  "anthropic",
3071
+ "claude-code",
2913
3072
  "openai",
2914
3073
  "google",
2915
3074
  "xai",
@@ -2922,6 +3081,7 @@ var CONFIGURABLE_KEYS = {
2922
3081
  ],
2923
3082
  validate: enumValidator([
2924
3083
  "anthropic",
3084
+ "claude-code",
2925
3085
  "openai",
2926
3086
  "google",
2927
3087
  "xai",
@@ -3240,6 +3400,7 @@ export {
3240
3400
  randomLong,
3241
3401
  withBlockchainRetry,
3242
3402
  sendTon,
3403
+ formatTransactions,
3243
3404
  adaptPlugin,
3244
3405
  ensurePluginDeps,
3245
3406
  loadEnhancedPlugins,