@chnak/zod-to-markdown 1.0.3 → 1.0.4

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.
@@ -1,191 +1,191 @@
1
- # OpenAI Chat Completion Schema
2
-
3
- 完整的 OpenAI API Schema 转换为 Markdown 文档的示例。
4
-
5
- ## TypeScript 定义
6
-
7
- ```typescript
8
- const messageSchema = z.discriminatedUnion('role', [
9
- z.object({
10
- role: z.literal('system'),
11
- content: z.string(),
12
- name: z.string().optional(),
13
- }),
14
- z.object({
15
- role: z.literal('user'),
16
- content: z.union([z.string(), z.array(z.object({
17
- type: z.literal('text'),
18
- text: z.string(),
19
- }))]),
20
- name: z.string().optional(),
21
- }),
22
- z.object({
23
- role: z.literal('assistant'),
24
- content: z.string().nullable(),
25
- tool_calls: z.array(z.object({
26
- id: z.string(),
27
- type: z.literal('function'),
28
- function: z.object({
29
- name: z.string(),
30
- arguments: z.string(),
31
- }),
32
- })).optional(),
33
- name: z.string().optional(),
34
- }),
35
- z.object({
36
- role: z.literal('tool'),
37
- content: z.union([z.string(), z.object({
38
- type: z.literal('image_url'),
39
- image_url: z.object({
40
- url: z.string(),
41
- detail: z.enum(['low', 'high', 'auto']).optional(),
42
- }),
43
- })]),
44
- tool_call_id: z.string(),
45
- name: z.string().optional(),
46
- }),
47
- ]);
48
-
49
- const chatCompletionRequestSchema = z.object({
50
- model: z.string(),
51
- messages: z.array(messageSchema),
52
- temperature: z.number().min(0).max(2).optional(),
53
- top_p: z.number().min(0).max(1).optional(),
54
- n: z.number().int().min(1).optional(),
55
- stream: z.boolean().optional(),
56
- stop: z.union([z.string(), z.array(z.string())]).optional(),
57
- max_tokens: z.number().int().optional(),
58
- presence_penalty: z.number().min(-2).max(2).optional(),
59
- frequency_penalty: z.number().min(-2).max(2).optional(),
60
- logit_bias: z.record(z.string(), z.number()).optional(),
61
- user: z.string().optional(),
62
- });
63
- ```
64
-
65
- ## 生成的 Markdown 文档
66
-
67
- ```markdown
68
- - model
69
- - String
70
- - messages
71
- - Array
72
- - DiscriminatedUnion (key: role)
73
- - role
74
- - Literal: "system"
75
- - content
76
- - String
77
- - name
78
- - Optional
79
- - String
80
- |
81
- - role
82
- - Literal: "user"
83
- - content
84
- - Union
85
- - String
86
- |
87
- - Array
88
- - type
89
- - Literal: "text"
90
- - text
91
- - String
92
- - name
93
- - Optional
94
- - String
95
- |
96
- - role
97
- - Literal: "assistant"
98
- - content
99
- - Nullable
100
- - String
101
- - tool_calls
102
- - Optional
103
- - Array
104
- - id
105
- - String
106
- - type
107
- - Literal: "function"
108
- - function
109
- - name
110
- - String
111
- - arguments
112
- - String
113
- - name
114
- - Optional
115
- - String
116
- |
117
- - role
118
- - Literal: "tool"
119
- - content
120
- - Union
121
- - String
122
- |
123
- - type
124
- - Literal: "image_url"
125
- - image_url
126
- - url
127
- - String
128
- - detail
129
- - Optional
130
- - Enum: low, high, auto
131
- - tool_call_id
132
- - String
133
- - name
134
- - Optional
135
- - String
136
- - temperature
137
- - Optional
138
- - Number (minValue: 0) (maxValue: 2)
139
- - top_p
140
- - Optional
141
- - Number (minValue: 0) (maxValue: 1)
142
- - n
143
- - Optional
144
- - Number (minValue: 1)
145
- - stream
146
- - Optional
147
- - Boolean
148
- - stop
149
- - Optional
150
- - Union
151
- - String
152
- |
153
- - Array
154
- - String
155
- - max_tokens
156
- - Optional
157
- - Number
158
- - presence_penalty
159
- - Optional
160
- - Number (minValue: -2) (maxValue: 2)
161
- - frequency_penalty
162
- - Optional
163
- - Number (minValue: -2) (maxValue: 2)
164
- - logit_bias
165
- - Optional
166
- - Record
167
- Key:
168
- - String
169
- Value:
170
- - Number
171
- - user
172
- - Optional
173
- - String
174
- ```
175
-
176
- ## 使用的类型覆盖
177
-
178
- | 类型 | 出现位置 |
179
- |------|----------|
180
- | `ZodDiscriminatedUnion` | `messages` 数组元素,role 鉴别键 |
181
- | `ZodObject` | 顶层 schema 和各 message 类型 |
182
- | `ZodArray` | `messages`, `tool_calls`, `stop` |
183
- | `ZodOptional` | `name`, `tool_calls`, `detail` 等 |
184
- | `ZodNullable` | `content` (assistant) |
185
- | `ZodUnion` | `content`, `stop` |
186
- | `ZodLiteral` | `role` 值,`type` 值 |
187
- | `ZodEnum` | `detail` 枚举 |
188
- | `ZodRecord` | `logit_bias` |
189
- | `ZodString` | 各种字符串字段 |
190
- | `ZodNumber` | 各种数字字段,带 min/max 约束 |
191
- | `ZodBoolean` | `stream` |
1
+ # OpenAI Chat Completion Schema
2
+
3
+ 完整的 OpenAI API Schema 转换为 Markdown 文档的示例。
4
+
5
+ ## TypeScript 定义
6
+
7
+ ```typescript
8
+ const messageSchema = z.discriminatedUnion('role', [
9
+ z.object({
10
+ role: z.literal('system'),
11
+ content: z.string(),
12
+ name: z.string().optional(),
13
+ }),
14
+ z.object({
15
+ role: z.literal('user'),
16
+ content: z.union([z.string(), z.array(z.object({
17
+ type: z.literal('text'),
18
+ text: z.string(),
19
+ }))]),
20
+ name: z.string().optional(),
21
+ }),
22
+ z.object({
23
+ role: z.literal('assistant'),
24
+ content: z.string().nullable(),
25
+ tool_calls: z.array(z.object({
26
+ id: z.string(),
27
+ type: z.literal('function'),
28
+ function: z.object({
29
+ name: z.string(),
30
+ arguments: z.string(),
31
+ }),
32
+ })).optional(),
33
+ name: z.string().optional(),
34
+ }),
35
+ z.object({
36
+ role: z.literal('tool'),
37
+ content: z.union([z.string(), z.object({
38
+ type: z.literal('image_url'),
39
+ image_url: z.object({
40
+ url: z.string(),
41
+ detail: z.enum(['low', 'high', 'auto']).optional(),
42
+ }),
43
+ })]),
44
+ tool_call_id: z.string(),
45
+ name: z.string().optional(),
46
+ }),
47
+ ]);
48
+
49
+ const chatCompletionRequestSchema = z.object({
50
+ model: z.string(),
51
+ messages: z.array(messageSchema),
52
+ temperature: z.number().min(0).max(2).optional(),
53
+ top_p: z.number().min(0).max(1).optional(),
54
+ n: z.number().int().min(1).optional(),
55
+ stream: z.boolean().optional(),
56
+ stop: z.union([z.string(), z.array(z.string())]).optional(),
57
+ max_tokens: z.number().int().optional(),
58
+ presence_penalty: z.number().min(-2).max(2).optional(),
59
+ frequency_penalty: z.number().min(-2).max(2).optional(),
60
+ logit_bias: z.record(z.string(), z.number()).optional(),
61
+ user: z.string().optional(),
62
+ });
63
+ ```
64
+
65
+ ## 生成的 Markdown 文档
66
+
67
+ ```markdown
68
+ - model
69
+ - String
70
+ - messages
71
+ - Array
72
+ - DiscriminatedUnion (key: role)
73
+ - role
74
+ - Literal: "system"
75
+ - content
76
+ - String
77
+ - name
78
+ - Optional
79
+ - String
80
+ |
81
+ - role
82
+ - Literal: "user"
83
+ - content
84
+ - Union
85
+ - String
86
+ |
87
+ - Array
88
+ - type
89
+ - Literal: "text"
90
+ - text
91
+ - String
92
+ - name
93
+ - Optional
94
+ - String
95
+ |
96
+ - role
97
+ - Literal: "assistant"
98
+ - content
99
+ - Nullable
100
+ - String
101
+ - tool_calls
102
+ - Optional
103
+ - Array
104
+ - id
105
+ - String
106
+ - type
107
+ - Literal: "function"
108
+ - function
109
+ - name
110
+ - String
111
+ - arguments
112
+ - String
113
+ - name
114
+ - Optional
115
+ - String
116
+ |
117
+ - role
118
+ - Literal: "tool"
119
+ - content
120
+ - Union
121
+ - String
122
+ |
123
+ - type
124
+ - Literal: "image_url"
125
+ - image_url
126
+ - url
127
+ - String
128
+ - detail
129
+ - Optional
130
+ - Enum: low, high, auto
131
+ - tool_call_id
132
+ - String
133
+ - name
134
+ - Optional
135
+ - String
136
+ - temperature
137
+ - Optional
138
+ - Number (minValue: 0) (maxValue: 2)
139
+ - top_p
140
+ - Optional
141
+ - Number (minValue: 0) (maxValue: 1)
142
+ - n
143
+ - Optional
144
+ - Number (minValue: 1)
145
+ - stream
146
+ - Optional
147
+ - Boolean
148
+ - stop
149
+ - Optional
150
+ - Union
151
+ - String
152
+ |
153
+ - Array
154
+ - String
155
+ - max_tokens
156
+ - Optional
157
+ - Number
158
+ - presence_penalty
159
+ - Optional
160
+ - Number (minValue: -2) (maxValue: 2)
161
+ - frequency_penalty
162
+ - Optional
163
+ - Number (minValue: -2) (maxValue: 2)
164
+ - logit_bias
165
+ - Optional
166
+ - Record
167
+ Key:
168
+ - String
169
+ Value:
170
+ - Number
171
+ - user
172
+ - Optional
173
+ - String
174
+ ```
175
+
176
+ ## 使用的类型覆盖
177
+
178
+ | 类型 | 出现位置 |
179
+ |------|----------|
180
+ | `ZodDiscriminatedUnion` | `messages` 数组元素,role 鉴别键 |
181
+ | `ZodObject` | 顶层 schema 和各 message 类型 |
182
+ | `ZodArray` | `messages`, `tool_calls`, `stop` |
183
+ | `ZodOptional` | `name`, `tool_calls`, `detail` 等 |
184
+ | `ZodNullable` | `content` (assistant) |
185
+ | `ZodUnion` | `content`, `stop` |
186
+ | `ZodLiteral` | `role` 值,`type` 值 |
187
+ | `ZodEnum` | `detail` 枚举 |
188
+ | `ZodRecord` | `logit_bias` |
189
+ | `ZodString` | 各种字符串字段 |
190
+ | `ZodNumber` | 各种数字字段,带 min/max 约束 |
191
+ | `ZodBoolean` | `stream` |
@@ -1,64 +1,64 @@
1
- import { z } from 'zod';
2
- import { zodSchemaToMarkdown } from '../src/index';
3
-
4
- // OpenAI Chat Message Schema Example
5
- const messageSchema = z.discriminatedUnion('role', [
6
- z.object({
7
- role: z.literal('system'),
8
- content: z.string(),
9
- name: z.string().optional(),
10
- }),
11
- z.object({
12
- role: z.literal('user'),
13
- content: z.union([z.string(), z.array(z.object({
14
- type: z.literal('text'),
15
- text: z.string(),
16
- }))]),
17
- name: z.string().optional(),
18
- }),
19
- z.object({
20
- role: z.literal('assistant'),
21
- content: z.string().nullable(),
22
- tool_calls: z.array(z.object({
23
- id: z.string(),
24
- type: z.literal('function'),
25
- function: z.object({
26
- name: z.string(),
27
- arguments: z.string(),
28
- }),
29
- })).optional(),
30
- name: z.string().optional(),
31
- }),
32
- z.object({
33
- role: z.literal('tool'),
34
- content: z.union([z.string(), z.object({
35
- type: z.literal('image_url'),
36
- image_url: z.object({
37
- url: z.string(),
38
- detail: z.enum(['low', 'high', 'auto']).optional(),
39
- }),
40
- })]),
41
- tool_call_id: z.string(),
42
- name: z.string().optional(),
43
- }),
44
- ]);
45
-
46
- const chatCompletionRequestSchema = z.object({
47
- model: z.string(),
48
- messages: z.array(messageSchema),
49
- temperature: z.number().min(0).max(2).optional(),
50
- top_p: z.number().min(0).max(1).optional(),
51
- n: z.number().int().min(1).optional(),
52
- stream: z.boolean().optional(),
53
- stop: z.union([z.string(), z.array(z.string())]).optional(),
54
- max_tokens: z.number().int().optional(),
55
- presence_penalty: z.number().min(-2).max(2).optional(),
56
- frequency_penalty: z.number().min(-2).max(2).optional(),
57
- logit_bias: z.record(z.string(), z.number()).optional(),
58
- user: z.string().optional(),
59
- });
60
-
61
- const schema = chatCompletionRequestSchema;
62
- const markdown = zodSchemaToMarkdown(schema);
63
-
64
- console.log(markdown);
1
+ import { z } from 'zod';
2
+ import { zodSchemaToMarkdown } from '../src/index';
3
+
4
+ // OpenAI Chat Message Schema Example
5
+ const messageSchema = z.discriminatedUnion('role', [
6
+ z.object({
7
+ role: z.literal('system'),
8
+ content: z.string(),
9
+ name: z.string().optional(),
10
+ }),
11
+ z.object({
12
+ role: z.literal('user'),
13
+ content: z.union([z.string(), z.array(z.object({
14
+ type: z.literal('text'),
15
+ text: z.string(),
16
+ }))]),
17
+ name: z.string().optional(),
18
+ }),
19
+ z.object({
20
+ role: z.literal('assistant'),
21
+ content: z.string().nullable(),
22
+ tool_calls: z.array(z.object({
23
+ id: z.string(),
24
+ type: z.literal('function'),
25
+ function: z.object({
26
+ name: z.string(),
27
+ arguments: z.string(),
28
+ }),
29
+ })).optional(),
30
+ name: z.string().optional(),
31
+ }),
32
+ z.object({
33
+ role: z.literal('tool'),
34
+ content: z.union([z.string(), z.object({
35
+ type: z.literal('image_url'),
36
+ image_url: z.object({
37
+ url: z.string(),
38
+ detail: z.enum(['low', 'high', 'auto']).optional(),
39
+ }),
40
+ })]),
41
+ tool_call_id: z.string(),
42
+ name: z.string().optional(),
43
+ }),
44
+ ]);
45
+
46
+ const chatCompletionRequestSchema = z.object({
47
+ model: z.string(),
48
+ messages: z.array(messageSchema),
49
+ temperature: z.number().min(0).max(2).optional(),
50
+ top_p: z.number().min(0).max(1).optional(),
51
+ n: z.number().int().min(1).optional(),
52
+ stream: z.boolean().optional(),
53
+ stop: z.union([z.string(), z.array(z.string())]).optional(),
54
+ max_tokens: z.number().int().optional(),
55
+ presence_penalty: z.number().min(-2).max(2).optional(),
56
+ frequency_penalty: z.number().min(-2).max(2).optional(),
57
+ logit_bias: z.record(z.string(), z.number()).optional(),
58
+ user: z.string().optional(),
59
+ });
60
+
61
+ const schema = chatCompletionRequestSchema;
62
+ const markdown = zodSchemaToMarkdown(schema);
63
+
64
+ console.log(markdown);
package/jest.config.js CHANGED
@@ -1,5 +1,5 @@
1
- module.exports = {
2
- preset: 'ts-jest',
3
- testEnvironment: 'node',
4
- testPathIgnorePatterns: ['/node_modules/', '/lib/'],
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ testPathIgnorePatterns: ['/node_modules/', '/lib/'],
5
5
  };