Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 7.4 KiBRawPermalink
1import * as fs from "fs";
2import {
3 decodeSecretLocation,
4 FQSN,
5 PackageIdentifier,
6 packageIdentifierToShorthandSlug,
7 PlatformClient,
8 PlatformSecretStore,
9 Registry,
10 resolveFQSN,
11 resolveSecretLocationInProxy,
12 SecretLocation,
13 SecretResult,
14 SecretStore,
15 SecretType,
16 unrollAssistant,
17} from "../index.js";
18
19// Test e2e flows from raw yaml -> unroll -> client render -> resolve secrets on proxy
20describe("E2E Scenarios", () => {
21 const userSecrets: Record<string, string> = {
22 OPENAI_API_KEY: "sk-123",
23 };
24
25 const orgSecrets: Record<string, string> = {
26 GEMINI_API_KEY: "gemini-api-key",
27 };
28
29 const proxyEnvSecrets: Record<string, string> = {
30 ANTHROPIC_API_KEY: "sk-ant-env",
31 GEMINI_API_KEY: "gemini-api-key-env",
32 };
33
34 const localUserSecretStore: SecretStore = {
35 get: async function (secretName: string): Promise<string | undefined> {
36 return userSecrets[secretName];
37 },
38 set: function (secretName: string, secretValue: string): Promise<void> {
39 throw new Error("Function not implemented.");
40 },
41 };
42
43 const platformClient: PlatformClient = {
44 resolveFQSNs: async function (
45 fqsns: FQSN[],
46 ): Promise<(SecretResult | undefined)[]> {
47 return await Promise.all(
48 fqsns.map((fqsn) =>
49 resolveFQSN("test-user", fqsn, platformSecretStore, "test-org"),
50 ),
51 );
52 },
53 };
54
55 const environmentSecretStore: SecretStore = {
56 get: async function (secretName: string): Promise<string | undefined> {
57 return proxyEnvSecrets[secretName];
58 },
59 set: function (secretName: string, secretValue: string): Promise<void> {
60 throw new Error("Function not implemented.");
61 },
62 };
63
64 const platformSecretStore: PlatformSecretStore = {
65 getSecretFromSecretLocation: async function (
66 secretLocation: SecretLocation,
67 ): Promise<string | undefined> {
68 switch (secretLocation.secretType) {
69 case SecretType.Package:
70 return undefined;
71 case SecretType.User:
72 return userSecrets[secretLocation.secretName];
73 case SecretType.Organization:
74 return orgSecrets[secretLocation.secretName];
75 case SecretType.ModelsAddOn:
76 case SecretType.FreeTrial:
77 if (
78 secretLocation.blockSlug.ownerSlug === "test-org" &&
79 secretLocation.blockSlug.packageSlug === "claude35sonnet" &&
80 secretLocation.secretName === "ANTHROPIC_API_KEY"
81 ) {
82 return "sk-ant";
83 }
84 return undefined;
85 case SecretType.NotFound:
86 return undefined;
87 default:
88 return undefined;
89 }
90 },
91 };
92
93 const registry: Registry = {
94 getContent: async function (id: PackageIdentifier): Promise<string> {
95 const slug = packageIdentifierToShorthandSlug(id);
96 return fs
97 .readFileSync(`./src/__tests__/packages/${slug}.yaml`)
98 .toString();
99 },
100 };
101
102 it("should unroll assistant with a single block that doesn't exist", async () => {
103 const unrolledConfig = await unrollAssistant(
104 {
105 uriType: "slug",
106 fullSlug: {
107 ownerSlug: "test-org",
108 packageSlug: "assistant-with-non-existing-block",
109 versionSlug: "latest",
110 },
111 },
112 registry,
113 {
114 renderSecrets: true,
115 platformClient,
116 orgScopeId: "test-org",
117 currentUserSlug: "test-user",
118 onPremProxyUrl: null,
119 },
120 );
121
122 expect(unrolledConfig.rules?.[0]).toBeNull();
123 });
124
125 it("should correctly unroll assistant", async () => {
126 const unrolledConfig = await unrollAssistant(
127 {
128 uriType: "slug",
129 fullSlug: {
130 ownerSlug: "test-org",
131 packageSlug: "assistant",
132 versionSlug: "latest",
133 },
134 },
135 registry,
136 {
137 renderSecrets: true,
138 platformClient,
139 orgScopeId: "test-org",
140 currentUserSlug: "test-user",
141 onPremProxyUrl: null,
142 },
143 );
144
145 // Test that packages were correctly unrolled and params replaced
146 expect(unrolledConfig.models?.length).toBe(3);
147
148 const openAiModel = unrolledConfig.models?.[0]!;
149 expect(openAiModel.apiKey).toBe("sk-123");
150
151 const geminiModel = unrolledConfig.models?.[1]!;
152 expect(geminiModel.provider).toBe("continue-proxy");
153 expect(geminiModel.apiKey).toBeUndefined();
154 const geminiSecretLocation = "organization:test-org/GEMINI_API_KEY";
155 expect((geminiModel as any).apiKeyLocation).toBe(geminiSecretLocation);
156
157 const anthropicModel = unrolledConfig.models?.[2]!;
158 expect(anthropicModel.provider).toBe("continue-proxy");
159 expect(anthropicModel.apiKey).toBeUndefined();
160 const anthropicSecretLocation =
161 "models_add_on:test-org/claude35sonnet/ANTHROPIC_API_KEY";
162 expect((anthropicModel as any).apiKeyLocation).toBe(
163 anthropicSecretLocation,
164 );
165
166 expect(unrolledConfig.rules?.length).toBe(2);
167 expect(unrolledConfig.docs?.[0]?.startUrl).toBe(
168 "https://docs.python.org/release/3.13.1",
169 );
170 expect(unrolledConfig.docs?.[0]?.rootUrl).toBe(
171 "https://docs.python.org/release/3.13.1",
172 );
173
174 // Test that proxy can correctly resolve secrets
175 const decodedAnthropicSecretLocation = decodeSecretLocation(
176 anthropicSecretLocation,
177 );
178 const decodedGeminiSecretLocation =
179 decodeSecretLocation(geminiSecretLocation);
180
181 // With environment
182 const antSecretValue = await resolveSecretLocationInProxy(
183 decodedAnthropicSecretLocation,
184 platformSecretStore,
185 environmentSecretStore,
186 );
187 expect(antSecretValue).toBe("sk-ant-env");
188 const geminiSecretValue = await resolveSecretLocationInProxy(
189 decodedGeminiSecretLocation,
190 platformSecretStore,
191 environmentSecretStore,
192 );
193 expect(geminiSecretValue).toBe("gemini-api-key-env");
194
195 // Without environment
196 const antSecretValue2 = await resolveSecretLocationInProxy(
197 decodedAnthropicSecretLocation,
198 platformSecretStore,
199 undefined,
200 );
201 expect(antSecretValue2).toBe("sk-ant");
202 const geminiSecretValue2 = await resolveSecretLocationInProxy(
203 decodedGeminiSecretLocation,
204 platformSecretStore,
205 undefined,
206 );
207 expect(geminiSecretValue2).toBe("gemini-api-key");
208 });
209
210 it("should correctly unroll assistant with injected blocks", async () => {
211 const unrolledConfig = await unrollAssistant(
212 {
213 uriType: "slug",
214 fullSlug: {
215 ownerSlug: "test-org",
216 packageSlug: "assistant",
217 versionSlug: "latest",
218 },
219 },
220 registry,
221 {
222 renderSecrets: true,
223 platformClient,
224 orgScopeId: "test-org",
225 currentUserSlug: "test-user",
226 onPremProxyUrl: null,
227 // Add an injected block
228 injectBlocks: [
229 {
230 uriType: "slug",
231 fullSlug: {
232 ownerSlug: "test-org",
233 packageSlug: "rules",
234 versionSlug: "latest",
235 },
236 },
237 ],
238 },
239 );
240
241 // The original rules array should have two items
242 expect(unrolledConfig.rules?.length).toBe(3); // Now 3 with the injected block
243
244 // Check the original doc is still there
245 expect(unrolledConfig.docs?.[0]?.startUrl).toBe(
246 "https://docs.python.org/release/3.13.1",
247 );
248
249 // Check the injected doc block was added
250 expect(unrolledConfig.rules?.[2]).toBe("Be kind");
251 });
252
253 it.skip("should prioritize org over user / package secrets", () => {});
254});