@chaoslabs/ai-sdk 0.0.1 → 0.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.
package/dist/response.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // Parses responses from /v1/chat/stream endpoint.
4
4
  // Handles type: "report" events with content.data blocks.
5
- import { convertBlock } from './converter.js';
5
+ import { parseRawBlock } from './schemas.js';
6
6
  // ============================================================================
7
7
  // Response Parser
8
8
  // ============================================================================
@@ -67,6 +67,48 @@ export async function parseV1Stream(body, onEvent) {
67
67
  }
68
68
  return { blocks };
69
69
  }
70
+ /**
71
+ * Parse a raw block using schema detection.
72
+ * Returns the parsed block or null if parsing fails.
73
+ * Transforms legacy data formats to normalized structure.
74
+ */
75
+ function parseBlock(rawBlock) {
76
+ const result = parseRawBlock(rawBlock);
77
+ if (result.success) {
78
+ const block = result.data;
79
+ // Transform legacy chart data format to segments
80
+ if (block.type === 'chart') {
81
+ const chartBlock = block;
82
+ // If server sent data as [[label, value], ...] but not segments, convert it
83
+ if (chartBlock.data && (!chartBlock.segments || chartBlock.segments.length === 0)) {
84
+ chartBlock.segments = chartBlock.data.map(([label, value]) => ({
85
+ label,
86
+ value,
87
+ }));
88
+ }
89
+ // Clean up null values for cleaner API
90
+ if (chartBlock.segments === null)
91
+ chartBlock.segments = undefined;
92
+ if (chartBlock.series === null)
93
+ chartBlock.series = undefined;
94
+ if (chartBlock.categories === null)
95
+ chartBlock.categories = undefined;
96
+ if (chartBlock.xAxis === null)
97
+ chartBlock.xAxis = undefined;
98
+ if (chartBlock.yAxis === null)
99
+ chartBlock.yAxis = undefined;
100
+ if (chartBlock.sourceName === null)
101
+ chartBlock.sourceName = undefined;
102
+ if (chartBlock.timeframe === null)
103
+ chartBlock.timeframe = undefined;
104
+ if (chartBlock.isCurrency === null)
105
+ chartBlock.isCurrency = undefined;
106
+ }
107
+ return block;
108
+ }
109
+ console.error('Block parse error:', result.error);
110
+ return null;
111
+ }
70
112
  /**
71
113
  * Convert v1 final state to SDK Response format.
72
114
  */
@@ -83,8 +125,8 @@ export function toV1Response(responseId, model, finalState) {
83
125
  });
84
126
  }
85
127
  else if (rawBlock && typeof rawBlock === 'object') {
86
- // Block content - use existing converter
87
- const block = convertBlock(rawBlock);
128
+ // Block content - parse with schema
129
+ const block = parseBlock(rawBlock);
88
130
  if (block) {
89
131
  content.push({
90
132
  type: 'chaos.block',
@@ -95,11 +137,13 @@ export function toV1Response(responseId, model, finalState) {
95
137
  }
96
138
  }
97
139
  const output = content.length > 0
98
- ? [{
140
+ ? [
141
+ {
99
142
  type: 'message',
100
143
  role: 'assistant',
101
144
  content,
102
- }]
145
+ },
146
+ ]
103
147
  : [];
104
148
  return {
105
149
  id: responseId,