@agentmark-ai/shared-utils 0.3.1 → 0.3.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.
package/dist/index.mjs CHANGED
@@ -163,27 +163,26 @@ async function generateTypeDefinitionsV1_0(prompts) {
163
163
  for (const prompt of prompts) {
164
164
  const { path: promptPath, input_schema } = prompt;
165
165
  const name = getInterfaceName(promptPath);
166
- let tools = {};
167
166
  try {
168
167
  let kind = "text";
169
168
  let output_schema = null;
169
+ let hasTools = false;
170
170
  if ("text_config" in prompt) {
171
171
  kind = "text";
172
- tools = prompt.text_config.tools || {};
172
+ hasTools = Array.isArray(prompt.text_config.tools) ? prompt.text_config.tools.length > 0 : !!prompt.text_config.tools;
173
173
  } else if ("object_config" in prompt) {
174
174
  kind = "object";
175
- tools = prompt.object_config.tools || {};
175
+ hasTools = Array.isArray(prompt.object_config.tools) ? prompt.object_config.tools.length > 0 : !!prompt.object_config.tools;
176
176
  output_schema = prompt.object_config.schema;
177
177
  } else if ("image_config" in prompt) {
178
178
  kind = "image";
179
- tools = prompt.image_config.tools || {};
179
+ hasTools = Array.isArray(prompt.image_config.tools) ? prompt.image_config.tools.length > 0 : !!prompt.image_config.tools;
180
180
  }
181
181
  const compile = await getCompile();
182
182
  const inputInterface = input_schema ? await compile(input_schema, `${name}In`, {
183
183
  bannerComment: "",
184
184
  additionalProperties: false
185
185
  }) : `interface ${name}In { [key: string]: any }`;
186
- const toolTypes = await generateToolTypes(tools);
187
186
  const outputInterface = output_schema ? await compile(output_schema, `${name}Out`, {
188
187
  bannerComment: "",
189
188
  additionalProperties: false
@@ -192,12 +191,11 @@ async function generateTypeDefinitionsV1_0(prompts) {
192
191
  inputInterface.replace("export interface", "interface"),
193
192
  outputInterface.replace("export type", "type").replace("export interface", "interface")
194
193
  );
195
- output += toolTypes || "";
196
194
  output += `type ${name} = {
197
195
  kind: '${kind}';
198
196
  input: ${name}In;
199
- output: ${name}Out;${toolTypes ? `
200
- tools?: Array<keyof Tools>;` : ""}
197
+ output: ${name}Out;${hasTools ? `
198
+ tools?: string[];` : ""}
201
199
  };
202
200
 
203
201
  `;
@@ -624,6 +622,7 @@ async function generateTypeDefinitions(prompts, language = "typescript") {
624
622
  return generateTypeDefinitionsV0(prompts);
625
623
  }
626
624
  async function fetchPromptsFrontmatter(options) {
625
+ var _a, _b, _c;
627
626
  if (options.local) {
628
627
  const baseUrl = `http://localhost:${options.local}`;
629
628
  try {
@@ -633,7 +632,8 @@ async function fetchPromptsFrontmatter(options) {
633
632
  `Failed to fetch prompt paths: ${pathsResponse.statusText}`
634
633
  );
635
634
  }
636
- const { paths } = await pathsResponse.json();
635
+ const body = await pathsResponse.json();
636
+ const paths = (_c = (_b = (_a = body == null ? void 0 : body.data) == null ? void 0 : _a.paths) != null ? _b : body == null ? void 0 : body.paths) != null ? _c : [];
637
637
  return Promise.all(
638
638
  paths.map(async (promptPath) => {
639
639
  const templateResponse = await fetch(
@@ -699,34 +699,6 @@ async function fetchPromptsFrontmatter(options) {
699
699
  }
700
700
  throw new Error("Either --local or --root-dir must be specified");
701
701
  }
702
- async function generateToolTypes(tools) {
703
- const toolArgTypes = [];
704
- for (const [toolName, schema] of Object.entries(tools)) {
705
- const typeName = `${getToolInterfaceName(toolName)}Args`;
706
- try {
707
- const compile = await getCompile();
708
- const argInterface = schema.parameters ? await compile(schema.parameters, typeName, {
709
- bannerComment: "",
710
- additionalProperties: false
711
- }) : `type ${typeName} = { ${Object.entries(schema.parameters || {}).map(([key]) => `${key}: any`).join("; ")} };`;
712
- toolArgTypes.push(
713
- argInterface.replace("export type", "type").replace("export interface", "interface")
714
- );
715
- } catch (error) {
716
- console.error(`Error processing tool ${toolName}:`, error);
717
- toolArgTypes.push(`type ${typeName} = { [key: string]: any };`);
718
- }
719
- }
720
- if (Object.keys(tools).length > 0) {
721
- const toolsInterface = `export interface Tools {
722
- ${Object.keys(tools).map(
723
- (toolName) => ` ${toolName}: { args: ${getToolInterfaceName(toolName)}Args };`
724
- ).join("\n")}
725
- }`;
726
- return toolArgTypes.join("\n\n") + "\n\n" + toolsInterface + "\n\n";
727
- }
728
- return null;
729
- }
730
702
 
731
703
  // src/normalizer/types.ts
732
704
  var SpanType = /* @__PURE__ */ ((SpanType2) => {
@@ -826,8 +798,7 @@ var KNOWN_METADATA_FIELDS = /* @__PURE__ */ new Set([
826
798
  "dataset_expected_output",
827
799
  "dataset_input",
828
800
  "prompt_name",
829
- "props",
830
- "commit_sha"
801
+ "props"
831
802
  ]);
832
803
  function parseMetadata(attributes, prefix = "agentmark.metadata.") {
833
804
  const result = {};
@@ -1612,7 +1583,7 @@ function parseAgentMarkAttributes(attributes, prefix = "agentmark.") {
1612
1583
  if (get("trace_name")) result.traceName = String(get("trace_name"));
1613
1584
  if (get("prompt_name")) result.promptName = String(get("prompt_name"));
1614
1585
  if (get("props")) result.props = String(get("props"));
1615
- if (get("span.kind")) result.kind = String(get("span.kind"));
1586
+ if (get("span.kind")) result.semanticKind = String(get("span.kind"));
1616
1587
  if (get("dataset_run_id")) result.datasetRunId = String(get("dataset_run_id"));
1617
1588
  if (get("dataset_run_name")) result.datasetRunName = String(get("dataset_run_name"));
1618
1589
  if (get("dataset_item_name")) result.datasetItemName = String(get("dataset_item_name"));
@@ -1682,6 +1653,9 @@ var AgentMarkTransformer = class {
1682
1653
  if (operationName === OperationNames.CHAT || operationName === OperationNames.TEXT_COMPLETION) {
1683
1654
  return "GENERATION" /* GENERATION */;
1684
1655
  }
1656
+ if (operationName === OperationNames.EMBEDDINGS) {
1657
+ return "GENERATION" /* GENERATION */;
1658
+ }
1685
1659
  if (span.name === SpanNames.CHAT || span.name.startsWith(SpanNames.CHAT + " ")) {
1686
1660
  return "GENERATION" /* GENERATION */;
1687
1661
  }
@@ -1711,6 +1685,7 @@ var AgentMarkTransformer = class {
1711
1685
  * Transform the span and extract normalized fields from GenAI attributes.
1712
1686
  */
1713
1687
  transform(_span, attributes) {
1688
+ var _a;
1714
1689
  const result = {};
1715
1690
  const responseModel = attributes[GenAIAttributes.RESPONSE_MODEL];
1716
1691
  const requestModel = attributes[GenAIAttributes.REQUEST_MODEL];
@@ -1784,7 +1759,7 @@ var AgentMarkTransformer = class {
1784
1759
  } catch {
1785
1760
  }
1786
1761
  }
1787
- const amInput = attributes["agentmark.input"];
1762
+ const amInput = (_a = attributes["agentmark.input"]) != null ? _a : attributes["agentmark.props"];
1788
1763
  if (amInput && typeof amInput === "string" && !result.input) {
1789
1764
  result.input = [{ role: "user", content: amInput }];
1790
1765
  }
@@ -1850,9 +1825,16 @@ var Attrs = {
1850
1825
  RESPONSE_FINISH_REASONS: "gen_ai.response.finish_reasons",
1851
1826
  USAGE_INPUT_TOKENS: "gen_ai.usage.input_tokens",
1852
1827
  USAGE_OUTPUT_TOKENS: "gen_ai.usage.output_tokens",
1853
- // v1.37.0+ content attributes
1828
+ // v1.37.0+ content attributes (canonical OTel GenAI semantic conventions).
1829
+ // SDKs that emit the AgentMark-scoped equivalents (`gen_ai.request.input`
1830
+ // / `gen_ai.response.output` — used by `claude-agent-sdk-v0-adapter`) are
1831
+ // also accepted here as fallbacks so an SDK picking either key set
1832
+ // doesn't silently lose IO data on ingest. The canonical pair always wins
1833
+ // when both are present.
1854
1834
  INPUT_MESSAGES: "gen_ai.input.messages",
1855
1835
  OUTPUT_MESSAGES: "gen_ai.output.messages",
1836
+ REQUEST_INPUT_FALLBACK: "gen_ai.request.input",
1837
+ RESPONSE_OUTPUT_FALLBACK: "gen_ai.response.output",
1856
1838
  SYSTEM_INSTRUCTIONS: "gen_ai.system_instructions",
1857
1839
  TOOL_DEFINITIONS: "gen_ai.tool.definitions",
1858
1840
  // Tool call attributes (v1.37.0+)
@@ -1917,6 +1899,7 @@ var OtelGenAiTransformer = class {
1917
1899
  return "SPAN" /* SPAN */;
1918
1900
  }
1919
1901
  transform(span, attributes) {
1902
+ var _a, _b;
1920
1903
  const result = {};
1921
1904
  const model = attributes[Attrs.RESPONSE_MODEL] || attributes[Attrs.REQUEST_MODEL];
1922
1905
  if (model && typeof model === "string") {
@@ -1937,12 +1920,12 @@ var OtelGenAiTransformer = class {
1937
1920
  if (typeof temperature === "number") {
1938
1921
  result.settings = { ...result.settings, temperature };
1939
1922
  }
1940
- const inputMessages = attributes[Attrs.INPUT_MESSAGES];
1923
+ const inputMessages = (_a = attributes[Attrs.INPUT_MESSAGES]) != null ? _a : attributes[Attrs.REQUEST_INPUT_FALLBACK];
1941
1924
  if (inputMessages && typeof inputMessages === "string") {
1942
1925
  const messages = normalizeMessages(inputMessages);
1943
1926
  if (messages) result.input = messages;
1944
1927
  }
1945
- const outputMessages = attributes[Attrs.OUTPUT_MESSAGES];
1928
+ const outputMessages = (_b = attributes[Attrs.OUTPUT_MESSAGES]) != null ? _b : attributes[Attrs.RESPONSE_OUTPUT_FALLBACK];
1946
1929
  if (outputMessages && typeof outputMessages === "string") {
1947
1930
  const extracted = extractStructuredOutput(outputMessages);
1948
1931
  if (extracted) {
@@ -2104,6 +2087,83 @@ function extractResourceScopeSpan(resourceSpans) {
2104
2087
  return result;
2105
2088
  }
2106
2089
 
2090
+ // src/normalizer/resolvers/semantic-kind-resolver.ts
2091
+ var SEMANTIC_KINDS = ["function", "llm", "tool", "agent", "retrieval", "embedding", "guardrail"];
2092
+ var VALID_KINDS = new Set(SEMANTIC_KINDS);
2093
+ var OPENINFERENCE_MAP = {
2094
+ "CHAIN": "function",
2095
+ "LLM": "llm",
2096
+ "TOOL": "tool",
2097
+ "AGENT": "agent",
2098
+ "RETRIEVER": "retrieval",
2099
+ "EMBEDDING": "embedding",
2100
+ "GUARDRAIL": "guardrail",
2101
+ "RERANKER": "retrieval"
2102
+ };
2103
+ var FRAMEWORK_MAPPINGS = [
2104
+ {
2105
+ key: "ai.operationId",
2106
+ // Vercel AI SDK
2107
+ map: {
2108
+ "embed": "embedding",
2109
+ "ai.embed": "embedding",
2110
+ "generateText": "llm",
2111
+ "streamText": "llm",
2112
+ "generateObject": "llm",
2113
+ "streamObject": "llm"
2114
+ }
2115
+ },
2116
+ {
2117
+ key: "traceloop.span.kind",
2118
+ // Traceloop / OpenLLMetry
2119
+ map: { "LLM": "llm", "TOOL": "tool", "AGENT": "agent", "WORKFLOW": "function", "TASK": "function" }
2120
+ },
2121
+ {
2122
+ key: "langchain.run_type",
2123
+ // LangChain via OTLP
2124
+ map: { "llm": "llm", "chat_model": "llm", "retriever": "retrieval", "tool": "tool", "chain": "function", "embedding": "embedding" }
2125
+ },
2126
+ {
2127
+ key: "genkit:type",
2128
+ // Firebase Genkit
2129
+ map: { "model": "llm", "tool": "tool", "flow": "function", "retriever": "retrieval", "embedder": "embedding" }
2130
+ }
2131
+ ];
2132
+ function resolveSemanticKind(normalized, allAttributes) {
2133
+ if (normalized.semanticKind && VALID_KINDS.has(normalized.semanticKind)) {
2134
+ return normalized.semanticKind;
2135
+ }
2136
+ const oiKind = allAttributes["openinference.span.kind"];
2137
+ if (oiKind) {
2138
+ const mapped = OPENINFERENCE_MAP[String(oiKind).toUpperCase()];
2139
+ if (mapped) return mapped;
2140
+ }
2141
+ for (const { key, map } of FRAMEWORK_MAPPINGS) {
2142
+ const val = allAttributes[key];
2143
+ if (val) {
2144
+ const mapped = map[String(val)];
2145
+ if (mapped) return mapped;
2146
+ }
2147
+ }
2148
+ const opName = allAttributes["gen_ai.operation.name"];
2149
+ if (opName) {
2150
+ const op = String(opName).toLowerCase();
2151
+ if (op === "chat" || op === "text_completion" || op === "generate_content") return "llm";
2152
+ if (op === "embeddings") return "embedding";
2153
+ }
2154
+ if (normalized.type === "GENERATION" /* GENERATION */) {
2155
+ return "llm";
2156
+ }
2157
+ if (normalized.toolCalls && normalized.toolCalls.length > 0) {
2158
+ return "tool";
2159
+ }
2160
+ const name = (normalized.name || "").toLowerCase();
2161
+ if (/retriev|search|rag/i.test(name)) return "retrieval";
2162
+ if (/embed/i.test(name)) return "embedding";
2163
+ if (/guard|safety/i.test(name)) return "guardrail";
2164
+ return "function";
2165
+ }
2166
+
2107
2167
  // src/normalizer/type-classifier.ts
2108
2168
  var TypeClassifier = class {
2109
2169
  classify(span, attributes) {
@@ -2182,6 +2242,7 @@ function normalizeSpan(resource, scope, span) {
2182
2242
  }
2183
2243
  const agentMarkAttributes = parseAgentMarkAttributes(allAttributes);
2184
2244
  Object.assign(normalized, agentMarkAttributes);
2245
+ normalized.semanticKind = resolveSemanticKind(normalized, allAttributes);
2185
2246
  return normalized;
2186
2247
  }
2187
2248
  function normalizeOtlpSpans(resourceSpans) {
@@ -2201,6 +2262,7 @@ export {
2201
2262
  AgentMarkTransformer as ClaudeAgentTransformer,
2202
2263
  MastraTransformer,
2203
2264
  OtelGenAiTransformer,
2265
+ SEMANTIC_KINDS,
2204
2266
  SpanType,
2205
2267
  TransformerRegistry,
2206
2268
  TypeClassifier,
@@ -2220,6 +2282,7 @@ export {
2220
2282
  parseMetadata,
2221
2283
  parseTokens,
2222
2284
  registry,
2285
+ resolveSemanticKind,
2223
2286
  toFrontMatter,
2224
2287
  typeClassifier,
2225
2288
  verifySignature