Skip to content

Commit 5fd3f5d

Browse files
authored
Return AST on scan. (#36)
When scanning a prompt returning the AST. using `parse` outside has the drawback that errors are not correctly handle and it breaking Latitude Playground by not showing compile errors
1 parent d41e592 commit 5fd3f5d

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/compiler/scan.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ describe('hash', async () => {
4747
expect(metadata1.hash).not.toBe(metadata3.hash)
4848
})
4949

50+
it('always returns ast (serialized fragments)', async () => {
51+
const prompt = 'This is a prompt'
52+
53+
const metadata = await scan({ prompt })
54+
expect(metadata.ast).toEqual({
55+
start: 0,
56+
end: 16,
57+
type: 'Fragment',
58+
children: [
59+
{
60+
start: 0,
61+
end: 16,
62+
type: 'Text',
63+
raw: 'This is a prompt',
64+
data: 'This is a prompt',
65+
},
66+
],
67+
})
68+
})
69+
5070
it('includes the content from referenced tags into account when calculating the hash', async () => {
5171
const parent = 'This is the parent prompt. <prompt path="child" /> The end.'
5272
const child1 = 'ABCDEFG'

src/compiler/scan.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export class Scan {
174174
...(scopeContext.onlyPredefinedVariables ?? new Set([])),
175175
]),
176176
hash,
177+
ast: fragment,
177178
resolvedPrompt,
178179
config: this.config ?? {},
179180
errors: this.errors,

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CompileError from '$promptl/error/error'
2+
import { Fragment } from '$promptl/index'
23

34
import { Message } from './message'
45

@@ -12,6 +13,7 @@ export type Conversation = {
1213
export type ConversationMetadata = {
1314
hash: string
1415
resolvedPrompt: string
16+
ast: Fragment
1517
config: Config
1618
errors: CompileError[]
1719
parameters: Set<string> // Variables used in the prompt that have not been defined in runtime

0 commit comments

Comments
 (0)