Skip to content

Commit 04dd074

Browse files
chore: add with * syntax to context tools
Basic example chat: true context: foo with * Say hi --- name: foo #!/bin/bash echo This is the input: ${GPTSCRIPT_INPUT}
1 parent 16ca484 commit 04dd074

File tree

9 files changed

+262
-4
lines changed

9 files changed

+262
-4
lines changed

pkg/runner/runner.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ func getToolRefInput(prg *types.Program, ref types.ToolReference, input string)
260260
targetArgs := prg.ToolSet[ref.ToolID].Arguments
261261
targetKeys := map[string]string{}
262262

263+
if ref.Arg == "*" {
264+
return input, nil
265+
}
266+
263267
if targetArgs == nil {
264268
return "", nil
265269
}
@@ -647,13 +651,17 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
647651
Env: env,
648652
}
649653

650-
var contentInput string
654+
var contextInput string
651655

652656
if state.Continuation != nil && state.Continuation.State != nil {
653-
contentInput = state.Continuation.State.Input
657+
contextInput = state.Continuation.State.Input
658+
}
659+
660+
if state.ResumeInput != nil {
661+
contextInput = *state.ResumeInput
654662
}
655663

656-
callCtx.InputContext, state, err = r.getContext(callCtx, state, monitor, env, contentInput)
664+
callCtx.InputContext, state, err = r.getContext(callCtx, state, monitor, env, contextInput)
657665
if err != nil || state.InputContextContinuation != nil {
658666
return state, err
659667
}

pkg/tests/runner2_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tests
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/gptscript-ai/gptscript/pkg/loader"
8+
"github.com/gptscript-ai/gptscript/pkg/tests/tester"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestContextWithAsterick(t *testing.T) {
13+
r := tester.NewRunner(t)
14+
prg, err := loader.ProgramFromSource(context.Background(), `
15+
chat: true
16+
context: foo with *
17+
18+
Say hi
19+
20+
---
21+
name: foo
22+
23+
#!/bin/bash
24+
25+
echo This is the input: ${GPTSCRIPT_INPUT}
26+
`, "")
27+
require.NoError(t, err)
28+
29+
resp, err := r.Chat(context.Background(), nil, prg, nil, "input 1")
30+
r.AssertStep(t, resp, err)
31+
32+
resp, err = r.Chat(context.Background(), resp.State, prg, nil, "input 2")
33+
r.AssertStep(t, resp, err)
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`{
2+
"role": "assistant",
3+
"content": [
4+
{
5+
"text": "TEST RESULT CALL: 1"
6+
}
7+
],
8+
"usage": {}
9+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
`{
2+
"model": "gpt-4o",
3+
"internalSystemPrompt": false,
4+
"messages": [
5+
{
6+
"role": "system",
7+
"content": [
8+
{
9+
"text": "This is the input: input 1\n\nSay hi"
10+
}
11+
],
12+
"usage": {}
13+
},
14+
{
15+
"role": "user",
16+
"content": [
17+
{
18+
"text": "input 1"
19+
}
20+
],
21+
"usage": {}
22+
}
23+
],
24+
"chat": true
25+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`{
2+
"role": "assistant",
3+
"content": [
4+
{
5+
"text": "TEST RESULT CALL: 2"
6+
}
7+
],
8+
"usage": {}
9+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
`{
2+
"model": "gpt-4o",
3+
"internalSystemPrompt": false,
4+
"messages": [
5+
{
6+
"role": "system",
7+
"content": [
8+
{
9+
"text": "This is the input: input 2\n\nSay hi"
10+
}
11+
],
12+
"usage": {}
13+
},
14+
{
15+
"role": "user",
16+
"content": [
17+
{
18+
"text": "input 1"
19+
}
20+
],
21+
"usage": {}
22+
},
23+
{
24+
"role": "assistant",
25+
"content": [
26+
{
27+
"text": "TEST RESULT CALL: 1"
28+
}
29+
],
30+
"usage": {}
31+
},
32+
{
33+
"role": "user",
34+
"content": [
35+
{
36+
"text": "input 2"
37+
}
38+
],
39+
"usage": {}
40+
}
41+
],
42+
"chat": true
43+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
`{
2+
"done": false,
3+
"content": "TEST RESULT CALL: 1",
4+
"toolID": "inline:",
5+
"state": {
6+
"continuation": {
7+
"state": {
8+
"input": "input 1",
9+
"completion": {
10+
"model": "gpt-4o",
11+
"internalSystemPrompt": false,
12+
"messages": [
13+
{
14+
"role": "system",
15+
"content": [
16+
{
17+
"text": "This is the input: input 1\n\nSay hi"
18+
}
19+
],
20+
"usage": {}
21+
},
22+
{
23+
"role": "user",
24+
"content": [
25+
{
26+
"text": "input 1"
27+
}
28+
],
29+
"usage": {}
30+
},
31+
{
32+
"role": "assistant",
33+
"content": [
34+
{
35+
"text": "TEST RESULT CALL: 1"
36+
}
37+
],
38+
"usage": {}
39+
}
40+
],
41+
"chat": true
42+
}
43+
},
44+
"result": "TEST RESULT CALL: 1"
45+
},
46+
"continuationToolID": "inline:"
47+
}
48+
}`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
`{
2+
"done": false,
3+
"content": "TEST RESULT CALL: 2",
4+
"toolID": "inline:",
5+
"state": {
6+
"continuation": {
7+
"state": {
8+
"input": "input 1",
9+
"completion": {
10+
"model": "gpt-4o",
11+
"internalSystemPrompt": false,
12+
"messages": [
13+
{
14+
"role": "system",
15+
"content": [
16+
{
17+
"text": "This is the input: input 2\n\nSay hi"
18+
}
19+
],
20+
"usage": {}
21+
},
22+
{
23+
"role": "user",
24+
"content": [
25+
{
26+
"text": "input 1"
27+
}
28+
],
29+
"usage": {}
30+
},
31+
{
32+
"role": "assistant",
33+
"content": [
34+
{
35+
"text": "TEST RESULT CALL: 1"
36+
}
37+
],
38+
"usage": {}
39+
},
40+
{
41+
"role": "user",
42+
"content": [
43+
{
44+
"text": "input 2"
45+
}
46+
],
47+
"usage": {}
48+
},
49+
{
50+
"role": "assistant",
51+
"content": [
52+
{
53+
"text": "TEST RESULT CALL: 2"
54+
}
55+
],
56+
"usage": {}
57+
}
58+
],
59+
"chat": true
60+
}
61+
},
62+
"result": "TEST RESULT CALL: 2"
63+
},
64+
"continuationToolID": "inline:"
65+
}
66+
}`

pkg/tests/tester/runner.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ func (c *Client) Call(_ context.Context, messageRequest types.CompletionRequest,
135135
type Runner struct {
136136
*runner.Runner
137137

138-
Client *Client
138+
Client *Client
139+
StepAsserted int
139140
}
140141

141142
func (r *Runner) RunDefault() string {
@@ -166,6 +167,21 @@ func (r *Runner) AssertResponded(t *testing.T) {
166167
require.Len(t, r.Client.result, 0)
167168
}
168169

170+
func toJSONString(t *testing.T, v interface{}) string {
171+
t.Helper()
172+
x, err := json.MarshalIndent(v, "", " ")
173+
require.NoError(t, err)
174+
return string(x)
175+
}
176+
177+
func (r *Runner) AssertStep(t *testing.T, resp runner.ChatResponse, err error) {
178+
t.Helper()
179+
r.StepAsserted++
180+
require.NoError(t, err)
181+
r.AssertResponded(t)
182+
autogold.ExpectFile(t, toJSONString(t, resp), autogold.Name(t.Name()+fmt.Sprintf("/step%d", r.StepAsserted)))
183+
}
184+
169185
func (r *Runner) RespondWith(result ...Result) {
170186
r.Client.result = append(r.Client.result, result...)
171187
}

0 commit comments

Comments
 (0)