Skip to content

Commit

Permalink
Improvements to editor tools (#72)
Browse files Browse the repository at this point in the history
* improvements to editor tools

* improvements

* fix bugs

* fix bugs

* more improvements

* fix ci?
  • Loading branch information
lucasavila00 committed Mar 29, 2024
1 parent 052b03e commit 1896892
Show file tree
Hide file tree
Showing 24 changed files with 603 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
pnpm install
pnpm build-package
pnpm build-packages
(cd packages/client && pnpm publish --no-git-checks --access public)
(cd packages/editor-tools && pnpm publish --no-git-checks --access public)
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
npm config set "@tiptap-pro:registry" https://registry.tiptap.dev/
npm config set "//registry.tiptap.dev/:_authToken" ${{ secrets.TIPTAP_TOKEN }}
pnpm i
pnpm run build-package
pnpm run build-packages
cd apps/gui && npm run tw
- name: build-linux
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- run: npm config set "@jsr:registry" https://npm.jsr.io
- run: npm config set "//registry.tiptap.dev/:_authToken" ${{ secrets.TIPTAP_TOKEN }}
- run: pnpm i
- run: pnpm run build-package
- run: pnpm run build-packages
- run: pnpm run static
- run: pnpm run test
10 changes: 10 additions & 0 deletions apps/egui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# egui

## 0.0.32

### Patch Changes

- Improve editor-tools
- Updated dependencies
- @lmscript/editor-tools@0.0.32
- @lmscript/client@0.0.32
- @lmscript/gui@0.0.32

## 0.0.31

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/egui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egui",
"version": "0.0.31",
"version": "0.0.32",
"description": "An Electron application with React and TypeScript",
"main": "./out/main/index.mjs",
"author": "example.com",
Expand Down
9 changes: 9 additions & 0 deletions apps/gui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @lmscript/gui

## 0.0.32

### Patch Changes

- Improve editor-tools
- Updated dependencies
- @lmscript/editor-tools@0.0.32
- @lmscript/client@0.0.32

## 0.0.31

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/gui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@lmscript/gui",
"private": true,
"version": "0.0.31",
"version": "0.0.32",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
5 changes: 4 additions & 1 deletion apps/gui/src/editor/components/Play/recoil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const generateAsyncAtom = atomFamily<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getter = (window as any).getBackendInstance as GetBackendInstance;
const instance = getter(param.backend);
const tasks = compileEditorState(param.editorState, param.backend.template);
const tasks = compileEditorState(param.editorState, {
template: param.backend.template,
useGenerationUuids: true,
});

instance
.executeJSON(
Expand Down
7 changes: 7 additions & 0 deletions examples/client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @lmscript/examples_client

## 1.0.5

### Patch Changes

- Updated dependencies
- @lmscript/client@0.0.32

## 1.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lmscript/examples_client",
"version": "1.0.4",
"version": "1.0.5",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"static": "pnpm run -r static",
"test": "pnpm run -r test",
"build-package": "pnpm run -r build-package"
"build-packages": "(cd packages/client && npm run build) && (cd packages/editor-tools && npm run build)"
},
"author": "",
"license": "ISC"
Expand Down
6 changes: 6 additions & 0 deletions packages/client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @lmscript/client

## 0.0.32

### Patch Changes

- Improve editor-tools

## 0.0.31

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@lmscript/client",
"version": "0.0.31",
"version": "0.0.32",
"description": "Dependency-Free Typescript client for LmScript.",
"main": "index.js",
"scripts": {
"build-package": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
"ts": "tsc --noEmit",
"tsw": "tsc --noEmit --watch",
"format": "prettier --write .",
Expand Down
8 changes: 8 additions & 0 deletions packages/editor-tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @lmscript/client

## 0.0.32

### Patch Changes

- Improve editor-tools
- Updated dependencies
- @lmscript/client@0.0.32

## 0.0.31

### Patch Changes
Expand Down
6 changes: 5 additions & 1 deletion packages/editor-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ TODO

## Installation

TODO
Install from [NPM](https://www.npmjs.com/package/@lmscript/editor-tools)

```shell
npm i @lmscript/editor-tools
```

## Usage

Expand Down
6 changes: 3 additions & 3 deletions packages/editor-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@lmscript/editor-tools",
"version": "0.0.31",
"version": "0.0.32",
"description": "Dependency-Free Typescript client for LmScript.",
"main": "index.js",
"scripts": {
"build-package": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
"ts": "tsc --noEmit",
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
"ts": "tsc --noEmit && tsc --noEmit -p ./tsconfig.tests.json",
"tsw": "tsc --noEmit --watch",
"format": "prettier --write .",
"format-check": "prettier --check .",
Expand Down
27 changes: 27 additions & 0 deletions packages/editor-tools/src/apply-chat-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { type Task } from "@lmscript/client/backends/abstract";
import { getRoleEnd, getRoleStart, ChatTemplate } from "@lmscript/client/chat-template";
import { MessageOfAuthor } from "./messages-of-author";
import { Author } from "./types";

export const applyChatTemplate = (messages: MessageOfAuthor[], template: ChatTemplate): Task[] => {
const countOfRoles: Record<Author, number> = {
system: 0,
user: 0,
assistant: 0,
};
return messages.flatMap((message): Task[] => {
const item: Task[] = [
{
tag: "AddTextTask",
text: getRoleStart(template, message.author, countOfRoles),
},
...message.tasks,
{
tag: "AddTextTask",
text: getRoleEnd(template, message.author, countOfRoles),
},
];
countOfRoles[message.author] += 1;
return item;
});
};
37 changes: 30 additions & 7 deletions packages/editor-tools/src/compile-editor-state.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { type Task } from "@lmscript/client/backends/abstract";
import { LmEditorState } from "./types";
import { MessageOfAuthorGetter } from "./messages-of-author";
import { messagesToTasks } from "./message-to-tasks";
import { MessageOfAuthorGetter, printCustomError } from "./messages-of-author";
import { applyChatTemplate } from "./apply-chat-template";
import { ChatTemplate } from "@lmscript/client/chat-template";

export const compileEditorState = (editorState: LmEditorState, template: ChatTemplate): Task[] => {
const state = new MessageOfAuthorGetter(editorState);
export const compileEditorState = (
editorState: LmEditorState,
options: {
template: ChatTemplate;
variableOverrides?: Record<string, string>;
useGenerationUuids?: boolean;
},
): Task[] => {
const overrides = options.variableOverrides ?? {};

const useGenerationUuids = options.useGenerationUuids ?? false;

const newVariables = editorState.variables.map((variable) => {
if (variable.name in overrides) {
return {
...variable,
value: overrides[variable.name],
};
}
return variable;
});

const state = new MessageOfAuthorGetter(
{ ...editorState, variables: newVariables },
useGenerationUuids,
);

const errors = state.getErrors();
if (errors.length > 0) {
// TODO: serialize errors
throw new Error(JSON.stringify(errors));
throw new Error(errors.map(printCustomError).join("\n"));
}
const messages = state.getAcc();

return messagesToTasks(messages, template, editorState.variables);
return applyChatTemplate(messages, options.template);
};
103 changes: 0 additions & 103 deletions packages/editor-tools/src/message-to-tasks.ts

This file was deleted.

Loading

0 comments on commit 1896892

Please sign in to comment.