Skip to content

Commit 56f8e69

Browse files
authored
Merge branch 'main' into feature/add_tool_result_content_type
2 parents 482f0b7 + 8556fa3 commit 56f8e69

File tree

17 files changed

+246
-195
lines changed

17 files changed

+246
-195
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ TODO.md
5858
bin
5959

6060
AGENTS.md
61+
62+
# Vim crap
63+
*-E

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "promptl-ai",
3-
"version": "0.7.2",
3+
"version": "0.7.6",
44
"author": "Latitude Data",
55
"license": "MIT",
66
"description": "Compiler for PromptL, the prompt language",

src/compiler/base/nodes/for.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import CompileError from '$promptl/error/error'
2-
import { getExpectedError } from '$promptl/test/helpers'
31
import { Message, MessageContent, TextContent } from '$promptl/types'
42
import { describe, expect, it } from 'vitest'
53

@@ -71,12 +69,11 @@ describe('each loops', async () => {
7169
const prompt1 = `{{ for elemenet in ['a', 'b', 'c'] }} {{foo = 5}} {{ endfor }} {{foo}}`
7270
const prompt2 = `{{foo = 5}} {{ for element in ['a', 'b', 'c'] }} {{foo = 7}} {{ endfor }} {{foo}}`
7371
const prompt3 = `{{foo = 5}} {{ for element in [1, 2, 3] }} {{foo += element}} {{ endfor }} {{foo}}`
74-
const action1 = () => render({ prompt: prompt1, parameters: {} })
75-
const error1 = await getExpectedError(action1, CompileError)
72+
const result1 = await getCompiledText(prompt1)
7673
const result2 = await getCompiledText(prompt2)
7774
const result3 = await getCompiledText(prompt3)
7875

79-
expect(error1.code).toBe('variable-not-declared')
76+
expect(result1).toBe('')
8077
expect(result2).toBe('7')
8178
expect(result3).toBe('11')
8279
})

src/compiler/base/nodes/tags/ref.test.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ describe('ref tags', async () => {
5656
child: 'child message',
5757
}
5858

59-
const action = () => render({ prompt: prompts['parent'], adapter: Adapters.default })
59+
const action = () =>
60+
render({ prompt: prompts['parent'], adapter: Adapters.default })
6061
const error = await getExpectedError(action, CompileError)
6162
expect(error.code).toBe('missing-reference-function')
6263
})
@@ -83,16 +84,19 @@ describe('ref tags', async () => {
8384
parent: '<prompt path="child" />',
8485
}
8586

86-
const action = () =>
87-
render({
88-
prompt: prompts['parent'],
89-
parameters: { foo: 'bar' },
90-
referenceFn: buildReferenceFn(prompts),
91-
adapter: Adapters.default,
92-
})
93-
94-
const error = await getExpectedError(action, CompileError)
95-
expect(error.code).toBe('variable-not-declared')
87+
const result = await render({
88+
prompt: prompts['parent'],
89+
referenceFn: buildReferenceFn(prompts),
90+
adapter: Adapters.default,
91+
})
92+
expect(result.messages.length).toBe(1)
93+
const message = result.messages[0]! as SystemMessage
94+
expect(message.content).toEqual([
95+
{
96+
type: 'text',
97+
text: 'Child message:',
98+
},
99+
])
96100
})
97101

98102
it('referenced prompts can receive parameters as tag attributes', async () => {
@@ -336,12 +340,12 @@ describe('ref tags', async () => {
336340
{{ bar }}
337341
</content-text>
338342
{{ endfor }}
339-
`)
343+
`),
340344
}
341345

342346
const metadata = await scan({
343347
prompt: prompts['parent'],
344-
referenceFn: buildReferenceFn(prompts)
348+
referenceFn: buildReferenceFn(prompts),
345349
})
346350

347351
const result = await render({
@@ -351,18 +355,19 @@ describe('ref tags', async () => {
351355
})
352356

353357
expect(result.messages.length).toBe(2)
354-
const [ firstMessage, secondMessage ] = result.messages as [UserMessage, SystemMessage]
358+
const [firstMessage, secondMessage] = result.messages as [
359+
UserMessage,
360+
SystemMessage,
361+
]
355362
expect(firstMessage.role).toBe(MessageRole.user)
356363
expect(firstMessage.content.length).toBe(3)
357364
expect(firstMessage.content).toEqual([
358-
{ type: 'text', text: '11'},
359-
{ type: 'text', text: '12'},
360-
{ type: 'text', text: '13'},
365+
{ type: 'text', text: '11' },
366+
{ type: 'text', text: '12' },
367+
{ type: 'text', text: '13' },
361368
])
362369
expect(secondMessage.role).toBe(MessageRole.system)
363370
expect(secondMessage.content.length).toBe(1)
364-
expect(secondMessage.content).toEqual([
365-
{ type: 'text', text: '10' }
366-
])
371+
expect(secondMessage.content).toEqual([{ type: 'text', text: '10' }])
367372
})
368373
})

src/compiler/base/nodes/tags/ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function compile(
3232
}
3333

3434
const { path, ...refParameters } = attributes
35-
if (!path) baseNodeError(errors.referenceTagWithoutPrompt, node)
35+
if (!path) baseNodeError(errors.referenceTagWithoutPath, node)
3636
if (typeof path !== 'string') baseNodeError(errors.invalidReferencePath, node)
3737

3838
if (!nodeWithStatus.status.refAst || !nodeWithStatus.status.refFullPath) {

src/compiler/base/nodes/tags/scope.test.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { Adapters, Chain, render } from '$promptl/index'
22
import { complete } from '$promptl/compiler/test/helpers'
33
import { removeCommonIndent } from '$promptl/compiler/utils'
4-
import CompileError from '$promptl/error/error'
5-
import { getExpectedError } from '$promptl/test/helpers'
6-
import {
7-
MessageRole,
8-
SystemMessage,
9-
UserMessage,
10-
} from '$promptl/types'
4+
import { MessageRole, SystemMessage, UserMessage } from '$promptl/types'
115
import { describe, expect, it, vi } from 'vitest'
126

137
describe('scope tags', async () => {
@@ -48,7 +42,7 @@ describe('scope tags', async () => {
4842
`)
4943

5044
const result = await render({ prompt, adapter: Adapters.default })
51-
45+
5246
expect(result.messages.length).toBe(1)
5347
const message = result.messages[0]! as SystemMessage
5448
expect(message.content).toEqual([
@@ -67,18 +61,19 @@ describe('scope tags', async () => {
6761
const prompt = removeCommonIndent(`
6862
{{ foo = 'bar' }}
6963
<scope>
70-
{{ foo }}
64+
{{ foo == 'bar' }}
7165
</scope>
7266
`)
7367

74-
const action = () => render({
75-
prompt,
76-
parameters: { foo: 'baz' },
77-
adapter: Adapters.default
78-
})
79-
80-
const error = await getExpectedError(action, CompileError)
81-
expect(error.code).toBe('variable-not-declared')
68+
const result = await render({ prompt, adapter: Adapters.default })
69+
expect(result.messages.length).toBe(1)
70+
const message = result.messages[0]! as SystemMessage
71+
expect(message.content).toEqual([
72+
{
73+
type: 'text',
74+
text: 'false',
75+
},
76+
])
8277
})
8378

8479
it('can inherit parameters from parents if explicitly passed', async () => {
@@ -91,7 +86,7 @@ describe('scope tags', async () => {
9186
const result = await render({
9287
prompt,
9388
parameters: { foo: 'bar' },
94-
adapter: Adapters.default
89+
adapter: Adapters.default,
9590
})
9691

9792
expect(result.messages.length).toBe(1)
Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import CompileError from '$promptl/error/error'
2-
import { complete, getExpectedError } from "$promptl/compiler/test/helpers";
3-
import { removeCommonIndent } from "$promptl/compiler/utils";
4-
import { Chain } from "$promptl/index";
5-
import { describe, expect, it, vi } from "vitest";
2+
import { complete } from '$promptl/compiler/test/helpers'
3+
import { removeCommonIndent } from '$promptl/compiler/utils'
4+
import { Chain } from '$promptl/index'
5+
import { describe, expect, it, vi } from 'vitest'
66

7-
describe("step tags", async () => {
8-
it("does not create a variable from response if not specified", async () => {
9-
const mock = vi.fn();
7+
describe('step tags', async () => {
8+
it('does not create a variable from response if not specified', async () => {
9+
const mock = vi.fn()
1010
const prompt = removeCommonIndent(`
1111
<step>
1212
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -15,18 +15,22 @@ describe("step tags", async () => {
1515
<step>
1616
Now correct the statement if it is not true.
1717
</step>
18-
`);
18+
`)
1919

20-
const chain = new Chain({ prompt, parameters: { mock }});
21-
await complete({ chain, callback: async () => `
20+
const chain = new Chain({ prompt, parameters: { mock } })
21+
await complete({
22+
chain,
23+
callback: async () =>
24+
`
2225
The statement is not true because it is fake. My confidence score is 100.
23-
`.trim()});
26+
`.trim(),
27+
})
2428

25-
expect(mock).not.toHaveBeenCalled();
26-
});
29+
expect(mock).not.toHaveBeenCalled()
30+
})
2731

28-
it("creates a text variable from response if specified", async () => {
29-
const mock = vi.fn();
32+
it('creates a text variable from response if specified', async () => {
33+
const mock = vi.fn()
3034
const prompt = removeCommonIndent(`
3135
<step as="analysis">
3236
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -36,18 +40,24 @@ describe("step tags", async () => {
3640
{{ mock(analysis) }}
3741
Now correct the statement if it is not true.
3842
</step>
39-
`);
43+
`)
4044

41-
const chain = new Chain({ prompt, parameters: { mock }});
42-
await complete({ chain, callback: async () => `
45+
const chain = new Chain({ prompt, parameters: { mock } })
46+
await complete({
47+
chain,
48+
callback: async () =>
49+
`
4350
The statement is not true because it is fake. My confidence score is 100.
44-
`.trim()});
51+
`.trim(),
52+
})
4553

46-
expect(mock).toHaveBeenCalledWith("The statement is not true because it is fake. My confidence score is 100.");
47-
});
54+
expect(mock).toHaveBeenCalledWith(
55+
'The statement is not true because it is fake. My confidence score is 100.',
56+
)
57+
})
4858

49-
it("creates an object variable from response if specified and schema is provided", async () => {
50-
const mock = vi.fn();
59+
it('creates an object variable from response if specified and schema is provided', async () => {
60+
const mock = vi.fn()
5161
const prompt = removeCommonIndent(`
5262
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
5363
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -59,27 +69,33 @@ describe("step tags", async () => {
5969
Correct the statement taking into account the reason: '{{ analysis.reason }}'.
6070
{{ endif }}
6171
</step>
62-
`);
72+
`)
6373

64-
const chain = new Chain({ prompt, parameters: { mock }});
65-
const { messages } = await complete({ chain, callback: async () => `
74+
const chain = new Chain({ prompt, parameters: { mock } })
75+
const { messages } = await complete({
76+
chain,
77+
callback: async () =>
78+
`
6679
{
6780
"truthful": false,
6881
"reason": "It is fake",
6982
"confidence": 100
7083
}
71-
`.trim()});
84+
`.trim(),
85+
})
7286

7387
expect(mock).toHaveBeenCalledWith({
7488
truthful: false,
75-
reason: "It is fake",
76-
confidence: 100
77-
});
78-
expect(messages[2]!.content).toEqual("Correct the statement taking into account the reason: 'It is fake'.");
79-
});
89+
reason: 'It is fake',
90+
confidence: 100,
91+
})
92+
expect(messages[2]!.content).toEqual(
93+
"Correct the statement taking into account the reason: 'It is fake'.",
94+
)
95+
})
8096

81-
it("fails creating an object variable from response if specified and schema is provided but response is invalid", async () => {
82-
const mock = vi.fn();
97+
it('fails creating an object variable from response if specified and schema is provided but response is invalid', async () => {
98+
const mock = vi.fn()
8399
const prompt = removeCommonIndent(`
84100
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
85101
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -91,19 +107,29 @@ describe("step tags", async () => {
91107
Correct the statement taking into account the reason: '{{ analysis.reason }}'.
92108
{{ endif }}
93109
</step>
94-
`);
110+
`)
95111

96-
const chain = new Chain({ prompt, parameters: { mock }});
97-
const error = await getExpectedError(() => complete({ chain, callback: async () => `
112+
const chain = new Chain({ prompt, parameters: { mock } })
113+
let error: CompileError
114+
try {
115+
await complete({
116+
chain,
117+
callback: async () =>
118+
`
98119
Bad JSON.
99-
`.trim()}), CompileError)
100-
expect(error.code).toBe('invalid-step-response-format')
120+
`.trim(),
121+
})
122+
} catch (e) {
123+
error = e as CompileError
124+
expect(e).toBeInstanceOf(CompileError)
125+
}
101126

102-
expect(mock).not.toHaveBeenCalled();
103-
});
127+
expect(error!.code).toBe('invalid-step-response-format')
128+
expect(mock).not.toHaveBeenCalled()
129+
})
104130

105-
it("creates a raw variable from response if specified", async () => {
106-
const mock = vi.fn();
131+
it('creates a raw variable from response if specified', async () => {
132+
const mock = vi.fn()
107133
const prompt = removeCommonIndent(`
108134
<step raw="analysis">
109135
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -113,21 +139,25 @@ describe("step tags", async () => {
113139
{{ mock(analysis) }}
114140
Now correct the statement if it is not true.
115141
</step>
116-
`);
142+
`)
117143

118-
const chain = new Chain({ prompt, parameters: { mock }});
119-
await complete({ chain, callback: async () => `
144+
const chain = new Chain({ prompt, parameters: { mock } })
145+
await complete({
146+
chain,
147+
callback: async () =>
148+
`
120149
The statement is not true because it is fake. My confidence score is 100.
121-
`.trim()});
150+
`.trim(),
151+
})
122152

123153
expect(mock).toHaveBeenCalledWith({
124-
role: "assistant",
154+
role: 'assistant',
125155
content: [
126156
{
127-
type: "text",
128-
text: "The statement is not true because it is fake. My confidence score is 100.",
157+
type: 'text',
158+
text: 'The statement is not true because it is fake. My confidence score is 100.',
129159
},
130160
],
131-
});
132-
});
133-
});
161+
})
162+
})
163+
})

0 commit comments

Comments
 (0)