@effect/ai-openai-compat 4.0.1 → 4.0.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.
@@ -190,6 +190,10 @@ declare module "effect/ai/Prompt" {
190
190
  * The status to send for the tool-call item.
191
191
  */
192
192
  readonly status?: MessageStatus | null
193
+ /**
194
+ * Provider-specific extra content to echo back with the tool call.
195
+ */
196
+ readonly extraContent?: Schema.JsonObject | null
193
197
  } | null
194
198
  }
195
199
 
@@ -424,6 +428,10 @@ declare module "effect/ai/Response" {
424
428
  * The OpenAI item ID associated with the tool call.
425
429
  */
426
430
  readonly itemId?: string | null
431
+ /**
432
+ * Provider-specific extra content returned with the tool call.
433
+ */
434
+ readonly extraContent?: Schema.JsonObject | null
427
435
  } | null
428
436
  }
429
437
 
@@ -1034,12 +1042,14 @@ const prepareMessages = Effect.fnUntraced(
1034
1042
  }
1035
1043
 
1036
1044
  const toolName = toolNameMapper.getProviderName(part.name)
1045
+ const extraContent = getExtraContent(part)
1037
1046
 
1038
1047
  messages.push({
1039
1048
  type: "function_call",
1040
1049
  name: toolName,
1041
1050
  call_id: part.id,
1042
1051
  arguments: JSON.stringify(part.params),
1052
+ ...(Predicate.isNotNull(extraContent) ? { extra_content: extraContent } : {}),
1043
1053
  ...(Predicate.isNotNull(id) ? { id } : {}),
1044
1054
  ...(Predicate.isNotNull(status) ? { status } : {})
1045
1055
  })
@@ -1134,6 +1144,7 @@ type ActiveToolCall = {
1134
1144
  readonly id: string
1135
1145
  name: string
1136
1146
  arguments: string
1147
+ extraContent: Schema.JsonObject | undefined
1137
1148
  }
1138
1149
 
1139
1150
  const makeResponse = Effect.fnUntraced(
@@ -1202,7 +1213,12 @@ const makeResponse = Effect.fnUntraced(
1202
1213
  id: toolId,
1203
1214
  name: toolName,
1204
1215
  params,
1205
- metadata: { openai: { ...makeItemIdMetadata(toolCall.id) } }
1216
+ metadata: {
1217
+ openai: {
1218
+ ...makeItemIdMetadata(toolCall.id),
1219
+ ...makeExtraContentMetadata(toolCall.extra_content ?? undefined)
1220
+ }
1221
+ }
1206
1222
  })
1207
1223
  }
1208
1224
  }
@@ -1294,7 +1310,12 @@ const makeStreamResponse = Effect.fnUntraced(
1294
1310
  id: toolCall.id,
1295
1311
  name: toolCall.name,
1296
1312
  params,
1297
- metadata: { openai: { ...makeItemIdMetadata(toolCall.id) } }
1313
+ metadata: {
1314
+ openai: {
1315
+ ...makeItemIdMetadata(toolCall.id),
1316
+ ...makeExtraContentMetadata(toolCall.extraContent)
1317
+ }
1318
+ }
1298
1319
  })
1299
1320
  hasToolCalls = true
1300
1321
  }
@@ -1356,7 +1377,7 @@ const makeStreamResponse = Effect.fnUntraced(
1356
1377
  parts.push({ type: "reasoning-delta", id: reasoningId, delta: reasoningDelta })
1357
1378
  }
1358
1379
 
1359
- if (choice.delta?.content !== undefined && Predicate.isNotNull(choice.delta.content)) {
1380
+ if (Predicate.isNotNullish(choice.delta?.content) && choice.delta.content.length > 0) {
1360
1381
  if (reasoningStarted) {
1361
1382
  reasoningStarted = false
1362
1383
  parts.push({
@@ -1393,12 +1414,16 @@ const makeStreamResponse = Effect.fnUntraced(
1393
1414
  activeToolCalls[toolIndex] = {
1394
1415
  id: toolId,
1395
1416
  name: toolName,
1396
- arguments: argumentsDelta
1417
+ arguments: argumentsDelta,
1418
+ extraContent: deltaTool.extra_content ?? undefined
1397
1419
  }
1398
1420
  parts.push({ type: "tool-params-start", id: toolId, name: toolName })
1399
1421
  } else {
1400
1422
  activeToolCall.name = toolName
1401
1423
  activeToolCall.arguments = `${activeToolCall.arguments}${argumentsDelta}`
1424
+ if (Predicate.isNotNullish(deltaTool.extra_content)) {
1425
+ activeToolCall.extraContent = deltaTool.extra_content
1426
+ }
1402
1427
  }
1403
1428
 
1404
1429
  if (argumentsDelta.length > 0) {
@@ -1427,7 +1452,7 @@ const annotateRequest = (
1427
1452
  request: CreateResponseRequestJson
1428
1453
  ): void => {
1429
1454
  addGenAIAnnotations(span, {
1430
- system: "openai",
1455
+ provider: { name: "openai" },
1431
1456
  operation: { name: "chat" },
1432
1457
  request: {
1433
1458
  model: request.model as string,
@@ -1437,7 +1462,6 @@ const annotateRequest = (
1437
1462
  },
1438
1463
  openai: {
1439
1464
  request: {
1440
- responseFormat: request.response_format?.type,
1441
1465
  serviceTier: request.service_tier as string | undefined
1442
1466
  }
1443
1467
  }
@@ -1879,7 +1903,8 @@ const toChatToolCall = (
1879
1903
  function: {
1880
1904
  name: item.name,
1881
1905
  arguments: item.arguments
1882
- }
1906
+ },
1907
+ ...(item.extra_content !== undefined ? { extra_content: item.extra_content } : undefined)
1883
1908
  })
1884
1909
 
1885
1910
  const toAssistantChatMessageContent = (
@@ -1985,11 +2010,17 @@ const getStatus = (
1985
2010
  const getEncryptedContent = (
1986
2011
  part: Prompt.ReasoningPart
1987
2012
  ): string | null => part.options.openai?.encryptedContent ?? null
2013
+ const getExtraContent = (
2014
+ part: Prompt.ToolCallPart
2015
+ ): Schema.JsonObject | null => part.options.openai?.extraContent ?? null
1988
2016
 
1989
2017
  const getImageDetail = (part: Prompt.FilePart): ImageDetail => part.options.openai?.imageDetail ?? "auto"
1990
2018
 
1991
2019
  const makeItemIdMetadata = (itemId: string | undefined) => itemId !== undefined ? { itemId } : undefined
1992
2020
 
2021
+ const makeExtraContentMetadata = (extraContent: Schema.JsonObject | undefined) =>
2022
+ extraContent !== undefined ? { extraContent } : undefined
2023
+
1993
2024
  const normalizeServiceTier = (
1994
2025
  serviceTier: string | undefined
1995
2026
  ): "default" | "auto" | "flex" | "scale" | "priority" | null | undefined => {
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * The `OpenAiTelemetry` module defines OpenAI-compatible telemetry attributes
3
3
  * and a helper for adding them to a tracing span. It keeps the standard GenAI
4
- * telemetry attributes and adds request and response metadata under the
5
- * `gen_ai.openai.*` OpenTelemetry namespaces.
4
+ * telemetry attributes and adds service tier and system fingerprint under
5
+ * `openai.*`.
6
6
  *
7
+ * @stability unstable
7
8
  * @since 4.0.0
8
9
  */
9
10
  import * as Telemetry from "effect/ai/Telemetry"
@@ -22,36 +23,24 @@ import type { Simplify } from "effect/Types"
22
23
  * conventions:
23
24
  * https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
24
25
  *
26
+ * @stability unstable
25
27
  * @category models
26
28
  * @since 4.0.0
27
29
  */
28
30
  export type OpenAiTelemetryAttributes = Simplify<
29
31
  & Telemetry.GenAITelemetryAttributes
30
- & Telemetry.AttributesWithPrefix<RequestAttributes, "gen_ai.openai.request">
31
- & Telemetry.AttributesWithPrefix<ResponseAttributes, "gen_ai.openai.response">
32
+ & Telemetry.AttributesWithPrefix<RequestAttributes, "openai.request">
33
+ & Telemetry.AttributesWithPrefix<ResponseAttributes, "openai.response">
32
34
  >
33
35
 
34
36
  /**
35
- * All telemetry attributes which are part of the GenAI specification,
36
- * including the OpenAI-specific attributes.
37
- *
38
- * @category models
39
- * @since 4.0.0
40
- */
41
- export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
42
-
43
- /**
44
- * Telemetry attributes which are part of the GenAI specification and are
45
- * namespaced by `gen_ai.openai.request`.
37
+ * OpenAI request metadata, written under `openai.request.*`.
46
38
  *
39
+ * @stability unstable
47
40
  * @category models
48
41
  * @since 4.0.0
49
42
  */
50
43
  export interface RequestAttributes {
51
- /**
52
- * The response format that is requested.
53
- */
54
- readonly responseFormat?: (string & {}) | WellKnownResponseFormat | null | undefined
55
44
  /**
56
45
  * The service tier requested. May be a specific tier, `default`, or `auto`.
57
46
  */
@@ -60,8 +49,9 @@ export interface RequestAttributes {
60
49
 
61
50
  /**
62
51
  * Telemetry attributes which are part of the GenAI specification and are
63
- * namespaced by `gen_ai.openai.response`.
52
+ * namespaced by `openai.response`.
64
53
  *
54
+ * @stability unstable
65
55
  * @category models
66
56
  * @since 4.0.0
67
57
  */
@@ -78,21 +68,7 @@ export interface ResponseAttributes {
78
68
  }
79
69
 
80
70
  /**
81
- * The `gen_ai.openai.request.response_format` attribute has a list of
82
- * well-known values.
83
- *
84
- * **Details**
85
- *
86
- * If one of them applies, then the respective value **MUST** be used;
87
- * otherwise, a custom value **MAY** be used.
88
- *
89
- * @category models
90
- * @since 4.0.0
91
- */
92
- export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
93
-
94
- /**
95
- * The `gen_ai.openai.request.service_tier` attribute has a list of
71
+ * The `openai.request.service_tier` attribute has a list of
96
72
  * well-known values.
97
73
  *
98
74
  * **Details**
@@ -100,6 +76,7 @@ export type WellKnownResponseFormat = "json_object" | "json_schema" | "text"
100
76
  * If one of them applies, then the respective value **MUST** be used;
101
77
  * otherwise, a custom value **MAY** be used.
102
78
  *
79
+ * @stability unstable
103
80
  * @category models
104
81
  * @since 4.0.0
105
82
  */
@@ -109,6 +86,7 @@ export type WellKnownServiceTier = "auto" | "default"
109
86
  * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry
110
87
  * attributes with optional OpenAI-compatible request and response attributes.
111
88
  *
89
+ * @stability unstable
112
90
  * @category options
113
91
  * @since 4.0.0
114
92
  */
@@ -119,10 +97,10 @@ export type OpenAiTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeO
119
97
  } | undefined
120
98
  }
121
99
 
122
- const addOpenAiRequestAttributes = Telemetry.addSpanAttributes("gen_ai.openai.request", String.camelToSnake)<
100
+ const addOpenAiRequestAttributes = Telemetry.addSpanAttributes("openai.request", String.camelToSnake)<
123
101
  RequestAttributes
124
102
  >
125
- const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.response", String.camelToSnake)<
103
+ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("openai.response", String.camelToSnake)<
126
104
  ResponseAttributes
127
105
  >
128
106
 
@@ -137,14 +115,14 @@ const addOpenAiResponseAttributes = Telemetry.addSpanAttributes("gen_ai.openai.r
137
115
  *
138
116
  * **Details**
139
117
  *
140
- * Standard GenAI attributes are applied first. When OpenAI request or response
141
- * metadata is present, it is written under `gen_ai.openai.request.*` and
142
- * `gen_ai.openai.response.*` attributes.
118
+ * Standard GenAI attributes are applied first. OpenAI metadata is written under
119
+ * `openai.request.*` and `openai.response.*`.
143
120
  *
144
121
  * **Gotchas**
145
122
  *
146
123
  * Mutates the supplied `Span` in place.
147
124
  *
125
+ * @stability unstable
148
126
  * @category tracing
149
127
  * @since 4.0.0
150
128
  */
@@ -160,14 +138,14 @@ export const addGenAIAnnotations: {
160
138
  *
161
139
  * **Details**
162
140
  *
163
- * Standard GenAI attributes are applied first. When OpenAI request or response
164
- * metadata is present, it is written under `gen_ai.openai.request.*` and
165
- * `gen_ai.openai.response.*` attributes.
141
+ * Standard GenAI attributes are applied first. OpenAI metadata is written under
142
+ * `openai.request.*` and `openai.response.*`.
166
143
  *
167
144
  * **Gotchas**
168
145
  *
169
146
  * Mutates the supplied `Span` in place.
170
147
  *
148
+ * @stability unstable
171
149
  * @category tracing
172
150
  * @since 4.0.0
173
151
  */
@@ -183,14 +161,14 @@ export const addGenAIAnnotations: {
183
161
  *
184
162
  * **Details**
185
163
  *
186
- * Standard GenAI attributes are applied first. When OpenAI request or response
187
- * metadata is present, it is written under `gen_ai.openai.request.*` and
188
- * `gen_ai.openai.response.*` attributes.
164
+ * Standard GenAI attributes are applied first. OpenAI metadata is written under
165
+ * `openai.request.*` and `openai.response.*`.
189
166
  *
190
167
  * **Gotchas**
191
168
  *
192
169
  * Mutates the supplied `Span` in place.
193
170
  *
171
+ * @stability unstable
194
172
  * @category tracing
195
173
  * @since 4.0.0
196
174
  */
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * @stability unstable
2
3
  * @since 4.0.0
3
4
  */
4
5
 
@@ -11,6 +12,7 @@
11
12
  export * as OpenAiClient from "./OpenAiClient.ts"
12
13
 
13
14
  /**
15
+ * @stability unstable
14
16
  * @since 4.0.0
15
17
  */
16
18
  export * as OpenAiConfig from "./OpenAiConfig.ts"
@@ -22,6 +24,7 @@ export * as OpenAiConfig from "./OpenAiConfig.ts"
22
24
  export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts"
23
25
 
24
26
  /**
27
+ * @stability unstable
25
28
  * @since 4.0.0
26
29
  */
27
30
  export * as OpenAiError from "./OpenAiError.ts"
@@ -33,6 +36,7 @@ export * as OpenAiError from "./OpenAiError.ts"
33
36
  export * as OpenAiLanguageModel from "./OpenAiLanguageModel.ts"
34
37
 
35
38
  /**
39
+ * @stability unstable
36
40
  * @since 4.0.0
37
41
  */
38
42
  export * as OpenAiTelemetry from "./OpenAiTelemetry.ts"