@tanstack/ai-groq 0.1.0

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 (40) hide show
  1. package/README.md +91 -0
  2. package/dist/esm/adapters/text.d.ts +104 -0
  3. package/dist/esm/adapters/text.js +384 -0
  4. package/dist/esm/adapters/text.js.map +1 -0
  5. package/dist/esm/index.d.ts +10 -0
  6. package/dist/esm/index.js +9 -0
  7. package/dist/esm/index.js.map +1 -0
  8. package/dist/esm/message-types.d.ts +292 -0
  9. package/dist/esm/model-meta.d.ts +275 -0
  10. package/dist/esm/model-meta.js +54 -0
  11. package/dist/esm/model-meta.js.map +1 -0
  12. package/dist/esm/text/text-provider-options.d.ts +179 -0
  13. package/dist/esm/text/text-provider-options.js +6 -0
  14. package/dist/esm/text/text-provider-options.js.map +1 -0
  15. package/dist/esm/tools/function-tool.d.ts +13 -0
  16. package/dist/esm/tools/function-tool.js +29 -0
  17. package/dist/esm/tools/function-tool.js.map +1 -0
  18. package/dist/esm/tools/index.d.ts +2 -0
  19. package/dist/esm/tools/tool-converter.d.ts +7 -0
  20. package/dist/esm/tools/tool-converter.js +10 -0
  21. package/dist/esm/tools/tool-converter.js.map +1 -0
  22. package/dist/esm/utils/client.d.ts +17 -0
  23. package/dist/esm/utils/client.js +23 -0
  24. package/dist/esm/utils/client.js.map +1 -0
  25. package/dist/esm/utils/index.d.ts +2 -0
  26. package/dist/esm/utils/schema-converter.d.ts +25 -0
  27. package/dist/esm/utils/schema-converter.js +78 -0
  28. package/dist/esm/utils/schema-converter.js.map +1 -0
  29. package/package.json +52 -0
  30. package/src/adapters/text.ts +599 -0
  31. package/src/index.ts +33 -0
  32. package/src/message-types.ts +359 -0
  33. package/src/model-meta.ts +370 -0
  34. package/src/text/text-provider-options.ts +225 -0
  35. package/src/tools/function-tool.ts +44 -0
  36. package/src/tools/index.ts +5 -0
  37. package/src/tools/tool-converter.ts +15 -0
  38. package/src/utils/client.ts +42 -0
  39. package/src/utils/index.ts +10 -0
  40. package/src/utils/schema-converter.ts +110 -0
@@ -0,0 +1,10 @@
1
+ export {
2
+ createGroqClient,
3
+ getGroqApiKeyFromEnv,
4
+ generateId,
5
+ type GroqClientConfig,
6
+ } from './client'
7
+ export {
8
+ makeGroqStructuredOutputCompatible,
9
+ transformNullsToUndefined,
10
+ } from './schema-converter'
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Recursively transform null values to undefined in an object.
3
+ *
4
+ * This is needed because Groq's structured output requires all fields to be
5
+ * in the `required` array, with optional fields made nullable (type: ["string", "null"]).
6
+ * When Groq returns null for optional fields, we need to convert them back to
7
+ * undefined to match the original Zod schema expectations.
8
+ *
9
+ * @param obj - Object to transform
10
+ * @returns Object with nulls converted to undefined
11
+ */
12
+ export function transformNullsToUndefined<T>(obj: T): T {
13
+ if (obj === null) {
14
+ return undefined as unknown as T
15
+ }
16
+
17
+ if (Array.isArray(obj)) {
18
+ return obj.map((item) => transformNullsToUndefined(item)) as unknown as T
19
+ }
20
+
21
+ if (typeof obj === 'object') {
22
+ const result: Record<string, unknown> = {}
23
+ for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
24
+ const transformed = transformNullsToUndefined(value)
25
+ if (transformed !== undefined) {
26
+ result[key] = transformed
27
+ }
28
+ }
29
+ return result as T
30
+ }
31
+
32
+ return obj
33
+ }
34
+
35
+ /**
36
+ * Transform a JSON schema to be compatible with Groq's structured output requirements.
37
+ *
38
+ * Groq requires:
39
+ * - All properties must be in the `required` array
40
+ * - Optional fields should have null added to their type union
41
+ * - additionalProperties must be false for objects
42
+ *
43
+ * @param schema - JSON schema to transform
44
+ * @param originalRequired - Original required array (to know which fields were optional)
45
+ * @returns Transformed schema compatible with Groq structured output
46
+ */
47
+ export function makeGroqStructuredOutputCompatible(
48
+ schema: Record<string, any>,
49
+ originalRequired: Array<string> = [],
50
+ ): Record<string, any> {
51
+ const result = { ...schema }
52
+
53
+ if (result.type === 'object') {
54
+ if (!result.properties) {
55
+ result.properties = {}
56
+ }
57
+ const properties = { ...result.properties }
58
+ const allPropertyNames = Object.keys(properties)
59
+
60
+ for (const propName of allPropertyNames) {
61
+ const prop = properties[propName]
62
+ const wasOptional = !originalRequired.includes(propName)
63
+
64
+ if (prop.type === 'object' && prop.properties) {
65
+ properties[propName] = makeGroqStructuredOutputCompatible(
66
+ prop,
67
+ prop.required || [],
68
+ )
69
+ } else if (prop.type === 'array' && prop.items) {
70
+ properties[propName] = {
71
+ ...prop,
72
+ items: makeGroqStructuredOutputCompatible(
73
+ prop.items,
74
+ prop.items.required || [],
75
+ ),
76
+ }
77
+ } else if (wasOptional) {
78
+ if (prop.type && !Array.isArray(prop.type)) {
79
+ properties[propName] = {
80
+ ...prop,
81
+ type: [prop.type, 'null'],
82
+ }
83
+ } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {
84
+ properties[propName] = {
85
+ ...prop,
86
+ type: [...prop.type, 'null'],
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ result.properties = properties
93
+ // Groq rejects `required` when there are no properties, even if it's an empty array
94
+ if (allPropertyNames.length > 0) {
95
+ result.required = allPropertyNames
96
+ } else {
97
+ delete result.required
98
+ }
99
+ result.additionalProperties = false
100
+ }
101
+
102
+ if (result.type === 'array' && result.items) {
103
+ result.items = makeGroqStructuredOutputCompatible(
104
+ result.items,
105
+ result.items.required || [],
106
+ )
107
+ }
108
+
109
+ return result
110
+ }