| 1 | import { RequestOptions } from "../browser.js"; |
| 2 | import { AssistantUnrolled, ConfigYaml } from "../schemas/index.js"; |
| 3 | |
| 4 | export function mergePackages( |
| 5 | current: ConfigYaml, |
| 6 | incoming: ConfigYaml, |
| 7 | ): ConfigYaml { |
| 8 | return { |
| 9 | ...current, |
| 10 | models: [...(current.models ?? []), ...(incoming.models ?? [])], |
| 11 | context: [...(current.context ?? []), ...(incoming.context ?? [])], |
| 12 | data: [...(current.data ?? []), ...(incoming.data ?? [])], |
| 13 | mcpServers: [...(current.mcpServers ?? []), ...(incoming.mcpServers ?? [])], |
| 14 | rules: [...(current.rules ?? []), ...(incoming.rules ?? [])], |
| 15 | prompts: [...(current.prompts ?? []), ...(incoming.prompts ?? [])], |
| 16 | docs: [...(current.docs ?? []), ...(incoming.docs ?? [])], |
| 17 | env: { ...current.env, ...incoming.env }, |
| 18 | requestOptions: mergeConfigYamlRequestOptions( |
| 19 | current.requestOptions, |
| 20 | incoming.requestOptions, |
| 21 | ), |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | export function mergeUnrolledAssistants( |
| 26 | current: AssistantUnrolled, |
| 27 | incoming: AssistantUnrolled, |
| 28 | ): AssistantUnrolled { |
| 29 | return { |
| 30 | ...current, |
| 31 | rules: [...(current.rules ?? []), ...(incoming.rules ?? [])], |
| 32 | models: [...(current.models ?? []), ...(incoming.models ?? [])], |
| 33 | docs: [...(current.docs ?? []), ...(incoming.docs ?? [])], |
| 34 | context: [...(current.context ?? []), ...(incoming.context ?? [])], |
| 35 | data: [...(current.data ?? []), ...(incoming.data ?? [])], |
| 36 | mcpServers: [...(current.mcpServers ?? []), ...(incoming.mcpServers ?? [])], |
| 37 | prompts: [...(current.prompts ?? []), ...(incoming.prompts ?? [])], |
| 38 | env: { ...current.env, ...incoming.env }, |
| 39 | requestOptions: mergeConfigYamlRequestOptions( |
| 40 | current.requestOptions, |
| 41 | incoming.requestOptions, |
| 42 | ), |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | export function mergeConfigYamlRequestOptions( |
| 47 | base: RequestOptions | undefined, |
| 48 | global: RequestOptions | undefined, |
| 49 | ): RequestOptions | undefined { |
| 50 | if (!base && !global) { |
| 51 | return undefined; |
| 52 | } |
| 53 | if (!base) { |
| 54 | return global; |
| 55 | } |
| 56 | if (!global) { |
| 57 | return base; |
| 58 | } |
| 59 | |
| 60 | const headers = { |
| 61 | ...global.headers, |
| 62 | ...base.headers, |
| 63 | }; |
| 64 | |
| 65 | return { |
| 66 | ...global, |
| 67 | ...base, // base overrides for simple values as well as noProxy and extraBodyProperties |
| 68 | headers: Object.keys(headers).length === 0 ? undefined : headers, // headers are the only thing that really merge |
| 69 | }; |
| 70 | } |