Skip to content

Commit

Permalink
fix: address templates when no load is specified (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Jun 14, 2024
1 parent 248fe10 commit 8a648e3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
37 changes: 36 additions & 1 deletion packages/template-resolver/src/SnippetTemplateResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
)"
`;
14 changes: 14 additions & 0 deletions packages/template-resolver/src/__test__/unit-tests/cohere.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 8a648e3

Please sign in to comment.