Skip to content

Commit ea84608

Browse files
committed
added ai generation on backend
1 parent 8c68bbd commit ea84608

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

packages/api/ai/generate.mts

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type CodeCellType,
77
randomid,
88
type CellWithPlaceholderType,
9+
type MarkdownCellType,
910
} from '@srcbook/shared';
1011
import { type SessionType } from '../types.mjs';
1112
import { readFileSync } from 'node:fs';
@@ -79,17 +80,19 @@ ${diagnostics}
7980
};
8081

8182
const makeGenerateCellEditSystemPrompt = (language: CodeLanguageType) => {
83+
console.log(`code-updater-${language}.txt`);
8284
return readFileSync(Path.join(PROMPTS_DIR, `code-updater-${language}.txt`), 'utf-8');
8385
};
8486

8587
const makeGenerateCellEditUserPrompt = (
8688
query: string,
8789
session: SessionType,
88-
cell: CodeCellType,
90+
cell: CodeCellType | MarkdownCellType,
8991
) => {
92+
const cellLanguage = cell.type === 'markdown' ? 'markdown' : session.language;
9093
// Intentionally not passing in tsconfig.json here as that doesn't need to be in the prompt.
9194
const inlineSrcbook = encode(
92-
{ cells: session.cells, language: session.language },
95+
{ cells: session.cells, language: cellLanguage as CodeLanguageType },
9396
{ inline: true },
9497
);
9598

@@ -98,7 +101,7 @@ ${inlineSrcbook}
98101
==== END SRCBOOK ====
99102
100103
==== BEGIN CODE CELL ====
101-
${cell.source}
104+
${cell.type === 'markdown' ? (cell as MarkdownCellType).text : (cell as CodeCellType).source}
102105
==== END CODE CELL ====
103106
104107
==== BEGIN USER REQUEST ====
@@ -180,10 +183,14 @@ export async function generateCells(
180183
}
181184
}
182185

183-
export async function generateCellEdit(query: string, session: SessionType, cell: CodeCellType) {
186+
export async function generateCellEdit(
187+
query: string,
188+
session: SessionType,
189+
cell: CodeCellType | MarkdownCellType,
190+
) {
184191
const model = await getModel();
185-
186-
const systemPrompt = makeGenerateCellEditSystemPrompt(session.language);
192+
const cellLanguage = cell.type === 'markdown' ? 'markdown' : session.language;
193+
const systemPrompt = makeGenerateCellEditSystemPrompt(cellLanguage as CodeLanguageType);
187194
const userPrompt = makeGenerateCellEditUserPrompt(query, session, cell);
188195
const result = await generateText({
189196
model,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!-- srcbook:{"language":"markdown"} -->
2+
3+
## Instructions Context
4+
5+
You are tasked with editing a **Markdown cell** in a Srcbook.
6+
7+
A Srcbook is a **Markdown-compatible notebook**, used for documentation or text-based content.
8+
9+
### Srcbook Spec
10+
11+
The structure of a Srcbook:
12+
0. The language comment: `<!-- srcbook:{"language":"markdown"} -->`
13+
1. Title cell (heading 1)
14+
2. N more cells, which are either:
15+
- **Markdown cells** (GitHub flavored Markdown)
16+
- Markdown cells, which have a filename and source content.
17+
18+
#### Important Note:
19+
Markdown cells cannot use h1 or h6 heading, as these are reserved for Srcbook.
20+
21+
The user is already working on an existing Srcbook and is asking you to edit a specific Markdown cell.
22+
The Srcbook contents will be passed to you as context, as well as the user's request about the intended edits for the Markdown cell.
23+
24+
---
25+
26+
## Example Srcbook
27+
28+
<!-- srcbook:{"language":"markdown"} -->
29+
30+
### Getting Started
31+
32+
#### What are Srcbooks?
33+
34+
Srcbooks are an interactive way of organizing and presenting information. They are similar to other notebooks but unique in their flexibility and format.
35+
36+
#### Dependencies
37+
38+
You can include any necessary information, resources, or links to external content.
39+
40+
##### Introduction
41+
42+
This is a Markdown cell showcasing various Markdown features.
43+
44+
#### Features Overview
45+
46+
##### Text Formatting
47+
48+
- **Bold text**
49+
- *Italic text*
50+
- ~~Strikethrough text~~
51+
52+
##### Lists
53+
54+
- **Unordered List:**
55+
- Item 1
56+
- Item 2
57+
58+
- **Ordered List:**
59+
1. First item
60+
2. Second item
61+
62+
##### Code Blocks
63+
64+
Inline code: `console.log("Hello, Markdown!")`
65+
66+
##### Links
67+
68+
[Click here to visit Google](https://www.google.com)
69+
70+
##### Images
71+
72+
![Alt text](image.png)
73+
74+
---
75+
76+
## Final Instructions
77+
78+
The user's Srcbook will be passed to you, surrounded with `==== BEGIN SRCBOOK ====` and `==== END SRCBOOK ====`.
79+
The specific **Markdown cell** they want updated will also be passed to you, surrounded with `==== BEGIN MARKDOWN CELL ====` and `==== END MARKDOWN CELL ====`.
80+
The user's intent will be passed to you between `==== BEGIN USER REQUEST ====` and `==== END USER REQUEST ====`.
81+
82+
Your job is to edit the cell based on the contents of the Srcbook and the user's intent.
83+
Act as a **Markdown expert**, writing the best possible content you can. Focus on being **elegant, concise, and clear**.
84+
**ONLY RETURN THE MARKDOWN CONTENT, NO PREAMBULE, NO SUFFIX, ONLY THE MARKDOWN**.

packages/shared/src/schemas/cells.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const PackageJsonCellSchema = z.object({
2222

2323
export const CodeCellSchema = z.object({
2424
id: z.string(),
25-
type: z.literal('code'),
25+
type: z.enum(['code', 'markdown']),
2626
source: z.string(),
2727
language: z.enum(['javascript', 'typescript']),
2828
filename: z.string(),

0 commit comments

Comments
 (0)