fork of hey-api/openapi-ts because I need some additional things
1import './App.css';
2
3import * as Form from '@radix-ui/react-form';
4import { PlusIcon } from '@radix-ui/react-icons';
5import { Box, Button, Container, Flex, Heading, Section, TextField } from '@radix-ui/themes';
6import OpenAI from 'openai';
7import { useState } from 'react';
8
9import { client as baseClient } from './client/client.gen';
10import { OpenAi } from './client/sdk.gen';
11
12const sdk = new OpenAI({
13 apiKey: import.meta.env.VITE_OPENAI_API_KEY,
14 dangerouslyAllowBrowser: true,
15});
16
17baseClient.setConfig({
18 auth() {
19 return import.meta.env.VITE_OPENAI_API_KEY;
20 },
21});
22
23const client = new OpenAi({
24 client: baseClient,
25});
26
27function App() {
28 const [isRequiredNameError] = useState(false);
29
30 const onCreateResponse = async (values: FormData) => {
31 const stream = await sdk.responses.create({
32 input: values.get('input') as string,
33 model: 'gpt-5-nano',
34 stream: true,
35 });
36
37 for await (const event of stream) {
38 console.log(event);
39 }
40
41 const { data, error } = await client.createResponse({
42 body: {
43 input: values.get('input') as string,
44 model: 'gpt-5-nano',
45 stream: true,
46 },
47 });
48 if (error) {
49 console.log(error);
50 return;
51 }
52 console.log(data?.output);
53 };
54
55 return (
56 <Box style={{ background: 'var(--gray-a2)', borderRadius: 'var(--radius-3)' }}>
57 <Container size="1">
58 <Section size="1" />
59 <Flex align="center">
60 <a className="shrink-0" href="https://heyapi.dev/" target="_blank">
61 <img
62 src="https://heyapi.dev/assets/raw/logo.png"
63 className="h-16 w-16 transition duration-300 will-change-auto"
64 alt="Hey API logo"
65 />
66 </a>
67 <Heading>@hey-api/openapi-ts 🤝 OpenAI</Heading>
68 </Flex>
69 <Section size="1" />
70 <Flex direction="column" gapY="2">
71 <Form.Root
72 className="w-[400px]"
73 onSubmit={(event) => {
74 event.preventDefault();
75 onCreateResponse(new FormData(event.currentTarget));
76 }}
77 >
78 <Form.Field className="grid mb-[10px]" name="input">
79 <div className="flex items-baseline justify-between">
80 <Form.Label className="text-[15px] font-medium leading-[35px] text-white">
81 Input
82 </Form.Label>
83 {isRequiredNameError && (
84 <Form.Message className="text-[13px] text-white opacity-[0.8]">
85 Please enter a name
86 </Form.Message>
87 )}
88 <Form.Message className="text-[13px] text-white opacity-[0.8]" match="valueMissing">
89 Please enter an input
90 </Form.Message>
91 </div>
92 <Form.Control asChild>
93 <TextField.Root
94 placeholder="Write a one-sentence bedtime story about a unicorn."
95 name="input"
96 type="text"
97 required
98 />
99 </Form.Control>
100 </Form.Field>
101 <Flex gapX="2">
102 <Form.Submit asChild>
103 <Button type="submit">
104 <PlusIcon /> Create Response
105 </Button>
106 </Form.Submit>
107 {/* <Button onClick={onUpdatePet} type="button">
108 <ReloadIcon /> Update Pet
109 </Button> */}
110 </Flex>
111 </Form.Root>
112 </Flex>
113 <Section size="1" />
114 </Container>
115 </Box>
116 );
117}
118
119export default App;