@grafana/sigil-sdk-js 0.0.1
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/LICENSE +3 -0
- package/README.md +367 -0
- package/dist/client.d.ts +102 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1502 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +190 -0
- package/dist/config.js.map +1 -0
- package/dist/content_capture.d.ts +35 -0
- package/dist/content_capture.d.ts.map +1 -0
- package/dist/content_capture.js +123 -0
- package/dist/content_capture.js.map +1 -0
- package/dist/context.d.ts +11 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +53 -0
- package/dist/context.js.map +1 -0
- package/dist/exporters/default.d.ts +3 -0
- package/dist/exporters/default.d.ts.map +1 -0
- package/dist/exporters/default.js +34 -0
- package/dist/exporters/default.js.map +1 -0
- package/dist/exporters/grpc.d.ts +14 -0
- package/dist/exporters/grpc.d.ts.map +1 -0
- package/dist/exporters/grpc.js +369 -0
- package/dist/exporters/grpc.js.map +1 -0
- package/dist/exporters/http.d.ts +8 -0
- package/dist/exporters/http.d.ts.map +1 -0
- package/dist/exporters/http.js +258 -0
- package/dist/exporters/http.js.map +1 -0
- package/dist/frameworks/google-adk/index.d.ts +136 -0
- package/dist/frameworks/google-adk/index.d.ts.map +1 -0
- package/dist/frameworks/google-adk/index.js +509 -0
- package/dist/frameworks/google-adk/index.js.map +1 -0
- package/dist/frameworks/langchain/index.d.ts +29 -0
- package/dist/frameworks/langchain/index.d.ts.map +1 -0
- package/dist/frameworks/langchain/index.js +70 -0
- package/dist/frameworks/langchain/index.js.map +1 -0
- package/dist/frameworks/langgraph/index.d.ts +29 -0
- package/dist/frameworks/langgraph/index.d.ts.map +1 -0
- package/dist/frameworks/langgraph/index.js +70 -0
- package/dist/frameworks/langgraph/index.js.map +1 -0
- package/dist/frameworks/llamaindex/index.d.ts +43 -0
- package/dist/frameworks/llamaindex/index.d.ts.map +1 -0
- package/dist/frameworks/llamaindex/index.js +493 -0
- package/dist/frameworks/llamaindex/index.js.map +1 -0
- package/dist/frameworks/openai-agents/index.d.ts +33 -0
- package/dist/frameworks/openai-agents/index.d.ts.map +1 -0
- package/dist/frameworks/openai-agents/index.js +531 -0
- package/dist/frameworks/openai-agents/index.js.map +1 -0
- package/dist/frameworks/shared.d.ts +57 -0
- package/dist/frameworks/shared.d.ts.map +1 -0
- package/dist/frameworks/shared.js +1032 -0
- package/dist/frameworks/shared.js.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/hooks.d.ts +18 -0
- package/dist/frameworks/vercel-ai-sdk/hooks.d.ts.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/hooks.js +672 -0
- package/dist/frameworks/vercel-ai-sdk/hooks.js.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/index.d.ts +8 -0
- package/dist/frameworks/vercel-ai-sdk/index.d.ts.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/index.js +7 -0
- package/dist/frameworks/vercel-ai-sdk/index.js.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/mapping.d.ts +49 -0
- package/dist/frameworks/vercel-ai-sdk/mapping.d.ts.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/mapping.js +660 -0
- package/dist/frameworks/vercel-ai-sdk/mapping.js.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/types.d.ts +126 -0
- package/dist/frameworks/vercel-ai-sdk/types.d.ts.map +1 -0
- package/dist/frameworks/vercel-ai-sdk/types.js +2 -0
- package/dist/frameworks/vercel-ai-sdk/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/anthropic.d.ts +35 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +581 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/gemini.d.ts +42 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +650 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/openai.d.ts +67 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +1007 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/types.d.ts +461 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +25 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +331 -0
- package/dist/utils.js.map +1 -0
- package/docs/frameworks/google-adk.md +84 -0
- package/docs/frameworks/langchain.md +102 -0
- package/docs/frameworks/langgraph.md +137 -0
- package/docs/frameworks/llamaindex.md +82 -0
- package/docs/frameworks/openai-agents.md +88 -0
- package/docs/frameworks/vercel-ai-sdk.md +124 -0
- package/docs/index.md +22 -0
- package/docs/providers/anthropic.md +69 -0
- package/docs/providers/gemini.md +86 -0
- package/docs/providers/openai.md +151 -0
- package/package.json +80 -0
- package/proto/opentelemetry/proto/collector/trace/v1/trace_service.proto +77 -0
- package/proto/opentelemetry/proto/common/v1/common.proto +115 -0
- package/proto/opentelemetry/proto/resource/v1/resource.proto +44 -0
- package/proto/opentelemetry/proto/trace/v1/trace.proto +357 -0
- package/proto/sigil/v1/generation_ingest.proto +145 -0
|
@@ -0,0 +1,1007 @@
|
|
|
1
|
+
const thinkingBudgetMetadataKey = 'sigil.gen_ai.request.thinking.budget_tokens';
|
|
2
|
+
async function chatCompletionsCreate(client, request, providerCall, options = {}) {
|
|
3
|
+
const requestMessages = mapChatRequestMessages(request);
|
|
4
|
+
const mappedTools = mapChatTools(request);
|
|
5
|
+
const maxTokens = readIntFromAny(request.max_completion_tokens) ?? readIntFromAny(request.max_tokens);
|
|
6
|
+
const temperature = readNumberFromAny(request.temperature);
|
|
7
|
+
const topP = readNumberFromAny(request.top_p);
|
|
8
|
+
const toolChoice = canonicalToolChoice(request.tool_choice);
|
|
9
|
+
const thinkingBudget = openAIThinkingBudget(request.reasoning);
|
|
10
|
+
return client.startGeneration({
|
|
11
|
+
conversationId: options.conversationId,
|
|
12
|
+
agentName: options.agentName,
|
|
13
|
+
agentVersion: options.agentVersion,
|
|
14
|
+
model: {
|
|
15
|
+
provider: 'openai',
|
|
16
|
+
name: String(request.model ?? ''),
|
|
17
|
+
},
|
|
18
|
+
systemPrompt: requestMessages.systemPrompt,
|
|
19
|
+
maxTokens,
|
|
20
|
+
temperature,
|
|
21
|
+
topP,
|
|
22
|
+
toolChoice,
|
|
23
|
+
thinkingEnabled: hasValue(request.reasoning) ? true : undefined,
|
|
24
|
+
tools: mappedTools,
|
|
25
|
+
tags: options.tags,
|
|
26
|
+
metadata: metadataWithThinkingBudget(options.metadata, thinkingBudget),
|
|
27
|
+
}, async (recorder) => {
|
|
28
|
+
const response = await providerCall(request);
|
|
29
|
+
recorder.setResult(chatCompletionsFromRequestResponse(request, response, options));
|
|
30
|
+
return response;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async function chatCompletionsStream(client, request, providerCall, options = {}) {
|
|
34
|
+
const requestMessages = mapChatRequestMessages(request);
|
|
35
|
+
const mappedTools = mapChatTools(request);
|
|
36
|
+
const maxTokens = readIntFromAny(request.max_completion_tokens) ?? readIntFromAny(request.max_tokens);
|
|
37
|
+
const temperature = readNumberFromAny(request.temperature);
|
|
38
|
+
const topP = readNumberFromAny(request.top_p);
|
|
39
|
+
const toolChoice = canonicalToolChoice(request.tool_choice);
|
|
40
|
+
const thinkingBudget = openAIThinkingBudget(request.reasoning);
|
|
41
|
+
return client.startStreamingGeneration({
|
|
42
|
+
conversationId: options.conversationId,
|
|
43
|
+
agentName: options.agentName,
|
|
44
|
+
agentVersion: options.agentVersion,
|
|
45
|
+
model: {
|
|
46
|
+
provider: 'openai',
|
|
47
|
+
name: String(request.model ?? ''),
|
|
48
|
+
},
|
|
49
|
+
systemPrompt: requestMessages.systemPrompt,
|
|
50
|
+
maxTokens,
|
|
51
|
+
temperature,
|
|
52
|
+
topP,
|
|
53
|
+
toolChoice,
|
|
54
|
+
thinkingEnabled: hasValue(request.reasoning) ? true : undefined,
|
|
55
|
+
tools: mappedTools,
|
|
56
|
+
tags: options.tags,
|
|
57
|
+
metadata: metadataWithThinkingBudget(options.metadata, thinkingBudget),
|
|
58
|
+
}, async (recorder) => {
|
|
59
|
+
const summary = await providerCall(request);
|
|
60
|
+
const firstChunkAt = asDate(summary.firstChunkAt);
|
|
61
|
+
if (firstChunkAt !== undefined) {
|
|
62
|
+
recorder.setFirstTokenAt(firstChunkAt);
|
|
63
|
+
}
|
|
64
|
+
recorder.setResult(chatCompletionsFromStream(request, summary, options));
|
|
65
|
+
return summary;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function chatCompletionsFromRequestResponse(request, response, options = {}) {
|
|
69
|
+
const requestMessages = mapChatRequestMessages(request);
|
|
70
|
+
const mappedTools = mapChatTools(request);
|
|
71
|
+
const maxTokens = readIntFromAny(request.max_completion_tokens) ?? readIntFromAny(request.max_tokens);
|
|
72
|
+
const temperature = readNumberFromAny(request.temperature);
|
|
73
|
+
const topP = readNumberFromAny(request.top_p);
|
|
74
|
+
const toolChoice = canonicalToolChoice(request.tool_choice);
|
|
75
|
+
const thinkingBudget = openAIThinkingBudget(request.reasoning);
|
|
76
|
+
const result = {
|
|
77
|
+
responseId: response.id,
|
|
78
|
+
responseModel: response.model ?? String(request.model ?? ''),
|
|
79
|
+
maxTokens,
|
|
80
|
+
temperature,
|
|
81
|
+
topP,
|
|
82
|
+
toolChoice,
|
|
83
|
+
thinkingEnabled: hasValue(request.reasoning) ? true : undefined,
|
|
84
|
+
input: requestMessages.input,
|
|
85
|
+
output: mapChatResponseOutput(response),
|
|
86
|
+
tools: mappedTools,
|
|
87
|
+
usage: mapChatUsage(response.usage),
|
|
88
|
+
stopReason: normalizeChatStopReason(firstFinishReason(response)),
|
|
89
|
+
metadata: metadataWithThinkingBudget(options.metadata, thinkingBudget),
|
|
90
|
+
tags: options.tags ? { ...options.tags } : undefined,
|
|
91
|
+
};
|
|
92
|
+
if (options.rawArtifacts) {
|
|
93
|
+
result.artifacts = [
|
|
94
|
+
jsonArtifact('request', 'openai.chat.request', request),
|
|
95
|
+
jsonArtifact('response', 'openai.chat.response', response),
|
|
96
|
+
];
|
|
97
|
+
if (mappedTools.length > 0) {
|
|
98
|
+
result.artifacts.push(jsonArtifact('tools', 'openai.chat.tools', mappedTools));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
function chatCompletionsFromStream(request, summary, options = {}) {
|
|
104
|
+
const requestMessages = mapChatRequestMessages(request);
|
|
105
|
+
const mappedTools = mapChatTools(request);
|
|
106
|
+
const maxTokens = readIntFromAny(request.max_completion_tokens) ?? readIntFromAny(request.max_tokens);
|
|
107
|
+
const temperature = readNumberFromAny(request.temperature);
|
|
108
|
+
const topP = readNumberFromAny(request.top_p);
|
|
109
|
+
const toolChoice = canonicalToolChoice(request.tool_choice);
|
|
110
|
+
const thinkingBudget = openAIThinkingBudget(request.reasoning);
|
|
111
|
+
const outputText = summary.outputText ?? extractChatStreamText(summary.events ?? []);
|
|
112
|
+
const fallbackOutput = outputText.length > 0 ? [{ role: 'assistant', content: outputText }] : [];
|
|
113
|
+
const result = summary.finalResponse
|
|
114
|
+
? {
|
|
115
|
+
...chatCompletionsFromRequestResponse(request, summary.finalResponse, options),
|
|
116
|
+
output: mapChatResponseOutput(summary.finalResponse).length > 0
|
|
117
|
+
? mapChatResponseOutput(summary.finalResponse)
|
|
118
|
+
: fallbackOutput,
|
|
119
|
+
}
|
|
120
|
+
: {
|
|
121
|
+
responseModel: String(request.model ?? ''),
|
|
122
|
+
maxTokens,
|
|
123
|
+
temperature,
|
|
124
|
+
topP,
|
|
125
|
+
toolChoice,
|
|
126
|
+
thinkingEnabled: hasValue(request.reasoning) ? true : undefined,
|
|
127
|
+
input: requestMessages.input,
|
|
128
|
+
output: fallbackOutput,
|
|
129
|
+
tools: mappedTools,
|
|
130
|
+
metadata: metadataWithThinkingBudget(options.metadata, thinkingBudget),
|
|
131
|
+
tags: options.tags ? { ...options.tags } : undefined,
|
|
132
|
+
};
|
|
133
|
+
if (options.rawArtifacts) {
|
|
134
|
+
const existing = result.artifacts ?? [];
|
|
135
|
+
if (!existing.some((artifact) => artifact.type === 'request')) {
|
|
136
|
+
existing.push(jsonArtifact('request', 'openai.chat.request', request));
|
|
137
|
+
}
|
|
138
|
+
if (mappedTools.length > 0 && !existing.some((artifact) => artifact.type === 'tools')) {
|
|
139
|
+
existing.push(jsonArtifact('tools', 'openai.chat.tools', mappedTools));
|
|
140
|
+
}
|
|
141
|
+
existing.push(jsonArtifact('provider_event', 'openai.chat.stream_events', summary.events ?? []));
|
|
142
|
+
result.artifacts = existing;
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
async function responsesCreate(client, request, providerCall, options = {}) {
|
|
147
|
+
const requestPayload = mapResponsesRequest(request);
|
|
148
|
+
const controls = mapResponsesRequestControls(request);
|
|
149
|
+
return client.startGeneration({
|
|
150
|
+
conversationId: options.conversationId,
|
|
151
|
+
agentName: options.agentName,
|
|
152
|
+
agentVersion: options.agentVersion,
|
|
153
|
+
model: {
|
|
154
|
+
provider: 'openai',
|
|
155
|
+
name: String(request.model ?? ''),
|
|
156
|
+
},
|
|
157
|
+
systemPrompt: requestPayload.systemPrompt,
|
|
158
|
+
maxTokens: controls.maxTokens,
|
|
159
|
+
temperature: controls.temperature,
|
|
160
|
+
topP: controls.topP,
|
|
161
|
+
toolChoice: controls.toolChoice,
|
|
162
|
+
thinkingEnabled: controls.thinkingEnabled,
|
|
163
|
+
tools: requestPayload.tools,
|
|
164
|
+
tags: options.tags,
|
|
165
|
+
metadata: metadataWithThinkingBudget(options.metadata, controls.thinkingBudget),
|
|
166
|
+
}, async (recorder) => {
|
|
167
|
+
const response = await providerCall(request);
|
|
168
|
+
recorder.setResult(responsesFromRequestResponse(request, response, options));
|
|
169
|
+
return response;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
async function responsesStream(client, request, providerCall, options = {}) {
|
|
173
|
+
const requestPayload = mapResponsesRequest(request);
|
|
174
|
+
const controls = mapResponsesRequestControls(request);
|
|
175
|
+
return client.startStreamingGeneration({
|
|
176
|
+
conversationId: options.conversationId,
|
|
177
|
+
agentName: options.agentName,
|
|
178
|
+
agentVersion: options.agentVersion,
|
|
179
|
+
model: {
|
|
180
|
+
provider: 'openai',
|
|
181
|
+
name: String(request.model ?? ''),
|
|
182
|
+
},
|
|
183
|
+
systemPrompt: requestPayload.systemPrompt,
|
|
184
|
+
maxTokens: controls.maxTokens,
|
|
185
|
+
temperature: controls.temperature,
|
|
186
|
+
topP: controls.topP,
|
|
187
|
+
toolChoice: controls.toolChoice,
|
|
188
|
+
thinkingEnabled: controls.thinkingEnabled,
|
|
189
|
+
tools: requestPayload.tools,
|
|
190
|
+
tags: options.tags,
|
|
191
|
+
metadata: metadataWithThinkingBudget(options.metadata, controls.thinkingBudget),
|
|
192
|
+
}, async (recorder) => {
|
|
193
|
+
const summary = await providerCall(request);
|
|
194
|
+
const firstChunkAt = asDate(summary.firstChunkAt);
|
|
195
|
+
if (firstChunkAt !== undefined) {
|
|
196
|
+
recorder.setFirstTokenAt(firstChunkAt);
|
|
197
|
+
}
|
|
198
|
+
recorder.setResult(responsesFromStream(request, summary, options));
|
|
199
|
+
return summary;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
function responsesFromRequestResponse(request, response, options = {}) {
|
|
203
|
+
const requestPayload = mapResponsesRequest(request);
|
|
204
|
+
const controls = mapResponsesRequestControls(request);
|
|
205
|
+
const result = {
|
|
206
|
+
responseId: response.id,
|
|
207
|
+
responseModel: response.model ?? String(request.model ?? ''),
|
|
208
|
+
maxTokens: controls.maxTokens,
|
|
209
|
+
temperature: controls.temperature,
|
|
210
|
+
topP: controls.topP,
|
|
211
|
+
toolChoice: controls.toolChoice,
|
|
212
|
+
thinkingEnabled: controls.thinkingEnabled,
|
|
213
|
+
input: requestPayload.input,
|
|
214
|
+
output: mapResponsesOutputItems(response.output),
|
|
215
|
+
tools: requestPayload.tools,
|
|
216
|
+
usage: mapResponsesUsage(response.usage),
|
|
217
|
+
stopReason: normalizeResponsesStopReason(response),
|
|
218
|
+
metadata: metadataWithThinkingBudget(options.metadata, controls.thinkingBudget),
|
|
219
|
+
tags: options.tags ? { ...options.tags } : undefined,
|
|
220
|
+
};
|
|
221
|
+
if (options.rawArtifacts) {
|
|
222
|
+
result.artifacts = [
|
|
223
|
+
jsonArtifact('request', 'openai.responses.request', request),
|
|
224
|
+
jsonArtifact('response', 'openai.responses.response', response),
|
|
225
|
+
];
|
|
226
|
+
if (requestPayload.tools.length > 0) {
|
|
227
|
+
result.artifacts.push(jsonArtifact('tools', 'openai.responses.tools', requestPayload.tools));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
function responsesFromStream(request, summary, options = {}) {
|
|
233
|
+
const requestPayload = mapResponsesRequest(request);
|
|
234
|
+
const controls = mapResponsesRequestControls(request);
|
|
235
|
+
const events = summary.events ?? [];
|
|
236
|
+
const finalFromEvents = findResponsesFinalFromEvents(events);
|
|
237
|
+
const finalResponse = summary.finalResponse ?? finalFromEvents;
|
|
238
|
+
const outputText = summary.outputText ?? extractResponsesStreamText(events);
|
|
239
|
+
const result = finalResponse
|
|
240
|
+
? {
|
|
241
|
+
...responsesFromRequestResponse(request, finalResponse, options),
|
|
242
|
+
output: mapResponsesOutputItems(finalResponse.output).length > 0
|
|
243
|
+
? mapResponsesOutputItems(finalResponse.output)
|
|
244
|
+
: outputText.length > 0
|
|
245
|
+
? [{ role: 'assistant', content: outputText }]
|
|
246
|
+
: [],
|
|
247
|
+
}
|
|
248
|
+
: {
|
|
249
|
+
responseModel: String(request.model ?? ''),
|
|
250
|
+
maxTokens: controls.maxTokens,
|
|
251
|
+
temperature: controls.temperature,
|
|
252
|
+
topP: controls.topP,
|
|
253
|
+
toolChoice: controls.toolChoice,
|
|
254
|
+
thinkingEnabled: controls.thinkingEnabled,
|
|
255
|
+
input: requestPayload.input,
|
|
256
|
+
output: outputText.length > 0 ? [{ role: 'assistant', content: outputText }] : [],
|
|
257
|
+
tools: requestPayload.tools,
|
|
258
|
+
stopReason: normalizeResponsesStopReasonFromEvents(events),
|
|
259
|
+
metadata: metadataWithThinkingBudget(options.metadata, controls.thinkingBudget),
|
|
260
|
+
tags: options.tags ? { ...options.tags } : undefined,
|
|
261
|
+
};
|
|
262
|
+
if (options.rawArtifacts) {
|
|
263
|
+
const existing = result.artifacts ?? [];
|
|
264
|
+
if (!existing.some((artifact) => artifact.type === 'request')) {
|
|
265
|
+
existing.push(jsonArtifact('request', 'openai.responses.request', request));
|
|
266
|
+
}
|
|
267
|
+
if (requestPayload.tools.length > 0 && !existing.some((artifact) => artifact.type === 'tools')) {
|
|
268
|
+
existing.push(jsonArtifact('tools', 'openai.responses.tools', requestPayload.tools));
|
|
269
|
+
}
|
|
270
|
+
existing.push(jsonArtifact('provider_event', 'openai.responses.stream_events', events));
|
|
271
|
+
result.artifacts = existing;
|
|
272
|
+
}
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
async function embeddingsCreate(client, request, providerCall, options = {}) {
|
|
276
|
+
const dimensions = readIntFromAny(request.dimensions);
|
|
277
|
+
const rawEncodingFormat = request.encoding_format;
|
|
278
|
+
const encodingFormat = typeof rawEncodingFormat === 'string' ? rawEncodingFormat.trim() : '';
|
|
279
|
+
return client.startEmbedding({
|
|
280
|
+
agentName: options.agentName,
|
|
281
|
+
agentVersion: options.agentVersion,
|
|
282
|
+
model: {
|
|
283
|
+
provider: 'openai',
|
|
284
|
+
name: String(request.model ?? ''),
|
|
285
|
+
},
|
|
286
|
+
dimensions,
|
|
287
|
+
encodingFormat: encodingFormat.length > 0 ? encodingFormat : undefined,
|
|
288
|
+
tags: options.tags,
|
|
289
|
+
metadata: options.metadata,
|
|
290
|
+
}, async (recorder) => {
|
|
291
|
+
const response = await providerCall(request);
|
|
292
|
+
recorder.setResult(embeddingsFromRequestResponse(request, response));
|
|
293
|
+
return response;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
function embeddingsFromRequestResponse(request, response) {
|
|
297
|
+
const input = request.input;
|
|
298
|
+
const result = {
|
|
299
|
+
inputCount: embeddingInputCount(input),
|
|
300
|
+
inputTexts: embeddingInputTexts(input),
|
|
301
|
+
};
|
|
302
|
+
if (!isRecord(response)) {
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
const usage = isRecord(response.usage) ? response.usage : undefined;
|
|
306
|
+
result.inputTokens = readIntFromAny(usage?.prompt_tokens) ?? readIntFromAny(usage?.total_tokens);
|
|
307
|
+
result.responseModel = typeof response.model === 'string' ? response.model : undefined;
|
|
308
|
+
const data = Array.isArray(response.data) ? response.data : [];
|
|
309
|
+
if (data.length > 0 && isRecord(data[0]) && Array.isArray(data[0].embedding)) {
|
|
310
|
+
result.dimensions = data[0].embedding.length;
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
export const chat = {
|
|
315
|
+
completions: {
|
|
316
|
+
create: chatCompletionsCreate,
|
|
317
|
+
stream: chatCompletionsStream,
|
|
318
|
+
fromRequestResponse: chatCompletionsFromRequestResponse,
|
|
319
|
+
fromStream: chatCompletionsFromStream,
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
export const responses = {
|
|
323
|
+
create: responsesCreate,
|
|
324
|
+
stream: responsesStream,
|
|
325
|
+
fromRequestResponse: responsesFromRequestResponse,
|
|
326
|
+
fromStream: responsesFromStream,
|
|
327
|
+
};
|
|
328
|
+
export const embeddings = {
|
|
329
|
+
create: embeddingsCreate,
|
|
330
|
+
fromRequestResponse: embeddingsFromRequestResponse,
|
|
331
|
+
};
|
|
332
|
+
function mapChatRequestMessages(request) {
|
|
333
|
+
const source = Array.isArray(request.messages) ? request.messages : [];
|
|
334
|
+
const systemChunks = [];
|
|
335
|
+
const input = [];
|
|
336
|
+
for (const rawMessage of source) {
|
|
337
|
+
if (!isRecord(rawMessage)) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const role = String(rawMessage.role ?? '')
|
|
341
|
+
.trim()
|
|
342
|
+
.toLowerCase();
|
|
343
|
+
const content = extractText(rawMessage.content);
|
|
344
|
+
if (role === 'system' || role === 'developer') {
|
|
345
|
+
if (content.length > 0) {
|
|
346
|
+
systemChunks.push(content);
|
|
347
|
+
}
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const normalizedRole = role === 'assistant' || role === 'tool' ? role : 'user';
|
|
351
|
+
const message = { role: normalizedRole };
|
|
352
|
+
if (normalizedRole !== 'tool' && content.length > 0) {
|
|
353
|
+
message.content = content;
|
|
354
|
+
}
|
|
355
|
+
if (typeof rawMessage.name === 'string' && rawMessage.name.trim().length > 0) {
|
|
356
|
+
message.name = rawMessage.name;
|
|
357
|
+
}
|
|
358
|
+
if (normalizedRole === 'tool') {
|
|
359
|
+
const toolResult = mapToolResultMessage(rawMessage.content, rawMessage.tool_call_id ?? rawMessage.toolCallId ?? rawMessage.id, rawMessage.name, rawMessage.is_error, 'tool_result');
|
|
360
|
+
if (toolResult) {
|
|
361
|
+
input.push(toolResult);
|
|
362
|
+
}
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (normalizedRole === 'assistant' && Array.isArray(rawMessage.tool_calls)) {
|
|
366
|
+
const parts = mapChatToolCallParts(rawMessage.tool_calls);
|
|
367
|
+
if (parts.length > 0) {
|
|
368
|
+
message.parts = message.parts ? [...message.parts, ...parts] : parts;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
input.push(message);
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
input,
|
|
375
|
+
systemPrompt: systemChunks.length > 0 ? systemChunks.join('\n\n') : undefined,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
function mapChatResponseOutput(response) {
|
|
379
|
+
const choice = response.choices?.[0];
|
|
380
|
+
if (!choice) {
|
|
381
|
+
return [];
|
|
382
|
+
}
|
|
383
|
+
const messageRecord = isRecord(choice.message) ? choice.message : undefined;
|
|
384
|
+
if (!messageRecord) {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
const textChunks = [];
|
|
388
|
+
const rawContent = messageRecord.content;
|
|
389
|
+
const contentText = extractText(rawContent);
|
|
390
|
+
if (contentText.length > 0) {
|
|
391
|
+
textChunks.push(contentText);
|
|
392
|
+
}
|
|
393
|
+
if (typeof messageRecord.refusal === 'string' && messageRecord.refusal.trim().length > 0) {
|
|
394
|
+
textChunks.push(messageRecord.refusal.trim());
|
|
395
|
+
}
|
|
396
|
+
const parts = mapChatToolCallParts(messageRecord.tool_calls);
|
|
397
|
+
if (textChunks.length === 0 && parts.length === 0) {
|
|
398
|
+
return [];
|
|
399
|
+
}
|
|
400
|
+
const output = {
|
|
401
|
+
role: 'assistant',
|
|
402
|
+
};
|
|
403
|
+
if (textChunks.length > 0) {
|
|
404
|
+
output.content = textChunks.join('\n');
|
|
405
|
+
}
|
|
406
|
+
if (parts.length > 0) {
|
|
407
|
+
output.parts = parts;
|
|
408
|
+
if (!output.content) {
|
|
409
|
+
output.content = parts
|
|
410
|
+
.map((part) => (part.type === 'tool_call' ? `${part.toolCall.name}(${part.toolCall.inputJSON ?? ''})` : ''))
|
|
411
|
+
.filter((chunk) => chunk.length > 0)
|
|
412
|
+
.join('\n');
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return [output];
|
|
416
|
+
}
|
|
417
|
+
function mapChatToolCallParts(value) {
|
|
418
|
+
if (!Array.isArray(value)) {
|
|
419
|
+
return [];
|
|
420
|
+
}
|
|
421
|
+
const parts = [];
|
|
422
|
+
for (const rawToolCall of value) {
|
|
423
|
+
if (!isRecord(rawToolCall)) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
const functionCall = isRecord(rawToolCall.function) ? rawToolCall.function : undefined;
|
|
427
|
+
if (!functionCall) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
const name = typeof functionCall.name === 'string' ? functionCall.name : '';
|
|
431
|
+
if (name.trim().length === 0) {
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
const input = typeof functionCall.arguments === 'string' ? functionCall.arguments : jsonString(functionCall.arguments);
|
|
435
|
+
parts.push({
|
|
436
|
+
type: 'tool_call',
|
|
437
|
+
toolCall: {
|
|
438
|
+
id: typeof rawToolCall.id === 'string' ? rawToolCall.id : undefined,
|
|
439
|
+
name,
|
|
440
|
+
inputJSON: input,
|
|
441
|
+
},
|
|
442
|
+
metadata: { providerType: 'tool_call' },
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
return parts;
|
|
446
|
+
}
|
|
447
|
+
function mapChatTools(request) {
|
|
448
|
+
const value = request.tools;
|
|
449
|
+
if (!Array.isArray(value)) {
|
|
450
|
+
return [];
|
|
451
|
+
}
|
|
452
|
+
const out = [];
|
|
453
|
+
for (const rawTool of value) {
|
|
454
|
+
if (!isRecord(rawTool)) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const rawType = typeof rawTool.type === 'string' ? rawTool.type : '';
|
|
458
|
+
if (rawType === 'function' && isRecord(rawTool.function)) {
|
|
459
|
+
const name = typeof rawTool.function.name === 'string' ? rawTool.function.name : '';
|
|
460
|
+
if (name.trim().length === 0) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
out.push({
|
|
464
|
+
name,
|
|
465
|
+
description: typeof rawTool.function.description === 'string' ? rawTool.function.description : undefined,
|
|
466
|
+
type: 'function',
|
|
467
|
+
inputSchemaJSON: hasValue(rawTool.function.parameters) ? jsonString(rawTool.function.parameters) : undefined,
|
|
468
|
+
});
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
if (rawType.length > 0 && typeof rawTool.name === 'string' && rawTool.name.trim().length > 0) {
|
|
472
|
+
out.push({
|
|
473
|
+
name: rawTool.name,
|
|
474
|
+
type: rawType,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
}
|
|
480
|
+
function mapChatUsage(usage) {
|
|
481
|
+
if (!isRecord(usage)) {
|
|
482
|
+
return undefined;
|
|
483
|
+
}
|
|
484
|
+
const inputTokens = readIntFromAny(usage.prompt_tokens);
|
|
485
|
+
const outputTokens = readIntFromAny(usage.completion_tokens);
|
|
486
|
+
const totalTokens = readIntFromAny(usage.total_tokens);
|
|
487
|
+
const cacheReadInputTokens = isRecord(usage.prompt_tokens_details)
|
|
488
|
+
? readIntFromAny(usage.prompt_tokens_details.cached_tokens)
|
|
489
|
+
: undefined;
|
|
490
|
+
const reasoningTokens = isRecord(usage.completion_tokens_details)
|
|
491
|
+
? readIntFromAny(usage.completion_tokens_details.reasoning_tokens)
|
|
492
|
+
: undefined;
|
|
493
|
+
const out = {};
|
|
494
|
+
if (inputTokens !== undefined) {
|
|
495
|
+
out.inputTokens = inputTokens;
|
|
496
|
+
}
|
|
497
|
+
if (outputTokens !== undefined) {
|
|
498
|
+
out.outputTokens = outputTokens;
|
|
499
|
+
}
|
|
500
|
+
if (totalTokens !== undefined) {
|
|
501
|
+
out.totalTokens = totalTokens;
|
|
502
|
+
}
|
|
503
|
+
if (cacheReadInputTokens !== undefined) {
|
|
504
|
+
out.cacheReadInputTokens = cacheReadInputTokens;
|
|
505
|
+
}
|
|
506
|
+
if (reasoningTokens !== undefined) {
|
|
507
|
+
out.reasoningTokens = reasoningTokens;
|
|
508
|
+
}
|
|
509
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
510
|
+
}
|
|
511
|
+
function firstFinishReason(response) {
|
|
512
|
+
for (const choice of response.choices ?? []) {
|
|
513
|
+
if (typeof choice.finish_reason === 'string' && choice.finish_reason.trim().length > 0) {
|
|
514
|
+
return choice.finish_reason;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return undefined;
|
|
518
|
+
}
|
|
519
|
+
function normalizeChatStopReason(value) {
|
|
520
|
+
if (!value) {
|
|
521
|
+
return undefined;
|
|
522
|
+
}
|
|
523
|
+
return value;
|
|
524
|
+
}
|
|
525
|
+
function extractChatStreamText(events) {
|
|
526
|
+
const chunks = [];
|
|
527
|
+
for (const event of events) {
|
|
528
|
+
for (const choice of event.choices ?? []) {
|
|
529
|
+
if (typeof choice.delta?.content === 'string' && choice.delta.content.length > 0) {
|
|
530
|
+
chunks.push(choice.delta.content);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return chunks.join('');
|
|
535
|
+
}
|
|
536
|
+
function mapResponsesRequest(request) {
|
|
537
|
+
const input = [];
|
|
538
|
+
const systemChunks = [];
|
|
539
|
+
const instructions = extractText(request.instructions);
|
|
540
|
+
if (instructions.length > 0) {
|
|
541
|
+
systemChunks.push(instructions);
|
|
542
|
+
}
|
|
543
|
+
const rawInput = request.input;
|
|
544
|
+
if (typeof rawInput === 'string') {
|
|
545
|
+
input.push({
|
|
546
|
+
role: 'user',
|
|
547
|
+
content: rawInput,
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
else if (Array.isArray(rawInput)) {
|
|
551
|
+
for (const rawItem of rawInput) {
|
|
552
|
+
if (!isRecord(rawItem)) {
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const role = typeof rawItem.role === 'string' ? rawItem.role.trim().toLowerCase() : '';
|
|
556
|
+
const itemType = typeof rawItem.type === 'string' ? rawItem.type.trim().toLowerCase() : '';
|
|
557
|
+
if ((role === 'system' || role === 'developer') && itemType === 'message') {
|
|
558
|
+
const content = extractText(rawItem.content);
|
|
559
|
+
if (content.length > 0) {
|
|
560
|
+
systemChunks.push(content);
|
|
561
|
+
}
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
if (itemType === 'function_call_output') {
|
|
565
|
+
const toolResult = mapToolResultMessage(rawItem.output, rawItem.call_id ?? rawItem.callId, rawItem.name, rawItem.is_error, 'tool_result');
|
|
566
|
+
if (toolResult) {
|
|
567
|
+
input.push(toolResult);
|
|
568
|
+
}
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
if (itemType === 'message' || role.length > 0) {
|
|
572
|
+
const content = extractText(rawItem.content);
|
|
573
|
+
if (content.length === 0) {
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
const mappedRole = role === 'assistant' || role === 'tool' ? role : 'user';
|
|
577
|
+
input.push({ role: mappedRole, content });
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
const tools = mapResponsesTools(request.tools);
|
|
582
|
+
return {
|
|
583
|
+
input,
|
|
584
|
+
systemPrompt: systemChunks.length > 0 ? systemChunks.join('\n\n') : undefined,
|
|
585
|
+
tools,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
function mapResponsesRequestControls(request) {
|
|
589
|
+
const maxTokens = readIntFromAny(request.max_output_tokens);
|
|
590
|
+
const temperature = readNumberFromAny(request.temperature);
|
|
591
|
+
const topP = readNumberFromAny(request.top_p);
|
|
592
|
+
const toolChoice = canonicalToolChoice(request.tool_choice);
|
|
593
|
+
const reasoning = request.reasoning;
|
|
594
|
+
return {
|
|
595
|
+
maxTokens,
|
|
596
|
+
temperature,
|
|
597
|
+
topP,
|
|
598
|
+
toolChoice,
|
|
599
|
+
thinkingEnabled: hasValue(reasoning) ? true : undefined,
|
|
600
|
+
thinkingBudget: openAIThinkingBudget(reasoning),
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
function mapResponsesTools(value) {
|
|
604
|
+
if (!Array.isArray(value)) {
|
|
605
|
+
return [];
|
|
606
|
+
}
|
|
607
|
+
const out = [];
|
|
608
|
+
for (const rawTool of value) {
|
|
609
|
+
if (!isRecord(rawTool)) {
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
const toolType = typeof rawTool.type === 'string' ? rawTool.type : '';
|
|
613
|
+
if (toolType === 'function') {
|
|
614
|
+
const name = typeof rawTool.name === 'string' ? rawTool.name : '';
|
|
615
|
+
if (name.trim().length === 0) {
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
out.push({
|
|
619
|
+
name,
|
|
620
|
+
description: typeof rawTool.description === 'string' ? rawTool.description : undefined,
|
|
621
|
+
type: 'function',
|
|
622
|
+
inputSchemaJSON: hasValue(rawTool.parameters) ? jsonString(rawTool.parameters) : undefined,
|
|
623
|
+
});
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
if (toolType.length > 0 && typeof rawTool.name === 'string' && rawTool.name.trim().length > 0) {
|
|
627
|
+
out.push({
|
|
628
|
+
name: rawTool.name,
|
|
629
|
+
type: toolType,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return out;
|
|
634
|
+
}
|
|
635
|
+
function mapResponsesOutputItems(value) {
|
|
636
|
+
if (!Array.isArray(value)) {
|
|
637
|
+
return [];
|
|
638
|
+
}
|
|
639
|
+
const output = [];
|
|
640
|
+
for (const rawItem of value) {
|
|
641
|
+
if (!isRecord(rawItem)) {
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
const itemType = typeof rawItem.type === 'string' ? rawItem.type : '';
|
|
645
|
+
if (itemType === 'message') {
|
|
646
|
+
const content = extractText(rawItem.content);
|
|
647
|
+
if (content.length > 0) {
|
|
648
|
+
output.push({ role: 'assistant', content });
|
|
649
|
+
}
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
if (itemType === 'function_call') {
|
|
653
|
+
const name = typeof rawItem.name === 'string' ? rawItem.name : '';
|
|
654
|
+
const args = typeof rawItem.arguments === 'string' ? rawItem.arguments : jsonString(rawItem.arguments);
|
|
655
|
+
if (name.trim().length > 0) {
|
|
656
|
+
output.push({
|
|
657
|
+
role: 'assistant',
|
|
658
|
+
content: `${name}(${args})`,
|
|
659
|
+
parts: [
|
|
660
|
+
{
|
|
661
|
+
type: 'tool_call',
|
|
662
|
+
toolCall: {
|
|
663
|
+
id: typeof rawItem.call_id === 'string' ? rawItem.call_id : undefined,
|
|
664
|
+
name,
|
|
665
|
+
inputJSON: args,
|
|
666
|
+
},
|
|
667
|
+
metadata: { providerType: 'tool_call' },
|
|
668
|
+
},
|
|
669
|
+
],
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
if (itemType === 'function_call_output') {
|
|
675
|
+
const toolResult = mapToolResultMessage(rawItem.output, rawItem.call_id ?? rawItem.callId, rawItem.name, rawItem.is_error, 'tool_result');
|
|
676
|
+
if (toolResult) {
|
|
677
|
+
output.push(toolResult);
|
|
678
|
+
}
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (itemType.length > 0) {
|
|
682
|
+
const fallback = extractText(rawItem) || jsonString(rawItem);
|
|
683
|
+
if (fallback.length > 0) {
|
|
684
|
+
output.push({ role: 'assistant', content: fallback });
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return output;
|
|
689
|
+
}
|
|
690
|
+
function mapResponsesUsage(value) {
|
|
691
|
+
if (!isRecord(value)) {
|
|
692
|
+
return undefined;
|
|
693
|
+
}
|
|
694
|
+
const inputTokens = readIntFromAny(value.input_tokens);
|
|
695
|
+
const outputTokens = readIntFromAny(value.output_tokens);
|
|
696
|
+
const totalTokens = readIntFromAny(value.total_tokens);
|
|
697
|
+
const cacheReadInputTokens = isRecord(value.input_tokens_details)
|
|
698
|
+
? readIntFromAny(value.input_tokens_details.cached_tokens)
|
|
699
|
+
: undefined;
|
|
700
|
+
const reasoningTokens = isRecord(value.output_tokens_details)
|
|
701
|
+
? readIntFromAny(value.output_tokens_details.reasoning_tokens)
|
|
702
|
+
: undefined;
|
|
703
|
+
const out = {};
|
|
704
|
+
if (inputTokens !== undefined) {
|
|
705
|
+
out.inputTokens = inputTokens;
|
|
706
|
+
}
|
|
707
|
+
if (outputTokens !== undefined) {
|
|
708
|
+
out.outputTokens = outputTokens;
|
|
709
|
+
}
|
|
710
|
+
if (totalTokens !== undefined) {
|
|
711
|
+
out.totalTokens = totalTokens;
|
|
712
|
+
}
|
|
713
|
+
if (cacheReadInputTokens !== undefined) {
|
|
714
|
+
out.cacheReadInputTokens = cacheReadInputTokens;
|
|
715
|
+
}
|
|
716
|
+
if (reasoningTokens !== undefined) {
|
|
717
|
+
out.reasoningTokens = reasoningTokens;
|
|
718
|
+
}
|
|
719
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
720
|
+
}
|
|
721
|
+
function normalizeResponsesStopReason(response) {
|
|
722
|
+
const status = typeof response.status === 'string' ? response.status.trim().toLowerCase() : '';
|
|
723
|
+
const incomplete = isRecord(response.incomplete_details)
|
|
724
|
+
? String(response.incomplete_details.reason ?? '')
|
|
725
|
+
.trim()
|
|
726
|
+
.toLowerCase()
|
|
727
|
+
: '';
|
|
728
|
+
if (status === 'incomplete' && incomplete.length > 0) {
|
|
729
|
+
return incomplete;
|
|
730
|
+
}
|
|
731
|
+
if (status === 'completed') {
|
|
732
|
+
return 'stop';
|
|
733
|
+
}
|
|
734
|
+
return status.length > 0 ? status : undefined;
|
|
735
|
+
}
|
|
736
|
+
function normalizeResponsesStopReasonFromEvents(events) {
|
|
737
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
738
|
+
const event = events[index];
|
|
739
|
+
const eventType = typeof event.type === 'string' ? event.type : '';
|
|
740
|
+
const response = isRecord(event.response) ? event.response : undefined;
|
|
741
|
+
if (eventType === 'response.incomplete' && response && isRecord(response.incomplete_details)) {
|
|
742
|
+
const reason = String(response.incomplete_details.reason ?? '')
|
|
743
|
+
.trim()
|
|
744
|
+
.toLowerCase();
|
|
745
|
+
if (reason.length > 0) {
|
|
746
|
+
return reason;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (eventType === 'response.completed') {
|
|
750
|
+
return 'stop';
|
|
751
|
+
}
|
|
752
|
+
if (eventType === 'response.failed') {
|
|
753
|
+
return 'failed';
|
|
754
|
+
}
|
|
755
|
+
if (eventType === 'response.cancelled') {
|
|
756
|
+
return 'cancelled';
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return undefined;
|
|
760
|
+
}
|
|
761
|
+
function extractResponsesStreamText(events) {
|
|
762
|
+
const chunks = [];
|
|
763
|
+
for (const event of events) {
|
|
764
|
+
const record = event;
|
|
765
|
+
const type = typeof record.type === 'string' ? record.type : '';
|
|
766
|
+
if (type === 'response.output_text.delta' && typeof record.delta === 'string') {
|
|
767
|
+
chunks.push(record.delta);
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
if (type === 'response.output_text.done' && typeof record.text === 'string' && chunks.length === 0) {
|
|
771
|
+
chunks.push(record.text);
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (type === 'response.refusal.delta' && typeof record.delta === 'string') {
|
|
775
|
+
chunks.push(record.delta);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
return chunks.join('');
|
|
779
|
+
}
|
|
780
|
+
function findResponsesFinalFromEvents(events) {
|
|
781
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
782
|
+
const event = events[index];
|
|
783
|
+
if ((event.type === 'response.completed' || event.type === 'response.incomplete') && isRecord(event.response)) {
|
|
784
|
+
return event.response;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return undefined;
|
|
788
|
+
}
|
|
789
|
+
function extractText(value) {
|
|
790
|
+
if (typeof value === 'string') {
|
|
791
|
+
return value.trim();
|
|
792
|
+
}
|
|
793
|
+
if (Array.isArray(value)) {
|
|
794
|
+
const chunks = [];
|
|
795
|
+
for (const item of value) {
|
|
796
|
+
if (typeof item === 'string') {
|
|
797
|
+
if (item.trim().length > 0) {
|
|
798
|
+
chunks.push(item.trim());
|
|
799
|
+
}
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
if (!isRecord(item)) {
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
const itemType = typeof item.type === 'string' ? item.type : '';
|
|
806
|
+
if ((itemType === 'text' || itemType === 'input_text' || itemType === 'output_text') &&
|
|
807
|
+
typeof item.text === 'string') {
|
|
808
|
+
if (item.text.trim().length > 0) {
|
|
809
|
+
chunks.push(item.text.trim());
|
|
810
|
+
}
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (itemType === 'refusal' && typeof item.refusal === 'string') {
|
|
814
|
+
if (item.refusal.trim().length > 0) {
|
|
815
|
+
chunks.push(item.refusal.trim());
|
|
816
|
+
}
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
if (typeof item.content === 'string' && item.content.trim().length > 0) {
|
|
820
|
+
chunks.push(item.content.trim());
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return chunks.join('\n');
|
|
824
|
+
}
|
|
825
|
+
if (isRecord(value)) {
|
|
826
|
+
if (typeof value.text === 'string' && value.text.trim().length > 0) {
|
|
827
|
+
return value.text.trim();
|
|
828
|
+
}
|
|
829
|
+
if (typeof value.content === 'string' && value.content.trim().length > 0) {
|
|
830
|
+
return value.content.trim();
|
|
831
|
+
}
|
|
832
|
+
if (typeof value.refusal === 'string' && value.refusal.trim().length > 0) {
|
|
833
|
+
return value.refusal.trim();
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
return '';
|
|
837
|
+
}
|
|
838
|
+
function embeddingInputCount(input) {
|
|
839
|
+
if (typeof input === 'string') {
|
|
840
|
+
return 1;
|
|
841
|
+
}
|
|
842
|
+
if (Array.isArray(input)) {
|
|
843
|
+
if (input.length === 0) {
|
|
844
|
+
return 0;
|
|
845
|
+
}
|
|
846
|
+
if (input.every((item) => typeof item === 'number' && Number.isFinite(item))) {
|
|
847
|
+
return 1;
|
|
848
|
+
}
|
|
849
|
+
return input.length;
|
|
850
|
+
}
|
|
851
|
+
if (isRecord(input)) {
|
|
852
|
+
return 1;
|
|
853
|
+
}
|
|
854
|
+
return 0;
|
|
855
|
+
}
|
|
856
|
+
function embeddingInputTexts(input) {
|
|
857
|
+
if (typeof input === 'string') {
|
|
858
|
+
const text = input.trim();
|
|
859
|
+
return text.length > 0 ? [text] : undefined;
|
|
860
|
+
}
|
|
861
|
+
if (Array.isArray(input)) {
|
|
862
|
+
const output = [];
|
|
863
|
+
for (const item of input) {
|
|
864
|
+
if (typeof item === 'string') {
|
|
865
|
+
const text = item.trim();
|
|
866
|
+
if (text.length > 0) {
|
|
867
|
+
output.push(text);
|
|
868
|
+
}
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
if (isRecord(item) && typeof item.text === 'string' && item.text.trim().length > 0) {
|
|
872
|
+
output.push(item.text.trim());
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return output.length > 0 ? output : undefined;
|
|
876
|
+
}
|
|
877
|
+
if (isRecord(input) && typeof input.text === 'string' && input.text.trim().length > 0) {
|
|
878
|
+
return [input.text.trim()];
|
|
879
|
+
}
|
|
880
|
+
return undefined;
|
|
881
|
+
}
|
|
882
|
+
function readIntFromAny(value) {
|
|
883
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
884
|
+
const asInt = Math.trunc(value);
|
|
885
|
+
return Number.isNaN(asInt) ? undefined : asInt;
|
|
886
|
+
}
|
|
887
|
+
if (typeof value === 'string') {
|
|
888
|
+
const parsed = Number.parseInt(value.trim(), 10);
|
|
889
|
+
return Number.isNaN(parsed) ? undefined : parsed;
|
|
890
|
+
}
|
|
891
|
+
return undefined;
|
|
892
|
+
}
|
|
893
|
+
function readNumberFromAny(value) {
|
|
894
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
895
|
+
return value;
|
|
896
|
+
}
|
|
897
|
+
if (typeof value === 'string') {
|
|
898
|
+
const parsed = Number.parseFloat(value.trim());
|
|
899
|
+
return Number.isNaN(parsed) ? undefined : parsed;
|
|
900
|
+
}
|
|
901
|
+
return undefined;
|
|
902
|
+
}
|
|
903
|
+
function openAIThinkingBudget(reasoning) {
|
|
904
|
+
if (!isRecord(reasoning)) {
|
|
905
|
+
return undefined;
|
|
906
|
+
}
|
|
907
|
+
for (const key of ['budget_tokens', 'thinking_budget', 'thinkingBudget', 'max_output_tokens']) {
|
|
908
|
+
const value = readIntFromAny(reasoning[key]);
|
|
909
|
+
if (value !== undefined) {
|
|
910
|
+
return value;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return undefined;
|
|
914
|
+
}
|
|
915
|
+
function mapToolResultMessage(value, toolCallId, name, isError, providerType) {
|
|
916
|
+
const content = extractText(value);
|
|
917
|
+
const contentJSON = jsonString(value);
|
|
918
|
+
const renderedContent = content.length > 0 ? content : contentJSON;
|
|
919
|
+
if (renderedContent.length === 0) {
|
|
920
|
+
return undefined;
|
|
921
|
+
}
|
|
922
|
+
return {
|
|
923
|
+
role: 'tool',
|
|
924
|
+
content: renderedContent,
|
|
925
|
+
parts: [
|
|
926
|
+
{
|
|
927
|
+
type: 'tool_result',
|
|
928
|
+
toolResult: {
|
|
929
|
+
toolCallId: typeof toolCallId === 'string' && toolCallId.trim().length > 0 ? toolCallId : undefined,
|
|
930
|
+
name: typeof name === 'string' && name.trim().length > 0 ? name : undefined,
|
|
931
|
+
content: renderedContent,
|
|
932
|
+
contentJSON,
|
|
933
|
+
isError: typeof isError === 'boolean' ? isError : undefined,
|
|
934
|
+
},
|
|
935
|
+
metadata: { providerType },
|
|
936
|
+
},
|
|
937
|
+
],
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
function metadataWithThinkingBudget(metadata, thinkingBudget) {
|
|
941
|
+
if (thinkingBudget === undefined) {
|
|
942
|
+
return metadata ? { ...metadata } : undefined;
|
|
943
|
+
}
|
|
944
|
+
const out = metadata ? { ...metadata } : {};
|
|
945
|
+
out[thinkingBudgetMetadataKey] = thinkingBudget;
|
|
946
|
+
return out;
|
|
947
|
+
}
|
|
948
|
+
function canonicalToolChoice(value) {
|
|
949
|
+
if (!hasValue(value)) {
|
|
950
|
+
return undefined;
|
|
951
|
+
}
|
|
952
|
+
if (typeof value === 'string') {
|
|
953
|
+
const normalized = value.trim().toLowerCase();
|
|
954
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
955
|
+
}
|
|
956
|
+
if (isRecord(value) && 'value' in value) {
|
|
957
|
+
const normalized = String(value.value ?? '')
|
|
958
|
+
.trim()
|
|
959
|
+
.toLowerCase();
|
|
960
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
961
|
+
}
|
|
962
|
+
const encoded = jsonString(value);
|
|
963
|
+
return encoded.length > 0 ? encoded : undefined;
|
|
964
|
+
}
|
|
965
|
+
function jsonArtifact(type, name, payload) {
|
|
966
|
+
return {
|
|
967
|
+
type,
|
|
968
|
+
name,
|
|
969
|
+
payload: jsonString(payload),
|
|
970
|
+
mimeType: 'application/json',
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
function jsonString(value) {
|
|
974
|
+
try {
|
|
975
|
+
return JSON.stringify(value, objectKeySorter);
|
|
976
|
+
}
|
|
977
|
+
catch {
|
|
978
|
+
return String(value ?? '');
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
function objectKeySorter(_key, value) {
|
|
982
|
+
if (!isRecord(value) || Array.isArray(value)) {
|
|
983
|
+
return value;
|
|
984
|
+
}
|
|
985
|
+
const sorted = {};
|
|
986
|
+
for (const key of Object.keys(value).sort()) {
|
|
987
|
+
sorted[key] = value[key];
|
|
988
|
+
}
|
|
989
|
+
return sorted;
|
|
990
|
+
}
|
|
991
|
+
function hasValue(value) {
|
|
992
|
+
return value !== undefined && value !== null;
|
|
993
|
+
}
|
|
994
|
+
function isRecord(value) {
|
|
995
|
+
return typeof value === 'object' && value !== null;
|
|
996
|
+
}
|
|
997
|
+
function asDate(value) {
|
|
998
|
+
if (value === undefined) {
|
|
999
|
+
return undefined;
|
|
1000
|
+
}
|
|
1001
|
+
const date = value instanceof Date ? new Date(value) : new Date(value);
|
|
1002
|
+
if (Number.isNaN(date.getTime())) {
|
|
1003
|
+
return undefined;
|
|
1004
|
+
}
|
|
1005
|
+
return date;
|
|
1006
|
+
}
|
|
1007
|
+
//# sourceMappingURL=openai.js.map
|