@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.
Files changed (110) hide show
  1. package/LICENSE +3 -0
  2. package/README.md +367 -0
  3. package/dist/client.d.ts +102 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +1502 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/config.d.ts +9 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +190 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/content_capture.d.ts +35 -0
  12. package/dist/content_capture.d.ts.map +1 -0
  13. package/dist/content_capture.js +123 -0
  14. package/dist/content_capture.js.map +1 -0
  15. package/dist/context.d.ts +11 -0
  16. package/dist/context.d.ts.map +1 -0
  17. package/dist/context.js +53 -0
  18. package/dist/context.js.map +1 -0
  19. package/dist/exporters/default.d.ts +3 -0
  20. package/dist/exporters/default.d.ts.map +1 -0
  21. package/dist/exporters/default.js +34 -0
  22. package/dist/exporters/default.js.map +1 -0
  23. package/dist/exporters/grpc.d.ts +14 -0
  24. package/dist/exporters/grpc.d.ts.map +1 -0
  25. package/dist/exporters/grpc.js +369 -0
  26. package/dist/exporters/grpc.js.map +1 -0
  27. package/dist/exporters/http.d.ts +8 -0
  28. package/dist/exporters/http.d.ts.map +1 -0
  29. package/dist/exporters/http.js +258 -0
  30. package/dist/exporters/http.js.map +1 -0
  31. package/dist/frameworks/google-adk/index.d.ts +136 -0
  32. package/dist/frameworks/google-adk/index.d.ts.map +1 -0
  33. package/dist/frameworks/google-adk/index.js +509 -0
  34. package/dist/frameworks/google-adk/index.js.map +1 -0
  35. package/dist/frameworks/langchain/index.d.ts +29 -0
  36. package/dist/frameworks/langchain/index.d.ts.map +1 -0
  37. package/dist/frameworks/langchain/index.js +70 -0
  38. package/dist/frameworks/langchain/index.js.map +1 -0
  39. package/dist/frameworks/langgraph/index.d.ts +29 -0
  40. package/dist/frameworks/langgraph/index.d.ts.map +1 -0
  41. package/dist/frameworks/langgraph/index.js +70 -0
  42. package/dist/frameworks/langgraph/index.js.map +1 -0
  43. package/dist/frameworks/llamaindex/index.d.ts +43 -0
  44. package/dist/frameworks/llamaindex/index.d.ts.map +1 -0
  45. package/dist/frameworks/llamaindex/index.js +493 -0
  46. package/dist/frameworks/llamaindex/index.js.map +1 -0
  47. package/dist/frameworks/openai-agents/index.d.ts +33 -0
  48. package/dist/frameworks/openai-agents/index.d.ts.map +1 -0
  49. package/dist/frameworks/openai-agents/index.js +531 -0
  50. package/dist/frameworks/openai-agents/index.js.map +1 -0
  51. package/dist/frameworks/shared.d.ts +57 -0
  52. package/dist/frameworks/shared.d.ts.map +1 -0
  53. package/dist/frameworks/shared.js +1032 -0
  54. package/dist/frameworks/shared.js.map +1 -0
  55. package/dist/frameworks/vercel-ai-sdk/hooks.d.ts +18 -0
  56. package/dist/frameworks/vercel-ai-sdk/hooks.d.ts.map +1 -0
  57. package/dist/frameworks/vercel-ai-sdk/hooks.js +672 -0
  58. package/dist/frameworks/vercel-ai-sdk/hooks.js.map +1 -0
  59. package/dist/frameworks/vercel-ai-sdk/index.d.ts +8 -0
  60. package/dist/frameworks/vercel-ai-sdk/index.d.ts.map +1 -0
  61. package/dist/frameworks/vercel-ai-sdk/index.js +7 -0
  62. package/dist/frameworks/vercel-ai-sdk/index.js.map +1 -0
  63. package/dist/frameworks/vercel-ai-sdk/mapping.d.ts +49 -0
  64. package/dist/frameworks/vercel-ai-sdk/mapping.d.ts.map +1 -0
  65. package/dist/frameworks/vercel-ai-sdk/mapping.js +660 -0
  66. package/dist/frameworks/vercel-ai-sdk/mapping.js.map +1 -0
  67. package/dist/frameworks/vercel-ai-sdk/types.d.ts +126 -0
  68. package/dist/frameworks/vercel-ai-sdk/types.d.ts.map +1 -0
  69. package/dist/frameworks/vercel-ai-sdk/types.js +2 -0
  70. package/dist/frameworks/vercel-ai-sdk/types.js.map +1 -0
  71. package/dist/index.d.ts +12 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +12 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/providers/anthropic.d.ts +35 -0
  76. package/dist/providers/anthropic.d.ts.map +1 -0
  77. package/dist/providers/anthropic.js +581 -0
  78. package/dist/providers/anthropic.js.map +1 -0
  79. package/dist/providers/gemini.d.ts +42 -0
  80. package/dist/providers/gemini.d.ts.map +1 -0
  81. package/dist/providers/gemini.js +650 -0
  82. package/dist/providers/gemini.js.map +1 -0
  83. package/dist/providers/openai.d.ts +67 -0
  84. package/dist/providers/openai.d.ts.map +1 -0
  85. package/dist/providers/openai.js +1007 -0
  86. package/dist/providers/openai.js.map +1 -0
  87. package/dist/types.d.ts +461 -0
  88. package/dist/types.d.ts.map +1 -0
  89. package/dist/types.js +2 -0
  90. package/dist/types.js.map +1 -0
  91. package/dist/utils.d.ts +25 -0
  92. package/dist/utils.d.ts.map +1 -0
  93. package/dist/utils.js +331 -0
  94. package/dist/utils.js.map +1 -0
  95. package/docs/frameworks/google-adk.md +84 -0
  96. package/docs/frameworks/langchain.md +102 -0
  97. package/docs/frameworks/langgraph.md +137 -0
  98. package/docs/frameworks/llamaindex.md +82 -0
  99. package/docs/frameworks/openai-agents.md +88 -0
  100. package/docs/frameworks/vercel-ai-sdk.md +124 -0
  101. package/docs/index.md +22 -0
  102. package/docs/providers/anthropic.md +69 -0
  103. package/docs/providers/gemini.md +86 -0
  104. package/docs/providers/openai.md +151 -0
  105. package/package.json +80 -0
  106. package/proto/opentelemetry/proto/collector/trace/v1/trace_service.proto +77 -0
  107. package/proto/opentelemetry/proto/common/v1/common.proto +115 -0
  108. package/proto/opentelemetry/proto/resource/v1/resource.proto +44 -0
  109. package/proto/opentelemetry/proto/trace/v1/trace.proto +357 -0
  110. package/proto/sigil/v1/generation_ingest.proto +145 -0
@@ -0,0 +1,660 @@
1
+ const frameworkConversationPrefix = 'sigil:framework:vercel-ai-sdk:';
2
+ const frameworkName = 'vercel-ai-sdk';
3
+ const frameworkSource = 'framework';
4
+ const frameworkLanguage = 'typescript';
5
+ const stepTypeMetadataKey = 'sigil.framework.step_type';
6
+ const reasoningTextMetadataKey = 'sigil.framework.reasoning_text';
7
+ const maxMetadataDepth = 5;
8
+ export function frameworkIdentity() {
9
+ return {
10
+ name: frameworkName,
11
+ source: frameworkSource,
12
+ language: frameworkLanguage,
13
+ };
14
+ }
15
+ export function buildFrameworkTags(extraTags) {
16
+ return {
17
+ ...(extraTags ?? {}),
18
+ 'sigil.framework.name': frameworkName,
19
+ 'sigil.framework.source': frameworkSource,
20
+ 'sigil.framework.language': frameworkLanguage,
21
+ };
22
+ }
23
+ export function buildFrameworkMetadata(extraMetadata, stepType, reasoningText) {
24
+ const raw = {
25
+ ...(extraMetadata ?? {}),
26
+ 'sigil.framework.name': frameworkName,
27
+ 'sigil.framework.source': frameworkSource,
28
+ 'sigil.framework.language': frameworkLanguage,
29
+ };
30
+ if (stepType !== undefined) {
31
+ raw[stepTypeMetadataKey] = stepType;
32
+ }
33
+ if (reasoningText !== undefined) {
34
+ raw[reasoningTextMetadataKey] = reasoningText;
35
+ }
36
+ return normalizeMetadata(raw);
37
+ }
38
+ export function fallbackConversationId(suffix) {
39
+ return `${frameworkConversationPrefix}${suffix}`;
40
+ }
41
+ export function resolveConversationId(params) {
42
+ const explicit = asString(params.explicitConversationId);
43
+ if (explicit.length > 0) {
44
+ return { conversationId: explicit, source: 'explicit' };
45
+ }
46
+ const fromResolver = asString(params.resolver?.(params.stepStartEvent));
47
+ if (fromResolver.length > 0) {
48
+ return { conversationId: fromResolver, source: 'resolver' };
49
+ }
50
+ return {
51
+ conversationId: fallbackConversationId(params.fallbackSeed),
52
+ source: 'fallback',
53
+ };
54
+ }
55
+ export function extractStepNumber(event, fallback) {
56
+ const stepNumber = asNonNegativeInt(event.stepNumber);
57
+ if (stepNumber !== undefined) {
58
+ return stepNumber;
59
+ }
60
+ return fallback;
61
+ }
62
+ export function mapModelFromStepStart(event) {
63
+ const model = asRecord(event.model);
64
+ const modelName = asString(model?.modelId) || asString(model?.id) || asString(model?.name) || 'unknown';
65
+ const provider = normalizeProvider(asString(model?.provider), modelName);
66
+ return {
67
+ provider,
68
+ modelName,
69
+ };
70
+ }
71
+ export function mapResponseFromStepFinish(event) {
72
+ const response = asRecord(event.response);
73
+ const responseId = asString(response?.id);
74
+ const responseModel = asString(response?.modelId) || asString(event.modelId);
75
+ const finishReason = asString(event.finishReason);
76
+ return {
77
+ responseId: responseId.length > 0 ? responseId : undefined,
78
+ responseModel: responseModel.length > 0 ? responseModel : undefined,
79
+ finishReason: finishReason.length > 0 ? finishReason : undefined,
80
+ };
81
+ }
82
+ export function shouldTreatStepAsError(event) {
83
+ const finishReason = asString(event.finishReason).toLowerCase();
84
+ if (finishReason === 'error') {
85
+ return true;
86
+ }
87
+ return event.error !== undefined;
88
+ }
89
+ export function mapUsageFromStepFinish(event) {
90
+ const usage = asRecord(event.usage);
91
+ if (usage === undefined) {
92
+ return undefined;
93
+ }
94
+ const inputTokens = numberFromCandidates([usage.inputTokens, usage.promptTokens]);
95
+ const outputTokens = numberFromCandidates([usage.outputTokens, usage.completionTokens]);
96
+ const totalTokens = numberFromCandidates([usage.totalTokens]);
97
+ const inputDetails = asRecord(usage.inputTokenDetails);
98
+ const outputDetails = asRecord(usage.outputTokenDetails);
99
+ const cacheReadTokens = numberFromCandidates([inputDetails?.cacheReadTokens]);
100
+ const cacheWriteTokens = numberFromCandidates([inputDetails?.cacheWriteTokens]);
101
+ const cacheCreationTokens = numberFromCandidates([inputDetails?.cacheCreationTokens]);
102
+ const reasoningTokens = numberFromCandidates([outputDetails?.reasoningTokens]);
103
+ const hasUsagePayload = inputTokens !== undefined ||
104
+ outputTokens !== undefined ||
105
+ totalTokens !== undefined ||
106
+ inputDetails !== undefined ||
107
+ outputDetails !== undefined;
108
+ if (!hasUsagePayload) {
109
+ return undefined;
110
+ }
111
+ const resolvedInput = inputTokens ?? 0;
112
+ const resolvedOutput = outputTokens ?? 0;
113
+ const resolvedTotal = totalTokens ?? resolvedInput + resolvedOutput;
114
+ return {
115
+ inputTokens: resolvedInput,
116
+ outputTokens: resolvedOutput,
117
+ totalTokens: resolvedTotal,
118
+ cacheReadInputTokens: cacheReadTokens ?? 0,
119
+ cacheWriteInputTokens: cacheWriteTokens ?? 0,
120
+ cacheCreationInputTokens: cacheCreationTokens ?? 0,
121
+ reasoningTokens: reasoningTokens ?? 0,
122
+ };
123
+ }
124
+ export function mapInputMessages(messages) {
125
+ if (!Array.isArray(messages)) {
126
+ return [];
127
+ }
128
+ const output = [];
129
+ for (const rawMessage of messages) {
130
+ const message = mapSingleMessage(rawMessage);
131
+ if (message !== undefined) {
132
+ output.push(message);
133
+ }
134
+ }
135
+ return output;
136
+ }
137
+ export function mapStepOutput(event) {
138
+ const text = asString(event.text);
139
+ const reasoningText = asString(event.reasoningText);
140
+ const parsedToolCalls = parseToolCalls(event.toolCalls);
141
+ const parsedToolResults = parseToolResults(event.toolResults);
142
+ const stepType = deriveStepType({
143
+ stepType: event.stepType,
144
+ stepNumber: event.stepNumber,
145
+ hasToolResults: parsedToolResults.length > 0,
146
+ });
147
+ const assistantParts = [];
148
+ if (text.length > 0) {
149
+ assistantParts.push({
150
+ type: 'text',
151
+ text,
152
+ });
153
+ }
154
+ for (const toolCall of parsedToolCalls) {
155
+ assistantParts.push({
156
+ type: 'tool_call',
157
+ toolCall: {
158
+ id: toolCall.id,
159
+ name: toolCall.name,
160
+ inputJSON: toolCall.inputJSON,
161
+ },
162
+ });
163
+ }
164
+ const outputMessages = [];
165
+ if (assistantParts.length > 0) {
166
+ outputMessages.push({
167
+ role: 'assistant',
168
+ content: text.length > 0 ? text : undefined,
169
+ parts: assistantParts,
170
+ });
171
+ }
172
+ for (const toolResult of parsedToolResults) {
173
+ outputMessages.push({
174
+ role: 'tool',
175
+ name: toolResult.name,
176
+ content: toolResult.content,
177
+ parts: [
178
+ {
179
+ type: 'tool_result',
180
+ toolResult: {
181
+ toolCallId: toolResult.toolCallId,
182
+ name: toolResult.name,
183
+ content: toolResult.content,
184
+ contentJSON: toolResult.contentJSON,
185
+ isError: toolResult.isError,
186
+ },
187
+ },
188
+ ],
189
+ });
190
+ }
191
+ return {
192
+ output: outputMessages.length > 0 ? outputMessages : undefined,
193
+ stepType,
194
+ reasoningText: reasoningText.length > 0 ? reasoningText : undefined,
195
+ };
196
+ }
197
+ export function parseToolCallStart(event) {
198
+ const toolCall = asRecord(event.toolCall);
199
+ const toolCallId = asString(toolCall?.toolCallId);
200
+ if (toolCallId.length === 0) {
201
+ return undefined;
202
+ }
203
+ const toolName = asString(toolCall?.toolName) || 'framework_tool';
204
+ const toolType = asString(toolCall?.type);
205
+ const description = asString(toolCall?.description);
206
+ const input = toolCall?.input;
207
+ return {
208
+ toolCallId,
209
+ toolName,
210
+ input,
211
+ toolType: toolType.length > 0 ? toolType : undefined,
212
+ description: description.length > 0 ? description : undefined,
213
+ };
214
+ }
215
+ export function parseToolCallFinish(event) {
216
+ const toolCall = asRecord(event.toolCall);
217
+ const toolCallId = asString(toolCall?.toolCallId);
218
+ if (toolCallId.length === 0) {
219
+ return undefined;
220
+ }
221
+ const success = asBoolean(event.success, event.error === undefined);
222
+ const durationMs = asNonNegativeInt(event.durationMs);
223
+ return {
224
+ toolCallId,
225
+ success,
226
+ output: event.output,
227
+ error: event.error,
228
+ durationMs,
229
+ };
230
+ }
231
+ export function isTextChunk(event) {
232
+ const directType = asString(event.type);
233
+ if (isTextChunkType(directType)) {
234
+ return true;
235
+ }
236
+ const chunk = asRecord(event.chunk);
237
+ return isTextChunkType(asString(chunk?.type));
238
+ }
239
+ function isTextChunkType(type) {
240
+ return type === 'text' || type === 'text-delta';
241
+ }
242
+ export function normalizeMetadata(raw) {
243
+ const output = {};
244
+ const seen = new WeakSet();
245
+ for (const [key, value] of Object.entries(raw)) {
246
+ const normalizedKey = key.trim();
247
+ if (normalizedKey.length === 0) {
248
+ continue;
249
+ }
250
+ const normalizedValue = normalizeMetadataValue(value, 0, seen);
251
+ if (normalizedValue !== undefined) {
252
+ output[normalizedKey] = normalizedValue;
253
+ }
254
+ }
255
+ return output;
256
+ }
257
+ function normalizeMetadataValue(value, depth, seen) {
258
+ if (depth > maxMetadataDepth || value === undefined) {
259
+ return undefined;
260
+ }
261
+ if (value === null || typeof value === 'boolean' || typeof value === 'string') {
262
+ return value;
263
+ }
264
+ if (typeof value === 'number') {
265
+ return Number.isFinite(value) ? value : undefined;
266
+ }
267
+ if (value instanceof Date) {
268
+ return Number.isFinite(value.getTime()) ? value.toISOString() : undefined;
269
+ }
270
+ if (typeof value === 'function' || typeof value === 'symbol' || typeof value === 'bigint') {
271
+ return undefined;
272
+ }
273
+ if (Array.isArray(value)) {
274
+ const normalizedItems = [];
275
+ for (const item of value) {
276
+ const normalized = normalizeMetadataValue(item, depth + 1, seen);
277
+ if (normalized !== undefined) {
278
+ normalizedItems.push(normalized);
279
+ }
280
+ }
281
+ return normalizedItems;
282
+ }
283
+ if (!isRecord(value)) {
284
+ return undefined;
285
+ }
286
+ if (seen.has(value)) {
287
+ return '[circular]';
288
+ }
289
+ seen.add(value);
290
+ try {
291
+ const output = {};
292
+ for (const [key, nestedValue] of Object.entries(value)) {
293
+ const normalizedKey = key.trim();
294
+ if (normalizedKey.length === 0) {
295
+ continue;
296
+ }
297
+ const normalizedValue = normalizeMetadataValue(nestedValue, depth + 1, seen);
298
+ if (normalizedValue !== undefined) {
299
+ output[normalizedKey] = normalizedValue;
300
+ }
301
+ }
302
+ return output;
303
+ }
304
+ finally {
305
+ seen.delete(value);
306
+ }
307
+ }
308
+ function normalizeProvider(explicitProvider, modelName) {
309
+ const normalizedProvider = explicitProvider.trim().toLowerCase();
310
+ const canonicalProvider = canonicalizeProvider(normalizedProvider);
311
+ if (canonicalProvider !== undefined) {
312
+ return canonicalProvider;
313
+ }
314
+ if (normalizedProvider.length > 0) {
315
+ return 'custom';
316
+ }
317
+ return inferProviderFromModel(modelName);
318
+ }
319
+ function canonicalizeProvider(normalizedProvider) {
320
+ if (matchesProvider(normalizedProvider, 'openai')) {
321
+ return 'openai';
322
+ }
323
+ if (matchesProvider(normalizedProvider, 'anthropic')) {
324
+ return 'anthropic';
325
+ }
326
+ if (matchesProvider(normalizedProvider, 'gemini') || matchesProvider(normalizedProvider, 'google')) {
327
+ return 'gemini';
328
+ }
329
+ return undefined;
330
+ }
331
+ function matchesProvider(value, providerPrefix) {
332
+ if (value === providerPrefix) {
333
+ return true;
334
+ }
335
+ if (!value.startsWith(providerPrefix)) {
336
+ return false;
337
+ }
338
+ const separator = value.charAt(providerPrefix.length);
339
+ return separator === '.' || separator === ':' || separator === '/' || separator === '_' || separator === '-';
340
+ }
341
+ function inferProviderFromModel(modelName) {
342
+ const normalized = modelName.trim().toLowerCase();
343
+ if (normalized.startsWith('gpt-') ||
344
+ normalized.startsWith('o1') ||
345
+ normalized.startsWith('o3') ||
346
+ normalized.startsWith('o4')) {
347
+ return 'openai';
348
+ }
349
+ if (normalized.startsWith('claude-')) {
350
+ return 'anthropic';
351
+ }
352
+ if (normalized.startsWith('gemini-')) {
353
+ return 'gemini';
354
+ }
355
+ return 'custom';
356
+ }
357
+ function normalizeStepType(value) {
358
+ const normalized = asString(value).toLowerCase();
359
+ if (normalized === 'initial' || normalized === 'continue' || normalized === 'tool-result') {
360
+ return normalized;
361
+ }
362
+ return undefined;
363
+ }
364
+ function deriveStepType(params) {
365
+ const explicit = normalizeStepType(params.stepType);
366
+ if (explicit !== undefined) {
367
+ return explicit;
368
+ }
369
+ if (params.hasToolResults) {
370
+ return 'tool-result';
371
+ }
372
+ const stepNumber = asNonNegativeInt(params.stepNumber);
373
+ if (stepNumber === 0) {
374
+ return 'initial';
375
+ }
376
+ if (stepNumber !== undefined) {
377
+ return 'continue';
378
+ }
379
+ return undefined;
380
+ }
381
+ function parseToolCalls(value) {
382
+ if (!Array.isArray(value)) {
383
+ return [];
384
+ }
385
+ const output = [];
386
+ for (const rawToolCall of value) {
387
+ const toolCall = asRecord(rawToolCall);
388
+ if (toolCall === undefined) {
389
+ continue;
390
+ }
391
+ const id = asString(toolCall.toolCallId) || asString(toolCall.callId) || asString(toolCall.id);
392
+ const name = asString(toolCall.toolName) || asString(toolCall.name);
393
+ if (name.length === 0) {
394
+ continue;
395
+ }
396
+ output.push({
397
+ id: id.length > 0 ? id : undefined,
398
+ name,
399
+ inputJSON: maybeJSONStringify(toolCall.input ?? toolCall.arguments),
400
+ });
401
+ }
402
+ return output;
403
+ }
404
+ function parseToolResults(value) {
405
+ if (!Array.isArray(value)) {
406
+ return [];
407
+ }
408
+ const output = [];
409
+ for (const rawToolResult of value) {
410
+ const toolResult = asRecord(rawToolResult);
411
+ if (toolResult === undefined) {
412
+ continue;
413
+ }
414
+ const toolCallId = asString(toolResult.toolCallId) || asString(toolResult.callId) || asString(toolResult.id);
415
+ const name = asString(toolResult.toolName) || asString(toolResult.name);
416
+ const isError = asBoolean(toolResult.isError, false);
417
+ const rawContent = toolResult.output ?? toolResult.result ?? toolResult.content;
418
+ const content = typeof rawContent === 'string' ? rawContent.trim() : undefined;
419
+ const contentJSON = content === undefined ? maybeJSONStringify(rawContent) : undefined;
420
+ output.push({
421
+ toolCallId: toolCallId.length > 0 ? toolCallId : undefined,
422
+ name: name.length > 0 ? name : undefined,
423
+ content,
424
+ contentJSON,
425
+ isError,
426
+ });
427
+ }
428
+ return output;
429
+ }
430
+ function mapSingleMessage(rawMessage) {
431
+ if (!isRecord(rawMessage)) {
432
+ const text = asString(rawMessage);
433
+ if (text.length === 0) {
434
+ return undefined;
435
+ }
436
+ return {
437
+ role: 'user',
438
+ content: text,
439
+ };
440
+ }
441
+ const role = normalizeRole(rawMessage.role ?? rawMessage.type);
442
+ const name = asString(rawMessage.name);
443
+ const messageName = name.length > 0 ? name : undefined;
444
+ const content = rawMessage.content;
445
+ if (typeof content === 'string') {
446
+ const text = content.trim();
447
+ if (text.length === 0) {
448
+ return undefined;
449
+ }
450
+ return {
451
+ role,
452
+ name: messageName,
453
+ content: text,
454
+ };
455
+ }
456
+ const parts = mapMessageParts(content);
457
+ const fallbackText = extractFallbackText(content);
458
+ const textFromParts = extractTextFromParts(parts);
459
+ const messageContent = fallbackText.length > 0 ? fallbackText : textFromParts;
460
+ if (parts.length === 0 && messageContent.length === 0) {
461
+ return undefined;
462
+ }
463
+ return {
464
+ role,
465
+ name: messageName,
466
+ content: messageContent.length > 0 ? messageContent : undefined,
467
+ parts: parts.length > 0 ? parts : undefined,
468
+ };
469
+ }
470
+ function mapMessageParts(content) {
471
+ const rawParts = Array.isArray(content) ? content : isRecord(content) ? [content] : [];
472
+ const parts = [];
473
+ for (const rawPart of rawParts) {
474
+ const mapped = mapSinglePart(rawPart);
475
+ if (mapped !== undefined) {
476
+ parts.push(mapped);
477
+ }
478
+ }
479
+ return parts;
480
+ }
481
+ function mapSinglePart(rawPart) {
482
+ if (typeof rawPart === 'string') {
483
+ const text = rawPart.trim();
484
+ if (text.length === 0) {
485
+ return undefined;
486
+ }
487
+ return {
488
+ type: 'text',
489
+ text,
490
+ };
491
+ }
492
+ const part = asRecord(rawPart);
493
+ if (part === undefined) {
494
+ return undefined;
495
+ }
496
+ const type = asString(part.type).toLowerCase();
497
+ const text = asString(part.text);
498
+ if (type === 'text' || (type.length === 0 && text.length > 0)) {
499
+ if (text.length === 0) {
500
+ return undefined;
501
+ }
502
+ return {
503
+ type: 'text',
504
+ text,
505
+ };
506
+ }
507
+ if (type === 'reasoning' || type === 'thinking') {
508
+ const thinking = text;
509
+ if (thinking.length === 0) {
510
+ return undefined;
511
+ }
512
+ return {
513
+ type: 'thinking',
514
+ thinking,
515
+ };
516
+ }
517
+ if (type === 'tool-call' || type === 'tool_call') {
518
+ const name = asString(part.toolName) || asString(part.name);
519
+ if (name.length === 0) {
520
+ return undefined;
521
+ }
522
+ const id = asString(part.toolCallId) || asString(part.callId) || asString(part.id);
523
+ const inputJSON = maybeJSONStringify(part.input ?? part.arguments);
524
+ return {
525
+ type: 'tool_call',
526
+ toolCall: {
527
+ id: id.length > 0 ? id : undefined,
528
+ name,
529
+ inputJSON,
530
+ },
531
+ };
532
+ }
533
+ if (type === 'tool-result' || type === 'tool_result') {
534
+ const toolCallId = asString(part.toolCallId) || asString(part.callId) || asString(part.id);
535
+ const name = asString(part.toolName) || asString(part.name);
536
+ const rawResult = part.result ?? part.output ?? part.content;
537
+ const content = typeof rawResult === 'string' ? rawResult.trim() : undefined;
538
+ const contentJSON = content === undefined ? maybeJSONStringify(rawResult) : undefined;
539
+ const isError = asBoolean(part.isError, false);
540
+ return {
541
+ type: 'tool_result',
542
+ toolResult: {
543
+ toolCallId: toolCallId.length > 0 ? toolCallId : undefined,
544
+ name: name.length > 0 ? name : undefined,
545
+ content,
546
+ contentJSON,
547
+ isError,
548
+ },
549
+ };
550
+ }
551
+ return undefined;
552
+ }
553
+ function extractFallbackText(content) {
554
+ if (!isRecord(content)) {
555
+ return '';
556
+ }
557
+ return asString(content.text);
558
+ }
559
+ function extractTextFromParts(parts) {
560
+ const textParts = parts
561
+ .filter((part) => part.type === 'text')
562
+ .map((part) => part.text.trim())
563
+ .filter((part) => part.length > 0);
564
+ return textParts.join(' ').trim();
565
+ }
566
+ function normalizeRole(value) {
567
+ const normalized = asString(value).toLowerCase();
568
+ if (normalized === 'assistant' || normalized === 'ai') {
569
+ return 'assistant';
570
+ }
571
+ if (normalized === 'tool') {
572
+ return 'tool';
573
+ }
574
+ return 'user';
575
+ }
576
+ function maybeJSONStringify(value) {
577
+ if (value === undefined) {
578
+ return undefined;
579
+ }
580
+ if (typeof value === 'string') {
581
+ const trimmed = value.trim();
582
+ if (trimmed.length === 0) {
583
+ return undefined;
584
+ }
585
+ if (isJSON(trimmed)) {
586
+ return trimmed;
587
+ }
588
+ return JSON.stringify(trimmed);
589
+ }
590
+ try {
591
+ const encoded = JSON.stringify(value);
592
+ if (encoded === undefined || encoded === 'null') {
593
+ return undefined;
594
+ }
595
+ return encoded;
596
+ }
597
+ catch {
598
+ return undefined;
599
+ }
600
+ }
601
+ function numberFromCandidates(values) {
602
+ for (const value of values) {
603
+ const parsed = asNonNegativeInt(value);
604
+ if (parsed !== undefined) {
605
+ return parsed;
606
+ }
607
+ }
608
+ return undefined;
609
+ }
610
+ function asRecord(value) {
611
+ return isRecord(value) ? value : undefined;
612
+ }
613
+ function isRecord(value) {
614
+ return typeof value === 'object' && value !== null;
615
+ }
616
+ function asString(value) {
617
+ if (typeof value !== 'string') {
618
+ return '';
619
+ }
620
+ return value.trim();
621
+ }
622
+ function asBoolean(value, fallback) {
623
+ if (typeof value === 'boolean') {
624
+ return value;
625
+ }
626
+ return fallback;
627
+ }
628
+ function asNonNegativeInt(value) {
629
+ if (typeof value === 'number') {
630
+ if (!Number.isFinite(value)) {
631
+ return undefined;
632
+ }
633
+ if (value < 0) {
634
+ return undefined;
635
+ }
636
+ return Math.trunc(value);
637
+ }
638
+ if (typeof value === 'string') {
639
+ const trimmed = value.trim();
640
+ if (trimmed.length === 0) {
641
+ return undefined;
642
+ }
643
+ const parsed = Number.parseInt(trimmed, 10);
644
+ if (Number.isNaN(parsed) || parsed < 0) {
645
+ return undefined;
646
+ }
647
+ return parsed;
648
+ }
649
+ return undefined;
650
+ }
651
+ function isJSON(value) {
652
+ try {
653
+ JSON.parse(value);
654
+ return true;
655
+ }
656
+ catch {
657
+ return false;
658
+ }
659
+ }
660
+ //# sourceMappingURL=mapping.js.map