Skip to content

Generate and compile Typescript for iframe recipes #869

@bfollington

Description

@bfollington

Came up talking to @seefeldb. Currently, we generate javascript and transform it at runtime using babel within our iframe. We also do a bunch of gymnastics around loading libraries and ESM modules.

If we generated typescript instead we could compile that using the same path in localBuild.ts we've used in the past. This lets us write standard import statements, do typechecking, transform JSX and emit one bundled output to run in the frame.

We can provide our standard API's via imports (useDoc, llm, readWebpage) instead of a large HTML string. This also means we can provide the latest versions of these functions rather than 'baking them in' to the charm.

To start, we can do the compile on every load. Later, we can cache it.

We can get a few wins in one go with this:

In general, this is a saner approach and requires less 'explaining' in the system prompt as a result. I expect the generated code would look very boring:

import { useDoc, llm } from '@commontools/lib'
import * as React from 'react'
import * as ReactDOM from 'react-dom'

const Counter = () => {
  const [count, setCount] = useState(0);

  return <div>{count} <button type="button" onClick={() => setCount(c => c + 1)}>Increment</button></div>
}

ReactDOM.render('#app', <Counter />);

That alone should improve our LLM results, and simplify reasoning about this whole stack.

Metadata

Metadata

Labels

P3corerunner/builder/charmenhancementNew feature or requestllmLLM prompt-engineer related

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions