diff --git a/packages/template-resolver/src/SnippetTemplateResolver.ts b/packages/template-resolver/src/SnippetTemplateResolver.ts index a3c86900db..61bb234208 100644 --- a/packages/template-resolver/src/SnippetTemplateResolver.ts +++ b/packages/template-resolver/src/SnippetTemplateResolver.ts @@ -235,9 +235,44 @@ export class SnippetTemplateResolver { } } + private isPayloadEmpty(): boolean { + return ( + this.payload.requestBody == null && + this.payload.queryParameters == null && + this.payload.pathParameters == null && + this.payload.headers == null + ); + } + private resolveSnippetV1TemplateString(template: SnippetTemplate): string { const clientSnippet = template.clientInstantiation; - const endpointSnippet = this.resolveV1Template(template.functionInvocation); + let endpointSnippet: V1Snippet | undefined; + if (this.isPayloadEmpty()) { + let invocation: string; + + // The top level template should really be generic, but we're not enforcing that right now. + switch (template.functionInvocation.type) { + case "generic": + case "discriminatedUnion": + case "union": + case "enum": + invocation = template.functionInvocation.templateString?.replace(TemplateSentinel, "") ?? ""; + break; + case "dict": + invocation = "{}"; + break; + case "iterable": + invocation = template.functionInvocation.containerTemplateString.replace(TemplateSentinel, ""); + break; + } + + endpointSnippet = { + imports: template.functionInvocation.imports ?? [], + invocation, + }; + } else { + endpointSnippet = this.resolveV1Template(template.functionInvocation); + } // TODO: We should split the Snippet data model to return these independently // so there's more flexibility on the consumer end to decide how to use them. diff --git a/packages/template-resolver/src/__test__/unit-tests/__snapshots__/cohere.test.ts.snap b/packages/template-resolver/src/__test__/unit-tests/__snapshots__/cohere.test.ts.snap index b1450c7f93..a667e56dd7 100644 --- a/packages/template-resolver/src/__test__/unit-tests/__snapshots__/cohere.test.ts.snap +++ b/packages/template-resolver/src/__test__/unit-tests/__snapshots__/cohere.test.ts.snap @@ -11,3 +11,13 @@ await cohere.chatStream( } )" `; + +exports[`Snippet Template Resolver > Test empty payload 1`] = ` +" + +const cohere = new CohereClient({ token: "YOUR_TOKEN", clientName: "YOUR_CLIENT_NAME" }); + +await cohere.chatStream( + +)" +`; diff --git a/packages/template-resolver/src/__test__/unit-tests/cohere.test.ts b/packages/template-resolver/src/__test__/unit-tests/cohere.test.ts index ce97efcb44..1d5461fd7c 100644 --- a/packages/template-resolver/src/__test__/unit-tests/cohere.test.ts +++ b/packages/template-resolver/src/__test__/unit-tests/cohere.test.ts @@ -19,4 +19,18 @@ describe("Snippet Template Resolver", () => { expect(customSnippet.client).toMatchSnapshot(); }); + + it("Test empty payload", () => { + const resolver = new SnippetTemplateResolver({ + payload: {}, + endpointSnippetTemplate: CHAT_COMPLETION_SNIPPET, + }); + const customSnippet = resolver.resolve(); + + if (customSnippet.type !== "typescript") { + throw new Error("Expected snippet to be typescript"); + } + + expect(customSnippet.client).toMatchSnapshot(); + }); });