ChatFlow

GitHub

UnitMesh - UnitServer Demo

Step 1. Setup REPL

首先,你需要 Setup 一下 FlowRepl: https://github.com/prompt-engineering/unit-server

然后,启动对应的 REPL 服务,例如:

  • Kotlin Repl
  • TypeScript Repl

然后,Run 一下下面的代码:

1var x = 1
2x + 10086

如果返回的是 {"resultValue":"10087","className":"java.lang.Integer"} 那就说明你的 REPL 服务已经可以正常工作了。

Step 2. Generate Code

ClickPrompt Logo
ChatGPT Logo

Step 3. Kotlin Ktor Sample

下面的代码是 AI 生成的,不过好像有点问题:

1%use kotless, ktor, exposed
2
3data class User(val id: Int, val username: String)
4
5class Server : KotlessAWS() {
6    override fun prepare(app: Application) {
7        Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
8
9        transaction {
10            SchemaUtils.create(Users)
11        }
12
13        app.routing {
14            get("/users") {
15                val users = transaction {
16                    Users.selectAll().map {
17                        User(it[Users.id], it[Users.username])
18                    }
19                }
20                call.respond(users)
21            }
22            post("/register") {
23                val user = call.receive<User>()
24                val id = transaction {
25                    // Insert the new user into the database
26                    Users.insert {
27                        it[username] = user.username
28                    } get Users.id
29                }
30
31                val newUser = User(id, user.username)
32                call.respond(newUser)
33            }
34        }
35    }
36}
37
38object Users : org.jetbrains.exposed.sql.Table("users") {
39    val id = integer("id").autoIncrement()
40    val username = varchar("username", 50).uniqueIndex()
41
42    override val primaryKey = PrimaryKey(id, name = "PK_User_ID")
43}

Step 4. Kotlin Spring Sample

1%use spring, kotless
2
3@RestController
4class SampleController {
5  @GetMapping("/hello")
6  fun helloKotlin(): String {
7    return "hello world"
8  }
9}

Step 5. React Hello, World

1import React, {useState, useEffect} from "react";
2import ReactDom, {createRoot} from "react-dom/client";
3
4const root = ReactDOM.createRoot(document.getElementById('root'));
5root.render(<h1>Hello, world!</h1>);

Step 6. React Sample

1import React, {useState, useEffect} from "react";
2import ReactDom, {createRoot} from "react-dom/client";
3
4function Root() {
5  const [tick, setTick] = useState<number>(0);
6
7  useEffect(() => {
8    const id = setInterval(() => {
9    setTick(it => it+1);
10  }, 1000);
11
12    return () => clearInterval(id);
13  }, []);
14
15  return <>
16    Hello world #{tick}
17  </>;
18}
19
20const root = ReactDOM.createRoot(document.getElementById('root'));
21root.render(<Root />);