opentool 0.8.23 → 0.8.24

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.
@@ -6,6 +6,29 @@ import { hexToBytes, concatBytes, bytesToHex } from '@noble/hashes/utils';
6
6
  // src/adapters/hyperliquid/index.ts
7
7
 
8
8
  // src/store/index.ts
9
+ var STORE_EVENT_LEVELS = [
10
+ "decision",
11
+ "execution",
12
+ "lifecycle"
13
+ ];
14
+ var STORE_EVENT_LEVEL_SET = new Set(STORE_EVENT_LEVELS);
15
+ var MARKET_REQUIRED_ACTIONS = [
16
+ "swap",
17
+ "bridge",
18
+ "order",
19
+ "trade",
20
+ "lend",
21
+ "borrow",
22
+ "repay",
23
+ "stake",
24
+ "unstake",
25
+ "withdraw",
26
+ "provide_liquidity",
27
+ "remove_liquidity",
28
+ "claim"
29
+ ];
30
+ var MARKET_REQUIRED_ACTIONS_SET = new Set(MARKET_REQUIRED_ACTIONS);
31
+ var EXECUTION_ACTIONS_SET = new Set(MARKET_REQUIRED_ACTIONS);
9
32
  var StoreError = class extends Error {
10
33
  constructor(message, status, causeData) {
11
34
  super(message);
@@ -14,13 +37,57 @@ var StoreError = class extends Error {
14
37
  this.name = "StoreError";
15
38
  }
16
39
  };
40
+ var normalizeAction = (action) => {
41
+ const normalized = action?.trim().toLowerCase();
42
+ return normalized ? normalized : null;
43
+ };
44
+ var coerceEventLevel = (value) => {
45
+ if (typeof value !== "string") return null;
46
+ const normalized = value.trim().toLowerCase();
47
+ if (!normalized || !STORE_EVENT_LEVEL_SET.has(normalized)) return null;
48
+ return normalized;
49
+ };
17
50
  var requiresMarketIdentity = (input) => {
18
- const action = (input.action ?? "").toLowerCase();
19
- if (action === "order" || action === "swap" || action === "trade") return true;
20
- return false;
51
+ const action = normalizeAction(input.action);
52
+ if (!action) return false;
53
+ return MARKET_REQUIRED_ACTIONS_SET.has(action);
21
54
  };
22
55
  var hasMarketIdentity = (value) => {
23
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
56
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
57
+ const record = value;
58
+ const requiredKeys = ["market_type", "venue", "environment", "canonical_symbol"];
59
+ return requiredKeys.every((key) => {
60
+ const field = record[key];
61
+ return typeof field === "string" && field.trim().length > 0;
62
+ });
63
+ };
64
+ var resolveEventLevel = (input) => {
65
+ const direct = coerceEventLevel(input.eventLevel);
66
+ if (direct) return direct;
67
+ const metadataLevel = coerceEventLevel(input.metadata?.eventLevel);
68
+ if (metadataLevel) return metadataLevel;
69
+ const action = normalizeAction(input.action);
70
+ if (action && EXECUTION_ACTIONS_SET.has(action) && (input.metadata?.lifecycle === true || typeof input.metadata?.executionRef === "string" || typeof input.metadata?.parentExecutionRef === "string")) {
71
+ return "lifecycle";
72
+ }
73
+ if (action && EXECUTION_ACTIONS_SET.has(action) || hasMarketIdentity(input.market)) {
74
+ return "execution";
75
+ }
76
+ if (action) return "decision";
77
+ return null;
78
+ };
79
+ var normalizeStoreInput = (input) => {
80
+ const metadata = { ...input.metadata ?? {} };
81
+ const eventLevel = resolveEventLevel({ ...input, metadata });
82
+ if (eventLevel) {
83
+ metadata.eventLevel = eventLevel;
84
+ }
85
+ const hasMetadata = Object.keys(metadata).length > 0;
86
+ return {
87
+ ...input,
88
+ ...eventLevel ? { eventLevel } : {},
89
+ ...hasMetadata ? { metadata } : {}
90
+ };
24
91
  };
25
92
  function resolveConfig(options) {
26
93
  const baseUrl = options?.baseUrl ?? process.env.BASE_URL ?? "https://api.openpond.ai";
@@ -41,8 +108,26 @@ function resolveConfig(options) {
41
108
  return { baseUrl: normalizedBaseUrl, apiKey, fetchFn };
42
109
  }
43
110
  async function store(input, options) {
44
- if (requiresMarketIdentity(input) && !hasMarketIdentity(input.market)) {
45
- throw new StoreError("market is required for trade events");
111
+ const normalizedInput = normalizeStoreInput(input);
112
+ const eventLevel = normalizedInput.eventLevel;
113
+ const normalizedAction = normalizeAction(normalizedInput.action);
114
+ if (eventLevel === "execution" || eventLevel === "lifecycle") {
115
+ if (!normalizedAction || !EXECUTION_ACTIONS_SET.has(normalizedAction)) {
116
+ throw new StoreError(
117
+ `eventLevel "${eventLevel}" requires an execution action`
118
+ );
119
+ }
120
+ }
121
+ if (eventLevel === "execution" && !hasMarketIdentity(normalizedInput.market)) {
122
+ throw new StoreError(
123
+ `market is required for execution events. market must include market_type, venue, environment, canonical_symbol`
124
+ );
125
+ }
126
+ const shouldApplyLegacyMarketRule = eventLevel == null || eventLevel === "execution";
127
+ if (shouldApplyLegacyMarketRule && requiresMarketIdentity(normalizedInput) && !hasMarketIdentity(normalizedInput.market)) {
128
+ throw new StoreError(
129
+ `market is required for action "${normalizedInput.action}". market must include market_type, venue, environment, canonical_symbol`
130
+ );
46
131
  }
47
132
  const { baseUrl, apiKey, fetchFn } = resolveConfig(options);
48
133
  const url = `${baseUrl}/apps/positions/tx`;
@@ -54,7 +139,7 @@ async function store(input, options) {
54
139
  "content-type": "application/json",
55
140
  "openpond-api-key": apiKey
56
141
  },
57
- body: JSON.stringify(input)
142
+ body: JSON.stringify(normalizedInput)
58
143
  });
59
144
  } catch (error) {
60
145
  throw new StoreError("Failed to reach store endpoint", void 0, error);