Skip to content

Commit 5aa1c7f

Browse files
Let's not try to invent, give the spec to the LLM
1 parent c712189 commit 5aa1c7f

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

plugins/agent_bridge_rewrite.go

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -274,42 +274,26 @@ func buildOperationString(operation *openapi3.Operation) (string, error) {
274274

275275
var sb strings.Builder
276276

277-
if operation.Description != "" {
278-
fmt.Fprintf(&sb, "Operation description: %s\n", operation.Description)
279-
} else if operation.Summary != "" {
280-
fmt.Fprintf(&sb, "Operation summary: %s\n", operation.Summary)
277+
278+
operationString, err := operation.MarshalJSON()
279+
if err != nil {
280+
return "", err
281281
}
282282

283+
sb.Write(operationString)
284+
sb.WriteString("\n")
285+
283286
if len(operation.Parameters) > 0 {
284287
getParameterRefs(operation.Parameters, refs)
285-
286-
sb.WriteString("The list of Parameters:\n")
287-
for _, param := range operation.Parameters {
288-
if param == nil || param.Value == nil {
289-
continue
290-
}
291-
m, err := param.Value.MarshalJSON()
292-
if err != nil {
293-
logger.Errorf("[+] Error marshalling parameter: %s", err)
294-
return "", err
295-
}
296-
fmt.Fprintf(&sb, "- %s\n", string(m))
297-
}
298288
}
299289

300290
if operation.RequestBody != nil {
301291
getRequestBodyRefs(operation.RequestBody.Value, refs)
302-
sb.WriteString("The request body:\n")
303-
m, err := operation.RequestBody.Value.MarshalJSON()
304-
if err != nil {
305-
logger.Errorf("[+] Error marshalling request body: %s", err)
306-
return "", err
307-
}
308-
fmt.Fprintf(&sb, "%s\n", string(m))
309292
}
310293

311294
if len(refs) > 0 {
312295
sb.WriteString("The list of References:\n")
296+
sb.WriteString("===\n")
313297

314298
// Sort refs
315299
sortedRefs := make([]string, 0, len(refs))
@@ -327,6 +311,7 @@ func buildOperationString(operation *openapi3.Operation) (string, error) {
327311
}
328312
fmt.Fprintf(&sb, "- %s: %s\n", refName, string(m))
329313
}
314+
sb.WriteString("===\n")
330315
}
331316

332317
return sb.String(), nil
@@ -490,16 +475,17 @@ The OpenAPI operation specification:
490475
func initResponseTemplates() {
491476
var err error
492477

493-
systemPrompt := `Given a API reponse body, and an instruction from a user. You must convert it to a natural language text, according to the user's request.
478+
systemPrompt := `Given a API reponse body, and an instruction from a user.
479+
You must convert the API Response body to a natural language text.
480+
Format the API response according to the original user's request.`
494481

495-
The API response body:
482+
userPrompt := `
483+
The API response:
496484
====
497485
{{.ResponseBody}}
498486
====
499-
`
500-
501-
userPrompt := `The user's request:
502487
488+
The user's request:
503489
====
504490
{{.UserRequest}}
505491
====

plugins/agent_bridge_rewrite_test.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ func TestBuildOperationString(t *testing.T) {
3030
}
3131
}
3232
}`),
33-
"",
33+
`{"responses":null}
34+
`,
3435
},
3536
{
3637
"Simple operation with summary",
@@ -44,7 +45,8 @@ func TestBuildOperationString(t *testing.T) {
4445
"/test": { "get": { "summary": "The request's query parameters." } }
4546
}
4647
}`),
47-
"Operation summary: The request's query parameters.\n",
48+
`{"responses":null,"summary":"The request's query parameters."}
49+
`,
4850
},
4951
{
5052
"Simple operation with description",
@@ -58,7 +60,8 @@ func TestBuildOperationString(t *testing.T) {
5860
"/test": { "get": { "description": "The request's query parameters, with a description." } }
5961
}
6062
}`),
61-
"Operation description: The request's query parameters, with a description.\n",
63+
`{"description":"The request's query parameters, with a description.","responses":null}
64+
`,
6265
},
6366
{
6467
"Simple operation with description and summary, prefer description",
@@ -72,7 +75,8 @@ func TestBuildOperationString(t *testing.T) {
7275
"/test": { "get": { "description": "The request's query parameters, with a description.", "summary": "The request's query parameters summary." } }
7376
}
7477
}`),
75-
"Operation description: The request's query parameters, with a description.\n",
78+
`{"description":"The request's query parameters, with a description.","responses":null,"summary":"The request's query parameters summary."}
79+
`,
7680
},
7781
{
7882
"An operation with a parameter using a $ref",
@@ -144,14 +148,12 @@ func TestBuildOperationString(t *testing.T) {
144148
}
145149
}
146150
}`),
147-
`Operation summary: The request's query parameters.
148-
The list of Parameters:
149-
- {"in":"query","name":"genre"}
150-
- {"description":"The age of the person","in":"header","name":"age","schema":{"$ref":"#/components/schemas/age0"}}
151-
- {"description":"The age of the person","in":"header","name":"name","schema":{"type":"integer"}}
151+
`{"parameters":[{"in":"query","name":"genre"},{"$ref":"#/components/parameters/age"},{"$ref":"#/components/parameters/name"}],"responses":null,"summary":"The request's query parameters."}
152152
The list of References:
153+
===
153154
- #/components/schemas/age: {"format":"int32","type":"integer"}
154155
- #/components/schemas/age0: {"properties":{"age":{"$ref":"#/components/schemas/age"},"romanage":{"type":"string"}},"type":"object"}
156+
===
155157
`,
156158
},
157159
{
@@ -189,14 +191,12 @@ The list of References:
189191
}
190192
}
191193
`),
192-
`Operation summary: The request's query parameters.
193-
The request body:
194-
{"content":{"application/json":{"schema":{"properties":{"url":{"type":"string"}},"required":["url"],"type":"object"}}},"required":true}
194+
`{"requestBody":{"content":{"application/json":{"schema":{"properties":{"url":{"type":"string"}},"required":["url"],"type":"object"}}},"required":true},"responses":null,"summary":"The request's query parameters."}
195195
`,
196196
},
197197
{
198-
"Simple operation - params, request body, with $refs ",
199-
[]byte(`{
198+
"Simple operation - params, request body, with $refs ",
199+
[]byte(`{
200200
"openapi": "3.0.0",
201201
"info": {
202202
"title": "Minimal API",
@@ -286,19 +286,15 @@ The request body:
286286
}
287287
}
288288
`),
289-
`Operation summary: The request's query parameters.
290-
The list of Parameters:
291-
- {"in":"query","name":"genre"}
292-
- {"description":"The age of the person","in":"header","name":"age","schema":{"$ref":"#/components/schemas/age0"}}
293-
- {"description":"The age of the person","in":"header","name":"name","schema":{"type":"integer"}}
294-
The request body:
295-
{"content":{"application/json":{"schema":{"properties":{"age":{"$ref":"#/components/schemas/age"},"city":{"$ref":"#/components/schemas/city"},"url":{"type":"string"}},"required":["url"],"type":"object"}}},"required":true}
289+
`{"parameters":[{"in":"query","name":"genre"},{"$ref":"#/components/parameters/age"},{"$ref":"#/components/parameters/name"}],"requestBody":{"content":{"application/json":{"schema":{"properties":{"age":{"$ref":"#/components/schemas/age"},"city":{"$ref":"#/components/schemas/city"},"url":{"type":"string"}},"required":["url"],"type":"object"}}},"required":true},"responses":null,"summary":"The request's query parameters."}
296290
The list of References:
291+
===
297292
- #/components/schemas/age: {"format":"int32","type":"integer"}
298293
- #/components/schemas/age0: {"properties":{"age":{"$ref":"#/components/schemas/age"},"romanage":{"type":"string"}},"type":"object"}
299294
- #/components/schemas/city: {"type":"string"}
295+
===
300296
`,
301-
},
297+
},
302298
}
303299

304300
for _, tt := range tests {

0 commit comments

Comments
 (0)